ext2_subr.c revision 217582
1139778Simp/*- 212115Sdyson * modified for Lites 1.1 312115Sdyson * 412115Sdyson * Aug 1995, Godmar Back (gback@cs.utah.edu) 512115Sdyson * University of Utah, Department of Computer Science 612115Sdyson */ 7139778Simp/*- 812115Sdyson * Copyright (c) 1982, 1986, 1989, 1993 912115Sdyson * The Regents of the University of California. All rights reserved. 1012115Sdyson * 1112115Sdyson * Redistribution and use in source and binary forms, with or without 1212115Sdyson * modification, are permitted provided that the following conditions 1312115Sdyson * are met: 1412115Sdyson * 1. Redistributions of source code must retain the above copyright 1512115Sdyson * notice, this list of conditions and the following disclaimer. 1612115Sdyson * 2. Redistributions in binary form must reproduce the above copyright 1712115Sdyson * notice, this list of conditions and the following disclaimer in the 1812115Sdyson * documentation and/or other materials provided with the distribution. 1912115Sdyson * 4. Neither the name of the University nor the names of its contributors 2012115Sdyson * may be used to endorse or promote products derived from this software 2112115Sdyson * without specific prior written permission. 2212115Sdyson * 2312115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2412115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2512115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2612115Sdyson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2712115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2812115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2912115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3012115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3212115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3312115Sdyson * SUCH DAMAGE. 3412115Sdyson * 3593015Sbde * @(#)ffs_subr.c 8.2 (Berkeley) 9/21/93 3660041Sphk * $FreeBSD: head/sys/fs/ext2fs/ext2_subr.c 217582 2011-01-19 16:46:13Z jhb $ 3712115Sdyson */ 3812115Sdyson 3912115Sdyson#include <sys/param.h> 4012115Sdyson 4176166Smarkm#include <sys/proc.h> 4276166Smarkm#include <sys/systm.h> 4376166Smarkm#include <sys/bio.h> 4476166Smarkm#include <sys/buf.h> 4531561Sbde#include <sys/lock.h> 4669517Sbde#include <sys/ucred.h> 4712115Sdyson#include <sys/vnode.h> 4876166Smarkm 49202283Slulf#include <fs/ext2fs/inode.h> 50202283Slulf#include <fs/ext2fs/ext2_extern.h> 51202283Slulf#include <fs/ext2fs/ext2fs.h> 52202283Slulf#include <fs/ext2fs/fs.h> 5376166Smarkm 54131925Smarcel#ifdef KDB 5592728Salfredvoid ext2_checkoverlap(struct buf *, struct inode *); 5641591Sarchie#endif 5733291Sbde 5812115Sdyson/* 5912115Sdyson * Return buffer with the contents of block "offset" from the beginning of 6012115Sdyson * directory "ip". If "res" is non-zero, fill it in with a pointer to the 6112115Sdyson * remaining space in the directory. 6212115Sdyson */ 6312115Sdysonint 6430474Sphkext2_blkatoff(vp, offset, res, bpp) 6530474Sphk struct vnode *vp; 6630474Sphk off_t offset; 6730474Sphk char **res; 6830474Sphk struct buf **bpp; 6912115Sdyson{ 7012115Sdyson struct inode *ip; 71202283Slulf struct m_ext2fs *fs; 7212115Sdyson struct buf *bp; 7396877Siedowse int32_t lbn; 7412115Sdyson int bsize, error; 7512115Sdyson 7630474Sphk ip = VTOI(vp); 7712115Sdyson fs = ip->i_e2fs; 7830474Sphk lbn = lblkno(fs, offset); 7912115Sdyson bsize = blksize(fs, ip, lbn); 8012115Sdyson 8130474Sphk *bpp = NULL; 8243301Sdillon if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) { 8312115Sdyson brelse(bp); 8412115Sdyson return (error); 8512115Sdyson } 8630474Sphk if (res) 8730474Sphk *res = (char *)bp->b_data + blkoff(fs, offset); 8830474Sphk *bpp = bp; 8912115Sdyson return (0); 9012115Sdyson} 9112115Sdyson 92131925Smarcel#ifdef KDB 9342539Seivindvoid 9412115Sdysonext2_checkoverlap(bp, ip) 9512115Sdyson struct buf *bp; 9612115Sdyson struct inode *ip; 9712115Sdyson{ 9896752Siedowse struct buf *ebp, *ep; 9996877Siedowse int32_t start, last; 10012115Sdyson struct vnode *vp; 10112115Sdyson 10212115Sdyson ebp = &buf[nbuf]; 10312115Sdyson start = bp->b_blkno; 10412115Sdyson last = start + btodb(bp->b_bcount) - 1; 10512115Sdyson for (ep = buf; ep < ebp; ep++) { 106136991Sphk if (ep == bp || (ep->b_flags & B_INVAL)) 10712115Sdyson continue; 108217582Sjhb vp = ip->i_ump->um_devvp; 10912115Sdyson /* look for overlap */ 11012115Sdyson if (ep->b_bcount == 0 || ep->b_blkno > last || 11112115Sdyson ep->b_blkno + btodb(ep->b_bcount) <= start) 11212115Sdyson continue; 11312115Sdyson vprint("Disk overlap", vp); 11492363Smckusick (void)printf("\tstart %d, end %d overlap start %lld, end %ld\n", 11596749Siedowse start, last, (long long)ep->b_blkno, 11669807Smjacob (long)(ep->b_blkno + btodb(ep->b_bcount) - 1)); 11712115Sdyson panic("Disk buffer overlap"); 11812115Sdyson } 11912115Sdyson} 120131925Smarcel#endif /* KDB */ 121