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