2 * Copyright 2004 Peter M. Jones <pjones@redhat.com>
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.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
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-
20 #include <linux/list.h>
21 #include <linux/genhd.h>
22 #include <linux/spinlock.h>
23 #include <linux/capability.h>
24 #include <linux/bitops.h>
25 #include <linux/blkdev.h>
27 #include <scsi/scsi.h>
28 #include <linux/cdrom.h>
30 int blk_verify_command(struct blk_cmd_filter
*filter
,
31 unsigned char *cmd
, fmode_t has_write_perm
)
33 /* root can do any command. */
34 if (capable(CAP_SYS_RAWIO
))
37 /* if there's no filter set, assume we're filtering everything out */
41 /* Anybody who can open the device can do a read-safe command */
42 if (test_bit(cmd
[0], filter
->read_ok
))
45 /* Write-safe commands require a writable open */
46 if (test_bit(cmd
[0], filter
->write_ok
) && has_write_perm
)
51 EXPORT_SYMBOL(blk_verify_command
);
54 /* and now, the sysfs stuff */
55 static ssize_t
rcf_cmds_show(struct blk_cmd_filter
*filter
, char *page
,
59 unsigned long *okbits
;
63 okbits
= filter
->read_ok
;
65 okbits
= filter
->write_ok
;
67 for (i
= 0; i
< BLK_SCSI_MAX_CMDS
; i
++) {
68 if (test_bit(i
, okbits
)) {
69 npage
+= sprintf(npage
, "0x%02x", i
);
70 if (i
< BLK_SCSI_MAX_CMDS
- 1)
71 sprintf(npage
++, " ");
76 npage
+= sprintf(npage
, "\n");
81 static ssize_t
rcf_readcmds_show(struct blk_cmd_filter
*filter
, char *page
)
83 return rcf_cmds_show(filter
, page
, READ
);
86 static ssize_t
rcf_writecmds_show(struct blk_cmd_filter
*filter
,
89 return rcf_cmds_show(filter
, page
, WRITE
);
92 static ssize_t
rcf_cmds_store(struct blk_cmd_filter
*filter
,
93 const char *page
, size_t count
, int rw
)
95 unsigned long okbits
[BLK_SCSI_CMD_PER_LONG
], *target_okbits
;
100 memcpy(&okbits
, filter
->read_ok
, sizeof(okbits
));
101 target_okbits
= filter
->read_ok
;
103 memcpy(&okbits
, filter
->write_ok
, sizeof(okbits
));
104 target_okbits
= filter
->write_ok
;
107 while ((p
= strsep((char **)&page
, " ")) != NULL
) {
112 } else if (p
[0] == '-') {
117 cmd
= simple_strtol(p
, &status
, 16);
119 /* either of these cases means invalid input, so do nothing. */
120 if ((status
== p
) || cmd
>= BLK_SCSI_MAX_CMDS
)
124 __set_bit(cmd
, okbits
);
126 __clear_bit(cmd
, okbits
);
129 memcpy(target_okbits
, okbits
, sizeof(okbits
));
133 static ssize_t
rcf_readcmds_store(struct blk_cmd_filter
*filter
,
134 const char *page
, size_t count
)
136 return rcf_cmds_store(filter
, page
, count
, READ
);
139 static ssize_t
rcf_writecmds_store(struct blk_cmd_filter
*filter
,
140 const char *page
, size_t count
)
142 return rcf_cmds_store(filter
, page
, count
, WRITE
);
145 struct rcf_sysfs_entry
{
146 struct attribute attr
;
147 ssize_t (*show
)(struct blk_cmd_filter
*, char *);
148 ssize_t (*store
)(struct blk_cmd_filter
*, const char *, size_t);
151 static 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
,
157 static 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
,
163 static struct attribute
*default_attrs
[] = {
164 &rcf_readcmds_entry
.attr
,
165 &rcf_writecmds_entry
.attr
,
169 #define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr)
172 rcf_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
174 struct rcf_sysfs_entry
*entry
= to_rcf(attr
);
175 struct blk_cmd_filter
*filter
;
177 filter
= container_of(kobj
, struct blk_cmd_filter
, kobj
);
179 return entry
->show(filter
, page
);
185 rcf_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
186 const char *page
, size_t length
)
188 struct rcf_sysfs_entry
*entry
= to_rcf(attr
);
189 struct blk_cmd_filter
*filter
;
191 if (!capable(CAP_SYS_RAWIO
))
197 filter
= container_of(kobj
, struct blk_cmd_filter
, kobj
);
198 return entry
->store(filter
, page
, length
);
201 static struct sysfs_ops rcf_sysfs_ops
= {
202 .show
= rcf_attr_show
,
203 .store
= rcf_attr_store
,
206 static struct kobj_type rcf_ktype
= {
207 .sysfs_ops
= &rcf_sysfs_ops
,
208 .default_attrs
= default_attrs
,
211 int blk_register_filter(struct gendisk
*disk
)
214 struct blk_cmd_filter
*filter
= &disk
->queue
->cmd_filter
;
216 ret
= kobject_init_and_add(&filter
->kobj
, &rcf_ktype
,
217 &disk_to_dev(disk
)->kobj
,
224 EXPORT_SYMBOL(blk_register_filter
);
226 void blk_unregister_filter(struct gendisk
*disk
)
228 struct blk_cmd_filter
*filter
= &disk
->queue
->cmd_filter
;
230 kobject_put(&filter
->kobj
);
232 EXPORT_SYMBOL(blk_unregister_filter
);