blob: a39bff64ff24edfc13fee709b8510e2c66c8c524 [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"
17#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040018#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040019#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040020#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040021#include "block-group.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040022
David Sterba9188db62019-08-02 12:52:48 +020023struct btrfs_feature_attr {
24 struct kobj_attribute kobj_attr;
25 enum btrfs_feature_set feature_set;
26 u64 feature_bit;
27};
28
29/* For raid type sysfs entries */
30struct raid_kobject {
31 u64 flags;
32 struct kobject kobj;
33};
34
35#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
36{ \
37 .attr = { .name = __stringify(_name), .mode = _mode }, \
38 .show = _show, \
39 .store = _store, \
40}
41
42#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
43 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
44 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
45
46#define BTRFS_ATTR(_prefix, _name, _show) \
47 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
48 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
49
50#define BTRFS_ATTR_PTR(_prefix, _name) \
51 (&btrfs_attr_##_prefix##_##_name.attr)
52
53#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
54static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
55 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
56 btrfs_feature_attr_show, \
57 btrfs_feature_attr_store), \
58 .feature_set = _feature_set, \
59 .feature_bit = _feature_prefix ##_## _feature_bit, \
60}
61#define BTRFS_FEAT_ATTR_PTR(_name) \
62 (&btrfs_attr_features_##_name.kobj_attr.attr)
63
64#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
65 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
66#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
68#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
69 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
70
Jeff Mahoney510d7362013-11-01 13:06:59 -040071static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080072static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040073
David Sterba8f523162019-08-02 13:07:38 +020074static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
75{
76 return container_of(a, struct btrfs_feature_attr, kobj_attr);
77}
78
79static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
80{
81 return container_of(attr, struct kobj_attribute, attr);
82}
83
84static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
85 struct attribute *attr)
86{
87 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
88}
89
Jeff Mahoney510d7362013-11-01 13:06:59 -040090static u64 get_features(struct btrfs_fs_info *fs_info,
91 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040092{
Jeff Mahoney510d7362013-11-01 13:06:59 -040093 struct btrfs_super_block *disk_super = fs_info->super_copy;
94 if (set == FEAT_COMPAT)
95 return btrfs_super_compat_flags(disk_super);
96 else if (set == FEAT_COMPAT_RO)
97 return btrfs_super_compat_ro_flags(disk_super);
98 else
99 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400100}
101
Jeff Mahoneyba631942013-11-01 13:07:01 -0400102static void set_features(struct btrfs_fs_info *fs_info,
103 enum btrfs_feature_set set, u64 features)
104{
105 struct btrfs_super_block *disk_super = fs_info->super_copy;
106 if (set == FEAT_COMPAT)
107 btrfs_set_super_compat_flags(disk_super, features);
108 else if (set == FEAT_COMPAT_RO)
109 btrfs_set_super_compat_ro_flags(disk_super, features);
110 else
111 btrfs_set_super_incompat_flags(disk_super, features);
112}
113
114static int can_modify_feature(struct btrfs_feature_attr *fa)
115{
116 int val = 0;
117 u64 set, clear;
118 switch (fa->feature_set) {
119 case FEAT_COMPAT:
120 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
121 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
122 break;
123 case FEAT_COMPAT_RO:
124 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
125 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
126 break;
127 case FEAT_INCOMPAT:
128 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
129 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
130 break;
131 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -0400132 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +0100133 fa->feature_set);
134 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400135 }
136
137 if (set & fa->feature_bit)
138 val |= 1;
139 if (clear & fa->feature_bit)
140 val |= 2;
141
142 return val;
143}
144
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400145static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
146 struct kobj_attribute *a, char *buf)
147{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400148 int val = 0;
149 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400150 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400151 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400152 u64 features = get_features(fs_info, fa->feature_set);
153 if (features & fa->feature_bit)
154 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400155 } else
156 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400157
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100158 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400159}
160
Jeff Mahoneyba631942013-11-01 13:07:01 -0400161static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
162 struct kobj_attribute *a,
163 const char *buf, size_t count)
164{
165 struct btrfs_fs_info *fs_info;
166 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400167 u64 features, set, clear;
168 unsigned long val;
169 int ret;
170
171 fs_info = to_fs_info(kobj);
172 if (!fs_info)
173 return -EPERM;
174
David Howellsbc98a422017-07-17 08:45:34 +0100175 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100176 return -EROFS;
177
Jeff Mahoneyba631942013-11-01 13:07:01 -0400178 ret = kstrtoul(skip_spaces(buf), 0, &val);
179 if (ret)
180 return ret;
181
182 if (fa->feature_set == FEAT_COMPAT) {
183 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
184 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
185 } else if (fa->feature_set == FEAT_COMPAT_RO) {
186 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
187 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
188 } else {
189 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
190 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
191 }
192
193 features = get_features(fs_info, fa->feature_set);
194
195 /* Nothing to do */
196 if ((val && (features & fa->feature_bit)) ||
197 (!val && !(features & fa->feature_bit)))
198 return count;
199
200 if ((val && !(set & fa->feature_bit)) ||
201 (!val && !(clear & fa->feature_bit))) {
202 btrfs_info(fs_info,
203 "%sabling feature %s on mounted fs is not supported.",
204 val ? "En" : "Dis", fa->kobj_attr.attr.name);
205 return -EPERM;
206 }
207
208 btrfs_info(fs_info, "%s %s feature flag",
209 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
210
Jeff Mahoneyba631942013-11-01 13:07:01 -0400211 spin_lock(&fs_info->super_lock);
212 features = get_features(fs_info, fa->feature_set);
213 if (val)
214 features |= fa->feature_bit;
215 else
216 features &= ~fa->feature_bit;
217 set_features(fs_info, fa->feature_set, features);
218 spin_unlock(&fs_info->super_lock);
219
David Sterba0eae2742014-11-12 14:22:21 +0100220 /*
221 * We don't want to do full transaction commit from inside sysfs
222 */
223 btrfs_set_pending(fs_info, COMMIT);
224 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400225
226 return count;
227}
228
Jeff Mahoney510d7362013-11-01 13:06:59 -0400229static umode_t btrfs_feature_visible(struct kobject *kobj,
230 struct attribute *attr, int unused)
231{
232 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
233 umode_t mode = attr->mode;
234
235 if (fs_info) {
236 struct btrfs_feature_attr *fa;
237 u64 features;
238
239 fa = attr_to_btrfs_feature_attr(attr);
240 features = get_features(fs_info, fa->feature_set);
241
Jeff Mahoneyba631942013-11-01 13:07:01 -0400242 if (can_modify_feature(fa))
243 mode |= S_IWUSR;
244 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400245 mode = 0;
246 }
247
248 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400249}
250
251BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
252BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
253BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
254BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700255BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400256BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
257BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
258BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
259BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100260BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200261BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100262BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
David Sterbacfbb8252018-07-10 18:15:05 +0200263BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400264
265static struct attribute *btrfs_supported_feature_attrs[] = {
266 BTRFS_FEAT_ATTR_PTR(mixed_backref),
267 BTRFS_FEAT_ATTR_PTR(default_subvol),
268 BTRFS_FEAT_ATTR_PTR(mixed_groups),
269 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700270 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400271 BTRFS_FEAT_ATTR_PTR(big_metadata),
272 BTRFS_FEAT_ATTR_PTR(extended_iref),
273 BTRFS_FEAT_ATTR_PTR(raid56),
274 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100275 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200276 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100277 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200278 BTRFS_FEAT_ATTR_PTR(raid1c34),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400279 NULL
280};
281
Misono Tomohirof902bd32018-05-17 14:24:51 +0900282/*
283 * Features which depend on feature bits and may differ between each fs.
284 *
285 * /sys/fs/btrfs/features lists all available features of this kernel while
286 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
287 * can be changed online.
288 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400289static const struct attribute_group btrfs_feature_attr_group = {
290 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400291 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400292 .attrs = btrfs_supported_feature_attrs,
293};
Josef Bacik58176a92007-08-29 15:47:34 -0400294
Misono Tomohirof902bd32018-05-17 14:24:51 +0900295static ssize_t rmdir_subvol_show(struct kobject *kobj,
296 struct kobj_attribute *ka, char *buf)
297{
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100298 return scnprintf(buf, PAGE_SIZE, "0\n");
Misono Tomohirof902bd32018-05-17 14:24:51 +0900299}
300BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
301
David Sterbaf7cea562019-10-07 11:11:03 +0200302static ssize_t supported_checksums_show(struct kobject *kobj,
303 struct kobj_attribute *a, char *buf)
304{
305 ssize_t ret = 0;
306 int i;
307
308 for (i = 0; i < btrfs_get_num_csums(); i++) {
309 /*
310 * This "trick" only works as long as 'enum btrfs_csum_type' has
311 * no holes in it
312 */
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100313 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
David Sterbaf7cea562019-10-07 11:11:03 +0200314 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
315
316 }
317
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100318 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
David Sterbaf7cea562019-10-07 11:11:03 +0200319 return ret;
320}
321BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
322
Misono Tomohirof902bd32018-05-17 14:24:51 +0900323static struct attribute *btrfs_supported_static_feature_attrs[] = {
324 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200325 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900326 NULL
327};
328
329/*
330 * Features which only depend on kernel version.
331 *
332 * These are listed in /sys/fs/btrfs/features along with
333 * btrfs_feature_attr_group
334 */
335static const struct attribute_group btrfs_static_feature_attr_group = {
336 .name = "features",
337 .attrs = btrfs_supported_static_feature_attrs,
338};
339
David Sterba6e369fe2019-06-13 17:23:02 +0200340#ifdef CONFIG_BTRFS_DEBUG
341
342/*
Dennis Zhoue4faab82019-12-13 16:22:19 -0800343 * Discard statistics and tunables
344 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800345#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
346
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800347static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
348 struct kobj_attribute *a,
349 char *buf)
350{
351 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
352
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100353 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800354 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
355}
356BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
357
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800358static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
359 struct kobj_attribute *a,
360 char *buf)
361{
362 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
363
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100364 return scnprintf(buf, PAGE_SIZE, "%d\n",
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800365 atomic_read(&fs_info->discard_ctl.discardable_extents));
366}
367BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
368
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500369static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
370 struct kobj_attribute *a,
371 char *buf)
372{
373 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
374
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100375 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500376 fs_info->discard_ctl.discard_bitmap_bytes);
377}
378BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
379
380static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
381 struct kobj_attribute *a,
382 char *buf)
383{
384 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
385
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100386 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500387 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
388}
389BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
390
391static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
392 struct kobj_attribute *a,
393 char *buf)
394{
395 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
396
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100397 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500398 fs_info->discard_ctl.discard_extent_bytes);
399}
400BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
401
Dennis Zhoua2309302020-01-02 16:26:35 -0500402static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
403 struct kobj_attribute *a,
404 char *buf)
405{
406 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
407
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100408 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoua2309302020-01-02 16:26:35 -0500409 READ_ONCE(fs_info->discard_ctl.iops_limit));
410}
411
412static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
413 struct kobj_attribute *a,
414 const char *buf, size_t len)
415{
416 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
417 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
418 u32 iops_limit;
419 int ret;
420
421 ret = kstrtou32(buf, 10, &iops_limit);
422 if (ret)
423 return -EINVAL;
424
425 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
426
427 return len;
428}
429BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
430 btrfs_discard_iops_limit_store);
431
Dennis Zhoue93591b2020-01-02 16:26:36 -0500432static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
433 struct kobj_attribute *a,
434 char *buf)
435{
436 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
437
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100438 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoue93591b2020-01-02 16:26:36 -0500439 READ_ONCE(fs_info->discard_ctl.kbps_limit));
440}
441
442static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
443 struct kobj_attribute *a,
444 const char *buf, size_t len)
445{
446 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
447 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
448 u32 kbps_limit;
449 int ret;
450
451 ret = kstrtou32(buf, 10, &kbps_limit);
452 if (ret)
453 return -EINVAL;
454
455 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
456
457 return len;
458}
459BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
460 btrfs_discard_kbps_limit_store);
461
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500462static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
463 struct kobj_attribute *a,
464 char *buf)
465{
466 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
467
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100468 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500469 READ_ONCE(fs_info->discard_ctl.max_discard_size));
470}
471
472static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
473 struct kobj_attribute *a,
474 const char *buf, size_t len)
475{
476 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
477 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
478 u64 max_discard_size;
479 int ret;
480
481 ret = kstrtou64(buf, 10, &max_discard_size);
482 if (ret)
483 return -EINVAL;
484
485 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
486
487 return len;
488}
489BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
490 btrfs_discard_max_discard_size_store);
491
Dennis Zhoue4faab82019-12-13 16:22:19 -0800492static const struct attribute *discard_debug_attrs[] = {
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800493 BTRFS_ATTR_PTR(discard, discardable_bytes),
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800494 BTRFS_ATTR_PTR(discard, discardable_extents),
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500495 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
496 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
497 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
Dennis Zhoua2309302020-01-02 16:26:35 -0500498 BTRFS_ATTR_PTR(discard, iops_limit),
Dennis Zhoue93591b2020-01-02 16:26:36 -0500499 BTRFS_ATTR_PTR(discard, kbps_limit),
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500500 BTRFS_ATTR_PTR(discard, max_discard_size),
Dennis Zhoue4faab82019-12-13 16:22:19 -0800501 NULL,
502};
503
504/*
David Sterba6e369fe2019-06-13 17:23:02 +0200505 * Runtime debugging exported via sysfs
506 *
507 * /sys/fs/btrfs/debug - applies to module or all filesystems
508 * /sys/fs/btrfs/UUID - applies only to the given filesystem
509 */
Dennis Zhou93945cb2019-12-13 16:22:18 -0800510static const struct attribute *btrfs_debug_mount_attrs[] = {
511 NULL,
512};
513
David Sterba6e369fe2019-06-13 17:23:02 +0200514static struct attribute *btrfs_debug_feature_attrs[] = {
515 NULL
516};
517
518static const struct attribute_group btrfs_debug_feature_attr_group = {
519 .name = "debug",
520 .attrs = btrfs_debug_feature_attrs,
521};
522
523#endif
524
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400525static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
526{
527 u64 val;
528 if (lock)
529 spin_lock(lock);
530 val = *value_ptr;
531 if (lock)
532 spin_unlock(lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100533 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400534}
535
536static ssize_t global_rsv_size_show(struct kobject *kobj,
537 struct kobj_attribute *ka, char *buf)
538{
539 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
540 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
541 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
542}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200543BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400544
545static ssize_t global_rsv_reserved_show(struct kobject *kobj,
546 struct kobj_attribute *a, char *buf)
547{
548 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
549 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
550 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
551}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200552BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400553
554#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400555#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400556
557static ssize_t raid_bytes_show(struct kobject *kobj,
558 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200559BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
560BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400561
562static ssize_t raid_bytes_show(struct kobject *kobj,
563 struct kobj_attribute *attr, char *buf)
564
565{
566 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100567 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400568 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400569 u64 val = 0;
570
571 down_read(&sinfo->groups_sem);
572 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200573 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200574 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400575 else
David Sterbabf38be62019-10-23 18:48:11 +0200576 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400577 }
578 up_read(&sinfo->groups_sem);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100579 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400580}
581
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400582static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200583 BTRFS_ATTR_PTR(raid, total_bytes),
584 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400585 NULL
586};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400587ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400588
589static void release_raid_kobj(struct kobject *kobj)
590{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400591 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400592}
593
David Sterba536ea452019-08-01 17:55:55 +0200594static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400595 .sysfs_ops = &kobj_sysfs_ops,
596 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400597 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400598};
599
600#define SPACE_INFO_ATTR(field) \
601static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
602 struct kobj_attribute *a, \
603 char *buf) \
604{ \
605 struct btrfs_space_info *sinfo = to_space_info(kobj); \
606 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
607} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200608BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400609
610static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
611 struct kobj_attribute *a,
612 char *buf)
613{
614 struct btrfs_space_info *sinfo = to_space_info(kobj);
615 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100616 return scnprintf(buf, PAGE_SIZE, "%lld\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400617}
618
619SPACE_INFO_ATTR(flags);
620SPACE_INFO_ATTR(total_bytes);
621SPACE_INFO_ATTR(bytes_used);
622SPACE_INFO_ATTR(bytes_pinned);
623SPACE_INFO_ATTR(bytes_reserved);
624SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800625SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400626SPACE_INFO_ATTR(disk_used);
627SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200628BTRFS_ATTR(space_info, total_bytes_pinned,
629 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400630
631static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200632 BTRFS_ATTR_PTR(space_info, flags),
633 BTRFS_ATTR_PTR(space_info, total_bytes),
634 BTRFS_ATTR_PTR(space_info, bytes_used),
635 BTRFS_ATTR_PTR(space_info, bytes_pinned),
636 BTRFS_ATTR_PTR(space_info, bytes_reserved),
637 BTRFS_ATTR_PTR(space_info, bytes_may_use),
638 BTRFS_ATTR_PTR(space_info, bytes_readonly),
639 BTRFS_ATTR_PTR(space_info, disk_used),
640 BTRFS_ATTR_PTR(space_info, disk_total),
641 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400642 NULL,
643};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400644ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400645
646static void space_info_release(struct kobject *kobj)
647{
648 struct btrfs_space_info *sinfo = to_space_info(kobj);
649 percpu_counter_destroy(&sinfo->total_bytes_pinned);
650 kfree(sinfo);
651}
652
David Sterba27992d02019-08-01 17:55:55 +0200653static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400654 .sysfs_ops = &kobj_sysfs_ops,
655 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400656 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400657};
658
659static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200660 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
661 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400662 NULL,
663};
664
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400665static ssize_t btrfs_label_show(struct kobject *kobj,
666 struct kobj_attribute *a, char *buf)
667{
668 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900669 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200670 ssize_t ret;
671
672 spin_lock(&fs_info->super_lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100673 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
David Sterbaee17fc82016-04-26 16:22:06 +0200674 spin_unlock(&fs_info->super_lock);
675
676 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400677}
678
679static ssize_t btrfs_label_store(struct kobject *kobj,
680 struct kobj_attribute *a,
681 const char *buf, size_t len)
682{
683 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900684 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400685
David Sterba66ac9fe2016-04-26 16:03:57 +0200686 if (!fs_info)
687 return -EPERM;
688
David Howellsbc98a422017-07-17 08:45:34 +0100689 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800690 return -EROFS;
691
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900692 /*
693 * p_len is the len until the first occurrence of either
694 * '\n' or '\0'
695 */
696 p_len = strcspn(buf, "\n");
697
698 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400699 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400700
David Sterbaa6f69dc2014-05-30 19:29:05 +0200701 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900702 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
703 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200704 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400705
David Sterbaa6f69dc2014-05-30 19:29:05 +0200706 /*
707 * We don't want to do full transaction commit from inside sysfs
708 */
709 btrfs_set_pending(fs_info, COMMIT);
710 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400711
David Sterbaa6f69dc2014-05-30 19:29:05 +0200712 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400713}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200714BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400715
David Sterbadf935892014-05-07 18:17:16 +0200716static ssize_t btrfs_nodesize_show(struct kobject *kobj,
717 struct kobj_attribute *a, char *buf)
718{
719 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
720
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100721 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200722}
723
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200724BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200725
726static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
727 struct kobj_attribute *a, char *buf)
728{
729 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
730
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100731 return scnprintf(buf, PAGE_SIZE, "%u\n",
732 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200733}
734
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200735BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200736
737static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
738 struct kobj_attribute *a, char *buf)
739{
740 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
741
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100742 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200743}
744
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200745BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200746
Sargun Dhillon27234802017-05-11 21:18:03 +0000747static ssize_t quota_override_show(struct kobject *kobj,
748 struct kobj_attribute *a, char *buf)
749{
750 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
751 int quota_override;
752
753 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100754 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
Sargun Dhillon27234802017-05-11 21:18:03 +0000755}
756
757static ssize_t quota_override_store(struct kobject *kobj,
758 struct kobj_attribute *a,
759 const char *buf, size_t len)
760{
761 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
762 unsigned long knob;
763 int err;
764
765 if (!fs_info)
766 return -EPERM;
767
768 if (!capable(CAP_SYS_RESOURCE))
769 return -EPERM;
770
771 err = kstrtoul(buf, 10, &knob);
772 if (err)
773 return err;
774 if (knob > 1)
775 return -EINVAL;
776
777 if (knob)
778 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
779 else
780 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
781
782 return len;
783}
784
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200785BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000786
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200787static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
788 struct kobj_attribute *a, char *buf)
789{
790 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
791
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100792 return scnprintf(buf, PAGE_SIZE, "%pU\n",
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200793 fs_info->fs_devices->metadata_uuid);
794}
795
796BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
797
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200798static ssize_t btrfs_checksum_show(struct kobject *kobj,
799 struct kobj_attribute *a, char *buf)
800{
801 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
802 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
803
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100804 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200805 btrfs_super_csum_name(csum_type),
806 crypto_shash_driver_name(fs_info->csum_shash));
807}
808
809BTRFS_ATTR(, checksum, btrfs_checksum_show);
810
Anand Jain0dd29062015-03-10 06:38:27 +0800811static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200812 BTRFS_ATTR_PTR(, label),
813 BTRFS_ATTR_PTR(, nodesize),
814 BTRFS_ATTR_PTR(, sectorsize),
815 BTRFS_ATTR_PTR(, clone_alignment),
816 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200817 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200818 BTRFS_ATTR_PTR(, checksum),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400819 NULL,
820};
821
Anand Jainc1b7e472015-08-14 18:32:50 +0800822static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400823{
Anand Jain2e7910d2015-03-10 06:38:29 +0800824 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800825
Anand Jainc1b7e472015-08-14 18:32:50 +0800826 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800827 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400828}
829
830static struct kobj_type btrfs_ktype = {
831 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800832 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400833};
834
Anand Jain2e7910d2015-03-10 06:38:29 +0800835static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
836{
837 if (kobj->ktype != &btrfs_ktype)
838 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800839 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800840}
841
Jeff Mahoney510d7362013-11-01 13:06:59 -0400842static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
843{
844 if (kobj->ktype != &btrfs_ktype)
845 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800846 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400847}
Josef Bacik58176a92007-08-29 15:47:34 -0400848
Jeff Mahoneye453d982013-11-21 10:37:16 -0500849#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900850#define BTRFS_FEATURE_NAME_MAX 13
851static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
852static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500853
Tomohiro Misono6c521572018-05-16 17:09:26 +0900854static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500855 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
856 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
857 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
858};
859
860static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400861{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500862 int set;
863
864 for (set = 0; set < FEAT_MAX; set++) {
865 int i;
866 struct attribute *attrs[2];
867 struct attribute_group agroup = {
868 .name = "features",
869 .attrs = attrs,
870 };
871 u64 features = get_features(fs_info, set);
872 features &= ~supported_feature_masks[set];
873
874 if (!features)
875 continue;
876
877 attrs[1] = NULL;
878 for (i = 0; i < NUM_FEATURE_BITS; i++) {
879 struct btrfs_feature_attr *fa;
880
881 if (!(features & (1ULL << i)))
882 continue;
883
884 fa = &btrfs_feature_attrs[set][i];
885 attrs[0] = &fa->kobj_attr.attr;
886 if (add) {
887 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800888 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500889 &agroup);
890 if (ret)
891 return ret;
892 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800893 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500894 &agroup);
895 }
896
897 }
898 return 0;
899}
900
Anand Jain2e3e1282015-03-10 06:38:32 +0800901static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500902{
Anand Jaina013d142020-02-12 17:28:10 +0800903 if (fs_devs->devinfo_kobj) {
904 kobject_del(fs_devs->devinfo_kobj);
905 kobject_put(fs_devs->devinfo_kobj);
906 fs_devs->devinfo_kobj = NULL;
907 }
908
Anand Jainb5501502019-11-21 17:33:30 +0800909 if (fs_devs->devices_kobj) {
910 kobject_del(fs_devs->devices_kobj);
911 kobject_put(fs_devs->devices_kobj);
912 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800913 }
914
Anand Jainc1b7e472015-08-14 18:32:50 +0800915 if (fs_devs->fsid_kobj.state_initialized) {
916 kobject_del(&fs_devs->fsid_kobj);
917 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800918 wait_for_completion(&fs_devs->kobj_unregister);
919 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400920}
921
Anand Jain2e3e1282015-03-10 06:38:32 +0800922/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800923void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800924{
925 struct list_head *fs_uuids = btrfs_get_fs_uuids();
926
927 if (fs_devs) {
928 __btrfs_sysfs_remove_fsid(fs_devs);
929 return;
930 }
931
Anand Jainc4babc52018-04-12 10:29:25 +0800932 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800933 __btrfs_sysfs_remove_fsid(fs_devs);
934 }
935}
936
Anand Jain6618a592015-08-14 18:32:47 +0800937void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500938{
Anand Jain5a13f432015-03-10 06:38:31 +0800939 btrfs_reset_fs_info_ptr(fs_info);
940
Jeff Mahoneye453d982013-11-21 10:37:16 -0500941 if (fs_info->space_info_kobj) {
942 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
943 kobject_del(fs_info->space_info_kobj);
944 kobject_put(fs_info->space_info_kobj);
945 }
Dennis Zhou71e89782019-12-13 16:22:17 -0800946#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhoue4faab82019-12-13 16:22:19 -0800947 if (fs_info->discard_debug_kobj) {
948 sysfs_remove_files(fs_info->discard_debug_kobj,
949 discard_debug_attrs);
950 kobject_del(fs_info->discard_debug_kobj);
951 kobject_put(fs_info->discard_debug_kobj);
952 }
Dennis Zhou93945cb2019-12-13 16:22:18 -0800953 if (fs_info->debug_kobj) {
954 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
955 kobject_del(fs_info->debug_kobj);
956 kobject_put(fs_info->debug_kobj);
957 }
Dennis Zhou71e89782019-12-13 16:22:17 -0800958#endif
Jeff Mahoneye453d982013-11-21 10:37:16 -0500959 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800960 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
961 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jainf3cd2c52020-02-12 17:28:13 +0800962 btrfs_sysfs_remove_devices_dir(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500963}
964
David Sterbaf10152b2019-08-01 19:07:55 +0200965static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400966 [FEAT_COMPAT] = "compat",
967 [FEAT_COMPAT_RO] = "compat_ro",
968 [FEAT_INCOMPAT] = "incompat",
969};
970
David Sterbaf10152b2019-08-01 19:07:55 +0200971const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
972{
973 return btrfs_feature_set_names[set];
974}
975
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400976char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
977{
978 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
979 int len = 0;
980 int i;
981 char *str;
982
983 str = kmalloc(bufsize, GFP_KERNEL);
984 if (!str)
985 return str;
986
987 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
988 const char *name;
989
990 if (!(flags & (1ULL << i)))
991 continue;
992
993 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100994 len += scnprintf(str + len, bufsize - len, "%s%s",
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400995 len ? "," : "", name);
996 }
997
998 return str;
999}
1000
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001001static void init_feature_attrs(void)
1002{
1003 struct btrfs_feature_attr *fa;
1004 int set, i;
1005
1006 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1007 ARRAY_SIZE(btrfs_feature_attrs));
1008 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1009 ARRAY_SIZE(btrfs_feature_attrs[0]));
1010
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001011 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1012 memset(btrfs_unknown_feature_names, 0,
1013 sizeof(btrfs_unknown_feature_names));
1014
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001015 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1016 struct btrfs_feature_attr *sfa;
1017 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001018 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001019 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001020 bit = ilog2(sfa->feature_bit);
1021 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001022
1023 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1024 }
1025
1026 for (set = 0; set < FEAT_MAX; set++) {
1027 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1028 char *name = btrfs_unknown_feature_names[set][i];
1029 fa = &btrfs_feature_attrs[set][i];
1030
1031 if (fa->kobj_attr.attr.name)
1032 continue;
1033
Tomohiro Misono6c521572018-05-16 17:09:26 +09001034 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001035 btrfs_feature_set_names[set], i);
1036
1037 fa->kobj_attr.attr.name = name;
1038 fa->kobj_attr.attr.mode = S_IRUGO;
1039 fa->feature_set = set;
1040 fa->feature_bit = 1ULL << i;
1041 }
1042 }
1043}
1044
David Sterba32a99912019-08-01 17:49:55 +02001045/*
1046 * Create a sysfs entry for a given block group type at path
1047 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1048 */
David Sterba32da53862019-10-29 19:20:18 +01001049void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +02001050{
1051 struct btrfs_fs_info *fs_info = cache->fs_info;
1052 struct btrfs_space_info *space_info = cache->space_info;
1053 struct raid_kobject *rkobj;
1054 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1055 unsigned int nofs_flag;
1056 int ret;
1057
1058 /*
1059 * Setup a NOFS context because kobject_add(), deep in its call chain,
1060 * does GFP_KERNEL allocations, and we are often called in a context
1061 * where if reclaim is triggered we can deadlock (we are either holding
1062 * a transaction handle or some lock required for a transaction
1063 * commit).
1064 */
1065 nofs_flag = memalloc_nofs_save();
1066
1067 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1068 if (!rkobj) {
1069 memalloc_nofs_restore(nofs_flag);
1070 btrfs_warn(cache->fs_info,
1071 "couldn't alloc memory for raid level kobject");
1072 return;
1073 }
1074
1075 rkobj->flags = cache->flags;
1076 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1077 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1078 btrfs_bg_type_to_raid_name(rkobj->flags));
1079 memalloc_nofs_restore(nofs_flag);
1080 if (ret) {
1081 kobject_put(&rkobj->kobj);
1082 btrfs_warn(fs_info,
1083 "failed to add kobject for block cache, ignoring");
1084 return;
1085 }
1086
1087 space_info->block_group_kobjs[index] = &rkobj->kobj;
1088}
1089
David Sterbab5865ba2019-08-01 18:50:16 +02001090/*
1091 * Remove sysfs directories for all block group types of a given space info and
1092 * the space info as well
1093 */
1094void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1095{
1096 int i;
1097
1098 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1099 struct kobject *kobj;
1100
1101 kobj = space_info->block_group_kobjs[i];
1102 space_info->block_group_kobjs[i] = NULL;
1103 if (kobj) {
1104 kobject_del(kobj);
1105 kobject_put(kobj);
1106 }
1107 }
1108 kobject_del(&space_info->kobj);
1109 kobject_put(&space_info->kobj);
1110}
1111
David Sterbab8823272019-08-01 18:50:16 +02001112static const char *alloc_name(u64 flags)
1113{
1114 switch (flags) {
1115 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1116 return "mixed";
1117 case BTRFS_BLOCK_GROUP_METADATA:
1118 return "metadata";
1119 case BTRFS_BLOCK_GROUP_DATA:
1120 return "data";
1121 case BTRFS_BLOCK_GROUP_SYSTEM:
1122 return "system";
1123 default:
1124 WARN_ON(1);
1125 return "invalid-combination";
1126 };
1127}
1128
1129/*
1130 * Create a sysfs entry for a space info type at path
1131 * /sys/fs/btrfs/UUID/allocation/TYPE
1132 */
1133int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1134 struct btrfs_space_info *space_info)
1135{
1136 int ret;
1137
1138 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1139 fs_info->space_info_kobj, "%s",
1140 alloc_name(space_info->flags));
1141 if (ret) {
1142 kobject_put(&space_info->kobj);
1143 return ret;
1144 }
1145
1146 return 0;
1147}
1148
Anand Jaine7e1aa92015-03-10 06:38:21 +08001149/* when one_device is NULL, it removes all device links */
1150
Anand Jainf3cd2c52020-02-12 17:28:13 +08001151int btrfs_sysfs_remove_devices_dir(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +08001152 struct btrfs_device *one_device)
1153{
1154 struct hd_struct *disk;
1155 struct kobject *disk_kobj;
1156
Anand Jainb5501502019-11-21 17:33:30 +08001157 if (!fs_devices->devices_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +08001158 return -EINVAL;
1159
Anand Jain668e48af2020-01-06 19:38:31 +08001160 if (one_device) {
1161 if (one_device->bdev) {
1162 disk = one_device->bdev->bd_part;
1163 disk_kobj = &part_to_dev(disk)->kobj;
1164 sysfs_remove_link(fs_devices->devices_kobj,
1165 disk_kobj->name);
1166 }
Anand Jain99994cd2014-06-03 11:36:00 +08001167
Anand Jain668e48af2020-01-06 19:38:31 +08001168 kobject_del(&one_device->devid_kobj);
1169 kobject_put(&one_device->devid_kobj);
1170
1171 wait_for_completion(&one_device->kobj_unregister);
1172
1173 return 0;
Anand Jain99994cd2014-06-03 11:36:00 +08001174 }
1175
Anand Jain668e48af2020-01-06 19:38:31 +08001176 list_for_each_entry(one_device, &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +08001177
Anand Jain668e48af2020-01-06 19:38:31 +08001178 if (one_device->bdev) {
1179 disk = one_device->bdev->bd_part;
1180 disk_kobj = &part_to_dev(disk)->kobj;
1181 sysfs_remove_link(fs_devices->devices_kobj,
1182 disk_kobj->name);
1183 }
1184 kobject_del(&one_device->devid_kobj);
1185 kobject_put(&one_device->devid_kobj);
Anand Jaine7e1aa92015-03-10 06:38:21 +08001186
Anand Jain668e48af2020-01-06 19:38:31 +08001187 wait_for_completion(&one_device->kobj_unregister);
Anand Jaine7e1aa92015-03-10 06:38:21 +08001188 }
1189
Anand Jain99994cd2014-06-03 11:36:00 +08001190 return 0;
1191}
1192
Anand Jain668e48af2020-01-06 19:38:31 +08001193static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1194 struct kobj_attribute *a,
1195 char *buf)
1196{
1197 int val;
1198 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1199 devid_kobj);
1200
1201 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1202
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001203 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001204}
1205BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1206
Anand Jain25864772020-02-13 16:40:53 +08001207static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
Anand Jain668e48af2020-01-06 19:38:31 +08001208 struct kobj_attribute *a, char *buf)
1209{
1210 int val;
1211 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1212 devid_kobj);
1213
1214 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1215
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001216 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001217}
Anand Jain25864772020-02-13 16:40:53 +08001218BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
Anand Jain668e48af2020-01-06 19:38:31 +08001219
1220static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1221 struct kobj_attribute *a,
1222 char *buf)
1223{
1224 int val;
1225 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1226 devid_kobj);
1227
1228 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1229
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001230 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001231}
1232BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1233
1234static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1235 struct kobj_attribute *a, char *buf)
1236{
1237 int val;
1238 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1239 devid_kobj);
1240
1241 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1242
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001243 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001244}
1245BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1246
1247static struct attribute *devid_attrs[] = {
1248 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1249 BTRFS_ATTR_PTR(devid, missing),
1250 BTRFS_ATTR_PTR(devid, replace_target),
1251 BTRFS_ATTR_PTR(devid, writeable),
1252 NULL
1253};
1254ATTRIBUTE_GROUPS(devid);
1255
1256static void btrfs_release_devid_kobj(struct kobject *kobj)
1257{
1258 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1259 devid_kobj);
1260
1261 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1262 complete(&device->kobj_unregister);
1263}
1264
1265static struct kobj_type devid_ktype = {
1266 .sysfs_ops = &kobj_sysfs_ops,
1267 .default_groups = devid_groups,
1268 .release = btrfs_release_devid_kobj,
1269};
1270
Anand Jainf3cd2c52020-02-12 17:28:13 +08001271int btrfs_sysfs_add_devices_dir(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +08001272 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001273{
1274 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001275 struct btrfs_device *dev;
1276
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001277 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +08001278
Anand Jain0d393762014-06-03 11:36:01 +08001279 if (one_device && one_device != dev)
1280 continue;
1281
Anand Jain668e48af2020-01-06 19:38:31 +08001282 if (dev->bdev) {
1283 struct hd_struct *disk;
1284 struct kobject *disk_kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001285
Anand Jain668e48af2020-01-06 19:38:31 +08001286 disk = dev->bdev->bd_part;
1287 disk_kobj = &part_to_dev(disk)->kobj;
1288
1289 error = sysfs_create_link(fs_devices->devices_kobj,
1290 disk_kobj, disk_kobj->name);
1291 if (error)
1292 break;
1293 }
1294
1295 init_completion(&dev->kobj_unregister);
1296 error = kobject_init_and_add(&dev->devid_kobj, &devid_ktype,
Anand Jain1b9867e2020-02-12 17:28:11 +08001297 fs_devices->devinfo_kobj, "%llu",
Anand Jain668e48af2020-01-06 19:38:31 +08001298 dev->devid);
1299 if (error) {
1300 kobject_put(&dev->devid_kobj);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001301 break;
Anand Jain668e48af2020-01-06 19:38:31 +08001302 }
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001303 }
1304
1305 return error;
1306}
1307
David Sterba5b286922019-08-01 18:50:16 +02001308void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1309{
1310 int ret;
1311
1312 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1313 if (ret)
1314 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1315 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1316 &disk_to_dev(bdev->bd_disk)->kobj);
1317}
1318
David Sterbaf93c3992019-08-01 18:50:16 +02001319void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1320 const u8 *fsid)
1321{
1322 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1323
1324 /*
1325 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1326 * directory
1327 */
1328 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1329 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1330 btrfs_warn(fs_devices->fs_info,
1331 "sysfs: failed to create fsid for sprout");
1332}
1333
Anand Jain668e48af2020-01-06 19:38:31 +08001334void btrfs_sysfs_update_devid(struct btrfs_device *device)
1335{
1336 char tmp[24];
1337
1338 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1339
1340 if (kobject_rename(&device->devid_kobj, tmp))
1341 btrfs_warn(device->fs_devices->fs_info,
1342 "sysfs: failed to update devid for %llu",
1343 device->devid);
1344}
1345
Jeff Mahoney510d7362013-11-01 13:06:59 -04001346/* /sys/fs/btrfs/ entry */
1347static struct kset *btrfs_kset;
1348
Anand Jain72059212015-03-10 06:38:26 +08001349/*
Anand Jainc6761a92019-11-21 17:33:32 +08001350 * Creates:
1351 * /sys/fs/btrfs/UUID
1352 *
Anand Jain72059212015-03-10 06:38:26 +08001353 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001354 */
Anand Jainc6761a92019-11-21 17:33:32 +08001355int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001356{
1357 int error;
1358
Anand Jain2e7910d2015-03-10 06:38:29 +08001359 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001360 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001361 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1362 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001363 if (error) {
1364 kobject_put(&fs_devs->fsid_kobj);
1365 return error;
1366 }
1367
Anand Jainbc036bb2019-11-21 17:33:34 +08001368 fs_devs->devices_kobj = kobject_create_and_add("devices",
1369 &fs_devs->fsid_kobj);
1370 if (!fs_devs->devices_kobj) {
1371 btrfs_err(fs_devs->fs_info,
1372 "failed to init sysfs device interface");
Anand Jain1f6087e2020-02-12 17:28:12 +08001373 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainbc036bb2019-11-21 17:33:34 +08001374 return -ENOMEM;
1375 }
1376
Anand Jaina013d142020-02-12 17:28:10 +08001377 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1378 &fs_devs->fsid_kobj);
1379 if (!fs_devs->devinfo_kobj) {
1380 btrfs_err(fs_devs->fs_info,
1381 "failed to init sysfs devinfo kobject");
1382 btrfs_sysfs_remove_fsid(fs_devs);
1383 return -ENOMEM;
1384 }
1385
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001386 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001387}
1388
Anand Jain96f31362015-08-14 18:32:46 +08001389int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001390{
1391 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001392 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001393 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001394
Anand Jain5a13f432015-03-10 06:38:31 +08001395 btrfs_set_fs_info_ptr(fs_info);
1396
Anand Jainf3cd2c52020-02-12 17:28:13 +08001397 error = btrfs_sysfs_add_devices_dir(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001398 if (error)
1399 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001400
Anand Jainc1b7e472015-08-14 18:32:50 +08001401 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001402 if (error) {
Anand Jainf3cd2c52020-02-12 17:28:13 +08001403 btrfs_sysfs_remove_devices_dir(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001404 return error;
1405 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001406
Anand Jainc1b7e472015-08-14 18:32:50 +08001407 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001408 &btrfs_feature_attr_group);
1409 if (error)
1410 goto failure;
1411
David Sterba6e369fe2019-06-13 17:23:02 +02001412#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhou93945cb2019-12-13 16:22:18 -08001413 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1414 if (!fs_info->debug_kobj) {
1415 error = -ENOMEM;
1416 goto failure;
1417 }
1418
1419 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
David Sterba6e369fe2019-06-13 17:23:02 +02001420 if (error)
1421 goto failure;
Dennis Zhoue4faab82019-12-13 16:22:19 -08001422
1423 /* Discard directory */
1424 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1425 fs_info->debug_kobj);
1426 if (!fs_info->discard_debug_kobj) {
1427 error = -ENOMEM;
1428 goto failure;
1429 }
1430
1431 error = sysfs_create_files(fs_info->discard_debug_kobj,
1432 discard_debug_attrs);
1433 if (error)
1434 goto failure;
David Sterba6e369fe2019-06-13 17:23:02 +02001435#endif
1436
Jeff Mahoneye453d982013-11-21 10:37:16 -05001437 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001438 if (error)
1439 goto failure;
1440
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001441 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001442 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001443 if (!fs_info->space_info_kobj) {
1444 error = -ENOMEM;
1445 goto failure;
1446 }
1447
1448 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1449 if (error)
1450 goto failure;
1451
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001452 return 0;
1453failure:
Anand Jain6618a592015-08-14 18:32:47 +08001454 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001455 return error;
1456}
1457
David Sterba444e7512016-01-21 18:50:40 +01001458
1459/*
1460 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1461 * values in superblock. Call after any changes to incompat/compat_ro flags
1462 */
1463void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1464 u64 bit, enum btrfs_feature_set set)
1465{
1466 struct btrfs_fs_devices *fs_devs;
1467 struct kobject *fsid_kobj;
1468 u64 features;
1469 int ret;
1470
1471 if (!fs_info)
1472 return;
1473
1474 features = get_features(fs_info, set);
1475 ASSERT(bit & supported_feature_masks[set]);
1476
1477 fs_devs = fs_info->fs_devices;
1478 fsid_kobj = &fs_devs->fsid_kobj;
1479
David Sterbabf609202016-01-27 14:06:29 +01001480 if (!fsid_kobj->state_initialized)
1481 return;
1482
David Sterba444e7512016-01-21 18:50:40 +01001483 /*
1484 * FIXME: this is too heavy to update just one value, ideally we'd like
1485 * to use sysfs_update_group but some refactoring is needed first.
1486 */
1487 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1488 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1489}
1490
Liu Bof5c29bd2017-11-02 17:21:50 -06001491int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001492{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001493 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001494
Greg KHe3fe4e72008-02-20 14:14:16 -05001495 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1496 if (!btrfs_kset)
1497 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001498
David Sterba1bae3092014-02-05 15:36:18 +01001499 init_feature_attrs();
1500 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001501 if (ret)
1502 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001503 ret = sysfs_merge_group(&btrfs_kset->kobj,
1504 &btrfs_static_feature_attr_group);
1505 if (ret)
1506 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001507
David Sterba6e369fe2019-06-13 17:23:02 +02001508#ifdef CONFIG_BTRFS_DEBUG
1509 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1510 if (ret)
1511 goto out2;
1512#endif
1513
Filipe Manana001a6482015-01-23 18:27:00 +00001514 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001515
1516out_remove_group:
1517 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001518out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001519 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001520
1521 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001522}
1523
David Sterbae67c7182018-02-19 17:24:18 +01001524void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001525{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001526 sysfs_unmerge_group(&btrfs_kset->kobj,
1527 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001528 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Dennis Zhou71e89782019-12-13 16:22:17 -08001529#ifdef CONFIG_BTRFS_DEBUG
1530 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1531#endif
Greg KHe3fe4e72008-02-20 14:14:16 -05001532 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001533}
Chris Mason55d47412008-02-20 16:02:51 -05001534