1 /* $OpenBSD: ext2fs_subr.c,v 1.32 2014/07/13 16:59:35 pelikan Exp $ */ 2 /* $NetBSD: ext2fs_subr.c,v 1.1 1997/06/11 09:34:03 bouyer Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Manuel Bouyer. 6 * Copyright (c) 1982, 1986, 1989, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ffs_subr.c 8.2 (Berkeley) 9/21/93 34 * Modified for ext2fs by Manuel Bouyer. 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/vnode.h> 40 #include <sys/mount.h> 41 #include <sys/buf.h> 42 #include <sys/specdev.h> 43 44 #include <ufs/ufs/quota.h> 45 #include <ufs/ufs/inode.h> 46 #include <ufs/ufs/ufsmount.h> 47 48 #include <ufs/ext2fs/ext2fs.h> 49 #include <ufs/ext2fs/ext2fs_extern.h> 50 #include <ufs/ext2fs/ext2fs_extents.h> 51 52 #include <miscfs/fifofs/fifo.h> 53 54 union _qcvt { 55 int64_t qcvt; 56 int32_t val[2]; 57 }; 58 59 #define SETHIGH(q, h) { \ 60 union _qcvt tmp; \ 61 tmp.qcvt = (q); \ 62 tmp.val[_QUAD_HIGHWORD] = (h); \ 63 (q) = tmp.qcvt; \ 64 } 65 66 #define SETLOW(q, l) { \ 67 union _qcvt tmp; \ 68 tmp.qcvt = (q); \ 69 tmp.val[_QUAD_LOWWORD] = (l); \ 70 (q) = tmp.qcvt; \ 71 } 72 73 #ifdef _KERNEL 74 75 /* 76 * Return buffer with the contents of block "offset" from the beginning of 77 * directory "ip". If "res" is non-zero, fill it in with a pointer to the 78 * remaining space in the directory. 79 */ 80 int 81 ext2fs_bufatoff(struct inode *ip, off_t offset, char **res, struct buf **bpp) 82 { 83 struct vnode *vp; 84 struct m_ext2fs *fs; 85 struct buf *bp; 86 daddr_t lbn, pos; 87 int error; 88 89 vp = ITOV(ip); 90 fs = ip->i_e2fs; 91 lbn = lblkno(fs, offset); 92 93 if (ip->i_e2din->e2di_flags & EXT4_EXTENTS) { 94 struct ext4_extent_path path; 95 struct ext4_extent *ep; 96 97 memset(&path, 0, sizeof path); 98 if (ext4_ext_find_extent(fs, ip, lbn, &path) == NULL || 99 (ep = path.ep_ext) == NULL) 100 goto normal; 101 102 if (path.ep_bp != NULL) { 103 brelse(path.ep_bp); 104 path.ep_bp = NULL; 105 } 106 pos = lbn - ep->e_blk + (((daddr_t)ep->e_start_hi << 32) | ep->e_start_lo); 107 error = bread(ip->i_devvp, fsbtodb(fs, pos), fs->e2fs_bsize, &bp); 108 if (error) { 109 brelse(bp); 110 return (error); 111 } 112 113 if (res) 114 *res = (char *)bp->b_data + blkoff(fs, offset); 115 116 *bpp = bp; 117 118 return (0); 119 } 120 121 normal: 122 *bpp = NULL; 123 if ((error = bread(vp, lbn, fs->e2fs_bsize, &bp)) != 0) { 124 brelse(bp); 125 return (error); 126 } 127 if (res) 128 *res = (char *)bp->b_data + blkoff(fs, offset); 129 *bpp = bp; 130 return (0); 131 } 132 #endif 133 134 #if defined(_KERNEL) && defined(DIAGNOSTIC) 135 void 136 ext2fs_checkoverlap(struct buf *bp, struct inode *ip) 137 { 138 struct buf *ep; 139 struct vnode *vp; 140 daddr_t start, last; 141 142 start = bp->b_blkno; 143 last = start + btodb(bp->b_bcount) - 1; 144 LIST_FOREACH(ep, &bufhead, b_list) { 145 if (ep == bp || (ep->b_flags & B_INVAL) || 146 ep->b_vp == NULLVP) 147 continue; 148 if (VOP_BMAP(ep->b_vp, 0, &vp, NULL, NULL)) 149 continue; 150 if (vp != ip->i_devvp) 151 continue; 152 /* look for overlap */ 153 if (ep->b_bcount == 0 || ep->b_blkno > last || 154 ep->b_blkno + btodb(ep->b_bcount) <= start) 155 continue; 156 vprint("Disk overlap", vp); 157 printf("\tstart %lld, end %lld overlap start %lld, end %lld\n", 158 start, last, (long long)ep->b_blkno, 159 (long long)(ep->b_blkno + btodb(ep->b_bcount) - 1)); 160 panic("Disk buffer overlap"); 161 } 162 } 163 #endif /* DIAGNOSTIC */ 164 165 /* 166 * Initialize the vnode associated with a new inode, handle aliased vnodes. 167 */ 168 int 169 ext2fs_vinit(struct mount *mp, struct vops *specops, 170 struct vops *fifoops, struct vnode **vpp) 171 { 172 struct inode *ip; 173 struct vnode *vp, *nvp; 174 struct timeval tv; 175 176 vp = *vpp; 177 ip = VTOI(vp); 178 vp->v_type = IFTOVT(ip->i_e2fs_mode); 179 180 switch(vp->v_type) { 181 case VCHR: 182 case VBLK: 183 vp->v_op = specops; 184 185 nvp = checkalias(vp, letoh32(ip->i_e2din->e2di_rdev), mp); 186 if (nvp != NULL) { 187 /* 188 * Discard unneeded vnode, but save its inode. Note 189 * that the lock is carried over in the inode to the 190 * replacement vnode. 191 */ 192 nvp->v_data = vp->v_data; 193 vp->v_data = NULL; 194 vp->v_op = &spec_vops; 195 #ifdef VFSLCKDEBUG 196 vp->v_flag &= ~VLOCKSWORK; 197 #endif 198 vrele(vp); 199 vgone(vp); 200 /* Reinitialize aliased vnode. */ 201 vp = nvp; 202 ip->i_vnode = vp; 203 } 204 205 break; 206 207 case VFIFO: 208 #ifdef FIFO 209 vp->v_op = fifoops; 210 break; 211 #else 212 return (EOPNOTSUPP); 213 #endif /* FIFO */ 214 215 default: 216 217 break; 218 } 219 220 if (ip->i_number == EXT2_ROOTINO) 221 vp->v_flag |= VROOT; 222 223 /* Initialize modrev times */ 224 getmicrouptime(&tv); 225 SETHIGH(ip->i_modrev, tv.tv_sec); 226 SETLOW(ip->i_modrev, tv.tv_usec * 4294); 227 228 *vpp = vp; 229 230 return (0); 231 } 232