xref: /openbmc/linux/fs/nilfs2/super.c (revision 2987a4cfc8332b4b516f3e03d29ec0316221e45f)
1ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
2783f6184SRyusuke Konishi /*
3783f6184SRyusuke Konishi  * super.c - NILFS module and super block management.
4783f6184SRyusuke Konishi  *
5783f6184SRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6783f6184SRyusuke Konishi  *
74b420ab4SRyusuke Konishi  * Written by Ryusuke Konishi.
8783f6184SRyusuke Konishi  */
9783f6184SRyusuke Konishi /*
10783f6184SRyusuke Konishi  *  linux/fs/ext2/super.c
11783f6184SRyusuke Konishi  *
12783f6184SRyusuke Konishi  * Copyright (C) 1992, 1993, 1994, 1995
13783f6184SRyusuke Konishi  * Remy Card (card@masi.ibp.fr)
14783f6184SRyusuke Konishi  * Laboratoire MASI - Institut Blaise Pascal
15783f6184SRyusuke Konishi  * Universite Pierre et Marie Curie (Paris VI)
16783f6184SRyusuke Konishi  *
17783f6184SRyusuke Konishi  *  from
18783f6184SRyusuke Konishi  *
19783f6184SRyusuke Konishi  *  linux/fs/minix/inode.c
20783f6184SRyusuke Konishi  *
21783f6184SRyusuke Konishi  *  Copyright (C) 1991, 1992  Linus Torvalds
22783f6184SRyusuke Konishi  *
23783f6184SRyusuke Konishi  *  Big-endian to little-endian byte-swapping/bitmaps by
24783f6184SRyusuke Konishi  *        David S. Miller (davem@caip.rutgers.edu), 1995
25783f6184SRyusuke Konishi  */
26783f6184SRyusuke Konishi 
27783f6184SRyusuke Konishi #include <linux/module.h>
28783f6184SRyusuke Konishi #include <linux/string.h>
29783f6184SRyusuke Konishi #include <linux/slab.h>
30783f6184SRyusuke Konishi #include <linux/init.h>
31783f6184SRyusuke Konishi #include <linux/blkdev.h>
32783f6184SRyusuke Konishi #include <linux/parser.h>
33783f6184SRyusuke Konishi #include <linux/crc32.h>
34783f6184SRyusuke Konishi #include <linux/vfs.h>
35783f6184SRyusuke Konishi #include <linux/writeback.h>
36b58a285bSJiro SEKIBA #include <linux/seq_file.h>
37b58a285bSJiro SEKIBA #include <linux/mount.h>
38783f6184SRyusuke Konishi #include "nilfs.h"
398e656fd5SRyusuke Konishi #include "export.h"
40783f6184SRyusuke Konishi #include "mdt.h"
41783f6184SRyusuke Konishi #include "alloc.h"
4205d0e94bSRyusuke Konishi #include "btree.h"
4305d0e94bSRyusuke Konishi #include "btnode.h"
44783f6184SRyusuke Konishi #include "page.h"
45783f6184SRyusuke Konishi #include "cpfile.h"
464e33f9eaSRyusuke Konishi #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
47783f6184SRyusuke Konishi #include "ifile.h"
48783f6184SRyusuke Konishi #include "dat.h"
49783f6184SRyusuke Konishi #include "segment.h"
50783f6184SRyusuke Konishi #include "segbuf.h"
51783f6184SRyusuke Konishi 
52783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
53783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
54783f6184SRyusuke Konishi 		   "(NILFS)");
55783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
56783f6184SRyusuke Konishi 
57abc0b50bSJiro SEKIBA static struct kmem_cache *nilfs_inode_cachep;
5841c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
5941c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
6041c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
6141c88bd7SLi Hong 
62f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount);
63783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
64783f6184SRyusuke Konishi 
65*2987a4cfSJoe Perches void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
66a66dfb0aSRyusuke Konishi {
67a66dfb0aSRyusuke Konishi 	struct va_format vaf;
68a66dfb0aSRyusuke Konishi 	va_list args;
69*2987a4cfSJoe Perches 	int level;
70a66dfb0aSRyusuke Konishi 
71a66dfb0aSRyusuke Konishi 	va_start(args, fmt);
72*2987a4cfSJoe Perches 
73*2987a4cfSJoe Perches 	level = printk_get_level(fmt);
74*2987a4cfSJoe Perches 	vaf.fmt = printk_skip_level(fmt);
75a66dfb0aSRyusuke Konishi 	vaf.va = &args;
76*2987a4cfSJoe Perches 
77a66dfb0aSRyusuke Konishi 	if (sb)
78*2987a4cfSJoe Perches 		printk("%c%cNILFS (%s): %pV\n",
79*2987a4cfSJoe Perches 		       KERN_SOH_ASCII, level, sb->s_id, &vaf);
80a66dfb0aSRyusuke Konishi 	else
81*2987a4cfSJoe Perches 		printk("%c%cNILFS: %pV\n",
82*2987a4cfSJoe Perches 		       KERN_SOH_ASCII, level, &vaf);
83*2987a4cfSJoe Perches 
84a66dfb0aSRyusuke Konishi 	va_end(args);
85a66dfb0aSRyusuke Konishi }
86a66dfb0aSRyusuke Konishi 
87f7545144SRyusuke Konishi static void nilfs_set_error(struct super_block *sb)
88c8a11c8aSRyusuke Konishi {
89e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
90d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
91c8a11c8aSRyusuke Konishi 
92c8a11c8aSRyusuke Konishi 	down_write(&nilfs->ns_sem);
93c8a11c8aSRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
94c8a11c8aSRyusuke Konishi 		nilfs->ns_mount_state |= NILFS_ERROR_FS;
95f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, 0);
96d26493b6SJiro SEKIBA 		if (likely(sbp)) {
97d26493b6SJiro SEKIBA 			sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
98b2ac86e1SJiro SEKIBA 			if (sbp[1])
99b2ac86e1SJiro SEKIBA 				sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
100f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
101c8a11c8aSRyusuke Konishi 		}
102d26493b6SJiro SEKIBA 	}
103c8a11c8aSRyusuke Konishi 	up_write(&nilfs->ns_sem);
104c8a11c8aSRyusuke Konishi }
105c8a11c8aSRyusuke Konishi 
106783f6184SRyusuke Konishi /**
107cae3d4caSRyusuke Konishi  * __nilfs_error() - report failure condition on a filesystem
108783f6184SRyusuke Konishi  *
109cae3d4caSRyusuke Konishi  * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
110cae3d4caSRyusuke Konishi  * reporting an error message.  This function should be called when
111cae3d4caSRyusuke Konishi  * NILFS detects incoherences or defects of meta data on disk.
112783f6184SRyusuke Konishi  *
113cae3d4caSRyusuke Konishi  * This implements the body of nilfs_error() macro.  Normally,
114cae3d4caSRyusuke Konishi  * nilfs_error() should be used.  As for sustainable errors such as a
115d6517debSRyusuke Konishi  * single-shot I/O error, nilfs_msg() should be used instead.
116cae3d4caSRyusuke Konishi  *
117cae3d4caSRyusuke Konishi  * Callers should not add a trailing newline since this will do it.
118783f6184SRyusuke Konishi  */
119cae3d4caSRyusuke Konishi void __nilfs_error(struct super_block *sb, const char *function,
120783f6184SRyusuke Konishi 		   const char *fmt, ...)
121783f6184SRyusuke Konishi {
122e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
123b004a5ebSJoe Perches 	struct va_format vaf;
124783f6184SRyusuke Konishi 	va_list args;
125783f6184SRyusuke Konishi 
126783f6184SRyusuke Konishi 	va_start(args, fmt);
127b004a5ebSJoe Perches 
128b004a5ebSJoe Perches 	vaf.fmt = fmt;
129b004a5ebSJoe Perches 	vaf.va = &args;
130b004a5ebSJoe Perches 
131b004a5ebSJoe Perches 	printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
132b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
133b004a5ebSJoe Perches 
134783f6184SRyusuke Konishi 	va_end(args);
135783f6184SRyusuke Konishi 
136bc98a42cSDavid Howells 	if (!sb_rdonly(sb)) {
137f7545144SRyusuke Konishi 		nilfs_set_error(sb);
138783f6184SRyusuke Konishi 
1393b2ce58bSRyusuke Konishi 		if (nilfs_test_opt(nilfs, ERRORS_RO)) {
140783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
1411751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
142783f6184SRyusuke Konishi 		}
143783f6184SRyusuke Konishi 	}
144783f6184SRyusuke Konishi 
1453b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
146783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
147783f6184SRyusuke Konishi 		      sb->s_id);
148783f6184SRyusuke Konishi }
149783f6184SRyusuke Konishi 
1502879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
151783f6184SRyusuke Konishi {
152783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
153783f6184SRyusuke Konishi 
154783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
155783f6184SRyusuke Konishi 	if (!ii)
156783f6184SRyusuke Konishi 		return NULL;
157783f6184SRyusuke Konishi 	ii->i_bh = NULL;
158783f6184SRyusuke Konishi 	ii->i_state = 0;
1590e14a359SRyusuke Konishi 	ii->i_cno = 0;
160b83ae6d4SChristoph Hellwig 	nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
161783f6184SRyusuke Konishi 	return &ii->vfs_inode;
162783f6184SRyusuke Konishi }
163783f6184SRyusuke Konishi 
164977c3d18SAl Viro static void nilfs_free_inode(struct inode *inode)
165783f6184SRyusuke Konishi {
1662d19961dSRyusuke Konishi 	if (nilfs_is_metadata_file_inode(inode))
1672d19961dSRyusuke Konishi 		nilfs_mdt_destroy(inode);
1682d19961dSRyusuke Konishi 
169783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
170783f6184SRyusuke Konishi }
171783f6184SRyusuke Konishi 
172f7545144SRyusuke Konishi static int nilfs_sync_super(struct super_block *sb, int flag)
173783f6184SRyusuke Konishi {
174e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
175783f6184SRyusuke Konishi 	int err;
176783f6184SRyusuke Konishi 
177783f6184SRyusuke Konishi  retry:
178e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
1793b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, BARRIER)) {
18087e99511SChristoph Hellwig 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
18170fd7614SChristoph Hellwig 					  REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
18287e99511SChristoph Hellwig 	} else {
18387e99511SChristoph Hellwig 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
18487e99511SChristoph Hellwig 	}
18587e99511SChristoph Hellwig 
186e339ad31SRyusuke Konishi 	if (unlikely(err)) {
187feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
188feee880fSRyusuke Konishi 			  err);
189e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
190b2ac86e1SJiro SEKIBA 			/*
191b2ac86e1SJiro SEKIBA 			 * sbp[0] points to newer log than sbp[1],
192b2ac86e1SJiro SEKIBA 			 * so copy sbp[0] to sbp[1] to take over sbp[0].
193b2ac86e1SJiro SEKIBA 			 */
194b2ac86e1SJiro SEKIBA 			memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
195b2ac86e1SJiro SEKIBA 			       nilfs->ns_sbsize);
196e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
197e339ad31SRyusuke Konishi 			goto retry;
198e339ad31SRyusuke Konishi 		}
199e339ad31SRyusuke Konishi 	} else {
200e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
201e339ad31SRyusuke Konishi 
202b2ac86e1SJiro SEKIBA 		nilfs->ns_sbwcount++;
203b2ac86e1SJiro SEKIBA 
204e339ad31SRyusuke Konishi 		/*
205e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
206e339ad31SRyusuke Konishi 		 * written in superblock.
207e339ad31SRyusuke Konishi 		 */
208783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
209e339ad31SRyusuke Konishi 
210e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
211e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
212b2ac86e1SJiro SEKIBA 			if (flag == NILFS_SB_COMMIT_ALL) {
213e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
214b2ac86e1SJiro SEKIBA 				if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
215b2ac86e1SJiro SEKIBA 					goto out;
216b2ac86e1SJiro SEKIBA 			}
217b2ac86e1SJiro SEKIBA 			if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
218b2ac86e1SJiro SEKIBA 			    le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
219e339ad31SRyusuke Konishi 				sbp = nilfs->ns_sbp[1];
220e339ad31SRyusuke Konishi 		}
221b2ac86e1SJiro SEKIBA 
2222c2e52fcSRyusuke Konishi 		spin_lock(&nilfs->ns_last_segment_lock);
223e339ad31SRyusuke Konishi 		nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2242c2e52fcSRyusuke Konishi 		spin_unlock(&nilfs->ns_last_segment_lock);
225783f6184SRyusuke Konishi 	}
226b2ac86e1SJiro SEKIBA  out:
227783f6184SRyusuke Konishi 	return err;
228783f6184SRyusuke Konishi }
229783f6184SRyusuke Konishi 
23060f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
23160f46b7eSRyusuke Konishi 			  struct the_nilfs *nilfs)
23260f46b7eSRyusuke Konishi {
23360f46b7eSRyusuke Konishi 	sector_t nfreeblocks;
23460f46b7eSRyusuke Konishi 
23560f46b7eSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
23660f46b7eSRyusuke Konishi 	nilfs_count_free_blocks(nilfs, &nfreeblocks);
23760f46b7eSRyusuke Konishi 	sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
23860f46b7eSRyusuke Konishi 
23960f46b7eSRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
24060f46b7eSRyusuke Konishi 	sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
24160f46b7eSRyusuke Konishi 	sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
24260f46b7eSRyusuke Konishi 	sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
24360f46b7eSRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
24460f46b7eSRyusuke Konishi }
24560f46b7eSRyusuke Konishi 
246f7545144SRyusuke Konishi struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
247b2ac86e1SJiro SEKIBA 					       int flip)
248d26493b6SJiro SEKIBA {
249e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
250d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
251d26493b6SJiro SEKIBA 
252d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
253d26493b6SJiro SEKIBA 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
254d26493b6SJiro SEKIBA 		if (sbp[1] &&
255d26493b6SJiro SEKIBA 		    sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
256b2ac86e1SJiro SEKIBA 			memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
257d26493b6SJiro SEKIBA 		} else {
258feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_CRIT, "superblock broke");
259d26493b6SJiro SEKIBA 			return NULL;
260d26493b6SJiro SEKIBA 		}
261b2ac86e1SJiro SEKIBA 	} else if (sbp[1] &&
262b2ac86e1SJiro SEKIBA 		   sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
263b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
264d26493b6SJiro SEKIBA 	}
265b2ac86e1SJiro SEKIBA 
266b2ac86e1SJiro SEKIBA 	if (flip && sbp[1])
267b2ac86e1SJiro SEKIBA 		nilfs_swap_super_block(nilfs);
268b2ac86e1SJiro SEKIBA 
269d26493b6SJiro SEKIBA 	return sbp;
270d26493b6SJiro SEKIBA }
271d26493b6SJiro SEKIBA 
272f7545144SRyusuke Konishi int nilfs_commit_super(struct super_block *sb, int flag)
273783f6184SRyusuke Konishi {
274e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
275e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
276fb04b91bSArnd Bergmann 	time64_t t;
277783f6184SRyusuke Konishi 
278d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
279fb04b91bSArnd Bergmann 	t = ktime_get_real_seconds();
280b2ac86e1SJiro SEKIBA 	nilfs->ns_sbwtime = t;
281e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
282e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
283e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
284e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
285e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
286b2ac86e1SJiro SEKIBA 	if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
287b2ac86e1SJiro SEKIBA 		sbp[1]->s_wtime = sbp[0]->s_wtime;
288b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = 0;
289b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
290b2ac86e1SJiro SEKIBA 					    (unsigned char *)sbp[1],
291b2ac86e1SJiro SEKIBA 					    nilfs->ns_sbsize));
292e339ad31SRyusuke Konishi 	}
293e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
294e2c7617aSAndreas Rohner 	nilfs->ns_flushed_device = 1;
295e2c7617aSAndreas Rohner 	/* make sure store to ns_flushed_device cannot be reordered */
296e2c7617aSAndreas Rohner 	smp_wmb();
297f7545144SRyusuke Konishi 	return nilfs_sync_super(sb, flag);
298783f6184SRyusuke Konishi }
299783f6184SRyusuke Konishi 
3007ecaa46cSRyusuke Konishi /**
3017ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
302f7545144SRyusuke Konishi  * @sb: super block instance to be unmounted or degraded to read-only
3037ecaa46cSRyusuke Konishi  *
3047ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
3057ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3067ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
3077ecaa46cSRyusuke Konishi  */
308f7545144SRyusuke Konishi int nilfs_cleanup_super(struct super_block *sb)
3097ecaa46cSRyusuke Konishi {
310e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
311d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
312b2ac86e1SJiro SEKIBA 	int flag = NILFS_SB_COMMIT;
313d26493b6SJiro SEKIBA 	int ret = -EIO;
3147ecaa46cSRyusuke Konishi 
315f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
316d26493b6SJiro SEKIBA 	if (sbp) {
317f7545144SRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
318f7545144SRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
319b2ac86e1SJiro SEKIBA 		if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
320b2ac86e1SJiro SEKIBA 			/*
321b2ac86e1SJiro SEKIBA 			 * make the "clean" flag also to the opposite
322b2ac86e1SJiro SEKIBA 			 * super block if both super blocks point to
323b2ac86e1SJiro SEKIBA 			 * the same checkpoint.
324b2ac86e1SJiro SEKIBA 			 */
325b2ac86e1SJiro SEKIBA 			sbp[1]->s_state = sbp[0]->s_state;
326b2ac86e1SJiro SEKIBA 			flag = NILFS_SB_COMMIT_ALL;
327b2ac86e1SJiro SEKIBA 		}
328f7545144SRyusuke Konishi 		ret = nilfs_commit_super(sb, flag);
329d26493b6SJiro SEKIBA 	}
3307ecaa46cSRyusuke Konishi 	return ret;
3317ecaa46cSRyusuke Konishi }
3327ecaa46cSRyusuke Konishi 
333cfb0a4bfSRyusuke Konishi /**
334cfb0a4bfSRyusuke Konishi  * nilfs_move_2nd_super - relocate secondary super block
335cfb0a4bfSRyusuke Konishi  * @sb: super block instance
336cfb0a4bfSRyusuke Konishi  * @sb2off: new offset of the secondary super block (in bytes)
337cfb0a4bfSRyusuke Konishi  */
338cfb0a4bfSRyusuke Konishi static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
339cfb0a4bfSRyusuke Konishi {
340cfb0a4bfSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
341cfb0a4bfSRyusuke Konishi 	struct buffer_head *nsbh;
342cfb0a4bfSRyusuke Konishi 	struct nilfs_super_block *nsbp;
343cfb0a4bfSRyusuke Konishi 	sector_t blocknr, newblocknr;
344cfb0a4bfSRyusuke Konishi 	unsigned long offset;
3454f05028fSRyusuke Konishi 	int sb2i;  /* array index of the secondary superblock */
346cfb0a4bfSRyusuke Konishi 	int ret = 0;
347cfb0a4bfSRyusuke Konishi 
348cfb0a4bfSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
349cfb0a4bfSRyusuke Konishi 	if (nilfs->ns_sbh[1] &&
350cfb0a4bfSRyusuke Konishi 	    nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
351cfb0a4bfSRyusuke Konishi 		sb2i = 1;
352cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[1]->b_blocknr;
353cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
354cfb0a4bfSRyusuke Konishi 		sb2i = 0;
355cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[0]->b_blocknr;
3564f05028fSRyusuke Konishi 	} else {
3574f05028fSRyusuke Konishi 		sb2i = -1;
3584f05028fSRyusuke Konishi 		blocknr = 0;
359cfb0a4bfSRyusuke Konishi 	}
360cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
361cfb0a4bfSRyusuke Konishi 		goto out;  /* super block location is unchanged */
362cfb0a4bfSRyusuke Konishi 
363cfb0a4bfSRyusuke Konishi 	/* Get new super block buffer */
364cfb0a4bfSRyusuke Konishi 	newblocknr = sb2off >> nilfs->ns_blocksize_bits;
365cfb0a4bfSRyusuke Konishi 	offset = sb2off & (nilfs->ns_blocksize - 1);
366cfb0a4bfSRyusuke Konishi 	nsbh = sb_getblk(sb, newblocknr);
367cfb0a4bfSRyusuke Konishi 	if (!nsbh) {
368feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
369feee880fSRyusuke Konishi 			  "unable to move secondary superblock to block %llu",
370feee880fSRyusuke Konishi 			  (unsigned long long)newblocknr);
371cfb0a4bfSRyusuke Konishi 		ret = -EIO;
372cfb0a4bfSRyusuke Konishi 		goto out;
373cfb0a4bfSRyusuke Konishi 	}
374cfb0a4bfSRyusuke Konishi 	nsbp = (void *)nsbh->b_data + offset;
375cfb0a4bfSRyusuke Konishi 	memset(nsbp, 0, nilfs->ns_blocksize);
376cfb0a4bfSRyusuke Konishi 
377cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0) {
378cfb0a4bfSRyusuke Konishi 		memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
379cfb0a4bfSRyusuke Konishi 		brelse(nilfs->ns_sbh[sb2i]);
380cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[sb2i] = nsbh;
381cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[sb2i] = nsbp;
382cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
383cfb0a4bfSRyusuke Konishi 		/* secondary super block will be restored to index 1 */
384cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[1] = nsbh;
385cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[1] = nsbp;
386cfb0a4bfSRyusuke Konishi 	} else {
387cfb0a4bfSRyusuke Konishi 		brelse(nsbh);
388cfb0a4bfSRyusuke Konishi 	}
389cfb0a4bfSRyusuke Konishi out:
390cfb0a4bfSRyusuke Konishi 	return ret;
391cfb0a4bfSRyusuke Konishi }
392cfb0a4bfSRyusuke Konishi 
3934e33f9eaSRyusuke Konishi /**
3944e33f9eaSRyusuke Konishi  * nilfs_resize_fs - resize the filesystem
3954e33f9eaSRyusuke Konishi  * @sb: super block instance
3964e33f9eaSRyusuke Konishi  * @newsize: new size of the filesystem (in bytes)
3974e33f9eaSRyusuke Konishi  */
3984e33f9eaSRyusuke Konishi int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
3994e33f9eaSRyusuke Konishi {
4004e33f9eaSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
4014e33f9eaSRyusuke Konishi 	struct nilfs_super_block **sbp;
4024e33f9eaSRyusuke Konishi 	__u64 devsize, newnsegs;
4034e33f9eaSRyusuke Konishi 	loff_t sb2off;
4044e33f9eaSRyusuke Konishi 	int ret;
4054e33f9eaSRyusuke Konishi 
4064e33f9eaSRyusuke Konishi 	ret = -ERANGE;
4074e33f9eaSRyusuke Konishi 	devsize = i_size_read(sb->s_bdev->bd_inode);
4084e33f9eaSRyusuke Konishi 	if (newsize > devsize)
4094e33f9eaSRyusuke Konishi 		goto out;
4104e33f9eaSRyusuke Konishi 
4114e33f9eaSRyusuke Konishi 	/*
4124e33f9eaSRyusuke Konishi 	 * Write lock is required to protect some functions depending
4134e33f9eaSRyusuke Konishi 	 * on the number of segments, the number of reserved segments,
4144e33f9eaSRyusuke Konishi 	 * and so forth.
4154e33f9eaSRyusuke Konishi 	 */
4164e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_segctor_sem);
4174e33f9eaSRyusuke Konishi 
4184e33f9eaSRyusuke Konishi 	sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
4194e33f9eaSRyusuke Konishi 	newnsegs = sb2off >> nilfs->ns_blocksize_bits;
4204e33f9eaSRyusuke Konishi 	do_div(newnsegs, nilfs->ns_blocks_per_segment);
4214e33f9eaSRyusuke Konishi 
4224e33f9eaSRyusuke Konishi 	ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
4234e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_segctor_sem);
4244e33f9eaSRyusuke Konishi 	if (ret < 0)
4254e33f9eaSRyusuke Konishi 		goto out;
4264e33f9eaSRyusuke Konishi 
4274e33f9eaSRyusuke Konishi 	ret = nilfs_construct_segment(sb);
4284e33f9eaSRyusuke Konishi 	if (ret < 0)
4294e33f9eaSRyusuke Konishi 		goto out;
4304e33f9eaSRyusuke Konishi 
4314e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_sem);
4324e33f9eaSRyusuke Konishi 	nilfs_move_2nd_super(sb, sb2off);
4334e33f9eaSRyusuke Konishi 	ret = -EIO;
4344e33f9eaSRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
4354e33f9eaSRyusuke Konishi 	if (likely(sbp)) {
4364e33f9eaSRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
4374e33f9eaSRyusuke Konishi 		/*
4384e33f9eaSRyusuke Konishi 		 * Drop NILFS_RESIZE_FS flag for compatibility with
4394e33f9eaSRyusuke Konishi 		 * mount-time resize which may be implemented in a
4404e33f9eaSRyusuke Konishi 		 * future release.
4414e33f9eaSRyusuke Konishi 		 */
4424e33f9eaSRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
4434e33f9eaSRyusuke Konishi 					      ~NILFS_RESIZE_FS);
4444e33f9eaSRyusuke Konishi 		sbp[0]->s_dev_size = cpu_to_le64(newsize);
4454e33f9eaSRyusuke Konishi 		sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
4464e33f9eaSRyusuke Konishi 		if (sbp[1])
4474e33f9eaSRyusuke Konishi 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
4484e33f9eaSRyusuke Konishi 		ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
4494e33f9eaSRyusuke Konishi 	}
4504e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_sem);
4514e33f9eaSRyusuke Konishi 
4524e33f9eaSRyusuke Konishi 	/*
4534e33f9eaSRyusuke Konishi 	 * Reset the range of allocatable segments last.  This order
4544e33f9eaSRyusuke Konishi 	 * is important in the case of expansion because the secondary
4554e33f9eaSRyusuke Konishi 	 * superblock must be protected from log write until migration
4564e33f9eaSRyusuke Konishi 	 * completes.
4574e33f9eaSRyusuke Konishi 	 */
4584e33f9eaSRyusuke Konishi 	if (!ret)
4594e33f9eaSRyusuke Konishi 		nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
4604e33f9eaSRyusuke Konishi out:
4614e33f9eaSRyusuke Konishi 	return ret;
4624e33f9eaSRyusuke Konishi }
4634e33f9eaSRyusuke Konishi 
464783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
465783f6184SRyusuke Konishi {
466e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
467783f6184SRyusuke Konishi 
468f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
469783f6184SRyusuke Konishi 
470bc98a42cSDavid Howells 	if (!sb_rdonly(sb)) {
471783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
472f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
473783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
474783f6184SRyusuke Konishi 	}
475783f6184SRyusuke Konishi 
476f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
477f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
478f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
479f1e89c86SRyusuke Konishi 
480348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
481783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
482783f6184SRyusuke Konishi }
483783f6184SRyusuke Konishi 
484783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
485783f6184SRyusuke Konishi {
486e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
487d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
488783f6184SRyusuke Konishi 	int err = 0;
489783f6184SRyusuke Konishi 
490783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
491783f6184SRyusuke Konishi 	if (wait)
492783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
4936233caa9SJiro SEKIBA 
4946233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
495d26493b6SJiro SEKIBA 	if (nilfs_sb_dirty(nilfs)) {
496f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
497b2ac86e1SJiro SEKIBA 		if (likely(sbp)) {
498b2ac86e1SJiro SEKIBA 			nilfs_set_log_cursor(sbp[0], nilfs);
499f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT);
500b2ac86e1SJiro SEKIBA 		}
501d26493b6SJiro SEKIBA 	}
5026233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
5036233caa9SJiro SEKIBA 
504e2c7617aSAndreas Rohner 	if (!err)
505e2c7617aSAndreas Rohner 		err = nilfs_flush_device(nilfs);
506e2c7617aSAndreas Rohner 
507783f6184SRyusuke Konishi 	return err;
508783f6184SRyusuke Konishi }
509783f6184SRyusuke Konishi 
510f7545144SRyusuke Konishi int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
5114d8d9293SRyusuke Konishi 			    struct nilfs_root **rootp)
512783f6184SRyusuke Konishi {
513e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5144d8d9293SRyusuke Konishi 	struct nilfs_root *root;
515783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
516783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
5174d8d9293SRyusuke Konishi 	int err = -ENOMEM;
518783f6184SRyusuke Konishi 
5194d8d9293SRyusuke Konishi 	root = nilfs_find_or_create_root(
5204d8d9293SRyusuke Konishi 		nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
5214d8d9293SRyusuke Konishi 	if (!root)
5224d8d9293SRyusuke Konishi 		return err;
523783f6184SRyusuke Konishi 
524e912a5b6SRyusuke Konishi 	if (root->ifile)
525e912a5b6SRyusuke Konishi 		goto reuse; /* already attached checkpoint */
526783f6184SRyusuke Konishi 
5271154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
528783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
529783f6184SRyusuke Konishi 					  &bh_cp);
5301154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
531783f6184SRyusuke Konishi 	if (unlikely(err)) {
532783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
533feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_ERR,
534feee880fSRyusuke Konishi 				  "Invalid checkpoint (checkpoint number=%llu)",
535783f6184SRyusuke Konishi 				  (unsigned long long)cno);
536783f6184SRyusuke Konishi 			err = -EINVAL;
537783f6184SRyusuke Konishi 		}
538783f6184SRyusuke Konishi 		goto failed;
539783f6184SRyusuke Konishi 	}
540f1e89c86SRyusuke Konishi 
541f7545144SRyusuke Konishi 	err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
542f1e89c86SRyusuke Konishi 			       &raw_cp->cp_ifile_inode, &root->ifile);
543f1e89c86SRyusuke Konishi 	if (err)
544783f6184SRyusuke Konishi 		goto failed_bh;
545b7c06342SRyusuke Konishi 
546e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->inodes_count,
547e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_inodes_count));
548e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->blocks_count,
549e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_blocks_count));
550783f6184SRyusuke Konishi 
551783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
5524d8d9293SRyusuke Konishi 
553e912a5b6SRyusuke Konishi  reuse:
5544d8d9293SRyusuke Konishi 	*rootp = root;
555783f6184SRyusuke Konishi 	return 0;
556783f6184SRyusuke Konishi 
557783f6184SRyusuke Konishi  failed_bh:
558783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
559783f6184SRyusuke Konishi  failed:
5604d8d9293SRyusuke Konishi 	nilfs_put_root(root);
561783f6184SRyusuke Konishi 
562783f6184SRyusuke Konishi 	return err;
563783f6184SRyusuke Konishi }
564783f6184SRyusuke Konishi 
5655beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb)
566783f6184SRyusuke Konishi {
567e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5685beb6e0bSRyusuke Konishi 	int err;
5695beb6e0bSRyusuke Konishi 
570bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5715beb6e0bSRyusuke Konishi 		return 0;
5725beb6e0bSRyusuke Konishi 
5735beb6e0bSRyusuke Konishi 	/* Mark super block clean */
5745beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
575f7545144SRyusuke Konishi 	err = nilfs_cleanup_super(sb);
5765beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
5775beb6e0bSRyusuke Konishi 	return err;
5785beb6e0bSRyusuke Konishi }
5795beb6e0bSRyusuke Konishi 
5805beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb)
5815beb6e0bSRyusuke Konishi {
582e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
583783f6184SRyusuke Konishi 
584bc98a42cSDavid Howells 	if (sb_rdonly(sb))
5855beb6e0bSRyusuke Konishi 		return 0;
5865beb6e0bSRyusuke Konishi 
5875beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
588f7545144SRyusuke Konishi 	nilfs_setup_super(sb, false);
5895beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
5905beb6e0bSRyusuke Konishi 	return 0;
591783f6184SRyusuke Konishi }
592783f6184SRyusuke Konishi 
593783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
594783f6184SRyusuke Konishi {
595783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
5962b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
597b7c06342SRyusuke Konishi 	struct the_nilfs *nilfs = root->nilfs;
598c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
599783f6184SRyusuke Konishi 	unsigned long long blocks;
600783f6184SRyusuke Konishi 	unsigned long overhead;
601783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
602783f6184SRyusuke Konishi 	sector_t nfreeblocks;
603c7ef972cSVyacheslav Dubeyko 	u64 nmaxinodes, nfreeinodes;
604783f6184SRyusuke Konishi 	int err;
605783f6184SRyusuke Konishi 
606783f6184SRyusuke Konishi 	/*
607783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
608783f6184SRyusuke Konishi 	 *
609783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
610783f6184SRyusuke Konishi 	 * are excluded.
611783f6184SRyusuke Konishi 	 */
612783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
613783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
614783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
615783f6184SRyusuke Konishi 
616783f6184SRyusuke Konishi 	/*
617783f6184SRyusuke Konishi 	 * Compute the overhead
618783f6184SRyusuke Konishi 	 *
6197a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
620783f6184SRyusuke Konishi 	 * We must count them as the overhead.
621783f6184SRyusuke Konishi 	 */
622783f6184SRyusuke Konishi 	overhead = 0;
623783f6184SRyusuke Konishi 
624783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
625783f6184SRyusuke Konishi 	if (unlikely(err))
626783f6184SRyusuke Konishi 		return err;
627783f6184SRyusuke Konishi 
628c7ef972cSVyacheslav Dubeyko 	err = nilfs_ifile_count_free_inodes(root->ifile,
629c7ef972cSVyacheslav Dubeyko 					    &nmaxinodes, &nfreeinodes);
630c7ef972cSVyacheslav Dubeyko 	if (unlikely(err)) {
631feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
632feee880fSRyusuke Konishi 			  "failed to count free inodes: err=%d", err);
633c7ef972cSVyacheslav Dubeyko 		if (err == -ERANGE) {
634c7ef972cSVyacheslav Dubeyko 			/*
635c7ef972cSVyacheslav Dubeyko 			 * If nilfs_palloc_count_max_entries() returns
636c7ef972cSVyacheslav Dubeyko 			 * -ERANGE error code then we simply treat
637c7ef972cSVyacheslav Dubeyko 			 * curent inodes count as maximum possible and
638c7ef972cSVyacheslav Dubeyko 			 * zero as free inodes value.
639c7ef972cSVyacheslav Dubeyko 			 */
640e5f7f848SVyacheslav Dubeyko 			nmaxinodes = atomic64_read(&root->inodes_count);
641c7ef972cSVyacheslav Dubeyko 			nfreeinodes = 0;
642c7ef972cSVyacheslav Dubeyko 			err = 0;
643c7ef972cSVyacheslav Dubeyko 		} else
644c7ef972cSVyacheslav Dubeyko 			return err;
645c7ef972cSVyacheslav Dubeyko 	}
646c7ef972cSVyacheslav Dubeyko 
647783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
648783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
649783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
650783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
651783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
652783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
653c7ef972cSVyacheslav Dubeyko 	buf->f_files = nmaxinodes;
654c7ef972cSVyacheslav Dubeyko 	buf->f_ffree = nfreeinodes;
655783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
656c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
657c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
658c306af23SRyusuke Konishi 
659783f6184SRyusuke Konishi 	return 0;
660783f6184SRyusuke Konishi }
661783f6184SRyusuke Konishi 
66234c80b1dSAl Viro static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
663b58a285bSJiro SEKIBA {
66434c80b1dSAl Viro 	struct super_block *sb = dentry->d_sb;
665e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
6662b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
667b58a285bSJiro SEKIBA 
6683b2ce58bSRyusuke Konishi 	if (!nilfs_test_opt(nilfs, BARRIER))
669c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
670f11459adSRyusuke Konishi 	if (root->cno != NILFS_CPTREE_CURRENT_CNO)
671f11459adSRyusuke Konishi 		seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
6723b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
673c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
6743b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_CONT))
675c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
6763b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, STRICT_ORDER))
677c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
6783b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, NORECOVERY))
679c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
6803b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, DISCARD))
681c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
682b58a285bSJiro SEKIBA 
683b58a285bSJiro SEKIBA 	return 0;
684b58a285bSJiro SEKIBA }
685b58a285bSJiro SEKIBA 
686b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
687783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
688977c3d18SAl Viro 	.free_inode     = nilfs_free_inode,
689783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
6906fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
691783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
692783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
6935beb6e0bSRyusuke Konishi 	.freeze_fs	= nilfs_freeze,
6945beb6e0bSRyusuke Konishi 	.unfreeze_fs	= nilfs_unfreeze,
695783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
696783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
697b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
698783f6184SRyusuke Konishi };
699783f6184SRyusuke Konishi 
700783f6184SRyusuke Konishi enum {
701783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
702773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
703802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, Opt_err,
704783f6184SRyusuke Konishi };
705783f6184SRyusuke Konishi 
706783f6184SRyusuke Konishi static match_table_t tokens = {
707783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
708783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
709783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
710773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
71191f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
712783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
713783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
7140234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
715e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
716802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
717783f6184SRyusuke Konishi 	{Opt_err, NULL}
718783f6184SRyusuke Konishi };
719783f6184SRyusuke Konishi 
7207c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
721783f6184SRyusuke Konishi {
722e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
723783f6184SRyusuke Konishi 	char *p;
724783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
725783f6184SRyusuke Konishi 
726783f6184SRyusuke Konishi 	if (!options)
727783f6184SRyusuke Konishi 		return 1;
728783f6184SRyusuke Konishi 
729783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
730783f6184SRyusuke Konishi 		int token;
7314ad364caSRyusuke Konishi 
732783f6184SRyusuke Konishi 		if (!*p)
733783f6184SRyusuke Konishi 			continue;
734783f6184SRyusuke Konishi 
735783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
736783f6184SRyusuke Konishi 		switch (token) {
737773bc4f3SRyusuke Konishi 		case Opt_barrier:
7383b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, BARRIER);
739773bc4f3SRyusuke Konishi 			break;
74091f1953bSJiro SEKIBA 		case Opt_nobarrier:
7413b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, BARRIER);
742783f6184SRyusuke Konishi 			break;
743783f6184SRyusuke Konishi 		case Opt_order:
744783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
745783f6184SRyusuke Konishi 				/* Ordered data semantics */
7463b2ce58bSRyusuke Konishi 				nilfs_clear_opt(nilfs, STRICT_ORDER);
747783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
748783f6184SRyusuke Konishi 				/* Strict in-order semantics */
7493b2ce58bSRyusuke Konishi 				nilfs_set_opt(nilfs, STRICT_ORDER);
750783f6184SRyusuke Konishi 			else
751783f6184SRyusuke Konishi 				return 0;
752783f6184SRyusuke Konishi 			break;
753783f6184SRyusuke Konishi 		case Opt_err_panic:
7543b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
755783f6184SRyusuke Konishi 			break;
756783f6184SRyusuke Konishi 		case Opt_err_ro:
7573b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
758783f6184SRyusuke Konishi 			break;
759783f6184SRyusuke Konishi 		case Opt_err_cont:
7603b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
761783f6184SRyusuke Konishi 			break;
762783f6184SRyusuke Konishi 		case Opt_snapshot:
7637c017457SRyusuke Konishi 			if (is_remount) {
764feee880fSRyusuke Konishi 				nilfs_msg(sb, KERN_ERR,
765feee880fSRyusuke Konishi 					  "\"%s\" option is invalid for remount",
766feee880fSRyusuke Konishi 					  p);
7677c017457SRyusuke Konishi 				return 0;
7687c017457SRyusuke Konishi 			}
7697c017457SRyusuke Konishi 			break;
7700234576dSRyusuke Konishi 		case Opt_norecovery:
7713b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, NORECOVERY);
7720234576dSRyusuke Konishi 			break;
773e902ec99SJiro SEKIBA 		case Opt_discard:
7743b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, DISCARD);
775e902ec99SJiro SEKIBA 			break;
776802d3177SRyusuke Konishi 		case Opt_nodiscard:
7773b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, DISCARD);
778802d3177SRyusuke Konishi 			break;
779783f6184SRyusuke Konishi 		default:
780feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_ERR,
781feee880fSRyusuke Konishi 				  "unrecognized mount option \"%s\"", p);
782783f6184SRyusuke Konishi 			return 0;
783783f6184SRyusuke Konishi 		}
784783f6184SRyusuke Konishi 	}
785783f6184SRyusuke Konishi 	return 1;
786783f6184SRyusuke Konishi }
787783f6184SRyusuke Konishi 
788783f6184SRyusuke Konishi static inline void
7893b2ce58bSRyusuke Konishi nilfs_set_default_options(struct super_block *sb,
790783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
791783f6184SRyusuke Konishi {
792e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
7933b2ce58bSRyusuke Konishi 
7943b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt =
795277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
796783f6184SRyusuke Konishi }
797783f6184SRyusuke Konishi 
798f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount)
799783f6184SRyusuke Konishi {
800e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
801d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
802d26493b6SJiro SEKIBA 	int max_mnt_count;
803d26493b6SJiro SEKIBA 	int mnt_count;
804783f6184SRyusuke Konishi 
805d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
806f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
807d26493b6SJiro SEKIBA 	if (!sbp)
808d26493b6SJiro SEKIBA 		return -EIO;
809d26493b6SJiro SEKIBA 
8105beb6e0bSRyusuke Konishi 	if (!is_mount)
8115beb6e0bSRyusuke Konishi 		goto skip_mount_setup;
8125beb6e0bSRyusuke Konishi 
813d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
814d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
815d26493b6SJiro SEKIBA 
816f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
817feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
818783f6184SRyusuke Konishi #if 0
819783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
820feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
821783f6184SRyusuke Konishi #endif
822783f6184SRyusuke Konishi 	}
823783f6184SRyusuke Konishi 	if (!max_mnt_count)
824d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
825783f6184SRyusuke Konishi 
826d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
82721a1a52dSArnd Bergmann 	sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
8285beb6e0bSRyusuke Konishi 
8295beb6e0bSRyusuke Konishi skip_mount_setup:
830d26493b6SJiro SEKIBA 	sbp[0]->s_state =
831d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
832b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
8330ca7a5b9SRyusuke Konishi 	if (sbp[1])
834b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
835f7545144SRyusuke Konishi 	return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
836783f6184SRyusuke Konishi }
837783f6184SRyusuke Konishi 
838e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
839e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
840e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
841783f6184SRyusuke Konishi {
842e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
843e339ad31SRyusuke Konishi 	unsigned long offset;
844783f6184SRyusuke Konishi 
845e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
846783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
847e339ad31SRyusuke Konishi 	if (!*pbh)
848783f6184SRyusuke Konishi 		return NULL;
849783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
850783f6184SRyusuke Konishi }
851783f6184SRyusuke Konishi 
852783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
853783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
854783f6184SRyusuke Konishi 				 char *data)
855783f6184SRyusuke Konishi {
856e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
857783f6184SRyusuke Konishi 
858783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
859783f6184SRyusuke Konishi 
860783f6184SRyusuke Konishi 	/* FS independent flags */
861783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
8621751e8a6SLinus Torvalds 	sb->s_flags |= SB_NOATIME;
863783f6184SRyusuke Konishi #endif
864783f6184SRyusuke Konishi 
8653b2ce58bSRyusuke Konishi 	nilfs_set_default_options(sb, sbp);
866783f6184SRyusuke Konishi 
867574e6c31SRyusuke Konishi 	nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
868574e6c31SRyusuke Konishi 	nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
869574e6c31SRyusuke Konishi 	nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
870574e6c31SRyusuke Konishi 	nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
871783f6184SRyusuke Konishi 
8727c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0;
873783f6184SRyusuke Konishi }
874783f6184SRyusuke Konishi 
875c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
876c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
877c5ca48aaSRyusuke Konishi {
878c5ca48aaSRyusuke Konishi 	__u64 features;
879c5ca48aaSRyusuke Konishi 
880c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
881c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
882c5ca48aaSRyusuke Konishi 	if (features) {
883feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
884feee880fSRyusuke Konishi 			  "couldn't mount because of unsupported optional features (%llx)",
885c5ca48aaSRyusuke Konishi 			  (unsigned long long)features);
886c5ca48aaSRyusuke Konishi 		return -EINVAL;
887c5ca48aaSRyusuke Konishi 	}
888c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
889c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
890bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && features) {
891feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
892feee880fSRyusuke Konishi 			  "couldn't mount RDWR because of unsupported optional features (%llx)",
893c5ca48aaSRyusuke Konishi 			  (unsigned long long)features);
894c5ca48aaSRyusuke Konishi 		return -EINVAL;
895c5ca48aaSRyusuke Konishi 	}
896c5ca48aaSRyusuke Konishi 	return 0;
897c5ca48aaSRyusuke Konishi }
898c5ca48aaSRyusuke Konishi 
899367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
900367ea334SRyusuke Konishi 				 struct nilfs_root *root,
901367ea334SRyusuke Konishi 				 struct dentry **root_dentry)
902367ea334SRyusuke Konishi {
903367ea334SRyusuke Konishi 	struct inode *inode;
904367ea334SRyusuke Konishi 	struct dentry *dentry;
905367ea334SRyusuke Konishi 	int ret = 0;
906367ea334SRyusuke Konishi 
907367ea334SRyusuke Konishi 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
908367ea334SRyusuke Konishi 	if (IS_ERR(inode)) {
909367ea334SRyusuke Konishi 		ret = PTR_ERR(inode);
910feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
911367ea334SRyusuke Konishi 		goto out;
912367ea334SRyusuke Konishi 	}
913367ea334SRyusuke Konishi 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
914367ea334SRyusuke Konishi 		iput(inode);
915feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "corrupt root inode");
916367ea334SRyusuke Konishi 		ret = -EINVAL;
917367ea334SRyusuke Konishi 		goto out;
918367ea334SRyusuke Konishi 	}
919367ea334SRyusuke Konishi 
920f11459adSRyusuke Konishi 	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
921f11459adSRyusuke Konishi 		dentry = d_find_alias(inode);
922f11459adSRyusuke Konishi 		if (!dentry) {
92348fde701SAl Viro 			dentry = d_make_root(inode);
924367ea334SRyusuke Konishi 			if (!dentry) {
925367ea334SRyusuke Konishi 				ret = -ENOMEM;
926f11459adSRyusuke Konishi 				goto failed_dentry;
927f11459adSRyusuke Konishi 			}
928f11459adSRyusuke Konishi 		} else {
929f11459adSRyusuke Konishi 			iput(inode);
930f11459adSRyusuke Konishi 		}
931f11459adSRyusuke Konishi 	} else {
9321a0a397eSJ. Bruce Fields 		dentry = d_obtain_root(inode);
933f11459adSRyusuke Konishi 		if (IS_ERR(dentry)) {
934f11459adSRyusuke Konishi 			ret = PTR_ERR(dentry);
935f11459adSRyusuke Konishi 			goto failed_dentry;
936f11459adSRyusuke Konishi 		}
937367ea334SRyusuke Konishi 	}
938367ea334SRyusuke Konishi 	*root_dentry = dentry;
939367ea334SRyusuke Konishi  out:
940367ea334SRyusuke Konishi 	return ret;
941f11459adSRyusuke Konishi 
942f11459adSRyusuke Konishi  failed_dentry:
943feee880fSRyusuke Konishi 	nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
944f11459adSRyusuke Konishi 	goto out;
945367ea334SRyusuke Konishi }
946367ea334SRyusuke Konishi 
947ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
948ab4d8f7eSRyusuke Konishi 				 struct dentry **root_dentry)
949ab4d8f7eSRyusuke Konishi {
950e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = s->s_fs_info;
951ab4d8f7eSRyusuke Konishi 	struct nilfs_root *root;
952ab4d8f7eSRyusuke Konishi 	int ret;
953ab4d8f7eSRyusuke Konishi 
954572d8b39SRyusuke Konishi 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
955572d8b39SRyusuke Konishi 
956ab4d8f7eSRyusuke Konishi 	down_read(&nilfs->ns_segctor_sem);
957ab4d8f7eSRyusuke Konishi 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
958ab4d8f7eSRyusuke Konishi 	up_read(&nilfs->ns_segctor_sem);
959ab4d8f7eSRyusuke Konishi 	if (ret < 0) {
960ab4d8f7eSRyusuke Konishi 		ret = (ret == -ENOENT) ? -EINVAL : ret;
961ab4d8f7eSRyusuke Konishi 		goto out;
962ab4d8f7eSRyusuke Konishi 	} else if (!ret) {
963feee880fSRyusuke Konishi 		nilfs_msg(s, KERN_ERR,
964feee880fSRyusuke Konishi 			  "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
965ab4d8f7eSRyusuke Konishi 			  (unsigned long long)cno);
966ab4d8f7eSRyusuke Konishi 		ret = -EINVAL;
967ab4d8f7eSRyusuke Konishi 		goto out;
968ab4d8f7eSRyusuke Konishi 	}
969ab4d8f7eSRyusuke Konishi 
970f7545144SRyusuke Konishi 	ret = nilfs_attach_checkpoint(s, cno, false, &root);
971ab4d8f7eSRyusuke Konishi 	if (ret) {
972feee880fSRyusuke Konishi 		nilfs_msg(s, KERN_ERR,
973feee880fSRyusuke Konishi 			  "error %d while loading snapshot (checkpoint number=%llu)",
974feee880fSRyusuke Konishi 			  ret, (unsigned long long)cno);
975ab4d8f7eSRyusuke Konishi 		goto out;
976ab4d8f7eSRyusuke Konishi 	}
977ab4d8f7eSRyusuke Konishi 	ret = nilfs_get_root_dentry(s, root, root_dentry);
978ab4d8f7eSRyusuke Konishi 	nilfs_put_root(root);
979ab4d8f7eSRyusuke Konishi  out:
980572d8b39SRyusuke Konishi 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
981ab4d8f7eSRyusuke Konishi 	return ret;
982ab4d8f7eSRyusuke Konishi }
983ab4d8f7eSRyusuke Konishi 
984f11459adSRyusuke Konishi /**
985e95c311eSAl Viro  * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
986f11459adSRyusuke Konishi  * @root_dentry: root dentry of the tree to be shrunk
987f11459adSRyusuke Konishi  *
988f11459adSRyusuke Konishi  * This function returns true if the tree was in-use.
989f11459adSRyusuke Konishi  */
990e95c311eSAl Viro static bool nilfs_tree_is_busy(struct dentry *root_dentry)
991f11459adSRyusuke Konishi {
992f11459adSRyusuke Konishi 	shrink_dcache_parent(root_dentry);
993e95c311eSAl Viro 	return d_count(root_dentry) > 1;
994f11459adSRyusuke Konishi }
995f11459adSRyusuke Konishi 
996032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
997032dbb3bSRyusuke Konishi {
998e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
999032dbb3bSRyusuke Konishi 	struct nilfs_root *root;
1000032dbb3bSRyusuke Konishi 	struct inode *inode;
1001032dbb3bSRyusuke Konishi 	struct dentry *dentry;
1002032dbb3bSRyusuke Konishi 	int ret;
1003032dbb3bSRyusuke Konishi 
10043377f843SRyusuke Konishi 	if (cno > nilfs->ns_cno)
1005032dbb3bSRyusuke Konishi 		return false;
1006032dbb3bSRyusuke Konishi 
1007032dbb3bSRyusuke Konishi 	if (cno >= nilfs_last_cno(nilfs))
1008032dbb3bSRyusuke Konishi 		return true;	/* protect recent checkpoints */
1009032dbb3bSRyusuke Konishi 
1010032dbb3bSRyusuke Konishi 	ret = false;
1011e3154e97SRyusuke Konishi 	root = nilfs_lookup_root(nilfs, cno);
1012032dbb3bSRyusuke Konishi 	if (root) {
1013032dbb3bSRyusuke Konishi 		inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1014032dbb3bSRyusuke Konishi 		if (inode) {
1015032dbb3bSRyusuke Konishi 			dentry = d_find_alias(inode);
1016032dbb3bSRyusuke Konishi 			if (dentry) {
1017e95c311eSAl Viro 				ret = nilfs_tree_is_busy(dentry);
1018032dbb3bSRyusuke Konishi 				dput(dentry);
1019032dbb3bSRyusuke Konishi 			}
1020032dbb3bSRyusuke Konishi 			iput(inode);
1021032dbb3bSRyusuke Konishi 		}
1022032dbb3bSRyusuke Konishi 		nilfs_put_root(root);
1023032dbb3bSRyusuke Konishi 	}
1024032dbb3bSRyusuke Konishi 	return ret;
1025032dbb3bSRyusuke Konishi }
1026032dbb3bSRyusuke Konishi 
1027783f6184SRyusuke Konishi /**
1028783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
1029783f6184SRyusuke Konishi  * @sb: super_block
1030783f6184SRyusuke Konishi  * @data: mount options
1031783f6184SRyusuke Konishi  * @silent: silent mode flag
1032783f6184SRyusuke Konishi  *
1033aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
1034783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
1035783f6184SRyusuke Konishi  */
1036783f6184SRyusuke Konishi static int
1037348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent)
1038783f6184SRyusuke Konishi {
1039348fe8daSRyusuke Konishi 	struct the_nilfs *nilfs;
10404d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
1041783f6184SRyusuke Konishi 	__u64 cno;
1042783f6184SRyusuke Konishi 	int err;
1043783f6184SRyusuke Konishi 
10446625689eSRyusuke Konishi 	nilfs = alloc_nilfs(sb);
1045e3154e97SRyusuke Konishi 	if (!nilfs)
1046783f6184SRyusuke Konishi 		return -ENOMEM;
1047783f6184SRyusuke Konishi 
1048e3154e97SRyusuke Konishi 	sb->s_fs_info = nilfs;
1049783f6184SRyusuke Konishi 
1050f7545144SRyusuke Konishi 	err = init_nilfs(nilfs, sb, (char *)data);
1051783f6184SRyusuke Konishi 	if (err)
1052348fe8daSRyusuke Konishi 		goto failed_nilfs;
1053783f6184SRyusuke Konishi 
1054783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
1055783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
1056783f6184SRyusuke Konishi 	sb->s_root = NULL;
105761239230SRyusuke Konishi 	sb->s_time_gran = 1;
10588de52778SAl Viro 	sb->s_max_links = NILFS_LINK_MAX;
1059026a7d63SRyusuke Konishi 
10600546c537SJan Kara 	sb->s_bdi = bdi_get(sb->s_bdev->bd_bdi);
1061783f6184SRyusuke Konishi 
1062f7545144SRyusuke Konishi 	err = load_nilfs(nilfs, sb);
1063783f6184SRyusuke Konishi 	if (err)
1064348fe8daSRyusuke Konishi 		goto failed_nilfs;
1065f50a4c81SRyusuke Konishi 
1066783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
1067f7545144SRyusuke Konishi 	err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1068783f6184SRyusuke Konishi 	if (err) {
1069feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
1070feee880fSRyusuke Konishi 			  "error %d while loading last checkpoint (checkpoint number=%llu)",
1071feee880fSRyusuke Konishi 			  err, (unsigned long long)cno);
1072f1e89c86SRyusuke Konishi 		goto failed_unload;
1073783f6184SRyusuke Konishi 	}
1074783f6184SRyusuke Konishi 
1075bc98a42cSDavid Howells 	if (!sb_rdonly(sb)) {
1076f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, fsroot);
1077783f6184SRyusuke Konishi 		if (err)
1078783f6184SRyusuke Konishi 			goto failed_checkpoint;
1079783f6184SRyusuke Konishi 	}
1080783f6184SRyusuke Konishi 
1081367ea334SRyusuke Konishi 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1082367ea334SRyusuke Konishi 	if (err)
1083783f6184SRyusuke Konishi 		goto failed_segctor;
1084783f6184SRyusuke Konishi 
10854d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1086783f6184SRyusuke Konishi 
1087bc98a42cSDavid Howells 	if (!sb_rdonly(sb)) {
1088783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1089f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1090783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1091783f6184SRyusuke Konishi 	}
1092783f6184SRyusuke Konishi 
1093783f6184SRyusuke Konishi 	return 0;
1094783f6184SRyusuke Konishi 
1095783f6184SRyusuke Konishi  failed_segctor:
1096f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
1097783f6184SRyusuke Konishi 
1098783f6184SRyusuke Konishi  failed_checkpoint:
10994d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1100783f6184SRyusuke Konishi 
1101f1e89c86SRyusuke Konishi  failed_unload:
1102f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
1103f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
1104f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
1105f1e89c86SRyusuke Konishi 
1106348fe8daSRyusuke Konishi  failed_nilfs:
1107348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
1108783f6184SRyusuke Konishi 	return err;
1109783f6184SRyusuke Konishi }
1110783f6184SRyusuke Konishi 
1111783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1112783f6184SRyusuke Konishi {
1113e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1114783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
111506df0f99SRyusuke Konishi 	unsigned long old_mount_opt;
1116f11459adSRyusuke Konishi 	int err;
1117783f6184SRyusuke Konishi 
111802b9984dSTheodore Ts'o 	sync_filesystem(sb);
1119783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
11203b2ce58bSRyusuke Konishi 	old_mount_opt = nilfs->ns_mount_opt;
1121783f6184SRyusuke Konishi 
11227c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
1123783f6184SRyusuke Konishi 		err = -EINVAL;
1124783f6184SRyusuke Konishi 		goto restore_opts;
1125783f6184SRyusuke Konishi 	}
11261751e8a6SLinus Torvalds 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
1127783f6184SRyusuke Konishi 
1128d240e067SRyusuke Konishi 	err = -EINVAL;
1129783f6184SRyusuke Konishi 
11300234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
1131feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
1132feee880fSRyusuke Konishi 			  "couldn't remount because the filesystem is in an incomplete recovery state");
11330234576dSRyusuke Konishi 		goto restore_opts;
11340234576dSRyusuke Konishi 	}
11350234576dSRyusuke Konishi 
11361751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1137783f6184SRyusuke Konishi 		goto out;
11381751e8a6SLinus Torvalds 	if (*flags & SB_RDONLY) {
1139f7545144SRyusuke Konishi 		/* Shutting down log writer */
1140f7545144SRyusuke Konishi 		nilfs_detach_log_writer(sb);
11411751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
1142783f6184SRyusuke Konishi 
1143783f6184SRyusuke Konishi 		/*
1144783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
1145783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
1146783f6184SRyusuke Konishi 		 */
1147783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1148f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
1149783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1150783f6184SRyusuke Konishi 	} else {
1151c5ca48aaSRyusuke Konishi 		__u64 features;
1152e912a5b6SRyusuke Konishi 		struct nilfs_root *root;
1153c5ca48aaSRyusuke Konishi 
1154783f6184SRyusuke Konishi 		/*
1155783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
1156783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
1157783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
1158783f6184SRyusuke Konishi 		 */
1159c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
1160c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1161c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
1162c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
1163c5ca48aaSRyusuke Konishi 		if (features) {
1164feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_WARNING,
1165feee880fSRyusuke Konishi 				  "couldn't remount RDWR because of unsupported optional features (%llx)",
1166feee880fSRyusuke Konishi 				  (unsigned long long)features);
1167c5ca48aaSRyusuke Konishi 			err = -EROFS;
1168c5ca48aaSRyusuke Konishi 			goto restore_opts;
1169c5ca48aaSRyusuke Konishi 		}
1170c5ca48aaSRyusuke Konishi 
11711751e8a6SLinus Torvalds 		sb->s_flags &= ~SB_RDONLY;
1172783f6184SRyusuke Konishi 
11732b0143b5SDavid Howells 		root = NILFS_I(d_inode(sb->s_root))->i_root;
1174f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, root);
1175783f6184SRyusuke Konishi 		if (err)
1176e59399d0SRyusuke Konishi 			goto restore_opts;
1177783f6184SRyusuke Konishi 
1178783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1179f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1180783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1181783f6184SRyusuke Konishi 	}
1182783f6184SRyusuke Konishi  out:
1183783f6184SRyusuke Konishi 	return 0;
1184783f6184SRyusuke Konishi 
1185783f6184SRyusuke Konishi  restore_opts:
1186783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
11873b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt = old_mount_opt;
1188783f6184SRyusuke Konishi 	return err;
1189783f6184SRyusuke Konishi }
1190783f6184SRyusuke Konishi 
1191783f6184SRyusuke Konishi struct nilfs_super_data {
1192783f6184SRyusuke Konishi 	struct block_device *bdev;
1193783f6184SRyusuke Konishi 	__u64 cno;
1194783f6184SRyusuke Konishi 	int flags;
1195783f6184SRyusuke Konishi };
1196783f6184SRyusuke Konishi 
1197a7d3f104SRyusuke Konishi static int nilfs_parse_snapshot_option(const char *option,
1198a7d3f104SRyusuke Konishi 				       const substring_t *arg,
1199a7d3f104SRyusuke Konishi 				       struct nilfs_super_data *sd)
1200a7d3f104SRyusuke Konishi {
1201a7d3f104SRyusuke Konishi 	unsigned long long val;
1202a7d3f104SRyusuke Konishi 	const char *msg = NULL;
1203a7d3f104SRyusuke Konishi 	int err;
1204a7d3f104SRyusuke Konishi 
12051751e8a6SLinus Torvalds 	if (!(sd->flags & SB_RDONLY)) {
1206a7d3f104SRyusuke Konishi 		msg = "read-only option is not specified";
1207a7d3f104SRyusuke Konishi 		goto parse_error;
1208a7d3f104SRyusuke Konishi 	}
1209a7d3f104SRyusuke Konishi 
1210a7d3f104SRyusuke Konishi 	err = kstrtoull(arg->from, 0, &val);
1211a7d3f104SRyusuke Konishi 	if (err) {
1212a7d3f104SRyusuke Konishi 		if (err == -ERANGE)
1213a7d3f104SRyusuke Konishi 			msg = "too large checkpoint number";
1214a7d3f104SRyusuke Konishi 		else
1215a7d3f104SRyusuke Konishi 			msg = "malformed argument";
1216a7d3f104SRyusuke Konishi 		goto parse_error;
1217a7d3f104SRyusuke Konishi 	} else if (val == 0) {
1218a7d3f104SRyusuke Konishi 		msg = "invalid checkpoint number 0";
1219a7d3f104SRyusuke Konishi 		goto parse_error;
1220a7d3f104SRyusuke Konishi 	}
1221a7d3f104SRyusuke Konishi 	sd->cno = val;
1222a7d3f104SRyusuke Konishi 	return 0;
1223a7d3f104SRyusuke Konishi 
1224a7d3f104SRyusuke Konishi parse_error:
1225a7d3f104SRyusuke Konishi 	nilfs_msg(NULL, KERN_ERR, "invalid option \"%s\": %s", option, msg);
1226a7d3f104SRyusuke Konishi 	return 1;
1227a7d3f104SRyusuke Konishi }
1228a7d3f104SRyusuke Konishi 
1229783f6184SRyusuke Konishi /**
1230783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1231783f6184SRyusuke Konishi  * @data: mount options
1232783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1233783f6184SRyusuke Konishi  */
1234783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1235783f6184SRyusuke Konishi {
1236783f6184SRyusuke Konishi 	char *p, *options = data;
1237783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1238c05dbfc2SRyusuke Konishi 	int token;
1239783f6184SRyusuke Konishi 	int ret = 0;
1240783f6184SRyusuke Konishi 
1241783f6184SRyusuke Konishi 	do {
1242783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1243783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1244783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1245a7d3f104SRyusuke Konishi 			if (token == Opt_snapshot)
1246a7d3f104SRyusuke Konishi 				ret = nilfs_parse_snapshot_option(p, &args[0],
1247a7d3f104SRyusuke Konishi 								  sd);
1248783f6184SRyusuke Konishi 		}
1249783f6184SRyusuke Konishi 		if (!options)
1250783f6184SRyusuke Konishi 			break;
1251783f6184SRyusuke Konishi 		BUG_ON(options == data);
1252783f6184SRyusuke Konishi 		*(options - 1) = ',';
1253783f6184SRyusuke Konishi 	} while (!ret);
1254783f6184SRyusuke Konishi 	return ret;
1255783f6184SRyusuke Konishi }
1256783f6184SRyusuke Konishi 
1257783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1258783f6184SRyusuke Konishi {
1259f11459adSRyusuke Konishi 	s->s_bdev = data;
1260783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1261783f6184SRyusuke Konishi 	return 0;
1262783f6184SRyusuke Konishi }
1263783f6184SRyusuke Konishi 
1264783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1265783f6184SRyusuke Konishi {
1266f11459adSRyusuke Konishi 	return (void *)s->s_bdev == data;
1267783f6184SRyusuke Konishi }
1268783f6184SRyusuke Konishi 
1269e4c59d61SAl Viro static struct dentry *
1270e4c59d61SAl Viro nilfs_mount(struct file_system_type *fs_type, int flags,
1271e4c59d61SAl Viro 	     const char *dev_name, void *data)
1272783f6184SRyusuke Konishi {
1273783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
127433c8e57cSRyusuke Konishi 	struct super_block *s;
1275d4d77629STejun Heo 	fmode_t mode = FMODE_READ | FMODE_EXCL;
1276f11459adSRyusuke Konishi 	struct dentry *root_dentry;
1277f11459adSRyusuke Konishi 	int err, s_new = false;
1278783f6184SRyusuke Konishi 
12791751e8a6SLinus Torvalds 	if (!(flags & SB_RDONLY))
128013e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
128113e90559SRyusuke Konishi 
1282d4d77629STejun Heo 	sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1283d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1284e4c59d61SAl Viro 		return ERR_CAST(sd.bdev);
1285783f6184SRyusuke Konishi 
1286783f6184SRyusuke Konishi 	sd.cno = 0;
1287783f6184SRyusuke Konishi 	sd.flags = flags;
1288783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1289783f6184SRyusuke Konishi 		err = -EINVAL;
1290783f6184SRyusuke Konishi 		goto failed;
1291783f6184SRyusuke Konishi 	}
1292783f6184SRyusuke Konishi 
12935beb6e0bSRyusuke Konishi 	/*
12945beb6e0bSRyusuke Konishi 	 * once the super is inserted into the list by sget, s_umount
12955beb6e0bSRyusuke Konishi 	 * will protect the lockfs code from trying to start a snapshot
12965beb6e0bSRyusuke Konishi 	 * while we are mounting
12975beb6e0bSRyusuke Konishi 	 */
12985beb6e0bSRyusuke Konishi 	mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
12995beb6e0bSRyusuke Konishi 	if (sd.bdev->bd_fsfreeze_count > 0) {
13005beb6e0bSRyusuke Konishi 		mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
13015beb6e0bSRyusuke Konishi 		err = -EBUSY;
130233c8e57cSRyusuke Konishi 		goto failed;
130333c8e57cSRyusuke Konishi 	}
13049249e17fSDavid Howells 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
13059249e17fSDavid Howells 		 sd.bdev);
13065beb6e0bSRyusuke Konishi 	mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
130733c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
130833c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
1309348fe8daSRyusuke Konishi 		goto failed;
1310783f6184SRyusuke Konishi 	}
1311783f6184SRyusuke Konishi 
1312783f6184SRyusuke Konishi 	if (!s->s_root) {
1313f11459adSRyusuke Konishi 		s_new = true;
1314f11459adSRyusuke Konishi 
131533c8e57cSRyusuke Konishi 		/* New superblock instance created */
13164571b82cSRyusuke Konishi 		s->s_mode = mode;
1317a1c6f057SDmitry Monakhov 		snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
1318783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1319783f6184SRyusuke Konishi 
13201751e8a6SLinus Torvalds 		err = nilfs_fill_super(s, data, flags & SB_SILENT ? 1 : 0);
1321783f6184SRyusuke Konishi 		if (err)
1322348fe8daSRyusuke Konishi 			goto failed_super;
1323783f6184SRyusuke Konishi 
13241751e8a6SLinus Torvalds 		s->s_flags |= SB_ACTIVE;
1325f11459adSRyusuke Konishi 	} else if (!sd.cno) {
1326e95c311eSAl Viro 		if (nilfs_tree_is_busy(s->s_root)) {
13271751e8a6SLinus Torvalds 			if ((flags ^ s->s_flags) & SB_RDONLY) {
1328feee880fSRyusuke Konishi 				nilfs_msg(s, KERN_ERR,
1329feee880fSRyusuke Konishi 					  "the device already has a %s mount.",
1330bc98a42cSDavid Howells 					  sb_rdonly(s) ? "read-only" : "read/write");
1331f11459adSRyusuke Konishi 				err = -EBUSY;
1332f11459adSRyusuke Konishi 				goto failed_super;
1333f11459adSRyusuke Konishi 			}
1334e95c311eSAl Viro 		} else {
1335f11459adSRyusuke Konishi 			/*
1336f11459adSRyusuke Konishi 			 * Try remount to setup mount states if the current
1337f11459adSRyusuke Konishi 			 * tree is not mounted and only snapshots use this sb.
1338f11459adSRyusuke Konishi 			 */
1339f11459adSRyusuke Konishi 			err = nilfs_remount(s, &flags, data);
1340f11459adSRyusuke Konishi 			if (err)
1341f11459adSRyusuke Konishi 				goto failed_super;
1342f11459adSRyusuke Konishi 		}
1343783f6184SRyusuke Konishi 	}
1344783f6184SRyusuke Konishi 
1345f11459adSRyusuke Konishi 	if (sd.cno) {
1346f11459adSRyusuke Konishi 		err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1347348fe8daSRyusuke Konishi 		if (err)
1348f11459adSRyusuke Konishi 			goto failed_super;
1349f11459adSRyusuke Konishi 	} else {
1350f11459adSRyusuke Konishi 		root_dentry = dget(s->s_root);
1351783f6184SRyusuke Konishi 	}
1352783f6184SRyusuke Konishi 
1353f11459adSRyusuke Konishi 	if (!s_new)
1354d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1355f11459adSRyusuke Konishi 
1356e4c59d61SAl Viro 	return root_dentry;
1357783f6184SRyusuke Konishi 
1358f11459adSRyusuke Konishi  failed_super:
1359a95161aaSAl Viro 	deactivate_locked_super(s);
1360783f6184SRyusuke Konishi 
1361348fe8daSRyusuke Konishi  failed:
1362348fe8daSRyusuke Konishi 	if (!s_new)
1363d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1364e4c59d61SAl Viro 	return ERR_PTR(err);
1365783f6184SRyusuke Konishi }
1366783f6184SRyusuke Konishi 
1367783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1368783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1369783f6184SRyusuke Konishi 	.name     = "nilfs2",
1370e4c59d61SAl Viro 	.mount    = nilfs_mount,
1371783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1372783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1373783f6184SRyusuke Konishi };
13747f78e035SEric W. Biederman MODULE_ALIAS_FS("nilfs2");
1375783f6184SRyusuke Konishi 
137641c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
137741c88bd7SLi Hong {
137841c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
137941c88bd7SLi Hong 
138041c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
138141c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
138241c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
138341c88bd7SLi Hong #endif
13842aa15890SMiklos Szeredi 	address_space_init_once(&ii->i_btnode_cache);
138505d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
138641c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
138741c88bd7SLi Hong }
138841c88bd7SLi Hong 
138941c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
139041c88bd7SLi Hong {
139141c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
139241c88bd7SLi Hong }
139341c88bd7SLi Hong 
139441c88bd7SLi Hong static void nilfs_destroy_cachep(void)
139541c88bd7SLi Hong {
13968c0a8537SKirill A. Shutemov 	/*
13978c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
13988c0a8537SKirill A. Shutemov 	 * destroy cache.
13998c0a8537SKirill A. Shutemov 	 */
14008c0a8537SKirill A. Shutemov 	rcu_barrier();
14018c0a8537SKirill A. Shutemov 
140241c88bd7SLi Hong 	kmem_cache_destroy(nilfs_inode_cachep);
140341c88bd7SLi Hong 	kmem_cache_destroy(nilfs_transaction_cachep);
140441c88bd7SLi Hong 	kmem_cache_destroy(nilfs_segbuf_cachep);
140541c88bd7SLi Hong 	kmem_cache_destroy(nilfs_btree_path_cache);
140641c88bd7SLi Hong }
140741c88bd7SLi Hong 
140841c88bd7SLi Hong static int __init nilfs_init_cachep(void)
140941c88bd7SLi Hong {
141041c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
141141c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
14125d097056SVladimir Davydov 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
14135d097056SVladimir Davydov 			nilfs_inode_init_once);
141441c88bd7SLi Hong 	if (!nilfs_inode_cachep)
141541c88bd7SLi Hong 		goto fail;
141641c88bd7SLi Hong 
141741c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
141841c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
141941c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
142041c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
142141c88bd7SLi Hong 		goto fail;
142241c88bd7SLi Hong 
142341c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
142441c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
142541c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
142641c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
142741c88bd7SLi Hong 		goto fail;
142841c88bd7SLi Hong 
142941c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
143041c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
143141c88bd7SLi Hong 			0, 0, NULL);
143241c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
143341c88bd7SLi Hong 		goto fail;
143441c88bd7SLi Hong 
143541c88bd7SLi Hong 	return 0;
143641c88bd7SLi Hong 
143741c88bd7SLi Hong fail:
143841c88bd7SLi Hong 	nilfs_destroy_cachep();
143941c88bd7SLi Hong 	return -ENOMEM;
144041c88bd7SLi Hong }
144141c88bd7SLi Hong 
1442783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1443783f6184SRyusuke Konishi {
1444783f6184SRyusuke Konishi 	int err;
1445783f6184SRyusuke Konishi 
144641c88bd7SLi Hong 	err = nilfs_init_cachep();
1447783f6184SRyusuke Konishi 	if (err)
144841c88bd7SLi Hong 		goto fail;
1449783f6184SRyusuke Konishi 
1450dd70edbdSVyacheslav Dubeyko 	err = nilfs_sysfs_init();
1451783f6184SRyusuke Konishi 	if (err)
145241c88bd7SLi Hong 		goto free_cachep;
1453783f6184SRyusuke Konishi 
1454dd70edbdSVyacheslav Dubeyko 	err = register_filesystem(&nilfs_fs_type);
1455dd70edbdSVyacheslav Dubeyko 	if (err)
1456dd70edbdSVyacheslav Dubeyko 		goto deinit_sysfs_entry;
1457dd70edbdSVyacheslav Dubeyko 
14589f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1459783f6184SRyusuke Konishi 	return 0;
1460783f6184SRyusuke Konishi 
1461dd70edbdSVyacheslav Dubeyko deinit_sysfs_entry:
1462dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
146341c88bd7SLi Hong free_cachep:
146441c88bd7SLi Hong 	nilfs_destroy_cachep();
146541c88bd7SLi Hong fail:
1466783f6184SRyusuke Konishi 	return err;
1467783f6184SRyusuke Konishi }
1468783f6184SRyusuke Konishi 
1469783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1470783f6184SRyusuke Konishi {
147141c88bd7SLi Hong 	nilfs_destroy_cachep();
1472dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
1473783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1474783f6184SRyusuke Konishi }
1475783f6184SRyusuke Konishi 
1476783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1477783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1478