]> git.ipfire.org Git - people/arne_f/kernel.git/blame - fs/nilfs2/super.c
nilfs2: add nilfs_msg() message interface
[people/arne_f/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)) {
783f6184
RK
217 printk(KERN_ERR
218 "NILFS: unable to write superblock (err=%d)\n", 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 {
e339ad31 288 printk(KERN_CRIT "NILFS: superblock broke on dev %s\n",
f7545144 289 sb->s_id);
d26493b6 290 return NULL;
e339ad31 291 }
b2ac86e1
JS
292 } else if (sbp[1] &&
293 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
7592ecde 294 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
e339ad31 295 }
b2ac86e1
JS
296
297 if (flip && sbp[1])
298 nilfs_swap_super_block(nilfs);
299
d26493b6
JS
300 return sbp;
301}
302
f7545144 303int nilfs_commit_super(struct super_block *sb, int flag)
d26493b6 304{
e3154e97 305 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6
JS
306 struct nilfs_super_block **sbp = nilfs->ns_sbp;
307 time_t t;
308
309 /* nilfs->ns_sem must be locked by the caller. */
e339ad31 310 t = get_seconds();
b2ac86e1 311 nilfs->ns_sbwtime = t;
e339ad31
RK
312 sbp[0]->s_wtime = cpu_to_le64(t);
313 sbp[0]->s_sum = 0;
314 sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
315 (unsigned char *)sbp[0],
316 nilfs->ns_sbsize));
b2ac86e1
JS
317 if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
318 sbp[1]->s_wtime = sbp[0]->s_wtime;
319 sbp[1]->s_sum = 0;
320 sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
321 (unsigned char *)sbp[1],
322 nilfs->ns_sbsize));
e339ad31 323 }
e605f0a7 324 clear_nilfs_sb_dirty(nilfs);
e2c7617a
AR
325 nilfs->ns_flushed_device = 1;
326 /* make sure store to ns_flushed_device cannot be reordered */
327 smp_wmb();
f7545144 328 return nilfs_sync_super(sb, flag);
783f6184
RK
329}
330
7ecaa46c
RK
331/**
332 * nilfs_cleanup_super() - write filesystem state for cleanup
f7545144 333 * @sb: super block instance to be unmounted or degraded to read-only
7ecaa46c
RK
334 *
335 * This function restores state flags in the on-disk super block.
336 * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
337 * filesystem was not clean previously.
338 */
f7545144 339int nilfs_cleanup_super(struct super_block *sb)
7ecaa46c 340{
e3154e97 341 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 342 struct nilfs_super_block **sbp;
b2ac86e1 343 int flag = NILFS_SB_COMMIT;
d26493b6 344 int ret = -EIO;
7ecaa46c 345
f7545144 346 sbp = nilfs_prepare_super(sb, 0);
d26493b6 347 if (sbp) {
f7545144
RK
348 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
349 nilfs_set_log_cursor(sbp[0], nilfs);
b2ac86e1
JS
350 if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
351 /*
352 * make the "clean" flag also to the opposite
353 * super block if both super blocks point to
354 * the same checkpoint.
355 */
356 sbp[1]->s_state = sbp[0]->s_state;
357 flag = NILFS_SB_COMMIT_ALL;
358 }
f7545144 359 ret = nilfs_commit_super(sb, flag);
d26493b6 360 }
7ecaa46c
RK
361 return ret;
362}
363
cfb0a4bf
RK
364/**
365 * nilfs_move_2nd_super - relocate secondary super block
366 * @sb: super block instance
367 * @sb2off: new offset of the secondary super block (in bytes)
368 */
369static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
370{
371 struct the_nilfs *nilfs = sb->s_fs_info;
372 struct buffer_head *nsbh;
373 struct nilfs_super_block *nsbp;
374 sector_t blocknr, newblocknr;
375 unsigned long offset;
4f05028f 376 int sb2i; /* array index of the secondary superblock */
cfb0a4bf
RK
377 int ret = 0;
378
379 /* nilfs->ns_sem must be locked by the caller. */
380 if (nilfs->ns_sbh[1] &&
381 nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
382 sb2i = 1;
383 blocknr = nilfs->ns_sbh[1]->b_blocknr;
384 } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
385 sb2i = 0;
386 blocknr = nilfs->ns_sbh[0]->b_blocknr;
4f05028f
RK
387 } else {
388 sb2i = -1;
389 blocknr = 0;
cfb0a4bf
RK
390 }
391 if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
392 goto out; /* super block location is unchanged */
393
394 /* Get new super block buffer */
395 newblocknr = sb2off >> nilfs->ns_blocksize_bits;
396 offset = sb2off & (nilfs->ns_blocksize - 1);
397 nsbh = sb_getblk(sb, newblocknr);
398 if (!nsbh) {
399 printk(KERN_WARNING
400 "NILFS warning: unable to move secondary superblock "
401 "to block %llu\n", (unsigned long long)newblocknr);
402 ret = -EIO;
403 goto out;
404 }
405 nsbp = (void *)nsbh->b_data + offset;
406 memset(nsbp, 0, nilfs->ns_blocksize);
407
408 if (sb2i >= 0) {
409 memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
410 brelse(nilfs->ns_sbh[sb2i]);
411 nilfs->ns_sbh[sb2i] = nsbh;
412 nilfs->ns_sbp[sb2i] = nsbp;
413 } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
414 /* secondary super block will be restored to index 1 */
415 nilfs->ns_sbh[1] = nsbh;
416 nilfs->ns_sbp[1] = nsbp;
417 } else {
418 brelse(nsbh);
419 }
420out:
421 return ret;
422}
423
4e33f9ea
RK
424/**
425 * nilfs_resize_fs - resize the filesystem
426 * @sb: super block instance
427 * @newsize: new size of the filesystem (in bytes)
428 */
429int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
430{
431 struct the_nilfs *nilfs = sb->s_fs_info;
432 struct nilfs_super_block **sbp;
433 __u64 devsize, newnsegs;
434 loff_t sb2off;
435 int ret;
436
437 ret = -ERANGE;
438 devsize = i_size_read(sb->s_bdev->bd_inode);
439 if (newsize > devsize)
440 goto out;
441
442 /*
443 * Write lock is required to protect some functions depending
444 * on the number of segments, the number of reserved segments,
445 * and so forth.
446 */
447 down_write(&nilfs->ns_segctor_sem);
448
449 sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
450 newnsegs = sb2off >> nilfs->ns_blocksize_bits;
451 do_div(newnsegs, nilfs->ns_blocks_per_segment);
452
453 ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
454 up_write(&nilfs->ns_segctor_sem);
455 if (ret < 0)
456 goto out;
457
458 ret = nilfs_construct_segment(sb);
459 if (ret < 0)
460 goto out;
461
462 down_write(&nilfs->ns_sem);
463 nilfs_move_2nd_super(sb, sb2off);
464 ret = -EIO;
465 sbp = nilfs_prepare_super(sb, 0);
466 if (likely(sbp)) {
467 nilfs_set_log_cursor(sbp[0], nilfs);
468 /*
469 * Drop NILFS_RESIZE_FS flag for compatibility with
470 * mount-time resize which may be implemented in a
471 * future release.
472 */
473 sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
474 ~NILFS_RESIZE_FS);
475 sbp[0]->s_dev_size = cpu_to_le64(newsize);
476 sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
477 if (sbp[1])
478 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
479 ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
480 }
481 up_write(&nilfs->ns_sem);
482
483 /*
484 * Reset the range of allocatable segments last. This order
485 * is important in the case of expansion because the secondary
486 * superblock must be protected from log write until migration
487 * completes.
488 */
489 if (!ret)
490 nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
491out:
492 return ret;
493}
494
783f6184
RK
495static void nilfs_put_super(struct super_block *sb)
496{
e3154e97 497 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 498
f7545144 499 nilfs_detach_log_writer(sb);
783f6184
RK
500
501 if (!(sb->s_flags & MS_RDONLY)) {
502 down_write(&nilfs->ns_sem);
f7545144 503 nilfs_cleanup_super(sb);
783f6184
RK
504 up_write(&nilfs->ns_sem);
505 }
506
f1e89c86
RK
507 iput(nilfs->ns_sufile);
508 iput(nilfs->ns_cpfile);
509 iput(nilfs->ns_dat);
510
348fe8da 511 destroy_nilfs(nilfs);
783f6184 512 sb->s_fs_info = NULL;
783f6184
RK
513}
514
783f6184
RK
515static int nilfs_sync_fs(struct super_block *sb, int wait)
516{
e3154e97 517 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 518 struct nilfs_super_block **sbp;
783f6184
RK
519 int err = 0;
520
521 /* This function is called when super block should be written back */
522 if (wait)
523 err = nilfs_construct_segment(sb);
6233caa9
JS
524
525 down_write(&nilfs->ns_sem);
d26493b6 526 if (nilfs_sb_dirty(nilfs)) {
f7545144 527 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
b2ac86e1
JS
528 if (likely(sbp)) {
529 nilfs_set_log_cursor(sbp[0], nilfs);
f7545144 530 nilfs_commit_super(sb, NILFS_SB_COMMIT);
b2ac86e1 531 }
d26493b6 532 }
6233caa9
JS
533 up_write(&nilfs->ns_sem);
534
e2c7617a
AR
535 if (!err)
536 err = nilfs_flush_device(nilfs);
537
783f6184
RK
538 return err;
539}
540
f7545144 541int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
4d8d9293 542 struct nilfs_root **rootp)
783f6184 543{
e3154e97 544 struct the_nilfs *nilfs = sb->s_fs_info;
4d8d9293 545 struct nilfs_root *root;
783f6184
RK
546 struct nilfs_checkpoint *raw_cp;
547 struct buffer_head *bh_cp;
4d8d9293 548 int err = -ENOMEM;
783f6184 549
4d8d9293
RK
550 root = nilfs_find_or_create_root(
551 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
552 if (!root)
553 return err;
783f6184 554
e912a5b6
RK
555 if (root->ifile)
556 goto reuse; /* already attached checkpoint */
783f6184 557
1154ecbd 558 down_read(&nilfs->ns_segctor_sem);
783f6184
RK
559 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
560 &bh_cp);
1154ecbd 561 up_read(&nilfs->ns_segctor_sem);
783f6184
RK
562 if (unlikely(err)) {
563 if (err == -ENOENT || err == -EINVAL) {
564 printk(KERN_ERR
565 "NILFS: Invalid checkpoint "
566 "(checkpoint number=%llu)\n",
567 (unsigned long long)cno);
568 err = -EINVAL;
569 }
570 goto failed;
571 }
f1e89c86 572
f7545144 573 err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
f1e89c86
RK
574 &raw_cp->cp_ifile_inode, &root->ifile);
575 if (err)
783f6184 576 goto failed_bh;
b7c06342 577
e5f7f848
VD
578 atomic64_set(&root->inodes_count,
579 le64_to_cpu(raw_cp->cp_inodes_count));
580 atomic64_set(&root->blocks_count,
581 le64_to_cpu(raw_cp->cp_blocks_count));
783f6184
RK
582
583 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
4d8d9293 584
e912a5b6 585 reuse:
4d8d9293 586 *rootp = root;
783f6184
RK
587 return 0;
588
589 failed_bh:
590 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
591 failed:
4d8d9293 592 nilfs_put_root(root);
783f6184
RK
593
594 return err;
595}
783f6184 596
5beb6e0b
RK
597static int nilfs_freeze(struct super_block *sb)
598{
e3154e97 599 struct the_nilfs *nilfs = sb->s_fs_info;
5beb6e0b
RK
600 int err;
601
602 if (sb->s_flags & MS_RDONLY)
603 return 0;
783f6184 604
5beb6e0b
RK
605 /* Mark super block clean */
606 down_write(&nilfs->ns_sem);
f7545144 607 err = nilfs_cleanup_super(sb);
5beb6e0b 608 up_write(&nilfs->ns_sem);
783f6184
RK
609 return err;
610}
611
5beb6e0b 612static int nilfs_unfreeze(struct super_block *sb)
783f6184 613{
e3154e97 614 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 615
5beb6e0b
RK
616 if (sb->s_flags & MS_RDONLY)
617 return 0;
618
619 down_write(&nilfs->ns_sem);
f7545144 620 nilfs_setup_super(sb, false);
5beb6e0b
RK
621 up_write(&nilfs->ns_sem);
622 return 0;
783f6184
RK
623}
624
783f6184
RK
625static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
626{
627 struct super_block *sb = dentry->d_sb;
2b0143b5 628 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b7c06342 629 struct the_nilfs *nilfs = root->nilfs;
c306af23 630 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
783f6184
RK
631 unsigned long long blocks;
632 unsigned long overhead;
633 unsigned long nrsvblocks;
634 sector_t nfreeblocks;
c7ef972c 635 u64 nmaxinodes, nfreeinodes;
783f6184
RK
636 int err;
637
638 /*
639 * Compute all of the segment blocks
640 *
641 * The blocks before first segment and after last segment
642 * are excluded.
643 */
644 blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
645 - nilfs->ns_first_data_block;
646 nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
647
648 /*
649 * Compute the overhead
650 *
7a65004b 651 * When distributing meta data blocks outside segment structure,
783f6184
RK
652 * We must count them as the overhead.
653 */
654 overhead = 0;
655
656 err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
657 if (unlikely(err))
658 return err;
659
c7ef972c
VD
660 err = nilfs_ifile_count_free_inodes(root->ifile,
661 &nmaxinodes, &nfreeinodes);
662 if (unlikely(err)) {
663 printk(KERN_WARNING
664 "NILFS warning: fail to count free inodes: err %d.\n",
665 err);
666 if (err == -ERANGE) {
667 /*
668 * If nilfs_palloc_count_max_entries() returns
669 * -ERANGE error code then we simply treat
670 * curent inodes count as maximum possible and
671 * zero as free inodes value.
672 */
e5f7f848 673 nmaxinodes = atomic64_read(&root->inodes_count);
c7ef972c
VD
674 nfreeinodes = 0;
675 err = 0;
676 } else
677 return err;
678 }
679
783f6184
RK
680 buf->f_type = NILFS_SUPER_MAGIC;
681 buf->f_bsize = sb->s_blocksize;
682 buf->f_blocks = blocks - overhead;
683 buf->f_bfree = nfreeblocks;
684 buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
685 (buf->f_bfree - nrsvblocks) : 0;
c7ef972c
VD
686 buf->f_files = nmaxinodes;
687 buf->f_ffree = nfreeinodes;
783f6184 688 buf->f_namelen = NILFS_NAME_LEN;
c306af23
RK
689 buf->f_fsid.val[0] = (u32)id;
690 buf->f_fsid.val[1] = (u32)(id >> 32);
691
783f6184
RK
692 return 0;
693}
694
34c80b1d 695static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
b58a285b 696{
34c80b1d 697 struct super_block *sb = dentry->d_sb;
e3154e97 698 struct the_nilfs *nilfs = sb->s_fs_info;
2b0143b5 699 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b58a285b 700
3b2ce58b 701 if (!nilfs_test_opt(nilfs, BARRIER))
c6b4d57d 702 seq_puts(seq, ",nobarrier");
f11459ad
RK
703 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
704 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
3b2ce58b 705 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
c6b4d57d 706 seq_puts(seq, ",errors=panic");
3b2ce58b 707 if (nilfs_test_opt(nilfs, ERRORS_CONT))
c6b4d57d 708 seq_puts(seq, ",errors=continue");
3b2ce58b 709 if (nilfs_test_opt(nilfs, STRICT_ORDER))
c6b4d57d 710 seq_puts(seq, ",order=strict");
3b2ce58b 711 if (nilfs_test_opt(nilfs, NORECOVERY))
c6b4d57d 712 seq_puts(seq, ",norecovery");
3b2ce58b 713 if (nilfs_test_opt(nilfs, DISCARD))
c6b4d57d 714 seq_puts(seq, ",discard");
b58a285b
JS
715
716 return 0;
717}
718
b87221de 719static const struct super_operations nilfs_sops = {
783f6184
RK
720 .alloc_inode = nilfs_alloc_inode,
721 .destroy_inode = nilfs_destroy_inode,
722 .dirty_inode = nilfs_dirty_inode,
6fd1e5c9 723 .evict_inode = nilfs_evict_inode,
783f6184 724 .put_super = nilfs_put_super,
783f6184 725 .sync_fs = nilfs_sync_fs,
5beb6e0b
RK
726 .freeze_fs = nilfs_freeze,
727 .unfreeze_fs = nilfs_unfreeze,
783f6184
RK
728 .statfs = nilfs_statfs,
729 .remount_fs = nilfs_remount,
b58a285b 730 .show_options = nilfs_show_options
783f6184
RK
731};
732
783f6184
RK
733enum {
734 Opt_err_cont, Opt_err_panic, Opt_err_ro,
773bc4f3 735 Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
802d3177 736 Opt_discard, Opt_nodiscard, Opt_err,
783f6184
RK
737};
738
739static match_table_t tokens = {
740 {Opt_err_cont, "errors=continue"},
741 {Opt_err_panic, "errors=panic"},
742 {Opt_err_ro, "errors=remount-ro"},
773bc4f3 743 {Opt_barrier, "barrier"},
91f1953b 744 {Opt_nobarrier, "nobarrier"},
783f6184
RK
745 {Opt_snapshot, "cp=%u"},
746 {Opt_order, "order=%s"},
0234576d 747 {Opt_norecovery, "norecovery"},
e902ec99 748 {Opt_discard, "discard"},
802d3177 749 {Opt_nodiscard, "nodiscard"},
783f6184
RK
750 {Opt_err, NULL}
751};
752
7c017457 753static int parse_options(char *options, struct super_block *sb, int is_remount)
783f6184 754{
e3154e97 755 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184
RK
756 char *p;
757 substring_t args[MAX_OPT_ARGS];
783f6184
RK
758
759 if (!options)
760 return 1;
761
762 while ((p = strsep(&options, ",")) != NULL) {
763 int token;
4ad364ca 764
783f6184
RK
765 if (!*p)
766 continue;
767
768 token = match_token(p, tokens, args);
769 switch (token) {
773bc4f3 770 case Opt_barrier:
3b2ce58b 771 nilfs_set_opt(nilfs, BARRIER);
773bc4f3 772 break;
91f1953b 773 case Opt_nobarrier:
3b2ce58b 774 nilfs_clear_opt(nilfs, BARRIER);
783f6184
RK
775 break;
776 case Opt_order:
777 if (strcmp(args[0].from, "relaxed") == 0)
778 /* Ordered data semantics */
3b2ce58b 779 nilfs_clear_opt(nilfs, STRICT_ORDER);
783f6184
RK
780 else if (strcmp(args[0].from, "strict") == 0)
781 /* Strict in-order semantics */
3b2ce58b 782 nilfs_set_opt(nilfs, STRICT_ORDER);
783f6184
RK
783 else
784 return 0;
785 break;
786 case Opt_err_panic:
3b2ce58b 787 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
783f6184
RK
788 break;
789 case Opt_err_ro:
3b2ce58b 790 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
783f6184
RK
791 break;
792 case Opt_err_cont:
3b2ce58b 793 nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
783f6184
RK
794 break;
795 case Opt_snapshot:
7c017457 796 if (is_remount) {
f11459ad
RK
797 printk(KERN_ERR
798 "NILFS: \"%s\" option is invalid "
799 "for remount.\n", p);
783f6184 800 return 0;
7c017457 801 }
783f6184 802 break;
0234576d 803 case Opt_norecovery:
3b2ce58b 804 nilfs_set_opt(nilfs, NORECOVERY);
0234576d 805 break;
e902ec99 806 case Opt_discard:
3b2ce58b 807 nilfs_set_opt(nilfs, DISCARD);
e902ec99 808 break;
802d3177 809 case Opt_nodiscard:
3b2ce58b 810 nilfs_clear_opt(nilfs, DISCARD);
802d3177 811 break;
783f6184
RK
812 default:
813 printk(KERN_ERR
814 "NILFS: Unrecognized mount option \"%s\"\n", p);
815 return 0;
816 }
817 }
818 return 1;
819}
820
821static inline void
3b2ce58b 822nilfs_set_default_options(struct super_block *sb,
783f6184
RK
823 struct nilfs_super_block *sbp)
824{
e3154e97 825 struct the_nilfs *nilfs = sb->s_fs_info;
3b2ce58b
RK
826
827 nilfs->ns_mount_opt =
277a6a34 828 NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
783f6184
RK
829}
830
f7545144 831static int nilfs_setup_super(struct super_block *sb, int is_mount)
783f6184 832{
e3154e97 833 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6
JS
834 struct nilfs_super_block **sbp;
835 int max_mnt_count;
836 int mnt_count;
837
838 /* nilfs->ns_sem must be locked by the caller. */
f7545144 839 sbp = nilfs_prepare_super(sb, 0);
d26493b6
JS
840 if (!sbp)
841 return -EIO;
842
5beb6e0b
RK
843 if (!is_mount)
844 goto skip_mount_setup;
845
d26493b6
JS
846 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
847 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
783f6184 848
f50a4c81 849 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
783f6184
RK
850 printk(KERN_WARNING
851 "NILFS warning: mounting fs with errors\n");
852#if 0
853 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
854 printk(KERN_WARNING
855 "NILFS warning: maximal mount count reached\n");
856#endif
857 }
858 if (!max_mnt_count)
d26493b6 859 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
783f6184 860
d26493b6 861 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
5beb6e0b
RK
862 sbp[0]->s_mtime = cpu_to_le64(get_seconds());
863
864skip_mount_setup:
d26493b6
JS
865 sbp[0]->s_state =
866 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
b2ac86e1 867 /* synchronize sbp[1] with sbp[0] */
0ca7a5b9
RK
868 if (sbp[1])
869 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
f7545144 870 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
783f6184
RK
871}
872
e339ad31
RK
873struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
874 u64 pos, int blocksize,
875 struct buffer_head **pbh)
783f6184 876{
e339ad31
RK
877 unsigned long long sb_index = pos;
878 unsigned long offset;
783f6184 879
e339ad31 880 offset = do_div(sb_index, blocksize);
783f6184 881 *pbh = sb_bread(sb, sb_index);
e339ad31 882 if (!*pbh)
783f6184 883 return NULL;
783f6184
RK
884 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
885}
886
783f6184
RK
887int nilfs_store_magic_and_option(struct super_block *sb,
888 struct nilfs_super_block *sbp,
889 char *data)
890{
e3154e97 891 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 892
783f6184
RK
893 sb->s_magic = le16_to_cpu(sbp->s_magic);
894
895 /* FS independent flags */
896#ifdef NILFS_ATIME_DISABLE
897 sb->s_flags |= MS_NOATIME;
898#endif
899
3b2ce58b 900 nilfs_set_default_options(sb, sbp);
783f6184 901
574e6c31
RK
902 nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
903 nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
904 nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
905 nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
783f6184 906
c9cb9b5c 907 return !parse_options(data, sb, 0) ? -EINVAL : 0;
783f6184
RK
908}
909
c5ca48aa
RK
910int nilfs_check_feature_compatibility(struct super_block *sb,
911 struct nilfs_super_block *sbp)
912{
913 __u64 features;
914
915 features = le64_to_cpu(sbp->s_feature_incompat) &
916 ~NILFS_FEATURE_INCOMPAT_SUPP;
917 if (features) {
918 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
919 "optional features (%llx)\n",
920 (unsigned long long)features);
921 return -EINVAL;
922 }
923 features = le64_to_cpu(sbp->s_feature_compat_ro) &
924 ~NILFS_FEATURE_COMPAT_RO_SUPP;
925 if (!(sb->s_flags & MS_RDONLY) && features) {
926 printk(KERN_ERR "NILFS: couldn't mount RDWR because of "
927 "unsupported optional features (%llx)\n",
928 (unsigned long long)features);
929 return -EINVAL;
930 }
931 return 0;
932}
933
367ea334
RK
934static int nilfs_get_root_dentry(struct super_block *sb,
935 struct nilfs_root *root,
936 struct dentry **root_dentry)
937{
938 struct inode *inode;
939 struct dentry *dentry;
940 int ret = 0;
941
942 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
943 if (IS_ERR(inode)) {
944 printk(KERN_ERR "NILFS: get root inode failed\n");
945 ret = PTR_ERR(inode);
946 goto out;
947 }
948 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
949 iput(inode);
950 printk(KERN_ERR "NILFS: corrupt root inode.\n");
951 ret = -EINVAL;
952 goto out;
953 }
954
f11459ad
RK
955 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
956 dentry = d_find_alias(inode);
957 if (!dentry) {
48fde701 958 dentry = d_make_root(inode);
f11459ad 959 if (!dentry) {
f11459ad
RK
960 ret = -ENOMEM;
961 goto failed_dentry;
962 }
963 } else {
964 iput(inode);
965 }
966 } else {
1a0a397e 967 dentry = d_obtain_root(inode);
f11459ad
RK
968 if (IS_ERR(dentry)) {
969 ret = PTR_ERR(dentry);
970 goto failed_dentry;
971 }
367ea334
RK
972 }
973 *root_dentry = dentry;
974 out:
975 return ret;
f11459ad
RK
976
977 failed_dentry:
978 printk(KERN_ERR "NILFS: get root dentry failed\n");
979 goto out;
367ea334
RK
980}
981
ab4d8f7e
RK
982static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
983 struct dentry **root_dentry)
984{
e3154e97 985 struct the_nilfs *nilfs = s->s_fs_info;
ab4d8f7e
RK
986 struct nilfs_root *root;
987 int ret;
988
572d8b39
RK
989 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
990
ab4d8f7e
RK
991 down_read(&nilfs->ns_segctor_sem);
992 ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
993 up_read(&nilfs->ns_segctor_sem);
994 if (ret < 0) {
995 ret = (ret == -ENOENT) ? -EINVAL : ret;
996 goto out;
997 } else if (!ret) {
998 printk(KERN_ERR "NILFS: The specified checkpoint is "
999 "not a snapshot (checkpoint number=%llu).\n",
1000 (unsigned long long)cno);
1001 ret = -EINVAL;
1002 goto out;
1003 }
1004
f7545144 1005 ret = nilfs_attach_checkpoint(s, cno, false, &root);
ab4d8f7e
RK
1006 if (ret) {
1007 printk(KERN_ERR "NILFS: error loading snapshot "
1008 "(checkpoint number=%llu).\n",
1009 (unsigned long long)cno);
1010 goto out;
1011 }
1012 ret = nilfs_get_root_dentry(s, root, root_dentry);
1013 nilfs_put_root(root);
1014 out:
572d8b39 1015 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
ab4d8f7e
RK
1016 return ret;
1017}
1018
f11459ad 1019/**
e95c311e 1020 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
f11459ad
RK
1021 * @root_dentry: root dentry of the tree to be shrunk
1022 *
1023 * This function returns true if the tree was in-use.
1024 */
e95c311e 1025static bool nilfs_tree_is_busy(struct dentry *root_dentry)
f11459ad 1026{
f11459ad 1027 shrink_dcache_parent(root_dentry);
e95c311e 1028 return d_count(root_dentry) > 1;
f11459ad
RK
1029}
1030
032dbb3b
RK
1031int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1032{
e3154e97 1033 struct the_nilfs *nilfs = sb->s_fs_info;
032dbb3b
RK
1034 struct nilfs_root *root;
1035 struct inode *inode;
1036 struct dentry *dentry;
1037 int ret;
1038
3377f843 1039 if (cno > nilfs->ns_cno)
032dbb3b
RK
1040 return false;
1041
1042 if (cno >= nilfs_last_cno(nilfs))
1043 return true; /* protect recent checkpoints */
1044
1045 ret = false;
e3154e97 1046 root = nilfs_lookup_root(nilfs, cno);
032dbb3b
RK
1047 if (root) {
1048 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1049 if (inode) {
1050 dentry = d_find_alias(inode);
1051 if (dentry) {
e95c311e 1052 ret = nilfs_tree_is_busy(dentry);
032dbb3b
RK
1053 dput(dentry);
1054 }
1055 iput(inode);
1056 }
1057 nilfs_put_root(root);
1058 }
1059 return ret;
1060}
1061
783f6184
RK
1062/**
1063 * nilfs_fill_super() - initialize a super block instance
1064 * @sb: super_block
1065 * @data: mount options
1066 * @silent: silent mode flag
783f6184 1067 *
aa7dfb89 1068 * This function is called exclusively by nilfs->ns_mount_mutex.
783f6184
RK
1069 * So, the recovery process is protected from other simultaneous mounts.
1070 */
1071static int
348fe8da 1072nilfs_fill_super(struct super_block *sb, void *data, int silent)
783f6184 1073{
348fe8da 1074 struct the_nilfs *nilfs;
4d8d9293 1075 struct nilfs_root *fsroot;
783f6184
RK
1076 __u64 cno;
1077 int err;
1078
e3154e97
RK
1079 nilfs = alloc_nilfs(sb->s_bdev);
1080 if (!nilfs)
783f6184
RK
1081 return -ENOMEM;
1082
e3154e97 1083 sb->s_fs_info = nilfs;
783f6184 1084
f7545144 1085 err = init_nilfs(nilfs, sb, (char *)data);
783f6184 1086 if (err)
348fe8da 1087 goto failed_nilfs;
783f6184 1088
783f6184
RK
1089 sb->s_op = &nilfs_sops;
1090 sb->s_export_op = &nilfs_export_ops;
1091 sb->s_root = NULL;
61239230 1092 sb->s_time_gran = 1;
8de52778 1093 sb->s_max_links = NILFS_LINK_MAX;
026a7d63 1094
26ff1304 1095 sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info;
783f6184 1096
f7545144 1097 err = load_nilfs(nilfs, sb);
f50a4c81 1098 if (err)
348fe8da 1099 goto failed_nilfs;
f50a4c81 1100
783f6184 1101 cno = nilfs_last_cno(nilfs);
f7545144 1102 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
783f6184 1103 if (err) {
f11459ad
RK
1104 printk(KERN_ERR "NILFS: error loading last checkpoint "
1105 "(checkpoint number=%llu).\n", (unsigned long long)cno);
f1e89c86 1106 goto failed_unload;
783f6184
RK
1107 }
1108
1109 if (!(sb->s_flags & MS_RDONLY)) {
f7545144 1110 err = nilfs_attach_log_writer(sb, fsroot);
783f6184
RK
1111 if (err)
1112 goto failed_checkpoint;
1113 }
1114
367ea334
RK
1115 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1116 if (err)
783f6184 1117 goto failed_segctor;
783f6184 1118
4d8d9293 1119 nilfs_put_root(fsroot);
783f6184
RK
1120
1121 if (!(sb->s_flags & MS_RDONLY)) {
1122 down_write(&nilfs->ns_sem);
f7545144 1123 nilfs_setup_super(sb, true);
783f6184
RK
1124 up_write(&nilfs->ns_sem);
1125 }
1126
783f6184
RK
1127 return 0;
1128
783f6184 1129 failed_segctor:
f7545144 1130 nilfs_detach_log_writer(sb);
783f6184
RK
1131
1132 failed_checkpoint:
4d8d9293 1133 nilfs_put_root(fsroot);
783f6184 1134
f1e89c86
RK
1135 failed_unload:
1136 iput(nilfs->ns_sufile);
1137 iput(nilfs->ns_cpfile);
1138 iput(nilfs->ns_dat);
1139
348fe8da
RK
1140 failed_nilfs:
1141 destroy_nilfs(nilfs);
783f6184
RK
1142 return err;
1143}
1144
1145static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1146{
e3154e97 1147 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 1148 unsigned long old_sb_flags;
06df0f99 1149 unsigned long old_mount_opt;
f11459ad 1150 int err;
783f6184 1151
02b9984d 1152 sync_filesystem(sb);
783f6184 1153 old_sb_flags = sb->s_flags;
3b2ce58b 1154 old_mount_opt = nilfs->ns_mount_opt;
783f6184 1155
7c017457 1156 if (!parse_options(data, sb, 1)) {
783f6184
RK
1157 err = -EINVAL;
1158 goto restore_opts;
1159 }
1160 sb->s_flags = (sb->s_flags & ~MS_POSIXACL);
1161
d240e067 1162 err = -EINVAL;
783f6184 1163
0234576d
RK
1164 if (!nilfs_valid_fs(nilfs)) {
1165 printk(KERN_WARNING "NILFS (device %s): couldn't "
1166 "remount because the filesystem is in an "
1167 "incomplete recovery state.\n", sb->s_id);
0234576d
RK
1168 goto restore_opts;
1169 }
1170
783f6184
RK
1171 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1172 goto out;
1173 if (*flags & MS_RDONLY) {
f7545144
RK
1174 /* Shutting down log writer */
1175 nilfs_detach_log_writer(sb);
783f6184
RK
1176 sb->s_flags |= MS_RDONLY;
1177
783f6184
RK
1178 /*
1179 * Remounting a valid RW partition RDONLY, so set
1180 * the RDONLY flag and then mark the partition as valid again.
1181 */
1182 down_write(&nilfs->ns_sem);
f7545144 1183 nilfs_cleanup_super(sb);
783f6184
RK
1184 up_write(&nilfs->ns_sem);
1185 } else {
c5ca48aa 1186 __u64 features;
e912a5b6 1187 struct nilfs_root *root;
c5ca48aa 1188
783f6184
RK
1189 /*
1190 * Mounting a RDONLY partition read-write, so reread and
1191 * store the current valid flag. (It may have been changed
1192 * by fsck since we originally mounted the partition.)
1193 */
c5ca48aa
RK
1194 down_read(&nilfs->ns_sem);
1195 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1196 ~NILFS_FEATURE_COMPAT_RO_SUPP;
1197 up_read(&nilfs->ns_sem);
1198 if (features) {
1199 printk(KERN_WARNING "NILFS (device %s): couldn't "
1200 "remount RDWR because of unsupported optional "
1201 "features (%llx)\n",
1202 sb->s_id, (unsigned long long)features);
1203 err = -EROFS;
1204 goto restore_opts;
1205 }
1206
783f6184 1207 sb->s_flags &= ~MS_RDONLY;
783f6184 1208
2b0143b5 1209 root = NILFS_I(d_inode(sb->s_root))->i_root;
f7545144 1210 err = nilfs_attach_log_writer(sb, root);
783f6184 1211 if (err)
e59399d0 1212 goto restore_opts;
783f6184
RK
1213
1214 down_write(&nilfs->ns_sem);
f7545144 1215 nilfs_setup_super(sb, true);
783f6184 1216 up_write(&nilfs->ns_sem);
783f6184
RK
1217 }
1218 out:
1219 return 0;
1220
783f6184
RK
1221 restore_opts:
1222 sb->s_flags = old_sb_flags;
3b2ce58b 1223 nilfs->ns_mount_opt = old_mount_opt;
783f6184
RK
1224 return err;
1225}
1226
1227struct nilfs_super_data {
1228 struct block_device *bdev;
1229 __u64 cno;
1230 int flags;
1231};
1232
1233/**
1234 * nilfs_identify - pre-read mount options needed to identify mount instance
1235 * @data: mount options
1236 * @sd: nilfs_super_data
1237 */
1238static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1239{
1240 char *p, *options = data;
1241 substring_t args[MAX_OPT_ARGS];
c05dbfc2 1242 int token;
783f6184
RK
1243 int ret = 0;
1244
1245 do {
1246 p = strsep(&options, ",");
1247 if (p != NULL && *p) {
1248 token = match_token(p, tokens, args);
1249 if (token == Opt_snapshot) {
c05dbfc2 1250 if (!(sd->flags & MS_RDONLY)) {
783f6184 1251 ret++;
c05dbfc2
RK
1252 } else {
1253 sd->cno = simple_strtoull(args[0].from,
1254 NULL, 0);
1255 /*
1256 * No need to see the end pointer;
1257 * match_token() has done syntax
1258 * checking.
1259 */
1260 if (sd->cno == 0)
1261 ret++;
783f6184
RK
1262 }
1263 }
1264 if (ret)
1265 printk(KERN_ERR
1266 "NILFS: invalid mount option: %s\n", p);
1267 }
1268 if (!options)
1269 break;
1270 BUG_ON(options == data);
1271 *(options - 1) = ',';
1272 } while (!ret);
1273 return ret;
1274}
1275
1276static int nilfs_set_bdev_super(struct super_block *s, void *data)
1277{
f11459ad 1278 s->s_bdev = data;
783f6184
RK
1279 s->s_dev = s->s_bdev->bd_dev;
1280 return 0;
1281}
1282
1283static int nilfs_test_bdev_super(struct super_block *s, void *data)
783f6184 1284{
f11459ad 1285 return (void *)s->s_bdev == data;
783f6184
RK
1286}
1287
e4c59d61
AV
1288static struct dentry *
1289nilfs_mount(struct file_system_type *fs_type, int flags,
1290 const char *dev_name, void *data)
783f6184
RK
1291{
1292 struct nilfs_super_data sd;
33c8e57c 1293 struct super_block *s;
d4d77629 1294 fmode_t mode = FMODE_READ | FMODE_EXCL;
f11459ad
RK
1295 struct dentry *root_dentry;
1296 int err, s_new = false;
783f6184 1297
13e90559
RK
1298 if (!(flags & MS_RDONLY))
1299 mode |= FMODE_WRITE;
1300
d4d77629 1301 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
d6d4c19c 1302 if (IS_ERR(sd.bdev))
e4c59d61 1303 return ERR_CAST(sd.bdev);
783f6184 1304
783f6184
RK
1305 sd.cno = 0;
1306 sd.flags = flags;
1307 if (nilfs_identify((char *)data, &sd)) {
1308 err = -EINVAL;
1309 goto failed;
1310 }
1311
6dd47406 1312 /*
5beb6e0b
RK
1313 * once the super is inserted into the list by sget, s_umount
1314 * will protect the lockfs code from trying to start a snapshot
1315 * while we are mounting
6dd47406 1316 */
5beb6e0b
RK
1317 mutex_lock(&sd.bdev->bd_fsfreeze_mutex);
1318 if (sd.bdev->bd_fsfreeze_count > 0) {
1319 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
1320 err = -EBUSY;
1321 goto failed;
1322 }
9249e17f
DH
1323 s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1324 sd.bdev);
5beb6e0b 1325 mutex_unlock(&sd.bdev->bd_fsfreeze_mutex);
33c8e57c
RK
1326 if (IS_ERR(s)) {
1327 err = PTR_ERR(s);
348fe8da 1328 goto failed;
783f6184
RK
1329 }
1330
1331 if (!s->s_root) {
8fa7c320 1332 s_new = true;
f11459ad 1333
33c8e57c 1334 /* New superblock instance created */
4571b82c 1335 s->s_mode = mode;
a1c6f057 1336 snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev);
783f6184
RK
1337 sb_set_blocksize(s, block_size(sd.bdev));
1338
348fe8da 1339 err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0);
783f6184 1340 if (err)
348fe8da 1341 goto failed_super;
783f6184
RK
1342
1343 s->s_flags |= MS_ACTIVE;
f11459ad 1344 } else if (!sd.cno) {
e95c311e
AV
1345 if (nilfs_tree_is_busy(s->s_root)) {
1346 if ((flags ^ s->s_flags) & MS_RDONLY) {
f11459ad
RK
1347 printk(KERN_ERR "NILFS: the device already "
1348 "has a %s mount.\n",
1349 (s->s_flags & MS_RDONLY) ?
1350 "read-only" : "read/write");
1351 err = -EBUSY;
1352 goto failed_super;
1353 }
e95c311e 1354 } else {
f11459ad
RK
1355 /*
1356 * Try remount to setup mount states if the current
1357 * tree is not mounted and only snapshots use this sb.
1358 */
1359 err = nilfs_remount(s, &flags, data);
1360 if (err)
1361 goto failed_super;
1362 }
783f6184
RK
1363 }
1364
f11459ad
RK
1365 if (sd.cno) {
1366 err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
348fe8da 1367 if (err)
f11459ad 1368 goto failed_super;
f11459ad
RK
1369 } else {
1370 root_dentry = dget(s->s_root);
783f6184
RK
1371 }
1372
f11459ad 1373 if (!s_new)
d4d77629 1374 blkdev_put(sd.bdev, mode);
783f6184 1375
e4c59d61 1376 return root_dentry;
783f6184 1377
f11459ad 1378 failed_super:
a95161aa 1379 deactivate_locked_super(s);
783f6184 1380
348fe8da
RK
1381 failed:
1382 if (!s_new)
d4d77629 1383 blkdev_put(sd.bdev, mode);
e4c59d61 1384 return ERR_PTR(err);
783f6184
RK
1385}
1386
783f6184
RK
1387struct file_system_type nilfs_fs_type = {
1388 .owner = THIS_MODULE,
1389 .name = "nilfs2",
e4c59d61 1390 .mount = nilfs_mount,
783f6184
RK
1391 .kill_sb = kill_block_super,
1392 .fs_flags = FS_REQUIRES_DEV,
1393};
7f78e035 1394MODULE_ALIAS_FS("nilfs2");
783f6184 1395
41c88bd7 1396static void nilfs_inode_init_once(void *obj)
783f6184 1397{
41c88bd7 1398 struct nilfs_inode_info *ii = obj;
783f6184 1399
41c88bd7
LH
1400 INIT_LIST_HEAD(&ii->i_dirty);
1401#ifdef CONFIG_NILFS_XATTR
1402 init_rwsem(&ii->xattr_sem);
1403#endif
2aa15890 1404 address_space_init_once(&ii->i_btnode_cache);
05d0e94b 1405 ii->i_bmap = &ii->i_bmap_data;
41c88bd7
LH
1406 inode_init_once(&ii->vfs_inode);
1407}
783f6184 1408
41c88bd7
LH
1409static void nilfs_segbuf_init_once(void *obj)
1410{
1411 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1412}
783f6184 1413
41c88bd7
LH
1414static void nilfs_destroy_cachep(void)
1415{
8c0a8537
KS
1416 /*
1417 * Make sure all delayed rcu free inodes are flushed before we
1418 * destroy cache.
1419 */
1420 rcu_barrier();
1421
da80a39f
JL
1422 kmem_cache_destroy(nilfs_inode_cachep);
1423 kmem_cache_destroy(nilfs_transaction_cachep);
1424 kmem_cache_destroy(nilfs_segbuf_cachep);
1425 kmem_cache_destroy(nilfs_btree_path_cache);
41c88bd7 1426}
783f6184 1427
41c88bd7
LH
1428static int __init nilfs_init_cachep(void)
1429{
1430 nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1431 sizeof(struct nilfs_inode_info), 0,
5d097056
VD
1432 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1433 nilfs_inode_init_once);
41c88bd7
LH
1434 if (!nilfs_inode_cachep)
1435 goto fail;
1436
1437 nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1438 sizeof(struct nilfs_transaction_info), 0,
1439 SLAB_RECLAIM_ACCOUNT, NULL);
1440 if (!nilfs_transaction_cachep)
1441 goto fail;
1442
1443 nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1444 sizeof(struct nilfs_segment_buffer), 0,
1445 SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1446 if (!nilfs_segbuf_cachep)
1447 goto fail;
1448
1449 nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1450 sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1451 0, 0, NULL);
1452 if (!nilfs_btree_path_cache)
1453 goto fail;
783f6184
RK
1454
1455 return 0;
1456
41c88bd7
LH
1457fail:
1458 nilfs_destroy_cachep();
1459 return -ENOMEM;
1460}
1461
1462static int __init init_nilfs_fs(void)
1463{
1464 int err;
783f6184 1465
41c88bd7
LH
1466 err = nilfs_init_cachep();
1467 if (err)
1468 goto fail;
783f6184 1469
dd70edbd 1470 err = nilfs_sysfs_init();
41c88bd7
LH
1471 if (err)
1472 goto free_cachep;
783f6184 1473
dd70edbd
VD
1474 err = register_filesystem(&nilfs_fs_type);
1475 if (err)
1476 goto deinit_sysfs_entry;
1477
9f130263 1478 printk(KERN_INFO "NILFS version 2 loaded\n");
41c88bd7 1479 return 0;
783f6184 1480
dd70edbd
VD
1481deinit_sysfs_entry:
1482 nilfs_sysfs_exit();
41c88bd7
LH
1483free_cachep:
1484 nilfs_destroy_cachep();
1485fail:
783f6184
RK
1486 return err;
1487}
1488
1489static void __exit exit_nilfs_fs(void)
1490{
41c88bd7 1491 nilfs_destroy_cachep();
dd70edbd 1492 nilfs_sysfs_exit();
783f6184
RK
1493 unregister_filesystem(&nilfs_fs_type);
1494}
1495
1496module_init(init_nilfs_fs)
1497module_exit(exit_nilfs_fs)