xref: /linux/fs/nilfs2/super.c (revision c306af23e19d3c94c9229263c39fe487e915e774)
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  *
16783f6184SRyusuke Konishi  * You should have received a copy of the GNU General Public License
17783f6184SRyusuke Konishi  * along with this program; if not, write to the Free Software
18783f6184SRyusuke Konishi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19783f6184SRyusuke Konishi  *
20783f6184SRyusuke Konishi  * Written by Ryusuke Konishi <ryusuke@osrg.net>
21783f6184SRyusuke Konishi  */
22783f6184SRyusuke Konishi /*
23783f6184SRyusuke Konishi  *  linux/fs/ext2/super.c
24783f6184SRyusuke Konishi  *
25783f6184SRyusuke Konishi  * Copyright (C) 1992, 1993, 1994, 1995
26783f6184SRyusuke Konishi  * Remy Card (card@masi.ibp.fr)
27783f6184SRyusuke Konishi  * Laboratoire MASI - Institut Blaise Pascal
28783f6184SRyusuke Konishi  * Universite Pierre et Marie Curie (Paris VI)
29783f6184SRyusuke Konishi  *
30783f6184SRyusuke Konishi  *  from
31783f6184SRyusuke Konishi  *
32783f6184SRyusuke Konishi  *  linux/fs/minix/inode.c
33783f6184SRyusuke Konishi  *
34783f6184SRyusuke Konishi  *  Copyright (C) 1991, 1992  Linus Torvalds
35783f6184SRyusuke Konishi  *
36783f6184SRyusuke Konishi  *  Big-endian to little-endian byte-swapping/bitmaps by
37783f6184SRyusuke Konishi  *        David S. Miller (davem@caip.rutgers.edu), 1995
38783f6184SRyusuke Konishi  */
39783f6184SRyusuke Konishi 
40783f6184SRyusuke Konishi #include <linux/module.h>
41783f6184SRyusuke Konishi #include <linux/string.h>
42783f6184SRyusuke Konishi #include <linux/slab.h>
43783f6184SRyusuke Konishi #include <linux/init.h>
44783f6184SRyusuke Konishi #include <linux/blkdev.h>
45783f6184SRyusuke Konishi #include <linux/parser.h>
46783f6184SRyusuke Konishi #include <linux/random.h>
47783f6184SRyusuke Konishi #include <linux/crc32.h>
48783f6184SRyusuke Konishi #include <linux/smp_lock.h>
49783f6184SRyusuke Konishi #include <linux/vfs.h>
50783f6184SRyusuke Konishi #include <linux/writeback.h>
51783f6184SRyusuke Konishi #include <linux/kobject.h>
52783f6184SRyusuke Konishi #include <linux/exportfs.h>
53783f6184SRyusuke Konishi #include "nilfs.h"
54783f6184SRyusuke Konishi #include "mdt.h"
55783f6184SRyusuke Konishi #include "alloc.h"
56783f6184SRyusuke Konishi #include "page.h"
57783f6184SRyusuke Konishi #include "cpfile.h"
58783f6184SRyusuke Konishi #include "ifile.h"
59783f6184SRyusuke Konishi #include "dat.h"
60783f6184SRyusuke Konishi #include "segment.h"
61783f6184SRyusuke Konishi #include "segbuf.h"
62783f6184SRyusuke Konishi 
63783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
64783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
65783f6184SRyusuke Konishi 		   "(NILFS)");
66783f6184SRyusuke Konishi MODULE_VERSION(NILFS_VERSION);
67783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
68783f6184SRyusuke Konishi 
69783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
70783f6184SRyusuke Konishi static int test_exclusive_mount(struct file_system_type *fs_type,
71783f6184SRyusuke Konishi 				struct block_device *bdev, int flags);
72783f6184SRyusuke Konishi 
73783f6184SRyusuke Konishi /**
74783f6184SRyusuke Konishi  * nilfs_error() - report failure condition on a filesystem
75783f6184SRyusuke Konishi  *
76783f6184SRyusuke Konishi  * nilfs_error() sets an ERROR_FS flag on the superblock as well as
77783f6184SRyusuke Konishi  * reporting an error message.  It should be called when NILFS detects
78783f6184SRyusuke Konishi  * incoherences or defects of meta data on disk.  As for sustainable
79783f6184SRyusuke Konishi  * errors such as a single-shot I/O error, nilfs_warning() or the printk()
80783f6184SRyusuke Konishi  * function should be used instead.
81783f6184SRyusuke Konishi  *
82783f6184SRyusuke Konishi  * The segment constructor must not call this function because it can
83783f6184SRyusuke Konishi  * kill itself.
84783f6184SRyusuke Konishi  */
85783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function,
86783f6184SRyusuke Konishi 		 const char *fmt, ...)
87783f6184SRyusuke Konishi {
88783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
89783f6184SRyusuke Konishi 	va_list args;
90783f6184SRyusuke Konishi 
91783f6184SRyusuke Konishi 	va_start(args, fmt);
92783f6184SRyusuke Konishi 	printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
93783f6184SRyusuke Konishi 	vprintk(fmt, args);
94783f6184SRyusuke Konishi 	printk("\n");
95783f6184SRyusuke Konishi 	va_end(args);
96783f6184SRyusuke Konishi 
97783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
98783f6184SRyusuke Konishi 		struct the_nilfs *nilfs = sbi->s_nilfs;
99783f6184SRyusuke Konishi 
100783f6184SRyusuke Konishi 		if (!nilfs_test_opt(sbi, ERRORS_CONT))
101783f6184SRyusuke Konishi 			nilfs_detach_segment_constructor(sbi);
102783f6184SRyusuke Konishi 
103783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
104783f6184SRyusuke Konishi 		if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
105783f6184SRyusuke Konishi 			nilfs->ns_mount_state |= NILFS_ERROR_FS;
106e339ad31SRyusuke Konishi 			nilfs->ns_sbp[0]->s_state |=
107e339ad31SRyusuke Konishi 				cpu_to_le16(NILFS_ERROR_FS);
108e339ad31SRyusuke Konishi 			nilfs_commit_super(sbi, 1);
109783f6184SRyusuke Konishi 		}
110783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
111783f6184SRyusuke Konishi 
112783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, ERRORS_RO)) {
113783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
114783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
115783f6184SRyusuke Konishi 		}
116783f6184SRyusuke Konishi 	}
117783f6184SRyusuke Konishi 
118783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
119783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
120783f6184SRyusuke Konishi 		      sb->s_id);
121783f6184SRyusuke Konishi }
122783f6184SRyusuke Konishi 
123783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function,
124783f6184SRyusuke Konishi 		   const char *fmt, ...)
125783f6184SRyusuke Konishi {
126783f6184SRyusuke Konishi 	va_list args;
127783f6184SRyusuke Konishi 
128783f6184SRyusuke Konishi 	va_start(args, fmt);
129783f6184SRyusuke Konishi 	printk(KERN_WARNING "NILFS warning (device %s): %s: ",
130783f6184SRyusuke Konishi 	       sb->s_id, function);
131783f6184SRyusuke Konishi 	vprintk(fmt, args);
132783f6184SRyusuke Konishi 	printk("\n");
133783f6184SRyusuke Konishi 	va_end(args);
134783f6184SRyusuke Konishi }
135783f6184SRyusuke Konishi 
136783f6184SRyusuke Konishi static struct kmem_cache *nilfs_inode_cachep;
137783f6184SRyusuke Konishi 
138783f6184SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
139783f6184SRyusuke Konishi {
140783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
141783f6184SRyusuke Konishi 
142783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
143783f6184SRyusuke Konishi 	if (!ii)
144783f6184SRyusuke Konishi 		return NULL;
145783f6184SRyusuke Konishi 	ii->i_bh = NULL;
146783f6184SRyusuke Konishi 	ii->i_state = 0;
147783f6184SRyusuke Konishi 	ii->vfs_inode.i_version = 1;
148783f6184SRyusuke Konishi 	nilfs_btnode_cache_init(&ii->i_btnode_cache);
149783f6184SRyusuke Konishi 	return &ii->vfs_inode;
150783f6184SRyusuke Konishi }
151783f6184SRyusuke Konishi 
152783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode)
153783f6184SRyusuke Konishi {
154783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
155783f6184SRyusuke Konishi }
156783f6184SRyusuke Konishi 
157783f6184SRyusuke Konishi static void init_once(void *obj)
158783f6184SRyusuke Konishi {
159783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii = obj;
160783f6184SRyusuke Konishi 
161783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&ii->i_dirty);
162783f6184SRyusuke Konishi #ifdef CONFIG_NILFS_XATTR
163783f6184SRyusuke Konishi 	init_rwsem(&ii->xattr_sem);
164783f6184SRyusuke Konishi #endif
165783f6184SRyusuke Konishi 	nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
166783f6184SRyusuke Konishi 	ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
167783f6184SRyusuke Konishi 	inode_init_once(&ii->vfs_inode);
168783f6184SRyusuke Konishi }
169783f6184SRyusuke Konishi 
170783f6184SRyusuke Konishi static int nilfs_init_inode_cache(void)
171783f6184SRyusuke Konishi {
172783f6184SRyusuke Konishi 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
173783f6184SRyusuke Konishi 					       sizeof(struct nilfs_inode_info),
174783f6184SRyusuke Konishi 					       0, SLAB_RECLAIM_ACCOUNT,
175783f6184SRyusuke Konishi 					       init_once);
176783f6184SRyusuke Konishi 
177783f6184SRyusuke Konishi 	return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0;
178783f6184SRyusuke Konishi }
179783f6184SRyusuke Konishi 
180783f6184SRyusuke Konishi static inline void nilfs_destroy_inode_cache(void)
181783f6184SRyusuke Konishi {
182783f6184SRyusuke Konishi 	kmem_cache_destroy(nilfs_inode_cachep);
183783f6184SRyusuke Konishi }
184783f6184SRyusuke Konishi 
185783f6184SRyusuke Konishi static void nilfs_clear_inode(struct inode *inode)
186783f6184SRyusuke Konishi {
187783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
188783f6184SRyusuke Konishi 
189783f6184SRyusuke Konishi #ifdef CONFIG_NILFS_POSIX_ACL
190783f6184SRyusuke Konishi 	if (ii->i_acl && ii->i_acl != NILFS_ACL_NOT_CACHED) {
191783f6184SRyusuke Konishi 		posix_acl_release(ii->i_acl);
192783f6184SRyusuke Konishi 		ii->i_acl = NILFS_ACL_NOT_CACHED;
193783f6184SRyusuke Konishi 	}
194783f6184SRyusuke Konishi 	if (ii->i_default_acl && ii->i_default_acl != NILFS_ACL_NOT_CACHED) {
195783f6184SRyusuke Konishi 		posix_acl_release(ii->i_default_acl);
196783f6184SRyusuke Konishi 		ii->i_default_acl = NILFS_ACL_NOT_CACHED;
197783f6184SRyusuke Konishi 	}
198783f6184SRyusuke Konishi #endif
199783f6184SRyusuke Konishi 	/*
200783f6184SRyusuke Konishi 	 * Free resources allocated in nilfs_read_inode(), here.
201783f6184SRyusuke Konishi 	 */
202a2e7d2dfSRyusuke Konishi 	BUG_ON(!list_empty(&ii->i_dirty));
203783f6184SRyusuke Konishi 	brelse(ii->i_bh);
204783f6184SRyusuke Konishi 	ii->i_bh = NULL;
205783f6184SRyusuke Konishi 
206783f6184SRyusuke Konishi 	if (test_bit(NILFS_I_BMAP, &ii->i_state))
207783f6184SRyusuke Konishi 		nilfs_bmap_clear(ii->i_bmap);
208783f6184SRyusuke Konishi 
209783f6184SRyusuke Konishi 	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
210783f6184SRyusuke Konishi }
211783f6184SRyusuke Konishi 
212e339ad31SRyusuke Konishi static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb)
213783f6184SRyusuke Konishi {
214783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
215783f6184SRyusuke Konishi 	int err;
216783f6184SRyusuke Konishi 	int barrier_done = 0;
217783f6184SRyusuke Konishi 
218783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, BARRIER)) {
219e339ad31SRyusuke Konishi 		set_buffer_ordered(nilfs->ns_sbh[0]);
220783f6184SRyusuke Konishi 		barrier_done = 1;
221783f6184SRyusuke Konishi 	}
222783f6184SRyusuke Konishi  retry:
223e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
224e339ad31SRyusuke Konishi 	err = sync_dirty_buffer(nilfs->ns_sbh[0]);
225783f6184SRyusuke Konishi 	if (err == -EOPNOTSUPP && barrier_done) {
226783f6184SRyusuke Konishi 		nilfs_warning(sbi->s_super, __func__,
227783f6184SRyusuke Konishi 			      "barrier-based sync failed. "
228783f6184SRyusuke Konishi 			      "disabling barriers\n");
229783f6184SRyusuke Konishi 		nilfs_clear_opt(sbi, BARRIER);
230783f6184SRyusuke Konishi 		barrier_done = 0;
231e339ad31SRyusuke Konishi 		clear_buffer_ordered(nilfs->ns_sbh[0]);
232783f6184SRyusuke Konishi 		goto retry;
233783f6184SRyusuke Konishi 	}
234e339ad31SRyusuke Konishi 	if (unlikely(err)) {
235783f6184SRyusuke Konishi 		printk(KERN_ERR
236783f6184SRyusuke Konishi 		       "NILFS: unable to write superblock (err=%d)\n", err);
237e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
238e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
239e339ad31SRyusuke Konishi 			goto retry;
240e339ad31SRyusuke Konishi 		}
241e339ad31SRyusuke Konishi 	} else {
242e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
243e339ad31SRyusuke Konishi 
244e339ad31SRyusuke Konishi 		/*
245e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
246e339ad31SRyusuke Konishi 		 * written in superblock.
247e339ad31SRyusuke Konishi 		 */
248783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
249e339ad31SRyusuke Konishi 
250e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
251e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
252e339ad31SRyusuke Konishi 			sbp = NULL;
253e339ad31SRyusuke Konishi 			if (dupsb) {
254e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
255e339ad31SRyusuke Konishi 				if (!sync_dirty_buffer(nilfs->ns_sbh[1]))
256e339ad31SRyusuke Konishi 					sbp = nilfs->ns_sbp[1];
257e339ad31SRyusuke Konishi 			}
258e339ad31SRyusuke Konishi 		}
259e339ad31SRyusuke Konishi 		if (sbp) {
2602c2e52fcSRyusuke Konishi 			spin_lock(&nilfs->ns_last_segment_lock);
261e339ad31SRyusuke Konishi 			nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2622c2e52fcSRyusuke Konishi 			spin_unlock(&nilfs->ns_last_segment_lock);
263783f6184SRyusuke Konishi 		}
264e339ad31SRyusuke Konishi 	}
265783f6184SRyusuke Konishi 
266783f6184SRyusuke Konishi 	return err;
267783f6184SRyusuke Konishi }
268783f6184SRyusuke Konishi 
269e339ad31SRyusuke Konishi int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
270783f6184SRyusuke Konishi {
271783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
272e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
273783f6184SRyusuke Konishi 	sector_t nfreeblocks;
274e339ad31SRyusuke Konishi 	time_t t;
275783f6184SRyusuke Konishi 	int err;
276783f6184SRyusuke Konishi 
277783f6184SRyusuke Konishi 	/* nilfs->sem must be locked by the caller. */
278e339ad31SRyusuke Konishi 	if (sbp[0]->s_magic != NILFS_SUPER_MAGIC) {
279e339ad31SRyusuke Konishi 		if (sbp[1] && sbp[1]->s_magic == NILFS_SUPER_MAGIC)
280e339ad31SRyusuke Konishi 			nilfs_swap_super_block(nilfs);
281e339ad31SRyusuke Konishi 		else {
282e339ad31SRyusuke Konishi 			printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
283e339ad31SRyusuke Konishi 			       sbi->s_super->s_id);
284e339ad31SRyusuke Konishi 			return -EIO;
285e339ad31SRyusuke Konishi 		}
286e339ad31SRyusuke Konishi 	}
287783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
288783f6184SRyusuke Konishi 	if (unlikely(err)) {
289783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: failed to count free blocks\n");
290783f6184SRyusuke Konishi 		return err;
291783f6184SRyusuke Konishi 	}
292e339ad31SRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
293e339ad31SRyusuke Konishi 	sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
294e339ad31SRyusuke Konishi 	sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
295e339ad31SRyusuke Konishi 	sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
296e339ad31SRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
297e339ad31SRyusuke Konishi 
298e339ad31SRyusuke Konishi 	t = get_seconds();
299e339ad31SRyusuke Konishi 	nilfs->ns_sbwtime[0] = t;
300e339ad31SRyusuke Konishi 	sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks);
301e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
302e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
303e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
304e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
305e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
306e339ad31SRyusuke Konishi 	if (dupsb && sbp[1]) {
307e339ad31SRyusuke Konishi 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
308e339ad31SRyusuke Konishi 		nilfs->ns_sbwtime[1] = t;
309e339ad31SRyusuke Konishi 	}
310783f6184SRyusuke Konishi 	sbi->s_super->s_dirt = 0;
311e339ad31SRyusuke Konishi 	return nilfs_sync_super(sbi, dupsb);
312783f6184SRyusuke Konishi }
313783f6184SRyusuke Konishi 
314783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
315783f6184SRyusuke Konishi {
316783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
317783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
318783f6184SRyusuke Konishi 
319783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
320783f6184SRyusuke Konishi 
321783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
322783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
323e339ad31SRyusuke Konishi 		nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
324e339ad31SRyusuke Konishi 		nilfs_commit_super(sbi, 1);
325783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
326783f6184SRyusuke Konishi 	}
327783f6184SRyusuke Konishi 
328783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
329783f6184SRyusuke Konishi 	put_nilfs(sbi->s_nilfs);
330783f6184SRyusuke Konishi 	sbi->s_super = NULL;
331783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
332783f6184SRyusuke Konishi 	kfree(sbi);
333783f6184SRyusuke Konishi }
334783f6184SRyusuke Konishi 
335783f6184SRyusuke Konishi /**
336783f6184SRyusuke Konishi  * nilfs_write_super - write super block(s) of NILFS
337783f6184SRyusuke Konishi  * @sb: super_block
338783f6184SRyusuke Konishi  *
339783f6184SRyusuke Konishi  * nilfs_write_super() gets a fs-dependent lock, writes super block(s), and
340783f6184SRyusuke Konishi  * clears s_dirt.  This function is called in the section protected by
341783f6184SRyusuke Konishi  * lock_super().
342783f6184SRyusuke Konishi  *
343783f6184SRyusuke Konishi  * The s_dirt flag is managed by each filesystem and we protect it by ns_sem
344783f6184SRyusuke Konishi  * of the struct the_nilfs.  Lock order must be as follows:
345783f6184SRyusuke Konishi  *
346783f6184SRyusuke Konishi  *   1. lock_super()
347783f6184SRyusuke Konishi  *   2.    down_write(&nilfs->ns_sem)
348783f6184SRyusuke Konishi  *
349783f6184SRyusuke Konishi  * Inside NILFS, locking ns_sem is enough to protect s_dirt and the buffer
350e339ad31SRyusuke Konishi  * of the super block (nilfs->ns_sbp[]).
351783f6184SRyusuke Konishi  *
352783f6184SRyusuke Konishi  * In most cases, VFS functions call lock_super() before calling these
353783f6184SRyusuke Konishi  * methods.  So we must be careful not to bring on deadlocks when using
354783f6184SRyusuke Konishi  * lock_super();  see generic_shutdown_super(), write_super(), and so on.
355783f6184SRyusuke Konishi  *
356783f6184SRyusuke Konishi  * Note that order of lock_kernel() and lock_super() depends on contexts
357783f6184SRyusuke Konishi  * of VFS.  We should also note that lock_kernel() can be used in its
358783f6184SRyusuke Konishi  * protective section and only the outermost one has an effect.
359783f6184SRyusuke Konishi  */
360783f6184SRyusuke Konishi static void nilfs_write_super(struct super_block *sb)
361783f6184SRyusuke Konishi {
362783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
363783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
364783f6184SRyusuke Konishi 
365783f6184SRyusuke Konishi 	down_write(&nilfs->ns_sem);
366e339ad31SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
367e339ad31SRyusuke Konishi 		struct nilfs_super_block **sbp = nilfs->ns_sbp;
368e339ad31SRyusuke Konishi 		u64 t = get_seconds();
369e339ad31SRyusuke Konishi 		int dupsb;
370e339ad31SRyusuke Konishi 
371e339ad31SRyusuke Konishi 		if (!nilfs_discontinued(nilfs) && t >= nilfs->ns_sbwtime[0] &&
372e339ad31SRyusuke Konishi 		    t < nilfs->ns_sbwtime[0] + NILFS_SB_FREQ) {
373e339ad31SRyusuke Konishi 			up_write(&nilfs->ns_sem);
374e339ad31SRyusuke Konishi 			return;
375e339ad31SRyusuke Konishi 		}
376e339ad31SRyusuke Konishi 		dupsb = sbp[1] && t > nilfs->ns_sbwtime[1] + NILFS_ALTSB_FREQ;
377e339ad31SRyusuke Konishi 		nilfs_commit_super(sbi, dupsb);
378e339ad31SRyusuke Konishi 	}
379783f6184SRyusuke Konishi 	sb->s_dirt = 0;
380783f6184SRyusuke Konishi 	up_write(&nilfs->ns_sem);
381783f6184SRyusuke Konishi }
382783f6184SRyusuke Konishi 
383783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
384783f6184SRyusuke Konishi {
385783f6184SRyusuke Konishi 	int err = 0;
386783f6184SRyusuke Konishi 
387783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
388783f6184SRyusuke Konishi 	if (wait)
389783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
390783f6184SRyusuke Konishi 	return err;
391783f6184SRyusuke Konishi }
392783f6184SRyusuke Konishi 
393783f6184SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
394783f6184SRyusuke Konishi {
395783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
396783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
397783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
398783f6184SRyusuke Konishi 	int err;
399783f6184SRyusuke Konishi 
400783f6184SRyusuke Konishi 	down_write(&nilfs->ns_sem);
401783f6184SRyusuke Konishi 	list_add(&sbi->s_list, &nilfs->ns_supers);
402783f6184SRyusuke Konishi 	up_write(&nilfs->ns_sem);
403783f6184SRyusuke Konishi 
404783f6184SRyusuke Konishi 	sbi->s_ifile = nilfs_mdt_new(
405783f6184SRyusuke Konishi 		nilfs, sbi->s_super, NILFS_IFILE_INO, NILFS_IFILE_GFP);
406783f6184SRyusuke Konishi 	if (!sbi->s_ifile)
407783f6184SRyusuke Konishi 		return -ENOMEM;
408783f6184SRyusuke Konishi 
409783f6184SRyusuke Konishi 	err = nilfs_palloc_init_blockgroup(sbi->s_ifile, nilfs->ns_inode_size);
410783f6184SRyusuke Konishi 	if (unlikely(err))
411783f6184SRyusuke Konishi 		goto failed;
412783f6184SRyusuke Konishi 
413783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
414783f6184SRyusuke Konishi 					  &bh_cp);
415783f6184SRyusuke Konishi 	if (unlikely(err)) {
416783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
417783f6184SRyusuke Konishi 			printk(KERN_ERR
418783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
419783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
420783f6184SRyusuke Konishi 			       (unsigned long long)cno);
421783f6184SRyusuke Konishi 			err = -EINVAL;
422783f6184SRyusuke Konishi 		}
423783f6184SRyusuke Konishi 		goto failed;
424783f6184SRyusuke Konishi 	}
425783f6184SRyusuke Konishi 	err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
426783f6184SRyusuke Konishi 	if (unlikely(err))
427783f6184SRyusuke Konishi 		goto failed_bh;
428783f6184SRyusuke Konishi 	atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
429783f6184SRyusuke Konishi 	atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
430783f6184SRyusuke Konishi 
431783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
432783f6184SRyusuke Konishi 	return 0;
433783f6184SRyusuke Konishi 
434783f6184SRyusuke Konishi  failed_bh:
435783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
436783f6184SRyusuke Konishi  failed:
437783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
438783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
439783f6184SRyusuke Konishi 
440783f6184SRyusuke Konishi 	down_write(&nilfs->ns_sem);
441783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
442783f6184SRyusuke Konishi 	up_write(&nilfs->ns_sem);
443783f6184SRyusuke Konishi 
444783f6184SRyusuke Konishi 	return err;
445783f6184SRyusuke Konishi }
446783f6184SRyusuke Konishi 
447783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
448783f6184SRyusuke Konishi {
449783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
450783f6184SRyusuke Konishi 
451783f6184SRyusuke Konishi 	nilfs_mdt_clear(sbi->s_ifile);
452783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
453783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
454783f6184SRyusuke Konishi 	down_write(&nilfs->ns_sem);
455783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
456783f6184SRyusuke Konishi 	up_write(&nilfs->ns_sem);
457783f6184SRyusuke Konishi }
458783f6184SRyusuke Konishi 
459783f6184SRyusuke Konishi static int nilfs_mark_recovery_complete(struct nilfs_sb_info *sbi)
460783f6184SRyusuke Konishi {
461783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
462783f6184SRyusuke Konishi 	int err = 0;
463783f6184SRyusuke Konishi 
464783f6184SRyusuke Konishi 	down_write(&nilfs->ns_sem);
465783f6184SRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
466783f6184SRyusuke Konishi 		nilfs->ns_mount_state |= NILFS_VALID_FS;
467e339ad31SRyusuke Konishi 		err = nilfs_commit_super(sbi, 1);
468783f6184SRyusuke Konishi 		if (likely(!err))
469783f6184SRyusuke Konishi 			printk(KERN_INFO "NILFS: recovery complete.\n");
470783f6184SRyusuke Konishi 	}
471783f6184SRyusuke Konishi 	up_write(&nilfs->ns_sem);
472783f6184SRyusuke Konishi 	return err;
473783f6184SRyusuke Konishi }
474783f6184SRyusuke Konishi 
475783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
476783f6184SRyusuke Konishi {
477783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
478783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
479*c306af23SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
480*c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
481783f6184SRyusuke Konishi 	unsigned long long blocks;
482783f6184SRyusuke Konishi 	unsigned long overhead;
483783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
484783f6184SRyusuke Konishi 	sector_t nfreeblocks;
485783f6184SRyusuke Konishi 	int err;
486783f6184SRyusuke Konishi 
487783f6184SRyusuke Konishi 	/*
488783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
489783f6184SRyusuke Konishi 	 *
490783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
491783f6184SRyusuke Konishi 	 * are excluded.
492783f6184SRyusuke Konishi 	 */
493783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
494783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
495783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
496783f6184SRyusuke Konishi 
497783f6184SRyusuke Konishi 	/*
498783f6184SRyusuke Konishi 	 * Compute the overhead
499783f6184SRyusuke Konishi 	 *
500783f6184SRyusuke Konishi 	 * When distributing meta data blocks outside semgent structure,
501783f6184SRyusuke Konishi 	 * We must count them as the overhead.
502783f6184SRyusuke Konishi 	 */
503783f6184SRyusuke Konishi 	overhead = 0;
504783f6184SRyusuke Konishi 
505783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
506783f6184SRyusuke Konishi 	if (unlikely(err))
507783f6184SRyusuke Konishi 		return err;
508783f6184SRyusuke Konishi 
509783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
510783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
511783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
512783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
513783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
514783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
515783f6184SRyusuke Konishi 	buf->f_files = atomic_read(&sbi->s_inodes_count);
516783f6184SRyusuke Konishi 	buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
517783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
518*c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
519*c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
520*c306af23SRyusuke Konishi 
521783f6184SRyusuke Konishi 	return 0;
522783f6184SRyusuke Konishi }
523783f6184SRyusuke Konishi 
524783f6184SRyusuke Konishi static struct super_operations nilfs_sops = {
525783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
526783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
527783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
528783f6184SRyusuke Konishi 	/* .write_inode    = nilfs_write_inode, */
529783f6184SRyusuke Konishi 	/* .put_inode      = nilfs_put_inode, */
530783f6184SRyusuke Konishi 	/* .drop_inode	  = nilfs_drop_inode, */
531783f6184SRyusuke Konishi 	.delete_inode   = nilfs_delete_inode,
532783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
533783f6184SRyusuke Konishi 	.write_super    = nilfs_write_super,
534783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
535783f6184SRyusuke Konishi 	/* .write_super_lockfs */
536783f6184SRyusuke Konishi 	/* .unlockfs */
537783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
538783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
539783f6184SRyusuke Konishi 	.clear_inode    = nilfs_clear_inode,
540783f6184SRyusuke Konishi 	/* .umount_begin */
541783f6184SRyusuke Konishi 	/* .show_options */
542783f6184SRyusuke Konishi };
543783f6184SRyusuke Konishi 
544783f6184SRyusuke Konishi static struct inode *
545783f6184SRyusuke Konishi nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
546783f6184SRyusuke Konishi {
547783f6184SRyusuke Konishi 	struct inode *inode;
548783f6184SRyusuke Konishi 
549783f6184SRyusuke Konishi 	if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
550783f6184SRyusuke Konishi 	    ino != NILFS_SKETCH_INO)
551783f6184SRyusuke Konishi 		return ERR_PTR(-ESTALE);
552783f6184SRyusuke Konishi 
553783f6184SRyusuke Konishi 	inode = nilfs_iget(sb, ino);
554783f6184SRyusuke Konishi 	if (IS_ERR(inode))
555783f6184SRyusuke Konishi 		return ERR_CAST(inode);
556783f6184SRyusuke Konishi 	if (generation && inode->i_generation != generation) {
557783f6184SRyusuke Konishi 		iput(inode);
558783f6184SRyusuke Konishi 		return ERR_PTR(-ESTALE);
559783f6184SRyusuke Konishi 	}
560783f6184SRyusuke Konishi 
561783f6184SRyusuke Konishi 	return inode;
562783f6184SRyusuke Konishi }
563783f6184SRyusuke Konishi 
564783f6184SRyusuke Konishi static struct dentry *
565783f6184SRyusuke Konishi nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
566783f6184SRyusuke Konishi 		   int fh_type)
567783f6184SRyusuke Konishi {
568783f6184SRyusuke Konishi 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
569783f6184SRyusuke Konishi 				    nilfs_nfs_get_inode);
570783f6184SRyusuke Konishi }
571783f6184SRyusuke Konishi 
572783f6184SRyusuke Konishi static struct dentry *
573783f6184SRyusuke Konishi nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
574783f6184SRyusuke Konishi 		   int fh_type)
575783f6184SRyusuke Konishi {
576783f6184SRyusuke Konishi 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
577783f6184SRyusuke Konishi 				    nilfs_nfs_get_inode);
578783f6184SRyusuke Konishi }
579783f6184SRyusuke Konishi 
580783f6184SRyusuke Konishi static struct export_operations nilfs_export_ops = {
581783f6184SRyusuke Konishi 	.fh_to_dentry = nilfs_fh_to_dentry,
582783f6184SRyusuke Konishi 	.fh_to_parent = nilfs_fh_to_parent,
583783f6184SRyusuke Konishi 	.get_parent = nilfs_get_parent,
584783f6184SRyusuke Konishi };
585783f6184SRyusuke Konishi 
586783f6184SRyusuke Konishi enum {
587783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
588783f6184SRyusuke Konishi 	Opt_barrier, Opt_snapshot, Opt_order,
589783f6184SRyusuke Konishi 	Opt_err,
590783f6184SRyusuke Konishi };
591783f6184SRyusuke Konishi 
592783f6184SRyusuke Konishi static match_table_t tokens = {
593783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
594783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
595783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
596783f6184SRyusuke Konishi 	{Opt_barrier, "barrier=%s"},
597783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
598783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
599783f6184SRyusuke Konishi 	{Opt_err, NULL}
600783f6184SRyusuke Konishi };
601783f6184SRyusuke Konishi 
602783f6184SRyusuke Konishi static int match_bool(substring_t *s, int *result)
603783f6184SRyusuke Konishi {
604783f6184SRyusuke Konishi 	int len = s->to - s->from;
605783f6184SRyusuke Konishi 
606783f6184SRyusuke Konishi 	if (strncmp(s->from, "on", len) == 0)
607783f6184SRyusuke Konishi 		*result = 1;
608783f6184SRyusuke Konishi 	else if (strncmp(s->from, "off", len) == 0)
609783f6184SRyusuke Konishi 		*result = 0;
610783f6184SRyusuke Konishi 	else
611783f6184SRyusuke Konishi 		return 1;
612783f6184SRyusuke Konishi 	return 0;
613783f6184SRyusuke Konishi }
614783f6184SRyusuke Konishi 
615783f6184SRyusuke Konishi static int parse_options(char *options, struct super_block *sb)
616783f6184SRyusuke Konishi {
617783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
618783f6184SRyusuke Konishi 	char *p;
619783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
620783f6184SRyusuke Konishi 	int option;
621783f6184SRyusuke Konishi 
622783f6184SRyusuke Konishi 	if (!options)
623783f6184SRyusuke Konishi 		return 1;
624783f6184SRyusuke Konishi 
625783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
626783f6184SRyusuke Konishi 		int token;
627783f6184SRyusuke Konishi 		if (!*p)
628783f6184SRyusuke Konishi 			continue;
629783f6184SRyusuke Konishi 
630783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
631783f6184SRyusuke Konishi 		switch (token) {
632783f6184SRyusuke Konishi 		case Opt_barrier:
633783f6184SRyusuke Konishi 			if (match_bool(&args[0], &option))
634783f6184SRyusuke Konishi 				return 0;
635783f6184SRyusuke Konishi 			if (option)
636783f6184SRyusuke Konishi 				nilfs_set_opt(sbi, BARRIER);
637783f6184SRyusuke Konishi 			else
638783f6184SRyusuke Konishi 				nilfs_clear_opt(sbi, BARRIER);
639783f6184SRyusuke Konishi 			break;
640783f6184SRyusuke Konishi 		case Opt_order:
641783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
642783f6184SRyusuke Konishi 				/* Ordered data semantics */
643783f6184SRyusuke Konishi 				nilfs_clear_opt(sbi, STRICT_ORDER);
644783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
645783f6184SRyusuke Konishi 				/* Strict in-order semantics */
646783f6184SRyusuke Konishi 				nilfs_set_opt(sbi, STRICT_ORDER);
647783f6184SRyusuke Konishi 			else
648783f6184SRyusuke Konishi 				return 0;
649783f6184SRyusuke Konishi 			break;
650783f6184SRyusuke Konishi 		case Opt_err_panic:
651783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
652783f6184SRyusuke Konishi 			break;
653783f6184SRyusuke Konishi 		case Opt_err_ro:
654783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
655783f6184SRyusuke Konishi 			break;
656783f6184SRyusuke Konishi 		case Opt_err_cont:
657783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
658783f6184SRyusuke Konishi 			break;
659783f6184SRyusuke Konishi 		case Opt_snapshot:
660783f6184SRyusuke Konishi 			if (match_int(&args[0], &option) || option <= 0)
661783f6184SRyusuke Konishi 				return 0;
662783f6184SRyusuke Konishi 			if (!(sb->s_flags & MS_RDONLY))
663783f6184SRyusuke Konishi 				return 0;
664783f6184SRyusuke Konishi 			sbi->s_snapshot_cno = option;
665783f6184SRyusuke Konishi 			nilfs_set_opt(sbi, SNAPSHOT);
666783f6184SRyusuke Konishi 			break;
667783f6184SRyusuke Konishi 		default:
668783f6184SRyusuke Konishi 			printk(KERN_ERR
669783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
670783f6184SRyusuke Konishi 			return 0;
671783f6184SRyusuke Konishi 		}
672783f6184SRyusuke Konishi 	}
673783f6184SRyusuke Konishi 	return 1;
674783f6184SRyusuke Konishi }
675783f6184SRyusuke Konishi 
676783f6184SRyusuke Konishi static inline void
677783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi,
678783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
679783f6184SRyusuke Konishi {
680783f6184SRyusuke Konishi 	sbi->s_mount_opt =
681783f6184SRyusuke Konishi 		NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER;
682783f6184SRyusuke Konishi }
683783f6184SRyusuke Konishi 
684783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi)
685783f6184SRyusuke Konishi {
686783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
687e339ad31SRyusuke Konishi 	struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
688783f6184SRyusuke Konishi 	int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count);
689783f6184SRyusuke Konishi 	int mnt_count = le16_to_cpu(sbp->s_mnt_count);
690783f6184SRyusuke Konishi 
691783f6184SRyusuke Konishi 	/* nilfs->sem must be locked by the caller. */
692783f6184SRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) {
693783f6184SRyusuke Konishi 		printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
694783f6184SRyusuke Konishi 	} else if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
695783f6184SRyusuke Konishi 		printk(KERN_WARNING
696783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
697783f6184SRyusuke Konishi #if 0
698783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
699783f6184SRyusuke Konishi 		printk(KERN_WARNING
700783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
701783f6184SRyusuke Konishi #endif
702783f6184SRyusuke Konishi 	}
703783f6184SRyusuke Konishi 	if (!max_mnt_count)
704783f6184SRyusuke Konishi 		sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
705783f6184SRyusuke Konishi 
706783f6184SRyusuke Konishi 	sbp->s_mnt_count = cpu_to_le16(mnt_count + 1);
707783f6184SRyusuke Konishi 	sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS);
708783f6184SRyusuke Konishi 	sbp->s_mtime = cpu_to_le64(get_seconds());
709e339ad31SRyusuke Konishi 	return nilfs_commit_super(sbi, 1);
710783f6184SRyusuke Konishi }
711783f6184SRyusuke Konishi 
712e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
713e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
714e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
715783f6184SRyusuke Konishi {
716e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
717e339ad31SRyusuke Konishi 	unsigned long offset;
718783f6184SRyusuke Konishi 
719e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
720783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
721e339ad31SRyusuke Konishi 	if (!*pbh)
722783f6184SRyusuke Konishi 		return NULL;
723783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
724783f6184SRyusuke Konishi }
725783f6184SRyusuke Konishi 
726783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
727783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
728783f6184SRyusuke Konishi 				 char *data)
729783f6184SRyusuke Konishi {
730783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
731783f6184SRyusuke Konishi 
732783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
733783f6184SRyusuke Konishi 
734783f6184SRyusuke Konishi 	/* FS independent flags */
735783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
736783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
737783f6184SRyusuke Konishi #endif
738783f6184SRyusuke Konishi 
739783f6184SRyusuke Konishi 	nilfs_set_default_options(sbi, sbp);
740783f6184SRyusuke Konishi 
741783f6184SRyusuke Konishi 	sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
742783f6184SRyusuke Konishi 	sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
743783f6184SRyusuke Konishi 	sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
744783f6184SRyusuke Konishi 	sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
745783f6184SRyusuke Konishi 
746e339ad31SRyusuke Konishi 	return !parse_options(data, sb) ? -EINVAL : 0 ;
747783f6184SRyusuke Konishi }
748783f6184SRyusuke Konishi 
749783f6184SRyusuke Konishi /**
750783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
751783f6184SRyusuke Konishi  * @sb: super_block
752783f6184SRyusuke Konishi  * @data: mount options
753783f6184SRyusuke Konishi  * @silent: silent mode flag
754783f6184SRyusuke Konishi  * @nilfs: the_nilfs struct
755783f6184SRyusuke Konishi  *
756783f6184SRyusuke Konishi  * This function is called exclusively by bd_mount_mutex.
757783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
758783f6184SRyusuke Konishi  */
759783f6184SRyusuke Konishi static int
760783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent,
761783f6184SRyusuke Konishi 		 struct the_nilfs *nilfs)
762783f6184SRyusuke Konishi {
763783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi;
764783f6184SRyusuke Konishi 	struct inode *root;
765783f6184SRyusuke Konishi 	__u64 cno;
766783f6184SRyusuke Konishi 	int err;
767783f6184SRyusuke Konishi 
768783f6184SRyusuke Konishi 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
769783f6184SRyusuke Konishi 	if (!sbi)
770783f6184SRyusuke Konishi 		return -ENOMEM;
771783f6184SRyusuke Konishi 
772783f6184SRyusuke Konishi 	sb->s_fs_info = sbi;
773783f6184SRyusuke Konishi 
774783f6184SRyusuke Konishi 	get_nilfs(nilfs);
775783f6184SRyusuke Konishi 	sbi->s_nilfs = nilfs;
776783f6184SRyusuke Konishi 	sbi->s_super = sb;
777783f6184SRyusuke Konishi 
778783f6184SRyusuke Konishi 	err = init_nilfs(nilfs, sbi, (char *)data);
779783f6184SRyusuke Konishi 	if (err)
780783f6184SRyusuke Konishi 		goto failed_sbi;
781783f6184SRyusuke Konishi 
782783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_inode_lock);
783783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_dirty_files);
784783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_list);
785783f6184SRyusuke Konishi 
786783f6184SRyusuke Konishi 	/*
787783f6184SRyusuke Konishi 	 * Following initialization is overlapped because
788783f6184SRyusuke Konishi 	 * nilfs_sb_info structure has been cleared at the beginning.
789783f6184SRyusuke Konishi 	 * But we reserve them to keep our interest and make ready
790783f6184SRyusuke Konishi 	 * for the future change.
791783f6184SRyusuke Konishi 	 */
792783f6184SRyusuke Konishi 	get_random_bytes(&sbi->s_next_generation,
793783f6184SRyusuke Konishi 			 sizeof(sbi->s_next_generation));
794783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_next_gen_lock);
795783f6184SRyusuke Konishi 
796783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
797783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
798783f6184SRyusuke Konishi 	sb->s_root = NULL;
79961239230SRyusuke Konishi 	sb->s_time_gran = 1;
800783f6184SRyusuke Konishi 
801783f6184SRyusuke Konishi 	if (!nilfs_loaded(nilfs)) {
802783f6184SRyusuke Konishi 		err = load_nilfs(nilfs, sbi);
803783f6184SRyusuke Konishi 		if (err)
804783f6184SRyusuke Konishi 			goto failed_sbi;
805783f6184SRyusuke Konishi 	}
806783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
807783f6184SRyusuke Konishi 
808783f6184SRyusuke Konishi 	if (sb->s_flags & MS_RDONLY) {
809783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, SNAPSHOT)) {
8101f5abe7eSRyusuke Konishi 			err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
8111f5abe7eSRyusuke Konishi 						       sbi->s_snapshot_cno);
8121f5abe7eSRyusuke Konishi 			if (err < 0)
8131f5abe7eSRyusuke Konishi 				goto failed_sbi;
8141f5abe7eSRyusuke Konishi 			if (!err) {
815783f6184SRyusuke Konishi 				printk(KERN_ERR
816783f6184SRyusuke Konishi 				       "NILFS: The specified checkpoint is "
817783f6184SRyusuke Konishi 				       "not a snapshot "
818783f6184SRyusuke Konishi 				       "(checkpoint number=%llu).\n",
819783f6184SRyusuke Konishi 				       (unsigned long long)sbi->s_snapshot_cno);
820783f6184SRyusuke Konishi 				err = -EINVAL;
821783f6184SRyusuke Konishi 				goto failed_sbi;
822783f6184SRyusuke Konishi 			}
823783f6184SRyusuke Konishi 			cno = sbi->s_snapshot_cno;
824783f6184SRyusuke Konishi 		} else
825783f6184SRyusuke Konishi 			/* Read-only mount */
826783f6184SRyusuke Konishi 			sbi->s_snapshot_cno = cno;
827783f6184SRyusuke Konishi 	}
828783f6184SRyusuke Konishi 
829783f6184SRyusuke Konishi 	err = nilfs_attach_checkpoint(sbi, cno);
830783f6184SRyusuke Konishi 	if (err) {
831783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading a checkpoint"
832783f6184SRyusuke Konishi 		       " (checkpoint number=%llu).\n", (unsigned long long)cno);
833783f6184SRyusuke Konishi 		goto failed_sbi;
834783f6184SRyusuke Konishi 	}
835783f6184SRyusuke Konishi 
836783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
837cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
838783f6184SRyusuke Konishi 		if (err)
839783f6184SRyusuke Konishi 			goto failed_checkpoint;
840783f6184SRyusuke Konishi 	}
841783f6184SRyusuke Konishi 
842783f6184SRyusuke Konishi 	root = nilfs_iget(sb, NILFS_ROOT_INO);
843783f6184SRyusuke Konishi 	if (IS_ERR(root)) {
844783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
845783f6184SRyusuke Konishi 		err = PTR_ERR(root);
846783f6184SRyusuke Konishi 		goto failed_segctor;
847783f6184SRyusuke Konishi 	}
848783f6184SRyusuke Konishi 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
849783f6184SRyusuke Konishi 		iput(root);
850783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
851783f6184SRyusuke Konishi 		err = -EINVAL;
852783f6184SRyusuke Konishi 		goto failed_segctor;
853783f6184SRyusuke Konishi 	}
854783f6184SRyusuke Konishi 	sb->s_root = d_alloc_root(root);
855783f6184SRyusuke Konishi 	if (!sb->s_root) {
856783f6184SRyusuke Konishi 		iput(root);
857783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root dentry failed\n");
858783f6184SRyusuke Konishi 		err = -ENOMEM;
859783f6184SRyusuke Konishi 		goto failed_segctor;
860783f6184SRyusuke Konishi 	}
861783f6184SRyusuke Konishi 
862783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
863783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
864783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
865783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
866783f6184SRyusuke Konishi 	}
867783f6184SRyusuke Konishi 
868783f6184SRyusuke Konishi 	err = nilfs_mark_recovery_complete(sbi);
869783f6184SRyusuke Konishi 	if (unlikely(err)) {
870783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: recovery failed.\n");
871783f6184SRyusuke Konishi 		goto failed_root;
872783f6184SRyusuke Konishi 	}
873783f6184SRyusuke Konishi 
874783f6184SRyusuke Konishi 	return 0;
875783f6184SRyusuke Konishi 
876783f6184SRyusuke Konishi  failed_root:
877783f6184SRyusuke Konishi 	dput(sb->s_root);
878783f6184SRyusuke Konishi 	sb->s_root = NULL;
879783f6184SRyusuke Konishi 
880783f6184SRyusuke Konishi  failed_segctor:
881783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
882783f6184SRyusuke Konishi 
883783f6184SRyusuke Konishi  failed_checkpoint:
884783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
885783f6184SRyusuke Konishi 
886783f6184SRyusuke Konishi  failed_sbi:
887783f6184SRyusuke Konishi 	put_nilfs(nilfs);
888783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
889783f6184SRyusuke Konishi 	kfree(sbi);
890783f6184SRyusuke Konishi 	return err;
891783f6184SRyusuke Konishi }
892783f6184SRyusuke Konishi 
893783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
894783f6184SRyusuke Konishi {
895783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
896783f6184SRyusuke Konishi 	struct nilfs_super_block *sbp;
897783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
898783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
899783f6184SRyusuke Konishi 	struct nilfs_mount_options old_opts;
900783f6184SRyusuke Konishi 	int err;
901783f6184SRyusuke Konishi 
902783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
903783f6184SRyusuke Konishi 	old_opts.mount_opt = sbi->s_mount_opt;
904783f6184SRyusuke Konishi 	old_opts.snapshot_cno = sbi->s_snapshot_cno;
905783f6184SRyusuke Konishi 
906783f6184SRyusuke Konishi 	if (!parse_options(data, sb)) {
907783f6184SRyusuke Konishi 		err = -EINVAL;
908783f6184SRyusuke Konishi 		goto restore_opts;
909783f6184SRyusuke Konishi 	}
910783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
911783f6184SRyusuke Konishi 
912783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) &&
913783f6184SRyusuke Konishi 	    sbi->s_snapshot_cno != old_opts.snapshot_cno) {
914783f6184SRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
915783f6184SRyusuke Konishi 		       "remount to a different snapshot. \n",
916783f6184SRyusuke Konishi 		       sb->s_id);
917783f6184SRyusuke Konishi 		err = -EINVAL;
918783f6184SRyusuke Konishi 		goto restore_opts;
919783f6184SRyusuke Konishi 	}
920783f6184SRyusuke Konishi 
921783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
922783f6184SRyusuke Konishi 		goto out;
923783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
924783f6184SRyusuke Konishi 		/* Shutting down the segment constructor */
925783f6184SRyusuke Konishi 		nilfs_detach_segment_constructor(sbi);
926783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
927783f6184SRyusuke Konishi 
928783f6184SRyusuke Konishi 		sbi->s_snapshot_cno = nilfs_last_cno(nilfs);
929783f6184SRyusuke Konishi 		/* nilfs_set_opt(sbi, SNAPSHOT); */
930783f6184SRyusuke Konishi 
931783f6184SRyusuke Konishi 		/*
932783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
933783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
934783f6184SRyusuke Konishi 		 */
935783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
936e339ad31SRyusuke Konishi 		sbp = nilfs->ns_sbp[0];
937783f6184SRyusuke Konishi 		if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) &&
938783f6184SRyusuke Konishi 		    (nilfs->ns_mount_state & NILFS_VALID_FS))
939783f6184SRyusuke Konishi 			sbp->s_state = cpu_to_le16(nilfs->ns_mount_state);
940783f6184SRyusuke Konishi 		sbp->s_mtime = cpu_to_le64(get_seconds());
941e339ad31SRyusuke Konishi 		nilfs_commit_super(sbi, 1);
942783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
943783f6184SRyusuke Konishi 	} else {
944783f6184SRyusuke Konishi 		/*
945783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
946783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
947783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
948783f6184SRyusuke Konishi 		 */
949783f6184SRyusuke Konishi 		down(&sb->s_bdev->bd_mount_sem);
950783f6184SRyusuke Konishi 		/* Check existing RW-mount */
951783f6184SRyusuke Konishi 		if (test_exclusive_mount(sb->s_type, sb->s_bdev, 0)) {
952783f6184SRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
953783f6184SRyusuke Konishi 			       "remount because a RW-mount exists.\n",
954783f6184SRyusuke Konishi 			       sb->s_id);
955783f6184SRyusuke Konishi 			err = -EBUSY;
956783f6184SRyusuke Konishi 			goto rw_remount_failed;
957783f6184SRyusuke Konishi 		}
958783f6184SRyusuke Konishi 		if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) {
959783f6184SRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
960783f6184SRyusuke Konishi 			       "remount because the current RO-mount is not "
961783f6184SRyusuke Konishi 			       "the latest one.\n",
962783f6184SRyusuke Konishi 			       sb->s_id);
963783f6184SRyusuke Konishi 			err = -EINVAL;
964783f6184SRyusuke Konishi 			goto rw_remount_failed;
965783f6184SRyusuke Konishi 		}
966783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
967783f6184SRyusuke Konishi 		nilfs_clear_opt(sbi, SNAPSHOT);
968783f6184SRyusuke Konishi 		sbi->s_snapshot_cno = 0;
969783f6184SRyusuke Konishi 
970cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
971783f6184SRyusuke Konishi 		if (err)
972783f6184SRyusuke Konishi 			goto rw_remount_failed;
973783f6184SRyusuke Konishi 
974783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
975783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
976783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
977783f6184SRyusuke Konishi 
978783f6184SRyusuke Konishi 		up(&sb->s_bdev->bd_mount_sem);
979783f6184SRyusuke Konishi 	}
980783f6184SRyusuke Konishi  out:
981783f6184SRyusuke Konishi 	return 0;
982783f6184SRyusuke Konishi 
983783f6184SRyusuke Konishi  rw_remount_failed:
984783f6184SRyusuke Konishi 	up(&sb->s_bdev->bd_mount_sem);
985783f6184SRyusuke Konishi  restore_opts:
986783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
987783f6184SRyusuke Konishi 	sbi->s_mount_opt = old_opts.mount_opt;
988783f6184SRyusuke Konishi 	sbi->s_snapshot_cno = old_opts.snapshot_cno;
989783f6184SRyusuke Konishi 	return err;
990783f6184SRyusuke Konishi }
991783f6184SRyusuke Konishi 
992783f6184SRyusuke Konishi struct nilfs_super_data {
993783f6184SRyusuke Konishi 	struct block_device *bdev;
994783f6184SRyusuke Konishi 	__u64 cno;
995783f6184SRyusuke Konishi 	int flags;
996783f6184SRyusuke Konishi };
997783f6184SRyusuke Konishi 
998783f6184SRyusuke Konishi /**
999783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1000783f6184SRyusuke Konishi  * @data: mount options
1001783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1002783f6184SRyusuke Konishi  */
1003783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1004783f6184SRyusuke Konishi {
1005783f6184SRyusuke Konishi 	char *p, *options = data;
1006783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1007783f6184SRyusuke Konishi 	int option, token;
1008783f6184SRyusuke Konishi 	int ret = 0;
1009783f6184SRyusuke Konishi 
1010783f6184SRyusuke Konishi 	do {
1011783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1012783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1013783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1014783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
1015783f6184SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY))
1016783f6184SRyusuke Konishi 					ret++;
1017783f6184SRyusuke Konishi 				else {
1018783f6184SRyusuke Konishi 					ret = match_int(&args[0], &option);
1019783f6184SRyusuke Konishi 					if (!ret) {
1020783f6184SRyusuke Konishi 						if (option > 0)
1021783f6184SRyusuke Konishi 							sd->cno = option;
1022783f6184SRyusuke Konishi 						else
1023783f6184SRyusuke Konishi 							ret++;
1024783f6184SRyusuke Konishi 					}
1025783f6184SRyusuke Konishi 				}
1026783f6184SRyusuke Konishi 			}
1027783f6184SRyusuke Konishi 			if (ret)
1028783f6184SRyusuke Konishi 				printk(KERN_ERR
1029783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
1030783f6184SRyusuke Konishi 		}
1031783f6184SRyusuke Konishi 		if (!options)
1032783f6184SRyusuke Konishi 			break;
1033783f6184SRyusuke Konishi 		BUG_ON(options == data);
1034783f6184SRyusuke Konishi 		*(options - 1) = ',';
1035783f6184SRyusuke Konishi 	} while (!ret);
1036783f6184SRyusuke Konishi 	return ret;
1037783f6184SRyusuke Konishi }
1038783f6184SRyusuke Konishi 
1039783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1040783f6184SRyusuke Konishi {
1041783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1042783f6184SRyusuke Konishi 
1043783f6184SRyusuke Konishi 	s->s_bdev = sd->bdev;
1044783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1045783f6184SRyusuke Konishi 	return 0;
1046783f6184SRyusuke Konishi }
1047783f6184SRyusuke Konishi 
1048783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1049783f6184SRyusuke Konishi {
1050783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1051783f6184SRyusuke Konishi 
1052783f6184SRyusuke Konishi 	return s->s_bdev == sd->bdev;
1053783f6184SRyusuke Konishi }
1054783f6184SRyusuke Konishi 
1055783f6184SRyusuke Konishi static int nilfs_test_bdev_super2(struct super_block *s, void *data)
1056783f6184SRyusuke Konishi {
1057783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1058783f6184SRyusuke Konishi 	int ret;
1059783f6184SRyusuke Konishi 
1060783f6184SRyusuke Konishi 	if (s->s_bdev != sd->bdev)
1061783f6184SRyusuke Konishi 		return 0;
1062783f6184SRyusuke Konishi 
1063783f6184SRyusuke Konishi 	if (!((s->s_flags | sd->flags) & MS_RDONLY))
1064783f6184SRyusuke Konishi 		return 1; /* Reuse an old R/W-mode super_block */
1065783f6184SRyusuke Konishi 
1066783f6184SRyusuke Konishi 	if (s->s_flags & sd->flags & MS_RDONLY) {
1067783f6184SRyusuke Konishi 		if (down_read_trylock(&s->s_umount)) {
1068783f6184SRyusuke Konishi 			ret = s->s_root &&
1069783f6184SRyusuke Konishi 				(sd->cno == NILFS_SB(s)->s_snapshot_cno);
1070783f6184SRyusuke Konishi 			up_read(&s->s_umount);
1071783f6184SRyusuke Konishi 			/*
1072783f6184SRyusuke Konishi 			 * This path is locked with sb_lock by sget().
1073783f6184SRyusuke Konishi 			 * So, drop_super() causes deadlock.
1074783f6184SRyusuke Konishi 			 */
1075783f6184SRyusuke Konishi 			return ret;
1076783f6184SRyusuke Konishi 		}
1077783f6184SRyusuke Konishi 	}
1078783f6184SRyusuke Konishi 	return 0;
1079783f6184SRyusuke Konishi }
1080783f6184SRyusuke Konishi 
1081783f6184SRyusuke Konishi static int
1082783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags,
1083783f6184SRyusuke Konishi 	     const char *dev_name, void *data, struct vfsmount *mnt)
1084783f6184SRyusuke Konishi {
1085783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
1086783f6184SRyusuke Konishi 	struct super_block *s, *s2;
1087783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = NULL;
1088783f6184SRyusuke Konishi 	int err, need_to_close = 1;
1089783f6184SRyusuke Konishi 
1090783f6184SRyusuke Konishi 	sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type);
1091783f6184SRyusuke Konishi 	if (IS_ERR(sd.bdev))
1092783f6184SRyusuke Konishi 		return PTR_ERR(sd.bdev);
1093783f6184SRyusuke Konishi 
1094783f6184SRyusuke Konishi 	/*
1095783f6184SRyusuke Konishi 	 * To get mount instance using sget() vfs-routine, NILFS needs
1096783f6184SRyusuke Konishi 	 * much more information than normal filesystems to identify mount
1097783f6184SRyusuke Konishi 	 * instance.  For snapshot mounts, not only a mount type (ro-mount
1098783f6184SRyusuke Konishi 	 * or rw-mount) but also a checkpoint number is required.
1099783f6184SRyusuke Konishi 	 * The results are passed in sget() using nilfs_super_data.
1100783f6184SRyusuke Konishi 	 */
1101783f6184SRyusuke Konishi 	sd.cno = 0;
1102783f6184SRyusuke Konishi 	sd.flags = flags;
1103783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1104783f6184SRyusuke Konishi 		err = -EINVAL;
1105783f6184SRyusuke Konishi 		goto failed;
1106783f6184SRyusuke Konishi 	}
1107783f6184SRyusuke Konishi 
1108783f6184SRyusuke Konishi 	/*
1109783f6184SRyusuke Konishi 	 * once the super is inserted into the list by sget, s_umount
1110783f6184SRyusuke Konishi 	 * will protect the lockfs code from trying to start a snapshot
1111783f6184SRyusuke Konishi 	 * while we are mounting
1112783f6184SRyusuke Konishi 	 */
1113783f6184SRyusuke Konishi 	down(&sd.bdev->bd_mount_sem);
1114783f6184SRyusuke Konishi 	if (!sd.cno &&
1115783f6184SRyusuke Konishi 	    (err = test_exclusive_mount(fs_type, sd.bdev, flags ^ MS_RDONLY))) {
1116783f6184SRyusuke Konishi 		err = (err < 0) ? : -EBUSY;
1117783f6184SRyusuke Konishi 		goto failed_unlock;
1118783f6184SRyusuke Konishi 	}
1119783f6184SRyusuke Konishi 
1120783f6184SRyusuke Konishi 	/*
1121783f6184SRyusuke Konishi 	 * Phase-1: search any existent instance and get the_nilfs
1122783f6184SRyusuke Konishi 	 */
1123783f6184SRyusuke Konishi 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
1124783f6184SRyusuke Konishi 	if (IS_ERR(s))
1125783f6184SRyusuke Konishi 		goto error_s;
1126783f6184SRyusuke Konishi 
1127783f6184SRyusuke Konishi 	if (!s->s_root) {
1128783f6184SRyusuke Konishi 		err = -ENOMEM;
1129783f6184SRyusuke Konishi 		nilfs = alloc_nilfs(sd.bdev);
1130783f6184SRyusuke Konishi 		if (!nilfs)
1131783f6184SRyusuke Konishi 			goto cancel_new;
1132783f6184SRyusuke Konishi 	} else {
1133783f6184SRyusuke Konishi 		struct nilfs_sb_info *sbi = NILFS_SB(s);
1134783f6184SRyusuke Konishi 
1135783f6184SRyusuke Konishi 		/*
1136783f6184SRyusuke Konishi 		 * s_umount protects super_block from unmount process;
1137783f6184SRyusuke Konishi 		 * It covers pointers of nilfs_sb_info and the_nilfs.
1138783f6184SRyusuke Konishi 		 */
1139783f6184SRyusuke Konishi 		nilfs = sbi->s_nilfs;
1140783f6184SRyusuke Konishi 		get_nilfs(nilfs);
1141783f6184SRyusuke Konishi 		up_write(&s->s_umount);
1142783f6184SRyusuke Konishi 
1143783f6184SRyusuke Konishi 		/*
1144783f6184SRyusuke Konishi 		 * Phase-2: search specified snapshot or R/W mode super_block
1145783f6184SRyusuke Konishi 		 */
1146783f6184SRyusuke Konishi 		if (!sd.cno)
1147783f6184SRyusuke Konishi 			/* trying to get the latest checkpoint.  */
1148783f6184SRyusuke Konishi 			sd.cno = nilfs_last_cno(nilfs);
1149783f6184SRyusuke Konishi 
1150783f6184SRyusuke Konishi 		s2 = sget(fs_type, nilfs_test_bdev_super2,
1151783f6184SRyusuke Konishi 			  nilfs_set_bdev_super, &sd);
1152783f6184SRyusuke Konishi 		deactivate_super(s);
1153783f6184SRyusuke Konishi 		/*
1154783f6184SRyusuke Konishi 		 * Although deactivate_super() invokes close_bdev_exclusive() at
1155783f6184SRyusuke Konishi 		 * kill_block_super().  Here, s is an existent mount; we need
1156783f6184SRyusuke Konishi 		 * one more close_bdev_exclusive() call.
1157783f6184SRyusuke Konishi 		 */
1158783f6184SRyusuke Konishi 		s = s2;
1159783f6184SRyusuke Konishi 		if (IS_ERR(s))
1160783f6184SRyusuke Konishi 			goto error_s;
1161783f6184SRyusuke Konishi 	}
1162783f6184SRyusuke Konishi 
1163783f6184SRyusuke Konishi 	if (!s->s_root) {
1164783f6184SRyusuke Konishi 		char b[BDEVNAME_SIZE];
1165783f6184SRyusuke Konishi 
1166783f6184SRyusuke Konishi 		s->s_flags = flags;
1167783f6184SRyusuke Konishi 		strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
1168783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1169783f6184SRyusuke Konishi 
1170783f6184SRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs);
1171783f6184SRyusuke Konishi 		if (err)
1172783f6184SRyusuke Konishi 			goto cancel_new;
1173783f6184SRyusuke Konishi 
1174783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1175783f6184SRyusuke Konishi 		need_to_close = 0;
1176783f6184SRyusuke Konishi 	} else if (!(s->s_flags & MS_RDONLY)) {
1177783f6184SRyusuke Konishi 		err = -EBUSY;
1178783f6184SRyusuke Konishi 	}
1179783f6184SRyusuke Konishi 
1180783f6184SRyusuke Konishi 	up(&sd.bdev->bd_mount_sem);
1181783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1182783f6184SRyusuke Konishi 	if (need_to_close)
1183783f6184SRyusuke Konishi 		close_bdev_exclusive(sd.bdev, flags);
1184783f6184SRyusuke Konishi 	simple_set_mnt(mnt, s);
1185783f6184SRyusuke Konishi 	return 0;
1186783f6184SRyusuke Konishi 
1187783f6184SRyusuke Konishi  error_s:
1188783f6184SRyusuke Konishi 	up(&sd.bdev->bd_mount_sem);
1189783f6184SRyusuke Konishi 	if (nilfs)
1190783f6184SRyusuke Konishi 		put_nilfs(nilfs);
1191783f6184SRyusuke Konishi 	close_bdev_exclusive(sd.bdev, flags);
1192783f6184SRyusuke Konishi 	return PTR_ERR(s);
1193783f6184SRyusuke Konishi 
1194783f6184SRyusuke Konishi  failed_unlock:
1195783f6184SRyusuke Konishi 	up(&sd.bdev->bd_mount_sem);
1196783f6184SRyusuke Konishi  failed:
1197783f6184SRyusuke Konishi 	close_bdev_exclusive(sd.bdev, flags);
1198783f6184SRyusuke Konishi 
1199783f6184SRyusuke Konishi 	return err;
1200783f6184SRyusuke Konishi 
1201783f6184SRyusuke Konishi  cancel_new:
1202783f6184SRyusuke Konishi 	/* Abandoning the newly allocated superblock */
1203783f6184SRyusuke Konishi 	up(&sd.bdev->bd_mount_sem);
1204783f6184SRyusuke Konishi 	if (nilfs)
1205783f6184SRyusuke Konishi 		put_nilfs(nilfs);
1206783f6184SRyusuke Konishi 	up_write(&s->s_umount);
1207783f6184SRyusuke Konishi 	deactivate_super(s);
1208783f6184SRyusuke Konishi 	/*
1209783f6184SRyusuke Konishi 	 * deactivate_super() invokes close_bdev_exclusive().
1210783f6184SRyusuke Konishi 	 * We must finish all post-cleaning before this call;
1211783f6184SRyusuke Konishi 	 * put_nilfs() and unlocking bd_mount_sem need the block device.
1212783f6184SRyusuke Konishi 	 */
1213783f6184SRyusuke Konishi 	return err;
1214783f6184SRyusuke Konishi }
1215783f6184SRyusuke Konishi 
1216783f6184SRyusuke Konishi static int nilfs_test_bdev_super3(struct super_block *s, void *data)
1217783f6184SRyusuke Konishi {
1218783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1219783f6184SRyusuke Konishi 	int ret;
1220783f6184SRyusuke Konishi 
1221783f6184SRyusuke Konishi 	if (s->s_bdev != sd->bdev)
1222783f6184SRyusuke Konishi 		return 0;
1223783f6184SRyusuke Konishi 	if (down_read_trylock(&s->s_umount)) {
1224783f6184SRyusuke Konishi 		ret = (s->s_flags & MS_RDONLY) && s->s_root &&
1225783f6184SRyusuke Konishi 			nilfs_test_opt(NILFS_SB(s), SNAPSHOT);
1226783f6184SRyusuke Konishi 		up_read(&s->s_umount);
1227783f6184SRyusuke Konishi 		if (ret)
1228783f6184SRyusuke Konishi 			return 0; /* ignore snapshot mounts */
1229783f6184SRyusuke Konishi 	}
1230783f6184SRyusuke Konishi 	return !((sd->flags ^ s->s_flags) & MS_RDONLY);
1231783f6184SRyusuke Konishi }
1232783f6184SRyusuke Konishi 
1233783f6184SRyusuke Konishi static int __false_bdev_super(struct super_block *s, void *data)
1234783f6184SRyusuke Konishi {
1235783f6184SRyusuke Konishi #if 0 /* XXX: workaround for lock debug. This is not good idea */
1236783f6184SRyusuke Konishi 	up_write(&s->s_umount);
1237783f6184SRyusuke Konishi #endif
1238783f6184SRyusuke Konishi 	return -EFAULT;
1239783f6184SRyusuke Konishi }
1240783f6184SRyusuke Konishi 
1241783f6184SRyusuke Konishi /**
1242783f6184SRyusuke Konishi  * test_exclusive_mount - check whether an exclusive RW/RO mount exists or not.
1243783f6184SRyusuke Konishi  * fs_type: filesystem type
1244783f6184SRyusuke Konishi  * bdev: block device
1245783f6184SRyusuke Konishi  * flag: 0 (check rw-mount) or MS_RDONLY (check ro-mount)
1246783f6184SRyusuke Konishi  * res: pointer to an integer to store result
1247783f6184SRyusuke Konishi  *
1248783f6184SRyusuke Konishi  * This function must be called within a section protected by bd_mount_mutex.
1249783f6184SRyusuke Konishi  */
1250783f6184SRyusuke Konishi static int test_exclusive_mount(struct file_system_type *fs_type,
1251783f6184SRyusuke Konishi 				struct block_device *bdev, int flags)
1252783f6184SRyusuke Konishi {
1253783f6184SRyusuke Konishi 	struct super_block *s;
1254783f6184SRyusuke Konishi 	struct nilfs_super_data sd = { .flags = flags, .bdev = bdev };
1255783f6184SRyusuke Konishi 
1256783f6184SRyusuke Konishi 	s = sget(fs_type, nilfs_test_bdev_super3, __false_bdev_super, &sd);
1257783f6184SRyusuke Konishi 	if (IS_ERR(s)) {
1258783f6184SRyusuke Konishi 		if (PTR_ERR(s) != -EFAULT)
1259783f6184SRyusuke Konishi 			return PTR_ERR(s);
1260783f6184SRyusuke Konishi 		return 0;  /* Not found */
1261783f6184SRyusuke Konishi 	}
1262783f6184SRyusuke Konishi 	up_write(&s->s_umount);
1263783f6184SRyusuke Konishi 	deactivate_super(s);
1264783f6184SRyusuke Konishi 	return 1;  /* Found */
1265783f6184SRyusuke Konishi }
1266783f6184SRyusuke Konishi 
1267783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1268783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1269783f6184SRyusuke Konishi 	.name     = "nilfs2",
1270783f6184SRyusuke Konishi 	.get_sb   = nilfs_get_sb,
1271783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1272783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1273783f6184SRyusuke Konishi };
1274783f6184SRyusuke Konishi 
1275783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1276783f6184SRyusuke Konishi {
1277783f6184SRyusuke Konishi 	int err;
1278783f6184SRyusuke Konishi 
1279783f6184SRyusuke Konishi 	err = nilfs_init_inode_cache();
1280783f6184SRyusuke Konishi 	if (err)
1281783f6184SRyusuke Konishi 		goto failed;
1282783f6184SRyusuke Konishi 
1283783f6184SRyusuke Konishi 	err = nilfs_init_transaction_cache();
1284783f6184SRyusuke Konishi 	if (err)
1285783f6184SRyusuke Konishi 		goto failed_inode_cache;
1286783f6184SRyusuke Konishi 
1287783f6184SRyusuke Konishi 	err = nilfs_init_segbuf_cache();
1288783f6184SRyusuke Konishi 	if (err)
1289783f6184SRyusuke Konishi 		goto failed_transaction_cache;
1290783f6184SRyusuke Konishi 
1291783f6184SRyusuke Konishi 	err = nilfs_btree_path_cache_init();
1292783f6184SRyusuke Konishi 	if (err)
1293783f6184SRyusuke Konishi 		goto failed_segbuf_cache;
1294783f6184SRyusuke Konishi 
1295783f6184SRyusuke Konishi 	err = register_filesystem(&nilfs_fs_type);
1296783f6184SRyusuke Konishi 	if (err)
1297783f6184SRyusuke Konishi 		goto failed_btree_path_cache;
1298783f6184SRyusuke Konishi 
1299783f6184SRyusuke Konishi 	return 0;
1300783f6184SRyusuke Konishi 
1301783f6184SRyusuke Konishi  failed_btree_path_cache:
1302783f6184SRyusuke Konishi 	nilfs_btree_path_cache_destroy();
1303783f6184SRyusuke Konishi 
1304783f6184SRyusuke Konishi  failed_segbuf_cache:
1305783f6184SRyusuke Konishi 	nilfs_destroy_segbuf_cache();
1306783f6184SRyusuke Konishi 
1307783f6184SRyusuke Konishi  failed_transaction_cache:
1308783f6184SRyusuke Konishi 	nilfs_destroy_transaction_cache();
1309783f6184SRyusuke Konishi 
1310783f6184SRyusuke Konishi  failed_inode_cache:
1311783f6184SRyusuke Konishi 	nilfs_destroy_inode_cache();
1312783f6184SRyusuke Konishi 
1313783f6184SRyusuke Konishi  failed:
1314783f6184SRyusuke Konishi 	return err;
1315783f6184SRyusuke Konishi }
1316783f6184SRyusuke Konishi 
1317783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1318783f6184SRyusuke Konishi {
1319783f6184SRyusuke Konishi 	nilfs_destroy_segbuf_cache();
1320783f6184SRyusuke Konishi 	nilfs_destroy_transaction_cache();
1321783f6184SRyusuke Konishi 	nilfs_destroy_inode_cache();
1322783f6184SRyusuke Konishi 	nilfs_btree_path_cache_destroy();
1323783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1324783f6184SRyusuke Konishi }
1325783f6184SRyusuke Konishi 
1326783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1327783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1328