Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / ext4 / extents.c
blob2182da8bdb4b9fa60805583cfce107c672e7402b
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/ext4_jbd2.h>
36 #include <linux/jbd2.h>
37 #include <linux/highuid.h>
38 #include <linux/pagemap.h>
39 #include <linux/quotaops.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/falloc.h>
43 #include <linux/ext4_fs_extents.h>
44 #include <asm/uaccess.h>
48 * ext_pblock:
49 * combine low and high parts of physical block number into ext4_fsblk_t
51 static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
53 ext4_fsblk_t block;
55 block = le32_to_cpu(ex->ee_start_lo);
56 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
57 return block;
61 * idx_pblock:
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
66 ext4_fsblk_t block;
68 block = le32_to_cpu(ix->ei_leaf_lo);
69 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
70 return block;
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
78 void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
80 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
89 static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
91 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
95 static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
97 int err;
99 if (handle->h_buffer_credits > needed)
100 return handle;
101 if (!ext4_journal_extend(handle, needed))
102 return handle;
103 err = ext4_journal_restart(handle, needed);
105 return handle;
109 * could return:
110 * - EROFS
111 * - ENOMEM
113 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114 struct ext4_ext_path *path)
116 if (path->p_bh) {
117 /* path points to block */
118 return ext4_journal_get_write_access(handle, path->p_bh);
120 /* path points to leaf/index in inode body */
121 /* we use in-core data, no need to protect them */
122 return 0;
126 * could return:
127 * - EROFS
128 * - ENOMEM
129 * - EIO
131 static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132 struct ext4_ext_path *path)
134 int err;
135 if (path->p_bh) {
136 /* path points to block */
137 err = ext4_journal_dirty_metadata(handle, path->p_bh);
138 } else {
139 /* path points to leaf/index in inode body */
140 err = ext4_mark_inode_dirty(handle, inode);
142 return err;
145 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
146 struct ext4_ext_path *path,
147 ext4_lblk_t block)
149 struct ext4_inode_info *ei = EXT4_I(inode);
150 ext4_fsblk_t bg_start;
151 <<<<<<< HEAD:fs/ext4/extents.c
152 =======
153 ext4_fsblk_t last_block;
154 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
155 ext4_grpblk_t colour;
156 int depth;
158 if (path) {
159 struct ext4_extent *ex;
160 depth = path->p_depth;
162 /* try to predict block placement */
163 ex = path[depth].p_ext;
164 if (ex)
165 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
167 /* it looks like index is empty;
168 * try to find starting block from index itself */
169 if (path[depth].p_bh)
170 return path[depth].p_bh->b_blocknr;
173 /* OK. use inode's group */
174 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
175 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
176 <<<<<<< HEAD:fs/ext4/extents.c
177 colour = (current->pid % 16) *
178 =======
179 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
181 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
182 colour = (current->pid % 16) *
183 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
184 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
185 <<<<<<< HEAD:fs/ext4/extents.c
186 =======
187 else
188 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
189 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
190 return bg_start + colour + block;
193 static ext4_fsblk_t
194 ext4_ext_new_block(handle_t *handle, struct inode *inode,
195 struct ext4_ext_path *path,
196 struct ext4_extent *ex, int *err)
198 ext4_fsblk_t goal, newblock;
200 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
201 newblock = ext4_new_block(handle, inode, goal, err);
202 return newblock;
205 static int ext4_ext_space_block(struct inode *inode)
207 int size;
209 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
210 / sizeof(struct ext4_extent);
211 #ifdef AGGRESSIVE_TEST
212 if (size > 6)
213 size = 6;
214 #endif
215 return size;
218 static int ext4_ext_space_block_idx(struct inode *inode)
220 int size;
222 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
223 / sizeof(struct ext4_extent_idx);
224 #ifdef AGGRESSIVE_TEST
225 if (size > 5)
226 size = 5;
227 #endif
228 return size;
231 static int ext4_ext_space_root(struct inode *inode)
233 int size;
235 size = sizeof(EXT4_I(inode)->i_data);
236 size -= sizeof(struct ext4_extent_header);
237 size /= sizeof(struct ext4_extent);
238 #ifdef AGGRESSIVE_TEST
239 if (size > 3)
240 size = 3;
241 #endif
242 return size;
245 static int ext4_ext_space_root_idx(struct inode *inode)
247 int size;
249 size = sizeof(EXT4_I(inode)->i_data);
250 size -= sizeof(struct ext4_extent_header);
251 size /= sizeof(struct ext4_extent_idx);
252 #ifdef AGGRESSIVE_TEST
253 if (size > 4)
254 size = 4;
255 #endif
256 return size;
259 static int
260 ext4_ext_max_entries(struct inode *inode, int depth)
262 int max;
264 if (depth == ext_depth(inode)) {
265 if (depth == 0)
266 max = ext4_ext_space_root(inode);
267 else
268 max = ext4_ext_space_root_idx(inode);
269 } else {
270 if (depth == 0)
271 max = ext4_ext_space_block(inode);
272 else
273 max = ext4_ext_space_block_idx(inode);
276 return max;
279 static int __ext4_ext_check_header(const char *function, struct inode *inode,
280 struct ext4_extent_header *eh,
281 int depth)
283 const char *error_msg;
284 int max = 0;
286 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
287 error_msg = "invalid magic";
288 goto corrupted;
290 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
291 error_msg = "unexpected eh_depth";
292 goto corrupted;
294 if (unlikely(eh->eh_max == 0)) {
295 error_msg = "invalid eh_max";
296 goto corrupted;
298 max = ext4_ext_max_entries(inode, depth);
299 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
300 error_msg = "too large eh_max";
301 goto corrupted;
303 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
304 error_msg = "invalid eh_entries";
305 goto corrupted;
307 return 0;
309 corrupted:
310 ext4_error(inode->i_sb, function,
311 "bad header in inode #%lu: %s - magic %x, "
312 "entries %u, max %u(%u), depth %u(%u)",
313 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
314 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
315 max, le16_to_cpu(eh->eh_depth), depth);
317 return -EIO;
320 #define ext4_ext_check_header(inode, eh, depth) \
321 __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
323 #ifdef EXT_DEBUG
324 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
326 int k, l = path->p_depth;
328 ext_debug("path:");
329 for (k = 0; k <= l; k++, path++) {
330 if (path->p_idx) {
331 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
332 idx_pblock(path->p_idx));
333 } else if (path->p_ext) {
334 ext_debug(" %d:%d:%llu ",
335 le32_to_cpu(path->p_ext->ee_block),
336 ext4_ext_get_actual_len(path->p_ext),
337 ext_pblock(path->p_ext));
338 } else
339 ext_debug(" []");
341 ext_debug("\n");
344 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
346 int depth = ext_depth(inode);
347 struct ext4_extent_header *eh;
348 struct ext4_extent *ex;
349 int i;
351 if (!path)
352 return;
354 eh = path[depth].p_hdr;
355 ex = EXT_FIRST_EXTENT(eh);
357 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
358 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
359 ext4_ext_get_actual_len(ex), ext_pblock(ex));
361 ext_debug("\n");
363 #else
364 #define ext4_ext_show_path(inode,path)
365 #define ext4_ext_show_leaf(inode,path)
366 #endif
368 <<<<<<< HEAD:fs/ext4/extents.c
369 static void ext4_ext_drop_refs(struct ext4_ext_path *path)
370 =======
371 void ext4_ext_drop_refs(struct ext4_ext_path *path)
372 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
374 int depth = path->p_depth;
375 int i;
377 for (i = 0; i <= depth; i++, path++)
378 if (path->p_bh) {
379 brelse(path->p_bh);
380 path->p_bh = NULL;
385 * ext4_ext_binsearch_idx:
386 * binary search for the closest index of the given block
387 * the header must be checked before calling this
389 static void
390 ext4_ext_binsearch_idx(struct inode *inode,
391 struct ext4_ext_path *path, ext4_lblk_t block)
393 struct ext4_extent_header *eh = path->p_hdr;
394 struct ext4_extent_idx *r, *l, *m;
397 ext_debug("binsearch for %u(idx): ", block);
399 l = EXT_FIRST_INDEX(eh) + 1;
400 r = EXT_LAST_INDEX(eh);
401 while (l <= r) {
402 m = l + (r - l) / 2;
403 if (block < le32_to_cpu(m->ei_block))
404 r = m - 1;
405 else
406 l = m + 1;
407 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
408 m, le32_to_cpu(m->ei_block),
409 r, le32_to_cpu(r->ei_block));
412 path->p_idx = l - 1;
413 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
414 idx_pblock(path->p_idx));
416 #ifdef CHECK_BINSEARCH
418 struct ext4_extent_idx *chix, *ix;
419 int k;
421 chix = ix = EXT_FIRST_INDEX(eh);
422 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
423 if (k != 0 &&
424 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
425 printk("k=%d, ix=0x%p, first=0x%p\n", k,
426 ix, EXT_FIRST_INDEX(eh));
427 printk("%u <= %u\n",
428 le32_to_cpu(ix->ei_block),
429 le32_to_cpu(ix[-1].ei_block));
431 BUG_ON(k && le32_to_cpu(ix->ei_block)
432 <= le32_to_cpu(ix[-1].ei_block));
433 if (block < le32_to_cpu(ix->ei_block))
434 break;
435 chix = ix;
437 BUG_ON(chix != path->p_idx);
439 #endif
444 * ext4_ext_binsearch:
445 * binary search for closest extent of the given block
446 * the header must be checked before calling this
448 static void
449 ext4_ext_binsearch(struct inode *inode,
450 struct ext4_ext_path *path, ext4_lblk_t block)
452 struct ext4_extent_header *eh = path->p_hdr;
453 struct ext4_extent *r, *l, *m;
455 if (eh->eh_entries == 0) {
457 * this leaf is empty:
458 * we get such a leaf in split/add case
460 return;
463 ext_debug("binsearch for %u: ", block);
465 l = EXT_FIRST_EXTENT(eh) + 1;
466 r = EXT_LAST_EXTENT(eh);
468 while (l <= r) {
469 m = l + (r - l) / 2;
470 if (block < le32_to_cpu(m->ee_block))
471 r = m - 1;
472 else
473 l = m + 1;
474 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
475 m, le32_to_cpu(m->ee_block),
476 r, le32_to_cpu(r->ee_block));
479 path->p_ext = l - 1;
480 ext_debug(" -> %d:%llu:%d ",
481 le32_to_cpu(path->p_ext->ee_block),
482 ext_pblock(path->p_ext),
483 ext4_ext_get_actual_len(path->p_ext));
485 #ifdef CHECK_BINSEARCH
487 struct ext4_extent *chex, *ex;
488 int k;
490 chex = ex = EXT_FIRST_EXTENT(eh);
491 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
492 BUG_ON(k && le32_to_cpu(ex->ee_block)
493 <= le32_to_cpu(ex[-1].ee_block));
494 if (block < le32_to_cpu(ex->ee_block))
495 break;
496 chex = ex;
498 BUG_ON(chex != path->p_ext);
500 #endif
504 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
506 struct ext4_extent_header *eh;
508 eh = ext_inode_hdr(inode);
509 eh->eh_depth = 0;
510 eh->eh_entries = 0;
511 eh->eh_magic = EXT4_EXT_MAGIC;
512 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
513 ext4_mark_inode_dirty(handle, inode);
514 ext4_ext_invalidate_cache(inode);
515 return 0;
518 struct ext4_ext_path *
519 ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
520 struct ext4_ext_path *path)
522 struct ext4_extent_header *eh;
523 struct buffer_head *bh;
524 short int depth, i, ppos = 0, alloc = 0;
526 eh = ext_inode_hdr(inode);
527 depth = ext_depth(inode);
528 if (ext4_ext_check_header(inode, eh, depth))
529 return ERR_PTR(-EIO);
532 /* account possible depth increase */
533 if (!path) {
534 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
535 GFP_NOFS);
536 if (!path)
537 return ERR_PTR(-ENOMEM);
538 alloc = 1;
540 path[0].p_hdr = eh;
542 i = depth;
543 /* walk through the tree */
544 while (i) {
545 ext_debug("depth %d: num %d, max %d\n",
546 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
548 ext4_ext_binsearch_idx(inode, path + ppos, block);
549 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
550 path[ppos].p_depth = i;
551 path[ppos].p_ext = NULL;
553 bh = sb_bread(inode->i_sb, path[ppos].p_block);
554 if (!bh)
555 goto err;
557 eh = ext_block_hdr(bh);
558 ppos++;
559 BUG_ON(ppos > depth);
560 path[ppos].p_bh = bh;
561 path[ppos].p_hdr = eh;
562 i--;
564 if (ext4_ext_check_header(inode, eh, i))
565 goto err;
568 path[ppos].p_depth = i;
569 path[ppos].p_hdr = eh;
570 path[ppos].p_ext = NULL;
571 path[ppos].p_idx = NULL;
573 /* find extent */
574 ext4_ext_binsearch(inode, path + ppos, block);
576 ext4_ext_show_path(inode, path);
578 return path;
580 err:
581 ext4_ext_drop_refs(path);
582 if (alloc)
583 kfree(path);
584 return ERR_PTR(-EIO);
588 * ext4_ext_insert_index:
589 * insert new index [@logical;@ptr] into the block at @curp;
590 * check where to insert: before @curp or after @curp
592 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
593 struct ext4_ext_path *curp,
594 int logical, ext4_fsblk_t ptr)
596 struct ext4_extent_idx *ix;
597 int len, err;
599 err = ext4_ext_get_access(handle, inode, curp);
600 if (err)
601 return err;
603 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
604 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
605 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
606 /* insert after */
607 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
608 len = (len - 1) * sizeof(struct ext4_extent_idx);
609 len = len < 0 ? 0 : len;
610 ext_debug("insert new index %d after: %llu. "
611 "move %d from 0x%p to 0x%p\n",
612 logical, ptr, len,
613 (curp->p_idx + 1), (curp->p_idx + 2));
614 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
616 ix = curp->p_idx + 1;
617 } else {
618 /* insert before */
619 len = len * sizeof(struct ext4_extent_idx);
620 len = len < 0 ? 0 : len;
621 ext_debug("insert new index %d before: %llu. "
622 "move %d from 0x%p to 0x%p\n",
623 logical, ptr, len,
624 curp->p_idx, (curp->p_idx + 1));
625 memmove(curp->p_idx + 1, curp->p_idx, len);
626 ix = curp->p_idx;
629 ix->ei_block = cpu_to_le32(logical);
630 ext4_idx_store_pblock(ix, ptr);
631 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
633 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
634 > le16_to_cpu(curp->p_hdr->eh_max));
635 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
637 err = ext4_ext_dirty(handle, inode, curp);
638 ext4_std_error(inode->i_sb, err);
640 return err;
644 * ext4_ext_split:
645 * inserts new subtree into the path, using free index entry
646 * at depth @at:
647 * - allocates all needed blocks (new leaf and all intermediate index blocks)
648 * - makes decision where to split
649 * - moves remaining extents and index entries (right to the split point)
650 * into the newly allocated blocks
651 * - initializes subtree
653 static int ext4_ext_split(handle_t *handle, struct inode *inode,
654 struct ext4_ext_path *path,
655 struct ext4_extent *newext, int at)
657 struct buffer_head *bh = NULL;
658 int depth = ext_depth(inode);
659 struct ext4_extent_header *neh;
660 struct ext4_extent_idx *fidx;
661 struct ext4_extent *ex;
662 int i = at, k, m, a;
663 ext4_fsblk_t newblock, oldblock;
664 __le32 border;
665 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
666 int err = 0;
668 /* make decision: where to split? */
669 /* FIXME: now decision is simplest: at current extent */
671 /* if current leaf will be split, then we should use
672 * border from split point */
673 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
674 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
675 border = path[depth].p_ext[1].ee_block;
676 ext_debug("leaf will be split."
677 " next leaf starts at %d\n",
678 le32_to_cpu(border));
679 } else {
680 border = newext->ee_block;
681 ext_debug("leaf will be added."
682 " next leaf starts at %d\n",
683 le32_to_cpu(border));
687 * If error occurs, then we break processing
688 * and mark filesystem read-only. index won't
689 * be inserted and tree will be in consistent
690 * state. Next mount will repair buffers too.
694 * Get array to track all allocated blocks.
695 * We need this to handle errors and free blocks
696 * upon them.
698 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
699 if (!ablocks)
700 return -ENOMEM;
702 /* allocate all needed blocks */
703 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
704 for (a = 0; a < depth - at; a++) {
705 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
706 if (newblock == 0)
707 goto cleanup;
708 ablocks[a] = newblock;
711 /* initialize new leaf */
712 newblock = ablocks[--a];
713 BUG_ON(newblock == 0);
714 bh = sb_getblk(inode->i_sb, newblock);
715 if (!bh) {
716 err = -EIO;
717 goto cleanup;
719 lock_buffer(bh);
721 err = ext4_journal_get_create_access(handle, bh);
722 if (err)
723 goto cleanup;
725 neh = ext_block_hdr(bh);
726 neh->eh_entries = 0;
727 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
728 neh->eh_magic = EXT4_EXT_MAGIC;
729 neh->eh_depth = 0;
730 ex = EXT_FIRST_EXTENT(neh);
732 /* move remainder of path[depth] to the new leaf */
733 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
734 /* start copy from next extent */
735 /* TODO: we could do it by single memmove */
736 m = 0;
737 path[depth].p_ext++;
738 while (path[depth].p_ext <=
739 EXT_MAX_EXTENT(path[depth].p_hdr)) {
740 ext_debug("move %d:%llu:%d in new leaf %llu\n",
741 le32_to_cpu(path[depth].p_ext->ee_block),
742 ext_pblock(path[depth].p_ext),
743 ext4_ext_get_actual_len(path[depth].p_ext),
744 newblock);
745 /*memmove(ex++, path[depth].p_ext++,
746 sizeof(struct ext4_extent));
747 neh->eh_entries++;*/
748 path[depth].p_ext++;
749 m++;
751 if (m) {
752 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
753 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
756 set_buffer_uptodate(bh);
757 unlock_buffer(bh);
759 err = ext4_journal_dirty_metadata(handle, bh);
760 if (err)
761 goto cleanup;
762 brelse(bh);
763 bh = NULL;
765 /* correct old leaf */
766 if (m) {
767 err = ext4_ext_get_access(handle, inode, path + depth);
768 if (err)
769 goto cleanup;
770 path[depth].p_hdr->eh_entries =
771 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
772 err = ext4_ext_dirty(handle, inode, path + depth);
773 if (err)
774 goto cleanup;
778 /* create intermediate indexes */
779 k = depth - at - 1;
780 BUG_ON(k < 0);
781 if (k)
782 ext_debug("create %d intermediate indices\n", k);
783 /* insert new index into current index block */
784 /* current depth stored in i var */
785 i = depth - 1;
786 while (k--) {
787 oldblock = newblock;
788 newblock = ablocks[--a];
789 bh = sb_getblk(inode->i_sb, newblock);
790 if (!bh) {
791 err = -EIO;
792 goto cleanup;
794 lock_buffer(bh);
796 err = ext4_journal_get_create_access(handle, bh);
797 if (err)
798 goto cleanup;
800 neh = ext_block_hdr(bh);
801 neh->eh_entries = cpu_to_le16(1);
802 neh->eh_magic = EXT4_EXT_MAGIC;
803 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
804 neh->eh_depth = cpu_to_le16(depth - i);
805 fidx = EXT_FIRST_INDEX(neh);
806 fidx->ei_block = border;
807 ext4_idx_store_pblock(fidx, oldblock);
809 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
810 i, newblock, le32_to_cpu(border), oldblock);
811 /* copy indexes */
812 m = 0;
813 path[i].p_idx++;
815 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
816 EXT_MAX_INDEX(path[i].p_hdr));
817 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
818 EXT_LAST_INDEX(path[i].p_hdr));
819 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
820 ext_debug("%d: move %d:%llu in new index %llu\n", i,
821 le32_to_cpu(path[i].p_idx->ei_block),
822 idx_pblock(path[i].p_idx),
823 newblock);
824 /*memmove(++fidx, path[i].p_idx++,
825 sizeof(struct ext4_extent_idx));
826 neh->eh_entries++;
827 BUG_ON(neh->eh_entries > neh->eh_max);*/
828 path[i].p_idx++;
829 m++;
831 if (m) {
832 memmove(++fidx, path[i].p_idx - m,
833 sizeof(struct ext4_extent_idx) * m);
834 neh->eh_entries =
835 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
837 set_buffer_uptodate(bh);
838 unlock_buffer(bh);
840 err = ext4_journal_dirty_metadata(handle, bh);
841 if (err)
842 goto cleanup;
843 brelse(bh);
844 bh = NULL;
846 /* correct old index */
847 if (m) {
848 err = ext4_ext_get_access(handle, inode, path + i);
849 if (err)
850 goto cleanup;
851 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
852 err = ext4_ext_dirty(handle, inode, path + i);
853 if (err)
854 goto cleanup;
857 i--;
860 /* insert new index */
861 err = ext4_ext_insert_index(handle, inode, path + at,
862 le32_to_cpu(border), newblock);
864 cleanup:
865 if (bh) {
866 if (buffer_locked(bh))
867 unlock_buffer(bh);
868 brelse(bh);
871 if (err) {
872 /* free all allocated blocks in error case */
873 for (i = 0; i < depth; i++) {
874 if (!ablocks[i])
875 continue;
876 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
879 kfree(ablocks);
881 return err;
885 * ext4_ext_grow_indepth:
886 * implements tree growing procedure:
887 * - allocates new block
888 * - moves top-level data (index block or leaf) into the new block
889 * - initializes new top-level, creating index that points to the
890 * just created block
892 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
893 struct ext4_ext_path *path,
894 struct ext4_extent *newext)
896 struct ext4_ext_path *curp = path;
897 struct ext4_extent_header *neh;
898 struct ext4_extent_idx *fidx;
899 struct buffer_head *bh;
900 ext4_fsblk_t newblock;
901 int err = 0;
903 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
904 if (newblock == 0)
905 return err;
907 bh = sb_getblk(inode->i_sb, newblock);
908 if (!bh) {
909 err = -EIO;
910 ext4_std_error(inode->i_sb, err);
911 return err;
913 lock_buffer(bh);
915 err = ext4_journal_get_create_access(handle, bh);
916 if (err) {
917 unlock_buffer(bh);
918 goto out;
921 /* move top-level index/leaf into new block */
922 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
924 /* set size of new block */
925 neh = ext_block_hdr(bh);
926 /* old root could have indexes or leaves
927 * so calculate e_max right way */
928 if (ext_depth(inode))
929 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
930 else
931 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
932 neh->eh_magic = EXT4_EXT_MAGIC;
933 set_buffer_uptodate(bh);
934 unlock_buffer(bh);
936 err = ext4_journal_dirty_metadata(handle, bh);
937 if (err)
938 goto out;
940 /* create index in new top-level index: num,max,pointer */
941 err = ext4_ext_get_access(handle, inode, curp);
942 if (err)
943 goto out;
945 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
946 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
947 curp->p_hdr->eh_entries = cpu_to_le16(1);
948 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
950 if (path[0].p_hdr->eh_depth)
951 curp->p_idx->ei_block =
952 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
953 else
954 curp->p_idx->ei_block =
955 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
956 ext4_idx_store_pblock(curp->p_idx, newblock);
958 neh = ext_inode_hdr(inode);
959 fidx = EXT_FIRST_INDEX(neh);
960 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
961 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
962 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
964 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
965 err = ext4_ext_dirty(handle, inode, curp);
966 out:
967 brelse(bh);
969 return err;
973 * ext4_ext_create_new_leaf:
974 * finds empty index and adds new leaf.
975 * if no free index is found, then it requests in-depth growing.
977 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
978 struct ext4_ext_path *path,
979 struct ext4_extent *newext)
981 struct ext4_ext_path *curp;
982 int depth, i, err = 0;
984 repeat:
985 i = depth = ext_depth(inode);
987 /* walk up to the tree and look for free index entry */
988 curp = path + depth;
989 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
990 i--;
991 curp--;
994 /* we use already allocated block for index block,
995 * so subsequent data blocks should be contiguous */
996 if (EXT_HAS_FREE_INDEX(curp)) {
997 /* if we found index with free entry, then use that
998 * entry: create all needed subtree and add new leaf */
999 err = ext4_ext_split(handle, inode, path, newext, i);
1001 /* refill path */
1002 ext4_ext_drop_refs(path);
1003 path = ext4_ext_find_extent(inode,
1004 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1005 path);
1006 if (IS_ERR(path))
1007 err = PTR_ERR(path);
1008 } else {
1009 /* tree is full, time to grow in depth */
1010 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1011 if (err)
1012 goto out;
1014 /* refill path */
1015 ext4_ext_drop_refs(path);
1016 path = ext4_ext_find_extent(inode,
1017 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1018 path);
1019 if (IS_ERR(path)) {
1020 err = PTR_ERR(path);
1021 goto out;
1025 * only first (depth 0 -> 1) produces free space;
1026 * in all other cases we have to split the grown tree
1028 depth = ext_depth(inode);
1029 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1030 /* now we need to split */
1031 goto repeat;
1035 out:
1036 return err;
1040 * search the closest allocated block to the left for *logical
1041 * and returns it at @logical + it's physical address at @phys
1042 * if *logical is the smallest allocated block, the function
1043 * returns 0 at @phys
1044 * return value contains 0 (success) or error code
1047 ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1048 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1050 struct ext4_extent_idx *ix;
1051 struct ext4_extent *ex;
1052 int depth, ee_len;
1054 BUG_ON(path == NULL);
1055 depth = path->p_depth;
1056 *phys = 0;
1058 if (depth == 0 && path->p_ext == NULL)
1059 return 0;
1061 /* usually extent in the path covers blocks smaller
1062 * then *logical, but it can be that extent is the
1063 * first one in the file */
1065 ex = path[depth].p_ext;
1066 ee_len = ext4_ext_get_actual_len(ex);
1067 if (*logical < le32_to_cpu(ex->ee_block)) {
1068 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1069 while (--depth >= 0) {
1070 ix = path[depth].p_idx;
1071 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1073 return 0;
1076 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1078 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1079 *phys = ext_pblock(ex) + ee_len - 1;
1080 return 0;
1084 * search the closest allocated block to the right for *logical
1085 * and returns it at @logical + it's physical address at @phys
1086 * if *logical is the smallest allocated block, the function
1087 * returns 0 at @phys
1088 * return value contains 0 (success) or error code
1091 ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1092 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1094 struct buffer_head *bh = NULL;
1095 struct ext4_extent_header *eh;
1096 struct ext4_extent_idx *ix;
1097 struct ext4_extent *ex;
1098 ext4_fsblk_t block;
1099 int depth, ee_len;
1101 BUG_ON(path == NULL);
1102 depth = path->p_depth;
1103 *phys = 0;
1105 if (depth == 0 && path->p_ext == NULL)
1106 return 0;
1108 /* usually extent in the path covers blocks smaller
1109 * then *logical, but it can be that extent is the
1110 * first one in the file */
1112 ex = path[depth].p_ext;
1113 ee_len = ext4_ext_get_actual_len(ex);
1114 if (*logical < le32_to_cpu(ex->ee_block)) {
1115 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1116 while (--depth >= 0) {
1117 ix = path[depth].p_idx;
1118 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1120 *logical = le32_to_cpu(ex->ee_block);
1121 *phys = ext_pblock(ex);
1122 return 0;
1125 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1127 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1128 /* next allocated block in this leaf */
1129 ex++;
1130 *logical = le32_to_cpu(ex->ee_block);
1131 *phys = ext_pblock(ex);
1132 return 0;
1135 /* go up and search for index to the right */
1136 while (--depth >= 0) {
1137 ix = path[depth].p_idx;
1138 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1139 break;
1142 if (depth < 0) {
1143 /* we've gone up to the root and
1144 * found no index to the right */
1145 return 0;
1148 /* we've found index to the right, let's
1149 * follow it and find the closest allocated
1150 * block to the right */
1151 ix++;
1152 block = idx_pblock(ix);
1153 while (++depth < path->p_depth) {
1154 bh = sb_bread(inode->i_sb, block);
1155 if (bh == NULL)
1156 return -EIO;
1157 eh = ext_block_hdr(bh);
1158 if (ext4_ext_check_header(inode, eh, depth)) {
1159 put_bh(bh);
1160 return -EIO;
1162 ix = EXT_FIRST_INDEX(eh);
1163 block = idx_pblock(ix);
1164 put_bh(bh);
1167 bh = sb_bread(inode->i_sb, block);
1168 if (bh == NULL)
1169 return -EIO;
1170 eh = ext_block_hdr(bh);
1171 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1172 put_bh(bh);
1173 return -EIO;
1175 ex = EXT_FIRST_EXTENT(eh);
1176 *logical = le32_to_cpu(ex->ee_block);
1177 *phys = ext_pblock(ex);
1178 put_bh(bh);
1179 return 0;
1184 * ext4_ext_next_allocated_block:
1185 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1186 * NOTE: it considers block number from index entry as
1187 * allocated block. Thus, index entries have to be consistent
1188 * with leaves.
1190 static ext4_lblk_t
1191 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1193 int depth;
1195 BUG_ON(path == NULL);
1196 depth = path->p_depth;
1198 if (depth == 0 && path->p_ext == NULL)
1199 return EXT_MAX_BLOCK;
1201 while (depth >= 0) {
1202 if (depth == path->p_depth) {
1203 /* leaf */
1204 if (path[depth].p_ext !=
1205 EXT_LAST_EXTENT(path[depth].p_hdr))
1206 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1207 } else {
1208 /* index */
1209 if (path[depth].p_idx !=
1210 EXT_LAST_INDEX(path[depth].p_hdr))
1211 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1213 depth--;
1216 return EXT_MAX_BLOCK;
1220 * ext4_ext_next_leaf_block:
1221 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1223 static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
1224 struct ext4_ext_path *path)
1226 int depth;
1228 BUG_ON(path == NULL);
1229 depth = path->p_depth;
1231 /* zero-tree has no leaf blocks at all */
1232 if (depth == 0)
1233 return EXT_MAX_BLOCK;
1235 /* go to index block */
1236 depth--;
1238 while (depth >= 0) {
1239 if (path[depth].p_idx !=
1240 EXT_LAST_INDEX(path[depth].p_hdr))
1241 return (ext4_lblk_t)
1242 le32_to_cpu(path[depth].p_idx[1].ei_block);
1243 depth--;
1246 return EXT_MAX_BLOCK;
1250 * ext4_ext_correct_indexes:
1251 * if leaf gets modified and modified extent is first in the leaf,
1252 * then we have to correct all indexes above.
1253 * TODO: do we need to correct tree in all cases?
1255 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1256 struct ext4_ext_path *path)
1258 struct ext4_extent_header *eh;
1259 int depth = ext_depth(inode);
1260 struct ext4_extent *ex;
1261 __le32 border;
1262 int k, err = 0;
1264 eh = path[depth].p_hdr;
1265 ex = path[depth].p_ext;
1266 BUG_ON(ex == NULL);
1267 BUG_ON(eh == NULL);
1269 if (depth == 0) {
1270 /* there is no tree at all */
1271 return 0;
1274 if (ex != EXT_FIRST_EXTENT(eh)) {
1275 /* we correct tree if first leaf got modified only */
1276 return 0;
1280 * TODO: we need correction if border is smaller than current one
1282 k = depth - 1;
1283 border = path[depth].p_ext->ee_block;
1284 err = ext4_ext_get_access(handle, inode, path + k);
1285 if (err)
1286 return err;
1287 path[k].p_idx->ei_block = border;
1288 err = ext4_ext_dirty(handle, inode, path + k);
1289 if (err)
1290 return err;
1292 while (k--) {
1293 /* change all left-side indexes */
1294 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1295 break;
1296 err = ext4_ext_get_access(handle, inode, path + k);
1297 if (err)
1298 break;
1299 path[k].p_idx->ei_block = border;
1300 err = ext4_ext_dirty(handle, inode, path + k);
1301 if (err)
1302 break;
1305 return err;
1308 static int
1309 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1310 struct ext4_extent *ex2)
1312 unsigned short ext1_ee_len, ext2_ee_len, max_len;
1315 * Make sure that either both extents are uninitialized, or
1316 * both are _not_.
1318 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1319 return 0;
1321 if (ext4_ext_is_uninitialized(ex1))
1322 max_len = EXT_UNINIT_MAX_LEN;
1323 else
1324 max_len = EXT_INIT_MAX_LEN;
1326 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1327 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1329 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1330 le32_to_cpu(ex2->ee_block))
1331 return 0;
1334 * To allow future support for preallocated extents to be added
1335 * as an RO_COMPAT feature, refuse to merge to extents if
1336 * this can result in the top bit of ee_len being set.
1338 if (ext1_ee_len + ext2_ee_len > max_len)
1339 return 0;
1340 #ifdef AGGRESSIVE_TEST
1341 if (ext1_ee_len >= 4)
1342 return 0;
1343 #endif
1345 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
1346 return 1;
1347 return 0;
1351 * This function tries to merge the "ex" extent to the next extent in the tree.
1352 * It always tries to merge towards right. If you want to merge towards
1353 * left, pass "ex - 1" as argument instead of "ex".
1354 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1355 * 1 if they got merged.
1357 int ext4_ext_try_to_merge(struct inode *inode,
1358 struct ext4_ext_path *path,
1359 struct ext4_extent *ex)
1361 struct ext4_extent_header *eh;
1362 unsigned int depth, len;
1363 int merge_done = 0;
1364 int uninitialized = 0;
1366 depth = ext_depth(inode);
1367 BUG_ON(path[depth].p_hdr == NULL);
1368 eh = path[depth].p_hdr;
1370 while (ex < EXT_LAST_EXTENT(eh)) {
1371 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1372 break;
1373 /* merge with next extent! */
1374 if (ext4_ext_is_uninitialized(ex))
1375 uninitialized = 1;
1376 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1377 + ext4_ext_get_actual_len(ex + 1));
1378 if (uninitialized)
1379 ext4_ext_mark_uninitialized(ex);
1381 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1382 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1383 * sizeof(struct ext4_extent);
1384 memmove(ex + 1, ex + 2, len);
1386 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
1387 merge_done = 1;
1388 WARN_ON(eh->eh_entries == 0);
1389 if (!eh->eh_entries)
1390 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1391 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1394 return merge_done;
1398 * check if a portion of the "newext" extent overlaps with an
1399 * existing extent.
1401 * If there is an overlap discovered, it updates the length of the newext
1402 * such that there will be no overlap, and then returns 1.
1403 * If there is no overlap found, it returns 0.
1405 unsigned int ext4_ext_check_overlap(struct inode *inode,
1406 struct ext4_extent *newext,
1407 struct ext4_ext_path *path)
1409 ext4_lblk_t b1, b2;
1410 unsigned int depth, len1;
1411 unsigned int ret = 0;
1413 b1 = le32_to_cpu(newext->ee_block);
1414 len1 = ext4_ext_get_actual_len(newext);
1415 depth = ext_depth(inode);
1416 if (!path[depth].p_ext)
1417 goto out;
1418 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1421 * get the next allocated block if the extent in the path
1422 * is before the requested block(s)
1424 if (b2 < b1) {
1425 b2 = ext4_ext_next_allocated_block(path);
1426 if (b2 == EXT_MAX_BLOCK)
1427 goto out;
1430 /* check for wrap through zero on extent logical start block*/
1431 if (b1 + len1 < b1) {
1432 len1 = EXT_MAX_BLOCK - b1;
1433 newext->ee_len = cpu_to_le16(len1);
1434 ret = 1;
1437 /* check for overlap */
1438 if (b1 + len1 > b2) {
1439 newext->ee_len = cpu_to_le16(b2 - b1);
1440 ret = 1;
1442 out:
1443 return ret;
1447 * ext4_ext_insert_extent:
1448 * tries to merge requsted extent into the existing extent or
1449 * inserts requested extent as new one into the tree,
1450 * creating new leaf in the no-space case.
1452 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1453 struct ext4_ext_path *path,
1454 struct ext4_extent *newext)
1456 struct ext4_extent_header * eh;
1457 struct ext4_extent *ex, *fex;
1458 struct ext4_extent *nearex; /* nearest extent */
1459 struct ext4_ext_path *npath = NULL;
1460 int depth, len, err;
1461 ext4_lblk_t next;
1462 unsigned uninitialized = 0;
1464 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
1465 depth = ext_depth(inode);
1466 ex = path[depth].p_ext;
1467 BUG_ON(path[depth].p_hdr == NULL);
1469 /* try to insert block into found extent and return */
1470 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
1471 ext_debug("append %d block to %d:%d (from %llu)\n",
1472 ext4_ext_get_actual_len(newext),
1473 le32_to_cpu(ex->ee_block),
1474 ext4_ext_get_actual_len(ex), ext_pblock(ex));
1475 err = ext4_ext_get_access(handle, inode, path + depth);
1476 if (err)
1477 return err;
1480 * ext4_can_extents_be_merged should have checked that either
1481 * both extents are uninitialized, or both aren't. Thus we
1482 * need to check only one of them here.
1484 if (ext4_ext_is_uninitialized(ex))
1485 uninitialized = 1;
1486 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1487 + ext4_ext_get_actual_len(newext));
1488 if (uninitialized)
1489 ext4_ext_mark_uninitialized(ex);
1490 eh = path[depth].p_hdr;
1491 nearex = ex;
1492 goto merge;
1495 repeat:
1496 depth = ext_depth(inode);
1497 eh = path[depth].p_hdr;
1498 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1499 goto has_space;
1501 /* probably next leaf has space for us? */
1502 fex = EXT_LAST_EXTENT(eh);
1503 next = ext4_ext_next_leaf_block(inode, path);
1504 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1505 && next != EXT_MAX_BLOCK) {
1506 ext_debug("next leaf block - %d\n", next);
1507 BUG_ON(npath != NULL);
1508 npath = ext4_ext_find_extent(inode, next, NULL);
1509 if (IS_ERR(npath))
1510 return PTR_ERR(npath);
1511 BUG_ON(npath->p_depth != path->p_depth);
1512 eh = npath[depth].p_hdr;
1513 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1514 ext_debug("next leaf isnt full(%d)\n",
1515 le16_to_cpu(eh->eh_entries));
1516 path = npath;
1517 goto repeat;
1519 ext_debug("next leaf has no free space(%d,%d)\n",
1520 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1524 * There is no free space in the found leaf.
1525 * We're gonna add a new leaf in the tree.
1527 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1528 if (err)
1529 goto cleanup;
1530 depth = ext_depth(inode);
1531 eh = path[depth].p_hdr;
1533 has_space:
1534 nearex = path[depth].p_ext;
1536 err = ext4_ext_get_access(handle, inode, path + depth);
1537 if (err)
1538 goto cleanup;
1540 if (!nearex) {
1541 /* there is no extent in this leaf, create first one */
1542 ext_debug("first extent in the leaf: %d:%llu:%d\n",
1543 le32_to_cpu(newext->ee_block),
1544 ext_pblock(newext),
1545 ext4_ext_get_actual_len(newext));
1546 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1547 } else if (le32_to_cpu(newext->ee_block)
1548 > le32_to_cpu(nearex->ee_block)) {
1549 /* BUG_ON(newext->ee_block == nearex->ee_block); */
1550 if (nearex != EXT_LAST_EXTENT(eh)) {
1551 len = EXT_MAX_EXTENT(eh) - nearex;
1552 len = (len - 1) * sizeof(struct ext4_extent);
1553 len = len < 0 ? 0 : len;
1554 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
1555 "move %d from 0x%p to 0x%p\n",
1556 le32_to_cpu(newext->ee_block),
1557 ext_pblock(newext),
1558 ext4_ext_get_actual_len(newext),
1559 nearex, len, nearex + 1, nearex + 2);
1560 memmove(nearex + 2, nearex + 1, len);
1562 path[depth].p_ext = nearex + 1;
1563 } else {
1564 BUG_ON(newext->ee_block == nearex->ee_block);
1565 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1566 len = len < 0 ? 0 : len;
1567 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
1568 "move %d from 0x%p to 0x%p\n",
1569 le32_to_cpu(newext->ee_block),
1570 ext_pblock(newext),
1571 ext4_ext_get_actual_len(newext),
1572 nearex, len, nearex + 1, nearex + 2);
1573 memmove(nearex + 1, nearex, len);
1574 path[depth].p_ext = nearex;
1577 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1578 nearex = path[depth].p_ext;
1579 nearex->ee_block = newext->ee_block;
1580 ext4_ext_store_pblock(nearex, ext_pblock(newext));
1581 nearex->ee_len = newext->ee_len;
1583 merge:
1584 /* try to merge extents to the right */
1585 ext4_ext_try_to_merge(inode, path, nearex);
1587 /* try to merge extents to the left */
1589 /* time to correct all indexes above */
1590 err = ext4_ext_correct_indexes(handle, inode, path);
1591 if (err)
1592 goto cleanup;
1594 err = ext4_ext_dirty(handle, inode, path + depth);
1596 cleanup:
1597 if (npath) {
1598 ext4_ext_drop_refs(npath);
1599 kfree(npath);
1601 ext4_ext_tree_changed(inode);
1602 ext4_ext_invalidate_cache(inode);
1603 return err;
1606 static void
1607 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
1608 __u32 len, ext4_fsblk_t start, int type)
1610 struct ext4_ext_cache *cex;
1611 BUG_ON(len == 0);
1612 cex = &EXT4_I(inode)->i_cached_extent;
1613 cex->ec_type = type;
1614 cex->ec_block = block;
1615 cex->ec_len = len;
1616 cex->ec_start = start;
1620 * ext4_ext_put_gap_in_cache:
1621 * calculate boundaries of the gap that the requested block fits into
1622 * and cache this gap
1624 static void
1625 ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1626 ext4_lblk_t block)
1628 int depth = ext_depth(inode);
1629 unsigned long len;
1630 ext4_lblk_t lblock;
1631 struct ext4_extent *ex;
1633 ex = path[depth].p_ext;
1634 if (ex == NULL) {
1635 /* there is no extent yet, so gap is [0;-] */
1636 lblock = 0;
1637 len = EXT_MAX_BLOCK;
1638 ext_debug("cache gap(whole file):");
1639 } else if (block < le32_to_cpu(ex->ee_block)) {
1640 lblock = block;
1641 len = le32_to_cpu(ex->ee_block) - block;
1642 ext_debug("cache gap(before): %u [%u:%u]",
1643 block,
1644 le32_to_cpu(ex->ee_block),
1645 ext4_ext_get_actual_len(ex));
1646 } else if (block >= le32_to_cpu(ex->ee_block)
1647 + ext4_ext_get_actual_len(ex)) {
1648 ext4_lblk_t next;
1649 lblock = le32_to_cpu(ex->ee_block)
1650 + ext4_ext_get_actual_len(ex);
1652 next = ext4_ext_next_allocated_block(path);
1653 ext_debug("cache gap(after): [%u:%u] %u",
1654 le32_to_cpu(ex->ee_block),
1655 ext4_ext_get_actual_len(ex),
1656 block);
1657 BUG_ON(next == lblock);
1658 len = next - lblock;
1659 } else {
1660 lblock = len = 0;
1661 BUG();
1664 ext_debug(" -> %u:%lu\n", lblock, len);
1665 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1668 static int
1669 ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
1670 struct ext4_extent *ex)
1672 struct ext4_ext_cache *cex;
1674 cex = &EXT4_I(inode)->i_cached_extent;
1676 /* has cache valid data? */
1677 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1678 return EXT4_EXT_CACHE_NO;
1680 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1681 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1682 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1683 ex->ee_block = cpu_to_le32(cex->ec_block);
1684 ext4_ext_store_pblock(ex, cex->ec_start);
1685 ex->ee_len = cpu_to_le16(cex->ec_len);
1686 ext_debug("%u cached by %u:%u:%llu\n",
1687 block,
1688 cex->ec_block, cex->ec_len, cex->ec_start);
1689 return cex->ec_type;
1692 /* not in cache */
1693 return EXT4_EXT_CACHE_NO;
1697 * ext4_ext_rm_idx:
1698 * removes index from the index block.
1699 * It's used in truncate case only, thus all requests are for
1700 * last index in the block only.
1702 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1703 struct ext4_ext_path *path)
1705 struct buffer_head *bh;
1706 int err;
1707 ext4_fsblk_t leaf;
1709 /* free index block */
1710 path--;
1711 leaf = idx_pblock(path->p_idx);
1712 BUG_ON(path->p_hdr->eh_entries == 0);
1713 err = ext4_ext_get_access(handle, inode, path);
1714 if (err)
1715 return err;
1716 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1717 err = ext4_ext_dirty(handle, inode, path);
1718 if (err)
1719 return err;
1720 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1721 bh = sb_find_get_block(inode->i_sb, leaf);
1722 ext4_forget(handle, 1, inode, bh, leaf);
1723 ext4_free_blocks(handle, inode, leaf, 1, 1);
1724 return err;
1728 * ext4_ext_calc_credits_for_insert:
1729 * This routine returns max. credits that the extent tree can consume.
1730 * It should be OK for low-performance paths like ->writepage()
1731 * To allow many writing processes to fit into a single transaction,
1732 * the caller should calculate credits under i_data_sem and
1733 * pass the actual path.
1735 int ext4_ext_calc_credits_for_insert(struct inode *inode,
1736 struct ext4_ext_path *path)
1738 int depth, needed;
1740 if (path) {
1741 /* probably there is space in leaf? */
1742 depth = ext_depth(inode);
1743 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1744 < le16_to_cpu(path[depth].p_hdr->eh_max))
1745 return 1;
1749 * given 32-bit logical block (4294967296 blocks), max. tree
1750 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
1751 * Let's also add one more level for imbalance.
1753 depth = 5;
1755 /* allocation of new data block(s) */
1756 needed = 2;
1759 * tree can be full, so it would need to grow in depth:
1760 * we need one credit to modify old root, credits for
1761 * new root will be added in split accounting
1763 needed += 1;
1766 * Index split can happen, we would need:
1767 * allocate intermediate indexes (bitmap + group)
1768 * + change two blocks at each level, but root (already included)
1770 needed += (depth * 2) + (depth * 2);
1772 /* any allocation modifies superblock */
1773 needed += 1;
1775 return needed;
1778 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1779 struct ext4_extent *ex,
1780 ext4_lblk_t from, ext4_lblk_t to)
1782 struct buffer_head *bh;
1783 unsigned short ee_len = ext4_ext_get_actual_len(ex);
1784 int i, metadata = 0;
1786 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1787 metadata = 1;
1788 #ifdef EXTENTS_STATS
1790 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1791 spin_lock(&sbi->s_ext_stats_lock);
1792 sbi->s_ext_blocks += ee_len;
1793 sbi->s_ext_extents++;
1794 if (ee_len < sbi->s_ext_min)
1795 sbi->s_ext_min = ee_len;
1796 if (ee_len > sbi->s_ext_max)
1797 sbi->s_ext_max = ee_len;
1798 if (ext_depth(inode) > sbi->s_depth_max)
1799 sbi->s_depth_max = ext_depth(inode);
1800 spin_unlock(&sbi->s_ext_stats_lock);
1802 #endif
1803 if (from >= le32_to_cpu(ex->ee_block)
1804 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
1805 /* tail removal */
1806 ext4_lblk_t num;
1807 ext4_fsblk_t start;
1809 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1810 start = ext_pblock(ex) + ee_len - num;
1811 ext_debug("free last %u blocks starting %llu\n", num, start);
1812 for (i = 0; i < num; i++) {
1813 bh = sb_find_get_block(inode->i_sb, start + i);
1814 ext4_forget(handle, 0, inode, bh, start + i);
1816 ext4_free_blocks(handle, inode, start, num, metadata);
1817 } else if (from == le32_to_cpu(ex->ee_block)
1818 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
1819 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
1820 from, to, le32_to_cpu(ex->ee_block), ee_len);
1821 } else {
1822 printk(KERN_INFO "strange request: removal(2) "
1823 "%u-%u from %u:%u\n",
1824 from, to, le32_to_cpu(ex->ee_block), ee_len);
1826 return 0;
1829 static int
1830 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1831 struct ext4_ext_path *path, ext4_lblk_t start)
1833 int err = 0, correct_index = 0;
1834 int depth = ext_depth(inode), credits;
1835 struct ext4_extent_header *eh;
1836 ext4_lblk_t a, b, block;
1837 unsigned num;
1838 ext4_lblk_t ex_ee_block;
1839 unsigned short ex_ee_len;
1840 unsigned uninitialized = 0;
1841 struct ext4_extent *ex;
1843 /* the header must be checked already in ext4_ext_remove_space() */
1844 ext_debug("truncate since %u in leaf\n", start);
1845 if (!path[depth].p_hdr)
1846 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1847 eh = path[depth].p_hdr;
1848 BUG_ON(eh == NULL);
1850 /* find where to start removing */
1851 ex = EXT_LAST_EXTENT(eh);
1853 ex_ee_block = le32_to_cpu(ex->ee_block);
1854 if (ext4_ext_is_uninitialized(ex))
1855 uninitialized = 1;
1856 ex_ee_len = ext4_ext_get_actual_len(ex);
1858 while (ex >= EXT_FIRST_EXTENT(eh) &&
1859 ex_ee_block + ex_ee_len > start) {
1860 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1861 path[depth].p_ext = ex;
1863 a = ex_ee_block > start ? ex_ee_block : start;
1864 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1865 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1867 ext_debug(" border %u:%u\n", a, b);
1869 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1870 block = 0;
1871 num = 0;
1872 BUG();
1873 } else if (a != ex_ee_block) {
1874 /* remove tail of the extent */
1875 block = ex_ee_block;
1876 num = a - block;
1877 } else if (b != ex_ee_block + ex_ee_len - 1) {
1878 /* remove head of the extent */
1879 block = a;
1880 num = b - a;
1881 /* there is no "make a hole" API yet */
1882 BUG();
1883 } else {
1884 /* remove whole extent: excellent! */
1885 block = ex_ee_block;
1886 num = 0;
1887 BUG_ON(a != ex_ee_block);
1888 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1891 /* at present, extent can't cross block group: */
1892 /* leaf + bitmap + group desc + sb + inode */
1893 credits = 5;
1894 if (ex == EXT_FIRST_EXTENT(eh)) {
1895 correct_index = 1;
1896 credits += (ext_depth(inode)) + 1;
1898 #ifdef CONFIG_QUOTA
1899 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1900 #endif
1902 handle = ext4_ext_journal_restart(handle, credits);
1903 if (IS_ERR(handle)) {
1904 err = PTR_ERR(handle);
1905 goto out;
1908 err = ext4_ext_get_access(handle, inode, path + depth);
1909 if (err)
1910 goto out;
1912 err = ext4_remove_blocks(handle, inode, ex, a, b);
1913 if (err)
1914 goto out;
1916 if (num == 0) {
1917 /* this extent is removed; mark slot entirely unused */
1918 ext4_ext_store_pblock(ex, 0);
1919 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1922 ex->ee_block = cpu_to_le32(block);
1923 ex->ee_len = cpu_to_le16(num);
1925 * Do not mark uninitialized if all the blocks in the
1926 * extent have been removed.
1928 if (uninitialized && num)
1929 ext4_ext_mark_uninitialized(ex);
1931 err = ext4_ext_dirty(handle, inode, path + depth);
1932 if (err)
1933 goto out;
1935 ext_debug("new extent: %u:%u:%llu\n", block, num,
1936 ext_pblock(ex));
1937 ex--;
1938 ex_ee_block = le32_to_cpu(ex->ee_block);
1939 ex_ee_len = ext4_ext_get_actual_len(ex);
1942 if (correct_index && eh->eh_entries)
1943 err = ext4_ext_correct_indexes(handle, inode, path);
1945 /* if this leaf is free, then we should
1946 * remove it from index block above */
1947 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1948 err = ext4_ext_rm_idx(handle, inode, path + depth);
1950 out:
1951 return err;
1955 * ext4_ext_more_to_rm:
1956 * returns 1 if current index has to be freed (even partial)
1958 static int
1959 ext4_ext_more_to_rm(struct ext4_ext_path *path)
1961 BUG_ON(path->p_idx == NULL);
1963 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1964 return 0;
1967 * if truncate on deeper level happened, it wasn't partial,
1968 * so we have to consider current index for truncation
1970 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1971 return 0;
1972 return 1;
1975 static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
1977 struct super_block *sb = inode->i_sb;
1978 int depth = ext_depth(inode);
1979 struct ext4_ext_path *path;
1980 handle_t *handle;
1981 int i = 0, err = 0;
1983 ext_debug("truncate since %u\n", start);
1985 /* probably first extent we're gonna free will be last in block */
1986 handle = ext4_journal_start(inode, depth + 1);
1987 if (IS_ERR(handle))
1988 return PTR_ERR(handle);
1990 ext4_ext_invalidate_cache(inode);
1993 * We start scanning from right side, freeing all the blocks
1994 * after i_size and walking into the tree depth-wise.
1996 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1997 if (path == NULL) {
1998 ext4_journal_stop(handle);
1999 return -ENOMEM;
2001 path[0].p_hdr = ext_inode_hdr(inode);
2002 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
2003 err = -EIO;
2004 goto out;
2006 path[0].p_depth = depth;
2008 while (i >= 0 && err == 0) {
2009 if (i == depth) {
2010 /* this is leaf block */
2011 err = ext4_ext_rm_leaf(handle, inode, path, start);
2012 /* root level has p_bh == NULL, brelse() eats this */
2013 brelse(path[i].p_bh);
2014 path[i].p_bh = NULL;
2015 i--;
2016 continue;
2019 /* this is index block */
2020 if (!path[i].p_hdr) {
2021 ext_debug("initialize header\n");
2022 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2025 if (!path[i].p_idx) {
2026 /* this level hasn't been touched yet */
2027 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2028 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2029 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2030 path[i].p_hdr,
2031 le16_to_cpu(path[i].p_hdr->eh_entries));
2032 } else {
2033 /* we were already here, see at next index */
2034 path[i].p_idx--;
2037 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2038 i, EXT_FIRST_INDEX(path[i].p_hdr),
2039 path[i].p_idx);
2040 if (ext4_ext_more_to_rm(path + i)) {
2041 struct buffer_head *bh;
2042 /* go to the next level */
2043 ext_debug("move to level %d (block %llu)\n",
2044 i + 1, idx_pblock(path[i].p_idx));
2045 memset(path + i + 1, 0, sizeof(*path));
2046 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2047 if (!bh) {
2048 /* should we reset i_size? */
2049 err = -EIO;
2050 break;
2052 if (WARN_ON(i + 1 > depth)) {
2053 err = -EIO;
2054 break;
2056 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2057 depth - i - 1)) {
2058 err = -EIO;
2059 break;
2061 path[i + 1].p_bh = bh;
2063 /* save actual number of indexes since this
2064 * number is changed at the next iteration */
2065 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2066 i++;
2067 } else {
2068 /* we finished processing this index, go up */
2069 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
2070 /* index is empty, remove it;
2071 * handle must be already prepared by the
2072 * truncatei_leaf() */
2073 err = ext4_ext_rm_idx(handle, inode, path + i);
2075 /* root level has p_bh == NULL, brelse() eats this */
2076 brelse(path[i].p_bh);
2077 path[i].p_bh = NULL;
2078 i--;
2079 ext_debug("return to level %d\n", i);
2083 /* TODO: flexible tree reduction should be here */
2084 if (path->p_hdr->eh_entries == 0) {
2086 * truncate to zero freed all the tree,
2087 * so we need to correct eh_depth
2089 err = ext4_ext_get_access(handle, inode, path);
2090 if (err == 0) {
2091 ext_inode_hdr(inode)->eh_depth = 0;
2092 ext_inode_hdr(inode)->eh_max =
2093 cpu_to_le16(ext4_ext_space_root(inode));
2094 err = ext4_ext_dirty(handle, inode, path);
2097 out:
2098 ext4_ext_tree_changed(inode);
2099 ext4_ext_drop_refs(path);
2100 kfree(path);
2101 ext4_journal_stop(handle);
2103 return err;
2107 * called at mount time
2109 void ext4_ext_init(struct super_block *sb)
2112 * possible initialization would be here
2115 if (test_opt(sb, EXTENTS)) {
2116 printk("EXT4-fs: file extents enabled");
2117 #ifdef AGGRESSIVE_TEST
2118 printk(", aggressive tests");
2119 #endif
2120 #ifdef CHECK_BINSEARCH
2121 printk(", check binsearch");
2122 #endif
2123 #ifdef EXTENTS_STATS
2124 printk(", stats");
2125 #endif
2126 printk("\n");
2127 #ifdef EXTENTS_STATS
2128 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2129 EXT4_SB(sb)->s_ext_min = 1 << 30;
2130 EXT4_SB(sb)->s_ext_max = 0;
2131 #endif
2136 * called at umount time
2138 void ext4_ext_release(struct super_block *sb)
2140 if (!test_opt(sb, EXTENTS))
2141 return;
2143 #ifdef EXTENTS_STATS
2144 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2145 struct ext4_sb_info *sbi = EXT4_SB(sb);
2146 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2147 sbi->s_ext_blocks, sbi->s_ext_extents,
2148 sbi->s_ext_blocks / sbi->s_ext_extents);
2149 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2150 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2152 #endif
2156 * This function is called by ext4_ext_get_blocks() if someone tries to write
2157 * to an uninitialized extent. It may result in splitting the uninitialized
2158 * extent into multiple extents (upto three - one initialized and two
2159 * uninitialized).
2160 * There are three possibilities:
2161 * a> There is no split required: Entire extent should be initialized
2162 * b> Splits in two extents: Write is happening at either end of the extent
2163 * c> Splits in three extents: Somone is writing in middle of the extent
2165 static int ext4_ext_convert_to_initialized(handle_t *handle,
2166 struct inode *inode,
2167 struct ext4_ext_path *path,
2168 ext4_lblk_t iblock,
2169 unsigned long max_blocks)
2171 struct ext4_extent *ex, newex;
2172 struct ext4_extent *ex1 = NULL;
2173 struct ext4_extent *ex2 = NULL;
2174 struct ext4_extent *ex3 = NULL;
2175 struct ext4_extent_header *eh;
2176 ext4_lblk_t ee_block;
2177 unsigned int allocated, ee_len, depth;
2178 ext4_fsblk_t newblock;
2179 int err = 0;
2180 int ret = 0;
2182 depth = ext_depth(inode);
2183 eh = path[depth].p_hdr;
2184 ex = path[depth].p_ext;
2185 ee_block = le32_to_cpu(ex->ee_block);
2186 ee_len = ext4_ext_get_actual_len(ex);
2187 allocated = ee_len - (iblock - ee_block);
2188 newblock = iblock - ee_block + ext_pblock(ex);
2189 ex2 = ex;
2191 <<<<<<< HEAD:fs/ext4/extents.c
2192 =======
2193 err = ext4_ext_get_access(handle, inode, path + depth);
2194 if (err)
2195 goto out;
2197 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2198 /* ex1: ee_block to iblock - 1 : uninitialized */
2199 if (iblock > ee_block) {
2200 ex1 = ex;
2201 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2202 ext4_ext_mark_uninitialized(ex1);
2203 ex2 = &newex;
2206 * for sanity, update the length of the ex2 extent before
2207 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2208 * overlap of blocks.
2210 if (!ex1 && allocated > max_blocks)
2211 ex2->ee_len = cpu_to_le16(max_blocks);
2212 /* ex3: to ee_block + ee_len : uninitialised */
2213 if (allocated > max_blocks) {
2214 unsigned int newdepth;
2215 ex3 = &newex;
2216 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2217 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2218 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2219 ext4_ext_mark_uninitialized(ex3);
2220 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2221 if (err)
2222 goto out;
2224 * The depth, and hence eh & ex might change
2225 * as part of the insert above.
2227 newdepth = ext_depth(inode);
2228 if (newdepth != depth) {
2229 depth = newdepth;
2230 <<<<<<< HEAD:fs/ext4/extents.c
2231 path = ext4_ext_find_extent(inode, iblock, NULL);
2232 =======
2233 ext4_ext_drop_refs(path);
2234 path = ext4_ext_find_extent(inode, iblock, path);
2235 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2236 if (IS_ERR(path)) {
2237 err = PTR_ERR(path);
2238 <<<<<<< HEAD:fs/ext4/extents.c
2239 path = NULL;
2240 =======
2241 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2242 goto out;
2244 eh = path[depth].p_hdr;
2245 ex = path[depth].p_ext;
2246 if (ex2 != &newex)
2247 ex2 = ex;
2248 <<<<<<< HEAD:fs/ext4/extents.c
2249 =======
2251 err = ext4_ext_get_access(handle, inode, path + depth);
2252 if (err)
2253 goto out;
2254 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2256 allocated = max_blocks;
2259 * If there was a change of depth as part of the
2260 * insertion of ex3 above, we need to update the length
2261 * of the ex1 extent again here
2263 if (ex1 && ex1 != ex) {
2264 ex1 = ex;
2265 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2266 ext4_ext_mark_uninitialized(ex1);
2267 ex2 = &newex;
2269 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2270 ex2->ee_block = cpu_to_le32(iblock);
2271 ext4_ext_store_pblock(ex2, newblock);
2272 ex2->ee_len = cpu_to_le16(allocated);
2273 if (ex2 != ex)
2274 goto insert;
2275 <<<<<<< HEAD:fs/ext4/extents.c
2276 err = ext4_ext_get_access(handle, inode, path + depth);
2277 if (err)
2278 goto out;
2279 =======
2280 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2282 * New (initialized) extent starts from the first block
2283 * in the current extent. i.e., ex2 == ex
2284 * We have to see if it can be merged with the extent
2285 * on the left.
2287 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2289 * To merge left, pass "ex2 - 1" to try_to_merge(),
2290 * since it merges towards right _only_.
2292 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2293 if (ret) {
2294 err = ext4_ext_correct_indexes(handle, inode, path);
2295 if (err)
2296 goto out;
2297 depth = ext_depth(inode);
2298 ex2--;
2302 * Try to Merge towards right. This might be required
2303 * only when the whole extent is being written to.
2304 * i.e. ex2 == ex and ex3 == NULL.
2306 if (!ex3) {
2307 ret = ext4_ext_try_to_merge(inode, path, ex2);
2308 if (ret) {
2309 err = ext4_ext_correct_indexes(handle, inode, path);
2310 if (err)
2311 goto out;
2314 /* Mark modified extent as dirty */
2315 err = ext4_ext_dirty(handle, inode, path + depth);
2316 goto out;
2317 insert:
2318 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2319 out:
2320 return err ? err : allocated;
2324 <<<<<<< HEAD:fs/ext4/extents.c
2325 =======
2326 * Block allocation/map/preallocation routine for extents based files
2329 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2330 * Need to be called with
2331 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2332 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
2333 <<<<<<< HEAD:fs/ext4/extents.c
2334 =======
2336 * return > 0, number of of blocks already mapped/allocated
2337 * if create == 0 and these are pre-allocated blocks
2338 * buffer head is unmapped
2339 * otherwise blocks are mapped
2341 * return = 0, if plain look up failed (blocks have not been allocated)
2342 * buffer head is unmapped
2344 * return < 0, error case.
2345 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2347 int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
2348 ext4_lblk_t iblock,
2349 unsigned long max_blocks, struct buffer_head *bh_result,
2350 int create, int extend_disksize)
2352 struct ext4_ext_path *path = NULL;
2353 struct ext4_extent_header *eh;
2354 struct ext4_extent newex, *ex;
2355 ext4_fsblk_t goal, newblock;
2356 int err = 0, depth, ret;
2357 unsigned long allocated = 0;
2358 struct ext4_allocation_request ar;
2360 __clear_bit(BH_New, &bh_result->b_state);
2361 ext_debug("blocks %u/%lu requested for inode %u\n",
2362 iblock, max_blocks, inode->i_ino);
2364 /* check in cache */
2365 goal = ext4_ext_in_cache(inode, iblock, &newex);
2366 if (goal) {
2367 if (goal == EXT4_EXT_CACHE_GAP) {
2368 if (!create) {
2370 * block isn't allocated yet and
2371 * user doesn't want to allocate it
2373 goto out2;
2375 /* we should allocate requested block */
2376 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2377 /* block is already allocated */
2378 newblock = iblock
2379 - le32_to_cpu(newex.ee_block)
2380 + ext_pblock(&newex);
2381 /* number of remaining blocks in the extent */
2382 allocated = ext4_ext_get_actual_len(&newex) -
2383 (iblock - le32_to_cpu(newex.ee_block));
2384 goto out;
2385 } else {
2386 BUG();
2390 /* find extent for this block */
2391 path = ext4_ext_find_extent(inode, iblock, NULL);
2392 if (IS_ERR(path)) {
2393 err = PTR_ERR(path);
2394 path = NULL;
2395 goto out2;
2398 depth = ext_depth(inode);
2401 * consistent leaf must not be empty;
2402 * this situation is possible, though, _during_ tree modification;
2403 * this is why assert can't be put in ext4_ext_find_extent()
2405 BUG_ON(path[depth].p_ext == NULL && depth != 0);
2406 eh = path[depth].p_hdr;
2408 ex = path[depth].p_ext;
2409 if (ex) {
2410 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
2411 ext4_fsblk_t ee_start = ext_pblock(ex);
2412 unsigned short ee_len;
2415 * Uninitialized extents are treated as holes, except that
2416 * we split out initialized portions during a write.
2418 ee_len = ext4_ext_get_actual_len(ex);
2419 /* if found extent covers block, simply return it */
2420 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2421 newblock = iblock - ee_block + ee_start;
2422 /* number of remaining blocks in the extent */
2423 allocated = ee_len - (iblock - ee_block);
2424 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
2425 ee_block, ee_len, newblock);
2427 /* Do not put uninitialized extent in the cache */
2428 if (!ext4_ext_is_uninitialized(ex)) {
2429 ext4_ext_put_in_cache(inode, ee_block,
2430 ee_len, ee_start,
2431 EXT4_EXT_CACHE_EXTENT);
2432 goto out;
2434 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2435 goto out;
2436 if (!create)
2437 goto out2;
2439 ret = ext4_ext_convert_to_initialized(handle, inode,
2440 path, iblock,
2441 max_blocks);
2442 if (ret <= 0) {
2443 err = ret;
2444 goto out2;
2445 } else
2446 allocated = ret;
2447 goto outnew;
2452 * requested block isn't allocated yet;
2453 * we couldn't try to create block if create flag is zero
2455 if (!create) {
2457 * put just found gap into cache to speed up
2458 * subsequent requests
2460 ext4_ext_put_gap_in_cache(inode, path, iblock);
2461 goto out2;
2464 * Okay, we need to do block allocation. Lazily initialize the block
2465 * allocation info here if necessary.
2467 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2468 ext4_init_block_alloc_info(inode);
2470 /* find neighbour allocated blocks */
2471 ar.lleft = iblock;
2472 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2473 if (err)
2474 goto out2;
2475 ar.lright = iblock;
2476 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2477 if (err)
2478 goto out2;
2481 * See if request is beyond maximum number of blocks we can have in
2482 * a single extent. For an initialized extent this limit is
2483 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2484 * EXT_UNINIT_MAX_LEN.
2486 if (max_blocks > EXT_INIT_MAX_LEN &&
2487 create != EXT4_CREATE_UNINITIALIZED_EXT)
2488 max_blocks = EXT_INIT_MAX_LEN;
2489 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2490 create == EXT4_CREATE_UNINITIALIZED_EXT)
2491 max_blocks = EXT_UNINIT_MAX_LEN;
2493 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2494 newex.ee_block = cpu_to_le32(iblock);
2495 newex.ee_len = cpu_to_le16(max_blocks);
2496 err = ext4_ext_check_overlap(inode, &newex, path);
2497 if (err)
2498 allocated = ext4_ext_get_actual_len(&newex);
2499 else
2500 allocated = max_blocks;
2502 /* allocate new block */
2503 ar.inode = inode;
2504 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2505 ar.logical = iblock;
2506 ar.len = allocated;
2507 if (S_ISREG(inode->i_mode))
2508 ar.flags = EXT4_MB_HINT_DATA;
2509 else
2510 /* disable in-core preallocation for non-regular files */
2511 ar.flags = 0;
2512 newblock = ext4_mb_new_blocks(handle, &ar, &err);
2513 if (!newblock)
2514 goto out2;
2515 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
2516 goal, newblock, allocated);
2518 /* try to insert new extent into found leaf and return */
2519 ext4_ext_store_pblock(&newex, newblock);
2520 newex.ee_len = cpu_to_le16(ar.len);
2521 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2522 ext4_ext_mark_uninitialized(&newex);
2523 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2524 if (err) {
2525 /* free data blocks we just allocated */
2526 /* not a good idea to call discard here directly,
2527 * but otherwise we'd need to call it every free() */
2528 ext4_mb_discard_inode_preallocations(inode);
2529 ext4_free_blocks(handle, inode, ext_pblock(&newex),
2530 ext4_ext_get_actual_len(&newex), 0);
2531 goto out2;
2534 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2535 EXT4_I(inode)->i_disksize = inode->i_size;
2537 /* previous routine could use block we allocated */
2538 newblock = ext_pblock(&newex);
2539 allocated = ext4_ext_get_actual_len(&newex);
2540 outnew:
2541 __set_bit(BH_New, &bh_result->b_state);
2543 /* Cache only when it is _not_ an uninitialized extent */
2544 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2545 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2546 EXT4_EXT_CACHE_EXTENT);
2547 out:
2548 if (allocated > max_blocks)
2549 allocated = max_blocks;
2550 ext4_ext_show_leaf(inode, path);
2551 __set_bit(BH_Mapped, &bh_result->b_state);
2552 bh_result->b_bdev = inode->i_sb->s_bdev;
2553 bh_result->b_blocknr = newblock;
2554 out2:
2555 if (path) {
2556 ext4_ext_drop_refs(path);
2557 kfree(path);
2559 return err ? err : allocated;
2562 void ext4_ext_truncate(struct inode * inode, struct page *page)
2564 struct address_space *mapping = inode->i_mapping;
2565 struct super_block *sb = inode->i_sb;
2566 ext4_lblk_t last_block;
2567 handle_t *handle;
2568 int err = 0;
2571 * probably first extent we're gonna free will be last in block
2573 err = ext4_writepage_trans_blocks(inode) + 3;
2574 handle = ext4_journal_start(inode, err);
2575 if (IS_ERR(handle)) {
2576 if (page) {
2577 clear_highpage(page);
2578 flush_dcache_page(page);
2579 unlock_page(page);
2580 page_cache_release(page);
2582 return;
2585 if (page)
2586 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2588 down_write(&EXT4_I(inode)->i_data_sem);
2589 ext4_ext_invalidate_cache(inode);
2591 ext4_mb_discard_inode_preallocations(inode);
2594 * TODO: optimization is possible here.
2595 * Probably we need not scan at all,
2596 * because page truncation is enough.
2598 if (ext4_orphan_add(handle, inode))
2599 goto out_stop;
2601 /* we have to know where to truncate from in crash case */
2602 EXT4_I(inode)->i_disksize = inode->i_size;
2603 ext4_mark_inode_dirty(handle, inode);
2605 last_block = (inode->i_size + sb->s_blocksize - 1)
2606 >> EXT4_BLOCK_SIZE_BITS(sb);
2607 err = ext4_ext_remove_space(inode, last_block);
2609 /* In a multi-transaction truncate, we only make the final
2610 * transaction synchronous.
2612 if (IS_SYNC(inode))
2613 handle->h_sync = 1;
2615 out_stop:
2617 * If this was a simple ftruncate() and the file will remain alive,
2618 * then we need to clear up the orphan record which we created above.
2619 * However, if this was a real unlink then we were called by
2620 * ext4_delete_inode(), and we allow that function to clean up the
2621 * orphan info for us.
2623 if (inode->i_nlink)
2624 ext4_orphan_del(handle, inode);
2626 up_write(&EXT4_I(inode)->i_data_sem);
2627 ext4_journal_stop(handle);
2631 * ext4_ext_writepage_trans_blocks:
2632 * calculate max number of blocks we could modify
2633 * in order to allocate new block for an inode
2635 int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2637 int needed;
2639 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2641 /* caller wants to allocate num blocks, but note it includes sb */
2642 needed = needed * num - (num - 1);
2644 #ifdef CONFIG_QUOTA
2645 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2646 #endif
2648 return needed;
2652 * preallocate space for a file. This implements ext4's fallocate inode
2653 * operation, which gets called from sys_fallocate system call.
2654 * For block-mapped files, posix_fallocate should fall back to the method
2655 * of writing zeroes to the required new blocks (the same behavior which is
2656 * expected for file systems which do not support fallocate() system call).
2658 long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2660 handle_t *handle;
2661 ext4_lblk_t block;
2662 unsigned long max_blocks;
2663 ext4_fsblk_t nblocks = 0;
2664 int ret = 0;
2665 int ret2 = 0;
2666 int retries = 0;
2667 struct buffer_head map_bh;
2668 unsigned int credits, blkbits = inode->i_blkbits;
2671 * currently supporting (pre)allocate mode for extent-based
2672 * files _only_
2674 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2675 return -EOPNOTSUPP;
2677 /* preallocation to directories is currently not supported */
2678 if (S_ISDIR(inode->i_mode))
2679 return -ENODEV;
2681 block = offset >> blkbits;
2682 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2683 - block;
2686 * credits to insert 1 extent into extent tree + buffers to be able to
2687 * modify 1 super block, 1 block bitmap and 1 group descriptor.
2689 credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
2690 <<<<<<< HEAD:fs/ext4/extents.c
2691 down_write((&EXT4_I(inode)->i_data_sem));
2692 =======
2693 mutex_lock(&inode->i_mutex);
2694 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2695 retry:
2696 while (ret >= 0 && ret < max_blocks) {
2697 block = block + ret;
2698 max_blocks = max_blocks - ret;
2699 handle = ext4_journal_start(inode, credits);
2700 if (IS_ERR(handle)) {
2701 ret = PTR_ERR(handle);
2702 break;
2705 <<<<<<< HEAD:fs/ext4/extents.c
2706 ret = ext4_ext_get_blocks(handle, inode, block,
2707 =======
2708 ret = ext4_get_blocks_wrap(handle, inode, block,
2709 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2710 max_blocks, &map_bh,
2711 EXT4_CREATE_UNINITIALIZED_EXT, 0);
2712 <<<<<<< HEAD:fs/ext4/extents.c
2713 WARN_ON(ret <= 0);
2714 =======
2715 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2716 if (ret <= 0) {
2717 <<<<<<< HEAD:fs/ext4/extents.c
2718 ext4_error(inode->i_sb, "ext4_fallocate",
2719 "ext4_ext_get_blocks returned error: "
2720 "inode#%lu, block=%u, max_blocks=%lu",
2721 =======
2722 #ifdef EXT4FS_DEBUG
2723 WARN_ON(ret <= 0);
2724 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2725 "returned error inode#%lu, block=%u, "
2726 "max_blocks=%lu", __func__,
2727 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2728 inode->i_ino, block, max_blocks);
2729 <<<<<<< HEAD:fs/ext4/extents.c
2730 ret = -EIO;
2731 =======
2732 #endif
2733 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2734 ext4_mark_inode_dirty(handle, inode);
2735 ret2 = ext4_journal_stop(handle);
2736 break;
2738 if (ret > 0) {
2739 /* check wrap through sign-bit/zero here */
2740 if ((block + ret) < 0 || (block + ret) < block) {
2741 ret = -EIO;
2742 ext4_mark_inode_dirty(handle, inode);
2743 ret2 = ext4_journal_stop(handle);
2744 break;
2746 if (buffer_new(&map_bh) && ((block + ret) >
2747 (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
2748 >> blkbits)))
2749 nblocks = nblocks + ret;
2752 /* Update ctime if new blocks get allocated */
2753 if (nblocks) {
2754 struct timespec now;
2756 now = current_fs_time(inode->i_sb);
2757 if (!timespec_equal(&inode->i_ctime, &now))
2758 inode->i_ctime = now;
2761 ext4_mark_inode_dirty(handle, inode);
2762 ret2 = ext4_journal_stop(handle);
2763 if (ret2)
2764 break;
2767 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2768 goto retry;
2770 <<<<<<< HEAD:fs/ext4/extents.c
2771 up_write((&EXT4_I(inode)->i_data_sem));
2772 =======
2773 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2775 * Time to update the file size.
2776 * Update only when preallocation was requested beyond the file size.
2778 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2779 (offset + len) > i_size_read(inode)) {
2780 if (ret > 0) {
2782 * if no error, we assume preallocation succeeded
2783 * completely
2785 <<<<<<< HEAD:fs/ext4/extents.c
2786 mutex_lock(&inode->i_mutex);
2787 =======
2788 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2789 i_size_write(inode, offset + len);
2790 EXT4_I(inode)->i_disksize = i_size_read(inode);
2791 <<<<<<< HEAD:fs/ext4/extents.c
2792 mutex_unlock(&inode->i_mutex);
2793 =======
2794 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2795 } else if (ret < 0 && nblocks) {
2796 /* Handle partial allocation scenario */
2797 loff_t newsize;
2799 <<<<<<< HEAD:fs/ext4/extents.c
2800 mutex_lock(&inode->i_mutex);
2801 =======
2802 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2803 newsize = (nblocks << blkbits) + i_size_read(inode);
2804 i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
2805 EXT4_I(inode)->i_disksize = i_size_read(inode);
2806 <<<<<<< HEAD:fs/ext4/extents.c
2807 mutex_unlock(&inode->i_mutex);
2808 =======
2809 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2813 <<<<<<< HEAD:fs/ext4/extents.c
2814 =======
2815 mutex_unlock(&inode->i_mutex);
2816 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/ext4/extents.c
2817 return ret > 0 ? ret2 : ret;