1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <thread.h> 30*7c478bd9Sstevel@tonic-gate #include <synch.h> 31*7c478bd9Sstevel@tonic-gate #include <errno.h> 32*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 33*7c478bd9Sstevel@tonic-gate #include <string.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 35*7c478bd9Sstevel@tonic-gate #include <signal.h> 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate #include "device.h" 40*7c478bd9Sstevel@tonic-gate #include "bstream.h" 41*7c478bd9Sstevel@tonic-gate #include "trackio.h" 42*7c478bd9Sstevel@tonic-gate #include "util.h" 43*7c478bd9Sstevel@tonic-gate #include "mmc.h" 44*7c478bd9Sstevel@tonic-gate #include "transport.h" 45*7c478bd9Sstevel@tonic-gate #include "misc_scsi.h" 46*7c478bd9Sstevel@tonic-gate #include "main.h" 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * tio data 50*7c478bd9Sstevel@tonic-gate */ 51*7c478bd9Sstevel@tonic-gate static struct iobuf tio_iobs[NIOBS]; 52*7c478bd9Sstevel@tonic-gate static uchar_t tio_synch_initialized, tio_abort, tio_done; 53*7c478bd9Sstevel@tonic-gate static int tio_errno; 54*7c478bd9Sstevel@tonic-gate static mutex_t tio_mutex; 55*7c478bd9Sstevel@tonic-gate static cond_t tio_cond; 56*7c478bd9Sstevel@tonic-gate static int tio_fd, tio_trackno; 57*7c478bd9Sstevel@tonic-gate static int tio_got_ctrl_c; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate /* 60*7c478bd9Sstevel@tonic-gate * Progress call back data. 61*7c478bd9Sstevel@tonic-gate */ 62*7c478bd9Sstevel@tonic-gate static mutex_t pcb_mutex; 63*7c478bd9Sstevel@tonic-gate static cond_t pcb_cond; 64*7c478bd9Sstevel@tonic-gate static uchar_t pcb_user_abort, pcb_done, pcb_synch_initialized; 65*7c478bd9Sstevel@tonic-gate static uint_t pcb_completed_io_size; 66*7c478bd9Sstevel@tonic-gate static int (*pcb_cb)(void *, int64_t); 67*7c478bd9Sstevel@tonic-gate static void *pcb_arg; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate static void 70*7c478bd9Sstevel@tonic-gate fini_tio_data(void) 71*7c478bd9Sstevel@tonic-gate { 72*7c478bd9Sstevel@tonic-gate int i; 73*7c478bd9Sstevel@tonic-gate for (i = 0; i < NIOBS; i++) { 74*7c478bd9Sstevel@tonic-gate if (tio_iobs[i].iob_buf) { 75*7c478bd9Sstevel@tonic-gate free(tio_iobs[i].iob_buf); 76*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_buf = NULL; 77*7c478bd9Sstevel@tonic-gate } 78*7c478bd9Sstevel@tonic-gate } 79*7c478bd9Sstevel@tonic-gate if (tio_synch_initialized == 1) { 80*7c478bd9Sstevel@tonic-gate (void) mutex_destroy(&tio_mutex); 81*7c478bd9Sstevel@tonic-gate (void) cond_destroy(&tio_cond); 82*7c478bd9Sstevel@tonic-gate tio_synch_initialized = 0; 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate tio_abort = tio_done = 0; 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate static void 88*7c478bd9Sstevel@tonic-gate init_tio_data(int bsize) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate int i; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate (void) memset(tio_iobs, 0, sizeof (tio_iobs)); 93*7c478bd9Sstevel@tonic-gate for (i = 0; i < NIOBS; i++) { 94*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_buf = (uchar_t *)my_zalloc(bsize); 95*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_total_size = bsize; 96*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_EMPTY; 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate (void) mutex_init(&tio_mutex, USYNC_THREAD, 0); 99*7c478bd9Sstevel@tonic-gate (void) cond_init(&tio_cond, USYNC_THREAD, 0); 100*7c478bd9Sstevel@tonic-gate tio_synch_initialized = 1; 101*7c478bd9Sstevel@tonic-gate tio_abort = tio_done = 0; 102*7c478bd9Sstevel@tonic-gate tio_got_ctrl_c = 0; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate static void 106*7c478bd9Sstevel@tonic-gate init_pcb_data(void) 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate (void) mutex_init(&pcb_mutex, USYNC_THREAD, 0); 109*7c478bd9Sstevel@tonic-gate (void) cond_init(&pcb_cond, USYNC_THREAD, 0); 110*7c478bd9Sstevel@tonic-gate pcb_user_abort = pcb_done = 0; 111*7c478bd9Sstevel@tonic-gate pcb_completed_io_size = 0; 112*7c478bd9Sstevel@tonic-gate pcb_synch_initialized = 1; 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate static void 116*7c478bd9Sstevel@tonic-gate fini_pcb_data(void) 117*7c478bd9Sstevel@tonic-gate { 118*7c478bd9Sstevel@tonic-gate if (pcb_synch_initialized == 1) { 119*7c478bd9Sstevel@tonic-gate (void) mutex_destroy(&pcb_mutex); 120*7c478bd9Sstevel@tonic-gate (void) cond_destroy(&pcb_cond); 121*7c478bd9Sstevel@tonic-gate pcb_synch_initialized = 0; 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate pcb_user_abort = pcb_done = 0; 124*7c478bd9Sstevel@tonic-gate pcb_completed_io_size = 0; 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 128*7c478bd9Sstevel@tonic-gate static void * 129*7c478bd9Sstevel@tonic-gate write_to_cd(void *arg) 130*7c478bd9Sstevel@tonic-gate { 131*7c478bd9Sstevel@tonic-gate int i; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate i = 0; 134*7c478bd9Sstevel@tonic-gate #ifndef lint 135*7c478bd9Sstevel@tonic-gate while (1) { 136*7c478bd9Sstevel@tonic-gate #endif 137*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 138*7c478bd9Sstevel@tonic-gate while ((tio_iobs[i].iob_state != IOBS_READY) && 139*7c478bd9Sstevel@tonic-gate (tio_abort == 0)) { 140*7c478bd9Sstevel@tonic-gate /* Wait for buffer to become ready */ 141*7c478bd9Sstevel@tonic-gate (void) cond_wait(&tio_cond, &tio_mutex); 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate if (tio_abort == 1) { 144*7c478bd9Sstevel@tonic-gate /* Do a flush cache before aborting */ 145*7c478bd9Sstevel@tonic-gate (void) flush_cache(tio_fd); 146*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 147*7c478bd9Sstevel@tonic-gate thr_exit((void *)1); 148*7c478bd9Sstevel@tonic-gate } 149*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_UNDER_DEVICE_IO; 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate /* If no more data, then close the track */ 152*7c478bd9Sstevel@tonic-gate if (tio_iobs[i].iob_data_size == 0) { 153*7c478bd9Sstevel@tonic-gate int retry = 20; 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* Some drives misbehave if flush_cache is not done */ 156*7c478bd9Sstevel@tonic-gate (void) flush_cache(tio_fd); 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate if (write_mode == TAO_MODE) { 159*7c478bd9Sstevel@tonic-gate /* Its important to try hard to close track */ 160*7c478bd9Sstevel@tonic-gate if (simulation) 161*7c478bd9Sstevel@tonic-gate retry = 5; 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate for (; retry > 0; retry--) { 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate /* OK to hold mutex when close_track */ 166*7c478bd9Sstevel@tonic-gate if (close_track(tio_fd, 167*7c478bd9Sstevel@tonic-gate tio_trackno, 0, 0)) 168*7c478bd9Sstevel@tonic-gate break; 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate (void) sleep(1); 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* Some drives don't allow close track in test write */ 175*7c478bd9Sstevel@tonic-gate if ((retry == 0) && (simulation == 0)) { 176*7c478bd9Sstevel@tonic-gate if (errno) 177*7c478bd9Sstevel@tonic-gate tio_errno = errno; 178*7c478bd9Sstevel@tonic-gate else 179*7c478bd9Sstevel@tonic-gate tio_errno = -1; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate tio_done = 1; 183*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&tio_cond); 184*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 185*7c478bd9Sstevel@tonic-gate thr_exit((void *)0); 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (!write10(tio_fd, tio_iobs[i].iob_start_blk, 191*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_nblks, tio_iobs[i].iob_buf, 192*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_data_size)) { 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate int err = errno; 195*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 196*7c478bd9Sstevel@tonic-gate if (err) 197*7c478bd9Sstevel@tonic-gate tio_errno = err; 198*7c478bd9Sstevel@tonic-gate else 199*7c478bd9Sstevel@tonic-gate tio_errno = -1; 200*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&tio_cond); 201*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 202*7c478bd9Sstevel@tonic-gate thr_exit((void *)2); 203*7c478bd9Sstevel@tonic-gate } 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 206*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_EMPTY; 207*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&tio_cond); 208*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 209*7c478bd9Sstevel@tonic-gate i++; 210*7c478bd9Sstevel@tonic-gate if (i == NIOBS) 211*7c478bd9Sstevel@tonic-gate i = 0; 212*7c478bd9Sstevel@tonic-gate #ifndef lint 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate #endif 215*7c478bd9Sstevel@tonic-gate return (NULL); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 219*7c478bd9Sstevel@tonic-gate static void * 220*7c478bd9Sstevel@tonic-gate progress_callback(void *arg) 221*7c478bd9Sstevel@tonic-gate { 222*7c478bd9Sstevel@tonic-gate int ret; 223*7c478bd9Sstevel@tonic-gate 224*7c478bd9Sstevel@tonic-gate pc_again: 225*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&pcb_mutex); 226*7c478bd9Sstevel@tonic-gate if (!pcb_done) { 227*7c478bd9Sstevel@tonic-gate (void) cond_wait(&pcb_cond, &pcb_mutex); 228*7c478bd9Sstevel@tonic-gate } 229*7c478bd9Sstevel@tonic-gate if (pcb_done) { 230*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 231*7c478bd9Sstevel@tonic-gate if (tio_got_ctrl_c) { 232*7c478bd9Sstevel@tonic-gate pcb_cb(pcb_arg, 0xFFFFFFFF); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate thr_exit((void *)0); 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 237*7c478bd9Sstevel@tonic-gate ret = pcb_cb(pcb_arg, pcb_completed_io_size); 238*7c478bd9Sstevel@tonic-gate if (ret != 0) { 239*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&pcb_mutex); 240*7c478bd9Sstevel@tonic-gate pcb_user_abort = (uchar_t)ret; 241*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 242*7c478bd9Sstevel@tonic-gate thr_exit((void *)0); 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate #ifdef lint 245*7c478bd9Sstevel@tonic-gate return (NULL); 246*7c478bd9Sstevel@tonic-gate #else 247*7c478bd9Sstevel@tonic-gate goto pc_again; 248*7c478bd9Sstevel@tonic-gate #endif 249*7c478bd9Sstevel@tonic-gate } 250*7c478bd9Sstevel@tonic-gate 251*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 252*7c478bd9Sstevel@tonic-gate static void 253*7c478bd9Sstevel@tonic-gate trackio_sig_handler(int i) 254*7c478bd9Sstevel@tonic-gate { 255*7c478bd9Sstevel@tonic-gate /* Dont need mutex as it is only modified here */ 256*7c478bd9Sstevel@tonic-gate tio_got_ctrl_c = 1; 257*7c478bd9Sstevel@tonic-gate (void) signal(SIGINT, trackio_sig_handler); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate int 261*7c478bd9Sstevel@tonic-gate write_track(cd_device *dev, struct track_info *ti, bstreamhandle h, 262*7c478bd9Sstevel@tonic-gate int (*cb)(void *, int64_t), void *arg, struct trackio_error *te) 263*7c478bd9Sstevel@tonic-gate { 264*7c478bd9Sstevel@tonic-gate int blksize, i, sz_read, rem; 265*7c478bd9Sstevel@tonic-gate uint32_t start_b; 266*7c478bd9Sstevel@tonic-gate thread_t tio_thread, pc_thread; 267*7c478bd9Sstevel@tonic-gate int write_cd_thr_created; 268*7c478bd9Sstevel@tonic-gate int progress_callback_thr_created; 269*7c478bd9Sstevel@tonic-gate int signal_handler_installed; 270*7c478bd9Sstevel@tonic-gate int retval; 271*7c478bd9Sstevel@tonic-gate void (*ohandler)(int); 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate write_cd_thr_created = progress_callback_thr_created = 0; 274*7c478bd9Sstevel@tonic-gate signal_handler_installed = retval = 0; 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate if (ti->ti_track_mode & 4) 277*7c478bd9Sstevel@tonic-gate blksize = DATA_TRACK_BLKSIZE; 278*7c478bd9Sstevel@tonic-gate else 279*7c478bd9Sstevel@tonic-gate blksize = AUDIO_TRACK_BLKSIZE; 280*7c478bd9Sstevel@tonic-gate 281*7c478bd9Sstevel@tonic-gate /* Initialize buffers */ 282*7c478bd9Sstevel@tonic-gate init_tio_data(NBLKS_PER_BUF*blksize); 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate /* Fill in all buffers before starting */ 285*7c478bd9Sstevel@tonic-gate start_b = ti->ti_start_address; 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate /* 288*7c478bd9Sstevel@tonic-gate * Start filling initial buffer to ensure that there is plenty of 289*7c478bd9Sstevel@tonic-gate * data when writing begins. 290*7c478bd9Sstevel@tonic-gate */ 291*7c478bd9Sstevel@tonic-gate for (i = 0; i < NIOBS; i++) { 292*7c478bd9Sstevel@tonic-gate sz_read = h->bstr_read(h, tio_iobs[i].iob_buf, 293*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_total_size); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate /* 297*7c478bd9Sstevel@tonic-gate * We need to read the source file into the buffer and make 298*7c478bd9Sstevel@tonic-gate * sure that the data in the buffer is a multiple of the 299*7c478bd9Sstevel@tonic-gate * blocksize (data or audio blocksize). iob_total_size is a 300*7c478bd9Sstevel@tonic-gate * multiple of the blocksize so this case should only be 301*7c478bd9Sstevel@tonic-gate * encountered at EOF or from piped input. 302*7c478bd9Sstevel@tonic-gate */ 303*7c478bd9Sstevel@tonic-gate while ((rem = (sz_read % blksize)) != 0) { 304*7c478bd9Sstevel@tonic-gate int ret; 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate /* 307*7c478bd9Sstevel@tonic-gate * rem contains the amount of data past the previous 308*7c478bd9Sstevel@tonic-gate * block boundry. we need to subtract it from the 309*7c478bd9Sstevel@tonic-gate * blocksize to get the amount needed to reach the 310*7c478bd9Sstevel@tonic-gate * next block boundry. 311*7c478bd9Sstevel@tonic-gate */ 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate if ((sz_read + (blksize - rem)) > 314*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_total_size) { 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate /* 317*7c478bd9Sstevel@tonic-gate * This should not occur, but we are trying to 318*7c478bd9Sstevel@tonic-gate * write past the end of the buffer. return 319*7c478bd9Sstevel@tonic-gate * with an error. 320*7c478bd9Sstevel@tonic-gate */ 321*7c478bd9Sstevel@tonic-gate sz_read = -1; 322*7c478bd9Sstevel@tonic-gate break; 323*7c478bd9Sstevel@tonic-gate } 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate /* 326*7c478bd9Sstevel@tonic-gate * Try to continue reading in case the data is being 327*7c478bd9Sstevel@tonic-gate * piped in. 328*7c478bd9Sstevel@tonic-gate */ 329*7c478bd9Sstevel@tonic-gate ret = h->bstr_read(h, &tio_iobs[i].iob_buf[sz_read], 330*7c478bd9Sstevel@tonic-gate (blksize - rem)); 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate if (ret < 0) { 333*7c478bd9Sstevel@tonic-gate sz_read = ret; 334*7c478bd9Sstevel@tonic-gate break; 335*7c478bd9Sstevel@tonic-gate } 336*7c478bd9Sstevel@tonic-gate 337*7c478bd9Sstevel@tonic-gate /* 338*7c478bd9Sstevel@tonic-gate * No more data. We need to make sure that we are 339*7c478bd9Sstevel@tonic-gate * aligned with the blocksize. so pad the rest of 340*7c478bd9Sstevel@tonic-gate * the buffer with 0s 341*7c478bd9Sstevel@tonic-gate */ 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate if (ret == 0) { 344*7c478bd9Sstevel@tonic-gate ret = blksize - rem; 345*7c478bd9Sstevel@tonic-gate (void) memset(&tio_iobs[i].iob_buf[sz_read], 346*7c478bd9Sstevel@tonic-gate 0, ret); 347*7c478bd9Sstevel@tonic-gate } 348*7c478bd9Sstevel@tonic-gate sz_read += ret; 349*7c478bd9Sstevel@tonic-gate } 350*7c478bd9Sstevel@tonic-gate 351*7c478bd9Sstevel@tonic-gate if (sz_read < 0) { 352*7c478bd9Sstevel@tonic-gate 353*7c478bd9Sstevel@tonic-gate /* reading the source failed, clean up and return */ 354*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_SYSTEM; 355*7c478bd9Sstevel@tonic-gate te->te_errno = errno; 356*7c478bd9Sstevel@tonic-gate goto write_track_failed; 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate 359*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_start_blk = start_b; 360*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_nblks = (sz_read/blksize); 361*7c478bd9Sstevel@tonic-gate start_b += tio_iobs[i].iob_nblks; 362*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_data_size = sz_read; 363*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_READY; 364*7c478bd9Sstevel@tonic-gate if (sz_read == 0) 365*7c478bd9Sstevel@tonic-gate break; 366*7c478bd9Sstevel@tonic-gate } 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate tio_fd = dev->d_fd; 369*7c478bd9Sstevel@tonic-gate tio_trackno = ti->ti_track_no; 370*7c478bd9Sstevel@tonic-gate 371*7c478bd9Sstevel@tonic-gate /* Install signal handler for CTRL-C */ 372*7c478bd9Sstevel@tonic-gate ohandler = signal(SIGINT, trackio_sig_handler); 373*7c478bd9Sstevel@tonic-gate if (ohandler) { 374*7c478bd9Sstevel@tonic-gate signal_handler_installed = 1; 375*7c478bd9Sstevel@tonic-gate } 376*7c478bd9Sstevel@tonic-gate 377*7c478bd9Sstevel@tonic-gate /* Create thread which will issue commands to write to device */ 378*7c478bd9Sstevel@tonic-gate if (thr_create(0, 0, write_to_cd, NULL, 379*7c478bd9Sstevel@tonic-gate THR_BOUND | THR_NEW_LWP, &tio_thread) != 0) { 380*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_SYSTEM; 381*7c478bd9Sstevel@tonic-gate te->te_errno = errno; 382*7c478bd9Sstevel@tonic-gate goto write_track_failed; 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate write_cd_thr_created = 1; 385*7c478bd9Sstevel@tonic-gate 386*7c478bd9Sstevel@tonic-gate /* If caller specified a callback, create a thread to do callbacks */ 387*7c478bd9Sstevel@tonic-gate if (cb != NULL) { 388*7c478bd9Sstevel@tonic-gate init_pcb_data(); 389*7c478bd9Sstevel@tonic-gate pcb_cb = cb; 390*7c478bd9Sstevel@tonic-gate pcb_arg = arg; 391*7c478bd9Sstevel@tonic-gate if (thr_create(0, 0, progress_callback, NULL, 392*7c478bd9Sstevel@tonic-gate THR_BOUND | THR_NEW_LWP, &pc_thread) != 0) { 393*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_SYSTEM; 394*7c478bd9Sstevel@tonic-gate te->te_errno = errno; 395*7c478bd9Sstevel@tonic-gate goto write_track_failed; 396*7c478bd9Sstevel@tonic-gate } 397*7c478bd9Sstevel@tonic-gate progress_callback_thr_created = 1; 398*7c478bd9Sstevel@tonic-gate } 399*7c478bd9Sstevel@tonic-gate 400*7c478bd9Sstevel@tonic-gate i = 0; 401*7c478bd9Sstevel@tonic-gate while (sz_read != 0) { 402*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 403*7c478bd9Sstevel@tonic-gate while ((tio_iobs[i].iob_state != IOBS_EMPTY) && 404*7c478bd9Sstevel@tonic-gate (tio_errno == 0) && (pcb_user_abort == 0)) { 405*7c478bd9Sstevel@tonic-gate 406*7c478bd9Sstevel@tonic-gate /* Do callbacks only if there is nothing else to do */ 407*7c478bd9Sstevel@tonic-gate if (cb != NULL) { 408*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&pcb_mutex); 409*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&pcb_cond); 410*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate 413*7c478bd9Sstevel@tonic-gate /* If user requested abort, bail out */ 414*7c478bd9Sstevel@tonic-gate if (pcb_user_abort || tio_got_ctrl_c) { 415*7c478bd9Sstevel@tonic-gate break; 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate (void) cond_wait(&tio_cond, &tio_mutex); 418*7c478bd9Sstevel@tonic-gate } 419*7c478bd9Sstevel@tonic-gate if (pcb_user_abort || tio_got_ctrl_c) { 420*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 421*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_USER_ABORT; 422*7c478bd9Sstevel@tonic-gate goto write_track_failed; 423*7c478bd9Sstevel@tonic-gate } 424*7c478bd9Sstevel@tonic-gate /* 425*7c478bd9Sstevel@tonic-gate * We've got a transport error, stop writing, save all 426*7c478bd9Sstevel@tonic-gate * of the error information and clean up the threads. 427*7c478bd9Sstevel@tonic-gate */ 428*7c478bd9Sstevel@tonic-gate if (tio_errno != 0) { 429*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 430*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_TRANSPORT; 431*7c478bd9Sstevel@tonic-gate te->te_errno = tio_errno; 432*7c478bd9Sstevel@tonic-gate te->status = uscsi_status; 433*7c478bd9Sstevel@tonic-gate if (uscsi_status == 2) { 434*7c478bd9Sstevel@tonic-gate te->key = SENSE_KEY(rqbuf) & 0xf; 435*7c478bd9Sstevel@tonic-gate te->asc = ASC(rqbuf); 436*7c478bd9Sstevel@tonic-gate te->ascq = ASCQ(rqbuf); 437*7c478bd9Sstevel@tonic-gate } 438*7c478bd9Sstevel@tonic-gate goto write_track_failed; 439*7c478bd9Sstevel@tonic-gate } 440*7c478bd9Sstevel@tonic-gate pcb_completed_io_size += tio_iobs[i].iob_data_size; 441*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_UNDER_FILE_IO; 442*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate sz_read = h->bstr_read(h, tio_iobs[i].iob_buf, 445*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_total_size); 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate /* 448*7c478bd9Sstevel@tonic-gate * We need to read the source file into the buffer and make 449*7c478bd9Sstevel@tonic-gate * sure that the data in the buffer is a multiple of the 450*7c478bd9Sstevel@tonic-gate * blocksize (data or audio blocksize). this case should only 451*7c478bd9Sstevel@tonic-gate * be encountered at EOF or from piped input. 452*7c478bd9Sstevel@tonic-gate */ 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate while ((rem = (sz_read % blksize)) != 0) { 455*7c478bd9Sstevel@tonic-gate int ret; 456*7c478bd9Sstevel@tonic-gate 457*7c478bd9Sstevel@tonic-gate 458*7c478bd9Sstevel@tonic-gate /* 459*7c478bd9Sstevel@tonic-gate * This should not occur, we are trying to write 460*7c478bd9Sstevel@tonic-gate * past the end of the buffer, return error. 461*7c478bd9Sstevel@tonic-gate */ 462*7c478bd9Sstevel@tonic-gate 463*7c478bd9Sstevel@tonic-gate if ((sz_read + (blksize - rem)) > 464*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_total_size) { 465*7c478bd9Sstevel@tonic-gate 466*7c478bd9Sstevel@tonic-gate sz_read = -1; 467*7c478bd9Sstevel@tonic-gate break; 468*7c478bd9Sstevel@tonic-gate } 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate /* 471*7c478bd9Sstevel@tonic-gate * Try to continue reading in case the data is being 472*7c478bd9Sstevel@tonic-gate * piped in. 473*7c478bd9Sstevel@tonic-gate */ 474*7c478bd9Sstevel@tonic-gate 475*7c478bd9Sstevel@tonic-gate ret = h->bstr_read(h, &tio_iobs[i].iob_buf[sz_read], 476*7c478bd9Sstevel@tonic-gate (blksize - rem)); 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate if (ret < 0) { 479*7c478bd9Sstevel@tonic-gate sz_read = ret; 480*7c478bd9Sstevel@tonic-gate break; 481*7c478bd9Sstevel@tonic-gate } 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate /* 484*7c478bd9Sstevel@tonic-gate * No more data. We need to make sure that we are 485*7c478bd9Sstevel@tonic-gate * aligned with the blocksize. so pad the rest of 486*7c478bd9Sstevel@tonic-gate * the buffer with 0s 487*7c478bd9Sstevel@tonic-gate */ 488*7c478bd9Sstevel@tonic-gate 489*7c478bd9Sstevel@tonic-gate if (ret == 0) { 490*7c478bd9Sstevel@tonic-gate /* 491*7c478bd9Sstevel@tonic-gate * rem contains the amount of data past the 492*7c478bd9Sstevel@tonic-gate * previous block boundry. we need to subtract 493*7c478bd9Sstevel@tonic-gate * it from the blocksize to get the amount 494*7c478bd9Sstevel@tonic-gate * needed to reach the next block boundry. 495*7c478bd9Sstevel@tonic-gate */ 496*7c478bd9Sstevel@tonic-gate ret = blksize - rem; 497*7c478bd9Sstevel@tonic-gate (void) memset(&tio_iobs[i].iob_buf[sz_read], 498*7c478bd9Sstevel@tonic-gate 0, ret); 499*7c478bd9Sstevel@tonic-gate } 500*7c478bd9Sstevel@tonic-gate sz_read += ret; 501*7c478bd9Sstevel@tonic-gate } 502*7c478bd9Sstevel@tonic-gate if (sz_read < 0) { 503*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_SYSTEM; 504*7c478bd9Sstevel@tonic-gate te->te_errno = errno; 505*7c478bd9Sstevel@tonic-gate goto write_track_failed; 506*7c478bd9Sstevel@tonic-gate } 507*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 508*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_start_blk = start_b; 509*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_nblks = (sz_read/blksize); 510*7c478bd9Sstevel@tonic-gate start_b += tio_iobs[i].iob_nblks; 511*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_data_size = sz_read; 512*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_state = IOBS_READY; 513*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&tio_cond); 514*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 515*7c478bd9Sstevel@tonic-gate i++; 516*7c478bd9Sstevel@tonic-gate if (i == NIOBS) 517*7c478bd9Sstevel@tonic-gate i = 0; 518*7c478bd9Sstevel@tonic-gate } 519*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 520*7c478bd9Sstevel@tonic-gate while ((tio_errno == 0) && (tio_done == 0)) { 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate /* Wait for track IO to complete */ 523*7c478bd9Sstevel@tonic-gate (void) cond_wait(&tio_cond, &tio_mutex); 524*7c478bd9Sstevel@tonic-gate if (tio_errno != 0) { 525*7c478bd9Sstevel@tonic-gate te->err_type = TRACKIO_ERR_TRANSPORT; 526*7c478bd9Sstevel@tonic-gate te->te_errno = tio_errno; 527*7c478bd9Sstevel@tonic-gate te->status = uscsi_status; 528*7c478bd9Sstevel@tonic-gate if (uscsi_status == 2) { 529*7c478bd9Sstevel@tonic-gate te->key = SENSE_KEY(rqbuf) & 0xf; 530*7c478bd9Sstevel@tonic-gate te->asc = ASC(rqbuf); 531*7c478bd9Sstevel@tonic-gate te->ascq = ASCQ(rqbuf); 532*7c478bd9Sstevel@tonic-gate } 533*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 534*7c478bd9Sstevel@tonic-gate goto write_track_failed; 535*7c478bd9Sstevel@tonic-gate } 536*7c478bd9Sstevel@tonic-gate if (cb != NULL) { 537*7c478bd9Sstevel@tonic-gate while (tio_iobs[i].iob_state == IOBS_EMPTY) { 538*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&pcb_mutex); 539*7c478bd9Sstevel@tonic-gate pcb_completed_io_size += 540*7c478bd9Sstevel@tonic-gate tio_iobs[i].iob_data_size; 541*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&pcb_cond); 542*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 543*7c478bd9Sstevel@tonic-gate i++; 544*7c478bd9Sstevel@tonic-gate if (i == NIOBS) 545*7c478bd9Sstevel@tonic-gate i = 0; 546*7c478bd9Sstevel@tonic-gate } 547*7c478bd9Sstevel@tonic-gate } 548*7c478bd9Sstevel@tonic-gate } 549*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 550*7c478bd9Sstevel@tonic-gate retval = 1; 551*7c478bd9Sstevel@tonic-gate write_track_failed: 552*7c478bd9Sstevel@tonic-gate if (progress_callback_thr_created) { 553*7c478bd9Sstevel@tonic-gate if (thr_kill(pc_thread, 0) == 0) { 554*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&pcb_mutex); 555*7c478bd9Sstevel@tonic-gate 556*7c478bd9Sstevel@tonic-gate pcb_done = 1; 557*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&pcb_cond); 558*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&pcb_mutex); 559*7c478bd9Sstevel@tonic-gate (void) thr_join(pc_thread, NULL, NULL); 560*7c478bd9Sstevel@tonic-gate } 561*7c478bd9Sstevel@tonic-gate } 562*7c478bd9Sstevel@tonic-gate if (write_cd_thr_created) { 563*7c478bd9Sstevel@tonic-gate if (thr_kill(tio_thread, 0) == 0) { 564*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tio_mutex); 565*7c478bd9Sstevel@tonic-gate tio_abort = 1; 566*7c478bd9Sstevel@tonic-gate (void) cond_broadcast(&tio_cond); 567*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tio_mutex); 568*7c478bd9Sstevel@tonic-gate (void) thr_join(tio_thread, NULL, NULL); 569*7c478bd9Sstevel@tonic-gate } 570*7c478bd9Sstevel@tonic-gate } 571*7c478bd9Sstevel@tonic-gate 572*7c478bd9Sstevel@tonic-gate if (signal_handler_installed) { 573*7c478bd9Sstevel@tonic-gate (void) signal(SIGINT, ohandler); 574*7c478bd9Sstevel@tonic-gate } 575*7c478bd9Sstevel@tonic-gate 576*7c478bd9Sstevel@tonic-gate fini_tio_data(); 577*7c478bd9Sstevel@tonic-gate fini_pcb_data(); 578*7c478bd9Sstevel@tonic-gate return (retval); 579*7c478bd9Sstevel@tonic-gate } 580