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