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. 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
29 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.6 (Berkeley) 5/1/95
31 * $FreeBSD: src/sbin/dump/main.c,v 1.20.2.9 2003/01/25 18:54:59 dillon Exp $
34 #include <sys/param.h>
37 #include <vfs/ufs/dinode.h>
38 #include <vfs/ufs/fs.h>
40 #include <protocols/dumprestore.h>
53 #include "pathnames.h"
55 int mapsize; /* size of the state maps */
56 char *usedinomap; /* map of allocated inodes */
57 char *dumpdirmap; /* map of directories to be dumped */
58 char *dumpinomap; /* map of files to be dumped */
61 #define SBOFF (SBLOCK * DEV_BSIZE)
64 char *disk; /* name of the disk file */
65 const char *tape; /* name of the tape file */
66 int level; /* dump level of this dump */
67 int uflag; /* update flag */
68 int diskfd; /* disk file descriptor */
69 int pipeout; /* true => output to standard output */
70 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
71 long tapesize; /* estimated tape size, blocks */
72 long tsize; /* tape size in 0.1" units */
73 int etapes; /* estimated number of tapes */
74 int nonodump; /* if set, do not honor UF_NODUMP user flags */
75 int unlimited; /* if set, write to end of medium */
76 int cachesize = 0; /* block cache size (in bytes) */
77 int notify = 0; /* notify operator flag */
78 int blockswritten = 0; /* number of blocks written on current tape */
79 int tapeno = 0; /* current tape number */
80 int ntrec = NTREC; /* # tape blocks in each tape record */
81 long blocksperfile; /* output blocks per file */
82 int cartridge = 0; /* Assume non-cartridge tape */
83 int dokerberos = 0; /* Use Kerberos authentication */
84 long dev_bsize = 1; /* recalculated below */
85 const char *host; /* remote host (if any) */
86 time_t tstart_writing; /* when started writing the first tape block */
87 time_t tend_writing; /* after writing the last tape block */
88 int passno; /* current dump pass number */
89 struct fs *sblock; /* the file system super block */
90 int dev_bshift; /* log2(dev_bsize) */
91 int tp_bshift; /* log2(TP_BSIZE) */
92 char sblock_buf[MAXBSIZE];
93 static long numarg(const char *, long, long);
94 static void obsolete(int *, char **[]);
95 static void usage(void);
98 main(int argc, char **argv)
103 struct ufs1_dinode *dp;
107 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
108 int just_estimate = 0;
112 time((time_t *)&spcl.c_date);
114 tsize = 0; /* Default later, based on 'c' option for cart tapes */
115 if ((tape = getenv("TAPE")) == NULL)
116 tape = _PATH_DEFTAPE;
117 dumpdates = _PATH_DUMPDATES;
118 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
119 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
125 obsolete(&argc, &argv);
127 #define optstring "0123456789aB:b:cd:f:h:kns:ST:uWwD:C:"
129 #define optstring "0123456789aB:b:cd:f:h:ns:ST:uWwD:C:"
131 while ((ch = getopt(argc, argv, optstring)) != -1)
135 case '0': case '1': case '2': case '3': case '4':
136 case '5': case '6': case '7': case '8': case '9':
140 case 'a': /* `auto-size', Write to EOM. */
144 case 'B': /* blocks per output file */
145 blocksperfile = numarg("number of blocks per file",
149 case 'b': /* blocks per tape write */
150 ntrec = numarg("number of blocks per write",
154 case 'c': /* Tape is cart. not 9-track */
158 case 'd': /* density, in bits per inch */
159 density = numarg("density", 10L, 327670L) / 10;
160 if (density >= 625 && !bflag)
161 ntrec = HIGHDENSITYTREC;
164 case 'f': /* output file */
173 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
177 honorlevel = numarg("honor level", 0L, 10L);
186 case 'n': /* notify operators */
190 case 's': /* tape size, feet */
191 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
194 case 'S': /* exit after estimating # of tapes */
198 case 'T': /* time of last dump */
199 spcl.c_ddate = unctime(optarg);
200 if (spcl.c_ddate < 0) {
201 fprintf(stderr, "bad time \"%s\"\n", optarg);
208 case 'u': /* update /etc/dumpdates */
212 case 'W': /* what to do */
215 exit(X_FINOK); /* do nothing else */
224 fprintf(stderr, "Must specify disk or filesystem\n");
230 fprintf(stderr, "Unknown arguments to dump:");
232 fprintf(stderr, " %s", *argv++);
233 fprintf(stderr, "\n");
236 if (Tflag && uflag) {
237 fprintf(stderr, "You cannot use the T and u flags together.\n");
240 if (strcmp(tape, "-") == 0) {
242 tape = "standard output";
246 blocksperfile = rounddown(blocksperfile, ntrec); /* round down */
247 else if (!unlimited) {
249 * Determine how to default tape size and density
252 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
253 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
254 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
256 * hilit19 hits again: "
259 density = cartridge ? 100 : 160;
261 tsize = cartridge ? 1700L*120L : 2300L*120L;
264 if (strchr(tape, ':')) {
266 if ((host = strdup(tape)) == NULL)
267 err(1, "strdup failed");
268 ehost = strchr(host, ':');
272 if (strchr(tape, '\n')) {
273 fprintf(stderr, "invalid characters in tape\n");
276 if (rmthost(host) == 0)
279 fprintf(stderr, "remote dump not enabled\n");
283 setuid(getuid()); /* rmthost() is the only reason to be setuid */
285 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
287 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
288 signal(SIGTRAP, sig);
289 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
291 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
293 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
294 signal(SIGSEGV, sig);
295 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
296 signal(SIGTERM, sig);
297 if (signal(SIGINT, interrupt) == SIG_IGN)
298 signal(SIGINT, SIG_IGN);
300 dump_getfstab(); /* /etc/fstab snarfed */
302 * disk can be either the full special file name,
303 * the suffix of the special file name,
304 * the special name missing the leading '/',
305 * the file system name with or without the leading '/'.
307 dt = fstabsearch(disk);
309 disk = rawname(dt->fs_spec);
310 strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
311 strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
313 strncpy(spcl.c_dev, disk, NAMELEN);
314 strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
316 spcl.c_dev[NAMELEN-1]='\0';
317 spcl.c_filesys[NAMELEN-1]='\0';
318 strcpy(spcl.c_label, "none");
319 gethostname(spcl.c_host, NAMELEN);
320 spcl.c_level = level - '0';
321 spcl.c_type = TS_TAPE;
323 getdumptime(); /* /etc/dumpdates snarfed */
325 msg("Date of this level %c dump: %s", level,
326 spcl.c_date == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_date));
327 msg("Date of last level %c dump: %s", lastlevel,
328 spcl.c_ddate == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_ddate));
329 msg("Dumping %s ", disk);
331 msgtail("(%s) ", dt->fs_file);
333 msgtail("to %s on host %s\n", tape, host);
335 msgtail("to %s\n", tape);
337 if ((diskfd = open(disk, O_RDONLY)) < 0)
338 err(X_STARTUP, "Cannot open %s", disk);
339 if (fstat(diskfd, &sb) != 0)
340 err(X_STARTUP, "%s: stat", disk);
341 if (S_ISDIR(sb.st_mode))
342 errx(X_STARTUP, "%s: unknown file system", disk);
344 sblock = (struct fs *)sblock_buf;
345 bread(SBOFF, (char *) sblock, SBSIZE);
346 if (sblock->fs_magic != FS_MAGIC)
347 quit("bad sblock magic number\n");
348 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
349 dev_bshift = ffs(dev_bsize) - 1;
350 if (dev_bsize != (1 << dev_bshift))
351 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
352 tp_bshift = ffs(TP_BSIZE) - 1;
353 if (TP_BSIZE != (1 << tp_bshift))
354 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
356 if (sblock->fs_inodefmt >= FS_44INODEFMT)
357 spcl.c_flags |= DR_NEWINODEFMT;
359 maxino = sblock->fs_ipg * sblock->fs_ncg;
360 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
361 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
362 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
363 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
364 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
366 nonodump = spcl.c_level < honorlevel;
369 setproctitle("%s: pass 1: regular files", disk);
370 msg("mapping (Pass I) [regular files]\n");
371 anydirskipped = mapfiles(maxino, &tapesize);
374 setproctitle("%s: pass 2: directories", disk);
375 msg("mapping (Pass II) [directories]\n");
376 while (anydirskipped) {
377 anydirskipped = mapdirs(maxino, &tapesize);
380 if (pipeout || unlimited) {
381 tapesize += 10; /* 10 trailer blocks */
382 msg("estimated %ld tape blocks.\n", tapesize);
387 fetapes = (double) tapesize / blocksperfile;
388 else if (cartridge) {
389 /* Estimate number of tapes, assuming streaming stops at
390 the end of each block written, and not in mid-block.
391 Assume no erroneous blocks; this can be compensated
392 for with an artificially low tape size. */
394 ( (double) tapesize /* blocks */
395 * TP_BSIZE /* bytes/block */
396 * (1.0/density) /* 0.1" / byte " */
398 (double) tapesize /* blocks */
399 * (1.0/ntrec) /* streaming-stops per block */
400 * 15.48 /* 0.1" / streaming-stop " */
401 ) * (1.0 / tsize ); /* tape / 0.1" " */
403 /* Estimate number of tapes, for old fashioned 9-track
405 int tenthsperirg = (density == 625) ? 3 : 7;
407 ( (double) tapesize /* blocks */
408 * TP_BSIZE /* bytes / block */
409 * (1.0/density) /* 0.1" / byte " */
411 (double) tapesize /* blocks */
412 * (1.0/ntrec) /* IRG's / block */
413 * tenthsperirg /* 0.1" / IRG " */
414 ) * (1.0 / tsize ); /* tape / 0.1" " */
416 etapes = fetapes; /* truncating assignment */
418 /* count the dumped inodes map on each additional tape */
419 tapesize += (etapes - 1) *
420 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
421 tapesize += etapes + 10; /* headers + 10 trailer blks */
422 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
427 * If the user only wants an estimate of the number of
434 * Allocate tape buffer.
438 "can't allocate tape buffers - try a smaller blocking factor.\n");
441 time((time_t *)&(tstart_writing));
442 dumpmap(usedinomap, TS_CLRI, maxino - 1);
445 setproctitle("%s: pass 3: directories", disk);
446 msg("dumping (Pass III) [directories]\n");
447 dirty = 0; /* XXX just to get gcc to shut up */
448 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
449 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
453 if ((dirty & 1) == 0)
456 * Skip directory inodes deleted and maybe reallocated
459 if ((dp->di_mode & IFMT) != IFDIR)
465 setproctitle("%s: pass 4: regular files", disk);
466 msg("dumping (Pass IV) [regular files]\n");
467 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
470 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
474 if ((dirty & 1) == 0)
477 * Skip inodes deleted and reallocated as directories.
480 mode = dp->di_mode & IFMT;
487 spcl.c_type = TS_END;
488 for (i = 0; i < ntrec; i++)
489 writeheader(maxino - 1);
491 msg("DUMP: %ld tape blocks\n", (long)spcl.c_tapea);
493 msg("DUMP: %ld tape blocks on %d volume%s\n",
494 (long)spcl.c_tapea, spcl.c_volume,
495 (spcl.c_volume == 1) ? "" : "s");
497 /* report dump performance, avoid division through zero */
498 if (tend_writing - tstart_writing == 0)
499 msg("finished in less than a second\n");
501 msg("finished in %ld seconds, throughput %ld KBytes/sec\n",
502 (long)(tend_writing - tstart_writing),
503 spcl.c_tapea / (tend_writing - tstart_writing));
507 broadcast("DUMP IS DONE!\a\a\n");
508 msg("DUMP IS DONE\n");
517 "usage: dump [-0123456789ac"
521 "nSu] [-B records] [-b blocksize] [-D dumpdates]\n"
522 " [-d density] [-f file ] [-h level] [-s feet]\n"
523 " [-T date] [-C cachesizeMB] filesystem\n"
524 " dump [-W | -w]\n");
529 * Pick up a numeric argument. It must be nonnegative and in the given
530 * range (except that a vmax of 0 means unlimited).
533 numarg(const char *meaning, long vmin, long vmax)
538 val = strtol(optarg, &p, 10);
540 errx(1, "illegal %s -- %s", meaning, optarg);
541 if (val < vmin || (vmax && val > vmax))
542 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
557 quit("Signal on pipe: cannot recover\n");
558 msg("Rewriting attempted as response to unknown signal.\n");
565 msg("SIGSEGV: ABORTING!\n");
566 signal(SIGSEGV, SIG_DFL);
575 static char rawbuf[MAXPATHLEN];
579 if (stat(cp, &sb) == 0) {
581 * If the name already refers to a raw device, return
582 * it immediately without tampering.
584 if ((sb.st_mode & S_IFMT) == S_IFCHR)
588 dp = strrchr(cp, '/');
593 strncpy(rawbuf, cp, MAXPATHLEN - 1);
594 rawbuf[MAXPATHLEN-1] = '\0';
596 strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
597 strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
603 * Change set of key letters and ordered arguments into something
604 * getopt(3) will like.
607 obsolete(int *argcp, char ***argvp)
610 char *ap, **argv, *flagsp, **nargv, *p;
616 /* Return if no arguments or first argument has leading dash. */
618 if (argc == 1 || *ap == '-')
621 /* Allocate space for new arguments. */
622 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
623 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
629 for (flags = 0; *ap; ++ap) {
641 warnx("option requires an argument -- %c", *ap);
644 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
648 strcpy(&nargv[0][2], *argv);
662 /* Terminate flags. */
668 /* Copy remaining arguments. */
669 while ((*nargv++ = *argv++));
671 /* Update argument count. */
672 *argcp = nargv - *argvp - 1;