]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/nilfs2/super.c
nilfs2: reduce bare use of printk() with nilfs_msg()
[mirror_ubuntu-artful-kernel.git] / fs / nilfs2 / super.c
CommitLineData
783f6184
RK
1/*
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 *
4b420ab4 16 * Written by Ryusuke Konishi.
783f6184
RK
17 */
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>
783f6184 42#include <linux/crc32.h>
783f6184
RK
43#include <linux/vfs.h>
44#include <linux/writeback.h>
b58a285b
JS
45#include <linux/seq_file.h>
46#include <linux/mount.h>
783f6184 47#include "nilfs.h"
8e656fd5 48#include "export.h"
783f6184
RK
49#include "mdt.h"
50#include "alloc.h"
05d0e94b
RK
51#include "btree.h"
52#include "btnode.h"
783f6184
RK
53#include "page.h"
54#include "cpfile.h"
4e33f9ea 55#include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
783f6184
RK
56#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)");
783f6184
RK
64MODULE_LICENSE("GPL");
65
abc0b50b 66static struct kmem_cache *nilfs_inode_cachep;
41c88bd7
LH
67struct kmem_cache *nilfs_transaction_cachep;
68struct kmem_cache *nilfs_segbuf_cachep;
69struct kmem_cache *nilfs_btree_path_cache;
70
f7545144 71static int nilfs_setup_super(struct super_block *sb, int is_mount);
783f6184 72static int nilfs_remount(struct super_block *sb, int *flags, char *data);
783f6184 73
a66dfb0a
RK
74void __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
f7545144 90static void nilfs_set_error(struct super_block *sb)
c8a11c8a 91{
e3154e97 92 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 93 struct nilfs_super_block **sbp;
c8a11c8a
RK
94
95 down_write(&nilfs->ns_sem);
96 if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
97 nilfs->ns_mount_state |= NILFS_ERROR_FS;
f7545144 98 sbp = nilfs_prepare_super(sb, 0);
d26493b6
JS
99 if (likely(sbp)) {
100 sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
b2ac86e1
JS
101 if (sbp[1])
102 sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
f7545144 103 nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
d26493b6 104 }
c8a11c8a
RK
105 }
106 up_write(&nilfs->ns_sem);
107}
108
783f6184 109/**
cae3d4ca 110 * __nilfs_error() - report failure condition on a filesystem
783f6184 111 *
cae3d4ca
RK
112 * __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.
783f6184 115 *
cae3d4ca
RK
116 * 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.
783f6184 122 */
cae3d4ca
RK
123void __nilfs_error(struct super_block *sb, const char *function,
124 const char *fmt, ...)
783f6184 125{
e3154e97 126 struct the_nilfs *nilfs = sb->s_fs_info;
b004a5eb 127 struct va_format vaf;
783f6184
RK
128 va_list args;
129
130 va_start(args, fmt);
b004a5eb
JP
131
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
783f6184
RK
138 va_end(args);
139
140 if (!(sb->s_flags & MS_RDONLY)) {
f7545144 141 nilfs_set_error(sb);
783f6184 142
3b2ce58b 143 if (nilfs_test_opt(nilfs, ERRORS_RO)) {
783f6184
RK
144 printk(KERN_CRIT "Remounting filesystem read-only\n");
145 sb->s_flags |= MS_RDONLY;
146 }
147 }
148
3b2ce58b 149 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
783f6184
RK
150 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{
b004a5eb 157 struct va_format vaf;
783f6184
RK
158 va_list args;
159
160 va_start(args, fmt);
b004a5eb
JP
161
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
783f6184
RK
168 va_end(args);
169}
170
783f6184 171
2879ed66 172struct inode *nilfs_alloc_inode(struct super_block *sb)
783f6184
RK
173{
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;
0e14a359 181 ii->i_cno = 0;
783f6184 182 ii->vfs_inode.i_version = 1;
b83ae6d4 183 nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode);
783f6184
RK
184 return &ii->vfs_inode;
185}
186
fa0d7e3d 187static void nilfs_i_callback(struct rcu_head *head)
783f6184 188{
fa0d7e3d 189 struct inode *inode = container_of(head, struct inode, i_rcu);
b91c9a97 190
2d19961d
RK
191 if (nilfs_is_metadata_file_inode(inode))
192 nilfs_mdt_destroy(inode);
193
783f6184
RK
194 kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
195}
196
fa0d7e3d
NP
197void nilfs_destroy_inode(struct inode *inode)
198{
199 call_rcu(&inode->i_rcu, nilfs_i_callback);
200}
201
f7545144 202static int nilfs_sync_super(struct super_block *sb, int flag)
783f6184 203{
e3154e97 204 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 205 int err;
783f6184 206
783f6184 207 retry:
e339ad31 208 set_buffer_dirty(nilfs->ns_sbh[0]);
3b2ce58b 209 if (nilfs_test_opt(nilfs, BARRIER)) {
87e99511 210 err = __sync_dirty_buffer(nilfs->ns_sbh[0],
f8c131f5 211 WRITE_SYNC | WRITE_FLUSH_FUA);
87e99511
CH
212 } else {
213 err = sync_dirty_buffer(nilfs->ns_sbh[0]);
783f6184 214 }
87e99511 215
e339ad31 216 if (unlikely(err)) {
feee880f
RK
217 nilfs_msg(sb, KERN_ERR, "unable to write superblock: err=%d",
218 err);
e339ad31 219 if (err == -EIO && nilfs->ns_sbh[1]) {
b2ac86e1
JS
220 /*
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);
e339ad31
RK
226 nilfs_fall_back_super_block(nilfs);
227 goto retry;
228 }
229 } else {
230 struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
231
b2ac86e1
JS
232 nilfs->ns_sbwcount++;
233
e339ad31
RK
234 /*
235 * The latest segment becomes trailable from the position
236 * written in superblock.
237 */
783f6184 238 clear_nilfs_discontinued(nilfs);
e339ad31
RK
239
240 /* update GC protection for recent segments */
241 if (nilfs->ns_sbh[1]) {
b2ac86e1 242 if (flag == NILFS_SB_COMMIT_ALL) {
e339ad31 243 set_buffer_dirty(nilfs->ns_sbh[1]);
b2ac86e1
JS
244 if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
245 goto out;
e339ad31 246 }
b2ac86e1
JS
247 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];
e339ad31 250 }
783f6184 251
b2ac86e1
JS
252 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:
783f6184
RK
257 return err;
258}
259
60f46b7e
RK
260void 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
f7545144 276struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
b2ac86e1 277 int flip)
783f6184 278{
e3154e97 279 struct the_nilfs *nilfs = sb->s_fs_info;
e339ad31 280 struct nilfs_super_block **sbp = nilfs->ns_sbp;
783f6184 281
d26493b6 282 /* nilfs->ns_sem must be locked by the caller. */
34cb9b5c 283 if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
d26493b6
JS
284 if (sbp[1] &&
285 sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
b2ac86e1 286 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
d26493b6 287 } else {
feee880f 288 nilfs_msg(sb, KERN_CRIT, "superblock broke");
d26493b6 289 return NULL;
e339ad31 290 }
b2ac86e1
JS
291 } else if (sbp[1] &&
292 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
7592ecde 293 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
e339ad31 294 }
b2ac86e1
JS
295
296 if (flip && sbp[1])
297 nilfs_swap_super_block(nilfs);
298
d26493b6
JS
299 return sbp;
300}
301
f7545144 302int nilfs_commit_super(struct super_block *sb, int flag)
d26493b6 303{
e3154e97 304 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6
JS
305 struct nilfs_super_block **sbp = nilfs->ns_sbp;
306 time_t t;
307
308 /* nilfs->ns_sem must be locked by the caller. */
e339ad31 309 t = get_seconds();
b2ac86e1 310 nilfs->ns_sbwtime = t;
e339ad31
RK
311 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));
b2ac86e1
JS
316 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));
e339ad31 322 }
e605f0a7 323 clear_nilfs_sb_dirty(nilfs);
e2c7617a
AR
324 nilfs->ns_flushed_device = 1;
325 /* make sure store to ns_flushed_device cannot be reordered */
326 smp_wmb();
f7545144 327 return nilfs_sync_super(sb, flag);
783f6184
RK
328}
329
7ecaa46c
RK
330/**
331 * nilfs_cleanup_super() - write filesystem state for cleanup
f7545144 332 * @sb: super block instance to be unmounted or degraded to read-only
7ecaa46c
RK
333 *
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 */
f7545144 338int nilfs_cleanup_super(struct super_block *sb)
7ecaa46c 339{
e3154e97 340 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 341 struct nilfs_super_block **sbp;
b2ac86e1 342 int flag = NILFS_SB_COMMIT;
d26493b6 343 int ret = -EIO;
7ecaa46c 344
f7545144 345 sbp = nilfs_prepare_super(sb, 0);
d26493b6 346 if (sbp) {
f7545144
RK
347 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
348 nilfs_set_log_cursor(sbp[0], nilfs);
b2ac86e1
JS
349 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 }
f7545144 358 ret = nilfs_commit_super(sb, flag);
d26493b6 359 }
7ecaa46c
RK
360 return ret;
361}
362
cfb0a4bf
RK
363/**
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;
4f05028f 375 int sb2i; /* array index of the secondary superblock */
cfb0a4bf
RK
376 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;
4f05028f
RK
386 } else {
387 sb2i = -1;
388 blocknr = 0;
cfb0a4bf
RK
389 }
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) {
feee880f
RK
398 nilfs_msg(sb, KERN_WARNING,
399 "unable to move secondary superblock to block %llu",
400 (unsigned long long)newblocknr);
cfb0a4bf
RK
401 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
4e33f9ea
RK
423/**
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
783f6184
RK
494static void nilfs_put_super(struct super_block *sb)
495{
e3154e97 496 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 497
f7545144 498 nilfs_detach_log_writer(sb);
783f6184
RK
499
500 if (!(sb->s_flags & MS_RDONLY)) {
501 down_write(&nilfs->ns_sem);
f7545144 502 nilfs_cleanup_super(sb);
783f6184
RK
503 up_write(&nilfs->ns_sem);
504 }
505
f1e89c86
RK
506 iput(nilfs->ns_sufile);
507 iput(nilfs->ns_cpfile);
508 iput(nilfs->ns_dat);
509
348fe8da 510 destroy_nilfs(nilfs);
783f6184 511 sb->s_fs_info = NULL;
783f6184
RK
512}
513
783f6184
RK
514static int nilfs_sync_fs(struct super_block *sb, int wait)
515{
e3154e97 516 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 517 struct nilfs_super_block **sbp;
783f6184
RK
518 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);
6233caa9
JS
523
524 down_write(&nilfs->ns_sem);
d26493b6 525 if (nilfs_sb_dirty(nilfs)) {
f7545144 526 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
b2ac86e1
JS
527 if (likely(sbp)) {
528 nilfs_set_log_cursor(sbp[0], nilfs);
f7545144 529 nilfs_commit_super(sb, NILFS_SB_COMMIT);
b2ac86e1 530 }
d26493b6 531 }
6233caa9
JS
532 up_write(&nilfs->ns_sem);
533
e2c7617a
AR
534 if (!err)
535 err = nilfs_flush_device(nilfs);
536
783f6184
RK
537 return err;
538}
539
f7545144 540int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
4d8d9293 541 struct nilfs_root **rootp)
783f6184 542{
e3154e97 543 struct the_nilfs *nilfs = sb->s_fs_info;
4d8d9293 544 struct nilfs_root *root;
783f6184
RK
545 struct nilfs_checkpoint *raw_cp;
546 struct buffer_head *bh_cp;
4d8d9293 547 int err = -ENOMEM;
783f6184 548
4d8d9293
RK
549 root = nilfs_find_or_create_root(
550 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
551 if (!root)
552 return err;
783f6184 553
e912a5b6
RK
554 if (root->ifile)
555 goto reuse; /* already attached checkpoint */
783f6184 556
1154ecbd 557 down_read(&nilfs->ns_segctor_sem);
783f6184
RK
558 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
559 &bh_cp);
1154ecbd 560 up_read(&nilfs->ns_segctor_sem);
783f6184
RK
561 if (unlikely(err)) {
562 if (err == -ENOENT || err == -EINVAL) {
feee880f
RK
563 nilfs_msg(sb, KERN_ERR,
564 "Invalid checkpoint (checkpoint number=%llu)",
565 (unsigned long long)cno);
783f6184
RK
566 err = -EINVAL;
567 }
568 goto failed;
569 }
f1e89c86 570
f7545144 571 err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
f1e89c86
RK
572 &raw_cp->cp_ifile_inode, &root->ifile);
573 if (err)
783f6184 574 goto failed_bh;
b7c06342 575
e5f7f848
VD
576 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));
783f6184
RK
580
581 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
4d8d9293 582
e912a5b6 583 reuse:
4d8d9293 584 *rootp = root;
783f6184
RK
585 return 0;
586
587 failed_bh:
588 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
589 failed:
4d8d9293 590 nilfs_put_root(root);
783f6184
RK
591
592 return err;
593}
783f6184 594
5beb6e0b
RK
595static int nilfs_freeze(struct super_block *sb)
596{
e3154e97 597 struct the_nilfs *nilfs = sb->s_fs_info;
5beb6e0b
RK
598 int err;
599
600 if (sb->s_flags & MS_RDONLY)
601 return 0;
783f6184 602
5beb6e0b
RK
603 /* Mark super block clean */
604 down_write(&nilfs->ns_sem);
f7545144 605 err = nilfs_cleanup_super(sb);
5beb6e0b 606 up_write(&nilfs->ns_sem);
783f6184
RK
607 return err;
608}
609
5beb6e0b 610static int nilfs_unfreeze(struct super_block *sb)
783f6184 611{
e3154e97 612 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 613
5beb6e0b
RK
614 if (sb->s_flags & MS_RDONLY)
615 return 0;
616
617 down_write(&nilfs->ns_sem);
f7545144 618 nilfs_setup_super(sb, false);
5beb6e0b
RK
619 up_write(&nilfs->ns_sem);
620 return 0;
783f6184
RK
621}
622
783f6184
RK
623static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
624{
625 struct super_block *sb = dentry->d_sb;
2b0143b5 626 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b7c06342 627 struct the_nilfs *nilfs = root->nilfs;
c306af23 628 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
783f6184
RK
629 unsigned long long blocks;
630 unsigned long overhead;
631 unsigned long nrsvblocks;
632 sector_t nfreeblocks;
c7ef972c 633 u64 nmaxinodes, nfreeinodes;
783f6184
RK
634 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 *
7a65004b 649 * When distributing meta data blocks outside segment structure,
783f6184
RK
650 * 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
c7ef972c
VD
658 err = nilfs_ifile_count_free_inodes(root->ifile,
659 &nmaxinodes, &nfreeinodes);
660 if (unlikely(err)) {
feee880f
RK
661 nilfs_msg(sb, KERN_WARNING,
662 "failed to count free inodes: err=%d", err);
c7ef972c
VD
663 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 */
e5f7f848 670 nmaxinodes = atomic64_read(&root->inodes_count);
c7ef972c
VD
671 nfreeinodes = 0;
672 err = 0;
673 } else
674 return err;
675 }
676
783f6184
RK
677 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;
c7ef972c
VD
683 buf->f_files = nmaxinodes;
684 buf->f_ffree = nfreeinodes;
783f6184 685 buf->f_namelen = NILFS_NAME_LEN;
c306af23
RK
686 buf->f_fsid.val[0] = (u32)id;
687 buf->f_fsid.val[1] = (u32)(id >> 32);
688
783f6184
RK
689 return 0;
690}
691
34c80b1d 692static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
b58a285b 693{
34c80b1d 694 struct super_block *sb = dentry->d_sb;
e3154e97 695 struct the_nilfs *nilfs = sb->s_fs_info;
2b0143b5 696 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b58a285b 697
3b2ce58b 698 if (!nilfs_test_opt(nilfs, BARRIER))
c6b4d57d 699 seq_puts(seq, ",nobarrier");
f11459ad
RK
700 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
701 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
3b2ce58b 702 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
c6b4d57d 703 seq_puts(seq, ",errors=panic");
3b2ce58b 704 if (nilfs_test_opt(nilfs, ERRORS_CONT))
c6b4d57d 705 seq_puts(seq, ",errors=continue");
3b2ce58b 706 if (nilfs_test_opt(nilfs, STRICT_ORDER))
c6b4d57d 707 seq_puts(seq, ",order=strict");
3b2ce58b 708 if (nilfs_test_opt(nilfs, NORECOVERY))
c6b4d57d 709 seq_puts(seq, ",norecovery");
3b2ce58b 710 if (nilfs_test_opt(nilfs, DISCARD))
c6b4d57d 711 seq_puts(seq, ",discard");
b58a285b
JS
712
713 return 0;
714}
715
b87221de 716static const struct super_operations nilfs_sops = {
783f6184
RK
717 .alloc_inode = nilfs_alloc_inode,
718 .destroy_inode = nilfs_destroy_inode,
719 .dirty_inode = nilfs_dirty_inode,
6fd1e5c9 720 .evict_inode = nilfs_evict_inode,
783f6184 721 .put_super = nilfs_put_super,
783f6184 722 .sync_fs = nilfs_sync_fs,
5beb6e0b
RK
723 .freeze_fs = nilfs_freeze,
724 .unfreeze_fs = nilfs_unfreeze,
783f6184
RK
725 .statfs = nilfs_statfs,
726 .remount_fs = nilfs_remount,
b58a285b 727 .show_options = nilfs_show_options
783f6184
RK
728};
729
783f6184
RK
730enum {
731 Opt_err_cont, Opt_err_panic, Opt_err_ro,
773bc4f3 732 Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
802d3177 733 Opt_discard, Opt_nodiscard, Opt_err,
783f6184
RK
734};
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"},
773bc4f3 740 {Opt_barrier, "barrier"},
91f1953b 741 {Opt_nobarrier, "nobarrier"},
783f6184
RK
742 {Opt_snapshot, "cp=%u"},
743 {Opt_order, "order=%s"},
0234576d 744 {Opt_norecovery, "norecovery"},
e902ec99 745 {Opt_discard, "discard"},
802d3177 746 {Opt_nodiscard, "nodiscard"},
783f6184
RK
747 {Opt_err, NULL}
748};
749
7c017457 750static int parse_options(char *options, struct super_block *sb, int is_remount)
783f6184 751{
e3154e97 752 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184
RK
753 char *p;
754 substring_t args[MAX_OPT_ARGS];
783f6184
RK
755
756 if (!options)
757 return 1;
758
759 while ((p = strsep(&options, ",")) != NULL) {
760 int token;
4ad364ca 761
783f6184
RK
762 if (!*p)
763 continue;
764
765 token = match_token(p, tokens, args);
766 switch (token) {
773bc4f3 767 case Opt_barrier:
3b2ce58b 768 nilfs_set_opt(nilfs, BARRIER);
773bc4f3 769 break;
91f1953b 770 case Opt_nobarrier:
3b2ce58b 771 nilfs_clear_opt(nilfs, BARRIER);
783f6184
RK
772 break;
773 case Opt_order:
774 if (strcmp(args[0].from, "relaxed") == 0)
775 /* Ordered data semantics */
3b2ce58b 776 nilfs_clear_opt(nilfs, STRICT_ORDER);
783f6184
RK
777 else if (strcmp(args[0].from, "strict") == 0)
778 /* Strict in-order semantics */
3b2ce58b 779 nilfs_set_opt(nilfs, STRICT_ORDER);
783f6184
RK
780 else
781 return 0;
782 break;
783 case Opt_err_panic:
3b2ce58b 784 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
783f6184
RK
785 break;
786 case Opt_err_ro:
3b2ce58b 787 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
783f6184
RK
788 break;
789 case Opt_err_cont:
3b2ce58b 790 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
783f6184
RK
791 break;
792 case Opt_snapshot:
7c017457 793 if (is_remount) {
feee880f
RK
794 nilfs_msg(sb, KERN_ERR,
795 "\"%s\" option is invalid for remount",
796 p);
783f6184 797 return 0;
7c017457 798 }
783f6184 799 break;
0234576d 800 case Opt_norecovery:
3b2ce58b 801 nilfs_set_opt(nilfs, NORECOVERY);
0234576d 802 break;
e902ec99 803 case Opt_discard:
3b2ce58b 804 nilfs_set_opt(nilfs, DISCARD);
e902ec99 805 break;
802d3177 806 case Opt_nodiscard:
3b2ce58b 807 nilfs_clear_opt(nilfs, DISCARD);
802d3177 808 break;
783f6184 809 default:
feee880f
RK
810 nilfs_msg(sb, KERN_ERR,
811 "unrecognized mount option \"%s\"", p);
783f6184
RK
812 return 0;
813 }
814 }
815 return 1;
816}
817
818static inline void
3b2ce58b 819nilfs_set_default_options(struct super_block *sb,
783f6184
RK
820 struct nilfs_super_block *sbp)
821{
e3154e97 822 struct the_nilfs *nilfs = sb->s_fs_info;
3b2ce58b
RK
823
824 nilfs->ns_mount_opt =
277a6a34 825 NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
783f6184
RK
826}
827
f7545144 828static int nilfs_setup_super(struct super_block *sb, int is_mount)
783f6184 829{
e3154e97 830 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6
JS
831 struct nilfs_super_block **sbp;
832 int max_mnt_count;
833 int mnt_count;
834
835 /* nilfs->ns_sem must be locked by the caller. */
f7545144 836 sbp = nilfs_prepare_super(sb, 0);
d26493b6
JS
837 if (!sbp)
838 return -EIO;
839
5beb6e0b
RK
840 if (!is_mount)
841 goto skip_mount_setup;
842
d26493b6
JS
843 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
844 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
783f6184 845
f50a4c81 846 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
feee880f 847 nilfs_msg(sb, KERN_WARNING, "mounting fs with errors");
783f6184
RK
848#if 0
849 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
feee880f 850 nilfs_msg(sb, KERN_WARNING, "maximal mount count reached");
783f6184
RK
851#endif
852 }
853 if (!max_mnt_count)
d26493b6 854 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
783f6184 855
d26493b6 856 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
5beb6e0b
RK
857 sbp[0]->s_mtime = cpu_to_le64(get_seconds());
858
859skip_mount_setup:
d26493b6
JS
860 sbp[0]->s_state =
861 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
b2ac86e1 862 /* synchronize sbp[1] with sbp[0] */
0ca7a5b9
RK
863 if (sbp[1])
864 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
f7545144 865 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
783f6184
RK
866}
867
e339ad31
RK
868struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
869 u64 pos, int blocksize,
870 struct buffer_head **pbh)
783f6184 871{
e339ad31
RK
872 unsigned long long sb_index = pos;
873 unsigned long offset;
783f6184 874
e339ad31 875 offset = do_div(sb_index, blocksize);
783f6184 876 *pbh = sb_bread(sb, sb_index);
e339ad31 877 if (!*pbh)
783f6184 878 return NULL;
783f6184
RK
879 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
880}
881
783f6184
RK
882int nilfs_store_magic_and_option(struct super_block *sb,
883 struct nilfs_super_block *sbp,
884 char *data)
885{
e3154e97 886 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 887
783f6184
RK
888 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
3b2ce58b 895 nilfs_set_default_options(sb, sbp);
783f6184 896
574e6c31
RK
897 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);
783f6184 901
c9cb9b5c 902 return !parse_options(data, sb, 0) ? -EINVAL : 0;
783f6184
RK
903}
904
c5ca48aa
RK
905int 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) {
feee880f
RK
913 nilfs_msg(sb, KERN_ERR,
914 "couldn't mount because of unsupported optional features (%llx)",
915 (unsigned long long)features);
c5ca48aa
RK
916 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) {
feee880f
RK
921 nilfs_msg(sb, KERN_ERR,
922 "couldn't mount RDWR because of unsupported optional features (%llx)",
923 (unsigned long long)features);
c5ca48aa
RK
924 return -EINVAL;
925 }
926 return 0;
927}
928
367ea334
RK
929static 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)) {
367ea334 939 ret = PTR_ERR(inode);
feee880f 940 nilfs_msg(sb, KERN_ERR, "error %d getting root inode", ret);
367ea334
RK
941 goto out;
942 }
943 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
944 iput(inode);
feee880f 945 nilfs_msg(sb, KERN_ERR, "corrupt root inode");
367ea334
RK
946 ret = -EINVAL;
947 goto out;
948 }
949
f11459ad
RK
950 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
951 dentry = d_find_alias(inode);
952 if (!dentry) {
48fde701 953 dentry = d_make_root(inode);
f11459ad 954 if (!dentry) {
f11459ad
RK
955 ret = -ENOMEM;
956 goto failed_dentry;
957 }
958 } else {
959 iput(inode);
960 }
961 } else {
1a0a397e 962 dentry = d_obtain_root(inode);
f11459ad
RK
963 if (IS_ERR(dentry)) {
964 ret = PTR_ERR(dentry);
965 goto failed_dentry;
966 }
367ea334
RK
967 }
968 *root_dentry = dentry;
969 out:
970 return ret;
f11459ad
RK
971
972 failed_dentry:
feee880f 973 nilfs_msg(sb, KERN_ERR, "error %d getting root dentry", ret);
f11459ad 974 goto out;
367ea334
RK
975}
976
ab4d8f7e
RK
977static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
978 struct dentry **root_dentry)
979{
e3154e97 980 struct the_nilfs *nilfs = s->s_fs_info;
ab4d8f7e
RK
981 struct nilfs_root *root;
982 int ret;
983
572d8b39
RK
984 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
985
ab4d8f7e
RK
986 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) {
feee880f
RK
993 nilfs_msg(s, KERN_ERR,
994 "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
995 (unsigned long long)cno);
ab4d8f7e
RK
996 ret = -EINVAL;
997 goto out;
998 }
999
f7545144 1000 ret = nilfs_attach_checkpoint(s, cno, false, &root);
ab4d8f7e 1001 if (ret) {
feee880f
RK
1002 nilfs_msg(s, KERN_ERR,
1003 "error %d while loading snapshot (checkpoint number=%llu)",
1004 ret, (unsigned long long)cno);
ab4d8f7e
RK
1005 goto out;
1006 }
1007 ret = nilfs_get_root_dentry(s, root, root_dentry);
1008 nilfs_put_root(root);
1009 out:
572d8b39 1010 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
ab4d8f7e
RK
1011 return ret;
1012}
1013
f11459ad 1014/**
e95c311e 1015 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
f11459ad
RK
1016 * @root_dentry: root dentry of the tree to be shrunk
1017 *
1018 * This function returns true if the tree was in-use.
1019 */
e95c311e 1020static bool nilfs_tree_is_busy(struct dentry *root_dentry)
f11459ad 1021{
f11459ad 1022 shrink_dcache_parent(root_dentry);
e95c311e 1023 return d_count(root_dentry) > 1;
f11459ad
RK
1024}
1025
032dbb3b
RK
1026int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1027{
e3154e97 1028 struct the_nilfs *nilfs = sb->s_fs_info;
032dbb3b
RK
1029 struct nilfs_root *root;
1030 struct inode *inode;
1031 struct dentry *dentry;
1032 int ret;
1033
3377f843 1034 if (cno > nilfs->ns_cno)
032dbb3b
RK
1035 return false;
1036
1037 if (cno >= nilfs_last_cno(nilfs))
1038 return true; /* protect recent checkpoints */
1039
1040 ret = false;
e3154e97 1041 root = nilfs_lookup_root(nilfs, cno);
032dbb3b
RK
1042 if (root) {
1043 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1044 if (inode) {
1045 dentry = d_find_alias(inode);
1046 if (dentry) {
e95c311e 1047 ret = nilfs_tree_is_busy(dentry);
032dbb3b
RK
1048 dput(dentry);
1049 }
1050 iput(inode);
1051 }
1052 nilfs_put_root(root);
1053 }
1054 return ret;
1055}
1056
783f6184
RK
1057/**
1058 * nilfs_fill_super() - initialize a super block instance
1059 * @sb: super_block
1060 * @data: mount options
1061 * @silent: silent mode flag
783f6184 1062 *
aa7dfb89 1063 * This function is called exclusively by nilfs->ns_mount_mutex.
783f6184
RK
1064 * So, the recovery process is protected from other simultaneous mounts.
1065 */
1066static int
348fe8da 1067nilfs_fill_super(struct super_block *sb, void *data, int silent)
783f6184 1068{
348fe8da 1069 struct the_nilfs *nilfs;
4d8d9293 1070 struct nilfs_root *fsroot;
783f6184
RK
1071 __u64 cno;
1072 int err;
1073
6625689e 1074 nilfs = alloc_nilfs(sb);
e3154e97 1075 if (!nilfs)
783f6184
RK
1076 return -ENOMEM;
1077
e3154e97 1078 sb->s_fs_info = nilfs;
783f6184 1079
f7545144 1080 err = init_nilfs(nilfs, sb, (char *)data);
783f6184 1081 if (err)
348fe8da 1082 goto failed_nilfs;
783f6184 1083
783f6184
RK
1084 sb->s_op = &nilfs_sops;
1085 sb->s_export_op = &nilfs_export_ops;
1086 sb->s_root = NULL;
61239230 1087 sb->s_time_gran = 1;
8de52778 1088 sb->s_max_links = NILFS_LINK_MAX;
026a7d63 1089
26ff1304 1090 sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
783f6184 1091
f7545144 1092 err = load_nilfs(nilfs, sb);
f50a4c81 1093 if (err)
348fe8da 1094 goto failed_nilfs;
f50a4c81 1095
783f6184 1096 cno = nilfs_last_cno(nilfs);
f7545144 1097 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
783f6184 1098 if (err) {
feee880f
RK
1099 nilfs_msg(sb, KERN_ERR,
1100 "error %d while loading last checkpoint (checkpoint number=%llu)",
1101 err, (unsigned long long)cno);
f1e89c86 1102 goto failed_unload;
783f6184
RK
1103 }
1104
1105 if (!(sb->s_flags & MS_RDONLY)) {
f7545144 1106 err = nilfs_attach_log_writer(sb, fsroot);
783f6184
RK
1107 if (err)
1108 goto failed_checkpoint;
1109 }
1110
367ea334
RK
1111 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1112 if (err)
783f6184 1113 goto failed_segctor;
783f6184 1114
4d8d9293 1115 nilfs_put_root(fsroot);
783f6184
RK
1116
1117 if (!(sb->s_flags & MS_RDONLY)) {
1118 down_write(&nilfs->ns_sem);
f7545144 1119 nilfs_setup_super(sb, true);
783f6184
RK
1120 up_write(&nilfs->ns_sem);
1121 }
1122
783f6184
RK
1123 return 0;
1124
783f6184 1125 failed_segctor:
f7545144 1126 nilfs_detach_log_writer(sb);
783f6184
RK
1127
1128 failed_checkpoint:
4d8d9293 1129 nilfs_put_root(fsroot);
783f6184 1130
f1e89c86
RK
1131 failed_unload:
1132 iput(nilfs->ns_sufile);
1133 iput(nilfs->ns_cpfile);
1134 iput(nilfs->ns_dat);
1135
348fe8da
RK
1136 failed_nilfs:
1137 destroy_nilfs(nilfs);
783f6184
RK
1138 return err;
1139}
1140
1141static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1142{
e3154e97 1143 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 1144 unsigned long old_sb_flags;
06df0f99 1145 unsigned long old_mount_opt;
f11459ad 1146 int err;
783f6184 1147
02b9984d 1148 sync_filesystem(sb);
783f6184 1149 old_sb_flags = sb->s_flags;
3b2ce58b 1150 old_mount_opt = nilfs->ns_mount_opt;
783f6184 1151
7c017457 1152 if (!parse_options(data, sb, 1)) {
783f6184
RK
1153 err = -EINVAL;
1154 goto restore_opts;
1155 }
1156 sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1157
d240e067 1158 err = -EINVAL;
783f6184 1159
0234576d 1160 if (!nilfs_valid_fs(nilfs)) {
feee880f
RK
1161 nilfs_msg(sb, KERN_WARNING,
1162 "couldn't remount because the filesystem is in an incomplete recovery state");
0234576d
RK
1163 goto restore_opts;
1164 }
1165
783f6184
RK
1166 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1167 goto out;
1168 if (*flags & MS_RDONLY) {
f7545144
RK
1169 /* Shutting down log writer */
1170 nilfs_detach_log_writer(sb);
783f6184
RK
1171 sb->s_flags |= MS_RDONLY;
1172
783f6184
RK
1173 /*
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);
f7545144 1178 nilfs_cleanup_super(sb);
783f6184
RK
1179 up_write(&nilfs->ns_sem);
1180 } else {
c5ca48aa 1181 __u64 features;
e912a5b6 1182 struct nilfs_root *root;
c5ca48aa 1183
783f6184
RK
1184 /*
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 */
c5ca48aa
RK
1189 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) {
feee880f
RK
1194 nilfs_msg(sb, KERN_WARNING,
1195 "couldn't remount RDWR because of unsupported optional features (%llx)",
1196 (unsigned long long)features);
c5ca48aa
RK
1197 err = -EROFS;
1198 goto restore_opts;
1199 }
1200
783f6184 1201 sb->s_flags &= ~MS_RDONLY;
783f6184 1202
2b0143b5 1203 root = NILFS_I(d_inode(sb->s_root))->i_root;
f7545144 1204 err = nilfs_attach_log_writer(sb, root);
783f6184 1205 if (err)
e59399d0 1206 goto restore_opts;
783f6184
RK
1207
1208 down_write(&nilfs->ns_sem);
f7545144 1209 nilfs_setup_super(sb, true);
783f6184 1210 up_write(&nilfs->ns_sem);
783f6184
RK
1211 }
1212 out:
1213 return 0;
1214
783f6184
RK
1215 restore_opts:
1216 sb->s_flags = old_sb_flags;
3b2ce58b 1217 nilfs->ns_mount_opt = old_mount_opt;
783f6184
RK
1218 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];
c05dbfc2 1236 int token;
783f6184
RK
1237 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) {
c05dbfc2 1244 if (!(sd->flags & MS_RDONLY)) {
783f6184 1245 ret++;
c05dbfc2
RK
1246 } 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++;
783f6184
RK
1256 }
1257 }
1258 if (ret)
feee880f
RK
1259 nilfs_msg(NULL, KERN_ERR,
1260 "invalid mount option: %s", p);
783f6184
RK
1261 }
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{
f11459ad 1272 s->s_bdev = data;
783f6184
RK
1273 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)
783f6184 1278{
f11459ad 1279 return (void *)s->s_bdev == data;
783f6184
RK
1280}
1281
e4c59d61
AV
1282static struct dentry *
1283nilfs_mount(struct file_system_type *fs_type, int flags,
1284 const char *dev_name, void *data)
783f6184
RK
1285{
1286 struct nilfs_super_data sd;
33c8e57c 1287 struct super_block *s;
d4d77629 1288 fmode_t mode = FMODE_READ | FMODE_EXCL;
f11459ad
RK
1289 struct dentry *root_dentry;
1290 int err, s_new = false;
783f6184 1291
13e90559
RK
1292 if (!(flags & MS_RDONLY))
1293 mode |= FMODE_WRITE;
1294
d4d77629 1295 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
d6d4c19c 1296 if (IS_ERR(sd.bdev))
e4c59d61 1297 return ERR_CAST(sd.bdev);
783f6184 1298
783f6184
RK
1299 sd.cno = 0;
1300 sd.flags = flags;
1301 if (nilfs_identify((char *)data, &sd)) {
1302 err = -EINVAL;
1303 goto failed;
1304 }
1305
6dd47406 1306 /*
5beb6e0b
RK
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
6dd47406 1310 */
5beb6e0b
RK
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;
1315 goto failed;
1316 }
9249e17f
DH
1317 s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1318 sd.bdev);
5beb6e0b 1319 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
33c8e57c
RK
1320 if (IS_ERR(s)) {
1321 err = PTR_ERR(s);
348fe8da 1322 goto failed;
783f6184
RK
1323 }
1324
1325 if (!s->s_root) {
8fa7c320 1326 s_new = true;
f11459ad 1327
33c8e57c 1328 /* New superblock instance created */
4571b82c 1329 s->s_mode = mode;
a1c6f057 1330 snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
783f6184
RK
1331 sb_set_blocksize(s, block_size(sd.bdev));
1332
348fe8da 1333 err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
783f6184 1334 if (err)
348fe8da 1335 goto failed_super;
783f6184
RK
1336
1337 s->s_flags |= MS_ACTIVE;
f11459ad 1338 } else if (!sd.cno) {
e95c311e
AV
1339 if (nilfs_tree_is_busy(s->s_root)) {
1340 if ((flags ^ s->s_flags) & MS_RDONLY) {
feee880f
RK
1341 nilfs_msg(s, KERN_ERR,
1342 "the device already has a %s mount.",
1343 (s->s_flags & MS_RDONLY) ?
1344 "read-only" : "read/write");
f11459ad
RK
1345 err = -EBUSY;
1346 goto failed_super;
1347 }
e95c311e 1348 } else {
f11459ad
RK
1349 /*
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 }
783f6184
RK
1357 }
1358
f11459ad
RK
1359 if (sd.cno) {
1360 err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
348fe8da 1361 if (err)
f11459ad 1362 goto failed_super;
f11459ad
RK
1363 } else {
1364 root_dentry = dget(s->s_root);
783f6184
RK
1365 }
1366
f11459ad 1367 if (!s_new)
d4d77629 1368 blkdev_put(sd.bdev, mode);
783f6184 1369
e4c59d61 1370 return root_dentry;
783f6184 1371
f11459ad 1372 failed_super:
a95161aa 1373 deactivate_locked_super(s);
783f6184 1374
348fe8da
RK
1375 failed:
1376 if (!s_new)
d4d77629 1377 blkdev_put(sd.bdev, mode);
e4c59d61 1378 return ERR_PTR(err);
783f6184
RK
1379}
1380
783f6184
RK
1381struct file_system_type nilfs_fs_type = {
1382 .owner = THIS_MODULE,
1383 .name = "nilfs2",
e4c59d61 1384 .mount = nilfs_mount,
783f6184
RK
1385 .kill_sb = kill_block_super,
1386 .fs_flags = FS_REQUIRES_DEV,
1387};
7f78e035 1388MODULE_ALIAS_FS("nilfs2");
783f6184 1389
41c88bd7 1390static void nilfs_inode_init_once(void *obj)
783f6184 1391{
41c88bd7 1392 struct nilfs_inode_info *ii = obj;
783f6184 1393
41c88bd7
LH
1394 INIT_LIST_HEAD(&ii->i_dirty);
1395#ifdef CONFIG_NILFS_XATTR
1396 init_rwsem(&ii->xattr_sem);
1397#endif
2aa15890 1398 address_space_init_once(&ii->i_btnode_cache);
05d0e94b 1399 ii->i_bmap = &ii->i_bmap_data;
41c88bd7
LH
1400 inode_init_once(&ii->vfs_inode);
1401}
783f6184 1402
41c88bd7
LH
1403static void nilfs_segbuf_init_once(void *obj)
1404{
1405 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1406}
783f6184 1407
41c88bd7
LH
1408static void nilfs_destroy_cachep(void)
1409{
8c0a8537
KS
1410 /*
1411 * Make sure all delayed rcu free inodes are flushed before we
1412 * destroy cache.
1413 */
1414 rcu_barrier();
1415
da80a39f
JL
1416 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);
41c88bd7 1420}
783f6184 1421
41c88bd7
LH
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,
5d097056
VD
1426 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1427 nilfs_inode_init_once);
41c88bd7
LH
1428 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;
783f6184
RK
1448
1449 return 0;
1450
41c88bd7
LH
1451fail:
1452 nilfs_destroy_cachep();
1453 return -ENOMEM;
1454}
1455
1456static int __init init_nilfs_fs(void)
1457{
1458 int err;
783f6184 1459
41c88bd7
LH
1460 err = nilfs_init_cachep();
1461 if (err)
1462 goto fail;
783f6184 1463
dd70edbd 1464 err = nilfs_sysfs_init();
41c88bd7
LH
1465 if (err)
1466 goto free_cachep;
783f6184 1467
dd70edbd
VD
1468 err = register_filesystem(&nilfs_fs_type);
1469 if (err)
1470 goto deinit_sysfs_entry;
1471
9f130263 1472 printk(KERN_INFO "NILFS version 2 loaded\n");
41c88bd7 1473 return 0;
783f6184 1474
dd70edbd
VD
1475deinit_sysfs_entry:
1476 nilfs_sysfs_exit();
41c88bd7
LH
1477free_cachep:
1478 nilfs_destroy_cachep();
1479fail:
783f6184
RK
1480 return err;
1481}
1482
1483static void __exit exit_nilfs_fs(void)
1484{
41c88bd7 1485 nilfs_destroy_cachep();
dd70edbd 1486 nilfs_sysfs_exit();
783f6184
RK
1487 unregister_filesystem(&nilfs_fs_type);
1488}
1489
1490module_init(init_nilfs_fs)
1491module_exit(exit_nilfs_fs)