blob: 90c62b4898570e1881ad88a1957247a5b9b9b93f [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 Konishifeee8802016-08-02 14:05:10 -0700217 nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
218 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 {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700288 nilfs_msg(sb, KERN_CRIT, "superblock broke");
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900289 return NULL;
290 }
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900291 } else if (sbp[1] &&
292 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
Ryusuke Konishi7592ecd2016-05-23 16:23:36 -0700293 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900294 }
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900295
296 if (flip && sbp[1])
297 nilfs_swap_super_block(nilfs);
298
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900299 return sbp;
300}
301
Ryusuke Konishif7545142011-03-09 11:05:08 +0900302int nilfs_commit_super(struct super_block *sb, int flag)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700303{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900304 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700305 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700306 time_t t;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700307
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900308 /* nilfs->ns_sem must be locked by the caller. */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700309 t = get_seconds();
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900310 nilfs->ns_sbwtime = t;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700311 sbp[0]->s_wtime = cpu_to_le64(t);
312 sbp[0]->s_sum = 0;
313 sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
314 (unsigned char *)sbp[0],
315 nilfs->ns_sbsize));
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900316 if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
317 sbp[1]->s_wtime = sbp[0]->s_wtime;
318 sbp[1]->s_sum = 0;
319 sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
320 (unsigned char *)sbp[1],
321 nilfs->ns_sbsize));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700322 }
Ryusuke Konishie605f0a2009-12-09 00:57:52 +0900323 clear_nilfs_sb_dirty(nilfs);
Andreas Rohnere2c76172014-10-13 15:53:20 -0700324 nilfs->ns_flushed_device = 1;
325 /* make sure store to ns_flushed_device cannot be reordered */
326 smp_wmb();
Ryusuke Konishif7545142011-03-09 11:05:08 +0900327 return nilfs_sync_super(sb, flag);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700328}
329
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900330/**
331 * nilfs_cleanup_super() - write filesystem state for cleanup
Ryusuke Konishif7545142011-03-09 11:05:08 +0900332 * @sb: super block instance to be unmounted or degraded to read-only
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900333 *
334 * This function restores state flags in the on-disk super block.
335 * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
336 * filesystem was not clean previously.
337 */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900338int nilfs_cleanup_super(struct super_block *sb)
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900339{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900340 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900341 struct nilfs_super_block **sbp;
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900342 int flag = NILFS_SB_COMMIT;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900343 int ret = -EIO;
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900344
Ryusuke Konishif7545142011-03-09 11:05:08 +0900345 sbp = nilfs_prepare_super(sb, 0);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900346 if (sbp) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900347 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
348 nilfs_set_log_cursor(sbp[0], nilfs);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900349 if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
350 /*
351 * make the "clean" flag also to the opposite
352 * super block if both super blocks point to
353 * the same checkpoint.
354 */
355 sbp[1]->s_state = sbp[0]->s_state;
356 flag = NILFS_SB_COMMIT_ALL;
357 }
Ryusuke Konishif7545142011-03-09 11:05:08 +0900358 ret = nilfs_commit_super(sb, flag);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900359 }
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900360 return ret;
361}
362
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900363/**
364 * nilfs_move_2nd_super - relocate secondary super block
365 * @sb: super block instance
366 * @sb2off: new offset of the secondary super block (in bytes)
367 */
368static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
369{
370 struct the_nilfs *nilfs = sb->s_fs_info;
371 struct buffer_head *nsbh;
372 struct nilfs_super_block *nsbp;
373 sector_t blocknr, newblocknr;
374 unsigned long offset;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800375 int sb2i; /* array index of the secondary superblock */
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900376 int ret = 0;
377
378 /* nilfs->ns_sem must be locked by the caller. */
379 if (nilfs->ns_sbh[1] &&
380 nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
381 sb2i = 1;
382 blocknr = nilfs->ns_sbh[1]->b_blocknr;
383 } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
384 sb2i = 0;
385 blocknr = nilfs->ns_sbh[0]->b_blocknr;
Ryusuke Konishi4f050282015-11-06 16:32:16 -0800386 } else {
387 sb2i = -1;
388 blocknr = 0;
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900389 }
390 if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
391 goto out; /* super block location is unchanged */
392
393 /* Get new super block buffer */
394 newblocknr = sb2off >> nilfs->ns_blocksize_bits;
395 offset = sb2off & (nilfs->ns_blocksize - 1);
396 nsbh = sb_getblk(sb, newblocknr);
397 if (!nsbh) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700398 nilfs_msg(sb, KERN_WARNING,
399 "unable to move secondary superblock to block %llu",
400 (unsigned long long)newblocknr);
Ryusuke Konishicfb0a4b2011-05-05 01:23:57 +0900401 ret = -EIO;
402 goto out;
403 }
404 nsbp = (void *)nsbh->b_data + offset;
405 memset(nsbp, 0, nilfs->ns_blocksize);
406
407 if (sb2i >= 0) {
408 memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
409 brelse(nilfs->ns_sbh[sb2i]);
410 nilfs->ns_sbh[sb2i] = nsbh;
411 nilfs->ns_sbp[sb2i] = nsbp;
412 } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
413 /* secondary super block will be restored to index 1 */
414 nilfs->ns_sbh[1] = nsbh;
415 nilfs->ns_sbp[1] = nsbp;
416 } else {
417 brelse(nsbh);
418 }
419out:
420 return ret;
421}
422
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900423/**
424 * nilfs_resize_fs - resize the filesystem
425 * @sb: super block instance
426 * @newsize: new size of the filesystem (in bytes)
427 */
428int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
429{
430 struct the_nilfs *nilfs = sb->s_fs_info;
431 struct nilfs_super_block **sbp;
432 __u64 devsize, newnsegs;
433 loff_t sb2off;
434 int ret;
435
436 ret = -ERANGE;
437 devsize = i_size_read(sb->s_bdev->bd_inode);
438 if (newsize > devsize)
439 goto out;
440
441 /*
442 * Write lock is required to protect some functions depending
443 * on the number of segments, the number of reserved segments,
444 * and so forth.
445 */
446 down_write(&nilfs->ns_segctor_sem);
447
448 sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
449 newnsegs = sb2off >> nilfs->ns_blocksize_bits;
450 do_div(newnsegs, nilfs->ns_blocks_per_segment);
451
452 ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
453 up_write(&nilfs->ns_segctor_sem);
454 if (ret < 0)
455 goto out;
456
457 ret = nilfs_construct_segment(sb);
458 if (ret < 0)
459 goto out;
460
461 down_write(&nilfs->ns_sem);
462 nilfs_move_2nd_super(sb, sb2off);
463 ret = -EIO;
464 sbp = nilfs_prepare_super(sb, 0);
465 if (likely(sbp)) {
466 nilfs_set_log_cursor(sbp[0], nilfs);
467 /*
468 * Drop NILFS_RESIZE_FS flag for compatibility with
469 * mount-time resize which may be implemented in a
470 * future release.
471 */
472 sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
473 ~NILFS_RESIZE_FS);
474 sbp[0]->s_dev_size = cpu_to_le64(newsize);
475 sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
476 if (sbp[1])
477 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
478 ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
479 }
480 up_write(&nilfs->ns_sem);
481
482 /*
483 * Reset the range of allocatable segments last. This order
484 * is important in the case of expansion because the secondary
485 * superblock must be protected from log write until migration
486 * completes.
487 */
488 if (!ret)
489 nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
490out:
491 return ret;
492}
493
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700494static void nilfs_put_super(struct super_block *sb)
495{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900496 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700497
Ryusuke Konishif7545142011-03-09 11:05:08 +0900498 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700499
500 if (!(sb->s_flags & MS_RDONLY)) {
501 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900502 nilfs_cleanup_super(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700503 up_write(&nilfs->ns_sem);
504 }
505
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900506 iput(nilfs->ns_sufile);
507 iput(nilfs->ns_cpfile);
508 iput(nilfs->ns_dat);
509
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +0900510 destroy_nilfs(nilfs);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700511 sb->s_fs_info = NULL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700512}
513
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700514static int nilfs_sync_fs(struct super_block *sb, int wait)
515{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900516 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900517 struct nilfs_super_block **sbp;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700518 int err = 0;
519
520 /* This function is called when super block should be written back */
521 if (wait)
522 err = nilfs_construct_segment(sb);
Jiro SEKIBA6233caa2009-07-23 01:26:33 +0900523
524 down_write(&nilfs->ns_sem);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900525 if (nilfs_sb_dirty(nilfs)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +0900526 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900527 if (likely(sbp)) {
528 nilfs_set_log_cursor(sbp[0], nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900529 nilfs_commit_super(sb, NILFS_SB_COMMIT);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900530 }
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900531 }
Jiro SEKIBA6233caa2009-07-23 01:26:33 +0900532 up_write(&nilfs->ns_sem);
533
Andreas Rohnere2c76172014-10-13 15:53:20 -0700534 if (!err)
535 err = nilfs_flush_device(nilfs);
536
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700537 return err;
538}
539
Ryusuke Konishif7545142011-03-09 11:05:08 +0900540int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900541 struct nilfs_root **rootp)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700542{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900543 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900544 struct nilfs_root *root;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700545 struct nilfs_checkpoint *raw_cp;
546 struct buffer_head *bh_cp;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900547 int err = -ENOMEM;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700548
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900549 root = nilfs_find_or_create_root(
550 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
551 if (!root)
552 return err;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700553
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900554 if (root->ifile)
555 goto reuse; /* already attached checkpoint */
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700556
Zhang Qiang1154ecb2009-08-18 14:58:24 +0800557 down_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700558 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
559 &bh_cp);
Zhang Qiang1154ecb2009-08-18 14:58:24 +0800560 up_read(&nilfs->ns_segctor_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700561 if (unlikely(err)) {
562 if (err == -ENOENT || err == -EINVAL) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700563 nilfs_msg(sb, KERN_ERR,
564 "Invalid checkpoint (checkpoint number=%llu)",
565 (unsigned long long)cno);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700566 err = -EINVAL;
567 }
568 goto failed;
569 }
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900570
Ryusuke Konishif7545142011-03-09 11:05:08 +0900571 err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900572 &raw_cp->cp_ifile_inode, &root->ifile);
573 if (err)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700574 goto failed_bh;
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900575
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700576 atomic64_set(&root->inodes_count,
577 le64_to_cpu(raw_cp->cp_inodes_count));
578 atomic64_set(&root->blocks_count,
579 le64_to_cpu(raw_cp->cp_blocks_count));
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700580
581 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900582
Ryusuke Konishie912a5b2010-08-14 13:07:15 +0900583 reuse:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900584 *rootp = root;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700585 return 0;
586
587 failed_bh:
588 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
589 failed:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +0900590 nilfs_put_root(root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700591
592 return err;
593}
594
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900595static int nilfs_freeze(struct super_block *sb)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700596{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900597 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900598 int err;
599
600 if (sb->s_flags & MS_RDONLY)
601 return 0;
602
603 /* Mark super block clean */
604 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900605 err = nilfs_cleanup_super(sb);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900606 up_write(&nilfs->ns_sem);
607 return err;
608}
609
610static int nilfs_unfreeze(struct super_block *sb)
611{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900612 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700613
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900614 if (sb->s_flags & MS_RDONLY)
615 return 0;
616
617 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900618 nilfs_setup_super(sb, false);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900619 up_write(&nilfs->ns_sem);
620 return 0;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700621}
622
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700623static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
624{
625 struct super_block *sb = dentry->d_sb;
David Howells2b0143b2015-03-17 22:25:59 +0000626 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
Ryusuke Konishib7c06342010-08-14 14:48:32 +0900627 struct the_nilfs *nilfs = root->nilfs;
Ryusuke Konishic306af22009-03-26 10:16:57 +0900628 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700629 unsigned long long blocks;
630 unsigned long overhead;
631 unsigned long nrsvblocks;
632 sector_t nfreeblocks;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700633 u64 nmaxinodes, nfreeinodes;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700634 int err;
635
636 /*
637 * Compute all of the segment blocks
638 *
639 * The blocks before first segment and after last segment
640 * are excluded.
641 */
642 blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
643 - nilfs->ns_first_data_block;
644 nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
645
646 /*
647 * Compute the overhead
648 *
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900649 * When distributing meta data blocks outside segment structure,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700650 * We must count them as the overhead.
651 */
652 overhead = 0;
653
654 err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
655 if (unlikely(err))
656 return err;
657
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700658 err = nilfs_ifile_count_free_inodes(root->ifile,
659 &nmaxinodes, &nfreeinodes);
660 if (unlikely(err)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700661 nilfs_msg(sb, KERN_WARNING,
662 "failed to count free inodes: err=%d", err);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700663 if (err == -ERANGE) {
664 /*
665 * If nilfs_palloc_count_max_entries() returns
666 * -ERANGE error code then we simply treat
667 * curent inodes count as maximum possible and
668 * zero as free inodes value.
669 */
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700670 nmaxinodes = atomic64_read(&root->inodes_count);
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700671 nfreeinodes = 0;
672 err = 0;
673 } else
674 return err;
675 }
676
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700677 buf->f_type = NILFS_SUPER_MAGIC;
678 buf->f_bsize = sb->s_blocksize;
679 buf->f_blocks = blocks - overhead;
680 buf->f_bfree = nfreeblocks;
681 buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
682 (buf->f_bfree - nrsvblocks) : 0;
Vyacheslav Dubeykoc7ef9722013-07-03 15:08:05 -0700683 buf->f_files = nmaxinodes;
684 buf->f_ffree = nfreeinodes;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700685 buf->f_namelen = NILFS_NAME_LEN;
Ryusuke Konishic306af22009-03-26 10:16:57 +0900686 buf->f_fsid.val[0] = (u32)id;
687 buf->f_fsid.val[1] = (u32)(id >> 32);
688
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700689 return 0;
690}
691
Al Viro34c80b12011-12-08 21:32:45 -0500692static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900693{
Al Viro34c80b12011-12-08 21:32:45 -0500694 struct super_block *sb = dentry->d_sb;
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900695 struct the_nilfs *nilfs = sb->s_fs_info;
David Howells2b0143b2015-03-17 22:25:59 +0000696 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900697
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900698 if (!nilfs_test_opt(nilfs, BARRIER))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900699 seq_puts(seq, ",nobarrier");
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900700 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
701 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900702 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900703 seq_puts(seq, ",errors=panic");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900704 if (nilfs_test_opt(nilfs, ERRORS_CONT))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900705 seq_puts(seq, ",errors=continue");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900706 if (nilfs_test_opt(nilfs, STRICT_ORDER))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900707 seq_puts(seq, ",order=strict");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900708 if (nilfs_test_opt(nilfs, NORECOVERY))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900709 seq_puts(seq, ",norecovery");
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900710 if (nilfs_test_opt(nilfs, DISCARD))
Ryusuke Konishic6b4d572010-07-05 14:40:27 +0900711 seq_puts(seq, ",discard");
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900712
713 return 0;
714}
715
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700716static const struct super_operations nilfs_sops = {
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700717 .alloc_inode = nilfs_alloc_inode,
718 .destroy_inode = nilfs_destroy_inode,
719 .dirty_inode = nilfs_dirty_inode,
Al Viro6fd1e5c2010-06-07 11:55:00 -0400720 .evict_inode = nilfs_evict_inode,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700721 .put_super = nilfs_put_super,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700722 .sync_fs = nilfs_sync_fs,
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900723 .freeze_fs = nilfs_freeze,
724 .unfreeze_fs = nilfs_unfreeze,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700725 .statfs = nilfs_statfs,
726 .remount_fs = nilfs_remount,
Jiro SEKIBAb58a2852009-06-24 20:06:34 +0900727 .show_options = nilfs_show_options
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700728};
729
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700730enum {
731 Opt_err_cont, Opt_err_panic, Opt_err_ro,
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900732 Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900733 Opt_discard, Opt_nodiscard, Opt_err,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700734};
735
736static match_table_t tokens = {
737 {Opt_err_cont, "errors=continue"},
738 {Opt_err_panic, "errors=panic"},
739 {Opt_err_ro, "errors=remount-ro"},
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900740 {Opt_barrier, "barrier"},
Jiro SEKIBA91f19532009-11-12 14:07:26 +0900741 {Opt_nobarrier, "nobarrier"},
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700742 {Opt_snapshot, "cp=%u"},
743 {Opt_order, "order=%s"},
Ryusuke Konishi02345762009-11-20 03:28:01 +0900744 {Opt_norecovery, "norecovery"},
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900745 {Opt_discard, "discard"},
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900746 {Opt_nodiscard, "nodiscard"},
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700747 {Opt_err, NULL}
748};
749
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900750static int parse_options(char *options, struct super_block *sb, int is_remount)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700751{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900752 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700753 char *p;
754 substring_t args[MAX_OPT_ARGS];
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700755
756 if (!options)
757 return 1;
758
759 while ((p = strsep(&options, ",")) != NULL) {
760 int token;
Ryusuke Konishi4ad364c2016-05-23 16:23:25 -0700761
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700762 if (!*p)
763 continue;
764
765 token = match_token(p, tokens, args);
766 switch (token) {
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900767 case Opt_barrier:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900768 nilfs_set_opt(nilfs, BARRIER);
Ryusuke Konishi773bc4f2010-07-05 13:00:08 +0900769 break;
Jiro SEKIBA91f19532009-11-12 14:07:26 +0900770 case Opt_nobarrier:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900771 nilfs_clear_opt(nilfs, BARRIER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700772 break;
773 case Opt_order:
774 if (strcmp(args[0].from, "relaxed") == 0)
775 /* Ordered data semantics */
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900776 nilfs_clear_opt(nilfs, STRICT_ORDER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700777 else if (strcmp(args[0].from, "strict") == 0)
778 /* Strict in-order semantics */
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900779 nilfs_set_opt(nilfs, STRICT_ORDER);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700780 else
781 return 0;
782 break;
783 case Opt_err_panic:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900784 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700785 break;
786 case Opt_err_ro:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900787 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700788 break;
789 case Opt_err_cont:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900790 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700791 break;
792 case Opt_snapshot:
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900793 if (is_remount) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700794 nilfs_msg(sb, KERN_ERR,
795 "\"%s\" option is invalid for remount",
796 p);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700797 return 0;
Ryusuke Konishi7c017452010-07-05 20:08:33 +0900798 }
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700799 break;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900800 case Opt_norecovery:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900801 nilfs_set_opt(nilfs, NORECOVERY);
Ryusuke Konishi02345762009-11-20 03:28:01 +0900802 break;
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900803 case Opt_discard:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900804 nilfs_set_opt(nilfs, DISCARD);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900805 break;
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900806 case Opt_nodiscard:
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900807 nilfs_clear_opt(nilfs, DISCARD);
Ryusuke Konishi802d3172010-07-05 14:27:04 +0900808 break;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700809 default:
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700810 nilfs_msg(sb, KERN_ERR,
811 "unrecognized mount option \"%s\"", p);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700812 return 0;
813 }
814 }
815 return 1;
816}
817
818static inline void
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900819nilfs_set_default_options(struct super_block *sb,
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700820 struct nilfs_super_block *sbp)
821{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900822 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900823
824 nilfs->ns_mount_opt =
Ryusuke Konishi277a6a32010-04-02 18:02:33 +0900825 NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700826}
827
Ryusuke Konishif7545142011-03-09 11:05:08 +0900828static int nilfs_setup_super(struct super_block *sb, int is_mount)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700829{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900830 struct the_nilfs *nilfs = sb->s_fs_info;
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900831 struct nilfs_super_block **sbp;
832 int max_mnt_count;
833 int mnt_count;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700834
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900835 /* nilfs->ns_sem must be locked by the caller. */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900836 sbp = nilfs_prepare_super(sb, 0);
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900837 if (!sbp)
838 return -EIO;
839
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900840 if (!is_mount)
841 goto skip_mount_setup;
842
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900843 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
844 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
845
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900846 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700847 nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700848#if 0
849 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700850 nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700851#endif
852 }
853 if (!max_mnt_count)
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900854 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700855
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900856 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +0900857 sbp[0]->s_mtime = cpu_to_le64(get_seconds());
858
859skip_mount_setup:
Jiro SEKIBAd26493b2010-06-28 17:49:32 +0900860 sbp[0]->s_state =
861 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900862 /* synchronize sbp[1] with sbp[0] */
Ryusuke Konishi0ca7a5b2011-01-21 16:40:31 +0900863 if (sbp[1])
864 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900865 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700866}
867
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700868struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
869 u64 pos, int blocksize,
870 struct buffer_head **pbh)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700871{
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700872 unsigned long long sb_index = pos;
873 unsigned long offset;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700874
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700875 offset = do_div(sb_index, blocksize);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700876 *pbh = sb_bread(sb, sb_index);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700877 if (!*pbh)
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700878 return NULL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700879 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
880}
881
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700882int nilfs_store_magic_and_option(struct super_block *sb,
883 struct nilfs_super_block *sbp,
884 char *data)
885{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900886 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700887
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700888 sb->s_magic = le16_to_cpu(sbp->s_magic);
889
890 /* FS independent flags */
891#ifdef NILFS_ATIME_DISABLE
892 sb->s_flags |= MS_NOATIME;
893#endif
894
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900895 nilfs_set_default_options(sb, sbp);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700896
Ryusuke Konishi574e6c32011-03-09 11:05:07 +0900897 nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
898 nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
899 nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
900 nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700901
Ryusuke Konishic9cb9b52016-05-23 16:23:34 -0700902 return !parse_options(data, sb, 0) ? -EINVAL : 0;
Ryusuke Konishi783f6182009-04-06 19:01:35 -0700903}
904
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900905int nilfs_check_feature_compatibility(struct super_block *sb,
906 struct nilfs_super_block *sbp)
907{
908 __u64 features;
909
910 features = le64_to_cpu(sbp->s_feature_incompat) &
911 ~NILFS_FEATURE_INCOMPAT_SUPP;
912 if (features) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700913 nilfs_msg(sb, KERN_ERR,
914 "couldn't mount because of unsupported optional features (%llx)",
915 (unsigned long long)features);
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900916 return -EINVAL;
917 }
918 features = le64_to_cpu(sbp->s_feature_compat_ro) &
919 ~NILFS_FEATURE_COMPAT_RO_SUPP;
920 if (!(sb->s_flags & MS_RDONLY) && features) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700921 nilfs_msg(sb, KERN_ERR,
922 "couldn't mount RDWR because of unsupported optional features (%llx)",
923 (unsigned long long)features);
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900924 return -EINVAL;
925 }
926 return 0;
927}
928
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900929static int nilfs_get_root_dentry(struct super_block *sb,
930 struct nilfs_root *root,
931 struct dentry **root_dentry)
932{
933 struct inode *inode;
934 struct dentry *dentry;
935 int ret = 0;
936
937 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
938 if (IS_ERR(inode)) {
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900939 ret = PTR_ERR(inode);
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700940 nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900941 goto out;
942 }
943 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
944 iput(inode);
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700945 nilfs_msg(sb, KERN_ERR, "corrupt root inode");
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900946 ret = -EINVAL;
947 goto out;
948 }
949
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900950 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
951 dentry = d_find_alias(inode);
952 if (!dentry) {
Al Viro48fde702012-01-08 22:15:13 -0500953 dentry = d_make_root(inode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900954 if (!dentry) {
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900955 ret = -ENOMEM;
956 goto failed_dentry;
957 }
958 } else {
959 iput(inode);
960 }
961 } else {
J. Bruce Fields1a0a3972014-02-14 17:35:37 -0500962 dentry = d_obtain_root(inode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900963 if (IS_ERR(dentry)) {
964 ret = PTR_ERR(dentry);
965 goto failed_dentry;
966 }
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900967 }
968 *root_dentry = dentry;
969 out:
970 return ret;
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900971
972 failed_dentry:
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700973 nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900974 goto out;
Ryusuke Konishi367ea332010-08-26 01:52:51 +0900975}
976
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900977static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
978 struct dentry **root_dentry)
979{
Ryusuke Konishie3154e92011-03-09 11:05:08 +0900980 struct the_nilfs *nilfs = s->s_fs_info;
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900981 struct nilfs_root *root;
982 int ret;
983
Ryusuke Konishi572d8b32012-07-30 14:42:07 -0700984 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
985
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900986 down_read(&nilfs->ns_segctor_sem);
987 ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
988 up_read(&nilfs->ns_segctor_sem);
989 if (ret < 0) {
990 ret = (ret == -ENOENT) ? -EINVAL : ret;
991 goto out;
992 } else if (!ret) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -0700993 nilfs_msg(s, KERN_ERR,
994 "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
995 (unsigned long long)cno);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +0900996 ret = -EINVAL;
997 goto out;
998 }
999
Ryusuke Konishif7545142011-03-09 11:05:08 +09001000 ret = nilfs_attach_checkpoint(s, cno, false, &root);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +09001001 if (ret) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001002 nilfs_msg(s, KERN_ERR,
1003 "error %d while loading snapshot (checkpoint number=%llu)",
1004 ret, (unsigned long long)cno);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +09001005 goto out;
1006 }
1007 ret = nilfs_get_root_dentry(s, root, root_dentry);
1008 nilfs_put_root(root);
1009 out:
Ryusuke Konishi572d8b32012-07-30 14:42:07 -07001010 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
Ryusuke Konishiab4d8f72010-08-26 02:15:41 +09001011 return ret;
1012}
1013
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001014/**
Al Viroe95c3112013-08-24 12:06:16 -04001015 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001016 * @root_dentry: root dentry of the tree to be shrunk
1017 *
1018 * This function returns true if the tree was in-use.
1019 */
Al Viroe95c3112013-08-24 12:06:16 -04001020static bool nilfs_tree_is_busy(struct dentry *root_dentry)
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001021{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001022 shrink_dcache_parent(root_dentry);
Al Viroe95c3112013-08-24 12:06:16 -04001023 return d_count(root_dentry) > 1;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001024}
1025
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001026int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1027{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001028 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001029 struct nilfs_root *root;
1030 struct inode *inode;
1031 struct dentry *dentry;
1032 int ret;
1033
Ryusuke Konishi3377f842015-04-16 12:46:45 -07001034 if (cno > nilfs->ns_cno)
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001035 return false;
1036
1037 if (cno >= nilfs_last_cno(nilfs))
1038 return true; /* protect recent checkpoints */
1039
1040 ret = false;
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001041 root = nilfs_lookup_root(nilfs, cno);
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001042 if (root) {
1043 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1044 if (inode) {
1045 dentry = d_find_alias(inode);
1046 if (dentry) {
Al Viroe95c3112013-08-24 12:06:16 -04001047 ret = nilfs_tree_is_busy(dentry);
Ryusuke Konishi032dbb32010-09-13 11:16:34 +09001048 dput(dentry);
1049 }
1050 iput(inode);
1051 }
1052 nilfs_put_root(root);
1053 }
1054 return ret;
1055}
1056
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001057/**
1058 * nilfs_fill_super() - initialize a super block instance
1059 * @sb: super_block
1060 * @data: mount options
1061 * @silent: silent mode flag
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001062 *
Ryusuke Konishiaa7dfb82009-06-08 01:39:33 +09001063 * This function is called exclusively by nilfs->ns_mount_mutex.
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001064 * So, the recovery process is protected from other simultaneous mounts.
1065 */
1066static int
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001067nilfs_fill_super(struct super_block *sb, void *data, int silent)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001068{
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001069 struct the_nilfs *nilfs;
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001070 struct nilfs_root *fsroot;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001071 __u64 cno;
1072 int err;
1073
Ryusuke Konishi66256892016-08-02 14:05:06 -07001074 nilfs = alloc_nilfs(sb);
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001075 if (!nilfs)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001076 return -ENOMEM;
1077
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001078 sb->s_fs_info = nilfs;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001079
Ryusuke Konishif7545142011-03-09 11:05:08 +09001080 err = init_nilfs(nilfs, sb, (char *)data);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001081 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001082 goto failed_nilfs;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001083
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001084 sb->s_op = &nilfs_sops;
1085 sb->s_export_op = &nilfs_export_ops;
1086 sb->s_root = NULL;
Ryusuke Konishi61239232009-04-06 19:02:00 -07001087 sb->s_time_gran = 1;
Al Viro8de52772012-02-06 12:45:27 -05001088 sb->s_max_links = NILFS_LINK_MAX;
Ryusuke Konishi026a7d62010-10-07 14:19:48 +09001089
Christoph Hellwig26ff1302015-01-14 10:42:35 +01001090 sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001091
Ryusuke Konishif7545142011-03-09 11:05:08 +09001092 err = load_nilfs(nilfs, sb);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +09001093 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001094 goto failed_nilfs;
Ryusuke Konishif50a4c82009-11-19 16:58:40 +09001095
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001096 cno = nilfs_last_cno(nilfs);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001097 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001098 if (err) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001099 nilfs_msg(sb, KERN_ERR,
1100 "error %d while loading last checkpoint (checkpoint number=%llu)",
1101 err, (unsigned long long)cno);
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001102 goto failed_unload;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001103 }
1104
1105 if (!(sb->s_flags & MS_RDONLY)) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09001106 err = nilfs_attach_log_writer(sb, fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001107 if (err)
1108 goto failed_checkpoint;
1109 }
1110
Ryusuke Konishi367ea332010-08-26 01:52:51 +09001111 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1112 if (err)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001113 goto failed_segctor;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001114
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001115 nilfs_put_root(fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001116
1117 if (!(sb->s_flags & MS_RDONLY)) {
1118 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001119 nilfs_setup_super(sb, true);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001120 up_write(&nilfs->ns_sem);
1121 }
1122
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001123 return 0;
1124
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001125 failed_segctor:
Ryusuke Konishif7545142011-03-09 11:05:08 +09001126 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001127
1128 failed_checkpoint:
Ryusuke Konishi4d8d9292010-08-25 17:45:44 +09001129 nilfs_put_root(fsroot);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001130
Ryusuke Konishif1e89c82010-09-05 12:20:59 +09001131 failed_unload:
1132 iput(nilfs->ns_sufile);
1133 iput(nilfs->ns_cpfile);
1134 iput(nilfs->ns_dat);
1135
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001136 failed_nilfs:
1137 destroy_nilfs(nilfs);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001138 return err;
1139}
1140
1141static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1142{
Ryusuke Konishie3154e92011-03-09 11:05:08 +09001143 struct the_nilfs *nilfs = sb->s_fs_info;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001144 unsigned long old_sb_flags;
Ryusuke Konishi06df0f92010-12-27 00:04:06 +09001145 unsigned long old_mount_opt;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001146 int err;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001147
Theodore Ts'o02b99842014-03-13 10:14:33 -04001148 sync_filesystem(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001149 old_sb_flags = sb->s_flags;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09001150 old_mount_opt = nilfs->ns_mount_opt;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001151
Ryusuke Konishi7c017452010-07-05 20:08:33 +09001152 if (!parse_options(data, sb, 1)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001153 err = -EINVAL;
1154 goto restore_opts;
1155 }
1156 sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1157
Ryusuke Konishid240e062010-05-09 21:51:53 +09001158 err = -EINVAL;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001159
Ryusuke Konishi02345762009-11-20 03:28:01 +09001160 if (!nilfs_valid_fs(nilfs)) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001161 nilfs_msg(sb, KERN_WARNING,
1162 "couldn't remount because the filesystem is in an incomplete recovery state");
Ryusuke Konishi02345762009-11-20 03:28:01 +09001163 goto restore_opts;
1164 }
1165
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001166 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1167 goto out;
1168 if (*flags & MS_RDONLY) {
Ryusuke Konishif7545142011-03-09 11:05:08 +09001169 /* Shutting down log writer */
1170 nilfs_detach_log_writer(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001171 sb->s_flags |= MS_RDONLY;
1172
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001173 /*
1174 * Remounting a valid RW partition RDONLY, so set
1175 * the RDONLY flag and then mark the partition as valid again.
1176 */
1177 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001178 nilfs_cleanup_super(sb);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001179 up_write(&nilfs->ns_sem);
1180 } else {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001181 __u64 features;
Ryusuke Konishie912a5b2010-08-14 13:07:15 +09001182 struct nilfs_root *root;
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001183
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001184 /*
1185 * Mounting a RDONLY partition read-write, so reread and
1186 * store the current valid flag. (It may have been changed
1187 * by fsck since we originally mounted the partition.)
1188 */
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001189 down_read(&nilfs->ns_sem);
1190 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1191 ~NILFS_FEATURE_COMPAT_RO_SUPP;
1192 up_read(&nilfs->ns_sem);
1193 if (features) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001194 nilfs_msg(sb, KERN_WARNING,
1195 "couldn't remount RDWR because of unsupported optional features (%llx)",
1196 (unsigned long long)features);
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +09001197 err = -EROFS;
1198 goto restore_opts;
1199 }
1200
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001201 sb->s_flags &= ~MS_RDONLY;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001202
David Howells2b0143b2015-03-17 22:25:59 +00001203 root = NILFS_I(d_inode(sb->s_root))->i_root;
Ryusuke Konishif7545142011-03-09 11:05:08 +09001204 err = nilfs_attach_log_writer(sb, root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001205 if (err)
Ryusuke Konishie59399d2009-06-08 01:39:32 +09001206 goto restore_opts;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001207
1208 down_write(&nilfs->ns_sem);
Ryusuke Konishif7545142011-03-09 11:05:08 +09001209 nilfs_setup_super(sb, true);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001210 up_write(&nilfs->ns_sem);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001211 }
1212 out:
1213 return 0;
1214
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001215 restore_opts:
1216 sb->s_flags = old_sb_flags;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +09001217 nilfs->ns_mount_opt = old_mount_opt;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001218 return err;
1219}
1220
1221struct nilfs_super_data {
1222 struct block_device *bdev;
1223 __u64 cno;
1224 int flags;
1225};
1226
1227/**
1228 * nilfs_identify - pre-read mount options needed to identify mount instance
1229 * @data: mount options
1230 * @sd: nilfs_super_data
1231 */
1232static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1233{
1234 char *p, *options = data;
1235 substring_t args[MAX_OPT_ARGS];
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001236 int token;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001237 int ret = 0;
1238
1239 do {
1240 p = strsep(&options, ",");
1241 if (p != NULL && *p) {
1242 token = match_token(p, tokens, args);
1243 if (token == Opt_snapshot) {
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001244 if (!(sd->flags & MS_RDONLY)) {
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001245 ret++;
Ryusuke Konishic05dbfc2010-09-16 00:36:24 +09001246 } else {
1247 sd->cno = simple_strtoull(args[0].from,
1248 NULL, 0);
1249 /*
1250 * No need to see the end pointer;
1251 * match_token() has done syntax
1252 * checking.
1253 */
1254 if (sd->cno == 0)
1255 ret++;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001256 }
1257 }
1258 if (ret)
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001259 nilfs_msg(NULL, KERN_ERR,
1260 "invalid mount option: %s", p);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001261 }
1262 if (!options)
1263 break;
1264 BUG_ON(options == data);
1265 *(options - 1) = ',';
1266 } while (!ret);
1267 return ret;
1268}
1269
1270static int nilfs_set_bdev_super(struct super_block *s, void *data)
1271{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001272 s->s_bdev = data;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001273 s->s_dev = s->s_bdev->bd_dev;
1274 return 0;
1275}
1276
1277static int nilfs_test_bdev_super(struct super_block *s, void *data)
1278{
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001279 return (void *)s->s_bdev == data;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001280}
1281
Al Viroe4c59d62010-07-26 12:19:34 +04001282static struct dentry *
1283nilfs_mount(struct file_system_type *fs_type, int flags,
1284 const char *dev_name, void *data)
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001285{
1286 struct nilfs_super_data sd;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001287 struct super_block *s;
Tejun Heod4d77622010-11-13 11:55:18 +01001288 fmode_t mode = FMODE_READ | FMODE_EXCL;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001289 struct dentry *root_dentry;
1290 int err, s_new = false;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001291
Ryusuke Konishi13e90552010-05-09 02:57:57 +09001292 if (!(flags & MS_RDONLY))
1293 mode |= FMODE_WRITE;
1294
Tejun Heod4d77622010-11-13 11:55:18 +01001295 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
Jan Blunckd6d4c192010-02-24 13:25:32 +01001296 if (IS_ERR(sd.bdev))
Al Viroe4c59d62010-07-26 12:19:34 +04001297 return ERR_CAST(sd.bdev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001298
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001299 sd.cno = 0;
1300 sd.flags = flags;
1301 if (nilfs_identify((char *)data, &sd)) {
1302 err = -EINVAL;
1303 goto failed;
1304 }
1305
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +09001306 /*
1307 * once the super is inserted into the list by sget, s_umount
1308 * will protect the lockfs code from trying to start a snapshot
1309 * while we are mounting
1310 */
1311 mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
1312 if (sd.bdev->bd_fsfreeze_count > 0) {
1313 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
1314 err = -EBUSY;
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001315 goto failed;
1316 }
David Howells9249e172012-06-25 12:55:37 +01001317 s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1318 sd.bdev);
Ryusuke Konishi5beb6e02010-09-20 18:19:06 +09001319 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001320 if (IS_ERR(s)) {
1321 err = PTR_ERR(s);
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001322 goto failed;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001323 }
1324
1325 if (!s->s_root) {
Ryusuke Konishi8fa7c322016-05-23 16:23:00 -07001326 s_new = true;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001327
Ryusuke Konishi33c8e572009-06-08 01:39:29 +09001328 /* New superblock instance created */
Ryusuke Konishi4571b822010-05-09 03:01:32 +09001329 s->s_mode = mode;
Dmitry Monakhova1c6f0572015-04-13 16:31:37 +04001330 snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001331 sb_set_blocksize(s, block_size(sd.bdev));
1332
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001333 err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001334 if (err)
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001335 goto failed_super;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001336
1337 s->s_flags |= MS_ACTIVE;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001338 } else if (!sd.cno) {
Al Viroe95c3112013-08-24 12:06:16 -04001339 if (nilfs_tree_is_busy(s->s_root)) {
1340 if ((flags ^ s->s_flags) & MS_RDONLY) {
Ryusuke Konishifeee8802016-08-02 14:05:10 -07001341 nilfs_msg(s, KERN_ERR,
1342 "the device already has a %s mount.",
1343 (s->s_flags & MS_RDONLY) ?
1344 "read-only" : "read/write");
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001345 err = -EBUSY;
1346 goto failed_super;
1347 }
Al Viroe95c3112013-08-24 12:06:16 -04001348 } else {
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001349 /*
1350 * Try remount to setup mount states if the current
1351 * tree is not mounted and only snapshots use this sb.
1352 */
1353 err = nilfs_remount(s, &flags, data);
1354 if (err)
1355 goto failed_super;
1356 }
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001357 }
1358
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001359 if (sd.cno) {
1360 err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001361 if (err)
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001362 goto failed_super;
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001363 } else {
1364 root_dentry = dget(s->s_root);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001365 }
1366
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001367 if (!s_new)
Tejun Heod4d77622010-11-13 11:55:18 +01001368 blkdev_put(sd.bdev, mode);
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001369
Al Viroe4c59d62010-07-26 12:19:34 +04001370 return root_dentry;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001371
Ryusuke Konishif11459a2010-08-16 01:54:52 +09001372 failed_super:
Al Viroa95161a2009-08-09 00:52:02 +04001373 deactivate_locked_super(s);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001374
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +09001375 failed:
1376 if (!s_new)
Tejun Heod4d77622010-11-13 11:55:18 +01001377 blkdev_put(sd.bdev, mode);
Al Viroe4c59d62010-07-26 12:19:34 +04001378 return ERR_PTR(err);
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001379}
1380
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001381struct file_system_type nilfs_fs_type = {
1382 .owner = THIS_MODULE,
1383 .name = "nilfs2",
Al Viroe4c59d62010-07-26 12:19:34 +04001384 .mount = nilfs_mount,
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001385 .kill_sb = kill_block_super,
1386 .fs_flags = FS_REQUIRES_DEV,
1387};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001388MODULE_ALIAS_FS("nilfs2");
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001389
Li Hong41c88bd2010-04-06 00:54:11 +08001390static void nilfs_inode_init_once(void *obj)
1391{
1392 struct nilfs_inode_info *ii = obj;
1393
1394 INIT_LIST_HEAD(&ii->i_dirty);
1395#ifdef CONFIG_NILFS_XATTR
1396 init_rwsem(&ii->xattr_sem);
1397#endif
Miklos Szeredi2aa15892011-02-23 13:49:47 +01001398 address_space_init_once(&ii->i_btnode_cache);
Ryusuke Konishi05d0e942010-07-10 20:52:09 +09001399 ii->i_bmap = &ii->i_bmap_data;
Li Hong41c88bd2010-04-06 00:54:11 +08001400 inode_init_once(&ii->vfs_inode);
1401}
1402
1403static void nilfs_segbuf_init_once(void *obj)
1404{
1405 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1406}
1407
1408static void nilfs_destroy_cachep(void)
1409{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001410 /*
1411 * Make sure all delayed rcu free inodes are flushed before we
1412 * destroy cache.
1413 */
1414 rcu_barrier();
1415
Julia Lawallda80a392015-11-06 16:31:40 -08001416 kmem_cache_destroy(nilfs_inode_cachep);
1417 kmem_cache_destroy(nilfs_transaction_cachep);
1418 kmem_cache_destroy(nilfs_segbuf_cachep);
1419 kmem_cache_destroy(nilfs_btree_path_cache);
Li Hong41c88bd2010-04-06 00:54:11 +08001420}
1421
1422static int __init nilfs_init_cachep(void)
1423{
1424 nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1425 sizeof(struct nilfs_inode_info), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08001426 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1427 nilfs_inode_init_once);
Li Hong41c88bd2010-04-06 00:54:11 +08001428 if (!nilfs_inode_cachep)
1429 goto fail;
1430
1431 nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1432 sizeof(struct nilfs_transaction_info), 0,
1433 SLAB_RECLAIM_ACCOUNT, NULL);
1434 if (!nilfs_transaction_cachep)
1435 goto fail;
1436
1437 nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1438 sizeof(struct nilfs_segment_buffer), 0,
1439 SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1440 if (!nilfs_segbuf_cachep)
1441 goto fail;
1442
1443 nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1444 sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1445 0, 0, NULL);
1446 if (!nilfs_btree_path_cache)
1447 goto fail;
1448
1449 return 0;
1450
1451fail:
1452 nilfs_destroy_cachep();
1453 return -ENOMEM;
1454}
1455
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001456static int __init init_nilfs_fs(void)
1457{
1458 int err;
1459
Li Hong41c88bd2010-04-06 00:54:11 +08001460 err = nilfs_init_cachep();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001461 if (err)
Li Hong41c88bd2010-04-06 00:54:11 +08001462 goto fail;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001463
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001464 err = nilfs_sysfs_init();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001465 if (err)
Li Hong41c88bd2010-04-06 00:54:11 +08001466 goto free_cachep;
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001467
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001468 err = register_filesystem(&nilfs_fs_type);
1469 if (err)
1470 goto deinit_sysfs_entry;
1471
Li Hong9f130262010-04-09 23:09:53 +08001472 printk(KERN_INFO "NILFS version 2 loaded\n");
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001473 return 0;
1474
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001475deinit_sysfs_entry:
1476 nilfs_sysfs_exit();
Li Hong41c88bd2010-04-06 00:54:11 +08001477free_cachep:
1478 nilfs_destroy_cachep();
1479fail:
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001480 return err;
1481}
1482
1483static void __exit exit_nilfs_fs(void)
1484{
Li Hong41c88bd2010-04-06 00:54:11 +08001485 nilfs_destroy_cachep();
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -07001486 nilfs_sysfs_exit();
Ryusuke Konishi783f6182009-04-06 19:01:35 -07001487 unregister_filesystem(&nilfs_fs_type);
1488}
1489
1490module_init(init_nilfs_fs)
1491module_exit(exit_nilfs_fs)