| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* History: |
| 4 |
* Started: Aug 9 by Lawrence Foard ([email protected]), |
| 5 |
* to allow user process control of SCSI devices. |
| 6 |
* Development Sponsored by Killy Corp. NY NY |
| 7 |
* |
| 8 |
* Original driver (sg.c): |
| 9 |
* Copyright (C) 1992 Lawrence Foard |
| 10 |
* Version 2 and 3 extensions to driver: |
| 11 |
* Copyright (C) 1998 - 2014 Douglas Gilbert |
| 12 |
*/ |
| 13 |
|
| 14 |
static int sg_version_num = 30536; /* 2 digits for each component */ |
| 15 |
#define SG_VERSION_STR "3.5.36" |
| 16 |
|
| 17 |
/* |
| 18 |
* D. P. Gilbert ([email protected]), notes: |
| 19 |
* - scsi logging is available via SCSI_LOG_TIMEOUT macros. First |
| 20 |
* the kernel/module needs to be built with CONFIG_SCSI_LOGGING |
| 21 |
* (otherwise the macros compile to empty statements). |
| 22 |
* |
| 23 |
*/ |
| 24 |
#include <linux/module.h> |
| 25 |
|
| 26 |
#include <linux/fs.h> |
| 27 |
#include <linux/kernel.h> |
| 28 |
#include <linux/sched.h> |
| 29 |
#include <linux/string.h> |
| 30 |
#include <linux/mm.h> |
| 31 |
#include <linux/errno.h> |
| 32 |
#include <linux/mtio.h> |
| 33 |
#include <linux/ioctl.h> |
| 34 |
#include <linux/major.h> |
| 35 |
#include <linux/slab.h> |
| 36 |
#include <linux/fcntl.h> |
| 37 |
#include <linux/init.h> |
| 38 |
#include <linux/poll.h> |
| 39 |
#include <linux/moduleparam.h> |
| 40 |
#include <linux/cdev.h> |
| 41 |
#include <linux/idr.h> |
| 42 |
#include <linux/seq_file.h> |
| 43 |
#include <linux/blkdev.h> |
| 44 |
#include <linux/delay.h> |
| 45 |
#include <linux/blktrace_api.h> |
| 46 |
#include <linux/mutex.h> |
| 47 |
#include <linux/atomic.h> |
| 48 |
#include <linux/ratelimit.h> |
| 49 |
#include <linux/uio.h> |
| 50 |
#include <linux/cred.h> /* for sg_check_file_access() */ |
| 51 |
|
| 52 |
#include <scsi/scsi.h> |
| 53 |
#include <scsi/scsi_cmnd.h> |
| 54 |
#include <scsi/scsi_dbg.h> |
| 55 |
#include <scsi/scsi_device.h> |
| 56 |
#include <scsi/scsi_driver.h> |
| 57 |
#include <scsi/scsi_eh.h> |
| 58 |
#include <scsi/scsi_host.h> |
| 59 |
#include <scsi/scsi_ioctl.h> |
| 60 |
#include <scsi/scsi_tcq.h> |
| 61 |
#include <scsi/sg.h> |
| 62 |
|
| 63 |
#include "scsi_logging.h" |
| 64 |
|
| 65 |
#ifdef CONFIG_SCSI_PROC_FS |
| 66 |
#include <linux/proc_fs.h> |
| 67 |
static char *sg_version_date = "20140603"; |
| 68 |
|
| 69 |
static int sg_proc_init(void); |
| 70 |
#endif |
| 71 |
|
| 72 |
#define SG_ALLOW_DIO_DEF 0 |
| 73 |
|
| 74 |
#define SG_MAX_DEVS (1 << MINORBITS) |
| 75 |
|
| 76 |
/* SG_MAX_CDB_SIZE should be 260 (spc4r37 section 3.1.30) however the type |
| 77 |
* of sg_io_hdr::cmd_len can only represent 255. All SCSI commands greater |
| 78 |
* than 16 bytes are "variable length" whose length is a multiple of 4 |
| 79 |
*/ |
| 80 |
#define SG_MAX_CDB_SIZE 252 |
| 81 |
|
| 82 |
#define SG_DEFAULT_TIMEOUT mult_frac(SG_DEFAULT_TIMEOUT_USER, HZ, USER_HZ) |
| 83 |
|
| 84 |
static int sg_big_buff = SG_DEF_RESERVED_SIZE; |
| 85 |
/* N.B. This variable is readable and writeable via |
| 86 |
/proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer |
| 87 |
of this size (or less if there is not enough memory) will be reserved |
| 88 |
for use by this file descriptor. [Deprecated usage: this variable is also |
| 89 |
readable via /proc/sys/kernel/sg-big-buff if the sg driver is built into |
| 90 |
the kernel (i.e. it is not a module).] */ |
| 91 |
static int def_reserved_size = -1; /* picks up init parameter */ |
| 92 |
static int sg_allow_dio = SG_ALLOW_DIO_DEF; |
| 93 |
|
| 94 |
static int scatter_elem_sz = SG_SCATTER_SZ; |
| 95 |
static int scatter_elem_sz_prev = SG_SCATTER_SZ; |
| 96 |
|
| 97 |
#define SG_SECTOR_SZ 512 |
| 98 |
|
| 99 |
static int sg_add_device(struct device *); |
| 100 |
static void sg_remove_device(struct device *); |
| 101 |
|
| 102 |
static DEFINE_IDR(sg_index_idr); |
| 103 |
static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock |
| 104 |
file descriptor list for device */ |
| 105 |
|
| 106 |
static struct class_interface sg_interface = { |
| 107 |
.add_dev = sg_add_device, |
| 108 |
.remove_dev = sg_remove_device, |
| 109 |
}; |
| 110 |
|
| 111 |
typedef struct sg_scatter_hold { /* holding area for scsi scatter gather info */ |
| 112 |
unsigned short k_use_sg; /* Count of kernel scatter-gather pieces */ |
| 113 |
unsigned sglist_len; /* size of malloc'd scatter-gather list ++ */ |
| 114 |
unsigned bufflen; /* Size of (aggregate) data buffer */ |
| 115 |
struct page **pages; |
| 116 |
int page_order; |
| 117 |
char dio_in_use; /* 0->indirect IO (or mmap), 1->dio */ |
| 118 |
unsigned char cmd_opcode; /* first byte of command */ |
| 119 |
} Sg_scatter_hold; |
| 120 |
|
| 121 |
struct sg_device; /* forward declarations */ |
| 122 |
struct sg_fd; |
| 123 |
|
| 124 |
typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */ |
| 125 |
struct list_head entry; /* list entry */ |
| 126 |
struct sg_fd *parentfp; /* NULL -> not in use */ |
| 127 |
Sg_scatter_hold data; /* hold buffer, perhaps scatter list */ |
| 128 |
sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */ |
| 129 |
unsigned char sense_b[SCSI_SENSE_BUFFERSIZE]; |
| 130 |
char res_used; /* 1 -> using reserve buffer, 0 -> not ... */ |
| 131 |
char orphan; /* 1 -> drop on sight, 0 -> normal */ |
| 132 |
char sg_io_owned; /* 1 -> packet belongs to SG_IO */ |
| 133 |
/* done protected by rq_list_lock */ |
| 134 |
char done; /* 0->before bh, 1->before read, 2->read */ |
| 135 |
struct request *rq; |
| 136 |
struct bio *bio; |
| 137 |
struct execute_work ew; |
| 138 |
} Sg_request; |
| 139 |
|
| 140 |
typedef struct sg_fd { /* holds the state of a file descriptor */ |
| 141 |
struct list_head sfd_siblings; /* protected by device's sfd_lock */ |
| 142 |
struct sg_device *parentdp; /* owning device */ |
| 143 |
wait_queue_head_t read_wait; /* queue read until command done */ |
| 144 |
rwlock_t rq_list_lock; /* protect access to list in req_arr */ |
| 145 |
struct mutex f_mutex; /* protect against changes in this fd */ |
| 146 |
int timeout; /* defaults to SG_DEFAULT_TIMEOUT */ |
| 147 |
int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */ |
| 148 |
Sg_scatter_hold reserve; /* buffer held for this file descriptor */ |
| 149 |
struct list_head rq_list; /* head of request list */ |
| 150 |
struct fasync_struct *async_qp; /* used by asynchronous notification */ |
| 151 |
Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */ |
| 152 |
char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */ |
| 153 |
char cmd_q; /* 1 -> allow command queuing, 0 -> don't */ |
| 154 |
unsigned char next_cmd_len; /* 0: automatic, >0: use on next write() */ |
| 155 |
char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */ |
| 156 |
char mmap_called; /* 0 -> mmap() never called on this fd */ |
| 157 |
char res_in_use; /* 1 -> 'reserve' array in use */ |
| 158 |
struct kref f_ref; |
| 159 |
struct execute_work ew; |
| 160 |
} Sg_fd; |
| 161 |
|
| 162 |
typedef struct sg_device { /* holds the state of each scsi generic device */ |
| 163 |
struct scsi_device *device; |
| 164 |
wait_queue_head_t open_wait; /* queue open() when O_EXCL present */ |
| 165 |
struct mutex open_rel_lock; /* held when in open() or release() */ |
| 166 |
int sg_tablesize; /* adapter's max scatter-gather table size */ |
| 167 |
u32 index; /* device index number */ |
| 168 |
struct list_head sfds; |
| 169 |
rwlock_t sfd_lock; /* protect access to sfd list */ |
| 170 |
atomic_t detaching; /* 0->device usable, 1->device detaching */ |
| 171 |
bool exclude; /* 1->open(O_EXCL) succeeded and is active */ |
| 172 |
int open_cnt; /* count of opens (perhaps < num(sfds) ) */ |
| 173 |
char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */ |
| 174 |
char name[DISK_NAME_LEN]; |
| 175 |
struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */ |
| 176 |
struct kref d_ref; |
| 177 |
} Sg_device; |
| 178 |
|
| 179 |
/* tasklet or soft irq callback */ |
| 180 |
static enum rq_end_io_ret sg_rq_end_io(struct request *rq, blk_status_t status, |
| 181 |
const struct io_comp_batch *iob); |
| 182 |
static int sg_start_req(Sg_request *srp, unsigned char *cmd); |
| 183 |
static int sg_finish_rem_req(Sg_request * srp); |
| 184 |
static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size); |
| 185 |
static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, |
| 186 |
Sg_request * srp); |
| 187 |
static ssize_t sg_new_write(Sg_fd *sfp, struct file *file, |
| 188 |
const char __user *buf, size_t count, int blocking, |
| 189 |
int read_only, int sg_io_owned, Sg_request **o_srp); |
| 190 |
static int sg_common_write(Sg_fd * sfp, Sg_request * srp, |
| 191 |
unsigned char *cmnd, int timeout, int blocking); |
| 192 |
static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer); |
| 193 |
static void sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp); |
| 194 |
static void sg_build_reserve(Sg_fd * sfp, int req_size); |
| 195 |
static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size); |
| 196 |
static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp); |
| 197 |
static Sg_fd *sg_add_sfp(Sg_device * sdp); |
| 198 |
static void sg_remove_sfp(struct kref *); |
| 199 |
static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id, bool *busy); |
| 200 |
static Sg_request *sg_add_request(Sg_fd * sfp); |
| 201 |
static int sg_remove_request(Sg_fd * sfp, Sg_request * srp); |
| 202 |
static Sg_device *sg_get_dev(int dev); |
| 203 |
static void sg_device_destroy(struct kref *kref); |
| 204 |
|
| 205 |
#define SZ_SG_HEADER sizeof(struct sg_header) |
| 206 |
#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t) |
| 207 |
#define SZ_SG_IOVEC sizeof(sg_iovec_t) |
| 208 |
#define SZ_SG_REQ_INFO sizeof(sg_req_info_t) |
| 209 |
|
| 210 |
#define sg_printk(prefix, sdp, fmt, a...) \ |
| 211 |
sdev_prefix_printk(prefix, (sdp)->device, (sdp)->name, fmt, ##a) |
| 212 |
|
| 213 |
/* |
| 214 |
* The SCSI interfaces that use read() and write() as an asynchronous variant of |
| 215 |
* ioctl(..., SG_IO, ...) are fundamentally unsafe, since there are lots of ways |
| 216 |
* to trigger read() and write() calls from various contexts with elevated |
| 217 |
* privileges. This can lead to kernel memory corruption (e.g. if these |
| 218 |
* interfaces are called through splice()) and privilege escalation inside |
| 219 |
* userspace (e.g. if a process with access to such a device passes a file |
| 220 |
* descriptor to a SUID binary as stdin/stdout/stderr). |
| 221 |
* |
| 222 |
* This function provides protection for the legacy API by restricting the |
| 223 |
* calling context. |
| 224 |
*/ |
| 225 |
static int sg_check_file_access(struct file *filp, const char *caller) |
| 226 |
{ |
| 227 |
if (filp->f_cred != current_real_cred()) { |
| 228 |
pr_err_once("%s: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n", |
| 229 |
caller, task_tgid_vnr(current), current->comm); |
| 230 |
return -EPERM; |
| 231 |
} |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
static int sg_allow_access(struct file *filp, unsigned char *cmd) |
| 236 |
{ |
| 237 |
struct sg_fd *sfp = filp->private_data; |
| 238 |
|
| 239 |
if (sfp->parentdp->device->type == TYPE_SCANNER) |
| 240 |
return 0; |
| 241 |
if (!scsi_cmd_allowed(cmd, filp->f_mode & FMODE_WRITE)) |
| 242 |
return -EPERM; |
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
static int |
| 247 |
open_wait(Sg_device *sdp, int flags) |
| 248 |
{ |
| 249 |
int retval = 0; |
| 250 |
|
| 251 |
if (flags & O_EXCL) { |
| 252 |
while (sdp->open_cnt > 0) { |
| 253 |
mutex_unlock(&sdp->open_rel_lock); |
| 254 |
retval = wait_event_interruptible(sdp->open_wait, |
| 255 |
(atomic_read(&sdp->detaching) || |
| 256 |
!sdp->open_cnt)); |
| 257 |
mutex_lock(&sdp->open_rel_lock); |
| 258 |
|
| 259 |
if (retval) /* -ERESTARTSYS */ |
| 260 |
return retval; |
| 261 |
if (atomic_read(&sdp->detaching)) |
| 262 |
return -ENODEV; |
| 263 |
} |
| 264 |
} else { |
| 265 |
while (sdp->exclude) { |
| 266 |
mutex_unlock(&sdp->open_rel_lock); |
| 267 |
retval = wait_event_interruptible(sdp->open_wait, |
| 268 |
(atomic_read(&sdp->detaching) || |
| 269 |
!sdp->exclude)); |
| 270 |
mutex_lock(&sdp->open_rel_lock); |
| 271 |
|
| 272 |
if (retval) /* -ERESTARTSYS */ |
| 273 |
return retval; |
| 274 |
if (atomic_read(&sdp->detaching)) |
| 275 |
return -ENODEV; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return retval; |
| 280 |
} |
| 281 |
|
| 282 |
/* Returns 0 on success, else a negated errno value */ |
| 283 |
static int |
| 284 |
sg_open(struct inode *inode, struct file *filp) |
| 285 |
{ |
| 286 |
int dev = iminor(inode); |
| 287 |
int flags = filp->f_flags; |
| 288 |
struct request_queue *q; |
| 289 |
struct scsi_device *device; |
| 290 |
Sg_device *sdp; |
| 291 |
Sg_fd *sfp; |
| 292 |
int retval; |
| 293 |
|
| 294 |
nonseekable_open(inode, filp); |
| 295 |
if ((flags & O_EXCL) && (O_RDONLY == (flags & O_ACCMODE))) |
| 296 |
return -EPERM; /* Can't lock it with read only access */ |
| 297 |
sdp = sg_get_dev(dev); |
| 298 |
if (IS_ERR(sdp)) |
| 299 |
return PTR_ERR(sdp); |
| 300 |
|
| 301 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 302 |
"sg_open: flags=0x%x\n", flags)); |
| 303 |
|
| 304 |
/* This driver's module count bumped by fops_get in <linux/fs.h> */ |
| 305 |
/* Prevent the device driver from vanishing while we sleep */ |
| 306 |
device = sdp->device; |
| 307 |
retval = scsi_device_get(device); |
| 308 |
if (retval) |
| 309 |
goto sg_put; |
| 310 |
|
| 311 |
/* scsi_block_when_processing_errors() may block so bypass |
| 312 |
* check if O_NONBLOCK. Permits SCSI commands to be issued |
| 313 |
* during error recovery. Tread carefully. */ |
| 314 |
if (!((flags & O_NONBLOCK) || |
| 315 |
scsi_block_when_processing_errors(device))) { |
| 316 |
retval = -ENXIO; |
| 317 |
/* we are in error recovery for this device */ |
| 318 |
goto sdp_put; |
| 319 |
} |
| 320 |
|
| 321 |
mutex_lock(&sdp->open_rel_lock); |
| 322 |
if (flags & O_NONBLOCK) { |
| 323 |
if (flags & O_EXCL) { |
| 324 |
if (sdp->open_cnt > 0) { |
| 325 |
retval = -EBUSY; |
| 326 |
goto error_mutex_locked; |
| 327 |
} |
| 328 |
} else { |
| 329 |
if (sdp->exclude) { |
| 330 |
retval = -EBUSY; |
| 331 |
goto error_mutex_locked; |
| 332 |
} |
| 333 |
} |
| 334 |
} else { |
| 335 |
retval = open_wait(sdp, flags); |
| 336 |
if (retval) /* -ERESTARTSYS or -ENODEV */ |
| 337 |
goto error_mutex_locked; |
| 338 |
} |
| 339 |
|
| 340 |
/* N.B. at this point we are holding the open_rel_lock */ |
| 341 |
if (flags & O_EXCL) |
| 342 |
sdp->exclude = true; |
| 343 |
|
| 344 |
if (sdp->open_cnt < 1) { /* no existing opens */ |
| 345 |
sdp->sgdebug = 0; |
| 346 |
q = device->request_queue; |
| 347 |
sdp->sg_tablesize = queue_max_segments(q); |
| 348 |
} |
| 349 |
sfp = sg_add_sfp(sdp); |
| 350 |
if (IS_ERR(sfp)) { |
| 351 |
retval = PTR_ERR(sfp); |
| 352 |
goto out_undo; |
| 353 |
} |
| 354 |
|
| 355 |
filp->private_data = sfp; |
| 356 |
sdp->open_cnt++; |
| 357 |
mutex_unlock(&sdp->open_rel_lock); |
| 358 |
|
| 359 |
retval = 0; |
| 360 |
sg_put: |
| 361 |
kref_put(&sdp->d_ref, sg_device_destroy); |
| 362 |
return retval; |
| 363 |
|
| 364 |
out_undo: |
| 365 |
if (flags & O_EXCL) { |
| 366 |
sdp->exclude = false; /* undo if error */ |
| 367 |
wake_up_interruptible(&sdp->open_wait); |
| 368 |
} |
| 369 |
error_mutex_locked: |
| 370 |
mutex_unlock(&sdp->open_rel_lock); |
| 371 |
sdp_put: |
| 372 |
kref_put(&sdp->d_ref, sg_device_destroy); |
| 373 |
scsi_device_put(device); |
| 374 |
return retval; |
| 375 |
} |
| 376 |
|
| 377 |
/* Release resources associated with a successful sg_open() |
| 378 |
* Returns 0 on success, else a negated errno value */ |
| 379 |
static int |
| 380 |
sg_release(struct inode *inode, struct file *filp) |
| 381 |
{ |
| 382 |
Sg_device *sdp; |
| 383 |
Sg_fd *sfp; |
| 384 |
|
| 385 |
if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 386 |
return -ENXIO; |
| 387 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, "sg_release\n")); |
| 388 |
|
| 389 |
mutex_lock(&sdp->open_rel_lock); |
| 390 |
sdp->open_cnt--; |
| 391 |
|
| 392 |
/* possibly many open()s waiting on exlude clearing, start many; |
| 393 |
* only open(O_EXCL)s wait on 0==open_cnt so only start one */ |
| 394 |
if (sdp->exclude) { |
| 395 |
sdp->exclude = false; |
| 396 |
wake_up_interruptible_all(&sdp->open_wait); |
| 397 |
} else if (0 == sdp->open_cnt) { |
| 398 |
wake_up_interruptible(&sdp->open_wait); |
| 399 |
} |
| 400 |
mutex_unlock(&sdp->open_rel_lock); |
| 401 |
kref_put(&sfp->f_ref, sg_remove_sfp); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
static int get_sg_io_pack_id(int *pack_id, void __user *buf, size_t count) |
| 406 |
{ |
| 407 |
struct sg_header __user *old_hdr = buf; |
| 408 |
int reply_len; |
| 409 |
|
| 410 |
if (count >= SZ_SG_HEADER) { |
| 411 |
/* negative reply_len means v3 format, otherwise v1/v2 */ |
| 412 |
if (get_user(reply_len, &old_hdr->reply_len)) |
| 413 |
return -EFAULT; |
| 414 |
|
| 415 |
if (reply_len >= 0) |
| 416 |
return get_user(*pack_id, &old_hdr->pack_id); |
| 417 |
|
| 418 |
if (in_compat_syscall() && |
| 419 |
count >= sizeof(struct compat_sg_io_hdr)) { |
| 420 |
struct compat_sg_io_hdr __user *hp = buf; |
| 421 |
|
| 422 |
return get_user(*pack_id, &hp->pack_id); |
| 423 |
} |
| 424 |
|
| 425 |
if (count >= sizeof(struct sg_io_hdr)) { |
| 426 |
struct sg_io_hdr __user *hp = buf; |
| 427 |
|
| 428 |
return get_user(*pack_id, &hp->pack_id); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/* no valid header was passed, so ignore the pack_id */ |
| 433 |
*pack_id = -1; |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
static ssize_t |
| 438 |
sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos) |
| 439 |
{ |
| 440 |
Sg_device *sdp; |
| 441 |
Sg_fd *sfp; |
| 442 |
Sg_request *srp; |
| 443 |
int req_pack_id = -1; |
| 444 |
bool busy; |
| 445 |
sg_io_hdr_t *hp; |
| 446 |
struct sg_header *old_hdr; |
| 447 |
int retval; |
| 448 |
|
| 449 |
/* |
| 450 |
* This could cause a response to be stranded. Close the associated |
| 451 |
* file descriptor to free up any resources being held. |
| 452 |
*/ |
| 453 |
retval = sg_check_file_access(filp, __func__); |
| 454 |
if (retval) |
| 455 |
return retval; |
| 456 |
|
| 457 |
if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 458 |
return -ENXIO; |
| 459 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 460 |
"sg_read: count=%d\n", (int) count)); |
| 461 |
|
| 462 |
if (sfp->force_packid) |
| 463 |
retval = get_sg_io_pack_id(&req_pack_id, buf, count); |
| 464 |
if (retval) |
| 465 |
return retval; |
| 466 |
|
| 467 |
srp = sg_get_rq_mark(sfp, req_pack_id, &busy); |
| 468 |
if (!srp) { /* now wait on packet to arrive */ |
| 469 |
if (filp->f_flags & O_NONBLOCK) |
| 470 |
return -EAGAIN; |
| 471 |
retval = wait_event_interruptible(sfp->read_wait, |
| 472 |
((srp = sg_get_rq_mark(sfp, req_pack_id, &busy)) || |
| 473 |
(!busy && atomic_read(&sdp->detaching)))); |
| 474 |
if (!srp) |
| 475 |
/* signal or detaching */ |
| 476 |
return retval ? retval : -ENODEV; |
| 477 |
} |
| 478 |
if (srp->header.interface_id != '\0') |
| 479 |
return sg_new_read(sfp, buf, count, srp); |
| 480 |
|
| 481 |
hp = &srp->header; |
| 482 |
old_hdr = kzalloc(SZ_SG_HEADER, GFP_KERNEL); |
| 483 |
if (!old_hdr) |
| 484 |
return -ENOMEM; |
| 485 |
|
| 486 |
old_hdr->reply_len = (int) hp->timeout; |
| 487 |
old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */ |
| 488 |
old_hdr->pack_id = hp->pack_id; |
| 489 |
old_hdr->twelve_byte = |
| 490 |
((srp->data.cmd_opcode >= 0xc0) && (12 == hp->cmd_len)) ? 1 : 0; |
| 491 |
old_hdr->target_status = hp->masked_status; |
| 492 |
old_hdr->host_status = hp->host_status; |
| 493 |
old_hdr->driver_status = hp->driver_status; |
| 494 |
if ((CHECK_CONDITION & hp->masked_status) || |
| 495 |
(srp->sense_b[0] & 0x70) == 0x70) { |
| 496 |
old_hdr->driver_status = DRIVER_SENSE; |
| 497 |
memcpy(old_hdr->sense_buffer, srp->sense_b, |
| 498 |
sizeof (old_hdr->sense_buffer)); |
| 499 |
} |
| 500 |
switch (hp->host_status) { |
| 501 |
/* This setup of 'result' is for backward compatibility and is best |
| 502 |
ignored by the user who should use target, host + driver status */ |
| 503 |
case DID_OK: |
| 504 |
case DID_PASSTHROUGH: |
| 505 |
case DID_SOFT_ERROR: |
| 506 |
old_hdr->result = 0; |
| 507 |
break; |
| 508 |
case DID_NO_CONNECT: |
| 509 |
case DID_BUS_BUSY: |
| 510 |
case DID_TIME_OUT: |
| 511 |
old_hdr->result = EBUSY; |
| 512 |
break; |
| 513 |
case DID_BAD_TARGET: |
| 514 |
case DID_ABORT: |
| 515 |
case DID_PARITY: |
| 516 |
case DID_RESET: |
| 517 |
case DID_BAD_INTR: |
| 518 |
old_hdr->result = EIO; |
| 519 |
break; |
| 520 |
case DID_ERROR: |
| 521 |
old_hdr->result = (srp->sense_b[0] == 0 && |
| 522 |
hp->masked_status == GOOD) ? 0 : EIO; |
| 523 |
break; |
| 524 |
default: |
| 525 |
old_hdr->result = EIO; |
| 526 |
break; |
| 527 |
} |
| 528 |
|
| 529 |
/* Now copy the result back to the user buffer. */ |
| 530 |
if (count >= SZ_SG_HEADER) { |
| 531 |
if (copy_to_user(buf, old_hdr, SZ_SG_HEADER)) { |
| 532 |
retval = -EFAULT; |
| 533 |
goto free_old_hdr; |
| 534 |
} |
| 535 |
buf += SZ_SG_HEADER; |
| 536 |
if (count > old_hdr->reply_len) |
| 537 |
count = old_hdr->reply_len; |
| 538 |
if (count > SZ_SG_HEADER) { |
| 539 |
if (sg_read_oxfer(srp, buf, count - SZ_SG_HEADER)) { |
| 540 |
retval = -EFAULT; |
| 541 |
goto free_old_hdr; |
| 542 |
} |
| 543 |
} |
| 544 |
} else |
| 545 |
count = (old_hdr->result == 0) ? 0 : -EIO; |
| 546 |
sg_finish_rem_req(srp); |
| 547 |
sg_remove_request(sfp, srp); |
| 548 |
retval = count; |
| 549 |
free_old_hdr: |
| 550 |
kfree(old_hdr); |
| 551 |
return retval; |
| 552 |
} |
| 553 |
|
| 554 |
static ssize_t |
| 555 |
sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, Sg_request * srp) |
| 556 |
{ |
| 557 |
sg_io_hdr_t *hp = &srp->header; |
| 558 |
int err = 0, err2; |
| 559 |
int len; |
| 560 |
|
| 561 |
if (in_compat_syscall()) { |
| 562 |
if (count < sizeof(struct compat_sg_io_hdr)) { |
| 563 |
err = -EINVAL; |
| 564 |
goto err_out; |
| 565 |
} |
| 566 |
} else if (count < SZ_SG_IO_HDR) { |
| 567 |
err = -EINVAL; |
| 568 |
goto err_out; |
| 569 |
} |
| 570 |
hp->sb_len_wr = 0; |
| 571 |
if ((hp->mx_sb_len > 0) && hp->sbp) { |
| 572 |
if ((CHECK_CONDITION & hp->masked_status) || |
| 573 |
(srp->sense_b[0] & 0x70) == 0x70) { |
| 574 |
int sb_len = SCSI_SENSE_BUFFERSIZE; |
| 575 |
sb_len = (hp->mx_sb_len > sb_len) ? sb_len : hp->mx_sb_len; |
| 576 |
len = 8 + (int) srp->sense_b[7]; /* Additional sense length field */ |
| 577 |
len = (len > sb_len) ? sb_len : len; |
| 578 |
if (copy_to_user(hp->sbp, srp->sense_b, len)) { |
| 579 |
err = -EFAULT; |
| 580 |
goto err_out; |
| 581 |
} |
| 582 |
hp->driver_status = DRIVER_SENSE; |
| 583 |
hp->sb_len_wr = len; |
| 584 |
} |
| 585 |
} |
| 586 |
if (hp->masked_status || hp->host_status || hp->driver_status) |
| 587 |
hp->info |= SG_INFO_CHECK; |
| 588 |
err = put_sg_io_hdr(hp, buf); |
| 589 |
err_out: |
| 590 |
err2 = sg_finish_rem_req(srp); |
| 591 |
sg_remove_request(sfp, srp); |
| 592 |
return err ? : err2 ? : count; |
| 593 |
} |
| 594 |
|
| 595 |
static ssize_t |
| 596 |
sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos) |
| 597 |
{ |
| 598 |
int mxsize, cmd_size, k; |
| 599 |
int input_size, blocking; |
| 600 |
unsigned char opcode; |
| 601 |
Sg_device *sdp; |
| 602 |
Sg_fd *sfp; |
| 603 |
Sg_request *srp; |
| 604 |
struct sg_header old_hdr; |
| 605 |
sg_io_hdr_t *hp; |
| 606 |
unsigned char cmnd[SG_MAX_CDB_SIZE]; |
| 607 |
int retval; |
| 608 |
|
| 609 |
retval = sg_check_file_access(filp, __func__); |
| 610 |
if (retval) |
| 611 |
return retval; |
| 612 |
|
| 613 |
if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 614 |
return -ENXIO; |
| 615 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 616 |
"sg_write: count=%d\n", (int) count)); |
| 617 |
if (atomic_read(&sdp->detaching)) |
| 618 |
return -ENODEV; |
| 619 |
if (!((filp->f_flags & O_NONBLOCK) || |
| 620 |
scsi_block_when_processing_errors(sdp->device))) |
| 621 |
return -ENXIO; |
| 622 |
|
| 623 |
if (count < SZ_SG_HEADER) |
| 624 |
return -EIO; |
| 625 |
if (copy_from_user(&old_hdr, buf, SZ_SG_HEADER)) |
| 626 |
return -EFAULT; |
| 627 |
blocking = !(filp->f_flags & O_NONBLOCK); |
| 628 |
if (old_hdr.reply_len < 0) |
| 629 |
return sg_new_write(sfp, filp, buf, count, |
| 630 |
blocking, 0, 0, NULL); |
| 631 |
if (count < (SZ_SG_HEADER + 6)) |
| 632 |
return -EIO; /* The minimum scsi command length is 6 bytes. */ |
| 633 |
|
| 634 |
buf += SZ_SG_HEADER; |
| 635 |
if (get_user(opcode, buf)) |
| 636 |
return -EFAULT; |
| 637 |
|
| 638 |
if (!(srp = sg_add_request(sfp))) { |
| 639 |
SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sdp, |
| 640 |
"sg_write: queue full\n")); |
| 641 |
return -EDOM; |
| 642 |
} |
| 643 |
mutex_lock(&sfp->f_mutex); |
| 644 |
if (sfp->next_cmd_len > 0) { |
| 645 |
cmd_size = sfp->next_cmd_len; |
| 646 |
sfp->next_cmd_len = 0; /* reset so only this write() effected */ |
| 647 |
} else { |
| 648 |
cmd_size = COMMAND_SIZE(opcode); /* based on SCSI command group */ |
| 649 |
if ((opcode >= 0xc0) && old_hdr.twelve_byte) |
| 650 |
cmd_size = 12; |
| 651 |
} |
| 652 |
mutex_unlock(&sfp->f_mutex); |
| 653 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp, |
| 654 |
"sg_write: scsi opcode=0x%02x, cmd_size=%d\n", (int) opcode, cmd_size)); |
| 655 |
/* Determine buffer size. */ |
| 656 |
input_size = count - cmd_size; |
| 657 |
mxsize = (input_size > old_hdr.reply_len) ? input_size : old_hdr.reply_len; |
| 658 |
mxsize -= SZ_SG_HEADER; |
| 659 |
input_size -= SZ_SG_HEADER; |
| 660 |
if (input_size < 0) { |
| 661 |
sg_remove_request(sfp, srp); |
| 662 |
return -EIO; /* User did not pass enough bytes for this command. */ |
| 663 |
} |
| 664 |
hp = &srp->header; |
| 665 |
hp->interface_id = '\0'; /* indicator of old interface tunnelled */ |
| 666 |
hp->cmd_len = (unsigned char) cmd_size; |
| 667 |
hp->iovec_count = 0; |
| 668 |
hp->mx_sb_len = 0; |
| 669 |
if (input_size > 0) |
| 670 |
hp->dxfer_direction = (old_hdr.reply_len > SZ_SG_HEADER) ? |
| 671 |
SG_DXFER_TO_FROM_DEV : SG_DXFER_TO_DEV; |
| 672 |
else |
| 673 |
hp->dxfer_direction = (mxsize > 0) ? SG_DXFER_FROM_DEV : SG_DXFER_NONE; |
| 674 |
hp->dxfer_len = mxsize; |
| 675 |
if ((hp->dxfer_direction == SG_DXFER_TO_DEV) || |
| 676 |
(hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)) |
| 677 |
hp->dxferp = (char __user *)buf + cmd_size; |
| 678 |
else |
| 679 |
hp->dxferp = NULL; |
| 680 |
hp->sbp = NULL; |
| 681 |
hp->timeout = old_hdr.reply_len; /* structure abuse ... */ |
| 682 |
hp->flags = input_size; /* structure abuse ... */ |
| 683 |
hp->pack_id = old_hdr.pack_id; |
| 684 |
hp->usr_ptr = NULL; |
| 685 |
if (copy_from_user(cmnd, buf, cmd_size)) { |
| 686 |
sg_remove_request(sfp, srp); |
| 687 |
return -EFAULT; |
| 688 |
} |
| 689 |
/* |
| 690 |
* SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV, |
| 691 |
* but is is possible that the app intended SG_DXFER_TO_DEV, because there |
| 692 |
* is a non-zero input_size, so emit a warning. |
| 693 |
*/ |
| 694 |
if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) { |
| 695 |
printk_ratelimited(KERN_WARNING |
| 696 |
"sg_write: data in/out %d/%d bytes " |
| 697 |
"for SCSI command 0x%x-- guessing " |
| 698 |
"data in;\n program %s not setting " |
| 699 |
"count and/or reply_len properly\n", |
| 700 |
old_hdr.reply_len - (int)SZ_SG_HEADER, |
| 701 |
input_size, (unsigned int) cmnd[0], |
| 702 |
current->comm); |
| 703 |
} |
| 704 |
k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking); |
| 705 |
return (k < 0) ? k : count; |
| 706 |
} |
| 707 |
|
| 708 |
static ssize_t |
| 709 |
sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf, |
| 710 |
size_t count, int blocking, int read_only, int sg_io_owned, |
| 711 |
Sg_request **o_srp) |
| 712 |
{ |
| 713 |
int k; |
| 714 |
Sg_request *srp; |
| 715 |
sg_io_hdr_t *hp; |
| 716 |
unsigned char cmnd[SG_MAX_CDB_SIZE]; |
| 717 |
int timeout; |
| 718 |
unsigned long ul_timeout; |
| 719 |
|
| 720 |
if (count < SZ_SG_IO_HDR) |
| 721 |
return -EINVAL; |
| 722 |
|
| 723 |
sfp->cmd_q = 1; /* when sg_io_hdr seen, set command queuing on */ |
| 724 |
if (!(srp = sg_add_request(sfp))) { |
| 725 |
SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, |
| 726 |
"sg_new_write: queue full\n")); |
| 727 |
return -EDOM; |
| 728 |
} |
| 729 |
srp->sg_io_owned = sg_io_owned; |
| 730 |
hp = &srp->header; |
| 731 |
if (get_sg_io_hdr(hp, buf)) { |
| 732 |
sg_remove_request(sfp, srp); |
| 733 |
return -EFAULT; |
| 734 |
} |
| 735 |
hp->duration = jiffies_to_msecs(jiffies); |
| 736 |
|
| 737 |
if (hp->interface_id != 'S') { |
| 738 |
sg_remove_request(sfp, srp); |
| 739 |
return -ENOSYS; |
| 740 |
} |
| 741 |
if (hp->flags & SG_FLAG_MMAP_IO) { |
| 742 |
if (hp->dxfer_len > sfp->reserve.bufflen) { |
| 743 |
sg_remove_request(sfp, srp); |
| 744 |
return -ENOMEM; /* MMAP_IO size must fit in reserve buffer */ |
| 745 |
} |
| 746 |
if (hp->flags & SG_FLAG_DIRECT_IO) { |
| 747 |
sg_remove_request(sfp, srp); |
| 748 |
return -EINVAL; /* either MMAP_IO or DIRECT_IO (not both) */ |
| 749 |
} |
| 750 |
if (sfp->res_in_use) { |
| 751 |
sg_remove_request(sfp, srp); |
| 752 |
return -EBUSY; /* reserve buffer already being used */ |
| 753 |
} |
| 754 |
} |
| 755 |
ul_timeout = msecs_to_jiffies(srp->header.timeout); |
| 756 |
timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX; |
| 757 |
if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) { |
| 758 |
sg_remove_request(sfp, srp); |
| 759 |
return -EMSGSIZE; |
| 760 |
} |
| 761 |
if (copy_from_user(cmnd, hp->cmdp, hp->cmd_len)) { |
| 762 |
sg_remove_request(sfp, srp); |
| 763 |
return -EFAULT; |
| 764 |
} |
| 765 |
if (read_only && sg_allow_access(file, cmnd)) { |
| 766 |
sg_remove_request(sfp, srp); |
| 767 |
return -EPERM; |
| 768 |
} |
| 769 |
k = sg_common_write(sfp, srp, cmnd, timeout, blocking); |
| 770 |
if (k < 0) |
| 771 |
return k; |
| 772 |
if (o_srp) |
| 773 |
*o_srp = srp; |
| 774 |
return count; |
| 775 |
} |
| 776 |
|
| 777 |
static int |
| 778 |
sg_common_write(Sg_fd * sfp, Sg_request * srp, |
| 779 |
unsigned char *cmnd, int timeout, int blocking) |
| 780 |
{ |
| 781 |
int k, at_head; |
| 782 |
Sg_device *sdp = sfp->parentdp; |
| 783 |
sg_io_hdr_t *hp = &srp->header; |
| 784 |
|
| 785 |
srp->data.cmd_opcode = cmnd[0]; /* hold opcode of command */ |
| 786 |
hp->status = 0; |
| 787 |
hp->masked_status = 0; |
| 788 |
hp->msg_status = 0; |
| 789 |
hp->info = 0; |
| 790 |
hp->host_status = 0; |
| 791 |
hp->driver_status = 0; |
| 792 |
hp->resid = 0; |
| 793 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 794 |
"sg_common_write: scsi opcode=0x%02x, cmd_size=%d\n", |
| 795 |
(int) cmnd[0], (int) hp->cmd_len)); |
| 796 |
|
| 797 |
if (hp->dxfer_len >= SZ_256M) { |
| 798 |
sg_remove_request(sfp, srp); |
| 799 |
return -EINVAL; |
| 800 |
} |
| 801 |
|
| 802 |
k = sg_start_req(srp, cmnd); |
| 803 |
if (k) { |
| 804 |
SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, |
| 805 |
"sg_common_write: start_req err=%d\n", k)); |
| 806 |
sg_finish_rem_req(srp); |
| 807 |
sg_remove_request(sfp, srp); |
| 808 |
return k; /* probably out of space --> ENOMEM */ |
| 809 |
} |
| 810 |
if (atomic_read(&sdp->detaching)) { |
| 811 |
if (srp->bio) { |
| 812 |
blk_mq_free_request(srp->rq); |
| 813 |
srp->rq = NULL; |
| 814 |
} |
| 815 |
|
| 816 |
sg_finish_rem_req(srp); |
| 817 |
sg_remove_request(sfp, srp); |
| 818 |
return -ENODEV; |
| 819 |
} |
| 820 |
|
| 821 |
if (hp->interface_id != '\0' && /* v3 (or later) interface */ |
| 822 |
(SG_FLAG_Q_AT_TAIL & hp->flags)) |
| 823 |
at_head = 0; |
| 824 |
else |
| 825 |
at_head = 1; |
| 826 |
|
| 827 |
srp->rq->timeout = timeout; |
| 828 |
kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */ |
| 829 |
srp->rq->end_io = sg_rq_end_io; |
| 830 |
blk_execute_rq_nowait(srp->rq, at_head); |
| 831 |
return 0; |
| 832 |
} |
| 833 |
|
| 834 |
static int srp_done(Sg_fd *sfp, Sg_request *srp) |
| 835 |
{ |
| 836 |
unsigned long flags; |
| 837 |
int ret; |
| 838 |
|
| 839 |
read_lock_irqsave(&sfp->rq_list_lock, flags); |
| 840 |
ret = srp->done; |
| 841 |
read_unlock_irqrestore(&sfp->rq_list_lock, flags); |
| 842 |
return ret; |
| 843 |
} |
| 844 |
|
| 845 |
static int max_sectors_bytes(struct request_queue *q) |
| 846 |
{ |
| 847 |
unsigned int max_sectors = queue_max_sectors(q); |
| 848 |
|
| 849 |
max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9); |
| 850 |
|
| 851 |
return max_sectors << 9; |
| 852 |
} |
| 853 |
|
| 854 |
static void |
| 855 |
sg_fill_request_table(Sg_fd *sfp, sg_req_info_t *rinfo) |
| 856 |
{ |
| 857 |
Sg_request *srp; |
| 858 |
int val; |
| 859 |
unsigned int ms; |
| 860 |
|
| 861 |
val = 0; |
| 862 |
list_for_each_entry(srp, &sfp->rq_list, entry) { |
| 863 |
if (val >= SG_MAX_QUEUE) |
| 864 |
break; |
| 865 |
rinfo[val].req_state = srp->done + 1; |
| 866 |
rinfo[val].problem = |
| 867 |
srp->header.masked_status & |
| 868 |
srp->header.host_status & |
| 869 |
srp->header.driver_status; |
| 870 |
if (srp->done) |
| 871 |
rinfo[val].duration = |
| 872 |
srp->header.duration; |
| 873 |
else { |
| 874 |
ms = jiffies_to_msecs(jiffies); |
| 875 |
rinfo[val].duration = |
| 876 |
(ms > srp->header.duration) ? |
| 877 |
(ms - srp->header.duration) : 0; |
| 878 |
} |
| 879 |
rinfo[val].orphan = srp->orphan; |
| 880 |
rinfo[val].sg_io_owned = srp->sg_io_owned; |
| 881 |
rinfo[val].pack_id = srp->header.pack_id; |
| 882 |
rinfo[val].usr_ptr = srp->header.usr_ptr; |
| 883 |
val++; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
#ifdef CONFIG_COMPAT |
| 888 |
struct compat_sg_req_info { /* used by SG_GET_REQUEST_TABLE ioctl() */ |
| 889 |
char req_state; |
| 890 |
char orphan; |
| 891 |
char sg_io_owned; |
| 892 |
char problem; |
| 893 |
int pack_id; |
| 894 |
compat_uptr_t usr_ptr; |
| 895 |
unsigned int duration; |
| 896 |
int unused; |
| 897 |
}; |
| 898 |
|
| 899 |
static int put_compat_request_table(struct compat_sg_req_info __user *o, |
| 900 |
struct sg_req_info *rinfo) |
| 901 |
{ |
| 902 |
int i; |
| 903 |
for (i = 0; i < SG_MAX_QUEUE; i++) { |
| 904 |
if (copy_to_user(o + i, rinfo + i, offsetof(sg_req_info_t, usr_ptr)) || |
| 905 |
put_user((uintptr_t)rinfo[i].usr_ptr, &o[i].usr_ptr) || |
| 906 |
put_user(rinfo[i].duration, &o[i].duration) || |
| 907 |
put_user(rinfo[i].unused, &o[i].unused)) |
| 908 |
return -EFAULT; |
| 909 |
} |
| 910 |
return 0; |
| 911 |
} |
| 912 |
#endif |
| 913 |
|
| 914 |
static long |
| 915 |
sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp, |
| 916 |
unsigned int cmd_in, void __user *p) |
| 917 |
{ |
| 918 |
int __user *ip = p; |
| 919 |
int result, val, read_only; |
| 920 |
Sg_request *srp; |
| 921 |
unsigned long iflags; |
| 922 |
|
| 923 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 924 |
"sg_ioctl: cmd=0x%x\n", (int) cmd_in)); |
| 925 |
read_only = (O_RDWR != (filp->f_flags & O_ACCMODE)); |
| 926 |
|
| 927 |
switch (cmd_in) { |
| 928 |
case SG_IO: |
| 929 |
if (atomic_read(&sdp->detaching)) |
| 930 |
return -ENODEV; |
| 931 |
if (!scsi_block_when_processing_errors(sdp->device)) |
| 932 |
return -ENXIO; |
| 933 |
result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR, |
| 934 |
1, read_only, 1, &srp); |
| 935 |
if (result < 0) |
| 936 |
return result; |
| 937 |
result = wait_event_interruptible(sfp->read_wait, |
| 938 |
srp_done(sfp, srp)); |
| 939 |
write_lock_irq(&sfp->rq_list_lock); |
| 940 |
if (srp->done) { |
| 941 |
srp->done = 2; |
| 942 |
write_unlock_irq(&sfp->rq_list_lock); |
| 943 |
result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp); |
| 944 |
return (result < 0) ? result : 0; |
| 945 |
} |
| 946 |
srp->orphan = 1; |
| 947 |
write_unlock_irq(&sfp->rq_list_lock); |
| 948 |
return result; /* -ERESTARTSYS because signal hit process */ |
| 949 |
case SG_SET_TIMEOUT: |
| 950 |
result = get_user(val, ip); |
| 951 |
if (result) |
| 952 |
return result; |
| 953 |
if (val < 0) |
| 954 |
return -EIO; |
| 955 |
if (val >= mult_frac((s64)INT_MAX, USER_HZ, HZ)) |
| 956 |
val = min_t(s64, mult_frac((s64)INT_MAX, USER_HZ, HZ), |
| 957 |
INT_MAX); |
| 958 |
sfp->timeout_user = val; |
| 959 |
sfp->timeout = mult_frac(val, HZ, USER_HZ); |
| 960 |
|
| 961 |
return 0; |
| 962 |
case SG_GET_TIMEOUT: /* N.B. User receives timeout as return value */ |
| 963 |
/* strange ..., for backward compatibility */ |
| 964 |
return sfp->timeout_user; |
| 965 |
case SG_SET_FORCE_LOW_DMA: |
| 966 |
/* |
| 967 |
* N.B. This ioctl never worked properly, but failed to |
| 968 |
* return an error value. So returning '0' to keep compability |
| 969 |
* with legacy applications. |
| 970 |
*/ |
| 971 |
return 0; |
| 972 |
case SG_GET_LOW_DMA: |
| 973 |
return put_user(0, ip); |
| 974 |
case SG_GET_SCSI_ID: |
| 975 |
{ |
| 976 |
sg_scsi_id_t v; |
| 977 |
|
| 978 |
if (atomic_read(&sdp->detaching)) |
| 979 |
return -ENODEV; |
| 980 |
memset(&v, 0, sizeof(v)); |
| 981 |
v.host_no = sdp->device->host->host_no; |
| 982 |
v.channel = sdp->device->channel; |
| 983 |
v.scsi_id = sdp->device->id; |
| 984 |
v.lun = sdp->device->lun; |
| 985 |
v.scsi_type = sdp->device->type; |
| 986 |
v.h_cmd_per_lun = sdp->device->host->cmd_per_lun; |
| 987 |
v.d_queue_depth = sdp->device->queue_depth; |
| 988 |
if (copy_to_user(p, &v, sizeof(sg_scsi_id_t))) |
| 989 |
return -EFAULT; |
| 990 |
return 0; |
| 991 |
} |
| 992 |
case SG_SET_FORCE_PACK_ID: |
| 993 |
result = get_user(val, ip); |
| 994 |
if (result) |
| 995 |
return result; |
| 996 |
sfp->force_packid = val ? 1 : 0; |
| 997 |
return 0; |
| 998 |
case SG_GET_PACK_ID: |
| 999 |
read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1000 |
list_for_each_entry(srp, &sfp->rq_list, entry) { |
| 1001 |
if ((1 == srp->done) && (!srp->sg_io_owned)) { |
| 1002 |
read_unlock_irqrestore(&sfp->rq_list_lock, |
| 1003 |
iflags); |
| 1004 |
return put_user(srp->header.pack_id, ip); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1008 |
return put_user(-1, ip); |
| 1009 |
case SG_GET_NUM_WAITING: |
| 1010 |
read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1011 |
val = 0; |
| 1012 |
list_for_each_entry(srp, &sfp->rq_list, entry) { |
| 1013 |
if ((1 == srp->done) && (!srp->sg_io_owned)) |
| 1014 |
++val; |
| 1015 |
} |
| 1016 |
read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1017 |
return put_user(val, ip); |
| 1018 |
case SG_GET_SG_TABLESIZE: |
| 1019 |
return put_user(sdp->sg_tablesize, ip); |
| 1020 |
case SG_SET_RESERVED_SIZE: |
| 1021 |
result = get_user(val, ip); |
| 1022 |
if (result) |
| 1023 |
return result; |
| 1024 |
if (val < 0) |
| 1025 |
return -EINVAL; |
| 1026 |
val = min_t(int, val, |
| 1027 |
max_sectors_bytes(sdp->device->request_queue)); |
| 1028 |
mutex_lock(&sfp->f_mutex); |
| 1029 |
if (val != sfp->reserve.bufflen) { |
| 1030 |
if (sfp->mmap_called || |
| 1031 |
sfp->res_in_use) { |
| 1032 |
mutex_unlock(&sfp->f_mutex); |
| 1033 |
return -EBUSY; |
| 1034 |
} |
| 1035 |
|
| 1036 |
sg_remove_scat(sfp, &sfp->reserve); |
| 1037 |
sg_build_reserve(sfp, val); |
| 1038 |
} |
| 1039 |
mutex_unlock(&sfp->f_mutex); |
| 1040 |
return 0; |
| 1041 |
case SG_GET_RESERVED_SIZE: |
| 1042 |
val = min_t(int, sfp->reserve.bufflen, |
| 1043 |
max_sectors_bytes(sdp->device->request_queue)); |
| 1044 |
return put_user(val, ip); |
| 1045 |
case SG_SET_COMMAND_Q: |
| 1046 |
result = get_user(val, ip); |
| 1047 |
if (result) |
| 1048 |
return result; |
| 1049 |
sfp->cmd_q = val ? 1 : 0; |
| 1050 |
return 0; |
| 1051 |
case SG_GET_COMMAND_Q: |
| 1052 |
return put_user((int) sfp->cmd_q, ip); |
| 1053 |
case SG_SET_KEEP_ORPHAN: |
| 1054 |
result = get_user(val, ip); |
| 1055 |
if (result) |
| 1056 |
return result; |
| 1057 |
sfp->keep_orphan = val; |
| 1058 |
return 0; |
| 1059 |
case SG_GET_KEEP_ORPHAN: |
| 1060 |
return put_user((int) sfp->keep_orphan, ip); |
| 1061 |
case SG_NEXT_CMD_LEN: |
| 1062 |
result = get_user(val, ip); |
| 1063 |
if (result) |
| 1064 |
return result; |
| 1065 |
if (val > SG_MAX_CDB_SIZE) |
| 1066 |
return -ENOMEM; |
| 1067 |
sfp->next_cmd_len = (val > 0) ? val : 0; |
| 1068 |
return 0; |
| 1069 |
case SG_GET_VERSION_NUM: |
| 1070 |
return put_user(sg_version_num, ip); |
| 1071 |
case SG_GET_ACCESS_COUNT: |
| 1072 |
/* faked - we don't have a real access count anymore */ |
| 1073 |
val = (sdp->device ? 1 : 0); |
| 1074 |
return put_user(val, ip); |
| 1075 |
case SG_GET_REQUEST_TABLE: |
| 1076 |
{ |
| 1077 |
sg_req_info_t *rinfo; |
| 1078 |
|
| 1079 |
rinfo = kcalloc(SG_MAX_QUEUE, SZ_SG_REQ_INFO, |
| 1080 |
GFP_KERNEL); |
| 1081 |
if (!rinfo) |
| 1082 |
return -ENOMEM; |
| 1083 |
read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1084 |
sg_fill_request_table(sfp, rinfo); |
| 1085 |
read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1086 |
#ifdef CONFIG_COMPAT |
| 1087 |
if (in_compat_syscall()) |
| 1088 |
result = put_compat_request_table(p, rinfo); |
| 1089 |
else |
| 1090 |
#endif |
| 1091 |
result = copy_to_user(p, rinfo, |
| 1092 |
SZ_SG_REQ_INFO * SG_MAX_QUEUE); |
| 1093 |
result = result ? -EFAULT : 0; |
| 1094 |
kfree(rinfo); |
| 1095 |
return result; |
| 1096 |
} |
| 1097 |
case SG_EMULATED_HOST: |
| 1098 |
if (atomic_read(&sdp->detaching)) |
| 1099 |
return -ENODEV; |
| 1100 |
return put_user(sdp->device->host->hostt->emulated, ip); |
| 1101 |
case SCSI_IOCTL_SEND_COMMAND: |
| 1102 |
if (atomic_read(&sdp->detaching)) |
| 1103 |
return -ENODEV; |
| 1104 |
return scsi_ioctl(sdp->device, filp->f_mode & FMODE_WRITE, |
| 1105 |
cmd_in, p); |
| 1106 |
case SG_SET_DEBUG: |
| 1107 |
result = get_user(val, ip); |
| 1108 |
if (result) |
| 1109 |
return result; |
| 1110 |
sdp->sgdebug = (char) val; |
| 1111 |
return 0; |
| 1112 |
case BLKSECTGET: |
| 1113 |
return put_user(max_sectors_bytes(sdp->device->request_queue), |
| 1114 |
ip); |
| 1115 |
case BLKTRACESETUP: |
| 1116 |
return blk_trace_setup(sdp->device->request_queue, sdp->name, |
| 1117 |
MKDEV(SCSI_GENERIC_MAJOR, sdp->index), |
| 1118 |
NULL, p); |
| 1119 |
case BLKTRACESTART: |
| 1120 |
return blk_trace_startstop(sdp->device->request_queue, 1); |
| 1121 |
case BLKTRACESTOP: |
| 1122 |
return blk_trace_startstop(sdp->device->request_queue, 0); |
| 1123 |
case BLKTRACETEARDOWN: |
| 1124 |
return blk_trace_remove(sdp->device->request_queue); |
| 1125 |
case SCSI_IOCTL_GET_IDLUN: |
| 1126 |
case SCSI_IOCTL_GET_BUS_NUMBER: |
| 1127 |
case SCSI_IOCTL_PROBE_HOST: |
| 1128 |
case SG_GET_TRANSFORM: |
| 1129 |
case SG_SCSI_RESET: |
| 1130 |
if (atomic_read(&sdp->detaching)) |
| 1131 |
return -ENODEV; |
| 1132 |
break; |
| 1133 |
default: |
| 1134 |
if (read_only) |
| 1135 |
return -EPERM; /* don't know so take safe approach */ |
| 1136 |
break; |
| 1137 |
} |
| 1138 |
|
| 1139 |
result = scsi_ioctl_block_when_processing_errors(sdp->device, |
| 1140 |
cmd_in, filp->f_flags & O_NDELAY); |
| 1141 |
if (result) |
| 1142 |
return result; |
| 1143 |
|
| 1144 |
return -ENOIOCTLCMD; |
| 1145 |
} |
| 1146 |
|
| 1147 |
static long |
| 1148 |
sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg) |
| 1149 |
{ |
| 1150 |
void __user *p = (void __user *)arg; |
| 1151 |
Sg_device *sdp; |
| 1152 |
Sg_fd *sfp; |
| 1153 |
int ret; |
| 1154 |
|
| 1155 |
if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 1156 |
return -ENXIO; |
| 1157 |
|
| 1158 |
ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p); |
| 1159 |
if (ret != -ENOIOCTLCMD) |
| 1160 |
return ret; |
| 1161 |
return scsi_ioctl(sdp->device, filp->f_mode & FMODE_WRITE, cmd_in, p); |
| 1162 |
} |
| 1163 |
|
| 1164 |
static __poll_t |
| 1165 |
sg_poll(struct file *filp, poll_table * wait) |
| 1166 |
{ |
| 1167 |
__poll_t res = 0; |
| 1168 |
Sg_device *sdp; |
| 1169 |
Sg_fd *sfp; |
| 1170 |
Sg_request *srp; |
| 1171 |
int count = 0; |
| 1172 |
unsigned long iflags; |
| 1173 |
|
| 1174 |
sfp = filp->private_data; |
| 1175 |
if (!sfp) |
| 1176 |
return EPOLLERR; |
| 1177 |
sdp = sfp->parentdp; |
| 1178 |
if (!sdp) |
| 1179 |
return EPOLLERR; |
| 1180 |
poll_wait(filp, &sfp->read_wait, wait); |
| 1181 |
read_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1182 |
list_for_each_entry(srp, &sfp->rq_list, entry) { |
| 1183 |
/* if any read waiting, flag it */ |
| 1184 |
if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned)) |
| 1185 |
res = EPOLLIN | EPOLLRDNORM; |
| 1186 |
++count; |
| 1187 |
} |
| 1188 |
read_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1189 |
|
| 1190 |
if (atomic_read(&sdp->detaching)) |
| 1191 |
res |= EPOLLHUP; |
| 1192 |
else if (!sfp->cmd_q) { |
| 1193 |
if (0 == count) |
| 1194 |
res |= EPOLLOUT | EPOLLWRNORM; |
| 1195 |
} else if (count < SG_MAX_QUEUE) |
| 1196 |
res |= EPOLLOUT | EPOLLWRNORM; |
| 1197 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 1198 |
"sg_poll: res=0x%x\n", (__force u32) res)); |
| 1199 |
return res; |
| 1200 |
} |
| 1201 |
|
| 1202 |
static int |
| 1203 |
sg_fasync(int fd, struct file *filp, int mode) |
| 1204 |
{ |
| 1205 |
Sg_device *sdp; |
| 1206 |
Sg_fd *sfp; |
| 1207 |
|
| 1208 |
if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) |
| 1209 |
return -ENXIO; |
| 1210 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 1211 |
"sg_fasync: mode=%d\n", mode)); |
| 1212 |
|
| 1213 |
return fasync_helper(fd, filp, mode, &sfp->async_qp); |
| 1214 |
} |
| 1215 |
|
| 1216 |
static vm_fault_t |
| 1217 |
sg_vma_fault(struct vm_fault *vmf) |
| 1218 |
{ |
| 1219 |
struct vm_area_struct *vma = vmf->vma; |
| 1220 |
Sg_fd *sfp; |
| 1221 |
unsigned long offset, len, sa; |
| 1222 |
Sg_scatter_hold *rsv_schp; |
| 1223 |
int k, length; |
| 1224 |
|
| 1225 |
if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data))) |
| 1226 |
return VM_FAULT_SIGBUS; |
| 1227 |
rsv_schp = &sfp->reserve; |
| 1228 |
offset = vmf->pgoff << PAGE_SHIFT; |
| 1229 |
if (offset >= rsv_schp->bufflen) |
| 1230 |
return VM_FAULT_SIGBUS; |
| 1231 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp, |
| 1232 |
"sg_vma_fault: offset=%lu, scatg=%d\n", |
| 1233 |
offset, rsv_schp->k_use_sg)); |
| 1234 |
sa = vma->vm_start; |
| 1235 |
length = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 1236 |
for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) { |
| 1237 |
len = vma->vm_end - sa; |
| 1238 |
len = (len < length) ? len : length; |
| 1239 |
if (offset < len) { |
| 1240 |
struct page *page = rsv_schp->pages[k] + (offset >> PAGE_SHIFT); |
| 1241 |
get_page(page); /* increment page count */ |
| 1242 |
vmf->page = page; |
| 1243 |
return 0; /* success */ |
| 1244 |
} |
| 1245 |
sa += len; |
| 1246 |
offset -= len; |
| 1247 |
} |
| 1248 |
|
| 1249 |
return VM_FAULT_SIGBUS; |
| 1250 |
} |
| 1251 |
|
| 1252 |
static const struct vm_operations_struct sg_mmap_vm_ops = { |
| 1253 |
.fault = sg_vma_fault, |
| 1254 |
}; |
| 1255 |
|
| 1256 |
static int |
| 1257 |
sg_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1258 |
{ |
| 1259 |
Sg_fd *sfp; |
| 1260 |
unsigned long req_sz, len, sa; |
| 1261 |
Sg_scatter_hold *rsv_schp; |
| 1262 |
int k, length; |
| 1263 |
int ret = 0; |
| 1264 |
|
| 1265 |
if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data))) |
| 1266 |
return -ENXIO; |
| 1267 |
req_sz = vma->vm_end - vma->vm_start; |
| 1268 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp, |
| 1269 |
"sg_mmap starting, vm_start=%p, len=%d\n", |
| 1270 |
(void *) vma->vm_start, (int) req_sz)); |
| 1271 |
if (vma->vm_pgoff) |
| 1272 |
return -EINVAL; /* want no offset */ |
| 1273 |
rsv_schp = &sfp->reserve; |
| 1274 |
mutex_lock(&sfp->f_mutex); |
| 1275 |
if (req_sz > rsv_schp->bufflen) { |
| 1276 |
ret = -ENOMEM; /* cannot map more than reserved buffer */ |
| 1277 |
goto out; |
| 1278 |
} |
| 1279 |
|
| 1280 |
sa = vma->vm_start; |
| 1281 |
length = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 1282 |
for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) { |
| 1283 |
len = vma->vm_end - sa; |
| 1284 |
len = (len < length) ? len : length; |
| 1285 |
sa += len; |
| 1286 |
} |
| 1287 |
|
| 1288 |
sfp->mmap_called = 1; |
| 1289 |
vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP); |
| 1290 |
vma->vm_private_data = sfp; |
| 1291 |
vma->vm_ops = &sg_mmap_vm_ops; |
| 1292 |
out: |
| 1293 |
mutex_unlock(&sfp->f_mutex); |
| 1294 |
return ret; |
| 1295 |
} |
| 1296 |
|
| 1297 |
static void |
| 1298 |
sg_rq_end_io_usercontext(struct work_struct *work) |
| 1299 |
{ |
| 1300 |
struct sg_request *srp = container_of(work, struct sg_request, ew.work); |
| 1301 |
struct sg_fd *sfp = srp->parentfp; |
| 1302 |
|
| 1303 |
sg_finish_rem_req(srp); |
| 1304 |
sg_remove_request(sfp, srp); |
| 1305 |
kref_put(&sfp->f_ref, sg_remove_sfp); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* |
| 1309 |
* This function is a "bottom half" handler that is called by the mid |
| 1310 |
* level when a command is completed (or has failed). |
| 1311 |
*/ |
| 1312 |
static enum rq_end_io_ret |
| 1313 |
sg_rq_end_io(struct request *rq, blk_status_t status, |
| 1314 |
const struct io_comp_batch *iob) |
| 1315 |
{ |
| 1316 |
struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq); |
| 1317 |
struct sg_request *srp = rq->end_io_data; |
| 1318 |
Sg_device *sdp; |
| 1319 |
Sg_fd *sfp; |
| 1320 |
unsigned long iflags; |
| 1321 |
unsigned int ms; |
| 1322 |
char *sense; |
| 1323 |
int result, resid, done = 1; |
| 1324 |
|
| 1325 |
if (WARN_ON(srp->done != 0)) |
| 1326 |
return RQ_END_IO_NONE; |
| 1327 |
|
| 1328 |
sfp = srp->parentfp; |
| 1329 |
if (WARN_ON(sfp == NULL)) |
| 1330 |
return RQ_END_IO_NONE; |
| 1331 |
|
| 1332 |
sdp = sfp->parentdp; |
| 1333 |
if (unlikely(atomic_read(&sdp->detaching))) |
| 1334 |
pr_info("%s: device detaching\n", __func__); |
| 1335 |
|
| 1336 |
sense = scmd->sense_buffer; |
| 1337 |
result = scmd->result; |
| 1338 |
resid = scmd->resid_len; |
| 1339 |
|
| 1340 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp, |
| 1341 |
"sg_cmd_done: pack_id=%d, res=0x%x\n", |
| 1342 |
srp->header.pack_id, result)); |
| 1343 |
srp->header.resid = resid; |
| 1344 |
if (0 != result) { |
| 1345 |
struct scsi_sense_hdr sshdr; |
| 1346 |
|
| 1347 |
srp->header.status = 0xff & result; |
| 1348 |
srp->header.masked_status = sg_status_byte(result); |
| 1349 |
srp->header.msg_status = COMMAND_COMPLETE; |
| 1350 |
srp->header.host_status = host_byte(result); |
| 1351 |
srp->header.driver_status = driver_byte(result); |
| 1352 |
if ((sdp->sgdebug > 0) && |
| 1353 |
((CHECK_CONDITION == srp->header.masked_status) || |
| 1354 |
(COMMAND_TERMINATED == srp->header.masked_status))) |
| 1355 |
__scsi_print_sense(sdp->device, __func__, sense, |
| 1356 |
SCSI_SENSE_BUFFERSIZE); |
| 1357 |
|
| 1358 |
/* Following if statement is a patch supplied by Eric Youngdale */ |
| 1359 |
if (driver_byte(result) != 0 |
| 1360 |
&& scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr) |
| 1361 |
&& !scsi_sense_is_deferred(&sshdr) |
| 1362 |
&& sshdr.sense_key == UNIT_ATTENTION |
| 1363 |
&& sdp->device->removable) { |
| 1364 |
/* Detected possible disc change. Set the bit - this */ |
| 1365 |
/* may be used if there are filesystems using this device */ |
| 1366 |
sdp->device->changed = 1; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (scmd->sense_len) |
| 1371 |
memcpy(srp->sense_b, scmd->sense_buffer, SCSI_SENSE_BUFFERSIZE); |
| 1372 |
|
| 1373 |
/* Rely on write phase to clean out srp status values, so no "else" */ |
| 1374 |
|
| 1375 |
/* |
| 1376 |
* Free the request as soon as it is complete so that its resources |
| 1377 |
* can be reused without waiting for userspace to read() the |
| 1378 |
* result. But keep the associated bio (if any) around until |
| 1379 |
* blk_rq_unmap_user() can be called from user context. |
| 1380 |
*/ |
| 1381 |
srp->rq = NULL; |
| 1382 |
blk_mq_free_request(rq); |
| 1383 |
|
| 1384 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 1385 |
if (unlikely(srp->orphan)) { |
| 1386 |
if (sfp->keep_orphan) |
| 1387 |
srp->sg_io_owned = 0; |
| 1388 |
else |
| 1389 |
done = 0; |
| 1390 |
} |
| 1391 |
srp->done = done; |
| 1392 |
ms = jiffies_to_msecs(jiffies); |
| 1393 |
srp->header.duration = (ms > srp->header.duration) ? |
| 1394 |
(ms - srp->header.duration) : 0; |
| 1395 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 1396 |
|
| 1397 |
if (likely(done)) { |
| 1398 |
/* Now wake up any sg_read() that is waiting for this |
| 1399 |
* packet. |
| 1400 |
*/ |
| 1401 |
wake_up_interruptible(&sfp->read_wait); |
| 1402 |
kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN); |
| 1403 |
kref_put(&sfp->f_ref, sg_remove_sfp); |
| 1404 |
} else { |
| 1405 |
INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext); |
| 1406 |
schedule_work(&srp->ew.work); |
| 1407 |
} |
| 1408 |
return RQ_END_IO_NONE; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static const struct file_operations sg_fops = { |
| 1412 |
.owner = THIS_MODULE, |
| 1413 |
.read = sg_read, |
| 1414 |
.write = sg_write, |
| 1415 |
.poll = sg_poll, |
| 1416 |
.unlocked_ioctl = sg_ioctl, |
| 1417 |
.compat_ioctl = compat_ptr_ioctl, |
| 1418 |
.open = sg_open, |
| 1419 |
.mmap = sg_mmap, |
| 1420 |
.release = sg_release, |
| 1421 |
.fasync = sg_fasync, |
| 1422 |
}; |
| 1423 |
|
| 1424 |
static const struct class sg_sysfs_class = { |
| 1425 |
.name = "scsi_generic" |
| 1426 |
}; |
| 1427 |
|
| 1428 |
static int sg_sysfs_valid = 0; |
| 1429 |
|
| 1430 |
static Sg_device * |
| 1431 |
sg_alloc(struct scsi_device *scsidp) |
| 1432 |
{ |
| 1433 |
struct request_queue *q = scsidp->request_queue; |
| 1434 |
Sg_device *sdp; |
| 1435 |
unsigned long iflags; |
| 1436 |
int error; |
| 1437 |
u32 k; |
| 1438 |
|
| 1439 |
sdp = kzalloc_obj(Sg_device); |
| 1440 |
if (!sdp) { |
| 1441 |
sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device " |
| 1442 |
"failure\n", __func__); |
| 1443 |
return ERR_PTR(-ENOMEM); |
| 1444 |
} |
| 1445 |
|
| 1446 |
idr_preload(GFP_KERNEL); |
| 1447 |
write_lock_irqsave(&sg_index_lock, iflags); |
| 1448 |
|
| 1449 |
error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT); |
| 1450 |
if (error < 0) { |
| 1451 |
if (error == -ENOSPC) { |
| 1452 |
sdev_printk(KERN_WARNING, scsidp, |
| 1453 |
"Unable to attach sg device type=%d, minor number exceeds %d\n", |
| 1454 |
scsidp->type, SG_MAX_DEVS - 1); |
| 1455 |
error = -ENODEV; |
| 1456 |
} else { |
| 1457 |
sdev_printk(KERN_WARNING, scsidp, "%s: idr " |
| 1458 |
"allocation Sg_device failure: %d\n", |
| 1459 |
__func__, error); |
| 1460 |
} |
| 1461 |
goto out_unlock; |
| 1462 |
} |
| 1463 |
k = error; |
| 1464 |
|
| 1465 |
SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp, |
| 1466 |
"sg_alloc: dev=%d \n", k)); |
| 1467 |
sprintf(sdp->name, "sg%d", k); |
| 1468 |
sdp->device = scsidp; |
| 1469 |
mutex_init(&sdp->open_rel_lock); |
| 1470 |
INIT_LIST_HEAD(&sdp->sfds); |
| 1471 |
init_waitqueue_head(&sdp->open_wait); |
| 1472 |
atomic_set(&sdp->detaching, 0); |
| 1473 |
rwlock_init(&sdp->sfd_lock); |
| 1474 |
sdp->sg_tablesize = queue_max_segments(q); |
| 1475 |
sdp->index = k; |
| 1476 |
kref_init(&sdp->d_ref); |
| 1477 |
error = 0; |
| 1478 |
|
| 1479 |
out_unlock: |
| 1480 |
write_unlock_irqrestore(&sg_index_lock, iflags); |
| 1481 |
idr_preload_end(); |
| 1482 |
|
| 1483 |
if (error) { |
| 1484 |
kfree(sdp); |
| 1485 |
return ERR_PTR(error); |
| 1486 |
} |
| 1487 |
return sdp; |
| 1488 |
} |
| 1489 |
|
| 1490 |
static int |
| 1491 |
sg_add_device(struct device *cl_dev) |
| 1492 |
{ |
| 1493 |
struct scsi_device *scsidp = to_scsi_device(cl_dev->parent); |
| 1494 |
Sg_device *sdp = NULL; |
| 1495 |
struct cdev * cdev = NULL; |
| 1496 |
int error; |
| 1497 |
unsigned long iflags; |
| 1498 |
|
| 1499 |
if (!blk_get_queue(scsidp->request_queue)) { |
| 1500 |
pr_warn("%s: get scsi_device queue failed\n", __func__); |
| 1501 |
return -ENODEV; |
| 1502 |
} |
| 1503 |
|
| 1504 |
error = -ENOMEM; |
| 1505 |
cdev = cdev_alloc(); |
| 1506 |
if (!cdev) { |
| 1507 |
pr_warn("%s: cdev_alloc failed\n", __func__); |
| 1508 |
goto out; |
| 1509 |
} |
| 1510 |
cdev->owner = THIS_MODULE; |
| 1511 |
cdev->ops = &sg_fops; |
| 1512 |
|
| 1513 |
sdp = sg_alloc(scsidp); |
| 1514 |
if (IS_ERR(sdp)) { |
| 1515 |
pr_warn("%s: sg_alloc failed\n", __func__); |
| 1516 |
error = PTR_ERR(sdp); |
| 1517 |
goto out; |
| 1518 |
} |
| 1519 |
|
| 1520 |
error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1); |
| 1521 |
if (error) |
| 1522 |
goto cdev_add_err; |
| 1523 |
|
| 1524 |
sdp->cdev = cdev; |
| 1525 |
if (sg_sysfs_valid) { |
| 1526 |
struct device *sg_class_member; |
| 1527 |
|
| 1528 |
sg_class_member = device_create(&sg_sysfs_class, cl_dev->parent, |
| 1529 |
MKDEV(SCSI_GENERIC_MAJOR, |
| 1530 |
sdp->index), |
| 1531 |
sdp, "%s", sdp->name); |
| 1532 |
if (IS_ERR(sg_class_member)) { |
| 1533 |
pr_err("%s: device_create failed\n", __func__); |
| 1534 |
error = PTR_ERR(sg_class_member); |
| 1535 |
goto cdev_add_err; |
| 1536 |
} |
| 1537 |
error = sysfs_create_link(&scsidp->sdev_gendev.kobj, |
| 1538 |
&sg_class_member->kobj, "generic"); |
| 1539 |
if (error) |
| 1540 |
pr_err("%s: unable to make symlink 'generic' back " |
| 1541 |
"to sg%d\n", __func__, sdp->index); |
| 1542 |
} else |
| 1543 |
pr_warn("%s: sg_sys Invalid\n", __func__); |
| 1544 |
|
| 1545 |
sdev_printk(KERN_NOTICE, scsidp, "Attached scsi generic sg%d " |
| 1546 |
"type %d\n", sdp->index, scsidp->type); |
| 1547 |
|
| 1548 |
dev_set_drvdata(cl_dev, sdp); |
| 1549 |
|
| 1550 |
return 0; |
| 1551 |
|
| 1552 |
cdev_add_err: |
| 1553 |
write_lock_irqsave(&sg_index_lock, iflags); |
| 1554 |
idr_remove(&sg_index_idr, sdp->index); |
| 1555 |
write_unlock_irqrestore(&sg_index_lock, iflags); |
| 1556 |
kfree(sdp); |
| 1557 |
|
| 1558 |
out: |
| 1559 |
if (cdev) |
| 1560 |
cdev_del(cdev); |
| 1561 |
blk_put_queue(scsidp->request_queue); |
| 1562 |
return error; |
| 1563 |
} |
| 1564 |
|
| 1565 |
static void |
| 1566 |
sg_device_destroy(struct kref *kref) |
| 1567 |
{ |
| 1568 |
struct sg_device *sdp = container_of(kref, struct sg_device, d_ref); |
| 1569 |
struct request_queue *q = sdp->device->request_queue; |
| 1570 |
unsigned long flags; |
| 1571 |
|
| 1572 |
/* CAUTION! Note that the device can still be found via idr_find() |
| 1573 |
* even though the refcount is 0. Therefore, do idr_remove() BEFORE |
| 1574 |
* any other cleanup. |
| 1575 |
*/ |
| 1576 |
|
| 1577 |
blk_trace_remove(q); |
| 1578 |
blk_put_queue(q); |
| 1579 |
|
| 1580 |
write_lock_irqsave(&sg_index_lock, flags); |
| 1581 |
idr_remove(&sg_index_idr, sdp->index); |
| 1582 |
write_unlock_irqrestore(&sg_index_lock, flags); |
| 1583 |
|
| 1584 |
SCSI_LOG_TIMEOUT(3, |
| 1585 |
sg_printk(KERN_INFO, sdp, "sg_device_destroy\n")); |
| 1586 |
|
| 1587 |
kfree(sdp); |
| 1588 |
} |
| 1589 |
|
| 1590 |
static void |
| 1591 |
sg_remove_device(struct device *cl_dev) |
| 1592 |
{ |
| 1593 |
struct scsi_device *scsidp = to_scsi_device(cl_dev->parent); |
| 1594 |
Sg_device *sdp = dev_get_drvdata(cl_dev); |
| 1595 |
unsigned long iflags; |
| 1596 |
Sg_fd *sfp; |
| 1597 |
int val; |
| 1598 |
|
| 1599 |
if (!sdp) |
| 1600 |
return; |
| 1601 |
/* want sdp->detaching non-zero as soon as possible */ |
| 1602 |
val = atomic_inc_return(&sdp->detaching); |
| 1603 |
if (val > 1) |
| 1604 |
return; /* only want to do following once per device */ |
| 1605 |
|
| 1606 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 1607 |
"%s\n", __func__)); |
| 1608 |
|
| 1609 |
read_lock_irqsave(&sdp->sfd_lock, iflags); |
| 1610 |
list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) { |
| 1611 |
wake_up_interruptible_all(&sfp->read_wait); |
| 1612 |
kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP); |
| 1613 |
} |
| 1614 |
wake_up_interruptible_all(&sdp->open_wait); |
| 1615 |
read_unlock_irqrestore(&sdp->sfd_lock, iflags); |
| 1616 |
|
| 1617 |
sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic"); |
| 1618 |
device_destroy(&sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index)); |
| 1619 |
cdev_del(sdp->cdev); |
| 1620 |
sdp->cdev = NULL; |
| 1621 |
|
| 1622 |
kref_put(&sdp->d_ref, sg_device_destroy); |
| 1623 |
} |
| 1624 |
|
| 1625 |
module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR); |
| 1626 |
module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR); |
| 1627 |
|
| 1628 |
static int def_reserved_size_set(const char *val, const struct kernel_param *kp) |
| 1629 |
{ |
| 1630 |
int size, ret; |
| 1631 |
|
| 1632 |
if (!val) |
| 1633 |
return -EINVAL; |
| 1634 |
|
| 1635 |
ret = kstrtoint(val, 0, &size); |
| 1636 |
if (ret) |
| 1637 |
return ret; |
| 1638 |
|
| 1639 |
/* limit to 1 MB */ |
| 1640 |
if (size < 0 || size > 1048576) |
| 1641 |
return -ERANGE; |
| 1642 |
|
| 1643 |
def_reserved_size = size; |
| 1644 |
return 0; |
| 1645 |
} |
| 1646 |
|
| 1647 |
static const struct kernel_param_ops def_reserved_size_ops = { |
| 1648 |
.set = def_reserved_size_set, |
| 1649 |
.get = param_get_int, |
| 1650 |
}; |
| 1651 |
|
| 1652 |
module_param_cb(def_reserved_size, &def_reserved_size_ops, &def_reserved_size, |
| 1653 |
S_IRUGO | S_IWUSR); |
| 1654 |
|
| 1655 |
MODULE_AUTHOR("Douglas Gilbert"); |
| 1656 |
MODULE_DESCRIPTION("SCSI generic (sg) driver"); |
| 1657 |
MODULE_LICENSE("GPL"); |
| 1658 |
MODULE_VERSION(SG_VERSION_STR); |
| 1659 |
MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR); |
| 1660 |
|
| 1661 |
MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element " |
| 1662 |
"size (default: max(SG_SCATTER_SZ, PAGE_SIZE))"); |
| 1663 |
MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd"); |
| 1664 |
MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))"); |
| 1665 |
|
| 1666 |
#ifdef CONFIG_SYSCTL |
| 1667 |
#include <linux/sysctl.h> |
| 1668 |
|
| 1669 |
static const struct ctl_table sg_sysctls[] = { |
| 1670 |
{ |
| 1671 |
.procname = "sg-big-buff", |
| 1672 |
.data = &sg_big_buff, |
| 1673 |
.maxlen = sizeof(int), |
| 1674 |
.mode = 0444, |
| 1675 |
.proc_handler = proc_dointvec, |
| 1676 |
}, |
| 1677 |
}; |
| 1678 |
|
| 1679 |
static struct ctl_table_header *hdr; |
| 1680 |
static void register_sg_sysctls(void) |
| 1681 |
{ |
| 1682 |
if (!hdr) |
| 1683 |
hdr = register_sysctl("kernel", sg_sysctls); |
| 1684 |
} |
| 1685 |
|
| 1686 |
static void unregister_sg_sysctls(void) |
| 1687 |
{ |
| 1688 |
unregister_sysctl_table(hdr); |
| 1689 |
} |
| 1690 |
#else |
| 1691 |
#define register_sg_sysctls() do { } while (0) |
| 1692 |
#define unregister_sg_sysctls() do { } while (0) |
| 1693 |
#endif /* CONFIG_SYSCTL */ |
| 1694 |
|
| 1695 |
static int __init |
| 1696 |
init_sg(void) |
| 1697 |
{ |
| 1698 |
int rc; |
| 1699 |
|
| 1700 |
if (scatter_elem_sz < PAGE_SIZE) { |
| 1701 |
scatter_elem_sz = PAGE_SIZE; |
| 1702 |
scatter_elem_sz_prev = scatter_elem_sz; |
| 1703 |
} |
| 1704 |
if (def_reserved_size >= 0) |
| 1705 |
sg_big_buff = def_reserved_size; |
| 1706 |
else |
| 1707 |
def_reserved_size = sg_big_buff; |
| 1708 |
|
| 1709 |
rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), |
| 1710 |
SG_MAX_DEVS, "sg"); |
| 1711 |
if (rc) |
| 1712 |
return rc; |
| 1713 |
rc = class_register(&sg_sysfs_class); |
| 1714 |
if (rc) |
| 1715 |
goto err_out; |
| 1716 |
sg_sysfs_valid = 1; |
| 1717 |
rc = scsi_register_interface(&sg_interface); |
| 1718 |
if (0 == rc) { |
| 1719 |
register_sg_sysctls(); |
| 1720 |
#ifdef CONFIG_SCSI_PROC_FS |
| 1721 |
sg_proc_init(); |
| 1722 |
#endif /* CONFIG_SCSI_PROC_FS */ |
| 1723 |
return 0; |
| 1724 |
} |
| 1725 |
class_unregister(&sg_sysfs_class); |
| 1726 |
err_out: |
| 1727 |
unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS); |
| 1728 |
return rc; |
| 1729 |
} |
| 1730 |
|
| 1731 |
static void __exit |
| 1732 |
exit_sg(void) |
| 1733 |
{ |
| 1734 |
unregister_sg_sysctls(); |
| 1735 |
#ifdef CONFIG_SCSI_PROC_FS |
| 1736 |
remove_proc_subtree("scsi/sg", NULL); |
| 1737 |
#endif /* CONFIG_SCSI_PROC_FS */ |
| 1738 |
scsi_unregister_interface(&sg_interface); |
| 1739 |
class_unregister(&sg_sysfs_class); |
| 1740 |
sg_sysfs_valid = 0; |
| 1741 |
unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), |
| 1742 |
SG_MAX_DEVS); |
| 1743 |
idr_destroy(&sg_index_idr); |
| 1744 |
} |
| 1745 |
|
| 1746 |
static int |
| 1747 |
sg_start_req(Sg_request *srp, unsigned char *cmd) |
| 1748 |
{ |
| 1749 |
int res; |
| 1750 |
struct request *rq; |
| 1751 |
Sg_fd *sfp = srp->parentfp; |
| 1752 |
sg_io_hdr_t *hp = &srp->header; |
| 1753 |
int dxfer_len = (int) hp->dxfer_len; |
| 1754 |
int dxfer_dir = hp->dxfer_direction; |
| 1755 |
unsigned int iov_count = hp->iovec_count; |
| 1756 |
Sg_scatter_hold *req_schp = &srp->data; |
| 1757 |
Sg_scatter_hold *rsv_schp = &sfp->reserve; |
| 1758 |
struct request_queue *q = sfp->parentdp->device->request_queue; |
| 1759 |
struct rq_map_data *md, map_data; |
| 1760 |
int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? ITER_SOURCE : ITER_DEST; |
| 1761 |
struct scsi_cmnd *scmd; |
| 1762 |
|
| 1763 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 1764 |
"sg_start_req: dxfer_len=%d\n", |
| 1765 |
dxfer_len)); |
| 1766 |
|
| 1767 |
/* |
| 1768 |
* NOTE |
| 1769 |
* |
| 1770 |
* With scsi-mq enabled, there are a fixed number of preallocated |
| 1771 |
* requests equal in number to shost->can_queue. If all of the |
| 1772 |
* preallocated requests are already in use, then scsi_alloc_request() |
| 1773 |
* will sleep until an active command completes, freeing up a request. |
| 1774 |
* Although waiting in an asynchronous interface is less than ideal, we |
| 1775 |
* do not want to use BLK_MQ_REQ_NOWAIT here because userspace might |
| 1776 |
* not expect an EWOULDBLOCK from this condition. |
| 1777 |
*/ |
| 1778 |
rq = scsi_alloc_request(q, hp->dxfer_direction == SG_DXFER_TO_DEV ? |
| 1779 |
REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
| 1780 |
if (IS_ERR(rq)) |
| 1781 |
return PTR_ERR(rq); |
| 1782 |
scmd = blk_mq_rq_to_pdu(rq); |
| 1783 |
|
| 1784 |
if (hp->cmd_len > sizeof(scmd->cmnd)) { |
| 1785 |
blk_mq_free_request(rq); |
| 1786 |
return -EINVAL; |
| 1787 |
} |
| 1788 |
|
| 1789 |
memcpy(scmd->cmnd, cmd, hp->cmd_len); |
| 1790 |
scmd->cmd_len = hp->cmd_len; |
| 1791 |
|
| 1792 |
srp->rq = rq; |
| 1793 |
rq->end_io_data = srp; |
| 1794 |
scmd->allowed = SG_DEFAULT_RETRIES; |
| 1795 |
|
| 1796 |
if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE)) |
| 1797 |
return 0; |
| 1798 |
|
| 1799 |
if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO && |
| 1800 |
dxfer_dir != SG_DXFER_UNKNOWN && !iov_count && |
| 1801 |
blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len)) |
| 1802 |
md = NULL; |
| 1803 |
else |
| 1804 |
md = &map_data; |
| 1805 |
|
| 1806 |
if (md) { |
| 1807 |
mutex_lock(&sfp->f_mutex); |
| 1808 |
if (dxfer_len <= rsv_schp->bufflen && |
| 1809 |
!sfp->res_in_use) { |
| 1810 |
sfp->res_in_use = 1; |
| 1811 |
sg_link_reserve(sfp, srp, dxfer_len); |
| 1812 |
} else if (hp->flags & SG_FLAG_MMAP_IO) { |
| 1813 |
res = -EBUSY; /* sfp->res_in_use == 1 */ |
| 1814 |
if (dxfer_len > rsv_schp->bufflen) |
| 1815 |
res = -ENOMEM; |
| 1816 |
mutex_unlock(&sfp->f_mutex); |
| 1817 |
return res; |
| 1818 |
} else { |
| 1819 |
res = sg_build_indirect(req_schp, sfp, dxfer_len); |
| 1820 |
if (res) { |
| 1821 |
mutex_unlock(&sfp->f_mutex); |
| 1822 |
return res; |
| 1823 |
} |
| 1824 |
} |
| 1825 |
mutex_unlock(&sfp->f_mutex); |
| 1826 |
|
| 1827 |
md->pages = req_schp->pages; |
| 1828 |
md->page_order = req_schp->page_order; |
| 1829 |
md->nr_entries = req_schp->k_use_sg; |
| 1830 |
md->offset = 0; |
| 1831 |
md->null_mapped = hp->dxferp ? 0 : 1; |
| 1832 |
if (dxfer_dir == SG_DXFER_TO_FROM_DEV) |
| 1833 |
md->from_user = 1; |
| 1834 |
else |
| 1835 |
md->from_user = 0; |
| 1836 |
} |
| 1837 |
|
| 1838 |
res = blk_rq_map_user_io(rq, md, hp->dxferp, hp->dxfer_len, |
| 1839 |
GFP_ATOMIC, iov_count, iov_count, 1, rw); |
| 1840 |
if (!res) { |
| 1841 |
srp->bio = rq->bio; |
| 1842 |
|
| 1843 |
if (!md) { |
| 1844 |
req_schp->dio_in_use = 1; |
| 1845 |
hp->info |= SG_INFO_DIRECT_IO; |
| 1846 |
} |
| 1847 |
} |
| 1848 |
return res; |
| 1849 |
} |
| 1850 |
|
| 1851 |
static int |
| 1852 |
sg_finish_rem_req(Sg_request *srp) |
| 1853 |
{ |
| 1854 |
int ret = 0; |
| 1855 |
|
| 1856 |
Sg_fd *sfp = srp->parentfp; |
| 1857 |
Sg_scatter_hold *req_schp = &srp->data; |
| 1858 |
|
| 1859 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 1860 |
"sg_finish_rem_req: res_used=%d\n", |
| 1861 |
(int) srp->res_used)); |
| 1862 |
if (srp->bio) |
| 1863 |
ret = blk_rq_unmap_user(srp->bio); |
| 1864 |
|
| 1865 |
if (srp->rq) |
| 1866 |
blk_mq_free_request(srp->rq); |
| 1867 |
|
| 1868 |
if (srp->res_used) |
| 1869 |
sg_unlink_reserve(sfp, srp); |
| 1870 |
else |
| 1871 |
sg_remove_scat(sfp, req_schp); |
| 1872 |
|
| 1873 |
return ret; |
| 1874 |
} |
| 1875 |
|
| 1876 |
static int |
| 1877 |
sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize) |
| 1878 |
{ |
| 1879 |
int sg_bufflen = tablesize * sizeof(struct page *); |
| 1880 |
gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN; |
| 1881 |
|
| 1882 |
schp->pages = kzalloc(sg_bufflen, gfp_flags); |
| 1883 |
if (!schp->pages) |
| 1884 |
return -ENOMEM; |
| 1885 |
schp->sglist_len = sg_bufflen; |
| 1886 |
return tablesize; /* number of scat_gath elements allocated */ |
| 1887 |
} |
| 1888 |
|
| 1889 |
static int |
| 1890 |
sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size) |
| 1891 |
{ |
| 1892 |
int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems; |
| 1893 |
int sg_tablesize = sfp->parentdp->sg_tablesize; |
| 1894 |
int blk_size = buff_size, order; |
| 1895 |
gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO; |
| 1896 |
|
| 1897 |
if (blk_size < 0) |
| 1898 |
return -EFAULT; |
| 1899 |
if (0 == blk_size) |
| 1900 |
++blk_size; /* don't know why */ |
| 1901 |
/* round request up to next highest SG_SECTOR_SZ byte boundary */ |
| 1902 |
blk_size = ALIGN(blk_size, SG_SECTOR_SZ); |
| 1903 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 1904 |
"sg_build_indirect: buff_size=%d, blk_size=%d\n", |
| 1905 |
buff_size, blk_size)); |
| 1906 |
|
| 1907 |
/* N.B. ret_sz carried into this block ... */ |
| 1908 |
mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize); |
| 1909 |
if (mx_sc_elems < 0) |
| 1910 |
return mx_sc_elems; /* most likely -ENOMEM */ |
| 1911 |
|
| 1912 |
num = scatter_elem_sz; |
| 1913 |
if (unlikely(num != scatter_elem_sz_prev)) { |
| 1914 |
if (num < PAGE_SIZE) { |
| 1915 |
scatter_elem_sz = PAGE_SIZE; |
| 1916 |
scatter_elem_sz_prev = PAGE_SIZE; |
| 1917 |
} else |
| 1918 |
scatter_elem_sz_prev = num; |
| 1919 |
} |
| 1920 |
|
| 1921 |
order = get_order(num); |
| 1922 |
retry: |
| 1923 |
ret_sz = 1 << (PAGE_SHIFT + order); |
| 1924 |
|
| 1925 |
for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems; |
| 1926 |
k++, rem_sz -= ret_sz) { |
| 1927 |
|
| 1928 |
num = (rem_sz > scatter_elem_sz_prev) ? |
| 1929 |
scatter_elem_sz_prev : rem_sz; |
| 1930 |
|
| 1931 |
schp->pages[k] = alloc_pages(gfp_mask, order); |
| 1932 |
if (!schp->pages[k]) |
| 1933 |
goto out; |
| 1934 |
|
| 1935 |
if (num == scatter_elem_sz_prev) { |
| 1936 |
if (unlikely(ret_sz > scatter_elem_sz_prev)) { |
| 1937 |
scatter_elem_sz = ret_sz; |
| 1938 |
scatter_elem_sz_prev = ret_sz; |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp, |
| 1943 |
"sg_build_indirect: k=%d, num=%d, ret_sz=%d\n", |
| 1944 |
k, num, ret_sz)); |
| 1945 |
} /* end of for loop */ |
| 1946 |
|
| 1947 |
schp->page_order = order; |
| 1948 |
schp->k_use_sg = k; |
| 1949 |
SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp, |
| 1950 |
"sg_build_indirect: k_use_sg=%d, rem_sz=%d\n", |
| 1951 |
k, rem_sz)); |
| 1952 |
|
| 1953 |
schp->bufflen = blk_size; |
| 1954 |
if (rem_sz > 0) /* must have failed */ |
| 1955 |
return -ENOMEM; |
| 1956 |
return 0; |
| 1957 |
out: |
| 1958 |
for (i = 0; i < k; i++) |
| 1959 |
__free_pages(schp->pages[i], order); |
| 1960 |
|
| 1961 |
if (--order >= 0) |
| 1962 |
goto retry; |
| 1963 |
|
| 1964 |
return -ENOMEM; |
| 1965 |
} |
| 1966 |
|
| 1967 |
static void |
| 1968 |
sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp) |
| 1969 |
{ |
| 1970 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 1971 |
"sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg)); |
| 1972 |
if (schp->pages && schp->sglist_len > 0) { |
| 1973 |
if (!schp->dio_in_use) { |
| 1974 |
int k; |
| 1975 |
|
| 1976 |
for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) { |
| 1977 |
SCSI_LOG_TIMEOUT(5, |
| 1978 |
sg_printk(KERN_INFO, sfp->parentdp, |
| 1979 |
"sg_remove_scat: k=%d, pg=0x%p\n", |
| 1980 |
k, schp->pages[k])); |
| 1981 |
__free_pages(schp->pages[k], schp->page_order); |
| 1982 |
} |
| 1983 |
|
| 1984 |
kfree(schp->pages); |
| 1985 |
} |
| 1986 |
} |
| 1987 |
memset(schp, 0, sizeof (*schp)); |
| 1988 |
} |
| 1989 |
|
| 1990 |
static int |
| 1991 |
sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer) |
| 1992 |
{ |
| 1993 |
Sg_scatter_hold *schp = &srp->data; |
| 1994 |
int k, num; |
| 1995 |
|
| 1996 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp, |
| 1997 |
"sg_read_oxfer: num_read_xfer=%d\n", |
| 1998 |
num_read_xfer)); |
| 1999 |
if ((!outp) || (num_read_xfer <= 0)) |
| 2000 |
return 0; |
| 2001 |
|
| 2002 |
num = 1 << (PAGE_SHIFT + schp->page_order); |
| 2003 |
for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) { |
| 2004 |
if (num > num_read_xfer) { |
| 2005 |
if (copy_to_user(outp, page_address(schp->pages[k]), |
| 2006 |
num_read_xfer)) |
| 2007 |
return -EFAULT; |
| 2008 |
break; |
| 2009 |
} else { |
| 2010 |
if (copy_to_user(outp, page_address(schp->pages[k]), |
| 2011 |
num)) |
| 2012 |
return -EFAULT; |
| 2013 |
num_read_xfer -= num; |
| 2014 |
if (num_read_xfer <= 0) |
| 2015 |
break; |
| 2016 |
outp += num; |
| 2017 |
} |
| 2018 |
} |
| 2019 |
|
| 2020 |
return 0; |
| 2021 |
} |
| 2022 |
|
| 2023 |
static void |
| 2024 |
sg_build_reserve(Sg_fd * sfp, int req_size) |
| 2025 |
{ |
| 2026 |
Sg_scatter_hold *schp = &sfp->reserve; |
| 2027 |
|
| 2028 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 2029 |
"sg_build_reserve: req_size=%d\n", req_size)); |
| 2030 |
do { |
| 2031 |
if (req_size < PAGE_SIZE) |
| 2032 |
req_size = PAGE_SIZE; |
| 2033 |
if (0 == sg_build_indirect(schp, sfp, req_size)) |
| 2034 |
return; |
| 2035 |
else |
| 2036 |
sg_remove_scat(sfp, schp); |
| 2037 |
req_size >>= 1; /* divide by 2 */ |
| 2038 |
} while (req_size > (PAGE_SIZE / 2)); |
| 2039 |
} |
| 2040 |
|
| 2041 |
static void |
| 2042 |
sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size) |
| 2043 |
{ |
| 2044 |
Sg_scatter_hold *req_schp = &srp->data; |
| 2045 |
Sg_scatter_hold *rsv_schp = &sfp->reserve; |
| 2046 |
int k, num, rem; |
| 2047 |
|
| 2048 |
srp->res_used = 1; |
| 2049 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp, |
| 2050 |
"sg_link_reserve: size=%d\n", size)); |
| 2051 |
rem = size; |
| 2052 |
|
| 2053 |
num = 1 << (PAGE_SHIFT + rsv_schp->page_order); |
| 2054 |
for (k = 0; k < rsv_schp->k_use_sg; k++) { |
| 2055 |
if (rem <= num) { |
| 2056 |
req_schp->k_use_sg = k + 1; |
| 2057 |
req_schp->sglist_len = rsv_schp->sglist_len; |
| 2058 |
req_schp->pages = rsv_schp->pages; |
| 2059 |
|
| 2060 |
req_schp->bufflen = size; |
| 2061 |
req_schp->page_order = rsv_schp->page_order; |
| 2062 |
break; |
| 2063 |
} else |
| 2064 |
rem -= num; |
| 2065 |
} |
| 2066 |
|
| 2067 |
if (k >= rsv_schp->k_use_sg) |
| 2068 |
SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp, |
| 2069 |
"sg_link_reserve: BAD size\n")); |
| 2070 |
} |
| 2071 |
|
| 2072 |
static void |
| 2073 |
sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp) |
| 2074 |
{ |
| 2075 |
Sg_scatter_hold *req_schp = &srp->data; |
| 2076 |
|
| 2077 |
SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp, |
| 2078 |
"sg_unlink_reserve: req->k_use_sg=%d\n", |
| 2079 |
(int) req_schp->k_use_sg)); |
| 2080 |
req_schp->k_use_sg = 0; |
| 2081 |
req_schp->bufflen = 0; |
| 2082 |
req_schp->pages = NULL; |
| 2083 |
req_schp->page_order = 0; |
| 2084 |
req_schp->sglist_len = 0; |
| 2085 |
srp->res_used = 0; |
| 2086 |
/* Called without mutex lock to avoid deadlock */ |
| 2087 |
sfp->res_in_use = 0; |
| 2088 |
} |
| 2089 |
|
| 2090 |
static Sg_request * |
| 2091 |
sg_get_rq_mark(Sg_fd * sfp, int pack_id, bool *busy) |
| 2092 |
{ |
| 2093 |
Sg_request *resp; |
| 2094 |
unsigned long iflags; |
| 2095 |
|
| 2096 |
*busy = false; |
| 2097 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2098 |
list_for_each_entry(resp, &sfp->rq_list, entry) { |
| 2099 |
/* look for requests that are not SG_IO owned */ |
| 2100 |
if ((!resp->sg_io_owned) && |
| 2101 |
((-1 == pack_id) || (resp->header.pack_id == pack_id))) { |
| 2102 |
switch (resp->done) { |
| 2103 |
case 0: /* request active */ |
| 2104 |
*busy = true; |
| 2105 |
break; |
| 2106 |
case 1: /* request done; response ready to return */ |
| 2107 |
resp->done = 2; /* guard against other readers */ |
| 2108 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2109 |
return resp; |
| 2110 |
case 2: /* response already being returned */ |
| 2111 |
break; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
} |
| 2115 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2116 |
return NULL; |
| 2117 |
} |
| 2118 |
|
| 2119 |
/* always adds to end of list */ |
| 2120 |
static Sg_request * |
| 2121 |
sg_add_request(Sg_fd * sfp) |
| 2122 |
{ |
| 2123 |
int k; |
| 2124 |
unsigned long iflags; |
| 2125 |
Sg_request *rp = sfp->req_arr; |
| 2126 |
|
| 2127 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2128 |
if (!list_empty(&sfp->rq_list)) { |
| 2129 |
if (!sfp->cmd_q) |
| 2130 |
goto out_unlock; |
| 2131 |
|
| 2132 |
for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) { |
| 2133 |
if (!rp->parentfp) |
| 2134 |
break; |
| 2135 |
} |
| 2136 |
if (k >= SG_MAX_QUEUE) |
| 2137 |
goto out_unlock; |
| 2138 |
} |
| 2139 |
memset(rp, 0, sizeof (Sg_request)); |
| 2140 |
rp->parentfp = sfp; |
| 2141 |
rp->header.duration = jiffies_to_msecs(jiffies); |
| 2142 |
list_add_tail(&rp->entry, &sfp->rq_list); |
| 2143 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2144 |
return rp; |
| 2145 |
out_unlock: |
| 2146 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2147 |
return NULL; |
| 2148 |
} |
| 2149 |
|
| 2150 |
/* Return of 1 for found; 0 for not found */ |
| 2151 |
static int |
| 2152 |
sg_remove_request(Sg_fd * sfp, Sg_request * srp) |
| 2153 |
{ |
| 2154 |
unsigned long iflags; |
| 2155 |
int res = 0; |
| 2156 |
|
| 2157 |
if (!sfp || !srp || list_empty(&sfp->rq_list)) |
| 2158 |
return res; |
| 2159 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2160 |
if (!list_empty(&srp->entry)) { |
| 2161 |
list_del(&srp->entry); |
| 2162 |
srp->parentfp = NULL; |
| 2163 |
res = 1; |
| 2164 |
} |
| 2165 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2166 |
|
| 2167 |
/* |
| 2168 |
* If the device is detaching, wakeup any readers in case we just |
| 2169 |
* removed the last response, which would leave nothing for them to |
| 2170 |
* return other than -ENODEV. |
| 2171 |
*/ |
| 2172 |
if (unlikely(atomic_read(&sfp->parentdp->detaching))) |
| 2173 |
wake_up_interruptible_all(&sfp->read_wait); |
| 2174 |
|
| 2175 |
return res; |
| 2176 |
} |
| 2177 |
|
| 2178 |
static Sg_fd * |
| 2179 |
sg_add_sfp(Sg_device * sdp) |
| 2180 |
{ |
| 2181 |
Sg_fd *sfp; |
| 2182 |
unsigned long iflags; |
| 2183 |
int bufflen; |
| 2184 |
|
| 2185 |
sfp = kzalloc_obj(*sfp, GFP_ATOMIC | __GFP_NOWARN); |
| 2186 |
if (!sfp) |
| 2187 |
return ERR_PTR(-ENOMEM); |
| 2188 |
|
| 2189 |
init_waitqueue_head(&sfp->read_wait); |
| 2190 |
rwlock_init(&sfp->rq_list_lock); |
| 2191 |
INIT_LIST_HEAD(&sfp->rq_list); |
| 2192 |
kref_init(&sfp->f_ref); |
| 2193 |
mutex_init(&sfp->f_mutex); |
| 2194 |
sfp->timeout = SG_DEFAULT_TIMEOUT; |
| 2195 |
sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER; |
| 2196 |
sfp->force_packid = SG_DEF_FORCE_PACK_ID; |
| 2197 |
sfp->cmd_q = SG_DEF_COMMAND_Q; |
| 2198 |
sfp->keep_orphan = SG_DEF_KEEP_ORPHAN; |
| 2199 |
sfp->parentdp = sdp; |
| 2200 |
write_lock_irqsave(&sdp->sfd_lock, iflags); |
| 2201 |
if (atomic_read(&sdp->detaching)) { |
| 2202 |
write_unlock_irqrestore(&sdp->sfd_lock, iflags); |
| 2203 |
kfree(sfp); |
| 2204 |
return ERR_PTR(-ENODEV); |
| 2205 |
} |
| 2206 |
list_add_tail(&sfp->sfd_siblings, &sdp->sfds); |
| 2207 |
write_unlock_irqrestore(&sdp->sfd_lock, iflags); |
| 2208 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 2209 |
"sg_add_sfp: sfp=0x%p\n", sfp)); |
| 2210 |
if (unlikely(sg_big_buff != def_reserved_size)) |
| 2211 |
sg_big_buff = def_reserved_size; |
| 2212 |
|
| 2213 |
bufflen = min_t(int, sg_big_buff, |
| 2214 |
max_sectors_bytes(sdp->device->request_queue)); |
| 2215 |
sg_build_reserve(sfp, bufflen); |
| 2216 |
SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp, |
| 2217 |
"sg_add_sfp: bufflen=%d, k_use_sg=%d\n", |
| 2218 |
sfp->reserve.bufflen, |
| 2219 |
sfp->reserve.k_use_sg)); |
| 2220 |
|
| 2221 |
kref_get(&sdp->d_ref); |
| 2222 |
__module_get(THIS_MODULE); |
| 2223 |
return sfp; |
| 2224 |
} |
| 2225 |
|
| 2226 |
static void |
| 2227 |
sg_remove_sfp_usercontext(struct work_struct *work) |
| 2228 |
{ |
| 2229 |
struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work); |
| 2230 |
struct sg_device *sdp = sfp->parentdp; |
| 2231 |
struct scsi_device *device = sdp->device; |
| 2232 |
Sg_request *srp; |
| 2233 |
unsigned long iflags; |
| 2234 |
|
| 2235 |
/* Cleanup any responses which were never read(). */ |
| 2236 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2237 |
while (!list_empty(&sfp->rq_list)) { |
| 2238 |
srp = list_first_entry(&sfp->rq_list, Sg_request, entry); |
| 2239 |
list_del(&srp->entry); |
| 2240 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2241 |
|
| 2242 |
sg_finish_rem_req(srp); |
| 2243 |
/* |
| 2244 |
* sg_rq_end_io() uses srp->parentfp. Hence, only clear |
| 2245 |
* srp->parentfp after blk_mq_free_request() has been called. |
| 2246 |
*/ |
| 2247 |
srp->parentfp = NULL; |
| 2248 |
|
| 2249 |
write_lock_irqsave(&sfp->rq_list_lock, iflags); |
| 2250 |
} |
| 2251 |
write_unlock_irqrestore(&sfp->rq_list_lock, iflags); |
| 2252 |
|
| 2253 |
if (sfp->reserve.bufflen > 0) { |
| 2254 |
SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp, |
| 2255 |
"sg_remove_sfp: bufflen=%d, k_use_sg=%d\n", |
| 2256 |
(int) sfp->reserve.bufflen, |
| 2257 |
(int) sfp->reserve.k_use_sg)); |
| 2258 |
sg_remove_scat(sfp, &sfp->reserve); |
| 2259 |
} |
| 2260 |
|
| 2261 |
SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp, |
| 2262 |
"sg_remove_sfp: sfp=0x%p\n", sfp)); |
| 2263 |
kfree(sfp); |
| 2264 |
|
| 2265 |
kref_put(&sdp->d_ref, sg_device_destroy); |
| 2266 |
scsi_device_put(device); |
| 2267 |
module_put(THIS_MODULE); |
| 2268 |
} |
| 2269 |
|
| 2270 |
static void |
| 2271 |
sg_remove_sfp(struct kref *kref) |
| 2272 |
{ |
| 2273 |
struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref); |
| 2274 |
struct sg_device *sdp = sfp->parentdp; |
| 2275 |
unsigned long iflags; |
| 2276 |
|
| 2277 |
write_lock_irqsave(&sdp->sfd_lock, iflags); |
| 2278 |
list_del(&sfp->sfd_siblings); |
| 2279 |
write_unlock_irqrestore(&sdp->sfd_lock, iflags); |
| 2280 |
|
| 2281 |
INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext); |
| 2282 |
schedule_work(&sfp->ew.work); |
| 2283 |
} |
| 2284 |
|
| 2285 |
#ifdef CONFIG_SCSI_PROC_FS |
| 2286 |
static int |
| 2287 |
sg_idr_max_id(int id, void *p, void *data) |
| 2288 |
{ |
| 2289 |
int *k = data; |
| 2290 |
|
| 2291 |
if (*k < id) |
| 2292 |
*k = id; |
| 2293 |
|
| 2294 |
return 0; |
| 2295 |
} |
| 2296 |
|
| 2297 |
static int |
| 2298 |
sg_last_dev(void) |
| 2299 |
{ |
| 2300 |
int k = -1; |
| 2301 |
unsigned long iflags; |
| 2302 |
|
| 2303 |
read_lock_irqsave(&sg_index_lock, iflags); |
| 2304 |
idr_for_each(&sg_index_idr, sg_idr_max_id, &k); |
| 2305 |
read_unlock_irqrestore(&sg_index_lock, iflags); |
| 2306 |
return k + 1; /* origin 1 */ |
| 2307 |
} |
| 2308 |
#endif |
| 2309 |
|
| 2310 |
/* must be called with sg_index_lock held */ |
| 2311 |
static Sg_device *sg_lookup_dev(int dev) |
| 2312 |
{ |
| 2313 |
return idr_find(&sg_index_idr, dev); |
| 2314 |
} |
| 2315 |
|
| 2316 |
static Sg_device * |
| 2317 |
sg_get_dev(int dev) |
| 2318 |
{ |
| 2319 |
struct sg_device *sdp; |
| 2320 |
unsigned long flags; |
| 2321 |
|
| 2322 |
read_lock_irqsave(&sg_index_lock, flags); |
| 2323 |
sdp = sg_lookup_dev(dev); |
| 2324 |
if (!sdp) |
| 2325 |
sdp = ERR_PTR(-ENXIO); |
| 2326 |
else if (atomic_read(&sdp->detaching)) { |
| 2327 |
/* If sdp->detaching, then the refcount may already be 0, in |
| 2328 |
* which case it would be a bug to do kref_get(). |
| 2329 |
*/ |
| 2330 |
sdp = ERR_PTR(-ENODEV); |
| 2331 |
} else |
| 2332 |
kref_get(&sdp->d_ref); |
| 2333 |
read_unlock_irqrestore(&sg_index_lock, flags); |
| 2334 |
|
| 2335 |
return sdp; |
| 2336 |
} |
| 2337 |
|
| 2338 |
#ifdef CONFIG_SCSI_PROC_FS |
| 2339 |
static int sg_proc_seq_show_int(struct seq_file *s, void *v); |
| 2340 |
|
| 2341 |
static int sg_proc_single_open_adio(struct inode *inode, struct file *file); |
| 2342 |
static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer, |
| 2343 |
size_t count, loff_t *off); |
| 2344 |
static const struct proc_ops adio_proc_ops = { |
| 2345 |
.proc_open = sg_proc_single_open_adio, |
| 2346 |
.proc_read = seq_read, |
| 2347 |
.proc_lseek = seq_lseek, |
| 2348 |
.proc_write = sg_proc_write_adio, |
| 2349 |
.proc_release = single_release, |
| 2350 |
}; |
| 2351 |
|
| 2352 |
static int sg_proc_single_open_dressz(struct inode *inode, struct file *file); |
| 2353 |
static ssize_t sg_proc_write_dressz(struct file *filp, |
| 2354 |
const char __user *buffer, size_t count, loff_t *off); |
| 2355 |
static const struct proc_ops dressz_proc_ops = { |
| 2356 |
.proc_open = sg_proc_single_open_dressz, |
| 2357 |
.proc_read = seq_read, |
| 2358 |
.proc_lseek = seq_lseek, |
| 2359 |
.proc_write = sg_proc_write_dressz, |
| 2360 |
.proc_release = single_release, |
| 2361 |
}; |
| 2362 |
|
| 2363 |
static int sg_proc_seq_show_version(struct seq_file *s, void *v); |
| 2364 |
static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v); |
| 2365 |
static int sg_proc_seq_show_dev(struct seq_file *s, void *v); |
| 2366 |
static void * dev_seq_start(struct seq_file *s, loff_t *pos); |
| 2367 |
static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos); |
| 2368 |
static void dev_seq_stop(struct seq_file *s, void *v); |
| 2369 |
static const struct seq_operations dev_seq_ops = { |
| 2370 |
.start = dev_seq_start, |
| 2371 |
.next = dev_seq_next, |
| 2372 |
.stop = dev_seq_stop, |
| 2373 |
.show = sg_proc_seq_show_dev, |
| 2374 |
}; |
| 2375 |
|
| 2376 |
static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v); |
| 2377 |
static const struct seq_operations devstrs_seq_ops = { |
| 2378 |
.start = dev_seq_start, |
| 2379 |
.next = dev_seq_next, |
| 2380 |
.stop = dev_seq_stop, |
| 2381 |
.show = sg_proc_seq_show_devstrs, |
| 2382 |
}; |
| 2383 |
|
| 2384 |
static int sg_proc_seq_show_debug(struct seq_file *s, void *v); |
| 2385 |
static const struct seq_operations debug_seq_ops = { |
| 2386 |
.start = dev_seq_start, |
| 2387 |
.next = dev_seq_next, |
| 2388 |
.stop = dev_seq_stop, |
| 2389 |
.show = sg_proc_seq_show_debug, |
| 2390 |
}; |
| 2391 |
|
| 2392 |
static int |
| 2393 |
sg_proc_init(void) |
| 2394 |
{ |
| 2395 |
struct proc_dir_entry *p; |
| 2396 |
|
| 2397 |
p = proc_mkdir("scsi/sg", NULL); |
| 2398 |
if (!p) |
| 2399 |
return 1; |
| 2400 |
|
| 2401 |
proc_create("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops); |
| 2402 |
proc_create_seq("debug", S_IRUGO, p, &debug_seq_ops); |
| 2403 |
proc_create("def_reserved_size", S_IRUGO | S_IWUSR, p, &dressz_proc_ops); |
| 2404 |
proc_create_single("device_hdr", S_IRUGO, p, sg_proc_seq_show_devhdr); |
| 2405 |
proc_create_seq("devices", S_IRUGO, p, &dev_seq_ops); |
| 2406 |
proc_create_seq("device_strs", S_IRUGO, p, &devstrs_seq_ops); |
| 2407 |
proc_create_single("version", S_IRUGO, p, sg_proc_seq_show_version); |
| 2408 |
return 0; |
| 2409 |
} |
| 2410 |
|
| 2411 |
|
| 2412 |
static int sg_proc_seq_show_int(struct seq_file *s, void *v) |
| 2413 |
{ |
| 2414 |
seq_printf(s, "%d\n", *((int *)s->private)); |
| 2415 |
return 0; |
| 2416 |
} |
| 2417 |
|
| 2418 |
static int sg_proc_single_open_adio(struct inode *inode, struct file *file) |
| 2419 |
{ |
| 2420 |
return single_open(file, sg_proc_seq_show_int, &sg_allow_dio); |
| 2421 |
} |
| 2422 |
|
| 2423 |
static ssize_t |
| 2424 |
sg_proc_write_adio(struct file *filp, const char __user *buffer, |
| 2425 |
size_t count, loff_t *off) |
| 2426 |
{ |
| 2427 |
int err; |
| 2428 |
unsigned long num; |
| 2429 |
|
| 2430 |
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 2431 |
return -EACCES; |
| 2432 |
err = kstrtoul_from_user(buffer, count, 0, &num); |
| 2433 |
if (err) |
| 2434 |
return err; |
| 2435 |
sg_allow_dio = num ? 1 : 0; |
| 2436 |
return count; |
| 2437 |
} |
| 2438 |
|
| 2439 |
static int sg_proc_single_open_dressz(struct inode *inode, struct file *file) |
| 2440 |
{ |
| 2441 |
return single_open(file, sg_proc_seq_show_int, &sg_big_buff); |
| 2442 |
} |
| 2443 |
|
| 2444 |
static ssize_t |
| 2445 |
sg_proc_write_dressz(struct file *filp, const char __user *buffer, |
| 2446 |
size_t count, loff_t *off) |
| 2447 |
{ |
| 2448 |
int err; |
| 2449 |
unsigned long k = ULONG_MAX; |
| 2450 |
|
| 2451 |
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 2452 |
return -EACCES; |
| 2453 |
|
| 2454 |
err = kstrtoul_from_user(buffer, count, 0, &k); |
| 2455 |
if (err) |
| 2456 |
return err; |
| 2457 |
if (k <= 1048576) { /* limit "big buff" to 1 MB */ |
| 2458 |
sg_big_buff = k; |
| 2459 |
return count; |
| 2460 |
} |
| 2461 |
return -ERANGE; |
| 2462 |
} |
| 2463 |
|
| 2464 |
static int sg_proc_seq_show_version(struct seq_file *s, void *v) |
| 2465 |
{ |
| 2466 |
seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR, |
| 2467 |
sg_version_date); |
| 2468 |
return 0; |
| 2469 |
} |
| 2470 |
|
| 2471 |
static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v) |
| 2472 |
{ |
| 2473 |
seq_puts(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n"); |
| 2474 |
return 0; |
| 2475 |
} |
| 2476 |
|
| 2477 |
struct sg_proc_deviter { |
| 2478 |
loff_t index; |
| 2479 |
size_t max; |
| 2480 |
}; |
| 2481 |
|
| 2482 |
static void * dev_seq_start(struct seq_file *s, loff_t *pos) |
| 2483 |
{ |
| 2484 |
struct sg_proc_deviter * it = kmalloc_obj(*it); |
| 2485 |
|
| 2486 |
s->private = it; |
| 2487 |
if (! it) |
| 2488 |
return NULL; |
| 2489 |
|
| 2490 |
it->index = *pos; |
| 2491 |
it->max = sg_last_dev(); |
| 2492 |
if (it->index >= it->max) |
| 2493 |
return NULL; |
| 2494 |
return it; |
| 2495 |
} |
| 2496 |
|
| 2497 |
static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 2498 |
{ |
| 2499 |
struct sg_proc_deviter * it = s->private; |
| 2500 |
|
| 2501 |
*pos = ++it->index; |
| 2502 |
return (it->index < it->max) ? it : NULL; |
| 2503 |
} |
| 2504 |
|
| 2505 |
static void dev_seq_stop(struct seq_file *s, void *v) |
| 2506 |
{ |
| 2507 |
kfree(s->private); |
| 2508 |
} |
| 2509 |
|
| 2510 |
static int sg_proc_seq_show_dev(struct seq_file *s, void *v) |
| 2511 |
{ |
| 2512 |
struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2513 |
Sg_device *sdp; |
| 2514 |
struct scsi_device *scsidp; |
| 2515 |
unsigned long iflags; |
| 2516 |
|
| 2517 |
read_lock_irqsave(&sg_index_lock, iflags); |
| 2518 |
sdp = it ? sg_lookup_dev(it->index) : NULL; |
| 2519 |
if ((NULL == sdp) || (NULL == sdp->device) || |
| 2520 |
(atomic_read(&sdp->detaching))) |
| 2521 |
seq_puts(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n"); |
| 2522 |
else { |
| 2523 |
scsidp = sdp->device; |
| 2524 |
seq_printf(s, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n", |
| 2525 |
scsidp->host->host_no, scsidp->channel, |
| 2526 |
scsidp->id, scsidp->lun, (int) scsidp->type, |
| 2527 |
1, |
| 2528 |
(int) scsidp->queue_depth, |
| 2529 |
(int) scsi_device_busy(scsidp), |
| 2530 |
(int) scsi_device_online(scsidp)); |
| 2531 |
} |
| 2532 |
read_unlock_irqrestore(&sg_index_lock, iflags); |
| 2533 |
return 0; |
| 2534 |
} |
| 2535 |
|
| 2536 |
static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v) |
| 2537 |
{ |
| 2538 |
struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2539 |
Sg_device *sdp; |
| 2540 |
struct scsi_device *scsidp; |
| 2541 |
unsigned long iflags; |
| 2542 |
|
| 2543 |
read_lock_irqsave(&sg_index_lock, iflags); |
| 2544 |
sdp = it ? sg_lookup_dev(it->index) : NULL; |
| 2545 |
scsidp = sdp ? sdp->device : NULL; |
| 2546 |
if (sdp && scsidp && (!atomic_read(&sdp->detaching))) |
| 2547 |
seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n", |
| 2548 |
scsidp->vendor, scsidp->model, scsidp->rev); |
| 2549 |
else |
| 2550 |
seq_puts(s, "<no active device>\n"); |
| 2551 |
read_unlock_irqrestore(&sg_index_lock, iflags); |
| 2552 |
return 0; |
| 2553 |
} |
| 2554 |
|
| 2555 |
/* must be called while holding sg_index_lock */ |
| 2556 |
static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp) |
| 2557 |
{ |
| 2558 |
int k, new_interface, blen, usg; |
| 2559 |
Sg_request *srp; |
| 2560 |
Sg_fd *fp; |
| 2561 |
const sg_io_hdr_t *hp; |
| 2562 |
const char * cp; |
| 2563 |
unsigned int ms; |
| 2564 |
unsigned int duration; |
| 2565 |
|
| 2566 |
k = 0; |
| 2567 |
list_for_each_entry(fp, &sdp->sfds, sfd_siblings) { |
| 2568 |
k++; |
| 2569 |
read_lock(&fp->rq_list_lock); /* irqs already disabled */ |
| 2570 |
seq_printf(s, " FD(%d): timeout=%dms bufflen=%d " |
| 2571 |
"(res)sgat=%d low_dma=%d\n", k, |
| 2572 |
jiffies_to_msecs(fp->timeout), |
| 2573 |
fp->reserve.bufflen, |
| 2574 |
(int) fp->reserve.k_use_sg, 0); |
| 2575 |
seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n", |
| 2576 |
(int) fp->cmd_q, (int) fp->force_packid, |
| 2577 |
(int) fp->keep_orphan); |
| 2578 |
list_for_each_entry(srp, &fp->rq_list, entry) { |
| 2579 |
hp = &srp->header; |
| 2580 |
new_interface = (hp->interface_id == '\0') ? 0 : 1; |
| 2581 |
if (srp->res_used) { |
| 2582 |
if (new_interface && |
| 2583 |
(SG_FLAG_MMAP_IO & hp->flags)) |
| 2584 |
cp = " mmap>> "; |
| 2585 |
else |
| 2586 |
cp = " rb>> "; |
| 2587 |
} else { |
| 2588 |
if (SG_INFO_DIRECT_IO_MASK & hp->info) |
| 2589 |
cp = " dio>> "; |
| 2590 |
else |
| 2591 |
cp = " "; |
| 2592 |
} |
| 2593 |
seq_puts(s, cp); |
| 2594 |
blen = srp->data.bufflen; |
| 2595 |
usg = srp->data.k_use_sg; |
| 2596 |
seq_puts(s, srp->done ? |
| 2597 |
((1 == srp->done) ? "rcv:" : "fin:") |
| 2598 |
: "act:"); |
| 2599 |
seq_printf(s, " id=%d blen=%d", |
| 2600 |
srp->header.pack_id, blen); |
| 2601 |
if (srp->done) |
| 2602 |
seq_printf(s, " dur=%u", hp->duration); |
| 2603 |
else { |
| 2604 |
ms = jiffies_to_msecs(jiffies); |
| 2605 |
duration = READ_ONCE(hp->duration); |
| 2606 |
if (duration) |
| 2607 |
duration = (ms > duration ? |
| 2608 |
ms - duration : 0); |
| 2609 |
seq_printf(s, " t_o/elap=%u/%u", |
| 2610 |
(new_interface ? hp->timeout : |
| 2611 |
jiffies_to_msecs(fp->timeout)), |
| 2612 |
duration); |
| 2613 |
} |
| 2614 |
seq_printf(s, "ms sgat=%d op=0x%02x\n", usg, |
| 2615 |
(int) srp->data.cmd_opcode); |
| 2616 |
} |
| 2617 |
if (list_empty(&fp->rq_list)) |
| 2618 |
seq_puts(s, " No requests active\n"); |
| 2619 |
read_unlock(&fp->rq_list_lock); |
| 2620 |
} |
| 2621 |
} |
| 2622 |
|
| 2623 |
static int sg_proc_seq_show_debug(struct seq_file *s, void *v) |
| 2624 |
{ |
| 2625 |
struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; |
| 2626 |
Sg_device *sdp; |
| 2627 |
unsigned long iflags; |
| 2628 |
|
| 2629 |
if (it && (0 == it->index)) |
| 2630 |
seq_printf(s, "max_active_device=%d def_reserved_size=%d\n", |
| 2631 |
(int)it->max, sg_big_buff); |
| 2632 |
|
| 2633 |
read_lock_irqsave(&sg_index_lock, iflags); |
| 2634 |
sdp = it ? sg_lookup_dev(it->index) : NULL; |
| 2635 |
if (NULL == sdp) |
| 2636 |
goto skip; |
| 2637 |
read_lock(&sdp->sfd_lock); |
| 2638 |
if (!list_empty(&sdp->sfds)) { |
| 2639 |
seq_printf(s, " >>> device=%s ", sdp->name); |
| 2640 |
if (atomic_read(&sdp->detaching)) |
| 2641 |
seq_puts(s, "detaching pending close "); |
| 2642 |
else if (sdp->device) { |
| 2643 |
struct scsi_device *scsidp = sdp->device; |
| 2644 |
|
| 2645 |
seq_printf(s, "%d:%d:%d:%llu em=%d", |
| 2646 |
scsidp->host->host_no, |
| 2647 |
scsidp->channel, scsidp->id, |
| 2648 |
scsidp->lun, |
| 2649 |
scsidp->host->hostt->emulated); |
| 2650 |
} |
| 2651 |
seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n", |
| 2652 |
sdp->sg_tablesize, sdp->exclude, sdp->open_cnt); |
| 2653 |
sg_proc_debug_helper(s, sdp); |
| 2654 |
} |
| 2655 |
read_unlock(&sdp->sfd_lock); |
| 2656 |
skip: |
| 2657 |
read_unlock_irqrestore(&sg_index_lock, iflags); |
| 2658 |
return 0; |
| 2659 |
} |
| 2660 |
|
| 2661 |
#endif /* CONFIG_SCSI_PROC_FS */ |
| 2662 |
|
| 2663 |
module_init(init_sg); |
| 2664 |
module_exit(exit_sg); |