1*a7b9eedcSflorian /* $OpenBSD: main.c,v 1.66 2024/05/09 08:35:40 florian Exp $ */
22904a520Smillert /* $NetBSD: main.c,v 1.14 1997/06/05 11:13:24 lukem Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
5df930be7Sderaadt * Copyright (c) 1980, 1991, 1993, 1994
6df930be7Sderaadt * The Regents of the University of California. All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt * modification, are permitted provided that the following conditions
10df930be7Sderaadt * are met:
11df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt * documentation and/or other materials provided with the distribution.
161ef0d710Smillert * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt * may be used to endorse or promote products derived from this software
18df930be7Sderaadt * without specific prior written permission.
19df930be7Sderaadt *
20df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt * SUCH DAMAGE.
31df930be7Sderaadt */
32df930be7Sderaadt
3378eb0b7eSderaadt #include <sys/param.h> /* MAXBSIZE DEV_BSIZE roundup */
342904a520Smillert #include <sys/mount.h>
352904a520Smillert #include <sys/stat.h>
36df930be7Sderaadt #include <sys/time.h>
37b39ffb6cSkrw #include <sys/ioctl.h>
38b39ffb6cSkrw #include <sys/disklabel.h>
39b39ffb6cSkrw #include <sys/dkio.h>
40df930be7Sderaadt #include <ufs/ffs/fs.h>
41df930be7Sderaadt #include <ufs/ufs/dinode.h>
42df930be7Sderaadt
43df930be7Sderaadt #include <protocols/dumprestore.h>
44df930be7Sderaadt
45df930be7Sderaadt #include <ctype.h>
46df930be7Sderaadt #include <err.h>
47df930be7Sderaadt #include <errno.h>
48df930be7Sderaadt #include <fcntl.h>
49df930be7Sderaadt #include <fstab.h>
50d473c96aSmillert #include <paths.h>
51df930be7Sderaadt #include <signal.h>
52df930be7Sderaadt #include <stdio.h>
53df930be7Sderaadt #include <stdlib.h>
54df930be7Sderaadt #include <string.h>
552904a520Smillert #include <time.h>
56df930be7Sderaadt #include <unistd.h>
57b9fc9a72Sderaadt #include <limits.h>
58150f0d6bShalex #include <util.h>
59df930be7Sderaadt
60df930be7Sderaadt #include "dump.h"
61df930be7Sderaadt #include "pathnames.h"
62df930be7Sderaadt
639be7043fSmortimer int mapsize; /* size of the state maps */
649be7043fSmortimer char *usedinomap; /* map of allocated inodes */
659be7043fSmortimer char *dumpdirmap; /* map of directories to be dumped */
669be7043fSmortimer char *dumpinomap; /* map of files to be dumped */
679be7043fSmortimer char *disk; /* name of the disk file */
689be7043fSmortimer char *tape; /* name of the tape file */
699be7043fSmortimer char level; /* dump level of this dump */
709be7043fSmortimer int uflag; /* update flag */
719be7043fSmortimer int diskfd; /* disk file descriptor */
729be7043fSmortimer int pipeout; /* true => output to standard output */
739be7043fSmortimer int density = 0; /* density in bytes/0.1" */
749be7043fSmortimer int64_t tapesize; /* estimated tape size, blocks */
759be7043fSmortimer int64_t tsize; /* tape size in 0.1" units */
769be7043fSmortimer int etapes; /* estimated number of tapes */
779be7043fSmortimer int nonodump; /* if set, do not honor UF_NODUMP user flags */
789be7043fSmortimer int unlimited; /* if set, write to end of medium */
79df930be7Sderaadt int notify = 0; /* notify operator flag */
800603cbefSnaddy int64_t blockswritten = 0; /* number of blocks written on current tape */
81df930be7Sderaadt int tapeno = 0; /* current tape number */
82df930be7Sderaadt int ntrec = NTREC; /* # tape blocks in each tape record */
830603cbefSnaddy int64_t blocksperfile; /* output blocks per file */
849be7043fSmortimer int cartridge = 0; /* Assume non-cartridge tape */
85df930be7Sderaadt char *host = NULL; /* remote host (if any) */
869be7043fSmortimer time_t tstart_writing; /* when started writing the first tape block */
879be7043fSmortimer long xferrate; /* averaged transfer rate of all volumes */
889be7043fSmortimer struct fs *sblock; /* the file system super block */
899be7043fSmortimer char sblock_buf[MAXBSIZE];
909be7043fSmortimer int tp_bshift; /* log2(TP_BSIZE) */
919be7043fSmortimer char *duid; /* duid of the disk being dumped */
925c4b5466Sderaadt int maxbsize = 64*1024; /* XXX MAXBSIZE from sys/param.h */
93df930be7Sderaadt
94b39ffb6cSkrw struct disklabel lab;
95b39ffb6cSkrw
960155e653Smillert /*
970155e653Smillert * Possible superblock locations ordered from most to least likely.
980155e653Smillert */
990155e653Smillert static int sblock_try[] = SBLOCKSEARCH;
1000155e653Smillert
1010603cbefSnaddy static long long numarg(char *, long long, long long);
102c72b5b24Smillert static void obsolete(int *, char **[]);
103c72b5b24Smillert static void usage(void);
104df930be7Sderaadt
105df930be7Sderaadt int
main(int argc,char * argv[])10653fedb49Sderaadt main(int argc, char *argv[])
107df930be7Sderaadt {
108e073c79dSmpech ino_t ino;
109e073c79dSmpech int dirty;
1100155e653Smillert union dinode *dp;
111e073c79dSmpech struct fstab *dt;
112e073c79dSmpech char *map;
1130155e653Smillert int ch, mode;
11400e3001fSderaadt struct tm then;
115da11bcd0Smillert struct statfs fsbuf;
116df930be7Sderaadt int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
117df930be7Sderaadt ino_t maxino;
1180155e653Smillert time_t t;
1192904a520Smillert int dirlist;
120*a7b9eedcSflorian char *toplevel, *str, *mount_point = NULL, *realpath, *ct;
1210b5a38dcSstephan int just_estimate = 0;
122150f0d6bShalex u_int64_t zero_uid = 0;
123df930be7Sderaadt
1240155e653Smillert spcl.c_date = (int64_t)time(NULL);
125df930be7Sderaadt
126df930be7Sderaadt tsize = 0; /* Default later, based on 'c' option for cart tapes */
12709f2560dSniklas if ((tape = getenv("TAPE")) == NULL)
128df930be7Sderaadt tape = _PATH_DEFTAPE;
129df930be7Sderaadt dumpdates = _PATH_DUMPDATES;
130df930be7Sderaadt if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
131df930be7Sderaadt quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
132df930be7Sderaadt level = '0';
133df930be7Sderaadt
134df930be7Sderaadt if (argc < 2)
135df930be7Sderaadt usage();
136df930be7Sderaadt
137df930be7Sderaadt obsolete(&argc, &argv);
138352e3770Sguenther while ((ch = getopt(argc, argv, "0123456789aB:b:cd:f:h:ns:ST:uWw")) != -1)
139df930be7Sderaadt switch (ch) {
140df930be7Sderaadt /* dump level */
141df930be7Sderaadt case '0': case '1': case '2': case '3': case '4':
142df930be7Sderaadt case '5': case '6': case '7': case '8': case '9':
143df930be7Sderaadt level = ch;
144df930be7Sderaadt break;
145df930be7Sderaadt
146df930be7Sderaadt case 'B': /* blocks per output file */
1470603cbefSnaddy blocksperfile = numarg("blocks per file", 1, 0);
148df930be7Sderaadt break;
149df930be7Sderaadt
150df930be7Sderaadt case 'b': /* blocks per tape write */
1510603cbefSnaddy ntrec = numarg("blocks per write", 1, 1000);
15231ebca96Sderaadt if (ntrec > maxbsize/1024) {
15331ebca96Sderaadt msg("Please choose a blocksize <= %dKB\n",
15431ebca96Sderaadt maxbsize/1024);
15530715473Sderaadt exit(X_STARTUP);
15631ebca96Sderaadt }
1576d9ee4f6Smillert bflag = 1;
158df930be7Sderaadt break;
159df930be7Sderaadt
160df930be7Sderaadt case 'c': /* Tape is cart. not 9-track */
161df930be7Sderaadt cartridge = 1;
162df930be7Sderaadt break;
163df930be7Sderaadt
164df930be7Sderaadt case 'd': /* density, in bits per inch */
1650603cbefSnaddy density = numarg("density", 10, 327670) / 10;
166df930be7Sderaadt if (density >= 625 && !bflag)
167df930be7Sderaadt ntrec = HIGHDENSITYTREC;
168df930be7Sderaadt break;
169df930be7Sderaadt
170df930be7Sderaadt case 'f': /* output file */
171df930be7Sderaadt tape = optarg;
172df930be7Sderaadt break;
173df930be7Sderaadt
174df930be7Sderaadt case 'h':
1750603cbefSnaddy honorlevel = numarg("honor level", 0, 10);
176df930be7Sderaadt break;
177df930be7Sderaadt
178df930be7Sderaadt case 'n': /* notify operators */
179df930be7Sderaadt notify = 1;
180df930be7Sderaadt break;
181df930be7Sderaadt
182df930be7Sderaadt case 's': /* tape size, feet */
1830603cbefSnaddy tsize = numarg("tape size", 1, 0) * 12 * 10;
184df930be7Sderaadt break;
185df930be7Sderaadt
1860b5a38dcSstephan case 'S': /* estimate blocks and # of tapes */
1870b5a38dcSstephan just_estimate = 1;
1880b5a38dcSstephan break;
1890b5a38dcSstephan
190df930be7Sderaadt case 'T': /* time of last dump */
19100e3001fSderaadt str = strptime(optarg, "%a %b %e %H:%M:%S %Y", &then);
19200e3001fSderaadt then.tm_isdst = -1;
19300e3001fSderaadt if (str == NULL || (*str != '\n' && *str != '\0'))
1940155e653Smillert spcl.c_ddate = -1;
19500e3001fSderaadt else
1960155e653Smillert spcl.c_ddate = (int64_t)mktime(&then);
197df930be7Sderaadt if (spcl.c_ddate < 0) {
198df930be7Sderaadt (void)fprintf(stderr, "bad time \"%s\"\n",
199df930be7Sderaadt optarg);
20030715473Sderaadt exit(X_STARTUP);
201df930be7Sderaadt }
202df930be7Sderaadt Tflag = 1;
203df930be7Sderaadt lastlevel = '?';
204df930be7Sderaadt break;
205df930be7Sderaadt
206df930be7Sderaadt case 'u': /* update /etc/dumpdates */
207df930be7Sderaadt uflag = 1;
208df930be7Sderaadt break;
209df930be7Sderaadt
210df930be7Sderaadt case 'W': /* what to do */
211df930be7Sderaadt case 'w':
212df930be7Sderaadt lastdump(ch);
21330715473Sderaadt exit(X_FINOK); /* do nothing else */
2140e419460Sderaadt break;
215df930be7Sderaadt
21631ebca96Sderaadt case 'a': /* `auto-size', Write to EOM. */
21731ebca96Sderaadt unlimited = 1;
21831ebca96Sderaadt break;
21931ebca96Sderaadt
220df930be7Sderaadt default:
221df930be7Sderaadt usage();
222df930be7Sderaadt }
223df930be7Sderaadt argc -= optind;
224df930be7Sderaadt argv += optind;
225df930be7Sderaadt
226df930be7Sderaadt if (argc < 1) {
227df930be7Sderaadt (void)fprintf(stderr, "Must specify disk or filesystem\n");
22830715473Sderaadt exit(X_STARTUP);
229df930be7Sderaadt }
2302904a520Smillert
2312904a520Smillert /*
2322904a520Smillert * determine if disk is a subdirectory, and setup appropriately
2332904a520Smillert */
2342904a520Smillert dirlist = 0;
2352904a520Smillert toplevel = NULL;
2362904a520Smillert for (i = 0; i < argc; i++) {
2372904a520Smillert struct stat sb;
2382904a520Smillert
239150f0d6bShalex /* Convert potential duid into a device name */
240150f0d6bShalex if ((diskfd = opendev(argv[i], O_RDONLY | O_NOFOLLOW, 0,
241150f0d6bShalex &realpath)) >= 0) {
242150f0d6bShalex argv[i] = strdup(realpath);
243150f0d6bShalex if (argv[i] == NULL) {
244150f0d6bShalex msg("Cannot malloc realpath\n");
245150f0d6bShalex exit(X_STARTUP);
246150f0d6bShalex }
247150f0d6bShalex (void)close(diskfd);
248150f0d6bShalex }
2492904a520Smillert if (lstat(argv[i], &sb) == -1) {
2502904a520Smillert msg("Cannot lstat %s: %s\n", argv[i], strerror(errno));
25130715473Sderaadt exit(X_STARTUP);
252df930be7Sderaadt }
2532904a520Smillert if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode))
2542904a520Smillert break;
2552904a520Smillert if (statfs(argv[i], &fsbuf) == -1) {
2562904a520Smillert msg("Cannot statfs %s: %s\n", argv[i], strerror(errno));
25730715473Sderaadt exit(X_STARTUP);
2582904a520Smillert }
2592904a520Smillert if (strcmp(argv[i], fsbuf.f_mntonname) == 0) {
2602904a520Smillert if (dirlist != 0) {
2612904a520Smillert msg("Can't dump a mountpoint and a filelist\n");
26230715473Sderaadt exit(X_STARTUP);
2632904a520Smillert }
2642904a520Smillert break; /* exit if sole mountpoint */
2652904a520Smillert }
2662904a520Smillert if (!disk) {
2672904a520Smillert if ((toplevel = strdup(fsbuf.f_mntonname)) == NULL) {
2682904a520Smillert msg("Cannot malloc diskname\n");
26930715473Sderaadt exit(X_STARTUP);
2702904a520Smillert }
2712904a520Smillert disk = toplevel;
2722904a520Smillert if (uflag) {
2732904a520Smillert msg("Ignoring u flag for subdir dump\n");
2742904a520Smillert uflag = 0;
2752904a520Smillert }
2762904a520Smillert if (level > '0') {
2772904a520Smillert msg("Subdir dump is done at level 0\n");
2782904a520Smillert level = '0';
2792904a520Smillert }
2802904a520Smillert msg("Dumping sub files/directories from %s\n", disk);
2812904a520Smillert } else {
2822904a520Smillert if (strcmp(disk, fsbuf.f_mntonname) != 0) {
2832904a520Smillert msg("%s is not on %s\n", argv[i], disk);
28430715473Sderaadt exit(X_STARTUP);
2852904a520Smillert }
2862904a520Smillert }
2872904a520Smillert msg("Dumping file/directory %s\n", argv[i]);
2882904a520Smillert dirlist++;
2892904a520Smillert }
2902904a520Smillert if (dirlist == 0) {
2912904a520Smillert disk = *argv++;
2922904a520Smillert if (argc != 1) {
2932904a520Smillert (void)fputs("Excess arguments to dump:", stderr);
2942904a520Smillert while (--argc) {
2952904a520Smillert (void)putc(' ', stderr);
2962904a520Smillert (void)fputs(*argv++, stderr);
2972904a520Smillert }
2982904a520Smillert (void)putc('\n', stderr);
29930715473Sderaadt exit(X_STARTUP);
3002904a520Smillert }
3012904a520Smillert }
302df930be7Sderaadt if (Tflag && uflag) {
303df930be7Sderaadt (void)fprintf(stderr,
304df930be7Sderaadt "You cannot use the T and u flags together.\n");
30530715473Sderaadt exit(X_STARTUP);
306df930be7Sderaadt }
307df930be7Sderaadt if (strcmp(tape, "-") == 0) {
308df930be7Sderaadt pipeout++;
309df930be7Sderaadt tape = "standard output";
310df930be7Sderaadt }
311df930be7Sderaadt
312df930be7Sderaadt if (blocksperfile)
313df930be7Sderaadt blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
31431ebca96Sderaadt else if (!unlimited) {
315df930be7Sderaadt /*
316df930be7Sderaadt * Determine how to default tape size and density
317df930be7Sderaadt *
318df930be7Sderaadt * density tape size
319df930be7Sderaadt * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
320df930be7Sderaadt * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
3217833183cSguenther * cartridge 8000 bpi (800 bytes/.1") 1700 ft.
322df930be7Sderaadt * (450*4 - slop)
323df930be7Sderaadt */
324df930be7Sderaadt if (density == 0)
325df930be7Sderaadt density = cartridge ? 100 : 160;
326df930be7Sderaadt if (tsize == 0)
327df930be7Sderaadt tsize = cartridge ? 1700L*120L : 2300L*120L;
328df930be7Sderaadt }
329df930be7Sderaadt
330df930be7Sderaadt if (strchr(tape, ':')) {
331df930be7Sderaadt host = tape;
332df930be7Sderaadt tape = strchr(host, ':');
333df930be7Sderaadt *tape++ = '\0';
334df930be7Sderaadt #ifdef RDUMP
335df930be7Sderaadt if (rmthost(host) == 0)
33630715473Sderaadt exit(X_STARTUP);
337df930be7Sderaadt #else
338df930be7Sderaadt (void)fprintf(stderr, "remote dump not enabled\n");
33930715473Sderaadt exit(X_STARTUP);
340df930be7Sderaadt #endif
341df930be7Sderaadt }
342df930be7Sderaadt
343df930be7Sderaadt if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
344df930be7Sderaadt signal(SIGHUP, sig);
345df930be7Sderaadt if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
346df930be7Sderaadt signal(SIGTERM, sig);
347df930be7Sderaadt if (signal(SIGINT, interrupt) == SIG_IGN)
348df930be7Sderaadt signal(SIGINT, SIG_IGN);
349df930be7Sderaadt
350df930be7Sderaadt getfstab(); /* /etc/fstab snarfed */
3512904a520Smillert
352df930be7Sderaadt /*
353df930be7Sderaadt * disk can be either the full special file name,
354df930be7Sderaadt * the suffix of the special file name,
355df930be7Sderaadt * the special name missing the leading '/',
356df930be7Sderaadt * the file system name with or without the leading '/'.
357df930be7Sderaadt */
3580c2b89afSmillert if (!statfs(disk, &fsbuf) && !strcmp(fsbuf.f_mntonname, disk)) {
3590c2b89afSmillert /* mounted disk? */
3600c2b89afSmillert disk = rawname(fsbuf.f_mntfromname);
3610d2e4232Smickey if (!disk) {
3620d2e4232Smickey (void)fprintf(stderr, "cannot get raw name for %s\n",
3630d2e4232Smickey fsbuf.f_mntfromname);
3640d2e4232Smickey exit(X_STARTUP);
3650d2e4232Smickey }
36640c0653dSmillert mount_point = fsbuf.f_mntonname;
3670c2b89afSmillert (void)strlcpy(spcl.c_dev, fsbuf.f_mntfromname,
3680c2b89afSmillert sizeof(spcl.c_dev));
3690c2b89afSmillert if (dirlist != 0) {
3700c2b89afSmillert (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
37140c0653dSmillert "a subset of %s", mount_point);
3720c2b89afSmillert } else {
37340c0653dSmillert (void)strlcpy(spcl.c_filesys, mount_point,
3740c2b89afSmillert sizeof(spcl.c_filesys));
3750c2b89afSmillert }
3760c2b89afSmillert } else if ((dt = fstabsearch(disk)) != NULL) {
3770c2b89afSmillert /* in fstab? */
3787833183cSguenther if (strchr(dt->fs_spec, '/')) {
3797833183cSguenther /* fs_spec is a /dev/something */
380df930be7Sderaadt disk = rawname(dt->fs_spec);
3817833183cSguenther } else {
3827833183cSguenther /* fs_spec is a DUID */
3837833183cSguenther disk = rawname(disk);
3847833183cSguenther }
38540c0653dSmillert mount_point = dt->fs_file;
3860c2b89afSmillert (void)strlcpy(spcl.c_dev, dt->fs_spec, sizeof(spcl.c_dev));
3872904a520Smillert if (dirlist != 0) {
3882904a520Smillert (void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
38940c0653dSmillert "a subset of %s", mount_point);
390df930be7Sderaadt } else {
39140c0653dSmillert (void)strlcpy(spcl.c_filesys, mount_point,
3920c2b89afSmillert sizeof(spcl.c_filesys));
393df930be7Sderaadt }
3942904a520Smillert } else {
3950c2b89afSmillert /* must be a device */
3960c2b89afSmillert (void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
3970c2b89afSmillert (void)strlcpy(spcl.c_filesys, "an unlisted file system",
3980c2b89afSmillert sizeof(spcl.c_filesys));
3992904a520Smillert }
4000c2b89afSmillert (void)strlcpy(spcl.c_label, "none", sizeof(spcl.c_label));
4012904a520Smillert (void)gethostname(spcl.c_host, sizeof(spcl.c_host));
402df930be7Sderaadt spcl.c_level = level - '0';
403df930be7Sderaadt spcl.c_type = TS_TAPE;
404150f0d6bShalex
405df69c215Sderaadt if ((diskfd = open(disk, O_RDONLY)) == -1) {
406150f0d6bShalex msg("Cannot open %s\n", disk);
407150f0d6bShalex exit(X_STARTUP);
408150f0d6bShalex }
409df69c215Sderaadt if (ioctl(diskfd, DIOCGDINFO, (char *)&lab) == -1)
410150f0d6bShalex err(1, "ioctl (DIOCGDINFO)");
411352e3770Sguenther
412352e3770Sguenther if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
413352e3770Sguenther if (asprintf(&duid,
414150f0d6bShalex "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
415150f0d6bShalex lab.d_uid[0], lab.d_uid[1], lab.d_uid[2], lab.d_uid[3],
416150f0d6bShalex lab.d_uid[4], lab.d_uid[5], lab.d_uid[6], lab.d_uid[7],
417150f0d6bShalex disk[strlen(disk)-1]) == -1) {
418150f0d6bShalex msg("Cannot malloc duid\n");
419150f0d6bShalex exit(X_STARTUP);
420150f0d6bShalex }
421352e3770Sguenther }
422df930be7Sderaadt if (!Tflag)
423df930be7Sderaadt getdumptime(); /* /etc/dumpdates snarfed */
424df930be7Sderaadt
4250155e653Smillert t = (time_t)spcl.c_date;
426*a7b9eedcSflorian ct = ctime(&t);
427df930be7Sderaadt msg("Date of this level %c dump: %s", level,
428*a7b9eedcSflorian t == 0 ? "the epoch\n" : ct ? ct : "?\n");
4290155e653Smillert t = (time_t)spcl.c_ddate;
430*a7b9eedcSflorian ct = ctime(&t);
431df930be7Sderaadt msg("Date of last level %c dump: %s", lastlevel,
432*a7b9eedcSflorian t == 0 ? "the epoch\n" : ct ? ct : "?\n");
433df930be7Sderaadt msg("Dumping %s ", disk);
43440c0653dSmillert if (mount_point != NULL)
43540c0653dSmillert msgtail("(%s) ", mount_point);
436df930be7Sderaadt if (host)
437df930be7Sderaadt msgtail("to %s on host %s\n", tape, host);
438df930be7Sderaadt else
439df930be7Sderaadt msgtail("to %s\n", tape);
440df930be7Sderaadt
441df69c215Sderaadt if (ioctl(diskfd, DIOCGPDINFO, (char *)&lab) == -1)
442b39ffb6cSkrw err(1, "ioctl (DIOCGPDINFO)");
443df930be7Sderaadt sync();
444df930be7Sderaadt sblock = (struct fs *)sblock_buf;
4450155e653Smillert for (i = 0; sblock_try[i] != -1; i++) {
4460155e653Smillert ssize_t n = pread(diskfd, sblock, SBLOCKSIZE,
4470155e653Smillert (off_t)sblock_try[i]);
4480155e653Smillert if (n == SBLOCKSIZE && (sblock->fs_magic == FS_UFS1_MAGIC ||
4490155e653Smillert (sblock->fs_magic == FS_UFS2_MAGIC &&
4500155e653Smillert sblock->fs_sblockloc == sblock_try[i])) &&
4510155e653Smillert sblock->fs_bsize <= MAXBSIZE &&
4520155e653Smillert sblock->fs_bsize >= sizeof(struct fs))
4530155e653Smillert break;
4540155e653Smillert }
4550155e653Smillert if (sblock_try[i] == -1)
4560155e653Smillert quit("Cannot find filesystem superblock\n");
457df930be7Sderaadt tp_bshift = ffs(TP_BSIZE) - 1;
458df930be7Sderaadt if (TP_BSIZE != (1 << tp_bshift))
45999ea2379Sderaadt quit("TP_BSIZE (%d) is not a power of 2\n", TP_BSIZE);
4600155e653Smillert if (sblock->fs_magic == FS_UFS2_MAGIC ||
4610155e653Smillert sblock->fs_inodefmt >= FS_44INODEFMT)
462df930be7Sderaadt spcl.c_flags |= DR_NEWINODEFMT;
463d16fc9bbSguenther maxino = (ino_t)sblock->fs_ipg * sblock->fs_ncg;
464df930be7Sderaadt mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
4655ae94ef8Sderaadt usedinomap = calloc((unsigned) mapsize, sizeof(char));
4665ae94ef8Sderaadt dumpdirmap = calloc((unsigned) mapsize, sizeof(char));
4675ae94ef8Sderaadt dumpinomap = calloc((unsigned) mapsize, sizeof(char));
4685f231c1eSotto if (usedinomap == NULL || dumpdirmap == NULL || dumpinomap == NULL)
4695f231c1eSotto quit("Failed to allocate tables");
4705f231c1eSotto
471df930be7Sderaadt tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
472df930be7Sderaadt
473df930be7Sderaadt nonodump = spcl.c_level < honorlevel;
474df930be7Sderaadt
4752904a520Smillert (void)signal(SIGINFO, statussig);
4762904a520Smillert
477df930be7Sderaadt msg("mapping (Pass I) [regular files]\n");
4782904a520Smillert anydirskipped = mapfiles(maxino, &tapesize, toplevel,
4792904a520Smillert (dirlist ? argv : NULL));
480df930be7Sderaadt
481df930be7Sderaadt msg("mapping (Pass II) [directories]\n");
482df930be7Sderaadt while (anydirskipped) {
483df930be7Sderaadt anydirskipped = mapdirs(maxino, &tapesize);
484df930be7Sderaadt }
485df930be7Sderaadt
48631ebca96Sderaadt if (pipeout || unlimited) {
487df930be7Sderaadt tapesize += 10; /* 10 trailer blocks */
4880155e653Smillert msg("estimated %lld tape blocks.\n", tapesize);
489df930be7Sderaadt } else {
490df930be7Sderaadt double fetapes;
491df930be7Sderaadt
492df930be7Sderaadt if (blocksperfile)
493df930be7Sderaadt fetapes = (double) tapesize / blocksperfile;
494df930be7Sderaadt else if (cartridge) {
495df930be7Sderaadt /* Estimate number of tapes, assuming streaming stops at
496df930be7Sderaadt the end of each block written, and not in mid-block.
497df930be7Sderaadt Assume no erroneous blocks; this can be compensated
498df930be7Sderaadt for with an artificially low tape size. */
499df930be7Sderaadt fetapes =
500df930be7Sderaadt ( tapesize /* blocks */
501df930be7Sderaadt * TP_BSIZE /* bytes/block */
502df930be7Sderaadt * (1.0/density) /* 0.1" / byte */
503df930be7Sderaadt +
504df930be7Sderaadt tapesize /* blocks */
505df930be7Sderaadt * (1.0/ntrec) /* streaming-stops per block */
506df930be7Sderaadt * 15.48 /* 0.1" / streaming-stop */
507df930be7Sderaadt ) * (1.0 / tsize ); /* tape / 0.1" */
508df930be7Sderaadt } else {
509df930be7Sderaadt /* Estimate number of tapes, for old fashioned 9-track
510df930be7Sderaadt tape */
511df930be7Sderaadt int tenthsperirg = (density == 625) ? 3 : 7;
512df930be7Sderaadt fetapes =
513df930be7Sderaadt ( tapesize /* blocks */
514df930be7Sderaadt * TP_BSIZE /* bytes / block */
515df930be7Sderaadt * (1.0/density) /* 0.1" / byte */
516df930be7Sderaadt +
517df930be7Sderaadt tapesize /* blocks */
518df930be7Sderaadt * (1.0/ntrec) /* IRG's / block */
519df930be7Sderaadt * tenthsperirg /* 0.1" / IRG */
520df930be7Sderaadt ) * (1.0 / tsize ); /* tape / 0.1" */
521df930be7Sderaadt }
522df930be7Sderaadt etapes = fetapes; /* truncating assignment */
523df930be7Sderaadt etapes++;
524df930be7Sderaadt /* count the dumped inodes map on each additional tape */
525df930be7Sderaadt tapesize += (etapes - 1) *
526df930be7Sderaadt (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
527df930be7Sderaadt tapesize += etapes + 10; /* headers + 10 trailer blks */
5280155e653Smillert msg("estimated %lld tape blocks on %3.2f tape(s).\n",
529df930be7Sderaadt tapesize, fetapes);
530df930be7Sderaadt }
531df930be7Sderaadt
532df930be7Sderaadt /*
5330b5a38dcSstephan * Exit if user wants an estimate of blocks and # of tapes only.
5340b5a38dcSstephan */
5350b5a38dcSstephan if (just_estimate)
5360b5a38dcSstephan exit(X_FINOK);
5370b5a38dcSstephan
5380b5a38dcSstephan /*
539df930be7Sderaadt * Allocate tape buffer.
540df930be7Sderaadt */
541df930be7Sderaadt if (!alloctape())
542df930be7Sderaadt quit("can't allocate tape buffers - try a smaller blocking factor.\n");
543df930be7Sderaadt
544df930be7Sderaadt startnewtape(1);
54591edd796Sderaadt (void)time(&tstart_writing);
5462904a520Smillert xferrate = 0;
547df930be7Sderaadt dumpmap(usedinomap, TS_CLRI, maxino - 1);
548df930be7Sderaadt
549df930be7Sderaadt msg("dumping (Pass III) [directories]\n");
550df930be7Sderaadt dirty = 0; /* XXX just to get gcc to shut up */
551df930be7Sderaadt for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
552df930be7Sderaadt if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
553df930be7Sderaadt dirty = *map++;
554df930be7Sderaadt else
555df930be7Sderaadt dirty >>= 1;
556df930be7Sderaadt if ((dirty & 1) == 0)
557df930be7Sderaadt continue;
558df930be7Sderaadt /*
559df930be7Sderaadt * Skip directory inodes deleted and maybe reallocated
560df930be7Sderaadt */
5610155e653Smillert dp = getino(ino, &mode);
5620155e653Smillert if (mode != IFDIR)
563df930be7Sderaadt continue;
564df930be7Sderaadt (void)dumpino(dp, ino);
565df930be7Sderaadt }
566df930be7Sderaadt
567df930be7Sderaadt msg("dumping (Pass IV) [regular files]\n");
568df930be7Sderaadt for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
569df930be7Sderaadt if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
570df930be7Sderaadt dirty = *map++;
571df930be7Sderaadt else
572df930be7Sderaadt dirty >>= 1;
573df930be7Sderaadt if ((dirty & 1) == 0)
574df930be7Sderaadt continue;
575df930be7Sderaadt /*
576df930be7Sderaadt * Skip inodes deleted and reallocated as directories.
577df930be7Sderaadt */
5780155e653Smillert dp = getino(ino, &mode);
579df930be7Sderaadt if (mode == IFDIR)
580df930be7Sderaadt continue;
581df930be7Sderaadt (void)dumpino(dp, ino);
582df930be7Sderaadt }
583df930be7Sderaadt
584df930be7Sderaadt spcl.c_type = TS_END;
585df930be7Sderaadt for (i = 0; i < ntrec; i++)
586df930be7Sderaadt writeheader(maxino - 1);
587df930be7Sderaadt if (pipeout)
5880155e653Smillert msg("%lld tape blocks\n", spcl.c_tapea);
589df930be7Sderaadt else
5900155e653Smillert msg("%lld tape blocks on %d volume%s\n",
5914a7a02b7Sderaadt spcl.c_tapea, spcl.c_volume,
5922904a520Smillert (spcl.c_volume == 1) ? "" : "s");
5930155e653Smillert t = (time_t)spcl.c_date;
594*a7b9eedcSflorian ct = ctime(&t);
5952904a520Smillert msg("Date of this level %c dump: %s", level,
596*a7b9eedcSflorian t == 0 ? "the epoch\n" : ct ? ct : "?\n");
5970155e653Smillert t = do_stats();
598*a7b9eedcSflorian ct = ctime(&t);
599*a7b9eedcSflorian msg("Date this dump completed: %s", ct ? ct : "?\n");
6002904a520Smillert msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno);
601df930be7Sderaadt putdumptime();
602df930be7Sderaadt trewind();
603df930be7Sderaadt broadcast("DUMP IS DONE!\7\7\n");
604df930be7Sderaadt msg("DUMP IS DONE\n");
605df930be7Sderaadt Exit(X_FINOK);
606df930be7Sderaadt /* NOTREACHED */
607df930be7Sderaadt }
608df930be7Sderaadt
609df930be7Sderaadt static void
usage(void)61053fedb49Sderaadt usage(void)
611df930be7Sderaadt {
6128f2b5575Smickey extern char *__progname;
613df930be7Sderaadt
614352e3770Sguenther (void)fprintf(stderr, "usage: %s [-0123456789acnSuWw] [-B records] "
615fa6f37eaSjmc "[-b blocksize] [-d density]\n"
616fa6f37eaSjmc "\t[-f file] [-h level] [-s feet] "
617fa6f37eaSjmc "[-T date] files-to-dump\n",
618fa6f37eaSjmc __progname);
61930715473Sderaadt exit(X_STARTUP);
620df930be7Sderaadt }
621df930be7Sderaadt
622df930be7Sderaadt /*
623df930be7Sderaadt * Pick up a numeric argument. It must be nonnegative and in the given
624df930be7Sderaadt * range (except that a vmax of 0 means unlimited).
625df930be7Sderaadt */
6260603cbefSnaddy static long long
numarg(char * meaning,long long vmin,long long vmax)6270603cbefSnaddy numarg(char *meaning, long long vmin, long long vmax)
628df930be7Sderaadt {
6290603cbefSnaddy long long val;
630ba265c57Smoritz const char *errstr;
631df930be7Sderaadt
632ba265c57Smoritz if (vmax == 0)
6330603cbefSnaddy vmax = LLONG_MAX;
634ba265c57Smoritz val = strtonum(optarg, vmin, vmax, &errstr);
635ba265c57Smoritz if (errstr)
6360603cbefSnaddy errx(X_STARTUP, "%s is %s [%lld - %lld]",
637ba265c57Smoritz meaning, errstr, vmin, vmax);
638ba265c57Smoritz
639df930be7Sderaadt return (val);
640df930be7Sderaadt }
641df930be7Sderaadt
642df930be7Sderaadt void
sig(int signo)64353fedb49Sderaadt sig(int signo)
644df930be7Sderaadt {
645df930be7Sderaadt switch(signo) {
646df930be7Sderaadt case SIGALRM:
647df930be7Sderaadt case SIGHUP:
648df930be7Sderaadt case SIGTERM:
649266deac0Sderaadt /* XXX signal race */
650df930be7Sderaadt if (pipeout)
651df930be7Sderaadt quit("Signal on pipe: cannot recover\n");
652df930be7Sderaadt msg("Rewriting attempted as response to unknown signal.\n");
653df930be7Sderaadt (void)fflush(stderr);
654df930be7Sderaadt (void)fflush(stdout);
655df930be7Sderaadt close_rewind();
656df930be7Sderaadt exit(X_REWRITE);
657df930be7Sderaadt /* NOTREACHED */
658df930be7Sderaadt }
659df930be7Sderaadt }
660df930be7Sderaadt
661df930be7Sderaadt char *
rawname(char * cp)66253fedb49Sderaadt rawname(char *cp)
663df930be7Sderaadt {
664b9fc9a72Sderaadt static char rawbuf[PATH_MAX];
665df930be7Sderaadt char *dp = strrchr(cp, '/');
6667833183cSguenther char *prefix;
667df930be7Sderaadt
668df930be7Sderaadt if (dp == NULL)
669df930be7Sderaadt return (NULL);
6707833183cSguenther prefix = dp[1] == 'r' ? "" : "r";
671df930be7Sderaadt *dp = '\0';
6727833183cSguenther (void)snprintf(rawbuf, sizeof(rawbuf), "%s/%s%s", cp, prefix, dp + 1);
673df930be7Sderaadt *dp = '/';
674df930be7Sderaadt return (rawbuf);
675df930be7Sderaadt }
676df930be7Sderaadt
6777833183cSguenther char *
getduid(char * path)6787833183cSguenther getduid(char *path)
6797833183cSguenther {
6807833183cSguenther int fd;
6817833183cSguenther struct disklabel lab;
6827833183cSguenther u_int64_t zero_uid = 0;
6837833183cSguenther char *duid;
6847833183cSguenther
6857833183cSguenther if ((fd = opendev(path, O_RDONLY | O_NOFOLLOW, 0, NULL)) >= 0) {
686df69c215Sderaadt if (ioctl(fd, DIOCGDINFO, (char *)&lab) == -1) {
6877833183cSguenther close(fd);
6887833183cSguenther warn("ioctl(DIOCGDINFO)");
6897833183cSguenther return (NULL);
6907833183cSguenther }
6917833183cSguenther close(fd);
6927833183cSguenther
6937833183cSguenther if (memcmp(lab.d_uid, &zero_uid, sizeof(lab.d_uid)) != 0) {
6947833183cSguenther if (asprintf(&duid,
6957833183cSguenther "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c",
6967833183cSguenther lab.d_uid[0], lab.d_uid[1], lab.d_uid[2],
6977833183cSguenther lab.d_uid[3], lab.d_uid[4], lab.d_uid[5],
6987833183cSguenther lab.d_uid[6], lab.d_uid[7],
6997833183cSguenther path[strlen(path)-1]) == -1) {
7007833183cSguenther warn("Cannot malloc duid");
7017833183cSguenther return (NULL);
7027833183cSguenther }
7037833183cSguenther return (duid);
7047833183cSguenther }
7057833183cSguenther }
7067833183cSguenther
7077833183cSguenther return (NULL);
7087833183cSguenther }
7097833183cSguenther
710df930be7Sderaadt /*
711df930be7Sderaadt * obsolete --
712df930be7Sderaadt * Change set of key letters and ordered arguments into something
713df930be7Sderaadt * getopt(3) will like.
714df930be7Sderaadt */
715df930be7Sderaadt static void
obsolete(int * argcp,char ** argvp[])71653fedb49Sderaadt obsolete(int *argcp, char **argvp[])
717df930be7Sderaadt {
718df930be7Sderaadt int argc, flags;
719df930be7Sderaadt char *ap, **argv, *flagsp, **nargv, *p;
720f41a3dd7Sderaadt size_t len;
721df930be7Sderaadt
722df930be7Sderaadt /* Setup. */
723df930be7Sderaadt argv = *argvp;
724df930be7Sderaadt argc = *argcp;
725df930be7Sderaadt
726ed108a78Smillert /* Return if no args or first argument has leading dash or a slash. */
727df930be7Sderaadt ap = argv[1];
728ed108a78Smillert if (argc == 1 || *ap == '-' || strchr(ap, '/') != NULL)
729df930be7Sderaadt return;
730df930be7Sderaadt
731df930be7Sderaadt /* Allocate space for new arguments. */
7321ed98fdfSderaadt if ((*argvp = nargv = calloc(argc + 1, sizeof(char *))) == NULL ||
733df930be7Sderaadt (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
734df930be7Sderaadt err(1, NULL);
735df930be7Sderaadt
736df930be7Sderaadt *nargv++ = *argv;
737df930be7Sderaadt argv += 2;
738df930be7Sderaadt
739df930be7Sderaadt for (flags = 0; *ap; ++ap) {
740df930be7Sderaadt switch (*ap) {
741df930be7Sderaadt case 'B':
742df930be7Sderaadt case 'b':
743df930be7Sderaadt case 'd':
744df930be7Sderaadt case 'f':
745df930be7Sderaadt case 'h':
746df930be7Sderaadt case 's':
747df930be7Sderaadt case 'T':
748df930be7Sderaadt if (*argv == NULL) {
749df930be7Sderaadt warnx("option requires an argument -- %c", *ap);
750df930be7Sderaadt usage();
751df930be7Sderaadt }
752f41a3dd7Sderaadt len = 2 + strlen(*argv) + 1;
753f41a3dd7Sderaadt if ((nargv[0] = malloc(len)) == NULL)
754df930be7Sderaadt err(1, NULL);
755df930be7Sderaadt nargv[0][0] = '-';
756df930be7Sderaadt nargv[0][1] = *ap;
757f41a3dd7Sderaadt (void)strlcpy(&nargv[0][2], *argv, len - 2);
758df930be7Sderaadt ++argv;
759df930be7Sderaadt ++nargv;
760df930be7Sderaadt break;
761df930be7Sderaadt default:
762df930be7Sderaadt if (!flags) {
763df930be7Sderaadt *p++ = '-';
764df930be7Sderaadt flags = 1;
765df930be7Sderaadt }
766df930be7Sderaadt *p++ = *ap;
767df930be7Sderaadt break;
768df930be7Sderaadt }
769df930be7Sderaadt }
770df930be7Sderaadt
771eb1d5d5eSderaadt /* Terminate flags, or toss the buffer we did not use. */
772df930be7Sderaadt if (flags) {
773df930be7Sderaadt *p = '\0';
774df930be7Sderaadt *nargv++ = flagsp;
775eb1d5d5eSderaadt } else
776eb1d5d5eSderaadt free(flagsp);
777df930be7Sderaadt
778df930be7Sderaadt /* Copy remaining arguments. */
779d2d7fbebSderaadt while ((*nargv++ = *argv++))
780252fb110Stedu continue;
781df930be7Sderaadt
782df930be7Sderaadt /* Update argument count. */
783df930be7Sderaadt *argcp = nargv - *argvp - 1;
784df930be7Sderaadt }
785