traverse.c revision 1.18 1 1.18 lukem /* $NetBSD: traverse.c,v 1.18 1997/09/15 07:58:09 lukem Exp $ */
2 1.9 cgd
3 1.1 cgd /*-
4 1.4 mycroft * Copyright (c) 1980, 1988, 1991, 1993
5 1.4 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.18 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.9 cgd #if 0
39 1.9 cgd static char sccsid[] = "@(#)traverse.c 8.2 (Berkeley) 9/23/93";
40 1.9 cgd #else
41 1.18 lukem __RCSID("$NetBSD: traverse.c,v 1.18 1997/09/15 07:58:09 lukem Exp $");
42 1.9 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.4 mycroft #include <sys/param.h>
46 1.4 mycroft #include <sys/time.h>
47 1.4 mycroft #include <sys/stat.h>
48 1.1 cgd #ifdef sunos
49 1.4 mycroft #include <sys/vnode.h>
50 1.4 mycroft
51 1.1 cgd #include <ufs/fs.h>
52 1.4 mycroft #include <ufs/fsdir.h>
53 1.4 mycroft #include <ufs/inode.h>
54 1.1 cgd #else
55 1.4 mycroft #include <ufs/ffs/fs.h>
56 1.4 mycroft #include <ufs/ufs/dir.h>
57 1.4 mycroft #include <ufs/ufs/dinode.h>
58 1.1 cgd #endif
59 1.4 mycroft
60 1.1 cgd #include <protocols/dumprestore.h>
61 1.4 mycroft
62 1.4 mycroft #include <ctype.h>
63 1.17 lukem #include <errno.h>
64 1.17 lukem #include <fts.h>
65 1.4 mycroft #include <stdio.h>
66 1.1 cgd #ifdef __STDC__
67 1.4 mycroft #include <string.h>
68 1.1 cgd #include <unistd.h>
69 1.1 cgd #endif
70 1.4 mycroft
71 1.1 cgd #include "dump.h"
72 1.1 cgd
73 1.1 cgd #define HASDUMPEDFILE 0x1
74 1.1 cgd #define HASSUBDIRS 0x2
75 1.1 cgd
76 1.4 mycroft #ifdef FS_44INODEFMT
77 1.4 mycroft typedef quad_t fsizeT;
78 1.4 mycroft #else
79 1.15 cgd typedef int32_t fsizeT;
80 1.4 mycroft #endif
81 1.4 mycroft
82 1.4 mycroft static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
83 1.4 mycroft static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
84 1.4 mycroft static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
85 1.4 mycroft
86 1.1 cgd /*
87 1.1 cgd * This is an estimation of the number of TP_BSIZE blocks in the file.
88 1.1 cgd * It estimates the number of blocks in files with holes by assuming
89 1.1 cgd * that all of the blocks accounted for by di_blocks are data blocks
90 1.1 cgd * (when some of the blocks are usually used for indirect pointers);
91 1.1 cgd * hence the estimate may be high.
92 1.1 cgd */
93 1.1 cgd long
94 1.1 cgd blockest(dp)
95 1.16 lukem struct dinode *dp;
96 1.1 cgd {
97 1.1 cgd long blkest, sizeest;
98 1.1 cgd
99 1.1 cgd /*
100 1.1 cgd * dp->di_size is the size of the file in bytes.
101 1.1 cgd * dp->di_blocks stores the number of sectors actually in the file.
102 1.1 cgd * If there are more sectors than the size would indicate, this just
103 1.1 cgd * means that there are indirect blocks in the file or unused
104 1.1 cgd * sectors in the last file block; we can safely ignore these
105 1.1 cgd * (blkest = sizeest below).
106 1.1 cgd * If the file is bigger than the number of sectors would indicate,
107 1.1 cgd * then the file has holes in it. In this case we must use the
108 1.1 cgd * block count to estimate the number of data blocks used, but
109 1.1 cgd * we use the actual size for estimating the number of indirect
110 1.1 cgd * dump blocks (sizeest vs. blkest in the indirect block
111 1.1 cgd * calculation).
112 1.1 cgd */
113 1.1 cgd blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
114 1.1 cgd sizeest = howmany(dp->di_size, TP_BSIZE);
115 1.1 cgd if (blkest > sizeest)
116 1.1 cgd blkest = sizeest;
117 1.1 cgd if (dp->di_size > sblock->fs_bsize * NDADDR) {
118 1.1 cgd /* calculate the number of indirect blocks on the dump tape */
119 1.1 cgd blkest +=
120 1.1 cgd howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
121 1.1 cgd TP_NINDIR);
122 1.1 cgd }
123 1.1 cgd return (blkest + 1);
124 1.1 cgd }
125 1.1 cgd
126 1.4 mycroft /* Auxiliary macro to pick up files changed since previous dump. */
127 1.4 mycroft #define CHANGEDSINCE(dp, t) \
128 1.4 mycroft ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
129 1.4 mycroft
130 1.4 mycroft /* The WANTTODUMP macro decides whether a file should be dumped. */
131 1.4 mycroft #ifdef UF_NODUMP
132 1.4 mycroft #define WANTTODUMP(dp) \
133 1.4 mycroft (CHANGEDSINCE(dp, spcl.c_ddate) && \
134 1.4 mycroft (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
135 1.4 mycroft #else
136 1.4 mycroft #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
137 1.4 mycroft #endif
138 1.4 mycroft
139 1.1 cgd /*
140 1.17 lukem * Determine if given inode should be dumped
141 1.17 lukem */
142 1.17 lukem void
143 1.17 lukem mapfileino(ino, tapesize, dirskipped)
144 1.17 lukem ino_t ino;
145 1.17 lukem long *tapesize;
146 1.17 lukem int *dirskipped;
147 1.17 lukem {
148 1.17 lukem int mode;
149 1.17 lukem struct dinode *dp;
150 1.17 lukem
151 1.17 lukem dp = getino(ino);
152 1.17 lukem if ((mode = (dp->di_mode & IFMT)) == 0)
153 1.17 lukem return;
154 1.17 lukem SETINO(ino, usedinomap);
155 1.17 lukem if (mode == IFDIR)
156 1.17 lukem SETINO(ino, dumpdirmap);
157 1.17 lukem if (WANTTODUMP(dp)) {
158 1.17 lukem SETINO(ino, dumpinomap);
159 1.17 lukem if (mode != IFREG && mode != IFDIR && mode != IFLNK)
160 1.17 lukem *tapesize += 1;
161 1.17 lukem else
162 1.17 lukem *tapesize += blockest(dp);
163 1.17 lukem return;
164 1.17 lukem }
165 1.17 lukem if (mode == IFDIR)
166 1.17 lukem *dirskipped = 1;
167 1.17 lukem }
168 1.17 lukem
169 1.17 lukem /*
170 1.1 cgd * Dump pass 1.
171 1.1 cgd *
172 1.1 cgd * Walk the inode list for a filesystem to find all allocated inodes
173 1.1 cgd * that have been modified since the previous dump time. Also, find all
174 1.1 cgd * the directories in the filesystem.
175 1.1 cgd */
176 1.4 mycroft int
177 1.17 lukem mapfiles(maxino, tapesize, disk, dirv)
178 1.1 cgd ino_t maxino;
179 1.1 cgd long *tapesize;
180 1.17 lukem char *disk;
181 1.17 lukem char * const *dirv;
182 1.1 cgd {
183 1.1 cgd int anydirskipped = 0;
184 1.1 cgd
185 1.17 lukem if (dirv != NULL) {
186 1.17 lukem char curdir[MAXPATHLEN];
187 1.17 lukem FTS *dirh;
188 1.17 lukem FTSENT *entry;
189 1.17 lukem int d;
190 1.17 lukem
191 1.17 lukem if (getcwd(curdir, sizeof(curdir)) == NULL) {
192 1.17 lukem msg("Can't determine cwd: %s\n", strerror(errno));
193 1.17 lukem dumpabort(0);
194 1.17 lukem }
195 1.17 lukem if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
196 1.18 lukem NULL)) == NULL) {
197 1.17 lukem msg("fts_open failed: %s\n", strerror(errno));
198 1.17 lukem dumpabort(0);
199 1.17 lukem }
200 1.17 lukem while ((entry = fts_read(dirh)) != NULL) {
201 1.17 lukem switch (entry->fts_info) {
202 1.17 lukem case FTS_DNR: /* an error */
203 1.17 lukem case FTS_ERR:
204 1.17 lukem case FTS_NS:
205 1.17 lukem msg("Can't fts_read %s: %s\n", entry->fts_path,
206 1.17 lukem strerror(errno));
207 1.17 lukem case FTS_DP: /* already seen dir */
208 1.17 lukem continue;
209 1.17 lukem }
210 1.17 lukem mapfileino(entry->fts_statp->st_ino, tapesize,
211 1.17 lukem &anydirskipped);
212 1.17 lukem }
213 1.17 lukem (void)fts_close(dirh);
214 1.17 lukem
215 1.17 lukem /*
216 1.17 lukem * Add any parent directories
217 1.17 lukem */
218 1.17 lukem for (d = 0 ; dirv[d] != NULL ; d++) {
219 1.17 lukem char path[MAXPATHLEN];
220 1.17 lukem
221 1.17 lukem if (dirv[d][0] != '/')
222 1.17 lukem (void)snprintf(path, sizeof(path), "%s/%s",
223 1.17 lukem curdir, dirv[d]);
224 1.4 mycroft else
225 1.17 lukem (void)snprintf(path, sizeof(path), "%s",
226 1.17 lukem dirv[d]);
227 1.17 lukem while (strcmp(path, disk) != 0) {
228 1.17 lukem char *p;
229 1.17 lukem struct stat sb;
230 1.17 lukem
231 1.17 lukem if (*path == '\0')
232 1.17 lukem break;
233 1.17 lukem if ((p = strrchr(path, '/')) == NULL)
234 1.17 lukem break;
235 1.17 lukem if (p == path)
236 1.17 lukem break;
237 1.17 lukem *p = '\0';
238 1.17 lukem if (stat(path, &sb) == -1) {
239 1.17 lukem msg("Can't stat %s: %s\n", path,
240 1.17 lukem strerror(errno));
241 1.17 lukem break;
242 1.17 lukem }
243 1.17 lukem mapfileino(sb.st_ino, tapesize, &anydirskipped);
244 1.17 lukem }
245 1.17 lukem }
246 1.17 lukem
247 1.17 lukem /*
248 1.17 lukem * Ensure that the root inode actually appears in the
249 1.17 lukem * file list for a subdir
250 1.17 lukem */
251 1.17 lukem mapfileino(ROOTINO, tapesize, &anydirskipped);
252 1.17 lukem } else {
253 1.17 lukem ino_t ino;
254 1.17 lukem
255 1.17 lukem for (ino = ROOTINO; ino < maxino; ino++) {
256 1.17 lukem mapfileino(ino, tapesize, &anydirskipped);
257 1.1 cgd }
258 1.1 cgd }
259 1.1 cgd /*
260 1.1 cgd * Restore gets very upset if the root is not dumped,
261 1.1 cgd * so ensure that it always is dumped.
262 1.1 cgd */
263 1.1 cgd SETINO(ROOTINO, dumpinomap);
264 1.1 cgd return (anydirskipped);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /*
268 1.1 cgd * Dump pass 2.
269 1.1 cgd *
270 1.1 cgd * Scan each directory on the filesystem to see if it has any modified
271 1.1 cgd * files in it. If it does, and has not already been added to the dump
272 1.1 cgd * list (because it was itself modified), then add it. If a directory
273 1.1 cgd * has not been modified itself, contains no modified files and has no
274 1.1 cgd * subdirectories, then it can be deleted from the dump list and from
275 1.1 cgd * the list of directories. By deleting it from the list of directories,
276 1.1 cgd * its parent may now qualify for the same treatment on this or a later
277 1.1 cgd * pass using this algorithm.
278 1.1 cgd */
279 1.4 mycroft int
280 1.1 cgd mapdirs(maxino, tapesize)
281 1.1 cgd ino_t maxino;
282 1.1 cgd long *tapesize;
283 1.1 cgd {
284 1.16 lukem struct dinode *dp;
285 1.16 lukem int i, isdir;
286 1.16 lukem char *map;
287 1.16 lukem ino_t ino;
288 1.1 cgd long filesize;
289 1.1 cgd int ret, change = 0;
290 1.1 cgd
291 1.4 mycroft isdir = 0; /* XXX just to get gcc to shut up */
292 1.4 mycroft for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
293 1.4 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
294 1.1 cgd isdir = *map++;
295 1.1 cgd else
296 1.1 cgd isdir >>= 1;
297 1.1 cgd if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
298 1.1 cgd continue;
299 1.1 cgd dp = getino(ino);
300 1.1 cgd filesize = dp->di_size;
301 1.1 cgd for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
302 1.1 cgd if (dp->di_db[i] != 0)
303 1.1 cgd ret |= searchdir(ino, dp->di_db[i],
304 1.1 cgd (long)dblksize(sblock, dp, i),
305 1.1 cgd filesize);
306 1.1 cgd if (ret & HASDUMPEDFILE)
307 1.1 cgd filesize = 0;
308 1.1 cgd else
309 1.1 cgd filesize -= sblock->fs_bsize;
310 1.1 cgd }
311 1.1 cgd for (i = 0; filesize > 0 && i < NIADDR; i++) {
312 1.1 cgd if (dp->di_ib[i] == 0)
313 1.1 cgd continue;
314 1.1 cgd ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
315 1.1 cgd }
316 1.1 cgd if (ret & HASDUMPEDFILE) {
317 1.1 cgd SETINO(ino, dumpinomap);
318 1.1 cgd *tapesize += blockest(dp);
319 1.1 cgd change = 1;
320 1.1 cgd continue;
321 1.1 cgd }
322 1.1 cgd if ((ret & HASSUBDIRS) == 0) {
323 1.1 cgd if (!TSTINO(ino, dumpinomap)) {
324 1.1 cgd CLRINO(ino, dumpdirmap);
325 1.1 cgd change = 1;
326 1.1 cgd }
327 1.1 cgd }
328 1.1 cgd }
329 1.1 cgd return (change);
330 1.1 cgd }
331 1.1 cgd
332 1.1 cgd /*
333 1.1 cgd * Read indirect blocks, and pass the data blocks to be searched
334 1.1 cgd * as directories. Quit as soon as any entry is found that will
335 1.1 cgd * require the directory to be dumped.
336 1.1 cgd */
337 1.4 mycroft static int
338 1.1 cgd dirindir(ino, blkno, ind_level, filesize)
339 1.1 cgd ino_t ino;
340 1.1 cgd daddr_t blkno;
341 1.1 cgd int ind_level;
342 1.1 cgd long *filesize;
343 1.1 cgd {
344 1.1 cgd int ret = 0;
345 1.16 lukem int i;
346 1.1 cgd daddr_t idblk[MAXNINDIR];
347 1.1 cgd
348 1.1 cgd bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
349 1.1 cgd if (ind_level <= 0) {
350 1.1 cgd for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
351 1.1 cgd blkno = idblk[i];
352 1.1 cgd if (blkno != 0)
353 1.1 cgd ret |= searchdir(ino, blkno, sblock->fs_bsize,
354 1.1 cgd *filesize);
355 1.1 cgd if (ret & HASDUMPEDFILE)
356 1.1 cgd *filesize = 0;
357 1.1 cgd else
358 1.1 cgd *filesize -= sblock->fs_bsize;
359 1.1 cgd }
360 1.1 cgd return (ret);
361 1.1 cgd }
362 1.1 cgd ind_level--;
363 1.1 cgd for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
364 1.1 cgd blkno = idblk[i];
365 1.1 cgd if (blkno != 0)
366 1.1 cgd ret |= dirindir(ino, blkno, ind_level, filesize);
367 1.1 cgd }
368 1.1 cgd return (ret);
369 1.1 cgd }
370 1.1 cgd
371 1.1 cgd /*
372 1.1 cgd * Scan a disk block containing directory information looking to see if
373 1.1 cgd * any of the entries are on the dump list and to see if the directory
374 1.1 cgd * contains any subdirectories.
375 1.1 cgd */
376 1.4 mycroft static int
377 1.1 cgd searchdir(ino, blkno, size, filesize)
378 1.1 cgd ino_t ino;
379 1.1 cgd daddr_t blkno;
380 1.16 lukem long size;
381 1.1 cgd long filesize;
382 1.1 cgd {
383 1.16 lukem struct direct *dp;
384 1.16 lukem long loc, ret = 0;
385 1.1 cgd char dblk[MAXBSIZE];
386 1.1 cgd
387 1.1 cgd bread(fsbtodb(sblock, blkno), dblk, (int)size);
388 1.1 cgd if (filesize < size)
389 1.1 cgd size = filesize;
390 1.1 cgd for (loc = 0; loc < size; ) {
391 1.1 cgd dp = (struct direct *)(dblk + loc);
392 1.1 cgd if (dp->d_reclen == 0) {
393 1.1 cgd msg("corrupted directory, inumber %d\n", ino);
394 1.1 cgd break;
395 1.1 cgd }
396 1.1 cgd loc += dp->d_reclen;
397 1.1 cgd if (dp->d_ino == 0)
398 1.1 cgd continue;
399 1.1 cgd if (dp->d_name[0] == '.') {
400 1.1 cgd if (dp->d_name[1] == '\0')
401 1.1 cgd continue;
402 1.1 cgd if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
403 1.1 cgd continue;
404 1.1 cgd }
405 1.1 cgd if (TSTINO(dp->d_ino, dumpinomap)) {
406 1.1 cgd ret |= HASDUMPEDFILE;
407 1.1 cgd if (ret & HASSUBDIRS)
408 1.1 cgd break;
409 1.1 cgd }
410 1.1 cgd if (TSTINO(dp->d_ino, dumpdirmap)) {
411 1.1 cgd ret |= HASSUBDIRS;
412 1.1 cgd if (ret & HASDUMPEDFILE)
413 1.1 cgd break;
414 1.1 cgd }
415 1.1 cgd }
416 1.1 cgd return (ret);
417 1.1 cgd }
418 1.1 cgd
419 1.1 cgd /*
420 1.1 cgd * Dump passes 3 and 4.
421 1.1 cgd *
422 1.1 cgd * Dump the contents of an inode to tape.
423 1.1 cgd */
424 1.1 cgd void
425 1.1 cgd dumpino(dp, ino)
426 1.16 lukem struct dinode *dp;
427 1.1 cgd ino_t ino;
428 1.1 cgd {
429 1.1 cgd int ind_level, cnt;
430 1.4 mycroft fsizeT size;
431 1.1 cgd char buf[TP_BSIZE];
432 1.1 cgd
433 1.1 cgd if (newtape) {
434 1.1 cgd newtape = 0;
435 1.1 cgd dumpmap(dumpinomap, TS_BITS, ino);
436 1.1 cgd }
437 1.1 cgd CLRINO(ino, dumpinomap);
438 1.1 cgd spcl.c_dinode = *dp;
439 1.1 cgd spcl.c_type = TS_INODE;
440 1.1 cgd spcl.c_count = 0;
441 1.8 mycroft switch (dp->di_mode & IFMT) {
442 1.1 cgd
443 1.4 mycroft case 0:
444 1.1 cgd /*
445 1.1 cgd * Freed inode.
446 1.1 cgd */
447 1.1 cgd return;
448 1.1 cgd
449 1.8 mycroft case IFLNK:
450 1.1 cgd /*
451 1.1 cgd * Check for short symbolic link.
452 1.1 cgd */
453 1.5 mycroft if (dp->di_size > 0 &&
454 1.4 mycroft #ifdef FS_44INODEFMT
455 1.4 mycroft (dp->di_size < sblock->fs_maxsymlinklen ||
456 1.5 mycroft (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0))) {
457 1.5 mycroft #else
458 1.5 mycroft dp->di_blocks == 0) {
459 1.5 mycroft #endif
460 1.1 cgd spcl.c_addr[0] = 1;
461 1.1 cgd spcl.c_count = 1;
462 1.1 cgd writeheader(ino);
463 1.7 mycroft memcpy(buf, dp->di_shortlink, (u_long)dp->di_size);
464 1.1 cgd buf[dp->di_size] = '\0';
465 1.1 cgd writerec(buf, 0);
466 1.1 cgd return;
467 1.1 cgd }
468 1.1 cgd /* fall through */
469 1.1 cgd
470 1.8 mycroft case IFDIR:
471 1.8 mycroft case IFREG:
472 1.1 cgd if (dp->di_size > 0)
473 1.1 cgd break;
474 1.1 cgd /* fall through */
475 1.1 cgd
476 1.8 mycroft case IFIFO:
477 1.8 mycroft case IFSOCK:
478 1.8 mycroft case IFCHR:
479 1.8 mycroft case IFBLK:
480 1.1 cgd writeheader(ino);
481 1.1 cgd return;
482 1.1 cgd
483 1.1 cgd default:
484 1.8 mycroft msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
485 1.1 cgd return;
486 1.1 cgd }
487 1.1 cgd if (dp->di_size > NDADDR * sblock->fs_bsize)
488 1.1 cgd cnt = NDADDR * sblock->fs_frag;
489 1.1 cgd else
490 1.1 cgd cnt = howmany(dp->di_size, sblock->fs_fsize);
491 1.1 cgd blksout(&dp->di_db[0], cnt, ino);
492 1.1 cgd if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
493 1.1 cgd return;
494 1.1 cgd for (ind_level = 0; ind_level < NIADDR; ind_level++) {
495 1.1 cgd dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
496 1.1 cgd if (size <= 0)
497 1.1 cgd return;
498 1.1 cgd }
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd /*
502 1.1 cgd * Read indirect blocks, and pass the data blocks to be dumped.
503 1.1 cgd */
504 1.4 mycroft static void
505 1.1 cgd dmpindir(ino, blk, ind_level, size)
506 1.1 cgd ino_t ino;
507 1.1 cgd daddr_t blk;
508 1.1 cgd int ind_level;
509 1.4 mycroft fsizeT *size;
510 1.1 cgd {
511 1.1 cgd int i, cnt;
512 1.1 cgd daddr_t idblk[MAXNINDIR];
513 1.1 cgd
514 1.1 cgd if (blk != 0)
515 1.1 cgd bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
516 1.1 cgd else
517 1.7 mycroft memset(idblk, 0, (int)sblock->fs_bsize);
518 1.1 cgd if (ind_level <= 0) {
519 1.1 cgd if (*size < NINDIR(sblock) * sblock->fs_bsize)
520 1.1 cgd cnt = howmany(*size, sblock->fs_fsize);
521 1.1 cgd else
522 1.1 cgd cnt = NINDIR(sblock) * sblock->fs_frag;
523 1.1 cgd *size -= NINDIR(sblock) * sblock->fs_bsize;
524 1.1 cgd blksout(&idblk[0], cnt, ino);
525 1.1 cgd return;
526 1.1 cgd }
527 1.1 cgd ind_level--;
528 1.1 cgd for (i = 0; i < NINDIR(sblock); i++) {
529 1.1 cgd dmpindir(ino, idblk[i], ind_level, size);
530 1.1 cgd if (*size <= 0)
531 1.1 cgd return;
532 1.1 cgd }
533 1.1 cgd }
534 1.1 cgd
535 1.1 cgd /*
536 1.1 cgd * Collect up the data into tape record sized buffers and output them.
537 1.1 cgd */
538 1.1 cgd void
539 1.1 cgd blksout(blkp, frags, ino)
540 1.1 cgd daddr_t *blkp;
541 1.1 cgd int frags;
542 1.1 cgd ino_t ino;
543 1.1 cgd {
544 1.16 lukem daddr_t *bp;
545 1.1 cgd int i, j, count, blks, tbperdb;
546 1.1 cgd
547 1.1 cgd blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
548 1.1 cgd tbperdb = sblock->fs_bsize >> tp_bshift;
549 1.1 cgd for (i = 0; i < blks; i += TP_NINDIR) {
550 1.1 cgd if (i + TP_NINDIR > blks)
551 1.1 cgd count = blks;
552 1.1 cgd else
553 1.1 cgd count = i + TP_NINDIR;
554 1.1 cgd for (j = i; j < count; j++)
555 1.1 cgd if (blkp[j / tbperdb] != 0)
556 1.1 cgd spcl.c_addr[j - i] = 1;
557 1.1 cgd else
558 1.1 cgd spcl.c_addr[j - i] = 0;
559 1.1 cgd spcl.c_count = count - i;
560 1.1 cgd writeheader(ino);
561 1.1 cgd bp = &blkp[i / tbperdb];
562 1.1 cgd for (j = i; j < count; j += tbperdb, bp++)
563 1.1 cgd if (*bp != 0)
564 1.1 cgd if (j + tbperdb <= count)
565 1.1 cgd dumpblock(*bp, (int)sblock->fs_bsize);
566 1.1 cgd else
567 1.1 cgd dumpblock(*bp, (count - j) * TP_BSIZE);
568 1.1 cgd spcl.c_type = TS_ADDR;
569 1.1 cgd }
570 1.1 cgd }
571 1.1 cgd
572 1.1 cgd /*
573 1.1 cgd * Dump a map to the tape.
574 1.1 cgd */
575 1.1 cgd void
576 1.1 cgd dumpmap(map, type, ino)
577 1.1 cgd char *map;
578 1.1 cgd int type;
579 1.1 cgd ino_t ino;
580 1.1 cgd {
581 1.16 lukem int i;
582 1.1 cgd char *cp;
583 1.1 cgd
584 1.1 cgd spcl.c_type = type;
585 1.1 cgd spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
586 1.1 cgd writeheader(ino);
587 1.1 cgd for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
588 1.1 cgd writerec(cp, 0);
589 1.1 cgd }
590 1.1 cgd
591 1.1 cgd /*
592 1.1 cgd * Write a header record to the dump tape.
593 1.1 cgd */
594 1.1 cgd void
595 1.1 cgd writeheader(ino)
596 1.1 cgd ino_t ino;
597 1.1 cgd {
598 1.16 lukem int32_t sum, cnt, *lp;
599 1.1 cgd
600 1.1 cgd spcl.c_inumber = ino;
601 1.1 cgd spcl.c_magic = NFS_MAGIC;
602 1.1 cgd spcl.c_checksum = 0;
603 1.15 cgd lp = (int32_t *)&spcl;
604 1.1 cgd sum = 0;
605 1.15 cgd cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
606 1.1 cgd while (--cnt >= 0) {
607 1.1 cgd sum += *lp++;
608 1.1 cgd sum += *lp++;
609 1.1 cgd sum += *lp++;
610 1.1 cgd sum += *lp++;
611 1.1 cgd }
612 1.1 cgd spcl.c_checksum = CHECKSUM - sum;
613 1.1 cgd writerec((char *)&spcl, 1);
614 1.1 cgd }
615 1.1 cgd
616 1.1 cgd struct dinode *
617 1.1 cgd getino(inum)
618 1.1 cgd ino_t inum;
619 1.1 cgd {
620 1.1 cgd static daddr_t minino, maxino;
621 1.1 cgd static struct dinode inoblock[MAXINOPB];
622 1.1 cgd
623 1.1 cgd curino = inum;
624 1.1 cgd if (inum >= minino && inum < maxino)
625 1.1 cgd return (&inoblock[inum - minino]);
626 1.4 mycroft bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
627 1.1 cgd (int)sblock->fs_bsize);
628 1.1 cgd minino = inum - (inum % INOPB(sblock));
629 1.1 cgd maxino = minino + INOPB(sblock);
630 1.1 cgd return (&inoblock[inum - minino]);
631 1.1 cgd }
632 1.1 cgd
633 1.1 cgd /*
634 1.1 cgd * Read a chunk of data from the disk.
635 1.1 cgd * Try to recover from hard errors by reading in sector sized pieces.
636 1.1 cgd * Error recovery is attempted at most BREADEMAX times before seeking
637 1.1 cgd * consent from the operator to continue.
638 1.1 cgd */
639 1.1 cgd int breaderrors = 0;
640 1.1 cgd #define BREADEMAX 32
641 1.1 cgd
642 1.1 cgd void
643 1.1 cgd bread(blkno, buf, size)
644 1.1 cgd daddr_t blkno;
645 1.1 cgd char *buf;
646 1.1 cgd int size;
647 1.1 cgd {
648 1.1 cgd int cnt, i;
649 1.1 cgd extern int errno;
650 1.1 cgd
651 1.1 cgd loop:
652 1.10 mycroft if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
653 1.1 cgd msg("bread: lseek fails\n");
654 1.1 cgd if ((cnt = read(diskfd, buf, size)) == size)
655 1.1 cgd return;
656 1.1 cgd if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
657 1.1 cgd /*
658 1.1 cgd * Trying to read the final fragment.
659 1.1 cgd *
660 1.1 cgd * NB - dump only works in TP_BSIZE blocks, hence
661 1.1 cgd * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
662 1.1 cgd * It should be smarter about not actually trying to
663 1.1 cgd * read more than it can get, but for the time being
664 1.1 cgd * we punt and scale back the read only when it gets
665 1.1 cgd * us into trouble. (mkm 9/25/83)
666 1.1 cgd */
667 1.1 cgd size -= dev_bsize;
668 1.1 cgd goto loop;
669 1.1 cgd }
670 1.1 cgd if (cnt == -1)
671 1.1 cgd msg("read error from %s: %s: [block %d]: count=%d\n",
672 1.1 cgd disk, strerror(errno), blkno, size);
673 1.1 cgd else
674 1.1 cgd msg("short read error from %s: [block %d]: count=%d, got=%d\n",
675 1.1 cgd disk, blkno, size, cnt);
676 1.1 cgd if (++breaderrors > BREADEMAX) {
677 1.1 cgd msg("More than %d block read errors from %d\n",
678 1.1 cgd BREADEMAX, disk);
679 1.1 cgd broadcast("DUMP IS AILING!\n");
680 1.1 cgd msg("This is an unrecoverable error.\n");
681 1.1 cgd if (!query("Do you want to attempt to continue?")){
682 1.1 cgd dumpabort(0);
683 1.1 cgd /*NOTREACHED*/
684 1.1 cgd } else
685 1.1 cgd breaderrors = 0;
686 1.1 cgd }
687 1.1 cgd /*
688 1.1 cgd * Zero buffer, then try to read each sector of buffer separately.
689 1.1 cgd */
690 1.7 mycroft memset(buf, 0, size);
691 1.1 cgd for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
692 1.11 mycroft if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
693 1.1 cgd msg("bread: lseek2 fails!\n");
694 1.1 cgd if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
695 1.1 cgd continue;
696 1.1 cgd if (cnt == -1) {
697 1.1 cgd msg("read error from %s: %s: [sector %d]: count=%d\n",
698 1.1 cgd disk, strerror(errno), blkno, dev_bsize);
699 1.1 cgd continue;
700 1.1 cgd }
701 1.1 cgd msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
702 1.1 cgd disk, blkno, dev_bsize, cnt);
703 1.1 cgd }
704 1.1 cgd }
705