scsi_ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 reading, writing;
  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. /*
  203. * we'll do that later
  204. */
  205. if (hdr->iovec_count)
  206. return -EOPNOTSUPP;
  207. if (hdr->dxfer_len > (q->max_sectors << 9))
  208. return -EIO;
  209. reading = writing = 0;
  210. if (hdr->dxfer_len) {
  211. switch (hdr->dxfer_direction) {
  212. default:
  213. return -EINVAL;
  214. case SG_DXFER_TO_FROM_DEV:
  215. reading = 1;
  216. /* fall through */
  217. case SG_DXFER_TO_DEV:
  218. writing = 1;
  219. break;
  220. case SG_DXFER_FROM_DEV:
  221. reading = 1;
  222. break;
  223. }
  224. rq = blk_rq_map_user(q, writing ? WRITE : READ, hdr->dxferp,
  225. hdr->dxfer_len);
  226. if (IS_ERR(rq))
  227. return PTR_ERR(rq);
  228. } else
  229. rq = blk_get_request(q, READ, __GFP_WAIT);
  230. /*
  231. * fill in request structure
  232. */
  233. rq->cmd_len = hdr->cmd_len;
  234. memcpy(rq->cmd, cmd, hdr->cmd_len);
  235. if (sizeof(rq->cmd) != hdr->cmd_len)
  236. memset(rq->cmd + hdr->cmd_len, 0, sizeof(rq->cmd) - hdr->cmd_len);
  237. memset(sense, 0, sizeof(sense));
  238. rq->sense = sense;
  239. rq->sense_len = 0;
  240. rq->flags |= REQ_BLOCK_PC;
  241. bio = rq->bio;
  242. /*
  243. * bounce this after holding a reference to the original bio, it's
  244. * needed for proper unmapping
  245. */
  246. if (rq->bio)
  247. blk_queue_bounce(q, &rq->bio);
  248. rq->timeout = (hdr->timeout * HZ) / 1000;
  249. if (!rq->timeout)
  250. rq->timeout = q->sg_timeout;
  251. if (!rq->timeout)
  252. rq->timeout = BLK_DEFAULT_TIMEOUT;
  253. start_time = jiffies;
  254. /* ignore return value. All information is passed back to caller
  255. * (if he doesn't check that is his problem).
  256. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  257. */
  258. blk_execute_rq(q, bd_disk, rq);
  259. /* write to all output members */
  260. hdr->status = 0xff & rq->errors;
  261. hdr->masked_status = status_byte(rq->errors);
  262. hdr->msg_status = msg_byte(rq->errors);
  263. hdr->host_status = host_byte(rq->errors);
  264. hdr->driver_status = driver_byte(rq->errors);
  265. hdr->info = 0;
  266. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  267. hdr->info |= SG_INFO_CHECK;
  268. hdr->resid = rq->data_len;
  269. hdr->duration = ((jiffies - start_time) * 1000) / HZ;
  270. hdr->sb_len_wr = 0;
  271. if (rq->sense_len && hdr->sbp) {
  272. int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
  273. if (!copy_to_user(hdr->sbp, rq->sense, len))
  274. hdr->sb_len_wr = len;
  275. }
  276. if (blk_rq_unmap_user(rq, bio, hdr->dxfer_len))
  277. return -EFAULT;
  278. /* may not have succeeded, but output values written to control
  279. * structure (struct sg_io_hdr). */
  280. return 0;
  281. }
  282. #define OMAX_SB_LEN 16 /* For backward compatibility */
  283. static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
  284. struct gendisk *bd_disk, Scsi_Ioctl_Command __user *sic)
  285. {
  286. struct request *rq;
  287. int err;
  288. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  289. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  290. /*
  291. * get in an out lengths, verify they don't exceed a page worth of data
  292. */
  293. if (get_user(in_len, &sic->inlen))
  294. return -EFAULT;
  295. if (get_user(out_len, &sic->outlen))
  296. return -EFAULT;
  297. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  298. return -EINVAL;
  299. if (get_user(opcode, sic->data))
  300. return -EFAULT;
  301. bytes = max(in_len, out_len);
  302. if (bytes) {
  303. buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  304. if (!buffer)
  305. return -ENOMEM;
  306. memset(buffer, 0, bytes);
  307. }
  308. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  309. cmdlen = COMMAND_SIZE(opcode);
  310. /*
  311. * get command and data to send to device, if any
  312. */
  313. err = -EFAULT;
  314. rq->cmd_len = cmdlen;
  315. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  316. goto error;
  317. if (copy_from_user(buffer, sic->data + cmdlen, in_len))
  318. goto error;
  319. err = verify_command(file, rq->cmd);
  320. if (err)
  321. goto error;
  322. switch (opcode) {
  323. case SEND_DIAGNOSTIC:
  324. case FORMAT_UNIT:
  325. rq->timeout = FORMAT_UNIT_TIMEOUT;
  326. break;
  327. case START_STOP:
  328. rq->timeout = START_STOP_TIMEOUT;
  329. break;
  330. case MOVE_MEDIUM:
  331. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  332. break;
  333. case READ_ELEMENT_STATUS:
  334. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  335. break;
  336. case READ_DEFECT_DATA:
  337. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  338. break;
  339. default:
  340. rq->timeout = BLK_DEFAULT_TIMEOUT;
  341. break;
  342. }
  343. memset(sense, 0, sizeof(sense));
  344. rq->sense = sense;
  345. rq->sense_len = 0;
  346. rq->data = buffer;
  347. rq->data_len = bytes;
  348. rq->flags |= REQ_BLOCK_PC;
  349. blk_execute_rq(q, bd_disk, rq);
  350. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  351. if (err) {
  352. if (rq->sense_len && rq->sense) {
  353. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  354. rq->sense_len : OMAX_SB_LEN;
  355. if (copy_to_user(sic->data, rq->sense, bytes))
  356. err = -EFAULT;
  357. }
  358. } else {
  359. if (copy_to_user(sic->data, buffer, out_len))
  360. err = -EFAULT;
  361. }
  362. error:
  363. kfree(buffer);
  364. blk_put_request(rq);
  365. return err;
  366. }
  367. int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
  368. {
  369. request_queue_t *q;
  370. struct request *rq;
  371. int close = 0, err;
  372. q = bd_disk->queue;
  373. if (!q)
  374. return -ENXIO;
  375. if (blk_get_queue(q))
  376. return -ENXIO;
  377. switch (cmd) {
  378. /*
  379. * new sgv3 interface
  380. */
  381. case SG_GET_VERSION_NUM:
  382. err = sg_get_version(arg);
  383. break;
  384. case SCSI_IOCTL_GET_IDLUN:
  385. err = scsi_get_idlun(q, arg);
  386. break;
  387. case SCSI_IOCTL_GET_BUS_NUMBER:
  388. err = scsi_get_bus(q, arg);
  389. break;
  390. case SG_SET_TIMEOUT:
  391. err = sg_set_timeout(q, arg);
  392. break;
  393. case SG_GET_TIMEOUT:
  394. err = sg_get_timeout(q);
  395. break;
  396. case SG_GET_RESERVED_SIZE:
  397. err = sg_get_reserved_size(q, arg);
  398. break;
  399. case SG_SET_RESERVED_SIZE:
  400. err = sg_set_reserved_size(q, arg);
  401. break;
  402. case SG_EMULATED_HOST:
  403. err = sg_emulated_host(q, arg);
  404. break;
  405. case SG_IO: {
  406. struct sg_io_hdr hdr;
  407. err = -EFAULT;
  408. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  409. break;
  410. err = sg_io(file, q, bd_disk, &hdr);
  411. if (err == -EFAULT)
  412. break;
  413. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  414. err = -EFAULT;
  415. break;
  416. }
  417. case CDROM_SEND_PACKET: {
  418. struct cdrom_generic_command cgc;
  419. struct sg_io_hdr hdr;
  420. err = -EFAULT;
  421. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  422. break;
  423. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  424. memset(&hdr, 0, sizeof(hdr));
  425. hdr.interface_id = 'S';
  426. hdr.cmd_len = sizeof(cgc.cmd);
  427. hdr.dxfer_len = cgc.buflen;
  428. err = 0;
  429. switch (cgc.data_direction) {
  430. case CGC_DATA_UNKNOWN:
  431. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  432. break;
  433. case CGC_DATA_WRITE:
  434. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  435. break;
  436. case CGC_DATA_READ:
  437. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  438. break;
  439. case CGC_DATA_NONE:
  440. hdr.dxfer_direction = SG_DXFER_NONE;
  441. break;
  442. default:
  443. err = -EINVAL;
  444. }
  445. if (err)
  446. break;
  447. hdr.dxferp = cgc.buffer;
  448. hdr.sbp = cgc.sense;
  449. if (hdr.sbp)
  450. hdr.mx_sb_len = sizeof(struct request_sense);
  451. hdr.timeout = cgc.timeout;
  452. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  453. hdr.cmd_len = sizeof(cgc.cmd);
  454. err = sg_io(file, q, bd_disk, &hdr);
  455. if (err == -EFAULT)
  456. break;
  457. if (hdr.status)
  458. err = -EIO;
  459. cgc.stat = err;
  460. cgc.buflen = hdr.resid;
  461. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  462. err = -EFAULT;
  463. break;
  464. }
  465. /*
  466. * old junk scsi send command ioctl
  467. */
  468. case SCSI_IOCTL_SEND_COMMAND:
  469. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  470. err = -EINVAL;
  471. if (!arg)
  472. break;
  473. err = sg_scsi_ioctl(file, q, bd_disk, arg);
  474. break;
  475. case CDROMCLOSETRAY:
  476. close = 1;
  477. case CDROMEJECT:
  478. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  479. rq->flags |= REQ_BLOCK_PC;
  480. rq->data = NULL;
  481. rq->data_len = 0;
  482. rq->timeout = BLK_DEFAULT_TIMEOUT;
  483. memset(rq->cmd, 0, sizeof(rq->cmd));
  484. rq->cmd[0] = GPCMD_START_STOP_UNIT;
  485. rq->cmd[4] = 0x02 + (close != 0);
  486. rq->cmd_len = 6;
  487. err = blk_execute_rq(q, bd_disk, rq);
  488. blk_put_request(rq);
  489. break;
  490. default:
  491. err = -ENOTTY;
  492. }
  493. blk_put_queue(q);
  494. return err;
  495. }
  496. EXPORT_SYMBOL(scsi_cmd_ioctl);