PageRenderTime 46ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ext4/extents.c

https://github.com/mstsirkin/linux
C | 1974 lines | 1432 code | 237 blank | 305 comment | 312 complexity | c034bc4ecbb4795f9345db3ce29f9973 MD5 | raw file
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/jbd2.h>
  34. #include <linux/highuid.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/quotaops.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <linux/falloc.h>
  40. #include <asm/uaccess.h>
  41. #include <linux/fiemap.h>
  42. #include "ext4_jbd2.h"
  43. #include "ext4_extents.h"
  44. #include <trace/events/ext4.h>
  45. static int ext4_split_extent(handle_t *handle,
  46. struct inode *inode,
  47. struct ext4_ext_path *path,
  48. struct ext4_map_blocks *map,
  49. int split_flag,
  50. int flags);
  51. static int ext4_ext_truncate_extend_restart(handle_t *handle,
  52. struct inode *inode,
  53. int needed)
  54. {
  55. int err;
  56. if (!ext4_handle_valid(handle))
  57. return 0;
  58. if (handle->h_buffer_credits > needed)
  59. return 0;
  60. err = ext4_journal_extend(handle, needed);
  61. if (err <= 0)
  62. return err;
  63. err = ext4_truncate_restart_trans(handle, inode, needed);
  64. if (err == 0)
  65. err = -EAGAIN;
  66. return err;
  67. }
  68. /*
  69. * could return:
  70. * - EROFS
  71. * - ENOMEM
  72. */
  73. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  74. struct ext4_ext_path *path)
  75. {
  76. if (path->p_bh) {
  77. /* path points to block */
  78. return ext4_journal_get_write_access(handle, path->p_bh);
  79. }
  80. /* path points to leaf/index in inode body */
  81. /* we use in-core data, no need to protect them */
  82. return 0;
  83. }
  84. /*
  85. * could return:
  86. * - EROFS
  87. * - ENOMEM
  88. * - EIO
  89. */
  90. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  91. struct ext4_ext_path *path)
  92. {
  93. int err;
  94. if (path->p_bh) {
  95. /* path points to block */
  96. err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
  97. } else {
  98. /* path points to leaf/index in inode body */
  99. err = ext4_mark_inode_dirty(handle, inode);
  100. }
  101. return err;
  102. }
  103. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  104. struct ext4_ext_path *path,
  105. ext4_lblk_t block)
  106. {
  107. int depth;
  108. if (path) {
  109. struct ext4_extent *ex;
  110. depth = path->p_depth;
  111. /*
  112. * Try to predict block placement assuming that we are
  113. * filling in a file which will eventually be
  114. * non-sparse --- i.e., in the case of libbfd writing
  115. * an ELF object sections out-of-order but in a way
  116. * the eventually results in a contiguous object or
  117. * executable file, or some database extending a table
  118. * space file. However, this is actually somewhat
  119. * non-ideal if we are writing a sparse file such as
  120. * qemu or KVM writing a raw image file that is going
  121. * to stay fairly sparse, since it will end up
  122. * fragmenting the file system's free space. Maybe we
  123. * should have some hueristics or some way to allow
  124. * userspace to pass a hint to file system,
  125. * especially if the latter case turns out to be
  126. * common.
  127. */
  128. ex = path[depth].p_ext;
  129. if (ex) {
  130. ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
  131. ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
  132. if (block > ext_block)
  133. return ext_pblk + (block - ext_block);
  134. else
  135. return ext_pblk - (ext_block - block);
  136. }
  137. /* it looks like index is empty;
  138. * try to find starting block from index itself */
  139. if (path[depth].p_bh)
  140. return path[depth].p_bh->b_blocknr;
  141. }
  142. /* OK. use inode's group */
  143. return ext4_inode_to_goal_block(inode);
  144. }
  145. /*
  146. * Allocation for a meta data block
  147. */
  148. static ext4_fsblk_t
  149. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  150. struct ext4_ext_path *path,
  151. struct ext4_extent *ex, int *err, unsigned int flags)
  152. {
  153. ext4_fsblk_t goal, newblock;
  154. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  155. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  156. NULL, err);
  157. return newblock;
  158. }
  159. static inline int ext4_ext_space_block(struct inode *inode, int check)
  160. {
  161. int size;
  162. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  163. / sizeof(struct ext4_extent);
  164. if (!check) {
  165. #ifdef AGGRESSIVE_TEST
  166. if (size > 6)
  167. size = 6;
  168. #endif
  169. }
  170. return size;
  171. }
  172. static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
  173. {
  174. int size;
  175. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  176. / sizeof(struct ext4_extent_idx);
  177. if (!check) {
  178. #ifdef AGGRESSIVE_TEST
  179. if (size > 5)
  180. size = 5;
  181. #endif
  182. }
  183. return size;
  184. }
  185. static inline int ext4_ext_space_root(struct inode *inode, int check)
  186. {
  187. int size;
  188. size = sizeof(EXT4_I(inode)->i_data);
  189. size -= sizeof(struct ext4_extent_header);
  190. size /= sizeof(struct ext4_extent);
  191. if (!check) {
  192. #ifdef AGGRESSIVE_TEST
  193. if (size > 3)
  194. size = 3;
  195. #endif
  196. }
  197. return size;
  198. }
  199. static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
  200. {
  201. int size;
  202. size = sizeof(EXT4_I(inode)->i_data);
  203. size -= sizeof(struct ext4_extent_header);
  204. size /= sizeof(struct ext4_extent_idx);
  205. if (!check) {
  206. #ifdef AGGRESSIVE_TEST
  207. if (size > 4)
  208. size = 4;
  209. #endif
  210. }
  211. return size;
  212. }
  213. /*
  214. * Calculate the number of metadata blocks needed
  215. * to allocate @blocks
  216. * Worse case is one block per extent
  217. */
  218. int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  219. {
  220. struct ext4_inode_info *ei = EXT4_I(inode);
  221. int idxs, num = 0;
  222. idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  223. / sizeof(struct ext4_extent_idx));
  224. /*
  225. * If the new delayed allocation block is contiguous with the
  226. * previous da block, it can share index blocks with the
  227. * previous block, so we only need to allocate a new index
  228. * block every idxs leaf blocks. At ldxs**2 blocks, we need
  229. * an additional index block, and at ldxs**3 blocks, yet
  230. * another index blocks.
  231. */
  232. if (ei->i_da_metadata_calc_len &&
  233. ei->i_da_metadata_calc_last_lblock+1 == lblock) {
  234. if ((ei->i_da_metadata_calc_len % idxs) == 0)
  235. num++;
  236. if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
  237. num++;
  238. if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
  239. num++;
  240. ei->i_da_metadata_calc_len = 0;
  241. } else
  242. ei->i_da_metadata_calc_len++;
  243. ei->i_da_metadata_calc_last_lblock++;
  244. return num;
  245. }
  246. /*
  247. * In the worst case we need a new set of index blocks at
  248. * every level of the inode's extent tree.
  249. */
  250. ei->i_da_metadata_calc_len = 1;
  251. ei->i_da_metadata_calc_last_lblock = lblock;
  252. return ext_depth(inode) + 1;
  253. }
  254. static int
  255. ext4_ext_max_entries(struct inode *inode, int depth)
  256. {
  257. int max;
  258. if (depth == ext_depth(inode)) {
  259. if (depth == 0)
  260. max = ext4_ext_space_root(inode, 1);
  261. else
  262. max = ext4_ext_space_root_idx(inode, 1);
  263. } else {
  264. if (depth == 0)
  265. max = ext4_ext_space_block(inode, 1);
  266. else
  267. max = ext4_ext_space_block_idx(inode, 1);
  268. }
  269. return max;
  270. }
  271. static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
  272. {
  273. ext4_fsblk_t block = ext4_ext_pblock(ext);
  274. int len = ext4_ext_get_actual_len(ext);
  275. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
  276. }
  277. static int ext4_valid_extent_idx(struct inode *inode,
  278. struct ext4_extent_idx *ext_idx)
  279. {
  280. ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
  281. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
  282. }
  283. static int ext4_valid_extent_entries(struct inode *inode,
  284. struct ext4_extent_header *eh,
  285. int depth)
  286. {
  287. struct ext4_extent *ext;
  288. struct ext4_extent_idx *ext_idx;
  289. unsigned short entries;
  290. if (eh->eh_entries == 0)
  291. return 1;
  292. entries = le16_to_cpu(eh->eh_entries);
  293. if (depth == 0) {
  294. /* leaf entries */
  295. ext = EXT_FIRST_EXTENT(eh);
  296. while (entries) {
  297. if (!ext4_valid_extent(inode, ext))
  298. return 0;
  299. ext++;
  300. entries--;
  301. }
  302. } else {
  303. ext_idx = EXT_FIRST_INDEX(eh);
  304. while (entries) {
  305. if (!ext4_valid_extent_idx(inode, ext_idx))
  306. return 0;
  307. ext_idx++;
  308. entries--;
  309. }
  310. }
  311. return 1;
  312. }
  313. static int __ext4_ext_check(const char *function, unsigned int line,
  314. struct inode *inode, struct ext4_extent_header *eh,
  315. int depth)
  316. {
  317. const char *error_msg;
  318. int max = 0;
  319. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  320. error_msg = "invalid magic";
  321. goto corrupted;
  322. }
  323. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  324. error_msg = "unexpected eh_depth";
  325. goto corrupted;
  326. }
  327. if (unlikely(eh->eh_max == 0)) {
  328. error_msg = "invalid eh_max";
  329. goto corrupted;
  330. }
  331. max = ext4_ext_max_entries(inode, depth);
  332. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  333. error_msg = "too large eh_max";
  334. goto corrupted;
  335. }
  336. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  337. error_msg = "invalid eh_entries";
  338. goto corrupted;
  339. }
  340. if (!ext4_valid_extent_entries(inode, eh, depth)) {
  341. error_msg = "invalid extent entries";
  342. goto corrupted;
  343. }
  344. return 0;
  345. corrupted:
  346. ext4_error_inode(inode, function, line, 0,
  347. "bad header/extent: %s - magic %x, "
  348. "entries %u, max %u(%u), depth %u(%u)",
  349. error_msg, le16_to_cpu(eh->eh_magic),
  350. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  351. max, le16_to_cpu(eh->eh_depth), depth);
  352. return -EIO;
  353. }
  354. #define ext4_ext_check(inode, eh, depth) \
  355. __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
  356. int ext4_ext_check_inode(struct inode *inode)
  357. {
  358. return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
  359. }
  360. #ifdef EXT_DEBUG
  361. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  362. {
  363. int k, l = path->p_depth;
  364. ext_debug("path:");
  365. for (k = 0; k <= l; k++, path++) {
  366. if (path->p_idx) {
  367. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  368. ext4_idx_pblock(path->p_idx));
  369. } else if (path->p_ext) {
  370. ext_debug(" %d:[%d]%d:%llu ",
  371. le32_to_cpu(path->p_ext->ee_block),
  372. ext4_ext_is_uninitialized(path->p_ext),
  373. ext4_ext_get_actual_len(path->p_ext),
  374. ext4_ext_pblock(path->p_ext));
  375. } else
  376. ext_debug(" []");
  377. }
  378. ext_debug("\n");
  379. }
  380. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  381. {
  382. int depth = ext_depth(inode);
  383. struct ext4_extent_header *eh;
  384. struct ext4_extent *ex;
  385. int i;
  386. if (!path)
  387. return;
  388. eh = path[depth].p_hdr;
  389. ex = EXT_FIRST_EXTENT(eh);
  390. ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
  391. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  392. ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
  393. ext4_ext_is_uninitialized(ex),
  394. ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
  395. }
  396. ext_debug("\n");
  397. }
  398. static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
  399. ext4_fsblk_t newblock, int level)
  400. {
  401. int depth = ext_depth(inode);
  402. struct ext4_extent *ex;
  403. if (depth != level) {
  404. struct ext4_extent_idx *idx;
  405. idx = path[level].p_idx;
  406. while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
  407. ext_debug("%d: move %d:%llu in new index %llu\n", level,
  408. le32_to_cpu(idx->ei_block),
  409. ext4_idx_pblock(idx),
  410. newblock);
  411. idx++;
  412. }
  413. return;
  414. }
  415. ex = path[depth].p_ext;
  416. while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
  417. ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
  418. le32_to_cpu(ex->ee_block),
  419. ext4_ext_pblock(ex),
  420. ext4_ext_is_uninitialized(ex),
  421. ext4_ext_get_actual_len(ex),
  422. newblock);
  423. ex++;
  424. }
  425. }
  426. #else
  427. #define ext4_ext_show_path(inode, path)
  428. #define ext4_ext_show_leaf(inode, path)
  429. #define ext4_ext_show_move(inode, path, newblock, level)
  430. #endif
  431. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  432. {
  433. int depth = path->p_depth;
  434. int i;
  435. for (i = 0; i <= depth; i++, path++)
  436. if (path->p_bh) {
  437. brelse(path->p_bh);
  438. path->p_bh = NULL;
  439. }
  440. }
  441. /*
  442. * ext4_ext_binsearch_idx:
  443. * binary search for the closest index of the given block
  444. * the header must be checked before calling this
  445. */
  446. static void
  447. ext4_ext_binsearch_idx(struct inode *inode,
  448. struct ext4_ext_path *path, ext4_lblk_t block)
  449. {
  450. struct ext4_extent_header *eh = path->p_hdr;
  451. struct ext4_extent_idx *r, *l, *m;
  452. ext_debug("binsearch for %u(idx): ", block);
  453. l = EXT_FIRST_INDEX(eh) + 1;
  454. r = EXT_LAST_INDEX(eh);
  455. while (l <= r) {
  456. m = l + (r - l) / 2;
  457. if (block < le32_to_cpu(m->ei_block))
  458. r = m - 1;
  459. else
  460. l = m + 1;
  461. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  462. m, le32_to_cpu(m->ei_block),
  463. r, le32_to_cpu(r->ei_block));
  464. }
  465. path->p_idx = l - 1;
  466. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  467. ext4_idx_pblock(path->p_idx));
  468. #ifdef CHECK_BINSEARCH
  469. {
  470. struct ext4_extent_idx *chix, *ix;
  471. int k;
  472. chix = ix = EXT_FIRST_INDEX(eh);
  473. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  474. if (k != 0 &&
  475. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  476. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  477. "first=0x%p\n", k,
  478. ix, EXT_FIRST_INDEX(eh));
  479. printk(KERN_DEBUG "%u <= %u\n",
  480. le32_to_cpu(ix->ei_block),
  481. le32_to_cpu(ix[-1].ei_block));
  482. }
  483. BUG_ON(k && le32_to_cpu(ix->ei_block)
  484. <= le32_to_cpu(ix[-1].ei_block));
  485. if (block < le32_to_cpu(ix->ei_block))
  486. break;
  487. chix = ix;
  488. }
  489. BUG_ON(chix != path->p_idx);
  490. }
  491. #endif
  492. }
  493. /*
  494. * ext4_ext_binsearch:
  495. * binary search for closest extent of the given block
  496. * the header must be checked before calling this
  497. */
  498. static void
  499. ext4_ext_binsearch(struct inode *inode,
  500. struct ext4_ext_path *path, ext4_lblk_t block)
  501. {
  502. struct ext4_extent_header *eh = path->p_hdr;
  503. struct ext4_extent *r, *l, *m;
  504. if (eh->eh_entries == 0) {
  505. /*
  506. * this leaf is empty:
  507. * we get such a leaf in split/add case
  508. */
  509. return;
  510. }
  511. ext_debug("binsearch for %u: ", block);
  512. l = EXT_FIRST_EXTENT(eh) + 1;
  513. r = EXT_LAST_EXTENT(eh);
  514. while (l <= r) {
  515. m = l + (r - l) / 2;
  516. if (block < le32_to_cpu(m->ee_block))
  517. r = m - 1;
  518. else
  519. l = m + 1;
  520. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  521. m, le32_to_cpu(m->ee_block),
  522. r, le32_to_cpu(r->ee_block));
  523. }
  524. path->p_ext = l - 1;
  525. ext_debug(" -> %d:%llu:[%d]%d ",
  526. le32_to_cpu(path->p_ext->ee_block),
  527. ext4_ext_pblock(path->p_ext),
  528. ext4_ext_is_uninitialized(path->p_ext),
  529. ext4_ext_get_actual_len(path->p_ext));
  530. #ifdef CHECK_BINSEARCH
  531. {
  532. struct ext4_extent *chex, *ex;
  533. int k;
  534. chex = ex = EXT_FIRST_EXTENT(eh);
  535. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  536. BUG_ON(k && le32_to_cpu(ex->ee_block)
  537. <= le32_to_cpu(ex[-1].ee_block));
  538. if (block < le32_to_cpu(ex->ee_block))
  539. break;
  540. chex = ex;
  541. }
  542. BUG_ON(chex != path->p_ext);
  543. }
  544. #endif
  545. }
  546. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  547. {
  548. struct ext4_extent_header *eh;
  549. eh = ext_inode_hdr(inode);
  550. eh->eh_depth = 0;
  551. eh->eh_entries = 0;
  552. eh->eh_magic = EXT4_EXT_MAGIC;
  553. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
  554. ext4_mark_inode_dirty(handle, inode);
  555. ext4_ext_invalidate_cache(inode);
  556. return 0;
  557. }
  558. struct ext4_ext_path *
  559. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  560. struct ext4_ext_path *path)
  561. {
  562. struct ext4_extent_header *eh;
  563. struct buffer_head *bh;
  564. short int depth, i, ppos = 0, alloc = 0;
  565. eh = ext_inode_hdr(inode);
  566. depth = ext_depth(inode);
  567. /* account possible depth increase */
  568. if (!path) {
  569. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  570. GFP_NOFS);
  571. if (!path)
  572. return ERR_PTR(-ENOMEM);
  573. alloc = 1;
  574. }
  575. path[0].p_hdr = eh;
  576. path[0].p_bh = NULL;
  577. i = depth;
  578. /* walk through the tree */
  579. while (i) {
  580. int need_to_validate = 0;
  581. ext_debug("depth %d: num %d, max %d\n",
  582. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  583. ext4_ext_binsearch_idx(inode, path + ppos, block);
  584. path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  585. path[ppos].p_depth = i;
  586. path[ppos].p_ext = NULL;
  587. bh = sb_getblk(inode->i_sb, path[ppos].p_block);
  588. if (unlikely(!bh))
  589. goto err;
  590. if (!bh_uptodate_or_lock(bh)) {
  591. trace_ext4_ext_load_extent(inode, block,
  592. path[ppos].p_block);
  593. if (bh_submit_read(bh) < 0) {
  594. put_bh(bh);
  595. goto err;
  596. }
  597. /* validate the extent entries */
  598. need_to_validate = 1;
  599. }
  600. eh = ext_block_hdr(bh);
  601. ppos++;
  602. if (unlikely(ppos > depth)) {
  603. put_bh(bh);
  604. EXT4_ERROR_INODE(inode,
  605. "ppos %d > depth %d", ppos, depth);
  606. goto err;
  607. }
  608. path[ppos].p_bh = bh;
  609. path[ppos].p_hdr = eh;
  610. i--;
  611. if (need_to_validate && ext4_ext_check(inode, eh, i))
  612. goto err;
  613. }
  614. path[ppos].p_depth = i;
  615. path[ppos].p_ext = NULL;
  616. path[ppos].p_idx = NULL;
  617. /* find extent */
  618. ext4_ext_binsearch(inode, path + ppos, block);
  619. /* if not an empty leaf */
  620. if (path[ppos].p_ext)
  621. path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  622. ext4_ext_show_path(inode, path);
  623. return path;
  624. err:
  625. ext4_ext_drop_refs(path);
  626. if (alloc)
  627. kfree(path);
  628. return ERR_PTR(-EIO);
  629. }
  630. /*
  631. * ext4_ext_insert_index:
  632. * insert new index [@logical;@ptr] into the block at @curp;
  633. * check where to insert: before @curp or after @curp
  634. */
  635. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  636. struct ext4_ext_path *curp,
  637. int logical, ext4_fsblk_t ptr)
  638. {
  639. struct ext4_extent_idx *ix;
  640. int len, err;
  641. err = ext4_ext_get_access(handle, inode, curp);
  642. if (err)
  643. return err;
  644. if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
  645. EXT4_ERROR_INODE(inode,
  646. "logical %d == ei_block %d!",
  647. logical, le32_to_cpu(curp->p_idx->ei_block));
  648. return -EIO;
  649. }
  650. if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
  651. >= le16_to_cpu(curp->p_hdr->eh_max))) {
  652. EXT4_ERROR_INODE(inode,
  653. "eh_entries %d >= eh_max %d!",
  654. le16_to_cpu(curp->p_hdr->eh_entries),
  655. le16_to_cpu(curp->p_hdr->eh_max));
  656. return -EIO;
  657. }
  658. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  659. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  660. /* insert after */
  661. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  662. len = (len - 1) * sizeof(struct ext4_extent_idx);
  663. len = len < 0 ? 0 : len;
  664. ext_debug("insert new index %d after: %llu. "
  665. "move %d from 0x%p to 0x%p\n",
  666. logical, ptr, len,
  667. (curp->p_idx + 1), (curp->p_idx + 2));
  668. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  669. }
  670. ix = curp->p_idx + 1;
  671. } else {
  672. /* insert before */
  673. len = len * sizeof(struct ext4_extent_idx);
  674. len = len < 0 ? 0 : len;
  675. ext_debug("insert new index %d before: %llu. "
  676. "move %d from 0x%p to 0x%p\n",
  677. logical, ptr, len,
  678. curp->p_idx, (curp->p_idx + 1));
  679. memmove(curp->p_idx + 1, curp->p_idx, len);
  680. ix = curp->p_idx;
  681. }
  682. ix->ei_block = cpu_to_le32(logical);
  683. ext4_idx_store_pblock(ix, ptr);
  684. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  685. if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
  686. EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
  687. return -EIO;
  688. }
  689. err = ext4_ext_dirty(handle, inode, curp);
  690. ext4_std_error(inode->i_sb, err);
  691. return err;
  692. }
  693. /*
  694. * ext4_ext_split:
  695. * inserts new subtree into the path, using free index entry
  696. * at depth @at:
  697. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  698. * - makes decision where to split
  699. * - moves remaining extents and index entries (right to the split point)
  700. * into the newly allocated blocks
  701. * - initializes subtree
  702. */
  703. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  704. unsigned int flags,
  705. struct ext4_ext_path *path,
  706. struct ext4_extent *newext, int at)
  707. {
  708. struct buffer_head *bh = NULL;
  709. int depth = ext_depth(inode);
  710. struct ext4_extent_header *neh;
  711. struct ext4_extent_idx *fidx;
  712. int i = at, k, m, a;
  713. ext4_fsblk_t newblock, oldblock;
  714. __le32 border;
  715. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  716. int err = 0;
  717. /* make decision: where to split? */
  718. /* FIXME: now decision is simplest: at current extent */
  719. /* if current leaf will be split, then we should use
  720. * border from split point */
  721. if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
  722. EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
  723. return -EIO;
  724. }
  725. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  726. border = path[depth].p_ext[1].ee_block;
  727. ext_debug("leaf will be split."
  728. " next leaf starts at %d\n",
  729. le32_to_cpu(border));
  730. } else {
  731. border = newext->ee_block;
  732. ext_debug("leaf will be added."
  733. " next leaf starts at %d\n",
  734. le32_to_cpu(border));
  735. }
  736. /*
  737. * If error occurs, then we break processing
  738. * and mark filesystem read-only. index won't
  739. * be inserted and tree will be in consistent
  740. * state. Next mount will repair buffers too.
  741. */
  742. /*
  743. * Get array to track all allocated blocks.
  744. * We need this to handle errors and free blocks
  745. * upon them.
  746. */
  747. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  748. if (!ablocks)
  749. return -ENOMEM;
  750. /* allocate all needed blocks */
  751. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  752. for (a = 0; a < depth - at; a++) {
  753. newblock = ext4_ext_new_meta_block(handle, inode, path,
  754. newext, &err, flags);
  755. if (newblock == 0)
  756. goto cleanup;
  757. ablocks[a] = newblock;
  758. }
  759. /* initialize new leaf */
  760. newblock = ablocks[--a];
  761. if (unlikely(newblock == 0)) {
  762. EXT4_ERROR_INODE(inode, "newblock == 0!");
  763. err = -EIO;
  764. goto cleanup;
  765. }
  766. bh = sb_getblk(inode->i_sb, newblock);
  767. if (!bh) {
  768. err = -EIO;
  769. goto cleanup;
  770. }
  771. lock_buffer(bh);
  772. err = ext4_journal_get_create_access(handle, bh);
  773. if (err)
  774. goto cleanup;
  775. neh = ext_block_hdr(bh);
  776. neh->eh_entries = 0;
  777. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  778. neh->eh_magic = EXT4_EXT_MAGIC;
  779. neh->eh_depth = 0;
  780. /* move remainder of path[depth] to the new leaf */
  781. if (unlikely(path[depth].p_hdr->eh_entries !=
  782. path[depth].p_hdr->eh_max)) {
  783. EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
  784. path[depth].p_hdr->eh_entries,
  785. path[depth].p_hdr->eh_max);
  786. err = -EIO;
  787. goto cleanup;
  788. }
  789. /* start copy from next extent */
  790. m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
  791. ext4_ext_show_move(inode, path, newblock, depth);
  792. if (m) {
  793. struct ext4_extent *ex;
  794. ex = EXT_FIRST_EXTENT(neh);
  795. memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
  796. le16_add_cpu(&neh->eh_entries, m);
  797. }
  798. set_buffer_uptodate(bh);
  799. unlock_buffer(bh);
  800. err = ext4_handle_dirty_metadata(handle, inode, bh);
  801. if (err)
  802. goto cleanup;
  803. brelse(bh);
  804. bh = NULL;
  805. /* correct old leaf */
  806. if (m) {
  807. err = ext4_ext_get_access(handle, inode, path + depth);
  808. if (err)
  809. goto cleanup;
  810. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  811. err = ext4_ext_dirty(handle, inode, path + depth);
  812. if (err)
  813. goto cleanup;
  814. }
  815. /* create intermediate indexes */
  816. k = depth - at - 1;
  817. if (unlikely(k < 0)) {
  818. EXT4_ERROR_INODE(inode, "k %d < 0!", k);
  819. err = -EIO;
  820. goto cleanup;
  821. }
  822. if (k)
  823. ext_debug("create %d intermediate indices\n", k);
  824. /* insert new index into current index block */
  825. /* current depth stored in i var */
  826. i = depth - 1;
  827. while (k--) {
  828. oldblock = newblock;
  829. newblock = ablocks[--a];
  830. bh = sb_getblk(inode->i_sb, newblock);
  831. if (!bh) {
  832. err = -EIO;
  833. goto cleanup;
  834. }
  835. lock_buffer(bh);
  836. err = ext4_journal_get_create_access(handle, bh);
  837. if (err)
  838. goto cleanup;
  839. neh = ext_block_hdr(bh);
  840. neh->eh_entries = cpu_to_le16(1);
  841. neh->eh_magic = EXT4_EXT_MAGIC;
  842. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  843. neh->eh_depth = cpu_to_le16(depth - i);
  844. fidx = EXT_FIRST_INDEX(neh);
  845. fidx->ei_block = border;
  846. ext4_idx_store_pblock(fidx, oldblock);
  847. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  848. i, newblock, le32_to_cpu(border), oldblock);
  849. /* move remainder of path[i] to the new index block */
  850. if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
  851. EXT_LAST_INDEX(path[i].p_hdr))) {
  852. EXT4_ERROR_INODE(inode,
  853. "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
  854. le32_to_cpu(path[i].p_ext->ee_block));
  855. err = -EIO;
  856. goto cleanup;
  857. }
  858. /* start copy indexes */
  859. m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
  860. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  861. EXT_MAX_INDEX(path[i].p_hdr));
  862. ext4_ext_show_move(inode, path, newblock, i);
  863. if (m) {
  864. memmove(++fidx, path[i].p_idx,
  865. sizeof(struct ext4_extent_idx) * m);
  866. le16_add_cpu(&neh->eh_entries, m);
  867. }
  868. set_buffer_uptodate(bh);
  869. unlock_buffer(bh);
  870. err = ext4_handle_dirty_metadata(handle, inode, bh);
  871. if (err)
  872. goto cleanup;
  873. brelse(bh);
  874. bh = NULL;
  875. /* correct old index */
  876. if (m) {
  877. err = ext4_ext_get_access(handle, inode, path + i);
  878. if (err)
  879. goto cleanup;
  880. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  881. err = ext4_ext_dirty(handle, inode, path + i);
  882. if (err)
  883. goto cleanup;
  884. }
  885. i--;
  886. }
  887. /* insert new index */
  888. err = ext4_ext_insert_index(handle, inode, path + at,
  889. le32_to_cpu(border), newblock);
  890. cleanup:
  891. if (bh) {
  892. if (buffer_locked(bh))
  893. unlock_buffer(bh);
  894. brelse(bh);
  895. }
  896. if (err) {
  897. /* free all allocated blocks in error case */
  898. for (i = 0; i < depth; i++) {
  899. if (!ablocks[i])
  900. continue;
  901. ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
  902. EXT4_FREE_BLOCKS_METADATA);
  903. }
  904. }
  905. kfree(ablocks);
  906. return err;
  907. }
  908. /*
  909. * ext4_ext_grow_indepth:
  910. * implements tree growing procedure:
  911. * - allocates new block
  912. * - moves top-level data (index block or leaf) into the new block
  913. * - initializes new top-level, creating index that points to the
  914. * just created block
  915. */
  916. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  917. unsigned int flags,
  918. struct ext4_ext_path *path,
  919. struct ext4_extent *newext)
  920. {
  921. struct ext4_ext_path *curp = path;
  922. struct ext4_extent_header *neh;
  923. struct buffer_head *bh;
  924. ext4_fsblk_t newblock;
  925. int err = 0;
  926. newblock = ext4_ext_new_meta_block(handle, inode, path,
  927. newext, &err, flags);
  928. if (newblock == 0)
  929. return err;
  930. bh = sb_getblk(inode->i_sb, newblock);
  931. if (!bh) {
  932. err = -EIO;
  933. ext4_std_error(inode->i_sb, err);
  934. return err;
  935. }
  936. lock_buffer(bh);
  937. err = ext4_journal_get_create_access(handle, bh);
  938. if (err) {
  939. unlock_buffer(bh);
  940. goto out;
  941. }
  942. /* move top-level index/leaf into new block */
  943. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  944. /* set size of new block */
  945. neh = ext_block_hdr(bh);
  946. /* old root could have indexes or leaves
  947. * so calculate e_max right way */
  948. if (ext_depth(inode))
  949. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  950. else
  951. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  952. neh->eh_magic = EXT4_EXT_MAGIC;
  953. set_buffer_uptodate(bh);
  954. unlock_buffer(bh);
  955. err = ext4_handle_dirty_metadata(handle, inode, bh);
  956. if (err)
  957. goto out;
  958. /* create index in new top-level index: num,max,pointer */
  959. err = ext4_ext_get_access(handle, inode, curp);
  960. if (err)
  961. goto out;
  962. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  963. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
  964. curp->p_hdr->eh_entries = cpu_to_le16(1);
  965. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  966. if (path[0].p_hdr->eh_depth)
  967. curp->p_idx->ei_block =
  968. EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
  969. else
  970. curp->p_idx->ei_block =
  971. EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  972. ext4_idx_store_pblock(curp->p_idx, newblock);
  973. neh = ext_inode_hdr(inode);
  974. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  975. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  976. le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
  977. ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
  978. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  979. err = ext4_ext_dirty(handle, inode, curp);
  980. out:
  981. brelse(bh);
  982. return err;
  983. }
  984. /*
  985. * ext4_ext_create_new_leaf:
  986. * finds empty index and adds new leaf.
  987. * if no free index is found, then it requests in-depth growing.
  988. */
  989. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  990. unsigned int flags,
  991. struct ext4_ext_path *path,
  992. struct ext4_extent *newext)
  993. {
  994. struct ext4_ext_path *curp;
  995. int depth, i, err = 0;
  996. repeat:
  997. i = depth = ext_depth(inode);
  998. /* walk up to the tree and look for free index entry */
  999. curp = path + depth;
  1000. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  1001. i--;
  1002. curp--;
  1003. }
  1004. /* we use already allocated block for index block,
  1005. * so subsequent data blocks should be contiguous */
  1006. if (EXT_HAS_FREE_INDEX(curp)) {
  1007. /* if we found index with free entry, then use that
  1008. * entry: create all needed subtree and add new leaf */
  1009. err = ext4_ext_split(handle, inode, flags, path, newext, i);
  1010. if (err)
  1011. goto out;
  1012. /* refill path */
  1013. ext4_ext_drop_refs(path);
  1014. path = ext4_ext_find_extent(inode,
  1015. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1016. path);
  1017. if (IS_ERR(path))
  1018. err = PTR_ERR(path);
  1019. } else {
  1020. /* tree is full, time to grow in depth */
  1021. err = ext4_ext_grow_indepth(handle, inode, flags,
  1022. path, newext);
  1023. if (err)
  1024. goto out;
  1025. /* refill path */
  1026. ext4_ext_drop_refs(path);
  1027. path = ext4_ext_find_extent(inode,
  1028. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1029. path);
  1030. if (IS_ERR(path)) {
  1031. err = PTR_ERR(path);
  1032. goto out;
  1033. }
  1034. /*
  1035. * only first (depth 0 -> 1) produces free space;
  1036. * in all other cases we have to split the grown tree
  1037. */
  1038. depth = ext_depth(inode);
  1039. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  1040. /* now we need to split */
  1041. goto repeat;
  1042. }
  1043. }
  1044. out:
  1045. return err;
  1046. }
  1047. /*
  1048. * search the closest allocated block to the left for *logical
  1049. * and returns it at @logical + it's physical address at @phys
  1050. * if *logical is the smallest allocated block, the function
  1051. * returns 0 at @phys
  1052. * return value contains 0 (success) or error code
  1053. */
  1054. static int ext4_ext_search_left(struct inode *inode,
  1055. struct ext4_ext_path *path,
  1056. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1057. {
  1058. struct ext4_extent_idx *ix;
  1059. struct ext4_extent *ex;
  1060. int depth, ee_len;
  1061. if (unlikely(path == NULL)) {
  1062. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1063. return -EIO;
  1064. }
  1065. depth = path->p_depth;
  1066. *phys = 0;
  1067. if (depth == 0 && path->p_ext == NULL)
  1068. return 0;
  1069. /* usually extent in the path covers blocks smaller
  1070. * then *logical, but it can be that extent is the
  1071. * first one in the file */
  1072. ex = path[depth].p_ext;
  1073. ee_len = ext4_ext_get_actual_len(ex);
  1074. if (*logical < le32_to_cpu(ex->ee_block)) {
  1075. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1076. EXT4_ERROR_INODE(inode,
  1077. "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
  1078. *logical, le32_to_cpu(ex->ee_block));
  1079. return -EIO;
  1080. }
  1081. while (--depth >= 0) {
  1082. ix = path[depth].p_idx;
  1083. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1084. EXT4_ERROR_INODE(inode,
  1085. "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
  1086. ix != NULL ? ix->ei_block : 0,
  1087. EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
  1088. EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
  1089. depth);
  1090. return -EIO;
  1091. }
  1092. }
  1093. return 0;
  1094. }
  1095. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1096. EXT4_ERROR_INODE(inode,
  1097. "logical %d < ee_block %d + ee_len %d!",
  1098. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1099. return -EIO;
  1100. }
  1101. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  1102. *phys = ext4_ext_pblock(ex) + ee_len - 1;
  1103. return 0;
  1104. }
  1105. /*
  1106. * search the closest allocated block to the right for *logical
  1107. * and returns it at @logical + it's physical address at @phys
  1108. * if *logical is the smallest allocated block, the function
  1109. * returns 0 at @phys
  1110. * return value contains 0 (success) or error code
  1111. */
  1112. static int ext4_ext_search_right(struct inode *inode,
  1113. struct ext4_ext_path *path,
  1114. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1115. {
  1116. struct buffer_head *bh = NULL;
  1117. struct ext4_extent_header *eh;
  1118. struct ext4_extent_idx *ix;
  1119. struct ext4_extent *ex;
  1120. ext4_fsblk_t block;
  1121. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1122. int ee_len;
  1123. if (unlikely(path == NULL)) {
  1124. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1125. return -EIO;
  1126. }
  1127. depth = path->p_depth;
  1128. *phys = 0;
  1129. if (depth == 0 && path->p_ext == NULL)
  1130. return 0;
  1131. /* usually extent in the path covers blocks smaller
  1132. * then *logical, but it can be that extent is the
  1133. * first one in the file */
  1134. ex = path[depth].p_ext;
  1135. ee_len = ext4_ext_get_actual_len(ex);
  1136. if (*logical < le32_to_cpu(ex->ee_block)) {
  1137. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1138. EXT4_ERROR_INODE(inode,
  1139. "first_extent(path[%d].p_hdr) != ex",
  1140. depth);
  1141. return -EIO;
  1142. }
  1143. while (--depth >= 0) {
  1144. ix = path[depth].p_idx;
  1145. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1146. EXT4_ERROR_INODE(inode,
  1147. "ix != EXT_FIRST_INDEX *logical %d!",
  1148. *logical);
  1149. return -EIO;
  1150. }
  1151. }
  1152. *logical = le32_to_cpu(ex->ee_block);
  1153. *phys = ext4_ext_pblock(ex);
  1154. return 0;
  1155. }
  1156. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1157. EXT4_ERROR_INODE(inode,
  1158. "logical %d < ee_block %d + ee_len %d!",
  1159. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1160. return -EIO;
  1161. }
  1162. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1163. /* next allocated block in this leaf */
  1164. ex++;
  1165. *logical = le32_to_cpu(ex->ee_block);
  1166. *phys = ext4_ext_pblock(ex);
  1167. return 0;
  1168. }
  1169. /* go up and search for index to the right */
  1170. while (--depth >= 0) {
  1171. ix = path[depth].p_idx;
  1172. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1173. goto got_index;
  1174. }
  1175. /* we've gone up to the root and found no index to the right */
  1176. return 0;
  1177. got_index:
  1178. /* we've found index to the right, let's
  1179. * follow it and find the closest allocated
  1180. * block to the right */
  1181. ix++;
  1182. block = ext4_idx_pblock(ix);
  1183. while (++depth < path->p_depth) {
  1184. bh = sb_bread(inode->i_sb, block);
  1185. if (bh == NULL)
  1186. return -EIO;
  1187. eh = ext_block_hdr(bh);
  1188. /* subtract from p_depth to get proper eh_depth */
  1189. if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
  1190. put_bh(bh);
  1191. return -EIO;
  1192. }
  1193. ix = EXT_FIRST_INDEX(eh);
  1194. block = ext4_idx_pblock(ix);
  1195. put_bh(bh);
  1196. }
  1197. bh = sb_bread(inode->i_sb, block);
  1198. if (bh == NULL)
  1199. return -EIO;
  1200. eh = ext_block_hdr(bh);
  1201. if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
  1202. put_bh(bh);
  1203. return -EIO;
  1204. }
  1205. ex = EXT_FIRST_EXTENT(eh);
  1206. *logical = le32_to_cpu(ex->ee_block);
  1207. *phys = ext4_ext_pblock(ex);
  1208. put_bh(bh);
  1209. return 0;
  1210. }
  1211. /*
  1212. * ext4_ext_next_allocated_block:
  1213. * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
  1214. * NOTE: it considers block number from index entry as
  1215. * allocated block. Thus, index entries have to be consistent
  1216. * with leaves.
  1217. */
  1218. static ext4_lblk_t
  1219. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1220. {
  1221. int depth;
  1222. BUG_ON(path == NULL);
  1223. depth = path->p_depth;
  1224. if (depth == 0 && path->p_ext == NULL)
  1225. return EXT_MAX_BLOCKS;
  1226. while (depth >= 0) {
  1227. if (depth == path->p_depth) {
  1228. /* leaf */
  1229. if (path[depth].p_ext !=
  1230. EXT_LAST_EXTENT(path[depth].p_hdr))
  1231. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1232. } else {
  1233. /* index */
  1234. if (path[depth].p_idx !=
  1235. EXT_LAST_INDEX(path[depth].p_hdr))
  1236. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1237. }
  1238. depth--;
  1239. }
  1240. return EXT_MAX_BLOCKS;
  1241. }
  1242. /*
  1243. * ext4_ext_next_leaf_block:
  1244. * returns first allocated block from next leaf or EXT_MAX_BLOCKS
  1245. */
  1246. static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
  1247. {
  1248. int depth;
  1249. BUG_ON(path == NULL);
  1250. depth = path->p_depth;
  1251. /* zero-tree has no leaf blocks at all */
  1252. if (depth == 0)
  1253. return EXT_MAX_BLOCKS;
  1254. /* go to index block */
  1255. depth--;
  1256. while (depth >= 0) {
  1257. if (path[depth].p_idx !=
  1258. EXT_LAST_INDEX(path[depth].p_hdr))
  1259. return (ext4_lblk_t)
  1260. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1261. depth--;
  1262. }
  1263. return EXT_MAX_BLOCKS;
  1264. }
  1265. /*
  1266. * ext4_ext_correct_indexes:
  1267. * if leaf gets modified and modified extent is first in the leaf,
  1268. * then we have to correct all indexes above.
  1269. * TODO: do we need to correct tree in all cases?
  1270. */
  1271. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1272. struct ext4_ext_path *path)
  1273. {
  1274. struct ext4_extent_header *eh;
  1275. int depth = ext_depth(inode);
  1276. struct ext4_extent *ex;
  1277. __le32 border;
  1278. int k, err = 0;
  1279. eh = path[depth].p_hdr;
  1280. ex = path[depth].p_ext;
  1281. if (unlikely(ex == NULL || eh == NULL)) {
  1282. EXT4_ERROR_INODE(inode,
  1283. "ex %p == NULL or eh %p == NULL", ex, eh);
  1284. return -EIO;
  1285. }
  1286. if (depth == 0) {
  1287. /* there is no tree at all */
  1288. return 0;
  1289. }
  1290. if (ex != EXT_FIRST_EXTENT(eh)) {
  1291. /* we correct tree if first leaf got modified only */
  1292. return 0;
  1293. }
  1294. /*
  1295. * TODO: we need correction if border is smaller than current one
  1296. */
  1297. k = depth - 1;
  1298. border = path[depth].p_ext->ee_block;
  1299. err = ext4_ext_get_access(handle, inode, path + k);
  1300. if (err)
  1301. return err;
  1302. path[k].p_idx->ei_block = border;
  1303. err = ext4_ext_dirty(handle, inode, path + k);
  1304. if (err)
  1305. return err;
  1306. while (k--) {
  1307. /* change all left-side indexes */
  1308. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1309. break;
  1310. err = ext4_ext_get_access(handle, inode, path + k);
  1311. if (err)
  1312. break;
  1313. path[k].p_idx->ei_block = border;
  1314. err = ext4_ext_dirty(handle, inode, path + k);
  1315. if (err)
  1316. break;
  1317. }
  1318. return err;
  1319. }
  1320. int
  1321. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1322. struct ext4_extent *ex2)
  1323. {
  1324. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1325. /*
  1326. * Make sure that either both extents are uninitialized, or
  1327. * both are _not_.
  1328. */
  1329. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1330. return 0;
  1331. if (ext4_ext_is_uninitialized(ex1))
  1332. max_len = EXT_UNINIT_MAX_LEN;
  1333. else
  1334. max_len = EXT_INIT_MAX_LEN;
  1335. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1336. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1337. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1338. le32_to_cpu(ex2->ee_block))
  1339. return 0;
  1340. /*
  1341. * To allow future support for preallocated extents to be added
  1342. * as an RO_COMPAT feature, refuse to merge to extents if
  1343. * this can result in the top bit of ee_len being set.
  1344. */
  1345. if (ext1_ee_len + ext2_ee_len > max_len)
  1346. return 0;
  1347. #ifdef AGGRESSIVE_TEST
  1348. if (ext1_ee_len >= 4)
  1349. return 0;
  1350. #endif
  1351. if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
  1352. return 1;
  1353. return 0;
  1354. }
  1355. /*
  1356. * This function tries to merge the "ex" extent to the next extent in the tree.
  1357. * It always tries to merge towards right. If you want to merge towards
  1358. * left, pass "ex - 1" as argument instead of "ex".
  1359. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1360. * 1 if they got merged.
  1361. */
  1362. static int ext4_ext_try_to_merge_right(struct inode *inode,
  1363. struct ext4_ext_path *path,
  1364. struct ext4_extent *ex)
  1365. {
  1366. struct ext4_extent_header *eh;
  1367. unsigned int depth, len;
  1368. int merge_done = 0;
  1369. int uninitialized = 0;
  1370. depth = ext_depth(inode);
  1371. BUG_ON(path[depth].p_hdr == NULL);
  1372. eh = path[depth].p_hdr;
  1373. while (ex < EXT_LAST_EXTENT(eh)) {
  1374. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1375. break;
  1376. /* merge with next extent! */
  1377. if (ext4_ext_is_uninitialized(ex))
  1378. uninitialized = 1;
  1379. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1380. + ext4_ext_get_actual_len(ex + 1));
  1381. if (uninitialized)
  1382. ext4_ext_mark_uninitialized(ex);
  1383. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1384. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1385. * sizeof(struct ext4_extent);
  1386. memmove(ex + 1, ex + 2, len);
  1387. }
  1388. le16_add_cpu(&eh->eh_entries, -1);
  1389. merge_done = 1;
  1390. WARN_ON(eh->eh_entries == 0);
  1391. if (!eh->eh_entries)
  1392. EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
  1393. }
  1394. return merge_done;
  1395. }
  1396. /*
  1397. * This function tries to merge the @ex extent to neighbours in the tree.
  1398. * return 1 if merge left else 0.
  1399. */
  1400. static int ext4_ext_try_to_merge(struct inode *inode,
  1401. struct ext4_ext_path *path,
  1402. struct ext4_extent *ex) {
  1403. struct ext4_extent_header *eh;
  1404. unsigned int depth;
  1405. int merge_done = 0;
  1406. int ret = 0;
  1407. depth = ext_depth(inode);
  1408. BUG_ON(path[depth].p_hdr == NULL);
  1409. eh = path[depth].p_hdr;
  1410. if (ex > EXT_FIRST_EXTENT(eh))
  1411. merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
  1412. if (!merge_done)
  1413. ret = ext4_ext_try_to_merge_right(inode, path, ex);
  1414. return ret;
  1415. }
  1416. /*
  1417. * check if a portion of the "newext" extent overlaps with an
  1418. * existing extent.
  1419. *
  1420. * If there is an overlap discovered, it updates the length of the newext
  1421. * such that there will be no overlap, and then returns 1.
  1422. * If there is no overlap found, it returns 0.
  1423. */
  1424. static unsigned int ext4_ext_check_overlap(struct inode *inode,
  1425. struct ext4_extent *newext,
  1426. struct ext4_ext_path *path)
  1427. {
  1428. ext4_lblk_t b1, b2;
  1429. unsigned int depth, len1;
  1430. unsigned int ret = 0;
  1431. b1 = le32_to_cpu(newext->ee_block);
  1432. len1 = ext4_ext_get_actual_len(newext);
  1433. depth = ext_depth(inode);
  1434. if (!path[depth].p_ext)
  1435. goto out;
  1436. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1437. /*
  1438. * get the next allocated block if the extent in the path
  1439. * is before the requested block(s)
  1440. */
  1441. if (b2 < b1) {
  1442. b2 = ext4_ext_next_allocated_block(path);
  1443. if (b2 == EXT_MAX_BLOCKS)
  1444. goto out;
  1445. }
  1446. /* check for wrap through zero on extent logical start block*/
  1447. if (b1 + len1 < b1) {
  1448. len1 = EXT_MAX_BLOCKS - b1;
  1449. newext->ee_len = cpu_to_le16(len1);
  1450. ret = 1;
  1451. }
  1452. /* check for overlap */
  1453. if (b1 + len1 > b2) {
  1454. newext->ee_len = cpu_to_le16(b2 - b1);
  1455. ret = 1;
  1456. }
  1457. out:
  1458. return ret;
  1459. }
  1460. /*
  1461. * ext4_ext_insert_extent:
  1462. * tries to merge requsted extent into the existing extent or
  1463. * inserts requested extent as new one into the tree,
  1464. * creating new leaf in the no-space case.
  1465. */
  1466. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1467. struct ext4_ext_path *path,
  1468. struct ext4_extent *newext, int flag)
  1469. {
  1470. struct ext4_extent_header *eh;
  1471. struct ext4_extent *ex, *fex;
  1472. struct ext4_extent *nearex; /* nearest extent */
  1473. struct ext4_ext_path *npath = NULL;
  1474. int depth, len, err;
  1475. ext4_lblk_t next;
  1476. unsigned uninitialized = 0;
  1477. int flags = 0;
  1478. if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
  1479. EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
  1480. return -EIO;
  1481. }
  1482. depth = ext_depth(inode);
  1483. ex = path[depth].p_ext;
  1484. if (unlikely(path[depth].p_hdr == NULL)) {
  1485. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1486. return -EIO;
  1487. }
  1488. /* try to insert block into found extent and return */
  1489. if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
  1490. && ext4_can_extents_be_merged(inode, ex, newext)) {
  1491. ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
  1492. ext4_ext_is_uninitialized(newext),
  1493. ext4_ext_get_actual_len(newext),
  1494. le32_to_cpu(ex->ee_block),
  1495. ext4_ext_is_uninitialized(ex),
  1496. ext4_ext_get_actual_len(ex),
  1497. ext4_ext_pblock(ex));
  1498. err = ext4_ext_get_access(handle, inode, path + depth);
  1499. if (err)
  1500. return err;
  1501. /*
  1502. * ext4_can_extents_be_merged should have checked that either
  1503. * both extents are uninitialized, or both aren't. Thus we
  1504. * need to check only one of them here.
  1505. */
  1506. if (ext4_ext_is_uninitialized(ex))
  1507. uninitialized = 1;
  1508. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1509. + ext4_ext_get_actual_len(newext));
  1510. if (uninitialized)
  1511. ext4_ext_mark_uninitialized(ex);
  1512. eh = path[depth].p_hdr;
  1513. nearex = ex;
  1514. goto merge;
  1515. }
  1516. depth = ext_depth(inode);
  1517. eh = path[depth].p_hdr;
  1518. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1519. goto has_space;
  1520. /* probably next leaf has space for us? */
  1521. fex = EXT_LAST_EXTENT(eh);
  1522. next = EXT_MAX_BLOCKS;
  1523. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
  1524. next = ext4_ext_next_leaf_block(path);
  1525. if (next != EXT_MAX_BLOCKS) {
  1526. ext_debug("next leaf block - %d\n", next);
  1527. BUG_ON(npath != NULL);
  1528. npath = ext4_ext_find_extent(inode, next, NULL);
  1529. if (IS_ERR(npath))
  1530. return PTR_ERR(npath);
  1531. BUG_ON(npath->p_depth != path->p_depth);
  1532. eh = npath[depth].p_hdr;
  1533. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1534. ext_debug("next leaf isn't full(%d)\n",
  1535. le16_to_cpu(eh->eh_entries));
  1536. path = npath;
  1537. goto has_space;
  1538. }
  1539. ext_debug("next leaf has no free space(%d,%d)\n",
  1540. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1541. }
  1542. /*
  1543. * There is no free space in the found leaf.
  1544. * We're gonna add a new leaf in the tree.
  1545. */
  1546. if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
  1547. flags = EXT4_MB_USE_ROOT_BLOCKS;
  1548. err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
  1549. if (err)
  1550. goto cleanup;
  1551. depth = ext_depth(inode);
  1552. eh = path[depth].p_hdr;
  1553. has_space:
  1554. nearex = path[depth].p_ext;
  1555. err = ext4_ext_get_access(handle, inode, path + depth);
  1556. if (err)
  1557. goto cleanup;
  1558. if (!nearex) {
  1559. /* there is no extent in this leaf, create first one */
  1560. ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
  1561. le32_to_cpu(newext->ee_block),
  1562. ext4_ext_pblock(newext),
  1563. ext4_ext_is_uninitialized(newext),
  1564. ext4_ext_get_actual_len(newext));
  1565. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1566. } else if (le32_to_cpu(newext->ee_block)
  1567. > le32_to_cpu(nearex->ee_block)) {
  1568. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1569. if (nearex != EXT_LAST_EXTENT(eh)) {
  1570. len = EXT_MAX_EXTENT(eh) - nearex;
  1571. len = (len - 1) * sizeof(struct ext4_extent);
  1572. len = len < 0 ? 0 : len;
  1573. ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
  1574. "move %d from 0x%p to 0x%p\n",
  1575. le32_to_cpu(newext->ee_block),
  1576. ext4_ext_pblock(newext),
  1577. ext4_ext_is_uninitialized(newext),
  1578. ext4_ext_get_actual_len(newext),
  1579. nearex, len, nearex + 1, nearex + 2);
  1580. memmove(nearex + 2, nearex + 1, len);
  1581. }
  1582. path[depth].p_ext = nearex + 1;
  1583. } else {
  1584. BUG_ON(newext->ee_block == nearex->ee_block);
  1585. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1586. len = len < 0 ? 0 : len;
  1587. ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
  1588. "move %d from 0x%p to 0x%p\n",
  1589. le32_to_cpu(newext->ee_block),
  1590. ext4_ext_pblock(newext),
  1591. ext4_ext_is_uninitialized(newext),
  1592. ext4_ext_get_actual_len(newext),
  1593. nearex, len, nearex, nearex + 1);
  1594. memmove(nearex + 1, nearex, len);
  1595. path[depth].p_ext = nearex;
  1596. }
  1597. le16_add_cpu(&eh->eh_entries, 1);
  1598. nearex = path[depth].p_ext;
  1599. nearex->ee_block = newext->ee_block;
  1600. ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
  1601. nearex->ee_len = newext->ee_len;
  1602. merge:
  1603. /* try to merge extents to the right */
  1604. if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
  1605. ext4_ext_try_to_merge(inode, path, nearex);
  1606. /* try to merge extents to the left */
  1607. /* time to correct all indexes above */
  1608. err = ext4_ext_correct_indexes(handle, inode, path);
  1609. if (err)
  1610. goto cleanup;
  1611. err = ext4_ext_dirty(handle, inode, path + depth);
  1612. cleanup:
  1613. if (npath) {
  1614. ext4_ext_drop_refs(npath);
  1615. kfree(npath);
  1616. }
  1617. ext4_ext_invalidate_cache(inode);
  1618. return err;
  1619. }
  1620. static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
  1621. ext4_lblk_t num, ext_prepare_callback func,
  1622. void *cbdata)
  1623. {
  1624. struct ext4_ext_path *path = NULL;
  1625. struct ext4_ext_cache cbex;
  1626. struct ext4_extent *ex;
  1627. ext4_lblk_t next, start = 0, end = 0;
  1628. ext4_lblk_t last = block + num;
  1629. int depth, exists, err = 0;
  1630. BUG_ON(func == NULL);
  1631. BUG_ON(inode == NULL);
  1632. while (block < last && block != EXT_MAX_BLOCKS) {
  1633. num = last - block;
  1634. /* find extent for this block */
  1635. down_read(&EXT4_I(inode)->i_data_sem);
  1636. path = ext4_ext_find_extent(inode, block, path);
  1637. up_read(&EXT4_I(inode)->i_data_sem);
  1638. if (IS_ERR(path)) {
  1639. err = PTR_ERR(path);
  1640. path = NULL;
  1641. break;
  1642. }
  1643. depth = ext_depth(inode);
  1644. if (unlikely(path[depth].p_hdr == NULL)) {
  1645. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1646. err = -EIO;
  1647. break;
  1648. }
  1649. ex = path[depth].p_ext;
  1650. next = ext4_ext_next_allocated_block(path);
  1651. exists = 0;
  1652. if (!ex) {
  1653. /* there is no extent yet, so try to allocate
  1654. * all requested space */
  1655. start = block;
  1656. end = block + num;
  1657. } else if (le32_to_cpu(ex->ee_block) > block) {
  1658. /* need to allocate space before found extent */
  1659. start = block;
  1660. end = le32_to_cpu(ex->ee_block);
  1661. if (block + num < end)
  1662. end = block + num;
  1663. } else if (block >= le32_to_cpu(ex->ee_block)
  1664. + ext4_ext_get_actual_len(ex)) {
  1665. /* need to allocate space after found extent */
  1666. start = block;
  1667. end = block + num;
  1668. if (end >= next)
  1669. end = next;
  1670. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1671. /*
  1672. * some part of requested space is covered
  1673. * by found extent
  1674. */
  1675. start = block;
  1676. end = le32_to_cpu(ex->ee_block)
  1677. + ext4_ext_get_actual_len(ex);
  1678. if (block + num < end)
  1679. end = block + num;
  1680. exists = 1;
  1681. } else {
  1682. BUG();
  1683. }
  1684. BUG_ON(end <= start);
  1685. if (!exists) {
  1686. cbex.ec_block = start;
  1687. cbex.ec_len = end - start;
  1688. cbex.ec_start = 0;
  1689. } else {
  1690. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1691. cbex.ec_len = ext4_ext_get_actual_len(ex);
  1692. cbex.ec_start = ext4_ext_pblock(ex);
  1693. }
  1694. if (unlikely(cbex.ec_len == 0)) {
  1695. EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
  1696. err = -EIO;
  1697. break;
  1698. }
  1699. err = func(inode, next, &cbex, ex, cbdata);
  1700. ext4_ext_drop_refs(path);
  1701. if (err < 0)
  1702. break;
  1703. if (err == EXT_REPEAT)
  1704. continue;
  1705. else if (err == EXT_BREAK) {
  1706. err = 0;
  1707. break;
  1708. }
  1709. if (ext_depth(inode) != depth) {
  1710. /* depth was changed. we have to realloc path */
  1711. kfree(path);
  1712. path = NULL;
  1713. }
  1714. block = cbex.ec_block + cbex.ec_len;
  1715. }
  1716. if (path) {
  1717. ext4_ext_drop_refs(path);
  1718. kfree(path);
  1719. }
  1720. return err;
  1721. }
  1722. static void
  1723. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1724. __u32 len, ext4_fsblk_t start)
  1725. {
  1726. struct ext4_ext_cache *cex;
  1727. BUG_ON(len == 0);
  1728. spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
  1729. cex = &EXT4_I(inode)->i_cached_extent;
  1730. cex->ec_block = block;
  1731. cex->ec_len = len;
  1732. cex->ec_start = start;
  1733. spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
  1734. }
  1735. /*
  1736. * ext4_ext_put_gap_in_cache:
  1737. * calculate boundaries of the gap that the requested bl