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