xref: /illumos-gate/usr/src/cmd/cdrw/trackio.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*4077f1edSminht  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <thread.h>
287c478bd9Sstevel@tonic-gate #include <synch.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "device.h"
387c478bd9Sstevel@tonic-gate #include "bstream.h"
397c478bd9Sstevel@tonic-gate #include "trackio.h"
407c478bd9Sstevel@tonic-gate #include "util.h"
417c478bd9Sstevel@tonic-gate #include "mmc.h"
427c478bd9Sstevel@tonic-gate #include "transport.h"
437c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
447c478bd9Sstevel@tonic-gate #include "main.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * tio data
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate static struct iobuf	tio_iobs[NIOBS];
507c478bd9Sstevel@tonic-gate static uchar_t		tio_synch_initialized, tio_abort, tio_done;
517c478bd9Sstevel@tonic-gate static int		tio_errno;
527c478bd9Sstevel@tonic-gate static mutex_t		tio_mutex;
537c478bd9Sstevel@tonic-gate static cond_t		tio_cond;
547c478bd9Sstevel@tonic-gate static int		tio_fd, tio_trackno;
557c478bd9Sstevel@tonic-gate static int		tio_got_ctrl_c;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Progress call back data.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate static mutex_t	pcb_mutex;
617c478bd9Sstevel@tonic-gate static cond_t	pcb_cond;
627c478bd9Sstevel@tonic-gate static uchar_t	pcb_user_abort, pcb_done, pcb_synch_initialized;
63*4077f1edSminht static int64_t	pcb_completed_io_size;
640c83a891Snakanon static int	(*pcb_cb)(int64_t, int64_t);
650c83a891Snakanon static int64_t	pcb_arg;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static void
fini_tio_data(void)687c478bd9Sstevel@tonic-gate fini_tio_data(void)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	int i;
717c478bd9Sstevel@tonic-gate 	for (i = 0; i < NIOBS; i++) {
727c478bd9Sstevel@tonic-gate 		if (tio_iobs[i].iob_buf) {
737c478bd9Sstevel@tonic-gate 			free(tio_iobs[i].iob_buf);
747c478bd9Sstevel@tonic-gate 			tio_iobs[i].iob_buf = NULL;
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if (tio_synch_initialized == 1) {
787c478bd9Sstevel@tonic-gate 		(void) mutex_destroy(&tio_mutex);
797c478bd9Sstevel@tonic-gate 		(void) cond_destroy(&tio_cond);
807c478bd9Sstevel@tonic-gate 		tio_synch_initialized = 0;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	tio_abort = tio_done = 0;
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static void
init_tio_data(int bsize)867c478bd9Sstevel@tonic-gate init_tio_data(int bsize)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	int i;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	(void) memset(tio_iobs, 0, sizeof (tio_iobs));
917c478bd9Sstevel@tonic-gate 	for (i = 0; i < NIOBS; i++) {
927c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_buf = (uchar_t *)my_zalloc(bsize);
937c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_total_size = bsize;
947c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_EMPTY;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	(void) mutex_init(&tio_mutex, USYNC_THREAD, 0);
977c478bd9Sstevel@tonic-gate 	(void) cond_init(&tio_cond, USYNC_THREAD, 0);
987c478bd9Sstevel@tonic-gate 	tio_synch_initialized = 1;
997c478bd9Sstevel@tonic-gate 	tio_abort = tio_done = 0;
1007c478bd9Sstevel@tonic-gate 	tio_got_ctrl_c = 0;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static void
init_pcb_data(void)1047c478bd9Sstevel@tonic-gate init_pcb_data(void)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	(void) mutex_init(&pcb_mutex, USYNC_THREAD, 0);
1077c478bd9Sstevel@tonic-gate 	(void) cond_init(&pcb_cond, USYNC_THREAD, 0);
1087c478bd9Sstevel@tonic-gate 	pcb_user_abort = pcb_done = 0;
1097c478bd9Sstevel@tonic-gate 	pcb_completed_io_size = 0;
1107c478bd9Sstevel@tonic-gate 	pcb_synch_initialized = 1;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static void
fini_pcb_data(void)1147c478bd9Sstevel@tonic-gate fini_pcb_data(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	if (pcb_synch_initialized == 1) {
1177c478bd9Sstevel@tonic-gate 		(void) mutex_destroy(&pcb_mutex);
1187c478bd9Sstevel@tonic-gate 		(void) cond_destroy(&pcb_cond);
1197c478bd9Sstevel@tonic-gate 		pcb_synch_initialized = 0;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	pcb_user_abort = pcb_done = 0;
1227c478bd9Sstevel@tonic-gate 	pcb_completed_io_size = 0;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /* ARGSUSED */
1267c478bd9Sstevel@tonic-gate static void *
write_to_cd(void * arg)1277c478bd9Sstevel@tonic-gate write_to_cd(void *arg)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	int i;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	i = 0;
1327c478bd9Sstevel@tonic-gate #ifndef lint
1337c478bd9Sstevel@tonic-gate 	while (1) {
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tio_mutex);
1367c478bd9Sstevel@tonic-gate 		while ((tio_iobs[i].iob_state != IOBS_READY) &&
1377c478bd9Sstevel@tonic-gate 		    (tio_abort == 0)) {
1387c478bd9Sstevel@tonic-gate 			/* Wait for buffer to become ready */
1397c478bd9Sstevel@tonic-gate 			(void) cond_wait(&tio_cond, &tio_mutex);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		if (tio_abort == 1) {
1427c478bd9Sstevel@tonic-gate 			/* Do a flush cache before aborting */
1437c478bd9Sstevel@tonic-gate 			(void) flush_cache(tio_fd);
1447c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
1457c478bd9Sstevel@tonic-gate 			thr_exit((void *)1);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_UNDER_DEVICE_IO;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		/* If no more data, then close the track */
1507c478bd9Sstevel@tonic-gate 		if (tio_iobs[i].iob_data_size == 0) {
1517c478bd9Sstevel@tonic-gate 			int retry = 20;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			/* Some drives misbehave if flush_cache is not done */
1547c478bd9Sstevel@tonic-gate 			(void) flush_cache(tio_fd);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 			if (write_mode == TAO_MODE) {
1577c478bd9Sstevel@tonic-gate 				/* Its important to try hard to close track */
1587c478bd9Sstevel@tonic-gate 				if (simulation)
1597c478bd9Sstevel@tonic-gate 					retry = 5;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 				for (; retry > 0; retry--) {
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 					/* OK to hold mutex when close_track */
1647c478bd9Sstevel@tonic-gate 					if (close_track(tio_fd,
1657c478bd9Sstevel@tonic-gate 					    tio_trackno, 0, 0))
1667c478bd9Sstevel@tonic-gate 						break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 					(void) sleep(1);
1697c478bd9Sstevel@tonic-gate 				}
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 			/* Some drives don't allow close track in test write */
1737c478bd9Sstevel@tonic-gate 			if ((retry == 0) && (simulation == 0)) {
1747c478bd9Sstevel@tonic-gate 				if (errno)
1757c478bd9Sstevel@tonic-gate 					tio_errno = errno;
1767c478bd9Sstevel@tonic-gate 				else
1777c478bd9Sstevel@tonic-gate 					tio_errno = -1;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			tio_done = 1;
1817c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&tio_cond);
1827c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
1837c478bd9Sstevel@tonic-gate 			thr_exit((void *)0);
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tio_mutex);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		if (!write10(tio_fd, tio_iobs[i].iob_start_blk,
1897c478bd9Sstevel@tonic-gate 		    tio_iobs[i].iob_nblks, tio_iobs[i].iob_buf,
1907c478bd9Sstevel@tonic-gate 		    tio_iobs[i].iob_data_size)) {
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			int err = errno;
1937c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tio_mutex);
1947c478bd9Sstevel@tonic-gate 			if (err)
1957c478bd9Sstevel@tonic-gate 				tio_errno = err;
1967c478bd9Sstevel@tonic-gate 			else
1977c478bd9Sstevel@tonic-gate 				tio_errno = -1;
1987c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&tio_cond);
1997c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
2007c478bd9Sstevel@tonic-gate 			thr_exit((void *)2);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tio_mutex);
2047c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_EMPTY;
2057c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&tio_cond);
2067c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tio_mutex);
2077c478bd9Sstevel@tonic-gate 		i++;
2087c478bd9Sstevel@tonic-gate 		if (i == NIOBS)
2097c478bd9Sstevel@tonic-gate 			i = 0;
2107c478bd9Sstevel@tonic-gate #ifndef lint
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate #endif
2137c478bd9Sstevel@tonic-gate 	return (NULL);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /* ARGSUSED */
2177c478bd9Sstevel@tonic-gate static void *
progress_callback(void * arg)2187c478bd9Sstevel@tonic-gate progress_callback(void *arg)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int ret;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate pc_again:
2237c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&pcb_mutex);
2247c478bd9Sstevel@tonic-gate 	if (!pcb_done) {
2257c478bd9Sstevel@tonic-gate 		(void) cond_wait(&pcb_cond, &pcb_mutex);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	if (pcb_done) {
2287c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&pcb_mutex);
2297c478bd9Sstevel@tonic-gate 		if (tio_got_ctrl_c) {
2307c478bd9Sstevel@tonic-gate 			pcb_cb(pcb_arg, 0xFFFFFFFF);
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 		thr_exit((void *)0);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&pcb_mutex);
2357c478bd9Sstevel@tonic-gate 	ret = pcb_cb(pcb_arg, pcb_completed_io_size);
2367c478bd9Sstevel@tonic-gate 	if (ret != 0) {
2377c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&pcb_mutex);
2387c478bd9Sstevel@tonic-gate 		pcb_user_abort = (uchar_t)ret;
2397c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&pcb_mutex);
2407c478bd9Sstevel@tonic-gate 		thr_exit((void *)0);
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate #ifdef lint
2437c478bd9Sstevel@tonic-gate 	return (NULL);
2447c478bd9Sstevel@tonic-gate #else
2457c478bd9Sstevel@tonic-gate 	goto pc_again;
2467c478bd9Sstevel@tonic-gate #endif
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /* ARGSUSED */
2507c478bd9Sstevel@tonic-gate static void
trackio_sig_handler(int i)2517c478bd9Sstevel@tonic-gate trackio_sig_handler(int i)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	/* Dont need mutex as it is only modified here */
2547c478bd9Sstevel@tonic-gate 	tio_got_ctrl_c = 1;
2557c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, trackio_sig_handler);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate int
write_track(cd_device * dev,struct track_info * ti,bstreamhandle h,int (* cb)(int64_t,int64_t),int64_t arg,struct trackio_error * te)2597c478bd9Sstevel@tonic-gate write_track(cd_device *dev, struct track_info *ti, bstreamhandle h,
2600c83a891Snakanon 	int (*cb)(int64_t, int64_t), int64_t arg, struct trackio_error *te)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	int			blksize, i, sz_read, rem;
2637c478bd9Sstevel@tonic-gate 	uint32_t		start_b;
2647c478bd9Sstevel@tonic-gate 	thread_t		tio_thread, pc_thread;
2657c478bd9Sstevel@tonic-gate 	int			write_cd_thr_created;
2667c478bd9Sstevel@tonic-gate 	int			progress_callback_thr_created;
2677c478bd9Sstevel@tonic-gate 	int			signal_handler_installed;
2687c478bd9Sstevel@tonic-gate 	int			retval;
2697c478bd9Sstevel@tonic-gate 	void			(*ohandler)(int);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	write_cd_thr_created = progress_callback_thr_created = 0;
2727c478bd9Sstevel@tonic-gate 	signal_handler_installed = retval = 0;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (ti->ti_track_mode & 4)
2757c478bd9Sstevel@tonic-gate 		blksize = DATA_TRACK_BLKSIZE;
2767c478bd9Sstevel@tonic-gate 	else
2777c478bd9Sstevel@tonic-gate 		blksize = AUDIO_TRACK_BLKSIZE;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/* Initialize buffers */
2807c478bd9Sstevel@tonic-gate 	init_tio_data(NBLKS_PER_BUF*blksize);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/* Fill in all buffers before starting */
2837c478bd9Sstevel@tonic-gate 	start_b = ti->ti_start_address;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Start filling initial buffer to ensure that there is plenty of
2877c478bd9Sstevel@tonic-gate 	 * data when writing begins.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	for (i = 0; i < NIOBS; i++) {
2907c478bd9Sstevel@tonic-gate 		sz_read = h->bstr_read(h, tio_iobs[i].iob_buf,
2917c478bd9Sstevel@tonic-gate 		    tio_iobs[i].iob_total_size);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * We need to read the source file into the buffer and make
2967c478bd9Sstevel@tonic-gate 		 * sure that the data in the buffer is a multiple of the
2977c478bd9Sstevel@tonic-gate 		 * blocksize (data or audio blocksize). iob_total_size is a
2987c478bd9Sstevel@tonic-gate 		 * multiple of the blocksize so this case should only be
2997c478bd9Sstevel@tonic-gate 		 * encountered at EOF or from piped input.
3007c478bd9Sstevel@tonic-gate 		 */
3017c478bd9Sstevel@tonic-gate 		while ((rem = (sz_read % blksize)) != 0) {
3027c478bd9Sstevel@tonic-gate 			int ret;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * rem contains the amount of data past the previous
3067c478bd9Sstevel@tonic-gate 			 * block boundry. we need to subtract it from the
3077c478bd9Sstevel@tonic-gate 			 * blocksize to get the amount needed to reach the
3087c478bd9Sstevel@tonic-gate 			 * next block boundry.
3097c478bd9Sstevel@tonic-gate 			 */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 			if ((sz_read + (blksize - rem)) >
3127c478bd9Sstevel@tonic-gate 			    tio_iobs[i].iob_total_size) {
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 			/*
3157c478bd9Sstevel@tonic-gate 			 * This should not occur, but we are trying to
3167c478bd9Sstevel@tonic-gate 			 * write past the end of the buffer. return
3177c478bd9Sstevel@tonic-gate 			 * with an error.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 				sz_read = -1;
3207c478bd9Sstevel@tonic-gate 				break;
3217c478bd9Sstevel@tonic-gate 			}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 			/*
3247c478bd9Sstevel@tonic-gate 			 * Try to continue reading in case the data is being
3257c478bd9Sstevel@tonic-gate 			 * piped in.
3267c478bd9Sstevel@tonic-gate 			 */
3277c478bd9Sstevel@tonic-gate 			ret = h->bstr_read(h, &tio_iobs[i].iob_buf[sz_read],
3287c478bd9Sstevel@tonic-gate 			    (blksize - rem));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 			if (ret < 0) {
3317c478bd9Sstevel@tonic-gate 				sz_read = ret;
3327c478bd9Sstevel@tonic-gate 				break;
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			/*
3367c478bd9Sstevel@tonic-gate 			 * No more data. We need to make sure that we are
3377c478bd9Sstevel@tonic-gate 			 * aligned with the blocksize. so pad the rest of
3387c478bd9Sstevel@tonic-gate 			 * the buffer with 0s
3397c478bd9Sstevel@tonic-gate 			 */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (ret == 0) {
3427c478bd9Sstevel@tonic-gate 				ret = blksize - rem;
3437c478bd9Sstevel@tonic-gate 				(void) memset(&tio_iobs[i].iob_buf[sz_read],
3447c478bd9Sstevel@tonic-gate 				    0, ret);
3457c478bd9Sstevel@tonic-gate 			}
3467c478bd9Sstevel@tonic-gate 			sz_read += ret;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		if (sz_read < 0) {
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 			/* reading the source failed, clean up and return */
3527c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_SYSTEM;
3537c478bd9Sstevel@tonic-gate 			te->te_errno = errno;
3547c478bd9Sstevel@tonic-gate 			goto write_track_failed;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_start_blk = start_b;
3587c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_nblks = (sz_read/blksize);
3597c478bd9Sstevel@tonic-gate 		start_b += tio_iobs[i].iob_nblks;
3607c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_data_size = sz_read;
3617c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_READY;
3627c478bd9Sstevel@tonic-gate 		if (sz_read == 0)
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	tio_fd = dev->d_fd;
3677c478bd9Sstevel@tonic-gate 	tio_trackno = ti->ti_track_no;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/* Install signal handler for CTRL-C */
3707c478bd9Sstevel@tonic-gate 	ohandler = signal(SIGINT, trackio_sig_handler);
3717c478bd9Sstevel@tonic-gate 	if (ohandler) {
3727c478bd9Sstevel@tonic-gate 		signal_handler_installed = 1;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/* Create thread which will issue commands to write to device */
3767c478bd9Sstevel@tonic-gate 	if (thr_create(0, 0, write_to_cd, NULL,
3777c478bd9Sstevel@tonic-gate 	    THR_BOUND | THR_NEW_LWP, &tio_thread) != 0) {
3787c478bd9Sstevel@tonic-gate 		te->err_type = TRACKIO_ERR_SYSTEM;
3797c478bd9Sstevel@tonic-gate 		te->te_errno = errno;
3807c478bd9Sstevel@tonic-gate 		goto write_track_failed;
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	write_cd_thr_created = 1;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/* If caller specified a callback, create a thread to do callbacks */
3857c478bd9Sstevel@tonic-gate 	if (cb != NULL) {
3867c478bd9Sstevel@tonic-gate 		init_pcb_data();
3877c478bd9Sstevel@tonic-gate 		pcb_cb = cb;
3887c478bd9Sstevel@tonic-gate 		pcb_arg = arg;
3897c478bd9Sstevel@tonic-gate 		if (thr_create(0, 0, progress_callback, NULL,
3907c478bd9Sstevel@tonic-gate 		    THR_BOUND | THR_NEW_LWP, &pc_thread) != 0) {
3917c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_SYSTEM;
3927c478bd9Sstevel@tonic-gate 			te->te_errno = errno;
3937c478bd9Sstevel@tonic-gate 			goto write_track_failed;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		progress_callback_thr_created = 1;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	i = 0;
3997c478bd9Sstevel@tonic-gate 	while (sz_read != 0) {
4007c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tio_mutex);
4017c478bd9Sstevel@tonic-gate 		while ((tio_iobs[i].iob_state != IOBS_EMPTY) &&
4027c478bd9Sstevel@tonic-gate 		    (tio_errno == 0) && (pcb_user_abort == 0)) {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 			/* Do callbacks only if there is nothing else to do */
4057c478bd9Sstevel@tonic-gate 			if (cb != NULL) {
4067c478bd9Sstevel@tonic-gate 				(void) mutex_lock(&pcb_mutex);
4077c478bd9Sstevel@tonic-gate 				(void) cond_broadcast(&pcb_cond);
4087c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&pcb_mutex);
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 			/* If user requested abort, bail out */
4127c478bd9Sstevel@tonic-gate 			if (pcb_user_abort || tio_got_ctrl_c) {
4137c478bd9Sstevel@tonic-gate 				break;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 			(void) cond_wait(&tio_cond, &tio_mutex);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 		if (pcb_user_abort || tio_got_ctrl_c) {
4187c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
4197c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_USER_ABORT;
4207c478bd9Sstevel@tonic-gate 			goto write_track_failed;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 		/*
4237c478bd9Sstevel@tonic-gate 		 * We've got a transport error, stop writing, save all
4247c478bd9Sstevel@tonic-gate 		 * of the error information and clean up the threads.
4257c478bd9Sstevel@tonic-gate 		 */
4267c478bd9Sstevel@tonic-gate 		if (tio_errno != 0) {
4277c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
4287c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_TRANSPORT;
4297c478bd9Sstevel@tonic-gate 			te->te_errno = tio_errno;
4307c478bd9Sstevel@tonic-gate 			te->status = uscsi_status;
4317c478bd9Sstevel@tonic-gate 			if (uscsi_status == 2) {
4327c478bd9Sstevel@tonic-gate 				te->key = SENSE_KEY(rqbuf) & 0xf;
4337c478bd9Sstevel@tonic-gate 				te->asc = ASC(rqbuf);
4347c478bd9Sstevel@tonic-gate 				te->ascq = ASCQ(rqbuf);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 			goto write_track_failed;
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		pcb_completed_io_size += tio_iobs[i].iob_data_size;
4397c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_UNDER_FILE_IO;
4407c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tio_mutex);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		sz_read = h->bstr_read(h, tio_iobs[i].iob_buf,
4437c478bd9Sstevel@tonic-gate 		    tio_iobs[i].iob_total_size);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		/*
4467c478bd9Sstevel@tonic-gate 		 * We need to read the source file into the buffer and make
4477c478bd9Sstevel@tonic-gate 		 * sure that the data in the buffer is a multiple of the
4487c478bd9Sstevel@tonic-gate 		 * blocksize (data or audio blocksize). this case should only
4497c478bd9Sstevel@tonic-gate 		 * be encountered at EOF or from piped input.
4507c478bd9Sstevel@tonic-gate 		 */
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		while ((rem = (sz_read % blksize)) != 0) {
4537c478bd9Sstevel@tonic-gate 			int ret;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 			/*
4577c478bd9Sstevel@tonic-gate 			 * This should not occur, we are trying to write
4587c478bd9Sstevel@tonic-gate 			 * past the end of the buffer, return error.
4597c478bd9Sstevel@tonic-gate 			 */
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			if ((sz_read + (blksize - rem)) >
4627c478bd9Sstevel@tonic-gate 			    tio_iobs[i].iob_total_size) {
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 				sz_read = -1;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 			/*
4697c478bd9Sstevel@tonic-gate 			 * Try to continue reading in case the data is being
4707c478bd9Sstevel@tonic-gate 			 * piped in.
4717c478bd9Sstevel@tonic-gate 			 */
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 			ret = h->bstr_read(h, &tio_iobs[i].iob_buf[sz_read],
4747c478bd9Sstevel@tonic-gate 			    (blksize - rem));
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 			if (ret < 0) {
4777c478bd9Sstevel@tonic-gate 				sz_read = ret;
4787c478bd9Sstevel@tonic-gate 				break;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 			/*
4827c478bd9Sstevel@tonic-gate 			 * No more data. We need to make sure that we are
4837c478bd9Sstevel@tonic-gate 			 * aligned with the blocksize. so pad the rest of
4847c478bd9Sstevel@tonic-gate 			 * the buffer with 0s
4857c478bd9Sstevel@tonic-gate 			 */
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			if (ret == 0) {
4887c478bd9Sstevel@tonic-gate 				/*
4897c478bd9Sstevel@tonic-gate 				 * rem contains the amount of data past the
4907c478bd9Sstevel@tonic-gate 				 * previous block boundry. we need to subtract
4917c478bd9Sstevel@tonic-gate 				 * it from the blocksize to get the amount
4927c478bd9Sstevel@tonic-gate 				 * needed to reach the next block boundry.
4937c478bd9Sstevel@tonic-gate 				 */
4947c478bd9Sstevel@tonic-gate 				ret = blksize - rem;
4957c478bd9Sstevel@tonic-gate 				(void) memset(&tio_iobs[i].iob_buf[sz_read],
4967c478bd9Sstevel@tonic-gate 				    0, ret);
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 			sz_read += ret;
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 		if (sz_read < 0) {
5017c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_SYSTEM;
5027c478bd9Sstevel@tonic-gate 			te->te_errno = errno;
5037c478bd9Sstevel@tonic-gate 			goto write_track_failed;
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&tio_mutex);
5067c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_start_blk = start_b;
5077c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_nblks = (sz_read/blksize);
5087c478bd9Sstevel@tonic-gate 		start_b += tio_iobs[i].iob_nblks;
5097c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_data_size = sz_read;
5107c478bd9Sstevel@tonic-gate 		tio_iobs[i].iob_state = IOBS_READY;
5117c478bd9Sstevel@tonic-gate 		(void) cond_broadcast(&tio_cond);
5127c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&tio_mutex);
5137c478bd9Sstevel@tonic-gate 		i++;
5147c478bd9Sstevel@tonic-gate 		if (i == NIOBS)
5157c478bd9Sstevel@tonic-gate 			i = 0;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&tio_mutex);
5187c478bd9Sstevel@tonic-gate 	while ((tio_errno == 0) && (tio_done == 0)) {
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		/* Wait for track IO to complete */
5217c478bd9Sstevel@tonic-gate 		(void) cond_wait(&tio_cond, &tio_mutex);
5227c478bd9Sstevel@tonic-gate 		if (tio_errno != 0) {
5237c478bd9Sstevel@tonic-gate 			te->err_type = TRACKIO_ERR_TRANSPORT;
5247c478bd9Sstevel@tonic-gate 			te->te_errno = tio_errno;
5257c478bd9Sstevel@tonic-gate 			te->status = uscsi_status;
5267c478bd9Sstevel@tonic-gate 			if (uscsi_status == 2) {
5277c478bd9Sstevel@tonic-gate 				te->key = SENSE_KEY(rqbuf) & 0xf;
5287c478bd9Sstevel@tonic-gate 				te->asc = ASC(rqbuf);
5297c478bd9Sstevel@tonic-gate 				te->ascq = ASCQ(rqbuf);
5307c478bd9Sstevel@tonic-gate 			}
5317c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
5327c478bd9Sstevel@tonic-gate 			goto write_track_failed;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 		if (cb != NULL) {
5357c478bd9Sstevel@tonic-gate 			while (tio_iobs[i].iob_state == IOBS_EMPTY) {
5367c478bd9Sstevel@tonic-gate 				(void) mutex_lock(&pcb_mutex);
5377c478bd9Sstevel@tonic-gate 				pcb_completed_io_size +=
5387c478bd9Sstevel@tonic-gate 				    tio_iobs[i].iob_data_size;
5397c478bd9Sstevel@tonic-gate 				(void) cond_broadcast(&pcb_cond);
5407c478bd9Sstevel@tonic-gate 				(void) mutex_unlock(&pcb_mutex);
5417c478bd9Sstevel@tonic-gate 				i++;
5427c478bd9Sstevel@tonic-gate 				if (i == NIOBS)
5437c478bd9Sstevel@tonic-gate 					i = 0;
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&tio_mutex);
5487c478bd9Sstevel@tonic-gate 	retval = 1;
5497c478bd9Sstevel@tonic-gate write_track_failed:
5507c478bd9Sstevel@tonic-gate 	if (progress_callback_thr_created) {
5517c478bd9Sstevel@tonic-gate 		if (thr_kill(pc_thread, 0) == 0) {
5527c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&pcb_mutex);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 			pcb_done = 1;
5557c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&pcb_cond);
5567c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&pcb_mutex);
5577c478bd9Sstevel@tonic-gate 			(void) thr_join(pc_thread, NULL, NULL);
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	if (write_cd_thr_created) {
5617c478bd9Sstevel@tonic-gate 		if (thr_kill(tio_thread, 0) == 0) {
5627c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&tio_mutex);
5637c478bd9Sstevel@tonic-gate 			tio_abort = 1;
5647c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&tio_cond);
5657c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&tio_mutex);
5667c478bd9Sstevel@tonic-gate 			(void) thr_join(tio_thread, NULL, NULL);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (signal_handler_installed) {
5717c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, ohandler);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	fini_tio_data();
5757c478bd9Sstevel@tonic-gate 	fini_pcb_data();
5767c478bd9Sstevel@tonic-gate 	return (retval);
5777c478bd9Sstevel@tonic-gate }
578