inline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * fs/f2fs/inline.c
  3. * Copyright (c) 2013, Intel Corporation
  4. * Authors: Huajun Li <huajun.li@intel.com>
  5. * Haicheng Li <haicheng.li@intel.com>
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/f2fs_fs.h>
  12. #include "f2fs.h"
  13. bool f2fs_may_inline(struct inode *inode)
  14. {
  15. if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
  16. return false;
  17. if (f2fs_is_atomic_file(inode))
  18. return false;
  19. if (!S_ISREG(inode->i_mode))
  20. return false;
  21. if (i_size_read(inode) > MAX_INLINE_DATA)
  22. return false;
  23. return true;
  24. }
  25. void read_inline_data(struct page *page, struct page *ipage)
  26. {
  27. void *src_addr, *dst_addr;
  28. if (PageUptodate(page))
  29. return;
  30. f2fs_bug_on(F2FS_P_SB(page), page->index);
  31. zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
  32. /* Copy the whole inline data block */
  33. src_addr = inline_data_addr(ipage);
  34. dst_addr = kmap_atomic(page);
  35. memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
  36. flush_dcache_page(page);
  37. kunmap_atomic(dst_addr);
  38. SetPageUptodate(page);
  39. }
  40. static void truncate_inline_data(struct page *ipage)
  41. {
  42. f2fs_wait_on_page_writeback(ipage, NODE);
  43. memset(inline_data_addr(ipage), 0, MAX_INLINE_DATA);
  44. }
  45. int f2fs_read_inline_data(struct inode *inode, struct page *page)
  46. {
  47. struct page *ipage;
  48. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  49. if (IS_ERR(ipage)) {
  50. unlock_page(page);
  51. return PTR_ERR(ipage);
  52. }
  53. if (!f2fs_has_inline_data(inode)) {
  54. f2fs_put_page(ipage, 1);
  55. return -EAGAIN;
  56. }
  57. if (page->index)
  58. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  59. else
  60. read_inline_data(page, ipage);
  61. SetPageUptodate(page);
  62. f2fs_put_page(ipage, 1);
  63. unlock_page(page);
  64. return 0;
  65. }
  66. int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
  67. {
  68. void *src_addr, *dst_addr;
  69. struct f2fs_io_info fio = {
  70. .type = DATA,
  71. .rw = WRITE_SYNC | REQ_PRIO,
  72. };
  73. int dirty, err;
  74. f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
  75. if (!f2fs_exist_data(dn->inode))
  76. goto clear_out;
  77. err = f2fs_reserve_block(dn, 0);
  78. if (err)
  79. return err;
  80. f2fs_wait_on_page_writeback(page, DATA);
  81. if (PageUptodate(page))
  82. goto no_update;
  83. zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
  84. /* Copy the whole inline data block */
  85. src_addr = inline_data_addr(dn->inode_page);
  86. dst_addr = kmap_atomic(page);
  87. memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
  88. flush_dcache_page(page);
  89. kunmap_atomic(dst_addr);
  90. SetPageUptodate(page);
  91. no_update:
  92. /* clear dirty state */
  93. dirty = clear_page_dirty_for_io(page);
  94. /* write data page to try to make data consistent */
  95. set_page_writeback(page);
  96. fio.blk_addr = dn->data_blkaddr;
  97. write_data_page(page, dn, &fio);
  98. update_extent_cache(dn);
  99. f2fs_wait_on_page_writeback(page, DATA);
  100. if (dirty)
  101. inode_dec_dirty_pages(dn->inode);
  102. /* this converted inline_data should be recovered. */
  103. set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
  104. /* clear inline data and flag after data writeback */
  105. truncate_inline_data(dn->inode_page);
  106. clear_out:
  107. stat_dec_inline_inode(dn->inode);
  108. f2fs_clear_inline_inode(dn->inode);
  109. sync_inode_page(dn);
  110. f2fs_put_dnode(dn);
  111. return 0;
  112. }
  113. int f2fs_convert_inline_inode(struct inode *inode)
  114. {
  115. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  116. struct dnode_of_data dn;
  117. struct page *ipage, *page;
  118. int err = 0;
  119. page = grab_cache_page(inode->i_mapping, 0);
  120. if (!page)
  121. return -ENOMEM;
  122. f2fs_lock_op(sbi);
  123. ipage = get_node_page(sbi, inode->i_ino);
  124. if (IS_ERR(ipage)) {
  125. err = PTR_ERR(ipage);
  126. goto out;
  127. }
  128. set_new_dnode(&dn, inode, ipage, ipage, 0);
  129. if (f2fs_has_inline_data(inode))
  130. err = f2fs_convert_inline_page(&dn, page);
  131. f2fs_put_dnode(&dn);
  132. out:
  133. f2fs_unlock_op(sbi);
  134. f2fs_put_page(page, 1);
  135. return err;
  136. }
  137. int f2fs_write_inline_data(struct inode *inode, struct page *page)
  138. {
  139. void *src_addr, *dst_addr;
  140. struct dnode_of_data dn;
  141. int err;
  142. set_new_dnode(&dn, inode, NULL, NULL, 0);
  143. err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
  144. if (err)
  145. return err;
  146. if (!f2fs_has_inline_data(inode)) {
  147. f2fs_put_dnode(&dn);
  148. return -EAGAIN;
  149. }
  150. f2fs_bug_on(F2FS_I_SB(inode), page->index);
  151. f2fs_wait_on_page_writeback(dn.inode_page, NODE);
  152. src_addr = kmap_atomic(page);
  153. dst_addr = inline_data_addr(dn.inode_page);
  154. memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
  155. kunmap_atomic(src_addr);
  156. set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
  157. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  158. sync_inode_page(&dn);
  159. f2fs_put_dnode(&dn);
  160. return 0;
  161. }
  162. bool recover_inline_data(struct inode *inode, struct page *npage)
  163. {
  164. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  165. struct f2fs_inode *ri = NULL;
  166. void *src_addr, *dst_addr;
  167. struct page *ipage;
  168. /*
  169. * The inline_data recovery policy is as follows.
  170. * [prev.] [next] of inline_data flag
  171. * o o -> recover inline_data
  172. * o x -> remove inline_data, and then recover data blocks
  173. * x o -> remove inline_data, and then recover inline_data
  174. * x x -> recover data blocks
  175. */
  176. if (IS_INODE(npage))
  177. ri = F2FS_INODE(npage);
  178. if (f2fs_has_inline_data(inode) &&
  179. ri && (ri->i_inline & F2FS_INLINE_DATA)) {
  180. process_inline:
  181. ipage = get_node_page(sbi, inode->i_ino);
  182. f2fs_bug_on(sbi, IS_ERR(ipage));
  183. f2fs_wait_on_page_writeback(ipage, NODE);
  184. src_addr = inline_data_addr(npage);
  185. dst_addr = inline_data_addr(ipage);
  186. memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
  187. set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
  188. set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  189. update_inode(inode, ipage);
  190. f2fs_put_page(ipage, 1);
  191. return true;
  192. }
  193. if (f2fs_has_inline_data(inode)) {
  194. ipage = get_node_page(sbi, inode->i_ino);
  195. f2fs_bug_on(sbi, IS_ERR(ipage));
  196. truncate_inline_data(ipage);
  197. f2fs_clear_inline_inode(inode);
  198. update_inode(inode, ipage);
  199. f2fs_put_page(ipage, 1);
  200. } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
  201. truncate_blocks(inode, 0, false);
  202. goto process_inline;
  203. }
  204. return false;
  205. }
  206. struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
  207. struct qstr *name, struct page **res_page)
  208. {
  209. struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
  210. struct f2fs_inline_dentry *inline_dentry;
  211. struct f2fs_dir_entry *de;
  212. struct f2fs_dentry_ptr d;
  213. struct page *ipage;
  214. ipage = get_node_page(sbi, dir->i_ino);
  215. if (IS_ERR(ipage))
  216. return NULL;
  217. inline_dentry = inline_data_addr(ipage);
  218. make_dentry_ptr(&d, (void *)inline_dentry, 2);
  219. de = find_target_dentry(name, NULL, &d);
  220. unlock_page(ipage);
  221. if (de)
  222. *res_page = ipage;
  223. else
  224. f2fs_put_page(ipage, 0);
  225. /*
  226. * For the most part, it should be a bug when name_len is zero.
  227. * We stop here for figuring out where the bugs has occurred.
  228. */
  229. f2fs_bug_on(sbi, d.max < 0);
  230. return de;
  231. }
  232. struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
  233. struct page **p)
  234. {
  235. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  236. struct page *ipage;
  237. struct f2fs_dir_entry *de;
  238. struct f2fs_inline_dentry *dentry_blk;
  239. ipage = get_node_page(sbi, dir->i_ino);
  240. if (IS_ERR(ipage))
  241. return NULL;
  242. dentry_blk = inline_data_addr(ipage);
  243. de = &dentry_blk->dentry[1];
  244. *p = ipage;
  245. unlock_page(ipage);
  246. return de;
  247. }
  248. int make_empty_inline_dir(struct inode *inode, struct inode *parent,
  249. struct page *ipage)
  250. {
  251. struct f2fs_inline_dentry *dentry_blk;
  252. struct f2fs_dentry_ptr d;
  253. dentry_blk = inline_data_addr(ipage);
  254. make_dentry_ptr(&d, (void *)dentry_blk, 2);
  255. do_make_empty_dir(inode, parent, &d);
  256. set_page_dirty(ipage);
  257. /* update i_size to MAX_INLINE_DATA */
  258. if (i_size_read(inode) < MAX_INLINE_DATA) {
  259. i_size_write(inode, MAX_INLINE_DATA);
  260. set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
  261. }
  262. return 0;
  263. }
  264. static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
  265. struct f2fs_inline_dentry *inline_dentry)
  266. {
  267. struct page *page;
  268. struct dnode_of_data dn;
  269. struct f2fs_dentry_block *dentry_blk;
  270. int err;
  271. page = grab_cache_page(dir->i_mapping, 0);
  272. if (!page)
  273. return -ENOMEM;
  274. set_new_dnode(&dn, dir, ipage, NULL, 0);
  275. err = f2fs_reserve_block(&dn, 0);
  276. if (err)
  277. goto out;
  278. f2fs_wait_on_page_writeback(page, DATA);
  279. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  280. dentry_blk = kmap_atomic(page);
  281. /* copy data from inline dentry block to new dentry block */
  282. memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
  283. INLINE_DENTRY_BITMAP_SIZE);
  284. memcpy(dentry_blk->dentry, inline_dentry->dentry,
  285. sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
  286. memcpy(dentry_blk->filename, inline_dentry->filename,
  287. NR_INLINE_DENTRY * F2FS_SLOT_LEN);
  288. kunmap_atomic(dentry_blk);
  289. SetPageUptodate(page);
  290. set_page_dirty(page);
  291. /* clear inline dir and flag after data writeback */
  292. truncate_inline_data(ipage);
  293. stat_dec_inline_dir(dir);
  294. clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
  295. if (i_size_read(dir) < PAGE_CACHE_SIZE) {
  296. i_size_write(dir, PAGE_CACHE_SIZE);
  297. set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
  298. }
  299. sync_inode_page(&dn);
  300. out:
  301. f2fs_put_page(page, 1);
  302. return err;
  303. }
  304. int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
  305. struct inode *inode)
  306. {
  307. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  308. struct page *ipage;
  309. unsigned int bit_pos;
  310. f2fs_hash_t name_hash;
  311. struct f2fs_dir_entry *de;
  312. size_t namelen = name->len;
  313. struct f2fs_inline_dentry *dentry_blk = NULL;
  314. int slots = GET_DENTRY_SLOTS(namelen);
  315. struct page *page;
  316. int err = 0;
  317. int i;
  318. name_hash = f2fs_dentry_hash(name);
  319. ipage = get_node_page(sbi, dir->i_ino);
  320. if (IS_ERR(ipage))
  321. return PTR_ERR(ipage);
  322. dentry_blk = inline_data_addr(ipage);
  323. bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
  324. slots, NR_INLINE_DENTRY);
  325. if (bit_pos >= NR_INLINE_DENTRY) {
  326. err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
  327. if (!err)
  328. err = -EAGAIN;
  329. goto out;
  330. }
  331. down_write(&F2FS_I(inode)->i_sem);
  332. page = init_inode_metadata(inode, dir, name, ipage);
  333. if (IS_ERR(page)) {
  334. err = PTR_ERR(page);
  335. goto fail;
  336. }
  337. f2fs_wait_on_page_writeback(ipage, NODE);
  338. de = &dentry_blk->dentry[bit_pos];
  339. de->hash_code = name_hash;
  340. de->name_len = cpu_to_le16(namelen);
  341. memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
  342. de->ino = cpu_to_le32(inode->i_ino);
  343. set_de_type(de, inode);
  344. for (i = 0; i < slots; i++)
  345. test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
  346. set_page_dirty(ipage);
  347. /* we don't need to mark_inode_dirty now */
  348. F2FS_I(inode)->i_pino = dir->i_ino;
  349. update_inode(inode, page);
  350. f2fs_put_page(page, 1);
  351. update_parent_metadata(dir, inode, 0);
  352. fail:
  353. up_write(&F2FS_I(inode)->i_sem);
  354. if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
  355. update_inode(dir, ipage);
  356. clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
  357. }
  358. out:
  359. f2fs_put_page(ipage, 1);
  360. return err;
  361. }
  362. void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
  363. struct inode *dir, struct inode *inode)
  364. {
  365. struct f2fs_inline_dentry *inline_dentry;
  366. int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
  367. unsigned int bit_pos;
  368. int i;
  369. lock_page(page);
  370. f2fs_wait_on_page_writeback(page, NODE);
  371. inline_dentry = inline_data_addr(page);
  372. bit_pos = dentry - inline_dentry->dentry;
  373. for (i = 0; i < slots; i++)
  374. test_and_clear_bit_le(bit_pos + i,
  375. &inline_dentry->dentry_bitmap);
  376. set_page_dirty(page);
  377. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  378. if (inode)
  379. f2fs_drop_nlink(dir, inode, page);
  380. f2fs_put_page(page, 1);
  381. }
  382. bool f2fs_empty_inline_dir(struct inode *dir)
  383. {
  384. struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
  385. struct page *ipage;
  386. unsigned int bit_pos = 2;
  387. struct f2fs_inline_dentry *dentry_blk;
  388. ipage = get_node_page(sbi, dir->i_ino);
  389. if (IS_ERR(ipage))
  390. return false;
  391. dentry_blk = inline_data_addr(ipage);
  392. bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
  393. NR_INLINE_DENTRY,
  394. bit_pos);
  395. f2fs_put_page(ipage, 1);
  396. if (bit_pos < NR_INLINE_DENTRY)
  397. return false;
  398. return true;
  399. }
  400. int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
  401. {
  402. struct inode *inode = file_inode(file);
  403. struct f2fs_inline_dentry *inline_dentry = NULL;
  404. struct page *ipage = NULL;
  405. struct f2fs_dentry_ptr d;
  406. if (ctx->pos == NR_INLINE_DENTRY)
  407. return 0;
  408. ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
  409. if (IS_ERR(ipage))
  410. return PTR_ERR(ipage);
  411. inline_dentry = inline_data_addr(ipage);
  412. make_dentry_ptr(&d, (void *)inline_dentry, 2);
  413. if (!f2fs_fill_dentries(ctx, &d, 0))
  414. ctx->pos = NR_INLINE_DENTRY;
  415. f2fs_put_page(ipage, 1);
  416. return 0;
  417. }