77 * Original driver (sg.c):
88 * Copyright (C) 1992 Lawrence Foard
99 * Version 2 and 3 extensions to driver:
10- * Copyright (C) 1998 - 2005 Douglas Gilbert
11- *
12- * Modified 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Devfs support
10+ * Copyright (C) 1998 - 2014 Douglas Gilbert
1311 *
1412 * This program is free software; you can redistribute it and/or modify
1513 * it under the terms of the GNU General Public License as published by
1816 *
1917 */
2018
21- static int sg_version_num = 30534 ; /* 2 digits for each component */
22- #define SG_VERSION_STR "3.5.34 "
19+ static int sg_version_num = 30536 ; /* 2 digits for each component */
20+ #define SG_VERSION_STR "3.5.36 "
2321
2422/*
25- * D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au ), notes:
23+ * D. P. Gilbert (dgilbert@interlog.com), notes:
2624 * - scsi logging is available via SCSI_LOG_TIMEOUT macros. First
2725 * the kernel/module needs to be built with CONFIG_SCSI_LOGGING
2826 * (otherwise the macros compile to empty statements).
@@ -64,7 +62,7 @@ static int sg_version_num = 30534; /* 2 digits for each component */
6462
6563#ifdef CONFIG_SCSI_PROC_FS
6664#include <linux/proc_fs.h>
67- static char * sg_version_date = "20061027 " ;
65+ static char * sg_version_date = "20140603 " ;
6866
6967static int sg_proc_init (void );
7068static void sg_proc_cleanup (void );
@@ -74,6 +72,12 @@ static void sg_proc_cleanup(void);
7472
7573#define SG_MAX_DEVS 32768
7674
75+ /* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type
76+ * of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater
77+ * than 16 bytes are "variable length" whose length is a multiple of 4
78+ */
79+ #define SG_MAX_CDB_SIZE 252
80+
7781/*
7882 * Suppose you want to calculate the formula muldiv(x,m,d)=int(x * m / d)
7983 * Then when using 32 bit integers x * m may overflow during the calculation.
@@ -161,7 +165,7 @@ typedef struct sg_fd { /* holds the state of a file descriptor */
161165 char low_dma ; /* as in parent but possibly overridden to 1 */
162166 char force_packid ; /* 1 -> pack_id input to read(), 0 -> ignored */
163167 char cmd_q ; /* 1 -> allow command queuing, 0 -> don't */
164- char next_cmd_len ; /* 0 -> automatic (def) , >0 -> use on next write() */
168+ unsigned char next_cmd_len ; /* 0: automatic, >0: use on next write() */
165169 char keep_orphan ; /* 0 -> drop orphan (def), 1 -> keep for read() */
166170 char mmap_called ; /* 0 -> mmap() never called on this fd */
167171 struct kref f_ref ;
@@ -566,7 +570,7 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
566570 Sg_request * srp ;
567571 struct sg_header old_hdr ;
568572 sg_io_hdr_t * hp ;
569- unsigned char cmnd [MAX_COMMAND_SIZE ];
573+ unsigned char cmnd [SG_MAX_CDB_SIZE ];
570574
571575 if ((!(sfp = (Sg_fd * ) filp -> private_data )) || (!(sdp = sfp -> parentdp )))
572576 return - ENXIO ;
@@ -598,12 +602,6 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
598602 buf += SZ_SG_HEADER ;
599603 __get_user (opcode , buf );
600604 if (sfp -> next_cmd_len > 0 ) {
601- if (sfp -> next_cmd_len > MAX_COMMAND_SIZE ) {
602- SCSI_LOG_TIMEOUT (1 , printk ("sg_write: command length too long\n" ));
603- sfp -> next_cmd_len = 0 ;
604- sg_remove_request (sfp , srp );
605- return - EIO ;
606- }
607605 cmd_size = sfp -> next_cmd_len ;
608606 sfp -> next_cmd_len = 0 ; /* reset so only this write() effected */
609607 } else {
@@ -675,7 +673,7 @@ sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
675673 int k ;
676674 Sg_request * srp ;
677675 sg_io_hdr_t * hp ;
678- unsigned char cmnd [MAX_COMMAND_SIZE ];
676+ unsigned char cmnd [SG_MAX_CDB_SIZE ];
679677 int timeout ;
680678 unsigned long ul_timeout ;
681679
@@ -1654,15 +1652,27 @@ static int sg_start_req(Sg_request *srp, unsigned char *cmd)
16541652 struct request_queue * q = sfp -> parentdp -> device -> request_queue ;
16551653 struct rq_map_data * md , map_data ;
16561654 int rw = hp -> dxfer_direction == SG_DXFER_TO_DEV ? WRITE : READ ;
1655+ unsigned char * long_cmdp = NULL ;
16571656
16581657 SCSI_LOG_TIMEOUT (4 , printk (KERN_INFO "sg_start_req: dxfer_len=%d\n" ,
16591658 dxfer_len ));
16601659
1660+ if (hp -> cmd_len > BLK_MAX_CDB ) {
1661+ long_cmdp = kzalloc (hp -> cmd_len , GFP_KERNEL );
1662+ if (!long_cmdp )
1663+ return - ENOMEM ;
1664+ }
1665+
16611666 rq = blk_get_request (q , rw , GFP_ATOMIC );
1662- if (!rq )
1667+ if (!rq ) {
1668+ kfree (long_cmdp );
16631669 return - ENOMEM ;
1670+ }
16641671
16651672 blk_rq_set_block_pc (rq );
1673+
1674+ if (hp -> cmd_len > BLK_MAX_CDB )
1675+ rq -> cmd = long_cmdp ;
16661676 memcpy (rq -> cmd , cmd , hp -> cmd_len );
16671677 rq -> cmd_len = hp -> cmd_len ;
16681678
@@ -1747,6 +1757,8 @@ static int sg_finish_rem_req(Sg_request * srp)
17471757 if (srp -> bio )
17481758 ret = blk_rq_unmap_user (srp -> bio );
17491759
1760+ if (srp -> rq -> cmd != srp -> rq -> __cmd )
1761+ kfree (srp -> rq -> cmd );
17501762 blk_put_request (srp -> rq );
17511763 }
17521764
0 commit comments