1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_subr.c 8.5 (Berkeley) 3/21/95
32 */
33
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/limits.h>
38
39 #ifndef _KERNEL
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #include <sys/errno.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
48
49 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
50 uint32_t ffs_calc_sbhash(struct fs *);
51 struct malloc_type;
52 #define UFS_MALLOC(size, type, flags) malloc(size)
53 #define UFS_FREE(ptr, type) free(ptr)
54 #define maxphys MAXPHYS
55
56 #else /* _KERNEL */
57 #include <sys/systm.h>
58 #include <sys/gsb_crc32.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/vnode.h>
63 #include <sys/bio.h>
64 #include <sys/buf.h>
65 #include <sys/ucred.h>
66 #include <sys/sysctl.h>
67
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/inode.h>
70 #include <ufs/ufs/extattr.h>
71 #include <ufs/ufs/ufsmount.h>
72 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ffs/ffs_extern.h>
74 #include <ufs/ffs/fs.h>
75
76 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
77 #define UFS_FREE(ptr, type) free(ptr, type)
78
79 #endif /* _KERNEL */
80
81 /*
82 * Verify an inode check-hash.
83 */
84 int
ffs_verify_dinode_ckhash(struct fs * fs,struct ufs2_dinode * dip)85 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
86 {
87 uint32_t ckhash, save_ckhash;
88
89 /*
90 * Return success if unallocated or we are not doing inode check-hash.
91 */
92 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
93 return (0);
94 /*
95 * Exclude di_ckhash from the crc32 calculation, e.g., always use
96 * a check-hash value of zero when calculating the check-hash.
97 */
98 save_ckhash = dip->di_ckhash;
99 dip->di_ckhash = 0;
100 ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
101 dip->di_ckhash = save_ckhash;
102 if (save_ckhash == ckhash)
103 return (0);
104 return (EINVAL);
105 }
106
107 /*
108 * Update an inode check-hash.
109 */
110 void
ffs_update_dinode_ckhash(struct fs * fs,struct ufs2_dinode * dip)111 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
112 {
113
114 if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
115 return;
116 /*
117 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
118 * a check-hash value of zero when calculating the new check-hash.
119 */
120 dip->di_ckhash = 0;
121 dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
122 }
123
124 /*
125 * These are the low-level functions that actually read and write
126 * the superblock and its associated data.
127 */
128 static off_t sblock_try[] = SBLOCKSEARCH;
129 static int readsuper(void *, struct fs **, off_t, int,
130 int (*)(void *, off_t, void **, int));
131 static void ffs_oldfscompat_read(struct fs *, ufs2_daddr_t);
132 static int validate_sblock(struct fs *, int);
133
134 /*
135 * Read a superblock from the devfd device.
136 *
137 * If an alternate superblock is specified, it is read. Otherwise the
138 * set of locations given in the SBLOCKSEARCH list is searched for a
139 * superblock. Memory is allocated for the superblock by the readfunc and
140 * is returned. If filltype is non-NULL, additional memory is allocated
141 * of type filltype and filled in with the superblock summary information.
142 * All memory is freed when any error is returned.
143 *
144 * If a superblock is found, zero is returned. Otherwise one of the
145 * following error values is returned:
146 * EIO: non-existent or truncated superblock.
147 * EIO: error reading summary information.
148 * ENOENT: no usable known superblock found.
149 * EILSEQ: filesystem with wrong byte order found.
150 * ENOMEM: failed to allocate space for the superblock.
151 * EINVAL: The previous newfs operation on this volume did not complete.
152 * The administrator must complete newfs before using this volume.
153 */
154 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))155 ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
156 struct malloc_type *filltype,
157 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
158 {
159 struct fs *fs;
160 struct fs_summary_info *fs_si;
161 int i, error;
162 uint64_t size, blks;
163 uint8_t *space;
164 int32_t *lp;
165 char *buf;
166
167 fs = NULL;
168 *fsp = NULL;
169 if (sblock != UFS_STDSB) {
170 if ((error = readsuper(devfd, &fs, sblock,
171 flags | UFS_ALTSBLK, readfunc)) != 0) {
172 if (fs != NULL)
173 UFS_FREE(fs, filltype);
174 return (error);
175 }
176 } else {
177 for (i = 0; sblock_try[i] != -1; i++) {
178 if ((error = readsuper(devfd, &fs, sblock_try[i],
179 flags, readfunc)) == 0) {
180 if ((flags & UFS_NOCSUM) != 0) {
181 *fsp = fs;
182 return (0);
183 }
184 break;
185 }
186 if (fs != NULL) {
187 UFS_FREE(fs, filltype);
188 fs = NULL;
189 }
190 if (error == ENOENT)
191 continue;
192 return (error);
193 }
194 if (sblock_try[i] == -1)
195 return (ENOENT);
196 }
197 /*
198 * Read in the superblock summary information.
199 */
200 size = fs->fs_cssize;
201 blks = howmany(size, fs->fs_fsize);
202 if (fs->fs_contigsumsize > 0)
203 size += fs->fs_ncg * sizeof(int32_t);
204 size += fs->fs_ncg * sizeof(uint8_t);
205 if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) {
206 UFS_FREE(fs, filltype);
207 return (ENOMEM);
208 }
209 bzero(fs_si, sizeof(*fs_si));
210 fs->fs_si = fs_si;
211 if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) {
212 UFS_FREE(fs->fs_si, filltype);
213 UFS_FREE(fs, filltype);
214 return (ENOMEM);
215 }
216 fs->fs_csp = (struct csum *)space;
217 for (i = 0; i < blks; i += fs->fs_frag) {
218 size = fs->fs_bsize;
219 if (i + fs->fs_frag > blks)
220 size = (blks - i) * fs->fs_fsize;
221 buf = NULL;
222 error = (*readfunc)(devfd,
223 dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
224 if (error) {
225 if (buf != NULL)
226 UFS_FREE(buf, filltype);
227 UFS_FREE(fs->fs_csp, filltype);
228 UFS_FREE(fs->fs_si, filltype);
229 UFS_FREE(fs, filltype);
230 return (error);
231 }
232 memcpy(space, buf, size);
233 UFS_FREE(buf, filltype);
234 space += size;
235 }
236 if (fs->fs_contigsumsize > 0) {
237 fs->fs_maxcluster = lp = (int32_t *)space;
238 for (i = 0; i < fs->fs_ncg; i++)
239 *lp++ = fs->fs_contigsumsize;
240 space = (uint8_t *)lp;
241 }
242 size = fs->fs_ncg * sizeof(uint8_t);
243 fs->fs_contigdirs = (uint8_t *)space;
244 bzero(fs->fs_contigdirs, size);
245 *fsp = fs;
246 return (0);
247 }
248
249 /*
250 * Try to read a superblock from the location specified by sblockloc.
251 * Return zero on success or an errno on failure.
252 */
253 static int
readsuper(void * devfd,struct fs ** fsp,off_t sblockloc,int flags,int (* readfunc)(void * devfd,off_t loc,void ** bufp,int size))254 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int flags,
255 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
256 {
257 struct fs *fs;
258 int error, res;
259 uint32_t ckhash;
260
261 error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
262 if (error != 0)
263 return (error);
264 fs = *fsp;
265 if (fs->fs_magic == FS_BAD_MAGIC)
266 return (EINVAL);
267 /*
268 * For UFS1 with a 65536 block size, the first backup superblock
269 * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2
270 * is the first location checked, the first backup is the superblock
271 * that will be accessed. Here we fail the lookup so that we can
272 * retry with the correct location for the UFS1 superblock.
273 */
274 if (fs->fs_magic == FS_UFS1_MAGIC && (flags & UFS_ALTSBLK) == 0 &&
275 fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2)
276 return (ENOENT);
277 ffs_oldfscompat_read(fs, sblockloc);
278 if ((error = validate_sblock(fs, flags)) > 0)
279 return (error);
280 /*
281 * If the filesystem has been run on a kernel without
282 * metadata check hashes, disable them.
283 */
284 if ((fs->fs_flags & FS_METACKHASH) == 0)
285 fs->fs_metackhash = 0;
286 /*
287 * Clear any check-hashes that are not maintained
288 * by this kernel. Also clear any unsupported flags.
289 */
290 fs->fs_metackhash &= CK_SUPPORTED;
291 fs->fs_flags &= FS_SUPPORTED;
292 if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
293 if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) ==
294 (UFS_NOMSG | UFS_NOHASHFAIL))
295 return (0);
296 if ((flags & UFS_NOMSG) != 0)
297 return (EINTEGRITY);
298 #ifdef _KERNEL
299 res = uprintf("Superblock check-hash failed: recorded "
300 "check-hash 0x%x != computed check-hash 0x%x%s\n",
301 fs->fs_ckhash, ckhash,
302 (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : "");
303 #else
304 res = 0;
305 #endif
306 /*
307 * Print check-hash failure if no controlling terminal
308 * in kernel or always if in user-mode (libufs).
309 */
310 if (res == 0)
311 printf("Superblock check-hash failed: recorded "
312 "check-hash 0x%x != computed check-hash "
313 "0x%x%s\n", fs->fs_ckhash, ckhash,
314 (flags & UFS_NOHASHFAIL) ? " (Ignored)" : "");
315 if ((flags & UFS_NOHASHFAIL) != 0)
316 return (0);
317 return (EINTEGRITY);
318 }
319 /* Have to set for old filesystems that predate this field */
320 fs->fs_sblockactualloc = sblockloc;
321 /* Not yet any summary information */
322 fs->fs_si = NULL;
323 return (0);
324 }
325
326 /*
327 * Sanity checks for loading old filesystem superblocks.
328 * See ffs_oldfscompat_write below for unwound actions.
329 *
330 * XXX - Parts get retired eventually.
331 * Unfortunately new bits get added.
332 */
333 static void
ffs_oldfscompat_read(struct fs * fs,ufs2_daddr_t sblockloc)334 ffs_oldfscompat_read(struct fs *fs, ufs2_daddr_t sblockloc)
335 {
336 uint64_t maxfilesize;
337
338 /*
339 * If not yet done, update fs_flags location and value of fs_sblockloc.
340 */
341 if ((fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
342 fs->fs_flags = fs->fs_old_flags;
343 fs->fs_old_flags |= FS_FLAGS_UPDATED;
344 fs->fs_sblockloc = sblockloc;
345 }
346 switch (fs->fs_magic) {
347 case FS_UFS2_MAGIC:
348 /* No changes for now */
349 break;
350
351 case FS_UFS1_MAGIC:
352 /*
353 * If not yet done, update UFS1 superblock with new wider fields
354 */
355 if (fs->fs_maxbsize != fs->fs_bsize) {
356 fs->fs_maxbsize = fs->fs_bsize;
357 fs->fs_time = fs->fs_old_time;
358 fs->fs_size = fs->fs_old_size;
359 fs->fs_dsize = fs->fs_old_dsize;
360 fs->fs_csaddr = fs->fs_old_csaddr;
361 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir;
362 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree;
363 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree;
364 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree;
365 }
366 if (fs->fs_old_inodefmt < FS_44INODEFMT) {
367 fs->fs_maxfilesize = ((uint64_t)1 << 31) - 1;
368 fs->fs_qbmask = ~fs->fs_bmask;
369 fs->fs_qfmask = ~fs->fs_fmask;
370 }
371 fs->fs_save_maxfilesize = fs->fs_maxfilesize;
372 maxfilesize = (uint64_t)0x80000000 * fs->fs_bsize - 1;
373 if (fs->fs_maxfilesize > maxfilesize)
374 fs->fs_maxfilesize = maxfilesize;
375 break;
376 }
377 /* Compatibility for old filesystems */
378 if (fs->fs_avgfilesize <= 0)
379 fs->fs_avgfilesize = AVFILESIZ;
380 if (fs->fs_avgfpdir <= 0)
381 fs->fs_avgfpdir = AFPDIR;
382 }
383
384 /*
385 * Unwinding superblock updates for old filesystems.
386 * See ffs_oldfscompat_read above for details.
387 *
388 * XXX - Parts get retired eventually.
389 * Unfortunately new bits get added.
390 */
391 void
ffs_oldfscompat_write(struct fs * fs)392 ffs_oldfscompat_write(struct fs *fs)
393 {
394
395 switch (fs->fs_magic) {
396 case FS_UFS1_MAGIC:
397 if (fs->fs_sblockloc != SBLOCK_UFS1 &&
398 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
399 printf(
400 "WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
401 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS1);
402 fs->fs_sblockloc = SBLOCK_UFS1;
403 }
404 /*
405 * Copy back UFS2 updated fields that UFS1 inspects.
406 */
407 fs->fs_old_time = fs->fs_time;
408 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir;
409 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree;
410 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree;
411 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree;
412 if (fs->fs_save_maxfilesize != 0)
413 fs->fs_maxfilesize = fs->fs_save_maxfilesize;
414 break;
415 case FS_UFS2_MAGIC:
416 if (fs->fs_sblockloc != SBLOCK_UFS2 &&
417 (fs->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
418 printf(
419 "WARNING: %s: correcting fs_sblockloc from %jd to %d\n",
420 fs->fs_fsmnt, fs->fs_sblockloc, SBLOCK_UFS2);
421 fs->fs_sblockloc = SBLOCK_UFS2;
422 }
423 break;
424 }
425 }
426
427 /*
428 * Sanity checks for loading old filesystem inodes.
429 *
430 * XXX - Parts get retired eventually.
431 * Unfortunately new bits get added.
432 */
433 static int prttimechgs = 0;
434 #ifdef _KERNEL
435 SYSCTL_NODE(_vfs, OID_AUTO, ffs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
436 "FFS filesystem");
437 SYSCTL_INT(_vfs_ffs, OID_AUTO, prttimechgs, CTLFLAG_RWTUN, &prttimechgs, 0,
438 "print UFS1 time changes made to inodes");
439 #endif /* _KERNEL */
440 bool
ffs_oldfscompat_inode_read(struct fs * fs,union dinodep dp,time_t now)441 ffs_oldfscompat_inode_read(struct fs *fs, union dinodep dp, time_t now)
442 {
443 bool change;
444
445 change = false;
446 switch (fs->fs_magic) {
447 case FS_UFS2_MAGIC:
448 /* No changes for now */
449 break;
450
451 case FS_UFS1_MAGIC:
452 /*
453 * With the change to unsigned time values in UFS1, times set
454 * before Jan 1, 1970 will appear to be in the future. Check
455 * for future times and set them to be the current time.
456 */
457 if (dp.dp1->di_ctime > now) {
458 if (prttimechgs)
459 printf("ctime %ud changed to %ld\n",
460 dp.dp1->di_ctime, (long)now);
461 dp.dp1->di_ctime = now;
462 change = true;
463 }
464 if (dp.dp1->di_mtime > now) {
465 if (prttimechgs)
466 printf("mtime %ud changed to %ld\n",
467 dp.dp1->di_mtime, (long)now);
468 dp.dp1->di_mtime = now;
469 dp.dp1->di_ctime = now;
470 change = true;
471 }
472 if (dp.dp1->di_atime > now) {
473 if (prttimechgs)
474 printf("atime %ud changed to %ld\n",
475 dp.dp1->di_atime, (long)now);
476 dp.dp1->di_atime = now;
477 dp.dp1->di_ctime = now;
478 change = true;
479 }
480 break;
481 }
482 return (change);
483 }
484
485 /*
486 * Verify the filesystem values.
487 */
488 #define ILOG2(num) (fls(num) - 1)
489 #ifdef STANDALONE_SMALL
490 #define MPRINT(...) do { } while (0)
491 #else
492 #define MPRINT(...) if (prtmsg) printf(__VA_ARGS__)
493 #endif
494 #define FCHK(lhs, op, rhs, fmt) \
495 if (lhs op rhs) { \
496 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
497 #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, \
498 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \
499 if (error < 0) \
500 return (ENOENT); \
501 if (error == 0) \
502 error = ENOENT; \
503 }
504 #define WCHK(lhs, op, rhs, fmt) \
505 if (lhs op rhs) { \
506 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
507 #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\
508 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\
509 if (error == 0) \
510 error = warnerr; \
511 if (warnerr == 0) \
512 lhs = rhs; \
513 }
514 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt) \
515 if (lhs1 op1 rhs1 && lhs2 op2 rhs2) { \
516 MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s (" \
517 #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n", \
518 fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, \
519 (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2, \
520 (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2); \
521 if (error < 0) \
522 return (ENOENT); \
523 if (error == 0) \
524 error = ENOENT; \
525 }
526
527 static int
validate_sblock(struct fs * fs,int flags)528 validate_sblock(struct fs *fs, int flags)
529 {
530 uint64_t i, sectorsize;
531 uint64_t maxfilesize, sizepb;
532 int error, prtmsg, warnerr;
533 char *wmsg;
534
535 error = 0;
536 sectorsize = dbtob(1);
537 prtmsg = ((flags & UFS_NOMSG) == 0);
538 warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT;
539 wmsg = warnerr ? "" : " (Ignored)";
540 /*
541 * Check for endian mismatch between machine and filesystem.
542 */
543 if (((fs->fs_magic != FS_UFS2_MAGIC) &&
544 (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) ||
545 ((fs->fs_magic != FS_UFS1_MAGIC) &&
546 (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) {
547 MPRINT("UFS superblock failed due to endian mismatch "
548 "between machine and filesystem\n");
549 return(EILSEQ);
550 }
551 /*
552 * If just validating for recovery, then do just the minimal
553 * checks needed for the superblock fields needed to find
554 * alternate superblocks.
555 */
556 if ((flags & UFS_FSRONLY) == UFS_FSRONLY &&
557 (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) {
558 error = -1; /* fail on first error */
559 if (fs->fs_magic == FS_UFS2_MAGIC) {
560 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
561 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
562 FCHK(fs->fs_sblockloc, <, 0, %jd);
563 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
564 }
565 FCHK(fs->fs_frag, <, 1, %jd);
566 FCHK(fs->fs_frag, >, MAXFRAG, %jd);
567 FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
568 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
569 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE),
570 %jd);
571 FCHK(fs->fs_fsize, <, sectorsize, %jd);
572 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
573 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
574 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
575 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
576 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
577 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
578 FCHK(fs->fs_ncg, <, 1, %jd);
579 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
580 FCHK(fs->fs_old_cgoffset, <, 0, %jd);
581 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
582 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg,
583 %jd);
584 FCHK(fs->fs_sblkno, !=, roundup(
585 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
586 fs->fs_frag), %jd);
587 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
588 /* Only need to validate these if reading in csum data */
589 if ((flags & UFS_NOCSUM) != 0)
590 return (error);
591 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
592 (((int64_t)(1)) << 32) - INOPB(fs), %jd);
593 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
594 FCHK(fs->fs_cstotal.cs_nifree, >,
595 (uint64_t)fs->fs_ipg * fs->fs_ncg, %jd);
596 FCHK(fs->fs_cstotal.cs_ndir, >,
597 ((uint64_t)fs->fs_ipg * fs->fs_ncg) -
598 fs->fs_cstotal.cs_nifree, %jd);
599 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
600 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg,
601 %jd);
602 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
603 FCHK(fs->fs_csaddr, <, 0, %jd);
604 FCHK(fs->fs_cssize, !=,
605 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
606 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
607 fs->fs_size, %jd);
608 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)),
609 %jd);
610 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize,
611 fs->fs_fsize)), >, dtog(fs, fs->fs_csaddr), %jd);
612 return (error);
613 }
614 if (fs->fs_magic == FS_UFS2_MAGIC) {
615 if ((flags & UFS_ALTSBLK) == 0)
616 FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
617 fs->fs_sblockactualloc, !=, 0, %jd);
618 FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
619 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
620 sizeof(ufs2_daddr_t)), %jd);
621 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
622 %jd);
623 FCHK(fs->fs_inopb, !=,
624 fs->fs_bsize / sizeof(struct ufs2_dinode), %jd);
625 } else if (fs->fs_magic == FS_UFS1_MAGIC) {
626 if ((flags & UFS_ALTSBLK) == 0)
627 FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
628 FCHK(fs->fs_sblockloc, <, 0, %jd);
629 FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
630 FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
631 %jd);
632 FCHK(fs->fs_inopb, !=,
633 fs->fs_bsize / sizeof(struct ufs1_dinode), %jd);
634 FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
635 sizeof(ufs1_daddr_t)), %jd);
636 WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
637 WCHK(fs->fs_old_rotdelay, !=, 0, %jd);
638 WCHK(fs->fs_old_rps, !=, 60, %jd);
639 WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
640 WCHK(fs->fs_old_interleave, !=, 1, %jd);
641 WCHK(fs->fs_old_trackskew, !=, 0, %jd);
642 WCHK(fs->fs_old_cpc, !=, 0, %jd);
643 WCHK(fs->fs_old_postblformat, !=, 1, %jd);
644 FCHK(fs->fs_old_nrpos, !=, 1, %jd);
645 WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
646 WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
647 } else {
648 /* Bad magic number, so assume not a superblock */
649 return (ENOENT);
650 }
651 FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
652 FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
653 FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
654 FCHK(powerof2(fs->fs_bsize), ==, 0, %jd);
655 FCHK(fs->fs_frag, <, 1, %jd);
656 FCHK(fs->fs_frag, >, MAXFRAG, %jd);
657 FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
658 FCHK(fs->fs_fsize, <, sectorsize, %jd);
659 FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
660 FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
661 FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
662 FCHK(fs->fs_ncg, <, 1, %jd);
663 FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd);
664 FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
665 (((int64_t)(1)) << 32) - INOPB(fs), %jd);
666 FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
667 FCHK(fs->fs_cstotal.cs_nifree, >, (uint64_t)fs->fs_ipg * fs->fs_ncg,
668 %jd);
669 FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd);
670 FCHK(fs->fs_cstotal.cs_ndir, >,
671 ((uint64_t)fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree,
672 %jd);
673 FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
674 FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
675 /* fix for misconfigured filesystems */
676 if (fs->fs_maxbsize == 0)
677 fs->fs_maxbsize = fs->fs_bsize;
678 FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
679 FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
680 FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
681 FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
682 FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
683 FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
684 FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
685 FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
686 FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
687 FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
688 FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
689 FCHK(fs->fs_old_cgoffset, <, 0, %jd);
690 FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
691 FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd);
692 FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
693 /*
694 * If anything has failed up to this point, it is usafe to proceed
695 * as checks below may divide by zero or make other fatal calculations.
696 * So if we have any errors at this point, give up.
697 */
698 if (error)
699 return (error);
700 FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
701 FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd);
702 FCHK(fs->fs_sblkno, !=, roundup(
703 howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
704 fs->fs_frag), %jd);
705 FCHK(fs->fs_cblkno, !=, fs->fs_sblkno +
706 roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
707 FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
708 FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
709 FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
710 FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd);
711 FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd);
712 /*
713 * This test is valid, however older versions of growfs failed
714 * to correctly update fs_dsize so will fail this test. Thus we
715 * exclude it from the requirements.
716 */
717 #ifdef notdef
718 WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
719 fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
720 howmany(fs->fs_cssize, fs->fs_fsize), %jd);
721 #endif
722 WCHK(fs->fs_metaspace, <, 0, %jd);
723 WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
724 WCHK(fs->fs_minfree, >, 99, %jd%%);
725 maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
726 for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
727 sizepb *= NINDIR(fs);
728 maxfilesize += sizepb;
729 }
730 WCHK(fs->fs_maxfilesize, >, maxfilesize, %jd);
731 /*
732 * These values have a tight interaction with each other that
733 * makes it hard to tightly bound them. So we can only check
734 * that they are within a broader possible range.
735 *
736 * The size cannot always be accurately determined, but ensure
737 * that it is consistent with the number of cylinder groups (fs_ncg)
738 * and the number of fragments per cylinder group (fs_fpg). Ensure
739 * that the summary information size is correct and that it starts
740 * and ends in the data area of the same cylinder group.
741 */
742 FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
743 FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, %jd);
744 FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
745 /*
746 * If we are not requested to read in the csum data stop here
747 * as the correctness of the remaining values is only important
748 * to bound the space needed to be allocated to hold the csum data.
749 */
750 if ((flags & UFS_NOCSUM) != 0)
751 return (error);
752 FCHK(fs->fs_csaddr, <, 0, %jd);
753 FCHK(fs->fs_cssize, !=,
754 fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
755 FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
756 fs->fs_size, %jd);
757 FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd);
758 FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
759 dtog(fs, fs->fs_csaddr), %jd);
760 /*
761 * With file system clustering it is possible to allocate
762 * many contiguous blocks. The kernel variable maxphys defines
763 * the maximum transfer size permitted by the controller and/or
764 * buffering. The fs_maxcontig parameter controls the maximum
765 * number of blocks that the filesystem will read or write
766 * in a single transfer. It is calculated when the filesystem
767 * is created as maxphys / fs_bsize. The loader uses a maxphys
768 * of 128K even when running on a system that supports larger
769 * values. If the filesystem was built on a system that supports
770 * a larger maxphys (1M is typical) it will have configured
771 * fs_maxcontig for that larger system. So we bound the upper
772 * allowable limit for fs_maxconfig to be able to at least
773 * work with a 1M maxphys on the smallest block size filesystem:
774 * 1M / 4096 == 256. There is no harm in allowing the mounting of
775 * filesystems that make larger than maxphys I/O requests because
776 * those (mostly 32-bit machines) can (very slowly) handle I/O
777 * requests that exceed maxphys.
778 */
779 WCHK(fs->fs_maxcontig, <, 0, %jd);
780 WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
781 FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
782 FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
783 MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
784 return (error);
785 }
786
787 /*
788 * Make an extensive search to find a superblock. If the superblock
789 * in the standard place cannot be used, try looking for one of the
790 * backup superblocks.
791 *
792 * Flags are made up of the following or'ed together options:
793 *
794 * UFS_NOMSG indicates that superblock inconsistency error messages
795 * should not be printed.
796 *
797 * UFS_NOCSUM causes only the superblock itself to be returned, but does
798 * not read in any auxillary data structures like the cylinder group
799 * summary information.
800 */
801 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))802 ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags,
803 struct malloc_type *filltype,
804 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
805 {
806 struct fsrecovery *fsr;
807 struct fs *protofs;
808 void *fsrbuf;
809 char *cp;
810 long nocsum, flags, msg, cg;
811 off_t sblk, secsize;
812 int error;
813
814 msg = (reqflags & UFS_NOMSG) == 0;
815 nocsum = reqflags & UFS_NOCSUM;
816 /*
817 * Try normal superblock read and return it if it works.
818 *
819 * Suppress messages if it fails until we find out if
820 * failure can be avoided.
821 */
822 flags = UFS_NOMSG | nocsum;
823 error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc);
824 /*
825 * If successful or endian error, no need to try further.
826 */
827 if (error == 0 || error == EILSEQ) {
828 if (msg && error == EILSEQ)
829 printf("UFS superblock failed due to endian mismatch "
830 "between machine and filesystem\n");
831 return (error);
832 }
833 /*
834 * First try: ignoring hash failures.
835 */
836 flags |= UFS_NOHASHFAIL;
837 if (msg)
838 flags &= ~UFS_NOMSG;
839 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0)
840 return (0);
841 /*
842 * Next up is to check if fields of the superblock that are
843 * needed to find backup superblocks are usable.
844 */
845 if (msg)
846 printf("Attempted recovery for standard superblock: failed\n");
847 flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG;
848 if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype,
849 readfunc) == 0) {
850 if (msg)
851 printf("Attempt extraction of recovery data from "
852 "standard superblock.\n");
853 } else {
854 /*
855 * Final desperation is to see if alternate superblock
856 * parameters have been saved in the boot area.
857 */
858 if (msg)
859 printf("Attempted extraction of recovery data from "
860 "standard superblock: failed\nAttempt to find "
861 "boot zone recovery data.\n");
862 /*
863 * Look to see if recovery information has been saved.
864 * If so we can generate a prototype superblock based
865 * on that information.
866 *
867 * We need fragments-per-group, number of cylinder groups,
868 * location of the superblock within the cylinder group, and
869 * the conversion from filesystem fragments to disk blocks.
870 *
871 * When building a UFS2 filesystem, newfs(8) stores these
872 * details at the end of the boot block area at the start
873 * of the filesystem partition. If they have been overwritten
874 * by a boot block, we fail. But usually they are there
875 * and we can use them.
876 *
877 * We could ask the underlying device for its sector size,
878 * but some devices lie. So we just try a plausible range.
879 */
880 error = ENOENT;
881 fsrbuf = NULL;
882 for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2)
883 if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize),
884 &fsrbuf, secsize)) == 0)
885 break;
886 if (error != 0)
887 goto trynowarn;
888 cp = fsrbuf; /* type change to keep compiler happy */
889 fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr];
890 if (fsr->fsr_magic != FS_UFS2_MAGIC ||
891 (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT))
892 == NULL) {
893 UFS_FREE(fsrbuf, filltype);
894 goto trynowarn;
895 }
896 memset(protofs, 0, sizeof(struct fs));
897 protofs->fs_fpg = fsr->fsr_fpg;
898 protofs->fs_fsbtodb = fsr->fsr_fsbtodb;
899 protofs->fs_sblkno = fsr->fsr_sblkno;
900 protofs->fs_magic = fsr->fsr_magic;
901 protofs->fs_ncg = fsr->fsr_ncg;
902 UFS_FREE(fsrbuf, filltype);
903 }
904 /*
905 * Scan looking for alternative superblocks.
906 */
907 flags = nocsum;
908 if (!msg)
909 flags |= UFS_NOMSG;
910 for (cg = 0; cg < protofs->fs_ncg; cg++) {
911 sblk = fsbtodb(protofs, cgsblock(protofs, cg));
912 if (msg)
913 printf("Try cg %ld at sblock loc %jd\n", cg,
914 (intmax_t)sblk);
915 if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype,
916 readfunc) == 0) {
917 if (msg)
918 printf("Succeeded with alternate superblock "
919 "at %jd\n", (intmax_t)sblk);
920 UFS_FREE(protofs, filltype);
921 return (0);
922 }
923 }
924 UFS_FREE(protofs, filltype);
925 /*
926 * Our alternate superblock strategies failed. Our last ditch effort
927 * is to see if the standard superblock has only non-critical errors.
928 */
929 trynowarn:
930 flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum;
931 if (msg) {
932 printf("Finding an alternate superblock failed.\nCheck for "
933 "only non-critical errors in standard superblock\n");
934 flags &= ~UFS_NOMSG;
935 }
936 if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) {
937 if (msg)
938 printf("Failed, superblock has critical errors\n");
939 return (ENOENT);
940 }
941 if (msg)
942 printf("Success, using standard superblock with "
943 "non-critical errors.\n");
944 return (0);
945 }
946
947 /*
948 * Write a superblock to the devfd device from the memory pointed to by fs.
949 * Write out the superblock summary information if it is present.
950 *
951 * If the write is successful, zero is returned. Otherwise one of the
952 * following error values is returned:
953 * EIO: failed to write superblock.
954 * EIO: failed to write superblock summary information.
955 */
956 int
ffs_sbput(void * devfd,struct fs * fs,off_t loc,int (* writefunc)(void * devfd,off_t loc,void * buf,int size))957 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
958 int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
959 {
960 struct fs_summary_info *fs_si;
961 int i, error, blks, size;
962 uint8_t *space;
963
964 /*
965 * If there is summary information, write it first, so if there
966 * is an error, the superblock will not be marked as clean.
967 */
968 if (fs->fs_si != NULL && fs->fs_csp != NULL) {
969 blks = howmany(fs->fs_cssize, fs->fs_fsize);
970 space = (uint8_t *)fs->fs_csp;
971 for (i = 0; i < blks; i += fs->fs_frag) {
972 size = fs->fs_bsize;
973 if (i + fs->fs_frag > blks)
974 size = (blks - i) * fs->fs_fsize;
975 if ((error = (*writefunc)(devfd,
976 dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
977 space, size)) != 0)
978 return (error);
979 space += size;
980 }
981 }
982 fs->fs_fmod = 0;
983 ffs_oldfscompat_write(fs);
984 #ifdef _KERNEL
985 fs->fs_time = time_second;
986 #else /* User Code */
987 fs->fs_time = time(NULL);
988 #endif
989 /* Clear the pointers for the duration of writing. */
990 fs_si = fs->fs_si;
991 fs->fs_si = NULL;
992 fs->fs_ckhash = ffs_calc_sbhash(fs);
993 error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
994 /*
995 * A negative error code is returned when a copy of the
996 * superblock has been made which is discarded when the I/O
997 * is done. So the fs_si field does not and indeed cannot be
998 * restored after the write is done. Convert the error code
999 * back to its usual positive value when returning it.
1000 */
1001 if (error < 0)
1002 return (-error - 1);
1003 fs->fs_si = fs_si;
1004 return (error);
1005 }
1006
1007 /*
1008 * Calculate the check-hash for a superblock.
1009 */
1010 uint32_t
ffs_calc_sbhash(struct fs * fs)1011 ffs_calc_sbhash(struct fs *fs)
1012 {
1013 uint32_t ckhash, save_ckhash;
1014
1015 /*
1016 * A filesystem that was using a superblock ckhash may be moved
1017 * to an older kernel that does not support ckhashes. The
1018 * older kernel will clear the FS_METACKHASH flag indicating
1019 * that it does not update hashes. When the disk is moved back
1020 * to a kernel capable of ckhashes it disables them on mount:
1021 *
1022 * if ((fs->fs_flags & FS_METACKHASH) == 0)
1023 * fs->fs_metackhash = 0;
1024 *
1025 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
1026 * old stale value in the fs->fs_ckhash field. Thus the need to
1027 * just accept what is there.
1028 */
1029 if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
1030 return (fs->fs_ckhash);
1031
1032 save_ckhash = fs->fs_ckhash;
1033 fs->fs_ckhash = 0;
1034 /*
1035 * If newly read from disk, the caller is responsible for
1036 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
1037 */
1038 ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
1039 fs->fs_ckhash = save_ckhash;
1040 return (ckhash);
1041 }
1042
1043 /*
1044 * Update the frsum fields to reflect addition or deletion
1045 * of some frags.
1046 */
1047 void
ffs_fragacct(struct fs * fs,int fragmap,int32_t fraglist[],int cnt)1048 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
1049 {
1050 int inblk;
1051 int field, subfield;
1052 int siz, pos;
1053
1054 inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
1055 fragmap <<= 1;
1056 for (siz = 1; siz < fs->fs_frag; siz++) {
1057 if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
1058 continue;
1059 field = around[siz];
1060 subfield = inside[siz];
1061 for (pos = siz; pos <= fs->fs_frag; pos++) {
1062 if ((fragmap & field) == subfield) {
1063 fraglist[siz] += cnt;
1064 pos += siz;
1065 field <<= siz;
1066 subfield <<= siz;
1067 }
1068 field <<= 1;
1069 subfield <<= 1;
1070 }
1071 }
1072 }
1073
1074 /*
1075 * block operations
1076 *
1077 * check if a block is available
1078 */
1079 int
ffs_isblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)1080 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
1081 {
1082 unsigned char mask;
1083
1084 switch ((int)fs->fs_frag) {
1085 case 8:
1086 return (cp[h] == 0xff);
1087 case 4:
1088 mask = 0x0f << ((h & 0x1) << 2);
1089 return ((cp[h >> 1] & mask) == mask);
1090 case 2:
1091 mask = 0x03 << ((h & 0x3) << 1);
1092 return ((cp[h >> 2] & mask) == mask);
1093 case 1:
1094 mask = 0x01 << (h & 0x7);
1095 return ((cp[h >> 3] & mask) == mask);
1096 default:
1097 #ifdef _KERNEL
1098 panic("ffs_isblock");
1099 #endif
1100 break;
1101 }
1102 return (0);
1103 }
1104
1105 /*
1106 * check if a block is free
1107 */
1108 int
ffs_isfreeblock(struct fs * fs,uint8_t * cp,ufs1_daddr_t h)1109 ffs_isfreeblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
1110 {
1111
1112 switch ((int)fs->fs_frag) {
1113 case 8:
1114 return (cp[h] == 0);
1115 case 4:
1116 return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
1117 case 2:
1118 return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
1119 case 1:
1120 return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
1121 default:
1122 #ifdef _KERNEL
1123 panic("ffs_isfreeblock");
1124 #endif
1125 break;
1126 }
1127 return (0);
1128 }
1129
1130 /*
1131 * take a block out of the map
1132 */
1133 void
ffs_clrblock(struct fs * fs,uint8_t * cp,ufs1_daddr_t h)1134 ffs_clrblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
1135 {
1136
1137 switch ((int)fs->fs_frag) {
1138 case 8:
1139 cp[h] = 0;
1140 return;
1141 case 4:
1142 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
1143 return;
1144 case 2:
1145 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
1146 return;
1147 case 1:
1148 cp[h >> 3] &= ~(0x01 << (h & 0x7));
1149 return;
1150 default:
1151 #ifdef _KERNEL
1152 panic("ffs_clrblock");
1153 #endif
1154 break;
1155 }
1156 }
1157
1158 /*
1159 * put a block into the map
1160 */
1161 void
ffs_setblock(struct fs * fs,unsigned char * cp,ufs1_daddr_t h)1162 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
1163 {
1164
1165 switch ((int)fs->fs_frag) {
1166 case 8:
1167 cp[h] = 0xff;
1168 return;
1169 case 4:
1170 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1171 return;
1172 case 2:
1173 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1174 return;
1175 case 1:
1176 cp[h >> 3] |= (0x01 << (h & 0x7));
1177 return;
1178 default:
1179 #ifdef _KERNEL
1180 panic("ffs_setblock");
1181 #endif
1182 break;
1183 }
1184 }
1185
1186 /*
1187 * Update the cluster map because of an allocation or free.
1188 *
1189 * Cnt == 1 means free; cnt == -1 means allocating.
1190 */
1191 void
ffs_clusteracct(struct fs * fs,struct cg * cgp,ufs1_daddr_t blkno,int cnt)1192 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
1193 {
1194 int32_t *sump;
1195 int32_t *lp;
1196 uint8_t *freemapp, *mapp;
1197 int i, start, end, forw, back, map;
1198 uint64_t bit;
1199
1200 if (fs->fs_contigsumsize <= 0)
1201 return;
1202 freemapp = cg_clustersfree(cgp);
1203 sump = cg_clustersum(cgp);
1204 /*
1205 * Allocate or clear the actual block.
1206 */
1207 if (cnt > 0)
1208 setbit(freemapp, blkno);
1209 else
1210 clrbit(freemapp, blkno);
1211 /*
1212 * Find the size of the cluster going forward.
1213 */
1214 start = blkno + 1;
1215 end = start + fs->fs_contigsumsize;
1216 if (end >= cgp->cg_nclusterblks)
1217 end = cgp->cg_nclusterblks;
1218 mapp = &freemapp[start / NBBY];
1219 map = *mapp++;
1220 bit = 1U << (start % NBBY);
1221 for (i = start; i < end; i++) {
1222 if ((map & bit) == 0)
1223 break;
1224 if ((i & (NBBY - 1)) != (NBBY - 1)) {
1225 bit <<= 1;
1226 } else {
1227 map = *mapp++;
1228 bit = 1;
1229 }
1230 }
1231 forw = i - start;
1232 /*
1233 * Find the size of the cluster going backward.
1234 */
1235 start = blkno - 1;
1236 end = start - fs->fs_contigsumsize;
1237 if (end < 0)
1238 end = -1;
1239 mapp = &freemapp[start / NBBY];
1240 map = *mapp--;
1241 bit = 1U << (start % NBBY);
1242 for (i = start; i > end; i--) {
1243 if ((map & bit) == 0)
1244 break;
1245 if ((i & (NBBY - 1)) != 0) {
1246 bit >>= 1;
1247 } else {
1248 map = *mapp--;
1249 bit = 1U << (NBBY - 1);
1250 }
1251 }
1252 back = start - i;
1253 /*
1254 * Account for old cluster and the possibly new forward and
1255 * back clusters.
1256 */
1257 i = back + forw + 1;
1258 if (i > fs->fs_contigsumsize)
1259 i = fs->fs_contigsumsize;
1260 sump[i] += cnt;
1261 if (back > 0)
1262 sump[back] -= cnt;
1263 if (forw > 0)
1264 sump[forw] -= cnt;
1265 /*
1266 * Update cluster summary information.
1267 */
1268 lp = &sump[fs->fs_contigsumsize];
1269 for (i = fs->fs_contigsumsize; i > 0; i--)
1270 if (*lp-- > 0)
1271 break;
1272 fs->fs_maxcluster[cgp->cg_cgx] = i;
1273 }
1274