blob: 572bbc2f900d89992da0b35d22b16c894e847adf [file] [log] [blame]
Adel Gadllah0b07de82008-06-26 13:48:27 +02001/*
2 * Copyright 2004 Peter M. Jones <pjones@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 *
18 */
19
20#include <linux/list.h>
21#include <linux/genhd.h>
22#include <linux/spinlock.h>
Adel Gadllah0b07de82008-06-26 13:48:27 +020023#include <linux/capability.h>
24#include <linux/bitops.h>
James Bottomleyf290f192009-02-08 21:59:48 -060025#include <linux/blkdev.h>
Adel Gadllah0b07de82008-06-26 13:48:27 +020026
27#include <scsi/scsi.h>
28#include <linux/cdrom.h>
29
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +090030int blk_verify_command(struct blk_cmd_filter *filter,
Al Viroaeb5d722008-09-02 15:28:45 -040031 unsigned char *cmd, fmode_t has_write_perm)
Adel Gadllah0b07de82008-06-26 13:48:27 +020032{
33 /* root can do any command. */
34 if (capable(CAP_SYS_RAWIO))
35 return 0;
36
37 /* if there's no filter set, assume we're filtering everything out */
38 if (!filter)
39 return -EPERM;
40
41 /* Anybody who can open the device can do a read-safe command */
42 if (test_bit(cmd[0], filter->read_ok))
43 return 0;
44
45 /* Write-safe commands require a writable open */
FUJITA Tomonoriabf54392008-08-16 14:10:05 +090046 if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
Adel Gadllah0b07de82008-06-26 13:48:27 +020047 return 0;
48
49 return -EPERM;
50}
Adel Gadllah0b07de82008-06-26 13:48:27 +020051EXPORT_SYMBOL(blk_verify_command);
52
Jens Axboe2dc75d32008-09-11 14:20:23 +020053#if 0
Adel Gadllah0b07de82008-06-26 13:48:27 +020054/* and now, the sysfs stuff */
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +090055static ssize_t rcf_cmds_show(struct blk_cmd_filter *filter, char *page,
Adel Gadllah0b07de82008-06-26 13:48:27 +020056 int rw)
57{
58 char *npage = page;
59 unsigned long *okbits;
60 int i;
61
62 if (rw == READ)
63 okbits = filter->read_ok;
64 else
65 okbits = filter->write_ok;
66
67 for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) {
68 if (test_bit(i, okbits)) {
Adel Gadllaha4a77892008-08-16 14:21:06 +090069 npage += sprintf(npage, "0x%02x", i);
Adel Gadllah0b07de82008-06-26 13:48:27 +020070 if (i < BLK_SCSI_MAX_CMDS - 1)
71 sprintf(npage++, " ");
72 }
73 }
74
75 if (npage != page)
76 npage += sprintf(npage, "\n");
77
78 return npage - page;
79}
80
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +090081static ssize_t rcf_readcmds_show(struct blk_cmd_filter *filter, char *page)
Adel Gadllah0b07de82008-06-26 13:48:27 +020082{
83 return rcf_cmds_show(filter, page, READ);
84}
85
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +090086static ssize_t rcf_writecmds_show(struct blk_cmd_filter *filter,
Adel Gadllah0b07de82008-06-26 13:48:27 +020087 char *page)
88{
89 return rcf_cmds_show(filter, page, WRITE);
90}
91
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +090092static ssize_t rcf_cmds_store(struct blk_cmd_filter *filter,
Adel Gadllah0b07de82008-06-26 13:48:27 +020093 const char *page, size_t count, int rw)
94{
Adel Gadllah0b07de82008-06-26 13:48:27 +020095 unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits;
Adel Gadllaha4a77892008-08-16 14:21:06 +090096 int cmd, set;
97 char *p, *status;
Adel Gadllah0b07de82008-06-26 13:48:27 +020098
Adel Gadllaha4a77892008-08-16 14:21:06 +090099 if (rw == READ) {
100 memcpy(&okbits, filter->read_ok, sizeof(okbits));
101 target_okbits = filter->read_ok;
102 } else {
103 memcpy(&okbits, filter->write_ok, sizeof(okbits));
104 target_okbits = filter->write_ok;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200105 }
106
Adel Gadllaha4a77892008-08-16 14:21:06 +0900107 while ((p = strsep((char **)&page, " ")) != NULL) {
108 set = 1;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200109
Adel Gadllaha4a77892008-08-16 14:21:06 +0900110 if (p[0] == '+') {
111 p++;
112 } else if (p[0] == '-') {
113 set = 0;
114 p++;
115 }
116
117 cmd = simple_strtol(p, &status, 16);
118
119 /* either of these cases means invalid input, so do nothing. */
120 if ((status == p) || cmd >= BLK_SCSI_MAX_CMDS)
121 return -EINVAL;
122
123 if (set)
124 __set_bit(cmd, okbits);
125 else
126 __clear_bit(cmd, okbits);
127 }
128
129 memcpy(target_okbits, okbits, sizeof(okbits));
Adel Gadllah0b07de82008-06-26 13:48:27 +0200130 return count;
131}
132
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900133static ssize_t rcf_readcmds_store(struct blk_cmd_filter *filter,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200134 const char *page, size_t count)
135{
136 return rcf_cmds_store(filter, page, count, READ);
137}
138
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900139static ssize_t rcf_writecmds_store(struct blk_cmd_filter *filter,
Adel Gadllah0b07de82008-06-26 13:48:27 +0200140 const char *page, size_t count)
141{
142 return rcf_cmds_store(filter, page, count, WRITE);
143}
144
145struct rcf_sysfs_entry {
146 struct attribute attr;
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900147 ssize_t (*show)(struct blk_cmd_filter *, char *);
148 ssize_t (*store)(struct blk_cmd_filter *, const char *, size_t);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200149};
150
151static struct rcf_sysfs_entry rcf_readcmds_entry = {
152 .attr = { .name = "read_table", .mode = S_IRUGO | S_IWUSR },
153 .show = rcf_readcmds_show,
154 .store = rcf_readcmds_store,
155};
156
157static struct rcf_sysfs_entry rcf_writecmds_entry = {
158 .attr = {.name = "write_table", .mode = S_IRUGO | S_IWUSR },
159 .show = rcf_writecmds_show,
160 .store = rcf_writecmds_store,
161};
162
163static struct attribute *default_attrs[] = {
164 &rcf_readcmds_entry.attr,
165 &rcf_writecmds_entry.attr,
166 NULL,
167};
168
169#define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr)
170
171static ssize_t
172rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
173{
174 struct rcf_sysfs_entry *entry = to_rcf(attr);
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900175 struct blk_cmd_filter *filter;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200176
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900177 filter = container_of(kobj, struct blk_cmd_filter, kobj);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200178 if (entry->show)
179 return entry->show(filter, page);
180
181 return 0;
182}
183
184static ssize_t
185rcf_attr_store(struct kobject *kobj, struct attribute *attr,
186 const char *page, size_t length)
187{
188 struct rcf_sysfs_entry *entry = to_rcf(attr);
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900189 struct blk_cmd_filter *filter;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200190
191 if (!capable(CAP_SYS_RAWIO))
192 return -EPERM;
193
194 if (!entry->store)
195 return -EINVAL;
196
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900197 filter = container_of(kobj, struct blk_cmd_filter, kobj);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200198 return entry->store(filter, page, length);
199}
200
201static struct sysfs_ops rcf_sysfs_ops = {
202 .show = rcf_attr_show,
203 .store = rcf_attr_store,
204};
205
206static struct kobj_type rcf_ktype = {
207 .sysfs_ops = &rcf_sysfs_ops,
208 .default_attrs = default_attrs,
209};
210
Adel Gadllah0b07de82008-06-26 13:48:27 +0200211int blk_register_filter(struct gendisk *disk)
212{
213 int ret;
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900214 struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200215
Tejun Heo4c465012008-08-25 19:56:11 +0900216 ret = kobject_init_and_add(&filter->kobj, &rcf_ktype,
217 &disk_to_dev(disk)->kobj,
FUJITA Tomonoriabf54392008-08-16 14:10:05 +0900218 "%s", "cmd_filter");
Adel Gadllah0b07de82008-06-26 13:48:27 +0200219 if (ret < 0)
220 return ret;
221
Adel Gadllah0b07de82008-06-26 13:48:27 +0200222 return 0;
223}
FUJITA Tomonoribb23b432008-08-29 11:47:07 +0200224EXPORT_SYMBOL(blk_register_filter);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200225
226void blk_unregister_filter(struct gendisk *disk)
227{
FUJITA Tomonori4beab5c2008-07-26 18:03:25 +0900228 struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
Adel Gadllah0b07de82008-06-26 13:48:27 +0200229
230 kobject_put(&filter->kobj);
Adel Gadllah0b07de82008-06-26 13:48:27 +0200231}
FUJITA Tomonoribb23b432008-08-29 11:47:07 +0200232EXPORT_SYMBOL(blk_unregister_filter);
Jens Axboe2dc75d32008-09-11 14:20:23 +0200233#endif