xref: /openbmc/linux/fs/nilfs2/super.c (revision ab4d8f7ebf33beff97e766d18db47f1ea9635769)
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"
548e656fd5SRyusuke 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;
361e912a5b6SRyusuke Konishi 	list_del_init(&sbi->s_list);
362e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
363783f6184SRyusuke Konishi 
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 
408e912a5b6SRyusuke Konishi 	if (root->ifile)
409e912a5b6SRyusuke Konishi 		goto reuse; /* already attached checkpoint */
410783f6184SRyusuke Konishi 
411e912a5b6SRyusuke Konishi 	root->ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size);
412e912a5b6SRyusuke Konishi 	if (!root->ifile)
413e912a5b6SRyusuke Konishi 		goto failed;
414783f6184SRyusuke Konishi 
4151154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
416783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
417783f6184SRyusuke Konishi 					  &bh_cp);
4181154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
419783f6184SRyusuke Konishi 	if (unlikely(err)) {
420783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
421783f6184SRyusuke Konishi 			printk(KERN_ERR
422783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
423783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
424783f6184SRyusuke Konishi 			       (unsigned long long)cno);
425783f6184SRyusuke Konishi 			err = -EINVAL;
426783f6184SRyusuke Konishi 		}
427783f6184SRyusuke Konishi 		goto failed;
428783f6184SRyusuke Konishi 	}
429e912a5b6SRyusuke Konishi 	err = nilfs_read_inode_common(root->ifile, &raw_cp->cp_ifile_inode);
430783f6184SRyusuke Konishi 	if (unlikely(err))
431783f6184SRyusuke Konishi 		goto failed_bh;
432b7c06342SRyusuke Konishi 
433b7c06342SRyusuke Konishi 	atomic_set(&root->inodes_count, le64_to_cpu(raw_cp->cp_inodes_count));
434b7c06342SRyusuke Konishi 	atomic_set(&root->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 
438e912a5b6SRyusuke Konishi  reuse:
4394d8d9293SRyusuke Konishi 	*rootp = root;
440783f6184SRyusuke Konishi 	return 0;
441783f6184SRyusuke Konishi 
442783f6184SRyusuke Konishi  failed_bh:
443783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
444783f6184SRyusuke Konishi  failed:
4454d8d9293SRyusuke Konishi 	nilfs_put_root(root);
446783f6184SRyusuke Konishi 
447783f6184SRyusuke Konishi 	return err;
448783f6184SRyusuke Konishi }
449783f6184SRyusuke Konishi 
450783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
451783f6184SRyusuke Konishi {
452783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
453b7c06342SRyusuke Konishi 	struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root;
454b7c06342SRyusuke Konishi 	struct the_nilfs *nilfs = root->nilfs;
455c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
456783f6184SRyusuke Konishi 	unsigned long long blocks;
457783f6184SRyusuke Konishi 	unsigned long overhead;
458783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
459783f6184SRyusuke Konishi 	sector_t nfreeblocks;
460783f6184SRyusuke Konishi 	int err;
461783f6184SRyusuke Konishi 
462783f6184SRyusuke Konishi 	/*
463783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
464783f6184SRyusuke Konishi 	 *
465783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
466783f6184SRyusuke Konishi 	 * are excluded.
467783f6184SRyusuke Konishi 	 */
468783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
469783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
470783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
471783f6184SRyusuke Konishi 
472783f6184SRyusuke Konishi 	/*
473783f6184SRyusuke Konishi 	 * Compute the overhead
474783f6184SRyusuke Konishi 	 *
4757a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
476783f6184SRyusuke Konishi 	 * We must count them as the overhead.
477783f6184SRyusuke Konishi 	 */
478783f6184SRyusuke Konishi 	overhead = 0;
479783f6184SRyusuke Konishi 
480783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
481783f6184SRyusuke Konishi 	if (unlikely(err))
482783f6184SRyusuke Konishi 		return err;
483783f6184SRyusuke Konishi 
484783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
485783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
486783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
487783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
488783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
489783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
490b7c06342SRyusuke Konishi 	buf->f_files = atomic_read(&root->inodes_count);
491783f6184SRyusuke Konishi 	buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */
492783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
493c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
494c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
495c306af23SRyusuke Konishi 
496783f6184SRyusuke Konishi 	return 0;
497783f6184SRyusuke Konishi }
498783f6184SRyusuke Konishi 
499b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
500b58a285bSJiro SEKIBA {
501b58a285bSJiro SEKIBA 	struct super_block *sb = vfs->mnt_sb;
502b58a285bSJiro SEKIBA 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
503b58a285bSJiro SEKIBA 
504b58a285bSJiro SEKIBA 	if (!nilfs_test_opt(sbi, BARRIER))
505c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
506b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, SNAPSHOT))
507b58a285bSJiro SEKIBA 		seq_printf(seq, ",cp=%llu",
508b58a285bSJiro SEKIBA 			   (unsigned long long int)sbi->s_snapshot_cno);
509b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, ERRORS_PANIC))
510c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
511277a6a34SRyusuke Konishi 	if (nilfs_test_opt(sbi, ERRORS_CONT))
512c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
513b58a285bSJiro SEKIBA 	if (nilfs_test_opt(sbi, STRICT_ORDER))
514c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
5150234576dSRyusuke Konishi 	if (nilfs_test_opt(sbi, NORECOVERY))
516c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
517e902ec99SJiro SEKIBA 	if (nilfs_test_opt(sbi, DISCARD))
518c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
519b58a285bSJiro SEKIBA 
520b58a285bSJiro SEKIBA 	return 0;
521b58a285bSJiro SEKIBA }
522b58a285bSJiro SEKIBA 
523b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
524783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
525783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
526783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
527783f6184SRyusuke Konishi 	/* .write_inode    = nilfs_write_inode, */
528783f6184SRyusuke Konishi 	/* .put_inode      = nilfs_put_inode, */
529783f6184SRyusuke Konishi 	/* .drop_inode	  = nilfs_drop_inode, */
5306fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
531783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
5321dfa2710SJiro SEKIBA 	/* .write_super    = nilfs_write_super, */
533783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
534783f6184SRyusuke Konishi 	/* .write_super_lockfs */
535783f6184SRyusuke Konishi 	/* .unlockfs */
536783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
537783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
538783f6184SRyusuke Konishi 	/* .umount_begin */
539b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
540783f6184SRyusuke Konishi };
541783f6184SRyusuke Konishi 
542783f6184SRyusuke Konishi enum {
543783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
544773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
545802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, 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"},
552773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
55391f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
554783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
555783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
5560234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
557e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
558802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
559783f6184SRyusuke Konishi 	{Opt_err, NULL}
560783f6184SRyusuke Konishi };
561783f6184SRyusuke Konishi 
5627c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
563783f6184SRyusuke Konishi {
564783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
565783f6184SRyusuke Konishi 	char *p;
566783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
567783f6184SRyusuke Konishi 	int option;
568783f6184SRyusuke Konishi 
569783f6184SRyusuke Konishi 	if (!options)
570783f6184SRyusuke Konishi 		return 1;
571783f6184SRyusuke Konishi 
572783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
573783f6184SRyusuke Konishi 		int token;
574783f6184SRyusuke Konishi 		if (!*p)
575783f6184SRyusuke Konishi 			continue;
576783f6184SRyusuke Konishi 
577783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
578783f6184SRyusuke Konishi 		switch (token) {
579773bc4f3SRyusuke Konishi 		case Opt_barrier:
580773bc4f3SRyusuke Konishi 			nilfs_set_opt(sbi, BARRIER);
581773bc4f3SRyusuke Konishi 			break;
58291f1953bSJiro SEKIBA 		case Opt_nobarrier:
583783f6184SRyusuke Konishi 			nilfs_clear_opt(sbi, BARRIER);
584783f6184SRyusuke Konishi 			break;
585783f6184SRyusuke Konishi 		case Opt_order:
586783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
587783f6184SRyusuke Konishi 				/* Ordered data semantics */
588783f6184SRyusuke Konishi 				nilfs_clear_opt(sbi, STRICT_ORDER);
589783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
590783f6184SRyusuke Konishi 				/* Strict in-order semantics */
591783f6184SRyusuke Konishi 				nilfs_set_opt(sbi, STRICT_ORDER);
592783f6184SRyusuke Konishi 			else
593783f6184SRyusuke Konishi 				return 0;
594783f6184SRyusuke Konishi 			break;
595783f6184SRyusuke Konishi 		case Opt_err_panic:
596783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC);
597783f6184SRyusuke Konishi 			break;
598783f6184SRyusuke Konishi 		case Opt_err_ro:
599783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO);
600783f6184SRyusuke Konishi 			break;
601783f6184SRyusuke Konishi 		case Opt_err_cont:
602783f6184SRyusuke Konishi 			nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT);
603783f6184SRyusuke Konishi 			break;
604783f6184SRyusuke Konishi 		case Opt_snapshot:
605783f6184SRyusuke Konishi 			if (match_int(&args[0], &option) || option <= 0)
606783f6184SRyusuke Konishi 				return 0;
6077c017457SRyusuke Konishi 			if (is_remount) {
6087c017457SRyusuke Konishi 				if (!nilfs_test_opt(sbi, SNAPSHOT)) {
6097c017457SRyusuke Konishi 					printk(KERN_ERR
6107c017457SRyusuke Konishi 					       "NILFS: cannot change regular "
6117c017457SRyusuke Konishi 					       "mount to snapshot.\n");
612783f6184SRyusuke Konishi 					return 0;
6137c017457SRyusuke Konishi 				} else if (option != sbi->s_snapshot_cno) {
6147c017457SRyusuke Konishi 					printk(KERN_ERR
6157c017457SRyusuke Konishi 					       "NILFS: cannot remount to a "
6167c017457SRyusuke Konishi 					       "different snapshot.\n");
6177c017457SRyusuke Konishi 					return 0;
6187c017457SRyusuke Konishi 				}
6197c017457SRyusuke Konishi 				break;
6207c017457SRyusuke Konishi 			}
6217c017457SRyusuke Konishi 			if (!(sb->s_flags & MS_RDONLY)) {
6227c017457SRyusuke Konishi 				printk(KERN_ERR "NILFS: cannot mount snapshot "
6237c017457SRyusuke Konishi 				       "read/write.  A read-only option is "
6247c017457SRyusuke Konishi 				       "required.\n");
6257c017457SRyusuke Konishi 				return 0;
6267c017457SRyusuke Konishi 			}
627783f6184SRyusuke Konishi 			sbi->s_snapshot_cno = option;
628783f6184SRyusuke Konishi 			nilfs_set_opt(sbi, SNAPSHOT);
629783f6184SRyusuke Konishi 			break;
6300234576dSRyusuke Konishi 		case Opt_norecovery:
6310234576dSRyusuke Konishi 			nilfs_set_opt(sbi, NORECOVERY);
6320234576dSRyusuke Konishi 			break;
633e902ec99SJiro SEKIBA 		case Opt_discard:
634e902ec99SJiro SEKIBA 			nilfs_set_opt(sbi, DISCARD);
635e902ec99SJiro SEKIBA 			break;
636802d3177SRyusuke Konishi 		case Opt_nodiscard:
637802d3177SRyusuke Konishi 			nilfs_clear_opt(sbi, DISCARD);
638802d3177SRyusuke Konishi 			break;
639783f6184SRyusuke Konishi 		default:
640783f6184SRyusuke Konishi 			printk(KERN_ERR
641783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
642783f6184SRyusuke Konishi 			return 0;
643783f6184SRyusuke Konishi 		}
644783f6184SRyusuke Konishi 	}
645783f6184SRyusuke Konishi 	return 1;
646783f6184SRyusuke Konishi }
647783f6184SRyusuke Konishi 
648783f6184SRyusuke Konishi static inline void
649783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi,
650783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
651783f6184SRyusuke Konishi {
652783f6184SRyusuke Konishi 	sbi->s_mount_opt =
653277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
654783f6184SRyusuke Konishi }
655783f6184SRyusuke Konishi 
656783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi)
657783f6184SRyusuke Konishi {
658783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
659d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
660d26493b6SJiro SEKIBA 	int max_mnt_count;
661d26493b6SJiro SEKIBA 	int mnt_count;
662783f6184SRyusuke Konishi 
663d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
664b2ac86e1SJiro SEKIBA 	sbp = nilfs_prepare_super(sbi, 0);
665d26493b6SJiro SEKIBA 	if (!sbp)
666d26493b6SJiro SEKIBA 		return -EIO;
667d26493b6SJiro SEKIBA 
668d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
669d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
670d26493b6SJiro SEKIBA 
671f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
672783f6184SRyusuke Konishi 		printk(KERN_WARNING
673783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
674783f6184SRyusuke Konishi #if 0
675783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
676783f6184SRyusuke Konishi 		printk(KERN_WARNING
677783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
678783f6184SRyusuke Konishi #endif
679783f6184SRyusuke Konishi 	}
680783f6184SRyusuke Konishi 	if (!max_mnt_count)
681d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
682783f6184SRyusuke Konishi 
683d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
684d26493b6SJiro SEKIBA 	sbp[0]->s_state =
685d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
686d26493b6SJiro SEKIBA 	sbp[0]->s_mtime = cpu_to_le64(get_seconds());
687b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
688b2ac86e1SJiro SEKIBA 	memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
689b2ac86e1SJiro SEKIBA 	return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL);
690783f6184SRyusuke Konishi }
691783f6184SRyusuke Konishi 
692e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
693e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
694e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
695783f6184SRyusuke Konishi {
696e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
697e339ad31SRyusuke Konishi 	unsigned long offset;
698783f6184SRyusuke Konishi 
699e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
700783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
701e339ad31SRyusuke Konishi 	if (!*pbh)
702783f6184SRyusuke Konishi 		return NULL;
703783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
704783f6184SRyusuke Konishi }
705783f6184SRyusuke Konishi 
706783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
707783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
708783f6184SRyusuke Konishi 				 char *data)
709783f6184SRyusuke Konishi {
710783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
711783f6184SRyusuke Konishi 
712783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
713783f6184SRyusuke Konishi 
714783f6184SRyusuke Konishi 	/* FS independent flags */
715783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
716783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
717783f6184SRyusuke Konishi #endif
718783f6184SRyusuke Konishi 
719783f6184SRyusuke Konishi 	nilfs_set_default_options(sbi, sbp);
720783f6184SRyusuke Konishi 
721783f6184SRyusuke Konishi 	sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid);
722783f6184SRyusuke Konishi 	sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid);
723783f6184SRyusuke Konishi 	sbi->s_interval = le32_to_cpu(sbp->s_c_interval);
724783f6184SRyusuke Konishi 	sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max);
725783f6184SRyusuke Konishi 
7267c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
727783f6184SRyusuke Konishi }
728783f6184SRyusuke Konishi 
729c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
730c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
731c5ca48aaSRyusuke Konishi {
732c5ca48aaSRyusuke Konishi 	__u64 features;
733c5ca48aaSRyusuke Konishi 
734c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
735c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
736c5ca48aaSRyusuke Konishi 	if (features) {
737c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
738c5ca48aaSRyusuke Konishi 		       "optional features (%llx)\n",
739c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
740c5ca48aaSRyusuke Konishi 		return -EINVAL;
741c5ca48aaSRyusuke Konishi 	}
742c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
743c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
744c5ca48aaSRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY) && features) {
745c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
746c5ca48aaSRyusuke Konishi 		       "unsupported optional features (%llx)\n",
747c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
748c5ca48aaSRyusuke Konishi 		return -EINVAL;
749c5ca48aaSRyusuke Konishi 	}
750c5ca48aaSRyusuke Konishi 	return 0;
751c5ca48aaSRyusuke Konishi }
752c5ca48aaSRyusuke Konishi 
753367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
754367ea334SRyusuke Konishi 				 struct nilfs_root *root,
755367ea334SRyusuke Konishi 				 struct dentry **root_dentry)
756367ea334SRyusuke Konishi {
757367ea334SRyusuke Konishi 	struct inode *inode;
758367ea334SRyusuke Konishi 	struct dentry *dentry;
759367ea334SRyusuke Konishi 	int ret = 0;
760367ea334SRyusuke Konishi 
761367ea334SRyusuke Konishi 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
762367ea334SRyusuke Konishi 	if (IS_ERR(inode)) {
763367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
764367ea334SRyusuke Konishi 		ret = PTR_ERR(inode);
765367ea334SRyusuke Konishi 		goto out;
766367ea334SRyusuke Konishi 	}
767367ea334SRyusuke Konishi 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
768367ea334SRyusuke Konishi 		iput(inode);
769367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
770367ea334SRyusuke Konishi 		ret = -EINVAL;
771367ea334SRyusuke Konishi 		goto out;
772367ea334SRyusuke Konishi 	}
773367ea334SRyusuke Konishi 
774367ea334SRyusuke Konishi 	dentry = d_alloc_root(inode);
775367ea334SRyusuke Konishi 	if (!dentry) {
776367ea334SRyusuke Konishi 		iput(inode);
777367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root dentry failed\n");
778367ea334SRyusuke Konishi 		ret = -ENOMEM;
779367ea334SRyusuke Konishi 		goto out;
780367ea334SRyusuke Konishi 	}
781367ea334SRyusuke Konishi 	*root_dentry = dentry;
782367ea334SRyusuke Konishi  out:
783367ea334SRyusuke Konishi 	return ret;
784367ea334SRyusuke Konishi }
785367ea334SRyusuke Konishi 
786*ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
787*ab4d8f7eSRyusuke Konishi 				 struct dentry **root_dentry)
788*ab4d8f7eSRyusuke Konishi {
789*ab4d8f7eSRyusuke Konishi 	struct the_nilfs *nilfs = NILFS_SB(s)->s_nilfs;
790*ab4d8f7eSRyusuke Konishi 	struct nilfs_root *root;
791*ab4d8f7eSRyusuke Konishi 	int ret;
792*ab4d8f7eSRyusuke Konishi 
793*ab4d8f7eSRyusuke Konishi 	down_read(&nilfs->ns_segctor_sem);
794*ab4d8f7eSRyusuke Konishi 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
795*ab4d8f7eSRyusuke Konishi 	up_read(&nilfs->ns_segctor_sem);
796*ab4d8f7eSRyusuke Konishi 	if (ret < 0) {
797*ab4d8f7eSRyusuke Konishi 		ret = (ret == -ENOENT) ? -EINVAL : ret;
798*ab4d8f7eSRyusuke Konishi 		goto out;
799*ab4d8f7eSRyusuke Konishi 	} else if (!ret) {
800*ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: The specified checkpoint is "
801*ab4d8f7eSRyusuke Konishi 		       "not a snapshot (checkpoint number=%llu).\n",
802*ab4d8f7eSRyusuke Konishi 		       (unsigned long long)cno);
803*ab4d8f7eSRyusuke Konishi 		ret = -EINVAL;
804*ab4d8f7eSRyusuke Konishi 		goto out;
805*ab4d8f7eSRyusuke Konishi 	}
806*ab4d8f7eSRyusuke Konishi 
807*ab4d8f7eSRyusuke Konishi 	ret = nilfs_attach_checkpoint(NILFS_SB(s), cno, false, &root);
808*ab4d8f7eSRyusuke Konishi 	if (ret) {
809*ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading snapshot "
810*ab4d8f7eSRyusuke Konishi 		       "(checkpoint number=%llu).\n",
811*ab4d8f7eSRyusuke Konishi 	       (unsigned long long)cno);
812*ab4d8f7eSRyusuke Konishi 		goto out;
813*ab4d8f7eSRyusuke Konishi 	}
814*ab4d8f7eSRyusuke Konishi 	ret = nilfs_get_root_dentry(s, root, root_dentry);
815*ab4d8f7eSRyusuke Konishi 	nilfs_put_root(root);
816*ab4d8f7eSRyusuke Konishi  out:
817*ab4d8f7eSRyusuke Konishi 	return ret;
818*ab4d8f7eSRyusuke Konishi }
819*ab4d8f7eSRyusuke Konishi 
820783f6184SRyusuke Konishi /**
821783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
822783f6184SRyusuke Konishi  * @sb: super_block
823783f6184SRyusuke Konishi  * @data: mount options
824783f6184SRyusuke Konishi  * @silent: silent mode flag
825783f6184SRyusuke Konishi  * @nilfs: the_nilfs struct
826783f6184SRyusuke Konishi  *
827aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
828783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
829783f6184SRyusuke Konishi  */
830783f6184SRyusuke Konishi static int
831783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent,
832783f6184SRyusuke Konishi 		 struct the_nilfs *nilfs)
833783f6184SRyusuke Konishi {
834783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi;
8354d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
836783f6184SRyusuke Konishi 	__u64 cno;
837*ab4d8f7eSRyusuke Konishi 	int err;
838783f6184SRyusuke Konishi 
839783f6184SRyusuke Konishi 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
840783f6184SRyusuke Konishi 	if (!sbi)
841783f6184SRyusuke Konishi 		return -ENOMEM;
842783f6184SRyusuke Konishi 
843783f6184SRyusuke Konishi 	sb->s_fs_info = sbi;
844783f6184SRyusuke Konishi 
845783f6184SRyusuke Konishi 	get_nilfs(nilfs);
846783f6184SRyusuke Konishi 	sbi->s_nilfs = nilfs;
847783f6184SRyusuke Konishi 	sbi->s_super = sb;
8486dd47406SRyusuke Konishi 	atomic_set(&sbi->s_count, 1);
849783f6184SRyusuke Konishi 
850783f6184SRyusuke Konishi 	err = init_nilfs(nilfs, sbi, (char *)data);
851783f6184SRyusuke Konishi 	if (err)
852783f6184SRyusuke Konishi 		goto failed_sbi;
853783f6184SRyusuke Konishi 
854783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_inode_lock);
855783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_dirty_files);
856783f6184SRyusuke Konishi 	INIT_LIST_HEAD(&sbi->s_list);
857783f6184SRyusuke Konishi 
858783f6184SRyusuke Konishi 	/*
859783f6184SRyusuke Konishi 	 * Following initialization is overlapped because
860783f6184SRyusuke Konishi 	 * nilfs_sb_info structure has been cleared at the beginning.
861783f6184SRyusuke Konishi 	 * But we reserve them to keep our interest and make ready
862783f6184SRyusuke Konishi 	 * for the future change.
863783f6184SRyusuke Konishi 	 */
864783f6184SRyusuke Konishi 	get_random_bytes(&sbi->s_next_generation,
865783f6184SRyusuke Konishi 			 sizeof(sbi->s_next_generation));
866783f6184SRyusuke Konishi 	spin_lock_init(&sbi->s_next_gen_lock);
867783f6184SRyusuke Konishi 
868783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
869783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
870783f6184SRyusuke Konishi 	sb->s_root = NULL;
87161239230SRyusuke Konishi 	sb->s_time_gran = 1;
872973bec34SRyusuke Konishi 	sb->s_bdi = nilfs->ns_bdi;
873783f6184SRyusuke Konishi 
874783f6184SRyusuke Konishi 	err = load_nilfs(nilfs, sbi);
875783f6184SRyusuke Konishi 	if (err)
876783f6184SRyusuke Konishi 		goto failed_sbi;
877f50a4c81SRyusuke Konishi 
878783f6184SRyusuke Konishi 	if (nilfs_test_opt(sbi, SNAPSHOT)) {
879*ab4d8f7eSRyusuke Konishi 		err = nilfs_attach_snapshot(sb, sbi->s_snapshot_cno,
880*ab4d8f7eSRyusuke Konishi 					    &sb->s_root);
881*ab4d8f7eSRyusuke Konishi 		if (err)
8821f5abe7eSRyusuke Konishi 			goto failed_sbi;
883*ab4d8f7eSRyusuke Konishi 
884*ab4d8f7eSRyusuke Konishi 		goto add_to_supers;
885783f6184SRyusuke Konishi 	}
886783f6184SRyusuke Konishi 
887*ab4d8f7eSRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
888*ab4d8f7eSRyusuke Konishi 	err = nilfs_attach_checkpoint(sbi, cno, true, &fsroot);
889783f6184SRyusuke Konishi 	if (err) {
890783f6184SRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading a checkpoint"
891783f6184SRyusuke Konishi 		       " (checkpoint number=%llu).\n", (unsigned long long)cno);
892783f6184SRyusuke Konishi 		goto failed_sbi;
893783f6184SRyusuke Konishi 	}
894783f6184SRyusuke Konishi 
895783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
896e912a5b6SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi, fsroot);
897783f6184SRyusuke Konishi 		if (err)
898783f6184SRyusuke Konishi 			goto failed_checkpoint;
899783f6184SRyusuke Konishi 	}
900783f6184SRyusuke Konishi 
901367ea334SRyusuke Konishi 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
902367ea334SRyusuke Konishi 	if (err)
903783f6184SRyusuke Konishi 		goto failed_segctor;
904783f6184SRyusuke Konishi 
9054d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
9064d8d9293SRyusuke Konishi 
907783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
908783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
909783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
910783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
911783f6184SRyusuke Konishi 	}
912783f6184SRyusuke Konishi 
913*ab4d8f7eSRyusuke Konishi  add_to_supers:
914e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
915e912a5b6SRyusuke Konishi 	list_add(&sbi->s_list, &nilfs->ns_supers);
9163f82ff55SRyusuke Konishi 	if (!nilfs_test_opt(sbi, SNAPSHOT))
9173f82ff55SRyusuke Konishi 		nilfs->ns_current = sbi;
918e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
9193f82ff55SRyusuke Konishi 
920783f6184SRyusuke Konishi 	return 0;
921783f6184SRyusuke Konishi 
922783f6184SRyusuke Konishi  failed_segctor:
923783f6184SRyusuke Konishi 	nilfs_detach_segment_constructor(sbi);
924783f6184SRyusuke Konishi 
925783f6184SRyusuke Konishi  failed_checkpoint:
9264d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
927783f6184SRyusuke Konishi 
928783f6184SRyusuke Konishi  failed_sbi:
929783f6184SRyusuke Konishi 	put_nilfs(nilfs);
930783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
9316dd47406SRyusuke Konishi 	nilfs_put_sbinfo(sbi);
932783f6184SRyusuke Konishi 	return err;
933783f6184SRyusuke Konishi }
934783f6184SRyusuke Konishi 
935783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
936783f6184SRyusuke Konishi {
937783f6184SRyusuke Konishi 	struct nilfs_sb_info *sbi = NILFS_SB(sb);
938783f6184SRyusuke Konishi 	struct the_nilfs *nilfs = sbi->s_nilfs;
939783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
940783f6184SRyusuke Konishi 	struct nilfs_mount_options old_opts;
941d240e067SRyusuke Konishi 	int was_snapshot, err;
942783f6184SRyusuke Konishi 
943e59399d0SRyusuke Konishi 	down_write(&nilfs->ns_super_sem);
944783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
945783f6184SRyusuke Konishi 	old_opts.mount_opt = sbi->s_mount_opt;
946783f6184SRyusuke Konishi 	old_opts.snapshot_cno = sbi->s_snapshot_cno;
947d240e067SRyusuke Konishi 	was_snapshot = nilfs_test_opt(sbi, SNAPSHOT);
948783f6184SRyusuke Konishi 
9497c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
950783f6184SRyusuke Konishi 		err = -EINVAL;
951783f6184SRyusuke Konishi 		goto restore_opts;
952783f6184SRyusuke Konishi 	}
953783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
954783f6184SRyusuke Konishi 
955d240e067SRyusuke Konishi 	err = -EINVAL;
9567c017457SRyusuke Konishi 	if (was_snapshot && !(*flags & MS_RDONLY)) {
9577c017457SRyusuke Konishi 		printk(KERN_ERR "NILFS (device %s): cannot remount snapshot "
9587c017457SRyusuke Konishi 		       "read/write.\n", sb->s_id);
959d240e067SRyusuke Konishi 		goto restore_opts;
960d240e067SRyusuke Konishi 	}
961783f6184SRyusuke Konishi 
9620234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
9630234576dSRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
9640234576dSRyusuke Konishi 		       "remount because the filesystem is in an "
9650234576dSRyusuke Konishi 		       "incomplete recovery state.\n", sb->s_id);
9660234576dSRyusuke Konishi 		goto restore_opts;
9670234576dSRyusuke Konishi 	}
9680234576dSRyusuke Konishi 
969783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
970783f6184SRyusuke Konishi 		goto out;
971783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
972783f6184SRyusuke Konishi 		/* Shutting down the segment constructor */
973783f6184SRyusuke Konishi 		nilfs_detach_segment_constructor(sbi);
974783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
975783f6184SRyusuke Konishi 
976783f6184SRyusuke Konishi 		/*
977783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
978783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
979783f6184SRyusuke Konishi 		 */
980783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
9817ecaa46cSRyusuke Konishi 		nilfs_cleanup_super(sbi);
982783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
983783f6184SRyusuke Konishi 	} else {
984c5ca48aaSRyusuke Konishi 		__u64 features;
985e912a5b6SRyusuke Konishi 		struct nilfs_root *root;
986c5ca48aaSRyusuke Konishi 
987783f6184SRyusuke Konishi 		/*
988783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
989783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
990783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
991783f6184SRyusuke Konishi 		 */
992c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
993c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
994c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
995c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
996c5ca48aaSRyusuke Konishi 		if (features) {
997c5ca48aaSRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
998c5ca48aaSRyusuke Konishi 			       "remount RDWR because of unsupported optional "
999c5ca48aaSRyusuke Konishi 			       "features (%llx)\n",
1000c5ca48aaSRyusuke Konishi 			       sb->s_id, (unsigned long long)features);
1001c5ca48aaSRyusuke Konishi 			err = -EROFS;
1002c5ca48aaSRyusuke Konishi 			goto restore_opts;
1003c5ca48aaSRyusuke Konishi 		}
1004c5ca48aaSRyusuke Konishi 
1005783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
1006783f6184SRyusuke Konishi 
1007e912a5b6SRyusuke Konishi 		root = NILFS_I(sb->s_root->d_inode)->i_root;
1008e912a5b6SRyusuke Konishi 		err = nilfs_attach_segment_constructor(sbi, root);
1009783f6184SRyusuke Konishi 		if (err)
1010e59399d0SRyusuke Konishi 			goto restore_opts;
1011783f6184SRyusuke Konishi 
1012783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1013783f6184SRyusuke Konishi 		nilfs_setup_super(sbi);
1014783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1015783f6184SRyusuke Konishi 	}
1016783f6184SRyusuke Konishi  out:
1017e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
1018783f6184SRyusuke Konishi 	return 0;
1019783f6184SRyusuke Konishi 
1020783f6184SRyusuke Konishi  restore_opts:
1021783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
1022783f6184SRyusuke Konishi 	sbi->s_mount_opt = old_opts.mount_opt;
1023783f6184SRyusuke Konishi 	sbi->s_snapshot_cno = old_opts.snapshot_cno;
1024e59399d0SRyusuke Konishi 	up_write(&nilfs->ns_super_sem);
1025783f6184SRyusuke Konishi 	return err;
1026783f6184SRyusuke Konishi }
1027783f6184SRyusuke Konishi 
1028783f6184SRyusuke Konishi struct nilfs_super_data {
1029783f6184SRyusuke Konishi 	struct block_device *bdev;
10306dd47406SRyusuke Konishi 	struct nilfs_sb_info *sbi;
1031783f6184SRyusuke Konishi 	__u64 cno;
1032783f6184SRyusuke Konishi 	int flags;
1033783f6184SRyusuke Konishi };
1034783f6184SRyusuke Konishi 
1035783f6184SRyusuke Konishi /**
1036783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1037783f6184SRyusuke Konishi  * @data: mount options
1038783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1039783f6184SRyusuke Konishi  */
1040783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1041783f6184SRyusuke Konishi {
1042783f6184SRyusuke Konishi 	char *p, *options = data;
1043783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1044783f6184SRyusuke Konishi 	int option, token;
1045783f6184SRyusuke Konishi 	int ret = 0;
1046783f6184SRyusuke Konishi 
1047783f6184SRyusuke Konishi 	do {
1048783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1049783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1050783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1051783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
1052783f6184SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY))
1053783f6184SRyusuke Konishi 					ret++;
1054783f6184SRyusuke Konishi 				else {
1055783f6184SRyusuke Konishi 					ret = match_int(&args[0], &option);
1056783f6184SRyusuke Konishi 					if (!ret) {
1057783f6184SRyusuke Konishi 						if (option > 0)
1058783f6184SRyusuke Konishi 							sd->cno = option;
1059783f6184SRyusuke Konishi 						else
1060783f6184SRyusuke Konishi 							ret++;
1061783f6184SRyusuke Konishi 					}
1062783f6184SRyusuke Konishi 				}
1063783f6184SRyusuke Konishi 			}
1064783f6184SRyusuke Konishi 			if (ret)
1065783f6184SRyusuke Konishi 				printk(KERN_ERR
1066783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
1067783f6184SRyusuke Konishi 		}
1068783f6184SRyusuke Konishi 		if (!options)
1069783f6184SRyusuke Konishi 			break;
1070783f6184SRyusuke Konishi 		BUG_ON(options == data);
1071783f6184SRyusuke Konishi 		*(options - 1) = ',';
1072783f6184SRyusuke Konishi 	} while (!ret);
1073783f6184SRyusuke Konishi 	return ret;
1074783f6184SRyusuke Konishi }
1075783f6184SRyusuke Konishi 
1076783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1077783f6184SRyusuke Konishi {
1078783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1079783f6184SRyusuke Konishi 
1080783f6184SRyusuke Konishi 	s->s_bdev = sd->bdev;
1081783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1082783f6184SRyusuke Konishi 	return 0;
1083783f6184SRyusuke Konishi }
1084783f6184SRyusuke Konishi 
1085783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1086783f6184SRyusuke Konishi {
1087783f6184SRyusuke Konishi 	struct nilfs_super_data *sd = data;
1088783f6184SRyusuke Konishi 
10896dd47406SRyusuke Konishi 	return sd->sbi && s->s_fs_info == (void *)sd->sbi;
1090783f6184SRyusuke Konishi }
1091783f6184SRyusuke Konishi 
1092783f6184SRyusuke Konishi static int
1093783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags,
1094783f6184SRyusuke Konishi 	     const char *dev_name, void *data, struct vfsmount *mnt)
1095783f6184SRyusuke Konishi {
1096783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
109733c8e57cSRyusuke Konishi 	struct super_block *s;
109813e90559SRyusuke Konishi 	fmode_t mode = FMODE_READ;
109933c8e57cSRyusuke Konishi 	struct the_nilfs *nilfs;
1100783f6184SRyusuke Konishi 	int err, need_to_close = 1;
1101783f6184SRyusuke Konishi 
110213e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
110313e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
110413e90559SRyusuke Konishi 
110513e90559SRyusuke Konishi 	sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type);
1106d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1107783f6184SRyusuke Konishi 		return PTR_ERR(sd.bdev);
1108783f6184SRyusuke Konishi 
1109783f6184SRyusuke Konishi 	/*
1110783f6184SRyusuke Konishi 	 * To get mount instance using sget() vfs-routine, NILFS needs
1111783f6184SRyusuke Konishi 	 * much more information than normal filesystems to identify mount
1112783f6184SRyusuke Konishi 	 * instance.  For snapshot mounts, not only a mount type (ro-mount
1113783f6184SRyusuke Konishi 	 * or rw-mount) but also a checkpoint number is required.
1114783f6184SRyusuke Konishi 	 */
1115783f6184SRyusuke Konishi 	sd.cno = 0;
1116783f6184SRyusuke Konishi 	sd.flags = flags;
1117783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1118783f6184SRyusuke Konishi 		err = -EINVAL;
1119783f6184SRyusuke Konishi 		goto failed;
1120783f6184SRyusuke Konishi 	}
1121783f6184SRyusuke Konishi 
112233c8e57cSRyusuke Konishi 	nilfs = find_or_create_nilfs(sd.bdev);
112333c8e57cSRyusuke Konishi 	if (!nilfs) {
112433c8e57cSRyusuke Konishi 		err = -ENOMEM;
112533c8e57cSRyusuke Konishi 		goto failed;
112633c8e57cSRyusuke Konishi 	}
112733c8e57cSRyusuke Konishi 
1128aa7dfb89SRyusuke Konishi 	mutex_lock(&nilfs->ns_mount_mutex);
11293f82ff55SRyusuke Konishi 
11303f82ff55SRyusuke Konishi 	if (!sd.cno) {
11313f82ff55SRyusuke Konishi 		/*
11323f82ff55SRyusuke Konishi 		 * Check if an exclusive mount exists or not.
11333f82ff55SRyusuke Konishi 		 * Snapshot mounts coexist with a current mount
11343f82ff55SRyusuke Konishi 		 * (i.e. rw-mount or ro-mount), whereas rw-mount and
11353f82ff55SRyusuke Konishi 		 * ro-mount are mutually exclusive.
11363f82ff55SRyusuke Konishi 		 */
1137e59399d0SRyusuke Konishi 		down_read(&nilfs->ns_super_sem);
11383f82ff55SRyusuke Konishi 		if (nilfs->ns_current &&
11393f82ff55SRyusuke Konishi 		    ((nilfs->ns_current->s_super->s_flags ^ flags)
11403f82ff55SRyusuke Konishi 		     & MS_RDONLY)) {
1141e59399d0SRyusuke Konishi 			up_read(&nilfs->ns_super_sem);
11423f82ff55SRyusuke Konishi 			err = -EBUSY;
1143783f6184SRyusuke Konishi 			goto failed_unlock;
1144783f6184SRyusuke Konishi 		}
1145e59399d0SRyusuke Konishi 		up_read(&nilfs->ns_super_sem);
11463f82ff55SRyusuke Konishi 	}
1147783f6184SRyusuke Konishi 
1148783f6184SRyusuke Konishi 	/*
11496dd47406SRyusuke Konishi 	 * Find existing nilfs_sb_info struct
1150783f6184SRyusuke Konishi 	 */
11516dd47406SRyusuke Konishi 	sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
11526dd47406SRyusuke Konishi 
11536dd47406SRyusuke Konishi 	/*
11546dd47406SRyusuke Konishi 	 * Get super block instance holding the nilfs_sb_info struct.
11556dd47406SRyusuke Konishi 	 * A new instance is allocated if no existing mount is present or
11566dd47406SRyusuke Konishi 	 * existing instance has been unmounted.
11576dd47406SRyusuke Konishi 	 */
115833c8e57cSRyusuke Konishi 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
11596dd47406SRyusuke Konishi 	if (sd.sbi)
11606dd47406SRyusuke Konishi 		nilfs_put_sbinfo(sd.sbi);
11616dd47406SRyusuke Konishi 
116233c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
116333c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
116433c8e57cSRyusuke Konishi 		goto failed_unlock;
1165783f6184SRyusuke Konishi 	}
1166783f6184SRyusuke Konishi 
1167783f6184SRyusuke Konishi 	if (!s->s_root) {
1168783f6184SRyusuke Konishi 		char b[BDEVNAME_SIZE];
1169783f6184SRyusuke Konishi 
117033c8e57cSRyusuke Konishi 		/* New superblock instance created */
1171783f6184SRyusuke Konishi 		s->s_flags = flags;
11724571b82cSRyusuke Konishi 		s->s_mode = mode;
1173783f6184SRyusuke Konishi 		strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
1174783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1175783f6184SRyusuke Konishi 
1176e2d1591aSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0,
1177e2d1591aSRyusuke Konishi 				       nilfs);
1178783f6184SRyusuke Konishi 		if (err)
1179783f6184SRyusuke Konishi 			goto cancel_new;
1180783f6184SRyusuke Konishi 
1181783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1182783f6184SRyusuke Konishi 		need_to_close = 0;
1183783f6184SRyusuke Konishi 	}
1184783f6184SRyusuke Konishi 
1185aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1186783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1187783f6184SRyusuke Konishi 	if (need_to_close)
118813e90559SRyusuke Konishi 		close_bdev_exclusive(sd.bdev, mode);
1189783f6184SRyusuke Konishi 	simple_set_mnt(mnt, s);
1190783f6184SRyusuke Konishi 	return 0;
1191783f6184SRyusuke Konishi 
1192783f6184SRyusuke Konishi  failed_unlock:
1193aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
119433c8e57cSRyusuke Konishi 	put_nilfs(nilfs);
1195783f6184SRyusuke Konishi  failed:
119613e90559SRyusuke Konishi 	close_bdev_exclusive(sd.bdev, mode);
1197783f6184SRyusuke Konishi 	return err;
1198783f6184SRyusuke Konishi 
1199783f6184SRyusuke Konishi  cancel_new:
1200783f6184SRyusuke Konishi 	/* Abandoning the newly allocated superblock */
1201aa7dfb89SRyusuke Konishi 	mutex_unlock(&nilfs->ns_mount_mutex);
1202783f6184SRyusuke Konishi 	put_nilfs(nilfs);
1203a95161aaSAl Viro 	deactivate_locked_super(s);
1204783f6184SRyusuke Konishi 	/*
1205b87ca919SRyusuke Konishi 	 * deactivate_locked_super() invokes close_bdev_exclusive().
1206783f6184SRyusuke Konishi 	 * We must finish all post-cleaning before this call;
1207aa7dfb89SRyusuke Konishi 	 * put_nilfs() needs the block device.
1208783f6184SRyusuke Konishi 	 */
1209783f6184SRyusuke Konishi 	return err;
1210783f6184SRyusuke Konishi }
1211783f6184SRyusuke Konishi 
1212783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1213783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1214783f6184SRyusuke Konishi 	.name     = "nilfs2",
1215783f6184SRyusuke Konishi 	.get_sb   = nilfs_get_sb,
1216783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1217783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1218783f6184SRyusuke Konishi };
1219783f6184SRyusuke Konishi 
122041c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
122141c88bd7SLi Hong {
122241c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
122341c88bd7SLi Hong 
122441c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
122541c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
122641c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
122741c88bd7SLi Hong #endif
122841c88bd7SLi Hong 	nilfs_btnode_cache_init_once(&ii->i_btnode_cache);
122905d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
123041c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
123141c88bd7SLi Hong }
123241c88bd7SLi Hong 
123341c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
123441c88bd7SLi Hong {
123541c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
123641c88bd7SLi Hong }
123741c88bd7SLi Hong 
123841c88bd7SLi Hong static void nilfs_destroy_cachep(void)
123941c88bd7SLi Hong {
124041c88bd7SLi Hong 	if (nilfs_inode_cachep)
124141c88bd7SLi Hong 		kmem_cache_destroy(nilfs_inode_cachep);
124241c88bd7SLi Hong 	if (nilfs_transaction_cachep)
124341c88bd7SLi Hong 		kmem_cache_destroy(nilfs_transaction_cachep);
124441c88bd7SLi Hong 	if (nilfs_segbuf_cachep)
124541c88bd7SLi Hong 		kmem_cache_destroy(nilfs_segbuf_cachep);
124641c88bd7SLi Hong 	if (nilfs_btree_path_cache)
124741c88bd7SLi Hong 		kmem_cache_destroy(nilfs_btree_path_cache);
124841c88bd7SLi Hong }
124941c88bd7SLi Hong 
125041c88bd7SLi Hong static int __init nilfs_init_cachep(void)
125141c88bd7SLi Hong {
125241c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
125341c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
125441c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
125541c88bd7SLi Hong 	if (!nilfs_inode_cachep)
125641c88bd7SLi Hong 		goto fail;
125741c88bd7SLi Hong 
125841c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
125941c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
126041c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
126141c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
126241c88bd7SLi Hong 		goto fail;
126341c88bd7SLi Hong 
126441c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
126541c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
126641c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
126741c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
126841c88bd7SLi Hong 		goto fail;
126941c88bd7SLi Hong 
127041c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
127141c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
127241c88bd7SLi Hong 			0, 0, NULL);
127341c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
127441c88bd7SLi Hong 		goto fail;
127541c88bd7SLi Hong 
127641c88bd7SLi Hong 	return 0;
127741c88bd7SLi Hong 
127841c88bd7SLi Hong fail:
127941c88bd7SLi Hong 	nilfs_destroy_cachep();
128041c88bd7SLi Hong 	return -ENOMEM;
128141c88bd7SLi Hong }
128241c88bd7SLi Hong 
1283783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1284783f6184SRyusuke Konishi {
1285783f6184SRyusuke Konishi 	int err;
1286783f6184SRyusuke Konishi 
128741c88bd7SLi Hong 	err = nilfs_init_cachep();
1288783f6184SRyusuke Konishi 	if (err)
128941c88bd7SLi Hong 		goto fail;
1290783f6184SRyusuke Konishi 
1291783f6184SRyusuke Konishi 	err = register_filesystem(&nilfs_fs_type);
1292783f6184SRyusuke Konishi 	if (err)
129341c88bd7SLi Hong 		goto free_cachep;
1294783f6184SRyusuke Konishi 
12959f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1296783f6184SRyusuke Konishi 	return 0;
1297783f6184SRyusuke Konishi 
129841c88bd7SLi Hong free_cachep:
129941c88bd7SLi Hong 	nilfs_destroy_cachep();
130041c88bd7SLi Hong fail:
1301783f6184SRyusuke Konishi 	return err;
1302783f6184SRyusuke Konishi }
1303783f6184SRyusuke Konishi 
1304783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1305783f6184SRyusuke Konishi {
130641c88bd7SLi Hong 	nilfs_destroy_cachep();
1307783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1308783f6184SRyusuke Konishi }
1309783f6184SRyusuke Konishi 
1310783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1311783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1312