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 * Return buffer with the contents of block "offset" from the beginning of
60 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
61 * remaining space in the directory.
64 ffs_blkatoff(vp, offset, res, bpp)
78 lbn = lblkno(fs, offset);
79 bsize = blksize(fs, ip, lbn);
82 error = bread(vp, lbn, bsize, NOCRED, &bp);
88 *res = (char *)bp->b_data + blkoff(fs, offset);
94 * Load up the contents of an inode and copy the appropriate pieces
98 ffs_load_inode(bp, ip, fs, ino)
107 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
108 ip->i_mode = ip->i_din1->di_mode;
109 ip->i_nlink = ip->i_din1->di_nlink;
110 ip->i_size = ip->i_din1->di_size;
111 ip->i_flags = ip->i_din1->di_flags;
112 ip->i_gen = ip->i_din1->di_gen;
113 ip->i_uid = ip->i_din1->di_uid;
114 ip->i_gid = ip->i_din1->di_gid;
117 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
118 ip->i_mode = ip->i_din2->di_mode;
119 ip->i_nlink = ip->i_din2->di_nlink;
120 ip->i_size = ip->i_din2->di_size;
121 ip->i_flags = ip->i_din2->di_flags;
122 ip->i_gen = ip->i_din2->di_gen;
123 ip->i_uid = ip->i_din2->di_uid;
124 ip->i_gid = ip->i_din2->di_gid;
130 * Update the frsum fields to reflect addition or deletion
134 ffs_fragacct(fs, fragmap, fraglist, cnt)
144 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
146 for (siz = 1; siz < fs->fs_frag; siz++) {
147 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
150 subfield = inside[siz];
151 for (pos = siz; pos <= fs->fs_frag; pos++) {
152 if ((fragmap & field) == subfield) {
153 fraglist[siz] += cnt;
167 * check if a block is available
170 ffs_isblock(fs, cp, h)
177 switch ((int)fs->fs_frag) {
179 return (cp[h] == 0xff);
181 mask = 0x0f << ((h & 0x1) << 2);
182 return ((cp[h >> 1] & mask) == mask);
184 mask = 0x03 << ((h & 0x3) << 1);
185 return ((cp[h >> 2] & mask) == mask);
187 mask = 0x01 << (h & 0x7);
188 return ((cp[h >> 3] & mask) == mask);
191 panic("ffs_isblock");
199 * check if a block is free
202 ffs_isfreeblock(fs, cp, h)
208 switch ((int)fs->fs_frag) {
212 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
214 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
216 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
219 panic("ffs_isfreeblock");
227 * take a block out of the map
230 ffs_clrblock(fs, cp, h)
236 switch ((int)fs->fs_frag) {
241 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
244 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
247 cp[h >> 3] &= ~(0x01 << (h & 0x7));
251 panic("ffs_clrblock");
258 * put a block into the map
261 ffs_setblock(fs, cp, h)
267 switch ((int)fs->fs_frag) {
273 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
276 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
279 cp[h >> 3] |= (0x01 << (h & 0x7));
283 panic("ffs_setblock");
290 * Update the cluster map because of an allocation or free.
292 * Cnt == 1 means free; cnt == -1 means allocating.
295 ffs_clusteracct(fs, cgp, blkno, cnt)
303 u_char *freemapp, *mapp;
304 int i, start, end, forw, back, map, bit;
306 if (fs->fs_contigsumsize <= 0)
308 freemapp = cg_clustersfree(cgp);
309 sump = cg_clustersum(cgp);
311 * Allocate or clear the actual block.
314 setbit(freemapp, blkno);
316 clrbit(freemapp, blkno);
318 * Find the size of the cluster going forward.
321 end = start + fs->fs_contigsumsize;
322 if (end >= cgp->cg_nclusterblks)
323 end = cgp->cg_nclusterblks;
324 mapp = &freemapp[start / NBBY];
326 bit = 1 << (start % NBBY);
327 for (i = start; i < end; i++) {
328 if ((map & bit) == 0)
330 if ((i & (NBBY - 1)) != (NBBY - 1)) {
339 * Find the size of the cluster going backward.
342 end = start - fs->fs_contigsumsize;
345 mapp = &freemapp[start / NBBY];
347 bit = 1 << (start % NBBY);
348 for (i = start; i > end; i--) {
349 if ((map & bit) == 0)
351 if ((i & (NBBY - 1)) != 0) {
355 bit = 1 << (NBBY - 1);
360 * Account for old cluster and the possibly new forward and
364 if (i > fs->fs_contigsumsize)
365 i = fs->fs_contigsumsize;
372 * Update cluster summary information.
374 lp = &sump[fs->fs_contigsumsize];
375 for (i = fs->fs_contigsumsize; i > 0; i--)
378 fs->fs_maxcluster[cgp->cg_cgx] = i;