]> git.wh0rd.org - dump.git/blob - dump/main.c
Fixed dumping of dangling symlinks.
[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.82 2003/03/07 09:15:50 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 <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <errno.h>
58 #include <mntent.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; /* # 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 mntent *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:D: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 case 'D': /* path of dumpdates file */
319 dumpdates = optarg;
320 break;
321
322 /* 04-Feb-00 ILC */
323 case 'e': /* exclude an inode */
324 {
325 char *p = optarg, *q;
326 while ((q = strchr(p, ','))) {
327 *q = '\0';
328 do_exclude_ino_str(p);
329 p = q + 1;
330 }
331 do_exclude_ino_str(p);
332 }
333 break;
334
335 case 'E': /* exclude inodes read from file */
336 do_exclude_from_file(optarg);
337 break;
338
339 case 'f': /* output file */
340 tapeprefix = optarg;
341 break;
342
343 case 'F': /* end of tape script */
344 eot_script = optarg;
345 break;
346
347 case 'h':
348 honorlevel = numarg("honor level", 0L, 10L);
349 break;
350
351 #ifdef HAVE_BZLIB
352 case 'j':
353 compressed = 2;
354 bzipflag = 1;
355 if (optarg)
356 compressed = numarg("compress level", 1L, 9L);
357 break;
358 #endif /* HAVE_BZLIB */
359
360 case 'I':
361 breademax =
362 numarg ("number of errors to ignore", 0L, 0L);
363 break;
364
365 #ifdef KERBEROS
366 case 'k':
367 dokerberos = 1;
368 break;
369 #endif
370
371 case 'L':
372 /*
373 * Note that although there are LBLSIZE characters,
374 * the last must be '\0', so the limit on strlen()
375 * is really LBLSIZE-1.
376 */
377 strncpy(spcl.c_label, optarg, LBLSIZE);
378 spcl.c_label[LBLSIZE-1] = '\0';
379 if (strlen(optarg) > LBLSIZE-1) {
380 msg(
381 "WARNING Label `%s' is larger than limit of %d characters.\n",
382 optarg, LBLSIZE-1);
383 msg("WARNING: Using truncated label `%s'.\n",
384 spcl.c_label);
385 }
386 break;
387
388 case 'm': /* metadata only flag */
389 mflag = 1;
390 break;
391
392 case 'M': /* multi-volume flag */
393 Mflag = 1;
394 break;
395
396 case 'n': /* notify operators */
397 notify = 1;
398 break;
399
400 case 'q':
401 qflag = 1;
402 break;
403
404 #ifdef USE_QFA
405 case 'Q': /* create tapeposfile */
406 gTapeposfile = optarg;
407 tapepos = 1;
408 break;
409 #endif /* USE_QFA */
410
411 case 's': /* tape size, feet */
412 unlimited = 0;
413 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
414 break;
415
416 case 'S':
417 sizest = 1; /* return size estimate only */
418 break;
419
420 case 'T': /* time of last dump */
421 spcl.c_ddate = unctime(optarg);
422 if (spcl.c_ddate < 0) {
423 msg("bad time \"%s\"\n", optarg);
424 msg("The ENTIRE dump is aborted.\n");
425 exit(X_STARTUP);
426 }
427 Tflag = 1;
428 lastlevel = '?';
429 break;
430
431 case 'u': /* update dumpdates */
432 uflag = 1;
433 break;
434
435 case 'v': /* verbose */
436 vflag = 1;
437 break;
438
439 case 'W': /* what to do */
440 case 'w':
441 lastdump(ch);
442 exit(X_FINOK); /* do nothing else */
443 #ifdef HAVE_ZLIB
444 case 'z':
445 compressed = 2;
446 if (optarg)
447 compressed = numarg("compress level", 1L, 9L);
448 break;
449 #endif /* HAVE_ZLIB */
450
451 default:
452 usage();
453 }
454 argc -= optind;
455 argv += optind;
456
457 if (argc < 1) {
458 msg("Must specify disk or filesystem\n");
459 msg("The ENTIRE dump is aborted.\n");
460 exit(X_STARTUP);
461 }
462 diskparam = *argv++;
463 if (strlen(diskparam) >= MAXPATHLEN) {
464 msg("Disk or filesystem name too long: %s\n", diskparam);
465 msg("The ENTIRE dump is aborted.\n");
466 exit(X_STARTUP);
467 }
468 argc--;
469 incompat_flags(Tflag && uflag, 'T', 'u');
470 incompat_flags(aflag && blocksperfile, 'a', 'B');
471 incompat_flags(aflag && cartridge, 'a', 'c');
472 incompat_flags(aflag && density, 'a', 'd');
473 incompat_flags(aflag && tsize, 'a', 's');
474
475 if (strcmp(tapeprefix, "-") == 0) {
476 pipeout++;
477 tapeprefix = "standard output";
478 }
479
480 if (blocksperfile && !compressed)
481 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
482 else if (!unlimited) {
483 /*
484 * Determine how to default tape size and density
485 *
486 * density tape size
487 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
488 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
489 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
490 * (450*4 - slop)
491 * hilit19 hits again: "
492 */
493 if (density == 0)
494 density = cartridge ? 100 : 160;
495 if (tsize == 0)
496 tsize = cartridge ? 1700L*120L : 2300L*120L;
497 }
498
499 if (strchr(tapeprefix, ':')) {
500 host = tapeprefix;
501 tapeprefix = strchr(host, ':');
502 *tapeprefix++ = '\0';
503 #ifdef RDUMP
504 if (index(tapeprefix, '\n')) {
505 msg("invalid characters in tape\n");
506 msg("The ENTIRE dump is aborted.\n");
507 exit(X_STARTUP);
508 }
509 if (rmthost(host) == 0)
510 exit(X_STARTUP);
511 #else
512 msg("remote dump not enabled\n");
513 msg("The ENTIRE dump is aborted.\n");
514 exit(X_STARTUP);
515 #endif
516 }
517 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
518
519 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
520 signal(SIGHUP, sig);
521 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
522 signal(SIGTRAP, sig);
523 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
524 signal(SIGFPE, sig);
525 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
526 signal(SIGBUS, sig);
527 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
528 signal(SIGSEGV, sig);
529 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
530 signal(SIGTERM, sig);
531 if (signal(SIGINT, interrupt) == SIG_IGN)
532 signal(SIGINT, SIG_IGN);
533 #ifdef SIGXCPU
534 signal(SIGXCPU, SIG_IGN);
535 #endif /* SIGXCPU */
536 #ifdef SIGXFSZ
537 signal(SIGXFSZ, SIG_IGN);
538 #endif /* SIGXFSZ */
539
540 set_operators(); /* /etc/group snarfed */
541 getfstab(); /* /etc/fstab snarfed */
542
543 /*
544 * disk may end in / and this can confuse
545 * fstabsearch.
546 */
547 i = strlen(diskparam) - 1;
548 if (i > 1 && diskparam[i] == '/')
549 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
550 diskparam[i] = '\0';
551
552 disk = get_device_name(diskparam);
553 if (!disk) { /* null means the disk is some form
554 of LABEL= or UID= but it was not
555 found */
556 msg("Cannot find a disk having %s\n", diskparam);
557 msg("The ENTIRE dump is aborted.\n");
558 exit(X_STARTUP);
559 }
560 /*
561 * disk can be either the full special file name,
562 * the suffix of the special file name,
563 * the special name missing the leading '/',
564 * the file system name with or without the leading '/'.
565 */
566 if ((dt = fstabsearch(disk)) != NULL) {
567 /* if found then only one parameter (i.e. partition)
568 * is allowed */
569 if (argc >= 1) {
570 (void)fprintf(stderr, "Unknown arguments to dump:");
571 while (argc--)
572 (void)fprintf(stderr, " %s", *argv++);
573 (void)fprintf(stderr, "\n");
574 msg("The ENTIRE dump is aborted.\n");
575 exit(X_STARTUP);
576 }
577 disk = rawname(dt->mnt_fsname);
578 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
579 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
580 } else {
581 #ifdef __linux__
582 #ifdef HAVE_REALPATH
583 if (realpath(disk, pathname) == NULL)
584 #endif
585 strcpy(pathname, disk);
586 /*
587 * The argument could be now a mountpoint of
588 * a filesystem specified in fstab. Search for it.
589 */
590 if ((dt = fstabsearch(pathname)) != NULL) {
591 disk = rawname(dt->mnt_fsname);
592 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
593 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
594 } else {
595 /*
596 * The argument was not found in the fstab
597 * assume that this is a subtree and search for it
598 */
599 dt = fstabsearchdir(pathname, directory);
600 if (dt != NULL) {
601 char name[MAXPATHLEN];
602 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
603 (void)snprintf(name, sizeof(name), "%s (dir %s)",
604 dt->mnt_dir, directory);
605 (void)strncpy(spcl.c_filesys, name, NAMELEN);
606 disk = rawname(dt->mnt_fsname);
607 } else {
608 (void)strncpy(spcl.c_dev, disk, NAMELEN);
609 (void)strncpy(spcl.c_filesys, "an unlisted file system",
610 NAMELEN);
611 }
612 }
613 #else
614 (void)strncpy(spcl.c_dev, disk, NAMELEN);
615 (void)strncpy(spcl.c_filesys, "an unlisted file system",
616 NAMELEN);
617 #endif
618 }
619
620 if (directory[0] != 0) {
621 if (level != '0') {
622 msg("Only level 0 dumps are allowed on a subdirectory\n");
623 msg("The ENTIRE dump is aborted.\n");
624 exit(X_STARTUP);
625 }
626 if (uflag) {
627 msg("You can't update the dumpdates file when dumping a subdirectory\n");
628 msg("The ENTIRE dump is aborted.\n");
629 exit(X_STARTUP);
630 }
631 }
632 spcl.c_dev[NAMELEN-1] = '\0';
633 spcl.c_filesys[NAMELEN-1] = '\0';
634 (void)gethostname(spcl.c_host, NAMELEN);
635 spcl.c_host[NAMELEN-1] = '\0';
636 spcl.c_level = level - '0';
637 spcl.c_type = TS_TAPE;
638 if (!Tflag)
639 getdumptime(uflag); /* dumpdates snarfed */
640
641 if (spcl.c_ddate == 0 && spcl.c_level) {
642 msg("WARNING: There is no inferior level dump on this filesystem\n");
643 msg("WARNING: Assuming a level 0 dump by default\n");
644 level = '0';
645 spcl.c_level = 0;
646 }
647
648 if (Mflag)
649 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
650 else
651 strncpy(tape, tapeprefix, MAXPATHLEN);
652 tape[MAXPATHLEN - 1] = '\0';
653
654 if (!pipeout) {
655 if (STAT(tape, &statbuf) != -1)
656 fifoout= statbuf.st_mode & S_IFIFO;
657 }
658
659 if (!sizest) {
660
661 msg("Date of this level %c dump: %s", level,
662 ctime4(&spcl.c_date));
663 #ifdef USE_QFA
664 gThisDumpDate = spcl.c_date;
665 #endif
666 if (spcl.c_ddate)
667 msg("Date of last level %c dump: %s", lastlevel,
668 ctime4(&spcl.c_ddate));
669 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
670 if (host)
671 msgtail("to %s on host %s\n", tape, host);
672 else
673 msgtail("to %s\n", tape);
674 } /* end of size estimate */
675
676 #ifdef __linux__
677 if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
678 msg("Cannot open %s\n", disk);
679 msg("The ENTIRE dump is aborted.\n");
680 exit(X_STARTUP);
681 }
682 #ifdef BLKFLSBUF
683 (void)ioctl(diskfd, BLKFLSBUF);
684 #endif
685 retval = dump_fs_open(disk, &fs);
686 if (retval) {
687 com_err(disk, retval, "while opening filesystem");
688 if (retval == EXT2_ET_REV_TOO_HIGH)
689 msg("Get a newer version of dump!\n");
690 msg("The ENTIRE dump is aborted.\n");
691 exit(X_STARTUP);
692 }
693 if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
694 com_err(disk, retval, "while opening filesystem");
695 msg("Get a newer version of dump!\n");
696 msg("The ENTIRE dump is aborted.\n");
697 exit(X_STARTUP);
698 }
699 /* if no user label specified, use ext2 filesystem label if available */
700 if (spcl.c_label[0] == '\0') {
701 const char *lbl;
702 if ( (lbl = get_device_label(disk)) != NULL) {
703 strncpy(spcl.c_label, lbl, LBLSIZE);
704 spcl.c_label[LBLSIZE-1] = '\0';
705 }
706 else
707 strcpy(spcl.c_label, "none"); /* safe strcpy. */
708 }
709 sync();
710 dev_bsize = DEV_BSIZE;
711 dev_bshift = ffs(dev_bsize) - 1;
712 if (dev_bsize != (1 << dev_bshift))
713 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
714 tp_bshift = ffs(TP_BSIZE) - 1;
715 if (TP_BSIZE != (1 << tp_bshift))
716 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
717 maxino = fs->super->s_inodes_count + 1;
718 spcl.c_flags |= DR_NEWINODEFMT;
719 #else /* __linux __*/
720 if ((diskfd = open(disk, O_RDONLY)) < 0) {
721 msg("Cannot open %s\n", disk);
722 msg("The ENTIRE dump is aborted.\n");
723 exit(X_STARTUP);
724 }
725 sync();
726 sblock = (struct fs *)sblock_buf;
727 bread(SBOFF, (char *) sblock, SBSIZE);
728 if (sblock->fs_magic != FS_MAGIC)
729 quit("bad sblock magic number\n");
730 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
731 dev_bshift = ffs(dev_bsize) - 1;
732 if (dev_bsize != (1 << dev_bshift))
733 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
734 tp_bshift = ffs(TP_BSIZE) - 1;
735 if (TP_BSIZE != (1 << tp_bshift))
736 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
737 #ifdef FS_44INODEFMT
738 if (sblock->fs_inodefmt >= FS_44INODEFMT)
739 spcl.c_flags |= DR_NEWINODEFMT;
740 #endif
741 maxino = sblock->fs_ipg * sblock->fs_ncg;
742 #endif /* __linux__ */
743 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
744 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
745 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
746 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
747 metainomap = (char *)calloc((unsigned) mapsize, sizeof(char));
748 if (usedinomap == NULL || dumpdirmap == NULL ||
749 dumpinomap == NULL || metainomap == NULL)
750 quit("out of memory allocating inode maps\n");
751 tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
752
753 nonodump = spcl.c_level < honorlevel;
754
755 if (!sizest) {
756 msg("Label: %s\n", spcl.c_label);
757
758 msg("Writing %d Kilobyte records\n", ntrec);
759
760 if (compressed)
761 msg("Compressing output at compression level %d (%s)\n",
762 compressed, bzipflag ? "bzlib" : "zlib");
763 }
764
765 #if defined(SIGINFO)
766 (void)signal(SIGINFO, statussig);
767 #endif
768
769 if (!sizest)
770 msg("mapping (Pass I) [regular files]\n");
771 #ifdef __linux__
772 if (directory[0] == 0)
773 anydirskipped = mapfiles(maxino, &tapesize);
774 else {
775 if (LSTAT(pathname, &statbuf) == -1) {
776 msg("File cannot be accessed (%s).\n", pathname);
777 msg("The ENTIRE dump is aborted.\n");
778 exit(X_STARTUP);
779 }
780 filedev = statbuf.st_dev;
781 if (!(statbuf.st_mode & S_IFDIR)) /* is a file */
782 anydirskipped = maponefile(maxino, &tapesize,
783 directory);
784 else
785 anydirskipped = mapfilesfromdir(maxino, &tapesize,
786 directory);
787 }
788 while (argc--) {
789 int anydirskipped2;
790 char *p = *argv;
791 /* check if file is available */
792 if (LSTAT(p, &statbuf) == -1) {
793 msg("File cannot be accessed (%s).\n", p);
794 msg("The ENTIRE dump is aborted.\n");
795 exit(X_STARTUP);
796 }
797 /* check if file is on same unix partiton as the first
798 * argument */
799 if (statbuf.st_dev != filedev) {
800 msg("Files are not on same file system (%s).\n", p);
801 msg("The ENTIRE dump is aborted.\n");
802 exit(X_STARTUP);
803 }
804 /* check if file is a directory */
805 if (!(statbuf.st_mode & S_IFDIR))
806 anydirskipped2 = maponefile(maxino, &tapesize,
807 p+strlen(dt->mnt_dir));
808 else
809 /* read directory inodes.
810 * NOTE: nested directories are not recognized
811 * so inodes may be umped twice!
812 */
813 anydirskipped2 = mapfilesfromdir(maxino, &tapesize,
814 p+strlen(dt->mnt_dir));
815 if (!anydirskipped)
816 anydirskipped = anydirskipped2;
817 argv++;
818 }
819 #else
820 anydirskipped = mapfiles(maxino, &tapesize);
821 #endif
822
823 if (!sizest)
824 msg("mapping (Pass II) [directories]\n");
825 while (anydirskipped) {
826 anydirskipped = mapdirs(maxino, &tapesize);
827 }
828
829 if (sizest) {
830 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
831 exit(X_FINOK);
832 } /* stop here for size estimate */
833
834 if (pipeout || unlimited) {
835 tapesize += 1 + ntrec; /* 1 map header + trailer blocks */
836 msg("estimated %ld blocks.\n", tapesize);
837 } else {
838 double fetapes;
839
840 if (blocksperfile)
841 fetapes = (double) tapesize / blocksperfile;
842 else if (cartridge) {
843 /* Estimate number of tapes, assuming streaming stops at
844 the end of each block written, and not in mid-block.
845 Assume no erroneous blocks; this can be compensated
846 for with an artificially low tape size. */
847 fetapes =
848 ( (double) tapesize /* blocks */
849 * TP_BSIZE /* bytes/block */
850 * (1.0/density) /* 0.1" / byte " */
851 +
852 (double) tapesize /* blocks */
853 * (1.0/ntrec) /* streaming-stops per block */
854 * 15.48 /* 0.1" / streaming-stop " */
855 ) * (1.0 / tsize ); /* tape / 0.1" " */
856 } else {
857 /* Estimate number of tapes, for old fashioned 9-track
858 tape */
859 int tenthsperirg = (density == 625) ? 3 : 7;
860 fetapes =
861 ( (double) tapesize /* blocks */
862 * TP_BSIZE /* bytes / block */
863 * (1.0/density) /* 0.1" / byte " */
864 +
865 (double) tapesize /* blocks */
866 * (1.0/ntrec) /* IRG's / block */
867 * tenthsperirg /* 0.1" / IRG " */
868 ) * (1.0 / tsize ); /* tape / 0.1" " */
869 }
870 etapes = fetapes; /* truncating assignment */
871 etapes++;
872 /* count the dumped inodes map on each additional tape */
873 tapesize += (etapes - 1) *
874 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
875 tapesize += etapes + ntrec; /* headers + trailer blks */
876 msg("estimated %ld blocks on %3.2f tape(s).\n",
877 tapesize, fetapes);
878 }
879
880 #ifdef USE_QFA
881 if (tapepos) {
882 msg("writing QFA positions to %s\n", gTapeposfile);
883 if ((gTapeposfd = open(gTapeposfile, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR)) < 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) {
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)
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 }