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>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
49 #define UFS_MALLOC(size, type, flags) malloc(size)
50 #define UFS_FREE(ptr, type) free(ptr)
51 #define UFS_TIME time(NULL)
54 #include <sys/systm.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/vnode.h>
61 #include <sys/ucred.h>
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/extattr.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/ufs_extern.h>
68 #include <ufs/ffs/ffs_extern.h>
69 #include <ufs/ffs/fs.h>
71 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
72 #define UFS_FREE(ptr, type) free(ptr, type)
73 #define UFS_TIME time_second
76 * Return buffer with the contents of block "offset" from the beginning of
77 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
78 * remaining space in the directory.
81 ffs_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
91 lbn = lblkno(fs, offset);
92 bsize = blksize(fs, ip, lbn);
95 error = bread(vp, lbn, bsize, NOCRED, &bp);
101 *res = (char *)bp->b_data + blkoff(fs, offset);
107 * Load up the contents of an inode and copy the appropriate pieces
108 * to the incore copy.
111 ffs_load_inode(struct buf *bp, struct inode *ip, struct fs *fs, ino_t ino)
116 *((struct ufs1_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
117 ip->i_mode = ip->i_din1->di_mode;
118 ip->i_nlink = ip->i_din1->di_nlink;
119 ip->i_size = ip->i_din1->di_size;
120 ip->i_flags = ip->i_din1->di_flags;
121 ip->i_gen = ip->i_din1->di_gen;
122 ip->i_uid = ip->i_din1->di_uid;
123 ip->i_gid = ip->i_din1->di_gid;
126 *((struct ufs2_dinode *)bp->b_data + ino_to_fsbo(fs, ino));
127 ip->i_mode = ip->i_din2->di_mode;
128 ip->i_nlink = ip->i_din2->di_nlink;
129 ip->i_size = ip->i_din2->di_size;
130 ip->i_flags = ip->i_din2->di_flags;
131 ip->i_gen = ip->i_din2->di_gen;
132 ip->i_uid = ip->i_din2->di_uid;
133 ip->i_gid = ip->i_din2->di_gid;
139 * These are the low-level functions that actually read and write
140 * the superblock and its associated data.
142 static off_t sblock_try[] = SBLOCKSEARCH;
143 static int readsuper(void *, struct fs **, off_t,
144 int (*)(void *, off_t, void **, int));
147 * Read a superblock from the devfd device.
149 * If an alternate superblock is specified, it is read. Otherwise the
150 * set of locations given in the SBLOCKSEARCH list is searched for a
151 * superblock. Memory is allocated for the superblock by the readfunc and
152 * is returned. If filltype is non-NULL, additional memory is allocated
153 * of type filltype and filled in with the superblock summary information.
155 * If a superblock is found, zero is returned. Otherwise one of the
156 * following error values is returned:
157 * EIO: non-existent or truncated superblock.
158 * EIO: error reading summary information.
159 * ENOENT: no usable known superblock found.
160 * ENOSPC: failed to allocate space for the superblock.
161 * EINVAL: The previous newfs operation on this volume did not complete.
162 * The administrator must complete newfs before using this volume.
165 ffs_sbget(void *devfd, struct fs **fsp, off_t altsuperblock,
166 struct malloc_type *filltype,
167 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
170 int i, ret, size, blks;
176 if (altsuperblock != -1) {
177 if ((ret = readsuper(devfd, fsp, altsuperblock, readfunc)) != 0)
180 for (i = 0; sblock_try[i] != -1; i++) {
181 if ((ret = readsuper(devfd, fsp, sblock_try[i],
188 if (sblock_try[i] == -1)
192 * If not filling in summary information, NULL out fs_csp and return.
195 if (filltype == NULL) {
200 * Read in the superblock summary information.
202 size = fs->fs_cssize;
203 blks = howmany(size, fs->fs_fsize);
204 if (fs->fs_contigsumsize > 0)
205 size += fs->fs_ncg * sizeof(int32_t);
206 size += fs->fs_ncg * sizeof(u_int8_t);
207 space = UFS_MALLOC(size, filltype, M_WAITOK);
208 fs->fs_csp = (struct csum *)space;
209 for (i = 0; i < blks; i += fs->fs_frag) {
211 if (i + fs->fs_frag > blks)
212 size = (blks - i) * fs->fs_fsize;
214 ret = (*readfunc)(devfd,
215 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
217 UFS_FREE(buf, filltype);
218 UFS_FREE(fs->fs_csp, filltype);
222 memcpy(space, buf, size);
223 UFS_FREE(buf, filltype);
226 if (fs->fs_contigsumsize > 0) {
227 fs->fs_maxcluster = lp = (int32_t *)space;
228 for (i = 0; i < fs->fs_ncg; i++)
229 *lp++ = fs->fs_contigsumsize;
230 space = (uint8_t *)lp;
232 size = fs->fs_ncg * sizeof(u_int8_t);
233 fs->fs_contigdirs = (u_int8_t *)space;
234 bzero(fs->fs_contigdirs, size);
239 * Try to read a superblock from the location specified by sblockloc.
240 * Return zero on success or an errno on failure.
243 readsuper(void *devfd, struct fs **fsp, off_t sblockloc,
244 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
249 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
253 if (fs->fs_magic == FS_BAD_MAGIC)
255 if (((fs->fs_magic == FS_UFS1_MAGIC && sblockloc <= SBLOCK_UFS1) ||
256 (fs->fs_magic == FS_UFS2_MAGIC &&
257 sblockloc == fs->fs_sblockloc)) &&
259 fs->fs_bsize >= MINBSIZE &&
260 fs->fs_bsize <= MAXBSIZE &&
261 fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE)) {
262 /* Have to set for old filesystems that predate this field */
263 fs->fs_sblockactualloc = sblockloc;
270 * Write a superblock to the devfd device from the memory pointed to by fs.
271 * Write out the superblock summary information if it is present.
273 * If the write is successful, zero is returned. Otherwise one of the
274 * following error values is returned:
275 * EIO: failed to write superblock.
276 * EIO: failed to write superblock summary information.
279 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
280 int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
282 int i, error, blks, size;
286 * If there is summary information, write it first, so if there
287 * is an error, the superblock will not be marked as clean.
289 if (fs->fs_csp != NULL) {
290 blks = howmany(fs->fs_cssize, fs->fs_fsize);
291 space = (uint8_t *)fs->fs_csp;
292 for (i = 0; i < blks; i += fs->fs_frag) {
294 if (i + fs->fs_frag > blks)
295 size = (blks - i) * fs->fs_fsize;
296 if ((error = (*writefunc)(devfd,
297 dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
304 fs->fs_time = UFS_TIME;
305 if ((error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize)) != 0)
311 * Update the frsum fields to reflect addition or deletion
315 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
321 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
323 for (siz = 1; siz < fs->fs_frag; siz++) {
324 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
327 subfield = inside[siz];
328 for (pos = siz; pos <= fs->fs_frag; pos++) {
329 if ((fragmap & field) == subfield) {
330 fraglist[siz] += cnt;
344 * check if a block is available
347 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
351 switch ((int)fs->fs_frag) {
353 return (cp[h] == 0xff);
355 mask = 0x0f << ((h & 0x1) << 2);
356 return ((cp[h >> 1] & mask) == mask);
358 mask = 0x03 << ((h & 0x3) << 1);
359 return ((cp[h >> 2] & mask) == mask);
361 mask = 0x01 << (h & 0x7);
362 return ((cp[h >> 3] & mask) == mask);
365 panic("ffs_isblock");
373 * check if a block is free
376 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
379 switch ((int)fs->fs_frag) {
383 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
385 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
387 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
390 panic("ffs_isfreeblock");
398 * take a block out of the map
401 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
404 switch ((int)fs->fs_frag) {
409 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
412 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
415 cp[h >> 3] &= ~(0x01 << (h & 0x7));
419 panic("ffs_clrblock");
426 * put a block into the map
429 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
432 switch ((int)fs->fs_frag) {
438 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
441 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
444 cp[h >> 3] |= (0x01 << (h & 0x7));
448 panic("ffs_setblock");
455 * Update the cluster map because of an allocation or free.
457 * Cnt == 1 means free; cnt == -1 means allocating.
460 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
464 u_char *freemapp, *mapp;
465 int i, start, end, forw, back, map, bit;
467 if (fs->fs_contigsumsize <= 0)
469 freemapp = cg_clustersfree(cgp);
470 sump = cg_clustersum(cgp);
472 * Allocate or clear the actual block.
475 setbit(freemapp, blkno);
477 clrbit(freemapp, blkno);
479 * Find the size of the cluster going forward.
482 end = start + fs->fs_contigsumsize;
483 if (end >= cgp->cg_nclusterblks)
484 end = cgp->cg_nclusterblks;
485 mapp = &freemapp[start / NBBY];
487 bit = 1 << (start % NBBY);
488 for (i = start; i < end; i++) {
489 if ((map & bit) == 0)
491 if ((i & (NBBY - 1)) != (NBBY - 1)) {
500 * Find the size of the cluster going backward.
503 end = start - fs->fs_contigsumsize;
506 mapp = &freemapp[start / NBBY];
508 bit = 1 << (start % NBBY);
509 for (i = start; i > end; i--) {
510 if ((map & bit) == 0)
512 if ((i & (NBBY - 1)) != 0) {
516 bit = 1 << (NBBY - 1);
521 * Account for old cluster and the possibly new forward and
525 if (i > fs->fs_contigsumsize)
526 i = fs->fs_contigsumsize;
533 * Update cluster summary information.
535 lp = &sump[fs->fs_contigsumsize];
536 for (i = fs->fs_contigsumsize; i > 0; i--)
539 fs->fs_maxcluster[cgp->cg_cgx] = i;