1 /* $NetBSD: ffs_inode.c,v 1.3 1999/10/01 04:35:22 perseant 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 45 #else 46 __RCSID("$NetBSD: ffs_inode.c,v 1.3 1999/10/01 04:35:22 perseant Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/stat.h> 53 #ifdef sunos 54 #include <sys/vnode.h> 55 56 #include <ufs/fs.h> 57 #include <ufs/fsdir.h> 58 #include <ufs/inode.h> 59 #else 60 #include <ufs/ufs/dir.h> 61 #include <ufs/ufs/dinode.h> 62 #include <ufs/ffs/fs.h> 63 #include <ufs/ffs/ffs_extern.h> 64 #endif 65 66 #include <protocols/dumprestore.h> 67 68 #include <ctype.h> 69 #include <errno.h> 70 #include <fts.h> 71 #include <stdio.h> 72 #ifdef __STDC__ 73 #include <string.h> 74 #include <unistd.h> 75 #endif 76 77 #include "dump.h" 78 79 #ifndef SBOFF 80 #define SBOFF (SBLOCK * DEV_BSIZE) 81 #endif 82 83 struct fs *sblock; 84 85 /* 86 * Read the superblock from disk, and check its magic number. 87 * Determine whether byte-swapping needs to be done on this filesystem. 88 */ 89 int 90 fs_read_sblock(char *sblock_buf) 91 { 92 int needswap = 0; 93 94 sblock = (struct fs *)sblock_buf; 95 rawread(SBOFF, (char *) sblock, SBSIZE); 96 if (sblock->fs_magic != FS_MAGIC) { 97 if (sblock->fs_magic == bswap32(FS_MAGIC)) { 98 ffs_sb_swap(sblock, sblock, 0); 99 needswap = 1; 100 } else 101 quit("bad sblock magic number\n"); 102 } 103 return needswap; 104 } 105 106 /* 107 * Fill in the ufsi struct, as well as the maxino and dev_bsize global 108 * variables. 109 */ 110 struct ufsi * 111 fs_parametrize(void) 112 { 113 static struct ufsi ufsi; 114 115 #ifdef FS_44INODEFMT 116 if (sblock->fs_inodefmt >= FS_44INODEFMT) { 117 spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT); 118 } else { 119 /* 120 * Determine parameters for older filesystems. From 121 * /sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat() 122 * 123 * XXX: not sure if other variables (fs_npsect, fs_interleave, 124 * fs_nrpos, fs_maxfilesize) need to be fudged too. 125 */ 126 sblock->fs_qbmask = ~sblock->fs_bmask; 127 sblock->fs_qfmask = ~sblock->fs_fmask; 128 } 129 #endif 130 131 /* Fill out ufsi struct */ 132 ufsi.ufs_dsize = fsbtodb(sblock,sblock->fs_size); 133 ufsi.ufs_bsize = sblock->fs_bsize; 134 ufsi.ufs_bshift = sblock->fs_bshift; 135 ufsi.ufs_fsize = sblock->fs_fsize; 136 ufsi.ufs_frag = sblock->fs_frag; 137 ufsi.ufs_fsatoda = sblock->fs_fsbtodb; 138 ufsi.ufs_nindir = sblock->fs_nindir; 139 ufsi.ufs_inopb = sblock->fs_inopb; 140 ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen; 141 ufsi.ufs_bmask = sblock->fs_bmask; 142 ufsi.ufs_fmask = sblock->fs_fmask; 143 ufsi.ufs_qbmask = sblock->fs_qbmask; 144 ufsi.ufs_qfmask = sblock->fs_qfmask; 145 146 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 147 148 return &ufsi; 149 } 150 151 ino_t 152 fs_maxino(void) 153 { 154 return sblock->fs_ipg * sblock->fs_ncg; 155 } 156 157 struct dinode * 158 getino(inum) 159 ino_t inum; 160 { 161 static daddr_t minino, maxino; 162 static struct dinode inoblock[MAXINOPB]; 163 int i; 164 165 curino = inum; 166 if (inum >= minino && inum < maxino) 167 return (&inoblock[inum - minino]); 168 bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock, 169 (int)ufsib->ufs_bsize); 170 if (needswap) 171 for (i = 0; i < MAXINOPB; i++) 172 ffs_dinode_swap(&inoblock[i], &inoblock[i]); 173 minino = inum - (inum % INOPB(sblock)); 174 maxino = minino + INOPB(sblock); 175 return (&inoblock[inum - minino]); 176 } 177