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/compat.h>
22 #include <asm/ccwdev.h>
23 #include <asm/schid.h>
25 #include <linux/uaccess.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 /* Formatting the dasd device can change the capacity. */
58 mutex_lock(&bdev
->bd_mutex
);
59 i_size_write(bdev
->bd_inode
,
60 (loff_t
)get_capacity(base
->block
->gdp
) << 9);
61 mutex_unlock(&bdev
->bd_mutex
);
62 dasd_put_device(base
);
68 * Used by dasdfmt. Disable I/O operations but allow ioctls.
71 dasd_ioctl_disable(struct block_device
*bdev
)
73 struct dasd_device
*base
;
75 if (!capable(CAP_SYS_ADMIN
))
78 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
82 * Man this is sick. We don't do a real disable but only downgrade
83 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
84 * BIODASDDISABLE to disable accesses to the device via the block
85 * device layer but it still wants to do i/o on the device by
86 * using the BIODASDFMT ioctl. Therefore the correct state for the
87 * device is DASD_STATE_BASIC that allows to do basic i/o.
89 dasd_set_target_state(base
, DASD_STATE_BASIC
);
91 * Set i_size to zero, since read, write, etc. check against this
94 mutex_lock(&bdev
->bd_mutex
);
95 i_size_write(bdev
->bd_inode
, 0);
96 mutex_unlock(&bdev
->bd_mutex
);
97 dasd_put_device(base
);
104 static int dasd_ioctl_quiesce(struct dasd_block
*block
)
107 struct dasd_device
*base
;
110 if (!capable (CAP_SYS_ADMIN
))
113 pr_info("%s: The DASD has been put in the quiesce "
114 "state\n", dev_name(&base
->cdev
->dev
));
115 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
116 dasd_device_set_stop_bits(base
, DASD_STOPPED_QUIESCE
);
117 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
), flags
);
125 static int dasd_ioctl_resume(struct dasd_block
*block
)
128 struct dasd_device
*base
;
131 if (!capable (CAP_SYS_ADMIN
))
134 pr_info("%s: I/O operations have been resumed "
135 "on the DASD\n", dev_name(&base
->cdev
->dev
));
136 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
137 dasd_device_remove_stop_bits(base
, DASD_STOPPED_QUIESCE
);
138 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
), flags
);
140 dasd_schedule_block_bh(block
);
145 * Abort all failfast I/O on a device.
147 static int dasd_ioctl_abortio(struct dasd_block
*block
)
150 struct dasd_device
*base
;
151 struct dasd_ccw_req
*cqr
, *n
;
154 if (!capable(CAP_SYS_ADMIN
))
157 if (test_and_set_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
159 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag set");
161 spin_lock_irqsave(&block
->request_queue_lock
, flags
);
162 spin_lock(&block
->queue_lock
);
163 list_for_each_entry_safe(cqr
, n
, &block
->ccw_queue
, blocklist
) {
164 if (test_bit(DASD_CQR_FLAGS_FAILFAST
, &cqr
->flags
) &&
165 cqr
->callback_data
&&
166 cqr
->callback_data
!= DASD_SLEEPON_START_TAG
&&
167 cqr
->callback_data
!= DASD_SLEEPON_END_TAG
) {
168 spin_unlock(&block
->queue_lock
);
169 blk_abort_request(cqr
->callback_data
);
170 spin_lock(&block
->queue_lock
);
173 spin_unlock(&block
->queue_lock
);
174 spin_unlock_irqrestore(&block
->request_queue_lock
, flags
);
176 dasd_schedule_block_bh(block
);
181 * Allow I/O on a device
183 static int dasd_ioctl_allowio(struct dasd_block
*block
)
185 struct dasd_device
*base
;
188 if (!capable(CAP_SYS_ADMIN
))
191 if (test_and_clear_bit(DASD_FLAG_ABORTALL
, &base
->flags
))
192 DBF_DEV_EVENT(DBF_NOTICE
, base
, "%s", "abortall flag unset");
198 * performs formatting of _device_ according to _fdata_
199 * Note: The discipline's format_function is assumed to deliver formatting
200 * commands to format multiple units of the device. In terms of the ECKD
201 * devices this means CCWs are generated to format multiple tracks.
204 dasd_format(struct dasd_block
*block
, struct format_data_t
*fdata
)
206 struct dasd_device
*base
;
210 if (base
->discipline
->format_device
== NULL
)
213 if (base
->state
!= DASD_STATE_BASIC
) {
214 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
215 dev_name(&base
->cdev
->dev
));
219 DBF_DEV_EVENT(DBF_NOTICE
, base
,
220 "formatting units %u to %u (%u B blocks) flags %u",
222 fdata
->stop_unit
, fdata
->blksize
, fdata
->intensity
);
224 /* Since dasdfmt keeps the device open after it was disabled,
225 * there still exists an inode for this device.
226 * We must update i_blkbits, otherwise we might get errors when
227 * enabling the device later.
229 if (fdata
->start_unit
== 0) {
230 struct block_device
*bdev
= bdget_disk(block
->gdp
, 0);
231 bdev
->bd_inode
->i_blkbits
= blksize_bits(fdata
->blksize
);
235 rc
= base
->discipline
->format_device(base
, fdata
, 1);
237 rc
= base
->discipline
->format_device(base
, fdata
, 0);
242 static int dasd_check_format(struct dasd_block
*block
,
243 struct format_check_t
*cdata
)
245 struct dasd_device
*base
;
249 if (!base
->discipline
->check_device_format
)
252 rc
= base
->discipline
->check_device_format(base
, cdata
, 1);
254 rc
= base
->discipline
->check_device_format(base
, cdata
, 0);
263 dasd_ioctl_format(struct block_device
*bdev
, void __user
*argp
)
265 struct dasd_device
*base
;
266 struct format_data_t fdata
;
269 if (!capable(CAP_SYS_ADMIN
))
273 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
276 if (base
->features
& DASD_FEATURE_READONLY
||
277 test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
278 dasd_put_device(base
);
281 if (copy_from_user(&fdata
, argp
, sizeof(struct format_data_t
))) {
282 dasd_put_device(base
);
285 if (bdev
!= bdev
->bd_contains
) {
286 pr_warn("%s: The specified DASD is a partition and cannot be formatted\n",
287 dev_name(&base
->cdev
->dev
));
288 dasd_put_device(base
);
291 rc
= dasd_format(base
->block
, &fdata
);
292 dasd_put_device(base
);
298 * Check device format
300 static int dasd_ioctl_check_format(struct block_device
*bdev
, void __user
*argp
)
302 struct format_check_t cdata
;
303 struct dasd_device
*base
;
309 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
312 if (bdev
!= bdev
->bd_contains
) {
313 pr_warn("%s: The specified DASD is a partition and cannot be checked\n",
314 dev_name(&base
->cdev
->dev
));
319 if (copy_from_user(&cdata
, argp
, sizeof(cdata
))) {
324 rc
= dasd_check_format(base
->block
, &cdata
);
328 if (copy_to_user(argp
, &cdata
, sizeof(cdata
)))
332 dasd_put_device(base
);
337 #ifdef CONFIG_DASD_PROFILE
339 * Reset device profile information
341 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
343 dasd_profile_reset(&block
->profile
);
348 * Return device profile information
350 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
352 struct dasd_profile_info_t
*data
;
355 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
359 spin_lock_bh(&block
->profile
.lock
);
360 if (block
->profile
.data
) {
361 data
->dasd_io_reqs
= block
->profile
.data
->dasd_io_reqs
;
362 data
->dasd_io_sects
= block
->profile
.data
->dasd_io_sects
;
363 memcpy(data
->dasd_io_secs
, block
->profile
.data
->dasd_io_secs
,
364 sizeof(data
->dasd_io_secs
));
365 memcpy(data
->dasd_io_times
, block
->profile
.data
->dasd_io_times
,
366 sizeof(data
->dasd_io_times
));
367 memcpy(data
->dasd_io_timps
, block
->profile
.data
->dasd_io_timps
,
368 sizeof(data
->dasd_io_timps
));
369 memcpy(data
->dasd_io_time1
, block
->profile
.data
->dasd_io_time1
,
370 sizeof(data
->dasd_io_time1
));
371 memcpy(data
->dasd_io_time2
, block
->profile
.data
->dasd_io_time2
,
372 sizeof(data
->dasd_io_time2
));
373 memcpy(data
->dasd_io_time2ps
,
374 block
->profile
.data
->dasd_io_time2ps
,
375 sizeof(data
->dasd_io_time2ps
));
376 memcpy(data
->dasd_io_time3
, block
->profile
.data
->dasd_io_time3
,
377 sizeof(data
->dasd_io_time3
));
378 memcpy(data
->dasd_io_nr_req
,
379 block
->profile
.data
->dasd_io_nr_req
,
380 sizeof(data
->dasd_io_nr_req
));
381 spin_unlock_bh(&block
->profile
.lock
);
383 spin_unlock_bh(&block
->profile
.lock
);
387 if (copy_to_user(argp
, data
, sizeof(*data
)))
394 static int dasd_ioctl_reset_profile(struct dasd_block
*block
)
399 static int dasd_ioctl_read_profile(struct dasd_block
*block
, void __user
*argp
)
406 * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
408 static int dasd_ioctl_information(struct dasd_block
*block
,
409 unsigned int cmd
, void __user
*argp
)
411 struct dasd_information2_t
*dasd_info
;
412 struct subchannel_id sch_id
;
413 struct ccw_dev_id dev_id
;
414 struct dasd_device
*base
;
415 struct ccw_device
*cdev
;
420 if (!base
->discipline
|| !base
->discipline
->fill_info
)
423 dasd_info
= kzalloc(sizeof(struct dasd_information2_t
), GFP_KERNEL
);
424 if (dasd_info
== NULL
)
427 rc
= base
->discipline
->fill_info(base
, dasd_info
);
434 ccw_device_get_id(cdev
, &dev_id
);
435 ccw_device_get_schid(cdev
, &sch_id
);
437 dasd_info
->devno
= dev_id
.devno
;
438 dasd_info
->schid
= sch_id
.sch_no
;
439 dasd_info
->cu_type
= cdev
->id
.cu_type
;
440 dasd_info
->cu_model
= cdev
->id
.cu_model
;
441 dasd_info
->dev_type
= cdev
->id
.dev_type
;
442 dasd_info
->dev_model
= cdev
->id
.dev_model
;
443 dasd_info
->status
= base
->state
;
445 * The open_count is increased for every opener, that includes
446 * the blkdev_get in dasd_scan_partitions.
447 * This must be hidden from user-space.
449 dasd_info
->open_count
= atomic_read(&block
->open_count
);
451 dasd_info
->open_count
++;
454 * check if device is really formatted
455 * LDL / CDL was returned by 'fill_info'
457 if ((base
->state
< DASD_STATE_READY
) ||
458 (dasd_check_blocksize(block
->bp_block
)))
459 dasd_info
->format
= DASD_FORMAT_NONE
;
461 dasd_info
->features
|=
462 ((base
->features
& DASD_FEATURE_READONLY
) != 0);
464 memcpy(dasd_info
->type
, base
->discipline
->name
, 4);
466 if (block
->request_queue
->request_fn
) {
468 #ifdef DASD_EXTENDED_PROFILING
471 spin_lock_irqsave(&block
->lock
, flags
);
472 list_for_each(l
, &block
->request_queue
->queue_head
)
473 dasd_info
->req_queue_len
++;
474 spin_unlock_irqrestore(&block
->lock
, flags
);
476 #endif /* DASD_EXTENDED_PROFILING */
477 spin_lock_irqsave(get_ccwdev_lock(base
->cdev
), flags
);
478 list_for_each(l
, &base
->ccw_queue
)
479 dasd_info
->chanq_len
++;
480 spin_unlock_irqrestore(get_ccwdev_lock(base
->cdev
),
485 if (copy_to_user(argp
, dasd_info
,
486 ((cmd
== (unsigned int) BIODASDINFO2
) ?
487 sizeof(struct dasd_information2_t
) :
488 sizeof(struct dasd_information_t
))))
498 dasd_ioctl_set_ro(struct block_device
*bdev
, void __user
*argp
)
500 struct dasd_device
*base
;
503 if (!capable(CAP_SYS_ADMIN
))
505 if (bdev
!= bdev
->bd_contains
)
506 // ro setting is not allowed for partitions
508 if (get_user(intval
, (int __user
*)argp
))
510 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
513 if (!intval
&& test_bit(DASD_FLAG_DEVICE_RO
, &base
->flags
)) {
514 dasd_put_device(base
);
517 set_disk_ro(bdev
->bd_disk
, intval
);
518 rc
= dasd_set_feature(base
->cdev
, DASD_FEATURE_READONLY
, intval
);
519 dasd_put_device(base
);
523 static int dasd_ioctl_readall_cmb(struct dasd_block
*block
, unsigned int cmd
,
524 struct cmbdata __user
*argp
)
526 size_t size
= _IOC_SIZE(cmd
);
530 ret
= cmf_readall(block
->base
->cdev
, &data
);
531 if (!ret
&& copy_to_user(argp
, &data
, min(size
, sizeof(*argp
))))
536 int dasd_ioctl(struct block_device
*bdev
, fmode_t mode
,
537 unsigned int cmd
, unsigned long arg
)
539 struct dasd_block
*block
;
540 struct dasd_device
*base
;
544 if (is_compat_task())
545 argp
= compat_ptr(arg
);
547 argp
= (void __user
*)arg
;
549 if ((_IOC_DIR(cmd
) != _IOC_NONE
) && !arg
) {
550 PRINT_DEBUG("empty data ptr");
554 base
= dasd_device_from_gendisk(bdev
->bd_disk
);
561 rc
= dasd_ioctl_disable(bdev
);
564 rc
= dasd_ioctl_enable(bdev
);
567 rc
= dasd_ioctl_quiesce(block
);
570 rc
= dasd_ioctl_resume(block
);
573 rc
= dasd_ioctl_abortio(block
);
576 rc
= dasd_ioctl_allowio(block
);
579 rc
= dasd_ioctl_format(bdev
, argp
);
581 case BIODASDCHECKFMT
:
582 rc
= dasd_ioctl_check_format(bdev
, argp
);
585 rc
= dasd_ioctl_information(block
, cmd
, argp
);
588 rc
= dasd_ioctl_information(block
, cmd
, argp
);
591 rc
= dasd_ioctl_read_profile(block
, argp
);
594 rc
= dasd_ioctl_reset_profile(block
);
597 rc
= dasd_ioctl_set_ro(bdev
, argp
);
600 rc
= dasd_ioctl_api_version(argp
);
602 case BIODASDCMFENABLE
:
603 rc
= enable_cmf(base
->cdev
);
605 case BIODASDCMFDISABLE
:
606 rc
= disable_cmf(base
->cdev
);
608 case BIODASDREADALLCMB
:
609 rc
= dasd_ioctl_readall_cmb(block
, cmd
, argp
);
612 /* if the discipline has an ioctl method try it. */
614 if (base
->discipline
->ioctl
)
615 rc
= base
->discipline
->ioctl(block
, cmd
, argp
);
617 dasd_put_device(base
);