xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 2a4a1db342263067035ce69a4017c645da63455d)
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.5 (Berkeley) 3/21/95
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <ufs/ffs/fs.h>
39 
40 #ifndef _KERNEL
41 #include <ufs/ufs/dinode.h>
42 #else
43 #include "opt_ddb.h"
44 
45 #include <sys/systm.h>
46 #include <sys/lock.h>
47 #include <sys/vnode.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/ucred.h>
51 
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ffs/ffs_extern.h>
55 
56 #ifdef DDB
57 void	ffs_checkoverlap(struct buf *, struct inode *);
58 #endif
59 
60 /*
61  * Return buffer with the contents of block "offset" from the beginning of
62  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
63  * remaining space in the directory.
64  */
65 int
66 ffs_blkatoff(vp, offset, res, bpp)
67 	struct vnode *vp;
68 	off_t offset;
69 	char **res;
70 	struct buf **bpp;
71 {
72 	struct inode *ip;
73 	struct fs *fs;
74 	struct buf *bp;
75 	ufs_daddr_t lbn;
76 	int bsize, error;
77 
78 	ip = VTOI(vp);
79 	fs = ip->i_fs;
80 	lbn = lblkno(fs, offset);
81 	bsize = blksize(fs, ip, lbn);
82 
83 	*bpp = NULL;
84 	error = bread(vp, lbn, bsize, NOCRED, &bp);
85 	if (error) {
86 		brelse(bp);
87 		return (error);
88 	}
89 	if (res)
90 		*res = (char *)bp->b_data + blkoff(fs, offset);
91 	*bpp = bp;
92 	return (0);
93 }
94 #endif
95 
96 /*
97  * Update the frsum fields to reflect addition or deletion
98  * of some frags.
99  */
100 void
101 ffs_fragacct(fs, fragmap, fraglist, cnt)
102 	struct fs *fs;
103 	int fragmap;
104 	int32_t fraglist[];
105 	int cnt;
106 {
107 	int inblk;
108 	int field, subfield;
109 	int siz, pos;
110 
111 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
112 	fragmap <<= 1;
113 	for (siz = 1; siz < fs->fs_frag; siz++) {
114 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
115 			continue;
116 		field = around[siz];
117 		subfield = inside[siz];
118 		for (pos = siz; pos <= fs->fs_frag; pos++) {
119 			if ((fragmap & field) == subfield) {
120 				fraglist[siz] += cnt;
121 				pos += siz;
122 				field <<= siz;
123 				subfield <<= siz;
124 			}
125 			field <<= 1;
126 			subfield <<= 1;
127 		}
128 	}
129 }
130 
131 #ifdef DDB
132 void
133 ffs_checkoverlap(bp, ip)
134 	struct buf *bp;
135 	struct inode *ip;
136 {
137 	struct buf *ebp, *ep;
138 	ufs_daddr_t start, last;
139 	struct vnode *vp;
140 
141 	ebp = &buf[nbuf];
142 	start = bp->b_blkno;
143 	last = start + btodb(bp->b_bcount) - 1;
144 	for (ep = buf; ep < ebp; ep++) {
145 		if (ep == bp || (ep->b_flags & B_INVAL) ||
146 		    ep->b_vp == NULLVP)
147 			continue;
148 		vp = ip->i_devvp;
149 		/* look for overlap */
150 		if (ep->b_bcount == 0 || ep->b_blkno > last ||
151 		    ep->b_blkno + btodb(ep->b_bcount) <= start)
152 			continue;
153 		vprint("Disk overlap", vp);
154 		(void)printf("\tstart %lu, end %lu overlap start %lu, end %lu\n",
155 			(u_long)start, (u_long)last, (u_long)ep->b_blkno,
156 			(u_long)(ep->b_blkno + btodb(ep->b_bcount) - 1));
157 		panic("ffs_checkoverlap: Disk buffer overlap");
158 	}
159 }
160 #endif /* DDB */
161 
162 /*
163  * block operations
164  *
165  * check if a block is available
166  */
167 int
168 ffs_isblock(fs, cp, h)
169 	struct fs *fs;
170 	unsigned char *cp;
171 	ufs_daddr_t h;
172 {
173 	unsigned char mask;
174 
175 	switch ((int)fs->fs_frag) {
176 	case 8:
177 		return (cp[h] == 0xff);
178 	case 4:
179 		mask = 0x0f << ((h & 0x1) << 2);
180 		return ((cp[h >> 1] & mask) == mask);
181 	case 2:
182 		mask = 0x03 << ((h & 0x3) << 1);
183 		return ((cp[h >> 2] & mask) == mask);
184 	case 1:
185 		mask = 0x01 << (h & 0x7);
186 		return ((cp[h >> 3] & mask) == mask);
187 	default:
188 		panic("ffs_isblock");
189 	}
190 	return (0);
191 }
192 
193 /*
194  * check if a block is free
195  */
196 int
197 ffs_isfreeblock(fs, cp, h)
198 	struct fs *fs;
199 	unsigned char *cp;
200 	ufs_daddr_t h;
201 {
202 
203 	switch ((int)fs->fs_frag) {
204 	case 8:
205 		return (cp[h] == 0);
206 	case 4:
207 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
208 	case 2:
209 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
210 	case 1:
211 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
212 	default:
213 		panic("ffs_isfreeblock");
214 	}
215 	return (0);
216 }
217 
218 /*
219  * take a block out of the map
220  */
221 void
222 ffs_clrblock(fs, cp, h)
223 	struct fs *fs;
224 	u_char *cp;
225 	ufs_daddr_t h;
226 {
227 
228 	switch ((int)fs->fs_frag) {
229 	case 8:
230 		cp[h] = 0;
231 		return;
232 	case 4:
233 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
234 		return;
235 	case 2:
236 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
237 		return;
238 	case 1:
239 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
240 		return;
241 	default:
242 		panic("ffs_clrblock");
243 	}
244 }
245 
246 /*
247  * put a block into the map
248  */
249 void
250 ffs_setblock(fs, cp, h)
251 	struct fs *fs;
252 	unsigned char *cp;
253 	ufs_daddr_t h;
254 {
255 
256 	switch ((int)fs->fs_frag) {
257 
258 	case 8:
259 		cp[h] = 0xff;
260 		return;
261 	case 4:
262 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
263 		return;
264 	case 2:
265 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
266 		return;
267 	case 1:
268 		cp[h >> 3] |= (0x01 << (h & 0x7));
269 		return;
270 	default:
271 		panic("ffs_setblock");
272 	}
273 }
274