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