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