1 /* 2 * inode.c - NILFS inode operations. 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/gfp.h> 26 #include <linux/mpage.h> 27 #include <linux/writeback.h> 28 #include <linux/aio.h> 29 #include "nilfs.h" 30 #include "btnode.h" 31 #include "segment.h" 32 #include "page.h" 33 #include "mdt.h" 34 #include "cpfile.h" 35 #include "ifile.h" 36 37 /** 38 * struct nilfs_iget_args - arguments used during comparison between inodes 39 * @ino: inode number 40 * @cno: checkpoint number 41 * @root: pointer on NILFS root object (mounted checkpoint) 42 * @for_gc: inode for GC flag 43 */ 44 struct nilfs_iget_args { 45 u64 ino; 46 __u64 cno; 47 struct nilfs_root *root; 48 int for_gc; 49 }; 50 51 void nilfs_inode_add_blocks(struct inode *inode, int n) 52 { 53 struct nilfs_root *root = NILFS_I(inode)->i_root; 54 55 inode_add_bytes(inode, (1 << inode->i_blkbits) * n); 56 if (root) 57 atomic_add(n, &root->blocks_count); 58 } 59 60 void nilfs_inode_sub_blocks(struct inode *inode, int n) 61 { 62 struct nilfs_root *root = NILFS_I(inode)->i_root; 63 64 inode_sub_bytes(inode, (1 << inode->i_blkbits) * n); 65 if (root) 66 atomic_sub(n, &root->blocks_count); 67 } 68 69 /** 70 * nilfs_get_block() - get a file block on the filesystem (callback function) 71 * @inode - inode struct of the target file 72 * @blkoff - file block number 73 * @bh_result - buffer head to be mapped on 74 * @create - indicate whether allocating the block or not when it has not 75 * been allocated yet. 76 * 77 * This function does not issue actual read request of the specified data 78 * block. It is done by VFS. 79 */ 80 int nilfs_get_block(struct inode *inode, sector_t blkoff, 81 struct buffer_head *bh_result, int create) 82 { 83 struct nilfs_inode_info *ii = NILFS_I(inode); 84 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 85 __u64 blknum = 0; 86 int err = 0, ret; 87 unsigned maxblocks = bh_result->b_size >> inode->i_blkbits; 88 89 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 90 ret = nilfs_bmap_lookup_contig(ii->i_bmap, blkoff, &blknum, maxblocks); 91 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 92 if (ret >= 0) { /* found */ 93 map_bh(bh_result, inode->i_sb, blknum); 94 if (ret > 0) 95 bh_result->b_size = (ret << inode->i_blkbits); 96 goto out; 97 } 98 /* data block was not found */ 99 if (ret == -ENOENT && create) { 100 struct nilfs_transaction_info ti; 101 102 bh_result->b_blocknr = 0; 103 err = nilfs_transaction_begin(inode->i_sb, &ti, 1); 104 if (unlikely(err)) 105 goto out; 106 err = nilfs_bmap_insert(ii->i_bmap, (unsigned long)blkoff, 107 (unsigned long)bh_result); 108 if (unlikely(err != 0)) { 109 if (err == -EEXIST) { 110 /* 111 * The get_block() function could be called 112 * from multiple callers for an inode. 113 * However, the page having this block must 114 * be locked in this case. 115 */ 116 printk(KERN_WARNING 117 "nilfs_get_block: a race condition " 118 "while inserting a data block. " 119 "(inode number=%lu, file block " 120 "offset=%llu)\n", 121 inode->i_ino, 122 (unsigned long long)blkoff); 123 err = 0; 124 } 125 nilfs_transaction_abort(inode->i_sb); 126 goto out; 127 } 128 nilfs_mark_inode_dirty(inode); 129 nilfs_transaction_commit(inode->i_sb); /* never fails */ 130 /* Error handling should be detailed */ 131 set_buffer_new(bh_result); 132 set_buffer_delay(bh_result); 133 map_bh(bh_result, inode->i_sb, 0); /* dbn must be changed 134 to proper value */ 135 } else if (ret == -ENOENT) { 136 /* not found is not error (e.g. hole); must return without 137 the mapped state flag. */ 138 ; 139 } else { 140 err = ret; 141 } 142 143 out: 144 return err; 145 } 146 147 /** 148 * nilfs_readpage() - implement readpage() method of nilfs_aops {} 149 * address_space_operations. 150 * @file - file struct of the file to be read 151 * @page - the page to be read 152 */ 153 static int nilfs_readpage(struct file *file, struct page *page) 154 { 155 return mpage_readpage(page, nilfs_get_block); 156 } 157 158 /** 159 * nilfs_readpages() - implement readpages() method of nilfs_aops {} 160 * address_space_operations. 161 * @file - file struct of the file to be read 162 * @mapping - address_space struct used for reading multiple pages 163 * @pages - the pages to be read 164 * @nr_pages - number of pages to be read 165 */ 166 static int nilfs_readpages(struct file *file, struct address_space *mapping, 167 struct list_head *pages, unsigned nr_pages) 168 { 169 return mpage_readpages(mapping, pages, nr_pages, nilfs_get_block); 170 } 171 172 static int nilfs_writepages(struct address_space *mapping, 173 struct writeback_control *wbc) 174 { 175 struct inode *inode = mapping->host; 176 int err = 0; 177 178 if (inode->i_sb->s_flags & MS_RDONLY) { 179 nilfs_clear_dirty_pages(mapping, false); 180 return -EROFS; 181 } 182 183 if (wbc->sync_mode == WB_SYNC_ALL) 184 err = nilfs_construct_dsync_segment(inode->i_sb, inode, 185 wbc->range_start, 186 wbc->range_end); 187 return err; 188 } 189 190 static int nilfs_writepage(struct page *page, struct writeback_control *wbc) 191 { 192 struct inode *inode = page->mapping->host; 193 int err; 194 195 if (inode->i_sb->s_flags & MS_RDONLY) { 196 /* 197 * It means that filesystem was remounted in read-only 198 * mode because of error or metadata corruption. But we 199 * have dirty pages that try to be flushed in background. 200 * So, here we simply discard this dirty page. 201 */ 202 nilfs_clear_dirty_page(page, false); 203 unlock_page(page); 204 return -EROFS; 205 } 206 207 redirty_page_for_writepage(wbc, page); 208 unlock_page(page); 209 210 if (wbc->sync_mode == WB_SYNC_ALL) { 211 err = nilfs_construct_segment(inode->i_sb); 212 if (unlikely(err)) 213 return err; 214 } else if (wbc->for_reclaim) 215 nilfs_flush_segment(inode->i_sb, inode->i_ino); 216 217 return 0; 218 } 219 220 static int nilfs_set_page_dirty(struct page *page) 221 { 222 int ret = __set_page_dirty_buffers(page); 223 224 if (ret) { 225 struct inode *inode = page->mapping->host; 226 unsigned nr_dirty = 1 << (PAGE_SHIFT - inode->i_blkbits); 227 228 nilfs_set_file_dirty(inode, nr_dirty); 229 } 230 return ret; 231 } 232 233 void nilfs_write_failed(struct address_space *mapping, loff_t to) 234 { 235 struct inode *inode = mapping->host; 236 237 if (to > inode->i_size) { 238 truncate_pagecache(inode, to, inode->i_size); 239 nilfs_truncate(inode); 240 } 241 } 242 243 static int nilfs_write_begin(struct file *file, struct address_space *mapping, 244 loff_t pos, unsigned len, unsigned flags, 245 struct page **pagep, void **fsdata) 246 247 { 248 struct inode *inode = mapping->host; 249 int err = nilfs_transaction_begin(inode->i_sb, NULL, 1); 250 251 if (unlikely(err)) 252 return err; 253 254 err = block_write_begin(mapping, pos, len, flags, pagep, 255 nilfs_get_block); 256 if (unlikely(err)) { 257 nilfs_write_failed(mapping, pos + len); 258 nilfs_transaction_abort(inode->i_sb); 259 } 260 return err; 261 } 262 263 static int nilfs_write_end(struct file *file, struct address_space *mapping, 264 loff_t pos, unsigned len, unsigned copied, 265 struct page *page, void *fsdata) 266 { 267 struct inode *inode = mapping->host; 268 unsigned start = pos & (PAGE_CACHE_SIZE - 1); 269 unsigned nr_dirty; 270 int err; 271 272 nr_dirty = nilfs_page_count_clean_buffers(page, start, 273 start + copied); 274 copied = generic_write_end(file, mapping, pos, len, copied, page, 275 fsdata); 276 nilfs_set_file_dirty(inode, nr_dirty); 277 err = nilfs_transaction_commit(inode->i_sb); 278 return err ? : copied; 279 } 280 281 static ssize_t 282 nilfs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov, 283 loff_t offset, unsigned long nr_segs) 284 { 285 struct file *file = iocb->ki_filp; 286 struct address_space *mapping = file->f_mapping; 287 struct inode *inode = file->f_mapping->host; 288 ssize_t size; 289 290 if (rw == WRITE) 291 return 0; 292 293 /* Needs synchronization with the cleaner */ 294 size = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, 295 nilfs_get_block); 296 297 /* 298 * In case of error extending write may have instantiated a few 299 * blocks outside i_size. Trim these off again. 300 */ 301 if (unlikely((rw & WRITE) && size < 0)) { 302 loff_t isize = i_size_read(inode); 303 loff_t end = offset + iov_length(iov, nr_segs); 304 305 if (end > isize) 306 nilfs_write_failed(mapping, end); 307 } 308 309 return size; 310 } 311 312 const struct address_space_operations nilfs_aops = { 313 .writepage = nilfs_writepage, 314 .readpage = nilfs_readpage, 315 .writepages = nilfs_writepages, 316 .set_page_dirty = nilfs_set_page_dirty, 317 .readpages = nilfs_readpages, 318 .write_begin = nilfs_write_begin, 319 .write_end = nilfs_write_end, 320 /* .releasepage = nilfs_releasepage, */ 321 .invalidatepage = block_invalidatepage, 322 .direct_IO = nilfs_direct_IO, 323 .is_partially_uptodate = block_is_partially_uptodate, 324 }; 325 326 struct inode *nilfs_new_inode(struct inode *dir, umode_t mode) 327 { 328 struct super_block *sb = dir->i_sb; 329 struct the_nilfs *nilfs = sb->s_fs_info; 330 struct inode *inode; 331 struct nilfs_inode_info *ii; 332 struct nilfs_root *root; 333 int err = -ENOMEM; 334 ino_t ino; 335 336 inode = new_inode(sb); 337 if (unlikely(!inode)) 338 goto failed; 339 340 mapping_set_gfp_mask(inode->i_mapping, 341 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 342 343 root = NILFS_I(dir)->i_root; 344 ii = NILFS_I(inode); 345 ii->i_state = 1 << NILFS_I_NEW; 346 ii->i_root = root; 347 348 err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh); 349 if (unlikely(err)) 350 goto failed_ifile_create_inode; 351 /* reference count of i_bh inherits from nilfs_mdt_read_block() */ 352 353 atomic_inc(&root->inodes_count); 354 inode_init_owner(inode, dir, mode); 355 inode->i_ino = ino; 356 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 357 358 if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)) { 359 err = nilfs_bmap_read(ii->i_bmap, NULL); 360 if (err < 0) 361 goto failed_bmap; 362 363 set_bit(NILFS_I_BMAP, &ii->i_state); 364 /* No lock is needed; iget() ensures it. */ 365 } 366 367 ii->i_flags = nilfs_mask_flags( 368 mode, NILFS_I(dir)->i_flags & NILFS_FL_INHERITED); 369 370 /* ii->i_file_acl = 0; */ 371 /* ii->i_dir_acl = 0; */ 372 ii->i_dir_start_lookup = 0; 373 nilfs_set_inode_flags(inode); 374 spin_lock(&nilfs->ns_next_gen_lock); 375 inode->i_generation = nilfs->ns_next_generation++; 376 spin_unlock(&nilfs->ns_next_gen_lock); 377 insert_inode_hash(inode); 378 379 err = nilfs_init_acl(inode, dir); 380 if (unlikely(err)) 381 goto failed_acl; /* never occur. When supporting 382 nilfs_init_acl(), proper cancellation of 383 above jobs should be considered */ 384 385 return inode; 386 387 failed_acl: 388 failed_bmap: 389 clear_nlink(inode); 390 iput(inode); /* raw_inode will be deleted through 391 generic_delete_inode() */ 392 goto failed; 393 394 failed_ifile_create_inode: 395 make_bad_inode(inode); 396 iput(inode); /* if i_nlink == 1, generic_forget_inode() will be 397 called */ 398 failed: 399 return ERR_PTR(err); 400 } 401 402 void nilfs_set_inode_flags(struct inode *inode) 403 { 404 unsigned int flags = NILFS_I(inode)->i_flags; 405 406 inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | 407 S_DIRSYNC); 408 if (flags & FS_SYNC_FL) 409 inode->i_flags |= S_SYNC; 410 if (flags & FS_APPEND_FL) 411 inode->i_flags |= S_APPEND; 412 if (flags & FS_IMMUTABLE_FL) 413 inode->i_flags |= S_IMMUTABLE; 414 if (flags & FS_NOATIME_FL) 415 inode->i_flags |= S_NOATIME; 416 if (flags & FS_DIRSYNC_FL) 417 inode->i_flags |= S_DIRSYNC; 418 mapping_set_gfp_mask(inode->i_mapping, 419 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 420 } 421 422 int nilfs_read_inode_common(struct inode *inode, 423 struct nilfs_inode *raw_inode) 424 { 425 struct nilfs_inode_info *ii = NILFS_I(inode); 426 int err; 427 428 inode->i_mode = le16_to_cpu(raw_inode->i_mode); 429 i_uid_write(inode, le32_to_cpu(raw_inode->i_uid)); 430 i_gid_write(inode, le32_to_cpu(raw_inode->i_gid)); 431 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 432 inode->i_size = le64_to_cpu(raw_inode->i_size); 433 inode->i_atime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 434 inode->i_ctime.tv_sec = le64_to_cpu(raw_inode->i_ctime); 435 inode->i_mtime.tv_sec = le64_to_cpu(raw_inode->i_mtime); 436 inode->i_atime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 437 inode->i_ctime.tv_nsec = le32_to_cpu(raw_inode->i_ctime_nsec); 438 inode->i_mtime.tv_nsec = le32_to_cpu(raw_inode->i_mtime_nsec); 439 if (inode->i_nlink == 0 && inode->i_mode == 0) 440 return -EINVAL; /* this inode is deleted */ 441 442 inode->i_blocks = le64_to_cpu(raw_inode->i_blocks); 443 ii->i_flags = le32_to_cpu(raw_inode->i_flags); 444 #if 0 445 ii->i_file_acl = le32_to_cpu(raw_inode->i_file_acl); 446 ii->i_dir_acl = S_ISREG(inode->i_mode) ? 447 0 : le32_to_cpu(raw_inode->i_dir_acl); 448 #endif 449 ii->i_dir_start_lookup = 0; 450 inode->i_generation = le32_to_cpu(raw_inode->i_generation); 451 452 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 453 S_ISLNK(inode->i_mode)) { 454 err = nilfs_bmap_read(ii->i_bmap, raw_inode); 455 if (err < 0) 456 return err; 457 set_bit(NILFS_I_BMAP, &ii->i_state); 458 /* No lock is needed; iget() ensures it. */ 459 } 460 return 0; 461 } 462 463 static int __nilfs_read_inode(struct super_block *sb, 464 struct nilfs_root *root, unsigned long ino, 465 struct inode *inode) 466 { 467 struct the_nilfs *nilfs = sb->s_fs_info; 468 struct buffer_head *bh; 469 struct nilfs_inode *raw_inode; 470 int err; 471 472 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 473 err = nilfs_ifile_get_inode_block(root->ifile, ino, &bh); 474 if (unlikely(err)) 475 goto bad_inode; 476 477 raw_inode = nilfs_ifile_map_inode(root->ifile, ino, bh); 478 479 err = nilfs_read_inode_common(inode, raw_inode); 480 if (err) 481 goto failed_unmap; 482 483 if (S_ISREG(inode->i_mode)) { 484 inode->i_op = &nilfs_file_inode_operations; 485 inode->i_fop = &nilfs_file_operations; 486 inode->i_mapping->a_ops = &nilfs_aops; 487 } else if (S_ISDIR(inode->i_mode)) { 488 inode->i_op = &nilfs_dir_inode_operations; 489 inode->i_fop = &nilfs_dir_operations; 490 inode->i_mapping->a_ops = &nilfs_aops; 491 } else if (S_ISLNK(inode->i_mode)) { 492 inode->i_op = &nilfs_symlink_inode_operations; 493 inode->i_mapping->a_ops = &nilfs_aops; 494 } else { 495 inode->i_op = &nilfs_special_inode_operations; 496 init_special_inode( 497 inode, inode->i_mode, 498 huge_decode_dev(le64_to_cpu(raw_inode->i_device_code))); 499 } 500 nilfs_ifile_unmap_inode(root->ifile, ino, bh); 501 brelse(bh); 502 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 503 nilfs_set_inode_flags(inode); 504 return 0; 505 506 failed_unmap: 507 nilfs_ifile_unmap_inode(root->ifile, ino, bh); 508 brelse(bh); 509 510 bad_inode: 511 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 512 return err; 513 } 514 515 static int nilfs_iget_test(struct inode *inode, void *opaque) 516 { 517 struct nilfs_iget_args *args = opaque; 518 struct nilfs_inode_info *ii; 519 520 if (args->ino != inode->i_ino || args->root != NILFS_I(inode)->i_root) 521 return 0; 522 523 ii = NILFS_I(inode); 524 if (!test_bit(NILFS_I_GCINODE, &ii->i_state)) 525 return !args->for_gc; 526 527 return args->for_gc && args->cno == ii->i_cno; 528 } 529 530 static int nilfs_iget_set(struct inode *inode, void *opaque) 531 { 532 struct nilfs_iget_args *args = opaque; 533 534 inode->i_ino = args->ino; 535 if (args->for_gc) { 536 NILFS_I(inode)->i_state = 1 << NILFS_I_GCINODE; 537 NILFS_I(inode)->i_cno = args->cno; 538 NILFS_I(inode)->i_root = NULL; 539 } else { 540 if (args->root && args->ino == NILFS_ROOT_INO) 541 nilfs_get_root(args->root); 542 NILFS_I(inode)->i_root = args->root; 543 } 544 return 0; 545 } 546 547 struct inode *nilfs_ilookup(struct super_block *sb, struct nilfs_root *root, 548 unsigned long ino) 549 { 550 struct nilfs_iget_args args = { 551 .ino = ino, .root = root, .cno = 0, .for_gc = 0 552 }; 553 554 return ilookup5(sb, ino, nilfs_iget_test, &args); 555 } 556 557 struct inode *nilfs_iget_locked(struct super_block *sb, struct nilfs_root *root, 558 unsigned long ino) 559 { 560 struct nilfs_iget_args args = { 561 .ino = ino, .root = root, .cno = 0, .for_gc = 0 562 }; 563 564 return iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 565 } 566 567 struct inode *nilfs_iget(struct super_block *sb, struct nilfs_root *root, 568 unsigned long ino) 569 { 570 struct inode *inode; 571 int err; 572 573 inode = nilfs_iget_locked(sb, root, ino); 574 if (unlikely(!inode)) 575 return ERR_PTR(-ENOMEM); 576 if (!(inode->i_state & I_NEW)) 577 return inode; 578 579 err = __nilfs_read_inode(sb, root, ino, inode); 580 if (unlikely(err)) { 581 iget_failed(inode); 582 return ERR_PTR(err); 583 } 584 unlock_new_inode(inode); 585 return inode; 586 } 587 588 struct inode *nilfs_iget_for_gc(struct super_block *sb, unsigned long ino, 589 __u64 cno) 590 { 591 struct nilfs_iget_args args = { 592 .ino = ino, .root = NULL, .cno = cno, .for_gc = 1 593 }; 594 struct inode *inode; 595 int err; 596 597 inode = iget5_locked(sb, ino, nilfs_iget_test, nilfs_iget_set, &args); 598 if (unlikely(!inode)) 599 return ERR_PTR(-ENOMEM); 600 if (!(inode->i_state & I_NEW)) 601 return inode; 602 603 err = nilfs_init_gcinode(inode); 604 if (unlikely(err)) { 605 iget_failed(inode); 606 return ERR_PTR(err); 607 } 608 unlock_new_inode(inode); 609 return inode; 610 } 611 612 void nilfs_write_inode_common(struct inode *inode, 613 struct nilfs_inode *raw_inode, int has_bmap) 614 { 615 struct nilfs_inode_info *ii = NILFS_I(inode); 616 617 raw_inode->i_mode = cpu_to_le16(inode->i_mode); 618 raw_inode->i_uid = cpu_to_le32(i_uid_read(inode)); 619 raw_inode->i_gid = cpu_to_le32(i_gid_read(inode)); 620 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 621 raw_inode->i_size = cpu_to_le64(inode->i_size); 622 raw_inode->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); 623 raw_inode->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec); 624 raw_inode->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 625 raw_inode->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec); 626 raw_inode->i_blocks = cpu_to_le64(inode->i_blocks); 627 628 raw_inode->i_flags = cpu_to_le32(ii->i_flags); 629 raw_inode->i_generation = cpu_to_le32(inode->i_generation); 630 631 if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) { 632 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 633 634 /* zero-fill unused portion in the case of super root block */ 635 raw_inode->i_xattr = 0; 636 raw_inode->i_pad = 0; 637 memset((void *)raw_inode + sizeof(*raw_inode), 0, 638 nilfs->ns_inode_size - sizeof(*raw_inode)); 639 } 640 641 if (has_bmap) 642 nilfs_bmap_write(ii->i_bmap, raw_inode); 643 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) 644 raw_inode->i_device_code = 645 cpu_to_le64(huge_encode_dev(inode->i_rdev)); 646 /* When extending inode, nilfs->ns_inode_size should be checked 647 for substitutions of appended fields */ 648 } 649 650 void nilfs_update_inode(struct inode *inode, struct buffer_head *ibh) 651 { 652 ino_t ino = inode->i_ino; 653 struct nilfs_inode_info *ii = NILFS_I(inode); 654 struct inode *ifile = ii->i_root->ifile; 655 struct nilfs_inode *raw_inode; 656 657 raw_inode = nilfs_ifile_map_inode(ifile, ino, ibh); 658 659 if (test_and_clear_bit(NILFS_I_NEW, &ii->i_state)) 660 memset(raw_inode, 0, NILFS_MDT(ifile)->mi_entry_size); 661 set_bit(NILFS_I_INODE_DIRTY, &ii->i_state); 662 663 nilfs_write_inode_common(inode, raw_inode, 0); 664 /* XXX: call with has_bmap = 0 is a workaround to avoid 665 deadlock of bmap. This delays update of i_bmap to just 666 before writing */ 667 nilfs_ifile_unmap_inode(ifile, ino, ibh); 668 } 669 670 #define NILFS_MAX_TRUNCATE_BLOCKS 16384 /* 64MB for 4KB block */ 671 672 static void nilfs_truncate_bmap(struct nilfs_inode_info *ii, 673 unsigned long from) 674 { 675 unsigned long b; 676 int ret; 677 678 if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 679 return; 680 repeat: 681 ret = nilfs_bmap_last_key(ii->i_bmap, &b); 682 if (ret == -ENOENT) 683 return; 684 else if (ret < 0) 685 goto failed; 686 687 if (b < from) 688 return; 689 690 b -= min_t(unsigned long, NILFS_MAX_TRUNCATE_BLOCKS, b - from); 691 ret = nilfs_bmap_truncate(ii->i_bmap, b); 692 nilfs_relax_pressure_in_lock(ii->vfs_inode.i_sb); 693 if (!ret || (ret == -ENOMEM && 694 nilfs_bmap_truncate(ii->i_bmap, b) == 0)) 695 goto repeat; 696 697 failed: 698 nilfs_warning(ii->vfs_inode.i_sb, __func__, 699 "failed to truncate bmap (ino=%lu, err=%d)", 700 ii->vfs_inode.i_ino, ret); 701 } 702 703 void nilfs_truncate(struct inode *inode) 704 { 705 unsigned long blkoff; 706 unsigned int blocksize; 707 struct nilfs_transaction_info ti; 708 struct super_block *sb = inode->i_sb; 709 struct nilfs_inode_info *ii = NILFS_I(inode); 710 711 if (!test_bit(NILFS_I_BMAP, &ii->i_state)) 712 return; 713 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 714 return; 715 716 blocksize = sb->s_blocksize; 717 blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits; 718 nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 719 720 block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block); 721 722 nilfs_truncate_bmap(ii, blkoff); 723 724 inode->i_mtime = inode->i_ctime = CURRENT_TIME; 725 if (IS_SYNC(inode)) 726 nilfs_set_transaction_flag(NILFS_TI_SYNC); 727 728 nilfs_mark_inode_dirty(inode); 729 nilfs_set_file_dirty(inode, 0); 730 nilfs_transaction_commit(sb); 731 /* May construct a logical segment and may fail in sync mode. 732 But truncate has no return value. */ 733 } 734 735 static void nilfs_clear_inode(struct inode *inode) 736 { 737 struct nilfs_inode_info *ii = NILFS_I(inode); 738 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 739 740 /* 741 * Free resources allocated in nilfs_read_inode(), here. 742 */ 743 BUG_ON(!list_empty(&ii->i_dirty)); 744 brelse(ii->i_bh); 745 ii->i_bh = NULL; 746 747 if (mdi && mdi->mi_palloc_cache) 748 nilfs_palloc_destroy_cache(inode); 749 750 if (test_bit(NILFS_I_BMAP, &ii->i_state)) 751 nilfs_bmap_clear(ii->i_bmap); 752 753 nilfs_btnode_cache_clear(&ii->i_btnode_cache); 754 755 if (ii->i_root && inode->i_ino == NILFS_ROOT_INO) 756 nilfs_put_root(ii->i_root); 757 } 758 759 void nilfs_evict_inode(struct inode *inode) 760 { 761 struct nilfs_transaction_info ti; 762 struct super_block *sb = inode->i_sb; 763 struct nilfs_inode_info *ii = NILFS_I(inode); 764 int ret; 765 766 if (inode->i_nlink || !ii->i_root || unlikely(is_bad_inode(inode))) { 767 if (inode->i_data.nrpages) 768 truncate_inode_pages(&inode->i_data, 0); 769 clear_inode(inode); 770 nilfs_clear_inode(inode); 771 return; 772 } 773 nilfs_transaction_begin(sb, &ti, 0); /* never fails */ 774 775 if (inode->i_data.nrpages) 776 truncate_inode_pages(&inode->i_data, 0); 777 778 /* TODO: some of the following operations may fail. */ 779 nilfs_truncate_bmap(ii, 0); 780 nilfs_mark_inode_dirty(inode); 781 clear_inode(inode); 782 783 ret = nilfs_ifile_delete_inode(ii->i_root->ifile, inode->i_ino); 784 if (!ret) 785 atomic_dec(&ii->i_root->inodes_count); 786 787 nilfs_clear_inode(inode); 788 789 if (IS_SYNC(inode)) 790 nilfs_set_transaction_flag(NILFS_TI_SYNC); 791 nilfs_transaction_commit(sb); 792 /* May construct a logical segment and may fail in sync mode. 793 But delete_inode has no return value. */ 794 } 795 796 int nilfs_setattr(struct dentry *dentry, struct iattr *iattr) 797 { 798 struct nilfs_transaction_info ti; 799 struct inode *inode = dentry->d_inode; 800 struct super_block *sb = inode->i_sb; 801 int err; 802 803 err = inode_change_ok(inode, iattr); 804 if (err) 805 return err; 806 807 err = nilfs_transaction_begin(sb, &ti, 0); 808 if (unlikely(err)) 809 return err; 810 811 if ((iattr->ia_valid & ATTR_SIZE) && 812 iattr->ia_size != i_size_read(inode)) { 813 inode_dio_wait(inode); 814 truncate_setsize(inode, iattr->ia_size); 815 nilfs_truncate(inode); 816 } 817 818 setattr_copy(inode, iattr); 819 mark_inode_dirty(inode); 820 821 if (iattr->ia_valid & ATTR_MODE) { 822 err = nilfs_acl_chmod(inode); 823 if (unlikely(err)) 824 goto out_err; 825 } 826 827 return nilfs_transaction_commit(sb); 828 829 out_err: 830 nilfs_transaction_abort(sb); 831 return err; 832 } 833 834 int nilfs_permission(struct inode *inode, int mask) 835 { 836 struct nilfs_root *root = NILFS_I(inode)->i_root; 837 if ((mask & MAY_WRITE) && root && 838 root->cno != NILFS_CPTREE_CURRENT_CNO) 839 return -EROFS; /* snapshot is not writable */ 840 841 return generic_permission(inode, mask); 842 } 843 844 int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh) 845 { 846 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 847 struct nilfs_inode_info *ii = NILFS_I(inode); 848 int err; 849 850 spin_lock(&nilfs->ns_inode_lock); 851 if (ii->i_bh == NULL) { 852 spin_unlock(&nilfs->ns_inode_lock); 853 err = nilfs_ifile_get_inode_block(ii->i_root->ifile, 854 inode->i_ino, pbh); 855 if (unlikely(err)) 856 return err; 857 spin_lock(&nilfs->ns_inode_lock); 858 if (ii->i_bh == NULL) 859 ii->i_bh = *pbh; 860 else { 861 brelse(*pbh); 862 *pbh = ii->i_bh; 863 } 864 } else 865 *pbh = ii->i_bh; 866 867 get_bh(*pbh); 868 spin_unlock(&nilfs->ns_inode_lock); 869 return 0; 870 } 871 872 int nilfs_inode_dirty(struct inode *inode) 873 { 874 struct nilfs_inode_info *ii = NILFS_I(inode); 875 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 876 int ret = 0; 877 878 if (!list_empty(&ii->i_dirty)) { 879 spin_lock(&nilfs->ns_inode_lock); 880 ret = test_bit(NILFS_I_DIRTY, &ii->i_state) || 881 test_bit(NILFS_I_BUSY, &ii->i_state); 882 spin_unlock(&nilfs->ns_inode_lock); 883 } 884 return ret; 885 } 886 887 int nilfs_set_file_dirty(struct inode *inode, unsigned nr_dirty) 888 { 889 struct nilfs_inode_info *ii = NILFS_I(inode); 890 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 891 892 atomic_add(nr_dirty, &nilfs->ns_ndirtyblks); 893 894 if (test_and_set_bit(NILFS_I_DIRTY, &ii->i_state)) 895 return 0; 896 897 spin_lock(&nilfs->ns_inode_lock); 898 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && 899 !test_bit(NILFS_I_BUSY, &ii->i_state)) { 900 /* Because this routine may race with nilfs_dispose_list(), 901 we have to check NILFS_I_QUEUED here, too. */ 902 if (list_empty(&ii->i_dirty) && igrab(inode) == NULL) { 903 /* This will happen when somebody is freeing 904 this inode. */ 905 nilfs_warning(inode->i_sb, __func__, 906 "cannot get inode (ino=%lu)\n", 907 inode->i_ino); 908 spin_unlock(&nilfs->ns_inode_lock); 909 return -EINVAL; /* NILFS_I_DIRTY may remain for 910 freeing inode */ 911 } 912 list_move_tail(&ii->i_dirty, &nilfs->ns_dirty_files); 913 set_bit(NILFS_I_QUEUED, &ii->i_state); 914 } 915 spin_unlock(&nilfs->ns_inode_lock); 916 return 0; 917 } 918 919 int nilfs_mark_inode_dirty(struct inode *inode) 920 { 921 struct buffer_head *ibh; 922 int err; 923 924 err = nilfs_load_inode_block(inode, &ibh); 925 if (unlikely(err)) { 926 nilfs_warning(inode->i_sb, __func__, 927 "failed to reget inode block.\n"); 928 return err; 929 } 930 nilfs_update_inode(inode, ibh); 931 mark_buffer_dirty(ibh); 932 nilfs_mdt_mark_dirty(NILFS_I(inode)->i_root->ifile); 933 brelse(ibh); 934 return 0; 935 } 936 937 /** 938 * nilfs_dirty_inode - reflect changes on given inode to an inode block. 939 * @inode: inode of the file to be registered. 940 * 941 * nilfs_dirty_inode() loads a inode block containing the specified 942 * @inode and copies data from a nilfs_inode to a corresponding inode 943 * entry in the inode block. This operation is excluded from the segment 944 * construction. This function can be called both as a single operation 945 * and as a part of indivisible file operations. 946 */ 947 void nilfs_dirty_inode(struct inode *inode, int flags) 948 { 949 struct nilfs_transaction_info ti; 950 struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 951 952 if (is_bad_inode(inode)) { 953 nilfs_warning(inode->i_sb, __func__, 954 "tried to mark bad_inode dirty. ignored.\n"); 955 dump_stack(); 956 return; 957 } 958 if (mdi) { 959 nilfs_mdt_mark_dirty(inode); 960 return; 961 } 962 nilfs_transaction_begin(inode->i_sb, &ti, 0); 963 nilfs_mark_inode_dirty(inode); 964 nilfs_transaction_commit(inode->i_sb); /* never fails */ 965 } 966 967 int nilfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 968 __u64 start, __u64 len) 969 { 970 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 971 __u64 logical = 0, phys = 0, size = 0; 972 __u32 flags = 0; 973 loff_t isize; 974 sector_t blkoff, end_blkoff; 975 sector_t delalloc_blkoff; 976 unsigned long delalloc_blklen; 977 unsigned int blkbits = inode->i_blkbits; 978 int ret, n; 979 980 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); 981 if (ret) 982 return ret; 983 984 mutex_lock(&inode->i_mutex); 985 986 isize = i_size_read(inode); 987 988 blkoff = start >> blkbits; 989 end_blkoff = (start + len - 1) >> blkbits; 990 991 delalloc_blklen = nilfs_find_uncommitted_extent(inode, blkoff, 992 &delalloc_blkoff); 993 994 do { 995 __u64 blkphy; 996 unsigned int maxblocks; 997 998 if (delalloc_blklen && blkoff == delalloc_blkoff) { 999 if (size) { 1000 /* End of the current extent */ 1001 ret = fiemap_fill_next_extent( 1002 fieinfo, logical, phys, size, flags); 1003 if (ret) 1004 break; 1005 } 1006 if (blkoff > end_blkoff) 1007 break; 1008 1009 flags = FIEMAP_EXTENT_MERGED | FIEMAP_EXTENT_DELALLOC; 1010 logical = blkoff << blkbits; 1011 phys = 0; 1012 size = delalloc_blklen << blkbits; 1013 1014 blkoff = delalloc_blkoff + delalloc_blklen; 1015 delalloc_blklen = nilfs_find_uncommitted_extent( 1016 inode, blkoff, &delalloc_blkoff); 1017 continue; 1018 } 1019 1020 /* 1021 * Limit the number of blocks that we look up so as 1022 * not to get into the next delayed allocation extent. 1023 */ 1024 maxblocks = INT_MAX; 1025 if (delalloc_blklen) 1026 maxblocks = min_t(sector_t, delalloc_blkoff - blkoff, 1027 maxblocks); 1028 blkphy = 0; 1029 1030 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1031 n = nilfs_bmap_lookup_contig( 1032 NILFS_I(inode)->i_bmap, blkoff, &blkphy, maxblocks); 1033 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem); 1034 1035 if (n < 0) { 1036 int past_eof; 1037 1038 if (unlikely(n != -ENOENT)) 1039 break; /* error */ 1040 1041 /* HOLE */ 1042 blkoff++; 1043 past_eof = ((blkoff << blkbits) >= isize); 1044 1045 if (size) { 1046 /* End of the current extent */ 1047 1048 if (past_eof) 1049 flags |= FIEMAP_EXTENT_LAST; 1050 1051 ret = fiemap_fill_next_extent( 1052 fieinfo, logical, phys, size, flags); 1053 if (ret) 1054 break; 1055 size = 0; 1056 } 1057 if (blkoff > end_blkoff || past_eof) 1058 break; 1059 } else { 1060 if (size) { 1061 if (phys && blkphy << blkbits == phys + size) { 1062 /* The current extent goes on */ 1063 size += n << blkbits; 1064 } else { 1065 /* Terminate the current extent */ 1066 ret = fiemap_fill_next_extent( 1067 fieinfo, logical, phys, size, 1068 flags); 1069 if (ret || blkoff > end_blkoff) 1070 break; 1071 1072 /* Start another extent */ 1073 flags = FIEMAP_EXTENT_MERGED; 1074 logical = blkoff << blkbits; 1075 phys = blkphy << blkbits; 1076 size = n << blkbits; 1077 } 1078 } else { 1079 /* Start a new extent */ 1080 flags = FIEMAP_EXTENT_MERGED; 1081 logical = blkoff << blkbits; 1082 phys = blkphy << blkbits; 1083 size = n << blkbits; 1084 } 1085 blkoff += n; 1086 } 1087 cond_resched(); 1088 } while (true); 1089 1090 /* If ret is 1 then we just hit the end of the extent array */ 1091 if (ret == 1) 1092 ret = 0; 1093 1094 mutex_unlock(&inode->i_mutex); 1095 return ret; 1096 } 1097