indirect.c 45 KB

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