Home | History | Annotate | Line # | Download | only in dump
ffs_inode.c revision 1.22
      1  1.22  dholland /*	$NetBSD: ffs_inode.c,v 1.22 2013/06/23 02:06:04 dholland Exp $ */
      2   1.1  perseant 
      3   1.1  perseant /*-
      4   1.1  perseant  * Copyright (c) 1980, 1991, 1993, 1994
      5   1.9     lukem  *	The Regents of the University of California.  All rights reserved.
      6   1.1  perseant  *
      7   1.1  perseant  * Redistribution and use in source and binary forms, with or without
      8   1.1  perseant  * modification, are permitted provided that the following conditions
      9   1.1  perseant  * are met:
     10   1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     11   1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     12   1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  perseant  *    documentation and/or other materials provided with the distribution.
     15  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1  perseant  *    may be used to endorse or promote products derived from this software
     17   1.1  perseant  *    without specific prior written permission.
     18   1.1  perseant  *
     19   1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1  perseant  * SUCH DAMAGE.
     30   1.1  perseant  */
     31   1.1  perseant 
     32   1.1  perseant #include <sys/cdefs.h>
     33   1.1  perseant #ifndef lint
     34  1.18     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
     35  1.18     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1  perseant #endif /* not lint */
     37   1.1  perseant 
     38   1.1  perseant #ifndef lint
     39  1.22  dholland __RCSID("$NetBSD: ffs_inode.c,v 1.22 2013/06/23 02:06:04 dholland Exp $");
     40   1.1  perseant #endif /* not lint */
     41   1.1  perseant 
     42   1.1  perseant #include <sys/param.h>
     43   1.1  perseant #include <sys/time.h>
     44   1.1  perseant #include <sys/stat.h>
     45   1.1  perseant #include <ufs/ufs/dir.h>
     46   1.1  perseant #include <ufs/ufs/dinode.h>
     47   1.1  perseant #include <ufs/ffs/fs.h>
     48   1.1  perseant #include <ufs/ffs/ffs_extern.h>
     49  1.12      fvdl #include <ufs/ufs/ufs_bswap.h>
     50   1.1  perseant 
     51   1.1  perseant #include <protocols/dumprestore.h>
     52   1.1  perseant 
     53   1.1  perseant #include <ctype.h>
     54   1.1  perseant #include <errno.h>
     55   1.1  perseant #include <fts.h>
     56   1.1  perseant #include <stdio.h>
     57  1.12      fvdl #include <stdlib.h>
     58   1.1  perseant #include <string.h>
     59   1.1  perseant #include <unistd.h>
     60   1.1  perseant 
     61   1.1  perseant #include "dump.h"
     62   1.1  perseant 
     63  1.19  dholland static struct fs *sblock;
     64  1.12      fvdl 
     65  1.12      fvdl static off_t sblock_try[] = SBLOCKSEARCH;
     66   1.1  perseant 
     67  1.12      fvdl int is_ufs2;
     68   1.1  perseant 
     69   1.1  perseant /*
     70   1.1  perseant  * Read the superblock from disk, and check its magic number.
     71   1.6     lukem  * Determine whether byte-swapping needs to be done on this file system.
     72   1.1  perseant  */
     73   1.1  perseant int
     74   1.8     lukem fs_read_sblock(char *superblock)
     75   1.1  perseant {
     76  1.12      fvdl 	int i;
     77   1.8     lukem 	int ns = 0;
     78   1.1  perseant 
     79   1.8     lukem 	sblock = (struct fs *)superblock;
     80  1.14       dsl 	for (i = 0; ; i++) {
     81  1.14       dsl 		if (sblock_try[i] == -1)
     82  1.14       dsl 			quit("can't find superblock\n");
     83  1.12      fvdl 		rawread(sblock_try[i], (char *)superblock, MAXBSIZE);
     84  1.12      fvdl 
     85  1.12      fvdl 		switch(sblock->fs_magic) {
     86  1.12      fvdl 		case FS_UFS2_MAGIC:
     87  1.12      fvdl 			is_ufs2 = 1;
     88  1.12      fvdl 			/*FALLTHROUGH*/
     89  1.12      fvdl 		case FS_UFS1_MAGIC:
     90  1.14       dsl 			break;
     91  1.12      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
     92  1.12      fvdl 			is_ufs2 = 1;
     93  1.12      fvdl 			/*FALLTHROUGH*/
     94  1.12      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
     95  1.12      fvdl 			ns = 1;
     96   1.7     lukem 			ffs_sb_swap(sblock, sblock);
     97  1.14       dsl 			break;
     98  1.12      fvdl                 default:
     99  1.12      fvdl                         continue;
    100  1.12      fvdl                 }
    101  1.14       dsl 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    102  1.14       dsl 			continue;
    103  1.16       dsl 		if ((is_ufs2 || sblock->fs_old_flags & FS_FLAGS_UPDATED)
    104  1.14       dsl 		    && sblock_try[i] != sblock->fs_sblockloc)
    105  1.14       dsl 			continue;
    106  1.14       dsl 		break;
    107  1.12      fvdl         }
    108  1.15        ws 	return ns;
    109   1.1  perseant }
    110   1.1  perseant 
    111   1.1  perseant /*
    112   1.1  perseant  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
    113   1.1  perseant  * variables.
    114   1.1  perseant  */
    115   1.1  perseant struct ufsi *
    116   1.1  perseant fs_parametrize(void)
    117   1.1  perseant {
    118   1.1  perseant 	static struct ufsi ufsi;
    119   1.1  perseant 
    120   1.1  perseant #ifdef FS_44INODEFMT
    121  1.12      fvdl 	if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
    122   1.1  perseant 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
    123   1.1  perseant 	} else {
    124   1.1  perseant 		/*
    125   1.6     lukem 		 * Determine parameters for older file systems. From
    126   1.1  perseant 		 *	/sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat()
    127   1.1  perseant 		 *
    128   1.1  perseant 		 * XXX: not sure if other variables (fs_npsect, fs_interleave,
    129   1.1  perseant 		 * fs_nrpos, fs_maxfilesize) need to be fudged too.
    130   1.1  perseant 		 */
    131   1.1  perseant 		sblock->fs_qbmask = ~sblock->fs_bmask;
    132   1.1  perseant 		sblock->fs_qfmask = ~sblock->fs_fmask;
    133   1.1  perseant 	}
    134   1.1  perseant #endif
    135   1.1  perseant 
    136   1.1  perseant 	/* Fill out ufsi struct */
    137  1.22  dholland 	ufsi.ufs_dsize = FFS_FSBTODB(sblock,sblock->fs_size);
    138   1.1  perseant 	ufsi.ufs_bsize = sblock->fs_bsize;
    139   1.1  perseant 	ufsi.ufs_bshift = sblock->fs_bshift;
    140   1.1  perseant 	ufsi.ufs_fsize = sblock->fs_fsize;
    141   1.1  perseant 	ufsi.ufs_frag = sblock->fs_frag;
    142   1.1  perseant 	ufsi.ufs_fsatoda = sblock->fs_fsbtodb;
    143   1.1  perseant 	ufsi.ufs_nindir = sblock->fs_nindir;
    144   1.1  perseant 	ufsi.ufs_inopb = sblock->fs_inopb;
    145   1.1  perseant 	ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen;
    146   1.1  perseant 	ufsi.ufs_bmask = sblock->fs_bmask;
    147   1.1  perseant 	ufsi.ufs_fmask = sblock->fs_fmask;
    148   1.1  perseant 	ufsi.ufs_qbmask = sblock->fs_qbmask;
    149   1.1  perseant 	ufsi.ufs_qfmask = sblock->fs_qfmask;
    150   1.1  perseant 
    151  1.22  dholland 	dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1);
    152   1.1  perseant 
    153   1.1  perseant 	return &ufsi;
    154   1.3  perseant }
    155   1.3  perseant 
    156   1.3  perseant ino_t
    157   1.3  perseant fs_maxino(void)
    158   1.3  perseant {
    159   1.4     lukem 
    160   1.3  perseant 	return sblock->fs_ipg * sblock->fs_ncg;
    161   1.1  perseant }
    162   1.1  perseant 
    163  1.12      fvdl void
    164  1.17  christos fs_mapinodes(ino_t maxino __unused, u_int64_t *tape_size, int *anydirskipped)
    165  1.12      fvdl {
    166  1.12      fvdl 	int i, cg, inosused;
    167  1.12      fvdl 	struct cg *cgp;
    168  1.12      fvdl 	ino_t ino;
    169  1.12      fvdl 	char *cp;
    170  1.12      fvdl 
    171  1.12      fvdl 	if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
    172  1.12      fvdl 		quit("fs_mapinodes: cannot allocate memory.\n");
    173  1.12      fvdl 
    174  1.12      fvdl 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
    175  1.12      fvdl 		ino = cg * sblock->fs_ipg;
    176  1.22  dholland 		bread(FFS_FSBTODB(sblock, cgtod(sblock, cg)), (char *)cgp,
    177  1.12      fvdl 		    sblock->fs_cgsize);
    178  1.12      fvdl 		if (needswap)
    179  1.12      fvdl 			ffs_cg_swap(cgp, cgp, sblock);
    180  1.12      fvdl 		if (sblock->fs_magic == FS_UFS2_MAGIC)
    181  1.12      fvdl 			inosused = cgp->cg_initediblk;
    182  1.12      fvdl 		else
    183  1.12      fvdl 			inosused = sblock->fs_ipg;
    184  1.12      fvdl 		/*
    185  1.12      fvdl 		 * If we are using soft updates, then we can trust the
    186  1.12      fvdl 		 * cylinder group inode allocation maps to tell us which
    187  1.12      fvdl 		 * inodes are allocated. We will scan the used inode map
    188  1.12      fvdl 		 * to find the inodes that are really in use, and then
    189  1.12      fvdl 		 * read only those inodes in from disk.
    190  1.12      fvdl 		 */
    191  1.12      fvdl 		if (sblock->fs_flags & FS_DOSOFTDEP) {
    192  1.12      fvdl 			if (!cg_chkmagic(cgp, 0))
    193  1.12      fvdl 				quit("mapfiles: cg %d: bad magic number\n", cg);
    194  1.12      fvdl 			cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT];
    195  1.12      fvdl 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
    196  1.12      fvdl 				if (*cp == 0)
    197  1.12      fvdl 					continue;
    198  1.12      fvdl 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
    199  1.12      fvdl 					if (*cp & i)
    200  1.12      fvdl 						break;
    201  1.12      fvdl 					inosused--;
    202  1.12      fvdl 				}
    203  1.12      fvdl 				break;
    204  1.12      fvdl 			}
    205  1.12      fvdl 			if (inosused <= 0)
    206  1.12      fvdl 				continue;
    207  1.12      fvdl 		}
    208  1.12      fvdl 		for (i = 0; i < inosused; i++, ino++) {
    209  1.20  dholland 			if (ino < UFS_ROOTINO)
    210  1.12      fvdl 				continue;
    211  1.12      fvdl 			mapfileino(ino, tape_size, anydirskipped);
    212  1.12      fvdl 		}
    213  1.12      fvdl 	}
    214  1.12      fvdl 
    215  1.12      fvdl 	free(cgp);
    216  1.12      fvdl }
    217  1.12      fvdl 
    218  1.12      fvdl union dinode *
    219   1.4     lukem getino(ino_t inum)
    220   1.1  perseant {
    221  1.11      fvdl 	static ino_t minino, maxino;
    222  1.12      fvdl 	static caddr_t inoblock;
    223  1.12      fvdl 	int ntoswap, i;
    224  1.12      fvdl 	struct ufs1_dinode *dp1;
    225  1.12      fvdl 	struct ufs2_dinode *dp2;
    226   1.1  perseant 
    227  1.12      fvdl 	if (inoblock == NULL && (inoblock = malloc(ufsib->ufs_bsize)) == NULL)
    228  1.12      fvdl 		quit("cannot allocate inode memory.\n");
    229   1.1  perseant 	curino = inum;
    230   1.1  perseant 	if (inum >= minino && inum < maxino)
    231  1.12      fvdl 		goto gotit;
    232   1.1  perseant 	bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock,
    233   1.1  perseant 	    (int)ufsib->ufs_bsize);
    234  1.21  dholland 	minino = inum - (inum % FFS_INOPB(sblock));
    235  1.21  dholland 	maxino = minino + FFS_INOPB(sblock);
    236  1.12      fvdl 	if (needswap) {
    237  1.12      fvdl 		if (is_ufs2) {
    238  1.12      fvdl 			dp2 = (struct ufs2_dinode *)inoblock;
    239  1.12      fvdl 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs2_dinode);
    240  1.12      fvdl 			for (i = 0; i < ntoswap; i++)
    241  1.12      fvdl 				ffs_dinode2_swap(&dp2[i], &dp2[i]);
    242  1.12      fvdl 		} else {
    243  1.12      fvdl 			dp1 = (struct ufs1_dinode *)inoblock;
    244  1.12      fvdl 			ntoswap = ufsib->ufs_bsize / sizeof(struct ufs1_dinode);
    245  1.12      fvdl 			for (i = 0; i < ntoswap; i++)
    246  1.12      fvdl 				ffs_dinode1_swap(&dp1[i], &dp1[i]);
    247  1.12      fvdl 		}
    248  1.12      fvdl 	}
    249  1.12      fvdl gotit:
    250  1.12      fvdl 	if (is_ufs2) {
    251  1.12      fvdl 		dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
    252  1.12      fvdl 		return (union dinode *)dp2;
    253  1.12      fvdl 	}
    254  1.12      fvdl 	dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
    255  1.12      fvdl 	return ((union dinode *)dp1);
    256   1.1  perseant }
    257