scsi_ioctl.c 14 KB

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