scsi_ioctl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Changes:
  3. * Arnaldo Carvalho de Melo <acme@conectiva.com.br> 08/23/2000
  4. * - get rid of some verify_areas and use __copy*user and __get/put_user
  5. * for the ones that remain
  6. */
  7. #include <linux/module.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/string.h>
  15. #include <asm/uaccess.h>
  16. #include <scsi/scsi.h>
  17. #include <scsi/scsi_cmnd.h>
  18. #include <scsi/scsi_device.h>
  19. #include <scsi/scsi_eh.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_ioctl.h>
  22. #include <scsi/sg.h>
  23. #include <scsi/scsi_dbg.h>
  24. #include "scsi_logging.h"
  25. #define NORMAL_RETRIES 5
  26. #define IOCTL_NORMAL_TIMEOUT (10 * HZ)
  27. #define MAX_BUF PAGE_SIZE
  28. /**
  29. * ioctl_probe -- return host identification
  30. * @host: host to identify
  31. * @buffer: userspace buffer for identification
  32. *
  33. * Return an identifying string at @buffer, if @buffer is non-NULL, filling
  34. * to the length stored at * (int *) @buffer.
  35. */
  36. static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
  37. {
  38. unsigned int len, slen;
  39. const char *string;
  40. if (buffer) {
  41. if (get_user(len, (unsigned int __user *) buffer))
  42. return -EFAULT;
  43. if (host->hostt->info)
  44. string = host->hostt->info(host);
  45. else
  46. string = host->hostt->name;
  47. if (string) {
  48. slen = strlen(string);
  49. if (len > slen)
  50. len = slen + 1;
  51. if (copy_to_user(buffer, string, len))
  52. return -EFAULT;
  53. }
  54. }
  55. return 1;
  56. }
  57. /*
  58. * The SCSI_IOCTL_SEND_COMMAND ioctl sends a command out to the SCSI host.
  59. * The IOCTL_NORMAL_TIMEOUT and NORMAL_RETRIES variables are used.
  60. *
  61. * dev is the SCSI device struct ptr, *(int *) arg is the length of the
  62. * input data, if any, not including the command string & counts,
  63. * *((int *)arg + 1) is the output buffer size in bytes.
  64. *
  65. * *(char *) ((int *) arg)[2] the actual command byte.
  66. *
  67. * Note that if more than MAX_BUF bytes are requested to be transferred,
  68. * the ioctl will fail with error EINVAL.
  69. *
  70. * This size *does not* include the initial lengths that were passed.
  71. *
  72. * The SCSI command is read from the memory location immediately after the
  73. * length words, and the input data is right after the command. The SCSI
  74. * routines know the command size based on the opcode decode.
  75. *
  76. * The output area is then filled in starting from the command byte.
  77. */
  78. static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
  79. int timeout, int retries)
  80. {
  81. int result;
  82. struct scsi_sense_hdr sshdr;
  83. SCSI_LOG_IOCTL(1, sdev_printk(KERN_INFO, sdev,
  84. "Trying ioctl with scsi command %d\n", *cmd));
  85. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
  86. &sshdr, timeout, retries, NULL);
  87. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  88. "Ioctl returned 0x%x\n", result));
  89. if ((driver_byte(result) & DRIVER_SENSE) &&
  90. (scsi_sense_valid(&sshdr))) {
  91. switch (sshdr.sense_key) {
  92. case ILLEGAL_REQUEST:
  93. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  94. sdev->lockable = 0;
  95. else
  96. sdev_printk(KERN_INFO, sdev,
  97. "ioctl_internal_command: "
  98. "ILLEGAL REQUEST "
  99. "asc=0x%x ascq=0x%x\n",
  100. sshdr.asc, sshdr.ascq);
  101. break;
  102. case NOT_READY: /* This happens if there is no disc in drive */
  103. if (sdev->removable)
  104. break;
  105. case UNIT_ATTENTION:
  106. if (sdev->removable) {
  107. sdev->changed = 1;
  108. result = 0; /* This is no longer considered an error */
  109. break;
  110. }
  111. default: /* Fall through for non-removable media */
  112. sdev_printk(KERN_INFO, sdev,
  113. "ioctl_internal_command return code = %x\n",
  114. result);
  115. scsi_print_sense_hdr(" ", &sshdr);
  116. break;
  117. }
  118. }
  119. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  120. "IOCTL Releasing command\n"));
  121. return result;
  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. /**
  165. * scsi_ioctl - Dispatch ioctl to scsi device
  166. * @sdev: scsi device receiving ioctl
  167. * @cmd: which ioctl is it
  168. * @arg: data associated with ioctl
  169. *
  170. * Description: The scsi_ioctl() function differs from most ioctls in that it
  171. * does not take a major/minor number as the dev field. Rather, it takes
  172. * a pointer to a &struct scsi_device.
  173. */
  174. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  175. {
  176. char scsi_cmd[MAX_COMMAND_SIZE];
  177. /* No idea how this happens.... */
  178. if (!sdev)
  179. return -ENXIO;
  180. /*
  181. * If we are in the middle of error recovery, don't let anyone
  182. * else try and use this device. Also, if error recovery fails, it
  183. * may try and take the device offline, in which case all further
  184. * access to the device is prohibited.
  185. */
  186. if (!scsi_block_when_processing_errors(sdev))
  187. return -ENODEV;
  188. /* Check for deprecated ioctls ... all the ioctls which don't
  189. * follow the new unique numbering scheme are deprecated */
  190. switch (cmd) {
  191. case SCSI_IOCTL_SEND_COMMAND:
  192. case SCSI_IOCTL_TEST_UNIT_READY:
  193. case SCSI_IOCTL_BENCHMARK_COMMAND:
  194. case SCSI_IOCTL_SYNC:
  195. case SCSI_IOCTL_START_UNIT:
  196. case SCSI_IOCTL_STOP_UNIT:
  197. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  198. "ioctl, please convert it to SG_IO\n", current->comm);
  199. break;
  200. default:
  201. break;
  202. }
  203. switch (cmd) {
  204. case SCSI_IOCTL_GET_IDLUN:
  205. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  206. return -EFAULT;
  207. __put_user((sdev->id & 0xff)
  208. + ((sdev->lun & 0xff) << 8)
  209. + ((sdev->channel & 0xff) << 16)
  210. + ((sdev->host->host_no & 0xff) << 24),
  211. &((struct scsi_idlun __user *)arg)->dev_id);
  212. __put_user(sdev->host->unique_id,
  213. &((struct scsi_idlun __user *)arg)->host_unique_id);
  214. return 0;
  215. case SCSI_IOCTL_GET_BUS_NUMBER:
  216. return put_user(sdev->host->host_no, (int __user *)arg);
  217. case SCSI_IOCTL_PROBE_HOST:
  218. return ioctl_probe(sdev->host, arg);
  219. case SCSI_IOCTL_SEND_COMMAND:
  220. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  221. return -EACCES;
  222. return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
  223. case SCSI_IOCTL_DOORLOCK:
  224. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  225. case SCSI_IOCTL_DOORUNLOCK:
  226. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  227. case SCSI_IOCTL_TEST_UNIT_READY:
  228. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  229. NORMAL_RETRIES, NULL);
  230. case SCSI_IOCTL_START_UNIT:
  231. scsi_cmd[0] = START_STOP;
  232. scsi_cmd[1] = 0;
  233. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  234. scsi_cmd[4] = 1;
  235. return ioctl_internal_command(sdev, scsi_cmd,
  236. START_STOP_TIMEOUT, NORMAL_RETRIES);
  237. case SCSI_IOCTL_STOP_UNIT:
  238. scsi_cmd[0] = START_STOP;
  239. scsi_cmd[1] = 0;
  240. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  241. scsi_cmd[4] = 0;
  242. return ioctl_internal_command(sdev, scsi_cmd,
  243. START_STOP_TIMEOUT, NORMAL_RETRIES);
  244. case SCSI_IOCTL_GET_PCI:
  245. return scsi_ioctl_get_pci(sdev, arg);
  246. default:
  247. if (sdev->host->hostt->ioctl)
  248. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  249. }
  250. return -EINVAL;
  251. }
  252. EXPORT_SYMBOL(scsi_ioctl);
  253. /**
  254. * scsi_nonblockable_ioctl() - Handle SG_SCSI_RESET
  255. * @sdev: scsi device receiving ioctl
  256. * @cmd: Must be SC_SCSI_RESET
  257. * @arg: pointer to int containing SG_SCSI_RESET_{DEVICE,BUS,HOST}
  258. * @ndelay: file mode O_NDELAY flag
  259. */
  260. int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
  261. void __user *arg, int ndelay)
  262. {
  263. int val, result;
  264. /* The first set of iocts may be executed even if we're doing
  265. * error processing, as long as the device was opened
  266. * non-blocking */
  267. if (ndelay) {
  268. if (scsi_host_in_recovery(sdev->host))
  269. return -ENODEV;
  270. } else if (!scsi_block_when_processing_errors(sdev))
  271. return -ENODEV;
  272. switch (cmd) {
  273. case SG_SCSI_RESET:
  274. result = get_user(val, (int __user *)arg);
  275. if (result)
  276. return result;
  277. if (val == SG_SCSI_RESET_NOTHING)
  278. return 0;
  279. switch (val) {
  280. case SG_SCSI_RESET_DEVICE:
  281. val = SCSI_TRY_RESET_DEVICE;
  282. break;
  283. case SG_SCSI_RESET_TARGET:
  284. val = SCSI_TRY_RESET_TARGET;
  285. break;
  286. case SG_SCSI_RESET_BUS:
  287. val = SCSI_TRY_RESET_BUS;
  288. break;
  289. case SG_SCSI_RESET_HOST:
  290. val = SCSI_TRY_RESET_HOST;
  291. break;
  292. default:
  293. return -EINVAL;
  294. }
  295. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  296. return -EACCES;
  297. return (scsi_reset_provider(sdev, val) ==
  298. SUCCESS) ? 0 : -EIO;
  299. }
  300. return -ENODEV;
  301. }
  302. EXPORT_SYMBOL(scsi_nonblockable_ioctl);