indirect.c 44 KB

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