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>
41 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/vnode.h>
48 #include <sys/ucred.h>
50 #include <ufs/ufs/quota.h>
51 #include <ufs/ufs/inode.h>
52 #include <ufs/ufs/extattr.h>
53 #include <ufs/ufs/ufsmount.h>
54 #include <ufs/ufs/ufs_extern.h>
55 #include <ufs/ffs/ffs_extern.h>
56 #include <ufs/ffs/fs.h>
59 void ffs_checkoverlap(struct buf *, struct inode *);
63 * Return buffer with the contents of block "offset" from the beginning of
64 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
65 * remaining space in the directory.
68 ffs_blkatoff(vp, offset, res, bpp)
82 lbn = lblkno(fs, offset);
83 bsize = blksize(fs, ip, lbn);
86 error = bread(vp, lbn, bsize, NOCRED, &bp);
92 *res = (char *)bp->b_data + blkoff(fs, offset);
98 * Load up the contents of an inode and copy the appropriate pieces
102 ffs_load_inode(bp, ip, fs, ino)
109 if (ip->i_ump->um_fstype == UFS1) {
111 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
112 ip->i_mode = ip->i_din1->di_mode;
113 ip->i_nlink = ip->i_din1->di_nlink;
114 ip->i_size = ip->i_din1->di_size;
115 ip->i_flags = ip->i_din1->di_flags;
116 ip->i_gen = ip->i_din1->di_gen;
117 ip->i_uid = ip->i_din1->di_uid;
118 ip->i_gid = ip->i_din1->di_gid;
121 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
122 ip->i_mode = ip->i_din2->di_mode;
123 ip->i_nlink = ip->i_din2->di_nlink;
124 ip->i_size = ip->i_din2->di_size;
125 ip->i_flags = ip->i_din2->di_flags;
126 ip->i_gen = ip->i_din2->di_gen;
127 ip->i_uid = ip->i_din2->di_uid;
128 ip->i_gid = ip->i_din2->di_gid;
134 * Update the frsum fields to reflect addition or deletion
138 ffs_fragacct(fs, fragmap, fraglist, cnt)
148 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
150 for (siz = 1; siz < fs->fs_frag; siz++) {
151 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
154 subfield = inside[siz];
155 for (pos = siz; pos <= fs->fs_frag; pos++) {
156 if ((fragmap & field) == subfield) {
157 fraglist[siz] += cnt;
170 ffs_checkoverlap(bp, ip)
174 struct buf *ebp, *ep;
175 ufs2_daddr_t start, last;
180 last = start + btodb(bp->b_bcount) - 1;
181 for (ep = buf; ep < ebp; ep++) {
182 if (ep == bp || (ep->b_flags & B_INVAL) ||
186 /* look for overlap */
187 if (ep->b_bcount == 0 || ep->b_blkno > last ||
188 ep->b_blkno + btodb(ep->b_bcount) <= start)
190 vprint("Disk overlap", vp);
191 printf("\tstart %jd, end %jd overlap start %jd, end %jd\n",
192 (intmax_t)start, (intmax_t)last, (intmax_t)ep->b_blkno,
193 (intmax_t)(ep->b_blkno + btodb(ep->b_bcount) - 1));
194 panic("ffs_checkoverlap: Disk buffer overlap");
202 * check if a block is available
205 ffs_isblock(fs, cp, h)
212 switch ((int)fs->fs_frag) {
214 return (cp[h] == 0xff);
216 mask = 0x0f << ((h & 0x1) << 2);
217 return ((cp[h >> 1] & mask) == mask);
219 mask = 0x03 << ((h & 0x3) << 1);
220 return ((cp[h >> 2] & mask) == mask);
222 mask = 0x01 << (h & 0x7);
223 return ((cp[h >> 3] & mask) == mask);
226 panic("ffs_isblock");
234 * check if a block is free
237 ffs_isfreeblock(fs, cp, h)
243 switch ((int)fs->fs_frag) {
247 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
249 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
251 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
254 panic("ffs_isfreeblock");
262 * take a block out of the map
265 ffs_clrblock(fs, cp, h)
271 switch ((int)fs->fs_frag) {
276 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
279 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
282 cp[h >> 3] &= ~(0x01 << (h & 0x7));
286 panic("ffs_clrblock");
293 * put a block into the map
296 ffs_setblock(fs, cp, h)
302 switch ((int)fs->fs_frag) {
308 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
311 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
314 cp[h >> 3] |= (0x01 << (h & 0x7));
318 panic("ffs_setblock");
325 * Update the cluster map because of an allocation or free.
327 * Cnt == 1 means free; cnt == -1 means allocating.
330 ffs_clusteracct(fs, cgp, blkno, cnt)
338 u_char *freemapp, *mapp;
339 int i, start, end, forw, back, map, bit;
341 if (fs->fs_contigsumsize <= 0)
343 freemapp = cg_clustersfree(cgp);
344 sump = cg_clustersum(cgp);
346 * Allocate or clear the actual block.
349 setbit(freemapp, blkno);
351 clrbit(freemapp, blkno);
353 * Find the size of the cluster going forward.
356 end = start + fs->fs_contigsumsize;
357 if (end >= cgp->cg_nclusterblks)
358 end = cgp->cg_nclusterblks;
359 mapp = &freemapp[start / NBBY];
361 bit = 1 << (start % NBBY);
362 for (i = start; i < end; i++) {
363 if ((map & bit) == 0)
365 if ((i & (NBBY - 1)) != (NBBY - 1)) {
374 * Find the size of the cluster going backward.
377 end = start - fs->fs_contigsumsize;
380 mapp = &freemapp[start / NBBY];
382 bit = 1 << (start % NBBY);
383 for (i = start; i > end; i--) {
384 if ((map & bit) == 0)
386 if ((i & (NBBY - 1)) != 0) {
390 bit = 1 << (NBBY - 1);
395 * Account for old cluster and the possibly new forward and
399 if (i > fs->fs_contigsumsize)
400 i = fs->fs_contigsumsize;
407 * Update cluster summary information.
409 lp = &sump[fs->fs_contigsumsize];
410 for (i = fs->fs_contigsumsize; i > 0; i--)
413 fs->fs_maxcluster[cgp->cg_cgx] = i;