Deleted Added
full compact
ffs_subr.c (55206) ffs_subr.c (60041)
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
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 $
34 * $FreeBSD: head/sys/ufs/ffs/ffs_subr.c 60041 2000-05-05 09:59:14Z phk $
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>
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>
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}
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
57void ffs_checkoverlap __P((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 */
65int
66ffs_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 register 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 */
100void
101ffs_fragacct(fs, fragmap, fraglist, cnt)
102 struct fs *fs;
103 int fragmap;
104 int32_t fraglist[];
105 int cnt;
106{
107 int inblk;
108 register int field, subfield;
109 register 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
132void
133ffs_checkoverlap(bp, ip)
134 struct buf *bp;
135 struct inode *ip;
136{
137 register struct buf *ebp, *ep;
138 register 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 if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t *)NULL,
149 (int *)NULL, (int *)NULL))
150 continue;
151 if (vp != ip->i_devvp)
152 continue;
153 /* look for overlap */
154 if (ep->b_bcount == 0 || ep->b_blkno > last ||
155 ep->b_blkno + btodb(ep->b_bcount) <= start)
156 continue;
157 vprint("Disk overlap", vp);
158 (void)printf("\tstart %lu, end %lu overlap start %lu, end %lu\n",
159 (u_long)start, (u_long)last, (u_long)ep->b_blkno,
160 (u_long)(ep->b_blkno + btodb(ep->b_bcount) - 1));
161 panic("ffs_checkoverlap: Disk buffer overlap");
162 }
163}
164#endif /* DDB */
165
166/*
167 * block operations
168 *
169 * check if a block is available
170 */
171int
172ffs_isblock(fs, cp, h)
173 struct fs *fs;
174 unsigned char *cp;
175 ufs_daddr_t h;
176{
177 unsigned char mask;
178
179 switch ((int)fs->fs_frag) {
180 case 8:
181 return (cp[h] == 0xff);
182 case 4:
183 mask = 0x0f << ((h & 0x1) << 2);
184 return ((cp[h >> 1] & mask) == mask);
185 case 2:
186 mask = 0x03 << ((h & 0x3) << 1);
187 return ((cp[h >> 2] & mask) == mask);
188 case 1:
189 mask = 0x01 << (h & 0x7);
190 return ((cp[h >> 3] & mask) == mask);
191 default:
192 panic("ffs_isblock");
193 }
194}
195
196/*
197 * check if a block is free
198 */
199int
200ffs_isfreeblock(fs, cp, h)
201 struct fs *fs;
202 unsigned char *cp;
203 ufs_daddr_t h;
204{
205
206 switch ((int)fs->fs_frag) {
207 case 8:
208 return (cp[h] == 0);
209 case 4:
210 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
211 case 2:
212 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
213 case 1:
214 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
215 default:
216 panic("ffs_isfreeblock");
217 }
218}
219
220/*
221 * take a block out of the map
222 */
223void
224ffs_clrblock(fs, cp, h)
225 struct fs *fs;
226 u_char *cp;
227 ufs_daddr_t h;
228{
229
230 switch ((int)fs->fs_frag) {
231 case 8:
232 cp[h] = 0;
233 return;
234 case 4:
235 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
236 return;
237 case 2:
238 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
239 return;
240 case 1:
241 cp[h >> 3] &= ~(0x01 << (h & 0x7));
242 return;
243 default:
244 panic("ffs_clrblock");
245 }
246}
247
248/*
249 * put a block into the map
250 */
251void
252ffs_setblock(fs, cp, h)
253 struct fs *fs;
254 unsigned char *cp;
255 ufs_daddr_t h;
256{
257
258 switch ((int)fs->fs_frag) {
259
260 case 8:
261 cp[h] = 0xff;
262 return;
263 case 4:
264 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
265 return;
266 case 2:
267 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
268 return;
269 case 1:
270 cp[h >> 3] |= (0x01 << (h & 0x7));
271 return;
272 default:
273 panic("ffs_setblock");
274 }
275}