160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved.
6df8bae1dSRodney W. Grimes *
7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes * are met:
10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution.
1515c377c3SEd Maste * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes * without specific prior written permission.
18df8bae1dSRodney W. Grimes *
19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes */
31df8bae1dSRodney W. Grimes
32df8bae1dSRodney W. Grimes #include <sys/param.h>
33b13110e9SAlfredo Dal'Ava Junior #include <sys/endian.h>
34076002f2SKirk McKusick #include <sys/limits.h>
35df8bae1dSRodney W. Grimes
36c4473420SPeter Wemm #ifndef _KERNEL
371111a443SKirk McKusick #include <stdbool.h>
38dffce215SKirk McKusick #include <stdio.h>
39dffce215SKirk McKusick #include <string.h>
40dffce215SKirk McKusick #include <stdlib.h>
41dffce215SKirk McKusick #include <time.h>
42dffce215SKirk McKusick #include <sys/errno.h>
43996c772fSJohn Dyson #include <ufs/ufs/dinode.h>
441c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
45dffce215SKirk McKusick
46ec888383SKirk McKusick uint32_t calculate_crc32c(uint32_t, const void *, size_t);
47ade67b50SKirk McKusick uint32_t ffs_calc_sbhash(struct fs *);
48dffce215SKirk McKusick struct malloc_type;
49dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size)
50dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr)
51076002f2SKirk McKusick #define maxphys MAXPHYS
52dffce215SKirk McKusick
53dffce215SKirk McKusick #else /* _KERNEL */
54df8bae1dSRodney W. Grimes #include <sys/systm.h>
55f89d2072SXin LI #include <sys/gsb_crc32.h>
561cd52ec3SBruce Evans #include <sys/lock.h>
571c85e6a3SKirk McKusick #include <sys/malloc.h>
581c85e6a3SKirk McKusick #include <sys/mount.h>
59df8bae1dSRodney W. Grimes #include <sys/vnode.h>
609626b608SPoul-Henning Kamp #include <sys/bio.h>
61df8bae1dSRodney W. Grimes #include <sys/buf.h>
6208637435SBruce Evans #include <sys/ucred.h>
631111a443SKirk McKusick #include <sys/sysctl.h>
6408637435SBruce Evans
65df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
66df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
671c85e6a3SKirk McKusick #include <ufs/ufs/extattr.h>
681c85e6a3SKirk McKusick #include <ufs/ufs/ufsmount.h>
691c85e6a3SKirk McKusick #include <ufs/ufs/ufs_extern.h>
70996c772fSJohn Dyson #include <ufs/ffs/ffs_extern.h>
711c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
72df8bae1dSRodney W. Grimes
73dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
74dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr, type)
75dffce215SKirk McKusick
76fb14e73cSKirk McKusick #endif /* _KERNEL */
77df8bae1dSRodney W. Grimes
78df8bae1dSRodney W. Grimes /*
798f829a5cSKirk McKusick * Verify an inode check-hash.
808f829a5cSKirk McKusick */
818f829a5cSKirk McKusick int
ffs_verify_dinode_ckhash(struct fs * fs,struct ufs2_dinode * dip)828f829a5cSKirk McKusick ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
838f829a5cSKirk McKusick {
84c8f55fc4SKirk McKusick uint32_t ckhash, save_ckhash;
858f829a5cSKirk McKusick
868f829a5cSKirk McKusick /*
878f829a5cSKirk McKusick * Return success if unallocated or we are not doing inode check-hash.
888f829a5cSKirk McKusick */
898f829a5cSKirk McKusick if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
908f829a5cSKirk McKusick return (0);
918f829a5cSKirk McKusick /*
928f829a5cSKirk McKusick * Exclude di_ckhash from the crc32 calculation, e.g., always use
938f829a5cSKirk McKusick * a check-hash value of zero when calculating the check-hash.
948f829a5cSKirk McKusick */
958f829a5cSKirk McKusick save_ckhash = dip->di_ckhash;
968f829a5cSKirk McKusick dip->di_ckhash = 0;
97c8f55fc4SKirk McKusick ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
988f829a5cSKirk McKusick dip->di_ckhash = save_ckhash;
99c8f55fc4SKirk McKusick if (save_ckhash == ckhash)
1008f829a5cSKirk McKusick return (0);
101c8f55fc4SKirk McKusick return (EINVAL);
1028f829a5cSKirk McKusick }
1038f829a5cSKirk McKusick
1048f829a5cSKirk McKusick /*
1058f829a5cSKirk McKusick * Update an inode check-hash.
1068f829a5cSKirk McKusick */
1078f829a5cSKirk McKusick void
ffs_update_dinode_ckhash(struct fs * fs,struct ufs2_dinode * dip)1088f829a5cSKirk McKusick ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
1098f829a5cSKirk McKusick {
1108f829a5cSKirk McKusick
1118f829a5cSKirk McKusick if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
1128f829a5cSKirk McKusick return;
1138f829a5cSKirk McKusick /*
1148f829a5cSKirk McKusick * Exclude old di_ckhash from the crc32 calculation, e.g., always use
1158f829a5cSKirk McKusick * a check-hash value of zero when calculating the new check-hash.
1168f829a5cSKirk McKusick */
1178f829a5cSKirk McKusick dip->di_ckhash = 0;
1188f829a5cSKirk McKusick dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
1198f829a5cSKirk McKusick }
1208f829a5cSKirk McKusick
1218f829a5cSKirk McKusick /*
122dffce215SKirk McKusick * These are the low-level functions that actually read and write
123dffce215SKirk McKusick * the superblock and its associated data.
124dffce215SKirk McKusick */
125dffce215SKirk McKusick static off_t sblock_try[] = SBLOCKSEARCH;
126b21582eeSKirk McKusick static int readsuper(void *, struct fs **, off_t, int,
127dffce215SKirk McKusick int (*)(void *, off_t, void **, int));
128661ca921SKirk McKusick static void ffs_oldfscompat_read(struct fs *, ufs2_daddr_t);
129076002f2SKirk McKusick static int validate_sblock(struct fs *, int);
130dffce215SKirk McKusick
131dffce215SKirk McKusick /*
132dffce215SKirk McKusick * Read a superblock from the devfd device.
133dffce215SKirk McKusick *
134dffce215SKirk McKusick * If an alternate superblock is specified, it is read. Otherwise the
135dffce215SKirk McKusick * set of locations given in the SBLOCKSEARCH list is searched for a
136dffce215SKirk McKusick * superblock. Memory is allocated for the superblock by the readfunc and
137dffce215SKirk McKusick * is returned. If filltype is non-NULL, additional memory is allocated
138dffce215SKirk McKusick * of type filltype and filled in with the superblock summary information.
139efbf3964SKirk McKusick * All memory is freed when any error is returned.
140dffce215SKirk McKusick *
141dffce215SKirk McKusick * If a superblock is found, zero is returned. Otherwise one of the
142dffce215SKirk McKusick * following error values is returned:
143dffce215SKirk McKusick * EIO: non-existent or truncated superblock.
144dffce215SKirk McKusick * EIO: error reading summary information.
145dffce215SKirk McKusick * ENOENT: no usable known superblock found.
146b13110e9SAlfredo Dal'Ava Junior * EILSEQ: filesystem with wrong byte order found.
147076002f2SKirk McKusick * ENOMEM: failed to allocate space for the superblock.
148dffce215SKirk McKusick * EINVAL: The previous newfs operation on this volume did not complete.
149dffce215SKirk McKusick * The administrator must complete newfs before using this volume.
150dffce215SKirk McKusick */
151dffce215SKirk McKusick int
ffs_sbget(void * devfd,struct fs ** fsp,off_t sblock,int flags,struct malloc_type * filltype,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))152b21582eeSKirk McKusick ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
153dffce215SKirk McKusick struct malloc_type *filltype,
154dffce215SKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
155dffce215SKirk McKusick {
156dffce215SKirk McKusick struct fs *fs;
15734816cb9SKirk McKusick struct fs_summary_info *fs_si;
158076002f2SKirk McKusick int i, error;
159076002f2SKirk McKusick uint64_t size, blks;
160dffce215SKirk McKusick uint8_t *space;
161dffce215SKirk McKusick int32_t *lp;
162dffce215SKirk McKusick char *buf;
163dffce215SKirk McKusick
164efbf3964SKirk McKusick fs = NULL;
16516759360SMark Johnston *fsp = NULL;
166b21582eeSKirk McKusick if (sblock != UFS_STDSB) {
167b21582eeSKirk McKusick if ((error = readsuper(devfd, &fs, sblock,
168b21582eeSKirk McKusick flags | UFS_ALTSBLK, readfunc)) != 0) {
169efbf3964SKirk McKusick if (fs != NULL)
170efbf3964SKirk McKusick UFS_FREE(fs, filltype);
1714cbd996aSKirk McKusick return (error);
172efbf3964SKirk McKusick }
173dffce215SKirk McKusick } else {
174dffce215SKirk McKusick for (i = 0; sblock_try[i] != -1; i++) {
175b21582eeSKirk McKusick if ((error = readsuper(devfd, &fs, sblock_try[i],
176b21582eeSKirk McKusick flags, readfunc)) == 0) {
177b21582eeSKirk McKusick if ((flags & UFS_NOCSUM) != 0) {
178b21582eeSKirk McKusick *fsp = fs;
179b21582eeSKirk McKusick return (0);
180b21582eeSKirk McKusick }
181dffce215SKirk McKusick break;
182b21582eeSKirk McKusick }
183efbf3964SKirk McKusick if (fs != NULL) {
184efbf3964SKirk McKusick UFS_FREE(fs, filltype);
185efbf3964SKirk McKusick fs = NULL;
186efbf3964SKirk McKusick }
1874cbd996aSKirk McKusick if (error == ENOENT)
188dffce215SKirk McKusick continue;
1894cbd996aSKirk McKusick return (error);
190dffce215SKirk McKusick }
191dffce215SKirk McKusick if (sblock_try[i] == -1)
192dffce215SKirk McKusick return (ENOENT);
193dffce215SKirk McKusick }
194dffce215SKirk McKusick /*
195dffce215SKirk McKusick * Read in the superblock summary information.
196dffce215SKirk McKusick */
197dffce215SKirk McKusick size = fs->fs_cssize;
198dffce215SKirk McKusick blks = howmany(size, fs->fs_fsize);
199dffce215SKirk McKusick if (fs->fs_contigsumsize > 0)
200dffce215SKirk McKusick size += fs->fs_ncg * sizeof(int32_t);
201831b1ff7SKirk McKusick size += fs->fs_ncg * sizeof(uint8_t);
202076002f2SKirk McKusick if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) {
20334816cb9SKirk McKusick UFS_FREE(fs, filltype);
204076002f2SKirk McKusick return (ENOMEM);
20534816cb9SKirk McKusick }
20634816cb9SKirk McKusick bzero(fs_si, sizeof(*fs_si));
20734816cb9SKirk McKusick fs->fs_si = fs_si;
208076002f2SKirk McKusick if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) {
20934816cb9SKirk McKusick UFS_FREE(fs->fs_si, filltype);
210efbf3964SKirk McKusick UFS_FREE(fs, filltype);
211076002f2SKirk McKusick return (ENOMEM);
212efbf3964SKirk McKusick }
213dffce215SKirk McKusick fs->fs_csp = (struct csum *)space;
214dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) {
215dffce215SKirk McKusick size = fs->fs_bsize;
216dffce215SKirk McKusick if (i + fs->fs_frag > blks)
217dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize;
21816759360SMark Johnston buf = NULL;
2194cbd996aSKirk McKusick error = (*readfunc)(devfd,
220dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
2214cbd996aSKirk McKusick if (error) {
222efbf3964SKirk McKusick if (buf != NULL)
22316759360SMark Johnston UFS_FREE(buf, filltype);
224dffce215SKirk McKusick UFS_FREE(fs->fs_csp, filltype);
22534816cb9SKirk McKusick UFS_FREE(fs->fs_si, filltype);
226efbf3964SKirk McKusick UFS_FREE(fs, filltype);
2274cbd996aSKirk McKusick return (error);
228dffce215SKirk McKusick }
229dffce215SKirk McKusick memcpy(space, buf, size);
230dffce215SKirk McKusick UFS_FREE(buf, filltype);
231dffce215SKirk McKusick space += size;
232dffce215SKirk McKusick }
233dffce215SKirk McKusick if (fs->fs_contigsumsize > 0) {
234dffce215SKirk McKusick fs->fs_maxcluster = lp = (int32_t *)space;
235dffce215SKirk McKusick for (i = 0; i < fs->fs_ncg; i++)
236dffce215SKirk McKusick *lp++ = fs->fs_contigsumsize;
237dffce215SKirk McKusick space = (uint8_t *)lp;
238dffce215SKirk McKusick }
239831b1ff7SKirk McKusick size = fs->fs_ncg * sizeof(uint8_t);
240831b1ff7SKirk McKusick fs->fs_contigdirs = (uint8_t *)space;
241dffce215SKirk McKusick bzero(fs->fs_contigdirs, size);
242efbf3964SKirk McKusick *fsp = fs;
243dffce215SKirk McKusick return (0);
244dffce215SKirk McKusick }
245dffce215SKirk McKusick
246dffce215SKirk McKusick /*
247dffce215SKirk McKusick * Try to read a superblock from the location specified by sblockloc.
248dffce215SKirk McKusick * Return zero on success or an errno on failure.
249dffce215SKirk McKusick */
250dffce215SKirk McKusick static int
readsuper(void * devfd,struct fs ** fsp,off_t sblockloc,int flags,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))251b21582eeSKirk McKusick readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int flags,
252b21582eeSKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
253dffce215SKirk McKusick {
254dffce215SKirk McKusick struct fs *fs;
255ec888383SKirk McKusick int error, res;
256ec888383SKirk McKusick uint32_t ckhash;
257dffce215SKirk McKusick
258dffce215SKirk McKusick error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
259dffce215SKirk McKusick if (error != 0)
260dffce215SKirk McKusick return (error);
261dffce215SKirk McKusick fs = *fsp;
262dffce215SKirk McKusick if (fs->fs_magic == FS_BAD_MAGIC)
263dffce215SKirk McKusick return (EINVAL);
26436e08b01SKirk McKusick /*
26536e08b01SKirk McKusick * For UFS1 with a 65536 block size, the first backup superblock
26636e08b01SKirk McKusick * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2
26736e08b01SKirk McKusick * is the first location checked, the first backup is the superblock
26836e08b01SKirk McKusick * that will be accessed. Here we fail the lookup so that we can
26936e08b01SKirk McKusick * retry with the correct location for the UFS1 superblock.
27036e08b01SKirk McKusick */
271b21582eeSKirk McKusick if (fs->fs_magic == FS_UFS1_MAGIC && (flags & UFS_ALTSBLK) == 0 &&
27236e08b01SKirk McKusick fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2)
27336e08b01SKirk McKusick return (ENOENT);
274661ca921SKirk McKusick ffs_oldfscompat_read(fs, sblockloc);
275b21582eeSKirk McKusick if ((error = validate_sblock(fs, flags)) > 0)
276076002f2SKirk McKusick return (error);
277a02bd3e3SKirk McKusick /*
278a02bd3e3SKirk McKusick * If the filesystem has been run on a kernel without
279a02bd3e3SKirk McKusick * metadata check hashes, disable them.
280a02bd3e3SKirk McKusick */
281a02bd3e3SKirk McKusick if ((fs->fs_flags & FS_METACKHASH) == 0)
282a02bd3e3SKirk McKusick fs->fs_metackhash = 0;
283996d40f9SKirk McKusick /*
284996d40f9SKirk McKusick * Clear any check-hashes that are not maintained
285996d40f9SKirk McKusick * by this kernel. Also clear any unsupported flags.
286996d40f9SKirk McKusick */
287996d40f9SKirk McKusick fs->fs_metackhash &= CK_SUPPORTED;
288996d40f9SKirk McKusick fs->fs_flags &= FS_SUPPORTED;
289ade67b50SKirk McKusick if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
290b21582eeSKirk McKusick if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) ==
291b21582eeSKirk McKusick (UFS_NOMSG | UFS_NOHASHFAIL))
292f2b39152SKirk McKusick return (0);
293b21582eeSKirk McKusick if ((flags & UFS_NOMSG) != 0)
294b21582eeSKirk McKusick return (EINTEGRITY);
295ec888383SKirk McKusick #ifdef _KERNEL
296ec888383SKirk McKusick res = uprintf("Superblock check-hash failed: recorded "
297fb14e73cSKirk McKusick "check-hash 0x%x != computed check-hash 0x%x%s\n",
298fb14e73cSKirk McKusick fs->fs_ckhash, ckhash,
299b21582eeSKirk McKusick (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : "");
300ec888383SKirk McKusick #else
301ec888383SKirk McKusick res = 0;
302ec888383SKirk McKusick #endif
303ec888383SKirk McKusick /*
304ec888383SKirk McKusick * Print check-hash failure if no controlling terminal
305ec888383SKirk McKusick * in kernel or always if in user-mode (libufs).
306ec888383SKirk McKusick */
307ec888383SKirk McKusick if (res == 0)
308ec888383SKirk McKusick printf("Superblock check-hash failed: recorded "
309ec888383SKirk McKusick "check-hash 0x%x != computed check-hash "
310fb14e73cSKirk McKusick "0x%x%s\n", fs->fs_ckhash, ckhash,
311b21582eeSKirk McKusick (flags & UFS_NOHASHFAIL) ? " (Ignored)" : "");
312b21582eeSKirk McKusick if ((flags & UFS_NOHASHFAIL) != 0)
313fb14e73cSKirk McKusick return (0);
314b21582eeSKirk McKusick return (EINTEGRITY);
315fb14e73cSKirk McKusick }
316dffce215SKirk McKusick /* Have to set for old filesystems that predate this field */
317dffce215SKirk McKusick fs->fs_sblockactualloc = sblockloc;
318efbf3964SKirk McKusick /* Not yet any summary information */
31934816cb9SKirk McKusick fs->fs_si = NULL;
320dffce215SKirk McKusick return (0);
321dffce215SKirk McKusick }
322dffce215SKirk McKusick
323dffce215SKirk McKusick /*
324661ca921SKirk McKusick * Sanity checks for loading old filesystem superblocks.
325661ca921SKirk McKusick * See ffs_oldfscompat_write below for unwound actions.
326661ca921SKirk McKusick *
327661ca921SKirk McKusick * XXX - Parts get retired eventually.
328661ca921SKirk McKusick * Unfortunately new bits get added.
329661ca921SKirk McKusick */
330661ca921SKirk McKusick static void
ffs_oldfscompat_read(struct fs * fs,ufs2_daddr_t sblockloc)331661ca921SKirk McKusick ffs_oldfscompat_read(struct fs *fs, ufs2_daddr_t sblockloc)
332661ca921SKirk McKusick {
333661ca921SKirk McKusick uint64_t maxfilesize;
334661ca921SKirk McKusick
335661ca921SKirk McKusick /*
336661ca921SKirk McKusick * If not yet done, update fs_flags location and value of fs_sblockloc.
337661ca921SKirk McKusick */
338661ca921SKirk McKusick if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
339661ca921SKirk McKusick fs->fs_flags = fs->fs_old_flags;
340661ca921SKirk McKusick fs->fs_old_flags |= FS_FLAGS_UPDATED;
341661ca921SKirk McKusick fs->fs_sblockloc = sblockloc;
342661ca921SKirk McKusick }
343*16649530SKirk McKusick switch (fs->fs_magic) {
344*16649530SKirk McKusick case FS_UFS2_MAGIC:
345*16649530SKirk McKusick /* No changes for now */
346*16649530SKirk McKusick break;
347*16649530SKirk McKusick
348*16649530SKirk McKusick case FS_UFS1_MAGIC:
349661ca921SKirk McKusick /*
350*16649530SKirk McKusick * If not yet done, update UFS1 superblock with new wider fields
351661ca921SKirk McKusick */
352*16649530SKirk McKusick if (fs->fs_maxbsize != fs->fs_bsize) {
353661ca921SKirk McKusick fs->fs_maxbsize = fs->fs_bsize;
354661ca921SKirk McKusick fs->fs_time = fs->fs_old_time;
355661ca921SKirk McKusick fs->fs_size = fs->fs_old_size;
356661ca921SKirk McKusick fs->fs_dsize = fs->fs_old_dsize;
357661ca921SKirk McKusick fs->fs_csaddr = fs->fs_old_csaddr;
358661ca921SKirk McKusick fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
359661ca921SKirk McKusick fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
360661ca921SKirk McKusick fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
361661ca921SKirk McKusick fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
362661ca921SKirk McKusick }
363*16649530SKirk McKusick if (fs->fs_old_inodefmt < FS_44INODEFMT) {
364661ca921SKirk McKusick fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
365661ca921SKirk McKusick fs->fs_qbmask = ~fs->fs_bmask;
366661ca921SKirk McKusick fs->fs_qfmask = ~fs->fs_fmask;
367661ca921SKirk McKusick }
368661ca921SKirk McKusick fs->fs_save_maxfilesize = fs->fs_maxfilesize;
369661ca921SKirk McKusick maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
370661ca921SKirk McKusick if (fs->fs_maxfilesize > maxfilesize)
371661ca921SKirk McKusick fs->fs_maxfilesize = maxfilesize;
372*16649530SKirk McKusick break;
373661ca921SKirk McKusick }
374661ca921SKirk McKusick /* Compatibility for old filesystems */
375661ca921SKirk McKusick if (fs->fs_avgfilesize <= 0)
376661ca921SKirk McKusick fs->fs_avgfilesize = AVFILESIZ;
377661ca921SKirk McKusick if (fs->fs_avgfpdir <= 0)
378661ca921SKirk McKusick fs->fs_avgfpdir = AFPDIR;
379661ca921SKirk McKusick }
380661ca921SKirk McKusick
381661ca921SKirk McKusick /*
382661ca921SKirk McKusick * Unwinding superblock updates for old filesystems.
383661ca921SKirk McKusick * See ffs_oldfscompat_read above for details.
384661ca921SKirk McKusick *
385661ca921SKirk McKusick * XXX - Parts get retired eventually.
386661ca921SKirk McKusick * Unfortunately new bits get added.
387661ca921SKirk McKusick */
388661ca921SKirk McKusick void
ffs_oldfscompat_write(struct fs * fs)389661ca921SKirk McKusick ffs_oldfscompat_write(struct fs *fs)
390661ca921SKirk McKusick {
391661ca921SKirk McKusick
392c2cd605eSKirk McKusick switch (fs->fs_magic) {
393c2cd605eSKirk McKusick case FS_UFS1_MAGIC:
394c2cd605eSKirk McKusick if (fs->fs_sblockloc != SBLOCK_UFS1 &&
395c2cd605eSKirk McKusick (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
396c2cd605eSKirk McKusick printf(
397c2cd605eSKirk McKusick "WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
398c2cd605eSKirk McKusick fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
399c2cd605eSKirk McKusick fs->fs_sblockloc = SBLOCK_UFS1;
400c2cd605eSKirk McKusick }
401661ca921SKirk McKusick /*
402661ca921SKirk McKusick * Copy back UFS2 updated fields that UFS1 inspects.
403661ca921SKirk McKusick */
404661ca921SKirk McKusick fs->fs_old_time = fs->fs_time;
405661ca921SKirk McKusick fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
406661ca921SKirk McKusick fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
407661ca921SKirk McKusick fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
408661ca921SKirk McKusick fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
409c2cd605eSKirk McKusick if (fs->fs_save_maxfilesize != 0)
410661ca921SKirk McKusick fs->fs_maxfilesize = fs->fs_save_maxfilesize;
411c2cd605eSKirk McKusick break;
412c2cd605eSKirk McKusick case FS_UFS2_MAGIC:
413c2cd605eSKirk McKusick if (fs->fs_sblockloc != SBLOCK_UFS2 &&
414c2cd605eSKirk McKusick (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
415c2cd605eSKirk McKusick printf(
416c2cd605eSKirk McKusick "WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
417c2cd605eSKirk McKusick fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
418c2cd605eSKirk McKusick fs->fs_sblockloc = SBLOCK_UFS2;
419c2cd605eSKirk McKusick }
420c2cd605eSKirk McKusick break;
421661ca921SKirk McKusick }
422661ca921SKirk McKusick }
423661ca921SKirk McKusick
424661ca921SKirk McKusick /*
4251111a443SKirk McKusick * Sanity checks for loading old filesystem inodes.
4261111a443SKirk McKusick *
4271111a443SKirk McKusick * XXX - Parts get retired eventually.
4281111a443SKirk McKusick * Unfortunately new bits get added.
4291111a443SKirk McKusick */
4301111a443SKirk McKusick static int prttimechgs = 0;
4311111a443SKirk McKusick #ifdef _KERNEL
432e1ebda44SGleb Smirnoff SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
433e1ebda44SGleb Smirnoff "FFS filesystem");
4341111a443SKirk McKusick SYSCTL_INT(_vfs_ffs, OID_AUTO, prttimechgs, CTLFLAG_RWTUN, &prttimechgs, 0,
4351111a443SKirk McKusick "print UFS1 time changes made to inodes");
4361111a443SKirk McKusick #endif /* _KERNEL */
4371111a443SKirk McKusick bool
ffs_oldfscompat_inode_read(struct fs * fs,union dinodep dp,time_t now)4381111a443SKirk McKusick ffs_oldfscompat_inode_read(struct fs *fs, union dinodep dp, time_t now)
4391111a443SKirk McKusick {
4401111a443SKirk McKusick bool change;
4411111a443SKirk McKusick
4421111a443SKirk McKusick change = false;
4431111a443SKirk McKusick switch (fs->fs_magic) {
4441111a443SKirk McKusick case FS_UFS2_MAGIC:
4451111a443SKirk McKusick /* No changes for now */
4461111a443SKirk McKusick break;
4471111a443SKirk McKusick
4481111a443SKirk McKusick case FS_UFS1_MAGIC:
4491111a443SKirk McKusick /*
4501111a443SKirk McKusick * With the change to unsigned time values in UFS1, times set
4511111a443SKirk McKusick * before Jan 1, 1970 will appear to be in the future. Check
4521111a443SKirk McKusick * for future times and set them to be the current time.
4531111a443SKirk McKusick */
4541111a443SKirk McKusick if (dp.dp1->di_ctime > now) {
4551111a443SKirk McKusick if (prttimechgs)
4561111a443SKirk McKusick printf("ctime %ud changed to %ld\n",
4571111a443SKirk McKusick dp.dp1->di_ctime, (long)now);
4581111a443SKirk McKusick dp.dp1->di_ctime = now;
4591111a443SKirk McKusick change = true;
4601111a443SKirk McKusick }
4611111a443SKirk McKusick if (dp.dp1->di_mtime > now) {
4621111a443SKirk McKusick if (prttimechgs)
4631111a443SKirk McKusick printf("mtime %ud changed to %ld\n",
4641111a443SKirk McKusick dp.dp1->di_mtime, (long)now);
4651111a443SKirk McKusick dp.dp1->di_mtime = now;
4661111a443SKirk McKusick dp.dp1->di_ctime = now;
4671111a443SKirk McKusick change = true;
4681111a443SKirk McKusick }
4691111a443SKirk McKusick if (dp.dp1->di_atime > now) {
4701111a443SKirk McKusick if (prttimechgs)
4711111a443SKirk McKusick printf("atime %ud changed to %ld\n",
4721111a443SKirk McKusick dp.dp1->di_atime, (long)now);
4731111a443SKirk McKusick dp.dp1->di_atime = now;
4741111a443SKirk McKusick dp.dp1->di_ctime = now;
4751111a443SKirk McKusick change = true;
4761111a443SKirk McKusick }
4771111a443SKirk McKusick break;
4781111a443SKirk McKusick }
4791111a443SKirk McKusick return (change);
4801111a443SKirk McKusick }
4811111a443SKirk McKusick
4821111a443SKirk McKusick /*
483076002f2SKirk McKusick * Verify the filesystem values.
484076002f2SKirk McKusick */
485076002f2SKirk McKusick #define ILOG2(num) (fls(num) - 1)
48614821130SWarner Losh #ifdef STANDALONE_SMALL
48714821130SWarner Losh #define MPRINT(...) do { } while (0)
48814821130SWarner Losh #else
48914821130SWarner Losh #define MPRINT(...) if (prtmsg) printf(__VA_ARGS__)
49014821130SWarner Losh #endif
491d22531d5SKirk McKusick #define FCHK(lhs, op, rhs, fmt) \
49250dc4c7dSKirk McKusick if (lhs op rhs) { \
493b21582eeSKirk McKusick MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
49450dc4c7dSKirk McKusick #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \
49550dc4c7dSKirk McKusick #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \
496f0be378aSKirk McKusick if (error < 0) \
497f0be378aSKirk McKusick return (ENOENT); \
498b21582eeSKirk McKusick if (error == 0) \
499b21582eeSKirk McKusick error = ENOENT; \
50050dc4c7dSKirk McKusick }
501d22531d5SKirk McKusick #define WCHK(lhs, op, rhs, fmt) \
502d22531d5SKirk McKusick if (lhs op rhs) { \
503d22531d5SKirk McKusick MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
504d22531d5SKirk McKusick #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\
505d22531d5SKirk McKusick #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\
506d22531d5SKirk McKusick if (error == 0) \
507d22531d5SKirk McKusick error = warnerr; \
5080eabe330SKirk McKusick if (warnerr == 0) \
5090eabe330SKirk McKusick lhs = rhs; \
510d22531d5SKirk McKusick }
511d22531d5SKirk McKusick #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \
51250dc4c7dSKirk McKusick if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \
513b21582eeSKirk McKusick MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
51450dc4c7dSKirk McKusick #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \
51550dc4c7dSKirk McKusick fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \
51650dc4c7dSKirk McKusick (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \
51750dc4c7dSKirk McKusick (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \
518f0be378aSKirk McKusick if (error < 0) \
519f0be378aSKirk McKusick return (ENOENT); \
520b21582eeSKirk McKusick if (error == 0) \
521b21582eeSKirk McKusick error = ENOENT; \
52250dc4c7dSKirk McKusick }
523076002f2SKirk McKusick
524076002f2SKirk McKusick static int
validate_sblock(struct fs * fs,int flags)525b21582eeSKirk McKusick validate_sblock(struct fs *fs, int flags)
526076002f2SKirk McKusick {
527831b1ff7SKirk McKusick uint64_t i, sectorsize;
528831b1ff7SKirk McKusick uint64_t maxfilesize, sizepb;
529d22531d5SKirk McKusick int error, prtmsg, warnerr;
530d22531d5SKirk McKusick char *wmsg;
531076002f2SKirk McKusick
532b21582eeSKirk McKusick error = 0;
533076002f2SKirk McKusick sectorsize = dbtob(1);
534b21582eeSKirk McKusick prtmsg = ((flags & UFS_NOMSG) == 0);
535d22531d5SKirk McKusick warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT;
536d22531d5SKirk McKusick wmsg = warnerr ? "" : " (Ignored)";
537e6886616SKirk McKusick /*
538b13110e9SAlfredo Dal'Ava Junior * Check for endian mismatch between machine and filesystem.
539b13110e9SAlfredo Dal'Ava Junior */
540b13110e9SAlfredo Dal'Ava Junior if (((fs->fs_magic != FS_UFS2_MAGIC) &&
541b13110e9SAlfredo Dal'Ava Junior (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) ||
542b13110e9SAlfredo Dal'Ava Junior ((fs->fs_magic != FS_UFS1_MAGIC) &&
543b13110e9SAlfredo Dal'Ava Junior (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) {
544b13110e9SAlfredo Dal'Ava Junior MPRINT("UFS superblock failed due to endian mismatch "
545b13110e9SAlfredo Dal'Ava Junior "between machine and filesystem\n");
546b13110e9SAlfredo Dal'Ava Junior return(EILSEQ);
547b13110e9SAlfredo Dal'Ava Junior }
548b13110e9SAlfredo Dal'Ava Junior /*
549e6886616SKirk McKusick * If just validating for recovery, then do just the minimal
550e6886616SKirk McKusick * checks needed for the superblock fields needed to find
551e6886616SKirk McKusick * alternate superblocks.
552e6886616SKirk McKusick */
553e6886616SKirk McKusick if ((flags & UFS_FSRONLY) == UFS_FSRONLY &&
554e6886616SKirk McKusick (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) {
555f0be378aSKirk McKusick error = -1; /* fail on first error */
556e6886616SKirk McKusick if (fs->fs_magic == FS_UFS2_MAGIC) {
557e6886616SKirk McKusick FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
558e6886616SKirk McKusick } else if (fs->fs_magic == FS_UFS1_MAGIC) {
559e6886616SKirk McKusick FCHK(fs->fs_sblockloc, <, 0, %jd);
560e6886616SKirk McKusick FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
561e6886616SKirk McKusick }
562e6886616SKirk McKusick FCHK(fs->fs_frag, <, 1, %jd);
563e6886616SKirk McKusick FCHK(fs->fs_frag, >, MAXFRAG, %jd);
564e6886616SKirk McKusick FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
565e6886616SKirk McKusick FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
566e6886616SKirk McKusick FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE),
567e6886616SKirk McKusick %jd);
568e6886616SKirk McKusick FCHK(fs->fs_fsize, <, sectorsize, %jd);
569e6886616SKirk McKusick FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
570e6886616SKirk McKusick FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
57182ee4e1cSKirk McKusick FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
57282ee4e1cSKirk McKusick FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
573e15d8f23SKirk McKusick FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
574e6886616SKirk McKusick FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
575e6886616SKirk McKusick FCHK(fs->fs_ncg, <, 1, %jd);
576e6886616SKirk McKusick FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
577e6886616SKirk McKusick FCHK(fs->fs_old_cgoffset, <, 0, %jd);
578e6886616SKirk McKusick FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
579e6886616SKirk McKusick FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg,
580e6886616SKirk McKusick %jd);
581e6886616SKirk McKusick FCHK(fs->fs_sblkno, !=, roundup(
582e6886616SKirk McKusick howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
583e6886616SKirk McKusick fs->fs_frag), %jd);
5840eabe330SKirk McKusick FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
5850eabe330SKirk McKusick /* Only need to validate these if reading in csum data */
5860eabe330SKirk McKusick if ((flags & UFS_NOCSUM) != 0)
5870eabe330SKirk McKusick return (error);
588831b1ff7SKirk McKusick FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
5890eabe330SKirk McKusick (((int64_t)(1)) << 32) - INOPB(fs), %jd);
5900eabe330SKirk McKusick FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
5910eabe330SKirk McKusick FCHK(fs->fs_cstotal.cs_nifree, >,
592831b1ff7SKirk McKusick (uint64_t)fs->fs_ipg * fs->fs_ncg, %jd);
5930eabe330SKirk McKusick FCHK(fs->fs_cstotal.cs_ndir, >,
594831b1ff7SKirk McKusick ((uint64_t)fs->fs_ipg * fs->fs_ncg) -
5950eabe330SKirk McKusick fs->fs_cstotal.cs_nifree, %jd);
5960eabe330SKirk McKusick FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
5970eabe330SKirk McKusick FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg,
5980eabe330SKirk McKusick %jd);
5990eabe330SKirk McKusick FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
6000eabe330SKirk McKusick FCHK(fs->fs_csaddr, <, 0, %jd);
6010eabe330SKirk McKusick FCHK(fs->fs_cssize, !=,
6020eabe330SKirk McKusick fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
603c79a1416SKirk McKusick FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
604c79a1416SKirk McKusick fs->fs_size, %jd);
6050eabe330SKirk McKusick FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)),
6060eabe330SKirk McKusick %jd);
6070eabe330SKirk McKusick FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize,
6080eabe330SKirk McKusick fs->fs_fsize)), >, dtog(fs, fs->fs_csaddr), %jd);
609e6886616SKirk McKusick return (error);
610e6886616SKirk McKusick }
611076002f2SKirk McKusick if (fs->fs_magic == FS_UFS2_MAGIC) {
612184e3118SKirk McKusick if ((flags & UFS_ALTSBLK) == 0)
613d22531d5SKirk McKusick FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
61450dc4c7dSKirk McKusick fs->fs_sblockactualloc, !=, 0, %jd);
615d22531d5SKirk McKusick FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
616d22531d5SKirk McKusick FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
61750dc4c7dSKirk McKusick sizeof(ufs2_daddr_t)), %jd);
618d22531d5SKirk McKusick FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
61950dc4c7dSKirk McKusick %jd);
620d22531d5SKirk McKusick FCHK(fs->fs_inopb, !=,
621184e3118SKirk McKusick fs->fs_bsize / sizeof(struct ufs2_dinode), %jd);
622076002f2SKirk McKusick } else if (fs->fs_magic == FS_UFS1_MAGIC) {
623184e3118SKirk McKusick if ((flags & UFS_ALTSBLK) == 0)
624d22531d5SKirk McKusick FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
625d22531d5SKirk McKusick FCHK(fs->fs_sblockloc, <, 0, %jd);
626d22531d5SKirk McKusick FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
627d22531d5SKirk McKusick FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
62850dc4c7dSKirk McKusick %jd);
629d22531d5SKirk McKusick FCHK(fs->fs_inopb, !=,
630184e3118SKirk McKusick fs->fs_bsize / sizeof(struct ufs1_dinode), %jd);
631d22531d5SKirk McKusick FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
63250dc4c7dSKirk McKusick sizeof(ufs1_daddr_t)), %jd);
633d22531d5SKirk McKusick WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
634d22531d5SKirk McKusick WCHK(fs->fs_old_rotdelay, !=, 0, %jd);
635d22531d5SKirk McKusick WCHK(fs->fs_old_rps, !=, 60, %jd);
636d22531d5SKirk McKusick WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
637d22531d5SKirk McKusick WCHK(fs->fs_old_interleave, !=, 1, %jd);
638d22531d5SKirk McKusick WCHK(fs->fs_old_trackskew, !=, 0, %jd);
639d22531d5SKirk McKusick WCHK(fs->fs_old_cpc, !=, 0, %jd);
640d22531d5SKirk McKusick WCHK(fs->fs_old_postblformat, !=, 1, %jd);
641f0be378aSKirk McKusick FCHK(fs->fs_old_nrpos, !=, 1, %jd);
642d22531d5SKirk McKusick WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
643d22531d5SKirk McKusick WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
644076002f2SKirk McKusick } else {
64550dc4c7dSKirk McKusick /* Bad magic number, so assume not a superblock */
646076002f2SKirk McKusick return (ENOENT);
647076002f2SKirk McKusick }
648d22531d5SKirk McKusick FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
649d22531d5SKirk McKusick FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
650d22531d5SKirk McKusick FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
651d22531d5SKirk McKusick FCHK(powerof2(fs->fs_bsize), ==, 0, %jd);
652d22531d5SKirk McKusick FCHK(fs->fs_frag, <, 1, %jd);
653d22531d5SKirk McKusick FCHK(fs->fs_frag, >, MAXFRAG, %jd);
654d22531d5SKirk McKusick FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
655d22531d5SKirk McKusick FCHK(fs->fs_fsize, <, sectorsize, %jd);
656d22531d5SKirk McKusick FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
657d22531d5SKirk McKusick FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
658d22531d5SKirk McKusick FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
659d22531d5SKirk McKusick FCHK(fs->fs_ncg, <, 1, %jd);
660f0be378aSKirk McKusick FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd);
661831b1ff7SKirk McKusick FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
6628435a9b2SKirk McKusick (((int64_t)(1)) << 32) - INOPB(fs), %jd);
663f0be378aSKirk McKusick FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
664831b1ff7SKirk McKusick FCHK(fs->fs_cstotal.cs_nifree, >, (uint64_t)fs->fs_ipg * fs->fs_ncg,
6658435a9b2SKirk McKusick %jd);
666f0be378aSKirk McKusick FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd);
667f0be378aSKirk McKusick FCHK(fs->fs_cstotal.cs_ndir, >,
668831b1ff7SKirk McKusick ((uint64_t)fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree,
6698435a9b2SKirk McKusick %jd);
670d22531d5SKirk McKusick FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
671f0be378aSKirk McKusick FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
672a2d1957bSKirk McKusick /* fix for misconfigured filesystems */
673a2d1957bSKirk McKusick if (fs->fs_maxbsize == 0)
674a2d1957bSKirk McKusick fs->fs_maxbsize = fs->fs_bsize;
675d22531d5SKirk McKusick FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
676d22531d5SKirk McKusick FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
677d22531d5SKirk McKusick FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
678d22531d5SKirk McKusick FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
679d22531d5SKirk McKusick FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
680d22531d5SKirk McKusick FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
681d22531d5SKirk McKusick FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
682d22531d5SKirk McKusick FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
683d22531d5SKirk McKusick FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
684d22531d5SKirk McKusick FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
685d22531d5SKirk McKusick FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
686d22531d5SKirk McKusick FCHK(fs->fs_old_cgoffset, <, 0, %jd);
687d22531d5SKirk McKusick FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
688d22531d5SKirk McKusick FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd);
6890eabe330SKirk McKusick FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
690f0be378aSKirk McKusick /*
691f0be378aSKirk McKusick * If anything has failed up to this point, it is usafe to proceed
692f0be378aSKirk McKusick * as checks below may divide by zero or make other fatal calculations.
693f0be378aSKirk McKusick * So if we have any errors at this point, give up.
694f0be378aSKirk McKusick */
695f0be378aSKirk McKusick if (error)
696f0be378aSKirk McKusick return (error);
697e15d8f23SKirk McKusick FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
6989dee5da7SKirk McKusick FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd);
699d22531d5SKirk McKusick FCHK(fs->fs_sblkno, !=, roundup(
70050dc4c7dSKirk McKusick howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
70150dc4c7dSKirk McKusick fs->fs_frag), %jd);
702d22531d5SKirk McKusick FCHK(fs->fs_cblkno, !=, fs->fs_sblkno +
70350dc4c7dSKirk McKusick roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
704d22531d5SKirk McKusick FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
705d22531d5SKirk McKusick FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
706d22531d5SKirk McKusick FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
707f0be378aSKirk McKusick FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd);
708f0be378aSKirk McKusick FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd);
7095bc926afSKirk McKusick /*
7105bc926afSKirk McKusick * This test is valid, however older versions of growfs failed
7115bc926afSKirk McKusick * to correctly update fs_dsize so will fail this test. Thus we
7125bc926afSKirk McKusick * exclude it from the requirements.
7135bc926afSKirk McKusick */
7145bc926afSKirk McKusick #ifdef notdef
715d22531d5SKirk McKusick WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
716076002f2SKirk McKusick fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
71750dc4c7dSKirk McKusick howmany(fs->fs_cssize, fs->fs_fsize), %jd);
7185bc926afSKirk McKusick #endif
719d22531d5SKirk McKusick WCHK(fs->fs_metaspace, <, 0, %jd);
720d22531d5SKirk McKusick WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
721d22531d5SKirk McKusick WCHK(fs->fs_minfree, >, 99, %jd%%);
722076002f2SKirk McKusick maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
723076002f2SKirk McKusick for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
724076002f2SKirk McKusick sizepb *= NINDIR(fs);
725076002f2SKirk McKusick maxfilesize += sizepb;
726076002f2SKirk McKusick }
727661ca921SKirk McKusick WCHK(fs->fs_maxfilesize, >, maxfilesize, %jd);
728076002f2SKirk McKusick /*
729076002f2SKirk McKusick * These values have a tight interaction with each other that
730076002f2SKirk McKusick * makes it hard to tightly bound them. So we can only check
731076002f2SKirk McKusick * that they are within a broader possible range.
732076002f2SKirk McKusick *
733f3f5368dSKirk McKusick * The size cannot always be accurately determined, but ensure
734f3f5368dSKirk McKusick * that it is consistent with the number of cylinder groups (fs_ncg)
735f3f5368dSKirk McKusick * and the number of fragments per cylinder group (fs_fpg). Ensure
736f3f5368dSKirk McKusick * that the summary information size is correct and that it starts
737f3f5368dSKirk McKusick * and ends in the data area of the same cylinder group.
738076002f2SKirk McKusick */
739d22531d5SKirk McKusick FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
740017367c1SKonstantin Belousov FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, %jd);
741017367c1SKonstantin Belousov FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
742b21582eeSKirk McKusick /*
743b21582eeSKirk McKusick * If we are not requested to read in the csum data stop here
744b21582eeSKirk McKusick * as the correctness of the remaining values is only important
745b21582eeSKirk McKusick * to bound the space needed to be allocated to hold the csum data.
746b21582eeSKirk McKusick */
747b21582eeSKirk McKusick if ((flags & UFS_NOCSUM) != 0)
748b21582eeSKirk McKusick return (error);
749d22531d5SKirk McKusick FCHK(fs->fs_csaddr, <, 0, %jd);
750d22531d5SKirk McKusick FCHK(fs->fs_cssize, !=,
751f3f5368dSKirk McKusick fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
752c79a1416SKirk McKusick FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
753c79a1416SKirk McKusick fs->fs_size, %jd);
754d22531d5SKirk McKusick FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd);
755d22531d5SKirk McKusick FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
756548045bfSKirk McKusick dtog(fs, fs->fs_csaddr), %jd);
757076002f2SKirk McKusick /*
758bc218d89SKirk McKusick * With file system clustering it is possible to allocate
759bc218d89SKirk McKusick * many contiguous blocks. The kernel variable maxphys defines
760bc218d89SKirk McKusick * the maximum transfer size permitted by the controller and/or
761bc218d89SKirk McKusick * buffering. The fs_maxcontig parameter controls the maximum
762bc218d89SKirk McKusick * number of blocks that the filesystem will read or write
763bc218d89SKirk McKusick * in a single transfer. It is calculated when the filesystem
764bc218d89SKirk McKusick * is created as maxphys / fs_bsize. The loader uses a maxphys
765bc218d89SKirk McKusick * of 128K even when running on a system that supports larger
766bc218d89SKirk McKusick * values. If the filesystem was built on a system that supports
767bc218d89SKirk McKusick * a larger maxphys (1M is typical) it will have configured
768bc218d89SKirk McKusick * fs_maxcontig for that larger system. So we bound the upper
769bc218d89SKirk McKusick * allowable limit for fs_maxconfig to be able to at least
770bc218d89SKirk McKusick * work with a 1M maxphys on the smallest block size filesystem:
771bc218d89SKirk McKusick * 1M / 4096 == 256. There is no harm in allowing the mounting of
772bc218d89SKirk McKusick * filesystems that make larger than maxphys I/O requests because
773bc218d89SKirk McKusick * those (mostly 32-bit machines) can (very slowly) handle I/O
774bc218d89SKirk McKusick * requests that exceed maxphys.
775076002f2SKirk McKusick */
776d22531d5SKirk McKusick WCHK(fs->fs_maxcontig, <, 0, %jd);
777d22531d5SKirk McKusick WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
778f0be378aSKirk McKusick FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
779f0be378aSKirk McKusick FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
78050dc4c7dSKirk McKusick MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
781b21582eeSKirk McKusick return (error);
782076002f2SKirk McKusick }
783076002f2SKirk McKusick
784076002f2SKirk McKusick /*
785e6886616SKirk McKusick * Make an extensive search to find a superblock. If the superblock
786e6886616SKirk McKusick * in the standard place cannot be used, try looking for one of the
787e6886616SKirk McKusick * backup superblocks.
788e6886616SKirk McKusick *
789e6886616SKirk McKusick * Flags are made up of the following or'ed together options:
790e6886616SKirk McKusick *
791e6886616SKirk McKusick * UFS_NOMSG indicates that superblock inconsistency error messages
792e6886616SKirk McKusick * should not be printed.
793e6886616SKirk McKusick *
794e6886616SKirk McKusick * UFS_NOCSUM causes only the superblock itself to be returned, but does
795e6886616SKirk McKusick * not read in any auxillary data structures like the cylinder group
796e6886616SKirk McKusick * summary information.
797e6886616SKirk McKusick */
798e6886616SKirk McKusick int
ffs_sbsearch(void * devfd,struct fs ** fsp,int reqflags,struct malloc_type * filltype,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))799e6886616SKirk McKusick ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags,
800e6886616SKirk McKusick struct malloc_type *filltype,
801e6886616SKirk McKusick int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
802e6886616SKirk McKusick {
803e6886616SKirk McKusick struct fsrecovery *fsr;
804e6886616SKirk McKusick struct fs *protofs;
805e6886616SKirk McKusick void *fsrbuf;
806e6886616SKirk McKusick char *cp;
807e6886616SKirk McKusick long nocsum, flags, msg, cg;
808e6886616SKirk McKusick off_t sblk, secsize;
809e6886616SKirk McKusick int error;
810e6886616SKirk McKusick
811e6886616SKirk McKusick msg = (reqflags & UFS_NOMSG) == 0;
812e6886616SKirk McKusick nocsum = reqflags & UFS_NOCSUM;
813e6886616SKirk McKusick /*
814e6886616SKirk McKusick * Try normal superblock read and return it if it works.
815e6886616SKirk McKusick *
816e6886616SKirk McKusick * Suppress messages if it fails until we find out if
817e6886616SKirk McKusick * failure can be avoided.
818e6886616SKirk McKusick */
819e6886616SKirk McKusick flags = UFS_NOMSG | nocsum;
820b13110e9SAlfredo Dal'Ava Junior error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc);
821b13110e9SAlfredo Dal'Ava Junior /*
822b13110e9SAlfredo Dal'Ava Junior * If successful or endian error, no need to try further.
823b13110e9SAlfredo Dal'Ava Junior */
824b13110e9SAlfredo Dal'Ava Junior if (error == 0 || error == EILSEQ) {
825b13110e9SAlfredo Dal'Ava Junior if (msg && error == EILSEQ)
826b13110e9SAlfredo Dal'Ava Junior printf("UFS superblock failed due to endian mismatch "
827b13110e9SAlfredo Dal'Ava Junior "between machine and filesystem\n");
828b13110e9SAlfredo Dal'Ava Junior return (error);
829b13110e9SAlfredo Dal'Ava Junior }
830e6886616SKirk McKusick /*
831e6886616SKirk McKusick * First try: ignoring hash failures.
832e6886616SKirk McKusick */
833e6886616SKirk McKusick flags |= UFS_NOHASHFAIL;
834e6886616SKirk McKusick if (msg)
835e6886616SKirk McKusick flags &= ~UFS_NOMSG;
836e6886616SKirk McKusick if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0)
837e6886616SKirk McKusick return (0);
838e6886616SKirk McKusick /*
839e6886616SKirk McKusick * Next up is to check if fields of the superblock that are
840e6886616SKirk McKusick * needed to find backup superblocks are usable.
841e6886616SKirk McKusick */
842e6886616SKirk McKusick if (msg)
843e6886616SKirk McKusick printf("Attempted recovery for standard superblock: failed\n");
84442c82aadSKirk McKusick flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG;
845e6886616SKirk McKusick if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype,
846e6886616SKirk McKusick readfunc) == 0) {
847e6886616SKirk McKusick if (msg)
848f0be378aSKirk McKusick printf("Attempt extraction of recovery data from "
849f0be378aSKirk McKusick "standard superblock.\n");
850e6886616SKirk McKusick } else {
851e6886616SKirk McKusick /*
852e6886616SKirk McKusick * Final desperation is to see if alternate superblock
853e6886616SKirk McKusick * parameters have been saved in the boot area.
854e6886616SKirk McKusick */
855e6886616SKirk McKusick if (msg)
856e6886616SKirk McKusick printf("Attempted extraction of recovery data from "
857e6886616SKirk McKusick "standard superblock: failed\nAttempt to find "
858f0be378aSKirk McKusick "boot zone recovery data.\n");
859e6886616SKirk McKusick /*
860e6886616SKirk McKusick * Look to see if recovery information has been saved.
861e6886616SKirk McKusick * If so we can generate a prototype superblock based
862e6886616SKirk McKusick * on that information.
863e6886616SKirk McKusick *
864e6886616SKirk McKusick * We need fragments-per-group, number of cylinder groups,
865e6886616SKirk McKusick * location of the superblock within the cylinder group, and
866e6886616SKirk McKusick * the conversion from filesystem fragments to disk blocks.
867e6886616SKirk McKusick *
868e6886616SKirk McKusick * When building a UFS2 filesystem, newfs(8) stores these
869e6886616SKirk McKusick * details at the end of the boot block area at the start
870e6886616SKirk McKusick * of the filesystem partition. If they have been overwritten
871e6886616SKirk McKusick * by a boot block, we fail. But usually they are there
872e6886616SKirk McKusick * and we can use them.
873e6886616SKirk McKusick *
874e6886616SKirk McKusick * We could ask the underlying device for its sector size,
875e6886616SKirk McKusick * but some devices lie. So we just try a plausible range.
876e6886616SKirk McKusick */
877e6886616SKirk McKusick error = ENOENT;
878b13110e9SAlfredo Dal'Ava Junior fsrbuf = NULL;
879e6886616SKirk McKusick for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2)
880e6886616SKirk McKusick if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize),
881e6886616SKirk McKusick &fsrbuf, secsize)) == 0)
882e6886616SKirk McKusick break;
883e6886616SKirk McKusick if (error != 0)
884e6886616SKirk McKusick goto trynowarn;
885e6886616SKirk McKusick cp = fsrbuf; /* type change to keep compiler happy */
886e6886616SKirk McKusick fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr];
887e6886616SKirk McKusick if (fsr->fsr_magic != FS_UFS2_MAGIC ||
888e6886616SKirk McKusick (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT))
889e6886616SKirk McKusick == NULL) {
890e6886616SKirk McKusick UFS_FREE(fsrbuf, filltype);
891e6886616SKirk McKusick goto trynowarn;
892e6886616SKirk McKusick }
893e6886616SKirk McKusick memset(protofs, 0, sizeof(struct fs));
894e6886616SKirk McKusick protofs->fs_fpg = fsr->fsr_fpg;
895e6886616SKirk McKusick protofs->fs_fsbtodb = fsr->fsr_fsbtodb;
896e6886616SKirk McKusick protofs->fs_sblkno = fsr->fsr_sblkno;
897e6886616SKirk McKusick protofs->fs_magic = fsr->fsr_magic;
898e6886616SKirk McKusick protofs->fs_ncg = fsr->fsr_ncg;
899e6886616SKirk McKusick UFS_FREE(fsrbuf, filltype);
900e6886616SKirk McKusick }
901e6886616SKirk McKusick /*
902e6886616SKirk McKusick * Scan looking for alternative superblocks.
903e6886616SKirk McKusick */
904f0be378aSKirk McKusick flags = nocsum;
905f0be378aSKirk McKusick if (!msg)
906f0be378aSKirk McKusick flags |= UFS_NOMSG;
907e6886616SKirk McKusick for (cg = 0; cg < protofs->fs_ncg; cg++) {
908f0be378aSKirk McKusick sblk = fsbtodb(protofs, cgsblock(protofs, cg));
909f0be378aSKirk McKusick if (msg)
910f0be378aSKirk McKusick printf("Try cg %ld at sblock loc %jd\n", cg,
911f0be378aSKirk McKusick (intmax_t)sblk);
912f0be378aSKirk McKusick if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype,
913e6886616SKirk McKusick readfunc) == 0) {
914e6886616SKirk McKusick if (msg)
915f0be378aSKirk McKusick printf("Succeeded with alternate superblock "
916f0be378aSKirk McKusick "at %jd\n", (intmax_t)sblk);
917e6886616SKirk McKusick UFS_FREE(protofs, filltype);
918e6886616SKirk McKusick return (0);
919e6886616SKirk McKusick }
920e6886616SKirk McKusick }
921e6886616SKirk McKusick UFS_FREE(protofs, filltype);
922e6886616SKirk McKusick /*
923e6886616SKirk McKusick * Our alternate superblock strategies failed. Our last ditch effort
924e6886616SKirk McKusick * is to see if the standard superblock has only non-critical errors.
925e6886616SKirk McKusick */
926e6886616SKirk McKusick trynowarn:
927e6886616SKirk McKusick flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum;
928e6886616SKirk McKusick if (msg) {
929f0be378aSKirk McKusick printf("Finding an alternate superblock failed.\nCheck for "
930f0be378aSKirk McKusick "only non-critical errors in standard superblock\n");
931e6886616SKirk McKusick flags &= ~UFS_NOMSG;
932e6886616SKirk McKusick }
933f0be378aSKirk McKusick if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) {
934e6886616SKirk McKusick if (msg)
935f0be378aSKirk McKusick printf("Failed, superblock has critical errors\n");
936f0be378aSKirk McKusick return (ENOENT);
937f0be378aSKirk McKusick }
938f0be378aSKirk McKusick if (msg)
939f0be378aSKirk McKusick printf("Success, using standard superblock with "
940f0be378aSKirk McKusick "non-critical errors.\n");
941e6886616SKirk McKusick return (0);
942e6886616SKirk McKusick }
943e6886616SKirk McKusick
944e6886616SKirk McKusick /*
945dffce215SKirk McKusick * Write a superblock to the devfd device from the memory pointed to by fs.
946dffce215SKirk McKusick * Write out the superblock summary information if it is present.
947dffce215SKirk McKusick *
948dffce215SKirk McKusick * If the write is successful, zero is returned. Otherwise one of the
949dffce215SKirk McKusick * following error values is returned:
950dffce215SKirk McKusick * EIO: failed to write superblock.
951dffce215SKirk McKusick * EIO: failed to write superblock summary information.
952dffce215SKirk McKusick */
953dffce215SKirk McKusick int
ffs_sbput(void * devfd,struct fs * fs,off_t loc,int (* writefunc)(void * devfd,off_t loc,void * buf,int size))954dffce215SKirk McKusick ffs_sbput(void *devfd, struct fs *fs, off_t loc,
955dffce215SKirk McKusick int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
956dffce215SKirk McKusick {
957c2cd605eSKirk McKusick struct fs_summary_info *fs_si;
958dffce215SKirk McKusick int i, error, blks, size;
959dffce215SKirk McKusick uint8_t *space;
960dffce215SKirk McKusick
961dffce215SKirk McKusick /*
962dffce215SKirk McKusick * If there is summary information, write it first, so if there
963dffce215SKirk McKusick * is an error, the superblock will not be marked as clean.
964dffce215SKirk McKusick */
96534816cb9SKirk McKusick if (fs->fs_si != NULL && fs->fs_csp != NULL) {
966dffce215SKirk McKusick blks = howmany(fs->fs_cssize, fs->fs_fsize);
967dffce215SKirk McKusick space = (uint8_t *)fs->fs_csp;
968dffce215SKirk McKusick for (i = 0; i < blks; i += fs->fs_frag) {
969dffce215SKirk McKusick size = fs->fs_bsize;
970dffce215SKirk McKusick if (i + fs->fs_frag > blks)
971dffce215SKirk McKusick size = (blks - i) * fs->fs_fsize;
972dffce215SKirk McKusick if ((error = (*writefunc)(devfd,
973dffce215SKirk McKusick dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
974dffce215SKirk McKusick space, size)) != 0)
975dffce215SKirk McKusick return (error);
976dffce215SKirk McKusick space += size;
977dffce215SKirk McKusick }
978dffce215SKirk McKusick }
979dffce215SKirk McKusick fs->fs_fmod = 0;
980c2cd605eSKirk McKusick ffs_oldfscompat_write(fs);
981c2cd605eSKirk McKusick #ifdef _KERNEL
982c2cd605eSKirk McKusick fs->fs_time = time_second;
983c2cd605eSKirk McKusick #else /* User Code */
98493440bbeSKirk McKusick fs->fs_time = time(NULL);
985c2cd605eSKirk McKusick #endif
98693440bbeSKirk McKusick /* Clear the pointers for the duration of writing. */
98793440bbeSKirk McKusick fs_si = fs->fs_si;
98893440bbeSKirk McKusick fs->fs_si = NULL;
989ade67b50SKirk McKusick fs->fs_ckhash = ffs_calc_sbhash(fs);
99093440bbeSKirk McKusick error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
991c2cd605eSKirk McKusick /*
992c2cd605eSKirk McKusick * A negative error code is returned when a copy of the
993c2cd605eSKirk McKusick * superblock has been made which is discarded when the I/O
994c2cd605eSKirk McKusick * is done. So the fs_si field does not and indeed cannot be
995c2cd605eSKirk McKusick * restored after the write is done. Convert the error code
996c2cd605eSKirk McKusick * back to its usual positive value when returning it.
997c2cd605eSKirk McKusick */
998c2cd605eSKirk McKusick if (error < 0)
999c2cd605eSKirk McKusick return (-error - 1);
100093440bbeSKirk McKusick fs->fs_si = fs_si;
1001dffce215SKirk McKusick return (error);
1002dffce215SKirk McKusick }
1003dffce215SKirk McKusick
1004dffce215SKirk McKusick /*
1005ec888383SKirk McKusick * Calculate the check-hash for a superblock.
1006ec888383SKirk McKusick */
1007ade67b50SKirk McKusick uint32_t
ffs_calc_sbhash(struct fs * fs)1008ade67b50SKirk McKusick ffs_calc_sbhash(struct fs *fs)
1009ec888383SKirk McKusick {
1010ec888383SKirk McKusick uint32_t ckhash, save_ckhash;
1011ec888383SKirk McKusick
1012ec888383SKirk McKusick /*
1013ec888383SKirk McKusick * A filesystem that was using a superblock ckhash may be moved
1014ec888383SKirk McKusick * to an older kernel that does not support ckhashes. The
1015ec888383SKirk McKusick * older kernel will clear the FS_METACKHASH flag indicating
1016ec888383SKirk McKusick * that it does not update hashes. When the disk is moved back
1017ec888383SKirk McKusick * to a kernel capable of ckhashes it disables them on mount:
1018ec888383SKirk McKusick *
1019ec888383SKirk McKusick * if ((fs->fs_flags & FS_METACKHASH) == 0)
1020ec888383SKirk McKusick * fs->fs_metackhash = 0;
1021ec888383SKirk McKusick *
1022ec888383SKirk McKusick * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
1023ec888383SKirk McKusick * old stale value in the fs->fs_ckhash field. Thus the need to
1024ec888383SKirk McKusick * just accept what is there.
1025ec888383SKirk McKusick */
1026ec888383SKirk McKusick if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
1027ec888383SKirk McKusick return (fs->fs_ckhash);
1028ec888383SKirk McKusick
1029ec888383SKirk McKusick save_ckhash = fs->fs_ckhash;
1030ec888383SKirk McKusick fs->fs_ckhash = 0;
1031ec888383SKirk McKusick /*
1032ec888383SKirk McKusick * If newly read from disk, the caller is responsible for
1033ec888383SKirk McKusick * verifying that fs->fs_sbsize <= SBLOCKSIZE.
1034ec888383SKirk McKusick */
1035ec888383SKirk McKusick ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
1036ec888383SKirk McKusick fs->fs_ckhash = save_ckhash;
1037ec888383SKirk McKusick return (ckhash);
1038ec888383SKirk McKusick }
1039ec888383SKirk McKusick
1040ec888383SKirk McKusick /*
1041df8bae1dSRodney W. Grimes * Update the frsum fields to reflect addition or deletion
1042df8bae1dSRodney W. Grimes * of some frags.
1043df8bae1dSRodney W. Grimes */
1044df8bae1dSRodney W. Grimes void
ffs_fragacct(struct fs * fs,int fragmap,int32_t fraglist[],int cnt)104515c377c3SEd Maste ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
1046df8bae1dSRodney W. Grimes {
1047df8bae1dSRodney W. Grimes int inblk;
104805f4ff5dSPoul-Henning Kamp int field, subfield;
104905f4ff5dSPoul-Henning Kamp int siz, pos;
1050df8bae1dSRodney W. Grimes
1051df8bae1dSRodney W. Grimes inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
1052df8bae1dSRodney W. Grimes fragmap <<= 1;
1053df8bae1dSRodney W. Grimes for (siz = 1; siz < fs->fs_frag; siz++) {
1054df8bae1dSRodney W. Grimes if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
1055df8bae1dSRodney W. Grimes continue;
1056df8bae1dSRodney W. Grimes field = around[siz];
1057df8bae1dSRodney W. Grimes subfield = inside[siz];
1058df8bae1dSRodney W. Grimes for (pos = siz; pos <= fs->fs_frag; pos++) {
1059df8bae1dSRodney W. Grimes if ((fragmap & field) == subfield) {
1060df8bae1dSRodney W. Grimes fraglist[siz] += cnt;
1061df8bae1dSRodney W. Grimes pos += siz;
1062df8bae1dSRodney W. Grimes field <<= siz;
1063df8bae1dSRodney W. Grimes subfield <<= siz;
1064df8bae1dSRodney W. Grimes }
1065df8bae1dSRodney W. Grimes field <<= 1;
1066df8bae1dSRodney W. Grimes subfield <<= 1;
1067df8bae1dSRodney W. Grimes }
1068df8bae1dSRodney W. Grimes }
1069df8bae1dSRodney W. Grimes }
1070df8bae1dSRodney W. Grimes
1071df8bae1dSRodney W. Grimes /*
1072df8bae1dSRodney W. Grimes * block operations
1073df8bae1dSRodney W. Grimes *
1074df8bae1dSRodney W. Grimes * check if a block is available
1075df8bae1dSRodney W. Grimes */
1076df8bae1dSRodney W. Grimes int
ffs_isblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)107715c377c3SEd Maste ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
1078df8bae1dSRodney W. Grimes {
1079df8bae1dSRodney W. Grimes unsigned char mask;
1080df8bae1dSRodney W. Grimes
1081df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) {
1082df8bae1dSRodney W. Grimes case 8:
1083df8bae1dSRodney W. Grimes return (cp[h] == 0xff);
1084df8bae1dSRodney W. Grimes case 4:
1085df8bae1dSRodney W. Grimes mask = 0x0f << ((h & 0x1) << 2);
1086df8bae1dSRodney W. Grimes return ((cp[h >> 1] & mask) == mask);
1087df8bae1dSRodney W. Grimes case 2:
1088df8bae1dSRodney W. Grimes mask = 0x03 << ((h & 0x3) << 1);
1089df8bae1dSRodney W. Grimes return ((cp[h >> 2] & mask) == mask);
1090df8bae1dSRodney W. Grimes case 1:
1091df8bae1dSRodney W. Grimes mask = 0x01 << (h & 0x7);
1092df8bae1dSRodney W. Grimes return ((cp[h >> 3] & mask) == mask);
1093df8bae1dSRodney W. Grimes default:
1094113db2ddSJeff Roberson #ifdef _KERNEL
1095df8bae1dSRodney W. Grimes panic("ffs_isblock");
1096113db2ddSJeff Roberson #endif
1097113db2ddSJeff Roberson break;
1098113db2ddSJeff Roberson }
1099113db2ddSJeff Roberson return (0);
1100113db2ddSJeff Roberson }
1101113db2ddSJeff Roberson
1102113db2ddSJeff Roberson /*
1103113db2ddSJeff Roberson * check if a block is free
1104113db2ddSJeff Roberson */
1105113db2ddSJeff Roberson int
ffs_isfreeblock(struct fs * fs,uint8_t * cp,ufs1_daddr_t h)1106831b1ff7SKirk McKusick ffs_isfreeblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
1107113db2ddSJeff Roberson {
1108113db2ddSJeff Roberson
1109113db2ddSJeff Roberson switch ((int)fs->fs_frag) {
1110113db2ddSJeff Roberson case 8:
1111113db2ddSJeff Roberson return (cp[h] == 0);
1112113db2ddSJeff Roberson case 4:
1113113db2ddSJeff Roberson return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1114113db2ddSJeff Roberson case 2:
1115113db2ddSJeff Roberson return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1116113db2ddSJeff Roberson case 1:
1117113db2ddSJeff Roberson return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1118113db2ddSJeff Roberson default:
1119113db2ddSJeff Roberson #ifdef _KERNEL
1120113db2ddSJeff Roberson panic("ffs_isfreeblock");
1121113db2ddSJeff Roberson #endif
1122113db2ddSJeff Roberson break;
1123df8bae1dSRodney W. Grimes }
1124589c7af9SKirk McKusick return (0);
1125df8bae1dSRodney W. Grimes }
1126df8bae1dSRodney W. Grimes
1127df8bae1dSRodney W. Grimes /*
1128df8bae1dSRodney W. Grimes * take a block out of the map
1129df8bae1dSRodney W. Grimes */
1130df8bae1dSRodney W. Grimes void
ffs_clrblock(struct fs * fs,uint8_t * cp,ufs1_daddr_t h)1131831b1ff7SKirk McKusick ffs_clrblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
1132df8bae1dSRodney W. Grimes {
1133df8bae1dSRodney W. Grimes
1134df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) {
1135df8bae1dSRodney W. Grimes case 8:
1136df8bae1dSRodney W. Grimes cp[h] = 0;
1137df8bae1dSRodney W. Grimes return;
1138df8bae1dSRodney W. Grimes case 4:
1139df8bae1dSRodney W. Grimes cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1140df8bae1dSRodney W. Grimes return;
1141df8bae1dSRodney W. Grimes case 2:
1142df8bae1dSRodney W. Grimes cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1143df8bae1dSRodney W. Grimes return;
1144df8bae1dSRodney W. Grimes case 1:
1145df8bae1dSRodney W. Grimes cp[h >> 3] &= ~(0x01 << (h & 0x7));
1146df8bae1dSRodney W. Grimes return;
1147df8bae1dSRodney W. Grimes default:
1148113db2ddSJeff Roberson #ifdef _KERNEL
1149df8bae1dSRodney W. Grimes panic("ffs_clrblock");
1150113db2ddSJeff Roberson #endif
1151113db2ddSJeff Roberson break;
1152df8bae1dSRodney W. Grimes }
1153df8bae1dSRodney W. Grimes }
1154df8bae1dSRodney W. Grimes
1155df8bae1dSRodney W. Grimes /*
1156df8bae1dSRodney W. Grimes * put a block into the map
1157df8bae1dSRodney W. Grimes */
1158df8bae1dSRodney W. Grimes void
ffs_setblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)115915c377c3SEd Maste ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
1160df8bae1dSRodney W. Grimes {
1161df8bae1dSRodney W. Grimes
1162df8bae1dSRodney W. Grimes switch ((int)fs->fs_frag) {
1163df8bae1dSRodney W. Grimes case 8:
1164df8bae1dSRodney W. Grimes cp[h] = 0xff;
1165df8bae1dSRodney W. Grimes return;
1166df8bae1dSRodney W. Grimes case 4:
1167df8bae1dSRodney W. Grimes cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1168df8bae1dSRodney W. Grimes return;
1169df8bae1dSRodney W. Grimes case 2:
1170df8bae1dSRodney W. Grimes cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1171df8bae1dSRodney W. Grimes return;
1172df8bae1dSRodney W. Grimes case 1:
1173df8bae1dSRodney W. Grimes cp[h >> 3] |= (0x01 << (h & 0x7));
1174df8bae1dSRodney W. Grimes return;
1175df8bae1dSRodney W. Grimes default:
1176113db2ddSJeff Roberson #ifdef _KERNEL
1177df8bae1dSRodney W. Grimes panic("ffs_setblock");
1178113db2ddSJeff Roberson #endif
1179113db2ddSJeff Roberson break;
1180df8bae1dSRodney W. Grimes }
1181df8bae1dSRodney W. Grimes }
1182113db2ddSJeff Roberson
1183113db2ddSJeff Roberson /*
1184113db2ddSJeff Roberson * Update the cluster map because of an allocation or free.
1185113db2ddSJeff Roberson *
1186113db2ddSJeff Roberson * Cnt == 1 means free; cnt == -1 means allocating.
1187113db2ddSJeff Roberson */
1188113db2ddSJeff Roberson void
ffs_clusteracct(struct fs * fs,struct cg * cgp,ufs1_daddr_t blkno,int cnt)118915c377c3SEd Maste ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
1190113db2ddSJeff Roberson {
1191113db2ddSJeff Roberson int32_t *sump;
1192113db2ddSJeff Roberson int32_t *lp;
1193831b1ff7SKirk McKusick uint8_t *freemapp, *mapp;
1194e1c27cf7SKirk McKusick int i, start, end, forw, back, map;
1195831b1ff7SKirk McKusick uint64_t bit;
1196113db2ddSJeff Roberson
1197113db2ddSJeff Roberson if (fs->fs_contigsumsize <= 0)
1198113db2ddSJeff Roberson return;
1199113db2ddSJeff Roberson freemapp = cg_clustersfree(cgp);
1200113db2ddSJeff Roberson sump = cg_clustersum(cgp);
1201113db2ddSJeff Roberson /*
1202113db2ddSJeff Roberson * Allocate or clear the actual block.
1203113db2ddSJeff Roberson */
1204113db2ddSJeff Roberson if (cnt > 0)
1205113db2ddSJeff Roberson setbit(freemapp, blkno);
1206113db2ddSJeff Roberson else
1207113db2ddSJeff Roberson clrbit(freemapp, blkno);
1208113db2ddSJeff Roberson /*
1209113db2ddSJeff Roberson * Find the size of the cluster going forward.
1210113db2ddSJeff Roberson */
1211113db2ddSJeff Roberson start = blkno + 1;
1212113db2ddSJeff Roberson end = start + fs->fs_contigsumsize;
1213113db2ddSJeff Roberson if (end >= cgp->cg_nclusterblks)
1214113db2ddSJeff Roberson end = cgp->cg_nclusterblks;
1215113db2ddSJeff Roberson mapp = &freemapp[start / NBBY];
1216113db2ddSJeff Roberson map = *mapp++;
1217e1c27cf7SKirk McKusick bit = 1U << (start % NBBY);
1218113db2ddSJeff Roberson for (i = start; i < end; i++) {
1219113db2ddSJeff Roberson if ((map & bit) == 0)
1220113db2ddSJeff Roberson break;
1221113db2ddSJeff Roberson if ((i & (NBBY - 1)) != (NBBY - 1)) {
1222113db2ddSJeff Roberson bit <<= 1;
1223113db2ddSJeff Roberson } else {
1224113db2ddSJeff Roberson map = *mapp++;
1225113db2ddSJeff Roberson bit = 1;
1226113db2ddSJeff Roberson }
1227113db2ddSJeff Roberson }
1228113db2ddSJeff Roberson forw = i - start;
1229113db2ddSJeff Roberson /*
1230113db2ddSJeff Roberson * Find the size of the cluster going backward.
1231113db2ddSJeff Roberson */
1232113db2ddSJeff Roberson start = blkno - 1;
1233113db2ddSJeff Roberson end = start - fs->fs_contigsumsize;
1234113db2ddSJeff Roberson if (end < 0)
1235113db2ddSJeff Roberson end = -1;
1236113db2ddSJeff Roberson mapp = &freemapp[start / NBBY];
1237113db2ddSJeff Roberson map = *mapp--;
1238e1c27cf7SKirk McKusick bit = 1U << (start % NBBY);
1239113db2ddSJeff Roberson for (i = start; i > end; i--) {
1240113db2ddSJeff Roberson if ((map & bit) == 0)
1241113db2ddSJeff Roberson break;
1242113db2ddSJeff Roberson if ((i & (NBBY - 1)) != 0) {
1243113db2ddSJeff Roberson bit >>= 1;
1244113db2ddSJeff Roberson } else {
1245113db2ddSJeff Roberson map = *mapp--;
1246e1c27cf7SKirk McKusick bit = 1U << (NBBY - 1);
1247113db2ddSJeff Roberson }
1248113db2ddSJeff Roberson }
1249113db2ddSJeff Roberson back = start - i;
1250113db2ddSJeff Roberson /*
1251113db2ddSJeff Roberson * Account for old cluster and the possibly new forward and
1252113db2ddSJeff Roberson * back clusters.
1253113db2ddSJeff Roberson */
1254113db2ddSJeff Roberson i = back + forw + 1;
1255113db2ddSJeff Roberson if (i > fs->fs_contigsumsize)
1256113db2ddSJeff Roberson i = fs->fs_contigsumsize;
1257113db2ddSJeff Roberson sump[i] += cnt;
1258113db2ddSJeff Roberson if (back > 0)
1259113db2ddSJeff Roberson sump[back] -= cnt;
1260113db2ddSJeff Roberson if (forw > 0)
1261113db2ddSJeff Roberson sump[forw] -= cnt;
1262113db2ddSJeff Roberson /*
1263113db2ddSJeff Roberson * Update cluster summary information.
1264113db2ddSJeff Roberson */
1265113db2ddSJeff Roberson lp = &sump[fs->fs_contigsumsize];
1266113db2ddSJeff Roberson for (i = fs->fs_contigsumsize; i > 0; i--)
1267113db2ddSJeff Roberson if (*lp-- > 0)
1268113db2ddSJeff Roberson break;
1269113db2ddSJeff Roberson fs->fs_maxcluster[cgp->cg_cgx] = i;
1270113db2ddSJeff Roberson }
1271