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