nilfs2: add missing error code in comment of nilfs_search_super_root
[deliverable/linux.git] / fs / nilfs2 / the_nilfs.c
CommitLineData
8a9d2191
RK
1/*
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>
e339ad31 28#include <linux/crc32.h>
8a9d2191
RK
29#include "nilfs.h"
30#include "segment.h"
31#include "alloc.h"
32#include "cpfile.h"
33#include "sufile.h"
34#include "dat.h"
8a9d2191
RK
35#include "segbuf.h"
36
33c8e57c
RK
37
38static LIST_HEAD(nilfs_objects);
39static DEFINE_SPINLOCK(nilfs_lock);
40
8a9d2191
RK
41void 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 */
33c8e57c 61static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
8a9d2191
RK
62{
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);
8a9d2191
RK
71 atomic_set(&nilfs->ns_ndirtyblks, 0);
72 init_rwsem(&nilfs->ns_sem);
e59399d0 73 init_rwsem(&nilfs->ns_super_sem);
aa7dfb89 74 mutex_init(&nilfs->ns_mount_mutex);
027d6404 75 init_rwsem(&nilfs->ns_writer_sem);
33c8e57c 76 INIT_LIST_HEAD(&nilfs->ns_list);
8a9d2191
RK
77 INIT_LIST_HEAD(&nilfs->ns_supers);
78 spin_lock_init(&nilfs->ns_last_segment_lock);
79 nilfs->ns_gc_inodes_h = NULL;
8a9d2191 80 init_rwsem(&nilfs->ns_segctor_sem);
8a9d2191
RK
81
82 return nilfs;
83}
84
33c8e57c
RK
85/**
86 * 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
8a9d2191
RK
124/**
125 * 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{
33c8e57c
RK
133 spin_lock(&nilfs_lock);
134 if (!atomic_dec_and_test(&nilfs->ns_count)) {
135 spin_unlock(&nilfs_lock);
8a9d2191 136 return;
33c8e57c
RK
137 }
138 list_del_init(&nilfs->ns_list);
139 spin_unlock(&nilfs_lock);
140
8a9d2191 141 /*
33c8e57c 142 * Increment of ns_count never occurs below because the caller
8a9d2191
RK
143 * of get_nilfs() holds at least one reference to the_nilfs.
144 * Thus its exclusion control is not required here.
145 */
33c8e57c 146
8a9d2191
RK
147 might_sleep();
148 if (nilfs_loaded(nilfs)) {
8a9d2191 149 nilfs_mdt_destroy(nilfs->ns_sufile);
8a9d2191 150 nilfs_mdt_destroy(nilfs->ns_cpfile);
8a9d2191 151 nilfs_mdt_destroy(nilfs->ns_dat);
8a9d2191
RK
152 nilfs_mdt_destroy(nilfs->ns_gc_dat);
153 }
154 if (nilfs_init(nilfs)) {
155 nilfs_destroy_gccache(nilfs);
e339ad31
RK
156 brelse(nilfs->ns_sbh[0]);
157 brelse(nilfs->ns_sbh[1]);
8a9d2191
RK
158 }
159 kfree(nilfs);
160}
161
8b94025c 162static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
8a9d2191
RK
163{
164 struct buffer_head *bh_sr;
165 struct nilfs_super_root *raw_sr;
e339ad31 166 struct nilfs_super_block **sbp = nilfs->ns_sbp;
8a9d2191
RK
167 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
168 unsigned inode_size;
169 int err;
170
8b94025c 171 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
8a9d2191
RK
172 if (unlikely(err))
173 return err;
174
175 down_read(&nilfs->ns_sem);
e339ad31
RK
176 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
177 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
178 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
8a9d2191
RK
179 up_read(&nilfs->ns_sem);
180
181 inode_size = nilfs->ns_inode_size;
182
183 err = -ENOMEM;
79739565 184 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
8a9d2191
RK
185 if (unlikely(!nilfs->ns_dat))
186 goto failed;
187
79739565 188 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
8a9d2191
RK
189 if (unlikely(!nilfs->ns_gc_dat))
190 goto failed_dat;
191
79739565 192 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
8a9d2191
RK
193 if (unlikely(!nilfs->ns_cpfile))
194 goto failed_gc_dat;
195
79739565 196 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
8a9d2191
RK
197 if (unlikely(!nilfs->ns_sufile))
198 goto failed_cpfile;
199
8a9d2191 200 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
8a9d2191 201
8707df38
RK
202 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
203 NILFS_SR_DAT_OFFSET(inode_size));
8a9d2191
RK
204 if (unlikely(err))
205 goto failed_sufile;
206
8707df38
RK
207 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
208 NILFS_SR_CPFILE_OFFSET(inode_size));
8a9d2191
RK
209 if (unlikely(err))
210 goto failed_sufile;
211
8707df38
RK
212 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
213 NILFS_SR_SUFILE_OFFSET(inode_size));
8a9d2191
RK
214 if (unlikely(err))
215 goto failed_sufile;
216
217 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
218 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
219
220 failed:
221 brelse(bh_sr);
222 return err;
223
224 failed_sufile:
225 nilfs_mdt_destroy(nilfs->ns_sufile);
226
227 failed_cpfile:
228 nilfs_mdt_destroy(nilfs->ns_cpfile);
229
230 failed_gc_dat:
231 nilfs_mdt_destroy(nilfs->ns_gc_dat);
232
233 failed_dat:
234 nilfs_mdt_destroy(nilfs->ns_dat);
235 goto failed;
236}
237
238static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
239{
240 memset(ri, 0, sizeof(*ri));
241 INIT_LIST_HEAD(&ri->ri_used_segments);
242}
243
244static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
245{
246 nilfs_dispose_segment_list(&ri->ri_used_segments);
247}
248
843d63ba
RK
249/**
250 * nilfs_store_log_cursor - load log cursor from a super block
251 * @nilfs: nilfs object
252 * @sbp: buffer storing super block to be read
253 *
254 * nilfs_store_log_cursor() reads the last position of the log
255 * containing a super root from a given super block, and initializes
256 * relevant information on the nilfs object preparatory for log
257 * scanning and recovery.
258 */
259static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
260 struct nilfs_super_block *sbp)
261{
262 int ret = 0;
263
264 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
265 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
266 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
267
268 nilfs->ns_seg_seq = nilfs->ns_last_seq;
269 nilfs->ns_segnum =
270 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
271 nilfs->ns_cno = nilfs->ns_last_cno + 1;
272 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
273 printk(KERN_ERR "NILFS invalid last segment number.\n");
274 ret = -EINVAL;
275 }
276 return ret;
277}
278
8a9d2191
RK
279/**
280 * load_nilfs - load and recover the nilfs
281 * @nilfs: the_nilfs structure to be released
282 * @sbi: nilfs_sb_info used to recover past segment
283 *
284 * load_nilfs() searches and load the latest super root,
285 * attaches the last segment, and does recovery if needed.
286 * The caller must call this exclusively for simultaneous mounts.
287 */
288int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
289{
290 struct nilfs_recovery_info ri;
291 unsigned int s_flags = sbi->s_super->s_flags;
292 int really_read_only = bdev_read_only(nilfs->ns_bdev);
a057d2c0 293 int valid_fs = nilfs_valid_fs(nilfs);
f50a4c81 294 int err;
8a9d2191 295
0234576d
RK
296 if (nilfs_loaded(nilfs)) {
297 if (valid_fs ||
298 ((s_flags & MS_RDONLY) && nilfs_test_opt(sbi, NORECOVERY)))
299 return 0;
300 printk(KERN_ERR "NILFS: the filesystem is in an incomplete "
301 "recovery state.\n");
302 return -EINVAL;
303 }
8a9d2191 304
f50a4c81
RK
305 if (!valid_fs) {
306 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
307 if (s_flags & MS_RDONLY) {
308 printk(KERN_INFO "NILFS: INFO: recovery "
309 "required for readonly filesystem.\n");
310 printk(KERN_INFO "NILFS: write access will "
311 "be enabled during recovery.\n");
8a9d2191 312 }
8a9d2191
RK
313 }
314
f50a4c81
RK
315 nilfs_init_recovery_info(&ri);
316
8b94025c 317 err = nilfs_search_super_root(nilfs, &ri);
8a9d2191
RK
318 if (unlikely(err)) {
319 printk(KERN_ERR "NILFS: error searching super root.\n");
320 goto failed;
321 }
322
8b94025c 323 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
8a9d2191
RK
324 if (unlikely(err)) {
325 printk(KERN_ERR "NILFS: error loading super root.\n");
326 goto failed;
327 }
328
f50a4c81
RK
329 if (valid_fs)
330 goto skip_recovery;
331
332 if (s_flags & MS_RDONLY) {
0234576d
RK
333 if (nilfs_test_opt(sbi, NORECOVERY)) {
334 printk(KERN_INFO "NILFS: norecovery option specified. "
335 "skipping roll-forward recovery\n");
336 goto skip_recovery;
337 }
f50a4c81
RK
338 if (really_read_only) {
339 printk(KERN_ERR "NILFS: write access "
340 "unavailable, cannot proceed.\n");
341 err = -EROFS;
342 goto failed_unload;
8a9d2191 343 }
f50a4c81 344 sbi->s_super->s_flags &= ~MS_RDONLY;
0234576d
RK
345 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
346 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
347 "option was specified for a read/write mount\n");
348 err = -EINVAL;
349 goto failed_unload;
f50a4c81
RK
350 }
351
aee5ce2f 352 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
f50a4c81
RK
353 if (err)
354 goto failed_unload;
355
356 down_write(&nilfs->ns_sem);
7ecaa46c
RK
357 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
358 err = nilfs_cleanup_super(sbi);
f50a4c81
RK
359 up_write(&nilfs->ns_sem);
360
361 if (err) {
362 printk(KERN_ERR "NILFS: failed to update super block. "
363 "recovery unfinished.\n");
364 goto failed_unload;
8a9d2191 365 }
f50a4c81 366 printk(KERN_INFO "NILFS: recovery complete.\n");
8a9d2191 367
f50a4c81 368 skip_recovery:
8a9d2191 369 set_nilfs_loaded(nilfs);
f50a4c81
RK
370 nilfs_clear_recovery_info(&ri);
371 sbi->s_super->s_flags = s_flags;
372 return 0;
373
374 failed_unload:
375 nilfs_mdt_destroy(nilfs->ns_cpfile);
376 nilfs_mdt_destroy(nilfs->ns_sufile);
377 nilfs_mdt_destroy(nilfs->ns_dat);
8a9d2191
RK
378
379 failed:
380 nilfs_clear_recovery_info(&ri);
381 sbi->s_super->s_flags = s_flags;
382 return err;
383}
384
385static unsigned long long nilfs_max_size(unsigned int blkbits)
386{
387 unsigned int max_bits;
388 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
389
390 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
391 if (max_bits < 64)
392 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
393 return res;
394}
395
e339ad31
RK
396static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
397 struct nilfs_super_block *sbp)
8a9d2191
RK
398{
399 if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
400 printk(KERN_ERR "NILFS: revision mismatch "
401 "(superblock rev.=%d.%d, current rev.=%d.%d). "
402 "Please check the version of mkfs.nilfs.\n",
403 le32_to_cpu(sbp->s_rev_level),
404 le16_to_cpu(sbp->s_minor_rev_level),
405 NILFS_CURRENT_REV, NILFS_MINOR_REV);
406 return -EINVAL;
407 }
e339ad31
RK
408 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
409 if (nilfs->ns_sbsize > BLOCK_SIZE)
410 return -EINVAL;
411
8a9d2191
RK
412 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
413 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
414
415 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
416 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
c91cea11 417 printk(KERN_ERR "NILFS: too short segment.\n");
8a9d2191
RK
418 return -EINVAL;
419 }
420
421 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
422 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
423 nilfs->ns_r_segments_percentage =
424 le32_to_cpu(sbp->s_r_segments_percentage);
425 nilfs->ns_nrsvsegs =
426 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
427 DIV_ROUND_UP(nilfs->ns_nsegments *
428 nilfs->ns_r_segments_percentage, 100));
429 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
430 return 0;
431}
432
e339ad31
RK
433static int nilfs_valid_sb(struct nilfs_super_block *sbp)
434{
435 static unsigned char sum[4];
436 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
437 size_t bytes;
438 u32 crc;
439
440 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
441 return 0;
442 bytes = le16_to_cpu(sbp->s_bytes);
443 if (bytes > BLOCK_SIZE)
444 return 0;
445 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
446 sumoff);
447 crc = crc32_le(crc, sum, 4);
448 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
449 bytes - sumoff - 4);
450 return crc == le32_to_cpu(sbp->s_sum);
451}
452
453static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
454{
455 return offset < ((le64_to_cpu(sbp->s_nsegments) *
456 le32_to_cpu(sbp->s_blocks_per_segment)) <<
457 (le32_to_cpu(sbp->s_log_block_size) + 10));
458}
459
460static void nilfs_release_super_block(struct the_nilfs *nilfs)
461{
462 int i;
463
464 for (i = 0; i < 2; i++) {
465 if (nilfs->ns_sbp[i]) {
466 brelse(nilfs->ns_sbh[i]);
467 nilfs->ns_sbh[i] = NULL;
468 nilfs->ns_sbp[i] = NULL;
469 }
470 }
471}
472
473void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
474{
475 brelse(nilfs->ns_sbh[0]);
476 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
477 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
478 nilfs->ns_sbh[1] = NULL;
479 nilfs->ns_sbp[1] = NULL;
480}
481
482void nilfs_swap_super_block(struct the_nilfs *nilfs)
483{
484 struct buffer_head *tsbh = nilfs->ns_sbh[0];
485 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
486
487 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
488 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
489 nilfs->ns_sbh[1] = tsbh;
490 nilfs->ns_sbp[1] = tsbp;
491}
492
493static int nilfs_load_super_block(struct the_nilfs *nilfs,
494 struct super_block *sb, int blocksize,
495 struct nilfs_super_block **sbpp)
496{
497 struct nilfs_super_block **sbp = nilfs->ns_sbp;
498 struct buffer_head **sbh = nilfs->ns_sbh;
499 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
500 int valid[2], swp = 0;
501
502 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
503 &sbh[0]);
504 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
505
506 if (!sbp[0]) {
507 if (!sbp[1]) {
508 printk(KERN_ERR "NILFS: unable to read superblock\n");
509 return -EIO;
510 }
511 printk(KERN_WARNING
512 "NILFS warning: unable to read primary superblock\n");
513 } else if (!sbp[1])
514 printk(KERN_WARNING
515 "NILFS warning: unable to read secondary superblock\n");
516
25294d8c
RK
517 /*
518 * Compare two super blocks and set 1 in swp if the secondary
519 * super block is valid and newer. Otherwise, set 0 in swp.
520 */
e339ad31
RK
521 valid[0] = nilfs_valid_sb(sbp[0]);
522 valid[1] = nilfs_valid_sb(sbp[1]);
25294d8c
RK
523 swp = valid[1] && (!valid[0] ||
524 le64_to_cpu(sbp[1]->s_last_cno) >
525 le64_to_cpu(sbp[0]->s_last_cno));
e339ad31
RK
526
527 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
528 brelse(sbh[1]);
529 sbh[1] = NULL;
530 sbp[1] = NULL;
531 swp = 0;
532 }
533 if (!valid[swp]) {
534 nilfs_release_super_block(nilfs);
535 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
536 sb->s_id);
537 return -EINVAL;
538 }
539
540 if (swp) {
541 printk(KERN_WARNING "NILFS warning: broken superblock. "
542 "using spare superblock.\n");
543 nilfs_swap_super_block(nilfs);
544 }
545
b2ac86e1
JS
546 nilfs->ns_sbwcount = 0;
547 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
e339ad31
RK
548 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
549 *sbpp = sbp[0];
550 return 0;
551}
552
8a9d2191
RK
553/**
554 * init_nilfs - initialize a NILFS instance.
555 * @nilfs: the_nilfs structure
556 * @sbi: nilfs_sb_info
557 * @sb: super block
558 * @data: mount options
559 *
560 * init_nilfs() performs common initialization per block device (e.g.
561 * reading the super block, getting disk layout information, initializing
562 * shared fields in the_nilfs). It takes on some portion of the jobs
563 * typically done by a fill_super() routine. This division arises from
564 * the nature that multiple NILFS instances may be simultaneously
565 * mounted on a device.
566 * For multiple mounts on the same device, only the first mount
567 * invokes these tasks.
568 *
569 * Return Value: On success, 0 is returned. On error, a negative error
570 * code is returned.
571 */
572int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
573{
574 struct super_block *sb = sbi->s_super;
8a9d2191
RK
575 struct nilfs_super_block *sbp;
576 struct backing_dev_info *bdi;
577 int blocksize;
e339ad31 578 int err;
8a9d2191
RK
579
580 down_write(&nilfs->ns_sem);
581 if (nilfs_init(nilfs)) {
582 /* Load values from existing the_nilfs */
e339ad31 583 sbp = nilfs->ns_sbp[0];
8a9d2191
RK
584 err = nilfs_store_magic_and_option(sb, sbp, data);
585 if (err)
586 goto out;
587
588 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
589 if (sb->s_blocksize != blocksize &&
590 !sb_set_blocksize(sb, blocksize)) {
591 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
592 blocksize);
593 err = -EINVAL;
594 }
595 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
596 goto out;
597 }
598
e339ad31
RK
599 blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
600 if (!blocksize) {
601 printk(KERN_ERR "NILFS: unable to set blocksize\n");
8a9d2191
RK
602 err = -EINVAL;
603 goto out;
604 }
e339ad31
RK
605 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
606 if (err)
607 goto out;
608
8a9d2191
RK
609 err = nilfs_store_magic_and_option(sb, sbp, data);
610 if (err)
611 goto failed_sbh;
612
613 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
614 if (sb->s_blocksize != blocksize) {
e1defc4f 615 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
e339ad31
RK
616
617 if (blocksize < hw_blocksize) {
618 printk(KERN_ERR
619 "NILFS: blocksize %d too small for device "
620 "(sector-size = %d).\n",
621 blocksize, hw_blocksize);
8a9d2191 622 err = -EINVAL;
e339ad31
RK
623 goto failed_sbh;
624 }
625 nilfs_release_super_block(nilfs);
626 sb_set_blocksize(sb, blocksize);
627
628 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
629 if (err)
8a9d2191
RK
630 goto out;
631 /* not failed_sbh; sbh is released automatically
632 when reloading fails. */
8a9d2191
RK
633 }
634 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
92c60cca 635 nilfs->ns_blocksize = blocksize;
8a9d2191 636
e339ad31 637 err = nilfs_store_disk_layout(nilfs, sbp);
8a9d2191
RK
638 if (err)
639 goto failed_sbh;
640
641 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
642
643 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
8a9d2191 644
2c96ce9f 645 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
8a9d2191
RK
646 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
647
843d63ba
RK
648 err = nilfs_store_log_cursor(nilfs, sbp);
649 if (err)
8a9d2191 650 goto failed_sbh;
8a9d2191
RK
651
652 /* Initialize gcinode cache */
653 err = nilfs_init_gccache(nilfs);
654 if (err)
655 goto failed_sbh;
656
657 set_nilfs_init(nilfs);
658 err = 0;
659 out:
660 up_write(&nilfs->ns_sem);
661 return err;
662
663 failed_sbh:
e339ad31 664 nilfs_release_super_block(nilfs);
8a9d2191
RK
665 goto out;
666}
667
e902ec99
JS
668int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
669 size_t nsegs)
670{
671 sector_t seg_start, seg_end;
672 sector_t start = 0, nblocks = 0;
673 unsigned int sects_per_block;
674 __u64 *sn;
675 int ret = 0;
676
677 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
678 bdev_logical_block_size(nilfs->ns_bdev);
679 for (sn = segnump; sn < segnump + nsegs; sn++) {
680 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
681
682 if (!nblocks) {
683 start = seg_start;
684 nblocks = seg_end - seg_start + 1;
685 } else if (start + nblocks == seg_start) {
686 nblocks += seg_end - seg_start + 1;
687 } else {
688 ret = blkdev_issue_discard(nilfs->ns_bdev,
689 start * sects_per_block,
690 nblocks * sects_per_block,
691 GFP_NOFS,
6a47dc14 692 BLKDEV_IFL_BARRIER);
e902ec99
JS
693 if (ret < 0)
694 return ret;
695 nblocks = 0;
696 }
697 }
698 if (nblocks)
699 ret = blkdev_issue_discard(nilfs->ns_bdev,
700 start * sects_per_block,
701 nblocks * sects_per_block,
6a47dc14 702 GFP_NOFS, BLKDEV_IFL_BARRIER);
e902ec99
JS
703 return ret;
704}
705
8a9d2191
RK
706int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
707{
708 struct inode *dat = nilfs_dat_inode(nilfs);
709 unsigned long ncleansegs;
8a9d2191
RK
710
711 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
ef7d4757 712 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
8a9d2191 713 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
ef7d4757
RK
714 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
715 return 0;
8a9d2191
RK
716}
717
8a9d2191
RK
718int nilfs_near_disk_full(struct the_nilfs *nilfs)
719{
8a9d2191 720 unsigned long ncleansegs, nincsegs;
ef7d4757
RK
721
722 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
723 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
724 nilfs->ns_blocks_per_segment + 1;
725
726 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
8a9d2191
RK
727}
728
6dd47406
RK
729/**
730 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
731 * @nilfs: nilfs object
732 * @rw_mount: mount type (non-zero value for read/write mount)
733 * @cno: checkpoint number (zero for read-only mount)
734 *
735 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
736 * @rw_mount and @cno (in case of snapshots) matched. If no instance
737 * was found, NULL is returned. Although the super block instance can
738 * be unmounted after this function returns, the nilfs_sb_info struct
739 * is kept on memory until nilfs_put_sbinfo() is called.
740 */
741struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
742 int rw_mount, __u64 cno)
743{
744 struct nilfs_sb_info *sbi;
745
e59399d0 746 down_read(&nilfs->ns_super_sem);
6dd47406
RK
747 /*
748 * The SNAPSHOT flag and sb->s_flags are supposed to be
e59399d0 749 * protected with nilfs->ns_super_sem.
6dd47406
RK
750 */
751 sbi = nilfs->ns_current;
752 if (rw_mount) {
753 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
754 goto found; /* read/write mount */
755 else
756 goto out;
757 } else if (cno == 0) {
758 if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
759 goto found; /* read-only mount */
760 else
761 goto out;
762 }
763
764 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
765 if (nilfs_test_opt(sbi, SNAPSHOT) &&
766 sbi->s_snapshot_cno == cno)
767 goto found; /* snapshot mount */
768 }
769 out:
e59399d0 770 up_read(&nilfs->ns_super_sem);
6dd47406
RK
771 return NULL;
772
773 found:
774 atomic_inc(&sbi->s_count);
e59399d0 775 up_read(&nilfs->ns_super_sem);
6dd47406
RK
776 return sbi;
777}
778
8a9d2191
RK
779int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
780 int snapshot_mount)
781{
782 struct nilfs_sb_info *sbi;
783 int ret = 0;
784
e59399d0 785 down_read(&nilfs->ns_super_sem);
8a9d2191
RK
786 if (cno == 0 || cno > nilfs->ns_cno)
787 goto out_unlock;
788
789 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
790 if (sbi->s_snapshot_cno == cno &&
791 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
792 /* exclude read-only mounts */
793 ret++;
794 break;
795 }
796 }
797 /* for protecting recent checkpoints */
798 if (cno >= nilfs_last_cno(nilfs))
799 ret++;
800
801 out_unlock:
e59399d0 802 up_read(&nilfs->ns_super_sem);
8a9d2191
RK
803 return ret;
804}
This page took 0.234482 seconds and 5 git commands to generate.