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