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