dasd_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
  3. * Horst Hummel <Horst.Hummel@de.ibm.com>
  4. * Carsten Otte <Cotte@de.ibm.com>
  5. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. * Bugreports.to..: <Linux390@de.ibm.com>
  7. * Copyright IBM Corp. 1999, 2001
  8. *
  9. * i/o controls for the dasd driver.
  10. */
  11. #define KMSG_COMPONENT "dasd"
  12. #include <linux/interrupt.h>
  13. #include <linux/compat.h>
  14. #include <linux/major.h>
  15. #include <linux/fs.h>
  16. #include <linux/blkpg.h>
  17. #include <linux/slab.h>
  18. #include <asm/compat.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/schid.h>
  21. #include <asm/cmb.h>
  22. #include <asm/uaccess.h>
  23. /* This is ugly... */
  24. #define PRINTK_HEADER "dasd_ioctl:"
  25. #include "dasd_int.h"
  26. static int
  27. dasd_ioctl_api_version(void __user *argp)
  28. {
  29. int ver = DASD_API_VERSION;
  30. return put_user(ver, (int __user *)argp);
  31. }
  32. /*
  33. * Enable device.
  34. * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
  35. */
  36. static int
  37. dasd_ioctl_enable(struct block_device *bdev)
  38. {
  39. struct dasd_device *base;
  40. if (!capable(CAP_SYS_ADMIN))
  41. return -EACCES;
  42. base = dasd_device_from_gendisk(bdev->bd_disk);
  43. if (!base)
  44. return -ENODEV;
  45. dasd_enable_device(base);
  46. /* Formatting the dasd device can change the capacity. */
  47. mutex_lock(&bdev->bd_mutex);
  48. i_size_write(bdev->bd_inode,
  49. (loff_t)get_capacity(base->block->gdp) << 9);
  50. mutex_unlock(&bdev->bd_mutex);
  51. dasd_put_device(base);
  52. return 0;
  53. }
  54. /*
  55. * Disable device.
  56. * Used by dasdfmt. Disable I/O operations but allow ioctls.
  57. */
  58. static int
  59. dasd_ioctl_disable(struct block_device *bdev)
  60. {
  61. struct dasd_device *base;
  62. if (!capable(CAP_SYS_ADMIN))
  63. return -EACCES;
  64. base = dasd_device_from_gendisk(bdev->bd_disk);
  65. if (!base)
  66. return -ENODEV;
  67. /*
  68. * Man this is sick. We don't do a real disable but only downgrade
  69. * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
  70. * BIODASDDISABLE to disable accesses to the device via the block
  71. * device layer but it still wants to do i/o on the device by
  72. * using the BIODASDFMT ioctl. Therefore the correct state for the
  73. * device is DASD_STATE_BASIC that allows to do basic i/o.
  74. */
  75. dasd_set_target_state(base, DASD_STATE_BASIC);
  76. /*
  77. * Set i_size to zero, since read, write, etc. check against this
  78. * value.
  79. */
  80. mutex_lock(&bdev->bd_mutex);
  81. i_size_write(bdev->bd_inode, 0);
  82. mutex_unlock(&bdev->bd_mutex);
  83. dasd_put_device(base);
  84. return 0;
  85. }
  86. /*
  87. * Quiesce device.
  88. */
  89. static int dasd_ioctl_quiesce(struct dasd_block *block)
  90. {
  91. unsigned long flags;
  92. struct dasd_device *base;
  93. base = block->base;
  94. if (!capable (CAP_SYS_ADMIN))
  95. return -EACCES;
  96. pr_info("%s: The DASD has been put in the quiesce "
  97. "state\n", dev_name(&base->cdev->dev));
  98. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  99. dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
  100. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
  101. return 0;
  102. }
  103. /*
  104. * Resume device.
  105. */
  106. static int dasd_ioctl_resume(struct dasd_block *block)
  107. {
  108. unsigned long flags;
  109. struct dasd_device *base;
  110. base = block->base;
  111. if (!capable (CAP_SYS_ADMIN))
  112. return -EACCES;
  113. pr_info("%s: I/O operations have been resumed "
  114. "on the DASD\n", dev_name(&base->cdev->dev));
  115. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  116. dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
  117. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
  118. dasd_schedule_block_bh(block);
  119. return 0;
  120. }
  121. /*
  122. * performs formatting of _device_ according to _fdata_
  123. * Note: The discipline's format_function is assumed to deliver formatting
  124. * commands to format multiple units of the device. In terms of the ECKD
  125. * devices this means CCWs are generated to format multiple tracks.
  126. */
  127. static int
  128. dasd_format(struct dasd_block *block, struct format_data_t *fdata)
  129. {
  130. struct dasd_device *base;
  131. int rc;
  132. base = block->base;
  133. if (base->discipline->format_device == NULL)
  134. return -EPERM;
  135. if (base->state != DASD_STATE_BASIC) {
  136. pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
  137. dev_name(&base->cdev->dev));
  138. return -EBUSY;
  139. }
  140. DBF_DEV_EVENT(DBF_NOTICE, base,
  141. "formatting units %u to %u (%u B blocks) flags %u",
  142. fdata->start_unit,
  143. fdata->stop_unit, fdata->blksize, fdata->intensity);
  144. /* Since dasdfmt keeps the device open after it was disabled,
  145. * there still exists an inode for this device.
  146. * We must update i_blkbits, otherwise we might get errors when
  147. * enabling the device later.
  148. */
  149. if (fdata->start_unit == 0) {
  150. struct block_device *bdev = bdget_disk(block->gdp, 0);
  151. bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
  152. bdput(bdev);
  153. }
  154. rc = base->discipline->format_device(base, fdata);
  155. if (rc)
  156. return rc;
  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_device *base;
  166. struct format_data_t fdata;
  167. int rc;
  168. if (!capable(CAP_SYS_ADMIN))
  169. return -EACCES;
  170. if (!argp)
  171. return -EINVAL;
  172. base = dasd_device_from_gendisk(bdev->bd_disk);
  173. if (!base)
  174. return -ENODEV;
  175. if (base->features & DASD_FEATURE_READONLY ||
  176. test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
  177. dasd_put_device(base);
  178. return -EROFS;
  179. }
  180. if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
  181. dasd_put_device(base);
  182. return -EFAULT;
  183. }
  184. if (bdev != bdev->bd_contains) {
  185. pr_warning("%s: The specified DASD is a partition and cannot "
  186. "be formatted\n",
  187. dev_name(&base->cdev->dev));
  188. dasd_put_device(base);
  189. return -EINVAL;
  190. }
  191. rc = dasd_format(base->block, &fdata);
  192. dasd_put_device(base);
  193. return rc;
  194. }
  195. #ifdef CONFIG_DASD_PROFILE
  196. /*
  197. * Reset device profile information
  198. */
  199. static int dasd_ioctl_reset_profile(struct dasd_block *block)
  200. {
  201. dasd_profile_reset(&block->profile);
  202. return 0;
  203. }
  204. /*
  205. * Return device profile information
  206. */
  207. static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
  208. {
  209. struct dasd_profile_info_t *data;
  210. int rc = 0;
  211. data = kmalloc(sizeof(*data), GFP_KERNEL);
  212. if (!data)
  213. return -ENOMEM;
  214. spin_lock_bh(&block->profile.lock);
  215. if (block->profile.data) {
  216. data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
  217. data->dasd_io_sects = block->profile.data->dasd_io_sects;
  218. memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
  219. sizeof(data->dasd_io_secs));
  220. memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
  221. sizeof(data->dasd_io_times));
  222. memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
  223. sizeof(data->dasd_io_timps));
  224. memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
  225. sizeof(data->dasd_io_time1));
  226. memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
  227. sizeof(data->dasd_io_time2));
  228. memcpy(data->dasd_io_time2ps,
  229. block->profile.data->dasd_io_time2ps,
  230. sizeof(data->dasd_io_time2ps));
  231. memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
  232. sizeof(data->dasd_io_time3));
  233. memcpy(data->dasd_io_nr_req,
  234. block->profile.data->dasd_io_nr_req,
  235. sizeof(data->dasd_io_nr_req));
  236. spin_unlock_bh(&block->profile.lock);
  237. } else {
  238. spin_unlock_bh(&block->profile.lock);
  239. rc = -EIO;
  240. goto out;
  241. }
  242. if (copy_to_user(argp, data, sizeof(*data)))
  243. rc = -EFAULT;
  244. out:
  245. kfree(data);
  246. return rc;
  247. }
  248. #else
  249. static int dasd_ioctl_reset_profile(struct dasd_block *block)
  250. {
  251. return -ENOTTY;
  252. }
  253. static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
  254. {
  255. return -ENOTTY;
  256. }
  257. #endif
  258. /*
  259. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  260. */
  261. static int dasd_ioctl_information(struct dasd_block *block,
  262. unsigned int cmd, void __user *argp)
  263. {
  264. struct dasd_information2_t *dasd_info;
  265. struct subchannel_id sch_id;
  266. struct ccw_dev_id dev_id;
  267. struct dasd_device *base;
  268. struct ccw_device *cdev;
  269. unsigned long flags;
  270. int rc;
  271. base = block->base;
  272. if (!base->discipline || !base->discipline->fill_info)
  273. return -EINVAL;
  274. dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  275. if (dasd_info == NULL)
  276. return -ENOMEM;
  277. rc = base->discipline->fill_info(base, dasd_info);
  278. if (rc) {
  279. kfree(dasd_info);
  280. return rc;
  281. }
  282. cdev = base->cdev;
  283. ccw_device_get_id(cdev, &dev_id);
  284. ccw_device_get_schid(cdev, &sch_id);
  285. dasd_info->devno = dev_id.devno;
  286. dasd_info->schid = sch_id.sch_no;
  287. dasd_info->cu_type = cdev->id.cu_type;
  288. dasd_info->cu_model = cdev->id.cu_model;
  289. dasd_info->dev_type = cdev->id.dev_type;
  290. dasd_info->dev_model = cdev->id.dev_model;
  291. dasd_info->status = base->state;
  292. /*
  293. * The open_count is increased for every opener, that includes
  294. * the blkdev_get in dasd_scan_partitions.
  295. * This must be hidden from user-space.
  296. */
  297. dasd_info->open_count = atomic_read(&block->open_count);
  298. if (!block->bdev)
  299. dasd_info->open_count++;
  300. /*
  301. * check if device is really formatted
  302. * LDL / CDL was returned by 'fill_info'
  303. */
  304. if ((base->state < DASD_STATE_READY) ||
  305. (dasd_check_blocksize(block->bp_block)))
  306. dasd_info->format = DASD_FORMAT_NONE;
  307. dasd_info->features |=
  308. ((base->features & DASD_FEATURE_READONLY) != 0);
  309. memcpy(dasd_info->type, base->discipline->name, 4);
  310. if (block->request_queue->request_fn) {
  311. struct list_head *l;
  312. #ifdef DASD_EXTENDED_PROFILING
  313. {
  314. struct list_head *l;
  315. spin_lock_irqsave(&block->lock, flags);
  316. list_for_each(l, &block->request_queue->queue_head)
  317. dasd_info->req_queue_len++;
  318. spin_unlock_irqrestore(&block->lock, flags);
  319. }
  320. #endif /* DASD_EXTENDED_PROFILING */
  321. spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
  322. list_for_each(l, &base->ccw_queue)
  323. dasd_info->chanq_len++;
  324. spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
  325. flags);
  326. }
  327. rc = 0;
  328. if (copy_to_user(argp, dasd_info,
  329. ((cmd == (unsigned int) BIODASDINFO2) ?
  330. sizeof(struct dasd_information2_t) :
  331. sizeof(struct dasd_information_t))))
  332. rc = -EFAULT;
  333. kfree(dasd_info);
  334. return rc;
  335. }
  336. /*
  337. * Set read only
  338. */
  339. static int
  340. dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
  341. {
  342. struct dasd_device *base;
  343. int intval, rc;
  344. if (!capable(CAP_SYS_ADMIN))
  345. return -EACCES;
  346. if (bdev != bdev->bd_contains)
  347. // ro setting is not allowed for partitions
  348. return -EINVAL;
  349. if (get_user(intval, (int __user *)argp))
  350. return -EFAULT;
  351. base = dasd_device_from_gendisk(bdev->bd_disk);
  352. if (!base)
  353. return -ENODEV;
  354. if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
  355. dasd_put_device(base);
  356. return -EROFS;
  357. }
  358. set_disk_ro(bdev->bd_disk, intval);
  359. rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
  360. dasd_put_device(base);
  361. return rc;
  362. }
  363. static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
  364. struct cmbdata __user *argp)
  365. {
  366. size_t size = _IOC_SIZE(cmd);
  367. struct cmbdata data;
  368. int ret;
  369. ret = cmf_readall(block->base->cdev, &data);
  370. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  371. return -EFAULT;
  372. return ret;
  373. }
  374. int dasd_ioctl(struct block_device *bdev, fmode_t mode,
  375. unsigned int cmd, unsigned long arg)
  376. {
  377. struct dasd_block *block;
  378. struct dasd_device *base;
  379. void __user *argp;
  380. int rc;
  381. if (is_compat_task())
  382. argp = compat_ptr(arg);
  383. else
  384. argp = (void __user *)arg;
  385. if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
  386. PRINT_DEBUG("empty data ptr");
  387. return -EINVAL;
  388. }
  389. base = dasd_device_from_gendisk(bdev->bd_disk);
  390. if (!base)
  391. return -ENODEV;
  392. block = base->block;
  393. rc = 0;
  394. switch (cmd) {
  395. case BIODASDDISABLE:
  396. rc = dasd_ioctl_disable(bdev);
  397. break;
  398. case BIODASDENABLE:
  399. rc = dasd_ioctl_enable(bdev);
  400. break;
  401. case BIODASDQUIESCE:
  402. rc = dasd_ioctl_quiesce(block);
  403. break;
  404. case BIODASDRESUME:
  405. rc = dasd_ioctl_resume(block);
  406. break;
  407. case BIODASDFMT:
  408. rc = dasd_ioctl_format(bdev, argp);
  409. break;
  410. case BIODASDINFO:
  411. rc = dasd_ioctl_information(block, cmd, argp);
  412. break;
  413. case BIODASDINFO2:
  414. rc = dasd_ioctl_information(block, cmd, argp);
  415. break;
  416. case BIODASDPRRD:
  417. rc = dasd_ioctl_read_profile(block, argp);
  418. break;
  419. case BIODASDPRRST:
  420. rc = dasd_ioctl_reset_profile(block);
  421. break;
  422. case BLKROSET:
  423. rc = dasd_ioctl_set_ro(bdev, argp);
  424. break;
  425. case DASDAPIVER:
  426. rc = dasd_ioctl_api_version(argp);
  427. break;
  428. case BIODASDCMFENABLE:
  429. rc = enable_cmf(base->cdev);
  430. break;
  431. case BIODASDCMFDISABLE:
  432. rc = disable_cmf(base->cdev);
  433. break;
  434. case BIODASDREADALLCMB:
  435. rc = dasd_ioctl_readall_cmb(block, cmd, argp);
  436. break;
  437. default:
  438. /* if the discipline has an ioctl method try it. */
  439. rc = -ENOTTY;
  440. if (base->discipline->ioctl)
  441. rc = base->discipline->ioctl(block, cmd, argp);
  442. }
  443. dasd_put_device(base);
  444. return rc;
  445. }