dasd_ioctl.c 11 KB

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