Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / fs / nilfs2 / super.c
CommitLineData
ae98043f 1// SPDX-License-Identifier: GPL-2.0+
783f6184 2/*
94ee1d91 3 * NILFS module and super block management.
783f6184
RK
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
4b420ab4 7 * Written by Ryusuke Konishi.
783f6184
RK
8 */
9/*
10 * linux/fs/ext2/super.c
11 *
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
16 *
17 * from
18 *
19 * linux/fs/minix/inode.c
20 *
21 * Copyright (C) 1991, 1992 Linus Torvalds
22 *
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
25 */
26
27#include <linux/module.h>
28#include <linux/string.h>
29#include <linux/slab.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
783f6184 32#include <linux/crc32.h>
783f6184
RK
33#include <linux/vfs.h>
34#include <linux/writeback.h>
b58a285b
JS
35#include <linux/seq_file.h>
36#include <linux/mount.h>
c1e012ea 37#include <linux/fs_context.h>
36defdd9 38#include <linux/fs_parser.h>
783f6184 39#include "nilfs.h"
8e656fd5 40#include "export.h"
783f6184
RK
41#include "mdt.h"
42#include "alloc.h"
05d0e94b
RK
43#include "btree.h"
44#include "btnode.h"
783f6184
RK
45#include "page.h"
46#include "cpfile.h"
4e33f9ea 47#include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
783f6184
RK
48#include "ifile.h"
49#include "dat.h"
50#include "segment.h"
51#include "segbuf.h"
52
53MODULE_AUTHOR("NTT Corp.");
54MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
55 "(NILFS)");
783f6184
RK
56MODULE_LICENSE("GPL");
57
abc0b50b 58static struct kmem_cache *nilfs_inode_cachep;
41c88bd7
LH
59struct kmem_cache *nilfs_transaction_cachep;
60struct kmem_cache *nilfs_segbuf_cachep;
61struct kmem_cache *nilfs_btree_path_cache;
62
f7545144 63static int nilfs_setup_super(struct super_block *sb, int is_mount);
783f6184 64
2987a4cf 65void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
a66dfb0a
RK
66{
67 struct va_format vaf;
68 va_list args;
2987a4cf 69 int level;
a66dfb0a
RK
70
71 va_start(args, fmt);
2987a4cf
JP
72
73 level = printk_get_level(fmt);
74 vaf.fmt = printk_skip_level(fmt);
a66dfb0a 75 vaf.va = &args;
2987a4cf 76
a66dfb0a 77 if (sb)
2987a4cf
JP
78 printk("%c%cNILFS (%s): %pV\n",
79 KERN_SOH_ASCII, level, sb->s_id, &vaf);
a66dfb0a 80 else
2987a4cf
JP
81 printk("%c%cNILFS: %pV\n",
82 KERN_SOH_ASCII, level, &vaf);
83
a66dfb0a
RK
84 va_end(args);
85}
86
f7545144 87static void nilfs_set_error(struct super_block *sb)
c8a11c8a 88{
e3154e97 89 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 90 struct nilfs_super_block **sbp;
c8a11c8a
RK
91
92 down_write(&nilfs->ns_sem);
93 if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
94 nilfs->ns_mount_state |= NILFS_ERROR_FS;
f7545144 95 sbp = nilfs_prepare_super(sb, 0);
d26493b6
JS
96 if (likely(sbp)) {
97 sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
b2ac86e1
JS
98 if (sbp[1])
99 sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
f7545144 100 nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
d26493b6 101 }
c8a11c8a
RK
102 }
103 up_write(&nilfs->ns_sem);
104}
105
783f6184 106/**
cae3d4ca 107 * __nilfs_error() - report failure condition on a filesystem
b79bdfdd
RK
108 * @sb: super block instance
109 * @function: name of calling function
110 * @fmt: format string for message to be output
111 * @...: optional arguments to @fmt
783f6184 112 *
cae3d4ca
RK
113 * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
114 * reporting an error message. This function should be called when
115 * NILFS detects incoherences or defects of meta data on disk.
783f6184 116 *
cae3d4ca
RK
117 * This implements the body of nilfs_error() macro. Normally,
118 * nilfs_error() should be used. As for sustainable errors such as a
a1d0747a 119 * single-shot I/O error, nilfs_err() should be used instead.
cae3d4ca
RK
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
bc98a42c 140 if (!sb_rdonly(sb)) {
f7545144 141 nilfs_set_error(sb);
783f6184 142
3b2ce58b 143 if (nilfs_test_opt(nilfs, ERRORS_RO)) {
783f6184 144 printk(KERN_CRIT "Remounting filesystem read-only\n");
1751e8a6 145 sb->s_flags |= SB_RDONLY;
783f6184
RK
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
2879ed66 154struct inode *nilfs_alloc_inode(struct super_block *sb)
783f6184
RK
155{
156 struct nilfs_inode_info *ii;
157
fd60b288 158 ii = alloc_inode_sb(sb, nilfs_inode_cachep, GFP_NOFS);
783f6184
RK
159 if (!ii)
160 return NULL;
161 ii->i_bh = NULL;
162 ii->i_state = 0;
d7cee0b3 163 ii->i_type = 0;
0e14a359 164 ii->i_cno = 0;
e897be17
RK
165 ii->i_assoc_inode = NULL;
166 ii->i_bmap = &ii->i_bmap_data;
783f6184
RK
167 return &ii->vfs_inode;
168}
169
977c3d18 170static void nilfs_free_inode(struct inode *inode)
783f6184 171{
2d19961d
RK
172 if (nilfs_is_metadata_file_inode(inode))
173 nilfs_mdt_destroy(inode);
174
783f6184
RK
175 kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
176}
177
f7545144 178static int nilfs_sync_super(struct super_block *sb, int flag)
783f6184 179{
e3154e97 180 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 181 int err;
783f6184 182
783f6184 183 retry:
e339ad31 184 set_buffer_dirty(nilfs->ns_sbh[0]);
3b2ce58b 185 if (nilfs_test_opt(nilfs, BARRIER)) {
87e99511 186 err = __sync_dirty_buffer(nilfs->ns_sbh[0],
70fd7614 187 REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
87e99511
CH
188 } else {
189 err = sync_dirty_buffer(nilfs->ns_sbh[0]);
783f6184 190 }
87e99511 191
e339ad31 192 if (unlikely(err)) {
a1d0747a 193 nilfs_err(sb, "unable to write superblock: err=%d", err);
e339ad31 194 if (err == -EIO && nilfs->ns_sbh[1]) {
b2ac86e1
JS
195 /*
196 * sbp[0] points to newer log than sbp[1],
197 * so copy sbp[0] to sbp[1] to take over sbp[0].
198 */
199 memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
200 nilfs->ns_sbsize);
e339ad31
RK
201 nilfs_fall_back_super_block(nilfs);
202 goto retry;
203 }
204 } else {
205 struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
206
b2ac86e1
JS
207 nilfs->ns_sbwcount++;
208
e339ad31
RK
209 /*
210 * The latest segment becomes trailable from the position
211 * written in superblock.
212 */
783f6184 213 clear_nilfs_discontinued(nilfs);
e339ad31
RK
214
215 /* update GC protection for recent segments */
216 if (nilfs->ns_sbh[1]) {
b2ac86e1 217 if (flag == NILFS_SB_COMMIT_ALL) {
e339ad31 218 set_buffer_dirty(nilfs->ns_sbh[1]);
b2ac86e1
JS
219 if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
220 goto out;
e339ad31 221 }
b2ac86e1
JS
222 if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
223 le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
224 sbp = nilfs->ns_sbp[1];
e339ad31 225 }
783f6184 226
b2ac86e1
JS
227 spin_lock(&nilfs->ns_last_segment_lock);
228 nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
229 spin_unlock(&nilfs->ns_last_segment_lock);
230 }
231 out:
783f6184
RK
232 return err;
233}
234
60f46b7e
RK
235void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
236 struct the_nilfs *nilfs)
237{
238 sector_t nfreeblocks;
239
240 /* nilfs->ns_sem must be locked by the caller. */
241 nilfs_count_free_blocks(nilfs, &nfreeblocks);
242 sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
243
244 spin_lock(&nilfs->ns_last_segment_lock);
245 sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
246 sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
247 sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
248 spin_unlock(&nilfs->ns_last_segment_lock);
249}
250
f7545144 251struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
b2ac86e1 252 int flip)
783f6184 253{
e3154e97 254 struct the_nilfs *nilfs = sb->s_fs_info;
e339ad31 255 struct nilfs_super_block **sbp = nilfs->ns_sbp;
783f6184 256
d26493b6 257 /* nilfs->ns_sem must be locked by the caller. */
34cb9b5c 258 if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
d26493b6
JS
259 if (sbp[1] &&
260 sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
b2ac86e1 261 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
d26493b6 262 } else {
a1d0747a 263 nilfs_crit(sb, "superblock broke");
d26493b6 264 return NULL;
e339ad31 265 }
b2ac86e1
JS
266 } else if (sbp[1] &&
267 sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
7592ecde 268 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
e339ad31 269 }
b2ac86e1
JS
270
271 if (flip && sbp[1])
272 nilfs_swap_super_block(nilfs);
273
d26493b6
JS
274 return sbp;
275}
276
f7545144 277int nilfs_commit_super(struct super_block *sb, int flag)
d26493b6 278{
e3154e97 279 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 280 struct nilfs_super_block **sbp = nilfs->ns_sbp;
fb04b91b 281 time64_t t;
d26493b6
JS
282
283 /* nilfs->ns_sem must be locked by the caller. */
fb04b91b 284 t = ktime_get_real_seconds();
b2ac86e1 285 nilfs->ns_sbwtime = t;
e339ad31
RK
286 sbp[0]->s_wtime = cpu_to_le64(t);
287 sbp[0]->s_sum = 0;
288 sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
289 (unsigned char *)sbp[0],
290 nilfs->ns_sbsize));
b2ac86e1
JS
291 if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
292 sbp[1]->s_wtime = sbp[0]->s_wtime;
293 sbp[1]->s_sum = 0;
294 sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
295 (unsigned char *)sbp[1],
296 nilfs->ns_sbsize));
e339ad31 297 }
e605f0a7 298 clear_nilfs_sb_dirty(nilfs);
e2c7617a
AR
299 nilfs->ns_flushed_device = 1;
300 /* make sure store to ns_flushed_device cannot be reordered */
301 smp_wmb();
f7545144 302 return nilfs_sync_super(sb, flag);
783f6184
RK
303}
304
7ecaa46c
RK
305/**
306 * nilfs_cleanup_super() - write filesystem state for cleanup
f7545144 307 * @sb: super block instance to be unmounted or degraded to read-only
7ecaa46c
RK
308 *
309 * This function restores state flags in the on-disk super block.
310 * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
311 * filesystem was not clean previously.
fd4e7fad
RK
312 *
313 * Return: 0 on success, %-EIO if I/O error or superblock is corrupted.
7ecaa46c 314 */
f7545144 315int nilfs_cleanup_super(struct super_block *sb)
7ecaa46c 316{
e3154e97 317 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 318 struct nilfs_super_block **sbp;
b2ac86e1 319 int flag = NILFS_SB_COMMIT;
d26493b6 320 int ret = -EIO;
7ecaa46c 321
f7545144 322 sbp = nilfs_prepare_super(sb, 0);
d26493b6 323 if (sbp) {
f7545144
RK
324 sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
325 nilfs_set_log_cursor(sbp[0], nilfs);
b2ac86e1
JS
326 if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
327 /*
328 * make the "clean" flag also to the opposite
329 * super block if both super blocks point to
330 * the same checkpoint.
331 */
332 sbp[1]->s_state = sbp[0]->s_state;
333 flag = NILFS_SB_COMMIT_ALL;
334 }
f7545144 335 ret = nilfs_commit_super(sb, flag);
d26493b6 336 }
7ecaa46c
RK
337 return ret;
338}
339
cfb0a4bf
RK
340/**
341 * nilfs_move_2nd_super - relocate secondary super block
342 * @sb: super block instance
343 * @sb2off: new offset of the secondary super block (in bytes)
fd4e7fad
RK
344 *
345 * Return: 0 on success, or a negative error code on failure.
cfb0a4bf
RK
346 */
347static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
348{
349 struct the_nilfs *nilfs = sb->s_fs_info;
350 struct buffer_head *nsbh;
351 struct nilfs_super_block *nsbp;
352 sector_t blocknr, newblocknr;
353 unsigned long offset;
4f05028f 354 int sb2i; /* array index of the secondary superblock */
cfb0a4bf
RK
355 int ret = 0;
356
357 /* nilfs->ns_sem must be locked by the caller. */
358 if (nilfs->ns_sbh[1] &&
359 nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
360 sb2i = 1;
361 blocknr = nilfs->ns_sbh[1]->b_blocknr;
362 } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
363 sb2i = 0;
364 blocknr = nilfs->ns_sbh[0]->b_blocknr;
4f05028f
RK
365 } else {
366 sb2i = -1;
367 blocknr = 0;
cfb0a4bf
RK
368 }
369 if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
370 goto out; /* super block location is unchanged */
371
372 /* Get new super block buffer */
373 newblocknr = sb2off >> nilfs->ns_blocksize_bits;
374 offset = sb2off & (nilfs->ns_blocksize - 1);
375 nsbh = sb_getblk(sb, newblocknr);
376 if (!nsbh) {
a1d0747a
JP
377 nilfs_warn(sb,
378 "unable to move secondary superblock to block %llu",
379 (unsigned long long)newblocknr);
cfb0a4bf
RK
380 ret = -EIO;
381 goto out;
382 }
383 nsbp = (void *)nsbh->b_data + offset;
cfb0a4bf 384
679bd7eb 385 lock_buffer(nsbh);
cfb0a4bf 386 if (sb2i >= 0) {
679bd7eb
RK
387 /*
388 * The position of the second superblock only changes by 4KiB,
389 * which is larger than the maximum superblock data size
390 * (= 1KiB), so there is no need to use memmove() to allow
391 * overlap between source and destination.
392 */
cfb0a4bf 393 memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
679bd7eb
RK
394
395 /*
396 * Zero fill after copy to avoid overwriting in case of move
397 * within the same block.
398 */
399 memset(nsbh->b_data, 0, offset);
400 memset((void *)nsbp + nilfs->ns_sbsize, 0,
401 nsbh->b_size - offset - nilfs->ns_sbsize);
402 } else {
403 memset(nsbh->b_data, 0, nsbh->b_size);
404 }
405 set_buffer_uptodate(nsbh);
406 unlock_buffer(nsbh);
407
408 if (sb2i >= 0) {
cfb0a4bf
RK
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)
fd4e7fad
RK
427 *
428 * Return: 0 on success, or a negative error code on failure.
4e33f9ea
RK
429 */
430int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
431{
432 struct the_nilfs *nilfs = sb->s_fs_info;
433 struct nilfs_super_block **sbp;
434 __u64 devsize, newnsegs;
435 loff_t sb2off;
436 int ret;
437
438 ret = -ERANGE;
4fcd6979 439 devsize = bdev_nr_bytes(sb->s_bdev);
4e33f9ea
RK
440 if (newsize > devsize)
441 goto out;
442
99b9402a
RK
443 /*
444 * Prevent underflow in second superblock position calculation.
445 * The exact minimum size check is done in nilfs_sufile_resize().
446 */
447 if (newsize < 4096) {
448 ret = -ENOSPC;
449 goto out;
450 }
451
4e33f9ea
RK
452 /*
453 * Write lock is required to protect some functions depending
454 * on the number of segments, the number of reserved segments,
455 * and so forth.
456 */
457 down_write(&nilfs->ns_segctor_sem);
458
459 sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
460 newnsegs = sb2off >> nilfs->ns_blocksize_bits;
adc2c8d0 461 newnsegs = div64_ul(newnsegs, nilfs->ns_blocks_per_segment);
4e33f9ea
RK
462
463 ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
464 up_write(&nilfs->ns_segctor_sem);
465 if (ret < 0)
466 goto out;
467
468 ret = nilfs_construct_segment(sb);
469 if (ret < 0)
470 goto out;
471
472 down_write(&nilfs->ns_sem);
473 nilfs_move_2nd_super(sb, sb2off);
474 ret = -EIO;
475 sbp = nilfs_prepare_super(sb, 0);
476 if (likely(sbp)) {
477 nilfs_set_log_cursor(sbp[0], nilfs);
478 /*
479 * Drop NILFS_RESIZE_FS flag for compatibility with
480 * mount-time resize which may be implemented in a
481 * future release.
482 */
483 sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
484 ~NILFS_RESIZE_FS);
485 sbp[0]->s_dev_size = cpu_to_le64(newsize);
486 sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
487 if (sbp[1])
488 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
489 ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
490 }
491 up_write(&nilfs->ns_sem);
492
493 /*
494 * Reset the range of allocatable segments last. This order
495 * is important in the case of expansion because the secondary
496 * superblock must be protected from log write until migration
497 * completes.
498 */
499 if (!ret)
500 nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
501out:
502 return ret;
503}
504
783f6184
RK
505static void nilfs_put_super(struct super_block *sb)
506{
e3154e97 507 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 508
f7545144 509 nilfs_detach_log_writer(sb);
783f6184 510
bc98a42c 511 if (!sb_rdonly(sb)) {
783f6184 512 down_write(&nilfs->ns_sem);
f7545144 513 nilfs_cleanup_super(sb);
783f6184
RK
514 up_write(&nilfs->ns_sem);
515 }
516
42560f9c 517 nilfs_sysfs_delete_device_group(nilfs);
f1e89c86
RK
518 iput(nilfs->ns_sufile);
519 iput(nilfs->ns_cpfile);
520 iput(nilfs->ns_dat);
521
348fe8da 522 destroy_nilfs(nilfs);
783f6184 523 sb->s_fs_info = NULL;
783f6184
RK
524}
525
783f6184
RK
526static int nilfs_sync_fs(struct super_block *sb, int wait)
527{
e3154e97 528 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6 529 struct nilfs_super_block **sbp;
783f6184
RK
530 int err = 0;
531
532 /* This function is called when super block should be written back */
533 if (wait)
534 err = nilfs_construct_segment(sb);
6233caa9
JS
535
536 down_write(&nilfs->ns_sem);
d26493b6 537 if (nilfs_sb_dirty(nilfs)) {
f7545144 538 sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
b2ac86e1
JS
539 if (likely(sbp)) {
540 nilfs_set_log_cursor(sbp[0], nilfs);
f7545144 541 nilfs_commit_super(sb, NILFS_SB_COMMIT);
b2ac86e1 542 }
d26493b6 543 }
6233caa9
JS
544 up_write(&nilfs->ns_sem);
545
e2c7617a
AR
546 if (!err)
547 err = nilfs_flush_device(nilfs);
548
783f6184
RK
549 return err;
550}
551
f7545144 552int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
4d8d9293 553 struct nilfs_root **rootp)
783f6184 554{
e3154e97 555 struct the_nilfs *nilfs = sb->s_fs_info;
4d8d9293 556 struct nilfs_root *root;
4d8d9293 557 int err = -ENOMEM;
783f6184 558
4d8d9293
RK
559 root = nilfs_find_or_create_root(
560 nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
561 if (!root)
562 return err;
783f6184 563
e912a5b6
RK
564 if (root->ifile)
565 goto reuse; /* already attached checkpoint */
783f6184 566
1154ecbd 567 down_read(&nilfs->ns_segctor_sem);
1244a6d7 568 err = nilfs_ifile_read(sb, root, cno, nilfs->ns_inode_size);
1154ecbd 569 up_read(&nilfs->ns_segctor_sem);
1244a6d7 570 if (unlikely(err))
783f6184 571 goto failed;
4d8d9293 572
e912a5b6 573 reuse:
4d8d9293 574 *rootp = root;
783f6184
RK
575 return 0;
576
783f6184 577 failed:
1244a6d7
RK
578 if (err == -EINVAL)
579 nilfs_err(sb, "Invalid checkpoint (checkpoint number=%llu)",
580 (unsigned long long)cno);
4d8d9293 581 nilfs_put_root(root);
783f6184
RK
582
583 return err;
584}
783f6184 585
5beb6e0b
RK
586static int nilfs_freeze(struct super_block *sb)
587{
e3154e97 588 struct the_nilfs *nilfs = sb->s_fs_info;
5beb6e0b
RK
589 int err;
590
bc98a42c 591 if (sb_rdonly(sb))
5beb6e0b 592 return 0;
783f6184 593
5beb6e0b
RK
594 /* Mark super block clean */
595 down_write(&nilfs->ns_sem);
f7545144 596 err = nilfs_cleanup_super(sb);
5beb6e0b 597 up_write(&nilfs->ns_sem);
783f6184
RK
598 return err;
599}
600
5beb6e0b 601static int nilfs_unfreeze(struct super_block *sb)
783f6184 602{
e3154e97 603 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 604
bc98a42c 605 if (sb_rdonly(sb))
5beb6e0b
RK
606 return 0;
607
608 down_write(&nilfs->ns_sem);
f7545144 609 nilfs_setup_super(sb, false);
5beb6e0b
RK
610 up_write(&nilfs->ns_sem);
611 return 0;
783f6184
RK
612}
613
783f6184
RK
614static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
615{
616 struct super_block *sb = dentry->d_sb;
2b0143b5 617 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b7c06342 618 struct the_nilfs *nilfs = root->nilfs;
c306af23 619 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
783f6184
RK
620 unsigned long long blocks;
621 unsigned long overhead;
622 unsigned long nrsvblocks;
623 sector_t nfreeblocks;
c7ef972c 624 u64 nmaxinodes, nfreeinodes;
783f6184
RK
625 int err;
626
627 /*
628 * Compute all of the segment blocks
629 *
630 * The blocks before first segment and after last segment
631 * are excluded.
632 */
633 blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
634 - nilfs->ns_first_data_block;
635 nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
636
637 /*
638 * Compute the overhead
639 *
7a65004b 640 * When distributing meta data blocks outside segment structure,
783f6184
RK
641 * We must count them as the overhead.
642 */
643 overhead = 0;
644
645 err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
646 if (unlikely(err))
647 return err;
648
c7ef972c
VD
649 err = nilfs_ifile_count_free_inodes(root->ifile,
650 &nmaxinodes, &nfreeinodes);
651 if (unlikely(err)) {
a1d0747a 652 nilfs_warn(sb, "failed to count free inodes: err=%d", err);
c7ef972c
VD
653 if (err == -ERANGE) {
654 /*
655 * If nilfs_palloc_count_max_entries() returns
656 * -ERANGE error code then we simply treat
657 * curent inodes count as maximum possible and
658 * zero as free inodes value.
659 */
e5f7f848 660 nmaxinodes = atomic64_read(&root->inodes_count);
c7ef972c
VD
661 nfreeinodes = 0;
662 err = 0;
663 } else
664 return err;
665 }
666
783f6184
RK
667 buf->f_type = NILFS_SUPER_MAGIC;
668 buf->f_bsize = sb->s_blocksize;
669 buf->f_blocks = blocks - overhead;
670 buf->f_bfree = nfreeblocks;
671 buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
672 (buf->f_bfree - nrsvblocks) : 0;
c7ef972c
VD
673 buf->f_files = nmaxinodes;
674 buf->f_ffree = nfreeinodes;
783f6184 675 buf->f_namelen = NILFS_NAME_LEN;
6d1349c7 676 buf->f_fsid = u64_to_fsid(id);
c306af23 677
783f6184
RK
678 return 0;
679}
680
34c80b1d 681static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
b58a285b 682{
34c80b1d 683 struct super_block *sb = dentry->d_sb;
e3154e97 684 struct the_nilfs *nilfs = sb->s_fs_info;
2b0143b5 685 struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
b58a285b 686
3b2ce58b 687 if (!nilfs_test_opt(nilfs, BARRIER))
c6b4d57d 688 seq_puts(seq, ",nobarrier");
f11459ad
RK
689 if (root->cno != NILFS_CPTREE_CURRENT_CNO)
690 seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
3b2ce58b 691 if (nilfs_test_opt(nilfs, ERRORS_PANIC))
c6b4d57d 692 seq_puts(seq, ",errors=panic");
3b2ce58b 693 if (nilfs_test_opt(nilfs, ERRORS_CONT))
c6b4d57d 694 seq_puts(seq, ",errors=continue");
3b2ce58b 695 if (nilfs_test_opt(nilfs, STRICT_ORDER))
c6b4d57d 696 seq_puts(seq, ",order=strict");
3b2ce58b 697 if (nilfs_test_opt(nilfs, NORECOVERY))
c6b4d57d 698 seq_puts(seq, ",norecovery");
3b2ce58b 699 if (nilfs_test_opt(nilfs, DISCARD))
c6b4d57d 700 seq_puts(seq, ",discard");
b58a285b
JS
701
702 return 0;
703}
704
b87221de 705static const struct super_operations nilfs_sops = {
783f6184 706 .alloc_inode = nilfs_alloc_inode,
977c3d18 707 .free_inode = nilfs_free_inode,
783f6184 708 .dirty_inode = nilfs_dirty_inode,
6fd1e5c9 709 .evict_inode = nilfs_evict_inode,
783f6184 710 .put_super = nilfs_put_super,
783f6184 711 .sync_fs = nilfs_sync_fs,
5beb6e0b
RK
712 .freeze_fs = nilfs_freeze,
713 .unfreeze_fs = nilfs_unfreeze,
783f6184 714 .statfs = nilfs_statfs,
b58a285b 715 .show_options = nilfs_show_options
783f6184
RK
716};
717
783f6184 718enum {
36defdd9
ES
719 Opt_err, Opt_barrier, Opt_snapshot, Opt_order, Opt_norecovery,
720 Opt_discard,
783f6184
RK
721};
722
36defdd9
ES
723static const struct constant_table nilfs_param_err[] = {
724 {"continue", NILFS_MOUNT_ERRORS_CONT},
725 {"panic", NILFS_MOUNT_ERRORS_PANIC},
726 {"remount-ro", NILFS_MOUNT_ERRORS_RO},
727 {}
783f6184
RK
728};
729
36defdd9
ES
730static const struct fs_parameter_spec nilfs_param_spec[] = {
731 fsparam_enum ("errors", Opt_err, nilfs_param_err),
732 fsparam_flag_no ("barrier", Opt_barrier),
733 fsparam_u64 ("cp", Opt_snapshot),
734 fsparam_string ("order", Opt_order),
735 fsparam_flag ("norecovery", Opt_norecovery),
736 fsparam_flag_no ("discard", Opt_discard),
737 {}
738};
4ad364ca 739
36defdd9
ES
740struct nilfs_fs_context {
741 unsigned long ns_mount_opt;
742 __u64 cno;
743};
783f6184 744
36defdd9
ES
745static int nilfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
746{
747 struct nilfs_fs_context *nilfs = fc->fs_private;
748 int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
749 struct fs_parse_result result;
750 int opt;
751
752 opt = fs_parse(fc, nilfs_param_spec, param, &result);
753 if (opt < 0)
754 return opt;
755
756 switch (opt) {
757 case Opt_barrier:
758 if (result.negated)
3b2ce58b 759 nilfs_clear_opt(nilfs, BARRIER);
36defdd9
ES
760 else
761 nilfs_set_opt(nilfs, BARRIER);
762 break;
763 case Opt_order:
764 if (strcmp(param->string, "relaxed") == 0)
765 /* Ordered data semantics */
766 nilfs_clear_opt(nilfs, STRICT_ORDER);
767 else if (strcmp(param->string, "strict") == 0)
768 /* Strict in-order semantics */
769 nilfs_set_opt(nilfs, STRICT_ORDER);
770 else
771 return -EINVAL;
772 break;
773 case Opt_err:
774 nilfs->ns_mount_opt &= ~NILFS_MOUNT_ERROR_MODE;
775 nilfs->ns_mount_opt |= result.uint_32;
776 break;
777 case Opt_snapshot:
778 if (is_remount) {
779 struct super_block *sb = fc->root->d_sb;
780
781 nilfs_err(sb,
782 "\"%s\" option is invalid for remount",
783 param->key);
784 return -EINVAL;
785 }
786 if (result.uint_64 == 0) {
787 nilfs_err(NULL,
788 "invalid option \"cp=0\": invalid checkpoint number 0");
789 return -EINVAL;
783f6184 790 }
36defdd9
ES
791 nilfs->cno = result.uint_64;
792 break;
793 case Opt_norecovery:
794 nilfs_set_opt(nilfs, NORECOVERY);
795 break;
796 case Opt_discard:
797 if (result.negated)
798 nilfs_clear_opt(nilfs, DISCARD);
799 else
800 nilfs_set_opt(nilfs, DISCARD);
801 break;
802 default:
803 return -EINVAL;
783f6184 804 }
3b2ce58b 805
36defdd9 806 return 0;
783f6184
RK
807}
808
f7545144 809static int nilfs_setup_super(struct super_block *sb, int is_mount)
783f6184 810{
e3154e97 811 struct the_nilfs *nilfs = sb->s_fs_info;
d26493b6
JS
812 struct nilfs_super_block **sbp;
813 int max_mnt_count;
814 int mnt_count;
815
816 /* nilfs->ns_sem must be locked by the caller. */
f7545144 817 sbp = nilfs_prepare_super(sb, 0);
d26493b6
JS
818 if (!sbp)
819 return -EIO;
820
5beb6e0b
RK
821 if (!is_mount)
822 goto skip_mount_setup;
823
d26493b6
JS
824 max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
825 mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
783f6184 826
f50a4c81 827 if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
a1d0747a 828 nilfs_warn(sb, "mounting fs with errors");
783f6184
RK
829#if 0
830 } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
a1d0747a 831 nilfs_warn(sb, "maximal mount count reached");
783f6184
RK
832#endif
833 }
834 if (!max_mnt_count)
d26493b6 835 sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
783f6184 836
d26493b6 837 sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
21a1a52d 838 sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
5beb6e0b
RK
839
840skip_mount_setup:
d26493b6
JS
841 sbp[0]->s_state =
842 cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
b2ac86e1 843 /* synchronize sbp[1] with sbp[0] */
0ca7a5b9
RK
844 if (sbp[1])
845 memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
f7545144 846 return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
783f6184
RK
847}
848
e339ad31
RK
849struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
850 u64 pos, int blocksize,
851 struct buffer_head **pbh)
783f6184 852{
e339ad31
RK
853 unsigned long long sb_index = pos;
854 unsigned long offset;
783f6184 855
e339ad31 856 offset = do_div(sb_index, blocksize);
783f6184 857 *pbh = sb_bread(sb, sb_index);
e339ad31 858 if (!*pbh)
783f6184 859 return NULL;
783f6184
RK
860 return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
861}
862
36defdd9
ES
863int nilfs_store_magic(struct super_block *sb,
864 struct nilfs_super_block *sbp)
783f6184 865{
e3154e97 866 struct the_nilfs *nilfs = sb->s_fs_info;
783f6184 867
783f6184
RK
868 sb->s_magic = le16_to_cpu(sbp->s_magic);
869
870 /* FS independent flags */
871#ifdef NILFS_ATIME_DISABLE
1751e8a6 872 sb->s_flags |= SB_NOATIME;
783f6184
RK
873#endif
874
574e6c31
RK
875 nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
876 nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
877 nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
878 nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
783f6184 879
36defdd9 880 return 0;
783f6184
RK
881}
882
c5ca48aa
RK
883int nilfs_check_feature_compatibility(struct super_block *sb,
884 struct nilfs_super_block *sbp)
885{
886 __u64 features;
887
888 features = le64_to_cpu(sbp->s_feature_incompat) &
889 ~NILFS_FEATURE_INCOMPAT_SUPP;
890 if (features) {
a1d0747a 891 nilfs_err(sb,
feee880f
RK
892 "couldn't mount because of unsupported optional features (%llx)",
893 (unsigned long long)features);
c5ca48aa
RK
894 return -EINVAL;
895 }
896 features = le64_to_cpu(sbp->s_feature_compat_ro) &
897 ~NILFS_FEATURE_COMPAT_RO_SUPP;
bc98a42c 898 if (!sb_rdonly(sb) && features) {
a1d0747a 899 nilfs_err(sb,
feee880f
RK
900 "couldn't mount RDWR because of unsupported optional features (%llx)",
901 (unsigned long long)features);
c5ca48aa
RK
902 return -EINVAL;
903 }
904 return 0;
905}
906
367ea334
RK
907static int nilfs_get_root_dentry(struct super_block *sb,
908 struct nilfs_root *root,
909 struct dentry **root_dentry)
910{
911 struct inode *inode;
912 struct dentry *dentry;
913 int ret = 0;
914
915 inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
916 if (IS_ERR(inode)) {
367ea334 917 ret = PTR_ERR(inode);
a1d0747a 918 nilfs_err(sb, "error %d getting root inode", ret);
367ea334
RK
919 goto out;
920 }
921 if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
922 iput(inode);
a1d0747a 923 nilfs_err(sb, "corrupt root inode");
367ea334
RK
924 ret = -EINVAL;
925 goto out;
926 }
927
f11459ad
RK
928 if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
929 dentry = d_find_alias(inode);
930 if (!dentry) {
48fde701 931 dentry = d_make_root(inode);
f11459ad 932 if (!dentry) {
f11459ad
RK
933 ret = -ENOMEM;
934 goto failed_dentry;
935 }
936 } else {
937 iput(inode);
938 }
939 } else {
1a0a397e 940 dentry = d_obtain_root(inode);
f11459ad
RK
941 if (IS_ERR(dentry)) {
942 ret = PTR_ERR(dentry);
943 goto failed_dentry;
944 }
367ea334
RK
945 }
946 *root_dentry = dentry;
947 out:
948 return ret;
f11459ad
RK
949
950 failed_dentry:
a1d0747a 951 nilfs_err(sb, "error %d getting root dentry", ret);
f11459ad 952 goto out;
367ea334
RK
953}
954
ab4d8f7e
RK
955static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
956 struct dentry **root_dentry)
957{
e3154e97 958 struct the_nilfs *nilfs = s->s_fs_info;
ab4d8f7e
RK
959 struct nilfs_root *root;
960 int ret;
961
572d8b39
RK
962 mutex_lock(&nilfs->ns_snapshot_mount_mutex);
963
ab4d8f7e
RK
964 down_read(&nilfs->ns_segctor_sem);
965 ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
966 up_read(&nilfs->ns_segctor_sem);
967 if (ret < 0) {
968 ret = (ret == -ENOENT) ? -EINVAL : ret;
969 goto out;
970 } else if (!ret) {
a1d0747a 971 nilfs_err(s,
feee880f
RK
972 "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
973 (unsigned long long)cno);
ab4d8f7e
RK
974 ret = -EINVAL;
975 goto out;
976 }
977
f7545144 978 ret = nilfs_attach_checkpoint(s, cno, false, &root);
ab4d8f7e 979 if (ret) {
a1d0747a 980 nilfs_err(s,
feee880f
RK
981 "error %d while loading snapshot (checkpoint number=%llu)",
982 ret, (unsigned long long)cno);
ab4d8f7e
RK
983 goto out;
984 }
985 ret = nilfs_get_root_dentry(s, root, root_dentry);
986 nilfs_put_root(root);
987 out:
572d8b39 988 mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
ab4d8f7e
RK
989 return ret;
990}
991
f11459ad 992/**
e95c311e 993 * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
f11459ad
RK
994 * @root_dentry: root dentry of the tree to be shrunk
995 *
fd4e7fad 996 * Return: true if the tree was in-use, false otherwise.
f11459ad 997 */
e95c311e 998static bool nilfs_tree_is_busy(struct dentry *root_dentry)
f11459ad 999{
f11459ad 1000 shrink_dcache_parent(root_dentry);
e95c311e 1001 return d_count(root_dentry) > 1;
f11459ad
RK
1002}
1003
032dbb3b
RK
1004int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1005{
e3154e97 1006 struct the_nilfs *nilfs = sb->s_fs_info;
032dbb3b
RK
1007 struct nilfs_root *root;
1008 struct inode *inode;
1009 struct dentry *dentry;
1010 int ret;
1011
3377f843 1012 if (cno > nilfs->ns_cno)
032dbb3b
RK
1013 return false;
1014
1015 if (cno >= nilfs_last_cno(nilfs))
1016 return true; /* protect recent checkpoints */
1017
1018 ret = false;
e3154e97 1019 root = nilfs_lookup_root(nilfs, cno);
032dbb3b
RK
1020 if (root) {
1021 inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1022 if (inode) {
1023 dentry = d_find_alias(inode);
1024 if (dentry) {
e95c311e 1025 ret = nilfs_tree_is_busy(dentry);
032dbb3b
RK
1026 dput(dentry);
1027 }
1028 iput(inode);
1029 }
1030 nilfs_put_root(root);
1031 }
1032 return ret;
1033}
1034
783f6184
RK
1035/**
1036 * nilfs_fill_super() - initialize a super block instance
1037 * @sb: super_block
36defdd9 1038 * @fc: filesystem context
783f6184 1039 *
aa7dfb89 1040 * This function is called exclusively by nilfs->ns_mount_mutex.
783f6184 1041 * So, the recovery process is protected from other simultaneous mounts.
fd4e7fad
RK
1042 *
1043 * Return: 0 on success, or a negative error code on failure.
783f6184
RK
1044 */
1045static int
36defdd9 1046nilfs_fill_super(struct super_block *sb, struct fs_context *fc)
783f6184 1047{
348fe8da 1048 struct the_nilfs *nilfs;
4d8d9293 1049 struct nilfs_root *fsroot;
36defdd9 1050 struct nilfs_fs_context *ctx = fc->fs_private;
783f6184
RK
1051 __u64 cno;
1052 int err;
1053
6625689e 1054 nilfs = alloc_nilfs(sb);
e3154e97 1055 if (!nilfs)
783f6184
RK
1056 return -ENOMEM;
1057
e3154e97 1058 sb->s_fs_info = nilfs;
783f6184 1059
36defdd9 1060 err = init_nilfs(nilfs, sb);
783f6184 1061 if (err)
348fe8da 1062 goto failed_nilfs;
783f6184 1063
36defdd9
ES
1064 /* Copy in parsed mount options */
1065 nilfs->ns_mount_opt = ctx->ns_mount_opt;
1066
783f6184
RK
1067 sb->s_op = &nilfs_sops;
1068 sb->s_export_op = &nilfs_export_ops;
1069 sb->s_root = NULL;
61239230 1070 sb->s_time_gran = 1;
8de52778 1071 sb->s_max_links = NILFS_LINK_MAX;
026a7d63 1072
a11d7fc2 1073 sb->s_bdi = bdi_get(sb->s_bdev->bd_disk->bdi);
783f6184 1074
f7545144 1075 err = load_nilfs(nilfs, sb);
f50a4c81 1076 if (err)
348fe8da 1077 goto failed_nilfs;
f50a4c81 1078
ef851d44
RK
1079 super_set_uuid(sb, nilfs->ns_sbp[0]->s_uuid,
1080 sizeof(nilfs->ns_sbp[0]->s_uuid));
8d1dba2e 1081 super_set_sysfs_name_bdev(sb);
ef851d44 1082
783f6184 1083 cno = nilfs_last_cno(nilfs);
f7545144 1084 err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
783f6184 1085 if (err) {
a1d0747a 1086 nilfs_err(sb,
feee880f
RK
1087 "error %d while loading last checkpoint (checkpoint number=%llu)",
1088 err, (unsigned long long)cno);
f1e89c86 1089 goto failed_unload;
783f6184
RK
1090 }
1091
bc98a42c 1092 if (!sb_rdonly(sb)) {
f7545144 1093 err = nilfs_attach_log_writer(sb, fsroot);
783f6184
RK
1094 if (err)
1095 goto failed_checkpoint;
1096 }
1097
367ea334
RK
1098 err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1099 if (err)
783f6184 1100 goto failed_segctor;
783f6184 1101
4d8d9293 1102 nilfs_put_root(fsroot);
783f6184 1103
bc98a42c 1104 if (!sb_rdonly(sb)) {
783f6184 1105 down_write(&nilfs->ns_sem);
f7545144 1106 nilfs_setup_super(sb, true);
783f6184
RK
1107 up_write(&nilfs->ns_sem);
1108 }
1109
783f6184
RK
1110 return 0;
1111
783f6184 1112 failed_segctor:
f7545144 1113 nilfs_detach_log_writer(sb);
783f6184
RK
1114
1115 failed_checkpoint:
4d8d9293 1116 nilfs_put_root(fsroot);
783f6184 1117
f1e89c86 1118 failed_unload:
42560f9c 1119 nilfs_sysfs_delete_device_group(nilfs);
f1e89c86
RK
1120 iput(nilfs->ns_sufile);
1121 iput(nilfs->ns_cpfile);
1122 iput(nilfs->ns_dat);
1123
348fe8da
RK
1124 failed_nilfs:
1125 destroy_nilfs(nilfs);
783f6184
RK
1126 return err;
1127}
1128
36defdd9 1129static int nilfs_reconfigure(struct fs_context *fc)
783f6184 1130{
36defdd9
ES
1131 struct nilfs_fs_context *ctx = fc->fs_private;
1132 struct super_block *sb = fc->root->d_sb;
e3154e97 1133 struct the_nilfs *nilfs = sb->s_fs_info;
f11459ad 1134 int err;
783f6184 1135
02b9984d 1136 sync_filesystem(sb);
783f6184 1137
d240e067 1138 err = -EINVAL;
783f6184 1139
0234576d 1140 if (!nilfs_valid_fs(nilfs)) {
a1d0747a
JP
1141 nilfs_warn(sb,
1142 "couldn't remount because the filesystem is in an incomplete recovery state");
36defdd9 1143 goto ignore_opts;
0234576d 1144 }
36defdd9 1145 if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
783f6184 1146 goto out;
36defdd9 1147 if (fc->sb_flags & SB_RDONLY) {
1751e8a6 1148 sb->s_flags |= SB_RDONLY;
783f6184 1149
783f6184
RK
1150 /*
1151 * Remounting a valid RW partition RDONLY, so set
1152 * the RDONLY flag and then mark the partition as valid again.
1153 */
1154 down_write(&nilfs->ns_sem);
f7545144 1155 nilfs_cleanup_super(sb);
783f6184
RK
1156 up_write(&nilfs->ns_sem);
1157 } else {
c5ca48aa 1158 __u64 features;
e912a5b6 1159 struct nilfs_root *root;
c5ca48aa 1160
783f6184
RK
1161 /*
1162 * Mounting a RDONLY partition read-write, so reread and
1163 * store the current valid flag. (It may have been changed
1164 * by fsck since we originally mounted the partition.)
1165 */
c5ca48aa
RK
1166 down_read(&nilfs->ns_sem);
1167 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1168 ~NILFS_FEATURE_COMPAT_RO_SUPP;
1169 up_read(&nilfs->ns_sem);
1170 if (features) {
a1d0747a
JP
1171 nilfs_warn(sb,
1172 "couldn't remount RDWR because of unsupported optional features (%llx)",
1173 (unsigned long long)features);
c5ca48aa 1174 err = -EROFS;
36defdd9 1175 goto ignore_opts;
c5ca48aa
RK
1176 }
1177
1751e8a6 1178 sb->s_flags &= ~SB_RDONLY;
783f6184 1179
2b0143b5 1180 root = NILFS_I(d_inode(sb->s_root))->i_root;
f7545144 1181 err = nilfs_attach_log_writer(sb, root);
36defdd9
ES
1182 if (err) {
1183 sb->s_flags |= SB_RDONLY;
1184 goto ignore_opts;
1185 }
783f6184
RK
1186
1187 down_write(&nilfs->ns_sem);
f7545144 1188 nilfs_setup_super(sb, true);
783f6184 1189 up_write(&nilfs->ns_sem);
783f6184
RK
1190 }
1191 out:
36defdd9
ES
1192 sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
1193 /* Copy over parsed remount options */
1194 nilfs->ns_mount_opt = ctx->ns_mount_opt;
783f6184 1195
783f6184 1196 return 0;
783f6184 1197
36defdd9
ES
1198 ignore_opts:
1199 return err;
783f6184
RK
1200}
1201
36defdd9
ES
1202static int
1203nilfs_get_tree(struct fs_context *fc)
783f6184 1204{
36defdd9 1205 struct nilfs_fs_context *ctx = fc->fs_private;
33c8e57c 1206 struct super_block *s;
c1e012ea
CH
1207 dev_t dev;
1208 int err;
783f6184 1209
36defdd9
ES
1210 if (ctx->cno && !(fc->sb_flags & SB_RDONLY)) {
1211 nilfs_err(NULL,
1212 "invalid option \"cp=%llu\": read-only option is not specified",
1213 ctx->cno);
1214 return -EINVAL;
1215 }
783f6184 1216
36defdd9 1217 err = lookup_bdev(fc->source, &dev);
c1e012ea 1218 if (err)
36defdd9 1219 return err;
783f6184 1220
36defdd9 1221 s = sget_dev(fc, dev);
c1e012ea 1222 if (IS_ERR(s))
36defdd9 1223 return PTR_ERR(s);
783f6184
RK
1224
1225 if (!s->s_root) {
36defdd9 1226 err = setup_bdev_super(s, fc->sb_flags, fc);
c1e012ea 1227 if (!err)
36defdd9 1228 err = nilfs_fill_super(s, fc);
783f6184 1229 if (err)
348fe8da 1230 goto failed_super;
783f6184 1231
1751e8a6 1232 s->s_flags |= SB_ACTIVE;
36defdd9 1233 } else if (!ctx->cno) {
e95c311e 1234 if (nilfs_tree_is_busy(s->s_root)) {
36defdd9 1235 if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
a1d0747a 1236 nilfs_err(s,
feee880f 1237 "the device already has a %s mount.",
bc98a42c 1238 sb_rdonly(s) ? "read-only" : "read/write");
f11459ad
RK
1239 err = -EBUSY;
1240 goto failed_super;
1241 }
e95c311e 1242 } else {
f11459ad 1243 /*
36defdd9 1244 * Try reconfigure to setup mount states if the current
f11459ad 1245 * tree is not mounted and only snapshots use this sb.
36defdd9
ES
1246 *
1247 * Since nilfs_reconfigure() requires fc->root to be
1248 * set, set it first and release it on failure.
f11459ad 1249 */
36defdd9
ES
1250 fc->root = dget(s->s_root);
1251 err = nilfs_reconfigure(fc);
1252 if (err) {
1253 dput(fc->root);
1254 fc->root = NULL; /* prevent double release */
f11459ad 1255 goto failed_super;
36defdd9
ES
1256 }
1257 return 0;
f11459ad 1258 }
783f6184
RK
1259 }
1260
36defdd9 1261 if (ctx->cno) {
c1e012ea
CH
1262 struct dentry *root_dentry;
1263
36defdd9 1264 err = nilfs_attach_snapshot(s, ctx->cno, &root_dentry);
348fe8da 1265 if (err)
f11459ad 1266 goto failed_super;
36defdd9
ES
1267 fc->root = root_dentry;
1268 return 0;
783f6184
RK
1269 }
1270
36defdd9
ES
1271 fc->root = dget(s->s_root);
1272 return 0;
783f6184 1273
f11459ad 1274 failed_super:
a95161aa 1275 deactivate_locked_super(s);
36defdd9
ES
1276 return err;
1277}
1278
1279static void nilfs_free_fc(struct fs_context *fc)
1280{
1281 kfree(fc->fs_private);
1282}
1283
1284static const struct fs_context_operations nilfs_context_ops = {
1285 .parse_param = nilfs_parse_param,
1286 .get_tree = nilfs_get_tree,
1287 .reconfigure = nilfs_reconfigure,
1288 .free = nilfs_free_fc,
1289};
1290
1291static int nilfs_init_fs_context(struct fs_context *fc)
1292{
1293 struct nilfs_fs_context *ctx;
1294
1295 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1296 if (!ctx)
1297 return -ENOMEM;
1298
1299 ctx->ns_mount_opt = NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
1300 fc->fs_private = ctx;
1301 fc->ops = &nilfs_context_ops;
1302
1303 return 0;
783f6184
RK
1304}
1305
783f6184
RK
1306struct file_system_type nilfs_fs_type = {
1307 .owner = THIS_MODULE,
1308 .name = "nilfs2",
783f6184
RK
1309 .kill_sb = kill_block_super,
1310 .fs_flags = FS_REQUIRES_DEV,
36defdd9
ES
1311 .init_fs_context = nilfs_init_fs_context,
1312 .parameters = nilfs_param_spec,
783f6184 1313};
7f78e035 1314MODULE_ALIAS_FS("nilfs2");
783f6184 1315
41c88bd7 1316static void nilfs_inode_init_once(void *obj)
783f6184 1317{
41c88bd7 1318 struct nilfs_inode_info *ii = obj;
783f6184 1319
41c88bd7
LH
1320 INIT_LIST_HEAD(&ii->i_dirty);
1321#ifdef CONFIG_NILFS_XATTR
1322 init_rwsem(&ii->xattr_sem);
1323#endif
41c88bd7
LH
1324 inode_init_once(&ii->vfs_inode);
1325}
783f6184 1326
41c88bd7
LH
1327static void nilfs_segbuf_init_once(void *obj)
1328{
1329 memset(obj, 0, sizeof(struct nilfs_segment_buffer));
1330}
783f6184 1331
41c88bd7
LH
1332static void nilfs_destroy_cachep(void)
1333{
8c0a8537
KS
1334 /*
1335 * Make sure all delayed rcu free inodes are flushed before we
1336 * destroy cache.
1337 */
1338 rcu_barrier();
1339
da80a39f
JL
1340 kmem_cache_destroy(nilfs_inode_cachep);
1341 kmem_cache_destroy(nilfs_transaction_cachep);
1342 kmem_cache_destroy(nilfs_segbuf_cachep);
1343 kmem_cache_destroy(nilfs_btree_path_cache);
41c88bd7 1344}
783f6184 1345
41c88bd7
LH
1346static int __init nilfs_init_cachep(void)
1347{
1348 nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
1349 sizeof(struct nilfs_inode_info), 0,
5d097056
VD
1350 SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
1351 nilfs_inode_init_once);
41c88bd7
LH
1352 if (!nilfs_inode_cachep)
1353 goto fail;
1354
1355 nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
1356 sizeof(struct nilfs_transaction_info), 0,
1357 SLAB_RECLAIM_ACCOUNT, NULL);
1358 if (!nilfs_transaction_cachep)
1359 goto fail;
1360
1361 nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
1362 sizeof(struct nilfs_segment_buffer), 0,
1363 SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
1364 if (!nilfs_segbuf_cachep)
1365 goto fail;
1366
1367 nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
1368 sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
1369 0, 0, NULL);
1370 if (!nilfs_btree_path_cache)
1371 goto fail;
783f6184
RK
1372
1373 return 0;
1374
41c88bd7
LH
1375fail:
1376 nilfs_destroy_cachep();
1377 return -ENOMEM;
1378}
1379
1380static int __init init_nilfs_fs(void)
1381{
1382 int err;
783f6184 1383
41c88bd7
LH
1384 err = nilfs_init_cachep();
1385 if (err)
1386 goto fail;
783f6184 1387
dd70edbd 1388 err = nilfs_sysfs_init();
41c88bd7
LH
1389 if (err)
1390 goto free_cachep;
783f6184 1391
dd70edbd
VD
1392 err = register_filesystem(&nilfs_fs_type);
1393 if (err)
1394 goto deinit_sysfs_entry;
1395
9f130263 1396 printk(KERN_INFO "NILFS version 2 loaded\n");
41c88bd7 1397 return 0;
783f6184 1398
dd70edbd
VD
1399deinit_sysfs_entry:
1400 nilfs_sysfs_exit();
41c88bd7
LH
1401free_cachep:
1402 nilfs_destroy_cachep();
1403fail:
783f6184
RK
1404 return err;
1405}
1406
1407static void __exit exit_nilfs_fs(void)
1408{
41c88bd7 1409 nilfs_destroy_cachep();
dd70edbd 1410 nilfs_sysfs_exit();
783f6184
RK
1411 unregister_filesystem(&nilfs_fs_type);
1412}
1413
1414module_init(init_nilfs_fs)
1415module_exit(exit_nilfs_fs)