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