Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License version 2 as
0006  * published by the Free Software Foundation.
0007  *
0008  * This program is distributed in the hope that it will be useful,
0009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0010  *
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public Licens
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
0017  *
0018  */
0019 #include <linux/kernel.h>
0020 #include <linux/errno.h>
0021 #include <linux/string.h>
0022 #include <linux/module.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/capability.h>
0025 #include <linux/completion.h>
0026 #include <linux/cdrom.h>
0027 #include <linux/ratelimit.h>
0028 #include <linux/slab.h>
0029 #include <linux/times.h>
0030 #include <linux/uio.h>
0031 #include <linux/uaccess.h>
0032 
0033 #include <scsi/scsi.h>
0034 #include <scsi/scsi_ioctl.h>
0035 #include <scsi/scsi_cmnd.h>
0036 
0037 struct blk_cmd_filter {
0038     unsigned long read_ok[BLK_SCSI_CMD_PER_LONG];
0039     unsigned long write_ok[BLK_SCSI_CMD_PER_LONG];
0040 };
0041 
0042 static struct blk_cmd_filter blk_default_cmd_filter;
0043 
0044 /* Command group 3 is reserved and should never be used.  */
0045 const unsigned char scsi_command_size_tbl[8] =
0046 {
0047     6, 10, 10, 12,
0048     16, 12, 10, 10
0049 };
0050 EXPORT_SYMBOL(scsi_command_size_tbl);
0051 
0052 #include <scsi/sg.h>
0053 
0054 static int sg_get_version(int __user *p)
0055 {
0056     static const int sg_version_num = 30527;
0057     return put_user(sg_version_num, p);
0058 }
0059 
0060 static int scsi_get_idlun(struct request_queue *q, int __user *p)
0061 {
0062     return put_user(0, p);
0063 }
0064 
0065 static int scsi_get_bus(struct request_queue *q, int __user *p)
0066 {
0067     return put_user(0, p);
0068 }
0069 
0070 static int sg_get_timeout(struct request_queue *q)
0071 {
0072     return jiffies_to_clock_t(q->sg_timeout);
0073 }
0074 
0075 static int sg_set_timeout(struct request_queue *q, int __user *p)
0076 {
0077     int timeout, err = get_user(timeout, p);
0078 
0079     if (!err)
0080         q->sg_timeout = clock_t_to_jiffies(timeout);
0081 
0082     return err;
0083 }
0084 
0085 static int max_sectors_bytes(struct request_queue *q)
0086 {
0087     unsigned int max_sectors = queue_max_sectors(q);
0088 
0089     max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
0090 
0091     return max_sectors << 9;
0092 }
0093 
0094 static int sg_get_reserved_size(struct request_queue *q, int __user *p)
0095 {
0096     int val = min_t(int, q->sg_reserved_size, max_sectors_bytes(q));
0097 
0098     return put_user(val, p);
0099 }
0100 
0101 static int sg_set_reserved_size(struct request_queue *q, int __user *p)
0102 {
0103     int size, err = get_user(size, p);
0104 
0105     if (err)
0106         return err;
0107 
0108     if (size < 0)
0109         return -EINVAL;
0110 
0111     q->sg_reserved_size = min(size, max_sectors_bytes(q));
0112     return 0;
0113 }
0114 
0115 /*
0116  * will always return that we are ATAPI even for a real SCSI drive, I'm not
0117  * so sure this is worth doing anything about (why would you care??)
0118  */
0119 static int sg_emulated_host(struct request_queue *q, int __user *p)
0120 {
0121     return put_user(1, p);
0122 }
0123 
0124 static void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
0125 {
0126     /* Basic read-only commands */
0127     __set_bit(TEST_UNIT_READY, filter->read_ok);
0128     __set_bit(REQUEST_SENSE, filter->read_ok);
0129     __set_bit(READ_6, filter->read_ok);
0130     __set_bit(READ_10, filter->read_ok);
0131     __set_bit(READ_12, filter->read_ok);
0132     __set_bit(READ_16, filter->read_ok);
0133     __set_bit(READ_BUFFER, filter->read_ok);
0134     __set_bit(READ_DEFECT_DATA, filter->read_ok);
0135     __set_bit(READ_CAPACITY, filter->read_ok);
0136     __set_bit(READ_LONG, filter->read_ok);
0137     __set_bit(INQUIRY, filter->read_ok);
0138     __set_bit(MODE_SENSE, filter->read_ok);
0139     __set_bit(MODE_SENSE_10, filter->read_ok);
0140     __set_bit(LOG_SENSE, filter->read_ok);
0141     __set_bit(START_STOP, filter->read_ok);
0142     __set_bit(GPCMD_VERIFY_10, filter->read_ok);
0143     __set_bit(VERIFY_16, filter->read_ok);
0144     __set_bit(REPORT_LUNS, filter->read_ok);
0145     __set_bit(SERVICE_ACTION_IN_16, filter->read_ok);
0146     __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
0147     __set_bit(MAINTENANCE_IN, filter->read_ok);
0148     __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
0149 
0150     /* Audio CD commands */
0151     __set_bit(GPCMD_PLAY_CD, filter->read_ok);
0152     __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
0153     __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
0154     __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
0155     __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
0156 
0157     /* CD/DVD data reading */
0158     __set_bit(GPCMD_READ_CD, filter->read_ok);
0159     __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
0160     __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
0161     __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
0162     __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
0163     __set_bit(GPCMD_READ_HEADER, filter->read_ok);
0164     __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
0165     __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
0166     __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
0167     __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
0168     __set_bit(GPCMD_SCAN, filter->read_ok);
0169     __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
0170     __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
0171     __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
0172     __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
0173     __set_bit(GPCMD_SEEK, filter->read_ok);
0174     __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
0175 
0176     /* Basic writing commands */
0177     __set_bit(WRITE_6, filter->write_ok);
0178     __set_bit(WRITE_10, filter->write_ok);
0179     __set_bit(WRITE_VERIFY, filter->write_ok);
0180     __set_bit(WRITE_12, filter->write_ok);
0181     __set_bit(WRITE_VERIFY_12, filter->write_ok);
0182     __set_bit(WRITE_16, filter->write_ok);
0183     __set_bit(WRITE_LONG, filter->write_ok);
0184     __set_bit(WRITE_LONG_2, filter->write_ok);
0185     __set_bit(WRITE_SAME, filter->write_ok);
0186     __set_bit(WRITE_SAME_16, filter->write_ok);
0187     __set_bit(WRITE_SAME_32, filter->write_ok);
0188     __set_bit(ERASE, filter->write_ok);
0189     __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
0190     __set_bit(MODE_SELECT, filter->write_ok);
0191     __set_bit(LOG_SELECT, filter->write_ok);
0192     __set_bit(GPCMD_BLANK, filter->write_ok);
0193     __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
0194     __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
0195     __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
0196     __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
0197     __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
0198     __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
0199     __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
0200     __set_bit(GPCMD_SEND_KEY, filter->write_ok);
0201     __set_bit(GPCMD_SEND_OPC, filter->write_ok);
0202     __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
0203     __set_bit(GPCMD_SET_SPEED, filter->write_ok);
0204     __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
0205     __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
0206     __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
0207     __set_bit(GPCMD_SET_READ_AHEAD, filter->write_ok);
0208 }
0209 
0210 int blk_verify_command(unsigned char *cmd, fmode_t has_write_perm)
0211 {
0212     struct blk_cmd_filter *filter = &blk_default_cmd_filter;
0213 
0214     /* root can do any command. */
0215     if (capable(CAP_SYS_RAWIO))
0216         return 0;
0217 
0218     /* Anybody who can open the device can do a read-safe command */
0219     if (test_bit(cmd[0], filter->read_ok))
0220         return 0;
0221 
0222     /* Write-safe commands require a writable open */
0223     if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
0224         return 0;
0225 
0226     return -EPERM;
0227 }
0228 EXPORT_SYMBOL(blk_verify_command);
0229 
0230 static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
0231                  struct sg_io_hdr *hdr, fmode_t mode)
0232 {
0233     if (copy_from_user(rq->cmd, hdr->cmdp, hdr->cmd_len))
0234         return -EFAULT;
0235     if (blk_verify_command(rq->cmd, mode & FMODE_WRITE))
0236         return -EPERM;
0237 
0238     /*
0239      * fill in request structure
0240      */
0241     rq->cmd_len = hdr->cmd_len;
0242 
0243     rq->timeout = msecs_to_jiffies(hdr->timeout);
0244     if (!rq->timeout)
0245         rq->timeout = q->sg_timeout;
0246     if (!rq->timeout)
0247         rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
0248     if (rq->timeout < BLK_MIN_SG_TIMEOUT)
0249         rq->timeout = BLK_MIN_SG_TIMEOUT;
0250 
0251     return 0;
0252 }
0253 
0254 static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
0255                  struct bio *bio)
0256 {
0257     int r, ret = 0;
0258 
0259     /*
0260      * fill in all the output members
0261      */
0262     hdr->status = rq->errors & 0xff;
0263     hdr->masked_status = status_byte(rq->errors);
0264     hdr->msg_status = msg_byte(rq->errors);
0265     hdr->host_status = host_byte(rq->errors);
0266     hdr->driver_status = driver_byte(rq->errors);
0267     hdr->info = 0;
0268     if (hdr->masked_status || hdr->host_status || hdr->driver_status)
0269         hdr->info |= SG_INFO_CHECK;
0270     hdr->resid = rq->resid_len;
0271     hdr->sb_len_wr = 0;
0272 
0273     if (rq->sense_len && hdr->sbp) {
0274         int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
0275 
0276         if (!copy_to_user(hdr->sbp, rq->sense, len))
0277             hdr->sb_len_wr = len;
0278         else
0279             ret = -EFAULT;
0280     }
0281 
0282     r = blk_rq_unmap_user(bio);
0283     if (!ret)
0284         ret = r;
0285 
0286     return ret;
0287 }
0288 
0289 static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
0290         struct sg_io_hdr *hdr, fmode_t mode)
0291 {
0292     unsigned long start_time;
0293     ssize_t ret = 0;
0294     int writing = 0;
0295     int at_head = 0;
0296     struct request *rq;
0297     char sense[SCSI_SENSE_BUFFERSIZE];
0298     struct bio *bio;
0299 
0300     if (hdr->interface_id != 'S')
0301         return -EINVAL;
0302 
0303     if (hdr->dxfer_len > (queue_max_hw_sectors(q) << 9))
0304         return -EIO;
0305 
0306     if (hdr->dxfer_len)
0307         switch (hdr->dxfer_direction) {
0308         default:
0309             return -EINVAL;
0310         case SG_DXFER_TO_DEV:
0311             writing = 1;
0312             break;
0313         case SG_DXFER_TO_FROM_DEV:
0314         case SG_DXFER_FROM_DEV:
0315             break;
0316         }
0317     if (hdr->flags & SG_FLAG_Q_AT_HEAD)
0318         at_head = 1;
0319 
0320     ret = -ENOMEM;
0321     rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
0322     if (IS_ERR(rq))
0323         return PTR_ERR(rq);
0324     blk_rq_set_block_pc(rq);
0325 
0326     if (hdr->cmd_len > BLK_MAX_CDB) {
0327         rq->cmd = kzalloc(hdr->cmd_len, GFP_KERNEL);
0328         if (!rq->cmd)
0329             goto out_put_request;
0330     }
0331 
0332     ret = blk_fill_sghdr_rq(q, rq, hdr, mode);
0333     if (ret < 0)
0334         goto out_free_cdb;
0335 
0336     ret = 0;
0337     if (hdr->iovec_count) {
0338         struct iov_iter i;
0339         struct iovec *iov = NULL;
0340 
0341         ret = import_iovec(rq_data_dir(rq),
0342                    hdr->dxferp, hdr->iovec_count,
0343                    0, &iov, &i);
0344         if (ret < 0)
0345             goto out_free_cdb;
0346 
0347         /* SG_IO howto says that the shorter of the two wins */
0348         iov_iter_truncate(&i, hdr->dxfer_len);
0349 
0350         ret = blk_rq_map_user_iov(q, rq, NULL, &i, GFP_KERNEL);
0351         kfree(iov);
0352     } else if (hdr->dxfer_len)
0353         ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
0354                       GFP_KERNEL);
0355 
0356     if (ret)
0357         goto out_free_cdb;
0358 
0359     bio = rq->bio;
0360     memset(sense, 0, sizeof(sense));
0361     rq->sense = sense;
0362     rq->sense_len = 0;
0363     rq->retries = 0;
0364 
0365     start_time = jiffies;
0366 
0367     /* ignore return value. All information is passed back to caller
0368      * (if he doesn't check that is his problem).
0369      * N.B. a non-zero SCSI status is _not_ necessarily an error.
0370      */
0371     blk_execute_rq(q, bd_disk, rq, at_head);
0372 
0373     hdr->duration = jiffies_to_msecs(jiffies - start_time);
0374 
0375     ret = blk_complete_sghdr_rq(rq, hdr, bio);
0376 
0377 out_free_cdb:
0378     if (rq->cmd != rq->__cmd)
0379         kfree(rq->cmd);
0380 out_put_request:
0381     blk_put_request(rq);
0382     return ret;
0383 }
0384 
0385 /**
0386  * sg_scsi_ioctl  --  handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
0387  * @file:   file this ioctl operates on (optional)
0388  * @q:      request queue to send scsi commands down
0389  * @disk:   gendisk to operate on (option)
0390  * @sic:    userspace structure describing the command to perform
0391  *
0392  * Send down the scsi command described by @sic to the device below
0393  * the request queue @q.  If @file is non-NULL it's used to perform
0394  * fine-grained permission checks that allow users to send down
0395  * non-destructive SCSI commands.  If the caller has a struct gendisk
0396  * available it should be passed in as @disk to allow the low level
0397  * driver to use the information contained in it.  A non-NULL @disk
0398  * is only allowed if the caller knows that the low level driver doesn't
0399  * need it (e.g. in the scsi subsystem).
0400  *
0401  * Notes:
0402  *   -  This interface is deprecated - users should use the SG_IO
0403  *      interface instead, as this is a more flexible approach to
0404  *      performing SCSI commands on a device.
0405  *   -  The SCSI command length is determined by examining the 1st byte
0406  *      of the given command. There is no way to override this.
0407  *   -  Data transfers are limited to PAGE_SIZE
0408  *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
0409  *      accommodate the sense buffer when an error occurs.
0410  *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
0411  *      old code will not be surprised.
0412  *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
0413  *      a negative return and the Unix error code in 'errno'.
0414  *      If the SCSI command succeeds then 0 is returned.
0415  *      Positive numbers returned are the compacted SCSI error codes (4
0416  *      bytes in one int) where the lowest byte is the SCSI status.
0417  */
0418 #define OMAX_SB_LEN 16          /* For backward compatibility */
0419 int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
0420         struct scsi_ioctl_command __user *sic)
0421 {
0422     struct request *rq;
0423     int err;
0424     unsigned int in_len, out_len, bytes, opcode, cmdlen;
0425     char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
0426 
0427     if (!sic)
0428         return -EINVAL;
0429 
0430     /*
0431      * get in an out lengths, verify they don't exceed a page worth of data
0432      */
0433     if (get_user(in_len, &sic->inlen))
0434         return -EFAULT;
0435     if (get_user(out_len, &sic->outlen))
0436         return -EFAULT;
0437     if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
0438         return -EINVAL;
0439     if (get_user(opcode, sic->data))
0440         return -EFAULT;
0441 
0442     bytes = max(in_len, out_len);
0443     if (bytes) {
0444         buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
0445         if (!buffer)
0446             return -ENOMEM;
0447 
0448     }
0449 
0450     rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_RECLAIM);
0451     if (IS_ERR(rq)) {
0452         err = PTR_ERR(rq);
0453         goto error_free_buffer;
0454     }
0455     blk_rq_set_block_pc(rq);
0456 
0457     cmdlen = COMMAND_SIZE(opcode);
0458 
0459     /*
0460      * get command and data to send to device, if any
0461      */
0462     err = -EFAULT;
0463     rq->cmd_len = cmdlen;
0464     if (copy_from_user(rq->cmd, sic->data, cmdlen))
0465         goto error;
0466 
0467     if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
0468         goto error;
0469 
0470     err = blk_verify_command(rq->cmd, mode & FMODE_WRITE);
0471     if (err)
0472         goto error;
0473 
0474     /* default.  possible overriden later */
0475     rq->retries = 5;
0476 
0477     switch (opcode) {
0478     case SEND_DIAGNOSTIC:
0479     case FORMAT_UNIT:
0480         rq->timeout = FORMAT_UNIT_TIMEOUT;
0481         rq->retries = 1;
0482         break;
0483     case START_STOP:
0484         rq->timeout = START_STOP_TIMEOUT;
0485         break;
0486     case MOVE_MEDIUM:
0487         rq->timeout = MOVE_MEDIUM_TIMEOUT;
0488         break;
0489     case READ_ELEMENT_STATUS:
0490         rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
0491         break;
0492     case READ_DEFECT_DATA:
0493         rq->timeout = READ_DEFECT_DATA_TIMEOUT;
0494         rq->retries = 1;
0495         break;
0496     default:
0497         rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
0498         break;
0499     }
0500 
0501     if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_RECLAIM)) {
0502         err = DRIVER_ERROR << 24;
0503         goto error;
0504     }
0505 
0506     memset(sense, 0, sizeof(sense));
0507     rq->sense = sense;
0508     rq->sense_len = 0;
0509 
0510     blk_execute_rq(q, disk, rq, 0);
0511 
0512     err = rq->errors & 0xff;    /* only 8 bit SCSI status */
0513     if (err) {
0514         if (rq->sense_len && rq->sense) {
0515             bytes = (OMAX_SB_LEN > rq->sense_len) ?
0516                 rq->sense_len : OMAX_SB_LEN;
0517             if (copy_to_user(sic->data, rq->sense, bytes))
0518                 err = -EFAULT;
0519         }
0520     } else {
0521         if (copy_to_user(sic->data, buffer, out_len))
0522             err = -EFAULT;
0523     }
0524     
0525 error:
0526     blk_put_request(rq);
0527 
0528 error_free_buffer:
0529     kfree(buffer);
0530 
0531     return err;
0532 }
0533 EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
0534 
0535 /* Send basic block requests */
0536 static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
0537                   int cmd, int data)
0538 {
0539     struct request *rq;
0540     int err;
0541 
0542     rq = blk_get_request(q, WRITE, __GFP_RECLAIM);
0543     if (IS_ERR(rq))
0544         return PTR_ERR(rq);
0545     blk_rq_set_block_pc(rq);
0546     rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
0547     rq->cmd[0] = cmd;
0548     rq->cmd[4] = data;
0549     rq->cmd_len = 6;
0550     err = blk_execute_rq(q, bd_disk, rq, 0);
0551     blk_put_request(rq);
0552 
0553     return err;
0554 }
0555 
0556 static inline int blk_send_start_stop(struct request_queue *q,
0557                       struct gendisk *bd_disk, int data)
0558 {
0559     return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
0560 }
0561 
0562 int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mode,
0563            unsigned int cmd, void __user *arg)
0564 {
0565     int err;
0566 
0567     if (!q)
0568         return -ENXIO;
0569 
0570     switch (cmd) {
0571         /*
0572          * new sgv3 interface
0573          */
0574         case SG_GET_VERSION_NUM:
0575             err = sg_get_version(arg);
0576             break;
0577         case SCSI_IOCTL_GET_IDLUN:
0578             err = scsi_get_idlun(q, arg);
0579             break;
0580         case SCSI_IOCTL_GET_BUS_NUMBER:
0581             err = scsi_get_bus(q, arg);
0582             break;
0583         case SG_SET_TIMEOUT:
0584             err = sg_set_timeout(q, arg);
0585             break;
0586         case SG_GET_TIMEOUT:
0587             err = sg_get_timeout(q);
0588             break;
0589         case SG_GET_RESERVED_SIZE:
0590             err = sg_get_reserved_size(q, arg);
0591             break;
0592         case SG_SET_RESERVED_SIZE:
0593             err = sg_set_reserved_size(q, arg);
0594             break;
0595         case SG_EMULATED_HOST:
0596             err = sg_emulated_host(q, arg);
0597             break;
0598         case SG_IO: {
0599             struct sg_io_hdr hdr;
0600 
0601             err = -EFAULT;
0602             if (copy_from_user(&hdr, arg, sizeof(hdr)))
0603                 break;
0604             err = sg_io(q, bd_disk, &hdr, mode);
0605             if (err == -EFAULT)
0606                 break;
0607 
0608             if (copy_to_user(arg, &hdr, sizeof(hdr)))
0609                 err = -EFAULT;
0610             break;
0611         }
0612         case CDROM_SEND_PACKET: {
0613             struct cdrom_generic_command cgc;
0614             struct sg_io_hdr hdr;
0615 
0616             err = -EFAULT;
0617             if (copy_from_user(&cgc, arg, sizeof(cgc)))
0618                 break;
0619             cgc.timeout = clock_t_to_jiffies(cgc.timeout);
0620             memset(&hdr, 0, sizeof(hdr));
0621             hdr.interface_id = 'S';
0622             hdr.cmd_len = sizeof(cgc.cmd);
0623             hdr.dxfer_len = cgc.buflen;
0624             err = 0;
0625             switch (cgc.data_direction) {
0626                 case CGC_DATA_UNKNOWN:
0627                     hdr.dxfer_direction = SG_DXFER_UNKNOWN;
0628                     break;
0629                 case CGC_DATA_WRITE:
0630                     hdr.dxfer_direction = SG_DXFER_TO_DEV;
0631                     break;
0632                 case CGC_DATA_READ:
0633                     hdr.dxfer_direction = SG_DXFER_FROM_DEV;
0634                     break;
0635                 case CGC_DATA_NONE:
0636                     hdr.dxfer_direction = SG_DXFER_NONE;
0637                     break;
0638                 default:
0639                     err = -EINVAL;
0640             }
0641             if (err)
0642                 break;
0643 
0644             hdr.dxferp = cgc.buffer;
0645             hdr.sbp = cgc.sense;
0646             if (hdr.sbp)
0647                 hdr.mx_sb_len = sizeof(struct request_sense);
0648             hdr.timeout = jiffies_to_msecs(cgc.timeout);
0649             hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
0650             hdr.cmd_len = sizeof(cgc.cmd);
0651 
0652             err = sg_io(q, bd_disk, &hdr, mode);
0653             if (err == -EFAULT)
0654                 break;
0655 
0656             if (hdr.status)
0657                 err = -EIO;
0658 
0659             cgc.stat = err;
0660             cgc.buflen = hdr.resid;
0661             if (copy_to_user(arg, &cgc, sizeof(cgc)))
0662                 err = -EFAULT;
0663 
0664             break;
0665         }
0666 
0667         /*
0668          * old junk scsi send command ioctl
0669          */
0670         case SCSI_IOCTL_SEND_COMMAND:
0671             printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
0672             err = -EINVAL;
0673             if (!arg)
0674                 break;
0675 
0676             err = sg_scsi_ioctl(q, bd_disk, mode, arg);
0677             break;
0678         case CDROMCLOSETRAY:
0679             err = blk_send_start_stop(q, bd_disk, 0x03);
0680             break;
0681         case CDROMEJECT:
0682             err = blk_send_start_stop(q, bd_disk, 0x02);
0683             break;
0684         default:
0685             err = -ENOTTY;
0686     }
0687 
0688     return err;
0689 }
0690 EXPORT_SYMBOL(scsi_cmd_ioctl);
0691 
0692 int scsi_verify_blk_ioctl(struct block_device *bd, unsigned int cmd)
0693 {
0694     if (bd && bd == bd->bd_contains)
0695         return 0;
0696 
0697     /* Actually none of these is particularly useful on a partition,
0698      * but they are safe.
0699      */
0700     switch (cmd) {
0701     case SCSI_IOCTL_GET_IDLUN:
0702     case SCSI_IOCTL_GET_BUS_NUMBER:
0703     case SCSI_IOCTL_GET_PCI:
0704     case SCSI_IOCTL_PROBE_HOST:
0705     case SG_GET_VERSION_NUM:
0706     case SG_SET_TIMEOUT:
0707     case SG_GET_TIMEOUT:
0708     case SG_GET_RESERVED_SIZE:
0709     case SG_SET_RESERVED_SIZE:
0710     case SG_EMULATED_HOST:
0711         return 0;
0712     case CDROM_GET_CAPABILITY:
0713         /* Keep this until we remove the printk below.  udev sends it
0714          * and we do not want to spam dmesg about it.   CD-ROMs do
0715          * not have partitions, so we get here only for disks.
0716          */
0717         return -ENOIOCTLCMD;
0718     default:
0719         break;
0720     }
0721 
0722     if (capable(CAP_SYS_RAWIO))
0723         return 0;
0724 
0725     /* In particular, rule out all resets and host-specific ioctls.  */
0726     printk_ratelimited(KERN_WARNING
0727                "%s: sending ioctl %x to a partition!\n", current->comm, cmd);
0728 
0729     return -ENOIOCTLCMD;
0730 }
0731 EXPORT_SYMBOL(scsi_verify_blk_ioctl);
0732 
0733 int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode,
0734                unsigned int cmd, void __user *arg)
0735 {
0736     int ret;
0737 
0738     ret = scsi_verify_blk_ioctl(bd, cmd);
0739     if (ret < 0)
0740         return ret;
0741 
0742     return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg);
0743 }
0744 EXPORT_SYMBOL(scsi_cmd_blk_ioctl);
0745 
0746 static int __init blk_scsi_ioctl_init(void)
0747 {
0748     blk_set_cmd_filter_defaults(&blk_default_cmd_filter);
0749     return 0;
0750 }
0751 fs_initcall(blk_scsi_ioctl_init);