scsi_ioctl.c 14 KB

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