scsi_ioctl.c 14 KB

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