indirect.c 41 KB

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