| 1 | |
| 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;
|
| 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>
|
| 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 | |
| 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 | |
| 85 | * /proc/scsi/sg/def_reserved_size . Each time sg_open() is called a buffer
|
| 86 | * of this size (or less if there is not enough memory) will be reserved
|
| 87 | * for use by this file descriptor.
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 | static int def_reserved_size = SG_DEF_RESERVED_SIZE;
|
| 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); |
| 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 {
|
| 112 | unsigned short k_use_sg;
|
| 113 | unsigned sglist_len;
|
| 114 | unsigned bufflen;
|
| 115 | struct page **pages;
|
| 116 | int page_order;
|
| 117 | char dio_in_use;
|
| 118 | unsigned char cmd_opcode;
|
| 119 | } Sg_scatter_hold;
|
| 120 |
|
| 121 | struct sg_device;
|
| 122 | struct sg_fd;
|
| 123 |
|
| 124 | typedef struct sg_request {
|
| 125 | struct list_head entry;
|
| 126 | struct sg_fd *parentfp;
|
| 127 | Sg_scatter_hold data;
|
| 128 | sg_io_hdr_t header;
|
| 129 | unsigned char sense_b[SCSI_SENSE_BUFFERSIZE];
|
| 130 | char res_used;
|
| 131 | char orphan;
|
| 132 | char sg_io_owned;
|
| 133 |
|
| 134 | char done;
|
| 135 | struct request *rq;
|
| 136 | struct bio *bio;
|
| 137 | struct execute_work ew;
|
| 138 | } Sg_request;
|
| 139 |
|
| 140 | typedef struct sg_fd {
|
| 141 | struct list_head sfd_siblings;
|
| 142 | struct sg_device *parentdp;
|
| 143 | wait_queue_head_t read_wait;
|
| 144 | rwlock_t rq_list_lock;
|
| 145 | struct mutex f_mutex;
|
| 146 | int timeout;
|
| 147 | int timeout_user;
|
| 148 | Sg_scatter_hold reserve;
|
| 149 | struct list_head rq_list;
|
| 150 | struct fasync_struct *async_qp;
|
| 151 | Sg_request req_arr[SG_MAX_QUEUE];
|
| 152 | char force_packid;
|
| 153 | char cmd_q;
|
| 154 | unsigned char next_cmd_len;
|
| 155 | char keep_orphan;
|
| 156 | char mmap_called;
|
| 157 | char res_in_use;
|
| 158 | struct kref f_ref;
|
| 159 | struct execute_work ew;
|
| 160 | } Sg_fd;
|
| 161 |
|
| 162 | typedef struct sg_device {
|
| 163 | struct scsi_device *device;
|
| 164 | wait_queue_head_t open_wait;
|
| 165 | struct mutex open_rel_lock;
|
| 166 | int sg_tablesize;
|
| 167 | u32 index;
|
| 168 | struct list_head sfds;
|
| 169 | rwlock_t sfd_lock;
|
| 170 | atomic_t detaching;
|
| 171 | bool exclude;
|
| 172 | int open_cnt;
|
| 173 | char sgdebug;
|
| 174 | char name[DISK_NAME_LEN];
|
| 175 | struct cdev * cdev;
|
| 176 | struct kref d_ref;
|
| 177 | } Sg_device;
|
| 178 |
|
| 179 |
|
| 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)
|
| 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)
|
| 273 | return retval;
|
| 274 | if (atomic_read(&sdp->detaching))
|
| 275 | return -ENODEV;
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | return retval;
|
| 280 | }
|
| 281 |
|
| 282 |
|
| 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;
|
| 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 |
|
| 305 |
|
| 306 | device = sdp->device;
|
| 307 | retval = scsi_device_get(device);
|
| 308 | if (retval)
|
| 309 | goto sg_put;
|
| 310 |
|
| 311 | |
| 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 |
|
| 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)
|
| 337 | goto error_mutex_locked;
|
| 338 | }
|
| 339 |
|
| 340 |
|
| 341 | if (flags & O_EXCL)
|
| 342 | sdp->exclude = true;
|
| 343 |
|
| 344 | if (sdp->open_cnt < 1) {
|
| 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;
|
| 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 | |
| 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 | |
| 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 |
|
| 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 |
|
| 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) {
|
| 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 |
|
| 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;
|
| 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 | |
| 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 |
|
| 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];
|
| 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;
|
| 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;
|
| 647 | } else {
|
| 648 | cmd_size = COMMAND_SIZE(opcode);
|
| 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 |
|
| 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;
|
| 663 | }
|
| 664 | hp = &srp->header;
|
| 665 | hp->interface_id = '\0';
|
| 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;
|
| 682 | hp->flags = input_size;
|
| 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;
|
| 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;
|
| 745 | }
|
| 746 | if (hp->flags & SG_FLAG_DIRECT_IO) {
|
| 747 | sg_remove_request(sfp, srp);
|
| 748 | return -EINVAL;
|
| 749 | }
|
| 750 | if (sfp->res_in_use) {
|
| 751 | sg_remove_request(sfp, srp);
|
| 752 | return -EBUSY;
|
| 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];
|
| 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;
|
| 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' &&
|
| 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);
|
| 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 = srp->header.masked_status ||
|
| 867 | srp->header.host_status ||
|
| 868 | srp->header.driver_status;
|
| 869 | if (srp->done)
|
| 870 | rinfo[val].duration =
|
| 871 | srp->header.duration;
|
| 872 | else {
|
| 873 | ms = jiffies_to_msecs(jiffies);
|
| 874 | rinfo[val].duration =
|
| 875 | (ms > srp->header.duration) ?
|
| 876 | (ms - srp->header.duration) : 0;
|
| 877 | }
|
| 878 | rinfo[val].orphan = srp->orphan;
|
| 879 | rinfo[val].sg_io_owned = srp->sg_io_owned;
|
| 880 | rinfo[val].pack_id = srp->header.pack_id;
|
| 881 | rinfo[val].usr_ptr = srp->header.usr_ptr;
|
| 882 | val++;
|
| 883 | }
|
| 884 | }
|
| 885 |
|
| 886 | #ifdef CONFIG_COMPAT
|
| 887 | struct compat_sg_req_info {
|
| 888 | char req_state;
|
| 889 | char orphan;
|
| 890 | char sg_io_owned;
|
| 891 | char problem;
|
| 892 | int pack_id;
|
| 893 | compat_uptr_t usr_ptr;
|
| 894 | unsigned int duration;
|
| 895 | int unused;
|
| 896 | };
|
| 897 |
|
| 898 | static int put_compat_request_table(struct compat_sg_req_info __user *o,
|
| 899 | struct sg_req_info *rinfo)
|
| 900 | {
|
| 901 | int i;
|
| 902 | for (i = 0; i < SG_MAX_QUEUE; i++) {
|
| 903 | if (copy_to_user(o + i, rinfo + i, offsetof(sg_req_info_t, usr_ptr)) ||
|
| 904 | put_user((uintptr_t)rinfo[i].usr_ptr, &o[i].usr_ptr) ||
|
| 905 | put_user(rinfo[i].duration, &o[i].duration) ||
|
| 906 | put_user(rinfo[i].unused, &o[i].unused))
|
| 907 | return -EFAULT;
|
| 908 | }
|
| 909 | return 0;
|
| 910 | }
|
| 911 | #endif
|
| 912 |
|
| 913 | static long
|
| 914 | sg_ioctl_common(struct file *filp, Sg_device *sdp, Sg_fd *sfp,
|
| 915 | unsigned int cmd_in, void __user *p)
|
| 916 | {
|
| 917 | int __user *ip = p;
|
| 918 | int result, val, read_only;
|
| 919 | Sg_request *srp;
|
| 920 | unsigned long iflags;
|
| 921 |
|
| 922 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 923 | "sg_ioctl: cmd=0x%x\n", (int) cmd_in));
|
| 924 | read_only = (O_RDWR != (filp->f_flags & O_ACCMODE));
|
| 925 |
|
| 926 | switch (cmd_in) {
|
| 927 | case SG_IO:
|
| 928 | if (atomic_read(&sdp->detaching))
|
| 929 | return -ENODEV;
|
| 930 | if (!scsi_block_when_processing_errors(sdp->device))
|
| 931 | return -ENXIO;
|
| 932 | result = sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
|
| 933 | 1, read_only, 1, &srp);
|
| 934 | if (result < 0)
|
| 935 | return result;
|
| 936 | result = wait_event_interruptible(sfp->read_wait,
|
| 937 | srp_done(sfp, srp));
|
| 938 | write_lock_irq(&sfp->rq_list_lock);
|
| 939 | if (srp->done) {
|
| 940 | srp->done = 2;
|
| 941 | write_unlock_irq(&sfp->rq_list_lock);
|
| 942 | result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
|
| 943 | return (result < 0) ? result : 0;
|
| 944 | }
|
| 945 | srp->orphan = 1;
|
| 946 | write_unlock_irq(&sfp->rq_list_lock);
|
| 947 | return result;
|
| 948 | case SG_SET_TIMEOUT:
|
| 949 | result = get_user(val, ip);
|
| 950 | if (result)
|
| 951 | return result;
|
| 952 | if (val < 0)
|
| 953 | return -EIO;
|
| 954 | if (val >= mult_frac((s64)INT_MAX, USER_HZ, HZ))
|
| 955 | val = min_t(s64, mult_frac((s64)INT_MAX, USER_HZ, HZ),
|
| 956 | INT_MAX);
|
| 957 | sfp->timeout_user = val;
|
| 958 | sfp->timeout = mult_frac(val, HZ, USER_HZ);
|
| 959 |
|
| 960 | return 0;
|
| 961 | case SG_GET_TIMEOUT:
|
| 962 |
|
| 963 | return sfp->timeout_user;
|
| 964 | case SG_SET_FORCE_LOW_DMA:
|
| 965 | |
| 966 | * N.B. This ioctl never worked properly, but failed to
|
| 967 | * return an error value. So returning '0' to keep compability
|
| 968 | * with legacy applications.
|
| 969 |
|
| 970 | return 0;
|
| 971 | case SG_GET_LOW_DMA:
|
| 972 | return put_user(0, ip);
|
| 973 | case SG_GET_SCSI_ID:
|
| 974 | {
|
| 975 | sg_scsi_id_t v;
|
| 976 |
|
| 977 | if (atomic_read(&sdp->detaching))
|
| 978 | return -ENODEV;
|
| 979 | memset(&v, 0, sizeof(v));
|
| 980 | v.host_no = sdp->device->host->host_no;
|
| 981 | v.channel = sdp->device->channel;
|
| 982 | v.scsi_id = sdp->device->id;
|
| 983 | v.lun = sdp->device->lun;
|
| 984 | v.scsi_type = sdp->device->type;
|
| 985 | v.h_cmd_per_lun = sdp->device->host->cmd_per_lun;
|
| 986 | v.d_queue_depth = sdp->device->queue_depth;
|
| 987 | if (copy_to_user(p, &v, sizeof(sg_scsi_id_t)))
|
| 988 | return -EFAULT;
|
| 989 | return 0;
|
| 990 | }
|
| 991 | case SG_SET_FORCE_PACK_ID:
|
| 992 | result = get_user(val, ip);
|
| 993 | if (result)
|
| 994 | return result;
|
| 995 | sfp->force_packid = val ? 1 : 0;
|
| 996 | return 0;
|
| 997 | case SG_GET_PACK_ID:
|
| 998 | read_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 999 | list_for_each_entry(srp, &sfp->rq_list, entry) {
|
| 1000 | if ((1 == srp->done) && (!srp->sg_io_owned)) {
|
| 1001 | read_unlock_irqrestore(&sfp->rq_list_lock,
|
| 1002 | iflags);
|
| 1003 | return put_user(srp->header.pack_id, ip);
|
| 1004 | }
|
| 1005 | }
|
| 1006 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 1007 | return put_user(-1, ip);
|
| 1008 | case SG_GET_NUM_WAITING:
|
| 1009 | read_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 1010 | val = 0;
|
| 1011 | list_for_each_entry(srp, &sfp->rq_list, entry) {
|
| 1012 | if ((1 == srp->done) && (!srp->sg_io_owned))
|
| 1013 | ++val;
|
| 1014 | }
|
| 1015 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 1016 | return put_user(val, ip);
|
| 1017 | case SG_GET_SG_TABLESIZE:
|
| 1018 | return put_user(sdp->sg_tablesize, ip);
|
| 1019 | case SG_SET_RESERVED_SIZE:
|
| 1020 | result = get_user(val, ip);
|
| 1021 | if (result)
|
| 1022 | return result;
|
| 1023 | if (val < 0)
|
| 1024 | return -EINVAL;
|
| 1025 | val = min_t(int, val,
|
| 1026 | max_sectors_bytes(sdp->device->request_queue));
|
| 1027 | mutex_lock(&sfp->f_mutex);
|
| 1028 | if (val != sfp->reserve.bufflen) {
|
| 1029 | if (sfp->mmap_called ||
|
| 1030 | sfp->res_in_use) {
|
| 1031 | mutex_unlock(&sfp->f_mutex);
|
| 1032 | return -EBUSY;
|
| 1033 | }
|
| 1034 |
|
| 1035 | sg_remove_scat(sfp, &sfp->reserve);
|
| 1036 | sg_build_reserve(sfp, val);
|
| 1037 | }
|
| 1038 | mutex_unlock(&sfp->f_mutex);
|
| 1039 | return 0;
|
| 1040 | case SG_GET_RESERVED_SIZE:
|
| 1041 | val = min_t(int, sfp->reserve.bufflen,
|
| 1042 | max_sectors_bytes(sdp->device->request_queue));
|
| 1043 | return put_user(val, ip);
|
| 1044 | case SG_SET_COMMAND_Q:
|
| 1045 | result = get_user(val, ip);
|
| 1046 | if (result)
|
| 1047 | return result;
|
| 1048 | sfp->cmd_q = val ? 1 : 0;
|
| 1049 | return 0;
|
| 1050 | case SG_GET_COMMAND_Q:
|
| 1051 | return put_user((int) sfp->cmd_q, ip);
|
| 1052 | case SG_SET_KEEP_ORPHAN:
|
| 1053 | result = get_user(val, ip);
|
| 1054 | if (result)
|
| 1055 | return result;
|
| 1056 | sfp->keep_orphan = val;
|
| 1057 | return 0;
|
| 1058 | case SG_GET_KEEP_ORPHAN:
|
| 1059 | return put_user((int) sfp->keep_orphan, ip);
|
| 1060 | case SG_NEXT_CMD_LEN:
|
| 1061 | result = get_user(val, ip);
|
| 1062 | if (result)
|
| 1063 | return result;
|
| 1064 | if (val > SG_MAX_CDB_SIZE)
|
| 1065 | return -ENOMEM;
|
| 1066 | sfp->next_cmd_len = (val > 0) ? val : 0;
|
| 1067 | return 0;
|
| 1068 | case SG_GET_VERSION_NUM:
|
| 1069 | return put_user(sg_version_num, ip);
|
| 1070 | case SG_GET_ACCESS_COUNT:
|
| 1071 |
|
| 1072 | val = (sdp->device ? 1 : 0);
|
| 1073 | return put_user(val, ip);
|
| 1074 | case SG_GET_REQUEST_TABLE:
|
| 1075 | {
|
| 1076 | sg_req_info_t *rinfo;
|
| 1077 |
|
| 1078 | rinfo = kcalloc(SG_MAX_QUEUE, SZ_SG_REQ_INFO,
|
| 1079 | GFP_KERNEL);
|
| 1080 | if (!rinfo)
|
| 1081 | return -ENOMEM;
|
| 1082 | read_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 1083 | sg_fill_request_table(sfp, rinfo);
|
| 1084 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 1085 | #ifdef CONFIG_COMPAT
|
| 1086 | if (in_compat_syscall())
|
| 1087 | result = put_compat_request_table(p, rinfo);
|
| 1088 | else
|
| 1089 | #endif
|
| 1090 | result = copy_to_user(p, rinfo,
|
| 1091 | SZ_SG_REQ_INFO * SG_MAX_QUEUE);
|
| 1092 | result = result ? -EFAULT : 0;
|
| 1093 | kfree(rinfo);
|
| 1094 | return result;
|
| 1095 | }
|
| 1096 | case SG_EMULATED_HOST:
|
| 1097 | if (atomic_read(&sdp->detaching))
|
| 1098 | return -ENODEV;
|
| 1099 | return put_user(sdp->device->host->hostt->emulated, ip);
|
| 1100 | case SCSI_IOCTL_SEND_COMMAND:
|
| 1101 | if (atomic_read(&sdp->detaching))
|
| 1102 | return -ENODEV;
|
| 1103 | return scsi_ioctl(sdp->device, filp->f_mode & FMODE_WRITE,
|
| 1104 | cmd_in, p);
|
| 1105 | case SG_SET_DEBUG:
|
| 1106 | result = get_user(val, ip);
|
| 1107 | if (result)
|
| 1108 | return result;
|
| 1109 | sdp->sgdebug = (char) val;
|
| 1110 | return 0;
|
| 1111 | case BLKSECTGET:
|
| 1112 | return put_user(max_sectors_bytes(sdp->device->request_queue),
|
| 1113 | ip);
|
| 1114 | case BLKTRACESETUP:
|
| 1115 | return blk_trace_setup(sdp->device->request_queue, sdp->name,
|
| 1116 | MKDEV(SCSI_GENERIC_MAJOR, sdp->index),
|
| 1117 | NULL, p);
|
| 1118 | case BLKTRACESTART:
|
| 1119 | return blk_trace_startstop(sdp->device->request_queue, 1);
|
| 1120 | case BLKTRACESTOP:
|
| 1121 | return blk_trace_startstop(sdp->device->request_queue, 0);
|
| 1122 | case BLKTRACETEARDOWN:
|
| 1123 | return blk_trace_remove(sdp->device->request_queue);
|
| 1124 | case SCSI_IOCTL_GET_IDLUN:
|
| 1125 | case SCSI_IOCTL_GET_BUS_NUMBER:
|
| 1126 | case SCSI_IOCTL_PROBE_HOST:
|
| 1127 | case SG_GET_TRANSFORM:
|
| 1128 | case SG_SCSI_RESET:
|
| 1129 | if (atomic_read(&sdp->detaching))
|
| 1130 | return -ENODEV;
|
| 1131 | break;
|
| 1132 | default:
|
| 1133 | if (read_only)
|
| 1134 | return -EPERM;
|
| 1135 | break;
|
| 1136 | }
|
| 1137 |
|
| 1138 | result = scsi_ioctl_block_when_processing_errors(sdp->device,
|
| 1139 | cmd_in, filp->f_flags & O_NDELAY);
|
| 1140 | if (result)
|
| 1141 | return result;
|
| 1142 |
|
| 1143 | return -ENOIOCTLCMD;
|
| 1144 | }
|
| 1145 |
|
| 1146 | static long
|
| 1147 | sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
|
| 1148 | {
|
| 1149 | void __user *p = (void __user *)arg;
|
| 1150 | Sg_device *sdp;
|
| 1151 | Sg_fd *sfp;
|
| 1152 | int ret;
|
| 1153 |
|
| 1154 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
|
| 1155 | return -ENXIO;
|
| 1156 |
|
| 1157 | ret = sg_ioctl_common(filp, sdp, sfp, cmd_in, p);
|
| 1158 | if (ret != -ENOIOCTLCMD)
|
| 1159 | return ret;
|
| 1160 | return scsi_ioctl(sdp->device, filp->f_mode & FMODE_WRITE, cmd_in, p);
|
| 1161 | }
|
| 1162 |
|
| 1163 | static __poll_t
|
| 1164 | sg_poll(struct file *filp, poll_table * wait)
|
| 1165 | {
|
| 1166 | __poll_t res = 0;
|
| 1167 | Sg_device *sdp;
|
| 1168 | Sg_fd *sfp;
|
| 1169 | Sg_request *srp;
|
| 1170 | int count = 0;
|
| 1171 | unsigned long iflags;
|
| 1172 |
|
| 1173 | sfp = filp->private_data;
|
| 1174 | if (!sfp)
|
| 1175 | return EPOLLERR;
|
| 1176 | sdp = sfp->parentdp;
|
| 1177 | if (!sdp)
|
| 1178 | return EPOLLERR;
|
| 1179 | poll_wait(filp, &sfp->read_wait, wait);
|
| 1180 | read_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 1181 | list_for_each_entry(srp, &sfp->rq_list, entry) {
|
| 1182 |
|
| 1183 | if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
|
| 1184 | res = EPOLLIN | EPOLLRDNORM;
|
| 1185 | ++count;
|
| 1186 | }
|
| 1187 | read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 1188 |
|
| 1189 | if (atomic_read(&sdp->detaching))
|
| 1190 | res |= EPOLLHUP;
|
| 1191 | else if (!sfp->cmd_q) {
|
| 1192 | if (0 == count)
|
| 1193 | res |= EPOLLOUT | EPOLLWRNORM;
|
| 1194 | } else if (count < SG_MAX_QUEUE)
|
| 1195 | res |= EPOLLOUT | EPOLLWRNORM;
|
| 1196 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 1197 | "sg_poll: res=0x%x\n", (__force u32) res));
|
| 1198 | return res;
|
| 1199 | }
|
| 1200 |
|
| 1201 | static int
|
| 1202 | sg_fasync(int fd, struct file *filp, int mode)
|
| 1203 | {
|
| 1204 | Sg_device *sdp;
|
| 1205 | Sg_fd *sfp;
|
| 1206 |
|
| 1207 | if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
|
| 1208 | return -ENXIO;
|
| 1209 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 1210 | "sg_fasync: mode=%d\n", mode));
|
| 1211 |
|
| 1212 | return fasync_helper(fd, filp, mode, &sfp->async_qp);
|
| 1213 | }
|
| 1214 |
|
| 1215 | static vm_fault_t
|
| 1216 | sg_vma_fault(struct vm_fault *vmf)
|
| 1217 | {
|
| 1218 | struct vm_area_struct *vma = vmf->vma;
|
| 1219 | Sg_fd *sfp;
|
| 1220 | unsigned long offset, len, sa;
|
| 1221 | Sg_scatter_hold *rsv_schp;
|
| 1222 | int k, length;
|
| 1223 |
|
| 1224 | if ((NULL == vma) || (!(sfp = (Sg_fd *) vma->vm_private_data)))
|
| 1225 | return VM_FAULT_SIGBUS;
|
| 1226 | rsv_schp = &sfp->reserve;
|
| 1227 | offset = vmf->pgoff << PAGE_SHIFT;
|
| 1228 | if (offset >= rsv_schp->bufflen)
|
| 1229 | return VM_FAULT_SIGBUS;
|
| 1230 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1231 | "sg_vma_fault: offset=%lu, scatg=%d\n",
|
| 1232 | offset, rsv_schp->k_use_sg));
|
| 1233 | sa = vma->vm_start;
|
| 1234 | length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
|
| 1235 | for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
|
| 1236 | len = vma->vm_end - sa;
|
| 1237 | len = (len < length) ? len : length;
|
| 1238 | if (offset < len) {
|
| 1239 | struct page *page = rsv_schp->pages[k] + (offset >> PAGE_SHIFT);
|
| 1240 | get_page(page);
|
| 1241 | vmf->page = page;
|
| 1242 | return 0;
|
| 1243 | }
|
| 1244 | sa += len;
|
| 1245 | offset -= len;
|
| 1246 | }
|
| 1247 |
|
| 1248 | return VM_FAULT_SIGBUS;
|
| 1249 | }
|
| 1250 |
|
| 1251 | static const struct vm_operations_struct sg_mmap_vm_ops = {
|
| 1252 | .fault = sg_vma_fault,
|
| 1253 | };
|
| 1254 |
|
| 1255 | static int
|
| 1256 | sg_mmap(struct file *filp, struct vm_area_struct *vma)
|
| 1257 | {
|
| 1258 | Sg_fd *sfp;
|
| 1259 | unsigned long req_sz, len, sa;
|
| 1260 | Sg_scatter_hold *rsv_schp;
|
| 1261 | int k, length;
|
| 1262 | int ret = 0;
|
| 1263 |
|
| 1264 | if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
|
| 1265 | return -ENXIO;
|
| 1266 | req_sz = vma->vm_end - vma->vm_start;
|
| 1267 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1268 | "sg_mmap starting, vm_start=%p, len=%d\n",
|
| 1269 | (void *) vma->vm_start, (int) req_sz));
|
| 1270 | if (vma->vm_pgoff)
|
| 1271 | return -EINVAL;
|
| 1272 | rsv_schp = &sfp->reserve;
|
| 1273 | mutex_lock(&sfp->f_mutex);
|
| 1274 | if (req_sz > rsv_schp->bufflen) {
|
| 1275 | ret = -ENOMEM;
|
| 1276 | goto out;
|
| 1277 | }
|
| 1278 |
|
| 1279 | sa = vma->vm_start;
|
| 1280 | length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
|
| 1281 | for (k = 0; k < rsv_schp->k_use_sg && sa < vma->vm_end; k++) {
|
| 1282 | len = vma->vm_end - sa;
|
| 1283 | len = (len < length) ? len : length;
|
| 1284 | sa += len;
|
| 1285 | }
|
| 1286 |
|
| 1287 | sfp->mmap_called = 1;
|
| 1288 | vm_flags_set(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
|
| 1289 | vma->vm_private_data = sfp;
|
| 1290 | vma->vm_ops = &sg_mmap_vm_ops;
|
| 1291 | out:
|
| 1292 | mutex_unlock(&sfp->f_mutex);
|
| 1293 | return ret;
|
| 1294 | }
|
| 1295 |
|
| 1296 | static void
|
| 1297 | sg_rq_end_io_usercontext(struct work_struct *work)
|
| 1298 | {
|
| 1299 | struct sg_request *srp = container_of(work, struct sg_request, ew.work);
|
| 1300 | struct sg_fd *sfp = srp->parentfp;
|
| 1301 |
|
| 1302 | sg_finish_rem_req(srp);
|
| 1303 | sg_remove_request(sfp, srp);
|
| 1304 | kref_put(&sfp->f_ref, sg_remove_sfp);
|
| 1305 | }
|
| 1306 |
|
| 1307 | |
| 1308 | * This function is a "bottom half" handler that is called by the mid
|
| 1309 | * level when a command is completed (or has failed).
|
| 1310 |
|
| 1311 | static enum rq_end_io_ret
|
| 1312 | sg_rq_end_io(struct request *rq, blk_status_t status,
|
| 1313 | const struct io_comp_batch *iob)
|
| 1314 | {
|
| 1315 | struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
|
| 1316 | struct sg_request *srp = rq->end_io_data;
|
| 1317 | Sg_device *sdp;
|
| 1318 | Sg_fd *sfp;
|
| 1319 | unsigned long iflags;
|
| 1320 | unsigned int ms;
|
| 1321 | char *sense;
|
| 1322 | int result, resid, done = 1;
|
| 1323 |
|
| 1324 | if (WARN_ON(srp->done != 0))
|
| 1325 | return RQ_END_IO_NONE;
|
| 1326 |
|
| 1327 | sfp = srp->parentfp;
|
| 1328 | if (WARN_ON(sfp == NULL))
|
| 1329 | return RQ_END_IO_NONE;
|
| 1330 |
|
| 1331 | sdp = sfp->parentdp;
|
| 1332 | if (unlikely(atomic_read(&sdp->detaching)))
|
| 1333 | pr_info("%s: device detaching\n", __func__);
|
| 1334 |
|
| 1335 | sense = scmd->sense_buffer;
|
| 1336 | result = scmd->result;
|
| 1337 | resid = scmd->resid_len;
|
| 1338 |
|
| 1339 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sdp,
|
| 1340 | "sg_cmd_done: pack_id=%d, res=0x%x\n",
|
| 1341 | srp->header.pack_id, result));
|
| 1342 | srp->header.resid = resid;
|
| 1343 | if (0 != result) {
|
| 1344 | struct scsi_sense_hdr sshdr;
|
| 1345 |
|
| 1346 | srp->header.status = 0xff & result;
|
| 1347 | srp->header.masked_status = sg_status_byte(result);
|
| 1348 | srp->header.msg_status = COMMAND_COMPLETE;
|
| 1349 | srp->header.host_status = host_byte(result);
|
| 1350 | srp->header.driver_status = driver_byte(result);
|
| 1351 | if ((sdp->sgdebug > 0) &&
|
| 1352 | ((CHECK_CONDITION == srp->header.masked_status) ||
|
| 1353 | (COMMAND_TERMINATED == srp->header.masked_status)))
|
| 1354 | __scsi_print_sense(sdp->device, __func__, sense,
|
| 1355 | SCSI_SENSE_BUFFERSIZE);
|
| 1356 |
|
| 1357 |
|
| 1358 | if (driver_byte(result) != 0
|
| 1359 | && scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)
|
| 1360 | && !scsi_sense_is_deferred(&sshdr)
|
| 1361 | && sshdr.sense_key == UNIT_ATTENTION
|
| 1362 | && sdp->device->removable) {
|
| 1363 |
|
| 1364 |
|
| 1365 | sdp->device->changed = 1;
|
| 1366 | }
|
| 1367 | }
|
| 1368 |
|
| 1369 | if (scmd->sense_len)
|
| 1370 | memcpy(srp->sense_b, scmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 | |
| 1375 | * Free the request as soon as it is complete so that its resources
|
| 1376 | * can be reused without waiting for userspace to read() the
|
| 1377 | * result. But keep the associated bio (if any) around until
|
| 1378 | * blk_rq_unmap_user() can be called from user context.
|
| 1379 |
|
| 1380 | srp->rq = NULL;
|
| 1381 | blk_mq_free_request(rq);
|
| 1382 |
|
| 1383 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 1384 | if (unlikely(srp->orphan)) {
|
| 1385 | if (sfp->keep_orphan)
|
| 1386 | srp->sg_io_owned = 0;
|
| 1387 | else
|
| 1388 | done = 0;
|
| 1389 | }
|
| 1390 | srp->done = done;
|
| 1391 | ms = jiffies_to_msecs(jiffies);
|
| 1392 | srp->header.duration = (ms > srp->header.duration) ?
|
| 1393 | (ms - srp->header.duration) : 0;
|
| 1394 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 1395 |
|
| 1396 | if (likely(done)) {
|
| 1397 | |
| 1398 | * packet.
|
| 1399 |
|
| 1400 | wake_up_interruptible(&sfp->read_wait);
|
| 1401 | kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
|
| 1402 | kref_put(&sfp->f_ref, sg_remove_sfp);
|
| 1403 | } else {
|
| 1404 | INIT_WORK(&srp->ew.work, sg_rq_end_io_usercontext);
|
| 1405 | schedule_work(&srp->ew.work);
|
| 1406 | }
|
| 1407 | return RQ_END_IO_NONE;
|
| 1408 | }
|
| 1409 |
|
| 1410 | static const struct file_operations sg_fops = {
|
| 1411 | .owner = THIS_MODULE,
|
| 1412 | .read = sg_read,
|
| 1413 | .write = sg_write,
|
| 1414 | .poll = sg_poll,
|
| 1415 | .unlocked_ioctl = sg_ioctl,
|
| 1416 | .compat_ioctl = compat_ptr_ioctl,
|
| 1417 | .open = sg_open,
|
| 1418 | .mmap = sg_mmap,
|
| 1419 | .release = sg_release,
|
| 1420 | .fasync = sg_fasync,
|
| 1421 | };
|
| 1422 |
|
| 1423 | static const struct class sg_sysfs_class = {
|
| 1424 | .name = "scsi_generic"
|
| 1425 | };
|
| 1426 |
|
| 1427 | static int sg_sysfs_valid = 0;
|
| 1428 |
|
| 1429 | static Sg_device *
|
| 1430 | sg_alloc(struct scsi_device *scsidp)
|
| 1431 | {
|
| 1432 | struct request_queue *q = scsidp->request_queue;
|
| 1433 | Sg_device *sdp;
|
| 1434 | unsigned long iflags;
|
| 1435 | int error;
|
| 1436 | u32 k;
|
| 1437 |
|
| 1438 | sdp = kzalloc_obj(Sg_device);
|
| 1439 | if (!sdp) {
|
| 1440 | sdev_printk(KERN_WARNING, scsidp, "%s: kmalloc Sg_device "
|
| 1441 | "failure\n", __func__);
|
| 1442 | return ERR_PTR(-ENOMEM);
|
| 1443 | }
|
| 1444 |
|
| 1445 | idr_preload(GFP_KERNEL);
|
| 1446 | write_lock_irqsave(&sg_index_lock, iflags);
|
| 1447 |
|
| 1448 | error = idr_alloc(&sg_index_idr, sdp, 0, SG_MAX_DEVS, GFP_NOWAIT);
|
| 1449 | if (error < 0) {
|
| 1450 | if (error == -ENOSPC) {
|
| 1451 | sdev_printk(KERN_WARNING, scsidp,
|
| 1452 | "Unable to attach sg device type=%d, minor number exceeds %d\n",
|
| 1453 | scsidp->type, SG_MAX_DEVS - 1);
|
| 1454 | error = -ENODEV;
|
| 1455 | } else {
|
| 1456 | sdev_printk(KERN_WARNING, scsidp, "%s: idr "
|
| 1457 | "allocation Sg_device failure: %d\n",
|
| 1458 | __func__, error);
|
| 1459 | }
|
| 1460 | goto out_unlock;
|
| 1461 | }
|
| 1462 | k = error;
|
| 1463 |
|
| 1464 | SCSI_LOG_TIMEOUT(3, sdev_printk(KERN_INFO, scsidp,
|
| 1465 | "sg_alloc: dev=%d \n", k));
|
| 1466 | sprintf(sdp->name, "sg%d", k);
|
| 1467 | sdp->device = scsidp;
|
| 1468 | mutex_init(&sdp->open_rel_lock);
|
| 1469 | INIT_LIST_HEAD(&sdp->sfds);
|
| 1470 | init_waitqueue_head(&sdp->open_wait);
|
| 1471 | atomic_set(&sdp->detaching, 0);
|
| 1472 | rwlock_init(&sdp->sfd_lock);
|
| 1473 | sdp->sg_tablesize = queue_max_segments(q);
|
| 1474 | sdp->index = k;
|
| 1475 | kref_init(&sdp->d_ref);
|
| 1476 | error = 0;
|
| 1477 |
|
| 1478 | out_unlock:
|
| 1479 | write_unlock_irqrestore(&sg_index_lock, iflags);
|
| 1480 | idr_preload_end();
|
| 1481 |
|
| 1482 | if (error) {
|
| 1483 | kfree(sdp);
|
| 1484 | return ERR_PTR(error);
|
| 1485 | }
|
| 1486 | return sdp;
|
| 1487 | }
|
| 1488 |
|
| 1489 | static int
|
| 1490 | sg_add_device(struct device *cl_dev)
|
| 1491 | {
|
| 1492 | struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
|
| 1493 | Sg_device *sdp = NULL;
|
| 1494 | struct cdev * cdev = NULL;
|
| 1495 | int error;
|
| 1496 | unsigned long iflags;
|
| 1497 |
|
| 1498 | if (!blk_get_queue(scsidp->request_queue)) {
|
| 1499 | pr_warn("%s: get scsi_device queue failed\n", __func__);
|
| 1500 | return -ENODEV;
|
| 1501 | }
|
| 1502 |
|
| 1503 | error = -ENOMEM;
|
| 1504 | cdev = cdev_alloc();
|
| 1505 | if (!cdev) {
|
| 1506 | pr_warn("%s: cdev_alloc failed\n", __func__);
|
| 1507 | goto out;
|
| 1508 | }
|
| 1509 | cdev->owner = THIS_MODULE;
|
| 1510 | cdev->ops = &sg_fops;
|
| 1511 |
|
| 1512 | sdp = sg_alloc(scsidp);
|
| 1513 | if (IS_ERR(sdp)) {
|
| 1514 | pr_warn("%s: sg_alloc failed\n", __func__);
|
| 1515 | error = PTR_ERR(sdp);
|
| 1516 | goto out;
|
| 1517 | }
|
| 1518 |
|
| 1519 | error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
|
| 1520 | if (error)
|
| 1521 | goto cdev_add_err;
|
| 1522 |
|
| 1523 | sdp->cdev = cdev;
|
| 1524 | if (sg_sysfs_valid) {
|
| 1525 | struct device *sg_class_member;
|
| 1526 |
|
| 1527 | sg_class_member = device_create(&sg_sysfs_class, cl_dev->parent,
|
| 1528 | MKDEV(SCSI_GENERIC_MAJOR,
|
| 1529 | sdp->index),
|
| 1530 | sdp, "%s", sdp->name);
|
| 1531 | if (IS_ERR(sg_class_member)) {
|
| 1532 | pr_err("%s: device_create failed\n", __func__);
|
| 1533 | error = PTR_ERR(sg_class_member);
|
| 1534 | goto cdev_add_err;
|
| 1535 | }
|
| 1536 | error = sysfs_create_link(&scsidp->sdev_gendev.kobj,
|
| 1537 | &sg_class_member->kobj, "generic");
|
| 1538 | if (error)
|
| 1539 | pr_err("%s: unable to make symlink 'generic' back "
|
| 1540 | "to sg%d\n", __func__, sdp->index);
|
| 1541 | } else
|
| 1542 | pr_warn("%s: sg_sys Invalid\n", __func__);
|
| 1543 |
|
| 1544 | sdev_printk(KERN_NOTICE, scsidp, "Attached scsi generic sg%d "
|
| 1545 | "type %d\n", sdp->index, scsidp->type);
|
| 1546 |
|
| 1547 | dev_set_drvdata(cl_dev, sdp);
|
| 1548 |
|
| 1549 | return 0;
|
| 1550 |
|
| 1551 | cdev_add_err:
|
| 1552 | write_lock_irqsave(&sg_index_lock, iflags);
|
| 1553 | idr_remove(&sg_index_idr, sdp->index);
|
| 1554 | write_unlock_irqrestore(&sg_index_lock, iflags);
|
| 1555 | kfree(sdp);
|
| 1556 |
|
| 1557 | out:
|
| 1558 | if (cdev)
|
| 1559 | cdev_del(cdev);
|
| 1560 | blk_put_queue(scsidp->request_queue);
|
| 1561 | return error;
|
| 1562 | }
|
| 1563 |
|
| 1564 | static void
|
| 1565 | sg_device_destroy(struct kref *kref)
|
| 1566 | {
|
| 1567 | struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
|
| 1568 | struct request_queue *q = sdp->device->request_queue;
|
| 1569 | unsigned long flags;
|
| 1570 |
|
| 1571 | |
| 1572 | * even though the refcount is 0. Therefore, do idr_remove() BEFORE
|
| 1573 | * any other cleanup.
|
| 1574 |
|
| 1575 |
|
| 1576 | blk_trace_remove(q);
|
| 1577 | blk_put_queue(q);
|
| 1578 |
|
| 1579 | write_lock_irqsave(&sg_index_lock, flags);
|
| 1580 | idr_remove(&sg_index_idr, sdp->index);
|
| 1581 | write_unlock_irqrestore(&sg_index_lock, flags);
|
| 1582 |
|
| 1583 | SCSI_LOG_TIMEOUT(3,
|
| 1584 | sg_printk(KERN_INFO, sdp, "sg_device_destroy\n"));
|
| 1585 |
|
| 1586 | kfree(sdp);
|
| 1587 | }
|
| 1588 |
|
| 1589 | static void
|
| 1590 | sg_remove_device(struct device *cl_dev)
|
| 1591 | {
|
| 1592 | struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
|
| 1593 | Sg_device *sdp = dev_get_drvdata(cl_dev);
|
| 1594 | unsigned long iflags;
|
| 1595 | Sg_fd *sfp;
|
| 1596 | int val;
|
| 1597 |
|
| 1598 | if (!sdp)
|
| 1599 | return;
|
| 1600 |
|
| 1601 | val = atomic_inc_return(&sdp->detaching);
|
| 1602 | if (val > 1)
|
| 1603 | return;
|
| 1604 |
|
| 1605 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 1606 | "%s\n", __func__));
|
| 1607 |
|
| 1608 | read_lock_irqsave(&sdp->sfd_lock, iflags);
|
| 1609 | list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
|
| 1610 | wake_up_interruptible_all(&sfp->read_wait);
|
| 1611 | kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
|
| 1612 | }
|
| 1613 | wake_up_interruptible_all(&sdp->open_wait);
|
| 1614 | read_unlock_irqrestore(&sdp->sfd_lock, iflags);
|
| 1615 |
|
| 1616 | sysfs_remove_link(&scsidp->sdev_gendev.kobj, "generic");
|
| 1617 | device_destroy(&sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
|
| 1618 | cdev_del(sdp->cdev);
|
| 1619 | sdp->cdev = NULL;
|
| 1620 |
|
| 1621 | kref_put(&sdp->d_ref, sg_device_destroy);
|
| 1622 | }
|
| 1623 |
|
| 1624 | module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
|
| 1625 | module_param_named(allow_dio, sg_allow_dio, int, S_IRUGO | S_IWUSR);
|
| 1626 |
|
| 1627 | static int def_reserved_size_set(const char *val, const struct kernel_param *kp)
|
| 1628 | {
|
| 1629 | int size, ret;
|
| 1630 |
|
| 1631 | if (!val)
|
| 1632 | return -EINVAL;
|
| 1633 |
|
| 1634 | ret = kstrtoint(val, 0, &size);
|
| 1635 | if (ret)
|
| 1636 | return ret;
|
| 1637 |
|
| 1638 |
|
| 1639 | if (size < 0 || size > 1048576)
|
| 1640 | return -ERANGE;
|
| 1641 |
|
| 1642 | def_reserved_size = size;
|
| 1643 | return 0;
|
| 1644 | }
|
| 1645 |
|
| 1646 | static const struct kernel_param_ops def_reserved_size_ops = {
|
| 1647 | .set = def_reserved_size_set,
|
| 1648 | .get = param_get_int,
|
| 1649 | };
|
| 1650 |
|
| 1651 | module_param_cb(def_reserved_size, &def_reserved_size_ops, &def_reserved_size,
|
| 1652 | S_IRUGO | S_IWUSR);
|
| 1653 |
|
| 1654 | MODULE_AUTHOR("Douglas Gilbert");
|
| 1655 | MODULE_DESCRIPTION("SCSI generic (sg) driver");
|
| 1656 | MODULE_LICENSE("GPL");
|
| 1657 | MODULE_VERSION(SG_VERSION_STR);
|
| 1658 | MODULE_ALIAS_CHARDEV_MAJOR(SCSI_GENERIC_MAJOR);
|
| 1659 |
|
| 1660 | MODULE_PARM_DESC(scatter_elem_sz, "scatter gather element "
|
| 1661 | "size (default: max(SG_SCATTER_SZ, PAGE_SIZE))");
|
| 1662 | MODULE_PARM_DESC(def_reserved_size, "size of buffer reserved for each fd");
|
| 1663 | MODULE_PARM_DESC(allow_dio, "allow direct I/O (default: 0 (disallow))");
|
| 1664 |
|
| 1665 | static int __init
|
| 1666 | init_sg(void)
|
| 1667 | {
|
| 1668 | int rc;
|
| 1669 |
|
| 1670 | if (scatter_elem_sz < PAGE_SIZE) {
|
| 1671 | scatter_elem_sz = PAGE_SIZE;
|
| 1672 | scatter_elem_sz_prev = scatter_elem_sz;
|
| 1673 | }
|
| 1674 |
|
| 1675 | rc = register_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
|
| 1676 | SG_MAX_DEVS, "sg");
|
| 1677 | if (rc)
|
| 1678 | return rc;
|
| 1679 | rc = class_register(&sg_sysfs_class);
|
| 1680 | if (rc)
|
| 1681 | goto err_out;
|
| 1682 | sg_sysfs_valid = 1;
|
| 1683 | rc = scsi_register_interface(&sg_interface);
|
| 1684 | if (0 == rc) {
|
| 1685 | #ifdef CONFIG_SCSI_PROC_FS
|
| 1686 | sg_proc_init();
|
| 1687 | #endif
|
| 1688 | return 0;
|
| 1689 | }
|
| 1690 | class_unregister(&sg_sysfs_class);
|
| 1691 | err_out:
|
| 1692 | unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0), SG_MAX_DEVS);
|
| 1693 | return rc;
|
| 1694 | }
|
| 1695 |
|
| 1696 | static void __exit
|
| 1697 | exit_sg(void)
|
| 1698 | {
|
| 1699 | #ifdef CONFIG_SCSI_PROC_FS
|
| 1700 | remove_proc_subtree("scsi/sg", NULL);
|
| 1701 | #endif
|
| 1702 | scsi_unregister_interface(&sg_interface);
|
| 1703 | class_unregister(&sg_sysfs_class);
|
| 1704 | sg_sysfs_valid = 0;
|
| 1705 | unregister_chrdev_region(MKDEV(SCSI_GENERIC_MAJOR, 0),
|
| 1706 | SG_MAX_DEVS);
|
| 1707 | idr_destroy(&sg_index_idr);
|
| 1708 | }
|
| 1709 |
|
| 1710 | static int
|
| 1711 | sg_start_req(Sg_request *srp, unsigned char *cmd)
|
| 1712 | {
|
| 1713 | int res;
|
| 1714 | struct request *rq;
|
| 1715 | Sg_fd *sfp = srp->parentfp;
|
| 1716 | sg_io_hdr_t *hp = &srp->header;
|
| 1717 | int dxfer_len = (int) hp->dxfer_len;
|
| 1718 | int dxfer_dir = hp->dxfer_direction;
|
| 1719 | unsigned int iov_count = hp->iovec_count;
|
| 1720 | Sg_scatter_hold *req_schp = &srp->data;
|
| 1721 | Sg_scatter_hold *rsv_schp = &sfp->reserve;
|
| 1722 | struct request_queue *q = sfp->parentdp->device->request_queue;
|
| 1723 | struct rq_map_data *md, map_data;
|
| 1724 | int rw = hp->dxfer_direction == SG_DXFER_TO_DEV ? ITER_SOURCE : ITER_DEST;
|
| 1725 | struct scsi_cmnd *scmd;
|
| 1726 |
|
| 1727 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1728 | "sg_start_req: dxfer_len=%d\n",
|
| 1729 | dxfer_len));
|
| 1730 |
|
| 1731 | |
| 1732 | * NOTE
|
| 1733 | *
|
| 1734 | * With scsi-mq enabled, there are a fixed number of preallocated
|
| 1735 | * requests equal in number to shost->can_queue. If all of the
|
| 1736 | * preallocated requests are already in use, then scsi_alloc_request()
|
| 1737 | * will sleep until an active command completes, freeing up a request.
|
| 1738 | * Although waiting in an asynchronous interface is less than ideal, we
|
| 1739 | * do not want to use BLK_MQ_REQ_NOWAIT here because userspace might
|
| 1740 | * not expect an EWOULDBLOCK from this condition.
|
| 1741 |
|
| 1742 | rq = scsi_alloc_request(q, hp->dxfer_direction == SG_DXFER_TO_DEV ?
|
| 1743 | REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
|
| 1744 | if (IS_ERR(rq))
|
| 1745 | return PTR_ERR(rq);
|
| 1746 | scmd = blk_mq_rq_to_pdu(rq);
|
| 1747 |
|
| 1748 | if (hp->cmd_len > sizeof(scmd->cmnd)) {
|
| 1749 | blk_mq_free_request(rq);
|
| 1750 | return -EINVAL;
|
| 1751 | }
|
| 1752 |
|
| 1753 | memcpy(scmd->cmnd, cmd, hp->cmd_len);
|
| 1754 | scmd->cmd_len = hp->cmd_len;
|
| 1755 |
|
| 1756 | srp->rq = rq;
|
| 1757 | rq->end_io_data = srp;
|
| 1758 | scmd->allowed = SG_DEFAULT_RETRIES;
|
| 1759 |
|
| 1760 | if ((dxfer_len <= 0) || (dxfer_dir == SG_DXFER_NONE))
|
| 1761 | return 0;
|
| 1762 |
|
| 1763 | if (sg_allow_dio && hp->flags & SG_FLAG_DIRECT_IO &&
|
| 1764 | dxfer_dir != SG_DXFER_UNKNOWN && !iov_count &&
|
| 1765 | blk_rq_aligned(q, (unsigned long)hp->dxferp, dxfer_len))
|
| 1766 | md = NULL;
|
| 1767 | else
|
| 1768 | md = &map_data;
|
| 1769 |
|
| 1770 | if (md) {
|
| 1771 | mutex_lock(&sfp->f_mutex);
|
| 1772 | if (dxfer_len <= rsv_schp->bufflen &&
|
| 1773 | !sfp->res_in_use) {
|
| 1774 | sfp->res_in_use = 1;
|
| 1775 | sg_link_reserve(sfp, srp, dxfer_len);
|
| 1776 | } else if (hp->flags & SG_FLAG_MMAP_IO) {
|
| 1777 | res = -EBUSY;
|
| 1778 | if (dxfer_len > rsv_schp->bufflen)
|
| 1779 | res = -ENOMEM;
|
| 1780 | mutex_unlock(&sfp->f_mutex);
|
| 1781 | return res;
|
| 1782 | } else {
|
| 1783 | res = sg_build_indirect(req_schp, sfp, dxfer_len);
|
| 1784 | if (res) {
|
| 1785 | mutex_unlock(&sfp->f_mutex);
|
| 1786 | return res;
|
| 1787 | }
|
| 1788 | }
|
| 1789 | mutex_unlock(&sfp->f_mutex);
|
| 1790 |
|
| 1791 | md->pages = req_schp->pages;
|
| 1792 | md->page_order = req_schp->page_order;
|
| 1793 | md->nr_entries = req_schp->k_use_sg;
|
| 1794 | md->offset = 0;
|
| 1795 | md->null_mapped = hp->dxferp ? 0 : 1;
|
| 1796 | if (dxfer_dir == SG_DXFER_TO_FROM_DEV)
|
| 1797 | md->from_user = 1;
|
| 1798 | else
|
| 1799 | md->from_user = 0;
|
| 1800 | }
|
| 1801 |
|
| 1802 | res = blk_rq_map_user_io(rq, md, hp->dxferp, hp->dxfer_len,
|
| 1803 | GFP_KERNEL, iov_count, iov_count, 1, rw);
|
| 1804 | if (!res) {
|
| 1805 | srp->bio = rq->bio;
|
| 1806 |
|
| 1807 | if (!md) {
|
| 1808 | req_schp->dio_in_use = 1;
|
| 1809 | hp->info |= SG_INFO_DIRECT_IO;
|
| 1810 | }
|
| 1811 | }
|
| 1812 | return res;
|
| 1813 | }
|
| 1814 |
|
| 1815 | static int
|
| 1816 | sg_finish_rem_req(Sg_request *srp)
|
| 1817 | {
|
| 1818 | int ret = 0;
|
| 1819 |
|
| 1820 | Sg_fd *sfp = srp->parentfp;
|
| 1821 | Sg_scatter_hold *req_schp = &srp->data;
|
| 1822 |
|
| 1823 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1824 | "sg_finish_rem_req: res_used=%d\n",
|
| 1825 | (int) srp->res_used));
|
| 1826 | if (srp->bio)
|
| 1827 | ret = blk_rq_unmap_user(srp->bio);
|
| 1828 |
|
| 1829 | if (srp->rq)
|
| 1830 | blk_mq_free_request(srp->rq);
|
| 1831 |
|
| 1832 | if (srp->res_used)
|
| 1833 | sg_unlink_reserve(sfp, srp);
|
| 1834 | else
|
| 1835 | sg_remove_scat(sfp, req_schp);
|
| 1836 |
|
| 1837 | return ret;
|
| 1838 | }
|
| 1839 |
|
| 1840 | static int
|
| 1841 | sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp, int tablesize)
|
| 1842 | {
|
| 1843 | int sg_bufflen = tablesize * sizeof(struct page *);
|
| 1844 | gfp_t gfp_flags = GFP_ATOMIC | __GFP_NOWARN;
|
| 1845 |
|
| 1846 | schp->pages = kzalloc(sg_bufflen, gfp_flags);
|
| 1847 | if (!schp->pages)
|
| 1848 | return -ENOMEM;
|
| 1849 | schp->sglist_len = sg_bufflen;
|
| 1850 | return tablesize;
|
| 1851 | }
|
| 1852 |
|
| 1853 | static int
|
| 1854 | sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
|
| 1855 | {
|
| 1856 | int ret_sz = 0, i, k, rem_sz, num, mx_sc_elems;
|
| 1857 | int sg_tablesize = sfp->parentdp->sg_tablesize;
|
| 1858 | int blk_size = buff_size, order;
|
| 1859 | gfp_t gfp_mask = GFP_ATOMIC | __GFP_COMP | __GFP_NOWARN | __GFP_ZERO;
|
| 1860 |
|
| 1861 | if (blk_size < 0)
|
| 1862 | return -EFAULT;
|
| 1863 | if (0 == blk_size)
|
| 1864 | ++blk_size;
|
| 1865 |
|
| 1866 | blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
|
| 1867 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1868 | "sg_build_indirect: buff_size=%d, blk_size=%d\n",
|
| 1869 | buff_size, blk_size));
|
| 1870 |
|
| 1871 |
|
| 1872 | mx_sc_elems = sg_build_sgat(schp, sfp, sg_tablesize);
|
| 1873 | if (mx_sc_elems < 0)
|
| 1874 | return mx_sc_elems;
|
| 1875 |
|
| 1876 | num = scatter_elem_sz;
|
| 1877 | if (unlikely(num != scatter_elem_sz_prev)) {
|
| 1878 | if (num < PAGE_SIZE) {
|
| 1879 | scatter_elem_sz = PAGE_SIZE;
|
| 1880 | scatter_elem_sz_prev = PAGE_SIZE;
|
| 1881 | } else
|
| 1882 | scatter_elem_sz_prev = num;
|
| 1883 | }
|
| 1884 |
|
| 1885 | order = get_order(num);
|
| 1886 | retry:
|
| 1887 | ret_sz = 1 << (PAGE_SHIFT + order);
|
| 1888 |
|
| 1889 | for (k = 0, rem_sz = blk_size; rem_sz > 0 && k < mx_sc_elems;
|
| 1890 | k++, rem_sz -= ret_sz) {
|
| 1891 |
|
| 1892 | num = (rem_sz > scatter_elem_sz_prev) ?
|
| 1893 | scatter_elem_sz_prev : rem_sz;
|
| 1894 |
|
| 1895 | schp->pages[k] = alloc_pages(gfp_mask, order);
|
| 1896 | if (!schp->pages[k])
|
| 1897 | goto out;
|
| 1898 |
|
| 1899 | if (num == scatter_elem_sz_prev) {
|
| 1900 | if (unlikely(ret_sz > scatter_elem_sz_prev)) {
|
| 1901 | scatter_elem_sz = ret_sz;
|
| 1902 | scatter_elem_sz_prev = ret_sz;
|
| 1903 | }
|
| 1904 | }
|
| 1905 |
|
| 1906 | SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1907 | "sg_build_indirect: k=%d, num=%d, ret_sz=%d\n",
|
| 1908 | k, num, ret_sz));
|
| 1909 | }
|
| 1910 |
|
| 1911 | schp->page_order = order;
|
| 1912 | schp->k_use_sg = k;
|
| 1913 | SCSI_LOG_TIMEOUT(5, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1914 | "sg_build_indirect: k_use_sg=%d, rem_sz=%d\n",
|
| 1915 | k, rem_sz));
|
| 1916 |
|
| 1917 | schp->bufflen = blk_size;
|
| 1918 | if (rem_sz > 0)
|
| 1919 | return -ENOMEM;
|
| 1920 | return 0;
|
| 1921 | out:
|
| 1922 | for (i = 0; i < k; i++)
|
| 1923 | __free_pages(schp->pages[i], order);
|
| 1924 |
|
| 1925 | if (--order >= 0)
|
| 1926 | goto retry;
|
| 1927 |
|
| 1928 | return -ENOMEM;
|
| 1929 | }
|
| 1930 |
|
| 1931 | static void
|
| 1932 | sg_remove_scat(Sg_fd * sfp, Sg_scatter_hold * schp)
|
| 1933 | {
|
| 1934 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1935 | "sg_remove_scat: k_use_sg=%d\n", schp->k_use_sg));
|
| 1936 | if (schp->pages && schp->sglist_len > 0) {
|
| 1937 | if (!schp->dio_in_use) {
|
| 1938 | int k;
|
| 1939 |
|
| 1940 | for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
|
| 1941 | SCSI_LOG_TIMEOUT(5,
|
| 1942 | sg_printk(KERN_INFO, sfp->parentdp,
|
| 1943 | "sg_remove_scat: k=%d, pg=0x%p\n",
|
| 1944 | k, schp->pages[k]));
|
| 1945 | __free_pages(schp->pages[k], schp->page_order);
|
| 1946 | }
|
| 1947 |
|
| 1948 | kfree(schp->pages);
|
| 1949 | }
|
| 1950 | }
|
| 1951 | memset(schp, 0, sizeof (*schp));
|
| 1952 | }
|
| 1953 |
|
| 1954 | static int
|
| 1955 | sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer)
|
| 1956 | {
|
| 1957 | Sg_scatter_hold *schp = &srp->data;
|
| 1958 | int k, num;
|
| 1959 |
|
| 1960 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
|
| 1961 | "sg_read_oxfer: num_read_xfer=%d\n",
|
| 1962 | num_read_xfer));
|
| 1963 | if ((!outp) || (num_read_xfer <= 0))
|
| 1964 | return 0;
|
| 1965 |
|
| 1966 | num = 1 << (PAGE_SHIFT + schp->page_order);
|
| 1967 | for (k = 0; k < schp->k_use_sg && schp->pages[k]; k++) {
|
| 1968 | if (num > num_read_xfer) {
|
| 1969 | if (copy_to_user(outp, page_address(schp->pages[k]),
|
| 1970 | num_read_xfer))
|
| 1971 | return -EFAULT;
|
| 1972 | break;
|
| 1973 | } else {
|
| 1974 | if (copy_to_user(outp, page_address(schp->pages[k]),
|
| 1975 | num))
|
| 1976 | return -EFAULT;
|
| 1977 | num_read_xfer -= num;
|
| 1978 | if (num_read_xfer <= 0)
|
| 1979 | break;
|
| 1980 | outp += num;
|
| 1981 | }
|
| 1982 | }
|
| 1983 |
|
| 1984 | return 0;
|
| 1985 | }
|
| 1986 |
|
| 1987 | static void
|
| 1988 | sg_build_reserve(Sg_fd * sfp, int req_size)
|
| 1989 | {
|
| 1990 | Sg_scatter_hold *schp = &sfp->reserve;
|
| 1991 |
|
| 1992 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 1993 | "sg_build_reserve: req_size=%d\n", req_size));
|
| 1994 | do {
|
| 1995 | if (req_size < PAGE_SIZE)
|
| 1996 | req_size = PAGE_SIZE;
|
| 1997 | if (0 == sg_build_indirect(schp, sfp, req_size))
|
| 1998 | return;
|
| 1999 | else
|
| 2000 | sg_remove_scat(sfp, schp);
|
| 2001 | req_size >>= 1;
|
| 2002 | } while (req_size > (PAGE_SIZE / 2));
|
| 2003 | }
|
| 2004 |
|
| 2005 | static void
|
| 2006 | sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size)
|
| 2007 | {
|
| 2008 | Sg_scatter_hold *req_schp = &srp->data;
|
| 2009 | Sg_scatter_hold *rsv_schp = &sfp->reserve;
|
| 2010 | int k, num, rem;
|
| 2011 |
|
| 2012 | srp->res_used = 1;
|
| 2013 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, sfp->parentdp,
|
| 2014 | "sg_link_reserve: size=%d\n", size));
|
| 2015 | rem = size;
|
| 2016 |
|
| 2017 | num = 1 << (PAGE_SHIFT + rsv_schp->page_order);
|
| 2018 | for (k = 0; k < rsv_schp->k_use_sg; k++) {
|
| 2019 | if (rem <= num) {
|
| 2020 | req_schp->k_use_sg = k + 1;
|
| 2021 | req_schp->sglist_len = rsv_schp->sglist_len;
|
| 2022 | req_schp->pages = rsv_schp->pages;
|
| 2023 |
|
| 2024 | req_schp->bufflen = size;
|
| 2025 | req_schp->page_order = rsv_schp->page_order;
|
| 2026 | break;
|
| 2027 | } else
|
| 2028 | rem -= num;
|
| 2029 | }
|
| 2030 |
|
| 2031 | if (k >= rsv_schp->k_use_sg)
|
| 2032 | SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
|
| 2033 | "sg_link_reserve: BAD size\n"));
|
| 2034 | }
|
| 2035 |
|
| 2036 | static void
|
| 2037 | sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp)
|
| 2038 | {
|
| 2039 | Sg_scatter_hold *req_schp = &srp->data;
|
| 2040 |
|
| 2041 | SCSI_LOG_TIMEOUT(4, sg_printk(KERN_INFO, srp->parentfp->parentdp,
|
| 2042 | "sg_unlink_reserve: req->k_use_sg=%d\n",
|
| 2043 | (int) req_schp->k_use_sg));
|
| 2044 | req_schp->k_use_sg = 0;
|
| 2045 | req_schp->bufflen = 0;
|
| 2046 | req_schp->pages = NULL;
|
| 2047 | req_schp->page_order = 0;
|
| 2048 | req_schp->sglist_len = 0;
|
| 2049 | srp->res_used = 0;
|
| 2050 |
|
| 2051 | sfp->res_in_use = 0;
|
| 2052 | }
|
| 2053 |
|
| 2054 | static Sg_request *
|
| 2055 | sg_get_rq_mark(Sg_fd * sfp, int pack_id, bool *busy)
|
| 2056 | {
|
| 2057 | Sg_request *resp;
|
| 2058 | unsigned long iflags;
|
| 2059 |
|
| 2060 | *busy = false;
|
| 2061 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 2062 | list_for_each_entry(resp, &sfp->rq_list, entry) {
|
| 2063 |
|
| 2064 | if ((!resp->sg_io_owned) &&
|
| 2065 | ((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
|
| 2066 | switch (resp->done) {
|
| 2067 | case 0:
|
| 2068 | *busy = true;
|
| 2069 | break;
|
| 2070 | case 1:
|
| 2071 | resp->done = 2;
|
| 2072 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2073 | return resp;
|
| 2074 | case 2:
|
| 2075 | break;
|
| 2076 | }
|
| 2077 | }
|
| 2078 | }
|
| 2079 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2080 | return NULL;
|
| 2081 | }
|
| 2082 |
|
| 2083 |
|
| 2084 | static Sg_request *
|
| 2085 | sg_add_request(Sg_fd * sfp)
|
| 2086 | {
|
| 2087 | int k;
|
| 2088 | unsigned long iflags;
|
| 2089 | Sg_request *rp = sfp->req_arr;
|
| 2090 |
|
| 2091 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 2092 | if (!list_empty(&sfp->rq_list)) {
|
| 2093 | if (!sfp->cmd_q)
|
| 2094 | goto out_unlock;
|
| 2095 |
|
| 2096 | for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
|
| 2097 | if (!rp->parentfp)
|
| 2098 | break;
|
| 2099 | }
|
| 2100 | if (k >= SG_MAX_QUEUE)
|
| 2101 | goto out_unlock;
|
| 2102 | }
|
| 2103 | memset(rp, 0, sizeof (Sg_request));
|
| 2104 | rp->parentfp = sfp;
|
| 2105 | rp->header.duration = jiffies_to_msecs(jiffies);
|
| 2106 | list_add_tail(&rp->entry, &sfp->rq_list);
|
| 2107 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2108 | return rp;
|
| 2109 | out_unlock:
|
| 2110 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2111 | return NULL;
|
| 2112 | }
|
| 2113 |
|
| 2114 |
|
| 2115 | static int
|
| 2116 | sg_remove_request(Sg_fd * sfp, Sg_request * srp)
|
| 2117 | {
|
| 2118 | unsigned long iflags;
|
| 2119 | int res = 0;
|
| 2120 |
|
| 2121 | if (!sfp || !srp || list_empty(&sfp->rq_list))
|
| 2122 | return res;
|
| 2123 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 2124 | if (!list_empty(&srp->entry)) {
|
| 2125 | list_del(&srp->entry);
|
| 2126 | srp->parentfp = NULL;
|
| 2127 | res = 1;
|
| 2128 | }
|
| 2129 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2130 |
|
| 2131 | |
| 2132 | * If the device is detaching, wakeup any readers in case we just
|
| 2133 | * removed the last response, which would leave nothing for them to
|
| 2134 | * return other than -ENODEV.
|
| 2135 |
|
| 2136 | if (unlikely(atomic_read(&sfp->parentdp->detaching)))
|
| 2137 | wake_up_interruptible_all(&sfp->read_wait);
|
| 2138 |
|
| 2139 | return res;
|
| 2140 | }
|
| 2141 |
|
| 2142 | static Sg_fd *
|
| 2143 | sg_add_sfp(Sg_device * sdp)
|
| 2144 | {
|
| 2145 | Sg_fd *sfp;
|
| 2146 | unsigned long iflags;
|
| 2147 | int bufflen;
|
| 2148 |
|
| 2149 | sfp = kzalloc_obj(*sfp, GFP_ATOMIC | __GFP_NOWARN);
|
| 2150 | if (!sfp)
|
| 2151 | return ERR_PTR(-ENOMEM);
|
| 2152 |
|
| 2153 | init_waitqueue_head(&sfp->read_wait);
|
| 2154 | rwlock_init(&sfp->rq_list_lock);
|
| 2155 | INIT_LIST_HEAD(&sfp->rq_list);
|
| 2156 | kref_init(&sfp->f_ref);
|
| 2157 | mutex_init(&sfp->f_mutex);
|
| 2158 | sfp->timeout = SG_DEFAULT_TIMEOUT;
|
| 2159 | sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
|
| 2160 | sfp->force_packid = SG_DEF_FORCE_PACK_ID;
|
| 2161 | sfp->cmd_q = SG_DEF_COMMAND_Q;
|
| 2162 | sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
|
| 2163 | sfp->parentdp = sdp;
|
| 2164 | write_lock_irqsave(&sdp->sfd_lock, iflags);
|
| 2165 | if (atomic_read(&sdp->detaching)) {
|
| 2166 | write_unlock_irqrestore(&sdp->sfd_lock, iflags);
|
| 2167 | kfree(sfp);
|
| 2168 | return ERR_PTR(-ENODEV);
|
| 2169 | }
|
| 2170 | list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
|
| 2171 | write_unlock_irqrestore(&sdp->sfd_lock, iflags);
|
| 2172 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 2173 | "sg_add_sfp: sfp=0x%p\n", sfp));
|
| 2174 |
|
| 2175 | bufflen = min_t(int, def_reserved_size,
|
| 2176 | max_sectors_bytes(sdp->device->request_queue));
|
| 2177 | sg_build_reserve(sfp, bufflen);
|
| 2178 | SCSI_LOG_TIMEOUT(3, sg_printk(KERN_INFO, sdp,
|
| 2179 | "sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
|
| 2180 | sfp->reserve.bufflen,
|
| 2181 | sfp->reserve.k_use_sg));
|
| 2182 |
|
| 2183 | kref_get(&sdp->d_ref);
|
| 2184 | __module_get(THIS_MODULE);
|
| 2185 | return sfp;
|
| 2186 | }
|
| 2187 |
|
| 2188 | static void
|
| 2189 | sg_remove_sfp_usercontext(struct work_struct *work)
|
| 2190 | {
|
| 2191 | struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
|
| 2192 | struct sg_device *sdp = sfp->parentdp;
|
| 2193 | struct scsi_device *device = sdp->device;
|
| 2194 | Sg_request *srp;
|
| 2195 | unsigned long iflags;
|
| 2196 |
|
| 2197 |
|
| 2198 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 2199 | while (!list_empty(&sfp->rq_list)) {
|
| 2200 | srp = list_first_entry(&sfp->rq_list, Sg_request, entry);
|
| 2201 | list_del(&srp->entry);
|
| 2202 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2203 |
|
| 2204 | sg_finish_rem_req(srp);
|
| 2205 | |
| 2206 | * sg_rq_end_io() uses srp->parentfp. Hence, only clear
|
| 2207 | * srp->parentfp after blk_mq_free_request() has been called.
|
| 2208 |
|
| 2209 | srp->parentfp = NULL;
|
| 2210 |
|
| 2211 | write_lock_irqsave(&sfp->rq_list_lock, iflags);
|
| 2212 | }
|
| 2213 | write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
|
| 2214 |
|
| 2215 | if (sfp->reserve.bufflen > 0) {
|
| 2216 | SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
|
| 2217 | "sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
|
| 2218 | (int) sfp->reserve.bufflen,
|
| 2219 | (int) sfp->reserve.k_use_sg));
|
| 2220 | sg_remove_scat(sfp, &sfp->reserve);
|
| 2221 | }
|
| 2222 |
|
| 2223 | SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
|
| 2224 | "sg_remove_sfp: sfp=0x%p\n", sfp));
|
| 2225 | kfree(sfp);
|
| 2226 |
|
| 2227 | kref_put(&sdp->d_ref, sg_device_destroy);
|
| 2228 | scsi_device_put(device);
|
| 2229 | module_put(THIS_MODULE);
|
| 2230 | }
|
| 2231 |
|
| 2232 | static void
|
| 2233 | sg_remove_sfp(struct kref *kref)
|
| 2234 | {
|
| 2235 | struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
|
| 2236 | struct sg_device *sdp = sfp->parentdp;
|
| 2237 | unsigned long iflags;
|
| 2238 |
|
| 2239 | write_lock_irqsave(&sdp->sfd_lock, iflags);
|
| 2240 | list_del(&sfp->sfd_siblings);
|
| 2241 | write_unlock_irqrestore(&sdp->sfd_lock, iflags);
|
| 2242 |
|
| 2243 | INIT_WORK(&sfp->ew.work, sg_remove_sfp_usercontext);
|
| 2244 | schedule_work(&sfp->ew.work);
|
| 2245 | }
|
| 2246 |
|
| 2247 | #ifdef CONFIG_SCSI_PROC_FS
|
| 2248 | static int
|
| 2249 | sg_idr_max_id(int id, void *p, void *data)
|
| 2250 | {
|
| 2251 | int *k = data;
|
| 2252 |
|
| 2253 | if (*k < id)
|
| 2254 | *k = id;
|
| 2255 |
|
| 2256 | return 0;
|
| 2257 | }
|
| 2258 |
|
| 2259 | static int
|
| 2260 | sg_last_dev(void)
|
| 2261 | {
|
| 2262 | int k = -1;
|
| 2263 | unsigned long iflags;
|
| 2264 |
|
| 2265 | read_lock_irqsave(&sg_index_lock, iflags);
|
| 2266 | idr_for_each(&sg_index_idr, sg_idr_max_id, &k);
|
| 2267 | read_unlock_irqrestore(&sg_index_lock, iflags);
|
| 2268 | return k + 1;
|
| 2269 | }
|
| 2270 | #endif
|
| 2271 |
|
| 2272 |
|
| 2273 | static Sg_device *sg_lookup_dev(int dev)
|
| 2274 | {
|
| 2275 | return idr_find(&sg_index_idr, dev);
|
| 2276 | }
|
| 2277 |
|
| 2278 | static Sg_device *
|
| 2279 | sg_get_dev(int dev)
|
| 2280 | {
|
| 2281 | struct sg_device *sdp;
|
| 2282 | unsigned long flags;
|
| 2283 |
|
| 2284 | read_lock_irqsave(&sg_index_lock, flags);
|
| 2285 | sdp = sg_lookup_dev(dev);
|
| 2286 | if (!sdp)
|
| 2287 | sdp = ERR_PTR(-ENXIO);
|
| 2288 | else if (atomic_read(&sdp->detaching)) {
|
| 2289 | |
| 2290 | * which case it would be a bug to do kref_get().
|
| 2291 |
|
| 2292 | sdp = ERR_PTR(-ENODEV);
|
| 2293 | } else
|
| 2294 | kref_get(&sdp->d_ref);
|
| 2295 | read_unlock_irqrestore(&sg_index_lock, flags);
|
| 2296 |
|
| 2297 | return sdp;
|
| 2298 | }
|
| 2299 |
|
| 2300 | #ifdef CONFIG_SCSI_PROC_FS
|
| 2301 | static int sg_proc_seq_show_int(struct seq_file *s, void *v);
|
| 2302 |
|
| 2303 | static int sg_proc_single_open_adio(struct inode *inode, struct file *file);
|
| 2304 | static ssize_t sg_proc_write_adio(struct file *filp, const char __user *buffer,
|
| 2305 | size_t count, loff_t *off);
|
| 2306 | static const struct proc_ops adio_proc_ops = {
|
| 2307 | .proc_open = sg_proc_single_open_adio,
|
| 2308 | .proc_read = seq_read,
|
| 2309 | .proc_lseek = seq_lseek,
|
| 2310 | .proc_write = sg_proc_write_adio,
|
| 2311 | .proc_release = single_release,
|
| 2312 | };
|
| 2313 |
|
| 2314 | static int sg_proc_single_open_dressz(struct inode *inode, struct file *file);
|
| 2315 | static ssize_t sg_proc_write_dressz(struct file *filp,
|
| 2316 | const char __user *buffer, size_t count, loff_t *off);
|
| 2317 | static const struct proc_ops dressz_proc_ops = {
|
| 2318 | .proc_open = sg_proc_single_open_dressz,
|
| 2319 | .proc_read = seq_read,
|
| 2320 | .proc_lseek = seq_lseek,
|
| 2321 | .proc_write = sg_proc_write_dressz,
|
| 2322 | .proc_release = single_release,
|
| 2323 | };
|
| 2324 |
|
| 2325 | static int sg_proc_seq_show_version(struct seq_file *s, void *v);
|
| 2326 | static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v);
|
| 2327 | static int sg_proc_seq_show_dev(struct seq_file *s, void *v);
|
| 2328 | static void * dev_seq_start(struct seq_file *s, loff_t *pos);
|
| 2329 | static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos);
|
| 2330 | static void dev_seq_stop(struct seq_file *s, void *v);
|
| 2331 | static const struct seq_operations dev_seq_ops = {
|
| 2332 | .start = dev_seq_start,
|
| 2333 | .next = dev_seq_next,
|
| 2334 | .stop = dev_seq_stop,
|
| 2335 | .show = sg_proc_seq_show_dev,
|
| 2336 | };
|
| 2337 |
|
| 2338 | static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v);
|
| 2339 | static const struct seq_operations devstrs_seq_ops = {
|
| 2340 | .start = dev_seq_start,
|
| 2341 | .next = dev_seq_next,
|
| 2342 | .stop = dev_seq_stop,
|
| 2343 | .show = sg_proc_seq_show_devstrs,
|
| 2344 | };
|
| 2345 |
|
| 2346 | static int sg_proc_seq_show_debug(struct seq_file *s, void *v);
|
| 2347 | static const struct seq_operations debug_seq_ops = {
|
| 2348 | .start = dev_seq_start,
|
| 2349 | .next = dev_seq_next,
|
| 2350 | .stop = dev_seq_stop,
|
| 2351 | .show = sg_proc_seq_show_debug,
|
| 2352 | };
|
| 2353 |
|
| 2354 | static int
|
| 2355 | sg_proc_init(void)
|
| 2356 | {
|
| 2357 | struct proc_dir_entry *p;
|
| 2358 |
|
| 2359 | p = proc_mkdir("scsi/sg", NULL);
|
| 2360 | if (!p)
|
| 2361 | return 1;
|
| 2362 |
|
| 2363 | proc_create("allow_dio", S_IRUGO | S_IWUSR, p, &adio_proc_ops);
|
| 2364 | proc_create_seq("debug", S_IRUGO, p, &debug_seq_ops);
|
| 2365 | proc_create("def_reserved_size", S_IRUGO | S_IWUSR, p, &dressz_proc_ops);
|
| 2366 | proc_create_single("device_hdr", S_IRUGO, p, sg_proc_seq_show_devhdr);
|
| 2367 | proc_create_seq("devices", S_IRUGO, p, &dev_seq_ops);
|
| 2368 | proc_create_seq("device_strs", S_IRUGO, p, &devstrs_seq_ops);
|
| 2369 | proc_create_single("version", S_IRUGO, p, sg_proc_seq_show_version);
|
| 2370 | return 0;
|
| 2371 | }
|
| 2372 |
|
| 2373 |
|
| 2374 | static int sg_proc_seq_show_int(struct seq_file *s, void *v)
|
| 2375 | {
|
| 2376 | seq_printf(s, "%d\n", *((int *)s->private));
|
| 2377 | return 0;
|
| 2378 | }
|
| 2379 |
|
| 2380 | static int sg_proc_single_open_adio(struct inode *inode, struct file *file)
|
| 2381 | {
|
| 2382 | return single_open(file, sg_proc_seq_show_int, &sg_allow_dio);
|
| 2383 | }
|
| 2384 |
|
| 2385 | static ssize_t
|
| 2386 | sg_proc_write_adio(struct file *filp, const char __user *buffer,
|
| 2387 | size_t count, loff_t *off)
|
| 2388 | {
|
| 2389 | int err;
|
| 2390 | unsigned long num;
|
| 2391 |
|
| 2392 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
|
| 2393 | return -EACCES;
|
| 2394 | err = kstrtoul_from_user(buffer, count, 0, &num);
|
| 2395 | if (err)
|
| 2396 | return err;
|
| 2397 | sg_allow_dio = num ? 1 : 0;
|
| 2398 | return count;
|
| 2399 | }
|
| 2400 |
|
| 2401 | static int sg_proc_single_open_dressz(struct inode *inode, struct file *file)
|
| 2402 | {
|
| 2403 | return single_open(file, sg_proc_seq_show_int, &def_reserved_size);
|
| 2404 | }
|
| 2405 |
|
| 2406 | static ssize_t
|
| 2407 | sg_proc_write_dressz(struct file *filp, const char __user *buffer,
|
| 2408 | size_t count, loff_t *off)
|
| 2409 | {
|
| 2410 | int err;
|
| 2411 | unsigned long k = ULONG_MAX;
|
| 2412 |
|
| 2413 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
|
| 2414 | return -EACCES;
|
| 2415 |
|
| 2416 | err = kstrtoul_from_user(buffer, count, 0, &k);
|
| 2417 | if (err)
|
| 2418 | return err;
|
| 2419 | if (k <= 1048576) {
|
| 2420 | def_reserved_size = k;
|
| 2421 | return count;
|
| 2422 | }
|
| 2423 | return -ERANGE;
|
| 2424 | }
|
| 2425 |
|
| 2426 | static int sg_proc_seq_show_version(struct seq_file *s, void *v)
|
| 2427 | {
|
| 2428 | seq_printf(s, "%d\t%s [%s]\n", sg_version_num, SG_VERSION_STR,
|
| 2429 | sg_version_date);
|
| 2430 | return 0;
|
| 2431 | }
|
| 2432 |
|
| 2433 | static int sg_proc_seq_show_devhdr(struct seq_file *s, void *v)
|
| 2434 | {
|
| 2435 | seq_puts(s, "host\tchan\tid\tlun\ttype\topens\tqdepth\tbusy\tonline\n");
|
| 2436 | return 0;
|
| 2437 | }
|
| 2438 |
|
| 2439 | struct sg_proc_deviter {
|
| 2440 | loff_t index;
|
| 2441 | size_t max;
|
| 2442 | };
|
| 2443 |
|
| 2444 | static void * dev_seq_start(struct seq_file *s, loff_t *pos)
|
| 2445 | {
|
| 2446 | struct sg_proc_deviter * it = kmalloc_obj(*it);
|
| 2447 |
|
| 2448 | s->private = it;
|
| 2449 | if (! it)
|
| 2450 | return NULL;
|
| 2451 |
|
| 2452 | it->index = *pos;
|
| 2453 | it->max = sg_last_dev();
|
| 2454 | if (it->index >= it->max)
|
| 2455 | return NULL;
|
| 2456 | return it;
|
| 2457 | }
|
| 2458 |
|
| 2459 | static void * dev_seq_next(struct seq_file *s, void *v, loff_t *pos)
|
| 2460 | {
|
| 2461 | struct sg_proc_deviter * it = s->private;
|
| 2462 |
|
| 2463 | *pos = ++it->index;
|
| 2464 | return (it->index < it->max) ? it : NULL;
|
| 2465 | }
|
| 2466 |
|
| 2467 | static void dev_seq_stop(struct seq_file *s, void *v)
|
| 2468 | {
|
| 2469 | kfree(s->private);
|
| 2470 | }
|
| 2471 |
|
| 2472 | static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
|
| 2473 | {
|
| 2474 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
|
| 2475 | Sg_device *sdp;
|
| 2476 | struct scsi_device *scsidp;
|
| 2477 | unsigned long iflags;
|
| 2478 |
|
| 2479 | read_lock_irqsave(&sg_index_lock, iflags);
|
| 2480 | sdp = it ? sg_lookup_dev(it->index) : NULL;
|
| 2481 | if ((NULL == sdp) || (NULL == sdp->device) ||
|
| 2482 | (atomic_read(&sdp->detaching)))
|
| 2483 | seq_puts(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
|
| 2484 | else {
|
| 2485 | scsidp = sdp->device;
|
| 2486 | seq_printf(s, "%d\t%d\t%d\t%llu\t%d\t%d\t%d\t%d\t%d\n",
|
| 2487 | scsidp->host->host_no, scsidp->channel,
|
| 2488 | scsidp->id, scsidp->lun, (int) scsidp->type,
|
| 2489 | 1,
|
| 2490 | (int) scsidp->queue_depth,
|
| 2491 | (int) scsi_device_busy(scsidp),
|
| 2492 | (int) scsi_device_online(scsidp));
|
| 2493 | }
|
| 2494 | read_unlock_irqrestore(&sg_index_lock, iflags);
|
| 2495 | return 0;
|
| 2496 | }
|
| 2497 |
|
| 2498 | static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
|
| 2499 | {
|
| 2500 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
|
| 2501 | Sg_device *sdp;
|
| 2502 | struct scsi_device *scsidp;
|
| 2503 | unsigned long iflags;
|
| 2504 |
|
| 2505 | read_lock_irqsave(&sg_index_lock, iflags);
|
| 2506 | sdp = it ? sg_lookup_dev(it->index) : NULL;
|
| 2507 | scsidp = sdp ? sdp->device : NULL;
|
| 2508 | if (sdp && scsidp && (!atomic_read(&sdp->detaching)))
|
| 2509 | seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
|
| 2510 | scsidp->vendor, scsidp->model, scsidp->rev);
|
| 2511 | else
|
| 2512 | seq_puts(s, "<no active device>\n");
|
| 2513 | read_unlock_irqrestore(&sg_index_lock, iflags);
|
| 2514 | return 0;
|
| 2515 | }
|
| 2516 |
|
| 2517 |
|
| 2518 | static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
|
| 2519 | {
|
| 2520 | int k, new_interface, blen, usg;
|
| 2521 | Sg_request *srp;
|
| 2522 | Sg_fd *fp;
|
| 2523 | const sg_io_hdr_t *hp;
|
| 2524 | const char * cp;
|
| 2525 | unsigned int ms;
|
| 2526 | unsigned int duration;
|
| 2527 |
|
| 2528 | k = 0;
|
| 2529 | list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
|
| 2530 | k++;
|
| 2531 | read_lock(&fp->rq_list_lock);
|
| 2532 | seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
|
| 2533 | "(res)sgat=%d low_dma=%d\n", k,
|
| 2534 | jiffies_to_msecs(fp->timeout),
|
| 2535 | fp->reserve.bufflen,
|
| 2536 | (int) fp->reserve.k_use_sg, 0);
|
| 2537 | seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
|
| 2538 | (int) fp->cmd_q, (int) fp->force_packid,
|
| 2539 | (int) fp->keep_orphan);
|
| 2540 | list_for_each_entry(srp, &fp->rq_list, entry) {
|
| 2541 | hp = &srp->header;
|
| 2542 | new_interface = (hp->interface_id == '\0') ? 0 : 1;
|
| 2543 | if (srp->res_used) {
|
| 2544 | if (new_interface &&
|
| 2545 | (SG_FLAG_MMAP_IO & hp->flags))
|
| 2546 | cp = " mmap>> ";
|
| 2547 | else
|
| 2548 | cp = " rb>> ";
|
| 2549 | } else {
|
| 2550 | if (SG_INFO_DIRECT_IO_MASK & hp->info)
|
| 2551 | cp = " dio>> ";
|
| 2552 | else
|
| 2553 | cp = " ";
|
| 2554 | }
|
| 2555 | seq_puts(s, cp);
|
| 2556 | blen = srp->data.bufflen;
|
| 2557 | usg = srp->data.k_use_sg;
|
| 2558 | seq_puts(s, srp->done ?
|
| 2559 | ((1 == srp->done) ? "rcv:" : "fin:")
|
| 2560 | : "act:");
|
| 2561 | seq_printf(s, " id=%d blen=%d",
|
| 2562 | srp->header.pack_id, blen);
|
| 2563 | if (srp->done)
|
| 2564 | seq_printf(s, " dur=%u", hp->duration);
|
| 2565 | else {
|
| 2566 | ms = jiffies_to_msecs(jiffies);
|
| 2567 | duration = READ_ONCE(hp->duration);
|
| 2568 | if (duration)
|
| 2569 | duration = (ms > duration ?
|
| 2570 | ms - duration : 0);
|
| 2571 | seq_printf(s, " t_o/elap=%u/%u",
|
| 2572 | (new_interface ? hp->timeout :
|
| 2573 | jiffies_to_msecs(fp->timeout)),
|
| 2574 | duration);
|
| 2575 | }
|
| 2576 | seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
|
| 2577 | (int) srp->data.cmd_opcode);
|
| 2578 | }
|
| 2579 | if (list_empty(&fp->rq_list))
|
| 2580 | seq_puts(s, " No requests active\n");
|
| 2581 | read_unlock(&fp->rq_list_lock);
|
| 2582 | }
|
| 2583 | }
|
| 2584 |
|
| 2585 | static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
|
| 2586 | {
|
| 2587 | struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
|
| 2588 | Sg_device *sdp;
|
| 2589 | unsigned long iflags;
|
| 2590 |
|
| 2591 | if (it && (0 == it->index))
|
| 2592 | seq_printf(s, "max_active_device=%d def_reserved_size=%d\n",
|
| 2593 | (int)it->max, def_reserved_size);
|
| 2594 |
|
| 2595 | read_lock_irqsave(&sg_index_lock, iflags);
|
| 2596 | sdp = it ? sg_lookup_dev(it->index) : NULL;
|
| 2597 | if (NULL == sdp)
|
| 2598 | goto skip;
|
| 2599 | read_lock(&sdp->sfd_lock);
|
| 2600 | if (!list_empty(&sdp->sfds)) {
|
| 2601 | seq_printf(s, " >>> device=%s ", sdp->name);
|
| 2602 | if (atomic_read(&sdp->detaching))
|
| 2603 | seq_puts(s, "detaching pending close ");
|
| 2604 | else if (sdp->device) {
|
| 2605 | struct scsi_device *scsidp = sdp->device;
|
| 2606 |
|
| 2607 | seq_printf(s, "%d:%d:%d:%llu em=%d",
|
| 2608 | scsidp->host->host_no,
|
| 2609 | scsidp->channel, scsidp->id,
|
| 2610 | scsidp->lun,
|
| 2611 | scsidp->host->hostt->emulated);
|
| 2612 | }
|
| 2613 | seq_printf(s, " sg_tablesize=%d excl=%d open_cnt=%d\n",
|
| 2614 | sdp->sg_tablesize, sdp->exclude, sdp->open_cnt);
|
| 2615 | sg_proc_debug_helper(s, sdp);
|
| 2616 | }
|
| 2617 | read_unlock(&sdp->sfd_lock);
|
| 2618 | skip:
|
| 2619 | read_unlock_irqrestore(&sg_index_lock, iflags);
|
| 2620 | return 0;
|
| 2621 | }
|
| 2622 |
|
| 2623 | #endif
|
| 2624 |
|
| 2625 | module_init(init_sg);
|
| 2626 | module_exit(exit_sg);
|