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 { 18e18c65b2SHuajun Li struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 19e18c65b2SHuajun Li block_t nr_blocks; 20e18c65b2SHuajun Li loff_t i_size; 21e18c65b2SHuajun Li 22e18c65b2SHuajun Li if (!test_opt(sbi, INLINE_DATA)) 23e18c65b2SHuajun Li return false; 24e18c65b2SHuajun Li 25e18c65b2SHuajun Li nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2; 26e18c65b2SHuajun Li if (inode->i_blocks > nr_blocks) 27e18c65b2SHuajun Li return false; 28e18c65b2SHuajun Li 29e18c65b2SHuajun Li i_size = i_size_read(inode); 30e18c65b2SHuajun Li if (i_size > MAX_INLINE_DATA) 31e18c65b2SHuajun Li return false; 32e18c65b2SHuajun Li 33e18c65b2SHuajun Li return true; 34e18c65b2SHuajun Li } 35e18c65b2SHuajun Li 36e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *inode, struct page *page) 37e18c65b2SHuajun Li { 38e18c65b2SHuajun Li struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 39e18c65b2SHuajun Li struct page *ipage; 40e18c65b2SHuajun Li void *src_addr, *dst_addr; 41e18c65b2SHuajun Li 4204a17fb1SChao Yu if (page->index) { 4304a17fb1SChao Yu zero_user_segment(page, 0, PAGE_CACHE_SIZE); 4404a17fb1SChao Yu goto out; 4504a17fb1SChao Yu } 4604a17fb1SChao Yu 47e18c65b2SHuajun Li ipage = get_node_page(sbi, inode->i_ino); 48*d54c795bSChao Yu if (IS_ERR(ipage)) { 49*d54c795bSChao Yu unlock_page(page); 50e18c65b2SHuajun Li return PTR_ERR(ipage); 51*d54c795bSChao Yu } 52e18c65b2SHuajun Li 5318309aaaSChao Yu zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 54e18c65b2SHuajun Li 55e18c65b2SHuajun Li /* Copy the whole inline data block */ 56e18c65b2SHuajun Li src_addr = inline_data_addr(ipage); 57e18c65b2SHuajun Li dst_addr = kmap(page); 58e18c65b2SHuajun Li memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 59e18c65b2SHuajun Li kunmap(page); 60e18c65b2SHuajun Li f2fs_put_page(ipage, 1); 61e18c65b2SHuajun Li 6204a17fb1SChao Yu out: 63e18c65b2SHuajun Li SetPageUptodate(page); 64e18c65b2SHuajun Li unlock_page(page); 65e18c65b2SHuajun Li 66e18c65b2SHuajun Li return 0; 67e18c65b2SHuajun Li } 68e18c65b2SHuajun Li 69e18c65b2SHuajun Li static int __f2fs_convert_inline_data(struct inode *inode, struct page *page) 70e18c65b2SHuajun Li { 71e18c65b2SHuajun Li int err; 72e18c65b2SHuajun Li struct page *ipage; 73e18c65b2SHuajun Li struct dnode_of_data dn; 74e18c65b2SHuajun Li void *src_addr, *dst_addr; 75e18c65b2SHuajun Li block_t new_blk_addr; 76e18c65b2SHuajun Li struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 77e18c65b2SHuajun Li struct f2fs_io_info fio = { 78e18c65b2SHuajun Li .type = DATA, 79e18c65b2SHuajun Li .rw = WRITE_SYNC | REQ_PRIO, 80e18c65b2SHuajun Li }; 81e18c65b2SHuajun Li 82e18c65b2SHuajun Li f2fs_lock_op(sbi); 83e18c65b2SHuajun Li ipage = get_node_page(sbi, inode->i_ino); 84e18c65b2SHuajun Li if (IS_ERR(ipage)) 85e18c65b2SHuajun Li return PTR_ERR(ipage); 86e18c65b2SHuajun Li 87e18c65b2SHuajun Li /* 88e18c65b2SHuajun Li * i_addr[0] is not used for inline data, 89e18c65b2SHuajun Li * so reserving new block will not destroy inline data 90e18c65b2SHuajun Li */ 91a8865372SJaegeuk Kim set_new_dnode(&dn, inode, ipage, NULL, 0); 92e18c65b2SHuajun Li err = f2fs_reserve_block(&dn, 0); 93e18c65b2SHuajun Li if (err) { 94e18c65b2SHuajun Li f2fs_unlock_op(sbi); 95e18c65b2SHuajun Li return err; 96e18c65b2SHuajun Li } 97e18c65b2SHuajun Li 9818309aaaSChao Yu zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE); 99e18c65b2SHuajun Li 100e18c65b2SHuajun Li /* Copy the whole inline data block */ 101e18c65b2SHuajun Li src_addr = inline_data_addr(ipage); 102e18c65b2SHuajun Li dst_addr = kmap(page); 103e18c65b2SHuajun Li memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 104e18c65b2SHuajun Li kunmap(page); 1059e09fc85SJaegeuk Kim SetPageUptodate(page); 106e18c65b2SHuajun Li 107e18c65b2SHuajun Li /* write data page to try to make data consistent */ 108e18c65b2SHuajun Li set_page_writeback(page); 109e18c65b2SHuajun Li write_data_page(page, &dn, &new_blk_addr, &fio); 110e18c65b2SHuajun Li update_extent_cache(new_blk_addr, &dn); 1115514f0aaSYuan Zhong f2fs_wait_on_page_writeback(page, DATA); 112e18c65b2SHuajun Li 113e18c65b2SHuajun Li /* clear inline data and flag after data writeback */ 114e18c65b2SHuajun Li zero_user_segment(ipage, INLINE_DATA_OFFSET, 115e18c65b2SHuajun Li INLINE_DATA_OFFSET + MAX_INLINE_DATA); 116e18c65b2SHuajun Li clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 1170dbdc2aeSJaegeuk Kim stat_dec_inline_inode(inode); 118e18c65b2SHuajun Li 119e18c65b2SHuajun Li sync_inode_page(&dn); 120a8865372SJaegeuk Kim f2fs_put_dnode(&dn); 121e18c65b2SHuajun Li f2fs_unlock_op(sbi); 122e18c65b2SHuajun Li return err; 123e18c65b2SHuajun Li } 124e18c65b2SHuajun Li 1259e09fc85SJaegeuk Kim int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size) 126e18c65b2SHuajun Li { 127e18c65b2SHuajun Li struct page *page; 1289e09fc85SJaegeuk Kim int err; 129e18c65b2SHuajun Li 1309e09fc85SJaegeuk Kim if (!f2fs_has_inline_data(inode)) 1319e09fc85SJaegeuk Kim return 0; 1329e09fc85SJaegeuk Kim else if (to_size <= MAX_INLINE_DATA) 1339e09fc85SJaegeuk Kim return 0; 1349e09fc85SJaegeuk Kim 1359e09fc85SJaegeuk Kim page = grab_cache_page_write_begin(inode->i_mapping, 0, AOP_FLAG_NOFS); 1369e09fc85SJaegeuk Kim if (!page) 1379e09fc85SJaegeuk Kim return -ENOMEM; 138e18c65b2SHuajun Li 139e18c65b2SHuajun Li err = __f2fs_convert_inline_data(inode, page); 140e18c65b2SHuajun Li f2fs_put_page(page, 1); 141e18c65b2SHuajun Li return err; 142e18c65b2SHuajun Li } 143e18c65b2SHuajun Li 144e18c65b2SHuajun Li int f2fs_write_inline_data(struct inode *inode, 145e18c65b2SHuajun Li struct page *page, unsigned size) 146e18c65b2SHuajun Li { 147e18c65b2SHuajun Li void *src_addr, *dst_addr; 148e18c65b2SHuajun Li struct page *ipage; 149e18c65b2SHuajun Li struct dnode_of_data dn; 150e18c65b2SHuajun Li int err; 151e18c65b2SHuajun Li 152e18c65b2SHuajun Li set_new_dnode(&dn, inode, NULL, NULL, 0); 153e18c65b2SHuajun Li err = get_dnode_of_data(&dn, 0, LOOKUP_NODE); 154e18c65b2SHuajun Li if (err) 155e18c65b2SHuajun Li return err; 156e18c65b2SHuajun Li ipage = dn.inode_page; 157e18c65b2SHuajun Li 158e18c65b2SHuajun Li zero_user_segment(ipage, INLINE_DATA_OFFSET, 159e18c65b2SHuajun Li INLINE_DATA_OFFSET + MAX_INLINE_DATA); 160e18c65b2SHuajun Li src_addr = kmap(page); 161e18c65b2SHuajun Li dst_addr = inline_data_addr(ipage); 162e18c65b2SHuajun Li memcpy(dst_addr, src_addr, size); 163e18c65b2SHuajun Li kunmap(page); 164e18c65b2SHuajun Li 165e18c65b2SHuajun Li /* Release the first data block if it is allocated */ 166e18c65b2SHuajun Li if (!f2fs_has_inline_data(inode)) { 167e18c65b2SHuajun Li truncate_data_blocks_range(&dn, 1); 168e18c65b2SHuajun Li set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 1690dbdc2aeSJaegeuk Kim stat_inc_inline_inode(inode); 170e18c65b2SHuajun Li } 171e18c65b2SHuajun Li 172e18c65b2SHuajun Li sync_inode_page(&dn); 173e18c65b2SHuajun Li f2fs_put_dnode(&dn); 174e18c65b2SHuajun Li 175e18c65b2SHuajun Li return 0; 176e18c65b2SHuajun Li } 1771e1bb4baSJaegeuk Kim 1781e1bb4baSJaegeuk Kim int recover_inline_data(struct inode *inode, struct page *npage) 1791e1bb4baSJaegeuk Kim { 1801e1bb4baSJaegeuk Kim struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); 1811e1bb4baSJaegeuk Kim struct f2fs_inode *ri = NULL; 1821e1bb4baSJaegeuk Kim void *src_addr, *dst_addr; 1831e1bb4baSJaegeuk Kim struct page *ipage; 1841e1bb4baSJaegeuk Kim 1851e1bb4baSJaegeuk Kim /* 1861e1bb4baSJaegeuk Kim * The inline_data recovery policy is as follows. 1871e1bb4baSJaegeuk Kim * [prev.] [next] of inline_data flag 1881e1bb4baSJaegeuk Kim * o o -> recover inline_data 1891e1bb4baSJaegeuk Kim * o x -> remove inline_data, and then recover data blocks 1901e1bb4baSJaegeuk Kim * x o -> remove inline_data, and then recover inline_data 1911e1bb4baSJaegeuk Kim * x x -> recover data blocks 1921e1bb4baSJaegeuk Kim */ 1931e1bb4baSJaegeuk Kim if (IS_INODE(npage)) 1941e1bb4baSJaegeuk Kim ri = F2FS_INODE(npage); 1951e1bb4baSJaegeuk Kim 1961e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode) && 1971e1bb4baSJaegeuk Kim ri && ri->i_inline & F2FS_INLINE_DATA) { 1981e1bb4baSJaegeuk Kim process_inline: 1991e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2001e1bb4baSJaegeuk Kim f2fs_bug_on(IS_ERR(ipage)); 2011e1bb4baSJaegeuk Kim 2021e1bb4baSJaegeuk Kim src_addr = inline_data_addr(npage); 2031e1bb4baSJaegeuk Kim dst_addr = inline_data_addr(ipage); 2041e1bb4baSJaegeuk Kim memcpy(dst_addr, src_addr, MAX_INLINE_DATA); 2051e1bb4baSJaegeuk Kim update_inode(inode, ipage); 2061e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2071e1bb4baSJaegeuk Kim return -1; 2081e1bb4baSJaegeuk Kim } 2091e1bb4baSJaegeuk Kim 2101e1bb4baSJaegeuk Kim if (f2fs_has_inline_data(inode)) { 2111e1bb4baSJaegeuk Kim ipage = get_node_page(sbi, inode->i_ino); 2121e1bb4baSJaegeuk Kim f2fs_bug_on(IS_ERR(ipage)); 2131e1bb4baSJaegeuk Kim zero_user_segment(ipage, INLINE_DATA_OFFSET, 2141e1bb4baSJaegeuk Kim INLINE_DATA_OFFSET + MAX_INLINE_DATA); 2151e1bb4baSJaegeuk Kim clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 2161e1bb4baSJaegeuk Kim update_inode(inode, ipage); 2171e1bb4baSJaegeuk Kim f2fs_put_page(ipage, 1); 2181e1bb4baSJaegeuk Kim } else if (ri && ri->i_inline & F2FS_INLINE_DATA) { 2191e1bb4baSJaegeuk Kim truncate_blocks(inode, 0); 2201e1bb4baSJaegeuk Kim set_inode_flag(F2FS_I(inode), FI_INLINE_DATA); 2211e1bb4baSJaegeuk Kim goto process_inline; 2221e1bb4baSJaegeuk Kim } 2231e1bb4baSJaegeuk Kim return 0; 2241e1bb4baSJaegeuk Kim } 225