dasd_ioctl.c 11 KB

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