| 1 | |
| 2 | |
| 3 | * NILFS module and super block management.
|
| 4 | *
|
| 5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
|
| 6 | *
|
| 7 | * Written by Ryusuke Konishi.
|
| 8 |
|
| 9 | |
| 10 | * linux/fs/ext2/super.c
|
| 11 | *
|
| 12 | * Copyright (C) 1992, 1993, 1994, 1995
|
| 13 | * Remy Card ([email protected])
|
| 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 ([email protected]), 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>
|
| 32 | #include <linux/crc32.h>
|
| 33 | #include <linux/vfs.h>
|
| 34 | #include <linux/writeback.h>
|
| 35 | #include <linux/seq_file.h>
|
| 36 | #include <linux/mount.h>
|
| 37 | #include <linux/fs_context.h>
|
| 38 | #include <linux/fs_parser.h>
|
| 39 | #include "nilfs.h"
|
| 40 | #include "export.h"
|
| 41 | #include "mdt.h"
|
| 42 | #include "alloc.h"
|
| 43 | #include "btree.h"
|
| 44 | #include "btnode.h"
|
| 45 | #include "page.h"
|
| 46 | #include "cpfile.h"
|
| 47 | #include "sufile.h"
|
| 48 | #include "ifile.h"
|
| 49 | #include "dat.h"
|
| 50 | #include "segment.h"
|
| 51 | #include "segbuf.h"
|
| 52 |
|
| 53 | MODULE_AUTHOR("NTT Corp.");
|
| 54 | MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
|
| 55 | "(NILFS)");
|
| 56 | MODULE_LICENSE("GPL");
|
| 57 |
|
| 58 | static struct kmem_cache *nilfs_inode_cachep;
|
| 59 | struct kmem_cache *nilfs_transaction_cachep;
|
| 60 | struct kmem_cache *nilfs_segbuf_cachep;
|
| 61 | struct kmem_cache *nilfs_btree_path_cache;
|
| 62 |
|
| 63 | static int nilfs_setup_super(struct super_block *sb, int is_mount);
|
| 64 |
|
| 65 | void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
|
| 66 | {
|
| 67 | struct va_format vaf;
|
| 68 | va_list args;
|
| 69 | int level;
|
| 70 |
|
| 71 | va_start(args, fmt);
|
| 72 |
|
| 73 | level = printk_get_level(fmt);
|
| 74 | vaf.fmt = printk_skip_level(fmt);
|
| 75 | vaf.va = &args;
|
| 76 |
|
| 77 | if (sb)
|
| 78 | printk("%c%cNILFS (%s): %pV\n",
|
| 79 | KERN_SOH_ASCII, level, sb->s_id, &vaf);
|
| 80 | else
|
| 81 | printk("%c%cNILFS: %pV\n",
|
| 82 | KERN_SOH_ASCII, level, &vaf);
|
| 83 |
|
| 84 | va_end(args);
|
| 85 | }
|
| 86 |
|
| 87 | static void nilfs_set_error(struct super_block *sb)
|
| 88 | {
|
| 89 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 90 | struct nilfs_super_block **sbp;
|
| 91 |
|
| 92 | down_write(&nilfs->ns_sem);
|
| 93 | if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
|
| 94 | nilfs->ns_mount_state |= NILFS_ERROR_FS;
|
| 95 | sbp = nilfs_prepare_super(sb, 0);
|
| 96 | if (likely(sbp)) {
|
| 97 | sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
|
| 98 | if (sbp[1])
|
| 99 | sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
|
| 100 | nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
|
| 101 | }
|
| 102 | }
|
| 103 | up_write(&nilfs->ns_sem);
|
| 104 | }
|
| 105 |
|
| 106 | |
| 107 | * __nilfs_error() - report failure condition on a filesystem
|
| 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
|
| 112 | *
|
| 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.
|
| 116 | *
|
| 117 | * This implements the body of nilfs_error() macro. Normally,
|
| 118 | * nilfs_error() should be used. As for sustainable errors such as a
|
| 119 | * single-shot I/O error, nilfs_err() should be used instead.
|
| 120 | *
|
| 121 | * Callers should not add a trailing newline since this will do it.
|
| 122 |
|
| 123 | void __nilfs_error(struct super_block *sb, const char *function,
|
| 124 | const char *fmt, ...)
|
| 125 | {
|
| 126 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 127 | struct va_format vaf;
|
| 128 | va_list args;
|
| 129 |
|
| 130 | va_start(args, fmt);
|
| 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 |
|
| 138 | va_end(args);
|
| 139 |
|
| 140 | if (!sb_rdonly(sb)) {
|
| 141 | nilfs_set_error(sb);
|
| 142 |
|
| 143 | if (nilfs_test_opt(nilfs, ERRORS_RO)) {
|
| 144 | printk(KERN_CRIT "Remounting filesystem read-only\n");
|
| 145 | sb->s_flags |= SB_RDONLY;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | if (nilfs_test_opt(nilfs, ERRORS_PANIC))
|
| 150 | panic("NILFS (device %s): panic forced after error\n",
|
| 151 | sb->s_id);
|
| 152 | }
|
| 153 |
|
| 154 | struct inode *nilfs_alloc_inode(struct super_block *sb)
|
| 155 | {
|
| 156 | struct nilfs_inode_info *ii;
|
| 157 |
|
| 158 | ii = alloc_inode_sb(sb, nilfs_inode_cachep, GFP_NOFS);
|
| 159 | if (!ii)
|
| 160 | return NULL;
|
| 161 | ii->i_bh = NULL;
|
| 162 | ii->i_state = 0;
|
| 163 | ii->i_type = 0;
|
| 164 | ii->i_cno = 0;
|
| 165 | ii->i_assoc_inode = NULL;
|
| 166 | ii->i_bmap = &ii->i_bmap_data;
|
| 167 | return &ii->vfs_inode;
|
| 168 | }
|
| 169 |
|
| 170 | static void nilfs_free_inode(struct inode *inode)
|
| 171 | {
|
| 172 | if (nilfs_is_metadata_file_inode(inode))
|
| 173 | nilfs_mdt_destroy(inode);
|
| 174 |
|
| 175 | kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
|
| 176 | }
|
| 177 |
|
| 178 | static int nilfs_sync_super(struct super_block *sb, int flag)
|
| 179 | {
|
| 180 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 181 | int err;
|
| 182 |
|
| 183 | retry:
|
| 184 | set_buffer_dirty(nilfs->ns_sbh[0]);
|
| 185 | if (nilfs_test_opt(nilfs, BARRIER)) {
|
| 186 | err = __sync_dirty_buffer(nilfs->ns_sbh[0],
|
| 187 | REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
|
| 188 | } else {
|
| 189 | err = sync_dirty_buffer(nilfs->ns_sbh[0]);
|
| 190 | }
|
| 191 |
|
| 192 | if (unlikely(err)) {
|
| 193 | nilfs_err(sb, "unable to write superblock: err=%d", err);
|
| 194 | if (err == -EIO && nilfs->ns_sbh[1]) {
|
| 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);
|
| 201 | nilfs_fall_back_super_block(nilfs);
|
| 202 | goto retry;
|
| 203 | }
|
| 204 | } else {
|
| 205 | struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
|
| 206 |
|
| 207 | nilfs->ns_sbwcount++;
|
| 208 |
|
| 209 | |
| 210 | * The latest segment becomes trailable from the position
|
| 211 | * written in superblock.
|
| 212 |
|
| 213 | clear_nilfs_discontinued(nilfs);
|
| 214 |
|
| 215 |
|
| 216 | if (nilfs->ns_sbh[1]) {
|
| 217 | if (flag == NILFS_SB_COMMIT_ALL) {
|
| 218 | set_buffer_dirty(nilfs->ns_sbh[1]);
|
| 219 | if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
|
| 220 | goto out;
|
| 221 | }
|
| 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];
|
| 225 | }
|
| 226 |
|
| 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:
|
| 232 | return err;
|
| 233 | }
|
| 234 |
|
| 235 | void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
|
| 236 | struct the_nilfs *nilfs)
|
| 237 | {
|
| 238 | sector_t nfreeblocks;
|
| 239 |
|
| 240 |
|
| 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 |
|
| 251 | struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
|
| 252 | int flip)
|
| 253 | {
|
| 254 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 255 | struct nilfs_super_block **sbp = nilfs->ns_sbp;
|
| 256 |
|
| 257 |
|
| 258 | if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
|
| 259 | if (sbp[1] &&
|
| 260 | sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
|
| 261 | memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
|
| 262 | } else {
|
| 263 | nilfs_crit(sb, "superblock broke");
|
| 264 | return NULL;
|
| 265 | }
|
| 266 | } else if (sbp[1] &&
|
| 267 | sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
|
| 268 | memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
|
| 269 | }
|
| 270 |
|
| 271 | if (flip && sbp[1])
|
| 272 | nilfs_swap_super_block(nilfs);
|
| 273 |
|
| 274 | return sbp;
|
| 275 | }
|
| 276 |
|
| 277 | int nilfs_commit_super(struct super_block *sb, int flag)
|
| 278 | {
|
| 279 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 280 | struct nilfs_super_block **sbp = nilfs->ns_sbp;
|
| 281 | time64_t t;
|
| 282 |
|
| 283 |
|
| 284 | t = ktime_get_real_seconds();
|
| 285 | nilfs->ns_sbwtime = t;
|
| 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));
|
| 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));
|
| 297 | }
|
| 298 | clear_nilfs_sb_dirty(nilfs);
|
| 299 | nilfs->ns_flushed_device = 1;
|
| 300 |
|
| 301 | smp_wmb();
|
| 302 | return nilfs_sync_super(sb, flag);
|
| 303 | }
|
| 304 |
|
| 305 | |
| 306 | * nilfs_cleanup_super() - write filesystem state for cleanup
|
| 307 | * @sb: super block instance to be unmounted or degraded to read-only
|
| 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.
|
| 312 | *
|
| 313 | * Return: 0 on success, %-EIO if I/O error or superblock is corrupted.
|
| 314 |
|
| 315 | int nilfs_cleanup_super(struct super_block *sb)
|
| 316 | {
|
| 317 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 318 | struct nilfs_super_block **sbp;
|
| 319 | int flag = NILFS_SB_COMMIT;
|
| 320 | int ret = -EIO;
|
| 321 |
|
| 322 | sbp = nilfs_prepare_super(sb, 0);
|
| 323 | if (sbp) {
|
| 324 | sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
|
| 325 | nilfs_set_log_cursor(sbp[0], nilfs);
|
| 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 | }
|
| 335 | ret = nilfs_commit_super(sb, flag);
|
| 336 | }
|
| 337 | return ret;
|
| 338 | }
|
| 339 |
|
| 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)
|
| 344 | *
|
| 345 | * Return: 0 on success, or a negative error code on failure.
|
| 346 |
|
| 347 | static 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;
|
| 354 | int sb2i;
|
| 355 | int ret = 0;
|
| 356 |
|
| 357 |
|
| 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;
|
| 365 | } else {
|
| 366 | sb2i = -1;
|
| 367 | blocknr = 0;
|
| 368 | }
|
| 369 | if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
|
| 370 | goto out;
|
| 371 |
|
| 372 |
|
| 373 | newblocknr = sb2off >> nilfs->ns_blocksize_bits;
|
| 374 | offset = sb2off & (nilfs->ns_blocksize - 1);
|
| 375 | nsbh = sb_getblk(sb, newblocknr);
|
| 376 | if (!nsbh) {
|
| 377 | nilfs_warn(sb,
|
| 378 | "unable to move secondary superblock to block %llu",
|
| 379 | (unsigned long long)newblocknr);
|
| 380 | ret = -EIO;
|
| 381 | goto out;
|
| 382 | }
|
| 383 | nsbp = (void *)nsbh->b_data + offset;
|
| 384 |
|
| 385 | lock_buffer(nsbh);
|
| 386 | if (sb2i >= 0) {
|
| 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 |
|
| 393 | memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
|
| 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) {
|
| 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 |
|
| 414 | nilfs->ns_sbh[1] = nsbh;
|
| 415 | nilfs->ns_sbp[1] = nsbp;
|
| 416 | } else {
|
| 417 | brelse(nsbh);
|
| 418 | }
|
| 419 | out:
|
| 420 | return ret;
|
| 421 | }
|
| 422 |
|
| 423 | |
| 424 | * nilfs_resize_fs - resize the filesystem
|
| 425 | * @sb: super block instance
|
| 426 | * @newsize: new size of the filesystem (in bytes)
|
| 427 | *
|
| 428 | * Return: 0 on success, or a negative error code on failure.
|
| 429 |
|
| 430 | int 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;
|
| 439 | devsize = bdev_nr_bytes(sb->s_bdev);
|
| 440 | if (newsize > devsize)
|
| 441 | goto out;
|
| 442 |
|
| 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 |
|
| 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;
|
| 461 | newnsegs = div64_ul(newnsegs, nilfs->ns_blocks_per_segment);
|
| 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);
|
| 501 | out:
|
| 502 | return ret;
|
| 503 | }
|
| 504 |
|
| 505 | static void nilfs_put_super(struct super_block *sb)
|
| 506 | {
|
| 507 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 508 |
|
| 509 | nilfs_detach_log_writer(sb);
|
| 510 |
|
| 511 | if (!sb_rdonly(sb)) {
|
| 512 | down_write(&nilfs->ns_sem);
|
| 513 | nilfs_cleanup_super(sb);
|
| 514 | up_write(&nilfs->ns_sem);
|
| 515 | }
|
| 516 |
|
| 517 | nilfs_sysfs_delete_device_group(nilfs);
|
| 518 | iput(nilfs->ns_sufile);
|
| 519 | iput(nilfs->ns_cpfile);
|
| 520 | iput(nilfs->ns_dat);
|
| 521 |
|
| 522 | destroy_nilfs(nilfs);
|
| 523 | sb->s_fs_info = NULL;
|
| 524 | }
|
| 525 |
|
| 526 | static int nilfs_sync_fs(struct super_block *sb, int wait)
|
| 527 | {
|
| 528 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 529 | struct nilfs_super_block **sbp;
|
| 530 | int err = 0;
|
| 531 |
|
| 532 |
|
| 533 | if (wait)
|
| 534 | err = nilfs_construct_segment(sb);
|
| 535 |
|
| 536 | down_write(&nilfs->ns_sem);
|
| 537 | if (nilfs_sb_dirty(nilfs)) {
|
| 538 | sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
|
| 539 | if (likely(sbp)) {
|
| 540 | nilfs_set_log_cursor(sbp[0], nilfs);
|
| 541 | nilfs_commit_super(sb, NILFS_SB_COMMIT);
|
| 542 | }
|
| 543 | }
|
| 544 | up_write(&nilfs->ns_sem);
|
| 545 |
|
| 546 | if (!err)
|
| 547 | err = nilfs_flush_device(nilfs);
|
| 548 |
|
| 549 | return err;
|
| 550 | }
|
| 551 |
|
| 552 | int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
|
| 553 | struct nilfs_root **rootp)
|
| 554 | {
|
| 555 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 556 | struct nilfs_root *root;
|
| 557 | int err = -ENOMEM;
|
| 558 |
|
| 559 | root = nilfs_find_or_create_root(
|
| 560 | nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
|
| 561 | if (!root)
|
| 562 | return err;
|
| 563 |
|
| 564 | if (root->ifile)
|
| 565 | goto reuse;
|
| 566 |
|
| 567 | down_read(&nilfs->ns_segctor_sem);
|
| 568 | err = nilfs_ifile_read(sb, root, cno, nilfs->ns_inode_size);
|
| 569 | up_read(&nilfs->ns_segctor_sem);
|
| 570 | if (unlikely(err))
|
| 571 | goto failed;
|
| 572 |
|
| 573 | reuse:
|
| 574 | *rootp = root;
|
| 575 | return 0;
|
| 576 |
|
| 577 | failed:
|
| 578 | if (err == -EINVAL)
|
| 579 | nilfs_err(sb, "Invalid checkpoint (checkpoint number=%llu)",
|
| 580 | (unsigned long long)cno);
|
| 581 | nilfs_put_root(root);
|
| 582 |
|
| 583 | return err;
|
| 584 | }
|
| 585 |
|
| 586 | static int nilfs_freeze(struct super_block *sb)
|
| 587 | {
|
| 588 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 589 | int err;
|
| 590 |
|
| 591 | if (sb_rdonly(sb))
|
| 592 | return 0;
|
| 593 |
|
| 594 |
|
| 595 | down_write(&nilfs->ns_sem);
|
| 596 | err = nilfs_cleanup_super(sb);
|
| 597 | up_write(&nilfs->ns_sem);
|
| 598 | return err;
|
| 599 | }
|
| 600 |
|
| 601 | static int nilfs_unfreeze(struct super_block *sb)
|
| 602 | {
|
| 603 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 604 |
|
| 605 | if (sb_rdonly(sb))
|
| 606 | return 0;
|
| 607 |
|
| 608 | down_write(&nilfs->ns_sem);
|
| 609 | nilfs_setup_super(sb, false);
|
| 610 | up_write(&nilfs->ns_sem);
|
| 611 | return 0;
|
| 612 | }
|
| 613 |
|
| 614 | static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
|
| 615 | {
|
| 616 | struct super_block *sb = dentry->d_sb;
|
| 617 | struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
|
| 618 | struct the_nilfs *nilfs = root->nilfs;
|
| 619 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
|
| 620 | unsigned long long blocks;
|
| 621 | unsigned long overhead;
|
| 622 | unsigned long nrsvblocks;
|
| 623 | sector_t nfreeblocks;
|
| 624 | u64 nmaxinodes, nfreeinodes;
|
| 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 | *
|
| 640 | * When distributing meta data blocks outside segment structure,
|
| 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 |
|
| 649 | err = nilfs_ifile_count_free_inodes(root->ifile,
|
| 650 | &nmaxinodes, &nfreeinodes);
|
| 651 | if (unlikely(err)) {
|
| 652 | nilfs_warn(sb, "failed to count free inodes: err=%d", err);
|
| 653 | if (err == -ERANGE) {
|
| 654 | |
| 655 | * If nilfs_palloc_count_max_entries() returns
|
| 656 | * -ERANGE error code then we simply treat
|
| 657 | * current inodes count as maximum possible and
|
| 658 | * zero as free inodes value.
|
| 659 |
|
| 660 | nmaxinodes = atomic64_read(&root->inodes_count);
|
| 661 | nfreeinodes = 0;
|
| 662 | err = 0;
|
| 663 | } else
|
| 664 | return err;
|
| 665 | }
|
| 666 |
|
| 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;
|
| 673 | buf->f_files = nmaxinodes;
|
| 674 | buf->f_ffree = nfreeinodes;
|
| 675 | buf->f_namelen = NILFS_NAME_LEN;
|
| 676 | buf->f_fsid = u64_to_fsid(id);
|
| 677 |
|
| 678 | return 0;
|
| 679 | }
|
| 680 |
|
| 681 | static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
|
| 682 | {
|
| 683 | struct super_block *sb = dentry->d_sb;
|
| 684 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 685 | struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
|
| 686 |
|
| 687 | if (!nilfs_test_opt(nilfs, BARRIER))
|
| 688 | seq_puts(seq, ",nobarrier");
|
| 689 | if (root->cno != NILFS_CPTREE_CURRENT_CNO)
|
| 690 | seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
|
| 691 | if (nilfs_test_opt(nilfs, ERRORS_PANIC))
|
| 692 | seq_puts(seq, ",errors=panic");
|
| 693 | if (nilfs_test_opt(nilfs, ERRORS_CONT))
|
| 694 | seq_puts(seq, ",errors=continue");
|
| 695 | if (nilfs_test_opt(nilfs, STRICT_ORDER))
|
| 696 | seq_puts(seq, ",order=strict");
|
| 697 | if (nilfs_test_opt(nilfs, NORECOVERY))
|
| 698 | seq_puts(seq, ",norecovery");
|
| 699 | if (nilfs_test_opt(nilfs, DISCARD))
|
| 700 | seq_puts(seq, ",discard");
|
| 701 |
|
| 702 | return 0;
|
| 703 | }
|
| 704 |
|
| 705 | static const struct super_operations nilfs_sops = {
|
| 706 | .alloc_inode = nilfs_alloc_inode,
|
| 707 | .free_inode = nilfs_free_inode,
|
| 708 | .dirty_inode = nilfs_dirty_inode,
|
| 709 | .evict_inode = nilfs_evict_inode,
|
| 710 | .put_super = nilfs_put_super,
|
| 711 | .sync_fs = nilfs_sync_fs,
|
| 712 | .freeze_fs = nilfs_freeze,
|
| 713 | .unfreeze_fs = nilfs_unfreeze,
|
| 714 | .statfs = nilfs_statfs,
|
| 715 | .show_options = nilfs_show_options
|
| 716 | };
|
| 717 |
|
| 718 | enum {
|
| 719 | Opt_err, Opt_barrier, Opt_snapshot, Opt_order, Opt_norecovery,
|
| 720 | Opt_discard,
|
| 721 | };
|
| 722 |
|
| 723 | static 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 | {}
|
| 728 | };
|
| 729 |
|
| 730 | static 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 | };
|
| 739 |
|
| 740 | struct nilfs_fs_context {
|
| 741 | unsigned long ns_mount_opt;
|
| 742 | __u64 cno;
|
| 743 | };
|
| 744 |
|
| 745 | static 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)
|
| 759 | nilfs_clear_opt(nilfs, BARRIER);
|
| 760 | else
|
| 761 | nilfs_set_opt(nilfs, BARRIER);
|
| 762 | break;
|
| 763 | case Opt_order:
|
| 764 | if (strcmp(param->string, "relaxed") == 0)
|
| 765 |
|
| 766 | nilfs_clear_opt(nilfs, STRICT_ORDER);
|
| 767 | else if (strcmp(param->string, "strict") == 0)
|
| 768 |
|
| 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;
|
| 790 | }
|
| 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;
|
| 804 | }
|
| 805 |
|
| 806 | return 0;
|
| 807 | }
|
| 808 |
|
| 809 | static int nilfs_setup_super(struct super_block *sb, int is_mount)
|
| 810 | {
|
| 811 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 812 | struct nilfs_super_block **sbp;
|
| 813 | int max_mnt_count;
|
| 814 | int mnt_count;
|
| 815 |
|
| 816 |
|
| 817 | sbp = nilfs_prepare_super(sb, 0);
|
| 818 | if (!sbp)
|
| 819 | return -EIO;
|
| 820 |
|
| 821 | if (!is_mount)
|
| 822 | goto skip_mount_setup;
|
| 823 |
|
| 824 | max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
|
| 825 | mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
|
| 826 |
|
| 827 | if (nilfs->ns_mount_state & NILFS_ERROR_FS)
|
| 828 | nilfs_warn(sb, "mounting fs with errors");
|
| 829 |
|
| 830 | if (!max_mnt_count)
|
| 831 | sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
|
| 832 |
|
| 833 | sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
|
| 834 | sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
|
| 835 |
|
| 836 | skip_mount_setup:
|
| 837 | sbp[0]->s_state =
|
| 838 | cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
|
| 839 |
|
| 840 | if (sbp[1])
|
| 841 | memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
|
| 842 | return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
|
| 843 | }
|
| 844 |
|
| 845 | struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
|
| 846 | u64 pos, int blocksize,
|
| 847 | struct buffer_head **pbh)
|
| 848 | {
|
| 849 | unsigned long long sb_index = pos;
|
| 850 | unsigned long offset;
|
| 851 |
|
| 852 | offset = do_div(sb_index, blocksize);
|
| 853 | *pbh = sb_bread(sb, sb_index);
|
| 854 | if (!*pbh)
|
| 855 | return NULL;
|
| 856 | return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
|
| 857 | }
|
| 858 |
|
| 859 | int nilfs_store_magic(struct super_block *sb,
|
| 860 | struct nilfs_super_block *sbp)
|
| 861 | {
|
| 862 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 863 |
|
| 864 | sb->s_magic = le16_to_cpu(sbp->s_magic);
|
| 865 |
|
| 866 |
|
| 867 | #ifdef NILFS_ATIME_DISABLE
|
| 868 | sb->s_flags |= SB_NOATIME;
|
| 869 | #endif
|
| 870 |
|
| 871 | nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
|
| 872 | nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
|
| 873 | nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
|
| 874 | nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
|
| 875 |
|
| 876 | return 0;
|
| 877 | }
|
| 878 |
|
| 879 | int nilfs_check_feature_compatibility(struct super_block *sb,
|
| 880 | struct nilfs_super_block *sbp)
|
| 881 | {
|
| 882 | __u64 features;
|
| 883 |
|
| 884 | features = le64_to_cpu(sbp->s_feature_incompat) &
|
| 885 | ~NILFS_FEATURE_INCOMPAT_SUPP;
|
| 886 | if (features) {
|
| 887 | nilfs_err(sb,
|
| 888 | "couldn't mount because of unsupported optional features (%llx)",
|
| 889 | (unsigned long long)features);
|
| 890 | return -EINVAL;
|
| 891 | }
|
| 892 | features = le64_to_cpu(sbp->s_feature_compat_ro) &
|
| 893 | ~NILFS_FEATURE_COMPAT_RO_SUPP;
|
| 894 | if (!sb_rdonly(sb) && features) {
|
| 895 | nilfs_err(sb,
|
| 896 | "couldn't mount RDWR because of unsupported optional features (%llx)",
|
| 897 | (unsigned long long)features);
|
| 898 | return -EINVAL;
|
| 899 | }
|
| 900 | return 0;
|
| 901 | }
|
| 902 |
|
| 903 | static int nilfs_get_root_dentry(struct super_block *sb,
|
| 904 | struct nilfs_root *root,
|
| 905 | struct dentry **root_dentry)
|
| 906 | {
|
| 907 | struct inode *inode;
|
| 908 | struct dentry *dentry;
|
| 909 | int ret = 0;
|
| 910 |
|
| 911 | inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
|
| 912 | if (IS_ERR(inode)) {
|
| 913 | ret = PTR_ERR(inode);
|
| 914 | nilfs_err(sb, "error %d getting root inode", ret);
|
| 915 | goto out;
|
| 916 | }
|
| 917 | if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
|
| 918 | iput(inode);
|
| 919 | nilfs_err(sb, "corrupt root inode");
|
| 920 | ret = -EINVAL;
|
| 921 | goto out;
|
| 922 | }
|
| 923 |
|
| 924 | if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
|
| 925 | dentry = d_find_alias(inode);
|
| 926 | if (!dentry) {
|
| 927 | dentry = d_make_root(inode);
|
| 928 | if (!dentry) {
|
| 929 | ret = -ENOMEM;
|
| 930 | goto failed_dentry;
|
| 931 | }
|
| 932 | } else {
|
| 933 | iput(inode);
|
| 934 | }
|
| 935 | } else {
|
| 936 | dentry = d_obtain_root(inode);
|
| 937 | if (IS_ERR(dentry)) {
|
| 938 | ret = PTR_ERR(dentry);
|
| 939 | goto failed_dentry;
|
| 940 | }
|
| 941 | }
|
| 942 | *root_dentry = dentry;
|
| 943 | out:
|
| 944 | return ret;
|
| 945 |
|
| 946 | failed_dentry:
|
| 947 | nilfs_err(sb, "error %d getting root dentry", ret);
|
| 948 | goto out;
|
| 949 | }
|
| 950 |
|
| 951 | static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
|
| 952 | struct dentry **root_dentry)
|
| 953 | {
|
| 954 | struct the_nilfs *nilfs = s->s_fs_info;
|
| 955 | struct nilfs_root *root;
|
| 956 | int ret;
|
| 957 |
|
| 958 | mutex_lock(&nilfs->ns_snapshot_mount_mutex);
|
| 959 |
|
| 960 | down_read(&nilfs->ns_segctor_sem);
|
| 961 | ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
|
| 962 | up_read(&nilfs->ns_segctor_sem);
|
| 963 | if (ret < 0) {
|
| 964 | ret = (ret == -ENOENT) ? -EINVAL : ret;
|
| 965 | goto out;
|
| 966 | } else if (!ret) {
|
| 967 | nilfs_err(s,
|
| 968 | "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
|
| 969 | (unsigned long long)cno);
|
| 970 | ret = -EINVAL;
|
| 971 | goto out;
|
| 972 | }
|
| 973 |
|
| 974 | ret = nilfs_attach_checkpoint(s, cno, false, &root);
|
| 975 | if (ret) {
|
| 976 | nilfs_err(s,
|
| 977 | "error %d while loading snapshot (checkpoint number=%llu)",
|
| 978 | ret, (unsigned long long)cno);
|
| 979 | goto out;
|
| 980 | }
|
| 981 | ret = nilfs_get_root_dentry(s, root, root_dentry);
|
| 982 | nilfs_put_root(root);
|
| 983 | out:
|
| 984 | mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
|
| 985 | return ret;
|
| 986 | }
|
| 987 |
|
| 988 | |
| 989 | * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
|
| 990 | * @root_dentry: root dentry of the tree to be shrunk
|
| 991 | *
|
| 992 | * Return: true if the tree was in-use, false otherwise.
|
| 993 |
|
| 994 | static bool nilfs_tree_is_busy(struct dentry *root_dentry)
|
| 995 | {
|
| 996 | shrink_dcache_parent(root_dentry);
|
| 997 | return d_count(root_dentry) > 1;
|
| 998 | }
|
| 999 |
|
| 1000 | int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
|
| 1001 | {
|
| 1002 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 1003 | struct nilfs_root *root;
|
| 1004 | struct inode *inode;
|
| 1005 | struct dentry *dentry;
|
| 1006 | int ret;
|
| 1007 |
|
| 1008 | if (cno > nilfs->ns_cno)
|
| 1009 | return false;
|
| 1010 |
|
| 1011 | if (cno >= nilfs_last_cno(nilfs))
|
| 1012 | return true;
|
| 1013 |
|
| 1014 | ret = false;
|
| 1015 | root = nilfs_lookup_root(nilfs, cno);
|
| 1016 | if (root) {
|
| 1017 | inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
|
| 1018 | if (inode) {
|
| 1019 | dentry = d_find_alias(inode);
|
| 1020 | if (dentry) {
|
| 1021 | ret = nilfs_tree_is_busy(dentry);
|
| 1022 | dput(dentry);
|
| 1023 | }
|
| 1024 | iput(inode);
|
| 1025 | }
|
| 1026 | nilfs_put_root(root);
|
| 1027 | }
|
| 1028 | return ret;
|
| 1029 | }
|
| 1030 |
|
| 1031 | |
| 1032 | * nilfs_fill_super() - initialize a super block instance
|
| 1033 | * @sb: super_block
|
| 1034 | * @fc: filesystem context
|
| 1035 | *
|
| 1036 | * This function is called exclusively by nilfs->ns_mount_mutex.
|
| 1037 | * So, the recovery process is protected from other simultaneous mounts.
|
| 1038 | *
|
| 1039 | * Return: 0 on success, or a negative error code on failure.
|
| 1040 |
|
| 1041 | static int
|
| 1042 | nilfs_fill_super(struct super_block *sb, struct fs_context *fc)
|
| 1043 | {
|
| 1044 | struct the_nilfs *nilfs;
|
| 1045 | struct nilfs_root *fsroot;
|
| 1046 | struct nilfs_fs_context *ctx = fc->fs_private;
|
| 1047 | __u64 cno;
|
| 1048 | int err;
|
| 1049 |
|
| 1050 | nilfs = alloc_nilfs(sb);
|
| 1051 | if (!nilfs)
|
| 1052 | return -ENOMEM;
|
| 1053 |
|
| 1054 | sb->s_fs_info = nilfs;
|
| 1055 |
|
| 1056 | err = init_nilfs(nilfs, sb);
|
| 1057 | if (err)
|
| 1058 | goto failed_nilfs;
|
| 1059 |
|
| 1060 |
|
| 1061 | nilfs->ns_mount_opt = ctx->ns_mount_opt;
|
| 1062 |
|
| 1063 | sb->s_op = &nilfs_sops;
|
| 1064 | sb->s_export_op = &nilfs_export_ops;
|
| 1065 | sb->s_root = NULL;
|
| 1066 | sb->s_time_gran = 1;
|
| 1067 | sb->s_max_links = NILFS_LINK_MAX;
|
| 1068 |
|
| 1069 | err = load_nilfs(nilfs, sb);
|
| 1070 | if (err)
|
| 1071 | goto failed_nilfs;
|
| 1072 |
|
| 1073 | super_set_uuid(sb, nilfs->ns_sbp[0]->s_uuid,
|
| 1074 | sizeof(nilfs->ns_sbp[0]->s_uuid));
|
| 1075 | super_set_sysfs_name_bdev(sb);
|
| 1076 |
|
| 1077 | cno = nilfs_last_cno(nilfs);
|
| 1078 | err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
|
| 1079 | if (err) {
|
| 1080 | nilfs_err(sb,
|
| 1081 | "error %d while loading last checkpoint (checkpoint number=%llu)",
|
| 1082 | err, (unsigned long long)cno);
|
| 1083 | goto failed_unload;
|
| 1084 | }
|
| 1085 |
|
| 1086 | if (!sb_rdonly(sb)) {
|
| 1087 | err = nilfs_attach_log_writer(sb, fsroot);
|
| 1088 | if (err)
|
| 1089 | goto failed_checkpoint;
|
| 1090 | }
|
| 1091 |
|
| 1092 | err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
|
| 1093 | if (err)
|
| 1094 | goto failed_segctor;
|
| 1095 |
|
| 1096 | nilfs_put_root(fsroot);
|
| 1097 |
|
| 1098 | if (!sb_rdonly(sb)) {
|
| 1099 | down_write(&nilfs->ns_sem);
|
| 1100 | nilfs_setup_super(sb, true);
|
| 1101 | up_write(&nilfs->ns_sem);
|
| 1102 | }
|
| 1103 |
|
| 1104 | return 0;
|
| 1105 |
|
| 1106 | failed_segctor:
|
| 1107 | nilfs_detach_log_writer(sb);
|
| 1108 |
|
| 1109 | failed_checkpoint:
|
| 1110 | nilfs_put_root(fsroot);
|
| 1111 |
|
| 1112 | failed_unload:
|
| 1113 | nilfs_sysfs_delete_device_group(nilfs);
|
| 1114 | iput(nilfs->ns_sufile);
|
| 1115 | iput(nilfs->ns_cpfile);
|
| 1116 | iput(nilfs->ns_dat);
|
| 1117 |
|
| 1118 | failed_nilfs:
|
| 1119 | destroy_nilfs(nilfs);
|
| 1120 | return err;
|
| 1121 | }
|
| 1122 |
|
| 1123 | static int nilfs_reconfigure(struct fs_context *fc)
|
| 1124 | {
|
| 1125 | struct nilfs_fs_context *ctx = fc->fs_private;
|
| 1126 | struct super_block *sb = fc->root->d_sb;
|
| 1127 | struct the_nilfs *nilfs = sb->s_fs_info;
|
| 1128 | int err;
|
| 1129 |
|
| 1130 | sync_filesystem(sb);
|
| 1131 |
|
| 1132 | err = -EINVAL;
|
| 1133 |
|
| 1134 | if (!nilfs_valid_fs(nilfs)) {
|
| 1135 | nilfs_warn(sb,
|
| 1136 | "couldn't remount because the filesystem is in an incomplete recovery state");
|
| 1137 | goto ignore_opts;
|
| 1138 | }
|
| 1139 | if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
|
| 1140 | goto out;
|
| 1141 | if (fc->sb_flags & SB_RDONLY) {
|
| 1142 | sb->s_flags |= SB_RDONLY;
|
| 1143 |
|
| 1144 | |
| 1145 | * Remounting a valid RW partition RDONLY, so set
|
| 1146 | * the RDONLY flag and then mark the partition as valid again.
|
| 1147 |
|
| 1148 | down_write(&nilfs->ns_sem);
|
| 1149 | nilfs_cleanup_super(sb);
|
| 1150 | up_write(&nilfs->ns_sem);
|
| 1151 | } else {
|
| 1152 | __u64 features;
|
| 1153 | struct nilfs_root *root;
|
| 1154 |
|
| 1155 | |
| 1156 | * Mounting a RDONLY partition read-write, so reread and
|
| 1157 | * store the current valid flag. (It may have been changed
|
| 1158 | * by fsck since we originally mounted the partition.)
|
| 1159 |
|
| 1160 | down_read(&nilfs->ns_sem);
|
| 1161 | features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
|
| 1162 | ~NILFS_FEATURE_COMPAT_RO_SUPP;
|
| 1163 | up_read(&nilfs->ns_sem);
|
| 1164 | if (features) {
|
| 1165 | nilfs_warn(sb,
|
| 1166 | "couldn't remount RDWR because of unsupported optional features (%llx)",
|
| 1167 | (unsigned long long)features);
|
| 1168 | err = -EROFS;
|
| 1169 | goto ignore_opts;
|
| 1170 | }
|
| 1171 |
|
| 1172 | sb->s_flags &= ~SB_RDONLY;
|
| 1173 |
|
| 1174 | root = NILFS_I(d_inode(sb->s_root))->i_root;
|
| 1175 | err = nilfs_attach_log_writer(sb, root);
|
| 1176 | if (err) {
|
| 1177 | sb->s_flags |= SB_RDONLY;
|
| 1178 | goto ignore_opts;
|
| 1179 | }
|
| 1180 |
|
| 1181 | down_write(&nilfs->ns_sem);
|
| 1182 | nilfs_setup_super(sb, true);
|
| 1183 | up_write(&nilfs->ns_sem);
|
| 1184 | }
|
| 1185 | out:
|
| 1186 | sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
|
| 1187 |
|
| 1188 | nilfs->ns_mount_opt = ctx->ns_mount_opt;
|
| 1189 |
|
| 1190 | return 0;
|
| 1191 |
|
| 1192 | ignore_opts:
|
| 1193 | return err;
|
| 1194 | }
|
| 1195 |
|
| 1196 | static int
|
| 1197 | nilfs_get_tree(struct fs_context *fc)
|
| 1198 | {
|
| 1199 | struct nilfs_fs_context *ctx = fc->fs_private;
|
| 1200 | struct super_block *s;
|
| 1201 | dev_t dev;
|
| 1202 | int err;
|
| 1203 |
|
| 1204 | if (ctx->cno && !(fc->sb_flags & SB_RDONLY)) {
|
| 1205 | nilfs_err(NULL,
|
| 1206 | "invalid option \"cp=%llu\": read-only option is not specified",
|
| 1207 | ctx->cno);
|
| 1208 | return -EINVAL;
|
| 1209 | }
|
| 1210 |
|
| 1211 | err = lookup_bdev(fc->source, &dev);
|
| 1212 | if (err)
|
| 1213 | return err;
|
| 1214 |
|
| 1215 | s = sget_dev(fc, dev);
|
| 1216 | if (IS_ERR(s))
|
| 1217 | return PTR_ERR(s);
|
| 1218 |
|
| 1219 | if (!s->s_root) {
|
| 1220 | err = setup_bdev_super(s, fc->sb_flags, fc);
|
| 1221 | if (!err)
|
| 1222 | err = nilfs_fill_super(s, fc);
|
| 1223 | if (err)
|
| 1224 | goto failed_super;
|
| 1225 |
|
| 1226 | s->s_flags |= SB_ACTIVE;
|
| 1227 | } else if (!ctx->cno) {
|
| 1228 | if (nilfs_tree_is_busy(s->s_root)) {
|
| 1229 | if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) {
|
| 1230 | nilfs_err(s,
|
| 1231 | "the device already has a %s mount.",
|
| 1232 | sb_rdonly(s) ? "read-only" : "read/write");
|
| 1233 | err = -EBUSY;
|
| 1234 | goto failed_super;
|
| 1235 | }
|
| 1236 | } else {
|
| 1237 | |
| 1238 | * Try reconfigure to setup mount states if the current
|
| 1239 | * tree is not mounted and only snapshots use this sb.
|
| 1240 | *
|
| 1241 | * Since nilfs_reconfigure() requires fc->root to be
|
| 1242 | * set, set it first and release it on failure.
|
| 1243 |
|
| 1244 | fc->root = dget(s->s_root);
|
| 1245 | err = nilfs_reconfigure(fc);
|
| 1246 | if (err) {
|
| 1247 | dput(fc->root);
|
| 1248 | fc->root = NULL;
|
| 1249 | goto failed_super;
|
| 1250 | }
|
| 1251 | return 0;
|
| 1252 | }
|
| 1253 | }
|
| 1254 |
|
| 1255 | if (ctx->cno) {
|
| 1256 | struct dentry *root_dentry;
|
| 1257 |
|
| 1258 | err = nilfs_attach_snapshot(s, ctx->cno, &root_dentry);
|
| 1259 | if (err)
|
| 1260 | goto failed_super;
|
| 1261 | fc->root = root_dentry;
|
| 1262 | return 0;
|
| 1263 | }
|
| 1264 |
|
| 1265 | fc->root = dget(s->s_root);
|
| 1266 | return 0;
|
| 1267 |
|
| 1268 | failed_super:
|
| 1269 | deactivate_locked_super(s);
|
| 1270 | return err;
|
| 1271 | }
|
| 1272 |
|
| 1273 | static void nilfs_free_fc(struct fs_context *fc)
|
| 1274 | {
|
| 1275 | kfree(fc->fs_private);
|
| 1276 | }
|
| 1277 |
|
| 1278 | static const struct fs_context_operations nilfs_context_ops = {
|
| 1279 | .parse_param = nilfs_parse_param,
|
| 1280 | .get_tree = nilfs_get_tree,
|
| 1281 | .reconfigure = nilfs_reconfigure,
|
| 1282 | .free = nilfs_free_fc,
|
| 1283 | };
|
| 1284 |
|
| 1285 | static int nilfs_init_fs_context(struct fs_context *fc)
|
| 1286 | {
|
| 1287 | struct nilfs_fs_context *ctx;
|
| 1288 |
|
| 1289 | ctx = kzalloc_obj(*ctx);
|
| 1290 | if (!ctx)
|
| 1291 | return -ENOMEM;
|
| 1292 |
|
| 1293 | ctx->ns_mount_opt = NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
|
| 1294 | fc->fs_private = ctx;
|
| 1295 | fc->ops = &nilfs_context_ops;
|
| 1296 |
|
| 1297 | return 0;
|
| 1298 | }
|
| 1299 |
|
| 1300 | struct file_system_type nilfs_fs_type = {
|
| 1301 | .owner = THIS_MODULE,
|
| 1302 | .name = "nilfs2",
|
| 1303 | .kill_sb = kill_block_super,
|
| 1304 | .fs_flags = FS_REQUIRES_DEV,
|
| 1305 | .init_fs_context = nilfs_init_fs_context,
|
| 1306 | .parameters = nilfs_param_spec,
|
| 1307 | };
|
| 1308 | MODULE_ALIAS_FS("nilfs2");
|
| 1309 |
|
| 1310 | static void nilfs_inode_init_once(void *obj)
|
| 1311 | {
|
| 1312 | struct nilfs_inode_info *ii = obj;
|
| 1313 |
|
| 1314 | INIT_LIST_HEAD(&ii->i_dirty);
|
| 1315 | #ifdef CONFIG_NILFS_XATTR
|
| 1316 | init_rwsem(&ii->xattr_sem);
|
| 1317 | #endif
|
| 1318 | inode_init_once(&ii->vfs_inode);
|
| 1319 | }
|
| 1320 |
|
| 1321 | static void nilfs_segbuf_init_once(void *obj)
|
| 1322 | {
|
| 1323 | memset(obj, 0, sizeof(struct nilfs_segment_buffer));
|
| 1324 | }
|
| 1325 |
|
| 1326 | static void nilfs_destroy_cachep(void)
|
| 1327 | {
|
| 1328 | |
| 1329 | * Make sure all delayed rcu free inodes are flushed before we
|
| 1330 | * destroy cache.
|
| 1331 |
|
| 1332 | rcu_barrier();
|
| 1333 |
|
| 1334 | kmem_cache_destroy(nilfs_inode_cachep);
|
| 1335 | kmem_cache_destroy(nilfs_transaction_cachep);
|
| 1336 | kmem_cache_destroy(nilfs_segbuf_cachep);
|
| 1337 | kmem_cache_destroy(nilfs_btree_path_cache);
|
| 1338 | }
|
| 1339 |
|
| 1340 | static int __init nilfs_init_cachep(void)
|
| 1341 | {
|
| 1342 | nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
|
| 1343 | sizeof(struct nilfs_inode_info), 0,
|
| 1344 | SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
|
| 1345 | nilfs_inode_init_once);
|
| 1346 | if (!nilfs_inode_cachep)
|
| 1347 | goto fail;
|
| 1348 |
|
| 1349 | nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
|
| 1350 | sizeof(struct nilfs_transaction_info), 0,
|
| 1351 | SLAB_RECLAIM_ACCOUNT, NULL);
|
| 1352 | if (!nilfs_transaction_cachep)
|
| 1353 | goto fail;
|
| 1354 |
|
| 1355 | nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
|
| 1356 | sizeof(struct nilfs_segment_buffer), 0,
|
| 1357 | SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
|
| 1358 | if (!nilfs_segbuf_cachep)
|
| 1359 | goto fail;
|
| 1360 |
|
| 1361 | nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
|
| 1362 | sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
|
| 1363 | 0, 0, NULL);
|
| 1364 | if (!nilfs_btree_path_cache)
|
| 1365 | goto fail;
|
| 1366 |
|
| 1367 | return 0;
|
| 1368 |
|
| 1369 | fail:
|
| 1370 | nilfs_destroy_cachep();
|
| 1371 | return -ENOMEM;
|
| 1372 | }
|
| 1373 |
|
| 1374 | static int __init init_nilfs_fs(void)
|
| 1375 | {
|
| 1376 | int err;
|
| 1377 |
|
| 1378 | err = nilfs_init_cachep();
|
| 1379 | if (err)
|
| 1380 | goto fail;
|
| 1381 |
|
| 1382 | err = nilfs_sysfs_init();
|
| 1383 | if (err)
|
| 1384 | goto free_cachep;
|
| 1385 |
|
| 1386 | err = register_filesystem(&nilfs_fs_type);
|
| 1387 | if (err)
|
| 1388 | goto deinit_sysfs_entry;
|
| 1389 |
|
| 1390 | printk(KERN_INFO "NILFS version 2 loaded\n");
|
| 1391 | return 0;
|
| 1392 |
|
| 1393 | deinit_sysfs_entry:
|
| 1394 | nilfs_sysfs_exit();
|
| 1395 | free_cachep:
|
| 1396 | nilfs_destroy_cachep();
|
| 1397 | fail:
|
| 1398 | return err;
|
| 1399 | }
|
| 1400 |
|
| 1401 | static void __exit exit_nilfs_fs(void)
|
| 1402 | {
|
| 1403 | nilfs_destroy_cachep();
|
| 1404 | nilfs_sysfs_exit();
|
| 1405 | unregister_filesystem(&nilfs_fs_type);
|
| 1406 | }
|
| 1407 |
|
| 1408 | module_init(init_nilfs_fs)
|
| 1409 | module_exit(exit_nilfs_fs)
|