scsi_ioctl.c 18 KB

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