aboutsummaryrefslogtreecommitdiffstats
path: root/block/cmd-filter.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/cmd-filter.c')
-rw-r--r--block/cmd-filter.c196
1 files changed, 48 insertions, 148 deletions
diff --git a/block/cmd-filter.c b/block/cmd-filter.c
index eec4404fd357..1d4026206ac2 100644
--- a/block/cmd-filter.c
+++ b/block/cmd-filter.c
@@ -20,15 +20,14 @@
20#include <linux/list.h> 20#include <linux/list.h>
21#include <linux/genhd.h> 21#include <linux/genhd.h>
22#include <linux/spinlock.h> 22#include <linux/spinlock.h>
23#include <linux/parser.h>
24#include <linux/capability.h> 23#include <linux/capability.h>
25#include <linux/bitops.h> 24#include <linux/bitops.h>
26 25
27#include <scsi/scsi.h> 26#include <scsi/scsi.h>
28#include <linux/cdrom.h> 27#include <linux/cdrom.h>
29 28
30int blk_cmd_filter_verify_command(struct blk_scsi_cmd_filter *filter, 29int blk_verify_command(struct blk_cmd_filter *filter,
31 unsigned char *cmd, mode_t *f_mode) 30 unsigned char *cmd, int has_write_perm)
32{ 31{
33 /* root can do any command. */ 32 /* root can do any command. */
34 if (capable(CAP_SYS_RAWIO)) 33 if (capable(CAP_SYS_RAWIO))
@@ -43,34 +42,15 @@ int blk_cmd_filter_verify_command(struct blk_scsi_cmd_filter *filter,
43 return 0; 42 return 0;
44 43
45 /* Write-safe commands require a writable open */ 44 /* Write-safe commands require a writable open */
46 if (test_bit(cmd[0], filter->write_ok) && (*f_mode & FMODE_WRITE)) 45 if (test_bit(cmd[0], filter->write_ok) && has_write_perm)
47 return 0; 46 return 0;
48 47
49 return -EPERM; 48 return -EPERM;
50} 49}
51EXPORT_SYMBOL(blk_cmd_filter_verify_command);
52
53int blk_verify_command(struct file *file, unsigned char *cmd)
54{
55 struct gendisk *disk;
56 struct inode *inode;
57
58 if (!file)
59 return -EINVAL;
60
61 inode = file->f_dentry->d_inode;
62 if (!inode)
63 return -EINVAL;
64
65 disk = inode->i_bdev->bd_disk;
66
67 return blk_cmd_filter_verify_command(&disk->cmd_filter,
68 cmd, &file->f_mode);
69}
70EXPORT_SYMBOL(blk_verify_command); 50EXPORT_SYMBOL(blk_verify_command);
71 51
72/* and now, the sysfs stuff */ 52/* and now, the sysfs stuff */
73static ssize_t rcf_cmds_show(struct blk_scsi_cmd_filter *filter, char *page, 53static ssize_t rcf_cmds_show(struct blk_cmd_filter *filter, char *page,
74 int rw) 54 int rw)
75{ 55{
76 char *npage = page; 56 char *npage = page;
@@ -84,8 +64,7 @@ static ssize_t rcf_cmds_show(struct blk_scsi_cmd_filter *filter, char *page,
84 64
85 for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) { 65 for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) {
86 if (test_bit(i, okbits)) { 66 if (test_bit(i, okbits)) {
87 sprintf(npage, "%02x", i); 67 npage += sprintf(npage, "0x%02x", i);
88 npage += 2;
89 if (i < BLK_SCSI_MAX_CMDS - 1) 68 if (i < BLK_SCSI_MAX_CMDS - 1)
90 sprintf(npage++, " "); 69 sprintf(npage++, " ");
91 } 70 }
@@ -97,57 +76,65 @@ static ssize_t rcf_cmds_show(struct blk_scsi_cmd_filter *filter, char *page,
97 return npage - page; 76 return npage - page;
98} 77}
99 78
100static ssize_t rcf_readcmds_show(struct blk_scsi_cmd_filter *filter, char *page) 79static ssize_t rcf_readcmds_show(struct blk_cmd_filter *filter, char *page)
101{ 80{
102 return rcf_cmds_show(filter, page, READ); 81 return rcf_cmds_show(filter, page, READ);
103} 82}
104 83
105static ssize_t rcf_writecmds_show(struct blk_scsi_cmd_filter *filter, 84static ssize_t rcf_writecmds_show(struct blk_cmd_filter *filter,
106 char *page) 85 char *page)
107{ 86{
108 return rcf_cmds_show(filter, page, WRITE); 87 return rcf_cmds_show(filter, page, WRITE);
109} 88}
110 89
111static ssize_t rcf_cmds_store(struct blk_scsi_cmd_filter *filter, 90static ssize_t rcf_cmds_store(struct blk_cmd_filter *filter,
112 const char *page, size_t count, int rw) 91 const char *page, size_t count, int rw)
113{ 92{
114 ssize_t ret = 0;
115 unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits; 93 unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits;
116 int cmd, status, len; 94 int cmd, set;
117 substring_t ss; 95 char *p, *status;
118 96
119 memset(&okbits, 0, sizeof(okbits)); 97 if (rw == READ) {
120 98 memcpy(&okbits, filter->read_ok, sizeof(okbits));
121 for (len = strlen(page); len > 0; len -= 3) { 99 target_okbits = filter->read_ok;
122 if (len < 2) 100 } else {
123 break; 101 memcpy(&okbits, filter->write_ok, sizeof(okbits));
124 ss.from = (char *) page + ret; 102 target_okbits = filter->write_ok;
125 ss.to = (char *) page + ret + 2; 103 }
126 ret += 3; 104
127 status = match_hex(&ss, &cmd); 105 while ((p = strsep((char **)&page, " ")) != NULL) {
106 set = 1;
107
108 if (p[0] == '+') {
109 p++;
110 } else if (p[0] == '-') {
111 set = 0;
112 p++;
113 }
114
115 cmd = simple_strtol(p, &status, 16);
116
128 /* either of these cases means invalid input, so do nothing. */ 117 /* either of these cases means invalid input, so do nothing. */
129 if (status || cmd >= BLK_SCSI_MAX_CMDS) 118 if ((status == p) || cmd >= BLK_SCSI_MAX_CMDS)
130 return -EINVAL; 119 return -EINVAL;
131 120
132 __set_bit(cmd, okbits); 121 if (set)
122 __set_bit(cmd, okbits);
123 else
124 __clear_bit(cmd, okbits);
133 } 125 }
134 126
135 if (rw == READ) 127 memcpy(target_okbits, okbits, sizeof(okbits));
136 target_okbits = filter->read_ok;
137 else
138 target_okbits = filter->write_ok;
139
140 memmove(target_okbits, okbits, sizeof(okbits));
141 return count; 128 return count;
142} 129}
143 130
144static ssize_t rcf_readcmds_store(struct blk_scsi_cmd_filter *filter, 131static ssize_t rcf_readcmds_store(struct blk_cmd_filter *filter,
145 const char *page, size_t count) 132 const char *page, size_t count)
146{ 133{
147 return rcf_cmds_store(filter, page, count, READ); 134 return rcf_cmds_store(filter, page, count, READ);
148} 135}
149 136
150static ssize_t rcf_writecmds_store(struct blk_scsi_cmd_filter *filter, 137static ssize_t rcf_writecmds_store(struct blk_cmd_filter *filter,
151 const char *page, size_t count) 138 const char *page, size_t count)
152{ 139{
153 return rcf_cmds_store(filter, page, count, WRITE); 140 return rcf_cmds_store(filter, page, count, WRITE);
@@ -155,8 +142,8 @@ static ssize_t rcf_writecmds_store(struct blk_scsi_cmd_filter *filter,
155 142
156struct rcf_sysfs_entry { 143struct rcf_sysfs_entry {
157 struct attribute attr; 144 struct attribute attr;
158 ssize_t (*show)(struct blk_scsi_cmd_filter *, char *); 145 ssize_t (*show)(struct blk_cmd_filter *, char *);
159 ssize_t (*store)(struct blk_scsi_cmd_filter *, const char *, size_t); 146 ssize_t (*store)(struct blk_cmd_filter *, const char *, size_t);
160}; 147};
161 148
162static struct rcf_sysfs_entry rcf_readcmds_entry = { 149static struct rcf_sysfs_entry rcf_readcmds_entry = {
@@ -183,9 +170,9 @@ static ssize_t
183rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page) 170rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
184{ 171{
185 struct rcf_sysfs_entry *entry = to_rcf(attr); 172 struct rcf_sysfs_entry *entry = to_rcf(attr);
186 struct blk_scsi_cmd_filter *filter; 173 struct blk_cmd_filter *filter;
187 174
188 filter = container_of(kobj, struct blk_scsi_cmd_filter, kobj); 175 filter = container_of(kobj, struct blk_cmd_filter, kobj);
189 if (entry->show) 176 if (entry->show)
190 return entry->show(filter, page); 177 return entry->show(filter, page);
191 178
@@ -197,7 +184,7 @@ rcf_attr_store(struct kobject *kobj, struct attribute *attr,
197 const char *page, size_t length) 184 const char *page, size_t length)
198{ 185{
199 struct rcf_sysfs_entry *entry = to_rcf(attr); 186 struct rcf_sysfs_entry *entry = to_rcf(attr);
200 struct blk_scsi_cmd_filter *filter; 187 struct blk_cmd_filter *filter;
201 188
202 if (!capable(CAP_SYS_RAWIO)) 189 if (!capable(CAP_SYS_RAWIO))
203 return -EPERM; 190 return -EPERM;
@@ -205,7 +192,7 @@ rcf_attr_store(struct kobject *kobj, struct attribute *attr,
205 if (!entry->store) 192 if (!entry->store)
206 return -EINVAL; 193 return -EINVAL;
207 194
208 filter = container_of(kobj, struct blk_scsi_cmd_filter, kobj); 195 filter = container_of(kobj, struct blk_cmd_filter, kobj);
209 return entry->store(filter, page, length); 196 return entry->store(filter, page, length);
210} 197}
211 198
@@ -219,114 +206,27 @@ static struct kobj_type rcf_ktype = {
219 .default_attrs = default_attrs, 206 .default_attrs = default_attrs,
220}; 207};
221 208
222#ifndef MAINTENANCE_IN_CMD
223#define MAINTENANCE_IN_CMD 0xa3
224#endif
225
226static void rcf_set_defaults(struct blk_scsi_cmd_filter *filter)
227{
228 /* Basic read-only commands */
229 __set_bit(TEST_UNIT_READY, filter->read_ok);
230 __set_bit(REQUEST_SENSE, filter->read_ok);
231 __set_bit(READ_6, filter->read_ok);
232 __set_bit(READ_10, filter->read_ok);
233 __set_bit(READ_12, filter->read_ok);
234 __set_bit(READ_16, filter->read_ok);
235 __set_bit(READ_BUFFER, filter->read_ok);
236 __set_bit(READ_DEFECT_DATA, filter->read_ok);
237 __set_bit(READ_CAPACITY, filter->read_ok);
238 __set_bit(READ_LONG, filter->read_ok);
239 __set_bit(INQUIRY, filter->read_ok);
240 __set_bit(MODE_SENSE, filter->read_ok);
241 __set_bit(MODE_SENSE_10, filter->read_ok);
242 __set_bit(LOG_SENSE, filter->read_ok);
243 __set_bit(START_STOP, filter->read_ok);
244 __set_bit(GPCMD_VERIFY_10, filter->read_ok);
245 __set_bit(VERIFY_16, filter->read_ok);
246 __set_bit(REPORT_LUNS, filter->read_ok);
247 __set_bit(SERVICE_ACTION_IN, filter->read_ok);
248 __set_bit(RECEIVE_DIAGNOSTIC, filter->read_ok);
249 __set_bit(MAINTENANCE_IN_CMD, filter->read_ok);
250 __set_bit(GPCMD_READ_BUFFER_CAPACITY, filter->read_ok);
251
252 /* Audio CD commands */
253 __set_bit(GPCMD_PLAY_CD, filter->read_ok);
254 __set_bit(GPCMD_PLAY_AUDIO_10, filter->read_ok);
255 __set_bit(GPCMD_PLAY_AUDIO_MSF, filter->read_ok);
256 __set_bit(GPCMD_PLAY_AUDIO_TI, filter->read_ok);
257 __set_bit(GPCMD_PAUSE_RESUME, filter->read_ok);
258
259 /* CD/DVD data reading */
260 __set_bit(GPCMD_READ_CD, filter->read_ok);
261 __set_bit(GPCMD_READ_CD_MSF, filter->read_ok);
262 __set_bit(GPCMD_READ_DISC_INFO, filter->read_ok);
263 __set_bit(GPCMD_READ_CDVD_CAPACITY, filter->read_ok);
264 __set_bit(GPCMD_READ_DVD_STRUCTURE, filter->read_ok);
265 __set_bit(GPCMD_READ_HEADER, filter->read_ok);
266 __set_bit(GPCMD_READ_TRACK_RZONE_INFO, filter->read_ok);
267 __set_bit(GPCMD_READ_SUBCHANNEL, filter->read_ok);
268 __set_bit(GPCMD_READ_TOC_PMA_ATIP, filter->read_ok);
269 __set_bit(GPCMD_REPORT_KEY, filter->read_ok);
270 __set_bit(GPCMD_SCAN, filter->read_ok);
271 __set_bit(GPCMD_GET_CONFIGURATION, filter->read_ok);
272 __set_bit(GPCMD_READ_FORMAT_CAPACITIES, filter->read_ok);
273 __set_bit(GPCMD_GET_EVENT_STATUS_NOTIFICATION, filter->read_ok);
274 __set_bit(GPCMD_GET_PERFORMANCE, filter->read_ok);
275 __set_bit(GPCMD_SEEK, filter->read_ok);
276 __set_bit(GPCMD_STOP_PLAY_SCAN, filter->read_ok);
277
278 /* Basic writing commands */
279 __set_bit(WRITE_6, filter->write_ok);
280 __set_bit(WRITE_10, filter->write_ok);
281 __set_bit(WRITE_VERIFY, filter->write_ok);
282 __set_bit(WRITE_12, filter->write_ok);
283 __set_bit(WRITE_VERIFY_12, filter->write_ok);
284 __set_bit(WRITE_16, filter->write_ok);
285 __set_bit(WRITE_LONG, filter->write_ok);
286 __set_bit(WRITE_LONG_2, filter->write_ok);
287 __set_bit(ERASE, filter->write_ok);
288 __set_bit(GPCMD_MODE_SELECT_10, filter->write_ok);
289 __set_bit(MODE_SELECT, filter->write_ok);
290 __set_bit(LOG_SELECT, filter->write_ok);
291 __set_bit(GPCMD_BLANK, filter->write_ok);
292 __set_bit(GPCMD_CLOSE_TRACK, filter->write_ok);
293 __set_bit(GPCMD_FLUSH_CACHE, filter->write_ok);
294 __set_bit(GPCMD_FORMAT_UNIT, filter->write_ok);
295 __set_bit(GPCMD_REPAIR_RZONE_TRACK, filter->write_ok);
296 __set_bit(GPCMD_RESERVE_RZONE_TRACK, filter->write_ok);
297 __set_bit(GPCMD_SEND_DVD_STRUCTURE, filter->write_ok);
298 __set_bit(GPCMD_SEND_EVENT, filter->write_ok);
299 __set_bit(GPCMD_SEND_KEY, filter->write_ok);
300 __set_bit(GPCMD_SEND_OPC, filter->write_ok);
301 __set_bit(GPCMD_SEND_CUE_SHEET, filter->write_ok);
302 __set_bit(GPCMD_SET_SPEED, filter->write_ok);
303 __set_bit(GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL, filter->write_ok);
304 __set_bit(GPCMD_LOAD_UNLOAD, filter->write_ok);
305 __set_bit(GPCMD_SET_STREAMING, filter->write_ok);
306}
307
308int blk_register_filter(struct gendisk *disk) 209int blk_register_filter(struct gendisk *disk)
309{ 210{
310 int ret; 211 int ret;
311 struct blk_scsi_cmd_filter *filter = &disk->cmd_filter; 212 struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
312 struct kobject *parent = kobject_get(disk->holder_dir->parent); 213 struct kobject *parent = kobject_get(disk->holder_dir->parent);
313 214
314 if (!parent) 215 if (!parent)
315 return -ENODEV; 216 return -ENODEV;
316 217
317 ret = kobject_init_and_add(&filter->kobj, &rcf_ktype, parent, 218 ret = kobject_init_and_add(&filter->kobj, &rcf_ktype, parent,
318 "%s", "cmd_filter"); 219 "%s", "cmd_filter");
319 220
320 if (ret < 0) 221 if (ret < 0)
321 return ret; 222 return ret;
322 223
323 rcf_set_defaults(filter);
324 return 0; 224 return 0;
325} 225}
326 226
327void blk_unregister_filter(struct gendisk *disk) 227void blk_unregister_filter(struct gendisk *disk)
328{ 228{
329 struct blk_scsi_cmd_filter *filter = &disk->cmd_filter; 229 struct blk_cmd_filter *filter = &disk->queue->cmd_filter;
330 230
331 kobject_put(&filter->kobj); 231 kobject_put(&filter->kobj);
332 kobject_put(disk->holder_dir->parent); 232 kobject_put(disk->holder_dir->parent);