ffs_inode.c revision 1.9 1 /* $NetBSD: ffs_inode.c,v 1.9 2001/12/22 08:05:24 lukem 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 __RCSID("$NetBSD: ffs_inode.c,v 1.9 2001/12/22 08:05:24 lukem Exp $");
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/stat.h>
49 #ifdef sunos
50 #include <sys/vnode.h>
51
52 #include <ufs/fs.h>
53 #include <ufs/fsdir.h>
54 #include <ufs/inode.h>
55 #else
56 #include <ufs/ufs/dir.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ffs/fs.h>
59 #include <ufs/ffs/ffs_extern.h>
60 #endif
61
62 #include <protocols/dumprestore.h>
63
64 #include <ctype.h>
65 #include <errno.h>
66 #include <fts.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <unistd.h>
70
71 #include "dump.h"
72
73 #ifndef SBOFF
74 #define SBOFF (SBLOCK * DEV_BSIZE)
75 #endif
76
77 struct fs *sblock;
78
79 /*
80 * Read the superblock from disk, and check its magic number.
81 * Determine whether byte-swapping needs to be done on this file system.
82 */
83 int
84 fs_read_sblock(char *superblock)
85 {
86 int ns = 0;
87
88 sblock = (struct fs *)superblock;
89 rawread(SBOFF, (char *) superblock, SBSIZE);
90 if (sblock->fs_magic != FS_MAGIC) {
91 if (sblock->fs_magic == bswap32(FS_MAGIC)) {
92 ffs_sb_swap(sblock, sblock);
93 ns = 1;
94 } else
95 quit("bad sblock magic number\n");
96 }
97 return ns;
98 }
99
100 /*
101 * Fill in the ufsi struct, as well as the maxino and dev_bsize global
102 * variables.
103 */
104 struct ufsi *
105 fs_parametrize(void)
106 {
107 static struct ufsi ufsi;
108
109 #ifdef FS_44INODEFMT
110 if (sblock->fs_inodefmt >= FS_44INODEFMT) {
111 spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
112 } else {
113 /*
114 * Determine parameters for older file systems. From
115 * /sys/ufs/ffs/ffs_vfsops.c::ffs_oldfscompat()
116 *
117 * XXX: not sure if other variables (fs_npsect, fs_interleave,
118 * fs_nrpos, fs_maxfilesize) need to be fudged too.
119 */
120 sblock->fs_qbmask = ~sblock->fs_bmask;
121 sblock->fs_qfmask = ~sblock->fs_fmask;
122 }
123 #endif
124
125 /* Fill out ufsi struct */
126 ufsi.ufs_dsize = fsbtodb(sblock,sblock->fs_size);
127 ufsi.ufs_bsize = sblock->fs_bsize;
128 ufsi.ufs_bshift = sblock->fs_bshift;
129 ufsi.ufs_fsize = sblock->fs_fsize;
130 ufsi.ufs_frag = sblock->fs_frag;
131 ufsi.ufs_fsatoda = sblock->fs_fsbtodb;
132 ufsi.ufs_nindir = sblock->fs_nindir;
133 ufsi.ufs_inopb = sblock->fs_inopb;
134 ufsi.ufs_maxsymlinklen = sblock->fs_maxsymlinklen;
135 ufsi.ufs_bmask = sblock->fs_bmask;
136 ufsi.ufs_fmask = sblock->fs_fmask;
137 ufsi.ufs_qbmask = sblock->fs_qbmask;
138 ufsi.ufs_qfmask = sblock->fs_qfmask;
139
140 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
141
142 return &ufsi;
143 }
144
145 ino_t
146 fs_maxino(void)
147 {
148
149 return sblock->fs_ipg * sblock->fs_ncg;
150 }
151
152 struct dinode *
153 getino(ino_t inum)
154 {
155 static daddr_t minino, maxino;
156 static struct dinode inoblock[MAXINOPB];
157 int i;
158
159 curino = inum;
160 if (inum >= minino && inum < maxino)
161 return (&inoblock[inum - minino]);
162 bread(fsatoda(ufsib, ino_to_fsba(sblock, inum)), (char *)inoblock,
163 (int)ufsib->ufs_bsize);
164 if (needswap)
165 for (i = 0; i < MAXINOPB; i++)
166 ffs_dinode_swap(&inoblock[i], &inoblock[i]);
167 minino = inum - (inum % INOPB(sblock));
168 maxino = minino + INOPB(sblock);
169 return (&inoblock[inum - minino]);
170 }
171