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