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