Merge with /pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / block / scsi_ioctl.c
1 /*
2  * Copyright (C) 2001 Jens Axboe <axboe@suse.de>
3  *
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.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
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-
17  *
18  */
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/slab.h>
28 #include <linux/times.h>
29 #include <asm/uaccess.h>
30
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_ioctl.h>
33 #include <scsi/scsi_cmnd.h>
34
35 /* Command group 3 is reserved and should never be used.  */
36 const unsigned char scsi_command_size[8] =
37 {
38         6, 10, 10, 12,
39         16, 12, 10, 10
40 };
41
42 EXPORT_SYMBOL(scsi_command_size);
43
44 #define BLK_DEFAULT_TIMEOUT     (60 * HZ)
45
46 #include <scsi/sg.h>
47
48 static int sg_get_version(int __user *p)
49 {
50         static const int sg_version_num = 30527;
51         return put_user(sg_version_num, p);
52 }
53
54 static int scsi_get_idlun(request_queue_t *q, int __user *p)
55 {
56         return put_user(0, p);
57 }
58
59 static int scsi_get_bus(request_queue_t *q, int __user *p)
60 {
61         return put_user(0, p);
62 }
63
64 static int sg_get_timeout(request_queue_t *q)
65 {
66         return q->sg_timeout / (HZ / USER_HZ);
67 }
68
69 static int sg_set_timeout(request_queue_t *q, int __user *p)
70 {
71         int timeout, err = get_user(timeout, p);
72
73         if (!err)
74                 q->sg_timeout = timeout * (HZ / USER_HZ);
75
76         return err;
77 }
78
79 static int sg_get_reserved_size(request_queue_t *q, int __user *p)
80 {
81         return put_user(q->sg_reserved_size, p);
82 }
83
84 static int sg_set_reserved_size(request_queue_t *q, int __user *p)
85 {
86         int size, err = get_user(size, p);
87
88         if (err)
89                 return err;
90
91         if (size < 0)
92                 return -EINVAL;
93         if (size > (q->max_sectors << 9))
94                 size = q->max_sectors << 9;
95
96         q->sg_reserved_size = size;
97         return 0;
98 }
99
100 /*
101  * will always return that we are ATAPI even for a real SCSI drive, I'm not
102  * so sure this is worth doing anything about (why would you care??)
103  */
104 static int sg_emulated_host(request_queue_t *q, int __user *p)
105 {
106         return put_user(1, p);
107 }
108
109 #define CMD_READ_SAFE   0x01
110 #define CMD_WRITE_SAFE  0x02
111 #define CMD_WARNED      0x04
112 #define safe_for_read(cmd)      [cmd] = CMD_READ_SAFE
113 #define safe_for_write(cmd)     [cmd] = CMD_WRITE_SAFE
114
115 static int verify_command(struct file *file, unsigned char *cmd)
116 {
117         static unsigned char cmd_type[256] = {
118
119                 /* Basic read-only commands */
120                 safe_for_read(TEST_UNIT_READY),
121                 safe_for_read(REQUEST_SENSE),
122                 safe_for_read(READ_6),
123                 safe_for_read(READ_10),
124                 safe_for_read(READ_12),
125                 safe_for_read(READ_16),
126                 safe_for_read(READ_BUFFER),
127                 safe_for_read(READ_DEFECT_DATA),
128                 safe_for_read(READ_LONG),
129                 safe_for_read(INQUIRY),
130                 safe_for_read(MODE_SENSE),
131                 safe_for_read(MODE_SENSE_10),
132                 safe_for_read(LOG_SENSE),
133                 safe_for_read(START_STOP),
134                 safe_for_read(GPCMD_VERIFY_10),
135                 safe_for_read(VERIFY_16),
136
137                 /* Audio CD commands */
138                 safe_for_read(GPCMD_PLAY_CD),
139                 safe_for_read(GPCMD_PLAY_AUDIO_10),
140                 safe_for_read(GPCMD_PLAY_AUDIO_MSF),
141                 safe_for_read(GPCMD_PLAY_AUDIO_TI),
142                 safe_for_read(GPCMD_PAUSE_RESUME),
143
144                 /* CD/DVD data reading */
145                 safe_for_read(GPCMD_READ_BUFFER_CAPACITY),
146                 safe_for_read(GPCMD_READ_CD),
147                 safe_for_read(GPCMD_READ_CD_MSF),
148                 safe_for_read(GPCMD_READ_DISC_INFO),
149                 safe_for_read(GPCMD_READ_CDVD_CAPACITY),
150                 safe_for_read(GPCMD_READ_DVD_STRUCTURE),
151                 safe_for_read(GPCMD_READ_HEADER),
152                 safe_for_read(GPCMD_READ_TRACK_RZONE_INFO),
153                 safe_for_read(GPCMD_READ_SUBCHANNEL),
154                 safe_for_read(GPCMD_READ_TOC_PMA_ATIP),
155                 safe_for_read(GPCMD_REPORT_KEY),
156                 safe_for_read(GPCMD_SCAN),
157                 safe_for_read(GPCMD_GET_CONFIGURATION),
158                 safe_for_read(GPCMD_READ_FORMAT_CAPACITIES),
159                 safe_for_read(GPCMD_GET_EVENT_STATUS_NOTIFICATION),
160                 safe_for_read(GPCMD_GET_PERFORMANCE),
161                 safe_for_read(GPCMD_SEEK),
162                 safe_for_read(GPCMD_STOP_PLAY_SCAN),
163
164                 /* Basic writing commands */
165                 safe_for_write(WRITE_6),
166                 safe_for_write(WRITE_10),
167                 safe_for_write(WRITE_VERIFY),
168                 safe_for_write(WRITE_12),
169                 safe_for_write(WRITE_VERIFY_12),
170                 safe_for_write(WRITE_16),
171                 safe_for_write(WRITE_LONG),
172                 safe_for_write(WRITE_LONG_2),
173                 safe_for_write(ERASE),
174                 safe_for_write(GPCMD_MODE_SELECT_10),
175                 safe_for_write(MODE_SELECT),
176                 safe_for_write(LOG_SELECT),
177                 safe_for_write(GPCMD_BLANK),
178                 safe_for_write(GPCMD_CLOSE_TRACK),
179                 safe_for_write(GPCMD_FLUSH_CACHE),
180                 safe_for_write(GPCMD_FORMAT_UNIT),
181                 safe_for_write(GPCMD_REPAIR_RZONE_TRACK),
182                 safe_for_write(GPCMD_RESERVE_RZONE_TRACK),
183                 safe_for_write(GPCMD_SEND_DVD_STRUCTURE),
184                 safe_for_write(GPCMD_SEND_EVENT),
185                 safe_for_write(GPCMD_SEND_KEY),
186                 safe_for_write(GPCMD_SEND_OPC),
187                 safe_for_write(GPCMD_SEND_CUE_SHEET),
188                 safe_for_write(GPCMD_SET_SPEED),
189                 safe_for_write(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL),
190                 safe_for_write(GPCMD_LOAD_UNLOAD),
191                 safe_for_write(GPCMD_SET_STREAMING),
192         };
193         unsigned char type = cmd_type[cmd[0]];
194         int has_write_perm = 0;
195
196         /* Anybody who can open the device can do a read-safe command */
197         if (type & CMD_READ_SAFE)
198                 return 0;
199
200         /*
201          * file can be NULL from ioctl_by_bdev()...
202          */
203         if (file)
204                 has_write_perm = file->f_mode & FMODE_WRITE;
205
206         /* Write-safe commands just require a writable open.. */
207         if ((type & CMD_WRITE_SAFE) && has_write_perm)
208                 return 0;
209
210         /* And root can do any command.. */
211         if (capable(CAP_SYS_RAWIO))
212                 return 0;
213
214         if (!type) {
215                 cmd_type[cmd[0]] = CMD_WARNED;
216                 printk(KERN_WARNING "scsi: unknown opcode 0x%02x\n", cmd[0]);
217         }
218
219         /* Otherwise fail it with an "Operation not permitted" */
220         return -EPERM;
221 }
222
223 static int sg_io(struct file *file, request_queue_t *q,
224                 struct gendisk *bd_disk, struct sg_io_hdr *hdr)
225 {
226         unsigned long start_time;
227         int writing = 0, ret = 0;
228         struct request *rq;
229         struct bio *bio;
230         char sense[SCSI_SENSE_BUFFERSIZE];
231         unsigned char cmd[BLK_MAX_CDB];
232
233         if (hdr->interface_id != 'S')
234                 return -EINVAL;
235         if (hdr->cmd_len > BLK_MAX_CDB)
236                 return -EINVAL;
237         if (copy_from_user(cmd, hdr->cmdp, hdr->cmd_len))
238                 return -EFAULT;
239         if (verify_command(file, cmd))
240                 return -EPERM;
241
242         if (hdr->dxfer_len > (q->max_hw_sectors << 9))
243                 return -EIO;
244
245         if (hdr->dxfer_len)
246                 switch (hdr->dxfer_direction) {
247                 default:
248                         return -EINVAL;
249                 case SG_DXFER_TO_FROM_DEV:
250                 case SG_DXFER_TO_DEV:
251                         writing = 1;
252                         break;
253                 case SG_DXFER_FROM_DEV:
254                         break;
255                 }
256
257         rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
258         if (!rq)
259                 return -ENOMEM;
260
261         if (hdr->iovec_count) {
262                 const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
263                 struct sg_iovec *iov;
264
265                 iov = kmalloc(size, GFP_KERNEL);
266                 if (!iov) {
267                         ret = -ENOMEM;
268                         goto out;
269                 }
270
271                 if (copy_from_user(iov, hdr->dxferp, size)) {
272                         kfree(iov);
273                         ret = -EFAULT;
274                         goto out;
275                 }
276
277                 ret = blk_rq_map_user_iov(q, rq, iov, hdr->iovec_count);
278                 kfree(iov);
279         } else if (hdr->dxfer_len)
280                 ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
281
282         if (ret)
283                 goto out;
284
285         /*
286          * fill in request structure
287          */
288         rq->cmd_len = hdr->cmd_len;
289         memcpy(rq->cmd, cmd, hdr->cmd_len);
290         if (sizeof(rq->cmd) != hdr->cmd_len)
291                 memset(rq->cmd + hdr->cmd_len, 0, sizeof(rq->cmd) - hdr->cmd_len);
292
293         memset(sense, 0, sizeof(sense));
294         rq->sense = sense;
295         rq->sense_len = 0;
296
297         rq->flags |= REQ_BLOCK_PC;
298         bio = rq->bio;
299
300         /*
301          * bounce this after holding a reference to the original bio, it's
302          * needed for proper unmapping
303          */
304         if (rq->bio)
305                 blk_queue_bounce(q, &rq->bio);
306
307         rq->timeout = (hdr->timeout * HZ) / 1000;
308         if (!rq->timeout)
309                 rq->timeout = q->sg_timeout;
310         if (!rq->timeout)
311                 rq->timeout = BLK_DEFAULT_TIMEOUT;
312
313         rq->retries = 0;
314
315         start_time = jiffies;
316
317         /* ignore return value. All information is passed back to caller
318          * (if he doesn't check that is his problem).
319          * N.B. a non-zero SCSI status is _not_ necessarily an error.
320          */
321         blk_execute_rq(q, bd_disk, rq, 0);
322
323         /* write to all output members */
324         hdr->status = 0xff & rq->errors;
325         hdr->masked_status = status_byte(rq->errors);
326         hdr->msg_status = msg_byte(rq->errors);
327         hdr->host_status = host_byte(rq->errors);
328         hdr->driver_status = driver_byte(rq->errors);
329         hdr->info = 0;
330         if (hdr->masked_status || hdr->host_status || hdr->driver_status)
331                 hdr->info |= SG_INFO_CHECK;
332         hdr->resid = rq->data_len;
333         hdr->duration = ((jiffies - start_time) * 1000) / HZ;
334         hdr->sb_len_wr = 0;
335
336         if (rq->sense_len && hdr->sbp) {
337                 int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
338
339                 if (!copy_to_user(hdr->sbp, rq->sense, len))
340                         hdr->sb_len_wr = len;
341         }
342
343         if (blk_rq_unmap_user(bio, hdr->dxfer_len))
344                 ret = -EFAULT;
345
346         /* may not have succeeded, but output values written to control
347          * structure (struct sg_io_hdr).  */
348 out:
349         blk_put_request(rq);
350         return ret;
351 }
352
353 #define OMAX_SB_LEN 16          /* For backward compatibility */
354
355 static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
356                          struct gendisk *bd_disk, Scsi_Ioctl_Command __user *sic)
357 {
358         struct request *rq;
359         int err;
360         unsigned int in_len, out_len, bytes, opcode, cmdlen;
361         char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
362
363         /*
364          * get in an out lengths, verify they don't exceed a page worth of data
365          */
366         if (get_user(in_len, &sic->inlen))
367                 return -EFAULT;
368         if (get_user(out_len, &sic->outlen))
369                 return -EFAULT;
370         if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
371                 return -EINVAL;
372         if (get_user(opcode, sic->data))
373                 return -EFAULT;
374
375         bytes = max(in_len, out_len);
376         if (bytes) {
377                 buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
378                 if (!buffer)
379                         return -ENOMEM;
380
381                 memset(buffer, 0, bytes);
382         }
383
384         rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
385
386         cmdlen = COMMAND_SIZE(opcode);
387
388         /*
389          * get command and data to send to device, if any
390          */
391         err = -EFAULT;
392         rq->cmd_len = cmdlen;
393         if (copy_from_user(rq->cmd, sic->data, cmdlen))
394                 goto error;
395
396         if (copy_from_user(buffer, sic->data + cmdlen, in_len))
397                 goto error;
398
399         err = verify_command(file, rq->cmd);
400         if (err)
401                 goto error;
402
403         switch (opcode) {
404                 case SEND_DIAGNOSTIC:
405                 case FORMAT_UNIT:
406                         rq->timeout = FORMAT_UNIT_TIMEOUT;
407                         break;
408                 case START_STOP:
409                         rq->timeout = START_STOP_TIMEOUT;
410                         break;
411                 case MOVE_MEDIUM:
412                         rq->timeout = MOVE_MEDIUM_TIMEOUT;
413                         break;
414                 case READ_ELEMENT_STATUS:
415                         rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
416                         break;
417                 case READ_DEFECT_DATA:
418                         rq->timeout = READ_DEFECT_DATA_TIMEOUT;
419                         break;
420                 default:
421                         rq->timeout = BLK_DEFAULT_TIMEOUT;
422                         break;
423         }
424
425         memset(sense, 0, sizeof(sense));
426         rq->sense = sense;
427         rq->sense_len = 0;
428
429         rq->data = buffer;
430         rq->data_len = bytes;
431         rq->flags |= REQ_BLOCK_PC;
432         rq->retries = 0;
433
434         blk_execute_rq(q, bd_disk, rq, 0);
435         err = rq->errors & 0xff;        /* only 8 bit SCSI status */
436         if (err) {
437                 if (rq->sense_len && rq->sense) {
438                         bytes = (OMAX_SB_LEN > rq->sense_len) ?
439                                 rq->sense_len : OMAX_SB_LEN;
440                         if (copy_to_user(sic->data, rq->sense, bytes))
441                                 err = -EFAULT;
442                 }
443         } else {
444                 if (copy_to_user(sic->data, buffer, out_len))
445                         err = -EFAULT;
446         }
447         
448 error:
449         kfree(buffer);
450         blk_put_request(rq);
451         return err;
452 }
453
454
455 /* Send basic block requests */
456 static int __blk_send_generic(request_queue_t *q, struct gendisk *bd_disk, int cmd, int data)
457 {
458         struct request *rq;
459         int err;
460
461         rq = blk_get_request(q, WRITE, __GFP_WAIT);
462         rq->flags |= REQ_BLOCK_PC;
463         rq->data = NULL;
464         rq->data_len = 0;
465         rq->timeout = BLK_DEFAULT_TIMEOUT;
466         memset(rq->cmd, 0, sizeof(rq->cmd));
467         rq->cmd[0] = cmd;
468         rq->cmd[4] = data;
469         rq->cmd_len = 6;
470         err = blk_execute_rq(q, bd_disk, rq, 0);
471         blk_put_request(rq);
472
473         return err;
474 }
475
476 static inline int blk_send_start_stop(request_queue_t *q, struct gendisk *bd_disk, int data)
477 {
478         return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
479 }
480
481 int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
482 {
483         request_queue_t *q;
484         int err;
485
486         q = bd_disk->queue;
487         if (!q)
488                 return -ENXIO;
489
490         if (blk_get_queue(q))
491                 return -ENXIO;
492
493         switch (cmd) {
494                 /*
495                  * new sgv3 interface
496                  */
497                 case SG_GET_VERSION_NUM:
498                         err = sg_get_version(arg);
499                         break;
500                 case SCSI_IOCTL_GET_IDLUN:
501                         err = scsi_get_idlun(q, arg);
502                         break;
503                 case SCSI_IOCTL_GET_BUS_NUMBER:
504                         err = scsi_get_bus(q, arg);
505                         break;
506                 case SG_SET_TIMEOUT:
507                         err = sg_set_timeout(q, arg);
508                         break;
509                 case SG_GET_TIMEOUT:
510                         err = sg_get_timeout(q);
511                         break;
512                 case SG_GET_RESERVED_SIZE:
513                         err = sg_get_reserved_size(q, arg);
514                         break;
515                 case SG_SET_RESERVED_SIZE:
516                         err = sg_set_reserved_size(q, arg);
517                         break;
518                 case SG_EMULATED_HOST:
519                         err = sg_emulated_host(q, arg);
520                         break;
521                 case SG_IO: {
522                         struct sg_io_hdr hdr;
523
524                         err = -EFAULT;
525                         if (copy_from_user(&hdr, arg, sizeof(hdr)))
526                                 break;
527                         err = sg_io(file, q, bd_disk, &hdr);
528                         if (err == -EFAULT)
529                                 break;
530
531                         if (copy_to_user(arg, &hdr, sizeof(hdr)))
532                                 err = -EFAULT;
533                         break;
534                 }
535                 case CDROM_SEND_PACKET: {
536                         struct cdrom_generic_command cgc;
537                         struct sg_io_hdr hdr;
538
539                         err = -EFAULT;
540                         if (copy_from_user(&cgc, arg, sizeof(cgc)))
541                                 break;
542                         cgc.timeout = clock_t_to_jiffies(cgc.timeout);
543                         memset(&hdr, 0, sizeof(hdr));
544                         hdr.interface_id = 'S';
545                         hdr.cmd_len = sizeof(cgc.cmd);
546                         hdr.dxfer_len = cgc.buflen;
547                         err = 0;
548                         switch (cgc.data_direction) {
549                                 case CGC_DATA_UNKNOWN:
550                                         hdr.dxfer_direction = SG_DXFER_UNKNOWN;
551                                         break;
552                                 case CGC_DATA_WRITE:
553                                         hdr.dxfer_direction = SG_DXFER_TO_DEV;
554                                         break;
555                                 case CGC_DATA_READ:
556                                         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
557                                         break;
558                                 case CGC_DATA_NONE:
559                                         hdr.dxfer_direction = SG_DXFER_NONE;
560                                         break;
561                                 default:
562                                         err = -EINVAL;
563                         }
564                         if (err)
565                                 break;
566
567                         hdr.dxferp = cgc.buffer;
568                         hdr.sbp = cgc.sense;
569                         if (hdr.sbp)
570                                 hdr.mx_sb_len = sizeof(struct request_sense);
571                         hdr.timeout = cgc.timeout;
572                         hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
573                         hdr.cmd_len = sizeof(cgc.cmd);
574
575                         err = sg_io(file, q, bd_disk, &hdr);
576                         if (err == -EFAULT)
577                                 break;
578
579                         if (hdr.status)
580                                 err = -EIO;
581
582                         cgc.stat = err;
583                         cgc.buflen = hdr.resid;
584                         if (copy_to_user(arg, &cgc, sizeof(cgc)))
585                                 err = -EFAULT;
586
587                         break;
588                 }
589
590                 /*
591                  * old junk scsi send command ioctl
592                  */
593                 case SCSI_IOCTL_SEND_COMMAND:
594                         printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
595                         err = -EINVAL;
596                         if (!arg)
597                                 break;
598
599                         err = sg_scsi_ioctl(file, q, bd_disk, arg);
600                         break;
601                 case CDROMCLOSETRAY:
602                         err = blk_send_start_stop(q, bd_disk, 0x03);
603                         break;
604                 case CDROMEJECT:
605                         err = blk_send_start_stop(q, bd_disk, 0x02);
606                         break;
607                 default:
608                         err = -ENOTTY;
609         }
610
611         blk_put_queue(q);
612         return err;
613 }
614
615 EXPORT_SYMBOL(scsi_cmd_ioctl);