dasd_ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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.50 $
  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 (dasd_profile_level == DASD_PROFILE_OFF)
  313. return -EIO;
  314. if (copy_to_user((long __user *) args, (long *) &device->profile,
  315. sizeof (struct dasd_profile_info_t)))
  316. return -EFAULT;
  317. return 0;
  318. }
  319. #else
  320. static int
  321. dasd_ioctl_reset_profile(struct block_device *bdev, int no, long args)
  322. {
  323. return -ENOSYS;
  324. }
  325. static int
  326. dasd_ioctl_read_profile(struct block_device *bdev, int no, long args)
  327. {
  328. return -ENOSYS;
  329. }
  330. #endif
  331. /*
  332. * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
  333. */
  334. static int
  335. dasd_ioctl_information(struct block_device *bdev, int no, long args)
  336. {
  337. struct dasd_device *device;
  338. struct dasd_information2_t *dasd_info;
  339. unsigned long flags;
  340. int rc;
  341. struct ccw_device *cdev;
  342. device = bdev->bd_disk->private_data;
  343. if (device == NULL)
  344. return -ENODEV;
  345. if (!device->discipline->fill_info)
  346. return -EINVAL;
  347. dasd_info = kmalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
  348. if (dasd_info == NULL)
  349. return -ENOMEM;
  350. rc = device->discipline->fill_info(device, dasd_info);
  351. if (rc) {
  352. kfree(dasd_info);
  353. return rc;
  354. }
  355. cdev = device->cdev;
  356. dasd_info->devno = _ccw_device_get_device_number(device->cdev);
  357. dasd_info->schid = _ccw_device_get_subchannel_number(device->cdev);
  358. dasd_info->cu_type = cdev->id.cu_type;
  359. dasd_info->cu_model = cdev->id.cu_model;
  360. dasd_info->dev_type = cdev->id.dev_type;
  361. dasd_info->dev_model = cdev->id.dev_model;
  362. dasd_info->open_count = atomic_read(&device->open_count);
  363. dasd_info->status = device->state;
  364. /*
  365. * check if device is really formatted
  366. * LDL / CDL was returned by 'fill_info'
  367. */
  368. if ((device->state < DASD_STATE_READY) ||
  369. (dasd_check_blocksize(device->bp_block)))
  370. dasd_info->format = DASD_FORMAT_NONE;
  371. dasd_info->features |=
  372. ((device->features & DASD_FEATURE_READONLY) != 0);
  373. if (device->discipline)
  374. memcpy(dasd_info->type, device->discipline->name, 4);
  375. else
  376. memcpy(dasd_info->type, "none", 4);
  377. dasd_info->req_queue_len = 0;
  378. dasd_info->chanq_len = 0;
  379. if (device->request_queue->request_fn) {
  380. struct list_head *l;
  381. #ifdef DASD_EXTENDED_PROFILING
  382. {
  383. struct list_head *l;
  384. spin_lock_irqsave(&device->lock, flags);
  385. list_for_each(l, &device->request_queue->queue_head)
  386. dasd_info->req_queue_len++;
  387. spin_unlock_irqrestore(&device->lock, flags);
  388. }
  389. #endif /* DASD_EXTENDED_PROFILING */
  390. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  391. list_for_each(l, &device->ccw_queue)
  392. dasd_info->chanq_len++;
  393. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
  394. flags);
  395. }
  396. rc = 0;
  397. if (copy_to_user((long __user *) args, (long *) dasd_info,
  398. ((no == (unsigned int) BIODASDINFO2) ?
  399. sizeof (struct dasd_information2_t) :
  400. sizeof (struct dasd_information_t))))
  401. rc = -EFAULT;
  402. kfree(dasd_info);
  403. return rc;
  404. }
  405. /*
  406. * Set read only
  407. */
  408. static int
  409. dasd_ioctl_set_ro(struct block_device *bdev, int no, long args)
  410. {
  411. struct dasd_device *device;
  412. int intval, rc;
  413. if (!capable(CAP_SYS_ADMIN))
  414. return -EACCES;
  415. if (bdev != bdev->bd_contains)
  416. // ro setting is not allowed for partitions
  417. return -EINVAL;
  418. if (get_user(intval, (int __user *) args))
  419. return -EFAULT;
  420. device = bdev->bd_disk->private_data;
  421. if (device == NULL)
  422. return -ENODEV;
  423. set_disk_ro(bdev->bd_disk, intval);
  424. rc = dasd_set_feature(device->cdev, DASD_FEATURE_READONLY, intval);
  425. return rc;
  426. }
  427. /*
  428. * List of static ioctls.
  429. */
  430. static struct { int no; dasd_ioctl_fn_t fn; } dasd_ioctls[] =
  431. {
  432. { BIODASDDISABLE, dasd_ioctl_disable },
  433. { BIODASDENABLE, dasd_ioctl_enable },
  434. { BIODASDQUIESCE, dasd_ioctl_quiesce },
  435. { BIODASDRESUME, dasd_ioctl_resume },
  436. { BIODASDFMT, dasd_ioctl_format },
  437. { BIODASDINFO, dasd_ioctl_information },
  438. { BIODASDINFO2, dasd_ioctl_information },
  439. { BIODASDPRRD, dasd_ioctl_read_profile },
  440. { BIODASDPRRST, dasd_ioctl_reset_profile },
  441. { BLKROSET, dasd_ioctl_set_ro },
  442. { DASDAPIVER, dasd_ioctl_api_version },
  443. { -1, NULL }
  444. };
  445. int
  446. dasd_ioctl_init(void)
  447. {
  448. int i;
  449. for (i = 0; dasd_ioctls[i].no != -1; i++)
  450. dasd_ioctl_no_register(NULL, dasd_ioctls[i].no,
  451. dasd_ioctls[i].fn);
  452. return 0;
  453. }
  454. void
  455. dasd_ioctl_exit(void)
  456. {
  457. int i;
  458. for (i = 0; dasd_ioctls[i].no != -1; i++)
  459. dasd_ioctl_no_unregister(NULL, dasd_ioctls[i].no,
  460. dasd_ioctls[i].fn);
  461. }
  462. EXPORT_SYMBOL(dasd_ioctl_no_register);
  463. EXPORT_SYMBOL(dasd_ioctl_no_unregister);