17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0 2e18c65b2SHuajun Li /* 3e18c65b2SHuajun Li * fs/f2fs/inline.c 4e18c65b2SHuajun Li * Copyright (c) 2013, Intel Corporation 5e18c65b2SHuajun Li * Authors: Huajun Li <huajun.li@intel.com> 6e18c65b2SHuajun Li * Haicheng Li <haicheng.li@intel.com> 7e18c65b2SHuajun Li */ 8e18c65b2SHuajun Li 9e18c65b2SHuajun Li #include <linux/fs.h> 10e18c65b2SHuajun Li #include <linux/f2fs_fs.h> 11e18c65b2SHuajun Li 12e18c65b2SHuajun Li #include "f2fs.h" 1367f8cf3cSJaegeuk Kim #include "node.h" 14e18c65b2SHuajun Li 1501b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *inode) 16e18c65b2SHuajun Li { 1788b88a66SJaegeuk Kim if (f2fs_is_atomic_file(inode)) 1888b88a66SJaegeuk Kim return false; 1988b88a66SJaegeuk Kim 20368a0e40SWanpeng Li if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) 21e18c65b2SHuajun Li return false; 22e18c65b2SHuajun Li 23f2470371SChao Yu if (i_size_read(inode) > MAX_INLINE_DATA(inode)) 2492dffd01SJaegeuk Kim return false; 2592dffd01SJaegeuk Kim 266dbb1796SEric Biggers if (f2fs_post_read_required(inode)) 27fcc85a4dSJaegeuk Kim return false; 28fcc85a4dSJaegeuk Kim 29e18c65b2SHuajun Li return true; 30e18c65b2SHuajun Li } 31e18c65b2SHuajun Li 3201b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode) 3301b960e9SJaegeuk Kim { 3401b960e9SJaegeuk Kim if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY)) 3501b960e9SJaegeuk Kim return false; 3601b960e9SJaegeuk Kim 3701b960e9SJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 3801b960e9SJaegeuk Kim return false; 3901b960e9SJaegeuk Kim 4001b960e9SJaegeuk Kim return true; 4101b960e9SJaegeuk Kim } 4201b960e9SJaegeuk Kim 434d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage) 44e18c65b2SHuajun Li { 45f2470371SChao Yu struct inode *inode = page->mapping->host; 46e18c65b2SHuajun Li void *src_addr, *dst_addr; 47e18c65b2SHuajun Li 48b3d208f9SJaegeuk Kim if (PageUptodate(page)) 49b3d208f9SJaegeuk Kim return; 5004a17fb1SChao Yu 51b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_P_SB(page), page->index); 52e18c65b2SHuajun Li 53f2470371SChao Yu zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE); 54e18c65b2SHuajun Li 55e18c65b2SHuajun Li /* Copy the whole inline data block */ 56f2470371SChao Yu src_addr = inline_data_addr(inode, ipage); 57f1e33a04SJaegeuk Kim dst_addr = kmap_atomic(page); 58f2470371SChao Yu memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 59427a45c8SJaegeuk Kim flush_dcache_page(page); 60f1e33a04SJaegeuk Kim kunmap_atomic(dst_addr); 61237c0790SJaegeuk Kim if (!PageUptodate(page)) 62e18c65b2SHuajun Li SetPageUptodate(page); 63b3d208f9SJaegeuk Kim } 64e18c65b2SHuajun Li 654d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode, 664d57b86dSChao Yu struct page *ipage, u64 from) 67feeb0debSChao Yu { 680bfcfccaSChao Yu void *addr; 690bfcfccaSChao Yu 70f2470371SChao Yu if (from >= MAX_INLINE_DATA(inode)) 71bd4667cbSKinglong Mee return; 720bfcfccaSChao Yu 73f2470371SChao Yu addr = inline_data_addr(inode, ipage); 740bfcfccaSChao Yu 75bae0ee7aSChao Yu f2fs_wait_on_page_writeback(ipage, NODE, true, true); 76f2470371SChao Yu memset(addr + from, 0, MAX_INLINE_DATA(inode) - from); 77ee6d182fSJaegeuk Kim set_page_dirty(ipage); 78bd4667cbSKinglong Mee 79bd4667cbSKinglong Mee if (from == 0) 80bd4667cbSKinglong Mee clear_inode_flag(inode, FI_DATA_EXIST); 81feeb0debSChao Yu } 82feeb0debSChao Yu 83b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page) 84b3d208f9SJaegeuk Kim { 85b3d208f9SJaegeuk Kim struct page *ipage; 86b3d208f9SJaegeuk Kim 874d57b86dSChao Yu ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 88b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 89b3d208f9SJaegeuk Kim unlock_page(page); 90b3d208f9SJaegeuk Kim return PTR_ERR(ipage); 91b3d208f9SJaegeuk Kim } 92b3d208f9SJaegeuk Kim 93b3d208f9SJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 94b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 95b3d208f9SJaegeuk Kim return -EAGAIN; 96b3d208f9SJaegeuk Kim } 97b3d208f9SJaegeuk Kim 98b3d208f9SJaegeuk Kim if (page->index) 9909cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 100b3d208f9SJaegeuk Kim else 1014d57b86dSChao Yu f2fs_do_read_inline_data(page, ipage); 102b3d208f9SJaegeuk Kim 103237c0790SJaegeuk Kim if (!PageUptodate(page)) 104b3d208f9SJaegeuk Kim SetPageUptodate(page); 105b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 106b3d208f9SJaegeuk Kim unlock_page(page); 107e18c65b2SHuajun Li return 0; 108e18c65b2SHuajun Li } 109e18c65b2SHuajun Li 110b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page) 111e18c65b2SHuajun Li { 112e18c65b2SHuajun Li struct f2fs_io_info fio = { 11305ca3632SJaegeuk Kim .sbi = F2FS_I_SB(dn->inode), 11439d787beSChao Yu .ino = dn->inode->i_ino, 115e18c65b2SHuajun Li .type = DATA, 11604d328deSMike Christie .op = REQ_OP_WRITE, 11770fd7614SChristoph Hellwig .op_flags = REQ_SYNC | REQ_PRIO, 11805ca3632SJaegeuk Kim .page = page, 1194375a336SJaegeuk Kim .encrypted_page = NULL, 120b0af6d49SChao Yu .io_type = FS_DATA_IO, 121e18c65b2SHuajun Li }; 1227735730dSChao Yu struct node_info ni; 123158c194cSJaegeuk Kim int dirty, err; 124e18c65b2SHuajun Li 125b3d208f9SJaegeuk Kim if (!f2fs_exist_data(dn->inode)) 126b3d208f9SJaegeuk Kim goto clear_out; 127ec4e7af4SJaegeuk Kim 128b3d208f9SJaegeuk Kim err = f2fs_reserve_block(dn, 0); 12915c6e3aaSJaegeuk Kim if (err) 130b3d208f9SJaegeuk Kim return err; 131e18c65b2SHuajun Li 1327735730dSChao Yu err = f2fs_get_node_info(fio.sbi, dn->nid, &ni); 1337735730dSChao Yu if (err) { 134*e8c82c11SChao Yu f2fs_truncate_data_blocks_range(dn, 1); 1357735730dSChao Yu f2fs_put_dnode(dn); 1367735730dSChao Yu return err; 1377735730dSChao Yu } 1387735730dSChao Yu 1397735730dSChao Yu fio.version = ni.version; 1407735730dSChao Yu 1414dbe38dcSChao Yu if (unlikely(dn->data_blkaddr != NEW_ADDR)) { 1424dbe38dcSChao Yu f2fs_put_dnode(dn); 1434dbe38dcSChao Yu set_sbi_flag(fio.sbi, SBI_NEED_FSCK); 144dcbb4c10SJoe Perches f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.", 1454dbe38dcSChao Yu __func__, dn->inode->i_ino, dn->data_blkaddr); 14610f966bbSChao Yu return -EFSCORRUPTED; 1474dbe38dcSChao Yu } 1484dbe38dcSChao Yu 14985ead818SYunlei He f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page)); 150b3d208f9SJaegeuk Kim 1514d57b86dSChao Yu f2fs_do_read_inline_data(page, dn->inode_page); 1526282adbfSJaegeuk Kim set_page_dirty(page); 1536282adbfSJaegeuk Kim 154158c194cSJaegeuk Kim /* clear dirty state */ 155158c194cSJaegeuk Kim dirty = clear_page_dirty_for_io(page); 156158c194cSJaegeuk Kim 157e18c65b2SHuajun Li /* write data page to try to make data consistent */ 158e18c65b2SHuajun Li set_page_writeback(page); 15917c50035SJaegeuk Kim ClearPageError(page); 1607a9d7548SChao Yu fio.old_blkaddr = dn->data_blkaddr; 161ef095d19SJaegeuk Kim set_inode_flag(dn->inode, FI_HOT_DATA); 1624d57b86dSChao Yu f2fs_outplace_write_data(dn, &fio); 163bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, DATA, true, true); 164933439c8SChao Yu if (dirty) { 165158c194cSJaegeuk Kim inode_dec_dirty_pages(dn->inode); 1664d57b86dSChao Yu f2fs_remove_dirty_inode(dn->inode); 167933439c8SChao Yu } 168e18c65b2SHuajun Li 16995f5b0fcSJaegeuk Kim /* this converted inline_data should be recovered. */ 17091942321SJaegeuk Kim set_inode_flag(dn->inode, FI_APPEND_WRITE); 17195f5b0fcSJaegeuk Kim 172e18c65b2SHuajun Li /* clear inline data and flag after data writeback */ 1734d57b86dSChao Yu f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0); 1742049d4fcSJaegeuk Kim clear_inline_node(dn->inode_page); 175b3d208f9SJaegeuk Kim clear_out: 176b3d208f9SJaegeuk Kim stat_dec_inline_inode(dn->inode); 177bd4667cbSKinglong Mee clear_inode_flag(dn->inode, FI_INLINE_DATA); 178b3d208f9SJaegeuk Kim f2fs_put_dnode(dn); 179b3d208f9SJaegeuk Kim return 0; 180e18c65b2SHuajun Li } 181e18c65b2SHuajun Li 182b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode) 183e18c65b2SHuajun Li { 184b3d208f9SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 185b3d208f9SJaegeuk Kim struct dnode_of_data dn; 186b3d208f9SJaegeuk Kim struct page *ipage, *page; 187b3d208f9SJaegeuk Kim int err = 0; 188e18c65b2SHuajun Li 189b9d777b8SJaegeuk Kim if (!f2fs_has_inline_data(inode)) 190b9d777b8SJaegeuk Kim return 0; 191b9d777b8SJaegeuk Kim 192300e129cSJaegeuk Kim page = f2fs_grab_cache_page(inode->i_mapping, 0, false); 193b3d208f9SJaegeuk Kim if (!page) 1949e09fc85SJaegeuk Kim return -ENOMEM; 195b3d208f9SJaegeuk Kim 196b3d208f9SJaegeuk Kim f2fs_lock_op(sbi); 197b3d208f9SJaegeuk Kim 1984d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, inode->i_ino); 199b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 2006d20aff8SJaegeuk Kim err = PTR_ERR(ipage); 2016d20aff8SJaegeuk Kim goto out; 202b067ba1fSJaegeuk Kim } 203e18c65b2SHuajun Li 204b3d208f9SJaegeuk Kim set_new_dnode(&dn, inode, ipage, ipage, 0); 205b3d208f9SJaegeuk Kim 206b3d208f9SJaegeuk Kim if (f2fs_has_inline_data(inode)) 207b3d208f9SJaegeuk Kim err = f2fs_convert_inline_page(&dn, page); 208b3d208f9SJaegeuk Kim 209b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 2106d20aff8SJaegeuk Kim out: 211b3d208f9SJaegeuk Kim f2fs_unlock_op(sbi); 212b3d208f9SJaegeuk Kim 213b3d208f9SJaegeuk Kim f2fs_put_page(page, 1); 2142a340760SJaegeuk Kim 2152c4db1a6SJaegeuk Kim f2fs_balance_fs(sbi, dn.node_changed); 2162a340760SJaegeuk Kim 217e18c65b2SHuajun Li return err; 218e18c65b2SHuajun Li } 219e18c65b2SHuajun Li 220b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page) 221e18c65b2SHuajun Li { 222e18c65b2SHuajun Li void *src_addr, *dst_addr; 223e18c65b2SHuajun Li struct dnode_of_data dn; 224e18c65b2SHuajun Li int err; 225e18c65b2SHuajun Li 226e18c65b2SHuajun Li set_new_dnode(&dn, inode, NULL, NULL, 0); 2274d57b86dSChao Yu err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE); 228e18c65b2SHuajun Li if (err) 229e18c65b2SHuajun Li return err; 230e18c65b2SHuajun Li 231c08a690bSJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 232b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 233b3d208f9SJaegeuk Kim return -EAGAIN; 234c08a690bSJaegeuk Kim } 235c08a690bSJaegeuk Kim 236b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_I_SB(inode), page->index); 237b3d208f9SJaegeuk Kim 238bae0ee7aSChao Yu f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true); 239f1e33a04SJaegeuk Kim src_addr = kmap_atomic(page); 240f2470371SChao Yu dst_addr = inline_data_addr(inode, dn.inode_page); 241f2470371SChao Yu memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 242f1e33a04SJaegeuk Kim kunmap_atomic(src_addr); 243ee6d182fSJaegeuk Kim set_page_dirty(dn.inode_page); 244e18c65b2SHuajun Li 2455ec2d99dSMatthew Wilcox f2fs_clear_page_cache_dirty_tag(page); 2460abd8e70SDaeho Jeong 24791942321SJaegeuk Kim set_inode_flag(inode, FI_APPEND_WRITE); 24891942321SJaegeuk Kim set_inode_flag(inode, FI_DATA_EXIST); 249b3d208f9SJaegeuk Kim 2502049d4fcSJaegeuk Kim clear_inline_node(dn.inode_page); 251e18c65b2SHuajun Li f2fs_put_dnode(&dn); 252e18c65b2SHuajun Li return 0; 253e18c65b2SHuajun Li } 2541e1bb4baSJaegeuk Kim 2554d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage) 2561e1bb4baSJaegeuk Kim { 2574081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2581e1bb4baSJaegeuk Kim struct f2fs_inode *ri = NULL; 2591e1bb4baSJaegeuk Kim void *src_addr, *dst_addr; 2601e1bb4baSJaegeuk Kim struct page *ipage; 2611e1bb4baSJaegeuk Kim 2621e1bb4baSJaegeuk Kim /* 2631e1bb4baSJaegeuk Kim * The inline_data recovery policy is as follows. 2641e1bb4baSJaegeuk Kim * [prev.] [next] of inline_data flag 2651e1bb4baSJaegeuk Kim * o o -> recover inline_data 2661e1bb4baSJaegeuk Kim * o x -> remove inline_data, and then recover data blocks 2671e1bb4baSJaegeuk Kim * x o -> remove inline_data, and then recover inline_data 2681e1bb4baSJaegeuk Kim * x x -> recover data blocks 2691e1bb4baSJaegeuk Kim */ 2701e1bb4baSJaegeuk Kim if (IS_INODE(npage)) 2711e1bb4baSJaegeuk Kim ri = F2FS_INODE(npage); 2721e1bb4baSJaegeuk Kim 2731e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode) && 2740342fd30SJaegeuk Kim ri && (ri->i_inline & F2FS_INLINE_DATA)) { 2751e1bb4baSJaegeuk Kim process_inline: 2764d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, inode->i_ino); 2779850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 2781e1bb4baSJaegeuk Kim 279bae0ee7aSChao Yu f2fs_wait_on_page_writeback(ipage, NODE, true, true); 28054b591dfSJaegeuk Kim 281f2470371SChao Yu src_addr = inline_data_addr(inode, npage); 282f2470371SChao Yu dst_addr = inline_data_addr(inode, ipage); 283f2470371SChao Yu memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode)); 284b3d208f9SJaegeuk Kim 28591942321SJaegeuk Kim set_inode_flag(inode, FI_INLINE_DATA); 28691942321SJaegeuk Kim set_inode_flag(inode, FI_DATA_EXIST); 287b3d208f9SJaegeuk Kim 288ee6d182fSJaegeuk Kim set_page_dirty(ipage); 2891e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2900342fd30SJaegeuk Kim return true; 2911e1bb4baSJaegeuk Kim } 2921e1bb4baSJaegeuk Kim 2931e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode)) { 2944d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, inode->i_ino); 2959850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 2964d57b86dSChao Yu f2fs_truncate_inline_inode(inode, ipage, 0); 297bd4667cbSKinglong Mee clear_inode_flag(inode, FI_INLINE_DATA); 2981e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2990342fd30SJaegeuk Kim } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 300c42d28ceSChao Yu if (f2fs_truncate_blocks(inode, 0, false)) 301545fe421SNicholas Krause return false; 3021e1bb4baSJaegeuk Kim goto process_inline; 3031e1bb4baSJaegeuk Kim } 3040342fd30SJaegeuk Kim return false; 3051e1bb4baSJaegeuk Kim } 306201a05beSChao Yu 3074d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir, 3080b81d077SJaegeuk Kim struct fscrypt_name *fname, struct page **res_page) 309201a05beSChao Yu { 310201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 3116e22c691SJaegeuk Kim struct qstr name = FSTR_TO_QSTR(&fname->disk_name); 312201a05beSChao Yu struct f2fs_dir_entry *de; 3137b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 3144e6ebf6dSJaegeuk Kim struct page *ipage; 315f2470371SChao Yu void *inline_dentry; 3166e22c691SJaegeuk Kim f2fs_hash_t namehash; 317201a05beSChao Yu 3184d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, dir->i_ino); 31942d96401SJaegeuk Kim if (IS_ERR(ipage)) { 32042d96401SJaegeuk Kim *res_page = ipage; 321201a05beSChao Yu return NULL; 32242d96401SJaegeuk Kim } 323201a05beSChao Yu 3242c2eb7a3SDaniel Rosenberg namehash = f2fs_dentry_hash(dir, &name, fname); 3256e22c691SJaegeuk Kim 326f2470371SChao Yu inline_dentry = inline_data_addr(dir, ipage); 327201a05beSChao Yu 328f2470371SChao Yu make_dentry_ptr_inline(dir, &d, inline_dentry); 3294d57b86dSChao Yu de = f2fs_find_target_dentry(fname, namehash, NULL, &d); 3304e6ebf6dSJaegeuk Kim unlock_page(ipage); 3314e6ebf6dSJaegeuk Kim if (de) 332201a05beSChao Yu *res_page = ipage; 3334e6ebf6dSJaegeuk Kim else 3344e6ebf6dSJaegeuk Kim f2fs_put_page(ipage, 0); 335201a05beSChao Yu 336201a05beSChao Yu return de; 337201a05beSChao Yu } 338201a05beSChao Yu 3394d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent, 340201a05beSChao Yu struct page *ipage) 341201a05beSChao Yu { 342062a3e7bSJaegeuk Kim struct f2fs_dentry_ptr d; 343f2470371SChao Yu void *inline_dentry; 344201a05beSChao Yu 345f2470371SChao Yu inline_dentry = inline_data_addr(inode, ipage); 346201a05beSChao Yu 347f2470371SChao Yu make_dentry_ptr_inline(inode, &d, inline_dentry); 3484d57b86dSChao Yu f2fs_do_make_empty_dir(inode, parent, &d); 349201a05beSChao Yu 350201a05beSChao Yu set_page_dirty(ipage); 351201a05beSChao Yu 352201a05beSChao Yu /* update i_size to MAX_INLINE_DATA */ 353f2470371SChao Yu if (i_size_read(inode) < MAX_INLINE_DATA(inode)) 354f2470371SChao Yu f2fs_i_size_write(inode, MAX_INLINE_DATA(inode)); 355201a05beSChao Yu return 0; 356201a05beSChao Yu } 357201a05beSChao Yu 358470f00e9SChao Yu /* 359470f00e9SChao Yu * NOTE: ipage is grabbed by caller, but if any error occurs, we should 360470f00e9SChao Yu * release ipage in this function. 361470f00e9SChao Yu */ 362675f10bdSChao Yu static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage, 363f2470371SChao Yu void *inline_dentry) 364201a05beSChao Yu { 365201a05beSChao Yu struct page *page; 366201a05beSChao Yu struct dnode_of_data dn; 367201a05beSChao Yu struct f2fs_dentry_block *dentry_blk; 36876a9dd85SChao Yu struct f2fs_dentry_ptr src, dst; 369201a05beSChao Yu int err; 370201a05beSChao Yu 371300e129cSJaegeuk Kim page = f2fs_grab_cache_page(dir->i_mapping, 0, false); 372470f00e9SChao Yu if (!page) { 373470f00e9SChao Yu f2fs_put_page(ipage, 1); 374201a05beSChao Yu return -ENOMEM; 375470f00e9SChao Yu } 376201a05beSChao Yu 377201a05beSChao Yu set_new_dnode(&dn, dir, ipage, NULL, 0); 378201a05beSChao Yu err = f2fs_reserve_block(&dn, 0); 379201a05beSChao Yu if (err) 380201a05beSChao Yu goto out; 381201a05beSChao Yu 3824dbe38dcSChao Yu if (unlikely(dn.data_blkaddr != NEW_ADDR)) { 3834dbe38dcSChao Yu f2fs_put_dnode(&dn); 3844dbe38dcSChao Yu set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK); 385dcbb4c10SJoe Perches f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.", 3864dbe38dcSChao Yu __func__, dir->i_ino, dn.data_blkaddr); 38710f966bbSChao Yu err = -EFSCORRUPTED; 3884dbe38dcSChao Yu goto out; 3894dbe38dcSChao Yu } 3904dbe38dcSChao Yu 391bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, DATA, true, true); 392201a05beSChao Yu 393bdbc90faSYunlong Song dentry_blk = page_address(page); 394201a05beSChao Yu 395f2470371SChao Yu make_dentry_ptr_inline(dir, &src, inline_dentry); 396f2470371SChao Yu make_dentry_ptr_block(dir, &dst, dentry_blk); 39776a9dd85SChao Yu 398201a05beSChao Yu /* copy data from inline dentry block to new dentry block */ 39976a9dd85SChao Yu memcpy(dst.bitmap, src.bitmap, src.nr_bitmap); 40076a9dd85SChao Yu memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap); 4014ec17d68SChao Yu /* 4024ec17d68SChao Yu * we do not need to zero out remainder part of dentry and filename 4034ec17d68SChao Yu * field, since we have used bitmap for marking the usage status of 4044ec17d68SChao Yu * them, besides, we can also ignore copying/zeroing reserved space 4054ec17d68SChao Yu * of dentry block, because them haven't been used so far. 4064ec17d68SChao Yu */ 40776a9dd85SChao Yu memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max); 40876a9dd85SChao Yu memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN); 409201a05beSChao Yu 410237c0790SJaegeuk Kim if (!PageUptodate(page)) 411201a05beSChao Yu SetPageUptodate(page); 412201a05beSChao Yu set_page_dirty(page); 413201a05beSChao Yu 414201a05beSChao Yu /* clear inline dir and flag after data writeback */ 4154d57b86dSChao Yu f2fs_truncate_inline_inode(dir, ipage, 0); 416b3d208f9SJaegeuk Kim 4173289c061SJaegeuk Kim stat_dec_inline_dir(dir); 41891942321SJaegeuk Kim clear_inode_flag(dir, FI_INLINE_DENTRY); 419201a05beSChao Yu 42045a74688SChao Yu /* 42145a74688SChao Yu * should retrieve reserved space which was used to keep 42245a74688SChao Yu * inline_dentry's structure for backward compatibility. 42345a74688SChao Yu */ 42445a74688SChao Yu if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) && 42545a74688SChao Yu !f2fs_has_inline_xattr(dir)) 42645a74688SChao Yu F2FS_I(dir)->i_inline_xattr_size = 0; 42745a74688SChao Yu 428205b9822SJaegeuk Kim f2fs_i_depth_write(dir, 1); 429ee6d182fSJaegeuk Kim if (i_size_read(dir) < PAGE_SIZE) 430fc9581c8SJaegeuk Kim f2fs_i_size_write(dir, PAGE_SIZE); 431201a05beSChao Yu out: 432201a05beSChao Yu f2fs_put_page(page, 1); 433201a05beSChao Yu return err; 434201a05beSChao Yu } 435201a05beSChao Yu 436f2470371SChao Yu static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry) 437675f10bdSChao Yu { 438675f10bdSChao Yu struct f2fs_dentry_ptr d; 439675f10bdSChao Yu unsigned long bit_pos = 0; 440675f10bdSChao Yu int err = 0; 441675f10bdSChao Yu 442f2470371SChao Yu make_dentry_ptr_inline(dir, &d, inline_dentry); 443675f10bdSChao Yu 444675f10bdSChao Yu while (bit_pos < d.max) { 445675f10bdSChao Yu struct f2fs_dir_entry *de; 446675f10bdSChao Yu struct qstr new_name; 447675f10bdSChao Yu nid_t ino; 448675f10bdSChao Yu umode_t fake_mode; 449675f10bdSChao Yu 450675f10bdSChao Yu if (!test_bit_le(bit_pos, d.bitmap)) { 451675f10bdSChao Yu bit_pos++; 452675f10bdSChao Yu continue; 453675f10bdSChao Yu } 454675f10bdSChao Yu 455675f10bdSChao Yu de = &d.dentry[bit_pos]; 456a4a13f58SChao Yu 457a4a13f58SChao Yu if (unlikely(!de->name_len)) { 458a4a13f58SChao Yu bit_pos++; 459a4a13f58SChao Yu continue; 460a4a13f58SChao Yu } 461a4a13f58SChao Yu 462675f10bdSChao Yu new_name.name = d.filename[bit_pos]; 4630c0b471eSEric Biggers new_name.len = le16_to_cpu(de->name_len); 464675f10bdSChao Yu 465675f10bdSChao Yu ino = le32_to_cpu(de->ino); 4664d57b86dSChao Yu fake_mode = f2fs_get_de_type(de) << S_SHIFT; 467675f10bdSChao Yu 4689421d570SChao Yu err = f2fs_add_regular_entry(dir, &new_name, NULL, NULL, 469675f10bdSChao Yu ino, fake_mode); 470675f10bdSChao Yu if (err) 471675f10bdSChao Yu goto punch_dentry_pages; 472675f10bdSChao Yu 473675f10bdSChao Yu bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 474675f10bdSChao Yu } 475675f10bdSChao Yu return 0; 476675f10bdSChao Yu punch_dentry_pages: 477675f10bdSChao Yu truncate_inode_pages(&dir->i_data, 0); 478c42d28ceSChao Yu f2fs_truncate_blocks(dir, 0, false); 4794d57b86dSChao Yu f2fs_remove_dirty_inode(dir); 480675f10bdSChao Yu return err; 481675f10bdSChao Yu } 482675f10bdSChao Yu 483675f10bdSChao Yu static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage, 484f2470371SChao Yu void *inline_dentry) 485675f10bdSChao Yu { 486f2470371SChao Yu void *backup_dentry; 487675f10bdSChao Yu int err; 488675f10bdSChao Yu 4891ecc0c5cSChao Yu backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir), 490f2470371SChao Yu MAX_INLINE_DATA(dir), GFP_F2FS_ZERO); 4918975bdf4SChao Yu if (!backup_dentry) { 4928975bdf4SChao Yu f2fs_put_page(ipage, 1); 493675f10bdSChao Yu return -ENOMEM; 4948975bdf4SChao Yu } 495675f10bdSChao Yu 496f2470371SChao Yu memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir)); 4974d57b86dSChao Yu f2fs_truncate_inline_inode(dir, ipage, 0); 498675f10bdSChao Yu 499675f10bdSChao Yu unlock_page(ipage); 500675f10bdSChao Yu 501675f10bdSChao Yu err = f2fs_add_inline_entries(dir, backup_dentry); 502675f10bdSChao Yu if (err) 503675f10bdSChao Yu goto recover; 504675f10bdSChao Yu 505675f10bdSChao Yu lock_page(ipage); 506675f10bdSChao Yu 507675f10bdSChao Yu stat_dec_inline_dir(dir); 50891942321SJaegeuk Kim clear_inode_flag(dir, FI_INLINE_DENTRY); 50945a74688SChao Yu 51045a74688SChao Yu /* 51145a74688SChao Yu * should retrieve reserved space which was used to keep 51245a74688SChao Yu * inline_dentry's structure for backward compatibility. 51345a74688SChao Yu */ 51445a74688SChao Yu if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) && 51545a74688SChao Yu !f2fs_has_inline_xattr(dir)) 51645a74688SChao Yu F2FS_I(dir)->i_inline_xattr_size = 0; 51745a74688SChao Yu 5185222595dSJaegeuk Kim kvfree(backup_dentry); 519675f10bdSChao Yu return 0; 520675f10bdSChao Yu recover: 521675f10bdSChao Yu lock_page(ipage); 522bae0ee7aSChao Yu f2fs_wait_on_page_writeback(ipage, NODE, true, true); 523f2470371SChao Yu memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir)); 524205b9822SJaegeuk Kim f2fs_i_depth_write(dir, 0); 525f2470371SChao Yu f2fs_i_size_write(dir, MAX_INLINE_DATA(dir)); 526ee6d182fSJaegeuk Kim set_page_dirty(ipage); 527675f10bdSChao Yu f2fs_put_page(ipage, 1); 528675f10bdSChao Yu 5295222595dSJaegeuk Kim kvfree(backup_dentry); 530675f10bdSChao Yu return err; 531675f10bdSChao Yu } 532675f10bdSChao Yu 533675f10bdSChao Yu static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 534f2470371SChao Yu void *inline_dentry) 535675f10bdSChao Yu { 536675f10bdSChao Yu if (!F2FS_I(dir)->i_dir_level) 537675f10bdSChao Yu return f2fs_move_inline_dirents(dir, ipage, inline_dentry); 538675f10bdSChao Yu else 539675f10bdSChao Yu return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry); 540675f10bdSChao Yu } 541675f10bdSChao Yu 5429421d570SChao Yu int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name, 5439421d570SChao Yu const struct qstr *orig_name, 544510022a8SJaegeuk Kim struct inode *inode, nid_t ino, umode_t mode) 545201a05beSChao Yu { 546201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 547201a05beSChao Yu struct page *ipage; 548201a05beSChao Yu unsigned int bit_pos; 549201a05beSChao Yu f2fs_hash_t name_hash; 550f2470371SChao Yu void *inline_dentry = NULL; 5513b4d732aSChao Yu struct f2fs_dentry_ptr d; 5529421d570SChao Yu int slots = GET_DENTRY_SLOTS(new_name->len); 553510022a8SJaegeuk Kim struct page *page = NULL; 554201a05beSChao Yu int err = 0; 555201a05beSChao Yu 5564d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, dir->i_ino); 557201a05beSChao Yu if (IS_ERR(ipage)) 558201a05beSChao Yu return PTR_ERR(ipage); 559201a05beSChao Yu 560f2470371SChao Yu inline_dentry = inline_data_addr(dir, ipage); 561f2470371SChao Yu make_dentry_ptr_inline(dir, &d, inline_dentry); 56276a9dd85SChao Yu 5634d57b86dSChao Yu bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max); 56476a9dd85SChao Yu if (bit_pos >= d.max) { 565a005774cSTiezhu Yang err = f2fs_convert_inline_dir(dir, ipage, inline_dentry); 566470f00e9SChao Yu if (err) 567470f00e9SChao Yu return err; 568201a05beSChao Yu err = -EAGAIN; 569201a05beSChao Yu goto out; 570201a05beSChao Yu } 571201a05beSChao Yu 572510022a8SJaegeuk Kim if (inode) { 573201a05beSChao Yu down_write(&F2FS_I(inode)->i_sem); 5744d57b86dSChao Yu page = f2fs_init_inode_metadata(inode, dir, new_name, 5759421d570SChao Yu orig_name, ipage); 576201a05beSChao Yu if (IS_ERR(page)) { 577201a05beSChao Yu err = PTR_ERR(page); 578201a05beSChao Yu goto fail; 579201a05beSChao Yu } 580510022a8SJaegeuk Kim } 581bce8d112SJaegeuk Kim 582bae0ee7aSChao Yu f2fs_wait_on_page_writeback(ipage, NODE, true, true); 5833b4d732aSChao Yu 5842c2eb7a3SDaniel Rosenberg name_hash = f2fs_dentry_hash(dir, new_name, NULL); 5859421d570SChao Yu f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos); 5863b4d732aSChao Yu 587201a05beSChao Yu set_page_dirty(ipage); 588201a05beSChao Yu 589201a05beSChao Yu /* we don't need to mark_inode_dirty now */ 590510022a8SJaegeuk Kim if (inode) { 591205b9822SJaegeuk Kim f2fs_i_pino_write(inode, dir->i_ino); 592201a05beSChao Yu f2fs_put_page(page, 1); 593510022a8SJaegeuk Kim } 594201a05beSChao Yu 5954d57b86dSChao Yu f2fs_update_parent_metadata(dir, inode, 0); 596201a05beSChao Yu fail: 597510022a8SJaegeuk Kim if (inode) 598201a05beSChao Yu up_write(&F2FS_I(inode)->i_sem); 599201a05beSChao Yu out: 600201a05beSChao Yu f2fs_put_page(ipage, 1); 601201a05beSChao Yu return err; 602201a05beSChao Yu } 603201a05beSChao Yu 604201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 605201a05beSChao Yu struct inode *dir, struct inode *inode) 606201a05beSChao Yu { 60776a9dd85SChao Yu struct f2fs_dentry_ptr d; 608f2470371SChao Yu void *inline_dentry; 609201a05beSChao Yu int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 610201a05beSChao Yu unsigned int bit_pos; 611201a05beSChao Yu int i; 612201a05beSChao Yu 613201a05beSChao Yu lock_page(page); 614bae0ee7aSChao Yu f2fs_wait_on_page_writeback(page, NODE, true, true); 615201a05beSChao Yu 616f2470371SChao Yu inline_dentry = inline_data_addr(dir, page); 617f2470371SChao Yu make_dentry_ptr_inline(dir, &d, inline_dentry); 61876a9dd85SChao Yu 61976a9dd85SChao Yu bit_pos = dentry - d.dentry; 620201a05beSChao Yu for (i = 0; i < slots; i++) 62176a9dd85SChao Yu __clear_bit_le(bit_pos + i, d.bitmap); 622201a05beSChao Yu 623201a05beSChao Yu set_page_dirty(page); 6249f7c45ccSJaegeuk Kim f2fs_put_page(page, 1); 625201a05beSChao Yu 626078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 6277c45729aSJaegeuk Kim f2fs_mark_inode_dirty_sync(dir, false); 628201a05beSChao Yu 629201a05beSChao Yu if (inode) 6309f7c45ccSJaegeuk Kim f2fs_drop_nlink(dir, inode); 631201a05beSChao Yu } 632201a05beSChao Yu 633201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir) 634201a05beSChao Yu { 635201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 636201a05beSChao Yu struct page *ipage; 637201a05beSChao Yu unsigned int bit_pos = 2; 638f2470371SChao Yu void *inline_dentry; 63976a9dd85SChao Yu struct f2fs_dentry_ptr d; 640201a05beSChao Yu 6414d57b86dSChao Yu ipage = f2fs_get_node_page(sbi, dir->i_ino); 642201a05beSChao Yu if (IS_ERR(ipage)) 643201a05beSChao Yu return false; 644201a05beSChao Yu 645f2470371SChao Yu inline_dentry = inline_data_addr(dir, ipage); 646f2470371SChao Yu make_dentry_ptr_inline(dir, &d, inline_dentry); 64776a9dd85SChao Yu 64876a9dd85SChao Yu bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos); 649201a05beSChao Yu 650201a05beSChao Yu f2fs_put_page(ipage, 1); 651201a05beSChao Yu 65276a9dd85SChao Yu if (bit_pos < d.max) 653201a05beSChao Yu return false; 654201a05beSChao Yu 655201a05beSChao Yu return true; 656201a05beSChao Yu } 657201a05beSChao Yu 658d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 6590b81d077SJaegeuk Kim struct fscrypt_str *fstr) 660201a05beSChao Yu { 661201a05beSChao Yu struct inode *inode = file_inode(file); 662201a05beSChao Yu struct page *ipage = NULL; 6637b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 664f2470371SChao Yu void *inline_dentry = NULL; 665ed6bd4b1SChao Yu int err; 666201a05beSChao Yu 66776a9dd85SChao Yu make_dentry_ptr_inline(inode, &d, inline_dentry); 66876a9dd85SChao Yu 66976a9dd85SChao Yu if (ctx->pos == d.max) 670201a05beSChao Yu return 0; 671201a05beSChao Yu 6724d57b86dSChao Yu ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 673201a05beSChao Yu if (IS_ERR(ipage)) 674201a05beSChao Yu return PTR_ERR(ipage); 675201a05beSChao Yu 676aadcef64SChao Yu /* 677aadcef64SChao Yu * f2fs_readdir was protected by inode.i_rwsem, it is safe to access 678aadcef64SChao Yu * ipage without page's lock held. 679aadcef64SChao Yu */ 680aadcef64SChao Yu unlock_page(ipage); 681aadcef64SChao Yu 682f2470371SChao Yu inline_dentry = inline_data_addr(inode, ipage); 683201a05beSChao Yu 68464c24ecbSTomohiro Kusumi make_dentry_ptr_inline(inode, &d, inline_dentry); 6857b3cd7d6SJaegeuk Kim 686ed6bd4b1SChao Yu err = f2fs_fill_dentries(ctx, &d, 0, fstr); 687ed6bd4b1SChao Yu if (!err) 68876a9dd85SChao Yu ctx->pos = d.max; 689201a05beSChao Yu 690aadcef64SChao Yu f2fs_put_page(ipage, 0); 691ed6bd4b1SChao Yu return err < 0 ? err : 0; 692201a05beSChao Yu } 69367f8cf3cSJaegeuk Kim 69467f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *inode, 69567f8cf3cSJaegeuk Kim struct fiemap_extent_info *fieinfo, __u64 start, __u64 len) 69667f8cf3cSJaegeuk Kim { 69767f8cf3cSJaegeuk Kim __u64 byteaddr, ilen; 69867f8cf3cSJaegeuk Kim __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | 69967f8cf3cSJaegeuk Kim FIEMAP_EXTENT_LAST; 70067f8cf3cSJaegeuk Kim struct node_info ni; 70167f8cf3cSJaegeuk Kim struct page *ipage; 70267f8cf3cSJaegeuk Kim int err = 0; 70367f8cf3cSJaegeuk Kim 7044d57b86dSChao Yu ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); 70567f8cf3cSJaegeuk Kim if (IS_ERR(ipage)) 70667f8cf3cSJaegeuk Kim return PTR_ERR(ipage); 70767f8cf3cSJaegeuk Kim 7087975f349SChao Yu if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) && 7097975f349SChao Yu !f2fs_has_inline_data(inode)) { 7107975f349SChao Yu err = -EAGAIN; 7117975f349SChao Yu goto out; 7127975f349SChao Yu } 7137975f349SChao Yu 7147975f349SChao Yu if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) { 71567f8cf3cSJaegeuk Kim err = -EAGAIN; 71667f8cf3cSJaegeuk Kim goto out; 71767f8cf3cSJaegeuk Kim } 71867f8cf3cSJaegeuk Kim 719f2470371SChao Yu ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode)); 72067f8cf3cSJaegeuk Kim if (start >= ilen) 72167f8cf3cSJaegeuk Kim goto out; 72267f8cf3cSJaegeuk Kim if (start + len < ilen) 72367f8cf3cSJaegeuk Kim ilen = start + len; 72467f8cf3cSJaegeuk Kim ilen -= start; 72567f8cf3cSJaegeuk Kim 7267735730dSChao Yu err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); 7277735730dSChao Yu if (err) 7287735730dSChao Yu goto out; 7297735730dSChao Yu 73067f8cf3cSJaegeuk Kim byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; 731f2470371SChao Yu byteaddr += (char *)inline_data_addr(inode, ipage) - 732f2470371SChao Yu (char *)F2FS_INODE(ipage); 73367f8cf3cSJaegeuk Kim err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); 73467f8cf3cSJaegeuk Kim out: 73567f8cf3cSJaegeuk Kim f2fs_put_page(ipage, 1); 73667f8cf3cSJaegeuk Kim return err; 73767f8cf3cSJaegeuk Kim } 738