main.c revision 1.37 1 1.37 david /* $NetBSD: main.c,v 1.37 2001/08/08 16:49:54 david Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.3 mycroft * Copyright (c) 1980, 1991, 1993, 1994
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.15 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 1.15 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.6 cgd #if 0
44 1.16 lukem static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
45 1.6 cgd #else
46 1.37 david __RCSID("$NetBSD: main.c,v 1.37 2001/08/08 16:49:54 david Exp $");
47 1.6 cgd #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.3 mycroft #include <sys/param.h>
51 1.3 mycroft #include <sys/time.h>
52 1.14 lukem #include <sys/stat.h>
53 1.14 lukem #include <sys/mount.h>
54 1.14 lukem
55 1.1 cgd #ifdef sunos
56 1.3 mycroft #include <sys/vnode.h>
57 1.3 mycroft
58 1.3 mycroft #include <ufs/inode.h>
59 1.1 cgd #include <ufs/fs.h>
60 1.1 cgd #else
61 1.16 lukem #include <ufs/ufs/dinode.h>
62 1.3 mycroft #include <ufs/ffs/fs.h>
63 1.18 bouyer #include <ufs/ffs/ffs_extern.h>
64 1.1 cgd #endif
65 1.3 mycroft
66 1.1 cgd #include <protocols/dumprestore.h>
67 1.3 mycroft
68 1.3 mycroft #include <ctype.h>
69 1.5 mycroft #include <err.h>
70 1.3 mycroft #include <errno.h>
71 1.1 cgd #include <fcntl.h>
72 1.1 cgd #include <fstab.h>
73 1.3 mycroft #include <signal.h>
74 1.1 cgd #include <stdio.h>
75 1.1 cgd #include <stdlib.h>
76 1.1 cgd #include <string.h>
77 1.10 lukem #include <time.h>
78 1.3 mycroft #include <unistd.h>
79 1.3 mycroft
80 1.1 cgd #include "dump.h"
81 1.1 cgd #include "pathnames.h"
82 1.1 cgd
83 1.37 david gid_t egid; /* Retain tty privs for notification */
84 1.27 scw int notify; /* notify operator flag */
85 1.27 scw int blockswritten; /* number of blocks written on current tape */
86 1.27 scw int tapeno; /* current tape number */
87 1.27 scw int density; /* density in bytes/0.1" */
88 1.17 lukem int ntrec = NTREC; /* # tape blocks in each tape record */
89 1.27 scw int cartridge; /* Assume non-cartridge tape */
90 1.17 lukem long dev_bsize = 1; /* recalculated below */
91 1.27 scw long blocksperfile; /* output blocks per file */
92 1.27 scw char *host; /* remote host (if any) */
93 1.22 bouyer int readcache = -1; /* read cache size (in readblksize blks) */
94 1.22 bouyer int readblksize = 32 * 1024; /* read block size */
95 1.1 cgd
96 1.31 lukem int main(int, char *[]);
97 1.31 lukem static long numarg(char *, long, long);
98 1.31 lukem static void obsolete(int *, char **[]);
99 1.31 lukem static void usage(void);
100 1.3 mycroft
101 1.3 mycroft int
102 1.31 lukem main(int argc, char *argv[])
103 1.1 cgd {
104 1.11 lukem ino_t ino;
105 1.11 lukem int dirty;
106 1.11 lukem struct dinode *dp;
107 1.33 lukem struct fstab *dt;
108 1.33 lukem struct statfs *mntinfo, fsbuf;
109 1.11 lukem char *map;
110 1.11 lukem int ch;
111 1.3 mycroft int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
112 1.1 cgd ino_t maxino;
113 1.18 bouyer time_t tnow, date;
114 1.33 lukem int dirc;
115 1.33 lukem char *mountpoint;
116 1.17 lukem int just_estimate = 0;
117 1.21 lukem char labelstr[LBLSIZE];
118 1.1 cgd
119 1.1 cgd spcl.c_date = 0;
120 1.3 mycroft (void)time((time_t *)&spcl.c_date);
121 1.1 cgd
122 1.37 david /* Save setgid bit for use later */
123 1.37 david egid = getegid();
124 1.37 david setegid(getgid());
125 1.37 david
126 1.1 cgd tsize = 0; /* Default later, based on 'c' option for cart tapes */
127 1.7 mrg if ((tape = getenv("TAPE")) == NULL)
128 1.7 mrg tape = _PATH_DEFTAPE;
129 1.1 cgd dumpdates = _PATH_DUMPDATES;
130 1.1 cgd temp = _PATH_DTMP;
131 1.21 lukem strcpy(labelstr, "none"); /* XXX safe strcpy. */
132 1.1 cgd if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
133 1.1 cgd quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
134 1.1 cgd level = '0';
135 1.5 mycroft
136 1.5 mycroft if (argc < 2)
137 1.5 mycroft usage();
138 1.5 mycroft
139 1.5 mycroft obsolete(&argc, &argv);
140 1.17 lukem while ((ch = getopt(argc, argv,
141 1.28 tron "0123456789B:b:cd:ef:h:k:L:nr:s:ST:uWw")) != -1)
142 1.5 mycroft switch (ch) {
143 1.5 mycroft /* dump level */
144 1.5 mycroft case '0': case '1': case '2': case '3': case '4':
145 1.5 mycroft case '5': case '6': case '7': case '8': case '9':
146 1.5 mycroft level = ch;
147 1.3 mycroft break;
148 1.1 cgd
149 1.5 mycroft case 'B': /* blocks per output file */
150 1.5 mycroft blocksperfile = numarg("blocks per file", 1L, 0L);
151 1.5 mycroft break;
152 1.1 cgd
153 1.5 mycroft case 'b': /* blocks per tape write */
154 1.5 mycroft ntrec = numarg("blocks per write", 1L, 1000L);
155 1.9 mikel bflag = 1;
156 1.5 mycroft break;
157 1.1 cgd
158 1.5 mycroft case 'c': /* Tape is cart. not 9-track */
159 1.5 mycroft cartridge = 1;
160 1.3 mycroft break;
161 1.1 cgd
162 1.1 cgd case 'd': /* density, in bits per inch */
163 1.5 mycroft density = numarg("density", 10L, 327670L) / 10;
164 1.1 cgd if (density >= 625 && !bflag)
165 1.1 cgd ntrec = HIGHDENSITYTREC;
166 1.28 tron break;
167 1.28 tron
168 1.28 tron case 'e': /* eject full tapes */
169 1.28 tron eflag = 1;
170 1.3 mycroft break;
171 1.1 cgd
172 1.5 mycroft case 'f': /* output file */
173 1.5 mycroft tape = optarg;
174 1.5 mycroft break;
175 1.5 mycroft
176 1.5 mycroft case 'h':
177 1.5 mycroft honorlevel = numarg("honor level", 0L, 10L);
178 1.5 mycroft break;
179 1.5 mycroft
180 1.22 bouyer case 'k':
181 1.22 bouyer readblksize = numarg("read block size", 0, 64) * 1024;
182 1.22 bouyer break;
183 1.22 bouyer
184 1.21 lukem case 'L':
185 1.21 lukem /*
186 1.21 lukem * Note that although there are LBLSIZE characters,
187 1.21 lukem * the last must be '\0', so the limit on strlen()
188 1.21 lukem * is really LBLSIZE-1.
189 1.21 lukem */
190 1.21 lukem strncpy(labelstr, optarg, LBLSIZE);
191 1.21 lukem labelstr[LBLSIZE-1] = '\0';
192 1.21 lukem if (strlen(optarg) > LBLSIZE-1) {
193 1.21 lukem msg(
194 1.21 lukem "WARNING Label `%s' is larger than limit of %d characters.\n",
195 1.21 lukem optarg, LBLSIZE-1);
196 1.21 lukem msg("WARNING: Using truncated label `%s'.\n",
197 1.21 lukem labelstr);
198 1.21 lukem }
199 1.21 lukem break;
200 1.5 mycroft case 'n': /* notify operators */
201 1.5 mycroft notify = 1;
202 1.5 mycroft break;
203 1.5 mycroft
204 1.22 bouyer case 'r': /* read cache size */
205 1.22 bouyer readcache = numarg("read cache size", 0, 512);
206 1.22 bouyer break;
207 1.22 bouyer
208 1.1 cgd case 's': /* tape size, feet */
209 1.5 mycroft tsize = numarg("tape size", 1L, 0L) * 12 * 10;
210 1.3 mycroft break;
211 1.1 cgd
212 1.17 lukem case 'S': /* exit after estimating # of tapes */
213 1.17 lukem just_estimate = 1;
214 1.17 lukem break;
215 1.17 lukem
216 1.1 cgd case 'T': /* time of last dump */
217 1.5 mycroft spcl.c_ddate = unctime(optarg);
218 1.1 cgd if (spcl.c_ddate < 0) {
219 1.3 mycroft (void)fprintf(stderr, "bad time \"%s\"\n",
220 1.5 mycroft optarg);
221 1.3 mycroft exit(X_ABORT);
222 1.1 cgd }
223 1.3 mycroft Tflag = 1;
224 1.1 cgd lastlevel = '?';
225 1.3 mycroft break;
226 1.1 cgd
227 1.1 cgd case 'u': /* update /etc/dumpdates */
228 1.3 mycroft uflag = 1;
229 1.3 mycroft break;
230 1.1 cgd
231 1.5 mycroft case 'W': /* what to do */
232 1.5 mycroft case 'w':
233 1.5 mycroft lastdump(ch);
234 1.5 mycroft exit(0); /* do nothing else */
235 1.1 cgd
236 1.1 cgd default:
237 1.5 mycroft usage();
238 1.1 cgd }
239 1.5 mycroft argc -= optind;
240 1.5 mycroft argv += optind;
241 1.5 mycroft
242 1.1 cgd if (argc < 1) {
243 1.3 mycroft (void)fprintf(stderr, "Must specify disk or filesystem\n");
244 1.3 mycroft exit(X_ABORT);
245 1.1 cgd }
246 1.14 lukem
247 1.33 lukem
248 1.14 lukem /*
249 1.14 lukem * determine if disk is a subdirectory, and setup appropriately
250 1.14 lukem */
251 1.33 lukem getfstab(); /* /etc/fstab snarfed */
252 1.33 lukem disk = NULL;
253 1.33 lukem mountpoint = NULL;
254 1.33 lukem dirc = 0;
255 1.14 lukem for (i = 0; i < argc; i++) {
256 1.14 lukem struct stat sb;
257 1.14 lukem
258 1.33 lukem if (lstat(argv[i], &sb) == -1)
259 1.33 lukem quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
260 1.33 lukem if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
261 1.33 lukem disk = argv[i];
262 1.33 lukem multicheck:
263 1.33 lukem if (dirc != 0)
264 1.33 lukem quit(
265 1.33 lukem "Can't dump a mountpoint and a filelist\n");
266 1.14 lukem break;
267 1.14 lukem }
268 1.33 lukem if ((dt = fstabsearch(argv[i])) != NULL) {
269 1.33 lukem disk = dt->fs_spec;
270 1.34 hannken mountpoint = xstrdup(dt->fs_file);
271 1.33 lukem goto multicheck;
272 1.14 lukem }
273 1.33 lukem if (statfs(argv[i], &fsbuf) == -1)
274 1.33 lukem quit("Cannot statfs %s: %s\n", argv[i],
275 1.33 lukem strerror(errno));
276 1.33 lukem disk = fsbuf.f_mntfromname;
277 1.33 lukem if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
278 1.33 lukem goto multicheck;
279 1.33 lukem if (mountpoint == NULL) {
280 1.33 lukem mountpoint = xstrdup(fsbuf.f_mntonname);
281 1.14 lukem if (uflag) {
282 1.14 lukem msg("Ignoring u flag for subdir dump\n");
283 1.14 lukem uflag = 0;
284 1.14 lukem }
285 1.14 lukem if (level > '0') {
286 1.14 lukem msg("Subdir dump is done at level 0\n");
287 1.14 lukem level = '0';
288 1.14 lukem }
289 1.33 lukem msg("Dumping sub files/directories from %s\n",
290 1.33 lukem mountpoint);
291 1.14 lukem } else {
292 1.33 lukem if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
293 1.33 lukem quit("%s is not on %s\n", argv[i], mountpoint);
294 1.14 lukem }
295 1.14 lukem msg("Dumping file/directory %s\n", argv[i]);
296 1.33 lukem dirc++;
297 1.14 lukem }
298 1.33 lukem if (mountpoint)
299 1.33 lukem free(mountpoint);
300 1.33 lukem
301 1.33 lukem if (dirc == 0) {
302 1.33 lukem argv++;
303 1.14 lukem if (argc != 1) {
304 1.14 lukem (void)fprintf(stderr, "Excess arguments to dump:");
305 1.14 lukem while (--argc)
306 1.14 lukem (void)fprintf(stderr, " %s", *argv++);
307 1.14 lukem (void)fprintf(stderr, "\n");
308 1.14 lukem exit(X_ABORT);
309 1.14 lukem }
310 1.1 cgd }
311 1.1 cgd if (Tflag && uflag) {
312 1.3 mycroft (void)fprintf(stderr,
313 1.1 cgd "You cannot use the T and u flags together.\n");
314 1.3 mycroft exit(X_ABORT);
315 1.1 cgd }
316 1.1 cgd if (strcmp(tape, "-") == 0) {
317 1.1 cgd pipeout++;
318 1.1 cgd tape = "standard output";
319 1.1 cgd }
320 1.1 cgd
321 1.1 cgd if (blocksperfile)
322 1.1 cgd blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
323 1.1 cgd else {
324 1.1 cgd /*
325 1.1 cgd * Determine how to default tape size and density
326 1.1 cgd *
327 1.1 cgd * density tape size
328 1.1 cgd * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
329 1.1 cgd * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
330 1.1 cgd * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
331 1.1 cgd * (450*4 - slop)
332 1.1 cgd */
333 1.1 cgd if (density == 0)
334 1.1 cgd density = cartridge ? 100 : 160;
335 1.1 cgd if (tsize == 0)
336 1.1 cgd tsize = cartridge ? 1700L*120L : 2300L*120L;
337 1.1 cgd }
338 1.1 cgd
339 1.4 mycroft if (strchr(tape, ':')) {
340 1.1 cgd host = tape;
341 1.4 mycroft tape = strchr(host, ':');
342 1.3 mycroft *tape++ = '\0';
343 1.1 cgd #ifdef RDUMP
344 1.1 cgd if (rmthost(host) == 0)
345 1.3 mycroft exit(X_ABORT);
346 1.1 cgd #else
347 1.3 mycroft (void)fprintf(stderr, "remote dump not enabled\n");
348 1.3 mycroft exit(X_ABORT);
349 1.1 cgd #endif
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
353 1.1 cgd signal(SIGHUP, sig);
354 1.1 cgd if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
355 1.1 cgd signal(SIGTRAP, sig);
356 1.1 cgd if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
357 1.1 cgd signal(SIGFPE, sig);
358 1.1 cgd if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
359 1.1 cgd signal(SIGBUS, sig);
360 1.1 cgd if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
361 1.1 cgd signal(SIGSEGV, sig);
362 1.1 cgd if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
363 1.1 cgd signal(SIGTERM, sig);
364 1.1 cgd if (signal(SIGINT, interrupt) == SIG_IGN)
365 1.1 cgd signal(SIGINT, SIG_IGN);
366 1.1 cgd
367 1.1 cgd set_operators(); /* /etc/group snarfed */
368 1.14 lukem
369 1.1 cgd /*
370 1.33 lukem * disk can be either the full special file name, or
371 1.33 lukem * the file system name.
372 1.1 cgd */
373 1.33 lukem mountpoint = NULL;
374 1.33 lukem if ((dt = fstabsearch(disk)) != NULL) {
375 1.1 cgd disk = rawname(dt->fs_spec);
376 1.33 lukem mountpoint = dt->fs_file;
377 1.33 lukem msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
378 1.33 lukem } else if ((mntinfo = mntinfosearch(disk)) != NULL) {
379 1.33 lukem disk = rawname(mntinfo->f_mntfromname);
380 1.33 lukem mountpoint = mntinfo->f_mntonname;
381 1.33 lukem msg("Found %s on %s in mount table\n", disk, mountpoint);
382 1.33 lukem }
383 1.33 lukem if (mountpoint != NULL) {
384 1.33 lukem if (dirc != 0)
385 1.14 lukem (void)snprintf(spcl.c_filesys, NAMELEN,
386 1.33 lukem "a subset of %s", mountpoint);
387 1.14 lukem else
388 1.33 lukem (void)strncpy(spcl.c_filesys, mountpoint, NAMELEN);
389 1.1 cgd } else {
390 1.3 mycroft (void)strncpy(spcl.c_filesys, "an unlisted file system",
391 1.1 cgd NAMELEN);
392 1.1 cgd }
393 1.33 lukem (void)strncpy(spcl.c_dev, disk, NAMELEN);
394 1.21 lukem (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1);
395 1.3 mycroft (void)gethostname(spcl.c_host, NAMELEN);
396 1.19 mrg spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
397 1.18 bouyer
398 1.18 bouyer if ((diskfd = open(disk, O_RDONLY)) < 0) {
399 1.18 bouyer msg("Cannot open %s\n", disk);
400 1.18 bouyer exit(X_ABORT);
401 1.18 bouyer }
402 1.18 bouyer sync();
403 1.25 perseant
404 1.25 perseant needswap = fs_read_sblock(sblock_buf);
405 1.18 bouyer
406 1.18 bouyer spcl.c_level = iswap32(level - '0');
407 1.18 bouyer spcl.c_type = iswap32(TS_TAPE);
408 1.18 bouyer spcl.c_date = iswap32(spcl.c_date);
409 1.18 bouyer spcl.c_ddate = iswap32(spcl.c_ddate);
410 1.1 cgd if (!Tflag)
411 1.1 cgd getdumptime(); /* /etc/dumpdates snarfed */
412 1.1 cgd
413 1.18 bouyer date = iswap32(spcl.c_date);
414 1.1 cgd msg("Date of this level %c dump: %s", level,
415 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
416 1.18 bouyer date = iswap32(spcl.c_ddate);
417 1.1 cgd msg("Date of last level %c dump: %s", lastlevel,
418 1.18 bouyer spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
419 1.17 lukem msg("Dumping ");
420 1.33 lukem if (dirc != 0)
421 1.17 lukem msgtail("a subset of ");
422 1.17 lukem msgtail("%s (%s) ", disk, spcl.c_filesys);
423 1.1 cgd if (host)
424 1.1 cgd msgtail("to %s on host %s\n", tape, host);
425 1.1 cgd else
426 1.1 cgd msgtail("to %s\n", tape);
427 1.21 lukem msg("Label: %s\n", labelstr);
428 1.1 cgd
429 1.25 perseant ufsib = fs_parametrize();
430 1.25 perseant
431 1.1 cgd dev_bshift = ffs(dev_bsize) - 1;
432 1.1 cgd if (dev_bsize != (1 << dev_bshift))
433 1.26 briggs quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
434 1.1 cgd tp_bshift = ffs(TP_BSIZE) - 1;
435 1.1 cgd if (TP_BSIZE != (1 << tp_bshift))
436 1.1 cgd quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
437 1.25 perseant maxino = fs_maxino();
438 1.3 mycroft mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
439 1.33 lukem usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
440 1.33 lukem dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
441 1.33 lukem dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
442 1.1 cgd tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
443 1.1 cgd
444 1.18 bouyer nonodump = iswap32(spcl.c_level) < honorlevel;
445 1.3 mycroft
446 1.22 bouyer initcache(readcache, readblksize);
447 1.22 bouyer
448 1.14 lukem (void)signal(SIGINFO, statussig);
449 1.14 lukem
450 1.1 cgd msg("mapping (Pass I) [regular files]\n");
451 1.33 lukem anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
452 1.33 lukem (dirc ? argv : NULL));
453 1.1 cgd
454 1.1 cgd msg("mapping (Pass II) [directories]\n");
455 1.1 cgd while (anydirskipped) {
456 1.1 cgd anydirskipped = mapdirs(maxino, &tapesize);
457 1.1 cgd }
458 1.1 cgd
459 1.3 mycroft if (pipeout) {
460 1.1 cgd tapesize += 10; /* 10 trailer blocks */
461 1.3 mycroft msg("estimated %ld tape blocks.\n", tapesize);
462 1.3 mycroft } else {
463 1.3 mycroft double fetapes;
464 1.3 mycroft
465 1.1 cgd if (blocksperfile)
466 1.3 mycroft fetapes = (double) tapesize / blocksperfile;
467 1.1 cgd else if (cartridge) {
468 1.1 cgd /* Estimate number of tapes, assuming streaming stops at
469 1.1 cgd the end of each block written, and not in mid-block.
470 1.1 cgd Assume no erroneous blocks; this can be compensated
471 1.1 cgd for with an artificially low tape size. */
472 1.1 cgd fetapes =
473 1.1 cgd ( tapesize /* blocks */
474 1.1 cgd * TP_BSIZE /* bytes/block */
475 1.1 cgd * (1.0/density) /* 0.1" / byte */
476 1.1 cgd +
477 1.1 cgd tapesize /* blocks */
478 1.1 cgd * (1.0/ntrec) /* streaming-stops per block */
479 1.1 cgd * 15.48 /* 0.1" / streaming-stop */
480 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
481 1.1 cgd } else {
482 1.1 cgd /* Estimate number of tapes, for old fashioned 9-track
483 1.1 cgd tape */
484 1.1 cgd int tenthsperirg = (density == 625) ? 3 : 7;
485 1.1 cgd fetapes =
486 1.1 cgd ( tapesize /* blocks */
487 1.1 cgd * TP_BSIZE /* bytes / block */
488 1.1 cgd * (1.0/density) /* 0.1" / byte */
489 1.1 cgd +
490 1.1 cgd tapesize /* blocks */
491 1.1 cgd * (1.0/ntrec) /* IRG's / block */
492 1.1 cgd * tenthsperirg /* 0.1" / IRG */
493 1.1 cgd ) * (1.0 / tsize ); /* tape / 0.1" */
494 1.1 cgd }
495 1.1 cgd etapes = fetapes; /* truncating assignment */
496 1.1 cgd etapes++;
497 1.1 cgd /* count the dumped inodes map on each additional tape */
498 1.1 cgd tapesize += (etapes - 1) *
499 1.1 cgd (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
500 1.1 cgd tapesize += etapes + 10; /* headers + 10 trailer blks */
501 1.1 cgd msg("estimated %ld tape blocks on %3.2f tape(s).\n",
502 1.1 cgd tapesize, fetapes);
503 1.3 mycroft }
504 1.17 lukem /*
505 1.17 lukem * If the user only wants an estimate of the number of
506 1.17 lukem * tapes, exit now.
507 1.17 lukem */
508 1.17 lukem if (just_estimate)
509 1.17 lukem exit(0);
510 1.1 cgd
511 1.1 cgd /*
512 1.3 mycroft * Allocate tape buffer.
513 1.1 cgd */
514 1.1 cgd if (!alloctape())
515 1.1 cgd quit("can't allocate tape buffers - try a smaller blocking factor.\n");
516 1.1 cgd
517 1.1 cgd startnewtape(1);
518 1.3 mycroft (void)time((time_t *)&(tstart_writing));
519 1.10 lukem xferrate = 0;
520 1.3 mycroft dumpmap(usedinomap, TS_CLRI, maxino - 1);
521 1.1 cgd
522 1.1 cgd msg("dumping (Pass III) [directories]\n");
523 1.3 mycroft dirty = 0; /* XXX just to get gcc to shut up */
524 1.3 mycroft for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
525 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
526 1.1 cgd dirty = *map++;
527 1.1 cgd else
528 1.1 cgd dirty >>= 1;
529 1.1 cgd if ((dirty & 1) == 0)
530 1.1 cgd continue;
531 1.1 cgd /*
532 1.1 cgd * Skip directory inodes deleted and maybe reallocated
533 1.1 cgd */
534 1.1 cgd dp = getino(ino);
535 1.1 cgd if ((dp->di_mode & IFMT) != IFDIR)
536 1.1 cgd continue;
537 1.3 mycroft (void)dumpino(dp, ino);
538 1.1 cgd }
539 1.1 cgd
540 1.1 cgd msg("dumping (Pass IV) [regular files]\n");
541 1.3 mycroft for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
542 1.3 mycroft int mode;
543 1.3 mycroft
544 1.3 mycroft if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
545 1.1 cgd dirty = *map++;
546 1.1 cgd else
547 1.1 cgd dirty >>= 1;
548 1.1 cgd if ((dirty & 1) == 0)
549 1.1 cgd continue;
550 1.1 cgd /*
551 1.1 cgd * Skip inodes deleted and reallocated as directories.
552 1.1 cgd */
553 1.1 cgd dp = getino(ino);
554 1.3 mycroft mode = dp->di_mode & IFMT;
555 1.3 mycroft if (mode == IFDIR)
556 1.1 cgd continue;
557 1.3 mycroft (void)dumpino(dp, ino);
558 1.1 cgd }
559 1.1 cgd
560 1.18 bouyer spcl.c_type = iswap32(TS_END);
561 1.1 cgd for (i = 0; i < ntrec; i++)
562 1.3 mycroft writeheader(maxino - 1);
563 1.1 cgd if (pipeout)
564 1.26 briggs msg("%d tape blocks\n",iswap32(spcl.c_tapea));
565 1.1 cgd else
566 1.26 briggs msg("%d tape blocks on %d volume%s\n",
567 1.18 bouyer iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
568 1.18 bouyer (iswap32(spcl.c_volume) == 1) ? "" : "s");
569 1.10 lukem tnow = do_stats();
570 1.18 bouyer date = iswap32(spcl.c_date);
571 1.10 lukem msg("Date of this level %c dump: %s", level,
572 1.18 bouyer spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
573 1.10 lukem msg("Date this dump completed: %s", ctime(&tnow));
574 1.26 briggs msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
575 1.1 cgd putdumptime();
576 1.29 tron trewind(0);
577 1.1 cgd broadcast("DUMP IS DONE!\7\7\n");
578 1.1 cgd msg("DUMP IS DONE\n");
579 1.1 cgd Exit(X_FINOK);
580 1.1 cgd /* NOTREACHED */
581 1.15 lukem exit(X_FINOK); /* XXX: to satisfy gcc */
582 1.1 cgd }
583 1.1 cgd
584 1.5 mycroft static void
585 1.31 lukem usage(void)
586 1.5 mycroft {
587 1.5 mycroft
588 1.22 bouyer (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
589 1.30 tron "usage: dump [-0123456789cenu] [-B records] [-b blocksize] [-d density]",
590 1.22 bouyer " [-f file] [-h level] [-k read block size] [-L label]",
591 1.22 bouyer " [-r read cache size] [-s feet] [-T date] filesystem",
592 1.21 lukem " dump [-W | -w]");
593 1.5 mycroft exit(1);
594 1.5 mycroft }
595 1.5 mycroft
596 1.3 mycroft /*
597 1.3 mycroft * Pick up a numeric argument. It must be nonnegative and in the given
598 1.3 mycroft * range (except that a vmax of 0 means unlimited).
599 1.3 mycroft */
600 1.3 mycroft static long
601 1.32 lukem numarg(char *meaning, long vmin, long vmax)
602 1.3 mycroft {
603 1.5 mycroft char *p;
604 1.3 mycroft long val;
605 1.3 mycroft
606 1.5 mycroft val = strtol(optarg, &p, 10);
607 1.5 mycroft if (*p)
608 1.5 mycroft errx(1, "illegal %s -- %s", meaning, optarg);
609 1.3 mycroft if (val < vmin || (vmax && val > vmax))
610 1.5 mycroft errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
611 1.3 mycroft return (val);
612 1.3 mycroft }
613 1.3 mycroft
614 1.1 cgd void
615 1.32 lukem sig(int signo)
616 1.1 cgd {
617 1.32 lukem
618 1.1 cgd switch(signo) {
619 1.1 cgd case SIGALRM:
620 1.1 cgd case SIGBUS:
621 1.1 cgd case SIGFPE:
622 1.1 cgd case SIGHUP:
623 1.1 cgd case SIGTERM:
624 1.1 cgd case SIGTRAP:
625 1.1 cgd if (pipeout)
626 1.1 cgd quit("Signal on pipe: cannot recover\n");
627 1.35 mrg msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
628 1.3 mycroft (void)fflush(stderr);
629 1.3 mycroft (void)fflush(stdout);
630 1.1 cgd close_rewind();
631 1.3 mycroft exit(X_REWRITE);
632 1.1 cgd /* NOTREACHED */
633 1.1 cgd case SIGSEGV:
634 1.1 cgd msg("SIGSEGV: ABORTING!\n");
635 1.3 mycroft (void)signal(SIGSEGV, SIG_DFL);
636 1.3 mycroft (void)kill(0, SIGSEGV);
637 1.1 cgd /* NOTREACHED */
638 1.1 cgd }
639 1.1 cgd }
640 1.1 cgd
641 1.1 cgd char *
642 1.32 lukem rawname(char *cp)
643 1.1 cgd {
644 1.1 cgd static char rawbuf[MAXPATHLEN];
645 1.4 mycroft char *dp = strrchr(cp, '/');
646 1.1 cgd
647 1.3 mycroft if (dp == NULL)
648 1.3 mycroft return (NULL);
649 1.3 mycroft *dp = '\0';
650 1.12 mrg (void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
651 1.1 cgd *dp = '/';
652 1.1 cgd return (rawbuf);
653 1.1 cgd }
654 1.1 cgd
655 1.5 mycroft /*
656 1.5 mycroft * obsolete --
657 1.5 mycroft * Change set of key letters and ordered arguments into something
658 1.5 mycroft * getopt(3) will like.
659 1.5 mycroft */
660 1.5 mycroft static void
661 1.32 lukem obsolete(int *argcp, char **argvp[])
662 1.1 cgd {
663 1.5 mycroft int argc, flags;
664 1.5 mycroft char *ap, **argv, *flagsp, **nargv, *p;
665 1.5 mycroft
666 1.5 mycroft /* Setup. */
667 1.5 mycroft argv = *argvp;
668 1.5 mycroft argc = *argcp;
669 1.5 mycroft
670 1.5 mycroft /* Return if no arguments or first argument has leading dash. */
671 1.5 mycroft ap = argv[1];
672 1.5 mycroft if (argc == 1 || *ap == '-')
673 1.5 mycroft return;
674 1.5 mycroft
675 1.5 mycroft /* Allocate space for new arguments. */
676 1.33 lukem *argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
677 1.33 lukem p = flagsp = xmalloc(strlen(ap) + 2);
678 1.5 mycroft
679 1.5 mycroft *nargv++ = *argv;
680 1.5 mycroft argv += 2;
681 1.5 mycroft
682 1.5 mycroft for (flags = 0; *ap; ++ap) {
683 1.5 mycroft switch (*ap) {
684 1.5 mycroft case 'B':
685 1.5 mycroft case 'b':
686 1.5 mycroft case 'd':
687 1.5 mycroft case 'f':
688 1.5 mycroft case 'h':
689 1.5 mycroft case 's':
690 1.5 mycroft case 'T':
691 1.5 mycroft if (*argv == NULL) {
692 1.5 mycroft warnx("option requires an argument -- %c", *ap);
693 1.5 mycroft usage();
694 1.5 mycroft }
695 1.33 lukem nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
696 1.5 mycroft nargv[0][0] = '-';
697 1.5 mycroft nargv[0][1] = *ap;
698 1.17 lukem (void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
699 1.5 mycroft ++argv;
700 1.5 mycroft ++nargv;
701 1.5 mycroft break;
702 1.5 mycroft default:
703 1.5 mycroft if (!flags) {
704 1.5 mycroft *p++ = '-';
705 1.5 mycroft flags = 1;
706 1.5 mycroft }
707 1.5 mycroft *p++ = *ap;
708 1.5 mycroft break;
709 1.5 mycroft }
710 1.5 mycroft }
711 1.1 cgd
712 1.5 mycroft /* Terminate flags. */
713 1.5 mycroft if (flags) {
714 1.5 mycroft *p = '\0';
715 1.5 mycroft *nargv++ = flagsp;
716 1.5 mycroft }
717 1.5 mycroft
718 1.5 mycroft /* Copy remaining arguments. */
719 1.14 lukem while ((*nargv++ = *argv++) != NULL)
720 1.14 lukem ;
721 1.5 mycroft
722 1.5 mycroft /* Update argument count. */
723 1.5 mycroft *argcp = nargv - *argvp - 1;
724 1.33 lukem }
725 1.33 lukem
726 1.33 lukem
727 1.33 lukem void *
728 1.33 lukem xcalloc(size_t number, size_t size)
729 1.33 lukem {
730 1.33 lukem void *p;
731 1.33 lukem
732 1.33 lukem p = calloc(number, size);
733 1.33 lukem if (p == NULL)
734 1.33 lukem quit("%s\n", strerror(errno));
735 1.33 lukem return (p);
736 1.33 lukem }
737 1.33 lukem
738 1.33 lukem void *
739 1.33 lukem xmalloc(size_t size)
740 1.33 lukem {
741 1.33 lukem void *p;
742 1.33 lukem
743 1.33 lukem p = malloc(size);
744 1.33 lukem if (p == NULL)
745 1.33 lukem quit("%s\n", strerror(errno));
746 1.33 lukem return (p);
747 1.33 lukem }
748 1.33 lukem
749 1.33 lukem char *
750 1.33 lukem xstrdup(const char *str)
751 1.33 lukem {
752 1.33 lukem char *p;
753 1.33 lukem
754 1.33 lukem p = strdup(str);
755 1.33 lukem if (p == NULL)
756 1.33 lukem quit("%s\n", strerror(errno));
757 1.33 lukem return (p);
758 1.1 cgd }
759