dasd_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. /*
  24. * SECTION: ioctl functions.
  25. */
  26. static struct list_head dasd_ioctl_list = LIST_HEAD_INIT(dasd_ioctl_list);
  27. /*
  28. * Find the ioctl with number no.
  29. */
  30. static struct dasd_ioctl *
  31. dasd_find_ioctl(int no)
  32. {
  33. struct dasd_ioctl *ioctl;
  34. list_for_each_entry (ioctl, &dasd_ioctl_list, list)
  35. if (ioctl->no == no)
  36. return ioctl;
  37. return NULL;
  38. }
  39. /*
  40. * Register ioctl with number no.
  41. */
  42. int
  43. dasd_ioctl_no_register(struct module *owner, int no, dasd_ioctl_fn_t handler)
  44. {
  45. struct dasd_ioctl *new;
  46. if (dasd_find_ioctl(no))
  47. return -EBUSY;
  48. new = kmalloc(sizeof (struct dasd_ioctl), GFP_KERNEL);
  49. if (new == NULL)
  50. return -ENOMEM;
  51. new->owner = owner;
  52. new->no = no;
  53. new->handler = handler;
  54. list_add(&new->list, &dasd_ioctl_list);
  55. return 0;
  56. }
  57. /*
  58. * Deregister ioctl with number no.
  59. */
  60. int
  61. dasd_ioctl_no_unregister(struct module *owner, int no, dasd_ioctl_fn_t handler)
  62. {
  63. struct dasd_ioctl *old = dasd_find_ioctl(no);
  64. if (old == NULL)
  65. return -ENOENT;
  66. if (old->no != no || old->handler != handler || owner != old->owner)
  67. return -EINVAL;
  68. list_del(&old->list);
  69. kfree(old);
  70. return 0;
  71. }
  72. static int
  73. dasd_ioctl_api_version(void __user *argp)
  74. {
  75. int ver = DASD_API_VERSION;
  76. return put_user(ver, (int *)argp);
  77. }
  78. /*
  79. * Enable device.
  80. * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
  81. */
  82. static int
  83. dasd_ioctl_enable(struct block_device *bdev)
  84. {
  85. struct dasd_device *device = bdev->bd_disk->private_data;
  86. if (!capable(CAP_SYS_ADMIN))
  87. return -EACCES;
  88. dasd_enable_device(device);
  89. /* Formatting the dasd device can change the capacity. */
  90. mutex_lock(&bdev->bd_mutex);
  91. i_size_write(bdev->bd_inode, (loff_t)get_capacity(device->gdp) << 9);
  92. mutex_unlock(&bdev->bd_mutex);
  93. return 0;
  94. }
  95. /*
  96. * Disable device.
  97. * Used by dasdfmt. Disable I/O operations but allow ioctls.
  98. */
  99. static int
  100. dasd_ioctl_disable(struct block_device *bdev)
  101. {
  102. struct dasd_device *device = bdev->bd_disk->private_data;
  103. if (!capable(CAP_SYS_ADMIN))
  104. return -EACCES;
  105. /*
  106. * Man this is sick. We don't do a real disable but only downgrade
  107. * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
  108. * BIODASDDISABLE to disable accesses to the device via the block
  109. * device layer but it still wants to do i/o on the device by
  110. * using the BIODASDFMT ioctl. Therefore the correct state for the
  111. * device is DASD_STATE_BASIC that allows to do basic i/o.
  112. */
  113. dasd_set_target_state(device, DASD_STATE_BASIC);
  114. /*
  115. * Set i_size to zero, since read, write, etc. check against this
  116. * value.
  117. */
  118. mutex_lock(&bdev->bd_mutex);
  119. i_size_write(bdev->bd_inode, 0);
  120. mutex_unlock(&bdev->bd_mutex);
  121. return 0;
  122. }
  123. /*
  124. * Quiesce device.
  125. */
  126. static int
  127. dasd_ioctl_quiesce(struct dasd_device *device)
  128. {
  129. unsigned long flags;
  130. if (!capable (CAP_SYS_ADMIN))
  131. return -EACCES;
  132. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  133. "Quiesce IO on device");
  134. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  135. device->stopped |= DASD_STOPPED_QUIESCE;
  136. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  137. return 0;
  138. }
  139. /*
  140. * Quiesce device.
  141. */
  142. static int
  143. dasd_ioctl_resume(struct dasd_device *device)
  144. {
  145. unsigned long flags;
  146. if (!capable (CAP_SYS_ADMIN))
  147. return -EACCES;
  148. DEV_MESSAGE (KERN_DEBUG, device, "%s",
  149. "resume IO on device");
  150. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  151. device->stopped &= ~DASD_STOPPED_QUIESCE;
  152. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  153. dasd_schedule_bh (device);
  154. return 0;
  155. }
  156. /*
  157. * performs formatting of _device_ according to _fdata_
  158. * Note: The discipline's format_function is assumed to deliver formatting
  159. * commands to format a single unit of the device. In terms of the ECKD
  160. * devices this means CCWs are generated to format a single track.
  161. */
  162. static int
  163. dasd_format(struct dasd_device * device, struct format_data_t * fdata)
  164. {
  165. struct dasd_ccw_req *cqr;
  166. int rc;
  167. if (device->discipline->format_device == NULL)
  168. return -EPERM;
  169. if (device->state != DASD_STATE_BASIC) {
  170. DEV_MESSAGE(KERN_WARNING, device, "%s",
  171. "dasd_format: device is not disabled! ");
  172. return -EBUSY;
  173. }
  174. DBF_DEV_EVENT(DBF_NOTICE, device,
  175. "formatting units %d to %d (%d B blocks) flags %d",
  176. fdata->start_unit,
  177. fdata->stop_unit, fdata->blksize, fdata->intensity);
  178. /* Since dasdfmt keeps the device open after it was disabled,
  179. * there still exists an inode for this device.
  180. * We must update i_blkbits, otherwise we might get errors when
  181. * enabling the device later.
  182. */
  183. if (fdata->start_unit == 0) {
  184. struct block_device *bdev = bdget_disk(device->gdp, 0);
  185. bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
  186. bdput(bdev);
  187. }
  188. while (fdata->start_unit <= fdata->stop_unit) {
  189. cqr = device->discipline->format_device(device, fdata);
  190. if (IS_ERR(cqr))
  191. return PTR_ERR(cqr);
  192. rc = dasd_sleep_on_interruptible(cqr);
  193. dasd_sfree_request(cqr, cqr->device);
  194. if (rc) {
  195. if (rc != -ERESTARTSYS)
  196. DEV_MESSAGE(KERN_ERR, device,
  197. " Formatting of unit %d failed "
  198. "with rc = %d",
  199. fdata->start_unit, rc);
  200. return rc;
  201. }
  202. fdata->start_unit++;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * Format device.
  208. */
  209. static int
  210. dasd_ioctl_format(struct block_device *bdev, void __user *argp)
  211. {
  212. struct dasd_device *device = bdev->bd_disk->private_data;
  213. struct format_data_t fdata;
  214. if (!capable(CAP_SYS_ADMIN))
  215. return -EACCES;
  216. if (!argp)
  217. return -EINVAL;
  218. if (device->features & DASD_FEATURE_READONLY)
  219. return -EROFS;
  220. if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
  221. return -EFAULT;
  222. if (bdev != bdev->bd_contains) {
  223. DEV_MESSAGE(KERN_WARNING, device, "%s",
  224. "Cannot low-level format a partition");
  225. return -EINVAL;
  226. }
  227. return dasd_format(device, &fdata);
  228. }
  229. #ifdef CONFIG_DASD_PROFILE
  230. /*
  231. * Reset device profile information
  232. */
  233. static int
  234. dasd_ioctl_reset_profile(struct dasd_device *device)
  235. {
  236. memset(&device->profile, 0, sizeof (struct dasd_profile_info_t));
  237. return 0;
  238. }
  239. /*
  240. * Return device profile information
  241. */
  242. static int
  243. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  244. {
  245. if (dasd_profile_level == DASD_PROFILE_OFF)
  246. return -EIO;
  247. if (copy_to_user(argp, &device->profile,
  248. sizeof (struct dasd_profile_info_t)))
  249. return -EFAULT;
  250. return 0;
  251. }
  252. #else
  253. static int
  254. dasd_ioctl_reset_profile(struct dasd_device *device)
  255. {
  256. return -ENOSYS;
  257. }
  258. static int
  259. dasd_ioctl_read_profile(struct dasd_device *device, void __user *argp)
  260. {
  261. return -ENOSYS;
  262. }
  263. #endif
  264. /*
  265. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  266. */
  267. static int
  268. dasd_ioctl_information(struct dasd_device *device,
  269. unsigned int cmd, void __user *argp)
  270. {
  271. struct dasd_information2_t *dasd_info;
  272. unsigned long flags;
  273. int rc;
  274. struct ccw_device *cdev;
  275. if (!device->discipline->fill_info)
  276. return -EINVAL;
  277. dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  278. if (dasd_info == NULL)
  279. return -ENOMEM;
  280. rc = device->discipline->fill_info(device, dasd_info);
  281. if (rc) {
  282. kfree(dasd_info);
  283. return rc;
  284. }
  285. cdev = device->cdev;
  286. dasd_info->devno = _ccw_device_get_device_number(device->cdev);
  287. dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
  288. dasd_info->cu_type = cdev->id.cu_type;
  289. dasd_info->cu_model = cdev->id.cu_model;
  290. dasd_info->dev_type = cdev->id.dev_type;
  291. dasd_info->dev_model = cdev->id.dev_model;
  292. dasd_info->status = device->state;
  293. /*
  294. * The open_count is increased for every opener, that includes
  295. * the blkdev_get in dasd_scan_partitions.
  296. * This must be hidden from user-space.
  297. */
  298. dasd_info->open_count = atomic_read(&device->open_count);
  299. if (!device->bdev)
  300. dasd_info->open_count++;
  301. /*
  302. * check if device is really formatted
  303. * LDL / CDL was returned by 'fill_info'
  304. */
  305. if ((device->state < DASD_STATE_READY) ||
  306. (dasd_check_blocksize(device->bp_block)))
  307. dasd_info->format = DASD_FORMAT_NONE;
  308. dasd_info->features |=
  309. ((device->features & DASD_FEATURE_READONLY) != 0);
  310. if (device->discipline)
  311. memcpy(dasd_info->type, device->discipline->name, 4);
  312. else
  313. memcpy(dasd_info->type, "none", 4);
  314. dasd_info->req_queue_len = 0;
  315. dasd_info->chanq_len = 0;
  316. if (device->request_queue->request_fn) {
  317. struct list_head *l;
  318. #ifdef DASD_EXTENDED_PROFILING
  319. {
  320. struct list_head *l;
  321. spin_lock_irqsave(&device->lock, flags);
  322. list_for_each(l, &device->request_queue->queue_head)
  323. dasd_info->req_queue_len++;
  324. spin_unlock_irqrestore(&device->lock, flags);
  325. }
  326. #endif /* DASD_EXTENDED_PROFILING */
  327. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  328. list_for_each(l, &device->ccw_queue)
  329. dasd_info->chanq_len++;
  330. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  331. flags);
  332. }
  333. rc = 0;
  334. if (copy_to_user(argp, dasd_info,
  335. ((cmd == (unsigned int) BIODASDINFO2) ?
  336. sizeof (struct dasd_information2_t) :
  337. sizeof (struct dasd_information_t))))
  338. rc = -EFAULT;
  339. kfree(dasd_info);
  340. return rc;
  341. }
  342. /*
  343. * Set read only
  344. */
  345. static int
  346. dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
  347. {
  348. struct dasd_device *device = bdev->bd_disk->private_data;
  349. int intval;
  350. if (!capable(CAP_SYS_ADMIN))
  351. return -EACCES;
  352. if (bdev != bdev->bd_contains)
  353. // ro setting is not allowed for partitions
  354. return -EINVAL;
  355. if (get_user(intval, (int *)argp))
  356. return -EFAULT;
  357. set_disk_ro(bdev->bd_disk, intval);
  358. return dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
  359. }
  360. static int
  361. dasd_ioctl_readall_cmb(struct dasd_device *device, unsigned int cmd,
  362. unsigned long arg)
  363. {
  364. struct cmbdata __user *argp = (void __user *) arg;
  365. size_t size = _IOC_SIZE(cmd);
  366. struct cmbdata data;
  367. int ret;
  368. ret = cmf_readall(device->cdev, &data);
  369. if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
  370. return -EFAULT;
  371. return ret;
  372. }
  373. int
  374. dasd_ioctl(struct inode *inode, struct file *file,
  375. unsigned int cmd, unsigned long arg)
  376. {
  377. struct block_device *bdev = inode->i_bdev;
  378. struct dasd_device *device = bdev->bd_disk->private_data;
  379. void __user *argp = (void __user *)arg;
  380. struct dasd_ioctl *ioctl;
  381. int rc;
  382. if (!device)
  383. return -ENODEV;
  384. if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
  385. PRINT_DEBUG("empty data ptr");
  386. return -EINVAL;
  387. }
  388. switch (cmd) {
  389. case BIODASDDISABLE:
  390. return dasd_ioctl_disable(bdev);
  391. case BIODASDENABLE:
  392. return dasd_ioctl_enable(bdev);
  393. case BIODASDQUIESCE:
  394. return dasd_ioctl_quiesce(device);
  395. case BIODASDRESUME:
  396. return dasd_ioctl_resume(device);
  397. case BIODASDFMT:
  398. return dasd_ioctl_format(bdev, argp);
  399. case BIODASDINFO:
  400. return dasd_ioctl_information(device, cmd, argp);
  401. case BIODASDINFO2:
  402. return dasd_ioctl_information(device, cmd, argp);
  403. case BIODASDPRRD:
  404. return dasd_ioctl_read_profile(device, argp);
  405. case BIODASDPRRST:
  406. return dasd_ioctl_reset_profile(device);
  407. case BLKROSET:
  408. return dasd_ioctl_set_ro(bdev, argp);
  409. case DASDAPIVER:
  410. return dasd_ioctl_api_version(argp);
  411. case BIODASDCMFENABLE:
  412. return enable_cmf(device->cdev);
  413. case BIODASDCMFDISABLE:
  414. return disable_cmf(device->cdev);
  415. case BIODASDREADALLCMB:
  416. return dasd_ioctl_readall_cmb(device, cmd, arg);
  417. default:
  418. /* if the discipline has an ioctl method try it. */
  419. if (device->discipline->ioctl) {
  420. int rval = device->discipline->ioctl(device, cmd, argp);
  421. if (rval != -ENOIOCTLCMD)
  422. return rval;
  423. }
  424. /* else resort to the deprecated dynamic ioctl list */
  425. list_for_each_entry(ioctl, &dasd_ioctl_list, list) {
  426. if (ioctl->no == cmd) {
  427. /* Found a matching ioctl. Call it. */
  428. if (!try_module_get(ioctl->owner))
  429. continue;
  430. rc = ioctl->handler(bdev, cmd, arg);
  431. module_put(ioctl->owner);
  432. return rc;
  433. }
  434. }
  435. return -EINVAL;
  436. }
  437. }
  438. long
  439. dasd_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  440. {
  441. int rval;
  442. lock_kernel();
  443. rval = dasd_ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
  444. unlock_kernel();
  445. return (rval == -EINVAL) ? -ENOIOCTLCMD : rval;
  446. }
  447. EXPORT_SYMBOL(dasd_ioctl_no_register);
  448. EXPORT_SYMBOL(dasd_ioctl_no_unregister);