1 /*	$OpenBSD: ext2fs_subr.c,v 1.12 2005/07/03 20:14:01 drahn 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 
43 #include <ufs/ufs/quota.h>
44 #include <ufs/ufs/inode.h>
45 #include <ufs/ufs/ufsmount.h>
46 
47 #include <ufs/ext2fs/ext2fs.h>
48 #include <ufs/ext2fs/ext2fs_extern.h>
49 
50 #ifdef _KERNEL
51 
52 /*
53  * Return buffer with the contents of block "offset" from the beginning of
54  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
55  * remaining space in the directory.
56  */
57 int
ext2fs_bufatoff(struct inode * ip,off_t offset,char ** res,struct buf ** bpp)58 ext2fs_bufatoff(struct inode *ip, off_t offset, char **res, struct buf **bpp)
59 {
60 	struct vnode *vp;
61 	struct m_ext2fs *fs;
62 	struct buf *bp;
63 	ufs1_daddr_t lbn;
64 	int error;
65 
66 	vp = ITOV(ip);
67 	fs = ip->i_e2fs;
68 	lbn = lblkno(fs, offset);
69 
70 	*bpp = NULL;
71 	if ((error = bread(vp, lbn, fs->e2fs_bsize, NOCRED, &bp)) != 0) {
72 		brelse(bp);
73 		return (error);
74 	}
75 	if (res)
76 		*res = (char *)bp->b_data + blkoff(fs, offset);
77 	*bpp = bp;
78 	return (0);
79 }
80 #endif
81 
82 #if defined(_KERNEL) && defined(DIAGNOSTIC)
83 void
ext2fs_checkoverlap(bp,ip)84 ext2fs_checkoverlap(bp, ip)
85 	struct buf *bp;
86 	struct inode *ip;
87 {
88 	struct buf *ebp, *ep;
89 	ufs1_daddr_t start, last;
90 	struct vnode *vp;
91 
92 	ebp = &buf[nbuf];
93 	start = bp->b_blkno;
94 	last = start + btodb(bp->b_bcount) - 1;
95 	for (ep = buf; ep < ebp; ep++) {
96 		if (ep == bp || (ep->b_flags & B_INVAL) ||
97 			ep->b_vp == NULLVP)
98 			continue;
99 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
100 			continue;
101 		if (vp != ip->i_devvp)
102 			continue;
103 		/* look for overlap */
104 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
105 			ep->b_blkno + btodb(ep->b_bcount) <= start)
106 			continue;
107 		vprint("Disk overlap", vp);
108 		printf("\tstart %d, end %d overlap start %d, end %ld\n",
109 			start, last, ep->b_blkno,
110 			ep->b_blkno + btodb(ep->b_bcount) - 1);
111 		panic("Disk buffer overlap");
112 	}
113 }
114 #endif /* DIAGNOSTIC */
115