1e18c65b2SHuajun Li /* 2e18c65b2SHuajun Li * fs/f2fs/inline.c 3e18c65b2SHuajun Li * Copyright (c) 2013, Intel Corporation 4e18c65b2SHuajun Li * Authors: Huajun Li <huajun.li@intel.com> 5e18c65b2SHuajun Li * Haicheng Li <haicheng.li@intel.com> 6e18c65b2SHuajun Li * This program is free software; you can redistribute it and/or modify 7e18c65b2SHuajun Li * it under the terms of the GNU General Public License version 2 as 8e18c65b2SHuajun Li * published by the Free Software Foundation. 9e18c65b2SHuajun Li */ 10e18c65b2SHuajun Li 11e18c65b2SHuajun Li #include <linux/fs.h> 12e18c65b2SHuajun Li #include <linux/f2fs_fs.h> 13e18c65b2SHuajun Li 14e18c65b2SHuajun Li #include "f2fs.h" 1567f8cf3cSJaegeuk Kim #include "node.h" 16e18c65b2SHuajun Li 1701b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *inode) 18e18c65b2SHuajun Li { 1988b88a66SJaegeuk Kim if (f2fs_is_atomic_file(inode)) 2088b88a66SJaegeuk Kim return false; 2188b88a66SJaegeuk Kim 22368a0e40SWanpeng Li if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode)) 23e18c65b2SHuajun Li return false; 24e18c65b2SHuajun Li 2592dffd01SJaegeuk Kim if (i_size_read(inode) > MAX_INLINE_DATA) 2692dffd01SJaegeuk Kim return false; 2792dffd01SJaegeuk Kim 28fcc85a4dSJaegeuk Kim if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) 29fcc85a4dSJaegeuk Kim return false; 30fcc85a4dSJaegeuk Kim 31e18c65b2SHuajun Li return true; 32e18c65b2SHuajun Li } 33e18c65b2SHuajun Li 3401b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode) 3501b960e9SJaegeuk Kim { 3601b960e9SJaegeuk Kim if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY)) 3701b960e9SJaegeuk Kim return false; 3801b960e9SJaegeuk Kim 3901b960e9SJaegeuk Kim if (!S_ISDIR(inode->i_mode)) 4001b960e9SJaegeuk Kim return false; 4101b960e9SJaegeuk Kim 4201b960e9SJaegeuk Kim return true; 4301b960e9SJaegeuk Kim } 4401b960e9SJaegeuk Kim 45b3d208f9SJaegeuk Kim void read_inline_data(struct page *page, struct page *ipage) 46e18c65b2SHuajun Li { 47e18c65b2SHuajun Li void *src_addr, *dst_addr; 48e18c65b2SHuajun Li 49b3d208f9SJaegeuk Kim if (PageUptodate(page)) 50b3d208f9SJaegeuk Kim return; 5104a17fb1SChao Yu 52b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_P_SB(page), page->index); 53e18c65b2SHuajun Li 5409cbfeafSKirill A. Shutemov zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE); 55e18c65b2SHuajun Li 56e18c65b2SHuajun Li /* Copy the whole inline data block */ 57e18c65b2SHuajun Li src_addr = inline_data_addr(ipage); 58f1e33a04SJaegeuk Kim dst_addr = kmap_atomic(page); 59e18c65b2SHuajun Li memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 60427a45c8SJaegeuk Kim flush_dcache_page(page); 61f1e33a04SJaegeuk Kim kunmap_atomic(dst_addr); 62e18c65b2SHuajun Li SetPageUptodate(page); 63b3d208f9SJaegeuk Kim } 64e18c65b2SHuajun Li 650bfcfccaSChao Yu bool truncate_inline_inode(struct page *ipage, u64 from) 66feeb0debSChao Yu { 670bfcfccaSChao Yu void *addr; 680bfcfccaSChao Yu 690bfcfccaSChao Yu if (from >= MAX_INLINE_DATA) 700bfcfccaSChao Yu return false; 710bfcfccaSChao Yu 720bfcfccaSChao Yu addr = inline_data_addr(ipage); 730bfcfccaSChao Yu 74fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(ipage, NODE, true); 750bfcfccaSChao Yu memset(addr + from, 0, MAX_INLINE_DATA - from); 76ee6d182fSJaegeuk Kim set_page_dirty(ipage); 770bfcfccaSChao Yu return true; 78feeb0debSChao Yu } 79feeb0debSChao Yu 80b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page) 81b3d208f9SJaegeuk Kim { 82b3d208f9SJaegeuk Kim struct page *ipage; 83b3d208f9SJaegeuk Kim 84b3d208f9SJaegeuk Kim ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 85b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 86b3d208f9SJaegeuk Kim unlock_page(page); 87b3d208f9SJaegeuk Kim return PTR_ERR(ipage); 88b3d208f9SJaegeuk Kim } 89b3d208f9SJaegeuk Kim 90b3d208f9SJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 91b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 92b3d208f9SJaegeuk Kim return -EAGAIN; 93b3d208f9SJaegeuk Kim } 94b3d208f9SJaegeuk Kim 95b3d208f9SJaegeuk Kim if (page->index) 9609cbfeafSKirill A. Shutemov zero_user_segment(page, 0, PAGE_SIZE); 97b3d208f9SJaegeuk Kim else 98b3d208f9SJaegeuk Kim read_inline_data(page, ipage); 99b3d208f9SJaegeuk Kim 100b3d208f9SJaegeuk Kim SetPageUptodate(page); 101b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 102b3d208f9SJaegeuk Kim unlock_page(page); 103e18c65b2SHuajun Li return 0; 104e18c65b2SHuajun Li } 105e18c65b2SHuajun Li 106b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page) 107e18c65b2SHuajun Li { 108e18c65b2SHuajun Li struct f2fs_io_info fio = { 10905ca3632SJaegeuk Kim .sbi = F2FS_I_SB(dn->inode), 110e18c65b2SHuajun Li .type = DATA, 111e18c65b2SHuajun Li .rw = WRITE_SYNC | REQ_PRIO, 11205ca3632SJaegeuk Kim .page = page, 1134375a336SJaegeuk Kim .encrypted_page = NULL, 114e18c65b2SHuajun Li }; 115158c194cSJaegeuk Kim int dirty, err; 116e18c65b2SHuajun Li 117b3d208f9SJaegeuk Kim if (!f2fs_exist_data(dn->inode)) 118b3d208f9SJaegeuk Kim goto clear_out; 119ec4e7af4SJaegeuk Kim 120b3d208f9SJaegeuk Kim err = f2fs_reserve_block(dn, 0); 12115c6e3aaSJaegeuk Kim if (err) 122b3d208f9SJaegeuk Kim return err; 123e18c65b2SHuajun Li 12485ead818SYunlei He f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page)); 125b3d208f9SJaegeuk Kim 1268060656aSShawn Lin read_inline_data(page, dn->inode_page); 1276282adbfSJaegeuk Kim set_page_dirty(page); 1286282adbfSJaegeuk Kim 129158c194cSJaegeuk Kim /* clear dirty state */ 130158c194cSJaegeuk Kim dirty = clear_page_dirty_for_io(page); 131158c194cSJaegeuk Kim 132e18c65b2SHuajun Li /* write data page to try to make data consistent */ 133e18c65b2SHuajun Li set_page_writeback(page); 1347a9d7548SChao Yu fio.old_blkaddr = dn->data_blkaddr; 13505ca3632SJaegeuk Kim write_data_page(dn, &fio); 136fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(page, DATA, true); 137158c194cSJaegeuk Kim if (dirty) 138158c194cSJaegeuk Kim inode_dec_dirty_pages(dn->inode); 139e18c65b2SHuajun Li 14095f5b0fcSJaegeuk Kim /* this converted inline_data should be recovered. */ 14191942321SJaegeuk Kim set_inode_flag(dn->inode, FI_APPEND_WRITE); 14295f5b0fcSJaegeuk Kim 143e18c65b2SHuajun Li /* clear inline data and flag after data writeback */ 1440bfcfccaSChao Yu truncate_inline_inode(dn->inode_page, 0); 1452049d4fcSJaegeuk Kim clear_inline_node(dn->inode_page); 146b3d208f9SJaegeuk Kim clear_out: 147b3d208f9SJaegeuk Kim stat_dec_inline_inode(dn->inode); 14857e2a2c0SJaegeuk Kim f2fs_clear_inline_inode(dn->inode); 149b3d208f9SJaegeuk Kim f2fs_put_dnode(dn); 150b3d208f9SJaegeuk Kim return 0; 151e18c65b2SHuajun Li } 152e18c65b2SHuajun Li 153b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode) 154e18c65b2SHuajun Li { 155b3d208f9SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 156b3d208f9SJaegeuk Kim struct dnode_of_data dn; 157b3d208f9SJaegeuk Kim struct page *ipage, *page; 158b3d208f9SJaegeuk Kim int err = 0; 159e18c65b2SHuajun Li 160b9d777b8SJaegeuk Kim if (!f2fs_has_inline_data(inode)) 161b9d777b8SJaegeuk Kim return 0; 162b9d777b8SJaegeuk Kim 163300e129cSJaegeuk Kim page = f2fs_grab_cache_page(inode->i_mapping, 0, false); 164b3d208f9SJaegeuk Kim if (!page) 1659e09fc85SJaegeuk Kim return -ENOMEM; 166b3d208f9SJaegeuk Kim 167b3d208f9SJaegeuk Kim f2fs_lock_op(sbi); 168b3d208f9SJaegeuk Kim 169b3d208f9SJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 170b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 1716d20aff8SJaegeuk Kim err = PTR_ERR(ipage); 1726d20aff8SJaegeuk Kim goto out; 173b067ba1fSJaegeuk Kim } 174e18c65b2SHuajun Li 175b3d208f9SJaegeuk Kim set_new_dnode(&dn, inode, ipage, ipage, 0); 176b3d208f9SJaegeuk Kim 177b3d208f9SJaegeuk Kim if (f2fs_has_inline_data(inode)) 178b3d208f9SJaegeuk Kim err = f2fs_convert_inline_page(&dn, page); 179b3d208f9SJaegeuk Kim 180b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 1816d20aff8SJaegeuk Kim out: 182b3d208f9SJaegeuk Kim f2fs_unlock_op(sbi); 183b3d208f9SJaegeuk Kim 184b3d208f9SJaegeuk Kim f2fs_put_page(page, 1); 1852a340760SJaegeuk Kim 1862c4db1a6SJaegeuk Kim f2fs_balance_fs(sbi, dn.node_changed); 1872a340760SJaegeuk Kim 188e18c65b2SHuajun Li return err; 189e18c65b2SHuajun Li } 190e18c65b2SHuajun Li 191b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page) 192e18c65b2SHuajun Li { 193e18c65b2SHuajun Li void *src_addr, *dst_addr; 194e18c65b2SHuajun Li struct dnode_of_data dn; 195e18c65b2SHuajun Li int err; 196e18c65b2SHuajun Li 197e18c65b2SHuajun Li set_new_dnode(&dn, inode, NULL, NULL, 0); 198e18c65b2SHuajun Li err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); 199e18c65b2SHuajun Li if (err) 200e18c65b2SHuajun Li return err; 201e18c65b2SHuajun Li 202c08a690bSJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 203b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 204b3d208f9SJaegeuk Kim return -EAGAIN; 205c08a690bSJaegeuk Kim } 206c08a690bSJaegeuk Kim 207b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_I_SB(inode), page->index); 208b3d208f9SJaegeuk Kim 209fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(dn.inode_page, NODE, true); 210f1e33a04SJaegeuk Kim src_addr = kmap_atomic(page); 211b3d208f9SJaegeuk Kim dst_addr = inline_data_addr(dn.inode_page); 212b3d208f9SJaegeuk Kim memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 213f1e33a04SJaegeuk Kim kunmap_atomic(src_addr); 214ee6d182fSJaegeuk Kim set_page_dirty(dn.inode_page); 215e18c65b2SHuajun Li 21691942321SJaegeuk Kim set_inode_flag(inode, FI_APPEND_WRITE); 21791942321SJaegeuk Kim set_inode_flag(inode, FI_DATA_EXIST); 218b3d208f9SJaegeuk Kim 2192049d4fcSJaegeuk Kim clear_inline_node(dn.inode_page); 220e18c65b2SHuajun Li f2fs_put_dnode(&dn); 221e18c65b2SHuajun Li return 0; 222e18c65b2SHuajun Li } 2231e1bb4baSJaegeuk Kim 2240342fd30SJaegeuk Kim bool recover_inline_data(struct inode *inode, struct page *npage) 2251e1bb4baSJaegeuk Kim { 2264081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2271e1bb4baSJaegeuk Kim struct f2fs_inode *ri = NULL; 2281e1bb4baSJaegeuk Kim void *src_addr, *dst_addr; 2291e1bb4baSJaegeuk Kim struct page *ipage; 2301e1bb4baSJaegeuk Kim 2311e1bb4baSJaegeuk Kim /* 2321e1bb4baSJaegeuk Kim * The inline_data recovery policy is as follows. 2331e1bb4baSJaegeuk Kim * [prev.] [next] of inline_data flag 2341e1bb4baSJaegeuk Kim * o o -> recover inline_data 2351e1bb4baSJaegeuk Kim * o x -> remove inline_data, and then recover data blocks 2361e1bb4baSJaegeuk Kim * x o -> remove inline_data, and then recover inline_data 2371e1bb4baSJaegeuk Kim * x x -> recover data blocks 2381e1bb4baSJaegeuk Kim */ 2391e1bb4baSJaegeuk Kim if (IS_INODE(npage)) 2401e1bb4baSJaegeuk Kim ri = F2FS_INODE(npage); 2411e1bb4baSJaegeuk Kim 2421e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode) && 2430342fd30SJaegeuk Kim ri && (ri->i_inline & F2FS_INLINE_DATA)) { 2441e1bb4baSJaegeuk Kim process_inline: 2451e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2469850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 2471e1bb4baSJaegeuk Kim 248fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(ipage, NODE, true); 24954b591dfSJaegeuk Kim 2501e1bb4baSJaegeuk Kim src_addr = inline_data_addr(npage); 2511e1bb4baSJaegeuk Kim dst_addr = inline_data_addr(ipage); 2521e1bb4baSJaegeuk Kim memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 253b3d208f9SJaegeuk Kim 25491942321SJaegeuk Kim set_inode_flag(inode, FI_INLINE_DATA); 25591942321SJaegeuk Kim set_inode_flag(inode, FI_DATA_EXIST); 256b3d208f9SJaegeuk Kim 257ee6d182fSJaegeuk Kim set_page_dirty(ipage); 2581e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2590342fd30SJaegeuk Kim return true; 2601e1bb4baSJaegeuk Kim } 2611e1bb4baSJaegeuk Kim 2621e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode)) { 2631e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2649850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 265545fe421SNicholas Krause if (!truncate_inline_inode(ipage, 0)) 266545fe421SNicholas Krause return false; 267b3d208f9SJaegeuk Kim f2fs_clear_inline_inode(inode); 2681e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2690342fd30SJaegeuk Kim } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 270545fe421SNicholas Krause if (truncate_blocks(inode, 0, false)) 271545fe421SNicholas Krause return false; 2721e1bb4baSJaegeuk Kim goto process_inline; 2731e1bb4baSJaegeuk Kim } 2740342fd30SJaegeuk Kim return false; 2751e1bb4baSJaegeuk Kim } 276201a05beSChao Yu 277201a05beSChao Yu struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir, 2780b81d077SJaegeuk Kim struct fscrypt_name *fname, struct page **res_page) 279201a05beSChao Yu { 280201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 2814e6ebf6dSJaegeuk Kim struct f2fs_inline_dentry *inline_dentry; 2826e22c691SJaegeuk Kim struct qstr name = FSTR_TO_QSTR(&fname->disk_name); 283201a05beSChao Yu struct f2fs_dir_entry *de; 2847b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 2854e6ebf6dSJaegeuk Kim struct page *ipage; 2866e22c691SJaegeuk Kim f2fs_hash_t namehash; 287201a05beSChao Yu 288201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 28942d96401SJaegeuk Kim if (IS_ERR(ipage)) { 29042d96401SJaegeuk Kim *res_page = ipage; 291201a05beSChao Yu return NULL; 29242d96401SJaegeuk Kim } 293201a05beSChao Yu 2946e22c691SJaegeuk Kim namehash = f2fs_dentry_hash(&name); 2956e22c691SJaegeuk Kim 2964e6ebf6dSJaegeuk Kim inline_dentry = inline_data_addr(ipage); 297201a05beSChao Yu 298d8c6822aSJaegeuk Kim make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2); 2996e22c691SJaegeuk Kim de = find_target_dentry(fname, namehash, NULL, &d); 3004e6ebf6dSJaegeuk Kim unlock_page(ipage); 3014e6ebf6dSJaegeuk Kim if (de) 302201a05beSChao Yu *res_page = ipage; 3034e6ebf6dSJaegeuk Kim else 3044e6ebf6dSJaegeuk Kim f2fs_put_page(ipage, 0); 305201a05beSChao Yu 306201a05beSChao Yu return de; 307201a05beSChao Yu } 308201a05beSChao Yu 309201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir, 310201a05beSChao Yu struct page **p) 311201a05beSChao Yu { 312201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 313201a05beSChao Yu struct page *ipage; 314201a05beSChao Yu struct f2fs_dir_entry *de; 315201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 316201a05beSChao Yu 317201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 318201a05beSChao Yu if (IS_ERR(ipage)) 319201a05beSChao Yu return NULL; 320201a05beSChao Yu 321201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 322201a05beSChao Yu de = &dentry_blk->dentry[1]; 323201a05beSChao Yu *p = ipage; 324201a05beSChao Yu unlock_page(ipage); 325201a05beSChao Yu return de; 326201a05beSChao Yu } 327201a05beSChao Yu 328201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *parent, 329201a05beSChao Yu struct page *ipage) 330201a05beSChao Yu { 331201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 332062a3e7bSJaegeuk Kim struct f2fs_dentry_ptr d; 333201a05beSChao Yu 334201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 335201a05beSChao Yu 336d8c6822aSJaegeuk Kim make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2); 337062a3e7bSJaegeuk Kim do_make_empty_dir(inode, parent, &d); 338201a05beSChao Yu 339201a05beSChao Yu set_page_dirty(ipage); 340201a05beSChao Yu 341201a05beSChao Yu /* update i_size to MAX_INLINE_DATA */ 342ee6d182fSJaegeuk Kim if (i_size_read(inode) < MAX_INLINE_DATA) 343fc9581c8SJaegeuk Kim f2fs_i_size_write(inode, MAX_INLINE_DATA); 344201a05beSChao Yu return 0; 345201a05beSChao Yu } 346201a05beSChao Yu 347470f00e9SChao Yu /* 348470f00e9SChao Yu * NOTE: ipage is grabbed by caller, but if any error occurs, we should 349470f00e9SChao Yu * release ipage in this function. 350470f00e9SChao Yu */ 351675f10bdSChao Yu static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage, 352201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry) 353201a05beSChao Yu { 354201a05beSChao Yu struct page *page; 355201a05beSChao Yu struct dnode_of_data dn; 356201a05beSChao Yu struct f2fs_dentry_block *dentry_blk; 357201a05beSChao Yu int err; 358201a05beSChao Yu 359300e129cSJaegeuk Kim page = f2fs_grab_cache_page(dir->i_mapping, 0, false); 360470f00e9SChao Yu if (!page) { 361470f00e9SChao Yu f2fs_put_page(ipage, 1); 362201a05beSChao Yu return -ENOMEM; 363470f00e9SChao Yu } 364201a05beSChao Yu 365201a05beSChao Yu set_new_dnode(&dn, dir, ipage, NULL, 0); 366201a05beSChao Yu err = f2fs_reserve_block(&dn, 0); 367201a05beSChao Yu if (err) 368201a05beSChao Yu goto out; 369201a05beSChao Yu 370fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(page, DATA, true); 37109cbfeafSKirill A. Shutemov zero_user_segment(page, MAX_INLINE_DATA, PAGE_SIZE); 372201a05beSChao Yu 373f1e33a04SJaegeuk Kim dentry_blk = kmap_atomic(page); 374201a05beSChao Yu 375201a05beSChao Yu /* copy data from inline dentry block to new dentry block */ 376201a05beSChao Yu memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, 377201a05beSChao Yu INLINE_DENTRY_BITMAP_SIZE); 3784ec17d68SChao Yu memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0, 3794ec17d68SChao Yu SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE); 3804ec17d68SChao Yu /* 3814ec17d68SChao Yu * we do not need to zero out remainder part of dentry and filename 3824ec17d68SChao Yu * field, since we have used bitmap for marking the usage status of 3834ec17d68SChao Yu * them, besides, we can also ignore copying/zeroing reserved space 3844ec17d68SChao Yu * of dentry block, because them haven't been used so far. 3854ec17d68SChao Yu */ 386201a05beSChao Yu memcpy(dentry_blk->dentry, inline_dentry->dentry, 387201a05beSChao Yu sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); 388201a05beSChao Yu memcpy(dentry_blk->filename, inline_dentry->filename, 389201a05beSChao Yu NR_INLINE_DENTRY * F2FS_SLOT_LEN); 390201a05beSChao Yu 391f1e33a04SJaegeuk Kim kunmap_atomic(dentry_blk); 392201a05beSChao Yu SetPageUptodate(page); 393201a05beSChao Yu set_page_dirty(page); 394201a05beSChao Yu 395201a05beSChao Yu /* clear inline dir and flag after data writeback */ 3960bfcfccaSChao Yu truncate_inline_inode(ipage, 0); 397b3d208f9SJaegeuk Kim 3983289c061SJaegeuk Kim stat_dec_inline_dir(dir); 39991942321SJaegeuk Kim clear_inode_flag(dir, FI_INLINE_DENTRY); 400201a05beSChao Yu 401205b9822SJaegeuk Kim f2fs_i_depth_write(dir, 1); 402ee6d182fSJaegeuk Kim if (i_size_read(dir) < PAGE_SIZE) 403fc9581c8SJaegeuk Kim f2fs_i_size_write(dir, PAGE_SIZE); 404201a05beSChao Yu out: 405201a05beSChao Yu f2fs_put_page(page, 1); 406201a05beSChao Yu return err; 407201a05beSChao Yu } 408201a05beSChao Yu 409675f10bdSChao Yu static int f2fs_add_inline_entries(struct inode *dir, 410675f10bdSChao Yu struct f2fs_inline_dentry *inline_dentry) 411675f10bdSChao Yu { 412675f10bdSChao Yu struct f2fs_dentry_ptr d; 413675f10bdSChao Yu unsigned long bit_pos = 0; 414675f10bdSChao Yu int err = 0; 415675f10bdSChao Yu 416675f10bdSChao Yu make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2); 417675f10bdSChao Yu 418675f10bdSChao Yu while (bit_pos < d.max) { 419675f10bdSChao Yu struct f2fs_dir_entry *de; 420675f10bdSChao Yu struct qstr new_name; 421675f10bdSChao Yu nid_t ino; 422675f10bdSChao Yu umode_t fake_mode; 423675f10bdSChao Yu 424675f10bdSChao Yu if (!test_bit_le(bit_pos, d.bitmap)) { 425675f10bdSChao Yu bit_pos++; 426675f10bdSChao Yu continue; 427675f10bdSChao Yu } 428675f10bdSChao Yu 429675f10bdSChao Yu de = &d.dentry[bit_pos]; 430a4a13f58SChao Yu 431a4a13f58SChao Yu if (unlikely(!de->name_len)) { 432a4a13f58SChao Yu bit_pos++; 433a4a13f58SChao Yu continue; 434a4a13f58SChao Yu } 435a4a13f58SChao Yu 436675f10bdSChao Yu new_name.name = d.filename[bit_pos]; 437675f10bdSChao Yu new_name.len = de->name_len; 438675f10bdSChao Yu 439675f10bdSChao Yu ino = le32_to_cpu(de->ino); 440675f10bdSChao Yu fake_mode = get_de_type(de) << S_SHIFT; 441675f10bdSChao Yu 442675f10bdSChao Yu err = f2fs_add_regular_entry(dir, &new_name, NULL, 443675f10bdSChao Yu ino, fake_mode); 444675f10bdSChao Yu if (err) 445675f10bdSChao Yu goto punch_dentry_pages; 446675f10bdSChao Yu 447675f10bdSChao Yu bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)); 448675f10bdSChao Yu } 449675f10bdSChao Yu return 0; 450675f10bdSChao Yu punch_dentry_pages: 451675f10bdSChao Yu truncate_inode_pages(&dir->i_data, 0); 452675f10bdSChao Yu truncate_blocks(dir, 0, false); 453675f10bdSChao Yu remove_dirty_inode(dir); 454675f10bdSChao Yu return err; 455675f10bdSChao Yu } 456675f10bdSChao Yu 457675f10bdSChao Yu static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage, 458675f10bdSChao Yu struct f2fs_inline_dentry *inline_dentry) 459675f10bdSChao Yu { 460675f10bdSChao Yu struct f2fs_inline_dentry *backup_dentry; 461675f10bdSChao Yu int err; 462675f10bdSChao Yu 4630414b004SJaegeuk Kim backup_dentry = f2fs_kmalloc(sizeof(struct f2fs_inline_dentry), 464675f10bdSChao Yu GFP_F2FS_ZERO); 4658975bdf4SChao Yu if (!backup_dentry) { 4668975bdf4SChao Yu f2fs_put_page(ipage, 1); 467675f10bdSChao Yu return -ENOMEM; 4688975bdf4SChao Yu } 469675f10bdSChao Yu 470675f10bdSChao Yu memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA); 471675f10bdSChao Yu truncate_inline_inode(ipage, 0); 472675f10bdSChao Yu 473675f10bdSChao Yu unlock_page(ipage); 474675f10bdSChao Yu 475675f10bdSChao Yu err = f2fs_add_inline_entries(dir, backup_dentry); 476675f10bdSChao Yu if (err) 477675f10bdSChao Yu goto recover; 478675f10bdSChao Yu 479675f10bdSChao Yu lock_page(ipage); 480675f10bdSChao Yu 481675f10bdSChao Yu stat_dec_inline_dir(dir); 48291942321SJaegeuk Kim clear_inode_flag(dir, FI_INLINE_DENTRY); 483675f10bdSChao Yu kfree(backup_dentry); 484675f10bdSChao Yu return 0; 485675f10bdSChao Yu recover: 486675f10bdSChao Yu lock_page(ipage); 487675f10bdSChao Yu memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA); 488205b9822SJaegeuk Kim f2fs_i_depth_write(dir, 0); 489fc9581c8SJaegeuk Kim f2fs_i_size_write(dir, MAX_INLINE_DATA); 490ee6d182fSJaegeuk Kim set_page_dirty(ipage); 491675f10bdSChao Yu f2fs_put_page(ipage, 1); 492675f10bdSChao Yu 493675f10bdSChao Yu kfree(backup_dentry); 494675f10bdSChao Yu return err; 495675f10bdSChao Yu } 496675f10bdSChao Yu 497675f10bdSChao Yu static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 498675f10bdSChao Yu struct f2fs_inline_dentry *inline_dentry) 499675f10bdSChao Yu { 500675f10bdSChao Yu if (!F2FS_I(dir)->i_dir_level) 501675f10bdSChao Yu return f2fs_move_inline_dirents(dir, ipage, inline_dentry); 502675f10bdSChao Yu else 503675f10bdSChao Yu return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry); 504675f10bdSChao Yu } 505675f10bdSChao Yu 506201a05beSChao Yu int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name, 507510022a8SJaegeuk Kim struct inode *inode, nid_t ino, umode_t mode) 508201a05beSChao Yu { 509201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 510201a05beSChao Yu struct page *ipage; 511201a05beSChao Yu unsigned int bit_pos; 512201a05beSChao Yu f2fs_hash_t name_hash; 513201a05beSChao Yu size_t namelen = name->len; 514201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk = NULL; 5153b4d732aSChao Yu struct f2fs_dentry_ptr d; 516201a05beSChao Yu int slots = GET_DENTRY_SLOTS(namelen); 517510022a8SJaegeuk Kim struct page *page = NULL; 518201a05beSChao Yu int err = 0; 519201a05beSChao Yu 520201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 521201a05beSChao Yu if (IS_ERR(ipage)) 522201a05beSChao Yu return PTR_ERR(ipage); 523201a05beSChao Yu 524201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 525a82afa20SJaegeuk Kim bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, 526a82afa20SJaegeuk Kim slots, NR_INLINE_DENTRY); 527201a05beSChao Yu if (bit_pos >= NR_INLINE_DENTRY) { 528201a05beSChao Yu err = f2fs_convert_inline_dir(dir, ipage, dentry_blk); 529470f00e9SChao Yu if (err) 530470f00e9SChao Yu return err; 531201a05beSChao Yu err = -EAGAIN; 532201a05beSChao Yu goto out; 533201a05beSChao Yu } 534201a05beSChao Yu 535510022a8SJaegeuk Kim if (inode) { 536201a05beSChao Yu down_write(&F2FS_I(inode)->i_sem); 537bce8d112SJaegeuk Kim page = init_inode_metadata(inode, dir, name, ipage); 538201a05beSChao Yu if (IS_ERR(page)) { 539201a05beSChao Yu err = PTR_ERR(page); 540201a05beSChao Yu goto fail; 541201a05beSChao Yu } 542510022a8SJaegeuk Kim } 543bce8d112SJaegeuk Kim 544fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(ipage, NODE, true); 5453b4d732aSChao Yu 5463b4d732aSChao Yu name_hash = f2fs_dentry_hash(name); 547d8c6822aSJaegeuk Kim make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2); 548510022a8SJaegeuk Kim f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos); 5493b4d732aSChao Yu 550201a05beSChao Yu set_page_dirty(ipage); 551201a05beSChao Yu 552201a05beSChao Yu /* we don't need to mark_inode_dirty now */ 553510022a8SJaegeuk Kim if (inode) { 554205b9822SJaegeuk Kim f2fs_i_pino_write(inode, dir->i_ino); 555201a05beSChao Yu f2fs_put_page(page, 1); 556510022a8SJaegeuk Kim } 557201a05beSChao Yu 558201a05beSChao Yu update_parent_metadata(dir, inode, 0); 559201a05beSChao Yu fail: 560510022a8SJaegeuk Kim if (inode) 561201a05beSChao Yu up_write(&F2FS_I(inode)->i_sem); 562201a05beSChao Yu out: 563201a05beSChao Yu f2fs_put_page(ipage, 1); 564201a05beSChao Yu return err; 565201a05beSChao Yu } 566201a05beSChao Yu 567201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 568201a05beSChao Yu struct inode *dir, struct inode *inode) 569201a05beSChao Yu { 570201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry; 571201a05beSChao Yu int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 572201a05beSChao Yu unsigned int bit_pos; 573201a05beSChao Yu int i; 574201a05beSChao Yu 575201a05beSChao Yu lock_page(page); 576fec1d657SJaegeuk Kim f2fs_wait_on_page_writeback(page, NODE, true); 577201a05beSChao Yu 578201a05beSChao Yu inline_dentry = inline_data_addr(page); 579201a05beSChao Yu bit_pos = dentry - inline_dentry->dentry; 580201a05beSChao Yu for (i = 0; i < slots; i++) 581201a05beSChao Yu test_and_clear_bit_le(bit_pos + i, 582201a05beSChao Yu &inline_dentry->dentry_bitmap); 583201a05beSChao Yu 584201a05beSChao Yu set_page_dirty(page); 585*9f7c45ccSJaegeuk Kim f2fs_put_page(page, 1); 586201a05beSChao Yu 587201a05beSChao Yu dir->i_ctime = dir->i_mtime = CURRENT_TIME; 588205b9822SJaegeuk Kim mark_inode_dirty_sync(dir); 589201a05beSChao Yu 590201a05beSChao Yu if (inode) 591*9f7c45ccSJaegeuk Kim f2fs_drop_nlink(dir, inode); 592201a05beSChao Yu } 593201a05beSChao Yu 594201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir) 595201a05beSChao Yu { 596201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 597201a05beSChao Yu struct page *ipage; 598201a05beSChao Yu unsigned int bit_pos = 2; 599201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 600201a05beSChao Yu 601201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 602201a05beSChao Yu if (IS_ERR(ipage)) 603201a05beSChao Yu return false; 604201a05beSChao Yu 605201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 606201a05beSChao Yu bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 607201a05beSChao Yu NR_INLINE_DENTRY, 608201a05beSChao Yu bit_pos); 609201a05beSChao Yu 610201a05beSChao Yu f2fs_put_page(ipage, 1); 611201a05beSChao Yu 612201a05beSChao Yu if (bit_pos < NR_INLINE_DENTRY) 613201a05beSChao Yu return false; 614201a05beSChao Yu 615201a05beSChao Yu return true; 616201a05beSChao Yu } 617201a05beSChao Yu 618d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx, 6190b81d077SJaegeuk Kim struct fscrypt_str *fstr) 620201a05beSChao Yu { 621201a05beSChao Yu struct inode *inode = file_inode(file); 622201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry = NULL; 623201a05beSChao Yu struct page *ipage = NULL; 6247b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 625201a05beSChao Yu 626201a05beSChao Yu if (ctx->pos == NR_INLINE_DENTRY) 627201a05beSChao Yu return 0; 628201a05beSChao Yu 62938594de7SJaegeuk Kim ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 630201a05beSChao Yu if (IS_ERR(ipage)) 631201a05beSChao Yu return PTR_ERR(ipage); 632201a05beSChao Yu 633201a05beSChao Yu inline_dentry = inline_data_addr(ipage); 634201a05beSChao Yu 635d8c6822aSJaegeuk Kim make_dentry_ptr(inode, &d, (void *)inline_dentry, 2); 6367b3cd7d6SJaegeuk Kim 637d8c6822aSJaegeuk Kim if (!f2fs_fill_dentries(ctx, &d, 0, fstr)) 638201a05beSChao Yu ctx->pos = NR_INLINE_DENTRY; 639201a05beSChao Yu 64038594de7SJaegeuk Kim f2fs_put_page(ipage, 1); 641201a05beSChao Yu return 0; 642201a05beSChao Yu } 64367f8cf3cSJaegeuk Kim 64467f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *inode, 64567f8cf3cSJaegeuk Kim struct fiemap_extent_info *fieinfo, __u64 start, __u64 len) 64667f8cf3cSJaegeuk Kim { 64767f8cf3cSJaegeuk Kim __u64 byteaddr, ilen; 64867f8cf3cSJaegeuk Kim __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | 64967f8cf3cSJaegeuk Kim FIEMAP_EXTENT_LAST; 65067f8cf3cSJaegeuk Kim struct node_info ni; 65167f8cf3cSJaegeuk Kim struct page *ipage; 65267f8cf3cSJaegeuk Kim int err = 0; 65367f8cf3cSJaegeuk Kim 65467f8cf3cSJaegeuk Kim ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 65567f8cf3cSJaegeuk Kim if (IS_ERR(ipage)) 65667f8cf3cSJaegeuk Kim return PTR_ERR(ipage); 65767f8cf3cSJaegeuk Kim 65867f8cf3cSJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 65967f8cf3cSJaegeuk Kim err = -EAGAIN; 66067f8cf3cSJaegeuk Kim goto out; 66167f8cf3cSJaegeuk Kim } 66267f8cf3cSJaegeuk Kim 66367f8cf3cSJaegeuk Kim ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode)); 66467f8cf3cSJaegeuk Kim if (start >= ilen) 66567f8cf3cSJaegeuk Kim goto out; 66667f8cf3cSJaegeuk Kim if (start + len < ilen) 66767f8cf3cSJaegeuk Kim ilen = start + len; 66867f8cf3cSJaegeuk Kim ilen -= start; 66967f8cf3cSJaegeuk Kim 67067f8cf3cSJaegeuk Kim get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni); 67167f8cf3cSJaegeuk Kim byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits; 67267f8cf3cSJaegeuk Kim byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage); 67367f8cf3cSJaegeuk Kim err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags); 67467f8cf3cSJaegeuk Kim out: 67567f8cf3cSJaegeuk Kim f2fs_put_page(ipage, 1); 67667f8cf3cSJaegeuk Kim return err; 67767f8cf3cSJaegeuk Kim } 678