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