indirect.c 45 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_feature_bigalloc(inode->i_sb)) {
  520. EXT4_ERROR_INODE(inode, "Can't allocate blocks for "
  521. "non-extent mapped inodes with bigalloc");
  522. return -EFSCORRUPTED;
  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. if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  533. ar.flags |= EXT4_MB_USE_RESERVED;
  534. ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
  535. /* the number of blocks need to allocate for [d,t]indirect blocks */
  536. indirect_blks = (chain + depth) - partial - 1;
  537. /*
  538. * Next look up the indirect map to count the totoal number of
  539. * direct blocks to allocate for this branch.
  540. */
  541. ar.len = ext4_blks_to_allocate(partial, indirect_blks,
  542. map->m_len, blocks_to_boundary);
  543. /*
  544. * Block out ext4_truncate while we alter the tree
  545. */
  546. err = ext4_alloc_branch(handle, &ar, indirect_blks,
  547. offsets + (partial - chain), partial);
  548. /*
  549. * The ext4_splice_branch call will free and forget any buffers
  550. * on the new chain if there is a failure, but that risks using
  551. * up transaction credits, especially for bitmaps where the
  552. * credits cannot be returned. Can we handle this somehow? We
  553. * may need to return -EAGAIN upwards in the worst case. --sct
  554. */
  555. if (!err)
  556. err = ext4_splice_branch(handle, &ar, partial, indirect_blks);
  557. if (err)
  558. goto cleanup;
  559. map->m_flags |= EXT4_MAP_NEW;
  560. ext4_update_inode_fsync_trans(handle, inode, 1);
  561. count = ar.len;
  562. got_it:
  563. map->m_flags |= EXT4_MAP_MAPPED;
  564. map->m_pblk = le32_to_cpu(chain[depth-1].key);
  565. map->m_len = count;
  566. if (count > blocks_to_boundary)
  567. map->m_flags |= EXT4_MAP_BOUNDARY;
  568. err = count;
  569. /* Clean up and exit */
  570. partial = chain + depth - 1; /* the whole chain */
  571. cleanup:
  572. while (partial > chain) {
  573. BUFFER_TRACE(partial->bh, "call brelse");
  574. brelse(partial->bh);
  575. partial--;
  576. }
  577. out:
  578. trace_ext4_ind_map_blocks_exit(inode, flags, map, err);
  579. return err;
  580. }
  581. /*
  582. * O_DIRECT for ext3 (or indirect map) based files
  583. *
  584. * If the O_DIRECT write will extend the file then add this inode to the
  585. * orphan list. So recovery will truncate it back to the original size
  586. * if the machine crashes during the write.
  587. *
  588. * If the O_DIRECT write is intantiating holes inside i_size and the machine
  589. * crashes then stale disk data _may_ be exposed inside the file. But current
  590. * VFS code falls back into buffered path in that case so we are safe.
  591. */
  592. ssize_t ext4_ind_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
  593. loff_t offset)
  594. {
  595. struct file *file = iocb->ki_filp;
  596. struct inode *inode = file->f_mapping->host;
  597. struct ext4_inode_info *ei = EXT4_I(inode);
  598. handle_t *handle;
  599. ssize_t ret;
  600. int orphan = 0;
  601. size_t count = iov_iter_count(iter);
  602. int retries = 0;
  603. if (iov_iter_rw(iter) == WRITE) {
  604. loff_t final_size = offset + count;
  605. if (final_size > inode->i_size) {
  606. /* Credits for sb + inode write */
  607. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  608. if (IS_ERR(handle)) {
  609. ret = PTR_ERR(handle);
  610. goto out;
  611. }
  612. ret = ext4_orphan_add(handle, inode);
  613. if (ret) {
  614. ext4_journal_stop(handle);
  615. goto out;
  616. }
  617. orphan = 1;
  618. ei->i_disksize = inode->i_size;
  619. ext4_journal_stop(handle);
  620. }
  621. }
  622. retry:
  623. if (iov_iter_rw(iter) == READ && ext4_should_dioread_nolock(inode)) {
  624. /*
  625. * Nolock dioread optimization may be dynamically disabled
  626. * via ext4_inode_block_unlocked_dio(). Check inode's state
  627. * while holding extra i_dio_count ref.
  628. */
  629. inode_dio_begin(inode);
  630. smp_mb();
  631. if (unlikely(ext4_test_inode_state(inode,
  632. EXT4_STATE_DIOREAD_LOCK))) {
  633. inode_dio_end(inode);
  634. goto locked;
  635. }
  636. if (IS_DAX(inode))
  637. ret = dax_do_io(iocb, inode, iter, offset,
  638. ext4_get_block, NULL, 0);
  639. else
  640. ret = __blockdev_direct_IO(iocb, inode,
  641. inode->i_sb->s_bdev, iter,
  642. offset, ext4_get_block, NULL,
  643. NULL, 0);
  644. inode_dio_end(inode);
  645. } else {
  646. locked:
  647. if (IS_DAX(inode))
  648. ret = dax_do_io(iocb, inode, iter, offset,
  649. ext4_get_block, NULL, DIO_LOCKING);
  650. else
  651. ret = blockdev_direct_IO(iocb, inode, iter, offset,
  652. ext4_get_block);
  653. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  654. loff_t isize = i_size_read(inode);
  655. loff_t end = offset + count;
  656. if (end > isize)
  657. ext4_truncate_failed_write(inode);
  658. }
  659. }
  660. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  661. goto retry;
  662. if (orphan) {
  663. int err;
  664. /* Credits for sb + inode write */
  665. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  666. if (IS_ERR(handle)) {
  667. /* This is really bad luck. We've written the data
  668. * but cannot extend i_size. Bail out and pretend
  669. * the write failed... */
  670. ret = PTR_ERR(handle);
  671. if (inode->i_nlink)
  672. ext4_orphan_del(NULL, inode);
  673. goto out;
  674. }
  675. if (inode->i_nlink)
  676. ext4_orphan_del(handle, inode);
  677. if (ret > 0) {
  678. loff_t end = offset + ret;
  679. if (end > inode->i_size) {
  680. ei->i_disksize = end;
  681. i_size_write(inode, end);
  682. /*
  683. * We're going to return a positive `ret'
  684. * here due to non-zero-length I/O, so there's
  685. * no way of reporting error returns from
  686. * ext4_mark_inode_dirty() to userspace. So
  687. * ignore it.
  688. */
  689. ext4_mark_inode_dirty(handle, inode);
  690. }
  691. }
  692. err = ext4_journal_stop(handle);
  693. if (ret == 0)
  694. ret = err;
  695. }
  696. out:
  697. return ret;
  698. }
  699. /*
  700. * Calculate the number of metadata blocks need to reserve
  701. * to allocate a new block at @lblocks for non extent file based file
  702. */
  703. int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock)
  704. {
  705. struct ext4_inode_info *ei = EXT4_I(inode);
  706. sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1);
  707. int blk_bits;
  708. if (lblock < EXT4_NDIR_BLOCKS)
  709. return 0;
  710. lblock -= EXT4_NDIR_BLOCKS;
  711. if (ei->i_da_metadata_calc_len &&
  712. (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) {
  713. ei->i_da_metadata_calc_len++;
  714. return 0;
  715. }
  716. ei->i_da_metadata_calc_last_lblock = lblock & dind_mask;
  717. ei->i_da_metadata_calc_len = 1;
  718. blk_bits = order_base_2(lblock);
  719. return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1;
  720. }
  721. /*
  722. * Calculate number of indirect blocks touched by mapping @nrblocks logically
  723. * contiguous blocks
  724. */
  725. int ext4_ind_trans_blocks(struct inode *inode, int nrblocks)
  726. {
  727. /*
  728. * With N contiguous data blocks, we need at most
  729. * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks,
  730. * 2 dindirect blocks, and 1 tindirect block
  731. */
  732. return DIV_ROUND_UP(nrblocks, EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4;
  733. }
  734. /*
  735. * Truncate transactions can be complex and absolutely huge. So we need to
  736. * be able to restart the transaction at a conventient checkpoint to make
  737. * sure we don't overflow the journal.
  738. *
  739. * Try to extend this transaction for the purposes of truncation. If
  740. * extend fails, we need to propagate the failure up and restart the
  741. * transaction in the top-level truncate loop. --sct
  742. *
  743. * Returns 0 if we managed to create more room. If we can't create more
  744. * room, and the transaction must be restarted we return 1.
  745. */
  746. static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
  747. {
  748. if (!ext4_handle_valid(handle))
  749. return 0;
  750. if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1))
  751. return 0;
  752. if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode)))
  753. return 0;
  754. return 1;
  755. }
  756. /*
  757. * Probably it should be a library function... search for first non-zero word
  758. * or memcmp with zero_page, whatever is better for particular architecture.
  759. * Linus?
  760. */
  761. static inline int all_zeroes(__le32 *p, __le32 *q)
  762. {
  763. while (p < q)
  764. if (*p++)
  765. return 0;
  766. return 1;
  767. }
  768. /**
  769. * ext4_find_shared - find the indirect blocks for partial truncation.
  770. * @inode: inode in question
  771. * @depth: depth of the affected branch
  772. * @offsets: offsets of pointers in that branch (see ext4_block_to_path)
  773. * @chain: place to store the pointers to partial indirect blocks
  774. * @top: place to the (detached) top of branch
  775. *
  776. * This is a helper function used by ext4_truncate().
  777. *
  778. * When we do truncate() we may have to clean the ends of several
  779. * indirect blocks but leave the blocks themselves alive. Block is
  780. * partially truncated if some data below the new i_size is referred
  781. * from it (and it is on the path to the first completely truncated
  782. * data block, indeed). We have to free the top of that path along
  783. * with everything to the right of the path. Since no allocation
  784. * past the truncation point is possible until ext4_truncate()
  785. * finishes, we may safely do the latter, but top of branch may
  786. * require special attention - pageout below the truncation point
  787. * might try to populate it.
  788. *
  789. * We atomically detach the top of branch from the tree, store the
  790. * block number of its root in *@top, pointers to buffer_heads of
  791. * partially truncated blocks - in @chain[].bh and pointers to
  792. * their last elements that should not be removed - in
  793. * @chain[].p. Return value is the pointer to last filled element
  794. * of @chain.
  795. *
  796. * The work left to caller to do the actual freeing of subtrees:
  797. * a) free the subtree starting from *@top
  798. * b) free the subtrees whose roots are stored in
  799. * (@chain[i].p+1 .. end of @chain[i].bh->b_data)
  800. * c) free the subtrees growing from the inode past the @chain[0].
  801. * (no partially truncated stuff there). */
  802. static Indirect *ext4_find_shared(struct inode *inode, int depth,
  803. ext4_lblk_t offsets[4], Indirect chain[4],
  804. __le32 *top)
  805. {
  806. Indirect *partial, *p;
  807. int k, err;
  808. *top = 0;
  809. /* Make k index the deepest non-null offset + 1 */
  810. for (k = depth; k > 1 && !offsets[k-1]; k--)
  811. ;
  812. partial = ext4_get_branch(inode, k, offsets, chain, &err);
  813. /* Writer: pointers */
  814. if (!partial)
  815. partial = chain + k-1;
  816. /*
  817. * If the branch acquired continuation since we've looked at it -
  818. * fine, it should all survive and (new) top doesn't belong to us.
  819. */
  820. if (!partial->key && *partial->p)
  821. /* Writer: end */
  822. goto no_top;
  823. for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--)
  824. ;
  825. /*
  826. * OK, we've found the last block that must survive. The rest of our
  827. * branch should be detached before unlocking. However, if that rest
  828. * of branch is all ours and does not grow immediately from the inode
  829. * it's easier to cheat and just decrement partial->p.
  830. */
  831. if (p == chain + k - 1 && p > chain) {
  832. p->p--;
  833. } else {
  834. *top = *p->p;
  835. /* Nope, don't do this in ext4. Must leave the tree intact */
  836. #if 0
  837. *p->p = 0;
  838. #endif
  839. }
  840. /* Writer: end */
  841. while (partial > p) {
  842. brelse(partial->bh);
  843. partial--;
  844. }
  845. no_top:
  846. return partial;
  847. }
  848. /*
  849. * Zero a number of block pointers in either an inode or an indirect block.
  850. * If we restart the transaction we must again get write access to the
  851. * indirect block for further modification.
  852. *
  853. * We release `count' blocks on disk, but (last - first) may be greater
  854. * than `count' because there can be holes in there.
  855. *
  856. * Return 0 on success, 1 on invalid block range
  857. * and < 0 on fatal error.
  858. */
  859. static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
  860. struct buffer_head *bh,
  861. ext4_fsblk_t block_to_free,
  862. unsigned long count, __le32 *first,
  863. __le32 *last)
  864. {
  865. __le32 *p;
  866. int flags = EXT4_FREE_BLOCKS_VALIDATED;
  867. int err;
  868. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  869. flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
  870. else if (ext4_should_journal_data(inode))
  871. flags |= EXT4_FREE_BLOCKS_FORGET;
  872. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free,
  873. count)) {
  874. EXT4_ERROR_INODE(inode, "attempt to clear invalid "
  875. "blocks %llu len %lu",
  876. (unsigned long long) block_to_free, count);
  877. return 1;
  878. }
  879. if (try_to_extend_transaction(handle, inode)) {
  880. if (bh) {
  881. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  882. err = ext4_handle_dirty_metadata(handle, inode, bh);
  883. if (unlikely(err))
  884. goto out_err;
  885. }
  886. err = ext4_mark_inode_dirty(handle, inode);
  887. if (unlikely(err))
  888. goto out_err;
  889. err = ext4_truncate_restart_trans(handle, inode,
  890. ext4_blocks_for_truncate(inode));
  891. if (unlikely(err))
  892. goto out_err;
  893. if (bh) {
  894. BUFFER_TRACE(bh, "retaking write access");
  895. err = ext4_journal_get_write_access(handle, bh);
  896. if (unlikely(err))
  897. goto out_err;
  898. }
  899. }
  900. for (p = first; p < last; p++)
  901. *p = 0;
  902. ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags);
  903. return 0;
  904. out_err:
  905. ext4_std_error(inode->i_sb, err);
  906. return err;
  907. }
  908. /**
  909. * ext4_free_data - free a list of data blocks
  910. * @handle: handle for this transaction
  911. * @inode: inode we are dealing with
  912. * @this_bh: indirect buffer_head which contains *@first and *@last
  913. * @first: array of block numbers
  914. * @last: points immediately past the end of array
  915. *
  916. * We are freeing all blocks referred from that array (numbers are stored as
  917. * little-endian 32-bit) and updating @inode->i_blocks appropriately.
  918. *
  919. * We accumulate contiguous runs of blocks to free. Conveniently, if these
  920. * blocks are contiguous then releasing them at one time will only affect one
  921. * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't
  922. * actually use a lot of journal space.
  923. *
  924. * @this_bh will be %NULL if @first and @last point into the inode's direct
  925. * block pointers.
  926. */
  927. static void ext4_free_data(handle_t *handle, struct inode *inode,
  928. struct buffer_head *this_bh,
  929. __le32 *first, __le32 *last)
  930. {
  931. ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */
  932. unsigned long count = 0; /* Number of blocks in the run */
  933. __le32 *block_to_free_p = NULL; /* Pointer into inode/ind
  934. corresponding to
  935. block_to_free */
  936. ext4_fsblk_t nr; /* Current block # */
  937. __le32 *p; /* Pointer into inode/ind
  938. for current block */
  939. int err = 0;
  940. if (this_bh) { /* For indirect block */
  941. BUFFER_TRACE(this_bh, "get_write_access");
  942. err = ext4_journal_get_write_access(handle, this_bh);
  943. /* Important: if we can't update the indirect pointers
  944. * to the blocks, we can't free them. */
  945. if (err)
  946. return;
  947. }
  948. for (p = first; p < last; p++) {
  949. nr = le32_to_cpu(*p);
  950. if (nr) {
  951. /* accumulate blocks to free if they're contiguous */
  952. if (count == 0) {
  953. block_to_free = nr;
  954. block_to_free_p = p;
  955. count = 1;
  956. } else if (nr == block_to_free + count) {
  957. count++;
  958. } else {
  959. err = ext4_clear_blocks(handle, inode, this_bh,
  960. block_to_free, count,
  961. block_to_free_p, p);
  962. if (err)
  963. break;
  964. block_to_free = nr;
  965. block_to_free_p = p;
  966. count = 1;
  967. }
  968. }
  969. }
  970. if (!err && count > 0)
  971. err = ext4_clear_blocks(handle, inode, this_bh, block_to_free,
  972. count, block_to_free_p, p);
  973. if (err < 0)
  974. /* fatal error */
  975. return;
  976. if (this_bh) {
  977. BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata");
  978. /*
  979. * The buffer head should have an attached journal head at this
  980. * point. However, if the data is corrupted and an indirect
  981. * block pointed to itself, it would have been detached when
  982. * the block was cleared. Check for this instead of OOPSing.
  983. */
  984. if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh))
  985. ext4_handle_dirty_metadata(handle, inode, this_bh);
  986. else
  987. EXT4_ERROR_INODE(inode,
  988. "circular indirect block detected at "
  989. "block %llu",
  990. (unsigned long long) this_bh->b_blocknr);
  991. }
  992. }
  993. /**
  994. * ext4_free_branches - free an array of branches
  995. * @handle: JBD handle for this transaction
  996. * @inode: inode we are dealing with
  997. * @parent_bh: the buffer_head which contains *@first and *@last
  998. * @first: array of block numbers
  999. * @last: pointer immediately past the end of array
  1000. * @depth: depth of the branches to free
  1001. *
  1002. * We are freeing all blocks referred from these branches (numbers are
  1003. * stored as little-endian 32-bit) and updating @inode->i_blocks
  1004. * appropriately.
  1005. */
  1006. static void ext4_free_branches(handle_t *handle, struct inode *inode,
  1007. struct buffer_head *parent_bh,
  1008. __le32 *first, __le32 *last, int depth)
  1009. {
  1010. ext4_fsblk_t nr;
  1011. __le32 *p;
  1012. if (ext4_handle_is_aborted(handle))
  1013. return;
  1014. if (depth--) {
  1015. struct buffer_head *bh;
  1016. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1017. p = last;
  1018. while (--p >= first) {
  1019. nr = le32_to_cpu(*p);
  1020. if (!nr)
  1021. continue; /* A hole */
  1022. if (!ext4_data_block_valid(EXT4_SB(inode->i_sb),
  1023. nr, 1)) {
  1024. EXT4_ERROR_INODE(inode,
  1025. "invalid indirect mapped "
  1026. "block %lu (level %d)",
  1027. (unsigned long) nr, depth);
  1028. break;
  1029. }
  1030. /* Go read the buffer for the next level down */
  1031. bh = sb_bread(inode->i_sb, nr);
  1032. /*
  1033. * A read failure? Report error and clear slot
  1034. * (should be rare).
  1035. */
  1036. if (!bh) {
  1037. EXT4_ERROR_INODE_BLOCK(inode, nr,
  1038. "Read failure");
  1039. continue;
  1040. }
  1041. /* This zaps the entire block. Bottom up. */
  1042. BUFFER_TRACE(bh, "free child branches");
  1043. ext4_free_branches(handle, inode, bh,
  1044. (__le32 *) bh->b_data,
  1045. (__le32 *) bh->b_data + addr_per_block,
  1046. depth);
  1047. brelse(bh);
  1048. /*
  1049. * Everything below this this pointer has been
  1050. * released. Now let this top-of-subtree go.
  1051. *
  1052. * We want the freeing of this indirect block to be
  1053. * atomic in the journal with the updating of the
  1054. * bitmap block which owns it. So make some room in
  1055. * the journal.
  1056. *
  1057. * We zero the parent pointer *after* freeing its
  1058. * pointee in the bitmaps, so if extend_transaction()
  1059. * for some reason fails to put the bitmap changes and
  1060. * the release into the same transaction, recovery
  1061. * will merely complain about releasing a free block,
  1062. * rather than leaking blocks.
  1063. */
  1064. if (ext4_handle_is_aborted(handle))
  1065. return;
  1066. if (try_to_extend_transaction(handle, inode)) {
  1067. ext4_mark_inode_dirty(handle, inode);
  1068. ext4_truncate_restart_trans(handle, inode,
  1069. ext4_blocks_for_truncate(inode));
  1070. }
  1071. /*
  1072. * The forget flag here is critical because if
  1073. * we are journaling (and not doing data
  1074. * journaling), we have to make sure a revoke
  1075. * record is written to prevent the journal
  1076. * replay from overwriting the (former)
  1077. * indirect block if it gets reallocated as a
  1078. * data block. This must happen in the same
  1079. * transaction where the data blocks are
  1080. * actually freed.
  1081. */
  1082. ext4_free_blocks(handle, inode, NULL, nr, 1,
  1083. EXT4_FREE_BLOCKS_METADATA|
  1084. EXT4_FREE_BLOCKS_FORGET);
  1085. if (parent_bh) {
  1086. /*
  1087. * The block which we have just freed is
  1088. * pointed to by an indirect block: journal it
  1089. */
  1090. BUFFER_TRACE(parent_bh, "get_write_access");
  1091. if (!ext4_journal_get_write_access(handle,
  1092. parent_bh)){
  1093. *p = 0;
  1094. BUFFER_TRACE(parent_bh,
  1095. "call ext4_handle_dirty_metadata");
  1096. ext4_handle_dirty_metadata(handle,
  1097. inode,
  1098. parent_bh);
  1099. }
  1100. }
  1101. }
  1102. } else {
  1103. /* We have reached the bottom of the tree. */
  1104. BUFFER_TRACE(parent_bh, "free data blocks");
  1105. ext4_free_data(handle, inode, parent_bh, first, last);
  1106. }
  1107. }
  1108. void ext4_ind_truncate(handle_t *handle, struct inode *inode)
  1109. {
  1110. struct ext4_inode_info *ei = EXT4_I(inode);
  1111. __le32 *i_data = ei->i_data;
  1112. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1113. ext4_lblk_t offsets[4];
  1114. Indirect chain[4];
  1115. Indirect *partial;
  1116. __le32 nr = 0;
  1117. int n = 0;
  1118. ext4_lblk_t last_block, max_block;
  1119. unsigned blocksize = inode->i_sb->s_blocksize;
  1120. last_block = (inode->i_size + blocksize-1)
  1121. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1122. max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1)
  1123. >> EXT4_BLOCK_SIZE_BITS(inode->i_sb);
  1124. if (last_block != max_block) {
  1125. n = ext4_block_to_path(inode, last_block, offsets, NULL);
  1126. if (n == 0)
  1127. return;
  1128. }
  1129. ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block);
  1130. /*
  1131. * The orphan list entry will now protect us from any crash which
  1132. * occurs before the truncate completes, so it is now safe to propagate
  1133. * the new, shorter inode size (held for now in i_size) into the
  1134. * on-disk inode. We do this via i_disksize, which is the value which
  1135. * ext4 *really* writes onto the disk inode.
  1136. */
  1137. ei->i_disksize = inode->i_size;
  1138. if (last_block == max_block) {
  1139. /*
  1140. * It is unnecessary to free any data blocks if last_block is
  1141. * equal to the indirect block limit.
  1142. */
  1143. return;
  1144. } else if (n == 1) { /* direct blocks */
  1145. ext4_free_data(handle, inode, NULL, i_data+offsets[0],
  1146. i_data + EXT4_NDIR_BLOCKS);
  1147. goto do_indirects;
  1148. }
  1149. partial = ext4_find_shared(inode, n, offsets, chain, &nr);
  1150. /* Kill the top of shared branch (not detached) */
  1151. if (nr) {
  1152. if (partial == chain) {
  1153. /* Shared branch grows from the inode */
  1154. ext4_free_branches(handle, inode, NULL,
  1155. &nr, &nr+1, (chain+n-1) - partial);
  1156. *partial->p = 0;
  1157. /*
  1158. * We mark the inode dirty prior to restart,
  1159. * and prior to stop. No need for it here.
  1160. */
  1161. } else {
  1162. /* Shared branch grows from an indirect block */
  1163. BUFFER_TRACE(partial->bh, "get_write_access");
  1164. ext4_free_branches(handle, inode, partial->bh,
  1165. partial->p,
  1166. partial->p+1, (chain+n-1) - partial);
  1167. }
  1168. }
  1169. /* Clear the ends of indirect blocks on the shared branch */
  1170. while (partial > chain) {
  1171. ext4_free_branches(handle, inode, partial->bh, partial->p + 1,
  1172. (__le32*)partial->bh->b_data+addr_per_block,
  1173. (chain+n-1) - partial);
  1174. BUFFER_TRACE(partial->bh, "call brelse");
  1175. brelse(partial->bh);
  1176. partial--;
  1177. }
  1178. do_indirects:
  1179. /* Kill the remaining (whole) subtrees */
  1180. switch (offsets[0]) {
  1181. default:
  1182. nr = i_data[EXT4_IND_BLOCK];
  1183. if (nr) {
  1184. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  1185. i_data[EXT4_IND_BLOCK] = 0;
  1186. }
  1187. case EXT4_IND_BLOCK:
  1188. nr = i_data[EXT4_DIND_BLOCK];
  1189. if (nr) {
  1190. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  1191. i_data[EXT4_DIND_BLOCK] = 0;
  1192. }
  1193. case EXT4_DIND_BLOCK:
  1194. nr = i_data[EXT4_TIND_BLOCK];
  1195. if (nr) {
  1196. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  1197. i_data[EXT4_TIND_BLOCK] = 0;
  1198. }
  1199. case EXT4_TIND_BLOCK:
  1200. ;
  1201. }
  1202. }
  1203. /**
  1204. * ext4_ind_remove_space - remove space from the range
  1205. * @handle: JBD handle for this transaction
  1206. * @inode: inode we are dealing with
  1207. * @start: First block to remove
  1208. * @end: One block after the last block to remove (exclusive)
  1209. *
  1210. * Free the blocks in the defined range (end is exclusive endpoint of
  1211. * range). This is used by ext4_punch_hole().
  1212. */
  1213. int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
  1214. ext4_lblk_t start, ext4_lblk_t end)
  1215. {
  1216. struct ext4_inode_info *ei = EXT4_I(inode);
  1217. __le32 *i_data = ei->i_data;
  1218. int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb);
  1219. ext4_lblk_t offsets[4], offsets2[4];
  1220. Indirect chain[4], chain2[4];
  1221. Indirect *partial, *partial2;
  1222. Indirect *p = NULL, *p2 = NULL;
  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 = p = 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. partial--;
  1282. }
  1283. end_range:
  1284. partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
  1285. if (nr2) {
  1286. if (partial2 == chain2) {
  1287. /*
  1288. * Remember, end is exclusive so here we're at
  1289. * the start of the next level we're not going
  1290. * to free. Everything was covered by the start
  1291. * of the range.
  1292. */
  1293. goto do_indirects;
  1294. }
  1295. } else {
  1296. /*
  1297. * ext4_find_shared returns Indirect structure which
  1298. * points to the last element which should not be
  1299. * removed by truncate. But this is end of the range
  1300. * in punch_hole so we need to point to the next element
  1301. */
  1302. partial2->p++;
  1303. }
  1304. /*
  1305. * Clear the ends of indirect blocks on the shared branch
  1306. * at the end of the range
  1307. */
  1308. while (partial2 > chain2) {
  1309. ext4_free_branches(handle, inode, partial2->bh,
  1310. (__le32 *)partial2->bh->b_data,
  1311. partial2->p,
  1312. (chain2+n2-1) - partial2);
  1313. partial2--;
  1314. }
  1315. goto do_indirects;
  1316. }
  1317. /* Punch happened within the same level (n == n2) */
  1318. partial = p = ext4_find_shared(inode, n, offsets, chain, &nr);
  1319. partial2 = p2 = ext4_find_shared(inode, n2, offsets2, chain2, &nr2);
  1320. /* Free top, but only if partial2 isn't its subtree. */
  1321. if (nr) {
  1322. int level = min(partial - chain, partial2 - chain2);
  1323. int i;
  1324. int subtree = 1;
  1325. for (i = 0; i <= level; i++) {
  1326. if (offsets[i] != offsets2[i]) {
  1327. subtree = 0;
  1328. break;
  1329. }
  1330. }
  1331. if (!subtree) {
  1332. if (partial == chain) {
  1333. /* Shared branch grows from the inode */
  1334. ext4_free_branches(handle, inode, NULL,
  1335. &nr, &nr+1,
  1336. (chain+n-1) - partial);
  1337. *partial->p = 0;
  1338. } else {
  1339. /* Shared branch grows from an indirect block */
  1340. BUFFER_TRACE(partial->bh, "get_write_access");
  1341. ext4_free_branches(handle, inode, partial->bh,
  1342. partial->p,
  1343. partial->p+1,
  1344. (chain+n-1) - partial);
  1345. }
  1346. }
  1347. }
  1348. if (!nr2) {
  1349. /*
  1350. * ext4_find_shared returns Indirect structure which
  1351. * points to the last element which should not be
  1352. * removed by truncate. But this is end of the range
  1353. * in punch_hole so we need to point to the next element
  1354. */
  1355. partial2->p++;
  1356. }
  1357. while (partial > chain || partial2 > chain2) {
  1358. int depth = (chain+n-1) - partial;
  1359. int depth2 = (chain2+n2-1) - partial2;
  1360. if (partial > chain && partial2 > chain2 &&
  1361. partial->bh->b_blocknr == partial2->bh->b_blocknr) {
  1362. /*
  1363. * We've converged on the same block. Clear the range,
  1364. * then we're done.
  1365. */
  1366. ext4_free_branches(handle, inode, partial->bh,
  1367. partial->p + 1,
  1368. partial2->p,
  1369. (chain+n-1) - partial);
  1370. goto cleanup;
  1371. }
  1372. /*
  1373. * The start and end partial branches may not be at the same
  1374. * level even though the punch happened within one level. So, we
  1375. * give them a chance to arrive at the same level, then walk
  1376. * them in step with each other until we converge on the same
  1377. * block.
  1378. */
  1379. if (partial > chain && depth <= depth2) {
  1380. ext4_free_branches(handle, inode, partial->bh,
  1381. partial->p + 1,
  1382. (__le32 *)partial->bh->b_data+addr_per_block,
  1383. (chain+n-1) - partial);
  1384. partial--;
  1385. }
  1386. if (partial2 > chain2 && depth2 <= depth) {
  1387. ext4_free_branches(handle, inode, partial2->bh,
  1388. (__le32 *)partial2->bh->b_data,
  1389. partial2->p,
  1390. (chain2+n2-1) - partial2);
  1391. partial2--;
  1392. }
  1393. }
  1394. cleanup:
  1395. while (p && p > chain) {
  1396. BUFFER_TRACE(p->bh, "call brelse");
  1397. brelse(p->bh);
  1398. p--;
  1399. }
  1400. while (p2 && p2 > chain2) {
  1401. BUFFER_TRACE(p2->bh, "call brelse");
  1402. brelse(p2->bh);
  1403. p2--;
  1404. }
  1405. return 0;
  1406. do_indirects:
  1407. /* Kill the remaining (whole) subtrees */
  1408. switch (offsets[0]) {
  1409. default:
  1410. if (++n >= n2)
  1411. break;
  1412. nr = i_data[EXT4_IND_BLOCK];
  1413. if (nr) {
  1414. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
  1415. i_data[EXT4_IND_BLOCK] = 0;
  1416. }
  1417. case EXT4_IND_BLOCK:
  1418. if (++n >= n2)
  1419. break;
  1420. nr = i_data[EXT4_DIND_BLOCK];
  1421. if (nr) {
  1422. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
  1423. i_data[EXT4_DIND_BLOCK] = 0;
  1424. }
  1425. case EXT4_DIND_BLOCK:
  1426. if (++n >= n2)
  1427. break;
  1428. nr = i_data[EXT4_TIND_BLOCK];
  1429. if (nr) {
  1430. ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
  1431. i_data[EXT4_TIND_BLOCK] = 0;
  1432. }
  1433. case EXT4_TIND_BLOCK:
  1434. ;
  1435. }
  1436. goto cleanup;
  1437. }