xref: /titanic_50/usr/src/cmd/fs.d/ufs/volcopy/volcopy.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 #include <sys/param.h>
42 #include <signal.h>
43 #include <sys/types.h>
44 #include <sys/sysmacros.h>
45 #include <sys/vnode.h>
46 #include <sys/fs/ufs_fsdir.h>
47 #include <sys/fs/ufs_inode.h>
48 #include <sys/fs/ufs_fs.h>
49 #include <sys/stat.h>
50 #include <sys/statvfs.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <errno.h>
56 #include <signal.h>
57 #include <stdarg.h>
58 #include <sys/errno.h>
59 #include <sys/utsname.h>
60 #include <sys/ipc.h>
61 #include <sys/sem.h>
62 #include <sys/shm.h>
63 #include <archives.h>
64 #include "volcopy.h"
65 
66 #include <locale.h>
67 
68 /*
69  * main I/O information structure, contains information for the
70  * source and destination files.
71  */
72 
73 struct file_info {
74 	char	*f_dev_p,	/* name of device */
75 		*f_vol_p;	/* volume name */
76 	int	f_bsize,	/* size to buffer I/O to */
77 		f_des,		/* file descriptor */
78 		f_dev;		/* device type (generic I/O library) */
79 } In, Out;
80 
81 
82 int	Sem_id[BUFCNT],	/* semaphore ids for controlling shared memory */
83 	Shm_id[BUFCNT],	/* shared memory identifier */
84 	*Cnts[BUFCNT];	/* an array of byte counts for shared memory */
85 
86 char	Empty[BLKSIZ],	/* empty memory used to clear sections of memory */
87 	*Buf[BUFCNT];	/* buffer pointers (possibly to shared memory) */
88 
89 struct sembuf	Sem_buf,	/* semaphore operation buffer */
90 		Rstsem_buf;	/* semaphore reset operation buffer */
91 
92 typedef union {
93 	char		dummy[SBSIZE];
94 	struct fs	sblk;
95 
96 } sb_un;
97 
98 sb_un	isup, osup, tsup;
99 
100 #define	Isup isup.sblk
101 #define	Osup osup.sblk
102 
103 struct fs *Sptr = (struct fs *)&tsup.sblk;	/* super-block pointer */
104 
105 char    *Ifname, *Ifpack, *Ofname, *Ofpack;
106 struct volcopy_label	V_labl;
107 
108 char	From_vol[VVOLLEN + 1],
109 	To_vol[VVOLLEN + 1],
110 	*Fsys_p;
111 
112 static	char	*nolabel = "";  /* used when there is no room for label */
113 
114 int	Blk_cnt = 1,	/* Do I/O in (Blk_cnt * BLKSIZ) byte blocks */
115 	Blocks = 0,	/* Number of blocks transferred */
116 	Bpi = 0,
117 	Bufcnt,
118 	Bufflg = 0,
119 	Bufsz = BLKSIZ,
120 	Disk_cnt = 1,	/* Disk I/O (Disk_cnt * Blk_cnt * BLKSIZ) byte blocks */
121 	Drive_typ = 0,	/* Flag for special tape drive types */
122 	Eomflg = 0,
123 	Ipc = 0,
124 	Itape,
125 	M3b15 = 0,	/* Machine types, set to 1 for the machine */
126 	M3b2 = 0,	/* the command is executing on */
127 	Otape,
128 	Pid = -1,
129 	R_blks = 0,	/* Number of blocks per tape reel */
130 	R_cur = 1,	/* Current tape reel being processed */
131 	R_len = 0,	/* Length in feet of tape reels */
132 	R_num = 0,	/* Number of tape reels to be processed */
133 	Shell_esc = 1,	/* Allow shell after delete -nosh (3b15) can disable */
134 	Yesflg = 0;
135 
136 void (*singal())();
137 long	Fs,
138 	Fstype;
139 
140 time_t	Tvec;
141 
142 FILE	*Devtty;
143 
144 static void 	getinfs(),
145 		getoutfs();
146 
147 static char 	*getfslabel(struct fs *);
148 static char 	*getvolabel(struct fs *);
149 static void	prompt(int, const char *, ...);
150 static void	perr(int, const char *, ...);
151 static void 	mklabel(void);
152 static void	get_mach_type(void);
153 static void 	mem_setup(void);
154 static void	rprt(void);
155 static void 	chgreel(struct file_info *, int);
156 static void	parent_copy(void);
157 static void 	copy(void);
158 static void 	flush_bufs(int);
159 static void 	cleanup(void);
160 
161 #ifdef LOG
162 static int 	fslog(void);
163 #endif	/* LOG */
164 
165 /*
166  * g_init(), g_read(), g_write() originally came from libgenIO,
167  * a totally obsolete device interface library, now deleted.
168  * volcopy should be deleted too, since it doesn't work.
169  */
170 
171 #define	G_TM_TAPE	1	/* Tapemaster controller    */
172 #define	G_XY_DISK	3	/* xy disks		*/
173 #define	G_SD_DISK	7	/* scsi sd disk		*/
174 #define	G_XT_TAPE	8	/* xt tapes		*/
175 #define	G_SF_FLOPPY	9	/* sf floppy		*/
176 #define	G_XD_DISK	10	/* xd disks		*/
177 #define	G_ST_TAPE	11	/* scsi tape		*/
178 #define	G_NS		12	/* noswap pseudo-dev	*/
179 #define	G_RAM		13	/* ram pseudo-dev	*/
180 #define	G_FT		14	/* tftp			*/
181 #define	G_HD		15	/* 386 network disk	*/
182 #define	G_FD		16	/* 386 AT disk		*/
183 #define	G_FILE		28	/* file, not a device	*/
184 #define	G_NO_DEV	29	/* device does not require special treatment */
185 #define	G_DEV_MAX	30	/* last valid device type */
186 
187 /*
188  * g_init: Determine the device being accessed, set the buffer size,
189  * and perform any device specific initialization. Since at this point
190  * Sun has no system call to read the configuration, the major numbers
191  * are assumed to be static and types are figured out as such. However,
192  * as a rough estimate, the buffer size for all types is set to 512
193  * as a default.
194  */
195 static int
196 g_init(int *devtype, int *fdes)
197 {
198 	major_t maj;
199 	int bufsize;
200 	struct stat64 st_buf;
201 	struct statvfs64 stfs_buf;
202 
203 	*devtype = G_NO_DEV;
204 	if (fstat64(*fdes, &st_buf) == -1)
205 		return (-1);
206 	if (!S_ISCHR(st_buf.st_mode) && !S_ISBLK(st_buf.st_mode)) {
207 		if (S_ISFIFO(st_buf.st_mode))
208 			bufsize = 512;
209 		else {
210 			/* find block size for this file system */
211 			*devtype = G_FILE;
212 			if (fstatvfs64(*fdes, &stfs_buf) < 0) {
213 				bufsize = -1;
214 				errno = ENODEV;
215 			} else {
216 				bufsize = stfs_buf.f_bsize;
217 			}
218 		}
219 		return (bufsize);
220 	}
221 
222 	return (512);
223 }
224 
225 /*
226  * g_read: Read nbytes of data from fdes (of type devtype) and place
227  * data in location pointed to by buf.  In case of end of medium,
228  * translate (where necessary) device specific EOM indications into
229  * the generic EOM indication of rv = -1, errno = ENOSPC.
230  */
231 /* ARGSUSED */
232 static ssize_t
233 g_read(int devtype, int fdes, void *buf, size_t nbytes)
234 {
235 	ssize_t rv;
236 
237 	rv = read(fdes, buf, nbytes);
238 
239 	/* st devices return 0 when no space left */
240 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
241 		errno = ENOSPC;
242 		rv = -1;
243 	}
244 
245 	return (rv);
246 }
247 
248 /*
249  * g_write: Write nbytes of data to fdes (of type devtype) from
250  * the location pointed to by buf.  In case of end of medium,
251  * translate (where necessary) device specific EOM indications into
252  * the generic EOM indication of rv = -1, errno = ENOSPC.
253  */
254 /* ARGSUSED */
255 static ssize_t
256 g_write(int devtype, int fdes, void *buf, size_t nbytes)
257 {
258 	ssize_t rv;
259 
260 	rv = write(fdes, buf, nbytes);
261 
262 	/* st devices return 0 when no more space left */
263 	if ((rv == 0 && errno == 0) || (rv == -1 && errno == EIO)) {
264 		errno = ENOSPC;
265 		rv = -1;
266 	}
267 
268 	return (rv);
269 }
270 
271 /*
272  * filesystem copy with propagation of volume ID and filesystem name:
273  *
274  * volcopy [-options]  filesystem /dev/from From_vol /dev/to To_vol
275  *
276  * options are:
277  * 	-feet - length of tape
278  * 	-bpi  - recording density
279  * 	-reel - reel number (if not starting from beginning)
280  * 	-buf  - use double buffered i/o (if dens >= 1600 bpi)
281  *	-block - Set the transfer block size to NUM physical blocks (512
282  *	bytes on 3B2 and 3B15).  Note that an arbitrary block size might
283  *	or might not work on a given system.  Also, the block size
284  *	read from the header of an input tape silently overrides this.
285  *	-nosh - Don't offer the user a shell after hitting break or delete.
286  *	-r - Read NUM transfer blocks from the disk at once and write it
287  *	to the output device one block at a time.  Intended only to
288  *	boost the 3B15 EDFC disk to tape performance.  Disabled on 3B2.
289  * 	-a    - ask "y or n" instead of "DEL if wrong"
290  * 	-s    - inverse of -a, from/to devices are printed followed by `?'.
291  * 		User has 10 seconds to DEL if mistaken!
292  * 	-y    - assume "yes" response to all questions
293  *
294  * Examples:
295  *
296  * volcopy root /dev/rdsk/0s2 pk5 /dev/rdsk/1s2 pk12
297  *
298  * volcopy u3 /dev/rdsk/1s5 pk1 /dev/rmt/0m tp123
299  *
300  * volcopy u5 /dev/rmt/0m -  /dev/rdsk/1s5 -
301  */
302 
303 int
304 main(int argc, char *argv[])
305 {
306 	char c;
307 	int lfdes, altflg = 0, result, verify;
308 	long dist;
309 	char *align();
310 	void sigalrm(), sigint();
311 	struct stat stbuf;
312 	int cnt;
313 
314 	(void) setlocale(LC_ALL, "");
315 #if !defined(TEXT_DOMAIN)
316 #define	TEXT_DOMAIN "SYS_TEST"
317 #endif
318 	(void) textdomain(TEXT_DOMAIN);
319 
320 	(void) get_mach_type();
321 	(void) signal(SIGINT, sigint);
322 	(void) signal(SIGALRM, sigalrm);
323 	In.f_bsize = Out.f_bsize = BLKSIZ;
324 	while (argc > 1 && argv[1][0] == '-') {
325 		if (EQ(argv[1], "-a", 2)) {
326 			altflg |= MINUSA;
327 		} else if (EQ(argv[1], "-e", 2)) {
328 			Eomflg = 1;
329 		} else if (EQ(argv[1], "-s", 2)) {
330 			altflg |= MINUSS;
331 		} else if (EQ(argv[1], "-y", 2)) {
332 			Yesflg++;
333 		} else if (EQ(argv[1], "-buf", 4)) {
334 			Bufflg++;
335 		} else if (EQ(argv[1], "-bpi", 4)) {
336 			if ((c = argv[1][4]) >= '0' && c <= '9')
337 				Bpi = getbpi(&argv[1][4]);
338 			else {
339 				++argv;
340 				--argc;
341 				Bpi = getbpi(&argv[1][0]);
342 			}
343 		} else if (EQ(argv[1], "-feet", 5)) {
344 			if ((c = argv[1][5]) >= '0' && c <= '9')
345 				R_len = atoi(&argv[1][5]);
346 			else {
347 				++argv;
348 				--argc;
349 				R_len = atoi(&argv[1][0]);
350 			}
351 		} else if (EQ(argv[1], "-reel", 5)) {
352 			if ((c = argv[1][5]) >= '0' && c <= '9')
353 				R_cur = atoi(&argv[1][5]);
354 			else {
355 				++argv;
356 				--argc;
357 				R_cur = atoi(&argv[1][0]);
358 			}
359 		} else if (EQ(argv[1], "-r", 2)) { /* 3b15 only */
360 			if ((c = argv[1][2]) >= '0' && c <= '9')
361 				Disk_cnt = atoi(&argv[1][2]);
362 			else {
363 				++argv;
364 				--argc;
365 				Disk_cnt = atoi(&argv[1][0]);
366 			}
367 			if (Disk_cnt == 0)
368 		perr(1, "volcopy: Need a non-zero value for the -r option\n");
369 		} else if (EQ(argv[1], "-block", 6)) { /* 3b15 only */
370 			if ((c = argv[1][6]) >= '0' && c <= '9')
371 				Blk_cnt = atoi(&argv[1][6]);
372 			else {
373 				++argv;
374 				--argc;
375 				Blk_cnt = atoi(&argv[1][0]);
376 			}
377 			if (Blk_cnt == 0)
378 	perr(1, "volcopy: Need a non-zero value for the -block option\n");
379 		} else if (EQ(argv[1], "-nosh", 5)) { /* 3b15 only */
380 			Shell_esc = 0;
381 		} else
382 			perr(1, "<%s> invalid option\n", argv[1]);
383 		++argv;
384 		--argc;
385 	} /* argv[1][0] == '-' */
386 
387 	Devtty = fopen("/dev/tty", "r");
388 	if ((Devtty == NULL) && !isatty(0))
389 		Devtty = stdin;
390 	time(&Tvec);
391 
392 	if (Eomflg && R_len)
393 		perr(9, "volcopy: -e and -feet are mutually exclusive\n");
394 	if ((altflg & MINUSA) && (altflg & MINUSS))
395 		perr(9, "volcopy: -a and -s are mutually exclusive\n");
396 	if (argc != 6) /* if mandatory fields not present */
397 		perr(9, "ufs usage: volcopy [-F ufs] [generic options] \
398 fsname /devfrom volfrom /devto volto\n");
399 	if (!(altflg & MINUSA)) /* -a was not specified, use default (-s) */
400 		altflg |= MINUSS;
401 
402 	In.f_dev_p = argv[DEV_IN];
403 	Out.f_dev_p = argv[DEV_OUT];
404 	strncpy(To_vol, argv[VOL_OUT], VVOLLEN);
405 	To_vol[VVOLLEN] = '\0';
406 	Out.f_vol_p = &To_vol[0];
407 	strncpy(From_vol, argv[VOL_IN], VVOLLEN);
408 	From_vol[VVOLLEN] = '\0';
409 	In.f_vol_p = &From_vol[0];
410 	Fsys_p = argv[FIL_SYS];
411 
412 	if ((In.f_des = open(In.f_dev_p, O_RDONLY)) < 1)
413 		perr(10, "%s: cannot open\n", In.f_dev_p);
414 	if ((Out.f_des = open(Out.f_dev_p, O_RDONLY)) < 1)
415 		perr(10, "%s: cannot open\n", Out.f_dev_p);
416 
417 	if (fstat(In.f_des, &stbuf) < 0 || (stbuf.st_mode & S_IFMT) != S_IFCHR)
418 		perr(10, "From device not character-special\n");
419 	if (fstat(Out.f_des, &stbuf) < 0 || (stbuf.st_mode & S_IFMT) != S_IFCHR)
420 		perr(10, "To device not character-special\n");
421 
422 	if ((Itape = tapeck(&In, INPUT)) == 1)
423 		R_blks = V_labl.v_reelblks;
424 	Otape = tapeck(&Out, OUTPUT);
425 	if (Otape && Itape)
426 		perr(10, "Use dd(1) command to copy tapes\n");
427 	(void) mem_setup();
428 	if (Bufflg && !Ipc)
429 		perr(1, "The -buf option requires ipc\n");
430 	if (!Itape && !Otape)
431 		R_cur = 1;
432 	if (R_cur == 1 || !Itape) {
433 		/* read in superblock */
434 		verify = 0;
435 		(void) getinfs(In.f_dev, In.f_des, Sptr);
436 
437 		if ((Sptr->fs_magic != FS_MAGIC) &&
438 		    (Sptr->fs_magic != MTB_UFS_MAGIC))
439 			perr(10, "File System type unknown--get help\n");
440 
441 		if (Sptr->fs_magic == FS_MAGIC &&
442 		    (Sptr->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
443 		    Sptr->fs_version != UFS_VERSION_MIN))
444 			perr(10, "Unrecognized version of UFS--get help\n");
445 
446 		if (Sptr->fs_magic == MTB_UFS_MAGIC &&
447 		    (Sptr->fs_version > MTB_UFS_VERSION_1 ||
448 		    Sptr->fs_version < MTB_UFS_VERSION_MIN))
449 			perr(10, "Unrecognized version of UFS--get help\n");
450 
451 		(void) memcpy(&Isup, Sptr, Sptr->fs_sbsize);
452 		Ifname = getfslabel(&Isup);
453 		Ifpack = getvolabel(&Isup);
454 		Fs = Sptr->fs_size * Sptr->fs_nspf;
455 	}  /* R_cur == 1 || !Itape */
456 
457 	/* read in superblock */
458 	verify = !Otape || (altflg & MINUSS);
459 	(void) getoutfs(Out.f_dev, Out.f_des, Sptr, verify);
460 
461 	if ((Sptr->fs_magic == FS_MAGIC) || (Sptr->fs_magic == MTB_UFS_MAGIC)) {
462 		(void) memcpy(&Osup, Sptr, Sptr->fs_sbsize);
463 		Ofname = getfslabel(&Osup);
464 		Ofpack = getvolabel(&Osup);
465 	} else  {
466 		int i;
467 
468 		/* out vol does not contain a ufs file system */
469 		/* stuff let over from Isup */
470 		(void) memcpy(&Osup, &Isup, Isup.fs_sbsize);
471 		Ofname = getfslabel(&Osup);
472 		Ofpack = getvolabel(&Osup);
473 		/* wipe out the fs name and pack name for warning purposes */
474 		for (i = 0; i < 6; i++) Ofname[i] = ' ';
475 		for (i = 0; i < 6; i++) Ofpack[i] = ' ';
476 	}
477 
478 	if (Itape) {
479 		if (R_cur != 1) {
480 			(void) printf(gettext(
481 				"\nvolcopy: IF REEL 1 HAS NOT BEEN RESTORED,"));
482 			(void) printf(gettext(
483 				" STOP NOW AND START OVER ***\07\n"));
484 			if (!ask(" Continue? ")) {
485 				cleanup();
486 				exit(31+9);
487 			}
488 			strncpy(Ifname, Fsys_p, 6);
489 			strncpy(Ifpack, In.f_vol_p, 6);
490 		}
491 		if (V_labl.v_reel != R_cur || V_labl.v_reels != R_num)
492 	prompt(1, "Tape disagrees: Reel %d of %d : looking for %d of %d\n",
493 				V_labl.v_reel, V_labl.v_reels, R_cur, R_num);
494 	} else if (Otape) {
495 		strncpy(V_labl.v_volume, Out.f_vol_p, 6);
496 		strncpy(Ofpack, Out.f_vol_p, 6);
497 		strncpy(Ofname, Fsys_p, 6);
498 		if (!Eomflg) {
499 			R_num = Fs / R_blks + ((Fs % R_blks) ? 1 : 0);
500 			(void) printf(gettext(
501 				"You will need %d reels.\n"), R_num);
502 			(void) printf(gettext(
503 		"(\tThe same size and density is expected for all reels)\n"));
504 		}
505 	}
506 	if (NOT_EQ(Fsys_p, Ifname, 6)) {
507 		verify = !Otape || (altflg & MINUSS);
508 		prompt(verify,
509 			"arg. (%.6s) doesn't agree with from fs. (%.6s)\n",
510 			Fsys_p, Ifname);
511 	}
512 	if (NOT_EQ(In.f_vol_p, "-", 6) && NOT_EQ(In.f_vol_p, Ifpack, 6)) {
513 		verify = !Otape || (altflg & MINUSS);
514 	prompt(verify, "arg. (%.6s) doesn't agree with from vol.(%.6s)\n",
515 			In.f_vol_p, Ifpack);
516 	}
517 
518 	if (*In.f_vol_p == '-')
519 		In.f_vol_p = Ifpack;
520 	if (*Out.f_vol_p == '-')
521 		Out.f_vol_p = Ofpack;
522 
523 	if (R_cur == 1 && (Osup.fs_time + _2_DAYS) > Isup.fs_time) {
524 		time_t t;
525 
526 		verify = altflg & MINUSS;
527 		t = (time_t)Osup.fs_time;
528 		prompt(verify, "%s less than 48 hours older than %s\n"
529 		    "To filesystem dated:  %s",
530 		    Out.f_dev_p, In.f_dev_p, ctime(&t));
531 	}
532 	if (NOT_EQ(Out.f_vol_p, Ofpack, 6)) {
533 		prompt(1, "arg.(%.6s) doesn't agree with to vol.(%.6s)\n",
534 			Out.f_vol_p, Ofpack);
535 		strncpy(Ofpack, Out.f_vol_p, 6);
536 	}
537 	if (Isup.fs_size > Osup.fs_size && !Otape)
538 		prompt(1, "from fs larger than to fs\n");
539 	if (!Otape && NOT_EQ(Ifname, Ofname, 6)) {
540 		verify = altflg & MINUSS;
541 	prompt(verify, "warning! from fs(%.6s) differs from to fs(%.6s)\n",
542 			Ifname, Ofname);
543 	}
544 
545 	(void) printf(gettext("From: %s, to: %s? "), In.f_dev_p, Out.f_dev_p);
546 	if (!(altflg & MINUSA)) {
547 		(void) printf(gettext("(DEL if wrong)\n"));
548 		sleep(10);
549 	} else if (!ask("(y or n) "))
550 		perr(10, "\nvolcopy: STOP\n");
551 	close(In.f_des);
552 	close(Out.f_des);
553 	sync();
554 	In.f_des = open(In.f_dev_p, O_RDONLY);
555 	Out.f_des = open(Out.f_dev_p, O_WRONLY);
556 	errno = 0;
557 	if (g_init(&In.f_dev, &In.f_des) < 0 ||
558 	    g_init(&Out.f_dev, &Out.f_des) < 0)
559 		perr(1, "volcopy: Error %d during initialization\n", errno);
560 	if (Itape) {
561 		errno = 0;
562 		if (g_read(In.f_dev, In.f_des, &V_labl, sizeof (V_labl)) <
563 		    sizeof (V_labl))
564 			perr(10, "Error while reading label\n");
565 	} else if (Otape) {
566 		V_labl.v_reels = R_num;
567 		V_labl.v_reel = R_cur;
568 		V_labl.v_time = Tvec;
569 		V_labl.v_reelblks = R_blks;
570 		V_labl.v_blksize = BLKSIZ * Blk_cnt;
571 		V_labl.v_nblocks = Blk_cnt;
572 		V_labl.v_offset = 0L;
573 		V_labl.v_type = T_TYPE;
574 		errno = 0;
575 		if (g_write(Out.f_dev, Out.f_des, &V_labl, sizeof (V_labl)) <
576 		    sizeof (V_labl))
577 			perr(10, "Error while writing label\n");
578 	}
579 	if (R_cur > 1) {
580 		if (!Eomflg) {
581 			Fs = (R_cur - 1) * actual_blocks();
582 			lfdes = Otape ? In.f_des : Out.f_des;
583 			dist = (long)(Fs * BLKSIZ);
584 		} else { /* Eomflg */
585 			if (Otape)
586 		perr(1, "Cannot use -reel with -e when copying to tape\n");
587 			lfdes = Out.f_des;
588 			dist = (long)(V_labl.v_offset * BLKSIZ);
589 			Fs = V_labl.v_offset;
590 		}
591 		if (lseek(lfdes, dist, 0) < 0)
592 			perr(1, "Cannot lseek()\n");
593 		Sptr = Otape ? &Isup : &Osup;
594 		if ((Sptr -> fs_magic != FS_MAGIC) &&
595 		    (Sptr -> fs_magic != MTB_UFS_MAGIC))
596 			perr(10, "File System type unknown--get help!\n");
597 
598 		if (Sptr->fs_magic == FS_MAGIC &&
599 		    (Sptr->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
600 		    Sptr->fs_version != UFS_VERSION_MIN))
601 			perr(10, "Unrecognized version of UFS--get help\n");
602 
603 		if (Sptr->fs_magic == MTB_UFS_MAGIC &&
604 		    (Sptr->fs_version > MTB_UFS_VERSION_1 ||
605 		    Sptr->fs_version < MTB_UFS_VERSION_MIN))
606 			perr(10, "Unrecognized version of UFS--get help\n");
607 
608 		Fs = (Sptr->fs_size * Sptr->fs_nspf) - Fs;
609 	}
610 	if (Itape || Otape)
611 		rprt();
612 
613 	if (Ipc) {
614 		parent_copy();
615 		(void) cleanup();
616 	} else
617 		copy();
618 	(void) printf(gettext("  END: %ld blocks.\n"), Blocks);
619 
620 #ifdef LOG
621 	fslog();
622 #endif
623 	if (Blocks)
624 		return (0);
625 	return (31+1);		/* failed.. 0 blocks */
626 }
627 
628 /*
629  * sigalrm: catch alarm signals.
630  */
631 
632 void
633 sigalrm()
634 {
635 	void (*signal())();
636 
637 	(void) signal(SIGALRM, sigalrm);
638 }
639 
640 /*
641  * sigsys: catch illegal system calls to determine if IPC is available.
642  */
643 
644 void
645 sigsys()
646 {
647 
648 	Ipc = 0;
649 }
650 
651 /*
652  * sigint: catch interrupts and prompt user for shell or to quit.
653  */
654 
655 void
656 sigint()
657 {
658 	void (*signal())();
659 	extern char **environ;
660 	int tmpflg, i = 0, ps1 = -1, ps2 = -1;
661 
662 	tmpflg = Yesflg; /* override yesflag for duration of interrupt */
663 	Yesflg = 0;
664 	if (Shell_esc && ask("Want Shell?   ")) {
665 		if (!fork()) {
666 			/* both PS1 and PS2 must be exported */
667 			while (environ[i]) {
668 				if (EQ(environ[i], "PS1", 3))
669 					ps1 = i;
670 				if (EQ(environ[i], "PS2", 3))
671 					ps2 = i;
672 				i++;
673 			}
674 			if (ps1 >= 0 && ps2 >= 0)
675 				environ[ps1] = environ[ps2];
676 			(void) signal(SIGINT, SIG_DFL);
677 			execl("/usr/bin/sh", "/usr/bin/sh", 0);
678 		} else { /* parent */
679 			(void) signal(SIGINT, SIG_IGN);
680 			wait((int *)0);
681 		}
682 	} else if (ask("Want to quit?    ")) {
683 		if (Pid > 0)
684 			kill(Pid, 9);
685 		(void) cleanup(); /* ipc */
686 		exit(31+2);
687 	}
688 	(void) signal(SIGINT, sigint);
689 	Yesflg = tmpflg; /* reset Yesflg */
690 }
691 
692 /*
693  * actual_blocks: Calculate the actual number of blocks written to
694  * the tape (will differ from V_labl.v_reelblks if v_reelblks is not
695  * an even multiple of the blocking factor Blk_cnt).
696  */
697 
698 int
699 actual_blocks()
700 {
701 
702 	if (R_blks % Blk_cnt)
703 		return (((R_blks / Blk_cnt) + 1) * Blk_cnt);
704 	else
705 		return (R_blks);
706 }
707 
708 /*
709  * get_mach_type: Determine what machine this is executing on.
710  */
711 
712 static void
713 get_mach_type(void)
714 {
715 	struct utsname utsinfo;
716 
717 	errno = 0;
718 	if (uname(&utsinfo) < 0)
719 		perr(1, "Unable to determine machine type\n");
720 	if (strcmp(utsinfo.machine, "3B2") == 0)
721 		M3b2 = 1;
722 	else if (strcmp(utsinfo.machine, "3B15") == 0)
723 		M3b15 = 1;
724 }
725 
726 /*
727  * mem_setup: Determine memory needs and check for IPC.  If IPC is available,
728  * used shared memory and semaphores to increase performance.  If no IPC,
729  * get normal memory and only use one process.
730  */
731 
732 static void
733 mem_setup(void)
734 {
735 	void (*signal())();
736 	int cnt, num, size;
737 	char *align();
738 
739 	union semun {
740 		int val;
741 		struct semid_ds *buf;
742 		ushort_t *array;
743 	} sem_arg;
744 
745 	if (Blk_cnt == 1) {
746 		switch (Drive_typ) {
747 		case A_DRIVE:
748 			Blk_cnt = 32;
749 			break;
750 		case C_DRIVE:
751 			Blk_cnt = 10;
752 			break;
753 		case K_DRIVE:
754 			Blk_cnt = 4;
755 			break;
756 		case T_DRIVE:
757 			if (Bpi == 6250)
758 				Blk_cnt = 50;
759 			else
760 				Blk_cnt = 10;
761 			break;
762 		default:
763 			if (M3b15) {
764 				if (Itape || Otape)
765 					Blk_cnt = 16;
766 			} else {
767 				if (Otape || Itape) {
768 					if (Bpi == 6250)
769 						Blk_cnt = 50;
770 					else
771 						Blk_cnt = 10;
772 				}
773 			}
774 			break;
775 		} /* Drive_typ */
776 	} /* Blk_cnt == 1 */
777 	if (Blk_cnt > 1) /* user overrode g_init */
778 		In.f_bsize = Out.f_bsize = Blk_cnt * BLKSIZ;
779 	In.f_bsize = (!Itape) ? Disk_cnt * In.f_bsize : In.f_bsize;
780 	Out.f_bsize = (!Otape) ? Disk_cnt * Out.f_bsize : Out.f_bsize;
781 	Bufsz = find_lcm(In.f_bsize, Out.f_bsize);
782 	num = _128K / (Bufsz + sizeof (int));
783 	Bufsz *= num;
784 	size = Bufsz + sizeof (int);
785 	/* test to see if ipc is available, the shmat should fail with EINVAL */
786 	(void) signal(SIGSYS, sigsys);
787 	errno = 0;
788 	if (Ipc) {
789 		if ((int)shmat(0, (char *)NULL, 0) < 0 && errno != EINVAL)
790 			Ipc = 0; /* something went wrong */
791 	}
792 	if (Ipc) { /* ipc is available */
793 		Bufcnt = 2;
794 		sem_arg.val = 0;
795 		for (cnt = 0; cnt < BUFCNT; cnt++) {
796 			errno = 0;
797 			if ((Sem_id[cnt] = semget(IPC_PRIVATE, 1, 0)) < 0)
798 				perr(1, "Error allocating semaphores: %d",
799 				    errno);
800 			if (semctl(Sem_id[cnt], 0, SETVAL, sem_arg) < 0)
801 				perr(1, "Error setting semaphores: %d", errno);
802 			if ((Shm_id[cnt] = shmget(IPC_PRIVATE, size, 0)) < 0)
803 				perr(1, "Error allocating shared memory: %d",
804 				    errno);
805 			if ((Buf[cnt] = shmat(Shm_id[cnt], 0, 0)) == (void *)-1)
806 				perr(1, "Error attaching shared memory: %d",
807 				    errno);
808 			if (shmctl(Shm_id[cnt], SHM_LOCK, 0) < 0)
809 				perr(0, "Error locking in shared memory: %d",
810 				    errno);
811 			Cnts[cnt] = (int *)(Buf[cnt] + Bufsz);
812 		}
813 	} else { /* ipc is not available */
814 		Bufcnt = 1;
815 		if ((Buf[0] = align(size)) == (char *)NULL)
816 			perr(1, "Out of memory\n");
817 		Cnts[0] = (int *)(Buf[0] + Bufsz);
818 		*Cnts[0] = 0;
819 	}
820 }
821 
822 /*
823  * prompt: Prompt the user for verification.
824  */
825 
826 static void
827 prompt(int verify, const char *fmt, ...)
828 {
829 	va_list ap;
830 
831 	va_start(ap, fmt);
832 	if (fmt != NULL)
833 		(void) vfprintf(stdout, gettext(fmt), ap);
834 	va_end(ap);
835 	if (verify) {
836 		(void) fprintf(stdout, gettext("Type 'y' to override:    "));
837 		if (!ask("")) {
838 			cleanup();
839 			exit(31+9);
840 		}
841 	}
842 }
843 
844 /*
845  * ask: Ask the user a question and get the answer.
846  */
847 
848 int
849 ask(char *s)
850 {
851 	char ans[12];
852 
853 	(void) printf(gettext(s));
854 	if (Yesflg) {
855 		(void) printf(gettext("YES\n"));
856 		return (1);
857 	}
858 	ans[0] = '\0';
859 	fgets(ans, 10, Devtty);
860 	for (;;) {
861 		switch (ans[0]) {
862 			case 'a':
863 			case 'A':
864 				if (Pid > 0) /* parent with a child */
865 					kill(Pid, 9);
866 				cleanup();
867 				exit(31+1);
868 			case 'y':
869 			case 'Y':
870 				return (1);
871 			case 'n':
872 			case 'N':
873 				return (0);
874 			default:
875 				(void) printf(gettext("\n(y or n)?"));
876 				fgets(ans, 10, Devtty);
877 		}
878 	}
879 }
880 
881 /*
882  * align: Align a malloc'd memory section on a page boundry.
883  */
884 
885 char *
886 align(int size)
887 {
888 	int pad;
889 
890 	if ((pad = ((int)malloc(0) & (PAGESIZE-1))) > 0) {
891 		pad = PAGESIZE - pad;
892 		if (malloc(pad) == (char *)NULL)
893 			return ((char *)NULL);
894 	}
895 	return (malloc((unsigned)size));
896 }
897 
898 /*
899  * child_copy:  Using IPC, this child process reads from shared memory
900  * and writes to the destination file system.
901  */
902 
903 int
904 child_copy()
905 {
906 	int rv, cur_buf, left, have, tpcnt;
907 	char *c_p;
908 
909 	(void) signal(SIGINT, SIG_IGN);
910 	Sem_buf.sem_op = -1;
911 	(void) close(In.f_des);
912 	if (Otape && !Eomflg)
913 		tpcnt = actual_blocks() * BLKSIZ;
914 	cur_buf = 0;
915 	for (;;) {
916 		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
917 			perr(1, "semaphore operation error %d\n", errno);
918 		left = *Cnts[cur_buf];
919 		if (!left)
920 			break;
921 		c_p = Buf[cur_buf];
922 		rv = 0;
923 		while (left) {
924 			have = (left < Out.f_bsize) ? left : Out.f_bsize;
925 			if (!Eomflg && Otape) {
926 				if (!tpcnt) {
927 					(void) chgreel(&Out, OUTPUT);
928 					tpcnt = actual_blocks() * BLKSIZ;
929 				}
930 				have = (tpcnt < have) ? tpcnt : have;
931 			}
932 			errno = 0;
933 			if ((rv = g_write(Out.f_dev, Out.f_des, c_p, have)) <
934 			    0) {
935 				if (Eomflg && errno == ENOSPC) {
936 					(void) chgreel(&Out, OUTPUT);
937 					continue;
938 				} else
939 					perr(1, "I/O error %d on write\n",
940 					    errno);
941 			}
942 			left -= rv;
943 			c_p += rv;
944 			V_labl.v_offset += rv;
945 			if (!Eomflg && Otape)
946 				tpcnt -= rv;
947 		}
948 		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
949 			perr(2, "semaphore operation error %d\n", errno);
950 		cur_buf = (cur_buf + 1) % BUFCNT;
951 	}
952 	exit(0);
953 }
954 
955 /*
956  * parent_copy:  Using shared memory, the parent process reads fromt the
957  * source file system and writes to shared memory.
958  */
959 
960 static void
961 parent_copy(void)
962 {
963 	int rv, left, have, tpcnt, cur_buf;
964 	char *c_p;
965 	int eom = 0, xfer_cnt = Fs * BLKSIZ;
966 
967 	Sem_buf.sem_num = 0;
968 	Sem_buf.sem_flg = 0;
969 	if ((Pid = fork()) == 0)
970 		child_copy(); /* child does not return */
971 	(void) close(Out.f_des);
972 	Rstsem_buf.sem_num = 0;
973 	Rstsem_buf.sem_flg = 0;
974 	Rstsem_buf.sem_op = 2;
975 	Sem_buf.sem_op = 0;
976 	cur_buf = 0;
977 	if (Itape && !Eomflg)
978 		tpcnt = actual_blocks() * BLKSIZ;
979 	while (xfer_cnt) {
980 		if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
981 			perr(1, "Semaphore operation error %d\n", errno);
982 		c_p = Buf[cur_buf];
983 		left = Bufsz;
984 		rv = 0;
985 		while (left >= In.f_bsize && xfer_cnt) {
986 			have = (xfer_cnt < In.f_bsize) ? xfer_cnt : In.f_bsize;
987 			if (!Eomflg && Itape) {
988 				if (!tpcnt) {
989 					*Cnts[cur_buf] = Bufsz - left;
990 					(void) flush_bufs(cur_buf);
991 					(void) chgreel(&In, INPUT);
992 					tpcnt = actual_blocks() * BLKSIZ;
993 					cur_buf = (cur_buf == 0) ? 1 : 0;
994 					eom = 1;
995 					break;
996 				}
997 				have = (tpcnt < have) ? tpcnt : have;
998 			}
999 			errno = 0;
1000 			if ((rv = g_read(In.f_dev, In.f_des, c_p, have)) < 0) {
1001 				if (Eomflg && errno == ENOSPC) {
1002 					*Cnts[cur_buf] = Bufsz - left;
1003 					(void) flush_bufs(cur_buf);
1004 					(void) chgreel(&In, INPUT);
1005 					cur_buf = (cur_buf == 0) ? 1 : 0;
1006 					eom = 1;
1007 					break;
1008 				} else
1009 					perr(1, "I/O error %d on read\n",
1010 					    errno);
1011 			}
1012 			left -= rv;
1013 			c_p += rv;
1014 			xfer_cnt -= rv;
1015 			if (!Eomflg && Itape)
1016 				tpcnt -= rv;
1017 		}
1018 		if (eom > 0) {
1019 			eom = 0;
1020 			if (Eomflg)
1021 				xfer_cnt -= rv;
1022 			else if (Itape)
1023 				tpcnt -= rv;
1024 			continue;
1025 		}
1026 		*Cnts[cur_buf] = Bufsz - left;
1027 		Blocks += *Cnts[cur_buf];
1028 		if (semop(Sem_id[cur_buf], &Rstsem_buf, 1) < 0)
1029 			perr(2, "Semaphore operation error %d\n", errno);
1030 		cur_buf = (cur_buf == 0) ? 1 : 0;
1031 	}
1032 	if (semop(Sem_id[cur_buf], &Sem_buf, 1) < 0)
1033 		perr(3, "Semaphore operation error %d\n", errno);
1034 	*Cnts[cur_buf] = 0;
1035 	if (semop(Sem_id[cur_buf], &Rstsem_buf, 1) < 0)
1036 		perr(4, "Semaphore operation error %d\n", errno);
1037 	wait((int *)NULL);
1038 	Blocks /= BLKSIZ;
1039 }
1040 
1041 /*
1042  * copy:  Copy without shared memory.  The process reads from the source
1043  * filesystem and writes to the destination filesystem.
1044  */
1045 
1046 static void
1047 copy(void)
1048 {
1049 	int rv, left, have, tpcnt = 1, xfer_cnt = Fs * BLKSIZ;
1050 	char *c_p;
1051 
1052 	if ((Itape || Otape) && !Eomflg)
1053 		tpcnt = actual_blocks() * BLKSIZ;
1054 	while (xfer_cnt) {
1055 		c_p = (char *)(Buf[0] + *Cnts[0]);
1056 		left = Bufsz - *Cnts[0];
1057 		rv = 0;
1058 		while (left >= In.f_bsize && xfer_cnt) {
1059 			have = (xfer_cnt < In.f_bsize) ? xfer_cnt : In.f_bsize;
1060 			if (!Eomflg && Itape) {
1061 				if (!tpcnt) {
1062 					*Cnts[0] = Bufsz - left;
1063 					(void) chgreel(&In, INPUT);
1064 					tpcnt = actual_blocks() * BLKSIZ;
1065 					break;
1066 				}
1067 				have = (tpcnt < have) ? tpcnt : have;
1068 			}
1069 			errno = 0;
1070 			if ((rv = g_read(In.f_dev, In.f_des, c_p, have)) < 0) {
1071 				if (Eomflg && errno == ENOSPC) {
1072 					(void) chgreel(&In, INPUT);
1073 					break;
1074 				} else
1075 					perr(1, "I/O error %d on read\n",
1076 					    errno);
1077 			}
1078 			left -= rv;
1079 			c_p += rv;
1080 			xfer_cnt -= rv;
1081 			if (!Eomflg && Itape)
1082 				tpcnt -= rv;
1083 		} /* left >= In.f_bsize && xfer_cnt */
1084 		*Cnts[0] = Bufsz - left;
1085 		Blocks += *Cnts[0];
1086 		c_p = Buf[0];
1087 		left = *Cnts[0];
1088 		rv = 0;
1089 		while (left >= Out.f_bsize || (left > 0 && !xfer_cnt)) {
1090 			have = (left < Out.f_bsize) ? left : Out.f_bsize;
1091 			if (!Eomflg && Otape) {
1092 				if (!tpcnt) {
1093 					(void) chgreel(&Out, OUTPUT);
1094 					tpcnt = actual_blocks() * BLKSIZ;
1095 				}
1096 				have = (tpcnt < have) ? tpcnt : have;
1097 			}
1098 			errno = 0;
1099 			if ((rv = g_write(Out.f_dev, Out.f_des, c_p, have)) <
1100 			    0) {
1101 				if (Eomflg && errno == ENOSPC) {
1102 					(void) chgreel(&Out, OUTPUT);
1103 					continue;
1104 				} else
1105 					perr(1, "I/O error %d on write\n",
1106 					    errno);
1107 			}
1108 			left -= rv;
1109 			c_p += rv;
1110 			V_labl.v_offset += rv;
1111 			if (!Eomflg && Otape)
1112 				tpcnt -= rv;
1113 		} /* left >= Out.f_bsize */
1114 		if (left) {
1115 			(void) memcpy(Buf[0], c_p, left);
1116 			Blocks -= left;
1117 		}
1118 		*Cnts[0] = left;
1119 	} /* xfer_cnt */
1120 	Blocks /= BLKSIZ;
1121 }
1122 
1123 /*
1124  * flush_bufs:  Permit child to read the remaining data from the
1125  * buffer before prompting user for end-of-media.
1126  */
1127 
1128 static void
1129 flush_bufs(int buffer)
1130 {
1131 
1132 	Blocks += *Cnts[buffer];
1133 	if (semop(Sem_id[buffer], &Rstsem_buf, 1) < 0)
1134 		perr(5, "Semaphore operation error %d\n", errno);
1135 	if (semop(Sem_id[buffer], &Sem_buf, 1) < 0)
1136 		perr(6, "Semaphore operation error %d\n", errno);
1137 }
1138 
1139 /*
1140  * cleanup:  Clean up shared memory and semaphore resources.
1141  */
1142 
1143 static void
1144 cleanup(void)
1145 {
1146 	int cnt;
1147 
1148 	if (Ipc) {
1149 		for (cnt = 0; cnt < BUFCNT; cnt++) {
1150 			(void) semctl(Sem_id[cnt], IPC_RMID, 0);
1151 			(void) shmctl(Shm_id[cnt], IPC_RMID, 0);
1152 		}
1153 	}
1154 }
1155 
1156 /*
1157  * find_lcm: Find the lowest common multiple of two numbers.  This is used
1158  * to determine the buffer size that should be malloc(3)'d such that the
1159  * input and output data blocks can both fit evenly into the buffer.
1160  */
1161 
1162 int
1163 find_lcm(int sz1, int sz2)
1164 {
1165 	int inc, lcm, small;
1166 
1167 	if (sz1 < sz2) {
1168 		lcm = inc = sz2;
1169 		small = sz1;
1170 	} else { /* sz1 >= sz2 */
1171 		lcm = inc = sz1;
1172 		small = sz2;
1173 	}
1174 	while (lcm % small != 0)
1175 		lcm += inc;
1176 	return (lcm);
1177 }
1178 
1179 /*
1180  * Determine bpi information from drive names.
1181  */
1182 
1183 int
1184 getbpi(char *inp)
1185 {
1186 
1187 /*
1188  * Kludge to recognize Accellerated Tape Controller usage from
1189  * letter 'a' or 'A' following density given by user.
1190  *
1191  * Kludge to recognize 3B15 Compatibility Mode from
1192  * letter 'c' or 'C' following density given by user.
1193  */
1194 	if (M3b15) {
1195 		if (inp[4] == 'a' || inp[4] == 'A') {
1196 			Drive_typ = A_DRIVE;
1197 			inp[4] = '\0';
1198 		}
1199 		if (inp[4] == 'c' || inp[4] == 'C') {
1200 			Drive_typ = C_DRIVE;
1201 			inp[4] = '\0';
1202 		}
1203 	}
1204 	return (atoi(inp));
1205 }
1206 
1207 /*
1208  * blks_per_ft:  Determine the number of blocks per foot of tape.
1209  * Inter-block gap (dgap) is 0.3 in.
1210  */
1211 
1212 int
1213 blks_per_ft(double disc)
1214 {
1215 	double dcnt = Blk_cnt, dBpi = Bpi, dsiz = BLKSIZ, dgap = 0.3;
1216 
1217 	return ((int)(dcnt / (((dcnt * dsiz / dBpi) + dgap) / 12.0) * disc));
1218 }
1219 
1220 /*
1221  * tapeck:  Arbitrary block size.  Determine the number of physical blocks per
1222  * foot of tape, including the inter-block gap, and the possibility of a short
1223  * tape.  Assume the usable portion of a tape is 85% of its length for small
1224  * block sizes and 88% for large block sizes.
1225  */
1226 
1227 int
1228 tapeck(struct file_info *f_p, int dir)
1229 {
1230 	int again = 1, verify, old_style, new_style;
1231 	char resp[16];
1232 
1233 	errno = 0;
1234 	if ((f_p->f_bsize = g_init(&f_p->f_dev, &f_p->f_des)) < 0)
1235 		perr(1, "volcopy: Error %d during initialization\n", errno);
1236 	if ((f_p->f_dev != G_TM_TAPE) && (f_p->f_dev != G_XT_TAPE) &&
1237 	    (f_p->f_dev != G_ST_TAPE))
1238 		return (0);
1239 	V_labl.v_magic[0] = '\0';	/* scribble on old data */
1240 	alarm(5);
1241 	if (g_read(f_p->f_dev, f_p->f_des, &V_labl, sizeof (V_labl)) <= 0) {
1242 		if (dir == INPUT)
1243 			perror("input tape");
1244 		else
1245 			perror("output tape");
1246 	}
1247 	alarm(0);
1248 	if (V_labl.v_reel == (char)NULL && dir == INPUT)
1249 		perr(9, "Input tape is empty\n");
1250 	else {
1251 		old_style = strncmp(V_labl.v_magic, "Volcopy", 7) == 0;
1252 		new_style = strncmp(V_labl.v_magic, "VOLCOPY", 7) == 0;
1253 		if (!old_style && !new_style) {
1254 			verify = (dir == INPUT) ? 0 : 1;
1255 			prompt(verify, "Not a labeled tape\n");
1256 			if (dir == INPUT)
1257 				perr(10, "Input tape not made by volcopy\n");
1258 			mklabel();
1259 			strncpy(V_labl.v_volume, f_p->f_vol_p, 6);
1260 			Osup.fs_time = 0;
1261 		} else if (new_style) {
1262 			Eomflg = (dir == INPUT) ? 1 : Eomflg;
1263 			if (!Eomflg)
1264 				strncpy(V_labl.v_magic, "Volcopy", 7);
1265 		}
1266 	}
1267 	if (*f_p->f_vol_p == '-')
1268 		strncpy(f_p->f_vol_p, V_labl.v_volume, 6);
1269 	else if (NOT_EQ(V_labl.v_volume, f_p->f_vol_p, 6)) {
1270 		prompt(1, "Header volume(%.6s) does not match (%s)\n",
1271 			V_labl.v_volume, f_p->f_vol_p);
1272 		strncpy(V_labl.v_volume, f_p->f_vol_p, 6);
1273 	}
1274 	if (dir == INPUT) {
1275 		Bpi = V_labl.v_dens;
1276 		if (!Eomflg) {
1277 			R_len = V_labl.v_length;
1278 			R_num = V_labl.v_reels;
1279 		}
1280 		if (M3b15) {
1281 			if (V_labl.v_type == T_TYPE) {
1282 				if (V_labl.v_nblocks == 0) {
1283 					Blk_cnt = 10;
1284 					Drive_typ = C_DRIVE;
1285 				} else
1286 					Blk_cnt = V_labl.v_nblocks;
1287 				if (V_labl.v_nblocks == 32)
1288 					Drive_typ = A_DRIVE;
1289 				else
1290 					Drive_typ = 0;
1291 			} else {
1292 				Drive_typ = 0;
1293 				Blk_cnt = 10;
1294 				Drive_typ = C_DRIVE;
1295 			}
1296 		}
1297 	}
1298 	while (!Eomflg && (R_len <= 0 || R_len > 3600)) {
1299 		(void) printf(gettext(
1300 			"Enter size of reel in feet for <%s>:   "),
1301 				f_p->f_vol_p);
1302 		fgets(resp, 10, Devtty);
1303 		R_len = atoi(resp);
1304 		if (R_len > 0 && R_len <= 3600)
1305 			break;
1306 		perr(0, "Size of reel must be > 0, <= 3600\n");
1307 	}
1308 	while (!Eomflg && again) {
1309 		again = 0;
1310 		if (!Bpi) {
1311 			(void) printf(gettext(
1312 				"Tape density? (i.e., 800 | 1600 | 6250)?   "));
1313 			fgets(resp, 10, Devtty);
1314 			Bpi = getbpi(resp);
1315 		}
1316 		switch (Bpi) {
1317 		case 800:
1318 			R_blks = Ft800x10 * R_len;
1319 			break;
1320 		case 1600:
1321 			if (M3b15) {
1322 				switch (Blk_cnt) {
1323 				case 1: /* Writing a new tape */
1324 					if (Drive_typ == A_DRIVE)
1325 						R_blks = Ft1600x32 * R_len;
1326 					else if (Drive_typ == C_DRIVE)
1327 						R_blks = Ft1600x10 * R_len;
1328 					else
1329 						R_blks = Ft1600x16 * R_len;
1330 					break;
1331 				case 10:
1332 					R_blks = Ft1600x10 * R_len;
1333 					break;
1334 				case 16:
1335 					R_blks = Ft1600x16 * R_len;
1336 					break;
1337 				case 32:
1338 					R_blks = Ft1600x32 * R_len;
1339 					break;
1340 				default:
1341 					if (Blk_cnt < 32)
1342 						R_blks = blks_per_ft(0.85);
1343 					else
1344 						R_blks = blks_per_ft(0.88);
1345 					R_blks *= R_len;
1346 				} /* Blk_cnt */
1347 			} else
1348 				R_blks = Ft1600x10 * R_len;
1349 			break;
1350 		case 6250:
1351 			if (M3b15) {
1352 				switch (Blk_cnt) {
1353 				case 1: /* Writing a new tape */
1354 					if (Drive_typ == A_DRIVE)
1355 						R_blks = Ft6250x32 * R_len;
1356 					else if (Drive_typ == C_DRIVE)
1357 						R_blks = Ft6250x10 * R_len;
1358 					else
1359 						R_blks = Ft6250x16 * R_len;
1360 					break;
1361 				case 10:
1362 					R_blks = Ft6250x10 * R_len;
1363 					break;
1364 				case 16:
1365 					R_blks = Ft6250x16 * R_len;
1366 					break;
1367 				case 32:
1368 					R_blks = Ft6250x32 * R_len;
1369 					break;
1370 				default:
1371 					if (Blk_cnt < 32)
1372 						R_blks = blks_per_ft(0.85);
1373 					else
1374 						R_blks = blks_per_ft(0.88);
1375 					R_blks *= R_len;
1376 				}
1377 			} else
1378 				R_blks = Ft6250x50 * R_len;
1379 			break;
1380 		default:
1381 			perr(0, "Bpi must be 800, 1600, or 6250\n");
1382 			Bpi = 0;
1383 			again = 1;
1384 		} /* Bpi */
1385 	} /* again */
1386 	(void) printf(gettext("\nReel %.6s"), V_labl.v_volume);
1387 	if (!Eomflg) {
1388 		V_labl.v_length = R_len;
1389 		V_labl.v_dens = Bpi;
1390 		(void) printf(gettext(", %d feet, %d BPI\n"), R_len, Bpi);
1391 	} else
1392 		(void) printf(gettext(", ? feet\n"));
1393 	return (1);
1394 }
1395 
1396 /*
1397  * hdrck:  Look for and validate a volcopy style tape label.
1398  */
1399 
1400 int
1401 hdrck(int dev, int fd, char *tvol)
1402 {
1403 	int verify;
1404 	struct volcopy_label tlabl;
1405 
1406 	alarm(15); /* don't scan whole tape for label */
1407 	errno = 0;
1408 	if (g_read(dev, fd, &tlabl, sizeof (tlabl)) != sizeof (tlabl)) {
1409 		alarm(0);
1410 		verify = Otape;
1411 		prompt(verify, "Cannot read header\n");
1412 		if (Itape)
1413 			close(fd);
1414 		else
1415 			strncpy(V_labl.v_volume, tvol, 6);
1416 		return (verify);
1417 	}
1418 	alarm(0);
1419 	V_labl.v_reel = tlabl.v_reel;
1420 	if (NOT_EQ(tlabl.v_volume, tvol, 6)) {
1421 		perr(0, "Volume is <%.6s>, not <%s>.\n", tlabl.v_volume, tvol);
1422 		if (ask("Want to override?   ")) {
1423 			if (Otape)
1424 				strncpy(V_labl.v_volume, tvol, 6);
1425 			else
1426 				strncpy(tvol, tlabl.v_volume, 6);
1427 			return (1);
1428 		}
1429 		return (0);
1430 	}
1431 	return (1);
1432 }
1433 
1434 /*
1435  * mklabel:  Zero out and initialize a volcopy label.
1436  */
1437 
1438 static void
1439 mklabel(void)
1440 {
1441 
1442 	(void) memcpy(&V_labl, Empty, sizeof (V_labl));
1443 	if (!Eomflg)
1444 		(void) strcpy(V_labl.v_magic, "Volcopy");
1445 	else
1446 		(void) strcpy(V_labl.v_magic, "VOLCOPY");
1447 }
1448 
1449 /*
1450  * rprt:  Report activity to user.
1451  */
1452 
1453 static void
1454 rprt(void)
1455 {
1456 
1457 	if (Itape)
1458 		(void) printf(gettext("\nReading "));
1459 	else /* Otape */
1460 		(void) printf(gettext("\nWriting "));
1461 	if (!Eomflg)
1462 		(void) printf(gettext(
1463 			"REEL %d of %d VOL = %.6s\n"),
1464 				R_cur, R_num, In.f_vol_p);
1465 	else
1466 		(void) printf(gettext(
1467 			"REEL %d of ? VOL = %.6s\n"), R_cur, In.f_vol_p);
1468 }
1469 
1470 #ifdef LOG
1471 /*
1472  * fslog: Log current activity.
1473  */
1474 
1475 static int
1476 fslog(void)
1477 {
1478 	FILE *fp = NULL;
1479 
1480 	fp = fopen("/var/adm/filesave.log", "a");
1481 	if (fp == NULL) {
1482 		perr(1, "volcopy: cannot open /var/adm/filesave.log\n");
1483 	}
1484 
1485 	fprintf(fp, "%s%c%.6s%c%.6s -> %s%c%.6s%c%.6s on %.24s\n",
1486 		In.f_dev_p, ';', Ifname, ';', Ifpack, Out.f_dev_p,
1487 		';', Ofname, ';', Ofpack, ctime(&Tvec));
1488 	fclose(fp);
1489 	return (0);
1490 }
1491 #endif	/* LOG */
1492 
1493 /*
1494  * getname:  Get device name.
1495  */
1496 
1497 void
1498 getname(char *nam_p)
1499 {
1500 	int lastchar;
1501 	char nam_buf[21];
1502 
1503 	nam_buf[0] = '\0';
1504 	(void) printf(gettext("Changing drives? (type RETURN for no,\n"));
1505 	(void) printf(gettext("\t`/dev/rmt/??\' or `/dev/rtp/??\' for yes: "));
1506 	fgets(nam_buf, 20, Devtty);
1507 	nam_buf[20] = '\0';
1508 	lastchar = strlen(nam_buf) - 1;
1509 	if (nam_buf[lastchar] == '\n')
1510 		nam_buf[lastchar] = '\0'; /* remove it */
1511 	if (nam_buf[0] != '\0')
1512 		(void) strcpy(nam_p, nam_buf);
1513 }
1514 
1515 /*
1516  * chgreel:  Change reel on end-of-media.
1517  */
1518 
1519 static void
1520 chgreel(struct file_info *f_p, int dir)
1521 {
1522 	int again = 1, lastchar, temp;
1523 	char vol_tmp[11];
1524 
1525 	R_cur++;
1526 	while (again) {
1527 		again = 0;
1528 		errno = 0;
1529 		(void) close(f_p->f_des);
1530 		(void) getname(f_p->f_dev_p);
1531 		(void) printf(gettext(
1532 			"Mount tape %d\nType volume-ID when ready:   "), R_cur);
1533 		vol_tmp[0] = '\0';
1534 		fgets(vol_tmp, 10, Devtty);
1535 		vol_tmp[10] = '\0';
1536 		lastchar = strlen(vol_tmp) - 1;
1537 		if (vol_tmp[lastchar] == '\n')
1538 			vol_tmp[lastchar] = '\0'; /* remove it */
1539 		if (vol_tmp[0] != '\0') { /* if null string, use old vol-id */
1540 			strncpy(f_p->f_vol_p, vol_tmp, 6);
1541 			strncpy(V_labl.v_volume, vol_tmp, 6);
1542 		}
1543 		errno = 0;
1544 		f_p->f_des = open(f_p->f_dev_p, 0);
1545 		if (f_p->f_des <= 0 || f_p->f_des > 10) {
1546 			if (dir == INPUT)
1547 				perror("input ERR");
1548 			else
1549 				perror("output ERR");
1550 		}
1551 		errno = 0;
1552 		if (g_init(&(f_p->f_dev), &(f_p->f_des)) < 0)
1553 			perr(0, "Initialization error %d\n", errno);
1554 		if ((f_p->f_dev != G_TM_TAPE) && (f_p->f_dev != G_XT_TAPE) &&
1555 		    (f_p->f_dev != G_ST_TAPE)) {
1556 			(void) printf(gettext(
1557 				"\n'%s' is not a valid device"), f_p->f_dev_p);
1558 			(void) printf(gettext(
1559 		"\n\tenter device name `/dev/rmt/??\' or `/dev/rtp/??\' :"));
1560 			again = 1;
1561 			continue;
1562 		}
1563 		if (!hdrck(f_p->f_dev, f_p->f_des, f_p->f_vol_p)) {
1564 			again = 1;
1565 			continue;
1566 		}
1567 		switch (dir) {
1568 		case INPUT:
1569 			if (V_labl.v_reel != R_cur) {
1570 				perr(0, "Need reel %d, label says reel %d\n",
1571 				    R_cur, V_labl.v_reel);
1572 				again = 1;
1573 				continue;
1574 			}
1575 			break;
1576 		case OUTPUT:
1577 			V_labl.v_reel = R_cur;
1578 			temp = V_labl.v_offset;
1579 			V_labl.v_offset /= BUFSIZ;
1580 			close(f_p->f_des);
1581 			sleep(2);
1582 			errno = 0;
1583 			f_p->f_des = open(f_p->f_dev_p, 1);
1584 			if (f_p->f_des <= 0 || f_p->f_des > 10)
1585 				perror("output ERR");
1586 			errno = 0;
1587 			if (g_init(&(f_p->f_dev), &(f_p->f_des)) < 0)
1588 				perr(1, "Initialization error %d\n", errno);
1589 			errno = 0;
1590 			if (g_write(f_p->f_dev, f_p->f_des, &V_labl,
1591 			    sizeof (V_labl)) < 0) {
1592 				perr(0, "Cannot re-write header -Try again!\n");
1593 				again = 1;
1594 				V_labl.v_offset = temp;
1595 				continue;
1596 			}
1597 			V_labl.v_offset = temp;
1598 			break;
1599 		default:
1600 			perr(1, "Impossible case\n");
1601 		} /* dir */
1602 	} /* again */
1603 	rprt();
1604 }
1605 
1606 /*
1607  * perr:  Print error messages.
1608  */
1609 
1610 static void
1611 perr(int severity, const char *fmt, ...)
1612 {
1613 	va_list ap;
1614 
1615 	va_start(ap, fmt);
1616 	(void) fflush(stdout);
1617 	(void) fflush(stderr);
1618 	if (severity == 10) {
1619 		(void) vfprintf(stdout, gettext(fmt), ap);
1620 		(void) fprintf(stdout, gettext(
1621 			"\t%d reel(s) completed\n"), --R_cur);
1622 		(void) fflush(stdout);
1623 		(void) fflush(stderr);
1624 		cleanup();
1625 		exit(31+9);
1626 	}
1627 	(void) vfprintf(stderr, gettext(fmt), ap);
1628 	(void) fflush(stderr);
1629 	va_end(ap);
1630 	if (severity > 0) {
1631 		(void) cleanup();
1632 		exit(31+severity);
1633 	}
1634 }
1635 
1636 
1637 static void
1638 getinfs(int dev, int fd, char *buf)
1639 {
1640 	int cnt;
1641 	int i;
1642 
1643 	if (lseek(fd, SBLOCK * DEV_BSIZE, 0) != SBLOCK * DEV_BSIZE) {
1644 		perr(10, "Unable to lseek on input\n");
1645 	}
1646 	cnt = SBSIZE/DEV_BSIZE;
1647 	for (i = 0; i < cnt; i++) {
1648 		if (g_read(dev, fd, (char *)buf + i*DEV_BSIZE, DEV_BSIZE)
1649 			!= DEV_BSIZE) {
1650 			perr(10, "Unable to read on input\n");
1651 		}
1652 	}
1653 }
1654 
1655 static void
1656 getoutfs(int dev, int fd, char *buf, int verify)
1657 {
1658 	int cnt;
1659 	int i;
1660 
1661 	errno = 0;
1662 	if (lseek(fd, SBLOCK * DEV_BSIZE, 0) != SBLOCK * DEV_BSIZE) {
1663 		prompt(verify, "Unable to lseek on output\n", errno);
1664 	}
1665 	cnt = SBSIZE/DEV_BSIZE;
1666 	for (i = 0; i < cnt; i++) {
1667 		if (g_read(dev, fd, (char *)buf + i*DEV_BSIZE, DEV_BSIZE)
1668 			!= DEV_BSIZE) {
1669 			prompt(verify, "Unable to read on output\n", errno);
1670 		}
1671 	}
1672 }
1673 
1674 static char *
1675 getfslabel(struct fs *sb)
1676 {
1677 	int i;
1678 	int blk;
1679 
1680 	/*
1681 	 * is there room for label?
1682 	 */
1683 
1684 	if (sb->fs_cpc <= 0)
1685 		return (nolabel);
1686 
1687 	/*
1688 	 * calculate the available blocks for each rotational position
1689 	 */
1690 	blk = sb->fs_spc * sb->fs_cpc / sb->fs_nspf;
1691 	for (i = 0; i < blk; i += sb->fs_frag)
1692 		/* void */;
1693 	i -= sb->fs_frag;
1694 	blk = i / sb->fs_frag;
1695 
1696 	return ((char *)&(fs_rotbl(sb)[blk]));
1697 }
1698 
1699 static char *
1700 getvolabel(struct fs *sb)
1701 {
1702 	char *p;
1703 	int i;
1704 
1705 	p = getfslabel(sb);
1706 
1707 	if (p == nolabel || p == NULL)
1708 		return (nolabel);
1709 
1710 	for (i = 0; *p && i < 6; p++, i++)
1711 		;
1712 	p++;
1713 	return (p);
1714 }
1715