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