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 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>
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
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}
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 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>
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
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}