xref: /openbmc/linux/fs/nilfs2/super.c (revision 8e656fd518784b49453f60c5f78b78703bc85cb2)
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/vfs.h>
49783f6184SRyusuke Konishi #include <linux/writeback.h>
50783f6184SRyusuke Konishi #include <linux/kobject.h>
51b58a285bSJiro SEKIBA #include <linux/seq_file.h>
52b58a285bSJiro SEKIBA #include <linux/mount.h>
53783f6184SRyusuke Konishi #include "nilfs.h"
54*8e656fd5SRyusuke Konishi #include "export.h"
55783f6184SRyusuke Konishi #include "mdt.h"
56783f6184SRyusuke Konishi #include "alloc.h"
5705d0e94bSRyusuke Konishi #include "btree.h"
5805d0e94bSRyusuke Konishi #include "btnode.h"
59783f6184SRyusuke Konishi #include "page.h"
60783f6184SRyusuke Konishi #include "cpfile.h"
61783f6184SRyusuke Konishi #include "ifile.h"
62783f6184SRyusuke Konishi #include "dat.h"
63783f6184SRyusuke Konishi #include "segment.h"
64783f6184SRyusuke Konishi #include "segbuf.h"
65783f6184SRyusuke Konishi 
66783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
67783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
68783f6184SRyusuke Konishi 		   "(NILFS)");
69783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
70783f6184SRyusuke Konishi 
7141c88bd7SLi Hong struct kmem_cache *nilfs_inode_cachep;
7241c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
7341c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
7441c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
7541c88bd7SLi Hong 
76783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
77783f6184SRyusuke Konishi 
78c8a11c8aSRyusuke Konishi static void nilfs_set_error(struct nilfs_sb_info *sbi)
79c8a11c8aSRyusuke Konishi {
80c8a11c8aSRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
81d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
82c8a11c8aSRyusuke Konishi 
83c8a11c8aSRyusuke Konishi 	down_write(&nilfs->ns_sem);
84c8a11c8aSRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
85c8a11c8aSRyusuke Konishi 		nilfs->ns_mount_state |= NILFS_ERROR_FS;
86b2ac86e1SJiro SEKIBA 		sbp = nilfs_prepare_super(sbi, 0);
87d26493b6SJiro SEKIBA 		if (likely(sbp)) {
88d26493b6SJiro SEKIBA 			sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
89b2ac86e1SJiro SEKIBA 			if (sbp[1])
90b2ac86e1SJiro SEKIBA 				sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
91b2ac86e1SJiro SEKIBA 			nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
92c8a11c8aSRyusuke Konishi 		}
93d26493b6SJiro SEKIBA 	}
94c8a11c8aSRyusuke Konishi 	up_write(&nilfs->ns_sem);
95c8a11c8aSRyusuke Konishi }
96c8a11c8aSRyusuke Konishi 
97783f6184SRyusuke Konishi /**
98783f6184SRyusuke Konishi  * nilfs_error() - report failure condition on a filesystem
99783f6184SRyusuke Konishi  *
100783f6184SRyusuke Konishi  * nilfs_error() sets an ERROR_FS flag on the superblock as well as
101783f6184SRyusuke Konishi  * reporting an error message.  It should be called when NILFS detects
102783f6184SRyusuke Konishi  * incoherences or defects of meta data on disk.  As for sustainable
103783f6184SRyusuke Konishi  * errors such as a single-shot I/O error, nilfs_warning() or the printk()
104783f6184SRyusuke Konishi  * function should be used instead.
105783f6184SRyusuke Konishi  *
106783f6184SRyusuke Konishi  * The segment constructor must not call this function because it can
107783f6184SRyusuke Konishi  * kill itself.
108783f6184SRyusuke Konishi  */
109783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function,
110783f6184SRyusuke Konishi 		 const char *fmt, ...)
111783f6184SRyusuke Konishi {
112783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
113783f6184SRyusuke Konishi 	va_list args;
114783f6184SRyusuke Konishi 
115783f6184SRyusuke Konishi 	va_start(args, fmt);
116783f6184SRyusuke Konishi 	printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function);
117783f6184SRyusuke Konishi 	vprintk(fmt, args);
118783f6184SRyusuke Konishi 	printk("\n");
119783f6184SRyusuke Konishi 	va_end(args);
120783f6184SRyusuke Konishi 
121783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
122c8a11c8aSRyusuke Konishi 		nilfs_set_error(sbi);
123783f6184SRyusuke Konishi 
124783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, ERRORS_RO)) {
125783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
126783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
127783f6184SRyusuke Konishi 		}
128783f6184SRyusuke Konishi 	}
129783f6184SRyusuke Konishi 
130783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
131783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
132783f6184SRyusuke Konishi 		      sb->s_id);
133783f6184SRyusuke Konishi }
134783f6184SRyusuke Konishi 
135783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function,
136783f6184SRyusuke Konishi 		   const char *fmt, ...)
137783f6184SRyusuke Konishi {
138783f6184SRyusuke Konishi 	va_list args;
139783f6184SRyusuke Konishi 
140783f6184SRyusuke Konishi 	va_start(args, fmt);
141783f6184SRyusuke Konishi 	printk(KERN_WARNING "NILFS warning (device %s): %s: ",
142783f6184SRyusuke Konishi 	       sb->s_id, function);
143783f6184SRyusuke Konishi 	vprintk(fmt, args);
144783f6184SRyusuke Konishi 	printk("\n");
145783f6184SRyusuke Konishi 	va_end(args);
146783f6184SRyusuke Konishi }
147783f6184SRyusuke Konishi 
148783f6184SRyusuke Konishi 
149a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
150783f6184SRyusuke Konishi {
151783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
152783f6184SRyusuke Konishi 
153783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
154783f6184SRyusuke Konishi 	if (!ii)
155783f6184SRyusuke Konishi 		return NULL;
156783f6184SRyusuke Konishi 	ii->i_bh = NULL;
157783f6184SRyusuke Konishi 	ii->i_state = 0;
1580e14a359SRyusuke Konishi 	ii->i_cno = 0;
159783f6184SRyusuke Konishi 	ii->vfs_inode.i_version = 1;
160a53b4751SRyusuke Konishi 	nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
161783f6184SRyusuke Konishi 	return &ii->vfs_inode;
162783f6184SRyusuke Konishi }
163783f6184SRyusuke Konishi 
164a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
165a53b4751SRyusuke Konishi {
166a53b4751SRyusuke Konishi 	return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
167a53b4751SRyusuke Konishi }
168a53b4751SRyusuke Konishi 
169783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode)
170783f6184SRyusuke Konishi {
171b91c9a97SRyusuke Konishi 	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
172b91c9a97SRyusuke Konishi 
173b91c9a97SRyusuke Konishi 	if (mdi) {
174b91c9a97SRyusuke Konishi 		kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
175b91c9a97SRyusuke Konishi 		kfree(mdi);
176b91c9a97SRyusuke Konishi 	}
177783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
178783f6184SRyusuke Konishi }
179783f6184SRyusuke Konishi 
180b2ac86e1SJiro SEKIBA static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag)
181783f6184SRyusuke Konishi {
182783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
183783f6184SRyusuke Konishi 	int err;
184783f6184SRyusuke Konishi 
185783f6184SRyusuke Konishi  retry:
186e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
18787e99511SChristoph Hellwig 
18887e99511SChristoph Hellwig 	if (nilfs_test_opt(sbi, BARRIER)) {
18987e99511SChristoph Hellwig 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
19087e99511SChristoph Hellwig 					  WRITE_SYNC | WRITE_BARRIER);
19187e99511SChristoph Hellwig 		if (err == -EOPNOTSUPP) {
192783f6184SRyusuke Konishi 			nilfs_warning(sbi->s_super, __func__,
193783f6184SRyusuke Konishi 				      "barrier-based sync failed. "
194783f6184SRyusuke Konishi 				      "disabling barriers\n");
195783f6184SRyusuke Konishi 			nilfs_clear_opt(sbi, BARRIER);
196783f6184SRyusuke Konishi 			goto retry;
197783f6184SRyusuke Konishi 		}
19887e99511SChristoph Hellwig 	} else {
19987e99511SChristoph Hellwig 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
20087e99511SChristoph Hellwig 	}
20187e99511SChristoph Hellwig 
202e339ad31SRyusuke Konishi 	if (unlikely(err)) {
203783f6184SRyusuke Konishi 		printk(KERN_ERR
204783f6184SRyusuke Konishi 		       "NILFS: unable to write superblock (err=%d)\n", err);
205e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
206b2ac86e1SJiro SEKIBA 			/*
207b2ac86e1SJiro SEKIBA 			 * sbp[0] points to newer log than sbp[1],
208b2ac86e1SJiro SEKIBA 			 * so copy sbp[0] to sbp[1] to take over sbp[0].
209b2ac86e1SJiro SEKIBA 			 */
210b2ac86e1SJiro SEKIBA 			memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
211b2ac86e1SJiro SEKIBA 			       nilfs->ns_sbsize);
212e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
213e339ad31SRyusuke Konishi 			goto retry;
214e339ad31SRyusuke Konishi 		}
215e339ad31SRyusuke Konishi 	} else {
216e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
217e339ad31SRyusuke Konishi 
218b2ac86e1SJiro SEKIBA 		nilfs->ns_sbwcount++;
219b2ac86e1SJiro SEKIBA 
220e339ad31SRyusuke Konishi 		/*
221e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
222e339ad31SRyusuke Konishi 		 * written in superblock.
223e339ad31SRyusuke Konishi 		 */
224783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
225e339ad31SRyusuke Konishi 
226e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
227e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
228b2ac86e1SJiro SEKIBA 			if (flag == NILFS_SB_COMMIT_ALL) {
229e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
230b2ac86e1SJiro SEKIBA 				if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
231b2ac86e1SJiro SEKIBA 					goto out;
232b2ac86e1SJiro SEKIBA 			}
233b2ac86e1SJiro SEKIBA 			if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
234b2ac86e1SJiro SEKIBA 			    le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
235e339ad31SRyusuke Konishi 				sbp = nilfs->ns_sbp[1];
236e339ad31SRyusuke Konishi 		}
237b2ac86e1SJiro SEKIBA 
2382c2e52fcSRyusuke Konishi 		spin_lock(&nilfs->ns_last_segment_lock);
239e339ad31SRyusuke Konishi 		nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2402c2e52fcSRyusuke Konishi 		spin_unlock(&nilfs->ns_last_segment_lock);
241783f6184SRyusuke Konishi 	}
242b2ac86e1SJiro SEKIBA  out:
243783f6184SRyusuke Konishi 	return err;
244783f6184SRyusuke Konishi }
245783f6184SRyusuke Konishi 
24660f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
24760f46b7eSRyusuke Konishi 			  struct the_nilfs *nilfs)
24860f46b7eSRyusuke Konishi {
24960f46b7eSRyusuke Konishi 	sector_t nfreeblocks;
25060f46b7eSRyusuke Konishi 
25160f46b7eSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
25260f46b7eSRyusuke Konishi 	nilfs_count_free_blocks(nilfs, &nfreeblocks);
25360f46b7eSRyusuke Konishi 	sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
25460f46b7eSRyusuke Konishi 
25560f46b7eSRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
25660f46b7eSRyusuke Konishi 	sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
25760f46b7eSRyusuke Konishi 	sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
25860f46b7eSRyusuke Konishi 	sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
25960f46b7eSRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
26060f46b7eSRyusuke Konishi }
26160f46b7eSRyusuke Konishi 
262b2ac86e1SJiro SEKIBA struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi,
263b2ac86e1SJiro SEKIBA 					       int flip)
264d26493b6SJiro SEKIBA {
265d26493b6SJiro SEKIBA 	struct the_nilfs *nilfs = sbi->s_nilfs;
266d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
267d26493b6SJiro SEKIBA 
268d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
269d26493b6SJiro SEKIBA 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
270d26493b6SJiro SEKIBA 		if (sbp[1] &&
271d26493b6SJiro SEKIBA 		    sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
272b2ac86e1SJiro SEKIBA 			memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
273d26493b6SJiro SEKIBA 		} else {
274d26493b6SJiro SEKIBA 			printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
275d26493b6SJiro SEKIBA 			       sbi->s_super->s_id);
276d26493b6SJiro SEKIBA 			return NULL;
277d26493b6SJiro SEKIBA 		}
278b2ac86e1SJiro SEKIBA 	} else if (sbp[1] &&
279b2ac86e1SJiro SEKIBA 		   sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
280b2ac86e1SJiro SEKIBA 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
281d26493b6SJiro SEKIBA 	}
282b2ac86e1SJiro SEKIBA 
283b2ac86e1SJiro SEKIBA 	if (flip && sbp[1])
284b2ac86e1SJiro SEKIBA 		nilfs_swap_super_block(nilfs);
285b2ac86e1SJiro SEKIBA 
286d26493b6SJiro SEKIBA 	return sbp;
287d26493b6SJiro SEKIBA }
288d26493b6SJiro SEKIBA 
289b2ac86e1SJiro SEKIBA int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag)
290783f6184SRyusuke Konishi {
291783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
292e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
293e339ad31SRyusuke Konishi 	time_t t;
294783f6184SRyusuke Konishi 
295d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
296e339ad31SRyusuke Konishi 	t = get_seconds();
297b2ac86e1SJiro SEKIBA 	nilfs->ns_sbwtime = t;
298e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
299e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
300e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
301e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
302e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
303b2ac86e1SJiro SEKIBA 	if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
304b2ac86e1SJiro SEKIBA 		sbp[1]->s_wtime = sbp[0]->s_wtime;
305b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = 0;
306b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
307b2ac86e1SJiro SEKIBA 					    (unsigned char *)sbp[1],
308b2ac86e1SJiro SEKIBA 					    nilfs->ns_sbsize));
309e339ad31SRyusuke Konishi 	}
310e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
311b2ac86e1SJiro SEKIBA 	return nilfs_sync_super(sbi, flag);
312783f6184SRyusuke Konishi }
313783f6184SRyusuke Konishi 
3147ecaa46cSRyusuke Konishi /**
3157ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
3167ecaa46cSRyusuke Konishi  * @sbi: nilfs_sb_info to be unmounted or degraded to read-only
3177ecaa46cSRyusuke Konishi  *
3187ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
3197ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3207ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
3217ecaa46cSRyusuke Konishi  */
3227ecaa46cSRyusuke Konishi int nilfs_cleanup_super(struct nilfs_sb_info *sbi)
3237ecaa46cSRyusuke Konishi {
324d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
325b2ac86e1SJiro SEKIBA 	int flag = NILFS_SB_COMMIT;
326d26493b6SJiro SEKIBA 	int ret = -EIO;
3277ecaa46cSRyusuke Konishi 
328b2ac86e1SJiro SEKIBA 	sbp = nilfs_prepare_super(sbi, 0);
329d26493b6SJiro SEKIBA 	if (sbp) {
3307ecaa46cSRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state);
331b2ac86e1SJiro SEKIBA 		nilfs_set_log_cursor(sbp[0], sbi->s_nilfs);
332b2ac86e1SJiro SEKIBA 		if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
333b2ac86e1SJiro SEKIBA 			/*
334b2ac86e1SJiro SEKIBA 			 * make the "clean" flag also to the opposite
335b2ac86e1SJiro SEKIBA 			 * super block if both super blocks point to
336b2ac86e1SJiro SEKIBA 			 * the same checkpoint.
337b2ac86e1SJiro SEKIBA 			 */
338b2ac86e1SJiro SEKIBA 			sbp[1]->s_state = sbp[0]->s_state;
339b2ac86e1SJiro SEKIBA 			flag = NILFS_SB_COMMIT_ALL;
340b2ac86e1SJiro SEKIBA 		}
341b2ac86e1SJiro SEKIBA 		ret = nilfs_commit_super(sbi, flag);
342d26493b6SJiro SEKIBA 	}
3437ecaa46cSRyusuke Konishi 	return ret;
3447ecaa46cSRyusuke Konishi }
3457ecaa46cSRyusuke Konishi 
346783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
347783f6184SRyusuke Konishi {
348783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
349783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
350783f6184SRyusuke Konishi 
351783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
352783f6184SRyusuke Konishi 
353783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
354783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
3557ecaa46cSRyusuke Konishi 		nilfs_cleanup_super(sbi);
356783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
357783f6184SRyusuke Konishi 	}
358e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
3593f82ff55SRyusuke Konishi 	if (nilfs->ns_current == sbi)
3603f82ff55SRyusuke Konishi 		nilfs->ns_current = NULL;
361e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
362783f6184SRyusuke Konishi 
363783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
364783f6184SRyusuke Konishi 	put_nilfs(sbi->s_nilfs);
365783f6184SRyusuke Konishi 	sbi->s_super = NULL;
366783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
3676dd47406SRyusuke Konishi 	nilfs_put_sbinfo(sbi);
368783f6184SRyusuke Konishi }
369783f6184SRyusuke Konishi 
370783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
371783f6184SRyusuke Konishi {
3726233caa9SJiro SEKIBA 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
3736233caa9SJiro SEKIBA 	struct the_nilfs *nilfs = sbi->s_nilfs;
374d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
375783f6184SRyusuke Konishi 	int err = 0;
376783f6184SRyusuke Konishi 
377783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
378783f6184SRyusuke Konishi 	if (wait)
379783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
3806233caa9SJiro SEKIBA 
3816233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
382d26493b6SJiro SEKIBA 	if (nilfs_sb_dirty(nilfs)) {
383b2ac86e1SJiro SEKIBA 		sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs));
384b2ac86e1SJiro SEKIBA 		if (likely(sbp)) {
385b2ac86e1SJiro SEKIBA 			nilfs_set_log_cursor(sbp[0], nilfs);
386b2ac86e1SJiro SEKIBA 			nilfs_commit_super(sbi, NILFS_SB_COMMIT);
387b2ac86e1SJiro SEKIBA 		}
388d26493b6SJiro SEKIBA 	}
3896233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
3906233caa9SJiro SEKIBA 
391783f6184SRyusuke Konishi 	return err;
392783f6184SRyusuke Konishi }
393783f6184SRyusuke Konishi 
3944d8d9293SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno, int curr_mnt,
3954d8d9293SRyusuke Konishi 			    struct nilfs_root **rootp)
396783f6184SRyusuke Konishi {
397783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
3984d8d9293SRyusuke Konishi 	struct nilfs_root *root;
399783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
400783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
4014d8d9293SRyusuke Konishi 	int err = -ENOMEM;
4024d8d9293SRyusuke Konishi 
4034d8d9293SRyusuke Konishi 	root = nilfs_find_or_create_root(
4044d8d9293SRyusuke Konishi 		nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
4054d8d9293SRyusuke Konishi 	if (!root)
4064d8d9293SRyusuke Konishi 		return err;
407783f6184SRyusuke Konishi 
408e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
409783f6184SRyusuke Konishi 	list_add(&sbi->s_list, &nilfs->ns_supers);
410e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
411783f6184SRyusuke Konishi 
41279739565SRyusuke Konishi 	sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
413783f6184SRyusuke Konishi 	if (!sbi->s_ifile)
414af4e3631SRyusuke Konishi 		goto delist;
415783f6184SRyusuke Konishi 
4161154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
417783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
418783f6184SRyusuke Konishi 					  &bh_cp);
4191154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
420783f6184SRyusuke Konishi 	if (unlikely(err)) {
421783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
422783f6184SRyusuke Konishi 			printk(KERN_ERR
423783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
424783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
425783f6184SRyusuke Konishi 			       (unsigned long long)cno);
426783f6184SRyusuke Konishi 			err = -EINVAL;
427783f6184SRyusuke Konishi 		}
428783f6184SRyusuke Konishi 		goto failed;
429783f6184SRyusuke Konishi 	}
430783f6184SRyusuke Konishi 	err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode);
431783f6184SRyusuke Konishi 	if (unlikely(err))
432783f6184SRyusuke Konishi 		goto failed_bh;
433783f6184SRyusuke Konishi 	atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
434783f6184SRyusuke Konishi 	atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count));
435783f6184SRyusuke Konishi 
436783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
4374d8d9293SRyusuke Konishi 
4384d8d9293SRyusuke Konishi 	*rootp = root;
439783f6184SRyusuke Konishi 	return 0;
440783f6184SRyusuke Konishi 
441783f6184SRyusuke Konishi  failed_bh:
442783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
443783f6184SRyusuke Konishi  failed:
444783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
445783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
446783f6184SRyusuke Konishi 
447af4e3631SRyusuke Konishi  delist:
448e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
449783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
450e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
4514d8d9293SRyusuke Konishi 	nilfs_put_root(root);
452783f6184SRyusuke Konishi 
453783f6184SRyusuke Konishi 	return err;
454783f6184SRyusuke Konishi }
455783f6184SRyusuke Konishi 
456783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
457783f6184SRyusuke Konishi {
458783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
459783f6184SRyusuke Konishi 
460783f6184SRyusuke Konishi 	nilfs_mdt_destroy(sbi->s_ifile);
461783f6184SRyusuke Konishi 	sbi->s_ifile = NULL;
462e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
463783f6184SRyusuke Konishi 	list_del_init(&sbi->s_list);
464e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
465783f6184SRyusuke Konishi }
466783f6184SRyusuke Konishi 
467783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
468783f6184SRyusuke Konishi {
469783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
470783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
471c306af23SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
472c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
473783f6184SRyusuke Konishi 	unsigned long long blocks;
474783f6184SRyusuke Konishi 	unsigned long overhead;
475783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
476783f6184SRyusuke Konishi 	sector_t nfreeblocks;
477783f6184SRyusuke Konishi 	int err;
478783f6184SRyusuke Konishi 
479783f6184SRyusuke Konishi 	/*
480783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
481783f6184SRyusuke Konishi 	 *
482783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
483783f6184SRyusuke Konishi 	 * are excluded.
484783f6184SRyusuke Konishi 	 */
485783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
486783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
487783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
488783f6184SRyusuke Konishi 
489783f6184SRyusuke Konishi 	/*
490783f6184SRyusuke Konishi 	 * Compute the overhead
491783f6184SRyusuke Konishi 	 *
4927a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
493783f6184SRyusuke Konishi 	 * We must count them as the overhead.
494783f6184SRyusuke Konishi 	 */
495783f6184SRyusuke Konishi 	overhead = 0;
496783f6184SRyusuke Konishi 
497783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
498783f6184SRyusuke Konishi 	if (unlikely(err))
499783f6184SRyusuke Konishi 		return err;
500783f6184SRyusuke Konishi 
501783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
502783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
503783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
504783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
505783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
506783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
507783f6184SRyusuke Konishi 	buf->f_files = atomic_read(&sbi->s_inodes_count);
508783f6184SRyusuke Konishi 	buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
509783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
510c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
511c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
512c306af23SRyusuke Konishi 
513783f6184SRyusuke Konishi 	return 0;
514783f6184SRyusuke Konishi }
515783f6184SRyusuke Konishi 
516b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
517b58a285bSJiro SEKIBA {
518b58a285bSJiro SEKIBA 	struct super_block *sb = vfs->mnt_sb;
519b58a285bSJiro SEKIBA 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
520b58a285bSJiro SEKIBA 
521b58a285bSJiro SEKIBA 	if (!nilfs_test_opt(sbi, BARRIER))
522c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
523b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, SNAPSHOT))
524b58a285bSJiro SEKIBA 		seq_printf(seq, ",cp=%llu",
525b58a285bSJiro SEKIBA 			   (unsigned long long int)sbi->s_snapshot_cno);
526b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
527c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
528277a6a34SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_CONT))
529c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
530b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, STRICT_ORDER))
531c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
5320234576dSRyusuke Konishi 	if (nilfs_test_opt(sbi, NORECOVERY))
533c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
534e902ec99SJiro SEKIBA 	if (nilfs_test_opt(sbi, DISCARD))
535c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
536b58a285bSJiro SEKIBA 
537b58a285bSJiro SEKIBA 	return 0;
538b58a285bSJiro SEKIBA }
539b58a285bSJiro SEKIBA 
540b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
541783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
542783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
543783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
544783f6184SRyusuke Konishi 	/* .write_inode    = nilfs_write_inode, */
545783f6184SRyusuke Konishi 	/* .put_inode      = nilfs_put_inode, */
546783f6184SRyusuke Konishi 	/* .drop_inode	  = nilfs_drop_inode, */
5476fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
548783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
5491dfa2710SJiro SEKIBA 	/* .write_super    = nilfs_write_super, */
550783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
551783f6184SRyusuke Konishi 	/* .write_super_lockfs */
552783f6184SRyusuke Konishi 	/* .unlockfs */
553783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
554783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
555783f6184SRyusuke Konishi 	/* .umount_begin */
556b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
557783f6184SRyusuke Konishi };
558783f6184SRyusuke Konishi 
559783f6184SRyusuke Konishi enum {
560783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
561773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
562802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, Opt_err,
563783f6184SRyusuke Konishi };
564783f6184SRyusuke Konishi 
565783f6184SRyusuke Konishi static match_table_t tokens = {
566783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
567783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
568783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
569773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
57091f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
571783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
572783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
5730234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
574e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
575802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
576783f6184SRyusuke Konishi 	{Opt_err, NULL}
577783f6184SRyusuke Konishi };
578783f6184SRyusuke Konishi 
5797c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
580783f6184SRyusuke Konishi {
581783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
582783f6184SRyusuke Konishi 	char *p;
583783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
584783f6184SRyusuke Konishi 	int option;
585783f6184SRyusuke Konishi 
586783f6184SRyusuke Konishi 	if (!options)
587783f6184SRyusuke Konishi 		return 1;
588783f6184SRyusuke Konishi 
589783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
590783f6184SRyusuke Konishi 		int token;
591783f6184SRyusuke Konishi 		if (!*p)
592783f6184SRyusuke Konishi 			continue;
593783f6184SRyusuke Konishi 
594783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
595783f6184SRyusuke Konishi 		switch (token) {
596773bc4f3SRyusuke Konishi 		case Opt_barrier:
597773bc4f3SRyusuke Konishi 			nilfs_set_opt(sbi, BARRIER);
598773bc4f3SRyusuke Konishi 			break;
59991f1953bSJiro SEKIBA 		case Opt_nobarrier:
600783f6184SRyusuke Konishi 			nilfs_clear_opt(sbi, BARRIER);
601783f6184SRyusuke Konishi 			break;
602783f6184SRyusuke Konishi 		case Opt_order:
603783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
604783f6184SRyusuke Konishi 				/* Ordered data semantics */
605783f6184SRyusuke Konishi 				nilfs_clear_opt(sbi, STRICT_ORDER);
606783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
607783f6184SRyusuke Konishi 				/* Strict in-order semantics */
608783f6184SRyusuke Konishi 				nilfs_set_opt(sbi, STRICT_ORDER);
609783f6184SRyusuke Konishi 			else
610783f6184SRyusuke Konishi 				return 0;
611783f6184SRyusuke Konishi 			break;
612783f6184SRyusuke Konishi 		case Opt_err_panic:
613783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
614783f6184SRyusuke Konishi 			break;
615783f6184SRyusuke Konishi 		case Opt_err_ro:
616783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
617783f6184SRyusuke Konishi 			break;
618783f6184SRyusuke Konishi 		case Opt_err_cont:
619783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
620783f6184SRyusuke Konishi 			break;
621783f6184SRyusuke Konishi 		case Opt_snapshot:
622783f6184SRyusuke Konishi 			if (match_int(&args[0], &option) || option <= 0)
623783f6184SRyusuke Konishi 				return 0;
6247c017457SRyusuke Konishi 			if (is_remount) {
6257c017457SRyusuke Konishi 				if (!nilfs_test_opt(sbi, SNAPSHOT)) {
6267c017457SRyusuke Konishi 					printk(KERN_ERR
6277c017457SRyusuke Konishi 					       "NILFS: cannot change regular "
6287c017457SRyusuke Konishi 					       "mount to snapshot.\n");
629783f6184SRyusuke Konishi 					return 0;
6307c017457SRyusuke Konishi 				} else if (option != sbi->s_snapshot_cno) {
6317c017457SRyusuke Konishi 					printk(KERN_ERR
6327c017457SRyusuke Konishi 					       "NILFS: cannot remount to a "
6337c017457SRyusuke Konishi 					       "different snapshot.\n");
6347c017457SRyusuke Konishi 					return 0;
6357c017457SRyusuke Konishi 				}
6367c017457SRyusuke Konishi 				break;
6377c017457SRyusuke Konishi 			}
6387c017457SRyusuke Konishi 			if (!(sb->s_flags & MS_RDONLY)) {
6397c017457SRyusuke Konishi 				printk(KERN_ERR "NILFS: cannot mount snapshot "
6407c017457SRyusuke Konishi 				       "read/write.  A read-only option is "
6417c017457SRyusuke Konishi 				       "required.\n");
6427c017457SRyusuke Konishi 				return 0;
6437c017457SRyusuke Konishi 			}
644783f6184SRyusuke Konishi 			sbi->s_snapshot_cno = option;
645783f6184SRyusuke Konishi 			nilfs_set_opt(sbi, SNAPSHOT);
646783f6184SRyusuke Konishi 			break;
6470234576dSRyusuke Konishi 		case Opt_norecovery:
6480234576dSRyusuke Konishi 			nilfs_set_opt(sbi, NORECOVERY);
6490234576dSRyusuke Konishi 			break;
650e902ec99SJiro SEKIBA 		case Opt_discard:
651e902ec99SJiro SEKIBA 			nilfs_set_opt(sbi, DISCARD);
652e902ec99SJiro SEKIBA 			break;
653802d3177SRyusuke Konishi 		case Opt_nodiscard:
654802d3177SRyusuke Konishi 			nilfs_clear_opt(sbi, DISCARD);
655802d3177SRyusuke Konishi 			break;
656783f6184SRyusuke Konishi 		default:
657783f6184SRyusuke Konishi 			printk(KERN_ERR
658783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
659783f6184SRyusuke Konishi 			return 0;
660783f6184SRyusuke Konishi 		}
661783f6184SRyusuke Konishi 	}
662783f6184SRyusuke Konishi 	return 1;
663783f6184SRyusuke Konishi }
664783f6184SRyusuke Konishi 
665783f6184SRyusuke Konishi static inline void
666783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi,
667783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
668783f6184SRyusuke Konishi {
669783f6184SRyusuke Konishi 	sbi->s_mount_opt =
670277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
671783f6184SRyusuke Konishi }
672783f6184SRyusuke Konishi 
673783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi)
674783f6184SRyusuke Konishi {
675783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
676d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
677d26493b6SJiro SEKIBA 	int max_mnt_count;
678d26493b6SJiro SEKIBA 	int mnt_count;
679783f6184SRyusuke Konishi 
680d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
681b2ac86e1SJiro SEKIBA 	sbp = nilfs_prepare_super(sbi, 0);
682d26493b6SJiro SEKIBA 	if (!sbp)
683d26493b6SJiro SEKIBA 		return -EIO;
684d26493b6SJiro SEKIBA 
685d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
686d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
687d26493b6SJiro SEKIBA 
688f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
689783f6184SRyusuke Konishi 		printk(KERN_WARNING
690783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
691783f6184SRyusuke Konishi #if 0
692783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
693783f6184SRyusuke Konishi 		printk(KERN_WARNING
694783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
695783f6184SRyusuke Konishi #endif
696783f6184SRyusuke Konishi 	}
697783f6184SRyusuke Konishi 	if (!max_mnt_count)
698d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
699783f6184SRyusuke Konishi 
700d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
701d26493b6SJiro SEKIBA 	sbp[0]->s_state =
702d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
703d26493b6SJiro SEKIBA 	sbp[0]->s_mtime = cpu_to_le64(get_seconds());
704b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
705b2ac86e1SJiro SEKIBA 	memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
706b2ac86e1SJiro SEKIBA 	return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
707783f6184SRyusuke Konishi }
708783f6184SRyusuke Konishi 
709e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
710e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
711e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
712783f6184SRyusuke Konishi {
713e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
714e339ad31SRyusuke Konishi 	unsigned long offset;
715783f6184SRyusuke Konishi 
716e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
717783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
718e339ad31SRyusuke Konishi 	if (!*pbh)
719783f6184SRyusuke Konishi 		return NULL;
720783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
721783f6184SRyusuke Konishi }
722783f6184SRyusuke Konishi 
723783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
724783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
725783f6184SRyusuke Konishi 				 char *data)
726783f6184SRyusuke Konishi {
727783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
728783f6184SRyusuke Konishi 
729783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
730783f6184SRyusuke Konishi 
731783f6184SRyusuke Konishi 	/* FS independent flags */
732783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
733783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
734783f6184SRyusuke Konishi #endif
735783f6184SRyusuke Konishi 
736783f6184SRyusuke Konishi 	nilfs_set_default_options(sbi, sbp);
737783f6184SRyusuke Konishi 
738783f6184SRyusuke Konishi 	sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
739783f6184SRyusuke Konishi 	sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
740783f6184SRyusuke Konishi 	sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
741783f6184SRyusuke Konishi 	sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
742783f6184SRyusuke Konishi 
7437c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
744783f6184SRyusuke Konishi }
745783f6184SRyusuke Konishi 
746c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
747c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
748c5ca48aaSRyusuke Konishi {
749c5ca48aaSRyusuke Konishi 	__u64 features;
750c5ca48aaSRyusuke Konishi 
751c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
752c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
753c5ca48aaSRyusuke Konishi 	if (features) {
754c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
755c5ca48aaSRyusuke Konishi 		       "optional features (%llx)\n",
756c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
757c5ca48aaSRyusuke Konishi 		return -EINVAL;
758c5ca48aaSRyusuke Konishi 	}
759c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
760c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
761c5ca48aaSRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY) && features) {
762c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
763c5ca48aaSRyusuke Konishi 		       "unsupported optional features (%llx)\n",
764c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
765c5ca48aaSRyusuke Konishi 		return -EINVAL;
766c5ca48aaSRyusuke Konishi 	}
767c5ca48aaSRyusuke Konishi 	return 0;
768c5ca48aaSRyusuke Konishi }
769c5ca48aaSRyusuke Konishi 
770783f6184SRyusuke Konishi /**
771783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
772783f6184SRyusuke Konishi  * @sb: super_block
773783f6184SRyusuke Konishi  * @data: mount options
774783f6184SRyusuke Konishi  * @silent: silent mode flag
775783f6184SRyusuke Konishi  * @nilfs: the_nilfs struct
776783f6184SRyusuke Konishi  *
777aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
778783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
779783f6184SRyusuke Konishi  */
780783f6184SRyusuke Konishi static int
781783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent,
782783f6184SRyusuke Konishi 		 struct the_nilfs *nilfs)
783783f6184SRyusuke Konishi {
784783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi;
7854d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
786783f6184SRyusuke Konishi 	struct inode *root;
787783f6184SRyusuke Konishi 	__u64 cno;
7884d8d9293SRyusuke Konishi 	int err, curr_mnt;
789783f6184SRyusuke Konishi 
790783f6184SRyusuke Konishi 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
791783f6184SRyusuke Konishi 	if (!sbi)
792783f6184SRyusuke Konishi 		return -ENOMEM;
793783f6184SRyusuke Konishi 
794783f6184SRyusuke Konishi 	sb->s_fs_info = sbi;
795783f6184SRyusuke Konishi 
796783f6184SRyusuke Konishi 	get_nilfs(nilfs);
797783f6184SRyusuke Konishi 	sbi->s_nilfs = nilfs;
798783f6184SRyusuke Konishi 	sbi->s_super = sb;
7996dd47406SRyusuke Konishi 	atomic_set(&sbi->s_count, 1);
800783f6184SRyusuke Konishi 
801783f6184SRyusuke Konishi 	err = init_nilfs(nilfs, sbi, (char *)data);
802783f6184SRyusuke Konishi 	if (err)
803783f6184SRyusuke Konishi 		goto failed_sbi;
804783f6184SRyusuke Konishi 
805783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_inode_lock);
806783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_dirty_files);
807783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_list);
808783f6184SRyusuke Konishi 
809783f6184SRyusuke Konishi 	/*
810783f6184SRyusuke Konishi 	 * Following initialization is overlapped because
811783f6184SRyusuke Konishi 	 * nilfs_sb_info structure has been cleared at the beginning.
812783f6184SRyusuke Konishi 	 * But we reserve them to keep our interest and make ready
813783f6184SRyusuke Konishi 	 * for the future change.
814783f6184SRyusuke Konishi 	 */
815783f6184SRyusuke Konishi 	get_random_bytes(&sbi->s_next_generation,
816783f6184SRyusuke Konishi 			 sizeof(sbi->s_next_generation));
817783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_next_gen_lock);
818783f6184SRyusuke Konishi 
819783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
820783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
821783f6184SRyusuke Konishi 	sb->s_root = NULL;
82261239230SRyusuke Konishi 	sb->s_time_gran = 1;
823973bec34SRyusuke Konishi 	sb->s_bdi = nilfs->ns_bdi;
824783f6184SRyusuke Konishi 
825783f6184SRyusuke Konishi 	err = load_nilfs(nilfs, sbi);
826783f6184SRyusuke Konishi 	if (err)
827783f6184SRyusuke Konishi 		goto failed_sbi;
828f50a4c81SRyusuke Konishi 
829783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
8304d8d9293SRyusuke Konishi 	curr_mnt = true;
831783f6184SRyusuke Konishi 
832783f6184SRyusuke Konishi 	if (sb->s_flags & MS_RDONLY) {
833783f6184SRyusuke Konishi 		if (nilfs_test_opt(sbi, SNAPSHOT)) {
83443be0ec0SZhu Yanhai 			down_read(&nilfs->ns_segctor_sem);
8351f5abe7eSRyusuke Konishi 			err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
8361f5abe7eSRyusuke Konishi 						       sbi->s_snapshot_cno);
83743be0ec0SZhu Yanhai 			up_read(&nilfs->ns_segctor_sem);
83843be0ec0SZhu Yanhai 			if (err < 0) {
83943be0ec0SZhu Yanhai 				if (err == -ENOENT)
84043be0ec0SZhu Yanhai 					err = -EINVAL;
8411f5abe7eSRyusuke Konishi 				goto failed_sbi;
84243be0ec0SZhu Yanhai 			}
8431f5abe7eSRyusuke Konishi 			if (!err) {
844783f6184SRyusuke Konishi 				printk(KERN_ERR
845783f6184SRyusuke Konishi 				       "NILFS: The specified checkpoint is "
846783f6184SRyusuke Konishi 				       "not a snapshot "
847783f6184SRyusuke Konishi 				       "(checkpoint number=%llu).\n",
848783f6184SRyusuke Konishi 				       (unsigned long long)sbi->s_snapshot_cno);
849783f6184SRyusuke Konishi 				err = -EINVAL;
850783f6184SRyusuke Konishi 				goto failed_sbi;
851783f6184SRyusuke Konishi 			}
852783f6184SRyusuke Konishi 			cno = sbi->s_snapshot_cno;
8534d8d9293SRyusuke Konishi 			curr_mnt = false;
854d240e067SRyusuke Konishi 		}
855783f6184SRyusuke Konishi 	}
856783f6184SRyusuke Konishi 
8574d8d9293SRyusuke Konishi 	err = nilfs_attach_checkpoint(sbi, cno, curr_mnt, &fsroot);
858783f6184SRyusuke Konishi 	if (err) {
859783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading a checkpoint"
860783f6184SRyusuke Konishi 		       " (checkpoint number=%llu).\n", (unsigned long long)cno);
861783f6184SRyusuke Konishi 		goto failed_sbi;
862783f6184SRyusuke Konishi 	}
863783f6184SRyusuke Konishi 
864783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
865cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
866783f6184SRyusuke Konishi 		if (err)
867783f6184SRyusuke Konishi 			goto failed_checkpoint;
868783f6184SRyusuke Konishi 	}
869783f6184SRyusuke Konishi 
8704d8d9293SRyusuke Konishi 	root = nilfs_iget(sb, fsroot, NILFS_ROOT_INO);
871783f6184SRyusuke Konishi 	if (IS_ERR(root)) {
872783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
873783f6184SRyusuke Konishi 		err = PTR_ERR(root);
874783f6184SRyusuke Konishi 		goto failed_segctor;
875783f6184SRyusuke Konishi 	}
876783f6184SRyusuke Konishi 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
877783f6184SRyusuke Konishi 		iput(root);
878783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
879783f6184SRyusuke Konishi 		err = -EINVAL;
880783f6184SRyusuke Konishi 		goto failed_segctor;
881783f6184SRyusuke Konishi 	}
882783f6184SRyusuke Konishi 	sb->s_root = d_alloc_root(root);
883783f6184SRyusuke Konishi 	if (!sb->s_root) {
884783f6184SRyusuke Konishi 		iput(root);
885783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root dentry failed\n");
886783f6184SRyusuke Konishi 		err = -ENOMEM;
887783f6184SRyusuke Konishi 		goto failed_segctor;
888783f6184SRyusuke Konishi 	}
889783f6184SRyusuke Konishi 
8904d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
8914d8d9293SRyusuke Konishi 
892783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
893783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
894783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
895783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
896783f6184SRyusuke Konishi 	}
897783f6184SRyusuke Konishi 
898e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
8993f82ff55SRyusuke Konishi 	if (!nilfs_test_opt(sbi, SNAPSHOT))
9003f82ff55SRyusuke Konishi 		nilfs->ns_current = sbi;
901e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
9023f82ff55SRyusuke Konishi 
903783f6184SRyusuke Konishi 	return 0;
904783f6184SRyusuke Konishi 
905783f6184SRyusuke Konishi  failed_segctor:
906783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
907783f6184SRyusuke Konishi 
908783f6184SRyusuke Konishi  failed_checkpoint:
909783f6184SRyusuke Konishi 	nilfs_detach_checkpoint(sbi);
9104d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
911783f6184SRyusuke Konishi 
912783f6184SRyusuke Konishi  failed_sbi:
913783f6184SRyusuke Konishi 	put_nilfs(nilfs);
914783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
9156dd47406SRyusuke Konishi 	nilfs_put_sbinfo(sbi);
916783f6184SRyusuke Konishi 	return err;
917783f6184SRyusuke Konishi }
918783f6184SRyusuke Konishi 
919783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
920783f6184SRyusuke Konishi {
921783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
922783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
923783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
924783f6184SRyusuke Konishi 	struct nilfs_mount_options old_opts;
925d240e067SRyusuke Konishi 	int was_snapshot, err;
926783f6184SRyusuke Konishi 
927e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
928783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
929783f6184SRyusuke Konishi 	old_opts.mount_opt = sbi->s_mount_opt;
930783f6184SRyusuke Konishi 	old_opts.snapshot_cno = sbi->s_snapshot_cno;
931d240e067SRyusuke Konishi 	was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
932783f6184SRyusuke Konishi 
9337c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
934783f6184SRyusuke Konishi 		err = -EINVAL;
935783f6184SRyusuke Konishi 		goto restore_opts;
936783f6184SRyusuke Konishi 	}
937783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
938783f6184SRyusuke Konishi 
939d240e067SRyusuke Konishi 	err = -EINVAL;
9407c017457SRyusuke Konishi 	if (was_snapshot && !(*flags & MS_RDONLY)) {
9417c017457SRyusuke Konishi 		printk(KERN_ERR "NILFS (device %s): cannot remount snapshot "
9427c017457SRyusuke Konishi 		       "read/write.\n", sb->s_id);
943d240e067SRyusuke Konishi 		goto restore_opts;
944d240e067SRyusuke Konishi 	}
945783f6184SRyusuke Konishi 
9460234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
9470234576dSRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
9480234576dSRyusuke Konishi 		       "remount because the filesystem is in an "
9490234576dSRyusuke Konishi 		       "incomplete recovery state.\n", sb->s_id);
9500234576dSRyusuke Konishi 		goto restore_opts;
9510234576dSRyusuke Konishi 	}
9520234576dSRyusuke Konishi 
953783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
954783f6184SRyusuke Konishi 		goto out;
955783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
956783f6184SRyusuke Konishi 		/* Shutting down the segment constructor */
957783f6184SRyusuke Konishi 		nilfs_detach_segment_constructor(sbi);
958783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
959783f6184SRyusuke Konishi 
960783f6184SRyusuke Konishi 		/*
961783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
962783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
963783f6184SRyusuke Konishi 		 */
964783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
9657ecaa46cSRyusuke Konishi 		nilfs_cleanup_super(sbi);
966783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
967783f6184SRyusuke Konishi 	} else {
968c5ca48aaSRyusuke Konishi 		__u64 features;
969c5ca48aaSRyusuke Konishi 
970783f6184SRyusuke Konishi 		/*
971783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
972783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
973783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
974783f6184SRyusuke Konishi 		 */
975c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
976c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
977c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
978c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
979c5ca48aaSRyusuke Konishi 		if (features) {
980c5ca48aaSRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
981c5ca48aaSRyusuke Konishi 			       "remount RDWR because of unsupported optional "
982c5ca48aaSRyusuke Konishi 			       "features (%llx)\n",
983c5ca48aaSRyusuke Konishi 			       sb->s_id, (unsigned long long)features);
984c5ca48aaSRyusuke Konishi 			err = -EROFS;
985c5ca48aaSRyusuke Konishi 			goto restore_opts;
986c5ca48aaSRyusuke Konishi 		}
987c5ca48aaSRyusuke Konishi 
988783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
989783f6184SRyusuke Konishi 
990cece5520SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi);
991783f6184SRyusuke Konishi 		if (err)
992e59399d0SRyusuke Konishi 			goto restore_opts;
993783f6184SRyusuke Konishi 
994783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
995783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
996783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
997783f6184SRyusuke Konishi 	}
998783f6184SRyusuke Konishi  out:
999e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
1000783f6184SRyusuke Konishi 	return 0;
1001783f6184SRyusuke Konishi 
1002783f6184SRyusuke Konishi  restore_opts:
1003783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
1004783f6184SRyusuke Konishi 	sbi->s_mount_opt = old_opts.mount_opt;
1005783f6184SRyusuke Konishi 	sbi->s_snapshot_cno = old_opts.snapshot_cno;
1006e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
1007783f6184SRyusuke Konishi 	return err;
1008783f6184SRyusuke Konishi }
1009783f6184SRyusuke Konishi 
1010783f6184SRyusuke Konishi struct nilfs_super_data {
1011783f6184SRyusuke Konishi 	struct block_device *bdev;
10126dd47406SRyusuke Konishi 	struct nilfs_sb_info *sbi;
1013783f6184SRyusuke Konishi 	__u64 cno;
1014783f6184SRyusuke Konishi 	int flags;
1015783f6184SRyusuke Konishi };
1016783f6184SRyusuke Konishi 
1017783f6184SRyusuke Konishi /**
1018783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1019783f6184SRyusuke Konishi  * @data: mount options
1020783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1021783f6184SRyusuke Konishi  */
1022783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1023783f6184SRyusuke Konishi {
1024783f6184SRyusuke Konishi 	char *p, *options = data;
1025783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1026783f6184SRyusuke Konishi 	int option, token;
1027783f6184SRyusuke Konishi 	int ret = 0;
1028783f6184SRyusuke Konishi 
1029783f6184SRyusuke Konishi 	do {
1030783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1031783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1032783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1033783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
1034783f6184SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY))
1035783f6184SRyusuke Konishi 					ret++;
1036783f6184SRyusuke Konishi 				else {
1037783f6184SRyusuke Konishi 					ret = match_int(&args[0], &option);
1038783f6184SRyusuke Konishi 					if (!ret) {
1039783f6184SRyusuke Konishi 						if (option > 0)
1040783f6184SRyusuke Konishi 							sd->cno = option;
1041783f6184SRyusuke Konishi 						else
1042783f6184SRyusuke Konishi 							ret++;
1043783f6184SRyusuke Konishi 					}
1044783f6184SRyusuke Konishi 				}
1045783f6184SRyusuke Konishi 			}
1046783f6184SRyusuke Konishi 			if (ret)
1047783f6184SRyusuke Konishi 				printk(KERN_ERR
1048783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
1049783f6184SRyusuke Konishi 		}
1050783f6184SRyusuke Konishi 		if (!options)
1051783f6184SRyusuke Konishi 			break;
1052783f6184SRyusuke Konishi 		BUG_ON(options == data);
1053783f6184SRyusuke Konishi 		*(options - 1) = ',';
1054783f6184SRyusuke Konishi 	} while (!ret);
1055783f6184SRyusuke Konishi 	return ret;
1056783f6184SRyusuke Konishi }
1057783f6184SRyusuke Konishi 
1058783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1059783f6184SRyusuke Konishi {
1060783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1061783f6184SRyusuke Konishi 
1062783f6184SRyusuke Konishi 	s->s_bdev = sd->bdev;
1063783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1064783f6184SRyusuke Konishi 	return 0;
1065783f6184SRyusuke Konishi }
1066783f6184SRyusuke Konishi 
1067783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1068783f6184SRyusuke Konishi {
1069783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1070783f6184SRyusuke Konishi 
10716dd47406SRyusuke Konishi 	return sd->sbi && s->s_fs_info == (void *)sd->sbi;
1072783f6184SRyusuke Konishi }
1073783f6184SRyusuke Konishi 
1074783f6184SRyusuke Konishi static int
1075783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags,
1076783f6184SRyusuke Konishi 	     const char *dev_name, void *data, struct vfsmount *mnt)
1077783f6184SRyusuke Konishi {
1078783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
107933c8e57cSRyusuke Konishi 	struct super_block *s;
108013e90559SRyusuke Konishi 	fmode_t mode = FMODE_READ;
108133c8e57cSRyusuke Konishi 	struct the_nilfs *nilfs;
1082783f6184SRyusuke Konishi 	int err, need_to_close = 1;
1083783f6184SRyusuke Konishi 
108413e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
108513e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
108613e90559SRyusuke Konishi 
108713e90559SRyusuke Konishi 	sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
1088d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1089783f6184SRyusuke Konishi 		return PTR_ERR(sd.bdev);
1090783f6184SRyusuke Konishi 
1091783f6184SRyusuke Konishi 	/*
1092783f6184SRyusuke Konishi 	 * To get mount instance using sget() vfs-routine, NILFS needs
1093783f6184SRyusuke Konishi 	 * much more information than normal filesystems to identify mount
1094783f6184SRyusuke Konishi 	 * instance.  For snapshot mounts, not only a mount type (ro-mount
1095783f6184SRyusuke Konishi 	 * or rw-mount) but also a checkpoint number is required.
1096783f6184SRyusuke Konishi 	 */
1097783f6184SRyusuke Konishi 	sd.cno = 0;
1098783f6184SRyusuke Konishi 	sd.flags = flags;
1099783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1100783f6184SRyusuke Konishi 		err = -EINVAL;
1101783f6184SRyusuke Konishi 		goto failed;
1102783f6184SRyusuke Konishi 	}
1103783f6184SRyusuke Konishi 
110433c8e57cSRyusuke Konishi 	nilfs = find_or_create_nilfs(sd.bdev);
110533c8e57cSRyusuke Konishi 	if (!nilfs) {
110633c8e57cSRyusuke Konishi 		err = -ENOMEM;
110733c8e57cSRyusuke Konishi 		goto failed;
110833c8e57cSRyusuke Konishi 	}
110933c8e57cSRyusuke Konishi 
1110aa7dfb89SRyusuke Konishi 	mutex_lock(&nilfs->ns_mount_mutex);
11113f82ff55SRyusuke Konishi 
11123f82ff55SRyusuke Konishi 	if (!sd.cno) {
11133f82ff55SRyusuke Konishi 		/*
11143f82ff55SRyusuke Konishi 		 * Check if an exclusive mount exists or not.
11153f82ff55SRyusuke Konishi 		 * Snapshot mounts coexist with a current mount
11163f82ff55SRyusuke Konishi 		 * (i.e. rw-mount or ro-mount), whereas rw-mount and
11173f82ff55SRyusuke Konishi 		 * ro-mount are mutually exclusive.
11183f82ff55SRyusuke Konishi 		 */
1119e59399d0SRyusuke Konishi 		down_read(&nilfs->ns_super_sem);
11203f82ff55SRyusuke Konishi 		if (nilfs->ns_current &&
11213f82ff55SRyusuke Konishi 		    ((nilfs->ns_current->s_super->s_flags ^ flags)
11223f82ff55SRyusuke Konishi 		     & MS_RDONLY)) {
1123e59399d0SRyusuke Konishi 			up_read(&nilfs->ns_super_sem);
11243f82ff55SRyusuke Konishi 			err = -EBUSY;
1125783f6184SRyusuke Konishi 			goto failed_unlock;
1126783f6184SRyusuke Konishi 		}
1127e59399d0SRyusuke Konishi 		up_read(&nilfs->ns_super_sem);
11283f82ff55SRyusuke Konishi 	}
1129783f6184SRyusuke Konishi 
1130783f6184SRyusuke Konishi 	/*
11316dd47406SRyusuke Konishi 	 * Find existing nilfs_sb_info struct
1132783f6184SRyusuke Konishi 	 */
11336dd47406SRyusuke Konishi 	sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
11346dd47406SRyusuke Konishi 
11356dd47406SRyusuke Konishi 	/*
11366dd47406SRyusuke Konishi 	 * Get super block instance holding the nilfs_sb_info struct.
11376dd47406SRyusuke Konishi 	 * A new instance is allocated if no existing mount is present or
11386dd47406SRyusuke Konishi 	 * existing instance has been unmounted.
11396dd47406SRyusuke Konishi 	 */
114033c8e57cSRyusuke Konishi 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
11416dd47406SRyusuke Konishi 	if (sd.sbi)
11426dd47406SRyusuke Konishi 		nilfs_put_sbinfo(sd.sbi);
11436dd47406SRyusuke Konishi 
114433c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
114533c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
114633c8e57cSRyusuke Konishi 		goto failed_unlock;
1147783f6184SRyusuke Konishi 	}
1148783f6184SRyusuke Konishi 
1149783f6184SRyusuke Konishi 	if (!s->s_root) {
1150783f6184SRyusuke Konishi 		char b[BDEVNAME_SIZE];
1151783f6184SRyusuke Konishi 
115233c8e57cSRyusuke Konishi 		/* New superblock instance created */
1153783f6184SRyusuke Konishi 		s->s_flags = flags;
11544571b82cSRyusuke Konishi 		s->s_mode = mode;
1155783f6184SRyusuke Konishi 		strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
1156783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1157783f6184SRyusuke Konishi 
1158e2d1591aSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
1159e2d1591aSRyusuke Konishi 				       nilfs);
1160783f6184SRyusuke Konishi 		if (err)
1161783f6184SRyusuke Konishi 			goto cancel_new;
1162783f6184SRyusuke Konishi 
1163783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1164783f6184SRyusuke Konishi 		need_to_close = 0;
1165783f6184SRyusuke Konishi 	}
1166783f6184SRyusuke Konishi 
1167aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1168783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1169783f6184SRyusuke Konishi 	if (need_to_close)
117013e90559SRyusuke Konishi 		close_bdev_exclusive(sd.bdev, mode);
1171783f6184SRyusuke Konishi 	simple_set_mnt(mnt, s);
1172783f6184SRyusuke Konishi 	return 0;
1173783f6184SRyusuke Konishi 
1174783f6184SRyusuke Konishi  failed_unlock:
1175aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
117633c8e57cSRyusuke Konishi 	put_nilfs(nilfs);
1177783f6184SRyusuke Konishi  failed:
117813e90559SRyusuke Konishi 	close_bdev_exclusive(sd.bdev, mode);
1179783f6184SRyusuke Konishi 	return err;
1180783f6184SRyusuke Konishi 
1181783f6184SRyusuke Konishi  cancel_new:
1182783f6184SRyusuke Konishi 	/* Abandoning the newly allocated superblock */
1183aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1184783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1185a95161aaSAl Viro 	deactivate_locked_super(s);
1186783f6184SRyusuke Konishi 	/*
1187b87ca919SRyusuke Konishi 	 * deactivate_locked_super() invokes close_bdev_exclusive().
1188783f6184SRyusuke Konishi 	 * We must finish all post-cleaning before this call;
1189aa7dfb89SRyusuke Konishi 	 * put_nilfs() needs the block device.
1190783f6184SRyusuke Konishi 	 */
1191783f6184SRyusuke Konishi 	return err;
1192783f6184SRyusuke Konishi }
1193783f6184SRyusuke Konishi 
1194783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1195783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1196783f6184SRyusuke Konishi 	.name     = "nilfs2",
1197783f6184SRyusuke Konishi 	.get_sb   = nilfs_get_sb,
1198783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1199783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1200783f6184SRyusuke Konishi };
1201783f6184SRyusuke Konishi 
120241c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
120341c88bd7SLi Hong {
120441c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
120541c88bd7SLi Hong 
120641c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
120741c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
120841c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
120941c88bd7SLi Hong #endif
121041c88bd7SLi Hong 	nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
121105d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
121241c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
121341c88bd7SLi Hong }
121441c88bd7SLi Hong 
121541c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
121641c88bd7SLi Hong {
121741c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
121841c88bd7SLi Hong }
121941c88bd7SLi Hong 
122041c88bd7SLi Hong static void nilfs_destroy_cachep(void)
122141c88bd7SLi Hong {
122241c88bd7SLi Hong 	if (nilfs_inode_cachep)
122341c88bd7SLi Hong 		kmem_cache_destroy(nilfs_inode_cachep);
122441c88bd7SLi Hong 	if (nilfs_transaction_cachep)
122541c88bd7SLi Hong 		kmem_cache_destroy(nilfs_transaction_cachep);
122641c88bd7SLi Hong 	if (nilfs_segbuf_cachep)
122741c88bd7SLi Hong 		kmem_cache_destroy(nilfs_segbuf_cachep);
122841c88bd7SLi Hong 	if (nilfs_btree_path_cache)
122941c88bd7SLi Hong 		kmem_cache_destroy(nilfs_btree_path_cache);
123041c88bd7SLi Hong }
123141c88bd7SLi Hong 
123241c88bd7SLi Hong static int __init nilfs_init_cachep(void)
123341c88bd7SLi Hong {
123441c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
123541c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
123641c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
123741c88bd7SLi Hong 	if (!nilfs_inode_cachep)
123841c88bd7SLi Hong 		goto fail;
123941c88bd7SLi Hong 
124041c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
124141c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
124241c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
124341c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
124441c88bd7SLi Hong 		goto fail;
124541c88bd7SLi Hong 
124641c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
124741c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
124841c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
124941c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
125041c88bd7SLi Hong 		goto fail;
125141c88bd7SLi Hong 
125241c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
125341c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
125441c88bd7SLi Hong 			0, 0, NULL);
125541c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
125641c88bd7SLi Hong 		goto fail;
125741c88bd7SLi Hong 
125841c88bd7SLi Hong 	return 0;
125941c88bd7SLi Hong 
126041c88bd7SLi Hong fail:
126141c88bd7SLi Hong 	nilfs_destroy_cachep();
126241c88bd7SLi Hong 	return -ENOMEM;
126341c88bd7SLi Hong }
126441c88bd7SLi Hong 
1265783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1266783f6184SRyusuke Konishi {
1267783f6184SRyusuke Konishi 	int err;
1268783f6184SRyusuke Konishi 
126941c88bd7SLi Hong 	err = nilfs_init_cachep();
1270783f6184SRyusuke Konishi 	if (err)
127141c88bd7SLi Hong 		goto fail;
1272783f6184SRyusuke Konishi 
1273783f6184SRyusuke Konishi 	err = register_filesystem(&nilfs_fs_type);
1274783f6184SRyusuke Konishi 	if (err)
127541c88bd7SLi Hong 		goto free_cachep;
1276783f6184SRyusuke Konishi 
12779f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1278783f6184SRyusuke Konishi 	return 0;
1279783f6184SRyusuke Konishi 
128041c88bd7SLi Hong free_cachep:
128141c88bd7SLi Hong 	nilfs_destroy_cachep();
128241c88bd7SLi Hong fail:
1283783f6184SRyusuke Konishi 	return err;
1284783f6184SRyusuke Konishi }
1285783f6184SRyusuke Konishi 
1286783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1287783f6184SRyusuke Konishi {
128841c88bd7SLi Hong 	nilfs_destroy_cachep();
1289783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1290783f6184SRyusuke Konishi }
1291783f6184SRyusuke Konishi 
1292783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1293783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1294