indirect.c 44 KB

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