/drivers/block/scsi_ioctl.c

https://bitbucket.org/evzijst/gittest · C · 580 lines · 440 code · 79 blank · 61 comment · 65 complexity · a73fb616c5d0bf1fdd918329b0f5b247 MD5 · raw file

  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 FORMAT_UNIT_TIMEOUT (2 * 60 * 60 * HZ)
  283. #define START_STOP_TIMEOUT (60 * HZ)
  284. #define MOVE_MEDIUM_TIMEOUT (5 * 60 * HZ)
  285. #define READ_ELEMENT_STATUS_TIMEOUT (5 * 60 * HZ)
  286. #define READ_DEFECT_DATA_TIMEOUT (60 * HZ )
  287. #define OMAX_SB_LEN 16 /* For backward compatibility */
  288. static int sg_scsi_ioctl(struct file *file, request_queue_t *q,
  289. struct gendisk *bd_disk, Scsi_Ioctl_Command __user *sic)
  290. {
  291. struct request *rq;
  292. int err;
  293. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  294. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  295. /*
  296. * get in an out lengths, verify they don't exceed a page worth of data
  297. */
  298. if (get_user(in_len, &sic->inlen))
  299. return -EFAULT;
  300. if (get_user(out_len, &sic->outlen))
  301. return -EFAULT;
  302. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  303. return -EINVAL;
  304. if (get_user(opcode, sic->data))
  305. return -EFAULT;
  306. bytes = max(in_len, out_len);
  307. if (bytes) {
  308. buffer = kmalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  309. if (!buffer)
  310. return -ENOMEM;
  311. memset(buffer, 0, bytes);
  312. }
  313. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  314. cmdlen = COMMAND_SIZE(opcode);
  315. /*
  316. * get command and data to send to device, if any
  317. */
  318. err = -EFAULT;
  319. rq->cmd_len = cmdlen;
  320. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  321. goto error;
  322. if (copy_from_user(buffer, sic->data + cmdlen, in_len))
  323. goto error;
  324. err = verify_command(file, rq->cmd);
  325. if (err)
  326. goto error;
  327. switch (opcode) {
  328. case SEND_DIAGNOSTIC:
  329. case FORMAT_UNIT:
  330. rq->timeout = FORMAT_UNIT_TIMEOUT;
  331. break;
  332. case START_STOP:
  333. rq->timeout = START_STOP_TIMEOUT;
  334. break;
  335. case MOVE_MEDIUM:
  336. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  337. break;
  338. case READ_ELEMENT_STATUS:
  339. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  340. break;
  341. case READ_DEFECT_DATA:
  342. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  343. break;
  344. default:
  345. rq->timeout = BLK_DEFAULT_TIMEOUT;
  346. break;
  347. }
  348. memset(sense, 0, sizeof(sense));
  349. rq->sense = sense;
  350. rq->sense_len = 0;
  351. rq->data = buffer;
  352. rq->data_len = bytes;
  353. rq->flags |= REQ_BLOCK_PC;
  354. blk_execute_rq(q, bd_disk, rq);
  355. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  356. if (err) {
  357. if (rq->sense_len && rq->sense) {
  358. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  359. rq->sense_len : OMAX_SB_LEN;
  360. if (copy_to_user(sic->data, rq->sense, bytes))
  361. err = -EFAULT;
  362. }
  363. } else {
  364. if (copy_to_user(sic->data, buffer, out_len))
  365. err = -EFAULT;
  366. }
  367. error:
  368. kfree(buffer);
  369. blk_put_request(rq);
  370. return err;
  371. }
  372. int scsi_cmd_ioctl(struct file *file, struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
  373. {
  374. request_queue_t *q;
  375. struct request *rq;
  376. int close = 0, err;
  377. q = bd_disk->queue;
  378. if (!q)
  379. return -ENXIO;
  380. if (blk_get_queue(q))
  381. return -ENXIO;
  382. switch (cmd) {
  383. /*
  384. * new sgv3 interface
  385. */
  386. case SG_GET_VERSION_NUM:
  387. err = sg_get_version(arg);
  388. break;
  389. case SCSI_IOCTL_GET_IDLUN:
  390. err = scsi_get_idlun(q, arg);
  391. break;
  392. case SCSI_IOCTL_GET_BUS_NUMBER:
  393. err = scsi_get_bus(q, arg);
  394. break;
  395. case SG_SET_TIMEOUT:
  396. err = sg_set_timeout(q, arg);
  397. break;
  398. case SG_GET_TIMEOUT:
  399. err = sg_get_timeout(q);
  400. break;
  401. case SG_GET_RESERVED_SIZE:
  402. err = sg_get_reserved_size(q, arg);
  403. break;
  404. case SG_SET_RESERVED_SIZE:
  405. err = sg_set_reserved_size(q, arg);
  406. break;
  407. case SG_EMULATED_HOST:
  408. err = sg_emulated_host(q, arg);
  409. break;
  410. case SG_IO: {
  411. struct sg_io_hdr hdr;
  412. err = -EFAULT;
  413. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  414. break;
  415. err = sg_io(file, q, bd_disk, &hdr);
  416. if (err == -EFAULT)
  417. break;
  418. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  419. err = -EFAULT;
  420. break;
  421. }
  422. case CDROM_SEND_PACKET: {
  423. struct cdrom_generic_command cgc;
  424. struct sg_io_hdr hdr;
  425. err = -EFAULT;
  426. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  427. break;
  428. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  429. memset(&hdr, 0, sizeof(hdr));
  430. hdr.interface_id = 'S';
  431. hdr.cmd_len = sizeof(cgc.cmd);
  432. hdr.dxfer_len = cgc.buflen;
  433. err = 0;
  434. switch (cgc.data_direction) {
  435. case CGC_DATA_UNKNOWN:
  436. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  437. break;
  438. case CGC_DATA_WRITE:
  439. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  440. break;
  441. case CGC_DATA_READ:
  442. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  443. break;
  444. case CGC_DATA_NONE:
  445. hdr.dxfer_direction = SG_DXFER_NONE;
  446. break;
  447. default:
  448. err = -EINVAL;
  449. }
  450. if (err)
  451. break;
  452. hdr.dxferp = cgc.buffer;
  453. hdr.sbp = cgc.sense;
  454. if (hdr.sbp)
  455. hdr.mx_sb_len = sizeof(struct request_sense);
  456. hdr.timeout = cgc.timeout;
  457. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  458. hdr.cmd_len = sizeof(cgc.cmd);
  459. err = sg_io(file, q, bd_disk, &hdr);
  460. if (err == -EFAULT)
  461. break;
  462. if (hdr.status)
  463. err = -EIO;
  464. cgc.stat = err;
  465. cgc.buflen = hdr.resid;
  466. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  467. err = -EFAULT;
  468. break;
  469. }
  470. /*
  471. * old junk scsi send command ioctl
  472. */
  473. case SCSI_IOCTL_SEND_COMMAND:
  474. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  475. err = -EINVAL;
  476. if (!arg)
  477. break;
  478. err = sg_scsi_ioctl(file, q, bd_disk, arg);
  479. break;
  480. case CDROMCLOSETRAY:
  481. close = 1;
  482. case CDROMEJECT:
  483. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  484. rq->flags |= REQ_BLOCK_PC;
  485. rq->data = NULL;
  486. rq->data_len = 0;
  487. rq->timeout = BLK_DEFAULT_TIMEOUT;
  488. memset(rq->cmd, 0, sizeof(rq->cmd));
  489. rq->cmd[0] = GPCMD_START_STOP_UNIT;
  490. rq->cmd[4] = 0x02 + (close != 0);
  491. rq->cmd_len = 6;
  492. err = blk_execute_rq(q, bd_disk, rq);
  493. blk_put_request(rq);
  494. break;
  495. default:
  496. err = -ENOTTY;
  497. }
  498. blk_put_queue(q);
  499. return err;
  500. }
  501. EXPORT_SYMBOL(scsi_cmd_ioctl);