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