blob: 86e3c00994e2a7459068559efa00f8e09247745a [file] [log] [blame]
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001/*
2 * super.c - NILFS module and super block management.
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Ryusuke Konishi.
Ryusuke Konishi783f6182009-04-06 19:01:35 -070017 */
18/*
19 * linux/fs/ext2/super.c
20 *
21 * Copyright (C) 1992, 1993, 1994, 1995
22 * Remy Card (card@masi.ibp.fr)
23 * Laboratoire MASI - Institut Blaise Pascal
24 * Universite Pierre et Marie Curie (Paris VI)
25 *
26 * from
27 *
28 * linux/fs/minix/inode.c
29 *
30 * Copyright (C) 1991, 1992 Linus Torvalds
31 *
32 * Big-endian to little-endian byte-swapping/bitmaps by
33 * David S. Miller (davem@caip.rutgers.edu), 1995
34 */
35
36#include <linux/module.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/init.h>
40#include <linux/blkdev.h>
41#include <linux/parser.h>
Ryusuke Konishi783f6182009-04-06 19:01:35 -070042#include <linux/crc32.h>
Ryusuke Konishi783f6182009-04-06 19:01:35 -070043#include <linux/vfs.h>
44#include <linux/writeback.h>
Jiro SEKIBAb58a2852009-06-24 20:06:34 +090045#include <linux/seq_file.h>
46#include <linux/mount.h>
Ryusuke Konishi783f6182009-04-06 19:01:35 -070047#include "nilfs.h"
Ryusuke Konishi8e656fd2010-08-27 00:23:02 +090048#include "export.h"
Ryusuke Konishi783f6182009-04-06 19:01:35 -070049#include "mdt.h"
50#include "alloc.h"
Ryusuke Konishi05d0e942010-07-10 20:52:09 +090051#include "btree.h"
52#include "btnode.h"
Ryusuke Konishi783f6182009-04-06 19:01:35 -070053#include "page.h"
54#include "cpfile.h"
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +090055#include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
Ryusuke Konishi783f6182009-04-06 19:01:35 -070056#include "ifile.h"
57#include "dat.h"
58#include "segment.h"
59#include "segbuf.h"
60
61MODULE_AUTHOR("NTT Corp.");
62MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
63 "(NILFS)");
Ryusuke Konishi783f6182009-04-06 19:01:35 -070064MODULE_LICENSE("GPL");
65
Jiro SEKIBAabc0b502010-10-08 22:37:27 +090066static struct kmem_cache *nilfs_inode_cachep;
Li Hong41c88bd2010-04-06 00:54:11 +080067struct kmem_cache *nilfs_transaction_cachep;
68struct kmem_cache *nilfs_segbuf_cachep;
69struct kmem_cache *nilfs_btree_path_cache;
70
Ryusuke Konishif7545142011-03-09 11:05:08 +090071static int nilfs_setup_super(struct super_block *sb, int is_mount);
Ryusuke Konishi783f6182009-04-06 19:01:35 -070072static int nilfs_remount(struct super_block *sb, int *flags, char *data);
Ryusuke Konishi783f6182009-04-06 19:01:35 -070073
Ryusuke Konishia66dfb02016-08-02 14:05:02 -070074void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt,
75 ...)
76{
77 struct va_format vaf;
78 va_list args;
79
80 va_start(args, fmt);
81 vaf.fmt = fmt;
82 vaf.va = &args;
83 if (sb)
84 printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf);
85 else
86 printk("%sNILFS: %pV\n", level, &vaf);
87 va_end(args);
88}
89
Ryusuke Konishif7545142011-03-09 11:05:08 +090090static void nilfs_set_error(struct super_block *sb)
Ryusuke Konishic8a11c82010-06-28 17:49:30 +090091{
Ryusuke Konishie3154e92011-03-09 11:05:08 +090092 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +090093 struct nilfs_super_block **sbp;
Ryusuke Konishic8a11c82010-06-28 17:49:30 +090094
95 down_write(&nilfs->ns_sem);
96 if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
97 nilfs->ns_mount_state |= NILFS_ERROR_FS;
Ryusuke Konishif7545142011-03-09 11:05:08 +090098 sbp = nilfs_prepare_super(sb, 0);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +090099 if (likely(sbp)) {
100 sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900101 if (sbp[1])
102 sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900103 nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900104 }
Ryusuke Konishic8a11c82010-06-28 17:49:30 +0900105 }
106 up_write(&nilfs->ns_sem);
107}
108
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700109/**
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700110 * __nilfs_error() - report failure condition on a filesystem
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700111 *
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700112 * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
113 * reporting an error message. This function should be called when
114 * NILFS detects incoherences or defects of meta data on disk.
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700115 *
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700116 * This implements the body of nilfs_error() macro. Normally,
117 * nilfs_error() should be used. As for sustainable errors such as a
118 * single-shot I/O error, nilfs_warning() or printk() should be used
119 * instead.
120 *
121 * Callers should not add a trailing newline since this will do it.
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700122 */
Ryusuke Konishicae3d4c2016-08-02 14:05:00 -0700123void __nilfs_error(struct super_block *sb, const char *function,
124 const char *fmt, ...)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700125{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900126 struct the_nilfs *nilfs = sb->s_fs_info;
Joe Perchesb004a5e2010-11-09 16:35:21 -0800127 struct va_format vaf;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700128 va_list args;
129
130 va_start(args, fmt);
Joe Perchesb004a5e2010-11-09 16:35:21 -0800131
132 vaf.fmt = fmt;
133 vaf.va = &args;
134
135 printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
136 sb->s_id, function, &vaf);
137
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700138 va_end(args);
139
140 if (!(sb->s_flags & MS_RDONLY)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900141 nilfs_set_error(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700142
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900143 if (nilfs_test_opt(nilfs, ERRORS_RO)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700144 printk(KERN_CRIT "Remounting filesystem read-only\n");
145 sb->s_flags |= MS_RDONLY;
146 }
147 }
148
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900149 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700150 panic("NILFS (device %s): panic forced after error\n",
151 sb->s_id);
152}
153
154void nilfs_warning(struct super_block *sb, const char *function,
155 const char *fmt, ...)
156{
Joe Perchesb004a5e2010-11-09 16:35:21 -0800157 struct va_format vaf;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700158 va_list args;
159
160 va_start(args, fmt);
Joe Perchesb004a5e2010-11-09 16:35:21 -0800161
162 vaf.fmt = fmt;
163 vaf.va = &args;
164
165 printk(KERN_WARNING "NILFS warning (device %s): %s: %pV\n",
166 sb->s_id, function, &vaf);
167
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700168 va_end(args);
169}
170
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700171
Ryusuke Konishi2879ed62010-09-05 13:35:53 +0900172struct inode *nilfs_alloc_inode(struct super_block *sb)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700173{
174 struct nilfs_inode_info *ii;
175
176 ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS);
177 if (!ii)
178 return NULL;
179 ii->i_bh = NULL;
180 ii->i_state = 0;
Ryusuke Konishi0e14a352010-08-20 21:20:29 +0900181 ii->i_cno = 0;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700182 ii->vfs_inode.i_version = 1;
Christoph Hellwigb83ae6d2015-01-14 10:42:37 +0100183 nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700184 return &ii->vfs_inode;
185}
186
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100187static void nilfs_i_callback(struct rcu_head *head)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700188{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100189 struct inode *inode = container_of(head, struct inode, i_rcu);
Ryusuke Konishib91c9a92010-08-20 23:46:06 +0900190
Ryusuke Konishi2d199612016-05-23 16:23:20 -0700191 if (nilfs_is_metadata_file_inode(inode))
192 nilfs_mdt_destroy(inode);
193
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700194 kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
195}
196
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100197void nilfs_destroy_inode(struct inode *inode)
198{
199 call_rcu(&inode->i_rcu, nilfs_i_callback);
200}
201
Ryusuke Konishif7545142011-03-09 11:05:08 +0900202static int nilfs_sync_super(struct super_block *sb, int flag)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700203{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900204 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700205 int err;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700206
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700207 retry:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700208 set_buffer_dirty(nilfs->ns_sbh[0]);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900209 if (nilfs_test_opt(nilfs, BARRIER)) {
Christoph Hellwig87e99512010-08-11 17:05:45 +0200210 err = __sync_dirty_buffer(nilfs->ns_sbh[0],
Christoph Hellwigf8c131f2010-08-18 05:29:15 -0400211 WRITE_SYNC | WRITE_FLUSH_FUA);
Christoph Hellwig87e99512010-08-11 17:05:45 +0200212 } else {
213 err = sync_dirty_buffer(nilfs->ns_sbh[0]);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700214 }
Christoph Hellwig87e99512010-08-11 17:05:45 +0200215
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700216 if (unlikely(err)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700217 printk(KERN_ERR
218 "NILFS: unable to write superblock (err=%d)\n", err);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700219 if (err == -EIO && nilfs->ns_sbh[1]) {
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900220 /*
221 * sbp[0] points to newer log than sbp[1],
222 * so copy sbp[0] to sbp[1] to take over sbp[0].
223 */
224 memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
225 nilfs->ns_sbsize);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700226 nilfs_fall_back_super_block(nilfs);
227 goto retry;
228 }
229 } else {
230 struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
231
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900232 nilfs->ns_sbwcount++;
233
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700234 /*
235 * The latest segment becomes trailable from the position
236 * written in superblock.
237 */
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700238 clear_nilfs_discontinued(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700239
240 /* update GC protection for recent segments */
241 if (nilfs->ns_sbh[1]) {
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900242 if (flag == NILFS_SB_COMMIT_ALL) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700243 set_buffer_dirty(nilfs->ns_sbh[1]);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900244 if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
245 goto out;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700246 }
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900247 if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
248 le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
249 sbp = nilfs->ns_sbp[1];
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700250 }
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700251
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900252 spin_lock(&nilfs->ns_last_segment_lock);
253 nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
254 spin_unlock(&nilfs->ns_last_segment_lock);
255 }
256 out:
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700257 return err;
258}
259
Ryusuke Konishi60f46b72010-06-28 17:49:31 +0900260void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
261 struct the_nilfs *nilfs)
262{
263 sector_t nfreeblocks;
264
265 /* nilfs->ns_sem must be locked by the caller. */
266 nilfs_count_free_blocks(nilfs, &nfreeblocks);
267 sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
268
269 spin_lock(&nilfs->ns_last_segment_lock);
270 sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
271 sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
272 sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
273 spin_unlock(&nilfs->ns_last_segment_lock);
274}
275
Ryusuke Konishif7545142011-03-09 11:05:08 +0900276struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900277 int flip)
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900278{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900279 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900280 struct nilfs_super_block **sbp = nilfs->ns_sbp;
281
282 /* nilfs->ns_sem must be locked by the caller. */
283 if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
284 if (sbp[1] &&
285 sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900286 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900287 } else {
288 printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
Ryusuke Konishif7545142011-03-09 11:05:08 +0900289 sb->s_id);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900290 return NULL;
291 }
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900292 } else if (sbp[1] &&
293 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
Ryusuke Konishi7592ecd2016-05-23 16:23:36 -0700294 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900295 }
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900296
297 if (flip && sbp[1])
298 nilfs_swap_super_block(nilfs);
299
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900300 return sbp;
301}
302
Ryusuke Konishif7545142011-03-09 11:05:08 +0900303int nilfs_commit_super(struct super_block *sb, int flag)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700304{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900305 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700306 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700307 time_t t;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700308
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900309 /* nilfs->ns_sem must be locked by the caller. */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700310 t = get_seconds();
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900311 nilfs->ns_sbwtime = t;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700312 sbp[0]->s_wtime = cpu_to_le64(t);
313 sbp[0]->s_sum = 0;
314 sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
315 (unsigned char *)sbp[0],
316 nilfs->ns_sbsize));
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900317 if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
318 sbp[1]->s_wtime = sbp[0]->s_wtime;
319 sbp[1]->s_sum = 0;
320 sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
321 (unsigned char *)sbp[1],
322 nilfs->ns_sbsize));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700323 }
Ryusuke Konishie605f0a2009-12-09 00:57:52 +0900324 clear_nilfs_sb_dirty(nilfs);
Andreas Rohnere2c76172014-10-13 15:53:20 -0700325 nilfs->ns_flushed_device = 1;
326 /* make sure store to ns_flushed_device cannot be reordered */
327 smp_wmb();
Ryusuke Konishif7545142011-03-09 11:05:08 +0900328 return nilfs_sync_super(sb, flag);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700329}
330
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900331/**
332 * nilfs_cleanup_super() - write filesystem state for cleanup
Ryusuke Konishif7545142011-03-09 11:05:08 +0900333 * @sb: super block instance to be unmounted or degraded to read-only
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900334 *
335 * This function restores state flags in the on-disk super block.
336 * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
337 * filesystem was not clean previously.
338 */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900339int nilfs_cleanup_super(struct super_block *sb)
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900340{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900341 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900342 struct nilfs_super_block **sbp;
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900343 int flag = NILFS_SB_COMMIT;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900344 int ret = -EIO;
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900345
Ryusuke Konishif7545142011-03-09 11:05:08 +0900346 sbp = nilfs_prepare_super(sb, 0);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900347 if (sbp) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900348 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
349 nilfs_set_log_cursor(sbp[0], nilfs);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900350 if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
351 /*
352 * make the "clean" flag also to the opposite
353 * super block if both super blocks point to
354 * the same checkpoint.
355 */
356 sbp[1]->s_state = sbp[0]->s_state;
357 flag = NILFS_SB_COMMIT_ALL;
358 }
Ryusuke Konishif7545142011-03-09 11:05:08 +0900359 ret = nilfs_commit_super(sb, flag);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900360 }
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900361 return ret;
362}
363
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900364/**
365 * nilfs_move_2nd_super - relocate secondary super block
366 * @sb: super block instance
367 * @sb2off: new offset of the secondary super block (in bytes)
368 */
369static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
370{
371 struct the_nilfs *nilfs = sb->s_fs_info;
372 struct buffer_head *nsbh;
373 struct nilfs_super_block *nsbp;
374 sector_t blocknr, newblocknr;
375 unsigned long offset;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800376 int sb2i; /* array index of the secondary superblock */
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900377 int ret = 0;
378
379 /* nilfs->ns_sem must be locked by the caller. */
380 if (nilfs->ns_sbh[1] &&
381 nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
382 sb2i = 1;
383 blocknr = nilfs->ns_sbh[1]->b_blocknr;
384 } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
385 sb2i = 0;
386 blocknr = nilfs->ns_sbh[0]->b_blocknr;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800387 } else {
388 sb2i = -1;
389 blocknr = 0;
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900390 }
391 if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
392 goto out; /* super block location is unchanged */
393
394 /* Get new super block buffer */
395 newblocknr = sb2off >> nilfs->ns_blocksize_bits;
396 offset = sb2off & (nilfs->ns_blocksize - 1);
397 nsbh = sb_getblk(sb, newblocknr);
398 if (!nsbh) {
399 printk(KERN_WARNING
400 "NILFS warning: unable to move secondary superblock "
401 "to block %llu\n", (unsigned long long)newblocknr);
402 ret = -EIO;
403 goto out;
404 }
405 nsbp = (void *)nsbh->b_data + offset;
406 memset(nsbp, 0, nilfs->ns_blocksize);
407
408 if (sb2i >= 0) {
409 memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
410 brelse(nilfs->ns_sbh[sb2i]);
411 nilfs->ns_sbh[sb2i] = nsbh;
412 nilfs->ns_sbp[sb2i] = nsbp;
413 } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
414 /* secondary super block will be restored to index 1 */
415 nilfs->ns_sbh[1] = nsbh;
416 nilfs->ns_sbp[1] = nsbp;
417 } else {
418 brelse(nsbh);
419 }
420out:
421 return ret;
422}
423
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900424/**
425 * nilfs_resize_fs - resize the filesystem
426 * @sb: super block instance
427 * @newsize: new size of the filesystem (in bytes)
428 */
429int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
430{
431 struct the_nilfs *nilfs = sb->s_fs_info;
432 struct nilfs_super_block **sbp;
433 __u64 devsize, newnsegs;
434 loff_t sb2off;
435 int ret;
436
437 ret = -ERANGE;
438 devsize = i_size_read(sb->s_bdev->bd_inode);
439 if (newsize > devsize)
440 goto out;
441
442 /*
443 * Write lock is required to protect some functions depending
444 * on the number of segments, the number of reserved segments,
445 * and so forth.
446 */
447 down_write(&nilfs->ns_segctor_sem);
448
449 sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
450 newnsegs = sb2off >> nilfs->ns_blocksize_bits;
451 do_div(newnsegs, nilfs->ns_blocks_per_segment);
452
453 ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
454 up_write(&nilfs->ns_segctor_sem);
455 if (ret < 0)
456 goto out;
457
458 ret = nilfs_construct_segment(sb);
459 if (ret < 0)
460 goto out;
461
462 down_write(&nilfs->ns_sem);
463 nilfs_move_2nd_super(sb, sb2off);
464 ret = -EIO;
465 sbp = nilfs_prepare_super(sb, 0);
466 if (likely(sbp)) {
467 nilfs_set_log_cursor(sbp[0], nilfs);
468 /*
469 * Drop NILFS_RESIZE_FS flag for compatibility with
470 * mount-time resize which may be implemented in a
471 * future release.
472 */
473 sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
474 ~NILFS_RESIZE_FS);
475 sbp[0]->s_dev_size = cpu_to_le64(newsize);
476 sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
477 if (sbp[1])
478 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
479 ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
480 }
481 up_write(&nilfs->ns_sem);
482
483 /*
484 * Reset the range of allocatable segments last. This order
485 * is important in the case of expansion because the secondary
486 * superblock must be protected from log write until migration
487 * completes.
488 */
489 if (!ret)
490 nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
491out:
492 return ret;
493}
494
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700495static void nilfs_put_super(struct super_block *sb)
496{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900497 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700498
Ryusuke Konishif7545142011-03-09 11:05:08 +0900499 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700500
501 if (!(sb->s_flags & MS_RDONLY)) {
502 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900503 nilfs_cleanup_super(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700504 up_write(&nilfs->ns_sem);
505 }
506
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900507 iput(nilfs->ns_sufile);
508 iput(nilfs->ns_cpfile);
509 iput(nilfs->ns_dat);
510
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +0900511 destroy_nilfs(nilfs);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700512 sb->s_fs_info = NULL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700513}
514
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700515static int nilfs_sync_fs(struct super_block *sb, int wait)
516{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900517 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900518 struct nilfs_super_block **sbp;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700519 int err = 0;
520
521 /* This function is called when super block should be written back */
522 if (wait)
523 err = nilfs_construct_segment(sb);
Jiro SEKIBA6233caa2009-07-23 01:26:33 +0900524
525 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900526 if (nilfs_sb_dirty(nilfs)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900527 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900528 if (likely(sbp)) {
529 nilfs_set_log_cursor(sbp[0], nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900530 nilfs_commit_super(sb, NILFS_SB_COMMIT);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900531 }
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900532 }
Jiro SEKIBA6233caa2009-07-23 01:26:33 +0900533 up_write(&nilfs->ns_sem);
534
Andreas Rohnere2c76172014-10-13 15:53:20 -0700535 if (!err)
536 err = nilfs_flush_device(nilfs);
537
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700538 return err;
539}
540
Ryusuke Konishif7545142011-03-09 11:05:08 +0900541int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900542 struct nilfs_root **rootp)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700543{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900544 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900545 struct nilfs_root *root;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700546 struct nilfs_checkpoint *raw_cp;
547 struct buffer_head *bh_cp;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900548 int err = -ENOMEM;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700549
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900550 root = nilfs_find_or_create_root(
551 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
552 if (!root)
553 return err;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700554
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900555 if (root->ifile)
556 goto reuse; /* already attached checkpoint */
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700557
Zhang Qiang1154ecb2009-08-18 14:58:24 +0800558 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700559 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
560 &bh_cp);
Zhang Qiang1154ecb2009-08-18 14:58:24 +0800561 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700562 if (unlikely(err)) {
563 if (err == -ENOENT || err == -EINVAL) {
564 printk(KERN_ERR
565 "NILFS: Invalid checkpoint "
566 "(checkpoint number=%llu)\n",
567 (unsigned long long)cno);
568 err = -EINVAL;
569 }
570 goto failed;
571 }
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900572
Ryusuke Konishif7545142011-03-09 11:05:08 +0900573 err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900574 &raw_cp->cp_ifile_inode, &root->ifile);
575 if (err)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700576 goto failed_bh;
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900577
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700578 atomic64_set(&root->inodes_count,
579 le64_to_cpu(raw_cp->cp_inodes_count));
580 atomic64_set(&root->blocks_count,
581 le64_to_cpu(raw_cp->cp_blocks_count));
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700582
583 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900584
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900585 reuse:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900586 *rootp = root;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700587 return 0;
588
589 failed_bh:
590 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
591 failed:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900592 nilfs_put_root(root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700593
594 return err;
595}
596
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900597static int nilfs_freeze(struct super_block *sb)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700598{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900599 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900600 int err;
601
602 if (sb->s_flags & MS_RDONLY)
603 return 0;
604
605 /* Mark super block clean */
606 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900607 err = nilfs_cleanup_super(sb);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900608 up_write(&nilfs->ns_sem);
609 return err;
610}
611
612static int nilfs_unfreeze(struct super_block *sb)
613{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900614 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700615
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900616 if (sb->s_flags & MS_RDONLY)
617 return 0;
618
619 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900620 nilfs_setup_super(sb, false);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900621 up_write(&nilfs->ns_sem);
622 return 0;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700623}
624
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700625static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
626{
627 struct super_block *sb = dentry->d_sb;
David Howells2b0143b2015-03-17 22:25:59 +0000628 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900629 struct the_nilfs *nilfs = root->nilfs;
Ryusuke Konishic306af22009-03-26 10:16:57 +0900630 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700631 unsigned long long blocks;
632 unsigned long overhead;
633 unsigned long nrsvblocks;
634 sector_t nfreeblocks;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700635 u64 nmaxinodes, nfreeinodes;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700636 int err;
637
638 /*
639 * Compute all of the segment blocks
640 *
641 * The blocks before first segment and after last segment
642 * are excluded.
643 */
644 blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
645 - nilfs->ns_first_data_block;
646 nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
647
648 /*
649 * Compute the overhead
650 *
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900651 * When distributing meta data blocks outside segment structure,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700652 * We must count them as the overhead.
653 */
654 overhead = 0;
655
656 err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
657 if (unlikely(err))
658 return err;
659
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700660 err = nilfs_ifile_count_free_inodes(root->ifile,
661 &nmaxinodes, &nfreeinodes);
662 if (unlikely(err)) {
663 printk(KERN_WARNING
664 "NILFS warning: fail to count free inodes: err %d.\n",
665 err);
666 if (err == -ERANGE) {
667 /*
668 * If nilfs_palloc_count_max_entries() returns
669 * -ERANGE error code then we simply treat
670 * curent inodes count as maximum possible and
671 * zero as free inodes value.
672 */
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700673 nmaxinodes = atomic64_read(&root->inodes_count);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700674 nfreeinodes = 0;
675 err = 0;
676 } else
677 return err;
678 }
679
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700680 buf->f_type = NILFS_SUPER_MAGIC;
681 buf->f_bsize = sb->s_blocksize;
682 buf->f_blocks = blocks - overhead;
683 buf->f_bfree = nfreeblocks;
684 buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
685 (buf->f_bfree - nrsvblocks) : 0;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700686 buf->f_files = nmaxinodes;
687 buf->f_ffree = nfreeinodes;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700688 buf->f_namelen = NILFS_NAME_LEN;
Ryusuke Konishic306af22009-03-26 10:16:57 +0900689 buf->f_fsid.val[0] = (u32)id;
690 buf->f_fsid.val[1] = (u32)(id >> 32);
691
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700692 return 0;
693}
694
Al Viro34c80b12011-12-08 21:32:45 -0500695static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900696{
Al Viro34c80b12011-12-08 21:32:45 -0500697 struct super_block *sb = dentry->d_sb;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900698 struct the_nilfs *nilfs = sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000699 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900700
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900701 if (!nilfs_test_opt(nilfs, BARRIER))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900702 seq_puts(seq, ",nobarrier");
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900703 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
704 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900705 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900706 seq_puts(seq, ",errors=panic");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900707 if (nilfs_test_opt(nilfs, ERRORS_CONT))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900708 seq_puts(seq, ",errors=continue");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900709 if (nilfs_test_opt(nilfs, STRICT_ORDER))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900710 seq_puts(seq, ",order=strict");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900711 if (nilfs_test_opt(nilfs, NORECOVERY))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900712 seq_puts(seq, ",norecovery");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900713 if (nilfs_test_opt(nilfs, DISCARD))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900714 seq_puts(seq, ",discard");
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900715
716 return 0;
717}
718
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700719static const struct super_operations nilfs_sops = {
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700720 .alloc_inode = nilfs_alloc_inode,
721 .destroy_inode = nilfs_destroy_inode,
722 .dirty_inode = nilfs_dirty_inode,
Al Viro6fd1e5c2010-06-07 11:55:00 -0400723 .evict_inode = nilfs_evict_inode,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700724 .put_super = nilfs_put_super,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700725 .sync_fs = nilfs_sync_fs,
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900726 .freeze_fs = nilfs_freeze,
727 .unfreeze_fs = nilfs_unfreeze,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700728 .statfs = nilfs_statfs,
729 .remount_fs = nilfs_remount,
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900730 .show_options = nilfs_show_options
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700731};
732
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700733enum {
734 Opt_err_cont, Opt_err_panic, Opt_err_ro,
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900735 Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900736 Opt_discard, Opt_nodiscard, Opt_err,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700737};
738
739static match_table_t tokens = {
740 {Opt_err_cont, "errors=continue"},
741 {Opt_err_panic, "errors=panic"},
742 {Opt_err_ro, "errors=remount-ro"},
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900743 {Opt_barrier, "barrier"},
Jiro SEKIBA91f19532009-11-12 14:07:26 +0900744 {Opt_nobarrier, "nobarrier"},
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700745 {Opt_snapshot, "cp=%u"},
746 {Opt_order, "order=%s"},
Ryusuke Konishi02345762009-11-20 03:28:01 +0900747 {Opt_norecovery, "norecovery"},
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900748 {Opt_discard, "discard"},
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900749 {Opt_nodiscard, "nodiscard"},
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700750 {Opt_err, NULL}
751};
752
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900753static int parse_options(char *options, struct super_block *sb, int is_remount)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700754{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900755 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700756 char *p;
757 substring_t args[MAX_OPT_ARGS];
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700758
759 if (!options)
760 return 1;
761
762 while ((p = strsep(&options, ",")) != NULL) {
763 int token;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -0700764
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700765 if (!*p)
766 continue;
767
768 token = match_token(p, tokens, args);
769 switch (token) {
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900770 case Opt_barrier:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900771 nilfs_set_opt(nilfs, BARRIER);
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900772 break;
Jiro SEKIBA91f19532009-11-12 14:07:26 +0900773 case Opt_nobarrier:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900774 nilfs_clear_opt(nilfs, BARRIER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700775 break;
776 case Opt_order:
777 if (strcmp(args[0].from, "relaxed") == 0)
778 /* Ordered data semantics */
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900779 nilfs_clear_opt(nilfs, STRICT_ORDER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700780 else if (strcmp(args[0].from, "strict") == 0)
781 /* Strict in-order semantics */
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900782 nilfs_set_opt(nilfs, STRICT_ORDER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700783 else
784 return 0;
785 break;
786 case Opt_err_panic:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900787 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700788 break;
789 case Opt_err_ro:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900790 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700791 break;
792 case Opt_err_cont:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900793 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700794 break;
795 case Opt_snapshot:
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900796 if (is_remount) {
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900797 printk(KERN_ERR
798 "NILFS: \"%s\" option is invalid "
799 "for remount.\n", p);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700800 return 0;
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900801 }
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700802 break;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900803 case Opt_norecovery:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900804 nilfs_set_opt(nilfs, NORECOVERY);
Ryusuke Konishi02345762009-11-20 03:28:01 +0900805 break;
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900806 case Opt_discard:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900807 nilfs_set_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900808 break;
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900809 case Opt_nodiscard:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900810 nilfs_clear_opt(nilfs, DISCARD);
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900811 break;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700812 default:
813 printk(KERN_ERR
814 "NILFS: Unrecognized mount option \"%s\"\n", p);
815 return 0;
816 }
817 }
818 return 1;
819}
820
821static inline void
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900822nilfs_set_default_options(struct super_block *sb,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700823 struct nilfs_super_block *sbp)
824{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900825 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900826
827 nilfs->ns_mount_opt =
Ryusuke Konishi277a6a32010-04-02 18:02:33 +0900828 NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700829}
830
Ryusuke Konishif7545142011-03-09 11:05:08 +0900831static int nilfs_setup_super(struct super_block *sb, int is_mount)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700832{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900833 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900834 struct nilfs_super_block **sbp;
835 int max_mnt_count;
836 int mnt_count;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700837
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900838 /* nilfs->ns_sem must be locked by the caller. */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900839 sbp = nilfs_prepare_super(sb, 0);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900840 if (!sbp)
841 return -EIO;
842
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900843 if (!is_mount)
844 goto skip_mount_setup;
845
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900846 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
847 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
848
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900849 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700850 printk(KERN_WARNING
851 "NILFS warning: mounting fs with errors\n");
852#if 0
853 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
854 printk(KERN_WARNING
855 "NILFS warning: maximal mount count reached\n");
856#endif
857 }
858 if (!max_mnt_count)
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900859 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700860
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900861 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900862 sbp[0]->s_mtime = cpu_to_le64(get_seconds());
863
864skip_mount_setup:
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900865 sbp[0]->s_state =
866 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900867 /* synchronize sbp[1] with sbp[0] */
Ryusuke Konishi0ca7a5b2011-01-21 16:40:31 +0900868 if (sbp[1])
869 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900870 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700871}
872
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700873struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
874 u64 pos, int blocksize,
875 struct buffer_head **pbh)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700876{
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700877 unsigned long long sb_index = pos;
878 unsigned long offset;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700879
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700880 offset = do_div(sb_index, blocksize);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700881 *pbh = sb_bread(sb, sb_index);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700882 if (!*pbh)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700883 return NULL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700884 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
885}
886
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700887int nilfs_store_magic_and_option(struct super_block *sb,
888 struct nilfs_super_block *sbp,
889 char *data)
890{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900891 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700892
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700893 sb->s_magic = le16_to_cpu(sbp->s_magic);
894
895 /* FS independent flags */
896#ifdef NILFS_ATIME_DISABLE
897 sb->s_flags |= MS_NOATIME;
898#endif
899
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900900 nilfs_set_default_options(sb, sbp);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700901
Ryusuke Konishi574e6c32011-03-09 11:05:07 +0900902 nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
903 nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
904 nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
905 nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700906
Ryusuke Konishic9cb9b52016-05-23 16:23:34 -0700907 return !parse_options(data, sb, 0) ? -EINVAL : 0;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700908}
909
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900910int nilfs_check_feature_compatibility(struct super_block *sb,
911 struct nilfs_super_block *sbp)
912{
913 __u64 features;
914
915 features = le64_to_cpu(sbp->s_feature_incompat) &
916 ~NILFS_FEATURE_INCOMPAT_SUPP;
917 if (features) {
918 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
919 "optional features (%llx)\n",
920 (unsigned long long)features);
921 return -EINVAL;
922 }
923 features = le64_to_cpu(sbp->s_feature_compat_ro) &
924 ~NILFS_FEATURE_COMPAT_RO_SUPP;
925 if (!(sb->s_flags & MS_RDONLY) && features) {
926 printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
927 "unsupported optional features (%llx)\n",
928 (unsigned long long)features);
929 return -EINVAL;
930 }
931 return 0;
932}
933
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900934static int nilfs_get_root_dentry(struct super_block *sb,
935 struct nilfs_root *root,
936 struct dentry **root_dentry)
937{
938 struct inode *inode;
939 struct dentry *dentry;
940 int ret = 0;
941
942 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
943 if (IS_ERR(inode)) {
944 printk(KERN_ERR "NILFS: get root inode failed\n");
945 ret = PTR_ERR(inode);
946 goto out;
947 }
948 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
949 iput(inode);
950 printk(KERN_ERR "NILFS: corrupt root inode.\n");
951 ret = -EINVAL;
952 goto out;
953 }
954
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900955 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
956 dentry = d_find_alias(inode);
957 if (!dentry) {
Al Viro48fde702012-01-08 22:15:13 -0500958 dentry = d_make_root(inode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900959 if (!dentry) {
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900960 ret = -ENOMEM;
961 goto failed_dentry;
962 }
963 } else {
964 iput(inode);
965 }
966 } else {
J. Bruce Fields1a0a3972014-02-14 17:35:37 -0500967 dentry = d_obtain_root(inode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900968 if (IS_ERR(dentry)) {
969 ret = PTR_ERR(dentry);
970 goto failed_dentry;
971 }
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900972 }
973 *root_dentry = dentry;
974 out:
975 return ret;
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900976
977 failed_dentry:
978 printk(KERN_ERR "NILFS: get root dentry failed\n");
979 goto out;
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900980}
981
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900982static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
983 struct dentry **root_dentry)
984{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900985 struct the_nilfs *nilfs = s->s_fs_info;
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900986 struct nilfs_root *root;
987 int ret;
988
Ryusuke Konishi572d8b32012-07-30 14:42:07 -0700989 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
990
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900991 down_read(&nilfs->ns_segctor_sem);
992 ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
993 up_read(&nilfs->ns_segctor_sem);
994 if (ret < 0) {
995 ret = (ret == -ENOENT) ? -EINVAL : ret;
996 goto out;
997 } else if (!ret) {
998 printk(KERN_ERR "NILFS: The specified checkpoint is "
999 "not a snapshot (checkpoint number=%llu).\n",
1000 (unsigned long long)cno);
1001 ret = -EINVAL;
1002 goto out;
1003 }
1004
Ryusuke Konishif7545142011-03-09 11:05:08 +09001005 ret = nilfs_attach_checkpoint(s, cno, false, &root);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +09001006 if (ret) {
1007 printk(KERN_ERR "NILFS: error loading snapshot "
1008 "(checkpoint number=%llu).\n",
1009 (unsigned long long)cno);
1010 goto out;
1011 }
1012 ret = nilfs_get_root_dentry(s, root, root_dentry);
1013 nilfs_put_root(root);
1014 out:
Ryusuke Konishi572d8b32012-07-30 14:42:07 -07001015 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +09001016 return ret;
1017}
1018
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001019/**
Al Viroe95c3112013-08-24 12:06:16 -04001020 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001021 * @root_dentry: root dentry of the tree to be shrunk
1022 *
1023 * This function returns true if the tree was in-use.
1024 */
Al Viroe95c3112013-08-24 12:06:16 -04001025static bool nilfs_tree_is_busy(struct dentry *root_dentry)
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001026{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001027 shrink_dcache_parent(root_dentry);
Al Viroe95c3112013-08-24 12:06:16 -04001028 return d_count(root_dentry) > 1;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001029}
1030
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001031int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1032{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001033 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001034 struct nilfs_root *root;
1035 struct inode *inode;
1036 struct dentry *dentry;
1037 int ret;
1038
Ryusuke Konishi3377f842015-04-16 12:46:45 -07001039 if (cno > nilfs->ns_cno)
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001040 return false;
1041
1042 if (cno >= nilfs_last_cno(nilfs))
1043 return true; /* protect recent checkpoints */
1044
1045 ret = false;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001046 root = nilfs_lookup_root(nilfs, cno);
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001047 if (root) {
1048 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1049 if (inode) {
1050 dentry = d_find_alias(inode);
1051 if (dentry) {
Al Viroe95c3112013-08-24 12:06:16 -04001052 ret = nilfs_tree_is_busy(dentry);
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001053 dput(dentry);
1054 }
1055 iput(inode);
1056 }
1057 nilfs_put_root(root);
1058 }
1059 return ret;
1060}
1061
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001062/**
1063 * nilfs_fill_super() - initialize a super block instance
1064 * @sb: super_block
1065 * @data: mount options
1066 * @silent: silent mode flag
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001067 *
Ryusuke Konishiaa7dfb82009-06-08 01:39:33 +09001068 * This function is called exclusively by nilfs->ns_mount_mutex.
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001069 * So, the recovery process is protected from other simultaneous mounts.
1070 */
1071static int
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001072nilfs_fill_super(struct super_block *sb, void *data, int silent)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001073{
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001074 struct the_nilfs *nilfs;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001075 struct nilfs_root *fsroot;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001076 __u64 cno;
1077 int err;
1078
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001079 nilfs = alloc_nilfs(sb->s_bdev);
1080 if (!nilfs)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001081 return -ENOMEM;
1082
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001083 sb->s_fs_info = nilfs;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001084
Ryusuke Konishif7545142011-03-09 11:05:08 +09001085 err = init_nilfs(nilfs, sb, (char *)data);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001086 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001087 goto failed_nilfs;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001088
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001089 sb->s_op = &nilfs_sops;
1090 sb->s_export_op = &nilfs_export_ops;
1091 sb->s_root = NULL;
Ryusuke Konishi61239232009-04-06 19:02:00 -07001092 sb->s_time_gran = 1;
Al Viro8de52772012-02-06 12:45:27 -05001093 sb->s_max_links = NILFS_LINK_MAX;
Ryusuke Konishi026a7d62010-10-07 14:19:48 +09001094
Christoph Hellwig26ff1302015-01-14 10:42:35 +01001095 sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001096
Ryusuke Konishif7545142011-03-09 11:05:08 +09001097 err = load_nilfs(nilfs, sb);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +09001098 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001099 goto failed_nilfs;
Ryusuke Konishif50a4c82009-11-19 16:58:40 +09001100
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001101 cno = nilfs_last_cno(nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001102 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001103 if (err) {
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001104 printk(KERN_ERR "NILFS: error loading last checkpoint "
1105 "(checkpoint number=%llu).\n", (unsigned long long)cno);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001106 goto failed_unload;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001107 }
1108
1109 if (!(sb->s_flags & MS_RDONLY)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09001110 err = nilfs_attach_log_writer(sb, fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001111 if (err)
1112 goto failed_checkpoint;
1113 }
1114
Ryusuke Konishi367ea332010-08-26 01:52:51 +09001115 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1116 if (err)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001117 goto failed_segctor;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001118
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001119 nilfs_put_root(fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001120
1121 if (!(sb->s_flags & MS_RDONLY)) {
1122 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001123 nilfs_setup_super(sb, true);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001124 up_write(&nilfs->ns_sem);
1125 }
1126
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001127 return 0;
1128
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001129 failed_segctor:
Ryusuke Konishif7545142011-03-09 11:05:08 +09001130 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001131
1132 failed_checkpoint:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001133 nilfs_put_root(fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001134
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001135 failed_unload:
1136 iput(nilfs->ns_sufile);
1137 iput(nilfs->ns_cpfile);
1138 iput(nilfs->ns_dat);
1139
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001140 failed_nilfs:
1141 destroy_nilfs(nilfs);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001142 return err;
1143}
1144
1145static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1146{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001147 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001148 unsigned long old_sb_flags;
Ryusuke Konishi06df0f92010-12-27 00:04:06 +09001149 unsigned long old_mount_opt;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001150 int err;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001151
Theodore Ts'o02b99842014-03-13 10:14:33 -04001152 sync_filesystem(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001153 old_sb_flags = sb->s_flags;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09001154 old_mount_opt = nilfs->ns_mount_opt;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001155
Ryusuke Konishi7c017452010-07-05 20:08:33 +09001156 if (!parse_options(data, sb, 1)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001157 err = -EINVAL;
1158 goto restore_opts;
1159 }
1160 sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1161
Ryusuke Konishid240e062010-05-09 21:51:53 +09001162 err = -EINVAL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001163
Ryusuke Konishi02345762009-11-20 03:28:01 +09001164 if (!nilfs_valid_fs(nilfs)) {
1165 printk(KERN_WARNING "NILFS (device %s): couldn't "
1166 "remount because the filesystem is in an "
1167 "incomplete recovery state.\n", sb->s_id);
Ryusuke Konishi02345762009-11-20 03:28:01 +09001168 goto restore_opts;
1169 }
1170
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001171 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1172 goto out;
1173 if (*flags & MS_RDONLY) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09001174 /* Shutting down log writer */
1175 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001176 sb->s_flags |= MS_RDONLY;
1177
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001178 /*
1179 * Remounting a valid RW partition RDONLY, so set
1180 * the RDONLY flag and then mark the partition as valid again.
1181 */
1182 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001183 nilfs_cleanup_super(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001184 up_write(&nilfs->ns_sem);
1185 } else {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001186 __u64 features;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001187 struct nilfs_root *root;
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001188
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001189 /*
1190 * Mounting a RDONLY partition read-write, so reread and
1191 * store the current valid flag. (It may have been changed
1192 * by fsck since we originally mounted the partition.)
1193 */
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001194 down_read(&nilfs->ns_sem);
1195 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1196 ~NILFS_FEATURE_COMPAT_RO_SUPP;
1197 up_read(&nilfs->ns_sem);
1198 if (features) {
1199 printk(KERN_WARNING "NILFS (device %s): couldn't "
1200 "remount RDWR because of unsupported optional "
1201 "features (%llx)\n",
1202 sb->s_id, (unsigned long long)features);
1203 err = -EROFS;
1204 goto restore_opts;
1205 }
1206
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001207 sb->s_flags &= ~MS_RDONLY;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001208
David Howells2b0143b2015-03-17 22:25:59 +00001209 root = NILFS_I(d_inode(sb->s_root))->i_root;
Ryusuke Konishif7545142011-03-09 11:05:08 +09001210 err = nilfs_attach_log_writer(sb, root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001211 if (err)
Ryusuke Konishie59399d2009-06-08 01:39:32 +09001212 goto restore_opts;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001213
1214 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001215 nilfs_setup_super(sb, true);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001216 up_write(&nilfs->ns_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001217 }
1218 out:
1219 return 0;
1220
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001221 restore_opts:
1222 sb->s_flags = old_sb_flags;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09001223 nilfs->ns_mount_opt = old_mount_opt;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001224 return err;
1225}
1226
1227struct nilfs_super_data {
1228 struct block_device *bdev;
1229 __u64 cno;
1230 int flags;
1231};
1232
1233/**
1234 * nilfs_identify - pre-read mount options needed to identify mount instance
1235 * @data: mount options
1236 * @sd: nilfs_super_data
1237 */
1238static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1239{
1240 char *p, *options = data;
1241 substring_t args[MAX_OPT_ARGS];
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001242 int token;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001243 int ret = 0;
1244
1245 do {
1246 p = strsep(&options, ",");
1247 if (p != NULL && *p) {
1248 token = match_token(p, tokens, args);
1249 if (token == Opt_snapshot) {
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001250 if (!(sd->flags & MS_RDONLY)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001251 ret++;
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001252 } else {
1253 sd->cno = simple_strtoull(args[0].from,
1254 NULL, 0);
1255 /*
1256 * No need to see the end pointer;
1257 * match_token() has done syntax
1258 * checking.
1259 */
1260 if (sd->cno == 0)
1261 ret++;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001262 }
1263 }
1264 if (ret)
1265 printk(KERN_ERR
1266 "NILFS: invalid mount option: %s\n", p);
1267 }
1268 if (!options)
1269 break;
1270 BUG_ON(options == data);
1271 *(options - 1) = ',';
1272 } while (!ret);
1273 return ret;
1274}
1275
1276static int nilfs_set_bdev_super(struct super_block *s, void *data)
1277{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001278 s->s_bdev = data;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001279 s->s_dev = s->s_bdev->bd_dev;
1280 return 0;
1281}
1282
1283static int nilfs_test_bdev_super(struct super_block *s, void *data)
1284{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001285 return (void *)s->s_bdev == data;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001286}
1287
Al Viroe4c59d62010-07-26 12:19:34 +04001288static struct dentry *
1289nilfs_mount(struct file_system_type *fs_type, int flags,
1290 const char *dev_name, void *data)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001291{
1292 struct nilfs_super_data sd;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001293 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +01001294 fmode_t mode = FMODE_READ | FMODE_EXCL;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001295 struct dentry *root_dentry;
1296 int err, s_new = false;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001297
Ryusuke Konishi13e90552010-05-09 02:57:57 +09001298 if (!(flags & MS_RDONLY))
1299 mode |= FMODE_WRITE;
1300
Tejun Heod4d77622010-11-13 11:55:18 +01001301 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Jan Blunckd6d4c192010-02-24 13:25:32 +01001302 if (IS_ERR(sd.bdev))
Al Viroe4c59d62010-07-26 12:19:34 +04001303 return ERR_CAST(sd.bdev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001304
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001305 sd.cno = 0;
1306 sd.flags = flags;
1307 if (nilfs_identify((char *)data, &sd)) {
1308 err = -EINVAL;
1309 goto failed;
1310 }
1311
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +09001312 /*
1313 * once the super is inserted into the list by sget, s_umount
1314 * will protect the lockfs code from trying to start a snapshot
1315 * while we are mounting
1316 */
1317 mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
1318 if (sd.bdev->bd_fsfreeze_count > 0) {
1319 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
1320 err = -EBUSY;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001321 goto failed;
1322 }
David Howells9249e172012-06-25 12:55:37 +01001323 s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1324 sd.bdev);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +09001325 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001326 if (IS_ERR(s)) {
1327 err = PTR_ERR(s);
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001328 goto failed;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001329 }
1330
1331 if (!s->s_root) {
Ryusuke Konishi8fa7c322016-05-23 16:23:00 -07001332 s_new = true;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001333
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001334 /* New superblock instance created */
Ryusuke Konishi4571b822010-05-09 03:01:32 +09001335 s->s_mode = mode;
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001336 snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001337 sb_set_blocksize(s, block_size(sd.bdev));
1338
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001339 err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001340 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001341 goto failed_super;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001342
1343 s->s_flags |= MS_ACTIVE;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001344 } else if (!sd.cno) {
Al Viroe95c3112013-08-24 12:06:16 -04001345 if (nilfs_tree_is_busy(s->s_root)) {
1346 if ((flags ^ s->s_flags) & MS_RDONLY) {
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001347 printk(KERN_ERR "NILFS: the device already "
1348 "has a %s mount.\n",
1349 (s->s_flags & MS_RDONLY) ?
1350 "read-only" : "read/write");
1351 err = -EBUSY;
1352 goto failed_super;
1353 }
Al Viroe95c3112013-08-24 12:06:16 -04001354 } else {
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001355 /*
1356 * Try remount to setup mount states if the current
1357 * tree is not mounted and only snapshots use this sb.
1358 */
1359 err = nilfs_remount(s, &flags, data);
1360 if (err)
1361 goto failed_super;
1362 }
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001363 }
1364
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001365 if (sd.cno) {
1366 err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001367 if (err)
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001368 goto failed_super;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001369 } else {
1370 root_dentry = dget(s->s_root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001371 }
1372
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001373 if (!s_new)
Tejun Heod4d77622010-11-13 11:55:18 +01001374 blkdev_put(sd.bdev, mode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001375
Al Viroe4c59d62010-07-26 12:19:34 +04001376 return root_dentry;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001377
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001378 failed_super:
Al Viroa95161a2009-08-09 00:52:02 +04001379 deactivate_locked_super(s);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001380
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001381 failed:
1382 if (!s_new)
Tejun Heod4d77622010-11-13 11:55:18 +01001383 blkdev_put(sd.bdev, mode);
Al Viroe4c59d62010-07-26 12:19:34 +04001384 return ERR_PTR(err);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001385}
1386
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001387struct file_system_type nilfs_fs_type = {
1388 .owner = THIS_MODULE,
1389 .name = "nilfs2",
Al Viroe4c59d62010-07-26 12:19:34 +04001390 .mount = nilfs_mount,
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001391 .kill_sb = kill_block_super,
1392 .fs_flags = FS_REQUIRES_DEV,
1393};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001394MODULE_ALIAS_FS("nilfs2");
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001395
Li Hong41c88bd2010-04-06 00:54:11 +08001396static void nilfs_inode_init_once(void *obj)
1397{
1398 struct nilfs_inode_info *ii = obj;
1399
1400 INIT_LIST_HEAD(&ii->i_dirty);
1401#ifdef CONFIG_NILFS_XATTR
1402 init_rwsem(&ii->xattr_sem);
1403#endif
Miklos Szeredi2aa15892011-02-23 13:49:47 +01001404 address_space_init_once(&ii->i_btnode_cache);
Ryusuke Konishi05d0e942010-07-10 20:52:09 +09001405 ii->i_bmap = &ii->i_bmap_data;
Li Hong41c88bd2010-04-06 00:54:11 +08001406 inode_init_once(&ii->vfs_inode);
1407}
1408
1409static void nilfs_segbuf_init_once(void *obj)
1410{
1411 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1412}
1413
1414static void nilfs_destroy_cachep(void)
1415{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001416 /*
1417 * Make sure all delayed rcu free inodes are flushed before we
1418 * destroy cache.
1419 */
1420 rcu_barrier();
1421
Julia Lawallda80a392015-11-06 16:31:40 -08001422 kmem_cache_destroy(nilfs_inode_cachep);
1423 kmem_cache_destroy(nilfs_transaction_cachep);
1424 kmem_cache_destroy(nilfs_segbuf_cachep);
1425 kmem_cache_destroy(nilfs_btree_path_cache);
Li Hong41c88bd2010-04-06 00:54:11 +08001426}
1427
1428static int __init nilfs_init_cachep(void)
1429{
1430 nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1431 sizeof(struct nilfs_inode_info), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08001432 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1433 nilfs_inode_init_once);
Li Hong41c88bd2010-04-06 00:54:11 +08001434 if (!nilfs_inode_cachep)
1435 goto fail;
1436
1437 nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1438 sizeof(struct nilfs_transaction_info), 0,
1439 SLAB_RECLAIM_ACCOUNT, NULL);
1440 if (!nilfs_transaction_cachep)
1441 goto fail;
1442
1443 nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1444 sizeof(struct nilfs_segment_buffer), 0,
1445 SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1446 if (!nilfs_segbuf_cachep)
1447 goto fail;
1448
1449 nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1450 sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1451 0, 0, NULL);
1452 if (!nilfs_btree_path_cache)
1453 goto fail;
1454
1455 return 0;
1456
1457fail:
1458 nilfs_destroy_cachep();
1459 return -ENOMEM;
1460}
1461
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001462static int __init init_nilfs_fs(void)
1463{
1464 int err;
1465
Li Hong41c88bd2010-04-06 00:54:11 +08001466 err = nilfs_init_cachep();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001467 if (err)
Li Hong41c88bd2010-04-06 00:54:11 +08001468 goto fail;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001469
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001470 err = nilfs_sysfs_init();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001471 if (err)
Li Hong41c88bd2010-04-06 00:54:11 +08001472 goto free_cachep;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001473
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001474 err = register_filesystem(&nilfs_fs_type);
1475 if (err)
1476 goto deinit_sysfs_entry;
1477
Li Hong9f130262010-04-09 23:09:53 +08001478 printk(KERN_INFO "NILFS version 2 loaded\n");
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001479 return 0;
1480
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001481deinit_sysfs_entry:
1482 nilfs_sysfs_exit();
Li Hong41c88bd2010-04-06 00:54:11 +08001483free_cachep:
1484 nilfs_destroy_cachep();
1485fail:
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001486 return err;
1487}
1488
1489static void __exit exit_nilfs_fs(void)
1490{
Li Hong41c88bd2010-04-06 00:54:11 +08001491 nilfs_destroy_cachep();
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001492 nilfs_sysfs_exit();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001493 unregister_filesystem(&nilfs_fs_type);
1494}
1495
1496module_init(init_nilfs_fs)
1497module_exit(exit_nilfs_fs)