dasd_ioctl.c 11 KB

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