xref: /netbsd-src/sys/ufs/ext2fs/ext2fs_subr.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: ext2fs_subr.c,v 1.9 2003/01/25 18:12:31 tron Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Manuel Bouyer.
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)ffs_subr.c	8.2 (Berkeley) 9/21/93
37  * Modified for ext2fs by Manuel Bouyer.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ext2fs_subr.c,v 1.9 2003/01/25 18:12:31 tron Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/vnode.h>
46 #include <sys/buf.h>
47 #include <sys/inttypes.h>
48 #include <ufs/ufs/inode.h>
49 #include <ufs/ext2fs/ext2fs.h>
50 #include <ufs/ext2fs/ext2fs_extern.h>
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_blkatoff(v)
59 	void *v;
60 {
61 	struct vop_blkatoff_args /* {
62 		struct vnode *a_vp;
63 		off_t a_offset;
64 		char **a_res;
65 		struct buf **a_bpp;
66 	} */ *ap = v;
67 	struct inode *ip;
68 	struct m_ext2fs *fs;
69 	struct buf *bp;
70 	daddr_t lbn;
71 	int error;
72 
73 	ip = VTOI(ap->a_vp);
74 	fs = ip->i_e2fs;
75 	lbn = lblkno(fs, ap->a_offset);
76 
77 	*ap->a_bpp = NULL;
78 	if ((error = bread(ap->a_vp, lbn, fs->e2fs_bsize, NOCRED, &bp)) != 0) {
79 		brelse(bp);
80 		return (error);
81 	}
82 	if (ap->a_res)
83 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
84 	*ap->a_bpp = bp;
85 	return (0);
86 }
87 
88 #ifdef DIAGNOSTIC
89 void
90 ext2fs_checkoverlap(bp, ip)
91 	struct buf *bp;
92 	struct inode *ip;
93 {
94 	struct buf *ebp, *ep;
95 	daddr_t start, last;
96 	struct vnode *vp;
97 
98 	ebp = &buf[nbuf];
99 	start = bp->b_blkno;
100 	last = start + btodb(bp->b_bcount) - 1;
101 	for (ep = buf; ep < ebp; ep++) {
102 		if (ep == bp || (ep->b_flags & B_INVAL) ||
103 			ep->b_vp == NULLVP)
104 			continue;
105 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL))
106 			continue;
107 		if (vp != ip->i_devvp)
108 			continue;
109 		/* look for overlap */
110 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
111 			ep->b_blkno + btodb(ep->b_bcount) <= start)
112 			continue;
113 		vprint("Disk overlap", vp);
114 		printf("\tstart %" PRId64 ", end %" PRId64 " overlap start "
115 			"%" PRId64 ", end %" PRId64 "\n",
116 			start, last, ep->b_blkno,
117 			ep->b_blkno + btodb(ep->b_bcount) - 1);
118 		panic("Disk buffer overlap");
119 	}
120 }
121 #endif
122