blob: 4522a1c4cd08dcfdc4e3d10373d1edd2b6bb07d9 [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);
Naohiro Aota7b3d5a92020-11-10 20:26:06 +0900266/* Remove once support for zoned allocation is feature complete */
267#ifdef CONFIG_BTRFS_DEBUG
268BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
269#endif
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400270
271static struct attribute *btrfs_supported_feature_attrs[] = {
272 BTRFS_FEAT_ATTR_PTR(mixed_backref),
273 BTRFS_FEAT_ATTR_PTR(default_subvol),
274 BTRFS_FEAT_ATTR_PTR(mixed_groups),
275 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700276 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400277 BTRFS_FEAT_ATTR_PTR(big_metadata),
278 BTRFS_FEAT_ATTR_PTR(extended_iref),
279 BTRFS_FEAT_ATTR_PTR(raid56),
280 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100281 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200282 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100283 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200284 BTRFS_FEAT_ATTR_PTR(raid1c34),
Naohiro Aota7b3d5a92020-11-10 20:26:06 +0900285#ifdef CONFIG_BTRFS_DEBUG
286 BTRFS_FEAT_ATTR_PTR(zoned),
287#endif
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400288 NULL
289};
290
Misono Tomohirof902bd32018-05-17 14:24:51 +0900291/*
292 * Features which depend on feature bits and may differ between each fs.
293 *
294 * /sys/fs/btrfs/features lists all available features of this kernel while
295 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
296 * can be changed online.
297 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400298static const struct attribute_group btrfs_feature_attr_group = {
299 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400300 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400301 .attrs = btrfs_supported_feature_attrs,
302};
Josef Bacik58176a92007-08-29 15:47:34 -0400303
Misono Tomohirof902bd32018-05-17 14:24:51 +0900304static ssize_t rmdir_subvol_show(struct kobject *kobj,
305 struct kobj_attribute *ka, char *buf)
306{
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100307 return scnprintf(buf, PAGE_SIZE, "0\n");
Misono Tomohirof902bd32018-05-17 14:24:51 +0900308}
309BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
310
David Sterbaf7cea562019-10-07 11:11:03 +0200311static ssize_t supported_checksums_show(struct kobject *kobj,
312 struct kobj_attribute *a, char *buf)
313{
314 ssize_t ret = 0;
315 int i;
316
317 for (i = 0; i < btrfs_get_num_csums(); i++) {
318 /*
319 * This "trick" only works as long as 'enum btrfs_csum_type' has
320 * no holes in it
321 */
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100322 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
David Sterbaf7cea562019-10-07 11:11:03 +0200323 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
324
325 }
326
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100327 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
David Sterbaf7cea562019-10-07 11:11:03 +0200328 return ret;
329}
330BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
331
Omar Sandoval7573df52020-08-21 00:39:54 -0700332static ssize_t send_stream_version_show(struct kobject *kobj,
333 struct kobj_attribute *ka, char *buf)
334{
335 return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
336}
337BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
338
Josef Bacikceafe3c2020-10-16 11:29:15 -0400339static const char *rescue_opts[] = {
340 "usebackuproot",
341 "nologreplay",
Josef Bacik42437a62020-10-16 11:29:18 -0400342 "ignorebadroots",
Josef Bacik882dbe02020-10-16 11:29:19 -0400343 "ignoredatacsums",
Josef Bacik9037d3c2020-10-16 11:29:20 -0400344 "all",
Josef Bacikceafe3c2020-10-16 11:29:15 -0400345};
346
347static ssize_t supported_rescue_options_show(struct kobject *kobj,
348 struct kobj_attribute *a,
349 char *buf)
350{
351 ssize_t ret = 0;
352 int i;
353
354 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
355 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
356 (i ? " " : ""), rescue_opts[i]);
357 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
358 return ret;
359}
360BTRFS_ATTR(static_feature, supported_rescue_options,
361 supported_rescue_options_show);
362
Misono Tomohirof902bd32018-05-17 14:24:51 +0900363static struct attribute *btrfs_supported_static_feature_attrs[] = {
364 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200365 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Omar Sandoval7573df52020-08-21 00:39:54 -0700366 BTRFS_ATTR_PTR(static_feature, send_stream_version),
Josef Bacikceafe3c2020-10-16 11:29:15 -0400367 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900368 NULL
369};
370
371/*
372 * Features which only depend on kernel version.
373 *
374 * These are listed in /sys/fs/btrfs/features along with
375 * btrfs_feature_attr_group
376 */
377static const struct attribute_group btrfs_static_feature_attr_group = {
378 .name = "features",
379 .attrs = btrfs_supported_static_feature_attrs,
380};
381
David Sterba6e369fe2019-06-13 17:23:02 +0200382#ifdef CONFIG_BTRFS_DEBUG
383
384/*
Dennis Zhoue4faab82019-12-13 16:22:19 -0800385 * Discard statistics and tunables
386 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800387#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
388
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800389static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
390 struct kobj_attribute *a,
391 char *buf)
392{
393 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
394
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100395 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800396 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
397}
398BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
399
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800400static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
401 struct kobj_attribute *a,
402 char *buf)
403{
404 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
405
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100406 return scnprintf(buf, PAGE_SIZE, "%d\n",
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800407 atomic_read(&fs_info->discard_ctl.discardable_extents));
408}
409BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
410
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500411static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
412 struct kobj_attribute *a,
413 char *buf)
414{
415 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
416
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100417 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500418 fs_info->discard_ctl.discard_bitmap_bytes);
419}
420BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
421
422static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
423 struct kobj_attribute *a,
424 char *buf)
425{
426 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
427
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100428 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500429 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
430}
431BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
432
433static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
434 struct kobj_attribute *a,
435 char *buf)
436{
437 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
438
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100439 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500440 fs_info->discard_ctl.discard_extent_bytes);
441}
442BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
443
Dennis Zhoua2309302020-01-02 16:26:35 -0500444static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
445 struct kobj_attribute *a,
446 char *buf)
447{
448 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
449
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100450 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoua2309302020-01-02 16:26:35 -0500451 READ_ONCE(fs_info->discard_ctl.iops_limit));
452}
453
454static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
455 struct kobj_attribute *a,
456 const char *buf, size_t len)
457{
458 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
459 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
460 u32 iops_limit;
461 int ret;
462
463 ret = kstrtou32(buf, 10, &iops_limit);
464 if (ret)
465 return -EINVAL;
466
467 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
Pavel Begunkov3e48d8d2020-11-04 09:45:54 +0000468 btrfs_discard_calc_delay(discard_ctl);
469 btrfs_discard_schedule_work(discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -0500470 return len;
471}
472BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
473 btrfs_discard_iops_limit_store);
474
Dennis Zhoue93591b2020-01-02 16:26:36 -0500475static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
476 struct kobj_attribute *a,
477 char *buf)
478{
479 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
480
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100481 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoue93591b2020-01-02 16:26:36 -0500482 READ_ONCE(fs_info->discard_ctl.kbps_limit));
483}
484
485static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
486 struct kobj_attribute *a,
487 const char *buf, size_t len)
488{
489 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
490 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
491 u32 kbps_limit;
492 int ret;
493
494 ret = kstrtou32(buf, 10, &kbps_limit);
495 if (ret)
496 return -EINVAL;
497
498 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
Pavel Begunkov3e48d8d2020-11-04 09:45:54 +0000499 btrfs_discard_schedule_work(discard_ctl, true);
Dennis Zhoue93591b2020-01-02 16:26:36 -0500500 return len;
501}
502BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
503 btrfs_discard_kbps_limit_store);
504
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500505static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
506 struct kobj_attribute *a,
507 char *buf)
508{
509 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
510
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100511 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500512 READ_ONCE(fs_info->discard_ctl.max_discard_size));
513}
514
515static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
516 struct kobj_attribute *a,
517 const char *buf, size_t len)
518{
519 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
520 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
521 u64 max_discard_size;
522 int ret;
523
524 ret = kstrtou64(buf, 10, &max_discard_size);
525 if (ret)
526 return -EINVAL;
527
528 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
529
530 return len;
531}
532BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
533 btrfs_discard_max_discard_size_store);
534
Dennis Zhoue4faab82019-12-13 16:22:19 -0800535static const struct attribute *discard_debug_attrs[] = {
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800536 BTRFS_ATTR_PTR(discard, discardable_bytes),
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800537 BTRFS_ATTR_PTR(discard, discardable_extents),
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500538 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
539 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
540 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
Dennis Zhoua2309302020-01-02 16:26:35 -0500541 BTRFS_ATTR_PTR(discard, iops_limit),
Dennis Zhoue93591b2020-01-02 16:26:36 -0500542 BTRFS_ATTR_PTR(discard, kbps_limit),
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500543 BTRFS_ATTR_PTR(discard, max_discard_size),
Dennis Zhoue4faab82019-12-13 16:22:19 -0800544 NULL,
545};
546
547/*
David Sterba6e369fe2019-06-13 17:23:02 +0200548 * Runtime debugging exported via sysfs
549 *
550 * /sys/fs/btrfs/debug - applies to module or all filesystems
551 * /sys/fs/btrfs/UUID - applies only to the given filesystem
552 */
Dennis Zhou93945cb2019-12-13 16:22:18 -0800553static const struct attribute *btrfs_debug_mount_attrs[] = {
554 NULL,
555};
556
David Sterba6e369fe2019-06-13 17:23:02 +0200557static struct attribute *btrfs_debug_feature_attrs[] = {
558 NULL
559};
560
561static const struct attribute_group btrfs_debug_feature_attr_group = {
562 .name = "debug",
563 .attrs = btrfs_debug_feature_attrs,
564};
565
566#endif
567
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400568static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
569{
570 u64 val;
571 if (lock)
572 spin_lock(lock);
573 val = *value_ptr;
574 if (lock)
575 spin_unlock(lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100576 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400577}
578
579static ssize_t global_rsv_size_show(struct kobject *kobj,
580 struct kobj_attribute *ka, char *buf)
581{
582 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
583 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
584 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
585}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200586BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400587
588static ssize_t global_rsv_reserved_show(struct kobject *kobj,
589 struct kobj_attribute *a, char *buf)
590{
591 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
592 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
593 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
594}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200595BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400596
597#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400598#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400599
600static ssize_t raid_bytes_show(struct kobject *kobj,
601 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200602BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
603BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400604
605static ssize_t raid_bytes_show(struct kobject *kobj,
606 struct kobj_attribute *attr, char *buf)
607
608{
609 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100610 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400611 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400612 u64 val = 0;
613
614 down_read(&sinfo->groups_sem);
615 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200616 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200617 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400618 else
David Sterbabf38be62019-10-23 18:48:11 +0200619 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400620 }
621 up_read(&sinfo->groups_sem);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100622 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400623}
624
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400625static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200626 BTRFS_ATTR_PTR(raid, total_bytes),
627 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400628 NULL
629};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400630ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400631
632static void release_raid_kobj(struct kobject *kobj)
633{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400634 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400635}
636
David Sterba536ea452019-08-01 17:55:55 +0200637static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400638 .sysfs_ops = &kobj_sysfs_ops,
639 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400640 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400641};
642
643#define SPACE_INFO_ATTR(field) \
644static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
645 struct kobj_attribute *a, \
646 char *buf) \
647{ \
648 struct btrfs_space_info *sinfo = to_space_info(kobj); \
649 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
650} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200651BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400652
653static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
654 struct kobj_attribute *a,
655 char *buf)
656{
657 struct btrfs_space_info *sinfo = to_space_info(kobj);
658 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100659 return scnprintf(buf, PAGE_SIZE, "%lld\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400660}
661
662SPACE_INFO_ATTR(flags);
663SPACE_INFO_ATTR(total_bytes);
664SPACE_INFO_ATTR(bytes_used);
665SPACE_INFO_ATTR(bytes_pinned);
666SPACE_INFO_ATTR(bytes_reserved);
667SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800668SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400669SPACE_INFO_ATTR(disk_used);
670SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200671BTRFS_ATTR(space_info, total_bytes_pinned,
672 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400673
674static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200675 BTRFS_ATTR_PTR(space_info, flags),
676 BTRFS_ATTR_PTR(space_info, total_bytes),
677 BTRFS_ATTR_PTR(space_info, bytes_used),
678 BTRFS_ATTR_PTR(space_info, bytes_pinned),
679 BTRFS_ATTR_PTR(space_info, bytes_reserved),
680 BTRFS_ATTR_PTR(space_info, bytes_may_use),
681 BTRFS_ATTR_PTR(space_info, bytes_readonly),
682 BTRFS_ATTR_PTR(space_info, disk_used),
683 BTRFS_ATTR_PTR(space_info, disk_total),
684 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400685 NULL,
686};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400687ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400688
689static void space_info_release(struct kobject *kobj)
690{
691 struct btrfs_space_info *sinfo = to_space_info(kobj);
692 percpu_counter_destroy(&sinfo->total_bytes_pinned);
693 kfree(sinfo);
694}
695
David Sterba27992d02019-08-01 17:55:55 +0200696static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400697 .sysfs_ops = &kobj_sysfs_ops,
698 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400699 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400700};
701
702static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200703 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
704 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400705 NULL,
706};
707
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400708static ssize_t btrfs_label_show(struct kobject *kobj,
709 struct kobj_attribute *a, char *buf)
710{
711 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900712 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200713 ssize_t ret;
714
715 spin_lock(&fs_info->super_lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100716 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
David Sterbaee17fc82016-04-26 16:22:06 +0200717 spin_unlock(&fs_info->super_lock);
718
719 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400720}
721
722static ssize_t btrfs_label_store(struct kobject *kobj,
723 struct kobj_attribute *a,
724 const char *buf, size_t len)
725{
726 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900727 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400728
David Sterba66ac9fe2016-04-26 16:03:57 +0200729 if (!fs_info)
730 return -EPERM;
731
David Howellsbc98a422017-07-17 08:45:34 +0100732 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800733 return -EROFS;
734
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900735 /*
736 * p_len is the len until the first occurrence of either
737 * '\n' or '\0'
738 */
739 p_len = strcspn(buf, "\n");
740
741 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400742 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400743
David Sterbaa6f69dc2014-05-30 19:29:05 +0200744 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900745 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
746 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200747 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400748
David Sterbaa6f69dc2014-05-30 19:29:05 +0200749 /*
750 * We don't want to do full transaction commit from inside sysfs
751 */
752 btrfs_set_pending(fs_info, COMMIT);
753 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400754
David Sterbaa6f69dc2014-05-30 19:29:05 +0200755 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400756}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200757BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400758
David Sterbadf935892014-05-07 18:17:16 +0200759static ssize_t btrfs_nodesize_show(struct kobject *kobj,
760 struct kobj_attribute *a, char *buf)
761{
762 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
763
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100764 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200765}
766
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200767BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200768
769static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
770 struct kobj_attribute *a, char *buf)
771{
772 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
773
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100774 return scnprintf(buf, PAGE_SIZE, "%u\n",
775 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200776}
777
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200778BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200779
780static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
781 struct kobj_attribute *a, char *buf)
782{
783 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
784
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100785 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200786}
787
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200788BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200789
Sargun Dhillon27234802017-05-11 21:18:03 +0000790static ssize_t quota_override_show(struct kobject *kobj,
791 struct kobj_attribute *a, char *buf)
792{
793 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
794 int quota_override;
795
796 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100797 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
Sargun Dhillon27234802017-05-11 21:18:03 +0000798}
799
800static ssize_t quota_override_store(struct kobject *kobj,
801 struct kobj_attribute *a,
802 const char *buf, size_t len)
803{
804 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
805 unsigned long knob;
806 int err;
807
808 if (!fs_info)
809 return -EPERM;
810
811 if (!capable(CAP_SYS_RESOURCE))
812 return -EPERM;
813
814 err = kstrtoul(buf, 10, &knob);
815 if (err)
816 return err;
817 if (knob > 1)
818 return -EINVAL;
819
820 if (knob)
821 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
822 else
823 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
824
825 return len;
826}
827
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200828BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000829
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200830static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
831 struct kobj_attribute *a, char *buf)
832{
833 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
834
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100835 return scnprintf(buf, PAGE_SIZE, "%pU\n",
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200836 fs_info->fs_devices->metadata_uuid);
837}
838
839BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
840
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200841static ssize_t btrfs_checksum_show(struct kobject *kobj,
842 struct kobj_attribute *a, char *buf)
843{
844 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
845 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
846
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100847 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200848 btrfs_super_csum_name(csum_type),
849 crypto_shash_driver_name(fs_info->csum_shash));
850}
851
852BTRFS_ATTR(, checksum, btrfs_checksum_show);
853
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500854static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
855 struct kobj_attribute *a, char *buf)
856{
857 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
858 const char *str;
859
860 switch (READ_ONCE(fs_info->exclusive_operation)) {
861 case BTRFS_EXCLOP_NONE:
862 str = "none\n";
863 break;
864 case BTRFS_EXCLOP_BALANCE:
865 str = "balance\n";
866 break;
867 case BTRFS_EXCLOP_DEV_ADD:
868 str = "device add\n";
869 break;
870 case BTRFS_EXCLOP_DEV_REMOVE:
871 str = "device remove\n";
872 break;
873 case BTRFS_EXCLOP_DEV_REPLACE:
874 str = "device replace\n";
875 break;
876 case BTRFS_EXCLOP_RESIZE:
877 str = "resize\n";
878 break;
879 case BTRFS_EXCLOP_SWAP_ACTIVATE:
880 str = "swap activate\n";
881 break;
882 default:
883 str = "UNKNOWN\n";
884 break;
885 }
886 return scnprintf(buf, PAGE_SIZE, "%s", str);
887}
888BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
889
Anand Jain089c8b02020-10-07 15:20:03 +0800890static ssize_t btrfs_generation_show(struct kobject *kobj,
891 struct kobj_attribute *a, char *buf)
892{
893 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
894
895 return scnprintf(buf, PAGE_SIZE, "%llu\n", fs_info->generation);
896}
897BTRFS_ATTR(, generation, btrfs_generation_show);
898
Anand Jainaaefed22020-10-28 21:14:45 +0800899/*
900 * Look for an exact string @string in @buffer with possible leading or
901 * trailing whitespace
902 */
903static bool strmatch(const char *buffer, const char *string)
904{
905 const size_t len = strlen(string);
906
907 /* Skip leading whitespace */
908 buffer = skip_spaces(buffer);
909
910 /* Match entire string, check if the rest is whitespace or empty */
911 if (strncmp(string, buffer, len) == 0 &&
912 strlen(skip_spaces(buffer + len)) == 0)
913 return true;
914
915 return false;
916}
917
Anand Jain3d8cc172020-10-28 21:14:47 +0800918static const char * const btrfs_read_policy_name[] = { "pid" };
919
920static ssize_t btrfs_read_policy_show(struct kobject *kobj,
921 struct kobj_attribute *a, char *buf)
922{
923 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
924 ssize_t ret = 0;
925 int i;
926
927 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
928 if (fs_devices->read_policy == i)
929 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
930 (ret == 0 ? "" : " "),
931 btrfs_read_policy_name[i]);
932 else
933 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
934 (ret == 0 ? "" : " "),
935 btrfs_read_policy_name[i]);
936 }
937
938 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
939
940 return ret;
941}
942
943static ssize_t btrfs_read_policy_store(struct kobject *kobj,
944 struct kobj_attribute *a,
945 const char *buf, size_t len)
946{
947 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
948 int i;
949
950 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
951 if (strmatch(buf, btrfs_read_policy_name[i])) {
952 if (i != fs_devices->read_policy) {
953 fs_devices->read_policy = i;
954 btrfs_info(fs_devices->fs_info,
955 "read policy set to '%s'",
956 btrfs_read_policy_name[i]);
957 }
958 return len;
959 }
960 }
961
962 return -EINVAL;
963}
964BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
965
Anand Jain0dd29062015-03-10 06:38:27 +0800966static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200967 BTRFS_ATTR_PTR(, label),
968 BTRFS_ATTR_PTR(, nodesize),
969 BTRFS_ATTR_PTR(, sectorsize),
970 BTRFS_ATTR_PTR(, clone_alignment),
971 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200972 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200973 BTRFS_ATTR_PTR(, checksum),
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500974 BTRFS_ATTR_PTR(, exclusive_operation),
Anand Jain089c8b02020-10-07 15:20:03 +0800975 BTRFS_ATTR_PTR(, generation),
Anand Jain3d8cc172020-10-28 21:14:47 +0800976 BTRFS_ATTR_PTR(, read_policy),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400977 NULL,
978};
979
Anand Jainc1b7e472015-08-14 18:32:50 +0800980static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400981{
Anand Jain2e7910d2015-03-10 06:38:29 +0800982 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800983
Anand Jainc1b7e472015-08-14 18:32:50 +0800984 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800985 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400986}
987
988static struct kobj_type btrfs_ktype = {
989 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800990 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400991};
992
Anand Jain2e7910d2015-03-10 06:38:29 +0800993static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
994{
995 if (kobj->ktype != &btrfs_ktype)
996 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800997 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800998}
999
Jeff Mahoney510d7362013-11-01 13:06:59 -04001000static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1001{
1002 if (kobj->ktype != &btrfs_ktype)
1003 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +08001004 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001005}
Josef Bacik58176a92007-08-29 15:47:34 -04001006
Jeff Mahoneye453d982013-11-21 10:37:16 -05001007#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +09001008#define BTRFS_FEATURE_NAME_MAX 13
1009static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1010static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -05001011
Tomohiro Misono6c521572018-05-16 17:09:26 +09001012static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -05001013 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1014 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1015 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1016};
1017
1018static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001019{
Jeff Mahoneye453d982013-11-21 10:37:16 -05001020 int set;
1021
1022 for (set = 0; set < FEAT_MAX; set++) {
1023 int i;
1024 struct attribute *attrs[2];
1025 struct attribute_group agroup = {
1026 .name = "features",
1027 .attrs = attrs,
1028 };
1029 u64 features = get_features(fs_info, set);
1030 features &= ~supported_feature_masks[set];
1031
1032 if (!features)
1033 continue;
1034
1035 attrs[1] = NULL;
1036 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1037 struct btrfs_feature_attr *fa;
1038
1039 if (!(features & (1ULL << i)))
1040 continue;
1041
1042 fa = &btrfs_feature_attrs[set][i];
1043 attrs[0] = &fa->kobj_attr.attr;
1044 if (add) {
1045 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +08001046 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -05001047 &agroup);
1048 if (ret)
1049 return ret;
1050 } else
Anand Jainc1b7e472015-08-14 18:32:50 +08001051 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -05001052 &agroup);
1053 }
1054
1055 }
1056 return 0;
1057}
1058
Anand Jain2e3e1282015-03-10 06:38:32 +08001059static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -05001060{
Anand Jaina013d142020-02-12 17:28:10 +08001061 if (fs_devs->devinfo_kobj) {
1062 kobject_del(fs_devs->devinfo_kobj);
1063 kobject_put(fs_devs->devinfo_kobj);
1064 fs_devs->devinfo_kobj = NULL;
1065 }
1066
Anand Jainb5501502019-11-21 17:33:30 +08001067 if (fs_devs->devices_kobj) {
1068 kobject_del(fs_devs->devices_kobj);
1069 kobject_put(fs_devs->devices_kobj);
1070 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +08001071 }
1072
Anand Jainc1b7e472015-08-14 18:32:50 +08001073 if (fs_devs->fsid_kobj.state_initialized) {
1074 kobject_del(&fs_devs->fsid_kobj);
1075 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +08001076 wait_for_completion(&fs_devs->kobj_unregister);
1077 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001078}
1079
Anand Jain2e3e1282015-03-10 06:38:32 +08001080/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +08001081void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +08001082{
1083 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1084
1085 if (fs_devs) {
1086 __btrfs_sysfs_remove_fsid(fs_devs);
1087 return;
1088 }
1089
Anand Jainc4babc52018-04-12 10:29:25 +08001090 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +08001091 __btrfs_sysfs_remove_fsid(fs_devs);
1092 }
1093}
1094
Anand Jain53f8a742020-09-05 01:34:27 +08001095static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1096{
1097 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001098 struct btrfs_fs_devices *seed;
Anand Jain53f8a742020-09-05 01:34:27 +08001099
1100 list_for_each_entry(device, &fs_devices->devices, dev_list)
1101 btrfs_sysfs_remove_device(device);
Anand Jain30b0e4e2020-09-05 01:34:28 +08001102
1103 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1104 list_for_each_entry(device, &seed->devices, dev_list)
1105 btrfs_sysfs_remove_device(device);
1106 }
Anand Jain53f8a742020-09-05 01:34:27 +08001107}
1108
Anand Jain6618a592015-08-14 18:32:47 +08001109void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -05001110{
Nikolay Borisov3092c682020-07-03 11:13:15 +03001111 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1112
Nikolay Borisov3092c682020-07-03 11:13:15 +03001113 sysfs_remove_link(fsid_kobj, "bdi");
1114
Jeff Mahoneye453d982013-11-21 10:37:16 -05001115 if (fs_info->space_info_kobj) {
1116 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1117 kobject_del(fs_info->space_info_kobj);
1118 kobject_put(fs_info->space_info_kobj);
1119 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001120#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhoue4faab82019-12-13 16:22:19 -08001121 if (fs_info->discard_debug_kobj) {
1122 sysfs_remove_files(fs_info->discard_debug_kobj,
1123 discard_debug_attrs);
1124 kobject_del(fs_info->discard_debug_kobj);
1125 kobject_put(fs_info->discard_debug_kobj);
1126 }
Dennis Zhou93945cb2019-12-13 16:22:18 -08001127 if (fs_info->debug_kobj) {
1128 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1129 kobject_del(fs_info->debug_kobj);
1130 kobject_put(fs_info->debug_kobj);
1131 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001132#endif
Jeff Mahoneye453d982013-11-21 10:37:16 -05001133 addrm_unknown_feature_attrs(fs_info, false);
Nikolay Borisov3092c682020-07-03 11:13:15 +03001134 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1135 sysfs_remove_files(fsid_kobj, btrfs_attrs);
Anand Jain53f8a742020-09-05 01:34:27 +08001136 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001137}
1138
David Sterbaf10152b2019-08-01 19:07:55 +02001139static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001140 [FEAT_COMPAT] = "compat",
1141 [FEAT_COMPAT_RO] = "compat_ro",
1142 [FEAT_INCOMPAT] = "incompat",
1143};
1144
David Sterba9e6df7c2020-08-17 10:56:00 +02001145const char *btrfs_feature_set_name(enum btrfs_feature_set set)
David Sterbaf10152b2019-08-01 19:07:55 +02001146{
1147 return btrfs_feature_set_names[set];
1148}
1149
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001150char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1151{
1152 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1153 int len = 0;
1154 int i;
1155 char *str;
1156
1157 str = kmalloc(bufsize, GFP_KERNEL);
1158 if (!str)
1159 return str;
1160
1161 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1162 const char *name;
1163
1164 if (!(flags & (1ULL << i)))
1165 continue;
1166
1167 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001168 len += scnprintf(str + len, bufsize - len, "%s%s",
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001169 len ? "," : "", name);
1170 }
1171
1172 return str;
1173}
1174
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001175static void init_feature_attrs(void)
1176{
1177 struct btrfs_feature_attr *fa;
1178 int set, i;
1179
1180 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1181 ARRAY_SIZE(btrfs_feature_attrs));
1182 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1183 ARRAY_SIZE(btrfs_feature_attrs[0]));
1184
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001185 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1186 memset(btrfs_unknown_feature_names, 0,
1187 sizeof(btrfs_unknown_feature_names));
1188
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001189 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1190 struct btrfs_feature_attr *sfa;
1191 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001192 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001193 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001194 bit = ilog2(sfa->feature_bit);
1195 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001196
1197 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1198 }
1199
1200 for (set = 0; set < FEAT_MAX; set++) {
1201 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1202 char *name = btrfs_unknown_feature_names[set][i];
1203 fa = &btrfs_feature_attrs[set][i];
1204
1205 if (fa->kobj_attr.attr.name)
1206 continue;
1207
Tomohiro Misono6c521572018-05-16 17:09:26 +09001208 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001209 btrfs_feature_set_names[set], i);
1210
1211 fa->kobj_attr.attr.name = name;
1212 fa->kobj_attr.attr.mode = S_IRUGO;
1213 fa->feature_set = set;
1214 fa->feature_bit = 1ULL << i;
1215 }
1216 }
1217}
1218
David Sterba32a99912019-08-01 17:49:55 +02001219/*
1220 * Create a sysfs entry for a given block group type at path
1221 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1222 */
David Sterba32da53862019-10-29 19:20:18 +01001223void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +02001224{
1225 struct btrfs_fs_info *fs_info = cache->fs_info;
1226 struct btrfs_space_info *space_info = cache->space_info;
1227 struct raid_kobject *rkobj;
1228 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1229 unsigned int nofs_flag;
1230 int ret;
1231
1232 /*
1233 * Setup a NOFS context because kobject_add(), deep in its call chain,
1234 * does GFP_KERNEL allocations, and we are often called in a context
1235 * where if reclaim is triggered we can deadlock (we are either holding
1236 * a transaction handle or some lock required for a transaction
1237 * commit).
1238 */
1239 nofs_flag = memalloc_nofs_save();
1240
1241 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1242 if (!rkobj) {
1243 memalloc_nofs_restore(nofs_flag);
1244 btrfs_warn(cache->fs_info,
1245 "couldn't alloc memory for raid level kobject");
1246 return;
1247 }
1248
1249 rkobj->flags = cache->flags;
1250 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
Josef Bacik49ea1122020-09-01 17:40:38 -04001251
1252 /*
1253 * We call this either on mount, or if we've created a block group for a
1254 * new index type while running (i.e. when restriping). The running
1255 * case is tricky because we could race with other threads, so we need
1256 * to have this check to make sure we didn't already init the kobject.
1257 *
1258 * We don't have to protect on the free side because it only happens on
1259 * unmount.
1260 */
1261 spin_lock(&space_info->lock);
1262 if (space_info->block_group_kobjs[index]) {
1263 spin_unlock(&space_info->lock);
1264 kobject_put(&rkobj->kobj);
1265 return;
1266 } else {
1267 space_info->block_group_kobjs[index] = &rkobj->kobj;
1268 }
1269 spin_unlock(&space_info->lock);
1270
David Sterba32a99912019-08-01 17:49:55 +02001271 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1272 btrfs_bg_type_to_raid_name(rkobj->flags));
1273 memalloc_nofs_restore(nofs_flag);
1274 if (ret) {
Josef Bacik49ea1122020-09-01 17:40:38 -04001275 spin_lock(&space_info->lock);
1276 space_info->block_group_kobjs[index] = NULL;
1277 spin_unlock(&space_info->lock);
David Sterba32a99912019-08-01 17:49:55 +02001278 kobject_put(&rkobj->kobj);
1279 btrfs_warn(fs_info,
1280 "failed to add kobject for block cache, ignoring");
1281 return;
1282 }
David Sterba32a99912019-08-01 17:49:55 +02001283}
1284
David Sterbab5865ba2019-08-01 18:50:16 +02001285/*
1286 * Remove sysfs directories for all block group types of a given space info and
1287 * the space info as well
1288 */
1289void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1290{
1291 int i;
1292
1293 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1294 struct kobject *kobj;
1295
1296 kobj = space_info->block_group_kobjs[i];
1297 space_info->block_group_kobjs[i] = NULL;
1298 if (kobj) {
1299 kobject_del(kobj);
1300 kobject_put(kobj);
1301 }
1302 }
1303 kobject_del(&space_info->kobj);
1304 kobject_put(&space_info->kobj);
1305}
1306
David Sterbab8823272019-08-01 18:50:16 +02001307static const char *alloc_name(u64 flags)
1308{
1309 switch (flags) {
1310 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1311 return "mixed";
1312 case BTRFS_BLOCK_GROUP_METADATA:
1313 return "metadata";
1314 case BTRFS_BLOCK_GROUP_DATA:
1315 return "data";
1316 case BTRFS_BLOCK_GROUP_SYSTEM:
1317 return "system";
1318 default:
1319 WARN_ON(1);
1320 return "invalid-combination";
Tom Rix445d8ab2020-11-01 07:30:08 -08001321 }
David Sterbab8823272019-08-01 18:50:16 +02001322}
1323
1324/*
1325 * Create a sysfs entry for a space info type at path
1326 * /sys/fs/btrfs/UUID/allocation/TYPE
1327 */
1328int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1329 struct btrfs_space_info *space_info)
1330{
1331 int ret;
1332
1333 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1334 fs_info->space_info_kobj, "%s",
1335 alloc_name(space_info->flags));
1336 if (ret) {
1337 kobject_put(&space_info->kobj);
1338 return ret;
1339 }
1340
1341 return 0;
1342}
1343
Anand Jain53f8a742020-09-05 01:34:27 +08001344void btrfs_sysfs_remove_device(struct btrfs_device *device)
Anand Jain99994cd2014-06-03 11:36:00 +08001345{
1346 struct hd_struct *disk;
1347 struct kobject *disk_kobj;
Anand Jain985e2332020-09-05 01:34:24 +08001348 struct kobject *devices_kobj;
Anand Jain99994cd2014-06-03 11:36:00 +08001349
Anand Jain985e2332020-09-05 01:34:24 +08001350 /*
1351 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1352 * fs_info::fs_devices.
1353 */
1354 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1355 ASSERT(devices_kobj);
Anand Jain99994cd2014-06-03 11:36:00 +08001356
Anand Jain985e2332020-09-05 01:34:24 +08001357 if (device->bdev) {
1358 disk = device->bdev->bd_part;
1359 disk_kobj = &part_to_dev(disk)->kobj;
1360 sysfs_remove_link(devices_kobj, disk_kobj->name);
1361 }
Anand Jain99994cd2014-06-03 11:36:00 +08001362
Anand Jain985e2332020-09-05 01:34:24 +08001363 if (device->devid_kobj.state_initialized) {
1364 kobject_del(&device->devid_kobj);
1365 kobject_put(&device->devid_kobj);
1366 wait_for_completion(&device->kobj_unregister);
1367 }
1368}
Anand Jain668e48af2020-01-06 19:38:31 +08001369
Anand Jain668e48af2020-01-06 19:38:31 +08001370static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1371 struct kobj_attribute *a,
1372 char *buf)
1373{
1374 int val;
1375 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1376 devid_kobj);
1377
1378 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1379
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001380 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001381}
1382BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1383
Anand Jain25864772020-02-13 16:40:53 +08001384static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
Anand Jain668e48af2020-01-06 19:38:31 +08001385 struct kobj_attribute *a, char *buf)
1386{
1387 int val;
1388 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1389 devid_kobj);
1390
1391 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1392
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001393 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001394}
Anand Jain25864772020-02-13 16:40:53 +08001395BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
Anand Jain668e48af2020-01-06 19:38:31 +08001396
1397static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1398 struct kobj_attribute *a,
1399 char *buf)
1400{
1401 int val;
1402 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1403 devid_kobj);
1404
1405 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1406
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001407 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001408}
1409BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1410
1411static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1412 struct kobj_attribute *a, char *buf)
1413{
1414 int val;
1415 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1416 devid_kobj);
1417
1418 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1419
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001420 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001421}
1422BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1423
1424static struct attribute *devid_attrs[] = {
1425 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1426 BTRFS_ATTR_PTR(devid, missing),
1427 BTRFS_ATTR_PTR(devid, replace_target),
1428 BTRFS_ATTR_PTR(devid, writeable),
1429 NULL
1430};
1431ATTRIBUTE_GROUPS(devid);
1432
1433static void btrfs_release_devid_kobj(struct kobject *kobj)
1434{
1435 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1436 devid_kobj);
1437
1438 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1439 complete(&device->kobj_unregister);
1440}
1441
1442static struct kobj_type devid_ktype = {
1443 .sysfs_ops = &kobj_sysfs_ops,
1444 .default_groups = devid_groups,
1445 .release = btrfs_release_devid_kobj,
1446};
1447
Anand Jaincd36da22020-09-05 01:34:26 +08001448int btrfs_sysfs_add_device(struct btrfs_device *device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001449{
Anand Jain178a16c2020-09-05 01:34:23 +08001450 int ret;
Josef Bacika47bd782020-07-21 10:17:50 -04001451 unsigned int nofs_flag;
Anand Jain178a16c2020-09-05 01:34:23 +08001452 struct kobject *devices_kobj;
1453 struct kobject *devinfo_kobj;
1454
1455 /*
1456 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1457 * for the seed fs_devices
1458 */
1459 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1460 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1461 ASSERT(devices_kobj);
1462 ASSERT(devinfo_kobj);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001463
Josef Bacika47bd782020-07-21 10:17:50 -04001464 nofs_flag = memalloc_nofs_save();
Anand Jainf0853812014-01-15 17:22:28 +08001465
Anand Jain178a16c2020-09-05 01:34:23 +08001466 if (device->bdev) {
1467 struct hd_struct *disk;
1468 struct kobject *disk_kobj;
Anand Jain0d393762014-06-03 11:36:01 +08001469
Anand Jain178a16c2020-09-05 01:34:23 +08001470 disk = device->bdev->bd_part;
1471 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001472
Anand Jain178a16c2020-09-05 01:34:23 +08001473 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1474 if (ret) {
1475 btrfs_warn(device->fs_info,
1476 "creating sysfs device link for devid %llu failed: %d",
1477 device->devid, ret);
1478 goto out;
Anand Jain668e48af2020-01-06 19:38:31 +08001479 }
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001480 }
1481
Anand Jain178a16c2020-09-05 01:34:23 +08001482 init_completion(&device->kobj_unregister);
1483 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1484 devinfo_kobj, "%llu", device->devid);
1485 if (ret) {
1486 kobject_put(&device->devid_kobj);
1487 btrfs_warn(device->fs_info,
1488 "devinfo init for devid %llu failed: %d",
1489 device->devid, ret);
1490 }
1491
1492out:
1493 memalloc_nofs_restore(nofs_flag);
1494 return ret;
1495}
1496
Anand Jaincd36da22020-09-05 01:34:26 +08001497static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
Anand Jain178a16c2020-09-05 01:34:23 +08001498{
1499 int ret;
Anand Jaincd36da22020-09-05 01:34:26 +08001500 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001501 struct btrfs_fs_devices *seed;
Anand Jain178a16c2020-09-05 01:34:23 +08001502
1503 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1504 ret = btrfs_sysfs_add_device(device);
1505 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001506 goto fail;
Anand Jain178a16c2020-09-05 01:34:23 +08001507 }
1508
Anand Jain30b0e4e2020-09-05 01:34:28 +08001509 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1510 list_for_each_entry(device, &seed->devices, dev_list) {
1511 ret = btrfs_sysfs_add_device(device);
1512 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001513 goto fail;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001514 }
1515 }
1516
Anand Jain178a16c2020-09-05 01:34:23 +08001517 return 0;
Anand Jain7ad39122020-09-05 01:34:29 +08001518
1519fail:
1520 btrfs_sysfs_remove_fs_devices(fs_devices);
1521 return ret;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001522}
1523
David Sterba5b286922019-08-01 18:50:16 +02001524void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1525{
1526 int ret;
1527
1528 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1529 if (ret)
1530 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1531 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1532 &disk_to_dev(bdev->bd_disk)->kobj);
1533}
1534
Nikolay Borisov8e560082020-08-12 16:18:51 +03001535void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1536
David Sterbaf93c3992019-08-01 18:50:16 +02001537{
1538 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1539
1540 /*
1541 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1542 * directory
1543 */
Nikolay Borisov8e560082020-08-12 16:18:51 +03001544 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
David Sterbaf93c3992019-08-01 18:50:16 +02001545 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1546 btrfs_warn(fs_devices->fs_info,
1547 "sysfs: failed to create fsid for sprout");
1548}
1549
Anand Jain668e48af2020-01-06 19:38:31 +08001550void btrfs_sysfs_update_devid(struct btrfs_device *device)
1551{
1552 char tmp[24];
1553
1554 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1555
1556 if (kobject_rename(&device->devid_kobj, tmp))
1557 btrfs_warn(device->fs_devices->fs_info,
1558 "sysfs: failed to update devid for %llu",
1559 device->devid);
1560}
1561
Jeff Mahoney510d7362013-11-01 13:06:59 -04001562/* /sys/fs/btrfs/ entry */
1563static struct kset *btrfs_kset;
1564
Anand Jain72059212015-03-10 06:38:26 +08001565/*
Anand Jainc6761a92019-11-21 17:33:32 +08001566 * Creates:
1567 * /sys/fs/btrfs/UUID
1568 *
Anand Jain72059212015-03-10 06:38:26 +08001569 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001570 */
Anand Jainc6761a92019-11-21 17:33:32 +08001571int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001572{
1573 int error;
1574
Anand Jain2e7910d2015-03-10 06:38:29 +08001575 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001576 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001577 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1578 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001579 if (error) {
1580 kobject_put(&fs_devs->fsid_kobj);
1581 return error;
1582 }
1583
Anand Jainbc036bb2019-11-21 17:33:34 +08001584 fs_devs->devices_kobj = kobject_create_and_add("devices",
1585 &fs_devs->fsid_kobj);
1586 if (!fs_devs->devices_kobj) {
1587 btrfs_err(fs_devs->fs_info,
1588 "failed to init sysfs device interface");
Anand Jain1f6087e2020-02-12 17:28:12 +08001589 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainbc036bb2019-11-21 17:33:34 +08001590 return -ENOMEM;
1591 }
1592
Anand Jaina013d142020-02-12 17:28:10 +08001593 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1594 &fs_devs->fsid_kobj);
1595 if (!fs_devs->devinfo_kobj) {
1596 btrfs_err(fs_devs->fs_info,
1597 "failed to init sysfs devinfo kobject");
1598 btrfs_sysfs_remove_fsid(fs_devs);
1599 return -ENOMEM;
1600 }
1601
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001602 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001603}
1604
Anand Jain96f31362015-08-14 18:32:46 +08001605int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001606{
1607 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001608 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001609 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001610
Anand Jaincd36da22020-09-05 01:34:26 +08001611 error = btrfs_sysfs_add_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001612 if (error)
1613 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001614
Anand Jainc1b7e472015-08-14 18:32:50 +08001615 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001616 if (error) {
Anand Jain53f8a742020-09-05 01:34:27 +08001617 btrfs_sysfs_remove_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001618 return error;
1619 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001620
Anand Jainc1b7e472015-08-14 18:32:50 +08001621 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001622 &btrfs_feature_attr_group);
1623 if (error)
1624 goto failure;
1625
David Sterba6e369fe2019-06-13 17:23:02 +02001626#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhou93945cb2019-12-13 16:22:18 -08001627 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1628 if (!fs_info->debug_kobj) {
1629 error = -ENOMEM;
1630 goto failure;
1631 }
1632
1633 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
David Sterba6e369fe2019-06-13 17:23:02 +02001634 if (error)
1635 goto failure;
Dennis Zhoue4faab82019-12-13 16:22:19 -08001636
1637 /* Discard directory */
1638 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1639 fs_info->debug_kobj);
1640 if (!fs_info->discard_debug_kobj) {
1641 error = -ENOMEM;
1642 goto failure;
1643 }
1644
1645 error = sysfs_create_files(fs_info->discard_debug_kobj,
1646 discard_debug_attrs);
1647 if (error)
1648 goto failure;
David Sterba6e369fe2019-06-13 17:23:02 +02001649#endif
1650
Jeff Mahoneye453d982013-11-21 10:37:16 -05001651 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001652 if (error)
1653 goto failure;
1654
Nikolay Borisov3092c682020-07-03 11:13:15 +03001655 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1656 if (error)
1657 goto failure;
1658
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001659 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001660 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001661 if (!fs_info->space_info_kobj) {
1662 error = -ENOMEM;
1663 goto failure;
1664 }
1665
1666 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1667 if (error)
1668 goto failure;
1669
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001670 return 0;
1671failure:
Anand Jain6618a592015-08-14 18:32:47 +08001672 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001673 return error;
1674}
1675
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001676static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1677{
1678 return to_fs_info(kobj->parent->parent);
1679}
1680
1681#define QGROUP_ATTR(_member, _show_name) \
1682static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1683 struct kobj_attribute *a, \
1684 char *buf) \
1685{ \
1686 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1687 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1688 struct btrfs_qgroup, kobj); \
1689 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1690} \
1691BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1692
1693#define QGROUP_RSV_ATTR(_name, _type) \
1694static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1695 struct kobj_attribute *a, \
1696 char *buf) \
1697{ \
1698 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1699 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1700 struct btrfs_qgroup, kobj); \
1701 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1702 &fs_info->qgroup_lock, buf); \
1703} \
1704BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1705
1706QGROUP_ATTR(rfer, referenced);
1707QGROUP_ATTR(excl, exclusive);
1708QGROUP_ATTR(max_rfer, max_referenced);
1709QGROUP_ATTR(max_excl, max_exclusive);
1710QGROUP_ATTR(lim_flags, limit_flags);
1711QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1712QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1713QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1714
1715static struct attribute *qgroup_attrs[] = {
1716 BTRFS_ATTR_PTR(qgroup, referenced),
1717 BTRFS_ATTR_PTR(qgroup, exclusive),
1718 BTRFS_ATTR_PTR(qgroup, max_referenced),
1719 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1720 BTRFS_ATTR_PTR(qgroup, limit_flags),
1721 BTRFS_ATTR_PTR(qgroup, rsv_data),
1722 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1723 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1724 NULL
1725};
1726ATTRIBUTE_GROUPS(qgroup);
1727
1728static void qgroup_release(struct kobject *kobj)
1729{
1730 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1731
1732 memset(&qgroup->kobj, 0, sizeof(*kobj));
1733}
1734
1735static struct kobj_type qgroup_ktype = {
1736 .sysfs_ops = &kobj_sysfs_ops,
1737 .release = qgroup_release,
1738 .default_groups = qgroup_groups,
1739};
1740
1741int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1742 struct btrfs_qgroup *qgroup)
1743{
1744 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1745 int ret;
1746
1747 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1748 return 0;
1749 if (qgroup->kobj.state_initialized)
1750 return 0;
1751 if (!qgroups_kobj)
1752 return -EINVAL;
1753
1754 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1755 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1756 btrfs_qgroup_subvolid(qgroup->qgroupid));
1757 if (ret < 0)
1758 kobject_put(&qgroup->kobj);
1759
1760 return ret;
1761}
1762
1763void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1764{
1765 struct btrfs_qgroup *qgroup;
1766 struct btrfs_qgroup *next;
1767
1768 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1769 return;
1770
1771 rbtree_postorder_for_each_entry_safe(qgroup, next,
1772 &fs_info->qgroup_tree, node)
1773 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
Qu Wenruo62ab2cc2020-08-03 14:20:11 +08001774 if (fs_info->qgroups_kobj) {
1775 kobject_del(fs_info->qgroups_kobj);
1776 kobject_put(fs_info->qgroups_kobj);
1777 fs_info->qgroups_kobj = NULL;
1778 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001779}
1780
1781/* Called when qgroups get initialized, thus there is no need for locking */
1782int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1783{
1784 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1785 struct btrfs_qgroup *qgroup;
1786 struct btrfs_qgroup *next;
1787 int ret = 0;
1788
1789 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1790 return 0;
1791
1792 ASSERT(fsid_kobj);
1793 if (fs_info->qgroups_kobj)
1794 return 0;
1795
1796 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1797 if (!fs_info->qgroups_kobj) {
1798 ret = -ENOMEM;
1799 goto out;
1800 }
1801 rbtree_postorder_for_each_entry_safe(qgroup, next,
1802 &fs_info->qgroup_tree, node) {
1803 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1804 if (ret < 0)
1805 goto out;
1806 }
1807
1808out:
1809 if (ret < 0)
1810 btrfs_sysfs_del_qgroups(fs_info);
1811 return ret;
1812}
1813
1814void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1815 struct btrfs_qgroup *qgroup)
1816{
1817 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1818 return;
1819
1820 if (qgroup->kobj.state_initialized) {
1821 kobject_del(&qgroup->kobj);
1822 kobject_put(&qgroup->kobj);
1823 }
1824}
David Sterba444e7512016-01-21 18:50:40 +01001825
1826/*
1827 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1828 * values in superblock. Call after any changes to incompat/compat_ro flags
1829 */
1830void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1831 u64 bit, enum btrfs_feature_set set)
1832{
1833 struct btrfs_fs_devices *fs_devs;
1834 struct kobject *fsid_kobj;
Leon Romanovsky24646482020-08-19 17:16:28 +03001835 u64 __maybe_unused features;
1836 int __maybe_unused ret;
David Sterba444e7512016-01-21 18:50:40 +01001837
1838 if (!fs_info)
1839 return;
1840
Leon Romanovsky24646482020-08-19 17:16:28 +03001841 /*
1842 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
1843 * safe when called from some contexts (eg. balance)
1844 */
David Sterba444e7512016-01-21 18:50:40 +01001845 features = get_features(fs_info, set);
1846 ASSERT(bit & supported_feature_masks[set]);
1847
1848 fs_devs = fs_info->fs_devices;
1849 fsid_kobj = &fs_devs->fsid_kobj;
1850
David Sterbabf609202016-01-27 14:06:29 +01001851 if (!fsid_kobj->state_initialized)
1852 return;
1853
David Sterba444e7512016-01-21 18:50:40 +01001854 /*
1855 * FIXME: this is too heavy to update just one value, ideally we'd like
1856 * to use sysfs_update_group but some refactoring is needed first.
1857 */
1858 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1859 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1860}
1861
Liu Bof5c29bd2017-11-02 17:21:50 -06001862int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001863{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001864 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001865
Greg KHe3fe4e72008-02-20 14:14:16 -05001866 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1867 if (!btrfs_kset)
1868 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001869
David Sterba1bae3092014-02-05 15:36:18 +01001870 init_feature_attrs();
1871 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001872 if (ret)
1873 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001874 ret = sysfs_merge_group(&btrfs_kset->kobj,
1875 &btrfs_static_feature_attr_group);
1876 if (ret)
1877 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001878
David Sterba6e369fe2019-06-13 17:23:02 +02001879#ifdef CONFIG_BTRFS_DEBUG
1880 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1881 if (ret)
1882 goto out2;
1883#endif
1884
Filipe Manana001a6482015-01-23 18:27:00 +00001885 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001886
1887out_remove_group:
1888 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001889out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001890 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001891
1892 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001893}
1894
David Sterbae67c7182018-02-19 17:24:18 +01001895void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001896{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001897 sysfs_unmerge_group(&btrfs_kset->kobj,
1898 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001899 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Dennis Zhou71e89782019-12-13 16:22:17 -08001900#ifdef CONFIG_BTRFS_DEBUG
1901 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1902#endif
Greg KHe3fe4e72008-02-20 14:14:16 -05001903 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001904}
Chris Mason55d47412008-02-20 16:02:51 -05001905