indirect.c 43 KB

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