1 /* 2 * fs/f2fs/inline.c 3 * Copyright (c) 2013, Intel Corporation 4 * Authors: Huajun Li <huajun.li@intel.com> 5 * Haicheng Li <haicheng.li@intel.com> 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 14 #include "f2fs.h" 15 #include "node.h" 16 17 bool f2fs_may_inline_data(struct inode *inode) 18 { 19 if (f2fs_is_atomic_file(inode)) 20 return false; 21 22 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) 23 return false; 24 25 if (i_size_read(inode) > MAX_INLINE_DATA(inode)) 26 return false; 27 28 if (f2fs_post_read_required(inode)) 29 return false; 30 31 return true; 32 } 33 34 bool f2fs_may_inline_dentry(struct inode *inode) 35 { 36 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY)) 37 return false; 38 39 if (!S_ISDIR(inode->i_mode)) 40 return false; 41 42 return true; 43 } 44 45 void f2fs_do_read_inline_data(struct page *page, struct page *ipage) 46 { 47 struct inode *inode = page->mapping->host; 48 void *src_addr, *dst_addr; 49 50 if (PageUptodate(page)) 51 return; 52 53 f2fs_bug_on(F2FS_P_SB(page), page->index); 54 55 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE); 56 57 /* Copy the whole inline data block */ 58 src_addr = inline_data_addr(inode, ipage); 59 dst_addr = kmap_atomic(page); 60 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 61 flush_dcache_page(page); 62 kunmap_atomic(dst_addr); 63 if (!PageUptodate(page)) 64 SetPageUptodate(page); 65 } 66 67 void f2fs_truncate_inline_inode(struct inode *inode, 68 struct page *ipage, u64 from) 69 { 70 void *addr; 71 72 if (from >= MAX_INLINE_DATA(inode)) 73 return; 74 75 addr = inline_data_addr(inode, ipage); 76 77 f2fs_wait_on_page_writeback(ipage, NODE, true); 78 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from); 79 set_page_dirty(ipage); 80 81 if (from == 0) 82 clear_inode_flag(inode, FI_DATA_EXIST); 83 } 84 85 int f2fs_read_inline_data(struct inode *inode, struct page *page) 86 { 87 struct page *ipage; 88 89 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 90 if (IS_ERR(ipage)) { 91 unlock_page(page); 92 return PTR_ERR(ipage); 93 } 94 95 if (!f2fs_has_inline_data(inode)) { 96 f2fs_put_page(ipage, 1); 97 return -EAGAIN; 98 } 99 100 if (page->index) 101 zero_user_segment(page, 0, PAGE_SIZE); 102 else 103 f2fs_do_read_inline_data(page, ipage); 104 105 if (!PageUptodate(page)) 106 SetPageUptodate(page); 107 f2fs_put_page(ipage, 1); 108 unlock_page(page); 109 return 0; 110 } 111 112 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page) 113 { 114 struct f2fs_io_info fio = { 115 .sbi = F2FS_I_SB(dn->inode), 116 .ino = dn->inode->i_ino, 117 .type = DATA, 118 .op = REQ_OP_WRITE, 119 .op_flags = REQ_SYNC | REQ_PRIO, 120 .page = page, 121 .encrypted_page = NULL, 122 .io_type = FS_DATA_IO, 123 }; 124 int dirty, err; 125 126 if (!f2fs_exist_data(dn->inode)) 127 goto clear_out; 128 129 err = f2fs_reserve_block(dn, 0); 130 if (err) 131 return err; 132 133 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page)); 134 135 f2fs_do_read_inline_data(page, dn->inode_page); 136 set_page_dirty(page); 137 138 /* clear dirty state */ 139 dirty = clear_page_dirty_for_io(page); 140 141 /* write data page to try to make data consistent */ 142 set_page_writeback(page); 143 ClearPageError(page); 144 fio.old_blkaddr = dn->data_blkaddr; 145 set_inode_flag(dn->inode, FI_HOT_DATA); 146 f2fs_outplace_write_data(dn, &fio); 147 f2fs_wait_on_page_writeback(page, DATA, true); 148 if (dirty) { 149 inode_dec_dirty_pages(dn->inode); 150 f2fs_remove_dirty_inode(dn->inode); 151 } 152 153 /* this converted inline_data should be recovered. */ 154 set_inode_flag(dn->inode, FI_APPEND_WRITE); 155 156 /* clear inline data and flag after data writeback */ 157 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0); 158 clear_inline_node(dn->inode_page); 159 clear_out: 160 stat_dec_inline_inode(dn->inode); 161 clear_inode_flag(dn->inode, FI_INLINE_DATA); 162 f2fs_put_dnode(dn); 163 return 0; 164 } 165 166 int f2fs_convert_inline_inode(struct inode *inode) 167 { 168 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 169 struct dnode_of_data dn; 170 struct page *ipage, *page; 171 int err = 0; 172 173 if (!f2fs_has_inline_data(inode)) 174 return 0; 175 176 page = f2fs_grab_cache_page(inode->i_mapping, 0, false); 177 if (!page) 178 return -ENOMEM; 179 180 f2fs_lock_op(sbi); 181 182 ipage = f2fs_get_node_page(sbi, inode->i_ino); 183 if (IS_ERR(ipage)) { 184 err = PTR_ERR(ipage); 185 goto out; 186 } 187 188 set_new_dnode(&dn, inode, ipage, ipage, 0); 189 190 if (f2fs_has_inline_data(inode)) 191 err = f2fs_convert_inline_page(&dn, page); 192 193 f2fs_put_dnode(&dn); 194 out: 195 f2fs_unlock_op(sbi); 196 197 f2fs_put_page(page, 1); 198 199 f2fs_balance_fs(sbi, dn.node_changed); 200 201 return err; 202 } 203 204 int f2fs_write_inline_data(struct inode *inode, struct page *page) 205 { 206 void *src_addr, *dst_addr; 207 struct dnode_of_data dn; 208 int err; 209 210 set_new_dnode(&dn, inode, NULL, NULL, 0); 211 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE); 212 if (err) 213 return err; 214 215 if (!f2fs_has_inline_data(inode)) { 216 f2fs_put_dnode(&dn); 217 return -EAGAIN; 218 } 219 220 f2fs_bug_on(F2FS_I_SB(inode), page->index); 221 222 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true); 223 src_addr = kmap_atomic(page); 224 dst_addr = inline_data_addr(inode, dn.inode_page); 225 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 226 kunmap_atomic(src_addr); 227 set_page_dirty(dn.inode_page); 228 229 f2fs_clear_radix_tree_dirty_tag(page); 230 231 set_inode_flag(inode, FI_APPEND_WRITE); 232 set_inode_flag(inode, FI_DATA_EXIST); 233 234 clear_inline_node(dn.inode_page); 235 f2fs_put_dnode(&dn); 236 return 0; 237 } 238 239 bool f2fs_recover_inline_data(struct inode *inode, struct page *npage) 240 { 241 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 242 struct f2fs_inode *ri = NULL; 243 void *src_addr, *dst_addr; 244 struct page *ipage; 245 246 /* 247 * The inline_data recovery policy is as follows. 248 * [prev.] [next] of inline_data flag 249 * o o -> recover inline_data 250 * o x -> remove inline_data, and then recover data blocks 251 * x o -> remove inline_data, and then recover inline_data 252 * x x -> recover data blocks 253 */ 254 if (IS_INODE(npage)) 255 ri = F2FS_INODE(npage); 256 257 if (f2fs_has_inline_data(inode) && 258 ri && (ri->i_inline & F2FS_INLINE_DATA)) { 259 process_inline: 260 ipage = f2fs_get_node_page(sbi, inode->i_ino); 261 f2fs_bug_on(sbi, IS_ERR(ipage)); 262 263 f2fs_wait_on_page_writeback(ipage, NODE, true); 264 265 src_addr = inline_data_addr(inode, npage); 266 dst_addr = inline_data_addr(inode, ipage); 267 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 268 269 set_inode_flag(inode, FI_INLINE_DATA); 270 set_inode_flag(inode, FI_DATA_EXIST); 271 272 set_page_dirty(ipage); 273 f2fs_put_page(ipage, 1); 274 return true; 275 } 276 277 if (f2fs_has_inline_data(inode)) { 278 ipage = f2fs_get_node_page(sbi, inode->i_ino); 279 f2fs_bug_on(sbi, IS_ERR(ipage)); 280 f2fs_truncate_inline_inode(inode, ipage, 0); 281 clear_inode_flag(inode, FI_INLINE_DATA); 282 f2fs_put_page(ipage, 1); 283 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 284 if (f2fs_truncate_blocks(inode, 0, false)) 285 return false; 286 goto process_inline; 287 } 288 return false; 289 } 290 291 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir, 292 struct fscrypt_name *fname, struct page **res_page) 293 { 294 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 295 struct qstr name = FSTR_TO_QSTR(&fname->disk_name); 296 struct f2fs_dir_entry *de; 297 struct f2fs_dentry_ptr d; 298 struct page *ipage; 299 void *inline_dentry; 300 f2fs_hash_t namehash; 301 302 ipage = f2fs_get_node_page(sbi, dir->i_ino); 303 if (IS_ERR(ipage)) { 304 *res_page = ipage; 305 return NULL; 306 } 307 308 namehash = f2fs_dentry_hash(&name, fname); 309 310 inline_dentry = inline_data_addr(dir, ipage); 311 312 make_dentry_ptr_inline(dir, &d, inline_dentry); 313 de = f2fs_find_target_dentry(fname, namehash, NULL, &d); 314 unlock_page(ipage); 315 if (de) 316 *res_page = ipage; 317 else 318 f2fs_put_page(ipage, 0); 319 320 return de; 321 } 322 323 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent, 324 struct page *ipage) 325 { 326 struct f2fs_dentry_ptr d; 327 void *inline_dentry; 328 329 inline_dentry = inline_data_addr(inode, ipage); 330 331 make_dentry_ptr_inline(inode, &d, inline_dentry); 332 f2fs_do_make_empty_dir(inode, parent, &d); 333 334 set_page_dirty(ipage); 335 336 /* update i_size to MAX_INLINE_DATA */ 337 if (i_size_read(inode) < MAX_INLINE_DATA(inode)) 338 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode)); 339 return 0; 340 } 341 342 /* 343 * NOTE: ipage is grabbed by caller, but if any error occurs, we should 344 * release ipage in this function. 345 */ 346 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage, 347 void *inline_dentry) 348 { 349 struct page *page; 350 struct dnode_of_data dn; 351 struct f2fs_dentry_block *dentry_blk; 352 struct f2fs_dentry_ptr src, dst; 353 int err; 354 355 page = f2fs_grab_cache_page(dir->i_mapping, 0, false); 356 if (!page) { 357 f2fs_put_page(ipage, 1); 358 return -ENOMEM; 359 } 360 361 set_new_dnode(&dn, dir, ipage, NULL, 0); 362 err = f2fs_reserve_block(&dn, 0); 363 if (err) 364 goto out; 365 366 f2fs_wait_on_page_writeback(page, DATA, true); 367 368 dentry_blk = page_address(page); 369 370 make_dentry_ptr_inline(dir, &src, inline_dentry); 371 make_dentry_ptr_block(dir, &dst, dentry_blk); 372 373 /* copy data from inline dentry block to new dentry block */ 374 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap); 375 memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap); 376 /* 377 * we do not need to zero out remainder part of dentry and filename 378 * field, since we have used bitmap for marking the usage status of 379 * them, besides, we can also ignore copying/zeroing reserved space 380 * of dentry block, because them haven't been used so far. 381 */ 382 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max); 383 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN); 384 385 if (!PageUptodate(page)) 386 SetPageUptodate(page); 387 set_page_dirty(page); 388 389 /* clear inline dir and flag after data writeback */ 390 f2fs_truncate_inline_inode(dir, ipage, 0); 391 392 stat_dec_inline_dir(dir); 393 clear_inode_flag(dir, FI_INLINE_DENTRY); 394 395 f2fs_i_depth_write(dir, 1); 396 if (i_size_read(dir) < PAGE_SIZE) 397 f2fs_i_size_write(dir, PAGE_SIZE); 398 out: 399 f2fs_put_page(page, 1); 400 return err; 401 } 402 403 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry) 404 { 405 struct f2fs_dentry_ptr d; 406 unsigned long bit_pos = 0; 407 int err = 0; 408 409 make_dentry_ptr_inline(dir, &d, inline_dentry); 410 411 while (bit_pos < d.max) { 412 struct f2fs_dir_entry *de; 413 struct qstr new_name; 414 nid_t ino; 415 umode_t fake_mode; 416 417 if (!test_bit_le(bit_pos, d.bitmap)) { 418 bit_pos++; 419 continue; 420 } 421 422 de = &d.dentry[bit_pos]; 423 424 if (unlikely(!de->name_len)) { 425 bit_pos++; 426 continue; 427 } 428 429 new_name.name = d.filename[bit_pos]; 430 new_name.len = le16_to_cpu(de->name_len); 431 432 ino = le32_to_cpu(de->ino); 433 fake_mode = f2fs_get_de_type(de) << S_SHIFT; 434 435 err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL, 436 ino, fake_mode); 437 if (err) 438 goto punch_dentry_pages; 439 440 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 441 } 442 return 0; 443 punch_dentry_pages: 444 truncate_inode_pages(&dir->i_data, 0); 445 f2fs_truncate_blocks(dir, 0, false); 446 f2fs_remove_dirty_inode(dir); 447 return err; 448 } 449 450 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage, 451 void *inline_dentry) 452 { 453 void *backup_dentry; 454 int err; 455 456 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir), 457 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO); 458 if (!backup_dentry) { 459 f2fs_put_page(ipage, 1); 460 return -ENOMEM; 461 } 462 463 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir)); 464 f2fs_truncate_inline_inode(dir, ipage, 0); 465 466 unlock_page(ipage); 467 468 err = f2fs_add_inline_entries(dir, backup_dentry); 469 if (err) 470 goto recover; 471 472 lock_page(ipage); 473 474 stat_dec_inline_dir(dir); 475 clear_inode_flag(dir, FI_INLINE_DENTRY); 476 kfree(backup_dentry); 477 return 0; 478 recover: 479 lock_page(ipage); 480 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir)); 481 f2fs_i_depth_write(dir, 0); 482 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir)); 483 set_page_dirty(ipage); 484 f2fs_put_page(ipage, 1); 485 486 kfree(backup_dentry); 487 return err; 488 } 489 490 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 491 void *inline_dentry) 492 { 493 if (!F2FS_I(dir)->i_dir_level) 494 return f2fs_move_inline_dirents(dir, ipage, inline_dentry); 495 else 496 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry); 497 } 498 499 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name, 500 const struct qstr *orig_name, 501 struct inode *inode, nid_t ino, umode_t mode) 502 { 503 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 504 struct page *ipage; 505 unsigned int bit_pos; 506 f2fs_hash_t name_hash; 507 void *inline_dentry = NULL; 508 struct f2fs_dentry_ptr d; 509 int slots = GET_DENTRY_SLOTS(new_name->len); 510 struct page *page = NULL; 511 int err = 0; 512 513 ipage = f2fs_get_node_page(sbi, dir->i_ino); 514 if (IS_ERR(ipage)) 515 return PTR_ERR(ipage); 516 517 inline_dentry = inline_data_addr(dir, ipage); 518 make_dentry_ptr_inline(dir, &d, inline_dentry); 519 520 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max); 521 if (bit_pos >= d.max) { 522 err = f2fs_convert_inline_dir(dir, ipage, inline_dentry); 523 if (err) 524 return err; 525 err = -EAGAIN; 526 goto out; 527 } 528 529 if (inode) { 530 down_write(&F2FS_I(inode)->i_sem); 531 page = f2fs_init_inode_metadata(inode, dir, new_name, 532 orig_name, ipage); 533 if (IS_ERR(page)) { 534 err = PTR_ERR(page); 535 goto fail; 536 } 537 } 538 539 f2fs_wait_on_page_writeback(ipage, NODE, true); 540 541 name_hash = f2fs_dentry_hash(new_name, NULL); 542 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos); 543 544 set_page_dirty(ipage); 545 546 /* we don't need to mark_inode_dirty now */ 547 if (inode) { 548 f2fs_i_pino_write(inode, dir->i_ino); 549 f2fs_put_page(page, 1); 550 } 551 552 f2fs_update_parent_metadata(dir, inode, 0); 553 fail: 554 if (inode) 555 up_write(&F2FS_I(inode)->i_sem); 556 out: 557 f2fs_put_page(ipage, 1); 558 return err; 559 } 560 561 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 562 struct inode *dir, struct inode *inode) 563 { 564 struct f2fs_dentry_ptr d; 565 void *inline_dentry; 566 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 567 unsigned int bit_pos; 568 int i; 569 570 lock_page(page); 571 f2fs_wait_on_page_writeback(page, NODE, true); 572 573 inline_dentry = inline_data_addr(dir, page); 574 make_dentry_ptr_inline(dir, &d, inline_dentry); 575 576 bit_pos = dentry - d.dentry; 577 for (i = 0; i < slots; i++) 578 __clear_bit_le(bit_pos + i, d.bitmap); 579 580 set_page_dirty(page); 581 f2fs_put_page(page, 1); 582 583 dir->i_ctime = dir->i_mtime = current_time(dir); 584 f2fs_mark_inode_dirty_sync(dir, false); 585 586 if (inode) 587 f2fs_drop_nlink(dir, inode); 588 } 589 590 bool f2fs_empty_inline_dir(struct inode *dir) 591 { 592 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 593 struct page *ipage; 594 unsigned int bit_pos = 2; 595 void *inline_dentry; 596 struct f2fs_dentry_ptr d; 597 598 ipage = f2fs_get_node_page(sbi, dir->i_ino); 599 if (IS_ERR(ipage)) 600 return false; 601 602 inline_dentry = inline_data_addr(dir, ipage); 603 make_dentry_ptr_inline(dir, &d, inline_dentry); 604 605 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos); 606 607 f2fs_put_page(ipage, 1); 608 609 if (bit_pos < d.max) 610 return false; 611 612 return true; 613 } 614 615 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 616 struct fscrypt_str *fstr) 617 { 618 struct inode *inode = file_inode(file); 619 struct page *ipage = NULL; 620 struct f2fs_dentry_ptr d; 621 void *inline_dentry = NULL; 622 int err; 623 624 make_dentry_ptr_inline(inode, &d, inline_dentry); 625 626 if (ctx->pos == d.max) 627 return 0; 628 629 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 630 if (IS_ERR(ipage)) 631 return PTR_ERR(ipage); 632 633 inline_dentry = inline_data_addr(inode, ipage); 634 635 make_dentry_ptr_inline(inode, &d, inline_dentry); 636 637 err = f2fs_fill_dentries(ctx, &d, 0, fstr); 638 if (!err) 639 ctx->pos = d.max; 640 641 f2fs_put_page(ipage, 1); 642 return err < 0 ? err : 0; 643 } 644 645 int f2fs_inline_data_fiemap(struct inode *inode, 646 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len) 647 { 648 __u64 byteaddr, ilen; 649 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | 650 FIEMAP_EXTENT_LAST; 651 struct node_info ni; 652 struct page *ipage; 653 int err = 0; 654 655 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 656 if (IS_ERR(ipage)) 657 return PTR_ERR(ipage); 658 659 if (!f2fs_has_inline_data(inode)) { 660 err = -EAGAIN; 661 goto out; 662 } 663 664 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode)); 665 if (start >= ilen) 666 goto out; 667 if (start + len < ilen) 668 ilen = start + len; 669 ilen -= start; 670 671 f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); 672 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; 673 byteaddr += (char *)inline_data_addr(inode, ipage) - 674 (char *)F2FS_INODE(ipage); 675 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); 676 out: 677 f2fs_put_page(ipage, 1); 678 return err; 679 } 680