dasd_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_device *device = bdev->bd_disk->private_data;
  36. if (!capable(CAP_SYS_ADMIN))
  37. return -EACCES;
  38. dasd_enable_device(device);
  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(device->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_device *device = 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(device, 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
  77. dasd_ioctl_quiesce(struct dasd_device *device)
  78. {
  79. unsigned long flags;
  80. if (!capable (CAP_SYS_ADMIN))
  81. return -EACCES;
  82. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  83. "Quiesce IO on device");
  84. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  85. device->stopped |= DASD_STOPPED_QUIESCE;
  86. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  87. return 0;
  88. }
  89. /*
  90. * Quiesce device.
  91. */
  92. static int
  93. dasd_ioctl_resume(struct dasd_device *device)
  94. {
  95. unsigned long flags;
  96. if (!capable (CAP_SYS_ADMIN))
  97. return -EACCES;
  98. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  99. "resume IO on device");
  100. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  101. device->stopped &= ~DASD_STOPPED_QUIESCE;
  102. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  103. dasd_schedule_bh (device);
  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
  113. dasd_format(struct dasd_device * device, struct format_data_t * fdata)
  114. {
  115. struct dasd_ccw_req *cqr;
  116. int rc;
  117. if (device->discipline->format_device == NULL)
  118. return -EPERM;
  119. if (device->state != DASD_STATE_BASIC) {
  120. DEV_MESSAGE(KERN_WARNING, device, "%s",
  121. "dasd_format: device is not disabled! ");
  122. return -EBUSY;
  123. }
  124. DBF_DEV_EVENT(DBF_NOTICE, device,
  125. "formatting units %d to %d (%d B blocks) flags %d",
  126. fdata->start_unit,
  127. fdata->stop_unit, fdata->blksize, fdata->intensity);
  128. /* Since dasdfmt keeps the device open after it was disabled,
  129. * there still exists an inode for this device.
  130. * We must update i_blkbits, otherwise we might get errors when
  131. * enabling the device later.
  132. */
  133. if (fdata->start_unit == 0) {
  134. struct block_device *bdev = bdget_disk(device->gdp, 0);
  135. bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
  136. bdput(bdev);
  137. }
  138. while (fdata->start_unit <= fdata->stop_unit) {
  139. cqr = device->discipline->format_device(device, fdata);
  140. if (IS_ERR(cqr))
  141. return PTR_ERR(cqr);
  142. rc = dasd_sleep_on_interruptible(cqr);
  143. dasd_sfree_request(cqr, cqr->device);
  144. if (rc) {
  145. if (rc != -ERESTARTSYS)
  146. DEV_MESSAGE(KERN_ERR, device,
  147. " Formatting of unit %d failed "
  148. "with rc = %d",
  149. fdata->start_unit, rc);
  150. return rc;
  151. }
  152. fdata->start_unit++;
  153. }
  154. return 0;
  155. }
  156. /*
  157. * Format device.
  158. */
  159. static int
  160. dasd_ioctl_format(struct block_device *bdev, void __user *argp)
  161. {
  162. struct dasd_device *device = bdev->bd_disk->private_data;
  163. struct format_data_t fdata;
  164. if (!capable(CAP_SYS_ADMIN))
  165. return -EACCES;
  166. if (!argp)
  167. return -EINVAL;
  168. if (device->features & DASD_FEATURE_READONLY)
  169. return -EROFS;
  170. if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
  171. return -EFAULT;
  172. if (bdev != bdev->bd_contains) {
  173. DEV_MESSAGE(KERN_WARNING, device, "%s",
  174. "Cannot low-level format a partition");
  175. return -EINVAL;
  176. }
  177. return dasd_format(device, &fdata);
  178. }
  179. #ifdef CONFIG_DASD_PROFILE
  180. /*
  181. * Reset device profile information
  182. */
  183. static int
  184. dasd_ioctl_reset_profile(struct dasd_device *device)
  185. {
  186. memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
  187. return 0;
  188. }
  189. /*
  190. * Return device profile information
  191. */
  192. static int
  193. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  194. {
  195. if (dasd_profile_level == DASD_PROFILE_OFF)
  196. return -EIO;
  197. if (copy_to_user(argp, &device->profile,
  198. sizeof (struct dasd_profile_info_t)))
  199. return -EFAULT;
  200. return 0;
  201. }
  202. #else
  203. static int
  204. dasd_ioctl_reset_profile(struct dasd_device *device)
  205. {
  206. return -ENOSYS;
  207. }
  208. static int
  209. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  210. {
  211. return -ENOSYS;
  212. }
  213. #endif
  214. /*
  215. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  216. */
  217. static int
  218. dasd_ioctl_information(struct dasd_device *device,
  219. unsigned int cmd, void __user *argp)
  220. {
  221. struct dasd_information2_t *dasd_info;
  222. unsigned long flags;
  223. int rc;
  224. struct ccw_device *cdev;
  225. if (!device->discipline->fill_info)
  226. return -EINVAL;
  227. dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  228. if (dasd_info == NULL)
  229. return -ENOMEM;
  230. rc = device->discipline->fill_info(device, dasd_info);
  231. if (rc) {
  232. kfree(dasd_info);
  233. return rc;
  234. }
  235. cdev = device->cdev;
  236. dasd_info->devno = _ccw_device_get_device_number(device->cdev);
  237. dasd_info->schid = _ccw_device_get_subchannel_number(device->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 = device->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(&device->open_count);
  249. if (!device->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 ((device->state < DASD_STATE_READY) ||
  256. (dasd_check_blocksize(device->bp_block)))
  257. dasd_info->format = DASD_FORMAT_NONE;
  258. dasd_info->features |=
  259. ((device->features & DASD_FEATURE_READONLY) != 0);
  260. if (device->discipline)
  261. memcpy(dasd_info->type, device->discipline->name, 4);
  262. else
  263. memcpy(dasd_info->type, "none", 4);
  264. if (device->request_queue->request_fn) {
  265. struct list_head *l;
  266. #ifdef DASD_EXTENDED_PROFILING
  267. {
  268. struct list_head *l;
  269. spin_lock_irqsave(&device->lock, flags);
  270. list_for_each(l, &device->request_queue->queue_head)
  271. dasd_info->req_queue_len++;
  272. spin_unlock_irqrestore(&device->lock, flags);
  273. }
  274. #endif /* DASD_EXTENDED_PROFILING */
  275. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  276. list_for_each(l, &device->ccw_queue)
  277. dasd_info->chanq_len++;
  278. spin_unlock_irqrestore(get_ccwdev_lock(device->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_device *device = 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(device->cdev, DASD_FEATURE_READONLY, intval);
  307. }
  308. static int
  309. dasd_ioctl_readall_cmb(struct dasd_device *device, unsigned int cmd,
  310. unsigned long arg)
  311. {
  312. struct cmbdata __user *argp = (void __user *) arg;
  313. size_t size = _IOC_SIZE(cmd);
  314. struct cmbdata data;
  315. int ret;
  316. ret = cmf_readall(device->cdev, &data);
  317. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  318. return -EFAULT;
  319. return ret;
  320. }
  321. int
  322. dasd_ioctl(struct inode *inode, struct file *file,
  323. unsigned int cmd, unsigned long arg)
  324. {
  325. struct block_device *bdev = inode->i_bdev;
  326. struct dasd_device *device = bdev->bd_disk->private_data;
  327. void __user *argp = (void __user *)arg;
  328. if (!device)
  329. return -ENODEV;
  330. if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
  331. PRINT_DEBUG("empty data ptr");
  332. return -EINVAL;
  333. }
  334. switch (cmd) {
  335. case BIODASDDISABLE:
  336. return dasd_ioctl_disable(bdev);
  337. case BIODASDENABLE:
  338. return dasd_ioctl_enable(bdev);
  339. case BIODASDQUIESCE:
  340. return dasd_ioctl_quiesce(device);
  341. case BIODASDRESUME:
  342. return dasd_ioctl_resume(device);
  343. case BIODASDFMT:
  344. return dasd_ioctl_format(bdev, argp);
  345. case BIODASDINFO:
  346. return dasd_ioctl_information(device, cmd, argp);
  347. case BIODASDINFO2:
  348. return dasd_ioctl_information(device, cmd, argp);
  349. case BIODASDPRRD:
  350. return dasd_ioctl_read_profile(device, argp);
  351. case BIODASDPRRST:
  352. return dasd_ioctl_reset_profile(device);
  353. case BLKROSET:
  354. return dasd_ioctl_set_ro(bdev, argp);
  355. case DASDAPIVER:
  356. return dasd_ioctl_api_version(argp);
  357. case BIODASDCMFENABLE:
  358. return enable_cmf(device->cdev);
  359. case BIODASDCMFDISABLE:
  360. return disable_cmf(device->cdev);
  361. case BIODASDREADALLCMB:
  362. return dasd_ioctl_readall_cmb(device, cmd, arg);
  363. default:
  364. /* if the discipline has an ioctl method try it. */
  365. if (device->discipline->ioctl) {
  366. int rval = device->discipline->ioctl(device, cmd, argp);
  367. if (rval != -ENOIOCTLCMD)
  368. return rval;
  369. }
  370. return -EINVAL;
  371. }
  372. }
  373. long
  374. dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  375. {
  376. int rval;
  377. lock_kernel();
  378. rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
  379. unlock_kernel();
  380. return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
  381. }