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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
42 #include <sys/systm.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
49 #include <sys/ucred.h>
51 #include <ufs/ufs/quota.h>
52 #include <ufs/ufs/inode.h>
53 #include <ufs/ufs/extattr.h>
54 #include <ufs/ufs/ufsmount.h>
55 #include <ufs/ufs/ufs_extern.h>
56 #include <ufs/ffs/ffs_extern.h>
57 #include <ufs/ffs/fs.h>
60 void ffs_checkoverlap(struct buf *, struct inode *);
64 * Return buffer with the contents of block "offset" from the beginning of
65 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
66 * remaining space in the directory.
69 ffs_blkatoff(vp, offset, res, bpp)
83 lbn = lblkno(fs, offset);
84 bsize = blksize(fs, ip, lbn);
87 error = bread(vp, lbn, bsize, NOCRED, &bp);
93 *res = (char *)bp->b_data + blkoff(fs, offset);
99 * Load up the contents of an inode and copy the appropriate pieces
100 * to the incore copy.
103 ffs_load_inode(bp, ip, fs, ino)
110 if (ip->i_ump->um_fstype == UFS1) {
112 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
113 ip->i_mode = ip->i_din1->di_mode;
114 ip->i_nlink = ip->i_din1->di_nlink;
115 ip->i_size = ip->i_din1->di_size;
116 ip->i_flags = ip->i_din1->di_flags;
117 ip->i_gen = ip->i_din1->di_gen;
118 ip->i_uid = ip->i_din1->di_uid;
119 ip->i_gid = ip->i_din1->di_gid;
122 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
123 ip->i_mode = ip->i_din2->di_mode;
124 ip->i_nlink = ip->i_din2->di_nlink;
125 ip->i_size = ip->i_din2->di_size;
126 ip->i_flags = ip->i_din2->di_flags;
127 ip->i_gen = ip->i_din2->di_gen;
128 ip->i_uid = ip->i_din2->di_uid;
129 ip->i_gid = ip->i_din2->di_gid;
135 * Update the frsum fields to reflect addition or deletion
139 ffs_fragacct(fs, fragmap, fraglist, cnt)
149 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
151 for (siz = 1; siz < fs->fs_frag; siz++) {
152 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
155 subfield = inside[siz];
156 for (pos = siz; pos <= fs->fs_frag; pos++) {
157 if ((fragmap & field) == subfield) {
158 fraglist[siz] += cnt;
171 ffs_checkoverlap(bp, ip)
175 struct buf *ebp, *ep;
176 ufs2_daddr_t start, last;
181 last = start + btodb(bp->b_bcount) - 1;
182 for (ep = buf; ep < ebp; ep++) {
183 if (ep == bp || (ep->b_flags & B_INVAL) ||
187 /* look for overlap */
188 if (ep->b_bcount == 0 || ep->b_blkno > last ||
189 ep->b_blkno + btodb(ep->b_bcount) <= start)
191 vprint("Disk overlap", vp);
192 printf("\tstart %jd, end %jd overlap start %jd, end %jd\n",
193 (intmax_t)start, (intmax_t)last, (intmax_t)ep->b_blkno,
194 (intmax_t)(ep->b_blkno + btodb(ep->b_bcount) - 1));
195 panic("ffs_checkoverlap: Disk buffer overlap");
203 * check if a block is available
206 ffs_isblock(fs, cp, h)
213 switch ((int)fs->fs_frag) {
215 return (cp[h] == 0xff);
217 mask = 0x0f << ((h & 0x1) << 2);
218 return ((cp[h >> 1] & mask) == mask);
220 mask = 0x03 << ((h & 0x3) << 1);
221 return ((cp[h >> 2] & mask) == mask);
223 mask = 0x01 << (h & 0x7);
224 return ((cp[h >> 3] & mask) == mask);
226 panic("ffs_isblock");
232 * take a block out of the map
235 ffs_clrblock(fs, cp, h)
241 switch ((int)fs->fs_frag) {
246 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
249 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
252 cp[h >> 3] &= ~(0x01 << (h & 0x7));
255 panic("ffs_clrblock");
260 * put a block into the map
263 ffs_setblock(fs, cp, h)
269 switch ((int)fs->fs_frag) {
275 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
278 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
281 cp[h >> 3] |= (0x01 << (h & 0x7));
284 panic("ffs_setblock");