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 #define KMSG_COMPONENT "dasd"
15 #include <linux/interrupt.h>
16 #include <linux/compat.h>
17 #include <linux/major.h>
19 #include <linux/blkpg.h>
20 #include <linux/slab.h>
21 #include <asm/ccwdev.h>
22 #include <asm/schid.h>
24 #include <linux/uaccess.h>
25 #include <linux/dasd_mod.h>
28 #define PRINTK_HEADER "dasd_ioctl:"
34 dasd_ioctl_api_version(void __user
*argp
)
36 int ver
= DASD_API_VERSION
;
37 return put_user(ver
, (int __user
*)argp
);
42 * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
45 dasd_ioctl_enable(struct block_device
*bdev
)
47 struct dasd_device
*base
;
49 if (!capable(CAP_SYS_ADMIN
))
52 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
56 dasd_enable_device(base
);
57 dasd_put_device(base
);
63 * Used by dasdfmt. Disable I/O operations but allow ioctls.
66 dasd_ioctl_disable(struct block_device
*bdev
)
68 struct dasd_device
*base
;
70 if (!capable(CAP_SYS_ADMIN
))
73 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
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.
84 dasd_set_target_state(base
, DASD_STATE_BASIC
);
86 * Set i_size to zero, since read, write, etc. check against this
89 set_capacity(bdev
->bd_disk
, 0);
90 dasd_put_device(base
);
97 static int dasd_ioctl_quiesce(struct dasd_block
*block
)
100 struct dasd_device
*base
;
103 if (!capable (CAP_SYS_ADMIN
))
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
);
118 static int dasd_ioctl_resume(struct dasd_block
*block
)
121 struct dasd_device
*base
;
124 if (!capable (CAP_SYS_ADMIN
))
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
);
133 dasd_schedule_block_bh(block
);
138 * Abort all failfast I/O on a device.
140 static int dasd_ioctl_abortio(struct dasd_block
*block
)
143 struct dasd_device
*base
;
144 struct dasd_ccw_req
*cqr
, *n
;
147 if (!capable(CAP_SYS_ADMIN
))
150 if (test_and_set_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
152 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag set");
154 spin_lock_irqsave(&block
->request_queue_lock
, flags
);
155 spin_lock(&block
->queue_lock
);
156 list_for_each_entry_safe(cqr
, n
, &block
->ccw_queue
, blocklist
) {
157 if (test_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
) &&
158 cqr
->callback_data
&&
159 cqr
->callback_data
!= DASD_SLEEPON_START_TAG
&&
160 cqr
->callback_data
!= DASD_SLEEPON_END_TAG
) {
161 spin_unlock(&block
->queue_lock
);
162 blk_abort_request(cqr
->callback_data
);
163 spin_lock(&block
->queue_lock
);
166 spin_unlock(&block
->queue_lock
);
167 spin_unlock_irqrestore(&block
->request_queue_lock
, flags
);
169 dasd_schedule_block_bh(block
);
174 * Allow I/O on a device
176 static int dasd_ioctl_allowio(struct dasd_block
*block
)
178 struct dasd_device
*base
;
181 if (!capable(CAP_SYS_ADMIN
))
184 if (test_and_clear_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
185 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag unset");
191 * performs formatting of _device_ according to _fdata_
192 * Note: The discipline's format_function is assumed to deliver formatting
193 * commands to format multiple units of the device. In terms of the ECKD
194 * devices this means CCWs are generated to format multiple tracks.
197 dasd_format(struct dasd_block
*block
, struct format_data_t
*fdata
)
199 struct dasd_device
*base
;
203 if (base
->discipline
->format_device
== NULL
)
206 if (base
->state
!= DASD_STATE_BASIC
) {
207 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
208 dev_name(&base
->cdev
->dev
));
212 DBF_DEV_EVENT(DBF_NOTICE
, base
,
213 "formatting units %u to %u (%u B blocks) flags %u",
215 fdata
->stop_unit
, fdata
->blksize
, fdata
->intensity
);
217 /* Since dasdfmt keeps the device open after it was disabled,
218 * there still exists an inode for this device.
219 * We must update i_blkbits, otherwise we might get errors when
220 * enabling the device later.
222 if (fdata
->start_unit
== 0) {
223 block
->gdp
->part0
->bd_inode
->i_blkbits
=
224 blksize_bits(fdata
->blksize
);
227 rc
= base
->discipline
->format_device(base
, fdata
, 1);
229 rc
= base
->discipline
->format_device(base
, fdata
, 0);
234 static int dasd_check_format(struct dasd_block
*block
,
235 struct format_check_t
*cdata
)
237 struct dasd_device
*base
;
241 if (!base
->discipline
->check_device_format
)
244 rc
= base
->discipline
->check_device_format(base
, cdata
, 1);
246 rc
= base
->discipline
->check_device_format(base
, cdata
, 0);
255 dasd_ioctl_format(struct block_device
*bdev
, void __user
*argp
)
257 struct dasd_device
*base
;
258 struct format_data_t fdata
;
261 if (!capable(CAP_SYS_ADMIN
))
265 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
268 if (base
->features
& DASD_FEATURE_READONLY
||
269 test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
270 dasd_put_device(base
);
273 if (copy_from_user(&fdata
, argp
, sizeof(struct format_data_t
))) {
274 dasd_put_device(base
);
277 if (bdev_is_partition(bdev
)) {
278 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
279 dev_name(&base
->cdev
->dev
));
280 dasd_put_device(base
);
283 rc
= dasd_format(base
->block
, &fdata
);
284 dasd_put_device(base
);
290 * Check device format
292 static int dasd_ioctl_check_format(struct block_device
*bdev
, void __user
*argp
)
294 struct format_check_t cdata
;
295 struct dasd_device
*base
;
301 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
304 if (bdev_is_partition(bdev
)) {
305 pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
306 dev_name(&base
->cdev
->dev
));
311 if (copy_from_user(&cdata
, argp
, sizeof(cdata
))) {
316 rc
= dasd_check_format(base
->block
, &cdata
);
320 if (copy_to_user(argp
, &cdata
, sizeof(cdata
)))
324 dasd_put_device(base
);
329 static int dasd_release_space(struct dasd_device
*device
,
330 struct format_data_t
*rdata
)
332 if (!device
->discipline
->is_ese
&& !device
->discipline
->is_ese(device
))
334 if (!device
->discipline
->release_space
)
337 return device
->discipline
->release_space(device
, rdata
);
341 * Release allocated space
343 static int dasd_ioctl_release_space(struct block_device
*bdev
, void __user
*argp
)
345 struct format_data_t rdata
;
346 struct dasd_device
*base
;
349 if (!capable(CAP_SYS_ADMIN
))
354 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
357 if (base
->features
& DASD_FEATURE_READONLY
||
358 test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
362 if (bdev_is_partition(bdev
)) {
363 pr_warn("%s: The specified DASD is a partition and tracks cannot be released\n",
364 dev_name(&base
->cdev
->dev
));
369 if (copy_from_user(&rdata
, argp
, sizeof(rdata
))) {
374 rc
= dasd_release_space(base
, &rdata
);
377 dasd_put_device(base
);
382 #ifdef CONFIG_DASD_PROFILE
384 * Reset device profile information
386 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
388 dasd_profile_reset(&block
->profile
);
393 * Return device profile information
395 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
397 struct dasd_profile_info_t
*data
;
400 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
404 spin_lock_bh(&block
->profile
.lock
);
405 if (block
->profile
.data
) {
406 data
->dasd_io_reqs
= block
->profile
.data
->dasd_io_reqs
;
407 data
->dasd_io_sects
= block
->profile
.data
->dasd_io_sects
;
408 memcpy(data
->dasd_io_secs
, block
->profile
.data
->dasd_io_secs
,
409 sizeof(data
->dasd_io_secs
));
410 memcpy(data
->dasd_io_times
, block
->profile
.data
->dasd_io_times
,
411 sizeof(data
->dasd_io_times
));
412 memcpy(data
->dasd_io_timps
, block
->profile
.data
->dasd_io_timps
,
413 sizeof(data
->dasd_io_timps
));
414 memcpy(data
->dasd_io_time1
, block
->profile
.data
->dasd_io_time1
,
415 sizeof(data
->dasd_io_time1
));
416 memcpy(data
->dasd_io_time2
, block
->profile
.data
->dasd_io_time2
,
417 sizeof(data
->dasd_io_time2
));
418 memcpy(data
->dasd_io_time2ps
,
419 block
->profile
.data
->dasd_io_time2ps
,
420 sizeof(data
->dasd_io_time2ps
));
421 memcpy(data
->dasd_io_time3
, block
->profile
.data
->dasd_io_time3
,
422 sizeof(data
->dasd_io_time3
));
423 memcpy(data
->dasd_io_nr_req
,
424 block
->profile
.data
->dasd_io_nr_req
,
425 sizeof(data
->dasd_io_nr_req
));
426 spin_unlock_bh(&block
->profile
.lock
);
428 spin_unlock_bh(&block
->profile
.lock
);
432 if (copy_to_user(argp
, data
, sizeof(*data
)))
439 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
444 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
451 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
453 static int __dasd_ioctl_information(struct dasd_block
*block
,
454 struct dasd_information2_t
*dasd_info
)
456 struct subchannel_id sch_id
;
457 struct ccw_dev_id dev_id
;
458 struct dasd_device
*base
;
459 struct ccw_device
*cdev
;
465 if (!base
->discipline
|| !base
->discipline
->fill_info
)
468 rc
= base
->discipline
->fill_info(base
, dasd_info
);
473 ccw_device_get_id(cdev
, &dev_id
);
474 ccw_device_get_schid(cdev
, &sch_id
);
476 dasd_info
->devno
= dev_id
.devno
;
477 dasd_info
->schid
= sch_id
.sch_no
;
478 dasd_info
->cu_type
= cdev
->id
.cu_type
;
479 dasd_info
->cu_model
= cdev
->id
.cu_model
;
480 dasd_info
->dev_type
= cdev
->id
.dev_type
;
481 dasd_info
->dev_model
= cdev
->id
.dev_model
;
482 dasd_info
->status
= base
->state
;
484 * The open_count is increased for every opener, that includes
485 * the blkdev_get in dasd_scan_partitions.
486 * This must be hidden from user-space.
488 dasd_info
->open_count
= atomic_read(&block
->open_count
);
490 dasd_info
->open_count
++;
493 * check if device is really formatted
494 * LDL / CDL was returned by 'fill_info'
496 if ((base
->state
< DASD_STATE_READY
) ||
497 (dasd_check_blocksize(block
->bp_block
)))
498 dasd_info
->format
= DASD_FORMAT_NONE
;
500 dasd_info
->features
|=
501 ((base
->features
& DASD_FEATURE_READONLY
) != 0);
503 memcpy(dasd_info
->type
, base
->discipline
->name
, 4);
505 spin_lock_irqsave(&block
->queue_lock
, flags
);
506 list_for_each(l
, &base
->ccw_queue
)
507 dasd_info
->chanq_len
++;
508 spin_unlock_irqrestore(&block
->queue_lock
, flags
);
512 static int dasd_ioctl_information(struct dasd_block
*block
, void __user
*argp
,
515 struct dasd_information2_t
*dasd_info
;
518 dasd_info
= kzalloc(sizeof(*dasd_info
), GFP_KERNEL
);
522 error
= __dasd_ioctl_information(block
, dasd_info
);
523 if (!error
&& copy_to_user(argp
, dasd_info
, copy_size
))
532 int dasd_set_read_only(struct block_device
*bdev
, bool ro
)
534 struct dasd_device
*base
;
537 /* do not manipulate hardware state for partitions */
538 if (bdev_is_partition(bdev
))
541 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
544 if (!ro
&& test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
))
547 rc
= dasd_set_feature(base
->cdev
, DASD_FEATURE_READONLY
, ro
);
548 dasd_put_device(base
);
552 static int dasd_ioctl_readall_cmb(struct dasd_block
*block
, unsigned int cmd
,
553 struct cmbdata __user
*argp
)
555 size_t size
= _IOC_SIZE(cmd
);
559 ret
= cmf_readall(block
->base
->cdev
, &data
);
560 if (!ret
&& copy_to_user(argp
, &data
, min(size
, sizeof(*argp
))))
565 int dasd_ioctl(struct block_device
*bdev
, fmode_t mode
,
566 unsigned int cmd
, unsigned long arg
)
568 struct dasd_block
*block
;
569 struct dasd_device
*base
;
573 if (is_compat_task())
574 argp
= compat_ptr(arg
);
576 argp
= (void __user
*)arg
;
578 if ((_IOC_DIR(cmd
) != _IOC_NONE
) && !arg
) {
579 PRINT_DEBUG("empty data ptr");
583 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
590 rc
= dasd_ioctl_disable(bdev
);
593 rc
= dasd_ioctl_enable(bdev
);
596 rc
= dasd_ioctl_quiesce(block
);
599 rc
= dasd_ioctl_resume(block
);
602 rc
= dasd_ioctl_abortio(block
);
605 rc
= dasd_ioctl_allowio(block
);
608 rc
= dasd_ioctl_format(bdev
, argp
);
610 case BIODASDCHECKFMT
:
611 rc
= dasd_ioctl_check_format(bdev
, argp
);
614 rc
= dasd_ioctl_information(block
, argp
,
615 sizeof(struct dasd_information_t
));
618 rc
= dasd_ioctl_information(block
, argp
,
619 sizeof(struct dasd_information2_t
));
622 rc
= dasd_ioctl_read_profile(block
, argp
);
625 rc
= dasd_ioctl_reset_profile(block
);
628 rc
= dasd_ioctl_api_version(argp
);
630 case BIODASDCMFENABLE
:
631 rc
= enable_cmf(base
->cdev
);
633 case BIODASDCMFDISABLE
:
634 rc
= disable_cmf(base
->cdev
);
636 case BIODASDREADALLCMB
:
637 rc
= dasd_ioctl_readall_cmb(block
, cmd
, argp
);
640 rc
= dasd_ioctl_release_space(bdev
, argp
);
643 /* if the discipline has an ioctl method try it. */
645 if (base
->discipline
->ioctl
)
646 rc
= base
->discipline
->ioctl(block
, cmd
, argp
);
648 dasd_put_device(base
);
654 * dasd_biodasdinfo() - fill out the dasd information structure
655 * @disk [in]: pointer to gendisk structure that references a DASD
656 * @info [out]: pointer to the dasd_information2_t structure
658 * Provide access to DASD specific information.
659 * The gendisk structure is checked if it belongs to the DASD driver by
660 * comparing the gendisk->fops pointer.
661 * If it does not belong to the DASD driver -EINVAL is returned.
662 * Otherwise the provided dasd_information2_t structure is filled out.
665 * %0 on success and a negative error value on failure.
667 int dasd_biodasdinfo(struct gendisk
*disk
, struct dasd_information2_t
*info
)
669 struct dasd_device
*base
;
672 if (disk
->fops
!= &dasd_device_operations
)
675 base
= dasd_device_from_gendisk(disk
);
678 error
= __dasd_ioctl_information(base
->block
, info
);
679 dasd_put_device(base
);
682 /* export that symbol_get in partition detection is possible */
683 EXPORT_SYMBOL_GPL(dasd_biodasdinfo
);