xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. 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  * $Id: ffs_subr.c,v 1.4 1995/05/30 08:15:00 rgrimes Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <ufs/ffs/fs.h>
39 
40 #ifdef KERNEL
41 #include <sys/systm.h>
42 #include <sys/vnode.h>
43 #include <ufs/ffs/ffs_extern.h>
44 #include <sys/buf.h>
45 #include <ufs/ufs/quota.h>
46 #include <ufs/ufs/inode.h>
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 ffs_blkatoff(ap)
55 	struct vop_blkatoff_args /* {
56 		struct vnode *a_vp;
57 		off_t a_offset;
58 		char **a_res;
59 		struct buf **a_bpp;
60 	} */ *ap;
61 {
62 	struct inode *ip;
63 	register struct fs *fs;
64 	struct buf *bp;
65 	daddr_t lbn;
66 	int bsize, error;
67 
68 	ip = VTOI(ap->a_vp);
69 	fs = ip->i_fs;
70 	lbn = lblkno(fs, ap->a_offset);
71 	bsize = blksize(fs, ip, lbn);
72 
73 	*ap->a_bpp = NULL;
74 	error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp);
75 	if (error) {
76 		brelse(bp);
77 		return (error);
78 	}
79 	if (ap->a_res)
80 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
81 	*ap->a_bpp = bp;
82 	return (0);
83 }
84 #endif
85 
86 /*
87  * Update the frsum fields to reflect addition or deletion
88  * of some frags.
89  */
90 void
91 ffs_fragacct(fs, fragmap, fraglist, cnt)
92 	struct fs *fs;
93 	int fragmap;
94 	long fraglist[];
95 	int cnt;
96 {
97 	int inblk;
98 	register int field, subfield;
99 	register int siz, pos;
100 
101 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
102 	fragmap <<= 1;
103 	for (siz = 1; siz < fs->fs_frag; siz++) {
104 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
105 			continue;
106 		field = around[siz];
107 		subfield = inside[siz];
108 		for (pos = siz; pos <= fs->fs_frag; pos++) {
109 			if ((fragmap & field) == subfield) {
110 				fraglist[siz] += cnt;
111 				pos += siz;
112 				field <<= siz;
113 				subfield <<= siz;
114 			}
115 			field <<= 1;
116 			subfield <<= 1;
117 		}
118 	}
119 }
120 
121 #if defined(KERNEL) && defined(DIAGNOSTIC)
122 void
123 ffs_checkoverlap(bp, ip)
124 	struct buf *bp;
125 	struct inode *ip;
126 {
127 	register struct buf *ebp, *ep;
128 	register daddr_t start, last;
129 	struct vnode *vp;
130 
131 	ebp = &buf[nbuf];
132 	start = bp->b_blkno;
133 	last = start + btodb(bp->b_bcount) - 1;
134 	for (ep = buf; ep < ebp; ep++) {
135 		if (ep == bp || (ep->b_flags & B_INVAL) ||
136 		    ep->b_vp == NULLVP)
137 			continue;
138 		if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0, NULL, NULL))
139 			continue;
140 		if (vp != ip->i_devvp)
141 			continue;
142 		/* look for overlap */
143 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
144 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
145 			continue;
146 		vprint("Disk overlap", vp);
147 		(void)printf("\tstart %lu, end %lu overlap start %lu, end %lu\n",
148 			(u_long)start, (u_long)last, (u_long)ep->b_blkno,
149 			(u_long)(ep->b_blkno + btodb(ep->b_bcount) - 1));
150 		panic("Disk buffer overlap");
151 	}
152 }
153 #endif /* DIAGNOSTIC */
154 
155 /*
156  * block operations
157  *
158  * check if a block is available
159  */
160 int
161 ffs_isblock(fs, cp, h)
162 	struct fs *fs;
163 	unsigned char *cp;
164 	daddr_t h;
165 {
166 	unsigned char mask;
167 
168 	switch ((int)fs->fs_frag) {
169 	case 8:
170 		return (cp[h] == 0xff);
171 	case 4:
172 		mask = 0x0f << ((h & 0x1) << 2);
173 		return ((cp[h >> 1] & mask) == mask);
174 	case 2:
175 		mask = 0x03 << ((h & 0x3) << 1);
176 		return ((cp[h >> 2] & mask) == mask);
177 	case 1:
178 		mask = 0x01 << (h & 0x7);
179 		return ((cp[h >> 3] & mask) == mask);
180 	default:
181 		panic("ffs_isblock");
182 	}
183 }
184 
185 /*
186  * take a block out of the map
187  */
188 void
189 ffs_clrblock(fs, cp, h)
190 	struct fs *fs;
191 	u_char *cp;
192 	daddr_t h;
193 {
194 
195 	switch ((int)fs->fs_frag) {
196 	case 8:
197 		cp[h] = 0;
198 		return;
199 	case 4:
200 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
201 		return;
202 	case 2:
203 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
204 		return;
205 	case 1:
206 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
207 		return;
208 	default:
209 		panic("ffs_clrblock");
210 	}
211 }
212 
213 /*
214  * put a block into the map
215  */
216 void
217 ffs_setblock(fs, cp, h)
218 	struct fs *fs;
219 	unsigned char *cp;
220 	daddr_t h;
221 {
222 
223 	switch ((int)fs->fs_frag) {
224 
225 	case 8:
226 		cp[h] = 0xff;
227 		return;
228 	case 4:
229 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
230 		return;
231 	case 2:
232 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
233 		return;
234 	case 1:
235 		cp[h >> 3] |= (0x01 << (h & 0x7));
236 		return;
237 	default:
238 		panic("ffs_setblock");
239 	}
240 }
241