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