indirect.c 44 KB

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