2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.6 (Berkeley) 5/1/95
35 * $FreeBSD: src/sbin/dump/main.c,v 1.20.2.9 2003/01/25 18:54:59 dillon Exp $
36 * $DragonFly: src/sbin/dump/main.c,v 1.6 2004/02/04 17:39:59 joerg Exp $
39 #include <sys/param.h>
43 #include <sys/vnode.h>
45 #include <ufs/inode.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/fs.h>
52 #include <protocols/dumprestore.h>
65 #include "pathnames.h"
68 #define SBOFF (SBLOCK * DEV_BSIZE)
71 int notify = 0; /* notify operator flag */
72 int blockswritten = 0; /* number of blocks written on current tape */
73 int tapeno = 0; /* current tape number */
74 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
75 int ntrec = NTREC; /* # tape blocks in each tape record */
76 int cartridge = 0; /* Assume non-cartridge tape */
77 int dokerberos = 0; /* Use Kerberos authentication */
78 int cachesize = 0; /* block cache size (in bytes) */
79 long dev_bsize = 1; /* recalculated below */
80 long blocksperfile; /* output blocks per file */
81 char *host = NULL; /* remote host (if any) */
83 static long numarg(char *, long, long);
84 static void obsolete(int *, char **[]);
85 static void usage(void);
88 main(int argc, char **argv)
93 register struct dinode *dp;
94 register struct fstab *dt;
97 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
98 int just_estimate = 0;
102 (void)time((time_t *)&spcl.c_date);
104 tsize = 0; /* Default later, based on 'c' option for cart tapes */
105 if ((tape = getenv("TAPE")) == NULL)
106 tape = _PATH_DEFTAPE;
107 dumpdates = _PATH_DUMPDATES;
109 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
110 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
116 obsolete(&argc, &argv);
118 #define optstring "0123456789aB:b:cd:f:h:kns:ST:uWwD:C:"
120 #define optstring "0123456789aB:b:cd:f:h:ns:ST:uWwD:C:"
122 while ((ch = getopt(argc, argv, optstring)) != -1)
126 case '0': case '1': case '2': case '3': case '4':
127 case '5': case '6': case '7': case '8': case '9':
131 case 'a': /* `auto-size', Write to EOM. */
135 case 'B': /* blocks per output file */
136 blocksperfile = numarg("number of blocks per file",
140 case 'b': /* blocks per tape write */
141 ntrec = numarg("number of blocks per write",
145 case 'c': /* Tape is cart. not 9-track */
149 case 'd': /* density, in bits per inch */
150 density = numarg("density", 10L, 327670L) / 10;
151 if (density >= 625 && !bflag)
152 ntrec = HIGHDENSITYTREC;
155 case 'f': /* output file */
164 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
168 honorlevel = numarg("honor level", 0L, 10L);
177 case 'n': /* notify operators */
181 case 's': /* tape size, feet */
182 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
185 case 'S': /* exit after estimating # of tapes */
189 case 'T': /* time of last dump */
190 spcl.c_ddate = unctime(optarg);
191 if (spcl.c_ddate < 0) {
192 (void)fprintf(stderr, "bad time \"%s\"\n",
200 case 'u': /* update /etc/dumpdates */
204 case 'W': /* what to do */
207 exit(X_FINOK); /* do nothing else */
216 (void)fprintf(stderr, "Must specify disk or filesystem\n");
222 (void)fprintf(stderr, "Unknown arguments to dump:");
224 (void)fprintf(stderr, " %s", *argv++);
225 (void)fprintf(stderr, "\n");
228 if (Tflag && uflag) {
229 (void)fprintf(stderr,
230 "You cannot use the T and u flags together.\n");
233 if (strcmp(tape, "-") == 0) {
235 tape = "standard output";
239 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
240 else if (!unlimited) {
242 * Determine how to default tape size and density
245 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
246 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
247 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
249 * hilit19 hits again: "
252 density = cartridge ? 100 : 160;
254 tsize = cartridge ? 1700L*120L : 2300L*120L;
257 if (strchr(tape, ':')) {
259 tape = strchr(host, ':');
262 if (index(tape, '\n')) {
263 (void)fprintf(stderr, "invalid characters in tape\n");
266 if (rmthost(host) == 0)
269 (void)fprintf(stderr, "remote dump not enabled\n");
273 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
275 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
277 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
278 signal(SIGTRAP, sig);
279 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
281 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
283 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
284 signal(SIGSEGV, sig);
285 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
286 signal(SIGTERM, sig);
287 if (signal(SIGINT, interrupt) == SIG_IGN)
288 signal(SIGINT, SIG_IGN);
290 getfstab(); /* /etc/fstab snarfed */
292 * disk can be either the full special file name,
293 * the suffix of the special file name,
294 * the special name missing the leading '/',
295 * the file system name with or without the leading '/'.
297 dt = fstabsearch(disk);
299 disk = rawname(dt->fs_spec);
300 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
301 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
303 (void)strncpy(spcl.c_dev, disk, NAMELEN);
304 (void)strncpy(spcl.c_filesys, "an unlisted file system",
307 spcl.c_dev[NAMELEN-1]='\0';
308 spcl.c_filesys[NAMELEN-1]='\0';
309 (void)strcpy(spcl.c_label, "none");
310 (void)gethostname(spcl.c_host, NAMELEN);
311 spcl.c_level = level - '0';
312 spcl.c_type = TS_TAPE;
314 getdumptime(); /* /etc/dumpdates snarfed */
316 msg("Date of this level %c dump: %s", level,
317 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
318 msg("Date of last level %c dump: %s", lastlevel,
319 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
320 msg("Dumping %s ", disk);
322 msgtail("(%s) ", dt->fs_file);
324 msgtail("to %s on host %s\n", tape, host);
326 msgtail("to %s\n", tape);
328 if ((diskfd = open(disk, O_RDONLY)) < 0)
329 err(X_STARTUP, "Cannot open %s", disk);
330 if (fstat(diskfd, &sb) != 0)
331 err(X_STARTUP, "%s: stat", disk);
332 if (S_ISDIR(sb.st_mode))
333 errx(X_STARTUP, "%s: unknown file system", disk);
335 sblock = (struct fs *)sblock_buf;
336 bread(SBOFF, (char *) sblock, SBSIZE);
337 if (sblock->fs_magic != FS_MAGIC)
338 quit("bad sblock magic number\n");
339 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
340 dev_bshift = ffs(dev_bsize) - 1;
341 if (dev_bsize != (1 << dev_bshift))
342 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
343 tp_bshift = ffs(TP_BSIZE) - 1;
344 if (TP_BSIZE != (1 << tp_bshift))
345 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
347 if (sblock->fs_inodefmt >= FS_44INODEFMT)
348 spcl.c_flags |= DR_NEWINODEFMT;
350 maxino = sblock->fs_ipg * sblock->fs_ncg;
351 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
352 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
353 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
354 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
355 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
357 nonodump = spcl.c_level < honorlevel;
360 setproctitle("%s: pass 1: regular files", disk);
361 msg("mapping (Pass I) [regular files]\n");
362 anydirskipped = mapfiles(maxino, &tapesize);
365 setproctitle("%s: pass 2: directories", disk);
366 msg("mapping (Pass II) [directories]\n");
367 while (anydirskipped) {
368 anydirskipped = mapdirs(maxino, &tapesize);
371 if (pipeout || unlimited) {
372 tapesize += 10; /* 10 trailer blocks */
373 msg("estimated %ld tape blocks.\n", tapesize);
378 fetapes = (double) tapesize / blocksperfile;
379 else if (cartridge) {
380 /* Estimate number of tapes, assuming streaming stops at
381 the end of each block written, and not in mid-block.
382 Assume no erroneous blocks; this can be compensated
383 for with an artificially low tape size. */
385 ( (double) tapesize /* blocks */
386 * TP_BSIZE /* bytes/block */
387 * (1.0/density) /* 0.1" / byte " */
389 (double) tapesize /* blocks */
390 * (1.0/ntrec) /* streaming-stops per block */
391 * 15.48 /* 0.1" / streaming-stop " */
392 ) * (1.0 / tsize ); /* tape / 0.1" " */
394 /* Estimate number of tapes, for old fashioned 9-track
396 int tenthsperirg = (density == 625) ? 3 : 7;
398 ( (double) tapesize /* blocks */
399 * TP_BSIZE /* bytes / block */
400 * (1.0/density) /* 0.1" / byte " */
402 (double) tapesize /* blocks */
403 * (1.0/ntrec) /* IRG's / block */
404 * tenthsperirg /* 0.1" / IRG " */
405 ) * (1.0 / tsize ); /* tape / 0.1" " */
407 etapes = fetapes; /* truncating assignment */
409 /* count the dumped inodes map on each additional tape */
410 tapesize += (etapes - 1) *
411 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
412 tapesize += etapes + 10; /* headers + 10 trailer blks */
413 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
418 * If the user only wants an estimate of the number of
425 * Allocate tape buffer.
429 "can't allocate tape buffers - try a smaller blocking factor.\n");
432 (void)time((time_t *)&(tstart_writing));
433 dumpmap(usedinomap, TS_CLRI, maxino - 1);
436 setproctitle("%s: pass 3: directories", disk);
437 msg("dumping (Pass III) [directories]\n");
438 dirty = 0; /* XXX just to get gcc to shut up */
439 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
440 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
444 if ((dirty & 1) == 0)
447 * Skip directory inodes deleted and maybe reallocated
450 if ((dp->di_mode & IFMT) != IFDIR)
452 (void)dumpino(dp, ino);
456 setproctitle("%s: pass 4: regular files", disk);
457 msg("dumping (Pass IV) [regular files]\n");
458 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
461 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
465 if ((dirty & 1) == 0)
468 * Skip inodes deleted and reallocated as directories.
471 mode = dp->di_mode & IFMT;
474 (void)dumpino(dp, ino);
478 spcl.c_type = TS_END;
479 for (i = 0; i < ntrec; i++)
480 writeheader(maxino - 1);
482 msg("DUMP: %ld tape blocks\n", (long)spcl.c_tapea);
484 msg("DUMP: %ld tape blocks on %d volume%s\n",
485 (long)spcl.c_tapea, spcl.c_volume,
486 (spcl.c_volume == 1) ? "" : "s");
488 /* report dump performance, avoid division through zero */
489 if (tend_writing - tstart_writing == 0)
490 msg("finished in less than a second\n");
492 msg("finished in %ld seconds, throughput %ld KBytes/sec\n",
493 (long)(tend_writing - tstart_writing),
494 spcl.c_tapea / (tend_writing - tstart_writing));
498 broadcast("DUMP IS DONE!\a\a\n");
499 msg("DUMP IS DONE\n");
508 "usage: dump [-0123456789ac"
512 "nSu] [-B records] [-b blocksize] [-D dumpdates]\n"
513 " [-d density] [-f file ] [-h level] [-s feet]\n"
514 " [-T date] [-C cachesizeMB] filesystem\n"
515 " dump [-W | -w]\n");
520 * Pick up a numeric argument. It must be nonnegative and in the given
521 * range (except that a vmax of 0 means unlimited).
524 numarg(char *meaning, long vmin, long vmax)
529 val = strtol(optarg, &p, 10);
531 errx(1, "illegal %s -- %s", meaning, optarg);
532 if (val < vmin || (vmax && val > vmax))
533 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
548 quit("Signal on pipe: cannot recover\n");
549 msg("Rewriting attempted as response to unknown signal.\n");
550 (void)fflush(stderr);
551 (void)fflush(stdout);
556 msg("SIGSEGV: ABORTING!\n");
557 (void)signal(SIGSEGV, SIG_DFL);
558 (void)kill(0, SIGSEGV);
566 static char rawbuf[MAXPATHLEN];
570 if (stat(cp, &sb) == 0) {
572 * If the name already refers to a raw device, return
573 * it immediately without tampering.
575 if ((sb.st_mode & S_IFMT) == S_IFCHR)
579 dp = strrchr(cp, '/');
584 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1);
585 rawbuf[MAXPATHLEN-1] = '\0';
587 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
588 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
594 * Change set of key letters and ordered arguments into something
595 * getopt(3) will like.
598 obsolete(int *argcp, char ***argvp)
601 char *ap, **argv, *flagsp, **nargv, *p;
607 /* Return if no arguments or first argument has leading dash. */
609 if (argc == 1 || *ap == '-')
612 /* Allocate space for new arguments. */
613 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
614 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
620 for (flags = 0; *ap; ++ap) {
632 warnx("option requires an argument -- %c", *ap);
635 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
639 (void)strcpy(&nargv[0][2], *argv);
653 /* Terminate flags. */
659 /* Copy remaining arguments. */
660 while ((*nargv++ = *argv++));
662 /* Update argument count. */
663 *argcp = nargv - *argvp - 1;