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
10 * Copyright (c) 1980, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
39 static const char rcsid[] =
40 "$Id: main.c,v 1.94 2004/07/05 15:12:45 stelian Exp $";
44 #include <compatlfs.h>
46 #include <compaterr.h>
57 #include <sys/param.h>
61 #include <linux/types.h>
62 #ifdef HAVE_EXT2FS_EXT2_FS_H
63 #include <ext2fs/ext2_fs.h>
65 #include <linux/ext2_fs.h>
67 #include <ext2fs/ext2fs.h>
69 #include <bsdcompat.h>
70 #include <linux/fs.h> /* for definition of BLKFLSBUF */
72 #include <sys/vnode.h>
74 #include <ufs/inode.h>
77 #include <ufs/ufs/dinode.h>
78 #include <ufs/ffs/fs.h>
81 #include <protocols/dumprestore.h>
84 #include "pathnames.h"
88 #define SBOFF (SBLOCK * DEV_BSIZE)
91 int abortifconnerr = 1; /* set to 1 if lib dumprmt.o should exit on connection errors
92 otherwise just print a message using msg */
94 * Dump maps used to describe what is to be dumped.
96 int mapsize; /* size of the state maps */
97 char *usedinomap; /* map of allocated inodes */
98 char *dumpdirmap; /* map of directories to be dumped */
99 char *dumpinomap; /* map of files to be dumped */
100 char *metainomap; /* which of the inodes in dumpinomap will get
101 only their metadata dumped */
103 const char *disk; /* name of the disk file */
104 char tape[MAXPATHLEN];/* name of the tape file */
105 char *tapeprefix; /* prefix of the tape file */
106 char *dumpdates; /* name of the file containing dump date information*/
107 char lastlevel[NUM_STR_SIZE];/* dump level of previous dump */
108 char level[NUM_STR_SIZE];/* dump level of this dump */
109 int zipflag; /* which compression method */
110 int Afile = -1; /* archive file descriptor */
111 int AfileActive = 1;/* Afile flag */
112 int uflag; /* update flag */
113 int mflag; /* dump metadata only if possible */
114 int Mflag; /* multi-volume flag */
115 int qflag; /* quit on errors flag */
116 int vflag; /* verbose flag */
117 int breademax = 32; /* maximum number of bread errors before we quit */
118 char *eot_script; /* end of volume script fiag */
119 int diskfd; /* disk file descriptor */
120 int tapefd; /* tape file descriptor */
121 int pipeout; /* true => output to standard output */
122 int fifoout; /* true => output to fifo */
123 dump_ino_t curino; /* current inumber; used globally */
124 int newtape; /* new tape flag */
125 int density; /* density in 0.1" units */
126 long tapesize; /* estimated tape size, blocks */
127 long tsize; /* tape size in 0.1" units */
128 long asize; /* number of 0.1" units written on current tape */
129 int etapes; /* estimated number of tapes */
130 int nonodump; /* if set, do not honor UF_NODUMP user flags */
131 int unlimited; /* if set, write to end of medium */
132 int compressed; /* if set, dump is to be compressed */
133 long long bytes_written;/* total bytes written to tape */
134 long uncomprblks; /* uncompressed blocks written to tape */
135 int notify; /* notify operator flag */
136 int blockswritten; /* number of blocks written on current tape */
137 int tapeno; /* current tape number */
138 time_t tstart_writing; /* when started writing the first tape block */
139 time_t tend_writing; /* after writing the last tape block */
143 struct fs *sblock; /* the file system super block */
144 char sblock_buf[MAXBSIZE];
146 long xferrate; /* averaged transfer rate of all volumes */
147 long dev_bsize; /* block size of underlying disk device */
148 int dev_bshift; /* log2(dev_bsize) */
149 int tp_bshift; /* log2(TP_BSIZE) */
150 dump_ino_t volinfo[TP_NINOS];/* which inode on which volume archive info */
156 int32_t gThisDumpDate;
159 struct dumptime *dthead; /* head of the list version */
160 int nddates; /* number of records (might be zero) */
161 int ddates_in; /* we have read the increment file */
162 struct dumpdates **ddatev; /* the arrayfied version */
164 int notify = 0; /* notify operator flag */
165 int blockswritten = 0; /* number of blocks written on current tape */
166 int tapeno = 0; /* current tape number */
167 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
168 int ntrec = NTREC; /* # blocks in each tape record */
169 int cartridge = 0; /* Assume non-cartridge tape */
171 int tapepos = 0; /* assume no QFA tapeposition needed by user */
173 int dokerberos = 0; /* Use Kerberos authentication */
174 long dev_bsize = 1; /* recalculated below */
175 long *blocksperfiles = NULL; /* output blocks per file(s) */
176 char *host = NULL; /* remote host (if any) */
177 int sizest = 0; /* return size estimate only */
178 int compressed = 0; /* use zlib to compress the output, compress level 1-9 */
179 long long bytes_written = 0; /* total bytes written */
180 long uncomprblks = 0;/* uncompressed blocks written */
188 int maxbsize = 1024*1024; /* XXX MAXBSIZE from sys/param.h */
189 static long numarg __P((const char *, long, long));
190 static long *numlistarg __P((const char *, long, long));
191 static void obsolete __P((int *, char **[]));
192 static void usage __P((void));
193 static void do_exclude_from_file __P((char *));
194 static void do_exclude_ino_str __P((char *));
195 static void incompat_flags __P((int, char, char));
197 static char* iexclude_bitmap = NULL; /* the inode exclude bitmap */
198 static unsigned int iexclude_bitmap_bytes = 0; /* size of bitmap in bytes */
201 main(int argc, char *argv[])
209 int i, anydirskipped;
210 int aflag = 0, bflag = 0, Tflag = 0, honorlevel = 1;
216 char directory[MAXPATHLEN];
217 char pathname[MAXPATHLEN];
223 spcl.c_label[0] = '\0';
224 spcl.c_date = time(NULL);
227 __progname = argv[0];
229 initialize_ext2_error_table();
232 tsize = 0; /* Default later, based on 'c' option for cart tapes */
235 if ((tapeprefix = getenv("TAPE")) == NULL)
236 tapeprefix = _PATH_DEFTAPE;
237 dumpdates = _PATH_DUMPDATES;
238 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
239 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
240 memset(&lastlevel, 0, NUM_STR_SIZE);
241 memset(&level, 0, NUM_STR_SIZE);
246 obsolete(&argc, &argv);
252 while ((ch = getopt(argc, argv,
253 "0123456789A:aB:b:cd:D:e:E:f:F:h:I:"
275 case '0': case '1': case '2': case '3': case '4':
276 case '5': case '6': case '7': case '8': case '9':
277 if ((pch >= '0') && (pch <= '9') && (strlen(level) < NUM_STR_SIZE))
278 level[strlen(level)] = ch;
284 case 'A': /* archive file */
288 case 'a': /* `auto-size', Write to EOM. */
293 case 'B': /* blocks per output file */
295 blocksperfiles = numlistarg("number of blocks per file",
299 case 'b': /* blocks per tape write */
300 ntrec = numarg("number of blocks per write",
302 if (ntrec > maxbsize/1024) {
303 msg("Please choose a blocksize <= %dkB\n",
305 msg("The ENTIRE dump is aborted.\n");
311 case 'c': /* Tape is cart. not 9-track */
316 case 'd': /* density, in bits per inch */
318 density = numarg("density", 10L, 327670L) / 10;
319 if (density >= 625 && !bflag)
320 ntrec = HIGHDENSITYTREC;
323 case 'D': /* path of dumpdates file */
328 case 'e': /* exclude an inode */
330 char *p = optarg, *q;
331 while ((q = strchr(p, ','))) {
333 do_exclude_ino_str(p);
336 do_exclude_ino_str(p);
340 case 'E': /* exclude inodes read from file */
341 do_exclude_from_file(optarg);
344 case 'f': /* output file */
348 case 'F': /* end of tape script */
353 honorlevel = numarg("honor level", 0L, 10L);
359 zipflag = COMPRESS_BZLIB;
361 compressed = numarg("compress level", 1L, 9L);
363 #endif /* HAVE_BZLIB */
367 numarg ("number of errors to ignore", 0L, 0L);
378 * Note that although there are LBLSIZE characters,
379 * the last must be '\0', so the limit on strlen()
380 * is really LBLSIZE-1.
382 strncpy(spcl.c_label, optarg, LBLSIZE);
383 spcl.c_label[LBLSIZE-1] = '\0';
384 if (strlen(optarg) > LBLSIZE-1) {
386 "WARNING Label `%s' is larger than limit of %d characters.\n",
388 msg("WARNING: Using truncated label `%s'.\n",
393 case 'm': /* metadata only flag */
397 case 'M': /* multi-volume flag */
401 case 'n': /* notify operators */
410 case 'Q': /* create tapeposfile */
411 gTapeposfile = optarg;
416 case 's': /* tape size, feet */
418 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
422 sizest = 1; /* return size estimate only */
425 case 'T': /* time of last dump */
426 spcl.c_ddate = unctime(optarg);
427 if (spcl.c_ddate < 0) {
428 msg("bad time \"%s\"\n", optarg);
429 msg("The ENTIRE dump is aborted.\n");
433 lastlevel[0] = '?'; lastlevel[1] = '\0';
436 case 'u': /* update dumpdates */
440 case 'v': /* verbose */
444 case 'W': /* what to do */
447 exit(X_FINOK); /* do nothing else */
451 zipflag = COMPRESS_LZO;
453 #endif /* HAVE_LZO */
458 zipflag = COMPRESS_ZLIB;
460 compressed = numarg("compress level", 1L, 9L);
462 #endif /* HAVE_ZLIB */
471 msg("Must specify disk or filesystem\n");
472 msg("The ENTIRE dump is aborted.\n");
476 if (strlen(diskparam) >= MAXPATHLEN) {
477 msg("Disk or filesystem name too long: %s\n", diskparam);
478 msg("The ENTIRE dump is aborted.\n");
482 incompat_flags(Tflag && uflag, 'T', 'u');
483 incompat_flags(aflag && blocksperfiles, 'a', 'B');
484 incompat_flags(aflag && cartridge, 'a', 'c');
485 incompat_flags(aflag && density, 'a', 'd');
486 incompat_flags(aflag && tsize, 'a', 's');
488 if (strcmp(tapeprefix, "-") == 0) {
490 tapeprefix = "standard output";
493 if (blocksperfiles && !compressed)
494 for (i = 1; i <= *blocksperfiles; i++)
495 blocksperfiles[i] = blocksperfiles[i] / ntrec * ntrec; /* round down */
496 else if (!unlimited) {
498 * Determine how to default tape size and density
501 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
502 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
503 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
505 * hilit19 hits again: "
508 density = cartridge ? 100 : 160;
510 tsize = cartridge ? 1700L*120L : 2300L*120L;
517 if ((n = strchr(tapeprefix, ':'))) {
518 for (i = 0; i < (n - tapeprefix); i++) {
519 if (tapeprefix[i] == '/')
522 if (tapeprefix[i] != '/') {
524 tapeprefix = strchr(host, ':');
525 *tapeprefix++ = '\0';
527 if (index(tapeprefix, '\n')) {
528 msg("invalid characters in tape\n");
529 msg("The ENTIRE dump is aborted.\n");
532 if (rmthost(host) == 0)
535 msg("remote dump not enabled\n");
536 msg("The ENTIRE dump is aborted.\n");
543 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
544 if (Apath && (Afile = open(Apath, O_WRONLY|O_CREAT|O_TRUNC,
545 S_IRUSR | S_IWUSR | S_IRGRP |
546 S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
547 msg("Cannot open %s for writing: %s\n",
548 optarg, strerror(errno));
549 msg("The ENTIRE dump is aborted.\n");
553 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
555 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
556 signal(SIGTRAP, sig);
557 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
559 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
561 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
562 signal(SIGSEGV, sig);
563 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
564 signal(SIGTERM, sig);
565 if (signal(SIGINT, interrupt) == SIG_IGN)
566 signal(SIGINT, SIG_IGN);
568 signal(SIGXCPU, SIG_IGN);
571 signal(SIGXFSZ, SIG_IGN);
574 set_operators(); /* /etc/group snarfed */
575 getfstab(); /* /etc/fstab snarfed */
578 * disk may end in / and this can confuse
581 i = strlen(diskparam) - 1;
582 if (i > 1 && diskparam[i] == '/')
583 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
586 disk = get_device_name(diskparam);
588 disk = strdup(diskparam);
591 * disk can be either the full special file name,
592 * the suffix of the special file name,
593 * the special name missing the leading '/',
594 * the file system name with or without the leading '/'.
596 if ((dt = fstabsearch(disk)) != NULL) {
597 /* if found then only one parameter (i.e. partition)
600 (void)fprintf(stderr, "Unknown arguments to dump:");
602 (void)fprintf(stderr, " %s", *argv++);
603 (void)fprintf(stderr, "\n");
604 msg("The ENTIRE dump is aborted.\n");
607 disk = rawname(dt->mnt_fsname);
608 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
609 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
613 if (realpath(disk, pathname) == NULL)
615 strcpy(pathname, disk);
617 * The argument could be now a mountpoint of
618 * a filesystem specified in fstab. Search for it.
620 if ((dt = fstabsearch(pathname)) != NULL) {
621 disk = rawname(dt->mnt_fsname);
622 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
623 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
626 * The argument was not found in the fstab
627 * assume that this is a subtree and search for it
629 dt = fstabsearchdir(pathname, directory);
631 char name[MAXPATHLEN];
632 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
633 (void)snprintf(name, sizeof(name), "%s (dir %s)",
634 dt->mnt_dir, directory);
635 (void)strncpy(spcl.c_filesys, name, NAMELEN);
636 disk = rawname(dt->mnt_fsname);
638 (void)strncpy(spcl.c_dev, disk, NAMELEN);
639 (void)strncpy(spcl.c_filesys, "an unlisted file system",
644 (void)strncpy(spcl.c_dev, disk, NAMELEN);
645 (void)strncpy(spcl.c_filesys, "an unlisted file system",
650 if (directory[0] != 0) {
651 if (atoi(level) != 0) {
652 msg("Only level 0 dumps are allowed on a subdirectory\n");
653 msg("The ENTIRE dump is aborted.\n");
657 msg("You can't update the dumpdates file when dumping a subdirectory\n");
658 msg("The ENTIRE dump is aborted.\n");
662 spcl.c_dev[NAMELEN-1] = '\0';
663 spcl.c_filesys[NAMELEN-1] = '\0';
664 (void)gethostname(spcl.c_host, NAMELEN);
665 spcl.c_host[NAMELEN-1] = '\0';
666 spcl.c_level = atoi(level);
667 spcl.c_type = TS_TAPE;
669 getdumptime(uflag); /* dumpdates snarfed */
671 if (spcl.c_ddate == 0 && spcl.c_level) {
672 msg("WARNING: There is no inferior level dump on this filesystem\n");
673 msg("WARNING: Assuming a level 0 dump by default\n");
674 level[0] = '0'; level[1] = '\0';
679 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
681 strncpy(tape, tapeprefix, MAXPATHLEN);
682 tape[MAXPATHLEN - 1] = '\0';
685 if (STAT(tape, &statbuf) != -1)
686 fifoout= statbuf.st_mode & S_IFIFO;
691 msg("Date of this level %s dump: %s", level,
692 ctime4(&spcl.c_date));
694 gThisDumpDate = spcl.c_date;
697 msg("Date of last level %s dump: %s", lastlevel,
698 ctime4(&spcl.c_ddate));
699 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
701 msgtail("to %s on host %s\n", tape, host);
703 msgtail("to %s\n", tape);
704 } /* end of size estimate */
707 if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
708 msg("Cannot open %s\n", disk);
709 msg("The ENTIRE dump is aborted.\n");
713 (void)ioctl(diskfd, BLKFLSBUF, 0);
715 retval = dump_fs_open(disk, &fs);
717 com_err(disk, retval, "while opening filesystem");
718 if (retval == EXT2_ET_REV_TOO_HIGH)
719 msg("Get a newer version of dump!\n");
720 msg("The ENTIRE dump is aborted.\n");
723 if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
724 com_err(disk, retval, "while opening filesystem");
725 msg("Get a newer version of dump!\n");
726 msg("The ENTIRE dump is aborted.\n");
729 /* if no user label specified, use ext2 filesystem label if available */
730 if (spcl.c_label[0] == '\0') {
732 if ( (lbl = get_device_label(disk)) != NULL) {
733 strncpy(spcl.c_label, lbl, LBLSIZE);
734 spcl.c_label[LBLSIZE-1] = '\0';
737 strcpy(spcl.c_label, "none"); /* safe strcpy. */
740 dev_bsize = DEV_BSIZE;
741 dev_bshift = ffs(dev_bsize) - 1;
742 if (dev_bsize != (1 << dev_bshift))
743 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
744 tp_bshift = ffs(TP_BSIZE) - 1;
745 if (TP_BSIZE != (1 << tp_bshift))
746 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
747 maxino = fs->super->s_inodes_count + 1;
748 spcl.c_flags |= DR_NEWINODEFMT;
749 #else /* __linux __*/
750 if ((diskfd = open(disk, O_RDONLY)) < 0) {
751 msg("Cannot open %s\n", disk);
752 msg("The ENTIRE dump is aborted.\n");
756 sblock = (struct fs *)sblock_buf;
757 bread(SBOFF, (char *) sblock, SBSIZE);
758 if (sblock->fs_magic != FS_MAGIC)
759 quit("bad sblock magic number\n");
760 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
761 dev_bshift = ffs(dev_bsize) - 1;
762 if (dev_bsize != (1 << dev_bshift))
763 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
764 tp_bshift = ffs(TP_BSIZE) - 1;
765 if (TP_BSIZE != (1 << tp_bshift))
766 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
768 if (sblock->fs_inodefmt >= FS_44INODEFMT)
769 spcl.c_flags |= DR_NEWINODEFMT;
771 maxino = sblock->fs_ipg * sblock->fs_ncg;
772 #endif /* __linux__ */
773 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
774 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
775 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
776 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
777 metainomap = (char *)calloc((unsigned) mapsize, sizeof(char));
778 if (usedinomap == NULL || dumpdirmap == NULL ||
779 dumpinomap == NULL || metainomap == NULL)
780 quit("out of memory allocating inode maps\n");
781 tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
783 nonodump = spcl.c_level < honorlevel;
786 msg("Label: %s\n", spcl.c_label);
788 msg("Writing %d Kilobyte records\n", ntrec);
791 if (zipflag == COMPRESS_LZO)
792 msg("Compressing output (lzo)\n");
794 msg("Compressing output at compression level %d (%s)\n",
795 compressed, zipflag == COMPRESS_ZLIB ? "zlib" : "bzlib");
800 (void)signal(SIGINFO, statussig);
804 msg("mapping (Pass I) [regular files]\n");
806 if (directory[0] == 0)
807 anydirskipped = mapfiles(maxino, &tapesize);
809 if (LSTAT(pathname, &statbuf) == -1) {
810 msg("File cannot be accessed (%s).\n", pathname);
811 msg("The ENTIRE dump is aborted.\n");
814 filedev = statbuf.st_dev;
815 if (!(statbuf.st_mode & S_IFDIR)) /* is a file */
816 anydirskipped = maponefile(maxino, &tapesize,
819 anydirskipped = mapfilesfromdir(maxino, &tapesize,
825 /* check if file is available */
826 if (LSTAT(p, &statbuf) == -1) {
827 msg("File cannot be accessed (%s).\n", p);
828 msg("The ENTIRE dump is aborted.\n");
831 /* check if file is on same unix partiton as the first
833 if (statbuf.st_dev != filedev) {
834 msg("Files are not on same file system (%s).\n", p);
835 msg("The ENTIRE dump is aborted.\n");
838 /* check if file is a directory */
839 if (!(statbuf.st_mode & S_IFDIR))
840 anydirskipped2 = maponefile(maxino, &tapesize,
841 p+strlen(dt->mnt_dir));
843 /* read directory inodes.
844 * NOTE: nested directories are not recognized
845 * so inodes may be umped twice!
847 anydirskipped2 = mapfilesfromdir(maxino, &tapesize,
848 p+strlen(dt->mnt_dir));
850 anydirskipped = anydirskipped2;
854 anydirskipped = mapfiles(maxino, &tapesize);
858 msg("mapping (Pass II) [directories]\n");
859 while (anydirskipped) {
860 anydirskipped = mapdirs(maxino, &tapesize);
864 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
866 } /* stop here for size estimate */
868 if (pipeout || unlimited) {
869 tapesize += 1 + ntrec; /* 1 map header + trailer blocks */
870 msg("estimated %ld blocks.\n", tapesize);
874 if (blocksperfiles) {
877 tapesize_left = tapesize;
879 for (i = 1; i < *blocksperfiles && tapesize_left; i++) {
881 if (tapesize_left > blocksperfiles[i])
882 tapesize_left -= blocksperfiles[i];
887 fetapes += (double)tapesize_left / blocksperfiles[*blocksperfiles];
888 } else if (cartridge) {
889 /* Estimate number of tapes, assuming streaming stops at
890 the end of each block written, and not in mid-block.
891 Assume no erroneous blocks; this can be compensated
892 for with an artificially low tape size. */
894 ( (double) tapesize /* blocks */
895 * TP_BSIZE /* bytes/block */
896 * (1.0/density) /* 0.1" / byte " */
898 (double) tapesize /* blocks */
899 * (1.0/ntrec) /* streaming-stops per block */
900 * 15.48 /* 0.1" / streaming-stop " */
901 ) * (1.0 / tsize ); /* tape / 0.1" " */
903 /* Estimate number of tapes, for old fashioned 9-track
905 int tenthsperirg = (density == 625) ? 3 : 7;
907 ( (double) tapesize /* blocks */
908 * TP_BSIZE /* bytes / block */
909 * (1.0/density) /* 0.1" / byte " */
911 (double) tapesize /* blocks */
912 * (1.0/ntrec) /* IRG's / block */
913 * tenthsperirg /* 0.1" / IRG " */
914 ) * (1.0 / tsize ); /* tape / 0.1" " */
916 etapes = fetapes; /* truncating assignment */
918 /* count the dumped inodes map on each additional tape */
919 tapesize += (etapes - 1) *
920 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
921 tapesize += etapes + ntrec; /* headers + trailer blks */
922 msg("estimated %ld blocks on %3.2f tape(s).\n",
928 msg("writing QFA positions to %s\n", gTapeposfile);
929 if ((gTapeposfd = open(gTapeposfile,
930 O_WRONLY|O_CREAT|O_TRUNC,
931 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
932 | S_IROTH | S_IWOTH)) < 0)
933 quit("can't open tapeposfile\n");
934 /* print QFA-file header */
935 snprintf(gTps, sizeof(gTps), "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
936 gTps[sizeof(gTps) - 1] = '\0';
937 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
938 quit("can't write tapeposfile\n");
939 sprintf(gTps, "ino\ttapeno\ttapepos\n");
940 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
941 quit("can't write tapeposfile\n");
946 * Allocate tape buffer.
950 "can't allocate tape buffers - try a smaller blocking factor.\n");
953 tstart_writing = time(NULL);
954 dumpmap(usedinomap, TS_CLRI, maxino - 1);
956 msg("dumping (Pass III) [directories]\n");
957 dirty = 0; /* XXX just to get gcc to shut up */
958 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
959 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
963 if ((dirty & 1) == 0)
966 * Skip directory inodes deleted and maybe reallocated
969 if ((dp->di_mode & IFMT) != IFDIR)
973 * Skip directory inodes deleted and not yes reallocated...
975 if (dp->di_nlink == 0 || dp->di_dtime != 0)
978 msg("dumping directory inode %lu\n", ino);
979 (void)dumpdirino(dp, ino);
981 (void)dumpino(dp, ino);
985 msg("dumping (Pass IV) [regular files]\n");
986 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
987 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
991 if ((dirty & 1) == 0)
994 * Skip inodes deleted and reallocated as directories.
997 if ((dp->di_mode & IFMT) == IFDIR)
1001 * No need to check here for deleted and not yet reallocated
1002 * inodes since this is done in dumpino().
1006 if (mflag && TSTINO(ino, metainomap))
1007 msg("dumping regular inode %lu (meta only)\n", ino);
1009 msg("dumping regular inode %lu\n", ino);
1011 (void)dumpino(dp, ino, mflag && TSTINO(ino, metainomap));
1014 tend_writing = time(NULL);
1015 spcl.c_type = TS_END;
1018 volinfo[1] = ROOTINO;
1019 memcpy(spcl.c_inos, volinfo, TP_NINOS * sizeof(dump_ino_t));
1020 spcl.c_flags |= DR_INODEINFO;
1024 * Finish off the current tape record with trailer blocks, to ensure
1025 * at least the data in the last partial record makes it to tape.
1026 * Also make sure we write at least 1 trailer block.
1028 for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
1029 writeheader(maxino - 1);
1033 if (pipeout || fifoout)
1034 msg("%ld blocks (%.2fMB)\n", spcl.c_tapea,
1035 ((double)spcl.c_tapea * TP_BSIZE / 1048576));
1037 msg("%ld blocks (%.2fMB) on %d volume(s)\n",
1039 ((double)spcl.c_tapea * TP_BSIZE / 1048576),
1042 /* report dump performance, avoid division by zero */
1043 if (tend_writing - tstart_writing == 0)
1044 msg("finished in less than a second\n");
1046 msg("finished in %d seconds, throughput %d kBytes/sec\n",
1047 tend_writing - tstart_writing,
1048 spcl.c_tapea / (tend_writing - tstart_writing));
1051 msg("Date of this level %s dump: %s", level,
1052 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
1053 msg("Date this dump completed: %s", ctime(&tnow));
1055 msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
1057 long tapekb = bytes_written / 1024;
1058 double rate = .0005 + (double) spcl.c_tapea / tapekb;
1059 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
1060 spcl.c_tapea, tapekb, rate);
1064 msg("Archiving dump to %s\n", Apath);
1066 broadcast("DUMP IS DONE!\7\7\n");
1067 msg("DUMP IS DONE\n");
1070 return 0; /* gcc - shut up */
1076 char white[MAXPATHLEN];
1077 const char *ext2ver, *ext2date;
1079 memset(white, ' ', MAXPATHLEN);
1080 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
1083 ext2fs_get_library_version(&ext2ver, &ext2date);
1084 fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
1085 __progname, _DUMP_VERSION, ext2ver, ext2date);
1087 fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
1090 "usage:\t%s [-level#] [-ac"
1095 "] [-A file] [-B records] [-b blocksize]\n"
1096 "\t%s [-d density] [-D file] [-e inode#,inode#,...] [-E file]\n"
1097 "\t%s [-f file] [-h level] [-I nr errors] "
1114 __progname, white, white, white, __progname);
1119 * Pick up a numeric argument. It must be nonnegative and in the given
1120 * range (except that a vmax of 0 means unlimited).
1123 numarg(const char *meaning, long vmin, long vmax)
1128 val = strtol(optarg, &p, 10);
1130 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1131 if (val < vmin || (vmax && val > vmax))
1132 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1137 * The same as numarg, just that it expects a comma separated list of numbers
1138 * and returns an array of longs with the first element containing the number
1139 * values in that array.
1142 numlistarg(const char *meaning, long vmin, long vmax)
1153 if ( !(vals = realloc(vals, (valnum + 1) * sizeof(*vals))) )
1154 errx(X_STARTUP, "allocating memory failed");
1155 curval = vals + valnum;
1156 *curval = strtol(p, &p, 10);
1158 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1159 if (*curval < vmin || (vmax && *curval > vmax))
1160 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1177 if (pipeout || fifoout)
1178 quit("Signal on pipe: cannot recover\n");
1179 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
1180 (void)fflush(stderr);
1181 (void)fflush(stdout);
1186 msg("SIGSEGV: ABORTING!\n");
1187 (void)signal(SIGSEGV, SIG_DFL);
1188 (void)kill(0, SIGSEGV);
1194 rawname(const char *cp)
1198 #else /* __linux__ */
1199 static char rawbuf[MAXPATHLEN];
1200 char *dp = strrchr(cp, '/');
1204 (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1205 rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1206 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1207 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1209 #endif /* __linux__ */
1214 * Change set of key letters and ordered arguments into something
1215 * getopt(3) will like.
1218 obsolete(int *argcp, char **argvp[])
1221 char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1227 /* Return if no arguments or first argument has leading dash. */
1229 if (argc == 1 || *ap == '-')
1232 /* Allocate space for new arguments. */
1233 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1234 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1235 err(X_STARTUP, "malloc new args");
1240 for (flags = 0; *ap; ++ap) {
1256 if (*argv == NULL) {
1257 warnx("option requires an argument -- %c", *ap);
1260 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1261 err(X_STARTUP, "malloc arg");
1264 (void)strcpy(&nargv[0][2], *argv);
1278 /* Terminate flags. */
1284 /* Copy remaining arguments. */
1285 while ((*nargv++ = *argv++));
1287 /* Update argument count. */
1288 *argcp = nargv - *argvp - 1;
1292 * This tests whether an inode is in the exclude bitmap
1295 exclude_ino(dump_ino_t ino)
1297 /* if the inode exclude bitmap exists and covers given inode */
1298 if (iexclude_bitmap && iexclude_bitmap_bytes > ino / 8) {
1299 /* then check this inode against it */
1300 int idx = iexclude_bitmap[ino / 8];
1301 if (idx & (1 << (ino % 8)))
1308 * This adds an inode to the exclusion bitmap if it isn't already there
1311 do_exclude_ino(dump_ino_t ino, const char *reason)
1313 if (exclude_ino(ino))
1318 msg("Excluding inode %u (%s) from dump\n", ino, reason);
1320 msg("Excluding inode %u from dump\n", ino);
1323 /* check for enough mem; initialize */
1324 if ((ino/8 + 1) > iexclude_bitmap_bytes) {
1325 if (iexclude_bitmap_bytes == 0) {
1327 iexclude_bitmap_bytes = 2 * (ino/8 + 1);
1328 iexclude_bitmap = (char*) malloc(iexclude_bitmap_bytes);
1329 if (iexclude_bitmap == NULL) {
1330 msg("allocating memory failed\n");
1333 for (j = 0; j < iexclude_bitmap_bytes; j++)
1334 iexclude_bitmap[j] = 0;
1337 unsigned int oldsize = iexclude_bitmap_bytes;
1338 iexclude_bitmap_bytes *=
1339 (ino / 8 + 1) / iexclude_bitmap_bytes + 1;
1340 iexclude_bitmap = (char*) realloc(iexclude_bitmap,
1341 iexclude_bitmap_bytes);
1342 if (iexclude_bitmap == NULL) {
1343 msg("allocating memory failed\n");
1346 for( ; oldsize < iexclude_bitmap_bytes; oldsize++)
1347 iexclude_bitmap[oldsize] = 0;
1351 iexclude_bitmap[ino / 8] |= 1 << (ino % 8);
1355 do_exclude_ino_str(char * ino) {
1359 inod = strtoul(ino, &r, 10);
1360 if (( *r != '\0' && !isspace(*r) ) || inod <= ROOTINO) {
1361 msg("Invalid inode argument %s\n", ino);
1362 msg("The ENTIRE dump is aborted.\n");
1365 do_exclude_ino(inod, NULL);
1369 * This reads a file containing one inode number per line and exclude them all
1372 do_exclude_from_file(char *file) {
1374 char *p, fname[MAXPATHLEN];
1377 if (!( f = fopen(file, "r")) ) {
1378 msg("Cannot open file for reading: %s\n", file);
1379 msg("The ENTIRE dump is aborted.\n");
1382 while (( p = fgets(fname, MAXPATHLEN, f))) {
1383 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1384 *(p + strlen(p) - 1) = '\0';
1385 if ( !*p ) /* skip empty lines */
1387 do_exclude_ino_str(p);
1392 static void incompat_flags(int cond, char flag1, char flag2) {
1394 msg("You cannot use the %c and %c flags together.\n",
1396 msg("The ENTIRE dump is aborted.\n");