init from v2.6.32.60
[mach-moxart.git] / block / scsi_ioctl.c
blob2be0a978dfbde0f85c8097d15cb4e60194308ed4
1 /*
2 * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/string.h>
22 #include <linux/module.h>
23 #include <linux/blkdev.h>
24 #include <linux/capability.h>
25 #include <linux/completion.h>
26 #include <linux/cdrom.h>
27 #include <linux/ratelimit.h>
28 #include <linux/slab.h>
29 #include <linux/times.h>
30 #include <asm/uaccess.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_ioctl.h>
34 #include <scsi/scsi_cmnd.h>
36 struct blk_cmd_filter {
37 unsigned long read_ok[BLK_SCSI_CMD_PER_LONG];
38 unsigned long write_ok[BLK_SCSI_CMD_PER_LONG];
39 } blk_default_cmd_filter;
41 /* Command group 3 is reserved and should never be used. */
42 const unsigned char scsi_command_size_tbl[8] =
44 6, 10, 10, 12,
45 16, 12, 10, 10
47 EXPORT_SYMBOL(scsi_command_size_tbl);
49 #include <scsi/sg.h>
51 static int sg_get_version(int __user *p)
53 static const int sg_version_num = 30527;
54 return put_user(sg_version_num, p);
57 static int scsi_get_idlun(struct request_queue *q, int __user *p)
59 return put_user(0, p);
62 static int scsi_get_bus(struct request_queue *q, int __user *p)
64 return put_user(0, p);
67 static int sg_get_timeout(struct request_queue *q)
69 return jiffies_to_clock_t(q->sg_timeout);
72 static int sg_set_timeout(struct request_queue *q, int __user *p)
74 int timeout, err = get_user(timeout, p);
76 if (!err)
77 q->sg_timeout = clock_t_to_jiffies(timeout);
79 return err;
82 static int sg_get_reserved_size(struct request_queue *q, int __user *p)
84 unsigned val = min(q->sg_reserved_size, queue_max_sectors(q) << 9);
86 return put_user(val, p);
89 static int sg_set_reserved_size(struct request_queue *q, int __user *p)
91 int size, err = get_user(size, p);
93 if (err)
94 return err;
96 if (size < 0)
97 return -EINVAL;
98 if (size > (queue_max_sectors(q) << 9))
99 size = queue_max_sectors(q) << 9;
101 q->sg_reserved_size = size;
102 return 0;
106 * will always return that we are ATAPI even for a real SCSI drive, I'm not
107 * so sure this is worth doing anything about (why would you care??)
109 static int sg_emulated_host(struct request_queue *q, int __user *p)
111 return put_user(1, p);
114 static void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
116 /* Basic read-only commands */
117 __set_bit(TEST_UNIT_READY, filter->read_ok);
118 __set_bit(REQUEST_SENSE, filter->read_ok);
119 __set_bit(READ_6, filter->read_ok);
120 __set_bit(READ_10, filter->read_ok);
121 __set_bit(READ_12, filter->read_ok);
122 __set_bit(READ_16, filter->read_ok);
123 __set_bit(READ_BUFFER, filter->read_ok);
124 __set_bit(READ_DEFECT_DATA, filter->read_ok);
125 __set_bit(READ_CAPACITY, filter->read_ok);
126 __set_bit(READ_LONG, filter->read_ok);
127 __set_bit(INQUIRY, filter->read_ok);
128 __set_bit(MODE_SENSE, filter->read_ok);
129 __set_bit(MODE_SENSE_10, filter->read_ok);
130 __set_bit(LOG_SENSE, filter->read_ok);
131 __set_bit(START_STOP, filter->read_ok);
132 __set_bit(GPCMD_VERIFY_10, filter->read_ok);
133 __set_bit(VERIFY_16, filter->read_ok);
134 __set_bit(REPORT_LUNS, filter->read_ok);
135 __set_bit(SERVICE_ACTION_IN, filter->read_ok);
136 __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
137 __set_bit(MAINTENANCE_IN, filter->read_ok);
138 __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
140 /* Audio CD commands */
141 __set_bit(GPCMD_PLAY_CD, filter->read_ok);
142 __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
143 __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
144 __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
145 __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
147 /* CD/DVD data reading */
148 __set_bit(GPCMD_READ_CD, filter->read_ok);
149 __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
150 __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
151 __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
152 __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
153 __set_bit(GPCMD_READ_HEADER, filter->read_ok);
154 __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
155 __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
156 __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
157 __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
158 __set_bit(GPCMD_SCAN, filter->read_ok);
159 __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
160 __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
161 __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
162 __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
163 __set_bit(GPCMD_SEEK, filter->read_ok);
164 __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
166 /* Basic writing commands */
167 __set_bit(WRITE_6, filter->write_ok);
168 __set_bit(WRITE_10, filter->write_ok);
169 __set_bit(WRITE_VERIFY, filter->write_ok);
170 __set_bit(WRITE_12, filter->write_ok);
171 __set_bit(WRITE_VERIFY_12, filter->write_ok);
172 __set_bit(WRITE_16, filter->write_ok);
173 __set_bit(WRITE_LONG, filter->write_ok);
174 __set_bit(WRITE_LONG_2, filter->write_ok);
175 __set_bit(ERASE, filter->write_ok);
176 __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
177 __set_bit(MODE_SELECT, filter->write_ok);
178 __set_bit(LOG_SELECT, filter->write_ok);
179 __set_bit(GPCMD_BLANK, filter->write_ok);
180 __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
181 __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
182 __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
183 __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
184 __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
185 __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
186 __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
187 __set_bit(GPCMD_SEND_KEY, filter->write_ok);
188 __set_bit(GPCMD_SEND_OPC, filter->write_ok);
189 __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
190 __set_bit(GPCMD_SET_SPEED, filter->write_ok);
191 __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
192 __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
193 __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
194 __set_bit(GPCMD_SET_READ_AHEAD, filter->write_ok);
197 int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm)
199 struct blk_cmd_filter *filter = &blk_default_cmd_filter;
201 /* root can do any command. */
202 if (capable(CAP_SYS_RAWIO))
203 return 0;
205 /* if there's no filter set, assume we're filtering everything out */
206 if (!filter)
207 return -EPERM;
209 /* Anybody who can open the device can do a read-safe command */
210 if (test_bit(cmd[0], filter->read_ok))
211 return 0;
213 /* Write-safe commands require a writable open */
214 if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
215 return 0;
217 return -EPERM;
219 EXPORT_SYMBOL(blk_verify_command);
221 static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
222 struct sg_io_hdr *hdr, fmode_t mode)
224 if (copy_from_user(rq->cmd, hdr->cmdp, hdr->cmd_len))
225 return -EFAULT;
226 if (blk_verify_command(rq->cmd, mode & FMODE_WRITE))
227 return -EPERM;
230 * fill in request structure
232 rq->cmd_len = hdr->cmd_len;
233 rq->cmd_type = REQ_TYPE_BLOCK_PC;
235 rq->timeout = msecs_to_jiffies(hdr->timeout);
236 if (!rq->timeout)
237 rq->timeout = q->sg_timeout;
238 if (!rq->timeout)
239 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
240 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
241 rq->timeout = BLK_MIN_SG_TIMEOUT;
243 return 0;
246 static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
247 struct bio *bio)
249 int r, ret = 0;
252 * fill in all the output members
254 hdr->status = rq->errors & 0xff;
255 hdr->masked_status = status_byte(rq->errors);
256 hdr->msg_status = msg_byte(rq->errors);
257 hdr->host_status = host_byte(rq->errors);
258 hdr->driver_status = driver_byte(rq->errors);
259 hdr->info = 0;
260 if (hdr->masked_status || hdr->host_status || hdr->driver_status)
261 hdr->info |= SG_INFO_CHECK;
262 hdr->resid = rq->resid_len;
263 hdr->sb_len_wr = 0;
265 if (rq->sense_len && hdr->sbp) {
266 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
268 if (!copy_to_user(hdr->sbp, rq->sense, len))
269 hdr->sb_len_wr = len;
270 else
271 ret = -EFAULT;
274 r = blk_rq_unmap_user(bio);
275 if (!ret)
276 ret = r;
277 blk_put_request(rq);
279 return ret;
282 static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
283 struct sg_io_hdr *hdr, fmode_t mode)
285 unsigned long start_time;
286 int writing = 0, ret = 0;
287 struct request *rq;
288 char sense[SCSI_SENSE_BUFFERSIZE];
289 struct bio *bio;
291 if (hdr->interface_id != 'S')
292 return -EINVAL;
293 if (hdr->cmd_len > BLK_MAX_CDB)
294 return -EINVAL;
296 if (hdr->dxfer_len > (queue_max_hw_sectors(q) << 9))
297 return -EIO;
299 if (hdr->dxfer_len)
300 switch (hdr->dxfer_direction) {
301 default:
302 return -EINVAL;
303 case SG_DXFER_TO_DEV:
304 writing = 1;
305 break;
306 case SG_DXFER_TO_FROM_DEV:
307 case SG_DXFER_FROM_DEV:
308 break;
311 rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
312 if (!rq)
313 return -ENOMEM;
315 if (blk_fill_sghdr_rq(q, rq, hdr, mode)) {
316 blk_put_request(rq);
317 return -EFAULT;
320 if (hdr->iovec_count) {
321 const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
322 size_t iov_data_len;
323 struct sg_iovec *sg_iov;
324 struct iovec *iov;
325 int i;
327 sg_iov = kmalloc(size, GFP_KERNEL);
328 if (!sg_iov) {
329 ret = -ENOMEM;
330 goto out;
333 if (copy_from_user(sg_iov, hdr->dxferp, size)) {
334 kfree(sg_iov);
335 ret = -EFAULT;
336 goto out;
340 * Sum up the vecs, making sure they don't overflow
342 iov = (struct iovec *) sg_iov;
343 iov_data_len = 0;
344 for (i = 0; i < hdr->iovec_count; i++) {
345 if (iov_data_len + iov[i].iov_len < iov_data_len) {
346 kfree(sg_iov);
347 ret = -EINVAL;
348 goto out;
350 iov_data_len += iov[i].iov_len;
353 /* SG_IO howto says that the shorter of the two wins */
354 if (hdr->dxfer_len < iov_data_len) {
355 hdr->iovec_count = iov_shorten(iov,
356 hdr->iovec_count,
357 hdr->dxfer_len);
358 iov_data_len = hdr->dxfer_len;
361 ret = blk_rq_map_user_iov(q, rq, NULL, sg_iov, hdr->iovec_count,
362 iov_data_len, GFP_KERNEL);
363 kfree(sg_iov);
364 } else if (hdr->dxfer_len)
365 ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
366 GFP_KERNEL);
368 if (ret)
369 goto out;
371 bio = rq->bio;
372 memset(sense, 0, sizeof(sense));
373 rq->sense = sense;
374 rq->sense_len = 0;
375 rq->retries = 0;
377 start_time = jiffies;
379 /* ignore return value. All information is passed back to caller
380 * (if he doesn't check that is his problem).
381 * N.B. a non-zero SCSI status is _not_ necessarily an error.
383 blk_execute_rq(q, bd_disk, rq, 0);
385 hdr->duration = jiffies_to_msecs(jiffies - start_time);
387 return blk_complete_sghdr_rq(rq, hdr, bio);
388 out:
389 blk_put_request(rq);
390 return ret;
394 * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
395 * @file: file this ioctl operates on (optional)
396 * @q: request queue to send scsi commands down
397 * @disk: gendisk to operate on (option)
398 * @sic: userspace structure describing the command to perform
400 * Send down the scsi command described by @sic to the device below
401 * the request queue @q. If @file is non-NULL it's used to perform
402 * fine-grained permission checks that allow users to send down
403 * non-destructive SCSI commands. If the caller has a struct gendisk
404 * available it should be passed in as @disk to allow the low level
405 * driver to use the information contained in it. A non-NULL @disk
406 * is only allowed if the caller knows that the low level driver doesn't
407 * need it (e.g. in the scsi subsystem).
409 * Notes:
410 * - This interface is deprecated - users should use the SG_IO
411 * interface instead, as this is a more flexible approach to
412 * performing SCSI commands on a device.
413 * - The SCSI command length is determined by examining the 1st byte
414 * of the given command. There is no way to override this.
415 * - Data transfers are limited to PAGE_SIZE
416 * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
417 * accommodate the sense buffer when an error occurs.
418 * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
419 * old code will not be surprised.
420 * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
421 * a negative return and the Unix error code in 'errno'.
422 * If the SCSI command succeeds then 0 is returned.
423 * Positive numbers returned are the compacted SCSI error codes (4
424 * bytes in one int) where the lowest byte is the SCSI status.
426 #define OMAX_SB_LEN 16 /* For backward compatibility */
427 int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
428 struct scsi_ioctl_command __user *sic)
430 struct request *rq;
431 int err;
432 unsigned int in_len, out_len, bytes, opcode, cmdlen;
433 char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
435 if (!sic)
436 return -EINVAL;
439 * get in an out lengths, verify they don't exceed a page worth of data
441 if (get_user(in_len, &sic->inlen))
442 return -EFAULT;
443 if (get_user(out_len, &sic->outlen))
444 return -EFAULT;
445 if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
446 return -EINVAL;
447 if (get_user(opcode, sic->data))
448 return -EFAULT;
450 bytes = max(in_len, out_len);
451 if (bytes) {
452 buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
453 if (!buffer)
454 return -ENOMEM;
458 rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
460 cmdlen = COMMAND_SIZE(opcode);
463 * get command and data to send to device, if any
465 err = -EFAULT;
466 rq->cmd_len = cmdlen;
467 if (copy_from_user(rq->cmd, sic->data, cmdlen))
468 goto error;
470 if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
471 goto error;
473 err = blk_verify_command(rq->cmd, mode & FMODE_WRITE);
474 if (err)
475 goto error;
477 /* default. possible overriden later */
478 rq->retries = 5;
480 switch (opcode) {
481 case SEND_DIAGNOSTIC:
482 case FORMAT_UNIT:
483 rq->timeout = FORMAT_UNIT_TIMEOUT;
484 rq->retries = 1;
485 break;
486 case START_STOP:
487 rq->timeout = START_STOP_TIMEOUT;
488 break;
489 case MOVE_MEDIUM:
490 rq->timeout = MOVE_MEDIUM_TIMEOUT;
491 break;
492 case READ_ELEMENT_STATUS:
493 rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
494 break;
495 case READ_DEFECT_DATA:
496 rq->timeout = READ_DEFECT_DATA_TIMEOUT;
497 rq->retries = 1;
498 break;
499 default:
500 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
501 break;
504 if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_WAIT)) {
505 err = DRIVER_ERROR << 24;
506 goto out;
509 memset(sense, 0, sizeof(sense));
510 rq->sense = sense;
511 rq->sense_len = 0;
512 rq->cmd_type = REQ_TYPE_BLOCK_PC;
514 blk_execute_rq(q, disk, rq, 0);
516 out:
517 err = rq->errors & 0xff; /* only 8 bit SCSI status */
518 if (err) {
519 if (rq->sense_len && rq->sense) {
520 bytes = (OMAX_SB_LEN > rq->sense_len) ?
521 rq->sense_len : OMAX_SB_LEN;
522 if (copy_to_user(sic->data, rq->sense, bytes))
523 err = -EFAULT;
525 } else {
526 if (copy_to_user(sic->data, buffer, out_len))
527 err = -EFAULT;
530 error:
531 kfree(buffer);
532 blk_put_request(rq);
533 return err;
535 EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
537 /* Send basic block requests */
538 static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
539 int cmd, int data)
541 struct request *rq;
542 int err;
544 rq = blk_get_request(q, WRITE, __GFP_WAIT);
545 rq->cmd_type = REQ_TYPE_BLOCK_PC;
546 rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
547 rq->cmd[0] = cmd;
548 rq->cmd[4] = data;
549 rq->cmd_len = 6;
550 err = blk_execute_rq(q, bd_disk, rq, 0);
551 blk_put_request(rq);
553 return err;
556 static inline int blk_send_start_stop(struct request_queue *q,
557 struct gendisk *bd_disk, int data)
559 return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
562 int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mode,
563 unsigned int cmd, void __user *arg)
565 int err;
567 if (!q || blk_get_queue(q))
568 return -ENXIO;
570 switch (cmd) {
572 * new sgv3 interface
574 case SG_GET_VERSION_NUM:
575 err = sg_get_version(arg);
576 break;
577 case SCSI_IOCTL_GET_IDLUN:
578 err = scsi_get_idlun(q, arg);
579 break;
580 case SCSI_IOCTL_GET_BUS_NUMBER:
581 err = scsi_get_bus(q, arg);
582 break;
583 case SG_SET_TIMEOUT:
584 err = sg_set_timeout(q, arg);
585 break;
586 case SG_GET_TIMEOUT:
587 err = sg_get_timeout(q);
588 break;
589 case SG_GET_RESERVED_SIZE:
590 err = sg_get_reserved_size(q, arg);
591 break;
592 case SG_SET_RESERVED_SIZE:
593 err = sg_set_reserved_size(q, arg);
594 break;
595 case SG_EMULATED_HOST:
596 err = sg_emulated_host(q, arg);
597 break;
598 case SG_IO: {
599 struct sg_io_hdr hdr;
601 err = -EFAULT;
602 if (copy_from_user(&hdr, arg, sizeof(hdr)))
603 break;
604 err = sg_io(q, bd_disk, &hdr, mode);
605 if (err == -EFAULT)
606 break;
608 if (copy_to_user(arg, &hdr, sizeof(hdr)))
609 err = -EFAULT;
610 break;
612 case CDROM_SEND_PACKET: {
613 struct cdrom_generic_command cgc;
614 struct sg_io_hdr hdr;
616 err = -EFAULT;
617 if (copy_from_user(&cgc, arg, sizeof(cgc)))
618 break;
619 cgc.timeout = clock_t_to_jiffies(cgc.timeout);
620 memset(&hdr, 0, sizeof(hdr));
621 hdr.interface_id = 'S';
622 hdr.cmd_len = sizeof(cgc.cmd);
623 hdr.dxfer_len = cgc.buflen;
624 err = 0;
625 switch (cgc.data_direction) {
626 case CGC_DATA_UNKNOWN:
627 hdr.dxfer_direction = SG_DXFER_UNKNOWN;
628 break;
629 case CGC_DATA_WRITE:
630 hdr.dxfer_direction = SG_DXFER_TO_DEV;
631 break;
632 case CGC_DATA_READ:
633 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
634 break;
635 case CGC_DATA_NONE:
636 hdr.dxfer_direction = SG_DXFER_NONE;
637 break;
638 default:
639 err = -EINVAL;
641 if (err)
642 break;
644 hdr.dxferp = cgc.buffer;
645 hdr.sbp = cgc.sense;
646 if (hdr.sbp)
647 hdr.mx_sb_len = sizeof(struct request_sense);
648 hdr.timeout = jiffies_to_msecs(cgc.timeout);
649 hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
650 hdr.cmd_len = sizeof(cgc.cmd);
652 err = sg_io(q, bd_disk, &hdr, mode);
653 if (err == -EFAULT)
654 break;
656 if (hdr.status)
657 err = -EIO;
659 cgc.stat = err;
660 cgc.buflen = hdr.resid;
661 if (copy_to_user(arg, &cgc, sizeof(cgc)))
662 err = -EFAULT;
664 break;
668 * old junk scsi send command ioctl
670 case SCSI_IOCTL_SEND_COMMAND:
671 printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
672 err = -EINVAL;
673 if (!arg)
674 break;
676 err = sg_scsi_ioctl(q, bd_disk, mode, arg);
677 break;
678 case CDROMCLOSETRAY:
679 err = blk_send_start_stop(q, bd_disk, 0x03);
680 break;
681 case CDROMEJECT:
682 err = blk_send_start_stop(q, bd_disk, 0x02);
683 break;
684 default:
685 err = -ENOTTY;
688 blk_put_queue(q);
689 return err;
691 EXPORT_SYMBOL(scsi_cmd_ioctl);
693 int scsi_verify_blk_ioctl(struct block_device *bd, unsigned int cmd)
695 if (bd && bd == bd->bd_contains)
696 return 0;
698 /* Actually none of these is particularly useful on a partition,
699 * but they are safe.
701 switch (cmd) {
702 case SCSI_IOCTL_GET_IDLUN:
703 case SCSI_IOCTL_GET_BUS_NUMBER:
704 case SCSI_IOCTL_GET_PCI:
705 case SCSI_IOCTL_PROBE_HOST:
706 case SG_GET_VERSION_NUM:
707 case SG_SET_TIMEOUT:
708 case SG_GET_TIMEOUT:
709 case SG_GET_RESERVED_SIZE:
710 case SG_SET_RESERVED_SIZE:
711 case SG_EMULATED_HOST:
712 return 0;
713 case CDROM_GET_CAPABILITY:
714 /* Keep this until we remove the printk below. udev sends it
715 * and we do not want to spam dmesg about it. CD-ROMs do
716 * not have partitions, so we get here only for disks.
718 return -ENOTTY;
719 default:
720 break;
723 /* In particular, rule out all resets and host-specific ioctls. */
724 printk_ratelimited(KERN_WARNING
725 "%s: sending ioctl %x to a partition!\n", current->comm, cmd);
727 return capable(CAP_SYS_RAWIO) ? 0 : -ENOTTY;
729 EXPORT_SYMBOL(scsi_verify_blk_ioctl);
731 int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode,
732 unsigned int cmd, void __user *arg)
734 int ret;
736 ret = scsi_verify_blk_ioctl(bd, cmd);
737 if (ret < 0)
738 return ret;
740 return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg);
742 EXPORT_SYMBOL(scsi_cmd_blk_ioctl);
744 int __init blk_scsi_ioctl_init(void)
746 blk_set_cmd_filter_defaults(&blk_default_cmd_filter);
747 return 0;
749 fs_initcall(blk_scsi_ioctl_init);