scsi_ioctl.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, printk("Trying ioctl with scsi command %d\n", *cmd));
  84. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
  85. &sshdr, timeout, retries, NULL);
  86. SCSI_LOG_IOCTL(2, printk("Ioctl returned 0x%x\n", result));
  87. if ((driver_byte(result) & DRIVER_SENSE) &&
  88. (scsi_sense_valid(&sshdr))) {
  89. switch (sshdr.sense_key) {
  90. case ILLEGAL_REQUEST:
  91. if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
  92. sdev->lockable = 0;
  93. else
  94. printk(KERN_INFO "ioctl_internal_command: "
  95. "ILLEGAL REQUEST asc=0x%x ascq=0x%x\n",
  96. sshdr.asc, sshdr.ascq);
  97. break;
  98. case NOT_READY: /* This happens if there is no disc in drive */
  99. if (sdev->removable)
  100. break;
  101. case UNIT_ATTENTION:
  102. if (sdev->removable) {
  103. sdev->changed = 1;
  104. result = 0; /* This is no longer considered an error */
  105. break;
  106. }
  107. default: /* Fall through for non-removable media */
  108. sdev_printk(KERN_INFO, sdev,
  109. "ioctl_internal_command return code = %x\n",
  110. result);
  111. scsi_print_sense_hdr(" ", &sshdr);
  112. break;
  113. }
  114. }
  115. SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
  116. return result;
  117. }
  118. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  119. {
  120. char scsi_cmd[MAX_COMMAND_SIZE];
  121. int ret;
  122. if (!sdev->removable || !sdev->lockable)
  123. return 0;
  124. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  125. scsi_cmd[1] = 0;
  126. scsi_cmd[2] = 0;
  127. scsi_cmd[3] = 0;
  128. scsi_cmd[4] = state;
  129. scsi_cmd[5] = 0;
  130. ret = ioctl_internal_command(sdev, scsi_cmd,
  131. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  132. if (ret == 0)
  133. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL(scsi_set_medium_removal);
  137. /*
  138. * The scsi_ioctl_get_pci() function places into arg the value
  139. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  140. * Returns: 0 on success
  141. * -ENXIO if there isn't a PCI device pointer
  142. * (could be because the SCSI driver hasn't been
  143. * updated yet, or because it isn't a SCSI
  144. * device)
  145. * any copy_to_user() error on failure there
  146. */
  147. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  148. {
  149. struct device *dev = scsi_get_device(sdev->host);
  150. const char *name;
  151. if (!dev)
  152. return -ENXIO;
  153. name = dev_name(dev);
  154. /* compatibility with old ioctl which only returned
  155. * 20 characters */
  156. return copy_to_user(arg, name, min(strlen(name), (size_t)20))
  157. ? -EFAULT: 0;
  158. }
  159. /**
  160. * scsi_ioctl - Dispatch ioctl to scsi device
  161. * @sdev: scsi device receiving ioctl
  162. * @cmd: which ioctl is it
  163. * @arg: data associated with ioctl
  164. *
  165. * Description: The scsi_ioctl() function differs from most ioctls in that it
  166. * does not take a major/minor number as the dev field. Rather, it takes
  167. * a pointer to a &struct scsi_device.
  168. */
  169. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  170. {
  171. char scsi_cmd[MAX_COMMAND_SIZE];
  172. /* No idea how this happens.... */
  173. if (!sdev)
  174. return -ENXIO;
  175. /*
  176. * If we are in the middle of error recovery, don't let anyone
  177. * else try and use this device. Also, if error recovery fails, it
  178. * may try and take the device offline, in which case all further
  179. * access to the device is prohibited.
  180. */
  181. if (!scsi_block_when_processing_errors(sdev))
  182. return -ENODEV;
  183. /* Check for deprecated ioctls ... all the ioctls which don't
  184. * follow the new unique numbering scheme are deprecated */
  185. switch (cmd) {
  186. case SCSI_IOCTL_SEND_COMMAND:
  187. case SCSI_IOCTL_TEST_UNIT_READY:
  188. case SCSI_IOCTL_BENCHMARK_COMMAND:
  189. case SCSI_IOCTL_SYNC:
  190. case SCSI_IOCTL_START_UNIT:
  191. case SCSI_IOCTL_STOP_UNIT:
  192. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  193. "ioctl, please convert it to SG_IO\n", current->comm);
  194. break;
  195. default:
  196. break;
  197. }
  198. switch (cmd) {
  199. case SCSI_IOCTL_GET_IDLUN:
  200. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  201. return -EFAULT;
  202. __put_user((sdev->id & 0xff)
  203. + ((sdev->lun & 0xff) << 8)
  204. + ((sdev->channel & 0xff) << 16)
  205. + ((sdev->host->host_no & 0xff) << 24),
  206. &((struct scsi_idlun __user *)arg)->dev_id);
  207. __put_user(sdev->host->unique_id,
  208. &((struct scsi_idlun __user *)arg)->host_unique_id);
  209. return 0;
  210. case SCSI_IOCTL_GET_BUS_NUMBER:
  211. return put_user(sdev->host->host_no, (int __user *)arg);
  212. case SCSI_IOCTL_PROBE_HOST:
  213. return ioctl_probe(sdev->host, arg);
  214. case SCSI_IOCTL_SEND_COMMAND:
  215. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  216. return -EACCES;
  217. return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
  218. case SCSI_IOCTL_DOORLOCK:
  219. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  220. case SCSI_IOCTL_DOORUNLOCK:
  221. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  222. case SCSI_IOCTL_TEST_UNIT_READY:
  223. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  224. NORMAL_RETRIES, NULL);
  225. case SCSI_IOCTL_START_UNIT:
  226. scsi_cmd[0] = START_STOP;
  227. scsi_cmd[1] = 0;
  228. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  229. scsi_cmd[4] = 1;
  230. return ioctl_internal_command(sdev, scsi_cmd,
  231. START_STOP_TIMEOUT, NORMAL_RETRIES);
  232. case SCSI_IOCTL_STOP_UNIT:
  233. scsi_cmd[0] = START_STOP;
  234. scsi_cmd[1] = 0;
  235. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  236. scsi_cmd[4] = 0;
  237. return ioctl_internal_command(sdev, scsi_cmd,
  238. START_STOP_TIMEOUT, NORMAL_RETRIES);
  239. case SCSI_IOCTL_GET_PCI:
  240. return scsi_ioctl_get_pci(sdev, arg);
  241. default:
  242. if (sdev->host->hostt->ioctl)
  243. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  244. }
  245. return -EINVAL;
  246. }
  247. EXPORT_SYMBOL(scsi_ioctl);
  248. /**
  249. * scsi_nonblockable_ioctl() - Handle SG_SCSI_RESET
  250. * @sdev: scsi device receiving ioctl
  251. * @cmd: Must be SC_SCSI_RESET
  252. * @arg: pointer to int containing SG_SCSI_RESET_{DEVICE,BUS,HOST}
  253. * @ndelay: file mode O_NDELAY flag
  254. */
  255. int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
  256. void __user *arg, int ndelay)
  257. {
  258. int val, result;
  259. /* The first set of iocts may be executed even if we're doing
  260. * error processing, as long as the device was opened
  261. * non-blocking */
  262. if (ndelay) {
  263. if (scsi_host_in_recovery(sdev->host))
  264. return -ENODEV;
  265. } else if (!scsi_block_when_processing_errors(sdev))
  266. return -ENODEV;
  267. switch (cmd) {
  268. case SG_SCSI_RESET:
  269. result = get_user(val, (int __user *)arg);
  270. if (result)
  271. return result;
  272. if (val == SG_SCSI_RESET_NOTHING)
  273. return 0;
  274. switch (val) {
  275. case SG_SCSI_RESET_DEVICE:
  276. val = SCSI_TRY_RESET_DEVICE;
  277. break;
  278. case SG_SCSI_RESET_TARGET:
  279. val = SCSI_TRY_RESET_TARGET;
  280. break;
  281. case SG_SCSI_RESET_BUS:
  282. val = SCSI_TRY_RESET_BUS;
  283. break;
  284. case SG_SCSI_RESET_HOST:
  285. val = SCSI_TRY_RESET_HOST;
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  291. return -EACCES;
  292. return (scsi_reset_provider(sdev, val) ==
  293. SUCCESS) ? 0 : -EIO;
  294. }
  295. return -ENODEV;
  296. }
  297. EXPORT_SYMBOL(scsi_nonblockable_ioctl);