Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as 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 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/completion.h> |
| 23 | #include <linux/buffer_head.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 24 | #include <linux/kobject.h> |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 25 | #include <linux/bug.h> |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 26 | |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "transaction.h" |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 30 | #include "sysfs.h" |
| 31 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 32 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 33 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 34 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 35 | enum btrfs_feature_set set) |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 36 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 37 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 38 | if (set == FEAT_COMPAT) |
| 39 | return btrfs_super_compat_flags(disk_super); |
| 40 | else if (set == FEAT_COMPAT_RO) |
| 41 | return btrfs_super_compat_ro_flags(disk_super); |
| 42 | else |
| 43 | return btrfs_super_incompat_flags(disk_super); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame^] | 46 | static void set_features(struct btrfs_fs_info *fs_info, |
| 47 | enum btrfs_feature_set set, u64 features) |
| 48 | { |
| 49 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 50 | if (set == FEAT_COMPAT) |
| 51 | btrfs_set_super_compat_flags(disk_super, features); |
| 52 | else if (set == FEAT_COMPAT_RO) |
| 53 | btrfs_set_super_compat_ro_flags(disk_super, features); |
| 54 | else |
| 55 | btrfs_set_super_incompat_flags(disk_super, features); |
| 56 | } |
| 57 | |
| 58 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 59 | { |
| 60 | int val = 0; |
| 61 | u64 set, clear; |
| 62 | switch (fa->feature_set) { |
| 63 | case FEAT_COMPAT: |
| 64 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 65 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 66 | break; |
| 67 | case FEAT_COMPAT_RO: |
| 68 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 69 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 70 | break; |
| 71 | case FEAT_INCOMPAT: |
| 72 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 73 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 74 | break; |
| 75 | default: |
| 76 | BUG(); |
| 77 | } |
| 78 | |
| 79 | if (set & fa->feature_bit) |
| 80 | val |= 1; |
| 81 | if (clear & fa->feature_bit) |
| 82 | val |= 2; |
| 83 | |
| 84 | return val; |
| 85 | } |
| 86 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 87 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 88 | struct kobj_attribute *a, char *buf) |
| 89 | { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 90 | int val = 0; |
| 91 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame^] | 92 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 93 | if (fs_info) { |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 94 | u64 features = get_features(fs_info, fa->feature_set); |
| 95 | if (features & fa->feature_bit) |
| 96 | val = 1; |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame^] | 97 | } else |
| 98 | val = can_modify_feature(fa); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 99 | |
| 100 | return snprintf(buf, PAGE_SIZE, "%d\n", val); |
| 101 | } |
| 102 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame^] | 103 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 104 | struct kobj_attribute *a, |
| 105 | const char *buf, size_t count) |
| 106 | { |
| 107 | struct btrfs_fs_info *fs_info; |
| 108 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
| 109 | struct btrfs_trans_handle *trans; |
| 110 | u64 features, set, clear; |
| 111 | unsigned long val; |
| 112 | int ret; |
| 113 | |
| 114 | fs_info = to_fs_info(kobj); |
| 115 | if (!fs_info) |
| 116 | return -EPERM; |
| 117 | |
| 118 | ret = kstrtoul(skip_spaces(buf), 0, &val); |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | |
| 122 | if (fa->feature_set == FEAT_COMPAT) { |
| 123 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 124 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 125 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 126 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 127 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 128 | } else { |
| 129 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 130 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 131 | } |
| 132 | |
| 133 | features = get_features(fs_info, fa->feature_set); |
| 134 | |
| 135 | /* Nothing to do */ |
| 136 | if ((val && (features & fa->feature_bit)) || |
| 137 | (!val && !(features & fa->feature_bit))) |
| 138 | return count; |
| 139 | |
| 140 | if ((val && !(set & fa->feature_bit)) || |
| 141 | (!val && !(clear & fa->feature_bit))) { |
| 142 | btrfs_info(fs_info, |
| 143 | "%sabling feature %s on mounted fs is not supported.", |
| 144 | val ? "En" : "Dis", fa->kobj_attr.attr.name); |
| 145 | return -EPERM; |
| 146 | } |
| 147 | |
| 148 | btrfs_info(fs_info, "%s %s feature flag", |
| 149 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); |
| 150 | |
| 151 | trans = btrfs_start_transaction(fs_info->fs_root, 1); |
| 152 | if (IS_ERR(trans)) |
| 153 | return PTR_ERR(trans); |
| 154 | |
| 155 | spin_lock(&fs_info->super_lock); |
| 156 | features = get_features(fs_info, fa->feature_set); |
| 157 | if (val) |
| 158 | features |= fa->feature_bit; |
| 159 | else |
| 160 | features &= ~fa->feature_bit; |
| 161 | set_features(fs_info, fa->feature_set, features); |
| 162 | spin_unlock(&fs_info->super_lock); |
| 163 | |
| 164 | ret = btrfs_commit_transaction(trans, fs_info->fs_root); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | return count; |
| 169 | } |
| 170 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 171 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 172 | struct attribute *attr, int unused) |
| 173 | { |
| 174 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 175 | umode_t mode = attr->mode; |
| 176 | |
| 177 | if (fs_info) { |
| 178 | struct btrfs_feature_attr *fa; |
| 179 | u64 features; |
| 180 | |
| 181 | fa = attr_to_btrfs_feature_attr(attr); |
| 182 | features = get_features(fs_info, fa->feature_set); |
| 183 | |
Jeff Mahoney | ba63194 | 2013-11-01 13:07:01 -0400 | [diff] [blame^] | 184 | if (can_modify_feature(fa)) |
| 185 | mode |= S_IWUSR; |
| 186 | else if (!(features & fa->feature_bit)) |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 187 | mode = 0; |
| 188 | } |
| 189 | |
| 190 | return mode; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); |
| 194 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 195 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 196 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
| 197 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2); |
| 198 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); |
| 199 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 200 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 201 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
| 202 | |
| 203 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 204 | BTRFS_FEAT_ATTR_PTR(mixed_backref), |
| 205 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 206 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 207 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
| 208 | BTRFS_FEAT_ATTR_PTR(compress_lzov2), |
| 209 | BTRFS_FEAT_ATTR_PTR(big_metadata), |
| 210 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 211 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 212 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
| 213 | NULL |
| 214 | }; |
| 215 | |
| 216 | static const struct attribute_group btrfs_feature_attr_group = { |
| 217 | .name = "features", |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 218 | .is_visible = btrfs_feature_visible, |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 219 | .attrs = btrfs_supported_feature_attrs, |
| 220 | }; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 221 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 222 | static void btrfs_release_super_kobj(struct kobject *kobj) |
| 223 | { |
| 224 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 225 | complete(&fs_info->kobj_unregister); |
| 226 | } |
| 227 | |
| 228 | static struct kobj_type btrfs_ktype = { |
| 229 | .sysfs_ops = &kobj_sysfs_ops, |
| 230 | .release = btrfs_release_super_kobj, |
| 231 | }; |
| 232 | |
| 233 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 234 | { |
| 235 | if (kobj->ktype != &btrfs_ktype) |
| 236 | return NULL; |
| 237 | return container_of(kobj, struct btrfs_fs_info, super_kobj); |
| 238 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 239 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 240 | void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info) |
| 241 | { |
| 242 | kobject_del(&fs_info->super_kobj); |
| 243 | kobject_put(&fs_info->super_kobj); |
| 244 | wait_for_completion(&fs_info->kobj_unregister); |
| 245 | } |
| 246 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 247 | const char * const btrfs_feature_set_names[3] = { |
| 248 | [FEAT_COMPAT] = "compat", |
| 249 | [FEAT_COMPAT_RO] = "compat_ro", |
| 250 | [FEAT_INCOMPAT] = "incompat", |
| 251 | }; |
| 252 | |
| 253 | #define NUM_FEATURE_BITS 64 |
| 254 | static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13]; |
| 255 | static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS]; |
| 256 | |
| 257 | static void init_feature_attrs(void) |
| 258 | { |
| 259 | struct btrfs_feature_attr *fa; |
| 260 | int set, i; |
| 261 | |
| 262 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != |
| 263 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 264 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != |
| 265 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 266 | |
| 267 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 268 | struct btrfs_feature_attr *sfa; |
| 269 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
| 270 | sfa = attr_to_btrfs_feature_attr(a); |
| 271 | fa = &btrfs_feature_attrs[sfa->feature_set][sfa->feature_bit]; |
| 272 | |
| 273 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 274 | } |
| 275 | |
| 276 | for (set = 0; set < FEAT_MAX; set++) { |
| 277 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 278 | char *name = btrfs_unknown_feature_names[set][i]; |
| 279 | fa = &btrfs_feature_attrs[set][i]; |
| 280 | |
| 281 | if (fa->kobj_attr.attr.name) |
| 282 | continue; |
| 283 | |
| 284 | snprintf(name, 13, "%s:%u", |
| 285 | btrfs_feature_set_names[set], i); |
| 286 | |
| 287 | fa->kobj_attr.attr.name = name; |
| 288 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 289 | fa->feature_set = set; |
| 290 | fa->feature_bit = 1ULL << i; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static u64 supported_feature_masks[3] = { |
| 296 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 297 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 298 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 299 | }; |
| 300 | |
| 301 | static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info) |
| 302 | { |
| 303 | int set; |
| 304 | |
| 305 | for (set = 0; set < FEAT_MAX; set++) { |
| 306 | int i, count, ret, index = 0; |
| 307 | struct attribute **attrs; |
| 308 | struct attribute_group agroup = { |
| 309 | .name = "features", |
| 310 | }; |
| 311 | u64 features = get_features(fs_info, set); |
| 312 | features &= ~supported_feature_masks[set]; |
| 313 | |
| 314 | count = hweight64(features); |
| 315 | |
| 316 | if (!count) |
| 317 | continue; |
| 318 | |
| 319 | attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL); |
| 320 | |
| 321 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 322 | struct btrfs_feature_attr *fa; |
| 323 | |
| 324 | if (!(features & (1ULL << i))) |
| 325 | continue; |
| 326 | |
| 327 | fa = &btrfs_feature_attrs[set][i]; |
| 328 | attrs[index++] = &fa->kobj_attr.attr; |
| 329 | } |
| 330 | |
| 331 | attrs[index] = NULL; |
| 332 | agroup.attrs = attrs; |
| 333 | |
| 334 | ret = sysfs_merge_group(&fs_info->super_kobj, &agroup); |
| 335 | kfree(attrs); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | } |
| 339 | return 0; |
| 340 | } |
| 341 | |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 342 | /* /sys/fs/btrfs/ entry */ |
| 343 | static struct kset *btrfs_kset; |
| 344 | |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 345 | int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info) |
| 346 | { |
| 347 | int error; |
| 348 | |
| 349 | init_completion(&fs_info->kobj_unregister); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 350 | fs_info->super_kobj.kset = btrfs_kset; |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 351 | error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL, |
| 352 | "%pU", fs_info->fsid); |
Jeff Mahoney | 510d736 | 2013-11-01 13:06:59 -0400 | [diff] [blame] | 353 | |
| 354 | error = sysfs_create_group(&fs_info->super_kobj, |
| 355 | &btrfs_feature_attr_group); |
| 356 | if (error) |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 357 | goto failure; |
| 358 | |
| 359 | error = add_unknown_feature_attrs(fs_info); |
| 360 | if (error) |
| 361 | goto failure; |
| 362 | |
| 363 | return 0; |
| 364 | failure: |
| 365 | btrfs_sysfs_remove_one(fs_info); |
Jeff Mahoney | 5ac1d20 | 2013-11-01 13:06:58 -0400 | [diff] [blame] | 366 | return error; |
| 367 | } |
| 368 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 369 | int btrfs_init_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 370 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 371 | int ret; |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 372 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
| 373 | if (!btrfs_kset) |
| 374 | return -ENOMEM; |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 375 | |
Jeff Mahoney | 79da4fa | 2013-11-01 13:07:00 -0400 | [diff] [blame] | 376 | init_feature_attrs(); |
| 377 | |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 378 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
| 379 | if (ret) { |
| 380 | kset_unregister(btrfs_kset); |
| 381 | return ret; |
| 382 | } |
| 383 | |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 384 | return 0; |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 385 | } |
| 386 | |
Christoph Hellwig | b214107 | 2008-09-05 16:43:31 -0400 | [diff] [blame] | 387 | void btrfs_exit_sysfs(void) |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 388 | { |
Jeff Mahoney | 079b72b | 2013-11-01 13:06:57 -0400 | [diff] [blame] | 389 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
Greg KH | e3fe4e7 | 2008-02-20 14:14:16 -0500 | [diff] [blame] | 390 | kset_unregister(btrfs_kset); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 391 | } |
Chris Mason | 55d4741 | 2008-02-20 16:02:51 -0500 | [diff] [blame] | 392 | |