indirect.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/indirect.c
  4. *
  5. * from
  6. *
  7. * linux/fs/ext4/inode.c
  8. *
  9. * Copyright (C) 1992, 1993, 1994, 1995
  10. * Remy Card (card@masi.ibp.fr)
  11. * Laboratoire MASI - Institut Blaise Pascal
  12. * Universite Pierre et Marie Curie (Paris VI)
  13. *
  14. * from
  15. *
  16. * linux/fs/minix/inode.c
  17. *
  18. * Copyright (C) 1991, 1992 Linus Torvalds
  19. *
  20. * Goal-directed block allocation by Stephen Tweedie
  21. * (sct@redhat.com), 1993, 1998
  22. */
  23. #include "ext4_jbd2.h"
  24. #include "truncate.h"
  25. #include <linux/dax.h>
  26. #include <linux/uio.h>
  27. #include <trace/events/ext4.h>
  28. typedef struct {
  29. __le32 *p;
  30. __le32 key;
  31. struct buffer_head *bh;
  32. } Indirect;
  33. static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
  34. {
  35. p->key = *(p->p = v);
  36. p->bh = bh;
  37. }
  38. /**
  39. * ext4_block_to_path - parse the block number into array of offsets
  40. * @inode: inode in question (we are only interested in its superblock)
  41. * @i_block: block number to be parsed
  42. * @offsets: array to store the offsets in
  43. * @boundary: set this non-zero if the referred-to block is likely to be
  44. * followed (on disk) by an indirect block.
  45. *
  46. * To store the locations of file's data ext4 uses a data structure common
  47. * for UNIX filesystems - tree of pointers anchored in the inode, with
  48. * data blocks at leaves and indirect blocks in intermediate nodes.
  49. * This function translates the block number into path in that tree -
  50. * return value is the path length and @offsets[n] is the offset of
  51. * pointer to (n+1)th node in the nth one. If @block is out of range
  52. * (negative or too large) warning is printed and zero returned.
  53. *
  54. * Note: function doesn't find node addresses, so no IO is needed. All
  55. * we need to know is the capacity of indirect blocks (taken from the
  56. * inode->i_sb).
  57. */
  58. /*
  59. * Portability note: the last comparison (check that we fit into triple
  60. * indirect block) is spelled differently, because otherwise on an
  61. * architecture with 32-bit longs and 8Kb pages we might get into trouble
  62. * if our filesystem had 8Kb blocks. We might use long long, but that would
  63. * kill us on x86. Oh, well, at least the sign propagation does not matter -
  64. * i_block would have to be negative in the very beginning, so we would not
  65. * get there at all.
  66. */
  67. static int ext4_block_to_path(struct inode *inode,
  68. ext4_lblk_t i_block,
  69. ext4_lblk_t offsets[4], int *boundary)
  70. {
  71. int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  72. int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb);
  73. const long direct_blocks = EXT4_NDIR_BLOCKS,
  74. indirect_blocks = ptrs,
  75. double_blocks = (1 << (ptrs_bits * 2));
  76. int n = 0;
  77. int final = 0;
  78. if (i_block < direct_blocks) {
  79. offsets[n++] = i_block;
  80. final = direct_blocks;
  81. } else if ((i_block -= direct_blocks) < indirect_blocks) {
  82. offsets[n++] = EXT4_IND_BLOCK;
  83. offsets[n++] = i_block;
  84. final = ptrs;
  85. } else if ((i_block -= indirect_blocks) < double_blocks) {
  86. offsets[n++] = EXT4_DIND_BLOCK;
  87. offsets[n++] = i_block >> ptrs_bits;
  88. offsets[n++] = i_block & (ptrs - 1);
  89. final = ptrs;
  90. } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
  91. offsets[n++] = EXT4_TIND_BLOCK;
  92. offsets[n++] = i_block >> (ptrs_bits * 2);
  93. offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
  94. offsets[n++] = i_block & (ptrs - 1);
  95. final = ptrs;
  96. } else {
  97. ext4_warning(inode->i_sb, "block %lu > max in inode %lu",
  98. i_block + direct_blocks +
  99. indirect_blocks + double_blocks, inode->i_ino);
  100. }
  101. if (boundary)
  102. *boundary = final - 1 - (i_block & (ptrs - 1));
  103. return n;
  104. }
  105. /**
  106. * ext4_get_branch - read the chain of indirect blocks leading to data
  107. * @inode: inode in question
  108. * @depth: depth of the chain (1 - direct pointer, etc.)
  109. * @offsets: offsets of pointers in inode/indirect blocks
  110. * @chain: place to store the result
  111. * @err: here we store the error value
  112. *
  113. * Function fills the array of triples <key, p, bh> and returns %NULL
  114. * if everything went OK or the pointer to the last filled triple
  115. * (incomplete one) otherwise. Upon the return chain[i].key contains
  116. * the number of (i+1)-th block in the chain (as it is stored in memory,
  117. * i.e. little-endian 32-bit), chain[i].p contains the address of that
  118. * number (it points into struct inode for i==0 and into the bh->b_data
  119. * for i>0) and chain[i].bh points to the buffer_head of i-th indirect
  120. * block for i>0 and NULL for i==0. In other words, it holds the block
  121. * numbers of the chain, addresses they were taken from (and where we can
  122. * verify that chain did not change) and buffer_heads hosting these
  123. * numbers.
  124. *
  125. * Function stops when it stumbles upon zero pointer (absent block)
  126. * (pointer to last triple returned, *@err == 0)
  127. * or when it gets an IO error reading an indirect block
  128. * (ditto, *@err == -EIO)
  129. * or when it reads all @depth-1 indirect blocks successfully and finds
  130. * the whole chain, all way to the data (returns %NULL, *err == 0).
  131. *
  132. * Need to be called with
  133. * down_read(&EXT4_I(inode)->i_data_sem)
  134. */
  135. static Indirect *ext4_get_branch(struct inode *inode, int depth,
  136. ext4_lblk_t *offsets,
  137. Indirect chain[4], int *err)
  138. {
  139. struct super_block *sb = inode->i_sb;
  140. Indirect *p = chain;
  141. struct buffer_head *bh;
  142. unsigned int key;
  143. int ret = -EIO;
  144. *err = 0;
  145. /* i_data is not going away, no lock needed */
  146. add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets);
  147. if (!p->key)
  148. goto no_block;
  149. while (--depth) {
  150. key = le32_to_cpu(p->key);
  151. if (key > ext4_blocks_count(EXT4_SB(sb)->s_es)) {
  152. /* the block was out of range */
  153. ret = -EFSCORRUPTED;
  154. goto failure;
  155. }
  156. bh = sb_getblk(sb, key);
  157. if (unlikely(!bh)) {
  158. ret = -ENOMEM;
  159. goto failure;
  160. }
  161. if (!bh_uptodate_or_lock(bh)) {
  162. if (ext4_read_bh(bh, 0, NULL, false) < 0) {
  163. put_bh(bh);
  164. goto failure;
  165. }
  166. /* validate block references */
  167. if (ext4_check_indirect_blockref(inode, bh)) {
  168. put_bh(bh);
  169. goto failure;
  170. }
  171. }
  172. add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets);
  173. /* Reader: end */
  174. if (!p->key)
  175. goto no_block;
  176. }
  177. return NULL;
  178. failure:
  179. *err = ret;
  180. no_block:
  181. return p;
  182. }
  183. /**
  184. * ext4_find_near - find a place for allocation with sufficient locality
  185. * @inode: owner
  186. * @ind: descriptor of indirect block.
  187. *
  188. * This function returns the preferred place for block allocation.
  189. * It is used when heuristic for sequential allocation fails.
  190. * Rules are:
  191. * + if there is a block to the left of our position - allocate near it.
  192. * + if pointer will live in indirect block - allocate near that block.
  193. * + if pointer will live in inode - allocate in the same
  194. * cylinder group.
  195. *
  196. * In the latter case we colour the starting block by the callers PID to
  197. * prevent it from clashing with concurrent allocations for a different inode
  198. * in the same block group. The PID is used here so that functionally related
  199. * files will be close-by on-disk.
  200. *
  201. * Caller must make sure that @ind is valid and will stay that way.
  202. */
  203. static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind)
  204. {
  205. struct ext4_inode_info *ei = EXT4_I(inode);
  206. __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
  207. __le32 *p;
  208. /* Try to find previous block */
  209. for (p = ind->p - 1; p >= start; p--) {
  210. if (*p)
  211. return le32_to_cpu(*p);
  212. }
  213. /* No such thing, so let's try location of indirect block */
  214. if (ind->bh)
  215. return ind->bh->b_blocknr;
  216. /*
  217. * It is going to be referred to from the inode itself? OK, just put it
  218. * into the same cylinder group then.
  219. */
  220. return ext4_inode_to_goal_block(inode);
  221. }
  222. /**
  223. * ext4_find_goal - find a preferred place for allocation.
  224. * @inode: owner
  225. * @block: block we want
  226. * @partial: pointer to the last triple within a chain
  227. *
  228. * Normally this function find the preferred place for block allocation,
  229. * returns it.
  230. * Because this is only used for non-extent files, we limit the block nr
  231. * to 32 bits.
  232. */
  233. static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block,
  234. Indirect *partial)
  235. {
  236. ext4_fsblk_t goal;
  237. /*
  238. * XXX need to get goal block from mballoc's data structures
  239. */
  240. goal = ext4_find_near(inode, partial);
  241. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  242. return goal;
  243. }
  244. /**
  245. * ext4_blks_to_allocate - Look up the block map and count the number
  246. * of direct blocks need to be allocated for the given branch.
  247. *
  248. * @branch: chain of indirect blocks
  249. * @k: number of blocks need for indirect blocks
  250. * @blks: number of data blocks to be mapped.
  251. * @blocks_to_boundary: the offset in the indirect block
  252. *
  253. * return the total number of blocks to be allocate, including the
  254. * direct and indirect blocks.
  255. */
  256. static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks,
  257. int blocks_to_boundary)
  258. {
  259. unsigned int count = 0;
  260. /*
  261. * Simple case, [t,d]Indirect block(s) has not allocated yet
  262. * then it's clear blocks on that path have not allocated
  263. */
  264. if (k > 0) {
  265. /* right now we don't handle cross boundary allocation */
  266. if (blks < blocks_to_boundary + 1)
  267. count += blks;
  268. else
  269. count += blocks_to_boundary + 1;
  270. return count;
  271. }
  272. count++;
  273. while (count < blks && count <= blocks_to_boundary &&
  274. le32_to_cpu(*(branch[0].p + count)) == 0) {
  275. count++;
  276. }
  277. return count;
  278. }
  279. /**
  280. * ext4_alloc_branch() - allocate and set up a chain of blocks
  281. * @handle: handle for this transaction
  282. * @ar: structure describing the allocation request
  283. * @indirect_blks: number of allocated indirect blocks
  284. * @offsets: offsets (in the blocks) to store the pointers to next.
  285. * @branch: place to store the chain in.
  286. *
  287. * This function allocates blocks, zeroes out all but the last one,
  288. * links them into chain and (if we are synchronous) writes them to disk.
  289. * In other words, it prepares a branch that can be spliced onto the
  290. * inode. It stores the information about that chain in the branch[], in
  291. * the same format as ext4_get_branch() would do. We are calling it after
  292. * we had read the existing part of chain and partial points to the last
  293. * triple of that (one with zero ->key). Upon the exit we have the same
  294. * picture as after the successful ext4_get_block(), except that in one
  295. * place chain is disconnected - *branch->p is still zero (we did not
  296. * set the last link), but branch->key contains the number that should
  297. * be placed into *branch->p to fill that gap.
  298. *
  299. * If allocation fails we free all blocks we've allocated (and forget
  300. * their buffer_heads) and return the error value the from failed
  301. * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain
  302. * as described above and return 0.
  303. */
  304. static int ext4_alloc_branch(handle_t *handle,
  305. struct ext4_allocation_request *ar,
  306. int indirect_blks, ext4_lblk_t *offsets,
  307. Indirect *branch)
  308. {
  309. struct buffer_head * bh;
  310. ext4_fsblk_t b, new_blocks[4];
  311. __le32 *p;
  312. int i, j, err, len = 1;
  313. for (i = 0; i <= indirect_blks; i++) {
  314. if (i == indirect_blks) {
  315. new_blocks[i] = ext4_mb_new_blocks(handle, ar, &err);
  316. } else {
  317. ar->goal = new_blocks[i] = ext4_new_meta_blocks(handle,
  318. ar->inode, ar->goal,
  319. ar->flags & EXT4_MB_DELALLOC_RESERVED,
  320. NULL, &err);
  321. /* Simplify error cleanup... */
  322. branch[i+1].bh = NULL;
  323. }
  324. if (err) {
  325. i--;
  326. goto failed;
  327. }
  328. branch[i].key = cpu_to_le32(new_blocks[i]);
  329. if (i == 0)
  330. continue;
  331. bh = branch[i].bh = sb_getblk(ar->inode->i_sb, new_blocks[i-1]);
  332. if (unlikely(!bh)) {
  333. err = -ENOMEM;
  334. goto failed;
  335. }
  336. lock_buffer(bh);
  337. BUFFER_TRACE(bh, "call get_create_access");
  338. err = ext4_journal_get_create_access(handle, ar->inode->i_sb,
  339. bh, EXT4_JTR_NONE);
  340. if (err) {
  341. unlock_buffer(bh);
  342. goto failed;
  343. }
  344. memset(bh->b_data, 0, bh->b_size);
  345. p = branch[i].p = (__le32 *) bh->b_data + offsets[i];
  346. b = new_blocks[i];
  347. if (i == indirect_blks)
  348. len = ar->len;
  349. for (j = 0; j < len; j++)
  350. *p++ = cpu_to_le32(b++);
  351. BUFFER_TRACE(bh, "marking uptodate");
  352. set_buffer_uptodate(bh);
  353. unlock_buffer(bh);
  354. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  355. err = ext4_handle_dirty_metadata(handle, ar->inode, bh);
  356. if (err)
  357. goto failed;
  358. }
  359. return 0;
  360. failed:
  361. if (i == indirect_blks) {
  362. /* Free data blocks */
  363. ext4_free_blocks(handle, ar->inode, NULL, new_blocks[i],
  364. ar->len, 0);
  365. i--;
  366. }
  367. for (; i >= 0; i--) {
  368. /*
  369. * We want to ext4_forget() only freshly allocated indirect
  370. * blocks. Buffer for new_blocks[i] is at branch[i+1].bh
  371. * (buffer at branch[0].bh is indirect block / inode already
  372. * existing before ext4_alloc_branch() was called). Also
  373. * because blocks are freshly allocated, we don't need to
  374. * revoke them which is why we don't set
  375. * EXT4_FREE_BLOCKS_METADATA.
  376. */
  377. ext4_free_blocks(handle, ar->inode, branch[i+1].bh,
  378. new_blocks[i], 1,
  379. branch[i+1].bh ? EXT4_FREE_BLOCKS_FORGET : 0);
  380. }
  381. return err;
  382. }
  383. /**
  384. * ext4_splice_branch() - splice the allocated branch onto inode.
  385. * @handle: handle for this transaction
  386. * @ar: structure describing the allocation request
  387. * @where: location of missing link
  388. * @num: number of indirect blocks we are adding
  389. *
  390. * This function fills the missing link and does all housekeeping needed in
  391. * inode (->i_blocks, etc.). In case of success we end up with the full
  392. * chain to new block and return 0.
  393. */
  394. static int ext4_splice_branch(handle_t *handle,
  395. struct ext4_allocation_request *ar,
  396. Indirect *where, int num)
  397. {
  398. int i;
  399. int err = 0;
  400. ext4_fsblk_t current_block;
  401. /*
  402. * If we're splicing into a [td]indirect block (as opposed to the
  403. * inode) then we need to get write access to the [td]indirect block
  404. * before the splice.
  405. */
  406. if (where->bh) {
  407. BUFFER_TRACE(where->bh, "get_write_access");
  408. err = ext4_journal_get_write_access(handle, ar->inode->i_sb,
  409. where->bh, EXT4_JTR_NONE);
  410. if (err)
  411. goto err_out;
  412. }
  413. /* That's it */
  414. *where->p = where->key;
  415. /*
  416. * Update the host buffer_head or inode to point to more just allocated
  417. * direct blocks blocks
  418. */
  419. if (num == 0 && ar->len > 1) {
  420. current_block = le32_to_cpu(where->key) + 1;
  421. for (i = 1; i < ar->len; i++)
  422. *(where->p + i) = cpu_to_le32(current_block++);
  423. }
  424. /* We are done with atomic stuff, now do the rest of housekeeping */
  425. /* had we spliced it onto indirect block? */
  426. if (where->bh) {
  427. /*
  428. * If we spliced it onto an indirect block, we haven't
  429. * altered the inode. Note however that if it is being spliced
  430. * onto an indirect block at the very end of the file (the
  431. * file is growing) then we *will* alter the inode to reflect
  432. * the new i_size. But that is not done here - it is done in
  433. * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
  434. */
  435. ext4_debug("splicing indirect only\n");
  436. BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
  437. err = ext4_handle_dirty_metadata(handle, ar->inode, where->bh);
  438. if (err)
  439. goto err_out;
  440. } else {
  441. /*
  442. * OK, we spliced it into the inode itself on a direct block.
  443. */
  444. err = ext4_mark_inode_dirty(handle, ar->inode);
  445. if (unlikely(err))
  446. goto err_out;
  447. ext4_debug("splicing direct\n");
  448. }
  449. return err;
  450. err_out:
  451. for (i = 1; i <= num; i++) {
  452. /*
  453. * branch[i].bh is newly allocated, so there is no
  454. * need to revoke the block, which is why we don't
  455. * need to set EXT4_FREE_BLOCKS_METADATA.
  456. */
  457. ext4_free_blocks(handle, ar->inode, where[i].bh, 0, 1,
  458. EXT4_FREE_BLOCKS_FORGET);
  459. }
  460. ext4_free_blocks(handle, ar->inode, NULL, le32_to_cpu(where[num].key),
  461. ar->len, 0);
  462. return err;
  463. }
  464. /*
  465. * The ext4_ind_map_blocks() function handles non-extents inodes
  466. * (i.e., using the traditional indirect/double-indirect i_blocks
  467. * scheme) for ext4_map_blocks().
  468. *
  469. * Allocation strategy is simple: if we have to allocate something, we will
  470. * have to go the whole way to leaf. So let's do it before attaching anything
  471. * to tree, set linkage between the newborn blocks, write them if sync is
  472. * required, recheck the path, free and repeat if check fails, otherwise
  473. * set the last missing link (that will protect us from any truncate-generated
  474. * removals - all blocks on the path are immune now) and possibly force the
  475. * write on the parent block.
  476. * That has a nice additional property: no special recovery from the failed
  477. * allocations is needed - we simply release blocks and do not touch anything
  478. * reachable from inode.
  479. *
  480. * `handle' can be NULL if create == 0.
  481. *
  482. * return > 0, # of blocks mapped or allocated.
  483. * return = 0, if plain lookup failed.
  484. * return < 0, error case.
  485. *
  486. * The ext4_ind_get_blocks() function should be called with
  487. * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
  488. * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
  489. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  490. * blocks.
  491. */
  492. int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
  493. struct ext4_map_blocks *map,
  494. int flags)
  495. {
  496. struct ext4_allocation_request ar;
  497. int err = -EIO;
  498. ext4_lblk_t offsets[4];
  499. Indirect chain[4];
  500. Indirect *partial;
  501. int indirect_blks;
  502. int blocks_to_boundary = 0;
  503. int depth;
  504. u64 count = 0;
  505. ext4_fsblk_t first_block = 0;
  506. trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  507. ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
  508. ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
  509. depth = ext4_block_to_path(inode, map->m_lblk, offsets,
  510. &blocks_to_boundary);
  511. if (depth == 0)
  512. goto out;
  513. partial = ext4_get_branch(inode, depth, offsets, chain, &err);
  514. /* Simplest case - block found, no allocation needed */
  515. if (!partial) {
  516. first_block = le32_to_cpu(chain[depth - 1].key);
  517. count++;
  518. /*map more blocks*/
  519. while (count < map->m_len && count <= blocks_to_boundary) {
  520. ext4_fsblk_t blk;
  521. blk = le32_to_cpu(*(chain[depth-1].p + count));
  522. if (blk == first_block + count)
  523. count++;
  524. else
  525. break;
  526. }
  527. goto got_it;
  528. }
  529. /* Next simple case - plain lookup failed */
  530. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  531. unsigned epb = inode->i_sb->s_blocksize / sizeof(u32);
  532. int i;
  533. /*
  534. * Count number blocks in a subtree under 'partial'. At each
  535. * level we count number of complete empty subtrees beyond
  536. * current offset and then descend into the subtree only
  537. * partially beyond current offset.
  538. */
  539. count = 0;
  540. for (i = partial - chain + 1; i < depth; i++)
  541. count = count * epb + (epb - offsets[i] - 1);
  542. count++;
  543. /* Fill in size of a hole we found */
  544. map->m_pblk = 0;
  545. map->m_len = umin(map->m_len, count);
  546. goto cleanup;
  547. }
  548. /* Failed read of indirect block */
  549. if (err == -EIO)
  550. goto cleanup;
  551. /*
  552. * Okay, we need to do block allocation.
  553. */
  554. if (ext4_has_feature_bigalloc(inode->i_sb)) {
  555. EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
  556. "non-extent mapped inodes with bigalloc");
  557. err = -EFSCORRUPTED;
  558. goto out;
  559. }
  560. /* Set up for the direct block allocation */
  561. memset(&ar, 0, sizeof(ar));
  562. ar.inode = inode;
  563. ar.logical = map->m_lblk;
  564. if (S_ISREG(inode->i_mode))
  565. ar.flags = EXT4_MB_HINT_DATA;
  566. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
  567. ar.flags |= EXT4_MB_DELALLOC_RESERVED;
  568. if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  569. ar.flags |= EXT4_MB_USE_RESERVED;
  570. ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
  571. /* the number of blocks need to allocate for [d,t]indirect blocks */
  572. indirect_blks = (chain + depth) - partial - 1;
  573. /*
  574. * Next look up the indirect map to count the totoal number of
  575. * direct blocks to allocate for this branch.
  576. */
  577. ar.len = ext4_blks_to_allocate(partial, indirect_blks,
  578. map->m_len, blocks_to_boundary);
  579. /*
  580. * Block out ext4_truncate while we alter the tree
  581. */
  582. err = ext4_alloc_branch(handle, &ar, indirect_blks,
  583. offsets + (partial - chain), partial);
  584. /*
  585. * The ext4_splice_branch call will free and forget any buffers
  586. * on the new chain if there is a failure, but that risks using
  587. * up transaction credits, especially for bitmaps where the
  588. * credits cannot be returned. Can we handle this somehow? We
  589. * may need to return -EAGAIN upwards in the worst case. --sct
  590. */
  591. if (!err)
  592. err = ext4_splice_branch(handle, &ar, partial, indirect_blks);
  593. if (err)
  594. goto cleanup;
  595. map->m_flags |= EXT4_MAP_NEW;
  596. ext4_update_inode_fsync_trans(handle, inode, 1);
  597. count = ar.len;
  598. got_it:
  599. map->m_flags |= EXT4_MAP_MAPPED;
  600. map->m_pblk = le32_to_cpu(chain[depth-1].key);
  601. map->m_len = count;
  602. if (count > blocks_to_boundary)
  603. map->m_flags |= EXT4_MAP_BOUNDARY;
  604. err = count;
  605. /* Clean up and exit */
  606. partial = chain + depth - 1; /* the whole chain */
  607. cleanup:
  608. while (partial > chain) {
  609. BUFFER_TRACE(partial->bh, "call brelse");
  610. brelse(partial->bh);
  611. partial--;
  612. }
  613. out:
  614. trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
  615. return err;
  616. }
  617. /*
  618. * Calculate number of indirect blocks touched by mapping @nrblocks logically
  619. * contiguous blocks
  620. */
  621. int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
  622. {
  623. /*
  624. * With N contiguous data blocks, we need at most
  625. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
  626. * 2 dindirect blocks, and 1 tindirect block
  627. */
  628. return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
  629. }
  630. static int ext4_ind_trunc_restart_fn(handle_t *handle, struct inode *inode,
  631. struct buffer_head *bh, int *dropped)
  632. {
  633. int err;
  634. if (bh) {
  635. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  636. err = ext4_handle_dirty_metadata(handle, inode, bh);
  637. if (unlikely(err))
  638. return err;
  639. }
  640. err = ext4_mark_inode_dirty(handle, inode);
  641. if (unlikely(err))
  642. return err;
  643. /*
  644. * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
  645. * moment, get_block can be called only for blocks inside i_size since
  646. * page cache has been already dropped and writes are blocked by
  647. * i_rwsem. So we can safely drop the i_data_sem here.
  648. */
  649. BUG_ON(EXT4_JOURNAL(inode) == NULL);
  650. ext4_discard_preallocations(inode);
  651. up_write(&EXT4_I(inode)->i_data_sem);
  652. *dropped = 1;
  653. return 0;
  654. }
  655. /*
  656. * Truncate transactions can be complex and absolutely huge. So we need to
  657. * be able to restart the transaction at a convenient checkpoint to make
  658. * sure we don't overflow the journal.
  659. *
  660. * Try to extend this transaction for the purposes of truncation. If
  661. * extend fails, we restart transaction.
  662. */
  663. static int ext4_ind_truncate_ensure_credits(handle_t *handle,
  664. struct inode *inode,
  665. struct buffer_head *bh,
  666. int revoke_creds)
  667. {
  668. int ret;
  669. int dropped = 0;
  670. ret = ext4_journal_ensure_credits_fn(handle, EXT4_RESERVE_TRANS_BLOCKS,
  671. ext4_blocks_for_truncate(inode), revoke_creds,
  672. ext4_ind_trunc_restart_fn(handle, inode, bh, &dropped));
  673. if (dropped)
  674. down_write(&EXT4_I(inode)->i_data_sem);
  675. if (ret <= 0)
  676. return ret;
  677. if (bh) {
  678. BUFFER_TRACE(bh, "retaking write access");
  679. ret = ext4_journal_get_write_access(handle, inode->i_sb, bh,
  680. EXT4_JTR_NONE);
  681. if (unlikely(ret))
  682. return ret;
  683. }
  684. return 0;
  685. }
  686. /*
  687. * Probably it should be a library function... search for first non-zero word
  688. * or memcmp with zero_page, whatever is better for particular architecture.
  689. * Linus?
  690. */
  691. static inline int all_zeroes(__le32 *p, __le32 *q)
  692. {
  693. while (p < q)
  694. if (*p++)
  695. return 0;
  696. return 1;
  697. }
  698. /**
  699. * ext4_find_shared - find the indirect blocks for partial truncation.
  700. * @inode: inode in question
  701. * @depth: depth of the affected branch
  702. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  703. * @chain: place to store the pointers to partial indirect blocks
  704. * @top: place to the (detached) top of branch
  705. *
  706. * This is a helper function used by ext4_truncate().
  707. *
  708. * When we do truncate() we may have to clean the ends of several
  709. * indirect blocks but leave the blocks themselves alive. Block is
  710. * partially truncated if some data below the new i_size is referred
  711. * from it (and it is on the path to the first completely truncated
  712. * data block, indeed). We have to free the top of that path along
  713. * with everything to the right of the path. Since no allocation
  714. * past the truncation point is possible until ext4_truncate()
  715. * finishes, we may safely do the latter, but top of branch may
  716. * require special attention - pageout below the truncation point
  717. * might try to populate it.
  718. *
  719. * We atomically detach the top of branch from the tree, store the
  720. * block number of its root in *@top, pointers to buffer_heads of
  721. * partially truncated blocks - in @chain[].bh and pointers to
  722. * their last elements that should not be removed - in
  723. * @chain[].p. Return value is the pointer to last filled element
  724. * of @chain.
  725. *
  726. * The work left to caller to do the actual freeing of subtrees:
  727. * a) free the subtree starting from *@top
  728. * b) free the subtrees whose roots are stored in
  729. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  730. * c) free the subtrees growing from the inode past the @chain[0].
  731. * (no partially truncated stuff there). */
  732. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  733. ext4_lblk_t offsets[4], Indirect chain[4],
  734. __le32 *top)
  735. {
  736. Indirect *partial, *p;
  737. int k, err;
  738. *top = 0;
  739. /* Make k index the deepest non-null offset + 1 */
  740. for (k = depth; k > 1 && !offsets[k-1]; k--)
  741. ;
  742. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  743. /* Writer: pointers */
  744. if (!partial)
  745. partial = chain + k-1;
  746. /*
  747. * If the branch acquired continuation since we've looked at it -
  748. * fine, it should all survive and (new) top doesn't belong to us.
  749. */
  750. if (!partial->key && *partial->p)
  751. /* Writer: end */
  752. goto no_top;
  753. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  754. ;
  755. /*
  756. * OK, we've found the last block that must survive. The rest of our
  757. * branch should be detached before unlocking. However, if that rest
  758. * of branch is all ours and does not grow immediately from the inode
  759. * it's easier to cheat and just decrement partial->p.
  760. */
  761. if (p == chain + k - 1 && p > chain) {
  762. p->p--;
  763. } else {
  764. *top = *p->p;
  765. /* Nope, don't do this in ext4. Must leave the tree intact */
  766. #if 0
  767. *p->p = 0;
  768. #endif
  769. }
  770. /* Writer: end */
  771. while (partial > p) {
  772. brelse(partial->bh);
  773. partial--;
  774. }
  775. no_top:
  776. return partial;
  777. }
  778. /*
  779. * Zero a number of block pointers in either an inode or an indirect block.
  780. * If we restart the transaction we must again get write access to the
  781. * indirect block for further modification.
  782. *
  783. * We release `count' blocks on disk, but (last - first) may be greater
  784. * than `count' because there can be holes in there.
  785. *
  786. * Return 0 on success, 1 on invalid block range
  787. * and < 0 on fatal error.
  788. */
  789. static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
  790. struct buffer_head *bh,
  791. ext4_fsblk_t block_to_free,
  792. unsigned long count, __le32 *first,
  793. __le32 *last)
  794. {
  795. __le32 *p;
  796. int flags = EXT4_FREE_BLOCKS_VALIDATED;
  797. int err;
  798. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
  799. ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
  800. flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
  801. else if (ext4_should_journal_data(inode))
  802. flags |= EXT4_FREE_BLOCKS_FORGET;
  803. if (!ext4_inode_block_valid(inode, block_to_free, count)) {
  804. EXT4_ERROR_INODE(inode, "attempt to clear invalid "
  805. "blocks %llu len %lu",
  806. (unsigned long long) block_to_free, count);
  807. return 1;
  808. }
  809. err = ext4_ind_truncate_ensure_credits(handle, inode, bh,
  810. ext4_free_data_revoke_credits(inode, count));
  811. if (err < 0)
  812. goto out_err;
  813. for (p = first; p < last; p++)
  814. *p = 0;
  815. ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
  816. return 0;
  817. out_err:
  818. ext4_std_error(inode->i_sb, err);
  819. return err;
  820. }
  821. /**
  822. * ext4_free_data - free a list of data blocks
  823. * @handle: handle for this transaction
  824. * @inode: inode we are dealing with
  825. * @this_bh: indirect buffer_head which contains *@first and *@last
  826. * @first: array of block numbers
  827. * @last: points immediately past the end of array
  828. *
  829. * We are freeing all blocks referred from that array (numbers are stored as
  830. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  831. *
  832. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  833. * blocks are contiguous then releasing them at one time will only affect one
  834. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  835. * actually use a lot of journal space.
  836. *
  837. * @this_bh will be %NULL if @first and @last point into the inode's direct
  838. * block pointers.
  839. */
  840. static void ext4_free_data(handle_t *handle, struct inode *inode,
  841. struct buffer_head *this_bh,
  842. __le32 *first, __le32 *last)
  843. {
  844. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  845. unsigned long count = 0; /* Number of blocks in the run */
  846. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  847. corresponding to
  848. block_to_free */
  849. ext4_fsblk_t nr; /* Current block # */
  850. __le32 *p; /* Pointer into inode/ind
  851. for current block */
  852. int err = 0;
  853. if (this_bh) { /* For indirect block */
  854. BUFFER_TRACE(this_bh, "get_write_access");
  855. err = ext4_journal_get_write_access(handle, inode->i_sb,
  856. this_bh, EXT4_JTR_NONE);
  857. /* Important: if we can't update the indirect pointers
  858. * to the blocks, we can't free them. */
  859. if (err)
  860. return;
  861. }
  862. for (p = first; p < last; p++) {
  863. nr = le32_to_cpu(*p);
  864. if (nr) {
  865. /* accumulate blocks to free if they're contiguous */
  866. if (count == 0) {
  867. block_to_free = nr;
  868. block_to_free_p = p;
  869. count = 1;
  870. } else if (nr == block_to_free + count) {
  871. count++;
  872. } else {
  873. err = ext4_clear_blocks(handle, inode, this_bh,
  874. block_to_free, count,
  875. block_to_free_p, p);
  876. if (err)
  877. break;
  878. block_to_free = nr;
  879. block_to_free_p = p;
  880. count = 1;
  881. }
  882. }
  883. }
  884. if (!err && count > 0)
  885. err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  886. count, block_to_free_p, p);
  887. if (err < 0)
  888. /* fatal error */
  889. return;
  890. if (this_bh) {
  891. BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
  892. /*
  893. * The buffer head should have an attached journal head at this
  894. * point. However, if the data is corrupted and an indirect
  895. * block pointed to itself, it would have been detached when
  896. * the block was cleared. Check for this instead of OOPSing.
  897. */
  898. if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
  899. ext4_handle_dirty_metadata(handle, inode, this_bh);
  900. else
  901. EXT4_ERROR_INODE(inode,
  902. "circular indirect block detected at "
  903. "block %llu",
  904. (unsigned long long) this_bh->b_blocknr);
  905. }
  906. }
  907. /**
  908. * ext4_free_branches - free an array of branches
  909. * @handle: JBD handle for this transaction
  910. * @inode: inode we are dealing with
  911. * @parent_bh: the buffer_head which contains *@first and *@last
  912. * @first: array of block numbers
  913. * @last: pointer immediately past the end of array
  914. * @depth: depth of the branches to free
  915. *
  916. * We are freeing all blocks referred from these branches (numbers are
  917. * stored as little-endian 32-bit) and updating @inode->i_blocks
  918. * appropriately.
  919. */
  920. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  921. struct buffer_head *parent_bh,
  922. __le32 *first, __le32 *last, int depth)
  923. {
  924. ext4_fsblk_t nr;
  925. __le32 *p;
  926. if (ext4_handle_is_aborted(handle))
  927. return;
  928. if (depth--) {
  929. struct buffer_head *bh;
  930. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  931. p = last;
  932. while (--p >= first) {
  933. nr = le32_to_cpu(*p);
  934. if (!nr)
  935. continue; /* A hole */
  936. if (!ext4_inode_block_valid(inode, nr, 1)) {
  937. EXT4_ERROR_INODE(inode,
  938. "invalid indirect mapped "
  939. "block %lu (level %d)",
  940. (unsigned long) nr, depth);
  941. break;
  942. }
  943. /* Go read the buffer for the next level down */
  944. bh = ext4_sb_bread_nofail(inode->i_sb, nr);
  945. /*
  946. * A read failure? Report error and clear slot
  947. * (should be rare).
  948. */
  949. if (IS_ERR(bh)) {
  950. ext4_error_inode_block(inode, nr, -PTR_ERR(bh),
  951. "Read failure");
  952. continue;
  953. }
  954. /* This zaps the entire block. Bottom up. */
  955. BUFFER_TRACE(bh, "free child branches");
  956. ext4_free_branches(handle, inode, bh,
  957. (__le32 *) bh->b_data,
  958. (__le32 *) bh->b_data + addr_per_block,
  959. depth);
  960. brelse(bh);
  961. /*
  962. * Everything below this pointer has been
  963. * released. Now let this top-of-subtree go.
  964. *
  965. * We want the freeing of this indirect block to be
  966. * atomic in the journal with the updating of the
  967. * bitmap block which owns it. So make some room in
  968. * the journal.
  969. *
  970. * We zero the parent pointer *after* freeing its
  971. * pointee in the bitmaps, so if extend_transaction()
  972. * for some reason fails to put the bitmap changes and
  973. * the release into the same transaction, recovery
  974. * will merely complain about releasing a free block,
  975. * rather than leaking blocks.
  976. */
  977. if (ext4_handle_is_aborted(handle))
  978. return;
  979. if (ext4_ind_truncate_ensure_credits(handle, inode,
  980. NULL,
  981. ext4_free_metadata_revoke_credits(
  982. inode->i_sb, 1)) < 0)
  983. return;
  984. /*
  985. * The forget flag here is critical because if
  986. * we are journaling (and not doing data
  987. * journaling), we have to make sure a revoke
  988. * record is written to prevent the journal
  989. * replay from overwriting the (former)
  990. * indirect block if it gets reallocated as a
  991. * data block. This must happen in the same
  992. * transaction where the data blocks are
  993. * actually freed.
  994. */
  995. ext4_free_blocks(handle, inode, NULL, nr, 1,
  996. EXT4_FREE_BLOCKS_METADATA|
  997. EXT4_FREE_BLOCKS_FORGET);
  998. if (parent_bh) {
  999. /*
  1000. * The block which we have just freed is
  1001. * pointed to by an indirect block: journal it
  1002. */
  1003. BUFFER_TRACE(parent_bh, "get_write_access");
  1004. if (!ext4_journal_get_write_access(handle,
  1005. inode->i_sb, parent_bh,
  1006. EXT4_JTR_NONE)) {
  1007. *p = 0;
  1008. BUFFER_TRACE(parent_bh,
  1009. "call ext4_handle_dirty_metadata");
  1010. ext4_handle_dirty_metadata(handle,
  1011. inode,
  1012. parent_bh);
  1013. }
  1014. }
  1015. }
  1016. } else {
  1017. /* We have reached the bottom of the tree. */
  1018. BUFFER_TRACE(parent_bh, "free data blocks");
  1019. ext4_free_data(handle, inode, parent_bh, first, last);
  1020. }
  1021. }
  1022. void ext4_ind_truncate(handle_t *handle, struct inode *inode)
  1023. {
  1024. struct ext4_inode_info *ei = EXT4_I(inode);
  1025. __le32 *i_data = ei->i_data;
  1026. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1027. ext4_lblk_t offsets[4];
  1028. Indirect chain[4];
  1029. Indirect *partial;
  1030. __le32 nr = 0;
  1031. int n = 0;
  1032. ext4_lblk_t last_block, max_block;
  1033. unsigned blocksize = inode->i_sb->s_blocksize;
  1034. last_block = (inode->i_size + blocksize-1)
  1035. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1036. max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
  1037. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1038. if (last_block != max_block) {
  1039. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  1040. if (n == 0)
  1041. return;
  1042. }
  1043. ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
  1044. /*
  1045. * The orphan list entry will now protect us from any crash which
  1046. * occurs before the truncate completes, so it is now safe to propagate
  1047. * the new, shorter inode size (held for now in i_size) into the
  1048. * on-disk inode. We do this via i_disksize, which is the value which
  1049. * ext4 *really* writes onto the disk inode.
  1050. */
  1051. ei->i_disksize = inode->i_size;
  1052. if (last_block == max_block) {
  1053. /*
  1054. * It is unnecessary to free any data blocks if last_block is
  1055. * equal to the indirect block limit.
  1056. */
  1057. return;
  1058. } else if (n == 1) { /* direct blocks */
  1059. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  1060. i_data + EXT4_NDIR_BLOCKS);
  1061. goto do_indirects;
  1062. }
  1063. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  1064. /* Kill the top of shared branch (not detached) */
  1065. if (nr) {
  1066. if (partial == chain) {
  1067. /* Shared branch grows from the inode */
  1068. ext4_free_branches(handle, inode, NULL,
  1069. &nr, &nr+1, (chain+n-1) - partial);
  1070. *partial->p = 0;
  1071. /*
  1072. * We mark the inode dirty prior to restart,
  1073. * and prior to stop. No need for it here.
  1074. */
  1075. } else {
  1076. /* Shared branch grows from an indirect block */
  1077. BUFFER_TRACE(partial->bh, "get_write_access");
  1078. ext4_free_branches(handle, inode, partial->bh,
  1079. partial->p,
  1080. partial->p+1, (chain+n-1) - partial);
  1081. }
  1082. }
  1083. /* Clear the ends of indirect blocks on the shared branch */
  1084. while (partial > chain) {
  1085. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  1086. (__le32*)partial->bh->b_data+addr_per_block,
  1087. (chain+n-1) - partial);
  1088. BUFFER_TRACE(partial->bh, "call brelse");
  1089. brelse(partial->bh);
  1090. partial--;
  1091. }
  1092. do_indirects:
  1093. /* Kill the remaining (whole) subtrees */
  1094. switch (offsets[0]) {
  1095. default:
  1096. nr = i_data[EXT4_IND_BLOCK];
  1097. if (nr) {
  1098. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  1099. i_data[EXT4_IND_BLOCK] = 0;
  1100. }
  1101. fallthrough;
  1102. case EXT4_IND_BLOCK:
  1103. nr = i_data[EXT4_DIND_BLOCK];
  1104. if (nr) {
  1105. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  1106. i_data[EXT4_DIND_BLOCK] = 0;
  1107. }
  1108. fallthrough;
  1109. case EXT4_DIND_BLOCK:
  1110. nr = i_data[EXT4_TIND_BLOCK];
  1111. if (nr) {
  1112. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  1113. i_data[EXT4_TIND_BLOCK] = 0;
  1114. }
  1115. fallthrough;
  1116. case EXT4_TIND_BLOCK:
  1117. ;
  1118. }
  1119. }
  1120. /**
  1121. * ext4_ind_remove_space - remove space from the range
  1122. * @handle: JBD handle for this transaction
  1123. * @inode: inode we are dealing with
  1124. * @start: First block to remove
  1125. * @end: One block after the last block to remove (exclusive)
  1126. *
  1127. * Free the blocks in the defined range (end is exclusive endpoint of
  1128. * range). This is used by ext4_punch_hole().
  1129. */
  1130. int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
  1131. ext4_lblk_t start, ext4_lblk_t end)
  1132. {
  1133. struct ext4_inode_info *ei = EXT4_I(inode);
  1134. __le32 *i_data = ei->i_data;
  1135. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1136. ext4_lblk_t offsets[4], offsets2[4];
  1137. Indirect chain[4], chain2[4];
  1138. Indirect *partial, *partial2;
  1139. Indirect *p = NULL, *p2 = NULL;
  1140. ext4_lblk_t max_block;
  1141. __le32 nr = 0, nr2 = 0;
  1142. int n = 0, n2 = 0;
  1143. unsigned blocksize = inode->i_sb->s_blocksize;
  1144. max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
  1145. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1146. if (end >= max_block)
  1147. end = max_block;
  1148. if ((start >= end) || (start > max_block))
  1149. return 0;
  1150. n = ext4_block_to_path(inode, start, offsets, NULL);
  1151. n2 = ext4_block_to_path(inode, end, offsets2, NULL);
  1152. BUG_ON(n > n2);
  1153. if ((n == 1) && (n == n2)) {
  1154. /* We're punching only within direct block range */
  1155. ext4_free_data(handle, inode, NULL, i_data + offsets[0],
  1156. i_data + offsets2[0]);
  1157. return 0;
  1158. } else if (n2 > n) {
  1159. /*
  1160. * Start and end are on a different levels so we're going to
  1161. * free partial block at start, and partial block at end of
  1162. * the range. If there are some levels in between then
  1163. * do_indirects label will take care of that.
  1164. */
  1165. if (n == 1) {
  1166. /*
  1167. * Start is at the direct block level, free
  1168. * everything to the end of the level.
  1169. */
  1170. ext4_free_data(handle, inode, NULL, i_data + offsets[0],
  1171. i_data + EXT4_NDIR_BLOCKS);
  1172. goto end_range;
  1173. }
  1174. partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
  1175. if (nr) {
  1176. if (partial == chain) {
  1177. /* Shared branch grows from the inode */
  1178. ext4_free_branches(handle, inode, NULL,
  1179. &nr, &nr+1, (chain+n-1) - partial);
  1180. *partial->p = 0;
  1181. } else {
  1182. /* Shared branch grows from an indirect block */
  1183. BUFFER_TRACE(partial->bh, "get_write_access");
  1184. ext4_free_branches(handle, inode, partial->bh,
  1185. partial->p,
  1186. partial->p+1, (chain+n-1) - partial);
  1187. }
  1188. }
  1189. /*
  1190. * Clear the ends of indirect blocks on the shared branch
  1191. * at the start of the range
  1192. */
  1193. while (partial > chain) {
  1194. ext4_free_branches(handle, inode, partial->bh,
  1195. partial->p + 1,
  1196. (__le32 *)partial->bh->b_data+addr_per_block,
  1197. (chain+n-1) - partial);
  1198. partial--;
  1199. }
  1200. end_range:
  1201. partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
  1202. if (nr2) {
  1203. if (partial2 == chain2) {
  1204. /*
  1205. * Remember, end is exclusive so here we're at
  1206. * the start of the next level we're not going
  1207. * to free. Everything was covered by the start
  1208. * of the range.
  1209. */
  1210. goto do_indirects;
  1211. }
  1212. } else {
  1213. /*
  1214. * ext4_find_shared returns Indirect structure which
  1215. * points to the last element which should not be
  1216. * removed by truncate. But this is end of the range
  1217. * in punch_hole so we need to point to the next element
  1218. */
  1219. partial2->p++;
  1220. }
  1221. /*
  1222. * Clear the ends of indirect blocks on the shared branch
  1223. * at the end of the range
  1224. */
  1225. while (partial2 > chain2) {
  1226. ext4_free_branches(handle, inode, partial2->bh,
  1227. (__le32 *)partial2->bh->b_data,
  1228. partial2->p,
  1229. (chain2+n2-1) - partial2);
  1230. partial2--;
  1231. }
  1232. goto do_indirects;
  1233. }
  1234. /* Punch happened within the same level (n == n2) */
  1235. partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
  1236. partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
  1237. /* Free top, but only if partial2 isn't its subtree. */
  1238. if (nr) {
  1239. int level = min(partial - chain, partial2 - chain2);
  1240. int i;
  1241. int subtree = 1;
  1242. for (i = 0; i <= level; i++) {
  1243. if (offsets[i] != offsets2[i]) {
  1244. subtree = 0;
  1245. break;
  1246. }
  1247. }
  1248. if (!subtree) {
  1249. if (partial == chain) {
  1250. /* Shared branch grows from the inode */
  1251. ext4_free_branches(handle, inode, NULL,
  1252. &nr, &nr+1,
  1253. (chain+n-1) - partial);
  1254. *partial->p = 0;
  1255. } else {
  1256. /* Shared branch grows from an indirect block */
  1257. BUFFER_TRACE(partial->bh, "get_write_access");
  1258. ext4_free_branches(handle, inode, partial->bh,
  1259. partial->p,
  1260. partial->p+1,
  1261. (chain+n-1) - partial);
  1262. }
  1263. }
  1264. }
  1265. if (!nr2) {
  1266. /*
  1267. * ext4_find_shared returns Indirect structure which
  1268. * points to the last element which should not be
  1269. * removed by truncate. But this is end of the range
  1270. * in punch_hole so we need to point to the next element
  1271. */
  1272. partial2->p++;
  1273. }
  1274. while (partial > chain || partial2 > chain2) {
  1275. int depth = (chain+n-1) - partial;
  1276. int depth2 = (chain2+n2-1) - partial2;
  1277. if (partial > chain && partial2 > chain2 &&
  1278. partial->bh->b_blocknr == partial2->bh->b_blocknr) {
  1279. /*
  1280. * We've converged on the same block. Clear the range,
  1281. * then we're done.
  1282. */
  1283. ext4_free_branches(handle, inode, partial->bh,
  1284. partial->p + 1,
  1285. partial2->p,
  1286. (chain+n-1) - partial);
  1287. goto cleanup;
  1288. }
  1289. /*
  1290. * The start and end partial branches may not be at the same
  1291. * level even though the punch happened within one level. So, we
  1292. * give them a chance to arrive at the same level, then walk
  1293. * them in step with each other until we converge on the same
  1294. * block.
  1295. */
  1296. if (partial > chain && depth <= depth2) {
  1297. ext4_free_branches(handle, inode, partial->bh,
  1298. partial->p + 1,
  1299. (__le32 *)partial->bh->b_data+addr_per_block,
  1300. (chain+n-1) - partial);
  1301. partial--;
  1302. }
  1303. if (partial2 > chain2 && depth2 <= depth) {
  1304. ext4_free_branches(handle, inode, partial2->bh,
  1305. (__le32 *)partial2->bh->b_data,
  1306. partial2->p,
  1307. (chain2+n2-1) - partial2);
  1308. partial2--;
  1309. }
  1310. }
  1311. cleanup:
  1312. while (p && p > chain) {
  1313. BUFFER_TRACE(p->bh, "call brelse");
  1314. brelse(p->bh);
  1315. p--;
  1316. }
  1317. while (p2 && p2 > chain2) {
  1318. BUFFER_TRACE(p2->bh, "call brelse");
  1319. brelse(p2->bh);
  1320. p2--;
  1321. }
  1322. return 0;
  1323. do_indirects:
  1324. /* Kill the remaining (whole) subtrees */
  1325. switch (offsets[0]) {
  1326. default:
  1327. if (++n >= n2)
  1328. break;
  1329. nr = i_data[EXT4_IND_BLOCK];
  1330. if (nr) {
  1331. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  1332. i_data[EXT4_IND_BLOCK] = 0;
  1333. }
  1334. fallthrough;
  1335. case EXT4_IND_BLOCK:
  1336. if (++n >= n2)
  1337. break;
  1338. nr = i_data[EXT4_DIND_BLOCK];
  1339. if (nr) {
  1340. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  1341. i_data[EXT4_DIND_BLOCK] = 0;
  1342. }
  1343. fallthrough;
  1344. case EXT4_DIND_BLOCK:
  1345. if (++n >= n2)
  1346. break;
  1347. nr = i_data[EXT4_TIND_BLOCK];
  1348. if (nr) {
  1349. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  1350. i_data[EXT4_TIND_BLOCK] = 0;
  1351. }
  1352. fallthrough;
  1353. case EXT4_TIND_BLOCK:
  1354. ;
  1355. }
  1356. goto cleanup;
  1357. }