blob: beb7f72d50b86871dabda18e50a45b79bd944e40 [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
Anand Jaine7849e32021-08-10 21:55:59 +080025/*
26 * Structure name Path
27 * --------------------------------------------------------------------------
28 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
29 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and
30 * /sys/fs/btrfs/<uuid>/features
31 * btrfs_attrs /sys/fs/btrfs/<uuid>
32 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid>
33 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation
34 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
35 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>
36 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
37 *
38 * When built with BTRFS_CONFIG_DEBUG:
39 *
40 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
41 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
42 * discard_debug_attrs /sys/fs/btrfs/<uuid>/debug/discard
43 */
44
David Sterba9188db62019-08-02 12:52:48 +020045struct btrfs_feature_attr {
46 struct kobj_attribute kobj_attr;
47 enum btrfs_feature_set feature_set;
48 u64 feature_bit;
49};
50
51/* For raid type sysfs entries */
52struct raid_kobject {
53 u64 flags;
54 struct kobject kobj;
55};
56
57#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
58{ \
59 .attr = { .name = __stringify(_name), .mode = _mode }, \
60 .show = _show, \
61 .store = _store, \
62}
63
64#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
65 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
66 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
67
68#define BTRFS_ATTR(_prefix, _name, _show) \
69 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
70 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
71
72#define BTRFS_ATTR_PTR(_prefix, _name) \
73 (&btrfs_attr_##_prefix##_##_name.attr)
74
75#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
76static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
77 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
78 btrfs_feature_attr_show, \
79 btrfs_feature_attr_store), \
80 .feature_set = _feature_set, \
81 .feature_bit = _feature_prefix ##_## _feature_bit, \
82}
83#define BTRFS_FEAT_ATTR_PTR(_name) \
84 (&btrfs_attr_features_##_name.kobj_attr.attr)
85
86#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
87 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
88#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
89 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
90#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
91 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
92
Jeff Mahoney510d7362013-11-01 13:06:59 -040093static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080094static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040095
David Sterba8f523162019-08-02 13:07:38 +020096static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
97{
98 return container_of(a, struct btrfs_feature_attr, kobj_attr);
99}
100
101static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
102{
103 return container_of(attr, struct kobj_attribute, attr);
104}
105
106static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
107 struct attribute *attr)
108{
109 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
110}
111
Jeff Mahoney510d7362013-11-01 13:06:59 -0400112static u64 get_features(struct btrfs_fs_info *fs_info,
113 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400114{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400115 struct btrfs_super_block *disk_super = fs_info->super_copy;
116 if (set == FEAT_COMPAT)
117 return btrfs_super_compat_flags(disk_super);
118 else if (set == FEAT_COMPAT_RO)
119 return btrfs_super_compat_ro_flags(disk_super);
120 else
121 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400122}
123
Jeff Mahoneyba631942013-11-01 13:07:01 -0400124static void set_features(struct btrfs_fs_info *fs_info,
125 enum btrfs_feature_set set, u64 features)
126{
127 struct btrfs_super_block *disk_super = fs_info->super_copy;
128 if (set == FEAT_COMPAT)
129 btrfs_set_super_compat_flags(disk_super, features);
130 else if (set == FEAT_COMPAT_RO)
131 btrfs_set_super_compat_ro_flags(disk_super, features);
132 else
133 btrfs_set_super_incompat_flags(disk_super, features);
134}
135
136static int can_modify_feature(struct btrfs_feature_attr *fa)
137{
138 int val = 0;
139 u64 set, clear;
140 switch (fa->feature_set) {
141 case FEAT_COMPAT:
142 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
143 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
144 break;
145 case FEAT_COMPAT_RO:
146 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
147 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
148 break;
149 case FEAT_INCOMPAT:
150 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
151 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
152 break;
153 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -0400154 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +0100155 fa->feature_set);
156 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400157 }
158
159 if (set & fa->feature_bit)
160 val |= 1;
161 if (clear & fa->feature_bit)
162 val |= 2;
163
164 return val;
165}
166
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400167static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
168 struct kobj_attribute *a, char *buf)
169{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400170 int val = 0;
171 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400172 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400173 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400174 u64 features = get_features(fs_info, fa->feature_set);
175 if (features & fa->feature_bit)
176 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400177 } else
178 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400179
Anand Jain020e5272021-10-19 08:22:09 +0800180 return sysfs_emit(buf, "%d\n", val);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400181}
182
Jeff Mahoneyba631942013-11-01 13:07:01 -0400183static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
184 struct kobj_attribute *a,
185 const char *buf, size_t count)
186{
187 struct btrfs_fs_info *fs_info;
188 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400189 u64 features, set, clear;
190 unsigned long val;
191 int ret;
192
193 fs_info = to_fs_info(kobj);
194 if (!fs_info)
195 return -EPERM;
196
David Howellsbc98a422017-07-17 08:45:34 +0100197 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100198 return -EROFS;
199
Jeff Mahoneyba631942013-11-01 13:07:01 -0400200 ret = kstrtoul(skip_spaces(buf), 0, &val);
201 if (ret)
202 return ret;
203
204 if (fa->feature_set == FEAT_COMPAT) {
205 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
206 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
207 } else if (fa->feature_set == FEAT_COMPAT_RO) {
208 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
209 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
210 } else {
211 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
212 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
213 }
214
215 features = get_features(fs_info, fa->feature_set);
216
217 /* Nothing to do */
218 if ((val && (features & fa->feature_bit)) ||
219 (!val && !(features & fa->feature_bit)))
220 return count;
221
222 if ((val && !(set & fa->feature_bit)) ||
223 (!val && !(clear & fa->feature_bit))) {
224 btrfs_info(fs_info,
225 "%sabling feature %s on mounted fs is not supported.",
226 val ? "En" : "Dis", fa->kobj_attr.attr.name);
227 return -EPERM;
228 }
229
230 btrfs_info(fs_info, "%s %s feature flag",
231 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
232
Jeff Mahoneyba631942013-11-01 13:07:01 -0400233 spin_lock(&fs_info->super_lock);
234 features = get_features(fs_info, fa->feature_set);
235 if (val)
236 features |= fa->feature_bit;
237 else
238 features &= ~fa->feature_bit;
239 set_features(fs_info, fa->feature_set, features);
240 spin_unlock(&fs_info->super_lock);
241
David Sterba0eae2742014-11-12 14:22:21 +0100242 /*
243 * We don't want to do full transaction commit from inside sysfs
244 */
245 btrfs_set_pending(fs_info, COMMIT);
246 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400247
248 return count;
249}
250
Jeff Mahoney510d7362013-11-01 13:06:59 -0400251static umode_t btrfs_feature_visible(struct kobject *kobj,
252 struct attribute *attr, int unused)
253{
254 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
255 umode_t mode = attr->mode;
256
257 if (fs_info) {
258 struct btrfs_feature_attr *fa;
259 u64 features;
260
261 fa = attr_to_btrfs_feature_attr(attr);
262 features = get_features(fs_info, fa->feature_set);
263
Jeff Mahoneyba631942013-11-01 13:07:01 -0400264 if (can_modify_feature(fa))
265 mode |= S_IWUSR;
266 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400267 mode = 0;
268 }
269
270 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400271}
272
273BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
274BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
275BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
276BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700277BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400278BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
279BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
280BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
281BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100282BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200283BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100284BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
David Sterbacfbb8252018-07-10 18:15:05 +0200285BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
Naohiro Aota7b3d5a92020-11-10 20:26:06 +0900286/* Remove once support for zoned allocation is feature complete */
287#ifdef CONFIG_BTRFS_DEBUG
288BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
289#endif
Boris Burkov14605402021-06-30 13:01:49 -0700290#ifdef CONFIG_FS_VERITY
291BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
292#endif
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400293
Anand Jaine7849e32021-08-10 21:55:59 +0800294/*
295 * Features which depend on feature bits and may differ between each fs.
296 *
297 * /sys/fs/btrfs/features - all available features implemeted by this version
298 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
299 * can be changed on a mounted filesystem.
300 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400301static struct attribute *btrfs_supported_feature_attrs[] = {
302 BTRFS_FEAT_ATTR_PTR(mixed_backref),
303 BTRFS_FEAT_ATTR_PTR(default_subvol),
304 BTRFS_FEAT_ATTR_PTR(mixed_groups),
305 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700306 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400307 BTRFS_FEAT_ATTR_PTR(big_metadata),
308 BTRFS_FEAT_ATTR_PTR(extended_iref),
309 BTRFS_FEAT_ATTR_PTR(raid56),
310 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100311 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200312 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100313 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200314 BTRFS_FEAT_ATTR_PTR(raid1c34),
Naohiro Aota7b3d5a92020-11-10 20:26:06 +0900315#ifdef CONFIG_BTRFS_DEBUG
316 BTRFS_FEAT_ATTR_PTR(zoned),
317#endif
Boris Burkov14605402021-06-30 13:01:49 -0700318#ifdef CONFIG_FS_VERITY
319 BTRFS_FEAT_ATTR_PTR(verity),
320#endif
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400321 NULL
322};
323
324static const struct attribute_group btrfs_feature_attr_group = {
325 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400326 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400327 .attrs = btrfs_supported_feature_attrs,
328};
Josef Bacik58176a92007-08-29 15:47:34 -0400329
Misono Tomohirof902bd32018-05-17 14:24:51 +0900330static ssize_t rmdir_subvol_show(struct kobject *kobj,
331 struct kobj_attribute *ka, char *buf)
332{
Anand Jain020e5272021-10-19 08:22:09 +0800333 return sysfs_emit(buf, "0\n");
Misono Tomohirof902bd32018-05-17 14:24:51 +0900334}
335BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
336
David Sterbaf7cea562019-10-07 11:11:03 +0200337static ssize_t supported_checksums_show(struct kobject *kobj,
338 struct kobj_attribute *a, char *buf)
339{
340 ssize_t ret = 0;
341 int i;
342
343 for (i = 0; i < btrfs_get_num_csums(); i++) {
344 /*
345 * This "trick" only works as long as 'enum btrfs_csum_type' has
346 * no holes in it
347 */
Anand Jain020e5272021-10-19 08:22:09 +0800348 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
349 btrfs_super_csum_name(i));
David Sterbaf7cea562019-10-07 11:11:03 +0200350
351 }
352
Anand Jain020e5272021-10-19 08:22:09 +0800353 ret += sysfs_emit_at(buf, ret, "\n");
David Sterbaf7cea562019-10-07 11:11:03 +0200354 return ret;
355}
356BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
357
Omar Sandoval7573df52020-08-21 00:39:54 -0700358static ssize_t send_stream_version_show(struct kobject *kobj,
359 struct kobj_attribute *ka, char *buf)
360{
Anand Jain020e5272021-10-19 08:22:09 +0800361 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
Omar Sandoval7573df52020-08-21 00:39:54 -0700362}
363BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
364
Josef Bacikceafe3c2020-10-16 11:29:15 -0400365static const char *rescue_opts[] = {
366 "usebackuproot",
367 "nologreplay",
Josef Bacik42437a62020-10-16 11:29:18 -0400368 "ignorebadroots",
Josef Bacik882dbe02020-10-16 11:29:19 -0400369 "ignoredatacsums",
Josef Bacik9037d3c2020-10-16 11:29:20 -0400370 "all",
Josef Bacikceafe3c2020-10-16 11:29:15 -0400371};
372
373static ssize_t supported_rescue_options_show(struct kobject *kobj,
374 struct kobj_attribute *a,
375 char *buf)
376{
377 ssize_t ret = 0;
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
Anand Jain020e5272021-10-19 08:22:09 +0800381 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
382 ret += sysfs_emit_at(buf, ret, "\n");
Josef Bacikceafe3c2020-10-16 11:29:15 -0400383 return ret;
384}
385BTRFS_ATTR(static_feature, supported_rescue_options,
386 supported_rescue_options_show);
387
Qu Wenruofc57ad82021-03-25 15:14:33 +0800388static ssize_t supported_sectorsizes_show(struct kobject *kobj,
389 struct kobj_attribute *a,
390 char *buf)
391{
392 ssize_t ret = 0;
393
Qu Wenruo95ea0482021-07-26 14:35:06 +0800394 /* 4K sector size is also supported with 64K page size */
395 if (PAGE_SIZE == SZ_64K)
Anand Jain020e5272021-10-19 08:22:09 +0800396 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
Qu Wenruo95ea0482021-07-26 14:35:06 +0800397
Qu Wenruofc57ad82021-03-25 15:14:33 +0800398 /* Only sectorsize == PAGE_SIZE is now supported */
Anand Jain020e5272021-10-19 08:22:09 +0800399 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
Qu Wenruofc57ad82021-03-25 15:14:33 +0800400
401 return ret;
402}
403BTRFS_ATTR(static_feature, supported_sectorsizes,
404 supported_sectorsizes_show);
405
Anand Jaine7849e32021-08-10 21:55:59 +0800406/*
407 * Features which only depend on kernel version.
408 *
409 * These are listed in /sys/fs/btrfs/features along with
410 * btrfs_supported_feature_attrs.
411 */
Misono Tomohirof902bd32018-05-17 14:24:51 +0900412static struct attribute *btrfs_supported_static_feature_attrs[] = {
413 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200414 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Omar Sandoval7573df52020-08-21 00:39:54 -0700415 BTRFS_ATTR_PTR(static_feature, send_stream_version),
Josef Bacikceafe3c2020-10-16 11:29:15 -0400416 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
Qu Wenruofc57ad82021-03-25 15:14:33 +0800417 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900418 NULL
419};
420
Misono Tomohirof902bd32018-05-17 14:24:51 +0900421static const struct attribute_group btrfs_static_feature_attr_group = {
422 .name = "features",
423 .attrs = btrfs_supported_static_feature_attrs,
424};
425
David Sterba6e369fe2019-06-13 17:23:02 +0200426#ifdef CONFIG_BTRFS_DEBUG
427
428/*
Dennis Zhoue4faab82019-12-13 16:22:19 -0800429 * Discard statistics and tunables
430 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800431#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
432
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800433static ssize_t btrfs_discardable_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
Anand Jain020e5272021-10-19 08:22:09 +0800439 return sysfs_emit(buf, "%lld\n",
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800440 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
441}
442BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
443
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800444static ssize_t btrfs_discardable_extents_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
Anand Jain020e5272021-10-19 08:22:09 +0800450 return sysfs_emit(buf, "%d\n",
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800451 atomic_read(&fs_info->discard_ctl.discardable_extents));
452}
453BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
454
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500455static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
456 struct kobj_attribute *a,
457 char *buf)
458{
459 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
460
Anand Jain020e5272021-10-19 08:22:09 +0800461 return sysfs_emit(buf, "%llu\n",
462 fs_info->discard_ctl.discard_bitmap_bytes);
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500463}
464BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
465
466static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
467 struct kobj_attribute *a,
468 char *buf)
469{
470 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
471
Anand Jain020e5272021-10-19 08:22:09 +0800472 return sysfs_emit(buf, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500473 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
474}
475BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
476
477static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
478 struct kobj_attribute *a,
479 char *buf)
480{
481 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
482
Anand Jain020e5272021-10-19 08:22:09 +0800483 return sysfs_emit(buf, "%llu\n",
484 fs_info->discard_ctl.discard_extent_bytes);
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500485}
486BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
487
Dennis Zhoua2309302020-01-02 16:26:35 -0500488static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
489 struct kobj_attribute *a,
490 char *buf)
491{
492 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
493
Anand Jain020e5272021-10-19 08:22:09 +0800494 return sysfs_emit(buf, "%u\n",
495 READ_ONCE(fs_info->discard_ctl.iops_limit));
Dennis Zhoua2309302020-01-02 16:26:35 -0500496}
497
498static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
499 struct kobj_attribute *a,
500 const char *buf, size_t len)
501{
502 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
503 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
504 u32 iops_limit;
505 int ret;
506
507 ret = kstrtou32(buf, 10, &iops_limit);
508 if (ret)
509 return -EINVAL;
510
511 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
Pavel Begunkov3e48d8d2020-11-04 09:45:54 +0000512 btrfs_discard_calc_delay(discard_ctl);
513 btrfs_discard_schedule_work(discard_ctl, true);
Dennis Zhoua2309302020-01-02 16:26:35 -0500514 return len;
515}
516BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
517 btrfs_discard_iops_limit_store);
518
Dennis Zhoue93591b2020-01-02 16:26:36 -0500519static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
520 struct kobj_attribute *a,
521 char *buf)
522{
523 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
524
Anand Jain020e5272021-10-19 08:22:09 +0800525 return sysfs_emit(buf, "%u\n",
526 READ_ONCE(fs_info->discard_ctl.kbps_limit));
Dennis Zhoue93591b2020-01-02 16:26:36 -0500527}
528
529static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
530 struct kobj_attribute *a,
531 const char *buf, size_t len)
532{
533 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
534 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
535 u32 kbps_limit;
536 int ret;
537
538 ret = kstrtou32(buf, 10, &kbps_limit);
539 if (ret)
540 return -EINVAL;
541
542 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
Pavel Begunkov3e48d8d2020-11-04 09:45:54 +0000543 btrfs_discard_schedule_work(discard_ctl, true);
Dennis Zhoue93591b2020-01-02 16:26:36 -0500544 return len;
545}
546BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
547 btrfs_discard_kbps_limit_store);
548
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500549static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
550 struct kobj_attribute *a,
551 char *buf)
552{
553 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
554
Anand Jain020e5272021-10-19 08:22:09 +0800555 return sysfs_emit(buf, "%llu\n",
556 READ_ONCE(fs_info->discard_ctl.max_discard_size));
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500557}
558
559static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
560 struct kobj_attribute *a,
561 const char *buf, size_t len)
562{
563 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
564 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
565 u64 max_discard_size;
566 int ret;
567
568 ret = kstrtou64(buf, 10, &max_discard_size);
569 if (ret)
570 return -EINVAL;
571
572 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
573
574 return len;
575}
576BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
577 btrfs_discard_max_discard_size_store);
578
Anand Jaine7849e32021-08-10 21:55:59 +0800579/*
580 * Per-filesystem debugging of discard (when mounted with discard=async).
581 *
582 * Path: /sys/fs/btrfs/<uuid>/debug/discard/
583 */
Dennis Zhoue4faab82019-12-13 16:22:19 -0800584static const struct attribute *discard_debug_attrs[] = {
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800585 BTRFS_ATTR_PTR(discard, discardable_bytes),
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800586 BTRFS_ATTR_PTR(discard, discardable_extents),
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500587 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
588 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
589 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
Dennis Zhoua2309302020-01-02 16:26:35 -0500590 BTRFS_ATTR_PTR(discard, iops_limit),
Dennis Zhoue93591b2020-01-02 16:26:36 -0500591 BTRFS_ATTR_PTR(discard, kbps_limit),
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500592 BTRFS_ATTR_PTR(discard, max_discard_size),
Dennis Zhoue4faab82019-12-13 16:22:19 -0800593 NULL,
594};
595
596/*
Anand Jaine7849e32021-08-10 21:55:59 +0800597 * Per-filesystem runtime debugging exported via sysfs.
David Sterba6e369fe2019-06-13 17:23:02 +0200598 *
Anand Jaine7849e32021-08-10 21:55:59 +0800599 * Path: /sys/fs/btrfs/UUID/debug/
David Sterba6e369fe2019-06-13 17:23:02 +0200600 */
Dennis Zhou93945cb2019-12-13 16:22:18 -0800601static const struct attribute *btrfs_debug_mount_attrs[] = {
602 NULL,
603};
604
Anand Jaine7849e32021-08-10 21:55:59 +0800605/*
606 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
607 *
608 * Path: /sys/fs/btrfs/debug
609 */
David Sterba6e369fe2019-06-13 17:23:02 +0200610static struct attribute *btrfs_debug_feature_attrs[] = {
611 NULL
612};
613
614static const struct attribute_group btrfs_debug_feature_attr_group = {
615 .name = "debug",
616 .attrs = btrfs_debug_feature_attrs,
617};
618
619#endif
620
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400621static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
622{
623 u64 val;
624 if (lock)
625 spin_lock(lock);
626 val = *value_ptr;
627 if (lock)
628 spin_unlock(lock);
Anand Jain020e5272021-10-19 08:22:09 +0800629 return sysfs_emit(buf, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400630}
631
632static ssize_t global_rsv_size_show(struct kobject *kobj,
633 struct kobj_attribute *ka, char *buf)
634{
635 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
636 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
637 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
638}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200639BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400640
641static ssize_t global_rsv_reserved_show(struct kobject *kobj,
642 struct kobj_attribute *a, char *buf)
643{
644 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
645 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
646 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
647}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200648BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400649
650#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400651#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400652
653static ssize_t raid_bytes_show(struct kobject *kobj,
654 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200655BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
656BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400657
658static ssize_t raid_bytes_show(struct kobject *kobj,
659 struct kobj_attribute *attr, char *buf)
660
661{
662 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100663 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400664 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400665 u64 val = 0;
666
667 down_read(&sinfo->groups_sem);
668 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200669 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200670 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400671 else
David Sterbabf38be62019-10-23 18:48:11 +0200672 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400673 }
674 up_read(&sinfo->groups_sem);
Anand Jain020e5272021-10-19 08:22:09 +0800675 return sysfs_emit(buf, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400676}
677
Anand Jaine7849e32021-08-10 21:55:59 +0800678/*
679 * Allocation information about block group profiles.
680 *
681 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
682 */
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400683static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200684 BTRFS_ATTR_PTR(raid, total_bytes),
685 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400686 NULL
687};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400688ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400689
690static void release_raid_kobj(struct kobject *kobj)
691{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400692 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400693}
694
David Sterba536ea452019-08-01 17:55:55 +0200695static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400696 .sysfs_ops = &kobj_sysfs_ops,
697 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400698 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400699};
700
701#define SPACE_INFO_ATTR(field) \
702static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
703 struct kobj_attribute *a, \
704 char *buf) \
705{ \
706 struct btrfs_space_info *sinfo = to_space_info(kobj); \
707 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
708} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200709BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400710
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400711SPACE_INFO_ATTR(flags);
712SPACE_INFO_ATTR(total_bytes);
713SPACE_INFO_ATTR(bytes_used);
714SPACE_INFO_ATTR(bytes_pinned);
715SPACE_INFO_ATTR(bytes_reserved);
716SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800717SPACE_INFO_ATTR(bytes_readonly);
Naohiro Aota169e0da2021-02-04 19:21:52 +0900718SPACE_INFO_ATTR(bytes_zone_unusable);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400719SPACE_INFO_ATTR(disk_used);
720SPACE_INFO_ATTR(disk_total);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400721
Anand Jaine7849e32021-08-10 21:55:59 +0800722/*
723 * Allocation information about block group types.
724 *
725 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
726 */
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400727static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200728 BTRFS_ATTR_PTR(space_info, flags),
729 BTRFS_ATTR_PTR(space_info, total_bytes),
730 BTRFS_ATTR_PTR(space_info, bytes_used),
731 BTRFS_ATTR_PTR(space_info, bytes_pinned),
732 BTRFS_ATTR_PTR(space_info, bytes_reserved),
733 BTRFS_ATTR_PTR(space_info, bytes_may_use),
734 BTRFS_ATTR_PTR(space_info, bytes_readonly),
Naohiro Aota169e0da2021-02-04 19:21:52 +0900735 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200736 BTRFS_ATTR_PTR(space_info, disk_used),
737 BTRFS_ATTR_PTR(space_info, disk_total),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400738 NULL,
739};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400740ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400741
742static void space_info_release(struct kobject *kobj)
743{
744 struct btrfs_space_info *sinfo = to_space_info(kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400745 kfree(sinfo);
746}
747
David Sterba27992d02019-08-01 17:55:55 +0200748static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400749 .sysfs_ops = &kobj_sysfs_ops,
750 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400751 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400752};
753
Anand Jaine7849e32021-08-10 21:55:59 +0800754/*
755 * Allocation information about block groups.
756 *
757 * Path: /sys/fs/btrfs/<uuid>/allocation/
758 */
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400759static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200760 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
761 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400762 NULL,
763};
764
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400765static ssize_t btrfs_label_show(struct kobject *kobj,
766 struct kobj_attribute *a, char *buf)
767{
768 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900769 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200770 ssize_t ret;
771
772 spin_lock(&fs_info->super_lock);
Anand Jain020e5272021-10-19 08:22:09 +0800773 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
David Sterbaee17fc82016-04-26 16:22:06 +0200774 spin_unlock(&fs_info->super_lock);
775
776 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400777}
778
779static ssize_t btrfs_label_store(struct kobject *kobj,
780 struct kobj_attribute *a,
781 const char *buf, size_t len)
782{
783 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900784 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400785
David Sterba66ac9fe2016-04-26 16:03:57 +0200786 if (!fs_info)
787 return -EPERM;
788
David Howellsbc98a422017-07-17 08:45:34 +0100789 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800790 return -EROFS;
791
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900792 /*
793 * p_len is the len until the first occurrence of either
794 * '\n' or '\0'
795 */
796 p_len = strcspn(buf, "\n");
797
798 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400799 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400800
David Sterbaa6f69dc2014-05-30 19:29:05 +0200801 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900802 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
803 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200804 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400805
David Sterbaa6f69dc2014-05-30 19:29:05 +0200806 /*
807 * We don't want to do full transaction commit from inside sysfs
808 */
809 btrfs_set_pending(fs_info, COMMIT);
810 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400811
David Sterbaa6f69dc2014-05-30 19:29:05 +0200812 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400813}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200814BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400815
David Sterbadf935892014-05-07 18:17:16 +0200816static ssize_t btrfs_nodesize_show(struct kobject *kobj,
817 struct kobj_attribute *a, char *buf)
818{
819 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
820
Anand Jain020e5272021-10-19 08:22:09 +0800821 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200822}
823
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200824BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200825
826static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
827 struct kobj_attribute *a, char *buf)
828{
829 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
830
Anand Jain020e5272021-10-19 08:22:09 +0800831 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200832}
833
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200834BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200835
836static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
837 struct kobj_attribute *a, char *buf)
838{
839 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
840
Anand Jain020e5272021-10-19 08:22:09 +0800841 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200842}
843
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200844BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200845
Sargun Dhillon27234802017-05-11 21:18:03 +0000846static ssize_t quota_override_show(struct kobject *kobj,
847 struct kobj_attribute *a, char *buf)
848{
849 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
850 int quota_override;
851
852 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
Anand Jain020e5272021-10-19 08:22:09 +0800853 return sysfs_emit(buf, "%d\n", quota_override);
Sargun Dhillon27234802017-05-11 21:18:03 +0000854}
855
856static ssize_t quota_override_store(struct kobject *kobj,
857 struct kobj_attribute *a,
858 const char *buf, size_t len)
859{
860 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
861 unsigned long knob;
862 int err;
863
864 if (!fs_info)
865 return -EPERM;
866
867 if (!capable(CAP_SYS_RESOURCE))
868 return -EPERM;
869
870 err = kstrtoul(buf, 10, &knob);
871 if (err)
872 return err;
873 if (knob > 1)
874 return -EINVAL;
875
876 if (knob)
877 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
878 else
879 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
880
881 return len;
882}
883
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200884BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000885
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200886static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
887 struct kobj_attribute *a, char *buf)
888{
889 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
890
Anand Jain020e5272021-10-19 08:22:09 +0800891 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200892}
893
894BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
895
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200896static ssize_t btrfs_checksum_show(struct kobject *kobj,
897 struct kobj_attribute *a, char *buf)
898{
899 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
900 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
901
Anand Jain020e5272021-10-19 08:22:09 +0800902 return sysfs_emit(buf, "%s (%s)\n",
903 btrfs_super_csum_name(csum_type),
904 crypto_shash_driver_name(fs_info->csum_shash));
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200905}
906
907BTRFS_ATTR(, checksum, btrfs_checksum_show);
908
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500909static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
910 struct kobj_attribute *a, char *buf)
911{
912 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
913 const char *str;
914
915 switch (READ_ONCE(fs_info->exclusive_operation)) {
916 case BTRFS_EXCLOP_NONE:
917 str = "none\n";
918 break;
919 case BTRFS_EXCLOP_BALANCE:
920 str = "balance\n";
921 break;
922 case BTRFS_EXCLOP_DEV_ADD:
923 str = "device add\n";
924 break;
925 case BTRFS_EXCLOP_DEV_REMOVE:
926 str = "device remove\n";
927 break;
928 case BTRFS_EXCLOP_DEV_REPLACE:
929 str = "device replace\n";
930 break;
931 case BTRFS_EXCLOP_RESIZE:
932 str = "resize\n";
933 break;
934 case BTRFS_EXCLOP_SWAP_ACTIVATE:
935 str = "swap activate\n";
936 break;
937 default:
938 str = "UNKNOWN\n";
939 break;
940 }
Anand Jain020e5272021-10-19 08:22:09 +0800941 return sysfs_emit(buf, "%s", str);
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -0500942}
943BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
944
Anand Jain089c8b02020-10-07 15:20:03 +0800945static ssize_t btrfs_generation_show(struct kobject *kobj,
946 struct kobj_attribute *a, char *buf)
947{
948 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
949
Anand Jain020e5272021-10-19 08:22:09 +0800950 return sysfs_emit(buf, "%llu\n", fs_info->generation);
Anand Jain089c8b02020-10-07 15:20:03 +0800951}
952BTRFS_ATTR(, generation, btrfs_generation_show);
953
Anand Jainaaefed22020-10-28 21:14:45 +0800954/*
955 * Look for an exact string @string in @buffer with possible leading or
956 * trailing whitespace
957 */
958static bool strmatch(const char *buffer, const char *string)
959{
960 const size_t len = strlen(string);
961
962 /* Skip leading whitespace */
963 buffer = skip_spaces(buffer);
964
965 /* Match entire string, check if the rest is whitespace or empty */
966 if (strncmp(string, buffer, len) == 0 &&
967 strlen(skip_spaces(buffer + len)) == 0)
968 return true;
969
970 return false;
971}
972
Anand Jain3d8cc172020-10-28 21:14:47 +0800973static const char * const btrfs_read_policy_name[] = { "pid" };
974
975static ssize_t btrfs_read_policy_show(struct kobject *kobj,
976 struct kobj_attribute *a, char *buf)
977{
978 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
979 ssize_t ret = 0;
980 int i;
981
982 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
983 if (fs_devices->read_policy == i)
984 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
985 (ret == 0 ? "" : " "),
986 btrfs_read_policy_name[i]);
987 else
988 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
989 (ret == 0 ? "" : " "),
990 btrfs_read_policy_name[i]);
991 }
992
993 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
994
995 return ret;
996}
997
998static ssize_t btrfs_read_policy_store(struct kobject *kobj,
999 struct kobj_attribute *a,
1000 const char *buf, size_t len)
1001{
1002 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1003 int i;
1004
1005 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1006 if (strmatch(buf, btrfs_read_policy_name[i])) {
1007 if (i != fs_devices->read_policy) {
1008 fs_devices->read_policy = i;
1009 btrfs_info(fs_devices->fs_info,
1010 "read policy set to '%s'",
1011 btrfs_read_policy_name[i]);
1012 }
1013 return len;
1014 }
1015 }
1016
1017 return -EINVAL;
1018}
1019BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1020
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001021static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1022 struct kobj_attribute *a,
1023 char *buf)
1024{
1025 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1026 ssize_t ret;
1027
Anand Jain020e5272021-10-19 08:22:09 +08001028 ret = sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001029
1030 return ret;
1031}
1032
1033static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1034 struct kobj_attribute *a,
1035 const char *buf, size_t len)
1036{
1037 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1038 int thresh;
1039 int ret;
1040
1041 ret = kstrtoint(buf, 10, &thresh);
1042 if (ret)
1043 return ret;
1044
Johannes Thumshirn77233c22021-08-09 20:41:17 +09001045 if (thresh != 0 && (thresh <= 50 || thresh > 100))
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001046 return -EINVAL;
1047
Johannes Thumshirn77233c22021-08-09 20:41:17 +09001048 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001049
1050 return len;
1051}
1052BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1053 btrfs_bg_reclaim_threshold_store);
1054
Anand Jaine7849e32021-08-10 21:55:59 +08001055/*
1056 * Per-filesystem information and stats.
1057 *
1058 * Path: /sys/fs/btrfs/<uuid>/
1059 */
Anand Jain0dd29062015-03-10 06:38:27 +08001060static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +02001061 BTRFS_ATTR_PTR(, label),
1062 BTRFS_ATTR_PTR(, nodesize),
1063 BTRFS_ATTR_PTR(, sectorsize),
1064 BTRFS_ATTR_PTR(, clone_alignment),
1065 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +02001066 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +02001067 BTRFS_ATTR_PTR(, checksum),
Goldwyn Rodrigues66a28232020-08-25 10:02:33 -05001068 BTRFS_ATTR_PTR(, exclusive_operation),
Anand Jain089c8b02020-10-07 15:20:03 +08001069 BTRFS_ATTR_PTR(, generation),
Anand Jain3d8cc172020-10-28 21:14:47 +08001070 BTRFS_ATTR_PTR(, read_policy),
Johannes Thumshirn18bb8bb2021-04-19 16:41:02 +09001071 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -04001072 NULL,
1073};
1074
Anand Jainc1b7e472015-08-14 18:32:50 +08001075static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -04001076{
Anand Jain2e7910d2015-03-10 06:38:29 +08001077 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +08001078
Anand Jainc1b7e472015-08-14 18:32:50 +08001079 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +08001080 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -04001081}
1082
1083static struct kobj_type btrfs_ktype = {
1084 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +08001085 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -04001086};
1087
Anand Jain2e7910d2015-03-10 06:38:29 +08001088static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1089{
1090 if (kobj->ktype != &btrfs_ktype)
1091 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +08001092 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +08001093}
1094
Jeff Mahoney510d7362013-11-01 13:06:59 -04001095static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1096{
1097 if (kobj->ktype != &btrfs_ktype)
1098 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +08001099 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001100}
Josef Bacik58176a92007-08-29 15:47:34 -04001101
Jeff Mahoneye453d982013-11-21 10:37:16 -05001102#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +09001103#define BTRFS_FEATURE_NAME_MAX 13
1104static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1105static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -05001106
Tomohiro Misono6c521572018-05-16 17:09:26 +09001107static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -05001108 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1109 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1110 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1111};
1112
1113static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001114{
Jeff Mahoneye453d982013-11-21 10:37:16 -05001115 int set;
1116
1117 for (set = 0; set < FEAT_MAX; set++) {
1118 int i;
1119 struct attribute *attrs[2];
1120 struct attribute_group agroup = {
1121 .name = "features",
1122 .attrs = attrs,
1123 };
1124 u64 features = get_features(fs_info, set);
1125 features &= ~supported_feature_masks[set];
1126
1127 if (!features)
1128 continue;
1129
1130 attrs[1] = NULL;
1131 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1132 struct btrfs_feature_attr *fa;
1133
1134 if (!(features & (1ULL << i)))
1135 continue;
1136
1137 fa = &btrfs_feature_attrs[set][i];
1138 attrs[0] = &fa->kobj_attr.attr;
1139 if (add) {
1140 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +08001141 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -05001142 &agroup);
1143 if (ret)
1144 return ret;
1145 } else
Anand Jainc1b7e472015-08-14 18:32:50 +08001146 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -05001147 &agroup);
1148 }
1149
1150 }
1151 return 0;
1152}
1153
Anand Jain2e3e1282015-03-10 06:38:32 +08001154static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -05001155{
Anand Jaina013d142020-02-12 17:28:10 +08001156 if (fs_devs->devinfo_kobj) {
1157 kobject_del(fs_devs->devinfo_kobj);
1158 kobject_put(fs_devs->devinfo_kobj);
1159 fs_devs->devinfo_kobj = NULL;
1160 }
1161
Anand Jainb5501502019-11-21 17:33:30 +08001162 if (fs_devs->devices_kobj) {
1163 kobject_del(fs_devs->devices_kobj);
1164 kobject_put(fs_devs->devices_kobj);
1165 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +08001166 }
1167
Anand Jainc1b7e472015-08-14 18:32:50 +08001168 if (fs_devs->fsid_kobj.state_initialized) {
1169 kobject_del(&fs_devs->fsid_kobj);
1170 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +08001171 wait_for_completion(&fs_devs->kobj_unregister);
1172 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001173}
1174
Anand Jain2e3e1282015-03-10 06:38:32 +08001175/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +08001176void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +08001177{
1178 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1179
1180 if (fs_devs) {
1181 __btrfs_sysfs_remove_fsid(fs_devs);
1182 return;
1183 }
1184
Anand Jainc4babc52018-04-12 10:29:25 +08001185 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +08001186 __btrfs_sysfs_remove_fsid(fs_devs);
1187 }
1188}
1189
Anand Jain53f8a742020-09-05 01:34:27 +08001190static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1191{
1192 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001193 struct btrfs_fs_devices *seed;
Anand Jain53f8a742020-09-05 01:34:27 +08001194
1195 list_for_each_entry(device, &fs_devices->devices, dev_list)
1196 btrfs_sysfs_remove_device(device);
Anand Jain30b0e4e2020-09-05 01:34:28 +08001197
1198 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1199 list_for_each_entry(device, &seed->devices, dev_list)
1200 btrfs_sysfs_remove_device(device);
1201 }
Anand Jain53f8a742020-09-05 01:34:27 +08001202}
1203
Anand Jain6618a592015-08-14 18:32:47 +08001204void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -05001205{
Nikolay Borisov3092c682020-07-03 11:13:15 +03001206 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1207
Nikolay Borisov3092c682020-07-03 11:13:15 +03001208 sysfs_remove_link(fsid_kobj, "bdi");
1209
Jeff Mahoneye453d982013-11-21 10:37:16 -05001210 if (fs_info->space_info_kobj) {
1211 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1212 kobject_del(fs_info->space_info_kobj);
1213 kobject_put(fs_info->space_info_kobj);
1214 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001215#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhoue4faab82019-12-13 16:22:19 -08001216 if (fs_info->discard_debug_kobj) {
1217 sysfs_remove_files(fs_info->discard_debug_kobj,
1218 discard_debug_attrs);
1219 kobject_del(fs_info->discard_debug_kobj);
1220 kobject_put(fs_info->discard_debug_kobj);
1221 }
Dennis Zhou93945cb2019-12-13 16:22:18 -08001222 if (fs_info->debug_kobj) {
1223 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1224 kobject_del(fs_info->debug_kobj);
1225 kobject_put(fs_info->debug_kobj);
1226 }
Dennis Zhou71e89782019-12-13 16:22:17 -08001227#endif
Jeff Mahoneye453d982013-11-21 10:37:16 -05001228 addrm_unknown_feature_attrs(fs_info, false);
Nikolay Borisov3092c682020-07-03 11:13:15 +03001229 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1230 sysfs_remove_files(fsid_kobj, btrfs_attrs);
Anand Jain53f8a742020-09-05 01:34:27 +08001231 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001232}
1233
David Sterbaf10152b2019-08-01 19:07:55 +02001234static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001235 [FEAT_COMPAT] = "compat",
1236 [FEAT_COMPAT_RO] = "compat_ro",
1237 [FEAT_INCOMPAT] = "incompat",
1238};
1239
David Sterba9e6df7c2020-08-17 10:56:00 +02001240const char *btrfs_feature_set_name(enum btrfs_feature_set set)
David Sterbaf10152b2019-08-01 19:07:55 +02001241{
1242 return btrfs_feature_set_names[set];
1243}
1244
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001245char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1246{
1247 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1248 int len = 0;
1249 int i;
1250 char *str;
1251
1252 str = kmalloc(bufsize, GFP_KERNEL);
1253 if (!str)
1254 return str;
1255
1256 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1257 const char *name;
1258
1259 if (!(flags & (1ULL << i)))
1260 continue;
1261
1262 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001263 len += scnprintf(str + len, bufsize - len, "%s%s",
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001264 len ? "," : "", name);
1265 }
1266
1267 return str;
1268}
1269
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001270static void init_feature_attrs(void)
1271{
1272 struct btrfs_feature_attr *fa;
1273 int set, i;
1274
1275 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1276 ARRAY_SIZE(btrfs_feature_attrs));
1277 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1278 ARRAY_SIZE(btrfs_feature_attrs[0]));
1279
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001280 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1281 memset(btrfs_unknown_feature_names, 0,
1282 sizeof(btrfs_unknown_feature_names));
1283
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001284 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1285 struct btrfs_feature_attr *sfa;
1286 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001287 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001288 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001289 bit = ilog2(sfa->feature_bit);
1290 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001291
1292 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1293 }
1294
1295 for (set = 0; set < FEAT_MAX; set++) {
1296 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1297 char *name = btrfs_unknown_feature_names[set][i];
1298 fa = &btrfs_feature_attrs[set][i];
1299
1300 if (fa->kobj_attr.attr.name)
1301 continue;
1302
Tomohiro Misono6c521572018-05-16 17:09:26 +09001303 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001304 btrfs_feature_set_names[set], i);
1305
1306 fa->kobj_attr.attr.name = name;
1307 fa->kobj_attr.attr.mode = S_IRUGO;
1308 fa->feature_set = set;
1309 fa->feature_bit = 1ULL << i;
1310 }
1311 }
1312}
1313
David Sterba32a99912019-08-01 17:49:55 +02001314/*
1315 * Create a sysfs entry for a given block group type at path
1316 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1317 */
David Sterba32da53862019-10-29 19:20:18 +01001318void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +02001319{
1320 struct btrfs_fs_info *fs_info = cache->fs_info;
1321 struct btrfs_space_info *space_info = cache->space_info;
1322 struct raid_kobject *rkobj;
1323 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1324 unsigned int nofs_flag;
1325 int ret;
1326
1327 /*
1328 * Setup a NOFS context because kobject_add(), deep in its call chain,
1329 * does GFP_KERNEL allocations, and we are often called in a context
1330 * where if reclaim is triggered we can deadlock (we are either holding
1331 * a transaction handle or some lock required for a transaction
1332 * commit).
1333 */
1334 nofs_flag = memalloc_nofs_save();
1335
1336 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1337 if (!rkobj) {
1338 memalloc_nofs_restore(nofs_flag);
1339 btrfs_warn(cache->fs_info,
1340 "couldn't alloc memory for raid level kobject");
1341 return;
1342 }
1343
1344 rkobj->flags = cache->flags;
1345 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
Josef Bacik49ea1122020-09-01 17:40:38 -04001346
1347 /*
1348 * We call this either on mount, or if we've created a block group for a
1349 * new index type while running (i.e. when restriping). The running
1350 * case is tricky because we could race with other threads, so we need
1351 * to have this check to make sure we didn't already init the kobject.
1352 *
1353 * We don't have to protect on the free side because it only happens on
1354 * unmount.
1355 */
1356 spin_lock(&space_info->lock);
1357 if (space_info->block_group_kobjs[index]) {
1358 spin_unlock(&space_info->lock);
1359 kobject_put(&rkobj->kobj);
1360 return;
1361 } else {
1362 space_info->block_group_kobjs[index] = &rkobj->kobj;
1363 }
1364 spin_unlock(&space_info->lock);
1365
David Sterba32a99912019-08-01 17:49:55 +02001366 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1367 btrfs_bg_type_to_raid_name(rkobj->flags));
1368 memalloc_nofs_restore(nofs_flag);
1369 if (ret) {
Josef Bacik49ea1122020-09-01 17:40:38 -04001370 spin_lock(&space_info->lock);
1371 space_info->block_group_kobjs[index] = NULL;
1372 spin_unlock(&space_info->lock);
David Sterba32a99912019-08-01 17:49:55 +02001373 kobject_put(&rkobj->kobj);
1374 btrfs_warn(fs_info,
1375 "failed to add kobject for block cache, ignoring");
1376 return;
1377 }
David Sterba32a99912019-08-01 17:49:55 +02001378}
1379
David Sterbab5865ba2019-08-01 18:50:16 +02001380/*
1381 * Remove sysfs directories for all block group types of a given space info and
1382 * the space info as well
1383 */
1384void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1385{
1386 int i;
1387
1388 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1389 struct kobject *kobj;
1390
1391 kobj = space_info->block_group_kobjs[i];
1392 space_info->block_group_kobjs[i] = NULL;
1393 if (kobj) {
1394 kobject_del(kobj);
1395 kobject_put(kobj);
1396 }
1397 }
1398 kobject_del(&space_info->kobj);
1399 kobject_put(&space_info->kobj);
1400}
1401
David Sterbab8823272019-08-01 18:50:16 +02001402static const char *alloc_name(u64 flags)
1403{
1404 switch (flags) {
1405 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1406 return "mixed";
1407 case BTRFS_BLOCK_GROUP_METADATA:
1408 return "metadata";
1409 case BTRFS_BLOCK_GROUP_DATA:
1410 return "data";
1411 case BTRFS_BLOCK_GROUP_SYSTEM:
1412 return "system";
1413 default:
1414 WARN_ON(1);
1415 return "invalid-combination";
Tom Rix445d8ab2020-11-01 07:30:08 -08001416 }
David Sterbab8823272019-08-01 18:50:16 +02001417}
1418
1419/*
1420 * Create a sysfs entry for a space info type at path
1421 * /sys/fs/btrfs/UUID/allocation/TYPE
1422 */
1423int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1424 struct btrfs_space_info *space_info)
1425{
1426 int ret;
1427
1428 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1429 fs_info->space_info_kobj, "%s",
1430 alloc_name(space_info->flags));
1431 if (ret) {
1432 kobject_put(&space_info->kobj);
1433 return ret;
1434 }
1435
1436 return 0;
1437}
1438
Anand Jain53f8a742020-09-05 01:34:27 +08001439void btrfs_sysfs_remove_device(struct btrfs_device *device)
Anand Jain99994cd2014-06-03 11:36:00 +08001440{
Anand Jain985e2332020-09-05 01:34:24 +08001441 struct kobject *devices_kobj;
Anand Jain99994cd2014-06-03 11:36:00 +08001442
Anand Jain985e2332020-09-05 01:34:24 +08001443 /*
1444 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1445 * fs_info::fs_devices.
1446 */
1447 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1448 ASSERT(devices_kobj);
Anand Jain99994cd2014-06-03 11:36:00 +08001449
Christoph Hellwig8d652692020-11-17 08:18:55 +01001450 if (device->bdev)
1451 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
Anand Jain99994cd2014-06-03 11:36:00 +08001452
Anand Jain985e2332020-09-05 01:34:24 +08001453 if (device->devid_kobj.state_initialized) {
1454 kobject_del(&device->devid_kobj);
1455 kobject_put(&device->devid_kobj);
1456 wait_for_completion(&device->kobj_unregister);
1457 }
1458}
Anand Jain668e48af2020-01-06 19:38:31 +08001459
Anand Jain668e48af2020-01-06 19:38:31 +08001460static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1461 struct kobj_attribute *a,
1462 char *buf)
1463{
1464 int val;
1465 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1466 devid_kobj);
1467
1468 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1469
Anand Jain020e5272021-10-19 08:22:09 +08001470 return sysfs_emit(buf, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001471}
1472BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1473
Anand Jain25864772020-02-13 16:40:53 +08001474static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
Anand Jain668e48af2020-01-06 19:38:31 +08001475 struct kobj_attribute *a, char *buf)
1476{
1477 int val;
1478 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1479 devid_kobj);
1480
1481 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1482
Anand Jain020e5272021-10-19 08:22:09 +08001483 return sysfs_emit(buf, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001484}
Anand Jain25864772020-02-13 16:40:53 +08001485BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
Anand Jain668e48af2020-01-06 19:38:31 +08001486
1487static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1488 struct kobj_attribute *a,
1489 char *buf)
1490{
1491 int val;
1492 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1493 devid_kobj);
1494
1495 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1496
Anand Jain020e5272021-10-19 08:22:09 +08001497 return sysfs_emit(buf, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001498}
1499BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1500
David Sterbaeb3b5052019-10-09 13:58:13 +02001501static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1502 struct kobj_attribute *a,
1503 char *buf)
1504{
1505 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1506 devid_kobj);
1507
Anand Jain020e5272021-10-19 08:22:09 +08001508 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
David Sterbaeb3b5052019-10-09 13:58:13 +02001509}
1510
1511static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1512 struct kobj_attribute *a,
1513 const char *buf, size_t len)
1514{
1515 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1516 devid_kobj);
1517 char *endptr;
1518 unsigned long long limit;
1519
1520 limit = memparse(buf, &endptr);
1521 WRITE_ONCE(device->scrub_speed_max, limit);
1522 return len;
1523}
1524BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1525 btrfs_devinfo_scrub_speed_max_store);
1526
Anand Jain668e48af2020-01-06 19:38:31 +08001527static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1528 struct kobj_attribute *a, char *buf)
1529{
1530 int val;
1531 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1532 devid_kobj);
1533
1534 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1535
Anand Jain020e5272021-10-19 08:22:09 +08001536 return sysfs_emit(buf, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001537}
1538BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1539
Anand Jaina26d60d2021-10-21 23:31:17 +08001540static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1541 struct kobj_attribute *a, char *buf)
1542{
1543 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1544 devid_kobj);
1545
1546 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1547}
1548BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1549
David Sterbada658b52021-06-04 15:00:05 +02001550static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1551 struct kobj_attribute *a, char *buf)
1552{
1553 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1554 devid_kobj);
1555
1556 if (!device->dev_stats_valid)
Anand Jain020e5272021-10-19 08:22:09 +08001557 return sysfs_emit(buf, "invalid\n");
David Sterbada658b52021-06-04 15:00:05 +02001558
1559 /*
1560 * Print all at once so we get a snapshot of all values from the same
1561 * time. Keep them in sync and in order of definition of
1562 * btrfs_dev_stat_values.
1563 */
Anand Jain020e5272021-10-19 08:22:09 +08001564 return sysfs_emit(buf,
David Sterbada658b52021-06-04 15:00:05 +02001565 "write_errs %d\n"
1566 "read_errs %d\n"
1567 "flush_errs %d\n"
1568 "corruption_errs %d\n"
1569 "generation_errs %d\n",
1570 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1571 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1572 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1573 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1574 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1575}
1576BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1577
Anand Jaine7849e32021-08-10 21:55:59 +08001578/*
1579 * Information about one device.
1580 *
1581 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1582 */
Anand Jain668e48af2020-01-06 19:38:31 +08001583static struct attribute *devid_attrs[] = {
David Sterbada658b52021-06-04 15:00:05 +02001584 BTRFS_ATTR_PTR(devid, error_stats),
Anand Jaina26d60d2021-10-21 23:31:17 +08001585 BTRFS_ATTR_PTR(devid, fsid),
Anand Jain668e48af2020-01-06 19:38:31 +08001586 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1587 BTRFS_ATTR_PTR(devid, missing),
1588 BTRFS_ATTR_PTR(devid, replace_target),
David Sterbaeb3b5052019-10-09 13:58:13 +02001589 BTRFS_ATTR_PTR(devid, scrub_speed_max),
Anand Jain668e48af2020-01-06 19:38:31 +08001590 BTRFS_ATTR_PTR(devid, writeable),
1591 NULL
1592};
1593ATTRIBUTE_GROUPS(devid);
1594
1595static void btrfs_release_devid_kobj(struct kobject *kobj)
1596{
1597 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1598 devid_kobj);
1599
1600 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1601 complete(&device->kobj_unregister);
1602}
1603
1604static struct kobj_type devid_ktype = {
1605 .sysfs_ops = &kobj_sysfs_ops,
1606 .default_groups = devid_groups,
1607 .release = btrfs_release_devid_kobj,
1608};
1609
Anand Jaincd36da22020-09-05 01:34:26 +08001610int btrfs_sysfs_add_device(struct btrfs_device *device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001611{
Anand Jain178a16c2020-09-05 01:34:23 +08001612 int ret;
Josef Bacika47bd782020-07-21 10:17:50 -04001613 unsigned int nofs_flag;
Anand Jain178a16c2020-09-05 01:34:23 +08001614 struct kobject *devices_kobj;
1615 struct kobject *devinfo_kobj;
1616
1617 /*
1618 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1619 * for the seed fs_devices
1620 */
1621 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1622 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1623 ASSERT(devices_kobj);
1624 ASSERT(devinfo_kobj);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001625
Josef Bacika47bd782020-07-21 10:17:50 -04001626 nofs_flag = memalloc_nofs_save();
Anand Jainf0853812014-01-15 17:22:28 +08001627
Anand Jain178a16c2020-09-05 01:34:23 +08001628 if (device->bdev) {
Christoph Hellwig8d652692020-11-17 08:18:55 +01001629 struct kobject *disk_kobj = bdev_kobj(device->bdev);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001630
Anand Jain178a16c2020-09-05 01:34:23 +08001631 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1632 if (ret) {
1633 btrfs_warn(device->fs_info,
1634 "creating sysfs device link for devid %llu failed: %d",
1635 device->devid, ret);
1636 goto out;
Anand Jain668e48af2020-01-06 19:38:31 +08001637 }
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001638 }
1639
Anand Jain178a16c2020-09-05 01:34:23 +08001640 init_completion(&device->kobj_unregister);
1641 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1642 devinfo_kobj, "%llu", device->devid);
1643 if (ret) {
1644 kobject_put(&device->devid_kobj);
1645 btrfs_warn(device->fs_info,
1646 "devinfo init for devid %llu failed: %d",
1647 device->devid, ret);
1648 }
1649
1650out:
1651 memalloc_nofs_restore(nofs_flag);
1652 return ret;
1653}
1654
Anand Jaincd36da22020-09-05 01:34:26 +08001655static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
Anand Jain178a16c2020-09-05 01:34:23 +08001656{
1657 int ret;
Anand Jaincd36da22020-09-05 01:34:26 +08001658 struct btrfs_device *device;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001659 struct btrfs_fs_devices *seed;
Anand Jain178a16c2020-09-05 01:34:23 +08001660
1661 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1662 ret = btrfs_sysfs_add_device(device);
1663 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001664 goto fail;
Anand Jain178a16c2020-09-05 01:34:23 +08001665 }
1666
Anand Jain30b0e4e2020-09-05 01:34:28 +08001667 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1668 list_for_each_entry(device, &seed->devices, dev_list) {
1669 ret = btrfs_sysfs_add_device(device);
1670 if (ret)
Anand Jain7ad39122020-09-05 01:34:29 +08001671 goto fail;
Anand Jain30b0e4e2020-09-05 01:34:28 +08001672 }
1673 }
1674
Anand Jain178a16c2020-09-05 01:34:23 +08001675 return 0;
Anand Jain7ad39122020-09-05 01:34:29 +08001676
1677fail:
1678 btrfs_sysfs_remove_fs_devices(fs_devices);
1679 return ret;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001680}
1681
David Sterba5b286922019-08-01 18:50:16 +02001682void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1683{
1684 int ret;
1685
1686 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1687 if (ret)
1688 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1689 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1690 &disk_to_dev(bdev->bd_disk)->kobj);
1691}
1692
Nikolay Borisov8e560082020-08-12 16:18:51 +03001693void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1694
David Sterbaf93c3992019-08-01 18:50:16 +02001695{
1696 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1697
1698 /*
1699 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1700 * directory
1701 */
Nikolay Borisov8e560082020-08-12 16:18:51 +03001702 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
David Sterbaf93c3992019-08-01 18:50:16 +02001703 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1704 btrfs_warn(fs_devices->fs_info,
1705 "sysfs: failed to create fsid for sprout");
1706}
1707
Anand Jain668e48af2020-01-06 19:38:31 +08001708void btrfs_sysfs_update_devid(struct btrfs_device *device)
1709{
1710 char tmp[24];
1711
1712 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1713
1714 if (kobject_rename(&device->devid_kobj, tmp))
1715 btrfs_warn(device->fs_devices->fs_info,
1716 "sysfs: failed to update devid for %llu",
1717 device->devid);
1718}
1719
Jeff Mahoney510d7362013-11-01 13:06:59 -04001720/* /sys/fs/btrfs/ entry */
1721static struct kset *btrfs_kset;
1722
Anand Jain72059212015-03-10 06:38:26 +08001723/*
Anand Jainc6761a92019-11-21 17:33:32 +08001724 * Creates:
1725 * /sys/fs/btrfs/UUID
1726 *
Anand Jain72059212015-03-10 06:38:26 +08001727 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001728 */
Anand Jainc6761a92019-11-21 17:33:32 +08001729int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001730{
1731 int error;
1732
Anand Jain2e7910d2015-03-10 06:38:29 +08001733 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001734 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001735 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1736 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001737 if (error) {
1738 kobject_put(&fs_devs->fsid_kobj);
1739 return error;
1740 }
1741
Anand Jainbc036bb2019-11-21 17:33:34 +08001742 fs_devs->devices_kobj = kobject_create_and_add("devices",
1743 &fs_devs->fsid_kobj);
1744 if (!fs_devs->devices_kobj) {
1745 btrfs_err(fs_devs->fs_info,
1746 "failed to init sysfs device interface");
Anand Jain1f6087e2020-02-12 17:28:12 +08001747 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainbc036bb2019-11-21 17:33:34 +08001748 return -ENOMEM;
1749 }
1750
Anand Jaina013d142020-02-12 17:28:10 +08001751 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1752 &fs_devs->fsid_kobj);
1753 if (!fs_devs->devinfo_kobj) {
1754 btrfs_err(fs_devs->fs_info,
1755 "failed to init sysfs devinfo kobject");
1756 btrfs_sysfs_remove_fsid(fs_devs);
1757 return -ENOMEM;
1758 }
1759
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001760 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001761}
1762
Anand Jain96f31362015-08-14 18:32:46 +08001763int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001764{
1765 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001766 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001767 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001768
Anand Jaincd36da22020-09-05 01:34:26 +08001769 error = btrfs_sysfs_add_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001770 if (error)
1771 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001772
Anand Jainc1b7e472015-08-14 18:32:50 +08001773 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001774 if (error) {
Anand Jain53f8a742020-09-05 01:34:27 +08001775 btrfs_sysfs_remove_fs_devices(fs_devs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001776 return error;
1777 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001778
Anand Jainc1b7e472015-08-14 18:32:50 +08001779 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001780 &btrfs_feature_attr_group);
1781 if (error)
1782 goto failure;
1783
David Sterba6e369fe2019-06-13 17:23:02 +02001784#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhou93945cb2019-12-13 16:22:18 -08001785 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1786 if (!fs_info->debug_kobj) {
1787 error = -ENOMEM;
1788 goto failure;
1789 }
1790
1791 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
David Sterba6e369fe2019-06-13 17:23:02 +02001792 if (error)
1793 goto failure;
Dennis Zhoue4faab82019-12-13 16:22:19 -08001794
1795 /* Discard directory */
1796 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1797 fs_info->debug_kobj);
1798 if (!fs_info->discard_debug_kobj) {
1799 error = -ENOMEM;
1800 goto failure;
1801 }
1802
1803 error = sysfs_create_files(fs_info->discard_debug_kobj,
1804 discard_debug_attrs);
1805 if (error)
1806 goto failure;
David Sterba6e369fe2019-06-13 17:23:02 +02001807#endif
1808
Jeff Mahoneye453d982013-11-21 10:37:16 -05001809 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001810 if (error)
1811 goto failure;
1812
Nikolay Borisov3092c682020-07-03 11:13:15 +03001813 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1814 if (error)
1815 goto failure;
1816
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001817 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001818 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001819 if (!fs_info->space_info_kobj) {
1820 error = -ENOMEM;
1821 goto failure;
1822 }
1823
1824 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1825 if (error)
1826 goto failure;
1827
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001828 return 0;
1829failure:
Anand Jain6618a592015-08-14 18:32:47 +08001830 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001831 return error;
1832}
1833
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001834static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1835{
1836 return to_fs_info(kobj->parent->parent);
1837}
1838
1839#define QGROUP_ATTR(_member, _show_name) \
1840static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1841 struct kobj_attribute *a, \
1842 char *buf) \
1843{ \
1844 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1845 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1846 struct btrfs_qgroup, kobj); \
1847 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1848} \
1849BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1850
1851#define QGROUP_RSV_ATTR(_name, _type) \
1852static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1853 struct kobj_attribute *a, \
1854 char *buf) \
1855{ \
1856 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1857 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1858 struct btrfs_qgroup, kobj); \
1859 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1860 &fs_info->qgroup_lock, buf); \
1861} \
1862BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1863
1864QGROUP_ATTR(rfer, referenced);
1865QGROUP_ATTR(excl, exclusive);
1866QGROUP_ATTR(max_rfer, max_referenced);
1867QGROUP_ATTR(max_excl, max_exclusive);
1868QGROUP_ATTR(lim_flags, limit_flags);
1869QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1870QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1871QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1872
Anand Jaine7849e32021-08-10 21:55:59 +08001873/*
1874 * Qgroup information.
1875 *
1876 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
1877 */
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001878static struct attribute *qgroup_attrs[] = {
1879 BTRFS_ATTR_PTR(qgroup, referenced),
1880 BTRFS_ATTR_PTR(qgroup, exclusive),
1881 BTRFS_ATTR_PTR(qgroup, max_referenced),
1882 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1883 BTRFS_ATTR_PTR(qgroup, limit_flags),
1884 BTRFS_ATTR_PTR(qgroup, rsv_data),
1885 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1886 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1887 NULL
1888};
1889ATTRIBUTE_GROUPS(qgroup);
1890
1891static void qgroup_release(struct kobject *kobj)
1892{
1893 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1894
1895 memset(&qgroup->kobj, 0, sizeof(*kobj));
1896}
1897
1898static struct kobj_type qgroup_ktype = {
1899 .sysfs_ops = &kobj_sysfs_ops,
1900 .release = qgroup_release,
1901 .default_groups = qgroup_groups,
1902};
1903
1904int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1905 struct btrfs_qgroup *qgroup)
1906{
1907 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1908 int ret;
1909
1910 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1911 return 0;
1912 if (qgroup->kobj.state_initialized)
1913 return 0;
1914 if (!qgroups_kobj)
1915 return -EINVAL;
1916
1917 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1918 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1919 btrfs_qgroup_subvolid(qgroup->qgroupid));
1920 if (ret < 0)
1921 kobject_put(&qgroup->kobj);
1922
1923 return ret;
1924}
1925
1926void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1927{
1928 struct btrfs_qgroup *qgroup;
1929 struct btrfs_qgroup *next;
1930
1931 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1932 return;
1933
1934 rbtree_postorder_for_each_entry_safe(qgroup, next,
1935 &fs_info->qgroup_tree, node)
1936 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
Qu Wenruo62ab2cc2020-08-03 14:20:11 +08001937 if (fs_info->qgroups_kobj) {
1938 kobject_del(fs_info->qgroups_kobj);
1939 kobject_put(fs_info->qgroups_kobj);
1940 fs_info->qgroups_kobj = NULL;
1941 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001942}
1943
1944/* Called when qgroups get initialized, thus there is no need for locking */
1945int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1946{
1947 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1948 struct btrfs_qgroup *qgroup;
1949 struct btrfs_qgroup *next;
1950 int ret = 0;
1951
1952 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1953 return 0;
1954
1955 ASSERT(fsid_kobj);
1956 if (fs_info->qgroups_kobj)
1957 return 0;
1958
1959 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1960 if (!fs_info->qgroups_kobj) {
1961 ret = -ENOMEM;
1962 goto out;
1963 }
1964 rbtree_postorder_for_each_entry_safe(qgroup, next,
1965 &fs_info->qgroup_tree, node) {
1966 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1967 if (ret < 0)
1968 goto out;
1969 }
1970
1971out:
1972 if (ret < 0)
1973 btrfs_sysfs_del_qgroups(fs_info);
1974 return ret;
1975}
1976
1977void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1978 struct btrfs_qgroup *qgroup)
1979{
1980 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1981 return;
1982
1983 if (qgroup->kobj.state_initialized) {
1984 kobject_del(&qgroup->kobj);
1985 kobject_put(&qgroup->kobj);
1986 }
1987}
David Sterba444e7512016-01-21 18:50:40 +01001988
1989/*
1990 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1991 * values in superblock. Call after any changes to incompat/compat_ro flags
1992 */
1993void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1994 u64 bit, enum btrfs_feature_set set)
1995{
1996 struct btrfs_fs_devices *fs_devs;
1997 struct kobject *fsid_kobj;
Leon Romanovsky24646482020-08-19 17:16:28 +03001998 u64 __maybe_unused features;
1999 int __maybe_unused ret;
David Sterba444e7512016-01-21 18:50:40 +01002000
2001 if (!fs_info)
2002 return;
2003
Leon Romanovsky24646482020-08-19 17:16:28 +03002004 /*
2005 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2006 * safe when called from some contexts (eg. balance)
2007 */
David Sterba444e7512016-01-21 18:50:40 +01002008 features = get_features(fs_info, set);
2009 ASSERT(bit & supported_feature_masks[set]);
2010
2011 fs_devs = fs_info->fs_devices;
2012 fsid_kobj = &fs_devs->fsid_kobj;
2013
David Sterbabf609202016-01-27 14:06:29 +01002014 if (!fsid_kobj->state_initialized)
2015 return;
2016
David Sterba444e7512016-01-21 18:50:40 +01002017 /*
2018 * FIXME: this is too heavy to update just one value, ideally we'd like
2019 * to use sysfs_update_group but some refactoring is needed first.
2020 */
2021 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2022 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2023}
2024
Liu Bof5c29bd2017-11-02 17:21:50 -06002025int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04002026{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04002027 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01002028
Greg KHe3fe4e72008-02-20 14:14:16 -05002029 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2030 if (!btrfs_kset)
2031 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04002032
David Sterba1bae3092014-02-05 15:36:18 +01002033 init_feature_attrs();
2034 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00002035 if (ret)
2036 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09002037 ret = sysfs_merge_group(&btrfs_kset->kobj,
2038 &btrfs_static_feature_attr_group);
2039 if (ret)
2040 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00002041
David Sterba6e369fe2019-06-13 17:23:02 +02002042#ifdef CONFIG_BTRFS_DEBUG
2043 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2044 if (ret)
2045 goto out2;
2046#endif
2047
Filipe Manana001a6482015-01-23 18:27:00 +00002048 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09002049
2050out_remove_group:
2051 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00002052out2:
Filipe Manana001a6482015-01-23 18:27:00 +00002053 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01002054
2055 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04002056}
2057
David Sterbae67c7182018-02-19 17:24:18 +01002058void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04002059{
Misono Tomohirof902bd32018-05-17 14:24:51 +09002060 sysfs_unmerge_group(&btrfs_kset->kobj,
2061 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04002062 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Dennis Zhou71e89782019-12-13 16:22:17 -08002063#ifdef CONFIG_BTRFS_DEBUG
2064 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2065#endif
Greg KHe3fe4e72008-02-20 14:14:16 -05002066 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04002067}
Chris Mason55d47412008-02-20 16:02:51 -05002068