1 // SPDX-License-Identifier: GPL-2.0
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
10 * i/o controls for the dasd driver.
13 #include <linux/interrupt.h>
14 #include <linux/compat.h>
15 #include <linux/major.h>
17 #include <linux/blkpg.h>
18 #include <linux/slab.h>
19 #include <asm/ccwdev.h>
20 #include <asm/schid.h>
22 #include <linux/uaccess.h>
23 #include <linux/dasd_mod.h>
28 dasd_ioctl_api_version(void __user
*argp
)
30 int ver
= DASD_API_VERSION
;
31 return put_user(ver
, (int __user
*)argp
);
36 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
39 dasd_ioctl_enable(struct block_device
*bdev
)
41 struct dasd_device
*base
;
43 if (!capable(CAP_SYS_ADMIN
))
46 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
50 dasd_enable_device(base
);
51 dasd_put_device(base
);
57 * Used by dasdfmt. Disable I/O operations but allow ioctls.
60 dasd_ioctl_disable(struct block_device
*bdev
)
62 struct dasd_device
*base
;
64 if (!capable(CAP_SYS_ADMIN
))
67 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
71 * Man this is sick. We don't do a real disable but only downgrade
72 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
73 * BIODASDDISABLE to disable accesses to the device via the block
74 * device layer but it still wants to do i/o on the device by
75 * using the BIODASDFMT ioctl. Therefore the correct state for the
76 * device is DASD_STATE_BASIC that allows to do basic i/o.
78 dasd_set_target_state(base
, DASD_STATE_BASIC
);
80 * Set i_size to zero, since read, write, etc. check against this
83 set_capacity(bdev
->bd_disk
, 0);
84 dasd_put_device(base
);
91 static int dasd_ioctl_quiesce(struct dasd_block
*block
)
94 struct dasd_device
*base
;
97 if (!capable (CAP_SYS_ADMIN
))
100 pr_info("%s: The DASD has been put in the quiesce "
101 "state\n", dev_name(&base
->cdev
->dev
));
102 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
103 dasd_device_set_stop_bits(base
, DASD_STOPPED_QUIESCE
);
104 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
), flags
);
112 static int dasd_ioctl_resume(struct dasd_block
*block
)
115 struct dasd_device
*base
;
118 if (!capable (CAP_SYS_ADMIN
))
121 pr_info("%s: I/O operations have been resumed "
122 "on the DASD\n", dev_name(&base
->cdev
->dev
));
123 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
124 dasd_device_remove_stop_bits(base
, DASD_STOPPED_QUIESCE
);
125 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
), flags
);
127 dasd_schedule_block_bh(block
);
128 dasd_schedule_device_bh(base
);
133 * Abort all failfast I/O on a device.
135 static int dasd_ioctl_abortio(struct dasd_block
*block
)
138 struct dasd_device
*base
;
139 struct dasd_ccw_req
*cqr
, *n
;
142 if (!capable(CAP_SYS_ADMIN
))
145 if (test_and_set_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
147 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag set");
149 spin_lock_irqsave(&block
->request_queue_lock
, flags
);
150 spin_lock(&block
->queue_lock
);
151 list_for_each_entry_safe(cqr
, n
, &block
->ccw_queue
, blocklist
) {
152 if (test_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
) &&
153 cqr
->callback_data
&&
154 cqr
->callback_data
!= DASD_SLEEPON_START_TAG
&&
155 cqr
->callback_data
!= DASD_SLEEPON_END_TAG
) {
156 spin_unlock(&block
->queue_lock
);
157 blk_abort_request(cqr
->callback_data
);
158 spin_lock(&block
->queue_lock
);
161 spin_unlock(&block
->queue_lock
);
162 spin_unlock_irqrestore(&block
->request_queue_lock
, flags
);
164 dasd_schedule_block_bh(block
);
169 * Allow I/O on a device
171 static int dasd_ioctl_allowio(struct dasd_block
*block
)
173 struct dasd_device
*base
;
176 if (!capable(CAP_SYS_ADMIN
))
179 if (test_and_clear_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
180 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag unset");
186 * performs formatting of _device_ according to _fdata_
187 * Note: The discipline's format_function is assumed to deliver formatting
188 * commands to format multiple units of the device. In terms of the ECKD
189 * devices this means CCWs are generated to format multiple tracks.
192 dasd_format(struct dasd_block
*block
, struct format_data_t
*fdata
)
194 struct dasd_device
*base
;
198 if (base
->discipline
->format_device
== NULL
)
201 if (base
->state
!= DASD_STATE_BASIC
) {
202 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
203 dev_name(&base
->cdev
->dev
));
207 DBF_DEV_EVENT(DBF_NOTICE
, base
,
208 "formatting units %u to %u (%u B blocks) flags %u",
210 fdata
->stop_unit
, fdata
->blksize
, fdata
->intensity
);
212 /* Since dasdfmt keeps the device open after it was disabled,
213 * there still exists an inode for this device.
214 * We must update i_blkbits, otherwise we might get errors when
215 * enabling the device later.
217 if (fdata
->start_unit
== 0) {
218 block
->gdp
->part0
->bd_mapping
->host
->i_blkbits
=
219 blksize_bits(fdata
->blksize
);
222 rc
= base
->discipline
->format_device(base
, fdata
, 1);
224 rc
= base
->discipline
->format_device(base
, fdata
, 0);
229 static int dasd_check_format(struct dasd_block
*block
,
230 struct format_check_t
*cdata
)
232 struct dasd_device
*base
;
236 if (!base
->discipline
->check_device_format
)
239 rc
= base
->discipline
->check_device_format(base
, cdata
, 1);
241 rc
= base
->discipline
->check_device_format(base
, cdata
, 0);
250 dasd_ioctl_format(struct block_device
*bdev
, void __user
*argp
)
252 struct dasd_device
*base
;
253 struct format_data_t fdata
;
256 if (!capable(CAP_SYS_ADMIN
))
260 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
263 if (base
->features
& DASD_FEATURE_READONLY
||
264 test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
265 dasd_put_device(base
);
268 if (copy_from_user(&fdata
, argp
, sizeof(struct format_data_t
))) {
269 dasd_put_device(base
);
272 if (bdev_is_partition(bdev
)) {
273 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
274 dev_name(&base
->cdev
->dev
));
275 dasd_put_device(base
);
278 rc
= dasd_format(base
->block
, &fdata
);
279 dasd_put_device(base
);
285 * Check device format
287 static int dasd_ioctl_check_format(struct block_device
*bdev
, void __user
*argp
)
289 struct format_check_t cdata
;
290 struct dasd_device
*base
;
296 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
299 if (bdev_is_partition(bdev
)) {
300 pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
301 dev_name(&base
->cdev
->dev
));
306 if (copy_from_user(&cdata
, argp
, sizeof(cdata
))) {
311 rc
= dasd_check_format(base
->block
, &cdata
);
315 if (copy_to_user(argp
, &cdata
, sizeof(cdata
)))
319 dasd_put_device(base
);
324 static int dasd_release_space(struct dasd_device
*device
,
325 struct format_data_t
*rdata
)
327 if (!device
->discipline
->is_ese
&& !device
->discipline
->is_ese(device
))
329 if (!device
->discipline
->release_space
)
332 return device
->discipline
->release_space(device
, rdata
);
336 * Release allocated space
338 static int dasd_ioctl_release_space(struct block_device
*bdev
, void __user
*argp
)
340 struct format_data_t rdata
;
341 struct dasd_device
*base
;
344 if (!capable(CAP_SYS_ADMIN
))
349 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
352 if (base
->features
& DASD_FEATURE_READONLY
||
353 test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
357 if (bdev_is_partition(bdev
)) {
358 pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n",
359 dev_name(&base
->cdev
->dev
));
364 if (copy_from_user(&rdata
, argp
, sizeof(rdata
))) {
369 rc
= dasd_release_space(base
, &rdata
);
372 dasd_put_device(base
);
378 * Swap driver iternal copy relation.
381 dasd_ioctl_copy_pair_swap(struct block_device
*bdev
, void __user
*argp
)
383 struct dasd_copypair_swap_data_t data
;
384 struct dasd_device
*device
;
387 if (!capable(CAP_SYS_ADMIN
))
390 device
= dasd_device_from_gendisk(bdev
->bd_disk
);
394 if (copy_from_user(&data
, argp
, sizeof(struct dasd_copypair_swap_data_t
))) {
395 dasd_put_device(device
);
398 if (memchr_inv(data
.reserved
, 0, sizeof(data
.reserved
))) {
399 pr_warn("%s: Invalid swap data specified\n",
400 dev_name(&device
->cdev
->dev
));
401 dasd_put_device(device
);
402 return DASD_COPYPAIRSWAP_INVALID
;
404 if (bdev_is_partition(bdev
)) {
405 pr_warn("%s: The specified DASD is a partition and cannot be swapped\n",
406 dev_name(&device
->cdev
->dev
));
407 dasd_put_device(device
);
408 return DASD_COPYPAIRSWAP_INVALID
;
411 pr_warn("%s: The specified DASD has no copy pair set up\n",
412 dev_name(&device
->cdev
->dev
));
413 dasd_put_device(device
);
416 if (!device
->discipline
->copy_pair_swap
) {
417 dasd_put_device(device
);
420 rc
= device
->discipline
->copy_pair_swap(device
, data
.primary
,
422 dasd_put_device(device
);
427 #ifdef CONFIG_DASD_PROFILE
429 * Reset device profile information
431 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
433 dasd_profile_reset(&block
->profile
);
438 * Return device profile information
440 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
442 struct dasd_profile_info_t
*data
;
445 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
449 spin_lock_bh(&block
->profile
.lock
);
450 if (block
->profile
.data
) {
451 data
->dasd_io_reqs
= block
->profile
.data
->dasd_io_reqs
;
452 data
->dasd_io_sects
= block
->profile
.data
->dasd_io_sects
;
453 memcpy(data
->dasd_io_secs
, block
->profile
.data
->dasd_io_secs
,
454 sizeof(data
->dasd_io_secs
));
455 memcpy(data
->dasd_io_times
, block
->profile
.data
->dasd_io_times
,
456 sizeof(data
->dasd_io_times
));
457 memcpy(data
->dasd_io_timps
, block
->profile
.data
->dasd_io_timps
,
458 sizeof(data
->dasd_io_timps
));
459 memcpy(data
->dasd_io_time1
, block
->profile
.data
->dasd_io_time1
,
460 sizeof(data
->dasd_io_time1
));
461 memcpy(data
->dasd_io_time2
, block
->profile
.data
->dasd_io_time2
,
462 sizeof(data
->dasd_io_time2
));
463 memcpy(data
->dasd_io_time2ps
,
464 block
->profile
.data
->dasd_io_time2ps
,
465 sizeof(data
->dasd_io_time2ps
));
466 memcpy(data
->dasd_io_time3
, block
->profile
.data
->dasd_io_time3
,
467 sizeof(data
->dasd_io_time3
));
468 memcpy(data
->dasd_io_nr_req
,
469 block
->profile
.data
->dasd_io_nr_req
,
470 sizeof(data
->dasd_io_nr_req
));
471 spin_unlock_bh(&block
->profile
.lock
);
473 spin_unlock_bh(&block
->profile
.lock
);
477 if (copy_to_user(argp
, data
, sizeof(*data
)))
484 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
489 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
496 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
498 static int __dasd_ioctl_information(struct dasd_block
*block
,
499 struct dasd_information2_t
*dasd_info
)
501 struct subchannel_id sch_id
;
502 struct ccw_dev_id dev_id
;
503 struct dasd_device
*base
;
504 struct ccw_device
*cdev
;
510 if (!base
->discipline
|| !base
->discipline
->fill_info
)
513 rc
= base
->discipline
->fill_info(base
, dasd_info
);
518 ccw_device_get_id(cdev
, &dev_id
);
519 ccw_device_get_schid(cdev
, &sch_id
);
521 dasd_info
->devno
= dev_id
.devno
;
522 dasd_info
->schid
= sch_id
.sch_no
;
523 dasd_info
->cu_type
= cdev
->id
.cu_type
;
524 dasd_info
->cu_model
= cdev
->id
.cu_model
;
525 dasd_info
->dev_type
= cdev
->id
.dev_type
;
526 dasd_info
->dev_model
= cdev
->id
.dev_model
;
527 dasd_info
->status
= base
->state
;
529 * The open_count is increased for every opener, that includes
530 * the blkdev_get in dasd_scan_partitions.
531 * This must be hidden from user-space.
533 dasd_info
->open_count
= atomic_read(&block
->open_count
);
534 if (!block
->bdev_file
)
535 dasd_info
->open_count
++;
538 * check if device is really formatted
539 * LDL / CDL was returned by 'fill_info'
541 if ((base
->state
< DASD_STATE_READY
) ||
542 (dasd_check_blocksize(block
->bp_block
)))
543 dasd_info
->format
= DASD_FORMAT_NONE
;
545 dasd_info
->features
|=
546 ((base
->features
& DASD_FEATURE_READONLY
) != 0);
548 memcpy(dasd_info
->type
, base
->discipline
->name
, 4);
550 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
551 list_for_each(l
, &base
->ccw_queue
)
552 dasd_info
->chanq_len
++;
553 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
), flags
);
557 static int dasd_ioctl_information(struct dasd_block
*block
, void __user
*argp
,
560 struct dasd_information2_t
*dasd_info
;
563 dasd_info
= kzalloc(sizeof(*dasd_info
), GFP_KERNEL
);
567 error
= __dasd_ioctl_information(block
, dasd_info
);
568 if (!error
&& copy_to_user(argp
, dasd_info
, copy_size
))
577 int dasd_set_read_only(struct block_device
*bdev
, bool ro
)
579 struct dasd_device
*base
;
582 /* do not manipulate hardware state for partitions */
583 if (bdev_is_partition(bdev
))
586 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
589 if (!ro
&& test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
))
592 rc
= dasd_set_feature(base
->cdev
, DASD_FEATURE_READONLY
, ro
);
593 dasd_put_device(base
);
597 static int dasd_ioctl_readall_cmb(struct dasd_block
*block
, unsigned int cmd
,
598 struct cmbdata __user
*argp
)
600 size_t size
= _IOC_SIZE(cmd
);
604 ret
= cmf_readall(block
->base
->cdev
, &data
);
605 if (!ret
&& copy_to_user(argp
, &data
, min(size
, sizeof(*argp
))))
610 int dasd_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
611 unsigned int cmd
, unsigned long arg
)
613 struct dasd_block
*block
;
614 struct dasd_device
*base
;
618 if (is_compat_task())
619 argp
= compat_ptr(arg
);
621 argp
= (void __user
*)arg
;
623 if ((_IOC_DIR(cmd
) != _IOC_NONE
) && !arg
)
626 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
633 rc
= dasd_ioctl_disable(bdev
);
636 rc
= dasd_ioctl_enable(bdev
);
639 rc
= dasd_ioctl_quiesce(block
);
642 rc
= dasd_ioctl_resume(block
);
645 rc
= dasd_ioctl_abortio(block
);
648 rc
= dasd_ioctl_allowio(block
);
651 rc
= dasd_ioctl_format(bdev
, argp
);
653 case BIODASDCHECKFMT
:
654 rc
= dasd_ioctl_check_format(bdev
, argp
);
657 rc
= dasd_ioctl_information(block
, argp
,
658 sizeof(struct dasd_information_t
));
661 rc
= dasd_ioctl_information(block
, argp
,
662 sizeof(struct dasd_information2_t
));
665 rc
= dasd_ioctl_read_profile(block
, argp
);
668 rc
= dasd_ioctl_reset_profile(block
);
671 rc
= dasd_ioctl_api_version(argp
);
673 case BIODASDCMFENABLE
:
674 rc
= enable_cmf(base
->cdev
);
676 case BIODASDCMFDISABLE
:
677 rc
= disable_cmf(base
->cdev
);
679 case BIODASDREADALLCMB
:
680 rc
= dasd_ioctl_readall_cmb(block
, cmd
, argp
);
683 rc
= dasd_ioctl_release_space(bdev
, argp
);
685 case BIODASDCOPYPAIRSWAP
:
686 rc
= dasd_ioctl_copy_pair_swap(bdev
, argp
);
689 /* if the discipline has an ioctl method try it. */
691 if (base
->discipline
->ioctl
)
692 rc
= base
->discipline
->ioctl(block
, cmd
, argp
);
694 dasd_put_device(base
);
700 * dasd_biodasdinfo() - fill out the dasd information structure
701 * @disk: [in] pointer to gendisk structure that references a DASD
702 * @info: [out] pointer to the dasd_information2_t structure
704 * Provide access to DASD specific information.
705 * The gendisk structure is checked if it belongs to the DASD driver by
706 * comparing the gendisk->fops pointer.
707 * If it does not belong to the DASD driver -EINVAL is returned.
708 * Otherwise the provided dasd_information2_t structure is filled out.
711 * %0 on success and a negative error value on failure.
713 int dasd_biodasdinfo(struct gendisk
*disk
, struct dasd_information2_t
*info
)
715 struct dasd_device
*base
;
718 if (disk
->fops
!= &dasd_device_operations
)
721 base
= dasd_device_from_gendisk(disk
);
724 error
= __dasd_ioctl_information(base
->block
, info
);
725 dasd_put_device(base
);
728 /* export that symbol_get in partition detection is possible */
729 EXPORT_SYMBOL_GPL(dasd_biodasdinfo
);