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