extent_cache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * f2fs extent cache support
  3. *
  4. * Copyright (c) 2015 Motorola Mobility
  5. * Copyright (c) 2015 Samsung Electronics
  6. * Authors: Jaegeuk Kim <jaegeuk@kernel.org>
  7. * Chao Yu <chao2.yu@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/fs.h>
  14. #include <linux/f2fs_fs.h>
  15. #include "f2fs.h"
  16. #include "node.h"
  17. #include <trace/events/f2fs.h>
  18. static struct kmem_cache *extent_tree_slab;
  19. static struct kmem_cache *extent_node_slab;
  20. static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
  21. struct extent_tree *et, struct extent_info *ei,
  22. struct rb_node *parent, struct rb_node **p)
  23. {
  24. struct extent_node *en;
  25. en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
  26. if (!en)
  27. return NULL;
  28. en->ei = *ei;
  29. INIT_LIST_HEAD(&en->list);
  30. en->et = et;
  31. rb_link_node(&en->rb_node, parent, p);
  32. rb_insert_color(&en->rb_node, &et->root);
  33. atomic_inc(&et->node_cnt);
  34. atomic_inc(&sbi->total_ext_node);
  35. return en;
  36. }
  37. static void __detach_extent_node(struct f2fs_sb_info *sbi,
  38. struct extent_tree *et, struct extent_node *en)
  39. {
  40. rb_erase(&en->rb_node, &et->root);
  41. atomic_dec(&et->node_cnt);
  42. atomic_dec(&sbi->total_ext_node);
  43. if (et->cached_en == en)
  44. et->cached_en = NULL;
  45. kmem_cache_free(extent_node_slab, en);
  46. }
  47. /*
  48. * Flow to release an extent_node:
  49. * 1. list_del_init
  50. * 2. __detach_extent_node
  51. * 3. kmem_cache_free.
  52. */
  53. static void __release_extent_node(struct f2fs_sb_info *sbi,
  54. struct extent_tree *et, struct extent_node *en)
  55. {
  56. spin_lock(&sbi->extent_lock);
  57. f2fs_bug_on(sbi, list_empty(&en->list));
  58. list_del_init(&en->list);
  59. spin_unlock(&sbi->extent_lock);
  60. __detach_extent_node(sbi, et, en);
  61. }
  62. static struct extent_tree *__grab_extent_tree(struct inode *inode)
  63. {
  64. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  65. struct extent_tree *et;
  66. nid_t ino = inode->i_ino;
  67. down_write(&sbi->extent_tree_lock);
  68. et = radix_tree_lookup(&sbi->extent_tree_root, ino);
  69. if (!et) {
  70. et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
  71. f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
  72. memset(et, 0, sizeof(struct extent_tree));
  73. et->ino = ino;
  74. et->root = RB_ROOT;
  75. et->cached_en = NULL;
  76. rwlock_init(&et->lock);
  77. INIT_LIST_HEAD(&et->list);
  78. atomic_set(&et->node_cnt, 0);
  79. atomic_inc(&sbi->total_ext_tree);
  80. } else {
  81. atomic_dec(&sbi->total_zombie_tree);
  82. list_del_init(&et->list);
  83. }
  84. up_write(&sbi->extent_tree_lock);
  85. /* never died until evict_inode */
  86. F2FS_I(inode)->extent_tree = et;
  87. return et;
  88. }
  89. static struct extent_node *__lookup_extent_tree(struct f2fs_sb_info *sbi,
  90. struct extent_tree *et, unsigned int fofs)
  91. {
  92. struct rb_node *node = et->root.rb_node;
  93. struct extent_node *en = et->cached_en;
  94. if (en) {
  95. struct extent_info *cei = &en->ei;
  96. if (cei->fofs <= fofs && cei->fofs + cei->len > fofs) {
  97. stat_inc_cached_node_hit(sbi);
  98. return en;
  99. }
  100. }
  101. while (node) {
  102. en = rb_entry(node, struct extent_node, rb_node);
  103. if (fofs < en->ei.fofs) {
  104. node = node->rb_left;
  105. } else if (fofs >= en->ei.fofs + en->ei.len) {
  106. node = node->rb_right;
  107. } else {
  108. stat_inc_rbtree_node_hit(sbi);
  109. return en;
  110. }
  111. }
  112. return NULL;
  113. }
  114. static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
  115. struct extent_tree *et, struct extent_info *ei)
  116. {
  117. struct rb_node **p = &et->root.rb_node;
  118. struct extent_node *en;
  119. en = __attach_extent_node(sbi, et, ei, NULL, p);
  120. if (!en)
  121. return NULL;
  122. et->largest = en->ei;
  123. et->cached_en = en;
  124. return en;
  125. }
  126. static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
  127. struct extent_tree *et)
  128. {
  129. struct rb_node *node, *next;
  130. struct extent_node *en;
  131. unsigned int count = atomic_read(&et->node_cnt);
  132. node = rb_first(&et->root);
  133. while (node) {
  134. next = rb_next(node);
  135. en = rb_entry(node, struct extent_node, rb_node);
  136. __release_extent_node(sbi, et, en);
  137. node = next;
  138. }
  139. return count - atomic_read(&et->node_cnt);
  140. }
  141. static void __drop_largest_extent(struct inode *inode,
  142. pgoff_t fofs, unsigned int len)
  143. {
  144. struct extent_info *largest = &F2FS_I(inode)->extent_tree->largest;
  145. if (fofs < largest->fofs + largest->len && fofs + len > largest->fofs) {
  146. largest->len = 0;
  147. f2fs_mark_inode_dirty_sync(inode);
  148. }
  149. }
  150. /* return true, if inode page is changed */
  151. static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
  152. {
  153. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  154. struct extent_tree *et;
  155. struct extent_node *en;
  156. struct extent_info ei;
  157. if (!f2fs_may_extent_tree(inode)) {
  158. /* drop largest extent */
  159. if (i_ext && i_ext->len) {
  160. i_ext->len = 0;
  161. return true;
  162. }
  163. return false;
  164. }
  165. et = __grab_extent_tree(inode);
  166. if (!i_ext || !i_ext->len)
  167. return false;
  168. get_extent_info(&ei, i_ext);
  169. write_lock(&et->lock);
  170. if (atomic_read(&et->node_cnt))
  171. goto out;
  172. en = __init_extent_tree(sbi, et, &ei);
  173. if (en) {
  174. spin_lock(&sbi->extent_lock);
  175. list_add_tail(&en->list, &sbi->extent_list);
  176. spin_unlock(&sbi->extent_lock);
  177. }
  178. out:
  179. write_unlock(&et->lock);
  180. return false;
  181. }
  182. bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
  183. {
  184. bool ret = __f2fs_init_extent_tree(inode, i_ext);
  185. if (!F2FS_I(inode)->extent_tree)
  186. set_inode_flag(inode, FI_NO_EXTENT);
  187. return ret;
  188. }
  189. static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
  190. struct extent_info *ei)
  191. {
  192. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  193. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  194. struct extent_node *en;
  195. bool ret = false;
  196. f2fs_bug_on(sbi, !et);
  197. trace_f2fs_lookup_extent_tree_start(inode, pgofs);
  198. read_lock(&et->lock);
  199. if (et->largest.fofs <= pgofs &&
  200. et->largest.fofs + et->largest.len > pgofs) {
  201. *ei = et->largest;
  202. ret = true;
  203. stat_inc_largest_node_hit(sbi);
  204. goto out;
  205. }
  206. en = __lookup_extent_tree(sbi, et, pgofs);
  207. if (en) {
  208. *ei = en->ei;
  209. spin_lock(&sbi->extent_lock);
  210. if (!list_empty(&en->list)) {
  211. list_move_tail(&en->list, &sbi->extent_list);
  212. et->cached_en = en;
  213. }
  214. spin_unlock(&sbi->extent_lock);
  215. ret = true;
  216. }
  217. out:
  218. stat_inc_total_hit(sbi);
  219. read_unlock(&et->lock);
  220. trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
  221. return ret;
  222. }
  223. /*
  224. * lookup extent at @fofs, if hit, return the extent
  225. * if not, return NULL and
  226. * @prev_ex: extent before fofs
  227. * @next_ex: extent after fofs
  228. * @insert_p: insert point for new extent at fofs
  229. * in order to simpfy the insertion after.
  230. * tree must stay unchanged between lookup and insertion.
  231. */
  232. static struct extent_node *__lookup_extent_tree_ret(struct extent_tree *et,
  233. unsigned int fofs,
  234. struct extent_node **prev_ex,
  235. struct extent_node **next_ex,
  236. struct rb_node ***insert_p,
  237. struct rb_node **insert_parent)
  238. {
  239. struct rb_node **pnode = &et->root.rb_node;
  240. struct rb_node *parent = NULL, *tmp_node;
  241. struct extent_node *en = et->cached_en;
  242. *insert_p = NULL;
  243. *insert_parent = NULL;
  244. *prev_ex = NULL;
  245. *next_ex = NULL;
  246. if (RB_EMPTY_ROOT(&et->root))
  247. return NULL;
  248. if (en) {
  249. struct extent_info *cei = &en->ei;
  250. if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
  251. goto lookup_neighbors;
  252. }
  253. while (*pnode) {
  254. parent = *pnode;
  255. en = rb_entry(*pnode, struct extent_node, rb_node);
  256. if (fofs < en->ei.fofs)
  257. pnode = &(*pnode)->rb_left;
  258. else if (fofs >= en->ei.fofs + en->ei.len)
  259. pnode = &(*pnode)->rb_right;
  260. else
  261. goto lookup_neighbors;
  262. }
  263. *insert_p = pnode;
  264. *insert_parent = parent;
  265. en = rb_entry(parent, struct extent_node, rb_node);
  266. tmp_node = parent;
  267. if (parent && fofs > en->ei.fofs)
  268. tmp_node = rb_next(parent);
  269. *next_ex = tmp_node ?
  270. rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
  271. tmp_node = parent;
  272. if (parent && fofs < en->ei.fofs)
  273. tmp_node = rb_prev(parent);
  274. *prev_ex = tmp_node ?
  275. rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
  276. return NULL;
  277. lookup_neighbors:
  278. if (fofs == en->ei.fofs) {
  279. /* lookup prev node for merging backward later */
  280. tmp_node = rb_prev(&en->rb_node);
  281. *prev_ex = tmp_node ?
  282. rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
  283. }
  284. if (fofs == en->ei.fofs + en->ei.len - 1) {
  285. /* lookup next node for merging frontward later */
  286. tmp_node = rb_next(&en->rb_node);
  287. *next_ex = tmp_node ?
  288. rb_entry(tmp_node, struct extent_node, rb_node) : NULL;
  289. }
  290. return en;
  291. }
  292. static struct extent_node *__try_merge_extent_node(struct inode *inode,
  293. struct extent_tree *et, struct extent_info *ei,
  294. struct extent_node *prev_ex,
  295. struct extent_node *next_ex)
  296. {
  297. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  298. struct extent_node *en = NULL;
  299. if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
  300. prev_ex->ei.len += ei->len;
  301. ei = &prev_ex->ei;
  302. en = prev_ex;
  303. }
  304. if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
  305. next_ex->ei.fofs = ei->fofs;
  306. next_ex->ei.blk = ei->blk;
  307. next_ex->ei.len += ei->len;
  308. if (en)
  309. __release_extent_node(sbi, et, prev_ex);
  310. en = next_ex;
  311. }
  312. if (!en)
  313. return NULL;
  314. __try_update_largest_extent(inode, et, en);
  315. spin_lock(&sbi->extent_lock);
  316. if (!list_empty(&en->list)) {
  317. list_move_tail(&en->list, &sbi->extent_list);
  318. et->cached_en = en;
  319. }
  320. spin_unlock(&sbi->extent_lock);
  321. return en;
  322. }
  323. static struct extent_node *__insert_extent_tree(struct inode *inode,
  324. struct extent_tree *et, struct extent_info *ei,
  325. struct rb_node **insert_p,
  326. struct rb_node *insert_parent)
  327. {
  328. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  329. struct rb_node **p = &et->root.rb_node;
  330. struct rb_node *parent = NULL;
  331. struct extent_node *en = NULL;
  332. if (insert_p && insert_parent) {
  333. parent = insert_parent;
  334. p = insert_p;
  335. goto do_insert;
  336. }
  337. while (*p) {
  338. parent = *p;
  339. en = rb_entry(parent, struct extent_node, rb_node);
  340. if (ei->fofs < en->ei.fofs)
  341. p = &(*p)->rb_left;
  342. else if (ei->fofs >= en->ei.fofs + en->ei.len)
  343. p = &(*p)->rb_right;
  344. else
  345. f2fs_bug_on(sbi, 1);
  346. }
  347. do_insert:
  348. en = __attach_extent_node(sbi, et, ei, parent, p);
  349. if (!en)
  350. return NULL;
  351. __try_update_largest_extent(inode, et, en);
  352. /* update in global extent list */
  353. spin_lock(&sbi->extent_lock);
  354. list_add_tail(&en->list, &sbi->extent_list);
  355. et->cached_en = en;
  356. spin_unlock(&sbi->extent_lock);
  357. return en;
  358. }
  359. static unsigned int f2fs_update_extent_tree_range(struct inode *inode,
  360. pgoff_t fofs, block_t blkaddr, unsigned int len)
  361. {
  362. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  363. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  364. struct extent_node *en = NULL, *en1 = NULL;
  365. struct extent_node *prev_en = NULL, *next_en = NULL;
  366. struct extent_info ei, dei, prev;
  367. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  368. unsigned int end = fofs + len;
  369. unsigned int pos = (unsigned int)fofs;
  370. if (!et)
  371. return false;
  372. trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
  373. write_lock(&et->lock);
  374. if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
  375. write_unlock(&et->lock);
  376. return false;
  377. }
  378. prev = et->largest;
  379. dei.len = 0;
  380. /*
  381. * drop largest extent before lookup, in case it's already
  382. * been shrunk from extent tree
  383. */
  384. __drop_largest_extent(inode, fofs, len);
  385. /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
  386. en = __lookup_extent_tree_ret(et, fofs, &prev_en, &next_en,
  387. &insert_p, &insert_parent);
  388. if (!en)
  389. en = next_en;
  390. /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
  391. while (en && en->ei.fofs < end) {
  392. unsigned int org_end;
  393. int parts = 0; /* # of parts current extent split into */
  394. next_en = en1 = NULL;
  395. dei = en->ei;
  396. org_end = dei.fofs + dei.len;
  397. f2fs_bug_on(sbi, pos >= org_end);
  398. if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
  399. en->ei.len = pos - en->ei.fofs;
  400. prev_en = en;
  401. parts = 1;
  402. }
  403. if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
  404. if (parts) {
  405. set_extent_info(&ei, end,
  406. end - dei.fofs + dei.blk,
  407. org_end - end);
  408. en1 = __insert_extent_tree(inode, et, &ei,
  409. NULL, NULL);
  410. next_en = en1;
  411. } else {
  412. en->ei.fofs = end;
  413. en->ei.blk += end - dei.fofs;
  414. en->ei.len -= end - dei.fofs;
  415. next_en = en;
  416. }
  417. parts++;
  418. }
  419. if (!next_en) {
  420. struct rb_node *node = rb_next(&en->rb_node);
  421. next_en = node ?
  422. rb_entry(node, struct extent_node, rb_node)
  423. : NULL;
  424. }
  425. if (parts)
  426. __try_update_largest_extent(inode, et, en);
  427. else
  428. __release_extent_node(sbi, et, en);
  429. /*
  430. * if original extent is split into zero or two parts, extent
  431. * tree has been altered by deletion or insertion, therefore
  432. * invalidate pointers regard to tree.
  433. */
  434. if (parts != 1) {
  435. insert_p = NULL;
  436. insert_parent = NULL;
  437. }
  438. en = next_en;
  439. }
  440. /* 3. update extent in extent cache */
  441. if (blkaddr) {
  442. set_extent_info(&ei, fofs, blkaddr, len);
  443. if (!__try_merge_extent_node(inode, et, &ei, prev_en, next_en))
  444. __insert_extent_tree(inode, et, &ei,
  445. insert_p, insert_parent);
  446. /* give up extent_cache, if split and small updates happen */
  447. if (dei.len >= 1 &&
  448. prev.len < F2FS_MIN_EXTENT_LEN &&
  449. et->largest.len < F2FS_MIN_EXTENT_LEN) {
  450. __drop_largest_extent(inode, 0, UINT_MAX);
  451. set_inode_flag(inode, FI_NO_EXTENT);
  452. }
  453. }
  454. if (is_inode_flag_set(inode, FI_NO_EXTENT))
  455. __free_extent_tree(sbi, et);
  456. write_unlock(&et->lock);
  457. return !__is_extent_same(&prev, &et->largest);
  458. }
  459. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
  460. {
  461. struct extent_tree *et, *next;
  462. struct extent_node *en;
  463. unsigned int node_cnt = 0, tree_cnt = 0;
  464. int remained;
  465. if (!test_opt(sbi, EXTENT_CACHE))
  466. return 0;
  467. if (!atomic_read(&sbi->total_zombie_tree))
  468. goto free_node;
  469. if (!down_write_trylock(&sbi->extent_tree_lock))
  470. goto out;
  471. /* 1. remove unreferenced extent tree */
  472. list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
  473. if (atomic_read(&et->node_cnt)) {
  474. write_lock(&et->lock);
  475. node_cnt += __free_extent_tree(sbi, et);
  476. write_unlock(&et->lock);
  477. }
  478. f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
  479. list_del_init(&et->list);
  480. radix_tree_delete(&sbi->extent_tree_root, et->ino);
  481. kmem_cache_free(extent_tree_slab, et);
  482. atomic_dec(&sbi->total_ext_tree);
  483. atomic_dec(&sbi->total_zombie_tree);
  484. tree_cnt++;
  485. if (node_cnt + tree_cnt >= nr_shrink)
  486. goto unlock_out;
  487. cond_resched();
  488. }
  489. up_write(&sbi->extent_tree_lock);
  490. free_node:
  491. /* 2. remove LRU extent entries */
  492. if (!down_write_trylock(&sbi->extent_tree_lock))
  493. goto out;
  494. remained = nr_shrink - (node_cnt + tree_cnt);
  495. spin_lock(&sbi->extent_lock);
  496. for (; remained > 0; remained--) {
  497. if (list_empty(&sbi->extent_list))
  498. break;
  499. en = list_first_entry(&sbi->extent_list,
  500. struct extent_node, list);
  501. et = en->et;
  502. if (!write_trylock(&et->lock)) {
  503. /* refresh this extent node's position in extent list */
  504. list_move_tail(&en->list, &sbi->extent_list);
  505. continue;
  506. }
  507. list_del_init(&en->list);
  508. spin_unlock(&sbi->extent_lock);
  509. __detach_extent_node(sbi, et, en);
  510. write_unlock(&et->lock);
  511. node_cnt++;
  512. spin_lock(&sbi->extent_lock);
  513. }
  514. spin_unlock(&sbi->extent_lock);
  515. unlock_out:
  516. up_write(&sbi->extent_tree_lock);
  517. out:
  518. trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
  519. return node_cnt + tree_cnt;
  520. }
  521. unsigned int f2fs_destroy_extent_node(struct inode *inode)
  522. {
  523. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  524. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  525. unsigned int node_cnt = 0;
  526. if (!et || !atomic_read(&et->node_cnt))
  527. return 0;
  528. write_lock(&et->lock);
  529. node_cnt = __free_extent_tree(sbi, et);
  530. write_unlock(&et->lock);
  531. return node_cnt;
  532. }
  533. void f2fs_drop_extent_tree(struct inode *inode)
  534. {
  535. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  536. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  537. if (!f2fs_may_extent_tree(inode))
  538. return;
  539. set_inode_flag(inode, FI_NO_EXTENT);
  540. write_lock(&et->lock);
  541. __free_extent_tree(sbi, et);
  542. __drop_largest_extent(inode, 0, UINT_MAX);
  543. write_unlock(&et->lock);
  544. }
  545. void f2fs_destroy_extent_tree(struct inode *inode)
  546. {
  547. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  548. struct extent_tree *et = F2FS_I(inode)->extent_tree;
  549. unsigned int node_cnt = 0;
  550. if (!et)
  551. return;
  552. if (inode->i_nlink && !is_bad_inode(inode) &&
  553. atomic_read(&et->node_cnt)) {
  554. down_write(&sbi->extent_tree_lock);
  555. list_add_tail(&et->list, &sbi->zombie_list);
  556. atomic_inc(&sbi->total_zombie_tree);
  557. up_write(&sbi->extent_tree_lock);
  558. return;
  559. }
  560. /* free all extent info belong to this extent tree */
  561. node_cnt = f2fs_destroy_extent_node(inode);
  562. /* delete extent tree entry in radix tree */
  563. down_write(&sbi->extent_tree_lock);
  564. f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
  565. radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
  566. kmem_cache_free(extent_tree_slab, et);
  567. atomic_dec(&sbi->total_ext_tree);
  568. up_write(&sbi->extent_tree_lock);
  569. F2FS_I(inode)->extent_tree = NULL;
  570. trace_f2fs_destroy_extent_tree(inode, node_cnt);
  571. }
  572. bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
  573. struct extent_info *ei)
  574. {
  575. if (!f2fs_may_extent_tree(inode))
  576. return false;
  577. return f2fs_lookup_extent_tree(inode, pgofs, ei);
  578. }
  579. void f2fs_update_extent_cache(struct dnode_of_data *dn)
  580. {
  581. pgoff_t fofs;
  582. block_t blkaddr;
  583. if (!f2fs_may_extent_tree(dn->inode))
  584. return;
  585. if (dn->data_blkaddr == NEW_ADDR)
  586. blkaddr = NULL_ADDR;
  587. else
  588. blkaddr = dn->data_blkaddr;
  589. fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
  590. dn->ofs_in_node;
  591. f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
  592. }
  593. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  594. pgoff_t fofs, block_t blkaddr, unsigned int len)
  595. {
  596. if (!f2fs_may_extent_tree(dn->inode))
  597. return;
  598. f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
  599. }
  600. void init_extent_cache_info(struct f2fs_sb_info *sbi)
  601. {
  602. INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
  603. init_rwsem(&sbi->extent_tree_lock);
  604. INIT_LIST_HEAD(&sbi->extent_list);
  605. spin_lock_init(&sbi->extent_lock);
  606. atomic_set(&sbi->total_ext_tree, 0);
  607. INIT_LIST_HEAD(&sbi->zombie_list);
  608. atomic_set(&sbi->total_zombie_tree, 0);
  609. atomic_set(&sbi->total_ext_node, 0);
  610. }
  611. int __init create_extent_cache(void)
  612. {
  613. extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
  614. sizeof(struct extent_tree));
  615. if (!extent_tree_slab)
  616. return -ENOMEM;
  617. extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
  618. sizeof(struct extent_node));
  619. if (!extent_node_slab) {
  620. kmem_cache_destroy(extent_tree_slab);
  621. return -ENOMEM;
  622. }
  623. return 0;
  624. }
  625. void destroy_extent_cache(void)
  626. {
  627. kmem_cache_destroy(extent_node_slab);
  628. kmem_cache_destroy(extent_tree_slab);
  629. }