ext2_subr.c revision 30474
112115Sdyson/*
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 */
712115Sdyson/*
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 * 3. All advertising materials mentioning features or use of this software
2012115Sdyson *    must display the following acknowledgement:
2112115Sdyson *	This product includes software developed by the University of
2212115Sdyson *	California, Berkeley and its contributors.
2312115Sdyson * 4. Neither the name of the University nor the names of its contributors
2412115Sdyson *    may be used to endorse or promote products derived from this software
2512115Sdyson *    without specific prior written permission.
2612115Sdyson *
2712115Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2812115Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2912115Sdyson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3012115Sdyson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3112115Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3212115Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3312115Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3412115Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3512115Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3612115Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3712115Sdyson * SUCH DAMAGE.
3812115Sdyson *
3912115Sdyson *	@(#)ext2_subr.c	8.2 (Berkeley) 9/21/93
4012115Sdyson */
4112115Sdyson
4212115Sdyson#include <sys/param.h>
4312115Sdyson#include <gnu/ext2fs/ext2_fs_sb.h>
4412115Sdyson#include <gnu/ext2fs/fs.h>
4512115Sdyson
4612115Sdyson#include <sys/systm.h>
4712115Sdyson#include <sys/vnode.h>
4812115Sdyson#include <gnu/ext2fs/ext2_extern.h>
4912115Sdyson#include <sys/buf.h>
5012115Sdyson#include <ufs/ufs/quota.h>
5112115Sdyson#include <ufs/ufs/inode.h>
5212115Sdyson
5312115Sdyson/*
5412115Sdyson * Return buffer with the contents of block "offset" from the beginning of
5512115Sdyson * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
5612115Sdyson * remaining space in the directory.
5712115Sdyson */
5812115Sdysonint
5930474Sphkext2_blkatoff(vp, offset, res, bpp)
6030474Sphk	struct vnode *vp;
6130474Sphk	off_t offset;
6230474Sphk	char **res;
6330474Sphk	struct buf **bpp;
6412115Sdyson{
6512115Sdyson	struct inode *ip;
6612115Sdyson	register struct ext2_sb_info *fs;
6712115Sdyson	struct buf *bp;
6812115Sdyson	daddr_t lbn;
6912115Sdyson	int bsize, error;
7012115Sdyson
7130474Sphk	ip = VTOI(vp);
7212115Sdyson	fs = ip->i_e2fs;
7330474Sphk	lbn = lblkno(fs, offset);
7412115Sdyson	bsize = blksize(fs, ip, lbn);
7512115Sdyson
7630474Sphk	*bpp = NULL;
7730474Sphk	if (error = bread(vp, lbn, bsize, NOCRED, &bp)) {
7812115Sdyson		brelse(bp);
7912115Sdyson		return (error);
8012115Sdyson	}
8130474Sphk	if (res)
8230474Sphk		*res = (char *)bp->b_data + blkoff(fs, offset);
8330474Sphk	*bpp = bp;
8412115Sdyson	return (0);
8512115Sdyson}
8612115Sdyson
8712115Sdyson#if defined(KERNEL) && defined(DIAGNOSTIC)
8812115Sdysonvoid
8912115Sdysonext2_checkoverlap(bp, ip)
9012115Sdyson	struct buf *bp;
9112115Sdyson	struct inode *ip;
9212115Sdyson{
9312115Sdyson	register struct buf *ebp, *ep;
9412115Sdyson	register daddr_t start, last;
9512115Sdyson	struct vnode *vp;
9612115Sdyson
9712115Sdyson	ebp = &buf[nbuf];
9812115Sdyson	start = bp->b_blkno;
9912115Sdyson	last = start + btodb(bp->b_bcount) - 1;
10012115Sdyson	for (ep = buf; ep < ebp; ep++) {
10112115Sdyson		if (ep == bp || (ep->b_flags & B_INVAL) ||
10212115Sdyson		    ep->b_vp == NULLVP)
10312115Sdyson			continue;
10412115Sdyson#if !defined(__FreeBSD__)
10512115Sdyson		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
10612115Sdyson			continue;
10712115Sdyson#else
10812115Sdyson		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL, NULL))
10912115Sdyson			continue;
11012115Sdyson#endif
11112115Sdyson		if (vp != ip->i_devvp)
11212115Sdyson			continue;
11312115Sdyson		/* look for overlap */
11412115Sdyson		if (ep->b_bcount == 0 || ep->b_blkno > last ||
11512115Sdyson		    ep->b_blkno + btodb(ep->b_bcount) <= start)
11612115Sdyson			continue;
11712115Sdyson		vprint("Disk overlap", vp);
11812115Sdyson		(void)printf("\tstart %d, end %d overlap start %d, end %d\n",
11912115Sdyson			start, last, ep->b_blkno,
12012115Sdyson			ep->b_blkno + btodb(ep->b_bcount) - 1);
12112115Sdyson		panic("Disk buffer overlap");
12212115Sdyson	}
12312115Sdyson}
12412115Sdyson#endif /* DIAGNOSTIC */
12512115Sdyson
126