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