Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * the_nilfs.c - the_nilfs shared structure. |
| 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 | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/buffer_head.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/blkdev.h> |
| 23 | #include <linux/backing-dev.h> |
Ryusuke Konishi | 9b1fc4e4 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 24 | #include <linux/random.h> |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 25 | #include <linux/crc32.h> |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 26 | #include "nilfs.h" |
| 27 | #include "segment.h" |
| 28 | #include "alloc.h" |
| 29 | #include "cpfile.h" |
| 30 | #include "sufile.h" |
| 31 | #include "dat.h" |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 32 | #include "segbuf.h" |
| 33 | |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 34 | |
Ryusuke Konishi | 6c125160 | 2010-06-28 19:15:26 +0900 | [diff] [blame] | 35 | static int nilfs_valid_sb(struct nilfs_super_block *sbp); |
| 36 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 37 | void nilfs_set_last_segment(struct the_nilfs *nilfs, |
| 38 | sector_t start_blocknr, u64 seq, __u64 cno) |
| 39 | { |
| 40 | spin_lock(&nilfs->ns_last_segment_lock); |
| 41 | nilfs->ns_last_pseg = start_blocknr; |
| 42 | nilfs->ns_last_seq = seq; |
| 43 | nilfs->ns_last_cno = cno; |
Ryusuke Konishi | 3250204 | 2010-06-29 14:42:13 +0900 | [diff] [blame] | 44 | |
| 45 | if (!nilfs_sb_dirty(nilfs)) { |
| 46 | if (nilfs->ns_prev_seq == nilfs->ns_last_seq) |
| 47 | goto stay_cursor; |
| 48 | |
| 49 | set_nilfs_sb_dirty(nilfs); |
| 50 | } |
| 51 | nilfs->ns_prev_seq = nilfs->ns_last_seq; |
| 52 | |
| 53 | stay_cursor: |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 54 | spin_unlock(&nilfs->ns_last_segment_lock); |
| 55 | } |
| 56 | |
| 57 | /** |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 58 | * alloc_nilfs - allocate a nilfs object |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 59 | * @bdev: block device to which the_nilfs is related |
| 60 | * |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 61 | * Return Value: On success, pointer to the_nilfs is returned. |
| 62 | * On error, NULL is returned. |
| 63 | */ |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 64 | struct the_nilfs *alloc_nilfs(struct block_device *bdev) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 65 | { |
| 66 | struct the_nilfs *nilfs; |
| 67 | |
| 68 | nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL); |
| 69 | if (!nilfs) |
| 70 | return NULL; |
| 71 | |
| 72 | nilfs->ns_bdev = bdev; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 73 | atomic_set(&nilfs->ns_ndirtyblks, 0); |
| 74 | init_rwsem(&nilfs->ns_sem); |
Ryusuke Konishi | 572d8b3 | 2012-07-30 14:42:07 -0700 | [diff] [blame] | 75 | mutex_init(&nilfs->ns_snapshot_mount_mutex); |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 76 | INIT_LIST_HEAD(&nilfs->ns_dirty_files); |
Ryusuke Konishi | 263d90c | 2010-08-20 19:06:11 +0900 | [diff] [blame] | 77 | INIT_LIST_HEAD(&nilfs->ns_gc_inodes); |
Ryusuke Konishi | 693dd32 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 78 | spin_lock_init(&nilfs->ns_inode_lock); |
Ryusuke Konishi | 9b1fc4e4 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 79 | spin_lock_init(&nilfs->ns_next_gen_lock); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 80 | spin_lock_init(&nilfs->ns_last_segment_lock); |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 81 | nilfs->ns_cptree = RB_ROOT; |
| 82 | spin_lock_init(&nilfs->ns_cptree_lock); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 83 | init_rwsem(&nilfs->ns_segctor_sem); |
Vyacheslav Dubeyko | caa05d4 | 2014-08-08 14:20:42 -0700 | [diff] [blame] | 84 | nilfs->ns_sb_update_freq = NILFS_SB_FREQ; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 85 | |
| 86 | return nilfs; |
| 87 | } |
| 88 | |
| 89 | /** |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 90 | * destroy_nilfs - destroy nilfs object |
| 91 | * @nilfs: nilfs object to be released |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 92 | */ |
Ryusuke Konishi | 348fe8d | 2010-09-09 02:07:56 +0900 | [diff] [blame] | 93 | void destroy_nilfs(struct the_nilfs *nilfs) |
Ryusuke Konishi | 33c8e57 | 2009-06-08 01:39:29 +0900 | [diff] [blame] | 94 | { |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 95 | might_sleep(); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 96 | if (nilfs_init(nilfs)) { |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 97 | nilfs_sysfs_delete_device_group(nilfs); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 98 | brelse(nilfs->ns_sbh[0]); |
| 99 | brelse(nilfs->ns_sbh[1]); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 100 | } |
| 101 | kfree(nilfs); |
| 102 | } |
| 103 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 104 | static int nilfs_load_super_root(struct the_nilfs *nilfs, |
| 105 | struct super_block *sb, sector_t sr_block) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 106 | { |
| 107 | struct buffer_head *bh_sr; |
| 108 | struct nilfs_super_root *raw_sr; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 109 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 110 | struct nilfs_inode *rawi; |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 111 | unsigned int dat_entry_size, segment_usage_size, checkpoint_size; |
| 112 | unsigned int inode_size; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 113 | int err; |
| 114 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 115 | err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 116 | if (unlikely(err)) |
| 117 | return err; |
| 118 | |
| 119 | down_read(&nilfs->ns_sem); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 120 | dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); |
| 121 | checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); |
| 122 | segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 123 | up_read(&nilfs->ns_sem); |
| 124 | |
| 125 | inode_size = nilfs->ns_inode_size; |
| 126 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 127 | rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size); |
| 128 | err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat); |
| 129 | if (err) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 130 | goto failed; |
| 131 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 132 | rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size); |
| 133 | err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile); |
| 134 | if (err) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 135 | goto failed_dat; |
| 136 | |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 137 | rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size); |
| 138 | err = nilfs_sufile_read(sb, segment_usage_size, rawi, |
| 139 | &nilfs->ns_sufile); |
| 140 | if (err) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 141 | goto failed_cpfile; |
| 142 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 143 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; |
| 144 | nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); |
| 145 | |
| 146 | failed: |
| 147 | brelse(bh_sr); |
| 148 | return err; |
| 149 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 150 | failed_cpfile: |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 151 | iput(nilfs->ns_cpfile); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 152 | |
| 153 | failed_dat: |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 154 | iput(nilfs->ns_dat); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 155 | goto failed; |
| 156 | } |
| 157 | |
| 158 | static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) |
| 159 | { |
| 160 | memset(ri, 0, sizeof(*ri)); |
| 161 | INIT_LIST_HEAD(&ri->ri_used_segments); |
| 162 | } |
| 163 | |
| 164 | static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) |
| 165 | { |
| 166 | nilfs_dispose_segment_list(&ri->ri_used_segments); |
| 167 | } |
| 168 | |
| 169 | /** |
Ryusuke Konishi | 843d63b | 2010-06-28 19:15:24 +0900 | [diff] [blame] | 170 | * nilfs_store_log_cursor - load log cursor from a super block |
| 171 | * @nilfs: nilfs object |
| 172 | * @sbp: buffer storing super block to be read |
| 173 | * |
| 174 | * nilfs_store_log_cursor() reads the last position of the log |
| 175 | * containing a super root from a given super block, and initializes |
| 176 | * relevant information on the nilfs object preparatory for log |
| 177 | * scanning and recovery. |
| 178 | */ |
| 179 | static int nilfs_store_log_cursor(struct the_nilfs *nilfs, |
| 180 | struct nilfs_super_block *sbp) |
| 181 | { |
| 182 | int ret = 0; |
| 183 | |
| 184 | nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); |
| 185 | nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); |
| 186 | nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); |
| 187 | |
Ryusuke Konishi | 3250204 | 2010-06-29 14:42:13 +0900 | [diff] [blame] | 188 | nilfs->ns_prev_seq = nilfs->ns_last_seq; |
Ryusuke Konishi | 843d63b | 2010-06-28 19:15:24 +0900 | [diff] [blame] | 189 | nilfs->ns_seg_seq = nilfs->ns_last_seq; |
| 190 | nilfs->ns_segnum = |
| 191 | nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); |
| 192 | nilfs->ns_cno = nilfs->ns_last_cno + 1; |
| 193 | if (nilfs->ns_segnum >= nilfs->ns_nsegments) { |
| 194 | printk(KERN_ERR "NILFS invalid last segment number.\n"); |
| 195 | ret = -EINVAL; |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | /** |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 201 | * load_nilfs - load and recover the nilfs |
| 202 | * @nilfs: the_nilfs structure to be released |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 203 | * @sb: super block isntance used to recover past segment |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 204 | * |
| 205 | * load_nilfs() searches and load the latest super root, |
| 206 | * attaches the last segment, and does recovery if needed. |
| 207 | * The caller must call this exclusively for simultaneous mounts. |
| 208 | */ |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 209 | int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 210 | { |
| 211 | struct nilfs_recovery_info ri; |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 212 | unsigned int s_flags = sb->s_flags; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 213 | int really_read_only = bdev_read_only(nilfs->ns_bdev); |
Ryusuke Konishi | a057d2c | 2009-11-19 19:58:46 +0900 | [diff] [blame] | 214 | int valid_fs = nilfs_valid_fs(nilfs); |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 215 | int err; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 216 | |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 217 | if (!valid_fs) { |
| 218 | printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n"); |
| 219 | if (s_flags & MS_RDONLY) { |
| 220 | printk(KERN_INFO "NILFS: INFO: recovery " |
| 221 | "required for readonly filesystem.\n"); |
| 222 | printk(KERN_INFO "NILFS: write access will " |
| 223 | "be enabled during recovery.\n"); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 224 | } |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 227 | nilfs_init_recovery_info(&ri); |
| 228 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 229 | err = nilfs_search_super_root(nilfs, &ri); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 230 | if (unlikely(err)) { |
Ryusuke Konishi | 6c125160 | 2010-06-28 19:15:26 +0900 | [diff] [blame] | 231 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 232 | int blocksize; |
| 233 | |
| 234 | if (err != -EINVAL) |
| 235 | goto scan_error; |
| 236 | |
| 237 | if (!nilfs_valid_sb(sbp[1])) { |
| 238 | printk(KERN_WARNING |
| 239 | "NILFS warning: unable to fall back to spare" |
| 240 | "super block\n"); |
| 241 | goto scan_error; |
| 242 | } |
| 243 | printk(KERN_INFO |
| 244 | "NILFS: try rollback from an earlier position\n"); |
| 245 | |
| 246 | /* |
| 247 | * restore super block with its spare and reconfigure |
| 248 | * relevant states of the nilfs object. |
| 249 | */ |
| 250 | memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); |
| 251 | nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed); |
| 252 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); |
| 253 | |
| 254 | /* verify consistency between two super blocks */ |
| 255 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size); |
| 256 | if (blocksize != nilfs->ns_blocksize) { |
| 257 | printk(KERN_WARNING |
| 258 | "NILFS warning: blocksize differs between " |
| 259 | "two super blocks (%d != %d)\n", |
| 260 | blocksize, nilfs->ns_blocksize); |
| 261 | goto scan_error; |
| 262 | } |
| 263 | |
| 264 | err = nilfs_store_log_cursor(nilfs, sbp[0]); |
| 265 | if (err) |
| 266 | goto scan_error; |
| 267 | |
| 268 | /* drop clean flag to allow roll-forward and recovery */ |
| 269 | nilfs->ns_mount_state &= ~NILFS_VALID_FS; |
| 270 | valid_fs = 0; |
| 271 | |
| 272 | err = nilfs_search_super_root(nilfs, &ri); |
| 273 | if (err) |
| 274 | goto scan_error; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 277 | err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 278 | if (unlikely(err)) { |
| 279 | printk(KERN_ERR "NILFS: error loading super root.\n"); |
| 280 | goto failed; |
| 281 | } |
| 282 | |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 283 | if (valid_fs) |
| 284 | goto skip_recovery; |
| 285 | |
| 286 | if (s_flags & MS_RDONLY) { |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 287 | __u64 features; |
| 288 | |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 289 | if (nilfs_test_opt(nilfs, NORECOVERY)) { |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 290 | printk(KERN_INFO "NILFS: norecovery option specified. " |
| 291 | "skipping roll-forward recovery\n"); |
| 292 | goto skip_recovery; |
| 293 | } |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 294 | features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & |
| 295 | ~NILFS_FEATURE_COMPAT_RO_SUPP; |
| 296 | if (features) { |
| 297 | printk(KERN_ERR "NILFS: couldn't proceed with " |
| 298 | "recovery because of unsupported optional " |
| 299 | "features (%llx)\n", |
| 300 | (unsigned long long)features); |
| 301 | err = -EROFS; |
| 302 | goto failed_unload; |
| 303 | } |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 304 | if (really_read_only) { |
| 305 | printk(KERN_ERR "NILFS: write access " |
| 306 | "unavailable, cannot proceed.\n"); |
| 307 | err = -EROFS; |
| 308 | goto failed_unload; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 309 | } |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 310 | sb->s_flags &= ~MS_RDONLY; |
Ryusuke Konishi | 3b2ce58 | 2011-03-09 11:05:07 +0900 | [diff] [blame] | 311 | } else if (nilfs_test_opt(nilfs, NORECOVERY)) { |
Ryusuke Konishi | 0234576 | 2009-11-20 03:28:01 +0900 | [diff] [blame] | 312 | printk(KERN_ERR "NILFS: recovery cancelled because norecovery " |
| 313 | "option was specified for a read/write mount\n"); |
| 314 | err = -EINVAL; |
| 315 | goto failed_unload; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 318 | err = nilfs_salvage_orphan_logs(nilfs, sb, &ri); |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 319 | if (err) |
| 320 | goto failed_unload; |
| 321 | |
| 322 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | 7ecaa46 | 2010-06-28 17:49:29 +0900 | [diff] [blame] | 323 | nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */ |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 324 | err = nilfs_cleanup_super(sb); |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 325 | up_write(&nilfs->ns_sem); |
| 326 | |
| 327 | if (err) { |
| 328 | printk(KERN_ERR "NILFS: failed to update super block. " |
| 329 | "recovery unfinished.\n"); |
| 330 | goto failed_unload; |
| 331 | } |
| 332 | printk(KERN_INFO "NILFS: recovery complete.\n"); |
| 333 | |
| 334 | skip_recovery: |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 335 | nilfs_clear_recovery_info(&ri); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 336 | sb->s_flags = s_flags; |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 337 | return 0; |
| 338 | |
Ryusuke Konishi | 6c125160 | 2010-06-28 19:15:26 +0900 | [diff] [blame] | 339 | scan_error: |
| 340 | printk(KERN_ERR "NILFS: error searching super root.\n"); |
| 341 | goto failed; |
| 342 | |
Ryusuke Konishi | f50a4c8 | 2009-11-19 16:58:40 +0900 | [diff] [blame] | 343 | failed_unload: |
Ryusuke Konishi | f1e89c8 | 2010-09-05 12:20:59 +0900 | [diff] [blame] | 344 | iput(nilfs->ns_cpfile); |
| 345 | iput(nilfs->ns_sufile); |
| 346 | iput(nilfs->ns_dat); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 347 | |
| 348 | failed: |
| 349 | nilfs_clear_recovery_info(&ri); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 350 | sb->s_flags = s_flags; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 351 | return err; |
| 352 | } |
| 353 | |
| 354 | static unsigned long long nilfs_max_size(unsigned int blkbits) |
| 355 | { |
| 356 | unsigned int max_bits; |
| 357 | unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */ |
| 358 | |
| 359 | max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */ |
| 360 | if (max_bits < 64) |
| 361 | res = min_t(unsigned long long, res, (1ULL << max_bits) - 1); |
| 362 | return res; |
| 363 | } |
| 364 | |
Ryusuke Konishi | 4e33f9e | 2011-05-05 01:23:58 +0900 | [diff] [blame] | 365 | /** |
| 366 | * nilfs_nrsvsegs - calculate the number of reserved segments |
| 367 | * @nilfs: nilfs object |
| 368 | * @nsegs: total number of segments |
| 369 | */ |
| 370 | unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs) |
| 371 | { |
| 372 | return max_t(unsigned long, NILFS_MIN_NRSVSEGS, |
| 373 | DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage, |
| 374 | 100)); |
| 375 | } |
| 376 | |
| 377 | void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs) |
| 378 | { |
| 379 | nilfs->ns_nsegments = nsegs; |
| 380 | nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs); |
| 381 | } |
| 382 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 383 | static int nilfs_store_disk_layout(struct the_nilfs *nilfs, |
| 384 | struct nilfs_super_block *sbp) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 385 | { |
Ryusuke Konishi | 9566a7a | 2010-08-10 00:58:41 +0900 | [diff] [blame] | 386 | if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) { |
| 387 | printk(KERN_ERR "NILFS: unsupported revision " |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 388 | "(superblock rev.=%d.%d, current rev.=%d.%d). " |
| 389 | "Please check the version of mkfs.nilfs.\n", |
| 390 | le32_to_cpu(sbp->s_rev_level), |
| 391 | le16_to_cpu(sbp->s_minor_rev_level), |
| 392 | NILFS_CURRENT_REV, NILFS_MINOR_REV); |
| 393 | return -EINVAL; |
| 394 | } |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 395 | nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); |
| 396 | if (nilfs->ns_sbsize > BLOCK_SIZE) |
| 397 | return -EINVAL; |
| 398 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 399 | nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size); |
Ryusuke Konishi | 0ec060d | 2014-04-03 14:50:31 -0700 | [diff] [blame] | 400 | if (nilfs->ns_inode_size > nilfs->ns_blocksize) { |
| 401 | printk(KERN_ERR "NILFS: too large inode size: %d bytes.\n", |
| 402 | nilfs->ns_inode_size); |
| 403 | return -EINVAL; |
| 404 | } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) { |
| 405 | printk(KERN_ERR "NILFS: too small inode size: %d bytes.\n", |
| 406 | nilfs->ns_inode_size); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 410 | nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino); |
| 411 | |
| 412 | nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment); |
| 413 | if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) { |
Ryusuke Konishi | c91cea1 | 2010-03-14 04:01:27 +0900 | [diff] [blame] | 414 | printk(KERN_ERR "NILFS: too short segment.\n"); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 419 | nilfs->ns_r_segments_percentage = |
| 420 | le32_to_cpu(sbp->s_r_segments_percentage); |
Haogang Chen | 3d777a6 | 2012-03-16 17:08:38 -0700 | [diff] [blame] | 421 | if (nilfs->ns_r_segments_percentage < 1 || |
| 422 | nilfs->ns_r_segments_percentage > 99) { |
| 423 | printk(KERN_ERR "NILFS: invalid reserved segments percentage.\n"); |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
Ryusuke Konishi | 4e33f9e | 2011-05-05 01:23:58 +0900 | [diff] [blame] | 427 | nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments)); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 428 | nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 432 | static int nilfs_valid_sb(struct nilfs_super_block *sbp) |
| 433 | { |
| 434 | static unsigned char sum[4]; |
| 435 | const int sumoff = offsetof(struct nilfs_super_block, s_sum); |
| 436 | size_t bytes; |
| 437 | u32 crc; |
| 438 | |
| 439 | if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC) |
| 440 | return 0; |
| 441 | bytes = le16_to_cpu(sbp->s_bytes); |
Torsten Hilbrich | 63d2f95 | 2016-06-24 14:50:18 -0700 | [diff] [blame] | 442 | if (bytes < sumoff + 4 || bytes > BLOCK_SIZE) |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 443 | return 0; |
| 444 | crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp, |
| 445 | sumoff); |
| 446 | crc = crc32_le(crc, sum, 4); |
| 447 | crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4, |
| 448 | bytes - sumoff - 4); |
| 449 | return crc == le32_to_cpu(sbp->s_sum); |
| 450 | } |
| 451 | |
| 452 | static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset) |
| 453 | { |
| 454 | return offset < ((le64_to_cpu(sbp->s_nsegments) * |
| 455 | le32_to_cpu(sbp->s_blocks_per_segment)) << |
| 456 | (le32_to_cpu(sbp->s_log_block_size) + 10)); |
| 457 | } |
| 458 | |
| 459 | static void nilfs_release_super_block(struct the_nilfs *nilfs) |
| 460 | { |
| 461 | int i; |
| 462 | |
| 463 | for (i = 0; i < 2; i++) { |
| 464 | if (nilfs->ns_sbp[i]) { |
| 465 | brelse(nilfs->ns_sbh[i]); |
| 466 | nilfs->ns_sbh[i] = NULL; |
| 467 | nilfs->ns_sbp[i] = NULL; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | void nilfs_fall_back_super_block(struct the_nilfs *nilfs) |
| 473 | { |
| 474 | brelse(nilfs->ns_sbh[0]); |
| 475 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; |
| 476 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; |
| 477 | nilfs->ns_sbh[1] = NULL; |
| 478 | nilfs->ns_sbp[1] = NULL; |
| 479 | } |
| 480 | |
| 481 | void nilfs_swap_super_block(struct the_nilfs *nilfs) |
| 482 | { |
| 483 | struct buffer_head *tsbh = nilfs->ns_sbh[0]; |
| 484 | struct nilfs_super_block *tsbp = nilfs->ns_sbp[0]; |
| 485 | |
| 486 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; |
| 487 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; |
| 488 | nilfs->ns_sbh[1] = tsbh; |
| 489 | nilfs->ns_sbp[1] = tsbp; |
| 490 | } |
| 491 | |
| 492 | static int nilfs_load_super_block(struct the_nilfs *nilfs, |
| 493 | struct super_block *sb, int blocksize, |
| 494 | struct nilfs_super_block **sbpp) |
| 495 | { |
| 496 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
| 497 | struct buffer_head **sbh = nilfs->ns_sbh; |
| 498 | u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); |
| 499 | int valid[2], swp = 0; |
| 500 | |
| 501 | sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, |
| 502 | &sbh[0]); |
| 503 | sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); |
| 504 | |
| 505 | if (!sbp[0]) { |
| 506 | if (!sbp[1]) { |
| 507 | printk(KERN_ERR "NILFS: unable to read superblock\n"); |
| 508 | return -EIO; |
| 509 | } |
| 510 | printk(KERN_WARNING |
Ryusuke Konishi | 4138ec2 | 2011-01-24 00:28:22 +0900 | [diff] [blame] | 511 | "NILFS warning: unable to read primary superblock " |
| 512 | "(blocksize = %d)\n", blocksize); |
| 513 | } else if (!sbp[1]) { |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 514 | printk(KERN_WARNING |
Ryusuke Konishi | 4138ec2 | 2011-01-24 00:28:22 +0900 | [diff] [blame] | 515 | "NILFS warning: unable to read secondary superblock " |
| 516 | "(blocksize = %d)\n", blocksize); |
| 517 | } |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 518 | |
Ryusuke Konishi | 25294d8 | 2010-05-01 11:54:21 +0900 | [diff] [blame] | 519 | /* |
| 520 | * Compare two super blocks and set 1 in swp if the secondary |
| 521 | * super block is valid and newer. Otherwise, set 0 in swp. |
| 522 | */ |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 523 | valid[0] = nilfs_valid_sb(sbp[0]); |
| 524 | valid[1] = nilfs_valid_sb(sbp[1]); |
Ryusuke Konishi | 25294d8 | 2010-05-01 11:54:21 +0900 | [diff] [blame] | 525 | swp = valid[1] && (!valid[0] || |
| 526 | le64_to_cpu(sbp[1]->s_last_cno) > |
| 527 | le64_to_cpu(sbp[0]->s_last_cno)); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 528 | |
| 529 | if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { |
| 530 | brelse(sbh[1]); |
| 531 | sbh[1] = NULL; |
| 532 | sbp[1] = NULL; |
Ryusuke Konishi | d7178c7 | 2012-03-16 17:08:39 -0700 | [diff] [blame] | 533 | valid[1] = 0; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 534 | swp = 0; |
| 535 | } |
| 536 | if (!valid[swp]) { |
| 537 | nilfs_release_super_block(nilfs); |
| 538 | printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n", |
| 539 | sb->s_id); |
| 540 | return -EINVAL; |
| 541 | } |
| 542 | |
Ryusuke Konishi | ea1a16f | 2010-08-15 20:16:11 +0900 | [diff] [blame] | 543 | if (!valid[!swp]) |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 544 | printk(KERN_WARNING "NILFS warning: broken superblock. " |
Ryusuke Konishi | 4138ec2 | 2011-01-24 00:28:22 +0900 | [diff] [blame] | 545 | "using spare superblock (blocksize = %d).\n", blocksize); |
Ryusuke Konishi | ea1a16f | 2010-08-15 20:16:11 +0900 | [diff] [blame] | 546 | if (swp) |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 547 | nilfs_swap_super_block(nilfs); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 548 | |
Jiro SEKIBA | b2ac86e | 2010-06-28 17:49:33 +0900 | [diff] [blame] | 549 | nilfs->ns_sbwcount = 0; |
| 550 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 551 | nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); |
| 552 | *sbpp = sbp[0]; |
| 553 | return 0; |
| 554 | } |
| 555 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 556 | /** |
| 557 | * init_nilfs - initialize a NILFS instance. |
| 558 | * @nilfs: the_nilfs structure |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 559 | * @sb: super block |
| 560 | * @data: mount options |
| 561 | * |
| 562 | * init_nilfs() performs common initialization per block device (e.g. |
| 563 | * reading the super block, getting disk layout information, initializing |
Ryusuke Konishi | f11459a | 2010-08-16 01:54:52 +0900 | [diff] [blame] | 564 | * shared fields in the_nilfs). |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 565 | * |
| 566 | * Return Value: On success, 0 is returned. On error, a negative error |
| 567 | * code is returned. |
| 568 | */ |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 569 | int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 570 | { |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 571 | struct nilfs_super_block *sbp; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 572 | int blocksize; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 573 | int err; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 574 | |
| 575 | down_write(&nilfs->ns_sem); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 576 | |
Ryusuke Konishi | 89c0fd0 | 2010-07-25 22:44:53 +0900 | [diff] [blame] | 577 | blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 578 | if (!blocksize) { |
| 579 | printk(KERN_ERR "NILFS: unable to set blocksize\n"); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 580 | err = -EINVAL; |
| 581 | goto out; |
| 582 | } |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 583 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); |
| 584 | if (err) |
| 585 | goto out; |
| 586 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 587 | err = nilfs_store_magic_and_option(sb, sbp, data); |
| 588 | if (err) |
| 589 | goto failed_sbh; |
| 590 | |
Ryusuke Konishi | c5ca48a | 2010-07-22 03:22:20 +0900 | [diff] [blame] | 591 | err = nilfs_check_feature_compatibility(sb, sbp); |
| 592 | if (err) |
| 593 | goto failed_sbh; |
| 594 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 595 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); |
Ryusuke Konishi | 89c0fd0 | 2010-07-25 22:44:53 +0900 | [diff] [blame] | 596 | if (blocksize < NILFS_MIN_BLOCK_SIZE || |
| 597 | blocksize > NILFS_MAX_BLOCK_SIZE) { |
| 598 | printk(KERN_ERR "NILFS: couldn't mount because of unsupported " |
| 599 | "filesystem blocksize %d\n", blocksize); |
| 600 | err = -EINVAL; |
| 601 | goto failed_sbh; |
| 602 | } |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 603 | if (sb->s_blocksize != blocksize) { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 604 | int hw_blocksize = bdev_logical_block_size(sb->s_bdev); |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 605 | |
| 606 | if (blocksize < hw_blocksize) { |
| 607 | printk(KERN_ERR |
| 608 | "NILFS: blocksize %d too small for device " |
| 609 | "(sector-size = %d).\n", |
| 610 | blocksize, hw_blocksize); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 611 | err = -EINVAL; |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 612 | goto failed_sbh; |
| 613 | } |
| 614 | nilfs_release_super_block(nilfs); |
| 615 | sb_set_blocksize(sb, blocksize); |
| 616 | |
| 617 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); |
| 618 | if (err) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 619 | goto out; |
Ryusuke Konishi | 076a378 | 2016-05-23 16:23:48 -0700 | [diff] [blame] | 620 | /* |
| 621 | * Not to failed_sbh; sbh is released automatically |
| 622 | * when reloading fails. |
| 623 | */ |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 624 | } |
| 625 | nilfs->ns_blocksize_bits = sb->s_blocksize_bits; |
Ryusuke Konishi | 92c60cc | 2010-05-23 00:17:48 +0900 | [diff] [blame] | 626 | nilfs->ns_blocksize = blocksize; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 627 | |
Ryusuke Konishi | 9b1fc4e4 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 628 | get_random_bytes(&nilfs->ns_next_generation, |
| 629 | sizeof(nilfs->ns_next_generation)); |
| 630 | |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 631 | err = nilfs_store_disk_layout(nilfs, sbp); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 632 | if (err) |
| 633 | goto failed_sbh; |
| 634 | |
| 635 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); |
| 636 | |
| 637 | nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 638 | |
Ryusuke Konishi | 843d63b | 2010-06-28 19:15:24 +0900 | [diff] [blame] | 639 | err = nilfs_store_log_cursor(nilfs, sbp); |
| 640 | if (err) |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 641 | goto failed_sbh; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 642 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 643 | err = nilfs_sysfs_create_device_group(sb); |
| 644 | if (err) |
| 645 | goto failed_sbh; |
| 646 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 647 | set_nilfs_init(nilfs); |
| 648 | err = 0; |
| 649 | out: |
| 650 | up_write(&nilfs->ns_sem); |
| 651 | return err; |
| 652 | |
| 653 | failed_sbh: |
Ryusuke Konishi | e339ad3 | 2009-04-06 19:01:59 -0700 | [diff] [blame] | 654 | nilfs_release_super_block(nilfs); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 655 | goto out; |
| 656 | } |
| 657 | |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 658 | int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump, |
| 659 | size_t nsegs) |
| 660 | { |
| 661 | sector_t seg_start, seg_end; |
| 662 | sector_t start = 0, nblocks = 0; |
| 663 | unsigned int sects_per_block; |
| 664 | __u64 *sn; |
| 665 | int ret = 0; |
| 666 | |
| 667 | sects_per_block = (1 << nilfs->ns_blocksize_bits) / |
| 668 | bdev_logical_block_size(nilfs->ns_bdev); |
| 669 | for (sn = segnump; sn < segnump + nsegs; sn++) { |
| 670 | nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end); |
| 671 | |
| 672 | if (!nblocks) { |
| 673 | start = seg_start; |
| 674 | nblocks = seg_end - seg_start + 1; |
| 675 | } else if (start + nblocks == seg_start) { |
| 676 | nblocks += seg_end - seg_start + 1; |
| 677 | } else { |
| 678 | ret = blkdev_issue_discard(nilfs->ns_bdev, |
| 679 | start * sects_per_block, |
| 680 | nblocks * sects_per_block, |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 681 | GFP_NOFS, 0); |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 682 | if (ret < 0) |
| 683 | return ret; |
| 684 | nblocks = 0; |
| 685 | } |
| 686 | } |
| 687 | if (nblocks) |
| 688 | ret = blkdev_issue_discard(nilfs->ns_bdev, |
| 689 | start * sects_per_block, |
| 690 | nblocks * sects_per_block, |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 691 | GFP_NOFS, 0); |
Jiro SEKIBA | e902ec9 | 2010-01-30 18:06:35 +0900 | [diff] [blame] | 692 | return ret; |
| 693 | } |
| 694 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 695 | int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) |
| 696 | { |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 697 | unsigned long ncleansegs; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 698 | |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 699 | down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | ef7d475 | 2009-11-13 08:45:32 +0900 | [diff] [blame] | 700 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); |
Ryusuke Konishi | 365e215 | 2010-12-27 00:07:30 +0900 | [diff] [blame] | 701 | up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); |
Ryusuke Konishi | ef7d475 | 2009-11-13 08:45:32 +0900 | [diff] [blame] | 702 | *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; |
| 703 | return 0; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 706 | int nilfs_near_disk_full(struct the_nilfs *nilfs) |
| 707 | { |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 708 | unsigned long ncleansegs, nincsegs; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 709 | |
Ryusuke Konishi | ef7d475 | 2009-11-13 08:45:32 +0900 | [diff] [blame] | 710 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); |
| 711 | nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / |
| 712 | nilfs->ns_blocks_per_segment + 1; |
| 713 | |
| 714 | return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 717 | struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno) |
Ryusuke Konishi | 6dd4740 | 2009-06-08 01:39:31 +0900 | [diff] [blame] | 718 | { |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 719 | struct rb_node *n; |
| 720 | struct nilfs_root *root; |
Ryusuke Konishi | 6dd4740 | 2009-06-08 01:39:31 +0900 | [diff] [blame] | 721 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 722 | spin_lock(&nilfs->ns_cptree_lock); |
| 723 | n = nilfs->ns_cptree.rb_node; |
| 724 | while (n) { |
| 725 | root = rb_entry(n, struct nilfs_root, rb_node); |
Ryusuke Konishi | 6dd4740 | 2009-06-08 01:39:31 +0900 | [diff] [blame] | 726 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 727 | if (cno < root->cno) { |
| 728 | n = n->rb_left; |
| 729 | } else if (cno > root->cno) { |
| 730 | n = n->rb_right; |
| 731 | } else { |
| 732 | atomic_inc(&root->count); |
| 733 | spin_unlock(&nilfs->ns_cptree_lock); |
| 734 | return root; |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 737 | spin_unlock(&nilfs->ns_cptree_lock); |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 738 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 739 | return NULL; |
| 740 | } |
| 741 | |
| 742 | struct nilfs_root * |
| 743 | nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) |
| 744 | { |
| 745 | struct rb_node **p, *parent; |
| 746 | struct nilfs_root *root, *new; |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 747 | int err; |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 748 | |
| 749 | root = nilfs_lookup_root(nilfs, cno); |
| 750 | if (root) |
| 751 | return root; |
| 752 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 753 | new = kzalloc(sizeof(*root), GFP_KERNEL); |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 754 | if (!new) |
| 755 | return NULL; |
| 756 | |
| 757 | spin_lock(&nilfs->ns_cptree_lock); |
| 758 | |
| 759 | p = &nilfs->ns_cptree.rb_node; |
| 760 | parent = NULL; |
| 761 | |
| 762 | while (*p) { |
| 763 | parent = *p; |
| 764 | root = rb_entry(parent, struct nilfs_root, rb_node); |
| 765 | |
| 766 | if (cno < root->cno) { |
| 767 | p = &(*p)->rb_left; |
| 768 | } else if (cno > root->cno) { |
| 769 | p = &(*p)->rb_right; |
| 770 | } else { |
| 771 | atomic_inc(&root->count); |
| 772 | spin_unlock(&nilfs->ns_cptree_lock); |
| 773 | kfree(new); |
| 774 | return root; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | new->cno = cno; |
| 779 | new->ifile = NULL; |
| 780 | new->nilfs = nilfs; |
| 781 | atomic_set(&new->count, 1); |
Vyacheslav Dubeyko | e5f7f84 | 2013-07-03 15:08:06 -0700 | [diff] [blame] | 782 | atomic64_set(&new->inodes_count, 0); |
| 783 | atomic64_set(&new->blocks_count, 0); |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 784 | |
| 785 | rb_link_node(&new->rb_node, parent, p); |
| 786 | rb_insert_color(&new->rb_node, &nilfs->ns_cptree); |
| 787 | |
| 788 | spin_unlock(&nilfs->ns_cptree_lock); |
| 789 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 790 | err = nilfs_sysfs_create_snapshot_group(new); |
| 791 | if (err) { |
| 792 | kfree(new); |
| 793 | new = NULL; |
| 794 | } |
| 795 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 796 | return new; |
| 797 | } |
| 798 | |
| 799 | void nilfs_put_root(struct nilfs_root *root) |
| 800 | { |
| 801 | if (atomic_dec_and_test(&root->count)) { |
| 802 | struct the_nilfs *nilfs = root->nilfs; |
| 803 | |
Vyacheslav Dubeyko | dd70edb | 2014-08-08 14:20:55 -0700 | [diff] [blame] | 804 | nilfs_sysfs_delete_snapshot_group(root); |
| 805 | |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 806 | spin_lock(&nilfs->ns_cptree_lock); |
| 807 | rb_erase(&root->rb_node, &nilfs->ns_cptree); |
| 808 | spin_unlock(&nilfs->ns_cptree_lock); |
Markus Elfring | 72b9918 | 2014-12-10 15:54:31 -0800 | [diff] [blame] | 809 | iput(root->ifile); |
Ryusuke Konishi | ba65ae4 | 2010-08-14 12:59:15 +0900 | [diff] [blame] | 810 | |
| 811 | kfree(root); |
| 812 | } |
Ryusuke Konishi | 8a9d219 | 2009-04-06 19:01:35 -0700 | [diff] [blame] | 813 | } |