blob: 6a012b9e1b31aa3a8db11d5aa4f1a9385f87104f [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);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +090090 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070091 spin_lock_init(&nilfs->ns_last_segment_lock);
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)) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700167 brelse(nilfs->ns_sbh[0]);
168 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700169 }
170 kfree(nilfs);
171}
172
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900173static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700174{
175 struct buffer_head *bh_sr;
176 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700177 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700178 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
179 unsigned inode_size;
180 int err;
181
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900182 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700183 if (unlikely(err))
184 return err;
185
186 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700187 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
188 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
189 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700190 up_read(&nilfs->ns_sem);
191
192 inode_size = nilfs->ns_inode_size;
193
194 err = -ENOMEM;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900195 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700196 if (unlikely(!nilfs->ns_dat))
197 goto failed;
198
Ryusuke Konishi79739562009-11-12 23:56:43 +0900199 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700200 if (unlikely(!nilfs->ns_gc_dat))
201 goto failed_dat;
202
Ryusuke Konishi79739562009-11-12 23:56:43 +0900203 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700204 if (unlikely(!nilfs->ns_cpfile))
205 goto failed_gc_dat;
206
Ryusuke Konishi79739562009-11-12 23:56:43 +0900207 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700208 if (unlikely(!nilfs->ns_sufile))
209 goto failed_cpfile;
210
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700211 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700212
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900213 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
214 NILFS_SR_DAT_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700215 if (unlikely(err))
216 goto failed_sufile;
217
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900218 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
219 NILFS_SR_CPFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700220 if (unlikely(err))
221 goto failed_sufile;
222
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900223 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
224 NILFS_SR_SUFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700225 if (unlikely(err))
226 goto failed_sufile;
227
228 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
229 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
230
231 failed:
232 brelse(bh_sr);
233 return err;
234
235 failed_sufile:
236 nilfs_mdt_destroy(nilfs->ns_sufile);
237
238 failed_cpfile:
239 nilfs_mdt_destroy(nilfs->ns_cpfile);
240
241 failed_gc_dat:
242 nilfs_mdt_destroy(nilfs->ns_gc_dat);
243
244 failed_dat:
245 nilfs_mdt_destroy(nilfs->ns_dat);
246 goto failed;
247}
248
249static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
250{
251 memset(ri, 0, sizeof(*ri));
252 INIT_LIST_HEAD(&ri->ri_used_segments);
253}
254
255static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
256{
257 nilfs_dispose_segment_list(&ri->ri_used_segments);
258}
259
260/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900261 * nilfs_store_log_cursor - load log cursor from a super block
262 * @nilfs: nilfs object
263 * @sbp: buffer storing super block to be read
264 *
265 * nilfs_store_log_cursor() reads the last position of the log
266 * containing a super root from a given super block, and initializes
267 * relevant information on the nilfs object preparatory for log
268 * scanning and recovery.
269 */
270static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
271 struct nilfs_super_block *sbp)
272{
273 int ret = 0;
274
275 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
276 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
277 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
278
Ryusuke Konishi32502042010-06-29 14:42:13 +0900279 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900280 nilfs->ns_seg_seq = nilfs->ns_last_seq;
281 nilfs->ns_segnum =
282 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
283 nilfs->ns_cno = nilfs->ns_last_cno + 1;
284 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
285 printk(KERN_ERR "NILFS invalid last segment number.\n");
286 ret = -EINVAL;
287 }
288 return ret;
289}
290
291/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700292 * load_nilfs - load and recover the nilfs
293 * @nilfs: the_nilfs structure to be released
294 * @sbi: nilfs_sb_info used to recover past segment
295 *
296 * load_nilfs() searches and load the latest super root,
297 * attaches the last segment, and does recovery if needed.
298 * The caller must call this exclusively for simultaneous mounts.
299 */
300int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
301{
302 struct nilfs_recovery_info ri;
303 unsigned int s_flags = sbi->s_super->s_flags;
304 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900305 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900306 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700307
Ryusuke Konishi02345762009-11-20 03:28:01 +0900308 if (nilfs_loaded(nilfs)) {
309 if (valid_fs ||
310 ((s_flags & MS_RDONLY) && nilfs_test_opt(sbi, NORECOVERY)))
311 return 0;
312 printk(KERN_ERR "NILFS: the filesystem is in an incomplete "
313 "recovery state.\n");
314 return -EINVAL;
315 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700316
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900317 if (!valid_fs) {
318 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
319 if (s_flags & MS_RDONLY) {
320 printk(KERN_INFO "NILFS: INFO: recovery "
321 "required for readonly filesystem.\n");
322 printk(KERN_INFO "NILFS: write access will "
323 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700324 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700325 }
326
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900327 nilfs_init_recovery_info(&ri);
328
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900329 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700330 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900331 struct nilfs_super_block **sbp = nilfs->ns_sbp;
332 int blocksize;
333
334 if (err != -EINVAL)
335 goto scan_error;
336
337 if (!nilfs_valid_sb(sbp[1])) {
338 printk(KERN_WARNING
339 "NILFS warning: unable to fall back to spare"
340 "super block\n");
341 goto scan_error;
342 }
343 printk(KERN_INFO
344 "NILFS: try rollback from an earlier position\n");
345
346 /*
347 * restore super block with its spare and reconfigure
348 * relevant states of the nilfs object.
349 */
350 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
351 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
352 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
353
354 /* verify consistency between two super blocks */
355 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
356 if (blocksize != nilfs->ns_blocksize) {
357 printk(KERN_WARNING
358 "NILFS warning: blocksize differs between "
359 "two super blocks (%d != %d)\n",
360 blocksize, nilfs->ns_blocksize);
361 goto scan_error;
362 }
363
364 err = nilfs_store_log_cursor(nilfs, sbp[0]);
365 if (err)
366 goto scan_error;
367
368 /* drop clean flag to allow roll-forward and recovery */
369 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
370 valid_fs = 0;
371
372 err = nilfs_search_super_root(nilfs, &ri);
373 if (err)
374 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700375 }
376
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900377 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700378 if (unlikely(err)) {
379 printk(KERN_ERR "NILFS: error loading super root.\n");
380 goto failed;
381 }
382
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900383 if (valid_fs)
384 goto skip_recovery;
385
386 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900387 __u64 features;
388
Ryusuke Konishi02345762009-11-20 03:28:01 +0900389 if (nilfs_test_opt(sbi, NORECOVERY)) {
390 printk(KERN_INFO "NILFS: norecovery option specified. "
391 "skipping roll-forward recovery\n");
392 goto skip_recovery;
393 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900394 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
395 ~NILFS_FEATURE_COMPAT_RO_SUPP;
396 if (features) {
397 printk(KERN_ERR "NILFS: couldn't proceed with "
398 "recovery because of unsupported optional "
399 "features (%llx)\n",
400 (unsigned long long)features);
401 err = -EROFS;
402 goto failed_unload;
403 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900404 if (really_read_only) {
405 printk(KERN_ERR "NILFS: write access "
406 "unavailable, cannot proceed.\n");
407 err = -EROFS;
408 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700409 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900410 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900411 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
412 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
413 "option was specified for a read/write mount\n");
414 err = -EINVAL;
415 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700416 }
417
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900418 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900419 if (err)
420 goto failed_unload;
421
422 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900423 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
424 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900425 up_write(&nilfs->ns_sem);
426
427 if (err) {
428 printk(KERN_ERR "NILFS: failed to update super block. "
429 "recovery unfinished.\n");
430 goto failed_unload;
431 }
432 printk(KERN_INFO "NILFS: recovery complete.\n");
433
434 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700435 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900436 nilfs_clear_recovery_info(&ri);
437 sbi->s_super->s_flags = s_flags;
438 return 0;
439
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900440 scan_error:
441 printk(KERN_ERR "NILFS: error searching super root.\n");
442 goto failed;
443
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900444 failed_unload:
445 nilfs_mdt_destroy(nilfs->ns_cpfile);
446 nilfs_mdt_destroy(nilfs->ns_sufile);
447 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi4afc3132010-08-29 01:55:38 +0900448 nilfs_mdt_destroy(nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700449
450 failed:
451 nilfs_clear_recovery_info(&ri);
452 sbi->s_super->s_flags = s_flags;
453 return err;
454}
455
456static unsigned long long nilfs_max_size(unsigned int blkbits)
457{
458 unsigned int max_bits;
459 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
460
461 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
462 if (max_bits < 64)
463 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
464 return res;
465}
466
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700467static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
468 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700469{
Ryusuke Konishi9566a7a2010-08-10 00:58:41 +0900470 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
471 printk(KERN_ERR "NILFS: unsupported revision "
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700472 "(superblock rev.=%d.%d, current rev.=%d.%d). "
473 "Please check the version of mkfs.nilfs.\n",
474 le32_to_cpu(sbp->s_rev_level),
475 le16_to_cpu(sbp->s_minor_rev_level),
476 NILFS_CURRENT_REV, NILFS_MINOR_REV);
477 return -EINVAL;
478 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700479 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
480 if (nilfs->ns_sbsize > BLOCK_SIZE)
481 return -EINVAL;
482
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700483 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
484 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
485
486 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
487 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900488 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700489 return -EINVAL;
490 }
491
492 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
493 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
494 nilfs->ns_r_segments_percentage =
495 le32_to_cpu(sbp->s_r_segments_percentage);
496 nilfs->ns_nrsvsegs =
497 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
498 DIV_ROUND_UP(nilfs->ns_nsegments *
499 nilfs->ns_r_segments_percentage, 100));
500 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
501 return 0;
502}
503
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700504static int nilfs_valid_sb(struct nilfs_super_block *sbp)
505{
506 static unsigned char sum[4];
507 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
508 size_t bytes;
509 u32 crc;
510
511 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
512 return 0;
513 bytes = le16_to_cpu(sbp->s_bytes);
514 if (bytes > BLOCK_SIZE)
515 return 0;
516 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
517 sumoff);
518 crc = crc32_le(crc, sum, 4);
519 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
520 bytes - sumoff - 4);
521 return crc == le32_to_cpu(sbp->s_sum);
522}
523
524static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
525{
526 return offset < ((le64_to_cpu(sbp->s_nsegments) *
527 le32_to_cpu(sbp->s_blocks_per_segment)) <<
528 (le32_to_cpu(sbp->s_log_block_size) + 10));
529}
530
531static void nilfs_release_super_block(struct the_nilfs *nilfs)
532{
533 int i;
534
535 for (i = 0; i < 2; i++) {
536 if (nilfs->ns_sbp[i]) {
537 brelse(nilfs->ns_sbh[i]);
538 nilfs->ns_sbh[i] = NULL;
539 nilfs->ns_sbp[i] = NULL;
540 }
541 }
542}
543
544void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
545{
546 brelse(nilfs->ns_sbh[0]);
547 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
548 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
549 nilfs->ns_sbh[1] = NULL;
550 nilfs->ns_sbp[1] = NULL;
551}
552
553void nilfs_swap_super_block(struct the_nilfs *nilfs)
554{
555 struct buffer_head *tsbh = nilfs->ns_sbh[0];
556 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
557
558 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
559 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
560 nilfs->ns_sbh[1] = tsbh;
561 nilfs->ns_sbp[1] = tsbp;
562}
563
564static int nilfs_load_super_block(struct the_nilfs *nilfs,
565 struct super_block *sb, int blocksize,
566 struct nilfs_super_block **sbpp)
567{
568 struct nilfs_super_block **sbp = nilfs->ns_sbp;
569 struct buffer_head **sbh = nilfs->ns_sbh;
570 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
571 int valid[2], swp = 0;
572
573 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
574 &sbh[0]);
575 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
576
577 if (!sbp[0]) {
578 if (!sbp[1]) {
579 printk(KERN_ERR "NILFS: unable to read superblock\n");
580 return -EIO;
581 }
582 printk(KERN_WARNING
583 "NILFS warning: unable to read primary superblock\n");
584 } else if (!sbp[1])
585 printk(KERN_WARNING
586 "NILFS warning: unable to read secondary superblock\n");
587
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900588 /*
589 * Compare two super blocks and set 1 in swp if the secondary
590 * super block is valid and newer. Otherwise, set 0 in swp.
591 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700592 valid[0] = nilfs_valid_sb(sbp[0]);
593 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900594 swp = valid[1] && (!valid[0] ||
595 le64_to_cpu(sbp[1]->s_last_cno) >
596 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700597
598 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
599 brelse(sbh[1]);
600 sbh[1] = NULL;
601 sbp[1] = NULL;
602 swp = 0;
603 }
604 if (!valid[swp]) {
605 nilfs_release_super_block(nilfs);
606 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
607 sb->s_id);
608 return -EINVAL;
609 }
610
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900611 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700612 printk(KERN_WARNING "NILFS warning: broken superblock. "
613 "using spare superblock.\n");
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900614 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700615 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700616
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900617 nilfs->ns_sbwcount = 0;
618 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700619 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
620 *sbpp = sbp[0];
621 return 0;
622}
623
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700624/**
625 * init_nilfs - initialize a NILFS instance.
626 * @nilfs: the_nilfs structure
627 * @sbi: nilfs_sb_info
628 * @sb: super block
629 * @data: mount options
630 *
631 * init_nilfs() performs common initialization per block device (e.g.
632 * reading the super block, getting disk layout information, initializing
633 * shared fields in the_nilfs). It takes on some portion of the jobs
634 * typically done by a fill_super() routine. This division arises from
635 * the nature that multiple NILFS instances may be simultaneously
636 * mounted on a device.
637 * For multiple mounts on the same device, only the first mount
638 * invokes these tasks.
639 *
640 * Return Value: On success, 0 is returned. On error, a negative error
641 * code is returned.
642 */
643int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
644{
645 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700646 struct nilfs_super_block *sbp;
647 struct backing_dev_info *bdi;
648 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700649 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700650
651 down_write(&nilfs->ns_sem);
652 if (nilfs_init(nilfs)) {
653 /* Load values from existing the_nilfs */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700654 sbp = nilfs->ns_sbp[0];
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700655 err = nilfs_store_magic_and_option(sb, sbp, data);
656 if (err)
657 goto out;
658
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900659 err = nilfs_check_feature_compatibility(sb, sbp);
660 if (err)
661 goto out;
662
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700663 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
664 if (sb->s_blocksize != blocksize &&
665 !sb_set_blocksize(sb, blocksize)) {
666 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
667 blocksize);
668 err = -EINVAL;
669 }
670 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
671 goto out;
672 }
673
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900674 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700675 if (!blocksize) {
676 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700677 err = -EINVAL;
678 goto out;
679 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700680 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
681 if (err)
682 goto out;
683
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700684 err = nilfs_store_magic_and_option(sb, sbp, data);
685 if (err)
686 goto failed_sbh;
687
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900688 err = nilfs_check_feature_compatibility(sb, sbp);
689 if (err)
690 goto failed_sbh;
691
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700692 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900693 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
694 blocksize > NILFS_MAX_BLOCK_SIZE) {
695 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
696 "filesystem blocksize %d\n", blocksize);
697 err = -EINVAL;
698 goto failed_sbh;
699 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700700 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400701 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700702
703 if (blocksize < hw_blocksize) {
704 printk(KERN_ERR
705 "NILFS: blocksize %d too small for device "
706 "(sector-size = %d).\n",
707 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700708 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700709 goto failed_sbh;
710 }
711 nilfs_release_super_block(nilfs);
712 sb_set_blocksize(sb, blocksize);
713
714 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
715 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700716 goto out;
717 /* not failed_sbh; sbh is released automatically
718 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700719 }
720 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900721 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700722
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700723 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700724 if (err)
725 goto failed_sbh;
726
727 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
728
729 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700730
Jens Axboe2c96ce92009-09-15 09:43:56 +0200731 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700732 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
733
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900734 err = nilfs_store_log_cursor(nilfs, sbp);
735 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700736 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700737
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700738 set_nilfs_init(nilfs);
739 err = 0;
740 out:
741 up_write(&nilfs->ns_sem);
742 return err;
743
744 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700745 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700746 goto out;
747}
748
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900749int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
750 size_t nsegs)
751{
752 sector_t seg_start, seg_end;
753 sector_t start = 0, nblocks = 0;
754 unsigned int sects_per_block;
755 __u64 *sn;
756 int ret = 0;
757
758 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
759 bdev_logical_block_size(nilfs->ns_bdev);
760 for (sn = segnump; sn < segnump + nsegs; sn++) {
761 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
762
763 if (!nblocks) {
764 start = seg_start;
765 nblocks = seg_end - seg_start + 1;
766 } else if (start + nblocks == seg_start) {
767 nblocks += seg_end - seg_start + 1;
768 } else {
769 ret = blkdev_issue_discard(nilfs->ns_bdev,
770 start * sects_per_block,
771 nblocks * sects_per_block,
772 GFP_NOFS,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900773 BLKDEV_IFL_WAIT |
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200774 BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900775 if (ret < 0)
776 return ret;
777 nblocks = 0;
778 }
779 }
780 if (nblocks)
781 ret = blkdev_issue_discard(nilfs->ns_bdev,
782 start * sects_per_block,
783 nblocks * sects_per_block,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900784 GFP_NOFS,
785 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900786 return ret;
787}
788
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700789int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
790{
791 struct inode *dat = nilfs_dat_inode(nilfs);
792 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700793
794 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900795 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700796 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900797 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
798 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700799}
800
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700801int nilfs_near_disk_full(struct the_nilfs *nilfs)
802{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700803 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700804
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900805 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
806 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
807 nilfs->ns_blocks_per_segment + 1;
808
809 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700810}
811
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900812/**
813 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
814 * @nilfs: nilfs object
815 * @rw_mount: mount type (non-zero value for read/write mount)
816 * @cno: checkpoint number (zero for read-only mount)
817 *
818 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
819 * @rw_mount and @cno (in case of snapshots) matched. If no instance
820 * was found, NULL is returned. Although the super block instance can
821 * be unmounted after this function returns, the nilfs_sb_info struct
822 * is kept on memory until nilfs_put_sbinfo() is called.
823 */
824struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
825 int rw_mount, __u64 cno)
826{
827 struct nilfs_sb_info *sbi;
828
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900829 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900830 /*
831 * The SNAPSHOT flag and sb->s_flags are supposed to be
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900832 * protected with nilfs->ns_super_sem.
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900833 */
834 sbi = nilfs->ns_current;
835 if (rw_mount) {
836 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
837 goto found; /* read/write mount */
838 else
839 goto out;
840 } else if (cno == 0) {
841 if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
842 goto found; /* read-only mount */
843 else
844 goto out;
845 }
846
847 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
848 if (nilfs_test_opt(sbi, SNAPSHOT) &&
849 sbi->s_snapshot_cno == cno)
850 goto found; /* snapshot mount */
851 }
852 out:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900853 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900854 return NULL;
855
856 found:
857 atomic_inc(&sbi->s_count);
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900858 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900859 return sbi;
860}
861
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700862int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
863 int snapshot_mount)
864{
865 struct nilfs_sb_info *sbi;
866 int ret = 0;
867
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900868 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700869 if (cno == 0 || cno > nilfs->ns_cno)
870 goto out_unlock;
871
872 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
873 if (sbi->s_snapshot_cno == cno &&
874 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
875 /* exclude read-only mounts */
876 ret++;
877 break;
878 }
879 }
880 /* for protecting recent checkpoints */
881 if (cno >= nilfs_last_cno(nilfs))
882 ret++;
883
884 out_unlock:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900885 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700886 return ret;
887}