scsi_ioctl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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/ratelimit.h>
  28. #include <linux/slab.h>
  29. #include <linux/times.h>
  30. #include <linux/uio.h>
  31. #include <linux/uaccess.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_ioctl.h>
  34. #include <scsi/scsi_cmnd.h>
  35. struct blk_cmd_filter {
  36. unsigned long read_ok[BLK_SCSI_CMD_PER_LONG];
  37. unsigned long write_ok[BLK_SCSI_CMD_PER_LONG];
  38. };
  39. static struct blk_cmd_filter blk_default_cmd_filter;
  40. /* Command group 3 is reserved and should never be used. */
  41. const unsigned char scsi_command_size_tbl[8] =
  42. {
  43. 6, 10, 10, 12,
  44. 16, 12, 10, 10
  45. };
  46. EXPORT_SYMBOL(scsi_command_size_tbl);
  47. #include <scsi/sg.h>
  48. static int sg_get_version(int __user *p)
  49. {
  50. static const int sg_version_num = 30527;
  51. return put_user(sg_version_num, p);
  52. }
  53. static int scsi_get_idlun(struct request_queue *q, int __user *p)
  54. {
  55. return put_user(0, p);
  56. }
  57. static int scsi_get_bus(struct request_queue *q, int __user *p)
  58. {
  59. return put_user(0, p);
  60. }
  61. static int sg_get_timeout(struct request_queue *q)
  62. {
  63. return jiffies_to_clock_t(q->sg_timeout);
  64. }
  65. static int sg_set_timeout(struct request_queue *q, int __user *p)
  66. {
  67. int timeout, err = get_user(timeout, p);
  68. if (!err)
  69. q->sg_timeout = clock_t_to_jiffies(timeout);
  70. return err;
  71. }
  72. static int max_sectors_bytes(struct request_queue *q)
  73. {
  74. unsigned int max_sectors = queue_max_sectors(q);
  75. max_sectors = min_t(unsigned int, max_sectors, INT_MAX >> 9);
  76. return max_sectors << 9;
  77. }
  78. static int sg_get_reserved_size(struct request_queue *q, int __user *p)
  79. {
  80. int val = min_t(int, q->sg_reserved_size, max_sectors_bytes(q));
  81. return put_user(val, p);
  82. }
  83. static int sg_set_reserved_size(struct request_queue *q, int __user *p)
  84. {
  85. int size, err = get_user(size, p);
  86. if (err)
  87. return err;
  88. if (size < 0)
  89. return -EINVAL;
  90. q->sg_reserved_size = min(size, max_sectors_bytes(q));
  91. return 0;
  92. }
  93. /*
  94. * will always return that we are ATAPI even for a real SCSI drive, I'm not
  95. * so sure this is worth doing anything about (why would you care??)
  96. */
  97. static int sg_emulated_host(struct request_queue *q, int __user *p)
  98. {
  99. return put_user(1, p);
  100. }
  101. static void blk_set_cmd_filter_defaults(struct blk_cmd_filter *filter)
  102. {
  103. /* Basic read-only commands */
  104. __set_bit(TEST_UNIT_READY, filter->read_ok);
  105. __set_bit(REQUEST_SENSE, filter->read_ok);
  106. __set_bit(READ_6, filter->read_ok);
  107. __set_bit(READ_10, filter->read_ok);
  108. __set_bit(READ_12, filter->read_ok);
  109. __set_bit(READ_16, filter->read_ok);
  110. __set_bit(READ_BUFFER, filter->read_ok);
  111. __set_bit(READ_DEFECT_DATA, filter->read_ok);
  112. __set_bit(READ_CAPACITY, filter->read_ok);
  113. __set_bit(READ_LONG, filter->read_ok);
  114. __set_bit(INQUIRY, filter->read_ok);
  115. __set_bit(MODE_SENSE, filter->read_ok);
  116. __set_bit(MODE_SENSE_10, filter->read_ok);
  117. __set_bit(LOG_SENSE, filter->read_ok);
  118. __set_bit(START_STOP, filter->read_ok);
  119. __set_bit(GPCMD_VERIFY_10, filter->read_ok);
  120. __set_bit(VERIFY_16, filter->read_ok);
  121. __set_bit(REPORT_LUNS, filter->read_ok);
  122. __set_bit(SERVICE_ACTION_IN_16, filter->read_ok);
  123. __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
  124. __set_bit(MAINTENANCE_IN, filter->read_ok);
  125. __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
  126. /* Audio CD commands */
  127. __set_bit(GPCMD_PLAY_CD, filter->read_ok);
  128. __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
  129. __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
  130. __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
  131. __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
  132. /* CD/DVD data reading */
  133. __set_bit(GPCMD_READ_CD, filter->read_ok);
  134. __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
  135. __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
  136. __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
  137. __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
  138. __set_bit(GPCMD_READ_HEADER, filter->read_ok);
  139. __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
  140. __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
  141. __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
  142. __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
  143. __set_bit(GPCMD_SCAN, filter->read_ok);
  144. __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
  145. __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
  146. __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
  147. __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
  148. __set_bit(GPCMD_SEEK, filter->read_ok);
  149. __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
  150. /* Basic writing commands */
  151. __set_bit(WRITE_6, filter->write_ok);
  152. __set_bit(WRITE_10, filter->write_ok);
  153. __set_bit(WRITE_VERIFY, filter->write_ok);
  154. __set_bit(WRITE_12, filter->write_ok);
  155. __set_bit(WRITE_VERIFY_12, filter->write_ok);
  156. __set_bit(WRITE_16, filter->write_ok);
  157. __set_bit(WRITE_LONG, filter->write_ok);
  158. __set_bit(WRITE_LONG_2, filter->write_ok);
  159. __set_bit(WRITE_SAME, filter->write_ok);
  160. __set_bit(WRITE_SAME_16, filter->write_ok);
  161. __set_bit(WRITE_SAME_32, filter->write_ok);
  162. __set_bit(ERASE, filter->write_ok);
  163. __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
  164. __set_bit(MODE_SELECT, filter->write_ok);
  165. __set_bit(LOG_SELECT, filter->write_ok);
  166. __set_bit(GPCMD_BLANK, filter->write_ok);
  167. __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
  168. __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
  169. __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
  170. __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
  171. __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
  172. __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
  173. __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
  174. __set_bit(GPCMD_SEND_KEY, filter->write_ok);
  175. __set_bit(GPCMD_SEND_OPC, filter->write_ok);
  176. __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
  177. __set_bit(GPCMD_SET_SPEED, filter->write_ok);
  178. __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
  179. __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
  180. __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
  181. __set_bit(GPCMD_SET_READ_AHEAD, filter->write_ok);
  182. }
  183. int blk_verify_command(unsigned char *cmd, fmode_t mode)
  184. {
  185. struct blk_cmd_filter *filter = &blk_default_cmd_filter;
  186. /* root can do any command. */
  187. if (capable(CAP_SYS_RAWIO))
  188. return 0;
  189. /* Anybody who can open the device can do a read-safe command */
  190. if (test_bit(cmd[0], filter->read_ok))
  191. return 0;
  192. /* Write-safe commands require a writable open */
  193. if (test_bit(cmd[0], filter->write_ok) && (mode & FMODE_WRITE))
  194. return 0;
  195. return -EPERM;
  196. }
  197. EXPORT_SYMBOL(blk_verify_command);
  198. static int blk_fill_sghdr_rq(struct request_queue *q, struct request *rq,
  199. struct sg_io_hdr *hdr, fmode_t mode)
  200. {
  201. struct scsi_request *req = scsi_req(rq);
  202. if (copy_from_user(req->cmd, hdr->cmdp, hdr->cmd_len))
  203. return -EFAULT;
  204. if (blk_verify_command(req->cmd, mode))
  205. return -EPERM;
  206. /*
  207. * fill in request structure
  208. */
  209. req->cmd_len = hdr->cmd_len;
  210. rq->timeout = msecs_to_jiffies(hdr->timeout);
  211. if (!rq->timeout)
  212. rq->timeout = q->sg_timeout;
  213. if (!rq->timeout)
  214. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  215. if (rq->timeout < BLK_MIN_SG_TIMEOUT)
  216. rq->timeout = BLK_MIN_SG_TIMEOUT;
  217. return 0;
  218. }
  219. static int blk_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
  220. struct bio *bio)
  221. {
  222. struct scsi_request *req = scsi_req(rq);
  223. int r, ret = 0;
  224. /*
  225. * fill in all the output members
  226. */
  227. hdr->status = req->result & 0xff;
  228. hdr->masked_status = status_byte(req->result);
  229. hdr->msg_status = msg_byte(req->result);
  230. hdr->host_status = host_byte(req->result);
  231. hdr->driver_status = driver_byte(req->result);
  232. hdr->info = 0;
  233. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  234. hdr->info |= SG_INFO_CHECK;
  235. hdr->resid = req->resid_len;
  236. hdr->sb_len_wr = 0;
  237. if (req->sense_len && hdr->sbp) {
  238. int len = min((unsigned int) hdr->mx_sb_len, req->sense_len);
  239. if (!copy_to_user(hdr->sbp, req->sense, len))
  240. hdr->sb_len_wr = len;
  241. else
  242. ret = -EFAULT;
  243. }
  244. r = blk_rq_unmap_user(bio);
  245. if (!ret)
  246. ret = r;
  247. return ret;
  248. }
  249. static int sg_io(struct request_queue *q, struct gendisk *bd_disk,
  250. struct sg_io_hdr *hdr, fmode_t mode)
  251. {
  252. unsigned long start_time;
  253. ssize_t ret = 0;
  254. int writing = 0;
  255. int at_head = 0;
  256. struct request *rq;
  257. struct scsi_request *req;
  258. struct bio *bio;
  259. if (hdr->interface_id != 'S')
  260. return -EINVAL;
  261. if (hdr->dxfer_len > (queue_max_hw_sectors(q) << 9))
  262. return -EIO;
  263. if (hdr->dxfer_len)
  264. switch (hdr->dxfer_direction) {
  265. default:
  266. return -EINVAL;
  267. case SG_DXFER_TO_DEV:
  268. writing = 1;
  269. break;
  270. case SG_DXFER_TO_FROM_DEV:
  271. case SG_DXFER_FROM_DEV:
  272. break;
  273. }
  274. if (hdr->flags & SG_FLAG_Q_AT_HEAD)
  275. at_head = 1;
  276. ret = -ENOMEM;
  277. rq = blk_get_request(q, writing ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
  278. if (IS_ERR(rq))
  279. return PTR_ERR(rq);
  280. req = scsi_req(rq);
  281. if (hdr->cmd_len > BLK_MAX_CDB) {
  282. req->cmd = kzalloc(hdr->cmd_len, GFP_KERNEL);
  283. if (!req->cmd)
  284. goto out_put_request;
  285. }
  286. ret = blk_fill_sghdr_rq(q, rq, hdr, mode);
  287. if (ret < 0)
  288. goto out_free_cdb;
  289. ret = 0;
  290. if (hdr->iovec_count) {
  291. struct iov_iter i;
  292. struct iovec *iov = NULL;
  293. ret = import_iovec(rq_data_dir(rq),
  294. hdr->dxferp, hdr->iovec_count,
  295. 0, &iov, &i);
  296. if (ret < 0)
  297. goto out_free_cdb;
  298. /* SG_IO howto says that the shorter of the two wins */
  299. iov_iter_truncate(&i, hdr->dxfer_len);
  300. ret = blk_rq_map_user_iov(q, rq, NULL, &i, GFP_KERNEL);
  301. kfree(iov);
  302. } else if (hdr->dxfer_len)
  303. ret = blk_rq_map_user(q, rq, NULL, hdr->dxferp, hdr->dxfer_len,
  304. GFP_KERNEL);
  305. if (ret)
  306. goto out_free_cdb;
  307. bio = rq->bio;
  308. req->retries = 0;
  309. start_time = jiffies;
  310. /* ignore return value. All information is passed back to caller
  311. * (if he doesn't check that is his problem).
  312. * N.B. a non-zero SCSI status is _not_ necessarily an error.
  313. */
  314. blk_execute_rq(q, bd_disk, rq, at_head);
  315. hdr->duration = jiffies_to_msecs(jiffies - start_time);
  316. ret = blk_complete_sghdr_rq(rq, hdr, bio);
  317. out_free_cdb:
  318. scsi_req_free_cmd(req);
  319. out_put_request:
  320. blk_put_request(rq);
  321. return ret;
  322. }
  323. /**
  324. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  325. * @q: request queue to send scsi commands down
  326. * @disk: gendisk to operate on (option)
  327. * @mode: mode used to open the file through which the ioctl has been
  328. * submitted
  329. * @sic: userspace structure describing the command to perform
  330. *
  331. * Send down the scsi command described by @sic to the device below
  332. * the request queue @q. If @file is non-NULL it's used to perform
  333. * fine-grained permission checks that allow users to send down
  334. * non-destructive SCSI commands. If the caller has a struct gendisk
  335. * available it should be passed in as @disk to allow the low level
  336. * driver to use the information contained in it. A non-NULL @disk
  337. * is only allowed if the caller knows that the low level driver doesn't
  338. * need it (e.g. in the scsi subsystem).
  339. *
  340. * Notes:
  341. * - This interface is deprecated - users should use the SG_IO
  342. * interface instead, as this is a more flexible approach to
  343. * performing SCSI commands on a device.
  344. * - The SCSI command length is determined by examining the 1st byte
  345. * of the given command. There is no way to override this.
  346. * - Data transfers are limited to PAGE_SIZE
  347. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  348. * accommodate the sense buffer when an error occurs.
  349. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  350. * old code will not be surprised.
  351. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  352. * a negative return and the Unix error code in 'errno'.
  353. * If the SCSI command succeeds then 0 is returned.
  354. * Positive numbers returned are the compacted SCSI error codes (4
  355. * bytes in one int) where the lowest byte is the SCSI status.
  356. */
  357. int sg_scsi_ioctl(struct request_queue *q, struct gendisk *disk, fmode_t mode,
  358. struct scsi_ioctl_command __user *sic)
  359. {
  360. enum { OMAX_SB_LEN = 16 }; /* For backward compatibility */
  361. struct request *rq;
  362. struct scsi_request *req;
  363. int err;
  364. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  365. char *buffer = NULL;
  366. if (!sic)
  367. return -EINVAL;
  368. /*
  369. * get in an out lengths, verify they don't exceed a page worth of data
  370. */
  371. if (get_user(in_len, &sic->inlen))
  372. return -EFAULT;
  373. if (get_user(out_len, &sic->outlen))
  374. return -EFAULT;
  375. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  376. return -EINVAL;
  377. if (get_user(opcode, sic->data))
  378. return -EFAULT;
  379. bytes = max(in_len, out_len);
  380. if (bytes) {
  381. buffer = kzalloc(bytes, q->bounce_gfp | GFP_USER| __GFP_NOWARN);
  382. if (!buffer)
  383. return -ENOMEM;
  384. }
  385. rq = blk_get_request(q, in_len ? REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
  386. if (IS_ERR(rq)) {
  387. err = PTR_ERR(rq);
  388. goto error_free_buffer;
  389. }
  390. req = scsi_req(rq);
  391. cmdlen = COMMAND_SIZE(opcode);
  392. /*
  393. * get command and data to send to device, if any
  394. */
  395. err = -EFAULT;
  396. req->cmd_len = cmdlen;
  397. if (copy_from_user(req->cmd, sic->data, cmdlen))
  398. goto error;
  399. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  400. goto error;
  401. err = blk_verify_command(req->cmd, mode);
  402. if (err)
  403. goto error;
  404. /* default. possible overriden later */
  405. req->retries = 5;
  406. switch (opcode) {
  407. case SEND_DIAGNOSTIC:
  408. case FORMAT_UNIT:
  409. rq->timeout = FORMAT_UNIT_TIMEOUT;
  410. req->retries = 1;
  411. break;
  412. case START_STOP:
  413. rq->timeout = START_STOP_TIMEOUT;
  414. break;
  415. case MOVE_MEDIUM:
  416. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  417. break;
  418. case READ_ELEMENT_STATUS:
  419. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  420. break;
  421. case READ_DEFECT_DATA:
  422. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  423. req->retries = 1;
  424. break;
  425. default:
  426. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  427. break;
  428. }
  429. if (bytes && blk_rq_map_kern(q, rq, buffer, bytes, GFP_NOIO)) {
  430. err = DRIVER_ERROR << 24;
  431. goto error;
  432. }
  433. blk_execute_rq(q, disk, rq, 0);
  434. err = req->result & 0xff; /* only 8 bit SCSI status */
  435. if (err) {
  436. if (req->sense_len && req->sense) {
  437. bytes = (OMAX_SB_LEN > req->sense_len) ?
  438. req->sense_len : OMAX_SB_LEN;
  439. if (copy_to_user(sic->data, req->sense, bytes))
  440. err = -EFAULT;
  441. }
  442. } else {
  443. if (copy_to_user(sic->data, buffer, out_len))
  444. err = -EFAULT;
  445. }
  446. error:
  447. blk_put_request(rq);
  448. error_free_buffer:
  449. kfree(buffer);
  450. return err;
  451. }
  452. EXPORT_SYMBOL_GPL(sg_scsi_ioctl);
  453. /* Send basic block requests */
  454. static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
  455. int cmd, int data)
  456. {
  457. struct request *rq;
  458. int err;
  459. rq = blk_get_request(q, REQ_OP_SCSI_OUT, 0);
  460. if (IS_ERR(rq))
  461. return PTR_ERR(rq);
  462. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  463. scsi_req(rq)->cmd[0] = cmd;
  464. scsi_req(rq)->cmd[4] = data;
  465. scsi_req(rq)->cmd_len = 6;
  466. blk_execute_rq(q, bd_disk, rq, 0);
  467. err = scsi_req(rq)->result ? -EIO : 0;
  468. blk_put_request(rq);
  469. return err;
  470. }
  471. static inline int blk_send_start_stop(struct request_queue *q,
  472. struct gendisk *bd_disk, int data)
  473. {
  474. return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
  475. }
  476. int scsi_cmd_ioctl(struct request_queue *q, struct gendisk *bd_disk, fmode_t mode,
  477. unsigned int cmd, void __user *arg)
  478. {
  479. int err;
  480. if (!q)
  481. return -ENXIO;
  482. switch (cmd) {
  483. /*
  484. * new sgv3 interface
  485. */
  486. case SG_GET_VERSION_NUM:
  487. err = sg_get_version(arg);
  488. break;
  489. case SCSI_IOCTL_GET_IDLUN:
  490. err = scsi_get_idlun(q, arg);
  491. break;
  492. case SCSI_IOCTL_GET_BUS_NUMBER:
  493. err = scsi_get_bus(q, arg);
  494. break;
  495. case SG_SET_TIMEOUT:
  496. err = sg_set_timeout(q, arg);
  497. break;
  498. case SG_GET_TIMEOUT:
  499. err = sg_get_timeout(q);
  500. break;
  501. case SG_GET_RESERVED_SIZE:
  502. err = sg_get_reserved_size(q, arg);
  503. break;
  504. case SG_SET_RESERVED_SIZE:
  505. err = sg_set_reserved_size(q, arg);
  506. break;
  507. case SG_EMULATED_HOST:
  508. err = sg_emulated_host(q, arg);
  509. break;
  510. case SG_IO: {
  511. struct sg_io_hdr hdr;
  512. err = -EFAULT;
  513. if (copy_from_user(&hdr, arg, sizeof(hdr)))
  514. break;
  515. err = sg_io(q, bd_disk, &hdr, mode);
  516. if (err == -EFAULT)
  517. break;
  518. if (copy_to_user(arg, &hdr, sizeof(hdr)))
  519. err = -EFAULT;
  520. break;
  521. }
  522. case CDROM_SEND_PACKET: {
  523. struct cdrom_generic_command cgc;
  524. struct sg_io_hdr hdr;
  525. err = -EFAULT;
  526. if (copy_from_user(&cgc, arg, sizeof(cgc)))
  527. break;
  528. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  529. memset(&hdr, 0, sizeof(hdr));
  530. hdr.interface_id = 'S';
  531. hdr.cmd_len = sizeof(cgc.cmd);
  532. hdr.dxfer_len = cgc.buflen;
  533. err = 0;
  534. switch (cgc.data_direction) {
  535. case CGC_DATA_UNKNOWN:
  536. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  537. break;
  538. case CGC_DATA_WRITE:
  539. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  540. break;
  541. case CGC_DATA_READ:
  542. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  543. break;
  544. case CGC_DATA_NONE:
  545. hdr.dxfer_direction = SG_DXFER_NONE;
  546. break;
  547. default:
  548. err = -EINVAL;
  549. }
  550. if (err)
  551. break;
  552. hdr.dxferp = cgc.buffer;
  553. hdr.sbp = cgc.sense;
  554. if (hdr.sbp)
  555. hdr.mx_sb_len = sizeof(struct request_sense);
  556. hdr.timeout = jiffies_to_msecs(cgc.timeout);
  557. hdr.cmdp = ((struct cdrom_generic_command __user*) arg)->cmd;
  558. hdr.cmd_len = sizeof(cgc.cmd);
  559. err = sg_io(q, bd_disk, &hdr, mode);
  560. if (err == -EFAULT)
  561. break;
  562. if (hdr.status)
  563. err = -EIO;
  564. cgc.stat = err;
  565. cgc.buflen = hdr.resid;
  566. if (copy_to_user(arg, &cgc, sizeof(cgc)))
  567. err = -EFAULT;
  568. break;
  569. }
  570. /*
  571. * old junk scsi send command ioctl
  572. */
  573. case SCSI_IOCTL_SEND_COMMAND:
  574. printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
  575. err = -EINVAL;
  576. if (!arg)
  577. break;
  578. err = sg_scsi_ioctl(q, bd_disk, mode, arg);
  579. break;
  580. case CDROMCLOSETRAY:
  581. err = blk_send_start_stop(q, bd_disk, 0x03);
  582. break;
  583. case CDROMEJECT:
  584. err = blk_send_start_stop(q, bd_disk, 0x02);
  585. break;
  586. default:
  587. err = -ENOTTY;
  588. }
  589. return err;
  590. }
  591. EXPORT_SYMBOL(scsi_cmd_ioctl);
  592. int scsi_verify_blk_ioctl(struct block_device *bd, unsigned int cmd)
  593. {
  594. if (bd && bd == bd->bd_contains)
  595. return 0;
  596. if (capable(CAP_SYS_RAWIO))
  597. return 0;
  598. return -ENOIOCTLCMD;
  599. }
  600. EXPORT_SYMBOL(scsi_verify_blk_ioctl);
  601. int scsi_cmd_blk_ioctl(struct block_device *bd, fmode_t mode,
  602. unsigned int cmd, void __user *arg)
  603. {
  604. int ret;
  605. ret = scsi_verify_blk_ioctl(bd, cmd);
  606. if (ret < 0)
  607. return ret;
  608. return scsi_cmd_ioctl(bd->bd_disk->queue, bd->bd_disk, mode, cmd, arg);
  609. }
  610. EXPORT_SYMBOL(scsi_cmd_blk_ioctl);
  611. /**
  612. * scsi_req_init - initialize certain fields of a scsi_request structure
  613. * @req: Pointer to a scsi_request structure.
  614. * Initializes .__cmd[], .cmd, .cmd_len and .sense_len but no other members
  615. * of struct scsi_request.
  616. */
  617. void scsi_req_init(struct scsi_request *req)
  618. {
  619. memset(req->__cmd, 0, sizeof(req->__cmd));
  620. req->cmd = req->__cmd;
  621. req->cmd_len = BLK_MAX_CDB;
  622. req->sense_len = 0;
  623. }
  624. EXPORT_SYMBOL(scsi_req_init);
  625. static int __init blk_scsi_ioctl_init(void)
  626. {
  627. blk_set_cmd_filter_defaults(&blk_default_cmd_filter);
  628. return 0;
  629. }
  630. fs_initcall(blk_scsi_ioctl_init);