xref: /openbmc/linux/fs/nilfs2/super.c (revision 70fd76140a6cb63262bd47b68d57b42e889c10ee)
1783f6184SRyusuke Konishi /*
2783f6184SRyusuke Konishi  * super.c - NILFS module and super block management.
3783f6184SRyusuke Konishi  *
4783f6184SRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5783f6184SRyusuke Konishi  *
6783f6184SRyusuke Konishi  * This program is free software; you can redistribute it and/or modify
7783f6184SRyusuke Konishi  * it under the terms of the GNU General Public License as published by
8783f6184SRyusuke Konishi  * the Free Software Foundation; either version 2 of the License, or
9783f6184SRyusuke Konishi  * (at your option) any later version.
10783f6184SRyusuke Konishi  *
11783f6184SRyusuke Konishi  * This program is distributed in the hope that it will be useful,
12783f6184SRyusuke Konishi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13783f6184SRyusuke Konishi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14783f6184SRyusuke Konishi  * GNU General Public License for more details.
15783f6184SRyusuke Konishi  *
164b420ab4SRyusuke Konishi  * Written by Ryusuke Konishi.
17783f6184SRyusuke Konishi  */
18783f6184SRyusuke Konishi /*
19783f6184SRyusuke Konishi  *  linux/fs/ext2/super.c
20783f6184SRyusuke Konishi  *
21783f6184SRyusuke Konishi  * Copyright (C) 1992, 1993, 1994, 1995
22783f6184SRyusuke Konishi  * Remy Card (card@masi.ibp.fr)
23783f6184SRyusuke Konishi  * Laboratoire MASI - Institut Blaise Pascal
24783f6184SRyusuke Konishi  * Universite Pierre et Marie Curie (Paris VI)
25783f6184SRyusuke Konishi  *
26783f6184SRyusuke Konishi  *  from
27783f6184SRyusuke Konishi  *
28783f6184SRyusuke Konishi  *  linux/fs/minix/inode.c
29783f6184SRyusuke Konishi  *
30783f6184SRyusuke Konishi  *  Copyright (C) 1991, 1992  Linus Torvalds
31783f6184SRyusuke Konishi  *
32783f6184SRyusuke Konishi  *  Big-endian to little-endian byte-swapping/bitmaps by
33783f6184SRyusuke Konishi  *        David S. Miller (davem@caip.rutgers.edu), 1995
34783f6184SRyusuke Konishi  */
35783f6184SRyusuke Konishi 
36783f6184SRyusuke Konishi #include <linux/module.h>
37783f6184SRyusuke Konishi #include <linux/string.h>
38783f6184SRyusuke Konishi #include <linux/slab.h>
39783f6184SRyusuke Konishi #include <linux/init.h>
40783f6184SRyusuke Konishi #include <linux/blkdev.h>
41783f6184SRyusuke Konishi #include <linux/parser.h>
42783f6184SRyusuke Konishi #include <linux/crc32.h>
43783f6184SRyusuke Konishi #include <linux/vfs.h>
44783f6184SRyusuke Konishi #include <linux/writeback.h>
45b58a285bSJiro SEKIBA #include <linux/seq_file.h>
46b58a285bSJiro SEKIBA #include <linux/mount.h>
47783f6184SRyusuke Konishi #include "nilfs.h"
488e656fd5SRyusuke Konishi #include "export.h"
49783f6184SRyusuke Konishi #include "mdt.h"
50783f6184SRyusuke Konishi #include "alloc.h"
5105d0e94bSRyusuke Konishi #include "btree.h"
5205d0e94bSRyusuke Konishi #include "btnode.h"
53783f6184SRyusuke Konishi #include "page.h"
54783f6184SRyusuke Konishi #include "cpfile.h"
554e33f9eaSRyusuke Konishi #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
56783f6184SRyusuke Konishi #include "ifile.h"
57783f6184SRyusuke Konishi #include "dat.h"
58783f6184SRyusuke Konishi #include "segment.h"
59783f6184SRyusuke Konishi #include "segbuf.h"
60783f6184SRyusuke Konishi 
61783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
62783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
63783f6184SRyusuke Konishi 		   "(NILFS)");
64783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
65783f6184SRyusuke Konishi 
66abc0b50bSJiro SEKIBA static struct kmem_cache *nilfs_inode_cachep;
6741c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
6841c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
6941c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
7041c88bd7SLi Hong 
71f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount);
72783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
73783f6184SRyusuke Konishi 
74a66dfb0aSRyusuke Konishi void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt,
75a66dfb0aSRyusuke Konishi 		 ...)
76a66dfb0aSRyusuke Konishi {
77a66dfb0aSRyusuke Konishi 	struct va_format vaf;
78a66dfb0aSRyusuke Konishi 	va_list args;
79a66dfb0aSRyusuke Konishi 
80a66dfb0aSRyusuke Konishi 	va_start(args, fmt);
81a66dfb0aSRyusuke Konishi 	vaf.fmt = fmt;
82a66dfb0aSRyusuke Konishi 	vaf.va = &args;
83a66dfb0aSRyusuke Konishi 	if (sb)
84a66dfb0aSRyusuke Konishi 		printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf);
85a66dfb0aSRyusuke Konishi 	else
86a66dfb0aSRyusuke Konishi 		printk("%sNILFS: %pV\n", level, &vaf);
87a66dfb0aSRyusuke Konishi 	va_end(args);
88a66dfb0aSRyusuke Konishi }
89a66dfb0aSRyusuke Konishi 
90f7545144SRyusuke Konishi static void nilfs_set_error(struct super_block *sb)
91c8a11c8aSRyusuke Konishi {
92e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
93d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
94c8a11c8aSRyusuke Konishi 
95c8a11c8aSRyusuke Konishi 	down_write(&nilfs->ns_sem);
96c8a11c8aSRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
97c8a11c8aSRyusuke Konishi 		nilfs->ns_mount_state |= NILFS_ERROR_FS;
98f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, 0);
99d26493b6SJiro SEKIBA 		if (likely(sbp)) {
100d26493b6SJiro SEKIBA 			sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
101b2ac86e1SJiro SEKIBA 			if (sbp[1])
102b2ac86e1SJiro SEKIBA 				sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
103f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
104c8a11c8aSRyusuke Konishi 		}
105d26493b6SJiro SEKIBA 	}
106c8a11c8aSRyusuke Konishi 	up_write(&nilfs->ns_sem);
107c8a11c8aSRyusuke Konishi }
108c8a11c8aSRyusuke Konishi 
109783f6184SRyusuke Konishi /**
110cae3d4caSRyusuke Konishi  * __nilfs_error() - report failure condition on a filesystem
111783f6184SRyusuke Konishi  *
112cae3d4caSRyusuke Konishi  * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
113cae3d4caSRyusuke Konishi  * reporting an error message.  This function should be called when
114cae3d4caSRyusuke Konishi  * NILFS detects incoherences or defects of meta data on disk.
115783f6184SRyusuke Konishi  *
116cae3d4caSRyusuke Konishi  * This implements the body of nilfs_error() macro.  Normally,
117cae3d4caSRyusuke Konishi  * nilfs_error() should be used.  As for sustainable errors such as a
118d6517debSRyusuke Konishi  * single-shot I/O error, nilfs_msg() should be used instead.
119cae3d4caSRyusuke Konishi  *
120cae3d4caSRyusuke Konishi  * Callers should not add a trailing newline since this will do it.
121783f6184SRyusuke Konishi  */
122cae3d4caSRyusuke Konishi void __nilfs_error(struct super_block *sb, const char *function,
123783f6184SRyusuke Konishi 		   const char *fmt, ...)
124783f6184SRyusuke Konishi {
125e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
126b004a5ebSJoe Perches 	struct va_format vaf;
127783f6184SRyusuke Konishi 	va_list args;
128783f6184SRyusuke Konishi 
129783f6184SRyusuke Konishi 	va_start(args, fmt);
130b004a5ebSJoe Perches 
131b004a5ebSJoe Perches 	vaf.fmt = fmt;
132b004a5ebSJoe Perches 	vaf.va = &args;
133b004a5ebSJoe Perches 
134b004a5ebSJoe Perches 	printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
135b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
136b004a5ebSJoe Perches 
137783f6184SRyusuke Konishi 	va_end(args);
138783f6184SRyusuke Konishi 
139783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
140f7545144SRyusuke Konishi 		nilfs_set_error(sb);
141783f6184SRyusuke Konishi 
1423b2ce58bSRyusuke Konishi 		if (nilfs_test_opt(nilfs, ERRORS_RO)) {
143783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
144783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
145783f6184SRyusuke Konishi 		}
146783f6184SRyusuke Konishi 	}
147783f6184SRyusuke Konishi 
1483b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
149783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
150783f6184SRyusuke Konishi 		      sb->s_id);
151783f6184SRyusuke Konishi }
152783f6184SRyusuke Konishi 
1532879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
154783f6184SRyusuke Konishi {
155783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
156783f6184SRyusuke Konishi 
157783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
158783f6184SRyusuke Konishi 	if (!ii)
159783f6184SRyusuke Konishi 		return NULL;
160783f6184SRyusuke Konishi 	ii->i_bh = NULL;
161783f6184SRyusuke Konishi 	ii->i_state = 0;
1620e14a359SRyusuke Konishi 	ii->i_cno = 0;
163783f6184SRyusuke Konishi 	ii->vfs_inode.i_version = 1;
164b83ae6d4SChristoph Hellwig 	nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
165783f6184SRyusuke Konishi 	return &ii->vfs_inode;
166783f6184SRyusuke Konishi }
167783f6184SRyusuke Konishi 
168fa0d7e3dSNick Piggin static void nilfs_i_callback(struct rcu_head *head)
169783f6184SRyusuke Konishi {
170fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
171b91c9a97SRyusuke Konishi 
1722d19961dSRyusuke Konishi 	if (nilfs_is_metadata_file_inode(inode))
1732d19961dSRyusuke Konishi 		nilfs_mdt_destroy(inode);
1742d19961dSRyusuke Konishi 
175783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
176783f6184SRyusuke Konishi }
177783f6184SRyusuke Konishi 
178fa0d7e3dSNick Piggin void nilfs_destroy_inode(struct inode *inode)
179fa0d7e3dSNick Piggin {
180fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, nilfs_i_callback);
181fa0d7e3dSNick Piggin }
182fa0d7e3dSNick Piggin 
183f7545144SRyusuke Konishi static int nilfs_sync_super(struct super_block *sb, int flag)
184783f6184SRyusuke Konishi {
185e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
186783f6184SRyusuke Konishi 	int err;
187783f6184SRyusuke Konishi 
188783f6184SRyusuke Konishi  retry:
189e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
1903b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, BARRIER)) {
19187e99511SChristoph Hellwig 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
192*70fd7614SChristoph Hellwig 					  REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
19387e99511SChristoph Hellwig 	} else {
19487e99511SChristoph Hellwig 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
19587e99511SChristoph Hellwig 	}
19687e99511SChristoph Hellwig 
197e339ad31SRyusuke Konishi 	if (unlikely(err)) {
198feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
199feee880fSRyusuke Konishi 			  err);
200e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
201b2ac86e1SJiro SEKIBA 			/*
202b2ac86e1SJiro SEKIBA 			 * sbp[0] points to newer log than sbp[1],
203b2ac86e1SJiro SEKIBA 			 * so copy sbp[0] to sbp[1] to take over sbp[0].
204b2ac86e1SJiro SEKIBA 			 */
205b2ac86e1SJiro SEKIBA 			memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
206b2ac86e1SJiro SEKIBA 			       nilfs->ns_sbsize);
207e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
208e339ad31SRyusuke Konishi 			goto retry;
209e339ad31SRyusuke Konishi 		}
210e339ad31SRyusuke Konishi 	} else {
211e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
212e339ad31SRyusuke Konishi 
213b2ac86e1SJiro SEKIBA 		nilfs->ns_sbwcount++;
214b2ac86e1SJiro SEKIBA 
215e339ad31SRyusuke Konishi 		/*
216e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
217e339ad31SRyusuke Konishi 		 * written in superblock.
218e339ad31SRyusuke Konishi 		 */
219783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
220e339ad31SRyusuke Konishi 
221e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
222e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
223b2ac86e1SJiro SEKIBA 			if (flag == NILFS_SB_COMMIT_ALL) {
224e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
225b2ac86e1SJiro SEKIBA 				if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
226b2ac86e1SJiro SEKIBA 					goto out;
227b2ac86e1SJiro SEKIBA 			}
228b2ac86e1SJiro SEKIBA 			if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
229b2ac86e1SJiro SEKIBA 			    le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
230e339ad31SRyusuke Konishi 				sbp = nilfs->ns_sbp[1];
231e339ad31SRyusuke Konishi 		}
232b2ac86e1SJiro SEKIBA 
2332c2e52fcSRyusuke Konishi 		spin_lock(&nilfs->ns_last_segment_lock);
234e339ad31SRyusuke Konishi 		nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2352c2e52fcSRyusuke Konishi 		spin_unlock(&nilfs->ns_last_segment_lock);
236783f6184SRyusuke Konishi 	}
237b2ac86e1SJiro SEKIBA  out:
238783f6184SRyusuke Konishi 	return err;
239783f6184SRyusuke Konishi }
240783f6184SRyusuke Konishi 
24160f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
24260f46b7eSRyusuke Konishi 			  struct the_nilfs *nilfs)
24360f46b7eSRyusuke Konishi {
24460f46b7eSRyusuke Konishi 	sector_t nfreeblocks;
24560f46b7eSRyusuke Konishi 
24660f46b7eSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
24760f46b7eSRyusuke Konishi 	nilfs_count_free_blocks(nilfs, &nfreeblocks);
24860f46b7eSRyusuke Konishi 	sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
24960f46b7eSRyusuke Konishi 
25060f46b7eSRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
25160f46b7eSRyusuke Konishi 	sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
25260f46b7eSRyusuke Konishi 	sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
25360f46b7eSRyusuke Konishi 	sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
25460f46b7eSRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
25560f46b7eSRyusuke Konishi }
25660f46b7eSRyusuke Konishi 
257f7545144SRyusuke Konishi struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
258b2ac86e1SJiro SEKIBA 					       int flip)
259d26493b6SJiro SEKIBA {
260e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
261d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
262d26493b6SJiro SEKIBA 
263d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
264d26493b6SJiro SEKIBA 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
265d26493b6SJiro SEKIBA 		if (sbp[1] &&
266d26493b6SJiro SEKIBA 		    sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
267b2ac86e1SJiro SEKIBA 			memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
268d26493b6SJiro SEKIBA 		} else {
269feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_CRIT, "superblock broke");
270d26493b6SJiro SEKIBA 			return NULL;
271d26493b6SJiro SEKIBA 		}
272b2ac86e1SJiro SEKIBA 	} else if (sbp[1] &&
273b2ac86e1SJiro SEKIBA 		   sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
274b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
275d26493b6SJiro SEKIBA 	}
276b2ac86e1SJiro SEKIBA 
277b2ac86e1SJiro SEKIBA 	if (flip && sbp[1])
278b2ac86e1SJiro SEKIBA 		nilfs_swap_super_block(nilfs);
279b2ac86e1SJiro SEKIBA 
280d26493b6SJiro SEKIBA 	return sbp;
281d26493b6SJiro SEKIBA }
282d26493b6SJiro SEKIBA 
283f7545144SRyusuke Konishi int nilfs_commit_super(struct super_block *sb, int flag)
284783f6184SRyusuke Konishi {
285e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
286e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
287e339ad31SRyusuke Konishi 	time_t t;
288783f6184SRyusuke Konishi 
289d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
290e339ad31SRyusuke Konishi 	t = get_seconds();
291b2ac86e1SJiro SEKIBA 	nilfs->ns_sbwtime = t;
292e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
293e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
294e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
295e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
296e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
297b2ac86e1SJiro SEKIBA 	if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
298b2ac86e1SJiro SEKIBA 		sbp[1]->s_wtime = sbp[0]->s_wtime;
299b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = 0;
300b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
301b2ac86e1SJiro SEKIBA 					    (unsigned char *)sbp[1],
302b2ac86e1SJiro SEKIBA 					    nilfs->ns_sbsize));
303e339ad31SRyusuke Konishi 	}
304e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
305e2c7617aSAndreas Rohner 	nilfs->ns_flushed_device = 1;
306e2c7617aSAndreas Rohner 	/* make sure store to ns_flushed_device cannot be reordered */
307e2c7617aSAndreas Rohner 	smp_wmb();
308f7545144SRyusuke Konishi 	return nilfs_sync_super(sb, flag);
309783f6184SRyusuke Konishi }
310783f6184SRyusuke Konishi 
3117ecaa46cSRyusuke Konishi /**
3127ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
313f7545144SRyusuke Konishi  * @sb: super block instance to be unmounted or degraded to read-only
3147ecaa46cSRyusuke Konishi  *
3157ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
3167ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3177ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
3187ecaa46cSRyusuke Konishi  */
319f7545144SRyusuke Konishi int nilfs_cleanup_super(struct super_block *sb)
3207ecaa46cSRyusuke Konishi {
321e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
322d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
323b2ac86e1SJiro SEKIBA 	int flag = NILFS_SB_COMMIT;
324d26493b6SJiro SEKIBA 	int ret = -EIO;
3257ecaa46cSRyusuke Konishi 
326f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
327d26493b6SJiro SEKIBA 	if (sbp) {
328f7545144SRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
329f7545144SRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
330b2ac86e1SJiro SEKIBA 		if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
331b2ac86e1SJiro SEKIBA 			/*
332b2ac86e1SJiro SEKIBA 			 * make the "clean" flag also to the opposite
333b2ac86e1SJiro SEKIBA 			 * super block if both super blocks point to
334b2ac86e1SJiro SEKIBA 			 * the same checkpoint.
335b2ac86e1SJiro SEKIBA 			 */
336b2ac86e1SJiro SEKIBA 			sbp[1]->s_state = sbp[0]->s_state;
337b2ac86e1SJiro SEKIBA 			flag = NILFS_SB_COMMIT_ALL;
338b2ac86e1SJiro SEKIBA 		}
339f7545144SRyusuke Konishi 		ret = nilfs_commit_super(sb, flag);
340d26493b6SJiro SEKIBA 	}
3417ecaa46cSRyusuke Konishi 	return ret;
3427ecaa46cSRyusuke Konishi }
3437ecaa46cSRyusuke Konishi 
344cfb0a4bfSRyusuke Konishi /**
345cfb0a4bfSRyusuke Konishi  * nilfs_move_2nd_super - relocate secondary super block
346cfb0a4bfSRyusuke Konishi  * @sb: super block instance
347cfb0a4bfSRyusuke Konishi  * @sb2off: new offset of the secondary super block (in bytes)
348cfb0a4bfSRyusuke Konishi  */
349cfb0a4bfSRyusuke Konishi static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
350cfb0a4bfSRyusuke Konishi {
351cfb0a4bfSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
352cfb0a4bfSRyusuke Konishi 	struct buffer_head *nsbh;
353cfb0a4bfSRyusuke Konishi 	struct nilfs_super_block *nsbp;
354cfb0a4bfSRyusuke Konishi 	sector_t blocknr, newblocknr;
355cfb0a4bfSRyusuke Konishi 	unsigned long offset;
3564f05028fSRyusuke Konishi 	int sb2i;  /* array index of the secondary superblock */
357cfb0a4bfSRyusuke Konishi 	int ret = 0;
358cfb0a4bfSRyusuke Konishi 
359cfb0a4bfSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
360cfb0a4bfSRyusuke Konishi 	if (nilfs->ns_sbh[1] &&
361cfb0a4bfSRyusuke Konishi 	    nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
362cfb0a4bfSRyusuke Konishi 		sb2i = 1;
363cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[1]->b_blocknr;
364cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
365cfb0a4bfSRyusuke Konishi 		sb2i = 0;
366cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[0]->b_blocknr;
3674f05028fSRyusuke Konishi 	} else {
3684f05028fSRyusuke Konishi 		sb2i = -1;
3694f05028fSRyusuke Konishi 		blocknr = 0;
370cfb0a4bfSRyusuke Konishi 	}
371cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
372cfb0a4bfSRyusuke Konishi 		goto out;  /* super block location is unchanged */
373cfb0a4bfSRyusuke Konishi 
374cfb0a4bfSRyusuke Konishi 	/* Get new super block buffer */
375cfb0a4bfSRyusuke Konishi 	newblocknr = sb2off >> nilfs->ns_blocksize_bits;
376cfb0a4bfSRyusuke Konishi 	offset = sb2off & (nilfs->ns_blocksize - 1);
377cfb0a4bfSRyusuke Konishi 	nsbh = sb_getblk(sb, newblocknr);
378cfb0a4bfSRyusuke Konishi 	if (!nsbh) {
379feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
380feee880fSRyusuke Konishi 			  "unable to move secondary superblock to block %llu",
381feee880fSRyusuke Konishi 			  (unsigned long long)newblocknr);
382cfb0a4bfSRyusuke Konishi 		ret = -EIO;
383cfb0a4bfSRyusuke Konishi 		goto out;
384cfb0a4bfSRyusuke Konishi 	}
385cfb0a4bfSRyusuke Konishi 	nsbp = (void *)nsbh->b_data + offset;
386cfb0a4bfSRyusuke Konishi 	memset(nsbp, 0, nilfs->ns_blocksize);
387cfb0a4bfSRyusuke Konishi 
388cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0) {
389cfb0a4bfSRyusuke Konishi 		memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
390cfb0a4bfSRyusuke Konishi 		brelse(nilfs->ns_sbh[sb2i]);
391cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[sb2i] = nsbh;
392cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[sb2i] = nsbp;
393cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
394cfb0a4bfSRyusuke Konishi 		/* secondary super block will be restored to index 1 */
395cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[1] = nsbh;
396cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[1] = nsbp;
397cfb0a4bfSRyusuke Konishi 	} else {
398cfb0a4bfSRyusuke Konishi 		brelse(nsbh);
399cfb0a4bfSRyusuke Konishi 	}
400cfb0a4bfSRyusuke Konishi out:
401cfb0a4bfSRyusuke Konishi 	return ret;
402cfb0a4bfSRyusuke Konishi }
403cfb0a4bfSRyusuke Konishi 
4044e33f9eaSRyusuke Konishi /**
4054e33f9eaSRyusuke Konishi  * nilfs_resize_fs - resize the filesystem
4064e33f9eaSRyusuke Konishi  * @sb: super block instance
4074e33f9eaSRyusuke Konishi  * @newsize: new size of the filesystem (in bytes)
4084e33f9eaSRyusuke Konishi  */
4094e33f9eaSRyusuke Konishi int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
4104e33f9eaSRyusuke Konishi {
4114e33f9eaSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
4124e33f9eaSRyusuke Konishi 	struct nilfs_super_block **sbp;
4134e33f9eaSRyusuke Konishi 	__u64 devsize, newnsegs;
4144e33f9eaSRyusuke Konishi 	loff_t sb2off;
4154e33f9eaSRyusuke Konishi 	int ret;
4164e33f9eaSRyusuke Konishi 
4174e33f9eaSRyusuke Konishi 	ret = -ERANGE;
4184e33f9eaSRyusuke Konishi 	devsize = i_size_read(sb->s_bdev->bd_inode);
4194e33f9eaSRyusuke Konishi 	if (newsize > devsize)
4204e33f9eaSRyusuke Konishi 		goto out;
4214e33f9eaSRyusuke Konishi 
4224e33f9eaSRyusuke Konishi 	/*
4234e33f9eaSRyusuke Konishi 	 * Write lock is required to protect some functions depending
4244e33f9eaSRyusuke Konishi 	 * on the number of segments, the number of reserved segments,
4254e33f9eaSRyusuke Konishi 	 * and so forth.
4264e33f9eaSRyusuke Konishi 	 */
4274e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_segctor_sem);
4284e33f9eaSRyusuke Konishi 
4294e33f9eaSRyusuke Konishi 	sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
4304e33f9eaSRyusuke Konishi 	newnsegs = sb2off >> nilfs->ns_blocksize_bits;
4314e33f9eaSRyusuke Konishi 	do_div(newnsegs, nilfs->ns_blocks_per_segment);
4324e33f9eaSRyusuke Konishi 
4334e33f9eaSRyusuke Konishi 	ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
4344e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_segctor_sem);
4354e33f9eaSRyusuke Konishi 	if (ret < 0)
4364e33f9eaSRyusuke Konishi 		goto out;
4374e33f9eaSRyusuke Konishi 
4384e33f9eaSRyusuke Konishi 	ret = nilfs_construct_segment(sb);
4394e33f9eaSRyusuke Konishi 	if (ret < 0)
4404e33f9eaSRyusuke Konishi 		goto out;
4414e33f9eaSRyusuke Konishi 
4424e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_sem);
4434e33f9eaSRyusuke Konishi 	nilfs_move_2nd_super(sb, sb2off);
4444e33f9eaSRyusuke Konishi 	ret = -EIO;
4454e33f9eaSRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
4464e33f9eaSRyusuke Konishi 	if (likely(sbp)) {
4474e33f9eaSRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
4484e33f9eaSRyusuke Konishi 		/*
4494e33f9eaSRyusuke Konishi 		 * Drop NILFS_RESIZE_FS flag for compatibility with
4504e33f9eaSRyusuke Konishi 		 * mount-time resize which may be implemented in a
4514e33f9eaSRyusuke Konishi 		 * future release.
4524e33f9eaSRyusuke Konishi 		 */
4534e33f9eaSRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
4544e33f9eaSRyusuke Konishi 					      ~NILFS_RESIZE_FS);
4554e33f9eaSRyusuke Konishi 		sbp[0]->s_dev_size = cpu_to_le64(newsize);
4564e33f9eaSRyusuke Konishi 		sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
4574e33f9eaSRyusuke Konishi 		if (sbp[1])
4584e33f9eaSRyusuke Konishi 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
4594e33f9eaSRyusuke Konishi 		ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
4604e33f9eaSRyusuke Konishi 	}
4614e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_sem);
4624e33f9eaSRyusuke Konishi 
4634e33f9eaSRyusuke Konishi 	/*
4644e33f9eaSRyusuke Konishi 	 * Reset the range of allocatable segments last.  This order
4654e33f9eaSRyusuke Konishi 	 * is important in the case of expansion because the secondary
4664e33f9eaSRyusuke Konishi 	 * superblock must be protected from log write until migration
4674e33f9eaSRyusuke Konishi 	 * completes.
4684e33f9eaSRyusuke Konishi 	 */
4694e33f9eaSRyusuke Konishi 	if (!ret)
4704e33f9eaSRyusuke Konishi 		nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
4714e33f9eaSRyusuke Konishi out:
4724e33f9eaSRyusuke Konishi 	return ret;
4734e33f9eaSRyusuke Konishi }
4744e33f9eaSRyusuke Konishi 
475783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
476783f6184SRyusuke Konishi {
477e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
478783f6184SRyusuke Konishi 
479f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
480783f6184SRyusuke Konishi 
481783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
482783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
483f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
484783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
485783f6184SRyusuke Konishi 	}
486783f6184SRyusuke Konishi 
487f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
488f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
489f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
490f1e89c86SRyusuke Konishi 
491348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
492783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
493783f6184SRyusuke Konishi }
494783f6184SRyusuke Konishi 
495783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
496783f6184SRyusuke Konishi {
497e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
498d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
499783f6184SRyusuke Konishi 	int err = 0;
500783f6184SRyusuke Konishi 
501783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
502783f6184SRyusuke Konishi 	if (wait)
503783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
5046233caa9SJiro SEKIBA 
5056233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
506d26493b6SJiro SEKIBA 	if (nilfs_sb_dirty(nilfs)) {
507f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
508b2ac86e1SJiro SEKIBA 		if (likely(sbp)) {
509b2ac86e1SJiro SEKIBA 			nilfs_set_log_cursor(sbp[0], nilfs);
510f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT);
511b2ac86e1SJiro SEKIBA 		}
512d26493b6SJiro SEKIBA 	}
5136233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
5146233caa9SJiro SEKIBA 
515e2c7617aSAndreas Rohner 	if (!err)
516e2c7617aSAndreas Rohner 		err = nilfs_flush_device(nilfs);
517e2c7617aSAndreas Rohner 
518783f6184SRyusuke Konishi 	return err;
519783f6184SRyusuke Konishi }
520783f6184SRyusuke Konishi 
521f7545144SRyusuke Konishi int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
5224d8d9293SRyusuke Konishi 			    struct nilfs_root **rootp)
523783f6184SRyusuke Konishi {
524e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5254d8d9293SRyusuke Konishi 	struct nilfs_root *root;
526783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
527783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
5284d8d9293SRyusuke Konishi 	int err = -ENOMEM;
529783f6184SRyusuke Konishi 
5304d8d9293SRyusuke Konishi 	root = nilfs_find_or_create_root(
5314d8d9293SRyusuke Konishi 		nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
5324d8d9293SRyusuke Konishi 	if (!root)
5334d8d9293SRyusuke Konishi 		return err;
534783f6184SRyusuke Konishi 
535e912a5b6SRyusuke Konishi 	if (root->ifile)
536e912a5b6SRyusuke Konishi 		goto reuse; /* already attached checkpoint */
537783f6184SRyusuke Konishi 
5381154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
539783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
540783f6184SRyusuke Konishi 					  &bh_cp);
5411154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
542783f6184SRyusuke Konishi 	if (unlikely(err)) {
543783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
544feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_ERR,
545feee880fSRyusuke Konishi 				  "Invalid checkpoint (checkpoint number=%llu)",
546783f6184SRyusuke Konishi 				  (unsigned long long)cno);
547783f6184SRyusuke Konishi 			err = -EINVAL;
548783f6184SRyusuke Konishi 		}
549783f6184SRyusuke Konishi 		goto failed;
550783f6184SRyusuke Konishi 	}
551f1e89c86SRyusuke Konishi 
552f7545144SRyusuke Konishi 	err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
553f1e89c86SRyusuke Konishi 			       &raw_cp->cp_ifile_inode, &root->ifile);
554f1e89c86SRyusuke Konishi 	if (err)
555783f6184SRyusuke Konishi 		goto failed_bh;
556b7c06342SRyusuke Konishi 
557e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->inodes_count,
558e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_inodes_count));
559e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->blocks_count,
560e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_blocks_count));
561783f6184SRyusuke Konishi 
562783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
5634d8d9293SRyusuke Konishi 
564e912a5b6SRyusuke Konishi  reuse:
5654d8d9293SRyusuke Konishi 	*rootp = root;
566783f6184SRyusuke Konishi 	return 0;
567783f6184SRyusuke Konishi 
568783f6184SRyusuke Konishi  failed_bh:
569783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
570783f6184SRyusuke Konishi  failed:
5714d8d9293SRyusuke Konishi 	nilfs_put_root(root);
572783f6184SRyusuke Konishi 
573783f6184SRyusuke Konishi 	return err;
574783f6184SRyusuke Konishi }
575783f6184SRyusuke Konishi 
5765beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb)
577783f6184SRyusuke Konishi {
578e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5795beb6e0bSRyusuke Konishi 	int err;
5805beb6e0bSRyusuke Konishi 
5815beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
5825beb6e0bSRyusuke Konishi 		return 0;
5835beb6e0bSRyusuke Konishi 
5845beb6e0bSRyusuke Konishi 	/* Mark super block clean */
5855beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
586f7545144SRyusuke Konishi 	err = nilfs_cleanup_super(sb);
5875beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
5885beb6e0bSRyusuke Konishi 	return err;
5895beb6e0bSRyusuke Konishi }
5905beb6e0bSRyusuke Konishi 
5915beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb)
5925beb6e0bSRyusuke Konishi {
593e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
594783f6184SRyusuke Konishi 
5955beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
5965beb6e0bSRyusuke Konishi 		return 0;
5975beb6e0bSRyusuke Konishi 
5985beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
599f7545144SRyusuke Konishi 	nilfs_setup_super(sb, false);
6005beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
6015beb6e0bSRyusuke Konishi 	return 0;
602783f6184SRyusuke Konishi }
603783f6184SRyusuke Konishi 
604783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
605783f6184SRyusuke Konishi {
606783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
6072b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
608b7c06342SRyusuke Konishi 	struct the_nilfs *nilfs = root->nilfs;
609c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
610783f6184SRyusuke Konishi 	unsigned long long blocks;
611783f6184SRyusuke Konishi 	unsigned long overhead;
612783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
613783f6184SRyusuke Konishi 	sector_t nfreeblocks;
614c7ef972cSVyacheslav Dubeyko 	u64 nmaxinodes, nfreeinodes;
615783f6184SRyusuke Konishi 	int err;
616783f6184SRyusuke Konishi 
617783f6184SRyusuke Konishi 	/*
618783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
619783f6184SRyusuke Konishi 	 *
620783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
621783f6184SRyusuke Konishi 	 * are excluded.
622783f6184SRyusuke Konishi 	 */
623783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
624783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
625783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
626783f6184SRyusuke Konishi 
627783f6184SRyusuke Konishi 	/*
628783f6184SRyusuke Konishi 	 * Compute the overhead
629783f6184SRyusuke Konishi 	 *
6307a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
631783f6184SRyusuke Konishi 	 * We must count them as the overhead.
632783f6184SRyusuke Konishi 	 */
633783f6184SRyusuke Konishi 	overhead = 0;
634783f6184SRyusuke Konishi 
635783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
636783f6184SRyusuke Konishi 	if (unlikely(err))
637783f6184SRyusuke Konishi 		return err;
638783f6184SRyusuke Konishi 
639c7ef972cSVyacheslav Dubeyko 	err = nilfs_ifile_count_free_inodes(root->ifile,
640c7ef972cSVyacheslav Dubeyko 					    &nmaxinodes, &nfreeinodes);
641c7ef972cSVyacheslav Dubeyko 	if (unlikely(err)) {
642feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
643feee880fSRyusuke Konishi 			  "failed to count free inodes: err=%d", err);
644c7ef972cSVyacheslav Dubeyko 		if (err == -ERANGE) {
645c7ef972cSVyacheslav Dubeyko 			/*
646c7ef972cSVyacheslav Dubeyko 			 * If nilfs_palloc_count_max_entries() returns
647c7ef972cSVyacheslav Dubeyko 			 * -ERANGE error code then we simply treat
648c7ef972cSVyacheslav Dubeyko 			 * curent inodes count as maximum possible and
649c7ef972cSVyacheslav Dubeyko 			 * zero as free inodes value.
650c7ef972cSVyacheslav Dubeyko 			 */
651e5f7f848SVyacheslav Dubeyko 			nmaxinodes = atomic64_read(&root->inodes_count);
652c7ef972cSVyacheslav Dubeyko 			nfreeinodes = 0;
653c7ef972cSVyacheslav Dubeyko 			err = 0;
654c7ef972cSVyacheslav Dubeyko 		} else
655c7ef972cSVyacheslav Dubeyko 			return err;
656c7ef972cSVyacheslav Dubeyko 	}
657c7ef972cSVyacheslav Dubeyko 
658783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
659783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
660783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
661783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
662783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
663783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
664c7ef972cSVyacheslav Dubeyko 	buf->f_files = nmaxinodes;
665c7ef972cSVyacheslav Dubeyko 	buf->f_ffree = nfreeinodes;
666783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
667c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
668c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
669c306af23SRyusuke Konishi 
670783f6184SRyusuke Konishi 	return 0;
671783f6184SRyusuke Konishi }
672783f6184SRyusuke Konishi 
67334c80b1dSAl Viro static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
674b58a285bSJiro SEKIBA {
67534c80b1dSAl Viro 	struct super_block *sb = dentry->d_sb;
676e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
6772b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
678b58a285bSJiro SEKIBA 
6793b2ce58bSRyusuke Konishi 	if (!nilfs_test_opt(nilfs, BARRIER))
680c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
681f11459adSRyusuke Konishi 	if (root->cno != NILFS_CPTREE_CURRENT_CNO)
682f11459adSRyusuke Konishi 		seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
6833b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
684c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
6853b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_CONT))
686c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
6873b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, STRICT_ORDER))
688c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
6893b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, NORECOVERY))
690c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
6913b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, DISCARD))
692c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
693b58a285bSJiro SEKIBA 
694b58a285bSJiro SEKIBA 	return 0;
695b58a285bSJiro SEKIBA }
696b58a285bSJiro SEKIBA 
697b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
698783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
699783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
700783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
7016fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
702783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
703783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
7045beb6e0bSRyusuke Konishi 	.freeze_fs	= nilfs_freeze,
7055beb6e0bSRyusuke Konishi 	.unfreeze_fs	= nilfs_unfreeze,
706783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
707783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
708b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
709783f6184SRyusuke Konishi };
710783f6184SRyusuke Konishi 
711783f6184SRyusuke Konishi enum {
712783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
713773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
714802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, Opt_err,
715783f6184SRyusuke Konishi };
716783f6184SRyusuke Konishi 
717783f6184SRyusuke Konishi static match_table_t tokens = {
718783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
719783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
720783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
721773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
72291f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
723783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
724783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
7250234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
726e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
727802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
728783f6184SRyusuke Konishi 	{Opt_err, NULL}
729783f6184SRyusuke Konishi };
730783f6184SRyusuke Konishi 
7317c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
732783f6184SRyusuke Konishi {
733e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
734783f6184SRyusuke Konishi 	char *p;
735783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
736783f6184SRyusuke Konishi 
737783f6184SRyusuke Konishi 	if (!options)
738783f6184SRyusuke Konishi 		return 1;
739783f6184SRyusuke Konishi 
740783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
741783f6184SRyusuke Konishi 		int token;
7424ad364caSRyusuke Konishi 
743783f6184SRyusuke Konishi 		if (!*p)
744783f6184SRyusuke Konishi 			continue;
745783f6184SRyusuke Konishi 
746783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
747783f6184SRyusuke Konishi 		switch (token) {
748773bc4f3SRyusuke Konishi 		case Opt_barrier:
7493b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, BARRIER);
750773bc4f3SRyusuke Konishi 			break;
75191f1953bSJiro SEKIBA 		case Opt_nobarrier:
7523b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, BARRIER);
753783f6184SRyusuke Konishi 			break;
754783f6184SRyusuke Konishi 		case Opt_order:
755783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
756783f6184SRyusuke Konishi 				/* Ordered data semantics */
7573b2ce58bSRyusuke Konishi 				nilfs_clear_opt(nilfs, STRICT_ORDER);
758783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
759783f6184SRyusuke Konishi 				/* Strict in-order semantics */
7603b2ce58bSRyusuke Konishi 				nilfs_set_opt(nilfs, STRICT_ORDER);
761783f6184SRyusuke Konishi 			else
762783f6184SRyusuke Konishi 				return 0;
763783f6184SRyusuke Konishi 			break;
764783f6184SRyusuke Konishi 		case Opt_err_panic:
7653b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
766783f6184SRyusuke Konishi 			break;
767783f6184SRyusuke Konishi 		case Opt_err_ro:
7683b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
769783f6184SRyusuke Konishi 			break;
770783f6184SRyusuke Konishi 		case Opt_err_cont:
7713b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
772783f6184SRyusuke Konishi 			break;
773783f6184SRyusuke Konishi 		case Opt_snapshot:
7747c017457SRyusuke Konishi 			if (is_remount) {
775feee880fSRyusuke Konishi 				nilfs_msg(sb, KERN_ERR,
776feee880fSRyusuke Konishi 					  "\"%s\" option is invalid for remount",
777feee880fSRyusuke Konishi 					  p);
7787c017457SRyusuke Konishi 				return 0;
7797c017457SRyusuke Konishi 			}
7807c017457SRyusuke Konishi 			break;
7810234576dSRyusuke Konishi 		case Opt_norecovery:
7823b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, NORECOVERY);
7830234576dSRyusuke Konishi 			break;
784e902ec99SJiro SEKIBA 		case Opt_discard:
7853b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, DISCARD);
786e902ec99SJiro SEKIBA 			break;
787802d3177SRyusuke Konishi 		case Opt_nodiscard:
7883b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, DISCARD);
789802d3177SRyusuke Konishi 			break;
790783f6184SRyusuke Konishi 		default:
791feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_ERR,
792feee880fSRyusuke Konishi 				  "unrecognized mount option \"%s\"", p);
793783f6184SRyusuke Konishi 			return 0;
794783f6184SRyusuke Konishi 		}
795783f6184SRyusuke Konishi 	}
796783f6184SRyusuke Konishi 	return 1;
797783f6184SRyusuke Konishi }
798783f6184SRyusuke Konishi 
799783f6184SRyusuke Konishi static inline void
8003b2ce58bSRyusuke Konishi nilfs_set_default_options(struct super_block *sb,
801783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
802783f6184SRyusuke Konishi {
803e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
8043b2ce58bSRyusuke Konishi 
8053b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt =
806277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
807783f6184SRyusuke Konishi }
808783f6184SRyusuke Konishi 
809f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount)
810783f6184SRyusuke Konishi {
811e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
812d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
813d26493b6SJiro SEKIBA 	int max_mnt_count;
814d26493b6SJiro SEKIBA 	int mnt_count;
815783f6184SRyusuke Konishi 
816d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
817f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
818d26493b6SJiro SEKIBA 	if (!sbp)
819d26493b6SJiro SEKIBA 		return -EIO;
820d26493b6SJiro SEKIBA 
8215beb6e0bSRyusuke Konishi 	if (!is_mount)
8225beb6e0bSRyusuke Konishi 		goto skip_mount_setup;
8235beb6e0bSRyusuke Konishi 
824d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
825d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
826d26493b6SJiro SEKIBA 
827f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
828feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
829783f6184SRyusuke Konishi #if 0
830783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
831feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
832783f6184SRyusuke Konishi #endif
833783f6184SRyusuke Konishi 	}
834783f6184SRyusuke Konishi 	if (!max_mnt_count)
835d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
836783f6184SRyusuke Konishi 
837d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
8385beb6e0bSRyusuke Konishi 	sbp[0]->s_mtime = cpu_to_le64(get_seconds());
8395beb6e0bSRyusuke Konishi 
8405beb6e0bSRyusuke Konishi skip_mount_setup:
841d26493b6SJiro SEKIBA 	sbp[0]->s_state =
842d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
843b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
8440ca7a5b9SRyusuke Konishi 	if (sbp[1])
845b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
846f7545144SRyusuke Konishi 	return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
847783f6184SRyusuke Konishi }
848783f6184SRyusuke Konishi 
849e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
850e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
851e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
852783f6184SRyusuke Konishi {
853e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
854e339ad31SRyusuke Konishi 	unsigned long offset;
855783f6184SRyusuke Konishi 
856e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
857783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
858e339ad31SRyusuke Konishi 	if (!*pbh)
859783f6184SRyusuke Konishi 		return NULL;
860783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
861783f6184SRyusuke Konishi }
862783f6184SRyusuke Konishi 
863783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
864783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
865783f6184SRyusuke Konishi 				 char *data)
866783f6184SRyusuke Konishi {
867e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
868783f6184SRyusuke Konishi 
869783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
870783f6184SRyusuke Konishi 
871783f6184SRyusuke Konishi 	/* FS independent flags */
872783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
873783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
874783f6184SRyusuke Konishi #endif
875783f6184SRyusuke Konishi 
8763b2ce58bSRyusuke Konishi 	nilfs_set_default_options(sb, sbp);
877783f6184SRyusuke Konishi 
878574e6c31SRyusuke Konishi 	nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
879574e6c31SRyusuke Konishi 	nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
880574e6c31SRyusuke Konishi 	nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
881574e6c31SRyusuke Konishi 	nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
882783f6184SRyusuke Konishi 
8837c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0;
884783f6184SRyusuke Konishi }
885783f6184SRyusuke Konishi 
886c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
887c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
888c5ca48aaSRyusuke Konishi {
889c5ca48aaSRyusuke Konishi 	__u64 features;
890c5ca48aaSRyusuke Konishi 
891c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
892c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
893c5ca48aaSRyusuke Konishi 	if (features) {
894feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
895feee880fSRyusuke Konishi 			  "couldn't mount because of unsupported optional features (%llx)",
896c5ca48aaSRyusuke Konishi 			  (unsigned long long)features);
897c5ca48aaSRyusuke Konishi 		return -EINVAL;
898c5ca48aaSRyusuke Konishi 	}
899c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
900c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
901c5ca48aaSRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY) && features) {
902feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
903feee880fSRyusuke Konishi 			  "couldn't mount RDWR because of unsupported optional features (%llx)",
904c5ca48aaSRyusuke Konishi 			  (unsigned long long)features);
905c5ca48aaSRyusuke Konishi 		return -EINVAL;
906c5ca48aaSRyusuke Konishi 	}
907c5ca48aaSRyusuke Konishi 	return 0;
908c5ca48aaSRyusuke Konishi }
909c5ca48aaSRyusuke Konishi 
910367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
911367ea334SRyusuke Konishi 				 struct nilfs_root *root,
912367ea334SRyusuke Konishi 				 struct dentry **root_dentry)
913367ea334SRyusuke Konishi {
914367ea334SRyusuke Konishi 	struct inode *inode;
915367ea334SRyusuke Konishi 	struct dentry *dentry;
916367ea334SRyusuke Konishi 	int ret = 0;
917367ea334SRyusuke Konishi 
918367ea334SRyusuke Konishi 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
919367ea334SRyusuke Konishi 	if (IS_ERR(inode)) {
920367ea334SRyusuke Konishi 		ret = PTR_ERR(inode);
921feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
922367ea334SRyusuke Konishi 		goto out;
923367ea334SRyusuke Konishi 	}
924367ea334SRyusuke Konishi 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
925367ea334SRyusuke Konishi 		iput(inode);
926feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR, "corrupt root inode");
927367ea334SRyusuke Konishi 		ret = -EINVAL;
928367ea334SRyusuke Konishi 		goto out;
929367ea334SRyusuke Konishi 	}
930367ea334SRyusuke Konishi 
931f11459adSRyusuke Konishi 	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
932f11459adSRyusuke Konishi 		dentry = d_find_alias(inode);
933f11459adSRyusuke Konishi 		if (!dentry) {
93448fde701SAl Viro 			dentry = d_make_root(inode);
935367ea334SRyusuke Konishi 			if (!dentry) {
936367ea334SRyusuke Konishi 				ret = -ENOMEM;
937f11459adSRyusuke Konishi 				goto failed_dentry;
938f11459adSRyusuke Konishi 			}
939f11459adSRyusuke Konishi 		} else {
940f11459adSRyusuke Konishi 			iput(inode);
941f11459adSRyusuke Konishi 		}
942f11459adSRyusuke Konishi 	} else {
9431a0a397eSJ. Bruce Fields 		dentry = d_obtain_root(inode);
944f11459adSRyusuke Konishi 		if (IS_ERR(dentry)) {
945f11459adSRyusuke Konishi 			ret = PTR_ERR(dentry);
946f11459adSRyusuke Konishi 			goto failed_dentry;
947f11459adSRyusuke Konishi 		}
948367ea334SRyusuke Konishi 	}
949367ea334SRyusuke Konishi 	*root_dentry = dentry;
950367ea334SRyusuke Konishi  out:
951367ea334SRyusuke Konishi 	return ret;
952f11459adSRyusuke Konishi 
953f11459adSRyusuke Konishi  failed_dentry:
954feee880fSRyusuke Konishi 	nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
955f11459adSRyusuke Konishi 	goto out;
956367ea334SRyusuke Konishi }
957367ea334SRyusuke Konishi 
958ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
959ab4d8f7eSRyusuke Konishi 				 struct dentry **root_dentry)
960ab4d8f7eSRyusuke Konishi {
961e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = s->s_fs_info;
962ab4d8f7eSRyusuke Konishi 	struct nilfs_root *root;
963ab4d8f7eSRyusuke Konishi 	int ret;
964ab4d8f7eSRyusuke Konishi 
965572d8b39SRyusuke Konishi 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
966572d8b39SRyusuke Konishi 
967ab4d8f7eSRyusuke Konishi 	down_read(&nilfs->ns_segctor_sem);
968ab4d8f7eSRyusuke Konishi 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
969ab4d8f7eSRyusuke Konishi 	up_read(&nilfs->ns_segctor_sem);
970ab4d8f7eSRyusuke Konishi 	if (ret < 0) {
971ab4d8f7eSRyusuke Konishi 		ret = (ret == -ENOENT) ? -EINVAL : ret;
972ab4d8f7eSRyusuke Konishi 		goto out;
973ab4d8f7eSRyusuke Konishi 	} else if (!ret) {
974feee880fSRyusuke Konishi 		nilfs_msg(s, KERN_ERR,
975feee880fSRyusuke Konishi 			  "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
976ab4d8f7eSRyusuke Konishi 			  (unsigned long long)cno);
977ab4d8f7eSRyusuke Konishi 		ret = -EINVAL;
978ab4d8f7eSRyusuke Konishi 		goto out;
979ab4d8f7eSRyusuke Konishi 	}
980ab4d8f7eSRyusuke Konishi 
981f7545144SRyusuke Konishi 	ret = nilfs_attach_checkpoint(s, cno, false, &root);
982ab4d8f7eSRyusuke Konishi 	if (ret) {
983feee880fSRyusuke Konishi 		nilfs_msg(s, KERN_ERR,
984feee880fSRyusuke Konishi 			  "error %d while loading snapshot (checkpoint number=%llu)",
985feee880fSRyusuke Konishi 			  ret, (unsigned long long)cno);
986ab4d8f7eSRyusuke Konishi 		goto out;
987ab4d8f7eSRyusuke Konishi 	}
988ab4d8f7eSRyusuke Konishi 	ret = nilfs_get_root_dentry(s, root, root_dentry);
989ab4d8f7eSRyusuke Konishi 	nilfs_put_root(root);
990ab4d8f7eSRyusuke Konishi  out:
991572d8b39SRyusuke Konishi 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
992ab4d8f7eSRyusuke Konishi 	return ret;
993ab4d8f7eSRyusuke Konishi }
994ab4d8f7eSRyusuke Konishi 
995f11459adSRyusuke Konishi /**
996e95c311eSAl Viro  * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
997f11459adSRyusuke Konishi  * @root_dentry: root dentry of the tree to be shrunk
998f11459adSRyusuke Konishi  *
999f11459adSRyusuke Konishi  * This function returns true if the tree was in-use.
1000f11459adSRyusuke Konishi  */
1001e95c311eSAl Viro static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1002f11459adSRyusuke Konishi {
1003f11459adSRyusuke Konishi 	shrink_dcache_parent(root_dentry);
1004e95c311eSAl Viro 	return d_count(root_dentry) > 1;
1005f11459adSRyusuke Konishi }
1006f11459adSRyusuke Konishi 
1007032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1008032dbb3bSRyusuke Konishi {
1009e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1010032dbb3bSRyusuke Konishi 	struct nilfs_root *root;
1011032dbb3bSRyusuke Konishi 	struct inode *inode;
1012032dbb3bSRyusuke Konishi 	struct dentry *dentry;
1013032dbb3bSRyusuke Konishi 	int ret;
1014032dbb3bSRyusuke Konishi 
10153377f843SRyusuke Konishi 	if (cno > nilfs->ns_cno)
1016032dbb3bSRyusuke Konishi 		return false;
1017032dbb3bSRyusuke Konishi 
1018032dbb3bSRyusuke Konishi 	if (cno >= nilfs_last_cno(nilfs))
1019032dbb3bSRyusuke Konishi 		return true;	/* protect recent checkpoints */
1020032dbb3bSRyusuke Konishi 
1021032dbb3bSRyusuke Konishi 	ret = false;
1022e3154e97SRyusuke Konishi 	root = nilfs_lookup_root(nilfs, cno);
1023032dbb3bSRyusuke Konishi 	if (root) {
1024032dbb3bSRyusuke Konishi 		inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1025032dbb3bSRyusuke Konishi 		if (inode) {
1026032dbb3bSRyusuke Konishi 			dentry = d_find_alias(inode);
1027032dbb3bSRyusuke Konishi 			if (dentry) {
1028e95c311eSAl Viro 				ret = nilfs_tree_is_busy(dentry);
1029032dbb3bSRyusuke Konishi 				dput(dentry);
1030032dbb3bSRyusuke Konishi 			}
1031032dbb3bSRyusuke Konishi 			iput(inode);
1032032dbb3bSRyusuke Konishi 		}
1033032dbb3bSRyusuke Konishi 		nilfs_put_root(root);
1034032dbb3bSRyusuke Konishi 	}
1035032dbb3bSRyusuke Konishi 	return ret;
1036032dbb3bSRyusuke Konishi }
1037032dbb3bSRyusuke Konishi 
1038783f6184SRyusuke Konishi /**
1039783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
1040783f6184SRyusuke Konishi  * @sb: super_block
1041783f6184SRyusuke Konishi  * @data: mount options
1042783f6184SRyusuke Konishi  * @silent: silent mode flag
1043783f6184SRyusuke Konishi  *
1044aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
1045783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
1046783f6184SRyusuke Konishi  */
1047783f6184SRyusuke Konishi static int
1048348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent)
1049783f6184SRyusuke Konishi {
1050348fe8daSRyusuke Konishi 	struct the_nilfs *nilfs;
10514d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
1052783f6184SRyusuke Konishi 	__u64 cno;
1053783f6184SRyusuke Konishi 	int err;
1054783f6184SRyusuke Konishi 
10556625689eSRyusuke Konishi 	nilfs = alloc_nilfs(sb);
1056e3154e97SRyusuke Konishi 	if (!nilfs)
1057783f6184SRyusuke Konishi 		return -ENOMEM;
1058783f6184SRyusuke Konishi 
1059e3154e97SRyusuke Konishi 	sb->s_fs_info = nilfs;
1060783f6184SRyusuke Konishi 
1061f7545144SRyusuke Konishi 	err = init_nilfs(nilfs, sb, (char *)data);
1062783f6184SRyusuke Konishi 	if (err)
1063348fe8daSRyusuke Konishi 		goto failed_nilfs;
1064783f6184SRyusuke Konishi 
1065783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
1066783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
1067783f6184SRyusuke Konishi 	sb->s_root = NULL;
106861239230SRyusuke Konishi 	sb->s_time_gran = 1;
10698de52778SAl Viro 	sb->s_max_links = NILFS_LINK_MAX;
1070026a7d63SRyusuke Konishi 
107126ff1304SChristoph Hellwig 	sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
1072783f6184SRyusuke Konishi 
1073f7545144SRyusuke Konishi 	err = load_nilfs(nilfs, sb);
1074783f6184SRyusuke Konishi 	if (err)
1075348fe8daSRyusuke Konishi 		goto failed_nilfs;
1076f50a4c81SRyusuke Konishi 
1077783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
1078f7545144SRyusuke Konishi 	err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1079783f6184SRyusuke Konishi 	if (err) {
1080feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_ERR,
1081feee880fSRyusuke Konishi 			  "error %d while loading last checkpoint (checkpoint number=%llu)",
1082feee880fSRyusuke Konishi 			  err, (unsigned long long)cno);
1083f1e89c86SRyusuke Konishi 		goto failed_unload;
1084783f6184SRyusuke Konishi 	}
1085783f6184SRyusuke Konishi 
1086783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1087f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, fsroot);
1088783f6184SRyusuke Konishi 		if (err)
1089783f6184SRyusuke Konishi 			goto failed_checkpoint;
1090783f6184SRyusuke Konishi 	}
1091783f6184SRyusuke Konishi 
1092367ea334SRyusuke Konishi 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1093367ea334SRyusuke Konishi 	if (err)
1094783f6184SRyusuke Konishi 		goto failed_segctor;
1095783f6184SRyusuke Konishi 
10964d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1097783f6184SRyusuke Konishi 
1098783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1099783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1100f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1101783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1102783f6184SRyusuke Konishi 	}
1103783f6184SRyusuke Konishi 
1104783f6184SRyusuke Konishi 	return 0;
1105783f6184SRyusuke Konishi 
1106783f6184SRyusuke Konishi  failed_segctor:
1107f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
1108783f6184SRyusuke Konishi 
1109783f6184SRyusuke Konishi  failed_checkpoint:
11104d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1111783f6184SRyusuke Konishi 
1112f1e89c86SRyusuke Konishi  failed_unload:
1113f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
1114f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
1115f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
1116f1e89c86SRyusuke Konishi 
1117348fe8daSRyusuke Konishi  failed_nilfs:
1118348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
1119783f6184SRyusuke Konishi 	return err;
1120783f6184SRyusuke Konishi }
1121783f6184SRyusuke Konishi 
1122783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1123783f6184SRyusuke Konishi {
1124e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1125783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
112606df0f99SRyusuke Konishi 	unsigned long old_mount_opt;
1127f11459adSRyusuke Konishi 	int err;
1128783f6184SRyusuke Konishi 
112902b9984dSTheodore Ts'o 	sync_filesystem(sb);
1130783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
11313b2ce58bSRyusuke Konishi 	old_mount_opt = nilfs->ns_mount_opt;
1132783f6184SRyusuke Konishi 
11337c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
1134783f6184SRyusuke Konishi 		err = -EINVAL;
1135783f6184SRyusuke Konishi 		goto restore_opts;
1136783f6184SRyusuke Konishi 	}
1137783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1138783f6184SRyusuke Konishi 
1139d240e067SRyusuke Konishi 	err = -EINVAL;
1140783f6184SRyusuke Konishi 
11410234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
1142feee880fSRyusuke Konishi 		nilfs_msg(sb, KERN_WARNING,
1143feee880fSRyusuke Konishi 			  "couldn't remount because the filesystem is in an incomplete recovery state");
11440234576dSRyusuke Konishi 		goto restore_opts;
11450234576dSRyusuke Konishi 	}
11460234576dSRyusuke Konishi 
1147783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1148783f6184SRyusuke Konishi 		goto out;
1149783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
1150f7545144SRyusuke Konishi 		/* Shutting down log writer */
1151f7545144SRyusuke Konishi 		nilfs_detach_log_writer(sb);
1152783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
1153783f6184SRyusuke Konishi 
1154783f6184SRyusuke Konishi 		/*
1155783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
1156783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
1157783f6184SRyusuke Konishi 		 */
1158783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1159f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
1160783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1161783f6184SRyusuke Konishi 	} else {
1162c5ca48aaSRyusuke Konishi 		__u64 features;
1163e912a5b6SRyusuke Konishi 		struct nilfs_root *root;
1164c5ca48aaSRyusuke Konishi 
1165783f6184SRyusuke Konishi 		/*
1166783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
1167783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
1168783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
1169783f6184SRyusuke Konishi 		 */
1170c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
1171c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1172c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
1173c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
1174c5ca48aaSRyusuke Konishi 		if (features) {
1175feee880fSRyusuke Konishi 			nilfs_msg(sb, KERN_WARNING,
1176feee880fSRyusuke Konishi 				  "couldn't remount RDWR because of unsupported optional features (%llx)",
1177feee880fSRyusuke Konishi 				  (unsigned long long)features);
1178c5ca48aaSRyusuke Konishi 			err = -EROFS;
1179c5ca48aaSRyusuke Konishi 			goto restore_opts;
1180c5ca48aaSRyusuke Konishi 		}
1181c5ca48aaSRyusuke Konishi 
1182783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
1183783f6184SRyusuke Konishi 
11842b0143b5SDavid Howells 		root = NILFS_I(d_inode(sb->s_root))->i_root;
1185f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, root);
1186783f6184SRyusuke Konishi 		if (err)
1187e59399d0SRyusuke Konishi 			goto restore_opts;
1188783f6184SRyusuke Konishi 
1189783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1190f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1191783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1192783f6184SRyusuke Konishi 	}
1193783f6184SRyusuke Konishi  out:
1194783f6184SRyusuke Konishi 	return 0;
1195783f6184SRyusuke Konishi 
1196783f6184SRyusuke Konishi  restore_opts:
1197783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
11983b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt = old_mount_opt;
1199783f6184SRyusuke Konishi 	return err;
1200783f6184SRyusuke Konishi }
1201783f6184SRyusuke Konishi 
1202783f6184SRyusuke Konishi struct nilfs_super_data {
1203783f6184SRyusuke Konishi 	struct block_device *bdev;
1204783f6184SRyusuke Konishi 	__u64 cno;
1205783f6184SRyusuke Konishi 	int flags;
1206783f6184SRyusuke Konishi };
1207783f6184SRyusuke Konishi 
1208a7d3f104SRyusuke Konishi static int nilfs_parse_snapshot_option(const char *option,
1209a7d3f104SRyusuke Konishi 				       const substring_t *arg,
1210a7d3f104SRyusuke Konishi 				       struct nilfs_super_data *sd)
1211a7d3f104SRyusuke Konishi {
1212a7d3f104SRyusuke Konishi 	unsigned long long val;
1213a7d3f104SRyusuke Konishi 	const char *msg = NULL;
1214a7d3f104SRyusuke Konishi 	int err;
1215a7d3f104SRyusuke Konishi 
1216a7d3f104SRyusuke Konishi 	if (!(sd->flags & MS_RDONLY)) {
1217a7d3f104SRyusuke Konishi 		msg = "read-only option is not specified";
1218a7d3f104SRyusuke Konishi 		goto parse_error;
1219a7d3f104SRyusuke Konishi 	}
1220a7d3f104SRyusuke Konishi 
1221a7d3f104SRyusuke Konishi 	err = kstrtoull(arg->from, 0, &val);
1222a7d3f104SRyusuke Konishi 	if (err) {
1223a7d3f104SRyusuke Konishi 		if (err == -ERANGE)
1224a7d3f104SRyusuke Konishi 			msg = "too large checkpoint number";
1225a7d3f104SRyusuke Konishi 		else
1226a7d3f104SRyusuke Konishi 			msg = "malformed argument";
1227a7d3f104SRyusuke Konishi 		goto parse_error;
1228a7d3f104SRyusuke Konishi 	} else if (val == 0) {
1229a7d3f104SRyusuke Konishi 		msg = "invalid checkpoint number 0";
1230a7d3f104SRyusuke Konishi 		goto parse_error;
1231a7d3f104SRyusuke Konishi 	}
1232a7d3f104SRyusuke Konishi 	sd->cno = val;
1233a7d3f104SRyusuke Konishi 	return 0;
1234a7d3f104SRyusuke Konishi 
1235a7d3f104SRyusuke Konishi parse_error:
1236a7d3f104SRyusuke Konishi 	nilfs_msg(NULL, KERN_ERR, "invalid option \"%s\": %s", option, msg);
1237a7d3f104SRyusuke Konishi 	return 1;
1238a7d3f104SRyusuke Konishi }
1239a7d3f104SRyusuke Konishi 
1240783f6184SRyusuke Konishi /**
1241783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1242783f6184SRyusuke Konishi  * @data: mount options
1243783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1244783f6184SRyusuke Konishi  */
1245783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1246783f6184SRyusuke Konishi {
1247783f6184SRyusuke Konishi 	char *p, *options = data;
1248783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1249c05dbfc2SRyusuke Konishi 	int token;
1250783f6184SRyusuke Konishi 	int ret = 0;
1251783f6184SRyusuke Konishi 
1252783f6184SRyusuke Konishi 	do {
1253783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1254783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1255783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1256a7d3f104SRyusuke Konishi 			if (token == Opt_snapshot)
1257a7d3f104SRyusuke Konishi 				ret = nilfs_parse_snapshot_option(p, &args[0],
1258a7d3f104SRyusuke Konishi 								  sd);
1259783f6184SRyusuke Konishi 		}
1260783f6184SRyusuke Konishi 		if (!options)
1261783f6184SRyusuke Konishi 			break;
1262783f6184SRyusuke Konishi 		BUG_ON(options == data);
1263783f6184SRyusuke Konishi 		*(options - 1) = ',';
1264783f6184SRyusuke Konishi 	} while (!ret);
1265783f6184SRyusuke Konishi 	return ret;
1266783f6184SRyusuke Konishi }
1267783f6184SRyusuke Konishi 
1268783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1269783f6184SRyusuke Konishi {
1270f11459adSRyusuke Konishi 	s->s_bdev = data;
1271783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1272783f6184SRyusuke Konishi 	return 0;
1273783f6184SRyusuke Konishi }
1274783f6184SRyusuke Konishi 
1275783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1276783f6184SRyusuke Konishi {
1277f11459adSRyusuke Konishi 	return (void *)s->s_bdev == data;
1278783f6184SRyusuke Konishi }
1279783f6184SRyusuke Konishi 
1280e4c59d61SAl Viro static struct dentry *
1281e4c59d61SAl Viro nilfs_mount(struct file_system_type *fs_type, int flags,
1282e4c59d61SAl Viro 	     const char *dev_name, void *data)
1283783f6184SRyusuke Konishi {
1284783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
128533c8e57cSRyusuke Konishi 	struct super_block *s;
1286d4d77629STejun Heo 	fmode_t mode = FMODE_READ | FMODE_EXCL;
1287f11459adSRyusuke Konishi 	struct dentry *root_dentry;
1288f11459adSRyusuke Konishi 	int err, s_new = false;
1289783f6184SRyusuke Konishi 
129013e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
129113e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
129213e90559SRyusuke Konishi 
1293d4d77629STejun Heo 	sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1294d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1295e4c59d61SAl Viro 		return ERR_CAST(sd.bdev);
1296783f6184SRyusuke Konishi 
1297783f6184SRyusuke Konishi 	sd.cno = 0;
1298783f6184SRyusuke Konishi 	sd.flags = flags;
1299783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1300783f6184SRyusuke Konishi 		err = -EINVAL;
1301783f6184SRyusuke Konishi 		goto failed;
1302783f6184SRyusuke Konishi 	}
1303783f6184SRyusuke Konishi 
13045beb6e0bSRyusuke Konishi 	/*
13055beb6e0bSRyusuke Konishi 	 * once the super is inserted into the list by sget, s_umount
13065beb6e0bSRyusuke Konishi 	 * will protect the lockfs code from trying to start a snapshot
13075beb6e0bSRyusuke Konishi 	 * while we are mounting
13085beb6e0bSRyusuke Konishi 	 */
13095beb6e0bSRyusuke Konishi 	mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
13105beb6e0bSRyusuke Konishi 	if (sd.bdev->bd_fsfreeze_count > 0) {
13115beb6e0bSRyusuke Konishi 		mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
13125beb6e0bSRyusuke Konishi 		err = -EBUSY;
131333c8e57cSRyusuke Konishi 		goto failed;
131433c8e57cSRyusuke Konishi 	}
13159249e17fSDavid Howells 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
13169249e17fSDavid Howells 		 sd.bdev);
13175beb6e0bSRyusuke Konishi 	mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
131833c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
131933c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
1320348fe8daSRyusuke Konishi 		goto failed;
1321783f6184SRyusuke Konishi 	}
1322783f6184SRyusuke Konishi 
1323783f6184SRyusuke Konishi 	if (!s->s_root) {
1324f11459adSRyusuke Konishi 		s_new = true;
1325f11459adSRyusuke Konishi 
132633c8e57cSRyusuke Konishi 		/* New superblock instance created */
13274571b82cSRyusuke Konishi 		s->s_mode = mode;
1328a1c6f057SDmitry Monakhov 		snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
1329783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1330783f6184SRyusuke Konishi 
1331348fe8daSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1332783f6184SRyusuke Konishi 		if (err)
1333348fe8daSRyusuke Konishi 			goto failed_super;
1334783f6184SRyusuke Konishi 
1335783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1336f11459adSRyusuke Konishi 	} else if (!sd.cno) {
1337e95c311eSAl Viro 		if (nilfs_tree_is_busy(s->s_root)) {
1338e95c311eSAl Viro 			if ((flags ^ s->s_flags) & MS_RDONLY) {
1339feee880fSRyusuke Konishi 				nilfs_msg(s, KERN_ERR,
1340feee880fSRyusuke Konishi 					  "the device already has a %s mount.",
1341f11459adSRyusuke Konishi 					  (s->s_flags & MS_RDONLY) ?
1342f11459adSRyusuke Konishi 					  "read-only" : "read/write");
1343f11459adSRyusuke Konishi 				err = -EBUSY;
1344f11459adSRyusuke Konishi 				goto failed_super;
1345f11459adSRyusuke Konishi 			}
1346e95c311eSAl Viro 		} else {
1347f11459adSRyusuke Konishi 			/*
1348f11459adSRyusuke Konishi 			 * Try remount to setup mount states if the current
1349f11459adSRyusuke Konishi 			 * tree is not mounted and only snapshots use this sb.
1350f11459adSRyusuke Konishi 			 */
1351f11459adSRyusuke Konishi 			err = nilfs_remount(s, &flags, data);
1352f11459adSRyusuke Konishi 			if (err)
1353f11459adSRyusuke Konishi 				goto failed_super;
1354f11459adSRyusuke Konishi 		}
1355783f6184SRyusuke Konishi 	}
1356783f6184SRyusuke Konishi 
1357f11459adSRyusuke Konishi 	if (sd.cno) {
1358f11459adSRyusuke Konishi 		err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1359348fe8daSRyusuke Konishi 		if (err)
1360f11459adSRyusuke Konishi 			goto failed_super;
1361f11459adSRyusuke Konishi 	} else {
1362f11459adSRyusuke Konishi 		root_dentry = dget(s->s_root);
1363783f6184SRyusuke Konishi 	}
1364783f6184SRyusuke Konishi 
1365f11459adSRyusuke Konishi 	if (!s_new)
1366d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1367f11459adSRyusuke Konishi 
1368e4c59d61SAl Viro 	return root_dentry;
1369783f6184SRyusuke Konishi 
1370f11459adSRyusuke Konishi  failed_super:
1371a95161aaSAl Viro 	deactivate_locked_super(s);
1372783f6184SRyusuke Konishi 
1373348fe8daSRyusuke Konishi  failed:
1374348fe8daSRyusuke Konishi 	if (!s_new)
1375d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1376e4c59d61SAl Viro 	return ERR_PTR(err);
1377783f6184SRyusuke Konishi }
1378783f6184SRyusuke Konishi 
1379783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1380783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1381783f6184SRyusuke Konishi 	.name     = "nilfs2",
1382e4c59d61SAl Viro 	.mount    = nilfs_mount,
1383783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1384783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1385783f6184SRyusuke Konishi };
13867f78e035SEric W. Biederman MODULE_ALIAS_FS("nilfs2");
1387783f6184SRyusuke Konishi 
138841c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
138941c88bd7SLi Hong {
139041c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
139141c88bd7SLi Hong 
139241c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
139341c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
139441c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
139541c88bd7SLi Hong #endif
13962aa15890SMiklos Szeredi 	address_space_init_once(&ii->i_btnode_cache);
139705d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
139841c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
139941c88bd7SLi Hong }
140041c88bd7SLi Hong 
140141c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
140241c88bd7SLi Hong {
140341c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
140441c88bd7SLi Hong }
140541c88bd7SLi Hong 
140641c88bd7SLi Hong static void nilfs_destroy_cachep(void)
140741c88bd7SLi Hong {
14088c0a8537SKirill A. Shutemov 	/*
14098c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14108c0a8537SKirill A. Shutemov 	 * destroy cache.
14118c0a8537SKirill A. Shutemov 	 */
14128c0a8537SKirill A. Shutemov 	rcu_barrier();
14138c0a8537SKirill A. Shutemov 
141441c88bd7SLi Hong 	kmem_cache_destroy(nilfs_inode_cachep);
141541c88bd7SLi Hong 	kmem_cache_destroy(nilfs_transaction_cachep);
141641c88bd7SLi Hong 	kmem_cache_destroy(nilfs_segbuf_cachep);
141741c88bd7SLi Hong 	kmem_cache_destroy(nilfs_btree_path_cache);
141841c88bd7SLi Hong }
141941c88bd7SLi Hong 
142041c88bd7SLi Hong static int __init nilfs_init_cachep(void)
142141c88bd7SLi Hong {
142241c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
142341c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
14245d097056SVladimir Davydov 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
14255d097056SVladimir Davydov 			nilfs_inode_init_once);
142641c88bd7SLi Hong 	if (!nilfs_inode_cachep)
142741c88bd7SLi Hong 		goto fail;
142841c88bd7SLi Hong 
142941c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
143041c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
143141c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
143241c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
143341c88bd7SLi Hong 		goto fail;
143441c88bd7SLi Hong 
143541c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
143641c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
143741c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
143841c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
143941c88bd7SLi Hong 		goto fail;
144041c88bd7SLi Hong 
144141c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
144241c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
144341c88bd7SLi Hong 			0, 0, NULL);
144441c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
144541c88bd7SLi Hong 		goto fail;
144641c88bd7SLi Hong 
144741c88bd7SLi Hong 	return 0;
144841c88bd7SLi Hong 
144941c88bd7SLi Hong fail:
145041c88bd7SLi Hong 	nilfs_destroy_cachep();
145141c88bd7SLi Hong 	return -ENOMEM;
145241c88bd7SLi Hong }
145341c88bd7SLi Hong 
1454783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1455783f6184SRyusuke Konishi {
1456783f6184SRyusuke Konishi 	int err;
1457783f6184SRyusuke Konishi 
145841c88bd7SLi Hong 	err = nilfs_init_cachep();
1459783f6184SRyusuke Konishi 	if (err)
146041c88bd7SLi Hong 		goto fail;
1461783f6184SRyusuke Konishi 
1462dd70edbdSVyacheslav Dubeyko 	err = nilfs_sysfs_init();
1463783f6184SRyusuke Konishi 	if (err)
146441c88bd7SLi Hong 		goto free_cachep;
1465783f6184SRyusuke Konishi 
1466dd70edbdSVyacheslav Dubeyko 	err = register_filesystem(&nilfs_fs_type);
1467dd70edbdSVyacheslav Dubeyko 	if (err)
1468dd70edbdSVyacheslav Dubeyko 		goto deinit_sysfs_entry;
1469dd70edbdSVyacheslav Dubeyko 
14709f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1471783f6184SRyusuke Konishi 	return 0;
1472783f6184SRyusuke Konishi 
1473dd70edbdSVyacheslav Dubeyko deinit_sysfs_entry:
1474dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
147541c88bd7SLi Hong free_cachep:
147641c88bd7SLi Hong 	nilfs_destroy_cachep();
147741c88bd7SLi Hong fail:
1478783f6184SRyusuke Konishi 	return err;
1479783f6184SRyusuke Konishi }
1480783f6184SRyusuke Konishi 
1481783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1482783f6184SRyusuke Konishi {
148341c88bd7SLi Hong 	nilfs_destroy_cachep();
1484dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
1485783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1486783f6184SRyusuke Konishi }
1487783f6184SRyusuke Konishi 
1488783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1489783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1490