scsi_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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_tbl[8] =
  35. {
  36. 6, 10, 10, 12,
  37. 16, 12, 10, 10
  38. };
  39. EXPORT_SYMBOL(scsi_command_size_tbl);
  40. #include <scsi/sg.h>
  41. static int sg_get_version(int __user *p)
  42. {
  43. static const int sg_version_num = 30527;
  44. return put_user(sg_version_num, p);
  45. }
  46. static int scsi_get_idlun(struct request_queue *q, int __user *p)
  47. {
  48. return put_user(0, p);
  49. }
  50. static int scsi_get_bus(struct request_queue *q, int __user *p)
  51. {
  52. return put_user(0, p);
  53. }
  54. static int sg_get_timeout(struct request_queue *q)
  55. {
  56. return q->sg_timeout / (HZ / USER_HZ);
  57. }
  58. static int sg_set_timeout(struct request_queue *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(struct request_queue *q, int __user *p)
  66. {
  67. unsigned val = min(q->sg_reserved_size, q->max_sectors << 9);
  68. return put_user(val, p);
  69. }
  70. static int sg_set_reserved_size(struct request_queue *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(struct request_queue *q, int __user *p)
  87. {
  88. return put_user(1, p);
  89. }
  90. static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
  91. struct sg_io_hdr *hdr, struct file *file)
  92. {
  93. if (copy_from_user(rq->cmd, hdr->cmdp, hdr->cmd_len))
  94. return -EFAULT;
  95. if (blk_verify_command(file, rq->cmd))
  96. return -EPERM;
  97. /*
  98. * fill in request structure
  99. */
  100. rq->cmd_len = hdr->cmd_len;
  101. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  102. rq->timeout = msecs_to_jiffies(hdr->timeout);
  103. if (!rq->timeout)
  104. rq->timeout = q->sg_timeout;
  105. if (!rq->timeout)
  106. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  107. return 0;
  108. }
  109. /*
  110. * unmap a request that was previously mapped to this sg_io_hdr. handles
  111. * both sg and non-sg sg_io_hdr.
  112. */
  113. static int blk_unmap_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr)
  114. {
  115. blk_rq_unmap_user(rq->bio);
  116. blk_put_request(rq);
  117. return 0;
  118. }
  119. static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
  120. struct bio *bio)
  121. {
  122. int r, ret = 0;
  123. /*
  124. * fill in all the output members
  125. */
  126. hdr->status = rq->errors & 0xff;
  127. hdr->masked_status = status_byte(rq->errors);
  128. hdr->msg_status = msg_byte(rq->errors);
  129. hdr->host_status = host_byte(rq->errors);
  130. hdr->driver_status = driver_byte(rq->errors);
  131. hdr->info = 0;
  132. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  133. hdr->info |= SG_INFO_CHECK;
  134. hdr->resid = rq->data_len;
  135. hdr->sb_len_wr = 0;
  136. if (rq->sense_len && hdr->sbp) {
  137. int len = min((unsigned int) hdr->mx_sb_len, rq->sense_len);
  138. if (!copy_to_user(hdr->sbp, rq->sense, len))
  139. hdr->sb_len_wr = len;
  140. else
  141. ret = -EFAULT;
  142. }
  143. rq->bio = bio;
  144. r = blk_unmap_sghdr_rq(rq, hdr);
  145. if (ret)
  146. r = ret;
  147. return r;
  148. }
  149. static int sg_io(struct file *file, struct request_queue *q,
  150. struct gendisk *bd_disk, struct sg_io_hdr *hdr)
  151. {
  152. unsigned long start_time;
  153. int writing = 0, ret = 0;
  154. struct request *rq;
  155. char sense[SCSI_SENSE_BUFFERSIZE];
  156. struct bio *bio;
  157. if (hdr->interface_id != 'S')
  158. return -EINVAL;
  159. if (hdr->cmd_len > BLK_MAX_CDB)
  160. return -EINVAL;
  161. if (hdr->dxfer_len > (q->max_hw_sectors << 9))
  162. return -EIO;
  163. if (hdr->dxfer_len)
  164. switch (hdr->dxfer_direction) {
  165. default:
  166. return -EINVAL;
  167. case SG_DXFER_TO_DEV:
  168. writing = 1;
  169. break;
  170. case SG_DXFER_TO_FROM_DEV:
  171. case SG_DXFER_FROM_DEV:
  172. break;
  173. }
  174. rq = blk_get_request(q, writing ? WRITE : READ, GFP_KERNEL);
  175. if (!rq)
  176. return -ENOMEM;
  177. if (blk_fill_sghdr_rq(q, rq, hdr, file)) {
  178. blk_put_request(rq);
  179. return -EFAULT;
  180. }
  181. if (hdr->iovec_count) {
  182. const int size = sizeof(struct sg_iovec) * hdr->iovec_count;
  183. struct sg_iovec *iov;
  184. iov = kmalloc(size, GFP_KERNEL);
  185. if (!iov) {
  186. ret = -ENOMEM;
  187. goto out;
  188. }
  189. if (copy_from_user(iov, hdr->dxferp, size)) {
  190. kfree(iov);
  191. ret = -EFAULT;
  192. goto out;
  193. }
  194. ret = blk_rq_map_user_iov(q, rq, iov, hdr->iovec_count,
  195. hdr->dxfer_len);
  196. kfree(iov);
  197. } else if (hdr->dxfer_len)
  198. ret = blk_rq_map_user(q, rq, hdr->dxferp, hdr->dxfer_len);
  199. if (ret)
  200. goto out;
  201. bio = rq->bio;
  202. memset(sense, 0, sizeof(sense));
  203. rq->sense = sense;
  204. rq->sense_len = 0;
  205. rq->retries = 0;
  206. start_time = jiffies;
  207. /* ignore return value. All information is passed back to caller
  208. * (if he doesn't check that is his problem).
  209. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  210. */
  211. blk_execute_rq(q, bd_disk, rq, 0);
  212. hdr->duration = jiffies_to_msecs(jiffies - start_time);
  213. return blk_complete_sghdr_rq(rq, hdr, bio);
  214. out:
  215. blk_put_request(rq);
  216. return ret;
  217. }
  218. /**
  219. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  220. * @file: file this ioctl operates on (optional)
  221. * @q: request queue to send scsi commands down
  222. * @disk: gendisk to operate on (option)
  223. * @sic: userspace structure describing the command to perform
  224. *
  225. * Send down the scsi command described by @sic to the device below
  226. * the request queue @q. If @file is non-NULL it's used to perform
  227. * fine-grained permission checks that allow users to send down
  228. * non-destructive SCSI commands. If the caller has a struct gendisk
  229. * available it should be passed in as @disk to allow the low level
  230. * driver to use the information contained in it. A non-NULL @disk
  231. * is only allowed if the caller knows that the low level driver doesn't
  232. * need it (e.g. in the scsi subsystem).
  233. *
  234. * Notes:
  235. * - This interface is deprecated - users should use the SG_IO
  236. * interface instead, as this is a more flexible approach to
  237. * performing SCSI commands on a device.
  238. * - The SCSI command length is determined by examining the 1st byte
  239. * of the given command. There is no way to override this.
  240. * - Data transfers are limited to PAGE_SIZE
  241. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  242. * accommodate the sense buffer when an error occurs.
  243. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  244. * old code will not be surprised.
  245. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  246. * a negative return and the Unix error code in 'errno'.
  247. * If the SCSI command succeeds then 0 is returned.
  248. * Positive numbers returned are the compacted SCSI error codes (4
  249. * bytes in one int) where the lowest byte is the SCSI status.
  250. */
  251. #define OMAX_SB_LEN 16 /* For backward compatibility */
  252. int sg_scsi_ioctl(struct file *file, struct request_queue *q,
  253. struct gendisk *disk, struct scsi_ioctl_command __user *sic)
  254. {
  255. struct request *rq;
  256. int err;
  257. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  258. char *buffer = NULL, sense[SCSI_SENSE_BUFFERSIZE];
  259. if (!sic)
  260. return -EINVAL;
  261. /*
  262. * get in an out lengths, verify they don't exceed a page worth of data
  263. */
  264. if (get_user(in_len, &sic->inlen))
  265. return -EFAULT;
  266. if (get_user(out_len, &sic->outlen))
  267. return -EFAULT;
  268. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  269. return -EINVAL;
  270. if (get_user(opcode, sic->data))
  271. return -EFAULT;
  272. bytes = max(in_len, out_len);
  273. if (bytes) {
  274. buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  275. if (!buffer)
  276. return -ENOMEM;
  277. }
  278. rq = blk_get_request(q, in_len ? WRITE : READ, __GFP_WAIT);
  279. cmdlen = COMMAND_SIZE(opcode);
  280. /*
  281. * get command and data to send to device, if any
  282. */
  283. err = -EFAULT;
  284. rq->cmd_len = cmdlen;
  285. if (copy_from_user(rq->cmd, sic->data, cmdlen))
  286. goto error;
  287. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  288. goto error;
  289. err = blk_verify_command(file, rq->cmd);
  290. if (err)
  291. goto error;
  292. /* default. possible overriden later */
  293. rq->retries = 5;
  294. switch (opcode) {
  295. case SEND_DIAGNOSTIC:
  296. case FORMAT_UNIT:
  297. rq->timeout = FORMAT_UNIT_TIMEOUT;
  298. rq->retries = 1;
  299. break;
  300. case START_STOP:
  301. rq->timeout = START_STOP_TIMEOUT;
  302. break;
  303. case MOVE_MEDIUM:
  304. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  305. break;
  306. case READ_ELEMENT_STATUS:
  307. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  308. break;
  309. case READ_DEFECT_DATA:
  310. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  311. rq->retries = 1;
  312. break;
  313. default:
  314. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  315. break;
  316. }
  317. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, __GFP_WAIT)) {
  318. err = DRIVER_ERROR << 24;
  319. goto out;
  320. }
  321. memset(sense, 0, sizeof(sense));
  322. rq->sense = sense;
  323. rq->sense_len = 0;
  324. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  325. blk_execute_rq(q, disk, rq, 0);
  326. out:
  327. err = rq->errors & 0xff; /* only 8 bit SCSI status */
  328. if (err) {
  329. if (rq->sense_len && rq->sense) {
  330. bytes = (OMAX_SB_LEN > rq->sense_len) ?
  331. rq->sense_len : OMAX_SB_LEN;
  332. if (copy_to_user(sic->data, rq->sense, bytes))
  333. err = -EFAULT;
  334. }
  335. } else {
  336. if (copy_to_user(sic->data, buffer, out_len))
  337. err = -EFAULT;
  338. }
  339. error:
  340. kfree(buffer);
  341. blk_put_request(rq);
  342. return err;
  343. }
  344. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  345. /* Send basic block requests */
  346. static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
  347. int cmd, int data)
  348. {
  349. struct request *rq;
  350. int err;
  351. rq = blk_get_request(q, WRITE, __GFP_WAIT);
  352. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  353. rq->data = NULL;
  354. rq->data_len = 0;
  355. rq->extra_len = 0;
  356. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  357. rq->cmd[0] = cmd;
  358. rq->cmd[4] = data;
  359. rq->cmd_len = 6;
  360. err = blk_execute_rq(q, bd_disk, rq, 0);
  361. blk_put_request(rq);
  362. return err;
  363. }
  364. static inline int blk_send_start_stop(struct request_queue *q,
  365. struct gendisk *bd_disk, int data)
  366. {
  367. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  368. }
  369. int scsi_cmd_ioctl(struct file *file, struct request_queue *q,
  370. struct gendisk *bd_disk, unsigned int cmd, void __user *arg)
  371. {
  372. int err;
  373. if (!q || blk_get_queue(q))
  374. return -ENXIO;
  375. switch (cmd) {
  376. /*
  377. * new sgv3 interface
  378. */
  379. case SG_GET_VERSION_NUM:
  380. err = sg_get_version(arg);
  381. break;
  382. case SCSI_IOCTL_GET_IDLUN:
  383. err = scsi_get_idlun(q, arg);
  384. break;
  385. case SCSI_IOCTL_GET_BUS_NUMBER:
  386. err = scsi_get_bus(q, arg);
  387. break;
  388. case SG_SET_TIMEOUT:
  389. err = sg_set_timeout(q, arg);
  390. break;
  391. case SG_GET_TIMEOUT:
  392. err = sg_get_timeout(q);
  393. break;
  394. case SG_GET_RESERVED_SIZE:
  395. err = sg_get_reserved_size(q, arg);
  396. break;
  397. case SG_SET_RESERVED_SIZE:
  398. err = sg_set_reserved_size(q, arg);
  399. break;
  400. case SG_EMULATED_HOST:
  401. err = sg_emulated_host(q, arg);
  402. break;
  403. case SG_IO: {
  404. struct sg_io_hdr hdr;
  405. err = -EFAULT;
  406. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  407. break;
  408. err = sg_io(file, q, bd_disk, &hdr);
  409. if (err == -EFAULT)
  410. break;
  411. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  412. err = -EFAULT;
  413. break;
  414. }
  415. case CDROM_SEND_PACKET: {
  416. struct cdrom_generic_command cgc;
  417. struct sg_io_hdr hdr;
  418. err = -EFAULT;
  419. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  420. break;
  421. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  422. memset(&hdr, 0, sizeof(hdr));
  423. hdr.interface_id = 'S';
  424. hdr.cmd_len = sizeof(cgc.cmd);
  425. hdr.dxfer_len = cgc.buflen;
  426. err = 0;
  427. switch (cgc.data_direction) {
  428. case CGC_DATA_UNKNOWN:
  429. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  430. break;
  431. case CGC_DATA_WRITE:
  432. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  433. break;
  434. case CGC_DATA_READ:
  435. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  436. break;
  437. case CGC_DATA_NONE:
  438. hdr.dxfer_direction = SG_DXFER_NONE;
  439. break;
  440. default:
  441. err = -EINVAL;
  442. }
  443. if (err)
  444. break;
  445. hdr.dxferp = cgc.buffer;
  446. hdr.sbp = cgc.sense;
  447. if (hdr.sbp)
  448. hdr.mx_sb_len = sizeof(struct request_sense);
  449. hdr.timeout = cgc.timeout;
  450. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  451. hdr.cmd_len = sizeof(cgc.cmd);
  452. err = sg_io(file, q, bd_disk, &hdr);
  453. if (err == -EFAULT)
  454. break;
  455. if (hdr.status)
  456. err = -EIO;
  457. cgc.stat = err;
  458. cgc.buflen = hdr.resid;
  459. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  460. err = -EFAULT;
  461. break;
  462. }
  463. /*
  464. * old junk scsi send command ioctl
  465. */
  466. case SCSI_IOCTL_SEND_COMMAND:
  467. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  468. err = -EINVAL;
  469. if (!arg)
  470. break;
  471. err = sg_scsi_ioctl(file, q, bd_disk, arg);
  472. break;
  473. case CDROMCLOSETRAY:
  474. err = blk_send_start_stop(q, bd_disk, 0x03);
  475. break;
  476. case CDROMEJECT:
  477. err = blk_send_start_stop(q, bd_disk, 0x02);
  478. break;
  479. default:
  480. err = -ENOTTY;
  481. }
  482. blk_put_queue(q);
  483. return err;
  484. }
  485. EXPORT_SYMBOL(scsi_cmd_ioctl);