dasd_ioctl.c 15 KB

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