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