xref: /openbmc/linux/fs/nilfs2/super.c (revision 3377f843cf80e33d63f1a3ded67fd129a298c6b0)
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/crc32.h>
47783f6184SRyusuke Konishi #include <linux/vfs.h>
48783f6184SRyusuke Konishi #include <linux/writeback.h>
49b58a285bSJiro SEKIBA #include <linux/seq_file.h>
50b58a285bSJiro SEKIBA #include <linux/mount.h>
51783f6184SRyusuke Konishi #include "nilfs.h"
528e656fd5SRyusuke Konishi #include "export.h"
53783f6184SRyusuke Konishi #include "mdt.h"
54783f6184SRyusuke Konishi #include "alloc.h"
5505d0e94bSRyusuke Konishi #include "btree.h"
5605d0e94bSRyusuke Konishi #include "btnode.h"
57783f6184SRyusuke Konishi #include "page.h"
58783f6184SRyusuke Konishi #include "cpfile.h"
594e33f9eaSRyusuke Konishi #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
60783f6184SRyusuke Konishi #include "ifile.h"
61783f6184SRyusuke Konishi #include "dat.h"
62783f6184SRyusuke Konishi #include "segment.h"
63783f6184SRyusuke Konishi #include "segbuf.h"
64783f6184SRyusuke Konishi 
65783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
66783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
67783f6184SRyusuke Konishi 		   "(NILFS)");
68783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
69783f6184SRyusuke Konishi 
70abc0b50bSJiro SEKIBA static struct kmem_cache *nilfs_inode_cachep;
7141c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
7241c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
7341c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
7441c88bd7SLi Hong 
75f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount);
76783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
77783f6184SRyusuke Konishi 
78f7545144SRyusuke Konishi static void nilfs_set_error(struct super_block *sb)
79c8a11c8aSRyusuke Konishi {
80e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
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;
86f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, 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);
91f7545144SRyusuke Konishi 			nilfs_commit_super(sb, 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 {
112e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
113b004a5ebSJoe Perches 	struct va_format vaf;
114783f6184SRyusuke Konishi 	va_list args;
115783f6184SRyusuke Konishi 
116783f6184SRyusuke Konishi 	va_start(args, fmt);
117b004a5ebSJoe Perches 
118b004a5ebSJoe Perches 	vaf.fmt = fmt;
119b004a5ebSJoe Perches 	vaf.va = &args;
120b004a5ebSJoe Perches 
121b004a5ebSJoe Perches 	printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
122b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
123b004a5ebSJoe Perches 
124783f6184SRyusuke Konishi 	va_end(args);
125783f6184SRyusuke Konishi 
126783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
127f7545144SRyusuke Konishi 		nilfs_set_error(sb);
128783f6184SRyusuke Konishi 
1293b2ce58bSRyusuke Konishi 		if (nilfs_test_opt(nilfs, ERRORS_RO)) {
130783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
131783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
132783f6184SRyusuke Konishi 		}
133783f6184SRyusuke Konishi 	}
134783f6184SRyusuke Konishi 
1353b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
136783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
137783f6184SRyusuke Konishi 		      sb->s_id);
138783f6184SRyusuke Konishi }
139783f6184SRyusuke Konishi 
140783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function,
141783f6184SRyusuke Konishi 		   const char *fmt, ...)
142783f6184SRyusuke Konishi {
143b004a5ebSJoe Perches 	struct va_format vaf;
144783f6184SRyusuke Konishi 	va_list args;
145783f6184SRyusuke Konishi 
146783f6184SRyusuke Konishi 	va_start(args, fmt);
147b004a5ebSJoe Perches 
148b004a5ebSJoe Perches 	vaf.fmt = fmt;
149b004a5ebSJoe Perches 	vaf.va = &args;
150b004a5ebSJoe Perches 
151b004a5ebSJoe Perches 	printk(KERN_WARNING "NILFS warning (device %s): %s: %pV\n",
152b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
153b004a5ebSJoe Perches 
154783f6184SRyusuke Konishi 	va_end(args);
155783f6184SRyusuke Konishi }
156783f6184SRyusuke Konishi 
157783f6184SRyusuke Konishi 
1582879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
159783f6184SRyusuke Konishi {
160783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
161783f6184SRyusuke Konishi 
162783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
163783f6184SRyusuke Konishi 	if (!ii)
164783f6184SRyusuke Konishi 		return NULL;
165783f6184SRyusuke Konishi 	ii->i_bh = NULL;
166783f6184SRyusuke Konishi 	ii->i_state = 0;
1670e14a359SRyusuke Konishi 	ii->i_cno = 0;
168783f6184SRyusuke Konishi 	ii->vfs_inode.i_version = 1;
169b83ae6d4SChristoph Hellwig 	nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
170783f6184SRyusuke Konishi 	return &ii->vfs_inode;
171783f6184SRyusuke Konishi }
172783f6184SRyusuke Konishi 
173fa0d7e3dSNick Piggin static void nilfs_i_callback(struct rcu_head *head)
174783f6184SRyusuke Konishi {
175fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
176b91c9a97SRyusuke Konishi 	struct nilfs_mdt_info *mdi = NILFS_MDT(inode);
177b91c9a97SRyusuke Konishi 
178b91c9a97SRyusuke Konishi 	if (mdi) {
179b91c9a97SRyusuke Konishi 		kfree(mdi->mi_bgl); /* kfree(NULL) is safe */
180b91c9a97SRyusuke Konishi 		kfree(mdi);
181b91c9a97SRyusuke Konishi 	}
182783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
183783f6184SRyusuke Konishi }
184783f6184SRyusuke Konishi 
185fa0d7e3dSNick Piggin void nilfs_destroy_inode(struct inode *inode)
186fa0d7e3dSNick Piggin {
187fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, nilfs_i_callback);
188fa0d7e3dSNick Piggin }
189fa0d7e3dSNick Piggin 
190f7545144SRyusuke Konishi static int nilfs_sync_super(struct super_block *sb, int flag)
191783f6184SRyusuke Konishi {
192e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
193783f6184SRyusuke Konishi 	int err;
194783f6184SRyusuke Konishi 
195783f6184SRyusuke Konishi  retry:
196e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
1973b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, BARRIER)) {
19887e99511SChristoph Hellwig 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
199f8c131f5SChristoph Hellwig 					  WRITE_SYNC | WRITE_FLUSH_FUA);
20087e99511SChristoph Hellwig 	} else {
20187e99511SChristoph Hellwig 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
20287e99511SChristoph Hellwig 	}
20387e99511SChristoph Hellwig 
204e339ad31SRyusuke Konishi 	if (unlikely(err)) {
205783f6184SRyusuke Konishi 		printk(KERN_ERR
206783f6184SRyusuke Konishi 		       "NILFS: unable to write superblock (err=%d)\n", err);
207e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
208b2ac86e1SJiro SEKIBA 			/*
209b2ac86e1SJiro SEKIBA 			 * sbp[0] points to newer log than sbp[1],
210b2ac86e1SJiro SEKIBA 			 * so copy sbp[0] to sbp[1] to take over sbp[0].
211b2ac86e1SJiro SEKIBA 			 */
212b2ac86e1SJiro SEKIBA 			memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
213b2ac86e1SJiro SEKIBA 			       nilfs->ns_sbsize);
214e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
215e339ad31SRyusuke Konishi 			goto retry;
216e339ad31SRyusuke Konishi 		}
217e339ad31SRyusuke Konishi 	} else {
218e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
219e339ad31SRyusuke Konishi 
220b2ac86e1SJiro SEKIBA 		nilfs->ns_sbwcount++;
221b2ac86e1SJiro SEKIBA 
222e339ad31SRyusuke Konishi 		/*
223e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
224e339ad31SRyusuke Konishi 		 * written in superblock.
225e339ad31SRyusuke Konishi 		 */
226783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
227e339ad31SRyusuke Konishi 
228e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
229e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
230b2ac86e1SJiro SEKIBA 			if (flag == NILFS_SB_COMMIT_ALL) {
231e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
232b2ac86e1SJiro SEKIBA 				if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
233b2ac86e1SJiro SEKIBA 					goto out;
234b2ac86e1SJiro SEKIBA 			}
235b2ac86e1SJiro SEKIBA 			if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
236b2ac86e1SJiro SEKIBA 			    le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
237e339ad31SRyusuke Konishi 				sbp = nilfs->ns_sbp[1];
238e339ad31SRyusuke Konishi 		}
239b2ac86e1SJiro SEKIBA 
2402c2e52fcSRyusuke Konishi 		spin_lock(&nilfs->ns_last_segment_lock);
241e339ad31SRyusuke Konishi 		nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2422c2e52fcSRyusuke Konishi 		spin_unlock(&nilfs->ns_last_segment_lock);
243783f6184SRyusuke Konishi 	}
244b2ac86e1SJiro SEKIBA  out:
245783f6184SRyusuke Konishi 	return err;
246783f6184SRyusuke Konishi }
247783f6184SRyusuke Konishi 
24860f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
24960f46b7eSRyusuke Konishi 			  struct the_nilfs *nilfs)
25060f46b7eSRyusuke Konishi {
25160f46b7eSRyusuke Konishi 	sector_t nfreeblocks;
25260f46b7eSRyusuke Konishi 
25360f46b7eSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
25460f46b7eSRyusuke Konishi 	nilfs_count_free_blocks(nilfs, &nfreeblocks);
25560f46b7eSRyusuke Konishi 	sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
25660f46b7eSRyusuke Konishi 
25760f46b7eSRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
25860f46b7eSRyusuke Konishi 	sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
25960f46b7eSRyusuke Konishi 	sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
26060f46b7eSRyusuke Konishi 	sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
26160f46b7eSRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
26260f46b7eSRyusuke Konishi }
26360f46b7eSRyusuke Konishi 
264f7545144SRyusuke Konishi struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
265b2ac86e1SJiro SEKIBA 					       int flip)
266d26493b6SJiro SEKIBA {
267e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
268d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
269d26493b6SJiro SEKIBA 
270d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
271d26493b6SJiro SEKIBA 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
272d26493b6SJiro SEKIBA 		if (sbp[1] &&
273d26493b6SJiro SEKIBA 		    sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
274b2ac86e1SJiro SEKIBA 			memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
275d26493b6SJiro SEKIBA 		} else {
276d26493b6SJiro SEKIBA 			printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
277f7545144SRyusuke Konishi 			       sb->s_id);
278d26493b6SJiro SEKIBA 			return NULL;
279d26493b6SJiro SEKIBA 		}
280b2ac86e1SJiro SEKIBA 	} else if (sbp[1] &&
281b2ac86e1SJiro SEKIBA 		   sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
282b2ac86e1SJiro SEKIBA 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
283d26493b6SJiro SEKIBA 	}
284b2ac86e1SJiro SEKIBA 
285b2ac86e1SJiro SEKIBA 	if (flip && sbp[1])
286b2ac86e1SJiro SEKIBA 		nilfs_swap_super_block(nilfs);
287b2ac86e1SJiro SEKIBA 
288d26493b6SJiro SEKIBA 	return sbp;
289d26493b6SJiro SEKIBA }
290d26493b6SJiro SEKIBA 
291f7545144SRyusuke Konishi int nilfs_commit_super(struct super_block *sb, int flag)
292783f6184SRyusuke Konishi {
293e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
294e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
295e339ad31SRyusuke Konishi 	time_t t;
296783f6184SRyusuke Konishi 
297d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
298e339ad31SRyusuke Konishi 	t = get_seconds();
299b2ac86e1SJiro SEKIBA 	nilfs->ns_sbwtime = t;
300e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
301e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
302e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
303e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
304e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
305b2ac86e1SJiro SEKIBA 	if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
306b2ac86e1SJiro SEKIBA 		sbp[1]->s_wtime = sbp[0]->s_wtime;
307b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = 0;
308b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
309b2ac86e1SJiro SEKIBA 					    (unsigned char *)sbp[1],
310b2ac86e1SJiro SEKIBA 					    nilfs->ns_sbsize));
311e339ad31SRyusuke Konishi 	}
312e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
313e2c7617aSAndreas Rohner 	nilfs->ns_flushed_device = 1;
314e2c7617aSAndreas Rohner 	/* make sure store to ns_flushed_device cannot be reordered */
315e2c7617aSAndreas Rohner 	smp_wmb();
316f7545144SRyusuke Konishi 	return nilfs_sync_super(sb, flag);
317783f6184SRyusuke Konishi }
318783f6184SRyusuke Konishi 
3197ecaa46cSRyusuke Konishi /**
3207ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
321f7545144SRyusuke Konishi  * @sb: super block instance to be unmounted or degraded to read-only
3227ecaa46cSRyusuke Konishi  *
3237ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
3247ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3257ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
3267ecaa46cSRyusuke Konishi  */
327f7545144SRyusuke Konishi int nilfs_cleanup_super(struct super_block *sb)
3287ecaa46cSRyusuke Konishi {
329e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
330d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
331b2ac86e1SJiro SEKIBA 	int flag = NILFS_SB_COMMIT;
332d26493b6SJiro SEKIBA 	int ret = -EIO;
3337ecaa46cSRyusuke Konishi 
334f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
335d26493b6SJiro SEKIBA 	if (sbp) {
336f7545144SRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
337f7545144SRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
338b2ac86e1SJiro SEKIBA 		if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
339b2ac86e1SJiro SEKIBA 			/*
340b2ac86e1SJiro SEKIBA 			 * make the "clean" flag also to the opposite
341b2ac86e1SJiro SEKIBA 			 * super block if both super blocks point to
342b2ac86e1SJiro SEKIBA 			 * the same checkpoint.
343b2ac86e1SJiro SEKIBA 			 */
344b2ac86e1SJiro SEKIBA 			sbp[1]->s_state = sbp[0]->s_state;
345b2ac86e1SJiro SEKIBA 			flag = NILFS_SB_COMMIT_ALL;
346b2ac86e1SJiro SEKIBA 		}
347f7545144SRyusuke Konishi 		ret = nilfs_commit_super(sb, flag);
348d26493b6SJiro SEKIBA 	}
3497ecaa46cSRyusuke Konishi 	return ret;
3507ecaa46cSRyusuke Konishi }
3517ecaa46cSRyusuke Konishi 
352cfb0a4bfSRyusuke Konishi /**
353cfb0a4bfSRyusuke Konishi  * nilfs_move_2nd_super - relocate secondary super block
354cfb0a4bfSRyusuke Konishi  * @sb: super block instance
355cfb0a4bfSRyusuke Konishi  * @sb2off: new offset of the secondary super block (in bytes)
356cfb0a4bfSRyusuke Konishi  */
357cfb0a4bfSRyusuke Konishi static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
358cfb0a4bfSRyusuke Konishi {
359cfb0a4bfSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
360cfb0a4bfSRyusuke Konishi 	struct buffer_head *nsbh;
361cfb0a4bfSRyusuke Konishi 	struct nilfs_super_block *nsbp;
362cfb0a4bfSRyusuke Konishi 	sector_t blocknr, newblocknr;
363cfb0a4bfSRyusuke Konishi 	unsigned long offset;
364cfb0a4bfSRyusuke Konishi 	int sb2i = -1;  /* array index of the secondary superblock */
365cfb0a4bfSRyusuke Konishi 	int ret = 0;
366cfb0a4bfSRyusuke Konishi 
367cfb0a4bfSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
368cfb0a4bfSRyusuke Konishi 	if (nilfs->ns_sbh[1] &&
369cfb0a4bfSRyusuke Konishi 	    nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
370cfb0a4bfSRyusuke Konishi 		sb2i = 1;
371cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[1]->b_blocknr;
372cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
373cfb0a4bfSRyusuke Konishi 		sb2i = 0;
374cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[0]->b_blocknr;
375cfb0a4bfSRyusuke Konishi 	}
376cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
377cfb0a4bfSRyusuke Konishi 		goto out;  /* super block location is unchanged */
378cfb0a4bfSRyusuke Konishi 
379cfb0a4bfSRyusuke Konishi 	/* Get new super block buffer */
380cfb0a4bfSRyusuke Konishi 	newblocknr = sb2off >> nilfs->ns_blocksize_bits;
381cfb0a4bfSRyusuke Konishi 	offset = sb2off & (nilfs->ns_blocksize - 1);
382cfb0a4bfSRyusuke Konishi 	nsbh = sb_getblk(sb, newblocknr);
383cfb0a4bfSRyusuke Konishi 	if (!nsbh) {
384cfb0a4bfSRyusuke Konishi 		printk(KERN_WARNING
385cfb0a4bfSRyusuke Konishi 		       "NILFS warning: unable to move secondary superblock "
386cfb0a4bfSRyusuke Konishi 		       "to block %llu\n", (unsigned long long)newblocknr);
387cfb0a4bfSRyusuke Konishi 		ret = -EIO;
388cfb0a4bfSRyusuke Konishi 		goto out;
389cfb0a4bfSRyusuke Konishi 	}
390cfb0a4bfSRyusuke Konishi 	nsbp = (void *)nsbh->b_data + offset;
391cfb0a4bfSRyusuke Konishi 	memset(nsbp, 0, nilfs->ns_blocksize);
392cfb0a4bfSRyusuke Konishi 
393cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0) {
394cfb0a4bfSRyusuke Konishi 		memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
395cfb0a4bfSRyusuke Konishi 		brelse(nilfs->ns_sbh[sb2i]);
396cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[sb2i] = nsbh;
397cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[sb2i] = nsbp;
398cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
399cfb0a4bfSRyusuke Konishi 		/* secondary super block will be restored to index 1 */
400cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[1] = nsbh;
401cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[1] = nsbp;
402cfb0a4bfSRyusuke Konishi 	} else {
403cfb0a4bfSRyusuke Konishi 		brelse(nsbh);
404cfb0a4bfSRyusuke Konishi 	}
405cfb0a4bfSRyusuke Konishi out:
406cfb0a4bfSRyusuke Konishi 	return ret;
407cfb0a4bfSRyusuke Konishi }
408cfb0a4bfSRyusuke Konishi 
4094e33f9eaSRyusuke Konishi /**
4104e33f9eaSRyusuke Konishi  * nilfs_resize_fs - resize the filesystem
4114e33f9eaSRyusuke Konishi  * @sb: super block instance
4124e33f9eaSRyusuke Konishi  * @newsize: new size of the filesystem (in bytes)
4134e33f9eaSRyusuke Konishi  */
4144e33f9eaSRyusuke Konishi int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
4154e33f9eaSRyusuke Konishi {
4164e33f9eaSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
4174e33f9eaSRyusuke Konishi 	struct nilfs_super_block **sbp;
4184e33f9eaSRyusuke Konishi 	__u64 devsize, newnsegs;
4194e33f9eaSRyusuke Konishi 	loff_t sb2off;
4204e33f9eaSRyusuke Konishi 	int ret;
4214e33f9eaSRyusuke Konishi 
4224e33f9eaSRyusuke Konishi 	ret = -ERANGE;
4234e33f9eaSRyusuke Konishi 	devsize = i_size_read(sb->s_bdev->bd_inode);
4244e33f9eaSRyusuke Konishi 	if (newsize > devsize)
4254e33f9eaSRyusuke Konishi 		goto out;
4264e33f9eaSRyusuke Konishi 
4274e33f9eaSRyusuke Konishi 	/*
4284e33f9eaSRyusuke Konishi 	 * Write lock is required to protect some functions depending
4294e33f9eaSRyusuke Konishi 	 * on the number of segments, the number of reserved segments,
4304e33f9eaSRyusuke Konishi 	 * and so forth.
4314e33f9eaSRyusuke Konishi 	 */
4324e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_segctor_sem);
4334e33f9eaSRyusuke Konishi 
4344e33f9eaSRyusuke Konishi 	sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
4354e33f9eaSRyusuke Konishi 	newnsegs = sb2off >> nilfs->ns_blocksize_bits;
4364e33f9eaSRyusuke Konishi 	do_div(newnsegs, nilfs->ns_blocks_per_segment);
4374e33f9eaSRyusuke Konishi 
4384e33f9eaSRyusuke Konishi 	ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
4394e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_segctor_sem);
4404e33f9eaSRyusuke Konishi 	if (ret < 0)
4414e33f9eaSRyusuke Konishi 		goto out;
4424e33f9eaSRyusuke Konishi 
4434e33f9eaSRyusuke Konishi 	ret = nilfs_construct_segment(sb);
4444e33f9eaSRyusuke Konishi 	if (ret < 0)
4454e33f9eaSRyusuke Konishi 		goto out;
4464e33f9eaSRyusuke Konishi 
4474e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_sem);
4484e33f9eaSRyusuke Konishi 	nilfs_move_2nd_super(sb, sb2off);
4494e33f9eaSRyusuke Konishi 	ret = -EIO;
4504e33f9eaSRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
4514e33f9eaSRyusuke Konishi 	if (likely(sbp)) {
4524e33f9eaSRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
4534e33f9eaSRyusuke Konishi 		/*
4544e33f9eaSRyusuke Konishi 		 * Drop NILFS_RESIZE_FS flag for compatibility with
4554e33f9eaSRyusuke Konishi 		 * mount-time resize which may be implemented in a
4564e33f9eaSRyusuke Konishi 		 * future release.
4574e33f9eaSRyusuke Konishi 		 */
4584e33f9eaSRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
4594e33f9eaSRyusuke Konishi 					      ~NILFS_RESIZE_FS);
4604e33f9eaSRyusuke Konishi 		sbp[0]->s_dev_size = cpu_to_le64(newsize);
4614e33f9eaSRyusuke Konishi 		sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
4624e33f9eaSRyusuke Konishi 		if (sbp[1])
4634e33f9eaSRyusuke Konishi 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
4644e33f9eaSRyusuke Konishi 		ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
4654e33f9eaSRyusuke Konishi 	}
4664e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_sem);
4674e33f9eaSRyusuke Konishi 
4684e33f9eaSRyusuke Konishi 	/*
4694e33f9eaSRyusuke Konishi 	 * Reset the range of allocatable segments last.  This order
4704e33f9eaSRyusuke Konishi 	 * is important in the case of expansion because the secondary
4714e33f9eaSRyusuke Konishi 	 * superblock must be protected from log write until migration
4724e33f9eaSRyusuke Konishi 	 * completes.
4734e33f9eaSRyusuke Konishi 	 */
4744e33f9eaSRyusuke Konishi 	if (!ret)
4754e33f9eaSRyusuke Konishi 		nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
4764e33f9eaSRyusuke Konishi out:
4774e33f9eaSRyusuke Konishi 	return ret;
4784e33f9eaSRyusuke Konishi }
4794e33f9eaSRyusuke Konishi 
480783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
481783f6184SRyusuke Konishi {
482e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
483783f6184SRyusuke Konishi 
484f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
485783f6184SRyusuke Konishi 
486783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
487783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
488f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
489783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
490783f6184SRyusuke Konishi 	}
491783f6184SRyusuke Konishi 
492f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
493f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
494f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
495f1e89c86SRyusuke Konishi 
496348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
497783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
498783f6184SRyusuke Konishi }
499783f6184SRyusuke Konishi 
500783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
501783f6184SRyusuke Konishi {
502e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
503d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
504783f6184SRyusuke Konishi 	int err = 0;
505783f6184SRyusuke Konishi 
506783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
507783f6184SRyusuke Konishi 	if (wait)
508783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
5096233caa9SJiro SEKIBA 
5106233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
511d26493b6SJiro SEKIBA 	if (nilfs_sb_dirty(nilfs)) {
512f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
513b2ac86e1SJiro SEKIBA 		if (likely(sbp)) {
514b2ac86e1SJiro SEKIBA 			nilfs_set_log_cursor(sbp[0], nilfs);
515f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT);
516b2ac86e1SJiro SEKIBA 		}
517d26493b6SJiro SEKIBA 	}
5186233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
5196233caa9SJiro SEKIBA 
520e2c7617aSAndreas Rohner 	if (!err)
521e2c7617aSAndreas Rohner 		err = nilfs_flush_device(nilfs);
522e2c7617aSAndreas Rohner 
523783f6184SRyusuke Konishi 	return err;
524783f6184SRyusuke Konishi }
525783f6184SRyusuke Konishi 
526f7545144SRyusuke Konishi int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
5274d8d9293SRyusuke Konishi 			    struct nilfs_root **rootp)
528783f6184SRyusuke Konishi {
529e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5304d8d9293SRyusuke Konishi 	struct nilfs_root *root;
531783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
532783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
5334d8d9293SRyusuke Konishi 	int err = -ENOMEM;
534783f6184SRyusuke Konishi 
5354d8d9293SRyusuke Konishi 	root = nilfs_find_or_create_root(
5364d8d9293SRyusuke Konishi 		nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
5374d8d9293SRyusuke Konishi 	if (!root)
5384d8d9293SRyusuke Konishi 		return err;
539783f6184SRyusuke Konishi 
540e912a5b6SRyusuke Konishi 	if (root->ifile)
541e912a5b6SRyusuke Konishi 		goto reuse; /* already attached checkpoint */
542783f6184SRyusuke Konishi 
5431154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
544783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
545783f6184SRyusuke Konishi 					  &bh_cp);
5461154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
547783f6184SRyusuke Konishi 	if (unlikely(err)) {
548783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
549783f6184SRyusuke Konishi 			printk(KERN_ERR
550783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
551783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
552783f6184SRyusuke Konishi 			       (unsigned long long)cno);
553783f6184SRyusuke Konishi 			err = -EINVAL;
554783f6184SRyusuke Konishi 		}
555783f6184SRyusuke Konishi 		goto failed;
556783f6184SRyusuke Konishi 	}
557f1e89c86SRyusuke Konishi 
558f7545144SRyusuke Konishi 	err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
559f1e89c86SRyusuke Konishi 			       &raw_cp->cp_ifile_inode, &root->ifile);
560f1e89c86SRyusuke Konishi 	if (err)
561783f6184SRyusuke Konishi 		goto failed_bh;
562b7c06342SRyusuke Konishi 
563e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->inodes_count,
564e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_inodes_count));
565e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->blocks_count,
566e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_blocks_count));
567783f6184SRyusuke Konishi 
568783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
5694d8d9293SRyusuke Konishi 
570e912a5b6SRyusuke Konishi  reuse:
5714d8d9293SRyusuke Konishi 	*rootp = root;
572783f6184SRyusuke Konishi 	return 0;
573783f6184SRyusuke Konishi 
574783f6184SRyusuke Konishi  failed_bh:
575783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
576783f6184SRyusuke Konishi  failed:
5774d8d9293SRyusuke Konishi 	nilfs_put_root(root);
578783f6184SRyusuke Konishi 
579783f6184SRyusuke Konishi 	return err;
580783f6184SRyusuke Konishi }
581783f6184SRyusuke Konishi 
5825beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb)
583783f6184SRyusuke Konishi {
584e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5855beb6e0bSRyusuke Konishi 	int err;
5865beb6e0bSRyusuke Konishi 
5875beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
5885beb6e0bSRyusuke Konishi 		return 0;
5895beb6e0bSRyusuke Konishi 
5905beb6e0bSRyusuke Konishi 	/* Mark super block clean */
5915beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
592f7545144SRyusuke Konishi 	err = nilfs_cleanup_super(sb);
5935beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
5945beb6e0bSRyusuke Konishi 	return err;
5955beb6e0bSRyusuke Konishi }
5965beb6e0bSRyusuke Konishi 
5975beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb)
5985beb6e0bSRyusuke Konishi {
599e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
600783f6184SRyusuke Konishi 
6015beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
6025beb6e0bSRyusuke Konishi 		return 0;
6035beb6e0bSRyusuke Konishi 
6045beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
605f7545144SRyusuke Konishi 	nilfs_setup_super(sb, false);
6065beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
6075beb6e0bSRyusuke Konishi 	return 0;
608783f6184SRyusuke Konishi }
609783f6184SRyusuke Konishi 
610783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
611783f6184SRyusuke Konishi {
612783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
613b7c06342SRyusuke Konishi 	struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root;
614b7c06342SRyusuke Konishi 	struct the_nilfs *nilfs = root->nilfs;
615c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
616783f6184SRyusuke Konishi 	unsigned long long blocks;
617783f6184SRyusuke Konishi 	unsigned long overhead;
618783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
619783f6184SRyusuke Konishi 	sector_t nfreeblocks;
620c7ef972cSVyacheslav Dubeyko 	u64 nmaxinodes, nfreeinodes;
621783f6184SRyusuke Konishi 	int err;
622783f6184SRyusuke Konishi 
623783f6184SRyusuke Konishi 	/*
624783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
625783f6184SRyusuke Konishi 	 *
626783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
627783f6184SRyusuke Konishi 	 * are excluded.
628783f6184SRyusuke Konishi 	 */
629783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
630783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
631783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
632783f6184SRyusuke Konishi 
633783f6184SRyusuke Konishi 	/*
634783f6184SRyusuke Konishi 	 * Compute the overhead
635783f6184SRyusuke Konishi 	 *
6367a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
637783f6184SRyusuke Konishi 	 * We must count them as the overhead.
638783f6184SRyusuke Konishi 	 */
639783f6184SRyusuke Konishi 	overhead = 0;
640783f6184SRyusuke Konishi 
641783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
642783f6184SRyusuke Konishi 	if (unlikely(err))
643783f6184SRyusuke Konishi 		return err;
644783f6184SRyusuke Konishi 
645c7ef972cSVyacheslav Dubeyko 	err = nilfs_ifile_count_free_inodes(root->ifile,
646c7ef972cSVyacheslav Dubeyko 					    &nmaxinodes, &nfreeinodes);
647c7ef972cSVyacheslav Dubeyko 	if (unlikely(err)) {
648c7ef972cSVyacheslav Dubeyko 		printk(KERN_WARNING
649c7ef972cSVyacheslav Dubeyko 			"NILFS warning: fail to count free inodes: err %d.\n",
650c7ef972cSVyacheslav Dubeyko 			err);
651c7ef972cSVyacheslav Dubeyko 		if (err == -ERANGE) {
652c7ef972cSVyacheslav Dubeyko 			/*
653c7ef972cSVyacheslav Dubeyko 			 * If nilfs_palloc_count_max_entries() returns
654c7ef972cSVyacheslav Dubeyko 			 * -ERANGE error code then we simply treat
655c7ef972cSVyacheslav Dubeyko 			 * curent inodes count as maximum possible and
656c7ef972cSVyacheslav Dubeyko 			 * zero as free inodes value.
657c7ef972cSVyacheslav Dubeyko 			 */
658e5f7f848SVyacheslav Dubeyko 			nmaxinodes = atomic64_read(&root->inodes_count);
659c7ef972cSVyacheslav Dubeyko 			nfreeinodes = 0;
660c7ef972cSVyacheslav Dubeyko 			err = 0;
661c7ef972cSVyacheslav Dubeyko 		} else
662c7ef972cSVyacheslav Dubeyko 			return err;
663c7ef972cSVyacheslav Dubeyko 	}
664c7ef972cSVyacheslav Dubeyko 
665783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
666783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
667783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
668783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
669783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
670783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
671c7ef972cSVyacheslav Dubeyko 	buf->f_files = nmaxinodes;
672c7ef972cSVyacheslav Dubeyko 	buf->f_ffree = nfreeinodes;
673783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
674c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
675c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
676c306af23SRyusuke Konishi 
677783f6184SRyusuke Konishi 	return 0;
678783f6184SRyusuke Konishi }
679783f6184SRyusuke Konishi 
68034c80b1dSAl Viro static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
681b58a285bSJiro SEKIBA {
68234c80b1dSAl Viro 	struct super_block *sb = dentry->d_sb;
683e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
68434c80b1dSAl Viro 	struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root;
685b58a285bSJiro SEKIBA 
6863b2ce58bSRyusuke Konishi 	if (!nilfs_test_opt(nilfs, BARRIER))
687c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
688f11459adSRyusuke Konishi 	if (root->cno != NILFS_CPTREE_CURRENT_CNO)
689f11459adSRyusuke Konishi 		seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
6903b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
691c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
6923b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_CONT))
693c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
6943b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, STRICT_ORDER))
695c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
6963b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, NORECOVERY))
697c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
6983b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, DISCARD))
699c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
700b58a285bSJiro SEKIBA 
701b58a285bSJiro SEKIBA 	return 0;
702b58a285bSJiro SEKIBA }
703b58a285bSJiro SEKIBA 
704b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
705783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
706783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
707783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
7086fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
709783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
710783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
7115beb6e0bSRyusuke Konishi 	.freeze_fs	= nilfs_freeze,
7125beb6e0bSRyusuke Konishi 	.unfreeze_fs	= nilfs_unfreeze,
713783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
714783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
715b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
716783f6184SRyusuke Konishi };
717783f6184SRyusuke Konishi 
718783f6184SRyusuke Konishi enum {
719783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
720773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
721802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, Opt_err,
722783f6184SRyusuke Konishi };
723783f6184SRyusuke Konishi 
724783f6184SRyusuke Konishi static match_table_t tokens = {
725783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
726783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
727783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
728773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
72991f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
730783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
731783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
7320234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
733e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
734802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
735783f6184SRyusuke Konishi 	{Opt_err, NULL}
736783f6184SRyusuke Konishi };
737783f6184SRyusuke Konishi 
7387c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
739783f6184SRyusuke Konishi {
740e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
741783f6184SRyusuke Konishi 	char *p;
742783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
743783f6184SRyusuke Konishi 
744783f6184SRyusuke Konishi 	if (!options)
745783f6184SRyusuke Konishi 		return 1;
746783f6184SRyusuke Konishi 
747783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
748783f6184SRyusuke Konishi 		int token;
749783f6184SRyusuke Konishi 		if (!*p)
750783f6184SRyusuke Konishi 			continue;
751783f6184SRyusuke Konishi 
752783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
753783f6184SRyusuke Konishi 		switch (token) {
754773bc4f3SRyusuke Konishi 		case Opt_barrier:
7553b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, BARRIER);
756773bc4f3SRyusuke Konishi 			break;
75791f1953bSJiro SEKIBA 		case Opt_nobarrier:
7583b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, BARRIER);
759783f6184SRyusuke Konishi 			break;
760783f6184SRyusuke Konishi 		case Opt_order:
761783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
762783f6184SRyusuke Konishi 				/* Ordered data semantics */
7633b2ce58bSRyusuke Konishi 				nilfs_clear_opt(nilfs, STRICT_ORDER);
764783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
765783f6184SRyusuke Konishi 				/* Strict in-order semantics */
7663b2ce58bSRyusuke Konishi 				nilfs_set_opt(nilfs, STRICT_ORDER);
767783f6184SRyusuke Konishi 			else
768783f6184SRyusuke Konishi 				return 0;
769783f6184SRyusuke Konishi 			break;
770783f6184SRyusuke Konishi 		case Opt_err_panic:
7713b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
772783f6184SRyusuke Konishi 			break;
773783f6184SRyusuke Konishi 		case Opt_err_ro:
7743b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
775783f6184SRyusuke Konishi 			break;
776783f6184SRyusuke Konishi 		case Opt_err_cont:
7773b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
778783f6184SRyusuke Konishi 			break;
779783f6184SRyusuke Konishi 		case Opt_snapshot:
7807c017457SRyusuke Konishi 			if (is_remount) {
7817c017457SRyusuke Konishi 				printk(KERN_ERR
782f11459adSRyusuke Konishi 				       "NILFS: \"%s\" option is invalid "
783f11459adSRyusuke Konishi 				       "for remount.\n", p);
7847c017457SRyusuke Konishi 				return 0;
7857c017457SRyusuke Konishi 			}
7867c017457SRyusuke Konishi 			break;
7870234576dSRyusuke Konishi 		case Opt_norecovery:
7883b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, NORECOVERY);
7890234576dSRyusuke Konishi 			break;
790e902ec99SJiro SEKIBA 		case Opt_discard:
7913b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, DISCARD);
792e902ec99SJiro SEKIBA 			break;
793802d3177SRyusuke Konishi 		case Opt_nodiscard:
7943b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, DISCARD);
795802d3177SRyusuke Konishi 			break;
796783f6184SRyusuke Konishi 		default:
797783f6184SRyusuke Konishi 			printk(KERN_ERR
798783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
799783f6184SRyusuke Konishi 			return 0;
800783f6184SRyusuke Konishi 		}
801783f6184SRyusuke Konishi 	}
802783f6184SRyusuke Konishi 	return 1;
803783f6184SRyusuke Konishi }
804783f6184SRyusuke Konishi 
805783f6184SRyusuke Konishi static inline void
8063b2ce58bSRyusuke Konishi nilfs_set_default_options(struct super_block *sb,
807783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
808783f6184SRyusuke Konishi {
809e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
8103b2ce58bSRyusuke Konishi 
8113b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt =
812277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
813783f6184SRyusuke Konishi }
814783f6184SRyusuke Konishi 
815f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount)
816783f6184SRyusuke Konishi {
817e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
818d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
819d26493b6SJiro SEKIBA 	int max_mnt_count;
820d26493b6SJiro SEKIBA 	int mnt_count;
821783f6184SRyusuke Konishi 
822d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
823f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
824d26493b6SJiro SEKIBA 	if (!sbp)
825d26493b6SJiro SEKIBA 		return -EIO;
826d26493b6SJiro SEKIBA 
8275beb6e0bSRyusuke Konishi 	if (!is_mount)
8285beb6e0bSRyusuke Konishi 		goto skip_mount_setup;
8295beb6e0bSRyusuke Konishi 
830d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
831d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
832d26493b6SJiro SEKIBA 
833f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
834783f6184SRyusuke Konishi 		printk(KERN_WARNING
835783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
836783f6184SRyusuke Konishi #if 0
837783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
838783f6184SRyusuke Konishi 		printk(KERN_WARNING
839783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
840783f6184SRyusuke Konishi #endif
841783f6184SRyusuke Konishi 	}
842783f6184SRyusuke Konishi 	if (!max_mnt_count)
843d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
844783f6184SRyusuke Konishi 
845d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
8465beb6e0bSRyusuke Konishi 	sbp[0]->s_mtime = cpu_to_le64(get_seconds());
8475beb6e0bSRyusuke Konishi 
8485beb6e0bSRyusuke Konishi skip_mount_setup:
849d26493b6SJiro SEKIBA 	sbp[0]->s_state =
850d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
851b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
8520ca7a5b9SRyusuke Konishi 	if (sbp[1])
853b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
854f7545144SRyusuke Konishi 	return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
855783f6184SRyusuke Konishi }
856783f6184SRyusuke Konishi 
857e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
858e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
859e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
860783f6184SRyusuke Konishi {
861e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
862e339ad31SRyusuke Konishi 	unsigned long offset;
863783f6184SRyusuke Konishi 
864e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
865783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
866e339ad31SRyusuke Konishi 	if (!*pbh)
867783f6184SRyusuke Konishi 		return NULL;
868783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
869783f6184SRyusuke Konishi }
870783f6184SRyusuke Konishi 
871783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
872783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
873783f6184SRyusuke Konishi 				 char *data)
874783f6184SRyusuke Konishi {
875e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
876783f6184SRyusuke Konishi 
877783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
878783f6184SRyusuke Konishi 
879783f6184SRyusuke Konishi 	/* FS independent flags */
880783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
881783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
882783f6184SRyusuke Konishi #endif
883783f6184SRyusuke Konishi 
8843b2ce58bSRyusuke Konishi 	nilfs_set_default_options(sb, sbp);
885783f6184SRyusuke Konishi 
886574e6c31SRyusuke Konishi 	nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
887574e6c31SRyusuke Konishi 	nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
888574e6c31SRyusuke Konishi 	nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
889574e6c31SRyusuke Konishi 	nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
890783f6184SRyusuke Konishi 
8917c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0 ;
892783f6184SRyusuke Konishi }
893783f6184SRyusuke Konishi 
894c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
895c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
896c5ca48aaSRyusuke Konishi {
897c5ca48aaSRyusuke Konishi 	__u64 features;
898c5ca48aaSRyusuke Konishi 
899c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
900c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
901c5ca48aaSRyusuke Konishi 	if (features) {
902c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
903c5ca48aaSRyusuke Konishi 		       "optional features (%llx)\n",
904c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
905c5ca48aaSRyusuke Konishi 		return -EINVAL;
906c5ca48aaSRyusuke Konishi 	}
907c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
908c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
909c5ca48aaSRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY) && features) {
910c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
911c5ca48aaSRyusuke Konishi 		       "unsupported optional features (%llx)\n",
912c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
913c5ca48aaSRyusuke Konishi 		return -EINVAL;
914c5ca48aaSRyusuke Konishi 	}
915c5ca48aaSRyusuke Konishi 	return 0;
916c5ca48aaSRyusuke Konishi }
917c5ca48aaSRyusuke Konishi 
918367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
919367ea334SRyusuke Konishi 				 struct nilfs_root *root,
920367ea334SRyusuke Konishi 				 struct dentry **root_dentry)
921367ea334SRyusuke Konishi {
922367ea334SRyusuke Konishi 	struct inode *inode;
923367ea334SRyusuke Konishi 	struct dentry *dentry;
924367ea334SRyusuke Konishi 	int ret = 0;
925367ea334SRyusuke Konishi 
926367ea334SRyusuke Konishi 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
927367ea334SRyusuke Konishi 	if (IS_ERR(inode)) {
928367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
929367ea334SRyusuke Konishi 		ret = PTR_ERR(inode);
930367ea334SRyusuke Konishi 		goto out;
931367ea334SRyusuke Konishi 	}
932367ea334SRyusuke Konishi 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
933367ea334SRyusuke Konishi 		iput(inode);
934367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
935367ea334SRyusuke Konishi 		ret = -EINVAL;
936367ea334SRyusuke Konishi 		goto out;
937367ea334SRyusuke Konishi 	}
938367ea334SRyusuke Konishi 
939f11459adSRyusuke Konishi 	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
940f11459adSRyusuke Konishi 		dentry = d_find_alias(inode);
941f11459adSRyusuke Konishi 		if (!dentry) {
94248fde701SAl Viro 			dentry = d_make_root(inode);
943367ea334SRyusuke Konishi 			if (!dentry) {
944367ea334SRyusuke Konishi 				ret = -ENOMEM;
945f11459adSRyusuke Konishi 				goto failed_dentry;
946f11459adSRyusuke Konishi 			}
947f11459adSRyusuke Konishi 		} else {
948f11459adSRyusuke Konishi 			iput(inode);
949f11459adSRyusuke Konishi 		}
950f11459adSRyusuke Konishi 	} else {
9511a0a397eSJ. Bruce Fields 		dentry = d_obtain_root(inode);
952f11459adSRyusuke Konishi 		if (IS_ERR(dentry)) {
953f11459adSRyusuke Konishi 			ret = PTR_ERR(dentry);
954f11459adSRyusuke Konishi 			goto failed_dentry;
955f11459adSRyusuke Konishi 		}
956367ea334SRyusuke Konishi 	}
957367ea334SRyusuke Konishi 	*root_dentry = dentry;
958367ea334SRyusuke Konishi  out:
959367ea334SRyusuke Konishi 	return ret;
960f11459adSRyusuke Konishi 
961f11459adSRyusuke Konishi  failed_dentry:
962f11459adSRyusuke Konishi 	printk(KERN_ERR "NILFS: get root dentry failed\n");
963f11459adSRyusuke Konishi 	goto out;
964367ea334SRyusuke Konishi }
965367ea334SRyusuke Konishi 
966ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
967ab4d8f7eSRyusuke Konishi 				 struct dentry **root_dentry)
968ab4d8f7eSRyusuke Konishi {
969e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = s->s_fs_info;
970ab4d8f7eSRyusuke Konishi 	struct nilfs_root *root;
971ab4d8f7eSRyusuke Konishi 	int ret;
972ab4d8f7eSRyusuke Konishi 
973572d8b39SRyusuke Konishi 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
974572d8b39SRyusuke Konishi 
975ab4d8f7eSRyusuke Konishi 	down_read(&nilfs->ns_segctor_sem);
976ab4d8f7eSRyusuke Konishi 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
977ab4d8f7eSRyusuke Konishi 	up_read(&nilfs->ns_segctor_sem);
978ab4d8f7eSRyusuke Konishi 	if (ret < 0) {
979ab4d8f7eSRyusuke Konishi 		ret = (ret == -ENOENT) ? -EINVAL : ret;
980ab4d8f7eSRyusuke Konishi 		goto out;
981ab4d8f7eSRyusuke Konishi 	} else if (!ret) {
982ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: The specified checkpoint is "
983ab4d8f7eSRyusuke Konishi 		       "not a snapshot (checkpoint number=%llu).\n",
984ab4d8f7eSRyusuke Konishi 		       (unsigned long long)cno);
985ab4d8f7eSRyusuke Konishi 		ret = -EINVAL;
986ab4d8f7eSRyusuke Konishi 		goto out;
987ab4d8f7eSRyusuke Konishi 	}
988ab4d8f7eSRyusuke Konishi 
989f7545144SRyusuke Konishi 	ret = nilfs_attach_checkpoint(s, cno, false, &root);
990ab4d8f7eSRyusuke Konishi 	if (ret) {
991ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading snapshot "
992ab4d8f7eSRyusuke Konishi 		       "(checkpoint number=%llu).\n",
993ab4d8f7eSRyusuke Konishi 	       (unsigned long long)cno);
994ab4d8f7eSRyusuke Konishi 		goto out;
995ab4d8f7eSRyusuke Konishi 	}
996ab4d8f7eSRyusuke Konishi 	ret = nilfs_get_root_dentry(s, root, root_dentry);
997ab4d8f7eSRyusuke Konishi 	nilfs_put_root(root);
998ab4d8f7eSRyusuke Konishi  out:
999572d8b39SRyusuke Konishi 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
1000ab4d8f7eSRyusuke Konishi 	return ret;
1001ab4d8f7eSRyusuke Konishi }
1002ab4d8f7eSRyusuke Konishi 
1003f11459adSRyusuke Konishi /**
1004e95c311eSAl Viro  * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
1005f11459adSRyusuke Konishi  * @root_dentry: root dentry of the tree to be shrunk
1006f11459adSRyusuke Konishi  *
1007f11459adSRyusuke Konishi  * This function returns true if the tree was in-use.
1008f11459adSRyusuke Konishi  */
1009e95c311eSAl Viro static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1010f11459adSRyusuke Konishi {
1011f11459adSRyusuke Konishi 	shrink_dcache_parent(root_dentry);
1012e95c311eSAl Viro 	return d_count(root_dentry) > 1;
1013f11459adSRyusuke Konishi }
1014f11459adSRyusuke Konishi 
1015032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1016032dbb3bSRyusuke Konishi {
1017e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1018032dbb3bSRyusuke Konishi 	struct nilfs_root *root;
1019032dbb3bSRyusuke Konishi 	struct inode *inode;
1020032dbb3bSRyusuke Konishi 	struct dentry *dentry;
1021032dbb3bSRyusuke Konishi 	int ret;
1022032dbb3bSRyusuke Konishi 
1023*3377f843SRyusuke Konishi 	if (cno > nilfs->ns_cno)
1024032dbb3bSRyusuke Konishi 		return false;
1025032dbb3bSRyusuke Konishi 
1026032dbb3bSRyusuke Konishi 	if (cno >= nilfs_last_cno(nilfs))
1027032dbb3bSRyusuke Konishi 		return true;	/* protect recent checkpoints */
1028032dbb3bSRyusuke Konishi 
1029032dbb3bSRyusuke Konishi 	ret = false;
1030e3154e97SRyusuke Konishi 	root = nilfs_lookup_root(nilfs, cno);
1031032dbb3bSRyusuke Konishi 	if (root) {
1032032dbb3bSRyusuke Konishi 		inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1033032dbb3bSRyusuke Konishi 		if (inode) {
1034032dbb3bSRyusuke Konishi 			dentry = d_find_alias(inode);
1035032dbb3bSRyusuke Konishi 			if (dentry) {
1036e95c311eSAl Viro 				ret = nilfs_tree_is_busy(dentry);
1037032dbb3bSRyusuke Konishi 				dput(dentry);
1038032dbb3bSRyusuke Konishi 			}
1039032dbb3bSRyusuke Konishi 			iput(inode);
1040032dbb3bSRyusuke Konishi 		}
1041032dbb3bSRyusuke Konishi 		nilfs_put_root(root);
1042032dbb3bSRyusuke Konishi 	}
1043032dbb3bSRyusuke Konishi 	return ret;
1044032dbb3bSRyusuke Konishi }
1045032dbb3bSRyusuke Konishi 
1046783f6184SRyusuke Konishi /**
1047783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
1048783f6184SRyusuke Konishi  * @sb: super_block
1049783f6184SRyusuke Konishi  * @data: mount options
1050783f6184SRyusuke Konishi  * @silent: silent mode flag
1051783f6184SRyusuke Konishi  *
1052aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
1053783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
1054783f6184SRyusuke Konishi  */
1055783f6184SRyusuke Konishi static int
1056348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent)
1057783f6184SRyusuke Konishi {
1058348fe8daSRyusuke Konishi 	struct the_nilfs *nilfs;
10594d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
1060783f6184SRyusuke Konishi 	__u64 cno;
1061783f6184SRyusuke Konishi 	int err;
1062783f6184SRyusuke Konishi 
1063e3154e97SRyusuke Konishi 	nilfs = alloc_nilfs(sb->s_bdev);
1064e3154e97SRyusuke Konishi 	if (!nilfs)
1065783f6184SRyusuke Konishi 		return -ENOMEM;
1066783f6184SRyusuke Konishi 
1067e3154e97SRyusuke Konishi 	sb->s_fs_info = nilfs;
1068783f6184SRyusuke Konishi 
1069f7545144SRyusuke Konishi 	err = init_nilfs(nilfs, sb, (char *)data);
1070783f6184SRyusuke Konishi 	if (err)
1071348fe8daSRyusuke Konishi 		goto failed_nilfs;
1072783f6184SRyusuke Konishi 
1073783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
1074783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
1075783f6184SRyusuke Konishi 	sb->s_root = NULL;
107661239230SRyusuke Konishi 	sb->s_time_gran = 1;
10778de52778SAl Viro 	sb->s_max_links = NILFS_LINK_MAX;
1078026a7d63SRyusuke Konishi 
107926ff1304SChristoph Hellwig 	sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
1080783f6184SRyusuke Konishi 
1081f7545144SRyusuke Konishi 	err = load_nilfs(nilfs, sb);
1082783f6184SRyusuke Konishi 	if (err)
1083348fe8daSRyusuke Konishi 		goto failed_nilfs;
1084f50a4c81SRyusuke Konishi 
1085783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
1086f7545144SRyusuke Konishi 	err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1087783f6184SRyusuke Konishi 	if (err) {
1088f11459adSRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading last checkpoint "
1089783f6184SRyusuke Konishi 		       "(checkpoint number=%llu).\n", (unsigned long long)cno);
1090f1e89c86SRyusuke Konishi 		goto failed_unload;
1091783f6184SRyusuke Konishi 	}
1092783f6184SRyusuke Konishi 
1093783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1094f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, fsroot);
1095783f6184SRyusuke Konishi 		if (err)
1096783f6184SRyusuke Konishi 			goto failed_checkpoint;
1097783f6184SRyusuke Konishi 	}
1098783f6184SRyusuke Konishi 
1099367ea334SRyusuke Konishi 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1100367ea334SRyusuke Konishi 	if (err)
1101783f6184SRyusuke Konishi 		goto failed_segctor;
1102783f6184SRyusuke Konishi 
11034d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1104783f6184SRyusuke Konishi 
1105783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1106783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1107f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1108783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1109783f6184SRyusuke Konishi 	}
1110783f6184SRyusuke Konishi 
1111783f6184SRyusuke Konishi 	return 0;
1112783f6184SRyusuke Konishi 
1113783f6184SRyusuke Konishi  failed_segctor:
1114f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
1115783f6184SRyusuke Konishi 
1116783f6184SRyusuke Konishi  failed_checkpoint:
11174d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1118783f6184SRyusuke Konishi 
1119f1e89c86SRyusuke Konishi  failed_unload:
1120f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
1121f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
1122f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
1123f1e89c86SRyusuke Konishi 
1124348fe8daSRyusuke Konishi  failed_nilfs:
1125348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
1126783f6184SRyusuke Konishi 	return err;
1127783f6184SRyusuke Konishi }
1128783f6184SRyusuke Konishi 
1129783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1130783f6184SRyusuke Konishi {
1131e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1132783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
113306df0f99SRyusuke Konishi 	unsigned long old_mount_opt;
1134f11459adSRyusuke Konishi 	int err;
1135783f6184SRyusuke Konishi 
113602b9984dSTheodore Ts'o 	sync_filesystem(sb);
1137783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
11383b2ce58bSRyusuke Konishi 	old_mount_opt = nilfs->ns_mount_opt;
1139783f6184SRyusuke Konishi 
11407c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
1141783f6184SRyusuke Konishi 		err = -EINVAL;
1142783f6184SRyusuke Konishi 		goto restore_opts;
1143783f6184SRyusuke Konishi 	}
1144783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1145783f6184SRyusuke Konishi 
1146d240e067SRyusuke Konishi 	err = -EINVAL;
1147783f6184SRyusuke Konishi 
11480234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
11490234576dSRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
11500234576dSRyusuke Konishi 		       "remount because the filesystem is in an "
11510234576dSRyusuke Konishi 		       "incomplete recovery state.\n", sb->s_id);
11520234576dSRyusuke Konishi 		goto restore_opts;
11530234576dSRyusuke Konishi 	}
11540234576dSRyusuke Konishi 
1155783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1156783f6184SRyusuke Konishi 		goto out;
1157783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
1158f7545144SRyusuke Konishi 		/* Shutting down log writer */
1159f7545144SRyusuke Konishi 		nilfs_detach_log_writer(sb);
1160783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
1161783f6184SRyusuke Konishi 
1162783f6184SRyusuke Konishi 		/*
1163783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
1164783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
1165783f6184SRyusuke Konishi 		 */
1166783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1167f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
1168783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1169783f6184SRyusuke Konishi 	} else {
1170c5ca48aaSRyusuke Konishi 		__u64 features;
1171e912a5b6SRyusuke Konishi 		struct nilfs_root *root;
1172c5ca48aaSRyusuke Konishi 
1173783f6184SRyusuke Konishi 		/*
1174783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
1175783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
1176783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
1177783f6184SRyusuke Konishi 		 */
1178c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
1179c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1180c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
1181c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
1182c5ca48aaSRyusuke Konishi 		if (features) {
1183c5ca48aaSRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
1184c5ca48aaSRyusuke Konishi 			       "remount RDWR because of unsupported optional "
1185c5ca48aaSRyusuke Konishi 			       "features (%llx)\n",
1186c5ca48aaSRyusuke Konishi 			       sb->s_id, (unsigned long long)features);
1187c5ca48aaSRyusuke Konishi 			err = -EROFS;
1188c5ca48aaSRyusuke Konishi 			goto restore_opts;
1189c5ca48aaSRyusuke Konishi 		}
1190c5ca48aaSRyusuke Konishi 
1191783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
1192783f6184SRyusuke Konishi 
1193e912a5b6SRyusuke Konishi 		root = NILFS_I(sb->s_root->d_inode)->i_root;
1194f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, root);
1195783f6184SRyusuke Konishi 		if (err)
1196e59399d0SRyusuke Konishi 			goto restore_opts;
1197783f6184SRyusuke Konishi 
1198783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1199f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1200783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1201783f6184SRyusuke Konishi 	}
1202783f6184SRyusuke Konishi  out:
1203783f6184SRyusuke Konishi 	return 0;
1204783f6184SRyusuke Konishi 
1205783f6184SRyusuke Konishi  restore_opts:
1206783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
12073b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt = old_mount_opt;
1208783f6184SRyusuke Konishi 	return err;
1209783f6184SRyusuke Konishi }
1210783f6184SRyusuke Konishi 
1211783f6184SRyusuke Konishi struct nilfs_super_data {
1212783f6184SRyusuke Konishi 	struct block_device *bdev;
1213783f6184SRyusuke Konishi 	__u64 cno;
1214783f6184SRyusuke Konishi 	int flags;
1215783f6184SRyusuke Konishi };
1216783f6184SRyusuke Konishi 
1217783f6184SRyusuke Konishi /**
1218783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1219783f6184SRyusuke Konishi  * @data: mount options
1220783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1221783f6184SRyusuke Konishi  */
1222783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1223783f6184SRyusuke Konishi {
1224783f6184SRyusuke Konishi 	char *p, *options = data;
1225783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1226c05dbfc2SRyusuke Konishi 	int token;
1227783f6184SRyusuke Konishi 	int ret = 0;
1228783f6184SRyusuke Konishi 
1229783f6184SRyusuke Konishi 	do {
1230783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1231783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1232783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1233783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
1234c05dbfc2SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY)) {
1235783f6184SRyusuke Konishi 					ret++;
1236c05dbfc2SRyusuke Konishi 				} else {
1237c05dbfc2SRyusuke Konishi 					sd->cno = simple_strtoull(args[0].from,
1238c05dbfc2SRyusuke Konishi 								  NULL, 0);
1239c05dbfc2SRyusuke Konishi 					/*
1240c05dbfc2SRyusuke Konishi 					 * No need to see the end pointer;
1241c05dbfc2SRyusuke Konishi 					 * match_token() has done syntax
1242c05dbfc2SRyusuke Konishi 					 * checking.
1243c05dbfc2SRyusuke Konishi 					 */
1244c05dbfc2SRyusuke Konishi 					if (sd->cno == 0)
1245783f6184SRyusuke Konishi 						ret++;
1246783f6184SRyusuke Konishi 				}
1247783f6184SRyusuke Konishi 			}
1248783f6184SRyusuke Konishi 			if (ret)
1249783f6184SRyusuke Konishi 				printk(KERN_ERR
1250783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
1251783f6184SRyusuke Konishi 		}
1252783f6184SRyusuke Konishi 		if (!options)
1253783f6184SRyusuke Konishi 			break;
1254783f6184SRyusuke Konishi 		BUG_ON(options == data);
1255783f6184SRyusuke Konishi 		*(options - 1) = ',';
1256783f6184SRyusuke Konishi 	} while (!ret);
1257783f6184SRyusuke Konishi 	return ret;
1258783f6184SRyusuke Konishi }
1259783f6184SRyusuke Konishi 
1260783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1261783f6184SRyusuke Konishi {
1262f11459adSRyusuke Konishi 	s->s_bdev = data;
1263783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1264783f6184SRyusuke Konishi 	return 0;
1265783f6184SRyusuke Konishi }
1266783f6184SRyusuke Konishi 
1267783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1268783f6184SRyusuke Konishi {
1269f11459adSRyusuke Konishi 	return (void *)s->s_bdev == data;
1270783f6184SRyusuke Konishi }
1271783f6184SRyusuke Konishi 
1272e4c59d61SAl Viro static struct dentry *
1273e4c59d61SAl Viro nilfs_mount(struct file_system_type *fs_type, int flags,
1274e4c59d61SAl Viro 	     const char *dev_name, void *data)
1275783f6184SRyusuke Konishi {
1276783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
127733c8e57cSRyusuke Konishi 	struct super_block *s;
1278d4d77629STejun Heo 	fmode_t mode = FMODE_READ | FMODE_EXCL;
1279f11459adSRyusuke Konishi 	struct dentry *root_dentry;
1280f11459adSRyusuke Konishi 	int err, s_new = false;
1281783f6184SRyusuke Konishi 
128213e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
128313e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
128413e90559SRyusuke Konishi 
1285d4d77629STejun Heo 	sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1286d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1287e4c59d61SAl Viro 		return ERR_CAST(sd.bdev);
1288783f6184SRyusuke Konishi 
1289783f6184SRyusuke Konishi 	sd.cno = 0;
1290783f6184SRyusuke Konishi 	sd.flags = flags;
1291783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1292783f6184SRyusuke Konishi 		err = -EINVAL;
1293783f6184SRyusuke Konishi 		goto failed;
1294783f6184SRyusuke Konishi 	}
1295783f6184SRyusuke Konishi 
12965beb6e0bSRyusuke Konishi 	/*
12975beb6e0bSRyusuke Konishi 	 * once the super is inserted into the list by sget, s_umount
12985beb6e0bSRyusuke Konishi 	 * will protect the lockfs code from trying to start a snapshot
12995beb6e0bSRyusuke Konishi 	 * while we are mounting
13005beb6e0bSRyusuke Konishi 	 */
13015beb6e0bSRyusuke Konishi 	mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
13025beb6e0bSRyusuke Konishi 	if (sd.bdev->bd_fsfreeze_count > 0) {
13035beb6e0bSRyusuke Konishi 		mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
13045beb6e0bSRyusuke Konishi 		err = -EBUSY;
130533c8e57cSRyusuke Konishi 		goto failed;
130633c8e57cSRyusuke Konishi 	}
13079249e17fSDavid Howells 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
13089249e17fSDavid Howells 		 sd.bdev);
13095beb6e0bSRyusuke Konishi 	mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
131033c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
131133c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
1312348fe8daSRyusuke Konishi 		goto failed;
1313783f6184SRyusuke Konishi 	}
1314783f6184SRyusuke Konishi 
1315783f6184SRyusuke Konishi 	if (!s->s_root) {
1316783f6184SRyusuke Konishi 		char b[BDEVNAME_SIZE];
1317783f6184SRyusuke Konishi 
1318f11459adSRyusuke Konishi 		s_new = true;
1319f11459adSRyusuke Konishi 
132033c8e57cSRyusuke Konishi 		/* New superblock instance created */
13214571b82cSRyusuke Konishi 		s->s_mode = mode;
1322783f6184SRyusuke Konishi 		strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id));
1323783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1324783f6184SRyusuke Konishi 
1325348fe8daSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1326783f6184SRyusuke Konishi 		if (err)
1327348fe8daSRyusuke Konishi 			goto failed_super;
1328783f6184SRyusuke Konishi 
1329783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1330f11459adSRyusuke Konishi 	} else if (!sd.cno) {
1331e95c311eSAl Viro 		if (nilfs_tree_is_busy(s->s_root)) {
1332e95c311eSAl Viro 			if ((flags ^ s->s_flags) & MS_RDONLY) {
1333f11459adSRyusuke Konishi 				printk(KERN_ERR "NILFS: the device already "
1334f11459adSRyusuke Konishi 				       "has a %s mount.\n",
1335f11459adSRyusuke Konishi 				       (s->s_flags & MS_RDONLY) ?
1336f11459adSRyusuke Konishi 				       "read-only" : "read/write");
1337f11459adSRyusuke Konishi 				err = -EBUSY;
1338f11459adSRyusuke Konishi 				goto failed_super;
1339f11459adSRyusuke Konishi 			}
1340e95c311eSAl Viro 		} else {
1341f11459adSRyusuke Konishi 			/*
1342f11459adSRyusuke Konishi 			 * Try remount to setup mount states if the current
1343f11459adSRyusuke Konishi 			 * tree is not mounted and only snapshots use this sb.
1344f11459adSRyusuke Konishi 			 */
1345f11459adSRyusuke Konishi 			err = nilfs_remount(s, &flags, data);
1346f11459adSRyusuke Konishi 			if (err)
1347f11459adSRyusuke Konishi 				goto failed_super;
1348f11459adSRyusuke Konishi 		}
1349783f6184SRyusuke Konishi 	}
1350783f6184SRyusuke Konishi 
1351f11459adSRyusuke Konishi 	if (sd.cno) {
1352f11459adSRyusuke Konishi 		err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1353348fe8daSRyusuke Konishi 		if (err)
1354f11459adSRyusuke Konishi 			goto failed_super;
1355f11459adSRyusuke Konishi 	} else {
1356f11459adSRyusuke Konishi 		root_dentry = dget(s->s_root);
1357783f6184SRyusuke Konishi 	}
1358783f6184SRyusuke Konishi 
1359f11459adSRyusuke Konishi 	if (!s_new)
1360d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1361f11459adSRyusuke Konishi 
1362e4c59d61SAl Viro 	return root_dentry;
1363783f6184SRyusuke Konishi 
1364f11459adSRyusuke Konishi  failed_super:
1365a95161aaSAl Viro 	deactivate_locked_super(s);
1366783f6184SRyusuke Konishi 
1367348fe8daSRyusuke Konishi  failed:
1368348fe8daSRyusuke Konishi 	if (!s_new)
1369d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1370e4c59d61SAl Viro 	return ERR_PTR(err);
1371783f6184SRyusuke Konishi }
1372783f6184SRyusuke Konishi 
1373783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1374783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1375783f6184SRyusuke Konishi 	.name     = "nilfs2",
1376e4c59d61SAl Viro 	.mount    = nilfs_mount,
1377783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1378783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1379783f6184SRyusuke Konishi };
13807f78e035SEric W. Biederman MODULE_ALIAS_FS("nilfs2");
1381783f6184SRyusuke Konishi 
138241c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
138341c88bd7SLi Hong {
138441c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
138541c88bd7SLi Hong 
138641c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
138741c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
138841c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
138941c88bd7SLi Hong #endif
13902aa15890SMiklos Szeredi 	address_space_init_once(&ii->i_btnode_cache);
139105d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
139241c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
139341c88bd7SLi Hong }
139441c88bd7SLi Hong 
139541c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
139641c88bd7SLi Hong {
139741c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
139841c88bd7SLi Hong }
139941c88bd7SLi Hong 
140041c88bd7SLi Hong static void nilfs_destroy_cachep(void)
140141c88bd7SLi Hong {
14028c0a8537SKirill A. Shutemov 	/*
14038c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14048c0a8537SKirill A. Shutemov 	 * destroy cache.
14058c0a8537SKirill A. Shutemov 	 */
14068c0a8537SKirill A. Shutemov 	rcu_barrier();
14078c0a8537SKirill A. Shutemov 
140841c88bd7SLi Hong 	if (nilfs_inode_cachep)
140941c88bd7SLi Hong 		kmem_cache_destroy(nilfs_inode_cachep);
141041c88bd7SLi Hong 	if (nilfs_transaction_cachep)
141141c88bd7SLi Hong 		kmem_cache_destroy(nilfs_transaction_cachep);
141241c88bd7SLi Hong 	if (nilfs_segbuf_cachep)
141341c88bd7SLi Hong 		kmem_cache_destroy(nilfs_segbuf_cachep);
141441c88bd7SLi Hong 	if (nilfs_btree_path_cache)
141541c88bd7SLi Hong 		kmem_cache_destroy(nilfs_btree_path_cache);
141641c88bd7SLi Hong }
141741c88bd7SLi Hong 
141841c88bd7SLi Hong static int __init nilfs_init_cachep(void)
141941c88bd7SLi Hong {
142041c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
142141c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
142241c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once);
142341c88bd7SLi Hong 	if (!nilfs_inode_cachep)
142441c88bd7SLi Hong 		goto fail;
142541c88bd7SLi Hong 
142641c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
142741c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
142841c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
142941c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
143041c88bd7SLi Hong 		goto fail;
143141c88bd7SLi Hong 
143241c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
143341c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
143441c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
143541c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
143641c88bd7SLi Hong 		goto fail;
143741c88bd7SLi Hong 
143841c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
143941c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
144041c88bd7SLi Hong 			0, 0, NULL);
144141c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
144241c88bd7SLi Hong 		goto fail;
144341c88bd7SLi Hong 
144441c88bd7SLi Hong 	return 0;
144541c88bd7SLi Hong 
144641c88bd7SLi Hong fail:
144741c88bd7SLi Hong 	nilfs_destroy_cachep();
144841c88bd7SLi Hong 	return -ENOMEM;
144941c88bd7SLi Hong }
145041c88bd7SLi Hong 
1451783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1452783f6184SRyusuke Konishi {
1453783f6184SRyusuke Konishi 	int err;
1454783f6184SRyusuke Konishi 
145541c88bd7SLi Hong 	err = nilfs_init_cachep();
1456783f6184SRyusuke Konishi 	if (err)
145741c88bd7SLi Hong 		goto fail;
1458783f6184SRyusuke Konishi 
1459dd70edbdSVyacheslav Dubeyko 	err = nilfs_sysfs_init();
1460783f6184SRyusuke Konishi 	if (err)
146141c88bd7SLi Hong 		goto free_cachep;
1462783f6184SRyusuke Konishi 
1463dd70edbdSVyacheslav Dubeyko 	err = register_filesystem(&nilfs_fs_type);
1464dd70edbdSVyacheslav Dubeyko 	if (err)
1465dd70edbdSVyacheslav Dubeyko 		goto deinit_sysfs_entry;
1466dd70edbdSVyacheslav Dubeyko 
14679f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1468783f6184SRyusuke Konishi 	return 0;
1469783f6184SRyusuke Konishi 
1470dd70edbdSVyacheslav Dubeyko deinit_sysfs_entry:
1471dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
147241c88bd7SLi Hong free_cachep:
147341c88bd7SLi Hong 	nilfs_destroy_cachep();
147441c88bd7SLi Hong fail:
1475783f6184SRyusuke Konishi 	return err;
1476783f6184SRyusuke Konishi }
1477783f6184SRyusuke Konishi 
1478783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1479783f6184SRyusuke Konishi {
148041c88bd7SLi Hong 	nilfs_destroy_cachep();
1481dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
1482783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1483783f6184SRyusuke Konishi }
1484783f6184SRyusuke Konishi 
1485783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1486783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1487