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