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