xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 32100375a661c1e16588ddfa7b90ca8d26cb9786)
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 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
49 uint32_t ffs_calc_sbhash(struct fs *);
50 struct malloc_type;
51 #define UFS_MALLOC(size, type, flags) malloc(size)
52 #define UFS_FREE(ptr, type) free(ptr)
53 /*
54  * Request standard superblock location in ffs_sbget
55  */
56 #define	STDSB			-1	/* Fail if check-hash is bad */
57 #define	STDSB_NOHASHFAIL	-2	/* Ignore check-hash failure */
58 
59 #else /* _KERNEL */
60 #include <sys/systm.h>
61 #include <sys/gsb_crc32.h>
62 #include <sys/lock.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
65 #include <sys/vnode.h>
66 #include <sys/bio.h>
67 #include <sys/buf.h>
68 #include <sys/ucred.h>
69 
70 #include <ufs/ufs/quota.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/extattr.h>
73 #include <ufs/ufs/ufsmount.h>
74 #include <ufs/ufs/ufs_extern.h>
75 #include <ufs/ffs/ffs_extern.h>
76 #include <ufs/ffs/fs.h>
77 
78 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
79 #define UFS_FREE(ptr, type) free(ptr, type)
80 
81 #endif /* _KERNEL */
82 
83 /*
84  * Verify an inode check-hash.
85  */
86 int
87 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
88 {
89 	uint32_t ckhash, save_ckhash;
90 
91 	/*
92 	 * Return success if unallocated or we are not doing inode check-hash.
93 	 */
94 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
95 		return (0);
96 	/*
97 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
98 	 * a check-hash value of zero when calculating the check-hash.
99 	 */
100 	save_ckhash = dip->di_ckhash;
101 	dip->di_ckhash = 0;
102 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
103 	dip->di_ckhash = save_ckhash;
104 	if (save_ckhash == ckhash)
105 		return (0);
106 	return (EINVAL);
107 }
108 
109 /*
110  * Update an inode check-hash.
111  */
112 void
113 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
114 {
115 
116 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
117 		return;
118 	/*
119 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
120 	 * a check-hash value of zero when calculating the new check-hash.
121 	 */
122 	dip->di_ckhash = 0;
123 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
124 }
125 
126 /*
127  * These are the low-level functions that actually read and write
128  * the superblock and its associated data.
129  */
130 static off_t sblock_try[] = SBLOCKSEARCH;
131 static int readsuper(void *, struct fs **, off_t, int, int,
132 	int (*)(void *, off_t, void **, 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  *     ENOSPC: failed to allocate space for the superblock.
150  *     EINVAL: The previous newfs operation on this volume did not complete.
151  *         The administrator must complete newfs before using this volume.
152  */
153 int
154 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
155     struct malloc_type *filltype,
156     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
157 {
158 	struct fs *fs;
159 	struct fs_summary_info *fs_si;
160 	int i, error, size, blks;
161 	uint8_t *space;
162 	int32_t *lp;
163 	int chkhash;
164 	char *buf;
165 
166 	fs = NULL;
167 	*fsp = NULL;
168 	chkhash = 1;
169 	if (altsblock >= 0) {
170 		if ((error = readsuper(devfd, &fs, altsblock, 1, chkhash,
171 		     readfunc)) != 0) {
172 			if (fs != NULL)
173 				UFS_FREE(fs, filltype);
174 			return (error);
175 		}
176 	} else {
177 		if (altsblock == STDSB_NOHASHFAIL)
178 			chkhash = 0;
179 		for (i = 0; sblock_try[i] != -1; i++) {
180 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
181 			     chkhash, readfunc)) == 0)
182 				break;
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(u_int8_t);
202 	/* When running in libufs or libsa, UFS_MALLOC may fail */
203 	if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_WAITOK)) == NULL) {
204 		UFS_FREE(fs, filltype);
205 		return (ENOSPC);
206 	}
207 	bzero(fs_si, sizeof(*fs_si));
208 	fs->fs_si = fs_si;
209 	if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
210 		UFS_FREE(fs->fs_si, filltype);
211 		UFS_FREE(fs, filltype);
212 		return (ENOSPC);
213 	}
214 	fs->fs_csp = (struct csum *)space;
215 	for (i = 0; i < blks; i += fs->fs_frag) {
216 		size = fs->fs_bsize;
217 		if (i + fs->fs_frag > blks)
218 			size = (blks - i) * fs->fs_fsize;
219 		buf = NULL;
220 		error = (*readfunc)(devfd,
221 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
222 		if (error) {
223 			if (buf != NULL)
224 				UFS_FREE(buf, filltype);
225 			UFS_FREE(fs->fs_csp, filltype);
226 			UFS_FREE(fs->fs_si, filltype);
227 			UFS_FREE(fs, filltype);
228 			return (error);
229 		}
230 		memcpy(space, buf, size);
231 		UFS_FREE(buf, filltype);
232 		space += size;
233 	}
234 	if (fs->fs_contigsumsize > 0) {
235 		fs->fs_maxcluster = lp = (int32_t *)space;
236 		for (i = 0; i < fs->fs_ncg; i++)
237 			*lp++ = fs->fs_contigsumsize;
238 		space = (uint8_t *)lp;
239 	}
240 	size = fs->fs_ncg * sizeof(u_int8_t);
241 	fs->fs_contigdirs = (u_int8_t *)space;
242 	bzero(fs->fs_contigdirs, size);
243 	*fsp = fs;
244 	return (0);
245 }
246 
247 /*
248  * Try to read a superblock from the location specified by sblockloc.
249  * Return zero on success or an errno on failure.
250  */
251 static int
252 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
253     int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
254 {
255 	struct fs *fs;
256 	int error, res;
257 	uint32_t ckhash;
258 
259 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
260 	if (error != 0)
261 		return (error);
262 	fs = *fsp;
263 	if (fs->fs_magic == FS_BAD_MAGIC)
264 		return (EINVAL);
265 	if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
266 	      sblockloc <= SBLOCK_UFS1)) ||
267 	     (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
268 	      sblockloc == fs->fs_sblockloc))) &&
269 	    fs->fs_ncg >= 1 &&
270 	    fs->fs_bsize >= MINBSIZE &&
271 	    fs->fs_bsize <= MAXBSIZE &&
272 	    fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) &&
273 	    fs->fs_sbsize <= SBLOCKSIZE) {
274 		/*
275 		 * If the filesystem has been run on a kernel without
276 		 * metadata check hashes, disable them.
277 		 */
278 		if ((fs->fs_flags & FS_METACKHASH) == 0)
279 			fs->fs_metackhash = 0;
280 		if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
281 #ifdef _KERNEL
282 			res = uprintf("Superblock check-hash failed: recorded "
283 			    "check-hash 0x%x != computed check-hash 0x%x%s\n",
284 			    fs->fs_ckhash, ckhash,
285 			    chkhash == 0 ? " (Ignored)" : "");
286 #else
287 			res = 0;
288 #endif
289 			/*
290 			 * Print check-hash failure if no controlling terminal
291 			 * in kernel or always if in user-mode (libufs).
292 			 */
293 			if (res == 0)
294 				printf("Superblock check-hash failed: recorded "
295 				    "check-hash 0x%x != computed check-hash "
296 				    "0x%x%s\n", fs->fs_ckhash, ckhash,
297 				    chkhash == 0 ? " (Ignored)" : "");
298 			if (chkhash == 0) {
299 				fs->fs_flags |= FS_NEEDSFSCK;
300 				fs->fs_fmod = 1;
301 				return (0);
302 			}
303 			fs->fs_fmod = 0;
304 			return (EINTEGRITY);
305 		}
306 		/* Have to set for old filesystems that predate this field */
307 		fs->fs_sblockactualloc = sblockloc;
308 		/* Not yet any summary information */
309 		fs->fs_si = NULL;
310 		return (0);
311 	}
312 	return (ENOENT);
313 }
314 
315 /*
316  * Write a superblock to the devfd device from the memory pointed to by fs.
317  * Write out the superblock summary information if it is present.
318  *
319  * If the write is successful, zero is returned. Otherwise one of the
320  * following error values is returned:
321  *     EIO: failed to write superblock.
322  *     EIO: failed to write superblock summary information.
323  */
324 int
325 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
326     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
327 {
328 	int i, error, blks, size;
329 	uint8_t *space;
330 
331 	/*
332 	 * If there is summary information, write it first, so if there
333 	 * is an error, the superblock will not be marked as clean.
334 	 */
335 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
336 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
337 		space = (uint8_t *)fs->fs_csp;
338 		for (i = 0; i < blks; i += fs->fs_frag) {
339 			size = fs->fs_bsize;
340 			if (i + fs->fs_frag > blks)
341 				size = (blks - i) * fs->fs_fsize;
342 			if ((error = (*writefunc)(devfd,
343 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
344 			     space, size)) != 0)
345 				return (error);
346 			space += size;
347 		}
348 	}
349 	fs->fs_fmod = 0;
350 #ifndef _KERNEL
351 	{
352 		struct fs_summary_info *fs_si;
353 
354 		fs->fs_time = time(NULL);
355 		/* Clear the pointers for the duration of writing. */
356 		fs_si = fs->fs_si;
357 		fs->fs_si = NULL;
358 		fs->fs_ckhash = ffs_calc_sbhash(fs);
359 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
360 		fs->fs_si = fs_si;
361 	}
362 #else /* _KERNEL */
363 	fs->fs_time = time_second;
364 	fs->fs_ckhash = ffs_calc_sbhash(fs);
365 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
366 #endif /* _KERNEL */
367 	return (error);
368 }
369 
370 /*
371  * Calculate the check-hash for a superblock.
372  */
373 uint32_t
374 ffs_calc_sbhash(struct fs *fs)
375 {
376 	uint32_t ckhash, save_ckhash;
377 
378 	/*
379 	 * A filesystem that was using a superblock ckhash may be moved
380 	 * to an older kernel that does not support ckhashes. The
381 	 * older kernel will clear the FS_METACKHASH flag indicating
382 	 * that it does not update hashes. When the disk is moved back
383 	 * to a kernel capable of ckhashes it disables them on mount:
384 	 *
385 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
386 	 *		fs->fs_metackhash = 0;
387 	 *
388 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
389 	 * old stale value in the fs->fs_ckhash field. Thus the need to
390 	 * just accept what is there.
391 	 */
392 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
393 		return (fs->fs_ckhash);
394 
395 	save_ckhash = fs->fs_ckhash;
396 	fs->fs_ckhash = 0;
397 	/*
398 	 * If newly read from disk, the caller is responsible for
399 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
400 	 */
401 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
402 	fs->fs_ckhash = save_ckhash;
403 	return (ckhash);
404 }
405 
406 /*
407  * Update the frsum fields to reflect addition or deletion
408  * of some frags.
409  */
410 void
411 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
412 {
413 	int inblk;
414 	int field, subfield;
415 	int siz, pos;
416 
417 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
418 	fragmap <<= 1;
419 	for (siz = 1; siz < fs->fs_frag; siz++) {
420 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
421 			continue;
422 		field = around[siz];
423 		subfield = inside[siz];
424 		for (pos = siz; pos <= fs->fs_frag; pos++) {
425 			if ((fragmap & field) == subfield) {
426 				fraglist[siz] += cnt;
427 				pos += siz;
428 				field <<= siz;
429 				subfield <<= siz;
430 			}
431 			field <<= 1;
432 			subfield <<= 1;
433 		}
434 	}
435 }
436 
437 /*
438  * block operations
439  *
440  * check if a block is available
441  */
442 int
443 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
444 {
445 	unsigned char mask;
446 
447 	switch ((int)fs->fs_frag) {
448 	case 8:
449 		return (cp[h] == 0xff);
450 	case 4:
451 		mask = 0x0f << ((h & 0x1) << 2);
452 		return ((cp[h >> 1] & mask) == mask);
453 	case 2:
454 		mask = 0x03 << ((h & 0x3) << 1);
455 		return ((cp[h >> 2] & mask) == mask);
456 	case 1:
457 		mask = 0x01 << (h & 0x7);
458 		return ((cp[h >> 3] & mask) == mask);
459 	default:
460 #ifdef _KERNEL
461 		panic("ffs_isblock");
462 #endif
463 		break;
464 	}
465 	return (0);
466 }
467 
468 /*
469  * check if a block is free
470  */
471 int
472 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
473 {
474 
475 	switch ((int)fs->fs_frag) {
476 	case 8:
477 		return (cp[h] == 0);
478 	case 4:
479 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
480 	case 2:
481 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
482 	case 1:
483 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
484 	default:
485 #ifdef _KERNEL
486 		panic("ffs_isfreeblock");
487 #endif
488 		break;
489 	}
490 	return (0);
491 }
492 
493 /*
494  * take a block out of the map
495  */
496 void
497 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
498 {
499 
500 	switch ((int)fs->fs_frag) {
501 	case 8:
502 		cp[h] = 0;
503 		return;
504 	case 4:
505 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
506 		return;
507 	case 2:
508 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
509 		return;
510 	case 1:
511 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
512 		return;
513 	default:
514 #ifdef _KERNEL
515 		panic("ffs_clrblock");
516 #endif
517 		break;
518 	}
519 }
520 
521 /*
522  * put a block into the map
523  */
524 void
525 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
526 {
527 
528 	switch ((int)fs->fs_frag) {
529 
530 	case 8:
531 		cp[h] = 0xff;
532 		return;
533 	case 4:
534 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
535 		return;
536 	case 2:
537 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
538 		return;
539 	case 1:
540 		cp[h >> 3] |= (0x01 << (h & 0x7));
541 		return;
542 	default:
543 #ifdef _KERNEL
544 		panic("ffs_setblock");
545 #endif
546 		break;
547 	}
548 }
549 
550 /*
551  * Update the cluster map because of an allocation or free.
552  *
553  * Cnt == 1 means free; cnt == -1 means allocating.
554  */
555 void
556 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
557 {
558 	int32_t *sump;
559 	int32_t *lp;
560 	u_char *freemapp, *mapp;
561 	int i, start, end, forw, back, map;
562 	u_int bit;
563 
564 	if (fs->fs_contigsumsize <= 0)
565 		return;
566 	freemapp = cg_clustersfree(cgp);
567 	sump = cg_clustersum(cgp);
568 	/*
569 	 * Allocate or clear the actual block.
570 	 */
571 	if (cnt > 0)
572 		setbit(freemapp, blkno);
573 	else
574 		clrbit(freemapp, blkno);
575 	/*
576 	 * Find the size of the cluster going forward.
577 	 */
578 	start = blkno + 1;
579 	end = start + fs->fs_contigsumsize;
580 	if (end >= cgp->cg_nclusterblks)
581 		end = cgp->cg_nclusterblks;
582 	mapp = &freemapp[start / NBBY];
583 	map = *mapp++;
584 	bit = 1U << (start % NBBY);
585 	for (i = start; i < end; i++) {
586 		if ((map & bit) == 0)
587 			break;
588 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
589 			bit <<= 1;
590 		} else {
591 			map = *mapp++;
592 			bit = 1;
593 		}
594 	}
595 	forw = i - start;
596 	/*
597 	 * Find the size of the cluster going backward.
598 	 */
599 	start = blkno - 1;
600 	end = start - fs->fs_contigsumsize;
601 	if (end < 0)
602 		end = -1;
603 	mapp = &freemapp[start / NBBY];
604 	map = *mapp--;
605 	bit = 1U << (start % NBBY);
606 	for (i = start; i > end; i--) {
607 		if ((map & bit) == 0)
608 			break;
609 		if ((i & (NBBY - 1)) != 0) {
610 			bit >>= 1;
611 		} else {
612 			map = *mapp--;
613 			bit = 1U << (NBBY - 1);
614 		}
615 	}
616 	back = start - i;
617 	/*
618 	 * Account for old cluster and the possibly new forward and
619 	 * back clusters.
620 	 */
621 	i = back + forw + 1;
622 	if (i > fs->fs_contigsumsize)
623 		i = fs->fs_contigsumsize;
624 	sump[i] += cnt;
625 	if (back > 0)
626 		sump[back] -= cnt;
627 	if (forw > 0)
628 		sump[forw] -= cnt;
629 	/*
630 	 * Update cluster summary information.
631 	 */
632 	lp = &sump[fs->fs_contigsumsize];
633 	for (i = fs->fs_contigsumsize; i > 0; i--)
634 		if (*lp-- > 0)
635 			break;
636 	fs->fs_maxcluster[cgp->cg_cgx] = i;
637 }
638