1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * NILFS inode operations. 4 * 5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 6 * 7 * Written by Ryusuke Konishi. 8 * 9 */ 10 11 #include <linux/buffer_head.h> 12 #include <linux/gfp.h> 13 #include <linux/mpage.h> 14 #include <linux/pagemap.h> 15 #include <linux/writeback.h> 16 #include <linux/uio.h> 17 #include <linux/fiemap.h> 18 #include <linux/random.h> 19 #include "nilfs.h" 20 #include "btnode.h" 21 #include "segment.h" 22 #include "page.h" 23 #include "mdt.h" 24 #include "cpfile.h" 25 #include "ifile.h" 26 27 /** 28 * struct nilfs_iget_args - arguments used during comparison between inodes 29 * @ino: inode number 30 * @cno: checkpoint number 31 * @root: pointer on NILFS root object (mounted checkpoint) 32 * @type: inode type 33 */ 34 struct nilfs_iget_args { 35 u64 ino; 36 __u64 cno; 37 struct nilfs_root *root; 38 unsigned int type; 39 }; 40 41 static int nilfs_iget_test(struct inode *inode, void *opaque); 42 43 void nilfs_inode_add_blocks(struct inode *inode, int n) 44 { 45 struct nilfs_root *root = NILFS_I(inode)->i_root; 46 47 inode_add_bytes(inode, i_blocksize(inode) * n); 48 if (root) 49 atomic64_add(n, &root->blocks_count); 50 } 51 52 void nilfs_inode_sub_blocks(struct inode *inode, int n) 53 { 54 struct nilfs_root *root = NILFS_I(inode)->i_root; 55 56 inode_sub_bytes(inode, i_blocksize(inode) * n); 57 if (root) 58 atomic64_sub(n, &root->blocks_count); 59 } 60 61 /** 62 * nilfs_get_block() - get a file block on the filesystem (callback function) 63 * @inode: inode struct of the target file 64 * @blkoff: file block number 65 * @bh_result: buffer head to be mapped on 66 * @create: indicate whether allocating the block or not when it has not 67 * been allocated yet. 68 * 69 * This function does not issue actual read request of the specified data 70 * block. It is done by VFS. 71 * 72 * Return: 0 on success, or a negative error code on failure. 73 */ 74 int nilfs_get_block(struct inode *inode, sector_t blkoff, 75 struct buffer_head *bh_result, int create) 76 { 77 struct nilfs_inode_info *ii = NILFS_I(inode); 78 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 79 __u64 blknum = 0; 80 int err = 0, ret; 81 unsigned int maxblocks = bh_result->b_size >> inode->i_blkbits; 82 83 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 84 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 85 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 86 if (ret >= 0) { /* found */ 87 map_bh(bh_result, inode->i_sb, blknum); 88 if (ret > 0) 89 bh_result->b_size = (ret << inode->i_blkbits); 90 goto out; 91 } 92 /* data block was not found */ 93 if (ret == -ENOENT && create) { 94 struct nilfs_transaction_info ti; 95 96 bh_result->b_blocknr = 0; 97 err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 98 if (unlikely(err)) 99 goto out; 100 err = nilfs_bmap_insert(ii->i_bmap, blkoff, 101 (unsigned long)bh_result); 102 if (unlikely(err != 0)) { 103 if (err == -EEXIST) { 104 /* 105 * The get_block() function could be called 106 * from multiple callers for an inode. 107 * However, the page having this block must 108 * be locked in this case. 109 */ 110 nilfs_warn(inode->i_sb, 111 "%s (ino=%llu): a race condition while inserting a data block at offset=%llu", 112 __func__, inode->i_ino, 113 (unsigned long long)blkoff); 114 err = -EAGAIN; 115 } 116 nilfs_transaction_abort(inode->i_sb); 117 goto out; 118 } 119 nilfs_mark_inode_dirty_sync(inode); 120 nilfs_transaction_commit(inode->i_sb); /* never fails */ 121 /* Error handling should be detailed */ 122 set_buffer_new(bh_result); 123 set_buffer_delay(bh_result); 124 map_bh(bh_result, inode->i_sb, 0); 125 /* Disk block number must be changed to proper value */ 126 127 } else if (ret == -ENOENT) { 128 /* 129 * not found is not error (e.g. hole); must return without 130 * the mapped state flag. 131 */ 132 ; 133 } else { 134 err = ret; 135 } 136 137 out: 138 return err; 139 } 140 141 /** 142 * nilfs_read_folio() - implement read_folio() method of nilfs_aops {} 143 * address_space_operations. 144 * @file: file struct of the file to be read 145 * @folio: the folio to be read 146 * 147 * Return: 0 on success, or a negative error code on failure. 148 */ 149 static int nilfs_read_folio(struct file *file, struct folio *folio) 150 { 151 return mpage_read_folio(folio, nilfs_get_block); 152 } 153 154 static void nilfs_readahead(struct readahead_control *rac) 155 { 156 mpage_readahead(rac, nilfs_get_block); 157 } 158 159 static int nilfs_writepages(struct address_space *mapping, 160 struct writeback_control *wbc) 161 { 162 struct inode *inode = mapping->host; 163 int err = 0; 164 165 if (sb_rdonly(inode->i_sb)) { 166 nilfs_clear_dirty_pages(mapping); 167 return -EROFS; 168 } 169 170 if (wbc->sync_mode == WB_SYNC_ALL) 171 err = nilfs_construct_dsync_segment(inode->i_sb, inode, 172 wbc->range_start, 173 wbc->range_end); 174 return err; 175 } 176 177 static bool nilfs_dirty_folio(struct address_space *mapping, 178 struct folio *folio) 179 { 180 struct inode *inode = mapping->host; 181 struct buffer_head *head; 182 unsigned int nr_dirty = 0; 183 bool ret = filemap_dirty_folio(mapping, folio); 184 185 /* 186 * The page may not be locked, eg if called from try_to_unmap_one() 187 */ 188 spin_lock(&mapping->i_private_lock); 189 head = folio_buffers(folio); 190 if (head) { 191 struct buffer_head *bh = head; 192 193 do { 194 /* Do not mark hole blocks dirty */ 195 if (buffer_dirty(bh) || !buffer_mapped(bh)) 196 continue; 197 198 set_buffer_dirty(bh); 199 nr_dirty++; 200 } while (bh = bh->b_this_page, bh != head); 201 } else if (ret) { 202 nr_dirty = 1 << (folio_shift(folio) - inode->i_blkbits); 203 } 204 spin_unlock(&mapping->i_private_lock); 205 206 if (nr_dirty) 207 nilfs_set_file_dirty(inode, nr_dirty); 208 return ret; 209 } 210 211 void nilfs_write_failed(struct address_space *mapping, loff_t to) 212 { 213 struct inode *inode = mapping->host; 214 215 if (to > inode->i_size) { 216 truncate_pagecache(inode, inode->i_size); 217 nilfs_truncate(inode); 218 } 219 } 220 221 static int nilfs_write_begin(const struct kiocb *iocb, 222 struct address_space *mapping, 223 loff_t pos, unsigned len, 224 struct folio **foliop, void **fsdata) 225 226 { 227 struct inode *inode = mapping->host; 228 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 229 230 if (unlikely(err)) 231 return err; 232 233 err = block_write_begin(mapping, pos, len, foliop, nilfs_get_block); 234 if (unlikely(err)) { 235 nilfs_write_failed(mapping, pos + len); 236 nilfs_transaction_abort(inode->i_sb); 237 } 238 return err; 239 } 240 241 static int nilfs_write_end(const struct kiocb *iocb, 242 struct address_space *mapping, 243 loff_t pos, unsigned len, unsigned copied, 244 struct folio *folio, void *fsdata) 245 { 246 struct inode *inode = mapping->host; 247 unsigned int start = pos & (PAGE_SIZE - 1); 248 unsigned int nr_dirty; 249 int err; 250 251 nr_dirty = nilfs_page_count_clean_buffers(folio, start, 252 start + copied); 253 copied = generic_write_end(iocb, mapping, pos, len, copied, folio, 254 fsdata); 255 nilfs_set_file_dirty(inode, nr_dirty); 256 err = nilfs_transaction_commit(inode->i_sb); 257 return err ? : copied; 258 } 259 260 static ssize_t 261 nilfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 262 { 263 struct inode *inode = file_inode(iocb->ki_filp); 264 265 if (iov_iter_rw(iter) == WRITE) 266 return 0; 267 268 /* Needs synchronization with the cleaner */ 269 return blockdev_direct_IO(iocb, inode, iter, nilfs_get_block); 270 } 271 272 const struct address_space_operations nilfs_aops = { 273 .read_folio = nilfs_read_folio, 274 .writepages = nilfs_writepages, 275 .dirty_folio = nilfs_dirty_folio, 276 .readahead = nilfs_readahead, 277 .write_begin = nilfs_write_begin, 278 .write_end = nilfs_write_end, 279 .invalidate_folio = block_invalidate_folio, 280 .direct_IO = nilfs_direct_IO, 281 .migrate_folio = buffer_migrate_folio_norefs, 282 .is_partially_uptodate = block_is_partially_uptodate, 283 }; 284 285 const struct address_space_operations nilfs_buffer_cache_aops = { 286 .invalidate_folio = block_invalidate_folio, 287 }; 288 289 static int nilfs_insert_inode_locked(struct inode *inode, 290 struct nilfs_root *root, u64 ino) 291 { 292 struct nilfs_iget_args args = { 293 .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL 294 }; 295 296 return insert_inode_locked4(inode, ino, nilfs_iget_test, &args); 297 } 298 299 struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) 300 { 301 struct super_block *sb = dir->i_sb; 302 struct inode *inode; 303 struct nilfs_inode_info *ii; 304 struct nilfs_root *root; 305 struct buffer_head *bh; 306 int err = -ENOMEM; 307 u64 ino; 308 309 inode = new_inode(sb); 310 if (unlikely(!inode)) 311 goto failed; 312 313 mapping_set_gfp_mask(inode->i_mapping, 314 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS)); 315 316 root = NILFS_I(dir)->i_root; 317 ii = NILFS_I(inode); 318 ii->i_state = BIT(NILFS_I_NEW); 319 ii->i_type = NILFS_I_TYPE_NORMAL; 320 ii->i_root = root; 321 322 err = nilfs_ifile_create_inode(root->ifile, &ino, &bh); 323 if (unlikely(err)) 324 goto failed_ifile_create_inode; 325 /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 326 ii->i_bh = bh; 327 328 atomic64_inc(&root->inodes_count); 329 inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 330 inode->i_ino = ino; 331 simple_inode_init_ts(inode); 332 333 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 334 err = nilfs_bmap_read(ii->i_bmap, NULL); 335 if (err < 0) 336 goto failed_after_creation; 337 338 set_bit(NILFS_I_BMAP, &ii->i_state); 339 /* No lock is needed; iget() ensures it. */ 340 } 341 342 ii->i_flags = nilfs_mask_flags( 343 mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); 344 345 /* ii->i_file_acl = 0; */ 346 /* ii->i_dir_acl = 0; */ 347 ii->i_dir_start_lookup = 0; 348 nilfs_set_inode_flags(inode); 349 inode->i_generation = get_random_u32(); 350 if (nilfs_insert_inode_locked(inode, root, ino) < 0) { 351 err = -EIO; 352 goto failed_after_creation; 353 } 354 355 err = nilfs_init_acl(inode, dir); 356 if (unlikely(err)) 357 /* 358 * Never occur. When supporting nilfs_init_acl(), 359 * proper cancellation of above jobs should be considered. 360 */ 361 goto failed_after_creation; 362 363 return inode; 364 365 failed_after_creation: 366 clear_nlink(inode); 367 if (inode_state_read_once(inode) & I_NEW) 368 unlock_new_inode(inode); 369 iput(inode); /* 370 * raw_inode will be deleted through 371 * nilfs_evict_inode(). 372 */ 373 goto failed; 374 375 failed_ifile_create_inode: 376 make_bad_inode(inode); 377 iput(inode); 378 failed: 379 return ERR_PTR(err); 380 } 381 382 void nilfs_set_inode_flags(struct inode *inode) 383 { 384 unsigned int flags = NILFS_I(inode)->i_flags; 385 unsigned int new_fl = 0; 386 387 if (flags & FS_SYNC_FL) 388 new_fl |= S_SYNC; 389 if (flags & FS_APPEND_FL) 390 new_fl |= S_APPEND; 391 if (flags & FS_IMMUTABLE_FL) 392 new_fl |= S_IMMUTABLE; 393 if (flags & FS_NOATIME_FL) 394 new_fl |= S_NOATIME; 395 if (flags & FS_DIRSYNC_FL) 396 new_fl |= S_DIRSYNC; 397 inode_set_flags(inode, new_fl, S_SYNC | S_APPEND | S_IMMUTABLE | 398 S_NOATIME | S_DIRSYNC); 399 } 400 401 int nilfs_read_inode_common(struct inode *inode, 402 struct nilfs_inode *raw_inode) 403 { 404 struct nilfs_inode_info *ii = NILFS_I(inode); 405 int err; 406 407 inode->i_mode = le16_to_cpu(raw_inode->i_mode); 408 i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); 409 i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); 410 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 411 inode->i_size = le64_to_cpu(raw_inode->i_size); 412 inode_set_atime(inode, le64_to_cpu(raw_inode->i_mtime), 413 le32_to_cpu(raw_inode->i_mtime_nsec)); 414 inode_set_ctime(inode, le64_to_cpu(raw_inode->i_ctime), 415 le32_to_cpu(raw_inode->i_ctime_nsec)); 416 inode_set_mtime(inode, le64_to_cpu(raw_inode->i_mtime), 417 le32_to_cpu(raw_inode->i_mtime_nsec)); 418 if (nilfs_is_metadata_file_inode(inode) && !S_ISREG(inode->i_mode)) 419 return -EIO; /* this inode is for metadata and corrupted */ 420 if (inode->i_nlink == 0) 421 return -ESTALE; /* this inode is deleted */ 422 423 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 424 ii->i_flags = le32_to_cpu(raw_inode->i_flags); 425 #if 0 426 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 427 ii->i_dir_acl = S_ISREG(inode->i_mode) ? 428 0 : le32_to_cpu(raw_inode->i_dir_acl); 429 #endif 430 ii->i_dir_start_lookup = 0; 431 inode->i_generation = le32_to_cpu(raw_inode->i_generation); 432 433 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 434 S_ISLNK(inode->i_mode)) { 435 err = nilfs_bmap_read(ii->i_bmap, raw_inode); 436 if (err < 0) 437 return err; 438 set_bit(NILFS_I_BMAP, &ii->i_state); 439 /* No lock is needed; iget() ensures it. */ 440 } 441 return 0; 442 } 443 444 static int __nilfs_read_inode(struct super_block *sb, 445 struct nilfs_root *root, u64 ino, 446 struct inode *inode) 447 { 448 struct the_nilfs *nilfs = sb->s_fs_info; 449 struct buffer_head *bh; 450 struct nilfs_inode *raw_inode; 451 int err; 452 453 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 454 err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 455 if (unlikely(err)) 456 goto bad_inode; 457 458 raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 459 460 err = nilfs_read_inode_common(inode, raw_inode); 461 if (err) 462 goto failed_unmap; 463 464 if (S_ISREG(inode->i_mode)) { 465 inode->i_op = &nilfs_file_inode_operations; 466 inode->i_fop = &nilfs_file_operations; 467 inode->i_mapping->a_ops = &nilfs_aops; 468 } else if (S_ISDIR(inode->i_mode)) { 469 inode->i_op = &nilfs_dir_inode_operations; 470 inode->i_fop = &nilfs_dir_operations; 471 inode->i_mapping->a_ops = &nilfs_aops; 472 } else if (S_ISLNK(inode->i_mode)) { 473 inode->i_op = &nilfs_symlink_inode_operations; 474 inode_nohighmem(inode); 475 inode->i_mapping->a_ops = &nilfs_aops; 476 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 477 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 478 inode->i_op = &nilfs_special_inode_operations; 479 init_special_inode( 480 inode, inode->i_mode, 481 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 482 } else { 483 nilfs_error(sb, 484 "invalid file type bits in mode 0%o for inode %llu", 485 inode->i_mode, ino); 486 err = -EIO; 487 goto failed_unmap; 488 } 489 nilfs_ifile_unmap_inode(raw_inode); 490 brelse(bh); 491 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 492 nilfs_set_inode_flags(inode); 493 mapping_set_gfp_mask(inode->i_mapping, 494 mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS)); 495 return 0; 496 497 failed_unmap: 498 nilfs_ifile_unmap_inode(raw_inode); 499 brelse(bh); 500 501 bad_inode: 502 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 503 return err; 504 } 505 506 static int nilfs_iget_test(struct inode *inode, void *opaque) 507 { 508 struct nilfs_iget_args *args = opaque; 509 struct nilfs_inode_info *ii; 510 511 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 512 return 0; 513 514 ii = NILFS_I(inode); 515 if (ii->i_type != args->type) 516 return 0; 517 518 return !(args->type & NILFS_I_TYPE_GC) || args->cno == ii->i_cno; 519 } 520 521 static int nilfs_iget_set(struct inode *inode, void *opaque) 522 { 523 struct nilfs_iget_args *args = opaque; 524 525 inode->i_ino = args->ino; 526 NILFS_I(inode)->i_cno = args->cno; 527 NILFS_I(inode)->i_root = args->root; 528 NILFS_I(inode)->i_type = args->type; 529 if (args->root && args->ino == NILFS_ROOT_INO) 530 nilfs_get_root(args->root); 531 return 0; 532 } 533 534 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 535 u64 ino) 536 { 537 struct nilfs_iget_args args = { 538 .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL 539 }; 540 541 return ilookup5(sb, ino, nilfs_iget_test, &args); 542 } 543 544 struct inode *nilfs_iget_locked(struct super_block *sb, 545 struct nilfs_root *root, u64 ino) 546 { 547 struct nilfs_iget_args args = { 548 .ino = ino, .root = root, .cno = 0, .type = NILFS_I_TYPE_NORMAL 549 }; 550 551 return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 552 } 553 554 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 555 u64 ino) 556 { 557 struct inode *inode; 558 int err; 559 560 inode = nilfs_iget_locked(sb, root, ino); 561 if (unlikely(!inode)) 562 return ERR_PTR(-ENOMEM); 563 564 if (!(inode_state_read_once(inode) & I_NEW)) { 565 if (!inode->i_nlink) { 566 iput(inode); 567 return ERR_PTR(-ESTALE); 568 } 569 return inode; 570 } 571 572 err = __nilfs_read_inode(sb, root, ino, inode); 573 if (unlikely(err)) { 574 iget_failed(inode); 575 return ERR_PTR(err); 576 } 577 unlock_new_inode(inode); 578 return inode; 579 } 580 581 struct inode *nilfs_iget_for_gc(struct super_block *sb, u64 ino, __u64 cno) 582 { 583 struct nilfs_iget_args args = { 584 .ino = ino, .root = NULL, .cno = cno, .type = NILFS_I_TYPE_GC 585 }; 586 struct inode *inode; 587 int err; 588 589 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 590 if (unlikely(!inode)) 591 return ERR_PTR(-ENOMEM); 592 if (!(inode_state_read_once(inode) & I_NEW)) 593 return inode; 594 595 err = nilfs_init_gcinode(inode); 596 if (unlikely(err)) { 597 iget_failed(inode); 598 return ERR_PTR(err); 599 } 600 unlock_new_inode(inode); 601 return inode; 602 } 603 604 /** 605 * nilfs_attach_btree_node_cache - attach a B-tree node cache to the inode 606 * @inode: inode object 607 * 608 * nilfs_attach_btree_node_cache() attaches a B-tree node cache to @inode, 609 * or does nothing if the inode already has it. This function allocates 610 * an additional inode to maintain page cache of B-tree nodes one-on-one. 611 * 612 * Return: 0 on success, or %-ENOMEM if memory is insufficient. 613 */ 614 int nilfs_attach_btree_node_cache(struct inode *inode) 615 { 616 struct nilfs_inode_info *ii = NILFS_I(inode); 617 struct inode *btnc_inode; 618 struct nilfs_iget_args args; 619 620 if (ii->i_assoc_inode) 621 return 0; 622 623 args.ino = inode->i_ino; 624 args.root = ii->i_root; 625 args.cno = ii->i_cno; 626 args.type = ii->i_type | NILFS_I_TYPE_BTNC; 627 628 btnc_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test, 629 nilfs_iget_set, &args); 630 if (unlikely(!btnc_inode)) 631 return -ENOMEM; 632 if (inode_state_read_once(btnc_inode) & I_NEW) { 633 nilfs_init_btnc_inode(btnc_inode); 634 unlock_new_inode(btnc_inode); 635 } 636 NILFS_I(btnc_inode)->i_assoc_inode = inode; 637 NILFS_I(btnc_inode)->i_bmap = ii->i_bmap; 638 ii->i_assoc_inode = btnc_inode; 639 640 return 0; 641 } 642 643 /** 644 * nilfs_detach_btree_node_cache - detach the B-tree node cache from the inode 645 * @inode: inode object 646 * 647 * nilfs_detach_btree_node_cache() detaches the B-tree node cache and its 648 * holder inode bound to @inode, or does nothing if @inode doesn't have it. 649 */ 650 void nilfs_detach_btree_node_cache(struct inode *inode) 651 { 652 struct nilfs_inode_info *ii = NILFS_I(inode); 653 struct inode *btnc_inode = ii->i_assoc_inode; 654 655 if (btnc_inode) { 656 NILFS_I(btnc_inode)->i_assoc_inode = NULL; 657 ii->i_assoc_inode = NULL; 658 iput(btnc_inode); 659 } 660 } 661 662 /** 663 * nilfs_iget_for_shadow - obtain inode for shadow mapping 664 * @inode: inode object that uses shadow mapping 665 * 666 * nilfs_iget_for_shadow() allocates a pair of inodes that holds page 667 * caches for shadow mapping. The page cache for data pages is set up 668 * in one inode and the one for b-tree node pages is set up in the 669 * other inode, which is attached to the former inode. 670 * 671 * Return: a pointer to the inode for data pages on success, or %-ENOMEM 672 * if memory is insufficient. 673 */ 674 struct inode *nilfs_iget_for_shadow(struct inode *inode) 675 { 676 struct nilfs_iget_args args = { 677 .ino = inode->i_ino, .root = NULL, .cno = 0, 678 .type = NILFS_I_TYPE_SHADOW 679 }; 680 struct inode *s_inode; 681 int err; 682 683 s_inode = iget5_locked(inode->i_sb, inode->i_ino, nilfs_iget_test, 684 nilfs_iget_set, &args); 685 if (unlikely(!s_inode)) 686 return ERR_PTR(-ENOMEM); 687 if (!(inode_state_read_once(s_inode) & I_NEW)) 688 return inode; 689 690 NILFS_I(s_inode)->i_flags = 0; 691 memset(NILFS_I(s_inode)->i_bmap, 0, sizeof(struct nilfs_bmap)); 692 mapping_set_gfp_mask(s_inode->i_mapping, GFP_NOFS); 693 s_inode->i_mapping->a_ops = &nilfs_buffer_cache_aops; 694 695 err = nilfs_attach_btree_node_cache(s_inode); 696 if (unlikely(err)) { 697 iget_failed(s_inode); 698 return ERR_PTR(err); 699 } 700 unlock_new_inode(s_inode); 701 return s_inode; 702 } 703 704 /** 705 * nilfs_write_inode_common - export common inode information to on-disk inode 706 * @inode: inode object 707 * @raw_inode: on-disk inode 708 * 709 * This function writes standard information from the on-memory inode @inode 710 * to @raw_inode on ifile, cpfile or a super root block. Since inode bmap 711 * data is not exported, nilfs_bmap_write() must be called separately during 712 * log writing. 713 */ 714 void nilfs_write_inode_common(struct inode *inode, 715 struct nilfs_inode *raw_inode) 716 { 717 struct nilfs_inode_info *ii = NILFS_I(inode); 718 719 raw_inode->i_mode = cpu_to_le16(inode->i_mode); 720 raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); 721 raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); 722 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 723 raw_inode->i_size = cpu_to_le64(inode->i_size); 724 raw_inode->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); 725 raw_inode->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode)); 726 raw_inode->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); 727 raw_inode->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode)); 728 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 729 730 raw_inode->i_flags = cpu_to_le32(ii->i_flags); 731 raw_inode->i_generation = cpu_to_le32(inode->i_generation); 732 733 /* 734 * When extending inode, nilfs->ns_inode_size should be checked 735 * for substitutions of appended fields. 736 */ 737 } 738 739 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh, int flags) 740 { 741 u64 ino = inode->i_ino; 742 struct nilfs_inode_info *ii = NILFS_I(inode); 743 struct inode *ifile = ii->i_root->ifile; 744 struct nilfs_inode *raw_inode; 745 746 raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 747 748 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 749 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 750 if (flags & I_DIRTY_DATASYNC) 751 set_bit(NILFS_I_INODE_SYNC, &ii->i_state); 752 753 nilfs_write_inode_common(inode, raw_inode); 754 755 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 756 raw_inode->i_device_code = 757 cpu_to_le64(huge_encode_dev(inode->i_rdev)); 758 759 nilfs_ifile_unmap_inode(raw_inode); 760 } 761 762 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 763 764 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 765 unsigned long from) 766 { 767 __u64 b; 768 int ret; 769 770 if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 771 return; 772 repeat: 773 ret = nilfs_bmap_last_key(ii->i_bmap, &b); 774 if (ret == -ENOENT) 775 return; 776 else if (ret < 0) 777 goto failed; 778 779 if (b < from) 780 return; 781 782 b -= min_t(__u64, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 783 ret = nilfs_bmap_truncate(ii->i_bmap, b); 784 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 785 if (!ret || (ret == -ENOMEM && 786 nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 787 goto repeat; 788 789 failed: 790 nilfs_warn(ii->vfs_inode.i_sb, "error %d truncating bmap (ino=%llu)", 791 ret, ii->vfs_inode.i_ino); 792 } 793 794 void nilfs_truncate(struct inode *inode) 795 { 796 unsigned long blkoff; 797 unsigned int blocksize; 798 struct nilfs_transaction_info ti; 799 struct super_block *sb = inode->i_sb; 800 struct nilfs_inode_info *ii = NILFS_I(inode); 801 802 if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 803 return; 804 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 805 return; 806 807 blocksize = sb->s_blocksize; 808 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 809 nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 810 811 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 812 813 nilfs_truncate_bmap(ii, blkoff); 814 815 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 816 if (IS_SYNC(inode)) 817 nilfs_set_transaction_flag(NILFS_TI_SYNC); 818 819 nilfs_mark_inode_dirty(inode); 820 nilfs_set_file_dirty(inode, 0); 821 nilfs_transaction_commit(sb); 822 /* 823 * May construct a logical segment and may fail in sync mode. 824 * But truncate has no return value. 825 */ 826 } 827 828 static void nilfs_clear_inode(struct inode *inode) 829 { 830 struct nilfs_inode_info *ii = NILFS_I(inode); 831 832 /* 833 * Free resources allocated in nilfs_read_inode(), here. 834 */ 835 BUG_ON(!list_empty(&ii->i_dirty)); 836 brelse(ii->i_bh); 837 ii->i_bh = NULL; 838 839 if (nilfs_is_metadata_file_inode(inode)) 840 nilfs_mdt_clear(inode); 841 842 if (test_bit(NILFS_I_BMAP, &ii->i_state)) 843 nilfs_bmap_clear(ii->i_bmap); 844 845 if (!(ii->i_type & NILFS_I_TYPE_BTNC)) 846 nilfs_detach_btree_node_cache(inode); 847 848 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 849 nilfs_put_root(ii->i_root); 850 } 851 852 void nilfs_evict_inode(struct inode *inode) 853 { 854 struct nilfs_transaction_info ti; 855 struct super_block *sb = inode->i_sb; 856 struct nilfs_inode_info *ii = NILFS_I(inode); 857 struct the_nilfs *nilfs; 858 int ret; 859 860 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 861 truncate_inode_pages_final(&inode->i_data); 862 clear_inode(inode); 863 nilfs_clear_inode(inode); 864 return; 865 } 866 nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 867 868 truncate_inode_pages_final(&inode->i_data); 869 870 nilfs = sb->s_fs_info; 871 if (unlikely(sb_rdonly(sb) || !nilfs->ns_writer)) { 872 /* 873 * If this inode is about to be disposed after the file system 874 * has been degraded to read-only due to file system corruption 875 * or after the writer has been detached, do not make any 876 * changes that cause writes, just clear it. 877 * Do this check after read-locking ns_segctor_sem by 878 * nilfs_transaction_begin() in order to avoid a race with 879 * the writer detach operation. 880 */ 881 clear_inode(inode); 882 nilfs_clear_inode(inode); 883 nilfs_transaction_abort(sb); 884 return; 885 } 886 887 /* TODO: some of the following operations may fail. */ 888 nilfs_truncate_bmap(ii, 0); 889 nilfs_mark_inode_dirty(inode); 890 clear_inode(inode); 891 892 ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 893 if (!ret) 894 atomic64_dec(&ii->i_root->inodes_count); 895 896 nilfs_clear_inode(inode); 897 898 if (IS_SYNC(inode)) 899 nilfs_set_transaction_flag(NILFS_TI_SYNC); 900 nilfs_transaction_commit(sb); 901 /* 902 * May construct a logical segment and may fail in sync mode. 903 * But delete_inode has no return value. 904 */ 905 } 906 907 int nilfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 908 struct iattr *iattr) 909 { 910 struct nilfs_transaction_info ti; 911 struct inode *inode = d_inode(dentry); 912 struct super_block *sb = inode->i_sb; 913 int err; 914 915 err = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 916 if (err) 917 return err; 918 919 err = nilfs_transaction_begin(sb, &ti, 0); 920 if (unlikely(err)) 921 return err; 922 923 if ((iattr->ia_valid & ATTR_SIZE) && 924 iattr->ia_size != i_size_read(inode)) { 925 inode_dio_wait(inode); 926 truncate_setsize(inode, iattr->ia_size); 927 nilfs_truncate(inode); 928 } 929 930 setattr_copy(&nop_mnt_idmap, inode, iattr); 931 mark_inode_dirty(inode); 932 933 if (iattr->ia_valid & ATTR_MODE) { 934 err = nilfs_acl_chmod(inode); 935 if (unlikely(err)) 936 goto out_err; 937 } 938 939 return nilfs_transaction_commit(sb); 940 941 out_err: 942 nilfs_transaction_abort(sb); 943 return err; 944 } 945 946 int nilfs_permission(struct mnt_idmap *idmap, struct inode *inode, 947 int mask) 948 { 949 struct nilfs_root *root = NILFS_I(inode)->i_root; 950 951 if ((mask & MAY_WRITE) && root && 952 root->cno != NILFS_CPTREE_CURRENT_CNO) 953 return -EROFS; /* snapshot is not writable */ 954 955 return generic_permission(&nop_mnt_idmap, inode, mask); 956 } 957 958 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) 959 { 960 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 961 struct nilfs_inode_info *ii = NILFS_I(inode); 962 int err; 963 964 spin_lock(&nilfs->ns_inode_lock); 965 if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) { 966 spin_unlock(&nilfs->ns_inode_lock); 967 err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 968 inode->i_ino, pbh); 969 if (unlikely(err)) 970 return err; 971 spin_lock(&nilfs->ns_inode_lock); 972 if (ii->i_bh == NULL) 973 ii->i_bh = *pbh; 974 else if (unlikely(!buffer_uptodate(ii->i_bh))) { 975 __brelse(ii->i_bh); 976 ii->i_bh = *pbh; 977 } else { 978 brelse(*pbh); 979 *pbh = ii->i_bh; 980 } 981 } else 982 *pbh = ii->i_bh; 983 984 get_bh(*pbh); 985 spin_unlock(&nilfs->ns_inode_lock); 986 return 0; 987 } 988 989 int nilfs_inode_dirty(struct inode *inode) 990 { 991 struct nilfs_inode_info *ii = NILFS_I(inode); 992 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 993 int ret = 0; 994 995 if (!list_empty(&ii->i_dirty)) { 996 spin_lock(&nilfs->ns_inode_lock); 997 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 998 test_bit(NILFS_I_BUSY, &ii->i_state); 999 spin_unlock(&nilfs->ns_inode_lock); 1000 } 1001 return ret; 1002 } 1003 1004 int nilfs_set_file_dirty(struct inode *inode, unsigned int nr_dirty) 1005 { 1006 struct nilfs_inode_info *ii = NILFS_I(inode); 1007 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 1008 1009 atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); 1010 1011 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 1012 return 0; 1013 1014 spin_lock(&nilfs->ns_inode_lock); 1015 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 1016 !test_bit(NILFS_I_BUSY, &ii->i_state)) { 1017 /* 1018 * Because this routine may race with nilfs_dispose_list(), 1019 * we have to check NILFS_I_QUEUED here, too. 1020 */ 1021 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 1022 /* 1023 * This will happen when somebody is freeing 1024 * this inode. 1025 */ 1026 nilfs_warn(inode->i_sb, 1027 "cannot set file dirty (ino=%llu): the file is being freed", 1028 inode->i_ino); 1029 spin_unlock(&nilfs->ns_inode_lock); 1030 return -EINVAL; /* 1031 * NILFS_I_DIRTY may remain for 1032 * freeing inode. 1033 */ 1034 } 1035 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); 1036 set_bit(NILFS_I_QUEUED, &ii->i_state); 1037 } 1038 spin_unlock(&nilfs->ns_inode_lock); 1039 return 0; 1040 } 1041 1042 int __nilfs_mark_inode_dirty(struct inode *inode, int flags) 1043 { 1044 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 1045 struct buffer_head *ibh; 1046 int err; 1047 1048 /* 1049 * Do not dirty inodes after the log writer has been detached 1050 * and its nilfs_root struct has been freed. 1051 */ 1052 if (unlikely(nilfs_purging(nilfs))) 1053 return 0; 1054 1055 err = nilfs_load_inode_block(inode, &ibh); 1056 if (unlikely(err)) { 1057 nilfs_warn(inode->i_sb, 1058 "cannot mark inode dirty (ino=%llu): error %d loading inode block", 1059 inode->i_ino, err); 1060 return err; 1061 } 1062 nilfs_update_inode(inode, ibh, flags); 1063 mark_buffer_dirty(ibh); 1064 nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 1065 brelse(ibh); 1066 return 0; 1067 } 1068 1069 /** 1070 * nilfs_dirty_inode - reflect changes on given inode to an inode block. 1071 * @inode: inode of the file to be registered. 1072 * @flags: flags to determine the dirty state of the inode 1073 * 1074 * nilfs_dirty_inode() loads a inode block containing the specified 1075 * @inode and copies data from a nilfs_inode to a corresponding inode 1076 * entry in the inode block. This operation is excluded from the segment 1077 * construction. This function can be called both as a single operation 1078 * and as a part of indivisible file operations. 1079 */ 1080 void nilfs_dirty_inode(struct inode *inode, int flags) 1081 { 1082 struct nilfs_transaction_info ti; 1083 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 1084 1085 if (is_bad_inode(inode)) { 1086 nilfs_warn(inode->i_sb, 1087 "tried to mark bad_inode dirty. ignored."); 1088 dump_stack(); 1089 return; 1090 } 1091 if (mdi) { 1092 nilfs_mdt_mark_dirty(inode); 1093 return; 1094 } 1095 nilfs_transaction_begin(inode->i_sb, &ti, 0); 1096 __nilfs_mark_inode_dirty(inode, flags); 1097 nilfs_transaction_commit(inode->i_sb); /* never fails */ 1098 } 1099 1100 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1101 __u64 start, __u64 len) 1102 { 1103 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 1104 __u64 logical = 0, phys = 0, size = 0; 1105 __u32 flags = 0; 1106 loff_t isize; 1107 sector_t blkoff, end_blkoff; 1108 sector_t delalloc_blkoff; 1109 unsigned long delalloc_blklen; 1110 unsigned int blkbits = inode->i_blkbits; 1111 int ret, n; 1112 1113 ret = fiemap_prep(inode, fieinfo, start, &len, 0); 1114 if (ret) 1115 return ret; 1116 1117 inode_lock(inode); 1118 1119 isize = i_size_read(inode); 1120 1121 blkoff = start >> blkbits; 1122 end_blkoff = (start + len - 1) >> blkbits; 1123 1124 delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, 1125 &delalloc_blkoff); 1126 1127 do { 1128 __u64 blkphy; 1129 unsigned int maxblocks; 1130 1131 if (delalloc_blklen && blkoff == delalloc_blkoff) { 1132 if (size) { 1133 /* End of the current extent */ 1134 ret = fiemap_fill_next_extent( 1135 fieinfo, logical, phys, size, flags); 1136 if (ret) 1137 break; 1138 } 1139 if (blkoff > end_blkoff) 1140 break; 1141 1142 flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; 1143 logical = blkoff << blkbits; 1144 phys = 0; 1145 size = delalloc_blklen << blkbits; 1146 1147 blkoff = delalloc_blkoff + delalloc_blklen; 1148 delalloc_blklen = nilfs_find_uncommitted_extent( 1149 inode, blkoff, &delalloc_blkoff); 1150 continue; 1151 } 1152 1153 /* 1154 * Limit the number of blocks that we look up so as 1155 * not to get into the next delayed allocation extent. 1156 */ 1157 maxblocks = INT_MAX; 1158 if (delalloc_blklen) 1159 maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, 1160 maxblocks); 1161 blkphy = 0; 1162 1163 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1164 n = nilfs_bmap_lookup_contig( 1165 NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); 1166 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1167 1168 if (n < 0) { 1169 int past_eof; 1170 1171 if (unlikely(n != -ENOENT)) 1172 break; /* error */ 1173 1174 /* HOLE */ 1175 blkoff++; 1176 past_eof = ((blkoff << blkbits) >= isize); 1177 1178 if (size) { 1179 /* End of the current extent */ 1180 1181 if (past_eof) 1182 flags |= FIEMAP_EXTENT_LAST; 1183 1184 ret = fiemap_fill_next_extent( 1185 fieinfo, logical, phys, size, flags); 1186 if (ret) 1187 break; 1188 size = 0; 1189 } 1190 if (blkoff > end_blkoff || past_eof) 1191 break; 1192 } else { 1193 if (size) { 1194 if (phys && blkphy << blkbits == phys + size) { 1195 /* The current extent goes on */ 1196 size += (u64)n << blkbits; 1197 } else { 1198 /* Terminate the current extent */ 1199 ret = fiemap_fill_next_extent( 1200 fieinfo, logical, phys, size, 1201 flags); 1202 if (ret || blkoff > end_blkoff) 1203 break; 1204 1205 /* Start another extent */ 1206 flags = FIEMAP_EXTENT_MERGED; 1207 logical = blkoff << blkbits; 1208 phys = blkphy << blkbits; 1209 size = (u64)n << blkbits; 1210 } 1211 } else { 1212 /* Start a new extent */ 1213 flags = FIEMAP_EXTENT_MERGED; 1214 logical = blkoff << blkbits; 1215 phys = blkphy << blkbits; 1216 size = (u64)n << blkbits; 1217 } 1218 blkoff += n; 1219 } 1220 cond_resched(); 1221 } while (true); 1222 1223 /* If ret is 1 then we just hit the end of the extent array */ 1224 if (ret == 1) 1225 ret = 0; 1226 1227 inode_unlock(inode); 1228 return ret; 1229 } 1230