Home | History | Annotate | Line # | Download | only in dump
      1 /*	$NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1980, 1991, 1993, 1994
      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 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 __RCSID("$NetBSD: ffs_inode.c,v 1.24 2022/11/17 06:40:38 chs Exp $");
     40 #endif /* not lint */
     41 
     42 #include <sys/param.h>
     43 #include <sys/time.h>
     44 #include <sys/stat.h>
     45 #include <ufs/ufs/dir.h>
     46 #include <ufs/ufs/dinode.h>
     47 #include <ufs/ffs/fs.h>
     48 #include <ufs/ffs/ffs_extern.h>
     49 #include <ufs/ufs/ufs_bswap.h>
     50 
     51 #include <protocols/dumprestore.h>
     52 
     53 #include <ctype.h>
     54 #include <errno.h>
     55 #include <fts.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include "dump.h"
     62 
     63 static struct fs *sblock;
     64 
     65 static off_t sblock_try[] = SBLOCKSEARCH;
     66 
     67 int is_ufs2;
     68 
     69 /*
     70  * Read the superblock from disk, and check its magic number.
     71  * Determine whether byte-swapping needs to be done on this file system.
     72  */
     73 int
     74 fs_read_sblock(char *superblock)
     75 {
     76 	int i;
     77 	int ns = 0;
     78 
     79 	sblock = (struct fs *)superblock;
     80 	for (i = 0; ; i++) {
     81 		if (sblock_try[i] == -1)
     82 			quit("can't find superblock");
     83 		rawread(sblock_try[i], (char *)superblock, MAXBSIZE);
     84 
     85 		switch(sblock->fs_magic) {
     86 		case FS_UFS2EA_MAGIC:
     87 			sblock->fs_magic = FS_UFS2_MAGIC;
     88 			/*FALLTHROUGH*/
     89 		case FS_UFS2_MAGIC:
     90 			is_ufs2 = 1;
     91 			/*FALLTHROUGH*/
     92 		case FS_UFS1_MAGIC:
     93 			break;
     94 		case FS_UFS2EA_MAGIC_SWAPPED:
     95 			sblock->fs_magic = FS_UFS2_MAGIC_SWAPPED;
     96 			/*FALLTHROUGH*/
     97 		case FS_UFS2_MAGIC_SWAPPED:
     98 			is_ufs2 = 1;
     99 			/*FALLTHROUGH*/
    100 		case FS_UFS1_MAGIC_SWAPPED:
    101 			ns = 1;
    102 			ffs_sb_swap(sblock, sblock);
    103 			break;
    104                 default:
    105                         continue;
    106                 }
    107 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    108 			continue;
    109 		if ((is_ufs2 || sblock->fs_old_flags & FS_FLAGS_UPDATED)
    110 		    && sblock_try[i] != sblock->fs_sblockloc)
    111 			continue;
    112 		break;
    113         }
    114 	return ns;
    115 }
    116 
    117 /*
    118  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
    119  * variables.
    120  */
    121 struct ufsi *
    122 fs_parametrize(void)
    123 {
    124 	static struct ufsi ufsi;
    125 
    126 #ifdef FS_44INODEFMT
    127 	if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
    128 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
    129 	} else {
    130 		/*
    131 		 * Determine parameters for older file systems. From
    132 		 *	/sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat()
    133 		 *
    134 		 * XXX: not sure if other variables (fs_npsect, fs_interleave,
    135 		 * fs_nrpos, fs_maxfilesize) need to be fudged too.
    136 		 */
    137 		sblock->fs_qbmask = ~sblock->fs_bmask;
    138 		sblock->fs_qfmask = ~sblock->fs_fmask;
    139 	}
    140 #endif
    141 
    142 	/* Fill out ufsi struct */
    143 	ufsi.ufs_dsize = FFS_FSBTODB(sblock,sblock->fs_size);
    144 	ufsi.ufs_bsize = sblock->fs_bsize;
    145 	ufsi.ufs_bshift = sblock->fs_bshift;
    146 	ufsi.ufs_fsize = sblock->fs_fsize;
    147 	ufsi.ufs_frag = sblock->fs_frag;
    148 	ufsi.ufs_fsatoda = sblock->fs_fsbtodb;
    149 	ufsi.ufs_nindir = sblock->fs_nindir;
    150 	ufsi.ufs_inopb = sblock->fs_inopb;
    151 	ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen;
    152 	ufsi.ufs_bmask = sblock->fs_bmask;
    153 	ufsi.ufs_fmask = sblock->fs_fmask;
    154 	ufsi.ufs_qbmask = sblock->fs_qbmask;
    155 	ufsi.ufs_qfmask = sblock->fs_qfmask;
    156 
    157 	dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1);
    158 
    159 	return &ufsi;
    160 }
    161 
    162 ino_t
    163 fs_maxino(void)
    164 {
    165 
    166 	return sblock->fs_ipg * sblock->fs_ncg;
    167 }
    168 
    169 void
    170 fs_mapinodes(ino_t maxino __unused, u_int64_t *tape_size, int *anydirskipped)
    171 {
    172 	int i, cg, inosused;
    173 	struct cg *cgp;
    174 	ino_t ino;
    175 	char *cp;
    176 
    177 	if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
    178 		quite(errno, "fs_mapinodes: cannot allocate memory.");
    179 
    180 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
    181 		ino = cg * sblock->fs_ipg;
    182 		bread(FFS_FSBTODB(sblock, cgtod(sblock, cg)), (char *)cgp,
    183 		    sblock->fs_cgsize);
    184 		if (needswap)
    185 			ffs_cg_swap(cgp, cgp, sblock);
    186 		if (sblock->fs_magic == FS_UFS2_MAGIC)
    187 			inosused = cgp->cg_initediblk;
    188 		else
    189 			inosused = sblock->fs_ipg;
    190 		/*
    191 		 * If we are using soft updates, then we can trust the
    192 		 * cylinder group inode allocation maps to tell us which
    193 		 * inodes are allocated. We will scan the used inode map
    194 		 * to find the inodes that are really in use, and then
    195 		 * read only those inodes in from disk.
    196 		 */
    197 		if (sblock->fs_flags & FS_DOSOFTDEP) {
    198 			if (!cg_chkmagic(cgp, 0))
    199 				quit("%s: cg %d: bad magic number\n",
    200 				    __func__, cg);
    201 			cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT];
    202 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
    203 				if (*cp == 0)
    204 					continue;
    205 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
    206 					if (*cp & i)
    207 						break;
    208 					inosused--;
    209 				}
    210 				break;
    211 			}
    212 			if (inosused <= 0)
    213 				continue;
    214 		}
    215 		for (i = 0; i < inosused; i++, ino++) {
    216 			if (ino < UFS_ROOTINO)
    217 				continue;
    218 			mapfileino(ino, tape_size, anydirskipped);
    219 		}
    220 	}
    221 
    222 	free(cgp);
    223 }
    224 
    225 union dinode *
    226 getino(ino_t inum)
    227 {
    228 	static ino_t minino, maxino;
    229 	static caddr_t inoblock;
    230 	int ntoswap, i;
    231 	struct ufs1_dinode *dp1;
    232 	struct ufs2_dinode *dp2;
    233 
    234 	if (inoblock == NULL && (inoblock = malloc(ufsib->ufs_bsize)) == NULL)
    235 		quite(errno, "cannot allocate inode memory.");
    236 	curino = inum;
    237 	if (inum >= minino && inum < maxino)
    238 		goto gotit;
    239 	bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock,
    240 	    (int)ufsib->ufs_bsize);
    241 	minino = inum - (inum % FFS_INOPB(sblock));
    242 	maxino = minino + FFS_INOPB(sblock);
    243 	if (needswap) {
    244 		if (is_ufs2) {
    245 			dp2 = (struct ufs2_dinode *)inoblock;
    246 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs2_dinode);
    247 			for (i = 0; i < ntoswap; i++)
    248 				ffs_dinode2_swap(&dp2[i], &dp2[i]);
    249 		} else {
    250 			dp1 = (struct ufs1_dinode *)inoblock;
    251 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs1_dinode);
    252 			for (i = 0; i < ntoswap; i++)
    253 				ffs_dinode1_swap(&dp1[i], &dp1[i]);
    254 		}
    255 	}
    256 gotit:
    257 	if (is_ufs2) {
    258 		dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
    259 		return (union dinode *)dp2;
    260 	}
    261 	dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
    262 	return ((union dinode *)dp1);
    263 }
    264