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. #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. struct ccw_dev_id dev_id;
  226. if (!device->discipline->fill_info)
  227. return -EINVAL;
  228. dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  229. if (dasd_info == NULL)
  230. return -ENOMEM;
  231. rc = device->discipline->fill_info(device, dasd_info);
  232. if (rc) {
  233. kfree(dasd_info);
  234. return rc;
  235. }
  236. cdev = device->cdev;
  237. ccw_device_get_id(cdev, &dev_id);
  238. dasd_info->devno = dev_id.devno;
  239. dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
  240. dasd_info->cu_type = cdev->id.cu_type;
  241. dasd_info->cu_model = cdev->id.cu_model;
  242. dasd_info->dev_type = cdev->id.dev_type;
  243. dasd_info->dev_model = cdev->id.dev_model;
  244. dasd_info->status = device->state;
  245. /*
  246. * The open_count is increased for every opener, that includes
  247. * the blkdev_get in dasd_scan_partitions.
  248. * This must be hidden from user-space.
  249. */
  250. dasd_info->open_count = atomic_read(&device->open_count);
  251. if (!device->bdev)
  252. dasd_info->open_count++;
  253. /*
  254. * check if device is really formatted
  255. * LDL / CDL was returned by 'fill_info'
  256. */
  257. if ((device->state < DASD_STATE_READY) ||
  258. (dasd_check_blocksize(device->bp_block)))
  259. dasd_info->format = DASD_FORMAT_NONE;
  260. dasd_info->features |=
  261. ((device->features & DASD_FEATURE_READONLY) != 0);
  262. if (device->discipline)
  263. memcpy(dasd_info->type, device->discipline->name, 4);
  264. else
  265. memcpy(dasd_info->type, "none", 4);
  266. if (device->request_queue->request_fn) {
  267. struct list_head *l;
  268. #ifdef DASD_EXTENDED_PROFILING
  269. {
  270. struct list_head *l;
  271. spin_lock_irqsave(&device->lock, flags);
  272. list_for_each(l, &device->request_queue->queue_head)
  273. dasd_info->req_queue_len++;
  274. spin_unlock_irqrestore(&device->lock, flags);
  275. }
  276. #endif /* DASD_EXTENDED_PROFILING */
  277. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  278. list_for_each(l, &device->ccw_queue)
  279. dasd_info->chanq_len++;
  280. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  281. flags);
  282. }
  283. rc = 0;
  284. if (copy_to_user(argp, dasd_info,
  285. ((cmd == (unsigned int) BIODASDINFO2) ?
  286. sizeof (struct dasd_information2_t) :
  287. sizeof (struct dasd_information_t))))
  288. rc = -EFAULT;
  289. kfree(dasd_info);
  290. return rc;
  291. }
  292. /*
  293. * Set read only
  294. */
  295. static int
  296. dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
  297. {
  298. struct dasd_device *device = bdev->bd_disk->private_data;
  299. int intval;
  300. if (!capable(CAP_SYS_ADMIN))
  301. return -EACCES;
  302. if (bdev != bdev->bd_contains)
  303. // ro setting is not allowed for partitions
  304. return -EINVAL;
  305. if (get_user(intval, (int __user *)argp))
  306. return -EFAULT;
  307. set_disk_ro(bdev->bd_disk, intval);
  308. return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
  309. }
  310. static int
  311. dasd_ioctl_readall_cmb(struct dasd_device *device, 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(device->cdev, &data);
  319. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  320. return -EFAULT;
  321. return ret;
  322. }
  323. int
  324. dasd_ioctl(struct inode *inode, struct file *file,
  325. unsigned int cmd, unsigned long arg)
  326. {
  327. struct block_device *bdev = inode->i_bdev;
  328. struct dasd_device *device = bdev->bd_disk->private_data;
  329. void __user *argp = (void __user *)arg;
  330. if (!device)
  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(device);
  343. case BIODASDRESUME:
  344. return dasd_ioctl_resume(device);
  345. case BIODASDFMT:
  346. return dasd_ioctl_format(bdev, argp);
  347. case BIODASDINFO:
  348. return dasd_ioctl_information(device, cmd, argp);
  349. case BIODASDINFO2:
  350. return dasd_ioctl_information(device, cmd, argp);
  351. case BIODASDPRRD:
  352. return dasd_ioctl_read_profile(device, argp);
  353. case BIODASDPRRST:
  354. return dasd_ioctl_reset_profile(device);
  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(device->cdev);
  361. case BIODASDCMFDISABLE:
  362. return disable_cmf(device->cdev);
  363. case BIODASDREADALLCMB:
  364. return dasd_ioctl_readall_cmb(device, cmd, arg);
  365. default:
  366. /* if the discipline has an ioctl method try it. */
  367. if (device->discipline->ioctl) {
  368. int rval = device->discipline->ioctl(device, cmd, argp);
  369. if (rval != -ENOIOCTLCMD)
  370. return rval;
  371. }
  372. return -EINVAL;
  373. }
  374. }
  375. long
  376. dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  377. {
  378. int rval;
  379. lock_kernel();
  380. rval = dasd_ioctl(filp->f_path.dentry->d_inode, filp, cmd, arg);
  381. unlock_kernel();
  382. return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
  383. }