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