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