indirect.c 44 KB

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