scsi_ioctl.c 20 KB

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