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>
42 #include <fs/ext2fs/ext2_mount.h>
43 #include <fs/ext2fs/fs.h>
44 #include <fs/ext2fs/inode.h>
45 #include <fs/ext2fs/ext2fs.h>
46 #include <fs/ext2fs/ext2_extents.h>
47 #include <fs/ext2fs/ext2_extern.h>
49 static MALLOC_DEFINE(M_EXT2EXTENTS, "ext2_extents", "EXT2 extents");
53 ext4_ext_print_extent(struct ext4_extent *ep)
56 printf(" ext %p => (blk %u len %u start %lu)\n",
57 ep, ep->e_blk, ep->e_len,
58 (uint64_t)ep->e_start_hi << 32 | ep->e_start_lo);
61 static void ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp);
64 ext4_ext_print_index(struct inode *ip, struct ext4_extent_index *ex, int do_walk)
72 printf(" index %p => (blk %u pblk %lu)\n",
73 ex, ex->ei_blk, (uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo);
78 if ((error = bread(ip->i_devvp,
79 fsbtodb(fs, ((uint64_t)ex->ei_leaf_hi << 32 | ex->ei_leaf_lo)),
80 (int)fs->e2fs_bsize, NOCRED, &bp)) != 0) {
85 ext4_ext_print_header(ip, (struct ext4_extent_header *)bp->b_data);
92 ext4_ext_print_header(struct inode *ip, struct ext4_extent_header *ehp)
96 printf("header %p => (magic 0x%x entries %d max %d depth %d gen %d)\n",
97 ehp, ehp->eh_magic, ehp->eh_ecount, ehp->eh_max, ehp->eh_depth,
100 for (i = 0; i < ehp->eh_ecount; i++)
101 if (ehp->eh_depth != 0)
102 ext4_ext_print_index(ip,
103 (struct ext4_extent_index *)(ehp + 1 + i), 1);
105 ext4_ext_print_extent((struct ext4_extent *)(ehp + 1 + i));
109 ext4_ext_print_path(struct inode *ip, struct ext4_extent_path *path)
115 printf("ip=%d, Path:\n", ip->i_number);
116 for (k = 0; k <= l; k++, path++) {
117 if (path->ep_index) {
118 ext4_ext_print_index(ip, path->ep_index, 0);
119 } else if (path->ep_ext) {
120 ext4_ext_print_extent(path->ep_ext);
126 ext4_ext_print_extent_tree_status(struct inode * ip)
128 struct ext4_extent_header *ehp;
130 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
132 printf("Extent status:ip=%d\n", ip->i_number);
133 if (!(ip->i_flag & IN_E4EXTENTS))
136 ext4_ext_print_header(ip, ehp);
142 static inline struct ext4_extent_header *
143 ext4_ext_inode_header(struct inode *ip)
146 return ((struct ext4_extent_header *)ip->i_db);
149 static inline struct ext4_extent_header *
150 ext4_ext_block_header(char *bdata)
153 return ((struct ext4_extent_header *)bdata);
156 static inline unsigned short
157 ext4_ext_inode_depth(struct inode *ip)
159 struct ext4_extent_header *ehp;
161 ehp = (struct ext4_extent_header *)ip->i_data;
162 return (ehp->eh_depth);
165 static inline e4fs_daddr_t
166 ext4_ext_index_pblock(struct ext4_extent_index *index)
170 blk = index->ei_leaf_lo;
171 blk |= (e4fs_daddr_t)index->ei_leaf_hi << 32;
177 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
180 index->ei_leaf_lo = pb & 0xffffffff;
181 index->ei_leaf_hi = (pb >> 32) & 0xffff;
185 static inline e4fs_daddr_t
186 ext4_ext_extent_pblock(struct ext4_extent *extent)
190 blk = extent->e_start_lo;
191 blk |= (e4fs_daddr_t)extent->e_start_hi << 32;
197 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
200 ex->e_start_lo = pb & 0xffffffff;
201 ex->e_start_hi = (pb >> 32) & 0xffff;
205 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
207 struct ext4_extent_cache *ecp;
208 int ret = EXT4_EXT_CACHE_NO;
210 ecp = &ip->i_ext_cache;
211 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
214 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
215 ep->e_blk = ecp->ec_blk;
216 ep->e_start_lo = ecp->ec_start & 0xffffffff;
217 ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
218 ep->e_len = ecp->ec_len;
225 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
232 if (eh->eh_magic != EXT4_EXT_MAGIC) {
233 error_msg = "invalid magic";
236 if (eh->eh_max == 0) {
237 error_msg = "invalid eh_max";
240 if (eh->eh_ecount > eh->eh_max) {
241 error_msg = "invalid eh_entries";
248 ext2_fserr(fs, ip->i_uid, error_msg);
253 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
255 struct ext4_extent_header *eh;
256 struct ext4_extent_index *r, *l, *m;
258 eh = path->ep_header;
260 KASSERT(eh->eh_ecount <= eh->eh_max && eh->eh_ecount > 0,
261 ("ext4_ext_binsearch_index: bad args"));
263 l = EXT_FIRST_INDEX(eh) + 1;
264 r = EXT_FIRST_INDEX(eh) + eh->eh_ecount - 1;
273 path->ep_index = l - 1;
277 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
279 struct ext4_extent_header *eh;
280 struct ext4_extent *r, *l, *m;
282 eh = path->ep_header;
284 KASSERT(eh->eh_ecount <= eh->eh_max,
285 ("ext4_ext_binsearch_ext: bad args"));
287 if (eh->eh_ecount == 0)
290 l = EXT_FIRST_EXTENT(eh) + 1;
291 r = EXT_FIRST_EXTENT(eh) + eh->eh_ecount - 1;
301 path->ep_ext = l - 1;
305 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
306 struct buf *bp, uint64_t blk)
309 KASSERT(path->ep_data == NULL,
310 ("ext4_ext_fill_path_bdata: bad ep_data"));
312 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
316 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
323 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
326 KASSERT(path->ep_data != NULL,
327 ("ext4_ext_fill_path_buf: bad ep_data"));
329 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
333 ext4_ext_drop_refs(struct ext4_extent_path *path)
340 depth = path->ep_depth;
341 for (i = 0; i <= depth; i++, path++)
343 free(path->ep_data, M_EXT2EXTENTS);
344 path->ep_data = NULL;
349 ext4_ext_path_free(struct ext4_extent_path *path)
355 ext4_ext_drop_refs(path);
356 free(path, M_EXT2EXTENTS);
360 ext4_ext_find_extent(struct inode *ip, daddr_t block,
361 struct ext4_extent_path **ppath)
364 struct ext4_extent_header *eh;
365 struct ext4_extent_path *path;
368 int error, depth, i, ppos, alloc;
371 eh = ext4_ext_inode_header(ip);
372 depth = ext4_ext_inode_depth(ip);
376 error = ext4_ext_check_header(ip, eh);
385 path = malloc(EXT4_EXT_DEPTH_MAX *
386 sizeof(struct ext4_extent_path),
387 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
395 path[0].ep_header = eh;
396 path[0].ep_data = NULL;
398 /* Walk through the tree. */
401 ext4_ext_binsearch_index(&path[ppos], block);
402 blk = ext4_ext_index_pblock(path[ppos].ep_index);
403 path[ppos].ep_depth = i;
404 path[ppos].ep_ext = NULL;
406 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
407 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
415 ext2_fserr(fs, ip->i_uid,
416 "ppos > depth => extent corrupted");
422 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
425 eh = ext4_ext_block_header(path[ppos].ep_data);
426 if (ext4_ext_check_header(ip, eh) ||
427 ext2_extent_blk_csum_verify(ip, path[ppos].ep_data)) {
432 path[ppos].ep_header = eh;
437 error = ext4_ext_check_header(ip, eh);
442 path[ppos].ep_depth = i;
443 path[ppos].ep_header = eh;
444 path[ppos].ep_ext = NULL;
445 path[ppos].ep_index = NULL;
446 ext4_ext_binsearch_ext(&path[ppos], block);
450 ext4_ext_drop_refs(path);
452 free(path, M_EXT2EXTENTS);
460 ext4_ext_space_root(struct inode *ip)
464 size = sizeof(ip->i_data);
465 size -= sizeof(struct ext4_extent_header);
466 size /= sizeof(struct ext4_extent);
472 ext4_ext_space_block(struct inode *ip)
479 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
480 sizeof(struct ext4_extent);
486 ext4_ext_space_block_index(struct inode *ip)
493 size = (fs->e2fs_bsize - sizeof(struct ext4_extent_header)) /
494 sizeof(struct ext4_extent_index);
500 ext4_ext_tree_init(struct inode *ip)
502 struct ext4_extent_header *ehp;
504 ip->i_flag |= IN_E4EXTENTS;
506 memset(ip->i_data, 0, EXT2_NDADDR + EXT2_NIADDR);
507 ehp = (struct ext4_extent_header *)ip->i_data;
508 ehp->eh_magic = EXT4_EXT_MAGIC;
509 ehp->eh_max = ext4_ext_space_root(ip);
510 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
511 ip->i_flag |= IN_CHANGE | IN_UPDATE;
512 ext2_update(ip->i_vnode, 1);
516 ext4_ext_put_in_cache(struct inode *ip, uint32_t blk,
517 uint32_t len, uint32_t start, int type)
520 KASSERT(len != 0, ("ext4_ext_put_in_cache: bad input"));
522 ip->i_ext_cache.ec_type = type;
523 ip->i_ext_cache.ec_blk = blk;
524 ip->i_ext_cache.ec_len = len;
525 ip->i_ext_cache.ec_start = start;
529 ext4_ext_blkpref(struct inode *ip, struct ext4_extent_path *path,
533 struct ext4_extent *ex;
534 e4fs_daddr_t bg_start;
540 depth = path->ep_depth;
541 ex = path[depth].ep_ext;
543 e4fs_daddr_t pblk = ext4_ext_extent_pblock(ex);
544 e2fs_daddr_t blk = ex->e_blk;
547 return (pblk + (block - blk));
549 return (pblk - (blk - block));
552 /* Try to get block from index itself. */
553 if (path[depth].ep_data)
554 return (path[depth].ep_blk);
557 /* Use inode's group. */
558 bg_start = (ip->i_block_group * EXT2_BLOCKS_PER_GROUP(ip->i_e2fs)) +
559 fs->e2fs->e2fs_first_dblock;
561 return (bg_start + block);
565 ext4_can_extents_be_merged(struct ext4_extent *ex1,
566 struct ext4_extent *ex2)
569 if (ex1->e_blk + ex1->e_len != ex2->e_blk)
572 if (ex1->e_len + ex2->e_len > EXT4_MAX_LEN)
575 if (ext4_ext_extent_pblock(ex1) + ex1->e_len ==
576 ext4_ext_extent_pblock(ex2))
583 ext4_ext_next_leaf_block(struct inode *ip, struct ext4_extent_path *path)
585 int depth = path->ep_depth;
589 return (EXT4_MAX_BLOCKS);
595 if (path[depth].ep_index !=
596 EXT_LAST_INDEX(path[depth].ep_header))
597 return (path[depth].ep_index[1].ei_blk);
602 return (EXT4_MAX_BLOCKS);
606 ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
620 bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
621 fs->e2fs_bsize, 0, 0, 0);
624 ext4_ext_fill_path_buf(path, bp);
625 ext2_extent_blk_csum_set(ip, bp->b_data);
628 ip->i_flag |= IN_CHANGE | IN_UPDATE;
629 error = ext2_update(ip->i_vnode, 1);
636 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
637 uint32_t lblk, e4fs_daddr_t blk)
640 struct ext4_extent_index *idx;
645 if (lblk == path->ep_index->ei_blk) {
646 ext2_fserr(fs, ip->i_uid,
647 "lblk == index blk => extent corrupted");
651 if (path->ep_header->eh_ecount >= path->ep_header->eh_max) {
652 ext2_fserr(fs, ip->i_uid,
653 "ecout > maxcount => extent corrupted");
657 if (lblk > path->ep_index->ei_blk) {
659 idx = path->ep_index + 1;
662 idx = path->ep_index;
665 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
667 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
669 if (idx > EXT_MAX_INDEX(path->ep_header)) {
670 ext2_fserr(fs, ip->i_uid,
671 "index is out of range => extent corrupted");
676 ext4_index_store_pblock(idx, blk);
677 path->ep_header->eh_ecount++;
679 return (ext4_ext_dirty(ip, path));
683 ext4_ext_alloc_meta(struct inode *ip)
685 e4fs_daddr_t blk = ext2_alloc_meta(ip);
687 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
688 ip->i_flag |= IN_CHANGE | IN_UPDATE;
689 ext2_update(ip->i_vnode, 1);
696 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
699 int i, blocksreleased;
702 blocksreleased = count;
704 for(i = 0; i < count; i++)
705 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
707 if (ip->i_blocks >= blocksreleased)
708 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
712 ip->i_flag |= IN_CHANGE | IN_UPDATE;
713 ext2_update(ip->i_vnode, 1);
717 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
718 struct ext4_extent *newext, int at)
722 int depth = ext4_ext_inode_depth(ip);
723 struct ext4_extent_header *neh;
724 struct ext4_extent_index *fidx;
725 struct ext4_extent *ex;
727 e4fs_daddr_t newblk, oldblk;
729 e4fs_daddr_t *ablks = NULL;
736 * We will split at current extent for now.
738 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
739 ext2_fserr(fs, ip->i_uid,
740 "extent is out of range => extent corrupted");
744 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
745 border = path[depth].ep_ext[1].e_blk;
747 border = newext->e_blk;
749 /* Allocate new blocks. */
750 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
751 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
754 for (a = 0; a < depth - at; a++) {
755 newblk = ext4_ext_alloc_meta(ip);
762 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
768 neh = ext4_ext_block_header(bp->b_data);
770 neh->eh_max = ext4_ext_space_block(ip);
771 neh->eh_magic = EXT4_EXT_MAGIC;
773 ex = EXT_FIRST_EXTENT(neh);
775 if (path[depth].ep_header->eh_ecount != path[depth].ep_header->eh_max) {
776 ext2_fserr(fs, ip->i_uid,
777 "extents count out of range => extent corrupted");
782 /* Start copy from next extent. */
784 path[depth].ep_ext++;
785 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
786 path[depth].ep_ext++;
790 memmove(ex, path[depth].ep_ext - m,
791 sizeof(struct ext4_extent) * m);
792 neh->eh_ecount = neh->eh_ecount + m;
795 ext2_extent_blk_csum_set(ip, bp->b_data);
801 path[depth].ep_header->eh_ecount =
802 path[depth].ep_header->eh_ecount - m;
803 ext4_ext_dirty(ip, path + depth);
806 /* Create intermediate indexes. */
808 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
810 /* Insert new index into current index block. */
815 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
816 (int)fs->e2fs_bsize, NOCRED, &bp);
822 neh = (struct ext4_extent_header *)bp->b_data;
824 neh->eh_magic = EXT4_EXT_MAGIC;
825 neh->eh_max = ext4_ext_space_block_index(ip);
826 neh->eh_depth = depth - i;
827 fidx = EXT_FIRST_INDEX(neh);
828 fidx->ei_blk = border;
829 ext4_index_store_pblock(fidx, oldblk);
833 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
838 memmove(++fidx, path[i].ep_index - m,
839 sizeof(struct ext4_extent_index) * m);
840 neh->eh_ecount = neh->eh_ecount + m;
843 ext2_extent_blk_csum_set(ip, bp->b_data);
849 path[i].ep_header->eh_ecount =
850 path[i].ep_header->eh_ecount - m;
851 ext4_ext_dirty(ip, path + i);
857 error = ext4_ext_insert_index(ip, path + at, border, newblk);
864 for (i = 0; i < depth; i++) {
867 ext4_ext_blkfree(ip, ablks[i], 1, 0);
871 free(ablks, M_EXT2EXTENTS);
877 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
878 struct ext4_extent *newext)
881 struct ext4_extent_path *curpath;
882 struct ext4_extent_header *neh;
890 newblk = ext4_ext_alloc_meta(ip);
894 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
898 /* Move top-level index/leaf into new block. */
899 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
901 /* Set size of new block */
902 neh = ext4_ext_block_header(bp->b_data);
903 neh->eh_magic = EXT4_EXT_MAGIC;
905 if (ext4_ext_inode_depth(ip))
906 neh->eh_max = ext4_ext_space_block_index(ip);
908 neh->eh_max = ext4_ext_space_block(ip);
910 ext2_extent_blk_csum_set(ip, bp->b_data);
917 curpath->ep_header->eh_magic = EXT4_EXT_MAGIC;
918 curpath->ep_header->eh_max = ext4_ext_space_root(ip);
919 curpath->ep_header->eh_ecount = 1;
920 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
921 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
922 ext4_index_store_pblock(curpath->ep_index, newblk);
924 neh = ext4_ext_inode_header(ip);
925 neh->eh_depth = path->ep_depth + 1;
926 ext4_ext_dirty(ip, curpath);
934 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
935 struct ext4_extent *newext)
937 struct ext4_extent_path *curpath;
941 i = depth = ext4_ext_inode_depth(ip);
943 /* Look for free index entry int the tree */
944 curpath = path + depth;
945 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
951 * We use already allocated block for index block,
952 * so subsequent data blocks should be contiguous.
954 if (EXT_HAS_FREE_INDEX(curpath)) {
955 error = ext4_ext_split(ip, path, newext, i);
960 ext4_ext_drop_refs(path);
961 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
965 /* Tree is full, do grow in depth. */
966 error = ext4_ext_grow_indepth(ip, path, newext);
971 ext4_ext_drop_refs(path);
972 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
976 /* Check and split tree if required. */
977 depth = ext4_ext_inode_depth(ip);
978 if (path[depth].ep_header->eh_ecount ==
979 path[depth].ep_header->eh_max)
988 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
990 struct ext4_extent_header *eh;
991 struct ext4_extent *ex;
995 depth = ext4_ext_inode_depth(ip);
996 eh = path[depth].ep_header;
997 ex = path[depth].ep_ext;
999 if (ex == NULL || eh == NULL)
1005 /* We will correct tree if first leaf got modified only. */
1006 if (ex != EXT_FIRST_EXTENT(eh))
1010 border = path[depth].ep_ext->e_blk;
1011 path[k].ep_index->ei_blk = border;
1012 ext4_ext_dirty(ip, path + k);
1014 /* Change all left-side indexes. */
1015 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1018 path[k].ep_index->ei_blk = border;
1019 ext4_ext_dirty(ip, path + k);
1026 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1027 struct ext4_extent *newext)
1029 struct ext4_extent_header * eh;
1030 struct ext4_extent *ex, *nex, *nearex;
1031 struct ext4_extent_path *npath;
1032 int depth, len, error, next;
1034 depth = ext4_ext_inode_depth(ip);
1035 ex = path[depth].ep_ext;
1038 if (newext->e_len == 0 || path[depth].ep_header == NULL)
1041 /* Insert block into found extent. */
1042 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1043 ex->e_len = ex->e_len + newext->e_len;
1044 eh = path[depth].ep_header;
1050 depth = ext4_ext_inode_depth(ip);
1051 eh = path[depth].ep_header;
1052 if (eh->eh_ecount < eh->eh_max)
1056 nex = EXT_LAST_EXTENT(eh);
1057 next = ext4_ext_next_leaf_block(ip, path);
1058 if (newext->e_blk > nex->e_blk && next != EXT4_MAX_BLOCKS) {
1059 KASSERT(npath == NULL,
1060 ("ext4_ext_insert_extent: bad path"));
1062 error = ext4_ext_find_extent(ip, next, &npath);
1066 if (npath->ep_depth != path->ep_depth) {
1071 eh = npath[depth].ep_header;
1072 if (eh->eh_ecount < eh->eh_max) {
1079 * There is no free space in the found leaf,
1080 * try to add a new leaf to the tree.
1082 error = ext4_ext_create_new_leaf(ip, path, newext);
1086 depth = ext4_ext_inode_depth(ip);
1087 eh = path[depth].ep_header;
1090 nearex = path[depth].ep_ext;
1092 /* Create new extent in the leaf. */
1093 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1094 } else if (newext->e_blk > nearex->e_blk) {
1095 if (nearex != EXT_LAST_EXTENT(eh)) {
1096 len = EXT_MAX_EXTENT(eh) - nearex;
1097 len = (len - 1) * sizeof(struct ext4_extent);
1098 len = len < 0 ? 0 : len;
1099 memmove(nearex + 2, nearex + 1, len);
1101 path[depth].ep_ext = nearex + 1;
1103 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1104 len = len < 0 ? 0 : len;
1105 memmove(nearex + 1, nearex, len);
1106 path[depth].ep_ext = nearex;
1109 eh->eh_ecount = eh->eh_ecount + 1;
1110 nearex = path[depth].ep_ext;
1111 nearex->e_blk = newext->e_blk;
1112 nearex->e_start_lo = newext->e_start_lo;
1113 nearex->e_start_hi = newext->e_start_hi;
1114 nearex->e_len = newext->e_len;
1117 /* Try to merge extents to the right. */
1118 while (nearex < EXT_LAST_EXTENT(eh)) {
1119 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1122 /* Merge with next extent. */
1123 nearex->e_len = nearex->e_len + nearex[1].e_len;
1124 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1125 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1126 sizeof(struct ext4_extent);
1127 memmove(nearex + 1, nearex + 2, len);
1130 eh->eh_ecount = eh->eh_ecount - 1;
1131 KASSERT(eh->eh_ecount != 0,
1132 ("ext4_ext_insert_extent: bad ecount"));
1136 * Try to merge extents to the left,
1137 * start from inexes correction.
1139 error = ext4_ext_correct_indexes(ip, path);
1143 ext4_ext_dirty(ip, path + depth);
1147 ext4_ext_drop_refs(npath);
1148 free(npath, M_EXT2EXTENTS);
1151 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1156 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1157 struct ucred *cred, unsigned long *count, int *perror)
1159 struct m_ext2fs *fs;
1160 e4fs_daddr_t newblk;
1165 * We will allocate only single block for now.
1170 EXT2_LOCK(ip->i_ump);
1171 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1176 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1177 ext2_update(ip->i_vnode, 1);
1184 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1185 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1186 int *pallocated, daddr_t *nb)
1188 struct m_ext2fs *fs;
1189 struct buf *bp = NULL;
1190 struct ext4_extent_path *path;
1191 struct ext4_extent newex, *ex;
1192 e4fs_daddr_t bpref, newblk = 0;
1193 unsigned long allocated = 0;
1194 int error = 0, depth;
1203 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1204 if (bpref == EXT4_EXT_CACHE_IN) {
1205 /* Block is already allocated. */
1206 newblk = iblk - newex.e_blk +
1207 ext4_ext_extent_pblock(&newex);
1208 allocated = newex.e_len - (iblk - newex.e_blk);
1216 error = ext4_ext_find_extent(ip, iblk, &path);
1221 depth = ext4_ext_inode_depth(ip);
1222 if (path[depth].ep_ext == NULL && depth != 0) {
1227 if ((ex = path[depth].ep_ext)) {
1228 uint64_t lblk = ex->e_blk;
1229 uint16_t e_len = ex->e_len;
1230 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1232 if (e_len > EXT4_MAX_LEN)
1235 /* If we found extent covers block, simply return it. */
1236 if (iblk >= lblk && iblk < lblk + e_len) {
1237 newblk = iblk - lblk + e_start;
1238 allocated = e_len - (iblk - lblk);
1239 ext4_ext_put_in_cache(ip, lblk, e_len,
1240 e_start, EXT4_EXT_CACHE_IN);
1245 /* Allocate the new block. */
1246 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1247 ip->i_next_alloc_goal = 0;
1250 bpref = ext4_ext_blkpref(ip, path, iblk);
1251 allocated = max_blocks;
1252 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1256 /* Try to insert new extent into found leaf and return. */
1258 ext4_ext_store_pblock(&newex, newblk);
1259 newex.e_len = allocated;
1260 error = ext4_ext_insert_extent(ip, path, &newex);
1264 newblk = ext4_ext_extent_pblock(&newex);
1265 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1269 if (allocated > max_blocks)
1270 allocated = max_blocks;
1274 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1275 fs->e2fs_bsize, cred, &bp);
1285 ext4_ext_drop_refs(path);
1286 free(path, M_EXT2EXTENTS);
1295 static inline uint16_t
1296 ext4_ext_get_actual_len(struct ext4_extent *ext)
1299 return (ext->e_len <= EXT_INIT_MAX_LEN ?
1300 ext->e_len : (ext->e_len - EXT_INIT_MAX_LEN));
1303 static inline struct ext4_extent_header *
1304 ext4_ext_header(struct inode *ip)
1307 return (struct ext4_extent_header *)ip->i_db;
1311 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1312 unsigned long from, unsigned long to)
1314 unsigned long num, start;
1316 if (from >= ex->e_blk &&
1317 to == ex->e_blk + ext4_ext_get_actual_len(ex) - 1) {
1319 num = ex->e_blk + ext4_ext_get_actual_len(ex) - from;
1320 start = ext4_ext_extent_pblock(ex) +
1321 ext4_ext_get_actual_len(ex) - num;
1322 ext4_ext_blkfree(ip, start, num, 0);
1329 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1333 /* Free index block. */
1335 leaf = ext4_ext_index_pblock(path->ep_index);
1336 KASSERT(path->ep_header->eh_ecount != 0,
1337 ("ext4_ext_rm_index: bad ecount"));
1338 path->ep_header->eh_ecount--;
1339 ext4_ext_dirty(ip, path);
1340 ext4_ext_blkfree(ip, leaf, 1, 0);
1345 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1348 struct m_ext2fs *fs;
1350 struct ext4_extent_header *eh;
1351 unsigned int a, b, block, num;
1352 unsigned long ex_blk;
1353 unsigned short ex_len;
1354 struct ext4_extent *ex;
1355 int error, correct_index;
1358 depth = ext4_ext_inode_depth(ip);
1361 if (!path[depth].ep_header) {
1362 if (path[depth].ep_data == NULL)
1364 path[depth].ep_header =
1365 (struct ext4_extent_header* )path[depth].ep_data;
1368 eh = path[depth].ep_header;
1370 ext2_fserr(fs, ip->i_uid, "bad header => extent corrupted");
1374 ex = EXT_LAST_EXTENT(eh);
1376 ex_len = ext4_ext_get_actual_len(ex);
1378 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1379 path[depth].ep_ext = ex;
1380 a = ex_blk > start ? ex_blk : start;
1381 b = (uint64_t)ex_blk + ex_len - 1 <
1382 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1384 if (a != ex_blk && b != ex_blk + ex_len - 1)
1386 else if (a != ex_blk) {
1387 /* Remove tail of the extent. */
1390 } else if (b != ex_blk + ex_len - 1) {
1391 /* Remove head of the extent, not implemented. */
1394 /* Remove whole extent. */
1399 if (ex == EXT_FIRST_EXTENT(eh))
1402 error = ext4_remove_blocks(ip, ex, a, b);
1407 ext4_ext_store_pblock(ex, 0);
1414 ext4_ext_dirty(ip, path + depth);
1418 ex_len = ext4_ext_get_actual_len(ex);
1421 if (correct_index && eh->eh_ecount)
1422 error = ext4_ext_correct_indexes(ip, path);
1425 * If this leaf is free, we should
1426 * remove it from index block above.
1428 if (error == 0 && eh->eh_ecount == 0 && path[depth].ep_data != NULL)
1429 error = ext4_ext_rm_index(ip, path + depth);
1436 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1437 int depth, int flags)
1439 struct m_ext2fs *fs;
1440 struct ext4_extent_header *eh;
1446 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1447 fs->e2fs_bsize, NOCRED, &bp);
1453 eh = ext4_ext_block_header(bp->b_data);
1454 if (eh->eh_depth != depth) {
1455 ext2_fserr(fs, ip->i_uid, "unexpected eh_depth");
1459 error = ext4_ext_check_header(ip, eh);
1472 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1475 KASSERT(path->ep_index != NULL,
1476 ("ext4_ext_more_to_rm: bad index from path"));
1478 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1481 if (path->ep_header->eh_ecount == path->index_count)
1488 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1489 struct ucred *cred, struct thread *td)
1492 struct ext4_extent_header *ehp;
1493 struct ext4_extent_path *path;
1497 ehp = (struct ext4_extent_header *)ip->i_db;
1498 depth = ext4_ext_inode_depth(ip);
1500 error = ext4_ext_check_header(ip, ehp);
1504 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1505 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1510 path[0].ep_header = ehp;
1511 path[0].ep_depth = depth;
1512 while (i >= 0 && error == 0) {
1515 error = ext4_ext_rm_leaf(ip, path, length);
1518 free(path[i].ep_data, M_EXT2EXTENTS);
1519 path[i].ep_data = NULL;
1524 /* This is index. */
1525 if (!path[i].ep_header)
1527 (struct ext4_extent_header *)path[i].ep_data;
1529 if (!path[i].ep_index) {
1530 /* This level hasn't touched yet. */
1531 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1532 path[i].index_count = path[i].ep_header->eh_ecount + 1;
1534 /* We've already was here, see at next index. */
1538 if (ext4_ext_more_to_rm(path + i)) {
1539 memset(path + i + 1, 0, sizeof(*path));
1540 bp = ext4_read_extent_tree_block(ip,
1541 ext4_ext_index_pblock(path[i].ep_index),
1542 path[0].ep_depth - (i + 1), 0);
1548 ext4_ext_fill_path_bdata(&path[i+1], bp,
1549 ext4_ext_index_pblock(path[i].ep_index));
1551 path[i].index_count = path[i].ep_header->eh_ecount;
1554 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1555 /* Index is empty, remove it. */
1556 error = ext4_ext_rm_index(ip, path + i);
1558 free(path[i].ep_data, M_EXT2EXTENTS);
1559 path[i].ep_data = NULL;
1564 if (path->ep_header->eh_ecount == 0) {
1566 * Truncate the tree to zero.
1568 ext4_ext_header(ip)->eh_depth = 0;
1569 ext4_ext_header(ip)->eh_max = ext4_ext_space_root(ip);
1570 ext4_ext_dirty(ip, path);
1574 ext4_ext_drop_refs(path);
1575 free(path, M_EXT2EXTENTS);