Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 16 | * Written by Ryusuke Konishi. |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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> |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 42 | #include <linux/crc32.h> |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 43 | #include <linux/vfs.h> |
| 44 | #include <linux/writeback.h> |
Jiro SEKIBA | b58a285 | 2009-06-24 20:06:34 +0900 | [diff] [blame] | 45 | #include <linux/seq_file.h> |
| 46 | #include <linux/mount.h> |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 47 | #include "nilfs.h" |
Ryusuke Konishi | 8e656fd | 2010-08-27 00:23:02 +0900 | [diff] [blame] | 48 | #include "export.h" |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 49 | #include "mdt.h" |
| 50 | #include "alloc.h" |
Ryusuke Konishi | 05d0e94 | 2010-07-10 20:52:09 +0900 | [diff] [blame] | 51 | #include "btree.h" |
| 52 | #include "btnode.h" |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 53 | #include "page.h" |
| 54 | #include "cpfile.h" |
Ryusuke Konishi | 4e33f9e | 2011-05-05 01:23:58 +0900 | [diff] [blame] | 55 | #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */ |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 56 | #include "ifile.h" |
| 57 | #include "dat.h" |
| 58 | #include "segment.h" |
| 59 | #include "segbuf.h" |
| 60 | |
| 61 | MODULE_AUTHOR("NTT Corp."); |
| 62 | MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem " |
| 63 | "(NILFS)"); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 64 | MODULE_LICENSE("GPL"); |
| 65 | |
Jiro SEKIBA | abc0b50 | 2010-10-08 22:37:27 +0900 | [diff] [blame] | 66 | static struct kmem_cache *nilfs_inode_cachep; |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 67 | struct kmem_cache *nilfs_transaction_cachep; |
| 68 | struct kmem_cache *nilfs_segbuf_cachep; |
| 69 | struct kmem_cache *nilfs_btree_path_cache; |
| 70 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 71 | static int nilfs_setup_super(struct super_block *sb, int is_mount); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 72 | static int nilfs_remount(struct super_block *sb, int *flags, char *data); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 73 | |
Ryusuke Konishi | a66dfb0 | 2016-08-02 14:05:02 -0700 | [diff] [blame^] | 74 | void __nilfs_msg(struct super_block *sb, const char *level, const char *fmt, |
| 75 | ...) |
| 76 | { |
| 77 | struct va_format vaf; |
| 78 | va_list args; |
| 79 | |
| 80 | va_start(args, fmt); |
| 81 | vaf.fmt = fmt; |
| 82 | vaf.va = &args; |
| 83 | if (sb) |
| 84 | printk("%sNILFS (%s): %pV\n", level, sb->s_id, &vaf); |
| 85 | else |
| 86 | printk("%sNILFS: %pV\n", level, &vaf); |
| 87 | va_end(args); |
| 88 | } |
| 89 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 90 | static void nilfs_set_error(struct super_block *sb) |
Ryusuke Konishi | c8a11c8 | 2010-06-28 17:49:30 +0900 | [diff] [blame] | 91 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 92 | struct the_nilfs *nilfs = sb->s_fs_info; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 93 | struct nilfs_super_block **sbp; |
Ryusuke Konishi | c8a11c8 | 2010-06-28 17:49:30 +0900 | [diff] [blame] | 94 | |
| 95 | down_write(&nilfs->ns_sem); |
| 96 | if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { |
| 97 | nilfs->ns_mount_state |= NILFS_ERROR_FS; |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 98 | sbp = nilfs_prepare_super(sb, 0); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 99 | if (likely(sbp)) { |
| 100 | sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 101 | if (sbp[1]) |
| 102 | sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 103 | nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 104 | } |
Ryusuke Konishi | c8a11c8 | 2010-06-28 17:49:30 +0900 | [diff] [blame] | 105 | } |
| 106 | up_write(&nilfs->ns_sem); |
| 107 | } |
| 108 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 109 | /** |
Ryusuke Konishi | cae3d4c | 2016-08-02 14:05:00 -0700 | [diff] [blame] | 110 | * __nilfs_error() - report failure condition on a filesystem |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 111 | * |
Ryusuke Konishi | cae3d4c | 2016-08-02 14:05:00 -0700 | [diff] [blame] | 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. |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 115 | * |
Ryusuke Konishi | cae3d4c | 2016-08-02 14:05:00 -0700 | [diff] [blame] | 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. |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 122 | */ |
Ryusuke Konishi | cae3d4c | 2016-08-02 14:05:00 -0700 | [diff] [blame] | 123 | void __nilfs_error(struct super_block *sb, const char *function, |
| 124 | const char *fmt, ...) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 125 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 126 | struct the_nilfs *nilfs = sb->s_fs_info; |
Joe Perches | b004a5e | 2010-11-09 16:35:21 -0800 | [diff] [blame] | 127 | struct va_format vaf; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 128 | va_list args; |
| 129 | |
| 130 | va_start(args, fmt); |
Joe Perches | b004a5e | 2010-11-09 16:35:21 -0800 | [diff] [blame] | 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 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 138 | va_end(args); |
| 139 | |
| 140 | if (!(sb->s_flags & MS_RDONLY)) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 141 | nilfs_set_error(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 142 | |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 143 | if (nilfs_test_opt(nilfs, ERRORS_RO)) { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 144 | printk(KERN_CRIT "Remounting filesystem read-only\n"); |
| 145 | sb->s_flags |= MS_RDONLY; |
| 146 | } |
| 147 | } |
| 148 | |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 149 | if (nilfs_test_opt(nilfs, ERRORS_PANIC)) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 150 | panic("NILFS (device %s): panic forced after error\n", |
| 151 | sb->s_id); |
| 152 | } |
| 153 | |
| 154 | void nilfs_warning(struct super_block *sb, const char *function, |
| 155 | const char *fmt, ...) |
| 156 | { |
Joe Perches | b004a5e | 2010-11-09 16:35:21 -0800 | [diff] [blame] | 157 | struct va_format vaf; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 158 | va_list args; |
| 159 | |
| 160 | va_start(args, fmt); |
Joe Perches | b004a5e | 2010-11-09 16:35:21 -0800 | [diff] [blame] | 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 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 168 | va_end(args); |
| 169 | } |
| 170 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 171 | |
Ryusuke Konishi | 2879ed6 | 2010-09-05 13:35:53 +0900 | [diff] [blame] | 172 | struct inode *nilfs_alloc_inode(struct super_block *sb) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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; |
Ryusuke Konishi | 0e14a35 | 2010-08-20 21:20:29 +0900 | [diff] [blame] | 181 | ii->i_cno = 0; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 182 | ii->vfs_inode.i_version = 1; |
Christoph Hellwig | b83ae6d | 2015-01-14 10:42:37 +0100 | [diff] [blame] | 183 | nilfs_mapping_init(&ii->i_btnode_cache, &ii->vfs_inode); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 184 | return &ii->vfs_inode; |
| 185 | } |
| 186 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 187 | static void nilfs_i_callback(struct rcu_head *head) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 188 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 189 | struct inode *inode = container_of(head, struct inode, i_rcu); |
Ryusuke Konishi | b91c9a9 | 2010-08-20 23:46:06 +0900 | [diff] [blame] | 190 | |
Ryusuke Konishi | 2d19961 | 2016-05-23 16:23:20 -0700 | [diff] [blame] | 191 | if (nilfs_is_metadata_file_inode(inode)) |
| 192 | nilfs_mdt_destroy(inode); |
| 193 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 194 | kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); |
| 195 | } |
| 196 | |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 197 | void nilfs_destroy_inode(struct inode *inode) |
| 198 | { |
| 199 | call_rcu(&inode->i_rcu, nilfs_i_callback); |
| 200 | } |
| 201 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 202 | static int nilfs_sync_super(struct super_block *sb, int flag) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 203 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 204 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 205 | int err; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 206 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 207 | retry: |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 208 | set_buffer_dirty(nilfs->ns_sbh[0]); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 209 | if (nilfs_test_opt(nilfs, BARRIER)) { |
Christoph Hellwig | 87e9951 | 2010-08-11 17:05:45 +0200 | [diff] [blame] | 210 | err = __sync_dirty_buffer(nilfs->ns_sbh[0], |
Christoph Hellwig | f8c131f | 2010-08-18 05:29:15 -0400 | [diff] [blame] | 211 | WRITE_SYNC | WRITE_FLUSH_FUA); |
Christoph Hellwig | 87e9951 | 2010-08-11 17:05:45 +0200 | [diff] [blame] | 212 | } else { |
| 213 | err = sync_dirty_buffer(nilfs->ns_sbh[0]); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 214 | } |
Christoph Hellwig | 87e9951 | 2010-08-11 17:05:45 +0200 | [diff] [blame] | 215 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 216 | if (unlikely(err)) { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 217 | printk(KERN_ERR |
| 218 | "NILFS: unable to write superblock (err=%d)\n", err); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 219 | if (err == -EIO && nilfs->ns_sbh[1]) { |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 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); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 226 | nilfs_fall_back_super_block(nilfs); |
| 227 | goto retry; |
| 228 | } |
| 229 | } else { |
| 230 | struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; |
| 231 | |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 232 | nilfs->ns_sbwcount++; |
| 233 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 234 | /* |
| 235 | * The latest segment becomes trailable from the position |
| 236 | * written in superblock. |
| 237 | */ |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 238 | clear_nilfs_discontinued(nilfs); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 239 | |
| 240 | /* update GC protection for recent segments */ |
| 241 | if (nilfs->ns_sbh[1]) { |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 242 | if (flag == NILFS_SB_COMMIT_ALL) { |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 243 | set_buffer_dirty(nilfs->ns_sbh[1]); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 244 | if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0) |
| 245 | goto out; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 246 | } |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 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]; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 250 | } |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 251 | |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 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: |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | |
Ryusuke Konishi | 60f46b7 | 2010-06-28 17:49:31 +0900 | [diff] [blame] | 260 | void nilfs_set_log_cursor(struct nilfs_super_block *sbp, |
| 261 | struct the_nilfs *nilfs) |
| 262 | { |
| 263 | sector_t nfreeblocks; |
| 264 | |
| 265 | /* nilfs->ns_sem must be locked by the caller. */ |
| 266 | nilfs_count_free_blocks(nilfs, &nfreeblocks); |
| 267 | sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks); |
| 268 | |
| 269 | spin_lock(&nilfs->ns_last_segment_lock); |
| 270 | sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); |
| 271 | sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); |
| 272 | sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); |
| 273 | spin_unlock(&nilfs->ns_last_segment_lock); |
| 274 | } |
| 275 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 276 | struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb, |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 277 | int flip) |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 278 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 279 | struct the_nilfs *nilfs = sb->s_fs_info; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 280 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 281 | |
| 282 | /* nilfs->ns_sem must be locked by the caller. */ |
| 283 | if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { |
| 284 | if (sbp[1] && |
| 285 | sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) { |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 286 | memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 287 | } else { |
| 288 | printk(KERN_CRIT "NILFS: superblock broke on dev %s\n", |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 289 | sb->s_id); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 292 | } else if (sbp[1] && |
| 293 | sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { |
Ryusuke Konishi | 7592ecd | 2016-05-23 16:23:36 -0700 | [diff] [blame] | 294 | memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 295 | } |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 296 | |
| 297 | if (flip && sbp[1]) |
| 298 | nilfs_swap_super_block(nilfs); |
| 299 | |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 300 | return sbp; |
| 301 | } |
| 302 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 303 | int nilfs_commit_super(struct super_block *sb, int flag) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 304 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 305 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 306 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 307 | time_t t; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 308 | |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 309 | /* nilfs->ns_sem must be locked by the caller. */ |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 310 | t = get_seconds(); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 311 | nilfs->ns_sbwtime = t; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 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)); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 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)); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 323 | } |
Ryusuke Konishi | e605f0a | 2009-12-09 00:57:52 +0900 | [diff] [blame] | 324 | clear_nilfs_sb_dirty(nilfs); |
Andreas Rohner | e2c7617 | 2014-10-13 15:53:20 -0700 | [diff] [blame] | 325 | nilfs->ns_flushed_device = 1; |
| 326 | /* make sure store to ns_flushed_device cannot be reordered */ |
| 327 | smp_wmb(); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 328 | return nilfs_sync_super(sb, flag); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 331 | /** |
| 332 | * nilfs_cleanup_super() - write filesystem state for cleanup |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 333 | * @sb: super block instance to be unmounted or degraded to read-only |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 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 | */ |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 339 | int nilfs_cleanup_super(struct super_block *sb) |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 340 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 341 | struct the_nilfs *nilfs = sb->s_fs_info; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 342 | struct nilfs_super_block **sbp; |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 343 | int flag = NILFS_SB_COMMIT; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 344 | int ret = -EIO; |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 345 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 346 | sbp = nilfs_prepare_super(sb, 0); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 347 | if (sbp) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 348 | sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state); |
| 349 | nilfs_set_log_cursor(sbp[0], nilfs); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 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 | } |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 359 | ret = nilfs_commit_super(sb, flag); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 360 | } |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | |
Ryusuke Konishi | cfb0a4b | 2011-05-05 01:23:57 +0900 | [diff] [blame] | 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 | */ |
| 369 | static 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; |
Ryusuke Konishi | 4f05028 | 2015-11-06 16:32:16 -0800 | [diff] [blame] | 376 | int sb2i; /* array index of the secondary superblock */ |
Ryusuke Konishi | cfb0a4b | 2011-05-05 01:23:57 +0900 | [diff] [blame] | 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; |
Ryusuke Konishi | 4f05028 | 2015-11-06 16:32:16 -0800 | [diff] [blame] | 387 | } else { |
| 388 | sb2i = -1; |
| 389 | blocknr = 0; |
Ryusuke Konishi | cfb0a4b | 2011-05-05 01:23:57 +0900 | [diff] [blame] | 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 | } |
| 420 | out: |
| 421 | return ret; |
| 422 | } |
| 423 | |
Ryusuke Konishi | 4e33f9e | 2011-05-05 01:23:58 +0900 | [diff] [blame] | 424 | /** |
| 425 | * nilfs_resize_fs - resize the filesystem |
| 426 | * @sb: super block instance |
| 427 | * @newsize: new size of the filesystem (in bytes) |
| 428 | */ |
| 429 | int 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); |
| 491 | out: |
| 492 | return ret; |
| 493 | } |
| 494 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 495 | static void nilfs_put_super(struct super_block *sb) |
| 496 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 497 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 498 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 499 | nilfs_detach_log_writer(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 500 | |
| 501 | if (!(sb->s_flags & MS_RDONLY)) { |
| 502 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 503 | nilfs_cleanup_super(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 504 | up_write(&nilfs->ns_sem); |
| 505 | } |
| 506 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 507 | iput(nilfs->ns_sufile); |
| 508 | iput(nilfs->ns_cpfile); |
| 509 | iput(nilfs->ns_dat); |
| 510 | |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 511 | destroy_nilfs(nilfs); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 512 | sb->s_fs_info = NULL; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 515 | static int nilfs_sync_fs(struct super_block *sb, int wait) |
| 516 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 517 | struct the_nilfs *nilfs = sb->s_fs_info; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 518 | struct nilfs_super_block **sbp; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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); |
Jiro SEKIBA | 6233caa | 2009-07-23 01:26:33 +0900 | [diff] [blame] | 524 | |
| 525 | down_write(&nilfs->ns_sem); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 526 | if (nilfs_sb_dirty(nilfs)) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 527 | sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs)); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 528 | if (likely(sbp)) { |
| 529 | nilfs_set_log_cursor(sbp[0], nilfs); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 530 | nilfs_commit_super(sb, NILFS_SB_COMMIT); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 531 | } |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 532 | } |
Jiro SEKIBA | 6233caa | 2009-07-23 01:26:33 +0900 | [diff] [blame] | 533 | up_write(&nilfs->ns_sem); |
| 534 | |
Andreas Rohner | e2c7617 | 2014-10-13 15:53:20 -0700 | [diff] [blame] | 535 | if (!err) |
| 536 | err = nilfs_flush_device(nilfs); |
| 537 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 538 | return err; |
| 539 | } |
| 540 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 541 | int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt, |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 542 | struct nilfs_root **rootp) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 543 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 544 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 545 | struct nilfs_root *root; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 546 | struct nilfs_checkpoint *raw_cp; |
| 547 | struct buffer_head *bh_cp; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 548 | int err = -ENOMEM; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 549 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 550 | root = nilfs_find_or_create_root( |
| 551 | nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno); |
| 552 | if (!root) |
| 553 | return err; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 554 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 555 | if (root->ifile) |
| 556 | goto reuse; /* already attached checkpoint */ |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 557 | |
Zhang Qiang | 1154ecb | 2009-08-18 14:58:24 +0800 | [diff] [blame] | 558 | down_read(&nilfs->ns_segctor_sem); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 559 | err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, |
| 560 | &bh_cp); |
Zhang Qiang | 1154ecb | 2009-08-18 14:58:24 +0800 | [diff] [blame] | 561 | up_read(&nilfs->ns_segctor_sem); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | } |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 572 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 573 | err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size, |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 574 | &raw_cp->cp_ifile_inode, &root->ifile); |
| 575 | if (err) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 576 | goto failed_bh; |
Ryusuke Konishi | b7c0634 | 2010-08-14 14:48:32 +0900 | [diff] [blame] | 577 | |
Vyacheslav Dubeyko | e5f7f84 | 2013-07-03 15:08:06 -0700 | [diff] [blame] | 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)); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 582 | |
| 583 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 584 | |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 585 | reuse: |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 586 | *rootp = root; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 587 | return 0; |
| 588 | |
| 589 | failed_bh: |
| 590 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); |
| 591 | failed: |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 592 | nilfs_put_root(root); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 593 | |
| 594 | return err; |
| 595 | } |
| 596 | |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 597 | static int nilfs_freeze(struct super_block *sb) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 598 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 599 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 600 | int err; |
| 601 | |
| 602 | if (sb->s_flags & MS_RDONLY) |
| 603 | return 0; |
| 604 | |
| 605 | /* Mark super block clean */ |
| 606 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 607 | err = nilfs_cleanup_super(sb); |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 608 | up_write(&nilfs->ns_sem); |
| 609 | return err; |
| 610 | } |
| 611 | |
| 612 | static int nilfs_unfreeze(struct super_block *sb) |
| 613 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 614 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 615 | |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 616 | if (sb->s_flags & MS_RDONLY) |
| 617 | return 0; |
| 618 | |
| 619 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 620 | nilfs_setup_super(sb, false); |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 621 | up_write(&nilfs->ns_sem); |
| 622 | return 0; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 625 | static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 626 | { |
| 627 | struct super_block *sb = dentry->d_sb; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 628 | struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root; |
Ryusuke Konishi | b7c0634 | 2010-08-14 14:48:32 +0900 | [diff] [blame] | 629 | struct the_nilfs *nilfs = root->nilfs; |
Ryusuke Konishi | c306af2 | 2009-03-26 10:16:57 +0900 | [diff] [blame] | 630 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 631 | unsigned long long blocks; |
| 632 | unsigned long overhead; |
| 633 | unsigned long nrsvblocks; |
| 634 | sector_t nfreeblocks; |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 635 | u64 nmaxinodes, nfreeinodes; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | * |
Ryusuke Konishi | 7a65004 | 2010-03-14 03:32:40 +0900 | [diff] [blame] | 651 | * When distributing meta data blocks outside segment structure, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 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 | */ |
Vyacheslav Dubeyko | e5f7f84 | 2013-07-03 15:08:06 -0700 | [diff] [blame] | 673 | nmaxinodes = atomic64_read(&root->inodes_count); |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 674 | nfreeinodes = 0; |
| 675 | err = 0; |
| 676 | } else |
| 677 | return err; |
| 678 | } |
| 679 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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; |
Vyacheslav Dubeyko | c7ef972 | 2013-07-03 15:08:05 -0700 | [diff] [blame] | 686 | buf->f_files = nmaxinodes; |
| 687 | buf->f_ffree = nfreeinodes; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 688 | buf->f_namelen = NILFS_NAME_LEN; |
Ryusuke Konishi | c306af2 | 2009-03-26 10:16:57 +0900 | [diff] [blame] | 689 | buf->f_fsid.val[0] = (u32)id; |
| 690 | buf->f_fsid.val[1] = (u32)(id >> 32); |
| 691 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 695 | static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry) |
Jiro SEKIBA | b58a285 | 2009-06-24 20:06:34 +0900 | [diff] [blame] | 696 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 697 | struct super_block *sb = dentry->d_sb; |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 698 | struct the_nilfs *nilfs = sb->s_fs_info; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 699 | struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root; |
Jiro SEKIBA | b58a285 | 2009-06-24 20:06:34 +0900 | [diff] [blame] | 700 | |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 701 | if (!nilfs_test_opt(nilfs, BARRIER)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 702 | seq_puts(seq, ",nobarrier"); |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 703 | if (root->cno != NILFS_CPTREE_CURRENT_CNO) |
| 704 | seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 705 | if (nilfs_test_opt(nilfs, ERRORS_PANIC)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 706 | seq_puts(seq, ",errors=panic"); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 707 | if (nilfs_test_opt(nilfs, ERRORS_CONT)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 708 | seq_puts(seq, ",errors=continue"); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 709 | if (nilfs_test_opt(nilfs, STRICT_ORDER)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 710 | seq_puts(seq, ",order=strict"); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 711 | if (nilfs_test_opt(nilfs, NORECOVERY)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 712 | seq_puts(seq, ",norecovery"); |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 713 | if (nilfs_test_opt(nilfs, DISCARD)) |
Ryusuke Konishi | c6b4d57 | 2010-07-05 14:40:27 +0900 | [diff] [blame] | 714 | seq_puts(seq, ",discard"); |
Jiro SEKIBA | b58a285 | 2009-06-24 20:06:34 +0900 | [diff] [blame] | 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Alexey Dobriyan | b87221d | 2009-09-21 17:01:09 -0700 | [diff] [blame] | 719 | static const struct super_operations nilfs_sops = { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 720 | .alloc_inode = nilfs_alloc_inode, |
| 721 | .destroy_inode = nilfs_destroy_inode, |
| 722 | .dirty_inode = nilfs_dirty_inode, |
Al Viro | 6fd1e5c | 2010-06-07 11:55:00 -0400 | [diff] [blame] | 723 | .evict_inode = nilfs_evict_inode, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 724 | .put_super = nilfs_put_super, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 725 | .sync_fs = nilfs_sync_fs, |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 726 | .freeze_fs = nilfs_freeze, |
| 727 | .unfreeze_fs = nilfs_unfreeze, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 728 | .statfs = nilfs_statfs, |
| 729 | .remount_fs = nilfs_remount, |
Jiro SEKIBA | b58a285 | 2009-06-24 20:06:34 +0900 | [diff] [blame] | 730 | .show_options = nilfs_show_options |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 731 | }; |
| 732 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 733 | enum { |
| 734 | Opt_err_cont, Opt_err_panic, Opt_err_ro, |
Ryusuke Konishi | 773bc4f | 2010-07-05 13:00:08 +0900 | [diff] [blame] | 735 | Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery, |
Ryusuke Konishi | 802d317 | 2010-07-05 14:27:04 +0900 | [diff] [blame] | 736 | Opt_discard, Opt_nodiscard, Opt_err, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 737 | }; |
| 738 | |
| 739 | static match_table_t tokens = { |
| 740 | {Opt_err_cont, "errors=continue"}, |
| 741 | {Opt_err_panic, "errors=panic"}, |
| 742 | {Opt_err_ro, "errors=remount-ro"}, |
Ryusuke Konishi | 773bc4f | 2010-07-05 13:00:08 +0900 | [diff] [blame] | 743 | {Opt_barrier, "barrier"}, |
Jiro SEKIBA | 91f1953 | 2009-11-12 14:07:26 +0900 | [diff] [blame] | 744 | {Opt_nobarrier, "nobarrier"}, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 745 | {Opt_snapshot, "cp=%u"}, |
| 746 | {Opt_order, "order=%s"}, |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 747 | {Opt_norecovery, "norecovery"}, |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 748 | {Opt_discard, "discard"}, |
Ryusuke Konishi | 802d317 | 2010-07-05 14:27:04 +0900 | [diff] [blame] | 749 | {Opt_nodiscard, "nodiscard"}, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 750 | {Opt_err, NULL} |
| 751 | }; |
| 752 | |
Ryusuke Konishi | 7c01745 | 2010-07-05 20:08:33 +0900 | [diff] [blame] | 753 | static int parse_options(char *options, struct super_block *sb, int is_remount) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 754 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 755 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 756 | char *p; |
| 757 | substring_t args[MAX_OPT_ARGS]; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 758 | |
| 759 | if (!options) |
| 760 | return 1; |
| 761 | |
| 762 | while ((p = strsep(&options, ",")) != NULL) { |
| 763 | int token; |
Ryusuke Konishi | 4ad364c | 2016-05-23 16:23:25 -0700 | [diff] [blame] | 764 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 765 | if (!*p) |
| 766 | continue; |
| 767 | |
| 768 | token = match_token(p, tokens, args); |
| 769 | switch (token) { |
Ryusuke Konishi | 773bc4f | 2010-07-05 13:00:08 +0900 | [diff] [blame] | 770 | case Opt_barrier: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 771 | nilfs_set_opt(nilfs, BARRIER); |
Ryusuke Konishi | 773bc4f | 2010-07-05 13:00:08 +0900 | [diff] [blame] | 772 | break; |
Jiro SEKIBA | 91f1953 | 2009-11-12 14:07:26 +0900 | [diff] [blame] | 773 | case Opt_nobarrier: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 774 | nilfs_clear_opt(nilfs, BARRIER); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 775 | break; |
| 776 | case Opt_order: |
| 777 | if (strcmp(args[0].from, "relaxed") == 0) |
| 778 | /* Ordered data semantics */ |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 779 | nilfs_clear_opt(nilfs, STRICT_ORDER); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 780 | else if (strcmp(args[0].from, "strict") == 0) |
| 781 | /* Strict in-order semantics */ |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 782 | nilfs_set_opt(nilfs, STRICT_ORDER); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 783 | else |
| 784 | return 0; |
| 785 | break; |
| 786 | case Opt_err_panic: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 787 | nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 788 | break; |
| 789 | case Opt_err_ro: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 790 | nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 791 | break; |
| 792 | case Opt_err_cont: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 793 | nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 794 | break; |
| 795 | case Opt_snapshot: |
Ryusuke Konishi | 7c01745 | 2010-07-05 20:08:33 +0900 | [diff] [blame] | 796 | if (is_remount) { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 797 | printk(KERN_ERR |
| 798 | "NILFS: \"%s\" option is invalid " |
| 799 | "for remount.\n", p); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 800 | return 0; |
Ryusuke Konishi | 7c01745 | 2010-07-05 20:08:33 +0900 | [diff] [blame] | 801 | } |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 802 | break; |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 803 | case Opt_norecovery: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 804 | nilfs_set_opt(nilfs, NORECOVERY); |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 805 | break; |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 806 | case Opt_discard: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 807 | nilfs_set_opt(nilfs, DISCARD); |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 808 | break; |
Ryusuke Konishi | 802d317 | 2010-07-05 14:27:04 +0900 | [diff] [blame] | 809 | case Opt_nodiscard: |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 810 | nilfs_clear_opt(nilfs, DISCARD); |
Ryusuke Konishi | 802d317 | 2010-07-05 14:27:04 +0900 | [diff] [blame] | 811 | break; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 812 | default: |
| 813 | printk(KERN_ERR |
| 814 | "NILFS: Unrecognized mount option \"%s\"\n", p); |
| 815 | return 0; |
| 816 | } |
| 817 | } |
| 818 | return 1; |
| 819 | } |
| 820 | |
| 821 | static inline void |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 822 | nilfs_set_default_options(struct super_block *sb, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 823 | struct nilfs_super_block *sbp) |
| 824 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 825 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 826 | |
| 827 | nilfs->ns_mount_opt = |
Ryusuke Konishi | 277a6a3 | 2010-04-02 18:02:33 +0900 | [diff] [blame] | 828 | NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 831 | static int nilfs_setup_super(struct super_block *sb, int is_mount) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 832 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 833 | struct the_nilfs *nilfs = sb->s_fs_info; |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 834 | struct nilfs_super_block **sbp; |
| 835 | int max_mnt_count; |
| 836 | int mnt_count; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 837 | |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 838 | /* nilfs->ns_sem must be locked by the caller. */ |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 839 | sbp = nilfs_prepare_super(sb, 0); |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 840 | if (!sbp) |
| 841 | return -EIO; |
| 842 | |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 843 | if (!is_mount) |
| 844 | goto skip_mount_setup; |
| 845 | |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 846 | max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count); |
| 847 | mnt_count = le16_to_cpu(sbp[0]->s_mnt_count); |
| 848 | |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 849 | if (nilfs->ns_mount_state & NILFS_ERROR_FS) { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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) |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 859 | sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 860 | |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 861 | sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1); |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 862 | sbp[0]->s_mtime = cpu_to_le64(get_seconds()); |
| 863 | |
| 864 | skip_mount_setup: |
Jiro SEKIBA | d26493b | 2010-06-28 17:49:32 +0900 | [diff] [blame] | 865 | sbp[0]->s_state = |
| 866 | cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS); |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 867 | /* synchronize sbp[1] with sbp[0] */ |
Ryusuke Konishi | 0ca7a5b | 2011-01-21 16:40:31 +0900 | [diff] [blame] | 868 | if (sbp[1]) |
| 869 | memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 870 | return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 873 | struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, |
| 874 | u64 pos, int blocksize, |
| 875 | struct buffer_head **pbh) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 876 | { |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 877 | unsigned long long sb_index = pos; |
| 878 | unsigned long offset; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 879 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 880 | offset = do_div(sb_index, blocksize); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 881 | *pbh = sb_bread(sb, sb_index); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 882 | if (!*pbh) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 883 | return NULL; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 884 | return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); |
| 885 | } |
| 886 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 887 | int nilfs_store_magic_and_option(struct super_block *sb, |
| 888 | struct nilfs_super_block *sbp, |
| 889 | char *data) |
| 890 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 891 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 892 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 900 | nilfs_set_default_options(sb, sbp); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 901 | |
Ryusuke Konishi | 574e6c3 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 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); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 906 | |
Ryusuke Konishi | c9cb9b5 | 2016-05-23 16:23:34 -0700 | [diff] [blame] | 907 | return !parse_options(data, sb, 0) ? -EINVAL : 0; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 910 | int 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 | |
Ryusuke Konishi | 367ea33 | 2010-08-26 01:52:51 +0900 | [diff] [blame] | 934 | static 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 | |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 955 | if (root->cno == NILFS_CPTREE_CURRENT_CNO) { |
| 956 | dentry = d_find_alias(inode); |
| 957 | if (!dentry) { |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 958 | dentry = d_make_root(inode); |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 959 | if (!dentry) { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 960 | ret = -ENOMEM; |
| 961 | goto failed_dentry; |
| 962 | } |
| 963 | } else { |
| 964 | iput(inode); |
| 965 | } |
| 966 | } else { |
J. Bruce Fields | 1a0a397 | 2014-02-14 17:35:37 -0500 | [diff] [blame] | 967 | dentry = d_obtain_root(inode); |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 968 | if (IS_ERR(dentry)) { |
| 969 | ret = PTR_ERR(dentry); |
| 970 | goto failed_dentry; |
| 971 | } |
Ryusuke Konishi | 367ea33 | 2010-08-26 01:52:51 +0900 | [diff] [blame] | 972 | } |
| 973 | *root_dentry = dentry; |
| 974 | out: |
| 975 | return ret; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 976 | |
| 977 | failed_dentry: |
| 978 | printk(KERN_ERR "NILFS: get root dentry failed\n"); |
| 979 | goto out; |
Ryusuke Konishi | 367ea33 | 2010-08-26 01:52:51 +0900 | [diff] [blame] | 980 | } |
| 981 | |
Ryusuke Konishi | ab4d8f7 | 2010-08-26 02:15:41 +0900 | [diff] [blame] | 982 | static int nilfs_attach_snapshot(struct super_block *s, __u64 cno, |
| 983 | struct dentry **root_dentry) |
| 984 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 985 | struct the_nilfs *nilfs = s->s_fs_info; |
Ryusuke Konishi | ab4d8f7 | 2010-08-26 02:15:41 +0900 | [diff] [blame] | 986 | struct nilfs_root *root; |
| 987 | int ret; |
| 988 | |
Ryusuke Konishi | 572d8b3 | 2012-07-30 14:42:07 -0700 | [diff] [blame] | 989 | mutex_lock(&nilfs->ns_snapshot_mount_mutex); |
| 990 | |
Ryusuke Konishi | ab4d8f7 | 2010-08-26 02:15:41 +0900 | [diff] [blame] | 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 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1005 | ret = nilfs_attach_checkpoint(s, cno, false, &root); |
Ryusuke Konishi | ab4d8f7 | 2010-08-26 02:15:41 +0900 | [diff] [blame] | 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: |
Ryusuke Konishi | 572d8b3 | 2012-07-30 14:42:07 -0700 | [diff] [blame] | 1015 | mutex_unlock(&nilfs->ns_snapshot_mount_mutex); |
Ryusuke Konishi | ab4d8f7 | 2010-08-26 02:15:41 +0900 | [diff] [blame] | 1016 | return ret; |
| 1017 | } |
| 1018 | |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1019 | /** |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1020 | * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1021 | * @root_dentry: root dentry of the tree to be shrunk |
| 1022 | * |
| 1023 | * This function returns true if the tree was in-use. |
| 1024 | */ |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1025 | static bool nilfs_tree_is_busy(struct dentry *root_dentry) |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1026 | { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1027 | shrink_dcache_parent(root_dentry); |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1028 | return d_count(root_dentry) > 1; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1029 | } |
| 1030 | |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 1031 | int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno) |
| 1032 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1033 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 1034 | struct nilfs_root *root; |
| 1035 | struct inode *inode; |
| 1036 | struct dentry *dentry; |
| 1037 | int ret; |
| 1038 | |
Ryusuke Konishi | 3377f84 | 2015-04-16 12:46:45 -0700 | [diff] [blame] | 1039 | if (cno > nilfs->ns_cno) |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 1040 | return false; |
| 1041 | |
| 1042 | if (cno >= nilfs_last_cno(nilfs)) |
| 1043 | return true; /* protect recent checkpoints */ |
| 1044 | |
| 1045 | ret = false; |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1046 | root = nilfs_lookup_root(nilfs, cno); |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 1047 | if (root) { |
| 1048 | inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO); |
| 1049 | if (inode) { |
| 1050 | dentry = d_find_alias(inode); |
| 1051 | if (dentry) { |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1052 | ret = nilfs_tree_is_busy(dentry); |
Ryusuke Konishi | 032dbb3 | 2010-09-13 11:16:34 +0900 | [diff] [blame] | 1053 | dput(dentry); |
| 1054 | } |
| 1055 | iput(inode); |
| 1056 | } |
| 1057 | nilfs_put_root(root); |
| 1058 | } |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1062 | /** |
| 1063 | * nilfs_fill_super() - initialize a super block instance |
| 1064 | * @sb: super_block |
| 1065 | * @data: mount options |
| 1066 | * @silent: silent mode flag |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1067 | * |
Ryusuke Konishi | aa7dfb8 | 2009-06-08 01:39:33 +0900 | [diff] [blame] | 1068 | * This function is called exclusively by nilfs->ns_mount_mutex. |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1069 | * So, the recovery process is protected from other simultaneous mounts. |
| 1070 | */ |
| 1071 | static int |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1072 | nilfs_fill_super(struct super_block *sb, void *data, int silent) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1073 | { |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1074 | struct the_nilfs *nilfs; |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 1075 | struct nilfs_root *fsroot; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1076 | __u64 cno; |
| 1077 | int err; |
| 1078 | |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1079 | nilfs = alloc_nilfs(sb->s_bdev); |
| 1080 | if (!nilfs) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1081 | return -ENOMEM; |
| 1082 | |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1083 | sb->s_fs_info = nilfs; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1084 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1085 | err = init_nilfs(nilfs, sb, (char *)data); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1086 | if (err) |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1087 | goto failed_nilfs; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1088 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1089 | sb->s_op = &nilfs_sops; |
| 1090 | sb->s_export_op = &nilfs_export_ops; |
| 1091 | sb->s_root = NULL; |
Ryusuke Konishi | 6123923 | 2009-04-06 19:02:00 -0700 | [diff] [blame] | 1092 | sb->s_time_gran = 1; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 1093 | sb->s_max_links = NILFS_LINK_MAX; |
Ryusuke Konishi | 026a7d6 | 2010-10-07 14:19:48 +0900 | [diff] [blame] | 1094 | |
Christoph Hellwig | 26ff130 | 2015-01-14 10:42:35 +0100 | [diff] [blame] | 1095 | sb->s_bdi = &bdev_get_queue(sb->s_bdev)->backing_dev_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1096 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1097 | err = load_nilfs(nilfs, sb); |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 1098 | if (err) |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1099 | goto failed_nilfs; |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 1100 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1101 | cno = nilfs_last_cno(nilfs); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1102 | err = nilfs_attach_checkpoint(sb, cno, true, &fsroot); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1103 | if (err) { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1104 | printk(KERN_ERR "NILFS: error loading last checkpoint " |
| 1105 | "(checkpoint number=%llu).\n", (unsigned long long)cno); |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 1106 | goto failed_unload; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | if (!(sb->s_flags & MS_RDONLY)) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1110 | err = nilfs_attach_log_writer(sb, fsroot); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1111 | if (err) |
| 1112 | goto failed_checkpoint; |
| 1113 | } |
| 1114 | |
Ryusuke Konishi | 367ea33 | 2010-08-26 01:52:51 +0900 | [diff] [blame] | 1115 | err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root); |
| 1116 | if (err) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1117 | goto failed_segctor; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1118 | |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 1119 | nilfs_put_root(fsroot); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (!(sb->s_flags & MS_RDONLY)) { |
| 1122 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1123 | nilfs_setup_super(sb, true); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1124 | up_write(&nilfs->ns_sem); |
| 1125 | } |
| 1126 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1127 | return 0; |
| 1128 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1129 | failed_segctor: |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1130 | nilfs_detach_log_writer(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1131 | |
| 1132 | failed_checkpoint: |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 1133 | nilfs_put_root(fsroot); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1134 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 1135 | failed_unload: |
| 1136 | iput(nilfs->ns_sufile); |
| 1137 | iput(nilfs->ns_cpfile); |
| 1138 | iput(nilfs->ns_dat); |
| 1139 | |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1140 | failed_nilfs: |
| 1141 | destroy_nilfs(nilfs); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1142 | return err; |
| 1143 | } |
| 1144 | |
| 1145 | static int nilfs_remount(struct super_block *sb, int *flags, char *data) |
| 1146 | { |
Ryusuke Konishi | e3154e9 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1147 | struct the_nilfs *nilfs = sb->s_fs_info; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1148 | unsigned long old_sb_flags; |
Ryusuke Konishi | 06df0f9 | 2010-12-27 00:04:06 +0900 | [diff] [blame] | 1149 | unsigned long old_mount_opt; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1150 | int err; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1151 | |
Theodore Ts'o | 02b9984 | 2014-03-13 10:14:33 -0400 | [diff] [blame] | 1152 | sync_filesystem(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1153 | old_sb_flags = sb->s_flags; |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 1154 | old_mount_opt = nilfs->ns_mount_opt; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1155 | |
Ryusuke Konishi | 7c01745 | 2010-07-05 20:08:33 +0900 | [diff] [blame] | 1156 | if (!parse_options(data, sb, 1)) { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1157 | err = -EINVAL; |
| 1158 | goto restore_opts; |
| 1159 | } |
| 1160 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL); |
| 1161 | |
Ryusuke Konishi | d240e06 | 2010-05-09 21:51:53 +0900 | [diff] [blame] | 1162 | err = -EINVAL; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1163 | |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 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); |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 1168 | goto restore_opts; |
| 1169 | } |
| 1170 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1171 | if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) |
| 1172 | goto out; |
| 1173 | if (*flags & MS_RDONLY) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1174 | /* Shutting down log writer */ |
| 1175 | nilfs_detach_log_writer(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1176 | sb->s_flags |= MS_RDONLY; |
| 1177 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1183 | nilfs_cleanup_super(sb); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1184 | up_write(&nilfs->ns_sem); |
| 1185 | } else { |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 1186 | __u64 features; |
Ryusuke Konishi | e912a5b | 2010-08-14 13:07:15 +0900 | [diff] [blame] | 1187 | struct nilfs_root *root; |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 1188 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | */ |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 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 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1207 | sb->s_flags &= ~MS_RDONLY; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1208 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1209 | root = NILFS_I(d_inode(sb->s_root))->i_root; |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1210 | err = nilfs_attach_log_writer(sb, root); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1211 | if (err) |
Ryusuke Konishi | e59399d | 2009-06-08 01:39:32 +0900 | [diff] [blame] | 1212 | goto restore_opts; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1213 | |
| 1214 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 1215 | nilfs_setup_super(sb, true); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1216 | up_write(&nilfs->ns_sem); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1217 | } |
| 1218 | out: |
| 1219 | return 0; |
| 1220 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1221 | restore_opts: |
| 1222 | sb->s_flags = old_sb_flags; |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 1223 | nilfs->ns_mount_opt = old_mount_opt; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1224 | return err; |
| 1225 | } |
| 1226 | |
| 1227 | struct 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 | */ |
| 1238 | static int nilfs_identify(char *data, struct nilfs_super_data *sd) |
| 1239 | { |
| 1240 | char *p, *options = data; |
| 1241 | substring_t args[MAX_OPT_ARGS]; |
Ryusuke Konishi | c05dbfc | 2010-09-16 00:36:24 +0900 | [diff] [blame] | 1242 | int token; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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) { |
Ryusuke Konishi | c05dbfc | 2010-09-16 00:36:24 +0900 | [diff] [blame] | 1250 | if (!(sd->flags & MS_RDONLY)) { |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1251 | ret++; |
Ryusuke Konishi | c05dbfc | 2010-09-16 00:36:24 +0900 | [diff] [blame] | 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++; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 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 | |
| 1276 | static int nilfs_set_bdev_super(struct super_block *s, void *data) |
| 1277 | { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1278 | s->s_bdev = data; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1279 | s->s_dev = s->s_bdev->bd_dev; |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | static int nilfs_test_bdev_super(struct super_block *s, void *data) |
| 1284 | { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1285 | return (void *)s->s_bdev == data; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Al Viro | e4c59d6 | 2010-07-26 12:19:34 +0400 | [diff] [blame] | 1288 | static struct dentry * |
| 1289 | nilfs_mount(struct file_system_type *fs_type, int flags, |
| 1290 | const char *dev_name, void *data) |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1291 | { |
| 1292 | struct nilfs_super_data sd; |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 1293 | struct super_block *s; |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1294 | fmode_t mode = FMODE_READ | FMODE_EXCL; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1295 | struct dentry *root_dentry; |
| 1296 | int err, s_new = false; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1297 | |
Ryusuke Konishi | 13e9055 | 2010-05-09 02:57:57 +0900 | [diff] [blame] | 1298 | if (!(flags & MS_RDONLY)) |
| 1299 | mode |= FMODE_WRITE; |
| 1300 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1301 | sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type); |
Jan Blunck | d6d4c19 | 2010-02-24 13:25:32 +0100 | [diff] [blame] | 1302 | if (IS_ERR(sd.bdev)) |
Al Viro | e4c59d6 | 2010-07-26 12:19:34 +0400 | [diff] [blame] | 1303 | return ERR_CAST(sd.bdev); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1304 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1305 | sd.cno = 0; |
| 1306 | sd.flags = flags; |
| 1307 | if (nilfs_identify((char *)data, &sd)) { |
| 1308 | err = -EINVAL; |
| 1309 | goto failed; |
| 1310 | } |
| 1311 | |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 1312 | /* |
| 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 |
| 1316 | */ |
| 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; |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 1321 | goto failed; |
| 1322 | } |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 1323 | s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags, |
| 1324 | sd.bdev); |
Ryusuke Konishi | 5beb6e0 | 2010-09-20 18:19:06 +0900 | [diff] [blame] | 1325 | mutex_unlock(&sd.bdev->bd_fsfreeze_mutex); |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 1326 | if (IS_ERR(s)) { |
| 1327 | err = PTR_ERR(s); |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1328 | goto failed; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | if (!s->s_root) { |
Ryusuke Konishi | 8fa7c32 | 2016-05-23 16:23:00 -0700 | [diff] [blame] | 1332 | s_new = true; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1333 | |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 1334 | /* New superblock instance created */ |
Ryusuke Konishi | 4571b82 | 2010-05-09 03:01:32 +0900 | [diff] [blame] | 1335 | s->s_mode = mode; |
Dmitry Monakhov | a1c6f057 | 2015-04-13 16:31:37 +0400 | [diff] [blame] | 1336 | snprintf(s->s_id, sizeof(s->s_id), "%pg", sd.bdev); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1337 | sb_set_blocksize(s, block_size(sd.bdev)); |
| 1338 | |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1339 | err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1340 | if (err) |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1341 | goto failed_super; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1342 | |
| 1343 | s->s_flags |= MS_ACTIVE; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1344 | } else if (!sd.cno) { |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1345 | if (nilfs_tree_is_busy(s->s_root)) { |
| 1346 | if ((flags ^ s->s_flags) & MS_RDONLY) { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 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 | } |
Al Viro | e95c311 | 2013-08-24 12:06:16 -0400 | [diff] [blame] | 1354 | } else { |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 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 | } |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1365 | if (sd.cno) { |
| 1366 | err = nilfs_attach_snapshot(s, sd.cno, &root_dentry); |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1367 | if (err) |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1368 | goto failed_super; |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1369 | } else { |
| 1370 | root_dentry = dget(s->s_root); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1373 | if (!s_new) |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1374 | blkdev_put(sd.bdev, mode); |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1375 | |
Al Viro | e4c59d6 | 2010-07-26 12:19:34 +0400 | [diff] [blame] | 1376 | return root_dentry; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1377 | |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 1378 | failed_super: |
Al Viro | a95161a | 2009-08-09 00:52:02 +0400 | [diff] [blame] | 1379 | deactivate_locked_super(s); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1380 | |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 1381 | failed: |
| 1382 | if (!s_new) |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1383 | blkdev_put(sd.bdev, mode); |
Al Viro | e4c59d6 | 2010-07-26 12:19:34 +0400 | [diff] [blame] | 1384 | return ERR_PTR(err); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1387 | struct file_system_type nilfs_fs_type = { |
| 1388 | .owner = THIS_MODULE, |
| 1389 | .name = "nilfs2", |
Al Viro | e4c59d6 | 2010-07-26 12:19:34 +0400 | [diff] [blame] | 1390 | .mount = nilfs_mount, |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1391 | .kill_sb = kill_block_super, |
| 1392 | .fs_flags = FS_REQUIRES_DEV, |
| 1393 | }; |
Eric W. Biederman | 7f78e03 | 2013-03-02 19:39:14 -0800 | [diff] [blame] | 1394 | MODULE_ALIAS_FS("nilfs2"); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1395 | |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1396 | static void nilfs_inode_init_once(void *obj) |
| 1397 | { |
| 1398 | struct nilfs_inode_info *ii = obj; |
| 1399 | |
| 1400 | INIT_LIST_HEAD(&ii->i_dirty); |
| 1401 | #ifdef CONFIG_NILFS_XATTR |
| 1402 | init_rwsem(&ii->xattr_sem); |
| 1403 | #endif |
Miklos Szeredi | 2aa1589 | 2011-02-23 13:49:47 +0100 | [diff] [blame] | 1404 | address_space_init_once(&ii->i_btnode_cache); |
Ryusuke Konishi | 05d0e94 | 2010-07-10 20:52:09 +0900 | [diff] [blame] | 1405 | ii->i_bmap = &ii->i_bmap_data; |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1406 | inode_init_once(&ii->vfs_inode); |
| 1407 | } |
| 1408 | |
| 1409 | static void nilfs_segbuf_init_once(void *obj) |
| 1410 | { |
| 1411 | memset(obj, 0, sizeof(struct nilfs_segment_buffer)); |
| 1412 | } |
| 1413 | |
| 1414 | static void nilfs_destroy_cachep(void) |
| 1415 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 1416 | /* |
| 1417 | * Make sure all delayed rcu free inodes are flushed before we |
| 1418 | * destroy cache. |
| 1419 | */ |
| 1420 | rcu_barrier(); |
| 1421 | |
Julia Lawall | da80a39 | 2015-11-06 16:31:40 -0800 | [diff] [blame] | 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); |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | static int __init nilfs_init_cachep(void) |
| 1429 | { |
| 1430 | nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", |
| 1431 | sizeof(struct nilfs_inode_info), 0, |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 1432 | SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, |
| 1433 | nilfs_inode_init_once); |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 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; |
| 1454 | |
| 1455 | return 0; |
| 1456 | |
| 1457 | fail: |
| 1458 | nilfs_destroy_cachep(); |
| 1459 | return -ENOMEM; |
| 1460 | } |
| 1461 | |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1462 | static int __init init_nilfs_fs(void) |
| 1463 | { |
| 1464 | int err; |
| 1465 | |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1466 | err = nilfs_init_cachep(); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1467 | if (err) |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1468 | goto fail; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1469 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 1470 | err = nilfs_sysfs_init(); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1471 | if (err) |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1472 | goto free_cachep; |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1473 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 1474 | err = register_filesystem(&nilfs_fs_type); |
| 1475 | if (err) |
| 1476 | goto deinit_sysfs_entry; |
| 1477 | |
Li Hong | 9f13026 | 2010-04-09 23:09:53 +0800 | [diff] [blame] | 1478 | printk(KERN_INFO "NILFS version 2 loaded\n"); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1479 | return 0; |
| 1480 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 1481 | deinit_sysfs_entry: |
| 1482 | nilfs_sysfs_exit(); |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1483 | free_cachep: |
| 1484 | nilfs_destroy_cachep(); |
| 1485 | fail: |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1486 | return err; |
| 1487 | } |
| 1488 | |
| 1489 | static void __exit exit_nilfs_fs(void) |
| 1490 | { |
Li Hong | 41c88bd | 2010-04-06 00:54:11 +0800 | [diff] [blame] | 1491 | nilfs_destroy_cachep(); |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 1492 | nilfs_sysfs_exit(); |
Ryusuke Konishi | 783f618 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1493 | unregister_filesystem(&nilfs_fs_type); |
| 1494 | } |
| 1495 | |
| 1496 | module_init(init_nilfs_fs) |
| 1497 | module_exit(exit_nilfs_fs) |