PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ext4/extents.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 4026 lines | 3198 code | 689 blank | 139 comment | 705 complexity | eddcc23f88711843b01bcd09484df9b1 MD5 | raw file
Possible License(s): GPL-2.0

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

Large files files are truncated, but you can click here to view the full file