2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <ufs/ufs/dinode.h>
41 #include <ufs/ffs/fs.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
50 #include <sys/ucred.h>
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/extattr.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/ufs_extern.h>
57 #include <ufs/ffs/ffs_extern.h>
58 #include <ufs/ffs/fs.h>
61 * Return buffer with the contents of block "offset" from the beginning of
62 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
63 * remaining space in the directory.
66 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
76 lbn = lblkno(fs, offset);
77 bsize = blksize(fs, ip, lbn);
80 error = bread(vp, lbn, bsize, NOCRED, &bp);
86 *res = (char *)bp->b_data + blkoff(fs, offset);
92 * Load up the contents of an inode and copy the appropriate pieces
96 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
101 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
102 ip->i_mode = ip->i_din1->di_mode;
103 ip->i_nlink = ip->i_din1->di_nlink;
104 ip->i_size = ip->i_din1->di_size;
105 ip->i_flags = ip->i_din1->di_flags;
106 ip->i_gen = ip->i_din1->di_gen;
107 ip->i_uid = ip->i_din1->di_uid;
108 ip->i_gid = ip->i_din1->di_gid;
111 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
112 ip->i_mode = ip->i_din2->di_mode;
113 ip->i_nlink = ip->i_din2->di_nlink;
114 ip->i_size = ip->i_din2->di_size;
115 ip->i_flags = ip->i_din2->di_flags;
116 ip->i_gen = ip->i_din2->di_gen;
117 ip->i_uid = ip->i_din2->di_uid;
118 ip->i_gid = ip->i_din2->di_gid;
124 * Update the frsum fields to reflect addition or deletion
128 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
134 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
136 for (siz = 1; siz < fs->fs_frag; siz++) {
137 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
140 subfield = inside[siz];
141 for (pos = siz; pos <= fs->fs_frag; pos++) {
142 if ((fragmap & field) == subfield) {
143 fraglist[siz] += cnt;
157 * check if a block is available
160 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
164 switch ((int)fs->fs_frag) {
166 return (cp[h] == 0xff);
168 mask = 0x0f << ((h & 0x1) << 2);
169 return ((cp[h >> 1] & mask) == mask);
171 mask = 0x03 << ((h & 0x3) << 1);
172 return ((cp[h >> 2] & mask) == mask);
174 mask = 0x01 << (h & 0x7);
175 return ((cp[h >> 3] & mask) == mask);
178 panic("ffs_isblock");
186 * check if a block is free
189 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
192 switch ((int)fs->fs_frag) {
196 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
198 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
200 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
203 panic("ffs_isfreeblock");
211 * take a block out of the map
214 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
217 switch ((int)fs->fs_frag) {
222 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
225 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
228 cp[h >> 3] &= ~(0x01 << (h & 0x7));
232 panic("ffs_clrblock");
239 * put a block into the map
242 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
245 switch ((int)fs->fs_frag) {
251 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
254 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
257 cp[h >> 3] |= (0x01 << (h & 0x7));
261 panic("ffs_setblock");
268 * Update the cluster map because of an allocation or free.
270 * Cnt == 1 means free; cnt == -1 means allocating.
273 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
277 u_char *freemapp, *mapp;
278 int i, start, end, forw, back, map, bit;
280 if (fs->fs_contigsumsize <= 0)
282 freemapp = cg_clustersfree(cgp);
283 sump = cg_clustersum(cgp);
285 * Allocate or clear the actual block.
288 setbit(freemapp, blkno);
290 clrbit(freemapp, blkno);
292 * Find the size of the cluster going forward.
295 end = start + fs->fs_contigsumsize;
296 if (end >= cgp->cg_nclusterblks)
297 end = cgp->cg_nclusterblks;
298 mapp = &freemapp[start / NBBY];
300 bit = 1 << (start % NBBY);
301 for (i = start; i < end; i++) {
302 if ((map & bit) == 0)
304 if ((i & (NBBY - 1)) != (NBBY - 1)) {
313 * Find the size of the cluster going backward.
316 end = start - fs->fs_contigsumsize;
319 mapp = &freemapp[start / NBBY];
321 bit = 1 << (start % NBBY);
322 for (i = start; i > end; i--) {
323 if ((map & bit) == 0)
325 if ((i & (NBBY - 1)) != 0) {
329 bit = 1 << (NBBY - 1);
334 * Account for old cluster and the possibly new forward and
338 if (i > fs->fs_contigsumsize)
339 i = fs->fs_contigsumsize;
346 * Update cluster summary information.
348 lp = &sump[fs->fs_contigsumsize];
349 for (i = fs->fs_contigsumsize; i > 0; i--)
352 fs->fs_maxcluster[cgp->cg_cgx] = i;