xref: /netbsd-src/sbin/dump/ffs_inode.c (revision 87ba0e2a319653af510fae24a6d2975d3589735b)
1*87ba0e2aSchs /*	$NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $ */
22763cc19Sperseant 
32763cc19Sperseant /*-
42763cc19Sperseant  * Copyright (c) 1980, 1991, 1993, 1994
52763cc19Sperseant  *	The Regents of the University of California.  All rights reserved.
62763cc19Sperseant  *
72763cc19Sperseant  * Redistribution and use in source and binary forms, with or without
82763cc19Sperseant  * modification, are permitted provided that the following conditions
92763cc19Sperseant  * are met:
102763cc19Sperseant  * 1. Redistributions of source code must retain the above copyright
112763cc19Sperseant  *    notice, this list of conditions and the following disclaimer.
122763cc19Sperseant  * 2. Redistributions in binary form must reproduce the above copyright
132763cc19Sperseant  *    notice, this list of conditions and the following disclaimer in the
142763cc19Sperseant  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
162763cc19Sperseant  *    may be used to endorse or promote products derived from this software
172763cc19Sperseant  *    without specific prior written permission.
182763cc19Sperseant  *
192763cc19Sperseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202763cc19Sperseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212763cc19Sperseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222763cc19Sperseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232763cc19Sperseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242763cc19Sperseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252763cc19Sperseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262763cc19Sperseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272763cc19Sperseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282763cc19Sperseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292763cc19Sperseant  * SUCH DAMAGE.
302763cc19Sperseant  */
312763cc19Sperseant 
322763cc19Sperseant #include <sys/cdefs.h>
332763cc19Sperseant #ifndef lint
346543a91fSlukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
356543a91fSlukem  The Regents of the University of California.  All rights reserved.");
362763cc19Sperseant #endif /* not lint */
372763cc19Sperseant 
382763cc19Sperseant #ifndef lint
39*87ba0e2aSchs __RCSID("$NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $");
402763cc19Sperseant #endif /* not lint */
412763cc19Sperseant 
422763cc19Sperseant #include <sys/param.h>
432763cc19Sperseant #include <sys/time.h>
442763cc19Sperseant #include <sys/stat.h>
452763cc19Sperseant #include <ufs/ufs/dir.h>
462763cc19Sperseant #include <ufs/ufs/dinode.h>
472763cc19Sperseant #include <ufs/ffs/fs.h>
482763cc19Sperseant #include <ufs/ffs/ffs_extern.h>
4942614ed3Sfvdl #include <ufs/ufs/ufs_bswap.h>
502763cc19Sperseant 
512763cc19Sperseant #include <protocols/dumprestore.h>
522763cc19Sperseant 
532763cc19Sperseant #include <ctype.h>
542763cc19Sperseant #include <errno.h>
552763cc19Sperseant #include <fts.h>
562763cc19Sperseant #include <stdio.h>
5742614ed3Sfvdl #include <stdlib.h>
582763cc19Sperseant #include <string.h>
592763cc19Sperseant #include <unistd.h>
602763cc19Sperseant 
612763cc19Sperseant #include "dump.h"
622763cc19Sperseant 
63a010b743Sdholland static struct fs *sblock;
642763cc19Sperseant 
6542614ed3Sfvdl static off_t sblock_try[] = SBLOCKSEARCH;
6642614ed3Sfvdl 
6742614ed3Sfvdl int is_ufs2;
6842614ed3Sfvdl 
692763cc19Sperseant /*
702763cc19Sperseant  * Read the superblock from disk, and check its magic number.
712763cc19Sperseant  * Determine whether byte-swapping needs to be done on this file system.
722763cc19Sperseant  */
732763cc19Sperseant int
fs_read_sblock(char * superblock)744c4307e3Slukem fs_read_sblock(char *superblock)
752763cc19Sperseant {
7642614ed3Sfvdl 	int i;
774c4307e3Slukem 	int ns = 0;
782763cc19Sperseant 
794c4307e3Slukem 	sblock = (struct fs *)superblock;
8013e2deaaSdsl 	for (i = 0; ; i++) {
8113e2deaaSdsl 		if (sblock_try[i] == -1)
82c8d11eb8Schristos 			quit("can't find superblock");
8342614ed3Sfvdl 		rawread(sblock_try[i], (char *)superblock, MAXBSIZE);
8442614ed3Sfvdl 
8542614ed3Sfvdl 		switch(sblock->fs_magic) {
86*87ba0e2aSchs 		case FS_UFS2EA_MAGIC:
87*87ba0e2aSchs 			sblock->fs_magic = FS_UFS2_MAGIC;
88*87ba0e2aSchs 			/*FALLTHROUGH*/
8942614ed3Sfvdl 		case FS_UFS2_MAGIC:
9042614ed3Sfvdl 			is_ufs2 = 1;
9142614ed3Sfvdl 			/*FALLTHROUGH*/
9242614ed3Sfvdl 		case FS_UFS1_MAGIC:
9313e2deaaSdsl 			break;
94*87ba0e2aSchs 		case FS_UFS2EA_MAGIC_SWAPPED:
95*87ba0e2aSchs 			sblock->fs_magic = FS_UFS2_MAGIC_SWAPPED;
96*87ba0e2aSchs 			/*FALLTHROUGH*/
9742614ed3Sfvdl 		case FS_UFS2_MAGIC_SWAPPED:
9842614ed3Sfvdl 			is_ufs2 = 1;
9942614ed3Sfvdl 			/*FALLTHROUGH*/
10042614ed3Sfvdl 		case FS_UFS1_MAGIC_SWAPPED:
1014c4307e3Slukem 			ns = 1;
10242614ed3Sfvdl 			ffs_sb_swap(sblock, sblock);
10313e2deaaSdsl 			break;
10442614ed3Sfvdl                 default:
10542614ed3Sfvdl                         continue;
1062763cc19Sperseant                 }
10713e2deaaSdsl 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
10813e2deaaSdsl 			continue;
10983a830e1Sdsl 		if ((is_ufs2 || sblock->fs_old_flags & FS_FLAGS_UPDATED)
11013e2deaaSdsl 		    && sblock_try[i] != sblock->fs_sblockloc)
11113e2deaaSdsl 			continue;
11213e2deaaSdsl 		break;
11342614ed3Sfvdl         }
1147f76a970Sws 	return ns;
1152763cc19Sperseant }
1162763cc19Sperseant 
1172763cc19Sperseant /*
1182763cc19Sperseant  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
1192763cc19Sperseant  * variables.
1202763cc19Sperseant  */
1212763cc19Sperseant struct ufsi *
fs_parametrize(void)1222763cc19Sperseant fs_parametrize(void)
1232763cc19Sperseant {
1242763cc19Sperseant 	static struct ufsi ufsi;
1252763cc19Sperseant 
1262763cc19Sperseant #ifdef FS_44INODEFMT
12742614ed3Sfvdl 	if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
1282763cc19Sperseant 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
1292763cc19Sperseant 	} else {
1302763cc19Sperseant 		/*
1312763cc19Sperseant 		 * Determine parameters for older file systems. From
1322763cc19Sperseant 		 *	/sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat()
1332763cc19Sperseant 		 *
1342763cc19Sperseant 		 * XXX: not sure if other variables (fs_npsect, fs_interleave,
1352763cc19Sperseant 		 * fs_nrpos, fs_maxfilesize) need to be fudged too.
1362763cc19Sperseant 		 */
1372763cc19Sperseant 		sblock->fs_qbmask = ~sblock->fs_bmask;
1382763cc19Sperseant 		sblock->fs_qfmask = ~sblock->fs_fmask;
1392763cc19Sperseant 	}
1402763cc19Sperseant #endif
1412763cc19Sperseant 
1422763cc19Sperseant 	/* Fill out ufsi struct */
1432737439dSdholland 	ufsi.ufs_dsize = FFS_FSBTODB(sblock,sblock->fs_size);
1442763cc19Sperseant 	ufsi.ufs_bsize = sblock->fs_bsize;
1452763cc19Sperseant 	ufsi.ufs_bshift = sblock->fs_bshift;
1462763cc19Sperseant 	ufsi.ufs_fsize = sblock->fs_fsize;
1472763cc19Sperseant 	ufsi.ufs_frag = sblock->fs_frag;
1482763cc19Sperseant 	ufsi.ufs_fsatoda = sblock->fs_fsbtodb;
1492763cc19Sperseant 	ufsi.ufs_nindir = sblock->fs_nindir;
1502763cc19Sperseant 	ufsi.ufs_inopb = sblock->fs_inopb;
1512763cc19Sperseant 	ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen;
1522763cc19Sperseant 	ufsi.ufs_bmask = sblock->fs_bmask;
1532763cc19Sperseant 	ufsi.ufs_fmask = sblock->fs_fmask;
1542763cc19Sperseant 	ufsi.ufs_qbmask = sblock->fs_qbmask;
1552763cc19Sperseant 	ufsi.ufs_qfmask = sblock->fs_qfmask;
1562763cc19Sperseant 
1572737439dSdholland 	dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1);
1582763cc19Sperseant 
1592763cc19Sperseant 	return &ufsi;
1602763cc19Sperseant }
1612763cc19Sperseant 
1622763cc19Sperseant ino_t
fs_maxino(void)1632763cc19Sperseant fs_maxino(void)
1642763cc19Sperseant {
16599feaf5bSlukem 
1662763cc19Sperseant 	return sblock->fs_ipg * sblock->fs_ncg;
1672763cc19Sperseant }
1682763cc19Sperseant 
16942614ed3Sfvdl void
fs_mapinodes(ino_t maxino __unused,u_int64_t * tape_size,int * anydirskipped)170746c5b06Schristos fs_mapinodes(ino_t maxino __unused, u_int64_t *tape_size, int *anydirskipped)
17142614ed3Sfvdl {
17242614ed3Sfvdl 	int i, cg, inosused;
17342614ed3Sfvdl 	struct cg *cgp;
17442614ed3Sfvdl 	ino_t ino;
17542614ed3Sfvdl 	char *cp;
17642614ed3Sfvdl 
17742614ed3Sfvdl 	if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
178c8d11eb8Schristos 		quite(errno, "fs_mapinodes: cannot allocate memory.");
17942614ed3Sfvdl 
18042614ed3Sfvdl 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
18142614ed3Sfvdl 		ino = cg * sblock->fs_ipg;
1822737439dSdholland 		bread(FFS_FSBTODB(sblock, cgtod(sblock, cg)), (char *)cgp,
18342614ed3Sfvdl 		    sblock->fs_cgsize);
18442614ed3Sfvdl 		if (needswap)
18542614ed3Sfvdl 			ffs_cg_swap(cgp, cgp, sblock);
18642614ed3Sfvdl 		if (sblock->fs_magic == FS_UFS2_MAGIC)
18742614ed3Sfvdl 			inosused = cgp->cg_initediblk;
18842614ed3Sfvdl 		else
18942614ed3Sfvdl 			inosused = sblock->fs_ipg;
19042614ed3Sfvdl 		/*
19142614ed3Sfvdl 		 * If we are using soft updates, then we can trust the
19242614ed3Sfvdl 		 * cylinder group inode allocation maps to tell us which
19342614ed3Sfvdl 		 * inodes are allocated. We will scan the used inode map
19442614ed3Sfvdl 		 * to find the inodes that are really in use, and then
19542614ed3Sfvdl 		 * read only those inodes in from disk.
19642614ed3Sfvdl 		 */
19742614ed3Sfvdl 		if (sblock->fs_flags & FS_DOSOFTDEP) {
19842614ed3Sfvdl 			if (!cg_chkmagic(cgp, 0))
199c8d11eb8Schristos 				quit("%s: cg %d: bad magic number\n",
200c8d11eb8Schristos 				    __func__, cg);
20142614ed3Sfvdl 			cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT];
20242614ed3Sfvdl 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
20342614ed3Sfvdl 				if (*cp == 0)
20442614ed3Sfvdl 					continue;
20542614ed3Sfvdl 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
20642614ed3Sfvdl 					if (*cp & i)
20742614ed3Sfvdl 						break;
20842614ed3Sfvdl 					inosused--;
20942614ed3Sfvdl 				}
21042614ed3Sfvdl 				break;
21142614ed3Sfvdl 			}
21242614ed3Sfvdl 			if (inosused <= 0)
21342614ed3Sfvdl 				continue;
21442614ed3Sfvdl 		}
21542614ed3Sfvdl 		for (i = 0; i < inosused; i++, ino++) {
216dcd34a91Sdholland 			if (ino < UFS_ROOTINO)
21742614ed3Sfvdl 				continue;
21842614ed3Sfvdl 			mapfileino(ino, tape_size, anydirskipped);
21942614ed3Sfvdl 		}
22042614ed3Sfvdl 	}
22142614ed3Sfvdl 
22242614ed3Sfvdl 	free(cgp);
22342614ed3Sfvdl }
22442614ed3Sfvdl 
22542614ed3Sfvdl union dinode *
getino(ino_t inum)22699feaf5bSlukem getino(ino_t inum)
2272763cc19Sperseant {
228a3ff3a30Sfvdl 	static ino_t minino, maxino;
22942614ed3Sfvdl 	static caddr_t inoblock;
23042614ed3Sfvdl 	int ntoswap, i;
23142614ed3Sfvdl 	struct ufs1_dinode *dp1;
23242614ed3Sfvdl 	struct ufs2_dinode *dp2;
2332763cc19Sperseant 
23442614ed3Sfvdl 	if (inoblock == NULL && (inoblock = malloc(ufsib->ufs_bsize)) == NULL)
235c8d11eb8Schristos 		quite(errno, "cannot allocate inode memory.");
2362763cc19Sperseant 	curino = inum;
2372763cc19Sperseant 	if (inum >= minino && inum < maxino)
23842614ed3Sfvdl 		goto gotit;
2392763cc19Sperseant 	bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock,
2402763cc19Sperseant 	    (int)ufsib->ufs_bsize);
241f1333577Sdholland 	minino = inum - (inum % FFS_INOPB(sblock));
242f1333577Sdholland 	maxino = minino + FFS_INOPB(sblock);
24342614ed3Sfvdl 	if (needswap) {
24442614ed3Sfvdl 		if (is_ufs2) {
24542614ed3Sfvdl 			dp2 = (struct ufs2_dinode *)inoblock;
24642614ed3Sfvdl 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs2_dinode);
24742614ed3Sfvdl 			for (i = 0; i < ntoswap; i++)
24842614ed3Sfvdl 				ffs_dinode2_swap(&dp2[i], &dp2[i]);
24942614ed3Sfvdl 		} else {
25042614ed3Sfvdl 			dp1 = (struct ufs1_dinode *)inoblock;
25142614ed3Sfvdl 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs1_dinode);
25242614ed3Sfvdl 			for (i = 0; i < ntoswap; i++)
25342614ed3Sfvdl 				ffs_dinode1_swap(&dp1[i], &dp1[i]);
25442614ed3Sfvdl 		}
25542614ed3Sfvdl 	}
25642614ed3Sfvdl gotit:
25742614ed3Sfvdl 	if (is_ufs2) {
25842614ed3Sfvdl 		dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
25942614ed3Sfvdl 		return (union dinode *)dp2;
26042614ed3Sfvdl 	}
26142614ed3Sfvdl 	dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
26242614ed3Sfvdl 	return ((union dinode *)dp1);
2632763cc19Sperseant }
264