mac80211: Reset dynamic ps timer in Rx path.
[linux-2.6/mini2440.git] / fs / ext4 / extents.c
blob8b8bae4c0cf4a0c8c2d043569858106708902b26
1 /*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
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.
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.
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-
24 * Extents support for EXT4
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
32 #include <linux/module.h>
33 #include <linux/fs.h>
34 #include <linux/time.h>
35 #include <linux/jbd2.h>
36 #include <linux/highuid.h>
37 #include <linux/pagemap.h>
38 #include <linux/quotaops.h>
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/falloc.h>
42 #include <asm/uaccess.h>
43 #include <linux/fiemap.h>
44 #include "ext4_jbd2.h"
45 #include "ext4_extents.h"
49 * ext_pblock:
50 * combine low and high parts of physical block number into ext4_fsblk_t
52 ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
54 ext4_fsblk_t block;
56 block = le32_to_cpu(ex->ee_start_lo);
57 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
58 return block;
62 * idx_pblock:
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
65 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
67 ext4_fsblk_t block;
69 block = le32_to_cpu(ix->ei_leaf_lo);
70 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
71 return block;
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
79 void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
81 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
82 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
90 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
92 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
93 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
96 static int ext4_ext_truncate_extend_restart(handle_t *handle,
97 struct inode *inode,
98 int needed)
100 int err;
102 if (!ext4_handle_valid(handle))
103 return 0;
104 if (handle->h_buffer_credits > needed)
105 return 0;
106 err = ext4_journal_extend(handle, needed);
107 if (err <= 0)
108 return err;
109 err = ext4_truncate_restart_trans(handle, inode, needed);
111 * We have dropped i_data_sem so someone might have cached again
112 * an extent we are going to truncate.
114 ext4_ext_invalidate_cache(inode);
116 return err;
120 * could return:
121 * - EROFS
122 * - ENOMEM
124 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
125 struct ext4_ext_path *path)
127 if (path->p_bh) {
128 /* path points to block */
129 return ext4_journal_get_write_access(handle, path->p_bh);
131 /* path points to leaf/index in inode body */
132 /* we use in-core data, no need to protect them */
133 return 0;
137 * could return:
138 * - EROFS
139 * - ENOMEM
140 * - EIO
142 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
143 struct ext4_ext_path *path)
145 int err;
146 if (path->p_bh) {
147 /* path points to block */
148 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
149 } else {
150 /* path points to leaf/index in inode body */
151 err = ext4_mark_inode_dirty(handle, inode);
153 return err;
156 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
157 struct ext4_ext_path *path,
158 ext4_lblk_t block)
160 struct ext4_inode_info *ei = EXT4_I(inode);
161 ext4_fsblk_t bg_start;
162 ext4_fsblk_t last_block;
163 ext4_grpblk_t colour;
164 ext4_group_t block_group;
165 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
166 int depth;
168 if (path) {
169 struct ext4_extent *ex;
170 depth = path->p_depth;
172 /* try to predict block placement */
173 ex = path[depth].p_ext;
174 if (ex)
175 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
177 /* it looks like index is empty;
178 * try to find starting block from index itself */
179 if (path[depth].p_bh)
180 return path[depth].p_bh->b_blocknr;
183 /* OK. use inode's group */
184 block_group = ei->i_block_group;
185 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
187 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
188 * block groups per flexgroup, reserve the first block
189 * group for directories and special files. Regular
190 * files will start at the second block group. This
191 * tends to speed up directory access and improves
192 * fsck times.
194 block_group &= ~(flex_size-1);
195 if (S_ISREG(inode->i_mode))
196 block_group++;
198 bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
199 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
200 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
203 * If we are doing delayed allocation, we don't need take
204 * colour into account.
206 if (test_opt(inode->i_sb, DELALLOC))
207 return bg_start;
209 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
210 colour = (current->pid % 16) *
211 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
212 else
213 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
214 return bg_start + colour + block;
218 * Allocation for a meta data block
220 static ext4_fsblk_t
221 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
222 struct ext4_ext_path *path,
223 struct ext4_extent *ex, int *err)
225 ext4_fsblk_t goal, newblock;
227 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
228 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
229 return newblock;
232 static inline int ext4_ext_space_block(struct inode *inode, int check)
234 int size;
236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
237 / sizeof(struct ext4_extent);
238 if (!check) {
239 #ifdef AGGRESSIVE_TEST
240 if (size > 6)
241 size = 6;
242 #endif
244 return size;
247 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
249 int size;
251 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
252 / sizeof(struct ext4_extent_idx);
253 if (!check) {
254 #ifdef AGGRESSIVE_TEST
255 if (size > 5)
256 size = 5;
257 #endif
259 return size;
262 static inline int ext4_ext_space_root(struct inode *inode, int check)
264 int size;
266 size = sizeof(EXT4_I(inode)->i_data);
267 size -= sizeof(struct ext4_extent_header);
268 size /= sizeof(struct ext4_extent);
269 if (!check) {
270 #ifdef AGGRESSIVE_TEST
271 if (size > 3)
272 size = 3;
273 #endif
275 return size;
278 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
280 int size;
282 size = sizeof(EXT4_I(inode)->i_data);
283 size -= sizeof(struct ext4_extent_header);
284 size /= sizeof(struct ext4_extent_idx);
285 if (!check) {
286 #ifdef AGGRESSIVE_TEST
287 if (size > 4)
288 size = 4;
289 #endif
291 return size;
295 * Calculate the number of metadata blocks needed
296 * to allocate @blocks
297 * Worse case is one block per extent
299 int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
301 int lcap, icap, rcap, leafs, idxs, num;
302 int newextents = blocks;
304 rcap = ext4_ext_space_root_idx(inode, 0);
305 lcap = ext4_ext_space_block(inode, 0);
306 icap = ext4_ext_space_block_idx(inode, 0);
308 /* number of new leaf blocks needed */
309 num = leafs = (newextents + lcap - 1) / lcap;
312 * Worse case, we need separate index block(s)
313 * to link all new leaf blocks
315 idxs = (leafs + icap - 1) / icap;
316 do {
317 num += idxs;
318 idxs = (idxs + icap - 1) / icap;
319 } while (idxs > rcap);
321 return num;
324 static int
325 ext4_ext_max_entries(struct inode *inode, int depth)
327 int max;
329 if (depth == ext_depth(inode)) {
330 if (depth == 0)
331 max = ext4_ext_space_root(inode, 1);
332 else
333 max = ext4_ext_space_root_idx(inode, 1);
334 } else {
335 if (depth == 0)
336 max = ext4_ext_space_block(inode, 1);
337 else
338 max = ext4_ext_space_block_idx(inode, 1);
341 return max;
344 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
346 ext4_fsblk_t block = ext_pblock(ext);
347 int len = ext4_ext_get_actual_len(ext);
349 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
352 static int ext4_valid_extent_idx(struct inode *inode,
353 struct ext4_extent_idx *ext_idx)
355 ext4_fsblk_t block = idx_pblock(ext_idx);
357 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
360 static int ext4_valid_extent_entries(struct inode *inode,
361 struct ext4_extent_header *eh,
362 int depth)
364 struct ext4_extent *ext;
365 struct ext4_extent_idx *ext_idx;
366 unsigned short entries;
367 if (eh->eh_entries == 0)
368 return 1;
370 entries = le16_to_cpu(eh->eh_entries);
372 if (depth == 0) {
373 /* leaf entries */
374 ext = EXT_FIRST_EXTENT(eh);
375 while (entries) {
376 if (!ext4_valid_extent(inode, ext))
377 return 0;
378 ext++;
379 entries--;
381 } else {
382 ext_idx = EXT_FIRST_INDEX(eh);
383 while (entries) {
384 if (!ext4_valid_extent_idx(inode, ext_idx))
385 return 0;
386 ext_idx++;
387 entries--;
390 return 1;
393 static int __ext4_ext_check(const char *function, struct inode *inode,
394 struct ext4_extent_header *eh,
395 int depth)
397 const char *error_msg;
398 int max = 0;
400 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
401 error_msg = "invalid magic";
402 goto corrupted;
404 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
405 error_msg = "unexpected eh_depth";
406 goto corrupted;
408 if (unlikely(eh->eh_max == 0)) {
409 error_msg = "invalid eh_max";
410 goto corrupted;
412 max = ext4_ext_max_entries(inode, depth);
413 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
414 error_msg = "too large eh_max";
415 goto corrupted;
417 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
418 error_msg = "invalid eh_entries";
419 goto corrupted;
421 if (!ext4_valid_extent_entries(inode, eh, depth)) {
422 error_msg = "invalid extent entries";
423 goto corrupted;
425 return 0;
427 corrupted:
428 ext4_error(inode->i_sb, function,
429 "bad header/extent in inode #%lu: %s - magic %x, "
430 "entries %u, max %u(%u), depth %u(%u)",
431 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
432 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
433 max, le16_to_cpu(eh->eh_depth), depth);
435 return -EIO;
438 #define ext4_ext_check(inode, eh, depth) \
439 __ext4_ext_check(__func__, inode, eh, depth)
441 int ext4_ext_check_inode(struct inode *inode)
443 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
446 #ifdef EXT_DEBUG
447 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
449 int k, l = path->p_depth;
451 ext_debug("path:");
452 for (k = 0; k <= l; k++, path++) {
453 if (path->p_idx) {
454 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
455 idx_pblock(path->p_idx));
456 } else if (path->p_ext) {
457 ext_debug(" %d:[%d]%d:%llu ",
458 le32_to_cpu(path->p_ext->ee_block),
459 ext4_ext_is_uninitialized(path->p_ext),
460 ext4_ext_get_actual_len(path->p_ext),
461 ext_pblock(path->p_ext));
462 } else
463 ext_debug(" []");
465 ext_debug("\n");
468 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
470 int depth = ext_depth(inode);
471 struct ext4_extent_header *eh;
472 struct ext4_extent *ex;
473 int i;
475 if (!path)
476 return;
478 eh = path[depth].p_hdr;
479 ex = EXT_FIRST_EXTENT(eh);
481 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
483 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
484 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
485 ext4_ext_is_uninitialized(ex),
486 ext4_ext_get_actual_len(ex), ext_pblock(ex));
488 ext_debug("\n");
490 #else
491 #define ext4_ext_show_path(inode, path)
492 #define ext4_ext_show_leaf(inode, path)
493 #endif
495 void ext4_ext_drop_refs(struct ext4_ext_path *path)
497 int depth = path->p_depth;
498 int i;
500 for (i = 0; i <= depth; i++, path++)
501 if (path->p_bh) {
502 brelse(path->p_bh);
503 path->p_bh = NULL;
508 * ext4_ext_binsearch_idx:
509 * binary search for the closest index of the given block
510 * the header must be checked before calling this
512 static void
513 ext4_ext_binsearch_idx(struct inode *inode,
514 struct ext4_ext_path *path, ext4_lblk_t block)
516 struct ext4_extent_header *eh = path->p_hdr;
517 struct ext4_extent_idx *r, *l, *m;
520 ext_debug("binsearch for %u(idx): ", block);
522 l = EXT_FIRST_INDEX(eh) + 1;
523 r = EXT_LAST_INDEX(eh);
524 while (l <= r) {
525 m = l + (r - l) / 2;
526 if (block < le32_to_cpu(m->ei_block))
527 r = m - 1;
528 else
529 l = m + 1;
530 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
531 m, le32_to_cpu(m->ei_block),
532 r, le32_to_cpu(r->ei_block));
535 path->p_idx = l - 1;
536 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
537 idx_pblock(path->p_idx));
539 #ifdef CHECK_BINSEARCH
541 struct ext4_extent_idx *chix, *ix;
542 int k;
544 chix = ix = EXT_FIRST_INDEX(eh);
545 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
546 if (k != 0 &&
547 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
548 printk(KERN_DEBUG "k=%d, ix=0x%p, "
549 "first=0x%p\n", k,
550 ix, EXT_FIRST_INDEX(eh));
551 printk(KERN_DEBUG "%u <= %u\n",
552 le32_to_cpu(ix->ei_block),
553 le32_to_cpu(ix[-1].ei_block));
555 BUG_ON(k && le32_to_cpu(ix->ei_block)
556 <= le32_to_cpu(ix[-1].ei_block));
557 if (block < le32_to_cpu(ix->ei_block))
558 break;
559 chix = ix;
561 BUG_ON(chix != path->p_idx);
563 #endif
568 * ext4_ext_binsearch:
569 * binary search for closest extent of the given block
570 * the header must be checked before calling this
572 static void
573 ext4_ext_binsearch(struct inode *inode,
574 struct ext4_ext_path *path, ext4_lblk_t block)
576 struct ext4_extent_header *eh = path->p_hdr;
577 struct ext4_extent *r, *l, *m;
579 if (eh->eh_entries == 0) {
581 * this leaf is empty:
582 * we get such a leaf in split/add case
584 return;
587 ext_debug("binsearch for %u: ", block);
589 l = EXT_FIRST_EXTENT(eh) + 1;
590 r = EXT_LAST_EXTENT(eh);
592 while (l <= r) {
593 m = l + (r - l) / 2;
594 if (block < le32_to_cpu(m->ee_block))
595 r = m - 1;
596 else
597 l = m + 1;
598 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
599 m, le32_to_cpu(m->ee_block),
600 r, le32_to_cpu(r->ee_block));
603 path->p_ext = l - 1;
604 ext_debug(" -> %d:%llu:[%d]%d ",
605 le32_to_cpu(path->p_ext->ee_block),
606 ext_pblock(path->p_ext),
607 ext4_ext_is_uninitialized(path->p_ext),
608 ext4_ext_get_actual_len(path->p_ext));
610 #ifdef CHECK_BINSEARCH
612 struct ext4_extent *chex, *ex;
613 int k;
615 chex = ex = EXT_FIRST_EXTENT(eh);
616 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
617 BUG_ON(k && le32_to_cpu(ex->ee_block)
618 <= le32_to_cpu(ex[-1].ee_block));
619 if (block < le32_to_cpu(ex->ee_block))
620 break;
621 chex = ex;
623 BUG_ON(chex != path->p_ext);
625 #endif
629 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
631 struct ext4_extent_header *eh;
633 eh = ext_inode_hdr(inode);
634 eh->eh_depth = 0;
635 eh->eh_entries = 0;
636 eh->eh_magic = EXT4_EXT_MAGIC;
637 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
638 ext4_mark_inode_dirty(handle, inode);
639 ext4_ext_invalidate_cache(inode);
640 return 0;
643 struct ext4_ext_path *
644 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
645 struct ext4_ext_path *path)
647 struct ext4_extent_header *eh;
648 struct buffer_head *bh;
649 short int depth, i, ppos = 0, alloc = 0;
651 eh = ext_inode_hdr(inode);
652 depth = ext_depth(inode);
654 /* account possible depth increase */
655 if (!path) {
656 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
657 GFP_NOFS);
658 if (!path)
659 return ERR_PTR(-ENOMEM);
660 alloc = 1;
662 path[0].p_hdr = eh;
663 path[0].p_bh = NULL;
665 i = depth;
666 /* walk through the tree */
667 while (i) {
668 int need_to_validate = 0;
670 ext_debug("depth %d: num %d, max %d\n",
671 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
673 ext4_ext_binsearch_idx(inode, path + ppos, block);
674 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
675 path[ppos].p_depth = i;
676 path[ppos].p_ext = NULL;
678 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
679 if (unlikely(!bh))
680 goto err;
681 if (!bh_uptodate_or_lock(bh)) {
682 if (bh_submit_read(bh) < 0) {
683 put_bh(bh);
684 goto err;
686 /* validate the extent entries */
687 need_to_validate = 1;
689 eh = ext_block_hdr(bh);
690 ppos++;
691 BUG_ON(ppos > depth);
692 path[ppos].p_bh = bh;
693 path[ppos].p_hdr = eh;
694 i--;
696 if (need_to_validate && ext4_ext_check(inode, eh, i))
697 goto err;
700 path[ppos].p_depth = i;
701 path[ppos].p_ext = NULL;
702 path[ppos].p_idx = NULL;
704 /* find extent */
705 ext4_ext_binsearch(inode, path + ppos, block);
706 /* if not an empty leaf */
707 if (path[ppos].p_ext)
708 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
710 ext4_ext_show_path(inode, path);
712 return path;
714 err:
715 ext4_ext_drop_refs(path);
716 if (alloc)
717 kfree(path);
718 return ERR_PTR(-EIO);
722 * ext4_ext_insert_index:
723 * insert new index [@logical;@ptr] into the block at @curp;
724 * check where to insert: before @curp or after @curp
726 int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
727 struct ext4_ext_path *curp,
728 int logical, ext4_fsblk_t ptr)
730 struct ext4_extent_idx *ix;
731 int len, err;
733 err = ext4_ext_get_access(handle, inode, curp);
734 if (err)
735 return err;
737 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
738 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
739 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
740 /* insert after */
741 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
742 len = (len - 1) * sizeof(struct ext4_extent_idx);
743 len = len < 0 ? 0 : len;
744 ext_debug("insert new index %d after: %llu. "
745 "move %d from 0x%p to 0x%p\n",
746 logical, ptr, len,
747 (curp->p_idx + 1), (curp->p_idx + 2));
748 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
750 ix = curp->p_idx + 1;
751 } else {
752 /* insert before */
753 len = len * sizeof(struct ext4_extent_idx);
754 len = len < 0 ? 0 : len;
755 ext_debug("insert new index %d before: %llu. "
756 "move %d from 0x%p to 0x%p\n",
757 logical, ptr, len,
758 curp->p_idx, (curp->p_idx + 1));
759 memmove(curp->p_idx + 1, curp->p_idx, len);
760 ix = curp->p_idx;
763 ix->ei_block = cpu_to_le32(logical);
764 ext4_idx_store_pblock(ix, ptr);
765 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
767 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
768 > le16_to_cpu(curp->p_hdr->eh_max));
769 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
771 err = ext4_ext_dirty(handle, inode, curp);
772 ext4_std_error(inode->i_sb, err);
774 return err;
778 * ext4_ext_split:
779 * inserts new subtree into the path, using free index entry
780 * at depth @at:
781 * - allocates all needed blocks (new leaf and all intermediate index blocks)
782 * - makes decision where to split
783 * - moves remaining extents and index entries (right to the split point)
784 * into the newly allocated blocks
785 * - initializes subtree
787 static int ext4_ext_split(handle_t *handle, struct inode *inode,
788 struct ext4_ext_path *path,
789 struct ext4_extent *newext, int at)
791 struct buffer_head *bh = NULL;
792 int depth = ext_depth(inode);
793 struct ext4_extent_header *neh;
794 struct ext4_extent_idx *fidx;
795 struct ext4_extent *ex;
796 int i = at, k, m, a;
797 ext4_fsblk_t newblock, oldblock;
798 __le32 border;
799 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
800 int err = 0;
802 /* make decision: where to split? */
803 /* FIXME: now decision is simplest: at current extent */
805 /* if current leaf will be split, then we should use
806 * border from split point */
807 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
808 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
809 border = path[depth].p_ext[1].ee_block;
810 ext_debug("leaf will be split."
811 " next leaf starts at %d\n",
812 le32_to_cpu(border));
813 } else {
814 border = newext->ee_block;
815 ext_debug("leaf will be added."
816 " next leaf starts at %d\n",
817 le32_to_cpu(border));
821 * If error occurs, then we break processing
822 * and mark filesystem read-only. index won't
823 * be inserted and tree will be in consistent
824 * state. Next mount will repair buffers too.
828 * Get array to track all allocated blocks.
829 * We need this to handle errors and free blocks
830 * upon them.
832 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
833 if (!ablocks)
834 return -ENOMEM;
836 /* allocate all needed blocks */
837 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
838 for (a = 0; a < depth - at; a++) {
839 newblock = ext4_ext_new_meta_block(handle, inode, path,
840 newext, &err);
841 if (newblock == 0)
842 goto cleanup;
843 ablocks[a] = newblock;
846 /* initialize new leaf */
847 newblock = ablocks[--a];
848 BUG_ON(newblock == 0);
849 bh = sb_getblk(inode->i_sb, newblock);
850 if (!bh) {
851 err = -EIO;
852 goto cleanup;
854 lock_buffer(bh);
856 err = ext4_journal_get_create_access(handle, bh);
857 if (err)
858 goto cleanup;
860 neh = ext_block_hdr(bh);
861 neh->eh_entries = 0;
862 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
863 neh->eh_magic = EXT4_EXT_MAGIC;
864 neh->eh_depth = 0;
865 ex = EXT_FIRST_EXTENT(neh);
867 /* move remainder of path[depth] to the new leaf */
868 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
869 /* start copy from next extent */
870 /* TODO: we could do it by single memmove */
871 m = 0;
872 path[depth].p_ext++;
873 while (path[depth].p_ext <=
874 EXT_MAX_EXTENT(path[depth].p_hdr)) {
875 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
876 le32_to_cpu(path[depth].p_ext->ee_block),
877 ext_pblock(path[depth].p_ext),
878 ext4_ext_is_uninitialized(path[depth].p_ext),
879 ext4_ext_get_actual_len(path[depth].p_ext),
880 newblock);
881 /*memmove(ex++, path[depth].p_ext++,
882 sizeof(struct ext4_extent));
883 neh->eh_entries++;*/
884 path[depth].p_ext++;
885 m++;
887 if (m) {
888 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
889 le16_add_cpu(&neh->eh_entries, m);
892 set_buffer_uptodate(bh);
893 unlock_buffer(bh);
895 err = ext4_handle_dirty_metadata(handle, inode, bh);
896 if (err)
897 goto cleanup;
898 brelse(bh);
899 bh = NULL;
901 /* correct old leaf */
902 if (m) {
903 err = ext4_ext_get_access(handle, inode, path + depth);
904 if (err)
905 goto cleanup;
906 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
907 err = ext4_ext_dirty(handle, inode, path + depth);
908 if (err)
909 goto cleanup;
913 /* create intermediate indexes */
914 k = depth - at - 1;
915 BUG_ON(k < 0);
916 if (k)
917 ext_debug("create %d intermediate indices\n", k);
918 /* insert new index into current index block */
919 /* current depth stored in i var */
920 i = depth - 1;
921 while (k--) {
922 oldblock = newblock;
923 newblock = ablocks[--a];
924 bh = sb_getblk(inode->i_sb, newblock);
925 if (!bh) {
926 err = -EIO;
927 goto cleanup;
929 lock_buffer(bh);
931 err = ext4_journal_get_create_access(handle, bh);
932 if (err)
933 goto cleanup;
935 neh = ext_block_hdr(bh);
936 neh->eh_entries = cpu_to_le16(1);
937 neh->eh_magic = EXT4_EXT_MAGIC;
938 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
939 neh->eh_depth = cpu_to_le16(depth - i);
940 fidx = EXT_FIRST_INDEX(neh);
941 fidx->ei_block = border;
942 ext4_idx_store_pblock(fidx, oldblock);
944 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
945 i, newblock, le32_to_cpu(border), oldblock);
946 /* copy indexes */
947 m = 0;
948 path[i].p_idx++;
950 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
951 EXT_MAX_INDEX(path[i].p_hdr));
952 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
953 EXT_LAST_INDEX(path[i].p_hdr));
954 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
955 ext_debug("%d: move %d:%llu in new index %llu\n", i,
956 le32_to_cpu(path[i].p_idx->ei_block),
957 idx_pblock(path[i].p_idx),
958 newblock);
959 /*memmove(++fidx, path[i].p_idx++,
960 sizeof(struct ext4_extent_idx));
961 neh->eh_entries++;
962 BUG_ON(neh->eh_entries > neh->eh_max);*/
963 path[i].p_idx++;
964 m++;
966 if (m) {
967 memmove(++fidx, path[i].p_idx - m,
968 sizeof(struct ext4_extent_idx) * m);
969 le16_add_cpu(&neh->eh_entries, m);
971 set_buffer_uptodate(bh);
972 unlock_buffer(bh);
974 err = ext4_handle_dirty_metadata(handle, inode, bh);
975 if (err)
976 goto cleanup;
977 brelse(bh);
978 bh = NULL;
980 /* correct old index */
981 if (m) {
982 err = ext4_ext_get_access(handle, inode, path + i);
983 if (err)
984 goto cleanup;
985 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
986 err = ext4_ext_dirty(handle, inode, path + i);
987 if (err)
988 goto cleanup;
991 i--;
994 /* insert new index */
995 err = ext4_ext_insert_index(handle, inode, path + at,
996 le32_to_cpu(border), newblock);
998 cleanup:
999 if (bh) {
1000 if (buffer_locked(bh))
1001 unlock_buffer(bh);
1002 brelse(bh);
1005 if (err) {
1006 /* free all allocated blocks in error case */
1007 for (i = 0; i < depth; i++) {
1008 if (!ablocks[i])
1009 continue;
1010 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
1013 kfree(ablocks);
1015 return err;
1019 * ext4_ext_grow_indepth:
1020 * implements tree growing procedure:
1021 * - allocates new block
1022 * - moves top-level data (index block or leaf) into the new block
1023 * - initializes new top-level, creating index that points to the
1024 * just created block
1026 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1027 struct ext4_ext_path *path,
1028 struct ext4_extent *newext)
1030 struct ext4_ext_path *curp = path;
1031 struct ext4_extent_header *neh;
1032 struct ext4_extent_idx *fidx;
1033 struct buffer_head *bh;
1034 ext4_fsblk_t newblock;
1035 int err = 0;
1037 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1038 if (newblock == 0)
1039 return err;
1041 bh = sb_getblk(inode->i_sb, newblock);
1042 if (!bh) {
1043 err = -EIO;
1044 ext4_std_error(inode->i_sb, err);
1045 return err;
1047 lock_buffer(bh);
1049 err = ext4_journal_get_create_access(handle, bh);
1050 if (err) {
1051 unlock_buffer(bh);
1052 goto out;
1055 /* move top-level index/leaf into new block */
1056 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1058 /* set size of new block */
1059 neh = ext_block_hdr(bh);
1060 /* old root could have indexes or leaves
1061 * so calculate e_max right way */
1062 if (ext_depth(inode))
1063 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1064 else
1065 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1066 neh->eh_magic = EXT4_EXT_MAGIC;
1067 set_buffer_uptodate(bh);
1068 unlock_buffer(bh);
1070 err = ext4_handle_dirty_metadata(handle, inode, bh);
1071 if (err)
1072 goto out;
1074 /* create index in new top-level index: num,max,pointer */
1075 err = ext4_ext_get_access(handle, inode, curp);
1076 if (err)
1077 goto out;
1079 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1080 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1081 curp->p_hdr->eh_entries = cpu_to_le16(1);
1082 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
1084 if (path[0].p_hdr->eh_depth)
1085 curp->p_idx->ei_block =
1086 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1087 else
1088 curp->p_idx->ei_block =
1089 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1090 ext4_idx_store_pblock(curp->p_idx, newblock);
1092 neh = ext_inode_hdr(inode);
1093 fidx = EXT_FIRST_INDEX(neh);
1094 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1095 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1096 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
1098 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1099 err = ext4_ext_dirty(handle, inode, curp);
1100 out:
1101 brelse(bh);
1103 return err;
1107 * ext4_ext_create_new_leaf:
1108 * finds empty index and adds new leaf.
1109 * if no free index is found, then it requests in-depth growing.
1111 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1112 struct ext4_ext_path *path,
1113 struct ext4_extent *newext)
1115 struct ext4_ext_path *curp;
1116 int depth, i, err = 0;
1118 repeat:
1119 i = depth = ext_depth(inode);
1121 /* walk up to the tree and look for free index entry */
1122 curp = path + depth;
1123 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1124 i--;
1125 curp--;
1128 /* we use already allocated block for index block,
1129 * so subsequent data blocks should be contiguous */
1130 if (EXT_HAS_FREE_INDEX(curp)) {
1131 /* if we found index with free entry, then use that
1132 * entry: create all needed subtree and add new leaf */
1133 err = ext4_ext_split(handle, inode, path, newext, i);
1134 if (err)
1135 goto out;
1137 /* refill path */
1138 ext4_ext_drop_refs(path);
1139 path = ext4_ext_find_extent(inode,
1140 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1141 path);
1142 if (IS_ERR(path))
1143 err = PTR_ERR(path);
1144 } else {
1145 /* tree is full, time to grow in depth */
1146 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1147 if (err)
1148 goto out;
1150 /* refill path */
1151 ext4_ext_drop_refs(path);
1152 path = ext4_ext_find_extent(inode,
1153 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1154 path);
1155 if (IS_ERR(path)) {
1156 err = PTR_ERR(path);
1157 goto out;
1161 * only first (depth 0 -> 1) produces free space;
1162 * in all other cases we have to split the grown tree
1164 depth = ext_depth(inode);
1165 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1166 /* now we need to split */
1167 goto repeat;
1171 out:
1172 return err;
1176 * search the closest allocated block to the left for *logical
1177 * and returns it at @logical + it's physical address at @phys
1178 * if *logical is the smallest allocated block, the function
1179 * returns 0 at @phys
1180 * return value contains 0 (success) or error code
1183 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1184 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1186 struct ext4_extent_idx *ix;
1187 struct ext4_extent *ex;
1188 int depth, ee_len;
1190 BUG_ON(path == NULL);
1191 depth = path->p_depth;
1192 *phys = 0;
1194 if (depth == 0 && path->p_ext == NULL)
1195 return 0;
1197 /* usually extent in the path covers blocks smaller
1198 * then *logical, but it can be that extent is the
1199 * first one in the file */
1201 ex = path[depth].p_ext;
1202 ee_len = ext4_ext_get_actual_len(ex);
1203 if (*logical < le32_to_cpu(ex->ee_block)) {
1204 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1205 while (--depth >= 0) {
1206 ix = path[depth].p_idx;
1207 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1209 return 0;
1212 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1214 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1215 *phys = ext_pblock(ex) + ee_len - 1;
1216 return 0;
1220 * search the closest allocated block to the right for *logical
1221 * and returns it at @logical + it's physical address at @phys
1222 * if *logical is the smallest allocated block, the function
1223 * returns 0 at @phys
1224 * return value contains 0 (success) or error code
1227 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1228 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1230 struct buffer_head *bh = NULL;
1231 struct ext4_extent_header *eh;
1232 struct ext4_extent_idx *ix;
1233 struct ext4_extent *ex;
1234 ext4_fsblk_t block;
1235 int depth; /* Note, NOT eh_depth; depth from top of tree */
1236 int ee_len;
1238 BUG_ON(path == NULL);
1239 depth = path->p_depth;
1240 *phys = 0;
1242 if (depth == 0 && path->p_ext == NULL)
1243 return 0;
1245 /* usually extent in the path covers blocks smaller
1246 * then *logical, but it can be that extent is the
1247 * first one in the file */
1249 ex = path[depth].p_ext;
1250 ee_len = ext4_ext_get_actual_len(ex);
1251 if (*logical < le32_to_cpu(ex->ee_block)) {
1252 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1253 while (--depth >= 0) {
1254 ix = path[depth].p_idx;
1255 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1257 *logical = le32_to_cpu(ex->ee_block);
1258 *phys = ext_pblock(ex);
1259 return 0;
1262 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1264 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1265 /* next allocated block in this leaf */
1266 ex++;
1267 *logical = le32_to_cpu(ex->ee_block);
1268 *phys = ext_pblock(ex);
1269 return 0;
1272 /* go up and search for index to the right */
1273 while (--depth >= 0) {
1274 ix = path[depth].p_idx;
1275 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1276 goto got_index;
1279 /* we've gone up to the root and found no index to the right */
1280 return 0;
1282 got_index:
1283 /* we've found index to the right, let's
1284 * follow it and find the closest allocated
1285 * block to the right */
1286 ix++;
1287 block = idx_pblock(ix);
1288 while (++depth < path->p_depth) {
1289 bh = sb_bread(inode->i_sb, block);
1290 if (bh == NULL)
1291 return -EIO;
1292 eh = ext_block_hdr(bh);
1293 /* subtract from p_depth to get proper eh_depth */
1294 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1295 put_bh(bh);
1296 return -EIO;
1298 ix = EXT_FIRST_INDEX(eh);
1299 block = idx_pblock(ix);
1300 put_bh(bh);
1303 bh = sb_bread(inode->i_sb, block);
1304 if (bh == NULL)
1305 return -EIO;
1306 eh = ext_block_hdr(bh);
1307 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1308 put_bh(bh);
1309 return -EIO;
1311 ex = EXT_FIRST_EXTENT(eh);
1312 *logical = le32_to_cpu(ex->ee_block);
1313 *phys = ext_pblock(ex);
1314 put_bh(bh);
1315 return 0;
1319 * ext4_ext_next_allocated_block:
1320 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1321 * NOTE: it considers block number from index entry as
1322 * allocated block. Thus, index entries have to be consistent
1323 * with leaves.
1325 static ext4_lblk_t
1326 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1328 int depth;
1330 BUG_ON(path == NULL);
1331 depth = path->p_depth;
1333 if (depth == 0 && path->p_ext == NULL)
1334 return EXT_MAX_BLOCK;
1336 while (depth >= 0) {
1337 if (depth == path->p_depth) {
1338 /* leaf */
1339 if (path[depth].p_ext !=
1340 EXT_LAST_EXTENT(path[depth].p_hdr))
1341 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1342 } else {
1343 /* index */
1344 if (path[depth].p_idx !=
1345 EXT_LAST_INDEX(path[depth].p_hdr))
1346 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1348 depth--;
1351 return EXT_MAX_BLOCK;
1355 * ext4_ext_next_leaf_block:
1356 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1358 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1359 struct ext4_ext_path *path)
1361 int depth;
1363 BUG_ON(path == NULL);
1364 depth = path->p_depth;
1366 /* zero-tree has no leaf blocks at all */
1367 if (depth == 0)
1368 return EXT_MAX_BLOCK;
1370 /* go to index block */
1371 depth--;
1373 while (depth >= 0) {
1374 if (path[depth].p_idx !=
1375 EXT_LAST_INDEX(path[depth].p_hdr))
1376 return (ext4_lblk_t)
1377 le32_to_cpu(path[depth].p_idx[1].ei_block);
1378 depth--;
1381 return EXT_MAX_BLOCK;
1385 * ext4_ext_correct_indexes:
1386 * if leaf gets modified and modified extent is first in the leaf,
1387 * then we have to correct all indexes above.
1388 * TODO: do we need to correct tree in all cases?
1390 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1391 struct ext4_ext_path *path)
1393 struct ext4_extent_header *eh;
1394 int depth = ext_depth(inode);
1395 struct ext4_extent *ex;
1396 __le32 border;
1397 int k, err = 0;
1399 eh = path[depth].p_hdr;
1400 ex = path[depth].p_ext;
1401 BUG_ON(ex == NULL);
1402 BUG_ON(eh == NULL);
1404 if (depth == 0) {
1405 /* there is no tree at all */
1406 return 0;
1409 if (ex != EXT_FIRST_EXTENT(eh)) {
1410 /* we correct tree if first leaf got modified only */
1411 return 0;
1415 * TODO: we need correction if border is smaller than current one
1417 k = depth - 1;
1418 border = path[depth].p_ext->ee_block;
1419 err = ext4_ext_get_access(handle, inode, path + k);
1420 if (err)
1421 return err;
1422 path[k].p_idx->ei_block = border;
1423 err = ext4_ext_dirty(handle, inode, path + k);
1424 if (err)
1425 return err;
1427 while (k--) {
1428 /* change all left-side indexes */
1429 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1430 break;
1431 err = ext4_ext_get_access(handle, inode, path + k);
1432 if (err)
1433 break;
1434 path[k].p_idx->ei_block = border;
1435 err = ext4_ext_dirty(handle, inode, path + k);
1436 if (err)
1437 break;
1440 return err;
1444 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1445 struct ext4_extent *ex2)
1447 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1450 * Make sure that either both extents are uninitialized, or
1451 * both are _not_.
1453 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1454 return 0;
1456 if (ext4_ext_is_uninitialized(ex1))
1457 max_len = EXT_UNINIT_MAX_LEN;
1458 else
1459 max_len = EXT_INIT_MAX_LEN;
1461 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1462 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1464 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1465 le32_to_cpu(ex2->ee_block))
1466 return 0;
1469 * To allow future support for preallocated extents to be added
1470 * as an RO_COMPAT feature, refuse to merge to extents if
1471 * this can result in the top bit of ee_len being set.
1473 if (ext1_ee_len + ext2_ee_len > max_len)
1474 return 0;
1475 #ifdef AGGRESSIVE_TEST
1476 if (ext1_ee_len >= 4)
1477 return 0;
1478 #endif
1480 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1481 return 1;
1482 return 0;
1486 * This function tries to merge the "ex" extent to the next extent in the tree.
1487 * It always tries to merge towards right. If you want to merge towards
1488 * left, pass "ex - 1" as argument instead of "ex".
1489 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1490 * 1 if they got merged.
1492 int ext4_ext_try_to_merge(struct inode *inode,
1493 struct ext4_ext_path *path,
1494 struct ext4_extent *ex)
1496 struct ext4_extent_header *eh;
1497 unsigned int depth, len;
1498 int merge_done = 0;
1499 int uninitialized = 0;
1501 depth = ext_depth(inode);
1502 BUG_ON(path[depth].p_hdr == NULL);
1503 eh = path[depth].p_hdr;
1505 while (ex < EXT_LAST_EXTENT(eh)) {
1506 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1507 break;
1508 /* merge with next extent! */
1509 if (ext4_ext_is_uninitialized(ex))
1510 uninitialized = 1;
1511 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1512 + ext4_ext_get_actual_len(ex + 1));
1513 if (uninitialized)
1514 ext4_ext_mark_uninitialized(ex);
1516 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1517 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1518 * sizeof(struct ext4_extent);
1519 memmove(ex + 1, ex + 2, len);
1521 le16_add_cpu(&eh->eh_entries, -1);
1522 merge_done = 1;
1523 WARN_ON(eh->eh_entries == 0);
1524 if (!eh->eh_entries)
1525 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1526 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1529 return merge_done;
1533 * check if a portion of the "newext" extent overlaps with an
1534 * existing extent.
1536 * If there is an overlap discovered, it updates the length of the newext
1537 * such that there will be no overlap, and then returns 1.
1538 * If there is no overlap found, it returns 0.
1540 unsigned int ext4_ext_check_overlap(struct inode *inode,
1541 struct ext4_extent *newext,
1542 struct ext4_ext_path *path)
1544 ext4_lblk_t b1, b2;
1545 unsigned int depth, len1;
1546 unsigned int ret = 0;
1548 b1 = le32_to_cpu(newext->ee_block);
1549 len1 = ext4_ext_get_actual_len(newext);
1550 depth = ext_depth(inode);
1551 if (!path[depth].p_ext)
1552 goto out;
1553 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1556 * get the next allocated block if the extent in the path
1557 * is before the requested block(s)
1559 if (b2 < b1) {
1560 b2 = ext4_ext_next_allocated_block(path);
1561 if (b2 == EXT_MAX_BLOCK)
1562 goto out;
1565 /* check for wrap through zero on extent logical start block*/
1566 if (b1 + len1 < b1) {
1567 len1 = EXT_MAX_BLOCK - b1;
1568 newext->ee_len = cpu_to_le16(len1);
1569 ret = 1;
1572 /* check for overlap */
1573 if (b1 + len1 > b2) {
1574 newext->ee_len = cpu_to_le16(b2 - b1);
1575 ret = 1;
1577 out:
1578 return ret;
1582 * ext4_ext_insert_extent:
1583 * tries to merge requsted extent into the existing extent or
1584 * inserts requested extent as new one into the tree,
1585 * creating new leaf in the no-space case.
1587 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1588 struct ext4_ext_path *path,
1589 struct ext4_extent *newext, int flag)
1591 struct ext4_extent_header *eh;
1592 struct ext4_extent *ex, *fex;
1593 struct ext4_extent *nearex; /* nearest extent */
1594 struct ext4_ext_path *npath = NULL;
1595 int depth, len, err;
1596 ext4_lblk_t next;
1597 unsigned uninitialized = 0;
1599 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1600 depth = ext_depth(inode);
1601 ex = path[depth].p_ext;
1602 BUG_ON(path[depth].p_hdr == NULL);
1604 /* try to insert block into found extent and return */
1605 if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1606 && ext4_can_extents_be_merged(inode, ex, newext)) {
1607 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1608 ext4_ext_is_uninitialized(newext),
1609 ext4_ext_get_actual_len(newext),
1610 le32_to_cpu(ex->ee_block),
1611 ext4_ext_is_uninitialized(ex),
1612 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1613 err = ext4_ext_get_access(handle, inode, path + depth);
1614 if (err)
1615 return err;
1618 * ext4_can_extents_be_merged should have checked that either
1619 * both extents are uninitialized, or both aren't. Thus we
1620 * need to check only one of them here.
1622 if (ext4_ext_is_uninitialized(ex))
1623 uninitialized = 1;
1624 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1625 + ext4_ext_get_actual_len(newext));
1626 if (uninitialized)
1627 ext4_ext_mark_uninitialized(ex);
1628 eh = path[depth].p_hdr;
1629 nearex = ex;
1630 goto merge;
1633 repeat:
1634 depth = ext_depth(inode);
1635 eh = path[depth].p_hdr;
1636 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1637 goto has_space;
1639 /* probably next leaf has space for us? */
1640 fex = EXT_LAST_EXTENT(eh);
1641 next = ext4_ext_next_leaf_block(inode, path);
1642 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1643 && next != EXT_MAX_BLOCK) {
1644 ext_debug("next leaf block - %d\n", next);
1645 BUG_ON(npath != NULL);
1646 npath = ext4_ext_find_extent(inode, next, NULL);
1647 if (IS_ERR(npath))
1648 return PTR_ERR(npath);
1649 BUG_ON(npath->p_depth != path->p_depth);
1650 eh = npath[depth].p_hdr;
1651 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1652 ext_debug("next leaf isnt full(%d)\n",
1653 le16_to_cpu(eh->eh_entries));
1654 path = npath;
1655 goto repeat;
1657 ext_debug("next leaf has no free space(%d,%d)\n",
1658 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1662 * There is no free space in the found leaf.
1663 * We're gonna add a new leaf in the tree.
1665 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1666 if (err)
1667 goto cleanup;
1668 depth = ext_depth(inode);
1669 eh = path[depth].p_hdr;
1671 has_space:
1672 nearex = path[depth].p_ext;
1674 err = ext4_ext_get_access(handle, inode, path + depth);
1675 if (err)
1676 goto cleanup;
1678 if (!nearex) {
1679 /* there is no extent in this leaf, create first one */
1680 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
1681 le32_to_cpu(newext->ee_block),
1682 ext_pblock(newext),
1683 ext4_ext_is_uninitialized(newext),
1684 ext4_ext_get_actual_len(newext));
1685 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1686 } else if (le32_to_cpu(newext->ee_block)
1687 > le32_to_cpu(nearex->ee_block)) {
1688 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1689 if (nearex != EXT_LAST_EXTENT(eh)) {
1690 len = EXT_MAX_EXTENT(eh) - nearex;
1691 len = (len - 1) * sizeof(struct ext4_extent);
1692 len = len < 0 ? 0 : len;
1693 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
1694 "move %d from 0x%p to 0x%p\n",
1695 le32_to_cpu(newext->ee_block),
1696 ext_pblock(newext),
1697 ext4_ext_is_uninitialized(newext),
1698 ext4_ext_get_actual_len(newext),
1699 nearex, len, nearex + 1, nearex + 2);
1700 memmove(nearex + 2, nearex + 1, len);
1702 path[depth].p_ext = nearex + 1;
1703 } else {
1704 BUG_ON(newext->ee_block == nearex->ee_block);
1705 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1706 len = len < 0 ? 0 : len;
1707 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
1708 "move %d from 0x%p to 0x%p\n",
1709 le32_to_cpu(newext->ee_block),
1710 ext_pblock(newext),
1711 ext4_ext_is_uninitialized(newext),
1712 ext4_ext_get_actual_len(newext),
1713 nearex, len, nearex + 1, nearex + 2);
1714 memmove(nearex + 1, nearex, len);
1715 path[depth].p_ext = nearex;
1718 le16_add_cpu(&eh->eh_entries, 1);
1719 nearex = path[depth].p_ext;
1720 nearex->ee_block = newext->ee_block;
1721 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1722 nearex->ee_len = newext->ee_len;
1724 merge:
1725 /* try to merge extents to the right */
1726 if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1727 ext4_ext_try_to_merge(inode, path, nearex);
1729 /* try to merge extents to the left */
1731 /* time to correct all indexes above */
1732 err = ext4_ext_correct_indexes(handle, inode, path);
1733 if (err)
1734 goto cleanup;
1736 err = ext4_ext_dirty(handle, inode, path + depth);
1738 cleanup:
1739 if (npath) {
1740 ext4_ext_drop_refs(npath);
1741 kfree(npath);
1743 ext4_ext_invalidate_cache(inode);
1744 return err;
1747 int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1748 ext4_lblk_t num, ext_prepare_callback func,
1749 void *cbdata)
1751 struct ext4_ext_path *path = NULL;
1752 struct ext4_ext_cache cbex;
1753 struct ext4_extent *ex;
1754 ext4_lblk_t next, start = 0, end = 0;
1755 ext4_lblk_t last = block + num;
1756 int depth, exists, err = 0;
1758 BUG_ON(func == NULL);
1759 BUG_ON(inode == NULL);
1761 while (block < last && block != EXT_MAX_BLOCK) {
1762 num = last - block;
1763 /* find extent for this block */
1764 down_read(&EXT4_I(inode)->i_data_sem);
1765 path = ext4_ext_find_extent(inode, block, path);
1766 up_read(&EXT4_I(inode)->i_data_sem);
1767 if (IS_ERR(path)) {
1768 err = PTR_ERR(path);
1769 path = NULL;
1770 break;
1773 depth = ext_depth(inode);
1774 BUG_ON(path[depth].p_hdr == NULL);
1775 ex = path[depth].p_ext;
1776 next = ext4_ext_next_allocated_block(path);
1778 exists = 0;
1779 if (!ex) {
1780 /* there is no extent yet, so try to allocate
1781 * all requested space */
1782 start = block;
1783 end = block + num;
1784 } else if (le32_to_cpu(ex->ee_block) > block) {
1785 /* need to allocate space before found extent */
1786 start = block;
1787 end = le32_to_cpu(ex->ee_block);
1788 if (block + num < end)
1789 end = block + num;
1790 } else if (block >= le32_to_cpu(ex->ee_block)
1791 + ext4_ext_get_actual_len(ex)) {
1792 /* need to allocate space after found extent */
1793 start = block;
1794 end = block + num;
1795 if (end >= next)
1796 end = next;
1797 } else if (block >= le32_to_cpu(ex->ee_block)) {
1799 * some part of requested space is covered
1800 * by found extent
1802 start = block;
1803 end = le32_to_cpu(ex->ee_block)
1804 + ext4_ext_get_actual_len(ex);
1805 if (block + num < end)
1806 end = block + num;
1807 exists = 1;
1808 } else {
1809 BUG();
1811 BUG_ON(end <= start);
1813 if (!exists) {
1814 cbex.ec_block = start;
1815 cbex.ec_len = end - start;
1816 cbex.ec_start = 0;
1817 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1818 } else {
1819 cbex.ec_block = le32_to_cpu(ex->ee_block);
1820 cbex.ec_len = ext4_ext_get_actual_len(ex);
1821 cbex.ec_start = ext_pblock(ex);
1822 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1825 BUG_ON(cbex.ec_len == 0);
1826 err = func(inode, path, &cbex, ex, cbdata);
1827 ext4_ext_drop_refs(path);
1829 if (err < 0)
1830 break;
1832 if (err == EXT_REPEAT)
1833 continue;
1834 else if (err == EXT_BREAK) {
1835 err = 0;
1836 break;
1839 if (ext_depth(inode) != depth) {
1840 /* depth was changed. we have to realloc path */
1841 kfree(path);
1842 path = NULL;
1845 block = cbex.ec_block + cbex.ec_len;
1848 if (path) {
1849 ext4_ext_drop_refs(path);
1850 kfree(path);
1853 return err;
1856 static void
1857 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1858 __u32 len, ext4_fsblk_t start, int type)
1860 struct ext4_ext_cache *cex;
1861 BUG_ON(len == 0);
1862 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1863 cex = &EXT4_I(inode)->i_cached_extent;
1864 cex->ec_type = type;
1865 cex->ec_block = block;
1866 cex->ec_len = len;
1867 cex->ec_start = start;
1868 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1872 * ext4_ext_put_gap_in_cache:
1873 * calculate boundaries of the gap that the requested block fits into
1874 * and cache this gap
1876 static void
1877 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1878 ext4_lblk_t block)
1880 int depth = ext_depth(inode);
1881 unsigned long len;
1882 ext4_lblk_t lblock;
1883 struct ext4_extent *ex;
1885 ex = path[depth].p_ext;
1886 if (ex == NULL) {
1887 /* there is no extent yet, so gap is [0;-] */
1888 lblock = 0;
1889 len = EXT_MAX_BLOCK;
1890 ext_debug("cache gap(whole file):");
1891 } else if (block < le32_to_cpu(ex->ee_block)) {
1892 lblock = block;
1893 len = le32_to_cpu(ex->ee_block) - block;
1894 ext_debug("cache gap(before): %u [%u:%u]",
1895 block,
1896 le32_to_cpu(ex->ee_block),
1897 ext4_ext_get_actual_len(ex));
1898 } else if (block >= le32_to_cpu(ex->ee_block)
1899 + ext4_ext_get_actual_len(ex)) {
1900 ext4_lblk_t next;
1901 lblock = le32_to_cpu(ex->ee_block)
1902 + ext4_ext_get_actual_len(ex);
1904 next = ext4_ext_next_allocated_block(path);
1905 ext_debug("cache gap(after): [%u:%u] %u",
1906 le32_to_cpu(ex->ee_block),
1907 ext4_ext_get_actual_len(ex),
1908 block);
1909 BUG_ON(next == lblock);
1910 len = next - lblock;
1911 } else {
1912 lblock = len = 0;
1913 BUG();
1916 ext_debug(" -> %u:%lu\n", lblock, len);
1917 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1920 static int
1921 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1922 struct ext4_extent *ex)
1924 struct ext4_ext_cache *cex;
1925 int ret = EXT4_EXT_CACHE_NO;
1928 * We borrow i_block_reservation_lock to protect i_cached_extent
1930 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
1931 cex = &EXT4_I(inode)->i_cached_extent;
1933 /* has cache valid data? */
1934 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1935 goto errout;
1937 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1938 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1939 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1940 ex->ee_block = cpu_to_le32(cex->ec_block);
1941 ext4_ext_store_pblock(ex, cex->ec_start);
1942 ex->ee_len = cpu_to_le16(cex->ec_len);
1943 ext_debug("%u cached by %u:%u:%llu\n",
1944 block,
1945 cex->ec_block, cex->ec_len, cex->ec_start);
1946 ret = cex->ec_type;
1948 errout:
1949 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1950 return ret;
1954 * ext4_ext_rm_idx:
1955 * removes index from the index block.
1956 * It's used in truncate case only, thus all requests are for
1957 * last index in the block only.
1959 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1960 struct ext4_ext_path *path)
1962 struct buffer_head *bh;
1963 int err;
1964 ext4_fsblk_t leaf;
1966 /* free index block */
1967 path--;
1968 leaf = idx_pblock(path->p_idx);
1969 BUG_ON(path->p_hdr->eh_entries == 0);
1970 err = ext4_ext_get_access(handle, inode, path);
1971 if (err)
1972 return err;
1973 le16_add_cpu(&path->p_hdr->eh_entries, -1);
1974 err = ext4_ext_dirty(handle, inode, path);
1975 if (err)
1976 return err;
1977 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1978 bh = sb_find_get_block(inode->i_sb, leaf);
1979 ext4_forget(handle, 1, inode, bh, leaf);
1980 ext4_free_blocks(handle, inode, leaf, 1, 1);
1981 return err;
1985 * ext4_ext_calc_credits_for_single_extent:
1986 * This routine returns max. credits that needed to insert an extent
1987 * to the extent tree.
1988 * When pass the actual path, the caller should calculate credits
1989 * under i_data_sem.
1991 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
1992 struct ext4_ext_path *path)
1994 if (path) {
1995 int depth = ext_depth(inode);
1996 int ret = 0;
1998 /* probably there is space in leaf? */
1999 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2000 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2003 * There are some space in the leaf tree, no
2004 * need to account for leaf block credit
2006 * bitmaps and block group descriptor blocks
2007 * and other metadat blocks still need to be
2008 * accounted.
2010 /* 1 bitmap, 1 block group descriptor */
2011 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2012 return ret;
2016 return ext4_chunk_trans_blocks(inode, nrblocks);
2020 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2022 * if nrblocks are fit in a single extent (chunk flag is 1), then
2023 * in the worse case, each tree level index/leaf need to be changed
2024 * if the tree split due to insert a new extent, then the old tree
2025 * index/leaf need to be updated too
2027 * If the nrblocks are discontiguous, they could cause
2028 * the whole tree split more than once, but this is really rare.
2030 int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
2032 int index;
2033 int depth = ext_depth(inode);
2035 if (chunk)
2036 index = depth * 2;
2037 else
2038 index = depth * 3;
2040 return index;
2043 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2044 struct ext4_extent *ex,
2045 ext4_lblk_t from, ext4_lblk_t to)
2047 struct buffer_head *bh;
2048 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2049 int i, metadata = 0;
2051 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2052 metadata = 1;
2053 #ifdef EXTENTS_STATS
2055 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2056 spin_lock(&sbi->s_ext_stats_lock);
2057 sbi->s_ext_blocks += ee_len;
2058 sbi->s_ext_extents++;
2059 if (ee_len < sbi->s_ext_min)
2060 sbi->s_ext_min = ee_len;
2061 if (ee_len > sbi->s_ext_max)
2062 sbi->s_ext_max = ee_len;
2063 if (ext_depth(inode) > sbi->s_depth_max)
2064 sbi->s_depth_max = ext_depth(inode);
2065 spin_unlock(&sbi->s_ext_stats_lock);
2067 #endif
2068 if (from >= le32_to_cpu(ex->ee_block)
2069 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2070 /* tail removal */
2071 ext4_lblk_t num;
2072 ext4_fsblk_t start;
2074 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2075 start = ext_pblock(ex) + ee_len - num;
2076 ext_debug("free last %u blocks starting %llu\n", num, start);
2077 for (i = 0; i < num; i++) {
2078 bh = sb_find_get_block(inode->i_sb, start + i);
2079 ext4_forget(handle, metadata, inode, bh, start + i);
2081 ext4_free_blocks(handle, inode, start, num, metadata);
2082 } else if (from == le32_to_cpu(ex->ee_block)
2083 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
2084 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
2085 from, to, le32_to_cpu(ex->ee_block), ee_len);
2086 } else {
2087 printk(KERN_INFO "strange request: removal(2) "
2088 "%u-%u from %u:%u\n",
2089 from, to, le32_to_cpu(ex->ee_block), ee_len);
2091 return 0;
2094 static int
2095 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2096 struct ext4_ext_path *path, ext4_lblk_t start)
2098 int err = 0, correct_index = 0;
2099 int depth = ext_depth(inode), credits;
2100 struct ext4_extent_header *eh;
2101 ext4_lblk_t a, b, block;
2102 unsigned num;
2103 ext4_lblk_t ex_ee_block;
2104 unsigned short ex_ee_len;
2105 unsigned uninitialized = 0;
2106 struct ext4_extent *ex;
2108 /* the header must be checked already in ext4_ext_remove_space() */
2109 ext_debug("truncate since %u in leaf\n", start);
2110 if (!path[depth].p_hdr)
2111 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2112 eh = path[depth].p_hdr;
2113 BUG_ON(eh == NULL);
2115 /* find where to start removing */
2116 ex = EXT_LAST_EXTENT(eh);
2118 ex_ee_block = le32_to_cpu(ex->ee_block);
2119 ex_ee_len = ext4_ext_get_actual_len(ex);
2121 while (ex >= EXT_FIRST_EXTENT(eh) &&
2122 ex_ee_block + ex_ee_len > start) {
2124 if (ext4_ext_is_uninitialized(ex))
2125 uninitialized = 1;
2126 else
2127 uninitialized = 0;
2129 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2130 uninitialized, ex_ee_len);
2131 path[depth].p_ext = ex;
2133 a = ex_ee_block > start ? ex_ee_block : start;
2134 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2135 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2137 ext_debug(" border %u:%u\n", a, b);
2139 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2140 block = 0;
2141 num = 0;
2142 BUG();
2143 } else if (a != ex_ee_block) {
2144 /* remove tail of the extent */
2145 block = ex_ee_block;
2146 num = a - block;
2147 } else if (b != ex_ee_block + ex_ee_len - 1) {
2148 /* remove head of the extent */
2149 block = a;
2150 num = b - a;
2151 /* there is no "make a hole" API yet */
2152 BUG();
2153 } else {
2154 /* remove whole extent: excellent! */
2155 block = ex_ee_block;
2156 num = 0;
2157 BUG_ON(a != ex_ee_block);
2158 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2162 * 3 for leaf, sb, and inode plus 2 (bmap and group
2163 * descriptor) for each block group; assume two block
2164 * groups plus ex_ee_len/blocks_per_block_group for
2165 * the worst case
2167 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2168 if (ex == EXT_FIRST_EXTENT(eh)) {
2169 correct_index = 1;
2170 credits += (ext_depth(inode)) + 1;
2172 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2174 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2175 if (err)
2176 goto out;
2178 err = ext4_ext_get_access(handle, inode, path + depth);
2179 if (err)
2180 goto out;
2182 err = ext4_remove_blocks(handle, inode, ex, a, b);
2183 if (err)
2184 goto out;
2186 if (num == 0) {
2187 /* this extent is removed; mark slot entirely unused */
2188 ext4_ext_store_pblock(ex, 0);
2189 le16_add_cpu(&eh->eh_entries, -1);
2192 ex->ee_block = cpu_to_le32(block);
2193 ex->ee_len = cpu_to_le16(num);
2195 * Do not mark uninitialized if all the blocks in the
2196 * extent have been removed.
2198 if (uninitialized && num)
2199 ext4_ext_mark_uninitialized(ex);
2201 err = ext4_ext_dirty(handle, inode, path + depth);
2202 if (err)
2203 goto out;
2205 ext_debug("new extent: %u:%u:%llu\n", block, num,
2206 ext_pblock(ex));
2207 ex--;
2208 ex_ee_block = le32_to_cpu(ex->ee_block);
2209 ex_ee_len = ext4_ext_get_actual_len(ex);
2212 if (correct_index && eh->eh_entries)
2213 err = ext4_ext_correct_indexes(handle, inode, path);
2215 /* if this leaf is free, then we should
2216 * remove it from index block above */
2217 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2218 err = ext4_ext_rm_idx(handle, inode, path + depth);
2220 out:
2221 return err;
2225 * ext4_ext_more_to_rm:
2226 * returns 1 if current index has to be freed (even partial)
2228 static int
2229 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2231 BUG_ON(path->p_idx == NULL);
2233 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2234 return 0;
2237 * if truncate on deeper level happened, it wasn't partial,
2238 * so we have to consider current index for truncation
2240 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2241 return 0;
2242 return 1;
2245 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
2247 struct super_block *sb = inode->i_sb;
2248 int depth = ext_depth(inode);
2249 struct ext4_ext_path *path;
2250 handle_t *handle;
2251 int i = 0, err = 0;
2253 ext_debug("truncate since %u\n", start);
2255 /* probably first extent we're gonna free will be last in block */
2256 handle = ext4_journal_start(inode, depth + 1);
2257 if (IS_ERR(handle))
2258 return PTR_ERR(handle);
2260 ext4_ext_invalidate_cache(inode);
2263 * We start scanning from right side, freeing all the blocks
2264 * after i_size and walking into the tree depth-wise.
2266 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
2267 if (path == NULL) {
2268 ext4_journal_stop(handle);
2269 return -ENOMEM;
2271 path[0].p_hdr = ext_inode_hdr(inode);
2272 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2273 err = -EIO;
2274 goto out;
2276 path[0].p_depth = depth;
2278 while (i >= 0 && err == 0) {
2279 if (i == depth) {
2280 /* this is leaf block */
2281 err = ext4_ext_rm_leaf(handle, inode, path, start);
2282 /* root level has p_bh == NULL, brelse() eats this */
2283 brelse(path[i].p_bh);
2284 path[i].p_bh = NULL;
2285 i--;
2286 continue;
2289 /* this is index block */
2290 if (!path[i].p_hdr) {
2291 ext_debug("initialize header\n");
2292 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2295 if (!path[i].p_idx) {
2296 /* this level hasn't been touched yet */
2297 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2298 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2299 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2300 path[i].p_hdr,
2301 le16_to_cpu(path[i].p_hdr->eh_entries));
2302 } else {
2303 /* we were already here, see at next index */
2304 path[i].p_idx--;
2307 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2308 i, EXT_FIRST_INDEX(path[i].p_hdr),
2309 path[i].p_idx);
2310 if (ext4_ext_more_to_rm(path + i)) {
2311 struct buffer_head *bh;
2312 /* go to the next level */
2313 ext_debug("move to level %d (block %llu)\n",
2314 i + 1, idx_pblock(path[i].p_idx));
2315 memset(path + i + 1, 0, sizeof(*path));
2316 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2317 if (!bh) {
2318 /* should we reset i_size? */
2319 err = -EIO;
2320 break;
2322 if (WARN_ON(i + 1 > depth)) {
2323 err = -EIO;
2324 break;
2326 if (ext4_ext_check(inode, ext_block_hdr(bh),
2327 depth - i - 1)) {
2328 err = -EIO;
2329 break;
2331 path[i + 1].p_bh = bh;
2333 /* save actual number of indexes since this
2334 * number is changed at the next iteration */
2335 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2336 i++;
2337 } else {
2338 /* we finished processing this index, go up */
2339 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2340 /* index is empty, remove it;
2341 * handle must be already prepared by the
2342 * truncatei_leaf() */
2343 err = ext4_ext_rm_idx(handle, inode, path + i);
2345 /* root level has p_bh == NULL, brelse() eats this */
2346 brelse(path[i].p_bh);
2347 path[i].p_bh = NULL;
2348 i--;
2349 ext_debug("return to level %d\n", i);
2353 /* TODO: flexible tree reduction should be here */
2354 if (path->p_hdr->eh_entries == 0) {
2356 * truncate to zero freed all the tree,
2357 * so we need to correct eh_depth
2359 err = ext4_ext_get_access(handle, inode, path);
2360 if (err == 0) {
2361 ext_inode_hdr(inode)->eh_depth = 0;
2362 ext_inode_hdr(inode)->eh_max =
2363 cpu_to_le16(ext4_ext_space_root(inode, 0));
2364 err = ext4_ext_dirty(handle, inode, path);
2367 out:
2368 ext4_ext_drop_refs(path);
2369 kfree(path);
2370 ext4_journal_stop(handle);
2372 return err;
2376 * called at mount time
2378 void ext4_ext_init(struct super_block *sb)
2381 * possible initialization would be here
2384 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2385 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
2386 printk(KERN_INFO "EXT4-fs: file extents enabled");
2387 #ifdef AGGRESSIVE_TEST
2388 printk(", aggressive tests");
2389 #endif
2390 #ifdef CHECK_BINSEARCH
2391 printk(", check binsearch");
2392 #endif
2393 #ifdef EXTENTS_STATS
2394 printk(", stats");
2395 #endif
2396 printk("\n");
2397 #endif
2398 #ifdef EXTENTS_STATS
2399 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2400 EXT4_SB(sb)->s_ext_min = 1 << 30;
2401 EXT4_SB(sb)->s_ext_max = 0;
2402 #endif
2407 * called at umount time
2409 void ext4_ext_release(struct super_block *sb)
2411 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2412 return;
2414 #ifdef EXTENTS_STATS
2415 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2416 struct ext4_sb_info *sbi = EXT4_SB(sb);
2417 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2418 sbi->s_ext_blocks, sbi->s_ext_extents,
2419 sbi->s_ext_blocks / sbi->s_ext_extents);
2420 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2421 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2423 #endif
2426 static void bi_complete(struct bio *bio, int error)
2428 complete((struct completion *)bio->bi_private);
2431 /* FIXME!! we need to try to merge to left or right after zero-out */
2432 static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2434 int ret = -EIO;
2435 struct bio *bio;
2436 int blkbits, blocksize;
2437 sector_t ee_pblock;
2438 struct completion event;
2439 unsigned int ee_len, len, done, offset;
2442 blkbits = inode->i_blkbits;
2443 blocksize = inode->i_sb->s_blocksize;
2444 ee_len = ext4_ext_get_actual_len(ex);
2445 ee_pblock = ext_pblock(ex);
2447 /* convert ee_pblock to 512 byte sectors */
2448 ee_pblock = ee_pblock << (blkbits - 9);
2450 while (ee_len > 0) {
2452 if (ee_len > BIO_MAX_PAGES)
2453 len = BIO_MAX_PAGES;
2454 else
2455 len = ee_len;
2457 bio = bio_alloc(GFP_NOIO, len);
2458 bio->bi_sector = ee_pblock;
2459 bio->bi_bdev = inode->i_sb->s_bdev;
2461 done = 0;
2462 offset = 0;
2463 while (done < len) {
2464 ret = bio_add_page(bio, ZERO_PAGE(0),
2465 blocksize, offset);
2466 if (ret != blocksize) {
2468 * We can't add any more pages because of
2469 * hardware limitations. Start a new bio.
2471 break;
2473 done++;
2474 offset += blocksize;
2475 if (offset >= PAGE_CACHE_SIZE)
2476 offset = 0;
2479 init_completion(&event);
2480 bio->bi_private = &event;
2481 bio->bi_end_io = bi_complete;
2482 submit_bio(WRITE, bio);
2483 wait_for_completion(&event);
2485 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2486 ret = 0;
2487 else {
2488 ret = -EIO;
2489 break;
2491 bio_put(bio);
2492 ee_len -= done;
2493 ee_pblock += done << (blkbits - 9);
2495 return ret;
2498 #define EXT4_EXT_ZERO_LEN 7
2500 * This function is called by ext4_ext_get_blocks() if someone tries to write
2501 * to an uninitialized extent. It may result in splitting the uninitialized
2502 * extent into multiple extents (upto three - one initialized and two
2503 * uninitialized).
2504 * There are three possibilities:
2505 * a> There is no split required: Entire extent should be initialized
2506 * b> Splits in two extents: Write is happening at either end of the extent
2507 * c> Splits in three extents: Somone is writing in middle of the extent
2509 static int ext4_ext_convert_to_initialized(handle_t *handle,
2510 struct inode *inode,
2511 struct ext4_ext_path *path,
2512 ext4_lblk_t iblock,
2513 unsigned int max_blocks)
2515 struct ext4_extent *ex, newex, orig_ex;
2516 struct ext4_extent *ex1 = NULL;
2517 struct ext4_extent *ex2 = NULL;
2518 struct ext4_extent *ex3 = NULL;
2519 struct ext4_extent_header *eh;
2520 ext4_lblk_t ee_block;
2521 unsigned int allocated, ee_len, depth;
2522 ext4_fsblk_t newblock;
2523 int err = 0;
2524 int ret = 0;
2526 depth = ext_depth(inode);
2527 eh = path[depth].p_hdr;
2528 ex = path[depth].p_ext;
2529 ee_block = le32_to_cpu(ex->ee_block);
2530 ee_len = ext4_ext_get_actual_len(ex);
2531 allocated = ee_len - (iblock - ee_block);
2532 newblock = iblock - ee_block + ext_pblock(ex);
2533 ex2 = ex;
2534 orig_ex.ee_block = ex->ee_block;
2535 orig_ex.ee_len = cpu_to_le16(ee_len);
2536 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2538 err = ext4_ext_get_access(handle, inode, path + depth);
2539 if (err)
2540 goto out;
2541 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2542 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2543 err = ext4_ext_zeroout(inode, &orig_ex);
2544 if (err)
2545 goto fix_extent_len;
2546 /* update the extent length and mark as initialized */
2547 ex->ee_block = orig_ex.ee_block;
2548 ex->ee_len = orig_ex.ee_len;
2549 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2550 ext4_ext_dirty(handle, inode, path + depth);
2551 /* zeroed the full extent */
2552 return allocated;
2555 /* ex1: ee_block to iblock - 1 : uninitialized */
2556 if (iblock > ee_block) {
2557 ex1 = ex;
2558 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2559 ext4_ext_mark_uninitialized(ex1);
2560 ex2 = &newex;
2563 * for sanity, update the length of the ex2 extent before
2564 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2565 * overlap of blocks.
2567 if (!ex1 && allocated > max_blocks)
2568 ex2->ee_len = cpu_to_le16(max_blocks);
2569 /* ex3: to ee_block + ee_len : uninitialised */
2570 if (allocated > max_blocks) {
2571 unsigned int newdepth;
2572 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2573 if (allocated <= EXT4_EXT_ZERO_LEN) {
2575 * iblock == ee_block is handled by the zerouout
2576 * at the beginning.
2577 * Mark first half uninitialized.
2578 * Mark second half initialized and zero out the
2579 * initialized extent
2581 ex->ee_block = orig_ex.ee_block;
2582 ex->ee_len = cpu_to_le16(ee_len - allocated);
2583 ext4_ext_mark_uninitialized(ex);
2584 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2585 ext4_ext_dirty(handle, inode, path + depth);
2587 ex3 = &newex;
2588 ex3->ee_block = cpu_to_le32(iblock);
2589 ext4_ext_store_pblock(ex3, newblock);
2590 ex3->ee_len = cpu_to_le16(allocated);
2591 err = ext4_ext_insert_extent(handle, inode, path,
2592 ex3, 0);
2593 if (err == -ENOSPC) {
2594 err = ext4_ext_zeroout(inode, &orig_ex);
2595 if (err)
2596 goto fix_extent_len;
2597 ex->ee_block = orig_ex.ee_block;
2598 ex->ee_len = orig_ex.ee_len;
2599 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2600 ext4_ext_dirty(handle, inode, path + depth);
2601 /* blocks available from iblock */
2602 return allocated;
2604 } else if (err)
2605 goto fix_extent_len;
2608 * We need to zero out the second half because
2609 * an fallocate request can update file size and
2610 * converting the second half to initialized extent
2611 * implies that we can leak some junk data to user
2612 * space.
2614 err = ext4_ext_zeroout(inode, ex3);
2615 if (err) {
2617 * We should actually mark the
2618 * second half as uninit and return error
2619 * Insert would have changed the extent
2621 depth = ext_depth(inode);
2622 ext4_ext_drop_refs(path);
2623 path = ext4_ext_find_extent(inode,
2624 iblock, path);
2625 if (IS_ERR(path)) {
2626 err = PTR_ERR(path);
2627 return err;
2629 /* get the second half extent details */
2630 ex = path[depth].p_ext;
2631 err = ext4_ext_get_access(handle, inode,
2632 path + depth);
2633 if (err)
2634 return err;
2635 ext4_ext_mark_uninitialized(ex);
2636 ext4_ext_dirty(handle, inode, path + depth);
2637 return err;
2640 /* zeroed the second half */
2641 return allocated;
2643 ex3 = &newex;
2644 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2645 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2646 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2647 ext4_ext_mark_uninitialized(ex3);
2648 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
2649 if (err == -ENOSPC) {
2650 err = ext4_ext_zeroout(inode, &orig_ex);
2651 if (err)
2652 goto fix_extent_len;
2653 /* update the extent length and mark as initialized */
2654 ex->ee_block = orig_ex.ee_block;
2655 ex->ee_len = orig_ex.ee_len;
2656 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2657 ext4_ext_dirty(handle, inode, path + depth);
2658 /* zeroed the full extent */
2659 /* blocks available from iblock */
2660 return allocated;
2662 } else if (err)
2663 goto fix_extent_len;
2665 * The depth, and hence eh & ex might change
2666 * as part of the insert above.
2668 newdepth = ext_depth(inode);
2670 * update the extent length after successful insert of the
2671 * split extent
2673 orig_ex.ee_len = cpu_to_le16(ee_len -
2674 ext4_ext_get_actual_len(ex3));
2675 depth = newdepth;
2676 ext4_ext_drop_refs(path);
2677 path = ext4_ext_find_extent(inode, iblock, path);
2678 if (IS_ERR(path)) {
2679 err = PTR_ERR(path);
2680 goto out;
2682 eh = path[depth].p_hdr;
2683 ex = path[depth].p_ext;
2684 if (ex2 != &newex)
2685 ex2 = ex;
2687 err = ext4_ext_get_access(handle, inode, path + depth);
2688 if (err)
2689 goto out;
2691 allocated = max_blocks;
2693 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2694 * to insert a extent in the middle zerout directly
2695 * otherwise give the extent a chance to merge to left
2697 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2698 iblock != ee_block) {
2699 err = ext4_ext_zeroout(inode, &orig_ex);
2700 if (err)
2701 goto fix_extent_len;
2702 /* update the extent length and mark as initialized */
2703 ex->ee_block = orig_ex.ee_block;
2704 ex->ee_len = orig_ex.ee_len;
2705 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2706 ext4_ext_dirty(handle, inode, path + depth);
2707 /* zero out the first half */
2708 /* blocks available from iblock */
2709 return allocated;
2713 * If there was a change of depth as part of the
2714 * insertion of ex3 above, we need to update the length
2715 * of the ex1 extent again here
2717 if (ex1 && ex1 != ex) {
2718 ex1 = ex;
2719 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2720 ext4_ext_mark_uninitialized(ex1);
2721 ex2 = &newex;
2723 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2724 ex2->ee_block = cpu_to_le32(iblock);
2725 ext4_ext_store_pblock(ex2, newblock);
2726 ex2->ee_len = cpu_to_le16(allocated);
2727 if (ex2 != ex)
2728 goto insert;
2730 * New (initialized) extent starts from the first block
2731 * in the current extent. i.e., ex2 == ex
2732 * We have to see if it can be merged with the extent
2733 * on the left.
2735 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2737 * To merge left, pass "ex2 - 1" to try_to_merge(),
2738 * since it merges towards right _only_.
2740 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2741 if (ret) {
2742 err = ext4_ext_correct_indexes(handle, inode, path);
2743 if (err)
2744 goto out;
2745 depth = ext_depth(inode);
2746 ex2--;
2750 * Try to Merge towards right. This might be required
2751 * only when the whole extent is being written to.
2752 * i.e. ex2 == ex and ex3 == NULL.
2754 if (!ex3) {
2755 ret = ext4_ext_try_to_merge(inode, path, ex2);
2756 if (ret) {
2757 err = ext4_ext_correct_indexes(handle, inode, path);
2758 if (err)
2759 goto out;
2762 /* Mark modified extent as dirty */
2763 err = ext4_ext_dirty(handle, inode, path + depth);
2764 goto out;
2765 insert:
2766 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
2767 if (err == -ENOSPC) {
2768 err = ext4_ext_zeroout(inode, &orig_ex);
2769 if (err)
2770 goto fix_extent_len;
2771 /* update the extent length and mark as initialized */
2772 ex->ee_block = orig_ex.ee_block;
2773 ex->ee_len = orig_ex.ee_len;
2774 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2775 ext4_ext_dirty(handle, inode, path + depth);
2776 /* zero out the first half */
2777 return allocated;
2778 } else if (err)
2779 goto fix_extent_len;
2780 out:
2781 ext4_ext_show_leaf(inode, path);
2782 return err ? err : allocated;
2784 fix_extent_len:
2785 ex->ee_block = orig_ex.ee_block;
2786 ex->ee_len = orig_ex.ee_len;
2787 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2788 ext4_ext_mark_uninitialized(ex);
2789 ext4_ext_dirty(handle, inode, path + depth);
2790 return err;
2794 * This function is called by ext4_ext_get_blocks() from
2795 * ext4_get_blocks_dio_write() when DIO to write
2796 * to an uninitialized extent.
2798 * Writing to an uninitized extent may result in splitting the uninitialized
2799 * extent into multiple /intialized unintialized extents (up to three)
2800 * There are three possibilities:
2801 * a> There is no split required: Entire extent should be uninitialized
2802 * b> Splits in two extents: Write is happening at either end of the extent
2803 * c> Splits in three extents: Somone is writing in middle of the extent
2805 * One of more index blocks maybe needed if the extent tree grow after
2806 * the unintialized extent split. To prevent ENOSPC occur at the IO
2807 * complete, we need to split the uninitialized extent before DIO submit
2808 * the IO. The uninitilized extent called at this time will be split
2809 * into three uninitialized extent(at most). After IO complete, the part
2810 * being filled will be convert to initialized by the end_io callback function
2811 * via ext4_convert_unwritten_extents().
2813 * Returns the size of uninitialized extent to be written on success.
2815 static int ext4_split_unwritten_extents(handle_t *handle,
2816 struct inode *inode,
2817 struct ext4_ext_path *path,
2818 ext4_lblk_t iblock,
2819 unsigned int max_blocks,
2820 int flags)
2822 struct ext4_extent *ex, newex, orig_ex;
2823 struct ext4_extent *ex1 = NULL;
2824 struct ext4_extent *ex2 = NULL;
2825 struct ext4_extent *ex3 = NULL;
2826 struct ext4_extent_header *eh;
2827 ext4_lblk_t ee_block;
2828 unsigned int allocated, ee_len, depth;
2829 ext4_fsblk_t newblock;
2830 int err = 0;
2832 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2833 "iblock %llu, max_blocks %u\n", inode->i_ino,
2834 (unsigned long long)iblock, max_blocks);
2835 depth = ext_depth(inode);
2836 eh = path[depth].p_hdr;
2837 ex = path[depth].p_ext;
2838 ee_block = le32_to_cpu(ex->ee_block);
2839 ee_len = ext4_ext_get_actual_len(ex);
2840 allocated = ee_len - (iblock - ee_block);
2841 newblock = iblock - ee_block + ext_pblock(ex);
2842 ex2 = ex;
2843 orig_ex.ee_block = ex->ee_block;
2844 orig_ex.ee_len = cpu_to_le16(ee_len);
2845 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2848 * If the uninitialized extent begins at the same logical
2849 * block where the write begins, and the write completely
2850 * covers the extent, then we don't need to split it.
2852 if ((iblock == ee_block) && (allocated <= max_blocks))
2853 return allocated;
2855 err = ext4_ext_get_access(handle, inode, path + depth);
2856 if (err)
2857 goto out;
2858 /* ex1: ee_block to iblock - 1 : uninitialized */
2859 if (iblock > ee_block) {
2860 ex1 = ex;
2861 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2862 ext4_ext_mark_uninitialized(ex1);
2863 ex2 = &newex;
2866 * for sanity, update the length of the ex2 extent before
2867 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2868 * overlap of blocks.
2870 if (!ex1 && allocated > max_blocks)
2871 ex2->ee_len = cpu_to_le16(max_blocks);
2872 /* ex3: to ee_block + ee_len : uninitialised */
2873 if (allocated > max_blocks) {
2874 unsigned int newdepth;
2875 ex3 = &newex;
2876 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2877 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2878 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2879 ext4_ext_mark_uninitialized(ex3);
2880 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2881 if (err == -ENOSPC) {
2882 err = ext4_ext_zeroout(inode, &orig_ex);
2883 if (err)
2884 goto fix_extent_len;
2885 /* update the extent length and mark as initialized */
2886 ex->ee_block = orig_ex.ee_block;
2887 ex->ee_len = orig_ex.ee_len;
2888 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2889 ext4_ext_dirty(handle, inode, path + depth);
2890 /* zeroed the full extent */
2891 /* blocks available from iblock */
2892 return allocated;
2894 } else if (err)
2895 goto fix_extent_len;
2897 * The depth, and hence eh & ex might change
2898 * as part of the insert above.
2900 newdepth = ext_depth(inode);
2902 * update the extent length after successful insert of the
2903 * split extent
2905 orig_ex.ee_len = cpu_to_le16(ee_len -
2906 ext4_ext_get_actual_len(ex3));
2907 depth = newdepth;
2908 ext4_ext_drop_refs(path);
2909 path = ext4_ext_find_extent(inode, iblock, path);
2910 if (IS_ERR(path)) {
2911 err = PTR_ERR(path);
2912 goto out;
2914 eh = path[depth].p_hdr;
2915 ex = path[depth].p_ext;
2916 if (ex2 != &newex)
2917 ex2 = ex;
2919 err = ext4_ext_get_access(handle, inode, path + depth);
2920 if (err)
2921 goto out;
2923 allocated = max_blocks;
2926 * If there was a change of depth as part of the
2927 * insertion of ex3 above, we need to update the length
2928 * of the ex1 extent again here
2930 if (ex1 && ex1 != ex) {
2931 ex1 = ex;
2932 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2933 ext4_ext_mark_uninitialized(ex1);
2934 ex2 = &newex;
2937 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2938 * uninitialised still.
2940 ex2->ee_block = cpu_to_le32(iblock);
2941 ext4_ext_store_pblock(ex2, newblock);
2942 ex2->ee_len = cpu_to_le16(allocated);
2943 ext4_ext_mark_uninitialized(ex2);
2944 if (ex2 != ex)
2945 goto insert;
2946 /* Mark modified extent as dirty */
2947 err = ext4_ext_dirty(handle, inode, path + depth);
2948 ext_debug("out here\n");
2949 goto out;
2950 insert:
2951 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2952 if (err == -ENOSPC) {
2953 err = ext4_ext_zeroout(inode, &orig_ex);
2954 if (err)
2955 goto fix_extent_len;
2956 /* update the extent length and mark as initialized */
2957 ex->ee_block = orig_ex.ee_block;
2958 ex->ee_len = orig_ex.ee_len;
2959 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2960 ext4_ext_dirty(handle, inode, path + depth);
2961 /* zero out the first half */
2962 return allocated;
2963 } else if (err)
2964 goto fix_extent_len;
2965 out:
2966 ext4_ext_show_leaf(inode, path);
2967 return err ? err : allocated;
2969 fix_extent_len:
2970 ex->ee_block = orig_ex.ee_block;
2971 ex->ee_len = orig_ex.ee_len;
2972 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2973 ext4_ext_mark_uninitialized(ex);
2974 ext4_ext_dirty(handle, inode, path + depth);
2975 return err;
2977 static int ext4_convert_unwritten_extents_dio(handle_t *handle,
2978 struct inode *inode,
2979 struct ext4_ext_path *path)
2981 struct ext4_extent *ex;
2982 struct ext4_extent_header *eh;
2983 int depth;
2984 int err = 0;
2985 int ret = 0;
2987 depth = ext_depth(inode);
2988 eh = path[depth].p_hdr;
2989 ex = path[depth].p_ext;
2991 err = ext4_ext_get_access(handle, inode, path + depth);
2992 if (err)
2993 goto out;
2994 /* first mark the extent as initialized */
2995 ext4_ext_mark_initialized(ex);
2998 * We have to see if it can be merged with the extent
2999 * on the left.
3001 if (ex > EXT_FIRST_EXTENT(eh)) {
3003 * To merge left, pass "ex - 1" to try_to_merge(),
3004 * since it merges towards right _only_.
3006 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3007 if (ret) {
3008 err = ext4_ext_correct_indexes(handle, inode, path);
3009 if (err)
3010 goto out;
3011 depth = ext_depth(inode);
3012 ex--;
3016 * Try to Merge towards right.
3018 ret = ext4_ext_try_to_merge(inode, path, ex);
3019 if (ret) {
3020 err = ext4_ext_correct_indexes(handle, inode, path);
3021 if (err)
3022 goto out;
3023 depth = ext_depth(inode);
3025 /* Mark modified extent as dirty */
3026 err = ext4_ext_dirty(handle, inode, path + depth);
3027 out:
3028 ext4_ext_show_leaf(inode, path);
3029 return err;
3032 static int
3033 ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3034 ext4_lblk_t iblock, unsigned int max_blocks,
3035 struct ext4_ext_path *path, int flags,
3036 unsigned int allocated, struct buffer_head *bh_result,
3037 ext4_fsblk_t newblock)
3039 int ret = 0;
3040 int err = 0;
3041 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3043 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3044 "block %llu, max_blocks %u, flags %d, allocated %u",
3045 inode->i_ino, (unsigned long long)iblock, max_blocks,
3046 flags, allocated);
3047 ext4_ext_show_leaf(inode, path);
3049 /* DIO get_block() before submit the IO, split the extent */
3050 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3051 ret = ext4_split_unwritten_extents(handle,
3052 inode, path, iblock,
3053 max_blocks, flags);
3055 * Flag the inode(non aio case) or end_io struct (aio case)
3056 * that this IO needs to convertion to written when IO is
3057 * completed
3059 if (io)
3060 io->flag = DIO_AIO_UNWRITTEN;
3061 else
3062 EXT4_I(inode)->i_state |= EXT4_STATE_DIO_UNWRITTEN;
3063 goto out;
3065 /* async DIO end_io complete, convert the filled extent to written */
3066 if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) {
3067 ret = ext4_convert_unwritten_extents_dio(handle, inode,
3068 path);
3069 if (ret >= 0)
3070 ext4_update_inode_fsync_trans(handle, inode, 1);
3071 goto out2;
3073 /* buffered IO case */
3075 * repeat fallocate creation request
3076 * we already have an unwritten extent
3078 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3079 goto map_out;
3081 /* buffered READ or buffered write_begin() lookup */
3082 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3084 * We have blocks reserved already. We
3085 * return allocated blocks so that delalloc
3086 * won't do block reservation for us. But
3087 * the buffer head will be unmapped so that
3088 * a read from the block returns 0s.
3090 set_buffer_unwritten(bh_result);
3091 goto out1;
3094 /* buffered write, writepage time, convert*/
3095 ret = ext4_ext_convert_to_initialized(handle, inode,
3096 path, iblock,
3097 max_blocks);
3098 if (ret >= 0)
3099 ext4_update_inode_fsync_trans(handle, inode, 1);
3100 out:
3101 if (ret <= 0) {
3102 err = ret;
3103 goto out2;
3104 } else
3105 allocated = ret;
3106 set_buffer_new(bh_result);
3107 map_out:
3108 set_buffer_mapped(bh_result);
3109 out1:
3110 if (allocated > max_blocks)
3111 allocated = max_blocks;
3112 ext4_ext_show_leaf(inode, path);
3113 bh_result->b_bdev = inode->i_sb->s_bdev;
3114 bh_result->b_blocknr = newblock;
3115 out2:
3116 if (path) {
3117 ext4_ext_drop_refs(path);
3118 kfree(path);
3120 return err ? err : allocated;
3123 * Block allocation/map/preallocation routine for extents based files
3126 * Need to be called with
3127 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3128 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
3130 * return > 0, number of of blocks already mapped/allocated
3131 * if create == 0 and these are pre-allocated blocks
3132 * buffer head is unmapped
3133 * otherwise blocks are mapped
3135 * return = 0, if plain look up failed (blocks have not been allocated)
3136 * buffer head is unmapped
3138 * return < 0, error case.
3140 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
3141 ext4_lblk_t iblock,
3142 unsigned int max_blocks, struct buffer_head *bh_result,
3143 int flags)
3145 struct ext4_ext_path *path = NULL;
3146 struct ext4_extent_header *eh;
3147 struct ext4_extent newex, *ex;
3148 ext4_fsblk_t newblock;
3149 int err = 0, depth, ret, cache_type;
3150 unsigned int allocated = 0;
3151 struct ext4_allocation_request ar;
3152 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
3154 __clear_bit(BH_New, &bh_result->b_state);
3155 ext_debug("blocks %u/%u requested for inode %lu\n",
3156 iblock, max_blocks, inode->i_ino);
3158 /* check in cache */
3159 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
3160 if (cache_type) {
3161 if (cache_type == EXT4_EXT_CACHE_GAP) {
3162 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3164 * block isn't allocated yet and
3165 * user doesn't want to allocate it
3167 goto out2;
3169 /* we should allocate requested block */
3170 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
3171 /* block is already allocated */
3172 newblock = iblock
3173 - le32_to_cpu(newex.ee_block)
3174 + ext_pblock(&newex);
3175 /* number of remaining blocks in the extent */
3176 allocated = ext4_ext_get_actual_len(&newex) -
3177 (iblock - le32_to_cpu(newex.ee_block));
3178 goto out;
3179 } else {
3180 BUG();
3184 /* find extent for this block */
3185 path = ext4_ext_find_extent(inode, iblock, NULL);
3186 if (IS_ERR(path)) {
3187 err = PTR_ERR(path);
3188 path = NULL;
3189 goto out2;
3192 depth = ext_depth(inode);
3195 * consistent leaf must not be empty;
3196 * this situation is possible, though, _during_ tree modification;
3197 * this is why assert can't be put in ext4_ext_find_extent()
3199 BUG_ON(path[depth].p_ext == NULL && depth != 0);
3200 eh = path[depth].p_hdr;
3202 ex = path[depth].p_ext;
3203 if (ex) {
3204 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3205 ext4_fsblk_t ee_start = ext_pblock(ex);
3206 unsigned short ee_len;
3209 * Uninitialized extents are treated as holes, except that
3210 * we split out initialized portions during a write.
3212 ee_len = ext4_ext_get_actual_len(ex);
3213 /* if found extent covers block, simply return it */
3214 if (iblock >= ee_block && iblock < ee_block + ee_len) {
3215 newblock = iblock - ee_block + ee_start;
3216 /* number of remaining blocks in the extent */
3217 allocated = ee_len - (iblock - ee_block);
3218 ext_debug("%u fit into %u:%d -> %llu\n", iblock,
3219 ee_block, ee_len, newblock);
3221 /* Do not put uninitialized extent in the cache */
3222 if (!ext4_ext_is_uninitialized(ex)) {
3223 ext4_ext_put_in_cache(inode, ee_block,
3224 ee_len, ee_start,
3225 EXT4_EXT_CACHE_EXTENT);
3226 goto out;
3228 ret = ext4_ext_handle_uninitialized_extents(handle,
3229 inode, iblock, max_blocks, path,
3230 flags, allocated, bh_result, newblock);
3231 return ret;
3236 * requested block isn't allocated yet;
3237 * we couldn't try to create block if create flag is zero
3239 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3241 * put just found gap into cache to speed up
3242 * subsequent requests
3244 ext4_ext_put_gap_in_cache(inode, path, iblock);
3245 goto out2;
3248 * Okay, we need to do block allocation.
3251 /* find neighbour allocated blocks */
3252 ar.lleft = iblock;
3253 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3254 if (err)
3255 goto out2;
3256 ar.lright = iblock;
3257 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3258 if (err)
3259 goto out2;
3262 * See if request is beyond maximum number of blocks we can have in
3263 * a single extent. For an initialized extent this limit is
3264 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3265 * EXT_UNINIT_MAX_LEN.
3267 if (max_blocks > EXT_INIT_MAX_LEN &&
3268 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3269 max_blocks = EXT_INIT_MAX_LEN;
3270 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
3271 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
3272 max_blocks = EXT_UNINIT_MAX_LEN;
3274 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3275 newex.ee_block = cpu_to_le32(iblock);
3276 newex.ee_len = cpu_to_le16(max_blocks);
3277 err = ext4_ext_check_overlap(inode, &newex, path);
3278 if (err)
3279 allocated = ext4_ext_get_actual_len(&newex);
3280 else
3281 allocated = max_blocks;
3283 /* allocate new block */
3284 ar.inode = inode;
3285 ar.goal = ext4_ext_find_goal(inode, path, iblock);
3286 ar.logical = iblock;
3287 ar.len = allocated;
3288 if (S_ISREG(inode->i_mode))
3289 ar.flags = EXT4_MB_HINT_DATA;
3290 else
3291 /* disable in-core preallocation for non-regular files */
3292 ar.flags = 0;
3293 newblock = ext4_mb_new_blocks(handle, &ar, &err);
3294 if (!newblock)
3295 goto out2;
3296 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
3297 ar.goal, newblock, allocated);
3299 /* try to insert new extent into found leaf and return */
3300 ext4_ext_store_pblock(&newex, newblock);
3301 newex.ee_len = cpu_to_le16(ar.len);
3302 /* Mark uninitialized */
3303 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
3304 ext4_ext_mark_uninitialized(&newex);
3306 * io_end structure was created for every async
3307 * direct IO write to the middle of the file.
3308 * To avoid unecessary convertion for every aio dio rewrite
3309 * to the mid of file, here we flag the IO that is really
3310 * need the convertion.
3311 * For non asycn direct IO case, flag the inode state
3312 * that we need to perform convertion when IO is done.
3314 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3315 if (io)
3316 io->flag = DIO_AIO_UNWRITTEN;
3317 else
3318 EXT4_I(inode)->i_state |=
3319 EXT4_STATE_DIO_UNWRITTEN;;
3322 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3323 if (err) {
3324 /* free data blocks we just allocated */
3325 /* not a good idea to call discard here directly,
3326 * but otherwise we'd need to call it every free() */
3327 ext4_discard_preallocations(inode);
3328 ext4_free_blocks(handle, inode, ext_pblock(&newex),
3329 ext4_ext_get_actual_len(&newex), 0);
3330 goto out2;
3333 /* previous routine could use block we allocated */
3334 newblock = ext_pblock(&newex);
3335 allocated = ext4_ext_get_actual_len(&newex);
3336 set_buffer_new(bh_result);
3339 * Cache the extent and update transaction to commit on fdatasync only
3340 * when it is _not_ an uninitialized extent.
3342 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
3343 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
3344 EXT4_EXT_CACHE_EXTENT);
3345 ext4_update_inode_fsync_trans(handle, inode, 1);
3346 } else
3347 ext4_update_inode_fsync_trans(handle, inode, 0);
3348 out:
3349 if (allocated > max_blocks)
3350 allocated = max_blocks;
3351 ext4_ext_show_leaf(inode, path);
3352 set_buffer_mapped(bh_result);
3353 bh_result->b_bdev = inode->i_sb->s_bdev;
3354 bh_result->b_blocknr = newblock;
3355 out2:
3356 if (path) {
3357 ext4_ext_drop_refs(path);
3358 kfree(path);
3360 return err ? err : allocated;
3363 void ext4_ext_truncate(struct inode *inode)
3365 struct address_space *mapping = inode->i_mapping;
3366 struct super_block *sb = inode->i_sb;
3367 ext4_lblk_t last_block;
3368 handle_t *handle;
3369 int err = 0;
3372 * probably first extent we're gonna free will be last in block
3374 err = ext4_writepage_trans_blocks(inode);
3375 handle = ext4_journal_start(inode, err);
3376 if (IS_ERR(handle))
3377 return;
3379 if (inode->i_size & (sb->s_blocksize - 1))
3380 ext4_block_truncate_page(handle, mapping, inode->i_size);
3382 if (ext4_orphan_add(handle, inode))
3383 goto out_stop;
3385 down_write(&EXT4_I(inode)->i_data_sem);
3386 ext4_ext_invalidate_cache(inode);
3388 ext4_discard_preallocations(inode);
3391 * TODO: optimization is possible here.
3392 * Probably we need not scan at all,
3393 * because page truncation is enough.
3396 /* we have to know where to truncate from in crash case */
3397 EXT4_I(inode)->i_disksize = inode->i_size;
3398 ext4_mark_inode_dirty(handle, inode);
3400 last_block = (inode->i_size + sb->s_blocksize - 1)
3401 >> EXT4_BLOCK_SIZE_BITS(sb);
3402 err = ext4_ext_remove_space(inode, last_block);
3404 /* In a multi-transaction truncate, we only make the final
3405 * transaction synchronous.
3407 if (IS_SYNC(inode))
3408 ext4_handle_sync(handle);
3410 out_stop:
3411 up_write(&EXT4_I(inode)->i_data_sem);
3413 * If this was a simple ftruncate() and the file will remain alive,
3414 * then we need to clear up the orphan record which we created above.
3415 * However, if this was a real unlink then we were called by
3416 * ext4_delete_inode(), and we allow that function to clean up the
3417 * orphan info for us.
3419 if (inode->i_nlink)
3420 ext4_orphan_del(handle, inode);
3422 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3423 ext4_mark_inode_dirty(handle, inode);
3424 ext4_journal_stop(handle);
3427 static void ext4_falloc_update_inode(struct inode *inode,
3428 int mode, loff_t new_size, int update_ctime)
3430 struct timespec now;
3432 if (update_ctime) {
3433 now = current_fs_time(inode->i_sb);
3434 if (!timespec_equal(&inode->i_ctime, &now))
3435 inode->i_ctime = now;
3438 * Update only when preallocation was requested beyond
3439 * the file size.
3441 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3442 if (new_size > i_size_read(inode))
3443 i_size_write(inode, new_size);
3444 if (new_size > EXT4_I(inode)->i_disksize)
3445 ext4_update_i_disksize(inode, new_size);
3451 * preallocate space for a file. This implements ext4's fallocate inode
3452 * operation, which gets called from sys_fallocate system call.
3453 * For block-mapped files, posix_fallocate should fall back to the method
3454 * of writing zeroes to the required new blocks (the same behavior which is
3455 * expected for file systems which do not support fallocate() system call).
3457 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3459 handle_t *handle;
3460 ext4_lblk_t block;
3461 loff_t new_size;
3462 unsigned int max_blocks;
3463 int ret = 0;
3464 int ret2 = 0;
3465 int retries = 0;
3466 struct buffer_head map_bh;
3467 unsigned int credits, blkbits = inode->i_blkbits;
3470 * currently supporting (pre)allocate mode for extent-based
3471 * files _only_
3473 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3474 return -EOPNOTSUPP;
3476 /* preallocation to directories is currently not supported */
3477 if (S_ISDIR(inode->i_mode))
3478 return -ENODEV;
3480 block = offset >> blkbits;
3482 * We can't just convert len to max_blocks because
3483 * If blocksize = 4096 offset = 3072 and len = 2048
3485 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3486 - block;
3488 * credits to insert 1 extent into extent tree
3490 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3491 mutex_lock(&inode->i_mutex);
3492 retry:
3493 while (ret >= 0 && ret < max_blocks) {
3494 block = block + ret;
3495 max_blocks = max_blocks - ret;
3496 handle = ext4_journal_start(inode, credits);
3497 if (IS_ERR(handle)) {
3498 ret = PTR_ERR(handle);
3499 break;
3501 map_bh.b_state = 0;
3502 ret = ext4_get_blocks(handle, inode, block,
3503 max_blocks, &map_bh,
3504 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
3505 if (ret <= 0) {
3506 #ifdef EXT4FS_DEBUG
3507 WARN_ON(ret <= 0);
3508 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3509 "returned error inode#%lu, block=%u, "
3510 "max_blocks=%u", __func__,
3511 inode->i_ino, block, max_blocks);
3512 #endif
3513 ext4_mark_inode_dirty(handle, inode);
3514 ret2 = ext4_journal_stop(handle);
3515 break;
3517 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3518 blkbits) >> blkbits))
3519 new_size = offset + len;
3520 else
3521 new_size = (block + ret) << blkbits;
3523 ext4_falloc_update_inode(inode, mode, new_size,
3524 buffer_new(&map_bh));
3525 ext4_mark_inode_dirty(handle, inode);
3526 ret2 = ext4_journal_stop(handle);
3527 if (ret2)
3528 break;
3530 if (ret == -ENOSPC &&
3531 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3532 ret = 0;
3533 goto retry;
3535 mutex_unlock(&inode->i_mutex);
3536 return ret > 0 ? ret2 : ret;
3540 * This function convert a range of blocks to written extents
3541 * The caller of this function will pass the start offset and the size.
3542 * all unwritten extents within this range will be converted to
3543 * written extents.
3545 * This function is called from the direct IO end io call back
3546 * function, to convert the fallocated extents after IO is completed.
3547 * Returns 0 on success.
3549 int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
3550 loff_t len)
3552 handle_t *handle;
3553 ext4_lblk_t block;
3554 unsigned int max_blocks;
3555 int ret = 0;
3556 int ret2 = 0;
3557 struct buffer_head map_bh;
3558 unsigned int credits, blkbits = inode->i_blkbits;
3560 block = offset >> blkbits;
3562 * We can't just convert len to max_blocks because
3563 * If blocksize = 4096 offset = 3072 and len = 2048
3565 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3566 - block;
3568 * credits to insert 1 extent into extent tree
3570 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3571 while (ret >= 0 && ret < max_blocks) {
3572 block = block + ret;
3573 max_blocks = max_blocks - ret;
3574 handle = ext4_journal_start(inode, credits);
3575 if (IS_ERR(handle)) {
3576 ret = PTR_ERR(handle);
3577 break;
3579 map_bh.b_state = 0;
3580 ret = ext4_get_blocks(handle, inode, block,
3581 max_blocks, &map_bh,
3582 EXT4_GET_BLOCKS_DIO_CONVERT_EXT);
3583 if (ret <= 0) {
3584 WARN_ON(ret <= 0);
3585 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3586 "returned error inode#%lu, block=%u, "
3587 "max_blocks=%u", __func__,
3588 inode->i_ino, block, max_blocks);
3590 ext4_mark_inode_dirty(handle, inode);
3591 ret2 = ext4_journal_stop(handle);
3592 if (ret <= 0 || ret2 )
3593 break;
3595 return ret > 0 ? ret2 : ret;
3598 * Callback function called for each extent to gather FIEMAP information.
3600 static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
3601 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3602 void *data)
3604 struct fiemap_extent_info *fieinfo = data;
3605 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
3606 __u64 logical;
3607 __u64 physical;
3608 __u64 length;
3609 __u32 flags = 0;
3610 int error;
3612 logical = (__u64)newex->ec_block << blksize_bits;
3614 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3615 pgoff_t offset;
3616 struct page *page;
3617 struct buffer_head *bh = NULL;
3619 offset = logical >> PAGE_SHIFT;
3620 page = find_get_page(inode->i_mapping, offset);
3621 if (!page || !page_has_buffers(page))
3622 return EXT_CONTINUE;
3624 bh = page_buffers(page);
3626 if (!bh)
3627 return EXT_CONTINUE;
3629 if (buffer_delay(bh)) {
3630 flags |= FIEMAP_EXTENT_DELALLOC;
3631 page_cache_release(page);
3632 } else {
3633 page_cache_release(page);
3634 return EXT_CONTINUE;
3638 physical = (__u64)newex->ec_start << blksize_bits;
3639 length = (__u64)newex->ec_len << blksize_bits;
3641 if (ex && ext4_ext_is_uninitialized(ex))
3642 flags |= FIEMAP_EXTENT_UNWRITTEN;
3645 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3647 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3648 * this also indicates no more allocated blocks.
3650 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3652 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
3653 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3654 loff_t size = i_size_read(inode);
3655 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3657 flags |= FIEMAP_EXTENT_LAST;
3658 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3659 logical+length > size)
3660 length = (size - logical + bs - 1) & ~(bs-1);
3663 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3664 length, flags);
3665 if (error < 0)
3666 return error;
3667 if (error == 1)
3668 return EXT_BREAK;
3670 return EXT_CONTINUE;
3673 /* fiemap flags we can handle specified here */
3674 #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3676 static int ext4_xattr_fiemap(struct inode *inode,
3677 struct fiemap_extent_info *fieinfo)
3679 __u64 physical = 0;
3680 __u64 length;
3681 __u32 flags = FIEMAP_EXTENT_LAST;
3682 int blockbits = inode->i_sb->s_blocksize_bits;
3683 int error = 0;
3685 /* in-inode? */
3686 if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
3687 struct ext4_iloc iloc;
3688 int offset; /* offset of xattr in inode */
3690 error = ext4_get_inode_loc(inode, &iloc);
3691 if (error)
3692 return error;
3693 physical = iloc.bh->b_blocknr << blockbits;
3694 offset = EXT4_GOOD_OLD_INODE_SIZE +
3695 EXT4_I(inode)->i_extra_isize;
3696 physical += offset;
3697 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3698 flags |= FIEMAP_EXTENT_DATA_INLINE;
3699 } else { /* external block */
3700 physical = EXT4_I(inode)->i_file_acl << blockbits;
3701 length = inode->i_sb->s_blocksize;
3704 if (physical)
3705 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3706 length, flags);
3707 return (error < 0 ? error : 0);
3710 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3711 __u64 start, __u64 len)
3713 ext4_lblk_t start_blk;
3714 ext4_lblk_t len_blks;
3715 int error = 0;
3717 /* fallback to generic here if not in extents fmt */
3718 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3719 return generic_block_fiemap(inode, fieinfo, start, len,
3720 ext4_get_block);
3722 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3723 return -EBADR;
3725 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3726 error = ext4_xattr_fiemap(inode, fieinfo);
3727 } else {
3728 start_blk = start >> inode->i_sb->s_blocksize_bits;
3729 len_blks = len >> inode->i_sb->s_blocksize_bits;
3732 * Walk the extent tree gathering extent information.
3733 * ext4_ext_fiemap_cb will push extents back to user.
3735 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3736 ext4_ext_fiemap_cb, fieinfo);
3739 return error;