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)
129 struct ext4_extent_header *ehp;
132 ehp = (struct ext4_extent_header *)(char *)ip->i_db;
134 printf("Extent status:ip=%d\n", ip->i_number);
135 if (!(ip->i_flag & IN_E4EXTENTS))
138 ext4_ext_print_header(ip, ehp);
144 static inline struct ext4_extent_header *
145 ext4_ext_inode_header(struct inode *ip)
148 return ((struct ext4_extent_header *)ip->i_db);
151 static inline struct ext4_extent_header *
152 ext4_ext_block_header(char *bdata)
155 return ((struct ext4_extent_header *)bdata);
158 static inline unsigned short
159 ext4_ext_inode_depth(struct inode *ip)
161 struct ext4_extent_header *ehp;
163 ehp = (struct ext4_extent_header *)ip->i_data;
164 return (ehp->eh_depth);
167 static inline e4fs_daddr_t
168 ext4_ext_index_pblock(struct ext4_extent_index *index)
172 blk = index->ei_leaf_lo;
173 blk |= (e4fs_daddr_t)index->ei_leaf_hi << 32;
179 ext4_index_store_pblock(struct ext4_extent_index *index, e4fs_daddr_t pb)
182 index->ei_leaf_lo = pb & 0xffffffff;
183 index->ei_leaf_hi = (pb >> 32) & 0xffff;
187 static inline e4fs_daddr_t
188 ext4_ext_extent_pblock(struct ext4_extent *extent)
192 blk = extent->e_start_lo;
193 blk |= (e4fs_daddr_t)extent->e_start_hi << 32;
199 ext4_ext_store_pblock(struct ext4_extent *ex, e4fs_daddr_t pb)
202 ex->e_start_lo = pb & 0xffffffff;
203 ex->e_start_hi = (pb >> 32) & 0xffff;
207 ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep)
209 struct ext4_extent_cache *ecp;
210 int ret = EXT4_EXT_CACHE_NO;
212 ecp = &ip->i_ext_cache;
213 if (ecp->ec_type == EXT4_EXT_CACHE_NO)
216 if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) {
217 ep->e_blk = ecp->ec_blk;
218 ep->e_start_lo = ecp->ec_start & 0xffffffff;
219 ep->e_start_hi = ecp->ec_start >> 32 & 0xffff;
220 ep->e_len = ecp->ec_len;
227 ext4_ext_check_header(struct inode *ip, struct ext4_extent_header *eh)
234 if (eh->eh_magic != EXT4_EXT_MAGIC) {
235 error_msg = "invalid magic";
238 if (eh->eh_max == 0) {
239 error_msg = "invalid eh_max";
242 if (eh->eh_ecount > eh->eh_max) {
243 error_msg = "invalid eh_entries";
250 ext2_fserr(fs, ip->i_uid, error_msg);
255 ext4_ext_binsearch_index(struct ext4_extent_path *path, int blk)
257 struct ext4_extent_header *eh;
258 struct ext4_extent_index *r, *l, *m;
260 eh = path->ep_header;
262 KASSERT(eh->eh_ecount <= eh->eh_max && eh->eh_ecount > 0,
263 ("ext4_ext_binsearch_index: bad args"));
265 l = EXT_FIRST_INDEX(eh) + 1;
266 r = EXT_FIRST_INDEX(eh) + eh->eh_ecount - 1;
275 path->ep_index = l - 1;
279 ext4_ext_binsearch_ext(struct ext4_extent_path *path, int blk)
281 struct ext4_extent_header *eh;
282 struct ext4_extent *r, *l, *m;
284 eh = path->ep_header;
286 KASSERT(eh->eh_ecount <= eh->eh_max,
287 ("ext4_ext_binsearch_ext: bad args"));
289 if (eh->eh_ecount == 0)
292 l = EXT_FIRST_EXTENT(eh) + 1;
293 r = EXT_FIRST_EXTENT(eh) + eh->eh_ecount - 1;
303 path->ep_ext = l - 1;
307 ext4_ext_fill_path_bdata(struct ext4_extent_path *path,
308 struct buf *bp, uint64_t blk)
311 KASSERT(path->ep_data == NULL,
312 ("ext4_ext_fill_path_bdata: bad ep_data"));
314 path->ep_data = malloc(bp->b_bufsize, M_EXT2EXTENTS, M_WAITOK);
318 memcpy(path->ep_data, bp->b_data, bp->b_bufsize);
325 ext4_ext_fill_path_buf(struct ext4_extent_path *path, struct buf *bp)
328 KASSERT(path->ep_data != NULL,
329 ("ext4_ext_fill_path_buf: bad ep_data"));
331 memcpy(bp->b_data, path->ep_data, bp->b_bufsize);
335 ext4_ext_drop_refs(struct ext4_extent_path *path)
342 depth = path->ep_depth;
343 for (i = 0; i <= depth; i++, path++)
345 free(path->ep_data, M_EXT2EXTENTS);
346 path->ep_data = NULL;
351 ext4_ext_path_free(struct ext4_extent_path *path)
357 ext4_ext_drop_refs(path);
358 free(path, M_EXT2EXTENTS);
362 ext4_ext_find_extent(struct inode *ip, daddr_t block,
363 struct ext4_extent_path **ppath)
366 struct ext4_extent_header *eh;
367 struct ext4_extent_path *path;
370 int error, depth, i, ppos, alloc;
373 eh = ext4_ext_inode_header(ip);
374 depth = ext4_ext_inode_depth(ip);
378 error = ext4_ext_check_header(ip, eh);
387 path = malloc(EXT4_EXT_DEPTH_MAX *
388 sizeof(struct ext4_extent_path),
389 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
397 path[0].ep_header = eh;
398 path[0].ep_data = NULL;
400 /* Walk through the tree. */
403 ext4_ext_binsearch_index(&path[ppos], block);
404 blk = ext4_ext_index_pblock(path[ppos].ep_index);
405 path[ppos].ep_depth = i;
406 path[ppos].ep_ext = NULL;
408 error = bread(ip->i_devvp, fsbtodb(ip->i_e2fs, blk),
409 ip->i_e2fs->e2fs_bsize, NOCRED, &bp);
417 ext2_fserr(fs, ip->i_uid,
418 "ppos > depth => extent corrupted");
424 ext4_ext_fill_path_bdata(&path[ppos], bp, blk);
427 eh = ext4_ext_block_header(path[ppos].ep_data);
428 error = ext4_ext_check_header(ip, eh);
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);
627 ip->i_flag |= IN_CHANGE | IN_UPDATE;
628 error = ext2_update(ip->i_vnode, 1);
635 ext4_ext_insert_index(struct inode *ip, struct ext4_extent_path *path,
636 uint32_t lblk, e4fs_daddr_t blk)
639 struct ext4_extent_index *idx;
644 if (lblk == path->ep_index->ei_blk) {
645 ext2_fserr(fs, ip->i_uid,
646 "lblk == index blk => extent corrupted");
650 if (path->ep_header->eh_ecount >= path->ep_header->eh_max) {
651 ext2_fserr(fs, ip->i_uid,
652 "ecout > maxcount => extent corrupted");
656 if (lblk > path->ep_index->ei_blk) {
658 idx = path->ep_index + 1;
661 idx = path->ep_index;
664 len = EXT_LAST_INDEX(path->ep_header) - idx + 1;
666 memmove(idx + 1, idx, len * sizeof(struct ext4_extent_index));
668 if (idx > EXT_MAX_INDEX(path->ep_header)) {
669 ext2_fserr(fs, ip->i_uid,
670 "index is out of range => extent corrupted");
675 ext4_index_store_pblock(idx, blk);
676 path->ep_header->eh_ecount++;
678 return (ext4_ext_dirty(ip, path));
682 ext4_ext_alloc_meta(struct inode *ip)
684 e4fs_daddr_t blk = ext2_alloc_meta(ip);
686 ip->i_blocks += btodb(ip->i_e2fs->e2fs_bsize);
687 ip->i_flag |= IN_CHANGE | IN_UPDATE;
688 ext2_update(ip->i_vnode, 1);
695 ext4_ext_blkfree(struct inode *ip, uint64_t blk, int count, int flags)
698 int i, blocksreleased;
701 blocksreleased = count;
703 for(i = 0; i < count; i++)
704 ext2_blkfree(ip, blk + i, fs->e2fs_bsize);
706 if (ip->i_blocks >= blocksreleased)
707 ip->i_blocks -= (btodb(fs->e2fs_bsize)*blocksreleased);
711 ip->i_flag |= IN_CHANGE | IN_UPDATE;
712 ext2_update(ip->i_vnode, 1);
716 ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
717 struct ext4_extent *newext, int at)
721 int depth = ext4_ext_inode_depth(ip);
722 struct ext4_extent_header *neh;
723 struct ext4_extent_index *fidx;
724 struct ext4_extent *ex;
726 e4fs_daddr_t newblk, oldblk;
728 e4fs_daddr_t *ablks = NULL;
735 * We will split at current extent for now.
737 if (path[depth].ep_ext > EXT_MAX_EXTENT(path[depth].ep_header)) {
738 ext2_fserr(fs, ip->i_uid,
739 "extent is out of range => extent corrupted");
743 if (path[depth].ep_ext != EXT_MAX_EXTENT(path[depth].ep_header))
744 border = path[depth].ep_ext[1].e_blk;
746 border = newext->e_blk;
748 /* Allocate new blocks. */
749 ablks = malloc(sizeof(e4fs_daddr_t) * depth,
750 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
753 for (a = 0; a < depth - at; a++) {
754 newblk = ext4_ext_alloc_meta(ip);
761 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
767 neh = ext4_ext_block_header(bp->b_data);
769 neh->eh_max = ext4_ext_space_block(ip);
770 neh->eh_magic = EXT4_EXT_MAGIC;
772 ex = EXT_FIRST_EXTENT(neh);
774 if (path[depth].ep_header->eh_ecount != path[depth].ep_header->eh_max) {
775 ext2_fserr(fs, ip->i_uid,
776 "extents count out of range => extent corrupted");
781 /* Start copy from next extent. */
783 path[depth].ep_ext++;
784 while (path[depth].ep_ext <= EXT_MAX_EXTENT(path[depth].ep_header)) {
785 path[depth].ep_ext++;
789 memmove(ex, path[depth].ep_ext - m,
790 sizeof(struct ext4_extent) * m);
791 neh->eh_ecount = neh->eh_ecount + m;
799 path[depth].ep_header->eh_ecount =
800 path[depth].ep_header->eh_ecount - m;
801 ext4_ext_dirty(ip, path + depth);
804 /* Create intermediate indexes. */
806 KASSERT(k >= 0, ("ext4_ext_split: negative k"));
808 /* Insert new index into current index block. */
813 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
814 (int)fs->e2fs_bsize, NOCRED, &bp);
820 neh = (struct ext4_extent_header *)bp->b_data;
822 neh->eh_magic = EXT4_EXT_MAGIC;
823 neh->eh_max = ext4_ext_space_block_index(ip);
824 neh->eh_depth = depth - i;
825 fidx = EXT_FIRST_INDEX(neh);
826 fidx->ei_blk = border;
827 ext4_index_store_pblock(fidx, oldblk);
831 while (path[i].ep_index <= EXT_MAX_INDEX(path[i].ep_header)) {
836 memmove(++fidx, path[i].ep_index - m,
837 sizeof(struct ext4_extent_index) * m);
838 neh->eh_ecount = neh->eh_ecount + m;
846 path[i].ep_header->eh_ecount =
847 path[i].ep_header->eh_ecount - m;
848 ext4_ext_dirty(ip, path + i);
854 error = ext4_ext_insert_index(ip, path + at, border, newblk);
861 for (i = 0; i < depth; i++) {
864 ext4_ext_blkfree(ip, ablks[i], 1, 0);
868 free(ablks, M_EXT2EXTENTS);
874 ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
875 struct ext4_extent *newext)
878 struct ext4_extent_path *curpath;
879 struct ext4_extent_header *neh;
880 struct ext4_extent_index *fidx;
888 newblk = ext4_ext_alloc_meta(ip);
892 bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
896 /* Move top-level index/leaf into new block. */
897 memmove(bp->b_data, curpath->ep_header, sizeof(ip->i_data));
899 /* Set size of new block */
900 neh = ext4_ext_block_header(bp->b_data);
901 neh->eh_magic = EXT4_EXT_MAGIC;
903 if (ext4_ext_inode_depth(ip))
904 neh->eh_max = ext4_ext_space_block_index(ip);
906 neh->eh_max = ext4_ext_space_block(ip);
914 curpath->ep_header->eh_magic = EXT4_EXT_MAGIC;
915 curpath->ep_header->eh_max = ext4_ext_space_root(ip);
916 curpath->ep_header->eh_ecount = 1;
917 curpath->ep_index = EXT_FIRST_INDEX(curpath->ep_header);
918 curpath->ep_index->ei_blk = EXT_FIRST_EXTENT(path[0].ep_header)->e_blk;
919 ext4_index_store_pblock(curpath->ep_index, newblk);
921 neh = ext4_ext_inode_header(ip);
922 fidx = EXT_FIRST_INDEX(neh);
923 neh->eh_depth = path->ep_depth + 1;
924 ext4_ext_dirty(ip, curpath);
932 ext4_ext_create_new_leaf(struct inode *ip, struct ext4_extent_path *path,
933 struct ext4_extent *newext)
936 struct ext4_extent_path *curpath;
942 i = depth = ext4_ext_inode_depth(ip);
944 /* Look for free index entry int the tree */
945 curpath = path + depth;
946 while (i > 0 && !EXT_HAS_FREE_INDEX(curpath)) {
952 * We use already allocated block for index block,
953 * so subsequent data blocks should be contiguous.
955 if (EXT_HAS_FREE_INDEX(curpath)) {
956 error = ext4_ext_split(ip, path, newext, i);
961 ext4_ext_drop_refs(path);
962 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
966 /* Tree is full, do grow in depth. */
967 error = ext4_ext_grow_indepth(ip, path, newext);
972 ext4_ext_drop_refs(path);
973 error = ext4_ext_find_extent(ip, newext->e_blk, &path);
977 /* Check and split tree if required. */
978 depth = ext4_ext_inode_depth(ip);
979 if (path[depth].ep_header->eh_ecount ==
980 path[depth].ep_header->eh_max)
989 ext4_ext_correct_indexes(struct inode *ip, struct ext4_extent_path *path)
991 struct ext4_extent_header *eh;
992 struct ext4_extent *ex;
996 depth = ext4_ext_inode_depth(ip);
997 eh = path[depth].ep_header;
998 ex = path[depth].ep_ext;
1000 if (ex == NULL || eh == NULL)
1006 /* We will correct tree if first leaf got modified only. */
1007 if (ex != EXT_FIRST_EXTENT(eh))
1011 border = path[depth].ep_ext->e_blk;
1012 path[k].ep_index->ei_blk = border;
1013 ext4_ext_dirty(ip, path + k);
1015 /* Change all left-side indexes. */
1016 if (path[k+1].ep_index != EXT_FIRST_INDEX(path[k+1].ep_header))
1019 path[k].ep_index->ei_blk = border;
1020 ext4_ext_dirty(ip, path + k);
1027 ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
1028 struct ext4_extent *newext)
1030 struct m_ext2fs *fs;
1031 struct ext4_extent_header * eh;
1032 struct ext4_extent *ex, *nex, *nearex;
1033 struct ext4_extent_path *npath;
1034 int depth, len, error, next;
1037 depth = ext4_ext_inode_depth(ip);
1038 ex = path[depth].ep_ext;
1041 if (newext->e_len == 0 || path[depth].ep_header == NULL)
1044 /* Insert block into found extent. */
1045 if (ex && ext4_can_extents_be_merged(ex, newext)) {
1046 ex->e_len = ex->e_len + newext->e_len;
1047 eh = path[depth].ep_header;
1053 depth = ext4_ext_inode_depth(ip);
1054 eh = path[depth].ep_header;
1055 if (eh->eh_ecount < eh->eh_max)
1059 nex = EXT_LAST_EXTENT(eh);
1060 next = ext4_ext_next_leaf_block(ip, path);
1061 if (newext->e_blk > nex->e_blk && next != EXT4_MAX_BLOCKS) {
1062 KASSERT(npath == NULL,
1063 ("ext4_ext_insert_extent: bad path"));
1065 error = ext4_ext_find_extent(ip, next, &npath);
1069 if (npath->ep_depth != path->ep_depth) {
1074 eh = npath[depth].ep_header;
1075 if (eh->eh_ecount < eh->eh_max) {
1082 * There is no free space in the found leaf,
1083 * try to add a new leaf to the tree.
1085 error = ext4_ext_create_new_leaf(ip, path, newext);
1089 depth = ext4_ext_inode_depth(ip);
1090 eh = path[depth].ep_header;
1093 nearex = path[depth].ep_ext;
1095 /* Create new extent in the leaf. */
1096 path[depth].ep_ext = EXT_FIRST_EXTENT(eh);
1097 } else if (newext->e_blk > nearex->e_blk) {
1098 if (nearex != EXT_LAST_EXTENT(eh)) {
1099 len = EXT_MAX_EXTENT(eh) - nearex;
1100 len = (len - 1) * sizeof(struct ext4_extent);
1101 len = len < 0 ? 0 : len;
1102 memmove(nearex + 2, nearex + 1, len);
1104 path[depth].ep_ext = nearex + 1;
1106 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1107 len = len < 0 ? 0 : len;
1108 memmove(nearex + 1, nearex, len);
1109 path[depth].ep_ext = nearex;
1112 eh->eh_ecount = eh->eh_ecount + 1;
1113 nearex = path[depth].ep_ext;
1114 nearex->e_blk = newext->e_blk;
1115 nearex->e_start_lo = newext->e_start_lo;
1116 nearex->e_start_hi = newext->e_start_hi;
1117 nearex->e_len = newext->e_len;
1120 /* Try to merge extents to the right. */
1121 while (nearex < EXT_LAST_EXTENT(eh)) {
1122 if (!ext4_can_extents_be_merged(nearex, nearex + 1))
1125 /* Merge with next extent. */
1126 nearex->e_len = nearex->e_len + nearex[1].e_len;
1127 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1128 len = (EXT_LAST_EXTENT(eh) - nearex - 1) *
1129 sizeof(struct ext4_extent);
1130 memmove(nearex + 1, nearex + 2, len);
1133 eh->eh_ecount = eh->eh_ecount - 1;
1134 KASSERT(eh->eh_ecount != 0,
1135 ("ext4_ext_insert_extent: bad ecount"));
1139 * Try to merge extents to the left,
1140 * start from inexes correction.
1142 error = ext4_ext_correct_indexes(ip, path);
1146 ext4_ext_dirty(ip, path + depth);
1150 ext4_ext_drop_refs(npath);
1151 free(npath, M_EXT2EXTENTS);
1154 ip->i_ext_cache.ec_type = EXT4_EXT_CACHE_NO;
1159 ext4_new_blocks(struct inode *ip, daddr_t lbn, e4fs_daddr_t pref,
1160 struct ucred *cred, unsigned long *count, int *perror)
1162 struct m_ext2fs *fs;
1163 struct ext2mount *ump;
1164 e4fs_daddr_t newblk;
1170 * We will allocate only single block for now.
1175 EXT2_LOCK(ip->i_ump);
1176 *perror = ext2_alloc(ip, lbn, pref, (int)fs->e2fs_bsize, cred, &newblk);
1181 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1182 ext2_update(ip->i_vnode, 1);
1189 ext4_ext_get_blocks(struct inode *ip, e4fs_daddr_t iblk,
1190 unsigned long max_blocks, struct ucred *cred, struct buf **bpp,
1191 int *pallocated, daddr_t *nb)
1193 struct m_ext2fs *fs;
1194 struct buf *bp = NULL;
1195 struct ext4_extent_path *path;
1196 struct ext4_extent newex, *ex;
1197 e4fs_daddr_t bpref, newblk = 0;
1198 unsigned long allocated = 0;
1199 int error = 0, depth;
1208 if ((bpref = ext4_ext_in_cache(ip, iblk, &newex))) {
1209 if (bpref == EXT4_EXT_CACHE_IN) {
1210 /* Block is already allocated. */
1211 newblk = iblk - newex.e_blk +
1212 ext4_ext_extent_pblock(&newex);
1213 allocated = newex.e_len - (iblk - newex.e_blk);
1221 error = ext4_ext_find_extent(ip, iblk, &path);
1226 depth = ext4_ext_inode_depth(ip);
1227 if (path[depth].ep_ext == NULL && depth != 0) {
1232 if ((ex = path[depth].ep_ext)) {
1233 uint64_t lblk = ex->e_blk;
1234 uint16_t e_len = ex->e_len;
1235 e4fs_daddr_t e_start = ext4_ext_extent_pblock(ex);
1237 if (e_len > EXT4_MAX_LEN)
1240 /* If we found extent covers block, simply return it. */
1241 if (iblk >= lblk && iblk < lblk + e_len) {
1242 newblk = iblk - lblk + e_start;
1243 allocated = e_len - (iblk - lblk);
1244 ext4_ext_put_in_cache(ip, lblk, e_len,
1245 e_start, EXT4_EXT_CACHE_IN);
1250 /* Allocate the new block. */
1251 if (S_ISREG(ip->i_mode) && (!ip->i_next_alloc_block)) {
1252 ip->i_next_alloc_goal = 0;
1255 bpref = ext4_ext_blkpref(ip, path, iblk);
1256 allocated = max_blocks;
1257 newblk = ext4_new_blocks(ip, iblk, bpref, cred, &allocated, &error);
1261 /* Try to insert new extent into found leaf and return. */
1263 ext4_ext_store_pblock(&newex, newblk);
1264 newex.e_len = allocated;
1265 error = ext4_ext_insert_extent(ip, path, &newex);
1269 newblk = ext4_ext_extent_pblock(&newex);
1270 ext4_ext_put_in_cache(ip, iblk, allocated, newblk, EXT4_EXT_CACHE_IN);
1274 if (allocated > max_blocks)
1275 allocated = max_blocks;
1279 error = bread(ip->i_devvp, fsbtodb(fs, newblk),
1280 fs->e2fs_bsize, cred, &bp);
1290 ext4_ext_drop_refs(path);
1291 free(path, M_EXT2EXTENTS);
1300 static inline uint16_t
1301 ext4_ext_get_actual_len(struct ext4_extent *ext)
1304 return (ext->e_len <= EXT_INIT_MAX_LEN ?
1305 ext->e_len : (ext->e_len - EXT_INIT_MAX_LEN));
1308 static inline struct ext4_extent_header *
1309 ext4_ext_header(struct inode *ip)
1312 return (struct ext4_extent_header *)ip->i_db;
1316 ext4_remove_blocks(struct inode *ip, struct ext4_extent *ex,
1317 unsigned long from, unsigned long to)
1319 unsigned long num, start;
1321 if (from >= ex->e_blk &&
1322 to == ex->e_blk + ext4_ext_get_actual_len(ex) - 1) {
1324 num = ex->e_blk + ext4_ext_get_actual_len(ex) - from;
1325 start = ext4_ext_extent_pblock(ex) +
1326 ext4_ext_get_actual_len(ex) - num;
1327 ext4_ext_blkfree(ip, start, num, 0);
1334 ext4_ext_rm_index(struct inode *ip, struct ext4_extent_path *path)
1338 /* Free index block. */
1340 leaf = ext4_ext_index_pblock(path->ep_index);
1341 KASSERT(path->ep_header->eh_ecount != 0,
1342 ("ext4_ext_rm_index: bad ecount"));
1343 path->ep_header->eh_ecount--;
1344 ext4_ext_dirty(ip, path);
1345 ext4_ext_blkfree(ip, leaf, 1, 0);
1350 ext4_ext_rm_leaf(struct inode *ip, struct ext4_extent_path *path,
1353 struct m_ext2fs *fs;
1355 struct ext4_extent_header *eh;
1356 unsigned int a, b, block, num;
1357 unsigned long ex_blk;
1358 unsigned short ex_len;
1359 struct ext4_extent *ex;
1360 int error, correct_index;
1363 depth = ext4_ext_inode_depth(ip);
1366 if (!path[depth].ep_header) {
1367 if (path[depth].ep_data == NULL)
1369 path[depth].ep_header =
1370 (struct ext4_extent_header* )path[depth].ep_data;
1373 eh = path[depth].ep_header;
1375 ext2_fserr(fs, ip->i_uid, "bad header => extent corrupted");
1379 ex = EXT_LAST_EXTENT(eh);
1381 ex_len = ext4_ext_get_actual_len(ex);
1383 while (ex >= EXT_FIRST_EXTENT(eh) && ex_blk + ex_len > start) {
1384 path[depth].ep_ext = ex;
1385 a = ex_blk > start ? ex_blk : start;
1386 b = (uint64_t)ex_blk + ex_len - 1 <
1387 EXT4_MAX_BLOCKS ? ex_blk + ex_len - 1 : EXT4_MAX_BLOCKS;
1389 if (a != ex_blk && b != ex_blk + ex_len - 1)
1391 else if (a != ex_blk) {
1392 /* Remove tail of the extent. */
1395 } else if (b != ex_blk + ex_len - 1) {
1396 /* Remove head of the extent, not implemented. */
1399 /* Remove whole extent. */
1404 if (ex == EXT_FIRST_EXTENT(eh))
1407 error = ext4_remove_blocks(ip, ex, a, b);
1412 ext4_ext_store_pblock(ex, 0);
1419 ext4_ext_dirty(ip, path + depth);
1423 ex_len = ext4_ext_get_actual_len(ex);
1426 if (correct_index && eh->eh_ecount)
1427 error = ext4_ext_correct_indexes(ip, path);
1430 * If this leaf is free, we should
1431 * remove it from index block above.
1433 if (error == 0 && eh->eh_ecount == 0 && path[depth].ep_data != NULL)
1434 error = ext4_ext_rm_index(ip, path + depth);
1441 ext4_read_extent_tree_block(struct inode *ip, e4fs_daddr_t pblk,
1442 int depth, int flags)
1444 struct m_ext2fs *fs;
1445 struct ext4_extent_header *eh;
1451 error = bread(ip->i_devvp, fsbtodb(fs, pblk),
1452 fs->e2fs_bsize, NOCRED, &bp);
1458 eh = ext4_ext_block_header(bp->b_data);
1459 if (eh->eh_depth != depth) {
1460 ext2_fserr(fs, ip->i_uid, "unexpected eh_depth");
1464 error = ext4_ext_check_header(ip, eh);
1477 ext4_ext_more_to_rm(struct ext4_extent_path *path)
1480 KASSERT(path->ep_index != NULL,
1481 ("ext4_ext_more_to_rm: bad index from path"));
1483 if (path->ep_index < EXT_FIRST_INDEX(path->ep_header))
1486 if (path->ep_header->eh_ecount == path->index_count)
1493 ext4_ext_remove_space(struct inode *ip, off_t length, int flags,
1494 struct ucred *cred, struct thread *td)
1497 struct ext4_extent_header *ehp;
1498 struct ext4_extent_path *path;
1502 ehp = (struct ext4_extent_header *)ip->i_db;
1503 depth = ext4_ext_inode_depth(ip);
1505 error = ext4_ext_check_header(ip, ehp);
1509 path = malloc(sizeof(struct ext4_extent_path) * (depth + 1),
1510 M_EXT2EXTENTS, M_WAITOK | M_ZERO);
1515 path[0].ep_header = ehp;
1516 path[0].ep_depth = depth;
1517 while (i >= 0 && error == 0) {
1520 error = ext4_ext_rm_leaf(ip, path, length);
1523 free(path[i].ep_data, M_EXT2EXTENTS);
1524 path[i].ep_data = NULL;
1529 /* This is index. */
1530 if (!path[i].ep_header)
1532 (struct ext4_extent_header *)path[i].ep_data;
1534 if (!path[i].ep_index) {
1535 /* This level hasn't touched yet. */
1536 path[i].ep_index = EXT_LAST_INDEX(path[i].ep_header);
1537 path[i].index_count = path[i].ep_header->eh_ecount + 1;
1539 /* We've already was here, see at next index. */
1543 if (ext4_ext_more_to_rm(path + i)) {
1544 memset(path + i + 1, 0, sizeof(*path));
1545 bp = ext4_read_extent_tree_block(ip,
1546 ext4_ext_index_pblock(path[i].ep_index),
1547 path[0].ep_depth - (i + 1), 0);
1553 ext4_ext_fill_path_bdata(&path[i+1], bp,
1554 ext4_ext_index_pblock(path[i].ep_index));
1556 path[i].index_count = path[i].ep_header->eh_ecount;
1559 if (path[i].ep_header->eh_ecount == 0 && i > 0) {
1560 /* Index is empty, remove it. */
1561 error = ext4_ext_rm_index(ip, path + i);
1563 free(path[i].ep_data, M_EXT2EXTENTS);
1564 path[i].ep_data = NULL;
1569 if (path->ep_header->eh_ecount == 0) {
1571 * Truncate the tree to zero.
1573 ext4_ext_header(ip)->eh_depth = 0;
1574 ext4_ext_header(ip)->eh_max = ext4_ext_space_root(ip);
1575 ext4_ext_dirty(ip, path);
1579 ext4_ext_drop_refs(path);
1580 free(path, M_EXT2EXTENTS);