extents_status.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/ext4/extents_status.c
  4. *
  5. * Written by Yongqiang Yang <xiaoqiangnk@gmail.com>
  6. * Modified by
  7. * Allison Henderson <achender@linux.vnet.ibm.com>
  8. * Hugh Dickins <hughd@google.com>
  9. * Zheng Liu <wenqing.lz@taobao.com>
  10. *
  11. * Ext4 extents status tree core functions.
  12. */
  13. #include <linux/list_sort.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/seq_file.h>
  16. #include "ext4.h"
  17. #include <trace/events/ext4.h>
  18. #include <kunit/static_stub.h>
  19. /*
  20. * According to previous discussion in Ext4 Developer Workshop, we
  21. * will introduce a new structure called io tree to track all extent
  22. * status in order to solve some problems that we have met
  23. * (e.g. Reservation space warning), and provide extent-level locking.
  24. * Delay extent tree is the first step to achieve this goal. It is
  25. * original built by Yongqiang Yang. At that time it is called delay
  26. * extent tree, whose goal is only track delayed extents in memory to
  27. * simplify the implementation of fiemap and bigalloc, and introduce
  28. * lseek SEEK_DATA/SEEK_HOLE support. That is why it is still called
  29. * delay extent tree at the first commit. But for better understand
  30. * what it does, it has been rename to extent status tree.
  31. *
  32. * Step1:
  33. * Currently the first step has been done. All delayed extents are
  34. * tracked in the tree. It maintains the delayed extent when a delayed
  35. * allocation is issued, and the delayed extent is written out or
  36. * invalidated. Therefore the implementation of fiemap and bigalloc
  37. * are simplified, and SEEK_DATA/SEEK_HOLE are introduced.
  38. *
  39. * The following comment describes the implemenmtation of extent
  40. * status tree and future works.
  41. *
  42. * Step2:
  43. * In this step all extent status are tracked by extent status tree.
  44. * Thus, we can first try to lookup a block mapping in this tree before
  45. * finding it in extent tree. Hence, single extent cache can be removed
  46. * because extent status tree can do a better job. Extents in status
  47. * tree are loaded on-demand. Therefore, the extent status tree may not
  48. * contain all of the extents in a file. Meanwhile we define a shrinker
  49. * to reclaim memory from extent status tree because fragmented extent
  50. * tree will make status tree cost too much memory. written/unwritten/-
  51. * hole extents in the tree will be reclaimed by this shrinker when we
  52. * are under high memory pressure. Delayed extents will not be
  53. * reclimed because fiemap, bigalloc, and seek_data/hole need it.
  54. */
  55. /*
  56. * Extent status tree implementation for ext4.
  57. *
  58. *
  59. * ==========================================================================
  60. * Extent status tree tracks all extent status.
  61. *
  62. * 1. Why we need to implement extent status tree?
  63. *
  64. * Without extent status tree, ext4 identifies a delayed extent by looking
  65. * up page cache, this has several deficiencies - complicated, buggy,
  66. * and inefficient code.
  67. *
  68. * FIEMAP, SEEK_HOLE/DATA, bigalloc, and writeout all need to know if a
  69. * block or a range of blocks are belonged to a delayed extent.
  70. *
  71. * Let us have a look at how they do without extent status tree.
  72. * -- FIEMAP
  73. * FIEMAP looks up page cache to identify delayed allocations from holes.
  74. *
  75. * -- SEEK_HOLE/DATA
  76. * SEEK_HOLE/DATA has the same problem as FIEMAP.
  77. *
  78. * -- bigalloc
  79. * bigalloc looks up page cache to figure out if a block is
  80. * already under delayed allocation or not to determine whether
  81. * quota reserving is needed for the cluster.
  82. *
  83. * -- writeout
  84. * Writeout looks up whole page cache to see if a buffer is
  85. * mapped, If there are not very many delayed buffers, then it is
  86. * time consuming.
  87. *
  88. * With extent status tree implementation, FIEMAP, SEEK_HOLE/DATA,
  89. * bigalloc and writeout can figure out if a block or a range of
  90. * blocks is under delayed allocation(belonged to a delayed extent) or
  91. * not by searching the extent tree.
  92. *
  93. *
  94. * ==========================================================================
  95. * 2. Ext4 extent status tree impelmentation
  96. *
  97. * -- extent
  98. * A extent is a range of blocks which are contiguous logically and
  99. * physically. Unlike extent in extent tree, this extent in ext4 is
  100. * a in-memory struct, there is no corresponding on-disk data. There
  101. * is no limit on length of extent, so an extent can contain as many
  102. * blocks as they are contiguous logically and physically.
  103. *
  104. * -- extent status tree
  105. * Every inode has an extent status tree and all allocation blocks
  106. * are added to the tree with different status. The extent in the
  107. * tree are ordered by logical block no.
  108. *
  109. * -- operations on a extent status tree
  110. * There are three important operations on a delayed extent tree: find
  111. * next extent, adding a extent(a range of blocks) and removing a extent.
  112. *
  113. * -- race on a extent status tree
  114. * Extent status tree is protected by inode->i_es_lock.
  115. *
  116. * -- memory consumption
  117. * Fragmented extent tree will make extent status tree cost too much
  118. * memory. Hence, we will reclaim written/unwritten/hole extents from
  119. * the tree under a heavy memory pressure.
  120. *
  121. * ==========================================================================
  122. * 3. Assurance of Ext4 extent status tree consistency
  123. *
  124. * When mapping blocks, Ext4 queries the extent status tree first and should
  125. * always trusts that the extent status tree is consistent and up to date.
  126. * Therefore, it is important to adheres to the following rules when createing,
  127. * modifying and removing extents.
  128. *
  129. * 1. Besides fastcommit replay, when Ext4 creates or queries block mappings,
  130. * the extent information should always be processed through the extent
  131. * status tree instead of being organized manually through the on-disk
  132. * extent tree.
  133. *
  134. * 2. When updating the extent tree, Ext4 should acquire the i_data_sem
  135. * exclusively and update the extent status tree atomically. If the extents
  136. * to be modified are large enough to exceed the range that a single
  137. * i_data_sem can process (as ext4_datasem_ensure_credits() may drop
  138. * i_data_sem to restart a transaction), it must (e.g. as ext4_punch_hole()
  139. * does):
  140. *
  141. * a) Hold the i_rwsem and invalidate_lock exclusively. This ensures
  142. * exclusion against page faults, as well as reads and writes that may
  143. * concurrently modify the extent status tree.
  144. * b) Evict all page cache in the affected range and recommend rebuilding
  145. * or dropping the extent status tree after modifying the on-disk
  146. * extent tree. This ensures exclusion against concurrent writebacks
  147. * that do not hold those locks but only holds a folio lock.
  148. *
  149. * 3. Based on the rules above, when querying block mappings, Ext4 should at
  150. * least hold the i_rwsem or invalidate_lock or folio lock(s) for the
  151. * specified querying range.
  152. *
  153. * ==========================================================================
  154. * 4. Performance analysis
  155. *
  156. * -- overhead
  157. * 1. There is a cache extent for write access, so if writes are
  158. * not very random, adding space operaions are in O(1) time.
  159. *
  160. * -- gain
  161. * 2. Code is much simpler, more readable, more maintainable and
  162. * more efficient.
  163. *
  164. *
  165. * ==========================================================================
  166. * 5. TODO list
  167. *
  168. * -- Refactor delayed space reservation
  169. *
  170. * -- Extent-level locking
  171. */
  172. static struct kmem_cache *ext4_es_cachep;
  173. static struct kmem_cache *ext4_pending_cachep;
  174. static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
  175. struct extent_status *prealloc);
  176. static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  177. ext4_lblk_t end, unsigned int status,
  178. int *reserved, struct extent_status *res,
  179. struct extent_status *prealloc);
  180. static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
  181. static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
  182. struct ext4_inode_info *locked_ei);
  183. static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
  184. ext4_lblk_t len,
  185. struct pending_reservation **prealloc);
  186. int __init ext4_init_es(void)
  187. {
  188. ext4_es_cachep = KMEM_CACHE(extent_status, SLAB_RECLAIM_ACCOUNT);
  189. if (ext4_es_cachep == NULL)
  190. return -ENOMEM;
  191. return 0;
  192. }
  193. void ext4_exit_es(void)
  194. {
  195. kmem_cache_destroy(ext4_es_cachep);
  196. }
  197. void ext4_es_init_tree(struct ext4_es_tree *tree)
  198. {
  199. tree->root = RB_ROOT;
  200. tree->cache_es = NULL;
  201. }
  202. #ifdef ES_DEBUG__
  203. static void ext4_es_print_tree(struct inode *inode)
  204. {
  205. struct ext4_es_tree *tree;
  206. struct rb_node *node;
  207. printk(KERN_DEBUG "status extents for inode %lu:", inode->i_ino);
  208. tree = &EXT4_I(inode)->i_es_tree;
  209. node = rb_first(&tree->root);
  210. while (node) {
  211. struct extent_status *es;
  212. es = rb_entry(node, struct extent_status, rb_node);
  213. printk(KERN_DEBUG " [%u/%u) %llu %x",
  214. es->es_lblk, es->es_len,
  215. ext4_es_pblock(es), ext4_es_status(es));
  216. node = rb_next(node);
  217. }
  218. printk(KERN_DEBUG "\n");
  219. }
  220. #else
  221. #define ext4_es_print_tree(inode)
  222. #endif
  223. static inline ext4_lblk_t ext4_es_end(struct extent_status *es)
  224. {
  225. BUG_ON(es->es_lblk + es->es_len < es->es_lblk);
  226. return es->es_lblk + es->es_len - 1;
  227. }
  228. static inline void ext4_es_inc_seq(struct inode *inode)
  229. {
  230. struct ext4_inode_info *ei = EXT4_I(inode);
  231. WRITE_ONCE(ei->i_es_seq, ei->i_es_seq + 1);
  232. }
  233. static inline int __es_check_extent_status(struct extent_status *es,
  234. unsigned int status,
  235. struct extent_status *res)
  236. {
  237. if (ext4_es_type(es) & status)
  238. return 0;
  239. if (res) {
  240. res->es_lblk = es->es_lblk;
  241. res->es_len = es->es_len;
  242. res->es_pblk = es->es_pblk;
  243. }
  244. return -EINVAL;
  245. }
  246. /*
  247. * search through the tree for an delayed extent with a given offset. If
  248. * it can't be found, try to find next extent.
  249. */
  250. static struct extent_status *__es_tree_search(struct rb_root *root,
  251. ext4_lblk_t lblk)
  252. {
  253. struct rb_node *node = root->rb_node;
  254. struct extent_status *es = NULL;
  255. while (node) {
  256. es = rb_entry(node, struct extent_status, rb_node);
  257. if (lblk < es->es_lblk)
  258. node = node->rb_left;
  259. else if (lblk > ext4_es_end(es))
  260. node = node->rb_right;
  261. else
  262. return es;
  263. }
  264. if (es && lblk < es->es_lblk)
  265. return es;
  266. if (es && lblk > ext4_es_end(es)) {
  267. node = rb_next(&es->rb_node);
  268. return node ? rb_entry(node, struct extent_status, rb_node) :
  269. NULL;
  270. }
  271. return NULL;
  272. }
  273. /*
  274. * ext4_es_find_extent_range - find extent with specified status within block
  275. * range or next extent following block range in
  276. * extents status tree
  277. *
  278. * @inode - file containing the range
  279. * @matching_fn - pointer to function that matches extents with desired status
  280. * @lblk - logical block defining start of range
  281. * @end - logical block defining end of range
  282. * @es - extent found, if any
  283. *
  284. * Find the first extent within the block range specified by @lblk and @end
  285. * in the extents status tree that satisfies @matching_fn. If a match
  286. * is found, it's returned in @es. If not, and a matching extent is found
  287. * beyond the block range, it's returned in @es. If no match is found, an
  288. * extent is returned in @es whose es_lblk, es_len, and es_pblk components
  289. * are 0.
  290. */
  291. static void __es_find_extent_range(struct inode *inode,
  292. int (*matching_fn)(struct extent_status *es),
  293. ext4_lblk_t lblk, ext4_lblk_t end,
  294. struct extent_status *es)
  295. {
  296. struct ext4_es_tree *tree = NULL;
  297. struct extent_status *es1 = NULL;
  298. struct rb_node *node;
  299. WARN_ON(es == NULL);
  300. WARN_ON(end < lblk);
  301. tree = &EXT4_I(inode)->i_es_tree;
  302. /* see if the extent has been cached */
  303. es->es_lblk = es->es_len = es->es_pblk = 0;
  304. es1 = READ_ONCE(tree->cache_es);
  305. if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
  306. es_debug("%u cached by [%u/%u) %llu %x\n",
  307. lblk, es1->es_lblk, es1->es_len,
  308. ext4_es_pblock(es1), ext4_es_status(es1));
  309. goto out;
  310. }
  311. es1 = __es_tree_search(&tree->root, lblk);
  312. out:
  313. if (es1 && !matching_fn(es1)) {
  314. while ((node = rb_next(&es1->rb_node)) != NULL) {
  315. es1 = rb_entry(node, struct extent_status, rb_node);
  316. if (es1->es_lblk > end) {
  317. es1 = NULL;
  318. break;
  319. }
  320. if (matching_fn(es1))
  321. break;
  322. }
  323. }
  324. if (es1 && matching_fn(es1)) {
  325. WRITE_ONCE(tree->cache_es, es1);
  326. es->es_lblk = es1->es_lblk;
  327. es->es_len = es1->es_len;
  328. es->es_pblk = es1->es_pblk;
  329. }
  330. }
  331. /*
  332. * Locking for __es_find_extent_range() for external use
  333. */
  334. void ext4_es_find_extent_range(struct inode *inode,
  335. int (*matching_fn)(struct extent_status *es),
  336. ext4_lblk_t lblk, ext4_lblk_t end,
  337. struct extent_status *es)
  338. {
  339. es->es_lblk = es->es_len = es->es_pblk = 0;
  340. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  341. return;
  342. trace_ext4_es_find_extent_range_enter(inode, lblk);
  343. read_lock(&EXT4_I(inode)->i_es_lock);
  344. __es_find_extent_range(inode, matching_fn, lblk, end, es);
  345. read_unlock(&EXT4_I(inode)->i_es_lock);
  346. trace_ext4_es_find_extent_range_exit(inode, es);
  347. }
  348. /*
  349. * __es_scan_range - search block range for block with specified status
  350. * in extents status tree
  351. *
  352. * @inode - file containing the range
  353. * @matching_fn - pointer to function that matches extents with desired status
  354. * @lblk - logical block defining start of range
  355. * @end - logical block defining end of range
  356. *
  357. * Returns true if at least one block in the specified block range satisfies
  358. * the criterion specified by @matching_fn, and false if not. If at least
  359. * one extent has the specified status, then there is at least one block
  360. * in the cluster with that status. Should only be called by code that has
  361. * taken i_es_lock.
  362. */
  363. static bool __es_scan_range(struct inode *inode,
  364. int (*matching_fn)(struct extent_status *es),
  365. ext4_lblk_t start, ext4_lblk_t end)
  366. {
  367. struct extent_status es;
  368. __es_find_extent_range(inode, matching_fn, start, end, &es);
  369. if (es.es_len == 0)
  370. return false; /* no matching extent in the tree */
  371. else if (es.es_lblk <= start &&
  372. start < es.es_lblk + es.es_len)
  373. return true;
  374. else if (start <= es.es_lblk && es.es_lblk <= end)
  375. return true;
  376. else
  377. return false;
  378. }
  379. /*
  380. * Locking for __es_scan_range() for external use
  381. */
  382. bool ext4_es_scan_range(struct inode *inode,
  383. int (*matching_fn)(struct extent_status *es),
  384. ext4_lblk_t lblk, ext4_lblk_t end)
  385. {
  386. bool ret;
  387. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  388. return false;
  389. read_lock(&EXT4_I(inode)->i_es_lock);
  390. ret = __es_scan_range(inode, matching_fn, lblk, end);
  391. read_unlock(&EXT4_I(inode)->i_es_lock);
  392. return ret;
  393. }
  394. /*
  395. * __es_scan_clu - search cluster for block with specified status in
  396. * extents status tree
  397. *
  398. * @inode - file containing the cluster
  399. * @matching_fn - pointer to function that matches extents with desired status
  400. * @lblk - logical block in cluster to be searched
  401. *
  402. * Returns true if at least one extent in the cluster containing @lblk
  403. * satisfies the criterion specified by @matching_fn, and false if not. If at
  404. * least one extent has the specified status, then there is at least one block
  405. * in the cluster with that status. Should only be called by code that has
  406. * taken i_es_lock.
  407. */
  408. static bool __es_scan_clu(struct inode *inode,
  409. int (*matching_fn)(struct extent_status *es),
  410. ext4_lblk_t lblk)
  411. {
  412. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  413. ext4_lblk_t lblk_start, lblk_end;
  414. lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
  415. lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
  416. return __es_scan_range(inode, matching_fn, lblk_start, lblk_end);
  417. }
  418. /*
  419. * Locking for __es_scan_clu() for external use
  420. */
  421. bool ext4_es_scan_clu(struct inode *inode,
  422. int (*matching_fn)(struct extent_status *es),
  423. ext4_lblk_t lblk)
  424. {
  425. bool ret;
  426. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  427. return false;
  428. read_lock(&EXT4_I(inode)->i_es_lock);
  429. ret = __es_scan_clu(inode, matching_fn, lblk);
  430. read_unlock(&EXT4_I(inode)->i_es_lock);
  431. return ret;
  432. }
  433. static void ext4_es_list_add(struct inode *inode)
  434. {
  435. struct ext4_inode_info *ei = EXT4_I(inode);
  436. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  437. if (!list_empty(&ei->i_es_list))
  438. return;
  439. spin_lock(&sbi->s_es_lock);
  440. if (list_empty(&ei->i_es_list)) {
  441. list_add_tail(&ei->i_es_list, &sbi->s_es_list);
  442. sbi->s_es_nr_inode++;
  443. }
  444. spin_unlock(&sbi->s_es_lock);
  445. }
  446. static void ext4_es_list_del(struct inode *inode)
  447. {
  448. struct ext4_inode_info *ei = EXT4_I(inode);
  449. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  450. spin_lock(&sbi->s_es_lock);
  451. if (!list_empty(&ei->i_es_list)) {
  452. list_del_init(&ei->i_es_list);
  453. sbi->s_es_nr_inode--;
  454. WARN_ON_ONCE(sbi->s_es_nr_inode < 0);
  455. }
  456. spin_unlock(&sbi->s_es_lock);
  457. }
  458. static inline struct pending_reservation *__alloc_pending(bool nofail)
  459. {
  460. if (!nofail)
  461. return kmem_cache_alloc(ext4_pending_cachep, GFP_ATOMIC);
  462. return kmem_cache_zalloc(ext4_pending_cachep, GFP_KERNEL | __GFP_NOFAIL);
  463. }
  464. static inline void __free_pending(struct pending_reservation *pr)
  465. {
  466. kmem_cache_free(ext4_pending_cachep, pr);
  467. }
  468. /*
  469. * Returns true if we cannot fail to allocate memory for this extent_status
  470. * entry and cannot reclaim it until its status changes.
  471. */
  472. static inline bool ext4_es_must_keep(struct extent_status *es)
  473. {
  474. /* fiemap, bigalloc, and seek_data/hole need to use it. */
  475. if (ext4_es_is_delayed(es))
  476. return true;
  477. return false;
  478. }
  479. static inline struct extent_status *__es_alloc_extent(bool nofail)
  480. {
  481. if (!nofail)
  482. return kmem_cache_alloc(ext4_es_cachep, GFP_ATOMIC);
  483. return kmem_cache_zalloc(ext4_es_cachep, GFP_KERNEL | __GFP_NOFAIL);
  484. }
  485. static void ext4_es_init_extent(struct inode *inode, struct extent_status *es,
  486. ext4_lblk_t lblk, ext4_lblk_t len, ext4_fsblk_t pblk)
  487. {
  488. es->es_lblk = lblk;
  489. es->es_len = len;
  490. es->es_pblk = pblk;
  491. /* We never try to reclaim a must kept extent, so we don't count it. */
  492. if (!ext4_es_must_keep(es)) {
  493. if (!EXT4_I(inode)->i_es_shk_nr++)
  494. ext4_es_list_add(inode);
  495. percpu_counter_inc(&EXT4_SB(inode->i_sb)->
  496. s_es_stats.es_stats_shk_cnt);
  497. }
  498. EXT4_I(inode)->i_es_all_nr++;
  499. percpu_counter_inc(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
  500. }
  501. static inline void __es_free_extent(struct extent_status *es)
  502. {
  503. kmem_cache_free(ext4_es_cachep, es);
  504. }
  505. static void ext4_es_free_extent(struct inode *inode, struct extent_status *es)
  506. {
  507. EXT4_I(inode)->i_es_all_nr--;
  508. percpu_counter_dec(&EXT4_SB(inode->i_sb)->s_es_stats.es_stats_all_cnt);
  509. /* Decrease the shrink counter when we can reclaim the extent. */
  510. if (!ext4_es_must_keep(es)) {
  511. BUG_ON(EXT4_I(inode)->i_es_shk_nr == 0);
  512. if (!--EXT4_I(inode)->i_es_shk_nr)
  513. ext4_es_list_del(inode);
  514. percpu_counter_dec(&EXT4_SB(inode->i_sb)->
  515. s_es_stats.es_stats_shk_cnt);
  516. }
  517. __es_free_extent(es);
  518. }
  519. /*
  520. * Check whether or not two extents can be merged
  521. * Condition:
  522. * - logical block number is contiguous
  523. * - physical block number is contiguous
  524. * - status is equal
  525. */
  526. static int ext4_es_can_be_merged(struct extent_status *es1,
  527. struct extent_status *es2)
  528. {
  529. if (ext4_es_type(es1) != ext4_es_type(es2))
  530. return 0;
  531. if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
  532. pr_warn("ES assertion failed when merging extents. "
  533. "The sum of lengths of es1 (%d) and es2 (%d) "
  534. "is bigger than allowed file size (%d)\n",
  535. es1->es_len, es2->es_len, EXT_MAX_BLOCKS);
  536. WARN_ON(1);
  537. return 0;
  538. }
  539. if (((__u64) es1->es_lblk) + es1->es_len != es2->es_lblk)
  540. return 0;
  541. if ((ext4_es_is_written(es1) || ext4_es_is_unwritten(es1)) &&
  542. (ext4_es_pblock(es1) + es1->es_len == ext4_es_pblock(es2)))
  543. return 1;
  544. if (ext4_es_is_hole(es1))
  545. return 1;
  546. /* we need to check delayed extent */
  547. if (ext4_es_is_delayed(es1))
  548. return 1;
  549. return 0;
  550. }
  551. static struct extent_status *
  552. ext4_es_try_to_merge_left(struct inode *inode, struct extent_status *es)
  553. {
  554. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  555. struct extent_status *es1;
  556. struct rb_node *node;
  557. node = rb_prev(&es->rb_node);
  558. if (!node)
  559. return es;
  560. es1 = rb_entry(node, struct extent_status, rb_node);
  561. if (ext4_es_can_be_merged(es1, es)) {
  562. es1->es_len += es->es_len;
  563. if (ext4_es_is_referenced(es))
  564. ext4_es_set_referenced(es1);
  565. rb_erase(&es->rb_node, &tree->root);
  566. ext4_es_free_extent(inode, es);
  567. es = es1;
  568. }
  569. return es;
  570. }
  571. static struct extent_status *
  572. ext4_es_try_to_merge_right(struct inode *inode, struct extent_status *es)
  573. {
  574. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  575. struct extent_status *es1;
  576. struct rb_node *node;
  577. node = rb_next(&es->rb_node);
  578. if (!node)
  579. return es;
  580. es1 = rb_entry(node, struct extent_status, rb_node);
  581. if (ext4_es_can_be_merged(es, es1)) {
  582. es->es_len += es1->es_len;
  583. if (ext4_es_is_referenced(es1))
  584. ext4_es_set_referenced(es);
  585. rb_erase(node, &tree->root);
  586. ext4_es_free_extent(inode, es1);
  587. }
  588. return es;
  589. }
  590. #ifdef ES_AGGRESSIVE_TEST
  591. #include "ext4_extents.h" /* Needed when ES_AGGRESSIVE_TEST is defined */
  592. static void ext4_es_insert_extent_ext_check(struct inode *inode,
  593. struct extent_status *es)
  594. {
  595. struct ext4_ext_path *path = NULL;
  596. struct ext4_extent *ex;
  597. ext4_lblk_t ee_block;
  598. ext4_fsblk_t ee_start;
  599. unsigned short ee_len;
  600. int depth, ee_status, es_status;
  601. path = ext4_find_extent(inode, es->es_lblk, NULL, EXT4_EX_NOCACHE);
  602. if (IS_ERR(path))
  603. return;
  604. depth = ext_depth(inode);
  605. ex = path[depth].p_ext;
  606. if (ex) {
  607. ee_block = le32_to_cpu(ex->ee_block);
  608. ee_start = ext4_ext_pblock(ex);
  609. ee_len = ext4_ext_get_actual_len(ex);
  610. ee_status = ext4_ext_is_unwritten(ex) ? 1 : 0;
  611. es_status = ext4_es_is_unwritten(es) ? 1 : 0;
  612. /*
  613. * Make sure ex and es are not overlap when we try to insert
  614. * a delayed/hole extent.
  615. */
  616. if (!ext4_es_is_written(es) && !ext4_es_is_unwritten(es)) {
  617. if (in_range(es->es_lblk, ee_block, ee_len)) {
  618. pr_warn("ES insert assertion failed for "
  619. "inode: %lu we can find an extent "
  620. "at block [%d/%d/%llu/%c], but we "
  621. "want to add a delayed/hole extent "
  622. "[%d/%d/%llu/%x]\n",
  623. inode->i_ino, ee_block, ee_len,
  624. ee_start, ee_status ? 'u' : 'w',
  625. es->es_lblk, es->es_len,
  626. ext4_es_pblock(es), ext4_es_status(es));
  627. }
  628. goto out;
  629. }
  630. /*
  631. * We don't check ee_block == es->es_lblk, etc. because es
  632. * might be a part of whole extent, vice versa.
  633. */
  634. if (es->es_lblk < ee_block ||
  635. ext4_es_pblock(es) != ee_start + es->es_lblk - ee_block) {
  636. pr_warn("ES insert assertion failed for inode: %lu "
  637. "ex_status [%d/%d/%llu/%c] != "
  638. "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
  639. ee_block, ee_len, ee_start,
  640. ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
  641. ext4_es_pblock(es), es_status ? 'u' : 'w');
  642. goto out;
  643. }
  644. if (ee_status ^ es_status) {
  645. pr_warn("ES insert assertion failed for inode: %lu "
  646. "ex_status [%d/%d/%llu/%c] != "
  647. "es_status [%d/%d/%llu/%c]\n", inode->i_ino,
  648. ee_block, ee_len, ee_start,
  649. ee_status ? 'u' : 'w', es->es_lblk, es->es_len,
  650. ext4_es_pblock(es), es_status ? 'u' : 'w');
  651. }
  652. } else {
  653. /*
  654. * We can't find an extent on disk. So we need to make sure
  655. * that we don't want to add an written/unwritten extent.
  656. */
  657. if (!ext4_es_is_delayed(es) && !ext4_es_is_hole(es)) {
  658. pr_warn("ES insert assertion failed for inode: %lu "
  659. "can't find an extent at block %d but we want "
  660. "to add a written/unwritten extent "
  661. "[%d/%d/%llu/%x]\n", inode->i_ino,
  662. es->es_lblk, es->es_lblk, es->es_len,
  663. ext4_es_pblock(es), ext4_es_status(es));
  664. }
  665. }
  666. out:
  667. ext4_free_ext_path(path);
  668. }
  669. static void ext4_es_insert_extent_ind_check(struct inode *inode,
  670. struct extent_status *es)
  671. {
  672. struct ext4_map_blocks map;
  673. int retval;
  674. /*
  675. * Here we call ext4_ind_map_blocks to lookup a block mapping because
  676. * 'Indirect' structure is defined in indirect.c. So we couldn't
  677. * access direct/indirect tree from outside. It is too dirty to define
  678. * this function in indirect.c file.
  679. */
  680. map.m_lblk = es->es_lblk;
  681. map.m_len = es->es_len;
  682. retval = ext4_ind_map_blocks(NULL, inode, &map, 0);
  683. if (retval > 0) {
  684. if (ext4_es_is_delayed(es) || ext4_es_is_hole(es)) {
  685. /*
  686. * We want to add a delayed/hole extent but this
  687. * block has been allocated.
  688. */
  689. pr_warn("ES insert assertion failed for inode: %lu "
  690. "We can find blocks but we want to add a "
  691. "delayed/hole extent [%d/%d/%llu/%x]\n",
  692. inode->i_ino, es->es_lblk, es->es_len,
  693. ext4_es_pblock(es), ext4_es_status(es));
  694. return;
  695. } else if (ext4_es_is_written(es)) {
  696. if (retval != es->es_len) {
  697. pr_warn("ES insert assertion failed for "
  698. "inode: %lu retval %d != es_len %d\n",
  699. inode->i_ino, retval, es->es_len);
  700. return;
  701. }
  702. if (map.m_pblk != ext4_es_pblock(es)) {
  703. pr_warn("ES insert assertion failed for "
  704. "inode: %lu m_pblk %llu != "
  705. "es_pblk %llu\n",
  706. inode->i_ino, map.m_pblk,
  707. ext4_es_pblock(es));
  708. return;
  709. }
  710. } else {
  711. /*
  712. * We don't need to check unwritten extent because
  713. * indirect-based file doesn't have it.
  714. */
  715. BUG();
  716. }
  717. } else if (retval == 0) {
  718. if (ext4_es_is_written(es)) {
  719. pr_warn("ES insert assertion failed for inode: %lu "
  720. "We can't find the block but we want to add "
  721. "a written extent [%d/%d/%llu/%x]\n",
  722. inode->i_ino, es->es_lblk, es->es_len,
  723. ext4_es_pblock(es), ext4_es_status(es));
  724. return;
  725. }
  726. }
  727. }
  728. static inline void ext4_es_insert_extent_check(struct inode *inode,
  729. struct extent_status *es)
  730. {
  731. /*
  732. * We don't need to worry about the race condition because
  733. * caller takes i_data_sem locking.
  734. */
  735. BUG_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
  736. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  737. ext4_es_insert_extent_ext_check(inode, es);
  738. else
  739. ext4_es_insert_extent_ind_check(inode, es);
  740. }
  741. #else
  742. static inline void ext4_es_insert_extent_check(struct inode *inode,
  743. struct extent_status *es)
  744. {
  745. }
  746. #endif
  747. static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
  748. struct extent_status *prealloc)
  749. {
  750. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  751. struct rb_node **p = &tree->root.rb_node;
  752. struct rb_node *parent = NULL;
  753. struct extent_status *es;
  754. while (*p) {
  755. parent = *p;
  756. es = rb_entry(parent, struct extent_status, rb_node);
  757. if (newes->es_lblk < es->es_lblk) {
  758. if (ext4_es_can_be_merged(newes, es)) {
  759. /*
  760. * Here we can modify es_lblk directly
  761. * because it isn't overlapped.
  762. */
  763. es->es_lblk = newes->es_lblk;
  764. es->es_len += newes->es_len;
  765. if (ext4_es_is_written(es) ||
  766. ext4_es_is_unwritten(es))
  767. ext4_es_store_pblock(es,
  768. newes->es_pblk);
  769. es = ext4_es_try_to_merge_left(inode, es);
  770. goto out;
  771. }
  772. p = &(*p)->rb_left;
  773. } else if (newes->es_lblk > ext4_es_end(es)) {
  774. if (ext4_es_can_be_merged(es, newes)) {
  775. es->es_len += newes->es_len;
  776. es = ext4_es_try_to_merge_right(inode, es);
  777. goto out;
  778. }
  779. p = &(*p)->rb_right;
  780. } else {
  781. BUG();
  782. return -EINVAL;
  783. }
  784. }
  785. if (prealloc)
  786. es = prealloc;
  787. else
  788. es = __es_alloc_extent(false);
  789. if (!es)
  790. return -ENOMEM;
  791. ext4_es_init_extent(inode, es, newes->es_lblk, newes->es_len,
  792. newes->es_pblk);
  793. rb_link_node(&es->rb_node, parent, p);
  794. rb_insert_color(&es->rb_node, &tree->root);
  795. out:
  796. tree->cache_es = es;
  797. return 0;
  798. }
  799. /*
  800. * ext4_es_insert_extent() adds information to an inode's extent
  801. * status tree. This interface is used for modifying extents. To cache
  802. * on-disk extents, use ext4_es_cache_extent() instead.
  803. */
  804. void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
  805. ext4_lblk_t len, ext4_fsblk_t pblk,
  806. unsigned int status, bool delalloc_reserve_used)
  807. {
  808. struct extent_status newes;
  809. ext4_lblk_t end = lblk + len - 1;
  810. int err1 = 0, err2 = 0, err3 = 0;
  811. int resv_used = 0, pending = 0;
  812. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  813. struct extent_status *es1 = NULL;
  814. struct extent_status *es2 = NULL;
  815. struct pending_reservation *pr = NULL;
  816. bool revise_pending = false;
  817. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  818. return;
  819. es_debug("add [%u/%u) %llu %x %d to extent status tree of inode %lu\n",
  820. lblk, len, pblk, status, delalloc_reserve_used, inode->i_ino);
  821. if (!len)
  822. return;
  823. BUG_ON(end < lblk);
  824. WARN_ON_ONCE(status & EXTENT_STATUS_DELAYED);
  825. newes.es_lblk = lblk;
  826. newes.es_len = len;
  827. ext4_es_store_pblock_status(&newes, pblk, status);
  828. ext4_es_insert_extent_check(inode, &newes);
  829. revise_pending = sbi->s_cluster_ratio > 1 &&
  830. test_opt(inode->i_sb, DELALLOC) &&
  831. (status & (EXTENT_STATUS_WRITTEN |
  832. EXTENT_STATUS_UNWRITTEN));
  833. retry:
  834. if (err1 && !es1)
  835. es1 = __es_alloc_extent(true);
  836. if ((err1 || err2) && !es2)
  837. es2 = __es_alloc_extent(true);
  838. if ((err1 || err2 || err3 < 0) && revise_pending && !pr)
  839. pr = __alloc_pending(true);
  840. write_lock(&EXT4_I(inode)->i_es_lock);
  841. err1 = __es_remove_extent(inode, lblk, end, 0, &resv_used, NULL, es1);
  842. if (err1 != 0)
  843. goto error;
  844. /* Free preallocated extent if it didn't get used. */
  845. if (es1) {
  846. if (!es1->es_len)
  847. __es_free_extent(es1);
  848. es1 = NULL;
  849. }
  850. err2 = __es_insert_extent(inode, &newes, es2);
  851. if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
  852. err2 = 0;
  853. if (err2 != 0)
  854. goto error;
  855. /* Free preallocated extent if it didn't get used. */
  856. if (es2) {
  857. if (!es2->es_len)
  858. __es_free_extent(es2);
  859. es2 = NULL;
  860. }
  861. if (revise_pending) {
  862. err3 = __revise_pending(inode, lblk, len, &pr);
  863. if (err3 < 0)
  864. goto error;
  865. if (pr) {
  866. __free_pending(pr);
  867. pr = NULL;
  868. }
  869. pending = err3;
  870. }
  871. ext4_es_inc_seq(inode);
  872. error:
  873. write_unlock(&EXT4_I(inode)->i_es_lock);
  874. /*
  875. * Reduce the reserved cluster count to reflect successful deferred
  876. * allocation of delayed allocated clusters or direct allocation of
  877. * clusters discovered to be delayed allocated. Once allocated, a
  878. * cluster is not included in the reserved count.
  879. *
  880. * When direct allocating (from fallocate, filemap, DIO, or clusters
  881. * allocated when delalloc has been disabled by ext4_nonda_switch())
  882. * an extent either 1) contains delayed blocks but start with
  883. * non-delayed allocated blocks (e.g. hole) or 2) contains non-delayed
  884. * allocated blocks which belong to delayed allocated clusters when
  885. * bigalloc feature is enabled, quota has already been claimed by
  886. * ext4_mb_new_blocks(), so release the quota reservations made for
  887. * any previously delayed allocated clusters instead of claim them
  888. * again.
  889. */
  890. resv_used += pending;
  891. if (resv_used)
  892. ext4_da_update_reserve_space(inode, resv_used,
  893. delalloc_reserve_used);
  894. if (err1 || err2 || err3 < 0)
  895. goto retry;
  896. trace_ext4_es_insert_extent(inode, &newes);
  897. ext4_es_print_tree(inode);
  898. return;
  899. }
  900. /*
  901. * ext4_es_cache_extent() inserts information into the extent status tree
  902. * only if there is no existing information about the specified range or
  903. * if the existing extents have the same status.
  904. *
  905. * Note that this interface is only used for caching on-disk extent
  906. * information and cannot be used to convert existing extents in the extent
  907. * status tree. To convert existing extents, use ext4_es_insert_extent()
  908. * instead.
  909. */
  910. void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
  911. ext4_lblk_t len, ext4_fsblk_t pblk,
  912. unsigned int status)
  913. {
  914. struct extent_status *es;
  915. struct extent_status chkes, newes;
  916. ext4_lblk_t end = lblk + len - 1;
  917. bool conflict = false;
  918. int err;
  919. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  920. return;
  921. newes.es_lblk = lblk;
  922. newes.es_len = len;
  923. ext4_es_store_pblock_status(&newes, pblk, status);
  924. if (!len)
  925. return;
  926. BUG_ON(end < lblk);
  927. write_lock(&EXT4_I(inode)->i_es_lock);
  928. es = __es_tree_search(&EXT4_I(inode)->i_es_tree.root, lblk);
  929. if (es && es->es_lblk <= end) {
  930. /* Found an extent that covers the entire range. */
  931. if (es->es_lblk <= lblk && es->es_lblk + es->es_len > end) {
  932. if (__es_check_extent_status(es, status, &chkes))
  933. conflict = true;
  934. goto unlock;
  935. }
  936. /* Check and remove all extents in range. */
  937. err = __es_remove_extent(inode, lblk, end, status, NULL,
  938. &chkes, NULL);
  939. if (err) {
  940. if (err == -EINVAL)
  941. conflict = true;
  942. goto unlock;
  943. }
  944. }
  945. __es_insert_extent(inode, &newes, NULL);
  946. trace_ext4_es_cache_extent(inode, &newes);
  947. ext4_es_print_tree(inode);
  948. unlock:
  949. write_unlock(&EXT4_I(inode)->i_es_lock);
  950. if (!conflict)
  951. return;
  952. /*
  953. * A hole in the on-disk extent but a delayed extent in the extent
  954. * status tree, is allowed.
  955. */
  956. if (status == EXTENT_STATUS_HOLE &&
  957. ext4_es_type(&chkes) == EXTENT_STATUS_DELAYED)
  958. return;
  959. ext4_warning_inode(inode,
  960. "ES cache extent failed: add [%d,%d,%llu,0x%x] conflict with existing [%d,%d,%llu,0x%x]\n",
  961. lblk, len, pblk, status, chkes.es_lblk, chkes.es_len,
  962. ext4_es_pblock(&chkes), ext4_es_status(&chkes));
  963. }
  964. /*
  965. * ext4_es_lookup_extent() looks up an extent in extent status tree.
  966. *
  967. * ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
  968. *
  969. * Return: 1 on found, 0 on not
  970. */
  971. int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
  972. ext4_lblk_t *next_lblk, struct extent_status *es,
  973. u64 *pseq)
  974. {
  975. struct ext4_es_tree *tree;
  976. struct ext4_es_stats *stats;
  977. struct extent_status *es1 = NULL;
  978. struct rb_node *node;
  979. int found = 0;
  980. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  981. return 0;
  982. trace_ext4_es_lookup_extent_enter(inode, lblk);
  983. es_debug("lookup extent in block %u\n", lblk);
  984. tree = &EXT4_I(inode)->i_es_tree;
  985. read_lock(&EXT4_I(inode)->i_es_lock);
  986. /* find extent in cache firstly */
  987. es->es_lblk = es->es_len = es->es_pblk = 0;
  988. es1 = READ_ONCE(tree->cache_es);
  989. if (es1 && in_range(lblk, es1->es_lblk, es1->es_len)) {
  990. es_debug("%u cached by [%u/%u)\n",
  991. lblk, es1->es_lblk, es1->es_len);
  992. found = 1;
  993. goto out;
  994. }
  995. node = tree->root.rb_node;
  996. while (node) {
  997. es1 = rb_entry(node, struct extent_status, rb_node);
  998. if (lblk < es1->es_lblk)
  999. node = node->rb_left;
  1000. else if (lblk > ext4_es_end(es1))
  1001. node = node->rb_right;
  1002. else {
  1003. found = 1;
  1004. break;
  1005. }
  1006. }
  1007. out:
  1008. stats = &EXT4_SB(inode->i_sb)->s_es_stats;
  1009. if (found) {
  1010. BUG_ON(!es1);
  1011. es->es_lblk = es1->es_lblk;
  1012. es->es_len = es1->es_len;
  1013. es->es_pblk = es1->es_pblk;
  1014. if (!ext4_es_is_referenced(es1))
  1015. ext4_es_set_referenced(es1);
  1016. percpu_counter_inc(&stats->es_stats_cache_hits);
  1017. if (next_lblk) {
  1018. node = rb_next(&es1->rb_node);
  1019. if (node) {
  1020. es1 = rb_entry(node, struct extent_status,
  1021. rb_node);
  1022. *next_lblk = es1->es_lblk;
  1023. } else
  1024. *next_lblk = 0;
  1025. }
  1026. if (pseq)
  1027. *pseq = EXT4_I(inode)->i_es_seq;
  1028. } else {
  1029. percpu_counter_inc(&stats->es_stats_cache_misses);
  1030. }
  1031. read_unlock(&EXT4_I(inode)->i_es_lock);
  1032. trace_ext4_es_lookup_extent_exit(inode, es, found);
  1033. return found;
  1034. }
  1035. struct rsvd_count {
  1036. int ndelayed;
  1037. bool first_do_lblk_found;
  1038. ext4_lblk_t first_do_lblk;
  1039. ext4_lblk_t last_do_lblk;
  1040. struct extent_status *left_es;
  1041. bool partial;
  1042. ext4_lblk_t lclu;
  1043. };
  1044. /*
  1045. * init_rsvd - initialize reserved count data before removing block range
  1046. * in file from extent status tree
  1047. *
  1048. * @inode - file containing range
  1049. * @lblk - first block in range
  1050. * @es - pointer to first extent in range
  1051. * @rc - pointer to reserved count data
  1052. *
  1053. * Assumes es is not NULL
  1054. */
  1055. static void init_rsvd(struct inode *inode, ext4_lblk_t lblk,
  1056. struct extent_status *es, struct rsvd_count *rc)
  1057. {
  1058. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1059. struct rb_node *node;
  1060. rc->ndelayed = 0;
  1061. /*
  1062. * for bigalloc, note the first delayed block in the range has not
  1063. * been found, record the extent containing the block to the left of
  1064. * the region to be removed, if any, and note that there's no partial
  1065. * cluster to track
  1066. */
  1067. if (sbi->s_cluster_ratio > 1) {
  1068. rc->first_do_lblk_found = false;
  1069. if (lblk > es->es_lblk) {
  1070. rc->left_es = es;
  1071. } else {
  1072. node = rb_prev(&es->rb_node);
  1073. rc->left_es = node ? rb_entry(node,
  1074. struct extent_status,
  1075. rb_node) : NULL;
  1076. }
  1077. rc->partial = false;
  1078. }
  1079. }
  1080. /*
  1081. * count_rsvd - count the clusters containing delayed blocks in a range
  1082. * within an extent and add to the running tally in rsvd_count
  1083. *
  1084. * @inode - file containing extent
  1085. * @lblk - first block in range
  1086. * @len - length of range in blocks
  1087. * @es - pointer to extent containing clusters to be counted
  1088. * @rc - pointer to reserved count data
  1089. *
  1090. * Tracks partial clusters found at the beginning and end of extents so
  1091. * they aren't overcounted when they span adjacent extents
  1092. */
  1093. static void count_rsvd(struct inode *inode, ext4_lblk_t lblk, long len,
  1094. struct extent_status *es, struct rsvd_count *rc)
  1095. {
  1096. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1097. ext4_lblk_t i, end, nclu;
  1098. if (!ext4_es_is_delayed(es))
  1099. return;
  1100. WARN_ON(len <= 0);
  1101. if (sbi->s_cluster_ratio == 1) {
  1102. rc->ndelayed += (int) len;
  1103. return;
  1104. }
  1105. /* bigalloc */
  1106. i = (lblk < es->es_lblk) ? es->es_lblk : lblk;
  1107. end = lblk + (ext4_lblk_t) len - 1;
  1108. end = (end > ext4_es_end(es)) ? ext4_es_end(es) : end;
  1109. /* record the first block of the first delayed extent seen */
  1110. if (!rc->first_do_lblk_found) {
  1111. rc->first_do_lblk = i;
  1112. rc->first_do_lblk_found = true;
  1113. }
  1114. /* update the last lblk in the region seen so far */
  1115. rc->last_do_lblk = end;
  1116. /*
  1117. * if we're tracking a partial cluster and the current extent
  1118. * doesn't start with it, count it and stop tracking
  1119. */
  1120. if (rc->partial && (rc->lclu != EXT4_B2C(sbi, i))) {
  1121. rc->ndelayed++;
  1122. rc->partial = false;
  1123. }
  1124. /*
  1125. * if the first cluster doesn't start on a cluster boundary but
  1126. * ends on one, count it
  1127. */
  1128. if (EXT4_LBLK_COFF(sbi, i) != 0) {
  1129. if (end >= EXT4_LBLK_CFILL(sbi, i)) {
  1130. rc->ndelayed++;
  1131. rc->partial = false;
  1132. i = EXT4_LBLK_CFILL(sbi, i) + 1;
  1133. }
  1134. }
  1135. /*
  1136. * if the current cluster starts on a cluster boundary, count the
  1137. * number of whole delayed clusters in the extent
  1138. */
  1139. if ((i + sbi->s_cluster_ratio - 1) <= end) {
  1140. nclu = (end - i + 1) >> sbi->s_cluster_bits;
  1141. rc->ndelayed += nclu;
  1142. i += nclu << sbi->s_cluster_bits;
  1143. }
  1144. /*
  1145. * start tracking a partial cluster if there's a partial at the end
  1146. * of the current extent and we're not already tracking one
  1147. */
  1148. if (!rc->partial && i <= end) {
  1149. rc->partial = true;
  1150. rc->lclu = EXT4_B2C(sbi, i);
  1151. }
  1152. }
  1153. /*
  1154. * __pr_tree_search - search for a pending cluster reservation
  1155. *
  1156. * @root - root of pending reservation tree
  1157. * @lclu - logical cluster to search for
  1158. *
  1159. * Returns the pending reservation for the cluster identified by @lclu
  1160. * if found. If not, returns a reservation for the next cluster if any,
  1161. * and if not, returns NULL.
  1162. */
  1163. static struct pending_reservation *__pr_tree_search(struct rb_root *root,
  1164. ext4_lblk_t lclu)
  1165. {
  1166. struct rb_node *node = root->rb_node;
  1167. struct pending_reservation *pr = NULL;
  1168. while (node) {
  1169. pr = rb_entry(node, struct pending_reservation, rb_node);
  1170. if (lclu < pr->lclu)
  1171. node = node->rb_left;
  1172. else if (lclu > pr->lclu)
  1173. node = node->rb_right;
  1174. else
  1175. return pr;
  1176. }
  1177. if (pr && lclu < pr->lclu)
  1178. return pr;
  1179. if (pr && lclu > pr->lclu) {
  1180. node = rb_next(&pr->rb_node);
  1181. return node ? rb_entry(node, struct pending_reservation,
  1182. rb_node) : NULL;
  1183. }
  1184. return NULL;
  1185. }
  1186. /*
  1187. * get_rsvd - calculates and returns the number of cluster reservations to be
  1188. * released when removing a block range from the extent status tree
  1189. * and releases any pending reservations within the range
  1190. *
  1191. * @inode - file containing block range
  1192. * @end - last block in range
  1193. * @right_es - pointer to extent containing next block beyond end or NULL
  1194. * @rc - pointer to reserved count data
  1195. *
  1196. * The number of reservations to be released is equal to the number of
  1197. * clusters containing delayed blocks within the range, minus the number of
  1198. * clusters still containing delayed blocks at the ends of the range, and
  1199. * minus the number of pending reservations within the range.
  1200. */
  1201. static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
  1202. struct extent_status *right_es,
  1203. struct rsvd_count *rc)
  1204. {
  1205. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1206. struct pending_reservation *pr;
  1207. struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
  1208. struct rb_node *node;
  1209. ext4_lblk_t first_lclu, last_lclu;
  1210. bool left_delayed, right_delayed, count_pending;
  1211. struct extent_status *es;
  1212. if (sbi->s_cluster_ratio > 1) {
  1213. /* count any remaining partial cluster */
  1214. if (rc->partial)
  1215. rc->ndelayed++;
  1216. if (rc->ndelayed == 0)
  1217. return 0;
  1218. first_lclu = EXT4_B2C(sbi, rc->first_do_lblk);
  1219. last_lclu = EXT4_B2C(sbi, rc->last_do_lblk);
  1220. /*
  1221. * decrease the delayed count by the number of clusters at the
  1222. * ends of the range that still contain delayed blocks -
  1223. * these clusters still need to be reserved
  1224. */
  1225. left_delayed = right_delayed = false;
  1226. es = rc->left_es;
  1227. while (es && ext4_es_end(es) >=
  1228. EXT4_LBLK_CMASK(sbi, rc->first_do_lblk)) {
  1229. if (ext4_es_is_delayed(es)) {
  1230. rc->ndelayed--;
  1231. left_delayed = true;
  1232. break;
  1233. }
  1234. node = rb_prev(&es->rb_node);
  1235. if (!node)
  1236. break;
  1237. es = rb_entry(node, struct extent_status, rb_node);
  1238. }
  1239. if (right_es && (!left_delayed || first_lclu != last_lclu)) {
  1240. if (end < ext4_es_end(right_es)) {
  1241. es = right_es;
  1242. } else {
  1243. node = rb_next(&right_es->rb_node);
  1244. es = node ? rb_entry(node, struct extent_status,
  1245. rb_node) : NULL;
  1246. }
  1247. while (es && es->es_lblk <=
  1248. EXT4_LBLK_CFILL(sbi, rc->last_do_lblk)) {
  1249. if (ext4_es_is_delayed(es)) {
  1250. rc->ndelayed--;
  1251. right_delayed = true;
  1252. break;
  1253. }
  1254. node = rb_next(&es->rb_node);
  1255. if (!node)
  1256. break;
  1257. es = rb_entry(node, struct extent_status,
  1258. rb_node);
  1259. }
  1260. }
  1261. /*
  1262. * Determine the block range that should be searched for
  1263. * pending reservations, if any. Clusters on the ends of the
  1264. * original removed range containing delayed blocks are
  1265. * excluded. They've already been accounted for and it's not
  1266. * possible to determine if an associated pending reservation
  1267. * should be released with the information available in the
  1268. * extents status tree.
  1269. */
  1270. if (first_lclu == last_lclu) {
  1271. if (left_delayed | right_delayed)
  1272. count_pending = false;
  1273. else
  1274. count_pending = true;
  1275. } else {
  1276. if (left_delayed)
  1277. first_lclu++;
  1278. if (right_delayed)
  1279. last_lclu--;
  1280. if (first_lclu <= last_lclu)
  1281. count_pending = true;
  1282. else
  1283. count_pending = false;
  1284. }
  1285. /*
  1286. * a pending reservation found between first_lclu and last_lclu
  1287. * represents an allocated cluster that contained at least one
  1288. * delayed block, so the delayed total must be reduced by one
  1289. * for each pending reservation found and released
  1290. */
  1291. if (count_pending) {
  1292. pr = __pr_tree_search(&tree->root, first_lclu);
  1293. while (pr && pr->lclu <= last_lclu) {
  1294. rc->ndelayed--;
  1295. node = rb_next(&pr->rb_node);
  1296. rb_erase(&pr->rb_node, &tree->root);
  1297. __free_pending(pr);
  1298. if (!node)
  1299. break;
  1300. pr = rb_entry(node, struct pending_reservation,
  1301. rb_node);
  1302. }
  1303. }
  1304. }
  1305. return rc->ndelayed;
  1306. }
  1307. /*
  1308. * __es_remove_extent - removes block range from extent status tree
  1309. *
  1310. * @inode - file containing range
  1311. * @lblk - first block in range
  1312. * @end - last block in range
  1313. * @status - the extent status to be checked
  1314. * @reserved - number of cluster reservations released
  1315. * @res - return the extent if the status is not match
  1316. * @prealloc - pre-allocated es to avoid memory allocation failures
  1317. *
  1318. * If @reserved is not NULL and delayed allocation is enabled, counts
  1319. * block/cluster reservations freed by removing range and if bigalloc
  1320. * enabled cancels pending reservations as needed. If @status is not
  1321. * zero, check extent status type while removing extent, return -EINVAL
  1322. * and pass out the extent through @res if not match. Returns 0 on
  1323. * success, error code on failure.
  1324. */
  1325. static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  1326. ext4_lblk_t end, unsigned int status,
  1327. int *reserved, struct extent_status *res,
  1328. struct extent_status *prealloc)
  1329. {
  1330. struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
  1331. struct rb_node *node;
  1332. struct extent_status *es;
  1333. struct extent_status orig_es;
  1334. ext4_lblk_t len1, len2;
  1335. ext4_fsblk_t block;
  1336. int err;
  1337. bool count_reserved = true;
  1338. struct rsvd_count rc;
  1339. if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
  1340. count_reserved = false;
  1341. if (status == 0)
  1342. status = ES_TYPE_MASK;
  1343. es = __es_tree_search(&tree->root, lblk);
  1344. if (!es)
  1345. return 0;
  1346. if (es->es_lblk > end)
  1347. return 0;
  1348. err = __es_check_extent_status(es, status, res);
  1349. if (err)
  1350. return err;
  1351. /* Simply invalidate cache_es. */
  1352. tree->cache_es = NULL;
  1353. if (count_reserved)
  1354. init_rsvd(inode, lblk, es, &rc);
  1355. orig_es.es_lblk = es->es_lblk;
  1356. orig_es.es_len = es->es_len;
  1357. orig_es.es_pblk = es->es_pblk;
  1358. len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
  1359. len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
  1360. if (len1 > 0)
  1361. es->es_len = len1;
  1362. if (len2 > 0) {
  1363. if (len1 > 0) {
  1364. struct extent_status newes;
  1365. newes.es_lblk = end + 1;
  1366. newes.es_len = len2;
  1367. block = 0x7FDEADBEEFULL;
  1368. if (ext4_es_is_written(&orig_es) ||
  1369. ext4_es_is_unwritten(&orig_es))
  1370. block = ext4_es_pblock(&orig_es) +
  1371. orig_es.es_len - len2;
  1372. ext4_es_store_pblock_status(&newes, block,
  1373. ext4_es_status(&orig_es));
  1374. err = __es_insert_extent(inode, &newes, prealloc);
  1375. if (err) {
  1376. if (!ext4_es_must_keep(&newes))
  1377. return 0;
  1378. es->es_lblk = orig_es.es_lblk;
  1379. es->es_len = orig_es.es_len;
  1380. return err;
  1381. }
  1382. } else {
  1383. es->es_lblk = end + 1;
  1384. es->es_len = len2;
  1385. if (ext4_es_is_written(es) ||
  1386. ext4_es_is_unwritten(es)) {
  1387. block = orig_es.es_pblk + orig_es.es_len - len2;
  1388. ext4_es_store_pblock(es, block);
  1389. }
  1390. }
  1391. if (count_reserved)
  1392. count_rsvd(inode, orig_es.es_lblk + len1,
  1393. orig_es.es_len - len1 - len2, &orig_es, &rc);
  1394. goto out;
  1395. }
  1396. if (len1 > 0) {
  1397. if (count_reserved)
  1398. count_rsvd(inode, lblk, orig_es.es_len - len1,
  1399. &orig_es, &rc);
  1400. node = rb_next(&es->rb_node);
  1401. if (node)
  1402. es = rb_entry(node, struct extent_status, rb_node);
  1403. else
  1404. es = NULL;
  1405. }
  1406. while (es && ext4_es_end(es) <= end) {
  1407. err = __es_check_extent_status(es, status, res);
  1408. if (err)
  1409. return err;
  1410. if (count_reserved)
  1411. count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
  1412. node = rb_next(&es->rb_node);
  1413. rb_erase(&es->rb_node, &tree->root);
  1414. ext4_es_free_extent(inode, es);
  1415. if (!node) {
  1416. es = NULL;
  1417. break;
  1418. }
  1419. es = rb_entry(node, struct extent_status, rb_node);
  1420. }
  1421. if (es && es->es_lblk < end + 1) {
  1422. ext4_lblk_t orig_len = es->es_len;
  1423. err = __es_check_extent_status(es, status, res);
  1424. if (err)
  1425. return err;
  1426. len1 = ext4_es_end(es) - end;
  1427. if (count_reserved)
  1428. count_rsvd(inode, es->es_lblk, orig_len - len1,
  1429. es, &rc);
  1430. es->es_lblk = end + 1;
  1431. es->es_len = len1;
  1432. if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
  1433. block = es->es_pblk + orig_len - len1;
  1434. ext4_es_store_pblock(es, block);
  1435. }
  1436. }
  1437. out:
  1438. if (count_reserved)
  1439. *reserved = get_rsvd(inode, end, es, &rc);
  1440. return 0;
  1441. }
  1442. /*
  1443. * ext4_es_remove_extent - removes block range from extent status tree
  1444. *
  1445. * @inode - file containing range
  1446. * @lblk - first block in range
  1447. * @len - number of blocks to remove
  1448. *
  1449. * Reduces block/cluster reservation count and for bigalloc cancels pending
  1450. * reservations as needed.
  1451. */
  1452. void ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
  1453. ext4_lblk_t len)
  1454. {
  1455. ext4_lblk_t end;
  1456. int err = 0;
  1457. int reserved = 0;
  1458. struct extent_status *es = NULL;
  1459. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  1460. return;
  1461. es_debug("remove [%u/%u) from extent status tree of inode %lu\n",
  1462. lblk, len, inode->i_ino);
  1463. if (!len)
  1464. return;
  1465. end = lblk + len - 1;
  1466. BUG_ON(end < lblk);
  1467. retry:
  1468. if (err && !es)
  1469. es = __es_alloc_extent(true);
  1470. /*
  1471. * ext4_clear_inode() depends on us taking i_es_lock unconditionally
  1472. * so that we are sure __es_shrink() is done with the inode before it
  1473. * is reclaimed.
  1474. */
  1475. write_lock(&EXT4_I(inode)->i_es_lock);
  1476. err = __es_remove_extent(inode, lblk, end, 0, &reserved, NULL, es);
  1477. if (err)
  1478. goto error;
  1479. /* Free preallocated extent if it didn't get used. */
  1480. if (es) {
  1481. if (!es->es_len)
  1482. __es_free_extent(es);
  1483. es = NULL;
  1484. }
  1485. ext4_es_inc_seq(inode);
  1486. error:
  1487. write_unlock(&EXT4_I(inode)->i_es_lock);
  1488. if (err)
  1489. goto retry;
  1490. trace_ext4_es_remove_extent(inode, lblk, len);
  1491. ext4_es_print_tree(inode);
  1492. ext4_da_release_space(inode, reserved);
  1493. }
  1494. static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
  1495. struct ext4_inode_info *locked_ei)
  1496. {
  1497. struct ext4_inode_info *ei;
  1498. struct ext4_es_stats *es_stats;
  1499. ktime_t start_time;
  1500. u64 scan_time;
  1501. int nr_to_walk;
  1502. int nr_shrunk = 0;
  1503. int retried = 0, nr_skipped = 0;
  1504. es_stats = &sbi->s_es_stats;
  1505. start_time = ktime_get();
  1506. retry:
  1507. spin_lock(&sbi->s_es_lock);
  1508. nr_to_walk = sbi->s_es_nr_inode;
  1509. while (nr_to_walk-- > 0) {
  1510. if (list_empty(&sbi->s_es_list)) {
  1511. spin_unlock(&sbi->s_es_lock);
  1512. goto out;
  1513. }
  1514. ei = list_first_entry(&sbi->s_es_list, struct ext4_inode_info,
  1515. i_es_list);
  1516. /* Move the inode to the tail */
  1517. list_move_tail(&ei->i_es_list, &sbi->s_es_list);
  1518. /*
  1519. * Normally we try hard to avoid shrinking precached inodes,
  1520. * but we will as a last resort.
  1521. */
  1522. if (!retried && ext4_test_inode_state(&ei->vfs_inode,
  1523. EXT4_STATE_EXT_PRECACHED)) {
  1524. nr_skipped++;
  1525. continue;
  1526. }
  1527. if (ei == locked_ei || !write_trylock(&ei->i_es_lock)) {
  1528. nr_skipped++;
  1529. continue;
  1530. }
  1531. /*
  1532. * Now we hold i_es_lock which protects us from inode reclaim
  1533. * freeing inode under us
  1534. */
  1535. spin_unlock(&sbi->s_es_lock);
  1536. nr_shrunk += es_reclaim_extents(ei, &nr_to_scan);
  1537. write_unlock(&ei->i_es_lock);
  1538. if (nr_to_scan <= 0)
  1539. goto out;
  1540. spin_lock(&sbi->s_es_lock);
  1541. }
  1542. spin_unlock(&sbi->s_es_lock);
  1543. /*
  1544. * If we skipped any inodes, and we weren't able to make any
  1545. * forward progress, try again to scan precached inodes.
  1546. */
  1547. if ((nr_shrunk == 0) && nr_skipped && !retried) {
  1548. retried++;
  1549. goto retry;
  1550. }
  1551. if (locked_ei && nr_shrunk == 0)
  1552. nr_shrunk = es_reclaim_extents(locked_ei, &nr_to_scan);
  1553. out:
  1554. scan_time = ktime_to_ns(ktime_sub(ktime_get(), start_time));
  1555. if (likely(es_stats->es_stats_scan_time))
  1556. es_stats->es_stats_scan_time = (scan_time +
  1557. es_stats->es_stats_scan_time*3) / 4;
  1558. else
  1559. es_stats->es_stats_scan_time = scan_time;
  1560. if (scan_time > es_stats->es_stats_max_scan_time)
  1561. es_stats->es_stats_max_scan_time = scan_time;
  1562. if (likely(es_stats->es_stats_shrunk))
  1563. es_stats->es_stats_shrunk = (nr_shrunk +
  1564. es_stats->es_stats_shrunk*3) / 4;
  1565. else
  1566. es_stats->es_stats_shrunk = nr_shrunk;
  1567. trace_ext4_es_shrink(sbi->s_sb, nr_shrunk, scan_time,
  1568. nr_skipped, retried);
  1569. return nr_shrunk;
  1570. }
  1571. static unsigned long ext4_es_count(struct shrinker *shrink,
  1572. struct shrink_control *sc)
  1573. {
  1574. unsigned long nr;
  1575. struct ext4_sb_info *sbi;
  1576. sbi = shrink->private_data;
  1577. nr = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
  1578. trace_ext4_es_shrink_count(sbi->s_sb, sc->nr_to_scan, nr);
  1579. return nr;
  1580. }
  1581. static unsigned long ext4_es_scan(struct shrinker *shrink,
  1582. struct shrink_control *sc)
  1583. {
  1584. struct ext4_sb_info *sbi = shrink->private_data;
  1585. int nr_to_scan = sc->nr_to_scan;
  1586. int ret, nr_shrunk;
  1587. ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
  1588. trace_ext4_es_shrink_scan_enter(sbi->s_sb, nr_to_scan, ret);
  1589. nr_shrunk = __es_shrink(sbi, nr_to_scan, NULL);
  1590. ret = percpu_counter_read_positive(&sbi->s_es_stats.es_stats_shk_cnt);
  1591. trace_ext4_es_shrink_scan_exit(sbi->s_sb, nr_shrunk, ret);
  1592. return nr_shrunk;
  1593. }
  1594. int ext4_seq_es_shrinker_info_show(struct seq_file *seq, void *v)
  1595. {
  1596. struct ext4_sb_info *sbi = EXT4_SB((struct super_block *) seq->private);
  1597. struct ext4_es_stats *es_stats = &sbi->s_es_stats;
  1598. struct ext4_inode_info *ei, *max = NULL;
  1599. unsigned int inode_cnt = 0;
  1600. if (v != SEQ_START_TOKEN)
  1601. return 0;
  1602. /* here we just find an inode that has the max nr. of objects */
  1603. spin_lock(&sbi->s_es_lock);
  1604. list_for_each_entry(ei, &sbi->s_es_list, i_es_list) {
  1605. inode_cnt++;
  1606. if (max && max->i_es_all_nr < ei->i_es_all_nr)
  1607. max = ei;
  1608. else if (!max)
  1609. max = ei;
  1610. }
  1611. spin_unlock(&sbi->s_es_lock);
  1612. seq_printf(seq, "stats:\n %lld objects\n %lld reclaimable objects\n",
  1613. percpu_counter_sum_positive(&es_stats->es_stats_all_cnt),
  1614. percpu_counter_sum_positive(&es_stats->es_stats_shk_cnt));
  1615. seq_printf(seq, " %lld/%lld cache hits/misses\n",
  1616. percpu_counter_sum_positive(&es_stats->es_stats_cache_hits),
  1617. percpu_counter_sum_positive(&es_stats->es_stats_cache_misses));
  1618. if (inode_cnt)
  1619. seq_printf(seq, " %d inodes on list\n", inode_cnt);
  1620. seq_printf(seq, "average:\n %llu us scan time\n",
  1621. div_u64(es_stats->es_stats_scan_time, 1000));
  1622. seq_printf(seq, " %lu shrunk objects\n", es_stats->es_stats_shrunk);
  1623. if (inode_cnt)
  1624. seq_printf(seq,
  1625. "maximum:\n %lu inode (%u objects, %u reclaimable)\n"
  1626. " %llu us max scan time\n",
  1627. max->vfs_inode.i_ino, max->i_es_all_nr, max->i_es_shk_nr,
  1628. div_u64(es_stats->es_stats_max_scan_time, 1000));
  1629. return 0;
  1630. }
  1631. int ext4_es_register_shrinker(struct ext4_sb_info *sbi)
  1632. {
  1633. int err;
  1634. /* Make sure we have enough bits for physical block number */
  1635. BUILD_BUG_ON(ES_SHIFT < 48);
  1636. INIT_LIST_HEAD(&sbi->s_es_list);
  1637. sbi->s_es_nr_inode = 0;
  1638. spin_lock_init(&sbi->s_es_lock);
  1639. sbi->s_es_stats.es_stats_shrunk = 0;
  1640. err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_hits, 0,
  1641. GFP_KERNEL);
  1642. if (err)
  1643. return err;
  1644. err = percpu_counter_init(&sbi->s_es_stats.es_stats_cache_misses, 0,
  1645. GFP_KERNEL);
  1646. if (err)
  1647. goto err1;
  1648. sbi->s_es_stats.es_stats_scan_time = 0;
  1649. sbi->s_es_stats.es_stats_max_scan_time = 0;
  1650. err = percpu_counter_init(&sbi->s_es_stats.es_stats_all_cnt, 0, GFP_KERNEL);
  1651. if (err)
  1652. goto err2;
  1653. err = percpu_counter_init(&sbi->s_es_stats.es_stats_shk_cnt, 0, GFP_KERNEL);
  1654. if (err)
  1655. goto err3;
  1656. sbi->s_es_shrinker = shrinker_alloc(0, "ext4-es:%s", sbi->s_sb->s_id);
  1657. if (!sbi->s_es_shrinker) {
  1658. err = -ENOMEM;
  1659. goto err4;
  1660. }
  1661. sbi->s_es_shrinker->scan_objects = ext4_es_scan;
  1662. sbi->s_es_shrinker->count_objects = ext4_es_count;
  1663. sbi->s_es_shrinker->private_data = sbi;
  1664. shrinker_register(sbi->s_es_shrinker);
  1665. return 0;
  1666. err4:
  1667. percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
  1668. err3:
  1669. percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
  1670. err2:
  1671. percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
  1672. err1:
  1673. percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
  1674. return err;
  1675. }
  1676. void ext4_es_unregister_shrinker(struct ext4_sb_info *sbi)
  1677. {
  1678. percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_hits);
  1679. percpu_counter_destroy(&sbi->s_es_stats.es_stats_cache_misses);
  1680. percpu_counter_destroy(&sbi->s_es_stats.es_stats_all_cnt);
  1681. percpu_counter_destroy(&sbi->s_es_stats.es_stats_shk_cnt);
  1682. shrinker_free(sbi->s_es_shrinker);
  1683. }
  1684. /*
  1685. * Shrink extents in given inode from ei->i_es_shrink_lblk till end. Scan at
  1686. * most *nr_to_scan extents, update *nr_to_scan accordingly.
  1687. *
  1688. * Return 0 if we hit end of tree / interval, 1 if we exhausted nr_to_scan.
  1689. * Increment *nr_shrunk by the number of reclaimed extents. Also update
  1690. * ei->i_es_shrink_lblk to where we should continue scanning.
  1691. */
  1692. static int es_do_reclaim_extents(struct ext4_inode_info *ei, ext4_lblk_t end,
  1693. int *nr_to_scan, int *nr_shrunk)
  1694. {
  1695. struct inode *inode = &ei->vfs_inode;
  1696. struct ext4_es_tree *tree = &ei->i_es_tree;
  1697. struct extent_status *es;
  1698. struct rb_node *node;
  1699. es = __es_tree_search(&tree->root, ei->i_es_shrink_lblk);
  1700. if (!es)
  1701. goto out_wrap;
  1702. while (*nr_to_scan > 0) {
  1703. if (es->es_lblk > end) {
  1704. ei->i_es_shrink_lblk = end + 1;
  1705. return 0;
  1706. }
  1707. (*nr_to_scan)--;
  1708. node = rb_next(&es->rb_node);
  1709. if (ext4_es_must_keep(es))
  1710. goto next;
  1711. if (ext4_es_is_referenced(es)) {
  1712. ext4_es_clear_referenced(es);
  1713. goto next;
  1714. }
  1715. rb_erase(&es->rb_node, &tree->root);
  1716. ext4_es_free_extent(inode, es);
  1717. (*nr_shrunk)++;
  1718. next:
  1719. if (!node)
  1720. goto out_wrap;
  1721. es = rb_entry(node, struct extent_status, rb_node);
  1722. }
  1723. ei->i_es_shrink_lblk = es->es_lblk;
  1724. return 1;
  1725. out_wrap:
  1726. ei->i_es_shrink_lblk = 0;
  1727. return 0;
  1728. }
  1729. static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan)
  1730. {
  1731. struct inode *inode = &ei->vfs_inode;
  1732. int nr_shrunk = 0;
  1733. ext4_lblk_t start = ei->i_es_shrink_lblk;
  1734. static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
  1735. DEFAULT_RATELIMIT_BURST);
  1736. if (ei->i_es_shk_nr == 0)
  1737. return 0;
  1738. if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED) &&
  1739. __ratelimit(&_rs))
  1740. ext4_warning(inode->i_sb, "forced shrink of precached extents");
  1741. if (!es_do_reclaim_extents(ei, EXT_MAX_BLOCKS, nr_to_scan, &nr_shrunk) &&
  1742. start != 0)
  1743. es_do_reclaim_extents(ei, start - 1, nr_to_scan, &nr_shrunk);
  1744. ei->i_es_tree.cache_es = NULL;
  1745. return nr_shrunk;
  1746. }
  1747. /*
  1748. * Called to support EXT4_IOC_CLEAR_ES_CACHE. We can only remove
  1749. * discretionary entries from the extent status cache. (Some entries
  1750. * must be present for proper operations.)
  1751. */
  1752. void ext4_clear_inode_es(struct inode *inode)
  1753. {
  1754. struct ext4_inode_info *ei = EXT4_I(inode);
  1755. struct extent_status *es;
  1756. struct ext4_es_tree *tree;
  1757. struct rb_node *node;
  1758. write_lock(&ei->i_es_lock);
  1759. tree = &EXT4_I(inode)->i_es_tree;
  1760. tree->cache_es = NULL;
  1761. node = rb_first(&tree->root);
  1762. while (node) {
  1763. es = rb_entry(node, struct extent_status, rb_node);
  1764. node = rb_next(node);
  1765. if (!ext4_es_must_keep(es)) {
  1766. rb_erase(&es->rb_node, &tree->root);
  1767. ext4_es_free_extent(inode, es);
  1768. }
  1769. }
  1770. ext4_clear_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
  1771. write_unlock(&ei->i_es_lock);
  1772. }
  1773. #ifdef ES_DEBUG__
  1774. static void ext4_print_pending_tree(struct inode *inode)
  1775. {
  1776. struct ext4_pending_tree *tree;
  1777. struct rb_node *node;
  1778. struct pending_reservation *pr;
  1779. printk(KERN_DEBUG "pending reservations for inode %lu:", inode->i_ino);
  1780. tree = &EXT4_I(inode)->i_pending_tree;
  1781. node = rb_first(&tree->root);
  1782. while (node) {
  1783. pr = rb_entry(node, struct pending_reservation, rb_node);
  1784. printk(KERN_DEBUG " %u", pr->lclu);
  1785. node = rb_next(node);
  1786. }
  1787. printk(KERN_DEBUG "\n");
  1788. }
  1789. #else
  1790. #define ext4_print_pending_tree(inode)
  1791. #endif
  1792. int __init ext4_init_pending(void)
  1793. {
  1794. ext4_pending_cachep = KMEM_CACHE(pending_reservation, SLAB_RECLAIM_ACCOUNT);
  1795. if (ext4_pending_cachep == NULL)
  1796. return -ENOMEM;
  1797. return 0;
  1798. }
  1799. void ext4_exit_pending(void)
  1800. {
  1801. kmem_cache_destroy(ext4_pending_cachep);
  1802. }
  1803. void ext4_init_pending_tree(struct ext4_pending_tree *tree)
  1804. {
  1805. tree->root = RB_ROOT;
  1806. }
  1807. /*
  1808. * __get_pending - retrieve a pointer to a pending reservation
  1809. *
  1810. * @inode - file containing the pending cluster reservation
  1811. * @lclu - logical cluster of interest
  1812. *
  1813. * Returns a pointer to a pending reservation if it's a member of
  1814. * the set, and NULL if not. Must be called holding i_es_lock.
  1815. */
  1816. static struct pending_reservation *__get_pending(struct inode *inode,
  1817. ext4_lblk_t lclu)
  1818. {
  1819. struct ext4_pending_tree *tree;
  1820. struct rb_node *node;
  1821. struct pending_reservation *pr = NULL;
  1822. tree = &EXT4_I(inode)->i_pending_tree;
  1823. node = (&tree->root)->rb_node;
  1824. while (node) {
  1825. pr = rb_entry(node, struct pending_reservation, rb_node);
  1826. if (lclu < pr->lclu)
  1827. node = node->rb_left;
  1828. else if (lclu > pr->lclu)
  1829. node = node->rb_right;
  1830. else if (lclu == pr->lclu)
  1831. return pr;
  1832. }
  1833. return NULL;
  1834. }
  1835. /*
  1836. * __insert_pending - adds a pending cluster reservation to the set of
  1837. * pending reservations
  1838. *
  1839. * @inode - file containing the cluster
  1840. * @lblk - logical block in the cluster to be added
  1841. * @prealloc - preallocated pending entry
  1842. *
  1843. * Returns 1 on successful insertion and -ENOMEM on failure. If the
  1844. * pending reservation is already in the set, returns successfully.
  1845. */
  1846. static int __insert_pending(struct inode *inode, ext4_lblk_t lblk,
  1847. struct pending_reservation **prealloc)
  1848. {
  1849. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1850. struct ext4_pending_tree *tree = &EXT4_I(inode)->i_pending_tree;
  1851. struct rb_node **p = &tree->root.rb_node;
  1852. struct rb_node *parent = NULL;
  1853. struct pending_reservation *pr;
  1854. ext4_lblk_t lclu;
  1855. int ret = 0;
  1856. lclu = EXT4_B2C(sbi, lblk);
  1857. /* search to find parent for insertion */
  1858. while (*p) {
  1859. parent = *p;
  1860. pr = rb_entry(parent, struct pending_reservation, rb_node);
  1861. if (lclu < pr->lclu) {
  1862. p = &(*p)->rb_left;
  1863. } else if (lclu > pr->lclu) {
  1864. p = &(*p)->rb_right;
  1865. } else {
  1866. /* pending reservation already inserted */
  1867. goto out;
  1868. }
  1869. }
  1870. if (likely(*prealloc == NULL)) {
  1871. pr = __alloc_pending(false);
  1872. if (!pr) {
  1873. ret = -ENOMEM;
  1874. goto out;
  1875. }
  1876. } else {
  1877. pr = *prealloc;
  1878. *prealloc = NULL;
  1879. }
  1880. pr->lclu = lclu;
  1881. rb_link_node(&pr->rb_node, parent, p);
  1882. rb_insert_color(&pr->rb_node, &tree->root);
  1883. ret = 1;
  1884. out:
  1885. return ret;
  1886. }
  1887. /*
  1888. * __remove_pending - removes a pending cluster reservation from the set
  1889. * of pending reservations
  1890. *
  1891. * @inode - file containing the cluster
  1892. * @lblk - logical block in the pending cluster reservation to be removed
  1893. *
  1894. * Returns successfully if pending reservation is not a member of the set.
  1895. */
  1896. static void __remove_pending(struct inode *inode, ext4_lblk_t lblk)
  1897. {
  1898. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1899. struct pending_reservation *pr;
  1900. struct ext4_pending_tree *tree;
  1901. pr = __get_pending(inode, EXT4_B2C(sbi, lblk));
  1902. if (pr != NULL) {
  1903. tree = &EXT4_I(inode)->i_pending_tree;
  1904. rb_erase(&pr->rb_node, &tree->root);
  1905. __free_pending(pr);
  1906. }
  1907. }
  1908. /*
  1909. * ext4_remove_pending - removes a pending cluster reservation from the set
  1910. * of pending reservations
  1911. *
  1912. * @inode - file containing the cluster
  1913. * @lblk - logical block in the pending cluster reservation to be removed
  1914. *
  1915. * Locking for external use of __remove_pending.
  1916. */
  1917. void ext4_remove_pending(struct inode *inode, ext4_lblk_t lblk)
  1918. {
  1919. struct ext4_inode_info *ei = EXT4_I(inode);
  1920. write_lock(&ei->i_es_lock);
  1921. __remove_pending(inode, lblk);
  1922. write_unlock(&ei->i_es_lock);
  1923. }
  1924. /*
  1925. * ext4_is_pending - determine whether a cluster has a pending reservation
  1926. * on it
  1927. *
  1928. * @inode - file containing the cluster
  1929. * @lblk - logical block in the cluster
  1930. *
  1931. * Returns true if there's a pending reservation for the cluster in the
  1932. * set of pending reservations, and false if not.
  1933. */
  1934. bool ext4_is_pending(struct inode *inode, ext4_lblk_t lblk)
  1935. {
  1936. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1937. struct ext4_inode_info *ei = EXT4_I(inode);
  1938. bool ret;
  1939. read_lock(&ei->i_es_lock);
  1940. ret = (bool)(__get_pending(inode, EXT4_B2C(sbi, lblk)) != NULL);
  1941. read_unlock(&ei->i_es_lock);
  1942. return ret;
  1943. }
  1944. /*
  1945. * ext4_es_insert_delayed_extent - adds some delayed blocks to the extents
  1946. * status tree, adding a pending reservation
  1947. * where needed
  1948. *
  1949. * @inode - file containing the newly added block
  1950. * @lblk - start logical block to be added
  1951. * @len - length of blocks to be added
  1952. * @lclu_allocated/end_allocated - indicates whether a physical cluster has
  1953. * been allocated for the logical cluster
  1954. * that contains the start/end block. Note that
  1955. * end_allocated should always be set to false
  1956. * if the start and the end block are in the
  1957. * same cluster
  1958. */
  1959. void ext4_es_insert_delayed_extent(struct inode *inode, ext4_lblk_t lblk,
  1960. ext4_lblk_t len, bool lclu_allocated,
  1961. bool end_allocated)
  1962. {
  1963. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1964. struct extent_status newes;
  1965. ext4_lblk_t end = lblk + len - 1;
  1966. int err1 = 0, err2 = 0, err3 = 0;
  1967. struct extent_status *es1 = NULL;
  1968. struct extent_status *es2 = NULL;
  1969. struct pending_reservation *pr1 = NULL;
  1970. struct pending_reservation *pr2 = NULL;
  1971. if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
  1972. return;
  1973. es_debug("add [%u/%u) delayed to extent status tree of inode %lu\n",
  1974. lblk, len, inode->i_ino);
  1975. if (!len)
  1976. return;
  1977. WARN_ON_ONCE((EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) &&
  1978. end_allocated);
  1979. newes.es_lblk = lblk;
  1980. newes.es_len = len;
  1981. ext4_es_store_pblock_status(&newes, ~0, EXTENT_STATUS_DELAYED);
  1982. ext4_es_insert_extent_check(inode, &newes);
  1983. retry:
  1984. if (err1 && !es1)
  1985. es1 = __es_alloc_extent(true);
  1986. if ((err1 || err2) && !es2)
  1987. es2 = __es_alloc_extent(true);
  1988. if (err1 || err2 || err3 < 0) {
  1989. if (lclu_allocated && !pr1)
  1990. pr1 = __alloc_pending(true);
  1991. if (end_allocated && !pr2)
  1992. pr2 = __alloc_pending(true);
  1993. }
  1994. write_lock(&EXT4_I(inode)->i_es_lock);
  1995. err1 = __es_remove_extent(inode, lblk, end, 0, NULL, NULL, es1);
  1996. if (err1 != 0)
  1997. goto error;
  1998. /* Free preallocated extent if it didn't get used. */
  1999. if (es1) {
  2000. if (!es1->es_len)
  2001. __es_free_extent(es1);
  2002. es1 = NULL;
  2003. }
  2004. err2 = __es_insert_extent(inode, &newes, es2);
  2005. if (err2 != 0)
  2006. goto error;
  2007. /* Free preallocated extent if it didn't get used. */
  2008. if (es2) {
  2009. if (!es2->es_len)
  2010. __es_free_extent(es2);
  2011. es2 = NULL;
  2012. }
  2013. if (lclu_allocated) {
  2014. err3 = __insert_pending(inode, lblk, &pr1);
  2015. if (err3 < 0)
  2016. goto error;
  2017. if (pr1) {
  2018. __free_pending(pr1);
  2019. pr1 = NULL;
  2020. }
  2021. }
  2022. if (end_allocated) {
  2023. err3 = __insert_pending(inode, end, &pr2);
  2024. if (err3 < 0)
  2025. goto error;
  2026. if (pr2) {
  2027. __free_pending(pr2);
  2028. pr2 = NULL;
  2029. }
  2030. }
  2031. ext4_es_inc_seq(inode);
  2032. error:
  2033. write_unlock(&EXT4_I(inode)->i_es_lock);
  2034. if (err1 || err2 || err3 < 0)
  2035. goto retry;
  2036. trace_ext4_es_insert_delayed_extent(inode, &newes, lclu_allocated,
  2037. end_allocated);
  2038. ext4_es_print_tree(inode);
  2039. ext4_print_pending_tree(inode);
  2040. return;
  2041. }
  2042. /*
  2043. * __revise_pending - makes, cancels, or leaves unchanged pending cluster
  2044. * reservations for a specified block range depending
  2045. * upon the presence or absence of delayed blocks
  2046. * outside the range within clusters at the ends of the
  2047. * range
  2048. *
  2049. * @inode - file containing the range
  2050. * @lblk - logical block defining the start of range
  2051. * @len - length of range in blocks
  2052. * @prealloc - preallocated pending entry
  2053. *
  2054. * Used after a newly allocated extent is added to the extents status tree.
  2055. * Requires that the extents in the range have either written or unwritten
  2056. * status. Must be called while holding i_es_lock. Returns number of new
  2057. * inserts pending cluster on insert pendings, returns 0 on remove pendings,
  2058. * return -ENOMEM on failure.
  2059. */
  2060. static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
  2061. ext4_lblk_t len,
  2062. struct pending_reservation **prealloc)
  2063. {
  2064. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2065. ext4_lblk_t end = lblk + len - 1;
  2066. ext4_lblk_t first, last;
  2067. bool f_del = false, l_del = false;
  2068. int pendings = 0;
  2069. int ret = 0;
  2070. if (len == 0)
  2071. return 0;
  2072. /*
  2073. * Two cases - block range within single cluster and block range
  2074. * spanning two or more clusters. Note that a cluster belonging
  2075. * to a range starting and/or ending on a cluster boundary is treated
  2076. * as if it does not contain a delayed extent. The new range may
  2077. * have allocated space for previously delayed blocks out to the
  2078. * cluster boundary, requiring that any pre-existing pending
  2079. * reservation be canceled. Because this code only looks at blocks
  2080. * outside the range, it should revise pending reservations
  2081. * correctly even if the extent represented by the range can't be
  2082. * inserted in the extents status tree due to ENOSPC.
  2083. */
  2084. if (EXT4_B2C(sbi, lblk) == EXT4_B2C(sbi, end)) {
  2085. first = EXT4_LBLK_CMASK(sbi, lblk);
  2086. if (first != lblk)
  2087. f_del = __es_scan_range(inode, &ext4_es_is_delayed,
  2088. first, lblk - 1);
  2089. if (f_del) {
  2090. ret = __insert_pending(inode, first, prealloc);
  2091. if (ret < 0)
  2092. goto out;
  2093. pendings += ret;
  2094. } else {
  2095. last = EXT4_LBLK_CMASK(sbi, end) +
  2096. sbi->s_cluster_ratio - 1;
  2097. if (last != end)
  2098. l_del = __es_scan_range(inode,
  2099. &ext4_es_is_delayed,
  2100. end + 1, last);
  2101. if (l_del) {
  2102. ret = __insert_pending(inode, last, prealloc);
  2103. if (ret < 0)
  2104. goto out;
  2105. pendings += ret;
  2106. } else
  2107. __remove_pending(inode, last);
  2108. }
  2109. } else {
  2110. first = EXT4_LBLK_CMASK(sbi, lblk);
  2111. if (first != lblk)
  2112. f_del = __es_scan_range(inode, &ext4_es_is_delayed,
  2113. first, lblk - 1);
  2114. if (f_del) {
  2115. ret = __insert_pending(inode, first, prealloc);
  2116. if (ret < 0)
  2117. goto out;
  2118. pendings += ret;
  2119. } else
  2120. __remove_pending(inode, first);
  2121. last = EXT4_LBLK_CMASK(sbi, end) + sbi->s_cluster_ratio - 1;
  2122. if (last != end)
  2123. l_del = __es_scan_range(inode, &ext4_es_is_delayed,
  2124. end + 1, last);
  2125. if (l_del) {
  2126. ret = __insert_pending(inode, last, prealloc);
  2127. if (ret < 0)
  2128. goto out;
  2129. pendings += ret;
  2130. } else
  2131. __remove_pending(inode, last);
  2132. }
  2133. out:
  2134. return (ret < 0) ? ret : pendings;
  2135. }