]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
ebcbe7f6 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999-2000 |
1227625a SP |
6 | */ |
7 | ||
8 | /*- | |
9 | * Copyright (c) 1980, 1991, 1993, 1994 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. All advertising materials mentioning features or use of this software | |
21 | * must display the following acknowledgement: | |
22 | * This product includes software developed by the University of | |
23 | * California, Berkeley and its contributors. | |
24 | * 4. Neither the name of the University nor the names of its contributors | |
25 | * may be used to endorse or promote products derived from this software | |
26 | * without specific prior written permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
38 | * SUCH DAMAGE. | |
39 | */ | |
40 | ||
df9ae507 SP |
41 | #ifndef lint |
42 | static const char rcsid[] = | |
8954518f | 43 | "$Id: main.c,v 1.24 2000/08/20 19:41:50 stelian Exp $"; |
df9ae507 SP |
44 | #endif /* not lint */ |
45 | ||
1227625a SP |
46 | #include <sys/param.h> |
47 | #include <sys/time.h> | |
48 | #ifdef __linux__ | |
49 | #include <linux/ext2_fs.h> | |
50 | #include <sys/stat.h> | |
51 | #include <bsdcompat.h> | |
52 | #else | |
53 | #ifdef sunos | |
54 | #include <sys/vnode.h> | |
55 | ||
56 | #include <ufs/inode.h> | |
57 | #include <ufs/fs.h> | |
58 | #else | |
59 | #include <ufs/ufs/dinode.h> | |
60 | #include <ufs/ffs/fs.h> | |
61 | #endif | |
62 | #endif | |
63 | ||
64 | #include <protocols/dumprestore.h> | |
65 | ||
66 | #include <ctype.h> | |
ddd2ef55 | 67 | #include <compaterr.h> |
1227625a SP |
68 | #include <fcntl.h> |
69 | #include <fstab.h> | |
70 | #include <signal.h> | |
71 | #include <stdio.h> | |
72 | #include <stdlib.h> | |
73 | #include <string.h> | |
74 | #include <unistd.h> | |
75 | ||
76 | #ifdef __linux__ | |
77 | #include <ext2fs/ext2fs.h> | |
78 | #endif | |
79 | ||
80 | #include "dump.h" | |
81 | #include "pathnames.h" | |
8954518f | 82 | #include "bylabel.h" |
1227625a SP |
83 | |
84 | #ifndef SBOFF | |
85 | #define SBOFF (SBLOCK * DEV_BSIZE) | |
86 | #endif | |
87 | ||
88 | int notify = 0; /* notify operator flag */ | |
89 | int blockswritten = 0; /* number of blocks written on current tape */ | |
90 | int tapeno = 0; /* current tape number */ | |
b45f51d6 | 91 | int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ |
1227625a SP |
92 | int ntrec = NTREC; /* # tape blocks in each tape record */ |
93 | int cartridge = 0; /* Assume non-cartridge tape */ | |
b45f51d6 | 94 | int dokerberos = 0; /* Use Kerberos authentication */ |
1227625a SP |
95 | long dev_bsize = 1; /* recalculated below */ |
96 | long blocksperfile; /* output blocks per file */ | |
97 | char *host = NULL; /* remote host (if any) */ | |
144a6db1 | 98 | int sizest = 0; /* return size estimate only */ |
1227625a SP |
99 | |
100 | #ifdef __linux__ | |
101 | char *__progname; | |
102 | #endif | |
103 | ||
ddd2ef55 SP |
104 | int maxbsize = 64*1024; /* XXX MAXBSIZE from sys/param.h */ |
105 | static long numarg __P((const char *, long, long)); | |
1227625a SP |
106 | static void obsolete __P((int *, char **[])); |
107 | static void usage __P((void)); | |
108 | ||
20c345aa SP |
109 | ino_t iexclude_list[IEXCLUDE_MAXNUM]; /* the inode exclude list */ |
110 | int iexclude_num = 0; /* number of elements in the list */ | |
111 | ||
1227625a | 112 | int |
ddd2ef55 | 113 | main(int argc, char *argv[]) |
1227625a SP |
114 | { |
115 | register ino_t ino; | |
b45f51d6 | 116 | register int dirty; |
1227625a SP |
117 | register struct dinode *dp; |
118 | register struct fstab *dt; | |
119 | register char *map; | |
120 | register int ch; | |
121 | int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; | |
122 | ino_t maxino; | |
123 | #ifdef __linux__ | |
124 | errcode_t retval; | |
92a9bf12 SP |
125 | char directory[MAXPATHLEN]; |
126 | char pathname[MAXPATHLEN]; | |
1227625a | 127 | #endif |
ddd2ef55 | 128 | time_t tnow; |
b45f51d6 | 129 | char labelstr[LBLSIZE]; |
8954518f | 130 | char *diskparam; |
1227625a SP |
131 | |
132 | spcl.c_date = 0; | |
133 | #ifdef __linux__ | |
134 | (void)time4(&spcl.c_date); | |
135 | #else | |
136 | (void)time((time_t *)&spcl.c_date); | |
137 | #endif | |
138 | ||
139 | #ifdef __linux__ | |
140 | __progname = argv[0]; | |
141 | directory[0] = 0; | |
142 | initialize_ext2_error_table(); | |
143 | #endif | |
144 | ||
145 | tsize = 0; /* Default later, based on 'c' option for cart tapes */ | |
0d7af9c5 | 146 | eot_script = NULL; |
d1c73b9a SP |
147 | if ((tapeprefix = getenv("TAPE")) == NULL) |
148 | tapeprefix = _PATH_DEFTAPE; | |
1227625a | 149 | dumpdates = _PATH_DUMPDATES; |
b45f51d6 | 150 | strcpy(labelstr, "none"); /* XXX safe strcpy. */ |
1227625a SP |
151 | if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) |
152 | quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); | |
153 | level = '0'; | |
b45f51d6 | 154 | |
1227625a SP |
155 | if (argc < 2) |
156 | usage(); | |
157 | ||
158 | obsolete(&argc, &argv); | |
b45f51d6 | 159 | #ifdef KERBEROS |
0d7af9c5 | 160 | #define optstring "0123456789aB:b:cd:e:f:F:h:kL:Mns:ST:uWw" |
b45f51d6 | 161 | #else |
0d7af9c5 | 162 | #define optstring "0123456789aB:b:cd:e:f:F:h:L:Mns:ST:uWw" |
b45f51d6 SP |
163 | #endif |
164 | while ((ch = getopt(argc, argv, optstring)) != -1) | |
165 | #undef optstring | |
166 | switch (ch) { | |
1227625a SP |
167 | /* dump level */ |
168 | case '0': case '1': case '2': case '3': case '4': | |
169 | case '5': case '6': case '7': case '8': case '9': | |
170 | level = ch; | |
171 | break; | |
172 | ||
b45f51d6 SP |
173 | case 'a': /* `auto-size', Write to EOM. */ |
174 | unlimited = 1; | |
175 | break; | |
176 | ||
1227625a | 177 | case 'B': /* blocks per output file */ |
b45f51d6 SP |
178 | blocksperfile = numarg("number of blocks per file", |
179 | 1L, 0L); | |
1227625a SP |
180 | break; |
181 | ||
182 | case 'b': /* blocks per tape write */ | |
b45f51d6 SP |
183 | ntrec = numarg("number of blocks per write", |
184 | 1L, 1000L); | |
ddd2ef55 SP |
185 | if (ntrec > maxbsize/1024) { |
186 | msg("Please choose a blocksize <= %dKB\n", | |
187 | maxbsize/1024); | |
188 | exit(X_STARTUP); | |
189 | } | |
190 | bflag = 1; | |
1227625a SP |
191 | break; |
192 | ||
193 | case 'c': /* Tape is cart. not 9-track */ | |
194 | cartridge = 1; | |
195 | break; | |
196 | ||
197 | case 'd': /* density, in bits per inch */ | |
198 | density = numarg("density", 10L, 327670L) / 10; | |
199 | if (density >= 625 && !bflag) | |
200 | ntrec = HIGHDENSITYTREC; | |
201 | break; | |
20c345aa SP |
202 | |
203 | /* 04-Feb-00 ILC */ | |
204 | case 'e': /* exclude an inode */ | |
205 | iexclude_list[iexclude_num++] = | |
206 | numarg("inode to exclude",0L,0L); | |
207 | msg("Added %d to exclude list\n", | |
208 | iexclude_list[iexclude_num-1]); | |
209 | break; | |
1227625a SP |
210 | |
211 | case 'f': /* output file */ | |
d1c73b9a | 212 | tapeprefix = optarg; |
1227625a SP |
213 | break; |
214 | ||
0d7af9c5 SP |
215 | case 'F': /* end of tape script */ |
216 | eot_script = optarg; | |
217 | break; | |
218 | ||
1227625a SP |
219 | case 'h': |
220 | honorlevel = numarg("honor level", 0L, 10L); | |
221 | break; | |
222 | ||
b45f51d6 SP |
223 | #ifdef KERBEROS |
224 | case 'k': | |
225 | dokerberos = 1; | |
226 | break; | |
227 | #endif | |
228 | ||
229 | case 'L': | |
230 | /* | |
231 | * Note that although there are LBLSIZE characters, | |
232 | * the last must be '\0', so the limit on strlen() | |
233 | * is really LBLSIZE-1. | |
234 | */ | |
235 | strncpy(labelstr, optarg, LBLSIZE); | |
236 | labelstr[LBLSIZE-1] = '\0'; | |
237 | if (strlen(optarg) > LBLSIZE-1) { | |
238 | msg( | |
239 | "WARNING Label `%s' is larger than limit of %d characters.\n", | |
240 | optarg, LBLSIZE-1); | |
241 | msg("WARNING: Using truncated label `%s'.\n", | |
242 | labelstr); | |
243 | } | |
244 | break; | |
245 | ||
d1c73b9a SP |
246 | case 'M': /* multi-volume flag */ |
247 | Mflag = 1; | |
248 | break; | |
249 | ||
1227625a SP |
250 | case 'n': /* notify operators */ |
251 | notify = 1; | |
252 | break; | |
253 | ||
254 | case 's': /* tape size, feet */ | |
255 | tsize = numarg("tape size", 1L, 0L) * 12 * 10; | |
256 | break; | |
257 | ||
144a6db1 SP |
258 | case 'S': |
259 | sizest = 1; /* return size estimate only */ | |
260 | break; | |
261 | ||
1227625a SP |
262 | case 'T': /* time of last dump */ |
263 | spcl.c_ddate = unctime(optarg); | |
264 | if (spcl.c_ddate < 0) { | |
265 | (void)fprintf(stderr, "bad time \"%s\"\n", | |
266 | optarg); | |
b45f51d6 | 267 | exit(X_STARTUP); |
1227625a SP |
268 | } |
269 | Tflag = 1; | |
270 | lastlevel = '?'; | |
b45f51d6 | 271 | break; |
1227625a | 272 | |
ddd2ef55 | 273 | case 'u': /* update dumpdates */ |
1227625a SP |
274 | uflag = 1; |
275 | break; | |
276 | ||
277 | case 'W': /* what to do */ | |
278 | case 'w': | |
279 | lastdump(ch); | |
b45f51d6 | 280 | exit(X_FINOK); /* do nothing else */ |
1227625a SP |
281 | |
282 | default: | |
283 | usage(); | |
284 | } | |
285 | argc -= optind; | |
286 | argv += optind; | |
287 | ||
288 | if (argc < 1) { | |
289 | (void)fprintf(stderr, "Must specify disk or filesystem\n"); | |
b45f51d6 | 290 | exit(X_STARTUP); |
1227625a | 291 | } |
8954518f SP |
292 | diskparam = *argv++; |
293 | if (strlen(diskparam) >= MAXPATHLEN) { | |
294 | (void)fprintf(stderr, "Disk or filesystem name too long: %s\n", diskparam); | |
92a9bf12 SP |
295 | exit(X_STARTUP); |
296 | } | |
1227625a SP |
297 | argc--; |
298 | if (argc >= 1) { | |
299 | (void)fprintf(stderr, "Unknown arguments to dump:"); | |
300 | while (argc--) | |
301 | (void)fprintf(stderr, " %s", *argv++); | |
302 | (void)fprintf(stderr, "\n"); | |
b45f51d6 | 303 | exit(X_STARTUP); |
1227625a SP |
304 | } |
305 | if (Tflag && uflag) { | |
306 | (void)fprintf(stderr, | |
307 | "You cannot use the T and u flags together.\n"); | |
b45f51d6 | 308 | exit(X_STARTUP); |
1227625a | 309 | } |
d1c73b9a | 310 | if (strcmp(tapeprefix, "-") == 0) { |
1227625a | 311 | pipeout++; |
d1c73b9a | 312 | tapeprefix = "standard output"; |
1227625a SP |
313 | } |
314 | ||
315 | if (blocksperfile) | |
316 | blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ | |
b45f51d6 | 317 | else if (!unlimited) { |
1227625a SP |
318 | /* |
319 | * Determine how to default tape size and density | |
320 | * | |
321 | * density tape size | |
322 | * 9-track 1600 bpi (160 bytes/.1") 2300 ft. | |
323 | * 9-track 6250 bpi (625 bytes/.1") 2300 ft. | |
324 | * cartridge 8000 bpi (100 bytes/.1") 1700 ft. | |
325 | * (450*4 - slop) | |
b45f51d6 | 326 | * hilit19 hits again: " |
1227625a SP |
327 | */ |
328 | if (density == 0) | |
329 | density = cartridge ? 100 : 160; | |
330 | if (tsize == 0) | |
331 | tsize = cartridge ? 1700L*120L : 2300L*120L; | |
332 | } | |
333 | ||
d1c73b9a SP |
334 | if (strchr(tapeprefix, ':')) { |
335 | host = tapeprefix; | |
336 | tapeprefix = strchr(host, ':'); | |
337 | *tapeprefix++ = '\0'; | |
1227625a | 338 | #ifdef RDUMP |
d1c73b9a | 339 | if (index(tapeprefix, '\n')) { |
b45f51d6 SP |
340 | (void)fprintf(stderr, "invalid characters in tape\n"); |
341 | exit(X_STARTUP); | |
342 | } | |
1227625a | 343 | if (rmthost(host) == 0) |
b45f51d6 | 344 | exit(X_STARTUP); |
1227625a SP |
345 | #else |
346 | (void)fprintf(stderr, "remote dump not enabled\n"); | |
b45f51d6 | 347 | exit(X_STARTUP); |
1227625a SP |
348 | #endif |
349 | } | |
350 | (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ | |
351 | ||
352 | if (signal(SIGHUP, SIG_IGN) != SIG_IGN) | |
353 | signal(SIGHUP, sig); | |
354 | if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) | |
355 | signal(SIGTRAP, sig); | |
356 | if (signal(SIGFPE, SIG_IGN) != SIG_IGN) | |
357 | signal(SIGFPE, sig); | |
358 | if (signal(SIGBUS, SIG_IGN) != SIG_IGN) | |
359 | signal(SIGBUS, sig); | |
360 | if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) | |
361 | signal(SIGSEGV, sig); | |
362 | if (signal(SIGTERM, SIG_IGN) != SIG_IGN) | |
363 | signal(SIGTERM, sig); | |
364 | if (signal(SIGINT, interrupt) == SIG_IGN) | |
365 | signal(SIGINT, SIG_IGN); | |
1227625a SP |
366 | set_operators(); /* /etc/group snarfed */ |
367 | getfstab(); /* /etc/fstab snarfed */ | |
8954518f SP |
368 | |
369 | disk = get_device_name(diskparam); | |
370 | if (!disk) { /* null means the disk is some form | |
371 | of LABEL= or UID= but it was not | |
372 | found */ | |
373 | msg("Cannot find a disk having %s\n", diskparam); | |
374 | exit(X_STARTUP); | |
375 | } | |
a2c9bd28 SP |
376 | /* |
377 | * disk may end in / and this can confuse | |
378 | * fstabsearch. | |
379 | */ | |
380 | if (strlen(disk) > 1 && disk[strlen(disk) - 1] == '/') | |
381 | disk[strlen(disk) - 1] = '\0'; | |
1227625a SP |
382 | /* |
383 | * disk can be either the full special file name, | |
384 | * the suffix of the special file name, | |
385 | * the special name missing the leading '/', | |
386 | * the file system name with or without the leading '/'. | |
387 | */ | |
ddd2ef55 | 388 | if ((dt = fstabsearch(disk)) != NULL) { |
1227625a SP |
389 | disk = rawname(dt->fs_spec); |
390 | (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); | |
391 | (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); | |
392 | #ifdef __linux__ | |
393 | } else { | |
394 | /* | |
395 | * The argument was not found in the fstab | |
396 | * assume that this is a subtree and search for it | |
397 | */ | |
398 | #ifdef HAVE_REALPATH | |
399 | if (realpath(disk, pathname) == NULL) | |
400 | #endif | |
401 | strcpy(pathname, disk); | |
402 | dt = fstabsearchdir(pathname, directory); | |
403 | if (dt != NULL) { | |
404 | char name[MAXPATHLEN]; | |
a2c9bd28 | 405 | (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); |
960c1264 | 406 | (void)snprintf(name, sizeof(name), "%s (dir %s)", |
a2c9bd28 SP |
407 | dt->fs_file, directory); |
408 | (void)strncpy(spcl.c_filesys, name, NAMELEN); | |
1227625a SP |
409 | disk = rawname(dt->fs_spec); |
410 | } else { | |
411 | (void)strncpy(spcl.c_dev, disk, NAMELEN); | |
412 | (void)strncpy(spcl.c_filesys, "an unlisted file system", | |
413 | NAMELEN); | |
414 | } | |
415 | } | |
416 | #else | |
417 | } else { | |
418 | (void)strncpy(spcl.c_dev, disk, NAMELEN); | |
419 | (void)strncpy(spcl.c_filesys, "an unlisted file system", | |
420 | NAMELEN); | |
421 | } | |
422 | #endif | |
79e31865 SP |
423 | |
424 | if (directory[0] != 0) { | |
425 | if (level != '0') { | |
426 | (void)fprintf(stderr, "Only level 0 dumps are allowed on a subdirectory\n"); | |
427 | exit(X_STARTUP); | |
428 | } | |
429 | if (uflag) { | |
430 | (void)fprintf(stderr, "You can't update the dumpdates file when dumping a subdirectory\n"); | |
431 | exit(X_STARTUP); | |
432 | } | |
433 | } | |
b45f51d6 SP |
434 | spcl.c_dev[NAMELEN-1]='\0'; |
435 | spcl.c_filesys[NAMELEN-1]='\0'; | |
436 | (void)strncpy(spcl.c_label, labelstr, sizeof(spcl.c_label) - 1); | |
1227625a SP |
437 | (void)gethostname(spcl.c_host, NAMELEN); |
438 | spcl.c_level = level - '0'; | |
439 | spcl.c_type = TS_TAPE; | |
440 | if (!Tflag) | |
8d4197bb SP |
441 | getdumptime(uflag); /* dumpdates snarfed */ |
442 | ||
443 | if (spcl.c_ddate == 0 && spcl.c_level) { | |
444 | msg("WARNING: There is no inferior level dump on this filesystem\n"); | |
445 | msg("WARNING: Assuming a level 0 dump by default\n"); | |
446 | level = '0'; | |
447 | spcl.c_level = 0; | |
448 | } | |
1227625a | 449 | |
d1c73b9a | 450 | if (Mflag) |
92a9bf12 | 451 | snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno + 1); |
d1c73b9a | 452 | else |
92a9bf12 SP |
453 | strncpy(tape, tapeprefix, MAXPATHLEN); |
454 | tape[MAXPATHLEN - 1] = '\0'; | |
d1c73b9a | 455 | |
144a6db1 SP |
456 | if (!sizest) { |
457 | ||
458 | msg("Date of this level %c dump: %s", level, | |
8d4197bb | 459 | #ifdef __linux__ |
144a6db1 | 460 | spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date)); |
1227625a | 461 | #else |
144a6db1 | 462 | spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); |
1227625a | 463 | #endif |
144a6db1 | 464 | msg("Date of last level %c dump: %s", lastlevel, |
1227625a | 465 | #ifdef __linux__ |
144a6db1 | 466 | spcl.c_ddate == 0 ? "the epoch\n" : ctime4(&spcl.c_ddate)); |
1227625a | 467 | #else |
144a6db1 | 468 | spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); |
1227625a | 469 | #endif |
a2c9bd28 | 470 | msg("Dumping %s (%s) ", disk, spcl.c_filesys); |
144a6db1 SP |
471 | if (host) |
472 | msgtail("to %s on host %s\n", tape, host); | |
473 | else | |
474 | msgtail("to %s\n", tape); | |
475 | msg("Label: %s\n", labelstr); | |
476 | ||
477 | } /* end of size estimate */ | |
1227625a SP |
478 | |
479 | #ifdef __linux__ | |
c930abff | 480 | retval = dump_fs_open(disk, &fs); |
1227625a SP |
481 | if (retval) { |
482 | com_err(disk, retval, "while opening filesystem"); | |
483 | if (retval == EXT2_ET_REV_TOO_HIGH) | |
484 | printf ("Get a newer version of dump!\n"); | |
b45f51d6 | 485 | exit(X_STARTUP); |
1227625a SP |
486 | } |
487 | if (fs->super->s_rev_level > DUMP_CURRENT_REV) { | |
488 | com_err(disk, retval, "while opening filesystem"); | |
489 | printf ("Get a newer version of dump!\n"); | |
b45f51d6 | 490 | exit(X_STARTUP); |
1227625a SP |
491 | } |
492 | if ((diskfd = open(disk, O_RDONLY)) < 0) { | |
493 | msg("Cannot open %s\n", disk); | |
b45f51d6 | 494 | exit(X_STARTUP); |
1227625a SP |
495 | } |
496 | sync(); | |
497 | dev_bsize = DEV_BSIZE; | |
498 | dev_bshift = ffs(dev_bsize) - 1; | |
499 | if (dev_bsize != (1 << dev_bshift)) | |
500 | quit("dev_bsize (%d) is not a power of 2", dev_bsize); | |
501 | tp_bshift = ffs(TP_BSIZE) - 1; | |
502 | if (TP_BSIZE != (1 << tp_bshift)) | |
503 | quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); | |
504 | maxino = fs->super->s_inodes_count; | |
505 | #if 0 | |
506 | spcl.c_flags |= DR_NEWINODEFMT; | |
507 | #endif | |
508 | #else /* __linux __*/ | |
509 | if ((diskfd = open(disk, O_RDONLY)) < 0) { | |
510 | msg("Cannot open %s\n", disk); | |
b45f51d6 | 511 | exit(X_STARTUP); |
1227625a SP |
512 | } |
513 | sync(); | |
514 | sblock = (struct fs *)sblock_buf; | |
515 | bread(SBOFF, (char *) sblock, SBSIZE); | |
516 | if (sblock->fs_magic != FS_MAGIC) | |
517 | quit("bad sblock magic number\n"); | |
518 | dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); | |
519 | dev_bshift = ffs(dev_bsize) - 1; | |
520 | if (dev_bsize != (1 << dev_bshift)) | |
521 | quit("dev_bsize (%d) is not a power of 2", dev_bsize); | |
522 | tp_bshift = ffs(TP_BSIZE) - 1; | |
523 | if (TP_BSIZE != (1 << tp_bshift)) | |
524 | quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); | |
1227625a SP |
525 | #ifdef FS_44INODEFMT |
526 | if (sblock->fs_inodefmt >= FS_44INODEFMT) | |
527 | spcl.c_flags |= DR_NEWINODEFMT; | |
528 | #endif | |
529 | maxino = sblock->fs_ipg * sblock->fs_ncg; | |
530 | #endif /* __linux__ */ | |
531 | mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); | |
532 | usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); | |
533 | dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); | |
534 | dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); | |
a0d8fe2d | 535 | tapesize = 2 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); |
1227625a SP |
536 | |
537 | nonodump = spcl.c_level < honorlevel; | |
538 | ||
ddd2ef55 SP |
539 | #if defined(SIGINFO) |
540 | (void)signal(SIGINFO, statussig); | |
541 | #endif | |
542 | ||
144a6db1 SP |
543 | if (!sizest) |
544 | msg("mapping (Pass I) [regular files]\n"); | |
1227625a SP |
545 | #ifdef __linux__ |
546 | if (directory[0] == 0) | |
547 | anydirskipped = mapfiles(maxino, &tapesize); | |
548 | else | |
549 | anydirskipped = mapfilesfromdir(maxino, &tapesize, directory); | |
550 | #else | |
551 | anydirskipped = mapfiles(maxino, &tapesize); | |
552 | #endif | |
553 | ||
144a6db1 SP |
554 | if (!sizest) |
555 | msg("mapping (Pass II) [directories]\n"); | |
1227625a SP |
556 | while (anydirskipped) { |
557 | anydirskipped = mapdirs(maxino, &tapesize); | |
558 | } | |
559 | ||
144a6db1 | 560 | if (sizest) { |
a0d8fe2d | 561 | printf("%.0f\n", ((double)tapesize + 11) * TP_BSIZE); |
a29c23cc | 562 | exit(X_FINOK); |
144a6db1 SP |
563 | } /* stop here for size estimate */ |
564 | ||
b45f51d6 | 565 | if (pipeout || unlimited) { |
a0d8fe2d | 566 | tapesize += 11; /* 10 trailer blocks + 1 map header */ |
1227625a SP |
567 | msg("estimated %ld tape blocks.\n", tapesize); |
568 | } else { | |
569 | double fetapes; | |
570 | ||
571 | if (blocksperfile) | |
572 | fetapes = (double) tapesize / blocksperfile; | |
573 | else if (cartridge) { | |
574 | /* Estimate number of tapes, assuming streaming stops at | |
575 | the end of each block written, and not in mid-block. | |
576 | Assume no erroneous blocks; this can be compensated | |
577 | for with an artificially low tape size. */ | |
b45f51d6 SP |
578 | fetapes = |
579 | ( (double) tapesize /* blocks */ | |
1227625a | 580 | * TP_BSIZE /* bytes/block */ |
b45f51d6 | 581 | * (1.0/density) /* 0.1" / byte " */ |
1227625a | 582 | + |
b45f51d6 | 583 | (double) tapesize /* blocks */ |
1227625a | 584 | * (1.0/ntrec) /* streaming-stops per block */ |
b45f51d6 SP |
585 | * 15.48 /* 0.1" / streaming-stop " */ |
586 | ) * (1.0 / tsize ); /* tape / 0.1" " */ | |
1227625a SP |
587 | } else { |
588 | /* Estimate number of tapes, for old fashioned 9-track | |
589 | tape */ | |
590 | int tenthsperirg = (density == 625) ? 3 : 7; | |
591 | fetapes = | |
b45f51d6 | 592 | ( (double) tapesize /* blocks */ |
1227625a | 593 | * TP_BSIZE /* bytes / block */ |
b45f51d6 | 594 | * (1.0/density) /* 0.1" / byte " */ |
1227625a | 595 | + |
b45f51d6 | 596 | (double) tapesize /* blocks */ |
1227625a | 597 | * (1.0/ntrec) /* IRG's / block */ |
b45f51d6 SP |
598 | * tenthsperirg /* 0.1" / IRG " */ |
599 | ) * (1.0 / tsize ); /* tape / 0.1" " */ | |
1227625a SP |
600 | } |
601 | etapes = fetapes; /* truncating assignment */ | |
602 | etapes++; | |
603 | /* count the dumped inodes map on each additional tape */ | |
604 | tapesize += (etapes - 1) * | |
605 | (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); | |
606 | tapesize += etapes + 10; /* headers + 10 trailer blks */ | |
607 | msg("estimated %ld tape blocks on %3.2f tape(s).\n", | |
608 | tapesize, fetapes); | |
609 | } | |
610 | ||
611 | /* | |
612 | * Allocate tape buffer. | |
613 | */ | |
614 | if (!alloctape()) | |
b45f51d6 SP |
615 | quit( |
616 | "can't allocate tape buffers - try a smaller blocking factor.\n"); | |
1227625a SP |
617 | |
618 | startnewtape(1); | |
8d4197bb SP |
619 | #ifdef __linux__ |
620 | (void)time4(&(tstart_writing)); | |
621 | #else | |
1227625a | 622 | (void)time((time_t *)&(tstart_writing)); |
8d4197bb | 623 | #endif |
1227625a SP |
624 | dumpmap(usedinomap, TS_CLRI, maxino - 1); |
625 | ||
626 | msg("dumping (Pass III) [directories]\n"); | |
627 | dirty = 0; /* XXX just to get gcc to shut up */ | |
628 | for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { | |
629 | if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ | |
630 | dirty = *map++; | |
631 | else | |
632 | dirty >>= 1; | |
633 | if ((dirty & 1) == 0) | |
634 | continue; | |
635 | /* | |
636 | * Skip directory inodes deleted and maybe reallocated | |
637 | */ | |
638 | dp = getino(ino); | |
639 | if ((dp->di_mode & IFMT) != IFDIR) | |
640 | continue; | |
641 | #ifdef __linux__ | |
d6f78b13 SP |
642 | /* |
643 | * Skip directory inodes deleted and not yes reallocated... | |
644 | */ | |
645 | if (dp->di_nlink == 0 || dp->di_dtime != 0) | |
646 | continue; | |
1227625a SP |
647 | (void)dumpdirino(dp, ino); |
648 | #else | |
649 | (void)dumpino(dp, ino); | |
650 | #endif | |
651 | } | |
652 | ||
653 | msg("dumping (Pass IV) [regular files]\n"); | |
654 | for (map = dumpinomap, ino = 1; ino < maxino; ino++) { | |
1227625a SP |
655 | if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ |
656 | dirty = *map++; | |
657 | else | |
658 | dirty >>= 1; | |
659 | if ((dirty & 1) == 0) | |
660 | continue; | |
661 | /* | |
662 | * Skip inodes deleted and reallocated as directories. | |
663 | */ | |
664 | dp = getino(ino); | |
d6f78b13 | 665 | if ((dp->di_mode & IFMT) == IFDIR) |
1227625a | 666 | continue; |
d6f78b13 SP |
667 | #ifdef __linux__ |
668 | /* | |
669 | * No need to check here for deleted and not yes reallocated inodes | |
670 | * since this is done in dumpino(). | |
671 | */ | |
672 | #endif | |
1227625a SP |
673 | (void)dumpino(dp, ino); |
674 | } | |
675 | ||
8d4197bb SP |
676 | #ifdef __linux__ |
677 | (void)time4(&(tend_writing)); | |
678 | #else | |
1227625a | 679 | (void)time((time_t *)&(tend_writing)); |
8d4197bb | 680 | #endif |
1227625a SP |
681 | spcl.c_type = TS_END; |
682 | for (i = 0; i < ntrec; i++) | |
683 | writeheader(maxino - 1); | |
0d7af9c5 SP |
684 | |
685 | tnow = trewind(); | |
686 | ||
1227625a | 687 | if (pipeout) |
cbc94239 SP |
688 | msg("%ld tape blocks (%.2fMB)\n", spcl.c_tapea, |
689 | ((double)spcl.c_tapea * TP_BSIZE / 1048576)); | |
1227625a | 690 | else |
cbc94239 SP |
691 | msg("%ld tape blocks (%.2fMB) on %d volume(s)\n", |
692 | spcl.c_tapea, | |
693 | ((double)spcl.c_tapea * TP_BSIZE / 1048576), | |
694 | spcl.c_volume); | |
b45f51d6 | 695 | |
1227625a SP |
696 | /* report dump performance, avoid division through zero */ |
697 | if (tend_writing - tstart_writing == 0) | |
698 | msg("finished in less than a second\n"); | |
699 | else | |
700 | msg("finished in %d seconds, throughput %d KBytes/sec\n", | |
701 | tend_writing - tstart_writing, | |
702 | spcl.c_tapea / (tend_writing - tstart_writing)); | |
b45f51d6 | 703 | |
1227625a | 704 | putdumptime(); |
ddd2ef55 | 705 | #ifdef __linux__ |
d65ed175 | 706 | msg("Date of this level %c dump: %s", level, |
ddd2ef55 SP |
707 | spcl.c_date == 0 ? "the epoch\n" : ctime4(&spcl.c_date)); |
708 | #else | |
d65ed175 | 709 | msg("Date of this level %c dump: %s", level, |
ddd2ef55 SP |
710 | spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); |
711 | #endif | |
d65ed175 | 712 | msg("Date this dump completed: %s", ctime(&tnow)); |
ddd2ef55 | 713 | |
d65ed175 | 714 | msg("Average transfer rate: %ld KB/s\n", xferrate / tapeno); |
ddd2ef55 | 715 | |
1227625a SP |
716 | broadcast("DUMP IS DONE!\7\7\n"); |
717 | msg("DUMP IS DONE\n"); | |
718 | Exit(X_FINOK); | |
719 | /* NOTREACHED */ | |
ddd2ef55 | 720 | return 0; /* gcc - shut up */ |
1227625a SP |
721 | } |
722 | ||
723 | static void | |
ddd2ef55 | 724 | usage(void) |
1227625a | 725 | { |
df9ae507 SP |
726 | char white[MAXPATHLEN]; |
727 | int i; | |
728 | ||
729 | strncpy(white, __progname, MAXPATHLEN-1); | |
730 | white[MAXPATHLEN-1] = '\0'; | |
731 | for (i=0; i<MAXPATHLEN; ++i) | |
732 | if (white[i] != '\0') white[i] = ' '; | |
733 | ||
734 | fprintf(stderr, | |
735 | "%s %s\n", __progname, _DUMP_VERSION); | |
b45f51d6 | 736 | fprintf(stderr, |
df9ae507 | 737 | "usage:\t%s [-0123456789ac" |
b45f51d6 SP |
738 | #ifdef KERBEROS |
739 | "k" | |
740 | #endif | |
144a6db1 | 741 | "MnSu] [-B records] [-b blocksize] [-d density]\n" |
20c345aa | 742 | "\t%s [-e inode#] [-f file] [-h level] [-s feet] [-T date] filesystem\n" |
df9ae507 | 743 | "\t%s [-W | -w]\n", __progname, white, __progname); |
b45f51d6 | 744 | exit(X_STARTUP); |
1227625a SP |
745 | } |
746 | ||
747 | /* | |
748 | * Pick up a numeric argument. It must be nonnegative and in the given | |
749 | * range (except that a vmax of 0 means unlimited). | |
750 | */ | |
751 | static long | |
ddd2ef55 | 752 | numarg(const char *meaning, long vmin, long vmax) |
1227625a SP |
753 | { |
754 | char *p; | |
755 | long val; | |
756 | ||
757 | val = strtol(optarg, &p, 10); | |
758 | if (*p) | |
ddd2ef55 | 759 | errx(X_STARTUP, "illegal %s -- %s", meaning, optarg); |
1227625a | 760 | if (val < vmin || (vmax && val > vmax)) |
ddd2ef55 | 761 | errx(X_STARTUP, "%s must be between %ld and %ld", meaning, vmin, vmax); |
1227625a SP |
762 | return (val); |
763 | } | |
764 | ||
765 | void | |
ddd2ef55 | 766 | sig(int signo) |
1227625a SP |
767 | { |
768 | switch(signo) { | |
769 | case SIGALRM: | |
770 | case SIGBUS: | |
771 | case SIGFPE: | |
772 | case SIGHUP: | |
773 | case SIGTERM: | |
774 | case SIGTRAP: | |
775 | if (pipeout) | |
776 | quit("Signal on pipe: cannot recover\n"); | |
777 | msg("Rewriting attempted as response to unknown signal.\n"); | |
778 | (void)fflush(stderr); | |
779 | (void)fflush(stdout); | |
780 | close_rewind(); | |
781 | exit(X_REWRITE); | |
782 | /* NOTREACHED */ | |
783 | case SIGSEGV: | |
784 | msg("SIGSEGV: ABORTING!\n"); | |
785 | (void)signal(SIGSEGV, SIG_DFL); | |
786 | (void)kill(0, SIGSEGV); | |
787 | /* NOTREACHED */ | |
788 | } | |
789 | } | |
790 | ||
791 | char * | |
ddd2ef55 | 792 | rawname(char *cp) |
1227625a SP |
793 | { |
794 | #ifdef __linux__ | |
795 | return cp; | |
796 | #else /* __linux__ */ | |
797 | static char rawbuf[MAXPATHLEN]; | |
798 | char *dp = strrchr(cp, '/'); | |
799 | ||
800 | if (dp == NULL) | |
801 | return (NULL); | |
802 | *dp = '\0'; | |
b45f51d6 SP |
803 | (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); |
804 | rawbuf[MAXPATHLEN-1] = '\0'; | |
1227625a | 805 | *dp = '/'; |
b45f51d6 SP |
806 | (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); |
807 | (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); | |
1227625a SP |
808 | return (rawbuf); |
809 | #endif /* __linux__ */ | |
810 | } | |
811 | ||
812 | /* | |
813 | * obsolete -- | |
b45f51d6 SP |
814 | * Change set of key letters and ordered arguments into something |
815 | * getopt(3) will like. | |
1227625a SP |
816 | */ |
817 | static void | |
ddd2ef55 | 818 | obsolete(int *argcp, char **argvp[]) |
1227625a SP |
819 | { |
820 | int argc, flags; | |
b45f51d6 | 821 | char *ap, **argv, *flagsp=NULL, **nargv, *p=NULL; |
1227625a SP |
822 | |
823 | /* Setup. */ | |
824 | argv = *argvp; | |
825 | argc = *argcp; | |
826 | ||
827 | /* Return if no arguments or first argument has leading dash. */ | |
828 | ap = argv[1]; | |
829 | if (argc == 1 || *ap == '-') | |
830 | return; | |
831 | ||
832 | /* Allocate space for new arguments. */ | |
833 | if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || | |
834 | (p = flagsp = malloc(strlen(ap) + 2)) == NULL) | |
ddd2ef55 | 835 | err(X_STARTUP, "malloc new args"); |
1227625a SP |
836 | |
837 | *nargv++ = *argv; | |
838 | argv += 2; | |
839 | ||
840 | for (flags = 0; *ap; ++ap) { | |
841 | switch (*ap) { | |
842 | case 'B': | |
843 | case 'b': | |
844 | case 'd': | |
0d7af9c5 | 845 | case 'e': |
1227625a | 846 | case 'f': |
0d7af9c5 | 847 | case 'F': |
1227625a | 848 | case 'h': |
0d7af9c5 | 849 | case 'L': |
1227625a SP |
850 | case 's': |
851 | case 'T': | |
852 | if (*argv == NULL) { | |
b45f51d6 SP |
853 | warnx("option requires an argument -- %c", *ap); |
854 | usage(); | |
1227625a SP |
855 | } |
856 | if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) | |
ddd2ef55 | 857 | err(X_STARTUP, "malloc arg"); |
1227625a SP |
858 | nargv[0][0] = '-'; |
859 | nargv[0][1] = *ap; | |
860 | (void)strcpy(&nargv[0][2], *argv); | |
861 | ++argv; | |
862 | ++nargv; | |
863 | break; | |
864 | default: | |
865 | if (!flags) { | |
866 | *p++ = '-'; | |
867 | flags = 1; | |
868 | } | |
869 | *p++ = *ap; | |
870 | break; | |
871 | } | |
872 | } | |
873 | ||
874 | /* Terminate flags. */ | |
875 | if (flags) { | |
876 | *p = '\0'; | |
877 | *nargv++ = flagsp; | |
878 | } | |
879 | ||
880 | /* Copy remaining arguments. */ | |
b45f51d6 | 881 | while ((*nargv++ = *argv++)); |
1227625a SP |
882 | |
883 | /* Update argument count. */ | |
884 | *argcp = nargv - *argvp - 1; | |
885 | } |