]> git.wh0rd.org - dump.git/blob - dump/main.c
Raised the -b maximal value to 1024.
[dump.git] / dump / main.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*-
10 * Copyright (c) 1980, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: main.c,v 1.75 2002/09/02 12:43:12 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <compatlfs.h>
49 #include <ctype.h>
50 #include <compaterr.h>
51 #include <fcntl.h>
52 #include <fstab.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <errno.h>
59
60 #include <sys/param.h>
61 #include <sys/time.h>
62 #include <time.h>
63 #ifdef __linux__
64 #ifdef HAVE_EXT2FS_EXT2_FS_H
65 #include <ext2fs/ext2_fs.h>
66 #else
67 #include <linux/ext2_fs.h>
68 #endif
69 #include <ext2fs/ext2fs.h>
70 #include <sys/stat.h>
71 #include <bsdcompat.h>
72 #elif defined sunos
73 #include <sys/vnode.h>
74
75 #include <ufs/inode.h>
76 #include <ufs/fs.h>
77 #else
78 #include <ufs/ufs/dinode.h>
79 #include <ufs/ffs/fs.h>
80 #endif
81
82 #include <protocols/dumprestore.h>
83
84 #include "dump.h"
85 #include "pathnames.h"
86 #include "bylabel.h"
87
88 #ifndef SBOFF
89 #define SBOFF (SBLOCK * DEV_BSIZE)
90 #endif
91
92 /*
93 * Dump maps used to describe what is to be dumped.
94 */
95 int mapsize; /* size of the state maps */
96 char *usedinomap; /* map of allocated inodes */
97 char *dumpdirmap; /* map of directories to be dumped */
98 char *dumpinomap; /* map of files to be dumped */
99 char *metainomap; /* which of the inodes in dumpinomap will get
100 only their metadata dumped */
101
102 const char *disk; /* name of the disk file */
103 char tape[MAXPATHLEN];/* name of the tape file */
104 char *tapeprefix; /* prefix of the tape file */
105 char *dumpdates; /* name of the file containing dump date information*/
106 char lastlevel; /* dump level of previous dump */
107 char level; /* dump level of this dump */
108 int bzipflag; /* compression is done using bzlib */
109 int Afile = 0; /* archive file descriptor */
110 int uflag; /* update flag */
111 int mflag; /* dump metadata only if possible */
112 int Mflag; /* multi-volume flag */
113 int qflag; /* quit on errors flag */
114 int vflag; /* verbose flag */
115 int breademax = 32; /* maximum number of bread errors before we quit */
116 char *eot_script; /* end of volume script fiag */
117 int diskfd; /* disk file descriptor */
118 int tapefd; /* tape file descriptor */
119 int pipeout; /* true => output to standard output */
120 int fifoout; /* true => output to fifo */
121 dump_ino_t curino; /* current inumber; used globally */
122 int newtape; /* new tape flag */
123 int density; /* density in 0.1" units */
124 long tapesize; /* estimated tape size, blocks */
125 long tsize; /* tape size in 0.1" units */
126 long asize; /* number of 0.1" units written on current tape */
127 int etapes; /* estimated number of tapes */
128 int nonodump; /* if set, do not honor UF_NODUMP user flags */
129 int unlimited; /* if set, write to end of medium */
130 int compressed; /* if set, dump is to be compressed */
131 long long bytes_written;/* total bytes written to tape */
132 long uncomprblks; /* uncompressed blocks written to tape */
133 int notify; /* notify operator flag */
134 int blockswritten; /* number of blocks written on current tape */
135 int tapeno; /* current tape number */
136 time_t tstart_writing; /* when started writing the first tape block */
137 time_t tend_writing; /* after writing the last tape block */
138 #ifdef __linux__
139 ext2_filsys fs;
140 #else
141 struct fs *sblock; /* the file system super block */
142 char sblock_buf[MAXBSIZE];
143 #endif
144 long xferrate; /* averaged transfer rate of all volumes */
145 long dev_bsize; /* block size of underlying disk device */
146 int dev_bshift; /* log2(dev_bsize) */
147 int tp_bshift; /* log2(TP_BSIZE) */
148 dump_ino_t volinfo[TP_NINOS];/* which inode on which volume archive info */
149
150 #ifdef USE_QFA
151 int gTapeposfd;
152 char *gTapeposfile;
153 char gTps[255];
154 int32_t gThisDumpDate;
155 #endif /* USE_QFA */
156
157 struct dumptime *dthead; /* head of the list version */
158 int nddates; /* number of records (might be zero) */
159 int ddates_in; /* we have read the increment file */
160 struct dumpdates **ddatev; /* the arrayfied version */
161
162 int notify = 0; /* notify operator flag */
163 int blockswritten = 0; /* number of blocks written on current tape */
164 int tapeno = 0; /* current tape number */
165 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
166 int ntrec = NTREC; /* # tape blocks in each tape record */
167 int cartridge = 0; /* Assume non-cartridge tape */
168 #ifdef USE_QFA
169 int tapepos = 0; /* assume no QFA tapeposition needed by user */
170 #endif /* USE_QFA */
171 int dokerberos = 0; /* Use Kerberos authentication */
172 long dev_bsize = 1; /* recalculated below */
173 long blocksperfile; /* output blocks per file */
174 char *host = NULL; /* remote host (if any) */
175 int sizest = 0; /* return size estimate only */
176 int compressed = 0; /* use zlib to compress the output, compress level 1-9 */
177 long long bytes_written = 0; /* total bytes written */
178 long uncomprblks = 0;/* uncompressed blocks written */
179
180 #ifdef __linux__
181 char *__progname;
182 #endif
183
184 int maxbsize = 1024*1024; /* XXX MAXBSIZE from sys/param.h */
185 static long numarg __P((const char *, long, long));
186 static void obsolete __P((int *, char **[]));
187 static void usage __P((void));
188 static void do_exclude_from_file __P((char *));
189 static void do_exclude_ino_str __P((char *));
190 static void incompat_flags __P((int, char, char));
191
192 static dump_ino_t iexclude_list[IEXCLUDE_MAXNUM];/* the inode exclude list */
193 static int iexclude_num = 0; /* number of elements in the list */
194
195 int
196 main(int argc, char *argv[])
197 {
198 dump_ino_t ino;
199 int dirty;
200 struct dinode *dp;
201 struct fstab *dt;
202 char *map;
203 int ch;
204 int i, anydirskipped;
205 int aflag = 0, bflag = 0, Tflag = 0, honorlevel = 1;
206 dump_ino_t maxino;
207 struct STAT statbuf;
208 dev_t filedev = 0;
209 #ifdef __linux__
210 errcode_t retval;
211 char directory[MAXPATHLEN];
212 char pathname[MAXPATHLEN];
213 #endif
214 time_t tnow;
215 char *diskparam;
216 char *Apath = NULL;
217
218 spcl.c_label[0] = '\0';
219 spcl.c_date = time(NULL);
220
221 #ifdef __linux__
222 __progname = argv[0];
223 directory[0] = 0;
224 initialize_ext2_error_table();
225 #endif
226
227 tsize = 0; /* Default later, based on 'c' option for cart tapes */
228 unlimited = 1;
229 eot_script = NULL;
230 if ((tapeprefix = getenv("TAPE")) == NULL)
231 tapeprefix = _PATH_DEFTAPE;
232 dumpdates = _PATH_DUMPDATES;
233 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
234 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
235 level = '0';
236
237 if (argc < 2)
238 usage();
239
240 obsolete(&argc, &argv);
241
242 #ifdef USE_QFA
243 gTapeposfd = -1;
244 #endif /* USE_QFA */
245
246 while ((ch = getopt(argc, argv,
247 "0123456789A:aB:b:cd:e:E:f:F:h:I:"
248 #ifdef HAVE_BZLIB
249 "j::"
250 #endif
251 "L:"
252 #ifdef KERBEROS
253 "k"
254 #endif
255 "mMnq"
256 #ifdef USE_QFA
257 "Q:"
258 #endif
259 "s:ST:uvWw"
260 #ifdef HAVE_ZLIB
261 "z::"
262 #endif
263 )) != -1)
264 switch (ch) {
265 /* dump level */
266 case '0': case '1': case '2': case '3': case '4':
267 case '5': case '6': case '7': case '8': case '9':
268 level = ch;
269 break;
270
271 case 'A': /* archive file */
272 Apath = optarg;
273 if ((Afile = open(Apath, O_RDWR|O_CREAT|O_TRUNC,
274 S_IRUSR | S_IWUSR)) < 0) {
275 msg("Cannot open %s for writing: %s\n",
276 optarg, strerror(errno));
277 msg("The ENTIRE dump is aborted.\n");
278 exit(X_STARTUP);
279 }
280 memset(volinfo, 0, TP_NINOS * sizeof(dump_ino_t));
281 break;
282
283 case 'a': /* `auto-size', Write to EOM. */
284 unlimited = 1;
285 aflag = 1;
286 break;
287
288 case 'B': /* blocks per output file */
289 unlimited = 0;
290 blocksperfile = numarg("number of blocks per file",
291 1L, 0L);
292 break;
293
294 case 'b': /* blocks per tape write */
295 ntrec = numarg("number of blocks per write",
296 1L, 1048576L);
297 if (ntrec > maxbsize/1024) {
298 msg("Please choose a blocksize <= %dkB\n",
299 maxbsize/1024);
300 msg("The ENTIRE dump is aborted.\n");
301 exit(X_STARTUP);
302 }
303 bflag = 1;
304 break;
305
306 case 'c': /* Tape is cart. not 9-track */
307 unlimited = 0;
308 cartridge = 1;
309 break;
310
311 case 'd': /* density, in bits per inch */
312 unlimited = 0;
313 density = numarg("density", 10L, 327670L) / 10;
314 if (density >= 625 && !bflag)
315 ntrec = HIGHDENSITYTREC;
316 break;
317
318 /* 04-Feb-00 ILC */
319 case 'e': /* exclude an inode */
320 {
321 char *p = optarg, *q;
322 while ((q = strchr(p, ','))) {
323 *q = '\0';
324 do_exclude_ino_str(p);
325 p = q + 1;
326 }
327 do_exclude_ino_str(p);
328 }
329 break;
330
331 case 'E': /* exclude inodes read from file */
332 do_exclude_from_file(optarg);
333 break;
334
335 case 'f': /* output file */
336 tapeprefix = optarg;
337 break;
338
339 case 'F': /* end of tape script */
340 eot_script = optarg;
341 break;
342
343 case 'h':
344 honorlevel = numarg("honor level", 0L, 10L);
345 break;
346
347 #ifdef HAVE_BZLIB
348 case 'j':
349 compressed = 2;
350 bzipflag = 1;
351 if (optarg)
352 compressed = numarg("compress level", 1L, 9L);
353 break;
354 #endif /* HAVE_BZLIB */
355
356 case 'I':
357 breademax =
358 numarg ("number of errors to ignore", 1L, 0L);
359 break;
360
361 #ifdef KERBEROS
362 case 'k':
363 dokerberos = 1;
364 break;
365 #endif
366
367 case 'L':
368 /*
369 * Note that although there are LBLSIZE characters,
370 * the last must be '\0', so the limit on strlen()
371 * is really LBLSIZE-1.
372 */
373 strncpy(spcl.c_label, optarg, LBLSIZE);
374 spcl.c_label[LBLSIZE-1] = '\0';
375 if (strlen(optarg) > LBLSIZE-1) {
376 msg(
377 "WARNING Label `%s' is larger than limit of %d characters.\n",
378 optarg, LBLSIZE-1);
379 msg("WARNING: Using truncated label `%s'.\n",
380 spcl.c_label);
381 }
382 break;
383
384 case 'm': /* metadata only flag */
385 mflag = 1;
386 break;
387
388 case 'M': /* multi-volume flag */
389 Mflag = 1;
390 break;
391
392 case 'n': /* notify operators */
393 notify = 1;
394 break;
395
396 case 'q':
397 qflag = 1;
398 break;
399
400 #ifdef USE_QFA
401 case 'Q': /* create tapeposfile */
402 gTapeposfile = optarg;
403 tapepos = 1;
404 break;
405 #endif /* USE_QFA */
406
407 case 's': /* tape size, feet */
408 unlimited = 0;
409 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
410 break;
411
412 case 'S':
413 sizest = 1; /* return size estimate only */
414 break;
415
416 case 'T': /* time of last dump */
417 spcl.c_ddate = unctime(optarg);
418 if (spcl.c_ddate < 0) {
419 msg("bad time \"%s\"\n", optarg);
420 msg("The ENTIRE dump is aborted.\n");
421 exit(X_STARTUP);
422 }
423 Tflag = 1;
424 lastlevel = '?';
425 break;
426
427 case 'u': /* update dumpdates */
428 uflag = 1;
429 break;
430
431 case 'v': /* verbose */
432 vflag = 1;
433 break;
434
435 case 'W': /* what to do */
436 case 'w':
437 lastdump(ch);
438 exit(X_FINOK); /* do nothing else */
439 #ifdef HAVE_ZLIB
440 case 'z':
441 compressed = 2;
442 if (optarg)
443 compressed = numarg("compress level", 1L, 9L);
444 break;
445 #endif /* HAVE_ZLIB */
446
447 default:
448 usage();
449 }
450 argc -= optind;
451 argv += optind;
452
453 if (argc < 1) {
454 msg("Must specify disk or filesystem\n");
455 msg("The ENTIRE dump is aborted.\n");
456 exit(X_STARTUP);
457 }
458 diskparam = *argv++;
459 if (strlen(diskparam) >= MAXPATHLEN) {
460 msg("Disk or filesystem name too long: %s\n", diskparam);
461 msg("The ENTIRE dump is aborted.\n");
462 exit(X_STARTUP);
463 }
464 argc--;
465 incompat_flags(Tflag && uflag, 'T', 'u');
466 incompat_flags(aflag && blocksperfile, 'a', 'B');
467 incompat_flags(aflag && cartridge, 'a', 'c');
468 incompat_flags(aflag && density, 'a', 'd');
469 incompat_flags(aflag && tsize, 'a', 's');
470
471 if (strcmp(tapeprefix, "-") == 0) {
472 pipeout++;
473 tapeprefix = "standard output";
474 }
475
476 if (blocksperfile && !compressed)
477 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
478 else if (!unlimited) {
479 /*
480 * Determine how to default tape size and density
481 *
482 * density tape size
483 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
484 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
485 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
486 * (450*4 - slop)
487 * hilit19 hits again: "
488 */
489 if (density == 0)
490 density = cartridge ? 100 : 160;
491 if (tsize == 0)
492 tsize = cartridge ? 1700L*120L : 2300L*120L;
493 }
494
495 if (strchr(tapeprefix, ':')) {
496 host = tapeprefix;
497 tapeprefix = strchr(host, ':');
498 *tapeprefix++ = '\0';
499 #ifdef RDUMP
500 if (index(tapeprefix, '\n')) {
501 msg("invalid characters in tape\n");
502 msg("The ENTIRE dump is aborted.\n");
503 exit(X_STARTUP);
504 }
505 if (rmthost(host) == 0)
506 exit(X_STARTUP);
507 #else
508 msg("remote dump not enabled\n");
509 msg("The ENTIRE dump is aborted.\n");
510 exit(X_STARTUP);
511 #endif
512 }
513 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
514
515 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
516 signal(SIGHUP, sig);
517 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
518 signal(SIGTRAP, sig);
519 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
520 signal(SIGFPE, sig);
521 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
522 signal(SIGBUS, sig);
523 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
524 signal(SIGSEGV, sig);
525 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
526 signal(SIGTERM, sig);
527 if (signal(SIGINT, interrupt) == SIG_IGN)
528 signal(SIGINT, SIG_IGN);
529 #ifdef SIGXCPU
530 signal(SIGXCPU, SIG_IGN);
531 #endif /* SIGXCPU */
532 #ifdef SIGXFSZ
533 signal(SIGXFSZ, SIG_IGN);
534 #endif /* SIGXFSZ */
535
536 set_operators(); /* /etc/group snarfed */
537 getfstab(); /* /etc/fstab snarfed */
538
539 /*
540 * disk may end in / and this can confuse
541 * fstabsearch.
542 */
543 i = strlen(diskparam) - 1;
544 if (i > 1 && diskparam[i] == '/')
545 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
546 diskparam[i] = '\0';
547
548 disk = get_device_name(diskparam);
549 if (!disk) { /* null means the disk is some form
550 of LABEL= or UID= but it was not
551 found */
552 msg("Cannot find a disk having %s\n", diskparam);
553 msg("The ENTIRE dump is aborted.\n");
554 exit(X_STARTUP);
555 }
556 /*
557 * disk can be either the full special file name,
558 * the suffix of the special file name,
559 * the special name missing the leading '/',
560 * the file system name with or without the leading '/'.
561 */
562 if ((dt = fstabsearch(disk)) != NULL) {
563 /* if found then only one parameter (i.e. partition)
564 * is allowed */
565 if (argc >= 1) {
566 (void)fprintf(stderr, "Unknown arguments to dump:");
567 while (argc--)
568 (void)fprintf(stderr, " %s", *argv++);
569 (void)fprintf(stderr, "\n");
570 msg("The ENTIRE dump is aborted.\n");
571 exit(X_STARTUP);
572 }
573 disk = rawname(dt->fs_spec);
574 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
575 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
576 } else {
577 #ifdef __linux__
578 #ifdef HAVE_REALPATH
579 if (realpath(disk, pathname) == NULL)
580 #endif
581 strcpy(pathname, disk);
582 /*
583 * The argument could be now a mountpoint of
584 * a filesystem specified in fstab. Search for it.
585 */
586 if ((dt = fstabsearch(pathname)) != NULL) {
587 disk = rawname(dt->fs_spec);
588 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
589 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
590 } else {
591 /*
592 * The argument was not found in the fstab
593 * assume that this is a subtree and search for it
594 */
595 dt = fstabsearchdir(pathname, directory);
596 if (dt != NULL) {
597 char name[MAXPATHLEN];
598 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
599 (void)snprintf(name, sizeof(name), "%s (dir %s)",
600 dt->fs_file, directory);
601 (void)strncpy(spcl.c_filesys, name, NAMELEN);
602 disk = rawname(dt->fs_spec);
603 } else {
604 (void)strncpy(spcl.c_dev, disk, NAMELEN);
605 (void)strncpy(spcl.c_filesys, "an unlisted file system",
606 NAMELEN);
607 }
608 }
609 #else
610 (void)strncpy(spcl.c_dev, disk, NAMELEN);
611 (void)strncpy(spcl.c_filesys, "an unlisted file system",
612 NAMELEN);
613 #endif
614 }
615
616 if (directory[0] != 0) {
617 if (level != '0') {
618 msg("Only level 0 dumps are allowed on a subdirectory\n");
619 msg("The ENTIRE dump is aborted.\n");
620 exit(X_STARTUP);
621 }
622 if (uflag) {
623 msg("You can't update the dumpdates file when dumping a subdirectory\n");
624 msg("The ENTIRE dump is aborted.\n");
625 exit(X_STARTUP);
626 }
627 }
628 spcl.c_dev[NAMELEN-1] = '\0';
629 spcl.c_filesys[NAMELEN-1] = '\0';
630 (void)gethostname(spcl.c_host, NAMELEN);
631 spcl.c_host[NAMELEN-1] = '\0';
632 spcl.c_level = level - '0';
633 spcl.c_type = TS_TAPE;
634 if (!Tflag)
635 getdumptime(uflag); /* dumpdates snarfed */
636
637 if (spcl.c_ddate == 0 && spcl.c_level) {
638 msg("WARNING: There is no inferior level dump on this filesystem\n");
639 msg("WARNING: Assuming a level 0 dump by default\n");
640 level = '0';
641 spcl.c_level = 0;
642 }
643
644 if (Mflag)
645 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
646 else
647 strncpy(tape, tapeprefix, MAXPATHLEN);
648 tape[MAXPATHLEN - 1] = '\0';
649
650 if (!pipeout) {
651 if (STAT(tape, &statbuf) != -1)
652 fifoout= statbuf.st_mode & S_IFIFO;
653 }
654
655 if (!sizest) {
656
657 msg("Date of this level %c dump: %s", level,
658 ctime4(&spcl.c_date));
659 #ifdef USE_QFA
660 gThisDumpDate = spcl.c_date;
661 #endif
662 if (spcl.c_ddate)
663 msg("Date of last level %c dump: %s", lastlevel,
664 ctime4(&spcl.c_ddate));
665 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
666 if (host)
667 msgtail("to %s on host %s\n", tape, host);
668 else
669 msgtail("to %s\n", tape);
670 } /* end of size estimate */
671
672 #ifdef __linux__
673 if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
674 msg("Cannot open %s\n", disk);
675 msg("The ENTIRE dump is aborted.\n");
676 exit(X_STARTUP);
677 }
678 #ifdef BLKFLSBUF
679 (void)ioctl(diskfd, BLKFLSBUF);
680 #endif
681 retval = dump_fs_open(disk, &fs);
682 if (retval) {
683 com_err(disk, retval, "while opening filesystem");
684 if (retval == EXT2_ET_REV_TOO_HIGH)
685 msg("Get a newer version of dump!\n");
686 msg("The ENTIRE dump is aborted.\n");
687 exit(X_STARTUP);
688 }
689 if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
690 com_err(disk, retval, "while opening filesystem");
691 msg("Get a newer version of dump!\n");
692 msg("The ENTIRE dump is aborted.\n");
693 exit(X_STARTUP);
694 }
695 /* if no user label specified, use ext2 filesystem label if available */
696 if (spcl.c_label[0] == '\0') {
697 const char *lbl;
698 if ( (lbl = get_device_label(disk)) != NULL) {
699 strncpy(spcl.c_label, lbl, LBLSIZE);
700 spcl.c_label[LBLSIZE-1] = '\0';
701 }
702 else
703 strcpy(spcl.c_label, "none"); /* safe strcpy. */
704 }
705 sync();
706 dev_bsize = DEV_BSIZE;
707 dev_bshift = ffs(dev_bsize) - 1;
708 if (dev_bsize != (1 << dev_bshift))
709 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
710 tp_bshift = ffs(TP_BSIZE) - 1;
711 if (TP_BSIZE != (1 << tp_bshift))
712 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
713 maxino = fs->super->s_inodes_count + 1;
714 spcl.c_flags |= DR_NEWINODEFMT;
715 #else /* __linux __*/
716 if ((diskfd = open(disk, O_RDONLY)) < 0) {
717 msg("Cannot open %s\n", disk);
718 msg("The ENTIRE dump is aborted.\n");
719 exit(X_STARTUP);
720 }
721 sync();
722 sblock = (struct fs *)sblock_buf;
723 bread(SBOFF, (char *) sblock, SBSIZE);
724 if (sblock->fs_magic != FS_MAGIC)
725 quit("bad sblock magic number\n");
726 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
727 dev_bshift = ffs(dev_bsize) - 1;
728 if (dev_bsize != (1 << dev_bshift))
729 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
730 tp_bshift = ffs(TP_BSIZE) - 1;
731 if (TP_BSIZE != (1 << tp_bshift))
732 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
733 #ifdef FS_44INODEFMT
734 if (sblock->fs_inodefmt >= FS_44INODEFMT)
735 spcl.c_flags |= DR_NEWINODEFMT;
736 #endif
737 maxino = sblock->fs_ipg * sblock->fs_ncg;
738 #endif /* __linux__ */
739 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
740 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
741 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
742 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
743 metainomap = (char *)calloc((unsigned) mapsize, sizeof(char));
744 if (usedinomap == NULL || dumpdirmap == NULL ||
745 dumpinomap == NULL || metainomap == NULL)
746 quit("out of memory allocating inode maps\n");
747 tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
748
749 nonodump = spcl.c_level < honorlevel;
750
751 if (!sizest) {
752 msg("Label: %s\n", spcl.c_label);
753
754 if (compressed)
755 msg("Compressing output at compression level %d (%s)\n",
756 compressed, bzipflag ? "bzlib" : "zlib");
757 }
758
759 #if defined(SIGINFO)
760 (void)signal(SIGINFO, statussig);
761 #endif
762
763 if (!sizest)
764 msg("mapping (Pass I) [regular files]\n");
765 #ifdef __linux__
766 if (directory[0] == 0)
767 anydirskipped = mapfiles(maxino, &tapesize);
768 else {
769 if (STAT(pathname, &statbuf) == -1) {
770 msg("File cannot be accessed (%s).\n", pathname);
771 msg("The ENTIRE dump is aborted.\n");
772 exit(X_STARTUP);
773 }
774 filedev = statbuf.st_dev;
775 if (!(statbuf.st_mode & S_IFDIR)) /* is a file */
776 anydirskipped = maponefile(maxino, &tapesize,
777 directory);
778 else
779 anydirskipped = mapfilesfromdir(maxino, &tapesize,
780 directory);
781 }
782 while (argc--) {
783 int anydirskipped2;
784 char *p = *argv;
785 /* check if file is available */
786 if (STAT(p, &statbuf) == -1) {
787 msg("File cannot be accessed (%s).\n", p);
788 msg("The ENTIRE dump is aborted.\n");
789 exit(X_STARTUP);
790 }
791 /* check if file is on same unix partiton as the first
792 * argument */
793 if (statbuf.st_dev != filedev) {
794 msg("Files are not on same file system (%s).\n", p);
795 msg("The ENTIRE dump is aborted.\n");
796 exit(X_STARTUP);
797 }
798 /* check if file is a directory */
799 if (!(statbuf.st_mode & S_IFDIR))
800 anydirskipped2 = maponefile(maxino, &tapesize,
801 p+strlen(dt->fs_file));
802 else
803 /* read directory inodes.
804 * NOTE: nested directories are not recognized
805 * so inodes may be umped twice!
806 */
807 anydirskipped2 = mapfilesfromdir(maxino, &tapesize,
808 p+strlen(dt->fs_file));
809 if (!anydirskipped)
810 anydirskipped = anydirskipped2;
811 argv++;
812 }
813 #else
814 anydirskipped = mapfiles(maxino, &tapesize);
815 #endif
816
817 if (!sizest)
818 msg("mapping (Pass II) [directories]\n");
819 while (anydirskipped) {
820 anydirskipped = mapdirs(maxino, &tapesize);
821 }
822
823 if (sizest) {
824 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
825 exit(X_FINOK);
826 } /* stop here for size estimate */
827
828 if (pipeout || unlimited) {
829 tapesize += 1 + ntrec; /* 1 map header + trailer blocks */
830 msg("estimated %ld tape blocks.\n", tapesize);
831 } else {
832 double fetapes;
833
834 if (blocksperfile)
835 fetapes = (double) tapesize / blocksperfile;
836 else if (cartridge) {
837 /* Estimate number of tapes, assuming streaming stops at
838 the end of each block written, and not in mid-block.
839 Assume no erroneous blocks; this can be compensated
840 for with an artificially low tape size. */
841 fetapes =
842 ( (double) tapesize /* blocks */
843 * TP_BSIZE /* bytes/block */
844 * (1.0/density) /* 0.1" / byte " */
845 +
846 (double) tapesize /* blocks */
847 * (1.0/ntrec) /* streaming-stops per block */
848 * 15.48 /* 0.1" / streaming-stop " */
849 ) * (1.0 / tsize ); /* tape / 0.1" " */
850 } else {
851 /* Estimate number of tapes, for old fashioned 9-track
852 tape */
853 int tenthsperirg = (density == 625) ? 3 : 7;
854 fetapes =
855 ( (double) tapesize /* blocks */
856 * TP_BSIZE /* bytes / block */
857 * (1.0/density) /* 0.1" / byte " */
858 +
859 (double) tapesize /* blocks */
860 * (1.0/ntrec) /* IRG's / block */
861 * tenthsperirg /* 0.1" / IRG " */
862 ) * (1.0 / tsize ); /* tape / 0.1" " */
863 }
864 etapes = fetapes; /* truncating assignment */
865 etapes++;
866 /* count the dumped inodes map on each additional tape */
867 tapesize += (etapes - 1) *
868 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
869 tapesize += etapes + ntrec; /* headers + trailer blks */
870 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
871 tapesize, fetapes);
872 }
873
874 #ifdef USE_QFA
875 if (tapepos) {
876 msg("writing QFA positions to %s\n", gTapeposfile);
877 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
878 quit("can't open tapeposfile\n");
879 /* print QFA-file header */
880 snprintf(gTps, sizeof(gTps), "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
881 gTps[sizeof(gTps) - 1] = '\0';
882 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
883 quit("can't write tapeposfile\n");
884 sprintf(gTps, "ino\ttapeno\ttapepos\n");
885 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
886 quit("can't write tapeposfile\n");
887 }
888 #endif /* USE_QFA */
889
890 /*
891 * Allocate tape buffer.
892 */
893 if (!alloctape())
894 quit(
895 "can't allocate tape buffers - try a smaller blocking factor.\n");
896
897 startnewtape(1);
898 tstart_writing = time(NULL);
899 dumpmap(usedinomap, TS_CLRI, maxino - 1);
900
901 msg("dumping (Pass III) [directories]\n");
902 dirty = 0; /* XXX just to get gcc to shut up */
903 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
904 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
905 dirty = *map++;
906 else
907 dirty >>= 1;
908 if ((dirty & 1) == 0)
909 continue;
910 /*
911 * Skip directory inodes deleted and maybe reallocated
912 */
913 dp = getino(ino);
914 if ((dp->di_mode & IFMT) != IFDIR)
915 continue;
916 #ifdef __linux__
917 /*
918 * Skip directory inodes deleted and not yes reallocated...
919 */
920 if (dp->di_nlink == 0 || dp->di_dtime != 0)
921 continue;
922 if (vflag)
923 msg("dumping directory inode %lu\n", ino);
924 (void)dumpdirino(dp, ino);
925 #else
926 (void)dumpino(dp, ino);
927 #endif
928 }
929
930 msg("dumping (Pass IV) [regular files]\n");
931 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
932 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
933 dirty = *map++;
934 else
935 dirty >>= 1;
936 if ((dirty & 1) == 0)
937 continue;
938 /*
939 * Skip inodes deleted and reallocated as directories.
940 */
941 dp = getino(ino);
942 if ((dp->di_mode & IFMT) == IFDIR)
943 continue;
944 #ifdef __linux__
945 /*
946 * No need to check here for deleted and not yet reallocated
947 * inodes since this is done in dumpino().
948 */
949 #endif
950 if (vflag) {
951 if (mflag && TSTINO(ino, metainomap))
952 msg("dumping regular inode %lu (meta only)\n", ino);
953 else
954 msg("dumping regular inode %lu\n", ino);
955 }
956 (void)dumpino(dp, ino, mflag && TSTINO(ino, metainomap));
957 }
958
959 tend_writing = time(NULL);
960 spcl.c_type = TS_END;
961
962 if (Afile) {
963 volinfo[1] = ROOTINO;
964 memcpy(spcl.c_inos, volinfo, TP_NINOS * sizeof(dump_ino_t));
965 spcl.c_flags |= DR_INODEINFO;
966 }
967
968 /*
969 * Finish off the current tape record with trailer blocks, to ensure
970 * at least the data in the last partial record makes it to tape.
971 * Also make sure we write at least 1 trailer block.
972 */
973 for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
974 writeheader(maxino - 1);
975
976 tnow = trewind();
977
978 if (pipeout || fifoout)
979 msg("%ld tape blocks (%.2fMB)\n", spcl.c_tapea,
980 ((double)spcl.c_tapea * TP_BSIZE / 1048576));
981 else
982 msg("%ld tape blocks (%.2fMB) on %d volume(s)\n",
983 spcl.c_tapea,
984 ((double)spcl.c_tapea * TP_BSIZE / 1048576),
985 spcl.c_volume);
986
987 /* report dump performance, avoid division by zero */
988 if (tend_writing - tstart_writing == 0)
989 msg("finished in less than a second\n");
990 else
991 msg("finished in %d seconds, throughput %d kBytes/sec\n",
992 tend_writing - tstart_writing,
993 spcl.c_tapea / (tend_writing - tstart_writing));
994
995 putdumptime();
996 msg("Date of this level %c dump: %s", level,
997 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
998 msg("Date this dump completed: %s", ctime(&tnow));
999
1000 msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
1001 if (compressed) {
1002 long tapekb = bytes_written / 1024;
1003 double rate = .0005 + (double) spcl.c_tapea / tapekb;
1004 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
1005 spcl.c_tapea, tapekb, rate);
1006 }
1007
1008 if (Afile)
1009 msg("Archiving dump to %s\n", Apath);
1010
1011 broadcast("DUMP IS DONE!\7\7\n");
1012 msg("DUMP IS DONE\n");
1013 Exit(X_FINOK);
1014 /* NOTREACHED */
1015 return 0; /* gcc - shut up */
1016 }
1017
1018 static void
1019 usage(void)
1020 {
1021 char white[MAXPATHLEN];
1022 const char *ext2ver, *ext2date;
1023
1024 memset(white, ' ', MAXPATHLEN);
1025 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
1026
1027 #ifdef __linux__
1028 ext2fs_get_library_version(&ext2ver, &ext2date);
1029 fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
1030 __progname, _DUMP_VERSION, ext2ver, ext2date);
1031 #else
1032 fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
1033 #endif
1034 fprintf(stderr,
1035 "usage:\t%s [-0123456789ac"
1036 #ifdef KERBEROS
1037 "k"
1038 #endif
1039 "mMnqSuv"
1040 "] [-A file] [-B records] [-b blocksize]\n"
1041 "\t%s [-d density] [-e inode#,inode#,...] [-E file] [-f file]\n"
1042 "\t%s [-h level] [-I nr errors] "
1043 #ifdef HAVE_BZLIB
1044 "[-j zlevel] "
1045 #endif
1046 #ifdef USE_QFA
1047 "[-Q file] "
1048 #endif
1049 "[-s feet]\n"
1050 "\t%s [-T date] "
1051 #ifdef HAVE_ZLIB
1052 "[-z zlevel] "
1053 #endif
1054 "filesystem\n"
1055 "\t%s [-W | -w]\n",
1056 __progname, white, white, white, __progname);
1057 exit(X_STARTUP);
1058 }
1059
1060 /*
1061 * Pick up a numeric argument. It must be nonnegative and in the given
1062 * range (except that a vmax of 0 means unlimited).
1063 */
1064 static long
1065 numarg(const char *meaning, long vmin, long vmax)
1066 {
1067 char *p;
1068 long val;
1069
1070 val = strtol(optarg, &p, 10);
1071 if (*p)
1072 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1073 if (val < vmin || (vmax && val > vmax))
1074 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1075 return (val);
1076 }
1077
1078 void
1079 sig(int signo)
1080 {
1081 switch(signo) {
1082 case SIGALRM:
1083 case SIGBUS:
1084 case SIGFPE:
1085 case SIGHUP:
1086 case SIGTERM:
1087 case SIGTRAP:
1088 if (pipeout || fifoout)
1089 quit("Signal on pipe: cannot recover\n");
1090 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
1091 (void)fflush(stderr);
1092 (void)fflush(stdout);
1093 close_rewind();
1094 exit(X_REWRITE);
1095 /* NOTREACHED */
1096 case SIGSEGV:
1097 msg("SIGSEGV: ABORTING!\n");
1098 (void)signal(SIGSEGV, SIG_DFL);
1099 (void)kill(0, SIGSEGV);
1100 /* NOTREACHED */
1101 }
1102 }
1103
1104 const char *
1105 rawname(const char *cp)
1106 {
1107 #ifdef __linux__
1108 return cp;
1109 #else /* __linux__ */
1110 static char rawbuf[MAXPATHLEN];
1111 char *dp = strrchr(cp, '/');
1112
1113 if (dp == NULL)
1114 return (NULL);
1115 (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1116 rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1117 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1118 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1119 return (rawbuf);
1120 #endif /* __linux__ */
1121 }
1122
1123 /*
1124 * obsolete --
1125 * Change set of key letters and ordered arguments into something
1126 * getopt(3) will like.
1127 */
1128 static void
1129 obsolete(int *argcp, char **argvp[])
1130 {
1131 int argc, flags;
1132 char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1133
1134 /* Setup. */
1135 argv = *argvp;
1136 argc = *argcp;
1137
1138 /* Return if no arguments or first argument has leading dash. */
1139 ap = argv[1];
1140 if (argc == 1 || *ap == '-')
1141 return;
1142
1143 /* Allocate space for new arguments. */
1144 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1145 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1146 err(X_STARTUP, "malloc new args");
1147
1148 *nargv++ = *argv;
1149 argv += 2;
1150
1151 for (flags = 0; *ap; ++ap) {
1152 switch (*ap) {
1153 case 'A':
1154 case 'B':
1155 case 'b':
1156 case 'd':
1157 case 'e':
1158 case 'E':
1159 case 'f':
1160 case 'F':
1161 case 'h':
1162 case 'L':
1163 case 'Q':
1164 case 's':
1165 case 'T':
1166 if (*argv == NULL) {
1167 warnx("option requires an argument -- %c", *ap);
1168 usage();
1169 }
1170 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1171 err(X_STARTUP, "malloc arg");
1172 nargv[0][0] = '-';
1173 nargv[0][1] = *ap;
1174 (void)strcpy(&nargv[0][2], *argv);
1175 ++argv;
1176 ++nargv;
1177 break;
1178 default:
1179 if (!flags) {
1180 *p++ = '-';
1181 flags = 1;
1182 }
1183 *p++ = *ap;
1184 break;
1185 }
1186 }
1187
1188 /* Terminate flags. */
1189 if (flags) {
1190 *p = '\0';
1191 *nargv++ = flagsp;
1192 }
1193
1194 /* Copy remaining arguments. */
1195 while ((*nargv++ = *argv++));
1196
1197 /* Update argument count. */
1198 *argcp = nargv - *argvp - 1;
1199 }
1200
1201 /*
1202 * This tests whether an inode is in the exclude list
1203 */
1204 int
1205 exclude_ino(dump_ino_t ino)
1206 {
1207 /* 04-Feb-00 ILC */
1208 if (iexclude_num) { /* if there are inodes in the exclude list */
1209 int idx; /* then check this inode against it */
1210 for (idx = 0; idx < iexclude_num; idx++)
1211 if (ino == iexclude_list[idx])
1212 return 1;
1213 }
1214 return 0;
1215 }
1216
1217 /*
1218 * This tests adds an inode to the exclusion list if it isn't already there
1219 */
1220 void
1221 do_exclude_ino(dump_ino_t ino, const char *reason)
1222 {
1223 if (!exclude_ino(ino)) {
1224 if (iexclude_num == IEXCLUDE_MAXNUM) {
1225 msg("Too many exclude options\n");
1226 msg("The ENTIRE dump is aborted.\n");
1227 exit(X_STARTUP);
1228 }
1229 if (reason)
1230 msg("Added inode %u to exclude list (%s)\n",
1231 ino, reason);
1232 else
1233 msg("Added inode %u to exclude list\n", ino);
1234 iexclude_list[iexclude_num++] = ino;
1235 }
1236 }
1237
1238 static void
1239 do_exclude_ino_str(char * ino) {
1240 char *r;
1241 unsigned long inod;
1242
1243 inod = strtoul(ino, &r, 10);
1244 if (*r != '\0' || inod <= ROOTINO) {
1245 msg("Invalid inode argument %s\n", ino);
1246 msg("The ENTIRE dump is aborted.\n");
1247 exit(X_STARTUP);
1248 }
1249 do_exclude_ino(inod, NULL);
1250 }
1251
1252 /*
1253 * This reads a file containing one inode number per line and exclude them all
1254 */
1255 static void
1256 do_exclude_from_file(char *file) {
1257 FILE *f;
1258 char *p, fname[MAXPATHLEN];
1259
1260
1261 if (!( f = fopen(file, "r")) ) {
1262 msg("Cannot open file for reading: %s\n", file);
1263 msg("The ENTIRE dump is aborted.\n");
1264 exit(X_STARTUP);
1265 }
1266 while (( p = fgets(fname, MAXPATHLEN, f))) {
1267 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1268 *(p + strlen(p) - 1) = '\0';
1269 if ( !*p ) /* skip empty lines */
1270 continue;
1271 do_exclude_ino_str(p);
1272 }
1273 fclose(f);
1274 }
1275
1276 static void incompat_flags(int cond, char flag1, char flag2) {
1277 if (cond) {
1278 msg("You cannot use the %c and %c flags together.\n",
1279 flag1, flag2);
1280 msg("The ENTIRE dump is aborted.\n");
1281 exit(X_STARTUP);
1282 }
1283 }