2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-10-11 04:39:33 +08:00
|
|
|
/*
|
|
|
|
* IDE ioctls handling.
|
|
|
|
*/
|
|
|
|
|
2011-07-18 03:33:58 +08:00
|
|
|
#include <linux/export.h>
|
2008-10-11 04:39:33 +08:00
|
|
|
#include <linux/hdreg.h>
|
|
|
|
#include <linux/ide.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
static const struct ide_ioctl_devset ide_ioctl_settings[] = {
|
2008-10-11 04:39:40 +08:00
|
|
|
{ HDIO_GET_32BIT, HDIO_SET_32BIT, &ide_devset_io_32bit },
|
|
|
|
{ HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, &ide_devset_keepsettings },
|
|
|
|
{ HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, &ide_devset_unmaskirq },
|
|
|
|
{ HDIO_GET_DMA, HDIO_SET_DMA, &ide_devset_using_dma },
|
|
|
|
{ -1, HDIO_SET_PIO_MODE, &ide_devset_pio_mode },
|
2008-10-11 04:39:33 +08:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int ide_setting_ioctl(ide_drive_t *drive, struct block_device *bdev,
|
|
|
|
unsigned int cmd, unsigned long arg,
|
|
|
|
const struct ide_ioctl_devset *s)
|
|
|
|
{
|
2008-10-11 04:39:40 +08:00
|
|
|
const struct ide_devset *ds;
|
2008-10-11 04:39:33 +08:00
|
|
|
int err = -EOPNOTSUPP;
|
|
|
|
|
2008-10-11 04:39:40 +08:00
|
|
|
for (; (ds = s->setting); s++) {
|
|
|
|
if (ds->get && s->get_ioctl == cmd)
|
2008-10-11 04:39:33 +08:00
|
|
|
goto read_val;
|
2008-10-11 04:39:40 +08:00
|
|
|
else if (ds->set && s->set_ioctl == cmd)
|
2008-10-11 04:39:33 +08:00
|
|
|
goto set_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
read_val:
|
|
|
|
mutex_lock(&ide_setting_mtx);
|
2008-10-11 04:39:40 +08:00
|
|
|
err = ds->get(drive);
|
2008-10-11 04:39:33 +08:00
|
|
|
mutex_unlock(&ide_setting_mtx);
|
|
|
|
return err >= 0 ? put_user(err, (long __user *)arg) : err;
|
|
|
|
|
|
|
|
set_val:
|
|
|
|
if (bdev != bdev->bd_contains)
|
|
|
|
err = -EINVAL;
|
|
|
|
else {
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
err = -EACCES;
|
|
|
|
else {
|
|
|
|
mutex_lock(&ide_setting_mtx);
|
2008-10-11 04:39:40 +08:00
|
|
|
err = ide_devset_execute(drive, ds, arg);
|
2008-10-11 04:39:33 +08:00
|
|
|
mutex_unlock(&ide_setting_mtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ide_setting_ioctl);
|
|
|
|
|
|
|
|
static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
u16 *id = NULL;
|
|
|
|
int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
|
|
|
|
int rc = 0;
|
|
|
|
|
2008-10-14 03:39:36 +08:00
|
|
|
if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
|
2008-10-11 04:39:33 +08:00
|
|
|
rc = -ENOMSG;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-06-30 10:31:41 +08:00
|
|
|
/* ata_id_to_hd_driveid() relies on 'id' to be fully allocated. */
|
|
|
|
id = kmalloc(ATA_ID_WORDS * 2, GFP_KERNEL);
|
2008-10-11 04:39:33 +08:00
|
|
|
if (id == NULL) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(id, drive->id, size);
|
|
|
|
ata_id_to_hd_driveid(id);
|
|
|
|
|
|
|
|
if (copy_to_user((void __user *)arg, id, size))
|
|
|
|
rc = -EFAULT;
|
|
|
|
|
|
|
|
kfree(id);
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
|
|
|
|
{
|
2008-10-14 03:39:36 +08:00
|
|
|
return put_user((!!(drive->dev_flags & IDE_DFLAG_DSC_OVERLAP)
|
|
|
|
<< IDE_NICE_DSC_OVERLAP) |
|
|
|
|
(!!(drive->dev_flags & IDE_DFLAG_NICE1)
|
|
|
|
<< IDE_NICE_1), (long __user *)arg);
|
2008-10-11 04:39:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
|
|
|
|
{
|
|
|
|
if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
|
2009-01-02 23:12:54 +08:00
|
|
|
(drive->media != ide_tape))
|
2008-10-11 04:39:33 +08:00
|
|
|
return -EPERM;
|
|
|
|
|
2008-10-14 03:39:36 +08:00
|
|
|
if ((arg >> IDE_NICE_DSC_OVERLAP) & 1)
|
|
|
|
drive->dev_flags |= IDE_DFLAG_DSC_OVERLAP;
|
|
|
|
else
|
|
|
|
drive->dev_flags &= ~IDE_DFLAG_DSC_OVERLAP;
|
|
|
|
|
|
|
|
if ((arg >> IDE_NICE_1) & 1)
|
|
|
|
drive->dev_flags |= IDE_DFLAG_NICE1;
|
|
|
|
else
|
|
|
|
drive->dev_flags &= ~IDE_DFLAG_NICE1;
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
static int ide_cmd_ioctl(ide_drive_t *drive, unsigned long arg)
|
2008-10-11 04:39:33 +08:00
|
|
|
{
|
|
|
|
u8 *buf = NULL;
|
|
|
|
int bufsize = 0, err = 0;
|
|
|
|
u8 args[4], xfer_rate = 0;
|
2009-03-27 19:46:37 +08:00
|
|
|
struct ide_cmd cmd;
|
|
|
|
struct ide_taskfile *tf = &cmd.tf;
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
if (NULL == (void *) arg) {
|
|
|
|
struct request *rq;
|
|
|
|
|
2018-05-09 15:54:05 +08:00
|
|
|
rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
|
2017-01-31 23:57:30 +08:00
|
|
|
ide_req(rq)->type = ATA_PRIV_TASKFILE;
|
2017-04-20 22:02:55 +08:00
|
|
|
blk_execute_rq(drive->queue, NULL, rq, 0);
|
2017-04-20 22:03:01 +08:00
|
|
|
err = scsi_req(rq)->result ? -EIO : 0;
|
2008-10-11 04:39:33 +08:00
|
|
|
blk_put_request(rq);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_from_user(args, (void __user *)arg, 4))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
2008-10-11 04:39:33 +08:00
|
|
|
tf->feature = args[2];
|
|
|
|
if (args[0] == ATA_CMD_SMART) {
|
|
|
|
tf->nsect = args[3];
|
|
|
|
tf->lbal = args[1];
|
2013-07-25 18:32:42 +08:00
|
|
|
tf->lbam = ATA_SMART_LBAM_PASS;
|
|
|
|
tf->lbah = ATA_SMART_LBAH_PASS;
|
2009-04-08 20:13:01 +08:00
|
|
|
cmd.valid.out.tf = IDE_VALID_OUT_TF;
|
|
|
|
cmd.valid.in.tf = IDE_VALID_NSECT;
|
2008-10-11 04:39:33 +08:00
|
|
|
} else {
|
|
|
|
tf->nsect = args[1];
|
2009-04-08 20:13:01 +08:00
|
|
|
cmd.valid.out.tf = IDE_VALID_FEATURE | IDE_VALID_NSECT;
|
|
|
|
cmd.valid.in.tf = IDE_VALID_NSECT;
|
2008-10-11 04:39:33 +08:00
|
|
|
}
|
|
|
|
tf->command = args[0];
|
2009-03-27 19:46:39 +08:00
|
|
|
cmd.protocol = args[3] ? ATA_PROT_PIO : ATA_PROT_NODATA;
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
if (args[3]) {
|
2009-03-27 19:46:37 +08:00
|
|
|
cmd.tf_flags |= IDE_TFLAG_IO_16BIT;
|
2008-10-11 04:39:33 +08:00
|
|
|
bufsize = SECTOR_SIZE * args[3];
|
|
|
|
buf = kzalloc(bufsize, GFP_KERNEL);
|
|
|
|
if (buf == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tf->command == ATA_CMD_SET_FEATURES &&
|
|
|
|
tf->feature == SETFEATURES_XFER &&
|
2009-06-16 04:13:45 +08:00
|
|
|
tf->nsect >= XFER_SW_DMA_0) {
|
2009-11-26 07:04:54 +08:00
|
|
|
xfer_rate = ide_find_dma_mode(drive, tf->nsect);
|
2009-06-16 04:13:45 +08:00
|
|
|
if (xfer_rate != tf->nsect) {
|
|
|
|
err = -EINVAL;
|
2008-10-11 04:39:33 +08:00
|
|
|
goto abort;
|
|
|
|
}
|
2009-06-23 19:35:51 +08:00
|
|
|
|
|
|
|
cmd.tf_flags |= IDE_TFLAG_SET_XFER;
|
2008-10-11 04:39:33 +08:00
|
|
|
}
|
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
err = ide_raw_taskfile(drive, &cmd, buf, args[3]);
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
args[0] = tf->status;
|
|
|
|
args[1] = tf->error;
|
|
|
|
args[2] = tf->nsect;
|
|
|
|
abort:
|
|
|
|
if (copy_to_user((void __user *)arg, &args, 4))
|
|
|
|
err = -EFAULT;
|
|
|
|
if (buf) {
|
|
|
|
if (copy_to_user((void __user *)(arg + 4), buf, bufsize))
|
|
|
|
err = -EFAULT;
|
|
|
|
kfree(buf);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
static int ide_task_ioctl(ide_drive_t *drive, unsigned long arg)
|
2008-10-11 04:39:33 +08:00
|
|
|
{
|
|
|
|
void __user *p = (void __user *)arg;
|
|
|
|
int err = 0;
|
|
|
|
u8 args[7];
|
2009-03-27 19:46:37 +08:00
|
|
|
struct ide_cmd cmd;
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
if (copy_from_user(args, p, 7))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
2009-04-08 20:13:02 +08:00
|
|
|
memcpy(&cmd.tf.feature, &args[1], 6);
|
2009-03-27 19:46:37 +08:00
|
|
|
cmd.tf.command = args[0];
|
2009-04-08 20:13:01 +08:00
|
|
|
cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
|
|
|
|
cmd.valid.in.tf = IDE_VALID_IN_TF | IDE_VALID_DEVICE;
|
2008-10-11 04:39:33 +08:00
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
err = ide_no_data_taskfile(drive, &cmd);
|
2008-10-11 04:39:33 +08:00
|
|
|
|
2009-03-27 19:46:37 +08:00
|
|
|
args[0] = cmd.tf.command;
|
2009-04-08 20:13:02 +08:00
|
|
|
memcpy(&args[1], &cmd.tf.feature, 6);
|
2008-10-11 04:39:33 +08:00
|
|
|
|
|
|
|
if (copy_to_user(p, args, 7))
|
|
|
|
err = -EFAULT;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int generic_drive_reset(ide_drive_t *drive)
|
|
|
|
{
|
|
|
|
struct request *rq;
|
|
|
|
int ret = 0;
|
|
|
|
|
2018-05-09 15:54:05 +08:00
|
|
|
rq = blk_get_request(drive->queue, REQ_OP_DRV_IN, 0);
|
2017-01-31 23:57:30 +08:00
|
|
|
ide_req(rq)->type = ATA_PRIV_MISC;
|
2017-01-27 16:46:29 +08:00
|
|
|
scsi_req(rq)->cmd_len = 1;
|
|
|
|
scsi_req(rq)->cmd[0] = REQ_DRIVE_RESET;
|
2017-04-20 22:02:55 +08:00
|
|
|
blk_execute_rq(drive->queue, NULL, rq, 1);
|
2017-04-20 22:03:01 +08:00
|
|
|
ret = scsi_req(rq)->result;
|
2008-10-11 04:39:33 +08:00
|
|
|
blk_put_request(rq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-03 05:19:43 +08:00
|
|
|
int generic_ide_ioctl(ide_drive_t *drive, struct block_device *bdev,
|
2008-10-11 04:39:33 +08:00
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = ide_setting_ioctl(drive, bdev, cmd, arg, ide_ioctl_settings);
|
|
|
|
if (err != -EOPNOTSUPP)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case HDIO_OBSOLETE_IDENTITY:
|
|
|
|
case HDIO_GET_IDENTITY:
|
|
|
|
if (bdev != bdev->bd_contains)
|
|
|
|
return -EINVAL;
|
|
|
|
return ide_get_identity_ioctl(drive, cmd, arg);
|
|
|
|
case HDIO_GET_NICE:
|
|
|
|
return ide_get_nice_ioctl(drive, arg);
|
|
|
|
case HDIO_SET_NICE:
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EACCES;
|
|
|
|
return ide_set_nice_ioctl(drive, arg);
|
|
|
|
#ifdef CONFIG_IDE_TASK_IOCTL
|
|
|
|
case HDIO_DRIVE_TASKFILE:
|
|
|
|
if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
|
|
|
|
return -EACCES;
|
|
|
|
if (drive->media == ide_disk)
|
2009-03-27 19:46:37 +08:00
|
|
|
return ide_taskfile_ioctl(drive, arg);
|
2008-10-11 04:39:33 +08:00
|
|
|
return -ENOMSG;
|
|
|
|
#endif
|
|
|
|
case HDIO_DRIVE_CMD:
|
|
|
|
if (!capable(CAP_SYS_RAWIO))
|
|
|
|
return -EACCES;
|
2009-03-27 19:46:37 +08:00
|
|
|
return ide_cmd_ioctl(drive, arg);
|
2008-10-11 04:39:33 +08:00
|
|
|
case HDIO_DRIVE_TASK:
|
|
|
|
if (!capable(CAP_SYS_RAWIO))
|
|
|
|
return -EACCES;
|
2009-03-27 19:46:37 +08:00
|
|
|
return ide_task_ioctl(drive, arg);
|
2008-10-11 04:39:33 +08:00
|
|
|
case HDIO_DRIVE_RESET:
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EACCES;
|
|
|
|
return generic_drive_reset(drive);
|
|
|
|
case HDIO_GET_BUSSTATE:
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EACCES;
|
|
|
|
if (put_user(BUSSTATE_ON, (long __user *)arg))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
case HDIO_SET_BUSSTATE:
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EACCES;
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(generic_ide_ioctl);
|