Deleted Added
full compact
main.c (11770) main.c (12377)
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 4/15/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#ifdef sunos
47#include <sys/vnode.h>
48
49#include <ufs/inode.h>
50#include <ufs/fs.h>
51#else
52#include <ufs/ffs/fs.h>
53#include <ufs/ufs/dinode.h>
54#endif
55
56#include <protocols/dumprestore.h>
57
58#include <ctype.h>
59#include <errno.h>
60#include <fcntl.h>
61#include <fstab.h>
62#include <signal.h>
63#include <stdio.h>
64#ifdef __STDC__
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68#endif
69
70#include "dump.h"
71#include "pathnames.h"
72
73#ifndef SBOFF
74#define SBOFF (SBLOCK * DEV_BSIZE)
75#endif
76
77int notify = 0; /* notify operator flag */
78int blockswritten = 0; /* number of blocks written on current tape */
79int tapeno = 0; /* current tape number */
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)main.c 8.4 (Berkeley) 4/15/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46#ifdef sunos
47#include <sys/vnode.h>
48
49#include <ufs/inode.h>
50#include <ufs/fs.h>
51#else
52#include <ufs/ffs/fs.h>
53#include <ufs/ufs/dinode.h>
54#endif
55
56#include <protocols/dumprestore.h>
57
58#include <ctype.h>
59#include <errno.h>
60#include <fcntl.h>
61#include <fstab.h>
62#include <signal.h>
63#include <stdio.h>
64#ifdef __STDC__
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68#endif
69
70#include "dump.h"
71#include "pathnames.h"
72
73#ifndef SBOFF
74#define SBOFF (SBLOCK * DEV_BSIZE)
75#endif
76
77int notify = 0; /* notify operator flag */
78int blockswritten = 0; /* number of blocks written on current tape */
79int tapeno = 0; /* current tape number */
80int density = 0; /* density in bytes/0.1" */
80int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */
81int ntrec = NTREC; /* # tape blocks in each tape record */
82int cartridge = 0; /* Assume non-cartridge tape */
83long dev_bsize = 1; /* recalculated below */
84long blocksperfile; /* output blocks per file */
85char *host = NULL; /* remote host (if any) */
86
87static long numarg __P((int, char *, long, long, int *, char ***));
88static __dead void missingarg __P((int, char *));
89
90int
91main(argc, argv)
92 int argc;
93 char **argv;
94{
95 register ino_t ino;
96 register int dirty;
97 register struct dinode *dp;
98 register struct fstab *dt;
99 register char *map;
100 register char *cp;
101 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
102 ino_t maxino;
103
104 spcl.c_date = 0;
105 (void)time((time_t *)&spcl.c_date);
106
107 tsize = 0; /* Default later, based on 'c' option for cart tapes */
108 tape = _PATH_DEFTAPE;
109 dumpdates = _PATH_DUMPDATES;
110 temp = _PATH_DTMP;
111 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
112 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
113 level = '0';
114 if (argc == 1) {
115 (void) fprintf(stderr, "Must specify a key.\n");
116 Exit(X_ABORT);
117 }
118 argv++;
119 argc -= 2;
120 for (cp = *argv++; cp != NULL && *cp != '\0'; cp++) {
121 switch (*cp) {
122 case '-':
123 break;
124
125 case 'w':
126 lastdump('w'); /* tell us only what has to be done */
127 exit(0);
128
129 case 'W': /* what to do */
130 lastdump('W'); /* tell us state of what is done */
131 exit(0); /* do nothing else */
132
133 case 'f': /* output file */
134 if (argc < 1)
135 missingarg('f', "output file");
136 tape = *argv++;
137 argc--;
138 break;
139
140 case 'd': /* density, in bits per inch */
141 density = numarg('d', "density",
142 10L, 327670L, &argc, &argv) / 10;
143 if (density >= 625 && !bflag)
144 ntrec = HIGHDENSITYTREC;
145 break;
146
147 case 's': /* tape size, feet */
148 tsize = numarg('s', "size",
149 1L, 0L, &argc, &argv) * 12 * 10;
150 break;
151
152 case 'T': /* time of last dump */
153 if (argc < 1)
154 missingarg('T', "time of last dump");
155 spcl.c_ddate = unctime(*argv);
156 if (spcl.c_ddate < 0) {
157 (void)fprintf(stderr, "bad time \"%s\"\n",
158 *argv);
159 exit(X_ABORT);
160 }
161 Tflag = 1;
162 lastlevel = '?';
163 argc--;
164 argv++;
165 break;
166
167 case 'b': /* blocks per tape write */
168 ntrec = numarg('b', "number of blocks per write",
169 1L, 1000L, &argc, &argv);
81int ntrec = NTREC; /* # tape blocks in each tape record */
82int cartridge = 0; /* Assume non-cartridge tape */
83long dev_bsize = 1; /* recalculated below */
84long blocksperfile; /* output blocks per file */
85char *host = NULL; /* remote host (if any) */
86
87static long numarg __P((int, char *, long, long, int *, char ***));
88static __dead void missingarg __P((int, char *));
89
90int
91main(argc, argv)
92 int argc;
93 char **argv;
94{
95 register ino_t ino;
96 register int dirty;
97 register struct dinode *dp;
98 register struct fstab *dt;
99 register char *map;
100 register char *cp;
101 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
102 ino_t maxino;
103
104 spcl.c_date = 0;
105 (void)time((time_t *)&spcl.c_date);
106
107 tsize = 0; /* Default later, based on 'c' option for cart tapes */
108 tape = _PATH_DEFTAPE;
109 dumpdates = _PATH_DUMPDATES;
110 temp = _PATH_DTMP;
111 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
112 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
113 level = '0';
114 if (argc == 1) {
115 (void) fprintf(stderr, "Must specify a key.\n");
116 Exit(X_ABORT);
117 }
118 argv++;
119 argc -= 2;
120 for (cp = *argv++; cp != NULL && *cp != '\0'; cp++) {
121 switch (*cp) {
122 case '-':
123 break;
124
125 case 'w':
126 lastdump('w'); /* tell us only what has to be done */
127 exit(0);
128
129 case 'W': /* what to do */
130 lastdump('W'); /* tell us state of what is done */
131 exit(0); /* do nothing else */
132
133 case 'f': /* output file */
134 if (argc < 1)
135 missingarg('f', "output file");
136 tape = *argv++;
137 argc--;
138 break;
139
140 case 'd': /* density, in bits per inch */
141 density = numarg('d', "density",
142 10L, 327670L, &argc, &argv) / 10;
143 if (density >= 625 && !bflag)
144 ntrec = HIGHDENSITYTREC;
145 break;
146
147 case 's': /* tape size, feet */
148 tsize = numarg('s', "size",
149 1L, 0L, &argc, &argv) * 12 * 10;
150 break;
151
152 case 'T': /* time of last dump */
153 if (argc < 1)
154 missingarg('T', "time of last dump");
155 spcl.c_ddate = unctime(*argv);
156 if (spcl.c_ddate < 0) {
157 (void)fprintf(stderr, "bad time \"%s\"\n",
158 *argv);
159 exit(X_ABORT);
160 }
161 Tflag = 1;
162 lastlevel = '?';
163 argc--;
164 argv++;
165 break;
166
167 case 'b': /* blocks per tape write */
168 ntrec = numarg('b', "number of blocks per write",
169 1L, 1000L, &argc, &argv);
170 /* XXX restore is unable to restore dumps that
171 were created with a blocksize larger than 32K.
172 Possibly a bug in the scsi tape driver. */
173 if ( ntrec > 32 ) {
174 msg("please choose a blocksize <= 32\n");
175 exit(X_ABORT);
176 }
170 break;
171
172 case 'B': /* blocks per output file */
173 blocksperfile = numarg('B', "number of blocks per file",
174 1L, 0L, &argc, &argv);
175 break;
176
177 case 'c': /* Tape is cart. not 9-track */
178 cartridge = 1;
179 break;
180
181 /* dump level */
182 case '0': case '1': case '2': case '3': case '4':
183 case '5': case '6': case '7': case '8': case '9':
184 level = *cp;
185 break;
186
187 case 'u': /* update /etc/dumpdates */
188 uflag = 1;
189 break;
190
191 case 'n': /* notify operators */
192 notify = 1;
193 break;
194
195 case 'h':
196 honorlevel = numarg('h', "honor level",
197 0L, 10L, &argc, &argv);
198 break;
199
200 default:
201 (void)fprintf(stderr, "bad key '%c'\n", *cp);
202 exit(X_ABORT);
203 }
204 }
205 if (argc < 1) {
206 (void)fprintf(stderr, "Must specify disk or filesystem\n");
207 exit(X_ABORT);
208 }
209 disk = *argv++;
210 argc--;
211 if (argc >= 1) {
212 (void)fprintf(stderr, "Unknown arguments to dump:");
213 while (argc--)
214 (void)fprintf(stderr, " %s", *argv++);
215 (void)fprintf(stderr, "\n");
216 exit(X_ABORT);
217 }
218 if (Tflag && uflag) {
219 (void)fprintf(stderr,
220 "You cannot use the T and u flags together.\n");
221 exit(X_ABORT);
222 }
223 if (strcmp(tape, "-") == 0) {
224 pipeout++;
225 tape = "standard output";
226 }
227
228 if (blocksperfile)
229 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
230 else {
231 /*
232 * Determine how to default tape size and density
233 *
234 * density tape size
235 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
236 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
237 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
238 * (450*4 - slop)
177 break;
178
179 case 'B': /* blocks per output file */
180 blocksperfile = numarg('B', "number of blocks per file",
181 1L, 0L, &argc, &argv);
182 break;
183
184 case 'c': /* Tape is cart. not 9-track */
185 cartridge = 1;
186 break;
187
188 /* dump level */
189 case '0': case '1': case '2': case '3': case '4':
190 case '5': case '6': case '7': case '8': case '9':
191 level = *cp;
192 break;
193
194 case 'u': /* update /etc/dumpdates */
195 uflag = 1;
196 break;
197
198 case 'n': /* notify operators */
199 notify = 1;
200 break;
201
202 case 'h':
203 honorlevel = numarg('h', "honor level",
204 0L, 10L, &argc, &argv);
205 break;
206
207 default:
208 (void)fprintf(stderr, "bad key '%c'\n", *cp);
209 exit(X_ABORT);
210 }
211 }
212 if (argc < 1) {
213 (void)fprintf(stderr, "Must specify disk or filesystem\n");
214 exit(X_ABORT);
215 }
216 disk = *argv++;
217 argc--;
218 if (argc >= 1) {
219 (void)fprintf(stderr, "Unknown arguments to dump:");
220 while (argc--)
221 (void)fprintf(stderr, " %s", *argv++);
222 (void)fprintf(stderr, "\n");
223 exit(X_ABORT);
224 }
225 if (Tflag && uflag) {
226 (void)fprintf(stderr,
227 "You cannot use the T and u flags together.\n");
228 exit(X_ABORT);
229 }
230 if (strcmp(tape, "-") == 0) {
231 pipeout++;
232 tape = "standard output";
233 }
234
235 if (blocksperfile)
236 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
237 else {
238 /*
239 * Determine how to default tape size and density
240 *
241 * density tape size
242 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
243 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
244 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
245 * (450*4 - slop)
246 * hilit19 hits again: "
239 */
240 if (density == 0)
241 density = cartridge ? 100 : 160;
242 if (tsize == 0)
243 tsize = cartridge ? 1700L*120L : 2300L*120L;
244 }
245
246 if (index(tape, ':')) {
247 host = tape;
248 tape = index(host, ':');
249 *tape++ = '\0';
250#ifdef RDUMP
251 if (rmthost(host) == 0)
252 exit(X_ABORT);
253#else
254 (void)fprintf(stderr, "remote dump not enabled\n");
255 exit(X_ABORT);
256#endif
257 }
258 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
259
260 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
261 signal(SIGHUP, sig);
262 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
263 signal(SIGTRAP, sig);
264 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
265 signal(SIGFPE, sig);
266 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
267 signal(SIGBUS, sig);
268 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
269 signal(SIGSEGV, sig);
270 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
271 signal(SIGTERM, sig);
272 if (signal(SIGINT, interrupt) == SIG_IGN)
273 signal(SIGINT, SIG_IGN);
274
275 set_operators(); /* /etc/group snarfed */
276 getfstab(); /* /etc/fstab snarfed */
277 /*
278 * disk can be either the full special file name,
279 * the suffix of the special file name,
280 * the special name missing the leading '/',
281 * the file system name with or without the leading '/'.
282 */
283 dt = fstabsearch(disk);
284 if (dt != NULL) {
285 disk = rawname(dt->fs_spec);
286 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
287 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
288 } else {
289 (void)strncpy(spcl.c_dev, disk, NAMELEN);
290 (void)strncpy(spcl.c_filesys, "an unlisted file system",
291 NAMELEN);
292 }
293 (void)strcpy(spcl.c_label, "none");
294 (void)gethostname(spcl.c_host, NAMELEN);
295 spcl.c_level = level - '0';
296 spcl.c_type = TS_TAPE;
297 if (!Tflag)
298 getdumptime(); /* /etc/dumpdates snarfed */
299
300 msg("Date of this level %c dump: %s", level,
301 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
302 msg("Date of last level %c dump: %s", lastlevel,
303 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
304 msg("Dumping %s ", disk);
305 if (dt != NULL)
306 msgtail("(%s) ", dt->fs_file);
307 if (host)
308 msgtail("to %s on host %s\n", tape, host);
309 else
310 msgtail("to %s\n", tape);
311
312 if ((diskfd = open(disk, O_RDONLY)) < 0) {
313 msg("Cannot open %s\n", disk);
314 exit(X_ABORT);
315 }
316 sync();
317 sblock = (struct fs *)sblock_buf;
318 bread(SBOFF, (char *) sblock, SBSIZE);
319 if (sblock->fs_magic != FS_MAGIC)
320 quit("bad sblock magic number\n");
321 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
322 dev_bshift = ffs(dev_bsize) - 1;
323 if (dev_bsize != (1 << dev_bshift))
324 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
325 tp_bshift = ffs(TP_BSIZE) - 1;
326 if (TP_BSIZE != (1 << tp_bshift))
327 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
328#ifdef FS_44INODEFMT
329 if (sblock->fs_inodefmt >= FS_44INODEFMT)
330 spcl.c_flags |= DR_NEWINODEFMT;
331#endif
332 maxino = sblock->fs_ipg * sblock->fs_ncg;
333 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
334 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
335 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
336 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
337 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
338
339 nonodump = spcl.c_level < honorlevel;
340
341 msg("mapping (Pass I) [regular files]\n");
342 anydirskipped = mapfiles(maxino, &tapesize);
343
344 msg("mapping (Pass II) [directories]\n");
345 while (anydirskipped) {
346 anydirskipped = mapdirs(maxino, &tapesize);
347 }
348
349 if (pipeout) {
350 tapesize += 10; /* 10 trailer blocks */
351 msg("estimated %ld tape blocks.\n", tapesize);
352 } else {
353 double fetapes;
354
355 if (blocksperfile)
356 fetapes = (double) tapesize / blocksperfile;
357 else if (cartridge) {
358 /* Estimate number of tapes, assuming streaming stops at
359 the end of each block written, and not in mid-block.
360 Assume no erroneous blocks; this can be compensated
361 for with an artificially low tape size. */
362 fetapes =
363 ( tapesize /* blocks */
364 * TP_BSIZE /* bytes/block */
247 */
248 if (density == 0)
249 density = cartridge ? 100 : 160;
250 if (tsize == 0)
251 tsize = cartridge ? 1700L*120L : 2300L*120L;
252 }
253
254 if (index(tape, ':')) {
255 host = tape;
256 tape = index(host, ':');
257 *tape++ = '\0';
258#ifdef RDUMP
259 if (rmthost(host) == 0)
260 exit(X_ABORT);
261#else
262 (void)fprintf(stderr, "remote dump not enabled\n");
263 exit(X_ABORT);
264#endif
265 }
266 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
267
268 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
269 signal(SIGHUP, sig);
270 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
271 signal(SIGTRAP, sig);
272 if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
273 signal(SIGFPE, sig);
274 if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
275 signal(SIGBUS, sig);
276 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
277 signal(SIGSEGV, sig);
278 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
279 signal(SIGTERM, sig);
280 if (signal(SIGINT, interrupt) == SIG_IGN)
281 signal(SIGINT, SIG_IGN);
282
283 set_operators(); /* /etc/group snarfed */
284 getfstab(); /* /etc/fstab snarfed */
285 /*
286 * disk can be either the full special file name,
287 * the suffix of the special file name,
288 * the special name missing the leading '/',
289 * the file system name with or without the leading '/'.
290 */
291 dt = fstabsearch(disk);
292 if (dt != NULL) {
293 disk = rawname(dt->fs_spec);
294 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
295 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
296 } else {
297 (void)strncpy(spcl.c_dev, disk, NAMELEN);
298 (void)strncpy(spcl.c_filesys, "an unlisted file system",
299 NAMELEN);
300 }
301 (void)strcpy(spcl.c_label, "none");
302 (void)gethostname(spcl.c_host, NAMELEN);
303 spcl.c_level = level - '0';
304 spcl.c_type = TS_TAPE;
305 if (!Tflag)
306 getdumptime(); /* /etc/dumpdates snarfed */
307
308 msg("Date of this level %c dump: %s", level,
309 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
310 msg("Date of last level %c dump: %s", lastlevel,
311 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
312 msg("Dumping %s ", disk);
313 if (dt != NULL)
314 msgtail("(%s) ", dt->fs_file);
315 if (host)
316 msgtail("to %s on host %s\n", tape, host);
317 else
318 msgtail("to %s\n", tape);
319
320 if ((diskfd = open(disk, O_RDONLY)) < 0) {
321 msg("Cannot open %s\n", disk);
322 exit(X_ABORT);
323 }
324 sync();
325 sblock = (struct fs *)sblock_buf;
326 bread(SBOFF, (char *) sblock, SBSIZE);
327 if (sblock->fs_magic != FS_MAGIC)
328 quit("bad sblock magic number\n");
329 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
330 dev_bshift = ffs(dev_bsize) - 1;
331 if (dev_bsize != (1 << dev_bshift))
332 quit("dev_bsize (%d) is not a power of 2", dev_bsize);
333 tp_bshift = ffs(TP_BSIZE) - 1;
334 if (TP_BSIZE != (1 << tp_bshift))
335 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
336#ifdef FS_44INODEFMT
337 if (sblock->fs_inodefmt >= FS_44INODEFMT)
338 spcl.c_flags |= DR_NEWINODEFMT;
339#endif
340 maxino = sblock->fs_ipg * sblock->fs_ncg;
341 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
342 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
343 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
344 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
345 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
346
347 nonodump = spcl.c_level < honorlevel;
348
349 msg("mapping (Pass I) [regular files]\n");
350 anydirskipped = mapfiles(maxino, &tapesize);
351
352 msg("mapping (Pass II) [directories]\n");
353 while (anydirskipped) {
354 anydirskipped = mapdirs(maxino, &tapesize);
355 }
356
357 if (pipeout) {
358 tapesize += 10; /* 10 trailer blocks */
359 msg("estimated %ld tape blocks.\n", tapesize);
360 } else {
361 double fetapes;
362
363 if (blocksperfile)
364 fetapes = (double) tapesize / blocksperfile;
365 else if (cartridge) {
366 /* Estimate number of tapes, assuming streaming stops at
367 the end of each block written, and not in mid-block.
368 Assume no erroneous blocks; this can be compensated
369 for with an artificially low tape size. */
370 fetapes =
371 ( tapesize /* blocks */
372 * TP_BSIZE /* bytes/block */
365 * (1.0/density) /* 0.1" / byte */
373 * (1.0/density) /* 0.1" / byte " */
366 +
367 tapesize /* blocks */
368 * (1.0/ntrec) /* streaming-stops per block */
374 +
375 tapesize /* blocks */
376 * (1.0/ntrec) /* streaming-stops per block */
369 * 15.48 /* 0.1" / streaming-stop */
370 ) * (1.0 / tsize ); /* tape / 0.1" */
377 * 15.48 /* 0.1" / streaming-stop " */
378 ) * (1.0 / tsize ); /* tape / 0.1" " */
371 } else {
372 /* Estimate number of tapes, for old fashioned 9-track
373 tape */
374 int tenthsperirg = (density == 625) ? 3 : 7;
375 fetapes =
376 ( tapesize /* blocks */
377 * TP_BSIZE /* bytes / block */
379 } else {
380 /* Estimate number of tapes, for old fashioned 9-track
381 tape */
382 int tenthsperirg = (density == 625) ? 3 : 7;
383 fetapes =
384 ( tapesize /* blocks */
385 * TP_BSIZE /* bytes / block */
378 * (1.0/density) /* 0.1" / byte */
386 * (1.0/density) /* 0.1" / byte " */
379 +
380 tapesize /* blocks */
381 * (1.0/ntrec) /* IRG's / block */
387 +
388 tapesize /* blocks */
389 * (1.0/ntrec) /* IRG's / block */
382 * tenthsperirg /* 0.1" / IRG */
383 ) * (1.0 / tsize ); /* tape / 0.1" */
390 * tenthsperirg /* 0.1" / IRG " */
391 ) * (1.0 / tsize ); /* tape / 0.1" " */
384 }
385 etapes = fetapes; /* truncating assignment */
386 etapes++;
387 /* count the dumped inodes map on each additional tape */
388 tapesize += (etapes - 1) *
389 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
390 tapesize += etapes + 10; /* headers + 10 trailer blks */
391 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
392 tapesize, fetapes);
393 }
394
395 /*
396 * Allocate tape buffer.
397 */
398 if (!alloctape())
399 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
400
401 startnewtape(1);
402 (void)time((time_t *)&(tstart_writing));
403 dumpmap(usedinomap, TS_CLRI, maxino - 1);
404
405 msg("dumping (Pass III) [directories]\n");
406 dirty = 0; /* XXX just to get gcc to shut up */
407 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
408 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
409 dirty = *map++;
410 else
411 dirty >>= 1;
412 if ((dirty & 1) == 0)
413 continue;
414 /*
415 * Skip directory inodes deleted and maybe reallocated
416 */
417 dp = getino(ino);
418 if ((dp->di_mode & IFMT) != IFDIR)
419 continue;
420 (void)dumpino(dp, ino);
421 }
422
423 msg("dumping (Pass IV) [regular files]\n");
424 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
425 int mode;
426
427 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
428 dirty = *map++;
429 else
430 dirty >>= 1;
431 if ((dirty & 1) == 0)
432 continue;
433 /*
434 * Skip inodes deleted and reallocated as directories.
435 */
436 dp = getino(ino);
437 mode = dp->di_mode & IFMT;
438 if (mode == IFDIR)
439 continue;
440 (void)dumpino(dp, ino);
441 }
442
392 }
393 etapes = fetapes; /* truncating assignment */
394 etapes++;
395 /* count the dumped inodes map on each additional tape */
396 tapesize += (etapes - 1) *
397 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
398 tapesize += etapes + 10; /* headers + 10 trailer blks */
399 msg("estimated %ld tape blocks on %3.2f tape(s).\n",
400 tapesize, fetapes);
401 }
402
403 /*
404 * Allocate tape buffer.
405 */
406 if (!alloctape())
407 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
408
409 startnewtape(1);
410 (void)time((time_t *)&(tstart_writing));
411 dumpmap(usedinomap, TS_CLRI, maxino - 1);
412
413 msg("dumping (Pass III) [directories]\n");
414 dirty = 0; /* XXX just to get gcc to shut up */
415 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
416 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
417 dirty = *map++;
418 else
419 dirty >>= 1;
420 if ((dirty & 1) == 0)
421 continue;
422 /*
423 * Skip directory inodes deleted and maybe reallocated
424 */
425 dp = getino(ino);
426 if ((dp->di_mode & IFMT) != IFDIR)
427 continue;
428 (void)dumpino(dp, ino);
429 }
430
431 msg("dumping (Pass IV) [regular files]\n");
432 for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
433 int mode;
434
435 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
436 dirty = *map++;
437 else
438 dirty >>= 1;
439 if ((dirty & 1) == 0)
440 continue;
441 /*
442 * Skip inodes deleted and reallocated as directories.
443 */
444 dp = getino(ino);
445 mode = dp->di_mode & IFMT;
446 if (mode == IFDIR)
447 continue;
448 (void)dumpino(dp, ino);
449 }
450
451 (void)time((time_t *)&(tend_writing));
443 spcl.c_type = TS_END;
444 for (i = 0; i < ntrec; i++)
445 writeheader(maxino - 1);
446 if (pipeout)
452 spcl.c_type = TS_END;
453 for (i = 0; i < ntrec; i++)
454 writeheader(maxino - 1);
455 if (pipeout)
447 msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
456 msg("%ld tape blocks\n", spcl.c_tapea);
448 else
457 else
449 msg("DUMP: %ld tape blocks on %d volumes(s)\n",
458 msg("%ld tape blocks on %d volumes(s)\n",
450 spcl.c_tapea, spcl.c_volume);
459 spcl.c_tapea, spcl.c_volume);
460
461 /* report dump performance, avoid division through zero */
462 if (tend_writing - tstart_writing == 0)
463 msg("finished in less than a second\n");
464 else
465 msg("finished in %d seconds, throughput %d KBytes/sec\n",
466 tend_writing - tstart_writing,
467 spcl.c_tapea / (tend_writing - tstart_writing));
468
451 putdumptime();
452 trewind();
453 broadcast("DUMP IS DONE!\7\7\n");
454 msg("DUMP IS DONE\n");
455 Exit(X_FINOK);
456 /* NOTREACHED */
457}
458
459/*
460 * Pick up a numeric argument. It must be nonnegative and in the given
461 * range (except that a vmax of 0 means unlimited).
462 */
463static long
464numarg(letter, meaning, vmin, vmax, pargc, pargv)
465 int letter;
466 char *meaning;
467 long vmin, vmax;
468 int *pargc;
469 char ***pargv;
470{
471 register char *p;
472 long val;
473 char *str;
474
475 if (--*pargc < 0)
476 missingarg(letter, meaning);
477 str = *(*pargv)++;
478 for (p = str; *p; p++)
479 if (!isdigit(*p))
480 goto bad;
481 val = atol(str);
482 if (val < vmin || (vmax && val > vmax))
483 goto bad;
484 return (val);
485
486bad:
487 (void)fprintf(stderr, "bad '%c' (%s) value \"%s\"\n",
488 letter, meaning, str);
489 exit(X_ABORT);
490}
491
492static __dead void
493missingarg(letter, meaning)
494 int letter;
495 char *meaning;
496{
497
498 (void)fprintf(stderr, "The '%c' flag (%s) requires an argument\n",
499 letter, meaning);
500 exit(X_ABORT);
501}
502
503void
504sig(signo)
505 int signo;
506{
507 switch(signo) {
508 case SIGALRM:
509 case SIGBUS:
510 case SIGFPE:
511 case SIGHUP:
512 case SIGTERM:
513 case SIGTRAP:
514 if (pipeout)
515 quit("Signal on pipe: cannot recover\n");
516 msg("Rewriting attempted as response to unknown signal.\n");
517 (void)fflush(stderr);
518 (void)fflush(stdout);
519 close_rewind();
520 exit(X_REWRITE);
521 /* NOTREACHED */
522 case SIGSEGV:
523 msg("SIGSEGV: ABORTING!\n");
524 (void)signal(SIGSEGV, SIG_DFL);
525 (void)kill(0, SIGSEGV);
526 /* NOTREACHED */
527 }
528}
529
530char *
531rawname(cp)
532 char *cp;
533{
534 static char rawbuf[MAXPATHLEN];
535 char *dp = rindex(cp, '/');
536
537 if (dp == NULL)
538 return (NULL);
539 *dp = '\0';
540 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1);
541 *dp = '/';
542 (void)strncat(rawbuf, "/r", MAXPATHLEN-1 - strlen(rawbuf));
543 (void)strncat(rawbuf, dp + 1, MAXPATHLEN-1 - strlen(rawbuf));
544 return (rawbuf);
545}
546
547#ifdef sunos
548const char *
549strerror(errnum)
550 int errnum;
551{
552 extern int sys_nerr;
553 extern const char *const sys_errlist[];
554
555 if (errnum < sys_nerr)
556 return (sys_errlist[errnum]);
557 else
558 return ("bogus errno in strerror");
559}
560#endif
469 putdumptime();
470 trewind();
471 broadcast("DUMP IS DONE!\7\7\n");
472 msg("DUMP IS DONE\n");
473 Exit(X_FINOK);
474 /* NOTREACHED */
475}
476
477/*
478 * Pick up a numeric argument. It must be nonnegative and in the given
479 * range (except that a vmax of 0 means unlimited).
480 */
481static long
482numarg(letter, meaning, vmin, vmax, pargc, pargv)
483 int letter;
484 char *meaning;
485 long vmin, vmax;
486 int *pargc;
487 char ***pargv;
488{
489 register char *p;
490 long val;
491 char *str;
492
493 if (--*pargc < 0)
494 missingarg(letter, meaning);
495 str = *(*pargv)++;
496 for (p = str; *p; p++)
497 if (!isdigit(*p))
498 goto bad;
499 val = atol(str);
500 if (val < vmin || (vmax && val > vmax))
501 goto bad;
502 return (val);
503
504bad:
505 (void)fprintf(stderr, "bad '%c' (%s) value \"%s\"\n",
506 letter, meaning, str);
507 exit(X_ABORT);
508}
509
510static __dead void
511missingarg(letter, meaning)
512 int letter;
513 char *meaning;
514{
515
516 (void)fprintf(stderr, "The '%c' flag (%s) requires an argument\n",
517 letter, meaning);
518 exit(X_ABORT);
519}
520
521void
522sig(signo)
523 int signo;
524{
525 switch(signo) {
526 case SIGALRM:
527 case SIGBUS:
528 case SIGFPE:
529 case SIGHUP:
530 case SIGTERM:
531 case SIGTRAP:
532 if (pipeout)
533 quit("Signal on pipe: cannot recover\n");
534 msg("Rewriting attempted as response to unknown signal.\n");
535 (void)fflush(stderr);
536 (void)fflush(stdout);
537 close_rewind();
538 exit(X_REWRITE);
539 /* NOTREACHED */
540 case SIGSEGV:
541 msg("SIGSEGV: ABORTING!\n");
542 (void)signal(SIGSEGV, SIG_DFL);
543 (void)kill(0, SIGSEGV);
544 /* NOTREACHED */
545 }
546}
547
548char *
549rawname(cp)
550 char *cp;
551{
552 static char rawbuf[MAXPATHLEN];
553 char *dp = rindex(cp, '/');
554
555 if (dp == NULL)
556 return (NULL);
557 *dp = '\0';
558 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1);
559 *dp = '/';
560 (void)strncat(rawbuf, "/r", MAXPATHLEN-1 - strlen(rawbuf));
561 (void)strncat(rawbuf, dp + 1, MAXPATHLEN-1 - strlen(rawbuf));
562 return (rawbuf);
563}
564
565#ifdef sunos
566const char *
567strerror(errnum)
568 int errnum;
569{
570 extern int sys_nerr;
571 extern const char *const sys_errlist[];
572
573 if (errnum < sys_nerr)
574 return (sys_errlist[errnum]);
575 else
576 return ("bogus errno in strerror");
577}
578#endif