extents_status.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * fs/ext4/extents_status.c
  3. *
  4. * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
  5. * Modified by
  6. * Allison Henderson <achender@linux.vnet.ibm.com>
  7. * Hugh Dickins <hughd@google.com>
  8. * Zheng Liu <wenqing.lz@taobao.com>
  9. *
  10. * Ext4 extents status tree core functions.
  11. */
  12. #include <linux/list_sort.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/seq_file.h>
  15. #include "ext4.h"
  16. #include <trace/events/ext4.h>
  17. /*
  18. * According to previous discussion in Ext4 Developer Workshop, we
  19. * will introduce a new structure called io tree to track all extent
  20. * status in order to solve some problems that we have met
  21. * (e.g. Reservation space warning), and provide extent-level locking.
  22. * Delay extent tree is the first step to achieve this goal. It is
  23. * original built by Yongqiang Yang. At that time it is called delay
  24. * extent tree, whose goal is only track delayed extents in memory to
  25. * simplify the implementation of fiemap and bigalloc, and introduce
  26. * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
  27. * delay extent tree at the first commit. But for better understand
  28. * what it does, it has been rename to extent status tree.
  29. *
  30. * Step1:
  31. * Currently the first step has been done. All delayed extents are
  32. * tracked in the tree. It maintains the delayed extent when a delayed
  33. * allocation is issued, and the delayed extent is written out or
  34. * invalidated. Therefore the implementation of fiemap and bigalloc
  35. * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
  36. *
  37. * The following comment describes the implemenmtation of extent
  38. * status tree and future works.
  39. *
  40. * Step2:
  41. * In this step all extent status are tracked by extent status tree.
  42. * Thus, we can first try to lookup a block mapping in this tree before
  43. * finding it in extent tree. Hence, single extent cache can be removed
  44. * because extent status tree can do a better job. Extents in status
  45. * tree are loaded on-demand. Therefore, the extent status tree may not
  46. * contain all of the extents in a file. Meanwhile we define a shrinker
  47. * to reclaim memory from extent status tree because fragmented extent
  48. * tree will make status tree cost too much memory. written/unwritten/-
  49. * hole extents in the tree will be reclaimed by this shrinker when we
  50. * are under high memory pressure. Delayed extents will not be
  51. * reclimed because fiemap, bigalloc, and seek_data/hole need it.
  52. */
  53. /*
  54. * Extent status tree implementation for ext4.
  55. *
  56. *
  57. * ==========================================================================
  58. * Extent status tree tracks all extent status.
  59. *
  60. * 1. Why we need to implement extent status tree?
  61. *
  62. * Without extent status tree, ext4 identifies a delayed extent by looking
  63. * up page cache, this has several deficiencies - complicated, buggy,
  64. * and inefficient code.
  65. *
  66. * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
  67. * block or a range of blocks are belonged to a delayed extent.
  68. *
  69. * Let us have a look at how they do without extent status tree.
  70. * -- FIEMAP
  71. * FIEMAP looks up page cache to identify delayed allocations from holes.
  72. *
  73. * -- SEEK_HOLE/DATA
  74. * SEEK_HOLE/DATA has the same problem as FIEMAP.
  75. *
  76. * -- bigalloc
  77. * bigalloc looks up page cache to figure out if a block is
  78. * already under delayed allocation or not to determine whether
  79. * quota reserving is needed for the cluster.
  80. *
  81. * -- writeout
  82. * Writeout looks up whole page cache to see if a buffer is
  83. * mapped, If there are not very many delayed buffers, then it is
  84. * time comsuming.
  85. *
  86. * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
  87. * bigalloc and writeout can figure out if a block or a range of
  88. * blocks is under delayed allocation(belonged to a delayed extent) or
  89. * not by searching the extent tree.
  90. *
  91. *
  92. * ==========================================================================
  93. * 2. Ext4 extent status tree impelmentation
  94. *
  95. * -- extent
  96. * A extent is a range of blocks which are contiguous logically and
  97. * physically. Unlike extent in extent tree, this extent in ext4 is
  98. * a in-memory struct, there is no corresponding on-disk data. There
  99. * is no limit on length of extent, so an extent can contain as many
  100. * blocks as they are contiguous logically and physically.
  101. *
  102. * -- extent status tree
  103. * Every inode has an extent status tree and all allocation blocks
  104. * are added to the tree with different status. The extent in the
  105. * tree are ordered by logical block no.
  106. *
  107. * -- operations on a extent status tree
  108. * There are three important operations on a delayed extent tree: find
  109. * next extent, adding a extent(a range of blocks) and removing a extent.
  110. *
  111. * -- race on a extent status tree
  112. * Extent status tree is protected by inode->i_es_lock.
  113. *
  114. * -- memory consumption
  115. * Fragmented extent tree will make extent status tree cost too much
  116. * memory. Hence, we will reclaim written/unwritten/hole extents from
  117. * the tree under a heavy memory pressure.
  118. *
  119. *
  120. * ==========================================================================
  121. * 3. Performance analysis
  122. *
  123. * -- overhead
  124. * 1. There is a cache extent for write access, so if writes are
  125. * not very random, adding space operaions are in O(1) time.
  126. *
  127. * -- gain
  128. * 2. Code is much simpler, more readable, more maintainable and
  129. * more efficient.
  130. *
  131. *
  132. * ==========================================================================
  133. * 4. TODO list
  134. *
  135. * -- Refactor delayed space reservation
  136. *
  137. * -- Extent-level locking
  138. */
  139. static struct kmem_cache *ext4_es_cachep;
  140. static int __es_insert_extent(struct inode *inode, struct extent_status *newes);
  141. static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  142. ext4_lblk_t end);
  143. static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
  144. static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
  145. struct ext4_inode_info *locked_ei);
  146. int __init ext4_init_es(void)
  147. {
  148. ext4_es_cachep = kmem_cache_create("ext4_extent_status",
  149. sizeof(struct extent_status),
  150. 0, (SLAB_RECLAIM_ACCOUNT), NULL);
  151. if (ext4_es_cachep == NULL)
  152. return -ENOMEM;
  153. return 0;
  154. }
  155. void ext4_exit_es(void)
  156. {
  157. if (ext4_es_cachep)
  158. kmem_cache_destroy(ext4_es_cachep);
  159. }
  160. void ext4_es_init_tree(struct ext4_es_tree *tree)
  161. {
  162. tree->root = RB_ROOT;
  163. tree->cache_es = NULL;
  164. }
  165. #ifdef ES_DEBUG__
  166. static void ext4_es_print_tree(struct inode *inode)
  167. {
  168. struct ext4_es_tree *tree;
  169. struct rb_node *node;
  170. printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
  171. tree = &EXT4_I(inode)->i_es_tree;
  172. node = rb_first(&tree->root);
  173. while (node) {
  174. struct extent_status *es;
  175. es = rb_entry(node, struct extent_status, rb_node);
  176. printk(KERN_DEBUG " [%u/%u) %llu %x",
  177. es->es_lblk, es->es_len,
  178. ext4_es_pblock(es), ext4_es_status(es));
  179. node = rb_next(node);
  180. }
  181. printk(KERN_DEBUG "\n");
  182. }
  183. #else
  184. #define ext4_es_print_tree(inode)
  185. #endif
  186. static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
  187. {
  188. BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
  189. return es->es_lblk + es->es_len - 1;
  190. }
  191. /*
  192. * search through the tree for an delayed extent with a given offset. If
  193. * it can't be found, try to find next extent.
  194. */
  195. static struct extent_status *__es_tree_search(struct rb_root *root,
  196. ext4_lblk_t lblk)
  197. {
  198. struct rb_node *node = root->rb_node;
  199. struct extent_status *es = NULL;
  200. while (node) {
  201. es = rb_entry(node, struct extent_status, rb_node);
  202. if (lblk < es->es_lblk)
  203. node = node->rb_left;
  204. else if (lblk > ext4_es_end(es))
  205. node = node->rb_right;
  206. else
  207. return es;
  208. }
  209. if (es && lblk < es->es_lblk)
  210. return es;
  211. if (es && lblk > ext4_es_end(es)) {
  212. node = rb_next(&es->rb_node);
  213. return node ? rb_entry(node, struct extent_status, rb_node) :
  214. NULL;
  215. }
  216. return NULL;
  217. }
  218. /*
  219. * ext4_es_find_delayed_extent_range: find the 1st delayed extent covering
  220. * @es->lblk if it exists, otherwise, the next extent after @es->lblk.
  221. *
  222. * @inode: the inode which owns delayed extents
  223. * @lblk: the offset where we start to search
  224. * @end: the offset where we stop to search
  225. * @es: delayed extent that we found
  226. */
  227. void ext4_es_find_delayed_extent_range(struct inode *inode,
  228. ext4_lblk_t lblk, ext4_lblk_t end,
  229. struct extent_status *es)
  230. {
  231. struct ext4_es_tree *tree = NULL;
  232. struct extent_status *es1 = NULL;
  233. struct rb_node *node;
  234. BUG_ON(es == NULL);
  235. BUG_ON(end < lblk);
  236. trace_ext4_es_find_delayed_extent_range_enter(inode, lblk);
  237. read_lock(&EXT4_I(inode)->i_es_lock);
  238. tree = &EXT4_I(inode)->i_es_tree;
  239. /* find extent in cache firstly */
  240. es->es_lblk = es->es_len = es->es_pblk = 0;
  241. if (tree->cache_es) {
  242. es1 = tree->cache_es;
  243. if (in_range(lblk, es1->es_lblk, es1->es_len)) {
  244. es_debug("%u cached by [%u/%u) %llu %x\n",
  245. lblk, es1->es_lblk, es1->es_len,
  246. ext4_es_pblock(es1), ext4_es_status(es1));
  247. goto out;
  248. }
  249. }
  250. es1 = __es_tree_search(&tree->root, lblk);
  251. out:
  252. if (es1 && !ext4_es_is_delayed(es1)) {
  253. while ((node = rb_next(&es1->rb_node)) != NULL) {
  254. es1 = rb_entry(node, struct extent_status, rb_node);
  255. if (es1->es_lblk > end) {
  256. es1 = NULL;
  257. break;
  258. }
  259. if (ext4_es_is_delayed(es1))
  260. break;
  261. }
  262. }
  263. if (es1 && ext4_es_is_delayed(es1)) {
  264. tree->cache_es = es1;
  265. es->es_lblk = es1->es_lblk;
  266. es->es_len = es1->es_len;
  267. es->es_pblk = es1->es_pblk;
  268. }
  269. read_unlock(&EXT4_I(inode)->i_es_lock);
  270. trace_ext4_es_find_delayed_extent_range_exit(inode, es);
  271. }
  272. static void ext4_es_list_add(struct inode *inode)
  273. {
  274. struct ext4_inode_info *ei = EXT4_I(inode);
  275. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  276. if (!list_empty(&ei->i_es_list))
  277. return;
  278. spin_lock(&sbi->s_es_lock);
  279. if (list_empty(&ei->i_es_list)) {
  280. list_add_tail(&ei->i_es_list, &sbi->s_es_list);
  281. sbi->s_es_nr_inode++;
  282. }
  283. spin_unlock(&sbi->s_es_lock);
  284. }
  285. static void ext4_es_list_del(struct inode *inode)
  286. {
  287. struct ext4_inode_info *ei = EXT4_I(inode);
  288. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  289. spin_lock(&sbi->s_es_lock);
  290. if (!list_empty(&ei->i_es_list)) {
  291. list_del_init(&ei->i_es_list);
  292. sbi->s_es_nr_inode--;
  293. WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
  294. }
  295. spin_unlock(&sbi->s_es_lock);
  296. }
  297. static struct extent_status *
  298. ext4_es_alloc_extent(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
  299. ext4_fsblk_t pblk)
  300. {
  301. struct extent_status *es;
  302. es = kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
  303. if (es == NULL)
  304. return NULL;
  305. es->es_lblk = lblk;
  306. es->es_len = len;
  307. es->es_pblk = pblk;
  308. /*
  309. * We don't count delayed extent because we never try to reclaim them
  310. */
  311. if (!ext4_es_is_delayed(es)) {
  312. if (!EXT4_I(inode)->i_es_shk_nr++)
  313. ext4_es_list_add(inode);
  314. percpu_counter_inc(&EXT4_SB(inode->i_sb)->
  315. s_es_stats.es_stats_shk_cnt);
  316. }
  317. EXT4_I(inode)->i_es_all_nr++;
  318. percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
  319. return es;
  320. }
  321. static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
  322. {
  323. EXT4_I(inode)->i_es_all_nr--;
  324. percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
  325. /* Decrease the shrink counter when this es is not delayed */
  326. if (!ext4_es_is_delayed(es)) {
  327. BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
  328. if (!--EXT4_I(inode)->i_es_shk_nr)
  329. ext4_es_list_del(inode);
  330. percpu_counter_dec(&EXT4_SB(inode->i_sb)->
  331. s_es_stats.es_stats_shk_cnt);
  332. }
  333. kmem_cache_free(ext4_es_cachep, es);
  334. }
  335. /*
  336. * Check whether or not two extents can be merged
  337. * Condition:
  338. * - logical block number is contiguous
  339. * - physical block number is contiguous
  340. * - status is equal
  341. */
  342. static int ext4_es_can_be_merged(struct extent_status *es1,
  343. struct extent_status *es2)
  344. {
  345. if (ext4_es_type(es1) != ext4_es_type(es2))
  346. return 0;
  347. if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
  348. pr_warn("ES assertion failed when merging extents. "
  349. "The sum of lengths of es1 (%d) and es2 (%d) "
  350. "is bigger than allowed file size (%d)\n",
  351. es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
  352. WARN_ON(1);
  353. return 0;
  354. }
  355. if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
  356. return 0;
  357. if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
  358. (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
  359. return 1;
  360. if (ext4_es_is_hole(es1))
  361. return 1;
  362. /* we need to check delayed extent is without unwritten status */
  363. if (ext4_es_is_delayed(es1) && !ext4_es_is_unwritten(es1))
  364. return 1;
  365. return 0;
  366. }
  367. static struct extent_status *
  368. ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
  369. {
  370. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  371. struct extent_status *es1;
  372. struct rb_node *node;
  373. node = rb_prev(&es->rb_node);
  374. if (!node)
  375. return es;
  376. es1 = rb_entry(node, struct extent_status, rb_node);
  377. if (ext4_es_can_be_merged(es1, es)) {
  378. es1->es_len += es->es_len;
  379. if (ext4_es_is_referenced(es))
  380. ext4_es_set_referenced(es1);
  381. rb_erase(&es->rb_node, &tree->root);
  382. ext4_es_free_extent(inode, es);
  383. es = es1;
  384. }
  385. return es;
  386. }
  387. static struct extent_status *
  388. ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
  389. {
  390. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  391. struct extent_status *es1;
  392. struct rb_node *node;
  393. node = rb_next(&es->rb_node);
  394. if (!node)
  395. return es;
  396. es1 = rb_entry(node, struct extent_status, rb_node);
  397. if (ext4_es_can_be_merged(es, es1)) {
  398. es->es_len += es1->es_len;
  399. if (ext4_es_is_referenced(es1))
  400. ext4_es_set_referenced(es);
  401. rb_erase(node, &tree->root);
  402. ext4_es_free_extent(inode, es1);
  403. }
  404. return es;
  405. }
  406. #ifdef ES_AGGRESSIVE_TEST
  407. #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
  408. static void ext4_es_insert_extent_ext_check(struct inode *inode,
  409. struct extent_status *es)
  410. {
  411. struct ext4_ext_path *path = NULL;
  412. struct ext4_extent *ex;
  413. ext4_lblk_t ee_block;
  414. ext4_fsblk_t ee_start;
  415. unsigned short ee_len;
  416. int depth, ee_status, es_status;
  417. path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
  418. if (IS_ERR(path))
  419. return;
  420. depth = ext_depth(inode);
  421. ex = path[depth].p_ext;
  422. if (ex) {
  423. ee_block = le32_to_cpu(ex->ee_block);
  424. ee_start = ext4_ext_pblock(ex);
  425. ee_len = ext4_ext_get_actual_len(ex);
  426. ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
  427. es_status = ext4_es_is_unwritten(es) ? 1 : 0;
  428. /*
  429. * Make sure ex and es are not overlap when we try to insert
  430. * a delayed/hole extent.
  431. */
  432. if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
  433. if (in_range(es->es_lblk, ee_block, ee_len)) {
  434. pr_warn("ES insert assertion failed for "
  435. "inode: %lu we can find an extent "
  436. "at block [%d/%d/%llu/%c], but we "
  437. "want to add a delayed/hole extent "
  438. "[%d/%d/%llu/%x]\n",
  439. inode->i_ino, ee_block, ee_len,
  440. ee_start, ee_status ? 'u' : 'w',
  441. es->es_lblk, es->es_len,
  442. ext4_es_pblock(es), ext4_es_status(es));
  443. }
  444. goto out;
  445. }
  446. /*
  447. * We don't check ee_block == es->es_lblk, etc. because es
  448. * might be a part of whole extent, vice versa.
  449. */
  450. if (es->es_lblk < ee_block ||
  451. ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
  452. pr_warn("ES insert assertion failed for inode: %lu "
  453. "ex_status [%d/%d/%llu/%c] != "
  454. "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
  455. ee_block, ee_len, ee_start,
  456. ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
  457. ext4_es_pblock(es), es_status ? 'u' : 'w');
  458. goto out;
  459. }
  460. if (ee_status ^ es_status) {
  461. pr_warn("ES insert assertion failed for inode: %lu "
  462. "ex_status [%d/%d/%llu/%c] != "
  463. "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
  464. ee_block, ee_len, ee_start,
  465. ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
  466. ext4_es_pblock(es), es_status ? 'u' : 'w');
  467. }
  468. } else {
  469. /*
  470. * We can't find an extent on disk. So we need to make sure
  471. * that we don't want to add an written/unwritten extent.
  472. */
  473. if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
  474. pr_warn("ES insert assertion failed for inode: %lu "
  475. "can't find an extent at block %d but we want "
  476. "to add a written/unwritten extent "
  477. "[%d/%d/%llu/%x]\n", inode->i_ino,
  478. es->es_lblk, es->es_lblk, es->es_len,
  479. ext4_es_pblock(es), ext4_es_status(es));
  480. }
  481. }
  482. out:
  483. ext4_ext_drop_refs(path);
  484. kfree(path);
  485. }
  486. static void ext4_es_insert_extent_ind_check(struct inode *inode,
  487. struct extent_status *es)
  488. {
  489. struct ext4_map_blocks map;
  490. int retval;
  491. /*
  492. * Here we call ext4_ind_map_blocks to lookup a block mapping because
  493. * 'Indirect' structure is defined in indirect.c. So we couldn't
  494. * access direct/indirect tree from outside. It is too dirty to define
  495. * this function in indirect.c file.
  496. */
  497. map.m_lblk = es->es_lblk;
  498. map.m_len = es->es_len;
  499. retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
  500. if (retval > 0) {
  501. if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
  502. /*
  503. * We want to add a delayed/hole extent but this
  504. * block has been allocated.
  505. */
  506. pr_warn("ES insert assertion failed for inode: %lu "
  507. "We can find blocks but we want to add a "
  508. "delayed/hole extent [%d/%d/%llu/%x]\n",
  509. inode->i_ino, es->es_lblk, es->es_len,
  510. ext4_es_pblock(es), ext4_es_status(es));
  511. return;
  512. } else if (ext4_es_is_written(es)) {
  513. if (retval != es->es_len) {
  514. pr_warn("ES insert assertion failed for "
  515. "inode: %lu retval %d != es_len %d\n",
  516. inode->i_ino, retval, es->es_len);
  517. return;
  518. }
  519. if (map.m_pblk != ext4_es_pblock(es)) {
  520. pr_warn("ES insert assertion failed for "
  521. "inode: %lu m_pblk %llu != "
  522. "es_pblk %llu\n",
  523. inode->i_ino, map.m_pblk,
  524. ext4_es_pblock(es));
  525. return;
  526. }
  527. } else {
  528. /*
  529. * We don't need to check unwritten extent because
  530. * indirect-based file doesn't have it.
  531. */
  532. BUG_ON(1);
  533. }
  534. } else if (retval == 0) {
  535. if (ext4_es_is_written(es)) {
  536. pr_warn("ES insert assertion failed for inode: %lu "
  537. "We can't find the block but we want to add "
  538. "a written extent [%d/%d/%llu/%x]\n",
  539. inode->i_ino, es->es_lblk, es->es_len,
  540. ext4_es_pblock(es), ext4_es_status(es));
  541. return;
  542. }
  543. }
  544. }
  545. static inline void ext4_es_insert_extent_check(struct inode *inode,
  546. struct extent_status *es)
  547. {
  548. /*
  549. * We don't need to worry about the race condition because
  550. * caller takes i_data_sem locking.
  551. */
  552. BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
  553. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  554. ext4_es_insert_extent_ext_check(inode, es);
  555. else
  556. ext4_es_insert_extent_ind_check(inode, es);
  557. }
  558. #else
  559. static inline void ext4_es_insert_extent_check(struct inode *inode,
  560. struct extent_status *es)
  561. {
  562. }
  563. #endif
  564. static int __es_insert_extent(struct inode *inode, struct extent_status *newes)
  565. {
  566. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  567. struct rb_node **p = &tree->root.rb_node;
  568. struct rb_node *parent = NULL;
  569. struct extent_status *es;
  570. while (*p) {
  571. parent = *p;
  572. es = rb_entry(parent, struct extent_status, rb_node);
  573. if (newes->es_lblk < es->es_lblk) {
  574. if (ext4_es_can_be_merged(newes, es)) {
  575. /*
  576. * Here we can modify es_lblk directly
  577. * because it isn't overlapped.
  578. */
  579. es->es_lblk = newes->es_lblk;
  580. es->es_len += newes->es_len;
  581. if (ext4_es_is_written(es) ||
  582. ext4_es_is_unwritten(es))
  583. ext4_es_store_pblock(es,
  584. newes->es_pblk);
  585. es = ext4_es_try_to_merge_left(inode, es);
  586. goto out;
  587. }
  588. p = &(*p)->rb_left;
  589. } else if (newes->es_lblk > ext4_es_end(es)) {
  590. if (ext4_es_can_be_merged(es, newes)) {
  591. es->es_len += newes->es_len;
  592. es = ext4_es_try_to_merge_right(inode, es);
  593. goto out;
  594. }
  595. p = &(*p)->rb_right;
  596. } else {
  597. BUG_ON(1);
  598. return -EINVAL;
  599. }
  600. }
  601. es = ext4_es_alloc_extent(inode, newes->es_lblk, newes->es_len,
  602. newes->es_pblk);
  603. if (!es)
  604. return -ENOMEM;
  605. rb_link_node(&es->rb_node, parent, p);
  606. rb_insert_color(&es->rb_node, &tree->root);
  607. out:
  608. tree->cache_es = es;
  609. return 0;
  610. }
  611. /*
  612. * ext4_es_insert_extent() adds information to an inode's extent
  613. * status tree.
  614. *
  615. * Return 0 on success, error code on failure.
  616. */
  617. int ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
  618. ext4_lblk_t len, ext4_fsblk_t pblk,
  619. unsigned int status)
  620. {
  621. struct extent_status newes;
  622. ext4_lblk_t end = lblk + len - 1;
  623. int err = 0;
  624. es_debug("add [%u/%u) %llu %x to extent status tree of inode %lu\n",
  625. lblk, len, pblk, status, inode->i_ino);
  626. if (!len)
  627. return 0;
  628. BUG_ON(end < lblk);
  629. if ((status & EXTENT_STATUS_DELAYED) &&
  630. (status & EXTENT_STATUS_WRITTEN)) {
  631. ext4_warning(inode->i_sb, "Inserting extent [%u/%u] as "
  632. " delayed and written which can potentially "
  633. " cause data loss.\n", lblk, len);
  634. WARN_ON(1);
  635. }
  636. newes.es_lblk = lblk;
  637. newes.es_len = len;
  638. ext4_es_store_pblock_status(&newes, pblk, status);
  639. trace_ext4_es_insert_extent(inode, &newes);
  640. ext4_es_insert_extent_check(inode, &newes);
  641. write_lock(&EXT4_I(inode)->i_es_lock);
  642. err = __es_remove_extent(inode, lblk, end);
  643. if (err != 0)
  644. goto error;
  645. retry:
  646. err = __es_insert_extent(inode, &newes);
  647. if (err == -ENOMEM && __es_shrink(EXT4_SB(inode->i_sb),
  648. 128, EXT4_I(inode)))
  649. goto retry;
  650. if (err == -ENOMEM && !ext4_es_is_delayed(&newes))
  651. err = 0;
  652. error:
  653. write_unlock(&EXT4_I(inode)->i_es_lock);
  654. ext4_es_print_tree(inode);
  655. return err;
  656. }
  657. /*
  658. * ext4_es_cache_extent() inserts information into the extent status
  659. * tree if and only if there isn't information about the range in
  660. * question already.
  661. */
  662. void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
  663. ext4_lblk_t len, ext4_fsblk_t pblk,
  664. unsigned int status)
  665. {
  666. struct extent_status *es;
  667. struct extent_status newes;
  668. ext4_lblk_t end = lblk + len - 1;
  669. newes.es_lblk = lblk;
  670. newes.es_len = len;
  671. ext4_es_store_pblock_status(&newes, pblk, status);
  672. trace_ext4_es_cache_extent(inode, &newes);
  673. if (!len)
  674. return;
  675. BUG_ON(end < lblk);
  676. write_lock(&EXT4_I(inode)->i_es_lock);
  677. es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
  678. if (!es || es->es_lblk > end)
  679. __es_insert_extent(inode, &newes);
  680. write_unlock(&EXT4_I(inode)->i_es_lock);
  681. }
  682. /*
  683. * ext4_es_lookup_extent() looks up an extent in extent status tree.
  684. *
  685. * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
  686. *
  687. * Return: 1 on found, 0 on not
  688. */
  689. int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
  690. struct extent_status *es)
  691. {
  692. struct ext4_es_tree *tree;
  693. struct ext4_es_stats *stats;
  694. struct extent_status *es1 = NULL;
  695. struct rb_node *node;
  696. int found = 0;
  697. trace_ext4_es_lookup_extent_enter(inode, lblk);
  698. es_debug("lookup extent in block %u\n", lblk);
  699. tree = &EXT4_I(inode)->i_es_tree;
  700. read_lock(&EXT4_I(inode)->i_es_lock);
  701. /* find extent in cache firstly */
  702. es->es_lblk = es->es_len = es->es_pblk = 0;
  703. if (tree->cache_es) {
  704. es1 = tree->cache_es;
  705. if (in_range(lblk, es1->es_lblk, es1->es_len)) {
  706. es_debug("%u cached by [%u/%u)\n",
  707. lblk, es1->es_lblk, es1->es_len);
  708. found = 1;
  709. goto out;
  710. }
  711. }
  712. node = tree->root.rb_node;
  713. while (node) {
  714. es1 = rb_entry(node, struct extent_status, rb_node);
  715. if (lblk < es1->es_lblk)
  716. node = node->rb_left;
  717. else if (lblk > ext4_es_end(es1))
  718. node = node->rb_right;
  719. else {
  720. found = 1;
  721. break;
  722. }
  723. }
  724. out:
  725. stats = &EXT4_SB(inode->i_sb)->s_es_stats;
  726. if (found) {
  727. BUG_ON(!es1);
  728. es->es_lblk = es1->es_lblk;
  729. es->es_len = es1->es_len;
  730. es->es_pblk = es1->es_pblk;
  731. if (!ext4_es_is_referenced(es))
  732. ext4_es_set_referenced(es);
  733. stats->es_stats_cache_hits++;
  734. } else {
  735. stats->es_stats_cache_misses++;
  736. }
  737. read_unlock(&EXT4_I(inode)->i_es_lock);
  738. trace_ext4_es_lookup_extent_exit(inode, es, found);
  739. return found;
  740. }
  741. static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  742. ext4_lblk_t end)
  743. {
  744. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  745. struct rb_node *node;
  746. struct extent_status *es;
  747. struct extent_status orig_es;
  748. ext4_lblk_t len1, len2;
  749. ext4_fsblk_t block;
  750. int err;
  751. retry:
  752. err = 0;
  753. es = __es_tree_search(&tree->root, lblk);
  754. if (!es)
  755. goto out;
  756. if (es->es_lblk > end)
  757. goto out;
  758. /* Simply invalidate cache_es. */
  759. tree->cache_es = NULL;
  760. orig_es.es_lblk = es->es_lblk;
  761. orig_es.es_len = es->es_len;
  762. orig_es.es_pblk = es->es_pblk;
  763. len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
  764. len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
  765. if (len1 > 0)
  766. es->es_len = len1;
  767. if (len2 > 0) {
  768. if (len1 > 0) {
  769. struct extent_status newes;
  770. newes.es_lblk = end + 1;
  771. newes.es_len = len2;
  772. block = 0x7FDEADBEEFULL;
  773. if (ext4_es_is_written(&orig_es) ||
  774. ext4_es_is_unwritten(&orig_es))
  775. block = ext4_es_pblock(&orig_es) +
  776. orig_es.es_len - len2;
  777. ext4_es_store_pblock_status(&newes, block,
  778. ext4_es_status(&orig_es));
  779. err = __es_insert_extent(inode, &newes);
  780. if (err) {
  781. es->es_lblk = orig_es.es_lblk;
  782. es->es_len = orig_es.es_len;
  783. if ((err == -ENOMEM) &&
  784. __es_shrink(EXT4_SB(inode->i_sb),
  785. 128, EXT4_I(inode)))
  786. goto retry;
  787. goto out;
  788. }
  789. } else {
  790. es->es_lblk = end + 1;
  791. es->es_len = len2;
  792. if (ext4_es_is_written(es) ||
  793. ext4_es_is_unwritten(es)) {
  794. block = orig_es.es_pblk + orig_es.es_len - len2;
  795. ext4_es_store_pblock(es, block);
  796. }
  797. }
  798. goto out;
  799. }
  800. if (len1 > 0) {
  801. node = rb_next(&es->rb_node);
  802. if (node)
  803. es = rb_entry(node, struct extent_status, rb_node);
  804. else
  805. es = NULL;
  806. }
  807. while (es && ext4_es_end(es) <= end) {
  808. node = rb_next(&es->rb_node);
  809. rb_erase(&es->rb_node, &tree->root);
  810. ext4_es_free_extent(inode, es);
  811. if (!node) {
  812. es = NULL;
  813. break;
  814. }
  815. es = rb_entry(node, struct extent_status, rb_node);
  816. }
  817. if (es && es->es_lblk < end + 1) {
  818. ext4_lblk_t orig_len = es->es_len;
  819. len1 = ext4_es_end(es) - end;
  820. es->es_lblk = end + 1;
  821. es->es_len = len1;
  822. if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
  823. block = es->es_pblk + orig_len - len1;
  824. ext4_es_store_pblock(es, block);
  825. }
  826. }
  827. out:
  828. return err;
  829. }
  830. /*
  831. * ext4_es_remove_extent() removes a space from a extent status tree.
  832. *
  833. * Return 0 on success, error code on failure.
  834. */
  835. int ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  836. ext4_lblk_t len)
  837. {
  838. ext4_lblk_t end;
  839. int err = 0;
  840. trace_ext4_es_remove_extent(inode, lblk, len);
  841. es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
  842. lblk, len, inode->i_ino);
  843. if (!len)
  844. return err;
  845. end = lblk + len - 1;
  846. BUG_ON(end < lblk);
  847. /*
  848. * ext4_clear_inode() depends on us taking i_es_lock unconditionally
  849. * so that we are sure __es_shrink() is done with the inode before it
  850. * is reclaimed.
  851. */
  852. write_lock(&EXT4_I(inode)->i_es_lock);
  853. err = __es_remove_extent(inode, lblk, end);
  854. write_unlock(&EXT4_I(inode)->i_es_lock);
  855. ext4_es_print_tree(inode);
  856. return err;
  857. }
  858. static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
  859. struct ext4_inode_info *locked_ei)
  860. {
  861. struct ext4_inode_info *ei;
  862. struct ext4_es_stats *es_stats;
  863. ktime_t start_time;
  864. u64 scan_time;
  865. int nr_to_walk;
  866. int nr_shrunk = 0;
  867. int retried = 0, nr_skipped = 0;
  868. es_stats = &sbi->s_es_stats;
  869. start_time = ktime_get();
  870. retry:
  871. spin_lock(&sbi->s_es_lock);
  872. nr_to_walk = sbi->s_es_nr_inode;
  873. while (nr_to_walk-- > 0) {
  874. if (list_empty(&sbi->s_es_list)) {
  875. spin_unlock(&sbi->s_es_lock);
  876. goto out;
  877. }
  878. ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
  879. i_es_list);
  880. /* Move the inode to the tail */
  881. list_move_tail(&ei->i_es_list, &sbi->s_es_list);
  882. /*
  883. * Normally we try hard to avoid shrinking precached inodes,
  884. * but we will as a last resort.
  885. */
  886. if (!retried && ext4_test_inode_state(&ei->vfs_inode,
  887. EXT4_STATE_EXT_PRECACHED)) {
  888. nr_skipped++;
  889. continue;
  890. }
  891. if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
  892. nr_skipped++;
  893. continue;
  894. }
  895. /*
  896. * Now we hold i_es_lock which protects us from inode reclaim
  897. * freeing inode under us
  898. */
  899. spin_unlock(&sbi->s_es_lock);
  900. nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
  901. write_unlock(&ei->i_es_lock);
  902. if (nr_to_scan <= 0)
  903. goto out;
  904. spin_lock(&sbi->s_es_lock);
  905. }
  906. spin_unlock(&sbi->s_es_lock);
  907. /*
  908. * If we skipped any inodes, and we weren't able to make any
  909. * forward progress, try again to scan precached inodes.
  910. */
  911. if ((nr_shrunk == 0) && nr_skipped && !retried) {
  912. retried++;
  913. goto retry;
  914. }
  915. if (locked_ei && nr_shrunk == 0)
  916. nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
  917. out:
  918. scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
  919. if (likely(es_stats->es_stats_scan_time))
  920. es_stats->es_stats_scan_time = (scan_time +
  921. es_stats->es_stats_scan_time*3) / 4;
  922. else
  923. es_stats->es_stats_scan_time = scan_time;
  924. if (scan_time > es_stats->es_stats_max_scan_time)
  925. es_stats->es_stats_max_scan_time = scan_time;
  926. if (likely(es_stats->es_stats_shrunk))
  927. es_stats->es_stats_shrunk = (nr_shrunk +
  928. es_stats->es_stats_shrunk*3) / 4;
  929. else
  930. es_stats->es_stats_shrunk = nr_shrunk;
  931. trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
  932. nr_skipped, retried);
  933. return nr_shrunk;
  934. }
  935. static unsigned long ext4_es_count(struct shrinker *shrink,
  936. struct shrink_control *sc)
  937. {
  938. unsigned long nr;
  939. struct ext4_sb_info *sbi;
  940. sbi = container_of(shrink, struct ext4_sb_info, s_es_shrinker);
  941. nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
  942. trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
  943. return nr;
  944. }
  945. static unsigned long ext4_es_scan(struct shrinker *shrink,
  946. struct shrink_control *sc)
  947. {
  948. struct ext4_sb_info *sbi = container_of(shrink,
  949. struct ext4_sb_info, s_es_shrinker);
  950. int nr_to_scan = sc->nr_to_scan;
  951. int ret, nr_shrunk;
  952. ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
  953. trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
  954. if (!nr_to_scan)
  955. return ret;
  956. nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
  957. trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
  958. return nr_shrunk;
  959. }
  960. int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
  961. {
  962. struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
  963. struct ext4_es_stats *es_stats = &sbi->s_es_stats;
  964. struct ext4_inode_info *ei, *max = NULL;
  965. unsigned int inode_cnt = 0;
  966. if (v != SEQ_START_TOKEN)
  967. return 0;
  968. /* here we just find an inode that has the max nr. of objects */
  969. spin_lock(&sbi->s_es_lock);
  970. list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
  971. inode_cnt++;
  972. if (max && max->i_es_all_nr < ei->i_es_all_nr)
  973. max = ei;
  974. else if (!max)
  975. max = ei;
  976. }
  977. spin_unlock(&sbi->s_es_lock);
  978. seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
  979. percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
  980. percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
  981. seq_printf(seq, " %lu/%lu cache hits/misses\n",
  982. es_stats->es_stats_cache_hits,
  983. es_stats->es_stats_cache_misses);
  984. if (inode_cnt)
  985. seq_printf(seq, " %d inodes on list\n", inode_cnt);
  986. seq_printf(seq, "average:\n %llu us scan time\n",
  987. div_u64(es_stats->es_stats_scan_time, 1000));
  988. seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
  989. if (inode_cnt)
  990. seq_printf(seq,
  991. "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
  992. " %llu us max scan time\n",
  993. max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
  994. div_u64(es_stats->es_stats_max_scan_time, 1000));
  995. return 0;
  996. }
  997. int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
  998. {
  999. int err;
  1000. /* Make sure we have enough bits for physical block number */
  1001. BUILD_BUG_ON(ES_SHIFT < 48);
  1002. INIT_LIST_HEAD(&sbi->s_es_list);
  1003. sbi->s_es_nr_inode = 0;
  1004. spin_lock_init(&sbi->s_es_lock);
  1005. sbi->s_es_stats.es_stats_shrunk = 0;
  1006. sbi->s_es_stats.es_stats_cache_hits = 0;
  1007. sbi->s_es_stats.es_stats_cache_misses = 0;
  1008. sbi->s_es_stats.es_stats_scan_time = 0;
  1009. sbi->s_es_stats.es_stats_max_scan_time = 0;
  1010. err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
  1011. if (err)
  1012. return err;
  1013. err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
  1014. if (err)
  1015. goto err1;
  1016. sbi->s_es_shrinker.scan_objects = ext4_es_scan;
  1017. sbi->s_es_shrinker.count_objects = ext4_es_count;
  1018. sbi->s_es_shrinker.seeks = DEFAULT_SEEKS;
  1019. err = register_shrinker(&sbi->s_es_shrinker);
  1020. if (err)
  1021. goto err2;
  1022. return 0;
  1023. err2:
  1024. percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
  1025. err1:
  1026. percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
  1027. return err;
  1028. }
  1029. void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
  1030. {
  1031. percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
  1032. percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
  1033. unregister_shrinker(&sbi->s_es_shrinker);
  1034. }
  1035. /*
  1036. * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
  1037. * most *nr_to_scan extents, update *nr_to_scan accordingly.
  1038. *
  1039. * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
  1040. * Increment *nr_shrunk by the number of reclaimed extents. Also update
  1041. * ei->i_es_shrink_lblk to where we should continue scanning.
  1042. */
  1043. static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
  1044. int *nr_to_scan, int *nr_shrunk)
  1045. {
  1046. struct inode *inode = &ei->vfs_inode;
  1047. struct ext4_es_tree *tree = &ei->i_es_tree;
  1048. struct extent_status *es;
  1049. struct rb_node *node;
  1050. es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
  1051. if (!es)
  1052. goto out_wrap;
  1053. node = &es->rb_node;
  1054. while (*nr_to_scan > 0) {
  1055. if (es->es_lblk > end) {
  1056. ei->i_es_shrink_lblk = end + 1;
  1057. return 0;
  1058. }
  1059. (*nr_to_scan)--;
  1060. node = rb_next(&es->rb_node);
  1061. /*
  1062. * We can't reclaim delayed extent from status tree because
  1063. * fiemap, bigallic, and seek_data/hole need to use it.
  1064. */
  1065. if (ext4_es_is_delayed(es))
  1066. goto next;
  1067. if (ext4_es_is_referenced(es)) {
  1068. ext4_es_clear_referenced(es);
  1069. goto next;
  1070. }
  1071. rb_erase(&es->rb_node, &tree->root);
  1072. ext4_es_free_extent(inode, es);
  1073. (*nr_shrunk)++;
  1074. next:
  1075. if (!node)
  1076. goto out_wrap;
  1077. es = rb_entry(node, struct extent_status, rb_node);
  1078. }
  1079. ei->i_es_shrink_lblk = es->es_lblk;
  1080. return 1;
  1081. out_wrap:
  1082. ei->i_es_shrink_lblk = 0;
  1083. return 0;
  1084. }
  1085. static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
  1086. {
  1087. struct inode *inode = &ei->vfs_inode;
  1088. int nr_shrunk = 0;
  1089. ext4_lblk_t start = ei->i_es_shrink_lblk;
  1090. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  1091. DEFAULT_RATELIMIT_BURST);
  1092. if (ei->i_es_shk_nr == 0)
  1093. return 0;
  1094. if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
  1095. __ratelimit(&_rs))
  1096. ext4_warning(inode->i_sb, "forced shrink of precached extents");
  1097. if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
  1098. start != 0)
  1099. es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
  1100. ei->i_es_tree.cache_es = NULL;
  1101. return nr_shrunk;
  1102. }