extent_cache.c 18 KB

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