2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Zheng Liu <lz@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/types.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/vnode.h>
43 #include <fs/ext2fs/ext2_mount.h>
44 #include <fs/ext2fs/fs.h>
45 #include <fs/ext2fs/inode.h>
46 #include <fs/ext2fs/ext2fs.h>
47 #include <fs/ext2fs/ext2_extents.h>
48 #include <fs/ext2fs/ext2_extern.h>
50 SDT_PROVIDER_DECLARE(ext2fs);
53 * arg0: verbosity. Higher numbers give more verbose messages
54 * arg1: Textual message
56 SDT_PROBE_DEFINE2(ext2fs, , trace, extents, "int", "char*");
58 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
60 #ifdef EXT2FS_PRINT_EXTENTS
62 ext4_ext_print_extent(struct ext4_extent *ep)
65 printf(" ext %p => (blk %u len %u start %ju)\n",
66 ep, ep->e_blk, ep->e_len,
67 (uint64_t)ep->e_start_hi << 32 | ep->e_start_lo);
70 static void ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp);
73 ext4_ext_print_index(struct inode *ip, struct ext4_extent_index *ex, int do_walk)
81 printf(" index %p => (blk %u pblk %ju)\n",
82 ex, ex->ei_blk, (uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo);
87 if ((error = bread(ip->i_devvp,
88 fsbtodb(fs, ((uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo)),
89 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
94 ext4_ext_print_header(ip, (struct ext4_extent_header *)bp->b_data);
101 ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp)
105 printf("header %p => (magic 0x%x entries %d max %d depth %d gen %d)\n",
106 ehp, ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
109 for (i = 0; i < ehp->eh_ecount; i++)
110 if (ehp->eh_depth != 0)
111 ext4_ext_print_index(ip,
112 (struct ext4_extent_index *)(ehp + 1 + i), 1);
114 ext4_ext_print_extent((struct ext4_extent *)(ehp + 1 + i));
118 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
124 printf("ip=%ju, Path:\n", ip->i_number);
125 for (k = 0; k <= l; k++, path++) {
126 if (path->ep_index) {
127 ext4_ext_print_index(ip, path->ep_index, 0);
128 } else if (path->ep_ext) {
129 ext4_ext_print_extent(path->ep_ext);
135 ext4_ext_print_extent_tree_status(struct inode *ip)
137 struct ext4_extent_header *ehp;
139 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
141 printf("Extent status:ip=%ju\n", ip->i_number);
142 if (!(ip->i_flag & IN_E4EXTENTS))
145 ext4_ext_print_header(ip, ehp);
151 static inline struct ext4_extent_header *
152 ext4_ext_inode_header(struct inode *ip)
155 return ((struct ext4_extent_header *)ip->i_db);
158 static inline struct ext4_extent_header *
159 ext4_ext_block_header(char *bdata)
162 return ((struct ext4_extent_header *)bdata);
165 static inline unsigned short
166 ext4_ext_inode_depth(struct inode *ip)
168 struct ext4_extent_header *ehp;
170 ehp = (struct ext4_extent_header *)ip->i_data;
171 return (ehp->eh_depth);
174 static inline e4fs_daddr_t
175 ext4_ext_index_pblock(struct ext4_extent_index *index)
179 blk = index->ei_leaf_lo;
180 blk |= (e4fs_daddr_t)index->ei_leaf_hi << 32;
186 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
189 index->ei_leaf_lo = pb & 0xffffffff;
190 index->ei_leaf_hi = (pb >> 32) & 0xffff;
194 static inline e4fs_daddr_t
195 ext4_ext_extent_pblock(struct ext4_extent *extent)
199 blk = extent->e_start_lo;
200 blk |= (e4fs_daddr_t)extent->e_start_hi << 32;
206 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
209 ex->e_start_lo = pb & 0xffffffff;
210 ex->e_start_hi = (pb >> 32) & 0xffff;
214 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
216 struct ext4_extent_cache *ecp;
217 int ret = EXT4_EXT_CACHE_NO;
219 ecp = &ip->i_ext_cache;
220 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
223 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
224 ep->e_blk = ecp->ec_blk;
225 ep->e_start_lo = ecp->ec_start & 0xffffffff;
226 ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
227 ep->e_len = ecp->ec_len;
234 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
241 if (eh->eh_magic != EXT4_EXT_MAGIC) {
242 error_msg = "header: invalid magic";
245 if (eh->eh_max == 0) {
246 error_msg = "header: invalid eh_max";
249 if (eh->eh_ecount > eh->eh_max) {
250 error_msg = "header: invalid eh_entries";
257 SDT_PROBE2(ext2fs, , trace, extents, 1, error_msg);
262 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
264 struct ext4_extent_header *eh;
265 struct ext4_extent_index *r, *l, *m;
267 eh = path->ep_header;
269 KASSERT(eh->eh_ecount <= eh->eh_max && eh->eh_ecount > 0,
270 ("ext4_ext_binsearch_index: bad args"));
272 l = EXT_FIRST_INDEX(eh) + 1;
273 r = EXT_FIRST_INDEX(eh) + eh->eh_ecount - 1;
282 path->ep_index = l - 1;
286 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
288 struct ext4_extent_header *eh;
289 struct ext4_extent *r, *l, *m;
291 eh = path->ep_header;
293 KASSERT(eh->eh_ecount <= eh->eh_max,
294 ("ext4_ext_binsearch_ext: bad args"));
296 if (eh->eh_ecount == 0)
299 l = EXT_FIRST_EXTENT(eh) + 1;
300 r = EXT_FIRST_EXTENT(eh) + eh->eh_ecount - 1;
310 path->ep_ext = l - 1;
314 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
315 struct buf *bp, uint64_t blk)
318 KASSERT(path->ep_data == NULL,
319 ("ext4_ext_fill_path_bdata: bad ep_data"));
321 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
325 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
332 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
335 KASSERT(path->ep_data != NULL,
336 ("ext4_ext_fill_path_buf: bad ep_data"));
338 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
342 ext4_ext_drop_refs(struct ext4_extent_path *path)
349 depth = path->ep_depth;
350 for (i = 0; i <= depth; i++, path++)
352 free(path->ep_data, M_EXT2EXTENTS);
353 path->ep_data = NULL;
358 ext4_ext_path_free(struct ext4_extent_path *path)
364 ext4_ext_drop_refs(path);
365 free(path, M_EXT2EXTENTS);
369 ext4_ext_find_extent(struct inode *ip, daddr_t block,
370 struct ext4_extent_path **ppath)
373 struct ext4_extent_header *eh;
374 struct ext4_extent_path *path;
377 int error, depth, i, ppos, alloc;
380 eh = ext4_ext_inode_header(ip);
381 depth = ext4_ext_inode_depth(ip);
385 error = ext4_ext_check_header(ip, eh);
394 path = malloc(EXT4_EXT_DEPTH_MAX *
395 sizeof(struct ext4_extent_path),
396 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
404 path[0].ep_header = eh;
405 path[0].ep_data = NULL;
407 /* Walk through the tree. */
410 ext4_ext_binsearch_index(&path[ppos], block);
411 blk = ext4_ext_index_pblock(path[ppos].ep_index);
412 path[ppos].ep_depth = i;
413 path[ppos].ep_ext = NULL;
415 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
416 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
424 SDT_PROBE2(ext2fs, , trace, extents, 1,
425 "ppos > depth => extent corrupted");
431 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
434 eh = ext4_ext_block_header(path[ppos].ep_data);
435 if (ext4_ext_check_header(ip, eh) ||
436 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
441 path[ppos].ep_header = eh;
446 error = ext4_ext_check_header(ip, eh);
451 path[ppos].ep_depth = i;
452 path[ppos].ep_header = eh;
453 path[ppos].ep_ext = NULL;
454 path[ppos].ep_index = NULL;
455 ext4_ext_binsearch_ext(&path[ppos], block);
459 ext4_ext_drop_refs(path);
461 free(path, M_EXT2EXTENTS);
469 ext4_ext_space_root(struct inode *ip)
473 size = sizeof(ip->i_data);
474 size -= sizeof(struct ext4_extent_header);
475 size /= sizeof(struct ext4_extent);
481 ext4_ext_space_block(struct inode *ip)
488 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
489 sizeof(struct ext4_extent);
495 ext4_ext_space_block_index(struct inode *ip)
502 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
503 sizeof(struct ext4_extent_index);
509 ext4_ext_tree_init(struct inode *ip)
511 struct ext4_extent_header *ehp;
513 ip->i_flag |= IN_E4EXTENTS;
515 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
516 ehp = (struct ext4_extent_header *)ip->i_data;
517 ehp->eh_magic = EXT4_EXT_MAGIC;
518 ehp->eh_max = ext4_ext_space_root(ip);
519 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
520 ip->i_flag |= IN_CHANGE | IN_UPDATE;
521 ext2_update(ip->i_vnode, 1);
525 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
526 uint32_t len, uint32_t start, int type)
529 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
531 ip->i_ext_cache.ec_type = type;
532 ip->i_ext_cache.ec_blk = blk;
533 ip->i_ext_cache.ec_len = len;
534 ip->i_ext_cache.ec_start = start;
538 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
542 struct ext4_extent *ex;
543 e4fs_daddr_t bg_start;
549 depth = path->ep_depth;
550 ex = path[depth].ep_ext;
552 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
553 e2fs_daddr_t blk = ex->e_blk;
556 return (pblk + (block - blk));
558 return (pblk - (blk - block));
561 /* Try to get block from index itself. */
562 if (path[depth].ep_data)
563 return (path[depth].ep_blk);
566 /* Use inode's group. */
567 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
568 fs->e2fs->e2fs_first_dblock;
570 return (bg_start + block);
574 ext4_can_extents_be_merged(struct ext4_extent *ex1,
575 struct ext4_extent *ex2)
578 if (ex1->e_blk + ex1->e_len != ex2->e_blk)
581 if (ex1->e_len + ex2->e_len > EXT4_MAX_LEN)
584 if (ext4_ext_extent_pblock(ex1) + ex1->e_len ==
585 ext4_ext_extent_pblock(ex2))
592 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
594 int depth = path->ep_depth;
598 return (EXT4_MAX_BLOCKS);
604 if (path[depth].ep_index !=
605 EXT_LAST_INDEX(path[depth].ep_header))
606 return (path[depth].ep_index[1].ei_blk);
611 return (EXT4_MAX_BLOCKS);
615 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
629 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
630 fs->e2fs_bsize, 0, 0, 0);
633 ext4_ext_fill_path_buf(path, bp);
634 ext2_extent_blk_csum_set(ip, bp->b_data);
637 ip->i_flag |= IN_CHANGE | IN_UPDATE;
638 error = ext2_update(ip->i_vnode, 1);
645 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
646 uint32_t lblk, e4fs_daddr_t blk)
649 struct ext4_extent_index *idx;
654 if (lblk == path->ep_index->ei_blk) {
655 SDT_PROBE2(ext2fs, , trace, extents, 1,
656 "lblk == index blk => extent corrupted");
660 if (path->ep_header->eh_ecount >= path->ep_header->eh_max) {
661 SDT_PROBE2(ext2fs, , trace, extents, 1,
662 "ecout > maxcount => extent corrupted");
666 if (lblk > path->ep_index->ei_blk) {
668 idx = path->ep_index + 1;
671 idx = path->ep_index;
674 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
676 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
678 if (idx > EXT_MAX_INDEX(path->ep_header)) {
679 SDT_PROBE2(ext2fs, , trace, extents, 1,
680 "index is out of range => extent corrupted");
685 ext4_index_store_pblock(idx, blk);
686 path->ep_header->eh_ecount++;
688 return (ext4_ext_dirty(ip, path));
692 ext4_ext_alloc_meta(struct inode *ip)
694 e4fs_daddr_t blk = ext2_alloc_meta(ip);
696 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
697 ip->i_flag |= IN_CHANGE | IN_UPDATE;
698 ext2_update(ip->i_vnode, 1);
705 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
708 int i, blocksreleased;
711 blocksreleased = count;
713 for(i = 0; i < count; i++)
714 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
716 if (ip->i_blocks >= blocksreleased)
717 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
721 ip->i_flag |= IN_CHANGE | IN_UPDATE;
722 ext2_update(ip->i_vnode, 1);
726 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
727 struct ext4_extent *newext, int at)
731 int depth = ext4_ext_inode_depth(ip);
732 struct ext4_extent_header *neh;
733 struct ext4_extent_index *fidx;
734 struct ext4_extent *ex;
736 e4fs_daddr_t newblk, oldblk;
738 e4fs_daddr_t *ablks = NULL;
745 * We will split at current extent for now.
747 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
748 SDT_PROBE2(ext2fs, , trace, extents, 1,
749 "extent is out of range => extent corrupted");
753 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
754 border = path[depth].ep_ext[1].e_blk;
756 border = newext->e_blk;
758 /* Allocate new blocks. */
759 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
760 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
763 for (a = 0; a < depth - at; a++) {
764 newblk = ext4_ext_alloc_meta(ip);
771 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
777 neh = ext4_ext_block_header(bp->b_data);
779 neh->eh_max = ext4_ext_space_block(ip);
780 neh->eh_magic = EXT4_EXT_MAGIC;
782 ex = EXT_FIRST_EXTENT(neh);
784 if (path[depth].ep_header->eh_ecount != path[depth].ep_header->eh_max) {
785 SDT_PROBE2(ext2fs, , trace, extents, 1,
786 "extents count out of range => extent corrupted");
791 /* Start copy from next extent. */
793 path[depth].ep_ext++;
794 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
795 path[depth].ep_ext++;
799 memmove(ex, path[depth].ep_ext - m,
800 sizeof(struct ext4_extent) * m);
801 neh->eh_ecount = neh->eh_ecount + m;
804 ext2_extent_blk_csum_set(ip, bp->b_data);
810 path[depth].ep_header->eh_ecount =
811 path[depth].ep_header->eh_ecount - m;
812 ext4_ext_dirty(ip, path + depth);
815 /* Create intermediate indexes. */
817 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
819 /* Insert new index into current index block. */
824 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
825 (int)fs->e2fs_bsize, NOCRED, &bp);
831 neh = (struct ext4_extent_header *)bp->b_data;
833 neh->eh_magic = EXT4_EXT_MAGIC;
834 neh->eh_max = ext4_ext_space_block_index(ip);
835 neh->eh_depth = depth - i;
836 fidx = EXT_FIRST_INDEX(neh);
837 fidx->ei_blk = border;
838 ext4_index_store_pblock(fidx, oldblk);
842 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
847 memmove(++fidx, path[i].ep_index - m,
848 sizeof(struct ext4_extent_index) * m);
849 neh->eh_ecount = neh->eh_ecount + m;
852 ext2_extent_blk_csum_set(ip, bp->b_data);
858 path[i].ep_header->eh_ecount =
859 path[i].ep_header->eh_ecount - m;
860 ext4_ext_dirty(ip, path + i);
866 error = ext4_ext_insert_index(ip, path + at, border, newblk);
873 for (i = 0; i < depth; i++) {
876 ext4_ext_blkfree(ip, ablks[i], 1, 0);
880 free(ablks, M_EXT2EXTENTS);
886 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
887 struct ext4_extent *newext)
890 struct ext4_extent_path *curpath;
891 struct ext4_extent_header *neh;
899 newblk = ext4_ext_alloc_meta(ip);
903 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
907 /* Move top-level index/leaf into new block. */
908 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
910 /* Set size of new block */
911 neh = ext4_ext_block_header(bp->b_data);
912 neh->eh_magic = EXT4_EXT_MAGIC;
914 if (ext4_ext_inode_depth(ip))
915 neh->eh_max = ext4_ext_space_block_index(ip);
917 neh->eh_max = ext4_ext_space_block(ip);
919 ext2_extent_blk_csum_set(ip, bp->b_data);
926 curpath->ep_header->eh_magic = EXT4_EXT_MAGIC;
927 curpath->ep_header->eh_max = ext4_ext_space_root(ip);
928 curpath->ep_header->eh_ecount = 1;
929 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
930 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
931 ext4_index_store_pblock(curpath->ep_index, newblk);
933 neh = ext4_ext_inode_header(ip);
934 neh->eh_depth = path->ep_depth + 1;
935 ext4_ext_dirty(ip, curpath);
943 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
944 struct ext4_extent *newext)
946 struct ext4_extent_path *curpath;
950 i = depth = ext4_ext_inode_depth(ip);
952 /* Look for free index entry int the tree */
953 curpath = path + depth;
954 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
960 * We use already allocated block for index block,
961 * so subsequent data blocks should be contiguous.
963 if (EXT_HAS_FREE_INDEX(curpath)) {
964 error = ext4_ext_split(ip, path, newext, i);
969 ext4_ext_drop_refs(path);
970 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
974 /* Tree is full, do grow in depth. */
975 error = ext4_ext_grow_indepth(ip, path, newext);
980 ext4_ext_drop_refs(path);
981 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
985 /* Check and split tree if required. */
986 depth = ext4_ext_inode_depth(ip);
987 if (path[depth].ep_header->eh_ecount ==
988 path[depth].ep_header->eh_max)
997 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
999 struct ext4_extent_header *eh;
1000 struct ext4_extent *ex;
1004 depth = ext4_ext_inode_depth(ip);
1005 eh = path[depth].ep_header;
1006 ex = path[depth].ep_ext;
1008 if (ex == NULL || eh == NULL)
1014 /* We will correct tree if first leaf got modified only. */
1015 if (ex != EXT_FIRST_EXTENT(eh))
1019 border = path[depth].ep_ext->e_blk;
1020 path[k].ep_index->ei_blk = border;
1021 ext4_ext_dirty(ip, path + k);
1023 /* Change all left-side indexes. */
1024 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1027 path[k].ep_index->ei_blk = border;
1028 ext4_ext_dirty(ip, path + k);
1035 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1036 struct ext4_extent *newext)
1038 struct ext4_extent_header * eh;
1039 struct ext4_extent *ex, *nex, *nearex;
1040 struct ext4_extent_path *npath;
1041 int depth, len, error, next;
1043 depth = ext4_ext_inode_depth(ip);
1044 ex = path[depth].ep_ext;
1047 if (newext->e_len == 0 || path[depth].ep_header == NULL)
1050 /* Insert block into found extent. */
1051 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1052 ex->e_len = ex->e_len + newext->e_len;
1053 eh = path[depth].ep_header;
1059 depth = ext4_ext_inode_depth(ip);
1060 eh = path[depth].ep_header;
1061 if (eh->eh_ecount < eh->eh_max)
1065 nex = EXT_LAST_EXTENT(eh);
1066 next = ext4_ext_next_leaf_block(ip, path);
1067 if (newext->e_blk > nex->e_blk && next != EXT4_MAX_BLOCKS) {
1068 KASSERT(npath == NULL,
1069 ("ext4_ext_insert_extent: bad path"));
1071 error = ext4_ext_find_extent(ip, next, &npath);
1075 if (npath->ep_depth != path->ep_depth) {
1080 eh = npath[depth].ep_header;
1081 if (eh->eh_ecount < eh->eh_max) {
1088 * There is no free space in the found leaf,
1089 * try to add a new leaf to the tree.
1091 error = ext4_ext_create_new_leaf(ip, path, newext);
1095 depth = ext4_ext_inode_depth(ip);
1096 eh = path[depth].ep_header;
1099 nearex = path[depth].ep_ext;
1101 /* Create new extent in the leaf. */
1102 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1103 } else if (newext->e_blk > nearex->e_blk) {
1104 if (nearex != EXT_LAST_EXTENT(eh)) {
1105 len = EXT_MAX_EXTENT(eh) - nearex;
1106 len = (len - 1) * sizeof(struct ext4_extent);
1107 len = len < 0 ? 0 : len;
1108 memmove(nearex + 2, nearex + 1, len);
1110 path[depth].ep_ext = nearex + 1;
1112 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1113 len = len < 0 ? 0 : len;
1114 memmove(nearex + 1, nearex, len);
1115 path[depth].ep_ext = nearex;
1118 eh->eh_ecount = eh->eh_ecount + 1;
1119 nearex = path[depth].ep_ext;
1120 nearex->e_blk = newext->e_blk;
1121 nearex->e_start_lo = newext->e_start_lo;
1122 nearex->e_start_hi = newext->e_start_hi;
1123 nearex->e_len = newext->e_len;
1126 /* Try to merge extents to the right. */
1127 while (nearex < EXT_LAST_EXTENT(eh)) {
1128 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1131 /* Merge with next extent. */
1132 nearex->e_len = nearex->e_len + nearex[1].e_len;
1133 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1134 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1135 sizeof(struct ext4_extent);
1136 memmove(nearex + 1, nearex + 2, len);
1139 eh->eh_ecount = eh->eh_ecount - 1;
1140 KASSERT(eh->eh_ecount != 0,
1141 ("ext4_ext_insert_extent: bad ecount"));
1145 * Try to merge extents to the left,
1146 * start from inexes correction.
1148 error = ext4_ext_correct_indexes(ip, path);
1152 ext4_ext_dirty(ip, path + depth);
1156 ext4_ext_drop_refs(npath);
1157 free(npath, M_EXT2EXTENTS);
1160 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1165 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1166 struct ucred *cred, unsigned long *count, int *perror)
1168 struct m_ext2fs *fs;
1169 e4fs_daddr_t newblk;
1172 * We will allocate only single block for now.
1178 EXT2_LOCK(ip->i_ump);
1179 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1184 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1185 ext2_update(ip->i_vnode, 1);
1192 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1193 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1194 int *pallocated, daddr_t *nb)
1196 struct m_ext2fs *fs;
1197 struct buf *bp = NULL;
1198 struct ext4_extent_path *path;
1199 struct ext4_extent newex, *ex;
1200 e4fs_daddr_t bpref, newblk = 0;
1201 unsigned long allocated = 0;
1202 int error = 0, depth;
1210 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1211 if (bpref == EXT4_EXT_CACHE_IN) {
1212 /* Block is already allocated. */
1213 newblk = iblk - newex.e_blk +
1214 ext4_ext_extent_pblock(&newex);
1215 allocated = newex.e_len - (iblk - newex.e_blk);
1223 error = ext4_ext_find_extent(ip, iblk, &path);
1228 depth = ext4_ext_inode_depth(ip);
1229 if (path[depth].ep_ext == NULL && depth != 0) {
1234 if ((ex = path[depth].ep_ext)) {
1235 uint64_t lblk = ex->e_blk;
1236 uint16_t e_len = ex->e_len;
1237 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1239 if (e_len > EXT4_MAX_LEN)
1242 /* If we found extent covers block, simply return it. */
1243 if (iblk >= lblk && iblk < lblk + e_len) {
1244 newblk = iblk - lblk + e_start;
1245 allocated = e_len - (iblk - lblk);
1246 ext4_ext_put_in_cache(ip, lblk, e_len,
1247 e_start, EXT4_EXT_CACHE_IN);
1252 /* Allocate the new block. */
1253 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1254 ip->i_next_alloc_goal = 0;
1257 bpref = ext4_ext_blkpref(ip, path, iblk);
1258 allocated = max_blocks;
1259 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1263 /* Try to insert new extent into found leaf and return. */
1265 ext4_ext_store_pblock(&newex, newblk);
1266 newex.e_len = allocated;
1267 error = ext4_ext_insert_extent(ip, path, &newex);
1271 newblk = ext4_ext_extent_pblock(&newex);
1272 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1276 if (allocated > max_blocks)
1277 allocated = max_blocks;
1282 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1283 fs->e2fs_bsize, cred, &bp);
1293 ext4_ext_drop_refs(path);
1294 free(path, M_EXT2EXTENTS);
1303 static inline uint16_t
1304 ext4_ext_get_actual_len(struct ext4_extent *ext)
1307 return (ext->e_len <= EXT_INIT_MAX_LEN ?
1308 ext->e_len : (ext->e_len - EXT_INIT_MAX_LEN));
1311 static inline struct ext4_extent_header *
1312 ext4_ext_header(struct inode *ip)
1315 return ((struct ext4_extent_header *)ip->i_db);
1319 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1320 unsigned long from, unsigned long to)
1322 unsigned long num, start;
1324 if (from >= ex->e_blk &&
1325 to == ex->e_blk + ext4_ext_get_actual_len(ex) - 1) {
1327 num = ex->e_blk + ext4_ext_get_actual_len(ex) - from;
1328 start = ext4_ext_extent_pblock(ex) +
1329 ext4_ext_get_actual_len(ex) - num;
1330 ext4_ext_blkfree(ip, start, num, 0);
1337 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1341 /* Free index block. */
1343 leaf = ext4_ext_index_pblock(path->ep_index);
1344 KASSERT(path->ep_header->eh_ecount != 0,
1345 ("ext4_ext_rm_index: bad ecount"));
1346 path->ep_header->eh_ecount--;
1347 ext4_ext_dirty(ip, path);
1348 ext4_ext_blkfree(ip, leaf, 1, 0);
1353 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1356 struct ext4_extent_header *eh;
1357 struct ext4_extent *ex;
1358 unsigned int a, b, block, num;
1359 unsigned long ex_blk;
1360 unsigned short ex_len;
1362 int error, correct_index;
1364 depth = ext4_ext_inode_depth(ip);
1365 if (!path[depth].ep_header) {
1366 if (path[depth].ep_data == NULL)
1368 path[depth].ep_header =
1369 (struct ext4_extent_header* )path[depth].ep_data;
1372 eh = path[depth].ep_header;
1374 SDT_PROBE2(ext2fs, , trace, extents, 1,
1375 "bad header => extent corrupted");
1379 ex = EXT_LAST_EXTENT(eh);
1381 ex_len = ext4_ext_get_actual_len(ex);
1385 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1386 path[depth].ep_ext = ex;
1387 a = ex_blk > start ? ex_blk : start;
1388 b = (uint64_t)ex_blk + ex_len - 1 <
1389 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1391 if (a != ex_blk && b != ex_blk + ex_len - 1)
1393 else if (a != ex_blk) {
1394 /* Remove tail of the extent. */
1397 } else if (b != ex_blk + ex_len - 1) {
1398 /* Remove head of the extent, not implemented. */
1401 /* Remove whole extent. */
1406 if (ex == EXT_FIRST_EXTENT(eh))
1409 error = ext4_remove_blocks(ip, ex, a, b);
1414 ext4_ext_store_pblock(ex, 0);
1421 ext4_ext_dirty(ip, path + depth);
1425 ex_len = ext4_ext_get_actual_len(ex);
1428 if (correct_index && eh->eh_ecount)
1429 error = ext4_ext_correct_indexes(ip, path);
1432 * If this leaf is free, we should
1433 * remove it from index block above.
1435 if (error == 0 && eh->eh_ecount == 0 && path[depth].ep_data != NULL)
1436 error = ext4_ext_rm_index(ip, path + depth);
1443 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1444 int depth, int flags)
1446 struct m_ext2fs *fs;
1447 struct ext4_extent_header *eh;
1452 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1453 fs->e2fs_bsize, NOCRED, &bp);
1459 eh = ext4_ext_block_header(bp->b_data);
1460 if (eh->eh_depth != depth) {
1461 SDT_PROBE2(ext2fs, , trace, extents, 1,
1462 "unexpected eh_depth");
1466 error = ext4_ext_check_header(ip, eh);
1479 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1482 KASSERT(path->ep_index != NULL,
1483 ("ext4_ext_more_to_rm: bad index from path"));
1485 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1488 if (path->ep_header->eh_ecount == path->index_count)
1495 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1496 struct ucred *cred, struct thread *td)
1499 struct ext4_extent_header *ehp;
1500 struct ext4_extent_path *path;
1504 ehp = (struct ext4_extent_header *)ip->i_db;
1505 depth = ext4_ext_inode_depth(ip);
1507 error = ext4_ext_check_header(ip, ehp);
1511 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1512 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1516 path[0].ep_header = ehp;
1517 path[0].ep_depth = depth;
1519 while (error == 0 && i >= 0) {
1522 error = ext4_ext_rm_leaf(ip, path, length);
1525 free(path[i].ep_data, M_EXT2EXTENTS);
1526 path[i].ep_data = NULL;
1531 /* This is index. */
1532 if (!path[i].ep_header)
1534 (struct ext4_extent_header *)path[i].ep_data;
1536 if (!path[i].ep_index) {
1537 /* This level hasn't touched yet. */
1538 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1539 path[i].index_count = path[i].ep_header->eh_ecount + 1;
1541 /* We've already was here, see at next index. */
1545 if (ext4_ext_more_to_rm(path + i)) {
1546 memset(path + i + 1, 0, sizeof(*path));
1547 bp = ext4_read_extent_tree_block(ip,
1548 ext4_ext_index_pblock(path[i].ep_index),
1549 path[0].ep_depth - (i + 1), 0);
1555 ext4_ext_fill_path_bdata(&path[i+1], bp,
1556 ext4_ext_index_pblock(path[i].ep_index));
1558 path[i].index_count = path[i].ep_header->eh_ecount;
1561 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1562 /* Index is empty, remove it. */
1563 error = ext4_ext_rm_index(ip, path + i);
1565 free(path[i].ep_data, M_EXT2EXTENTS);
1566 path[i].ep_data = NULL;
1571 if (path->ep_header->eh_ecount == 0) {
1573 * Truncate the tree to zero.
1575 ext4_ext_header(ip)->eh_depth = 0;
1576 ext4_ext_header(ip)->eh_max = ext4_ext_space_root(ip);
1577 ext4_ext_dirty(ip, path);
1580 ext4_ext_drop_refs(path);
1581 free(path, M_EXT2EXTENTS);