xref: /openbmc/linux/fs/nilfs2/super.c (revision 7ecaa46cfea453238a369b3019739d50ff5d7c37)
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>
53b58a285bSJiro SEKIBA #include <linux/seq_file.h>
54b58a285bSJiro SEKIBA #include <linux/mount.h>
55783f6184SRyusuke Konishi #include "nilfs.h"
56783f6184SRyusuke Konishi #include "mdt.h"
57783f6184SRyusuke Konishi #include "alloc.h"
58783f6184SRyusuke Konishi #include "page.h"
59783f6184SRyusuke Konishi #include "cpfile.h"
60783f6184SRyusuke Konishi #include "ifile.h"
61783f6184SRyusuke Konishi #include "dat.h"
62783f6184SRyusuke Konishi #include "segment.h"
63783f6184SRyusuke Konishi #include "segbuf.h"
64783f6184SRyusuke Konishi 
65783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
66783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
67783f6184SRyusuke Konishi 		   "(NILFS)");
68783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
69783f6184SRyusuke Konishi 
7041c88bd7SLi Hong struct kmem_cache *nilfs_inode_cachep;
7141c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
7241c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
7341c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
7441c88bd7SLi Hong 
75783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
76783f6184SRyusuke Konishi 
77783f6184SRyusuke Konishi /**
78783f6184SRyusuke Konishi  * nilfs_error() - report failure condition on a filesystem
79783f6184SRyusuke Konishi  *
80783f6184SRyusuke Konishi  * nilfs_error() sets an ERROR_FS flag on the superblock as well as
81783f6184SRyusuke Konishi  * reporting an error message.  It should be called when NILFS detects
82783f6184SRyusuke Konishi  * incoherences or defects of meta data on disk.  As for sustainable
83783f6184SRyusuke Konishi  * errors such as a single-shot I/O error, nilfs_warning() or the printk()
84783f6184SRyusuke Konishi  * function should be used instead.
85783f6184SRyusuke Konishi  *
86783f6184SRyusuke Konishi  * The segment constructor must not call this function because it can
87783f6184SRyusuke Konishi  * kill itself.
88783f6184SRyusuke Konishi  */
89783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function,
90783f6184SRyusuke Konishi 		 const char *fmt, ...)
91783f6184SRyusuke Konishi {
92783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
93783f6184SRyusuke Konishi 	va_list args;
94783f6184SRyusuke Konishi 
95783f6184SRyusuke Konishi 	va_start(args, fmt);
96783f6184SRyusuke Konishi 	printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
97783f6184SRyusuke Konishi 	vprintk(fmt, args);
98783f6184SRyusuke Konishi 	printk("\n");
99783f6184SRyusuke Konishi 	va_end(args);
100783f6184SRyusuke Konishi 
101783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
102783f6184SRyusuke Konishi 		struct the_nilfs *nilfs = sbi->s_nilfs;
103783f6184SRyusuke Konishi 
104783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
105783f6184SRyusuke Konishi 		if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
106783f6184SRyusuke Konishi 			nilfs->ns_mount_state |= NILFS_ERROR_FS;
107e339ad31SRyusuke Konishi 			nilfs->ns_sbp[0]->s_state |=
108e339ad31SRyusuke Konishi 				cpu_to_le16(NILFS_ERROR_FS);
109e339ad31SRyusuke Konishi 			nilfs_commit_super(sbi, 1);
110783f6184SRyusuke Konishi 		}
111783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
112783f6184SRyusuke Konishi 
113783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, ERRORS_RO)) {
114783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
115783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
116783f6184SRyusuke Konishi 		}
117783f6184SRyusuke Konishi 	}
118783f6184SRyusuke Konishi 
119783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
120783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
121783f6184SRyusuke Konishi 		      sb->s_id);
122783f6184SRyusuke Konishi }
123783f6184SRyusuke Konishi 
124783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function,
125783f6184SRyusuke Konishi 		   const char *fmt, ...)
126783f6184SRyusuke Konishi {
127783f6184SRyusuke Konishi 	va_list args;
128783f6184SRyusuke Konishi 
129783f6184SRyusuke Konishi 	va_start(args, fmt);
130783f6184SRyusuke Konishi 	printk(KERN_WARNING "NILFS warning (device %s): %s: ",
131783f6184SRyusuke Konishi 	       sb->s_id, function);
132783f6184SRyusuke Konishi 	vprintk(fmt, args);
133783f6184SRyusuke Konishi 	printk("\n");
134783f6184SRyusuke Konishi 	va_end(args);
135783f6184SRyusuke Konishi }
136783f6184SRyusuke Konishi 
137783f6184SRyusuke Konishi 
138a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
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;
148a53b4751SRyusuke Konishi 	nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
149783f6184SRyusuke Konishi 	return &ii->vfs_inode;
150783f6184SRyusuke Konishi }
151783f6184SRyusuke Konishi 
152a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
153a53b4751SRyusuke Konishi {
154a53b4751SRyusuke Konishi 	return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
155a53b4751SRyusuke Konishi }
156a53b4751SRyusuke Konishi 
157783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode)
158783f6184SRyusuke Konishi {
159783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
160783f6184SRyusuke Konishi }
161783f6184SRyusuke Konishi 
162783f6184SRyusuke Konishi static void nilfs_clear_inode(struct inode *inode)
163783f6184SRyusuke Konishi {
164783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(inode);
165783f6184SRyusuke Konishi 
166783f6184SRyusuke Konishi 	/*
167783f6184SRyusuke Konishi 	 * Free resources allocated in nilfs_read_inode(), here.
168783f6184SRyusuke Konishi 	 */
169a2e7d2dfSRyusuke Konishi 	BUG_ON(!list_empty(&ii->i_dirty));
170783f6184SRyusuke Konishi 	brelse(ii->i_bh);
171783f6184SRyusuke Konishi 	ii->i_bh = NULL;
172783f6184SRyusuke Konishi 
173783f6184SRyusuke Konishi 	if (test_bit(NILFS_I_BMAP, &ii->i_state))
174783f6184SRyusuke Konishi 		nilfs_bmap_clear(ii->i_bmap);
175783f6184SRyusuke Konishi 
176783f6184SRyusuke Konishi 	nilfs_btnode_cache_clear(&ii->i_btnode_cache);
177783f6184SRyusuke Konishi }
178783f6184SRyusuke Konishi 
179e339ad31SRyusuke Konishi static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb)
180783f6184SRyusuke Konishi {
181783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
182783f6184SRyusuke Konishi 	int err;
183783f6184SRyusuke Konishi 	int barrier_done = 0;
184783f6184SRyusuke Konishi 
185783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, BARRIER)) {
186e339ad31SRyusuke Konishi 		set_buffer_ordered(nilfs->ns_sbh[0]);
187783f6184SRyusuke Konishi 		barrier_done = 1;
188783f6184SRyusuke Konishi 	}
189783f6184SRyusuke Konishi  retry:
190e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
191e339ad31SRyusuke Konishi 	err = sync_dirty_buffer(nilfs->ns_sbh[0]);
192783f6184SRyusuke Konishi 	if (err == -EOPNOTSUPP && barrier_done) {
193783f6184SRyusuke Konishi 		nilfs_warning(sbi->s_super, __func__,
194783f6184SRyusuke Konishi 			      "barrier-based sync failed. "
195783f6184SRyusuke Konishi 			      "disabling barriers\n");
196783f6184SRyusuke Konishi 		nilfs_clear_opt(sbi, BARRIER);
197783f6184SRyusuke Konishi 		barrier_done = 0;
198e339ad31SRyusuke Konishi 		clear_buffer_ordered(nilfs->ns_sbh[0]);
199783f6184SRyusuke Konishi 		goto retry;
200783f6184SRyusuke Konishi 	}
201e339ad31SRyusuke Konishi 	if (unlikely(err)) {
202783f6184SRyusuke Konishi 		printk(KERN_ERR
203783f6184SRyusuke Konishi 		       "NILFS: unable to write superblock (err=%d)\n", err);
204e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
205e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
206e339ad31SRyusuke Konishi 			goto retry;
207e339ad31SRyusuke Konishi 		}
208e339ad31SRyusuke Konishi 	} else {
209e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
210e339ad31SRyusuke Konishi 
211e339ad31SRyusuke Konishi 		/*
212e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
213e339ad31SRyusuke Konishi 		 * written in superblock.
214e339ad31SRyusuke Konishi 		 */
215783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
216e339ad31SRyusuke Konishi 
217e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
218e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
219e339ad31SRyusuke Konishi 			sbp = NULL;
220e339ad31SRyusuke Konishi 			if (dupsb) {
221e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
222e339ad31SRyusuke Konishi 				if (!sync_dirty_buffer(nilfs->ns_sbh[1]))
223e339ad31SRyusuke Konishi 					sbp = nilfs->ns_sbp[1];
224e339ad31SRyusuke Konishi 			}
225e339ad31SRyusuke Konishi 		}
226e339ad31SRyusuke Konishi 		if (sbp) {
2272c2e52fcSRyusuke Konishi 			spin_lock(&nilfs->ns_last_segment_lock);
228e339ad31SRyusuke Konishi 			nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2292c2e52fcSRyusuke Konishi 			spin_unlock(&nilfs->ns_last_segment_lock);
230783f6184SRyusuke Konishi 		}
231e339ad31SRyusuke Konishi 	}
232783f6184SRyusuke Konishi 
233783f6184SRyusuke Konishi 	return err;
234783f6184SRyusuke Konishi }
235783f6184SRyusuke Konishi 
236e339ad31SRyusuke Konishi int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb)
237783f6184SRyusuke Konishi {
238783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
239e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
240783f6184SRyusuke Konishi 	sector_t nfreeblocks;
241e339ad31SRyusuke Konishi 	time_t t;
242783f6184SRyusuke Konishi 	int err;
243783f6184SRyusuke Konishi 
244783f6184SRyusuke Konishi 	/* nilfs->sem must be locked by the caller. */
24534cb9b5cSRyusuke Konishi 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
24634cb9b5cSRyusuke Konishi 		if (sbp[1] && sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC))
247e339ad31SRyusuke Konishi 			nilfs_swap_super_block(nilfs);
248e339ad31SRyusuke Konishi 		else {
249e339ad31SRyusuke Konishi 			printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
250e339ad31SRyusuke Konishi 			       sbi->s_super->s_id);
251e339ad31SRyusuke Konishi 			return -EIO;
252e339ad31SRyusuke Konishi 		}
253e339ad31SRyusuke Konishi 	}
254783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
255783f6184SRyusuke Konishi 	if (unlikely(err)) {
256783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: failed to count free blocks\n");
257783f6184SRyusuke Konishi 		return err;
258783f6184SRyusuke Konishi 	}
259e339ad31SRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
260e339ad31SRyusuke Konishi 	sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
261e339ad31SRyusuke Konishi 	sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
262e339ad31SRyusuke Konishi 	sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
263e339ad31SRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
264e339ad31SRyusuke Konishi 
265e339ad31SRyusuke Konishi 	t = get_seconds();
266e339ad31SRyusuke Konishi 	nilfs->ns_sbwtime[0] = t;
267e339ad31SRyusuke Konishi 	sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks);
268e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
269e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
270e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
271e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
272e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
273e339ad31SRyusuke Konishi 	if (dupsb && sbp[1]) {
274e339ad31SRyusuke Konishi 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
275e339ad31SRyusuke Konishi 		nilfs->ns_sbwtime[1] = t;
276e339ad31SRyusuke Konishi 	}
277e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
278e339ad31SRyusuke Konishi 	return nilfs_sync_super(sbi, dupsb);
279783f6184SRyusuke Konishi }
280783f6184SRyusuke Konishi 
281*7ecaa46cSRyusuke Konishi /**
282*7ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
283*7ecaa46cSRyusuke Konishi  * @sbi: nilfs_sb_info to be unmounted or degraded to read-only
284*7ecaa46cSRyusuke Konishi  *
285*7ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
286*7ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
287*7ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
288*7ecaa46cSRyusuke Konishi  */
289*7ecaa46cSRyusuke Konishi int nilfs_cleanup_super(struct nilfs_sb_info *sbi)
290*7ecaa46cSRyusuke Konishi {
291*7ecaa46cSRyusuke Konishi 	struct nilfs_super_block **sbp = sbi->s_nilfs->ns_sbp;
292*7ecaa46cSRyusuke Konishi 	int ret;
293*7ecaa46cSRyusuke Konishi 
294*7ecaa46cSRyusuke Konishi 	sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state);
295*7ecaa46cSRyusuke Konishi 	ret = nilfs_commit_super(sbi, 1);
296*7ecaa46cSRyusuke Konishi 	return ret;
297*7ecaa46cSRyusuke Konishi }
298*7ecaa46cSRyusuke Konishi 
299783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
300783f6184SRyusuke Konishi {
301783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
302783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
303783f6184SRyusuke Konishi 
3046cfd0148SChristoph Hellwig 	lock_kernel();
3056cfd0148SChristoph Hellwig 
306783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
307783f6184SRyusuke Konishi 
308783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
309783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
310*7ecaa46cSRyusuke Konishi 		nilfs_cleanup_super(sbi);
311783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
312783f6184SRyusuke Konishi 	}
313e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
3143f82ff55SRyusuke Konishi 	if (nilfs->ns_current == sbi)
3153f82ff55SRyusuke Konishi 		nilfs->ns_current = NULL;
316e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
317783f6184SRyusuke Konishi 
318783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
319783f6184SRyusuke Konishi 	put_nilfs(sbi->s_nilfs);
320783f6184SRyusuke Konishi 	sbi->s_super = NULL;
321783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
3226dd47406SRyusuke Konishi 	nilfs_put_sbinfo(sbi);
3236cfd0148SChristoph Hellwig 
3246cfd0148SChristoph Hellwig 	unlock_kernel();
325783f6184SRyusuke Konishi }
326783f6184SRyusuke Konishi 
327783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
328783f6184SRyusuke Konishi {
3296233caa9SJiro SEKIBA 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
3306233caa9SJiro SEKIBA 	struct the_nilfs *nilfs = sbi->s_nilfs;
331783f6184SRyusuke Konishi 	int err = 0;
332783f6184SRyusuke Konishi 
333783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
334783f6184SRyusuke Konishi 	if (wait)
335783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
3366233caa9SJiro SEKIBA 
3376233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
338e605f0a7SRyusuke Konishi 	if (nilfs_sb_dirty(nilfs))
3396233caa9SJiro SEKIBA 		nilfs_commit_super(sbi, 1);
3406233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
3416233caa9SJiro SEKIBA 
342783f6184SRyusuke Konishi 	return err;
343783f6184SRyusuke Konishi }
344783f6184SRyusuke Konishi 
345783f6184SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
346783f6184SRyusuke Konishi {
347783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
348783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
349783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
350783f6184SRyusuke Konishi 	int err;
351783f6184SRyusuke Konishi 
352e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
353783f6184SRyusuke Konishi 	list_add(&sbi->s_list, &nilfs->ns_supers);
354e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
355783f6184SRyusuke Konishi 
35679739565SRyusuke Konishi 	sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
357783f6184SRyusuke Konishi 	if (!sbi->s_ifile)
358783f6184SRyusuke Konishi 		return -ENOMEM;
359783f6184SRyusuke Konishi 
3601154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
361783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
362783f6184SRyusuke Konishi 					  &bh_cp);
3631154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
364783f6184SRyusuke Konishi 	if (unlikely(err)) {
365783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
366783f6184SRyusuke Konishi 			printk(KERN_ERR
367783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
368783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
369783f6184SRyusuke Konishi 			       (unsigned long long)cno);
370783f6184SRyusuke Konishi 			err = -EINVAL;
371783f6184SRyusuke Konishi 		}
372783f6184SRyusuke Konishi 		goto failed;
373783f6184SRyusuke Konishi 	}
374783f6184SRyusuke Konishi 	err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
375783f6184SRyusuke Konishi 	if (unlikely(err))
376783f6184SRyusuke Konishi 		goto failed_bh;
377783f6184SRyusuke Konishi 	atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
378783f6184SRyusuke Konishi 	atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
379783f6184SRyusuke Konishi 
380783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
381783f6184SRyusuke Konishi 	return 0;
382783f6184SRyusuke Konishi 
383783f6184SRyusuke Konishi  failed_bh:
384783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
385783f6184SRyusuke Konishi  failed:
386783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
387783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
388783f6184SRyusuke Konishi 
389e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
390783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
391e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
392783f6184SRyusuke Konishi 
393783f6184SRyusuke Konishi 	return err;
394783f6184SRyusuke Konishi }
395783f6184SRyusuke Konishi 
396783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
397783f6184SRyusuke Konishi {
398783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
399783f6184SRyusuke Konishi 
400783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
401783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
402e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
403783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
404e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
405783f6184SRyusuke Konishi }
406783f6184SRyusuke Konishi 
407783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
408783f6184SRyusuke Konishi {
409783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
410783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
411c306af23SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
412c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
413783f6184SRyusuke Konishi 	unsigned long long blocks;
414783f6184SRyusuke Konishi 	unsigned long overhead;
415783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
416783f6184SRyusuke Konishi 	sector_t nfreeblocks;
417783f6184SRyusuke Konishi 	int err;
418783f6184SRyusuke Konishi 
419783f6184SRyusuke Konishi 	/*
420783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
421783f6184SRyusuke Konishi 	 *
422783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
423783f6184SRyusuke Konishi 	 * are excluded.
424783f6184SRyusuke Konishi 	 */
425783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
426783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
427783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
428783f6184SRyusuke Konishi 
429783f6184SRyusuke Konishi 	/*
430783f6184SRyusuke Konishi 	 * Compute the overhead
431783f6184SRyusuke Konishi 	 *
4327a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
433783f6184SRyusuke Konishi 	 * We must count them as the overhead.
434783f6184SRyusuke Konishi 	 */
435783f6184SRyusuke Konishi 	overhead = 0;
436783f6184SRyusuke Konishi 
437783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
438783f6184SRyusuke Konishi 	if (unlikely(err))
439783f6184SRyusuke Konishi 		return err;
440783f6184SRyusuke Konishi 
441783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
442783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
443783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
444783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
445783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
446783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
447783f6184SRyusuke Konishi 	buf->f_files = atomic_read(&sbi->s_inodes_count);
448783f6184SRyusuke Konishi 	buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
449783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
450c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
451c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
452c306af23SRyusuke Konishi 
453783f6184SRyusuke Konishi 	return 0;
454783f6184SRyusuke Konishi }
455783f6184SRyusuke Konishi 
456b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
457b58a285bSJiro SEKIBA {
458b58a285bSJiro SEKIBA 	struct super_block *sb = vfs->mnt_sb;
459b58a285bSJiro SEKIBA 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
460b58a285bSJiro SEKIBA 
461b58a285bSJiro SEKIBA 	if (!nilfs_test_opt(sbi, BARRIER))
46291f1953bSJiro SEKIBA 		seq_printf(seq, ",nobarrier");
463b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, SNAPSHOT))
464b58a285bSJiro SEKIBA 		seq_printf(seq, ",cp=%llu",
465b58a285bSJiro SEKIBA 			   (unsigned long long int)sbi->s_snapshot_cno);
466b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
467b58a285bSJiro SEKIBA 		seq_printf(seq, ",errors=panic");
468277a6a34SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_CONT))
469277a6a34SRyusuke Konishi 		seq_printf(seq, ",errors=continue");
470b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, STRICT_ORDER))
471b58a285bSJiro SEKIBA 		seq_printf(seq, ",order=strict");
4720234576dSRyusuke Konishi 	if (nilfs_test_opt(sbi, NORECOVERY))
4730234576dSRyusuke Konishi 		seq_printf(seq, ",norecovery");
474e902ec99SJiro SEKIBA 	if (nilfs_test_opt(sbi, DISCARD))
475e902ec99SJiro SEKIBA 		seq_printf(seq, ",discard");
476b58a285bSJiro SEKIBA 
477b58a285bSJiro SEKIBA 	return 0;
478b58a285bSJiro SEKIBA }
479b58a285bSJiro SEKIBA 
480b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
481783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
482783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
483783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
484783f6184SRyusuke Konishi 	/* .write_inode    = nilfs_write_inode, */
485783f6184SRyusuke Konishi 	/* .put_inode      = nilfs_put_inode, */
486783f6184SRyusuke Konishi 	/* .drop_inode	  = nilfs_drop_inode, */
487783f6184SRyusuke Konishi 	.delete_inode   = nilfs_delete_inode,
488783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
4891dfa2710SJiro SEKIBA 	/* .write_super    = nilfs_write_super, */
490783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
491783f6184SRyusuke Konishi 	/* .write_super_lockfs */
492783f6184SRyusuke Konishi 	/* .unlockfs */
493783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
494783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
495783f6184SRyusuke Konishi 	.clear_inode    = nilfs_clear_inode,
496783f6184SRyusuke Konishi 	/* .umount_begin */
497b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
498783f6184SRyusuke Konishi };
499783f6184SRyusuke Konishi 
500783f6184SRyusuke Konishi static struct inode *
501783f6184SRyusuke Konishi nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation)
502783f6184SRyusuke Konishi {
503783f6184SRyusuke Konishi 	struct inode *inode;
504783f6184SRyusuke Konishi 
505783f6184SRyusuke Konishi 	if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO &&
506783f6184SRyusuke Konishi 	    ino != NILFS_SKETCH_INO)
507783f6184SRyusuke Konishi 		return ERR_PTR(-ESTALE);
508783f6184SRyusuke Konishi 
509783f6184SRyusuke Konishi 	inode = nilfs_iget(sb, ino);
510783f6184SRyusuke Konishi 	if (IS_ERR(inode))
511783f6184SRyusuke Konishi 		return ERR_CAST(inode);
512783f6184SRyusuke Konishi 	if (generation && inode->i_generation != generation) {
513783f6184SRyusuke Konishi 		iput(inode);
514783f6184SRyusuke Konishi 		return ERR_PTR(-ESTALE);
515783f6184SRyusuke Konishi 	}
516783f6184SRyusuke Konishi 
517783f6184SRyusuke Konishi 	return inode;
518783f6184SRyusuke Konishi }
519783f6184SRyusuke Konishi 
520783f6184SRyusuke Konishi static struct dentry *
521783f6184SRyusuke Konishi nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
522783f6184SRyusuke Konishi 		   int fh_type)
523783f6184SRyusuke Konishi {
524783f6184SRyusuke Konishi 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
525783f6184SRyusuke Konishi 				    nilfs_nfs_get_inode);
526783f6184SRyusuke Konishi }
527783f6184SRyusuke Konishi 
528783f6184SRyusuke Konishi static struct dentry *
529783f6184SRyusuke Konishi nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
530783f6184SRyusuke Konishi 		   int fh_type)
531783f6184SRyusuke Konishi {
532783f6184SRyusuke Konishi 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
533783f6184SRyusuke Konishi 				    nilfs_nfs_get_inode);
534783f6184SRyusuke Konishi }
535783f6184SRyusuke Konishi 
536ac4cfdd6SAlexey Dobriyan static const struct export_operations nilfs_export_ops = {
537783f6184SRyusuke Konishi 	.fh_to_dentry = nilfs_fh_to_dentry,
538783f6184SRyusuke Konishi 	.fh_to_parent = nilfs_fh_to_parent,
539783f6184SRyusuke Konishi 	.get_parent = nilfs_get_parent,
540783f6184SRyusuke Konishi };
541783f6184SRyusuke Konishi 
542783f6184SRyusuke Konishi enum {
543783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
5440234576dSRyusuke Konishi 	Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
545e902ec99SJiro SEKIBA 	Opt_discard, Opt_err,
546783f6184SRyusuke Konishi };
547783f6184SRyusuke Konishi 
548783f6184SRyusuke Konishi static match_table_t tokens = {
549783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
550783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
551783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
55291f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
553783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
554783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
5550234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
556e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
557783f6184SRyusuke Konishi 	{Opt_err, NULL}
558783f6184SRyusuke Konishi };
559783f6184SRyusuke Konishi 
560783f6184SRyusuke Konishi static int parse_options(char *options, struct super_block *sb)
561783f6184SRyusuke Konishi {
562783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
563783f6184SRyusuke Konishi 	char *p;
564783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
565783f6184SRyusuke Konishi 	int option;
566783f6184SRyusuke Konishi 
567783f6184SRyusuke Konishi 	if (!options)
568783f6184SRyusuke Konishi 		return 1;
569783f6184SRyusuke Konishi 
570783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
571783f6184SRyusuke Konishi 		int token;
572783f6184SRyusuke Konishi 		if (!*p)
573783f6184SRyusuke Konishi 			continue;
574783f6184SRyusuke Konishi 
575783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
576783f6184SRyusuke Konishi 		switch (token) {
57791f1953bSJiro SEKIBA 		case Opt_nobarrier:
578783f6184SRyusuke Konishi 			nilfs_clear_opt(sbi, BARRIER);
579783f6184SRyusuke Konishi 			break;
580783f6184SRyusuke Konishi 		case Opt_order:
581783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
582783f6184SRyusuke Konishi 				/* Ordered data semantics */
583783f6184SRyusuke Konishi 				nilfs_clear_opt(sbi, STRICT_ORDER);
584783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
585783f6184SRyusuke Konishi 				/* Strict in-order semantics */
586783f6184SRyusuke Konishi 				nilfs_set_opt(sbi, STRICT_ORDER);
587783f6184SRyusuke Konishi 			else
588783f6184SRyusuke Konishi 				return 0;
589783f6184SRyusuke Konishi 			break;
590783f6184SRyusuke Konishi 		case Opt_err_panic:
591783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
592783f6184SRyusuke Konishi 			break;
593783f6184SRyusuke Konishi 		case Opt_err_ro:
594783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
595783f6184SRyusuke Konishi 			break;
596783f6184SRyusuke Konishi 		case Opt_err_cont:
597783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
598783f6184SRyusuke Konishi 			break;
599783f6184SRyusuke Konishi 		case Opt_snapshot:
600783f6184SRyusuke Konishi 			if (match_int(&args[0], &option) || option <= 0)
601783f6184SRyusuke Konishi 				return 0;
602783f6184SRyusuke Konishi 			if (!(sb->s_flags & MS_RDONLY))
603783f6184SRyusuke Konishi 				return 0;
604783f6184SRyusuke Konishi 			sbi->s_snapshot_cno = option;
605783f6184SRyusuke Konishi 			nilfs_set_opt(sbi, SNAPSHOT);
606783f6184SRyusuke Konishi 			break;
6070234576dSRyusuke Konishi 		case Opt_norecovery:
6080234576dSRyusuke Konishi 			nilfs_set_opt(sbi, NORECOVERY);
6090234576dSRyusuke Konishi 			break;
610e902ec99SJiro SEKIBA 		case Opt_discard:
611e902ec99SJiro SEKIBA 			nilfs_set_opt(sbi, DISCARD);
612e902ec99SJiro SEKIBA 			break;
613783f6184SRyusuke Konishi 		default:
614783f6184SRyusuke Konishi 			printk(KERN_ERR
615783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
616783f6184SRyusuke Konishi 			return 0;
617783f6184SRyusuke Konishi 		}
618783f6184SRyusuke Konishi 	}
619783f6184SRyusuke Konishi 	return 1;
620783f6184SRyusuke Konishi }
621783f6184SRyusuke Konishi 
622783f6184SRyusuke Konishi static inline void
623783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi,
624783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
625783f6184SRyusuke Konishi {
626783f6184SRyusuke Konishi 	sbi->s_mount_opt =
627277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
628783f6184SRyusuke Konishi }
629783f6184SRyusuke Konishi 
630783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi)
631783f6184SRyusuke Konishi {
632783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
633e339ad31SRyusuke Konishi 	struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
634783f6184SRyusuke Konishi 	int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count);
635783f6184SRyusuke Konishi 	int mnt_count = le16_to_cpu(sbp->s_mnt_count);
636783f6184SRyusuke Konishi 
637783f6184SRyusuke Konishi 	/* nilfs->sem must be locked by the caller. */
638f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
639783f6184SRyusuke Konishi 		printk(KERN_WARNING
640783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
641783f6184SRyusuke Konishi #if 0
642783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
643783f6184SRyusuke Konishi 		printk(KERN_WARNING
644783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
645783f6184SRyusuke Konishi #endif
646783f6184SRyusuke Konishi 	}
647783f6184SRyusuke Konishi 	if (!max_mnt_count)
648783f6184SRyusuke Konishi 		sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
649783f6184SRyusuke Konishi 
650783f6184SRyusuke Konishi 	sbp->s_mnt_count = cpu_to_le16(mnt_count + 1);
651783f6184SRyusuke Konishi 	sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS);
652783f6184SRyusuke Konishi 	sbp->s_mtime = cpu_to_le64(get_seconds());
653e339ad31SRyusuke Konishi 	return nilfs_commit_super(sbi, 1);
654783f6184SRyusuke Konishi }
655783f6184SRyusuke Konishi 
656e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
657e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
658e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
659783f6184SRyusuke Konishi {
660e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
661e339ad31SRyusuke Konishi 	unsigned long offset;
662783f6184SRyusuke Konishi 
663e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
664783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
665e339ad31SRyusuke Konishi 	if (!*pbh)
666783f6184SRyusuke Konishi 		return NULL;
667783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
668783f6184SRyusuke Konishi }
669783f6184SRyusuke Konishi 
670783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
671783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
672783f6184SRyusuke Konishi 				 char *data)
673783f6184SRyusuke Konishi {
674783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
675783f6184SRyusuke Konishi 
676783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
677783f6184SRyusuke Konishi 
678783f6184SRyusuke Konishi 	/* FS independent flags */
679783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
680783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
681783f6184SRyusuke Konishi #endif
682783f6184SRyusuke Konishi 
683783f6184SRyusuke Konishi 	nilfs_set_default_options(sbi, sbp);
684783f6184SRyusuke Konishi 
685783f6184SRyusuke Konishi 	sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
686783f6184SRyusuke Konishi 	sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
687783f6184SRyusuke Konishi 	sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
688783f6184SRyusuke Konishi 	sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
689783f6184SRyusuke Konishi 
690e339ad31SRyusuke Konishi 	return !parse_options(data, sb) ? -EINVAL : 0 ;
691783f6184SRyusuke Konishi }
692783f6184SRyusuke Konishi 
693783f6184SRyusuke Konishi /**
694783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
695783f6184SRyusuke Konishi  * @sb: super_block
696783f6184SRyusuke Konishi  * @data: mount options
697783f6184SRyusuke Konishi  * @silent: silent mode flag
698783f6184SRyusuke Konishi  * @nilfs: the_nilfs struct
699783f6184SRyusuke Konishi  *
700aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
701783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
702783f6184SRyusuke Konishi  */
703783f6184SRyusuke Konishi static int
704783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent,
705783f6184SRyusuke Konishi 		 struct the_nilfs *nilfs)
706783f6184SRyusuke Konishi {
707783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi;
708783f6184SRyusuke Konishi 	struct inode *root;
709783f6184SRyusuke Konishi 	__u64 cno;
710783f6184SRyusuke Konishi 	int err;
711783f6184SRyusuke Konishi 
712783f6184SRyusuke Konishi 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
713783f6184SRyusuke Konishi 	if (!sbi)
714783f6184SRyusuke Konishi 		return -ENOMEM;
715783f6184SRyusuke Konishi 
716783f6184SRyusuke Konishi 	sb->s_fs_info = sbi;
717783f6184SRyusuke Konishi 
718783f6184SRyusuke Konishi 	get_nilfs(nilfs);
719783f6184SRyusuke Konishi 	sbi->s_nilfs = nilfs;
720783f6184SRyusuke Konishi 	sbi->s_super = sb;
7216dd47406SRyusuke Konishi 	atomic_set(&sbi->s_count, 1);
722783f6184SRyusuke Konishi 
723783f6184SRyusuke Konishi 	err = init_nilfs(nilfs, sbi, (char *)data);
724783f6184SRyusuke Konishi 	if (err)
725783f6184SRyusuke Konishi 		goto failed_sbi;
726783f6184SRyusuke Konishi 
727783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_inode_lock);
728783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_dirty_files);
729783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_list);
730783f6184SRyusuke Konishi 
731783f6184SRyusuke Konishi 	/*
732783f6184SRyusuke Konishi 	 * Following initialization is overlapped because
733783f6184SRyusuke Konishi 	 * nilfs_sb_info structure has been cleared at the beginning.
734783f6184SRyusuke Konishi 	 * But we reserve them to keep our interest and make ready
735783f6184SRyusuke Konishi 	 * for the future change.
736783f6184SRyusuke Konishi 	 */
737783f6184SRyusuke Konishi 	get_random_bytes(&sbi->s_next_generation,
738783f6184SRyusuke Konishi 			 sizeof(sbi->s_next_generation));
739783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_next_gen_lock);
740783f6184SRyusuke Konishi 
741783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
742783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
743783f6184SRyusuke Konishi 	sb->s_root = NULL;
74461239230SRyusuke Konishi 	sb->s_time_gran = 1;
745973bec34SRyusuke Konishi 	sb->s_bdi = nilfs->ns_bdi;
746783f6184SRyusuke Konishi 
747783f6184SRyusuke Konishi 	err = load_nilfs(nilfs, sbi);
748783f6184SRyusuke Konishi 	if (err)
749783f6184SRyusuke Konishi 		goto failed_sbi;
750f50a4c81SRyusuke Konishi 
751783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
752783f6184SRyusuke Konishi 
753783f6184SRyusuke Konishi 	if (sb->s_flags & MS_RDONLY) {
754783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, SNAPSHOT)) {
75543be0ec0SZhu Yanhai 			down_read(&nilfs->ns_segctor_sem);
7561f5abe7eSRyusuke Konishi 			err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
7571f5abe7eSRyusuke Konishi 						       sbi->s_snapshot_cno);
75843be0ec0SZhu Yanhai 			up_read(&nilfs->ns_segctor_sem);
75943be0ec0SZhu Yanhai 			if (err < 0) {
76043be0ec0SZhu Yanhai 				if (err == -ENOENT)
76143be0ec0SZhu Yanhai 					err = -EINVAL;
7621f5abe7eSRyusuke Konishi 				goto failed_sbi;
76343be0ec0SZhu Yanhai 			}
7641f5abe7eSRyusuke Konishi 			if (!err) {
765783f6184SRyusuke Konishi 				printk(KERN_ERR
766783f6184SRyusuke Konishi 				       "NILFS: The specified checkpoint is "
767783f6184SRyusuke Konishi 				       "not a snapshot "
768783f6184SRyusuke Konishi 				       "(checkpoint number=%llu).\n",
769783f6184SRyusuke Konishi 				       (unsigned long long)sbi->s_snapshot_cno);
770783f6184SRyusuke Konishi 				err = -EINVAL;
771783f6184SRyusuke Konishi 				goto failed_sbi;
772783f6184SRyusuke Konishi 			}
773783f6184SRyusuke Konishi 			cno = sbi->s_snapshot_cno;
774d240e067SRyusuke Konishi 		}
775783f6184SRyusuke Konishi 	}
776783f6184SRyusuke Konishi 
777783f6184SRyusuke Konishi 	err = nilfs_attach_checkpoint(sbi, cno);
778783f6184SRyusuke Konishi 	if (err) {
779783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading a checkpoint"
780783f6184SRyusuke Konishi 		       " (checkpoint number=%llu).\n", (unsigned long long)cno);
781783f6184SRyusuke Konishi 		goto failed_sbi;
782783f6184SRyusuke Konishi 	}
783783f6184SRyusuke Konishi 
784783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
785cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
786783f6184SRyusuke Konishi 		if (err)
787783f6184SRyusuke Konishi 			goto failed_checkpoint;
788783f6184SRyusuke Konishi 	}
789783f6184SRyusuke Konishi 
790783f6184SRyusuke Konishi 	root = nilfs_iget(sb, NILFS_ROOT_INO);
791783f6184SRyusuke Konishi 	if (IS_ERR(root)) {
792783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
793783f6184SRyusuke Konishi 		err = PTR_ERR(root);
794783f6184SRyusuke Konishi 		goto failed_segctor;
795783f6184SRyusuke Konishi 	}
796783f6184SRyusuke Konishi 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
797783f6184SRyusuke Konishi 		iput(root);
798783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
799783f6184SRyusuke Konishi 		err = -EINVAL;
800783f6184SRyusuke Konishi 		goto failed_segctor;
801783f6184SRyusuke Konishi 	}
802783f6184SRyusuke Konishi 	sb->s_root = d_alloc_root(root);
803783f6184SRyusuke Konishi 	if (!sb->s_root) {
804783f6184SRyusuke Konishi 		iput(root);
805783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root dentry failed\n");
806783f6184SRyusuke Konishi 		err = -ENOMEM;
807783f6184SRyusuke Konishi 		goto failed_segctor;
808783f6184SRyusuke Konishi 	}
809783f6184SRyusuke Konishi 
810783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
811783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
812783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
813783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
814783f6184SRyusuke Konishi 	}
815783f6184SRyusuke Konishi 
816e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
8173f82ff55SRyusuke Konishi 	if (!nilfs_test_opt(sbi, SNAPSHOT))
8183f82ff55SRyusuke Konishi 		nilfs->ns_current = sbi;
819e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
8203f82ff55SRyusuke Konishi 
821783f6184SRyusuke Konishi 	return 0;
822783f6184SRyusuke Konishi 
823783f6184SRyusuke Konishi  failed_segctor:
824783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
825783f6184SRyusuke Konishi 
826783f6184SRyusuke Konishi  failed_checkpoint:
827783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
828783f6184SRyusuke Konishi 
829783f6184SRyusuke Konishi  failed_sbi:
830783f6184SRyusuke Konishi 	put_nilfs(nilfs);
831783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
8326dd47406SRyusuke Konishi 	nilfs_put_sbinfo(sbi);
833783f6184SRyusuke Konishi 	return err;
834783f6184SRyusuke Konishi }
835783f6184SRyusuke Konishi 
836783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
837783f6184SRyusuke Konishi {
838783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
839783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
840783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
841783f6184SRyusuke Konishi 	struct nilfs_mount_options old_opts;
842d240e067SRyusuke Konishi 	int was_snapshot, err;
843783f6184SRyusuke Konishi 
844337eb00aSAlessio Igor Bogani 	lock_kernel();
845337eb00aSAlessio Igor Bogani 
846e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
847783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
848783f6184SRyusuke Konishi 	old_opts.mount_opt = sbi->s_mount_opt;
849783f6184SRyusuke Konishi 	old_opts.snapshot_cno = sbi->s_snapshot_cno;
850d240e067SRyusuke Konishi 	was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
851783f6184SRyusuke Konishi 
852783f6184SRyusuke Konishi 	if (!parse_options(data, sb)) {
853783f6184SRyusuke Konishi 		err = -EINVAL;
854783f6184SRyusuke Konishi 		goto restore_opts;
855783f6184SRyusuke Konishi 	}
856783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
857783f6184SRyusuke Konishi 
858d240e067SRyusuke Konishi 	err = -EINVAL;
859d240e067SRyusuke Konishi 	if (was_snapshot) {
860d240e067SRyusuke Konishi 		if (!(*flags & MS_RDONLY)) {
861d240e067SRyusuke Konishi 			printk(KERN_ERR "NILFS (device %s): cannot remount "
862d240e067SRyusuke Konishi 			       "snapshot read/write.\n",
863d240e067SRyusuke Konishi 			       sb->s_id);
864d240e067SRyusuke Konishi 			goto restore_opts;
865d240e067SRyusuke Konishi 		} else if (sbi->s_snapshot_cno != old_opts.snapshot_cno) {
866d240e067SRyusuke Konishi 			printk(KERN_ERR "NILFS (device %s): cannot "
867783f6184SRyusuke Konishi 			       "remount to a different snapshot.\n",
868783f6184SRyusuke Konishi 			       sb->s_id);
869783f6184SRyusuke Konishi 			goto restore_opts;
870783f6184SRyusuke Konishi 		}
871d240e067SRyusuke Konishi 	} else {
872d240e067SRyusuke Konishi 		if (nilfs_test_opt(sbi, SNAPSHOT)) {
873d240e067SRyusuke Konishi 			printk(KERN_ERR "NILFS (device %s): cannot change "
874d240e067SRyusuke Konishi 			       "a regular mount to a snapshot.\n",
875d240e067SRyusuke Konishi 			       sb->s_id);
876d240e067SRyusuke Konishi 			goto restore_opts;
877d240e067SRyusuke Konishi 		}
878d240e067SRyusuke Konishi 	}
879783f6184SRyusuke Konishi 
8800234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
8810234576dSRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
8820234576dSRyusuke Konishi 		       "remount because the filesystem is in an "
8830234576dSRyusuke Konishi 		       "incomplete recovery state.\n", sb->s_id);
8840234576dSRyusuke Konishi 		goto restore_opts;
8850234576dSRyusuke Konishi 	}
8860234576dSRyusuke Konishi 
887783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
888783f6184SRyusuke Konishi 		goto out;
889783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
890783f6184SRyusuke Konishi 		/* Shutting down the segment constructor */
891783f6184SRyusuke Konishi 		nilfs_detach_segment_constructor(sbi);
892783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
893783f6184SRyusuke Konishi 
894783f6184SRyusuke Konishi 		/*
895783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
896783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
897783f6184SRyusuke Konishi 		 */
898783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
899*7ecaa46cSRyusuke Konishi 		nilfs_cleanup_super(sbi);
900783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
901783f6184SRyusuke Konishi 	} else {
902783f6184SRyusuke Konishi 		/*
903783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
904783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
905783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
906783f6184SRyusuke Konishi 		 */
907783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
908783f6184SRyusuke Konishi 
909cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
910783f6184SRyusuke Konishi 		if (err)
911e59399d0SRyusuke Konishi 			goto restore_opts;
912783f6184SRyusuke Konishi 
913783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
914783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
915783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
916783f6184SRyusuke Konishi 	}
917783f6184SRyusuke Konishi  out:
918e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
919337eb00aSAlessio Igor Bogani 	unlock_kernel();
920783f6184SRyusuke Konishi 	return 0;
921783f6184SRyusuke Konishi 
922783f6184SRyusuke Konishi  restore_opts:
923783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
924783f6184SRyusuke Konishi 	sbi->s_mount_opt = old_opts.mount_opt;
925783f6184SRyusuke Konishi 	sbi->s_snapshot_cno = old_opts.snapshot_cno;
926e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
927337eb00aSAlessio Igor Bogani 	unlock_kernel();
928783f6184SRyusuke Konishi 	return err;
929783f6184SRyusuke Konishi }
930783f6184SRyusuke Konishi 
931783f6184SRyusuke Konishi struct nilfs_super_data {
932783f6184SRyusuke Konishi 	struct block_device *bdev;
9336dd47406SRyusuke Konishi 	struct nilfs_sb_info *sbi;
934783f6184SRyusuke Konishi 	__u64 cno;
935783f6184SRyusuke Konishi 	int flags;
936783f6184SRyusuke Konishi };
937783f6184SRyusuke Konishi 
938783f6184SRyusuke Konishi /**
939783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
940783f6184SRyusuke Konishi  * @data: mount options
941783f6184SRyusuke Konishi  * @sd: nilfs_super_data
942783f6184SRyusuke Konishi  */
943783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
944783f6184SRyusuke Konishi {
945783f6184SRyusuke Konishi 	char *p, *options = data;
946783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
947783f6184SRyusuke Konishi 	int option, token;
948783f6184SRyusuke Konishi 	int ret = 0;
949783f6184SRyusuke Konishi 
950783f6184SRyusuke Konishi 	do {
951783f6184SRyusuke Konishi 		p = strsep(&options, ",");
952783f6184SRyusuke Konishi 		if (p != NULL && *p) {
953783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
954783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
955783f6184SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY))
956783f6184SRyusuke Konishi 					ret++;
957783f6184SRyusuke Konishi 				else {
958783f6184SRyusuke Konishi 					ret = match_int(&args[0], &option);
959783f6184SRyusuke Konishi 					if (!ret) {
960783f6184SRyusuke Konishi 						if (option > 0)
961783f6184SRyusuke Konishi 							sd->cno = option;
962783f6184SRyusuke Konishi 						else
963783f6184SRyusuke Konishi 							ret++;
964783f6184SRyusuke Konishi 					}
965783f6184SRyusuke Konishi 				}
966783f6184SRyusuke Konishi 			}
967783f6184SRyusuke Konishi 			if (ret)
968783f6184SRyusuke Konishi 				printk(KERN_ERR
969783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
970783f6184SRyusuke Konishi 		}
971783f6184SRyusuke Konishi 		if (!options)
972783f6184SRyusuke Konishi 			break;
973783f6184SRyusuke Konishi 		BUG_ON(options == data);
974783f6184SRyusuke Konishi 		*(options - 1) = ',';
975783f6184SRyusuke Konishi 	} while (!ret);
976783f6184SRyusuke Konishi 	return ret;
977783f6184SRyusuke Konishi }
978783f6184SRyusuke Konishi 
979783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
980783f6184SRyusuke Konishi {
981783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
982783f6184SRyusuke Konishi 
983783f6184SRyusuke Konishi 	s->s_bdev = sd->bdev;
984783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
985783f6184SRyusuke Konishi 	return 0;
986783f6184SRyusuke Konishi }
987783f6184SRyusuke Konishi 
988783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
989783f6184SRyusuke Konishi {
990783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
991783f6184SRyusuke Konishi 
9926dd47406SRyusuke Konishi 	return sd->sbi && s->s_fs_info == (void *)sd->sbi;
993783f6184SRyusuke Konishi }
994783f6184SRyusuke Konishi 
995783f6184SRyusuke Konishi static int
996783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags,
997783f6184SRyusuke Konishi 	     const char *dev_name, void *data, struct vfsmount *mnt)
998783f6184SRyusuke Konishi {
999783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
100033c8e57cSRyusuke Konishi 	struct super_block *s;
100113e90559SRyusuke Konishi 	fmode_t mode = FMODE_READ;
100233c8e57cSRyusuke Konishi 	struct the_nilfs *nilfs;
1003783f6184SRyusuke Konishi 	int err, need_to_close = 1;
1004783f6184SRyusuke Konishi 
100513e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
100613e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
100713e90559SRyusuke Konishi 
100813e90559SRyusuke Konishi 	sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
1009783f6184SRyusuke Konishi 	if (IS_ERR(sd.bdev))
1010783f6184SRyusuke Konishi 		return PTR_ERR(sd.bdev);
1011783f6184SRyusuke Konishi 
1012783f6184SRyusuke Konishi 	/*
1013783f6184SRyusuke Konishi 	 * To get mount instance using sget() vfs-routine, NILFS needs
1014783f6184SRyusuke Konishi 	 * much more information than normal filesystems to identify mount
1015783f6184SRyusuke Konishi 	 * instance.  For snapshot mounts, not only a mount type (ro-mount
1016783f6184SRyusuke Konishi 	 * or rw-mount) but also a checkpoint number is required.
1017783f6184SRyusuke Konishi 	 */
1018783f6184SRyusuke Konishi 	sd.cno = 0;
1019783f6184SRyusuke Konishi 	sd.flags = flags;
1020783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1021783f6184SRyusuke Konishi 		err = -EINVAL;
1022783f6184SRyusuke Konishi 		goto failed;
1023783f6184SRyusuke Konishi 	}
1024783f6184SRyusuke Konishi 
102533c8e57cSRyusuke Konishi 	nilfs = find_or_create_nilfs(sd.bdev);
102633c8e57cSRyusuke Konishi 	if (!nilfs) {
102733c8e57cSRyusuke Konishi 		err = -ENOMEM;
102833c8e57cSRyusuke Konishi 		goto failed;
102933c8e57cSRyusuke Konishi 	}
103033c8e57cSRyusuke Konishi 
1031aa7dfb89SRyusuke Konishi 	mutex_lock(&nilfs->ns_mount_mutex);
10323f82ff55SRyusuke Konishi 
10333f82ff55SRyusuke Konishi 	if (!sd.cno) {
10343f82ff55SRyusuke Konishi 		/*
10353f82ff55SRyusuke Konishi 		 * Check if an exclusive mount exists or not.
10363f82ff55SRyusuke Konishi 		 * Snapshot mounts coexist with a current mount
10373f82ff55SRyusuke Konishi 		 * (i.e. rw-mount or ro-mount), whereas rw-mount and
10383f82ff55SRyusuke Konishi 		 * ro-mount are mutually exclusive.
10393f82ff55SRyusuke Konishi 		 */
1040e59399d0SRyusuke Konishi 		down_read(&nilfs->ns_super_sem);
10413f82ff55SRyusuke Konishi 		if (nilfs->ns_current &&
10423f82ff55SRyusuke Konishi 		    ((nilfs->ns_current->s_super->s_flags ^ flags)
10433f82ff55SRyusuke Konishi 		     & MS_RDONLY)) {
1044e59399d0SRyusuke Konishi 			up_read(&nilfs->ns_super_sem);
10453f82ff55SRyusuke Konishi 			err = -EBUSY;
1046783f6184SRyusuke Konishi 			goto failed_unlock;
1047783f6184SRyusuke Konishi 		}
1048e59399d0SRyusuke Konishi 		up_read(&nilfs->ns_super_sem);
10493f82ff55SRyusuke Konishi 	}
1050783f6184SRyusuke Konishi 
1051783f6184SRyusuke Konishi 	/*
10526dd47406SRyusuke Konishi 	 * Find existing nilfs_sb_info struct
1053783f6184SRyusuke Konishi 	 */
10546dd47406SRyusuke Konishi 	sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
10556dd47406SRyusuke Konishi 
10566dd47406SRyusuke Konishi 	/*
10576dd47406SRyusuke Konishi 	 * Get super block instance holding the nilfs_sb_info struct.
10586dd47406SRyusuke Konishi 	 * A new instance is allocated if no existing mount is present or
10596dd47406SRyusuke Konishi 	 * existing instance has been unmounted.
10606dd47406SRyusuke Konishi 	 */
106133c8e57cSRyusuke Konishi 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
10626dd47406SRyusuke Konishi 	if (sd.sbi)
10636dd47406SRyusuke Konishi 		nilfs_put_sbinfo(sd.sbi);
10646dd47406SRyusuke Konishi 
106533c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
106633c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
106733c8e57cSRyusuke Konishi 		goto failed_unlock;
1068783f6184SRyusuke Konishi 	}
1069783f6184SRyusuke Konishi 
1070783f6184SRyusuke Konishi 	if (!s->s_root) {
1071783f6184SRyusuke Konishi 		char b[BDEVNAME_SIZE];
1072783f6184SRyusuke Konishi 
107333c8e57cSRyusuke Konishi 		/* New superblock instance created */
1074783f6184SRyusuke Konishi 		s->s_flags = flags;
10754571b82cSRyusuke Konishi 		s->s_mode = mode;
1076783f6184SRyusuke Konishi 		strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
1077783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1078783f6184SRyusuke Konishi 
1079e2d1591aSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
1080e2d1591aSRyusuke Konishi 				       nilfs);
1081783f6184SRyusuke Konishi 		if (err)
1082783f6184SRyusuke Konishi 			goto cancel_new;
1083783f6184SRyusuke Konishi 
1084783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1085783f6184SRyusuke Konishi 		need_to_close = 0;
1086783f6184SRyusuke Konishi 	}
1087783f6184SRyusuke Konishi 
1088aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1089783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1090783f6184SRyusuke Konishi 	if (need_to_close)
109113e90559SRyusuke Konishi 		close_bdev_exclusive(sd.bdev, mode);
1092783f6184SRyusuke Konishi 	simple_set_mnt(mnt, s);
1093783f6184SRyusuke Konishi 	return 0;
1094783f6184SRyusuke Konishi 
1095783f6184SRyusuke Konishi  failed_unlock:
1096aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
109733c8e57cSRyusuke Konishi 	put_nilfs(nilfs);
1098783f6184SRyusuke Konishi  failed:
109913e90559SRyusuke Konishi 	close_bdev_exclusive(sd.bdev, mode);
1100783f6184SRyusuke Konishi 
1101783f6184SRyusuke Konishi 	return err;
1102783f6184SRyusuke Konishi 
1103783f6184SRyusuke Konishi  cancel_new:
1104783f6184SRyusuke Konishi 	/* Abandoning the newly allocated superblock */
1105aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1106783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1107a95161aaSAl Viro 	deactivate_locked_super(s);
1108783f6184SRyusuke Konishi 	/*
1109b87ca919SRyusuke Konishi 	 * deactivate_locked_super() invokes close_bdev_exclusive().
1110783f6184SRyusuke Konishi 	 * We must finish all post-cleaning before this call;
1111aa7dfb89SRyusuke Konishi 	 * put_nilfs() needs the block device.
1112783f6184SRyusuke Konishi 	 */
1113783f6184SRyusuke Konishi 	return err;
1114783f6184SRyusuke Konishi }
1115783f6184SRyusuke Konishi 
1116783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1117783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1118783f6184SRyusuke Konishi 	.name     = "nilfs2",
1119783f6184SRyusuke Konishi 	.get_sb   = nilfs_get_sb,
1120783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1121783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1122783f6184SRyusuke Konishi };
1123783f6184SRyusuke Konishi 
112441c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
112541c88bd7SLi Hong {
112641c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
112741c88bd7SLi Hong 
112841c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
112941c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
113041c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
113141c88bd7SLi Hong #endif
113241c88bd7SLi Hong 	nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
113341c88bd7SLi Hong 	ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union;
113441c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
113541c88bd7SLi Hong }
113641c88bd7SLi Hong 
113741c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
113841c88bd7SLi Hong {
113941c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
114041c88bd7SLi Hong }
114141c88bd7SLi Hong 
114241c88bd7SLi Hong static void nilfs_destroy_cachep(void)
114341c88bd7SLi Hong {
114441c88bd7SLi Hong 	if (nilfs_inode_cachep)
114541c88bd7SLi Hong 		kmem_cache_destroy(nilfs_inode_cachep);
114641c88bd7SLi Hong 	if (nilfs_transaction_cachep)
114741c88bd7SLi Hong 		kmem_cache_destroy(nilfs_transaction_cachep);
114841c88bd7SLi Hong 	if (nilfs_segbuf_cachep)
114941c88bd7SLi Hong 		kmem_cache_destroy(nilfs_segbuf_cachep);
115041c88bd7SLi Hong 	if (nilfs_btree_path_cache)
115141c88bd7SLi Hong 		kmem_cache_destroy(nilfs_btree_path_cache);
115241c88bd7SLi Hong }
115341c88bd7SLi Hong 
115441c88bd7SLi Hong static int __init nilfs_init_cachep(void)
115541c88bd7SLi Hong {
115641c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
115741c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
115841c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
115941c88bd7SLi Hong 	if (!nilfs_inode_cachep)
116041c88bd7SLi Hong 		goto fail;
116141c88bd7SLi Hong 
116241c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
116341c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
116441c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
116541c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
116641c88bd7SLi Hong 		goto fail;
116741c88bd7SLi Hong 
116841c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
116941c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
117041c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
117141c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
117241c88bd7SLi Hong 		goto fail;
117341c88bd7SLi Hong 
117441c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
117541c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
117641c88bd7SLi Hong 			0, 0, NULL);
117741c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
117841c88bd7SLi Hong 		goto fail;
117941c88bd7SLi Hong 
118041c88bd7SLi Hong 	return 0;
118141c88bd7SLi Hong 
118241c88bd7SLi Hong fail:
118341c88bd7SLi Hong 	nilfs_destroy_cachep();
118441c88bd7SLi Hong 	return -ENOMEM;
118541c88bd7SLi Hong }
118641c88bd7SLi Hong 
1187783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1188783f6184SRyusuke Konishi {
1189783f6184SRyusuke Konishi 	int err;
1190783f6184SRyusuke Konishi 
119141c88bd7SLi Hong 	err = nilfs_init_cachep();
1192783f6184SRyusuke Konishi 	if (err)
119341c88bd7SLi Hong 		goto fail;
1194783f6184SRyusuke Konishi 
1195783f6184SRyusuke Konishi 	err = register_filesystem(&nilfs_fs_type);
1196783f6184SRyusuke Konishi 	if (err)
119741c88bd7SLi Hong 		goto free_cachep;
1198783f6184SRyusuke Konishi 
11999f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1200783f6184SRyusuke Konishi 	return 0;
1201783f6184SRyusuke Konishi 
120241c88bd7SLi Hong free_cachep:
120341c88bd7SLi Hong 	nilfs_destroy_cachep();
120441c88bd7SLi Hong fail:
1205783f6184SRyusuke Konishi 	return err;
1206783f6184SRyusuke Konishi }
1207783f6184SRyusuke Konishi 
1208783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1209783f6184SRyusuke Konishi {
121041c88bd7SLi Hong 	nilfs_destroy_cachep();
1211783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1212783f6184SRyusuke Konishi }
1213783f6184SRyusuke Konishi 
1214783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1215783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1216