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
48uint32_t calculate_crc32c(uint32_t, const void *, size_t);
49uint32_t ffs_calc_sbhash(struct fs *);
50struct 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 */
86int
87ffs_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 */
112void
113ffs_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 */
130static off_t sblock_try[] = SBLOCKSEARCH;
131static 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 */
153int
154ffs_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	if (altsblock >= 0) {
169		if ((error = readsuper(devfd, &fs, altsblock, 1, 0,
170		     readfunc)) != 0) {
171			if (fs != NULL)
172				UFS_FREE(fs, filltype);
173			return (error);
174		}
175	} else {
176		chkhash = 1;
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 */
251static int
252readsuper(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		/*
281		 * Clear any check-hashes that are not maintained
282		 * by this kernel. Also clear any unsupported flags.
283		 */
284		fs->fs_metackhash &= CK_SUPPORTED;
285		fs->fs_flags &= FS_SUPPORTED;
286		if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
287#ifdef _KERNEL
288			res = uprintf("Superblock check-hash failed: recorded "
289			    "check-hash 0x%x != computed check-hash 0x%x%s\n",
290			    fs->fs_ckhash, ckhash,
291			    chkhash == 0 ? " (Ignored)" : "");
292#else
293			res = 0;
294#endif
295			/*
296			 * Print check-hash failure if no controlling terminal
297			 * in kernel or always if in user-mode (libufs).
298			 */
299			if (res == 0)
300				printf("Superblock check-hash failed: recorded "
301				    "check-hash 0x%x != computed check-hash "
302				    "0x%x%s\n", fs->fs_ckhash, ckhash,
303				    chkhash == 0 ? " (Ignored)" : "");
304			if (chkhash == 0) {
305				fs->fs_flags |= FS_NEEDSFSCK;
306				fs->fs_fmod = 1;
307				return (0);
308			}
309			fs->fs_fmod = 0;
310			return (EINTEGRITY);
311		}
312		/* Have to set for old filesystems that predate this field */
313		fs->fs_sblockactualloc = sblockloc;
314		/* Not yet any summary information */
315		fs->fs_si = NULL;
316		return (0);
317	}
318	return (ENOENT);
319}
320
321/*
322 * Write a superblock to the devfd device from the memory pointed to by fs.
323 * Write out the superblock summary information if it is present.
324 *
325 * If the write is successful, zero is returned. Otherwise one of the
326 * following error values is returned:
327 *     EIO: failed to write superblock.
328 *     EIO: failed to write superblock summary information.
329 */
330int
331ffs_sbput(void *devfd, struct fs *fs, off_t loc,
332    int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
333{
334	int i, error, blks, size;
335	uint8_t *space;
336
337	/*
338	 * If there is summary information, write it first, so if there
339	 * is an error, the superblock will not be marked as clean.
340	 */
341	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
342		blks = howmany(fs->fs_cssize, fs->fs_fsize);
343		space = (uint8_t *)fs->fs_csp;
344		for (i = 0; i < blks; i += fs->fs_frag) {
345			size = fs->fs_bsize;
346			if (i + fs->fs_frag > blks)
347				size = (blks - i) * fs->fs_fsize;
348			if ((error = (*writefunc)(devfd,
349			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
350			     space, size)) != 0)
351				return (error);
352			space += size;
353		}
354	}
355	fs->fs_fmod = 0;
356#ifndef _KERNEL
357	{
358		struct fs_summary_info *fs_si;
359
360		fs->fs_time = time(NULL);
361		/* Clear the pointers for the duration of writing. */
362		fs_si = fs->fs_si;
363		fs->fs_si = NULL;
364		fs->fs_ckhash = ffs_calc_sbhash(fs);
365		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
366		fs->fs_si = fs_si;
367	}
368#else /* _KERNEL */
369	fs->fs_time = time_second;
370	fs->fs_ckhash = ffs_calc_sbhash(fs);
371	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
372#endif /* _KERNEL */
373	return (error);
374}
375
376/*
377 * Calculate the check-hash for a superblock.
378 */
379uint32_t
380ffs_calc_sbhash(struct fs *fs)
381{
382	uint32_t ckhash, save_ckhash;
383
384	/*
385	 * A filesystem that was using a superblock ckhash may be moved
386	 * to an older kernel that does not support ckhashes. The
387	 * older kernel will clear the FS_METACKHASH flag indicating
388	 * that it does not update hashes. When the disk is moved back
389	 * to a kernel capable of ckhashes it disables them on mount:
390	 *
391	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
392	 *		fs->fs_metackhash = 0;
393	 *
394	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
395	 * old stale value in the fs->fs_ckhash field. Thus the need to
396	 * just accept what is there.
397	 */
398	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
399		return (fs->fs_ckhash);
400
401	save_ckhash = fs->fs_ckhash;
402	fs->fs_ckhash = 0;
403	/*
404	 * If newly read from disk, the caller is responsible for
405	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
406	 */
407	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
408	fs->fs_ckhash = save_ckhash;
409	return (ckhash);
410}
411
412/*
413 * Update the frsum fields to reflect addition or deletion
414 * of some frags.
415 */
416void
417ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
418{
419	int inblk;
420	int field, subfield;
421	int siz, pos;
422
423	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
424	fragmap <<= 1;
425	for (siz = 1; siz < fs->fs_frag; siz++) {
426		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
427			continue;
428		field = around[siz];
429		subfield = inside[siz];
430		for (pos = siz; pos <= fs->fs_frag; pos++) {
431			if ((fragmap & field) == subfield) {
432				fraglist[siz] += cnt;
433				pos += siz;
434				field <<= siz;
435				subfield <<= siz;
436			}
437			field <<= 1;
438			subfield <<= 1;
439		}
440	}
441}
442
443/*
444 * block operations
445 *
446 * check if a block is available
447 */
448int
449ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
450{
451	unsigned char mask;
452
453	switch ((int)fs->fs_frag) {
454	case 8:
455		return (cp[h] == 0xff);
456	case 4:
457		mask = 0x0f << ((h & 0x1) << 2);
458		return ((cp[h >> 1] & mask) == mask);
459	case 2:
460		mask = 0x03 << ((h & 0x3) << 1);
461		return ((cp[h >> 2] & mask) == mask);
462	case 1:
463		mask = 0x01 << (h & 0x7);
464		return ((cp[h >> 3] & mask) == mask);
465	default:
466#ifdef _KERNEL
467		panic("ffs_isblock");
468#endif
469		break;
470	}
471	return (0);
472}
473
474/*
475 * check if a block is free
476 */
477int
478ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
479{
480
481	switch ((int)fs->fs_frag) {
482	case 8:
483		return (cp[h] == 0);
484	case 4:
485		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
486	case 2:
487		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
488	case 1:
489		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
490	default:
491#ifdef _KERNEL
492		panic("ffs_isfreeblock");
493#endif
494		break;
495	}
496	return (0);
497}
498
499/*
500 * take a block out of the map
501 */
502void
503ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
504{
505
506	switch ((int)fs->fs_frag) {
507	case 8:
508		cp[h] = 0;
509		return;
510	case 4:
511		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
512		return;
513	case 2:
514		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
515		return;
516	case 1:
517		cp[h >> 3] &= ~(0x01 << (h & 0x7));
518		return;
519	default:
520#ifdef _KERNEL
521		panic("ffs_clrblock");
522#endif
523		break;
524	}
525}
526
527/*
528 * put a block into the map
529 */
530void
531ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
532{
533
534	switch ((int)fs->fs_frag) {
535	case 8:
536		cp[h] = 0xff;
537		return;
538	case 4:
539		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
540		return;
541	case 2:
542		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
543		return;
544	case 1:
545		cp[h >> 3] |= (0x01 << (h & 0x7));
546		return;
547	default:
548#ifdef _KERNEL
549		panic("ffs_setblock");
550#endif
551		break;
552	}
553}
554
555/*
556 * Update the cluster map because of an allocation or free.
557 *
558 * Cnt == 1 means free; cnt == -1 means allocating.
559 */
560void
561ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
562{
563	int32_t *sump;
564	int32_t *lp;
565	u_char *freemapp, *mapp;
566	int i, start, end, forw, back, map;
567	u_int bit;
568
569	if (fs->fs_contigsumsize <= 0)
570		return;
571	freemapp = cg_clustersfree(cgp);
572	sump = cg_clustersum(cgp);
573	/*
574	 * Allocate or clear the actual block.
575	 */
576	if (cnt > 0)
577		setbit(freemapp, blkno);
578	else
579		clrbit(freemapp, blkno);
580	/*
581	 * Find the size of the cluster going forward.
582	 */
583	start = blkno + 1;
584	end = start + fs->fs_contigsumsize;
585	if (end >= cgp->cg_nclusterblks)
586		end = cgp->cg_nclusterblks;
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)) != (NBBY - 1)) {
594			bit <<= 1;
595		} else {
596			map = *mapp++;
597			bit = 1;
598		}
599	}
600	forw = i - start;
601	/*
602	 * Find the size of the cluster going backward.
603	 */
604	start = blkno - 1;
605	end = start - fs->fs_contigsumsize;
606	if (end < 0)
607		end = -1;
608	mapp = &freemapp[start / NBBY];
609	map = *mapp--;
610	bit = 1U << (start % NBBY);
611	for (i = start; i > end; i--) {
612		if ((map & bit) == 0)
613			break;
614		if ((i & (NBBY - 1)) != 0) {
615			bit >>= 1;
616		} else {
617			map = *mapp--;
618			bit = 1U << (NBBY - 1);
619		}
620	}
621	back = start - i;
622	/*
623	 * Account for old cluster and the possibly new forward and
624	 * back clusters.
625	 */
626	i = back + forw + 1;
627	if (i > fs->fs_contigsumsize)
628		i = fs->fs_contigsumsize;
629	sump[i] += cnt;
630	if (back > 0)
631		sump[back] -= cnt;
632	if (forw > 0)
633		sump[forw] -= cnt;
634	/*
635	 * Update cluster summary information.
636	 */
637	lp = &sump[fs->fs_contigsumsize];
638	for (i = fs->fs_contigsumsize; i > 0; i--)
639		if (*lp-- > 0)
640			break;
641	fs->fs_maxcluster[cgp->cg_cgx] = i;
642}
643