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