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" 15e18c65b2SHuajun Li 16e18c65b2SHuajun Li bool f2fs_may_inline(struct inode *inode) 17e18c65b2SHuajun Li { 184081363fSJaegeuk Kim if (!test_opt(F2FS_I_SB(inode), INLINE_DATA)) 19e18c65b2SHuajun Li return false; 20e18c65b2SHuajun Li 2188b88a66SJaegeuk Kim if (f2fs_is_atomic_file(inode)) 2288b88a66SJaegeuk Kim return false; 2388b88a66SJaegeuk Kim 24b3d208f9SJaegeuk Kim if (!S_ISREG(inode->i_mode)) 25e18c65b2SHuajun Li return false; 26e18c65b2SHuajun Li 2792dffd01SJaegeuk Kim if (i_size_read(inode) > MAX_INLINE_DATA) 2892dffd01SJaegeuk Kim return false; 2992dffd01SJaegeuk Kim 30e18c65b2SHuajun Li return true; 31e18c65b2SHuajun Li } 32e18c65b2SHuajun Li 33b3d208f9SJaegeuk Kim void read_inline_data(struct page *page, struct page *ipage) 34e18c65b2SHuajun Li { 35e18c65b2SHuajun Li void *src_addr, *dst_addr; 36e18c65b2SHuajun Li 37b3d208f9SJaegeuk Kim if (PageUptodate(page)) 38b3d208f9SJaegeuk Kim return; 3904a17fb1SChao Yu 40b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_P_SB(page), page->index); 41e18c65b2SHuajun Li 4218309aaaSChao Yu zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 43e18c65b2SHuajun Li 44e18c65b2SHuajun Li /* Copy the whole inline data block */ 45e18c65b2SHuajun Li src_addr = inline_data_addr(ipage); 46f1e33a04SJaegeuk Kim dst_addr = kmap_atomic(page); 47e18c65b2SHuajun Li memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 48427a45c8SJaegeuk Kim flush_dcache_page(page); 49f1e33a04SJaegeuk Kim kunmap_atomic(dst_addr); 50e18c65b2SHuajun Li SetPageUptodate(page); 51b3d208f9SJaegeuk Kim } 52e18c65b2SHuajun Li 53feeb0debSChao Yu static void truncate_inline_data(struct page *ipage) 54feeb0debSChao Yu { 55feeb0debSChao Yu f2fs_wait_on_page_writeback(ipage, NODE); 56feeb0debSChao Yu memset(inline_data_addr(ipage), 0, MAX_INLINE_DATA); 57feeb0debSChao Yu } 58feeb0debSChao Yu 59b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page) 60b3d208f9SJaegeuk Kim { 61b3d208f9SJaegeuk Kim struct page *ipage; 62b3d208f9SJaegeuk Kim 63b3d208f9SJaegeuk Kim ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 64b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 65b3d208f9SJaegeuk Kim unlock_page(page); 66b3d208f9SJaegeuk Kim return PTR_ERR(ipage); 67b3d208f9SJaegeuk Kim } 68b3d208f9SJaegeuk Kim 69b3d208f9SJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 70b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 71b3d208f9SJaegeuk Kim return -EAGAIN; 72b3d208f9SJaegeuk Kim } 73b3d208f9SJaegeuk Kim 74b3d208f9SJaegeuk Kim if (page->index) 75b3d208f9SJaegeuk Kim zero_user_segment(page, 0, PAGE_CACHE_SIZE); 76b3d208f9SJaegeuk Kim else 77b3d208f9SJaegeuk Kim read_inline_data(page, ipage); 78b3d208f9SJaegeuk Kim 79b3d208f9SJaegeuk Kim SetPageUptodate(page); 80b3d208f9SJaegeuk Kim f2fs_put_page(ipage, 1); 81b3d208f9SJaegeuk Kim unlock_page(page); 82e18c65b2SHuajun Li return 0; 83e18c65b2SHuajun Li } 84e18c65b2SHuajun Li 85b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page) 86e18c65b2SHuajun Li { 87e18c65b2SHuajun Li void *src_addr, *dst_addr; 88e18c65b2SHuajun Li struct f2fs_io_info fio = { 89e18c65b2SHuajun Li .type = DATA, 90e18c65b2SHuajun Li .rw = WRITE_SYNC | REQ_PRIO, 91e18c65b2SHuajun Li }; 92158c194cSJaegeuk Kim int dirty, err; 93e18c65b2SHuajun Li 94b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_I_SB(dn->inode), page->index); 95e18c65b2SHuajun Li 96b3d208f9SJaegeuk Kim if (!f2fs_exist_data(dn->inode)) 97b3d208f9SJaegeuk Kim goto clear_out; 98ec4e7af4SJaegeuk Kim 99b3d208f9SJaegeuk Kim err = f2fs_reserve_block(dn, 0); 10015c6e3aaSJaegeuk Kim if (err) 101b3d208f9SJaegeuk Kim return err; 102e18c65b2SHuajun Li 1039ac1349aSJaegeuk Kim f2fs_wait_on_page_writeback(page, DATA); 104b3d208f9SJaegeuk Kim 105b3d208f9SJaegeuk Kim if (PageUptodate(page)) 106b3d208f9SJaegeuk Kim goto no_update; 107b3d208f9SJaegeuk Kim 10818309aaaSChao Yu zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 109e18c65b2SHuajun Li 110e18c65b2SHuajun Li /* Copy the whole inline data block */ 111b3d208f9SJaegeuk Kim src_addr = inline_data_addr(dn->inode_page); 112f1e33a04SJaegeuk Kim dst_addr = kmap_atomic(page); 113e18c65b2SHuajun Li memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 11409b8b3c8SJaegeuk Kim flush_dcache_page(page); 115f1e33a04SJaegeuk Kim kunmap_atomic(dst_addr); 1169e09fc85SJaegeuk Kim SetPageUptodate(page); 117b3d208f9SJaegeuk Kim no_update: 118158c194cSJaegeuk Kim /* clear dirty state */ 119158c194cSJaegeuk Kim dirty = clear_page_dirty_for_io(page); 120158c194cSJaegeuk Kim 121e18c65b2SHuajun Li /* write data page to try to make data consistent */ 122e18c65b2SHuajun Li set_page_writeback(page); 123cf04e8ebSJaegeuk Kim fio.blk_addr = dn->data_blkaddr; 124cf04e8ebSJaegeuk Kim write_data_page(page, dn, &fio); 125e1509cf2SJaegeuk Kim update_extent_cache(dn); 1265514f0aaSYuan Zhong f2fs_wait_on_page_writeback(page, DATA); 127158c194cSJaegeuk Kim if (dirty) 128158c194cSJaegeuk Kim inode_dec_dirty_pages(dn->inode); 129e18c65b2SHuajun Li 13095f5b0fcSJaegeuk Kim /* this converted inline_data should be recovered. */ 13195f5b0fcSJaegeuk Kim set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE); 13295f5b0fcSJaegeuk Kim 133e18c65b2SHuajun Li /* clear inline data and flag after data writeback */ 134feeb0debSChao Yu truncate_inline_data(dn->inode_page); 135b3d208f9SJaegeuk Kim clear_out: 136b3d208f9SJaegeuk Kim stat_dec_inline_inode(dn->inode); 13757e2a2c0SJaegeuk Kim f2fs_clear_inline_inode(dn->inode); 138b3d208f9SJaegeuk Kim sync_inode_page(dn); 139b3d208f9SJaegeuk Kim f2fs_put_dnode(dn); 140b3d208f9SJaegeuk Kim return 0; 141e18c65b2SHuajun Li } 142e18c65b2SHuajun Li 143b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode) 144e18c65b2SHuajun Li { 145b3d208f9SJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 146b3d208f9SJaegeuk Kim struct dnode_of_data dn; 147b3d208f9SJaegeuk Kim struct page *ipage, *page; 148b3d208f9SJaegeuk Kim int err = 0; 149e18c65b2SHuajun Li 150b3d208f9SJaegeuk Kim page = grab_cache_page(inode->i_mapping, 0); 151b3d208f9SJaegeuk Kim if (!page) 1529e09fc85SJaegeuk Kim return -ENOMEM; 153b3d208f9SJaegeuk Kim 154b3d208f9SJaegeuk Kim f2fs_lock_op(sbi); 155b3d208f9SJaegeuk Kim 156b3d208f9SJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 157b3d208f9SJaegeuk Kim if (IS_ERR(ipage)) { 1586d20aff8SJaegeuk Kim err = PTR_ERR(ipage); 1596d20aff8SJaegeuk Kim goto out; 160b067ba1fSJaegeuk Kim } 161e18c65b2SHuajun Li 162b3d208f9SJaegeuk Kim set_new_dnode(&dn, inode, ipage, ipage, 0); 163b3d208f9SJaegeuk Kim 164b3d208f9SJaegeuk Kim if (f2fs_has_inline_data(inode)) 165b3d208f9SJaegeuk Kim err = f2fs_convert_inline_page(&dn, page); 166b3d208f9SJaegeuk Kim 167b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 1686d20aff8SJaegeuk Kim out: 169b3d208f9SJaegeuk Kim f2fs_unlock_op(sbi); 170b3d208f9SJaegeuk Kim 171b3d208f9SJaegeuk Kim f2fs_put_page(page, 1); 172e18c65b2SHuajun Li return err; 173e18c65b2SHuajun Li } 174e18c65b2SHuajun Li 175b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page) 176e18c65b2SHuajun Li { 177e18c65b2SHuajun Li void *src_addr, *dst_addr; 178e18c65b2SHuajun Li struct dnode_of_data dn; 179e18c65b2SHuajun Li int err; 180e18c65b2SHuajun Li 181e18c65b2SHuajun Li set_new_dnode(&dn, inode, NULL, NULL, 0); 182e18c65b2SHuajun Li err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); 183e18c65b2SHuajun Li if (err) 184e18c65b2SHuajun Li return err; 185e18c65b2SHuajun Li 186c08a690bSJaegeuk Kim if (!f2fs_has_inline_data(inode)) { 187b3d208f9SJaegeuk Kim f2fs_put_dnode(&dn); 188b3d208f9SJaegeuk Kim return -EAGAIN; 189c08a690bSJaegeuk Kim } 190c08a690bSJaegeuk Kim 191b3d208f9SJaegeuk Kim f2fs_bug_on(F2FS_I_SB(inode), page->index); 192b3d208f9SJaegeuk Kim 193b3d208f9SJaegeuk Kim f2fs_wait_on_page_writeback(dn.inode_page, NODE); 194f1e33a04SJaegeuk Kim src_addr = kmap_atomic(page); 195b3d208f9SJaegeuk Kim dst_addr = inline_data_addr(dn.inode_page); 196b3d208f9SJaegeuk Kim memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 197f1e33a04SJaegeuk Kim kunmap_atomic(src_addr); 198e18c65b2SHuajun Li 199fff04f90SJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); 200b3d208f9SJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); 201b3d208f9SJaegeuk Kim 202e18c65b2SHuajun Li sync_inode_page(&dn); 203e18c65b2SHuajun Li f2fs_put_dnode(&dn); 204e18c65b2SHuajun Li return 0; 205e18c65b2SHuajun Li } 2061e1bb4baSJaegeuk Kim 2070342fd30SJaegeuk Kim bool recover_inline_data(struct inode *inode, struct page *npage) 2081e1bb4baSJaegeuk Kim { 2094081363fSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 2101e1bb4baSJaegeuk Kim struct f2fs_inode *ri = NULL; 2111e1bb4baSJaegeuk Kim void *src_addr, *dst_addr; 2121e1bb4baSJaegeuk Kim struct page *ipage; 2131e1bb4baSJaegeuk Kim 2141e1bb4baSJaegeuk Kim /* 2151e1bb4baSJaegeuk Kim * The inline_data recovery policy is as follows. 2161e1bb4baSJaegeuk Kim * [prev.] [next] of inline_data flag 2171e1bb4baSJaegeuk Kim * o o -> recover inline_data 2181e1bb4baSJaegeuk Kim * o x -> remove inline_data, and then recover data blocks 2191e1bb4baSJaegeuk Kim * x o -> remove inline_data, and then recover inline_data 2201e1bb4baSJaegeuk Kim * x x -> recover data blocks 2211e1bb4baSJaegeuk Kim */ 2221e1bb4baSJaegeuk Kim if (IS_INODE(npage)) 2231e1bb4baSJaegeuk Kim ri = F2FS_INODE(npage); 2241e1bb4baSJaegeuk Kim 2251e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode) && 2260342fd30SJaegeuk Kim ri && (ri->i_inline & F2FS_INLINE_DATA)) { 2271e1bb4baSJaegeuk Kim process_inline: 2281e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2299850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 2301e1bb4baSJaegeuk Kim 23154b591dfSJaegeuk Kim f2fs_wait_on_page_writeback(ipage, NODE); 23254b591dfSJaegeuk Kim 2331e1bb4baSJaegeuk Kim src_addr = inline_data_addr(npage); 2341e1bb4baSJaegeuk Kim dst_addr = inline_data_addr(ipage); 2351e1bb4baSJaegeuk Kim memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 236b3d208f9SJaegeuk Kim 237b3d208f9SJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 238b3d208f9SJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); 239b3d208f9SJaegeuk Kim 2401e1bb4baSJaegeuk Kim update_inode(inode, ipage); 2411e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2420342fd30SJaegeuk Kim return true; 2431e1bb4baSJaegeuk Kim } 2441e1bb4baSJaegeuk Kim 2451e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode)) { 2461e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2479850cf4aSJaegeuk Kim f2fs_bug_on(sbi, IS_ERR(ipage)); 248feeb0debSChao Yu truncate_inline_data(ipage); 249b3d208f9SJaegeuk Kim f2fs_clear_inline_inode(inode); 2501e1bb4baSJaegeuk Kim update_inode(inode, ipage); 2511e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2520342fd30SJaegeuk Kim } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) { 253764aa3e9SJaegeuk Kim truncate_blocks(inode, 0, false); 2541e1bb4baSJaegeuk Kim goto process_inline; 2551e1bb4baSJaegeuk Kim } 2560342fd30SJaegeuk Kim return false; 2571e1bb4baSJaegeuk Kim } 258201a05beSChao Yu 259201a05beSChao Yu struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir, 260201a05beSChao Yu struct qstr *name, struct page **res_page) 261201a05beSChao Yu { 262201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb); 2634e6ebf6dSJaegeuk Kim struct f2fs_inline_dentry *inline_dentry; 264201a05beSChao Yu struct f2fs_dir_entry *de; 2657b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 2664e6ebf6dSJaegeuk Kim struct page *ipage; 267201a05beSChao Yu 268201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 269201a05beSChao Yu if (IS_ERR(ipage)) 270201a05beSChao Yu return NULL; 271201a05beSChao Yu 2724e6ebf6dSJaegeuk Kim inline_dentry = inline_data_addr(ipage); 273201a05beSChao Yu 2747b3cd7d6SJaegeuk Kim make_dentry_ptr(&d, (void *)inline_dentry, 2); 2757b3cd7d6SJaegeuk Kim de = find_target_dentry(name, NULL, &d); 2767b3cd7d6SJaegeuk Kim 2774e6ebf6dSJaegeuk Kim unlock_page(ipage); 2784e6ebf6dSJaegeuk Kim if (de) 279201a05beSChao Yu *res_page = ipage; 2804e6ebf6dSJaegeuk Kim else 2814e6ebf6dSJaegeuk Kim f2fs_put_page(ipage, 0); 282201a05beSChao Yu 283201a05beSChao Yu /* 284201a05beSChao Yu * For the most part, it should be a bug when name_len is zero. 2854e6ebf6dSJaegeuk Kim * We stop here for figuring out where the bugs has occurred. 286201a05beSChao Yu */ 2877b3cd7d6SJaegeuk Kim f2fs_bug_on(sbi, d.max < 0); 288201a05beSChao Yu return de; 289201a05beSChao Yu } 290201a05beSChao Yu 291201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir, 292201a05beSChao Yu struct page **p) 293201a05beSChao Yu { 294201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 295201a05beSChao Yu struct page *ipage; 296201a05beSChao Yu struct f2fs_dir_entry *de; 297201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 298201a05beSChao Yu 299201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 300201a05beSChao Yu if (IS_ERR(ipage)) 301201a05beSChao Yu return NULL; 302201a05beSChao Yu 303201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 304201a05beSChao Yu de = &dentry_blk->dentry[1]; 305201a05beSChao Yu *p = ipage; 306201a05beSChao Yu unlock_page(ipage); 307201a05beSChao Yu return de; 308201a05beSChao Yu } 309201a05beSChao Yu 310201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *parent, 311201a05beSChao Yu struct page *ipage) 312201a05beSChao Yu { 313201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 314062a3e7bSJaegeuk Kim struct f2fs_dentry_ptr d; 315201a05beSChao Yu 316201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 317201a05beSChao Yu 318062a3e7bSJaegeuk Kim make_dentry_ptr(&d, (void *)dentry_blk, 2); 319062a3e7bSJaegeuk Kim do_make_empty_dir(inode, parent, &d); 320201a05beSChao Yu 321201a05beSChao Yu set_page_dirty(ipage); 322201a05beSChao Yu 323201a05beSChao Yu /* update i_size to MAX_INLINE_DATA */ 324201a05beSChao Yu if (i_size_read(inode) < MAX_INLINE_DATA) { 325201a05beSChao Yu i_size_write(inode, MAX_INLINE_DATA); 326201a05beSChao Yu set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); 327201a05beSChao Yu } 328201a05beSChao Yu return 0; 329201a05beSChao Yu } 330201a05beSChao Yu 331d64948a4SJaegeuk Kim static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage, 332201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry) 333201a05beSChao Yu { 334201a05beSChao Yu struct page *page; 335201a05beSChao Yu struct dnode_of_data dn; 336201a05beSChao Yu struct f2fs_dentry_block *dentry_blk; 337201a05beSChao Yu int err; 338201a05beSChao Yu 339201a05beSChao Yu page = grab_cache_page(dir->i_mapping, 0); 340201a05beSChao Yu if (!page) 341201a05beSChao Yu return -ENOMEM; 342201a05beSChao Yu 343201a05beSChao Yu set_new_dnode(&dn, dir, ipage, NULL, 0); 344201a05beSChao Yu err = f2fs_reserve_block(&dn, 0); 345201a05beSChao Yu if (err) 346201a05beSChao Yu goto out; 347201a05beSChao Yu 348201a05beSChao Yu f2fs_wait_on_page_writeback(page, DATA); 349201a05beSChao Yu zero_user_segment(page, 0, PAGE_CACHE_SIZE); 350201a05beSChao Yu 351f1e33a04SJaegeuk Kim dentry_blk = kmap_atomic(page); 352201a05beSChao Yu 353201a05beSChao Yu /* copy data from inline dentry block to new dentry block */ 354201a05beSChao Yu memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap, 355201a05beSChao Yu INLINE_DENTRY_BITMAP_SIZE); 356201a05beSChao Yu memcpy(dentry_blk->dentry, inline_dentry->dentry, 357201a05beSChao Yu sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY); 358201a05beSChao Yu memcpy(dentry_blk->filename, inline_dentry->filename, 359201a05beSChao Yu NR_INLINE_DENTRY * F2FS_SLOT_LEN); 360201a05beSChao Yu 361f1e33a04SJaegeuk Kim kunmap_atomic(dentry_blk); 362201a05beSChao Yu SetPageUptodate(page); 363201a05beSChao Yu set_page_dirty(page); 364201a05beSChao Yu 365201a05beSChao Yu /* clear inline dir and flag after data writeback */ 366feeb0debSChao Yu truncate_inline_data(ipage); 367b3d208f9SJaegeuk Kim 3683289c061SJaegeuk Kim stat_dec_inline_dir(dir); 369622f28aeSChao Yu clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY); 370201a05beSChao Yu 371201a05beSChao Yu if (i_size_read(dir) < PAGE_CACHE_SIZE) { 372201a05beSChao Yu i_size_write(dir, PAGE_CACHE_SIZE); 373201a05beSChao Yu set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 374201a05beSChao Yu } 375201a05beSChao Yu 376201a05beSChao Yu sync_inode_page(&dn); 377201a05beSChao Yu out: 378201a05beSChao Yu f2fs_put_page(page, 1); 379201a05beSChao Yu return err; 380201a05beSChao Yu } 381201a05beSChao Yu 382201a05beSChao Yu int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name, 383201a05beSChao Yu struct inode *inode) 384201a05beSChao Yu { 385201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 386201a05beSChao Yu struct page *ipage; 387201a05beSChao Yu unsigned int bit_pos; 388201a05beSChao Yu f2fs_hash_t name_hash; 389201a05beSChao Yu size_t namelen = name->len; 390201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk = NULL; 391*3b4d732aSChao Yu struct f2fs_dentry_ptr d; 392201a05beSChao Yu int slots = GET_DENTRY_SLOTS(namelen); 393201a05beSChao Yu struct page *page; 394201a05beSChao Yu int err = 0; 395201a05beSChao Yu 396201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 397201a05beSChao Yu if (IS_ERR(ipage)) 398201a05beSChao Yu return PTR_ERR(ipage); 399201a05beSChao Yu 400201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 401a82afa20SJaegeuk Kim bit_pos = room_for_filename(&dentry_blk->dentry_bitmap, 402a82afa20SJaegeuk Kim slots, NR_INLINE_DENTRY); 403201a05beSChao Yu if (bit_pos >= NR_INLINE_DENTRY) { 404201a05beSChao Yu err = f2fs_convert_inline_dir(dir, ipage, dentry_blk); 405201a05beSChao Yu if (!err) 406201a05beSChao Yu err = -EAGAIN; 407201a05beSChao Yu goto out; 408201a05beSChao Yu } 409201a05beSChao Yu 410201a05beSChao Yu down_write(&F2FS_I(inode)->i_sem); 411bce8d112SJaegeuk Kim page = init_inode_metadata(inode, dir, name, ipage); 412201a05beSChao Yu if (IS_ERR(page)) { 413201a05beSChao Yu err = PTR_ERR(page); 414201a05beSChao Yu goto fail; 415201a05beSChao Yu } 416bce8d112SJaegeuk Kim 417bce8d112SJaegeuk Kim f2fs_wait_on_page_writeback(ipage, NODE); 418*3b4d732aSChao Yu 419*3b4d732aSChao Yu name_hash = f2fs_dentry_hash(name); 420*3b4d732aSChao Yu make_dentry_ptr(&d, (void *)dentry_blk, 2); 421*3b4d732aSChao Yu f2fs_update_dentry(inode, &d, name, name_hash, bit_pos); 422*3b4d732aSChao Yu 423201a05beSChao Yu set_page_dirty(ipage); 424201a05beSChao Yu 425201a05beSChao Yu /* we don't need to mark_inode_dirty now */ 426201a05beSChao Yu F2FS_I(inode)->i_pino = dir->i_ino; 427201a05beSChao Yu update_inode(inode, page); 428201a05beSChao Yu f2fs_put_page(page, 1); 429201a05beSChao Yu 430201a05beSChao Yu update_parent_metadata(dir, inode, 0); 431201a05beSChao Yu fail: 432201a05beSChao Yu up_write(&F2FS_I(inode)->i_sem); 433201a05beSChao Yu 434201a05beSChao Yu if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) { 435201a05beSChao Yu update_inode(dir, ipage); 436201a05beSChao Yu clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR); 437201a05beSChao Yu } 438201a05beSChao Yu out: 439201a05beSChao Yu f2fs_put_page(ipage, 1); 440201a05beSChao Yu return err; 441201a05beSChao Yu } 442201a05beSChao Yu 443201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page, 444201a05beSChao Yu struct inode *dir, struct inode *inode) 445201a05beSChao Yu { 446201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry; 447201a05beSChao Yu int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len)); 448201a05beSChao Yu unsigned int bit_pos; 449201a05beSChao Yu int i; 450201a05beSChao Yu 451201a05beSChao Yu lock_page(page); 45259a06155SJaegeuk Kim f2fs_wait_on_page_writeback(page, NODE); 453201a05beSChao Yu 454201a05beSChao Yu inline_dentry = inline_data_addr(page); 455201a05beSChao Yu bit_pos = dentry - inline_dentry->dentry; 456201a05beSChao Yu for (i = 0; i < slots; i++) 457201a05beSChao Yu test_and_clear_bit_le(bit_pos + i, 458201a05beSChao Yu &inline_dentry->dentry_bitmap); 459201a05beSChao Yu 460201a05beSChao Yu set_page_dirty(page); 461201a05beSChao Yu 462201a05beSChao Yu dir->i_ctime = dir->i_mtime = CURRENT_TIME; 463201a05beSChao Yu 464201a05beSChao Yu if (inode) 465201a05beSChao Yu f2fs_drop_nlink(dir, inode, page); 466201a05beSChao Yu 467201a05beSChao Yu f2fs_put_page(page, 1); 468201a05beSChao Yu } 469201a05beSChao Yu 470201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir) 471201a05beSChao Yu { 472201a05beSChao Yu struct f2fs_sb_info *sbi = F2FS_I_SB(dir); 473201a05beSChao Yu struct page *ipage; 474201a05beSChao Yu unsigned int bit_pos = 2; 475201a05beSChao Yu struct f2fs_inline_dentry *dentry_blk; 476201a05beSChao Yu 477201a05beSChao Yu ipage = get_node_page(sbi, dir->i_ino); 478201a05beSChao Yu if (IS_ERR(ipage)) 479201a05beSChao Yu return false; 480201a05beSChao Yu 481201a05beSChao Yu dentry_blk = inline_data_addr(ipage); 482201a05beSChao Yu bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap, 483201a05beSChao Yu NR_INLINE_DENTRY, 484201a05beSChao Yu bit_pos); 485201a05beSChao Yu 486201a05beSChao Yu f2fs_put_page(ipage, 1); 487201a05beSChao Yu 488201a05beSChao Yu if (bit_pos < NR_INLINE_DENTRY) 489201a05beSChao Yu return false; 490201a05beSChao Yu 491201a05beSChao Yu return true; 492201a05beSChao Yu } 493201a05beSChao Yu 494201a05beSChao Yu int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx) 495201a05beSChao Yu { 496201a05beSChao Yu struct inode *inode = file_inode(file); 497201a05beSChao Yu struct f2fs_inline_dentry *inline_dentry = NULL; 498201a05beSChao Yu struct page *ipage = NULL; 4997b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr d; 500201a05beSChao Yu 501201a05beSChao Yu if (ctx->pos == NR_INLINE_DENTRY) 502201a05beSChao Yu return 0; 503201a05beSChao Yu 50438594de7SJaegeuk Kim ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino); 505201a05beSChao Yu if (IS_ERR(ipage)) 506201a05beSChao Yu return PTR_ERR(ipage); 507201a05beSChao Yu 508201a05beSChao Yu inline_dentry = inline_data_addr(ipage); 509201a05beSChao Yu 5107b3cd7d6SJaegeuk Kim make_dentry_ptr(&d, (void *)inline_dentry, 2); 5117b3cd7d6SJaegeuk Kim 5127b3cd7d6SJaegeuk Kim if (!f2fs_fill_dentries(ctx, &d, 0)) 513201a05beSChao Yu ctx->pos = NR_INLINE_DENTRY; 514201a05beSChao Yu 51538594de7SJaegeuk Kim f2fs_put_page(ipage, 1); 516201a05beSChao Yu return 0; 517201a05beSChao Yu } 518