scsi_ioctl.c 18 KB

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