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