| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <linux/sched.h> |
| 7 | #include <linux/sched/mm.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/completion.h> |
| 11 | #include <linux/bug.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/string_choices.h> |
| 14 | #include <crypto/hash.h> |
| 15 | #include "messages.h" |
| 16 | #include "ctree.h" |
| 17 | #include "discard.h" |
| 18 | #include "disk-io.h" |
| 19 | #include "send.h" |
| 20 | #include "transaction.h" |
| 21 | #include "sysfs.h" |
| 22 | #include "volumes.h" |
| 23 | #include "space-info.h" |
| 24 | #include "block-group.h" |
| 25 | #include "qgroup.h" |
| 26 | #include "misc.h" |
| 27 | #include "fs.h" |
| 28 | #include "accessors.h" |
| 29 | |
| 30 | /* |
| 31 | * Structure name Path |
| 32 | * -------------------------------------------------------------------------- |
| 33 | * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features |
| 34 | * btrfs_supported_feature_attrs /sys/fs/btrfs/features and |
| 35 | * /sys/fs/btrfs/<uuid>/features |
| 36 | * btrfs_attrs /sys/fs/btrfs/<uuid> |
| 37 | * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid> |
| 38 | * allocation_attrs /sys/fs/btrfs/<uuid>/allocation |
| 39 | * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid> |
| 40 | * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type> |
| 41 | * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile> |
| 42 | * discard_attrs /sys/fs/btrfs/<uuid>/discard |
| 43 | * |
| 44 | * When built with BTRFS_CONFIG_DEBUG: |
| 45 | * |
| 46 | * btrfs_debug_feature_attrs /sys/fs/btrfs/debug |
| 47 | * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug |
| 48 | */ |
| 49 | |
| 50 | struct btrfs_feature_attr { |
| 51 | struct kobj_attribute kobj_attr; |
| 52 | enum btrfs_feature_set feature_set; |
| 53 | u64 feature_bit; |
| 54 | }; |
| 55 | |
| 56 | /* For raid type sysfs entries */ |
| 57 | struct raid_kobject { |
| 58 | u64 flags; |
| 59 | struct kobject kobj; |
| 60 | }; |
| 61 | |
| 62 | #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ |
| 63 | { \ |
| 64 | .attr = { .name = __stringify(_name), .mode = _mode }, \ |
| 65 | .show = _show, \ |
| 66 | .store = _store, \ |
| 67 | } |
| 68 | |
| 69 | #define BTRFS_ATTR_W(_prefix, _name, _store) \ |
| 70 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 71 | __INIT_KOBJ_ATTR(_name, 0200, NULL, _store) |
| 72 | |
| 73 | #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \ |
| 74 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 75 | __INIT_KOBJ_ATTR(_name, 0644, _show, _store) |
| 76 | |
| 77 | #define BTRFS_ATTR(_prefix, _name, _show) \ |
| 78 | static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \ |
| 79 | __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) |
| 80 | |
| 81 | #define BTRFS_ATTR_PTR(_prefix, _name) \ |
| 82 | (&btrfs_attr_##_prefix##_##_name.attr) |
| 83 | |
| 84 | #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \ |
| 85 | static struct btrfs_feature_attr btrfs_attr_features_##_name = { \ |
| 86 | .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \ |
| 87 | btrfs_feature_attr_show, \ |
| 88 | btrfs_feature_attr_store), \ |
| 89 | .feature_set = _feature_set, \ |
| 90 | .feature_bit = _feature_prefix ##_## _feature_bit, \ |
| 91 | } |
| 92 | #define BTRFS_FEAT_ATTR_PTR(_name) \ |
| 93 | (&btrfs_attr_features_##_name.kobj_attr.attr) |
| 94 | |
| 95 | #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \ |
| 96 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature) |
| 97 | #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \ |
| 98 | BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature) |
| 99 | #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \ |
| 100 | BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature) |
| 101 | |
| 102 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
| 103 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj); |
| 104 | static struct kobject *get_btrfs_kobj(struct kobject *kobj); |
| 105 | |
| 106 | static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a) |
| 107 | { |
| 108 | return container_of(a, struct btrfs_feature_attr, kobj_attr); |
| 109 | } |
| 110 | |
| 111 | static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr) |
| 112 | { |
| 113 | return container_of(attr, struct kobj_attribute, attr); |
| 114 | } |
| 115 | |
| 116 | static struct btrfs_feature_attr *attr_to_btrfs_feature_attr( |
| 117 | struct attribute *attr) |
| 118 | { |
| 119 | return to_btrfs_feature_attr(a: attr_to_btrfs_attr(attr)); |
| 120 | } |
| 121 | |
| 122 | static u64 get_features(struct btrfs_fs_info *fs_info, |
| 123 | enum btrfs_feature_set set) |
| 124 | { |
| 125 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 126 | if (set == FEAT_COMPAT) |
| 127 | return btrfs_super_compat_flags(s: disk_super); |
| 128 | else if (set == FEAT_COMPAT_RO) |
| 129 | return btrfs_super_compat_ro_flags(s: disk_super); |
| 130 | else |
| 131 | return btrfs_super_incompat_flags(s: disk_super); |
| 132 | } |
| 133 | |
| 134 | static void set_features(struct btrfs_fs_info *fs_info, |
| 135 | enum btrfs_feature_set set, u64 features) |
| 136 | { |
| 137 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
| 138 | if (set == FEAT_COMPAT) |
| 139 | btrfs_set_super_compat_flags(s: disk_super, val: features); |
| 140 | else if (set == FEAT_COMPAT_RO) |
| 141 | btrfs_set_super_compat_ro_flags(s: disk_super, val: features); |
| 142 | else |
| 143 | btrfs_set_super_incompat_flags(s: disk_super, val: features); |
| 144 | } |
| 145 | |
| 146 | static int can_modify_feature(struct btrfs_feature_attr *fa) |
| 147 | { |
| 148 | int val = 0; |
| 149 | u64 set, clear; |
| 150 | switch (fa->feature_set) { |
| 151 | case FEAT_COMPAT: |
| 152 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 153 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 154 | break; |
| 155 | case FEAT_COMPAT_RO: |
| 156 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 157 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 158 | break; |
| 159 | case FEAT_INCOMPAT: |
| 160 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 161 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 162 | break; |
| 163 | default: |
| 164 | btrfs_warn(NULL, "sysfs: unknown feature set %d" , fa->feature_set); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | if (set & fa->feature_bit) |
| 169 | val |= 1; |
| 170 | if (clear & fa->feature_bit) |
| 171 | val |= 2; |
| 172 | |
| 173 | return val; |
| 174 | } |
| 175 | |
| 176 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
| 177 | struct kobj_attribute *a, char *buf) |
| 178 | { |
| 179 | int val = 0; |
| 180 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 181 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
| 182 | if (fs_info) { |
| 183 | u64 features = get_features(fs_info, set: fa->feature_set); |
| 184 | if (features & fa->feature_bit) |
| 185 | val = 1; |
| 186 | } else |
| 187 | val = can_modify_feature(fa); |
| 188 | |
| 189 | return sysfs_emit(buf, fmt: "%d\n" , val); |
| 190 | } |
| 191 | |
| 192 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
| 193 | struct kobj_attribute *a, |
| 194 | const char *buf, size_t count) |
| 195 | { |
| 196 | struct btrfs_fs_info *fs_info; |
| 197 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
| 198 | u64 features, set, clear; |
| 199 | unsigned long val; |
| 200 | int ret; |
| 201 | |
| 202 | fs_info = to_fs_info(kobj); |
| 203 | if (!fs_info) |
| 204 | return -EPERM; |
| 205 | |
| 206 | if (sb_rdonly(sb: fs_info->sb)) |
| 207 | return -EROFS; |
| 208 | |
| 209 | ret = kstrtoul(s: skip_spaces(buf), base: 0, res: &val); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | if (fa->feature_set == FEAT_COMPAT) { |
| 214 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; |
| 215 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; |
| 216 | } else if (fa->feature_set == FEAT_COMPAT_RO) { |
| 217 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; |
| 218 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; |
| 219 | } else { |
| 220 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; |
| 221 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; |
| 222 | } |
| 223 | |
| 224 | features = get_features(fs_info, set: fa->feature_set); |
| 225 | |
| 226 | /* Nothing to do */ |
| 227 | if ((val && (features & fa->feature_bit)) || |
| 228 | (!val && !(features & fa->feature_bit))) |
| 229 | return count; |
| 230 | |
| 231 | if ((val && !(set & fa->feature_bit)) || |
| 232 | (!val && !(clear & fa->feature_bit))) { |
| 233 | btrfs_info(fs_info, |
| 234 | "%sabling feature %s on mounted fs is not supported." , |
| 235 | val ? "En" : "Dis" , fa->kobj_attr.attr.name); |
| 236 | return -EPERM; |
| 237 | } |
| 238 | |
| 239 | btrfs_info(fs_info, "%s %s feature flag" , |
| 240 | val ? "Setting" : "Clearing" , fa->kobj_attr.attr.name); |
| 241 | |
| 242 | spin_lock(lock: &fs_info->super_lock); |
| 243 | features = get_features(fs_info, set: fa->feature_set); |
| 244 | if (val) |
| 245 | features |= fa->feature_bit; |
| 246 | else |
| 247 | features &= ~fa->feature_bit; |
| 248 | set_features(fs_info, set: fa->feature_set, features); |
| 249 | spin_unlock(lock: &fs_info->super_lock); |
| 250 | |
| 251 | /* |
| 252 | * We don't want to do full transaction commit from inside sysfs |
| 253 | */ |
| 254 | set_bit(nr: BTRFS_FS_NEED_TRANS_COMMIT, addr: &fs_info->flags); |
| 255 | wake_up_process(tsk: fs_info->transaction_kthread); |
| 256 | |
| 257 | return count; |
| 258 | } |
| 259 | |
| 260 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
| 261 | struct attribute *attr, int unused) |
| 262 | { |
| 263 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 264 | umode_t mode = attr->mode; |
| 265 | |
| 266 | if (fs_info) { |
| 267 | struct btrfs_feature_attr *fa; |
| 268 | u64 features; |
| 269 | |
| 270 | fa = attr_to_btrfs_feature_attr(attr); |
| 271 | features = get_features(fs_info, set: fa->feature_set); |
| 272 | |
| 273 | if (can_modify_feature(fa)) |
| 274 | mode |= S_IWUSR; |
| 275 | else if (!(features & fa->feature_bit)) |
| 276 | mode = 0; |
| 277 | } |
| 278 | |
| 279 | return mode; |
| 280 | } |
| 281 | |
| 282 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); |
| 283 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); |
| 284 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); |
| 285 | BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD); |
| 286 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); |
| 287 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); |
| 288 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); |
| 289 | BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES); |
| 290 | BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID); |
| 291 | BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE); |
| 292 | BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE); |
| 293 | BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34); |
| 294 | BTRFS_FEAT_ATTR_INCOMPAT(simple_quota, SIMPLE_QUOTA); |
| 295 | #ifdef CONFIG_BLK_DEV_ZONED |
| 296 | BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED); |
| 297 | #endif |
| 298 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 299 | /* Remove once support for extent tree v2 is feature complete */ |
| 300 | BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2); |
| 301 | /* Remove once support for raid stripe tree is feature complete. */ |
| 302 | BTRFS_FEAT_ATTR_INCOMPAT(raid_stripe_tree, RAID_STRIPE_TREE); |
| 303 | #endif |
| 304 | #ifdef CONFIG_FS_VERITY |
| 305 | BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY); |
| 306 | #endif |
| 307 | |
| 308 | /* |
| 309 | * Features which depend on feature bits and may differ between each fs. |
| 310 | * |
| 311 | * /sys/fs/btrfs/features - all available features implemented by this version |
| 312 | * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or |
| 313 | * can be changed on a mounted filesystem. |
| 314 | */ |
| 315 | static struct attribute *btrfs_supported_feature_attrs[] = { |
| 316 | BTRFS_FEAT_ATTR_PTR(default_subvol), |
| 317 | BTRFS_FEAT_ATTR_PTR(mixed_groups), |
| 318 | BTRFS_FEAT_ATTR_PTR(compress_lzo), |
| 319 | BTRFS_FEAT_ATTR_PTR(compress_zstd), |
| 320 | BTRFS_FEAT_ATTR_PTR(extended_iref), |
| 321 | BTRFS_FEAT_ATTR_PTR(raid56), |
| 322 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), |
| 323 | BTRFS_FEAT_ATTR_PTR(no_holes), |
| 324 | BTRFS_FEAT_ATTR_PTR(metadata_uuid), |
| 325 | BTRFS_FEAT_ATTR_PTR(free_space_tree), |
| 326 | BTRFS_FEAT_ATTR_PTR(raid1c34), |
| 327 | BTRFS_FEAT_ATTR_PTR(block_group_tree), |
| 328 | BTRFS_FEAT_ATTR_PTR(simple_quota), |
| 329 | #ifdef CONFIG_BLK_DEV_ZONED |
| 330 | BTRFS_FEAT_ATTR_PTR(zoned), |
| 331 | #endif |
| 332 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 333 | BTRFS_FEAT_ATTR_PTR(extent_tree_v2), |
| 334 | BTRFS_FEAT_ATTR_PTR(raid_stripe_tree), |
| 335 | #endif |
| 336 | #ifdef CONFIG_FS_VERITY |
| 337 | BTRFS_FEAT_ATTR_PTR(verity), |
| 338 | #endif |
| 339 | NULL |
| 340 | }; |
| 341 | |
| 342 | static const struct attribute_group btrfs_feature_attr_group = { |
| 343 | .name = "features" , |
| 344 | .is_visible = btrfs_feature_visible, |
| 345 | .attrs = btrfs_supported_feature_attrs, |
| 346 | }; |
| 347 | |
| 348 | static ssize_t rmdir_subvol_show(struct kobject *kobj, |
| 349 | struct kobj_attribute *ka, char *buf) |
| 350 | { |
| 351 | return sysfs_emit(buf, fmt: "0\n" ); |
| 352 | } |
| 353 | BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show); |
| 354 | |
| 355 | static ssize_t supported_checksums_show(struct kobject *kobj, |
| 356 | struct kobj_attribute *a, char *buf) |
| 357 | { |
| 358 | ssize_t ret = 0; |
| 359 | int i; |
| 360 | |
| 361 | for (i = 0; i < btrfs_get_num_csums(); i++) { |
| 362 | /* |
| 363 | * This "trick" only works as long as 'enum btrfs_csum_type' has |
| 364 | * no holes in it |
| 365 | */ |
| 366 | ret += sysfs_emit_at(buf, at: ret, fmt: "%s%s" , (i == 0 ? "" : " " ), |
| 367 | btrfs_super_csum_name(csum_type: i)); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | ret += sysfs_emit_at(buf, at: ret, fmt: "\n" ); |
| 372 | return ret; |
| 373 | } |
| 374 | BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show); |
| 375 | |
| 376 | static ssize_t send_stream_version_show(struct kobject *kobj, |
| 377 | struct kobj_attribute *ka, char *buf) |
| 378 | { |
| 379 | return sysfs_emit(buf, fmt: "%d\n" , BTRFS_SEND_STREAM_VERSION); |
| 380 | } |
| 381 | BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show); |
| 382 | |
| 383 | static const char *rescue_opts[] = { |
| 384 | "usebackuproot" , |
| 385 | "nologreplay" , |
| 386 | "ignorebadroots" , |
| 387 | "ignoredatacsums" , |
| 388 | "ignoremetacsums" , |
| 389 | "ignoresuperflags" , |
| 390 | "all" , |
| 391 | }; |
| 392 | |
| 393 | static ssize_t supported_rescue_options_show(struct kobject *kobj, |
| 394 | struct kobj_attribute *a, |
| 395 | char *buf) |
| 396 | { |
| 397 | ssize_t ret = 0; |
| 398 | int i; |
| 399 | |
| 400 | for (i = 0; i < ARRAY_SIZE(rescue_opts); i++) |
| 401 | ret += sysfs_emit_at(buf, at: ret, fmt: "%s%s" , (i ? " " : "" ), rescue_opts[i]); |
| 402 | ret += sysfs_emit_at(buf, at: ret, fmt: "\n" ); |
| 403 | return ret; |
| 404 | } |
| 405 | BTRFS_ATTR(static_feature, supported_rescue_options, |
| 406 | supported_rescue_options_show); |
| 407 | |
| 408 | static ssize_t supported_sectorsizes_show(struct kobject *kobj, |
| 409 | struct kobj_attribute *a, |
| 410 | char *buf) |
| 411 | { |
| 412 | ssize_t ret = 0; |
| 413 | bool has_output = false; |
| 414 | |
| 415 | for (u32 cur = BTRFS_MIN_BLOCKSIZE; cur <= BTRFS_MAX_BLOCKSIZE; cur *= 2) { |
| 416 | if (!btrfs_supported_blocksize(blocksize: cur)) |
| 417 | continue; |
| 418 | if (has_output) |
| 419 | ret += sysfs_emit_at(buf, at: ret, fmt: " " ); |
| 420 | ret += sysfs_emit_at(buf, at: ret, fmt: "%u" , cur); |
| 421 | has_output = true; |
| 422 | } |
| 423 | ret += sysfs_emit_at(buf, at: ret, fmt: "\n" ); |
| 424 | return ret; |
| 425 | } |
| 426 | BTRFS_ATTR(static_feature, supported_sectorsizes, |
| 427 | supported_sectorsizes_show); |
| 428 | |
| 429 | static ssize_t acl_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) |
| 430 | { |
| 431 | return sysfs_emit(buf, fmt: "%d\n" , IS_ENABLED(CONFIG_BTRFS_FS_POSIX_ACL)); |
| 432 | } |
| 433 | BTRFS_ATTR(static_feature, acl, acl_show); |
| 434 | |
| 435 | static ssize_t temp_fsid_supported_show(struct kobject *kobj, |
| 436 | struct kobj_attribute *a, char *buf) |
| 437 | { |
| 438 | return sysfs_emit(buf, fmt: "0\n" ); |
| 439 | } |
| 440 | BTRFS_ATTR(static_feature, temp_fsid, temp_fsid_supported_show); |
| 441 | |
| 442 | /* |
| 443 | * Features which only depend on kernel version. |
| 444 | * |
| 445 | * These are listed in /sys/fs/btrfs/features along with |
| 446 | * btrfs_supported_feature_attrs. |
| 447 | */ |
| 448 | static struct attribute *btrfs_supported_static_feature_attrs[] = { |
| 449 | BTRFS_ATTR_PTR(static_feature, acl), |
| 450 | BTRFS_ATTR_PTR(static_feature, rmdir_subvol), |
| 451 | BTRFS_ATTR_PTR(static_feature, supported_checksums), |
| 452 | BTRFS_ATTR_PTR(static_feature, send_stream_version), |
| 453 | BTRFS_ATTR_PTR(static_feature, supported_rescue_options), |
| 454 | BTRFS_ATTR_PTR(static_feature, supported_sectorsizes), |
| 455 | BTRFS_ATTR_PTR(static_feature, temp_fsid), |
| 456 | NULL |
| 457 | }; |
| 458 | |
| 459 | static const struct attribute_group btrfs_static_feature_attr_group = { |
| 460 | .name = "features" , |
| 461 | .attrs = btrfs_supported_static_feature_attrs, |
| 462 | }; |
| 463 | |
| 464 | /* |
| 465 | * Discard statistics and tunables |
| 466 | */ |
| 467 | #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj)) |
| 468 | |
| 469 | static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj, |
| 470 | struct kobj_attribute *a, |
| 471 | char *buf) |
| 472 | { |
| 473 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 474 | |
| 475 | return sysfs_emit(buf, fmt: "%lld\n" , |
| 476 | atomic64_read(v: &fs_info->discard_ctl.discardable_bytes)); |
| 477 | } |
| 478 | BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show); |
| 479 | |
| 480 | static ssize_t btrfs_discardable_extents_show(struct kobject *kobj, |
| 481 | struct kobj_attribute *a, |
| 482 | char *buf) |
| 483 | { |
| 484 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 485 | |
| 486 | return sysfs_emit(buf, fmt: "%d\n" , |
| 487 | atomic_read(v: &fs_info->discard_ctl.discardable_extents)); |
| 488 | } |
| 489 | BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show); |
| 490 | |
| 491 | static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj, |
| 492 | struct kobj_attribute *a, |
| 493 | char *buf) |
| 494 | { |
| 495 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 496 | |
| 497 | return sysfs_emit(buf, fmt: "%llu\n" , |
| 498 | fs_info->discard_ctl.discard_bitmap_bytes); |
| 499 | } |
| 500 | BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show); |
| 501 | |
| 502 | static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj, |
| 503 | struct kobj_attribute *a, |
| 504 | char *buf) |
| 505 | { |
| 506 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 507 | |
| 508 | return sysfs_emit(buf, fmt: "%lld\n" , |
| 509 | atomic64_read(v: &fs_info->discard_ctl.discard_bytes_saved)); |
| 510 | } |
| 511 | BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show); |
| 512 | |
| 513 | static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj, |
| 514 | struct kobj_attribute *a, |
| 515 | char *buf) |
| 516 | { |
| 517 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 518 | |
| 519 | return sysfs_emit(buf, fmt: "%llu\n" , |
| 520 | fs_info->discard_ctl.discard_extent_bytes); |
| 521 | } |
| 522 | BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show); |
| 523 | |
| 524 | static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj, |
| 525 | struct kobj_attribute *a, |
| 526 | char *buf) |
| 527 | { |
| 528 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 529 | |
| 530 | return sysfs_emit(buf, fmt: "%u\n" , |
| 531 | READ_ONCE(fs_info->discard_ctl.iops_limit)); |
| 532 | } |
| 533 | |
| 534 | static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj, |
| 535 | struct kobj_attribute *a, |
| 536 | const char *buf, size_t len) |
| 537 | { |
| 538 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 539 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 540 | u32 iops_limit; |
| 541 | int ret; |
| 542 | |
| 543 | ret = kstrtou32(s: buf, base: 10, res: &iops_limit); |
| 544 | if (ret) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | WRITE_ONCE(discard_ctl->iops_limit, iops_limit); |
| 548 | btrfs_discard_calc_delay(discard_ctl); |
| 549 | btrfs_discard_schedule_work(discard_ctl, override: true); |
| 550 | return len; |
| 551 | } |
| 552 | BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show, |
| 553 | btrfs_discard_iops_limit_store); |
| 554 | |
| 555 | static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj, |
| 556 | struct kobj_attribute *a, |
| 557 | char *buf) |
| 558 | { |
| 559 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 560 | |
| 561 | return sysfs_emit(buf, fmt: "%u\n" , |
| 562 | READ_ONCE(fs_info->discard_ctl.kbps_limit)); |
| 563 | } |
| 564 | |
| 565 | static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj, |
| 566 | struct kobj_attribute *a, |
| 567 | const char *buf, size_t len) |
| 568 | { |
| 569 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 570 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 571 | u32 kbps_limit; |
| 572 | int ret; |
| 573 | |
| 574 | ret = kstrtou32(s: buf, base: 10, res: &kbps_limit); |
| 575 | if (ret) |
| 576 | return -EINVAL; |
| 577 | |
| 578 | WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit); |
| 579 | btrfs_discard_schedule_work(discard_ctl, override: true); |
| 580 | return len; |
| 581 | } |
| 582 | BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show, |
| 583 | btrfs_discard_kbps_limit_store); |
| 584 | |
| 585 | static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj, |
| 586 | struct kobj_attribute *a, |
| 587 | char *buf) |
| 588 | { |
| 589 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 590 | |
| 591 | return sysfs_emit(buf, fmt: "%llu\n" , |
| 592 | READ_ONCE(fs_info->discard_ctl.max_discard_size)); |
| 593 | } |
| 594 | |
| 595 | static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj, |
| 596 | struct kobj_attribute *a, |
| 597 | const char *buf, size_t len) |
| 598 | { |
| 599 | struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj); |
| 600 | struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl; |
| 601 | u64 max_discard_size; |
| 602 | int ret; |
| 603 | |
| 604 | ret = kstrtou64(s: buf, base: 10, res: &max_discard_size); |
| 605 | if (ret) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size); |
| 609 | |
| 610 | return len; |
| 611 | } |
| 612 | BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show, |
| 613 | btrfs_discard_max_discard_size_store); |
| 614 | |
| 615 | /* |
| 616 | * Per-filesystem stats for discard (when mounted with discard=async). |
| 617 | * |
| 618 | * Path: /sys/fs/btrfs/<uuid>/discard/ |
| 619 | */ |
| 620 | static const struct attribute *discard_attrs[] = { |
| 621 | BTRFS_ATTR_PTR(discard, discardable_bytes), |
| 622 | BTRFS_ATTR_PTR(discard, discardable_extents), |
| 623 | BTRFS_ATTR_PTR(discard, discard_bitmap_bytes), |
| 624 | BTRFS_ATTR_PTR(discard, discard_bytes_saved), |
| 625 | BTRFS_ATTR_PTR(discard, discard_extent_bytes), |
| 626 | BTRFS_ATTR_PTR(discard, iops_limit), |
| 627 | BTRFS_ATTR_PTR(discard, kbps_limit), |
| 628 | BTRFS_ATTR_PTR(discard, max_discard_size), |
| 629 | NULL, |
| 630 | }; |
| 631 | |
| 632 | #ifdef CONFIG_BTRFS_DEBUG |
| 633 | |
| 634 | /* |
| 635 | * Per-filesystem runtime debugging exported via sysfs. |
| 636 | * |
| 637 | * Path: /sys/fs/btrfs/UUID/debug/ |
| 638 | */ |
| 639 | static const struct attribute *btrfs_debug_mount_attrs[] = { |
| 640 | NULL, |
| 641 | }; |
| 642 | |
| 643 | /* |
| 644 | * Runtime debugging exported via sysfs, applies to all mounted filesystems. |
| 645 | * |
| 646 | * Path: /sys/fs/btrfs/debug |
| 647 | */ |
| 648 | static struct attribute *btrfs_debug_feature_attrs[] = { |
| 649 | NULL |
| 650 | }; |
| 651 | |
| 652 | static const struct attribute_group btrfs_debug_feature_attr_group = { |
| 653 | .name = "debug" , |
| 654 | .attrs = btrfs_debug_feature_attrs, |
| 655 | }; |
| 656 | |
| 657 | #endif |
| 658 | |
| 659 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
| 660 | { |
| 661 | u64 val; |
| 662 | if (lock) |
| 663 | spin_lock(lock); |
| 664 | val = *value_ptr; |
| 665 | if (lock) |
| 666 | spin_unlock(lock); |
| 667 | return sysfs_emit(buf, fmt: "%llu\n" , val); |
| 668 | } |
| 669 | |
| 670 | static ssize_t global_rsv_size_show(struct kobject *kobj, |
| 671 | struct kobj_attribute *ka, char *buf) |
| 672 | { |
| 673 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: kobj->parent); |
| 674 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 675 | return btrfs_show_u64(value_ptr: &block_rsv->size, lock: &block_rsv->lock, buf); |
| 676 | } |
| 677 | BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show); |
| 678 | |
| 679 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, |
| 680 | struct kobj_attribute *a, char *buf) |
| 681 | { |
| 682 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: kobj->parent); |
| 683 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 684 | return btrfs_show_u64(value_ptr: &block_rsv->reserved, lock: &block_rsv->lock, buf); |
| 685 | } |
| 686 | BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show); |
| 687 | |
| 688 | #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) |
| 689 | #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj) |
| 690 | |
| 691 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 692 | struct kobj_attribute *attr, char *buf); |
| 693 | BTRFS_ATTR(raid, total_bytes, raid_bytes_show); |
| 694 | BTRFS_ATTR(raid, used_bytes, raid_bytes_show); |
| 695 | |
| 696 | static ssize_t raid_bytes_show(struct kobject *kobj, |
| 697 | struct kobj_attribute *attr, char *buf) |
| 698 | |
| 699 | { |
| 700 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); |
| 701 | struct btrfs_block_group *block_group; |
| 702 | int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags); |
| 703 | u64 val = 0; |
| 704 | |
| 705 | down_read(sem: &sinfo->groups_sem); |
| 706 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { |
| 707 | if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes)) |
| 708 | val += block_group->length; |
| 709 | else |
| 710 | val += block_group->used; |
| 711 | } |
| 712 | up_read(sem: &sinfo->groups_sem); |
| 713 | return sysfs_emit(buf, fmt: "%llu\n" , val); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Allocation information about block group profiles. |
| 718 | * |
| 719 | * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/ |
| 720 | */ |
| 721 | static struct attribute *raid_attrs[] = { |
| 722 | BTRFS_ATTR_PTR(raid, total_bytes), |
| 723 | BTRFS_ATTR_PTR(raid, used_bytes), |
| 724 | NULL |
| 725 | }; |
| 726 | ATTRIBUTE_GROUPS(raid); |
| 727 | |
| 728 | static void release_raid_kobj(struct kobject *kobj) |
| 729 | { |
| 730 | kfree(to_raid_kobj(kobj)); |
| 731 | } |
| 732 | |
| 733 | static const struct kobj_type btrfs_raid_ktype = { |
| 734 | .sysfs_ops = &kobj_sysfs_ops, |
| 735 | .release = release_raid_kobj, |
| 736 | .default_groups = raid_groups, |
| 737 | }; |
| 738 | |
| 739 | #define SPACE_INFO_ATTR(field) \ |
| 740 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ |
| 741 | struct kobj_attribute *a, \ |
| 742 | char *buf) \ |
| 743 | { \ |
| 744 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ |
| 745 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ |
| 746 | } \ |
| 747 | BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field) |
| 748 | |
| 749 | static ssize_t btrfs_chunk_size_show(struct kobject *kobj, |
| 750 | struct kobj_attribute *a, char *buf) |
| 751 | { |
| 752 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 753 | |
| 754 | return sysfs_emit(buf, fmt: "%llu\n" , READ_ONCE(sinfo->chunk_size)); |
| 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Store new chunk size in space info. Can be called on a read-only filesystem. |
| 759 | * |
| 760 | * If the new chunk size value is larger than 10% of free space it is reduced |
| 761 | * to match that limit. Alignment must be to 256M and the system chunk size |
| 762 | * cannot be set. |
| 763 | */ |
| 764 | static ssize_t btrfs_chunk_size_store(struct kobject *kobj, |
| 765 | struct kobj_attribute *a, |
| 766 | const char *buf, size_t len) |
| 767 | { |
| 768 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 769 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: get_btrfs_kobj(kobj)); |
| 770 | char *retptr; |
| 771 | u64 val; |
| 772 | |
| 773 | if (!capable(CAP_SYS_ADMIN)) |
| 774 | return -EPERM; |
| 775 | |
| 776 | if (!fs_info->fs_devices) |
| 777 | return -EINVAL; |
| 778 | |
| 779 | if (btrfs_is_zoned(fs_info)) |
| 780 | return -EINVAL; |
| 781 | |
| 782 | /* System block type must not be changed. */ |
| 783 | if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 784 | return -EPERM; |
| 785 | |
| 786 | val = memparse(ptr: buf, retptr: &retptr); |
| 787 | /* There could be trailing '\n', also catch any typos after the value */ |
| 788 | retptr = skip_spaces(retptr); |
| 789 | if (*retptr != 0 || val == 0) |
| 790 | return -EINVAL; |
| 791 | |
| 792 | val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE); |
| 793 | |
| 794 | /* Limit stripe size to 10% of available space. */ |
| 795 | val = min(mult_perc(fs_info->fs_devices->total_rw_bytes, 10), val); |
| 796 | |
| 797 | /* Must be multiple of 256M. */ |
| 798 | val &= ~((u64)SZ_256M - 1); |
| 799 | |
| 800 | /* Must be at least 256M. */ |
| 801 | if (val < SZ_256M) |
| 802 | return -EINVAL; |
| 803 | |
| 804 | btrfs_update_space_info_chunk_size(space_info, chunk_size: val); |
| 805 | |
| 806 | return len; |
| 807 | } |
| 808 | |
| 809 | static ssize_t btrfs_size_classes_show(struct kobject *kobj, |
| 810 | struct kobj_attribute *a, char *buf) |
| 811 | { |
| 812 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 813 | struct btrfs_block_group *bg; |
| 814 | u32 none = 0; |
| 815 | u32 small = 0; |
| 816 | u32 medium = 0; |
| 817 | u32 large = 0; |
| 818 | |
| 819 | for (int i = 0; i < BTRFS_NR_RAID_TYPES; ++i) { |
| 820 | down_read(sem: &sinfo->groups_sem); |
| 821 | list_for_each_entry(bg, &sinfo->block_groups[i], list) { |
| 822 | if (!btrfs_block_group_should_use_size_class(bg)) |
| 823 | continue; |
| 824 | switch (bg->size_class) { |
| 825 | case BTRFS_BG_SZ_NONE: |
| 826 | none++; |
| 827 | break; |
| 828 | case BTRFS_BG_SZ_SMALL: |
| 829 | small++; |
| 830 | break; |
| 831 | case BTRFS_BG_SZ_MEDIUM: |
| 832 | medium++; |
| 833 | break; |
| 834 | case BTRFS_BG_SZ_LARGE: |
| 835 | large++; |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | up_read(sem: &sinfo->groups_sem); |
| 840 | } |
| 841 | return sysfs_emit(buf, fmt: "none %u\n" |
| 842 | "small %u\n" |
| 843 | "medium %u\n" |
| 844 | "large %u\n" , |
| 845 | none, small, medium, large); |
| 846 | } |
| 847 | |
| 848 | #ifdef CONFIG_BTRFS_DEBUG |
| 849 | /* |
| 850 | * Request chunk allocation with current chunk size. |
| 851 | */ |
| 852 | static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj, |
| 853 | struct kobj_attribute *a, |
| 854 | const char *buf, size_t len) |
| 855 | { |
| 856 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 857 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: get_btrfs_kobj(kobj)); |
| 858 | struct btrfs_trans_handle *trans; |
| 859 | bool val; |
| 860 | int ret; |
| 861 | |
| 862 | if (!capable(CAP_SYS_ADMIN)) |
| 863 | return -EPERM; |
| 864 | |
| 865 | if (sb_rdonly(sb: fs_info->sb)) |
| 866 | return -EROFS; |
| 867 | |
| 868 | ret = kstrtobool(s: buf, res: &val); |
| 869 | if (ret) |
| 870 | return ret; |
| 871 | |
| 872 | if (!val) |
| 873 | return -EINVAL; |
| 874 | |
| 875 | /* |
| 876 | * This is unsafe to be called from sysfs context and may cause |
| 877 | * unexpected problems. |
| 878 | */ |
| 879 | trans = btrfs_start_transaction(root: fs_info->tree_root, num_items: 0); |
| 880 | if (IS_ERR(ptr: trans)) |
| 881 | return PTR_ERR(ptr: trans); |
| 882 | ret = btrfs_force_chunk_alloc(trans, type: space_info->flags); |
| 883 | btrfs_end_transaction(trans); |
| 884 | |
| 885 | if (ret == 1) |
| 886 | return len; |
| 887 | |
| 888 | return -ENOSPC; |
| 889 | } |
| 890 | BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store); |
| 891 | |
| 892 | #endif |
| 893 | |
| 894 | SPACE_INFO_ATTR(flags); |
| 895 | SPACE_INFO_ATTR(total_bytes); |
| 896 | SPACE_INFO_ATTR(bytes_used); |
| 897 | SPACE_INFO_ATTR(bytes_pinned); |
| 898 | SPACE_INFO_ATTR(bytes_reserved); |
| 899 | SPACE_INFO_ATTR(bytes_may_use); |
| 900 | SPACE_INFO_ATTR(bytes_readonly); |
| 901 | SPACE_INFO_ATTR(bytes_zone_unusable); |
| 902 | SPACE_INFO_ATTR(disk_used); |
| 903 | SPACE_INFO_ATTR(disk_total); |
| 904 | SPACE_INFO_ATTR(reclaim_count); |
| 905 | SPACE_INFO_ATTR(reclaim_bytes); |
| 906 | SPACE_INFO_ATTR(reclaim_errors); |
| 907 | BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store); |
| 908 | BTRFS_ATTR(space_info, size_classes, btrfs_size_classes_show); |
| 909 | |
| 910 | static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj, |
| 911 | struct kobj_attribute *a, |
| 912 | char *buf) |
| 913 | { |
| 914 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 915 | ssize_t ret; |
| 916 | |
| 917 | spin_lock(lock: &space_info->lock); |
| 918 | ret = sysfs_emit(buf, fmt: "%d\n" , btrfs_calc_reclaim_threshold(space_info)); |
| 919 | spin_unlock(lock: &space_info->lock); |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj, |
| 924 | struct kobj_attribute *a, |
| 925 | const char *buf, size_t len) |
| 926 | { |
| 927 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 928 | int thresh; |
| 929 | int ret; |
| 930 | |
| 931 | if (READ_ONCE(space_info->dynamic_reclaim)) |
| 932 | return -EINVAL; |
| 933 | |
| 934 | ret = kstrtoint(s: buf, base: 10, res: &thresh); |
| 935 | if (ret) |
| 936 | return ret; |
| 937 | |
| 938 | if (thresh < 0 || thresh > 100) |
| 939 | return -EINVAL; |
| 940 | |
| 941 | WRITE_ONCE(space_info->bg_reclaim_threshold, thresh); |
| 942 | |
| 943 | return len; |
| 944 | } |
| 945 | |
| 946 | BTRFS_ATTR_RW(space_info, bg_reclaim_threshold, |
| 947 | btrfs_sinfo_bg_reclaim_threshold_show, |
| 948 | btrfs_sinfo_bg_reclaim_threshold_store); |
| 949 | |
| 950 | static ssize_t btrfs_sinfo_dynamic_reclaim_show(struct kobject *kobj, |
| 951 | struct kobj_attribute *a, |
| 952 | char *buf) |
| 953 | { |
| 954 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 955 | |
| 956 | return sysfs_emit(buf, fmt: "%d\n" , READ_ONCE(space_info->dynamic_reclaim)); |
| 957 | } |
| 958 | |
| 959 | static ssize_t btrfs_sinfo_dynamic_reclaim_store(struct kobject *kobj, |
| 960 | struct kobj_attribute *a, |
| 961 | const char *buf, size_t len) |
| 962 | { |
| 963 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 964 | int dynamic_reclaim; |
| 965 | int ret; |
| 966 | |
| 967 | ret = kstrtoint(s: buf, base: 10, res: &dynamic_reclaim); |
| 968 | if (ret) |
| 969 | return ret; |
| 970 | |
| 971 | if (dynamic_reclaim < 0) |
| 972 | return -EINVAL; |
| 973 | |
| 974 | WRITE_ONCE(space_info->dynamic_reclaim, dynamic_reclaim != 0); |
| 975 | |
| 976 | return len; |
| 977 | } |
| 978 | |
| 979 | BTRFS_ATTR_RW(space_info, dynamic_reclaim, |
| 980 | btrfs_sinfo_dynamic_reclaim_show, |
| 981 | btrfs_sinfo_dynamic_reclaim_store); |
| 982 | |
| 983 | static ssize_t btrfs_sinfo_periodic_reclaim_show(struct kobject *kobj, |
| 984 | struct kobj_attribute *a, |
| 985 | char *buf) |
| 986 | { |
| 987 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 988 | |
| 989 | return sysfs_emit(buf, fmt: "%d\n" , READ_ONCE(space_info->periodic_reclaim)); |
| 990 | } |
| 991 | |
| 992 | static ssize_t btrfs_sinfo_periodic_reclaim_store(struct kobject *kobj, |
| 993 | struct kobj_attribute *a, |
| 994 | const char *buf, size_t len) |
| 995 | { |
| 996 | struct btrfs_space_info *space_info = to_space_info(kobj); |
| 997 | int periodic_reclaim; |
| 998 | int ret; |
| 999 | |
| 1000 | ret = kstrtoint(s: buf, base: 10, res: &periodic_reclaim); |
| 1001 | if (ret) |
| 1002 | return ret; |
| 1003 | |
| 1004 | if (periodic_reclaim < 0) |
| 1005 | return -EINVAL; |
| 1006 | |
| 1007 | WRITE_ONCE(space_info->periodic_reclaim, periodic_reclaim != 0); |
| 1008 | |
| 1009 | return len; |
| 1010 | } |
| 1011 | |
| 1012 | BTRFS_ATTR_RW(space_info, periodic_reclaim, |
| 1013 | btrfs_sinfo_periodic_reclaim_show, |
| 1014 | btrfs_sinfo_periodic_reclaim_store); |
| 1015 | |
| 1016 | /* |
| 1017 | * Allocation information about block group types. |
| 1018 | * |
| 1019 | * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/ |
| 1020 | */ |
| 1021 | static struct attribute *space_info_attrs[] = { |
| 1022 | BTRFS_ATTR_PTR(space_info, flags), |
| 1023 | BTRFS_ATTR_PTR(space_info, total_bytes), |
| 1024 | BTRFS_ATTR_PTR(space_info, bytes_used), |
| 1025 | BTRFS_ATTR_PTR(space_info, bytes_pinned), |
| 1026 | BTRFS_ATTR_PTR(space_info, bytes_reserved), |
| 1027 | BTRFS_ATTR_PTR(space_info, bytes_may_use), |
| 1028 | BTRFS_ATTR_PTR(space_info, bytes_readonly), |
| 1029 | BTRFS_ATTR_PTR(space_info, bytes_zone_unusable), |
| 1030 | BTRFS_ATTR_PTR(space_info, disk_used), |
| 1031 | BTRFS_ATTR_PTR(space_info, disk_total), |
| 1032 | BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold), |
| 1033 | BTRFS_ATTR_PTR(space_info, dynamic_reclaim), |
| 1034 | BTRFS_ATTR_PTR(space_info, chunk_size), |
| 1035 | BTRFS_ATTR_PTR(space_info, size_classes), |
| 1036 | BTRFS_ATTR_PTR(space_info, reclaim_count), |
| 1037 | BTRFS_ATTR_PTR(space_info, reclaim_bytes), |
| 1038 | BTRFS_ATTR_PTR(space_info, reclaim_errors), |
| 1039 | BTRFS_ATTR_PTR(space_info, periodic_reclaim), |
| 1040 | #ifdef CONFIG_BTRFS_DEBUG |
| 1041 | BTRFS_ATTR_PTR(space_info, force_chunk_alloc), |
| 1042 | #endif |
| 1043 | NULL, |
| 1044 | }; |
| 1045 | ATTRIBUTE_GROUPS(space_info); |
| 1046 | |
| 1047 | static void space_info_release(struct kobject *kobj) |
| 1048 | { |
| 1049 | struct btrfs_space_info *sinfo = to_space_info(kobj); |
| 1050 | kfree(objp: sinfo); |
| 1051 | } |
| 1052 | |
| 1053 | static const struct kobj_type space_info_ktype = { |
| 1054 | .sysfs_ops = &kobj_sysfs_ops, |
| 1055 | .release = space_info_release, |
| 1056 | .default_groups = space_info_groups, |
| 1057 | }; |
| 1058 | |
| 1059 | /* |
| 1060 | * Allocation information about block groups. |
| 1061 | * |
| 1062 | * Path: /sys/fs/btrfs/<uuid>/allocation/ |
| 1063 | */ |
| 1064 | static const struct attribute *allocation_attrs[] = { |
| 1065 | BTRFS_ATTR_PTR(allocation, global_rsv_reserved), |
| 1066 | BTRFS_ATTR_PTR(allocation, global_rsv_size), |
| 1067 | NULL, |
| 1068 | }; |
| 1069 | |
| 1070 | static ssize_t btrfs_label_show(struct kobject *kobj, |
| 1071 | struct kobj_attribute *a, char *buf) |
| 1072 | { |
| 1073 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1074 | char *label = fs_info->super_copy->label; |
| 1075 | ssize_t ret; |
| 1076 | |
| 1077 | spin_lock(lock: &fs_info->super_lock); |
| 1078 | ret = sysfs_emit(buf, fmt: label[0] ? "%s\n" : "%s" , label); |
| 1079 | spin_unlock(lock: &fs_info->super_lock); |
| 1080 | |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | static ssize_t btrfs_label_store(struct kobject *kobj, |
| 1085 | struct kobj_attribute *a, |
| 1086 | const char *buf, size_t len) |
| 1087 | { |
| 1088 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1089 | size_t p_len; |
| 1090 | |
| 1091 | if (!fs_info) |
| 1092 | return -EPERM; |
| 1093 | |
| 1094 | if (sb_rdonly(sb: fs_info->sb)) |
| 1095 | return -EROFS; |
| 1096 | |
| 1097 | /* |
| 1098 | * p_len is the len until the first occurrence of either |
| 1099 | * '\n' or '\0' |
| 1100 | */ |
| 1101 | p_len = strcspn(buf, "\n" ); |
| 1102 | |
| 1103 | if (p_len >= BTRFS_LABEL_SIZE) |
| 1104 | return -EINVAL; |
| 1105 | |
| 1106 | spin_lock(lock: &fs_info->super_lock); |
| 1107 | memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); |
| 1108 | memcpy(fs_info->super_copy->label, buf, p_len); |
| 1109 | spin_unlock(lock: &fs_info->super_lock); |
| 1110 | |
| 1111 | /* |
| 1112 | * We don't want to do full transaction commit from inside sysfs |
| 1113 | */ |
| 1114 | set_bit(nr: BTRFS_FS_NEED_TRANS_COMMIT, addr: &fs_info->flags); |
| 1115 | wake_up_process(tsk: fs_info->transaction_kthread); |
| 1116 | |
| 1117 | return len; |
| 1118 | } |
| 1119 | BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store); |
| 1120 | |
| 1121 | static ssize_t btrfs_nodesize_show(struct kobject *kobj, |
| 1122 | struct kobj_attribute *a, char *buf) |
| 1123 | { |
| 1124 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1125 | |
| 1126 | return sysfs_emit(buf, fmt: "%u\n" , fs_info->nodesize); |
| 1127 | } |
| 1128 | |
| 1129 | BTRFS_ATTR(, nodesize, btrfs_nodesize_show); |
| 1130 | |
| 1131 | static ssize_t btrfs_sectorsize_show(struct kobject *kobj, |
| 1132 | struct kobj_attribute *a, char *buf) |
| 1133 | { |
| 1134 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1135 | |
| 1136 | return sysfs_emit(buf, fmt: "%u\n" , fs_info->sectorsize); |
| 1137 | } |
| 1138 | |
| 1139 | BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show); |
| 1140 | |
| 1141 | static ssize_t btrfs_commit_stats_show(struct kobject *kobj, |
| 1142 | struct kobj_attribute *a, char *buf) |
| 1143 | { |
| 1144 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1145 | u64 now = ktime_get_ns(); |
| 1146 | u64 start_time = fs_info->commit_stats.critical_section_start_time; |
| 1147 | u64 pending = 0; |
| 1148 | |
| 1149 | if (start_time) |
| 1150 | pending = now - start_time; |
| 1151 | |
| 1152 | return sysfs_emit(buf, |
| 1153 | fmt: "commits %llu\n" |
| 1154 | "cur_commit_ms %llu\n" |
| 1155 | "last_commit_ms %llu\n" |
| 1156 | "max_commit_ms %llu\n" |
| 1157 | "total_commit_ms %llu\n" , |
| 1158 | fs_info->commit_stats.commit_count, |
| 1159 | div_u64(dividend: pending, NSEC_PER_MSEC), |
| 1160 | div_u64(dividend: fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC), |
| 1161 | div_u64(dividend: fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC), |
| 1162 | div_u64(dividend: fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC)); |
| 1163 | } |
| 1164 | |
| 1165 | static ssize_t btrfs_commit_stats_store(struct kobject *kobj, |
| 1166 | struct kobj_attribute *a, |
| 1167 | const char *buf, size_t len) |
| 1168 | { |
| 1169 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1170 | unsigned long val; |
| 1171 | int ret; |
| 1172 | |
| 1173 | if (!fs_info) |
| 1174 | return -EPERM; |
| 1175 | |
| 1176 | if (!capable(CAP_SYS_RESOURCE)) |
| 1177 | return -EPERM; |
| 1178 | |
| 1179 | ret = kstrtoul(s: buf, base: 10, res: &val); |
| 1180 | if (ret) |
| 1181 | return ret; |
| 1182 | if (val) |
| 1183 | return -EINVAL; |
| 1184 | |
| 1185 | WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0); |
| 1186 | |
| 1187 | return len; |
| 1188 | } |
| 1189 | BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store); |
| 1190 | |
| 1191 | static ssize_t btrfs_clone_alignment_show(struct kobject *kobj, |
| 1192 | struct kobj_attribute *a, char *buf) |
| 1193 | { |
| 1194 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1195 | |
| 1196 | return sysfs_emit(buf, fmt: "%u\n" , fs_info->sectorsize); |
| 1197 | } |
| 1198 | |
| 1199 | BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show); |
| 1200 | |
| 1201 | static ssize_t quota_override_show(struct kobject *kobj, |
| 1202 | struct kobj_attribute *a, char *buf) |
| 1203 | { |
| 1204 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1205 | int quota_override; |
| 1206 | |
| 1207 | quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags); |
| 1208 | return sysfs_emit(buf, fmt: "%d\n" , quota_override); |
| 1209 | } |
| 1210 | |
| 1211 | static ssize_t quota_override_store(struct kobject *kobj, |
| 1212 | struct kobj_attribute *a, |
| 1213 | const char *buf, size_t len) |
| 1214 | { |
| 1215 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1216 | unsigned long knob; |
| 1217 | int ret; |
| 1218 | |
| 1219 | if (!fs_info) |
| 1220 | return -EPERM; |
| 1221 | |
| 1222 | if (!capable(CAP_SYS_RESOURCE)) |
| 1223 | return -EPERM; |
| 1224 | |
| 1225 | ret = kstrtoul(s: buf, base: 10, res: &knob); |
| 1226 | if (ret) |
| 1227 | return ret; |
| 1228 | if (knob > 1) |
| 1229 | return -EINVAL; |
| 1230 | |
| 1231 | if (knob) |
| 1232 | set_bit(nr: BTRFS_FS_QUOTA_OVERRIDE, addr: &fs_info->flags); |
| 1233 | else |
| 1234 | clear_bit(nr: BTRFS_FS_QUOTA_OVERRIDE, addr: &fs_info->flags); |
| 1235 | |
| 1236 | return len; |
| 1237 | } |
| 1238 | |
| 1239 | BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store); |
| 1240 | |
| 1241 | static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj, |
| 1242 | struct kobj_attribute *a, char *buf) |
| 1243 | { |
| 1244 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1245 | |
| 1246 | return sysfs_emit(buf, fmt: "%pU\n" , fs_info->fs_devices->metadata_uuid); |
| 1247 | } |
| 1248 | |
| 1249 | BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show); |
| 1250 | |
| 1251 | static ssize_t btrfs_checksum_show(struct kobject *kobj, |
| 1252 | struct kobj_attribute *a, char *buf) |
| 1253 | { |
| 1254 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1255 | u16 csum_type = btrfs_super_csum_type(s: fs_info->super_copy); |
| 1256 | |
| 1257 | return sysfs_emit(buf, fmt: "%s (%s)\n" , |
| 1258 | btrfs_super_csum_name(csum_type), |
| 1259 | crypto_shash_driver_name(tfm: fs_info->csum_shash)); |
| 1260 | } |
| 1261 | |
| 1262 | BTRFS_ATTR(, checksum, btrfs_checksum_show); |
| 1263 | |
| 1264 | static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj, |
| 1265 | struct kobj_attribute *a, char *buf) |
| 1266 | { |
| 1267 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1268 | const char *str; |
| 1269 | |
| 1270 | switch (READ_ONCE(fs_info->exclusive_operation)) { |
| 1271 | case BTRFS_EXCLOP_NONE: |
| 1272 | str = "none\n" ; |
| 1273 | break; |
| 1274 | case BTRFS_EXCLOP_BALANCE: |
| 1275 | str = "balance\n" ; |
| 1276 | break; |
| 1277 | case BTRFS_EXCLOP_BALANCE_PAUSED: |
| 1278 | str = "balance paused\n" ; |
| 1279 | break; |
| 1280 | case BTRFS_EXCLOP_DEV_ADD: |
| 1281 | str = "device add\n" ; |
| 1282 | break; |
| 1283 | case BTRFS_EXCLOP_DEV_REMOVE: |
| 1284 | str = "device remove\n" ; |
| 1285 | break; |
| 1286 | case BTRFS_EXCLOP_DEV_REPLACE: |
| 1287 | str = "device replace\n" ; |
| 1288 | break; |
| 1289 | case BTRFS_EXCLOP_RESIZE: |
| 1290 | str = "resize\n" ; |
| 1291 | break; |
| 1292 | case BTRFS_EXCLOP_SWAP_ACTIVATE: |
| 1293 | str = "swap activate\n" ; |
| 1294 | break; |
| 1295 | default: |
| 1296 | str = "UNKNOWN\n" ; |
| 1297 | break; |
| 1298 | } |
| 1299 | return sysfs_emit(buf, fmt: "%s" , str); |
| 1300 | } |
| 1301 | BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show); |
| 1302 | |
| 1303 | static ssize_t btrfs_generation_show(struct kobject *kobj, |
| 1304 | struct kobj_attribute *a, char *buf) |
| 1305 | { |
| 1306 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1307 | |
| 1308 | return sysfs_emit(buf, fmt: "%llu\n" , btrfs_get_fs_generation(fs_info)); |
| 1309 | } |
| 1310 | BTRFS_ATTR(, generation, btrfs_generation_show); |
| 1311 | |
| 1312 | static ssize_t btrfs_temp_fsid_show(struct kobject *kobj, |
| 1313 | struct kobj_attribute *a, char *buf) |
| 1314 | { |
| 1315 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1316 | |
| 1317 | return sysfs_emit(buf, fmt: "%d\n" , fs_info->fs_devices->temp_fsid); |
| 1318 | } |
| 1319 | BTRFS_ATTR(, temp_fsid, btrfs_temp_fsid_show); |
| 1320 | |
| 1321 | static const char *btrfs_read_policy_name[] = { |
| 1322 | "pid" , |
| 1323 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1324 | "round-robin" , |
| 1325 | "devid" , |
| 1326 | #endif |
| 1327 | }; |
| 1328 | |
| 1329 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1330 | |
| 1331 | /* Global module configuration parameters. */ |
| 1332 | static char *read_policy; |
| 1333 | char *btrfs_get_mod_read_policy(void) |
| 1334 | { |
| 1335 | return read_policy; |
| 1336 | } |
| 1337 | |
| 1338 | /* Set perms to 0, disable /sys/module/btrfs/parameter/read_policy interface. */ |
| 1339 | module_param(read_policy, charp, 0); |
| 1340 | MODULE_PARM_DESC(read_policy, |
| 1341 | "Global read policy: pid (default), round-robin[:<min_contig_read>], devid[:<devid>]" ); |
| 1342 | #endif |
| 1343 | |
| 1344 | int btrfs_read_policy_to_enum(const char *str, s64 *value_ret) |
| 1345 | { |
| 1346 | char param[32]; |
| 1347 | char __maybe_unused *value_str; |
| 1348 | |
| 1349 | if (!str || strlen(str) == 0) |
| 1350 | return 0; |
| 1351 | |
| 1352 | strscpy(param, str); |
| 1353 | |
| 1354 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1355 | /* Separate value from input in policy:value format. */ |
| 1356 | value_str = strchr(param, ':'); |
| 1357 | if (value_str) { |
| 1358 | char *retptr; |
| 1359 | |
| 1360 | *value_str = 0; |
| 1361 | value_str++; |
| 1362 | if (!value_ret) |
| 1363 | return -EINVAL; |
| 1364 | |
| 1365 | *value_ret = memparse(ptr: value_str, retptr: &retptr); |
| 1366 | /* There could be any trailing typos after the value. */ |
| 1367 | retptr = skip_spaces(retptr); |
| 1368 | if (*retptr != 0 || *value_ret <= 0) |
| 1369 | return -EINVAL; |
| 1370 | } |
| 1371 | #endif |
| 1372 | |
| 1373 | return sysfs_match_string(btrfs_read_policy_name, param); |
| 1374 | } |
| 1375 | |
| 1376 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1377 | int __init btrfs_read_policy_init(void) |
| 1378 | { |
| 1379 | s64 value; |
| 1380 | |
| 1381 | if (btrfs_read_policy_to_enum(str: read_policy, value_ret: &value) == -EINVAL) { |
| 1382 | btrfs_err(NULL, "invalid read policy or value %s" , read_policy); |
| 1383 | return -EINVAL; |
| 1384 | } |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | #endif |
| 1389 | |
| 1390 | static ssize_t btrfs_read_policy_show(struct kobject *kobj, |
| 1391 | struct kobj_attribute *a, char *buf) |
| 1392 | { |
| 1393 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 1394 | const enum btrfs_read_policy policy = READ_ONCE(fs_devices->read_policy); |
| 1395 | ssize_t ret = 0; |
| 1396 | int i; |
| 1397 | |
| 1398 | for (i = 0; i < BTRFS_NR_READ_POLICY; i++) { |
| 1399 | if (ret != 0) |
| 1400 | ret += sysfs_emit_at(buf, at: ret, fmt: " " ); |
| 1401 | |
| 1402 | if (i == policy) |
| 1403 | ret += sysfs_emit_at(buf, at: ret, fmt: "[" ); |
| 1404 | |
| 1405 | ret += sysfs_emit_at(buf, at: ret, fmt: "%s" , btrfs_read_policy_name[i]); |
| 1406 | |
| 1407 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1408 | if (i == BTRFS_READ_POLICY_RR) |
| 1409 | ret += sysfs_emit_at(buf, at: ret, fmt: ":%u" , |
| 1410 | READ_ONCE(fs_devices->rr_min_contig_read)); |
| 1411 | |
| 1412 | if (i == BTRFS_READ_POLICY_DEVID) |
| 1413 | ret += sysfs_emit_at(buf, at: ret, fmt: ":%llu" , |
| 1414 | READ_ONCE(fs_devices->read_devid)); |
| 1415 | #endif |
| 1416 | if (i == policy) |
| 1417 | ret += sysfs_emit_at(buf, at: ret, fmt: "]" ); |
| 1418 | } |
| 1419 | |
| 1420 | ret += sysfs_emit_at(buf, at: ret, fmt: "\n" ); |
| 1421 | |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
| 1425 | static ssize_t btrfs_read_policy_store(struct kobject *kobj, |
| 1426 | struct kobj_attribute *a, |
| 1427 | const char *buf, size_t len) |
| 1428 | { |
| 1429 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 1430 | int index; |
| 1431 | s64 value = -1; |
| 1432 | |
| 1433 | index = btrfs_read_policy_to_enum(str: buf, value_ret: &value); |
| 1434 | if (index < 0) |
| 1435 | return -EINVAL; |
| 1436 | |
| 1437 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1438 | /* If moving from RR then disable collecting fs stats. */ |
| 1439 | if (fs_devices->read_policy == BTRFS_READ_POLICY_RR && index != BTRFS_READ_POLICY_RR) |
| 1440 | fs_devices->collect_fs_stats = false; |
| 1441 | |
| 1442 | if (index == BTRFS_READ_POLICY_RR) { |
| 1443 | if (value != -1) { |
| 1444 | const u32 sectorsize = fs_devices->fs_info->sectorsize; |
| 1445 | |
| 1446 | if (!IS_ALIGNED(value, sectorsize)) { |
| 1447 | u64 temp_value = round_up(value, sectorsize); |
| 1448 | |
| 1449 | btrfs_debug(fs_devices->fs_info, |
| 1450 | "read_policy: min contig read %lld should be multiple of sectorsize %u, rounded to %llu" , |
| 1451 | value, sectorsize, temp_value); |
| 1452 | value = temp_value; |
| 1453 | } |
| 1454 | } else { |
| 1455 | value = BTRFS_DEFAULT_RR_MIN_CONTIG_READ; |
| 1456 | } |
| 1457 | |
| 1458 | if (index != READ_ONCE(fs_devices->read_policy) || |
| 1459 | value != READ_ONCE(fs_devices->rr_min_contig_read)) { |
| 1460 | WRITE_ONCE(fs_devices->read_policy, index); |
| 1461 | WRITE_ONCE(fs_devices->rr_min_contig_read, value); |
| 1462 | |
| 1463 | btrfs_info(fs_devices->fs_info, "read policy set to '%s:%lld'" , |
| 1464 | btrfs_read_policy_name[index], value); |
| 1465 | } |
| 1466 | |
| 1467 | fs_devices->collect_fs_stats = true; |
| 1468 | |
| 1469 | return len; |
| 1470 | } |
| 1471 | |
| 1472 | if (index == BTRFS_READ_POLICY_DEVID) { |
| 1473 | if (value != -1) { |
| 1474 | BTRFS_DEV_LOOKUP_ARGS(args); |
| 1475 | |
| 1476 | /* Validate input devid. */ |
| 1477 | args.devid = value; |
| 1478 | if (btrfs_find_device(fs_devices, args: &args) == NULL) |
| 1479 | return -EINVAL; |
| 1480 | } else { |
| 1481 | /* Set default devid to the devid of the latest device. */ |
| 1482 | value = fs_devices->latest_dev->devid; |
| 1483 | } |
| 1484 | |
| 1485 | if (index != READ_ONCE(fs_devices->read_policy) || |
| 1486 | value != READ_ONCE(fs_devices->read_devid)) { |
| 1487 | WRITE_ONCE(fs_devices->read_policy, index); |
| 1488 | WRITE_ONCE(fs_devices->read_devid, value); |
| 1489 | |
| 1490 | btrfs_info(fs_devices->fs_info, "read policy set to '%s:%llu'" , |
| 1491 | btrfs_read_policy_name[index], value); |
| 1492 | } |
| 1493 | |
| 1494 | return len; |
| 1495 | } |
| 1496 | #endif |
| 1497 | if (index != READ_ONCE(fs_devices->read_policy)) { |
| 1498 | WRITE_ONCE(fs_devices->read_policy, index); |
| 1499 | btrfs_info(fs_devices->fs_info, "read policy set to '%s'" , |
| 1500 | btrfs_read_policy_name[index]); |
| 1501 | } |
| 1502 | |
| 1503 | return len; |
| 1504 | } |
| 1505 | BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store); |
| 1506 | |
| 1507 | static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj, |
| 1508 | struct kobj_attribute *a, |
| 1509 | char *buf) |
| 1510 | { |
| 1511 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1512 | |
| 1513 | return sysfs_emit(buf, fmt: "%d\n" , READ_ONCE(fs_info->bg_reclaim_threshold)); |
| 1514 | } |
| 1515 | |
| 1516 | static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj, |
| 1517 | struct kobj_attribute *a, |
| 1518 | const char *buf, size_t len) |
| 1519 | { |
| 1520 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
| 1521 | int thresh; |
| 1522 | int ret; |
| 1523 | |
| 1524 | ret = kstrtoint(s: buf, base: 10, res: &thresh); |
| 1525 | if (ret) |
| 1526 | return ret; |
| 1527 | |
| 1528 | #ifdef CONFIG_BTRFS_DEBUG |
| 1529 | if (thresh != 0 && (thresh > 100)) |
| 1530 | return -EINVAL; |
| 1531 | #else |
| 1532 | if (thresh != 0 && (thresh <= 50 || thresh > 100)) |
| 1533 | return -EINVAL; |
| 1534 | #endif |
| 1535 | |
| 1536 | WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh); |
| 1537 | |
| 1538 | return len; |
| 1539 | } |
| 1540 | BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show, |
| 1541 | btrfs_bg_reclaim_threshold_store); |
| 1542 | |
| 1543 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1544 | static ssize_t btrfs_offload_csum_show(struct kobject *kobj, |
| 1545 | struct kobj_attribute *a, char *buf) |
| 1546 | { |
| 1547 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 1548 | |
| 1549 | switch (READ_ONCE(fs_devices->offload_csum_mode)) { |
| 1550 | case BTRFS_OFFLOAD_CSUM_AUTO: |
| 1551 | return sysfs_emit(buf, fmt: "auto\n" ); |
| 1552 | case BTRFS_OFFLOAD_CSUM_FORCE_ON: |
| 1553 | return sysfs_emit(buf, fmt: "1\n" ); |
| 1554 | case BTRFS_OFFLOAD_CSUM_FORCE_OFF: |
| 1555 | return sysfs_emit(buf, fmt: "0\n" ); |
| 1556 | default: |
| 1557 | WARN_ON(1); |
| 1558 | return -EINVAL; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | static ssize_t btrfs_offload_csum_store(struct kobject *kobj, |
| 1563 | struct kobj_attribute *a, const char *buf, |
| 1564 | size_t len) |
| 1565 | { |
| 1566 | struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj); |
| 1567 | int ret; |
| 1568 | bool val; |
| 1569 | |
| 1570 | ret = kstrtobool(s: buf, res: &val); |
| 1571 | if (ret == 0) |
| 1572 | WRITE_ONCE(fs_devices->offload_csum_mode, |
| 1573 | val ? BTRFS_OFFLOAD_CSUM_FORCE_ON : BTRFS_OFFLOAD_CSUM_FORCE_OFF); |
| 1574 | else if (ret == -EINVAL && sysfs_streq(s1: buf, s2: "auto" )) |
| 1575 | WRITE_ONCE(fs_devices->offload_csum_mode, BTRFS_OFFLOAD_CSUM_AUTO); |
| 1576 | else |
| 1577 | return -EINVAL; |
| 1578 | |
| 1579 | return len; |
| 1580 | } |
| 1581 | BTRFS_ATTR_RW(, offload_csum, btrfs_offload_csum_show, btrfs_offload_csum_store); |
| 1582 | #endif |
| 1583 | |
| 1584 | /* |
| 1585 | * Per-filesystem information and stats. |
| 1586 | * |
| 1587 | * Path: /sys/fs/btrfs/<uuid>/ |
| 1588 | */ |
| 1589 | static const struct attribute *btrfs_attrs[] = { |
| 1590 | BTRFS_ATTR_PTR(, label), |
| 1591 | BTRFS_ATTR_PTR(, nodesize), |
| 1592 | BTRFS_ATTR_PTR(, sectorsize), |
| 1593 | BTRFS_ATTR_PTR(, clone_alignment), |
| 1594 | BTRFS_ATTR_PTR(, quota_override), |
| 1595 | BTRFS_ATTR_PTR(, metadata_uuid), |
| 1596 | BTRFS_ATTR_PTR(, checksum), |
| 1597 | BTRFS_ATTR_PTR(, exclusive_operation), |
| 1598 | BTRFS_ATTR_PTR(, generation), |
| 1599 | BTRFS_ATTR_PTR(, read_policy), |
| 1600 | BTRFS_ATTR_PTR(, bg_reclaim_threshold), |
| 1601 | BTRFS_ATTR_PTR(, commit_stats), |
| 1602 | BTRFS_ATTR_PTR(, temp_fsid), |
| 1603 | #ifdef CONFIG_BTRFS_EXPERIMENTAL |
| 1604 | BTRFS_ATTR_PTR(, offload_csum), |
| 1605 | #endif |
| 1606 | NULL, |
| 1607 | }; |
| 1608 | |
| 1609 | static void btrfs_release_fsid_kobj(struct kobject *kobj) |
| 1610 | { |
| 1611 | struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); |
| 1612 | |
| 1613 | memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject)); |
| 1614 | complete(&fs_devs->kobj_unregister); |
| 1615 | } |
| 1616 | |
| 1617 | static const struct kobj_type btrfs_ktype = { |
| 1618 | .sysfs_ops = &kobj_sysfs_ops, |
| 1619 | .release = btrfs_release_fsid_kobj, |
| 1620 | }; |
| 1621 | |
| 1622 | static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) |
| 1623 | { |
| 1624 | if (kobj->ktype != &btrfs_ktype) |
| 1625 | return NULL; |
| 1626 | return container_of(kobj, struct btrfs_fs_devices, fsid_kobj); |
| 1627 | } |
| 1628 | |
| 1629 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) |
| 1630 | { |
| 1631 | if (kobj->ktype != &btrfs_ktype) |
| 1632 | return NULL; |
| 1633 | return to_fs_devs(kobj)->fs_info; |
| 1634 | } |
| 1635 | |
| 1636 | static struct kobject *get_btrfs_kobj(struct kobject *kobj) |
| 1637 | { |
| 1638 | while (kobj) { |
| 1639 | if (kobj->ktype == &btrfs_ktype) |
| 1640 | return kobj; |
| 1641 | kobj = kobj->parent; |
| 1642 | } |
| 1643 | return NULL; |
| 1644 | } |
| 1645 | |
| 1646 | #define NUM_FEATURE_BITS 64 |
| 1647 | #define BTRFS_FEATURE_NAME_MAX 13 |
| 1648 | static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX]; |
| 1649 | static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS]; |
| 1650 | |
| 1651 | static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) == |
| 1652 | ARRAY_SIZE(btrfs_feature_attrs)); |
| 1653 | static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) == |
| 1654 | ARRAY_SIZE(btrfs_feature_attrs[0])); |
| 1655 | |
| 1656 | static const u64 supported_feature_masks[FEAT_MAX] = { |
| 1657 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, |
| 1658 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, |
| 1659 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, |
| 1660 | }; |
| 1661 | |
| 1662 | static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add) |
| 1663 | { |
| 1664 | int set; |
| 1665 | |
| 1666 | for (set = 0; set < FEAT_MAX; set++) { |
| 1667 | int i; |
| 1668 | struct attribute *attrs[2]; |
| 1669 | struct attribute_group agroup = { |
| 1670 | .name = "features" , |
| 1671 | .attrs = attrs, |
| 1672 | }; |
| 1673 | u64 features = get_features(fs_info, set); |
| 1674 | features &= ~supported_feature_masks[set]; |
| 1675 | |
| 1676 | if (!features) |
| 1677 | continue; |
| 1678 | |
| 1679 | attrs[1] = NULL; |
| 1680 | for (i = 0; i < NUM_FEATURE_BITS; i++) { |
| 1681 | struct btrfs_feature_attr *fa; |
| 1682 | |
| 1683 | if (!(features & (1ULL << i))) |
| 1684 | continue; |
| 1685 | |
| 1686 | fa = &btrfs_feature_attrs[set][i]; |
| 1687 | attrs[0] = &fa->kobj_attr.attr; |
| 1688 | if (add) { |
| 1689 | int ret; |
| 1690 | ret = sysfs_merge_group(kobj: &fs_info->fs_devices->fsid_kobj, |
| 1691 | grp: &agroup); |
| 1692 | if (ret) |
| 1693 | return ret; |
| 1694 | } else |
| 1695 | sysfs_unmerge_group(kobj: &fs_info->fs_devices->fsid_kobj, |
| 1696 | grp: &agroup); |
| 1697 | } |
| 1698 | |
| 1699 | } |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
| 1704 | { |
| 1705 | if (fs_devs->devinfo_kobj) { |
| 1706 | kobject_del(kobj: fs_devs->devinfo_kobj); |
| 1707 | kobject_put(kobj: fs_devs->devinfo_kobj); |
| 1708 | fs_devs->devinfo_kobj = NULL; |
| 1709 | } |
| 1710 | |
| 1711 | if (fs_devs->devices_kobj) { |
| 1712 | kobject_del(kobj: fs_devs->devices_kobj); |
| 1713 | kobject_put(kobj: fs_devs->devices_kobj); |
| 1714 | fs_devs->devices_kobj = NULL; |
| 1715 | } |
| 1716 | |
| 1717 | if (fs_devs->fsid_kobj.state_initialized) { |
| 1718 | kobject_del(kobj: &fs_devs->fsid_kobj); |
| 1719 | kobject_put(kobj: &fs_devs->fsid_kobj); |
| 1720 | wait_for_completion(&fs_devs->kobj_unregister); |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /* when fs_devs is NULL it will remove all fsid kobject */ |
| 1725 | void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs) |
| 1726 | { |
| 1727 | struct list_head *fs_uuids = btrfs_get_fs_uuids(); |
| 1728 | |
| 1729 | if (fs_devs) { |
| 1730 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | list_for_each_entry(fs_devs, fs_uuids, fs_list) { |
| 1735 | __btrfs_sysfs_remove_fsid(fs_devs); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 1740 | { |
| 1741 | struct btrfs_device *device; |
| 1742 | struct btrfs_fs_devices *seed; |
| 1743 | |
| 1744 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 1745 | btrfs_sysfs_remove_device(device); |
| 1746 | |
| 1747 | list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { |
| 1748 | list_for_each_entry(device, &seed->devices, dev_list) |
| 1749 | btrfs_sysfs_remove_device(device); |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) |
| 1754 | { |
| 1755 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 1756 | |
| 1757 | sysfs_remove_link(kobj: fsid_kobj, name: "bdi" ); |
| 1758 | |
| 1759 | if (fs_info->space_info_kobj) { |
| 1760 | sysfs_remove_files(kobj: fs_info->space_info_kobj, attr: allocation_attrs); |
| 1761 | kobject_del(kobj: fs_info->space_info_kobj); |
| 1762 | kobject_put(kobj: fs_info->space_info_kobj); |
| 1763 | } |
| 1764 | if (fs_info->discard_kobj) { |
| 1765 | sysfs_remove_files(kobj: fs_info->discard_kobj, attr: discard_attrs); |
| 1766 | kobject_del(kobj: fs_info->discard_kobj); |
| 1767 | kobject_put(kobj: fs_info->discard_kobj); |
| 1768 | } |
| 1769 | #ifdef CONFIG_BTRFS_DEBUG |
| 1770 | if (fs_info->debug_kobj) { |
| 1771 | sysfs_remove_files(kobj: fs_info->debug_kobj, attr: btrfs_debug_mount_attrs); |
| 1772 | kobject_del(kobj: fs_info->debug_kobj); |
| 1773 | kobject_put(kobj: fs_info->debug_kobj); |
| 1774 | } |
| 1775 | #endif |
| 1776 | addrm_unknown_feature_attrs(fs_info, add: false); |
| 1777 | sysfs_remove_group(kobj: fsid_kobj, grp: &btrfs_feature_attr_group); |
| 1778 | sysfs_remove_files(kobj: fsid_kobj, attr: btrfs_attrs); |
| 1779 | btrfs_sysfs_remove_fs_devices(fs_devices: fs_info->fs_devices); |
| 1780 | } |
| 1781 | |
| 1782 | static const char * const btrfs_feature_set_names[FEAT_MAX] = { |
| 1783 | [FEAT_COMPAT] = "compat" , |
| 1784 | [FEAT_COMPAT_RO] = "compat_ro" , |
| 1785 | [FEAT_INCOMPAT] = "incompat" , |
| 1786 | }; |
| 1787 | |
| 1788 | const char *btrfs_feature_set_name(enum btrfs_feature_set set) |
| 1789 | { |
| 1790 | return btrfs_feature_set_names[set]; |
| 1791 | } |
| 1792 | |
| 1793 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
| 1794 | { |
| 1795 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ |
| 1796 | int len = 0; |
| 1797 | int i; |
| 1798 | char *str; |
| 1799 | |
| 1800 | str = kmalloc(bufsize, GFP_KERNEL); |
| 1801 | if (!str) |
| 1802 | return str; |
| 1803 | |
| 1804 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1805 | const char *name; |
| 1806 | |
| 1807 | if (!(flags & (1ULL << i))) |
| 1808 | continue; |
| 1809 | |
| 1810 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; |
| 1811 | len += scnprintf(buf: str + len, size: bufsize - len, fmt: "%s%s" , |
| 1812 | len ? "," : "" , name); |
| 1813 | } |
| 1814 | |
| 1815 | return str; |
| 1816 | } |
| 1817 | |
| 1818 | static void init_feature_attrs(void) |
| 1819 | { |
| 1820 | struct btrfs_feature_attr *fa; |
| 1821 | int set, i; |
| 1822 | |
| 1823 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
| 1824 | memset(btrfs_unknown_feature_names, 0, |
| 1825 | sizeof(btrfs_unknown_feature_names)); |
| 1826 | |
| 1827 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
| 1828 | struct btrfs_feature_attr *sfa; |
| 1829 | struct attribute *a = btrfs_supported_feature_attrs[i]; |
| 1830 | int bit; |
| 1831 | sfa = attr_to_btrfs_feature_attr(attr: a); |
| 1832 | bit = ilog2(sfa->feature_bit); |
| 1833 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; |
| 1834 | |
| 1835 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; |
| 1836 | } |
| 1837 | |
| 1838 | for (set = 0; set < FEAT_MAX; set++) { |
| 1839 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { |
| 1840 | char *name = btrfs_unknown_feature_names[set][i]; |
| 1841 | fa = &btrfs_feature_attrs[set][i]; |
| 1842 | |
| 1843 | if (fa->kobj_attr.attr.name) |
| 1844 | continue; |
| 1845 | |
| 1846 | snprintf(buf: name, BTRFS_FEATURE_NAME_MAX, fmt: "%s:%u" , |
| 1847 | btrfs_feature_set_names[set], i); |
| 1848 | |
| 1849 | fa->kobj_attr.attr.name = name; |
| 1850 | fa->kobj_attr.attr.mode = S_IRUGO; |
| 1851 | fa->feature_set = set; |
| 1852 | fa->feature_bit = 1ULL << i; |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * Create a sysfs entry for a given block group type at path |
| 1859 | * /sys/fs/btrfs/UUID/allocation/data/TYPE |
| 1860 | */ |
| 1861 | void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache) |
| 1862 | { |
| 1863 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 1864 | struct btrfs_space_info *space_info = cache->space_info; |
| 1865 | struct raid_kobject *rkobj; |
| 1866 | const int index = btrfs_bg_flags_to_raid_index(flags: cache->flags); |
| 1867 | unsigned int nofs_flag; |
| 1868 | int ret; |
| 1869 | |
| 1870 | /* |
| 1871 | * Setup a NOFS context because kobject_add(), deep in its call chain, |
| 1872 | * does GFP_KERNEL allocations, and we are often called in a context |
| 1873 | * where if reclaim is triggered we can deadlock (we are either holding |
| 1874 | * a transaction handle or some lock required for a transaction |
| 1875 | * commit). |
| 1876 | */ |
| 1877 | nofs_flag = memalloc_nofs_save(); |
| 1878 | |
| 1879 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
| 1880 | if (!rkobj) { |
| 1881 | memalloc_nofs_restore(flags: nofs_flag); |
| 1882 | btrfs_warn(cache->fs_info, |
| 1883 | "couldn't alloc memory for raid level kobject" ); |
| 1884 | return; |
| 1885 | } |
| 1886 | |
| 1887 | rkobj->flags = cache->flags; |
| 1888 | kobject_init(kobj: &rkobj->kobj, ktype: &btrfs_raid_ktype); |
| 1889 | |
| 1890 | /* |
| 1891 | * We call this either on mount, or if we've created a block group for a |
| 1892 | * new index type while running (i.e. when restriping). The running |
| 1893 | * case is tricky because we could race with other threads, so we need |
| 1894 | * to have this check to make sure we didn't already init the kobject. |
| 1895 | * |
| 1896 | * We don't have to protect on the free side because it only happens on |
| 1897 | * unmount. |
| 1898 | */ |
| 1899 | spin_lock(lock: &space_info->lock); |
| 1900 | if (space_info->block_group_kobjs[index]) { |
| 1901 | spin_unlock(lock: &space_info->lock); |
| 1902 | kobject_put(kobj: &rkobj->kobj); |
| 1903 | return; |
| 1904 | } else { |
| 1905 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
| 1906 | } |
| 1907 | spin_unlock(lock: &space_info->lock); |
| 1908 | |
| 1909 | ret = kobject_add(kobj: &rkobj->kobj, parent: &space_info->kobj, fmt: "%s" , |
| 1910 | btrfs_bg_type_to_raid_name(flags: rkobj->flags)); |
| 1911 | memalloc_nofs_restore(flags: nofs_flag); |
| 1912 | if (ret) { |
| 1913 | spin_lock(lock: &space_info->lock); |
| 1914 | space_info->block_group_kobjs[index] = NULL; |
| 1915 | spin_unlock(lock: &space_info->lock); |
| 1916 | kobject_put(kobj: &rkobj->kobj); |
| 1917 | btrfs_warn(fs_info, |
| 1918 | "failed to add kobject for block cache, ignoring" ); |
| 1919 | return; |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | /* |
| 1924 | * Remove sysfs directories for all block group types of a given space info and |
| 1925 | * the space info as well |
| 1926 | */ |
| 1927 | void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info) |
| 1928 | { |
| 1929 | int i; |
| 1930 | |
| 1931 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 1932 | struct kobject *kobj; |
| 1933 | |
| 1934 | kobj = space_info->block_group_kobjs[i]; |
| 1935 | space_info->block_group_kobjs[i] = NULL; |
| 1936 | if (kobj) { |
| 1937 | kobject_del(kobj); |
| 1938 | kobject_put(kobj); |
| 1939 | } |
| 1940 | } |
| 1941 | kobject_del(kobj: &space_info->kobj); |
| 1942 | kobject_put(kobj: &space_info->kobj); |
| 1943 | } |
| 1944 | |
| 1945 | static const char *alloc_name(struct btrfs_space_info *space_info) |
| 1946 | { |
| 1947 | u64 flags = space_info->flags; |
| 1948 | |
| 1949 | switch (flags) { |
| 1950 | case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA: |
| 1951 | return "mixed" ; |
| 1952 | case BTRFS_BLOCK_GROUP_METADATA: |
| 1953 | switch (space_info->subgroup_id) { |
| 1954 | case BTRFS_SUB_GROUP_PRIMARY: |
| 1955 | return "metadata" ; |
| 1956 | case BTRFS_SUB_GROUP_TREELOG: |
| 1957 | return "metadata-treelog" ; |
| 1958 | default: |
| 1959 | WARN_ON_ONCE(1); |
| 1960 | return "metadata (unknown sub-group)" ; |
| 1961 | } |
| 1962 | case BTRFS_BLOCK_GROUP_DATA: |
| 1963 | switch (space_info->subgroup_id) { |
| 1964 | case BTRFS_SUB_GROUP_PRIMARY: |
| 1965 | return "data" ; |
| 1966 | case BTRFS_SUB_GROUP_DATA_RELOC: |
| 1967 | return "data-reloc" ; |
| 1968 | default: |
| 1969 | WARN_ON_ONCE(1); |
| 1970 | return "data (unknown sub-group)" ; |
| 1971 | } |
| 1972 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 1973 | ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_PRIMARY); |
| 1974 | return "system" ; |
| 1975 | default: |
| 1976 | WARN_ON(1); |
| 1977 | return "invalid-combination" ; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * Create a sysfs entry for a space info type at path |
| 1983 | * /sys/fs/btrfs/UUID/allocation/TYPE |
| 1984 | */ |
| 1985 | int btrfs_sysfs_add_space_info_type(struct btrfs_space_info *space_info) |
| 1986 | { |
| 1987 | int ret; |
| 1988 | |
| 1989 | ret = kobject_init_and_add(kobj: &space_info->kobj, ktype: &space_info_ktype, |
| 1990 | parent: space_info->fs_info->space_info_kobj, fmt: "%s" , |
| 1991 | alloc_name(space_info)); |
| 1992 | if (ret) { |
| 1993 | kobject_put(kobj: &space_info->kobj); |
| 1994 | return ret; |
| 1995 | } |
| 1996 | |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | void btrfs_sysfs_remove_device(struct btrfs_device *device) |
| 2001 | { |
| 2002 | struct kobject *devices_kobj; |
| 2003 | |
| 2004 | /* |
| 2005 | * Seed fs_devices devices_kobj aren't used, fetch kobject from the |
| 2006 | * fs_info::fs_devices. |
| 2007 | */ |
| 2008 | devices_kobj = device->fs_info->fs_devices->devices_kobj; |
| 2009 | ASSERT(devices_kobj); |
| 2010 | |
| 2011 | if (device->bdev) |
| 2012 | sysfs_remove_link(kobj: devices_kobj, bdev_kobj(device->bdev)->name); |
| 2013 | |
| 2014 | if (device->devid_kobj.state_initialized) { |
| 2015 | kobject_del(kobj: &device->devid_kobj); |
| 2016 | kobject_put(kobj: &device->devid_kobj); |
| 2017 | wait_for_completion(&device->kobj_unregister); |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj, |
| 2022 | struct kobj_attribute *a, |
| 2023 | char *buf) |
| 2024 | { |
| 2025 | int val; |
| 2026 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2027 | devid_kobj); |
| 2028 | |
| 2029 | val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
| 2030 | |
| 2031 | return sysfs_emit(buf, fmt: "%d\n" , val); |
| 2032 | } |
| 2033 | BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show); |
| 2034 | |
| 2035 | static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj, |
| 2036 | struct kobj_attribute *a, char *buf) |
| 2037 | { |
| 2038 | int val; |
| 2039 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2040 | devid_kobj); |
| 2041 | |
| 2042 | val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
| 2043 | |
| 2044 | return sysfs_emit(buf, fmt: "%d\n" , val); |
| 2045 | } |
| 2046 | BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show); |
| 2047 | |
| 2048 | static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj, |
| 2049 | struct kobj_attribute *a, |
| 2050 | char *buf) |
| 2051 | { |
| 2052 | int val; |
| 2053 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2054 | devid_kobj); |
| 2055 | |
| 2056 | val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
| 2057 | |
| 2058 | return sysfs_emit(buf, fmt: "%d\n" , val); |
| 2059 | } |
| 2060 | BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show); |
| 2061 | |
| 2062 | static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj, |
| 2063 | struct kobj_attribute *a, |
| 2064 | char *buf) |
| 2065 | { |
| 2066 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2067 | devid_kobj); |
| 2068 | |
| 2069 | return sysfs_emit(buf, fmt: "%llu\n" , READ_ONCE(device->scrub_speed_max)); |
| 2070 | } |
| 2071 | |
| 2072 | static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj, |
| 2073 | struct kobj_attribute *a, |
| 2074 | const char *buf, size_t len) |
| 2075 | { |
| 2076 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2077 | devid_kobj); |
| 2078 | char *endptr; |
| 2079 | unsigned long long limit; |
| 2080 | |
| 2081 | limit = memparse(ptr: buf, retptr: &endptr); |
| 2082 | /* There could be trailing '\n', also catch any typos after the value. */ |
| 2083 | endptr = skip_spaces(endptr); |
| 2084 | if (*endptr != 0) |
| 2085 | return -EINVAL; |
| 2086 | WRITE_ONCE(device->scrub_speed_max, limit); |
| 2087 | return len; |
| 2088 | } |
| 2089 | BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show, |
| 2090 | btrfs_devinfo_scrub_speed_max_store); |
| 2091 | |
| 2092 | static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj, |
| 2093 | struct kobj_attribute *a, char *buf) |
| 2094 | { |
| 2095 | int val; |
| 2096 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2097 | devid_kobj); |
| 2098 | |
| 2099 | val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 2100 | |
| 2101 | return sysfs_emit(buf, fmt: "%d\n" , val); |
| 2102 | } |
| 2103 | BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show); |
| 2104 | |
| 2105 | static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj, |
| 2106 | struct kobj_attribute *a, char *buf) |
| 2107 | { |
| 2108 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2109 | devid_kobj); |
| 2110 | |
| 2111 | return sysfs_emit(buf, fmt: "%pU\n" , device->fs_devices->fsid); |
| 2112 | } |
| 2113 | BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show); |
| 2114 | |
| 2115 | static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj, |
| 2116 | struct kobj_attribute *a, char *buf) |
| 2117 | { |
| 2118 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2119 | devid_kobj); |
| 2120 | |
| 2121 | if (!device->dev_stats_valid) |
| 2122 | return sysfs_emit(buf, fmt: "invalid\n" ); |
| 2123 | |
| 2124 | /* |
| 2125 | * Print all at once so we get a snapshot of all values from the same |
| 2126 | * time. Keep them in sync and in order of definition of |
| 2127 | * btrfs_dev_stat_values. |
| 2128 | */ |
| 2129 | return sysfs_emit(buf, |
| 2130 | fmt: "write_errs %d\n" |
| 2131 | "read_errs %d\n" |
| 2132 | "flush_errs %d\n" |
| 2133 | "corruption_errs %d\n" |
| 2134 | "generation_errs %d\n" , |
| 2135 | btrfs_dev_stat_read(dev: device, index: BTRFS_DEV_STAT_WRITE_ERRS), |
| 2136 | btrfs_dev_stat_read(dev: device, index: BTRFS_DEV_STAT_READ_ERRS), |
| 2137 | btrfs_dev_stat_read(dev: device, index: BTRFS_DEV_STAT_FLUSH_ERRS), |
| 2138 | btrfs_dev_stat_read(dev: device, index: BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 2139 | btrfs_dev_stat_read(dev: device, index: BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 2140 | } |
| 2141 | BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show); |
| 2142 | |
| 2143 | /* |
| 2144 | * Information about one device. |
| 2145 | * |
| 2146 | * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/ |
| 2147 | */ |
| 2148 | static struct attribute *devid_attrs[] = { |
| 2149 | BTRFS_ATTR_PTR(devid, error_stats), |
| 2150 | BTRFS_ATTR_PTR(devid, fsid), |
| 2151 | BTRFS_ATTR_PTR(devid, in_fs_metadata), |
| 2152 | BTRFS_ATTR_PTR(devid, missing), |
| 2153 | BTRFS_ATTR_PTR(devid, replace_target), |
| 2154 | BTRFS_ATTR_PTR(devid, scrub_speed_max), |
| 2155 | BTRFS_ATTR_PTR(devid, writeable), |
| 2156 | NULL |
| 2157 | }; |
| 2158 | ATTRIBUTE_GROUPS(devid); |
| 2159 | |
| 2160 | static void btrfs_release_devid_kobj(struct kobject *kobj) |
| 2161 | { |
| 2162 | struct btrfs_device *device = container_of(kobj, struct btrfs_device, |
| 2163 | devid_kobj); |
| 2164 | |
| 2165 | memset(&device->devid_kobj, 0, sizeof(struct kobject)); |
| 2166 | complete(&device->kobj_unregister); |
| 2167 | } |
| 2168 | |
| 2169 | static const struct kobj_type devid_ktype = { |
| 2170 | .sysfs_ops = &kobj_sysfs_ops, |
| 2171 | .default_groups = devid_groups, |
| 2172 | .release = btrfs_release_devid_kobj, |
| 2173 | }; |
| 2174 | |
| 2175 | int btrfs_sysfs_add_device(struct btrfs_device *device) |
| 2176 | { |
| 2177 | int ret; |
| 2178 | unsigned int nofs_flag; |
| 2179 | struct kobject *devices_kobj; |
| 2180 | struct kobject *devinfo_kobj; |
| 2181 | |
| 2182 | /* |
| 2183 | * Make sure we use the fs_info::fs_devices to fetch the kobjects even |
| 2184 | * for the seed fs_devices |
| 2185 | */ |
| 2186 | devices_kobj = device->fs_info->fs_devices->devices_kobj; |
| 2187 | devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj; |
| 2188 | ASSERT(devices_kobj); |
| 2189 | ASSERT(devinfo_kobj); |
| 2190 | |
| 2191 | nofs_flag = memalloc_nofs_save(); |
| 2192 | |
| 2193 | if (device->bdev) { |
| 2194 | struct kobject *disk_kobj = bdev_kobj(device->bdev); |
| 2195 | |
| 2196 | ret = sysfs_create_link(kobj: devices_kobj, target: disk_kobj, name: disk_kobj->name); |
| 2197 | if (ret) { |
| 2198 | btrfs_warn(device->fs_info, |
| 2199 | "creating sysfs device link for devid %llu failed: %d" , |
| 2200 | device->devid, ret); |
| 2201 | goto out; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | init_completion(x: &device->kobj_unregister); |
| 2206 | ret = kobject_init_and_add(kobj: &device->devid_kobj, ktype: &devid_ktype, |
| 2207 | parent: devinfo_kobj, fmt: "%llu" , device->devid); |
| 2208 | if (ret) { |
| 2209 | kobject_put(kobj: &device->devid_kobj); |
| 2210 | btrfs_warn(device->fs_info, |
| 2211 | "devinfo init for devid %llu failed: %d" , |
| 2212 | device->devid, ret); |
| 2213 | } |
| 2214 | |
| 2215 | out: |
| 2216 | memalloc_nofs_restore(flags: nofs_flag); |
| 2217 | return ret; |
| 2218 | } |
| 2219 | |
| 2220 | static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 2221 | { |
| 2222 | int ret; |
| 2223 | struct btrfs_device *device; |
| 2224 | struct btrfs_fs_devices *seed; |
| 2225 | |
| 2226 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 2227 | ret = btrfs_sysfs_add_device(device); |
| 2228 | if (ret) |
| 2229 | goto fail; |
| 2230 | } |
| 2231 | |
| 2232 | list_for_each_entry(seed, &fs_devices->seed_list, seed_list) { |
| 2233 | list_for_each_entry(device, &seed->devices, dev_list) { |
| 2234 | ret = btrfs_sysfs_add_device(device); |
| 2235 | if (ret) |
| 2236 | goto fail; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | return 0; |
| 2241 | |
| 2242 | fail: |
| 2243 | btrfs_sysfs_remove_fs_devices(fs_devices); |
| 2244 | return ret; |
| 2245 | } |
| 2246 | |
| 2247 | void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action) |
| 2248 | { |
| 2249 | int ret; |
| 2250 | |
| 2251 | ret = kobject_uevent(kobj: &disk_to_dev(bdev->bd_disk)->kobj, action); |
| 2252 | if (ret) |
| 2253 | btrfs_warn(NULL, "sending event %d to kobject: '%s' (%p): failed" , |
| 2254 | action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 2255 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 2256 | } |
| 2257 | |
| 2258 | void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices) |
| 2259 | |
| 2260 | { |
| 2261 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2262 | |
| 2263 | /* |
| 2264 | * Sprouting changes fsid of the mounted filesystem, rename the fsid |
| 2265 | * directory |
| 2266 | */ |
| 2267 | snprintf(buf: fsid_buf, BTRFS_UUID_UNPARSED_SIZE, fmt: "%pU" , fs_devices->fsid); |
| 2268 | if (kobject_rename(&fs_devices->fsid_kobj, new_name: fsid_buf)) |
| 2269 | btrfs_warn(fs_devices->fs_info, |
| 2270 | "sysfs: failed to create fsid for sprout" ); |
| 2271 | } |
| 2272 | |
| 2273 | void btrfs_sysfs_update_devid(struct btrfs_device *device) |
| 2274 | { |
| 2275 | char tmp[24]; |
| 2276 | |
| 2277 | snprintf(buf: tmp, size: sizeof(tmp), fmt: "%llu" , device->devid); |
| 2278 | |
| 2279 | if (kobject_rename(&device->devid_kobj, new_name: tmp)) |
| 2280 | btrfs_warn(device->fs_devices->fs_info, |
| 2281 | "sysfs: failed to update devid for %llu" , |
| 2282 | device->devid); |
| 2283 | } |
| 2284 | |
| 2285 | /* /sys/fs/btrfs/ entry */ |
| 2286 | static struct kset *btrfs_kset; |
| 2287 | |
| 2288 | /* |
| 2289 | * Creates: |
| 2290 | * /sys/fs/btrfs/UUID |
| 2291 | * |
| 2292 | * Can be called by the device discovery thread. |
| 2293 | */ |
| 2294 | int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs) |
| 2295 | { |
| 2296 | int ret; |
| 2297 | |
| 2298 | init_completion(x: &fs_devs->kobj_unregister); |
| 2299 | fs_devs->fsid_kobj.kset = btrfs_kset; |
| 2300 | ret = kobject_init_and_add(kobj: &fs_devs->fsid_kobj, ktype: &btrfs_ktype, NULL, |
| 2301 | fmt: "%pU" , fs_devs->fsid); |
| 2302 | if (ret) { |
| 2303 | kobject_put(kobj: &fs_devs->fsid_kobj); |
| 2304 | return ret; |
| 2305 | } |
| 2306 | |
| 2307 | fs_devs->devices_kobj = kobject_create_and_add(name: "devices" , |
| 2308 | parent: &fs_devs->fsid_kobj); |
| 2309 | if (!fs_devs->devices_kobj) { |
| 2310 | btrfs_err(fs_devs->fs_info, |
| 2311 | "failed to init sysfs device interface" ); |
| 2312 | btrfs_sysfs_remove_fsid(fs_devs); |
| 2313 | return -ENOMEM; |
| 2314 | } |
| 2315 | |
| 2316 | fs_devs->devinfo_kobj = kobject_create_and_add(name: "devinfo" , |
| 2317 | parent: &fs_devs->fsid_kobj); |
| 2318 | if (!fs_devs->devinfo_kobj) { |
| 2319 | btrfs_err(fs_devs->fs_info, |
| 2320 | "failed to init sysfs devinfo kobject" ); |
| 2321 | btrfs_sysfs_remove_fsid(fs_devs); |
| 2322 | return -ENOMEM; |
| 2323 | } |
| 2324 | |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info) |
| 2329 | { |
| 2330 | int ret; |
| 2331 | struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; |
| 2332 | struct kobject *fsid_kobj = &fs_devs->fsid_kobj; |
| 2333 | |
| 2334 | ret = btrfs_sysfs_add_fs_devices(fs_devices: fs_devs); |
| 2335 | if (ret) |
| 2336 | return ret; |
| 2337 | |
| 2338 | ret = sysfs_create_files(kobj: fsid_kobj, attr: btrfs_attrs); |
| 2339 | if (ret) { |
| 2340 | btrfs_sysfs_remove_fs_devices(fs_devices: fs_devs); |
| 2341 | return ret; |
| 2342 | } |
| 2343 | |
| 2344 | ret = sysfs_create_group(kobj: fsid_kobj, grp: &btrfs_feature_attr_group); |
| 2345 | if (ret) |
| 2346 | goto failure; |
| 2347 | |
| 2348 | #ifdef CONFIG_BTRFS_DEBUG |
| 2349 | fs_info->debug_kobj = kobject_create_and_add(name: "debug" , parent: fsid_kobj); |
| 2350 | if (!fs_info->debug_kobj) { |
| 2351 | ret = -ENOMEM; |
| 2352 | goto failure; |
| 2353 | } |
| 2354 | |
| 2355 | ret = sysfs_create_files(kobj: fs_info->debug_kobj, attr: btrfs_debug_mount_attrs); |
| 2356 | if (ret) |
| 2357 | goto failure; |
| 2358 | #endif |
| 2359 | |
| 2360 | /* Discard directory */ |
| 2361 | fs_info->discard_kobj = kobject_create_and_add(name: "discard" , parent: fsid_kobj); |
| 2362 | if (!fs_info->discard_kobj) { |
| 2363 | ret = -ENOMEM; |
| 2364 | goto failure; |
| 2365 | } |
| 2366 | |
| 2367 | ret = sysfs_create_files(kobj: fs_info->discard_kobj, attr: discard_attrs); |
| 2368 | if (ret) |
| 2369 | goto failure; |
| 2370 | |
| 2371 | ret = addrm_unknown_feature_attrs(fs_info, add: true); |
| 2372 | if (ret) |
| 2373 | goto failure; |
| 2374 | |
| 2375 | ret = sysfs_create_link(kobj: fsid_kobj, target: &fs_info->sb->s_bdi->dev->kobj, name: "bdi" ); |
| 2376 | if (ret) |
| 2377 | goto failure; |
| 2378 | |
| 2379 | fs_info->space_info_kobj = kobject_create_and_add(name: "allocation" , |
| 2380 | parent: fsid_kobj); |
| 2381 | if (!fs_info->space_info_kobj) { |
| 2382 | ret = -ENOMEM; |
| 2383 | goto failure; |
| 2384 | } |
| 2385 | |
| 2386 | ret = sysfs_create_files(kobj: fs_info->space_info_kobj, attr: allocation_attrs); |
| 2387 | if (ret) |
| 2388 | goto failure; |
| 2389 | |
| 2390 | return 0; |
| 2391 | failure: |
| 2392 | btrfs_sysfs_remove_mounted(fs_info); |
| 2393 | return ret; |
| 2394 | } |
| 2395 | |
| 2396 | static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj, |
| 2397 | struct kobj_attribute *a, |
| 2398 | char *buf) |
| 2399 | { |
| 2400 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: qgroups_kobj->parent); |
| 2401 | bool enabled; |
| 2402 | |
| 2403 | spin_lock(lock: &fs_info->qgroup_lock); |
| 2404 | enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON; |
| 2405 | spin_unlock(lock: &fs_info->qgroup_lock); |
| 2406 | |
| 2407 | return sysfs_emit(buf, fmt: "%d\n" , enabled); |
| 2408 | } |
| 2409 | BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show); |
| 2410 | |
| 2411 | static ssize_t qgroup_mode_show(struct kobject *qgroups_kobj, |
| 2412 | struct kobj_attribute *a, |
| 2413 | char *buf) |
| 2414 | { |
| 2415 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: qgroups_kobj->parent); |
| 2416 | ssize_t ret = 0; |
| 2417 | |
| 2418 | spin_lock(lock: &fs_info->qgroup_lock); |
| 2419 | ASSERT(btrfs_qgroup_enabled(fs_info)); |
| 2420 | switch (btrfs_qgroup_mode(fs_info)) { |
| 2421 | case BTRFS_QGROUP_MODE_FULL: |
| 2422 | ret = sysfs_emit(buf, fmt: "qgroup\n" ); |
| 2423 | break; |
| 2424 | case BTRFS_QGROUP_MODE_SIMPLE: |
| 2425 | ret = sysfs_emit(buf, fmt: "squota\n" ); |
| 2426 | break; |
| 2427 | default: |
| 2428 | btrfs_warn(fs_info, "unexpected qgroup mode %d\n" , |
| 2429 | btrfs_qgroup_mode(fs_info)); |
| 2430 | break; |
| 2431 | } |
| 2432 | spin_unlock(lock: &fs_info->qgroup_lock); |
| 2433 | |
| 2434 | return ret; |
| 2435 | } |
| 2436 | BTRFS_ATTR(qgroups, mode, qgroup_mode_show); |
| 2437 | |
| 2438 | static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj, |
| 2439 | struct kobj_attribute *a, |
| 2440 | char *buf) |
| 2441 | { |
| 2442 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: qgroups_kobj->parent); |
| 2443 | bool inconsistent; |
| 2444 | |
| 2445 | spin_lock(lock: &fs_info->qgroup_lock); |
| 2446 | inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT); |
| 2447 | spin_unlock(lock: &fs_info->qgroup_lock); |
| 2448 | |
| 2449 | return sysfs_emit(buf, fmt: "%d\n" , inconsistent); |
| 2450 | } |
| 2451 | BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show); |
| 2452 | |
| 2453 | static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj, |
| 2454 | struct kobj_attribute *a, |
| 2455 | char *buf) |
| 2456 | { |
| 2457 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: qgroups_kobj->parent); |
| 2458 | u8 result; |
| 2459 | |
| 2460 | spin_lock(lock: &fs_info->qgroup_lock); |
| 2461 | result = fs_info->qgroup_drop_subtree_thres; |
| 2462 | spin_unlock(lock: &fs_info->qgroup_lock); |
| 2463 | |
| 2464 | return sysfs_emit(buf, fmt: "%d\n" , result); |
| 2465 | } |
| 2466 | |
| 2467 | static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj, |
| 2468 | struct kobj_attribute *a, |
| 2469 | const char *buf, size_t len) |
| 2470 | { |
| 2471 | struct btrfs_fs_info *fs_info = to_fs_info(kobj: qgroups_kobj->parent); |
| 2472 | u8 new_thres; |
| 2473 | int ret; |
| 2474 | |
| 2475 | ret = kstrtou8(s: buf, base: 10, res: &new_thres); |
| 2476 | if (ret) |
| 2477 | return -EINVAL; |
| 2478 | |
| 2479 | if (new_thres > BTRFS_MAX_LEVEL) |
| 2480 | return -EINVAL; |
| 2481 | |
| 2482 | spin_lock(lock: &fs_info->qgroup_lock); |
| 2483 | fs_info->qgroup_drop_subtree_thres = new_thres; |
| 2484 | spin_unlock(lock: &fs_info->qgroup_lock); |
| 2485 | |
| 2486 | return len; |
| 2487 | } |
| 2488 | BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show, |
| 2489 | qgroup_drop_subtree_thres_store); |
| 2490 | |
| 2491 | /* |
| 2492 | * Qgroups global info |
| 2493 | * |
| 2494 | * Path: /sys/fs/btrfs/<uuid>/qgroups/ |
| 2495 | */ |
| 2496 | static struct attribute *qgroups_attrs[] = { |
| 2497 | BTRFS_ATTR_PTR(qgroups, enabled), |
| 2498 | BTRFS_ATTR_PTR(qgroups, inconsistent), |
| 2499 | BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold), |
| 2500 | BTRFS_ATTR_PTR(qgroups, mode), |
| 2501 | NULL |
| 2502 | }; |
| 2503 | ATTRIBUTE_GROUPS(qgroups); |
| 2504 | |
| 2505 | static void qgroups_release(struct kobject *kobj) |
| 2506 | { |
| 2507 | kfree(objp: kobj); |
| 2508 | } |
| 2509 | |
| 2510 | static const struct kobj_type qgroups_ktype = { |
| 2511 | .sysfs_ops = &kobj_sysfs_ops, |
| 2512 | .default_groups = qgroups_groups, |
| 2513 | .release = qgroups_release, |
| 2514 | }; |
| 2515 | |
| 2516 | static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj) |
| 2517 | { |
| 2518 | return to_fs_info(kobj: kobj->parent->parent); |
| 2519 | } |
| 2520 | |
| 2521 | #define QGROUP_ATTR(_member, _show_name) \ |
| 2522 | static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \ |
| 2523 | struct kobj_attribute *a, \ |
| 2524 | char *buf) \ |
| 2525 | { \ |
| 2526 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 2527 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 2528 | struct btrfs_qgroup, kobj); \ |
| 2529 | return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \ |
| 2530 | } \ |
| 2531 | BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member) |
| 2532 | |
| 2533 | #define QGROUP_RSV_ATTR(_name, _type) \ |
| 2534 | static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \ |
| 2535 | struct kobj_attribute *a, \ |
| 2536 | char *buf) \ |
| 2537 | { \ |
| 2538 | struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \ |
| 2539 | struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \ |
| 2540 | struct btrfs_qgroup, kobj); \ |
| 2541 | return btrfs_show_u64(&qgroup->rsv.values[_type], \ |
| 2542 | &fs_info->qgroup_lock, buf); \ |
| 2543 | } \ |
| 2544 | BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name) |
| 2545 | |
| 2546 | QGROUP_ATTR(rfer, referenced); |
| 2547 | QGROUP_ATTR(excl, exclusive); |
| 2548 | QGROUP_ATTR(max_rfer, max_referenced); |
| 2549 | QGROUP_ATTR(max_excl, max_exclusive); |
| 2550 | QGROUP_ATTR(lim_flags, limit_flags); |
| 2551 | QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA); |
| 2552 | QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS); |
| 2553 | QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC); |
| 2554 | |
| 2555 | /* |
| 2556 | * Qgroup information. |
| 2557 | * |
| 2558 | * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/ |
| 2559 | */ |
| 2560 | static struct attribute *qgroup_attrs[] = { |
| 2561 | BTRFS_ATTR_PTR(qgroup, referenced), |
| 2562 | BTRFS_ATTR_PTR(qgroup, exclusive), |
| 2563 | BTRFS_ATTR_PTR(qgroup, max_referenced), |
| 2564 | BTRFS_ATTR_PTR(qgroup, max_exclusive), |
| 2565 | BTRFS_ATTR_PTR(qgroup, limit_flags), |
| 2566 | BTRFS_ATTR_PTR(qgroup, rsv_data), |
| 2567 | BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans), |
| 2568 | BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc), |
| 2569 | NULL |
| 2570 | }; |
| 2571 | ATTRIBUTE_GROUPS(qgroup); |
| 2572 | |
| 2573 | static void qgroup_release(struct kobject *kobj) |
| 2574 | { |
| 2575 | struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj); |
| 2576 | |
| 2577 | memset(&qgroup->kobj, 0, sizeof(*kobj)); |
| 2578 | } |
| 2579 | |
| 2580 | static const struct kobj_type qgroup_ktype = { |
| 2581 | .sysfs_ops = &kobj_sysfs_ops, |
| 2582 | .release = qgroup_release, |
| 2583 | .default_groups = qgroup_groups, |
| 2584 | }; |
| 2585 | |
| 2586 | int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info, |
| 2587 | struct btrfs_qgroup *qgroup) |
| 2588 | { |
| 2589 | struct kobject *qgroups_kobj = fs_info->qgroups_kobj; |
| 2590 | int ret; |
| 2591 | |
| 2592 | if (btrfs_is_testing(fs_info)) |
| 2593 | return 0; |
| 2594 | if (qgroup->kobj.state_initialized) |
| 2595 | return 0; |
| 2596 | if (!qgroups_kobj) |
| 2597 | return -EINVAL; |
| 2598 | |
| 2599 | ret = kobject_init_and_add(kobj: &qgroup->kobj, ktype: &qgroup_ktype, parent: qgroups_kobj, |
| 2600 | fmt: "%hu_%llu" , btrfs_qgroup_level(qgroupid: qgroup->qgroupid), |
| 2601 | btrfs_qgroup_subvolid(qgroupid: qgroup->qgroupid)); |
| 2602 | if (ret < 0) |
| 2603 | kobject_put(kobj: &qgroup->kobj); |
| 2604 | |
| 2605 | return ret; |
| 2606 | } |
| 2607 | |
| 2608 | void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info) |
| 2609 | { |
| 2610 | struct btrfs_qgroup *qgroup; |
| 2611 | struct btrfs_qgroup *next; |
| 2612 | |
| 2613 | if (btrfs_is_testing(fs_info)) |
| 2614 | return; |
| 2615 | |
| 2616 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 2617 | &fs_info->qgroup_tree, node) |
| 2618 | btrfs_sysfs_del_one_qgroup(fs_info, qgroup); |
| 2619 | if (fs_info->qgroups_kobj) { |
| 2620 | kobject_del(kobj: fs_info->qgroups_kobj); |
| 2621 | kobject_put(kobj: fs_info->qgroups_kobj); |
| 2622 | fs_info->qgroups_kobj = NULL; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | /* Called when qgroups get initialized, thus there is no need for locking */ |
| 2627 | int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info) |
| 2628 | { |
| 2629 | struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 2630 | struct btrfs_qgroup *qgroup; |
| 2631 | struct btrfs_qgroup *next; |
| 2632 | int ret = 0; |
| 2633 | |
| 2634 | if (btrfs_is_testing(fs_info)) |
| 2635 | return 0; |
| 2636 | |
| 2637 | ASSERT(fsid_kobj); |
| 2638 | if (fs_info->qgroups_kobj) |
| 2639 | return 0; |
| 2640 | |
| 2641 | fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL); |
| 2642 | if (!fs_info->qgroups_kobj) |
| 2643 | return -ENOMEM; |
| 2644 | |
| 2645 | ret = kobject_init_and_add(kobj: fs_info->qgroups_kobj, ktype: &qgroups_ktype, |
| 2646 | parent: fsid_kobj, fmt: "qgroups" ); |
| 2647 | if (ret < 0) |
| 2648 | goto out; |
| 2649 | |
| 2650 | rbtree_postorder_for_each_entry_safe(qgroup, next, |
| 2651 | &fs_info->qgroup_tree, node) { |
| 2652 | ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup); |
| 2653 | if (ret < 0) |
| 2654 | goto out; |
| 2655 | } |
| 2656 | |
| 2657 | out: |
| 2658 | if (ret < 0) |
| 2659 | btrfs_sysfs_del_qgroups(fs_info); |
| 2660 | return ret; |
| 2661 | } |
| 2662 | |
| 2663 | void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info, |
| 2664 | struct btrfs_qgroup *qgroup) |
| 2665 | { |
| 2666 | if (btrfs_is_testing(fs_info)) |
| 2667 | return; |
| 2668 | |
| 2669 | if (qgroup->kobj.state_initialized) { |
| 2670 | kobject_del(kobj: &qgroup->kobj); |
| 2671 | kobject_put(kobj: &qgroup->kobj); |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | /* |
| 2676 | * Change per-fs features in /sys/fs/btrfs/UUID/features to match current |
| 2677 | * values in superblock. Call after any changes to incompat/compat_ro flags |
| 2678 | */ |
| 2679 | void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info) |
| 2680 | { |
| 2681 | struct kobject *fsid_kobj; |
| 2682 | int ret; |
| 2683 | |
| 2684 | if (!fs_info) |
| 2685 | return; |
| 2686 | |
| 2687 | fsid_kobj = &fs_info->fs_devices->fsid_kobj; |
| 2688 | if (!fsid_kobj->state_initialized) |
| 2689 | return; |
| 2690 | |
| 2691 | ret = sysfs_update_group(kobj: fsid_kobj, grp: &btrfs_feature_attr_group); |
| 2692 | if (ret < 0) |
| 2693 | btrfs_warn(fs_info, |
| 2694 | "failed to update /sys/fs/btrfs/%pU/features: %d" , |
| 2695 | fs_info->fs_devices->fsid, ret); |
| 2696 | } |
| 2697 | |
| 2698 | int __init btrfs_init_sysfs(void) |
| 2699 | { |
| 2700 | int ret; |
| 2701 | |
| 2702 | btrfs_kset = kset_create_and_add(name: "btrfs" , NULL, parent_kobj: fs_kobj); |
| 2703 | if (!btrfs_kset) |
| 2704 | return -ENOMEM; |
| 2705 | |
| 2706 | init_feature_attrs(); |
| 2707 | ret = sysfs_create_group(kobj: &btrfs_kset->kobj, grp: &btrfs_feature_attr_group); |
| 2708 | if (ret) |
| 2709 | goto out2; |
| 2710 | ret = sysfs_merge_group(kobj: &btrfs_kset->kobj, |
| 2711 | grp: &btrfs_static_feature_attr_group); |
| 2712 | if (ret) |
| 2713 | goto out_remove_group; |
| 2714 | |
| 2715 | #ifdef CONFIG_BTRFS_DEBUG |
| 2716 | ret = sysfs_create_group(kobj: &btrfs_kset->kobj, grp: &btrfs_debug_feature_attr_group); |
| 2717 | if (ret) { |
| 2718 | sysfs_unmerge_group(kobj: &btrfs_kset->kobj, |
| 2719 | grp: &btrfs_static_feature_attr_group); |
| 2720 | goto out_remove_group; |
| 2721 | } |
| 2722 | #endif |
| 2723 | |
| 2724 | return 0; |
| 2725 | |
| 2726 | out_remove_group: |
| 2727 | sysfs_remove_group(kobj: &btrfs_kset->kobj, grp: &btrfs_feature_attr_group); |
| 2728 | out2: |
| 2729 | kset_unregister(kset: btrfs_kset); |
| 2730 | |
| 2731 | return ret; |
| 2732 | } |
| 2733 | |
| 2734 | void __cold btrfs_exit_sysfs(void) |
| 2735 | { |
| 2736 | sysfs_unmerge_group(kobj: &btrfs_kset->kobj, |
| 2737 | grp: &btrfs_static_feature_attr_group); |
| 2738 | sysfs_remove_group(kobj: &btrfs_kset->kobj, grp: &btrfs_feature_attr_group); |
| 2739 | #ifdef CONFIG_BTRFS_DEBUG |
| 2740 | sysfs_remove_group(kobj: &btrfs_kset->kobj, grp: &btrfs_debug_feature_attr_group); |
| 2741 | #endif |
| 2742 | kset_unregister(kset: btrfs_kset); |
| 2743 | } |
| 2744 | |