]> git.wh0rd.org - dump.git/blob - dump/main.c
Added LZO compression.
[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.85 2003/03/31 09:42:58 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 zipflag; /* which compression method */
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_LZO
258 "y"
259 #endif
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 break;
274
275 case 'a': /* `auto-size', Write to EOM. */
276 unlimited = 1;
277 aflag = 1;
278 break;
279
280 case 'B': /* blocks per output file */
281 unlimited = 0;
282 blocksperfile = numarg("number of blocks per file",
283 1L, 0L);
284 break;
285
286 case 'b': /* blocks per tape write */
287 ntrec = numarg("number of blocks per write",
288 1L, 1048576L);
289 if (ntrec > maxbsize/1024) {
290 msg("Please choose a blocksize <= %dkB\n",
291 maxbsize/1024);
292 msg("The ENTIRE dump is aborted.\n");
293 exit(X_STARTUP);
294 }
295 bflag = 1;
296 break;
297
298 case 'c': /* Tape is cart. not 9-track */
299 unlimited = 0;
300 cartridge = 1;
301 break;
302
303 case 'd': /* density, in bits per inch */
304 unlimited = 0;
305 density = numarg("density", 10L, 327670L) / 10;
306 if (density >= 625 && !bflag)
307 ntrec = HIGHDENSITYTREC;
308 break;
309
310 case 'D': /* path of dumpdates file */
311 dumpdates = optarg;
312 break;
313
314 /* 04-Feb-00 ILC */
315 case 'e': /* exclude an inode */
316 {
317 char *p = optarg, *q;
318 while ((q = strchr(p, ','))) {
319 *q = '\0';
320 do_exclude_ino_str(p);
321 p = q + 1;
322 }
323 do_exclude_ino_str(p);
324 }
325 break;
326
327 case 'E': /* exclude inodes read from file */
328 do_exclude_from_file(optarg);
329 break;
330
331 case 'f': /* output file */
332 tapeprefix = optarg;
333 break;
334
335 case 'F': /* end of tape script */
336 eot_script = optarg;
337 break;
338
339 case 'h':
340 honorlevel = numarg("honor level", 0L, 10L);
341 break;
342
343 #ifdef HAVE_BZLIB
344 case 'j':
345 compressed = 2;
346 zipflag = COMPRESS_BZLIB;
347 if (optarg)
348 compressed = numarg("compress level", 1L, 9L);
349 break;
350 #endif /* HAVE_BZLIB */
351
352 case 'I':
353 breademax =
354 numarg ("number of errors to ignore", 0L, 0L);
355 break;
356
357 #ifdef KERBEROS
358 case 'k':
359 dokerberos = 1;
360 break;
361 #endif
362
363 case 'L':
364 /*
365 * Note that although there are LBLSIZE characters,
366 * the last must be '\0', so the limit on strlen()
367 * is really LBLSIZE-1.
368 */
369 strncpy(spcl.c_label, optarg, LBLSIZE);
370 spcl.c_label[LBLSIZE-1] = '\0';
371 if (strlen(optarg) > LBLSIZE-1) {
372 msg(
373 "WARNING Label `%s' is larger than limit of %d characters.\n",
374 optarg, LBLSIZE-1);
375 msg("WARNING: Using truncated label `%s'.\n",
376 spcl.c_label);
377 }
378 break;
379
380 case 'm': /* metadata only flag */
381 mflag = 1;
382 break;
383
384 case 'M': /* multi-volume flag */
385 Mflag = 1;
386 break;
387
388 case 'n': /* notify operators */
389 notify = 1;
390 break;
391
392 case 'q':
393 qflag = 1;
394 break;
395
396 #ifdef USE_QFA
397 case 'Q': /* create tapeposfile */
398 gTapeposfile = optarg;
399 tapepos = 1;
400 break;
401 #endif /* USE_QFA */
402
403 case 's': /* tape size, feet */
404 unlimited = 0;
405 tsize = numarg("tape size", 1L, 0L) * 12 * 10;
406 break;
407
408 case 'S':
409 sizest = 1; /* return size estimate only */
410 break;
411
412 case 'T': /* time of last dump */
413 spcl.c_ddate = unctime(optarg);
414 if (spcl.c_ddate < 0) {
415 msg("bad time \"%s\"\n", optarg);
416 msg("The ENTIRE dump is aborted.\n");
417 exit(X_STARTUP);
418 }
419 Tflag = 1;
420 lastlevel = '?';
421 break;
422
423 case 'u': /* update dumpdates */
424 uflag = 1;
425 break;
426
427 case 'v': /* verbose */
428 vflag = 1;
429 break;
430
431 case 'W': /* what to do */
432 case 'w':
433 lastdump(ch);
434 exit(X_FINOK); /* do nothing else */
435 #ifdef HAVE_LZO
436 case 'y':
437 compressed = 2;
438 zipflag = COMPRESS_LZO;
439 break;
440 #endif /* HAVE_LZO */
441
442 #ifdef HAVE_ZLIB
443 case 'z':
444 compressed = 2;
445 zipflag = COMPRESS_ZLIB;
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 if (Apath && (Afile = open(Apath, O_WRONLY|O_CREAT|O_TRUNC,
519 S_IRUSR | S_IWUSR | S_IRGRP |
520 S_IWGRP | S_IROTH | S_IWOTH)) < 0) {
521 msg("Cannot open %s for writing: %s\n",
522 optarg, strerror(errno));
523 msg("The ENTIRE dump is aborted.\n");
524 exit(X_STARTUP);
525 }
526
527 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
528 signal(SIGHUP, sig);
529 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
530 signal(SIGTRAP, sig);
531 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
532 signal(SIGFPE, sig);
533 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
534 signal(SIGBUS, sig);
535 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
536 signal(SIGSEGV, sig);
537 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
538 signal(SIGTERM, sig);
539 if (signal(SIGINT, interrupt) == SIG_IGN)
540 signal(SIGINT, SIG_IGN);
541 #ifdef SIGXCPU
542 signal(SIGXCPU, SIG_IGN);
543 #endif /* SIGXCPU */
544 #ifdef SIGXFSZ
545 signal(SIGXFSZ, SIG_IGN);
546 #endif /* SIGXFSZ */
547
548 set_operators(); /* /etc/group snarfed */
549 getfstab(); /* /etc/fstab snarfed */
550
551 /*
552 * disk may end in / and this can confuse
553 * fstabsearch.
554 */
555 i = strlen(diskparam) - 1;
556 if (i > 1 && diskparam[i] == '/')
557 if (!(i == 6 && !strcmp(diskparam, "LABEL=/")))
558 diskparam[i] = '\0';
559
560 disk = get_device_name(diskparam);
561 if (!disk) { /* null means the disk is some form
562 of LABEL= or UID= but it was not
563 found */
564 msg("Cannot find a disk having %s\n", diskparam);
565 msg("The ENTIRE dump is aborted.\n");
566 exit(X_STARTUP);
567 }
568 /*
569 * disk can be either the full special file name,
570 * the suffix of the special file name,
571 * the special name missing the leading '/',
572 * the file system name with or without the leading '/'.
573 */
574 if ((dt = fstabsearch(disk)) != NULL) {
575 /* if found then only one parameter (i.e. partition)
576 * is allowed */
577 if (argc >= 1) {
578 (void)fprintf(stderr, "Unknown arguments to dump:");
579 while (argc--)
580 (void)fprintf(stderr, " %s", *argv++);
581 (void)fprintf(stderr, "\n");
582 msg("The ENTIRE dump is aborted.\n");
583 exit(X_STARTUP);
584 }
585 disk = rawname(dt->mnt_fsname);
586 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
587 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
588 } else {
589 #ifdef __linux__
590 #ifdef HAVE_REALPATH
591 if (realpath(disk, pathname) == NULL)
592 #endif
593 strcpy(pathname, disk);
594 /*
595 * The argument could be now a mountpoint of
596 * a filesystem specified in fstab. Search for it.
597 */
598 if ((dt = fstabsearch(pathname)) != NULL) {
599 disk = rawname(dt->mnt_fsname);
600 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
601 (void)strncpy(spcl.c_filesys, dt->mnt_dir, NAMELEN);
602 } else {
603 /*
604 * The argument was not found in the fstab
605 * assume that this is a subtree and search for it
606 */
607 dt = fstabsearchdir(pathname, directory);
608 if (dt != NULL) {
609 char name[MAXPATHLEN];
610 (void)strncpy(spcl.c_dev, dt->mnt_fsname, NAMELEN);
611 (void)snprintf(name, sizeof(name), "%s (dir %s)",
612 dt->mnt_dir, directory);
613 (void)strncpy(spcl.c_filesys, name, NAMELEN);
614 disk = rawname(dt->mnt_fsname);
615 } else {
616 (void)strncpy(spcl.c_dev, disk, NAMELEN);
617 (void)strncpy(spcl.c_filesys, "an unlisted file system",
618 NAMELEN);
619 }
620 }
621 #else
622 (void)strncpy(spcl.c_dev, disk, NAMELEN);
623 (void)strncpy(spcl.c_filesys, "an unlisted file system",
624 NAMELEN);
625 #endif
626 }
627
628 if (directory[0] != 0) {
629 if (level != '0') {
630 msg("Only level 0 dumps are allowed on a subdirectory\n");
631 msg("The ENTIRE dump is aborted.\n");
632 exit(X_STARTUP);
633 }
634 if (uflag) {
635 msg("You can't update the dumpdates file when dumping a subdirectory\n");
636 msg("The ENTIRE dump is aborted.\n");
637 exit(X_STARTUP);
638 }
639 }
640 spcl.c_dev[NAMELEN-1] = '\0';
641 spcl.c_filesys[NAMELEN-1] = '\0';
642 (void)gethostname(spcl.c_host, NAMELEN);
643 spcl.c_host[NAMELEN-1] = '\0';
644 spcl.c_level = level - '0';
645 spcl.c_type = TS_TAPE;
646 if (!Tflag)
647 getdumptime(uflag); /* dumpdates snarfed */
648
649 if (spcl.c_ddate == 0 && spcl.c_level) {
650 msg("WARNING: There is no inferior level dump on this filesystem\n");
651 msg("WARNING: Assuming a level 0 dump by default\n");
652 level = '0';
653 spcl.c_level = 0;
654 }
655
656 if (Mflag)
657 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1);
658 else
659 strncpy(tape, tapeprefix, MAXPATHLEN);
660 tape[MAXPATHLEN - 1] = '\0';
661
662 if (!pipeout) {
663 if (STAT(tape, &statbuf) != -1)
664 fifoout= statbuf.st_mode & S_IFIFO;
665 }
666
667 if (!sizest) {
668
669 msg("Date of this level %c dump: %s", level,
670 ctime4(&spcl.c_date));
671 #ifdef USE_QFA
672 gThisDumpDate = spcl.c_date;
673 #endif
674 if (spcl.c_ddate)
675 msg("Date of last level %c dump: %s", lastlevel,
676 ctime4(&spcl.c_ddate));
677 msg("Dumping %s (%s) ", disk, spcl.c_filesys);
678 if (host)
679 msgtail("to %s on host %s\n", tape, host);
680 else
681 msgtail("to %s\n", tape);
682 } /* end of size estimate */
683
684 #ifdef __linux__
685 if ((diskfd = OPEN(disk, O_RDONLY)) < 0) {
686 msg("Cannot open %s\n", disk);
687 msg("The ENTIRE dump is aborted.\n");
688 exit(X_STARTUP);
689 }
690 #ifdef BLKFLSBUF
691 (void)ioctl(diskfd, BLKFLSBUF);
692 #endif
693 retval = dump_fs_open(disk, &fs);
694 if (retval) {
695 com_err(disk, retval, "while opening filesystem");
696 if (retval == EXT2_ET_REV_TOO_HIGH)
697 msg("Get a newer version of dump!\n");
698 msg("The ENTIRE dump is aborted.\n");
699 exit(X_STARTUP);
700 }
701 if (fs->super->s_rev_level > DUMP_CURRENT_REV) {
702 com_err(disk, retval, "while opening filesystem");
703 msg("Get a newer version of dump!\n");
704 msg("The ENTIRE dump is aborted.\n");
705 exit(X_STARTUP);
706 }
707 /* if no user label specified, use ext2 filesystem label if available */
708 if (spcl.c_label[0] == '\0') {
709 const char *lbl;
710 if ( (lbl = get_device_label(disk)) != NULL) {
711 strncpy(spcl.c_label, lbl, LBLSIZE);
712 spcl.c_label[LBLSIZE-1] = '\0';
713 }
714 else
715 strcpy(spcl.c_label, "none"); /* safe strcpy. */
716 }
717 sync();
718 dev_bsize = DEV_BSIZE;
719 dev_bshift = ffs(dev_bsize) - 1;
720 if (dev_bsize != (1 << dev_bshift))
721 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
722 tp_bshift = ffs(TP_BSIZE) - 1;
723 if (TP_BSIZE != (1 << tp_bshift))
724 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
725 maxino = fs->super->s_inodes_count + 1;
726 spcl.c_flags |= DR_NEWINODEFMT;
727 #else /* __linux __*/
728 if ((diskfd = open(disk, O_RDONLY)) < 0) {
729 msg("Cannot open %s\n", disk);
730 msg("The ENTIRE dump is aborted.\n");
731 exit(X_STARTUP);
732 }
733 sync();
734 sblock = (struct fs *)sblock_buf;
735 bread(SBOFF, (char *) sblock, SBSIZE);
736 if (sblock->fs_magic != FS_MAGIC)
737 quit("bad sblock magic number\n");
738 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
739 dev_bshift = ffs(dev_bsize) - 1;
740 if (dev_bsize != (1 << dev_bshift))
741 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
742 tp_bshift = ffs(TP_BSIZE) - 1;
743 if (TP_BSIZE != (1 << tp_bshift))
744 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
745 #ifdef FS_44INODEFMT
746 if (sblock->fs_inodefmt >= FS_44INODEFMT)
747 spcl.c_flags |= DR_NEWINODEFMT;
748 #endif
749 maxino = sblock->fs_ipg * sblock->fs_ncg;
750 #endif /* __linux__ */
751 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
752 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
753 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
754 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
755 metainomap = (char *)calloc((unsigned) mapsize, sizeof(char));
756 if (usedinomap == NULL || dumpdirmap == NULL ||
757 dumpinomap == NULL || metainomap == NULL)
758 quit("out of memory allocating inode maps\n");
759 tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
760
761 nonodump = spcl.c_level < honorlevel;
762
763 if (!sizest) {
764 msg("Label: %s\n", spcl.c_label);
765
766 msg("Writing %d Kilobyte records\n", ntrec);
767
768 if (compressed) {
769 if (zipflag == COMPRESS_LZO)
770 msg("Compressing output (lzo)\n");
771 else
772 msg("Compressing output at compression level %d (%s)\n",
773 compressed, zipflag == COMPRESS_ZLIB ? "zlib" : "bzlib");
774 }
775 }
776
777 #if defined(SIGINFO)
778 (void)signal(SIGINFO, statussig);
779 #endif
780
781 if (!sizest)
782 msg("mapping (Pass I) [regular files]\n");
783 #ifdef __linux__
784 if (directory[0] == 0)
785 anydirskipped = mapfiles(maxino, &tapesize);
786 else {
787 if (LSTAT(pathname, &statbuf) == -1) {
788 msg("File cannot be accessed (%s).\n", pathname);
789 msg("The ENTIRE dump is aborted.\n");
790 exit(X_STARTUP);
791 }
792 filedev = statbuf.st_dev;
793 if (!(statbuf.st_mode & S_IFDIR)) /* is a file */
794 anydirskipped = maponefile(maxino, &tapesize,
795 directory);
796 else
797 anydirskipped = mapfilesfromdir(maxino, &tapesize,
798 directory);
799 }
800 while (argc--) {
801 int anydirskipped2;
802 char *p = *argv;
803 /* check if file is available */
804 if (LSTAT(p, &statbuf) == -1) {
805 msg("File cannot be accessed (%s).\n", p);
806 msg("The ENTIRE dump is aborted.\n");
807 exit(X_STARTUP);
808 }
809 /* check if file is on same unix partiton as the first
810 * argument */
811 if (statbuf.st_dev != filedev) {
812 msg("Files are not on same file system (%s).\n", p);
813 msg("The ENTIRE dump is aborted.\n");
814 exit(X_STARTUP);
815 }
816 /* check if file is a directory */
817 if (!(statbuf.st_mode & S_IFDIR))
818 anydirskipped2 = maponefile(maxino, &tapesize,
819 p+strlen(dt->mnt_dir));
820 else
821 /* read directory inodes.
822 * NOTE: nested directories are not recognized
823 * so inodes may be umped twice!
824 */
825 anydirskipped2 = mapfilesfromdir(maxino, &tapesize,
826 p+strlen(dt->mnt_dir));
827 if (!anydirskipped)
828 anydirskipped = anydirskipped2;
829 argv++;
830 }
831 #else
832 anydirskipped = mapfiles(maxino, &tapesize);
833 #endif
834
835 if (!sizest)
836 msg("mapping (Pass II) [directories]\n");
837 while (anydirskipped) {
838 anydirskipped = mapdirs(maxino, &tapesize);
839 }
840
841 if (sizest) {
842 printf("%.0f\n", ((double)tapesize + 1 + ntrec) * TP_BSIZE);
843 exit(X_FINOK);
844 } /* stop here for size estimate */
845
846 if (pipeout || unlimited) {
847 tapesize += 1 + ntrec; /* 1 map header + trailer blocks */
848 msg("estimated %ld blocks.\n", tapesize);
849 } else {
850 double fetapes;
851
852 if (blocksperfile)
853 fetapes = (double) tapesize / blocksperfile;
854 else if (cartridge) {
855 /* Estimate number of tapes, assuming streaming stops at
856 the end of each block written, and not in mid-block.
857 Assume no erroneous blocks; this can be compensated
858 for with an artificially low tape size. */
859 fetapes =
860 ( (double) tapesize /* blocks */
861 * TP_BSIZE /* bytes/block */
862 * (1.0/density) /* 0.1" / byte " */
863 +
864 (double) tapesize /* blocks */
865 * (1.0/ntrec) /* streaming-stops per block */
866 * 15.48 /* 0.1" / streaming-stop " */
867 ) * (1.0 / tsize ); /* tape / 0.1" " */
868 } else {
869 /* Estimate number of tapes, for old fashioned 9-track
870 tape */
871 int tenthsperirg = (density == 625) ? 3 : 7;
872 fetapes =
873 ( (double) tapesize /* blocks */
874 * TP_BSIZE /* bytes / block */
875 * (1.0/density) /* 0.1" / byte " */
876 +
877 (double) tapesize /* blocks */
878 * (1.0/ntrec) /* IRG's / block */
879 * tenthsperirg /* 0.1" / IRG " */
880 ) * (1.0 / tsize ); /* tape / 0.1" " */
881 }
882 etapes = fetapes; /* truncating assignment */
883 etapes++;
884 /* count the dumped inodes map on each additional tape */
885 tapesize += (etapes - 1) *
886 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
887 tapesize += etapes + ntrec; /* headers + trailer blks */
888 msg("estimated %ld blocks on %3.2f tape(s).\n",
889 tapesize, fetapes);
890 }
891
892 #ifdef USE_QFA
893 if (tapepos) {
894 msg("writing QFA positions to %s\n", gTapeposfile);
895 if ((gTapeposfd = open(gTapeposfile,
896 O_WRONLY|O_CREAT|O_TRUNC,
897 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
898 | S_IROTH | S_IWOTH)) < 0)
899 quit("can't open tapeposfile\n");
900 /* print QFA-file header */
901 snprintf(gTps, sizeof(gTps), "%s\n%s\n%ld\n\n", QFA_MAGIC, QFA_VERSION, (unsigned long)spcl.c_date);
902 gTps[sizeof(gTps) - 1] = '\0';
903 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
904 quit("can't write tapeposfile\n");
905 sprintf(gTps, "ino\ttapeno\ttapepos\n");
906 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps))
907 quit("can't write tapeposfile\n");
908 }
909 #endif /* USE_QFA */
910
911 /*
912 * Allocate tape buffer.
913 */
914 if (!alloctape())
915 quit(
916 "can't allocate tape buffers - try a smaller blocking factor.\n");
917
918 startnewtape(1);
919 tstart_writing = time(NULL);
920 dumpmap(usedinomap, TS_CLRI, maxino - 1);
921
922 msg("dumping (Pass III) [directories]\n");
923 dirty = 0; /* XXX just to get gcc to shut up */
924 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
925 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
926 dirty = *map++;
927 else
928 dirty >>= 1;
929 if ((dirty & 1) == 0)
930 continue;
931 /*
932 * Skip directory inodes deleted and maybe reallocated
933 */
934 dp = getino(ino);
935 if ((dp->di_mode & IFMT) != IFDIR)
936 continue;
937 #ifdef __linux__
938 /*
939 * Skip directory inodes deleted and not yes reallocated...
940 */
941 if (dp->di_nlink == 0 || dp->di_dtime != 0)
942 continue;
943 if (vflag)
944 msg("dumping directory inode %lu\n", ino);
945 (void)dumpdirino(dp, ino);
946 #else
947 (void)dumpino(dp, ino);
948 #endif
949 }
950
951 msg("dumping (Pass IV) [regular files]\n");
952 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
953 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
954 dirty = *map++;
955 else
956 dirty >>= 1;
957 if ((dirty & 1) == 0)
958 continue;
959 /*
960 * Skip inodes deleted and reallocated as directories.
961 */
962 dp = getino(ino);
963 if ((dp->di_mode & IFMT) == IFDIR)
964 continue;
965 #ifdef __linux__
966 /*
967 * No need to check here for deleted and not yet reallocated
968 * inodes since this is done in dumpino().
969 */
970 #endif
971 if (vflag) {
972 if (mflag && TSTINO(ino, metainomap))
973 msg("dumping regular inode %lu (meta only)\n", ino);
974 else
975 msg("dumping regular inode %lu\n", ino);
976 }
977 (void)dumpino(dp, ino, mflag && TSTINO(ino, metainomap));
978 }
979
980 tend_writing = time(NULL);
981 spcl.c_type = TS_END;
982
983 if (Afile >= 0) {
984 volinfo[1] = ROOTINO;
985 memcpy(spcl.c_inos, volinfo, TP_NINOS * sizeof(dump_ino_t));
986 spcl.c_flags |= DR_INODEINFO;
987 }
988
989 /*
990 * Finish off the current tape record with trailer blocks, to ensure
991 * at least the data in the last partial record makes it to tape.
992 * Also make sure we write at least 1 trailer block.
993 */
994 for (i = ntrec - (spcl.c_tapea % ntrec); i; --i)
995 writeheader(maxino - 1);
996
997 tnow = trewind();
998
999 if (pipeout || fifoout)
1000 msg("%ld blocks (%.2fMB)\n", spcl.c_tapea,
1001 ((double)spcl.c_tapea * TP_BSIZE / 1048576));
1002 else
1003 msg("%ld blocks (%.2fMB) on %d volume(s)\n",
1004 spcl.c_tapea,
1005 ((double)spcl.c_tapea * TP_BSIZE / 1048576),
1006 spcl.c_volume);
1007
1008 /* report dump performance, avoid division by zero */
1009 if (tend_writing - tstart_writing == 0)
1010 msg("finished in less than a second\n");
1011 else
1012 msg("finished in %d seconds, throughput %d kBytes/sec\n",
1013 tend_writing - tstart_writing,
1014 spcl.c_tapea / (tend_writing - tstart_writing));
1015
1016 putdumptime();
1017 msg("Date of this level %c dump: %s", level,
1018 spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date));
1019 msg("Date this dump completed: %s", ctime(&tnow));
1020
1021 msg("Average transfer rate: %ld kB/s\n", xferrate / tapeno);
1022 if (compressed) {
1023 long tapekb = bytes_written / 1024;
1024 double rate = .0005 + (double) spcl.c_tapea / tapekb;
1025 msg("Wrote %ldkB uncompressed, %ldkB compressed, %1.3f:1\n",
1026 spcl.c_tapea, tapekb, rate);
1027 }
1028
1029 if (Afile >= 0)
1030 msg("Archiving dump to %s\n", Apath);
1031
1032 broadcast("DUMP IS DONE!\7\7\n");
1033 msg("DUMP IS DONE\n");
1034 Exit(X_FINOK);
1035 /* NOTREACHED */
1036 return 0; /* gcc - shut up */
1037 }
1038
1039 static void
1040 usage(void)
1041 {
1042 char white[MAXPATHLEN];
1043 const char *ext2ver, *ext2date;
1044
1045 memset(white, ' ', MAXPATHLEN);
1046 white[MIN(strlen(__progname), MAXPATHLEN - 1)] = '\0';
1047
1048 #ifdef __linux__
1049 ext2fs_get_library_version(&ext2ver, &ext2date);
1050 fprintf(stderr, "%s %s (using libext2fs %s of %s)\n",
1051 __progname, _DUMP_VERSION, ext2ver, ext2date);
1052 #else
1053 fprintf(stderr, "%s %s\n", __progname, _DUMP_VERSION);
1054 #endif
1055 fprintf(stderr,
1056 "usage:\t%s [-0123456789ac"
1057 #ifdef KERBEROS
1058 "k"
1059 #endif
1060 "mMnqSuv"
1061 "] [-A file] [-B records] [-b blocksize]\n"
1062 "\t%s [-d density] [-D file] [-e inode#,inode#,...] [-E file]\n"
1063 "\t%s [-f file] [-h level] [-I nr errors] "
1064 #ifdef HAVE_BZLIB
1065 "[-j zlevel] "
1066 #endif
1067 #ifdef USE_QFA
1068 "[-Q file]\n"
1069 #endif
1070 "\t%s [-s feet] "
1071 "[-T date] "
1072 #ifdef HAVE_LZO
1073 "[-y] "
1074 #endif
1075 #ifdef HAVE_ZLIB
1076 "[-z zlevel] "
1077 #endif
1078 "filesystem\n"
1079 "\t%s [-W | -w]\n",
1080 __progname, white, white, white, __progname);
1081 exit(X_STARTUP);
1082 }
1083
1084 /*
1085 * Pick up a numeric argument. It must be nonnegative and in the given
1086 * range (except that a vmax of 0 means unlimited).
1087 */
1088 static long
1089 numarg(const char *meaning, long vmin, long vmax)
1090 {
1091 char *p;
1092 long val;
1093
1094 val = strtol(optarg, &p, 10);
1095 if (*p)
1096 errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
1097 if (val < vmin || (vmax && val > vmax))
1098 errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax);
1099 return (val);
1100 }
1101
1102 void
1103 sig(int signo)
1104 {
1105 switch(signo) {
1106 case SIGALRM:
1107 case SIGBUS:
1108 case SIGFPE:
1109 case SIGHUP:
1110 case SIGTERM:
1111 case SIGTRAP:
1112 if (pipeout || fifoout)
1113 quit("Signal on pipe: cannot recover\n");
1114 msg("Rewriting attempted as response to unknown signal: %d.\n", signo);
1115 (void)fflush(stderr);
1116 (void)fflush(stdout);
1117 close_rewind();
1118 exit(X_REWRITE);
1119 /* NOTREACHED */
1120 case SIGSEGV:
1121 msg("SIGSEGV: ABORTING!\n");
1122 (void)signal(SIGSEGV, SIG_DFL);
1123 (void)kill(0, SIGSEGV);
1124 /* NOTREACHED */
1125 }
1126 }
1127
1128 const char *
1129 rawname(const char *cp)
1130 {
1131 #ifdef __linux__
1132 return cp;
1133 #else /* __linux__ */
1134 static char rawbuf[MAXPATHLEN];
1135 char *dp = strrchr(cp, '/');
1136
1137 if (dp == NULL)
1138 return (NULL);
1139 (void)strncpy(rawbuf, cp, min(dp-cp, MAXPATHLEN - 1));
1140 rawbuf[min(dp-cp, MAXPATHLEN-1)] = '\0';
1141 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf));
1142 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf));
1143 return (rawbuf);
1144 #endif /* __linux__ */
1145 }
1146
1147 /*
1148 * obsolete --
1149 * Change set of key letters and ordered arguments into something
1150 * getopt(3) will like.
1151 */
1152 static void
1153 obsolete(int *argcp, char **argvp[])
1154 {
1155 int argc, flags;
1156 char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL;
1157
1158 /* Setup. */
1159 argv = *argvp;
1160 argc = *argcp;
1161
1162 /* Return if no arguments or first argument has leading dash. */
1163 ap = argv[1];
1164 if (argc == 1 || *ap == '-')
1165 return;
1166
1167 /* Allocate space for new arguments. */
1168 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
1169 (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
1170 err(X_STARTUP, "malloc new args");
1171
1172 *nargv++ = *argv;
1173 argv += 2;
1174
1175 for (flags = 0; *ap; ++ap) {
1176 switch (*ap) {
1177 case 'A':
1178 case 'B':
1179 case 'b':
1180 case 'd':
1181 case 'D':
1182 case 'e':
1183 case 'E':
1184 case 'f':
1185 case 'F':
1186 case 'h':
1187 case 'L':
1188 case 'Q':
1189 case 's':
1190 case 'T':
1191 if (*argv == NULL) {
1192 warnx("option requires an argument -- %c", *ap);
1193 usage();
1194 }
1195 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
1196 err(X_STARTUP, "malloc arg");
1197 nargv[0][0] = '-';
1198 nargv[0][1] = *ap;
1199 (void)strcpy(&nargv[0][2], *argv);
1200 ++argv;
1201 ++nargv;
1202 break;
1203 default:
1204 if (!flags) {
1205 *p++ = '-';
1206 flags = 1;
1207 }
1208 *p++ = *ap;
1209 break;
1210 }
1211 }
1212
1213 /* Terminate flags. */
1214 if (flags) {
1215 *p = '\0';
1216 *nargv++ = flagsp;
1217 }
1218
1219 /* Copy remaining arguments. */
1220 while ((*nargv++ = *argv++));
1221
1222 /* Update argument count. */
1223 *argcp = nargv - *argvp - 1;
1224 }
1225
1226 /*
1227 * This tests whether an inode is in the exclude list
1228 */
1229 int
1230 exclude_ino(dump_ino_t ino)
1231 {
1232 /* 04-Feb-00 ILC */
1233 if (iexclude_num) { /* if there are inodes in the exclude list */
1234 int idx; /* then check this inode against it */
1235 for (idx = 0; idx < iexclude_num; idx++)
1236 if (ino == iexclude_list[idx])
1237 return 1;
1238 }
1239 return 0;
1240 }
1241
1242 /*
1243 * This tests adds an inode to the exclusion list if it isn't already there
1244 */
1245 void
1246 do_exclude_ino(dump_ino_t ino, const char *reason)
1247 {
1248 if (!exclude_ino(ino)) {
1249 if (iexclude_num == IEXCLUDE_MAXNUM) {
1250 msg("Too many exclude options\n");
1251 msg("The ENTIRE dump is aborted.\n");
1252 exit(X_STARTUP);
1253 }
1254 if (reason)
1255 msg("Excluding inode %u (%s) from dump\n",
1256 ino, reason);
1257 else
1258 msg("Excluding inode %u from dump\n", ino);
1259 iexclude_list[iexclude_num++] = ino;
1260 }
1261 }
1262
1263 static void
1264 do_exclude_ino_str(char * ino) {
1265 char *r;
1266 unsigned long inod;
1267
1268 inod = strtoul(ino, &r, 10);
1269 if (( *r != '\0' && !isspace(*r) ) || inod <= ROOTINO) {
1270 msg("Invalid inode argument %s\n", ino);
1271 msg("The ENTIRE dump is aborted.\n");
1272 exit(X_STARTUP);
1273 }
1274 do_exclude_ino(inod, NULL);
1275 }
1276
1277 /*
1278 * This reads a file containing one inode number per line and exclude them all
1279 */
1280 static void
1281 do_exclude_from_file(char *file) {
1282 FILE *f;
1283 char *p, fname[MAXPATHLEN];
1284
1285
1286 if (!( f = fopen(file, "r")) ) {
1287 msg("Cannot open file for reading: %s\n", file);
1288 msg("The ENTIRE dump is aborted.\n");
1289 exit(X_STARTUP);
1290 }
1291 while (( p = fgets(fname, MAXPATHLEN, f))) {
1292 if ( *p && *(p + strlen(p) - 1) == '\n' ) /* possible null string */
1293 *(p + strlen(p) - 1) = '\0';
1294 if ( !*p ) /* skip empty lines */
1295 continue;
1296 do_exclude_ino_str(p);
1297 }
1298 fclose(f);
1299 }
1300
1301 static void incompat_flags(int cond, char flag1, char flag2) {
1302 if (cond) {
1303 msg("You cannot use the %c and %c flags together.\n",
1304 flag1, flag2);
1305 msg("The ENTIRE dump is aborted.\n");
1306 exit(X_STARTUP);
1307 }
1308 }