dasd_ioctl.c 13 KB

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