blob: 279d9262b676d46f9f77db1d63af8e324f14ed7e [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Josef Bacik58176a92007-08-29 15:47:34 -04006#include <linux/sched.h>
David Sterba32a99912019-08-01 17:49:55 +02007#include <linux/sched/mm.h>
Josef Bacik58176a92007-08-29 15:47:34 -04008#include <linux/slab.h>
9#include <linux/spinlock.h>
10#include <linux/completion.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040011#include <linux/bug.h>
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +020012#include <crypto/hash.h>
Josef Bacik58176a92007-08-29 15:47:34 -040013
Chris Masonbae45de2007-04-04 21:22:22 -040014#include "ctree.h"
Dennis Zhoudfb79dd2019-12-13 16:22:20 -080015#include "discard.h"
Chris Masonbae45de2007-04-04 21:22:22 -040016#include "disk-io.h"
Omar Sandoval7573df52020-08-21 00:39:54 -070017#include "send.h"
Chris Masonbae45de2007-04-04 21:22:22 -040018#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040019#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040020#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040021#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040022#include "block-group.h"
Qu Wenruo49e5fb42020-06-28 13:07:15 +080023#include "qgroup.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040024
David Sterba9188db62019-08-02 12:52:48 +020025struct btrfs_feature_attr {
26 struct kobj_attribute kobj_attr;
27 enum btrfs_feature_set feature_set;
28 u64 feature_bit;
29};
30
31/* For raid type sysfs entries */
32struct raid_kobject {
33 u64 flags;
34 struct kobject kobj;
35};
36
37#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
38{ \
39 .attr = { .name = __stringify(_name), .mode = _mode }, \
40 .show = _show, \
41 .store = _store, \
42}
43
44#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
45 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
46 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
47
48#define BTRFS_ATTR(_prefix, _name, _show) \
49 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
50 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
51
52#define BTRFS_ATTR_PTR(_prefix, _name) \
53 (&btrfs_attr_##_prefix##_##_name.attr)
54
55#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
56static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
57 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
58 btrfs_feature_attr_show, \
59 btrfs_feature_attr_store), \
60 .feature_set = _feature_set, \
61 .feature_bit = _feature_prefix ##_## _feature_bit, \
62}
63#define BTRFS_FEAT_ATTR_PTR(_name) \
64 (&btrfs_attr_features_##_name.kobj_attr.attr)
65
66#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
68#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
69 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
70#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
71 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
72
Jeff Mahoney510d7362013-11-01 13:06:59 -040073static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080074static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040075
David Sterba8f523162019-08-02 13:07:38 +020076static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
77{
78 return container_of(a, struct btrfs_feature_attr, kobj_attr);
79}
80
81static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
82{
83 return container_of(attr, struct kobj_attribute, attr);
84}
85
86static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
87 struct attribute *attr)
88{
89 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
90}
91
Jeff Mahoney510d7362013-11-01 13:06:59 -040092static u64 get_features(struct btrfs_fs_info *fs_info,
93 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040094{
Jeff Mahoney510d7362013-11-01 13:06:59 -040095 struct btrfs_super_block *disk_super = fs_info->super_copy;
96 if (set == FEAT_COMPAT)
97 return btrfs_super_compat_flags(disk_super);
98 else if (set == FEAT_COMPAT_RO)
99 return btrfs_super_compat_ro_flags(disk_super);
100 else
101 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400102}
103
Jeff Mahoneyba631942013-11-01 13:07:01 -0400104static void set_features(struct btrfs_fs_info *fs_info,
105 enum btrfs_feature_set set, u64 features)
106{
107 struct btrfs_super_block *disk_super = fs_info->super_copy;
108 if (set == FEAT_COMPAT)
109 btrfs_set_super_compat_flags(disk_super, features);
110 else if (set == FEAT_COMPAT_RO)
111 btrfs_set_super_compat_ro_flags(disk_super, features);
112 else
113 btrfs_set_super_incompat_flags(disk_super, features);
114}
115
116static int can_modify_feature(struct btrfs_feature_attr *fa)
117{
118 int val = 0;
119 u64 set, clear;
120 switch (fa->feature_set) {
121 case FEAT_COMPAT:
122 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
123 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
124 break;
125 case FEAT_COMPAT_RO:
126 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
127 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
128 break;
129 case FEAT_INCOMPAT:
130 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
131 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
132 break;
133 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -0400134 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +0100135 fa->feature_set);
136 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400137 }
138
139 if (set & fa->feature_bit)
140 val |= 1;
141 if (clear & fa->feature_bit)
142 val |= 2;
143
144 return val;
145}
146
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400147static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
148 struct kobj_attribute *a, char *buf)
149{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400150 int val = 0;
151 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400152 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400153 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400154 u64 features = get_features(fs_info, fa->feature_set);
155 if (features & fa->feature_bit)
156 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400157 } else
158 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400159
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100160 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400161}
162
Jeff Mahoneyba631942013-11-01 13:07:01 -0400163static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
164 struct kobj_attribute *a,
165 const char *buf, size_t count)
166{
167 struct btrfs_fs_info *fs_info;
168 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400169 u64 features, set, clear;
170 unsigned long val;
171 int ret;
172
173 fs_info = to_fs_info(kobj);
174 if (!fs_info)
175 return -EPERM;
176
David Howellsbc98a422017-07-17 08:45:34 +0100177 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100178 return -EROFS;
179
Jeff Mahoneyba631942013-11-01 13:07:01 -0400180 ret = kstrtoul(skip_spaces(buf), 0, &val);
181 if (ret)
182 return ret;
183
184 if (fa->feature_set == FEAT_COMPAT) {
185 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
186 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
187 } else if (fa->feature_set == FEAT_COMPAT_RO) {
188 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
189 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
190 } else {
191 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
192 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
193 }
194
195 features = get_features(fs_info, fa->feature_set);
196
197 /* Nothing to do */
198 if ((val && (features & fa->feature_bit)) ||
199 (!val && !(features & fa->feature_bit)))
200 return count;
201
202 if ((val && !(set & fa->feature_bit)) ||
203 (!val && !(clear & fa->feature_bit))) {
204 btrfs_info(fs_info,
205 "%sabling feature %s on mounted fs is not supported.",
206 val ? "En" : "Dis", fa->kobj_attr.attr.name);
207 return -EPERM;
208 }
209
210 btrfs_info(fs_info, "%s %s feature flag",
211 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
212
Jeff Mahoneyba631942013-11-01 13:07:01 -0400213 spin_lock(&fs_info->super_lock);
214 features = get_features(fs_info, fa->feature_set);
215 if (val)
216 features |= fa->feature_bit;
217 else
218 features &= ~fa->feature_bit;
219 set_features(fs_info, fa->feature_set, features);
220 spin_unlock(&fs_info->super_lock);
221
David Sterba0eae2742014-11-12 14:22:21 +0100222 /*
223 * We don't want to do full transaction commit from inside sysfs
224 */
225 btrfs_set_pending(fs_info, COMMIT);
226 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400227
228 return count;
229}
230
Jeff Mahoney510d7362013-11-01 13:06:59 -0400231static umode_t btrfs_feature_visible(struct kobject *kobj,
232 struct attribute *attr, int unused)
233{
234 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
235 umode_t mode = attr->mode;
236
237 if (fs_info) {
238 struct btrfs_feature_attr *fa;
239 u64 features;
240
241 fa = attr_to_btrfs_feature_attr(attr);
242 features = get_features(fs_info, fa->feature_set);
243
Jeff Mahoneyba631942013-11-01 13:07:01 -0400244 if (can_modify_feature(fa))
245 mode |= S_IWUSR;
246 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400247 mode = 0;
248 }
249
250 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400251}
252
253BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
254BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
255BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
256BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700257BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400258BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
259BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
260BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
261BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100262BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200263BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100264BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
David Sterbacfbb8252018-07-10 18:15:05 +0200265BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400266
267static struct attribute *btrfs_supported_feature_attrs[] = {
268 BTRFS_FEAT_ATTR_PTR(mixed_backref),
269 BTRFS_FEAT_ATTR_PTR(default_subvol),
270 BTRFS_FEAT_ATTR_PTR(mixed_groups),
271 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700272 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400273 BTRFS_FEAT_ATTR_PTR(big_metadata),
274 BTRFS_FEAT_ATTR_PTR(extended_iref),
275 BTRFS_FEAT_ATTR_PTR(raid56),
276 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100277 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200278 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100279 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200280 BTRFS_FEAT_ATTR_PTR(raid1c34),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400281 NULL
282};
283
Misono Tomohirof902bd32018-05-17 14:24:51 +0900284/*
285 * Features which depend on feature bits and may differ between each fs.
286 *
287 * /sys/fs/btrfs/features lists all available features of this kernel while
288 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
289 * can be changed online.
290 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400291static const struct attribute_group btrfs_feature_attr_group = {
292 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400293 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400294 .attrs = btrfs_supported_feature_attrs,
295};
Josef Bacik58176a92007-08-29 15:47:34 -0400296
Misono Tomohirof902bd32018-05-17 14:24:51 +0900297static ssize_t rmdir_subvol_show(struct kobject *kobj,
298 struct kobj_attribute *ka, char *buf)
299{
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100300 return scnprintf(buf, PAGE_SIZE, "0\n");
Misono Tomohirof902bd32018-05-17 14:24:51 +0900301}
302BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
303
David Sterbaf7cea562019-10-07 11:11:03 +0200304static ssize_t supported_checksums_show(struct kobject *kobj,
305 struct kobj_attribute *a, char *buf)
306{
307 ssize_t ret = 0;
308 int i;
309
310 for (i = 0; i < btrfs_get_num_csums(); i++) {
311 /*
312 * This "trick" only works as long as 'enum btrfs_csum_type' has
313 * no holes in it
314 */
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100315 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
David Sterbaf7cea562019-10-07 11:11:03 +0200316 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
317
318 }
319
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100320 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
David Sterbaf7cea562019-10-07 11:11:03 +0200321 return ret;
322}
323BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
324
Omar Sandoval7573df52020-08-21 00:39:54 -0700325static ssize_t send_stream_version_show(struct kobject *kobj,
326 struct kobj_attribute *ka, char *buf)
327{
328 return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
329}
330BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
331
Misono Tomohirof902bd32018-05-17 14:24:51 +0900332static struct attribute *btrfs_supported_static_feature_attrs[] = {
333 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200334 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Omar Sandoval7573df52020-08-21 00:39:54 -0700335 BTRFS_ATTR_PTR(static_feature, send_stream_version),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900336 NULL
337};
338
339/*
340 * Features which only depend on kernel version.
341 *
342 * These are listed in /sys/fs/btrfs/features along with
343 * btrfs_feature_attr_group
344 */
345static const struct attribute_group btrfs_static_feature_attr_group = {
346 .name = "features",
347 .attrs = btrfs_supported_static_feature_attrs,
348};
349
David Sterba6e369fe2019-06-13 17:23:02 +0200350#ifdef CONFIG_BTRFS_DEBUG
351
352/*
Dennis Zhoue4faab82019-12-13 16:22:19 -0800353 * Discard statistics and tunables
354 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800355#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
356
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800357static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
358 struct kobj_attribute *a,
359 char *buf)
360{
361 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
362
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100363 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800364 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
365}
366BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
367
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800368static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
369 struct kobj_attribute *a,
370 char *buf)
371{
372 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
373
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100374 return scnprintf(buf, PAGE_SIZE, "%d\n",
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800375 atomic_read(&fs_info->discard_ctl.discardable_extents));
376}
377BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
378
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500379static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
380 struct kobj_attribute *a,
381 char *buf)
382{
383 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
384
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100385 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500386 fs_info->discard_ctl.discard_bitmap_bytes);
387}
388BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
389
390static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
391 struct kobj_attribute *a,
392 char *buf)
393{
394 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
395
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100396 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500397 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
398}
399BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
400
401static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
402 struct kobj_attribute *a,
403 char *buf)
404{
405 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
406
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100407 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500408 fs_info->discard_ctl.discard_extent_bytes);
409}
410BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
411
Dennis Zhoua2309302020-01-02 16:26:35 -0500412static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
413 struct kobj_attribute *a,
414 char *buf)
415{
416 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
417
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100418 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoua2309302020-01-02 16:26:35 -0500419 READ_ONCE(fs_info->discard_ctl.iops_limit));
420}
421
422static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
423 struct kobj_attribute *a,
424 const char *buf, size_t len)
425{
426 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
427 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
428 u32 iops_limit;
429 int ret;
430
431 ret = kstrtou32(buf, 10, &iops_limit);
432 if (ret)
433 return -EINVAL;
434
435 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
436
437 return len;
438}
439BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
440 btrfs_discard_iops_limit_store);
441
Dennis Zhoue93591b2020-01-02 16:26:36 -0500442static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
443 struct kobj_attribute *a,
444 char *buf)
445{
446 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
447
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100448 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoue93591b2020-01-02 16:26:36 -0500449 READ_ONCE(fs_info->discard_ctl.kbps_limit));
450}
451
452static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
453 struct kobj_attribute *a,
454 const char *buf, size_t len)
455{
456 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
457 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
458 u32 kbps_limit;
459 int ret;
460
461 ret = kstrtou32(buf, 10, &kbps_limit);
462 if (ret)
463 return -EINVAL;
464
465 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
466
467 return len;
468}
469BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
470 btrfs_discard_kbps_limit_store);
471
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500472static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
473 struct kobj_attribute *a,
474 char *buf)
475{
476 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
477
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100478 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500479 READ_ONCE(fs_info->discard_ctl.max_discard_size));
480}
481
482static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
483 struct kobj_attribute *a,
484 const char *buf, size_t len)
485{
486 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
487 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
488 u64 max_discard_size;
489 int ret;
490
491 ret = kstrtou64(buf, 10, &max_discard_size);
492 if (ret)
493 return -EINVAL;
494
495 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
496
497 return len;
498}
499BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
500 btrfs_discard_max_discard_size_store);
501
Dennis Zhoue4faab82019-12-13 16:22:19 -0800502static const struct attribute *discard_debug_attrs[] = {
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800503 BTRFS_ATTR_PTR(discard, discardable_bytes),
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800504 BTRFS_ATTR_PTR(discard, discardable_extents),
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500505 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
506 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
507 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
Dennis Zhoua2309302020-01-02 16:26:35 -0500508 BTRFS_ATTR_PTR(discard, iops_limit),
Dennis Zhoue93591b2020-01-02 16:26:36 -0500509 BTRFS_ATTR_PTR(discard, kbps_limit),
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500510 BTRFS_ATTR_PTR(discard, max_discard_size),
Dennis Zhoue4faab82019-12-13 16:22:19 -0800511 NULL,
512};
513
514/*
David Sterba6e369fe2019-06-13 17:23:02 +0200515 * Runtime debugging exported via sysfs
516 *
517 * /sys/fs/btrfs/debug - applies to module or all filesystems
518 * /sys/fs/btrfs/UUID - applies only to the given filesystem
519 */
Dennis Zhou93945cb2019-12-13 16:22:18 -0800520static const struct attribute *btrfs_debug_mount_attrs[] = {
521 NULL,
522};
523
David Sterba6e369fe2019-06-13 17:23:02 +0200524static struct attribute *btrfs_debug_feature_attrs[] = {
525 NULL
526};
527
528static const struct attribute_group btrfs_debug_feature_attr_group = {
529 .name = "debug",
530 .attrs = btrfs_debug_feature_attrs,
531};
532
533#endif
534
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400535static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
536{
537 u64 val;
538 if (lock)
539 spin_lock(lock);
540 val = *value_ptr;
541 if (lock)
542 spin_unlock(lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100543 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400544}
545
546static ssize_t global_rsv_size_show(struct kobject *kobj,
547 struct kobj_attribute *ka, char *buf)
548{
549 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
550 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
551 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
552}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200553BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400554
555static ssize_t global_rsv_reserved_show(struct kobject *kobj,
556 struct kobj_attribute *a, char *buf)
557{
558 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
559 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
560 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
561}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200562BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400563
564#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400565#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400566
567static ssize_t raid_bytes_show(struct kobject *kobj,
568 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200569BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
570BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400571
572static ssize_t raid_bytes_show(struct kobject *kobj,
573 struct kobj_attribute *attr, char *buf)
574
575{
576 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100577 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400578 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400579 u64 val = 0;
580
581 down_read(&sinfo->groups_sem);
582 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200583 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200584 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400585 else
David Sterbabf38be62019-10-23 18:48:11 +0200586 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400587 }
588 up_read(&sinfo->groups_sem);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100589 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400590}
591
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400592static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200593 BTRFS_ATTR_PTR(raid, total_bytes),
594 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400595 NULL
596};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400597ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400598
599static void release_raid_kobj(struct kobject *kobj)
600{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400601 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400602}
603
David Sterba536ea452019-08-01 17:55:55 +0200604static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400605 .sysfs_ops = &kobj_sysfs_ops,
606 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400607 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400608};
609
610#define SPACE_INFO_ATTR(field) \
611static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
612 struct kobj_attribute *a, \
613 char *buf) \
614{ \
615 struct btrfs_space_info *sinfo = to_space_info(kobj); \
616 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
617} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200618BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400619
620static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
621 struct kobj_attribute *a,
622 char *buf)
623{
624 struct btrfs_space_info *sinfo = to_space_info(kobj);
625 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100626 return scnprintf(buf, PAGE_SIZE, "%lld\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400627}
628
629SPACE_INFO_ATTR(flags);
630SPACE_INFO_ATTR(total_bytes);
631SPACE_INFO_ATTR(bytes_used);
632SPACE_INFO_ATTR(bytes_pinned);
633SPACE_INFO_ATTR(bytes_reserved);
634SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800635SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400636SPACE_INFO_ATTR(disk_used);
637SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200638BTRFS_ATTR(space_info, total_bytes_pinned,
639 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400640
641static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200642 BTRFS_ATTR_PTR(space_info, flags),
643 BTRFS_ATTR_PTR(space_info, total_bytes),
644 BTRFS_ATTR_PTR(space_info, bytes_used),
645 BTRFS_ATTR_PTR(space_info, bytes_pinned),
646 BTRFS_ATTR_PTR(space_info, bytes_reserved),
647 BTRFS_ATTR_PTR(space_info, bytes_may_use),
648 BTRFS_ATTR_PTR(space_info, bytes_readonly),
649 BTRFS_ATTR_PTR(space_info, disk_used),
650 BTRFS_ATTR_PTR(space_info, disk_total),
651 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400652 NULL,
653};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400654ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400655
656static void space_info_release(struct kobject *kobj)
657{
658 struct btrfs_space_info *sinfo = to_space_info(kobj);
659 percpu_counter_destroy(&sinfo->total_bytes_pinned);
660 kfree(sinfo);
661}
662
David Sterba27992d02019-08-01 17:55:55 +0200663static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400664 .sysfs_ops = &kobj_sysfs_ops,
665 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400666 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400667};
668
669static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200670 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
671 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400672 NULL,
673};
674
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400675static ssize_t btrfs_label_show(struct kobject *kobj,
676 struct kobj_attribute *a, char *buf)
677{
678 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900679 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200680 ssize_t ret;
681
682 spin_lock(&fs_info->super_lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100683 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
David Sterbaee17fc82016-04-26 16:22:06 +0200684 spin_unlock(&fs_info->super_lock);
685
686 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400687}
688
689static ssize_t btrfs_label_store(struct kobject *kobj,
690 struct kobj_attribute *a,
691 const char *buf, size_t len)
692{
693 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900694 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400695
David Sterba66ac9fe2016-04-26 16:03:57 +0200696 if (!fs_info)
697 return -EPERM;
698
David Howellsbc98a422017-07-17 08:45:34 +0100699 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800700 return -EROFS;
701
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900702 /*
703 * p_len is the len until the first occurrence of either
704 * '\n' or '\0'
705 */
706 p_len = strcspn(buf, "\n");
707
708 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400709 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400710
David Sterbaa6f69dc2014-05-30 19:29:05 +0200711 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900712 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
713 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200714 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400715
David Sterbaa6f69dc2014-05-30 19:29:05 +0200716 /*
717 * We don't want to do full transaction commit from inside sysfs
718 */
719 btrfs_set_pending(fs_info, COMMIT);
720 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400721
David Sterbaa6f69dc2014-05-30 19:29:05 +0200722 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400723}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200724BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400725
David Sterbadf935892014-05-07 18:17:16 +0200726static ssize_t btrfs_nodesize_show(struct kobject *kobj,
727 struct kobj_attribute *a, char *buf)
728{
729 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
730
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100731 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200732}
733
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200734BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200735
736static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
737 struct kobj_attribute *a, char *buf)
738{
739 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
740
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100741 return scnprintf(buf, PAGE_SIZE, "%u\n",
742 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200743}
744
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200745BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200746
747static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
748 struct kobj_attribute *a, char *buf)
749{
750 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
751
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100752 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200753}
754
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200755BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200756
Sargun Dhillon27234802017-05-11 21:18:03 +0000757static ssize_t quota_override_show(struct kobject *kobj,
758 struct kobj_attribute *a, char *buf)
759{
760 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
761 int quota_override;
762
763 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100764 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
Sargun Dhillon27234802017-05-11 21:18:03 +0000765}
766
767static ssize_t quota_override_store(struct kobject *kobj,
768 struct kobj_attribute *a,
769 const char *buf, size_t len)
770{
771 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
772 unsigned long knob;
773 int err;
774
775 if (!fs_info)
776 return -EPERM;
777
778 if (!capable(CAP_SYS_RESOURCE))
779 return -EPERM;
780
781 err = kstrtoul(buf, 10, &knob);
782 if (err)
783 return err;
784 if (knob > 1)
785 return -EINVAL;
786
787 if (knob)
788 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
789 else
790 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
791
792 return len;
793}
794
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200795BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000796
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200797static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
798 struct kobj_attribute *a, char *buf)
799{
800 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
801
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100802 return scnprintf(buf, PAGE_SIZE, "%pU\n",
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200803 fs_info->fs_devices->metadata_uuid);
804}
805
806BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
807
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200808static ssize_t btrfs_checksum_show(struct kobject *kobj,
809 struct kobj_attribute *a, char *buf)
810{
811 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
812 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
813
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100814 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200815 btrfs_super_csum_name(csum_type),
816 crypto_shash_driver_name(fs_info->csum_shash));
817}
818
819BTRFS_ATTR(, checksum, btrfs_checksum_show);
820
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500821static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
822 struct kobj_attribute *a, char *buf)
823{
824 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
825 const char *str;
826
827 switch (READ_ONCE(fs_info->exclusive_operation)) {
828 case BTRFS_EXCLOP_NONE:
829 str = "none\n";
830 break;
831 case BTRFS_EXCLOP_BALANCE:
832 str = "balance\n";
833 break;
834 case BTRFS_EXCLOP_DEV_ADD:
835 str = "device add\n";
836 break;
837 case BTRFS_EXCLOP_DEV_REMOVE:
838 str = "device remove\n";
839 break;
840 case BTRFS_EXCLOP_DEV_REPLACE:
841 str = "device replace\n";
842 break;
843 case BTRFS_EXCLOP_RESIZE:
844 str = "resize\n";
845 break;
846 case BTRFS_EXCLOP_SWAP_ACTIVATE:
847 str = "swap activate\n";
848 break;
849 default:
850 str = "UNKNOWN\n";
851 break;
852 }
853 return scnprintf(buf, PAGE_SIZE, "%s", str);
854}
855BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
856
Anand Jain0dd29062015-03-10 06:38:27 +0800857static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200858 BTRFS_ATTR_PTR(, label),
859 BTRFS_ATTR_PTR(, nodesize),
860 BTRFS_ATTR_PTR(, sectorsize),
861 BTRFS_ATTR_PTR(, clone_alignment),
862 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200863 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200864 BTRFS_ATTR_PTR(, checksum),
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500865 BTRFS_ATTR_PTR(, exclusive_operation),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400866 NULL,
867};
868
Anand Jainc1b7e472015-08-14 18:32:50 +0800869static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400870{
Anand Jain2e7910d2015-03-10 06:38:29 +0800871 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800872
Anand Jainc1b7e472015-08-14 18:32:50 +0800873 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800874 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400875}
876
877static struct kobj_type btrfs_ktype = {
878 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800879 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400880};
881
Anand Jain2e7910d2015-03-10 06:38:29 +0800882static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
883{
884 if (kobj->ktype != &btrfs_ktype)
885 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800886 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800887}
888
Jeff Mahoney510d7362013-11-01 13:06:59 -0400889static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
890{
891 if (kobj->ktype != &btrfs_ktype)
892 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800893 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400894}
Josef Bacik58176a92007-08-29 15:47:34 -0400895
Jeff Mahoneye453d982013-11-21 10:37:16 -0500896#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900897#define BTRFS_FEATURE_NAME_MAX 13
898static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
899static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500900
Tomohiro Misono6c521572018-05-16 17:09:26 +0900901static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500902 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
903 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
904 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
905};
906
907static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400908{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500909 int set;
910
911 for (set = 0; set < FEAT_MAX; set++) {
912 int i;
913 struct attribute *attrs[2];
914 struct attribute_group agroup = {
915 .name = "features",
916 .attrs = attrs,
917 };
918 u64 features = get_features(fs_info, set);
919 features &= ~supported_feature_masks[set];
920
921 if (!features)
922 continue;
923
924 attrs[1] = NULL;
925 for (i = 0; i < NUM_FEATURE_BITS; i++) {
926 struct btrfs_feature_attr *fa;
927
928 if (!(features & (1ULL << i)))
929 continue;
930
931 fa = &btrfs_feature_attrs[set][i];
932 attrs[0] = &fa->kobj_attr.attr;
933 if (add) {
934 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800935 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500936 &agroup);
937 if (ret)
938 return ret;
939 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800940 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500941 &agroup);
942 }
943
944 }
945 return 0;
946}
947
Anand Jain2e3e1282015-03-10 06:38:32 +0800948static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500949{
Anand Jaina013d142020-02-12 17:28:10 +0800950 if (fs_devs->devinfo_kobj) {
951 kobject_del(fs_devs->devinfo_kobj);
952 kobject_put(fs_devs->devinfo_kobj);
953 fs_devs->devinfo_kobj = NULL;
954 }
955
Anand Jainb5501502019-11-21 17:33:30 +0800956 if (fs_devs->devices_kobj) {
957 kobject_del(fs_devs->devices_kobj);
958 kobject_put(fs_devs->devices_kobj);
959 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800960 }
961
Anand Jainc1b7e472015-08-14 18:32:50 +0800962 if (fs_devs->fsid_kobj.state_initialized) {
963 kobject_del(&fs_devs->fsid_kobj);
964 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800965 wait_for_completion(&fs_devs->kobj_unregister);
966 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400967}
968
Anand Jain2e3e1282015-03-10 06:38:32 +0800969/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800970void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800971{
972 struct list_head *fs_uuids = btrfs_get_fs_uuids();
973
974 if (fs_devs) {
975 __btrfs_sysfs_remove_fsid(fs_devs);
976 return;
977 }
978
Anand Jainc4babc52018-04-12 10:29:25 +0800979 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800980 __btrfs_sysfs_remove_fsid(fs_devs);
981 }
982}
983
Anand Jain53f8a742020-09-05 01:34:27 +0800984static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
985{
986 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +0800987 struct btrfs_fs_devices *seed;
Anand Jain53f8a742020-09-05 01:34:27 +0800988
989 list_for_each_entry(device, &fs_devices->devices, dev_list)
990 btrfs_sysfs_remove_device(device);
Anand Jain30b0e4e2020-09-05 01:34:28 +0800991
992 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
993 list_for_each_entry(device, &seed->devices, dev_list)
994 btrfs_sysfs_remove_device(device);
995 }
Anand Jain53f8a742020-09-05 01:34:27 +0800996}
997
Anand Jain6618a592015-08-14 18:32:47 +0800998void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500999{
Nikolay Borisov3092c682020-07-03 11:13:15 +03001000 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1001
Nikolay Borisov3092c682020-07-03 11:13:15 +03001002 sysfs_remove_link(fsid_kobj, "bdi");
1003
Jeff Mahoneye453d982013-11-21 10:37:16 -05001004 if (fs_info->space_info_kobj) {
1005 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1006 kobject_del(fs_info->space_info_kobj);
1007 kobject_put(fs_info->space_info_kobj);
1008 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001009#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhoue4faab82019-12-13 16:22:19 -08001010 if (fs_info->discard_debug_kobj) {
1011 sysfs_remove_files(fs_info->discard_debug_kobj,
1012 discard_debug_attrs);
1013 kobject_del(fs_info->discard_debug_kobj);
1014 kobject_put(fs_info->discard_debug_kobj);
1015 }
Dennis Zhou93945cb2019-12-13 16:22:18 -08001016 if (fs_info->debug_kobj) {
1017 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1018 kobject_del(fs_info->debug_kobj);
1019 kobject_put(fs_info->debug_kobj);
1020 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001021#endif
Jeff Mahoneye453d982013-11-21 10:37:16 -05001022 addrm_unknown_feature_attrs(fs_info, false);
Nikolay Borisov3092c682020-07-03 11:13:15 +03001023 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1024 sysfs_remove_files(fsid_kobj, btrfs_attrs);
Anand Jain53f8a742020-09-05 01:34:27 +08001025 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001026}
1027
David Sterbaf10152b2019-08-01 19:07:55 +02001028static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001029 [FEAT_COMPAT] = "compat",
1030 [FEAT_COMPAT_RO] = "compat_ro",
1031 [FEAT_INCOMPAT] = "incompat",
1032};
1033
David Sterba9e6df7c2020-08-17 10:56:00 +02001034const char *btrfs_feature_set_name(enum btrfs_feature_set set)
David Sterbaf10152b2019-08-01 19:07:55 +02001035{
1036 return btrfs_feature_set_names[set];
1037}
1038
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001039char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1040{
1041 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1042 int len = 0;
1043 int i;
1044 char *str;
1045
1046 str = kmalloc(bufsize, GFP_KERNEL);
1047 if (!str)
1048 return str;
1049
1050 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1051 const char *name;
1052
1053 if (!(flags & (1ULL << i)))
1054 continue;
1055
1056 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001057 len += scnprintf(str + len, bufsize - len, "%s%s",
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001058 len ? "," : "", name);
1059 }
1060
1061 return str;
1062}
1063
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001064static void init_feature_attrs(void)
1065{
1066 struct btrfs_feature_attr *fa;
1067 int set, i;
1068
1069 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1070 ARRAY_SIZE(btrfs_feature_attrs));
1071 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1072 ARRAY_SIZE(btrfs_feature_attrs[0]));
1073
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001074 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1075 memset(btrfs_unknown_feature_names, 0,
1076 sizeof(btrfs_unknown_feature_names));
1077
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001078 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1079 struct btrfs_feature_attr *sfa;
1080 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001081 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001082 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001083 bit = ilog2(sfa->feature_bit);
1084 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001085
1086 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1087 }
1088
1089 for (set = 0; set < FEAT_MAX; set++) {
1090 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1091 char *name = btrfs_unknown_feature_names[set][i];
1092 fa = &btrfs_feature_attrs[set][i];
1093
1094 if (fa->kobj_attr.attr.name)
1095 continue;
1096
Tomohiro Misono6c521572018-05-16 17:09:26 +09001097 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001098 btrfs_feature_set_names[set], i);
1099
1100 fa->kobj_attr.attr.name = name;
1101 fa->kobj_attr.attr.mode = S_IRUGO;
1102 fa->feature_set = set;
1103 fa->feature_bit = 1ULL << i;
1104 }
1105 }
1106}
1107
David Sterba32a99912019-08-01 17:49:55 +02001108/*
1109 * Create a sysfs entry for a given block group type at path
1110 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1111 */
David Sterba32da53862019-10-29 19:20:18 +01001112void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +02001113{
1114 struct btrfs_fs_info *fs_info = cache->fs_info;
1115 struct btrfs_space_info *space_info = cache->space_info;
1116 struct raid_kobject *rkobj;
1117 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1118 unsigned int nofs_flag;
1119 int ret;
1120
1121 /*
1122 * Setup a NOFS context because kobject_add(), deep in its call chain,
1123 * does GFP_KERNEL allocations, and we are often called in a context
1124 * where if reclaim is triggered we can deadlock (we are either holding
1125 * a transaction handle or some lock required for a transaction
1126 * commit).
1127 */
1128 nofs_flag = memalloc_nofs_save();
1129
1130 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1131 if (!rkobj) {
1132 memalloc_nofs_restore(nofs_flag);
1133 btrfs_warn(cache->fs_info,
1134 "couldn't alloc memory for raid level kobject");
1135 return;
1136 }
1137
1138 rkobj->flags = cache->flags;
1139 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
Josef Bacik49ea1122020-09-01 17:40:38 -04001140
1141 /*
1142 * We call this either on mount, or if we've created a block group for a
1143 * new index type while running (i.e. when restriping). The running
1144 * case is tricky because we could race with other threads, so we need
1145 * to have this check to make sure we didn't already init the kobject.
1146 *
1147 * We don't have to protect on the free side because it only happens on
1148 * unmount.
1149 */
1150 spin_lock(&space_info->lock);
1151 if (space_info->block_group_kobjs[index]) {
1152 spin_unlock(&space_info->lock);
1153 kobject_put(&rkobj->kobj);
1154 return;
1155 } else {
1156 space_info->block_group_kobjs[index] = &rkobj->kobj;
1157 }
1158 spin_unlock(&space_info->lock);
1159
David Sterba32a99912019-08-01 17:49:55 +02001160 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1161 btrfs_bg_type_to_raid_name(rkobj->flags));
1162 memalloc_nofs_restore(nofs_flag);
1163 if (ret) {
Josef Bacik49ea1122020-09-01 17:40:38 -04001164 spin_lock(&space_info->lock);
1165 space_info->block_group_kobjs[index] = NULL;
1166 spin_unlock(&space_info->lock);
David Sterba32a99912019-08-01 17:49:55 +02001167 kobject_put(&rkobj->kobj);
1168 btrfs_warn(fs_info,
1169 "failed to add kobject for block cache, ignoring");
1170 return;
1171 }
David Sterba32a99912019-08-01 17:49:55 +02001172}
1173
David Sterbab5865ba2019-08-01 18:50:16 +02001174/*
1175 * Remove sysfs directories for all block group types of a given space info and
1176 * the space info as well
1177 */
1178void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1179{
1180 int i;
1181
1182 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1183 struct kobject *kobj;
1184
1185 kobj = space_info->block_group_kobjs[i];
1186 space_info->block_group_kobjs[i] = NULL;
1187 if (kobj) {
1188 kobject_del(kobj);
1189 kobject_put(kobj);
1190 }
1191 }
1192 kobject_del(&space_info->kobj);
1193 kobject_put(&space_info->kobj);
1194}
1195
David Sterbab8823272019-08-01 18:50:16 +02001196static const char *alloc_name(u64 flags)
1197{
1198 switch (flags) {
1199 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1200 return "mixed";
1201 case BTRFS_BLOCK_GROUP_METADATA:
1202 return "metadata";
1203 case BTRFS_BLOCK_GROUP_DATA:
1204 return "data";
1205 case BTRFS_BLOCK_GROUP_SYSTEM:
1206 return "system";
1207 default:
1208 WARN_ON(1);
1209 return "invalid-combination";
1210 };
1211}
1212
1213/*
1214 * Create a sysfs entry for a space info type at path
1215 * /sys/fs/btrfs/UUID/allocation/TYPE
1216 */
1217int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1218 struct btrfs_space_info *space_info)
1219{
1220 int ret;
1221
1222 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1223 fs_info->space_info_kobj, "%s",
1224 alloc_name(space_info->flags));
1225 if (ret) {
1226 kobject_put(&space_info->kobj);
1227 return ret;
1228 }
1229
1230 return 0;
1231}
1232
Anand Jain53f8a742020-09-05 01:34:27 +08001233void btrfs_sysfs_remove_device(struct btrfs_device *device)
Anand Jain99994cd2014-06-03 11:36:00 +08001234{
1235 struct hd_struct *disk;
1236 struct kobject *disk_kobj;
Anand Jain985e2332020-09-05 01:34:24 +08001237 struct kobject *devices_kobj;
Anand Jain99994cd2014-06-03 11:36:00 +08001238
Anand Jain985e2332020-09-05 01:34:24 +08001239 /*
1240 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1241 * fs_info::fs_devices.
1242 */
1243 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1244 ASSERT(devices_kobj);
Anand Jain99994cd2014-06-03 11:36:00 +08001245
Anand Jain985e2332020-09-05 01:34:24 +08001246 if (device->bdev) {
1247 disk = device->bdev->bd_part;
1248 disk_kobj = &part_to_dev(disk)->kobj;
1249 sysfs_remove_link(devices_kobj, disk_kobj->name);
1250 }
Anand Jain99994cd2014-06-03 11:36:00 +08001251
Anand Jain985e2332020-09-05 01:34:24 +08001252 if (device->devid_kobj.state_initialized) {
1253 kobject_del(&device->devid_kobj);
1254 kobject_put(&device->devid_kobj);
1255 wait_for_completion(&device->kobj_unregister);
1256 }
1257}
Anand Jain668e48af2020-01-06 19:38:31 +08001258
Anand Jain668e48af2020-01-06 19:38:31 +08001259static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1260 struct kobj_attribute *a,
1261 char *buf)
1262{
1263 int val;
1264 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1265 devid_kobj);
1266
1267 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1268
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001269 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001270}
1271BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1272
Anand Jain25864772020-02-13 16:40:53 +08001273static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
Anand Jain668e48af2020-01-06 19:38:31 +08001274 struct kobj_attribute *a, char *buf)
1275{
1276 int val;
1277 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1278 devid_kobj);
1279
1280 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1281
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001282 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001283}
Anand Jain25864772020-02-13 16:40:53 +08001284BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
Anand Jain668e48af2020-01-06 19:38:31 +08001285
1286static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1287 struct kobj_attribute *a,
1288 char *buf)
1289{
1290 int val;
1291 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1292 devid_kobj);
1293
1294 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1295
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001296 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001297}
1298BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1299
1300static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1301 struct kobj_attribute *a, char *buf)
1302{
1303 int val;
1304 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1305 devid_kobj);
1306
1307 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1308
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001309 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001310}
1311BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1312
1313static struct attribute *devid_attrs[] = {
1314 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1315 BTRFS_ATTR_PTR(devid, missing),
1316 BTRFS_ATTR_PTR(devid, replace_target),
1317 BTRFS_ATTR_PTR(devid, writeable),
1318 NULL
1319};
1320ATTRIBUTE_GROUPS(devid);
1321
1322static void btrfs_release_devid_kobj(struct kobject *kobj)
1323{
1324 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1325 devid_kobj);
1326
1327 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1328 complete(&device->kobj_unregister);
1329}
1330
1331static struct kobj_type devid_ktype = {
1332 .sysfs_ops = &kobj_sysfs_ops,
1333 .default_groups = devid_groups,
1334 .release = btrfs_release_devid_kobj,
1335};
1336
Anand Jaincd36da22020-09-05 01:34:26 +08001337int btrfs_sysfs_add_device(struct btrfs_device *device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001338{
Anand Jain178a16c2020-09-05 01:34:23 +08001339 int ret;
Josef Bacika47bd782020-07-21 10:17:50 -04001340 unsigned int nofs_flag;
Anand Jain178a16c2020-09-05 01:34:23 +08001341 struct kobject *devices_kobj;
1342 struct kobject *devinfo_kobj;
1343
1344 /*
1345 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1346 * for the seed fs_devices
1347 */
1348 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1349 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1350 ASSERT(devices_kobj);
1351 ASSERT(devinfo_kobj);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001352
Josef Bacika47bd782020-07-21 10:17:50 -04001353 nofs_flag = memalloc_nofs_save();
Anand Jainf0853812014-01-15 17:22:28 +08001354
Anand Jain178a16c2020-09-05 01:34:23 +08001355 if (device->bdev) {
1356 struct hd_struct *disk;
1357 struct kobject *disk_kobj;
Anand Jain0d393762014-06-03 11:36:01 +08001358
Anand Jain178a16c2020-09-05 01:34:23 +08001359 disk = device->bdev->bd_part;
1360 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001361
Anand Jain178a16c2020-09-05 01:34:23 +08001362 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1363 if (ret) {
1364 btrfs_warn(device->fs_info,
1365 "creating sysfs device link for devid %llu failed: %d",
1366 device->devid, ret);
1367 goto out;
Anand Jain668e48af2020-01-06 19:38:31 +08001368 }
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001369 }
1370
Anand Jain178a16c2020-09-05 01:34:23 +08001371 init_completion(&device->kobj_unregister);
1372 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1373 devinfo_kobj, "%llu", device->devid);
1374 if (ret) {
1375 kobject_put(&device->devid_kobj);
1376 btrfs_warn(device->fs_info,
1377 "devinfo init for devid %llu failed: %d",
1378 device->devid, ret);
1379 }
1380
1381out:
1382 memalloc_nofs_restore(nofs_flag);
1383 return ret;
1384}
1385
Anand Jaincd36da22020-09-05 01:34:26 +08001386static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
Anand Jain178a16c2020-09-05 01:34:23 +08001387{
1388 int ret;
Anand Jaincd36da22020-09-05 01:34:26 +08001389 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001390 struct btrfs_fs_devices *seed;
Anand Jain178a16c2020-09-05 01:34:23 +08001391
1392 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1393 ret = btrfs_sysfs_add_device(device);
1394 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001395 goto fail;
Anand Jain178a16c2020-09-05 01:34:23 +08001396 }
1397
Anand Jain30b0e4e2020-09-05 01:34:28 +08001398 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1399 list_for_each_entry(device, &seed->devices, dev_list) {
1400 ret = btrfs_sysfs_add_device(device);
1401 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001402 goto fail;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001403 }
1404 }
1405
Anand Jain178a16c2020-09-05 01:34:23 +08001406 return 0;
Anand Jain7ad39122020-09-05 01:34:29 +08001407
1408fail:
1409 btrfs_sysfs_remove_fs_devices(fs_devices);
1410 return ret;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001411}
1412
David Sterba5b286922019-08-01 18:50:16 +02001413void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1414{
1415 int ret;
1416
1417 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1418 if (ret)
1419 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1420 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1421 &disk_to_dev(bdev->bd_disk)->kobj);
1422}
1423
Nikolay Borisov8e560082020-08-12 16:18:51 +03001424void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1425
David Sterbaf93c3992019-08-01 18:50:16 +02001426{
1427 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1428
1429 /*
1430 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1431 * directory
1432 */
Nikolay Borisov8e560082020-08-12 16:18:51 +03001433 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
David Sterbaf93c3992019-08-01 18:50:16 +02001434 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1435 btrfs_warn(fs_devices->fs_info,
1436 "sysfs: failed to create fsid for sprout");
1437}
1438
Anand Jain668e48af2020-01-06 19:38:31 +08001439void btrfs_sysfs_update_devid(struct btrfs_device *device)
1440{
1441 char tmp[24];
1442
1443 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1444
1445 if (kobject_rename(&device->devid_kobj, tmp))
1446 btrfs_warn(device->fs_devices->fs_info,
1447 "sysfs: failed to update devid for %llu",
1448 device->devid);
1449}
1450
Jeff Mahoney510d7362013-11-01 13:06:59 -04001451/* /sys/fs/btrfs/ entry */
1452static struct kset *btrfs_kset;
1453
Anand Jain72059212015-03-10 06:38:26 +08001454/*
Anand Jainc6761a92019-11-21 17:33:32 +08001455 * Creates:
1456 * /sys/fs/btrfs/UUID
1457 *
Anand Jain72059212015-03-10 06:38:26 +08001458 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001459 */
Anand Jainc6761a92019-11-21 17:33:32 +08001460int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001461{
1462 int error;
1463
Anand Jain2e7910d2015-03-10 06:38:29 +08001464 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001465 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001466 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1467 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001468 if (error) {
1469 kobject_put(&fs_devs->fsid_kobj);
1470 return error;
1471 }
1472
Anand Jainbc036bb2019-11-21 17:33:34 +08001473 fs_devs->devices_kobj = kobject_create_and_add("devices",
1474 &fs_devs->fsid_kobj);
1475 if (!fs_devs->devices_kobj) {
1476 btrfs_err(fs_devs->fs_info,
1477 "failed to init sysfs device interface");
Anand Jain1f6087e2020-02-12 17:28:12 +08001478 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainbc036bb2019-11-21 17:33:34 +08001479 return -ENOMEM;
1480 }
1481
Anand Jaina013d142020-02-12 17:28:10 +08001482 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1483 &fs_devs->fsid_kobj);
1484 if (!fs_devs->devinfo_kobj) {
1485 btrfs_err(fs_devs->fs_info,
1486 "failed to init sysfs devinfo kobject");
1487 btrfs_sysfs_remove_fsid(fs_devs);
1488 return -ENOMEM;
1489 }
1490
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001491 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001492}
1493
Anand Jain96f31362015-08-14 18:32:46 +08001494int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001495{
1496 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001497 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001498 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001499
Anand Jaincd36da22020-09-05 01:34:26 +08001500 error = btrfs_sysfs_add_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001501 if (error)
1502 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001503
Anand Jainc1b7e472015-08-14 18:32:50 +08001504 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001505 if (error) {
Anand Jain53f8a742020-09-05 01:34:27 +08001506 btrfs_sysfs_remove_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001507 return error;
1508 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001509
Anand Jainc1b7e472015-08-14 18:32:50 +08001510 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001511 &btrfs_feature_attr_group);
1512 if (error)
1513 goto failure;
1514
David Sterba6e369fe2019-06-13 17:23:02 +02001515#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhou93945cb2019-12-13 16:22:18 -08001516 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1517 if (!fs_info->debug_kobj) {
1518 error = -ENOMEM;
1519 goto failure;
1520 }
1521
1522 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
David Sterba6e369fe2019-06-13 17:23:02 +02001523 if (error)
1524 goto failure;
Dennis Zhoue4faab82019-12-13 16:22:19 -08001525
1526 /* Discard directory */
1527 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1528 fs_info->debug_kobj);
1529 if (!fs_info->discard_debug_kobj) {
1530 error = -ENOMEM;
1531 goto failure;
1532 }
1533
1534 error = sysfs_create_files(fs_info->discard_debug_kobj,
1535 discard_debug_attrs);
1536 if (error)
1537 goto failure;
David Sterba6e369fe2019-06-13 17:23:02 +02001538#endif
1539
Jeff Mahoneye453d982013-11-21 10:37:16 -05001540 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001541 if (error)
1542 goto failure;
1543
Nikolay Borisov3092c682020-07-03 11:13:15 +03001544 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1545 if (error)
1546 goto failure;
1547
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001548 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001549 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001550 if (!fs_info->space_info_kobj) {
1551 error = -ENOMEM;
1552 goto failure;
1553 }
1554
1555 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1556 if (error)
1557 goto failure;
1558
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001559 return 0;
1560failure:
Anand Jain6618a592015-08-14 18:32:47 +08001561 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001562 return error;
1563}
1564
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001565static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1566{
1567 return to_fs_info(kobj->parent->parent);
1568}
1569
1570#define QGROUP_ATTR(_member, _show_name) \
1571static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1572 struct kobj_attribute *a, \
1573 char *buf) \
1574{ \
1575 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1576 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1577 struct btrfs_qgroup, kobj); \
1578 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1579} \
1580BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1581
1582#define QGROUP_RSV_ATTR(_name, _type) \
1583static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1584 struct kobj_attribute *a, \
1585 char *buf) \
1586{ \
1587 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1588 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1589 struct btrfs_qgroup, kobj); \
1590 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1591 &fs_info->qgroup_lock, buf); \
1592} \
1593BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1594
1595QGROUP_ATTR(rfer, referenced);
1596QGROUP_ATTR(excl, exclusive);
1597QGROUP_ATTR(max_rfer, max_referenced);
1598QGROUP_ATTR(max_excl, max_exclusive);
1599QGROUP_ATTR(lim_flags, limit_flags);
1600QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1601QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1602QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1603
1604static struct attribute *qgroup_attrs[] = {
1605 BTRFS_ATTR_PTR(qgroup, referenced),
1606 BTRFS_ATTR_PTR(qgroup, exclusive),
1607 BTRFS_ATTR_PTR(qgroup, max_referenced),
1608 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1609 BTRFS_ATTR_PTR(qgroup, limit_flags),
1610 BTRFS_ATTR_PTR(qgroup, rsv_data),
1611 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1612 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1613 NULL
1614};
1615ATTRIBUTE_GROUPS(qgroup);
1616
1617static void qgroup_release(struct kobject *kobj)
1618{
1619 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1620
1621 memset(&qgroup->kobj, 0, sizeof(*kobj));
1622}
1623
1624static struct kobj_type qgroup_ktype = {
1625 .sysfs_ops = &kobj_sysfs_ops,
1626 .release = qgroup_release,
1627 .default_groups = qgroup_groups,
1628};
1629
1630int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1631 struct btrfs_qgroup *qgroup)
1632{
1633 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1634 int ret;
1635
1636 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1637 return 0;
1638 if (qgroup->kobj.state_initialized)
1639 return 0;
1640 if (!qgroups_kobj)
1641 return -EINVAL;
1642
1643 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1644 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1645 btrfs_qgroup_subvolid(qgroup->qgroupid));
1646 if (ret < 0)
1647 kobject_put(&qgroup->kobj);
1648
1649 return ret;
1650}
1651
1652void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1653{
1654 struct btrfs_qgroup *qgroup;
1655 struct btrfs_qgroup *next;
1656
1657 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1658 return;
1659
1660 rbtree_postorder_for_each_entry_safe(qgroup, next,
1661 &fs_info->qgroup_tree, node)
1662 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
Qu Wenruo62ab2cc2020-08-03 14:20:11 +08001663 if (fs_info->qgroups_kobj) {
1664 kobject_del(fs_info->qgroups_kobj);
1665 kobject_put(fs_info->qgroups_kobj);
1666 fs_info->qgroups_kobj = NULL;
1667 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001668}
1669
1670/* Called when qgroups get initialized, thus there is no need for locking */
1671int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1672{
1673 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1674 struct btrfs_qgroup *qgroup;
1675 struct btrfs_qgroup *next;
1676 int ret = 0;
1677
1678 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1679 return 0;
1680
1681 ASSERT(fsid_kobj);
1682 if (fs_info->qgroups_kobj)
1683 return 0;
1684
1685 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1686 if (!fs_info->qgroups_kobj) {
1687 ret = -ENOMEM;
1688 goto out;
1689 }
1690 rbtree_postorder_for_each_entry_safe(qgroup, next,
1691 &fs_info->qgroup_tree, node) {
1692 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1693 if (ret < 0)
1694 goto out;
1695 }
1696
1697out:
1698 if (ret < 0)
1699 btrfs_sysfs_del_qgroups(fs_info);
1700 return ret;
1701}
1702
1703void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1704 struct btrfs_qgroup *qgroup)
1705{
1706 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1707 return;
1708
1709 if (qgroup->kobj.state_initialized) {
1710 kobject_del(&qgroup->kobj);
1711 kobject_put(&qgroup->kobj);
1712 }
1713}
David Sterba444e7512016-01-21 18:50:40 +01001714
1715/*
1716 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1717 * values in superblock. Call after any changes to incompat/compat_ro flags
1718 */
1719void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1720 u64 bit, enum btrfs_feature_set set)
1721{
1722 struct btrfs_fs_devices *fs_devs;
1723 struct kobject *fsid_kobj;
Leon Romanovsky24646482020-08-19 17:16:28 +03001724 u64 __maybe_unused features;
1725 int __maybe_unused ret;
David Sterba444e7512016-01-21 18:50:40 +01001726
1727 if (!fs_info)
1728 return;
1729
Leon Romanovsky24646482020-08-19 17:16:28 +03001730 /*
1731 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
1732 * safe when called from some contexts (eg. balance)
1733 */
David Sterba444e7512016-01-21 18:50:40 +01001734 features = get_features(fs_info, set);
1735 ASSERT(bit & supported_feature_masks[set]);
1736
1737 fs_devs = fs_info->fs_devices;
1738 fsid_kobj = &fs_devs->fsid_kobj;
1739
David Sterbabf609202016-01-27 14:06:29 +01001740 if (!fsid_kobj->state_initialized)
1741 return;
1742
David Sterba444e7512016-01-21 18:50:40 +01001743 /*
1744 * FIXME: this is too heavy to update just one value, ideally we'd like
1745 * to use sysfs_update_group but some refactoring is needed first.
1746 */
1747 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1748 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1749}
1750
Liu Bof5c29bd2017-11-02 17:21:50 -06001751int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001752{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001753 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001754
Greg KHe3fe4e72008-02-20 14:14:16 -05001755 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1756 if (!btrfs_kset)
1757 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001758
David Sterba1bae3092014-02-05 15:36:18 +01001759 init_feature_attrs();
1760 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001761 if (ret)
1762 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001763 ret = sysfs_merge_group(&btrfs_kset->kobj,
1764 &btrfs_static_feature_attr_group);
1765 if (ret)
1766 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001767
David Sterba6e369fe2019-06-13 17:23:02 +02001768#ifdef CONFIG_BTRFS_DEBUG
1769 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1770 if (ret)
1771 goto out2;
1772#endif
1773
Filipe Manana001a6482015-01-23 18:27:00 +00001774 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001775
1776out_remove_group:
1777 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001778out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001779 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001780
1781 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001782}
1783
David Sterbae67c7182018-02-19 17:24:18 +01001784void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001785{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001786 sysfs_unmerge_group(&btrfs_kset->kobj,
1787 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001788 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Dennis Zhou71e89782019-12-13 16:22:17 -08001789#ifdef CONFIG_BTRFS_DEBUG
1790 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1791#endif
Greg KHe3fe4e72008-02-20 14:14:16 -05001792 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001793}
Chris Mason55d47412008-02-20 16:02:51 -05001794