indirect.c 46 KB

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