Home | History | Annotate | Line # | Download | only in dump_lfs
lfs_inode.c revision 1.2.10.1
      1  1.2.10.1  perseant /*      $NetBSD: lfs_inode.c,v 1.2.10.1 2001/07/02 17:48:11 perseant Exp $ */
      2       1.1  perseant 
      3       1.1  perseant /*-
      4       1.1  perseant  * Copyright (c) 1980, 1991, 1993, 1994
      5       1.1  perseant  *      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.1  perseant  * 3. All advertising materials mentioning features or use of this software
     16       1.1  perseant  *    must display the following acknowledgement:
     17       1.1  perseant  *      This product includes software developed by the University of
     18       1.1  perseant  *      California, Berkeley and its contributors.
     19       1.1  perseant  * 4. Neither the name of the University nor the names of its contributors
     20       1.1  perseant  *    may be used to endorse or promote products derived from this software
     21       1.1  perseant  *    without specific prior written permission.
     22       1.1  perseant  *
     23       1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1  perseant  * SUCH DAMAGE.
     34       1.1  perseant  */
     35       1.1  perseant 
     36       1.1  perseant #include <sys/cdefs.h>
     37       1.1  perseant #ifndef lint
     38       1.1  perseant __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     39       1.1  perseant         The Regents of the University of California.  All rights reserved.\n");
     40       1.1  perseant #endif /* not lint */
     41       1.1  perseant 
     42       1.1  perseant #ifndef lint
     43       1.1  perseant #if 0
     44       1.1  perseant static char sccsid[] = "@(#)main.c      8.6 (Berkeley) 5/1/95";
     45       1.1  perseant #else
     46  1.2.10.1  perseant __RCSID("$NetBSD: lfs_inode.c,v 1.2.10.1 2001/07/02 17:48:11 perseant Exp $");
     47       1.1  perseant #endif
     48       1.1  perseant #endif /* not lint */
     49       1.1  perseant 
     50       1.1  perseant #include <sys/param.h>
     51       1.1  perseant #include <sys/time.h>
     52       1.1  perseant #include <sys/stat.h>
     53       1.1  perseant #include <ufs/ufs/dir.h>
     54       1.1  perseant #include <ufs/ufs/dinode.h>
     55       1.1  perseant #include <sys/mount.h>
     56       1.1  perseant #include <ufs/lfs/lfs.h>
     57       1.1  perseant 
     58       1.1  perseant #include <protocols/dumprestore.h>
     59       1.1  perseant 
     60       1.1  perseant #include <ctype.h>
     61       1.1  perseant #include <errno.h>
     62       1.1  perseant #include <fts.h>
     63       1.1  perseant #include <stdio.h>
     64       1.1  perseant #ifdef __STDC__
     65       1.1  perseant #include <string.h>
     66       1.1  perseant #include <unistd.h>
     67       1.1  perseant #endif
     68       1.1  perseant 
     69       1.1  perseant #include "dump.h"
     70       1.1  perseant 
     71       1.1  perseant #define MAXIFPB        (MAXBSIZE / sizeof(IFILE))
     72       1.1  perseant 
     73       1.1  perseant #define	HASDUMPEDFILE	0x1
     74       1.1  perseant #define	HASSUBDIRS	0x2
     75       1.1  perseant 
     76       1.1  perseant struct lfs *sblock;
     77       1.1  perseant 
     78       1.1  perseant /*
     79       1.1  perseant  * Read the superblock from disk, and check its magic number.
     80       1.1  perseant  * Determine whether byte-swapping needs to be done on this filesystem.
     81       1.1  perseant  */
     82       1.1  perseant int
     83       1.1  perseant fs_read_sblock(char *sblock_buf)
     84       1.1  perseant {
     85       1.1  perseant 	int needswap = 0;
     86       1.1  perseant 
     87       1.1  perseant 	sblock = (struct lfs *)sblock_buf;
     88       1.1  perseant 	rawread(LFS_LABELPAD, (char *) sblock, LFS_SBPAD);
     89       1.1  perseant 	if (sblock->lfs_magic != LFS_MAGIC) {
     90       1.2  perseant #ifdef notyet
     91       1.1  perseant 		if (sblock->lfs_magic == bswap32(LFS_MAGIC)) {
     92       1.1  perseant 			lfs_sb_swap(sblock, sblock, 0);
     93       1.1  perseant 			needswap = 1;
     94       1.1  perseant 		} else
     95       1.1  perseant #endif
     96       1.1  perseant 			quit("bad sblock magic number\n");
     97       1.1  perseant 	}
     98       1.1  perseant 	return needswap;
     99       1.1  perseant }
    100       1.1  perseant 
    101       1.1  perseant /*
    102       1.1  perseant  * Fill in the ufsi struct, as well as the maxino and dev_bsize global
    103       1.1  perseant  * variables.
    104       1.1  perseant  */
    105       1.1  perseant struct ufsi *
    106       1.1  perseant fs_parametrize(void)
    107       1.1  perseant {
    108       1.1  perseant 	static struct ufsi ufsi;
    109       1.1  perseant 
    110       1.2  perseant 	spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
    111       1.2  perseant 
    112       1.1  perseant 	ufsi.ufs_dsize = fsbtodb(sblock,sblock->lfs_size);
    113  1.2.10.1  perseant 	if (sblock->lfs_version == 1)
    114  1.2.10.1  perseant 		ufsi.ufs_dsize = sblock->lfs_size >> sblock->lfs_blktodb;
    115       1.1  perseant 	ufsi.ufs_bsize = sblock->lfs_bsize;
    116       1.1  perseant 	ufsi.ufs_bshift = sblock->lfs_bshift;
    117       1.1  perseant 	ufsi.ufs_fsize = sblock->lfs_fsize;
    118       1.1  perseant 	ufsi.ufs_frag = sblock->lfs_frag;
    119  1.2.10.1  perseant 	ufsi.ufs_fsatoda = sblock->lfs_fsbtodb;
    120  1.2.10.1  perseant 	if (sblock->lfs_version == 1)
    121  1.2.10.1  perseant 		ufsi.ufs_fsatoda = 0;
    122       1.1  perseant 	ufsi.ufs_nindir = sblock->lfs_nindir;
    123       1.1  perseant 	ufsi.ufs_inopb = sblock->lfs_inopb;
    124       1.1  perseant 	ufsi.ufs_maxsymlinklen = sblock->lfs_maxsymlinklen;
    125       1.1  perseant 	ufsi.ufs_bmask = ~(sblock->lfs_bmask);
    126       1.1  perseant 	ufsi.ufs_qbmask = sblock->lfs_bmask;
    127       1.1  perseant 	ufsi.ufs_fmask = ~(sblock->lfs_ffmask);
    128       1.1  perseant 	ufsi.ufs_qfmask = sblock->lfs_ffmask;
    129       1.1  perseant 
    130  1.2.10.1  perseant 	dev_bsize = sblock->lfs_bsize >> sblock->lfs_blktodb;
    131       1.1  perseant 
    132       1.1  perseant 	return &ufsi;
    133       1.1  perseant }
    134       1.1  perseant 
    135       1.1  perseant ino_t
    136       1.1  perseant fs_maxino(void)
    137       1.1  perseant {
    138       1.1  perseant 	return ((getino(sblock->lfs_ifile)->di_size
    139       1.1  perseant 		   - (sblock->lfs_cleansz + sblock->lfs_segtabsz)
    140       1.1  perseant 		   * sblock->lfs_bsize)
    141       1.1  perseant 		  / sblock->lfs_bsize) * sblock->lfs_ifpb - 1;
    142       1.1  perseant }
    143       1.1  perseant 
    144       1.1  perseant /*
    145       1.1  perseant  * XXX KS - I know there's a better way to do this.
    146       1.1  perseant  */
    147       1.1  perseant #define BASE_SINDIR (NDADDR)
    148       1.1  perseant #define BASE_DINDIR (NDADDR+NINDIR(fs))
    149       1.1  perseant #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
    150       1.1  perseant 
    151       1.1  perseant #define D_UNITS (NINDIR(fs))
    152       1.1  perseant #define T_UNITS (NINDIR(fs)*NINDIR(fs))
    153       1.1  perseant 
    154       1.1  perseant static daddr_t
    155       1.1  perseant lfs_bmap(struct lfs *fs, struct dinode *idinode, ufs_daddr_t lbn)
    156       1.1  perseant {
    157       1.1  perseant 	ufs_daddr_t residue, up;
    158       1.1  perseant 	int off=0;
    159       1.1  perseant 	char bp[MAXBSIZE];
    160       1.1  perseant 
    161  1.2.10.1  perseant 	if(lbn > 0 && lbn > lblkno(fs, idinode->di_size)) {
    162       1.1  perseant 		return UNASSIGNED;
    163       1.1  perseant 	}
    164       1.1  perseant 	/*
    165       1.1  perseant 	 * Indirect blocks: if it is a first-level indirect, pull its
    166       1.1  perseant 	 * address from the inode; otherwise, call ourselves to find the
    167       1.1  perseant 	 * address of the parent indirect block, and load that to find
    168       1.1  perseant 	 * the desired address.
    169       1.1  perseant 	 */
    170       1.1  perseant 	if(lbn < 0) {
    171       1.1  perseant 		lbn *= -1;
    172       1.1  perseant 		if(lbn == NDADDR) {
    173       1.1  perseant 			/* printf("lbn %d: single indir base\n", -lbn); */
    174       1.1  perseant 			return idinode->di_ib[0]; /* single indirect */
    175       1.1  perseant 		} else if(lbn == BASE_DINDIR+1) {
    176       1.1  perseant 			/* printf("lbn %d: double indir base\n", -lbn); */
    177       1.1  perseant 			return idinode->di_ib[1]; /* double indirect */
    178       1.1  perseant 		} else if(lbn == BASE_TINDIR+2) {
    179       1.1  perseant 			/* printf("lbn %d: triple indir base\n", -lbn); */
    180       1.1  perseant 			return idinode->di_ib[2]; /* triple indirect */
    181       1.1  perseant 		}
    182       1.1  perseant 
    183       1.1  perseant 		/*
    184       1.1  perseant 		 * Find the immediate parent. This is essentially finding the
    185       1.1  perseant 		 * residue of modulus, and then rounding accordingly.
    186       1.1  perseant 		 */
    187       1.1  perseant 		residue = (lbn-NDADDR) % NINDIR(fs);
    188       1.1  perseant 		if(residue == 1) {
    189       1.1  perseant 			/* Double indirect.  Parent is the triple. */
    190       1.1  perseant 			up = idinode->di_ib[2];
    191       1.1  perseant 			off = (lbn-2-BASE_TINDIR)/(NINDIR(fs)*NINDIR(fs));
    192       1.1  perseant 			if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
    193       1.1  perseant 				return UNASSIGNED;
    194       1.1  perseant 			/* printf("lbn %d: parent is the triple\n", -lbn); */
    195  1.2.10.1  perseant 			bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
    196       1.1  perseant 			return ((daddr_t *)bp)[off];
    197       1.1  perseant 		} else /* residue == 0 */ {
    198       1.1  perseant 			/* Single indirect.  Two cases. */
    199       1.1  perseant 			if(lbn < BASE_TINDIR) {
    200       1.1  perseant 				/* Parent is the double, simple */
    201       1.1  perseant 				up = -(BASE_DINDIR) - 1;
    202       1.1  perseant 				off = (lbn-BASE_DINDIR) / D_UNITS;
    203       1.1  perseant 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
    204       1.1  perseant 			} else {
    205       1.1  perseant 				/* Ancestor is the triple, more complex */
    206       1.1  perseant 				up = ((lbn-BASE_TINDIR) / T_UNITS)
    207       1.1  perseant 					* T_UNITS + BASE_TINDIR + 1;
    208       1.1  perseant 				off = (lbn/D_UNITS) - (up/D_UNITS);
    209       1.1  perseant 				up = -up;
    210       1.1  perseant 				/* printf("lbn %d: parent is %d/%d\n", -lbn, up,off); */
    211       1.1  perseant 			}
    212       1.1  perseant 		}
    213       1.1  perseant 	} else {
    214       1.1  perseant 		/* Direct block.  Its parent must be a single indirect. */
    215       1.1  perseant 		if(lbn < NDADDR)
    216       1.1  perseant 			return idinode->di_db[lbn];
    217       1.1  perseant 		else {
    218       1.1  perseant 			/* Parent is an indirect block. */
    219       1.1  perseant 			up = -(((lbn-NDADDR) / D_UNITS) * D_UNITS + NDADDR);
    220       1.1  perseant 			off = (lbn-NDADDR) % D_UNITS;
    221       1.1  perseant 			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
    222       1.1  perseant 		}
    223       1.1  perseant 	}
    224       1.1  perseant 	up = lfs_bmap(fs,idinode,up);
    225       1.1  perseant 	if(up == UNASSIGNED || up == LFS_UNUSED_DADDR)
    226       1.1  perseant 		return UNASSIGNED;
    227  1.2.10.1  perseant 	bread(fsbtodb(sblock, up), bp, sblock->lfs_bsize);
    228       1.1  perseant 	return ((daddr_t *)bp)[off];
    229       1.1  perseant }
    230       1.1  perseant 
    231       1.1  perseant static struct ifile *
    232       1.1  perseant lfs_ientry(ino_t ino)
    233       1.1  perseant {
    234       1.1  perseant     static struct ifile ifileblock[MAXIFPB];
    235       1.1  perseant     static daddr_t ifblkno;
    236       1.1  perseant     ufs_daddr_t lbn;
    237       1.1  perseant     daddr_t blkno;
    238       1.1  perseant 
    239       1.1  perseant     lbn = ino/sblock->lfs_ifpb + sblock->lfs_cleansz + sblock->lfs_segtabsz;
    240       1.1  perseant     blkno = lfs_bmap(sblock,getino(sblock->lfs_ifile),lbn);
    241       1.1  perseant     if(blkno != ifblkno)
    242  1.2.10.1  perseant 	    bread(fsbtodb(sblock, blkno), (char *)ifileblock,
    243  1.2.10.1  perseant 		  sblock->lfs_bsize);
    244       1.1  perseant     return ifileblock + (ino%sblock->lfs_ifpb);
    245       1.1  perseant }
    246       1.1  perseant 
    247       1.1  perseant /* Search a block for a specific dinode. */
    248       1.2  perseant static struct dinode *
    249       1.1  perseant lfs_ifind(struct lfs *fs, ino_t ino, struct dinode *dip)
    250       1.1  perseant {
    251       1.1  perseant 	register int cnt;
    252       1.1  perseant 
    253       1.1  perseant 	for(cnt=0;cnt<INOPB(fs);cnt++)
    254       1.1  perseant 		if(dip[cnt].di_inumber == ino)
    255       1.1  perseant 			return &(dip[cnt]);
    256       1.1  perseant 	return NULL;
    257       1.1  perseant }
    258       1.1  perseant 
    259       1.1  perseant struct dinode *
    260       1.1  perseant getino(inum)
    261       1.1  perseant 	ino_t inum;
    262       1.1  perseant {
    263       1.1  perseant 	static daddr_t inoblkno;
    264       1.1  perseant 	daddr_t blkno;
    265       1.1  perseant 	static struct dinode inoblock[MAXINOPB];
    266       1.1  perseant 	static struct dinode ifile_dinode; /* XXX fill this in */
    267       1.1  perseant 	static struct dinode empty_dinode; /* Always stays zeroed */
    268       1.1  perseant 	struct dinode *dp;
    269       1.1  perseant 
    270       1.1  perseant 	if(inum == sblock->lfs_ifile) {
    271       1.1  perseant 		/* Load the ifile inode if not already */
    272       1.1  perseant 		if(ifile_dinode.di_u.inumber == 0) {
    273       1.1  perseant 			blkno = sblock->lfs_idaddr;
    274  1.2.10.1  perseant 			bread(fsbtodb(sblock, blkno), (char *)inoblock,
    275  1.2.10.1  perseant 				(int)sblock->lfs_bsize);
    276       1.1  perseant 			dp = lfs_ifind(sblock, inum, inoblock);
    277       1.1  perseant 			ifile_dinode = *dp; /* Structure copy */
    278       1.1  perseant 		}
    279       1.1  perseant 		return &ifile_dinode;
    280       1.1  perseant 	}
    281       1.1  perseant 
    282       1.1  perseant 	curino = inum;
    283       1.1  perseant 	blkno = lfs_ientry(inum)->if_daddr;
    284       1.1  perseant 	if(blkno == LFS_UNUSED_DADDR)
    285       1.1  perseant 		return &empty_dinode;
    286       1.1  perseant 
    287       1.1  perseant 	if(blkno != inoblkno) {
    288  1.2.10.1  perseant 		bread(fsbtodb(sblock, blkno), (char *)inoblock,
    289  1.2.10.1  perseant 			(int)sblock->lfs_bsize);
    290       1.2  perseant #ifdef notyet
    291       1.1  perseant 		if (needswap)
    292       1.1  perseant 			for (i = 0; i < MAXINOPB; i++)
    293       1.1  perseant 				ffs_dinode_swap(&inoblock[i], &inoblock[i]);
    294       1.2  perseant #endif
    295       1.1  perseant 	}
    296       1.1  perseant 	return lfs_ifind(sblock, inum, inoblock);
    297       1.1  perseant }
    298