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_CACHE_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); 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_CACHE_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 void *src_addr, *dst_addr; 109 struct f2fs_io_info fio = { 110 .sbi = F2FS_I_SB(dn->inode), 111 .type = DATA, 112 .rw = WRITE_SYNC | REQ_PRIO, 113 .page = page, 114 .encrypted_page = NULL, 115 }; 116 int dirty, err; 117 118 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index); 119 120 if (!f2fs_exist_data(dn->inode)) 121 goto clear_out; 122 123 err = f2fs_reserve_block(dn, 0); 124 if (err) 125 return err; 126 127 f2fs_wait_on_page_writeback(page, DATA); 128 129 if (PageUptodate(page)) 130 goto no_update; 131 132 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 133 134 /* Copy the whole inline data block */ 135 src_addr = inline_data_addr(dn->inode_page); 136 dst_addr = kmap_atomic(page); 137 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 138 flush_dcache_page(page); 139 kunmap_atomic(dst_addr); 140 SetPageUptodate(page); 141 no_update: 142 set_page_dirty(page); 143 144 /* clear dirty state */ 145 dirty = clear_page_dirty_for_io(page); 146 147 /* write data page to try to make data consistent */ 148 set_page_writeback(page); 149 fio.blk_addr = dn->data_blkaddr; 150 write_data_page(dn, &fio); 151 set_data_blkaddr(dn); 152 f2fs_update_extent_cache(dn); 153 f2fs_wait_on_page_writeback(page, DATA); 154 if (dirty) 155 inode_dec_dirty_pages(dn->inode); 156 157 /* this converted inline_data should be recovered. */ 158 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE); 159 160 /* clear inline data and flag after data writeback */ 161 truncate_inline_inode(dn->inode_page, 0); 162 clear_out: 163 stat_dec_inline_inode(dn->inode); 164 f2fs_clear_inline_inode(dn->inode); 165 sync_inode_page(dn); 166 f2fs_put_dnode(dn); 167 return 0; 168 } 169 170 int f2fs_convert_inline_inode(struct inode *inode) 171 { 172 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 173 struct dnode_of_data dn; 174 struct page *ipage, *page; 175 int err = 0; 176 177 if (!f2fs_has_inline_data(inode)) 178 return 0; 179 180 page = grab_cache_page(inode->i_mapping, 0); 181 if (!page) 182 return -ENOMEM; 183 184 f2fs_lock_op(sbi); 185 186 ipage = get_node_page(sbi, inode->i_ino); 187 if (IS_ERR(ipage)) { 188 err = PTR_ERR(ipage); 189 goto out; 190 } 191 192 set_new_dnode(&dn, inode, ipage, ipage, 0); 193 194 if (f2fs_has_inline_data(inode)) 195 err = f2fs_convert_inline_page(&dn, page); 196 197 f2fs_put_dnode(&dn); 198 out: 199 f2fs_unlock_op(sbi); 200 201 f2fs_put_page(page, 1); 202 203 f2fs_balance_fs(sbi, dn.node_changed); 204 205 return err; 206 } 207 208 int f2fs_write_inline_data(struct inode *inode, struct page *page) 209 { 210 void *src_addr, *dst_addr; 211 struct dnode_of_data dn; 212 int err; 213 214 set_new_dnode(&dn, inode, NULL, NULL, 0); 215 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); 216 if (err) 217 return err; 218 219 if (!f2fs_has_inline_data(inode)) { 220 f2fs_put_dnode(&dn); 221 return -EAGAIN; 222 } 223 224 f2fs_bug_on(F2FS_I_SB(inode), page->index); 225 226 f2fs_wait_on_page_writeback(dn.inode_page, NODE); 227 src_addr = kmap_atomic(page); 228 dst_addr = inline_data_addr(dn.inode_page); 229 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 230 kunmap_atomic(src_addr); 231 232 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); 233 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); 234 235 sync_inode_page(&dn); 236 f2fs_put_dnode(&dn); 237 return 0; 238 } 239 240 bool recover_inline_data(struct inode *inode, struct page *npage) 241 { 242 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 243 struct f2fs_inode *ri = NULL; 244 void *src_addr, *dst_addr; 245 struct page *ipage; 246 247 /* 248 * The inline_data recovery policy is as follows. 249 * [prev.] [next] of inline_data flag 250 * o o -> recover inline_data 251 * o x -> remove inline_data, and then recover data blocks 252 * x o -> remove inline_data, and then recover inline_data 253 * x x -> recover data blocks 254 */ 255 if (IS_INODE(npage)) 256 ri = F2FS_INODE(npage); 257 258 if (f2fs_has_inline_data(inode) && 259 ri && (ri->i_inline & F2FS_INLINE_DATA)) { 260 process_inline: 261 ipage = get_node_page(sbi, inode->i_ino); 262 f2fs_bug_on(sbi, IS_ERR(ipage)); 263 264 f2fs_wait_on_page_writeback(ipage, NODE); 265 266 src_addr = inline_data_addr(npage); 267 dst_addr = inline_data_addr(ipage); 268 memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 269 270 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 271 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); 272 273 update_inode(inode, ipage); 274 f2fs_put_page(ipage, 1); 275 return true; 276 } 277 278 if (f2fs_has_inline_data(inode)) { 279 ipage = get_node_page(sbi, inode->i_ino); 280 f2fs_bug_on(sbi, IS_ERR(ipage)); 281 if (!truncate_inline_inode(ipage, 0)) 282 return false; 283 f2fs_clear_inline_inode(inode); 284 update_inode(inode, ipage); 285 f2fs_put_page(ipage, 1); 286 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 287 if (truncate_blocks(inode, 0, false)) 288 return false; 289 goto process_inline; 290 } 291 return false; 292 } 293 294 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir, 295 struct f2fs_filename *fname, struct page **res_page) 296 { 297 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 298 struct f2fs_inline_dentry *inline_dentry; 299 struct qstr name = FSTR_TO_QSTR(&fname->disk_name); 300 struct f2fs_dir_entry *de; 301 struct f2fs_dentry_ptr d; 302 struct page *ipage; 303 f2fs_hash_t namehash; 304 305 ipage = get_node_page(sbi, dir->i_ino); 306 if (IS_ERR(ipage)) 307 return NULL; 308 309 namehash = f2fs_dentry_hash(&name); 310 311 inline_dentry = inline_data_addr(ipage); 312 313 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2); 314 de = find_target_dentry(fname, namehash, NULL, &d); 315 unlock_page(ipage); 316 if (de) 317 *res_page = ipage; 318 else 319 f2fs_put_page(ipage, 0); 320 321 /* 322 * For the most part, it should be a bug when name_len is zero. 323 * We stop here for figuring out where the bugs has occurred. 324 */ 325 f2fs_bug_on(sbi, d.max < 0); 326 return de; 327 } 328 329 struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir, 330 struct page **p) 331 { 332 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 333 struct page *ipage; 334 struct f2fs_dir_entry *de; 335 struct f2fs_inline_dentry *dentry_blk; 336 337 ipage = get_node_page(sbi, dir->i_ino); 338 if (IS_ERR(ipage)) 339 return NULL; 340 341 dentry_blk = inline_data_addr(ipage); 342 de = &dentry_blk->dentry[1]; 343 *p = ipage; 344 unlock_page(ipage); 345 return de; 346 } 347 348 int make_empty_inline_dir(struct inode *inode, struct inode *parent, 349 struct page *ipage) 350 { 351 struct f2fs_inline_dentry *dentry_blk; 352 struct f2fs_dentry_ptr d; 353 354 dentry_blk = inline_data_addr(ipage); 355 356 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2); 357 do_make_empty_dir(inode, parent, &d); 358 359 set_page_dirty(ipage); 360 361 /* update i_size to MAX_INLINE_DATA */ 362 if (i_size_read(inode) < MAX_INLINE_DATA) { 363 i_size_write(inode, MAX_INLINE_DATA); 364 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); 365 } 366 return 0; 367 } 368 369 /* 370 * NOTE: ipage is grabbed by caller, but if any error occurs, we should 371 * release ipage in this function. 372 */ 373 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 374 struct f2fs_inline_dentry *inline_dentry) 375 { 376 struct page *page; 377 struct dnode_of_data dn; 378 struct f2fs_dentry_block *dentry_blk; 379 int err; 380 381 page = grab_cache_page(dir->i_mapping, 0); 382 if (!page) { 383 f2fs_put_page(ipage, 1); 384 return -ENOMEM; 385 } 386 387 set_new_dnode(&dn, dir, ipage, NULL, 0); 388 err = f2fs_reserve_block(&dn, 0); 389 if (err) 390 goto out; 391 392 f2fs_wait_on_page_writeback(page, DATA); 393 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 394 395 dentry_blk = kmap_atomic(page); 396 397 /* copy data from inline dentry block to new dentry block */ 398 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, 399 INLINE_DENTRY_BITMAP_SIZE); 400 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0, 401 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE); 402 /* 403 * we do not need to zero out remainder part of dentry and filename 404 * field, since we have used bitmap for marking the usage status of 405 * them, besides, we can also ignore copying/zeroing reserved space 406 * of dentry block, because them haven't been used so far. 407 */ 408 memcpy(dentry_blk->dentry, inline_dentry->dentry, 409 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); 410 memcpy(dentry_blk->filename, inline_dentry->filename, 411 NR_INLINE_DENTRY * F2FS_SLOT_LEN); 412 413 kunmap_atomic(dentry_blk); 414 SetPageUptodate(page); 415 set_page_dirty(page); 416 417 /* clear inline dir and flag after data writeback */ 418 truncate_inline_inode(ipage, 0); 419 420 stat_dec_inline_dir(dir); 421 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY); 422 423 if (i_size_read(dir) < PAGE_CACHE_SIZE) { 424 i_size_write(dir, PAGE_CACHE_SIZE); 425 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 426 } 427 428 sync_inode_page(&dn); 429 out: 430 f2fs_put_page(page, 1); 431 return err; 432 } 433 434 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name, 435 struct inode *inode, nid_t ino, umode_t mode) 436 { 437 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 438 struct page *ipage; 439 unsigned int bit_pos; 440 f2fs_hash_t name_hash; 441 size_t namelen = name->len; 442 struct f2fs_inline_dentry *dentry_blk = NULL; 443 struct f2fs_dentry_ptr d; 444 int slots = GET_DENTRY_SLOTS(namelen); 445 struct page *page = NULL; 446 int err = 0; 447 448 ipage = get_node_page(sbi, dir->i_ino); 449 if (IS_ERR(ipage)) 450 return PTR_ERR(ipage); 451 452 dentry_blk = inline_data_addr(ipage); 453 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, 454 slots, NR_INLINE_DENTRY); 455 if (bit_pos >= NR_INLINE_DENTRY) { 456 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk); 457 if (err) 458 return err; 459 err = -EAGAIN; 460 goto out; 461 } 462 463 if (inode) { 464 down_write(&F2FS_I(inode)->i_sem); 465 page = init_inode_metadata(inode, dir, name, ipage); 466 if (IS_ERR(page)) { 467 err = PTR_ERR(page); 468 goto fail; 469 } 470 } 471 472 f2fs_wait_on_page_writeback(ipage, NODE); 473 474 name_hash = f2fs_dentry_hash(name); 475 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2); 476 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos); 477 478 set_page_dirty(ipage); 479 480 /* we don't need to mark_inode_dirty now */ 481 if (inode) { 482 F2FS_I(inode)->i_pino = dir->i_ino; 483 update_inode(inode, page); 484 f2fs_put_page(page, 1); 485 } 486 487 update_parent_metadata(dir, inode, 0); 488 fail: 489 if (inode) 490 up_write(&F2FS_I(inode)->i_sem); 491 492 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { 493 update_inode(dir, ipage); 494 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 495 } 496 out: 497 f2fs_put_page(ipage, 1); 498 return err; 499 } 500 501 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 502 struct inode *dir, struct inode *inode) 503 { 504 struct f2fs_inline_dentry *inline_dentry; 505 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 506 unsigned int bit_pos; 507 int i; 508 509 lock_page(page); 510 f2fs_wait_on_page_writeback(page, NODE); 511 512 inline_dentry = inline_data_addr(page); 513 bit_pos = dentry - inline_dentry->dentry; 514 for (i = 0; i < slots; i++) 515 test_and_clear_bit_le(bit_pos + i, 516 &inline_dentry->dentry_bitmap); 517 518 set_page_dirty(page); 519 520 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 521 522 if (inode) 523 f2fs_drop_nlink(dir, inode, page); 524 525 f2fs_put_page(page, 1); 526 } 527 528 bool f2fs_empty_inline_dir(struct inode *dir) 529 { 530 struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 531 struct page *ipage; 532 unsigned int bit_pos = 2; 533 struct f2fs_inline_dentry *dentry_blk; 534 535 ipage = get_node_page(sbi, dir->i_ino); 536 if (IS_ERR(ipage)) 537 return false; 538 539 dentry_blk = inline_data_addr(ipage); 540 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 541 NR_INLINE_DENTRY, 542 bit_pos); 543 544 f2fs_put_page(ipage, 1); 545 546 if (bit_pos < NR_INLINE_DENTRY) 547 return false; 548 549 return true; 550 } 551 552 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 553 struct f2fs_str *fstr) 554 { 555 struct inode *inode = file_inode(file); 556 struct f2fs_inline_dentry *inline_dentry = NULL; 557 struct page *ipage = NULL; 558 struct f2fs_dentry_ptr d; 559 560 if (ctx->pos == NR_INLINE_DENTRY) 561 return 0; 562 563 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 564 if (IS_ERR(ipage)) 565 return PTR_ERR(ipage); 566 567 inline_dentry = inline_data_addr(ipage); 568 569 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2); 570 571 if (!f2fs_fill_dentries(ctx, &d, 0, fstr)) 572 ctx->pos = NR_INLINE_DENTRY; 573 574 f2fs_put_page(ipage, 1); 575 return 0; 576 } 577 578 int f2fs_inline_data_fiemap(struct inode *inode, 579 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len) 580 { 581 __u64 byteaddr, ilen; 582 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | 583 FIEMAP_EXTENT_LAST; 584 struct node_info ni; 585 struct page *ipage; 586 int err = 0; 587 588 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 589 if (IS_ERR(ipage)) 590 return PTR_ERR(ipage); 591 592 if (!f2fs_has_inline_data(inode)) { 593 err = -EAGAIN; 594 goto out; 595 } 596 597 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode)); 598 if (start >= ilen) 599 goto out; 600 if (start + len < ilen) 601 ilen = start + len; 602 ilen -= start; 603 604 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); 605 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; 606 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage); 607 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); 608 out: 609 f2fs_put_page(ipage, 1); 610 return err; 611 } 612