xref: /openbmc/linux/fs/nilfs2/super.c (revision a66dfb0a91c211c77b5d4e503d3e760e2e566189)
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  *
164b420ab4SRyusuke Konishi  * Written by Ryusuke Konishi.
17783f6184SRyusuke Konishi  */
18783f6184SRyusuke Konishi /*
19783f6184SRyusuke Konishi  *  linux/fs/ext2/super.c
20783f6184SRyusuke Konishi  *
21783f6184SRyusuke Konishi  * Copyright (C) 1992, 1993, 1994, 1995
22783f6184SRyusuke Konishi  * Remy Card (card@masi.ibp.fr)
23783f6184SRyusuke Konishi  * Laboratoire MASI - Institut Blaise Pascal
24783f6184SRyusuke Konishi  * Universite Pierre et Marie Curie (Paris VI)
25783f6184SRyusuke Konishi  *
26783f6184SRyusuke Konishi  *  from
27783f6184SRyusuke Konishi  *
28783f6184SRyusuke Konishi  *  linux/fs/minix/inode.c
29783f6184SRyusuke Konishi  *
30783f6184SRyusuke Konishi  *  Copyright (C) 1991, 1992  Linus Torvalds
31783f6184SRyusuke Konishi  *
32783f6184SRyusuke Konishi  *  Big-endian to little-endian byte-swapping/bitmaps by
33783f6184SRyusuke Konishi  *        David S. Miller (davem@caip.rutgers.edu), 1995
34783f6184SRyusuke Konishi  */
35783f6184SRyusuke Konishi 
36783f6184SRyusuke Konishi #include <linux/module.h>
37783f6184SRyusuke Konishi #include <linux/string.h>
38783f6184SRyusuke Konishi #include <linux/slab.h>
39783f6184SRyusuke Konishi #include <linux/init.h>
40783f6184SRyusuke Konishi #include <linux/blkdev.h>
41783f6184SRyusuke Konishi #include <linux/parser.h>
42783f6184SRyusuke Konishi #include <linux/crc32.h>
43783f6184SRyusuke Konishi #include <linux/vfs.h>
44783f6184SRyusuke Konishi #include <linux/writeback.h>
45b58a285bSJiro SEKIBA #include <linux/seq_file.h>
46b58a285bSJiro SEKIBA #include <linux/mount.h>
47783f6184SRyusuke Konishi #include "nilfs.h"
488e656fd5SRyusuke Konishi #include "export.h"
49783f6184SRyusuke Konishi #include "mdt.h"
50783f6184SRyusuke Konishi #include "alloc.h"
5105d0e94bSRyusuke Konishi #include "btree.h"
5205d0e94bSRyusuke Konishi #include "btnode.h"
53783f6184SRyusuke Konishi #include "page.h"
54783f6184SRyusuke Konishi #include "cpfile.h"
554e33f9eaSRyusuke Konishi #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
56783f6184SRyusuke Konishi #include "ifile.h"
57783f6184SRyusuke Konishi #include "dat.h"
58783f6184SRyusuke Konishi #include "segment.h"
59783f6184SRyusuke Konishi #include "segbuf.h"
60783f6184SRyusuke Konishi 
61783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
62783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
63783f6184SRyusuke Konishi 		   "(NILFS)");
64783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
65783f6184SRyusuke Konishi 
66abc0b50bSJiro SEKIBA static struct kmem_cache *nilfs_inode_cachep;
6741c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
6841c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
6941c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
7041c88bd7SLi Hong 
71f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount);
72783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
73783f6184SRyusuke Konishi 
74*a66dfb0aSRyusuke Konishi void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt,
75*a66dfb0aSRyusuke Konishi 		 ...)
76*a66dfb0aSRyusuke Konishi {
77*a66dfb0aSRyusuke Konishi 	struct va_format vaf;
78*a66dfb0aSRyusuke Konishi 	va_list args;
79*a66dfb0aSRyusuke Konishi 
80*a66dfb0aSRyusuke Konishi 	va_start(args, fmt);
81*a66dfb0aSRyusuke Konishi 	vaf.fmt = fmt;
82*a66dfb0aSRyusuke Konishi 	vaf.va = &args;
83*a66dfb0aSRyusuke Konishi 	if (sb)
84*a66dfb0aSRyusuke Konishi 		printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf);
85*a66dfb0aSRyusuke Konishi 	else
86*a66dfb0aSRyusuke Konishi 		printk("%sNILFS: %pV\n", level, &vaf);
87*a66dfb0aSRyusuke Konishi 	va_end(args);
88*a66dfb0aSRyusuke Konishi }
89*a66dfb0aSRyusuke Konishi 
90f7545144SRyusuke Konishi static void nilfs_set_error(struct super_block *sb)
91c8a11c8aSRyusuke Konishi {
92e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
93d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
94c8a11c8aSRyusuke Konishi 
95c8a11c8aSRyusuke Konishi 	down_write(&nilfs->ns_sem);
96c8a11c8aSRyusuke Konishi 	if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
97c8a11c8aSRyusuke Konishi 		nilfs->ns_mount_state |= NILFS_ERROR_FS;
98f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, 0);
99d26493b6SJiro SEKIBA 		if (likely(sbp)) {
100d26493b6SJiro SEKIBA 			sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
101b2ac86e1SJiro SEKIBA 			if (sbp[1])
102b2ac86e1SJiro SEKIBA 				sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
103f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
104c8a11c8aSRyusuke Konishi 		}
105d26493b6SJiro SEKIBA 	}
106c8a11c8aSRyusuke Konishi 	up_write(&nilfs->ns_sem);
107c8a11c8aSRyusuke Konishi }
108c8a11c8aSRyusuke Konishi 
109783f6184SRyusuke Konishi /**
110cae3d4caSRyusuke Konishi  * __nilfs_error() - report failure condition on a filesystem
111783f6184SRyusuke Konishi  *
112cae3d4caSRyusuke Konishi  * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
113cae3d4caSRyusuke Konishi  * reporting an error message.  This function should be called when
114cae3d4caSRyusuke Konishi  * NILFS detects incoherences or defects of meta data on disk.
115783f6184SRyusuke Konishi  *
116cae3d4caSRyusuke Konishi  * This implements the body of nilfs_error() macro.  Normally,
117cae3d4caSRyusuke Konishi  * nilfs_error() should be used.  As for sustainable errors such as a
118cae3d4caSRyusuke Konishi  * single-shot I/O error, nilfs_warning() or printk() should be used
119cae3d4caSRyusuke Konishi  * instead.
120cae3d4caSRyusuke Konishi  *
121cae3d4caSRyusuke Konishi  * Callers should not add a trailing newline since this will do it.
122783f6184SRyusuke Konishi  */
123cae3d4caSRyusuke Konishi void __nilfs_error(struct super_block *sb, const char *function,
124783f6184SRyusuke Konishi 		   const char *fmt, ...)
125783f6184SRyusuke Konishi {
126e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
127b004a5ebSJoe Perches 	struct va_format vaf;
128783f6184SRyusuke Konishi 	va_list args;
129783f6184SRyusuke Konishi 
130783f6184SRyusuke Konishi 	va_start(args, fmt);
131b004a5ebSJoe Perches 
132b004a5ebSJoe Perches 	vaf.fmt = fmt;
133b004a5ebSJoe Perches 	vaf.va = &args;
134b004a5ebSJoe Perches 
135b004a5ebSJoe Perches 	printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
136b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
137b004a5ebSJoe Perches 
138783f6184SRyusuke Konishi 	va_end(args);
139783f6184SRyusuke Konishi 
140783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
141f7545144SRyusuke Konishi 		nilfs_set_error(sb);
142783f6184SRyusuke Konishi 
1433b2ce58bSRyusuke Konishi 		if (nilfs_test_opt(nilfs, ERRORS_RO)) {
144783f6184SRyusuke Konishi 			printk(KERN_CRIT "Remounting filesystem read-only\n");
145783f6184SRyusuke Konishi 			sb->s_flags |= MS_RDONLY;
146783f6184SRyusuke Konishi 		}
147783f6184SRyusuke Konishi 	}
148783f6184SRyusuke Konishi 
1493b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
150783f6184SRyusuke Konishi 		panic("NILFS (device %s): panic forced after error\n",
151783f6184SRyusuke Konishi 		      sb->s_id);
152783f6184SRyusuke Konishi }
153783f6184SRyusuke Konishi 
154783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function,
155783f6184SRyusuke Konishi 		   const char *fmt, ...)
156783f6184SRyusuke Konishi {
157b004a5ebSJoe Perches 	struct va_format vaf;
158783f6184SRyusuke Konishi 	va_list args;
159783f6184SRyusuke Konishi 
160783f6184SRyusuke Konishi 	va_start(args, fmt);
161b004a5ebSJoe Perches 
162b004a5ebSJoe Perches 	vaf.fmt = fmt;
163b004a5ebSJoe Perches 	vaf.va = &args;
164b004a5ebSJoe Perches 
165b004a5ebSJoe Perches 	printk(KERN_WARNING "NILFS warning (device %s): %s: %pV\n",
166b004a5ebSJoe Perches 	       sb->s_id, function, &vaf);
167b004a5ebSJoe Perches 
168783f6184SRyusuke Konishi 	va_end(args);
169783f6184SRyusuke Konishi }
170783f6184SRyusuke Konishi 
171783f6184SRyusuke Konishi 
1722879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
173783f6184SRyusuke Konishi {
174783f6184SRyusuke Konishi 	struct nilfs_inode_info *ii;
175783f6184SRyusuke Konishi 
176783f6184SRyusuke Konishi 	ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
177783f6184SRyusuke Konishi 	if (!ii)
178783f6184SRyusuke Konishi 		return NULL;
179783f6184SRyusuke Konishi 	ii->i_bh = NULL;
180783f6184SRyusuke Konishi 	ii->i_state = 0;
1810e14a359SRyusuke Konishi 	ii->i_cno = 0;
182783f6184SRyusuke Konishi 	ii->vfs_inode.i_version = 1;
183b83ae6d4SChristoph Hellwig 	nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
184783f6184SRyusuke Konishi 	return &ii->vfs_inode;
185783f6184SRyusuke Konishi }
186783f6184SRyusuke Konishi 
187fa0d7e3dSNick Piggin static void nilfs_i_callback(struct rcu_head *head)
188783f6184SRyusuke Konishi {
189fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
190b91c9a97SRyusuke Konishi 
1912d19961dSRyusuke Konishi 	if (nilfs_is_metadata_file_inode(inode))
1922d19961dSRyusuke Konishi 		nilfs_mdt_destroy(inode);
1932d19961dSRyusuke Konishi 
194783f6184SRyusuke Konishi 	kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
195783f6184SRyusuke Konishi }
196783f6184SRyusuke Konishi 
197fa0d7e3dSNick Piggin void nilfs_destroy_inode(struct inode *inode)
198fa0d7e3dSNick Piggin {
199fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, nilfs_i_callback);
200fa0d7e3dSNick Piggin }
201fa0d7e3dSNick Piggin 
202f7545144SRyusuke Konishi static int nilfs_sync_super(struct super_block *sb, int flag)
203783f6184SRyusuke Konishi {
204e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
205783f6184SRyusuke Konishi 	int err;
206783f6184SRyusuke Konishi 
207783f6184SRyusuke Konishi  retry:
208e339ad31SRyusuke Konishi 	set_buffer_dirty(nilfs->ns_sbh[0]);
2093b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, BARRIER)) {
21087e99511SChristoph Hellwig 		err = __sync_dirty_buffer(nilfs->ns_sbh[0],
211f8c131f5SChristoph Hellwig 					  WRITE_SYNC | WRITE_FLUSH_FUA);
21287e99511SChristoph Hellwig 	} else {
21387e99511SChristoph Hellwig 		err = sync_dirty_buffer(nilfs->ns_sbh[0]);
21487e99511SChristoph Hellwig 	}
21587e99511SChristoph Hellwig 
216e339ad31SRyusuke Konishi 	if (unlikely(err)) {
217783f6184SRyusuke Konishi 		printk(KERN_ERR
218783f6184SRyusuke Konishi 		       "NILFS: unable to write superblock (err=%d)\n", err);
219e339ad31SRyusuke Konishi 		if (err == -EIO && nilfs->ns_sbh[1]) {
220b2ac86e1SJiro SEKIBA 			/*
221b2ac86e1SJiro SEKIBA 			 * sbp[0] points to newer log than sbp[1],
222b2ac86e1SJiro SEKIBA 			 * so copy sbp[0] to sbp[1] to take over sbp[0].
223b2ac86e1SJiro SEKIBA 			 */
224b2ac86e1SJiro SEKIBA 			memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
225b2ac86e1SJiro SEKIBA 			       nilfs->ns_sbsize);
226e339ad31SRyusuke Konishi 			nilfs_fall_back_super_block(nilfs);
227e339ad31SRyusuke Konishi 			goto retry;
228e339ad31SRyusuke Konishi 		}
229e339ad31SRyusuke Konishi 	} else {
230e339ad31SRyusuke Konishi 		struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
231e339ad31SRyusuke Konishi 
232b2ac86e1SJiro SEKIBA 		nilfs->ns_sbwcount++;
233b2ac86e1SJiro SEKIBA 
234e339ad31SRyusuke Konishi 		/*
235e339ad31SRyusuke Konishi 		 * The latest segment becomes trailable from the position
236e339ad31SRyusuke Konishi 		 * written in superblock.
237e339ad31SRyusuke Konishi 		 */
238783f6184SRyusuke Konishi 		clear_nilfs_discontinued(nilfs);
239e339ad31SRyusuke Konishi 
240e339ad31SRyusuke Konishi 		/* update GC protection for recent segments */
241e339ad31SRyusuke Konishi 		if (nilfs->ns_sbh[1]) {
242b2ac86e1SJiro SEKIBA 			if (flag == NILFS_SB_COMMIT_ALL) {
243e339ad31SRyusuke Konishi 				set_buffer_dirty(nilfs->ns_sbh[1]);
244b2ac86e1SJiro SEKIBA 				if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
245b2ac86e1SJiro SEKIBA 					goto out;
246b2ac86e1SJiro SEKIBA 			}
247b2ac86e1SJiro SEKIBA 			if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
248b2ac86e1SJiro SEKIBA 			    le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
249e339ad31SRyusuke Konishi 				sbp = nilfs->ns_sbp[1];
250e339ad31SRyusuke Konishi 		}
251b2ac86e1SJiro SEKIBA 
2522c2e52fcSRyusuke Konishi 		spin_lock(&nilfs->ns_last_segment_lock);
253e339ad31SRyusuke Konishi 		nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2542c2e52fcSRyusuke Konishi 		spin_unlock(&nilfs->ns_last_segment_lock);
255783f6184SRyusuke Konishi 	}
256b2ac86e1SJiro SEKIBA  out:
257783f6184SRyusuke Konishi 	return err;
258783f6184SRyusuke Konishi }
259783f6184SRyusuke Konishi 
26060f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
26160f46b7eSRyusuke Konishi 			  struct the_nilfs *nilfs)
26260f46b7eSRyusuke Konishi {
26360f46b7eSRyusuke Konishi 	sector_t nfreeblocks;
26460f46b7eSRyusuke Konishi 
26560f46b7eSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
26660f46b7eSRyusuke Konishi 	nilfs_count_free_blocks(nilfs, &nfreeblocks);
26760f46b7eSRyusuke Konishi 	sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
26860f46b7eSRyusuke Konishi 
26960f46b7eSRyusuke Konishi 	spin_lock(&nilfs->ns_last_segment_lock);
27060f46b7eSRyusuke Konishi 	sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
27160f46b7eSRyusuke Konishi 	sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
27260f46b7eSRyusuke Konishi 	sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
27360f46b7eSRyusuke Konishi 	spin_unlock(&nilfs->ns_last_segment_lock);
27460f46b7eSRyusuke Konishi }
27560f46b7eSRyusuke Konishi 
276f7545144SRyusuke Konishi struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
277b2ac86e1SJiro SEKIBA 					       int flip)
278d26493b6SJiro SEKIBA {
279e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
280d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
281d26493b6SJiro SEKIBA 
282d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
283d26493b6SJiro SEKIBA 	if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
284d26493b6SJiro SEKIBA 		if (sbp[1] &&
285d26493b6SJiro SEKIBA 		    sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
286b2ac86e1SJiro SEKIBA 			memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
287d26493b6SJiro SEKIBA 		} else {
288d26493b6SJiro SEKIBA 			printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
289f7545144SRyusuke Konishi 			       sb->s_id);
290d26493b6SJiro SEKIBA 			return NULL;
291d26493b6SJiro SEKIBA 		}
292b2ac86e1SJiro SEKIBA 	} else if (sbp[1] &&
293b2ac86e1SJiro SEKIBA 		   sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
294b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
295d26493b6SJiro SEKIBA 	}
296b2ac86e1SJiro SEKIBA 
297b2ac86e1SJiro SEKIBA 	if (flip && sbp[1])
298b2ac86e1SJiro SEKIBA 		nilfs_swap_super_block(nilfs);
299b2ac86e1SJiro SEKIBA 
300d26493b6SJiro SEKIBA 	return sbp;
301d26493b6SJiro SEKIBA }
302d26493b6SJiro SEKIBA 
303f7545144SRyusuke Konishi int nilfs_commit_super(struct super_block *sb, int flag)
304783f6184SRyusuke Konishi {
305e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
306e339ad31SRyusuke Konishi 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
307e339ad31SRyusuke Konishi 	time_t t;
308783f6184SRyusuke Konishi 
309d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
310e339ad31SRyusuke Konishi 	t = get_seconds();
311b2ac86e1SJiro SEKIBA 	nilfs->ns_sbwtime = t;
312e339ad31SRyusuke Konishi 	sbp[0]->s_wtime = cpu_to_le64(t);
313e339ad31SRyusuke Konishi 	sbp[0]->s_sum = 0;
314e339ad31SRyusuke Konishi 	sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
315e339ad31SRyusuke Konishi 					     (unsigned char *)sbp[0],
316e339ad31SRyusuke Konishi 					     nilfs->ns_sbsize));
317b2ac86e1SJiro SEKIBA 	if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
318b2ac86e1SJiro SEKIBA 		sbp[1]->s_wtime = sbp[0]->s_wtime;
319b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = 0;
320b2ac86e1SJiro SEKIBA 		sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
321b2ac86e1SJiro SEKIBA 					    (unsigned char *)sbp[1],
322b2ac86e1SJiro SEKIBA 					    nilfs->ns_sbsize));
323e339ad31SRyusuke Konishi 	}
324e605f0a7SRyusuke Konishi 	clear_nilfs_sb_dirty(nilfs);
325e2c7617aSAndreas Rohner 	nilfs->ns_flushed_device = 1;
326e2c7617aSAndreas Rohner 	/* make sure store to ns_flushed_device cannot be reordered */
327e2c7617aSAndreas Rohner 	smp_wmb();
328f7545144SRyusuke Konishi 	return nilfs_sync_super(sb, flag);
329783f6184SRyusuke Konishi }
330783f6184SRyusuke Konishi 
3317ecaa46cSRyusuke Konishi /**
3327ecaa46cSRyusuke Konishi  * nilfs_cleanup_super() - write filesystem state for cleanup
333f7545144SRyusuke Konishi  * @sb: super block instance to be unmounted or degraded to read-only
3347ecaa46cSRyusuke Konishi  *
3357ecaa46cSRyusuke Konishi  * This function restores state flags in the on-disk super block.
3367ecaa46cSRyusuke Konishi  * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3377ecaa46cSRyusuke Konishi  * filesystem was not clean previously.
3387ecaa46cSRyusuke Konishi  */
339f7545144SRyusuke Konishi int nilfs_cleanup_super(struct super_block *sb)
3407ecaa46cSRyusuke Konishi {
341e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
342d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
343b2ac86e1SJiro SEKIBA 	int flag = NILFS_SB_COMMIT;
344d26493b6SJiro SEKIBA 	int ret = -EIO;
3457ecaa46cSRyusuke Konishi 
346f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
347d26493b6SJiro SEKIBA 	if (sbp) {
348f7545144SRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
349f7545144SRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
350b2ac86e1SJiro SEKIBA 		if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
351b2ac86e1SJiro SEKIBA 			/*
352b2ac86e1SJiro SEKIBA 			 * make the "clean" flag also to the opposite
353b2ac86e1SJiro SEKIBA 			 * super block if both super blocks point to
354b2ac86e1SJiro SEKIBA 			 * the same checkpoint.
355b2ac86e1SJiro SEKIBA 			 */
356b2ac86e1SJiro SEKIBA 			sbp[1]->s_state = sbp[0]->s_state;
357b2ac86e1SJiro SEKIBA 			flag = NILFS_SB_COMMIT_ALL;
358b2ac86e1SJiro SEKIBA 		}
359f7545144SRyusuke Konishi 		ret = nilfs_commit_super(sb, flag);
360d26493b6SJiro SEKIBA 	}
3617ecaa46cSRyusuke Konishi 	return ret;
3627ecaa46cSRyusuke Konishi }
3637ecaa46cSRyusuke Konishi 
364cfb0a4bfSRyusuke Konishi /**
365cfb0a4bfSRyusuke Konishi  * nilfs_move_2nd_super - relocate secondary super block
366cfb0a4bfSRyusuke Konishi  * @sb: super block instance
367cfb0a4bfSRyusuke Konishi  * @sb2off: new offset of the secondary super block (in bytes)
368cfb0a4bfSRyusuke Konishi  */
369cfb0a4bfSRyusuke Konishi static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
370cfb0a4bfSRyusuke Konishi {
371cfb0a4bfSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
372cfb0a4bfSRyusuke Konishi 	struct buffer_head *nsbh;
373cfb0a4bfSRyusuke Konishi 	struct nilfs_super_block *nsbp;
374cfb0a4bfSRyusuke Konishi 	sector_t blocknr, newblocknr;
375cfb0a4bfSRyusuke Konishi 	unsigned long offset;
3764f05028fSRyusuke Konishi 	int sb2i;  /* array index of the secondary superblock */
377cfb0a4bfSRyusuke Konishi 	int ret = 0;
378cfb0a4bfSRyusuke Konishi 
379cfb0a4bfSRyusuke Konishi 	/* nilfs->ns_sem must be locked by the caller. */
380cfb0a4bfSRyusuke Konishi 	if (nilfs->ns_sbh[1] &&
381cfb0a4bfSRyusuke Konishi 	    nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
382cfb0a4bfSRyusuke Konishi 		sb2i = 1;
383cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[1]->b_blocknr;
384cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
385cfb0a4bfSRyusuke Konishi 		sb2i = 0;
386cfb0a4bfSRyusuke Konishi 		blocknr = nilfs->ns_sbh[0]->b_blocknr;
3874f05028fSRyusuke Konishi 	} else {
3884f05028fSRyusuke Konishi 		sb2i = -1;
3894f05028fSRyusuke Konishi 		blocknr = 0;
390cfb0a4bfSRyusuke Konishi 	}
391cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
392cfb0a4bfSRyusuke Konishi 		goto out;  /* super block location is unchanged */
393cfb0a4bfSRyusuke Konishi 
394cfb0a4bfSRyusuke Konishi 	/* Get new super block buffer */
395cfb0a4bfSRyusuke Konishi 	newblocknr = sb2off >> nilfs->ns_blocksize_bits;
396cfb0a4bfSRyusuke Konishi 	offset = sb2off & (nilfs->ns_blocksize - 1);
397cfb0a4bfSRyusuke Konishi 	nsbh = sb_getblk(sb, newblocknr);
398cfb0a4bfSRyusuke Konishi 	if (!nsbh) {
399cfb0a4bfSRyusuke Konishi 		printk(KERN_WARNING
400cfb0a4bfSRyusuke Konishi 		       "NILFS warning: unable to move secondary superblock "
401cfb0a4bfSRyusuke Konishi 		       "to block %llu\n", (unsigned long long)newblocknr);
402cfb0a4bfSRyusuke Konishi 		ret = -EIO;
403cfb0a4bfSRyusuke Konishi 		goto out;
404cfb0a4bfSRyusuke Konishi 	}
405cfb0a4bfSRyusuke Konishi 	nsbp = (void *)nsbh->b_data + offset;
406cfb0a4bfSRyusuke Konishi 	memset(nsbp, 0, nilfs->ns_blocksize);
407cfb0a4bfSRyusuke Konishi 
408cfb0a4bfSRyusuke Konishi 	if (sb2i >= 0) {
409cfb0a4bfSRyusuke Konishi 		memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
410cfb0a4bfSRyusuke Konishi 		brelse(nilfs->ns_sbh[sb2i]);
411cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[sb2i] = nsbh;
412cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[sb2i] = nsbp;
413cfb0a4bfSRyusuke Konishi 	} else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
414cfb0a4bfSRyusuke Konishi 		/* secondary super block will be restored to index 1 */
415cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbh[1] = nsbh;
416cfb0a4bfSRyusuke Konishi 		nilfs->ns_sbp[1] = nsbp;
417cfb0a4bfSRyusuke Konishi 	} else {
418cfb0a4bfSRyusuke Konishi 		brelse(nsbh);
419cfb0a4bfSRyusuke Konishi 	}
420cfb0a4bfSRyusuke Konishi out:
421cfb0a4bfSRyusuke Konishi 	return ret;
422cfb0a4bfSRyusuke Konishi }
423cfb0a4bfSRyusuke Konishi 
4244e33f9eaSRyusuke Konishi /**
4254e33f9eaSRyusuke Konishi  * nilfs_resize_fs - resize the filesystem
4264e33f9eaSRyusuke Konishi  * @sb: super block instance
4274e33f9eaSRyusuke Konishi  * @newsize: new size of the filesystem (in bytes)
4284e33f9eaSRyusuke Konishi  */
4294e33f9eaSRyusuke Konishi int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
4304e33f9eaSRyusuke Konishi {
4314e33f9eaSRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
4324e33f9eaSRyusuke Konishi 	struct nilfs_super_block **sbp;
4334e33f9eaSRyusuke Konishi 	__u64 devsize, newnsegs;
4344e33f9eaSRyusuke Konishi 	loff_t sb2off;
4354e33f9eaSRyusuke Konishi 	int ret;
4364e33f9eaSRyusuke Konishi 
4374e33f9eaSRyusuke Konishi 	ret = -ERANGE;
4384e33f9eaSRyusuke Konishi 	devsize = i_size_read(sb->s_bdev->bd_inode);
4394e33f9eaSRyusuke Konishi 	if (newsize > devsize)
4404e33f9eaSRyusuke Konishi 		goto out;
4414e33f9eaSRyusuke Konishi 
4424e33f9eaSRyusuke Konishi 	/*
4434e33f9eaSRyusuke Konishi 	 * Write lock is required to protect some functions depending
4444e33f9eaSRyusuke Konishi 	 * on the number of segments, the number of reserved segments,
4454e33f9eaSRyusuke Konishi 	 * and so forth.
4464e33f9eaSRyusuke Konishi 	 */
4474e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_segctor_sem);
4484e33f9eaSRyusuke Konishi 
4494e33f9eaSRyusuke Konishi 	sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
4504e33f9eaSRyusuke Konishi 	newnsegs = sb2off >> nilfs->ns_blocksize_bits;
4514e33f9eaSRyusuke Konishi 	do_div(newnsegs, nilfs->ns_blocks_per_segment);
4524e33f9eaSRyusuke Konishi 
4534e33f9eaSRyusuke Konishi 	ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
4544e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_segctor_sem);
4554e33f9eaSRyusuke Konishi 	if (ret < 0)
4564e33f9eaSRyusuke Konishi 		goto out;
4574e33f9eaSRyusuke Konishi 
4584e33f9eaSRyusuke Konishi 	ret = nilfs_construct_segment(sb);
4594e33f9eaSRyusuke Konishi 	if (ret < 0)
4604e33f9eaSRyusuke Konishi 		goto out;
4614e33f9eaSRyusuke Konishi 
4624e33f9eaSRyusuke Konishi 	down_write(&nilfs->ns_sem);
4634e33f9eaSRyusuke Konishi 	nilfs_move_2nd_super(sb, sb2off);
4644e33f9eaSRyusuke Konishi 	ret = -EIO;
4654e33f9eaSRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
4664e33f9eaSRyusuke Konishi 	if (likely(sbp)) {
4674e33f9eaSRyusuke Konishi 		nilfs_set_log_cursor(sbp[0], nilfs);
4684e33f9eaSRyusuke Konishi 		/*
4694e33f9eaSRyusuke Konishi 		 * Drop NILFS_RESIZE_FS flag for compatibility with
4704e33f9eaSRyusuke Konishi 		 * mount-time resize which may be implemented in a
4714e33f9eaSRyusuke Konishi 		 * future release.
4724e33f9eaSRyusuke Konishi 		 */
4734e33f9eaSRyusuke Konishi 		sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
4744e33f9eaSRyusuke Konishi 					      ~NILFS_RESIZE_FS);
4754e33f9eaSRyusuke Konishi 		sbp[0]->s_dev_size = cpu_to_le64(newsize);
4764e33f9eaSRyusuke Konishi 		sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
4774e33f9eaSRyusuke Konishi 		if (sbp[1])
4784e33f9eaSRyusuke Konishi 			memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
4794e33f9eaSRyusuke Konishi 		ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
4804e33f9eaSRyusuke Konishi 	}
4814e33f9eaSRyusuke Konishi 	up_write(&nilfs->ns_sem);
4824e33f9eaSRyusuke Konishi 
4834e33f9eaSRyusuke Konishi 	/*
4844e33f9eaSRyusuke Konishi 	 * Reset the range of allocatable segments last.  This order
4854e33f9eaSRyusuke Konishi 	 * is important in the case of expansion because the secondary
4864e33f9eaSRyusuke Konishi 	 * superblock must be protected from log write until migration
4874e33f9eaSRyusuke Konishi 	 * completes.
4884e33f9eaSRyusuke Konishi 	 */
4894e33f9eaSRyusuke Konishi 	if (!ret)
4904e33f9eaSRyusuke Konishi 		nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
4914e33f9eaSRyusuke Konishi out:
4924e33f9eaSRyusuke Konishi 	return ret;
4934e33f9eaSRyusuke Konishi }
4944e33f9eaSRyusuke Konishi 
495783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
496783f6184SRyusuke Konishi {
497e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
498783f6184SRyusuke Konishi 
499f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
500783f6184SRyusuke Konishi 
501783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
502783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
503f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
504783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
505783f6184SRyusuke Konishi 	}
506783f6184SRyusuke Konishi 
507f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
508f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
509f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
510f1e89c86SRyusuke Konishi 
511348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
512783f6184SRyusuke Konishi 	sb->s_fs_info = NULL;
513783f6184SRyusuke Konishi }
514783f6184SRyusuke Konishi 
515783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
516783f6184SRyusuke Konishi {
517e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
518d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
519783f6184SRyusuke Konishi 	int err = 0;
520783f6184SRyusuke Konishi 
521783f6184SRyusuke Konishi 	/* This function is called when super block should be written back */
522783f6184SRyusuke Konishi 	if (wait)
523783f6184SRyusuke Konishi 		err = nilfs_construct_segment(sb);
5246233caa9SJiro SEKIBA 
5256233caa9SJiro SEKIBA 	down_write(&nilfs->ns_sem);
526d26493b6SJiro SEKIBA 	if (nilfs_sb_dirty(nilfs)) {
527f7545144SRyusuke Konishi 		sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
528b2ac86e1SJiro SEKIBA 		if (likely(sbp)) {
529b2ac86e1SJiro SEKIBA 			nilfs_set_log_cursor(sbp[0], nilfs);
530f7545144SRyusuke Konishi 			nilfs_commit_super(sb, NILFS_SB_COMMIT);
531b2ac86e1SJiro SEKIBA 		}
532d26493b6SJiro SEKIBA 	}
5336233caa9SJiro SEKIBA 	up_write(&nilfs->ns_sem);
5346233caa9SJiro SEKIBA 
535e2c7617aSAndreas Rohner 	if (!err)
536e2c7617aSAndreas Rohner 		err = nilfs_flush_device(nilfs);
537e2c7617aSAndreas Rohner 
538783f6184SRyusuke Konishi 	return err;
539783f6184SRyusuke Konishi }
540783f6184SRyusuke Konishi 
541f7545144SRyusuke Konishi int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
5424d8d9293SRyusuke Konishi 			    struct nilfs_root **rootp)
543783f6184SRyusuke Konishi {
544e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
5454d8d9293SRyusuke Konishi 	struct nilfs_root *root;
546783f6184SRyusuke Konishi 	struct nilfs_checkpoint *raw_cp;
547783f6184SRyusuke Konishi 	struct buffer_head *bh_cp;
5484d8d9293SRyusuke Konishi 	int err = -ENOMEM;
549783f6184SRyusuke Konishi 
5504d8d9293SRyusuke Konishi 	root = nilfs_find_or_create_root(
5514d8d9293SRyusuke Konishi 		nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
5524d8d9293SRyusuke Konishi 	if (!root)
5534d8d9293SRyusuke Konishi 		return err;
554783f6184SRyusuke Konishi 
555e912a5b6SRyusuke Konishi 	if (root->ifile)
556e912a5b6SRyusuke Konishi 		goto reuse; /* already attached checkpoint */
557783f6184SRyusuke Konishi 
5581154ecbdSZhang Qiang 	down_read(&nilfs->ns_segctor_sem);
559783f6184SRyusuke Konishi 	err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
560783f6184SRyusuke Konishi 					  &bh_cp);
5611154ecbdSZhang Qiang 	up_read(&nilfs->ns_segctor_sem);
562783f6184SRyusuke Konishi 	if (unlikely(err)) {
563783f6184SRyusuke Konishi 		if (err == -ENOENT || err == -EINVAL) {
564783f6184SRyusuke Konishi 			printk(KERN_ERR
565783f6184SRyusuke Konishi 			       "NILFS: Invalid checkpoint "
566783f6184SRyusuke Konishi 			       "(checkpoint number=%llu)\n",
567783f6184SRyusuke Konishi 			       (unsigned long long)cno);
568783f6184SRyusuke Konishi 			err = -EINVAL;
569783f6184SRyusuke Konishi 		}
570783f6184SRyusuke Konishi 		goto failed;
571783f6184SRyusuke Konishi 	}
572f1e89c86SRyusuke Konishi 
573f7545144SRyusuke Konishi 	err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
574f1e89c86SRyusuke Konishi 			       &raw_cp->cp_ifile_inode, &root->ifile);
575f1e89c86SRyusuke Konishi 	if (err)
576783f6184SRyusuke Konishi 		goto failed_bh;
577b7c06342SRyusuke Konishi 
578e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->inodes_count,
579e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_inodes_count));
580e5f7f848SVyacheslav Dubeyko 	atomic64_set(&root->blocks_count,
581e5f7f848SVyacheslav Dubeyko 			le64_to_cpu(raw_cp->cp_blocks_count));
582783f6184SRyusuke Konishi 
583783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
5844d8d9293SRyusuke Konishi 
585e912a5b6SRyusuke Konishi  reuse:
5864d8d9293SRyusuke Konishi 	*rootp = root;
587783f6184SRyusuke Konishi 	return 0;
588783f6184SRyusuke Konishi 
589783f6184SRyusuke Konishi  failed_bh:
590783f6184SRyusuke Konishi 	nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
591783f6184SRyusuke Konishi  failed:
5924d8d9293SRyusuke Konishi 	nilfs_put_root(root);
593783f6184SRyusuke Konishi 
594783f6184SRyusuke Konishi 	return err;
595783f6184SRyusuke Konishi }
596783f6184SRyusuke Konishi 
5975beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb)
598783f6184SRyusuke Konishi {
599e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
6005beb6e0bSRyusuke Konishi 	int err;
6015beb6e0bSRyusuke Konishi 
6025beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
6035beb6e0bSRyusuke Konishi 		return 0;
6045beb6e0bSRyusuke Konishi 
6055beb6e0bSRyusuke Konishi 	/* Mark super block clean */
6065beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
607f7545144SRyusuke Konishi 	err = nilfs_cleanup_super(sb);
6085beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
6095beb6e0bSRyusuke Konishi 	return err;
6105beb6e0bSRyusuke Konishi }
6115beb6e0bSRyusuke Konishi 
6125beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb)
6135beb6e0bSRyusuke Konishi {
614e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
615783f6184SRyusuke Konishi 
6165beb6e0bSRyusuke Konishi 	if (sb->s_flags & MS_RDONLY)
6175beb6e0bSRyusuke Konishi 		return 0;
6185beb6e0bSRyusuke Konishi 
6195beb6e0bSRyusuke Konishi 	down_write(&nilfs->ns_sem);
620f7545144SRyusuke Konishi 	nilfs_setup_super(sb, false);
6215beb6e0bSRyusuke Konishi 	up_write(&nilfs->ns_sem);
6225beb6e0bSRyusuke Konishi 	return 0;
623783f6184SRyusuke Konishi }
624783f6184SRyusuke Konishi 
625783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
626783f6184SRyusuke Konishi {
627783f6184SRyusuke Konishi 	struct super_block *sb = dentry->d_sb;
6282b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
629b7c06342SRyusuke Konishi 	struct the_nilfs *nilfs = root->nilfs;
630c306af23SRyusuke Konishi 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
631783f6184SRyusuke Konishi 	unsigned long long blocks;
632783f6184SRyusuke Konishi 	unsigned long overhead;
633783f6184SRyusuke Konishi 	unsigned long nrsvblocks;
634783f6184SRyusuke Konishi 	sector_t nfreeblocks;
635c7ef972cSVyacheslav Dubeyko 	u64 nmaxinodes, nfreeinodes;
636783f6184SRyusuke Konishi 	int err;
637783f6184SRyusuke Konishi 
638783f6184SRyusuke Konishi 	/*
639783f6184SRyusuke Konishi 	 * Compute all of the segment blocks
640783f6184SRyusuke Konishi 	 *
641783f6184SRyusuke Konishi 	 * The blocks before first segment and after last segment
642783f6184SRyusuke Konishi 	 * are excluded.
643783f6184SRyusuke Konishi 	 */
644783f6184SRyusuke Konishi 	blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
645783f6184SRyusuke Konishi 		- nilfs->ns_first_data_block;
646783f6184SRyusuke Konishi 	nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
647783f6184SRyusuke Konishi 
648783f6184SRyusuke Konishi 	/*
649783f6184SRyusuke Konishi 	 * Compute the overhead
650783f6184SRyusuke Konishi 	 *
6517a65004bSRyusuke Konishi 	 * When distributing meta data blocks outside segment structure,
652783f6184SRyusuke Konishi 	 * We must count them as the overhead.
653783f6184SRyusuke Konishi 	 */
654783f6184SRyusuke Konishi 	overhead = 0;
655783f6184SRyusuke Konishi 
656783f6184SRyusuke Konishi 	err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
657783f6184SRyusuke Konishi 	if (unlikely(err))
658783f6184SRyusuke Konishi 		return err;
659783f6184SRyusuke Konishi 
660c7ef972cSVyacheslav Dubeyko 	err = nilfs_ifile_count_free_inodes(root->ifile,
661c7ef972cSVyacheslav Dubeyko 					    &nmaxinodes, &nfreeinodes);
662c7ef972cSVyacheslav Dubeyko 	if (unlikely(err)) {
663c7ef972cSVyacheslav Dubeyko 		printk(KERN_WARNING
664c7ef972cSVyacheslav Dubeyko 			"NILFS warning: fail to count free inodes: err %d.\n",
665c7ef972cSVyacheslav Dubeyko 			err);
666c7ef972cSVyacheslav Dubeyko 		if (err == -ERANGE) {
667c7ef972cSVyacheslav Dubeyko 			/*
668c7ef972cSVyacheslav Dubeyko 			 * If nilfs_palloc_count_max_entries() returns
669c7ef972cSVyacheslav Dubeyko 			 * -ERANGE error code then we simply treat
670c7ef972cSVyacheslav Dubeyko 			 * curent inodes count as maximum possible and
671c7ef972cSVyacheslav Dubeyko 			 * zero as free inodes value.
672c7ef972cSVyacheslav Dubeyko 			 */
673e5f7f848SVyacheslav Dubeyko 			nmaxinodes = atomic64_read(&root->inodes_count);
674c7ef972cSVyacheslav Dubeyko 			nfreeinodes = 0;
675c7ef972cSVyacheslav Dubeyko 			err = 0;
676c7ef972cSVyacheslav Dubeyko 		} else
677c7ef972cSVyacheslav Dubeyko 			return err;
678c7ef972cSVyacheslav Dubeyko 	}
679c7ef972cSVyacheslav Dubeyko 
680783f6184SRyusuke Konishi 	buf->f_type = NILFS_SUPER_MAGIC;
681783f6184SRyusuke Konishi 	buf->f_bsize = sb->s_blocksize;
682783f6184SRyusuke Konishi 	buf->f_blocks = blocks - overhead;
683783f6184SRyusuke Konishi 	buf->f_bfree = nfreeblocks;
684783f6184SRyusuke Konishi 	buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
685783f6184SRyusuke Konishi 		(buf->f_bfree - nrsvblocks) : 0;
686c7ef972cSVyacheslav Dubeyko 	buf->f_files = nmaxinodes;
687c7ef972cSVyacheslav Dubeyko 	buf->f_ffree = nfreeinodes;
688783f6184SRyusuke Konishi 	buf->f_namelen = NILFS_NAME_LEN;
689c306af23SRyusuke Konishi 	buf->f_fsid.val[0] = (u32)id;
690c306af23SRyusuke Konishi 	buf->f_fsid.val[1] = (u32)(id >> 32);
691c306af23SRyusuke Konishi 
692783f6184SRyusuke Konishi 	return 0;
693783f6184SRyusuke Konishi }
694783f6184SRyusuke Konishi 
69534c80b1dSAl Viro static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
696b58a285bSJiro SEKIBA {
69734c80b1dSAl Viro 	struct super_block *sb = dentry->d_sb;
698e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
6992b0143b5SDavid Howells 	struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
700b58a285bSJiro SEKIBA 
7013b2ce58bSRyusuke Konishi 	if (!nilfs_test_opt(nilfs, BARRIER))
702c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",nobarrier");
703f11459adSRyusuke Konishi 	if (root->cno != NILFS_CPTREE_CURRENT_CNO)
704f11459adSRyusuke Konishi 		seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
7053b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_PANIC))
706c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=panic");
7073b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, ERRORS_CONT))
708c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",errors=continue");
7093b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, STRICT_ORDER))
710c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",order=strict");
7113b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, NORECOVERY))
712c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",norecovery");
7133b2ce58bSRyusuke Konishi 	if (nilfs_test_opt(nilfs, DISCARD))
714c6b4d57dSRyusuke Konishi 		seq_puts(seq, ",discard");
715b58a285bSJiro SEKIBA 
716b58a285bSJiro SEKIBA 	return 0;
717b58a285bSJiro SEKIBA }
718b58a285bSJiro SEKIBA 
719b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
720783f6184SRyusuke Konishi 	.alloc_inode    = nilfs_alloc_inode,
721783f6184SRyusuke Konishi 	.destroy_inode  = nilfs_destroy_inode,
722783f6184SRyusuke Konishi 	.dirty_inode    = nilfs_dirty_inode,
7236fd1e5c9SAl Viro 	.evict_inode    = nilfs_evict_inode,
724783f6184SRyusuke Konishi 	.put_super      = nilfs_put_super,
725783f6184SRyusuke Konishi 	.sync_fs        = nilfs_sync_fs,
7265beb6e0bSRyusuke Konishi 	.freeze_fs	= nilfs_freeze,
7275beb6e0bSRyusuke Konishi 	.unfreeze_fs	= nilfs_unfreeze,
728783f6184SRyusuke Konishi 	.statfs         = nilfs_statfs,
729783f6184SRyusuke Konishi 	.remount_fs     = nilfs_remount,
730b58a285bSJiro SEKIBA 	.show_options = nilfs_show_options
731783f6184SRyusuke Konishi };
732783f6184SRyusuke Konishi 
733783f6184SRyusuke Konishi enum {
734783f6184SRyusuke Konishi 	Opt_err_cont, Opt_err_panic, Opt_err_ro,
735773bc4f3SRyusuke Konishi 	Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
736802d3177SRyusuke Konishi 	Opt_discard, Opt_nodiscard, Opt_err,
737783f6184SRyusuke Konishi };
738783f6184SRyusuke Konishi 
739783f6184SRyusuke Konishi static match_table_t tokens = {
740783f6184SRyusuke Konishi 	{Opt_err_cont, "errors=continue"},
741783f6184SRyusuke Konishi 	{Opt_err_panic, "errors=panic"},
742783f6184SRyusuke Konishi 	{Opt_err_ro, "errors=remount-ro"},
743773bc4f3SRyusuke Konishi 	{Opt_barrier, "barrier"},
74491f1953bSJiro SEKIBA 	{Opt_nobarrier, "nobarrier"},
745783f6184SRyusuke Konishi 	{Opt_snapshot, "cp=%u"},
746783f6184SRyusuke Konishi 	{Opt_order, "order=%s"},
7470234576dSRyusuke Konishi 	{Opt_norecovery, "norecovery"},
748e902ec99SJiro SEKIBA 	{Opt_discard, "discard"},
749802d3177SRyusuke Konishi 	{Opt_nodiscard, "nodiscard"},
750783f6184SRyusuke Konishi 	{Opt_err, NULL}
751783f6184SRyusuke Konishi };
752783f6184SRyusuke Konishi 
7537c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
754783f6184SRyusuke Konishi {
755e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
756783f6184SRyusuke Konishi 	char *p;
757783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
758783f6184SRyusuke Konishi 
759783f6184SRyusuke Konishi 	if (!options)
760783f6184SRyusuke Konishi 		return 1;
761783f6184SRyusuke Konishi 
762783f6184SRyusuke Konishi 	while ((p = strsep(&options, ",")) != NULL) {
763783f6184SRyusuke Konishi 		int token;
7644ad364caSRyusuke Konishi 
765783f6184SRyusuke Konishi 		if (!*p)
766783f6184SRyusuke Konishi 			continue;
767783f6184SRyusuke Konishi 
768783f6184SRyusuke Konishi 		token = match_token(p, tokens, args);
769783f6184SRyusuke Konishi 		switch (token) {
770773bc4f3SRyusuke Konishi 		case Opt_barrier:
7713b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, BARRIER);
772773bc4f3SRyusuke Konishi 			break;
77391f1953bSJiro SEKIBA 		case Opt_nobarrier:
7743b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, BARRIER);
775783f6184SRyusuke Konishi 			break;
776783f6184SRyusuke Konishi 		case Opt_order:
777783f6184SRyusuke Konishi 			if (strcmp(args[0].from, "relaxed") == 0)
778783f6184SRyusuke Konishi 				/* Ordered data semantics */
7793b2ce58bSRyusuke Konishi 				nilfs_clear_opt(nilfs, STRICT_ORDER);
780783f6184SRyusuke Konishi 			else if (strcmp(args[0].from, "strict") == 0)
781783f6184SRyusuke Konishi 				/* Strict in-order semantics */
7823b2ce58bSRyusuke Konishi 				nilfs_set_opt(nilfs, STRICT_ORDER);
783783f6184SRyusuke Konishi 			else
784783f6184SRyusuke Konishi 				return 0;
785783f6184SRyusuke Konishi 			break;
786783f6184SRyusuke Konishi 		case Opt_err_panic:
7873b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
788783f6184SRyusuke Konishi 			break;
789783f6184SRyusuke Konishi 		case Opt_err_ro:
7903b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
791783f6184SRyusuke Konishi 			break;
792783f6184SRyusuke Konishi 		case Opt_err_cont:
7933b2ce58bSRyusuke Konishi 			nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
794783f6184SRyusuke Konishi 			break;
795783f6184SRyusuke Konishi 		case Opt_snapshot:
7967c017457SRyusuke Konishi 			if (is_remount) {
7977c017457SRyusuke Konishi 				printk(KERN_ERR
798f11459adSRyusuke Konishi 				       "NILFS: \"%s\" option is invalid "
799f11459adSRyusuke Konishi 				       "for remount.\n", p);
8007c017457SRyusuke Konishi 				return 0;
8017c017457SRyusuke Konishi 			}
8027c017457SRyusuke Konishi 			break;
8030234576dSRyusuke Konishi 		case Opt_norecovery:
8043b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, NORECOVERY);
8050234576dSRyusuke Konishi 			break;
806e902ec99SJiro SEKIBA 		case Opt_discard:
8073b2ce58bSRyusuke Konishi 			nilfs_set_opt(nilfs, DISCARD);
808e902ec99SJiro SEKIBA 			break;
809802d3177SRyusuke Konishi 		case Opt_nodiscard:
8103b2ce58bSRyusuke Konishi 			nilfs_clear_opt(nilfs, DISCARD);
811802d3177SRyusuke Konishi 			break;
812783f6184SRyusuke Konishi 		default:
813783f6184SRyusuke Konishi 			printk(KERN_ERR
814783f6184SRyusuke Konishi 			       "NILFS: Unrecognized mount option \"%s\"\n", p);
815783f6184SRyusuke Konishi 			return 0;
816783f6184SRyusuke Konishi 		}
817783f6184SRyusuke Konishi 	}
818783f6184SRyusuke Konishi 	return 1;
819783f6184SRyusuke Konishi }
820783f6184SRyusuke Konishi 
821783f6184SRyusuke Konishi static inline void
8223b2ce58bSRyusuke Konishi nilfs_set_default_options(struct super_block *sb,
823783f6184SRyusuke Konishi 			  struct nilfs_super_block *sbp)
824783f6184SRyusuke Konishi {
825e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
8263b2ce58bSRyusuke Konishi 
8273b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt =
828277a6a34SRyusuke Konishi 		NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
829783f6184SRyusuke Konishi }
830783f6184SRyusuke Konishi 
831f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount)
832783f6184SRyusuke Konishi {
833e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
834d26493b6SJiro SEKIBA 	struct nilfs_super_block **sbp;
835d26493b6SJiro SEKIBA 	int max_mnt_count;
836d26493b6SJiro SEKIBA 	int mnt_count;
837783f6184SRyusuke Konishi 
838d26493b6SJiro SEKIBA 	/* nilfs->ns_sem must be locked by the caller. */
839f7545144SRyusuke Konishi 	sbp = nilfs_prepare_super(sb, 0);
840d26493b6SJiro SEKIBA 	if (!sbp)
841d26493b6SJiro SEKIBA 		return -EIO;
842d26493b6SJiro SEKIBA 
8435beb6e0bSRyusuke Konishi 	if (!is_mount)
8445beb6e0bSRyusuke Konishi 		goto skip_mount_setup;
8455beb6e0bSRyusuke Konishi 
846d26493b6SJiro SEKIBA 	max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
847d26493b6SJiro SEKIBA 	mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
848d26493b6SJiro SEKIBA 
849f50a4c81SRyusuke Konishi 	if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
850783f6184SRyusuke Konishi 		printk(KERN_WARNING
851783f6184SRyusuke Konishi 		       "NILFS warning: mounting fs with errors\n");
852783f6184SRyusuke Konishi #if 0
853783f6184SRyusuke Konishi 	} else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
854783f6184SRyusuke Konishi 		printk(KERN_WARNING
855783f6184SRyusuke Konishi 		       "NILFS warning: maximal mount count reached\n");
856783f6184SRyusuke Konishi #endif
857783f6184SRyusuke Konishi 	}
858783f6184SRyusuke Konishi 	if (!max_mnt_count)
859d26493b6SJiro SEKIBA 		sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
860783f6184SRyusuke Konishi 
861d26493b6SJiro SEKIBA 	sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
8625beb6e0bSRyusuke Konishi 	sbp[0]->s_mtime = cpu_to_le64(get_seconds());
8635beb6e0bSRyusuke Konishi 
8645beb6e0bSRyusuke Konishi skip_mount_setup:
865d26493b6SJiro SEKIBA 	sbp[0]->s_state =
866d26493b6SJiro SEKIBA 		cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
867b2ac86e1SJiro SEKIBA 	/* synchronize sbp[1] with sbp[0] */
8680ca7a5b9SRyusuke Konishi 	if (sbp[1])
869b2ac86e1SJiro SEKIBA 		memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
870f7545144SRyusuke Konishi 	return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
871783f6184SRyusuke Konishi }
872783f6184SRyusuke Konishi 
873e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
874e339ad31SRyusuke Konishi 						 u64 pos, int blocksize,
875e339ad31SRyusuke Konishi 						 struct buffer_head **pbh)
876783f6184SRyusuke Konishi {
877e339ad31SRyusuke Konishi 	unsigned long long sb_index = pos;
878e339ad31SRyusuke Konishi 	unsigned long offset;
879783f6184SRyusuke Konishi 
880e339ad31SRyusuke Konishi 	offset = do_div(sb_index, blocksize);
881783f6184SRyusuke Konishi 	*pbh = sb_bread(sb, sb_index);
882e339ad31SRyusuke Konishi 	if (!*pbh)
883783f6184SRyusuke Konishi 		return NULL;
884783f6184SRyusuke Konishi 	return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
885783f6184SRyusuke Konishi }
886783f6184SRyusuke Konishi 
887783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
888783f6184SRyusuke Konishi 				 struct nilfs_super_block *sbp,
889783f6184SRyusuke Konishi 				 char *data)
890783f6184SRyusuke Konishi {
891e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
892783f6184SRyusuke Konishi 
893783f6184SRyusuke Konishi 	sb->s_magic = le16_to_cpu(sbp->s_magic);
894783f6184SRyusuke Konishi 
895783f6184SRyusuke Konishi 	/* FS independent flags */
896783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
897783f6184SRyusuke Konishi 	sb->s_flags |= MS_NOATIME;
898783f6184SRyusuke Konishi #endif
899783f6184SRyusuke Konishi 
9003b2ce58bSRyusuke Konishi 	nilfs_set_default_options(sb, sbp);
901783f6184SRyusuke Konishi 
902574e6c31SRyusuke Konishi 	nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
903574e6c31SRyusuke Konishi 	nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
904574e6c31SRyusuke Konishi 	nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
905574e6c31SRyusuke Konishi 	nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
906783f6184SRyusuke Konishi 
9077c017457SRyusuke Konishi 	return !parse_options(data, sb, 0) ? -EINVAL : 0;
908783f6184SRyusuke Konishi }
909783f6184SRyusuke Konishi 
910c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
911c5ca48aaSRyusuke Konishi 				      struct nilfs_super_block *sbp)
912c5ca48aaSRyusuke Konishi {
913c5ca48aaSRyusuke Konishi 	__u64 features;
914c5ca48aaSRyusuke Konishi 
915c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_incompat) &
916c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_INCOMPAT_SUPP;
917c5ca48aaSRyusuke Konishi 	if (features) {
918c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
919c5ca48aaSRyusuke Konishi 		       "optional features (%llx)\n",
920c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
921c5ca48aaSRyusuke Konishi 		return -EINVAL;
922c5ca48aaSRyusuke Konishi 	}
923c5ca48aaSRyusuke Konishi 	features = le64_to_cpu(sbp->s_feature_compat_ro) &
924c5ca48aaSRyusuke Konishi 		~NILFS_FEATURE_COMPAT_RO_SUPP;
925c5ca48aaSRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY) && features) {
926c5ca48aaSRyusuke Konishi 		printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
927c5ca48aaSRyusuke Konishi 		       "unsupported optional features (%llx)\n",
928c5ca48aaSRyusuke Konishi 		       (unsigned long long)features);
929c5ca48aaSRyusuke Konishi 		return -EINVAL;
930c5ca48aaSRyusuke Konishi 	}
931c5ca48aaSRyusuke Konishi 	return 0;
932c5ca48aaSRyusuke Konishi }
933c5ca48aaSRyusuke Konishi 
934367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
935367ea334SRyusuke Konishi 				 struct nilfs_root *root,
936367ea334SRyusuke Konishi 				 struct dentry **root_dentry)
937367ea334SRyusuke Konishi {
938367ea334SRyusuke Konishi 	struct inode *inode;
939367ea334SRyusuke Konishi 	struct dentry *dentry;
940367ea334SRyusuke Konishi 	int ret = 0;
941367ea334SRyusuke Konishi 
942367ea334SRyusuke Konishi 	inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
943367ea334SRyusuke Konishi 	if (IS_ERR(inode)) {
944367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: get root inode failed\n");
945367ea334SRyusuke Konishi 		ret = PTR_ERR(inode);
946367ea334SRyusuke Konishi 		goto out;
947367ea334SRyusuke Konishi 	}
948367ea334SRyusuke Konishi 	if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
949367ea334SRyusuke Konishi 		iput(inode);
950367ea334SRyusuke Konishi 		printk(KERN_ERR "NILFS: corrupt root inode.\n");
951367ea334SRyusuke Konishi 		ret = -EINVAL;
952367ea334SRyusuke Konishi 		goto out;
953367ea334SRyusuke Konishi 	}
954367ea334SRyusuke Konishi 
955f11459adSRyusuke Konishi 	if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
956f11459adSRyusuke Konishi 		dentry = d_find_alias(inode);
957f11459adSRyusuke Konishi 		if (!dentry) {
95848fde701SAl Viro 			dentry = d_make_root(inode);
959367ea334SRyusuke Konishi 			if (!dentry) {
960367ea334SRyusuke Konishi 				ret = -ENOMEM;
961f11459adSRyusuke Konishi 				goto failed_dentry;
962f11459adSRyusuke Konishi 			}
963f11459adSRyusuke Konishi 		} else {
964f11459adSRyusuke Konishi 			iput(inode);
965f11459adSRyusuke Konishi 		}
966f11459adSRyusuke Konishi 	} else {
9671a0a397eSJ. Bruce Fields 		dentry = d_obtain_root(inode);
968f11459adSRyusuke Konishi 		if (IS_ERR(dentry)) {
969f11459adSRyusuke Konishi 			ret = PTR_ERR(dentry);
970f11459adSRyusuke Konishi 			goto failed_dentry;
971f11459adSRyusuke Konishi 		}
972367ea334SRyusuke Konishi 	}
973367ea334SRyusuke Konishi 	*root_dentry = dentry;
974367ea334SRyusuke Konishi  out:
975367ea334SRyusuke Konishi 	return ret;
976f11459adSRyusuke Konishi 
977f11459adSRyusuke Konishi  failed_dentry:
978f11459adSRyusuke Konishi 	printk(KERN_ERR "NILFS: get root dentry failed\n");
979f11459adSRyusuke Konishi 	goto out;
980367ea334SRyusuke Konishi }
981367ea334SRyusuke Konishi 
982ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
983ab4d8f7eSRyusuke Konishi 				 struct dentry **root_dentry)
984ab4d8f7eSRyusuke Konishi {
985e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = s->s_fs_info;
986ab4d8f7eSRyusuke Konishi 	struct nilfs_root *root;
987ab4d8f7eSRyusuke Konishi 	int ret;
988ab4d8f7eSRyusuke Konishi 
989572d8b39SRyusuke Konishi 	mutex_lock(&nilfs->ns_snapshot_mount_mutex);
990572d8b39SRyusuke Konishi 
991ab4d8f7eSRyusuke Konishi 	down_read(&nilfs->ns_segctor_sem);
992ab4d8f7eSRyusuke Konishi 	ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
993ab4d8f7eSRyusuke Konishi 	up_read(&nilfs->ns_segctor_sem);
994ab4d8f7eSRyusuke Konishi 	if (ret < 0) {
995ab4d8f7eSRyusuke Konishi 		ret = (ret == -ENOENT) ? -EINVAL : ret;
996ab4d8f7eSRyusuke Konishi 		goto out;
997ab4d8f7eSRyusuke Konishi 	} else if (!ret) {
998ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: The specified checkpoint is "
999ab4d8f7eSRyusuke Konishi 		       "not a snapshot (checkpoint number=%llu).\n",
1000ab4d8f7eSRyusuke Konishi 		       (unsigned long long)cno);
1001ab4d8f7eSRyusuke Konishi 		ret = -EINVAL;
1002ab4d8f7eSRyusuke Konishi 		goto out;
1003ab4d8f7eSRyusuke Konishi 	}
1004ab4d8f7eSRyusuke Konishi 
1005f7545144SRyusuke Konishi 	ret = nilfs_attach_checkpoint(s, cno, false, &root);
1006ab4d8f7eSRyusuke Konishi 	if (ret) {
1007ab4d8f7eSRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading snapshot "
1008ab4d8f7eSRyusuke Konishi 		       "(checkpoint number=%llu).\n",
1009ab4d8f7eSRyusuke Konishi 	       (unsigned long long)cno);
1010ab4d8f7eSRyusuke Konishi 		goto out;
1011ab4d8f7eSRyusuke Konishi 	}
1012ab4d8f7eSRyusuke Konishi 	ret = nilfs_get_root_dentry(s, root, root_dentry);
1013ab4d8f7eSRyusuke Konishi 	nilfs_put_root(root);
1014ab4d8f7eSRyusuke Konishi  out:
1015572d8b39SRyusuke Konishi 	mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
1016ab4d8f7eSRyusuke Konishi 	return ret;
1017ab4d8f7eSRyusuke Konishi }
1018ab4d8f7eSRyusuke Konishi 
1019f11459adSRyusuke Konishi /**
1020e95c311eSAl Viro  * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
1021f11459adSRyusuke Konishi  * @root_dentry: root dentry of the tree to be shrunk
1022f11459adSRyusuke Konishi  *
1023f11459adSRyusuke Konishi  * This function returns true if the tree was in-use.
1024f11459adSRyusuke Konishi  */
1025e95c311eSAl Viro static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1026f11459adSRyusuke Konishi {
1027f11459adSRyusuke Konishi 	shrink_dcache_parent(root_dentry);
1028e95c311eSAl Viro 	return d_count(root_dentry) > 1;
1029f11459adSRyusuke Konishi }
1030f11459adSRyusuke Konishi 
1031032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1032032dbb3bSRyusuke Konishi {
1033e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1034032dbb3bSRyusuke Konishi 	struct nilfs_root *root;
1035032dbb3bSRyusuke Konishi 	struct inode *inode;
1036032dbb3bSRyusuke Konishi 	struct dentry *dentry;
1037032dbb3bSRyusuke Konishi 	int ret;
1038032dbb3bSRyusuke Konishi 
10393377f843SRyusuke Konishi 	if (cno > nilfs->ns_cno)
1040032dbb3bSRyusuke Konishi 		return false;
1041032dbb3bSRyusuke Konishi 
1042032dbb3bSRyusuke Konishi 	if (cno >= nilfs_last_cno(nilfs))
1043032dbb3bSRyusuke Konishi 		return true;	/* protect recent checkpoints */
1044032dbb3bSRyusuke Konishi 
1045032dbb3bSRyusuke Konishi 	ret = false;
1046e3154e97SRyusuke Konishi 	root = nilfs_lookup_root(nilfs, cno);
1047032dbb3bSRyusuke Konishi 	if (root) {
1048032dbb3bSRyusuke Konishi 		inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1049032dbb3bSRyusuke Konishi 		if (inode) {
1050032dbb3bSRyusuke Konishi 			dentry = d_find_alias(inode);
1051032dbb3bSRyusuke Konishi 			if (dentry) {
1052e95c311eSAl Viro 				ret = nilfs_tree_is_busy(dentry);
1053032dbb3bSRyusuke Konishi 				dput(dentry);
1054032dbb3bSRyusuke Konishi 			}
1055032dbb3bSRyusuke Konishi 			iput(inode);
1056032dbb3bSRyusuke Konishi 		}
1057032dbb3bSRyusuke Konishi 		nilfs_put_root(root);
1058032dbb3bSRyusuke Konishi 	}
1059032dbb3bSRyusuke Konishi 	return ret;
1060032dbb3bSRyusuke Konishi }
1061032dbb3bSRyusuke Konishi 
1062783f6184SRyusuke Konishi /**
1063783f6184SRyusuke Konishi  * nilfs_fill_super() - initialize a super block instance
1064783f6184SRyusuke Konishi  * @sb: super_block
1065783f6184SRyusuke Konishi  * @data: mount options
1066783f6184SRyusuke Konishi  * @silent: silent mode flag
1067783f6184SRyusuke Konishi  *
1068aa7dfb89SRyusuke Konishi  * This function is called exclusively by nilfs->ns_mount_mutex.
1069783f6184SRyusuke Konishi  * So, the recovery process is protected from other simultaneous mounts.
1070783f6184SRyusuke Konishi  */
1071783f6184SRyusuke Konishi static int
1072348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent)
1073783f6184SRyusuke Konishi {
1074348fe8daSRyusuke Konishi 	struct the_nilfs *nilfs;
10754d8d9293SRyusuke Konishi 	struct nilfs_root *fsroot;
1076783f6184SRyusuke Konishi 	__u64 cno;
1077783f6184SRyusuke Konishi 	int err;
1078783f6184SRyusuke Konishi 
1079e3154e97SRyusuke Konishi 	nilfs = alloc_nilfs(sb->s_bdev);
1080e3154e97SRyusuke Konishi 	if (!nilfs)
1081783f6184SRyusuke Konishi 		return -ENOMEM;
1082783f6184SRyusuke Konishi 
1083e3154e97SRyusuke Konishi 	sb->s_fs_info = nilfs;
1084783f6184SRyusuke Konishi 
1085f7545144SRyusuke Konishi 	err = init_nilfs(nilfs, sb, (char *)data);
1086783f6184SRyusuke Konishi 	if (err)
1087348fe8daSRyusuke Konishi 		goto failed_nilfs;
1088783f6184SRyusuke Konishi 
1089783f6184SRyusuke Konishi 	sb->s_op = &nilfs_sops;
1090783f6184SRyusuke Konishi 	sb->s_export_op = &nilfs_export_ops;
1091783f6184SRyusuke Konishi 	sb->s_root = NULL;
109261239230SRyusuke Konishi 	sb->s_time_gran = 1;
10938de52778SAl Viro 	sb->s_max_links = NILFS_LINK_MAX;
1094026a7d63SRyusuke Konishi 
109526ff1304SChristoph Hellwig 	sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
1096783f6184SRyusuke Konishi 
1097f7545144SRyusuke Konishi 	err = load_nilfs(nilfs, sb);
1098783f6184SRyusuke Konishi 	if (err)
1099348fe8daSRyusuke Konishi 		goto failed_nilfs;
1100f50a4c81SRyusuke Konishi 
1101783f6184SRyusuke Konishi 	cno = nilfs_last_cno(nilfs);
1102f7545144SRyusuke Konishi 	err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1103783f6184SRyusuke Konishi 	if (err) {
1104f11459adSRyusuke Konishi 		printk(KERN_ERR "NILFS: error loading last checkpoint "
1105783f6184SRyusuke Konishi 		       "(checkpoint number=%llu).\n", (unsigned long long)cno);
1106f1e89c86SRyusuke Konishi 		goto failed_unload;
1107783f6184SRyusuke Konishi 	}
1108783f6184SRyusuke Konishi 
1109783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1110f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, fsroot);
1111783f6184SRyusuke Konishi 		if (err)
1112783f6184SRyusuke Konishi 			goto failed_checkpoint;
1113783f6184SRyusuke Konishi 	}
1114783f6184SRyusuke Konishi 
1115367ea334SRyusuke Konishi 	err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1116367ea334SRyusuke Konishi 	if (err)
1117783f6184SRyusuke Konishi 		goto failed_segctor;
1118783f6184SRyusuke Konishi 
11194d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1120783f6184SRyusuke Konishi 
1121783f6184SRyusuke Konishi 	if (!(sb->s_flags & MS_RDONLY)) {
1122783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1123f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1124783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1125783f6184SRyusuke Konishi 	}
1126783f6184SRyusuke Konishi 
1127783f6184SRyusuke Konishi 	return 0;
1128783f6184SRyusuke Konishi 
1129783f6184SRyusuke Konishi  failed_segctor:
1130f7545144SRyusuke Konishi 	nilfs_detach_log_writer(sb);
1131783f6184SRyusuke Konishi 
1132783f6184SRyusuke Konishi  failed_checkpoint:
11334d8d9293SRyusuke Konishi 	nilfs_put_root(fsroot);
1134783f6184SRyusuke Konishi 
1135f1e89c86SRyusuke Konishi  failed_unload:
1136f1e89c86SRyusuke Konishi 	iput(nilfs->ns_sufile);
1137f1e89c86SRyusuke Konishi 	iput(nilfs->ns_cpfile);
1138f1e89c86SRyusuke Konishi 	iput(nilfs->ns_dat);
1139f1e89c86SRyusuke Konishi 
1140348fe8daSRyusuke Konishi  failed_nilfs:
1141348fe8daSRyusuke Konishi 	destroy_nilfs(nilfs);
1142783f6184SRyusuke Konishi 	return err;
1143783f6184SRyusuke Konishi }
1144783f6184SRyusuke Konishi 
1145783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1146783f6184SRyusuke Konishi {
1147e3154e97SRyusuke Konishi 	struct the_nilfs *nilfs = sb->s_fs_info;
1148783f6184SRyusuke Konishi 	unsigned long old_sb_flags;
114906df0f99SRyusuke Konishi 	unsigned long old_mount_opt;
1150f11459adSRyusuke Konishi 	int err;
1151783f6184SRyusuke Konishi 
115202b9984dSTheodore Ts'o 	sync_filesystem(sb);
1153783f6184SRyusuke Konishi 	old_sb_flags = sb->s_flags;
11543b2ce58bSRyusuke Konishi 	old_mount_opt = nilfs->ns_mount_opt;
1155783f6184SRyusuke Konishi 
11567c017457SRyusuke Konishi 	if (!parse_options(data, sb, 1)) {
1157783f6184SRyusuke Konishi 		err = -EINVAL;
1158783f6184SRyusuke Konishi 		goto restore_opts;
1159783f6184SRyusuke Konishi 	}
1160783f6184SRyusuke Konishi 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1161783f6184SRyusuke Konishi 
1162d240e067SRyusuke Konishi 	err = -EINVAL;
1163783f6184SRyusuke Konishi 
11640234576dSRyusuke Konishi 	if (!nilfs_valid_fs(nilfs)) {
11650234576dSRyusuke Konishi 		printk(KERN_WARNING "NILFS (device %s): couldn't "
11660234576dSRyusuke Konishi 		       "remount because the filesystem is in an "
11670234576dSRyusuke Konishi 		       "incomplete recovery state.\n", sb->s_id);
11680234576dSRyusuke Konishi 		goto restore_opts;
11690234576dSRyusuke Konishi 	}
11700234576dSRyusuke Konishi 
1171783f6184SRyusuke Konishi 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1172783f6184SRyusuke Konishi 		goto out;
1173783f6184SRyusuke Konishi 	if (*flags & MS_RDONLY) {
1174f7545144SRyusuke Konishi 		/* Shutting down log writer */
1175f7545144SRyusuke Konishi 		nilfs_detach_log_writer(sb);
1176783f6184SRyusuke Konishi 		sb->s_flags |= MS_RDONLY;
1177783f6184SRyusuke Konishi 
1178783f6184SRyusuke Konishi 		/*
1179783f6184SRyusuke Konishi 		 * Remounting a valid RW partition RDONLY, so set
1180783f6184SRyusuke Konishi 		 * the RDONLY flag and then mark the partition as valid again.
1181783f6184SRyusuke Konishi 		 */
1182783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1183f7545144SRyusuke Konishi 		nilfs_cleanup_super(sb);
1184783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1185783f6184SRyusuke Konishi 	} else {
1186c5ca48aaSRyusuke Konishi 		__u64 features;
1187e912a5b6SRyusuke Konishi 		struct nilfs_root *root;
1188c5ca48aaSRyusuke Konishi 
1189783f6184SRyusuke Konishi 		/*
1190783f6184SRyusuke Konishi 		 * Mounting a RDONLY partition read-write, so reread and
1191783f6184SRyusuke Konishi 		 * store the current valid flag.  (It may have been changed
1192783f6184SRyusuke Konishi 		 * by fsck since we originally mounted the partition.)
1193783f6184SRyusuke Konishi 		 */
1194c5ca48aaSRyusuke Konishi 		down_read(&nilfs->ns_sem);
1195c5ca48aaSRyusuke Konishi 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1196c5ca48aaSRyusuke Konishi 			~NILFS_FEATURE_COMPAT_RO_SUPP;
1197c5ca48aaSRyusuke Konishi 		up_read(&nilfs->ns_sem);
1198c5ca48aaSRyusuke Konishi 		if (features) {
1199c5ca48aaSRyusuke Konishi 			printk(KERN_WARNING "NILFS (device %s): couldn't "
1200c5ca48aaSRyusuke Konishi 			       "remount RDWR because of unsupported optional "
1201c5ca48aaSRyusuke Konishi 			       "features (%llx)\n",
1202c5ca48aaSRyusuke Konishi 			       sb->s_id, (unsigned long long)features);
1203c5ca48aaSRyusuke Konishi 			err = -EROFS;
1204c5ca48aaSRyusuke Konishi 			goto restore_opts;
1205c5ca48aaSRyusuke Konishi 		}
1206c5ca48aaSRyusuke Konishi 
1207783f6184SRyusuke Konishi 		sb->s_flags &= ~MS_RDONLY;
1208783f6184SRyusuke Konishi 
12092b0143b5SDavid Howells 		root = NILFS_I(d_inode(sb->s_root))->i_root;
1210f7545144SRyusuke Konishi 		err = nilfs_attach_log_writer(sb, root);
1211783f6184SRyusuke Konishi 		if (err)
1212e59399d0SRyusuke Konishi 			goto restore_opts;
1213783f6184SRyusuke Konishi 
1214783f6184SRyusuke Konishi 		down_write(&nilfs->ns_sem);
1215f7545144SRyusuke Konishi 		nilfs_setup_super(sb, true);
1216783f6184SRyusuke Konishi 		up_write(&nilfs->ns_sem);
1217783f6184SRyusuke Konishi 	}
1218783f6184SRyusuke Konishi  out:
1219783f6184SRyusuke Konishi 	return 0;
1220783f6184SRyusuke Konishi 
1221783f6184SRyusuke Konishi  restore_opts:
1222783f6184SRyusuke Konishi 	sb->s_flags = old_sb_flags;
12233b2ce58bSRyusuke Konishi 	nilfs->ns_mount_opt = old_mount_opt;
1224783f6184SRyusuke Konishi 	return err;
1225783f6184SRyusuke Konishi }
1226783f6184SRyusuke Konishi 
1227783f6184SRyusuke Konishi struct nilfs_super_data {
1228783f6184SRyusuke Konishi 	struct block_device *bdev;
1229783f6184SRyusuke Konishi 	__u64 cno;
1230783f6184SRyusuke Konishi 	int flags;
1231783f6184SRyusuke Konishi };
1232783f6184SRyusuke Konishi 
1233783f6184SRyusuke Konishi /**
1234783f6184SRyusuke Konishi  * nilfs_identify - pre-read mount options needed to identify mount instance
1235783f6184SRyusuke Konishi  * @data: mount options
1236783f6184SRyusuke Konishi  * @sd: nilfs_super_data
1237783f6184SRyusuke Konishi  */
1238783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1239783f6184SRyusuke Konishi {
1240783f6184SRyusuke Konishi 	char *p, *options = data;
1241783f6184SRyusuke Konishi 	substring_t args[MAX_OPT_ARGS];
1242c05dbfc2SRyusuke Konishi 	int token;
1243783f6184SRyusuke Konishi 	int ret = 0;
1244783f6184SRyusuke Konishi 
1245783f6184SRyusuke Konishi 	do {
1246783f6184SRyusuke Konishi 		p = strsep(&options, ",");
1247783f6184SRyusuke Konishi 		if (p != NULL && *p) {
1248783f6184SRyusuke Konishi 			token = match_token(p, tokens, args);
1249783f6184SRyusuke Konishi 			if (token == Opt_snapshot) {
1250c05dbfc2SRyusuke Konishi 				if (!(sd->flags & MS_RDONLY)) {
1251783f6184SRyusuke Konishi 					ret++;
1252c05dbfc2SRyusuke Konishi 				} else {
1253c05dbfc2SRyusuke Konishi 					sd->cno = simple_strtoull(args[0].from,
1254c05dbfc2SRyusuke Konishi 								  NULL, 0);
1255c05dbfc2SRyusuke Konishi 					/*
1256c05dbfc2SRyusuke Konishi 					 * No need to see the end pointer;
1257c05dbfc2SRyusuke Konishi 					 * match_token() has done syntax
1258c05dbfc2SRyusuke Konishi 					 * checking.
1259c05dbfc2SRyusuke Konishi 					 */
1260c05dbfc2SRyusuke Konishi 					if (sd->cno == 0)
1261783f6184SRyusuke Konishi 						ret++;
1262783f6184SRyusuke Konishi 				}
1263783f6184SRyusuke Konishi 			}
1264783f6184SRyusuke Konishi 			if (ret)
1265783f6184SRyusuke Konishi 				printk(KERN_ERR
1266783f6184SRyusuke Konishi 				       "NILFS: invalid mount option: %s\n", p);
1267783f6184SRyusuke Konishi 		}
1268783f6184SRyusuke Konishi 		if (!options)
1269783f6184SRyusuke Konishi 			break;
1270783f6184SRyusuke Konishi 		BUG_ON(options == data);
1271783f6184SRyusuke Konishi 		*(options - 1) = ',';
1272783f6184SRyusuke Konishi 	} while (!ret);
1273783f6184SRyusuke Konishi 	return ret;
1274783f6184SRyusuke Konishi }
1275783f6184SRyusuke Konishi 
1276783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1277783f6184SRyusuke Konishi {
1278f11459adSRyusuke Konishi 	s->s_bdev = data;
1279783f6184SRyusuke Konishi 	s->s_dev = s->s_bdev->bd_dev;
1280783f6184SRyusuke Konishi 	return 0;
1281783f6184SRyusuke Konishi }
1282783f6184SRyusuke Konishi 
1283783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1284783f6184SRyusuke Konishi {
1285f11459adSRyusuke Konishi 	return (void *)s->s_bdev == data;
1286783f6184SRyusuke Konishi }
1287783f6184SRyusuke Konishi 
1288e4c59d61SAl Viro static struct dentry *
1289e4c59d61SAl Viro nilfs_mount(struct file_system_type *fs_type, int flags,
1290e4c59d61SAl Viro 	     const char *dev_name, void *data)
1291783f6184SRyusuke Konishi {
1292783f6184SRyusuke Konishi 	struct nilfs_super_data sd;
129333c8e57cSRyusuke Konishi 	struct super_block *s;
1294d4d77629STejun Heo 	fmode_t mode = FMODE_READ | FMODE_EXCL;
1295f11459adSRyusuke Konishi 	struct dentry *root_dentry;
1296f11459adSRyusuke Konishi 	int err, s_new = false;
1297783f6184SRyusuke Konishi 
129813e90559SRyusuke Konishi 	if (!(flags & MS_RDONLY))
129913e90559SRyusuke Konishi 		mode |= FMODE_WRITE;
130013e90559SRyusuke Konishi 
1301d4d77629STejun Heo 	sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1302d6d4c19cSJan Blunck 	if (IS_ERR(sd.bdev))
1303e4c59d61SAl Viro 		return ERR_CAST(sd.bdev);
1304783f6184SRyusuke Konishi 
1305783f6184SRyusuke Konishi 	sd.cno = 0;
1306783f6184SRyusuke Konishi 	sd.flags = flags;
1307783f6184SRyusuke Konishi 	if (nilfs_identify((char *)data, &sd)) {
1308783f6184SRyusuke Konishi 		err = -EINVAL;
1309783f6184SRyusuke Konishi 		goto failed;
1310783f6184SRyusuke Konishi 	}
1311783f6184SRyusuke Konishi 
13125beb6e0bSRyusuke Konishi 	/*
13135beb6e0bSRyusuke Konishi 	 * once the super is inserted into the list by sget, s_umount
13145beb6e0bSRyusuke Konishi 	 * will protect the lockfs code from trying to start a snapshot
13155beb6e0bSRyusuke Konishi 	 * while we are mounting
13165beb6e0bSRyusuke Konishi 	 */
13175beb6e0bSRyusuke Konishi 	mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
13185beb6e0bSRyusuke Konishi 	if (sd.bdev->bd_fsfreeze_count > 0) {
13195beb6e0bSRyusuke Konishi 		mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
13205beb6e0bSRyusuke Konishi 		err = -EBUSY;
132133c8e57cSRyusuke Konishi 		goto failed;
132233c8e57cSRyusuke Konishi 	}
13239249e17fSDavid Howells 	s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
13249249e17fSDavid Howells 		 sd.bdev);
13255beb6e0bSRyusuke Konishi 	mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
132633c8e57cSRyusuke Konishi 	if (IS_ERR(s)) {
132733c8e57cSRyusuke Konishi 		err = PTR_ERR(s);
1328348fe8daSRyusuke Konishi 		goto failed;
1329783f6184SRyusuke Konishi 	}
1330783f6184SRyusuke Konishi 
1331783f6184SRyusuke Konishi 	if (!s->s_root) {
1332f11459adSRyusuke Konishi 		s_new = true;
1333f11459adSRyusuke Konishi 
133433c8e57cSRyusuke Konishi 		/* New superblock instance created */
13354571b82cSRyusuke Konishi 		s->s_mode = mode;
1336a1c6f057SDmitry Monakhov 		snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
1337783f6184SRyusuke Konishi 		sb_set_blocksize(s, block_size(sd.bdev));
1338783f6184SRyusuke Konishi 
1339348fe8daSRyusuke Konishi 		err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
1340783f6184SRyusuke Konishi 		if (err)
1341348fe8daSRyusuke Konishi 			goto failed_super;
1342783f6184SRyusuke Konishi 
1343783f6184SRyusuke Konishi 		s->s_flags |= MS_ACTIVE;
1344f11459adSRyusuke Konishi 	} else if (!sd.cno) {
1345e95c311eSAl Viro 		if (nilfs_tree_is_busy(s->s_root)) {
1346e95c311eSAl Viro 			if ((flags ^ s->s_flags) & MS_RDONLY) {
1347f11459adSRyusuke Konishi 				printk(KERN_ERR "NILFS: the device already "
1348f11459adSRyusuke Konishi 				       "has a %s mount.\n",
1349f11459adSRyusuke Konishi 				       (s->s_flags & MS_RDONLY) ?
1350f11459adSRyusuke Konishi 				       "read-only" : "read/write");
1351f11459adSRyusuke Konishi 				err = -EBUSY;
1352f11459adSRyusuke Konishi 				goto failed_super;
1353f11459adSRyusuke Konishi 			}
1354e95c311eSAl Viro 		} else {
1355f11459adSRyusuke Konishi 			/*
1356f11459adSRyusuke Konishi 			 * Try remount to setup mount states if the current
1357f11459adSRyusuke Konishi 			 * tree is not mounted and only snapshots use this sb.
1358f11459adSRyusuke Konishi 			 */
1359f11459adSRyusuke Konishi 			err = nilfs_remount(s, &flags, data);
1360f11459adSRyusuke Konishi 			if (err)
1361f11459adSRyusuke Konishi 				goto failed_super;
1362f11459adSRyusuke Konishi 		}
1363783f6184SRyusuke Konishi 	}
1364783f6184SRyusuke Konishi 
1365f11459adSRyusuke Konishi 	if (sd.cno) {
1366f11459adSRyusuke Konishi 		err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1367348fe8daSRyusuke Konishi 		if (err)
1368f11459adSRyusuke Konishi 			goto failed_super;
1369f11459adSRyusuke Konishi 	} else {
1370f11459adSRyusuke Konishi 		root_dentry = dget(s->s_root);
1371783f6184SRyusuke Konishi 	}
1372783f6184SRyusuke Konishi 
1373f11459adSRyusuke Konishi 	if (!s_new)
1374d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1375f11459adSRyusuke Konishi 
1376e4c59d61SAl Viro 	return root_dentry;
1377783f6184SRyusuke Konishi 
1378f11459adSRyusuke Konishi  failed_super:
1379a95161aaSAl Viro 	deactivate_locked_super(s);
1380783f6184SRyusuke Konishi 
1381348fe8daSRyusuke Konishi  failed:
1382348fe8daSRyusuke Konishi 	if (!s_new)
1383d4d77629STejun Heo 		blkdev_put(sd.bdev, mode);
1384e4c59d61SAl Viro 	return ERR_PTR(err);
1385783f6184SRyusuke Konishi }
1386783f6184SRyusuke Konishi 
1387783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1388783f6184SRyusuke Konishi 	.owner    = THIS_MODULE,
1389783f6184SRyusuke Konishi 	.name     = "nilfs2",
1390e4c59d61SAl Viro 	.mount    = nilfs_mount,
1391783f6184SRyusuke Konishi 	.kill_sb  = kill_block_super,
1392783f6184SRyusuke Konishi 	.fs_flags = FS_REQUIRES_DEV,
1393783f6184SRyusuke Konishi };
13947f78e035SEric W. Biederman MODULE_ALIAS_FS("nilfs2");
1395783f6184SRyusuke Konishi 
139641c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
139741c88bd7SLi Hong {
139841c88bd7SLi Hong 	struct nilfs_inode_info *ii = obj;
139941c88bd7SLi Hong 
140041c88bd7SLi Hong 	INIT_LIST_HEAD(&ii->i_dirty);
140141c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
140241c88bd7SLi Hong 	init_rwsem(&ii->xattr_sem);
140341c88bd7SLi Hong #endif
14042aa15890SMiklos Szeredi 	address_space_init_once(&ii->i_btnode_cache);
140505d0e94bSRyusuke Konishi 	ii->i_bmap = &ii->i_bmap_data;
140641c88bd7SLi Hong 	inode_init_once(&ii->vfs_inode);
140741c88bd7SLi Hong }
140841c88bd7SLi Hong 
140941c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
141041c88bd7SLi Hong {
141141c88bd7SLi Hong 	memset(obj, 0, sizeof(struct nilfs_segment_buffer));
141241c88bd7SLi Hong }
141341c88bd7SLi Hong 
141441c88bd7SLi Hong static void nilfs_destroy_cachep(void)
141541c88bd7SLi Hong {
14168c0a8537SKirill A. Shutemov 	/*
14178c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
14188c0a8537SKirill A. Shutemov 	 * destroy cache.
14198c0a8537SKirill A. Shutemov 	 */
14208c0a8537SKirill A. Shutemov 	rcu_barrier();
14218c0a8537SKirill A. Shutemov 
142241c88bd7SLi Hong 	kmem_cache_destroy(nilfs_inode_cachep);
142341c88bd7SLi Hong 	kmem_cache_destroy(nilfs_transaction_cachep);
142441c88bd7SLi Hong 	kmem_cache_destroy(nilfs_segbuf_cachep);
142541c88bd7SLi Hong 	kmem_cache_destroy(nilfs_btree_path_cache);
142641c88bd7SLi Hong }
142741c88bd7SLi Hong 
142841c88bd7SLi Hong static int __init nilfs_init_cachep(void)
142941c88bd7SLi Hong {
143041c88bd7SLi Hong 	nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
143141c88bd7SLi Hong 			sizeof(struct nilfs_inode_info), 0,
14325d097056SVladimir Davydov 			SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
14335d097056SVladimir Davydov 			nilfs_inode_init_once);
143441c88bd7SLi Hong 	if (!nilfs_inode_cachep)
143541c88bd7SLi Hong 		goto fail;
143641c88bd7SLi Hong 
143741c88bd7SLi Hong 	nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
143841c88bd7SLi Hong 			sizeof(struct nilfs_transaction_info), 0,
143941c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, NULL);
144041c88bd7SLi Hong 	if (!nilfs_transaction_cachep)
144141c88bd7SLi Hong 		goto fail;
144241c88bd7SLi Hong 
144341c88bd7SLi Hong 	nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
144441c88bd7SLi Hong 			sizeof(struct nilfs_segment_buffer), 0,
144541c88bd7SLi Hong 			SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
144641c88bd7SLi Hong 	if (!nilfs_segbuf_cachep)
144741c88bd7SLi Hong 		goto fail;
144841c88bd7SLi Hong 
144941c88bd7SLi Hong 	nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
145041c88bd7SLi Hong 			sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
145141c88bd7SLi Hong 			0, 0, NULL);
145241c88bd7SLi Hong 	if (!nilfs_btree_path_cache)
145341c88bd7SLi Hong 		goto fail;
145441c88bd7SLi Hong 
145541c88bd7SLi Hong 	return 0;
145641c88bd7SLi Hong 
145741c88bd7SLi Hong fail:
145841c88bd7SLi Hong 	nilfs_destroy_cachep();
145941c88bd7SLi Hong 	return -ENOMEM;
146041c88bd7SLi Hong }
146141c88bd7SLi Hong 
1462783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1463783f6184SRyusuke Konishi {
1464783f6184SRyusuke Konishi 	int err;
1465783f6184SRyusuke Konishi 
146641c88bd7SLi Hong 	err = nilfs_init_cachep();
1467783f6184SRyusuke Konishi 	if (err)
146841c88bd7SLi Hong 		goto fail;
1469783f6184SRyusuke Konishi 
1470dd70edbdSVyacheslav Dubeyko 	err = nilfs_sysfs_init();
1471783f6184SRyusuke Konishi 	if (err)
147241c88bd7SLi Hong 		goto free_cachep;
1473783f6184SRyusuke Konishi 
1474dd70edbdSVyacheslav Dubeyko 	err = register_filesystem(&nilfs_fs_type);
1475dd70edbdSVyacheslav Dubeyko 	if (err)
1476dd70edbdSVyacheslav Dubeyko 		goto deinit_sysfs_entry;
1477dd70edbdSVyacheslav Dubeyko 
14789f130263SLi Hong 	printk(KERN_INFO "NILFS version 2 loaded\n");
1479783f6184SRyusuke Konishi 	return 0;
1480783f6184SRyusuke Konishi 
1481dd70edbdSVyacheslav Dubeyko deinit_sysfs_entry:
1482dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
148341c88bd7SLi Hong free_cachep:
148441c88bd7SLi Hong 	nilfs_destroy_cachep();
148541c88bd7SLi Hong fail:
1486783f6184SRyusuke Konishi 	return err;
1487783f6184SRyusuke Konishi }
1488783f6184SRyusuke Konishi 
1489783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1490783f6184SRyusuke Konishi {
149141c88bd7SLi Hong 	nilfs_destroy_cachep();
1492dd70edbdSVyacheslav Dubeyko 	nilfs_sysfs_exit();
1493783f6184SRyusuke Konishi 	unregister_filesystem(&nilfs_fs_type);
1494783f6184SRyusuke Konishi }
1495783f6184SRyusuke Konishi 
1496783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1497783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1498