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