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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
40 static const char rcsid[] =
44 #include <sys/param.h>
47 #include <sys/mount.h>
48 #include <sys/disklabel.h>
50 #include <ufs/ufs/dinode.h>
51 #include <ufs/ufs/ufsmount.h>
52 #include <ufs/ffs/fs.h>
54 #include <protocols/dumprestore.h>
71 #include "pathnames.h"
73 int notify = 0; /* notify operator flag */
74 int snapdump = 0; /* dumping live filesystem, so use snapshot */
75 int blockswritten = 0; /* number of blocks written on current tape */
76 int tapeno = 0; /* current tape number */
77 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
78 int ntrec = NTREC; /* # tape blocks in each tape record */
79 int cartridge = 0; /* Assume non-cartridge tape */
80 int cachesize = 0; /* block cache size (in bytes), defaults to 0 */
81 long dev_bsize = 1; /* recalculated below */
82 long blocksperfile; /* output blocks per file */
83 char *host = NULL; /* remote host (if any) */
86 * Possible superblock locations ordered from most to least likely.
88 static int sblock_try[] = SBLOCKSEARCH;
90 static char *getmntpt(char *, int *);
91 static long numarg(const char *, long, long);
92 static void obsolete(int *, char **[]);
93 static void usage(void) __dead2;
96 main(int argc, char *argv[])
104 int ch, mode, mntflags;
105 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
106 int just_estimate = 0;
110 spcl.c_date = _time_to_time64(time(NULL));
112 tsize = 0; /* Default later, based on 'c' option for cart tapes */
113 dumpdates = _PATH_DUMPDATES;
117 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
118 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
124 obsolete(&argc, &argv);
125 while ((ch = getopt(argc, argv,
126 "0123456789aB:b:C:cD:d:f:h:LnP:Ss:T:uWw")) != -1)
129 case '0': case '1': case '2': case '3': case '4':
130 case '5': case '6': case '7': case '8': case '9':
134 case 'a': /* `auto-size', Write to EOM. */
138 case 'B': /* blocks per output file */
139 blocksperfile = numarg("number of blocks per file",
143 case 'b': /* blocks per tape write */
144 ntrec = numarg("number of blocks per write",
149 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
152 case 'c': /* Tape is cart. not 9-track */
160 case 'd': /* density, in bits per inch */
161 density = numarg("density", 10L, 327670L) / 10;
162 if (density >= 625 && !bflag)
163 ntrec = HIGHDENSITYTREC;
166 case 'f': /* output file */
167 if (popenout != NULL)
168 errx(X_STARTUP, "You cannot use the P and f "
169 "flags together.\n");
174 honorlevel = numarg("honor level", 0L, 10L);
181 case 'n': /* notify operators */
187 errx(X_STARTUP, "You cannot use the P and f "
188 "flags together.\n");
192 case 'S': /* exit after estimating # of tapes */
196 case 's': /* tape size, feet */
197 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
200 case 'T': /* time of last dump */
201 spcl.c_ddate = unctime(optarg);
202 if (spcl.c_ddate < 0) {
203 (void)fprintf(stderr, "bad time \"%s\"\n",
211 case 'u': /* update /etc/dumpdates */
215 case 'W': /* what to do */
218 exit(X_FINOK); /* do nothing else */
227 (void)fprintf(stderr, "Must specify disk or file system\n");
233 (void)fprintf(stderr, "Unknown arguments to dump:");
235 (void)fprintf(stderr, " %s", *argv++);
236 (void)fprintf(stderr, "\n");
239 if (Tflag && uflag) {
240 (void)fprintf(stderr,
241 "You cannot use the T and u flags together.\n");
245 tape = "child pipeline process";
246 } else if (tape == NULL && (tape = getenv("TAPE")) == NULL)
247 tape = _PATH_DEFTAPE;
248 if (strcmp(tape, "-") == 0) {
250 tape = "standard output";
254 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
255 else if (!unlimited) {
257 * Determine how to default tape size and density
260 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
261 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
262 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
264 * hilit19 hits again: "
267 density = cartridge ? 100 : 160;
269 tsize = cartridge ? 1700L*120L : 2300L*120L;
272 if (strchr(tape, ':')) {
274 tape = strchr(host, ':');
277 if (index(tape, '\n')) {
278 (void)fprintf(stderr, "invalid characters in tape\n");
281 if (rmthost(host) == 0)
284 (void)fprintf(stderr, "remote dump not enabled\n");
288 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
290 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
292 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
293 signal(SIGTRAP, sig);
294 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
296 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
298 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
299 signal(SIGSEGV, sig);
300 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
301 signal(SIGTERM, sig);
302 if (signal(SIGINT, interrupt) == SIG_IGN)
303 signal(SIGINT, SIG_IGN);
305 dump_getfstab(); /* /etc/fstab snarfed */
307 * disk can be either the full special file name,
308 * the suffix of the special file name,
309 * the special name missing the leading '/',
310 * the file system name with or without the leading '/'.
312 dt = fstabsearch(disk);
314 disk = rawname(dt->fs_spec);
316 errx(X_STARTUP, "%s: unknown file system", dt->fs_spec);
317 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
318 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
320 (void)strncpy(spcl.c_dev, disk, NAMELEN);
321 (void)strncpy(spcl.c_filesys, "an unlisted file system",
324 spcl.c_dev[NAMELEN-1]='\0';
325 spcl.c_filesys[NAMELEN-1]='\0';
327 if ((mntpt = getmntpt(disk, &mntflags)) != 0) {
328 if (mntflags & MNT_RDONLY) {
331 "-L ignored for read-only filesystem.");
334 } else if (snapdump == 0) {
336 "should use -L when dumping live read-write "
339 char snapname[BUFSIZ], snapcmd[BUFSIZ];
341 snprintf(snapname, sizeof snapname, "%s/.snap", mntpt);
342 if ((stat(snapname, &sb) < 0) || !S_ISDIR(sb.st_mode)) {
343 msg("WARNING: %s %s\n",
344 "-L requested but snapshot location",
347 "is not a directory",
348 "dump downgraded, -L ignored");
351 snprintf(snapname, sizeof snapname,
352 "%s/.snap/dump_snapshot", mntpt);
353 snprintf(snapcmd, sizeof snapcmd, "%s %s %s",
354 _PATH_MKSNAP_FFS, mntpt, snapname);
356 if (system(snapcmd) != 0)
357 errx(X_STARTUP, "Cannot create %s: %s\n",
358 snapname, strerror(errno));
359 if ((diskfd = open(snapname, O_RDONLY)) < 0) {
361 errx(X_STARTUP, "Cannot open %s: %s\n",
362 snapname, strerror(errno));
365 if (fstat(diskfd, &sb) != 0)
366 err(X_STARTUP, "%s: stat", snapname);
367 spcl.c_date = _time_to_time64(sb.st_mtime);
370 } else if (snapdump != 0) {
371 msg("WARNING: Cannot use -L on an unmounted filesystem.\n");
375 if ((diskfd = open(disk, O_RDONLY)) < 0)
376 err(X_STARTUP, "Cannot open %s", disk);
377 if (fstat(diskfd, &sb) != 0)
378 err(X_STARTUP, "%s: stat", disk);
379 if (S_ISDIR(sb.st_mode))
380 errx(X_STARTUP, "%s: unknown file system", disk);
383 (void)strcpy(spcl.c_label, "none");
384 (void)gethostname(spcl.c_host, NAMELEN);
385 spcl.c_level = level - '0';
386 spcl.c_type = TS_TAPE;
388 if (spcl.c_date == 0) {
389 tmsg = "the epoch\n";
391 time_t t = _time64_to_time(spcl.c_date);
394 msg("Date of this level %c dump: %s", level, tmsg);
397 getdumptime(); /* /etc/dumpdates snarfed */
398 if (spcl.c_ddate == 0) {
399 tmsg = "the epoch\n";
401 time_t t = _time64_to_time(spcl.c_ddate);
404 msg("Date of last level %c dump: %s", lastlevel, tmsg);
406 msg("Dumping %s%s ", snapdump ? "snapshot of ": "", disk);
408 msgtail("(%s) ", dt->fs_file);
410 msgtail("to %s on host %s\n", tape, host);
412 msgtail("to %s\n", tape);
415 sblock = (struct fs *)sblock_buf;
416 for (i = 0; sblock_try[i] != -1; i++) {
417 sblock->fs_fsize = SBLOCKSIZE; /* needed in bread */
418 bread(sblock_try[i] >> dev_bshift, (char *) sblock, SBLOCKSIZE);
419 if ((sblock->fs_magic == FS_UFS1_MAGIC ||
420 (sblock->fs_magic == FS_UFS2_MAGIC &&
421 sblock->fs_sblockloc == sblock_try[i])) &&
422 sblock->fs_bsize <= MAXBSIZE &&
423 sblock->fs_bsize >= sizeof(struct fs))
426 if (sblock_try[i] == -1)
427 quit("Cannot find file system superblock\n");
428 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
429 dev_bshift = ffs(dev_bsize) - 1;
430 if (dev_bsize != (1 << dev_bshift))
431 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
432 tp_bshift = ffs(TP_BSIZE) - 1;
433 if (TP_BSIZE != (1 << tp_bshift))
434 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
435 maxino = sblock->fs_ipg * sblock->fs_ncg;
436 mapsize = roundup(howmany(maxino, CHAR_BIT), TP_BSIZE);
437 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
438 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
439 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
440 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
442 nonodump = spcl.c_level < honorlevel;
445 setproctitle("%s: pass 1: regular files", disk);
446 msg("mapping (Pass I) [regular files]\n");
447 anydirskipped = mapfiles(maxino, &tapesize);
450 setproctitle("%s: pass 2: directories", disk);
451 msg("mapping (Pass II) [directories]\n");
452 while (anydirskipped) {
453 anydirskipped = mapdirs(maxino, &tapesize);
456 if (pipeout || unlimited) {
457 tapesize += 10; /* 10 trailer blocks */
458 msg("estimated %ld tape blocks.\n", tapesize);
463 fetapes = (double) tapesize / blocksperfile;
464 else if (cartridge) {
465 /* Estimate number of tapes, assuming streaming stops at
466 the end of each block written, and not in mid-block.
467 Assume no erroneous blocks; this can be compensated
468 for with an artificially low tape size. */
470 ( (double) tapesize /* blocks */
471 * TP_BSIZE /* bytes/block */
472 * (1.0/density) /* 0.1" / byte " */
474 (double) tapesize /* blocks */
475 * (1.0/ntrec) /* streaming-stops per block */
476 * 15.48 /* 0.1" / streaming-stop " */
477 ) * (1.0 / tsize ); /* tape / 0.1" " */
479 /* Estimate number of tapes, for old fashioned 9-track
481 int tenthsperirg = (density == 625) ? 3 : 7;
483 ( (double) tapesize /* blocks */
484 * TP_BSIZE /* bytes / block */
485 * (1.0/density) /* 0.1" / byte " */
487 (double) tapesize /* blocks */
488 * (1.0/ntrec) /* IRG's / block */
489 * tenthsperirg /* 0.1" / IRG " */
490 ) * (1.0 / tsize ); /* tape / 0.1" " */
492 etapes = fetapes; /* truncating assignment */
494 /* count the dumped inodes map on each additional tape */
495 tapesize += (etapes - 1) *
496 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
497 tapesize += etapes + 10; /* headers + 10 trailer blks */
498 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
503 * If the user only wants an estimate of the number of
510 * Allocate tape buffer.
514 "can't allocate tape buffers - try a smaller blocking factor.\n");
517 (void)time((time_t *)&(tstart_writing));
518 dumpmap(usedinomap, TS_CLRI, maxino - 1);
521 setproctitle("%s: pass 3: directories", disk);
522 msg("dumping (Pass III) [directories]\n");
523 dirty = 0; /* XXX just to get gcc to shut up */
524 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
525 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */
529 if ((dirty & 1) == 0)
532 * Skip directory inodes deleted and maybe reallocated
534 dp = getino(ino, &mode);
537 (void)dumpino(dp, ino);
541 setproctitle("%s: pass 4: regular files", disk);
542 msg("dumping (Pass IV) [regular files]\n");
543 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
544 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */
548 if ((dirty & 1) == 0)
551 * Skip inodes deleted and reallocated as directories.
553 dp = getino(ino, &mode);
556 (void)dumpino(dp, ino);
559 (void)time((time_t *)&(tend_writing));
560 spcl.c_type = TS_END;
561 for (i = 0; i < ntrec; i++)
562 writeheader(maxino - 1);
564 msg("DUMP: %jd tape blocks\n", (intmax_t)spcl.c_tapea);
566 msg("DUMP: %jd tape blocks on %d volume%s\n",
567 (intmax_t)spcl.c_tapea, spcl.c_volume,
568 (spcl.c_volume == 1) ? "" : "s");
570 /* report dump performance, avoid division through zero */
571 if (tend_writing - tstart_writing == 0)
572 msg("finished in less than a second\n");
574 msg("finished in %jd seconds, throughput %jd KBytes/sec\n",
575 (intmax_t)tend_writing - tstart_writing,
576 (intmax_t)(spcl.c_tapea /
577 (tend_writing - tstart_writing)));
581 broadcast("DUMP IS DONE!\a\a\n");
582 msg("DUMP IS DONE\n");
591 "usage: dump [-0123456789acLnSu] [-B records] [-b blocksize] [-C cachesize]\n"
592 " [-D dumpdates] [-d density] [-f file | -P pipecommand] [-h level]\n"
593 " [-s feet] [-T date] filesystem\n"
599 * Check to see if a disk is currently mounted.
602 getmntpt(char *name, int *mntflagsp)
605 struct statfs *mntbuf;
607 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
608 for (i = 0; i < mntsize; i++) {
609 if (!strcmp(mntbuf[i].f_mntfromname, name)) {
610 *mntflagsp = mntbuf[i].f_flags;
611 return (mntbuf[i].f_mntonname);
618 * Pick up a numeric argument. It must be nonnegative and in the given
619 * range (except that a vmax of 0 means unlimited).
622 numarg(const char *meaning, long vmin, long vmax)
627 val = strtol(optarg, &p, 10);
629 errx(1, "illegal %s -- %s", meaning, optarg);
630 if (val < vmin || (vmax && val > vmax))
631 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
646 quit("Signal on pipe: cannot recover\n");
647 msg("Rewriting attempted as response to unknown signal.\n");
648 (void)fflush(stderr);
649 (void)fflush(stdout);
654 msg("SIGSEGV: ABORTING!\n");
655 (void)signal(SIGSEGV, SIG_DFL);
656 (void)kill(0, SIGSEGV);
667 * Ensure that the device passed in is a raw device.
669 if (stat(cp, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFCHR)
673 * Since there's only one device type now, we can't construct any
674 * better name, so we have to return NULL.
681 * Change set of key letters and ordered arguments into something
682 * getopt(3) will like.
685 obsolete(int *argcp, char **argvp[])
688 char *ap, **argv, *flagsp, **nargv, *p;
695 * Return if no arguments or first argument has leading
699 if (argc == 1 || *ap == '-' || *ap == '/')
702 /* Allocate space for new arguments. */
703 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
704 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
710 for (flags = 0; *ap; ++ap) {
722 warnx("option requires an argument -- %c", *ap);
725 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
729 (void)strcpy(&nargv[0][2], *argv);
743 /* Terminate flags. */
749 /* Copy remaining arguments. */
750 while ((*nargv++ = *argv++));
752 /* Update argument count. */
753 *argcp = nargv - *argvp - 1;