indirect.c 43 KB

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