2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
34 * $FreeBSD: src/sys/ufs/ffs/ffs_subr.c,v 1.25 1999/12/29 04:55:04 peter Exp $
37 #include <sys/param.h>
38 #include <ufs/ffs/fs.h>
41 #include <ufs/ufs/dinode.h>
45 #include <sys/systm.h>
47 #include <sys/vnode.h>
49 #include <sys/ucred.h>
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ffs/ffs_extern.h>
56 void ffs_checkoverlap __P((struct buf *, struct inode *));
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.
65 ffs_blkatoff(vp, offset, res, bpp)
72 register struct fs *fs;
79 lbn = lblkno(fs, offset);
80 bsize = blksize(fs, ip, lbn);
83 error = bread(vp, lbn, bsize, NOCRED, &bp);
89 *res = (char *)bp->b_data + blkoff(fs, offset);
96 * Update the frsum fields to reflect addition or deletion
100 ffs_fragacct(fs, fragmap, fraglist, cnt)
107 register int field, subfield;
108 register int siz, pos;
110 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
112 for (siz = 1; siz < fs->fs_frag; siz++) {
113 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
116 subfield = inside[siz];
117 for (pos = siz; pos <= fs->fs_frag; pos++) {
118 if ((fragmap & field) == subfield) {
119 fraglist[siz] += cnt;
132 ffs_checkoverlap(bp, ip)
136 register struct buf *ebp, *ep;
137 register ufs_daddr_t start, last;
142 last = start + btodb(bp->b_bcount) - 1;
143 for (ep = buf; ep < ebp; ep++) {
144 if (ep == bp || (ep->b_flags & B_INVAL) ||
147 if (VOP_BMAP(ep->b_vp, (ufs_daddr_t)0, &vp, (ufs_daddr_t *)NULL,
148 (int *)NULL, (int *)NULL))
150 if (vp != ip->i_devvp)
152 /* look for overlap */
153 if (ep->b_bcount == 0 || ep->b_blkno > last ||
154 ep->b_blkno + btodb(ep->b_bcount) <= start)
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");
168 * check if a block is available
171 ffs_isblock(fs, cp, h)
178 switch ((int)fs->fs_frag) {
180 return (cp[h] == 0xff);
182 mask = 0x0f << ((h & 0x1) << 2);
183 return ((cp[h >> 1] & mask) == mask);
185 mask = 0x03 << ((h & 0x3) << 1);
186 return ((cp[h >> 2] & mask) == mask);
188 mask = 0x01 << (h & 0x7);
189 return ((cp[h >> 3] & mask) == mask);
191 panic("ffs_isblock");
196 * check if a block is free
199 ffs_isfreeblock(fs, cp, h)
205 switch ((int)fs->fs_frag) {
209 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
211 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
213 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
215 panic("ffs_isfreeblock");
220 * take a block out of the map
223 ffs_clrblock(fs, cp, h)
229 switch ((int)fs->fs_frag) {
234 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
237 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
240 cp[h >> 3] &= ~(0x01 << (h & 0x7));
243 panic("ffs_clrblock");
248 * put a block into the map
251 ffs_setblock(fs, cp, h)
257 switch ((int)fs->fs_frag) {
263 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
266 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
269 cp[h >> 3] |= (0x01 << (h & 0x7));
272 panic("ffs_setblock");