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. 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(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
74 lbn = lblkno(fs, offset);
75 bsize = blksize(fs, ip, lbn);
78 error = bread(vp, lbn, bsize, NOCRED, &bp);
84 *res = (char *)bp->b_data + blkoff(fs, offset);
90 * Load up the contents of an inode and copy the appropriate pieces
94 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
99 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
100 ip->i_mode = ip->i_din1->di_mode;
101 ip->i_nlink = ip->i_din1->di_nlink;
102 ip->i_size = ip->i_din1->di_size;
103 ip->i_flags = ip->i_din1->di_flags;
104 ip->i_gen = ip->i_din1->di_gen;
105 ip->i_uid = ip->i_din1->di_uid;
106 ip->i_gid = ip->i_din1->di_gid;
109 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
110 ip->i_mode = ip->i_din2->di_mode;
111 ip->i_nlink = ip->i_din2->di_nlink;
112 ip->i_size = ip->i_din2->di_size;
113 ip->i_flags = ip->i_din2->di_flags;
114 ip->i_gen = ip->i_din2->di_gen;
115 ip->i_uid = ip->i_din2->di_uid;
116 ip->i_gid = ip->i_din2->di_gid;
122 * Update the frsum fields to reflect addition or deletion
126 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
132 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
134 for (siz = 1; siz < fs->fs_frag; siz++) {
135 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
138 subfield = inside[siz];
139 for (pos = siz; pos <= fs->fs_frag; pos++) {
140 if ((fragmap & field) == subfield) {
141 fraglist[siz] += cnt;
155 * check if a block is available
158 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
162 switch ((int)fs->fs_frag) {
164 return (cp[h] == 0xff);
166 mask = 0x0f << ((h & 0x1) << 2);
167 return ((cp[h >> 1] & mask) == mask);
169 mask = 0x03 << ((h & 0x3) << 1);
170 return ((cp[h >> 2] & mask) == mask);
172 mask = 0x01 << (h & 0x7);
173 return ((cp[h >> 3] & mask) == mask);
176 panic("ffs_isblock");
184 * check if a block is free
187 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
190 switch ((int)fs->fs_frag) {
194 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
196 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
198 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
201 panic("ffs_isfreeblock");
209 * take a block out of the map
212 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
215 switch ((int)fs->fs_frag) {
220 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
223 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
226 cp[h >> 3] &= ~(0x01 << (h & 0x7));
230 panic("ffs_clrblock");
237 * put a block into the map
240 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
243 switch ((int)fs->fs_frag) {
249 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
252 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
255 cp[h >> 3] |= (0x01 << (h & 0x7));
259 panic("ffs_setblock");
266 * Update the cluster map because of an allocation or free.
268 * Cnt == 1 means free; cnt == -1 means allocating.
271 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
275 u_char *freemapp, *mapp;
276 int i, start, end, forw, back, map, bit;
278 if (fs->fs_contigsumsize <= 0)
280 freemapp = cg_clustersfree(cgp);
281 sump = cg_clustersum(cgp);
283 * Allocate or clear the actual block.
286 setbit(freemapp, blkno);
288 clrbit(freemapp, blkno);
290 * Find the size of the cluster going forward.
293 end = start + fs->fs_contigsumsize;
294 if (end >= cgp->cg_nclusterblks)
295 end = cgp->cg_nclusterblks;
296 mapp = &freemapp[start / NBBY];
298 bit = 1 << (start % NBBY);
299 for (i = start; i < end; i++) {
300 if ((map & bit) == 0)
302 if ((i & (NBBY - 1)) != (NBBY - 1)) {
311 * Find the size of the cluster going backward.
314 end = start - fs->fs_contigsumsize;
317 mapp = &freemapp[start / NBBY];
319 bit = 1 << (start % NBBY);
320 for (i = start; i > end; i--) {
321 if ((map & bit) == 0)
323 if ((i & (NBBY - 1)) != 0) {
327 bit = 1 << (NBBY - 1);
332 * Account for old cluster and the possibly new forward and
336 if (i > fs->fs_contigsumsize)
337 i = fs->fs_contigsumsize;
344 * Update cluster summary information.
346 lp = &sump[fs->fs_contigsumsize];
347 for (i = fs->fs_contigsumsize; i > 0; i--)
350 fs->fs_maxcluster[cgp->cg_cgx] = i;