linux/block/cmd-filter.c

233 lines
5.4 KiB
C
Raw Normal View History

/*
* Copyright 2004 Peter M. Jones <pjones@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
*
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public Licens
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
*
*/
#include <linux/list.h>
#include <linux/genhd.h>
#include <linux/spinlock.h>
#include <linux/capability.h>
#include <linux/bitops.h>
#include <scsi/scsi.h>
#include <linux/cdrom.h>
int blk_verify_command(struct blk_cmd_filter *filter,
unsigned char *cmd, fmode_t has_write_perm)
{
/* root can do any command. */
if (capable(CAP_SYS_RAWIO))
return 0;
/* if there's no filter set, assume we're filtering everything out */
if (!filter)
return -EPERM;
/* Anybody who can open the device can do a read-safe command */
if (test_bit(cmd[0], filter->read_ok))
return 0;
/* Write-safe commands require a writable open */
if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
return 0;
return -EPERM;
}
EXPORT_SYMBOL(blk_verify_command);
#if 0
/* and now, the sysfs stuff */
static ssize_t rcf_cmds_show(struct blk_cmd_filter *filter, char *page,
int rw)
{
char *npage = page;
unsigned long *okbits;
int i;
if (rw == READ)
okbits = filter->read_ok;
else
okbits = filter->write_ok;
for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) {
if (test_bit(i, okbits)) {
npage += sprintf(npage, "0x%02x", i);
if (i < BLK_SCSI_MAX_CMDS - 1)
sprintf(npage++, " ");
}
}
if (npage != page)
npage += sprintf(npage, "\n");
return npage - page;
}
static ssize_t rcf_readcmds_show(struct blk_cmd_filter *filter, char *page)
{
return rcf_cmds_show(filter, page, READ);
}
static ssize_t rcf_writecmds_show(struct blk_cmd_filter *filter,
char *page)
{
return rcf_cmds_show(filter, page, WRITE);
}
static ssize_t rcf_cmds_store(struct blk_cmd_filter *filter,
const char *page, size_t count, int rw)
{
unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits;
int cmd, set;
char *p, *status;
if (rw == READ) {
memcpy(&okbits, filter->read_ok, sizeof(okbits));
target_okbits = filter->read_ok;
} else {
memcpy(&okbits, filter->write_ok, sizeof(okbits));
target_okbits = filter->write_ok;
}
while ((p = strsep((char **)&page, " ")) != NULL) {
set = 1;
if (p[0] == '+') {
p++;
} else if (p[0] == '-') {
set = 0;
p++;
}
cmd = simple_strtol(p, &status, 16);
/* either of these cases means invalid input, so do nothing. */
if ((status == p) || cmd >= BLK_SCSI_MAX_CMDS)
return -EINVAL;
if (set)
__set_bit(cmd, okbits);
else
__clear_bit(cmd, okbits);
}
memcpy(target_okbits, okbits, sizeof(okbits));
return count;
}
static ssize_t rcf_readcmds_store(struct blk_cmd_filter *filter,
const char *page, size_t count)
{
return rcf_cmds_store(filter, page, count, READ);
}
static ssize_t rcf_writecmds_store(struct blk_cmd_filter *filter,
const char *page, size_t count)
{
return rcf_cmds_store(filter, page, count, WRITE);
}
struct rcf_sysfs_entry {
struct attribute attr;
ssize_t (*show)(struct blk_cmd_filter *, char *);
ssize_t (*store)(struct blk_cmd_filter *, const char *, size_t);
};
static struct rcf_sysfs_entry rcf_readcmds_entry = {
.attr = { .name = "read_table", .mode = S_IRUGO | S_IWUSR },
.show = rcf_readcmds_show,
.store = rcf_readcmds_store,
};
static struct rcf_sysfs_entry rcf_writecmds_entry = {
.attr = {.name = "write_table", .mode = S_IRUGO | S_IWUSR },
.show = rcf_writecmds_show,
.store = rcf_writecmds_store,
};
static struct attribute *default_attrs[] = {
&rcf_readcmds_entry.attr,
&rcf_writecmds_entry.attr,
NULL,
};
#define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr)
static ssize_t
rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
{
struct rcf_sysfs_entry *entry = to_rcf(attr);
struct blk_cmd_filter *filter;
filter = container_of(kobj, struct blk_cmd_filter, kobj);
if (entry->show)
return entry->show(filter, page);
return 0;
}
static ssize_t
rcf_attr_store(struct kobject *kobj, struct attribute *attr,
const char *page, size_t length)
{
struct rcf_sysfs_entry *entry = to_rcf(attr);
struct blk_cmd_filter *filter;
if (!capable(CAP_SYS_RAWIO))
return -EPERM;
if (!entry->store)
return -EINVAL;
filter = container_of(kobj, struct blk_cmd_filter, kobj);
return entry->store(filter, page, length);
}
static struct sysfs_ops rcf_sysfs_ops = {
.show = rcf_attr_show,
.store = rcf_attr_store,
};
static struct kobj_type rcf_ktype = {
.sysfs_ops = &rcf_sysfs_ops,
.default_attrs = default_attrs,
};
int blk_register_filter(struct gendisk *disk)
{
int ret;
struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
ret = kobject_init_and_add(&filter->kobj, &rcf_ktype,
&disk_to_dev(disk)->kobj,
"%s", "cmd_filter");
if (ret < 0)
return ret;
return 0;
}
remove blk_register_filter and blk_unregister_filter in gendisk This patch remove blk_register_filter and blk_unregister_filter in gendisk, and adds them to sd.c, sr.c. and ide-cd.c The commit abf5439370491dd6fbb4fe1a7939680d2a9bc9d4 moved cmdfilter from gendisk to request_queue. It turned out that in some subsystems multiple gendisks share a single request_queue. So we get: Using physmap partition information Creating 3 MTD partitions on "physmap-flash": 0x00000000-0x01c00000 : "User FS" 0x01c00000-0x01c40000 : "booter" kobject (8511c410): tried to init an initialized object, something is seriously wrong. Call Trace: [<8036644c>] dump_stack+0x8/0x34 [<8021f050>] kobject_init+0x50/0xcc [<8021fa18>] kobject_init_and_add+0x24/0x58 [<8021d20c>] blk_register_filter+0x4c/0x64 [<8021c194>] add_disk+0x78/0xe0 [<8027d14c>] add_mtd_blktrans_dev+0x254/0x278 [<8027c8f0>] blktrans_notify_add+0x40/0x78 [<80279c00>] add_mtd_device+0xd0/0x150 [<8027b090>] add_mtd_partitions+0x568/0x5d8 [<80285458>] physmap_flash_probe+0x2ac/0x334 [<802644f8>] driver_probe_device+0x12c/0x244 [<8026465c>] __driver_attach+0x4c/0x84 [<80263c64>] bus_for_each_dev+0x58/0xac [<802633ec>] bus_add_driver+0xc4/0x24c [<802648e0>] driver_register+0xcc/0x184 [<80100460>] _stext+0x60/0x1bc In the long term, we need to fix such subsystems but we need a quick fix now. This patch add the command filter support to only sd and sr though it might be useful for other SG_IO users (such as cciss). Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> Reported-by: Manuel Lauss <mano@roarinelk.homelinux.net> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
2008-08-29 11:47:07 +02:00
EXPORT_SYMBOL(blk_register_filter);
void blk_unregister_filter(struct gendisk *disk)
{
struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
kobject_put(&filter->kobj);
}
remove blk_register_filter and blk_unregister_filter in gendisk This patch remove blk_register_filter and blk_unregister_filter in gendisk, and adds them to sd.c, sr.c. and ide-cd.c The commit abf5439370491dd6fbb4fe1a7939680d2a9bc9d4 moved cmdfilter from gendisk to request_queue. It turned out that in some subsystems multiple gendisks share a single request_queue. So we get: Using physmap partition information Creating 3 MTD partitions on "physmap-flash": 0x00000000-0x01c00000 : "User FS" 0x01c00000-0x01c40000 : "booter" kobject (8511c410): tried to init an initialized object, something is seriously wrong. Call Trace: [<8036644c>] dump_stack+0x8/0x34 [<8021f050>] kobject_init+0x50/0xcc [<8021fa18>] kobject_init_and_add+0x24/0x58 [<8021d20c>] blk_register_filter+0x4c/0x64 [<8021c194>] add_disk+0x78/0xe0 [<8027d14c>] add_mtd_blktrans_dev+0x254/0x278 [<8027c8f0>] blktrans_notify_add+0x40/0x78 [<80279c00>] add_mtd_device+0xd0/0x150 [<8027b090>] add_mtd_partitions+0x568/0x5d8 [<80285458>] physmap_flash_probe+0x2ac/0x334 [<802644f8>] driver_probe_device+0x12c/0x244 [<8026465c>] __driver_attach+0x4c/0x84 [<80263c64>] bus_for_each_dev+0x58/0xac [<802633ec>] bus_add_driver+0xc4/0x24c [<802648e0>] driver_register+0xcc/0x184 [<80100460>] _stext+0x60/0x1bc In the long term, we need to fix such subsystems but we need a quick fix now. This patch add the command filter support to only sd and sr though it might be useful for other SG_IO users (such as cciss). Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> Reported-by: Manuel Lauss <mano@roarinelk.homelinux.net> Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
2008-08-29 11:47:07 +02:00
EXPORT_SYMBOL(blk_unregister_filter);
#endif