main.c revision 1.18 1 1.18 bouyer /* $NetBSD: main.c,v 1.18 1998/03/18 16:54:56 bouyer Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.3 mycroft * Copyright (c) 1980, 1991, 1993, 1994
5 1.3 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.15 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 1.15 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.6 cgd #if 0
44 1.16 lukem static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
45 1.6 cgd #else
46 1.18 bouyer __RCSID("$NetBSD: main.c,v 1.18 1998/03/18 16:54:56 bouyer Exp $");
47 1.6 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.3 mycroft #include <sys/param.h>
51 1.3 mycroft #include <sys/time.h>
52 1.14 lukem #include <sys/stat.h>
53 1.14 lukem #include <sys/mount.h>
54 1.14 lukem
55 1.1 cgd #ifdef sunos
56 1.3 mycroft #include <sys/vnode.h>
57 1.3 mycroft
58 1.3 mycroft #include <ufs/inode.h>
59 1.1 cgd #include <ufs/fs.h>
60 1.1 cgd #else
61 1.16 lukem #include <ufs/ufs/dinode.h>
62 1.3 mycroft #include <ufs/ffs/fs.h>
63 1.18 bouyer #include <ufs/ffs/ffs_extern.h>
64 1.1 cgd #endif
65 1.3 mycroft
66 1.1 cgd #include <protocols/dumprestore.h>
67 1.3 mycroft
68 1.3 mycroft #include <ctype.h>
69 1.5 mycroft #include <err.h>
70 1.3 mycroft #include <errno.h>
71 1.1 cgd #include <fcntl.h>
72 1.1 cgd #include <fstab.h>
73 1.3 mycroft #include <signal.h>
74 1.1 cgd #include <stdio.h>
75 1.1 cgd #include <stdlib.h>
76 1.1 cgd #include <string.h>
77 1.10 lukem #include <time.h>
78 1.3 mycroft #include <unistd.h>
79 1.3 mycroft
80 1.1 cgd #include "dump.h"
81 1.1 cgd #include "pathnames.h"
82 1.1 cgd
83 1.3 mycroft #ifndef SBOFF
84 1.3 mycroft #define SBOFF (SBLOCK * DEV_BSIZE)
85 1.3 mycroft #endif
86 1.3 mycroft
87 1.17 lukem int notify = 0; /* notify operator flag */
88 1.1 cgd int blockswritten = 0; /* number of blocks written on current tape */
89 1.17 lukem int tapeno = 0; /* current tape number */
90 1.17 lukem int density = 0; /* density in bytes/0.1" */
91 1.17 lukem int ntrec = NTREC; /* # tape blocks in each tape record */
92 1.17 lukem int cartridge = 0; /* Assume non-cartridge tape */
93 1.17 lukem long dev_bsize = 1; /* recalculated below */
94 1.17 lukem long blocksperfile = 0; /* output blocks per file */
95 1.17 lukem char *host = NULL; /* remote host (if any) */
96 1.1 cgd
97 1.15 lukem int main __P((int, char *[]));
98 1.5 mycroft static long numarg __P((char *, long, long));
99 1.5 mycroft static void obsolete __P((int *, char **[]));
100 1.5 mycroft static void usage __P((void));
101 1.3 mycroft
102 1.3 mycroft int
103 1.1 cgd main(argc, argv)
104 1.1 cgd int argc;
105 1.5 mycroft char *argv[];
106 1.1 cgd {
107 1.11 lukem ino_t ino;
108 1.11 lukem int dirty;
109 1.11 lukem struct dinode *dp;
110 1.11 lukem struct fstab *dt;
111 1.11 lukem char *map;
112 1.11 lukem int ch;
113 1.3 mycroft int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
114 1.1 cgd ino_t maxino;
115 1.18 bouyer time_t tnow, date;
116 1.14 lukem int dirlist;
117 1.14 lukem char *toplevel;
118 1.17 lukem int just_estimate = 0;
119 1.1 cgd
120 1.1 cgd spcl.c_date = 0;
121 1.3 mycroft (void)time((time_t *)&spcl.c_date);
122 1.1 cgd
123 1.1 cgd tsize = 0; /* Default later, based on 'c' option for cart tapes */
124 1.7 mrg if ((tape = getenv("TAPE")) == NULL)
125 1.7 mrg tape = _PATH_DEFTAPE;
126 1.1 cgd dumpdates = _PATH_DUMPDATES;
127 1.1 cgd temp = _PATH_DTMP;
128 1.1 cgd if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
129 1.1 cgd quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
130 1.1 cgd level = '0';
131 1.5 mycroft
132 1.5 mycroft if (argc < 2)
133 1.5 mycroft usage();
134 1.5 mycroft
135 1.5 mycroft obsolete(&argc, &argv);
136 1.17 lukem while ((ch = getopt(argc, argv,
137 1.17 lukem "0123456789B:b:cd:f:h:ns:ST:uWw")) != -1)
138 1.5 mycroft switch (ch) {
139 1.5 mycroft /* dump level */
140 1.5 mycroft case '0': case '1': case '2': case '3': case '4':
141 1.5 mycroft case '5': case '6': case '7': case '8': case '9':
142 1.5 mycroft level = ch;
143 1.3 mycroft break;
144 1.1 cgd
145 1.5 mycroft case 'B': /* blocks per output file */
146 1.5 mycroft blocksperfile = numarg("blocks per file", 1L, 0L);
147 1.5 mycroft break;
148 1.1 cgd
149 1.5 mycroft case 'b': /* blocks per tape write */
150 1.5 mycroft ntrec = numarg("blocks per write", 1L, 1000L);
151 1.9 mikel bflag = 1;
152 1.5 mycroft break;
153 1.1 cgd
154 1.5 mycroft case 'c': /* Tape is cart. not 9-track */
155 1.5 mycroft cartridge = 1;
156 1.3 mycroft break;
157 1.1 cgd
158 1.1 cgd case 'd': /* density, in bits per inch */
159 1.5 mycroft density = numarg("density", 10L, 327670L) / 10;
160 1.1 cgd if (density >= 625 && !bflag)
161 1.1 cgd ntrec = HIGHDENSITYTREC;
162 1.3 mycroft break;
163 1.1 cgd
164 1.5 mycroft case 'f': /* output file */
165 1.5 mycroft tape = optarg;
166 1.5 mycroft break;
167 1.5 mycroft
168 1.5 mycroft case 'h':
169 1.5 mycroft honorlevel = numarg("honor level", 0L, 10L);
170 1.5 mycroft break;
171 1.5 mycroft
172 1.5 mycroft case 'n': /* notify operators */
173 1.5 mycroft notify = 1;
174 1.5 mycroft break;
175 1.5 mycroft
176 1.1 cgd case 's': /* tape size, feet */
177 1.5 mycroft tsize = numarg("tape size", 1L, 0L) * 12 * 10;
178 1.3 mycroft break;
179 1.1 cgd
180 1.17 lukem case 'S': /* exit after estimating # of tapes */
181 1.17 lukem just_estimate = 1;
182 1.17 lukem break;
183 1.17 lukem
184 1.1 cgd case 'T': /* time of last dump */
185 1.5 mycroft spcl.c_ddate = unctime(optarg);
186 1.1 cgd if (spcl.c_ddate < 0) {
187 1.3 mycroft (void)fprintf(stderr, "bad time \"%s\"\n",
188 1.5 mycroft optarg);
189 1.3 mycroft exit(X_ABORT);
190 1.1 cgd }
191 1.3 mycroft Tflag = 1;
192 1.1 cgd lastlevel = '?';
193 1.3 mycroft break;
194 1.1 cgd
195 1.1 cgd case 'u': /* update /etc/dumpdates */
196 1.3 mycroft uflag = 1;
197 1.3 mycroft break;
198 1.1 cgd
199 1.5 mycroft case 'W': /* what to do */
200 1.5 mycroft case 'w':
201 1.5 mycroft lastdump(ch);
202 1.5 mycroft exit(0); /* do nothing else */
203 1.1 cgd
204 1.1 cgd default:
205 1.5 mycroft usage();
206 1.1 cgd }
207 1.5 mycroft argc -= optind;
208 1.5 mycroft argv += optind;
209 1.5 mycroft
210 1.1 cgd if (argc < 1) {
211 1.3 mycroft (void)fprintf(stderr, "Must specify disk or filesystem\n");
212 1.3 mycroft exit(X_ABORT);
213 1.1 cgd }
214 1.14 lukem
215 1.14 lukem /*
216 1.14 lukem * determine if disk is a subdirectory, and setup appropriately
217 1.14 lukem */
218 1.14 lukem dirlist = 0;
219 1.14 lukem toplevel = NULL;
220 1.14 lukem for (i = 0; i < argc; i++) {
221 1.14 lukem struct stat sb;
222 1.14 lukem struct statfs fsbuf;
223 1.14 lukem
224 1.14 lukem if (lstat(argv[i], &sb) == -1) {
225 1.14 lukem msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
226 1.14 lukem exit(X_ABORT);
227 1.14 lukem }
228 1.14 lukem if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
229 1.14 lukem break;
230 1.14 lukem if (statfs(argv[i], &fsbuf) == -1) {
231 1.14 lukem msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
232 1.14 lukem exit(X_ABORT);
233 1.14 lukem }
234 1.14 lukem if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
235 1.14 lukem if (dirlist != 0) {
236 1.14 lukem msg("Can't dump a mountpoint and a filelist\n");
237 1.14 lukem exit(X_ABORT);
238 1.14 lukem }
239 1.14 lukem break; /* exit if sole mountpoint */
240 1.14 lukem }
241 1.14 lukem if (!disk) {
242 1.14 lukem if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
243 1.14 lukem msg("Cannot malloc diskname\n");
244 1.14 lukem exit(X_ABORT);
245 1.14 lukem }
246 1.14 lukem disk = toplevel;
247 1.14 lukem if (uflag) {
248 1.14 lukem msg("Ignoring u flag for subdir dump\n");
249 1.14 lukem uflag = 0;
250 1.14 lukem }
251 1.14 lukem if (level > '0') {
252 1.14 lukem msg("Subdir dump is done at level 0\n");
253 1.14 lukem level = '0';
254 1.14 lukem }
255 1.14 lukem msg("Dumping sub files/directories from %s\n", disk);
256 1.14 lukem } else {
257 1.14 lukem if (strcmp(disk, fsbuf.f_mntonname) != 0) {
258 1.14 lukem msg("%s is not on %s\n", argv[i], disk);
259 1.14 lukem exit(X_ABORT);
260 1.14 lukem }
261 1.14 lukem }
262 1.14 lukem msg("Dumping file/directory %s\n", argv[i]);
263 1.14 lukem dirlist++;
264 1.14 lukem }
265 1.14 lukem if (dirlist == 0) {
266 1.14 lukem disk = *argv++;
267 1.14 lukem if (argc != 1) {
268 1.14 lukem (void)fprintf(stderr, "Excess arguments to dump:");
269 1.14 lukem while (--argc)
270 1.14 lukem (void)fprintf(stderr, " %s", *argv++);
271 1.14 lukem (void)fprintf(stderr, "\n");
272 1.14 lukem exit(X_ABORT);
273 1.14 lukem }
274 1.1 cgd }
275 1.1 cgd if (Tflag && uflag) {
276 1.3 mycroft (void)fprintf(stderr,
277 1.1 cgd "You cannot use the T and u flags together.\n");
278 1.3 mycroft exit(X_ABORT);
279 1.1 cgd }
280 1.1 cgd if (strcmp(tape, "-") == 0) {
281 1.1 cgd pipeout++;
282 1.1 cgd tape = "standard output";
283 1.1 cgd }
284 1.1 cgd
285 1.1 cgd if (blocksperfile)
286 1.1 cgd blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
287 1.1 cgd else {
288 1.1 cgd /*
289 1.1 cgd * Determine how to default tape size and density
290 1.1 cgd *
291 1.1 cgd * density tape size
292 1.1 cgd * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
293 1.1 cgd * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
294 1.1 cgd * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
295 1.1 cgd * (450*4 - slop)
296 1.1 cgd */
297 1.1 cgd if (density == 0)
298 1.1 cgd density = cartridge ? 100 : 160;
299 1.1 cgd if (tsize == 0)
300 1.1 cgd tsize = cartridge ? 1700L*120L : 2300L*120L;
301 1.1 cgd }
302 1.1 cgd
303 1.4 mycroft if (strchr(tape, ':')) {
304 1.1 cgd host = tape;
305 1.4 mycroft tape = strchr(host, ':');
306 1.3 mycroft *tape++ = '\0';
307 1.1 cgd #ifdef RDUMP
308 1.1 cgd if (rmthost(host) == 0)
309 1.3 mycroft exit(X_ABORT);
310 1.1 cgd #else
311 1.3 mycroft (void)fprintf(stderr, "remote dump not enabled\n");
312 1.3 mycroft exit(X_ABORT);
313 1.1 cgd #endif
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
317 1.1 cgd signal(SIGHUP, sig);
318 1.1 cgd if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
319 1.1 cgd signal(SIGTRAP, sig);
320 1.1 cgd if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
321 1.1 cgd signal(SIGFPE, sig);
322 1.1 cgd if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
323 1.1 cgd signal(SIGBUS, sig);
324 1.1 cgd if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
325 1.1 cgd signal(SIGSEGV, sig);
326 1.1 cgd if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
327 1.1 cgd signal(SIGTERM, sig);
328 1.1 cgd if (signal(SIGINT, interrupt) == SIG_IGN)
329 1.1 cgd signal(SIGINT, SIG_IGN);
330 1.1 cgd
331 1.1 cgd set_operators(); /* /etc/group snarfed */
332 1.1 cgd getfstab(); /* /etc/fstab snarfed */
333 1.14 lukem
334 1.1 cgd /*
335 1.1 cgd * disk can be either the full special file name,
336 1.1 cgd * the suffix of the special file name,
337 1.1 cgd * the special name missing the leading '/',
338 1.1 cgd * the file system name with or without the leading '/'.
339 1.1 cgd */
340 1.1 cgd dt = fstabsearch(disk);
341 1.3 mycroft if (dt != NULL) {
342 1.1 cgd disk = rawname(dt->fs_spec);
343 1.3 mycroft (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
344 1.14 lukem if (dirlist != 0)
345 1.14 lukem (void)snprintf(spcl.c_filesys, NAMELEN,
346 1.14 lukem "a subset of %s", dt->fs_file);
347 1.14 lukem else
348 1.14 lukem (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
349 1.1 cgd } else {
350 1.3 mycroft (void)strncpy(spcl.c_dev, disk, NAMELEN);
351 1.3 mycroft (void)strncpy(spcl.c_filesys, "an unlisted file system",
352 1.1 cgd NAMELEN);
353 1.1 cgd }
354 1.12 mrg (void)strncpy(spcl.c_label, "none", sizeof(spcl.c_label) - 1);
355 1.3 mycroft (void)gethostname(spcl.c_host, NAMELEN);
356 1.18 bouyer
357 1.18 bouyer if ((diskfd = open(disk, O_RDONLY)) < 0) {
358 1.18 bouyer msg("Cannot open %s\n", disk);
359 1.18 bouyer exit(X_ABORT);
360 1.18 bouyer }
361 1.18 bouyer sync();
362 1.18 bouyer sblock = (struct fs *)sblock_buf;
363 1.18 bouyer bread(SBOFF, (char *) sblock, SBSIZE);
364 1.18 bouyer if (sblock->fs_magic != FS_MAGIC)
365 1.18 bouyer if (sblock->fs_magic == bswap32(FS_MAGIC)) {
366 1.18 bouyer ffs_sb_swap(sblock, sblock, 0);
367 1.18 bouyer needswap = 1;
368 1.18 bouyer } else
369 1.18 bouyer quit("bad sblock magic number\n");
370 1.18 bouyer
371 1.18 bouyer spcl.c_level = iswap32(level - '0');
372 1.18 bouyer spcl.c_type = iswap32(TS_TAPE);
373 1.18 bouyer spcl.c_date = iswap32(spcl.c_date);
374 1.18 bouyer spcl.c_ddate = iswap32(spcl.c_ddate);
375 1.1 cgd if (!Tflag)
376 1.1 cgd getdumptime(); /* /etc/dumpdates snarfed */
377 1.1 cgd
378 1.18 bouyer date = iswap32(spcl.c_date);
379 1.1 cgd msg("Date of this level %c dump: %s", level,
380 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
381 1.18 bouyer date = iswap32(spcl.c_ddate);
382 1.1 cgd msg("Date of last level %c dump: %s", lastlevel,
383 1.18 bouyer spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
384 1.17 lukem msg("Dumping ");
385 1.17 lukem if (dt != NULL && dirlist != 0)
386 1.17 lukem msgtail("a subset of ");
387 1.17 lukem msgtail("%s (%s) ", disk, spcl.c_filesys);
388 1.1 cgd if (host)
389 1.1 cgd msgtail("to %s on host %s\n", tape, host);
390 1.1 cgd else
391 1.1 cgd msgtail("to %s\n", tape);
392 1.1 cgd
393 1.1 cgd dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
394 1.1 cgd dev_bshift = ffs(dev_bsize) - 1;
395 1.1 cgd if (dev_bsize != (1 << dev_bshift))
396 1.1 cgd quit("dev_bsize (%d) is not a power of 2", dev_bsize);
397 1.1 cgd tp_bshift = ffs(TP_BSIZE) - 1;
398 1.1 cgd if (TP_BSIZE != (1 << tp_bshift))
399 1.1 cgd quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
400 1.3 mycroft #ifdef FS_44INODEFMT
401 1.1 cgd if (sblock->fs_inodefmt >= FS_44INODEFMT)
402 1.18 bouyer spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWINODEFMT);
403 1.1 cgd #endif
404 1.3 mycroft maxino = sblock->fs_ipg * sblock->fs_ncg;
405 1.3 mycroft mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
406 1.1 cgd usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
407 1.1 cgd dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
408 1.1 cgd dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
409 1.1 cgd tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
410 1.1 cgd
411 1.18 bouyer nonodump = iswap32(spcl.c_level) < honorlevel;
412 1.3 mycroft
413 1.14 lukem (void)signal(SIGINFO, statussig);
414 1.14 lukem
415 1.1 cgd msg("mapping (Pass I) [regular files]\n");
416 1.14 lukem anydirskipped = mapfiles(maxino, &tapesize, toplevel,
417 1.14 lukem (dirlist ? argv : NULL));
418 1.1 cgd
419 1.1 cgd msg("mapping (Pass II) [directories]\n");
420 1.1 cgd while (anydirskipped) {
421 1.1 cgd anydirskipped = mapdirs(maxino, &tapesize);
422 1.1 cgd }
423 1.1 cgd
424 1.3 mycroft if (pipeout) {
425 1.1 cgd tapesize += 10; /* 10 trailer blocks */
426 1.3 mycroft msg("estimated %ld tape blocks.\n", tapesize);
427 1.3 mycroft } else {
428 1.3 mycroft double fetapes;
429 1.3 mycroft
430 1.1 cgd if (blocksperfile)
431 1.3 mycroft fetapes = (double) tapesize / blocksperfile;
432 1.1 cgd else if (cartridge) {
433 1.1 cgd /* Estimate number of tapes, assuming streaming stops at
434 1.1 cgd the end of each block written, and not in mid-block.
435 1.1 cgd Assume no erroneous blocks; this can be compensated
436 1.1 cgd for with an artificially low tape size. */
437 1.1 cgd fetapes =
438 1.1 cgd ( tapesize /* blocks */
439 1.1 cgd * TP_BSIZE /* bytes/block */
440 1.1 cgd * (1.0/density) /* 0.1" / byte */
441 1.1 cgd +
442 1.1 cgd tapesize /* blocks */
443 1.1 cgd * (1.0/ntrec) /* streaming-stops per block */
444 1.1 cgd * 15.48 /* 0.1" / streaming-stop */
445 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
446 1.1 cgd } else {
447 1.1 cgd /* Estimate number of tapes, for old fashioned 9-track
448 1.1 cgd tape */
449 1.1 cgd int tenthsperirg = (density == 625) ? 3 : 7;
450 1.1 cgd fetapes =
451 1.1 cgd ( tapesize /* blocks */
452 1.1 cgd * TP_BSIZE /* bytes / block */
453 1.1 cgd * (1.0/density) /* 0.1" / byte */
454 1.1 cgd +
455 1.1 cgd tapesize /* blocks */
456 1.1 cgd * (1.0/ntrec) /* IRG's / block */
457 1.1 cgd * tenthsperirg /* 0.1" / IRG */
458 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
459 1.1 cgd }
460 1.1 cgd etapes = fetapes; /* truncating assignment */
461 1.1 cgd etapes++;
462 1.1 cgd /* count the dumped inodes map on each additional tape */
463 1.1 cgd tapesize += (etapes - 1) *
464 1.1 cgd (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
465 1.1 cgd tapesize += etapes + 10; /* headers + 10 trailer blks */
466 1.1 cgd msg("estimated %ld tape blocks on %3.2f tape(s).\n",
467 1.1 cgd tapesize, fetapes);
468 1.3 mycroft }
469 1.17 lukem /*
470 1.17 lukem * If the user only wants an estimate of the number of
471 1.17 lukem * tapes, exit now.
472 1.17 lukem */
473 1.17 lukem if (just_estimate)
474 1.17 lukem exit(0);
475 1.1 cgd
476 1.1 cgd /*
477 1.3 mycroft * Allocate tape buffer.
478 1.1 cgd */
479 1.1 cgd if (!alloctape())
480 1.1 cgd quit("can't allocate tape buffers - try a smaller blocking factor.\n");
481 1.1 cgd
482 1.1 cgd startnewtape(1);
483 1.3 mycroft (void)time((time_t *)&(tstart_writing));
484 1.10 lukem xferrate = 0;
485 1.3 mycroft dumpmap(usedinomap, TS_CLRI, maxino - 1);
486 1.1 cgd
487 1.1 cgd msg("dumping (Pass III) [directories]\n");
488 1.3 mycroft dirty = 0; /* XXX just to get gcc to shut up */
489 1.3 mycroft for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
490 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
491 1.1 cgd dirty = *map++;
492 1.1 cgd else
493 1.1 cgd dirty >>= 1;
494 1.1 cgd if ((dirty & 1) == 0)
495 1.1 cgd continue;
496 1.1 cgd /*
497 1.1 cgd * Skip directory inodes deleted and maybe reallocated
498 1.1 cgd */
499 1.1 cgd dp = getino(ino);
500 1.1 cgd if ((dp->di_mode & IFMT) != IFDIR)
501 1.1 cgd continue;
502 1.3 mycroft (void)dumpino(dp, ino);
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd msg("dumping (Pass IV) [regular files]\n");
506 1.3 mycroft for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
507 1.3 mycroft int mode;
508 1.3 mycroft
509 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
510 1.1 cgd dirty = *map++;
511 1.1 cgd else
512 1.1 cgd dirty >>= 1;
513 1.1 cgd if ((dirty & 1) == 0)
514 1.1 cgd continue;
515 1.1 cgd /*
516 1.1 cgd * Skip inodes deleted and reallocated as directories.
517 1.1 cgd */
518 1.1 cgd dp = getino(ino);
519 1.3 mycroft mode = dp->di_mode & IFMT;
520 1.3 mycroft if (mode == IFDIR)
521 1.1 cgd continue;
522 1.3 mycroft (void)dumpino(dp, ino);
523 1.1 cgd }
524 1.1 cgd
525 1.18 bouyer spcl.c_type = iswap32(TS_END);
526 1.1 cgd for (i = 0; i < ntrec; i++)
527 1.3 mycroft writeheader(maxino - 1);
528 1.1 cgd if (pipeout)
529 1.18 bouyer msg("%ld tape blocks\n",iswap32(spcl.c_tapea));
530 1.1 cgd else
531 1.17 lukem msg("%ld tape blocks on %d volume%s\n",
532 1.18 bouyer iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
533 1.18 bouyer (iswap32(spcl.c_volume) == 1) ? "" : "s");
534 1.10 lukem tnow = do_stats();
535 1.18 bouyer date = iswap32(spcl.c_date);
536 1.10 lukem msg("Date of this level %c dump: %s", level,
537 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
538 1.10 lukem msg("Date this dump completed: %s", ctime(&tnow));
539 1.14 lukem msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
540 1.1 cgd putdumptime();
541 1.1 cgd trewind();
542 1.1 cgd broadcast("DUMP IS DONE!\7\7\n");
543 1.1 cgd msg("DUMP IS DONE\n");
544 1.1 cgd Exit(X_FINOK);
545 1.1 cgd /* NOTREACHED */
546 1.15 lukem exit(X_FINOK); /* XXX: to satisfy gcc */
547 1.1 cgd }
548 1.1 cgd
549 1.5 mycroft static void
550 1.5 mycroft usage()
551 1.5 mycroft {
552 1.5 mycroft
553 1.5 mycroft (void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n [-h level] [-s feet] [-T date] filesystem\n");
554 1.5 mycroft (void)fprintf(stderr, " dump [-W | -w]\n");
555 1.5 mycroft exit(1);
556 1.5 mycroft }
557 1.5 mycroft
558 1.3 mycroft /*
559 1.3 mycroft * Pick up a numeric argument. It must be nonnegative and in the given
560 1.3 mycroft * range (except that a vmax of 0 means unlimited).
561 1.3 mycroft */
562 1.3 mycroft static long
563 1.5 mycroft numarg(meaning, vmin, vmax)
564 1.3 mycroft char *meaning;
565 1.3 mycroft long vmin, vmax;
566 1.3 mycroft {
567 1.5 mycroft char *p;
568 1.3 mycroft long val;
569 1.3 mycroft
570 1.5 mycroft val = strtol(optarg, &p, 10);
571 1.5 mycroft if (*p)
572 1.5 mycroft errx(1, "illegal %s -- %s", meaning, optarg);
573 1.3 mycroft if (val < vmin || (vmax && val > vmax))
574 1.5 mycroft errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
575 1.3 mycroft return (val);
576 1.3 mycroft }
577 1.3 mycroft
578 1.1 cgd void
579 1.1 cgd sig(signo)
580 1.1 cgd int signo;
581 1.1 cgd {
582 1.1 cgd switch(signo) {
583 1.1 cgd case SIGALRM:
584 1.1 cgd case SIGBUS:
585 1.1 cgd case SIGFPE:
586 1.1 cgd case SIGHUP:
587 1.1 cgd case SIGTERM:
588 1.1 cgd case SIGTRAP:
589 1.1 cgd if (pipeout)
590 1.1 cgd quit("Signal on pipe: cannot recover\n");
591 1.1 cgd msg("Rewriting attempted as response to unknown signal.\n");
592 1.3 mycroft (void)fflush(stderr);
593 1.3 mycroft (void)fflush(stdout);
594 1.1 cgd close_rewind();
595 1.3 mycroft exit(X_REWRITE);
596 1.1 cgd /* NOTREACHED */
597 1.1 cgd case SIGSEGV:
598 1.1 cgd msg("SIGSEGV: ABORTING!\n");
599 1.3 mycroft (void)signal(SIGSEGV, SIG_DFL);
600 1.3 mycroft (void)kill(0, SIGSEGV);
601 1.1 cgd /* NOTREACHED */
602 1.1 cgd }
603 1.1 cgd }
604 1.1 cgd
605 1.1 cgd char *
606 1.1 cgd rawname(cp)
607 1.1 cgd char *cp;
608 1.1 cgd {
609 1.1 cgd static char rawbuf[MAXPATHLEN];
610 1.4 mycroft char *dp = strrchr(cp, '/');
611 1.1 cgd
612 1.3 mycroft if (dp == NULL)
613 1.3 mycroft return (NULL);
614 1.3 mycroft *dp = '\0';
615 1.12 mrg (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
616 1.1 cgd *dp = '/';
617 1.1 cgd return (rawbuf);
618 1.1 cgd }
619 1.1 cgd
620 1.5 mycroft /*
621 1.5 mycroft * obsolete --
622 1.5 mycroft * Change set of key letters and ordered arguments into something
623 1.5 mycroft * getopt(3) will like.
624 1.5 mycroft */
625 1.5 mycroft static void
626 1.5 mycroft obsolete(argcp, argvp)
627 1.5 mycroft int *argcp;
628 1.5 mycroft char **argvp[];
629 1.1 cgd {
630 1.5 mycroft int argc, flags;
631 1.5 mycroft char *ap, **argv, *flagsp, **nargv, *p;
632 1.5 mycroft
633 1.5 mycroft /* Setup. */
634 1.5 mycroft argv = *argvp;
635 1.5 mycroft argc = *argcp;
636 1.5 mycroft
637 1.5 mycroft /* Return if no arguments or first argument has leading dash. */
638 1.5 mycroft ap = argv[1];
639 1.5 mycroft if (argc == 1 || *ap == '-')
640 1.5 mycroft return;
641 1.5 mycroft
642 1.5 mycroft /* Allocate space for new arguments. */
643 1.5 mycroft if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
644 1.5 mycroft (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
645 1.14 lukem err(1, "malloc");
646 1.5 mycroft
647 1.5 mycroft *nargv++ = *argv;
648 1.5 mycroft argv += 2;
649 1.5 mycroft
650 1.5 mycroft for (flags = 0; *ap; ++ap) {
651 1.5 mycroft switch (*ap) {
652 1.5 mycroft case 'B':
653 1.5 mycroft case 'b':
654 1.5 mycroft case 'd':
655 1.5 mycroft case 'f':
656 1.5 mycroft case 'h':
657 1.5 mycroft case 's':
658 1.5 mycroft case 'T':
659 1.5 mycroft if (*argv == NULL) {
660 1.5 mycroft warnx("option requires an argument -- %c", *ap);
661 1.5 mycroft usage();
662 1.5 mycroft }
663 1.5 mycroft if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
664 1.14 lukem err(1, "malloc");
665 1.5 mycroft nargv[0][0] = '-';
666 1.5 mycroft nargv[0][1] = *ap;
667 1.17 lukem (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
668 1.5 mycroft ++argv;
669 1.5 mycroft ++nargv;
670 1.5 mycroft break;
671 1.5 mycroft default:
672 1.5 mycroft if (!flags) {
673 1.5 mycroft *p++ = '-';
674 1.5 mycroft flags = 1;
675 1.5 mycroft }
676 1.5 mycroft *p++ = *ap;
677 1.5 mycroft break;
678 1.5 mycroft }
679 1.5 mycroft }
680 1.1 cgd
681 1.5 mycroft /* Terminate flags. */
682 1.5 mycroft if (flags) {
683 1.5 mycroft *p = '\0';
684 1.5 mycroft *nargv++ = flagsp;
685 1.5 mycroft }
686 1.5 mycroft
687 1.5 mycroft /* Copy remaining arguments. */
688 1.14 lukem while ((*nargv++ = *argv++) != NULL)
689 1.14 lukem ;
690 1.5 mycroft
691 1.5 mycroft /* Update argument count. */
692 1.5 mycroft *argcp = nargv - *argvp - 1;
693 1.1 cgd }
694