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