1d7511a40SPedro F. Giffuni /*- 2d7511a40SPedro F. Giffuni * Copyright (c) 2010 Zheng Liu <lz@freebsd.org> 3d7511a40SPedro F. Giffuni * All rights reserved. 4d7511a40SPedro F. Giffuni * 5d7511a40SPedro F. Giffuni * Redistribution and use in source and binary forms, with or without 6d7511a40SPedro F. Giffuni * modification, are permitted provided that the following conditions 7d7511a40SPedro F. Giffuni * are met: 8d7511a40SPedro F. Giffuni * 1. Redistributions of source code must retain the above copyright 9d7511a40SPedro F. Giffuni * notice, this list of conditions and the following disclaimer. 10d7511a40SPedro F. Giffuni * 2. Redistributions in binary form must reproduce the above copyright 11d7511a40SPedro F. Giffuni * notice, this list of conditions and the following disclaimer in the 12d7511a40SPedro F. Giffuni * documentation and/or other materials provided with the distribution. 13d7511a40SPedro F. Giffuni * 14d7511a40SPedro F. Giffuni * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15d7511a40SPedro F. Giffuni * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16d7511a40SPedro F. Giffuni * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17d7511a40SPedro F. Giffuni * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18d7511a40SPedro F. Giffuni * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19d7511a40SPedro F. Giffuni * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20d7511a40SPedro F. Giffuni * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21d7511a40SPedro F. Giffuni * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22d7511a40SPedro F. Giffuni * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23d7511a40SPedro F. Giffuni * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24d7511a40SPedro F. Giffuni * SUCH DAMAGE. 25d7511a40SPedro F. Giffuni * 26d7511a40SPedro F. Giffuni * $FreeBSD$ 27d7511a40SPedro F. Giffuni */ 28d7511a40SPedro F. Giffuni 29d7511a40SPedro F. Giffuni #include <sys/param.h> 30d7511a40SPedro F. Giffuni #include <sys/systm.h> 31d7511a40SPedro F. Giffuni #include <sys/types.h> 32d7511a40SPedro F. Giffuni #include <sys/kernel.h> 33d7511a40SPedro F. Giffuni #include <sys/malloc.h> 34d7511a40SPedro F. Giffuni #include <sys/vnode.h> 35d7511a40SPedro F. Giffuni #include <sys/bio.h> 36d7511a40SPedro F. Giffuni #include <sys/buf.h> 37d7511a40SPedro F. Giffuni #include <sys/conf.h> 38d7511a40SPedro F. Giffuni 39d7511a40SPedro F. Giffuni #include <fs/ext2fs/ext2_mount.h> 40d7511a40SPedro F. Giffuni #include <fs/ext2fs/fs.h> 41d7511a40SPedro F. Giffuni #include <fs/ext2fs/inode.h> 42d7511a40SPedro F. Giffuni #include <fs/ext2fs/ext2fs.h> 43d7511a40SPedro F. Giffuni #include <fs/ext2fs/ext2_extents.h> 44d7511a40SPedro F. Giffuni #include <fs/ext2fs/ext2_extern.h> 45d7511a40SPedro F. Giffuni 46*78f6ea54SPedro F. Giffuni static int 47*78f6ea54SPedro F. Giffuni ext4_ext_binsearch_index(struct inode *ip, struct ext4_extent_path *path, 48*78f6ea54SPedro F. Giffuni daddr_t lbn, daddr_t *first_lbn, daddr_t *last_lbn) 49d7511a40SPedro F. Giffuni { 50d7511a40SPedro F. Giffuni struct ext4_extent_header *ehp = path->ep_header; 51*78f6ea54SPedro F. Giffuni struct ext4_extent_index *first, *last, *l, *r, *m; 52d7511a40SPedro F. Giffuni 53*78f6ea54SPedro F. Giffuni first = (struct ext4_extent_index *)(char *)(ehp + 1); 54*78f6ea54SPedro F. Giffuni last = first + ehp->eh_ecount - 1; 55*78f6ea54SPedro F. Giffuni l = first; 56*78f6ea54SPedro F. Giffuni r = last; 57d7511a40SPedro F. Giffuni while (l <= r) { 58d7511a40SPedro F. Giffuni m = l + (r - l) / 2; 59d7511a40SPedro F. Giffuni if (lbn < m->ei_blk) 60d7511a40SPedro F. Giffuni r = m - 1; 61d7511a40SPedro F. Giffuni else 62d7511a40SPedro F. Giffuni l = m + 1; 63d7511a40SPedro F. Giffuni } 64d7511a40SPedro F. Giffuni 65*78f6ea54SPedro F. Giffuni if (l == first) { 66*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_blk = *first_lbn; 67*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_len = first->ei_blk - *first_lbn; 68*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_start_hi = 0; 69*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_start_lo = 0; 70*78f6ea54SPedro F. Giffuni path->ep_is_sparse = 1; 71*78f6ea54SPedro F. Giffuni return (1); 72*78f6ea54SPedro F. Giffuni } 73d7511a40SPedro F. Giffuni path->ep_index = l - 1; 74*78f6ea54SPedro F. Giffuni *first_lbn = path->ep_index->ei_blk; 75*78f6ea54SPedro F. Giffuni if (path->ep_index < last) 76*78f6ea54SPedro F. Giffuni *last_lbn = l->ei_blk - 1; 77*78f6ea54SPedro F. Giffuni return (0); 78d7511a40SPedro F. Giffuni } 79d7511a40SPedro F. Giffuni 80d7511a40SPedro F. Giffuni static void 81*78f6ea54SPedro F. Giffuni ext4_ext_binsearch(struct inode *ip, struct ext4_extent_path *path, daddr_t lbn, 82*78f6ea54SPedro F. Giffuni daddr_t first_lbn, daddr_t last_lbn) 83d7511a40SPedro F. Giffuni { 84d7511a40SPedro F. Giffuni struct ext4_extent_header *ehp = path->ep_header; 85e813d9d7SPedro F. Giffuni struct ext4_extent *first, *l, *r, *m; 86d7511a40SPedro F. Giffuni 87d7511a40SPedro F. Giffuni if (ehp->eh_ecount == 0) 88d7511a40SPedro F. Giffuni return; 89d7511a40SPedro F. Giffuni 90e813d9d7SPedro F. Giffuni first = (struct ext4_extent *)(char *)(ehp + 1); 91e813d9d7SPedro F. Giffuni l = first; 92e813d9d7SPedro F. Giffuni r = first + ehp->eh_ecount - 1; 93d7511a40SPedro F. Giffuni while (l <= r) { 94d7511a40SPedro F. Giffuni m = l + (r - l) / 2; 95d7511a40SPedro F. Giffuni if (lbn < m->e_blk) 96d7511a40SPedro F. Giffuni r = m - 1; 97d7511a40SPedro F. Giffuni else 98d7511a40SPedro F. Giffuni l = m + 1; 99d7511a40SPedro F. Giffuni } 100d7511a40SPedro F. Giffuni 101e813d9d7SPedro F. Giffuni if (l == first) { 102*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_blk = first_lbn; 103*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_len = first->e_blk - first_lbn; 104e813d9d7SPedro F. Giffuni path->ep_sparse_ext.e_start_hi = 0; 105e813d9d7SPedro F. Giffuni path->ep_sparse_ext.e_start_lo = 0; 106e813d9d7SPedro F. Giffuni path->ep_is_sparse = 1; 107e813d9d7SPedro F. Giffuni return; 108e813d9d7SPedro F. Giffuni } 109d7511a40SPedro F. Giffuni path->ep_ext = l - 1; 110e813d9d7SPedro F. Giffuni if (path->ep_ext->e_blk + path->ep_ext->e_len <= lbn) { 111*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_blk = path->ep_ext->e_blk + 112*78f6ea54SPedro F. Giffuni path->ep_ext->e_len; 113e813d9d7SPedro F. Giffuni if (l <= (first + ehp->eh_ecount - 1)) 114*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_len = l->e_blk - 115*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_blk; 116*78f6ea54SPedro F. Giffuni else 117*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_len = last_lbn - 118*78f6ea54SPedro F. Giffuni path->ep_sparse_ext.e_blk + 1; 119e813d9d7SPedro F. Giffuni path->ep_sparse_ext.e_start_hi = 0; 120e813d9d7SPedro F. Giffuni path->ep_sparse_ext.e_start_lo = 0; 121e813d9d7SPedro F. Giffuni path->ep_is_sparse = 1; 122e813d9d7SPedro F. Giffuni } 123d7511a40SPedro F. Giffuni } 124d7511a40SPedro F. Giffuni 125d7511a40SPedro F. Giffuni /* 126d7511a40SPedro F. Giffuni * Find a block in ext4 extent cache. 127d7511a40SPedro F. Giffuni */ 128d7511a40SPedro F. Giffuni int 129d7511a40SPedro F. Giffuni ext4_ext_in_cache(struct inode *ip, daddr_t lbn, struct ext4_extent *ep) 130d7511a40SPedro F. Giffuni { 131d7511a40SPedro F. Giffuni struct ext4_extent_cache *ecp; 132d7511a40SPedro F. Giffuni int ret = EXT4_EXT_CACHE_NO; 133d7511a40SPedro F. Giffuni 134d7511a40SPedro F. Giffuni ecp = &ip->i_ext_cache; 135d7511a40SPedro F. Giffuni 136d7511a40SPedro F. Giffuni /* cache is invalid */ 137d7511a40SPedro F. Giffuni if (ecp->ec_type == EXT4_EXT_CACHE_NO) 138d7511a40SPedro F. Giffuni return (ret); 139d7511a40SPedro F. Giffuni 140d7511a40SPedro F. Giffuni if (lbn >= ecp->ec_blk && lbn < ecp->ec_blk + ecp->ec_len) { 141d7511a40SPedro F. Giffuni ep->e_blk = ecp->ec_blk; 142d7511a40SPedro F. Giffuni ep->e_start_lo = ecp->ec_start & 0xffffffff; 143d7511a40SPedro F. Giffuni ep->e_start_hi = ecp->ec_start >> 32 & 0xffff; 144d7511a40SPedro F. Giffuni ep->e_len = ecp->ec_len; 145d7511a40SPedro F. Giffuni ret = ecp->ec_type; 146d7511a40SPedro F. Giffuni } 147d7511a40SPedro F. Giffuni return (ret); 148d7511a40SPedro F. Giffuni } 149d7511a40SPedro F. Giffuni 150d7511a40SPedro F. Giffuni /* 151d7511a40SPedro F. Giffuni * Put an ext4_extent structure in ext4 cache. 152d7511a40SPedro F. Giffuni */ 153d7511a40SPedro F. Giffuni void 154d7511a40SPedro F. Giffuni ext4_ext_put_cache(struct inode *ip, struct ext4_extent *ep, int type) 155d7511a40SPedro F. Giffuni { 156d7511a40SPedro F. Giffuni struct ext4_extent_cache *ecp; 157d7511a40SPedro F. Giffuni 158d7511a40SPedro F. Giffuni ecp = &ip->i_ext_cache; 159d7511a40SPedro F. Giffuni ecp->ec_type = type; 160d7511a40SPedro F. Giffuni ecp->ec_blk = ep->e_blk; 161d7511a40SPedro F. Giffuni ecp->ec_len = ep->e_len; 162d7511a40SPedro F. Giffuni ecp->ec_start = (daddr_t)ep->e_start_hi << 32 | ep->e_start_lo; 163d7511a40SPedro F. Giffuni } 164d7511a40SPedro F. Giffuni 165d7511a40SPedro F. Giffuni /* 166d7511a40SPedro F. Giffuni * Find an extent. 167d7511a40SPedro F. Giffuni */ 168d7511a40SPedro F. Giffuni struct ext4_extent_path * 169d7511a40SPedro F. Giffuni ext4_ext_find_extent(struct m_ext2fs *fs, struct inode *ip, 170d7511a40SPedro F. Giffuni daddr_t lbn, struct ext4_extent_path *path) 171d7511a40SPedro F. Giffuni { 172d7511a40SPedro F. Giffuni struct ext4_extent_header *ehp; 173d7511a40SPedro F. Giffuni uint16_t i; 174d7511a40SPedro F. Giffuni int error, size; 175d7511a40SPedro F. Giffuni daddr_t nblk; 176d7511a40SPedro F. Giffuni 177d7511a40SPedro F. Giffuni ehp = (struct ext4_extent_header *)(char *)ip->i_db; 178d7511a40SPedro F. Giffuni 179d7511a40SPedro F. Giffuni if (ehp->eh_magic != EXT4_EXT_MAGIC) 180d7511a40SPedro F. Giffuni return (NULL); 181d7511a40SPedro F. Giffuni 182d7511a40SPedro F. Giffuni path->ep_header = ehp; 183d7511a40SPedro F. Giffuni 184*78f6ea54SPedro F. Giffuni daddr_t first_lbn = 0; 185*78f6ea54SPedro F. Giffuni daddr_t last_lbn = lblkno(ip->i_e2fs, ip->i_size); 186*78f6ea54SPedro F. Giffuni 187d7511a40SPedro F. Giffuni for (i = ehp->eh_depth; i != 0; --i) { 188*78f6ea54SPedro F. Giffuni path->ep_depth = i; 189d7511a40SPedro F. Giffuni path->ep_ext = NULL; 190*78f6ea54SPedro F. Giffuni if (ext4_ext_binsearch_index(ip, path, lbn, &first_lbn, 191*78f6ea54SPedro F. Giffuni &last_lbn)) { 192*78f6ea54SPedro F. Giffuni return (path); 193*78f6ea54SPedro F. Giffuni } 194d7511a40SPedro F. Giffuni 195d7511a40SPedro F. Giffuni nblk = (daddr_t)path->ep_index->ei_leaf_hi << 32 | 196d7511a40SPedro F. Giffuni path->ep_index->ei_leaf_lo; 197d7511a40SPedro F. Giffuni size = blksize(fs, ip, nblk); 198d7511a40SPedro F. Giffuni if (path->ep_bp != NULL) { 199d7511a40SPedro F. Giffuni brelse(path->ep_bp); 200d7511a40SPedro F. Giffuni path->ep_bp = NULL; 201d7511a40SPedro F. Giffuni } 202d7511a40SPedro F. Giffuni error = bread(ip->i_devvp, fsbtodb(fs, nblk), size, NOCRED, 203d7511a40SPedro F. Giffuni &path->ep_bp); 204d7511a40SPedro F. Giffuni if (error) { 205d7511a40SPedro F. Giffuni brelse(path->ep_bp); 206d7511a40SPedro F. Giffuni path->ep_bp = NULL; 207d7511a40SPedro F. Giffuni return (NULL); 208d7511a40SPedro F. Giffuni } 209d7511a40SPedro F. Giffuni ehp = (struct ext4_extent_header *)path->ep_bp->b_data; 210d7511a40SPedro F. Giffuni path->ep_header = ehp; 211d7511a40SPedro F. Giffuni } 212d7511a40SPedro F. Giffuni 213d7511a40SPedro F. Giffuni path->ep_depth = i; 214d7511a40SPedro F. Giffuni path->ep_ext = NULL; 215d7511a40SPedro F. Giffuni path->ep_index = NULL; 216e813d9d7SPedro F. Giffuni path->ep_is_sparse = 0; 217d7511a40SPedro F. Giffuni 218*78f6ea54SPedro F. Giffuni ext4_ext_binsearch(ip, path, lbn, first_lbn, last_lbn); 219d7511a40SPedro F. Giffuni return (path); 220d7511a40SPedro F. Giffuni } 221