indirect.c 46 KB

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