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/config.h>
  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 *)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_device *device = bdev->bd_disk->private_data;
  37. if (!capable(CAP_SYS_ADMIN))
  38. return -EACCES;
  39. dasd_enable_device(device);
  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(device->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_device *device = 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(device, 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
  78. dasd_ioctl_quiesce(struct dasd_device *device)
  79. {
  80. unsigned long flags;
  81. if (!capable (CAP_SYS_ADMIN))
  82. return -EACCES;
  83. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  84. "Quiesce IO on device");
  85. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  86. device->stopped |= DASD_STOPPED_QUIESCE;
  87. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  88. return 0;
  89. }
  90. /*
  91. * Quiesce device.
  92. */
  93. static int
  94. dasd_ioctl_resume(struct dasd_device *device)
  95. {
  96. unsigned long flags;
  97. if (!capable (CAP_SYS_ADMIN))
  98. return -EACCES;
  99. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  100. "resume IO on device");
  101. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  102. device->stopped &= ~DASD_STOPPED_QUIESCE;
  103. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  104. dasd_schedule_bh (device);
  105. return 0;
  106. }
  107. /*
  108. * performs formatting of _device_ according to _fdata_
  109. * Note: The discipline's format_function is assumed to deliver formatting
  110. * commands to format a single unit of the device. In terms of the ECKD
  111. * devices this means CCWs are generated to format a single track.
  112. */
  113. static int
  114. dasd_format(struct dasd_device * device, struct format_data_t * fdata)
  115. {
  116. struct dasd_ccw_req *cqr;
  117. int rc;
  118. if (device->discipline->format_device == NULL)
  119. return -EPERM;
  120. if (device->state != DASD_STATE_BASIC) {
  121. DEV_MESSAGE(KERN_WARNING, device, "%s",
  122. "dasd_format: device is not disabled! ");
  123. return -EBUSY;
  124. }
  125. DBF_DEV_EVENT(DBF_NOTICE, device,
  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(device->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 = device->discipline->format_device(device, fdata);
  141. if (IS_ERR(cqr))
  142. return PTR_ERR(cqr);
  143. rc = dasd_sleep_on_interruptible(cqr);
  144. dasd_sfree_request(cqr, cqr->device);
  145. if (rc) {
  146. if (rc != -ERESTARTSYS)
  147. DEV_MESSAGE(KERN_ERR, device,
  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_device *device = 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 (device->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, device, "%s",
  175. "Cannot low-level format a partition");
  176. return -EINVAL;
  177. }
  178. return dasd_format(device, &fdata);
  179. }
  180. #ifdef CONFIG_DASD_PROFILE
  181. /*
  182. * Reset device profile information
  183. */
  184. static int
  185. dasd_ioctl_reset_profile(struct dasd_device *device)
  186. {
  187. memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
  188. return 0;
  189. }
  190. /*
  191. * Return device profile information
  192. */
  193. static int
  194. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  195. {
  196. if (dasd_profile_level == DASD_PROFILE_OFF)
  197. return -EIO;
  198. if (copy_to_user(argp, &device->profile,
  199. sizeof (struct dasd_profile_info_t)))
  200. return -EFAULT;
  201. return 0;
  202. }
  203. #else
  204. static int
  205. dasd_ioctl_reset_profile(struct dasd_device *device)
  206. {
  207. return -ENOSYS;
  208. }
  209. static int
  210. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  211. {
  212. return -ENOSYS;
  213. }
  214. #endif
  215. /*
  216. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  217. */
  218. static int
  219. dasd_ioctl_information(struct dasd_device *device,
  220. unsigned int cmd, void __user *argp)
  221. {
  222. struct dasd_information2_t *dasd_info;
  223. unsigned long flags;
  224. int rc;
  225. struct ccw_device *cdev;
  226. if (!device->discipline->fill_info)
  227. return -EINVAL;
  228. dasd_info = kmalloc(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. dasd_info->devno = _ccw_device_get_device_number(device->cdev);
  238. dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
  239. dasd_info->cu_type = cdev->id.cu_type;
  240. dasd_info->cu_model = cdev->id.cu_model;
  241. dasd_info->dev_type = cdev->id.dev_type;
  242. dasd_info->dev_model = cdev->id.dev_model;
  243. dasd_info->status = device->state;
  244. /*
  245. * The open_count is increased for every opener, that includes
  246. * the blkdev_get in dasd_scan_partitions.
  247. * This must be hidden from user-space.
  248. */
  249. dasd_info->open_count = atomic_read(&device->open_count);
  250. if (!device->bdev)
  251. dasd_info->open_count++;
  252. /*
  253. * check if device is really formatted
  254. * LDL / CDL was returned by 'fill_info'
  255. */
  256. if ((device->state < DASD_STATE_READY) ||
  257. (dasd_check_blocksize(device->bp_block)))
  258. dasd_info->format = DASD_FORMAT_NONE;
  259. dasd_info->features |=
  260. ((device->features & DASD_FEATURE_READONLY) != 0);
  261. if (device->discipline)
  262. memcpy(dasd_info->type, device->discipline->name, 4);
  263. else
  264. memcpy(dasd_info->type, "none", 4);
  265. dasd_info->req_queue_len = 0;
  266. dasd_info->chanq_len = 0;
  267. if (device->request_queue->request_fn) {
  268. struct list_head *l;
  269. #ifdef DASD_EXTENDED_PROFILING
  270. {
  271. struct list_head *l;
  272. spin_lock_irqsave(&device->lock, flags);
  273. list_for_each(l, &device->request_queue->queue_head)
  274. dasd_info->req_queue_len++;
  275. spin_unlock_irqrestore(&device->lock, flags);
  276. }
  277. #endif /* DASD_EXTENDED_PROFILING */
  278. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  279. list_for_each(l, &device->ccw_queue)
  280. dasd_info->chanq_len++;
  281. spin_unlock_irqrestore(get_ccwdev_lock(device->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_device *device = 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 *)argp))
  307. return -EFAULT;
  308. set_disk_ro(bdev->bd_disk, intval);
  309. return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
  310. }
  311. static int
  312. dasd_ioctl_readall_cmb(struct dasd_device *device, unsigned int cmd,
  313. unsigned long arg)
  314. {
  315. struct cmbdata __user *argp = (void __user *) arg;
  316. size_t size = _IOC_SIZE(cmd);
  317. struct cmbdata data;
  318. int ret;
  319. ret = cmf_readall(device->cdev, &data);
  320. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  321. return -EFAULT;
  322. return ret;
  323. }
  324. int
  325. dasd_ioctl(struct inode *inode, struct file *file,
  326. unsigned int cmd, unsigned long arg)
  327. {
  328. struct block_device *bdev = inode->i_bdev;
  329. struct dasd_device *device = bdev->bd_disk->private_data;
  330. void __user *argp = (void __user *)arg;
  331. if (!device)
  332. return -ENODEV;
  333. if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
  334. PRINT_DEBUG("empty data ptr");
  335. return -EINVAL;
  336. }
  337. switch (cmd) {
  338. case BIODASDDISABLE:
  339. return dasd_ioctl_disable(bdev);
  340. case BIODASDENABLE:
  341. return dasd_ioctl_enable(bdev);
  342. case BIODASDQUIESCE:
  343. return dasd_ioctl_quiesce(device);
  344. case BIODASDRESUME:
  345. return dasd_ioctl_resume(device);
  346. case BIODASDFMT:
  347. return dasd_ioctl_format(bdev, argp);
  348. case BIODASDINFO:
  349. return dasd_ioctl_information(device, cmd, argp);
  350. case BIODASDINFO2:
  351. return dasd_ioctl_information(device, cmd, argp);
  352. case BIODASDPRRD:
  353. return dasd_ioctl_read_profile(device, argp);
  354. case BIODASDPRRST:
  355. return dasd_ioctl_reset_profile(device);
  356. case BLKROSET:
  357. return dasd_ioctl_set_ro(bdev, argp);
  358. case DASDAPIVER:
  359. return dasd_ioctl_api_version(argp);
  360. case BIODASDCMFENABLE:
  361. return enable_cmf(device->cdev);
  362. case BIODASDCMFDISABLE:
  363. return disable_cmf(device->cdev);
  364. case BIODASDREADALLCMB:
  365. return dasd_ioctl_readall_cmb(device, cmd, arg);
  366. default:
  367. /* if the discipline has an ioctl method try it. */
  368. if (device->discipline->ioctl) {
  369. int rval = device->discipline->ioctl(device, cmd, argp);
  370. if (rval != -ENOIOCTLCMD)
  371. return rval;
  372. }
  373. return -EINVAL;
  374. }
  375. }
  376. long
  377. dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  378. {
  379. int rval;
  380. lock_kernel();
  381. rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
  382. unlock_kernel();
  383. return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
  384. }