inline.c 13 KB

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