indirect.c 46 KB

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