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