extent_cache.c 19 KB

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