dasd_ioctl.c 12 KB

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