scsi_ioctl.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <linux/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. /* FALLTHROUGH */
  106. case UNIT_ATTENTION:
  107. if (sdev->removable) {
  108. sdev->changed = 1;
  109. result = 0; /* This is no longer considered an error */
  110. break;
  111. }
  112. /* FALLTHROUGH -- for non-removable media */
  113. default:
  114. sdev_printk(KERN_INFO, sdev,
  115. "ioctl_internal_command return code = %x\n",
  116. result);
  117. scsi_print_sense_hdr(sdev, NULL, &sshdr);
  118. break;
  119. }
  120. }
  121. SCSI_LOG_IOCTL(2, sdev_printk(KERN_INFO, sdev,
  122. "IOCTL Releasing command\n"));
  123. return result;
  124. }
  125. int scsi_set_medium_removal(struct scsi_device *sdev, char state)
  126. {
  127. char scsi_cmd[MAX_COMMAND_SIZE];
  128. int ret;
  129. if (!sdev->removable || !sdev->lockable)
  130. return 0;
  131. scsi_cmd[0] = ALLOW_MEDIUM_REMOVAL;
  132. scsi_cmd[1] = 0;
  133. scsi_cmd[2] = 0;
  134. scsi_cmd[3] = 0;
  135. scsi_cmd[4] = state;
  136. scsi_cmd[5] = 0;
  137. ret = ioctl_internal_command(sdev, scsi_cmd,
  138. IOCTL_NORMAL_TIMEOUT, NORMAL_RETRIES);
  139. if (ret == 0)
  140. sdev->locked = (state == SCSI_REMOVAL_PREVENT);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(scsi_set_medium_removal);
  144. /*
  145. * The scsi_ioctl_get_pci() function places into arg the value
  146. * pci_dev::slot_name (8 characters) for the PCI device (if any).
  147. * Returns: 0 on success
  148. * -ENXIO if there isn't a PCI device pointer
  149. * (could be because the SCSI driver hasn't been
  150. * updated yet, or because it isn't a SCSI
  151. * device)
  152. * any copy_to_user() error on failure there
  153. */
  154. static int scsi_ioctl_get_pci(struct scsi_device *sdev, void __user *arg)
  155. {
  156. struct device *dev = scsi_get_device(sdev->host);
  157. const char *name;
  158. if (!dev)
  159. return -ENXIO;
  160. name = dev_name(dev);
  161. /* compatibility with old ioctl which only returned
  162. * 20 characters */
  163. return copy_to_user(arg, name, min(strlen(name), (size_t)20))
  164. ? -EFAULT: 0;
  165. }
  166. /**
  167. * scsi_ioctl - Dispatch ioctl to scsi device
  168. * @sdev: scsi device receiving ioctl
  169. * @cmd: which ioctl is it
  170. * @arg: data associated with ioctl
  171. *
  172. * Description: The scsi_ioctl() function differs from most ioctls in that it
  173. * does not take a major/minor number as the dev field. Rather, it takes
  174. * a pointer to a &struct scsi_device.
  175. */
  176. int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
  177. {
  178. char scsi_cmd[MAX_COMMAND_SIZE];
  179. struct scsi_sense_hdr sense_hdr;
  180. /* Check for deprecated ioctls ... all the ioctls which don't
  181. * follow the new unique numbering scheme are deprecated */
  182. switch (cmd) {
  183. case SCSI_IOCTL_SEND_COMMAND:
  184. case SCSI_IOCTL_TEST_UNIT_READY:
  185. case SCSI_IOCTL_BENCHMARK_COMMAND:
  186. case SCSI_IOCTL_SYNC:
  187. case SCSI_IOCTL_START_UNIT:
  188. case SCSI_IOCTL_STOP_UNIT:
  189. printk(KERN_WARNING "program %s is using a deprecated SCSI "
  190. "ioctl, please convert it to SG_IO\n", current->comm);
  191. break;
  192. default:
  193. break;
  194. }
  195. switch (cmd) {
  196. case SCSI_IOCTL_GET_IDLUN:
  197. if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
  198. return -EFAULT;
  199. __put_user((sdev->id & 0xff)
  200. + ((sdev->lun & 0xff) << 8)
  201. + ((sdev->channel & 0xff) << 16)
  202. + ((sdev->host->host_no & 0xff) << 24),
  203. &((struct scsi_idlun __user *)arg)->dev_id);
  204. __put_user(sdev->host->unique_id,
  205. &((struct scsi_idlun __user *)arg)->host_unique_id);
  206. return 0;
  207. case SCSI_IOCTL_GET_BUS_NUMBER:
  208. return put_user(sdev->host->host_no, (int __user *)arg);
  209. case SCSI_IOCTL_PROBE_HOST:
  210. return ioctl_probe(sdev->host, arg);
  211. case SCSI_IOCTL_SEND_COMMAND:
  212. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  213. return -EACCES;
  214. return sg_scsi_ioctl(sdev->request_queue, NULL, 0, arg);
  215. case SCSI_IOCTL_DOORLOCK:
  216. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  217. case SCSI_IOCTL_DOORUNLOCK:
  218. return scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  219. case SCSI_IOCTL_TEST_UNIT_READY:
  220. return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
  221. NORMAL_RETRIES, &sense_hdr);
  222. case SCSI_IOCTL_START_UNIT:
  223. scsi_cmd[0] = START_STOP;
  224. scsi_cmd[1] = 0;
  225. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  226. scsi_cmd[4] = 1;
  227. return ioctl_internal_command(sdev, scsi_cmd,
  228. START_STOP_TIMEOUT, NORMAL_RETRIES);
  229. case SCSI_IOCTL_STOP_UNIT:
  230. scsi_cmd[0] = START_STOP;
  231. scsi_cmd[1] = 0;
  232. scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  233. scsi_cmd[4] = 0;
  234. return ioctl_internal_command(sdev, scsi_cmd,
  235. START_STOP_TIMEOUT, NORMAL_RETRIES);
  236. case SCSI_IOCTL_GET_PCI:
  237. return scsi_ioctl_get_pci(sdev, arg);
  238. case SG_SCSI_RESET:
  239. return scsi_ioctl_reset(sdev, arg);
  240. default:
  241. if (sdev->host->hostt->ioctl)
  242. return sdev->host->hostt->ioctl(sdev, cmd, arg);
  243. }
  244. return -EINVAL;
  245. }
  246. EXPORT_SYMBOL(scsi_ioctl);
  247. /*
  248. * We can process a reset even when a device isn't fully operable.
  249. */
  250. int scsi_ioctl_block_when_processing_errors(struct scsi_device *sdev, int cmd,
  251. bool ndelay)
  252. {
  253. if (cmd == SG_SCSI_RESET && ndelay) {
  254. if (scsi_host_in_recovery(sdev->host))
  255. return -EAGAIN;
  256. } else {
  257. if (!scsi_block_when_processing_errors(sdev))
  258. return -ENODEV;
  259. }
  260. return 0;
  261. }
  262. EXPORT_SYMBOL_GPL(scsi_ioctl_block_when_processing_errors);