blob: aea2b58ba03bfa4edd3c5cf3a1b63c2c48b2997a [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 Konishi8a9d2192009-04-06 19:01:35 -070041void nilfs_set_last_segment(struct the_nilfs *nilfs,
42 sector_t start_blocknr, u64 seq, __u64 cno)
43{
44 spin_lock(&nilfs->ns_last_segment_lock);
45 nilfs->ns_last_pseg = start_blocknr;
46 nilfs->ns_last_seq = seq;
47 nilfs->ns_last_cno = cno;
48 spin_unlock(&nilfs->ns_last_segment_lock);
49}
50
51/**
52 * alloc_nilfs - allocate the_nilfs structure
53 * @bdev: block device to which the_nilfs is related
54 *
55 * alloc_nilfs() allocates memory for the_nilfs and
56 * initializes its reference count and locks.
57 *
58 * Return Value: On success, pointer to the_nilfs is returned.
59 * On error, NULL is returned.
60 */
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090061static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070062{
63 struct the_nilfs *nilfs;
64
65 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
66 if (!nilfs)
67 return NULL;
68
69 nilfs->ns_bdev = bdev;
70 atomic_set(&nilfs->ns_count, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070071 atomic_set(&nilfs->ns_ndirtyblks, 0);
72 init_rwsem(&nilfs->ns_sem);
Ryusuke Konishie59399d2009-06-08 01:39:32 +090073 init_rwsem(&nilfs->ns_super_sem);
Ryusuke Konishiaa7dfb82009-06-08 01:39:33 +090074 mutex_init(&nilfs->ns_mount_mutex);
Ryusuke Konishi027d6402009-08-02 22:45:33 +090075 init_rwsem(&nilfs->ns_writer_sem);
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090076 INIT_LIST_HEAD(&nilfs->ns_list);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070077 INIT_LIST_HEAD(&nilfs->ns_supers);
78 spin_lock_init(&nilfs->ns_last_segment_lock);
79 nilfs->ns_gc_inodes_h = NULL;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070080 init_rwsem(&nilfs->ns_segctor_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070081
82 return nilfs;
83}
84
85/**
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090086 * find_or_create_nilfs - find or create nilfs object
87 * @bdev: block device to which the_nilfs is related
88 *
89 * find_nilfs() looks up an existent nilfs object created on the
90 * device and gets the reference count of the object. If no nilfs object
91 * is found on the device, a new nilfs object is allocated.
92 *
93 * Return Value: On success, pointer to the nilfs object is returned.
94 * On error, NULL is returned.
95 */
96struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
97{
98 struct the_nilfs *nilfs, *new = NULL;
99
100 retry:
101 spin_lock(&nilfs_lock);
102 list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
103 if (nilfs->ns_bdev == bdev) {
104 get_nilfs(nilfs);
105 spin_unlock(&nilfs_lock);
106 if (new)
107 put_nilfs(new);
108 return nilfs; /* existing object */
109 }
110 }
111 if (new) {
112 list_add_tail(&new->ns_list, &nilfs_objects);
113 spin_unlock(&nilfs_lock);
114 return new; /* new object */
115 }
116 spin_unlock(&nilfs_lock);
117
118 new = alloc_nilfs(bdev);
119 if (new)
120 goto retry;
121 return NULL; /* insufficient memory */
122}
123
124/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700125 * put_nilfs - release a reference to the_nilfs
126 * @nilfs: the_nilfs structure to be released
127 *
128 * put_nilfs() decrements a reference counter of the_nilfs.
129 * If the reference count reaches zero, the_nilfs is freed.
130 */
131void put_nilfs(struct the_nilfs *nilfs)
132{
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900133 spin_lock(&nilfs_lock);
134 if (!atomic_dec_and_test(&nilfs->ns_count)) {
135 spin_unlock(&nilfs_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700136 return;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900137 }
138 list_del_init(&nilfs->ns_list);
139 spin_unlock(&nilfs_lock);
140
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700141 /*
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900142 * Increment of ns_count never occurs below because the caller
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700143 * of get_nilfs() holds at least one reference to the_nilfs.
144 * Thus its exclusion control is not required here.
145 */
Ryusuke Konishi33c8e572009-06-08 01:39:29 +0900146
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700147 might_sleep();
148 if (nilfs_loaded(nilfs)) {
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700149 nilfs_mdt_destroy(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700150 nilfs_mdt_destroy(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700151 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700152 nilfs_mdt_destroy(nilfs->ns_gc_dat);
153 }
154 if (nilfs_init(nilfs)) {
155 nilfs_destroy_gccache(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700156 brelse(nilfs->ns_sbh[0]);
157 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700158 }
159 kfree(nilfs);
160}
161
162static int nilfs_load_super_root(struct the_nilfs *nilfs,
163 struct nilfs_sb_info *sbi, sector_t sr_block)
164{
165 struct buffer_head *bh_sr;
166 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700167 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700168 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
169 unsigned inode_size;
170 int err;
171
172 err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
173 if (unlikely(err))
174 return err;
175
176 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700177 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
178 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
179 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700180 up_read(&nilfs->ns_sem);
181
182 inode_size = nilfs->ns_inode_size;
183
184 err = -ENOMEM;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900185 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700186 if (unlikely(!nilfs->ns_dat))
187 goto failed;
188
Ryusuke Konishi79739562009-11-12 23:56:43 +0900189 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700190 if (unlikely(!nilfs->ns_gc_dat))
191 goto failed_dat;
192
Ryusuke Konishi79739562009-11-12 23:56:43 +0900193 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700194 if (unlikely(!nilfs->ns_cpfile))
195 goto failed_gc_dat;
196
Ryusuke Konishi79739562009-11-12 23:56:43 +0900197 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700198 if (unlikely(!nilfs->ns_sufile))
199 goto failed_cpfile;
200
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700201 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700202
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900203 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
204 NILFS_SR_DAT_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700205 if (unlikely(err))
206 goto failed_sufile;
207
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900208 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
209 NILFS_SR_CPFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700210 if (unlikely(err))
211 goto failed_sufile;
212
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900213 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
214 NILFS_SR_SUFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700215 if (unlikely(err))
216 goto failed_sufile;
217
218 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
219 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
220
221 failed:
222 brelse(bh_sr);
223 return err;
224
225 failed_sufile:
226 nilfs_mdt_destroy(nilfs->ns_sufile);
227
228 failed_cpfile:
229 nilfs_mdt_destroy(nilfs->ns_cpfile);
230
231 failed_gc_dat:
232 nilfs_mdt_destroy(nilfs->ns_gc_dat);
233
234 failed_dat:
235 nilfs_mdt_destroy(nilfs->ns_dat);
236 goto failed;
237}
238
239static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
240{
241 memset(ri, 0, sizeof(*ri));
242 INIT_LIST_HEAD(&ri->ri_used_segments);
243}
244
245static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
246{
247 nilfs_dispose_segment_list(&ri->ri_used_segments);
248}
249
250/**
251 * load_nilfs - load and recover the nilfs
252 * @nilfs: the_nilfs structure to be released
253 * @sbi: nilfs_sb_info used to recover past segment
254 *
255 * load_nilfs() searches and load the latest super root,
256 * attaches the last segment, and does recovery if needed.
257 * The caller must call this exclusively for simultaneous mounts.
258 */
259int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
260{
261 struct nilfs_recovery_info ri;
262 unsigned int s_flags = sbi->s_super->s_flags;
263 int really_read_only = bdev_read_only(nilfs->ns_bdev);
264 unsigned valid_fs;
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900265 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700266
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900267 if (nilfs_loaded(nilfs))
268 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700269
270 down_write(&nilfs->ns_sem);
271 valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
272 up_write(&nilfs->ns_sem);
273
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900274 if (!valid_fs) {
275 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
276 if (s_flags & MS_RDONLY) {
277 printk(KERN_INFO "NILFS: INFO: recovery "
278 "required for readonly filesystem.\n");
279 printk(KERN_INFO "NILFS: write access will "
280 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700281 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700282 }
283
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900284 nilfs_init_recovery_info(&ri);
285
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700286 err = nilfs_search_super_root(nilfs, sbi, &ri);
287 if (unlikely(err)) {
288 printk(KERN_ERR "NILFS: error searching super root.\n");
289 goto failed;
290 }
291
292 err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
293 if (unlikely(err)) {
294 printk(KERN_ERR "NILFS: error loading super root.\n");
295 goto failed;
296 }
297
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900298 if (valid_fs)
299 goto skip_recovery;
300
301 if (s_flags & MS_RDONLY) {
302 if (really_read_only) {
303 printk(KERN_ERR "NILFS: write access "
304 "unavailable, cannot proceed.\n");
305 err = -EROFS;
306 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700307 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900308 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700309 }
310
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900311 err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
312 if (err)
313 goto failed_unload;
314
315 down_write(&nilfs->ns_sem);
316 nilfs->ns_mount_state |= NILFS_VALID_FS;
317 nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
318 err = nilfs_commit_super(sbi, 1);
319 up_write(&nilfs->ns_sem);
320
321 if (err) {
322 printk(KERN_ERR "NILFS: failed to update super block. "
323 "recovery unfinished.\n");
324 goto failed_unload;
325 }
326 printk(KERN_INFO "NILFS: recovery complete.\n");
327
328 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700329 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900330 nilfs_clear_recovery_info(&ri);
331 sbi->s_super->s_flags = s_flags;
332 return 0;
333
334 failed_unload:
335 nilfs_mdt_destroy(nilfs->ns_cpfile);
336 nilfs_mdt_destroy(nilfs->ns_sufile);
337 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700338
339 failed:
340 nilfs_clear_recovery_info(&ri);
341 sbi->s_super->s_flags = s_flags;
342 return err;
343}
344
345static unsigned long long nilfs_max_size(unsigned int blkbits)
346{
347 unsigned int max_bits;
348 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
349
350 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
351 if (max_bits < 64)
352 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
353 return res;
354}
355
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700356static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
357 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700358{
359 if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
360 printk(KERN_ERR "NILFS: revision mismatch "
361 "(superblock rev.=%d.%d, current rev.=%d.%d). "
362 "Please check the version of mkfs.nilfs.\n",
363 le32_to_cpu(sbp->s_rev_level),
364 le16_to_cpu(sbp->s_minor_rev_level),
365 NILFS_CURRENT_REV, NILFS_MINOR_REV);
366 return -EINVAL;
367 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700368 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
369 if (nilfs->ns_sbsize > BLOCK_SIZE)
370 return -EINVAL;
371
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700372 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
373 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
374
375 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
376 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
377 printk(KERN_ERR "NILFS: too short segment. \n");
378 return -EINVAL;
379 }
380
381 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
382 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
383 nilfs->ns_r_segments_percentage =
384 le32_to_cpu(sbp->s_r_segments_percentage);
385 nilfs->ns_nrsvsegs =
386 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
387 DIV_ROUND_UP(nilfs->ns_nsegments *
388 nilfs->ns_r_segments_percentage, 100));
389 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
390 return 0;
391}
392
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700393static int nilfs_valid_sb(struct nilfs_super_block *sbp)
394{
395 static unsigned char sum[4];
396 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
397 size_t bytes;
398 u32 crc;
399
400 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
401 return 0;
402 bytes = le16_to_cpu(sbp->s_bytes);
403 if (bytes > BLOCK_SIZE)
404 return 0;
405 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
406 sumoff);
407 crc = crc32_le(crc, sum, 4);
408 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
409 bytes - sumoff - 4);
410 return crc == le32_to_cpu(sbp->s_sum);
411}
412
413static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
414{
415 return offset < ((le64_to_cpu(sbp->s_nsegments) *
416 le32_to_cpu(sbp->s_blocks_per_segment)) <<
417 (le32_to_cpu(sbp->s_log_block_size) + 10));
418}
419
420static void nilfs_release_super_block(struct the_nilfs *nilfs)
421{
422 int i;
423
424 for (i = 0; i < 2; i++) {
425 if (nilfs->ns_sbp[i]) {
426 brelse(nilfs->ns_sbh[i]);
427 nilfs->ns_sbh[i] = NULL;
428 nilfs->ns_sbp[i] = NULL;
429 }
430 }
431}
432
433void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
434{
435 brelse(nilfs->ns_sbh[0]);
436 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
437 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
438 nilfs->ns_sbh[1] = NULL;
439 nilfs->ns_sbp[1] = NULL;
440}
441
442void nilfs_swap_super_block(struct the_nilfs *nilfs)
443{
444 struct buffer_head *tsbh = nilfs->ns_sbh[0];
445 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
446
447 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
448 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
449 nilfs->ns_sbh[1] = tsbh;
450 nilfs->ns_sbp[1] = tsbp;
451}
452
453static int nilfs_load_super_block(struct the_nilfs *nilfs,
454 struct super_block *sb, int blocksize,
455 struct nilfs_super_block **sbpp)
456{
457 struct nilfs_super_block **sbp = nilfs->ns_sbp;
458 struct buffer_head **sbh = nilfs->ns_sbh;
459 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
460 int valid[2], swp = 0;
461
462 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
463 &sbh[0]);
464 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
465
466 if (!sbp[0]) {
467 if (!sbp[1]) {
468 printk(KERN_ERR "NILFS: unable to read superblock\n");
469 return -EIO;
470 }
471 printk(KERN_WARNING
472 "NILFS warning: unable to read primary superblock\n");
473 } else if (!sbp[1])
474 printk(KERN_WARNING
475 "NILFS warning: unable to read secondary superblock\n");
476
477 valid[0] = nilfs_valid_sb(sbp[0]);
478 valid[1] = nilfs_valid_sb(sbp[1]);
479 swp = valid[1] &&
480 (!valid[0] ||
481 le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
482
483 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
484 brelse(sbh[1]);
485 sbh[1] = NULL;
486 sbp[1] = NULL;
487 swp = 0;
488 }
489 if (!valid[swp]) {
490 nilfs_release_super_block(nilfs);
491 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
492 sb->s_id);
493 return -EINVAL;
494 }
495
496 if (swp) {
497 printk(KERN_WARNING "NILFS warning: broken superblock. "
498 "using spare superblock.\n");
499 nilfs_swap_super_block(nilfs);
500 }
501
502 nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
503 nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
504 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
505 *sbpp = sbp[0];
506 return 0;
507}
508
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700509/**
510 * init_nilfs - initialize a NILFS instance.
511 * @nilfs: the_nilfs structure
512 * @sbi: nilfs_sb_info
513 * @sb: super block
514 * @data: mount options
515 *
516 * init_nilfs() performs common initialization per block device (e.g.
517 * reading the super block, getting disk layout information, initializing
518 * shared fields in the_nilfs). It takes on some portion of the jobs
519 * typically done by a fill_super() routine. This division arises from
520 * the nature that multiple NILFS instances may be simultaneously
521 * mounted on a device.
522 * For multiple mounts on the same device, only the first mount
523 * invokes these tasks.
524 *
525 * Return Value: On success, 0 is returned. On error, a negative error
526 * code is returned.
527 */
528int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
529{
530 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700531 struct nilfs_super_block *sbp;
532 struct backing_dev_info *bdi;
533 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700534 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700535
536 down_write(&nilfs->ns_sem);
537 if (nilfs_init(nilfs)) {
538 /* Load values from existing the_nilfs */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700539 sbp = nilfs->ns_sbp[0];
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700540 err = nilfs_store_magic_and_option(sb, sbp, data);
541 if (err)
542 goto out;
543
544 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
545 if (sb->s_blocksize != blocksize &&
546 !sb_set_blocksize(sb, blocksize)) {
547 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
548 blocksize);
549 err = -EINVAL;
550 }
551 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
552 goto out;
553 }
554
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700555 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
556 if (!blocksize) {
557 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700558 err = -EINVAL;
559 goto out;
560 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700561 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
562 if (err)
563 goto out;
564
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700565 err = nilfs_store_magic_and_option(sb, sbp, data);
566 if (err)
567 goto failed_sbh;
568
569 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
570 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400571 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700572
573 if (blocksize < hw_blocksize) {
574 printk(KERN_ERR
575 "NILFS: blocksize %d too small for device "
576 "(sector-size = %d).\n",
577 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700578 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700579 goto failed_sbh;
580 }
581 nilfs_release_super_block(nilfs);
582 sb_set_blocksize(sb, blocksize);
583
584 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
585 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700586 goto out;
587 /* not failed_sbh; sbh is released automatically
588 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700589 }
590 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
591
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700592 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700593 if (err)
594 goto failed_sbh;
595
596 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
597
598 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700599
Jens Axboe2c96ce92009-09-15 09:43:56 +0200600 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700601 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
602
603 /* Finding last segment */
604 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
605 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
606 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
607
608 nilfs->ns_seg_seq = nilfs->ns_last_seq;
609 nilfs->ns_segnum =
610 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
611 nilfs->ns_cno = nilfs->ns_last_cno + 1;
612 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
613 printk(KERN_ERR "NILFS invalid last segment number.\n");
614 err = -EINVAL;
615 goto failed_sbh;
616 }
617 /* Dummy values */
618 nilfs->ns_free_segments_count =
619 nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
620
621 /* Initialize gcinode cache */
622 err = nilfs_init_gccache(nilfs);
623 if (err)
624 goto failed_sbh;
625
626 set_nilfs_init(nilfs);
627 err = 0;
628 out:
629 up_write(&nilfs->ns_sem);
630 return err;
631
632 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700633 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700634 goto out;
635}
636
637int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
638{
639 struct inode *dat = nilfs_dat_inode(nilfs);
640 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700641
642 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900643 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700644 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900645 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
646 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700647}
648
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700649int nilfs_near_disk_full(struct the_nilfs *nilfs)
650{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700651 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700652
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900653 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
654 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
655 nilfs->ns_blocks_per_segment + 1;
656
657 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700658}
659
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900660/**
661 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
662 * @nilfs: nilfs object
663 * @rw_mount: mount type (non-zero value for read/write mount)
664 * @cno: checkpoint number (zero for read-only mount)
665 *
666 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
667 * @rw_mount and @cno (in case of snapshots) matched. If no instance
668 * was found, NULL is returned. Although the super block instance can
669 * be unmounted after this function returns, the nilfs_sb_info struct
670 * is kept on memory until nilfs_put_sbinfo() is called.
671 */
672struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
673 int rw_mount, __u64 cno)
674{
675 struct nilfs_sb_info *sbi;
676
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900677 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900678 /*
679 * The SNAPSHOT flag and sb->s_flags are supposed to be
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900680 * protected with nilfs->ns_super_sem.
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900681 */
682 sbi = nilfs->ns_current;
683 if (rw_mount) {
684 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
685 goto found; /* read/write mount */
686 else
687 goto out;
688 } else if (cno == 0) {
689 if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
690 goto found; /* read-only mount */
691 else
692 goto out;
693 }
694
695 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
696 if (nilfs_test_opt(sbi, SNAPSHOT) &&
697 sbi->s_snapshot_cno == cno)
698 goto found; /* snapshot mount */
699 }
700 out:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900701 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900702 return NULL;
703
704 found:
705 atomic_inc(&sbi->s_count);
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900706 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900707 return sbi;
708}
709
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700710int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
711 int snapshot_mount)
712{
713 struct nilfs_sb_info *sbi;
714 int ret = 0;
715
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900716 down_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700717 if (cno == 0 || cno > nilfs->ns_cno)
718 goto out_unlock;
719
720 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
721 if (sbi->s_snapshot_cno == cno &&
722 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
723 /* exclude read-only mounts */
724 ret++;
725 break;
726 }
727 }
728 /* for protecting recent checkpoints */
729 if (cno >= nilfs_last_cno(nilfs))
730 ret++;
731
732 out_unlock:
Ryusuke Konishie59399d2009-06-08 01:39:32 +0900733 up_read(&nilfs->ns_super_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700734 return ret;
735}