Add note about reboot before 'make upgrade' step.
[dragonfly.git] / sbin / dump / main.c
blobb22d02973fe24462d40df1818e4affab4ace1dc8
1 /*-
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
7 * are met:
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
27 * SUCH DAMAGE.
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 $
32 * $DragonFly: src/sbin/dump/main.c,v 1.15 2006/10/21 04:10:02 pavalos Exp $
35 #include <sys/param.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #ifdef sunos
39 #include <sys/vnode.h>
41 #include <ufs/inode.h>
42 #include <ufs/fs.h>
43 #else
44 #include <vfs/ufs/dinode.h>
45 #include <vfs/ufs/fs.h>
46 #endif
48 #include <protocols/dumprestore.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <fcntl.h>
53 #include <fstab.h>
54 #include <signal.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 #include "dump.h"
61 #include "pathnames.h"
63 #ifndef SBOFF
64 #define SBOFF (SBLOCK * DEV_BSIZE)
65 #endif
67 int notify = 0; /* notify operator flag */
68 int blockswritten = 0; /* number of blocks written on current tape */
69 int tapeno = 0; /* current tape number */
70 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
71 int ntrec = NTREC; /* # tape blocks in each tape record */
72 int cartridge = 0; /* Assume non-cartridge tape */
73 int dokerberos = 0; /* Use Kerberos authentication */
74 int cachesize = 0; /* block cache size (in bytes) */
75 long dev_bsize = 1; /* recalculated below */
76 long blocksperfile; /* output blocks per file */
77 const char *host; /* remote host (if any) */
78 const char *dumpdates; /* name of the file containing dump date information*/
79 const char *temp; /* name of the file for doing rewrite of dumpdates */
81 static long numarg(const char *, long, long);
82 static void obsolete(int *, char **[]);
83 static void usage(void);
85 int
86 main(int argc, char **argv)
88 struct stat sb;
89 ufs1_ino_t ino;
90 int dirty;
91 struct ufs1_dinode *dp;
92 struct fstab *dt;
93 char *map;
94 int ch;
95 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
96 int just_estimate = 0;
97 ufs1_ino_t maxino;
99 spcl.c_date = 0;
100 time((time_t *)&spcl.c_date);
102 tsize = 0; /* Default later, based on 'c' option for cart tapes */
103 if ((tape = getenv("TAPE")) == NULL)
104 tape = _PATH_DEFTAPE;
105 dumpdates = _PATH_DUMPDATES;
106 temp = _PATH_DTMP;
107 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
108 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
109 level = '0';
111 if (argc < 2)
112 usage();
114 obsolete(&argc, &argv);
115 #ifdef KERBEROS
116 #define optstring "0123456789aB:b:cd:f:h:kns:ST:uWwD:C:"
117 #else
118 #define optstring "0123456789aB:b:cd:f:h:ns:ST:uWwD:C:"
119 #endif
120 while ((ch = getopt(argc, argv, optstring)) != -1)
121 #undef optstring
122 switch (ch) {
123 /* dump level */
124 case '0': case '1': case '2': case '3': case '4':
125 case '5': case '6': case '7': case '8': case '9':
126 level = ch;
127 break;
129 case 'a': /* `auto-size', Write to EOM. */
130 unlimited = 1;
131 break;
133 case 'B': /* blocks per output file */
134 blocksperfile = numarg("number of blocks per file",
135 1L, 0L);
136 break;
138 case 'b': /* blocks per tape write */
139 ntrec = numarg("number of blocks per write",
140 1L, 1000L);
141 break;
143 case 'c': /* Tape is cart. not 9-track */
144 cartridge = 1;
145 break;
147 case 'd': /* density, in bits per inch */
148 density = numarg("density", 10L, 327670L) / 10;
149 if (density >= 625 && !bflag)
150 ntrec = HIGHDENSITYTREC;
151 break;
153 case 'f': /* output file */
154 tape = optarg;
155 break;
157 case 'D':
158 dumpdates = optarg;
159 break;
161 case 'C':
162 cachesize = numarg("cachesize", 0, 0) * 1024 * 1024;
163 break;
165 case 'h':
166 honorlevel = numarg("honor level", 0L, 10L);
167 break;
169 #ifdef KERBEROS
170 case 'k':
171 dokerberos = 1;
172 break;
173 #endif
175 case 'n': /* notify operators */
176 notify = 1;
177 break;
179 case 's': /* tape size, feet */
180 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
181 break;
183 case 'S': /* exit after estimating # of tapes */
184 just_estimate = 1;
185 break;
187 case 'T': /* time of last dump */
188 spcl.c_ddate = unctime(optarg);
189 if (spcl.c_ddate < 0) {
190 fprintf(stderr, "bad time \"%s\"\n", optarg);
191 exit(X_STARTUP);
193 Tflag = 1;
194 lastlevel = '?';
195 break;
197 case 'u': /* update /etc/dumpdates */
198 uflag = 1;
199 break;
201 case 'W': /* what to do */
202 case 'w':
203 lastdump(ch);
204 exit(X_FINOK); /* do nothing else */
206 default:
207 usage();
209 argc -= optind;
210 argv += optind;
212 if (argc < 1) {
213 fprintf(stderr, "Must specify disk or filesystem\n");
214 exit(X_STARTUP);
216 disk = *argv++;
217 argc--;
218 if (argc >= 1) {
219 fprintf(stderr, "Unknown arguments to dump:");
220 while (argc--)
221 fprintf(stderr, " %s", *argv++);
222 fprintf(stderr, "\n");
223 exit(X_STARTUP);
225 if (Tflag && uflag) {
226 fprintf(stderr, "You cannot use the T and u flags together.\n");
227 exit(X_STARTUP);
229 if (strcmp(tape, "-") == 0) {
230 pipeout++;
231 tape = "standard output";
234 if (blocksperfile)
235 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
236 else if (!unlimited) {
238 * Determine how to default tape size and density
240 * density tape size
241 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
242 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
243 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
244 * (450*4 - slop)
245 * hilit19 hits again: "
247 if (density == 0)
248 density = cartridge ? 100 : 160;
249 if (tsize == 0)
250 tsize = cartridge ? 1700L*120L : 2300L*120L;
253 if (strchr(tape, ':')) {
254 char *ehost;
255 if ((host = strdup(tape)) == NULL)
256 err(1, "strdup failed");
257 ehost = strchr(host, ':');
258 *ehost++ = '\0';
259 tape = ehost;
260 #ifdef RDUMP
261 if (strchr(tape, '\n')) {
262 fprintf(stderr, "invalid characters in tape\n");
263 exit(X_STARTUP);
265 if (rmthost(host) == 0)
266 exit(X_STARTUP);
267 #else
268 fprintf(stderr, "remote dump not enabled\n");
269 exit(X_STARTUP);
270 #endif
272 setuid(getuid()); /* rmthost() is the only reason to be setuid */
274 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
275 signal(SIGHUP, sig);
276 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
277 signal(SIGTRAP, sig);
278 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
279 signal(SIGFPE, sig);
280 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
281 signal(SIGBUS, sig);
282 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
283 signal(SIGSEGV, sig);
284 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
285 signal(SIGTERM, sig);
286 if (signal(SIGINT, interrupt) == SIG_IGN)
287 signal(SIGINT, SIG_IGN);
289 dump_getfstab(); /* /etc/fstab snarfed */
291 * disk can be either the full special file name,
292 * the suffix of the special file name,
293 * the special name missing the leading '/',
294 * the file system name with or without the leading '/'.
296 dt = fstabsearch(disk);
297 if (dt != NULL) {
298 disk = rawname(dt->fs_spec);
299 strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
300 strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
301 } else {
302 strncpy(spcl.c_dev, disk, NAMELEN);
303 strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
305 spcl.c_dev[NAMELEN-1]='\0';
306 spcl.c_filesys[NAMELEN-1]='\0';
307 strcpy(spcl.c_label, "none");
308 gethostname(spcl.c_host, NAMELEN);
309 spcl.c_level = level - '0';
310 spcl.c_type = TS_TAPE;
311 if (!Tflag)
312 getdumptime(); /* /etc/dumpdates snarfed */
314 msg("Date of this level %c dump: %s", level,
315 spcl.c_date == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_date));
316 msg("Date of last level %c dump: %s", lastlevel,
317 spcl.c_ddate == 0 ? "the epoch\n" : ctime((const time_t *)&spcl.c_ddate));
318 msg("Dumping %s ", disk);
319 if (dt != NULL)
320 msgtail("(%s) ", dt->fs_file);
321 if (host)
322 msgtail("to %s on host %s\n", tape, host);
323 else
324 msgtail("to %s\n", tape);
326 if ((diskfd = open(disk, O_RDONLY)) < 0)
327 err(X_STARTUP, "Cannot open %s", disk);
328 if (fstat(diskfd, &sb) != 0)
329 err(X_STARTUP, "%s: stat", disk);
330 if (S_ISDIR(sb.st_mode))
331 errx(X_STARTUP, "%s: unknown file system", disk);
332 sync();
333 sblock = (struct fs *)sblock_buf;
334 bread(SBOFF, (char *) sblock, SBSIZE);
335 if (sblock->fs_magic != FS_MAGIC)
336 quit("bad sblock magic number\n");
337 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
338 dev_bshift = ffs(dev_bsize) - 1;
339 if (dev_bsize != (1 << dev_bshift))
340 quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
341 tp_bshift = ffs(TP_BSIZE) - 1;
342 if (TP_BSIZE != (1 << tp_bshift))
343 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
344 #ifdef FS_44INODEFMT
345 if (sblock->fs_inodefmt >= FS_44INODEFMT)
346 spcl.c_flags |= DR_NEWINODEFMT;
347 #endif
348 maxino = sblock->fs_ipg * sblock->fs_ncg;
349 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
350 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
351 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
352 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
353 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
355 nonodump = spcl.c_level < honorlevel;
357 passno = 1;
358 setproctitle("%s: pass 1: regular files", disk);
359 msg("mapping (Pass I) [regular files]\n");
360 anydirskipped = mapfiles(maxino, &tapesize);
362 passno = 2;
363 setproctitle("%s: pass 2: directories", disk);
364 msg("mapping (Pass II) [directories]\n");
365 while (anydirskipped) {
366 anydirskipped = mapdirs(maxino, &tapesize);
369 if (pipeout || unlimited) {
370 tapesize += 10; /* 10 trailer blocks */
371 msg("estimated %ld tape blocks.\n", tapesize);
372 } else {
373 double fetapes;
375 if (blocksperfile)
376 fetapes = (double) tapesize / blocksperfile;
377 else if (cartridge) {
378 /* Estimate number of tapes, assuming streaming stops at
379 the end of each block written, and not in mid-block.
380 Assume no erroneous blocks; this can be compensated
381 for with an artificially low tape size. */
382 fetapes =
383 ( (double) tapesize /* blocks */
384 * TP_BSIZE /* bytes/block */
385 * (1.0/density) /* 0.1" / byte " */
387 (double) tapesize /* blocks */
388 * (1.0/ntrec) /* streaming-stops per block */
389 * 15.48 /* 0.1" / streaming-stop " */
390 ) * (1.0 / tsize ); /* tape / 0.1" " */
391 } else {
392 /* Estimate number of tapes, for old fashioned 9-track
393 tape */
394 int tenthsperirg = (density == 625) ? 3 : 7;
395 fetapes =
396 ( (double) tapesize /* blocks */
397 * TP_BSIZE /* bytes / block */
398 * (1.0/density) /* 0.1" / byte " */
400 (double) tapesize /* blocks */
401 * (1.0/ntrec) /* IRG's / block */
402 * tenthsperirg /* 0.1" / IRG " */
403 ) * (1.0 / tsize ); /* tape / 0.1" " */
405 etapes = fetapes; /* truncating assignment */
406 etapes++;
407 /* count the dumped inodes map on each additional tape */
408 tapesize += (etapes - 1) *
409 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
410 tapesize += etapes + 10; /* headers + 10 trailer blks */
411 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
412 tapesize, fetapes);
416 * If the user only wants an estimate of the number of
417 * tapes, exit now.
419 if (just_estimate)
420 exit(0);
423 * Allocate tape buffer.
425 if (!alloctape())
426 quit(
427 "can't allocate tape buffers - try a smaller blocking factor.\n");
429 startnewtape(1);
430 time((time_t *)&(tstart_writing));
431 dumpmap(usedinomap, TS_CLRI, maxino - 1);
433 passno = 3;
434 setproctitle("%s: pass 3: directories", disk);
435 msg("dumping (Pass III) [directories]\n");
436 dirty = 0; /* XXX just to get gcc to shut up */
437 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
438 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
439 dirty = *map++;
440 else
441 dirty >>= 1;
442 if ((dirty & 1) == 0)
443 continue;
445 * Skip directory inodes deleted and maybe reallocated
447 dp = getino(ino);
448 if ((dp->di_mode & IFMT) != IFDIR)
449 continue;
450 dumpino(dp, ino);
453 passno = 4;
454 setproctitle("%s: pass 4: regular files", disk);
455 msg("dumping (Pass IV) [regular files]\n");
456 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
457 int mode;
459 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
460 dirty = *map++;
461 else
462 dirty >>= 1;
463 if ((dirty & 1) == 0)
464 continue;
466 * Skip inodes deleted and reallocated as directories.
468 dp = getino(ino);
469 mode = dp->di_mode & IFMT;
470 if (mode == IFDIR)
471 continue;
472 dumpino(dp, ino);
475 time(&tend_writing);
476 spcl.c_type = TS_END;
477 for (i = 0; i < ntrec; i++)
478 writeheader(maxino - 1);
479 if (pipeout)
480 msg("DUMP: %ld tape blocks\n", (long)spcl.c_tapea);
481 else
482 msg("DUMP: %ld tape blocks on %d volume%s\n",
483 (long)spcl.c_tapea, spcl.c_volume,
484 (spcl.c_volume == 1) ? "" : "s");
486 /* report dump performance, avoid division through zero */
487 if (tend_writing - tstart_writing == 0)
488 msg("finished in less than a second\n");
489 else
490 msg("finished in %ld seconds, throughput %ld KBytes/sec\n",
491 (long)(tend_writing - tstart_writing),
492 spcl.c_tapea / (tend_writing - tstart_writing));
494 putdumptime();
495 trewind();
496 broadcast("DUMP IS DONE!\a\a\n");
497 msg("DUMP IS DONE\n");
498 Exit(X_FINOK);
499 /* NOTREACHED */
502 static void
503 usage(void)
505 fprintf(stderr,
506 "usage: dump [-0123456789ac"
507 #ifdef KERBEROS
509 #endif
510 "nSu] [-B records] [-b blocksize] [-D dumpdates]\n"
511 " [-d density] [-f file ] [-h level] [-s feet]\n"
512 " [-T date] [-C cachesizeMB] filesystem\n"
513 " dump [-W | -w]\n");
514 exit(X_STARTUP);
518 * Pick up a numeric argument. It must be nonnegative and in the given
519 * range (except that a vmax of 0 means unlimited).
521 static long
522 numarg(const char *meaning, long vmin, long vmax)
524 char *p;
525 long val;
527 val = strtol(optarg, &p, 10);
528 if (*p)
529 errx(1, "illegal %s -- %s", meaning, optarg);
530 if (val < vmin || (vmax && val > vmax))
531 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
532 return (val);
535 void
536 sig(int signo)
538 switch(signo) {
539 case SIGALRM:
540 case SIGBUS:
541 case SIGFPE:
542 case SIGHUP:
543 case SIGTERM:
544 case SIGTRAP:
545 if (pipeout)
546 quit("Signal on pipe: cannot recover\n");
547 msg("Rewriting attempted as response to unknown signal.\n");
548 fflush(stderr);
549 fflush(stdout);
550 close_rewind();
551 exit(X_REWRITE);
552 /* NOTREACHED */
553 case SIGSEGV:
554 msg("SIGSEGV: ABORTING!\n");
555 signal(SIGSEGV, SIG_DFL);
556 kill(0, SIGSEGV);
557 /* NOTREACHED */
561 char *
562 rawname(char *cp)
564 static char rawbuf[MAXPATHLEN];
565 char *dp;
566 struct stat sb;
568 if (stat(cp, &sb) == 0) {
570 * If the name already refers to a raw device, return
571 * it immediately without tampering.
573 if ((sb.st_mode & S_IFMT) == S_IFCHR)
574 return (cp);
577 dp = strrchr(cp, '/');
579 if (dp == NULL)
580 return (NULL);
581 *dp = '\0';
582 strncpy(rawbuf, cp, MAXPATHLEN - 1);
583 rawbuf[MAXPATHLEN-1] = '\0';
584 *dp = '/';
585 strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
586 strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
587 return (rawbuf);
591 * obsolete --
592 * Change set of key letters and ordered arguments into something
593 * getopt(3) will like.
595 static void
596 obsolete(int *argcp, char ***argvp)
598 int argc, flags;
599 char *ap, **argv, *flagsp, **nargv, *p;
601 /* Setup. */
602 argv = *argvp;
603 argc = *argcp;
605 /* Return if no arguments or first argument has leading dash. */
606 ap = argv[1];
607 if (argc == 1 || *ap == '-')
608 return;
610 /* Allocate space for new arguments. */
611 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
612 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
613 err(1, NULL);
615 *nargv++ = *argv;
616 argv += 2;
618 for (flags = 0; *ap; ++ap) {
619 switch (*ap) {
620 case 'B':
621 case 'b':
622 case 'd':
623 case 'f':
624 case 'D':
625 case 'C':
626 case 'h':
627 case 's':
628 case 'T':
629 if (*argv == NULL) {
630 warnx("option requires an argument -- %c", *ap);
631 usage();
633 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
634 err(1, NULL);
635 nargv[0][0] = '-';
636 nargv[0][1] = *ap;
637 strcpy(&nargv[0][2], *argv);
638 ++argv;
639 ++nargv;
640 break;
641 default:
642 if (!flags) {
643 *p++ = '-';
644 flags = 1;
646 *p++ = *ap;
647 break;
651 /* Terminate flags. */
652 if (flags) {
653 *p = '\0';
654 *nargv++ = flagsp;
657 /* Copy remaining arguments. */
658 while ((*nargv++ = *argv++));
660 /* Update argument count. */
661 *argcp = nargv - *argvp - 1;