1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 * Copyright IBM Corp. 1999, 2001
9 *
10 * i/o controls for the dasd driver.
11 */
12
13 #define KMSG_COMPONENT "dasd"
14
15 #include <linux/interrupt.h>
16 #include <linux/compat.h>
17 #include <linux/major.h>
18 #include <linux/fs.h>
19 #include <linux/blkpg.h>
20 #include <linux/slab.h>
21 #include <asm/ccwdev.h>
22 #include <asm/schid.h>
23 #include <asm/cmb.h>
24 #include <linux/uaccess.h>
25 #include <linux/dasd_mod.h>
26
27 /* This is ugly... */
28 #define PRINTK_HEADER "dasd_ioctl:"
29
30 #include "dasd_int.h"
31
32
33 static int
dasd_ioctl_api_version(void __user * argp)34 dasd_ioctl_api_version(void __user *argp)
35 {
36 int ver = DASD_API_VERSION;
37 return put_user(ver, (int __user *)argp);
38 }
39
40 /*
41 * Enable device.
42 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
43 */
44 static int
dasd_ioctl_enable(struct block_device * bdev)45 dasd_ioctl_enable(struct block_device *bdev)
46 {
47 struct dasd_device *base;
48
49 if (!capable(CAP_SYS_ADMIN))
50 return -EACCES;
51
52 base = dasd_device_from_gendisk(bdev->bd_disk);
53 if (!base)
54 return -ENODEV;
55
56 dasd_enable_device(base);
57 dasd_put_device(base);
58 return 0;
59 }
60
61 /*
62 * Disable device.
63 * Used by dasdfmt. Disable I/O operations but allow ioctls.
64 */
65 static int
dasd_ioctl_disable(struct block_device * bdev)66 dasd_ioctl_disable(struct block_device *bdev)
67 {
68 struct dasd_device *base;
69
70 if (!capable(CAP_SYS_ADMIN))
71 return -EACCES;
72
73 base = dasd_device_from_gendisk(bdev->bd_disk);
74 if (!base)
75 return -ENODEV;
76 /*
77 * Man this is sick. We don't do a real disable but only downgrade
78 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
79 * BIODASDDISABLE to disable accesses to the device via the block
80 * device layer but it still wants to do i/o on the device by
81 * using the BIODASDFMT ioctl. Therefore the correct state for the
82 * device is DASD_STATE_BASIC that allows to do basic i/o.
83 */
84 dasd_set_target_state(base, DASD_STATE_BASIC);
85 /*
86 * Set i_size to zero, since read, write, etc. check against this
87 * value.
88 */
89 set_capacity(bdev->bd_disk, 0);
90 dasd_put_device(base);
91 return 0;
92 }
93
94 /*
95 * Quiesce device.
96 */
dasd_ioctl_quiesce(struct dasd_block * block)97 static int dasd_ioctl_quiesce(struct dasd_block *block)
98 {
99 unsigned long flags;
100 struct dasd_device *base;
101
102 base = block->base;
103 if (!capable (CAP_SYS_ADMIN))
104 return -EACCES;
105
106 pr_info("%s: The DASD has been put in the quiesce "
107 "state\n", dev_name(&base->cdev->dev));
108 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
109 dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
110 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
111 return 0;
112 }
113
114
115 /*
116 * Resume device.
117 */
dasd_ioctl_resume(struct dasd_block * block)118 static int dasd_ioctl_resume(struct dasd_block *block)
119 {
120 unsigned long flags;
121 struct dasd_device *base;
122
123 base = block->base;
124 if (!capable (CAP_SYS_ADMIN))
125 return -EACCES;
126
127 pr_info("%s: I/O operations have been resumed "
128 "on the DASD\n", dev_name(&base->cdev->dev));
129 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
130 dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
131 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
132
133 dasd_schedule_block_bh(block);
134 dasd_schedule_device_bh(base);
135 return 0;
136 }
137
138 /*
139 * Abort all failfast I/O on a device.
140 */
dasd_ioctl_abortio(struct dasd_block * block)141 static int dasd_ioctl_abortio(struct dasd_block *block)
142 {
143 unsigned long flags;
144 struct dasd_device *base;
145 struct dasd_ccw_req *cqr, *n;
146
147 base = block->base;
148 if (!capable(CAP_SYS_ADMIN))
149 return -EACCES;
150
151 if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
152 return 0;
153 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
154
155 spin_lock_irqsave(&block->request_queue_lock, flags);
156 spin_lock(&block->queue_lock);
157 list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
158 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
159 cqr->callback_data &&
160 cqr->callback_data != DASD_SLEEPON_START_TAG &&
161 cqr->callback_data != DASD_SLEEPON_END_TAG) {
162 spin_unlock(&block->queue_lock);
163 blk_abort_request(cqr->callback_data);
164 spin_lock(&block->queue_lock);
165 }
166 }
167 spin_unlock(&block->queue_lock);
168 spin_unlock_irqrestore(&block->request_queue_lock, flags);
169
170 dasd_schedule_block_bh(block);
171 return 0;
172 }
173
174 /*
175 * Allow I/O on a device
176 */
dasd_ioctl_allowio(struct dasd_block * block)177 static int dasd_ioctl_allowio(struct dasd_block *block)
178 {
179 struct dasd_device *base;
180
181 base = block->base;
182 if (!capable(CAP_SYS_ADMIN))
183 return -EACCES;
184
185 if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
186 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
187
188 return 0;
189 }
190
191 /*
192 * performs formatting of _device_ according to _fdata_
193 * Note: The discipline's format_function is assumed to deliver formatting
194 * commands to format multiple units of the device. In terms of the ECKD
195 * devices this means CCWs are generated to format multiple tracks.
196 */
197 static int
dasd_format(struct dasd_block * block,struct format_data_t * fdata)198 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
199 {
200 struct dasd_device *base;
201 int rc;
202
203 base = block->base;
204 if (base->discipline->format_device == NULL)
205 return -EPERM;
206
207 if (base->state != DASD_STATE_BASIC) {
208 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
209 dev_name(&base->cdev->dev));
210 return -EBUSY;
211 }
212
213 DBF_DEV_EVENT(DBF_NOTICE, base,
214 "formatting units %u to %u (%u B blocks) flags %u",
215 fdata->start_unit,
216 fdata->stop_unit, fdata->blksize, fdata->intensity);
217
218 /* Since dasdfmt keeps the device open after it was disabled,
219 * there still exists an inode for this device.
220 * We must update i_blkbits, otherwise we might get errors when
221 * enabling the device later.
222 */
223 if (fdata->start_unit == 0) {
224 block->gdp->part0->bd_inode->i_blkbits =
225 blksize_bits(fdata->blksize);
226 }
227
228 rc = base->discipline->format_device(base, fdata, 1);
229 if (rc == -EAGAIN)
230 rc = base->discipline->format_device(base, fdata, 0);
231
232 return rc;
233 }
234
dasd_check_format(struct dasd_block * block,struct format_check_t * cdata)235 static int dasd_check_format(struct dasd_block *block,
236 struct format_check_t *cdata)
237 {
238 struct dasd_device *base;
239 int rc;
240
241 base = block->base;
242 if (!base->discipline->check_device_format)
243 return -ENOTTY;
244
245 rc = base->discipline->check_device_format(base, cdata, 1);
246 if (rc == -EAGAIN)
247 rc = base->discipline->check_device_format(base, cdata, 0);
248
249 return rc;
250 }
251
252 /*
253 * Format device.
254 */
255 static int
dasd_ioctl_format(struct block_device * bdev,void __user * argp)256 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
257 {
258 struct dasd_device *base;
259 struct format_data_t fdata;
260 int rc;
261
262 if (!capable(CAP_SYS_ADMIN))
263 return -EACCES;
264 if (!argp)
265 return -EINVAL;
266 base = dasd_device_from_gendisk(bdev->bd_disk);
267 if (!base)
268 return -ENODEV;
269 if (base->features & DASD_FEATURE_READONLY ||
270 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
271 dasd_put_device(base);
272 return -EROFS;
273 }
274 if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
275 dasd_put_device(base);
276 return -EFAULT;
277 }
278 if (bdev_is_partition(bdev)) {
279 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
280 dev_name(&base->cdev->dev));
281 dasd_put_device(base);
282 return -EINVAL;
283 }
284 rc = dasd_format(base->block, &fdata);
285 dasd_put_device(base);
286
287 return rc;
288 }
289
290 /*
291 * Check device format
292 */
dasd_ioctl_check_format(struct block_device * bdev,void __user * argp)293 static int dasd_ioctl_check_format(struct block_device *bdev, void __user *argp)
294 {
295 struct format_check_t cdata;
296 struct dasd_device *base;
297 int rc = 0;
298
299 if (!argp)
300 return -EINVAL;
301
302 base = dasd_device_from_gendisk(bdev->bd_disk);
303 if (!base)
304 return -ENODEV;
305 if (bdev_is_partition(bdev)) {
306 pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
307 dev_name(&base->cdev->dev));
308 rc = -EINVAL;
309 goto out_err;
310 }
311
312 if (copy_from_user(&cdata, argp, sizeof(cdata))) {
313 rc = -EFAULT;
314 goto out_err;
315 }
316
317 rc = dasd_check_format(base->block, &cdata);
318 if (rc)
319 goto out_err;
320
321 if (copy_to_user(argp, &cdata, sizeof(cdata)))
322 rc = -EFAULT;
323
324 out_err:
325 dasd_put_device(base);
326
327 return rc;
328 }
329
dasd_release_space(struct dasd_device * device,struct format_data_t * rdata)330 static int dasd_release_space(struct dasd_device *device,
331 struct format_data_t *rdata)
332 {
333 if (!device->discipline->is_ese && !device->discipline->is_ese(device))
334 return -ENOTSUPP;
335 if (!device->discipline->release_space)
336 return -ENOTSUPP;
337
338 return device->discipline->release_space(device, rdata);
339 }
340
341 /*
342 * Release allocated space
343 */
dasd_ioctl_release_space(struct block_device * bdev,void __user * argp)344 static int dasd_ioctl_release_space(struct block_device *bdev, void __user *argp)
345 {
346 struct format_data_t rdata;
347 struct dasd_device *base;
348 int rc = 0;
349
350 if (!capable(CAP_SYS_ADMIN))
351 return -EACCES;
352 if (!argp)
353 return -EINVAL;
354
355 base = dasd_device_from_gendisk(bdev->bd_disk);
356 if (!base)
357 return -ENODEV;
358 if (base->features & DASD_FEATURE_READONLY ||
359 test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
360 rc = -EROFS;
361 goto out_err;
362 }
363 if (bdev_is_partition(bdev)) {
364 pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n",
365 dev_name(&base->cdev->dev));
366 rc = -EINVAL;
367 goto out_err;
368 }
369
370 if (copy_from_user(&rdata, argp, sizeof(rdata))) {
371 rc = -EFAULT;
372 goto out_err;
373 }
374
375 rc = dasd_release_space(base, &rdata);
376
377 out_err:
378 dasd_put_device(base);
379
380 return rc;
381 }
382
383 #ifdef CONFIG_DASD_PROFILE
384 /*
385 * Reset device profile information
386 */
dasd_ioctl_reset_profile(struct dasd_block * block)387 static int dasd_ioctl_reset_profile(struct dasd_block *block)
388 {
389 dasd_profile_reset(&block->profile);
390 return 0;
391 }
392
393 /*
394 * Return device profile information
395 */
dasd_ioctl_read_profile(struct dasd_block * block,void __user * argp)396 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
397 {
398 struct dasd_profile_info_t *data;
399 int rc = 0;
400
401 data = kmalloc(sizeof(*data), GFP_KERNEL);
402 if (!data)
403 return -ENOMEM;
404
405 spin_lock_bh(&block->profile.lock);
406 if (block->profile.data) {
407 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
408 data->dasd_io_sects = block->profile.data->dasd_io_sects;
409 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
410 sizeof(data->dasd_io_secs));
411 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
412 sizeof(data->dasd_io_times));
413 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
414 sizeof(data->dasd_io_timps));
415 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
416 sizeof(data->dasd_io_time1));
417 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
418 sizeof(data->dasd_io_time2));
419 memcpy(data->dasd_io_time2ps,
420 block->profile.data->dasd_io_time2ps,
421 sizeof(data->dasd_io_time2ps));
422 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
423 sizeof(data->dasd_io_time3));
424 memcpy(data->dasd_io_nr_req,
425 block->profile.data->dasd_io_nr_req,
426 sizeof(data->dasd_io_nr_req));
427 spin_unlock_bh(&block->profile.lock);
428 } else {
429 spin_unlock_bh(&block->profile.lock);
430 rc = -EIO;
431 goto out;
432 }
433 if (copy_to_user(argp, data, sizeof(*data)))
434 rc = -EFAULT;
435 out:
436 kfree(data);
437 return rc;
438 }
439 #else
dasd_ioctl_reset_profile(struct dasd_block * block)440 static int dasd_ioctl_reset_profile(struct dasd_block *block)
441 {
442 return -ENOTTY;
443 }
444
dasd_ioctl_read_profile(struct dasd_block * block,void __user * argp)445 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
446 {
447 return -ENOTTY;
448 }
449 #endif
450
451 /*
452 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
453 */
__dasd_ioctl_information(struct dasd_block * block,struct dasd_information2_t * dasd_info)454 static int __dasd_ioctl_information(struct dasd_block *block,
455 struct dasd_information2_t *dasd_info)
456 {
457 struct subchannel_id sch_id;
458 struct ccw_dev_id dev_id;
459 struct dasd_device *base;
460 struct ccw_device *cdev;
461 struct list_head *l;
462 unsigned long flags;
463 int rc;
464
465 base = block->base;
466 if (!base->discipline || !base->discipline->fill_info)
467 return -EINVAL;
468
469 rc = base->discipline->fill_info(base, dasd_info);
470 if (rc)
471 return rc;
472
473 cdev = base->cdev;
474 ccw_device_get_id(cdev, &dev_id);
475 ccw_device_get_schid(cdev, &sch_id);
476
477 dasd_info->devno = dev_id.devno;
478 dasd_info->schid = sch_id.sch_no;
479 dasd_info->cu_type = cdev->id.cu_type;
480 dasd_info->cu_model = cdev->id.cu_model;
481 dasd_info->dev_type = cdev->id.dev_type;
482 dasd_info->dev_model = cdev->id.dev_model;
483 dasd_info->status = base->state;
484 /*
485 * The open_count is increased for every opener, that includes
486 * the blkdev_get in dasd_scan_partitions.
487 * This must be hidden from user-space.
488 */
489 dasd_info->open_count = atomic_read(&block->open_count);
490 if (!block->bdev)
491 dasd_info->open_count++;
492
493 /*
494 * check if device is really formatted
495 * LDL / CDL was returned by 'fill_info'
496 */
497 if ((base->state < DASD_STATE_READY) ||
498 (dasd_check_blocksize(block->bp_block)))
499 dasd_info->format = DASD_FORMAT_NONE;
500
501 dasd_info->features |=
502 ((base->features & DASD_FEATURE_READONLY) != 0);
503
504 memcpy(dasd_info->type, base->discipline->name, 4);
505
506 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
507 list_for_each(l, &base->ccw_queue)
508 dasd_info->chanq_len++;
509 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
510 return 0;
511 }
512
dasd_ioctl_information(struct dasd_block * block,void __user * argp,size_t copy_size)513 static int dasd_ioctl_information(struct dasd_block *block, void __user *argp,
514 size_t copy_size)
515 {
516 struct dasd_information2_t *dasd_info;
517 int error;
518
519 dasd_info = kzalloc(sizeof(*dasd_info), GFP_KERNEL);
520 if (!dasd_info)
521 return -ENOMEM;
522
523 error = __dasd_ioctl_information(block, dasd_info);
524 if (!error && copy_to_user(argp, dasd_info, copy_size))
525 error = -EFAULT;
526 kfree(dasd_info);
527 return error;
528 }
529
530 /*
531 * Set read only
532 */
dasd_set_read_only(struct block_device * bdev,bool ro)533 int dasd_set_read_only(struct block_device *bdev, bool ro)
534 {
535 struct dasd_device *base;
536 int rc;
537
538 /* do not manipulate hardware state for partitions */
539 if (bdev_is_partition(bdev))
540 return 0;
541
542 base = dasd_device_from_gendisk(bdev->bd_disk);
543 if (!base)
544 return -ENODEV;
545 if (!ro && test_bit(DASD_FLAG_DEVICE_RO, &base->flags))
546 rc = -EROFS;
547 else
548 rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, ro);
549 dasd_put_device(base);
550 return rc;
551 }
552
dasd_ioctl_readall_cmb(struct dasd_block * block,unsigned int cmd,struct cmbdata __user * argp)553 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
554 struct cmbdata __user *argp)
555 {
556 size_t size = _IOC_SIZE(cmd);
557 struct cmbdata data;
558 int ret;
559
560 ret = cmf_readall(block->base->cdev, &data);
561 if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
562 return -EFAULT;
563 return ret;
564 }
565
dasd_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)566 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
567 unsigned int cmd, unsigned long arg)
568 {
569 struct dasd_block *block;
570 struct dasd_device *base;
571 void __user *argp;
572 int rc;
573
574 if (is_compat_task())
575 argp = compat_ptr(arg);
576 else
577 argp = (void __user *)arg;
578
579 if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg)
580 return -EINVAL;
581
582 base = dasd_device_from_gendisk(bdev->bd_disk);
583 if (!base)
584 return -ENODEV;
585 block = base->block;
586 rc = 0;
587 switch (cmd) {
588 case BIODASDDISABLE:
589 rc = dasd_ioctl_disable(bdev);
590 break;
591 case BIODASDENABLE:
592 rc = dasd_ioctl_enable(bdev);
593 break;
594 case BIODASDQUIESCE:
595 rc = dasd_ioctl_quiesce(block);
596 break;
597 case BIODASDRESUME:
598 rc = dasd_ioctl_resume(block);
599 break;
600 case BIODASDABORTIO:
601 rc = dasd_ioctl_abortio(block);
602 break;
603 case BIODASDALLOWIO:
604 rc = dasd_ioctl_allowio(block);
605 break;
606 case BIODASDFMT:
607 rc = dasd_ioctl_format(bdev, argp);
608 break;
609 case BIODASDCHECKFMT:
610 rc = dasd_ioctl_check_format(bdev, argp);
611 break;
612 case BIODASDINFO:
613 rc = dasd_ioctl_information(block, argp,
614 sizeof(struct dasd_information_t));
615 break;
616 case BIODASDINFO2:
617 rc = dasd_ioctl_information(block, argp,
618 sizeof(struct dasd_information2_t));
619 break;
620 case BIODASDPRRD:
621 rc = dasd_ioctl_read_profile(block, argp);
622 break;
623 case BIODASDPRRST:
624 rc = dasd_ioctl_reset_profile(block);
625 break;
626 case DASDAPIVER:
627 rc = dasd_ioctl_api_version(argp);
628 break;
629 case BIODASDCMFENABLE:
630 rc = enable_cmf(base->cdev);
631 break;
632 case BIODASDCMFDISABLE:
633 rc = disable_cmf(base->cdev);
634 break;
635 case BIODASDREADALLCMB:
636 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
637 break;
638 case BIODASDRAS:
639 rc = dasd_ioctl_release_space(bdev, argp);
640 break;
641 default:
642 /* if the discipline has an ioctl method try it. */
643 rc = -ENOTTY;
644 if (base->discipline->ioctl)
645 rc = base->discipline->ioctl(block, cmd, argp);
646 }
647 dasd_put_device(base);
648 return rc;
649 }
650
651
652 /**
653 * dasd_biodasdinfo() - fill out the dasd information structure
654 * @disk [in]: pointer to gendisk structure that references a DASD
655 * @info [out]: pointer to the dasd_information2_t structure
656 *
657 * Provide access to DASD specific information.
658 * The gendisk structure is checked if it belongs to the DASD driver by
659 * comparing the gendisk->fops pointer.
660 * If it does not belong to the DASD driver -EINVAL is returned.
661 * Otherwise the provided dasd_information2_t structure is filled out.
662 *
663 * Returns:
664 * %0 on success and a negative error value on failure.
665 */
dasd_biodasdinfo(struct gendisk * disk,struct dasd_information2_t * info)666 int dasd_biodasdinfo(struct gendisk *disk, struct dasd_information2_t *info)
667 {
668 struct dasd_device *base;
669 int error;
670
671 if (disk->fops != &dasd_device_operations)
672 return -EINVAL;
673
674 base = dasd_device_from_gendisk(disk);
675 if (!base)
676 return -ENODEV;
677 error = __dasd_ioctl_information(base->block, info);
678 dasd_put_device(base);
679 return error;
680 }
681 /* export that symbol_get in partition detection is possible */
682 EXPORT_SYMBOL_GPL(dasd_biodasdinfo);
683