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