blob: f2efc8c5be7fbd6a75f7f4773b095ec9c63037d3 [file] [log] [blame]
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -07001/*
2 * the_nilfs.c - the_nilfs shared structure.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
21 *
22 */
23
24#include <linux/buffer_head.h>
25#include <linux/slab.h>
26#include <linux/blkdev.h>
27#include <linux/backing-dev.h>
Ryusuke Konishie339ad32009-04-06 19:01:59 -070028#include <linux/crc32.h>
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070029#include "nilfs.h"
30#include "segment.h"
31#include "alloc.h"
32#include "cpfile.h"
33#include "sufile.h"
34#include "dat.h"
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070035#include "segbuf.h"
36
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090037
38static LIST_HEAD(nilfs_objects);
39static DEFINE_SPINLOCK(nilfs_lock);
40
Ryusuke Konishi6c1251602010-06-28 19:15:26 +090041static int nilfs_valid_sb(struct nilfs_super_block *sbp);
42
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070043void nilfs_set_last_segment(struct the_nilfs *nilfs,
44 sector_t start_blocknr, u64 seq, __u64 cno)
45{
46 spin_lock(&nilfs->ns_last_segment_lock);
47 nilfs->ns_last_pseg = start_blocknr;
48 nilfs->ns_last_seq = seq;
49 nilfs->ns_last_cno = cno;
Ryusuke Konishi32502042010-06-29 14:42:13 +090050
51 if (!nilfs_sb_dirty(nilfs)) {
52 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
53 goto stay_cursor;
54
55 set_nilfs_sb_dirty(nilfs);
56 }
57 nilfs->ns_prev_seq = nilfs->ns_last_seq;
58
59 stay_cursor:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070060 spin_unlock(&nilfs->ns_last_segment_lock);
61}
62
63/**
64 * alloc_nilfs - allocate the_nilfs structure
65 * @bdev: block device to which the_nilfs is related
66 *
67 * alloc_nilfs() allocates memory for the_nilfs and
68 * initializes its reference count and locks.
69 *
70 * Return Value: On success, pointer to the_nilfs is returned.
71 * On error, NULL is returned.
72 */
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090073static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070074{
75 struct the_nilfs *nilfs;
76
77 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
78 if (!nilfs)
79 return NULL;
80
81 nilfs->ns_bdev = bdev;
82 atomic_set(&nilfs->ns_count, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070083 atomic_set(&nilfs->ns_ndirtyblks, 0);
84 init_rwsem(&nilfs->ns_sem);
Ryusuke Konishie59399d2009-06-08 01:39:32 +090085 init_rwsem(&nilfs->ns_super_sem);
Ryusuke Konishiaa7dfb82009-06-08 01:39:33 +090086 mutex_init(&nilfs->ns_mount_mutex);
Ryusuke Konishi027d6402009-08-02 22:45:33 +090087 init_rwsem(&nilfs->ns_writer_sem);
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090088 INIT_LIST_HEAD(&nilfs->ns_list);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070089 INIT_LIST_HEAD(&nilfs->ns_supers);
90 spin_lock_init(&nilfs->ns_last_segment_lock);
91 nilfs->ns_gc_inodes_h = NULL;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070092 init_rwsem(&nilfs->ns_segctor_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070093
94 return nilfs;
95}
96
97/**
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090098 * find_or_create_nilfs - find or create nilfs object
99 * @bdev: block device to which the_nilfs is related
100 *
101 * find_nilfs() looks up an existent nilfs object created on the
102 * device and gets the reference count of the object. If no nilfs object
103 * is found on the device, a new nilfs object is allocated.
104 *
105 * Return Value: On success, pointer to the nilfs object is returned.
106 * On error, NULL is returned.
107 */
108struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
109{
110 struct the_nilfs *nilfs, *new = NULL;
111
112 retry:
113 spin_lock(&nilfs_lock);
114 list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
115 if (nilfs->ns_bdev == bdev) {
116 get_nilfs(nilfs);
117 spin_unlock(&nilfs_lock);
118 if (new)
119 put_nilfs(new);
120 return nilfs; /* existing object */
121 }
122 }
123 if (new) {
124 list_add_tail(&new->ns_list, &nilfs_objects);
125 spin_unlock(&nilfs_lock);
126 return new; /* new object */
127 }
128 spin_unlock(&nilfs_lock);
129
130 new = alloc_nilfs(bdev);
131 if (new)
132 goto retry;
133 return NULL; /* insufficient memory */
134}
135
136/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700137 * put_nilfs - release a reference to the_nilfs
138 * @nilfs: the_nilfs structure to be released
139 *
140 * put_nilfs() decrements a reference counter of the_nilfs.
141 * If the reference count reaches zero, the_nilfs is freed.
142 */
143void put_nilfs(struct the_nilfs *nilfs)
144{
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900145 spin_lock(&nilfs_lock);
146 if (!atomic_dec_and_test(&nilfs->ns_count)) {
147 spin_unlock(&nilfs_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700148 return;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900149 }
150 list_del_init(&nilfs->ns_list);
151 spin_unlock(&nilfs_lock);
152
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700153 /*
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900154 * Increment of ns_count never occurs below because the caller
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700155 * of get_nilfs() holds at least one reference to the_nilfs.
156 * Thus its exclusion control is not required here.
157 */
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900158
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700159 might_sleep();
160 if (nilfs_loaded(nilfs)) {
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700161 nilfs_mdt_destroy(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700162 nilfs_mdt_destroy(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700163 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700164 nilfs_mdt_destroy(nilfs->ns_gc_dat);
165 }
166 if (nilfs_init(nilfs)) {
167 nilfs_destroy_gccache(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700168 brelse(nilfs->ns_sbh[0]);
169 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700170 }
171 kfree(nilfs);
172}
173
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900174static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700175{
176 struct buffer_head *bh_sr;
177 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700178 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700179 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
180 unsigned inode_size;
181 int err;
182
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900183 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700184 if (unlikely(err))
185 return err;
186
187 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700188 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
189 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
190 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700191 up_read(&nilfs->ns_sem);
192
193 inode_size = nilfs->ns_inode_size;
194
195 err = -ENOMEM;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900196 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700197 if (unlikely(!nilfs->ns_dat))
198 goto failed;
199
Ryusuke Konishi79739562009-11-12 23:56:43 +0900200 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700201 if (unlikely(!nilfs->ns_gc_dat))
202 goto failed_dat;
203
Ryusuke Konishi79739562009-11-12 23:56:43 +0900204 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700205 if (unlikely(!nilfs->ns_cpfile))
206 goto failed_gc_dat;
207
Ryusuke Konishi79739562009-11-12 23:56:43 +0900208 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700209 if (unlikely(!nilfs->ns_sufile))
210 goto failed_cpfile;
211
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700212 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700213
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900214 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
215 NILFS_SR_DAT_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700216 if (unlikely(err))
217 goto failed_sufile;
218
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900219 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
220 NILFS_SR_CPFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700221 if (unlikely(err))
222 goto failed_sufile;
223
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900224 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
225 NILFS_SR_SUFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700226 if (unlikely(err))
227 goto failed_sufile;
228
229 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
230 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
231
232 failed:
233 brelse(bh_sr);
234 return err;
235
236 failed_sufile:
237 nilfs_mdt_destroy(nilfs->ns_sufile);
238
239 failed_cpfile:
240 nilfs_mdt_destroy(nilfs->ns_cpfile);
241
242 failed_gc_dat:
243 nilfs_mdt_destroy(nilfs->ns_gc_dat);
244
245 failed_dat:
246 nilfs_mdt_destroy(nilfs->ns_dat);
247 goto failed;
248}
249
250static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
251{
252 memset(ri, 0, sizeof(*ri));
253 INIT_LIST_HEAD(&ri->ri_used_segments);
254}
255
256static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
257{
258 nilfs_dispose_segment_list(&ri->ri_used_segments);
259}
260
261/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900262 * nilfs_store_log_cursor - load log cursor from a super block
263 * @nilfs: nilfs object
264 * @sbp: buffer storing super block to be read
265 *
266 * nilfs_store_log_cursor() reads the last position of the log
267 * containing a super root from a given super block, and initializes
268 * relevant information on the nilfs object preparatory for log
269 * scanning and recovery.
270 */
271static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
272 struct nilfs_super_block *sbp)
273{
274 int ret = 0;
275
276 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
277 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
278 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
279
Ryusuke Konishi32502042010-06-29 14:42:13 +0900280 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900281 nilfs->ns_seg_seq = nilfs->ns_last_seq;
282 nilfs->ns_segnum =
283 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
284 nilfs->ns_cno = nilfs->ns_last_cno + 1;
285 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
286 printk(KERN_ERR "NILFS invalid last segment number.\n");
287 ret = -EINVAL;
288 }
289 return ret;
290}
291
292/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700293 * load_nilfs - load and recover the nilfs
294 * @nilfs: the_nilfs structure to be released
295 * @sbi: nilfs_sb_info used to recover past segment
296 *
297 * load_nilfs() searches and load the latest super root,
298 * attaches the last segment, and does recovery if needed.
299 * The caller must call this exclusively for simultaneous mounts.
300 */
301int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
302{
303 struct nilfs_recovery_info ri;
304 unsigned int s_flags = sbi->s_super->s_flags;
305 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900306 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900307 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700308
Ryusuke Konishi02345762009-11-20 03:28:01 +0900309 if (nilfs_loaded(nilfs)) {
310 if (valid_fs ||
311 ((s_flags & MS_RDONLY) && nilfs_test_opt(sbi, NORECOVERY)))
312 return 0;
313 printk(KERN_ERR "NILFS: the filesystem is in an incomplete "
314 "recovery state.\n");
315 return -EINVAL;
316 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700317
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900318 if (!valid_fs) {
319 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
320 if (s_flags & MS_RDONLY) {
321 printk(KERN_INFO "NILFS: INFO: recovery "
322 "required for readonly filesystem.\n");
323 printk(KERN_INFO "NILFS: write access will "
324 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700325 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700326 }
327
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900328 nilfs_init_recovery_info(&ri);
329
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900330 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700331 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900332 struct nilfs_super_block **sbp = nilfs->ns_sbp;
333 int blocksize;
334
335 if (err != -EINVAL)
336 goto scan_error;
337
338 if (!nilfs_valid_sb(sbp[1])) {
339 printk(KERN_WARNING
340 "NILFS warning: unable to fall back to spare"
341 "super block\n");
342 goto scan_error;
343 }
344 printk(KERN_INFO
345 "NILFS: try rollback from an earlier position\n");
346
347 /*
348 * restore super block with its spare and reconfigure
349 * relevant states of the nilfs object.
350 */
351 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
352 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
353 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
354
355 /* verify consistency between two super blocks */
356 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
357 if (blocksize != nilfs->ns_blocksize) {
358 printk(KERN_WARNING
359 "NILFS warning: blocksize differs between "
360 "two super blocks (%d != %d)\n",
361 blocksize, nilfs->ns_blocksize);
362 goto scan_error;
363 }
364
365 err = nilfs_store_log_cursor(nilfs, sbp[0]);
366 if (err)
367 goto scan_error;
368
369 /* drop clean flag to allow roll-forward and recovery */
370 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
371 valid_fs = 0;
372
373 err = nilfs_search_super_root(nilfs, &ri);
374 if (err)
375 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700376 }
377
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900378 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700379 if (unlikely(err)) {
380 printk(KERN_ERR "NILFS: error loading super root.\n");
381 goto failed;
382 }
383
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900384 if (valid_fs)
385 goto skip_recovery;
386
387 if (s_flags & MS_RDONLY) {
Ryusuke Konishi02345762009-11-20 03:28:01 +0900388 if (nilfs_test_opt(sbi, NORECOVERY)) {
389 printk(KERN_INFO "NILFS: norecovery option specified. "
390 "skipping roll-forward recovery\n");
391 goto skip_recovery;
392 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900393 if (really_read_only) {
394 printk(KERN_ERR "NILFS: write access "
395 "unavailable, cannot proceed.\n");
396 err = -EROFS;
397 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700398 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900399 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900400 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
401 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
402 "option was specified for a read/write mount\n");
403 err = -EINVAL;
404 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700405 }
406
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900407 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900408 if (err)
409 goto failed_unload;
410
411 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900412 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
413 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900414 up_write(&nilfs->ns_sem);
415
416 if (err) {
417 printk(KERN_ERR "NILFS: failed to update super block. "
418 "recovery unfinished.\n");
419 goto failed_unload;
420 }
421 printk(KERN_INFO "NILFS: recovery complete.\n");
422
423 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700424 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900425 nilfs_clear_recovery_info(&ri);
426 sbi->s_super->s_flags = s_flags;
427 return 0;
428
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900429 scan_error:
430 printk(KERN_ERR "NILFS: error searching super root.\n");
431 goto failed;
432
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900433 failed_unload:
434 nilfs_mdt_destroy(nilfs->ns_cpfile);
435 nilfs_mdt_destroy(nilfs->ns_sufile);
436 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700437
438 failed:
439 nilfs_clear_recovery_info(&ri);
440 sbi->s_super->s_flags = s_flags;
441 return err;
442}
443
444static unsigned long long nilfs_max_size(unsigned int blkbits)
445{
446 unsigned int max_bits;
447 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
448
449 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
450 if (max_bits < 64)
451 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
452 return res;
453}
454
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700455static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
456 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700457{
458 if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
459 printk(KERN_ERR "NILFS: revision mismatch "
460 "(superblock rev.=%d.%d, current rev.=%d.%d). "
461 "Please check the version of mkfs.nilfs.\n",
462 le32_to_cpu(sbp->s_rev_level),
463 le16_to_cpu(sbp->s_minor_rev_level),
464 NILFS_CURRENT_REV, NILFS_MINOR_REV);
465 return -EINVAL;
466 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700467 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
468 if (nilfs->ns_sbsize > BLOCK_SIZE)
469 return -EINVAL;
470
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700471 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
472 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
473
474 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
475 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900476 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700477 return -EINVAL;
478 }
479
480 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
481 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
482 nilfs->ns_r_segments_percentage =
483 le32_to_cpu(sbp->s_r_segments_percentage);
484 nilfs->ns_nrsvsegs =
485 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
486 DIV_ROUND_UP(nilfs->ns_nsegments *
487 nilfs->ns_r_segments_percentage, 100));
488 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
489 return 0;
490}
491
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700492static int nilfs_valid_sb(struct nilfs_super_block *sbp)
493{
494 static unsigned char sum[4];
495 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
496 size_t bytes;
497 u32 crc;
498
499 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
500 return 0;
501 bytes = le16_to_cpu(sbp->s_bytes);
502 if (bytes > BLOCK_SIZE)
503 return 0;
504 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
505 sumoff);
506 crc = crc32_le(crc, sum, 4);
507 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
508 bytes - sumoff - 4);
509 return crc == le32_to_cpu(sbp->s_sum);
510}
511
512static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
513{
514 return offset < ((le64_to_cpu(sbp->s_nsegments) *
515 le32_to_cpu(sbp->s_blocks_per_segment)) <<
516 (le32_to_cpu(sbp->s_log_block_size) + 10));
517}
518
519static void nilfs_release_super_block(struct the_nilfs *nilfs)
520{
521 int i;
522
523 for (i = 0; i < 2; i++) {
524 if (nilfs->ns_sbp[i]) {
525 brelse(nilfs->ns_sbh[i]);
526 nilfs->ns_sbh[i] = NULL;
527 nilfs->ns_sbp[i] = NULL;
528 }
529 }
530}
531
532void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
533{
534 brelse(nilfs->ns_sbh[0]);
535 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
536 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
537 nilfs->ns_sbh[1] = NULL;
538 nilfs->ns_sbp[1] = NULL;
539}
540
541void nilfs_swap_super_block(struct the_nilfs *nilfs)
542{
543 struct buffer_head *tsbh = nilfs->ns_sbh[0];
544 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
545
546 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
547 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
548 nilfs->ns_sbh[1] = tsbh;
549 nilfs->ns_sbp[1] = tsbp;
550}
551
552static int nilfs_load_super_block(struct the_nilfs *nilfs,
553 struct super_block *sb, int blocksize,
554 struct nilfs_super_block **sbpp)
555{
556 struct nilfs_super_block **sbp = nilfs->ns_sbp;
557 struct buffer_head **sbh = nilfs->ns_sbh;
558 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
559 int valid[2], swp = 0;
560
561 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
562 &sbh[0]);
563 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
564
565 if (!sbp[0]) {
566 if (!sbp[1]) {
567 printk(KERN_ERR "NILFS: unable to read superblock\n");
568 return -EIO;
569 }
570 printk(KERN_WARNING
571 "NILFS warning: unable to read primary superblock\n");
572 } else if (!sbp[1])
573 printk(KERN_WARNING
574 "NILFS warning: unable to read secondary superblock\n");
575
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900576 /*
577 * Compare two super blocks and set 1 in swp if the secondary
578 * super block is valid and newer. Otherwise, set 0 in swp.
579 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700580 valid[0] = nilfs_valid_sb(sbp[0]);
581 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900582 swp = valid[1] && (!valid[0] ||
583 le64_to_cpu(sbp[1]->s_last_cno) >
584 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700585
586 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
587 brelse(sbh[1]);
588 sbh[1] = NULL;
589 sbp[1] = NULL;
590 swp = 0;
591 }
592 if (!valid[swp]) {
593 nilfs_release_super_block(nilfs);
594 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
595 sb->s_id);
596 return -EINVAL;
597 }
598
599 if (swp) {
600 printk(KERN_WARNING "NILFS warning: broken superblock. "
601 "using spare superblock.\n");
602 nilfs_swap_super_block(nilfs);
603 }
604
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900605 nilfs->ns_sbwcount = 0;
606 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700607 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
608 *sbpp = sbp[0];
609 return 0;
610}
611
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700612/**
613 * init_nilfs - initialize a NILFS instance.
614 * @nilfs: the_nilfs structure
615 * @sbi: nilfs_sb_info
616 * @sb: super block
617 * @data: mount options
618 *
619 * init_nilfs() performs common initialization per block device (e.g.
620 * reading the super block, getting disk layout information, initializing
621 * shared fields in the_nilfs). It takes on some portion of the jobs
622 * typically done by a fill_super() routine. This division arises from
623 * the nature that multiple NILFS instances may be simultaneously
624 * mounted on a device.
625 * For multiple mounts on the same device, only the first mount
626 * invokes these tasks.
627 *
628 * Return Value: On success, 0 is returned. On error, a negative error
629 * code is returned.
630 */
631int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
632{
633 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700634 struct nilfs_super_block *sbp;
635 struct backing_dev_info *bdi;
636 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700637 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700638
639 down_write(&nilfs->ns_sem);
640 if (nilfs_init(nilfs)) {
641 /* Load values from existing the_nilfs */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700642 sbp = nilfs->ns_sbp[0];
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700643 err = nilfs_store_magic_and_option(sb, sbp, data);
644 if (err)
645 goto out;
646
647 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
648 if (sb->s_blocksize != blocksize &&
649 !sb_set_blocksize(sb, blocksize)) {
650 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
651 blocksize);
652 err = -EINVAL;
653 }
654 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
655 goto out;
656 }
657
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700658 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
659 if (!blocksize) {
660 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700661 err = -EINVAL;
662 goto out;
663 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700664 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
665 if (err)
666 goto out;
667
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700668 err = nilfs_store_magic_and_option(sb, sbp, data);
669 if (err)
670 goto failed_sbh;
671
672 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
673 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400674 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700675
676 if (blocksize < hw_blocksize) {
677 printk(KERN_ERR
678 "NILFS: blocksize %d too small for device "
679 "(sector-size = %d).\n",
680 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700681 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700682 goto failed_sbh;
683 }
684 nilfs_release_super_block(nilfs);
685 sb_set_blocksize(sb, blocksize);
686
687 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
688 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700689 goto out;
690 /* not failed_sbh; sbh is released automatically
691 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700692 }
693 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900694 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700695
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700696 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700697 if (err)
698 goto failed_sbh;
699
700 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
701
702 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700703
Jens Axboe2c96ce92009-09-15 09:43:56 +0200704 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700705 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
706
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900707 err = nilfs_store_log_cursor(nilfs, sbp);
708 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700709 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700710
711 /* Initialize gcinode cache */
712 err = nilfs_init_gccache(nilfs);
713 if (err)
714 goto failed_sbh;
715
716 set_nilfs_init(nilfs);
717 err = 0;
718 out:
719 up_write(&nilfs->ns_sem);
720 return err;
721
722 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700723 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700724 goto out;
725}
726
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900727int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
728 size_t nsegs)
729{
730 sector_t seg_start, seg_end;
731 sector_t start = 0, nblocks = 0;
732 unsigned int sects_per_block;
733 __u64 *sn;
734 int ret = 0;
735
736 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
737 bdev_logical_block_size(nilfs->ns_bdev);
738 for (sn = segnump; sn < segnump + nsegs; sn++) {
739 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
740
741 if (!nblocks) {
742 start = seg_start;
743 nblocks = seg_end - seg_start + 1;
744 } else if (start + nblocks == seg_start) {
745 nblocks += seg_end - seg_start + 1;
746 } else {
747 ret = blkdev_issue_discard(nilfs->ns_bdev,
748 start * sects_per_block,
749 nblocks * sects_per_block,
750 GFP_NOFS,
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200751 BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900752 if (ret < 0)
753 return ret;
754 nblocks = 0;
755 }
756 }
757 if (nblocks)
758 ret = blkdev_issue_discard(nilfs->ns_bdev,
759 start * sects_per_block,
760 nblocks * sects_per_block,
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200761 GFP_NOFS, BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900762 return ret;
763}
764
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700765int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
766{
767 struct inode *dat = nilfs_dat_inode(nilfs);
768 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700769
770 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900771 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700772 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900773 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
774 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700775}
776
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700777int nilfs_near_disk_full(struct the_nilfs *nilfs)
778{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700779 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700780
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900781 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
782 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
783 nilfs->ns_blocks_per_segment + 1;
784
785 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700786}
787
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900788/**
789 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
790 * @nilfs: nilfs object
791 * @rw_mount: mount type (non-zero value for read/write mount)
792 * @cno: checkpoint number (zero for read-only mount)
793 *
794 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
795 * @rw_mount and @cno (in case of snapshots) matched. If no instance
796 * was found, NULL is returned. Although the super block instance can
797 * be unmounted after this function returns, the nilfs_sb_info struct
798 * is kept on memory until nilfs_put_sbinfo() is called.
799 */
800struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
801 int rw_mount, __u64 cno)
802{
803 struct nilfs_sb_info *sbi;
804
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900805 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900806 /*
807 * The SNAPSHOT flag and sb->s_flags are supposed to be
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900808 * protected with nilfs->ns_super_sem.
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900809 */
810 sbi = nilfs->ns_current;
811 if (rw_mount) {
812 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
813 goto found; /* read/write mount */
814 else
815 goto out;
816 } else if (cno == 0) {
817 if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
818 goto found; /* read-only mount */
819 else
820 goto out;
821 }
822
823 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
824 if (nilfs_test_opt(sbi, SNAPSHOT) &&
825 sbi->s_snapshot_cno == cno)
826 goto found; /* snapshot mount */
827 }
828 out:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900829 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900830 return NULL;
831
832 found:
833 atomic_inc(&sbi->s_count);
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900834 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900835 return sbi;
836}
837
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700838int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
839 int snapshot_mount)
840{
841 struct nilfs_sb_info *sbi;
842 int ret = 0;
843
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900844 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700845 if (cno == 0 || cno > nilfs->ns_cno)
846 goto out_unlock;
847
848 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
849 if (sbi->s_snapshot_cno == cno &&
850 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
851 /* exclude read-only mounts */
852 ret++;
853 break;
854 }
855 }
856 /* for protecting recent checkpoints */
857 if (cno >= nilfs_last_cno(nilfs))
858 ret++;
859
860 out_unlock:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900861 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700862 return ret;
863}