2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
42 static const char rcsid[] =
46 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/disklabel.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/ufsmount.h>
53 #include <ufs/ffs/fs.h>
55 #include <protocols/dumprestore.h>
74 #include "pathnames.h"
76 int mapsize; /* size of the state maps */
77 char *usedinomap; /* map of allocated inodes */
78 char *dumpdirmap; /* map of directories to be dumped */
79 char *dumpinomap; /* map of files to be dumped */
80 char *disk; /* name of the disk file */
81 char *tape; /* name of the tape file */
82 char *popenout; /* popen(3) per-"tape" command */
83 int level; /* dump level of this dump */
84 int uflag; /* update flag */
85 int diskfd; /* disk file descriptor */
86 int pipeout; /* true => output to standard output */
87 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
88 long tapesize; /* estimated tape size, blocks */
89 long tsize; /* tape size in 0.1" units */
90 int etapes; /* estimated number of tapes */
91 int nonodump; /* if set, do not honor UF_NODUMP user flags */
92 int unlimited; /* if set, write to end of medium */
93 int cachesize = 0; /* block cache size (in bytes), defaults to 0 */
94 int rsync_friendly; /* be friendly with rsync */
95 int notify = 0; /* notify operator flag */
96 int blockswritten = 0; /* number of blocks written on current tape */
97 int tapeno = 0; /* current tape number */
98 int ntrec = NTREC; /* # tape blocks in each tape record */
99 long blocksperfile; /* number of blocks per output file */
100 int cartridge = 0; /* Assume non-cartridge tape */
101 char *host = NULL; /* remote host (if any) */
102 time_t tstart_writing; /* when started writing the first tape block */
103 time_t tend_writing; /* after writing the last tape block */
104 int passno; /* current dump pass number */
105 struct fs *sblock; /* the file system super block */
106 long dev_bsize = 1; /* recalculated below */
107 int dev_bshift; /* log2(dev_bsize) */
108 int tp_bshift; /* log2(TP_BSIZE) */
109 int snapdump = 0; /* dumping live filesystem, so use snapshot */
111 static char *getmntpt(char *, int *);
112 static long numarg(const char *, long, long);
113 static void obsolete(int *, char **[]);
114 static void usage(void) __dead2;
117 main(int argc, char *argv[])
125 int ch, mode, mntflags;
126 int i, ret, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
127 int just_estimate = 0;
131 spcl.c_date = _time_to_time64(time(NULL));
133 tsize = 0; /* Default later, based on 'c' option for cart tapes */
134 dumpdates = _PATH_DUMPDATES;
137 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
138 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
145 obsolete(&argc, &argv);
146 while ((ch = getopt(argc, argv,
147 "0123456789aB:b:C:cD:d:f:h:LnP:RrSs:T:uWw")) != -1)
150 case '0': case '1': case '2': case '3': case '4':
151 case '5': case '6': case '7': case '8': case '9':
152 level = 10 * level + ch - '0';
155 case 'a': /* `auto-size', Write to EOM. */
159 case 'B': /* blocks per output file */
160 blocksperfile = numarg("number of blocks per file",
164 case 'b': /* blocks per tape write */
165 ntrec = numarg("number of blocks per write",
170 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
173 case 'c': /* Tape is cart. not 9-track */
181 case 'd': /* density, in bits per inch */
182 density = numarg("density", 10L, 327670L) / 10;
183 if (density >= 625 && !bflag)
184 ntrec = HIGHDENSITYTREC;
187 case 'f': /* output file */
188 if (popenout != NULL)
189 errx(X_STARTUP, "You cannot use the P and f "
190 "flags together.\n");
195 honorlevel = numarg("honor level", 0L, 10L);
202 case 'n': /* notify operators */
208 errx(X_STARTUP, "You cannot use the P and f "
209 "flags together.\n");
213 case 'r': /* store slightly less data to be friendly to rsync */
214 if (rsync_friendly < 1)
218 case 'R': /* store even less data to be friendlier to rsync */
219 if (rsync_friendly < 2)
223 case 'S': /* exit after estimating # of tapes */
227 case 's': /* tape size, feet */
228 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
231 case 'T': /* time of last dump */
232 spcl.c_ddate = unctime(optarg);
233 if (spcl.c_ddate < 0) {
234 (void)fprintf(stderr, "bad time \"%s\"\n",
242 case 'u': /* update /etc/dumpdates */
246 case 'W': /* what to do */
249 exit(X_FINOK); /* do nothing else */
258 (void)fprintf(stderr, "Must specify disk or file system\n");
264 (void)fprintf(stderr, "Unknown arguments to dump:");
266 (void)fprintf(stderr, " %s", *argv++);
267 (void)fprintf(stderr, "\n");
270 if (rsync_friendly && (level > 0)) {
271 (void)fprintf(stderr, "%s %s\n", "rsync friendly options",
272 "can be used only with level 0 dumps.");
275 if (Tflag && uflag) {
276 (void)fprintf(stderr,
277 "You cannot use the T and u flags together.\n");
281 tape = "child pipeline process";
282 } else if (tape == NULL && (tape = getenv("TAPE")) == NULL)
283 tape = _PATH_DEFTAPE;
284 if (strcmp(tape, "-") == 0) {
286 tape = "standard output";
290 blocksperfile = rounddown(blocksperfile, ntrec);
291 else if (!unlimited) {
293 * Determine how to default tape size and density
296 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
297 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
298 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
300 * hilit19 hits again: "
303 density = cartridge ? 100 : 160;
305 tsize = cartridge ? 1700L*120L : 2300L*120L;
308 if (strchr(tape, ':')) {
310 tape = strchr(host, ':');
313 if (strchr(tape, '\n')) {
314 (void)fprintf(stderr, "invalid characters in tape\n");
317 if (rmthost(host) == 0)
320 (void)fprintf(stderr, "remote dump not enabled\n");
324 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
326 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
328 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
329 signal(SIGTRAP, sig);
330 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
332 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
334 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
335 signal(SIGSEGV, sig);
336 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
337 signal(SIGTERM, sig);
338 if (signal(SIGINT, interrupt) == SIG_IGN)
339 signal(SIGINT, SIG_IGN);
341 dump_getfstab(); /* /etc/fstab snarfed */
343 * disk can be either the full special file name,
344 * the suffix of the special file name,
345 * the special name missing the leading '/',
346 * the file system name with or without the leading '/'.
348 dt = fstabsearch(disk);
350 disk = rawname(dt->fs_spec);
352 errx(X_STARTUP, "%s: unknown file system", dt->fs_spec);
353 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
354 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
356 (void)strncpy(spcl.c_dev, disk, NAMELEN);
357 (void)strncpy(spcl.c_filesys, "an unlisted file system",
360 spcl.c_dev[NAMELEN-1]='\0';
361 spcl.c_filesys[NAMELEN-1]='\0';
363 if ((mntpt = getmntpt(disk, &mntflags)) != NULL) {
364 if (mntflags & MNT_RDONLY) {
367 "-L ignored for read-only filesystem.");
370 } else if (snapdump == 0) {
372 "should use -L when dumping live read-write "
375 char snapname[BUFSIZ], snapcmd[BUFSIZ];
377 snprintf(snapname, sizeof snapname, "%s/.snap", mntpt);
378 if ((stat(snapname, &sb) < 0) || !S_ISDIR(sb.st_mode)) {
379 msg("WARNING: %s %s\n",
380 "-L requested but snapshot location",
383 "is not a directory",
384 "dump downgraded, -L ignored");
387 snprintf(snapname, sizeof snapname,
388 "%s/.snap/dump_snapshot", mntpt);
389 snprintf(snapcmd, sizeof snapcmd, "%s %s %s",
390 _PATH_MKSNAP_FFS, mntpt, snapname);
392 if (system(snapcmd) != 0)
393 errx(X_STARTUP, "Cannot create %s: %s\n",
394 snapname, strerror(errno));
395 if ((diskfd = open(snapname, O_RDONLY)) < 0) {
397 errx(X_STARTUP, "Cannot open %s: %s\n",
398 snapname, strerror(errno));
401 if (fstat(diskfd, &sb) != 0)
402 err(X_STARTUP, "%s: stat", snapname);
403 spcl.c_date = _time_to_time64(sb.st_mtime);
406 } else if (snapdump != 0) {
407 msg("WARNING: Cannot use -L on an unmounted filesystem.\n");
411 if ((diskfd = open(disk, O_RDONLY)) < 0)
412 err(X_STARTUP, "Cannot open %s", disk);
413 if (fstat(diskfd, &sb) != 0)
414 err(X_STARTUP, "%s: stat", disk);
415 if (S_ISDIR(sb.st_mode))
416 errx(X_STARTUP, "%s: unknown file system", disk);
419 (void)strcpy(spcl.c_label, "none");
420 (void)gethostname(spcl.c_host, NAMELEN);
421 spcl.c_level = level;
422 spcl.c_type = TS_TAPE;
423 if (rsync_friendly) {
424 /* don't store real dump times */
428 if (spcl.c_date == 0) {
429 tmsg = "the epoch\n";
431 time_t t = _time64_to_time(spcl.c_date);
434 msg("Date of this level %d dump: %s", level, tmsg);
436 if (!Tflag && (!rsync_friendly))
437 getdumptime(); /* /etc/dumpdates snarfed */
438 if (spcl.c_ddate == 0) {
439 tmsg = "the epoch\n";
441 time_t t = _time64_to_time(spcl.c_ddate);
445 msg("Date of last (level unknown) dump: %s", tmsg);
447 msg("Date of last level %d dump: %s", lastlevel, tmsg);
449 msg("Dumping %s%s ", snapdump ? "snapshot of ": "", disk);
451 msgtail("(%s) ", dt->fs_file);
453 msgtail("to %s on host %s\n", tape, host);
455 msgtail("to %s\n", tape);
458 if ((ret = sbget(diskfd, &sblock, STDSB)) != 0) {
461 warn("Cannot find file system superblock");
464 warn("Unable to read file system superblock");
468 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
469 dev_bshift = ffs(dev_bsize) - 1;
470 if (dev_bsize != (1 << dev_bshift))
471 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
472 tp_bshift = ffs(TP_BSIZE) - 1;
473 if (TP_BSIZE != (1 << tp_bshift))
474 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
475 maxino = sblock->fs_ipg * sblock->fs_ncg;
476 mapsize = roundup(howmany(maxino, CHAR_BIT), TP_BSIZE);
477 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
478 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
479 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
480 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
482 nonodump = spcl.c_level < honorlevel;
485 setproctitle("%s: pass 1: regular files", disk);
486 msg("mapping (Pass I) [regular files]\n");
487 anydirskipped = mapfiles(maxino, &tapesize);
490 setproctitle("%s: pass 2: directories", disk);
491 msg("mapping (Pass II) [directories]\n");
492 while (anydirskipped) {
493 anydirskipped = mapdirs(maxino, &tapesize);
496 if (pipeout || unlimited) {
497 tapesize += 10; /* 10 trailer blocks */
498 msg("estimated %ld tape blocks.\n", tapesize);
503 fetapes = (double) tapesize / blocksperfile;
504 else if (cartridge) {
505 /* Estimate number of tapes, assuming streaming stops at
506 the end of each block written, and not in mid-block.
507 Assume no erroneous blocks; this can be compensated
508 for with an artificially low tape size. */
510 ( (double) tapesize /* blocks */
511 * TP_BSIZE /* bytes/block */
512 * (1.0/density) /* 0.1" / byte " */
514 (double) tapesize /* blocks */
515 * (1.0/ntrec) /* streaming-stops per block */
516 * 15.48 /* 0.1" / streaming-stop " */
517 ) * (1.0 / tsize ); /* tape / 0.1" " */
519 /* Estimate number of tapes, for old fashioned 9-track
521 int tenthsperirg = (density == 625) ? 3 : 7;
523 ( (double) tapesize /* blocks */
524 * TP_BSIZE /* bytes / block */
525 * (1.0/density) /* 0.1" / byte " */
527 (double) tapesize /* blocks */
528 * (1.0/ntrec) /* IRG's / block */
529 * tenthsperirg /* 0.1" / IRG " */
530 ) * (1.0 / tsize ); /* tape / 0.1" " */
532 etapes = fetapes; /* truncating assignment */
534 /* count the dumped inodes map on each additional tape */
535 tapesize += (etapes - 1) *
536 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
537 tapesize += etapes + 10; /* headers + 10 trailer blks */
538 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
543 * If the user only wants an estimate of the number of
550 * Allocate tape buffer.
554 "can't allocate tape buffers - try a smaller blocking factor.\n");
557 (void)time((time_t *)&(tstart_writing));
558 dumpmap(usedinomap, TS_CLRI, maxino - 1);
561 setproctitle("%s: pass 3: directories", disk);
562 msg("dumping (Pass III) [directories]\n");
563 dirty = 0; /* XXX just to get gcc to shut up */
564 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
565 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */
569 if ((dirty & 1) == 0)
572 * Skip directory inodes deleted and maybe reallocated
574 dp = getino(ino, &mode);
577 (void)dumpino(dp, ino);
581 setproctitle("%s: pass 4: regular files", disk);
582 msg("dumping (Pass IV) [regular files]\n");
583 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
584 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */
588 if ((dirty & 1) == 0)
591 * Skip inodes deleted and reallocated as directories.
593 dp = getino(ino, &mode);
596 (void)dumpino(dp, ino);
599 (void)time((time_t *)&(tend_writing));
600 spcl.c_type = TS_END;
601 for (i = 0; i < ntrec; i++)
602 writeheader(maxino - 1);
604 msg("DUMP: %jd tape blocks\n", (intmax_t)spcl.c_tapea);
606 msg("DUMP: %jd tape blocks on %d volume%s\n",
607 (intmax_t)spcl.c_tapea, spcl.c_volume,
608 (spcl.c_volume == 1) ? "" : "s");
610 /* report dump performance, avoid division through zero */
611 if (tend_writing - tstart_writing == 0)
612 msg("finished in less than a second\n");
614 msg("finished in %jd seconds, throughput %jd KBytes/sec\n",
615 (intmax_t)tend_writing - tstart_writing,
616 (intmax_t)(spcl.c_tapea /
617 (tend_writing - tstart_writing)));
621 broadcast("DUMP IS DONE!\a\a\n");
622 msg("DUMP IS DONE\n");
631 "usage: dump [-0123456789acLnSu] [-B records] [-b blocksize] [-C cachesize]\n"
632 " [-D dumpdates] [-d density] [-f file | -P pipecommand] [-h level]\n"
633 " [-s feet] [-T date] filesystem\n"
639 * Check to see if a disk is currently mounted.
642 getmntpt(char *name, int *mntflagsp)
645 struct statfs *mntbuf;
647 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
648 for (i = 0; i < mntsize; i++) {
649 if (!strcmp(mntbuf[i].f_mntfromname, name)) {
650 *mntflagsp = mntbuf[i].f_flags;
651 return (mntbuf[i].f_mntonname);
658 * Pick up a numeric argument. It must be nonnegative and in the given
659 * range (except that a vmax of 0 means unlimited).
662 numarg(const char *meaning, long vmin, long vmax)
667 val = strtol(optarg, &p, 10);
669 errx(1, "illegal %s -- %s", meaning, optarg);
670 if (val < vmin || (vmax && val > vmax))
671 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
686 quit("Signal on pipe: cannot recover\n");
687 msg("Rewriting attempted as response to unknown signal.\n");
688 (void)fflush(stderr);
689 (void)fflush(stdout);
694 msg("SIGSEGV: ABORTING!\n");
695 (void)signal(SIGSEGV, SIG_DFL);
696 (void)kill(0, SIGSEGV);
707 * Ensure that the device passed in is a raw device.
709 if (stat(cp, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFCHR)
713 * Since there's only one device type now, we can't construct any
714 * better name, so we have to return NULL.
721 * Change set of key letters and ordered arguments into something
722 * getopt(3) will like.
725 obsolete(int *argcp, char **argvp[])
728 char *ap, **argv, *flagsp, **nargv, *p;
735 * Return if no arguments or first argument has leading
739 if (argc == 1 || *ap == '-' || *ap == '/')
742 /* Allocate space for new arguments. */
743 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
744 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
750 for (flags = 0; *ap; ++ap) {
762 warnx("option requires an argument -- %c", *ap);
765 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
769 (void)strcpy(&nargv[0][2], *argv);
783 /* Terminate flags. */
790 /* Copy remaining arguments. */
791 while ((*nargv++ = *argv++));
793 /* Update argument count. */
794 *argcp = nargv - *argvp - 1;