dasd_ioctl.c 13 KB

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