Skip to content

Commit 65c26a0

Browse files
doug-gilbertChristoph Hellwig
authored andcommitted
sg: relax 16 byte cdb restriction
- remove the 16 byte CDB (SCSI command) length limit from the sg driver by handling longer CDBs the same way as the bsg driver. Remove comment from sg.h public interface about the cmd_len field being limited to 16 bytes. - remove some dead code caused by this change - cleanup comment block at the top of sg.h, fix urls Signed-off-by: Douglas Gilbert <dgilbert@interlog.com> Reviewed-by: Mike Christie <michaelc@cs.wisc.edu> Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent bcdb247 commit 65c26a0

2 files changed

Lines changed: 53 additions & 84 deletions

File tree

‎drivers/scsi/sg.c‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
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
@@ -18,11 +16,11 @@
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

6967
static int sg_proc_init(void);
7068
static 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

‎include/scsi/sg.h‎

Lines changed: 24 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,34 @@
44
#include <linux/compiler.h>
55

66
/*
7-
History:
8-
Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
9-
process control of SCSI devices.
10-
Development Sponsored by Killy Corp. NY NY
11-
Original driver (sg.h):
12-
* Copyright (C) 1992 Lawrence Foard
13-
Version 2 and 3 extensions to driver:
14-
* Copyright (C) 1998 - 2006 Douglas Gilbert
15-
16-
Version: 3.5.34 (20060920)
17-
This version is for 2.6 series kernels.
18-
19-
For a full changelog see http://www.torque.net/sg
20-
21-
Map of SG verions to the Linux kernels in which they appear:
22-
---------- ----------------------------------
23-
original all kernels < 2.2.6
24-
2.1.40 2.2.20
25-
3.0.x optional version 3 sg driver for 2.2 series
26-
3.1.17++ 2.4.0++
27-
3.5.30++ 2.6.0++
28-
29-
Major new features in SG 3.x driver (cf SG 2.x drivers)
30-
- SG_IO ioctl() combines function if write() and read()
31-
- new interface (sg_io_hdr_t) but still supports old interface
32-
- scatter/gather in user space, direct IO, and mmap supported
33-
34-
The normal action of this driver is to use the adapter (HBA) driver to DMA
35-
data into kernel buffers and then use the CPU to copy the data into the
36-
user space (vice versa for writes). That is called "indirect" IO due to
37-
the double handling of data. There are two methods offered to remove the
38-
redundant copy: 1) direct IO and 2) using the mmap() system call to map
39-
the reserve buffer (this driver has one reserve buffer per fd) into the
40-
user space. Both have their advantages.
41-
In terms of absolute speed mmap() is faster. If speed is not a concern,
42-
indirect IO should be fine. Read the documentation for more information.
43-
44-
** N.B. To use direct IO 'echo 1 > /proc/scsi/sg/allow_dio' or
45-
'echo 1 > /sys/module/sg/parameters/allow_dio' is needed.
46-
That attribute is 0 by default. **
47-
48-
Historical note: this SCSI pass-through driver has been known as "sg" for
49-
a decade. In broader kernel discussions "sg" is used to refer to scatter
50-
gather techniques. The context should clarify which "sg" is referred to.
51-
52-
Documentation
53-
=============
54-
A web site for the SG device driver can be found at:
55-
http://www.torque.net/sg [alternatively check the MAINTAINERS file]
56-
The documentation for the sg version 3 driver can be found at:
57-
http://www.torque.net/sg/p/sg_v3_ho.html
58-
This is a rendering from DocBook source [change the extension to "sgml"
59-
or "xml"]. There are renderings in "ps", "pdf", "rtf" and "txt" (soon).
60-
The SG_IO ioctl is now found in other parts kernel (e.g. the block layer).
61-
For more information see http://www.torque.net/sg/sg_io.html
62-
63-
The older, version 2 documents discuss the original sg interface in detail:
64-
http://www.torque.net/sg/p/scsi-generic.txt
65-
http://www.torque.net/sg/p/scsi-generic_long.txt
66-
Also available: <kernel_source>/Documentation/scsi/scsi-generic.txt
67-
68-
Utility and test programs are available at the sg web site. They are
69-
packaged as sg3_utils (for the lk 2.4 and 2.6 series) and sg_utils
70-
(for the lk 2.2 series).
71-
*/
7+
* History:
8+
* Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user
9+
* process control of SCSI devices.
10+
* Development Sponsored by Killy Corp. NY NY
11+
*
12+
* Original driver (sg.h):
13+
* Copyright (C) 1992 Lawrence Foard
14+
* Version 2 and 3 extensions to driver:
15+
* Copyright (C) 1998 - 2014 Douglas Gilbert
16+
*
17+
* Version: 3.5.36 (20140603)
18+
* This version is for 2.6 and 3 series kernels.
19+
*
20+
* Documentation
21+
* =============
22+
* A web site for the SG device driver can be found at:
23+
* http://sg.danny.cz/sg [alternatively check the MAINTAINERS file]
24+
* The documentation for the sg version 3 driver can be found at:
25+
* http://sg.danny.cz/sg/p/sg_v3_ho.html
26+
* Also see: <kernel_source>/Documentation/scsi/scsi-generic.txt
27+
*
28+
* For utility and test programs see: http://sg.danny.cz/sg/sg3_utils.html
29+
*/
7230

7331
#ifdef __KERNEL__
7432
extern int sg_big_buff; /* for sysctl */
7533
#endif
7634

77-
/* New interface introduced in the 3.x SG drivers follows */
7835

7936
typedef struct sg_iovec /* same structure as used by readv() Linux system */
8037
{ /* call. It defines one scatter-gather element. */
@@ -87,7 +44,7 @@ typedef struct sg_io_hdr
8744
{
8845
int interface_id; /* [i] 'S' for SCSI generic (required) */
8946
int dxfer_direction; /* [i] data transfer direction */
90-
unsigned char cmd_len; /* [i] SCSI command length ( <= 16 bytes) */
47+
unsigned char cmd_len; /* [i] SCSI command length */
9148
unsigned char mx_sb_len; /* [i] max length to write to sbp */
9249
unsigned short iovec_count; /* [i] 0 implies no scatter gather */
9350
unsigned int dxfer_len; /* [i] byte count of data transfer */

0 commit comments

Comments
 (0)