PageRenderTime 65ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 = ext4_ext_get_actual_len(ex);
  1764. }
  1765. if (correct_index && eh->eh_entries)
  1766. err = ext4_ext_correct_indexes(handle, inode, path);
  1767. if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
  1768. (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
  1769. *partial_cluster)) {
  1770. int flags = EXT4_FREE_BLOCKS_FORGET;
  1771. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1772. flags |= EXT4_FREE_BLOCKS_METADATA;
  1773. ext4_free_blocks(handle, inode, NULL,
  1774. EXT4_C2B(sbi, *partial_cluster),
  1775. sbi->s_cluster_ratio, flags);
  1776. *partial_cluster = 0;
  1777. }
  1778. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1779. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1780. out:
  1781. return err;
  1782. }
  1783. static int
  1784. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1785. {
  1786. BUG_ON(path->p_idx == NULL);
  1787. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1788. return 0;
  1789. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1790. return 0;
  1791. return 1;
  1792. }
  1793. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  1794. ext4_lblk_t end)
  1795. {
  1796. struct super_block *sb = inode->i_sb;
  1797. int depth = ext_depth(inode);
  1798. struct ext4_ext_path *path;
  1799. ext4_fsblk_t partial_cluster = 0;
  1800. handle_t *handle;
  1801. int i, err;
  1802. ext_debug("truncate since %u to %u\n", start, end);
  1803. handle = ext4_journal_start(inode, depth + 1);
  1804. if (IS_ERR(handle))
  1805. return PTR_ERR(handle);
  1806. again:
  1807. ext4_ext_invalidate_cache(inode);
  1808. trace_ext4_ext_remove_space(inode, start, depth);
  1809. if (end < EXT_MAX_BLOCKS - 1) {
  1810. struct ext4_extent *ex;
  1811. ext4_lblk_t ee_block;
  1812. path = ext4_ext_find_extent(inode, end, NULL);
  1813. if (IS_ERR(path)) {
  1814. ext4_journal_stop(handle);
  1815. return PTR_ERR(path);
  1816. }
  1817. depth = ext_depth(inode);
  1818. ex = path[depth].p_ext;
  1819. if (!ex)
  1820. goto cont;
  1821. ee_block = le32_to_cpu(ex->ee_block);
  1822. if (end >= ee_block &&
  1823. end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
  1824. int split_flag = 0;
  1825. if (ext4_ext_is_uninitialized(ex))
  1826. split_flag = EXT4_EXT_MARK_UNINIT1 |
  1827. EXT4_EXT_MARK_UNINIT2;
  1828. err = ext4_split_extent_at(handle, inode, path,
  1829. end + 1, split_flag,
  1830. EXT4_GET_BLOCKS_PRE_IO |
  1831. EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
  1832. if (err < 0)
  1833. goto out;
  1834. }
  1835. ext4_ext_drop_refs(path);
  1836. kfree(path);
  1837. }
  1838. cont:
  1839. depth = ext_depth(inode);
  1840. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
  1841. if (path == NULL) {
  1842. ext4_journal_stop(handle);
  1843. return -ENOMEM;
  1844. }
  1845. path[0].p_depth = depth;
  1846. path[0].p_hdr = ext_inode_hdr(inode);
  1847. if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
  1848. err = -EIO;
  1849. goto out;
  1850. }
  1851. i = err = 0;
  1852. while (i >= 0 && err == 0) {
  1853. if (i == depth) {
  1854. err = ext4_ext_rm_leaf(handle, inode, path,
  1855. &partial_cluster, start,
  1856. end);
  1857. brelse(path[i].p_bh);
  1858. path[i].p_bh = NULL;
  1859. i--;
  1860. continue;
  1861. }
  1862. if (!path[i].p_hdr) {
  1863. ext_debug("initialize header\n");
  1864. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1865. }
  1866. if (!path[i].p_idx) {
  1867. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1868. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1869. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1870. path[i].p_hdr,
  1871. le16_to_cpu(path[i].p_hdr->eh_entries));
  1872. } else {
  1873. path[i].p_idx--;
  1874. }
  1875. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1876. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1877. path[i].p_idx);
  1878. if (ext4_ext_more_to_rm(path + i)) {
  1879. struct buffer_head *bh;
  1880. ext_debug("move to level %d (block %llu)\n",
  1881. i + 1, ext4_idx_pblock(path[i].p_idx));
  1882. memset(path + i + 1, 0, sizeof(*path));
  1883. bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
  1884. if (!bh) {
  1885. err = -EIO;
  1886. break;
  1887. }
  1888. if (WARN_ON(i + 1 > depth)) {
  1889. err = -EIO;
  1890. break;
  1891. }
  1892. if (ext4_ext_check(inode, ext_block_hdr(bh),
  1893. depth - i - 1)) {
  1894. err = -EIO;
  1895. break;
  1896. }
  1897. path[i + 1].p_bh = bh;
  1898. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1899. i++;
  1900. } else {
  1901. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1902. err = ext4_ext_rm_idx(handle, inode, path + i);
  1903. }
  1904. brelse(path[i].p_bh);
  1905. path[i].p_bh = NULL;
  1906. i--;
  1907. ext_debug("return to level %d\n", i);
  1908. }
  1909. }
  1910. trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
  1911. path->p_hdr->eh_entries);
  1912. if (partial_cluster && path->p_hdr->eh_entries == 0) {
  1913. int flags = EXT4_FREE_BLOCKS_FORGET;
  1914. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1915. flags |= EXT4_FREE_BLOCKS_METADATA;
  1916. ext4_free_blocks(handle, inode, NULL,
  1917. EXT4_C2B(EXT4_SB(sb), partial_cluster),
  1918. EXT4_SB(sb)->s_cluster_ratio, flags);
  1919. partial_cluster = 0;
  1920. }
  1921. if (path->p_hdr->eh_entries == 0) {
  1922. err = ext4_ext_get_access(handle, inode, path);
  1923. if (err == 0) {
  1924. ext_inode_hdr(inode)->eh_depth = 0;
  1925. ext_inode_hdr(inode)->eh_max =
  1926. cpu_to_le16(ext4_ext_space_root(inode, 0));
  1927. err = ext4_ext_dirty(handle, inode, path);
  1928. }
  1929. }
  1930. out:
  1931. ext4_ext_drop_refs(path);
  1932. kfree(path);
  1933. if (err == -EAGAIN)
  1934. goto again;
  1935. ext4_journal_stop(handle);
  1936. return err;
  1937. }
  1938. void ext4_ext_init(struct super_block *sb)
  1939. {
  1940. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  1941. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  1942. printk(KERN_INFO "EXT4-fs: file extents enabled"
  1943. #ifdef AGGRESSIVE_TEST
  1944. ", aggressive tests"
  1945. #endif
  1946. #ifdef CHECK_BINSEARCH
  1947. ", check binsearch"
  1948. #endif
  1949. #ifdef EXTENTS_STATS
  1950. ", stats"
  1951. #endif
  1952. "\n");
  1953. #endif
  1954. #ifdef EXTENTS_STATS
  1955. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1956. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1957. EXT4_SB(sb)->s_ext_max = 0;
  1958. #endif
  1959. }
  1960. }
  1961. void ext4_ext_release(struct super_block *sb)
  1962. {
  1963. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
  1964. return;
  1965. #ifdef EXTENTS_STATS
  1966. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1967. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1968. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1969. sbi->s_ext_blocks, sbi->s_ext_extents,
  1970. sbi->s_ext_blocks / sbi->s_ext_extents);
  1971. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1972. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1973. }
  1974. #endif
  1975. }
  1976. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  1977. {
  1978. ext4_fsblk_t ee_pblock;
  1979. unsigned int ee_len;
  1980. int ret;
  1981. ee_len = ext4_ext_get_actual_len(ex);
  1982. ee_pblock = ext4_ext_pblock(ex);
  1983. ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
  1984. if (ret > 0)
  1985. ret = 0;
  1986. return ret;
  1987. }
  1988. static int ext4_split_extent_at(handle_t *handle,
  1989. struct inode *inode,
  1990. struct ext4_ext_path *path,
  1991. ext4_lblk_t split,
  1992. int split_flag,
  1993. int flags)
  1994. {
  1995. ext4_fsblk_t newblock;
  1996. ext4_lblk_t ee_block;
  1997. struct ext4_extent *ex, newex, orig_ex;
  1998. struct ext4_extent *ex2 = NULL;
  1999. unsigned int ee_len, depth;
  2000. int err = 0;
  2001. ext_debug("ext4_split_extents_at: inode %lu, logical"
  2002. "block %llu\n", inode->i_ino, (unsigned long long)split);
  2003. ext4_ext_show_leaf(inode, path);
  2004. depth = ext_depth(inode);
  2005. ex = path[depth].p_ext;
  2006. ee_block = le32_to_cpu(ex->ee_block);
  2007. ee_len = ext4_ext_get_actual_len(ex);
  2008. newblock = split - ee_block + ext4_ext_pblock(ex);
  2009. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2010. err = ext4_ext_get_access(handle, inode, path + depth);
  2011. if (err)
  2012. goto out;
  2013. if (split == ee_block) {
  2014. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2015. ext4_ext_mark_uninitialized(ex);
  2016. else
  2017. ext4_ext_mark_initialized(ex);
  2018. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2019. ext4_ext_try_to_merge(inode, path, ex);
  2020. err = ext4_ext_dirty(handle, inode, path + depth);
  2021. goto out;
  2022. }
  2023. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2024. ex->ee_len = cpu_to_le16(split - ee_block);
  2025. if (split_flag & EXT4_EXT_MARK_UNINIT1)
  2026. ext4_ext_mark_uninitialized(ex);
  2027. err = ext4_ext_dirty(handle, inode, path + depth);
  2028. if (err)
  2029. goto fix_extent_len;
  2030. ex2 = &newex;
  2031. ex2->ee_block = cpu_to_le32(split);
  2032. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2033. ext4_ext_store_pblock(ex2, newblock);
  2034. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2035. ext4_ext_mark_uninitialized(ex2);
  2036. err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  2037. if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2038. err = ext4_ext_zeroout(inode, &orig_ex);
  2039. if (err)
  2040. goto fix_extent_len;
  2041. ex->ee_len = cpu_to_le16(ee_len);
  2042. ext4_ext_try_to_merge(inode, path, ex);
  2043. err = ext4_ext_dirty(handle, inode, path + depth);
  2044. goto out;
  2045. } else if (err)
  2046. goto fix_extent_len;
  2047. out:
  2048. ext4_ext_show_leaf(inode, path);
  2049. return err;
  2050. fix_extent_len:
  2051. ex->ee_len = orig_ex.ee_len;
  2052. ext4_ext_dirty(handle, inode, path + depth);
  2053. return err;
  2054. }
  2055. static int ext4_split_extent(handle_t *handle,
  2056. struct inode *inode,
  2057. struct ext4_ext_path *path,
  2058. struct ext4_map_blocks *map,
  2059. int split_flag,
  2060. int flags)
  2061. {
  2062. ext4_lblk_t ee_block;
  2063. struct ext4_extent *ex;
  2064. unsigned int ee_len, depth;
  2065. int err = 0;
  2066. int uninitialized;
  2067. int split_flag1, flags1;
  2068. depth = ext_depth(inode);
  2069. ex = path[depth].p_ext;
  2070. ee_block = le32_to_cpu(ex->ee_block);
  2071. ee_len = ext4_ext_get_actual_len(ex);
  2072. uninitialized = ext4_ext_is_uninitialized(ex);
  2073. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2074. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2075. EXT4_EXT_MAY_ZEROOUT : 0;
  2076. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2077. if (uninitialized)
  2078. split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
  2079. EXT4_EXT_MARK_UNINIT2;
  2080. err = ext4_split_extent_at(handle, inode, path,
  2081. map->m_lblk + map->m_len, split_flag1, flags1);
  2082. if (err)
  2083. goto out;
  2084. }
  2085. ext4_ext_drop_refs(path);
  2086. path = ext4_ext_find_extent(inode, map->m_lblk, path);
  2087. if (IS_ERR(path))
  2088. return PTR_ERR(path);
  2089. if (map->m_lblk >= ee_block) {
  2090. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
  2091. EXT4_EXT_MAY_ZEROOUT : 0;
  2092. if (uninitialized)
  2093. split_flag1 |= EXT4_EXT_MARK_UNINIT1;
  2094. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2095. split_flag1 |= EXT4_EXT_MARK_UNINIT2;
  2096. err = ext4_split_extent_at(handle, inode, path,
  2097. map->m_lblk, split_flag1, flags);
  2098. if (err)
  2099. goto out;
  2100. }
  2101. ext4_ext_show_leaf(inode, path);
  2102. out:
  2103. return err ? err : map->m_len;
  2104. }
  2105. #define EXT4_EXT_ZERO_LEN 7
  2106. static int ext4_ext_convert_to_initialized(handle_t *handle,
  2107. struct inode *inode,
  2108. struct ext4_map_blocks *map,
  2109. struct ext4_ext_path *path)
  2110. {
  2111. struct ext4_extent_header *eh;
  2112. struct ext4_map_blocks split_map;
  2113. struct ext4_extent zero_ex;
  2114. struct ext4_extent *ex;
  2115. ext4_lblk_t ee_block, eof_block;
  2116. unsigned int ee_len, depth;
  2117. int allocated;
  2118. int err = 0;
  2119. int split_flag = 0;
  2120. ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
  2121. "block %llu, max_blocks %u\n", inode->i_ino,
  2122. (unsigned long long)map->m_lblk, map->m_len);
  2123. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2124. inode->i_sb->s_blocksize_bits;
  2125. if (eof_block < map->m_lblk + map->m_len)
  2126. eof_block = map->m_lblk + map->m_len;
  2127. depth = ext_depth(inode);
  2128. eh = path[depth].p_hdr;
  2129. ex = path[depth].p_ext;
  2130. ee_block = le32_to_cpu(ex->ee_block);
  2131. ee_len = ext4_ext_get_actual_len(ex);
  2132. allocated = ee_len - (map->m_lblk - ee_block);
  2133. trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
  2134. BUG_ON(!ext4_ext_is_uninitialized(ex));
  2135. BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
  2136. if ((map->m_lblk == ee_block) &&
  2137. (map->m_len < ee_len) &&
  2138. (ex > EXT_FIRST_EXTENT(eh))) {
  2139. struct ext4_extent *prev_ex;
  2140. ext4_lblk_t prev_lblk;
  2141. ext4_fsblk_t prev_pblk, ee_pblk;
  2142. unsigned int prev_len, write_len;
  2143. prev_ex = ex - 1;
  2144. prev_lblk = le32_to_cpu(prev_ex->ee_block);
  2145. prev_len = ext4_ext_get_actual_len(prev_ex);
  2146. prev_pblk = ext4_ext_pblock(prev_ex);
  2147. ee_pblk = ext4_ext_pblock(ex);
  2148. write_len = map->m_len;
  2149. if ((!ext4_ext_is_uninitialized(prev_ex)) &&
  2150. ((prev_lblk + prev_len) == ee_block) &&
  2151. ((prev_pblk + prev_len) == ee_pblk) &&
  2152. (prev_len < (EXT_INIT_MAX_LEN - write_len))) {
  2153. err = ext4_ext_get_access(handle, inode, path + depth);
  2154. if (err)
  2155. goto out;
  2156. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  2157. map, ex, prev_ex);
  2158. ex->ee_block = cpu_to_le32(ee_block + write_len);
  2159. ext4_ext_store_pblock(ex, ee_pblk + write_len);
  2160. ex->ee_len = cpu_to_le16(ee_len - write_len);
  2161. ext4_ext_mark_uninitialized(ex);
  2162. prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
  2163. ext4_ext_dirty(handle, inode, path + depth);
  2164. path[depth].p_ext = prev_ex;
  2165. allocated = write_len;
  2166. goto out;
  2167. }
  2168. }
  2169. WARN_ON(map->m_lblk < ee_block);
  2170. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2171. if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
  2172. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2173. err = ext4_ext_zeroout(inode, ex);
  2174. if (err)
  2175. goto out;
  2176. err = ext4_ext_get_access(handle, inode, path + depth);
  2177. if (err)
  2178. goto out;
  2179. ext4_ext_mark_initialized(ex);
  2180. ext4_ext_try_to_merge(inode, path, ex);
  2181. err = ext4_ext_dirty(handle, inode, path + depth);
  2182. goto out;
  2183. }
  2184. split_map.m_lblk = map->m_lblk;
  2185. split_map.m_len = map->m_len;
  2186. if (allocated > map->m_len) {
  2187. if (allocated <= EXT4_EXT_ZERO_LEN &&
  2188. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2189. zero_ex.ee_block =
  2190. cpu_to_le32(map->m_lblk);
  2191. zero_ex.ee_len = cpu_to_le16(allocated);
  2192. ext4_ext_store_pblock(&zero_ex,
  2193. ext4_ext_pblock(ex) + map->m_lblk - ee_block);
  2194. err = ext4_ext_zeroout(inode, &zero_ex);
  2195. if (err)
  2196. goto out;
  2197. split_map.m_lblk = map->m_lblk;
  2198. split_map.m_len = allocated;
  2199. } else if ((map->m_lblk - ee_block + map->m_len <
  2200. EXT4_EXT_ZERO_LEN) &&
  2201. (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2202. if (map->m_lblk != ee_block) {
  2203. zero_ex.ee_block = ex->ee_block;
  2204. zero_ex.ee_len = cpu_to_le16(map->m_lblk -
  2205. ee_block);
  2206. ext4_ext_store_pblock(&zero_ex,
  2207. ext4_ext_pblock(ex));
  2208. err = ext4_ext_zeroout(inode, &zero_ex);
  2209. if (err)
  2210. goto out;
  2211. }
  2212. split_map.m_lblk = ee_block;
  2213. split_map.m_len = map->m_lblk - ee_block + map->m_len;
  2214. allocated = map->m_len;
  2215. }
  2216. }
  2217. allocated = ext4_split_extent(handle, inode, path,
  2218. &split_map, split_flag, 0);
  2219. if (allocated < 0)
  2220. err = allocated;
  2221. out:
  2222. return err ? err : allocated;
  2223. }
  2224. /*
  2225. * This function is called by ext4_ext_map_blocks() from
  2226. * ext4_get_blocks_dio_write() when DIO to write
  2227. * to an uninitialized extent.
  2228. *
  2229. * Writing to an uninitialized extent may result in splitting the uninitialized
  2230. * extent into multiple /initialized uninitialized extents (up to three)
  2231. * There are three possibilities:
  2232. * a> There is no split required: Entire extent should be uninitialized
  2233. * b> Splits in two extents: Write is happening at either end of the extent
  2234. * c> Splits in three extents: Somone is writing in middle of the extent
  2235. *
  2236. * One of more index blocks maybe needed if the extent tree grow after
  2237. * the uninitialized extent split. To prevent ENOSPC occur at the IO
  2238. * complete, we need to split the uninitialized extent before DIO submit
  2239. * the IO. The uninitialized extent called at this time will be split
  2240. * into three uninitialized extent(at most). After IO complete, the part
  2241. * being filled will be convert to initialized by the end_io callback function
  2242. * via ext4_convert_unwritten_extents().
  2243. *
  2244. * Returns the size of uninitialized extent to be written on success.
  2245. */
  2246. static int ext4_split_unwritten_extents(handle_t *handle,
  2247. struct inode *inode,
  2248. struct ext4_map_blocks *map,
  2249. struct ext4_ext_path *path,
  2250. int flags)
  2251. {
  2252. ext4_lblk_t eof_block;
  2253. ext4_lblk_t ee_block;
  2254. struct ext4_extent *ex;
  2255. unsigned int ee_len;
  2256. int split_flag = 0, depth;
  2257. ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
  2258. "block %llu, max_blocks %u\n", inode->i_ino,
  2259. (unsigned long long)map->m_lblk, map->m_len);
  2260. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2261. inode->i_sb->s_blocksize_bits;
  2262. if (eof_block < map->m_lblk + map->m_len)
  2263. eof_block = map->m_lblk + map->m_len;
  2264. depth = ext_depth(inode);
  2265. ex = path[depth].p_ext;
  2266. ee_block = le32_to_cpu(ex->ee_block);
  2267. ee_len = ext4_ext_get_actual_len(ex);
  2268. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  2269. split_flag |= EXT4_EXT_MARK_UNINIT2;
  2270. flags |= EXT4_GET_BLOCKS_PRE_IO;
  2271. return ext4_split_extent(handle, inode, path, map, split_flag, flags);
  2272. }
  2273. static int ext4_convert_unwritten_extents_endio(handle_t *handle,
  2274. struct inode *inode,
  2275. struct ext4_ext_path *path)
  2276. {
  2277. struct ext4_extent *ex;
  2278. int depth;
  2279. int err = 0;
  2280. depth = ext_depth(inode);
  2281. ex = path[depth].p_ext;
  2282. ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
  2283. "block %llu, max_blocks %u\n", inode->i_ino,
  2284. (unsigned long long)le32_to_cpu(ex->ee_block),
  2285. ext4_ext_get_actual_len(ex));
  2286. err = ext4_ext_get_access(handle, inode, path + depth);
  2287. if (err)
  2288. goto out;
  2289. ext4_ext_mark_initialized(ex);
  2290. ext4_ext_try_to_merge(inode, path, ex);
  2291. err = ext4_ext_dirty(handle, inode, path + depth);
  2292. out:
  2293. ext4_ext_show_leaf(inode, path);
  2294. return err;
  2295. }
  2296. static void unmap_underlying_metadata_blocks(struct block_device *bdev,
  2297. sector_t block, int count)
  2298. {
  2299. int i;
  2300. for (i = 0; i < count; i++)
  2301. unmap_underlying_metadata(bdev, block + i);
  2302. }
  2303. static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
  2304. ext4_lblk_t lblk,
  2305. struct ext4_ext_path *path,
  2306. unsigned int len)
  2307. {
  2308. int i, depth;
  2309. struct ext4_extent_header *eh;
  2310. struct ext4_extent *last_ex;
  2311. if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
  2312. return 0;
  2313. depth = ext_depth(inode);
  2314. eh = path[depth].p_hdr;
  2315. if (unlikely(!eh->eh_entries))
  2316. goto out;
  2317. last_ex = EXT_LAST_EXTENT(eh);
  2318. if (lblk + len < le32_to_cpu(last_ex->ee_block) +
  2319. ext4_ext_get_actual_len(last_ex))
  2320. return 0;
  2321. for (i = depth-1; i >= 0; i--)
  2322. if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
  2323. return 0;
  2324. out:
  2325. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  2326. return ext4_mark_inode_dirty(handle, inode);
  2327. }
  2328. static int ext4_find_delalloc_range(struct inode *inode,
  2329. ext4_lblk_t lblk_start,
  2330. ext4_lblk_t lblk_end,
  2331. int search_hint_reverse)
  2332. {
  2333. struct address_space *mapping = inode->i_mapping;
  2334. struct buffer_head *head, *bh = NULL;
  2335. struct page *page;
  2336. ext4_lblk_t i, pg_lblk;
  2337. pgoff_t index;
  2338. if (!test_opt(inode->i_sb, DELALLOC))
  2339. return 0;
  2340. if (inode->i_blkbits < PAGE_CACHE_SHIFT)
  2341. search_hint_reverse = 0;
  2342. if (search_hint_reverse)
  2343. i = lblk_end;
  2344. else
  2345. i = lblk_start;
  2346. index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2347. while ((i >= lblk_start) && (i <= lblk_end)) {
  2348. page = find_get_page(mapping, index);
  2349. if (!page)
  2350. goto nextpage;
  2351. if (!page_has_buffers(page))
  2352. goto nextpage;
  2353. head = page_buffers(page);
  2354. if (!head)
  2355. goto nextpage;
  2356. bh = head;
  2357. pg_lblk = index << (PAGE_CACHE_SHIFT -
  2358. inode->i_blkbits);
  2359. do {
  2360. if (unlikely(pg_lblk < lblk_start)) {
  2361. pg_lblk++;
  2362. continue;
  2363. }
  2364. if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
  2365. page_cache_release(page);
  2366. trace_ext4_find_delalloc_range(inode,
  2367. lblk_start, lblk_end,
  2368. search_hint_reverse,
  2369. 1, i);
  2370. return 1;
  2371. }
  2372. if (search_hint_reverse)
  2373. i--;
  2374. else
  2375. i++;
  2376. } while ((i >= lblk_start) && (i <= lblk_end) &&
  2377. ((bh = bh->b_this_page) != head));
  2378. nextpage:
  2379. if (page)
  2380. page_cache_release(page);
  2381. if (search_hint_reverse)
  2382. index--;
  2383. else
  2384. index++;
  2385. i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  2386. }
  2387. trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
  2388. search_hint_reverse, 0, 0);
  2389. return 0;
  2390. }
  2391. int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
  2392. int search_hint_reverse)
  2393. {
  2394. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2395. ext4_lblk_t lblk_start, lblk_end;
  2396. lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
  2397. lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
  2398. return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
  2399. search_hint_reverse);
  2400. }
  2401. /**
  2402. * Determines how many complete clusters (out of those specified by the 'map')
  2403. * are under delalloc and were reserved quota for.
  2404. * This function is called when we are writing out the blocks that were
  2405. * originally written with their allocation delayed, but then the space was
  2406. * allocated using fallocate() before the delayed allocation could be resolved.
  2407. * The cases to look for are:
  2408. * ('=' indicated delayed allocated blocks
  2409. * '-' indicates non-delayed allocated blocks)
  2410. * (a) partial clusters towards beginning and/or end outside of allocated range
  2411. * are not delalloc'ed.
  2412. * Ex:
  2413. * |----c---=|====c====|====c====|===-c----|
  2414. * |++++++ allocated ++++++|
  2415. * ==> 4 complete clusters in above example
  2416. *
  2417. * (b) partial cluster (outside of allocated range) towards either end is
  2418. * marked for delayed allocation. In this case, we will exclude that
  2419. * cluster.
  2420. * Ex:
  2421. * |----====c========|========c========|
  2422. * |++++++ allocated ++++++|
  2423. * ==> 1 complete clusters in above example
  2424. *
  2425. * Ex:
  2426. * |================c================|
  2427. * |++++++ allocated ++++++|
  2428. * ==> 0 complete clusters in above example
  2429. *
  2430. * The ext4_da_update_reserve_space will be called only if we
  2431. * determine here that there were some "entire" clusters that span
  2432. * this 'allocated' range.
  2433. * In the non-bigalloc case, this function will just end up returning num_blks
  2434. * without ever calling ext4_find_delalloc_range.
  2435. */
  2436. static unsigned int
  2437. get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
  2438. unsigned int num_blks)
  2439. {
  2440. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2441. ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
  2442. ext4_lblk_t lblk_from, lblk_to, c_offset;
  2443. unsigned int allocated_clusters = 0;
  2444. alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
  2445. alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
  2446. allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
  2447. trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
  2448. c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
  2449. if (c_offset) {
  2450. lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
  2451. lblk_to = lblk_from + c_offset - 1;
  2452. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
  2453. allocated_clusters--;
  2454. }
  2455. c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
  2456. if (allocated_clusters && c_offset) {
  2457. lblk_from = lblk_start + num_blks;
  2458. lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
  2459. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
  2460. allocated_clusters--;
  2461. }
  2462. return allocated_clusters;
  2463. }
  2464. static int
  2465. ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
  2466. struct ext4_map_blocks *map,
  2467. struct ext4_ext_path *path, int flags,
  2468. unsigned int allocated, ext4_fsblk_t newblock)
  2469. {
  2470. int ret = 0;
  2471. int err = 0;
  2472. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  2473. ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
  2474. "block %llu, max_blocks %u, flags %x, allocated %u\n",
  2475. inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
  2476. flags, allocated);
  2477. ext4_ext_show_leaf(inode, path);
  2478. trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
  2479. newblock);
  2480. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  2481. ret = ext4_split_unwritten_extents(handle, inode, map,
  2482. path, flags);
  2483. /*
  2484. * Flag the inode(non aio case) or end_io struct (aio case)
  2485. * that this IO needs to conversion to written when IO is
  2486. * completed
  2487. */
  2488. if (io)
  2489. ext4_set_io_unwritten_flag(inode, io);
  2490. else
  2491. ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  2492. if (ext4_should_dioread_nolock(inode))
  2493. map->m_flags |= EXT4_MAP_UNINIT;
  2494. goto out;
  2495. }
  2496. /* IO end_io complete, convert the filled extent to written */
  2497. if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
  2498. ret = ext4_convert_unwritten_extents_endio(handle, inode,
  2499. path);
  2500. if (ret >= 0) {
  2501. ext4_update_inode_fsync_trans(handle, inode, 1);
  2502. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  2503. path, map->m_len);
  2504. } else
  2505. err = ret;
  2506. goto out2;
  2507. }
  2508. /*
  2509. * repeat fallocate creation request
  2510. * we already have an unwritten extent
  2511. */
  2512. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
  2513. goto map_out;
  2514. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  2515. map->m_flags |= EXT4_MAP_UNWRITTEN;
  2516. goto out1;
  2517. }
  2518. ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
  2519. if (ret >= 0)
  2520. ext4_update_inode_fsync_trans(handle, inode, 1);
  2521. out:
  2522. if (ret <= 0) {
  2523. err = ret;
  2524. goto out2;
  2525. } else
  2526. allocated = ret;
  2527. map->m_flags |= EXT4_MAP_NEW;
  2528. if (allocated > map->m_len) {
  2529. unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
  2530. newblock + map->m_len,
  2531. allocated - map->m_len);
  2532. allocated = map->m_len;
  2533. }
  2534. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  2535. unsigned int reserved_clusters;
  2536. reserved_clusters = get_reserved_cluster_alloc(inode,
  2537. map->m_lblk, map->m_len);
  2538. if (reserved_clusters)
  2539. ext4_da_update_reserve_space(inode,
  2540. reserved_clusters,
  2541. 0);
  2542. }
  2543. map_out:
  2544. map->m_flags |= EXT4_MAP_MAPPED;
  2545. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
  2546. err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
  2547. map->m_len);
  2548. if (err < 0)
  2549. goto out2;
  2550. }
  2551. out1:
  2552. if (allocated > map->m_len)
  2553. allocated = map->m_len;
  2554. ext4_ext_show_leaf(inode, path);
  2555. map->m_pblk = newblock;
  2556. map->m_len = allocated;
  2557. out2:
  2558. if (path) {
  2559. ext4_ext_drop_refs(path);
  2560. kfree(path);
  2561. }
  2562. return err ? err : allocated;
  2563. }
  2564. static int get_implied_cluster_alloc(struct super_block *sb,
  2565. struct ext4_map_blocks *map,
  2566. struct ext4_extent *ex,
  2567. struct ext4_ext_path *path)
  2568. {
  2569. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2570. ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  2571. ext4_lblk_t ex_cluster_start, ex_cluster_end;
  2572. ext4_lblk_t rr_cluster_start;
  2573. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2574. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  2575. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  2576. ex_cluster_start = EXT4_B2C(sbi, ee_block);
  2577. ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
  2578. rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
  2579. if ((rr_cluster_start == ex_cluster_end) ||
  2580. (rr_cluster_start == ex_cluster_start)) {
  2581. if (rr_cluster_start == ex_cluster_end)
  2582. ee_start += ee_len - 1;
  2583. map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
  2584. c_offset;
  2585. map->m_len = min(map->m_len,
  2586. (unsigned) sbi->s_cluster_ratio - c_offset);
  2587. if (map->m_lblk < ee_block)
  2588. map->m_len = min(map->m_len, ee_block - map->m_lblk);
  2589. if (map->m_lblk > ee_block) {
  2590. ext4_lblk_t next = ext4_ext_next_allocated_block(path);
  2591. map->m_len = min(map->m_len, next - map->m_lblk);
  2592. }
  2593. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
  2594. return 1;
  2595. }
  2596. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
  2597. return 0;
  2598. }
  2599. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  2600. struct ext4_map_blocks *map, int flags)
  2601. {
  2602. struct ext4_ext_path *path = NULL;
  2603. struct ext4_extent newex, *ex, *ex2;
  2604. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2605. ext4_fsblk_t newblock = 0;
  2606. int free_on_err = 0, err = 0, depth, ret;
  2607. unsigned int allocated = 0, offset = 0;
  2608. unsigned int allocated_clusters = 0;
  2609. struct ext4_allocation_request ar;
  2610. ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
  2611. ext4_lblk_t cluster_offset;
  2612. ext_debug("blocks %u/%u requested for inode %lu\n",
  2613. map->m_lblk, map->m_len, inode->i_ino);
  2614. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  2615. if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
  2616. if (!newex.ee_start_lo && !newex.ee_start_hi) {
  2617. if ((sbi->s_cluster_ratio > 1) &&
  2618. ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
  2619. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  2620. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  2621. goto out2;
  2622. }
  2623. } else {
  2624. if (sbi->s_cluster_ratio > 1)
  2625. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  2626. newblock = map->m_lblk
  2627. - le32_to_cpu(newex.ee_block)
  2628. + ext4_ext_pblock(&newex);
  2629. allocated = ext4_ext_get_actual_len(&newex) -
  2630. (map->m_lblk - le32_to_cpu(newex.ee_block));
  2631. goto out;
  2632. }
  2633. }
  2634. path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
  2635. if (IS_ERR(path)) {
  2636. err = PTR_ERR(path);
  2637. path = NULL;
  2638. goto out2;
  2639. }
  2640. depth = ext_depth(inode);
  2641. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  2642. EXT4_ERROR_INODE(inode, "bad extent address "
  2643. "lblock: %lu, depth: %d pblock %lld",
  2644. (unsigned long) map->m_lblk, depth,
  2645. path[depth].p_block);
  2646. err = -EIO;
  2647. goto out2;
  2648. }
  2649. ex = path[depth].p_ext;
  2650. if (ex) {
  2651. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2652. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  2653. unsigned short ee_len;
  2654. ee_len = ext4_ext_get_actual_len(ex);
  2655. trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
  2656. if (in_range(map->m_lblk, ee_block, ee_len)) {
  2657. newblock = map->m_lblk - ee_block + ee_start;
  2658. allocated = ee_len - (map->m_lblk - ee_block);
  2659. ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
  2660. ee_block, ee_len, newblock);
  2661. if (!ext4_ext_is_uninitialized(ex)) {
  2662. ext4_ext_put_in_cache(inode, ee_block,
  2663. ee_len, ee_start);
  2664. goto out;
  2665. }
  2666. ret = ext4_ext_handle_uninitialized_extents(
  2667. handle, inode, map, path, flags,
  2668. allocated, newblock);
  2669. return ret;
  2670. }
  2671. }
  2672. if ((sbi->s_cluster_ratio > 1) &&
  2673. ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
  2674. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  2675. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  2676. ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
  2677. goto out2;
  2678. }
  2679. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  2680. newex.ee_block = cpu_to_le32(map->m_lblk);
  2681. cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  2682. if (cluster_offset && ex &&
  2683. get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
  2684. ar.len = allocated = map->m_len;
  2685. newblock = map->m_pblk;
  2686. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  2687. goto got_allocated_blocks;
  2688. }
  2689. ar.lleft = map->m_lblk;
  2690. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  2691. if (err)
  2692. goto out2;
  2693. ar.lright = map->m_lblk;
  2694. ex2 = NULL;
  2695. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
  2696. if (err)
  2697. goto out2;
  2698. if ((sbi->s_cluster_ratio > 1) && ex2 &&
  2699. get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
  2700. ar.len = allocated = map->m_len;
  2701. newblock = map->m_pblk;
  2702. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  2703. goto got_allocated_blocks;
  2704. }
  2705. if (map->m_len > EXT_INIT_MAX_LEN &&
  2706. !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  2707. map->m_len = EXT_INIT_MAX_LEN;
  2708. else if (map->m_len > EXT_UNINIT_MAX_LEN &&
  2709. (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  2710. map->m_len = EXT_UNINIT_MAX_LEN;
  2711. newex.ee_len = cpu_to_le16(map->m_len);
  2712. err = ext4_ext_check_overlap(sbi, inode, &newex, path);
  2713. if (err)
  2714. allocated = ext4_ext_get_actual_len(&newex);
  2715. else
  2716. allocated = map->m_len;
  2717. ar.inode = inode;
  2718. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  2719. ar.logical = map->m_lblk;
  2720. offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
  2721. ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
  2722. ar.goal -= offset;
  2723. ar.logical -= offset;
  2724. if (S_ISREG(inode->i_mode))
  2725. ar.flags = EXT4_MB_HINT_DATA;
  2726. else
  2727. ar.flags = 0;
  2728. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  2729. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  2730. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  2731. if (!newblock)
  2732. goto out2;
  2733. ext_debug("allocate new block: goal %llu, found %llu/%u\n",
  2734. ar.goal, newblock, allocated);
  2735. free_on_err = 1;
  2736. allocated_clusters = ar.len;
  2737. ar.len = EXT4_C2B(sbi, ar.len) - offset;
  2738. if (ar.len > allocated)
  2739. ar.len = allocated;
  2740. got_allocated_blocks:
  2741. ext4_ext_store_pblock(&newex, newblock + offset);
  2742. newex.ee_len = cpu_to_le16(ar.len);
  2743. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
  2744. ext4_ext_mark_uninitialized(&newex);
  2745. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  2746. if (io)
  2747. ext4_set_io_unwritten_flag(inode, io);
  2748. else
  2749. ext4_set_inode_state(inode,
  2750. EXT4_STATE_DIO_UNWRITTEN);
  2751. }
  2752. if (ext4_should_dioread_nolock(inode))
  2753. map->m_flags |= EXT4_MAP_UNINIT;
  2754. }
  2755. err = 0;
  2756. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
  2757. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  2758. path, ar.len);
  2759. if (!err)
  2760. err = ext4_ext_insert_extent(handle, inode, path,
  2761. &newex, flags);
  2762. if (err && free_on_err) {
  2763. int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
  2764. EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
  2765. ext4_discard_preallocations(inode);
  2766. ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
  2767. ext4_ext_get_actual_len(&newex), fb_flags);
  2768. goto out2;
  2769. }
  2770. newblock = ext4_ext_pblock(&newex);
  2771. allocated = ext4_ext_get_actual_len(&newex);
  2772. if (allocated > map->m_len)
  2773. allocated = map->m_len;
  2774. map->m_flags |= EXT4_MAP_NEW;
  2775. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  2776. unsigned int reserved_clusters;
  2777. reserved_clusters = get_reserved_cluster_alloc(inode,
  2778. map->m_lblk, allocated);
  2779. if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
  2780. if (reserved_clusters) {
  2781. ext4_da_update_reserve_space(inode,
  2782. reserved_clusters, 0);
  2783. }
  2784. } else {
  2785. BUG_ON(allocated_clusters < reserved_clusters);
  2786. ext4_da_update_reserve_space(inode, allocated_clusters,
  2787. 1);
  2788. if (reserved_clusters < allocated_clusters) {
  2789. struct ext4_inode_info *ei = EXT4_I(inode);
  2790. int reservation = allocated_clusters -
  2791. reserved_clusters;
  2792. /*
  2793. * It seems we claimed few clusters outside of
  2794. * the range of this allocation. We should give
  2795. * it back to the reservation pool. This can
  2796. * happen in the following case:
  2797. *
  2798. * * Suppose s_cluster_ratio is 4 (i.e., each
  2799. * cluster has 4 blocks. Thus, the clusters
  2800. * are [0-3],[4-7],[8-11]...
  2801. * * First comes delayed allocation write for
  2802. * logical blocks 10 & 11. Since there were no
  2803. * previous delayed allocated blocks in the
  2804. * range [8-11], we would reserve 1 cluster
  2805. * for this write.
  2806. * * Next comes write for logical blocks 3 to 8.
  2807. * In this case, we will reserve 2 clusters
  2808. * (for [0-3] and [4-7]; and not for [8-11] as
  2809. * that range has a delayed allocated blocks.
  2810. * Thus total reserved clusters now becomes 3.
  2811. * * Now, during the delayed allocation writeout
  2812. * time, we will first write blocks [3-8] and
  2813. * allocate 3 clusters for writing these
  2814. * blocks. Also, we would claim all these
  2815. * three clusters above.
  2816. * * Now when we come here to writeout the
  2817. * blocks [10-11], we would expect to claim
  2818. * the reservation of 1 cluster we had made
  2819. * (and we would claim it since there are no
  2820. * more delayed allocated blocks in the range
  2821. * [8-11]. But our reserved cluster count had
  2822. * already gone to 0.
  2823. *
  2824. * Thus, at the step 4 above when we determine
  2825. * that there are still some unwritten delayed
  2826. * allocated blocks outside of our current
  2827. * block range, we should increment the
  2828. * reserved clusters count so that when the
  2829. * remaining blocks finally gets written, we
  2830. * could claim them.
  2831. */
  2832. dquot_reserve_block(inode,
  2833. EXT4_C2B(sbi, reservation));
  2834. spin_lock(&ei->i_block_reservation_lock);
  2835. ei->i_reserved_data_blocks += reservation;
  2836. spin_unlock(&ei->i_block_reservation_lock);
  2837. }
  2838. }
  2839. }
  2840. if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
  2841. ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
  2842. ext4_update_inode_fsync_trans(handle, inode, 1);
  2843. } else
  2844. ext4_update_inode_fsync_trans(handle, inode, 0);
  2845. out:
  2846. if (allocated > map->m_len)
  2847. allocated = map->m_len;
  2848. ext4_ext_show_leaf(inode, path);
  2849. map->m_flags |= EXT4_MAP_MAPPED;
  2850. map->m_pblk = newblock;
  2851. map->m_len = allocated;
  2852. out2:
  2853. if (path) {
  2854. ext4_ext_drop_refs(path);
  2855. kfree(path);
  2856. }
  2857. trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
  2858. newblock, map->m_len, err ? err : allocated);
  2859. return err ? err : allocated;
  2860. }
  2861. void ext4_ext_truncate(struct inode *inode)
  2862. {
  2863. struct address_space *mapping = inode->i_mapping;
  2864. struct super_block *sb = inode->i_sb;
  2865. ext4_lblk_t last_block;
  2866. handle_t *handle;
  2867. loff_t page_len;
  2868. int err = 0;
  2869. ext4_flush_completed_IO(inode);
  2870. err = ext4_writepage_trans_blocks(inode);
  2871. handle = ext4_journal_start(inode, err);
  2872. if (IS_ERR(handle))
  2873. return;
  2874. if (inode->i_size % PAGE_CACHE_SIZE != 0) {
  2875. page_len = PAGE_CACHE_SIZE -
  2876. (inode->i_size & (PAGE_CACHE_SIZE - 1));
  2877. err = ext4_discard_partial_page_buffers(handle,
  2878. mapping, inode->i_size, page_len, 0);
  2879. if (err)
  2880. goto out_stop;
  2881. }
  2882. if (ext4_orphan_add(handle, inode))
  2883. goto out_stop;
  2884. down_write(&EXT4_I(inode)->i_data_sem);
  2885. ext4_ext_invalidate_cache(inode);
  2886. ext4_discard_preallocations(inode);
  2887. EXT4_I(inode)->i_disksize = inode->i_size;
  2888. ext4_mark_inode_dirty(handle, inode);
  2889. last_block = (inode->i_size + sb->s_blocksize - 1)
  2890. >> EXT4_BLOCK_SIZE_BITS(sb);
  2891. err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
  2892. if (IS_SYNC(inode))
  2893. ext4_handle_sync(handle);
  2894. up_write(&EXT4_I(inode)->i_data_sem);
  2895. out_stop:
  2896. if (inode->i_nlink)
  2897. ext4_orphan_del(handle, inode);
  2898. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  2899. ext4_mark_inode_dirty(handle, inode);
  2900. ext4_journal_stop(handle);
  2901. }
  2902. static void ext4_falloc_update_inode(struct inode *inode,
  2903. int mode, loff_t new_size, int update_ctime)
  2904. {
  2905. struct timespec now;
  2906. if (update_ctime) {
  2907. now = current_fs_time(inode->i_sb);
  2908. if (!timespec_equal(&inode->i_ctime, &now))
  2909. inode->i_ctime = now;
  2910. }
  2911. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  2912. if (new_size > i_size_read(inode))
  2913. i_size_write(inode, new_size);
  2914. if (new_size > EXT4_I(inode)->i_disksize)
  2915. ext4_update_i_disksize(inode, new_size);
  2916. } else {
  2917. if (new_size > i_size_read(inode))
  2918. ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  2919. }
  2920. }
  2921. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  2922. {
  2923. struct inode *inode = file->f_path.dentry->d_inode;
  2924. handle_t *handle;
  2925. loff_t new_size;
  2926. unsigned int max_blocks;
  2927. int ret = 0;
  2928. int ret2 = 0;
  2929. int retries = 0;
  2930. int flags;
  2931. struct ext4_map_blocks map;
  2932. unsigned int credits, blkbits = inode->i_blkbits;
  2933. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  2934. return -EOPNOTSUPP;
  2935. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  2936. return -EOPNOTSUPP;
  2937. if (mode & FALLOC_FL_PUNCH_HOLE)
  2938. return ext4_punch_hole(file, offset, len);
  2939. trace_ext4_fallocate_enter(inode, offset, len, mode);
  2940. map.m_lblk = offset >> blkbits;
  2941. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  2942. - map.m_lblk;
  2943. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  2944. mutex_lock(&inode->i_mutex);
  2945. ret = inode_newsize_ok(inode, (len + offset));
  2946. if (ret) {
  2947. mutex_unlock(&inode->i_mutex);
  2948. trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
  2949. return ret;
  2950. }
  2951. flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
  2952. if (mode & FALLOC_FL_KEEP_SIZE)
  2953. flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
  2954. if (len <= EXT_UNINIT_MAX_LEN << blkbits)
  2955. flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
  2956. retry:
  2957. while (ret >= 0 && ret < max_blocks) {
  2958. map.m_lblk = map.m_lblk + ret;
  2959. map.m_len = max_blocks = max_blocks - ret;
  2960. handle = ext4_journal_start(inode, credits);
  2961. if (IS_ERR(handle)) {
  2962. ret = PTR_ERR(handle);
  2963. break;
  2964. }
  2965. ret = ext4_map_blocks(handle, inode, &map, flags);
  2966. if (ret <= 0) {
  2967. #ifdef EXT4FS_DEBUG
  2968. WARN_ON(ret <= 0);
  2969. printk(KERN_ERR "%s: ext4_ext_map_blocks "
  2970. "returned error inode#%lu, block=%u, "
  2971. "max_blocks=%u", __func__,
  2972. inode->i_ino, map.m_lblk, max_blocks);
  2973. #endif
  2974. ext4_mark_inode_dirty(handle, inode);
  2975. ret2 = ext4_journal_stop(handle);
  2976. break;
  2977. }
  2978. if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  2979. blkbits) >> blkbits))
  2980. new_size = offset + len;
  2981. else
  2982. new_size = ((loff_t) map.m_lblk + ret) << blkbits;
  2983. ext4_falloc_update_inode(inode, mode, new_size,
  2984. (map.m_flags & EXT4_MAP_NEW));
  2985. ext4_mark_inode_dirty(handle, inode);
  2986. ret2 = ext4_journal_stop(handle);
  2987. if (ret2)
  2988. break;
  2989. }
  2990. if (ret == -ENOSPC &&
  2991. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  2992. ret = 0;
  2993. goto retry;
  2994. }
  2995. mutex_unlock(&inode->i_mutex);
  2996. trace_ext4_fallocate_exit(inode, offset, max_blocks,
  2997. ret > 0 ? ret2 : ret);
  2998. return ret > 0 ? ret2 : ret;
  2999. }
  3000. /*
  3001. * This function convert a range of blocks to written extents
  3002. * The caller of this function will pass the start offset and the size.
  3003. * all unwritten extents within this range will be converted to
  3004. * written extents.
  3005. *
  3006. * This function is called from the direct IO end io call back
  3007. * function, to convert the fallocated extents after IO is completed.
  3008. * Returns 0 on success.
  3009. */
  3010. int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
  3011. ssize_t len)
  3012. {
  3013. handle_t *handle;
  3014. unsigned int max_blocks;
  3015. int ret = 0;
  3016. int ret2 = 0;
  3017. struct ext4_map_blocks map;
  3018. unsigned int credits, blkbits = inode->i_blkbits;
  3019. map.m_lblk = offset >> blkbits;
  3020. max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
  3021. map.m_lblk);
  3022. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  3023. while (ret >= 0 && ret < max_blocks) {
  3024. map.m_lblk += ret;
  3025. map.m_len = (max_blocks -= ret);
  3026. handle = ext4_journal_start(inode, credits);
  3027. if (IS_ERR(handle)) {
  3028. ret = PTR_ERR(handle);
  3029. break;
  3030. }
  3031. ret = ext4_map_blocks(handle, inode, &map,
  3032. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  3033. if (ret <= 0) {
  3034. WARN_ON(ret <= 0);
  3035. ext4_msg(inode->i_sb, KERN_ERR,
  3036. "%s:%d: inode #%lu: block %u: len %u: "
  3037. "ext4_ext_map_blocks returned %d",
  3038. __func__, __LINE__, inode->i_ino, map.m_lblk,
  3039. map.m_len, ret);
  3040. }
  3041. ext4_mark_inode_dirty(handle, inode);
  3042. ret2 = ext4_journal_stop(handle);
  3043. if (ret <= 0 || ret2 )
  3044. break;
  3045. }
  3046. return ret > 0 ? ret2 : ret;
  3047. }
  3048. static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
  3049. struct ext4_ext_cache *newex, struct ext4_extent *ex,
  3050. void *data)
  3051. {
  3052. __u64 logical;
  3053. __u64 physical;
  3054. __u64 length;
  3055. __u32 flags = 0;
  3056. int ret = 0;
  3057. struct fiemap_extent_info *fieinfo = data;
  3058. unsigned char blksize_bits;
  3059. blksize_bits = inode->i_sb->s_blocksize_bits;
  3060. logical = (__u64)newex->ec_block << blksize_bits;
  3061. if (newex->ec_start == 0) {
  3062. ext4_lblk_t end = 0;
  3063. pgoff_t last_offset;
  3064. pgoff_t offset;
  3065. pgoff_t index;
  3066. pgoff_t start_index = 0;
  3067. struct page **pages = NULL;
  3068. struct buffer_head *bh = NULL;
  3069. struct buffer_head *head = NULL;
  3070. unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
  3071. pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3072. if (pages == NULL)
  3073. return -ENOMEM;
  3074. offset = logical >> PAGE_SHIFT;
  3075. repeat:
  3076. last_offset = offset;
  3077. head = NULL;
  3078. ret = find_get_pages_tag(inode->i_mapping, &offset,
  3079. PAGECACHE_TAG_DIRTY, nr_pages, pages);
  3080. if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
  3081. if (ret == 0) {
  3082. out:
  3083. for (index = 0; index < ret; index++)
  3084. page_cache_release(pages[index]);
  3085. kfree(pages);
  3086. return EXT_CONTINUE;
  3087. }
  3088. index = 0;
  3089. next_page:
  3090. end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
  3091. blksize_bits;
  3092. if (!page_has_buffers(pages[index]))
  3093. goto out;
  3094. head = page_buffers(pages[index]);
  3095. if (!head)
  3096. goto out;
  3097. index++;
  3098. bh = head;
  3099. do {
  3100. if (end >= newex->ec_block +
  3101. newex->ec_len)
  3102. goto out;
  3103. if (buffer_mapped(bh) &&
  3104. end >= newex->ec_block) {
  3105. start_index = index - 1;
  3106. goto found_mapped_buffer;
  3107. }
  3108. bh = bh->b_this_page;
  3109. end++;
  3110. } while (bh != head);
  3111. if (index >= ret) {
  3112. newex->ec_len = end - newex->ec_block;
  3113. goto out;
  3114. }
  3115. goto next_page;
  3116. } else {
  3117. if (ret > 0 && pages[0]->index == last_offset)
  3118. head = page_buffers(pages[0]);
  3119. bh = head;
  3120. index = 1;
  3121. start_index = 0;
  3122. }
  3123. found_mapped_buffer:
  3124. if (bh != NULL && buffer_delay(bh)) {
  3125. if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
  3126. flags |= FIEMAP_EXTENT_DELALLOC;
  3127. newex->ec_block = end;
  3128. logical = (__u64)end << blksize_bits;
  3129. }
  3130. do {
  3131. if (!buffer_delay(bh))
  3132. goto found_delayed_extent;
  3133. bh = bh->b_this_page;
  3134. end++;
  3135. } while (bh != head);
  3136. for (; index < ret; index++) {
  3137. if (!page_has_buffers(pages[index])) {
  3138. bh = NULL;
  3139. break;
  3140. }
  3141. head = page_buffers(pages[index]);
  3142. if (!head) {
  3143. bh = NULL;
  3144. break;
  3145. }
  3146. if (pages[index]->index !=
  3147. pages[start_index]->index + index
  3148. - start_index) {
  3149. bh = NULL;
  3150. break;
  3151. }
  3152. bh = head;
  3153. do {
  3154. if (!buffer_delay(bh))
  3155. goto found_delayed_extent;
  3156. bh = bh->b_this_page;
  3157. end++;
  3158. } while (bh != head);
  3159. }
  3160. } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
  3161. goto out;
  3162. found_delayed_extent:
  3163. newex->ec_len = min(end - newex->ec_block,
  3164. (ext4_lblk_t)EXT_INIT_MAX_LEN);
  3165. if (ret == nr_pages && bh != NULL &&
  3166. newex->ec_len < EXT_INIT_MAX_LEN &&
  3167. buffer_delay(bh)) {
  3168. for (index = 0; index < ret; index++)
  3169. page_cache_release(pages[index]);
  3170. goto repeat;
  3171. }
  3172. for (index = 0; index < ret; index++)
  3173. page_cache_release(pages[index]);
  3174. kfree(pages);
  3175. }
  3176. physical = (__u64)newex->ec_start << blksize_bits;
  3177. length = (__u64)newex->ec_len << blksize_bits;
  3178. if (ex && ext4_ext_is_uninitialized(ex))
  3179. flags |= FIEMAP_EXTENT_UNWRITTEN;
  3180. if (next == EXT_MAX_BLOCKS)
  3181. flags |= FIEMAP_EXTENT_LAST;
  3182. ret = fiemap_fill_next_extent(fieinfo, logical, physical,
  3183. length, flags);
  3184. if (ret < 0)
  3185. return ret;
  3186. if (ret == 1)
  3187. return EXT_BREAK;
  3188. return EXT_CONTINUE;
  3189. }
  3190. #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  3191. static int ext4_xattr_fiemap(struct inode *inode,
  3192. struct fiemap_extent_info *fieinfo)
  3193. {
  3194. __u64 physical = 0;
  3195. __u64 length;
  3196. __u32 flags = FIEMAP_EXTENT_LAST;
  3197. int blockbits = inode->i_sb->s_blocksize_bits;
  3198. int error = 0;
  3199. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  3200. struct ext4_iloc iloc;
  3201. int offset;
  3202. error = ext4_get_inode_loc(inode, &iloc);
  3203. if (error)
  3204. return error;
  3205. physical = iloc.bh->b_blocknr << blockbits;
  3206. offset = EXT4_GOOD_OLD_INODE_SIZE +
  3207. EXT4_I(inode)->i_extra_isize;
  3208. physical += offset;
  3209. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  3210. flags |= FIEMAP_EXTENT_DATA_INLINE;
  3211. brelse(iloc.bh);
  3212. } else {
  3213. physical = EXT4_I(inode)->i_file_acl << blockbits;
  3214. length = inode->i_sb->s_blocksize;
  3215. }
  3216. if (physical)
  3217. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  3218. length, flags);
  3219. return (error < 0 ? error : 0);
  3220. }
  3221. int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
  3222. {
  3223. struct inode *inode = file->f_path.dentry->d_inode;
  3224. struct super_block *sb = inode->i_sb;
  3225. ext4_lblk_t first_block, stop_block;
  3226. struct address_space *mapping = inode->i_mapping;
  3227. handle_t *handle;
  3228. loff_t first_page, last_page, page_len;
  3229. loff_t first_page_offset, last_page_offset;
  3230. int credits, err = 0;
  3231. if (offset >= inode->i_size)
  3232. return 0;
  3233. if (offset + length > inode->i_size) {
  3234. length = inode->i_size +
  3235. PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
  3236. offset;
  3237. }
  3238. first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  3239. last_page = (offset + length) >> PAGE_CACHE_SHIFT;
  3240. first_page_offset = first_page << PAGE_CACHE_SHIFT;
  3241. last_page_offset = last_page << PAGE_CACHE_SHIFT;
  3242. if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
  3243. err = filemap_write_and_wait_range(mapping,
  3244. offset, offset + length - 1);
  3245. if (err)
  3246. return err;
  3247. }
  3248. if (last_page_offset > first_page_offset) {
  3249. truncate_inode_pages_range(mapping, first_page_offset,
  3250. last_page_offset-1);
  3251. }
  3252. ext4_flush_completed_IO(inode);
  3253. credits = ext4_writepage_trans_blocks(inode);
  3254. handle = ext4_journal_start(inode, credits);
  3255. if (IS_ERR(handle))
  3256. return PTR_ERR(handle);
  3257. err = ext4_orphan_add(handle, inode);
  3258. if (err)
  3259. goto out;
  3260. if (first_page > last_page) {
  3261. err = ext4_discard_partial_page_buffers(handle,
  3262. mapping, offset, length, 0);
  3263. if (err)
  3264. goto out;
  3265. } else {
  3266. page_len = first_page_offset - offset;
  3267. if (page_len > 0) {
  3268. err = ext4_discard_partial_page_buffers(handle, mapping,
  3269. offset, page_len, 0);
  3270. if (err)
  3271. goto out;
  3272. }
  3273. page_len = offset + length - last_page_offset;
  3274. if (page_len > 0) {
  3275. err = ext4_discard_partial_page_buffers(handle, mapping,
  3276. last_page_offset, page_len, 0);
  3277. if (err)
  3278. goto out;
  3279. }
  3280. }
  3281. if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
  3282. inode->i_size % PAGE_CACHE_SIZE != 0) {
  3283. page_len = PAGE_CACHE_SIZE -
  3284. (inode->i_size & (PAGE_CACHE_SIZE - 1));
  3285. if (page_len > 0) {
  3286. err = ext4_discard_partial_page_buffers(handle,
  3287. mapping, inode->i_size, page_len, 0);
  3288. if (err)
  3289. goto out;
  3290. }
  3291. }
  3292. first_block = (offset + sb->s_blocksize - 1) >>
  3293. EXT4_BLOCK_SIZE_BITS(sb);
  3294. stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
  3295. if (first_block >= stop_block)
  3296. goto out;
  3297. down_write(&EXT4_I(inode)->i_data_sem);
  3298. ext4_ext_invalidate_cache(inode);
  3299. ext4_discard_preallocations(inode);
  3300. err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
  3301. ext4_ext_invalidate_cache(inode);
  3302. ext4_discard_preallocations(inode);
  3303. if (IS_SYNC(inode))
  3304. ext4_handle_sync(handle);
  3305. up_write(&EXT4_I(inode)->i_data_sem);
  3306. out:
  3307. ext4_orphan_del(handle, inode);
  3308. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  3309. ext4_mark_inode_dirty(handle, inode);
  3310. ext4_journal_stop(handle);
  3311. return err;
  3312. }
  3313. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3314. __u64 start, __u64 len)
  3315. {
  3316. ext4_lblk_t start_blk;
  3317. int error = 0;
  3318. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  3319. return generic_block_fiemap(inode, fieinfo, start, len,
  3320. ext4_get_block);
  3321. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  3322. return -EBADR;
  3323. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  3324. error = ext4_xattr_fiemap(inode, fieinfo);
  3325. } else {
  3326. ext4_lblk_t len_blks;
  3327. __u64 last_blk;
  3328. start_blk = start >> inode->i_sb->s_blocksize_bits;
  3329. last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
  3330. if (last_blk >= EXT_MAX_BLOCKS)
  3331. last_blk = EXT_MAX_BLOCKS-1;
  3332. len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
  3333. error = ext4_ext_walk_space(inode, start_blk, len_blks,
  3334. ext4_ext_fiemap_cb, fieinfo);
  3335. }
  3336. return error;
  3337. }