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