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