indirect.c 43 KB

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