• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * 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 <linux/fs_context.h>
39 #include "nilfs.h"
40 #include "export.h"
41 #include "mdt.h"
42 #include "alloc.h"
43 #include "btree.h"
44 #include "btnode.h"
45 #include "page.h"
46 #include "cpfile.h"
47 #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
48 #include "ifile.h"
49 #include "dat.h"
50 #include "segment.h"
51 #include "segbuf.h"
52 
53 MODULE_AUTHOR("NTT Corp.");
54 MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
55 		   "(NILFS)");
56 MODULE_LICENSE("GPL");
57 MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
58 
59 static struct kmem_cache *nilfs_inode_cachep;
60 struct kmem_cache *nilfs_transaction_cachep;
61 struct kmem_cache *nilfs_segbuf_cachep;
62 struct kmem_cache *nilfs_btree_path_cache;
63 
64 static int nilfs_setup_super(struct super_block *sb, int is_mount);
65 static int nilfs_remount(struct super_block *sb, int *flags, char *data);
66 
__nilfs_msg(struct super_block * sb,const char * fmt,...)67 void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
68 {
69 	struct va_format vaf;
70 	va_list args;
71 	int level;
72 
73 	va_start(args, fmt);
74 
75 	level = printk_get_level(fmt);
76 	vaf.fmt = printk_skip_level(fmt);
77 	vaf.va = &args;
78 
79 	if (sb)
80 		printk("%c%cNILFS (%s): %pV\n",
81 		       KERN_SOH_ASCII, level, sb->s_id, &vaf);
82 	else
83 		printk("%c%cNILFS: %pV\n",
84 		       KERN_SOH_ASCII, level, &vaf);
85 
86 	va_end(args);
87 }
88 
nilfs_set_error(struct super_block * sb)89 static void nilfs_set_error(struct super_block *sb)
90 {
91 	struct the_nilfs *nilfs = sb->s_fs_info;
92 	struct nilfs_super_block **sbp;
93 
94 	down_write(&nilfs->ns_sem);
95 	if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
96 		nilfs->ns_mount_state |= NILFS_ERROR_FS;
97 		sbp = nilfs_prepare_super(sb, 0);
98 		if (likely(sbp)) {
99 			sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
100 			if (sbp[1])
101 				sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
102 			nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
103 		}
104 	}
105 	up_write(&nilfs->ns_sem);
106 }
107 
108 /**
109  * __nilfs_error() - report failure condition on a filesystem
110  *
111  * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
112  * reporting an error message.  This function should be called when
113  * NILFS detects incoherences or defects of meta data on disk.
114  *
115  * This implements the body of nilfs_error() macro.  Normally,
116  * nilfs_error() should be used.  As for sustainable errors such as a
117  * single-shot I/O error, nilfs_err() should be used instead.
118  *
119  * Callers should not add a trailing newline since this will do it.
120  */
__nilfs_error(struct super_block * sb,const char * function,const char * fmt,...)121 void __nilfs_error(struct super_block *sb, const char *function,
122 		   const char *fmt, ...)
123 {
124 	struct the_nilfs *nilfs = sb->s_fs_info;
125 	struct va_format vaf;
126 	va_list args;
127 
128 	va_start(args, fmt);
129 
130 	vaf.fmt = fmt;
131 	vaf.va = &args;
132 
133 	printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
134 	       sb->s_id, function, &vaf);
135 
136 	va_end(args);
137 
138 	if (!sb_rdonly(sb)) {
139 		nilfs_set_error(sb);
140 
141 		if (nilfs_test_opt(nilfs, ERRORS_RO)) {
142 			printk(KERN_CRIT "Remounting filesystem read-only\n");
143 			sb->s_flags |= SB_RDONLY;
144 		}
145 	}
146 
147 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
148 		panic("NILFS (device %s): panic forced after error\n",
149 		      sb->s_id);
150 }
151 
nilfs_alloc_inode(struct super_block * sb)152 struct inode *nilfs_alloc_inode(struct super_block *sb)
153 {
154 	struct nilfs_inode_info *ii;
155 
156 	ii = alloc_inode_sb(sb, nilfs_inode_cachep, GFP_NOFS);
157 	if (!ii)
158 		return NULL;
159 	ii->i_bh = NULL;
160 	ii->i_state = 0;
161 	ii->i_cno = 0;
162 	ii->i_assoc_inode = NULL;
163 	ii->i_bmap = &ii->i_bmap_data;
164 	return &ii->vfs_inode;
165 }
166 
nilfs_free_inode(struct inode * inode)167 static void nilfs_free_inode(struct inode *inode)
168 {
169 	if (nilfs_is_metadata_file_inode(inode))
170 		nilfs_mdt_destroy(inode);
171 
172 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
173 }
174 
nilfs_sync_super(struct super_block * sb,int flag)175 static int nilfs_sync_super(struct super_block *sb, int flag)
176 {
177 	struct the_nilfs *nilfs = sb->s_fs_info;
178 	int err;
179 
180  retry:
181 	set_buffer_dirty(nilfs->ns_sbh[0]);
182 	if (nilfs_test_opt(nilfs, BARRIER)) {
183 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
184 					  REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
185 	} else {
186 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
187 	}
188 
189 	if (unlikely(err)) {
190 		nilfs_err(sb, "unable to write superblock: err=%d", 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_crit(sb, "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_warn(sb,
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 = bdev_nr_bytes(sb->s_bdev);
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_err(sb,
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_warn(sb, "failed to count free inodes: err=%d", err);
665 		if (err == -ERANGE) {
666 			/*
667 			 * If nilfs_palloc_count_max_entries() returns
668 			 * -ERANGE error code then we simply treat
669 			 * curent inodes count as maximum possible and
670 			 * zero as free inodes value.
671 			 */
672 			nmaxinodes = atomic64_read(&root->inodes_count);
673 			nfreeinodes = 0;
674 			err = 0;
675 		} else
676 			return err;
677 	}
678 
679 	buf->f_type = NILFS_SUPER_MAGIC;
680 	buf->f_bsize = sb->s_blocksize;
681 	buf->f_blocks = blocks - overhead;
682 	buf->f_bfree = nfreeblocks;
683 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
684 		(buf->f_bfree - nrsvblocks) : 0;
685 	buf->f_files = nmaxinodes;
686 	buf->f_ffree = nfreeinodes;
687 	buf->f_namelen = NILFS_NAME_LEN;
688 	buf->f_fsid = u64_to_fsid(id);
689 
690 	return 0;
691 }
692 
nilfs_show_options(struct seq_file * seq,struct dentry * dentry)693 static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
694 {
695 	struct super_block *sb = dentry->d_sb;
696 	struct the_nilfs *nilfs = sb->s_fs_info;
697 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
698 
699 	if (!nilfs_test_opt(nilfs, BARRIER))
700 		seq_puts(seq, ",nobarrier");
701 	if (root->cno != NILFS_CPTREE_CURRENT_CNO)
702 		seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
703 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
704 		seq_puts(seq, ",errors=panic");
705 	if (nilfs_test_opt(nilfs, ERRORS_CONT))
706 		seq_puts(seq, ",errors=continue");
707 	if (nilfs_test_opt(nilfs, STRICT_ORDER))
708 		seq_puts(seq, ",order=strict");
709 	if (nilfs_test_opt(nilfs, NORECOVERY))
710 		seq_puts(seq, ",norecovery");
711 	if (nilfs_test_opt(nilfs, DISCARD))
712 		seq_puts(seq, ",discard");
713 
714 	return 0;
715 }
716 
717 static const struct super_operations nilfs_sops = {
718 	.alloc_inode    = nilfs_alloc_inode,
719 	.free_inode     = nilfs_free_inode,
720 	.dirty_inode    = nilfs_dirty_inode,
721 	.evict_inode    = nilfs_evict_inode,
722 	.put_super      = nilfs_put_super,
723 	.sync_fs        = nilfs_sync_fs,
724 	.freeze_fs	= nilfs_freeze,
725 	.unfreeze_fs	= nilfs_unfreeze,
726 	.statfs         = nilfs_statfs,
727 	.remount_fs     = nilfs_remount,
728 	.show_options = nilfs_show_options
729 };
730 
731 enum {
732 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
733 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
734 	Opt_discard, Opt_nodiscard, Opt_err,
735 };
736 
737 static match_table_t tokens = {
738 	{Opt_err_cont, "errors=continue"},
739 	{Opt_err_panic, "errors=panic"},
740 	{Opt_err_ro, "errors=remount-ro"},
741 	{Opt_barrier, "barrier"},
742 	{Opt_nobarrier, "nobarrier"},
743 	{Opt_snapshot, "cp=%u"},
744 	{Opt_order, "order=%s"},
745 	{Opt_norecovery, "norecovery"},
746 	{Opt_discard, "discard"},
747 	{Opt_nodiscard, "nodiscard"},
748 	{Opt_err, NULL}
749 };
750 
parse_options(char * options,struct super_block * sb,int is_remount)751 static int parse_options(char *options, struct super_block *sb, int is_remount)
752 {
753 	struct the_nilfs *nilfs = sb->s_fs_info;
754 	char *p;
755 	substring_t args[MAX_OPT_ARGS];
756 
757 	if (!options)
758 		return 1;
759 
760 	while ((p = strsep(&options, ",")) != NULL) {
761 		int token;
762 
763 		if (!*p)
764 			continue;
765 
766 		token = match_token(p, tokens, args);
767 		switch (token) {
768 		case Opt_barrier:
769 			nilfs_set_opt(nilfs, BARRIER);
770 			break;
771 		case Opt_nobarrier:
772 			nilfs_clear_opt(nilfs, BARRIER);
773 			break;
774 		case Opt_order:
775 			if (strcmp(args[0].from, "relaxed") == 0)
776 				/* Ordered data semantics */
777 				nilfs_clear_opt(nilfs, STRICT_ORDER);
778 			else if (strcmp(args[0].from, "strict") == 0)
779 				/* Strict in-order semantics */
780 				nilfs_set_opt(nilfs, STRICT_ORDER);
781 			else
782 				return 0;
783 			break;
784 		case Opt_err_panic:
785 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
786 			break;
787 		case Opt_err_ro:
788 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
789 			break;
790 		case Opt_err_cont:
791 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
792 			break;
793 		case Opt_snapshot:
794 			if (is_remount) {
795 				nilfs_err(sb,
796 					  "\"%s\" option is invalid for remount",
797 					  p);
798 				return 0;
799 			}
800 			break;
801 		case Opt_norecovery:
802 			nilfs_set_opt(nilfs, NORECOVERY);
803 			break;
804 		case Opt_discard:
805 			nilfs_set_opt(nilfs, DISCARD);
806 			break;
807 		case Opt_nodiscard:
808 			nilfs_clear_opt(nilfs, DISCARD);
809 			break;
810 		default:
811 			nilfs_err(sb, "unrecognized mount option \"%s\"", p);
812 			return 0;
813 		}
814 	}
815 	return 1;
816 }
817 
818 static inline void
nilfs_set_default_options(struct super_block * sb,struct nilfs_super_block * sbp)819 nilfs_set_default_options(struct super_block *sb,
820 			  struct nilfs_super_block *sbp)
821 {
822 	struct the_nilfs *nilfs = sb->s_fs_info;
823 
824 	nilfs->ns_mount_opt =
825 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
826 }
827 
nilfs_setup_super(struct super_block * sb,int is_mount)828 static int nilfs_setup_super(struct super_block *sb, int is_mount)
829 {
830 	struct the_nilfs *nilfs = sb->s_fs_info;
831 	struct nilfs_super_block **sbp;
832 	int max_mnt_count;
833 	int mnt_count;
834 
835 	/* nilfs->ns_sem must be locked by the caller. */
836 	sbp = nilfs_prepare_super(sb, 0);
837 	if (!sbp)
838 		return -EIO;
839 
840 	if (!is_mount)
841 		goto skip_mount_setup;
842 
843 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
844 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
845 
846 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
847 		nilfs_warn(sb, "mounting fs with errors");
848 #if 0
849 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
850 		nilfs_warn(sb, "maximal mount count reached");
851 #endif
852 	}
853 	if (!max_mnt_count)
854 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
855 
856 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
857 	sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
858 
859 skip_mount_setup:
860 	sbp[0]->s_state =
861 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
862 	/* synchronize sbp[1] with sbp[0] */
863 	if (sbp[1])
864 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
865 	return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
866 }
867 
nilfs_read_super_block(struct super_block * sb,u64 pos,int blocksize,struct buffer_head ** pbh)868 struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
869 						 u64 pos, int blocksize,
870 						 struct buffer_head **pbh)
871 {
872 	unsigned long long sb_index = pos;
873 	unsigned long offset;
874 
875 	offset = do_div(sb_index, blocksize);
876 	*pbh = sb_bread(sb, sb_index);
877 	if (!*pbh)
878 		return NULL;
879 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
880 }
881 
nilfs_store_magic_and_option(struct super_block * sb,struct nilfs_super_block * sbp,char * data)882 int nilfs_store_magic_and_option(struct super_block *sb,
883 				 struct nilfs_super_block *sbp,
884 				 char *data)
885 {
886 	struct the_nilfs *nilfs = sb->s_fs_info;
887 
888 	sb->s_magic = le16_to_cpu(sbp->s_magic);
889 
890 	/* FS independent flags */
891 #ifdef NILFS_ATIME_DISABLE
892 	sb->s_flags |= SB_NOATIME;
893 #endif
894 
895 	nilfs_set_default_options(sb, sbp);
896 
897 	nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
898 	nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
899 	nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
900 	nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
901 
902 	return !parse_options(data, sb, 0) ? -EINVAL : 0;
903 }
904 
nilfs_check_feature_compatibility(struct super_block * sb,struct nilfs_super_block * sbp)905 int nilfs_check_feature_compatibility(struct super_block *sb,
906 				      struct nilfs_super_block *sbp)
907 {
908 	__u64 features;
909 
910 	features = le64_to_cpu(sbp->s_feature_incompat) &
911 		~NILFS_FEATURE_INCOMPAT_SUPP;
912 	if (features) {
913 		nilfs_err(sb,
914 			  "couldn't mount because of unsupported optional features (%llx)",
915 			  (unsigned long long)features);
916 		return -EINVAL;
917 	}
918 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
919 		~NILFS_FEATURE_COMPAT_RO_SUPP;
920 	if (!sb_rdonly(sb) && features) {
921 		nilfs_err(sb,
922 			  "couldn't mount RDWR because of unsupported optional features (%llx)",
923 			  (unsigned long long)features);
924 		return -EINVAL;
925 	}
926 	return 0;
927 }
928 
nilfs_get_root_dentry(struct super_block * sb,struct nilfs_root * root,struct dentry ** root_dentry)929 static int nilfs_get_root_dentry(struct super_block *sb,
930 				 struct nilfs_root *root,
931 				 struct dentry **root_dentry)
932 {
933 	struct inode *inode;
934 	struct dentry *dentry;
935 	int ret = 0;
936 
937 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
938 	if (IS_ERR(inode)) {
939 		ret = PTR_ERR(inode);
940 		nilfs_err(sb, "error %d getting root inode", ret);
941 		goto out;
942 	}
943 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
944 		iput(inode);
945 		nilfs_err(sb, "corrupt root inode");
946 		ret = -EINVAL;
947 		goto out;
948 	}
949 
950 	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
951 		dentry = d_find_alias(inode);
952 		if (!dentry) {
953 			dentry = d_make_root(inode);
954 			if (!dentry) {
955 				ret = -ENOMEM;
956 				goto failed_dentry;
957 			}
958 		} else {
959 			iput(inode);
960 		}
961 	} else {
962 		dentry = d_obtain_root(inode);
963 		if (IS_ERR(dentry)) {
964 			ret = PTR_ERR(dentry);
965 			goto failed_dentry;
966 		}
967 	}
968 	*root_dentry = dentry;
969  out:
970 	return ret;
971 
972  failed_dentry:
973 	nilfs_err(sb, "error %d getting root dentry", ret);
974 	goto out;
975 }
976 
nilfs_attach_snapshot(struct super_block * s,__u64 cno,struct dentry ** root_dentry)977 static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
978 				 struct dentry **root_dentry)
979 {
980 	struct the_nilfs *nilfs = s->s_fs_info;
981 	struct nilfs_root *root;
982 	int ret;
983 
984 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
985 
986 	down_read(&nilfs->ns_segctor_sem);
987 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
988 	up_read(&nilfs->ns_segctor_sem);
989 	if (ret < 0) {
990 		ret = (ret == -ENOENT) ? -EINVAL : ret;
991 		goto out;
992 	} else if (!ret) {
993 		nilfs_err(s,
994 			  "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
995 			  (unsigned long long)cno);
996 		ret = -EINVAL;
997 		goto out;
998 	}
999 
1000 	ret = nilfs_attach_checkpoint(s, cno, false, &root);
1001 	if (ret) {
1002 		nilfs_err(s,
1003 			  "error %d while loading snapshot (checkpoint number=%llu)",
1004 			  ret, (unsigned long long)cno);
1005 		goto out;
1006 	}
1007 	ret = nilfs_get_root_dentry(s, root, root_dentry);
1008 	nilfs_put_root(root);
1009  out:
1010 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
1011 	return ret;
1012 }
1013 
1014 /**
1015  * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
1016  * @root_dentry: root dentry of the tree to be shrunk
1017  *
1018  * This function returns true if the tree was in-use.
1019  */
nilfs_tree_is_busy(struct dentry * root_dentry)1020 static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1021 {
1022 	shrink_dcache_parent(root_dentry);
1023 	return d_count(root_dentry) > 1;
1024 }
1025 
nilfs_checkpoint_is_mounted(struct super_block * sb,__u64 cno)1026 int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1027 {
1028 	struct the_nilfs *nilfs = sb->s_fs_info;
1029 	struct nilfs_root *root;
1030 	struct inode *inode;
1031 	struct dentry *dentry;
1032 	int ret;
1033 
1034 	if (cno > nilfs->ns_cno)
1035 		return false;
1036 
1037 	if (cno >= nilfs_last_cno(nilfs))
1038 		return true;	/* protect recent checkpoints */
1039 
1040 	ret = false;
1041 	root = nilfs_lookup_root(nilfs, cno);
1042 	if (root) {
1043 		inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1044 		if (inode) {
1045 			dentry = d_find_alias(inode);
1046 			if (dentry) {
1047 				ret = nilfs_tree_is_busy(dentry);
1048 				dput(dentry);
1049 			}
1050 			iput(inode);
1051 		}
1052 		nilfs_put_root(root);
1053 	}
1054 	return ret;
1055 }
1056 
1057 /**
1058  * nilfs_fill_super() - initialize a super block instance
1059  * @sb: super_block
1060  * @data: mount options
1061  * @silent: silent mode flag
1062  *
1063  * This function is called exclusively by nilfs->ns_mount_mutex.
1064  * So, the recovery process is protected from other simultaneous mounts.
1065  */
1066 static int
nilfs_fill_super(struct super_block * sb,void * data,int silent)1067 nilfs_fill_super(struct super_block *sb, void *data, int silent)
1068 {
1069 	struct the_nilfs *nilfs;
1070 	struct nilfs_root *fsroot;
1071 	__u64 cno;
1072 	int err;
1073 
1074 	nilfs = alloc_nilfs(sb);
1075 	if (!nilfs)
1076 		return -ENOMEM;
1077 
1078 	sb->s_fs_info = nilfs;
1079 
1080 	err = init_nilfs(nilfs, sb, (char *)data);
1081 	if (err)
1082 		goto failed_nilfs;
1083 
1084 	sb->s_op = &nilfs_sops;
1085 	sb->s_export_op = &nilfs_export_ops;
1086 	sb->s_root = NULL;
1087 	sb->s_time_gran = 1;
1088 	sb->s_max_links = NILFS_LINK_MAX;
1089 
1090 	sb->s_bdi = bdi_get(sb->s_bdev->bd_disk->bdi);
1091 
1092 	err = load_nilfs(nilfs, sb);
1093 	if (err)
1094 		goto failed_nilfs;
1095 
1096 	cno = nilfs_last_cno(nilfs);
1097 	err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1098 	if (err) {
1099 		nilfs_err(sb,
1100 			  "error %d while loading last checkpoint (checkpoint number=%llu)",
1101 			  err, (unsigned long long)cno);
1102 		goto failed_unload;
1103 	}
1104 
1105 	if (!sb_rdonly(sb)) {
1106 		err = nilfs_attach_log_writer(sb, fsroot);
1107 		if (err)
1108 			goto failed_checkpoint;
1109 	}
1110 
1111 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1112 	if (err)
1113 		goto failed_segctor;
1114 
1115 	nilfs_put_root(fsroot);
1116 
1117 	if (!sb_rdonly(sb)) {
1118 		down_write(&nilfs->ns_sem);
1119 		nilfs_setup_super(sb, true);
1120 		up_write(&nilfs->ns_sem);
1121 	}
1122 
1123 	return 0;
1124 
1125  failed_segctor:
1126 	nilfs_detach_log_writer(sb);
1127 
1128  failed_checkpoint:
1129 	nilfs_put_root(fsroot);
1130 
1131  failed_unload:
1132 	nilfs_sysfs_delete_device_group(nilfs);
1133 	iput(nilfs->ns_sufile);
1134 	iput(nilfs->ns_cpfile);
1135 	iput(nilfs->ns_dat);
1136 
1137  failed_nilfs:
1138 	destroy_nilfs(nilfs);
1139 	return err;
1140 }
1141 
nilfs_remount(struct super_block * sb,int * flags,char * data)1142 static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1143 {
1144 	struct the_nilfs *nilfs = sb->s_fs_info;
1145 	unsigned long old_sb_flags;
1146 	unsigned long old_mount_opt;
1147 	int err;
1148 
1149 	sync_filesystem(sb);
1150 	old_sb_flags = sb->s_flags;
1151 	old_mount_opt = nilfs->ns_mount_opt;
1152 
1153 	if (!parse_options(data, sb, 1)) {
1154 		err = -EINVAL;
1155 		goto restore_opts;
1156 	}
1157 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
1158 
1159 	err = -EINVAL;
1160 
1161 	if (!nilfs_valid_fs(nilfs)) {
1162 		nilfs_warn(sb,
1163 			   "couldn't remount because the filesystem is in an incomplete recovery state");
1164 		goto restore_opts;
1165 	}
1166 
1167 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1168 		goto out;
1169 	if (*flags & SB_RDONLY) {
1170 		sb->s_flags |= SB_RDONLY;
1171 
1172 		/*
1173 		 * Remounting a valid RW partition RDONLY, so set
1174 		 * the RDONLY flag and then mark the partition as valid again.
1175 		 */
1176 		down_write(&nilfs->ns_sem);
1177 		nilfs_cleanup_super(sb);
1178 		up_write(&nilfs->ns_sem);
1179 	} else {
1180 		__u64 features;
1181 		struct nilfs_root *root;
1182 
1183 		/*
1184 		 * Mounting a RDONLY partition read-write, so reread and
1185 		 * store the current valid flag.  (It may have been changed
1186 		 * by fsck since we originally mounted the partition.)
1187 		 */
1188 		down_read(&nilfs->ns_sem);
1189 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1190 			~NILFS_FEATURE_COMPAT_RO_SUPP;
1191 		up_read(&nilfs->ns_sem);
1192 		if (features) {
1193 			nilfs_warn(sb,
1194 				   "couldn't remount RDWR because of unsupported optional features (%llx)",
1195 				   (unsigned long long)features);
1196 			err = -EROFS;
1197 			goto restore_opts;
1198 		}
1199 
1200 		sb->s_flags &= ~SB_RDONLY;
1201 
1202 		root = NILFS_I(d_inode(sb->s_root))->i_root;
1203 		err = nilfs_attach_log_writer(sb, root);
1204 		if (err)
1205 			goto restore_opts;
1206 
1207 		down_write(&nilfs->ns_sem);
1208 		nilfs_setup_super(sb, true);
1209 		up_write(&nilfs->ns_sem);
1210 	}
1211  out:
1212 	return 0;
1213 
1214  restore_opts:
1215 	sb->s_flags = old_sb_flags;
1216 	nilfs->ns_mount_opt = old_mount_opt;
1217 	return err;
1218 }
1219 
1220 struct nilfs_super_data {
1221 	__u64 cno;
1222 	int flags;
1223 };
1224 
nilfs_parse_snapshot_option(const char * option,const substring_t * arg,struct nilfs_super_data * sd)1225 static int nilfs_parse_snapshot_option(const char *option,
1226 				       const substring_t *arg,
1227 				       struct nilfs_super_data *sd)
1228 {
1229 	unsigned long long val;
1230 	const char *msg = NULL;
1231 	int err;
1232 
1233 	if (!(sd->flags & SB_RDONLY)) {
1234 		msg = "read-only option is not specified";
1235 		goto parse_error;
1236 	}
1237 
1238 	err = kstrtoull(arg->from, 0, &val);
1239 	if (err) {
1240 		if (err == -ERANGE)
1241 			msg = "too large checkpoint number";
1242 		else
1243 			msg = "malformed argument";
1244 		goto parse_error;
1245 	} else if (val == 0) {
1246 		msg = "invalid checkpoint number 0";
1247 		goto parse_error;
1248 	}
1249 	sd->cno = val;
1250 	return 0;
1251 
1252 parse_error:
1253 	nilfs_err(NULL, "invalid option \"%s\": %s", option, msg);
1254 	return 1;
1255 }
1256 
1257 /**
1258  * nilfs_identify - pre-read mount options needed to identify mount instance
1259  * @data: mount options
1260  * @sd: nilfs_super_data
1261  */
nilfs_identify(char * data,struct nilfs_super_data * sd)1262 static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1263 {
1264 	char *p, *options = data;
1265 	substring_t args[MAX_OPT_ARGS];
1266 	int token;
1267 	int ret = 0;
1268 
1269 	do {
1270 		p = strsep(&options, ",");
1271 		if (p != NULL && *p) {
1272 			token = match_token(p, tokens, args);
1273 			if (token == Opt_snapshot)
1274 				ret = nilfs_parse_snapshot_option(p, &args[0],
1275 								  sd);
1276 		}
1277 		if (!options)
1278 			break;
1279 		BUG_ON(options == data);
1280 		*(options - 1) = ',';
1281 	} while (!ret);
1282 	return ret;
1283 }
1284 
nilfs_set_bdev_super(struct super_block * s,void * data)1285 static int nilfs_set_bdev_super(struct super_block *s, void *data)
1286 {
1287 	s->s_dev = *(dev_t *)data;
1288 	return 0;
1289 }
1290 
nilfs_test_bdev_super(struct super_block * s,void * data)1291 static int nilfs_test_bdev_super(struct super_block *s, void *data)
1292 {
1293 	return !(s->s_iflags & SB_I_RETIRED) && s->s_dev == *(dev_t *)data;
1294 }
1295 
1296 static struct dentry *
nilfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)1297 nilfs_mount(struct file_system_type *fs_type, int flags,
1298 	     const char *dev_name, void *data)
1299 {
1300 	struct nilfs_super_data sd = { .flags = flags };
1301 	struct super_block *s;
1302 	dev_t dev;
1303 	int err;
1304 
1305 	if (nilfs_identify(data, &sd))
1306 		return ERR_PTR(-EINVAL);
1307 
1308 	err = lookup_bdev(dev_name, &dev);
1309 	if (err)
1310 		return ERR_PTR(err);
1311 
1312 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1313 		 &dev);
1314 	if (IS_ERR(s))
1315 		return ERR_CAST(s);
1316 
1317 	if (!s->s_root) {
1318 		/*
1319 		 * We drop s_umount here because we need to open the bdev and
1320 		 * bdev->open_mutex ranks above s_umount (blkdev_put() ->
1321 		 * __invalidate_device()). It is safe because we have active sb
1322 		 * reference and SB_BORN is not set yet.
1323 		 */
1324 		up_write(&s->s_umount);
1325 		err = setup_bdev_super(s, flags, NULL);
1326 		down_write(&s->s_umount);
1327 		if (!err)
1328 			err = nilfs_fill_super(s, data,
1329 					       flags & SB_SILENT ? 1 : 0);
1330 		if (err)
1331 			goto failed_super;
1332 
1333 		s->s_flags |= SB_ACTIVE;
1334 	} else if (!sd.cno) {
1335 		if (nilfs_tree_is_busy(s->s_root)) {
1336 			if ((flags ^ s->s_flags) & SB_RDONLY) {
1337 				nilfs_err(s,
1338 					  "the device already has a %s mount.",
1339 					  sb_rdonly(s) ? "read-only" : "read/write");
1340 				err = -EBUSY;
1341 				goto failed_super;
1342 			}
1343 		} else {
1344 			/*
1345 			 * Try remount to setup mount states if the current
1346 			 * tree is not mounted and only snapshots use this sb.
1347 			 */
1348 			err = nilfs_remount(s, &flags, data);
1349 			if (err)
1350 				goto failed_super;
1351 		}
1352 	}
1353 
1354 	if (sd.cno) {
1355 		struct dentry *root_dentry;
1356 
1357 		err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1358 		if (err)
1359 			goto failed_super;
1360 		return root_dentry;
1361 	}
1362 
1363 	return dget(s->s_root);
1364 
1365  failed_super:
1366 	deactivate_locked_super(s);
1367 	return ERR_PTR(err);
1368 }
1369 
1370 struct file_system_type nilfs_fs_type = {
1371 	.owner    = THIS_MODULE,
1372 	.name     = "nilfs2",
1373 	.mount    = nilfs_mount,
1374 	.kill_sb  = kill_block_super,
1375 	.fs_flags = FS_REQUIRES_DEV,
1376 };
1377 MODULE_ALIAS_FS("nilfs2");
1378 
nilfs_inode_init_once(void * obj)1379 static void nilfs_inode_init_once(void *obj)
1380 {
1381 	struct nilfs_inode_info *ii = obj;
1382 
1383 	INIT_LIST_HEAD(&ii->i_dirty);
1384 #ifdef CONFIG_NILFS_XATTR
1385 	init_rwsem(&ii->xattr_sem);
1386 #endif
1387 	inode_init_once(&ii->vfs_inode);
1388 }
1389 
nilfs_segbuf_init_once(void * obj)1390 static void nilfs_segbuf_init_once(void *obj)
1391 {
1392 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1393 }
1394 
nilfs_destroy_cachep(void)1395 static void nilfs_destroy_cachep(void)
1396 {
1397 	/*
1398 	 * Make sure all delayed rcu free inodes are flushed before we
1399 	 * destroy cache.
1400 	 */
1401 	rcu_barrier();
1402 
1403 	kmem_cache_destroy(nilfs_inode_cachep);
1404 	kmem_cache_destroy(nilfs_transaction_cachep);
1405 	kmem_cache_destroy(nilfs_segbuf_cachep);
1406 	kmem_cache_destroy(nilfs_btree_path_cache);
1407 }
1408 
nilfs_init_cachep(void)1409 static int __init nilfs_init_cachep(void)
1410 {
1411 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1412 			sizeof(struct nilfs_inode_info), 0,
1413 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1414 			nilfs_inode_init_once);
1415 	if (!nilfs_inode_cachep)
1416 		goto fail;
1417 
1418 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1419 			sizeof(struct nilfs_transaction_info), 0,
1420 			SLAB_RECLAIM_ACCOUNT, NULL);
1421 	if (!nilfs_transaction_cachep)
1422 		goto fail;
1423 
1424 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1425 			sizeof(struct nilfs_segment_buffer), 0,
1426 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1427 	if (!nilfs_segbuf_cachep)
1428 		goto fail;
1429 
1430 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1431 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1432 			0, 0, NULL);
1433 	if (!nilfs_btree_path_cache)
1434 		goto fail;
1435 
1436 	return 0;
1437 
1438 fail:
1439 	nilfs_destroy_cachep();
1440 	return -ENOMEM;
1441 }
1442 
init_nilfs_fs(void)1443 static int __init init_nilfs_fs(void)
1444 {
1445 	int err;
1446 
1447 	err = nilfs_init_cachep();
1448 	if (err)
1449 		goto fail;
1450 
1451 	err = nilfs_sysfs_init();
1452 	if (err)
1453 		goto free_cachep;
1454 
1455 	err = register_filesystem(&nilfs_fs_type);
1456 	if (err)
1457 		goto deinit_sysfs_entry;
1458 
1459 	printk(KERN_INFO "NILFS version 2 loaded\n");
1460 	return 0;
1461 
1462 deinit_sysfs_entry:
1463 	nilfs_sysfs_exit();
1464 free_cachep:
1465 	nilfs_destroy_cachep();
1466 fail:
1467 	return err;
1468 }
1469 
exit_nilfs_fs(void)1470 static void __exit exit_nilfs_fs(void)
1471 {
1472 	nilfs_destroy_cachep();
1473 	nilfs_sysfs_exit();
1474 	unregister_filesystem(&nilfs_fs_type);
1475 }
1476 
1477 module_init(init_nilfs_fs)
1478 module_exit(exit_nilfs_fs)
1479