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