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