Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 1 | /* |
| 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 Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 23 | #include <linux/capability.h> |
| 24 | #include <linux/bitops.h> |
| 25 | |
| 26 | #include <scsi/scsi.h> |
| 27 | #include <linux/cdrom.h> |
| 28 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 29 | int blk_verify_command(struct blk_cmd_filter *filter, |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 30 | unsigned char *cmd, int has_write_perm) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 31 | { |
| 32 | /* root can do any command. */ |
| 33 | if (capable(CAP_SYS_RAWIO)) |
| 34 | return 0; |
| 35 | |
| 36 | /* if there's no filter set, assume we're filtering everything out */ |
| 37 | if (!filter) |
| 38 | return -EPERM; |
| 39 | |
| 40 | /* Anybody who can open the device can do a read-safe command */ |
| 41 | if (test_bit(cmd[0], filter->read_ok)) |
| 42 | return 0; |
| 43 | |
| 44 | /* Write-safe commands require a writable open */ |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 45 | if (test_bit(cmd[0], filter->write_ok) && has_write_perm) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 46 | return 0; |
| 47 | |
| 48 | return -EPERM; |
| 49 | } |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(blk_verify_command); |
| 51 | |
Jens Axboe | 2dc75d3 | 2008-09-11 14:20:23 +0200 | [diff] [blame^] | 52 | #if 0 |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 53 | /* and now, the sysfs stuff */ |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 54 | static ssize_t rcf_cmds_show(struct blk_cmd_filter *filter, char *page, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 55 | int rw) |
| 56 | { |
| 57 | char *npage = page; |
| 58 | unsigned long *okbits; |
| 59 | int i; |
| 60 | |
| 61 | if (rw == READ) |
| 62 | okbits = filter->read_ok; |
| 63 | else |
| 64 | okbits = filter->write_ok; |
| 65 | |
| 66 | for (i = 0; i < BLK_SCSI_MAX_CMDS; i++) { |
| 67 | if (test_bit(i, okbits)) { |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 68 | npage += sprintf(npage, "0x%02x", i); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 69 | if (i < BLK_SCSI_MAX_CMDS - 1) |
| 70 | sprintf(npage++, " "); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (npage != page) |
| 75 | npage += sprintf(npage, "\n"); |
| 76 | |
| 77 | return npage - page; |
| 78 | } |
| 79 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 80 | static ssize_t rcf_readcmds_show(struct blk_cmd_filter *filter, char *page) |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 81 | { |
| 82 | return rcf_cmds_show(filter, page, READ); |
| 83 | } |
| 84 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 85 | static ssize_t rcf_writecmds_show(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 86 | char *page) |
| 87 | { |
| 88 | return rcf_cmds_show(filter, page, WRITE); |
| 89 | } |
| 90 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 91 | static ssize_t rcf_cmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 92 | const char *page, size_t count, int rw) |
| 93 | { |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 94 | unsigned long okbits[BLK_SCSI_CMD_PER_LONG], *target_okbits; |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 95 | int cmd, set; |
| 96 | char *p, *status; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 97 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 98 | if (rw == READ) { |
| 99 | memcpy(&okbits, filter->read_ok, sizeof(okbits)); |
| 100 | target_okbits = filter->read_ok; |
| 101 | } else { |
| 102 | memcpy(&okbits, filter->write_ok, sizeof(okbits)); |
| 103 | target_okbits = filter->write_ok; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 106 | while ((p = strsep((char **)&page, " ")) != NULL) { |
| 107 | set = 1; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 108 | |
Adel Gadllah | a4a7789 | 2008-08-16 14:21:06 +0900 | [diff] [blame] | 109 | if (p[0] == '+') { |
| 110 | p++; |
| 111 | } else if (p[0] == '-') { |
| 112 | set = 0; |
| 113 | p++; |
| 114 | } |
| 115 | |
| 116 | cmd = simple_strtol(p, &status, 16); |
| 117 | |
| 118 | /* either of these cases means invalid input, so do nothing. */ |
| 119 | if ((status == p) || cmd >= BLK_SCSI_MAX_CMDS) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | if (set) |
| 123 | __set_bit(cmd, okbits); |
| 124 | else |
| 125 | __clear_bit(cmd, okbits); |
| 126 | } |
| 127 | |
| 128 | memcpy(target_okbits, okbits, sizeof(okbits)); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 129 | return count; |
| 130 | } |
| 131 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 132 | static ssize_t rcf_readcmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 133 | const char *page, size_t count) |
| 134 | { |
| 135 | return rcf_cmds_store(filter, page, count, READ); |
| 136 | } |
| 137 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 138 | static ssize_t rcf_writecmds_store(struct blk_cmd_filter *filter, |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 139 | const char *page, size_t count) |
| 140 | { |
| 141 | return rcf_cmds_store(filter, page, count, WRITE); |
| 142 | } |
| 143 | |
| 144 | struct rcf_sysfs_entry { |
| 145 | struct attribute attr; |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 146 | ssize_t (*show)(struct blk_cmd_filter *, char *); |
| 147 | ssize_t (*store)(struct blk_cmd_filter *, const char *, size_t); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static struct rcf_sysfs_entry rcf_readcmds_entry = { |
| 151 | .attr = { .name = "read_table", .mode = S_IRUGO | S_IWUSR }, |
| 152 | .show = rcf_readcmds_show, |
| 153 | .store = rcf_readcmds_store, |
| 154 | }; |
| 155 | |
| 156 | static struct rcf_sysfs_entry rcf_writecmds_entry = { |
| 157 | .attr = {.name = "write_table", .mode = S_IRUGO | S_IWUSR }, |
| 158 | .show = rcf_writecmds_show, |
| 159 | .store = rcf_writecmds_store, |
| 160 | }; |
| 161 | |
| 162 | static struct attribute *default_attrs[] = { |
| 163 | &rcf_readcmds_entry.attr, |
| 164 | &rcf_writecmds_entry.attr, |
| 165 | NULL, |
| 166 | }; |
| 167 | |
| 168 | #define to_rcf(atr) container_of((atr), struct rcf_sysfs_entry, attr) |
| 169 | |
| 170 | static ssize_t |
| 171 | rcf_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 172 | { |
| 173 | struct rcf_sysfs_entry *entry = to_rcf(attr); |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 174 | struct blk_cmd_filter *filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 175 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 176 | filter = container_of(kobj, struct blk_cmd_filter, kobj); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 177 | if (entry->show) |
| 178 | return entry->show(filter, page); |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static ssize_t |
| 184 | rcf_attr_store(struct kobject *kobj, struct attribute *attr, |
| 185 | const char *page, size_t length) |
| 186 | { |
| 187 | struct rcf_sysfs_entry *entry = to_rcf(attr); |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 188 | struct blk_cmd_filter *filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 189 | |
| 190 | if (!capable(CAP_SYS_RAWIO)) |
| 191 | return -EPERM; |
| 192 | |
| 193 | if (!entry->store) |
| 194 | return -EINVAL; |
| 195 | |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 196 | filter = container_of(kobj, struct blk_cmd_filter, kobj); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 197 | return entry->store(filter, page, length); |
| 198 | } |
| 199 | |
| 200 | static struct sysfs_ops rcf_sysfs_ops = { |
| 201 | .show = rcf_attr_show, |
| 202 | .store = rcf_attr_store, |
| 203 | }; |
| 204 | |
| 205 | static struct kobj_type rcf_ktype = { |
| 206 | .sysfs_ops = &rcf_sysfs_ops, |
| 207 | .default_attrs = default_attrs, |
| 208 | }; |
| 209 | |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 210 | int blk_register_filter(struct gendisk *disk) |
| 211 | { |
| 212 | int ret; |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 213 | struct blk_cmd_filter *filter = &disk->queue->cmd_filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 214 | struct kobject *parent = kobject_get(disk->holder_dir->parent); |
| 215 | |
| 216 | if (!parent) |
| 217 | return -ENODEV; |
| 218 | |
| 219 | ret = kobject_init_and_add(&filter->kobj, &rcf_ktype, parent, |
FUJITA Tomonori | abf5439 | 2008-08-16 14:10:05 +0900 | [diff] [blame] | 220 | "%s", "cmd_filter"); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 221 | |
| 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 225 | return 0; |
| 226 | } |
FUJITA Tomonori | bb23b43 | 2008-08-29 11:47:07 +0200 | [diff] [blame] | 227 | EXPORT_SYMBOL(blk_register_filter); |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 228 | |
| 229 | void blk_unregister_filter(struct gendisk *disk) |
| 230 | { |
FUJITA Tomonori | 4beab5c | 2008-07-26 18:03:25 +0900 | [diff] [blame] | 231 | struct blk_cmd_filter *filter = &disk->queue->cmd_filter; |
Adel Gadllah | 0b07de8 | 2008-06-26 13:48:27 +0200 | [diff] [blame] | 232 | |
| 233 | kobject_put(&filter->kobj); |
| 234 | kobject_put(disk->holder_dir->parent); |
| 235 | } |
FUJITA Tomonori | bb23b43 | 2008-08-29 11:47:07 +0200 | [diff] [blame] | 236 | EXPORT_SYMBOL(blk_unregister_filter); |
Jens Axboe | 2dc75d3 | 2008-09-11 14:20:23 +0200 | [diff] [blame^] | 237 | #endif |