indirect.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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 "ext4_extents.h" /* Needed for EXT_MAX_BLOCKS */
  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, struct inode *inode,
  299. ext4_lblk_t iblock, int indirect_blks,
  300. int *blks, ext4_fsblk_t goal,
  301. ext4_lblk_t *offsets, Indirect *branch)
  302. {
  303. struct ext4_allocation_request ar;
  304. struct buffer_head * bh;
  305. ext4_fsblk_t b, new_blocks[4];
  306. __le32 *p;
  307. int i, j, err, len = 1;
  308. /*
  309. * Set up for the direct block allocation
  310. */
  311. memset(&ar, 0, sizeof(ar));
  312. ar.inode = inode;
  313. ar.len = *blks;
  314. ar.logical = iblock;
  315. if (S_ISREG(inode->i_mode))
  316. ar.flags = EXT4_MB_HINT_DATA;
  317. for (i = 0; i <= indirect_blks; i++) {
  318. if (i == indirect_blks) {
  319. ar.goal = goal;
  320. new_blocks[i] = ext4_mb_new_blocks(handle, &ar, &err);
  321. } else
  322. goal = new_blocks[i] = ext4_new_meta_blocks(handle, inode,
  323. goal, 0, NULL, &err);
  324. if (err) {
  325. i--;
  326. goto failed;
  327. }
  328. branch[i].key = cpu_to_le32(new_blocks[i]);
  329. if (i == 0)
  330. continue;
  331. bh = branch[i].bh = sb_getblk(inode->i_sb, new_blocks[i-1]);
  332. if (unlikely(!bh)) {
  333. err = -ENOMEM;
  334. goto failed;
  335. }
  336. lock_buffer(bh);
  337. BUFFER_TRACE(bh, "call get_create_access");
  338. err = ext4_journal_get_create_access(handle, bh);
  339. if (err) {
  340. unlock_buffer(bh);
  341. goto failed;
  342. }
  343. memset(bh->b_data, 0, bh->b_size);
  344. p = branch[i].p = (__le32 *) bh->b_data + offsets[i];
  345. b = new_blocks[i];
  346. if (i == indirect_blks)
  347. len = ar.len;
  348. for (j = 0; j < len; j++)
  349. *p++ = cpu_to_le32(b++);
  350. BUFFER_TRACE(bh, "marking uptodate");
  351. set_buffer_uptodate(bh);
  352. unlock_buffer(bh);
  353. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  354. err = ext4_handle_dirty_metadata(handle, inode, bh);
  355. if (err)
  356. goto failed;
  357. }
  358. *blks = ar.len;
  359. return 0;
  360. failed:
  361. for (; i >= 0; i--) {
  362. if (i != indirect_blks && branch[i].bh)
  363. ext4_forget(handle, 1, inode, branch[i].bh,
  364. branch[i].bh->b_blocknr);
  365. ext4_free_blocks(handle, inode, NULL, new_blocks[i],
  366. (i == indirect_blks) ? ar.len : 1, 0);
  367. }
  368. return err;
  369. }
  370. /**
  371. * ext4_splice_branch - splice the allocated branch onto inode.
  372. * @handle: handle for this transaction
  373. * @inode: owner
  374. * @block: (logical) number of block we are adding
  375. * @chain: chain of indirect blocks (with a missing link - see
  376. * ext4_alloc_branch)
  377. * @where: location of missing link
  378. * @num: number of indirect blocks we are adding
  379. * @blks: number of direct blocks we are adding
  380. *
  381. * This function fills the missing link and does all housekeeping needed in
  382. * inode (->i_blocks, etc.). In case of success we end up with the full
  383. * chain to new block and return 0.
  384. */
  385. static int ext4_splice_branch(handle_t *handle, struct inode *inode,
  386. ext4_lblk_t block, Indirect *where, int num,
  387. int blks)
  388. {
  389. int i;
  390. int err = 0;
  391. ext4_fsblk_t current_block;
  392. /*
  393. * If we're splicing into a [td]indirect block (as opposed to the
  394. * inode) then we need to get write access to the [td]indirect block
  395. * before the splice.
  396. */
  397. if (where->bh) {
  398. BUFFER_TRACE(where->bh, "get_write_access");
  399. err = ext4_journal_get_write_access(handle, where->bh);
  400. if (err)
  401. goto err_out;
  402. }
  403. /* That's it */
  404. *where->p = where->key;
  405. /*
  406. * Update the host buffer_head or inode to point to more just allocated
  407. * direct blocks blocks
  408. */
  409. if (num == 0 && blks > 1) {
  410. current_block = le32_to_cpu(where->key) + 1;
  411. for (i = 1; i < blks; i++)
  412. *(where->p + i) = cpu_to_le32(current_block++);
  413. }
  414. /* We are done with atomic stuff, now do the rest of housekeeping */
  415. /* had we spliced it onto indirect block? */
  416. if (where->bh) {
  417. /*
  418. * If we spliced it onto an indirect block, we haven't
  419. * altered the inode. Note however that if it is being spliced
  420. * onto an indirect block at the very end of the file (the
  421. * file is growing) then we *will* alter the inode to reflect
  422. * the new i_size. But that is not done here - it is done in
  423. * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode.
  424. */
  425. jbd_debug(5, "splicing indirect only\n");
  426. BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata");
  427. err = ext4_handle_dirty_metadata(handle, inode, where->bh);
  428. if (err)
  429. goto err_out;
  430. } else {
  431. /*
  432. * OK, we spliced it into the inode itself on a direct block.
  433. */
  434. ext4_mark_inode_dirty(handle, inode);
  435. jbd_debug(5, "splicing direct\n");
  436. }
  437. return err;
  438. err_out:
  439. for (i = 1; i <= num; i++) {
  440. /*
  441. * branch[i].bh is newly allocated, so there is no
  442. * need to revoke the block, which is why we don't
  443. * need to set EXT4_FREE_BLOCKS_METADATA.
  444. */
  445. ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
  446. EXT4_FREE_BLOCKS_FORGET);
  447. }
  448. ext4_free_blocks(handle, inode, NULL, le32_to_cpu(where[num].key),
  449. blks, 0);
  450. return err;
  451. }
  452. /*
  453. * The ext4_ind_map_blocks() function handles non-extents inodes
  454. * (i.e., using the traditional indirect/double-indirect i_blocks
  455. * scheme) for ext4_map_blocks().
  456. *
  457. * Allocation strategy is simple: if we have to allocate something, we will
  458. * have to go the whole way to leaf. So let's do it before attaching anything
  459. * to tree, set linkage between the newborn blocks, write them if sync is
  460. * required, recheck the path, free and repeat if check fails, otherwise
  461. * set the last missing link (that will protect us from any truncate-generated
  462. * removals - all blocks on the path are immune now) and possibly force the
  463. * write on the parent block.
  464. * That has a nice additional property: no special recovery from the failed
  465. * allocations is needed - we simply release blocks and do not touch anything
  466. * reachable from inode.
  467. *
  468. * `handle' can be NULL if create == 0.
  469. *
  470. * return > 0, # of blocks mapped or allocated.
  471. * return = 0, if plain lookup failed.
  472. * return < 0, error case.
  473. *
  474. * The ext4_ind_get_blocks() function should be called with
  475. * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem
  476. * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or
  477. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system
  478. * blocks.
  479. */
  480. int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
  481. struct ext4_map_blocks *map,
  482. int flags)
  483. {
  484. int err = -EIO;
  485. ext4_lblk_t offsets[4];
  486. Indirect chain[4];
  487. Indirect *partial;
  488. ext4_fsblk_t goal;
  489. int indirect_blks;
  490. int blocks_to_boundary = 0;
  491. int depth;
  492. int count = 0;
  493. ext4_fsblk_t first_block = 0;
  494. trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  495. J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)));
  496. J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0);
  497. depth = ext4_block_to_path(inode, map->m_lblk, offsets,
  498. &blocks_to_boundary);
  499. if (depth == 0)
  500. goto out;
  501. partial = ext4_get_branch(inode, depth, offsets, chain, &err);
  502. /* Simplest case - block found, no allocation needed */
  503. if (!partial) {
  504. first_block = le32_to_cpu(chain[depth - 1].key);
  505. count++;
  506. /*map more blocks*/
  507. while (count < map->m_len && count <= blocks_to_boundary) {
  508. ext4_fsblk_t blk;
  509. blk = le32_to_cpu(*(chain[depth-1].p + count));
  510. if (blk == first_block + count)
  511. count++;
  512. else
  513. break;
  514. }
  515. goto got_it;
  516. }
  517. /* Next simple case - plain lookup or failed read of indirect block */
  518. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO)
  519. goto cleanup;
  520. /*
  521. * Okay, we need to do block allocation.
  522. */
  523. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  524. EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
  525. EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
  526. "non-extent mapped inodes with bigalloc");
  527. return -ENOSPC;
  528. }
  529. goal = ext4_find_goal(inode, map->m_lblk, partial);
  530. /* the number of blocks need to allocate for [d,t]indirect blocks */
  531. indirect_blks = (chain + depth) - partial - 1;
  532. /*
  533. * Next look up the indirect map to count the totoal number of
  534. * direct blocks to allocate for this branch.
  535. */
  536. count = ext4_blks_to_allocate(partial, indirect_blks,
  537. map->m_len, blocks_to_boundary);
  538. /*
  539. * Block out ext4_truncate while we alter the tree
  540. */
  541. err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks,
  542. &count, goal,
  543. offsets + (partial - chain), partial);
  544. /*
  545. * The ext4_splice_branch call will free and forget any buffers
  546. * on the new chain if there is a failure, but that risks using
  547. * up transaction credits, especially for bitmaps where the
  548. * credits cannot be returned. Can we handle this somehow? We
  549. * may need to return -EAGAIN upwards in the worst case. --sct
  550. */
  551. if (!err)
  552. err = ext4_splice_branch(handle, inode, map->m_lblk,
  553. partial, indirect_blks, count);
  554. if (err)
  555. goto cleanup;
  556. map->m_flags |= EXT4_MAP_NEW;
  557. ext4_update_inode_fsync_trans(handle, inode, 1);
  558. got_it:
  559. map->m_flags |= EXT4_MAP_MAPPED;
  560. map->m_pblk = le32_to_cpu(chain[depth-1].key);
  561. map->m_len = count;
  562. if (count > blocks_to_boundary)
  563. map->m_flags |= EXT4_MAP_BOUNDARY;
  564. err = count;
  565. /* Clean up and exit */
  566. partial = chain + depth - 1; /* the whole chain */
  567. cleanup:
  568. while (partial > chain) {
  569. BUFFER_TRACE(partial->bh, "call brelse");
  570. brelse(partial->bh);
  571. partial--;
  572. }
  573. out:
  574. trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
  575. return err;
  576. }
  577. /*
  578. * O_DIRECT for ext3 (or indirect map) based files
  579. *
  580. * If the O_DIRECT write will extend the file then add this inode to the
  581. * orphan list. So recovery will truncate it back to the original size
  582. * if the machine crashes during the write.
  583. *
  584. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  585. * crashes then stale disk data _may_ be exposed inside the file. But current
  586. * VFS code falls back into buffered path in that case so we are safe.
  587. */
  588. ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
  589. const struct iovec *iov, loff_t offset,
  590. unsigned long nr_segs)
  591. {
  592. struct file *file = iocb->ki_filp;
  593. struct inode *inode = file->f_mapping->host;
  594. struct ext4_inode_info *ei = EXT4_I(inode);
  595. handle_t *handle;
  596. ssize_t ret;
  597. int orphan = 0;
  598. size_t count = iov_length(iov, nr_segs);
  599. int retries = 0;
  600. if (rw == WRITE) {
  601. loff_t final_size = offset + count;
  602. if (final_size > inode->i_size) {
  603. /* Credits for sb + inode write */
  604. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  605. if (IS_ERR(handle)) {
  606. ret = PTR_ERR(handle);
  607. goto out;
  608. }
  609. ret = ext4_orphan_add(handle, inode);
  610. if (ret) {
  611. ext4_journal_stop(handle);
  612. goto out;
  613. }
  614. orphan = 1;
  615. ei->i_disksize = inode->i_size;
  616. ext4_journal_stop(handle);
  617. }
  618. }
  619. retry:
  620. if (rw == READ && ext4_should_dioread_nolock(inode)) {
  621. /*
  622. * Nolock dioread optimization may be dynamically disabled
  623. * via ext4_inode_block_unlocked_dio(). Check inode's state
  624. * while holding extra i_dio_count ref.
  625. */
  626. atomic_inc(&inode->i_dio_count);
  627. smp_mb();
  628. if (unlikely(ext4_test_inode_state(inode,
  629. EXT4_STATE_DIOREAD_LOCK))) {
  630. inode_dio_done(inode);
  631. goto locked;
  632. }
  633. ret = __blockdev_direct_IO(rw, iocb, inode,
  634. inode->i_sb->s_bdev, iov,
  635. offset, nr_segs,
  636. ext4_get_block, NULL, NULL, 0);
  637. inode_dio_done(inode);
  638. } else {
  639. locked:
  640. ret = blockdev_direct_IO(rw, iocb, inode, iov,
  641. offset, nr_segs, ext4_get_block);
  642. if (unlikely((rw & WRITE) && ret < 0)) {
  643. loff_t isize = i_size_read(inode);
  644. loff_t end = offset + iov_length(iov, nr_segs);
  645. if (end > isize)
  646. ext4_truncate_failed_write(inode);
  647. }
  648. }
  649. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  650. goto retry;
  651. if (orphan) {
  652. int err;
  653. /* Credits for sb + inode write */
  654. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  655. if (IS_ERR(handle)) {
  656. /* This is really bad luck. We've written the data
  657. * but cannot extend i_size. Bail out and pretend
  658. * the write failed... */
  659. ret = PTR_ERR(handle);
  660. if (inode->i_nlink)
  661. ext4_orphan_del(NULL, inode);
  662. goto out;
  663. }
  664. if (inode->i_nlink)
  665. ext4_orphan_del(handle, inode);
  666. if (ret > 0) {
  667. loff_t end = offset + ret;
  668. if (end > inode->i_size) {
  669. ei->i_disksize = end;
  670. i_size_write(inode, end);
  671. /*
  672. * We're going to return a positive `ret'
  673. * here due to non-zero-length I/O, so there's
  674. * no way of reporting error returns from
  675. * ext4_mark_inode_dirty() to userspace. So
  676. * ignore it.
  677. */
  678. ext4_mark_inode_dirty(handle, inode);
  679. }
  680. }
  681. err = ext4_journal_stop(handle);
  682. if (ret == 0)
  683. ret = err;
  684. }
  685. out:
  686. return ret;
  687. }
  688. /*
  689. * Calculate the number of metadata blocks need to reserve
  690. * to allocate a new block at @lblocks for non extent file based file
  691. */
  692. int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock)
  693. {
  694. struct ext4_inode_info *ei = EXT4_I(inode);
  695. sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
  696. int blk_bits;
  697. if (lblock < EXT4_NDIR_BLOCKS)
  698. return 0;
  699. lblock -= EXT4_NDIR_BLOCKS;
  700. if (ei->i_da_metadata_calc_len &&
  701. (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
  702. ei->i_da_metadata_calc_len++;
  703. return 0;
  704. }
  705. ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
  706. ei->i_da_metadata_calc_len = 1;
  707. blk_bits = order_base_2(lblock);
  708. return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
  709. }
  710. /*
  711. * Calculate number of indirect blocks touched by mapping @nrblocks logically
  712. * contiguous blocks
  713. */
  714. int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
  715. {
  716. /*
  717. * With N contiguous data blocks, we need at most
  718. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
  719. * 2 dindirect blocks, and 1 tindirect block
  720. */
  721. return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
  722. }
  723. /*
  724. * Truncate transactions can be complex and absolutely huge. So we need to
  725. * be able to restart the transaction at a conventient checkpoint to make
  726. * sure we don't overflow the journal.
  727. *
  728. * Try to extend this transaction for the purposes of truncation. If
  729. * extend fails, we need to propagate the failure up and restart the
  730. * transaction in the top-level truncate loop. --sct
  731. *
  732. * Returns 0 if we managed to create more room. If we can't create more
  733. * room, and the transaction must be restarted we return 1.
  734. */
  735. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  736. {
  737. if (!ext4_handle_valid(handle))
  738. return 0;
  739. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  740. return 0;
  741. if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode)))
  742. return 0;
  743. return 1;
  744. }
  745. /*
  746. * Probably it should be a library function... search for first non-zero word
  747. * or memcmp with zero_page, whatever is better for particular architecture.
  748. * Linus?
  749. */
  750. static inline int all_zeroes(__le32 *p, __le32 *q)
  751. {
  752. while (p < q)
  753. if (*p++)
  754. return 0;
  755. return 1;
  756. }
  757. /**
  758. * ext4_find_shared - find the indirect blocks for partial truncation.
  759. * @inode: inode in question
  760. * @depth: depth of the affected branch
  761. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  762. * @chain: place to store the pointers to partial indirect blocks
  763. * @top: place to the (detached) top of branch
  764. *
  765. * This is a helper function used by ext4_truncate().
  766. *
  767. * When we do truncate() we may have to clean the ends of several
  768. * indirect blocks but leave the blocks themselves alive. Block is
  769. * partially truncated if some data below the new i_size is referred
  770. * from it (and it is on the path to the first completely truncated
  771. * data block, indeed). We have to free the top of that path along
  772. * with everything to the right of the path. Since no allocation
  773. * past the truncation point is possible until ext4_truncate()
  774. * finishes, we may safely do the latter, but top of branch may
  775. * require special attention - pageout below the truncation point
  776. * might try to populate it.
  777. *
  778. * We atomically detach the top of branch from the tree, store the
  779. * block number of its root in *@top, pointers to buffer_heads of
  780. * partially truncated blocks - in @chain[].bh and pointers to
  781. * their last elements that should not be removed - in
  782. * @chain[].p. Return value is the pointer to last filled element
  783. * of @chain.
  784. *
  785. * The work left to caller to do the actual freeing of subtrees:
  786. * a) free the subtree starting from *@top
  787. * b) free the subtrees whose roots are stored in
  788. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  789. * c) free the subtrees growing from the inode past the @chain[0].
  790. * (no partially truncated stuff there). */
  791. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  792. ext4_lblk_t offsets[4], Indirect chain[4],
  793. __le32 *top)
  794. {
  795. Indirect *partial, *p;
  796. int k, err;
  797. *top = 0;
  798. /* Make k index the deepest non-null offset + 1 */
  799. for (k = depth; k > 1 && !offsets[k-1]; k--)
  800. ;
  801. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  802. /* Writer: pointers */
  803. if (!partial)
  804. partial = chain + k-1;
  805. /*
  806. * If the branch acquired continuation since we've looked at it -
  807. * fine, it should all survive and (new) top doesn't belong to us.
  808. */
  809. if (!partial->key && *partial->p)
  810. /* Writer: end */
  811. goto no_top;
  812. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  813. ;
  814. /*
  815. * OK, we've found the last block that must survive. The rest of our
  816. * branch should be detached before unlocking. However, if that rest
  817. * of branch is all ours and does not grow immediately from the inode
  818. * it's easier to cheat and just decrement partial->p.
  819. */
  820. if (p == chain + k - 1 && p > chain) {
  821. p->p--;
  822. } else {
  823. *top = *p->p;
  824. /* Nope, don't do this in ext4. Must leave the tree intact */
  825. #if 0
  826. *p->p = 0;
  827. #endif
  828. }
  829. /* Writer: end */
  830. while (partial > p) {
  831. brelse(partial->bh);
  832. partial--;
  833. }
  834. no_top:
  835. return partial;
  836. }
  837. /*
  838. * Zero a number of block pointers in either an inode or an indirect block.
  839. * If we restart the transaction we must again get write access to the
  840. * indirect block for further modification.
  841. *
  842. * We release `count' blocks on disk, but (last - first) may be greater
  843. * than `count' because there can be holes in there.
  844. *
  845. * Return 0 on success, 1 on invalid block range
  846. * and < 0 on fatal error.
  847. */
  848. static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
  849. struct buffer_head *bh,
  850. ext4_fsblk_t block_to_free,
  851. unsigned long count, __le32 *first,
  852. __le32 *last)
  853. {
  854. __le32 *p;
  855. int flags = EXT4_FREE_BLOCKS_VALIDATED;
  856. int err;
  857. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  858. flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
  859. else if (ext4_should_journal_data(inode))
  860. flags |= EXT4_FREE_BLOCKS_FORGET;
  861. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
  862. count)) {
  863. EXT4_ERROR_INODE(inode, "attempt to clear invalid "
  864. "blocks %llu len %lu",
  865. (unsigned long long) block_to_free, count);
  866. return 1;
  867. }
  868. if (try_to_extend_transaction(handle, inode)) {
  869. if (bh) {
  870. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  871. err = ext4_handle_dirty_metadata(handle, inode, bh);
  872. if (unlikely(err))
  873. goto out_err;
  874. }
  875. err = ext4_mark_inode_dirty(handle, inode);
  876. if (unlikely(err))
  877. goto out_err;
  878. err = ext4_truncate_restart_trans(handle, inode,
  879. ext4_blocks_for_truncate(inode));
  880. if (unlikely(err))
  881. goto out_err;
  882. if (bh) {
  883. BUFFER_TRACE(bh, "retaking write access");
  884. err = ext4_journal_get_write_access(handle, bh);
  885. if (unlikely(err))
  886. goto out_err;
  887. }
  888. }
  889. for (p = first; p < last; p++)
  890. *p = 0;
  891. ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
  892. return 0;
  893. out_err:
  894. ext4_std_error(inode->i_sb, err);
  895. return err;
  896. }
  897. /**
  898. * ext4_free_data - free a list of data blocks
  899. * @handle: handle for this transaction
  900. * @inode: inode we are dealing with
  901. * @this_bh: indirect buffer_head which contains *@first and *@last
  902. * @first: array of block numbers
  903. * @last: points immediately past the end of array
  904. *
  905. * We are freeing all blocks referred from that array (numbers are stored as
  906. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  907. *
  908. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  909. * blocks are contiguous then releasing them at one time will only affect one
  910. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  911. * actually use a lot of journal space.
  912. *
  913. * @this_bh will be %NULL if @first and @last point into the inode's direct
  914. * block pointers.
  915. */
  916. static void ext4_free_data(handle_t *handle, struct inode *inode,
  917. struct buffer_head *this_bh,
  918. __le32 *first, __le32 *last)
  919. {
  920. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  921. unsigned long count = 0; /* Number of blocks in the run */
  922. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  923. corresponding to
  924. block_to_free */
  925. ext4_fsblk_t nr; /* Current block # */
  926. __le32 *p; /* Pointer into inode/ind
  927. for current block */
  928. int err = 0;
  929. if (this_bh) { /* For indirect block */
  930. BUFFER_TRACE(this_bh, "get_write_access");
  931. err = ext4_journal_get_write_access(handle, this_bh);
  932. /* Important: if we can't update the indirect pointers
  933. * to the blocks, we can't free them. */
  934. if (err)
  935. return;
  936. }
  937. for (p = first; p < last; p++) {
  938. nr = le32_to_cpu(*p);
  939. if (nr) {
  940. /* accumulate blocks to free if they're contiguous */
  941. if (count == 0) {
  942. block_to_free = nr;
  943. block_to_free_p = p;
  944. count = 1;
  945. } else if (nr == block_to_free + count) {
  946. count++;
  947. } else {
  948. err = ext4_clear_blocks(handle, inode, this_bh,
  949. block_to_free, count,
  950. block_to_free_p, p);
  951. if (err)
  952. break;
  953. block_to_free = nr;
  954. block_to_free_p = p;
  955. count = 1;
  956. }
  957. }
  958. }
  959. if (!err && count > 0)
  960. err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  961. count, block_to_free_p, p);
  962. if (err < 0)
  963. /* fatal error */
  964. return;
  965. if (this_bh) {
  966. BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
  967. /*
  968. * The buffer head should have an attached journal head at this
  969. * point. However, if the data is corrupted and an indirect
  970. * block pointed to itself, it would have been detached when
  971. * the block was cleared. Check for this instead of OOPSing.
  972. */
  973. if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
  974. ext4_handle_dirty_metadata(handle, inode, this_bh);
  975. else
  976. EXT4_ERROR_INODE(inode,
  977. "circular indirect block detected at "
  978. "block %llu",
  979. (unsigned long long) this_bh->b_blocknr);
  980. }
  981. }
  982. /**
  983. * ext4_free_branches - free an array of branches
  984. * @handle: JBD handle for this transaction
  985. * @inode: inode we are dealing with
  986. * @parent_bh: the buffer_head which contains *@first and *@last
  987. * @first: array of block numbers
  988. * @last: pointer immediately past the end of array
  989. * @depth: depth of the branches to free
  990. *
  991. * We are freeing all blocks referred from these branches (numbers are
  992. * stored as little-endian 32-bit) and updating @inode->i_blocks
  993. * appropriately.
  994. */
  995. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  996. struct buffer_head *parent_bh,
  997. __le32 *first, __le32 *last, int depth)
  998. {
  999. ext4_fsblk_t nr;
  1000. __le32 *p;
  1001. if (ext4_handle_is_aborted(handle))
  1002. return;
  1003. if (depth--) {
  1004. struct buffer_head *bh;
  1005. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1006. p = last;
  1007. while (--p >= first) {
  1008. nr = le32_to_cpu(*p);
  1009. if (!nr)
  1010. continue; /* A hole */
  1011. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
  1012. nr, 1)) {
  1013. EXT4_ERROR_INODE(inode,
  1014. "invalid indirect mapped "
  1015. "block %lu (level %d)",
  1016. (unsigned long) nr, depth);
  1017. break;
  1018. }
  1019. /* Go read the buffer for the next level down */
  1020. bh = sb_bread(inode->i_sb, nr);
  1021. /*
  1022. * A read failure? Report error and clear slot
  1023. * (should be rare).
  1024. */
  1025. if (!bh) {
  1026. EXT4_ERROR_INODE_BLOCK(inode, nr,
  1027. "Read failure");
  1028. continue;
  1029. }
  1030. /* This zaps the entire block. Bottom up. */
  1031. BUFFER_TRACE(bh, "free child branches");
  1032. ext4_free_branches(handle, inode, bh,
  1033. (__le32 *) bh->b_data,
  1034. (__le32 *) bh->b_data + addr_per_block,
  1035. depth);
  1036. brelse(bh);
  1037. /*
  1038. * Everything below this this pointer has been
  1039. * released. Now let this top-of-subtree go.
  1040. *
  1041. * We want the freeing of this indirect block to be
  1042. * atomic in the journal with the updating of the
  1043. * bitmap block which owns it. So make some room in
  1044. * the journal.
  1045. *
  1046. * We zero the parent pointer *after* freeing its
  1047. * pointee in the bitmaps, so if extend_transaction()
  1048. * for some reason fails to put the bitmap changes and
  1049. * the release into the same transaction, recovery
  1050. * will merely complain about releasing a free block,
  1051. * rather than leaking blocks.
  1052. */
  1053. if (ext4_handle_is_aborted(handle))
  1054. return;
  1055. if (try_to_extend_transaction(handle, inode)) {
  1056. ext4_mark_inode_dirty(handle, inode);
  1057. ext4_truncate_restart_trans(handle, inode,
  1058. ext4_blocks_for_truncate(inode));
  1059. }
  1060. /*
  1061. * The forget flag here is critical because if
  1062. * we are journaling (and not doing data
  1063. * journaling), we have to make sure a revoke
  1064. * record is written to prevent the journal
  1065. * replay from overwriting the (former)
  1066. * indirect block if it gets reallocated as a
  1067. * data block. This must happen in the same
  1068. * transaction where the data blocks are
  1069. * actually freed.
  1070. */
  1071. ext4_free_blocks(handle, inode, NULL, nr, 1,
  1072. EXT4_FREE_BLOCKS_METADATA|
  1073. EXT4_FREE_BLOCKS_FORGET);
  1074. if (parent_bh) {
  1075. /*
  1076. * The block which we have just freed is
  1077. * pointed to by an indirect block: journal it
  1078. */
  1079. BUFFER_TRACE(parent_bh, "get_write_access");
  1080. if (!ext4_journal_get_write_access(handle,
  1081. parent_bh)){
  1082. *p = 0;
  1083. BUFFER_TRACE(parent_bh,
  1084. "call ext4_handle_dirty_metadata");
  1085. ext4_handle_dirty_metadata(handle,
  1086. inode,
  1087. parent_bh);
  1088. }
  1089. }
  1090. }
  1091. } else {
  1092. /* We have reached the bottom of the tree. */
  1093. BUFFER_TRACE(parent_bh, "free data blocks");
  1094. ext4_free_data(handle, inode, parent_bh, first, last);
  1095. }
  1096. }
  1097. void ext4_ind_truncate(handle_t *handle, struct inode *inode)
  1098. {
  1099. struct ext4_inode_info *ei = EXT4_I(inode);
  1100. __le32 *i_data = ei->i_data;
  1101. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1102. ext4_lblk_t offsets[4];
  1103. Indirect chain[4];
  1104. Indirect *partial;
  1105. __le32 nr = 0;
  1106. int n = 0;
  1107. ext4_lblk_t last_block, max_block;
  1108. unsigned blocksize = inode->i_sb->s_blocksize;
  1109. last_block = (inode->i_size + blocksize-1)
  1110. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1111. max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
  1112. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1113. if (last_block != max_block) {
  1114. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  1115. if (n == 0)
  1116. return;
  1117. }
  1118. ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
  1119. /*
  1120. * The orphan list entry will now protect us from any crash which
  1121. * occurs before the truncate completes, so it is now safe to propagate
  1122. * the new, shorter inode size (held for now in i_size) into the
  1123. * on-disk inode. We do this via i_disksize, which is the value which
  1124. * ext4 *really* writes onto the disk inode.
  1125. */
  1126. ei->i_disksize = inode->i_size;
  1127. if (last_block == max_block) {
  1128. /*
  1129. * It is unnecessary to free any data blocks if last_block is
  1130. * equal to the indirect block limit.
  1131. */
  1132. return;
  1133. } else if (n == 1) { /* direct blocks */
  1134. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  1135. i_data + EXT4_NDIR_BLOCKS);
  1136. goto do_indirects;
  1137. }
  1138. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  1139. /* Kill the top of shared branch (not detached) */
  1140. if (nr) {
  1141. if (partial == chain) {
  1142. /* Shared branch grows from the inode */
  1143. ext4_free_branches(handle, inode, NULL,
  1144. &nr, &nr+1, (chain+n-1) - partial);
  1145. *partial->p = 0;
  1146. /*
  1147. * We mark the inode dirty prior to restart,
  1148. * and prior to stop. No need for it here.
  1149. */
  1150. } else {
  1151. /* Shared branch grows from an indirect block */
  1152. BUFFER_TRACE(partial->bh, "get_write_access");
  1153. ext4_free_branches(handle, inode, partial->bh,
  1154. partial->p,
  1155. partial->p+1, (chain+n-1) - partial);
  1156. }
  1157. }
  1158. /* Clear the ends of indirect blocks on the shared branch */
  1159. while (partial > chain) {
  1160. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  1161. (__le32*)partial->bh->b_data+addr_per_block,
  1162. (chain+n-1) - partial);
  1163. BUFFER_TRACE(partial->bh, "call brelse");
  1164. brelse(partial->bh);
  1165. partial--;
  1166. }
  1167. do_indirects:
  1168. /* Kill the remaining (whole) subtrees */
  1169. switch (offsets[0]) {
  1170. default:
  1171. nr = i_data[EXT4_IND_BLOCK];
  1172. if (nr) {
  1173. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  1174. i_data[EXT4_IND_BLOCK] = 0;
  1175. }
  1176. case EXT4_IND_BLOCK:
  1177. nr = i_data[EXT4_DIND_BLOCK];
  1178. if (nr) {
  1179. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  1180. i_data[EXT4_DIND_BLOCK] = 0;
  1181. }
  1182. case EXT4_DIND_BLOCK:
  1183. nr = i_data[EXT4_TIND_BLOCK];
  1184. if (nr) {
  1185. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  1186. i_data[EXT4_TIND_BLOCK] = 0;
  1187. }
  1188. case EXT4_TIND_BLOCK:
  1189. ;
  1190. }
  1191. }
  1192. static int free_hole_blocks(handle_t *handle, struct inode *inode,
  1193. struct buffer_head *parent_bh, __le32 *i_data,
  1194. int level, ext4_lblk_t first,
  1195. ext4_lblk_t count, int max)
  1196. {
  1197. struct buffer_head *bh = NULL;
  1198. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1199. int ret = 0;
  1200. int i, inc;
  1201. ext4_lblk_t offset;
  1202. __le32 blk;
  1203. inc = 1 << ((EXT4_BLOCK_SIZE_BITS(inode->i_sb) - 2) * level);
  1204. for (i = 0, offset = 0; i < max; i++, i_data++, offset += inc) {
  1205. if (offset >= count + first)
  1206. break;
  1207. if (*i_data == 0 || (offset + inc) <= first)
  1208. continue;
  1209. blk = *i_data;
  1210. if (level > 0) {
  1211. ext4_lblk_t first2;
  1212. bh = sb_bread(inode->i_sb, le32_to_cpu(blk));
  1213. if (!bh) {
  1214. EXT4_ERROR_INODE_BLOCK(inode, le32_to_cpu(blk),
  1215. "Read failure");
  1216. return -EIO;
  1217. }
  1218. first2 = (first > offset) ? first - offset : 0;
  1219. ret = free_hole_blocks(handle, inode, bh,
  1220. (__le32 *)bh->b_data, level - 1,
  1221. first2, count - offset,
  1222. inode->i_sb->s_blocksize >> 2);
  1223. if (ret) {
  1224. brelse(bh);
  1225. goto err;
  1226. }
  1227. }
  1228. if (level == 0 ||
  1229. (bh && all_zeroes((__le32 *)bh->b_data,
  1230. (__le32 *)bh->b_data + addr_per_block))) {
  1231. ext4_free_data(handle, inode, parent_bh, &blk, &blk+1);
  1232. *i_data = 0;
  1233. }
  1234. brelse(bh);
  1235. bh = NULL;
  1236. }
  1237. err:
  1238. return ret;
  1239. }
  1240. int ext4_free_hole_blocks(handle_t *handle, struct inode *inode,
  1241. ext4_lblk_t first, ext4_lblk_t stop)
  1242. {
  1243. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1244. int level, ret = 0;
  1245. int num = EXT4_NDIR_BLOCKS;
  1246. ext4_lblk_t count, max = EXT4_NDIR_BLOCKS;
  1247. __le32 *i_data = EXT4_I(inode)->i_data;
  1248. count = stop - first;
  1249. for (level = 0; level < 4; level++, max *= addr_per_block) {
  1250. if (first < max) {
  1251. ret = free_hole_blocks(handle, inode, NULL, i_data,
  1252. level, first, count, num);
  1253. if (ret)
  1254. goto err;
  1255. if (count > max - first)
  1256. count -= max - first;
  1257. else
  1258. break;
  1259. first = 0;
  1260. } else {
  1261. first -= max;
  1262. }
  1263. i_data += num;
  1264. if (level == 0) {
  1265. num = 1;
  1266. max = 1;
  1267. }
  1268. }
  1269. err:
  1270. return ret;
  1271. }