dasd_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * File...........: linux/drivers/s390/block/dasd_ioctl.c
  3. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  4. * Horst Hummel <Horst.Hummel@de.ibm.com>
  5. * Carsten Otte <Cotte@de.ibm.com>
  6. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Bugreports.to..: <Linux390@de.ibm.com>
  8. * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
  9. *
  10. * i/o controls for the dasd driver.
  11. */
  12. #define KMSG_COMPONENT "dasd"
  13. #include <linux/interrupt.h>
  14. #include <linux/major.h>
  15. #include <linux/fs.h>
  16. #include <linux/blkpg.h>
  17. #include <linux/smp_lock.h>
  18. #include <asm/ccwdev.h>
  19. #include <asm/cmb.h>
  20. #include <asm/uaccess.h>
  21. /* This is ugly... */
  22. #define PRINTK_HEADER "dasd_ioctl:"
  23. #include "dasd_int.h"
  24. static int
  25. dasd_ioctl_api_version(void __user *argp)
  26. {
  27. int ver = DASD_API_VERSION;
  28. return put_user(ver, (int __user *)argp);
  29. }
  30. /*
  31. * Enable device.
  32. * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
  33. */
  34. static int
  35. dasd_ioctl_enable(struct block_device *bdev)
  36. {
  37. struct dasd_block *block = bdev->bd_disk->private_data;
  38. if (!capable(CAP_SYS_ADMIN))
  39. return -EACCES;
  40. dasd_enable_device(block->base);
  41. /* Formatting the dasd device can change the capacity. */
  42. mutex_lock(&bdev->bd_mutex);
  43. i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
  44. mutex_unlock(&bdev->bd_mutex);
  45. return 0;
  46. }
  47. /*
  48. * Disable device.
  49. * Used by dasdfmt. Disable I/O operations but allow ioctls.
  50. */
  51. static int
  52. dasd_ioctl_disable(struct block_device *bdev)
  53. {
  54. struct dasd_block *block = bdev->bd_disk->private_data;
  55. if (!capable(CAP_SYS_ADMIN))
  56. return -EACCES;
  57. /*
  58. * Man this is sick. We don't do a real disable but only downgrade
  59. * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
  60. * BIODASDDISABLE to disable accesses to the device via the block
  61. * device layer but it still wants to do i/o on the device by
  62. * using the BIODASDFMT ioctl. Therefore the correct state for the
  63. * device is DASD_STATE_BASIC that allows to do basic i/o.
  64. */
  65. dasd_set_target_state(block->base, DASD_STATE_BASIC);
  66. /*
  67. * Set i_size to zero, since read, write, etc. check against this
  68. * value.
  69. */
  70. mutex_lock(&bdev->bd_mutex);
  71. i_size_write(bdev->bd_inode, 0);
  72. mutex_unlock(&bdev->bd_mutex);
  73. return 0;
  74. }
  75. /*
  76. * Quiesce device.
  77. */
  78. static int dasd_ioctl_quiesce(struct dasd_block *block)
  79. {
  80. unsigned long flags;
  81. struct dasd_device *base;
  82. base = block->base;
  83. if (!capable (CAP_SYS_ADMIN))
  84. return -EACCES;
  85. pr_info("%s: The DASD has been put in the quiesce "
  86. "state\n", dev_name(&base->cdev->dev));
  87. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  88. base->stopped |= DASD_STOPPED_QUIESCE;
  89. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
  90. return 0;
  91. }
  92. /*
  93. * Resume device.
  94. */
  95. static int dasd_ioctl_resume(struct dasd_block *block)
  96. {
  97. unsigned long flags;
  98. struct dasd_device *base;
  99. base = block->base;
  100. if (!capable (CAP_SYS_ADMIN))
  101. return -EACCES;
  102. pr_info("%s: I/O operations have been resumed "
  103. "on the DASD\n", dev_name(&base->cdev->dev));
  104. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  105. base->stopped &= ~DASD_STOPPED_QUIESCE;
  106. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
  107. dasd_schedule_block_bh(block);
  108. return 0;
  109. }
  110. /*
  111. * performs formatting of _device_ according to _fdata_
  112. * Note: The discipline's format_function is assumed to deliver formatting
  113. * commands to format a single unit of the device. In terms of the ECKD
  114. * devices this means CCWs are generated to format a single track.
  115. */
  116. static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
  117. {
  118. struct dasd_ccw_req *cqr;
  119. struct dasd_device *base;
  120. int rc;
  121. base = block->base;
  122. if (base->discipline->format_device == NULL)
  123. return -EPERM;
  124. if (base->state != DASD_STATE_BASIC) {
  125. pr_warning("%s: The DASD cannot be formatted while it is "
  126. "enabled\n", dev_name(&base->cdev->dev));
  127. return -EBUSY;
  128. }
  129. DBF_DEV_EVENT(DBF_NOTICE, base,
  130. "formatting units %u to %u (%u B blocks) flags %u",
  131. fdata->start_unit,
  132. fdata->stop_unit, fdata->blksize, fdata->intensity);
  133. /* Since dasdfmt keeps the device open after it was disabled,
  134. * there still exists an inode for this device.
  135. * We must update i_blkbits, otherwise we might get errors when
  136. * enabling the device later.
  137. */
  138. if (fdata->start_unit == 0) {
  139. struct block_device *bdev = bdget_disk(block->gdp, 0);
  140. bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
  141. bdput(bdev);
  142. }
  143. while (fdata->start_unit <= fdata->stop_unit) {
  144. cqr = base->discipline->format_device(base, fdata);
  145. if (IS_ERR(cqr))
  146. return PTR_ERR(cqr);
  147. rc = dasd_sleep_on_interruptible(cqr);
  148. dasd_sfree_request(cqr, cqr->memdev);
  149. if (rc) {
  150. if (rc != -ERESTARTSYS)
  151. pr_err("%s: Formatting unit %d failed with "
  152. "rc=%d\n", dev_name(&base->cdev->dev),
  153. fdata->start_unit, rc);
  154. return rc;
  155. }
  156. fdata->start_unit++;
  157. }
  158. return 0;
  159. }
  160. /*
  161. * Format device.
  162. */
  163. static int
  164. dasd_ioctl_format(struct block_device *bdev, void __user *argp)
  165. {
  166. struct dasd_block *block = bdev->bd_disk->private_data;
  167. struct format_data_t fdata;
  168. if (!capable(CAP_SYS_ADMIN))
  169. return -EACCES;
  170. if (!argp)
  171. return -EINVAL;
  172. if (block->base->features & DASD_FEATURE_READONLY)
  173. return -EROFS;
  174. if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
  175. return -EFAULT;
  176. if (bdev != bdev->bd_contains) {
  177. pr_warning("%s: The specified DASD is a partition and cannot "
  178. "be formatted\n",
  179. dev_name(&block->base->cdev->dev));
  180. return -EINVAL;
  181. }
  182. return dasd_format(block, &fdata);
  183. }
  184. #ifdef CONFIG_DASD_PROFILE
  185. /*
  186. * Reset device profile information
  187. */
  188. static int dasd_ioctl_reset_profile(struct dasd_block *block)
  189. {
  190. memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
  191. return 0;
  192. }
  193. /*
  194. * Return device profile information
  195. */
  196. static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
  197. {
  198. if (dasd_profile_level == DASD_PROFILE_OFF)
  199. return -EIO;
  200. if (copy_to_user(argp, &block->profile,
  201. sizeof(struct dasd_profile_info_t)))
  202. return -EFAULT;
  203. return 0;
  204. }
  205. #else
  206. static int dasd_ioctl_reset_profile(struct dasd_block *block)
  207. {
  208. return -ENOSYS;
  209. }
  210. static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
  211. {
  212. return -ENOSYS;
  213. }
  214. #endif
  215. /*
  216. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  217. */
  218. static int dasd_ioctl_information(struct dasd_block *block,
  219. unsigned int cmd, void __user *argp)
  220. {
  221. struct dasd_information2_t *dasd_info;
  222. unsigned long flags;
  223. int rc;
  224. struct dasd_device *base;
  225. struct ccw_device *cdev;
  226. struct ccw_dev_id dev_id;
  227. base = block->base;
  228. if (!base->discipline->fill_info)
  229. return -EINVAL;
  230. dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  231. if (dasd_info == NULL)
  232. return -ENOMEM;
  233. rc = base->discipline->fill_info(base, dasd_info);
  234. if (rc) {
  235. kfree(dasd_info);
  236. return rc;
  237. }
  238. cdev = base->cdev;
  239. ccw_device_get_id(cdev, &dev_id);
  240. dasd_info->devno = dev_id.devno;
  241. dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
  242. dasd_info->cu_type = cdev->id.cu_type;
  243. dasd_info->cu_model = cdev->id.cu_model;
  244. dasd_info->dev_type = cdev->id.dev_type;
  245. dasd_info->dev_model = cdev->id.dev_model;
  246. dasd_info->status = base->state;
  247. /*
  248. * The open_count is increased for every opener, that includes
  249. * the blkdev_get in dasd_scan_partitions.
  250. * This must be hidden from user-space.
  251. */
  252. dasd_info->open_count = atomic_read(&block->open_count);
  253. if (!block->bdev)
  254. dasd_info->open_count++;
  255. /*
  256. * check if device is really formatted
  257. * LDL / CDL was returned by 'fill_info'
  258. */
  259. if ((base->state < DASD_STATE_READY) ||
  260. (dasd_check_blocksize(block->bp_block)))
  261. dasd_info->format = DASD_FORMAT_NONE;
  262. dasd_info->features |=
  263. ((base->features & DASD_FEATURE_READONLY) != 0);
  264. if (base->discipline)
  265. memcpy(dasd_info->type, base->discipline->name, 4);
  266. else
  267. memcpy(dasd_info->type, "none", 4);
  268. if (block->request_queue->request_fn) {
  269. struct list_head *l;
  270. #ifdef DASD_EXTENDED_PROFILING
  271. {
  272. struct list_head *l;
  273. spin_lock_irqsave(&block->lock, flags);
  274. list_for_each(l, &block->request_queue->queue_head)
  275. dasd_info->req_queue_len++;
  276. spin_unlock_irqrestore(&block->lock, flags);
  277. }
  278. #endif /* DASD_EXTENDED_PROFILING */
  279. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  280. list_for_each(l, &base->ccw_queue)
  281. dasd_info->chanq_len++;
  282. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  283. flags);
  284. }
  285. rc = 0;
  286. if (copy_to_user(argp, dasd_info,
  287. ((cmd == (unsigned int) BIODASDINFO2) ?
  288. sizeof(struct dasd_information2_t) :
  289. sizeof(struct dasd_information_t))))
  290. rc = -EFAULT;
  291. kfree(dasd_info);
  292. return rc;
  293. }
  294. /*
  295. * Set read only
  296. */
  297. static int
  298. dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
  299. {
  300. struct dasd_block *block = bdev->bd_disk->private_data;
  301. int intval;
  302. if (!capable(CAP_SYS_ADMIN))
  303. return -EACCES;
  304. if (bdev != bdev->bd_contains)
  305. // ro setting is not allowed for partitions
  306. return -EINVAL;
  307. if (get_user(intval, (int __user *)argp))
  308. return -EFAULT;
  309. set_disk_ro(bdev->bd_disk, intval);
  310. return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
  311. }
  312. static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
  313. unsigned long arg)
  314. {
  315. struct cmbdata __user *argp = (void __user *) arg;
  316. size_t size = _IOC_SIZE(cmd);
  317. struct cmbdata data;
  318. int ret;
  319. ret = cmf_readall(block->base->cdev, &data);
  320. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  321. return -EFAULT;
  322. return ret;
  323. }
  324. static int
  325. dasd_do_ioctl(struct block_device *bdev, fmode_t mode,
  326. unsigned int cmd, unsigned long arg)
  327. {
  328. struct dasd_block *block = bdev->bd_disk->private_data;
  329. void __user *argp = (void __user *)arg;
  330. if (!block)
  331. return -ENODEV;
  332. if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
  333. PRINT_DEBUG("empty data ptr");
  334. return -EINVAL;
  335. }
  336. switch (cmd) {
  337. case BIODASDDISABLE:
  338. return dasd_ioctl_disable(bdev);
  339. case BIODASDENABLE:
  340. return dasd_ioctl_enable(bdev);
  341. case BIODASDQUIESCE:
  342. return dasd_ioctl_quiesce(block);
  343. case BIODASDRESUME:
  344. return dasd_ioctl_resume(block);
  345. case BIODASDFMT:
  346. return dasd_ioctl_format(bdev, argp);
  347. case BIODASDINFO:
  348. return dasd_ioctl_information(block, cmd, argp);
  349. case BIODASDINFO2:
  350. return dasd_ioctl_information(block, cmd, argp);
  351. case BIODASDPRRD:
  352. return dasd_ioctl_read_profile(block, argp);
  353. case BIODASDPRRST:
  354. return dasd_ioctl_reset_profile(block);
  355. case BLKROSET:
  356. return dasd_ioctl_set_ro(bdev, argp);
  357. case DASDAPIVER:
  358. return dasd_ioctl_api_version(argp);
  359. case BIODASDCMFENABLE:
  360. return enable_cmf(block->base->cdev);
  361. case BIODASDCMFDISABLE:
  362. return disable_cmf(block->base->cdev);
  363. case BIODASDREADALLCMB:
  364. return dasd_ioctl_readall_cmb(block, cmd, arg);
  365. default:
  366. /* if the discipline has an ioctl method try it. */
  367. if (block->base->discipline->ioctl) {
  368. int rval = block->base->discipline->ioctl(block, cmd, argp);
  369. if (rval != -ENOIOCTLCMD)
  370. return rval;
  371. }
  372. return -EINVAL;
  373. }
  374. }
  375. int dasd_ioctl(struct block_device *bdev, fmode_t mode,
  376. unsigned int cmd, unsigned long arg)
  377. {
  378. int rc;
  379. lock_kernel();
  380. rc = dasd_do_ioctl(bdev, mode, cmd, arg);
  381. unlock_kernel();
  382. return rc;
  383. }