@@ -350,16 +350,51 @@ static int sg_io(struct file *file, request_queue_t *q,
350350 return ret ;
351351}
352352
353+ /**
354+ * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
355+ * @file: file this ioctl operates on (optional)
356+ * @q: request queue to send scsi commands down
357+ * @disk: gendisk to operate on (option)
358+ * @sic: userspace structure describing the command to perform
359+ *
360+ * Send down the scsi command described by @sic to the device below
361+ * the request queue @q. If @file is non-NULL it's used to perform
362+ * fine-grained permission checks that allow users to send down
363+ * non-destructive SCSI commands. If the caller has a struct gendisk
364+ * available it should be passed in as @disk to allow the low level
365+ * driver to use the information contained in it. A non-NULL @disk
366+ * is only allowed if the caller knows that the low level driver doesn't
367+ * need it (e.g. in the scsi subsystem).
368+ *
369+ * Notes:
370+ * - This interface is deprecated - users should use the SG_IO
371+ * interface instead, as this is a more flexible approach to
372+ * performing SCSI commands on a device.
373+ * - The SCSI command length is determined by examining the 1st byte
374+ * of the given command. There is no way to override this.
375+ * - Data transfers are limited to PAGE_SIZE
376+ * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
377+ * accommodate the sense buffer when an error occurs.
378+ * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
379+ * old code will not be surprised.
380+ * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
381+ * a negative return and the Unix error code in 'errno'.
382+ * If the SCSI command succeeds then 0 is returned.
383+ * Positive numbers returned are the compacted SCSI error codes (4
384+ * bytes in one int) where the lowest byte is the SCSI status.
385+ */
353386#define OMAX_SB_LEN 16 /* For backward compatibility */
354-
355- static int sg_scsi_ioctl (struct file * file , request_queue_t * q ,
356- struct gendisk * bd_disk , Scsi_Ioctl_Command __user * sic )
387+ int sg_scsi_ioctl (struct file * file , struct request_queue * q ,
388+ struct gendisk * disk , struct scsi_ioctl_command __user * sic )
357389{
358390 struct request * rq ;
359391 int err ;
360392 unsigned int in_len , out_len , bytes , opcode , cmdlen ;
361393 char * buffer = NULL , sense [SCSI_SENSE_BUFFERSIZE ];
362394
395+ if (!sic )
396+ return - EINVAL ;
397+
363398 /*
364399 * get in an out lengths, verify they don't exceed a page worth of data
365400 */
@@ -393,45 +428,53 @@ static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
393428 if (copy_from_user (rq -> cmd , sic -> data , cmdlen ))
394429 goto error ;
395430
396- if (copy_from_user (buffer , sic -> data + cmdlen , in_len ))
431+ if (in_len && copy_from_user (buffer , sic -> data + cmdlen , in_len ))
397432 goto error ;
398433
399434 err = verify_command (file , rq -> cmd );
400435 if (err )
401436 goto error ;
402437
438+ /* default. possible overriden later */
439+ rq -> retries = 5 ;
440+
403441 switch (opcode ) {
404- case SEND_DIAGNOSTIC :
405- case FORMAT_UNIT :
406- rq -> timeout = FORMAT_UNIT_TIMEOUT ;
407- break ;
408- case START_STOP :
409- rq -> timeout = START_STOP_TIMEOUT ;
410- break ;
411- case MOVE_MEDIUM :
412- rq -> timeout = MOVE_MEDIUM_TIMEOUT ;
413- break ;
414- case READ_ELEMENT_STATUS :
415- rq -> timeout = READ_ELEMENT_STATUS_TIMEOUT ;
416- break ;
417- case READ_DEFECT_DATA :
418- rq -> timeout = READ_DEFECT_DATA_TIMEOUT ;
419- break ;
420- default :
421- rq -> timeout = BLK_DEFAULT_TIMEOUT ;
422- break ;
442+ case SEND_DIAGNOSTIC :
443+ case FORMAT_UNIT :
444+ rq -> timeout = FORMAT_UNIT_TIMEOUT ;
445+ rq -> retries = 1 ;
446+ break ;
447+ case START_STOP :
448+ rq -> timeout = START_STOP_TIMEOUT ;
449+ break ;
450+ case MOVE_MEDIUM :
451+ rq -> timeout = MOVE_MEDIUM_TIMEOUT ;
452+ break ;
453+ case READ_ELEMENT_STATUS :
454+ rq -> timeout = READ_ELEMENT_STATUS_TIMEOUT ;
455+ break ;
456+ case READ_DEFECT_DATA :
457+ rq -> timeout = READ_DEFECT_DATA_TIMEOUT ;
458+ rq -> retries = 1 ;
459+ break ;
460+ default :
461+ rq -> timeout = BLK_DEFAULT_TIMEOUT ;
462+ break ;
463+ }
464+
465+ if (bytes && blk_rq_map_kern (q , rq , buffer , bytes , __GFP_WAIT )) {
466+ err = DRIVER_ERROR << 24 ;
467+ goto out ;
423468 }
424469
425470 memset (sense , 0 , sizeof (sense ));
426471 rq -> sense = sense ;
427472 rq -> sense_len = 0 ;
428-
429- rq -> data = buffer ;
430- rq -> data_len = bytes ;
431473 rq -> flags |= REQ_BLOCK_PC ;
432- rq -> retries = 0 ;
433474
434- blk_execute_rq (q , bd_disk , rq , 0 );
475+ blk_execute_rq (q , disk , rq , 0 );
476+
477+ out :
435478 err = rq -> errors & 0xff ; /* only 8 bit SCSI status */
436479 if (err ) {
437480 if (rq -> sense_len && rq -> sense ) {
@@ -450,7 +493,7 @@ static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
450493 blk_put_request (rq );
451494 return err ;
452495}
453-
496+ EXPORT_SYMBOL_GPL ( sg_scsi_ioctl );
454497
455498/* Send basic block requests */
456499static int __blk_send_generic (request_queue_t * q , struct gendisk * bd_disk , int cmd , int data )
0 commit comments