scsi_ioctl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Changes:
  4. * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
  5. * - get rid of some verify_areas and use __copy*user and __get/put_user
  6. * for the ones that remain
  7. */
  8. #include <linux/module.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/errno.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/string.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/cdrom.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_eh.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi_ioctl.h>
  24. #include <scsi/sg.h>
  25. #include <scsi/scsi_dbg.h>
  26. #include "scsi_logging.h"
  27. #define NORMAL_RETRIES 5
  28. #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
  29. #define MAX_BUF PAGE_SIZE
  30. /**
  31. * ioctl_probe -- return host identification
  32. * @host: host to identify
  33. * @buffer: userspace buffer for identification
  34. *
  35. * Return:
  36. * * if successful, %1 and an identifying string at @buffer, if @buffer
  37. * is non-NULL, filling to the length stored at * (int *) @buffer.
  38. * * <0 error code on failure.
  39. */
  40. static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
  41. {
  42. unsigned int len, slen;
  43. const char *string;
  44. if (buffer) {
  45. if (get_user(len, (unsigned int __user *) buffer))
  46. return -EFAULT;
  47. if (host->hostt->info)
  48. string = host->hostt->info(host);
  49. else
  50. string = host->hostt->name;
  51. if (string) {
  52. slen = strlen(string);
  53. if (len > slen)
  54. len = slen + 1;
  55. if (copy_to_user(buffer, string, len))
  56. return -EFAULT;
  57. }
  58. }
  59. return 1;
  60. }
  61. static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
  62. int timeout, int retries)
  63. {
  64. int result;
  65. struct scsi_sense_hdr sshdr;
  66. const struct scsi_exec_args exec_args = {
  67. .sshdr = &sshdr,
  68. };
  69. SCSI_LOG_IOCTL(1, sdev_printk(KERN_INFO, sdev,
  70. "Trying ioctl with scsi command %d\n", *cmd));
  71. result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, NULL, 0, timeout,
  72. retries, &exec_args);
  73. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  74. "Ioctl returned 0x%x\n", result));
  75. if (result < 0)
  76. goto out;
  77. if (scsi_sense_valid(&sshdr)) {
  78. switch (sshdr.sense_key) {
  79. case ILLEGAL_REQUEST:
  80. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  81. sdev->lockable = 0;
  82. else
  83. sdev_printk(KERN_INFO, sdev,
  84. "ioctl_internal_command: "
  85. "ILLEGAL REQUEST "
  86. "asc=0x%x ascq=0x%x\n",
  87. sshdr.asc, sshdr.ascq);
  88. break;
  89. case NOT_READY: /* This happens if there is no disc in drive */
  90. if (sdev->removable)
  91. break;
  92. fallthrough;
  93. case UNIT_ATTENTION:
  94. if (sdev->removable) {
  95. sdev->changed = 1;
  96. result = 0; /* This is no longer considered an error */
  97. break;
  98. }
  99. fallthrough; /* for non-removable media */
  100. default:
  101. sdev_printk(KERN_INFO, sdev,
  102. "ioctl_internal_command return code = %x\n",
  103. result);
  104. scsi_print_sense_hdr(sdev, NULL, &sshdr);
  105. break;
  106. }
  107. }
  108. out:
  109. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  110. "IOCTL Releasing command\n"));
  111. return result;
  112. }
  113. /**
  114. * scsi_set_medium_removal() - send command to allow or prevent medium removal
  115. * @sdev: target scsi device
  116. * @state: removal state to set (prevent or allow)
  117. *
  118. * Returns:
  119. * * %0 if @sdev is not removable or not lockable or successful.
  120. * * non-%0 is a SCSI result code if > 0 or kernel error code if < 0.
  121. * * Sets @sdev->locked to the new state on success.
  122. */
  123. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  124. {
  125. char scsi_cmd[MAX_COMMAND_SIZE];
  126. int ret;
  127. if (!sdev->removable || !sdev->lockable)
  128. return 0;
  129. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  130. scsi_cmd[1] = 0;
  131. scsi_cmd[2] = 0;
  132. scsi_cmd[3] = 0;
  133. scsi_cmd[4] = state;
  134. scsi_cmd[5] = 0;
  135. ret = ioctl_internal_command(sdev, scsi_cmd,
  136. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  137. if (ret == 0)
  138. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(scsi_set_medium_removal);
  142. /*
  143. * The scsi_ioctl_get_pci() function places into arg the value
  144. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  145. * Returns: 0 on success
  146. * -ENXIO if there isn't a PCI device pointer
  147. * (could be because the SCSI driver hasn't been
  148. * updated yet, or because it isn't a SCSI
  149. * device)
  150. * any copy_to_user() error on failure there
  151. */
  152. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  153. {
  154. struct device *dev = scsi_get_device(sdev->host);
  155. const char *name;
  156. if (!dev)
  157. return -ENXIO;
  158. name = dev_name(dev);
  159. /* compatibility with old ioctl which only returned
  160. * 20 characters */
  161. return copy_to_user(arg, name, min(strlen(name), (size_t)20))
  162. ? -EFAULT: 0;
  163. }
  164. static int sg_get_version(int __user *p)
  165. {
  166. static const int sg_version_num = 30527;
  167. return put_user(sg_version_num, p);
  168. }
  169. static int sg_set_timeout(struct scsi_device *sdev, int __user *p)
  170. {
  171. int timeout, err = get_user(timeout, p);
  172. if (!err)
  173. sdev->sg_timeout = clock_t_to_jiffies(timeout);
  174. return err;
  175. }
  176. static int sg_get_reserved_size(struct scsi_device *sdev, int __user *p)
  177. {
  178. int val = min(sdev->sg_reserved_size,
  179. queue_max_bytes(sdev->request_queue));
  180. return put_user(val, p);
  181. }
  182. static int sg_set_reserved_size(struct scsi_device *sdev, int __user *p)
  183. {
  184. int size, err = get_user(size, p);
  185. if (err)
  186. return err;
  187. if (size < 0)
  188. return -EINVAL;
  189. sdev->sg_reserved_size = min_t(unsigned int, size,
  190. queue_max_bytes(sdev->request_queue));
  191. return 0;
  192. }
  193. /*
  194. * will always return that we are ATAPI even for a real SCSI drive, I'm not
  195. * so sure this is worth doing anything about (why would you care??)
  196. */
  197. static int sg_emulated_host(struct request_queue *q, int __user *p)
  198. {
  199. return put_user(1, p);
  200. }
  201. static int scsi_get_idlun(struct scsi_device *sdev, void __user *argp)
  202. {
  203. struct scsi_idlun v = {
  204. .dev_id = (sdev->id & 0xff) +
  205. ((sdev->lun & 0xff) << 8) +
  206. ((sdev->channel & 0xff) << 16) +
  207. ((sdev->host->host_no & 0xff) << 24),
  208. .host_unique_id = sdev->host->unique_id
  209. };
  210. if (copy_to_user(argp, &v, sizeof(struct scsi_idlun)))
  211. return -EFAULT;
  212. return 0;
  213. }
  214. static int scsi_send_start_stop(struct scsi_device *sdev, int data)
  215. {
  216. u8 cdb[MAX_COMMAND_SIZE] = { };
  217. cdb[0] = START_STOP;
  218. cdb[4] = data;
  219. return ioctl_internal_command(sdev, cdb, START_STOP_TIMEOUT,
  220. NORMAL_RETRIES);
  221. }
  222. /**
  223. * scsi_cmd_allowed() - Check if the given command is allowed.
  224. * @cmd: SCSI command to check
  225. * @open_for_write: is the file / block device opened for writing?
  226. *
  227. * Only a subset of commands are allowed for unprivileged users. Commands used
  228. * to format the media, update the firmware, etc. are not permitted.
  229. *
  230. * Return: %true if the cmd is allowed, otherwise @false.
  231. */
  232. bool scsi_cmd_allowed(unsigned char *cmd, bool open_for_write)
  233. {
  234. /* root can do any command. */
  235. if (capable(CAP_SYS_RAWIO))
  236. return true;
  237. /* Anybody who can open the device can do a read-safe command */
  238. switch (cmd[0]) {
  239. /* Basic read-only commands */
  240. case TEST_UNIT_READY:
  241. case REQUEST_SENSE:
  242. case READ_6:
  243. case READ_10:
  244. case READ_12:
  245. case READ_16:
  246. case READ_BUFFER:
  247. case READ_DEFECT_DATA:
  248. case READ_CAPACITY: /* also GPCMD_READ_CDVD_CAPACITY */
  249. case READ_LONG:
  250. case INQUIRY:
  251. case MODE_SENSE:
  252. case MODE_SENSE_10:
  253. case LOG_SENSE:
  254. case START_STOP:
  255. case GPCMD_VERIFY_10:
  256. case VERIFY_16:
  257. case REPORT_LUNS:
  258. case SERVICE_ACTION_IN_16:
  259. case RECEIVE_DIAGNOSTIC:
  260. case MAINTENANCE_IN: /* also GPCMD_SEND_KEY, which is a write command */
  261. case GPCMD_READ_BUFFER_CAPACITY:
  262. /* Audio CD commands */
  263. case GPCMD_PLAY_CD:
  264. case GPCMD_PLAY_AUDIO_10:
  265. case GPCMD_PLAY_AUDIO_MSF:
  266. case GPCMD_PLAY_AUDIO_TI:
  267. case GPCMD_PAUSE_RESUME:
  268. /* CD/DVD data reading */
  269. case GPCMD_READ_CD:
  270. case GPCMD_READ_CD_MSF:
  271. case GPCMD_READ_DISC_INFO:
  272. case GPCMD_READ_DVD_STRUCTURE:
  273. case GPCMD_READ_HEADER:
  274. case GPCMD_READ_TRACK_RZONE_INFO:
  275. case GPCMD_READ_SUBCHANNEL:
  276. case GPCMD_READ_TOC_PMA_ATIP:
  277. case GPCMD_REPORT_KEY:
  278. case GPCMD_SCAN:
  279. case GPCMD_GET_CONFIGURATION:
  280. case GPCMD_READ_FORMAT_CAPACITIES:
  281. case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
  282. case GPCMD_GET_PERFORMANCE:
  283. case GPCMD_SEEK:
  284. case GPCMD_STOP_PLAY_SCAN:
  285. /* ZBC */
  286. case ZBC_IN:
  287. return true;
  288. /* Basic writing commands */
  289. case WRITE_6:
  290. case WRITE_10:
  291. case WRITE_VERIFY:
  292. case WRITE_12:
  293. case WRITE_VERIFY_12:
  294. case WRITE_16:
  295. case WRITE_LONG:
  296. case WRITE_LONG_2:
  297. case WRITE_SAME:
  298. case WRITE_SAME_16:
  299. case WRITE_SAME_32:
  300. case ERASE:
  301. case GPCMD_MODE_SELECT_10:
  302. case MODE_SELECT:
  303. case LOG_SELECT:
  304. case GPCMD_BLANK:
  305. case GPCMD_CLOSE_TRACK:
  306. case GPCMD_FLUSH_CACHE:
  307. case GPCMD_FORMAT_UNIT:
  308. case GPCMD_REPAIR_RZONE_TRACK:
  309. case GPCMD_RESERVE_RZONE_TRACK:
  310. case GPCMD_SEND_DVD_STRUCTURE:
  311. case GPCMD_SEND_EVENT:
  312. case GPCMD_SEND_OPC:
  313. case GPCMD_SEND_CUE_SHEET:
  314. case GPCMD_SET_SPEED:
  315. case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  316. case GPCMD_LOAD_UNLOAD:
  317. case GPCMD_SET_STREAMING:
  318. case GPCMD_SET_READ_AHEAD:
  319. /* ZBC */
  320. case ZBC_OUT:
  321. return open_for_write;
  322. default:
  323. return false;
  324. }
  325. }
  326. EXPORT_SYMBOL(scsi_cmd_allowed);
  327. static int scsi_fill_sghdr_rq(struct scsi_device *sdev, struct request *rq,
  328. struct sg_io_hdr *hdr, bool open_for_write)
  329. {
  330. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
  331. if (hdr->cmd_len < 6)
  332. return -EMSGSIZE;
  333. if (copy_from_user(scmd->cmnd, hdr->cmdp, hdr->cmd_len))
  334. return -EFAULT;
  335. if (!scsi_cmd_allowed(scmd->cmnd, open_for_write))
  336. return -EPERM;
  337. scmd->cmd_len = hdr->cmd_len;
  338. rq->timeout = msecs_to_jiffies(hdr->timeout);
  339. if (!rq->timeout)
  340. rq->timeout = sdev->sg_timeout;
  341. if (!rq->timeout)
  342. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  343. if (rq->timeout < BLK_MIN_SG_TIMEOUT)
  344. rq->timeout = BLK_MIN_SG_TIMEOUT;
  345. return 0;
  346. }
  347. static int scsi_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
  348. struct bio *bio)
  349. {
  350. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
  351. int r, ret = 0;
  352. /*
  353. * fill in all the output members
  354. */
  355. hdr->status = scmd->result & 0xff;
  356. hdr->masked_status = sg_status_byte(scmd->result);
  357. hdr->msg_status = COMMAND_COMPLETE;
  358. hdr->host_status = host_byte(scmd->result);
  359. hdr->driver_status = 0;
  360. if (scsi_status_is_check_condition(hdr->status))
  361. hdr->driver_status = DRIVER_SENSE;
  362. hdr->info = 0;
  363. if (hdr->masked_status || hdr->host_status || hdr->driver_status)
  364. hdr->info |= SG_INFO_CHECK;
  365. hdr->resid = scmd->resid_len;
  366. hdr->sb_len_wr = 0;
  367. if (scmd->sense_len && hdr->sbp) {
  368. int len = min((unsigned int) hdr->mx_sb_len, scmd->sense_len);
  369. if (!copy_to_user(hdr->sbp, scmd->sense_buffer, len))
  370. hdr->sb_len_wr = len;
  371. else
  372. ret = -EFAULT;
  373. }
  374. r = blk_rq_unmap_user(bio);
  375. if (!ret)
  376. ret = r;
  377. return ret;
  378. }
  379. static int sg_io(struct scsi_device *sdev, struct sg_io_hdr *hdr,
  380. bool open_for_write)
  381. {
  382. unsigned long start_time;
  383. ssize_t ret = 0;
  384. int writing = 0;
  385. int at_head = 0;
  386. struct request *rq;
  387. struct scsi_cmnd *scmd;
  388. struct bio *bio;
  389. if (hdr->interface_id != 'S')
  390. return -EINVAL;
  391. if (hdr->dxfer_len > (queue_max_hw_sectors(sdev->request_queue) << 9))
  392. return -EIO;
  393. if (hdr->dxfer_len)
  394. switch (hdr->dxfer_direction) {
  395. default:
  396. return -EINVAL;
  397. case SG_DXFER_TO_DEV:
  398. writing = 1;
  399. break;
  400. case SG_DXFER_TO_FROM_DEV:
  401. case SG_DXFER_FROM_DEV:
  402. break;
  403. }
  404. if (hdr->flags & SG_FLAG_Q_AT_HEAD)
  405. at_head = 1;
  406. rq = scsi_alloc_request(sdev->request_queue, writing ?
  407. REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
  408. if (IS_ERR(rq))
  409. return PTR_ERR(rq);
  410. scmd = blk_mq_rq_to_pdu(rq);
  411. if (hdr->cmd_len > sizeof(scmd->cmnd)) {
  412. ret = -EINVAL;
  413. goto out_put_request;
  414. }
  415. ret = scsi_fill_sghdr_rq(sdev, rq, hdr, open_for_write);
  416. if (ret < 0)
  417. goto out_put_request;
  418. ret = blk_rq_map_user_io(rq, NULL, hdr->dxferp, hdr->dxfer_len,
  419. GFP_KERNEL, hdr->iovec_count && hdr->dxfer_len,
  420. hdr->iovec_count, 0, rq_data_dir(rq));
  421. if (ret)
  422. goto out_put_request;
  423. bio = rq->bio;
  424. scmd->allowed = 0;
  425. start_time = jiffies;
  426. blk_execute_rq(rq, at_head);
  427. hdr->duration = jiffies_to_msecs(jiffies - start_time);
  428. ret = scsi_complete_sghdr_rq(rq, hdr, bio);
  429. out_put_request:
  430. blk_mq_free_request(rq);
  431. return ret;
  432. }
  433. /**
  434. * sg_scsi_ioctl -- handle deprecated SCSI_IOCTL_SEND_COMMAND ioctl
  435. * @q: request queue to send scsi commands down
  436. * @open_for_write: is the file / block device opened for writing?
  437. * @sic: userspace structure describing the command to perform
  438. *
  439. * Send down the scsi command described by @sic to the device below
  440. * the request queue @q.
  441. *
  442. * Notes:
  443. * - This interface is deprecated - users should use the SG_IO
  444. * interface instead, as this is a more flexible approach to
  445. * performing SCSI commands on a device.
  446. * - The SCSI command length is determined by examining the 1st byte
  447. * of the given command. There is no way to override this.
  448. * - Data transfers are limited to PAGE_SIZE
  449. * - The length (x + y) must be at least OMAX_SB_LEN bytes long to
  450. * accommodate the sense buffer when an error occurs.
  451. * The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
  452. * old code will not be surprised.
  453. * - If a Unix error occurs (e.g. ENOMEM) then the user will receive
  454. * a negative return and the Unix error code in 'errno'.
  455. * If the SCSI command succeeds then 0 is returned.
  456. * Positive numbers returned are the compacted SCSI error codes (4
  457. * bytes in one int) where the lowest byte is the SCSI status.
  458. */
  459. static int sg_scsi_ioctl(struct request_queue *q, bool open_for_write,
  460. struct scsi_ioctl_command __user *sic)
  461. {
  462. struct request *rq;
  463. int err;
  464. unsigned int in_len, out_len, bytes, opcode, cmdlen;
  465. struct scsi_cmnd *scmd;
  466. char *buffer = NULL;
  467. if (!sic)
  468. return -EINVAL;
  469. /*
  470. * get in an out lengths, verify they don't exceed a page worth of data
  471. */
  472. if (get_user(in_len, &sic->inlen))
  473. return -EFAULT;
  474. if (get_user(out_len, &sic->outlen))
  475. return -EFAULT;
  476. if (in_len > PAGE_SIZE || out_len > PAGE_SIZE)
  477. return -EINVAL;
  478. if (get_user(opcode, &sic->data[0]))
  479. return -EFAULT;
  480. bytes = max(in_len, out_len);
  481. if (bytes) {
  482. buffer = kzalloc(bytes, GFP_NOIO | GFP_USER | __GFP_NOWARN);
  483. if (!buffer)
  484. return -ENOMEM;
  485. }
  486. rq = scsi_alloc_request(q, in_len ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
  487. if (IS_ERR(rq)) {
  488. err = PTR_ERR(rq);
  489. goto error_free_buffer;
  490. }
  491. scmd = blk_mq_rq_to_pdu(rq);
  492. cmdlen = COMMAND_SIZE(opcode);
  493. /*
  494. * get command and data to send to device, if any
  495. */
  496. err = -EFAULT;
  497. scmd->cmd_len = cmdlen;
  498. if (copy_from_user(scmd->cmnd, sic->data, cmdlen))
  499. goto error;
  500. if (in_len && copy_from_user(buffer, sic->data + cmdlen, in_len))
  501. goto error;
  502. err = -EPERM;
  503. if (!scsi_cmd_allowed(scmd->cmnd, open_for_write))
  504. goto error;
  505. /* default. possible overridden later */
  506. scmd->allowed = 5;
  507. switch (opcode) {
  508. case SEND_DIAGNOSTIC:
  509. case FORMAT_UNIT:
  510. rq->timeout = FORMAT_UNIT_TIMEOUT;
  511. scmd->allowed = 1;
  512. break;
  513. case START_STOP:
  514. rq->timeout = START_STOP_TIMEOUT;
  515. break;
  516. case MOVE_MEDIUM:
  517. rq->timeout = MOVE_MEDIUM_TIMEOUT;
  518. break;
  519. case READ_ELEMENT_STATUS:
  520. rq->timeout = READ_ELEMENT_STATUS_TIMEOUT;
  521. break;
  522. case READ_DEFECT_DATA:
  523. rq->timeout = READ_DEFECT_DATA_TIMEOUT;
  524. scmd->allowed = 1;
  525. break;
  526. default:
  527. rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
  528. break;
  529. }
  530. if (bytes) {
  531. err = blk_rq_map_kern(rq, buffer, bytes, GFP_NOIO);
  532. if (err)
  533. goto error;
  534. }
  535. blk_execute_rq(rq, false);
  536. err = scmd->result & 0xff; /* only 8 bit SCSI status */
  537. if (err) {
  538. if (scmd->sense_len && scmd->sense_buffer) {
  539. /* limit sense len for backward compatibility */
  540. if (copy_to_user(sic->data, scmd->sense_buffer,
  541. min(scmd->sense_len, 16U)))
  542. err = -EFAULT;
  543. }
  544. } else {
  545. if (copy_to_user(sic->data, buffer, out_len))
  546. err = -EFAULT;
  547. }
  548. error:
  549. blk_mq_free_request(rq);
  550. error_free_buffer:
  551. kfree(buffer);
  552. return err;
  553. }
  554. int put_sg_io_hdr(const struct sg_io_hdr *hdr, void __user *argp)
  555. {
  556. #ifdef CONFIG_COMPAT
  557. if (in_compat_syscall()) {
  558. struct compat_sg_io_hdr hdr32 = {
  559. .interface_id = hdr->interface_id,
  560. .dxfer_direction = hdr->dxfer_direction,
  561. .cmd_len = hdr->cmd_len,
  562. .mx_sb_len = hdr->mx_sb_len,
  563. .iovec_count = hdr->iovec_count,
  564. .dxfer_len = hdr->dxfer_len,
  565. .dxferp = (uintptr_t)hdr->dxferp,
  566. .cmdp = (uintptr_t)hdr->cmdp,
  567. .sbp = (uintptr_t)hdr->sbp,
  568. .timeout = hdr->timeout,
  569. .flags = hdr->flags,
  570. .pack_id = hdr->pack_id,
  571. .usr_ptr = (uintptr_t)hdr->usr_ptr,
  572. .status = hdr->status,
  573. .masked_status = hdr->masked_status,
  574. .msg_status = hdr->msg_status,
  575. .sb_len_wr = hdr->sb_len_wr,
  576. .host_status = hdr->host_status,
  577. .driver_status = hdr->driver_status,
  578. .resid = hdr->resid,
  579. .duration = hdr->duration,
  580. .info = hdr->info,
  581. };
  582. if (copy_to_user(argp, &hdr32, sizeof(hdr32)))
  583. return -EFAULT;
  584. return 0;
  585. }
  586. #endif
  587. if (copy_to_user(argp, hdr, sizeof(*hdr)))
  588. return -EFAULT;
  589. return 0;
  590. }
  591. EXPORT_SYMBOL(put_sg_io_hdr);
  592. int get_sg_io_hdr(struct sg_io_hdr *hdr, const void __user *argp)
  593. {
  594. #ifdef CONFIG_COMPAT
  595. struct compat_sg_io_hdr hdr32;
  596. if (in_compat_syscall()) {
  597. if (copy_from_user(&hdr32, argp, sizeof(hdr32)))
  598. return -EFAULT;
  599. *hdr = (struct sg_io_hdr) {
  600. .interface_id = hdr32.interface_id,
  601. .dxfer_direction = hdr32.dxfer_direction,
  602. .cmd_len = hdr32.cmd_len,
  603. .mx_sb_len = hdr32.mx_sb_len,
  604. .iovec_count = hdr32.iovec_count,
  605. .dxfer_len = hdr32.dxfer_len,
  606. .dxferp = compat_ptr(hdr32.dxferp),
  607. .cmdp = compat_ptr(hdr32.cmdp),
  608. .sbp = compat_ptr(hdr32.sbp),
  609. .timeout = hdr32.timeout,
  610. .flags = hdr32.flags,
  611. .pack_id = hdr32.pack_id,
  612. .usr_ptr = compat_ptr(hdr32.usr_ptr),
  613. .status = hdr32.status,
  614. .masked_status = hdr32.masked_status,
  615. .msg_status = hdr32.msg_status,
  616. .sb_len_wr = hdr32.sb_len_wr,
  617. .host_status = hdr32.host_status,
  618. .driver_status = hdr32.driver_status,
  619. .resid = hdr32.resid,
  620. .duration = hdr32.duration,
  621. .info = hdr32.info,
  622. };
  623. return 0;
  624. }
  625. #endif
  626. if (copy_from_user(hdr, argp, sizeof(*hdr)))
  627. return -EFAULT;
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(get_sg_io_hdr);
  631. #ifdef CONFIG_COMPAT
  632. struct compat_cdrom_generic_command {
  633. unsigned char cmd[CDROM_PACKET_SIZE];
  634. compat_caddr_t buffer;
  635. compat_uint_t buflen;
  636. compat_int_t stat;
  637. compat_caddr_t sense;
  638. unsigned char data_direction;
  639. unsigned char pad[3];
  640. compat_int_t quiet;
  641. compat_int_t timeout;
  642. compat_caddr_t unused;
  643. };
  644. #endif
  645. static int scsi_get_cdrom_generic_arg(struct cdrom_generic_command *cgc,
  646. const void __user *arg)
  647. {
  648. #ifdef CONFIG_COMPAT
  649. if (in_compat_syscall()) {
  650. struct compat_cdrom_generic_command cgc32;
  651. if (copy_from_user(&cgc32, arg, sizeof(cgc32)))
  652. return -EFAULT;
  653. *cgc = (struct cdrom_generic_command) {
  654. .buffer = compat_ptr(cgc32.buffer),
  655. .buflen = cgc32.buflen,
  656. .stat = cgc32.stat,
  657. .sense = compat_ptr(cgc32.sense),
  658. .data_direction = cgc32.data_direction,
  659. .quiet = cgc32.quiet,
  660. .timeout = cgc32.timeout,
  661. .unused = compat_ptr(cgc32.unused),
  662. };
  663. memcpy(&cgc->cmd, &cgc32.cmd, CDROM_PACKET_SIZE);
  664. return 0;
  665. }
  666. #endif
  667. if (copy_from_user(cgc, arg, sizeof(*cgc)))
  668. return -EFAULT;
  669. return 0;
  670. }
  671. static int scsi_put_cdrom_generic_arg(const struct cdrom_generic_command *cgc,
  672. void __user *arg)
  673. {
  674. #ifdef CONFIG_COMPAT
  675. if (in_compat_syscall()) {
  676. struct compat_cdrom_generic_command cgc32 = {
  677. .buffer = (uintptr_t)(cgc->buffer),
  678. .buflen = cgc->buflen,
  679. .stat = cgc->stat,
  680. .sense = (uintptr_t)(cgc->sense),
  681. .data_direction = cgc->data_direction,
  682. .quiet = cgc->quiet,
  683. .timeout = cgc->timeout,
  684. .unused = (uintptr_t)(cgc->unused),
  685. };
  686. memcpy(&cgc32.cmd, &cgc->cmd, CDROM_PACKET_SIZE);
  687. if (copy_to_user(arg, &cgc32, sizeof(cgc32)))
  688. return -EFAULT;
  689. return 0;
  690. }
  691. #endif
  692. if (copy_to_user(arg, cgc, sizeof(*cgc)))
  693. return -EFAULT;
  694. return 0;
  695. }
  696. static int scsi_cdrom_send_packet(struct scsi_device *sdev, bool open_for_write,
  697. void __user *arg)
  698. {
  699. struct cdrom_generic_command cgc;
  700. struct sg_io_hdr hdr;
  701. int err;
  702. err = scsi_get_cdrom_generic_arg(&cgc, arg);
  703. if (err)
  704. return err;
  705. cgc.timeout = clock_t_to_jiffies(cgc.timeout);
  706. memset(&hdr, 0, sizeof(hdr));
  707. hdr.interface_id = 'S';
  708. hdr.cmd_len = sizeof(cgc.cmd);
  709. hdr.dxfer_len = cgc.buflen;
  710. switch (cgc.data_direction) {
  711. case CGC_DATA_UNKNOWN:
  712. hdr.dxfer_direction = SG_DXFER_UNKNOWN;
  713. break;
  714. case CGC_DATA_WRITE:
  715. hdr.dxfer_direction = SG_DXFER_TO_DEV;
  716. break;
  717. case CGC_DATA_READ:
  718. hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  719. break;
  720. case CGC_DATA_NONE:
  721. hdr.dxfer_direction = SG_DXFER_NONE;
  722. break;
  723. default:
  724. return -EINVAL;
  725. }
  726. hdr.dxferp = cgc.buffer;
  727. hdr.sbp = cgc.sense;
  728. if (hdr.sbp)
  729. hdr.mx_sb_len = sizeof(struct request_sense);
  730. hdr.timeout = jiffies_to_msecs(cgc.timeout);
  731. hdr.cmdp = ((struct cdrom_generic_command __user *) arg)->cmd;
  732. hdr.cmd_len = sizeof(cgc.cmd);
  733. err = sg_io(sdev, &hdr, open_for_write);
  734. if (err == -EFAULT)
  735. return -EFAULT;
  736. if (hdr.status)
  737. return -EIO;
  738. cgc.stat = err;
  739. cgc.buflen = hdr.resid;
  740. if (scsi_put_cdrom_generic_arg(&cgc, arg))
  741. return -EFAULT;
  742. return err;
  743. }
  744. static int scsi_ioctl_sg_io(struct scsi_device *sdev, bool open_for_write,
  745. void __user *argp)
  746. {
  747. struct sg_io_hdr hdr;
  748. int error;
  749. error = get_sg_io_hdr(&hdr, argp);
  750. if (error)
  751. return error;
  752. error = sg_io(sdev, &hdr, open_for_write);
  753. if (error == -EFAULT)
  754. return error;
  755. if (put_sg_io_hdr(&hdr, argp))
  756. return -EFAULT;
  757. return error;
  758. }
  759. /**
  760. * scsi_ioctl - Dispatch ioctl to scsi device
  761. * @sdev: scsi device receiving ioctl
  762. * @open_for_write: is the file / block device opened for writing?
  763. * @cmd: which ioctl is it
  764. * @arg: data associated with ioctl
  765. *
  766. * Description: The scsi_ioctl() function differs from most ioctls in that it
  767. * does not take a major/minor number as the dev field. Rather, it takes
  768. * a pointer to a &struct scsi_device.
  769. *
  770. * Return: varies depending on the @cmd
  771. */
  772. int scsi_ioctl(struct scsi_device *sdev, bool open_for_write, int cmd,
  773. void __user *arg)
  774. {
  775. struct request_queue *q = sdev->request_queue;
  776. struct scsi_sense_hdr sense_hdr;
  777. /* Check for deprecated ioctls ... all the ioctls which don't
  778. * follow the new unique numbering scheme are deprecated */
  779. switch (cmd) {
  780. case SCSI_IOCTL_SEND_COMMAND:
  781. case SCSI_IOCTL_TEST_UNIT_READY:
  782. case SCSI_IOCTL_BENCHMARK_COMMAND:
  783. case SCSI_IOCTL_SYNC:
  784. case SCSI_IOCTL_START_UNIT:
  785. case SCSI_IOCTL_STOP_UNIT:
  786. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  787. "ioctl, please convert it to SG_IO\n", current->comm);
  788. break;
  789. default:
  790. break;
  791. }
  792. switch (cmd) {
  793. case SG_GET_VERSION_NUM:
  794. return sg_get_version(arg);
  795. case SG_SET_TIMEOUT:
  796. return sg_set_timeout(sdev, arg);
  797. case SG_GET_TIMEOUT:
  798. return jiffies_to_clock_t(sdev->sg_timeout);
  799. case SG_GET_RESERVED_SIZE:
  800. return sg_get_reserved_size(sdev, arg);
  801. case SG_SET_RESERVED_SIZE:
  802. return sg_set_reserved_size(sdev, arg);
  803. case SG_EMULATED_HOST:
  804. return sg_emulated_host(q, arg);
  805. case SG_IO:
  806. return scsi_ioctl_sg_io(sdev, open_for_write, arg);
  807. case SCSI_IOCTL_SEND_COMMAND:
  808. return sg_scsi_ioctl(q, open_for_write, arg);
  809. case CDROM_SEND_PACKET:
  810. return scsi_cdrom_send_packet(sdev, open_for_write, arg);
  811. case CDROMCLOSETRAY:
  812. return scsi_send_start_stop(sdev, 3);
  813. case CDROMEJECT:
  814. return scsi_send_start_stop(sdev, 2);
  815. case SCSI_IOCTL_GET_IDLUN:
  816. return scsi_get_idlun(sdev, arg);
  817. case SCSI_IOCTL_GET_BUS_NUMBER:
  818. return put_user(sdev->host->host_no, (int __user *)arg);
  819. case SCSI_IOCTL_PROBE_HOST:
  820. return ioctl_probe(sdev->host, arg);
  821. case SCSI_IOCTL_DOORLOCK:
  822. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  823. case SCSI_IOCTL_DOORUNLOCK:
  824. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  825. case SCSI_IOCTL_TEST_UNIT_READY:
  826. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  827. NORMAL_RETRIES, &sense_hdr);
  828. case SCSI_IOCTL_START_UNIT:
  829. return scsi_send_start_stop(sdev, 1);
  830. case SCSI_IOCTL_STOP_UNIT:
  831. return scsi_send_start_stop(sdev, 0);
  832. case SCSI_IOCTL_GET_PCI:
  833. return scsi_ioctl_get_pci(sdev, arg);
  834. case SG_SCSI_RESET:
  835. return scsi_ioctl_reset(sdev, arg);
  836. }
  837. #ifdef CONFIG_COMPAT
  838. if (in_compat_syscall()) {
  839. if (!sdev->host->hostt->compat_ioctl)
  840. return -EINVAL;
  841. return sdev->host->hostt->compat_ioctl(sdev, cmd, arg);
  842. }
  843. #endif
  844. if (!sdev->host->hostt->ioctl)
  845. return -EINVAL;
  846. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  847. }
  848. EXPORT_SYMBOL(scsi_ioctl);
  849. /**
  850. * scsi_ioctl_block_when_processing_errors - prevent commands from being queued
  851. * @sdev: target scsi device
  852. * @cmd: which ioctl is it
  853. * @ndelay: no delay (non-blocking)
  854. *
  855. * We can process a reset even when a device isn't fully operable.
  856. *
  857. * Return: %0 on success, <0 error code.
  858. */
  859. int scsi_ioctl_block_when_processing_errors(struct scsi_device *sdev, int cmd,
  860. bool ndelay)
  861. {
  862. if (cmd == SG_SCSI_RESET && ndelay) {
  863. if (scsi_host_in_recovery(sdev->host))
  864. return -EAGAIN;
  865. } else {
  866. if (!scsi_block_when_processing_errors(sdev))
  867. return -ENODEV;
  868. }
  869. return 0;
  870. }
  871. EXPORT_SYMBOL_GPL(scsi_ioctl_block_when_processing_errors);