xref: /openbsd-src/sys/ufs/ext2fs/ext2fs_subr.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: ext2fs_subr.c,v 1.8 2001/12/19 08:58:07 art 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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)ffs_subr.c	8.2 (Berkeley) 9/21/93
38  * Modified for ext2fs by Manuel Bouyer.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 #include <sys/buf.h>
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/inode.h>
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
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 	ufs_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
84 ext2fs_checkoverlap(bp, ip)
85 	struct buf *bp;
86 	struct inode *ip;
87 {
88 	struct buf *ebp, *ep;
89 	ufs_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