1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * super.c - NILFS module and super block management.
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
7 * Written by Ryusuke Konishi.
8 */
9 /*
10 * linux/fs/ext2/super.c
11 *
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
16 *
17 * from
18 *
19 * linux/fs/minix/inode.c
20 *
21 * Copyright (C) 1991, 1992 Linus Torvalds
22 *
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
25 */
26
27 #include <linux/module.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/blkdev.h>
32 #include <linux/parser.h>
33 #include <linux/crc32.h>
34 #include <linux/vfs.h>
35 #include <linux/writeback.h>
36 #include <linux/seq_file.h>
37 #include <linux/mount.h>
38 #include "nilfs.h"
39 #include "export.h"
40 #include "mdt.h"
41 #include "alloc.h"
42 #include "btree.h"
43 #include "btnode.h"
44 #include "page.h"
45 #include "cpfile.h"
46 #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
47 #include "ifile.h"
48 #include "dat.h"
49 #include "segment.h"
50 #include "segbuf.h"
51
52 MODULE_AUTHOR("NTT Corp.");
53 MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
54 "(NILFS)");
55 MODULE_LICENSE("GPL");
56
57 static struct kmem_cache *nilfs_inode_cachep;
58 struct kmem_cache *nilfs_transaction_cachep;
59 struct kmem_cache *nilfs_segbuf_cachep;
60 struct kmem_cache *nilfs_btree_path_cache;
61
62 static int nilfs_setup_super(struct super_block *sb, int is_mount);
63 static int nilfs_remount(struct super_block *sb, int *flags, char *data);
64
__nilfs_msg(struct super_block * sb,const char * level,const char * fmt,...)65 void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt,
66 ...)
67 {
68 struct va_format vaf;
69 va_list args;
70
71 va_start(args, fmt);
72 vaf.fmt = fmt;
73 vaf.va = &args;
74 if (sb)
75 printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf);
76 else
77 printk("%sNILFS: %pV\n", level, &vaf);
78 va_end(args);
79 }
80
nilfs_set_error(struct super_block * sb)81 static void nilfs_set_error(struct super_block *sb)
82 {
83 struct the_nilfs *nilfs = sb->s_fs_info;
84 struct nilfs_super_block **sbp;
85
86 down_write(&nilfs->ns_sem);
87 if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
88 nilfs->ns_mount_state |= NILFS_ERROR_FS;
89 sbp = nilfs_prepare_super(sb, 0);
90 if (likely(sbp)) {
91 sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
92 if (sbp[1])
93 sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
94 nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
95 }
96 }
97 up_write(&nilfs->ns_sem);
98 }
99
100 /**
101 * __nilfs_error() - report failure condition on a filesystem
102 *
103 * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
104 * reporting an error message. This function should be called when
105 * NILFS detects incoherences or defects of meta data on disk.
106 *
107 * This implements the body of nilfs_error() macro. Normally,
108 * nilfs_error() should be used. As for sustainable errors such as a
109 * single-shot I/O error, nilfs_msg() should be used instead.
110 *
111 * Callers should not add a trailing newline since this will do it.
112 */
__nilfs_error(struct super_block * sb,const char * function,const char * fmt,...)113 void __nilfs_error(struct super_block *sb, const char *function,
114 const char *fmt, ...)
115 {
116 struct the_nilfs *nilfs = sb->s_fs_info;
117 struct va_format vaf;
118 va_list args;
119
120 va_start(args, fmt);
121
122 vaf.fmt = fmt;
123 vaf.va = &args;
124
125 printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
126 sb->s_id, function, &vaf);
127
128 va_end(args);
129
130 if (!sb_rdonly(sb)) {
131 nilfs_set_error(sb);
132
133 if (nilfs_test_opt(nilfs, ERRORS_RO)) {
134 printk(KERN_CRIT "Remounting filesystem read-only\n");
135 sb->s_flags |= SB_RDONLY;
136 }
137 }
138
139 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
140 panic("NILFS (device %s): panic forced after error\n",
141 sb->s_id);
142 }
143
nilfs_alloc_inode(struct super_block * sb)144 struct inode *nilfs_alloc_inode(struct super_block *sb)
145 {
146 struct nilfs_inode_info *ii;
147
148 ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
149 if (!ii)
150 return NULL;
151 ii->i_bh = NULL;
152 ii->i_state = 0;
153 ii->i_cno = 0;
154 ii->i_assoc_inode = NULL;
155 ii->i_bmap = &ii->i_bmap_data;
156 return &ii->vfs_inode;
157 }
158
nilfs_i_callback(struct rcu_head * head)159 static void nilfs_i_callback(struct rcu_head *head)
160 {
161 struct inode *inode = container_of(head, struct inode, i_rcu);
162
163 if (nilfs_is_metadata_file_inode(inode))
164 nilfs_mdt_destroy(inode);
165
166 kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
167 }
168
nilfs_destroy_inode(struct inode * inode)169 void nilfs_destroy_inode(struct inode *inode)
170 {
171 call_rcu(&inode->i_rcu, nilfs_i_callback);
172 }
173
nilfs_sync_super(struct super_block * sb,int flag)174 static int nilfs_sync_super(struct super_block *sb, int flag)
175 {
176 struct the_nilfs *nilfs = sb->s_fs_info;
177 int err;
178
179 retry:
180 set_buffer_dirty(nilfs->ns_sbh[0]);
181 if (nilfs_test_opt(nilfs, BARRIER)) {
182 err = __sync_dirty_buffer(nilfs->ns_sbh[0],
183 REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
184 } else {
185 err = sync_dirty_buffer(nilfs->ns_sbh[0]);
186 }
187
188 if (unlikely(err)) {
189 nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
190 err);
191 if (err == -EIO && nilfs->ns_sbh[1]) {
192 /*
193 * sbp[0] points to newer log than sbp[1],
194 * so copy sbp[0] to sbp[1] to take over sbp[0].
195 */
196 memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
197 nilfs->ns_sbsize);
198 nilfs_fall_back_super_block(nilfs);
199 goto retry;
200 }
201 } else {
202 struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
203
204 nilfs->ns_sbwcount++;
205
206 /*
207 * The latest segment becomes trailable from the position
208 * written in superblock.
209 */
210 clear_nilfs_discontinued(nilfs);
211
212 /* update GC protection for recent segments */
213 if (nilfs->ns_sbh[1]) {
214 if (flag == NILFS_SB_COMMIT_ALL) {
215 set_buffer_dirty(nilfs->ns_sbh[1]);
216 if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
217 goto out;
218 }
219 if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
220 le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
221 sbp = nilfs->ns_sbp[1];
222 }
223
224 spin_lock(&nilfs->ns_last_segment_lock);
225 nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
226 spin_unlock(&nilfs->ns_last_segment_lock);
227 }
228 out:
229 return err;
230 }
231
nilfs_set_log_cursor(struct nilfs_super_block * sbp,struct the_nilfs * nilfs)232 void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
233 struct the_nilfs *nilfs)
234 {
235 sector_t nfreeblocks;
236
237 /* nilfs->ns_sem must be locked by the caller. */
238 nilfs_count_free_blocks(nilfs, &nfreeblocks);
239 sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
240
241 spin_lock(&nilfs->ns_last_segment_lock);
242 sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
243 sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
244 sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
245 spin_unlock(&nilfs->ns_last_segment_lock);
246 }
247
nilfs_prepare_super(struct super_block * sb,int flip)248 struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
249 int flip)
250 {
251 struct the_nilfs *nilfs = sb->s_fs_info;
252 struct nilfs_super_block **sbp = nilfs->ns_sbp;
253
254 /* nilfs->ns_sem must be locked by the caller. */
255 if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
256 if (sbp[1] &&
257 sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
258 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
259 } else {
260 nilfs_msg(sb, KERN_CRIT, "superblock broke");
261 return NULL;
262 }
263 } else if (sbp[1] &&
264 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
265 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
266 }
267
268 if (flip && sbp[1])
269 nilfs_swap_super_block(nilfs);
270
271 return sbp;
272 }
273
nilfs_commit_super(struct super_block * sb,int flag)274 int nilfs_commit_super(struct super_block *sb, int flag)
275 {
276 struct the_nilfs *nilfs = sb->s_fs_info;
277 struct nilfs_super_block **sbp = nilfs->ns_sbp;
278 time64_t t;
279
280 /* nilfs->ns_sem must be locked by the caller. */
281 t = ktime_get_real_seconds();
282 nilfs->ns_sbwtime = t;
283 sbp[0]->s_wtime = cpu_to_le64(t);
284 sbp[0]->s_sum = 0;
285 sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
286 (unsigned char *)sbp[0],
287 nilfs->ns_sbsize));
288 if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
289 sbp[1]->s_wtime = sbp[0]->s_wtime;
290 sbp[1]->s_sum = 0;
291 sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
292 (unsigned char *)sbp[1],
293 nilfs->ns_sbsize));
294 }
295 clear_nilfs_sb_dirty(nilfs);
296 nilfs->ns_flushed_device = 1;
297 /* make sure store to ns_flushed_device cannot be reordered */
298 smp_wmb();
299 return nilfs_sync_super(sb, flag);
300 }
301
302 /**
303 * nilfs_cleanup_super() - write filesystem state for cleanup
304 * @sb: super block instance to be unmounted or degraded to read-only
305 *
306 * This function restores state flags in the on-disk super block.
307 * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
308 * filesystem was not clean previously.
309 */
nilfs_cleanup_super(struct super_block * sb)310 int nilfs_cleanup_super(struct super_block *sb)
311 {
312 struct the_nilfs *nilfs = sb->s_fs_info;
313 struct nilfs_super_block **sbp;
314 int flag = NILFS_SB_COMMIT;
315 int ret = -EIO;
316
317 sbp = nilfs_prepare_super(sb, 0);
318 if (sbp) {
319 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
320 nilfs_set_log_cursor(sbp[0], nilfs);
321 if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
322 /*
323 * make the "clean" flag also to the opposite
324 * super block if both super blocks point to
325 * the same checkpoint.
326 */
327 sbp[1]->s_state = sbp[0]->s_state;
328 flag = NILFS_SB_COMMIT_ALL;
329 }
330 ret = nilfs_commit_super(sb, flag);
331 }
332 return ret;
333 }
334
335 /**
336 * nilfs_move_2nd_super - relocate secondary super block
337 * @sb: super block instance
338 * @sb2off: new offset of the secondary super block (in bytes)
339 */
nilfs_move_2nd_super(struct super_block * sb,loff_t sb2off)340 static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
341 {
342 struct the_nilfs *nilfs = sb->s_fs_info;
343 struct buffer_head *nsbh;
344 struct nilfs_super_block *nsbp;
345 sector_t blocknr, newblocknr;
346 unsigned long offset;
347 int sb2i; /* array index of the secondary superblock */
348 int ret = 0;
349
350 /* nilfs->ns_sem must be locked by the caller. */
351 if (nilfs->ns_sbh[1] &&
352 nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
353 sb2i = 1;
354 blocknr = nilfs->ns_sbh[1]->b_blocknr;
355 } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
356 sb2i = 0;
357 blocknr = nilfs->ns_sbh[0]->b_blocknr;
358 } else {
359 sb2i = -1;
360 blocknr = 0;
361 }
362 if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
363 goto out; /* super block location is unchanged */
364
365 /* Get new super block buffer */
366 newblocknr = sb2off >> nilfs->ns_blocksize_bits;
367 offset = sb2off & (nilfs->ns_blocksize - 1);
368 nsbh = sb_getblk(sb, newblocknr);
369 if (!nsbh) {
370 nilfs_msg(sb, KERN_WARNING,
371 "unable to move secondary superblock to block %llu",
372 (unsigned long long)newblocknr);
373 ret = -EIO;
374 goto out;
375 }
376 nsbp = (void *)nsbh->b_data + offset;
377
378 lock_buffer(nsbh);
379 if (sb2i >= 0) {
380 /*
381 * The position of the second superblock only changes by 4KiB,
382 * which is larger than the maximum superblock data size
383 * (= 1KiB), so there is no need to use memmove() to allow
384 * overlap between source and destination.
385 */
386 memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
387
388 /*
389 * Zero fill after copy to avoid overwriting in case of move
390 * within the same block.
391 */
392 memset(nsbh->b_data, 0, offset);
393 memset((void *)nsbp + nilfs->ns_sbsize, 0,
394 nsbh->b_size - offset - nilfs->ns_sbsize);
395 } else {
396 memset(nsbh->b_data, 0, nsbh->b_size);
397 }
398 set_buffer_uptodate(nsbh);
399 unlock_buffer(nsbh);
400
401 if (sb2i >= 0) {
402 brelse(nilfs->ns_sbh[sb2i]);
403 nilfs->ns_sbh[sb2i] = nsbh;
404 nilfs->ns_sbp[sb2i] = nsbp;
405 } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
406 /* secondary super block will be restored to index 1 */
407 nilfs->ns_sbh[1] = nsbh;
408 nilfs->ns_sbp[1] = nsbp;
409 } else {
410 brelse(nsbh);
411 }
412 out:
413 return ret;
414 }
415
416 /**
417 * nilfs_resize_fs - resize the filesystem
418 * @sb: super block instance
419 * @newsize: new size of the filesystem (in bytes)
420 */
nilfs_resize_fs(struct super_block * sb,__u64 newsize)421 int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
422 {
423 struct the_nilfs *nilfs = sb->s_fs_info;
424 struct nilfs_super_block **sbp;
425 __u64 devsize, newnsegs;
426 loff_t sb2off;
427 int ret;
428
429 ret = -ERANGE;
430 devsize = i_size_read(sb->s_bdev->bd_inode);
431 if (newsize > devsize)
432 goto out;
433
434 /*
435 * Prevent underflow in second superblock position calculation.
436 * The exact minimum size check is done in nilfs_sufile_resize().
437 */
438 if (newsize < 4096) {
439 ret = -ENOSPC;
440 goto out;
441 }
442
443 /*
444 * Write lock is required to protect some functions depending
445 * on the number of segments, the number of reserved segments,
446 * and so forth.
447 */
448 down_write(&nilfs->ns_segctor_sem);
449
450 sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
451 newnsegs = sb2off >> nilfs->ns_blocksize_bits;
452 do_div(newnsegs, nilfs->ns_blocks_per_segment);
453
454 ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
455 up_write(&nilfs->ns_segctor_sem);
456 if (ret < 0)
457 goto out;
458
459 ret = nilfs_construct_segment(sb);
460 if (ret < 0)
461 goto out;
462
463 down_write(&nilfs->ns_sem);
464 nilfs_move_2nd_super(sb, sb2off);
465 ret = -EIO;
466 sbp = nilfs_prepare_super(sb, 0);
467 if (likely(sbp)) {
468 nilfs_set_log_cursor(sbp[0], nilfs);
469 /*
470 * Drop NILFS_RESIZE_FS flag for compatibility with
471 * mount-time resize which may be implemented in a
472 * future release.
473 */
474 sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
475 ~NILFS_RESIZE_FS);
476 sbp[0]->s_dev_size = cpu_to_le64(newsize);
477 sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
478 if (sbp[1])
479 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
480 ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
481 }
482 up_write(&nilfs->ns_sem);
483
484 /*
485 * Reset the range of allocatable segments last. This order
486 * is important in the case of expansion because the secondary
487 * superblock must be protected from log write until migration
488 * completes.
489 */
490 if (!ret)
491 nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
492 out:
493 return ret;
494 }
495
nilfs_put_super(struct super_block * sb)496 static void nilfs_put_super(struct super_block *sb)
497 {
498 struct the_nilfs *nilfs = sb->s_fs_info;
499
500 nilfs_detach_log_writer(sb);
501
502 if (!sb_rdonly(sb)) {
503 down_write(&nilfs->ns_sem);
504 nilfs_cleanup_super(sb);
505 up_write(&nilfs->ns_sem);
506 }
507
508 nilfs_sysfs_delete_device_group(nilfs);
509 iput(nilfs->ns_sufile);
510 iput(nilfs->ns_cpfile);
511 iput(nilfs->ns_dat);
512
513 destroy_nilfs(nilfs);
514 sb->s_fs_info = NULL;
515 }
516
nilfs_sync_fs(struct super_block * sb,int wait)517 static int nilfs_sync_fs(struct super_block *sb, int wait)
518 {
519 struct the_nilfs *nilfs = sb->s_fs_info;
520 struct nilfs_super_block **sbp;
521 int err = 0;
522
523 /* This function is called when super block should be written back */
524 if (wait)
525 err = nilfs_construct_segment(sb);
526
527 down_write(&nilfs->ns_sem);
528 if (nilfs_sb_dirty(nilfs)) {
529 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
530 if (likely(sbp)) {
531 nilfs_set_log_cursor(sbp[0], nilfs);
532 nilfs_commit_super(sb, NILFS_SB_COMMIT);
533 }
534 }
535 up_write(&nilfs->ns_sem);
536
537 if (!err)
538 err = nilfs_flush_device(nilfs);
539
540 return err;
541 }
542
nilfs_attach_checkpoint(struct super_block * sb,__u64 cno,int curr_mnt,struct nilfs_root ** rootp)543 int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
544 struct nilfs_root **rootp)
545 {
546 struct the_nilfs *nilfs = sb->s_fs_info;
547 struct nilfs_root *root;
548 struct nilfs_checkpoint *raw_cp;
549 struct buffer_head *bh_cp;
550 int err = -ENOMEM;
551
552 root = nilfs_find_or_create_root(
553 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
554 if (!root)
555 return err;
556
557 if (root->ifile)
558 goto reuse; /* already attached checkpoint */
559
560 down_read(&nilfs->ns_segctor_sem);
561 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
562 &bh_cp);
563 up_read(&nilfs->ns_segctor_sem);
564 if (unlikely(err)) {
565 if (err == -ENOENT || err == -EINVAL) {
566 nilfs_msg(sb, KERN_ERR,
567 "Invalid checkpoint (checkpoint number=%llu)",
568 (unsigned long long)cno);
569 err = -EINVAL;
570 }
571 goto failed;
572 }
573
574 err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
575 &raw_cp->cp_ifile_inode, &root->ifile);
576 if (err)
577 goto failed_bh;
578
579 atomic64_set(&root->inodes_count,
580 le64_to_cpu(raw_cp->cp_inodes_count));
581 atomic64_set(&root->blocks_count,
582 le64_to_cpu(raw_cp->cp_blocks_count));
583
584 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
585
586 reuse:
587 *rootp = root;
588 return 0;
589
590 failed_bh:
591 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
592 failed:
593 nilfs_put_root(root);
594
595 return err;
596 }
597
nilfs_freeze(struct super_block * sb)598 static int nilfs_freeze(struct super_block *sb)
599 {
600 struct the_nilfs *nilfs = sb->s_fs_info;
601 int err;
602
603 if (sb_rdonly(sb))
604 return 0;
605
606 /* Mark super block clean */
607 down_write(&nilfs->ns_sem);
608 err = nilfs_cleanup_super(sb);
609 up_write(&nilfs->ns_sem);
610 return err;
611 }
612
nilfs_unfreeze(struct super_block * sb)613 static int nilfs_unfreeze(struct super_block *sb)
614 {
615 struct the_nilfs *nilfs = sb->s_fs_info;
616
617 if (sb_rdonly(sb))
618 return 0;
619
620 down_write(&nilfs->ns_sem);
621 nilfs_setup_super(sb, false);
622 up_write(&nilfs->ns_sem);
623 return 0;
624 }
625
nilfs_statfs(struct dentry * dentry,struct kstatfs * buf)626 static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
627 {
628 struct super_block *sb = dentry->d_sb;
629 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
630 struct the_nilfs *nilfs = root->nilfs;
631 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
632 unsigned long long blocks;
633 unsigned long overhead;
634 unsigned long nrsvblocks;
635 sector_t nfreeblocks;
636 u64 nmaxinodes, nfreeinodes;
637 int err;
638
639 /*
640 * Compute all of the segment blocks
641 *
642 * The blocks before first segment and after last segment
643 * are excluded.
644 */
645 blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
646 - nilfs->ns_first_data_block;
647 nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
648
649 /*
650 * Compute the overhead
651 *
652 * When distributing meta data blocks outside segment structure,
653 * We must count them as the overhead.
654 */
655 overhead = 0;
656
657 err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
658 if (unlikely(err))
659 return err;
660
661 err = nilfs_ifile_count_free_inodes(root->ifile,
662 &nmaxinodes, &nfreeinodes);
663 if (unlikely(err)) {
664 nilfs_msg(sb, KERN_WARNING,
665 "failed to count free inodes: err=%d", err);
666 if (err == -ERANGE) {
667 /*
668 * If nilfs_palloc_count_max_entries() returns
669 * -ERANGE error code then we simply treat
670 * curent inodes count as maximum possible and
671 * zero as free inodes value.
672 */
673 nmaxinodes = atomic64_read(&root->inodes_count);
674 nfreeinodes = 0;
675 err = 0;
676 } else
677 return err;
678 }
679
680 buf->f_type = NILFS_SUPER_MAGIC;
681 buf->f_bsize = sb->s_blocksize;
682 buf->f_blocks = blocks - overhead;
683 buf->f_bfree = nfreeblocks;
684 buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
685 (buf->f_bfree - nrsvblocks) : 0;
686 buf->f_files = nmaxinodes;
687 buf->f_ffree = nfreeinodes;
688 buf->f_namelen = NILFS_NAME_LEN;
689 buf->f_fsid.val[0] = (u32)id;
690 buf->f_fsid.val[1] = (u32)(id >> 32);
691
692 return 0;
693 }
694
nilfs_show_options(struct seq_file * seq,struct dentry * dentry)695 static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
696 {
697 struct super_block *sb = dentry->d_sb;
698 struct the_nilfs *nilfs = sb->s_fs_info;
699 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
700
701 if (!nilfs_test_opt(nilfs, BARRIER))
702 seq_puts(seq, ",nobarrier");
703 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
704 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
705 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
706 seq_puts(seq, ",errors=panic");
707 if (nilfs_test_opt(nilfs, ERRORS_CONT))
708 seq_puts(seq, ",errors=continue");
709 if (nilfs_test_opt(nilfs, STRICT_ORDER))
710 seq_puts(seq, ",order=strict");
711 if (nilfs_test_opt(nilfs, NORECOVERY))
712 seq_puts(seq, ",norecovery");
713 if (nilfs_test_opt(nilfs, DISCARD))
714 seq_puts(seq, ",discard");
715
716 return 0;
717 }
718
719 static const struct super_operations nilfs_sops = {
720 .alloc_inode = nilfs_alloc_inode,
721 .destroy_inode = nilfs_destroy_inode,
722 .dirty_inode = nilfs_dirty_inode,
723 .evict_inode = nilfs_evict_inode,
724 .put_super = nilfs_put_super,
725 .sync_fs = nilfs_sync_fs,
726 .freeze_fs = nilfs_freeze,
727 .unfreeze_fs = nilfs_unfreeze,
728 .statfs = nilfs_statfs,
729 .remount_fs = nilfs_remount,
730 .show_options = nilfs_show_options
731 };
732
733 enum {
734 Opt_err_cont, Opt_err_panic, Opt_err_ro,
735 Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
736 Opt_discard, Opt_nodiscard, Opt_err,
737 };
738
739 static match_table_t tokens = {
740 {Opt_err_cont, "errors=continue"},
741 {Opt_err_panic, "errors=panic"},
742 {Opt_err_ro, "errors=remount-ro"},
743 {Opt_barrier, "barrier"},
744 {Opt_nobarrier, "nobarrier"},
745 {Opt_snapshot, "cp=%u"},
746 {Opt_order, "order=%s"},
747 {Opt_norecovery, "norecovery"},
748 {Opt_discard, "discard"},
749 {Opt_nodiscard, "nodiscard"},
750 {Opt_err, NULL}
751 };
752
parse_options(char * options,struct super_block * sb,int is_remount)753 static int parse_options(char *options, struct super_block *sb, int is_remount)
754 {
755 struct the_nilfs *nilfs = sb->s_fs_info;
756 char *p;
757 substring_t args[MAX_OPT_ARGS];
758
759 if (!options)
760 return 1;
761
762 while ((p = strsep(&options, ",")) != NULL) {
763 int token;
764
765 if (!*p)
766 continue;
767
768 token = match_token(p, tokens, args);
769 switch (token) {
770 case Opt_barrier:
771 nilfs_set_opt(nilfs, BARRIER);
772 break;
773 case Opt_nobarrier:
774 nilfs_clear_opt(nilfs, BARRIER);
775 break;
776 case Opt_order:
777 if (strcmp(args[0].from, "relaxed") == 0)
778 /* Ordered data semantics */
779 nilfs_clear_opt(nilfs, STRICT_ORDER);
780 else if (strcmp(args[0].from, "strict") == 0)
781 /* Strict in-order semantics */
782 nilfs_set_opt(nilfs, STRICT_ORDER);
783 else
784 return 0;
785 break;
786 case Opt_err_panic:
787 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
788 break;
789 case Opt_err_ro:
790 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
791 break;
792 case Opt_err_cont:
793 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
794 break;
795 case Opt_snapshot:
796 if (is_remount) {
797 nilfs_msg(sb, KERN_ERR,
798 "\"%s\" option is invalid for remount",
799 p);
800 return 0;
801 }
802 break;
803 case Opt_norecovery:
804 nilfs_set_opt(nilfs, NORECOVERY);
805 break;
806 case Opt_discard:
807 nilfs_set_opt(nilfs, DISCARD);
808 break;
809 case Opt_nodiscard:
810 nilfs_clear_opt(nilfs, DISCARD);
811 break;
812 default:
813 nilfs_msg(sb, KERN_ERR,
814 "unrecognized mount option \"%s\"", p);
815 return 0;
816 }
817 }
818 return 1;
819 }
820
821 static inline void
nilfs_set_default_options(struct super_block * sb,struct nilfs_super_block * sbp)822 nilfs_set_default_options(struct super_block *sb,
823 struct nilfs_super_block *sbp)
824 {
825 struct the_nilfs *nilfs = sb->s_fs_info;
826
827 nilfs->ns_mount_opt =
828 NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
829 }
830
nilfs_setup_super(struct super_block * sb,int is_mount)831 static int nilfs_setup_super(struct super_block *sb, int is_mount)
832 {
833 struct the_nilfs *nilfs = sb->s_fs_info;
834 struct nilfs_super_block **sbp;
835 int max_mnt_count;
836 int mnt_count;
837
838 /* nilfs->ns_sem must be locked by the caller. */
839 sbp = nilfs_prepare_super(sb, 0);
840 if (!sbp)
841 return -EIO;
842
843 if (!is_mount)
844 goto skip_mount_setup;
845
846 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
847 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
848
849 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
850 nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
851 #if 0
852 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
853 nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
854 #endif
855 }
856 if (!max_mnt_count)
857 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
858
859 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
860 sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
861
862 skip_mount_setup:
863 sbp[0]->s_state =
864 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
865 /* synchronize sbp[1] with sbp[0] */
866 if (sbp[1])
867 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
868 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
869 }
870
nilfs_read_super_block(struct super_block * sb,u64 pos,int blocksize,struct buffer_head ** pbh)871 struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
872 u64 pos, int blocksize,
873 struct buffer_head **pbh)
874 {
875 unsigned long long sb_index = pos;
876 unsigned long offset;
877
878 offset = do_div(sb_index, blocksize);
879 *pbh = sb_bread(sb, sb_index);
880 if (!*pbh)
881 return NULL;
882 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
883 }
884
nilfs_store_magic_and_option(struct super_block * sb,struct nilfs_super_block * sbp,char * data)885 int nilfs_store_magic_and_option(struct super_block *sb,
886 struct nilfs_super_block *sbp,
887 char *data)
888 {
889 struct the_nilfs *nilfs = sb->s_fs_info;
890
891 sb->s_magic = le16_to_cpu(sbp->s_magic);
892
893 /* FS independent flags */
894 #ifdef NILFS_ATIME_DISABLE
895 sb->s_flags |= SB_NOATIME;
896 #endif
897
898 nilfs_set_default_options(sb, sbp);
899
900 nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
901 nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
902 nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
903 nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
904
905 return !parse_options(data, sb, 0) ? -EINVAL : 0;
906 }
907
nilfs_check_feature_compatibility(struct super_block * sb,struct nilfs_super_block * sbp)908 int nilfs_check_feature_compatibility(struct super_block *sb,
909 struct nilfs_super_block *sbp)
910 {
911 __u64 features;
912
913 features = le64_to_cpu(sbp->s_feature_incompat) &
914 ~NILFS_FEATURE_INCOMPAT_SUPP;
915 if (features) {
916 nilfs_msg(sb, KERN_ERR,
917 "couldn't mount because of unsupported optional features (%llx)",
918 (unsigned long long)features);
919 return -EINVAL;
920 }
921 features = le64_to_cpu(sbp->s_feature_compat_ro) &
922 ~NILFS_FEATURE_COMPAT_RO_SUPP;
923 if (!sb_rdonly(sb) && features) {
924 nilfs_msg(sb, KERN_ERR,
925 "couldn't mount RDWR because of unsupported optional features (%llx)",
926 (unsigned long long)features);
927 return -EINVAL;
928 }
929 return 0;
930 }
931
nilfs_get_root_dentry(struct super_block * sb,struct nilfs_root * root,struct dentry ** root_dentry)932 static int nilfs_get_root_dentry(struct super_block *sb,
933 struct nilfs_root *root,
934 struct dentry **root_dentry)
935 {
936 struct inode *inode;
937 struct dentry *dentry;
938 int ret = 0;
939
940 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
941 if (IS_ERR(inode)) {
942 ret = PTR_ERR(inode);
943 nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
944 goto out;
945 }
946 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
947 iput(inode);
948 nilfs_msg(sb, KERN_ERR, "corrupt root inode");
949 ret = -EINVAL;
950 goto out;
951 }
952
953 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
954 dentry = d_find_alias(inode);
955 if (!dentry) {
956 dentry = d_make_root(inode);
957 if (!dentry) {
958 ret = -ENOMEM;
959 goto failed_dentry;
960 }
961 } else {
962 iput(inode);
963 }
964 } else {
965 dentry = d_obtain_root(inode);
966 if (IS_ERR(dentry)) {
967 ret = PTR_ERR(dentry);
968 goto failed_dentry;
969 }
970 }
971 *root_dentry = dentry;
972 out:
973 return ret;
974
975 failed_dentry:
976 nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
977 goto out;
978 }
979
nilfs_attach_snapshot(struct super_block * s,__u64 cno,struct dentry ** root_dentry)980 static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
981 struct dentry **root_dentry)
982 {
983 struct the_nilfs *nilfs = s->s_fs_info;
984 struct nilfs_root *root;
985 int ret;
986
987 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
988
989 down_read(&nilfs->ns_segctor_sem);
990 ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
991 up_read(&nilfs->ns_segctor_sem);
992 if (ret < 0) {
993 ret = (ret == -ENOENT) ? -EINVAL : ret;
994 goto out;
995 } else if (!ret) {
996 nilfs_msg(s, KERN_ERR,
997 "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
998 (unsigned long long)cno);
999 ret = -EINVAL;
1000 goto out;
1001 }
1002
1003 ret = nilfs_attach_checkpoint(s, cno, false, &root);
1004 if (ret) {
1005 nilfs_msg(s, KERN_ERR,
1006 "error %d while loading snapshot (checkpoint number=%llu)",
1007 ret, (unsigned long long)cno);
1008 goto out;
1009 }
1010 ret = nilfs_get_root_dentry(s, root, root_dentry);
1011 nilfs_put_root(root);
1012 out:
1013 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
1014 return ret;
1015 }
1016
1017 /**
1018 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
1019 * @root_dentry: root dentry of the tree to be shrunk
1020 *
1021 * This function returns true if the tree was in-use.
1022 */
nilfs_tree_is_busy(struct dentry * root_dentry)1023 static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1024 {
1025 shrink_dcache_parent(root_dentry);
1026 return d_count(root_dentry) > 1;
1027 }
1028
nilfs_checkpoint_is_mounted(struct super_block * sb,__u64 cno)1029 int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1030 {
1031 struct the_nilfs *nilfs = sb->s_fs_info;
1032 struct nilfs_root *root;
1033 struct inode *inode;
1034 struct dentry *dentry;
1035 int ret;
1036
1037 if (cno > nilfs->ns_cno)
1038 return false;
1039
1040 if (cno >= nilfs_last_cno(nilfs))
1041 return true; /* protect recent checkpoints */
1042
1043 ret = false;
1044 root = nilfs_lookup_root(nilfs, cno);
1045 if (root) {
1046 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1047 if (inode) {
1048 dentry = d_find_alias(inode);
1049 if (dentry) {
1050 ret = nilfs_tree_is_busy(dentry);
1051 dput(dentry);
1052 }
1053 iput(inode);
1054 }
1055 nilfs_put_root(root);
1056 }
1057 return ret;
1058 }
1059
1060 /**
1061 * nilfs_fill_super() - initialize a super block instance
1062 * @sb: super_block
1063 * @data: mount options
1064 * @silent: silent mode flag
1065 *
1066 * This function is called exclusively by nilfs->ns_mount_mutex.
1067 * So, the recovery process is protected from other simultaneous mounts.
1068 */
1069 static int
nilfs_fill_super(struct super_block * sb,void * data,int silent)1070 nilfs_fill_super(struct super_block *sb, void *data, int silent)
1071 {
1072 struct the_nilfs *nilfs;
1073 struct nilfs_root *fsroot;
1074 __u64 cno;
1075 int err;
1076
1077 nilfs = alloc_nilfs(sb);
1078 if (!nilfs)
1079 return -ENOMEM;
1080
1081 sb->s_fs_info = nilfs;
1082
1083 err = init_nilfs(nilfs, sb, (char *)data);
1084 if (err)
1085 goto failed_nilfs;
1086
1087 sb->s_op = &nilfs_sops;
1088 sb->s_export_op = &nilfs_export_ops;
1089 sb->s_root = NULL;
1090 sb->s_time_gran = 1;
1091 sb->s_max_links = NILFS_LINK_MAX;
1092
1093 sb->s_bdi = bdi_get(sb->s_bdev->bd_bdi);
1094
1095 err = load_nilfs(nilfs, sb);
1096 if (err)
1097 goto failed_nilfs;
1098
1099 cno = nilfs_last_cno(nilfs);
1100 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1101 if (err) {
1102 nilfs_msg(sb, KERN_ERR,
1103 "error %d while loading last checkpoint (checkpoint number=%llu)",
1104 err, (unsigned long long)cno);
1105 goto failed_unload;
1106 }
1107
1108 if (!sb_rdonly(sb)) {
1109 err = nilfs_attach_log_writer(sb, fsroot);
1110 if (err)
1111 goto failed_checkpoint;
1112 }
1113
1114 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1115 if (err)
1116 goto failed_segctor;
1117
1118 nilfs_put_root(fsroot);
1119
1120 if (!sb_rdonly(sb)) {
1121 down_write(&nilfs->ns_sem);
1122 nilfs_setup_super(sb, true);
1123 up_write(&nilfs->ns_sem);
1124 }
1125
1126 return 0;
1127
1128 failed_segctor:
1129 nilfs_detach_log_writer(sb);
1130
1131 failed_checkpoint:
1132 nilfs_put_root(fsroot);
1133
1134 failed_unload:
1135 nilfs_sysfs_delete_device_group(nilfs);
1136 iput(nilfs->ns_sufile);
1137 iput(nilfs->ns_cpfile);
1138 iput(nilfs->ns_dat);
1139
1140 failed_nilfs:
1141 destroy_nilfs(nilfs);
1142 return err;
1143 }
1144
nilfs_remount(struct super_block * sb,int * flags,char * data)1145 static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1146 {
1147 struct the_nilfs *nilfs = sb->s_fs_info;
1148 unsigned long old_sb_flags;
1149 unsigned long old_mount_opt;
1150 int err;
1151
1152 sync_filesystem(sb);
1153 old_sb_flags = sb->s_flags;
1154 old_mount_opt = nilfs->ns_mount_opt;
1155
1156 if (!parse_options(data, sb, 1)) {
1157 err = -EINVAL;
1158 goto restore_opts;
1159 }
1160 sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
1161
1162 err = -EINVAL;
1163
1164 if (!nilfs_valid_fs(nilfs)) {
1165 nilfs_msg(sb, KERN_WARNING,
1166 "couldn't remount because the filesystem is in an incomplete recovery state");
1167 goto restore_opts;
1168 }
1169
1170 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1171 goto out;
1172 if (*flags & SB_RDONLY) {
1173 sb->s_flags |= SB_RDONLY;
1174
1175 /*
1176 * Remounting a valid RW partition RDONLY, so set
1177 * the RDONLY flag and then mark the partition as valid again.
1178 */
1179 down_write(&nilfs->ns_sem);
1180 nilfs_cleanup_super(sb);
1181 up_write(&nilfs->ns_sem);
1182 } else {
1183 __u64 features;
1184 struct nilfs_root *root;
1185
1186 /*
1187 * Mounting a RDONLY partition read-write, so reread and
1188 * store the current valid flag. (It may have been changed
1189 * by fsck since we originally mounted the partition.)
1190 */
1191 down_read(&nilfs->ns_sem);
1192 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1193 ~NILFS_FEATURE_COMPAT_RO_SUPP;
1194 up_read(&nilfs->ns_sem);
1195 if (features) {
1196 nilfs_msg(sb, KERN_WARNING,
1197 "couldn't remount RDWR because of unsupported optional features (%llx)",
1198 (unsigned long long)features);
1199 err = -EROFS;
1200 goto restore_opts;
1201 }
1202
1203 sb->s_flags &= ~SB_RDONLY;
1204
1205 root = NILFS_I(d_inode(sb->s_root))->i_root;
1206 err = nilfs_attach_log_writer(sb, root);
1207 if (err)
1208 goto restore_opts;
1209
1210 down_write(&nilfs->ns_sem);
1211 nilfs_setup_super(sb, true);
1212 up_write(&nilfs->ns_sem);
1213 }
1214 out:
1215 return 0;
1216
1217 restore_opts:
1218 sb->s_flags = old_sb_flags;
1219 nilfs->ns_mount_opt = old_mount_opt;
1220 return err;
1221 }
1222
1223 struct nilfs_super_data {
1224 struct block_device *bdev;
1225 __u64 cno;
1226 int flags;
1227 };
1228
nilfs_parse_snapshot_option(const char * option,const substring_t * arg,struct nilfs_super_data * sd)1229 static int nilfs_parse_snapshot_option(const char *option,
1230 const substring_t *arg,
1231 struct nilfs_super_data *sd)
1232 {
1233 unsigned long long val;
1234 const char *msg = NULL;
1235 int err;
1236
1237 if (!(sd->flags & SB_RDONLY)) {
1238 msg = "read-only option is not specified";
1239 goto parse_error;
1240 }
1241
1242 err = kstrtoull(arg->from, 0, &val);
1243 if (err) {
1244 if (err == -ERANGE)
1245 msg = "too large checkpoint number";
1246 else
1247 msg = "malformed argument";
1248 goto parse_error;
1249 } else if (val == 0) {
1250 msg = "invalid checkpoint number 0";
1251 goto parse_error;
1252 }
1253 sd->cno = val;
1254 return 0;
1255
1256 parse_error:
1257 nilfs_msg(NULL, KERN_ERR, "invalid option \"%s\": %s", option, msg);
1258 return 1;
1259 }
1260
1261 /**
1262 * nilfs_identify - pre-read mount options needed to identify mount instance
1263 * @data: mount options
1264 * @sd: nilfs_super_data
1265 */
nilfs_identify(char * data,struct nilfs_super_data * sd)1266 static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1267 {
1268 char *p, *options = data;
1269 substring_t args[MAX_OPT_ARGS];
1270 int token;
1271 int ret = 0;
1272
1273 do {
1274 p = strsep(&options, ",");
1275 if (p != NULL && *p) {
1276 token = match_token(p, tokens, args);
1277 if (token == Opt_snapshot)
1278 ret = nilfs_parse_snapshot_option(p, &args[0],
1279 sd);
1280 }
1281 if (!options)
1282 break;
1283 BUG_ON(options == data);
1284 *(options - 1) = ',';
1285 } while (!ret);
1286 return ret;
1287 }
1288
nilfs_set_bdev_super(struct super_block * s,void * data)1289 static int nilfs_set_bdev_super(struct super_block *s, void *data)
1290 {
1291 s->s_bdev = data;
1292 s->s_dev = s->s_bdev->bd_dev;
1293 return 0;
1294 }
1295
nilfs_test_bdev_super(struct super_block * s,void * data)1296 static int nilfs_test_bdev_super(struct super_block *s, void *data)
1297 {
1298 return (void *)s->s_bdev == data;
1299 }
1300
1301 static struct dentry *
nilfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)1302 nilfs_mount(struct file_system_type *fs_type, int flags,
1303 const char *dev_name, void *data)
1304 {
1305 struct nilfs_super_data sd;
1306 struct super_block *s;
1307 fmode_t mode = FMODE_READ | FMODE_EXCL;
1308 struct dentry *root_dentry;
1309 int err, s_new = false;
1310
1311 if (!(flags & SB_RDONLY))
1312 mode |= FMODE_WRITE;
1313
1314 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1315 if (IS_ERR(sd.bdev))
1316 return ERR_CAST(sd.bdev);
1317
1318 sd.cno = 0;
1319 sd.flags = flags;
1320 if (nilfs_identify((char *)data, &sd)) {
1321 err = -EINVAL;
1322 goto failed;
1323 }
1324
1325 /*
1326 * once the super is inserted into the list by sget, s_umount
1327 * will protect the lockfs code from trying to start a snapshot
1328 * while we are mounting
1329 */
1330 mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
1331 if (sd.bdev->bd_fsfreeze_count > 0) {
1332 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
1333 err = -EBUSY;
1334 goto failed;
1335 }
1336 s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1337 sd.bdev);
1338 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
1339 if (IS_ERR(s)) {
1340 err = PTR_ERR(s);
1341 goto failed;
1342 }
1343
1344 if (!s->s_root) {
1345 s_new = true;
1346
1347 /* New superblock instance created */
1348 s->s_mode = mode;
1349 snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
1350 sb_set_blocksize(s, block_size(sd.bdev));
1351
1352 err = nilfs_fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1353 if (err)
1354 goto failed_super;
1355
1356 s->s_flags |= SB_ACTIVE;
1357 } else if (!sd.cno) {
1358 if (nilfs_tree_is_busy(s->s_root)) {
1359 if ((flags ^ s->s_flags) & SB_RDONLY) {
1360 nilfs_msg(s, KERN_ERR,
1361 "the device already has a %s mount.",
1362 sb_rdonly(s) ? "read-only" : "read/write");
1363 err = -EBUSY;
1364 goto failed_super;
1365 }
1366 } else {
1367 /*
1368 * Try remount to setup mount states if the current
1369 * tree is not mounted and only snapshots use this sb.
1370 */
1371 err = nilfs_remount(s, &flags, data);
1372 if (err)
1373 goto failed_super;
1374 }
1375 }
1376
1377 if (sd.cno) {
1378 err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1379 if (err)
1380 goto failed_super;
1381 } else {
1382 root_dentry = dget(s->s_root);
1383 }
1384
1385 if (!s_new)
1386 blkdev_put(sd.bdev, mode);
1387
1388 return root_dentry;
1389
1390 failed_super:
1391 deactivate_locked_super(s);
1392
1393 failed:
1394 if (!s_new)
1395 blkdev_put(sd.bdev, mode);
1396 return ERR_PTR(err);
1397 }
1398
1399 struct file_system_type nilfs_fs_type = {
1400 .owner = THIS_MODULE,
1401 .name = "nilfs2",
1402 .mount = nilfs_mount,
1403 .kill_sb = kill_block_super,
1404 .fs_flags = FS_REQUIRES_DEV,
1405 };
1406 MODULE_ALIAS_FS("nilfs2");
1407
nilfs_inode_init_once(void * obj)1408 static void nilfs_inode_init_once(void *obj)
1409 {
1410 struct nilfs_inode_info *ii = obj;
1411
1412 INIT_LIST_HEAD(&ii->i_dirty);
1413 #ifdef CONFIG_NILFS_XATTR
1414 init_rwsem(&ii->xattr_sem);
1415 #endif
1416 inode_init_once(&ii->vfs_inode);
1417 }
1418
nilfs_segbuf_init_once(void * obj)1419 static void nilfs_segbuf_init_once(void *obj)
1420 {
1421 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1422 }
1423
nilfs_destroy_cachep(void)1424 static void nilfs_destroy_cachep(void)
1425 {
1426 /*
1427 * Make sure all delayed rcu free inodes are flushed before we
1428 * destroy cache.
1429 */
1430 rcu_barrier();
1431
1432 kmem_cache_destroy(nilfs_inode_cachep);
1433 kmem_cache_destroy(nilfs_transaction_cachep);
1434 kmem_cache_destroy(nilfs_segbuf_cachep);
1435 kmem_cache_destroy(nilfs_btree_path_cache);
1436 }
1437
nilfs_init_cachep(void)1438 static int __init nilfs_init_cachep(void)
1439 {
1440 nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1441 sizeof(struct nilfs_inode_info), 0,
1442 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1443 nilfs_inode_init_once);
1444 if (!nilfs_inode_cachep)
1445 goto fail;
1446
1447 nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1448 sizeof(struct nilfs_transaction_info), 0,
1449 SLAB_RECLAIM_ACCOUNT, NULL);
1450 if (!nilfs_transaction_cachep)
1451 goto fail;
1452
1453 nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1454 sizeof(struct nilfs_segment_buffer), 0,
1455 SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1456 if (!nilfs_segbuf_cachep)
1457 goto fail;
1458
1459 nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1460 sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1461 0, 0, NULL);
1462 if (!nilfs_btree_path_cache)
1463 goto fail;
1464
1465 return 0;
1466
1467 fail:
1468 nilfs_destroy_cachep();
1469 return -ENOMEM;
1470 }
1471
init_nilfs_fs(void)1472 static int __init init_nilfs_fs(void)
1473 {
1474 int err;
1475
1476 err = nilfs_init_cachep();
1477 if (err)
1478 goto fail;
1479
1480 err = nilfs_sysfs_init();
1481 if (err)
1482 goto free_cachep;
1483
1484 err = register_filesystem(&nilfs_fs_type);
1485 if (err)
1486 goto deinit_sysfs_entry;
1487
1488 printk(KERN_INFO "NILFS version 2 loaded\n");
1489 return 0;
1490
1491 deinit_sysfs_entry:
1492 nilfs_sysfs_exit();
1493 free_cachep:
1494 nilfs_destroy_cachep();
1495 fail:
1496 return err;
1497 }
1498
exit_nilfs_fs(void)1499 static void __exit exit_nilfs_fs(void)
1500 {
1501 nilfs_destroy_cachep();
1502 nilfs_sysfs_exit();
1503 unregister_filesystem(&nilfs_fs_type);
1504 }
1505
1506 module_init(init_nilfs_fs)
1507 module_exit(exit_nilfs_fs)
1508