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> 28 #include <linux/crc32.h> 29 #include "nilfs.h" 30 #include "segment.h" 31 #include "alloc.h" 32 #include "cpfile.h" 33 #include "sufile.h" 34 #include "dat.h" 35 #include "segbuf.h" 36 37 38 static LIST_HEAD(nilfs_objects); 39 static DEFINE_SPINLOCK(nilfs_lock); 40 41 void nilfs_set_last_segment(struct the_nilfs *nilfs, 42 sector_t start_blocknr, u64 seq, __u64 cno) 43 { 44 spin_lock(&nilfs->ns_last_segment_lock); 45 nilfs->ns_last_pseg = start_blocknr; 46 nilfs->ns_last_seq = seq; 47 nilfs->ns_last_cno = cno; 48 spin_unlock(&nilfs->ns_last_segment_lock); 49 } 50 51 /** 52 * alloc_nilfs - allocate the_nilfs structure 53 * @bdev: block device to which the_nilfs is related 54 * 55 * alloc_nilfs() allocates memory for the_nilfs and 56 * initializes its reference count and locks. 57 * 58 * Return Value: On success, pointer to the_nilfs is returned. 59 * On error, NULL is returned. 60 */ 61 static struct the_nilfs *alloc_nilfs(struct block_device *bdev) 62 { 63 struct the_nilfs *nilfs; 64 65 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL); 66 if (!nilfs) 67 return NULL; 68 69 nilfs->ns_bdev = bdev; 70 atomic_set(&nilfs->ns_count, 1); 71 atomic_set(&nilfs->ns_ndirtyblks, 0); 72 init_rwsem(&nilfs->ns_sem); 73 init_rwsem(&nilfs->ns_super_sem); 74 mutex_init(&nilfs->ns_mount_mutex); 75 init_rwsem(&nilfs->ns_writer_sem); 76 INIT_LIST_HEAD(&nilfs->ns_list); 77 INIT_LIST_HEAD(&nilfs->ns_supers); 78 spin_lock_init(&nilfs->ns_last_segment_lock); 79 nilfs->ns_gc_inodes_h = NULL; 80 init_rwsem(&nilfs->ns_segctor_sem); 81 82 return nilfs; 83 } 84 85 /** 86 * find_or_create_nilfs - find or create nilfs object 87 * @bdev: block device to which the_nilfs is related 88 * 89 * find_nilfs() looks up an existent nilfs object created on the 90 * device and gets the reference count of the object. If no nilfs object 91 * is found on the device, a new nilfs object is allocated. 92 * 93 * Return Value: On success, pointer to the nilfs object is returned. 94 * On error, NULL is returned. 95 */ 96 struct the_nilfs *find_or_create_nilfs(struct block_device *bdev) 97 { 98 struct the_nilfs *nilfs, *new = NULL; 99 100 retry: 101 spin_lock(&nilfs_lock); 102 list_for_each_entry(nilfs, &nilfs_objects, ns_list) { 103 if (nilfs->ns_bdev == bdev) { 104 get_nilfs(nilfs); 105 spin_unlock(&nilfs_lock); 106 if (new) 107 put_nilfs(new); 108 return nilfs; /* existing object */ 109 } 110 } 111 if (new) { 112 list_add_tail(&new->ns_list, &nilfs_objects); 113 spin_unlock(&nilfs_lock); 114 return new; /* new object */ 115 } 116 spin_unlock(&nilfs_lock); 117 118 new = alloc_nilfs(bdev); 119 if (new) 120 goto retry; 121 return NULL; /* insufficient memory */ 122 } 123 124 /** 125 * put_nilfs - release a reference to the_nilfs 126 * @nilfs: the_nilfs structure to be released 127 * 128 * put_nilfs() decrements a reference counter of the_nilfs. 129 * If the reference count reaches zero, the_nilfs is freed. 130 */ 131 void put_nilfs(struct the_nilfs *nilfs) 132 { 133 spin_lock(&nilfs_lock); 134 if (!atomic_dec_and_test(&nilfs->ns_count)) { 135 spin_unlock(&nilfs_lock); 136 return; 137 } 138 list_del_init(&nilfs->ns_list); 139 spin_unlock(&nilfs_lock); 140 141 /* 142 * Increment of ns_count never occurs below because the caller 143 * of get_nilfs() holds at least one reference to the_nilfs. 144 * Thus its exclusion control is not required here. 145 */ 146 147 might_sleep(); 148 if (nilfs_loaded(nilfs)) { 149 nilfs_mdt_destroy(nilfs->ns_sufile); 150 nilfs_mdt_destroy(nilfs->ns_cpfile); 151 nilfs_mdt_destroy(nilfs->ns_dat); 152 nilfs_mdt_destroy(nilfs->ns_gc_dat); 153 } 154 if (nilfs_init(nilfs)) { 155 nilfs_destroy_gccache(nilfs); 156 brelse(nilfs->ns_sbh[0]); 157 brelse(nilfs->ns_sbh[1]); 158 } 159 kfree(nilfs); 160 } 161 162 static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block) 163 { 164 struct buffer_head *bh_sr; 165 struct nilfs_super_root *raw_sr; 166 struct nilfs_super_block **sbp = nilfs->ns_sbp; 167 unsigned dat_entry_size, segment_usage_size, checkpoint_size; 168 unsigned inode_size; 169 int err; 170 171 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1); 172 if (unlikely(err)) 173 return err; 174 175 down_read(&nilfs->ns_sem); 176 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); 177 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); 178 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); 179 up_read(&nilfs->ns_sem); 180 181 inode_size = nilfs->ns_inode_size; 182 183 err = -ENOMEM; 184 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size); 185 if (unlikely(!nilfs->ns_dat)) 186 goto failed; 187 188 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size); 189 if (unlikely(!nilfs->ns_gc_dat)) 190 goto failed_dat; 191 192 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size); 193 if (unlikely(!nilfs->ns_cpfile)) 194 goto failed_gc_dat; 195 196 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size); 197 if (unlikely(!nilfs->ns_sufile)) 198 goto failed_cpfile; 199 200 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat); 201 202 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data + 203 NILFS_SR_DAT_OFFSET(inode_size)); 204 if (unlikely(err)) 205 goto failed_sufile; 206 207 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data + 208 NILFS_SR_CPFILE_OFFSET(inode_size)); 209 if (unlikely(err)) 210 goto failed_sufile; 211 212 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data + 213 NILFS_SR_SUFILE_OFFSET(inode_size)); 214 if (unlikely(err)) 215 goto failed_sufile; 216 217 raw_sr = (struct nilfs_super_root *)bh_sr->b_data; 218 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); 219 220 failed: 221 brelse(bh_sr); 222 return err; 223 224 failed_sufile: 225 nilfs_mdt_destroy(nilfs->ns_sufile); 226 227 failed_cpfile: 228 nilfs_mdt_destroy(nilfs->ns_cpfile); 229 230 failed_gc_dat: 231 nilfs_mdt_destroy(nilfs->ns_gc_dat); 232 233 failed_dat: 234 nilfs_mdt_destroy(nilfs->ns_dat); 235 goto failed; 236 } 237 238 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) 239 { 240 memset(ri, 0, sizeof(*ri)); 241 INIT_LIST_HEAD(&ri->ri_used_segments); 242 } 243 244 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) 245 { 246 nilfs_dispose_segment_list(&ri->ri_used_segments); 247 } 248 249 /** 250 * load_nilfs - load and recover the nilfs 251 * @nilfs: the_nilfs structure to be released 252 * @sbi: nilfs_sb_info used to recover past segment 253 * 254 * load_nilfs() searches and load the latest super root, 255 * attaches the last segment, and does recovery if needed. 256 * The caller must call this exclusively for simultaneous mounts. 257 */ 258 int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi) 259 { 260 struct nilfs_recovery_info ri; 261 unsigned int s_flags = sbi->s_super->s_flags; 262 int really_read_only = bdev_read_only(nilfs->ns_bdev); 263 int valid_fs = nilfs_valid_fs(nilfs); 264 int err; 265 266 if (nilfs_loaded(nilfs)) { 267 if (valid_fs || 268 ((s_flags & MS_RDONLY) && nilfs_test_opt(sbi, NORECOVERY))) 269 return 0; 270 printk(KERN_ERR "NILFS: the filesystem is in an incomplete " 271 "recovery state.\n"); 272 return -EINVAL; 273 } 274 275 if (!valid_fs) { 276 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n"); 277 if (s_flags & MS_RDONLY) { 278 printk(KERN_INFO "NILFS: INFO: recovery " 279 "required for readonly filesystem.\n"); 280 printk(KERN_INFO "NILFS: write access will " 281 "be enabled during recovery.\n"); 282 } 283 } 284 285 nilfs_init_recovery_info(&ri); 286 287 err = nilfs_search_super_root(nilfs, &ri); 288 if (unlikely(err)) { 289 printk(KERN_ERR "NILFS: error searching super root.\n"); 290 goto failed; 291 } 292 293 err = nilfs_load_super_root(nilfs, ri.ri_super_root); 294 if (unlikely(err)) { 295 printk(KERN_ERR "NILFS: error loading super root.\n"); 296 goto failed; 297 } 298 299 if (valid_fs) 300 goto skip_recovery; 301 302 if (s_flags & MS_RDONLY) { 303 if (nilfs_test_opt(sbi, NORECOVERY)) { 304 printk(KERN_INFO "NILFS: norecovery option specified. " 305 "skipping roll-forward recovery\n"); 306 goto skip_recovery; 307 } 308 if (really_read_only) { 309 printk(KERN_ERR "NILFS: write access " 310 "unavailable, cannot proceed.\n"); 311 err = -EROFS; 312 goto failed_unload; 313 } 314 sbi->s_super->s_flags &= ~MS_RDONLY; 315 } else if (nilfs_test_opt(sbi, NORECOVERY)) { 316 printk(KERN_ERR "NILFS: recovery cancelled because norecovery " 317 "option was specified for a read/write mount\n"); 318 err = -EINVAL; 319 goto failed_unload; 320 } 321 322 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri); 323 if (err) 324 goto failed_unload; 325 326 down_write(&nilfs->ns_sem); 327 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */ 328 err = nilfs_cleanup_super(sbi); 329 up_write(&nilfs->ns_sem); 330 331 if (err) { 332 printk(KERN_ERR "NILFS: failed to update super block. " 333 "recovery unfinished.\n"); 334 goto failed_unload; 335 } 336 printk(KERN_INFO "NILFS: recovery complete.\n"); 337 338 skip_recovery: 339 set_nilfs_loaded(nilfs); 340 nilfs_clear_recovery_info(&ri); 341 sbi->s_super->s_flags = s_flags; 342 return 0; 343 344 failed_unload: 345 nilfs_mdt_destroy(nilfs->ns_cpfile); 346 nilfs_mdt_destroy(nilfs->ns_sufile); 347 nilfs_mdt_destroy(nilfs->ns_dat); 348 349 failed: 350 nilfs_clear_recovery_info(&ri); 351 sbi->s_super->s_flags = s_flags; 352 return err; 353 } 354 355 static unsigned long long nilfs_max_size(unsigned int blkbits) 356 { 357 unsigned int max_bits; 358 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */ 359 360 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */ 361 if (max_bits < 64) 362 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1); 363 return res; 364 } 365 366 static int nilfs_store_disk_layout(struct the_nilfs *nilfs, 367 struct nilfs_super_block *sbp) 368 { 369 if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) { 370 printk(KERN_ERR "NILFS: revision mismatch " 371 "(superblock rev.=%d.%d, current rev.=%d.%d). " 372 "Please check the version of mkfs.nilfs.\n", 373 le32_to_cpu(sbp->s_rev_level), 374 le16_to_cpu(sbp->s_minor_rev_level), 375 NILFS_CURRENT_REV, NILFS_MINOR_REV); 376 return -EINVAL; 377 } 378 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); 379 if (nilfs->ns_sbsize > BLOCK_SIZE) 380 return -EINVAL; 381 382 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size); 383 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino); 384 385 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment); 386 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) { 387 printk(KERN_ERR "NILFS: too short segment.\n"); 388 return -EINVAL; 389 } 390 391 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block); 392 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments); 393 nilfs->ns_r_segments_percentage = 394 le32_to_cpu(sbp->s_r_segments_percentage); 395 nilfs->ns_nrsvsegs = 396 max_t(unsigned long, NILFS_MIN_NRSVSEGS, 397 DIV_ROUND_UP(nilfs->ns_nsegments * 398 nilfs->ns_r_segments_percentage, 100)); 399 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed); 400 return 0; 401 } 402 403 static int nilfs_valid_sb(struct nilfs_super_block *sbp) 404 { 405 static unsigned char sum[4]; 406 const int sumoff = offsetof(struct nilfs_super_block, s_sum); 407 size_t bytes; 408 u32 crc; 409 410 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC) 411 return 0; 412 bytes = le16_to_cpu(sbp->s_bytes); 413 if (bytes > BLOCK_SIZE) 414 return 0; 415 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp, 416 sumoff); 417 crc = crc32_le(crc, sum, 4); 418 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4, 419 bytes - sumoff - 4); 420 return crc == le32_to_cpu(sbp->s_sum); 421 } 422 423 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset) 424 { 425 return offset < ((le64_to_cpu(sbp->s_nsegments) * 426 le32_to_cpu(sbp->s_blocks_per_segment)) << 427 (le32_to_cpu(sbp->s_log_block_size) + 10)); 428 } 429 430 static void nilfs_release_super_block(struct the_nilfs *nilfs) 431 { 432 int i; 433 434 for (i = 0; i < 2; i++) { 435 if (nilfs->ns_sbp[i]) { 436 brelse(nilfs->ns_sbh[i]); 437 nilfs->ns_sbh[i] = NULL; 438 nilfs->ns_sbp[i] = NULL; 439 } 440 } 441 } 442 443 void nilfs_fall_back_super_block(struct the_nilfs *nilfs) 444 { 445 brelse(nilfs->ns_sbh[0]); 446 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 447 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 448 nilfs->ns_sbh[1] = NULL; 449 nilfs->ns_sbp[1] = NULL; 450 } 451 452 void nilfs_swap_super_block(struct the_nilfs *nilfs) 453 { 454 struct buffer_head *tsbh = nilfs->ns_sbh[0]; 455 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0]; 456 457 nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; 458 nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; 459 nilfs->ns_sbh[1] = tsbh; 460 nilfs->ns_sbp[1] = tsbp; 461 } 462 463 static int nilfs_load_super_block(struct the_nilfs *nilfs, 464 struct super_block *sb, int blocksize, 465 struct nilfs_super_block **sbpp) 466 { 467 struct nilfs_super_block **sbp = nilfs->ns_sbp; 468 struct buffer_head **sbh = nilfs->ns_sbh; 469 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); 470 int valid[2], swp = 0; 471 472 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, 473 &sbh[0]); 474 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); 475 476 if (!sbp[0]) { 477 if (!sbp[1]) { 478 printk(KERN_ERR "NILFS: unable to read superblock\n"); 479 return -EIO; 480 } 481 printk(KERN_WARNING 482 "NILFS warning: unable to read primary superblock\n"); 483 } else if (!sbp[1]) 484 printk(KERN_WARNING 485 "NILFS warning: unable to read secondary superblock\n"); 486 487 /* 488 * Compare two super blocks and set 1 in swp if the secondary 489 * super block is valid and newer. Otherwise, set 0 in swp. 490 */ 491 valid[0] = nilfs_valid_sb(sbp[0]); 492 valid[1] = nilfs_valid_sb(sbp[1]); 493 swp = valid[1] && (!valid[0] || 494 le64_to_cpu(sbp[1]->s_last_cno) > 495 le64_to_cpu(sbp[0]->s_last_cno)); 496 497 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { 498 brelse(sbh[1]); 499 sbh[1] = NULL; 500 sbp[1] = NULL; 501 swp = 0; 502 } 503 if (!valid[swp]) { 504 nilfs_release_super_block(nilfs); 505 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n", 506 sb->s_id); 507 return -EINVAL; 508 } 509 510 if (swp) { 511 printk(KERN_WARNING "NILFS warning: broken superblock. " 512 "using spare superblock.\n"); 513 nilfs_swap_super_block(nilfs); 514 } 515 516 nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime); 517 nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0; 518 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); 519 *sbpp = sbp[0]; 520 return 0; 521 } 522 523 /** 524 * init_nilfs - initialize a NILFS instance. 525 * @nilfs: the_nilfs structure 526 * @sbi: nilfs_sb_info 527 * @sb: super block 528 * @data: mount options 529 * 530 * init_nilfs() performs common initialization per block device (e.g. 531 * reading the super block, getting disk layout information, initializing 532 * shared fields in the_nilfs). It takes on some portion of the jobs 533 * typically done by a fill_super() routine. This division arises from 534 * the nature that multiple NILFS instances may be simultaneously 535 * mounted on a device. 536 * For multiple mounts on the same device, only the first mount 537 * invokes these tasks. 538 * 539 * Return Value: On success, 0 is returned. On error, a negative error 540 * code is returned. 541 */ 542 int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data) 543 { 544 struct super_block *sb = sbi->s_super; 545 struct nilfs_super_block *sbp; 546 struct backing_dev_info *bdi; 547 int blocksize; 548 int err; 549 550 down_write(&nilfs->ns_sem); 551 if (nilfs_init(nilfs)) { 552 /* Load values from existing the_nilfs */ 553 sbp = nilfs->ns_sbp[0]; 554 err = nilfs_store_magic_and_option(sb, sbp, data); 555 if (err) 556 goto out; 557 558 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); 559 if (sb->s_blocksize != blocksize && 560 !sb_set_blocksize(sb, blocksize)) { 561 printk(KERN_ERR "NILFS: blocksize %d unfit to device\n", 562 blocksize); 563 err = -EINVAL; 564 } 565 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); 566 goto out; 567 } 568 569 blocksize = sb_min_blocksize(sb, BLOCK_SIZE); 570 if (!blocksize) { 571 printk(KERN_ERR "NILFS: unable to set blocksize\n"); 572 err = -EINVAL; 573 goto out; 574 } 575 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); 576 if (err) 577 goto out; 578 579 err = nilfs_store_magic_and_option(sb, sbp, data); 580 if (err) 581 goto failed_sbh; 582 583 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); 584 if (sb->s_blocksize != blocksize) { 585 int hw_blocksize = bdev_logical_block_size(sb->s_bdev); 586 587 if (blocksize < hw_blocksize) { 588 printk(KERN_ERR 589 "NILFS: blocksize %d too small for device " 590 "(sector-size = %d).\n", 591 blocksize, hw_blocksize); 592 err = -EINVAL; 593 goto failed_sbh; 594 } 595 nilfs_release_super_block(nilfs); 596 sb_set_blocksize(sb, blocksize); 597 598 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); 599 if (err) 600 goto out; 601 /* not failed_sbh; sbh is released automatically 602 when reloading fails. */ 603 } 604 nilfs->ns_blocksize_bits = sb->s_blocksize_bits; 605 nilfs->ns_blocksize = blocksize; 606 607 err = nilfs_store_disk_layout(nilfs, sbp); 608 if (err) 609 goto failed_sbh; 610 611 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); 612 613 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); 614 615 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info; 616 nilfs->ns_bdi = bdi ? : &default_backing_dev_info; 617 618 /* Finding last segment */ 619 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); 620 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); 621 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); 622 623 nilfs->ns_seg_seq = nilfs->ns_last_seq; 624 nilfs->ns_segnum = 625 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); 626 nilfs->ns_cno = nilfs->ns_last_cno + 1; 627 if (nilfs->ns_segnum >= nilfs->ns_nsegments) { 628 printk(KERN_ERR "NILFS invalid last segment number.\n"); 629 err = -EINVAL; 630 goto failed_sbh; 631 } 632 633 /* Initialize gcinode cache */ 634 err = nilfs_init_gccache(nilfs); 635 if (err) 636 goto failed_sbh; 637 638 set_nilfs_init(nilfs); 639 err = 0; 640 out: 641 up_write(&nilfs->ns_sem); 642 return err; 643 644 failed_sbh: 645 nilfs_release_super_block(nilfs); 646 goto out; 647 } 648 649 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump, 650 size_t nsegs) 651 { 652 sector_t seg_start, seg_end; 653 sector_t start = 0, nblocks = 0; 654 unsigned int sects_per_block; 655 __u64 *sn; 656 int ret = 0; 657 658 sects_per_block = (1 << nilfs->ns_blocksize_bits) / 659 bdev_logical_block_size(nilfs->ns_bdev); 660 for (sn = segnump; sn < segnump + nsegs; sn++) { 661 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end); 662 663 if (!nblocks) { 664 start = seg_start; 665 nblocks = seg_end - seg_start + 1; 666 } else if (start + nblocks == seg_start) { 667 nblocks += seg_end - seg_start + 1; 668 } else { 669 ret = blkdev_issue_discard(nilfs->ns_bdev, 670 start * sects_per_block, 671 nblocks * sects_per_block, 672 GFP_NOFS, 673 BLKDEV_IFL_BARRIER); 674 if (ret < 0) 675 return ret; 676 nblocks = 0; 677 } 678 } 679 if (nblocks) 680 ret = blkdev_issue_discard(nilfs->ns_bdev, 681 start * sects_per_block, 682 nblocks * sects_per_block, 683 GFP_NOFS, BLKDEV_IFL_BARRIER); 684 return ret; 685 } 686 687 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) 688 { 689 struct inode *dat = nilfs_dat_inode(nilfs); 690 unsigned long ncleansegs; 691 692 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 693 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); 694 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ 695 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; 696 return 0; 697 } 698 699 int nilfs_near_disk_full(struct the_nilfs *nilfs) 700 { 701 unsigned long ncleansegs, nincsegs; 702 703 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); 704 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / 705 nilfs->ns_blocks_per_segment + 1; 706 707 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs; 708 } 709 710 /** 711 * nilfs_find_sbinfo - find existing nilfs_sb_info structure 712 * @nilfs: nilfs object 713 * @rw_mount: mount type (non-zero value for read/write mount) 714 * @cno: checkpoint number (zero for read-only mount) 715 * 716 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which 717 * @rw_mount and @cno (in case of snapshots) matched. If no instance 718 * was found, NULL is returned. Although the super block instance can 719 * be unmounted after this function returns, the nilfs_sb_info struct 720 * is kept on memory until nilfs_put_sbinfo() is called. 721 */ 722 struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs, 723 int rw_mount, __u64 cno) 724 { 725 struct nilfs_sb_info *sbi; 726 727 down_read(&nilfs->ns_super_sem); 728 /* 729 * The SNAPSHOT flag and sb->s_flags are supposed to be 730 * protected with nilfs->ns_super_sem. 731 */ 732 sbi = nilfs->ns_current; 733 if (rw_mount) { 734 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY)) 735 goto found; /* read/write mount */ 736 else 737 goto out; 738 } else if (cno == 0) { 739 if (sbi && (sbi->s_super->s_flags & MS_RDONLY)) 740 goto found; /* read-only mount */ 741 else 742 goto out; 743 } 744 745 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) { 746 if (nilfs_test_opt(sbi, SNAPSHOT) && 747 sbi->s_snapshot_cno == cno) 748 goto found; /* snapshot mount */ 749 } 750 out: 751 up_read(&nilfs->ns_super_sem); 752 return NULL; 753 754 found: 755 atomic_inc(&sbi->s_count); 756 up_read(&nilfs->ns_super_sem); 757 return sbi; 758 } 759 760 int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno, 761 int snapshot_mount) 762 { 763 struct nilfs_sb_info *sbi; 764 int ret = 0; 765 766 down_read(&nilfs->ns_super_sem); 767 if (cno == 0 || cno > nilfs->ns_cno) 768 goto out_unlock; 769 770 list_for_each_entry(sbi, &nilfs->ns_supers, s_list) { 771 if (sbi->s_snapshot_cno == cno && 772 (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) { 773 /* exclude read-only mounts */ 774 ret++; 775 break; 776 } 777 } 778 /* for protecting recent checkpoints */ 779 if (cno >= nilfs_last_cno(nilfs)) 780 ret++; 781 782 out_unlock: 783 up_read(&nilfs->ns_super_sem); 784 return ret; 785 } 786