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