1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Meta data file for NILFS 4 * 5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 6 * 7 * Written by Ryusuke Konishi. 8 */ 9 10 #include <linux/buffer_head.h> 11 #include <linux/mpage.h> 12 #include <linux/mm.h> 13 #include <linux/writeback.h> 14 #include <linux/backing-dev.h> 15 #include <linux/swap.h> 16 #include <linux/slab.h> 17 #include "nilfs.h" 18 #include "btnode.h" 19 #include "segment.h" 20 #include "page.h" 21 #include "mdt.h" 22 #include "alloc.h" /* nilfs_palloc_destroy_cache() */ 23 24 #include <trace/events/nilfs2.h> 25 26 #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1) 27 28 29 static int 30 nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block, 31 struct buffer_head *bh, 32 void (*init_block)(struct inode *, 33 struct buffer_head *, void *)) 34 { 35 struct nilfs_inode_info *ii = NILFS_I(inode); 36 struct folio *folio = bh->b_folio; 37 void *from; 38 int ret; 39 40 /* Caller exclude read accesses using page lock */ 41 42 /* set_buffer_new(bh); */ 43 bh->b_blocknr = 0; 44 45 ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh); 46 if (unlikely(ret)) 47 return ret; 48 49 set_buffer_mapped(bh); 50 51 /* Initialize block (block size > PAGE_SIZE not yet supported) */ 52 from = kmap_local_folio(folio, offset_in_folio(folio, bh->b_data)); 53 memset(from, 0, bh->b_size); 54 if (init_block) 55 init_block(inode, bh, from); 56 kunmap_local(from); 57 58 flush_dcache_folio(folio); 59 60 set_buffer_uptodate(bh); 61 mark_buffer_dirty(bh); 62 nilfs_mdt_mark_dirty(inode); 63 64 trace_nilfs2_mdt_insert_new_block(inode, inode->i_ino, block); 65 66 return 0; 67 } 68 69 static int nilfs_mdt_create_block(struct inode *inode, unsigned long block, 70 struct buffer_head **out_bh, 71 void (*init_block)(struct inode *, 72 struct buffer_head *, 73 void *)) 74 { 75 struct super_block *sb = inode->i_sb; 76 struct nilfs_transaction_info ti; 77 struct buffer_head *bh; 78 int err; 79 80 nilfs_transaction_begin(sb, &ti, 0); 81 82 err = -ENOMEM; 83 bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0); 84 if (unlikely(!bh)) 85 goto failed_unlock; 86 87 err = -EEXIST; 88 if (buffer_uptodate(bh)) 89 goto failed_bh; 90 91 wait_on_buffer(bh); 92 if (buffer_uptodate(bh)) 93 goto failed_bh; 94 95 err = nilfs_mdt_insert_new_block(inode, block, bh, init_block); 96 if (likely(!err)) { 97 get_bh(bh); 98 *out_bh = bh; 99 } 100 101 failed_bh: 102 folio_unlock(bh->b_folio); 103 folio_put(bh->b_folio); 104 brelse(bh); 105 106 failed_unlock: 107 if (likely(!err)) 108 err = nilfs_transaction_commit(sb); 109 else 110 nilfs_transaction_abort(sb); 111 112 return err; 113 } 114 115 static int 116 nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff, blk_opf_t opf, 117 struct buffer_head **out_bh) 118 { 119 struct buffer_head *bh; 120 __u64 blknum = 0; 121 int ret = -ENOMEM; 122 123 bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0); 124 if (unlikely(!bh)) 125 goto failed; 126 127 ret = -EEXIST; /* internal code */ 128 if (buffer_uptodate(bh)) 129 goto out; 130 131 if (opf & REQ_RAHEAD) { 132 if (!trylock_buffer(bh)) { 133 ret = -EBUSY; 134 goto failed_bh; 135 } 136 } else /* opf == REQ_OP_READ */ 137 lock_buffer(bh); 138 139 if (buffer_uptodate(bh)) { 140 unlock_buffer(bh); 141 goto out; 142 } 143 144 ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum); 145 if (unlikely(ret)) { 146 unlock_buffer(bh); 147 goto failed_bh; 148 } 149 map_bh(bh, inode->i_sb, (sector_t)blknum); 150 151 bh_submit(bh, opf, bh_end_read); 152 ret = 0; 153 154 trace_nilfs2_mdt_submit_block(inode, inode->i_ino, blkoff, 155 opf & REQ_OP_MASK); 156 out: 157 get_bh(bh); 158 *out_bh = bh; 159 160 failed_bh: 161 folio_unlock(bh->b_folio); 162 folio_put(bh->b_folio); 163 brelse(bh); 164 failed: 165 return ret; 166 } 167 168 static int nilfs_mdt_read_block(struct inode *inode, unsigned long block, 169 int readahead, struct buffer_head **out_bh) 170 { 171 struct buffer_head *first_bh, *bh; 172 unsigned long blkoff; 173 int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS; 174 int err; 175 176 err = nilfs_mdt_submit_block(inode, block, REQ_OP_READ, &first_bh); 177 if (err == -EEXIST) /* internal code */ 178 goto out; 179 180 if (unlikely(err)) 181 goto failed; 182 183 if (readahead) { 184 blkoff = block + 1; 185 for (i = 0; i < nr_ra_blocks; i++, blkoff++) { 186 err = nilfs_mdt_submit_block(inode, blkoff, 187 REQ_OP_READ | REQ_RAHEAD, &bh); 188 if (likely(!err || err == -EEXIST)) 189 brelse(bh); 190 else if (err != -EBUSY) 191 break; 192 /* abort readahead if bmap lookup failed */ 193 if (!buffer_locked(first_bh)) 194 goto out_no_wait; 195 } 196 } 197 198 wait_on_buffer(first_bh); 199 200 out_no_wait: 201 err = -EIO; 202 if (!buffer_uptodate(first_bh)) { 203 nilfs_err(inode->i_sb, 204 "I/O error reading meta-data file (ino=%llu, block-offset=%lu)", 205 inode->i_ino, block); 206 goto failed_bh; 207 } 208 out: 209 *out_bh = first_bh; 210 return 0; 211 212 failed_bh: 213 brelse(first_bh); 214 failed: 215 return err; 216 } 217 218 /** 219 * nilfs_mdt_get_block - read or create a buffer on meta data file. 220 * @inode: inode of the meta data file 221 * @blkoff: block offset 222 * @create: create flag 223 * @init_block: initializer used for newly allocated block 224 * @out_bh: output of a pointer to the buffer_head 225 * 226 * nilfs_mdt_get_block() looks up the specified buffer and tries to create 227 * a new buffer if @create is not zero. If (and only if) this function 228 * succeeds, it stores a pointer to the retrieved buffer head in the location 229 * pointed to by @out_bh. 230 * 231 * The retrieved buffer may be either an existing one or a newly allocated one. 232 * For a newly created buffer, if the callback function argument @init_block 233 * is non-NULL, the callback will be called with the buffer locked to format 234 * the block. 235 * 236 * Return: 0 on success, or one of the following negative error codes on 237 * failure: 238 * * %-EIO - I/O error (including metadata corruption). 239 * * %-ENOENT - The specified block does not exist (hole block). 240 * * %-ENOMEM - Insufficient memory available. 241 * * %-EROFS - Read only filesystem (for create mode). 242 */ 243 int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create, 244 void (*init_block)(struct inode *, 245 struct buffer_head *, void *), 246 struct buffer_head **out_bh) 247 { 248 int ret; 249 250 /* Should be rewritten with merging nilfs_mdt_read_block() */ 251 retry: 252 ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh); 253 if (!create || ret != -ENOENT) 254 return ret; 255 256 ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block); 257 if (unlikely(ret == -EEXIST)) { 258 /* create = 0; */ /* limit read-create loop retries */ 259 goto retry; 260 } 261 return ret; 262 } 263 264 /** 265 * nilfs_mdt_find_block - find and get a buffer on meta data file. 266 * @inode: inode of the meta data file 267 * @start: start block offset (inclusive) 268 * @end: end block offset (inclusive) 269 * @blkoff: block offset 270 * @out_bh: place to store a pointer to buffer_head struct 271 * 272 * nilfs_mdt_find_block() looks up an existing block in range of 273 * [@start, @end] and stores pointer to a buffer head of the block to 274 * @out_bh, and block offset to @blkoff, respectively. @out_bh and 275 * @blkoff are substituted only when zero is returned. 276 * 277 * Return: 0 on success, or one of the following negative error codes on 278 * failure: 279 * * %-EIO - I/O error (including metadata corruption). 280 * * %-ENOENT - No block was found in the range. 281 * * %-ENOMEM - Insufficient memory available. 282 */ 283 int nilfs_mdt_find_block(struct inode *inode, unsigned long start, 284 unsigned long end, unsigned long *blkoff, 285 struct buffer_head **out_bh) 286 { 287 __u64 next; 288 int ret; 289 290 if (unlikely(start > end)) 291 return -ENOENT; 292 293 ret = nilfs_mdt_read_block(inode, start, true, out_bh); 294 if (!ret) { 295 *blkoff = start; 296 goto out; 297 } 298 if (unlikely(ret != -ENOENT || start == ULONG_MAX)) 299 goto out; 300 301 ret = nilfs_bmap_seek_key(NILFS_I(inode)->i_bmap, start + 1, &next); 302 if (!ret) { 303 if (next <= end) { 304 ret = nilfs_mdt_read_block(inode, next, true, out_bh); 305 if (!ret) 306 *blkoff = next; 307 } else { 308 ret = -ENOENT; 309 } 310 } 311 out: 312 return ret; 313 } 314 315 /** 316 * nilfs_mdt_delete_block - make a hole on the meta data file. 317 * @inode: inode of the meta data file 318 * @block: block offset 319 * 320 * Return: 0 on success, or one of the following negative error codes on 321 * failure: 322 * * %-EIO - I/O error (including metadata corruption). 323 * * %-ENOENT - Non-existent block. 324 * * %-ENOMEM - Insufficient memory available. 325 */ 326 int nilfs_mdt_delete_block(struct inode *inode, unsigned long block) 327 { 328 struct nilfs_inode_info *ii = NILFS_I(inode); 329 int err; 330 331 err = nilfs_bmap_delete(ii->i_bmap, block); 332 if (!err || err == -ENOENT) { 333 nilfs_mdt_mark_dirty(inode); 334 nilfs_mdt_forget_block(inode, block); 335 } 336 return err; 337 } 338 339 /** 340 * nilfs_mdt_forget_block - discard dirty state and try to remove the page 341 * @inode: inode of the meta data file 342 * @block: block offset 343 * 344 * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and 345 * tries to release the page including the buffer from a page cache. 346 * 347 * Return: 0 on success, or one of the following negative error codes on 348 * failure: 349 * * %-EBUSY - Page has an active buffer. 350 * * %-ENOENT - Page cache has no page addressed by the offset. 351 */ 352 int nilfs_mdt_forget_block(struct inode *inode, unsigned long block) 353 { 354 pgoff_t index = block >> (PAGE_SHIFT - inode->i_blkbits); 355 struct folio *folio; 356 struct buffer_head *bh; 357 int ret = 0; 358 int still_dirty; 359 360 folio = filemap_lock_folio(inode->i_mapping, index); 361 if (IS_ERR(folio)) 362 return -ENOENT; 363 364 folio_wait_writeback(folio); 365 366 bh = folio_buffers(folio); 367 if (bh) { 368 unsigned long first_block = index << 369 (PAGE_SHIFT - inode->i_blkbits); 370 bh = get_nth_bh(bh, block - first_block); 371 nilfs_forget_buffer(bh); 372 } 373 still_dirty = folio_test_dirty(folio); 374 folio_unlock(folio); 375 folio_put(folio); 376 377 if (still_dirty || 378 invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0) 379 ret = -EBUSY; 380 return ret; 381 } 382 383 int nilfs_mdt_fetch_dirty(struct inode *inode) 384 { 385 struct nilfs_inode_info *ii = NILFS_I(inode); 386 387 if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) { 388 set_bit(NILFS_I_DIRTY, &ii->i_state); 389 return 1; 390 } 391 return test_bit(NILFS_I_DIRTY, &ii->i_state); 392 } 393 394 static int nilfs_mdt_write_folio(struct folio *folio, 395 struct writeback_control *wbc) 396 { 397 struct inode *inode = folio->mapping->host; 398 struct super_block *sb; 399 int err = 0; 400 401 if (inode && sb_rdonly(inode->i_sb)) { 402 /* 403 * It means that filesystem was remounted in read-only 404 * mode because of error or metadata corruption. But we 405 * have dirty folios that try to be flushed in background. 406 * So, here we simply discard this dirty folio. 407 */ 408 nilfs_clear_folio_dirty(folio); 409 folio_unlock(folio); 410 return -EROFS; 411 } 412 413 folio_redirty_for_writepage(wbc, folio); 414 folio_unlock(folio); 415 416 if (!inode) 417 return 0; 418 419 sb = inode->i_sb; 420 421 if (wbc->sync_mode == WB_SYNC_ALL) 422 err = nilfs_construct_segment(sb); 423 424 return err; 425 } 426 427 static int nilfs_mdt_writeback(struct address_space *mapping, 428 struct writeback_control *wbc) 429 { 430 struct folio *folio = NULL; 431 int error; 432 433 while ((folio = writeback_iter(mapping, wbc, folio, &error))) 434 error = nilfs_mdt_write_folio(folio, wbc); 435 436 return error; 437 } 438 439 static const struct address_space_operations def_mdt_aops = { 440 .dirty_folio = block_dirty_folio, 441 .invalidate_folio = block_invalidate_folio, 442 .writepages = nilfs_mdt_writeback, 443 .migrate_folio = buffer_migrate_folio_norefs, 444 }; 445 446 static const struct inode_operations def_mdt_iops; 447 static const struct file_operations def_mdt_fops; 448 449 450 int nilfs_mdt_init(struct inode *inode, gfp_t gfp_mask, size_t objsz) 451 { 452 struct nilfs_mdt_info *mi; 453 454 mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS); 455 if (!mi) 456 return -ENOMEM; 457 458 init_rwsem(&mi->mi_sem); 459 inode->i_private = mi; 460 461 inode->i_mode = S_IFREG; 462 mapping_set_gfp_mask(inode->i_mapping, gfp_mask); 463 464 inode->i_op = &def_mdt_iops; 465 inode->i_fop = &def_mdt_fops; 466 inode->i_mapping->a_ops = &def_mdt_aops; 467 468 return 0; 469 } 470 471 /** 472 * nilfs_mdt_clear - do cleanup for the metadata file 473 * @inode: inode of the metadata file 474 */ 475 void nilfs_mdt_clear(struct inode *inode) 476 { 477 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 478 struct nilfs_shadow_map *shadow = mdi->mi_shadow; 479 480 if (mdi->mi_palloc_cache) 481 nilfs_palloc_destroy_cache(inode); 482 483 if (shadow) { 484 struct inode *s_inode = shadow->inode; 485 486 shadow->inode = NULL; 487 iput(s_inode); 488 mdi->mi_shadow = NULL; 489 } 490 } 491 492 /** 493 * nilfs_mdt_destroy - release resources used by the metadata file 494 * @inode: inode of the metadata file 495 */ 496 void nilfs_mdt_destroy(struct inode *inode) 497 { 498 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 499 500 kfree(mdi->mi_bgl); /* kfree(NULL) is safe */ 501 kfree(mdi); 502 } 503 504 void nilfs_mdt_set_entry_size(struct inode *inode, unsigned int entry_size, 505 unsigned int header_size) 506 { 507 struct nilfs_mdt_info *mi = NILFS_MDT(inode); 508 509 mi->mi_entry_size = entry_size; 510 mi->mi_entries_per_block = i_blocksize(inode) / entry_size; 511 mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size); 512 } 513 514 /** 515 * nilfs_mdt_setup_shadow_map - setup shadow map and bind it to metadata file 516 * @inode: inode of the metadata file 517 * @shadow: shadow mapping 518 * 519 * Return: 0 on success, or a negative error code on failure. 520 */ 521 int nilfs_mdt_setup_shadow_map(struct inode *inode, 522 struct nilfs_shadow_map *shadow) 523 { 524 struct nilfs_mdt_info *mi = NILFS_MDT(inode); 525 struct inode *s_inode; 526 527 INIT_LIST_HEAD(&shadow->frozen_buffers); 528 529 s_inode = nilfs_iget_for_shadow(inode); 530 if (IS_ERR(s_inode)) 531 return PTR_ERR(s_inode); 532 533 shadow->inode = s_inode; 534 mi->mi_shadow = shadow; 535 return 0; 536 } 537 538 /** 539 * nilfs_mdt_save_to_shadow_map - copy bmap and dirty pages to shadow map 540 * @inode: inode of the metadata file 541 * 542 * Return: 0 on success, or a negative error code on failure. 543 */ 544 int nilfs_mdt_save_to_shadow_map(struct inode *inode) 545 { 546 struct nilfs_mdt_info *mi = NILFS_MDT(inode); 547 struct nilfs_inode_info *ii = NILFS_I(inode); 548 struct nilfs_shadow_map *shadow = mi->mi_shadow; 549 struct inode *s_inode = shadow->inode; 550 int ret; 551 552 ret = nilfs_copy_dirty_pages(s_inode->i_mapping, inode->i_mapping); 553 if (ret) 554 goto out; 555 556 ret = nilfs_copy_dirty_pages(NILFS_I(s_inode)->i_assoc_inode->i_mapping, 557 ii->i_assoc_inode->i_mapping); 558 if (ret) 559 goto out; 560 561 nilfs_bmap_save(ii->i_bmap, &shadow->bmap_store); 562 out: 563 return ret; 564 } 565 566 int nilfs_mdt_freeze_buffer(struct inode *inode, struct buffer_head *bh) 567 { 568 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow; 569 struct buffer_head *bh_frozen; 570 struct folio *folio; 571 int blkbits = inode->i_blkbits; 572 573 folio = filemap_grab_folio(shadow->inode->i_mapping, 574 bh->b_folio->index); 575 if (IS_ERR(folio)) 576 return PTR_ERR(folio); 577 578 bh_frozen = folio_buffers(folio); 579 if (!bh_frozen) 580 bh_frozen = create_empty_buffers(folio, 1 << blkbits, 0); 581 582 bh_frozen = get_nth_bh(bh_frozen, 583 offset_in_folio(folio, bh->b_data) >> blkbits); 584 585 if (!buffer_uptodate(bh_frozen)) 586 nilfs_copy_buffer(bh_frozen, bh); 587 if (list_empty(&bh_frozen->b_assoc_buffers)) { 588 list_add_tail(&bh_frozen->b_assoc_buffers, 589 &shadow->frozen_buffers); 590 set_buffer_nilfs_redirected(bh); 591 } else { 592 brelse(bh_frozen); /* already frozen */ 593 } 594 595 folio_unlock(folio); 596 folio_put(folio); 597 return 0; 598 } 599 600 struct buffer_head * 601 nilfs_mdt_get_frozen_buffer(struct inode *inode, struct buffer_head *bh) 602 { 603 struct nilfs_shadow_map *shadow = NILFS_MDT(inode)->mi_shadow; 604 struct buffer_head *bh_frozen = NULL; 605 struct folio *folio; 606 int n; 607 608 folio = filemap_lock_folio(shadow->inode->i_mapping, 609 bh->b_folio->index); 610 if (!IS_ERR(folio)) { 611 bh_frozen = folio_buffers(folio); 612 if (bh_frozen) { 613 n = offset_in_folio(folio, bh->b_data) >> 614 inode->i_blkbits; 615 bh_frozen = get_nth_bh(bh_frozen, n); 616 } 617 folio_unlock(folio); 618 folio_put(folio); 619 } 620 return bh_frozen; 621 } 622 623 static void nilfs_release_frozen_buffers(struct nilfs_shadow_map *shadow) 624 { 625 struct list_head *head = &shadow->frozen_buffers; 626 struct buffer_head *bh; 627 628 while (!list_empty(head)) { 629 bh = list_first_entry(head, struct buffer_head, 630 b_assoc_buffers); 631 list_del_init(&bh->b_assoc_buffers); 632 brelse(bh); /* drop ref-count to make it releasable */ 633 } 634 } 635 636 /** 637 * nilfs_mdt_restore_from_shadow_map - restore dirty pages and bmap state 638 * @inode: inode of the metadata file 639 */ 640 void nilfs_mdt_restore_from_shadow_map(struct inode *inode) 641 { 642 struct nilfs_mdt_info *mi = NILFS_MDT(inode); 643 struct nilfs_inode_info *ii = NILFS_I(inode); 644 struct nilfs_shadow_map *shadow = mi->mi_shadow; 645 646 down_write(&mi->mi_sem); 647 648 if (mi->mi_palloc_cache) 649 nilfs_palloc_clear_cache(inode); 650 651 nilfs_clear_dirty_pages(inode->i_mapping); 652 nilfs_copy_back_pages(inode->i_mapping, shadow->inode->i_mapping); 653 654 nilfs_clear_dirty_pages(ii->i_assoc_inode->i_mapping); 655 nilfs_copy_back_pages(ii->i_assoc_inode->i_mapping, 656 NILFS_I(shadow->inode)->i_assoc_inode->i_mapping); 657 658 nilfs_bmap_restore(ii->i_bmap, &shadow->bmap_store); 659 660 up_write(&mi->mi_sem); 661 } 662 663 /** 664 * nilfs_mdt_clear_shadow_map - truncate pages in shadow map caches 665 * @inode: inode of the metadata file 666 */ 667 void nilfs_mdt_clear_shadow_map(struct inode *inode) 668 { 669 struct nilfs_mdt_info *mi = NILFS_MDT(inode); 670 struct nilfs_shadow_map *shadow = mi->mi_shadow; 671 struct inode *shadow_btnc_inode = NILFS_I(shadow->inode)->i_assoc_inode; 672 673 down_write(&mi->mi_sem); 674 nilfs_release_frozen_buffers(shadow); 675 truncate_inode_pages(shadow->inode->i_mapping, 0); 676 truncate_inode_pages(shadow_btnc_inode->i_mapping, 0); 677 up_write(&mi->mi_sem); 678 } 679