1 // SPDX-License-Identifier: GPL-2.0+ << 2 /* 1 /* 3 * the_nilfs shared structure. !! 2 * the_nilfs.c - the_nilfs shared structure. 4 * 3 * 5 * Copyright (C) 2005-2008 Nippon Telegraph an 4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 6 * 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 * 7 * Written by Ryusuke Konishi. 16 * Written by Ryusuke Konishi. 8 * 17 * 9 */ 18 */ 10 19 11 #include <linux/buffer_head.h> 20 #include <linux/buffer_head.h> 12 #include <linux/slab.h> 21 #include <linux/slab.h> 13 #include <linux/blkdev.h> 22 #include <linux/blkdev.h> 14 #include <linux/backing-dev.h> 23 #include <linux/backing-dev.h> 15 #include <linux/log2.h> !! 24 #include <linux/random.h> 16 #include <linux/crc32.h> 25 #include <linux/crc32.h> 17 #include "nilfs.h" 26 #include "nilfs.h" 18 #include "segment.h" 27 #include "segment.h" 19 #include "alloc.h" 28 #include "alloc.h" 20 #include "cpfile.h" 29 #include "cpfile.h" 21 #include "sufile.h" 30 #include "sufile.h" 22 #include "dat.h" 31 #include "dat.h" 23 #include "segbuf.h" 32 #include "segbuf.h" 24 33 25 34 26 static int nilfs_valid_sb(struct nilfs_super_b 35 static int nilfs_valid_sb(struct nilfs_super_block *sbp); 27 36 28 void nilfs_set_last_segment(struct the_nilfs * 37 void nilfs_set_last_segment(struct the_nilfs *nilfs, 29 sector_t start_blo 38 sector_t start_blocknr, u64 seq, __u64 cno) 30 { 39 { 31 spin_lock(&nilfs->ns_last_segment_lock 40 spin_lock(&nilfs->ns_last_segment_lock); 32 nilfs->ns_last_pseg = start_blocknr; 41 nilfs->ns_last_pseg = start_blocknr; 33 nilfs->ns_last_seq = seq; 42 nilfs->ns_last_seq = seq; 34 nilfs->ns_last_cno = cno; 43 nilfs->ns_last_cno = cno; 35 44 36 if (!nilfs_sb_dirty(nilfs)) { 45 if (!nilfs_sb_dirty(nilfs)) { 37 if (nilfs->ns_prev_seq == nilf 46 if (nilfs->ns_prev_seq == nilfs->ns_last_seq) 38 goto stay_cursor; 47 goto stay_cursor; 39 48 40 set_nilfs_sb_dirty(nilfs); 49 set_nilfs_sb_dirty(nilfs); 41 } 50 } 42 nilfs->ns_prev_seq = nilfs->ns_last_se 51 nilfs->ns_prev_seq = nilfs->ns_last_seq; 43 52 44 stay_cursor: 53 stay_cursor: 45 spin_unlock(&nilfs->ns_last_segment_lo 54 spin_unlock(&nilfs->ns_last_segment_lock); 46 } 55 } 47 56 48 /** 57 /** 49 * alloc_nilfs - allocate a nilfs object 58 * alloc_nilfs - allocate a nilfs object 50 * @sb: super block instance 59 * @sb: super block instance 51 * 60 * 52 * Return Value: On success, pointer to the_ni 61 * Return Value: On success, pointer to the_nilfs is returned. 53 * On error, NULL is returned. 62 * On error, NULL is returned. 54 */ 63 */ 55 struct the_nilfs *alloc_nilfs(struct super_blo 64 struct the_nilfs *alloc_nilfs(struct super_block *sb) 56 { 65 { 57 struct the_nilfs *nilfs; 66 struct the_nilfs *nilfs; 58 67 59 nilfs = kzalloc(sizeof(*nilfs), GFP_KE 68 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL); 60 if (!nilfs) 69 if (!nilfs) 61 return NULL; 70 return NULL; 62 71 63 nilfs->ns_sb = sb; 72 nilfs->ns_sb = sb; 64 nilfs->ns_bdev = sb->s_bdev; 73 nilfs->ns_bdev = sb->s_bdev; 65 atomic_set(&nilfs->ns_ndirtyblks, 0); 74 atomic_set(&nilfs->ns_ndirtyblks, 0); 66 init_rwsem(&nilfs->ns_sem); 75 init_rwsem(&nilfs->ns_sem); 67 mutex_init(&nilfs->ns_snapshot_mount_m 76 mutex_init(&nilfs->ns_snapshot_mount_mutex); 68 INIT_LIST_HEAD(&nilfs->ns_dirty_files) 77 INIT_LIST_HEAD(&nilfs->ns_dirty_files); 69 INIT_LIST_HEAD(&nilfs->ns_gc_inodes); 78 INIT_LIST_HEAD(&nilfs->ns_gc_inodes); 70 spin_lock_init(&nilfs->ns_inode_lock); 79 spin_lock_init(&nilfs->ns_inode_lock); >> 80 spin_lock_init(&nilfs->ns_next_gen_lock); 71 spin_lock_init(&nilfs->ns_last_segment 81 spin_lock_init(&nilfs->ns_last_segment_lock); 72 nilfs->ns_cptree = RB_ROOT; 82 nilfs->ns_cptree = RB_ROOT; 73 spin_lock_init(&nilfs->ns_cptree_lock) 83 spin_lock_init(&nilfs->ns_cptree_lock); 74 init_rwsem(&nilfs->ns_segctor_sem); 84 init_rwsem(&nilfs->ns_segctor_sem); 75 nilfs->ns_sb_update_freq = NILFS_SB_FR 85 nilfs->ns_sb_update_freq = NILFS_SB_FREQ; 76 86 77 return nilfs; 87 return nilfs; 78 } 88 } 79 89 80 /** 90 /** 81 * destroy_nilfs - destroy nilfs object 91 * destroy_nilfs - destroy nilfs object 82 * @nilfs: nilfs object to be released 92 * @nilfs: nilfs object to be released 83 */ 93 */ 84 void destroy_nilfs(struct the_nilfs *nilfs) 94 void destroy_nilfs(struct the_nilfs *nilfs) 85 { 95 { 86 might_sleep(); 96 might_sleep(); 87 if (nilfs_init(nilfs)) { 97 if (nilfs_init(nilfs)) { >> 98 nilfs_sysfs_delete_device_group(nilfs); 88 brelse(nilfs->ns_sbh[0]); 99 brelse(nilfs->ns_sbh[0]); 89 brelse(nilfs->ns_sbh[1]); 100 brelse(nilfs->ns_sbh[1]); 90 } 101 } 91 kfree(nilfs); 102 kfree(nilfs); 92 } 103 } 93 104 94 static int nilfs_load_super_root(struct the_ni 105 static int nilfs_load_super_root(struct the_nilfs *nilfs, 95 struct super_ 106 struct super_block *sb, sector_t sr_block) 96 { 107 { 97 struct buffer_head *bh_sr; 108 struct buffer_head *bh_sr; 98 struct nilfs_super_root *raw_sr; 109 struct nilfs_super_root *raw_sr; 99 struct nilfs_super_block **sbp = nilfs 110 struct nilfs_super_block **sbp = nilfs->ns_sbp; 100 struct nilfs_inode *rawi; 111 struct nilfs_inode *rawi; 101 unsigned int dat_entry_size, segment_u 112 unsigned int dat_entry_size, segment_usage_size, checkpoint_size; 102 unsigned int inode_size; 113 unsigned int inode_size; 103 int err; 114 int err; 104 115 105 err = nilfs_read_super_root_block(nilf 116 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1); 106 if (unlikely(err)) 117 if (unlikely(err)) 107 return err; 118 return err; 108 119 109 down_read(&nilfs->ns_sem); 120 down_read(&nilfs->ns_sem); 110 dat_entry_size = le16_to_cpu(sbp[0]->s 121 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); 111 checkpoint_size = le16_to_cpu(sbp[0]-> 122 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); 112 segment_usage_size = le16_to_cpu(sbp[0 123 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); 113 up_read(&nilfs->ns_sem); 124 up_read(&nilfs->ns_sem); 114 125 115 inode_size = nilfs->ns_inode_size; 126 inode_size = nilfs->ns_inode_size; 116 127 117 rawi = (void *)bh_sr->b_data + NILFS_S 128 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size); 118 err = nilfs_dat_read(sb, dat_entry_siz 129 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat); 119 if (err) 130 if (err) 120 goto failed; 131 goto failed; 121 132 122 rawi = (void *)bh_sr->b_data + NILFS_S 133 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size); 123 err = nilfs_cpfile_read(sb, checkpoint 134 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile); 124 if (err) 135 if (err) 125 goto failed_dat; 136 goto failed_dat; 126 137 127 rawi = (void *)bh_sr->b_data + NILFS_S 138 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size); 128 err = nilfs_sufile_read(sb, segment_us 139 err = nilfs_sufile_read(sb, segment_usage_size, rawi, 129 &nilfs->ns_suf 140 &nilfs->ns_sufile); 130 if (err) 141 if (err) 131 goto failed_cpfile; 142 goto failed_cpfile; 132 143 133 raw_sr = (struct nilfs_super_root *)bh 144 raw_sr = (struct nilfs_super_root *)bh_sr->b_data; 134 nilfs->ns_nongc_ctime = le64_to_cpu(ra 145 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); 135 146 136 failed: 147 failed: 137 brelse(bh_sr); 148 brelse(bh_sr); 138 return err; 149 return err; 139 150 140 failed_cpfile: 151 failed_cpfile: 141 iput(nilfs->ns_cpfile); 152 iput(nilfs->ns_cpfile); 142 153 143 failed_dat: 154 failed_dat: 144 iput(nilfs->ns_dat); 155 iput(nilfs->ns_dat); 145 goto failed; 156 goto failed; 146 } 157 } 147 158 148 static void nilfs_init_recovery_info(struct ni 159 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) 149 { 160 { 150 memset(ri, 0, sizeof(*ri)); 161 memset(ri, 0, sizeof(*ri)); 151 INIT_LIST_HEAD(&ri->ri_used_segments); 162 INIT_LIST_HEAD(&ri->ri_used_segments); 152 } 163 } 153 164 154 static void nilfs_clear_recovery_info(struct n 165 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) 155 { 166 { 156 nilfs_dispose_segment_list(&ri->ri_use 167 nilfs_dispose_segment_list(&ri->ri_used_segments); 157 } 168 } 158 169 159 /** 170 /** 160 * nilfs_store_log_cursor - load log cursor fr 171 * nilfs_store_log_cursor - load log cursor from a super block 161 * @nilfs: nilfs object 172 * @nilfs: nilfs object 162 * @sbp: buffer storing super block to be read 173 * @sbp: buffer storing super block to be read 163 * 174 * 164 * nilfs_store_log_cursor() reads the last pos 175 * nilfs_store_log_cursor() reads the last position of the log 165 * containing a super root from a given super 176 * containing a super root from a given super block, and initializes 166 * relevant information on the nilfs object pr 177 * relevant information on the nilfs object preparatory for log 167 * scanning and recovery. 178 * scanning and recovery. 168 */ 179 */ 169 static int nilfs_store_log_cursor(struct the_n 180 static int nilfs_store_log_cursor(struct the_nilfs *nilfs, 170 struct nilfs 181 struct nilfs_super_block *sbp) 171 { 182 { 172 int ret = 0; 183 int ret = 0; 173 184 174 nilfs->ns_last_pseg = le64_to_cpu(sbp- 185 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); 175 nilfs->ns_last_cno = le64_to_cpu(sbp-> 186 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); 176 nilfs->ns_last_seq = le64_to_cpu(sbp-> 187 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); 177 188 178 nilfs->ns_prev_seq = nilfs->ns_last_se 189 nilfs->ns_prev_seq = nilfs->ns_last_seq; 179 nilfs->ns_seg_seq = nilfs->ns_last_seq 190 nilfs->ns_seg_seq = nilfs->ns_last_seq; 180 nilfs->ns_segnum = 191 nilfs->ns_segnum = 181 nilfs_get_segnum_of_block(nilf 192 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); 182 nilfs->ns_cno = nilfs->ns_last_cno + 1 193 nilfs->ns_cno = nilfs->ns_last_cno + 1; 183 if (nilfs->ns_segnum >= nilfs->ns_nseg 194 if (nilfs->ns_segnum >= nilfs->ns_nsegments) { 184 nilfs_err(nilfs->ns_sb, !! 195 nilfs_msg(nilfs->ns_sb, KERN_ERR, 185 "pointed segment num 196 "pointed segment number is out of range: segnum=%llu, nsegments=%lu", 186 (unsigned long long) 197 (unsigned long long)nilfs->ns_segnum, 187 nilfs->ns_nsegments) 198 nilfs->ns_nsegments); 188 ret = -EINVAL; 199 ret = -EINVAL; 189 } 200 } 190 return ret; 201 return ret; 191 } 202 } 192 203 193 /** 204 /** 194 * nilfs_get_blocksize - get block size from r << 195 * @sb: super block instance << 196 * @sbp: superblock raw data buffer << 197 * @blocksize: place to store block size << 198 * << 199 * nilfs_get_blocksize() calculates the block << 200 * exponent information written in @sbp and st << 201 * or aborts with an error message if it's too << 202 * << 203 * Return Value: On success, 0 is returned. If << 204 * large, -EINVAL is returned. << 205 */ << 206 static int nilfs_get_blocksize(struct super_bl << 207 struct nilfs_su << 208 { << 209 unsigned int shift_bits = le32_to_cpu( << 210 << 211 if (unlikely(shift_bits > << 212 ilog2(NILFS_MAX_BLOCK_SIZ << 213 nilfs_err(sb, "too large files << 214 shift_bits); << 215 return -EINVAL; << 216 } << 217 *blocksize = BLOCK_SIZE << shift_bits; << 218 return 0; << 219 } << 220 << 221 /** << 222 * load_nilfs - load and recover the nilfs 205 * load_nilfs - load and recover the nilfs 223 * @nilfs: the_nilfs structure to be released 206 * @nilfs: the_nilfs structure to be released 224 * @sb: super block instance used to recover p !! 207 * @sb: super block isntance used to recover past segment 225 * 208 * 226 * load_nilfs() searches and load the latest s 209 * load_nilfs() searches and load the latest super root, 227 * attaches the last segment, and does recover 210 * attaches the last segment, and does recovery if needed. 228 * The caller must call this exclusively for s 211 * The caller must call this exclusively for simultaneous mounts. 229 */ 212 */ 230 int load_nilfs(struct the_nilfs *nilfs, struct 213 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb) 231 { 214 { 232 struct nilfs_recovery_info ri; 215 struct nilfs_recovery_info ri; 233 unsigned int s_flags = sb->s_flags; 216 unsigned int s_flags = sb->s_flags; 234 int really_read_only = bdev_read_only( 217 int really_read_only = bdev_read_only(nilfs->ns_bdev); 235 int valid_fs = nilfs_valid_fs(nilfs); 218 int valid_fs = nilfs_valid_fs(nilfs); 236 int err; 219 int err; 237 220 238 if (!valid_fs) { 221 if (!valid_fs) { 239 nilfs_warn(sb, "mounting unche !! 222 nilfs_msg(sb, KERN_WARNING, "mounting unchecked fs"); 240 if (s_flags & SB_RDONLY) { !! 223 if (s_flags & MS_RDONLY) { 241 nilfs_info(sb, !! 224 nilfs_msg(sb, KERN_INFO, 242 "recovery r !! 225 "recovery required for readonly filesystem"); 243 nilfs_info(sb, !! 226 nilfs_msg(sb, KERN_INFO, 244 "write acce !! 227 "write access will be enabled during recovery"); 245 } 228 } 246 } 229 } 247 230 248 nilfs_init_recovery_info(&ri); 231 nilfs_init_recovery_info(&ri); 249 232 250 err = nilfs_search_super_root(nilfs, & 233 err = nilfs_search_super_root(nilfs, &ri); 251 if (unlikely(err)) { 234 if (unlikely(err)) { 252 struct nilfs_super_block **sbp 235 struct nilfs_super_block **sbp = nilfs->ns_sbp; 253 int blocksize; 236 int blocksize; 254 237 255 if (err != -EINVAL) 238 if (err != -EINVAL) 256 goto scan_error; 239 goto scan_error; 257 240 258 if (!nilfs_valid_sb(sbp[1])) { 241 if (!nilfs_valid_sb(sbp[1])) { 259 nilfs_warn(sb, !! 242 nilfs_msg(sb, KERN_WARNING, 260 "unable to !! 243 "unable to fall back to spare super block"); 261 goto scan_error; 244 goto scan_error; 262 } 245 } 263 nilfs_info(sb, "trying rollbac !! 246 nilfs_msg(sb, KERN_INFO, >> 247 "trying rollback from an earlier position"); 264 248 265 /* 249 /* 266 * restore super block with it 250 * restore super block with its spare and reconfigure 267 * relevant states of the nilf 251 * relevant states of the nilfs object. 268 */ 252 */ 269 memcpy(sbp[0], sbp[1], nilfs-> 253 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); 270 nilfs->ns_crc_seed = le32_to_c 254 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed); 271 nilfs->ns_sbwtime = le64_to_cp 255 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); 272 256 273 /* verify consistency between 257 /* verify consistency between two super blocks */ 274 err = nilfs_get_blocksize(sb, !! 258 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size); 275 if (err) << 276 goto scan_error; << 277 << 278 if (blocksize != nilfs->ns_blo 259 if (blocksize != nilfs->ns_blocksize) { 279 nilfs_warn(sb, !! 260 nilfs_msg(sb, KERN_WARNING, 280 "blocksize !! 261 "blocksize differs between two super blocks (%d != %d)", 281 blocksize, !! 262 blocksize, nilfs->ns_blocksize); 282 err = -EINVAL; << 283 goto scan_error; 263 goto scan_error; 284 } 264 } 285 265 286 err = nilfs_store_log_cursor(n 266 err = nilfs_store_log_cursor(nilfs, sbp[0]); 287 if (err) 267 if (err) 288 goto scan_error; 268 goto scan_error; 289 269 290 /* drop clean flag to allow ro 270 /* drop clean flag to allow roll-forward and recovery */ 291 nilfs->ns_mount_state &= ~NILF 271 nilfs->ns_mount_state &= ~NILFS_VALID_FS; 292 valid_fs = 0; 272 valid_fs = 0; 293 273 294 err = nilfs_search_super_root( 274 err = nilfs_search_super_root(nilfs, &ri); 295 if (err) 275 if (err) 296 goto scan_error; 276 goto scan_error; 297 } 277 } 298 278 299 err = nilfs_load_super_root(nilfs, sb, 279 err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root); 300 if (unlikely(err)) { 280 if (unlikely(err)) { 301 nilfs_err(sb, "error %d while !! 281 nilfs_msg(sb, KERN_ERR, "error %d while loading super root", >> 282 err); 302 goto failed; 283 goto failed; 303 } 284 } 304 285 305 err = nilfs_sysfs_create_device_group( << 306 if (unlikely(err)) << 307 goto sysfs_error; << 308 << 309 if (valid_fs) 286 if (valid_fs) 310 goto skip_recovery; 287 goto skip_recovery; 311 288 312 if (s_flags & SB_RDONLY) { !! 289 if (s_flags & MS_RDONLY) { 313 __u64 features; 290 __u64 features; 314 291 315 if (nilfs_test_opt(nilfs, NORE 292 if (nilfs_test_opt(nilfs, NORECOVERY)) { 316 nilfs_info(sb, !! 293 nilfs_msg(sb, KERN_INFO, 317 "norecovery !! 294 "norecovery option specified, skipping roll-forward recovery"); 318 goto skip_recovery; 295 goto skip_recovery; 319 } 296 } 320 features = le64_to_cpu(nilfs-> 297 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & 321 ~NILFS_FEATURE_COMPAT_ 298 ~NILFS_FEATURE_COMPAT_RO_SUPP; 322 if (features) { 299 if (features) { 323 nilfs_err(sb, !! 300 nilfs_msg(sb, KERN_ERR, 324 "couldn't pr 301 "couldn't proceed with recovery because of unsupported optional features (%llx)", 325 (unsigned lo 302 (unsigned long long)features); 326 err = -EROFS; 303 err = -EROFS; 327 goto failed_unload; 304 goto failed_unload; 328 } 305 } 329 if (really_read_only) { 306 if (really_read_only) { 330 nilfs_err(sb, !! 307 nilfs_msg(sb, KERN_ERR, 331 "write acces 308 "write access unavailable, cannot proceed"); 332 err = -EROFS; 309 err = -EROFS; 333 goto failed_unload; 310 goto failed_unload; 334 } 311 } 335 sb->s_flags &= ~SB_RDONLY; !! 312 sb->s_flags &= ~MS_RDONLY; 336 } else if (nilfs_test_opt(nilfs, NOREC 313 } else if (nilfs_test_opt(nilfs, NORECOVERY)) { 337 nilfs_err(sb, !! 314 nilfs_msg(sb, KERN_ERR, 338 "recovery cancelled 315 "recovery cancelled because norecovery option was specified for a read/write mount"); 339 err = -EINVAL; 316 err = -EINVAL; 340 goto failed_unload; 317 goto failed_unload; 341 } 318 } 342 319 343 err = nilfs_salvage_orphan_logs(nilfs, 320 err = nilfs_salvage_orphan_logs(nilfs, sb, &ri); 344 if (err) 321 if (err) 345 goto failed_unload; 322 goto failed_unload; 346 323 347 down_write(&nilfs->ns_sem); 324 down_write(&nilfs->ns_sem); 348 nilfs->ns_mount_state |= NILFS_VALID_F 325 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */ 349 err = nilfs_cleanup_super(sb); 326 err = nilfs_cleanup_super(sb); 350 up_write(&nilfs->ns_sem); 327 up_write(&nilfs->ns_sem); 351 328 352 if (err) { 329 if (err) { 353 nilfs_err(sb, !! 330 nilfs_msg(sb, KERN_ERR, 354 "error %d updating s 331 "error %d updating super block. recovery unfinished.", 355 err); 332 err); 356 goto failed_unload; 333 goto failed_unload; 357 } 334 } 358 nilfs_info(sb, "recovery complete"); !! 335 nilfs_msg(sb, KERN_INFO, "recovery complete"); 359 336 360 skip_recovery: 337 skip_recovery: 361 nilfs_clear_recovery_info(&ri); 338 nilfs_clear_recovery_info(&ri); 362 sb->s_flags = s_flags; 339 sb->s_flags = s_flags; 363 return 0; 340 return 0; 364 341 365 scan_error: 342 scan_error: 366 nilfs_err(sb, "error %d while searchin !! 343 nilfs_msg(sb, KERN_ERR, "error %d while searching super root", err); 367 goto failed; 344 goto failed; 368 345 369 failed_unload: 346 failed_unload: 370 nilfs_sysfs_delete_device_group(nilfs) << 371 << 372 sysfs_error: << 373 iput(nilfs->ns_cpfile); 347 iput(nilfs->ns_cpfile); 374 iput(nilfs->ns_sufile); 348 iput(nilfs->ns_sufile); 375 iput(nilfs->ns_dat); 349 iput(nilfs->ns_dat); 376 350 377 failed: 351 failed: 378 nilfs_clear_recovery_info(&ri); 352 nilfs_clear_recovery_info(&ri); 379 sb->s_flags = s_flags; 353 sb->s_flags = s_flags; 380 return err; 354 return err; 381 } 355 } 382 356 383 static unsigned long long nilfs_max_size(unsig 357 static unsigned long long nilfs_max_size(unsigned int blkbits) 384 { 358 { 385 unsigned int max_bits; 359 unsigned int max_bits; 386 unsigned long long res = MAX_LFS_FILES 360 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */ 387 361 388 max_bits = blkbits + NILFS_BMAP_KEY_BI 362 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */ 389 if (max_bits < 64) 363 if (max_bits < 64) 390 res = min_t(unsigned long long 364 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1); 391 return res; 365 return res; 392 } 366 } 393 367 394 /** 368 /** 395 * nilfs_nrsvsegs - calculate the number of re 369 * nilfs_nrsvsegs - calculate the number of reserved segments 396 * @nilfs: nilfs object 370 * @nilfs: nilfs object 397 * @nsegs: total number of segments 371 * @nsegs: total number of segments 398 */ 372 */ 399 unsigned long nilfs_nrsvsegs(struct the_nilfs 373 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs) 400 { 374 { 401 return max_t(unsigned long, NILFS_MIN_ 375 return max_t(unsigned long, NILFS_MIN_NRSVSEGS, 402 DIV_ROUND_UP(nsegs * nilf 376 DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage, 403 100)); 377 100)); 404 } 378 } 405 379 406 /** << 407 * nilfs_max_segment_count - calculate the max << 408 * @nilfs: nilfs object << 409 */ << 410 static u64 nilfs_max_segment_count(struct the_ << 411 { << 412 u64 max_count = U64_MAX; << 413 << 414 max_count = div64_ul(max_count, nilfs- << 415 return min_t(u64, max_count, ULONG_MAX << 416 } << 417 << 418 void nilfs_set_nsegments(struct the_nilfs *nil 380 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs) 419 { 381 { 420 nilfs->ns_nsegments = nsegs; 382 nilfs->ns_nsegments = nsegs; 421 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(ni 383 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs); 422 } 384 } 423 385 424 static int nilfs_store_disk_layout(struct the_ 386 static int nilfs_store_disk_layout(struct the_nilfs *nilfs, 425 struct nilf 387 struct nilfs_super_block *sbp) 426 { 388 { 427 u64 nsegments, nblocks; << 428 << 429 if (le32_to_cpu(sbp->s_rev_level) < NI 389 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) { 430 nilfs_err(nilfs->ns_sb, !! 390 nilfs_msg(nilfs->ns_sb, KERN_ERR, 431 "unsupported revisio 391 "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).", 432 le32_to_cpu(sbp->s_r 392 le32_to_cpu(sbp->s_rev_level), 433 le16_to_cpu(sbp->s_m 393 le16_to_cpu(sbp->s_minor_rev_level), 434 NILFS_CURRENT_REV, N 394 NILFS_CURRENT_REV, NILFS_MINOR_REV); 435 return -EINVAL; 395 return -EINVAL; 436 } 396 } 437 nilfs->ns_sbsize = le16_to_cpu(sbp->s_ 397 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); 438 if (nilfs->ns_sbsize > BLOCK_SIZE) 398 if (nilfs->ns_sbsize > BLOCK_SIZE) 439 return -EINVAL; 399 return -EINVAL; 440 400 441 nilfs->ns_inode_size = le16_to_cpu(sbp 401 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size); 442 if (nilfs->ns_inode_size > nilfs->ns_b 402 if (nilfs->ns_inode_size > nilfs->ns_blocksize) { 443 nilfs_err(nilfs->ns_sb, "too l !! 403 nilfs_msg(nilfs->ns_sb, KERN_ERR, >> 404 "too large inode size: %d bytes", 444 nilfs->ns_inode_size 405 nilfs->ns_inode_size); 445 return -EINVAL; 406 return -EINVAL; 446 } else if (nilfs->ns_inode_size < NILF 407 } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) { 447 nilfs_err(nilfs->ns_sb, "too s !! 408 nilfs_msg(nilfs->ns_sb, KERN_ERR, >> 409 "too small inode size: %d bytes", 448 nilfs->ns_inode_size 410 nilfs->ns_inode_size); 449 return -EINVAL; 411 return -EINVAL; 450 } 412 } 451 413 452 nilfs->ns_first_ino = le32_to_cpu(sbp- 414 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino); 453 if (nilfs->ns_first_ino < NILFS_USER_I << 454 nilfs_err(nilfs->ns_sb, << 455 "too small lower lim << 456 nilfs->ns_first_ino) << 457 return -EINVAL; << 458 } << 459 415 460 nilfs->ns_blocks_per_segment = le32_to 416 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment); 461 if (nilfs->ns_blocks_per_segment < NIL 417 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) { 462 nilfs_err(nilfs->ns_sb, "too s !! 418 nilfs_msg(nilfs->ns_sb, KERN_ERR, >> 419 "too short segment: %lu blocks", 463 nilfs->ns_blocks_per 420 nilfs->ns_blocks_per_segment); 464 return -EINVAL; 421 return -EINVAL; 465 } 422 } 466 423 467 nilfs->ns_first_data_block = le64_to_c 424 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block); 468 nilfs->ns_r_segments_percentage = 425 nilfs->ns_r_segments_percentage = 469 le32_to_cpu(sbp->s_r_segments_ 426 le32_to_cpu(sbp->s_r_segments_percentage); 470 if (nilfs->ns_r_segments_percentage < 427 if (nilfs->ns_r_segments_percentage < 1 || 471 nilfs->ns_r_segments_percentage > 428 nilfs->ns_r_segments_percentage > 99) { 472 nilfs_err(nilfs->ns_sb, !! 429 nilfs_msg(nilfs->ns_sb, KERN_ERR, 473 "invalid reserved se 430 "invalid reserved segments percentage: %lu", 474 nilfs->ns_r_segments 431 nilfs->ns_r_segments_percentage); 475 return -EINVAL; 432 return -EINVAL; 476 } 433 } 477 434 478 nsegments = le64_to_cpu(sbp->s_nsegmen !! 435 nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments)); 479 if (nsegments > nilfs_max_segment_coun << 480 nilfs_err(nilfs->ns_sb, << 481 "segment count %llu << 482 (unsigned long long) << 483 (unsigned long long) << 484 return -EINVAL; << 485 } << 486 << 487 nblocks = sb_bdev_nr_blocks(nilfs->ns_ << 488 if (nblocks) { << 489 u64 min_block_count = nsegment << 490 /* << 491 * To avoid failing to mount e << 492 * second superblock, exclude << 493 * "min_block_count" calculati << 494 */ << 495 << 496 if (nblocks < min_block_count) << 497 nilfs_err(nilfs->ns_sb << 498 "total numbe << 499 (unsigned lo << 500 (unsigned lo << 501 return -EINVAL; << 502 } << 503 } << 504 << 505 nilfs_set_nsegments(nilfs, nsegments); << 506 nilfs->ns_crc_seed = le32_to_cpu(sbp-> 436 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed); 507 return 0; 437 return 0; 508 } 438 } 509 439 510 static int nilfs_valid_sb(struct nilfs_super_b 440 static int nilfs_valid_sb(struct nilfs_super_block *sbp) 511 { 441 { 512 static unsigned char sum[4]; 442 static unsigned char sum[4]; 513 const int sumoff = offsetof(struct nil 443 const int sumoff = offsetof(struct nilfs_super_block, s_sum); 514 size_t bytes; 444 size_t bytes; 515 u32 crc; 445 u32 crc; 516 446 517 if (!sbp || le16_to_cpu(sbp->s_magic) 447 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC) 518 return 0; 448 return 0; 519 bytes = le16_to_cpu(sbp->s_bytes); 449 bytes = le16_to_cpu(sbp->s_bytes); 520 if (bytes < sumoff + 4 || bytes > BLOC 450 if (bytes < sumoff + 4 || bytes > BLOCK_SIZE) 521 return 0; 451 return 0; 522 crc = crc32_le(le32_to_cpu(sbp->s_crc_ 452 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp, 523 sumoff); 453 sumoff); 524 crc = crc32_le(crc, sum, 4); 454 crc = crc32_le(crc, sum, 4); 525 crc = crc32_le(crc, (unsigned char *)s 455 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4, 526 bytes - sumoff - 4); 456 bytes - sumoff - 4); 527 return crc == le32_to_cpu(sbp->s_sum); 457 return crc == le32_to_cpu(sbp->s_sum); 528 } 458 } 529 459 530 /** !! 460 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset) 531 * nilfs_sb2_bad_offset - check the location o << 532 * @sbp: superblock raw data buffer << 533 * @offset: byte offset of second superblock c << 534 * << 535 * nilfs_sb2_bad_offset() checks if the positi << 536 * superblock is valid or not based on the fil << 537 * stored in @sbp. If @offset points to a loc << 538 * area, or if the parameters themselves are n << 539 * determined to be invalid. << 540 * << 541 * Return Value: true if invalid, false if val << 542 */ << 543 static bool nilfs_sb2_bad_offset(struct nilfs_ << 544 { 461 { 545 unsigned int shift_bits = le32_to_cpu( !! 462 return offset < ((le64_to_cpu(sbp->s_nsegments) * 546 u32 blocks_per_segment = le32_to_cpu(s !! 463 le32_to_cpu(sbp->s_blocks_per_segment)) << 547 u64 nsegments = le64_to_cpu(sbp->s_nse !! 464 (le32_to_cpu(sbp->s_log_block_size) + 10)); 548 u64 index; << 549 << 550 if (blocks_per_segment < NILFS_SEG_MIN << 551 shift_bits > ilog2(NILFS_MAX_BLOCK << 552 return true; << 553 << 554 index = offset >> (shift_bits + BLOCK_ << 555 do_div(index, blocks_per_segment); << 556 return index < nsegments; << 557 } 465 } 558 466 559 static void nilfs_release_super_block(struct t 467 static void nilfs_release_super_block(struct the_nilfs *nilfs) 560 { 468 { 561 int i; 469 int i; 562 470 563 for (i = 0; i < 2; i++) { 471 for (i = 0; i < 2; i++) { 564 if (nilfs->ns_sbp[i]) { 472 if (nilfs->ns_sbp[i]) { 565 brelse(nilfs->ns_sbh[i 473 brelse(nilfs->ns_sbh[i]); 566 nilfs->ns_sbh[i] = NUL 474 nilfs->ns_sbh[i] = NULL; 567 nilfs->ns_sbp[i] = NUL 475 nilfs->ns_sbp[i] = NULL; 568 } 476 } 569 } 477 } 570 } 478 } 571 479 572 void nilfs_fall_back_super_block(struct the_ni 480 void nilfs_fall_back_super_block(struct the_nilfs *nilfs) 573 { 481 { 574 brelse(nilfs->ns_sbh[0]); 482 brelse(nilfs->ns_sbh[0]); 575 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 483 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 576 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 484 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 577 nilfs->ns_sbh[1] = NULL; 485 nilfs->ns_sbh[1] = NULL; 578 nilfs->ns_sbp[1] = NULL; 486 nilfs->ns_sbp[1] = NULL; 579 } 487 } 580 488 581 void nilfs_swap_super_block(struct the_nilfs * 489 void nilfs_swap_super_block(struct the_nilfs *nilfs) 582 { 490 { 583 struct buffer_head *tsbh = nilfs->ns_s 491 struct buffer_head *tsbh = nilfs->ns_sbh[0]; 584 struct nilfs_super_block *tsbp = nilfs 492 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0]; 585 493 586 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 494 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 587 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 495 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 588 nilfs->ns_sbh[1] = tsbh; 496 nilfs->ns_sbh[1] = tsbh; 589 nilfs->ns_sbp[1] = tsbp; 497 nilfs->ns_sbp[1] = tsbp; 590 } 498 } 591 499 592 static int nilfs_load_super_block(struct the_n 500 static int nilfs_load_super_block(struct the_nilfs *nilfs, 593 struct super 501 struct super_block *sb, int blocksize, 594 struct nilfs 502 struct nilfs_super_block **sbpp) 595 { 503 { 596 struct nilfs_super_block **sbp = nilfs 504 struct nilfs_super_block **sbp = nilfs->ns_sbp; 597 struct buffer_head **sbh = nilfs->ns_s 505 struct buffer_head **sbh = nilfs->ns_sbh; 598 u64 sb2off, devsize = bdev_nr_bytes(ni !! 506 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); 599 int valid[2], swp = 0, older; !! 507 int valid[2], swp = 0; 600 << 601 if (devsize < NILFS_SEG_MIN_BLOCKS * N << 602 nilfs_err(sb, "device size too << 603 return -EINVAL; << 604 } << 605 sb2off = NILFS_SB2_OFFSET_BYTES(devsiz << 606 508 607 sbp[0] = nilfs_read_super_block(sb, NI 509 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, 608 &sbh[0 510 &sbh[0]); 609 sbp[1] = nilfs_read_super_block(sb, sb 511 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); 610 512 611 if (!sbp[0]) { 513 if (!sbp[0]) { 612 if (!sbp[1]) { 514 if (!sbp[1]) { 613 nilfs_err(sb, "unable !! 515 nilfs_msg(sb, KERN_ERR, "unable to read superblock"); 614 return -EIO; 516 return -EIO; 615 } 517 } 616 nilfs_warn(sb, !! 518 nilfs_msg(sb, KERN_WARNING, 617 "unable to read pri !! 519 "unable to read primary superblock (blocksize = %d)", 618 blocksize); !! 520 blocksize); 619 } else if (!sbp[1]) { 521 } else if (!sbp[1]) { 620 nilfs_warn(sb, !! 522 nilfs_msg(sb, KERN_WARNING, 621 "unable to read sec !! 523 "unable to read secondary superblock (blocksize = %d)", 622 blocksize); !! 524 blocksize); 623 } 525 } 624 526 625 /* 527 /* 626 * Compare two super blocks and set 1 528 * Compare two super blocks and set 1 in swp if the secondary 627 * super block is valid and newer. Ot 529 * super block is valid and newer. Otherwise, set 0 in swp. 628 */ 530 */ 629 valid[0] = nilfs_valid_sb(sbp[0]); 531 valid[0] = nilfs_valid_sb(sbp[0]); 630 valid[1] = nilfs_valid_sb(sbp[1]); 532 valid[1] = nilfs_valid_sb(sbp[1]); 631 swp = valid[1] && (!valid[0] || 533 swp = valid[1] && (!valid[0] || 632 le64_to_cpu(sbp[1]- 534 le64_to_cpu(sbp[1]->s_last_cno) > 633 le64_to_cpu(sbp[0]- 535 le64_to_cpu(sbp[0]->s_last_cno)); 634 536 635 if (valid[swp] && nilfs_sb2_bad_offset 537 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { 636 brelse(sbh[1]); 538 brelse(sbh[1]); 637 sbh[1] = NULL; 539 sbh[1] = NULL; 638 sbp[1] = NULL; 540 sbp[1] = NULL; 639 valid[1] = 0; 541 valid[1] = 0; 640 swp = 0; 542 swp = 0; 641 } 543 } 642 if (!valid[swp]) { 544 if (!valid[swp]) { 643 nilfs_release_super_block(nilf 545 nilfs_release_super_block(nilfs); 644 nilfs_err(sb, "couldn't find n !! 546 nilfs_msg(sb, KERN_ERR, "couldn't find nilfs on the device"); 645 return -EINVAL; 547 return -EINVAL; 646 } 548 } 647 549 648 if (!valid[!swp]) 550 if (!valid[!swp]) 649 nilfs_warn(sb, !! 551 nilfs_msg(sb, KERN_WARNING, 650 "broken superblock, !! 552 "broken superblock, retrying with spare superblock (blocksize = %d)", 651 blocksize); !! 553 blocksize); 652 if (swp) 554 if (swp) 653 nilfs_swap_super_block(nilfs); 555 nilfs_swap_super_block(nilfs); 654 556 655 /* << 656 * Calculate the array index of the ol << 657 * If one has been dropped, set index << 658 * otherwise set index 1 pointing to t << 659 * are the same). << 660 * << 661 * Divided case valid[0] << 662 * ---------------------------------- << 663 * Both SBs are invalid 0 << 664 * SB1 is invalid 0 << 665 * SB2 is invalid 1 << 666 * SB2 is newer 1 << 667 * SB2 is older or the same 1 << 668 */ << 669 older = valid[1] ^ swp; << 670 << 671 nilfs->ns_sbwcount = 0; 557 nilfs->ns_sbwcount = 0; 672 nilfs->ns_sbwtime = le64_to_cpu(sbp[0] 558 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); 673 nilfs->ns_prot_seq = le64_to_cpu(sbp[o !! 559 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); 674 *sbpp = sbp[0]; 560 *sbpp = sbp[0]; 675 return 0; 561 return 0; 676 } 562 } 677 563 678 /** 564 /** 679 * init_nilfs - initialize a NILFS instance. 565 * init_nilfs - initialize a NILFS instance. 680 * @nilfs: the_nilfs structure 566 * @nilfs: the_nilfs structure 681 * @sb: super block 567 * @sb: super block >> 568 * @data: mount options 682 * 569 * 683 * init_nilfs() performs common initialization 570 * init_nilfs() performs common initialization per block device (e.g. 684 * reading the super block, getting disk layou 571 * reading the super block, getting disk layout information, initializing 685 * shared fields in the_nilfs). 572 * shared fields in the_nilfs). 686 * 573 * 687 * Return Value: On success, 0 is returned. On 574 * Return Value: On success, 0 is returned. On error, a negative error 688 * code is returned. 575 * code is returned. 689 */ 576 */ 690 int init_nilfs(struct the_nilfs *nilfs, struct !! 577 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data) 691 { 578 { 692 struct nilfs_super_block *sbp; 579 struct nilfs_super_block *sbp; 693 int blocksize; 580 int blocksize; 694 int err; 581 int err; 695 582 696 down_write(&nilfs->ns_sem); 583 down_write(&nilfs->ns_sem); 697 584 698 blocksize = sb_min_blocksize(sb, NILFS 585 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE); 699 if (!blocksize) { 586 if (!blocksize) { 700 nilfs_err(sb, "unable to set b !! 587 nilfs_msg(sb, KERN_ERR, "unable to set blocksize"); 701 err = -EINVAL; 588 err = -EINVAL; 702 goto out; 589 goto out; 703 } 590 } 704 err = nilfs_load_super_block(nilfs, sb 591 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); 705 if (err) 592 if (err) 706 goto out; 593 goto out; 707 594 708 err = nilfs_store_magic(sb, sbp); !! 595 err = nilfs_store_magic_and_option(sb, sbp, data); 709 if (err) 596 if (err) 710 goto failed_sbh; 597 goto failed_sbh; 711 598 712 err = nilfs_check_feature_compatibilit 599 err = nilfs_check_feature_compatibility(sb, sbp); 713 if (err) 600 if (err) 714 goto failed_sbh; 601 goto failed_sbh; 715 602 716 err = nilfs_get_blocksize(sb, sbp, &bl !! 603 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); 717 if (err) !! 604 if (blocksize < NILFS_MIN_BLOCK_SIZE || 718 goto failed_sbh; !! 605 blocksize > NILFS_MAX_BLOCK_SIZE) { 719 !! 606 nilfs_msg(sb, KERN_ERR, 720 if (blocksize < NILFS_MIN_BLOCK_SIZE) << 721 nilfs_err(sb, << 722 "couldn't mount beca 607 "couldn't mount because of unsupported filesystem blocksize %d", 723 blocksize); 608 blocksize); 724 err = -EINVAL; 609 err = -EINVAL; 725 goto failed_sbh; 610 goto failed_sbh; 726 } 611 } 727 if (sb->s_blocksize != blocksize) { 612 if (sb->s_blocksize != blocksize) { 728 int hw_blocksize = bdev_logica 613 int hw_blocksize = bdev_logical_block_size(sb->s_bdev); 729 614 730 if (blocksize < hw_blocksize) 615 if (blocksize < hw_blocksize) { 731 nilfs_err(sb, !! 616 nilfs_msg(sb, KERN_ERR, 732 "blocksize % 617 "blocksize %d too small for device (sector-size = %d)", 733 blocksize, h 618 blocksize, hw_blocksize); 734 err = -EINVAL; 619 err = -EINVAL; 735 goto failed_sbh; 620 goto failed_sbh; 736 } 621 } 737 nilfs_release_super_block(nilf 622 nilfs_release_super_block(nilfs); 738 if (!sb_set_blocksize(sb, bloc !! 623 sb_set_blocksize(sb, blocksize); 739 nilfs_err(sb, "bad blo << 740 err = -EINVAL; << 741 goto out; << 742 } << 743 624 744 err = nilfs_load_super_block(n 625 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); 745 if (err) 626 if (err) 746 goto out; 627 goto out; 747 /* 628 /* 748 * Not to failed_sbh; 629 * Not to failed_sbh; sbh is released automatically 749 * when reloading fail 630 * when reloading fails. 750 */ 631 */ 751 } 632 } 752 nilfs->ns_blocksize_bits = sb->s_block 633 nilfs->ns_blocksize_bits = sb->s_blocksize_bits; 753 nilfs->ns_blocksize = blocksize; 634 nilfs->ns_blocksize = blocksize; 754 635 >> 636 get_random_bytes(&nilfs->ns_next_generation, >> 637 sizeof(nilfs->ns_next_generation)); >> 638 755 err = nilfs_store_disk_layout(nilfs, s 639 err = nilfs_store_disk_layout(nilfs, sbp); 756 if (err) 640 if (err) 757 goto failed_sbh; 641 goto failed_sbh; 758 642 759 sb->s_maxbytes = nilfs_max_size(sb->s_ 643 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); 760 644 761 nilfs->ns_mount_state = le16_to_cpu(sb 645 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); 762 646 763 err = nilfs_store_log_cursor(nilfs, sb 647 err = nilfs_store_log_cursor(nilfs, sbp); 764 if (err) 648 if (err) 765 goto failed_sbh; 649 goto failed_sbh; 766 650 >> 651 err = nilfs_sysfs_create_device_group(sb); >> 652 if (err) >> 653 goto failed_sbh; >> 654 767 set_nilfs_init(nilfs); 655 set_nilfs_init(nilfs); 768 err = 0; 656 err = 0; 769 out: 657 out: 770 up_write(&nilfs->ns_sem); 658 up_write(&nilfs->ns_sem); 771 return err; 659 return err; 772 660 773 failed_sbh: 661 failed_sbh: 774 nilfs_release_super_block(nilfs); 662 nilfs_release_super_block(nilfs); 775 goto out; 663 goto out; 776 } 664 } 777 665 778 int nilfs_discard_segments(struct the_nilfs *n 666 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump, 779 size_t nsegs) 667 size_t nsegs) 780 { 668 { 781 sector_t seg_start, seg_end; 669 sector_t seg_start, seg_end; 782 sector_t start = 0, nblocks = 0; 670 sector_t start = 0, nblocks = 0; 783 unsigned int sects_per_block; 671 unsigned int sects_per_block; 784 __u64 *sn; 672 __u64 *sn; 785 int ret = 0; 673 int ret = 0; 786 674 787 sects_per_block = (1 << nilfs->ns_bloc 675 sects_per_block = (1 << nilfs->ns_blocksize_bits) / 788 bdev_logical_block_size(nilfs- 676 bdev_logical_block_size(nilfs->ns_bdev); 789 for (sn = segnump; sn < segnump + nseg 677 for (sn = segnump; sn < segnump + nsegs; sn++) { 790 nilfs_get_segment_range(nilfs, 678 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end); 791 679 792 if (!nblocks) { 680 if (!nblocks) { 793 start = seg_start; 681 start = seg_start; 794 nblocks = seg_end - se 682 nblocks = seg_end - seg_start + 1; 795 } else if (start + nblocks == 683 } else if (start + nblocks == seg_start) { 796 nblocks += seg_end - s 684 nblocks += seg_end - seg_start + 1; 797 } else { 685 } else { 798 ret = blkdev_issue_dis 686 ret = blkdev_issue_discard(nilfs->ns_bdev, 799 687 start * sects_per_block, 800 688 nblocks * sects_per_block, 801 !! 689 GFP_NOFS, 0); 802 if (ret < 0) 690 if (ret < 0) 803 return ret; 691 return ret; 804 nblocks = 0; 692 nblocks = 0; 805 } 693 } 806 } 694 } 807 if (nblocks) 695 if (nblocks) 808 ret = blkdev_issue_discard(nil 696 ret = blkdev_issue_discard(nilfs->ns_bdev, 809 sta 697 start * sects_per_block, 810 nbl 698 nblocks * sects_per_block, 811 GFP !! 699 GFP_NOFS, 0); 812 return ret; 700 return ret; 813 } 701 } 814 702 815 int nilfs_count_free_blocks(struct the_nilfs * 703 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) 816 { 704 { 817 unsigned long ncleansegs; 705 unsigned long ncleansegs; 818 706 >> 707 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 819 ncleansegs = nilfs_sufile_get_ncleanse 708 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); >> 709 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 820 *nblocks = (sector_t)ncleansegs * nilf 710 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; 821 return 0; 711 return 0; 822 } 712 } 823 713 824 int nilfs_near_disk_full(struct the_nilfs *nil 714 int nilfs_near_disk_full(struct the_nilfs *nilfs) 825 { 715 { 826 unsigned long ncleansegs, nincsegs; 716 unsigned long ncleansegs, nincsegs; 827 717 828 ncleansegs = nilfs_sufile_get_ncleanse 718 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); 829 nincsegs = atomic_read(&nilfs->ns_ndir 719 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / 830 nilfs->ns_blocks_per_segment + 720 nilfs->ns_blocks_per_segment + 1; 831 721 832 return ncleansegs <= nilfs->ns_nrsvseg 722 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs; 833 } 723 } 834 724 835 struct nilfs_root *nilfs_lookup_root(struct th 725 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno) 836 { 726 { 837 struct rb_node *n; 727 struct rb_node *n; 838 struct nilfs_root *root; 728 struct nilfs_root *root; 839 729 840 spin_lock(&nilfs->ns_cptree_lock); 730 spin_lock(&nilfs->ns_cptree_lock); 841 n = nilfs->ns_cptree.rb_node; 731 n = nilfs->ns_cptree.rb_node; 842 while (n) { 732 while (n) { 843 root = rb_entry(n, struct nilf 733 root = rb_entry(n, struct nilfs_root, rb_node); 844 734 845 if (cno < root->cno) { 735 if (cno < root->cno) { 846 n = n->rb_left; 736 n = n->rb_left; 847 } else if (cno > root->cno) { 737 } else if (cno > root->cno) { 848 n = n->rb_right; 738 n = n->rb_right; 849 } else { 739 } else { 850 refcount_inc(&root->co !! 740 atomic_inc(&root->count); 851 spin_unlock(&nilfs->ns 741 spin_unlock(&nilfs->ns_cptree_lock); 852 return root; 742 return root; 853 } 743 } 854 } 744 } 855 spin_unlock(&nilfs->ns_cptree_lock); 745 spin_unlock(&nilfs->ns_cptree_lock); 856 746 857 return NULL; 747 return NULL; 858 } 748 } 859 749 860 struct nilfs_root * 750 struct nilfs_root * 861 nilfs_find_or_create_root(struct the_nilfs *ni 751 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) 862 { 752 { 863 struct rb_node **p, *parent; 753 struct rb_node **p, *parent; 864 struct nilfs_root *root, *new; 754 struct nilfs_root *root, *new; 865 int err; 755 int err; 866 756 867 root = nilfs_lookup_root(nilfs, cno); 757 root = nilfs_lookup_root(nilfs, cno); 868 if (root) 758 if (root) 869 return root; 759 return root; 870 760 871 new = kzalloc(sizeof(*root), GFP_KERNE 761 new = kzalloc(sizeof(*root), GFP_KERNEL); 872 if (!new) 762 if (!new) 873 return NULL; 763 return NULL; 874 764 875 spin_lock(&nilfs->ns_cptree_lock); 765 spin_lock(&nilfs->ns_cptree_lock); 876 766 877 p = &nilfs->ns_cptree.rb_node; 767 p = &nilfs->ns_cptree.rb_node; 878 parent = NULL; 768 parent = NULL; 879 769 880 while (*p) { 770 while (*p) { 881 parent = *p; 771 parent = *p; 882 root = rb_entry(parent, struct 772 root = rb_entry(parent, struct nilfs_root, rb_node); 883 773 884 if (cno < root->cno) { 774 if (cno < root->cno) { 885 p = &(*p)->rb_left; 775 p = &(*p)->rb_left; 886 } else if (cno > root->cno) { 776 } else if (cno > root->cno) { 887 p = &(*p)->rb_right; 777 p = &(*p)->rb_right; 888 } else { 778 } else { 889 refcount_inc(&root->co !! 779 atomic_inc(&root->count); 890 spin_unlock(&nilfs->ns 780 spin_unlock(&nilfs->ns_cptree_lock); 891 kfree(new); 781 kfree(new); 892 return root; 782 return root; 893 } 783 } 894 } 784 } 895 785 896 new->cno = cno; 786 new->cno = cno; 897 new->ifile = NULL; 787 new->ifile = NULL; 898 new->nilfs = nilfs; 788 new->nilfs = nilfs; 899 refcount_set(&new->count, 1); !! 789 atomic_set(&new->count, 1); 900 atomic64_set(&new->inodes_count, 0); 790 atomic64_set(&new->inodes_count, 0); 901 atomic64_set(&new->blocks_count, 0); 791 atomic64_set(&new->blocks_count, 0); 902 792 903 rb_link_node(&new->rb_node, parent, p) 793 rb_link_node(&new->rb_node, parent, p); 904 rb_insert_color(&new->rb_node, &nilfs- 794 rb_insert_color(&new->rb_node, &nilfs->ns_cptree); 905 795 906 spin_unlock(&nilfs->ns_cptree_lock); 796 spin_unlock(&nilfs->ns_cptree_lock); 907 797 908 err = nilfs_sysfs_create_snapshot_grou 798 err = nilfs_sysfs_create_snapshot_group(new); 909 if (err) { 799 if (err) { 910 kfree(new); 800 kfree(new); 911 new = NULL; 801 new = NULL; 912 } 802 } 913 803 914 return new; 804 return new; 915 } 805 } 916 806 917 void nilfs_put_root(struct nilfs_root *root) 807 void nilfs_put_root(struct nilfs_root *root) 918 { 808 { 919 struct the_nilfs *nilfs = root->nilfs; !! 809 if (atomic_dec_and_test(&root->count)) { >> 810 struct the_nilfs *nilfs = root->nilfs; 920 811 921 if (refcount_dec_and_lock(&root->count !! 812 nilfs_sysfs_delete_snapshot_group(root); >> 813 >> 814 spin_lock(&nilfs->ns_cptree_lock); 922 rb_erase(&root->rb_node, &nilf 815 rb_erase(&root->rb_node, &nilfs->ns_cptree); 923 spin_unlock(&nilfs->ns_cptree_ 816 spin_unlock(&nilfs->ns_cptree_lock); 924 << 925 nilfs_sysfs_delete_snapshot_gr << 926 iput(root->ifile); 817 iput(root->ifile); 927 818 928 kfree(root); 819 kfree(root); 929 } 820 } 930 } 821 } 931 822
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.