blob: 5562ce1f70907c0baad718f6192d7adac2b57b80 [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>
Josef Bacik58176a92007-08-29 15:47:34 -040011#include <linux/kobject.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040012#include <linux/bug.h>
Josef Bacik58176a92007-08-29 15:47:34 -040013
Chris Masonbae45de2007-04-04 21:22:22 -040014#include "ctree.h"
15#include "disk-io.h"
16#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040017#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040018#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040019#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040020#include "block-group.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040021
Jeff Mahoney510d7362013-11-01 13:06:59 -040022static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080023static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040024
Jeff Mahoney510d7362013-11-01 13:06:59 -040025static u64 get_features(struct btrfs_fs_info *fs_info,
26 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040027{
Jeff Mahoney510d7362013-11-01 13:06:59 -040028 struct btrfs_super_block *disk_super = fs_info->super_copy;
29 if (set == FEAT_COMPAT)
30 return btrfs_super_compat_flags(disk_super);
31 else if (set == FEAT_COMPAT_RO)
32 return btrfs_super_compat_ro_flags(disk_super);
33 else
34 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040035}
36
Jeff Mahoneyba631942013-11-01 13:07:01 -040037static void set_features(struct btrfs_fs_info *fs_info,
38 enum btrfs_feature_set set, u64 features)
39{
40 struct btrfs_super_block *disk_super = fs_info->super_copy;
41 if (set == FEAT_COMPAT)
42 btrfs_set_super_compat_flags(disk_super, features);
43 else if (set == FEAT_COMPAT_RO)
44 btrfs_set_super_compat_ro_flags(disk_super, features);
45 else
46 btrfs_set_super_incompat_flags(disk_super, features);
47}
48
49static int can_modify_feature(struct btrfs_feature_attr *fa)
50{
51 int val = 0;
52 u64 set, clear;
53 switch (fa->feature_set) {
54 case FEAT_COMPAT:
55 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
56 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
57 break;
58 case FEAT_COMPAT_RO:
59 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
60 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
61 break;
62 case FEAT_INCOMPAT:
63 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
64 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
65 break;
66 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -040067 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +010068 fa->feature_set);
69 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -040070 }
71
72 if (set & fa->feature_bit)
73 val |= 1;
74 if (clear & fa->feature_bit)
75 val |= 2;
76
77 return val;
78}
79
Jeff Mahoney079b72b2013-11-01 13:06:57 -040080static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
81 struct kobj_attribute *a, char *buf)
82{
Jeff Mahoney510d7362013-11-01 13:06:59 -040083 int val = 0;
84 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -040085 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -040086 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -040087 u64 features = get_features(fs_info, fa->feature_set);
88 if (features & fa->feature_bit)
89 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -040090 } else
91 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -040092
93 return snprintf(buf, PAGE_SIZE, "%d\n", val);
94}
95
Jeff Mahoneyba631942013-11-01 13:07:01 -040096static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
97 struct kobj_attribute *a,
98 const char *buf, size_t count)
99{
100 struct btrfs_fs_info *fs_info;
101 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400102 u64 features, set, clear;
103 unsigned long val;
104 int ret;
105
106 fs_info = to_fs_info(kobj);
107 if (!fs_info)
108 return -EPERM;
109
David Howellsbc98a422017-07-17 08:45:34 +0100110 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100111 return -EROFS;
112
Jeff Mahoneyba631942013-11-01 13:07:01 -0400113 ret = kstrtoul(skip_spaces(buf), 0, &val);
114 if (ret)
115 return ret;
116
117 if (fa->feature_set == FEAT_COMPAT) {
118 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
119 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
120 } else if (fa->feature_set == FEAT_COMPAT_RO) {
121 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
122 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
123 } else {
124 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
125 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
126 }
127
128 features = get_features(fs_info, fa->feature_set);
129
130 /* Nothing to do */
131 if ((val && (features & fa->feature_bit)) ||
132 (!val && !(features & fa->feature_bit)))
133 return count;
134
135 if ((val && !(set & fa->feature_bit)) ||
136 (!val && !(clear & fa->feature_bit))) {
137 btrfs_info(fs_info,
138 "%sabling feature %s on mounted fs is not supported.",
139 val ? "En" : "Dis", fa->kobj_attr.attr.name);
140 return -EPERM;
141 }
142
143 btrfs_info(fs_info, "%s %s feature flag",
144 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
145
Jeff Mahoneyba631942013-11-01 13:07:01 -0400146 spin_lock(&fs_info->super_lock);
147 features = get_features(fs_info, fa->feature_set);
148 if (val)
149 features |= fa->feature_bit;
150 else
151 features &= ~fa->feature_bit;
152 set_features(fs_info, fa->feature_set, features);
153 spin_unlock(&fs_info->super_lock);
154
David Sterba0eae2742014-11-12 14:22:21 +0100155 /*
156 * We don't want to do full transaction commit from inside sysfs
157 */
158 btrfs_set_pending(fs_info, COMMIT);
159 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400160
161 return count;
162}
163
Jeff Mahoney510d7362013-11-01 13:06:59 -0400164static umode_t btrfs_feature_visible(struct kobject *kobj,
165 struct attribute *attr, int unused)
166{
167 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
168 umode_t mode = attr->mode;
169
170 if (fs_info) {
171 struct btrfs_feature_attr *fa;
172 u64 features;
173
174 fa = attr_to_btrfs_feature_attr(attr);
175 features = get_features(fs_info, fa->feature_set);
176
Jeff Mahoneyba631942013-11-01 13:07:01 -0400177 if (can_modify_feature(fa))
178 mode |= S_IWUSR;
179 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400180 mode = 0;
181 }
182
183 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400184}
185
186BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
187BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
188BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
189BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700190BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400191BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
192BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
193BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
194BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100195BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200196BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100197BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400198
199static struct attribute *btrfs_supported_feature_attrs[] = {
200 BTRFS_FEAT_ATTR_PTR(mixed_backref),
201 BTRFS_FEAT_ATTR_PTR(default_subvol),
202 BTRFS_FEAT_ATTR_PTR(mixed_groups),
203 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700204 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400205 BTRFS_FEAT_ATTR_PTR(big_metadata),
206 BTRFS_FEAT_ATTR_PTR(extended_iref),
207 BTRFS_FEAT_ATTR_PTR(raid56),
208 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100209 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200210 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100211 BTRFS_FEAT_ATTR_PTR(free_space_tree),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400212 NULL
213};
214
Misono Tomohirof902bd32018-05-17 14:24:51 +0900215/*
216 * Features which depend on feature bits and may differ between each fs.
217 *
218 * /sys/fs/btrfs/features lists all available features of this kernel while
219 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
220 * can be changed online.
221 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400222static const struct attribute_group btrfs_feature_attr_group = {
223 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400224 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400225 .attrs = btrfs_supported_feature_attrs,
226};
Josef Bacik58176a92007-08-29 15:47:34 -0400227
Misono Tomohirof902bd32018-05-17 14:24:51 +0900228static ssize_t rmdir_subvol_show(struct kobject *kobj,
229 struct kobj_attribute *ka, char *buf)
230{
231 return snprintf(buf, PAGE_SIZE, "0\n");
232}
233BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
234
235static struct attribute *btrfs_supported_static_feature_attrs[] = {
236 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
237 NULL
238};
239
240/*
241 * Features which only depend on kernel version.
242 *
243 * These are listed in /sys/fs/btrfs/features along with
244 * btrfs_feature_attr_group
245 */
246static const struct attribute_group btrfs_static_feature_attr_group = {
247 .name = "features",
248 .attrs = btrfs_supported_static_feature_attrs,
249};
250
David Sterba6e369fe2019-06-13 17:23:02 +0200251#ifdef CONFIG_BTRFS_DEBUG
252
253/*
254 * Runtime debugging exported via sysfs
255 *
256 * /sys/fs/btrfs/debug - applies to module or all filesystems
257 * /sys/fs/btrfs/UUID - applies only to the given filesystem
258 */
259static struct attribute *btrfs_debug_feature_attrs[] = {
260 NULL
261};
262
263static const struct attribute_group btrfs_debug_feature_attr_group = {
264 .name = "debug",
265 .attrs = btrfs_debug_feature_attrs,
266};
267
268#endif
269
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400270static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
271{
272 u64 val;
273 if (lock)
274 spin_lock(lock);
275 val = *value_ptr;
276 if (lock)
277 spin_unlock(lock);
278 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
279}
280
281static ssize_t global_rsv_size_show(struct kobject *kobj,
282 struct kobj_attribute *ka, char *buf)
283{
284 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
285 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
286 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
287}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200288BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400289
290static ssize_t global_rsv_reserved_show(struct kobject *kobj,
291 struct kobj_attribute *a, char *buf)
292{
293 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
294 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
295 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
296}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200297BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400298
299#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400300#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400301
302static ssize_t raid_bytes_show(struct kobject *kobj,
303 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200304BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
305BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400306
307static ssize_t raid_bytes_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *buf)
309
310{
311 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
312 struct btrfs_block_group_cache *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400313 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400314 u64 val = 0;
315
316 down_read(&sinfo->groups_sem);
317 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200318 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400319 val += block_group->key.offset;
320 else
321 val += btrfs_block_group_used(&block_group->item);
322 }
323 up_read(&sinfo->groups_sem);
324 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
325}
326
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400327static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200328 BTRFS_ATTR_PTR(raid, total_bytes),
329 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400330 NULL
331};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400332ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400333
334static void release_raid_kobj(struct kobject *kobj)
335{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400336 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400337}
338
David Sterba536ea452019-08-01 17:55:55 +0200339static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400340 .sysfs_ops = &kobj_sysfs_ops,
341 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400342 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400343};
344
345#define SPACE_INFO_ATTR(field) \
346static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
347 struct kobj_attribute *a, \
348 char *buf) \
349{ \
350 struct btrfs_space_info *sinfo = to_space_info(kobj); \
351 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
352} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200353BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400354
355static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
356 struct kobj_attribute *a,
357 char *buf)
358{
359 struct btrfs_space_info *sinfo = to_space_info(kobj);
360 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
361 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
362}
363
364SPACE_INFO_ATTR(flags);
365SPACE_INFO_ATTR(total_bytes);
366SPACE_INFO_ATTR(bytes_used);
367SPACE_INFO_ATTR(bytes_pinned);
368SPACE_INFO_ATTR(bytes_reserved);
369SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800370SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400371SPACE_INFO_ATTR(disk_used);
372SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200373BTRFS_ATTR(space_info, total_bytes_pinned,
374 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400375
376static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200377 BTRFS_ATTR_PTR(space_info, flags),
378 BTRFS_ATTR_PTR(space_info, total_bytes),
379 BTRFS_ATTR_PTR(space_info, bytes_used),
380 BTRFS_ATTR_PTR(space_info, bytes_pinned),
381 BTRFS_ATTR_PTR(space_info, bytes_reserved),
382 BTRFS_ATTR_PTR(space_info, bytes_may_use),
383 BTRFS_ATTR_PTR(space_info, bytes_readonly),
384 BTRFS_ATTR_PTR(space_info, disk_used),
385 BTRFS_ATTR_PTR(space_info, disk_total),
386 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400387 NULL,
388};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400389ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400390
391static void space_info_release(struct kobject *kobj)
392{
393 struct btrfs_space_info *sinfo = to_space_info(kobj);
394 percpu_counter_destroy(&sinfo->total_bytes_pinned);
395 kfree(sinfo);
396}
397
David Sterba27992d02019-08-01 17:55:55 +0200398static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400399 .sysfs_ops = &kobj_sysfs_ops,
400 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400401 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400402};
403
404static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200405 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
406 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400407 NULL,
408};
409
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400410static ssize_t btrfs_label_show(struct kobject *kobj,
411 struct kobj_attribute *a, char *buf)
412{
413 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900414 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200415 ssize_t ret;
416
417 spin_lock(&fs_info->super_lock);
418 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
419 spin_unlock(&fs_info->super_lock);
420
421 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400422}
423
424static ssize_t btrfs_label_store(struct kobject *kobj,
425 struct kobj_attribute *a,
426 const char *buf, size_t len)
427{
428 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900429 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400430
David Sterba66ac9fe2016-04-26 16:03:57 +0200431 if (!fs_info)
432 return -EPERM;
433
David Howellsbc98a422017-07-17 08:45:34 +0100434 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800435 return -EROFS;
436
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900437 /*
438 * p_len is the len until the first occurrence of either
439 * '\n' or '\0'
440 */
441 p_len = strcspn(buf, "\n");
442
443 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400444 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400445
David Sterbaa6f69dc2014-05-30 19:29:05 +0200446 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900447 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
448 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200449 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400450
David Sterbaa6f69dc2014-05-30 19:29:05 +0200451 /*
452 * We don't want to do full transaction commit from inside sysfs
453 */
454 btrfs_set_pending(fs_info, COMMIT);
455 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400456
David Sterbaa6f69dc2014-05-30 19:29:05 +0200457 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400458}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200459BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400460
David Sterbadf935892014-05-07 18:17:16 +0200461static ssize_t btrfs_nodesize_show(struct kobject *kobj,
462 struct kobj_attribute *a, char *buf)
463{
464 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
465
David Sterba093e0372018-03-16 14:31:43 +0100466 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200467}
468
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200469BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200470
471static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
472 struct kobj_attribute *a, char *buf)
473{
474 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
475
David Sterba093e0372018-03-16 14:31:43 +0100476 return snprintf(buf, PAGE_SIZE, "%u\n",
477 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200478}
479
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200480BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200481
482static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
483 struct kobj_attribute *a, char *buf)
484{
485 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
486
David Sterba093e0372018-03-16 14:31:43 +0100487 return snprintf(buf, PAGE_SIZE, "%u\n",
488 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200489}
490
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200491BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200492
Sargun Dhillon27234802017-05-11 21:18:03 +0000493static ssize_t quota_override_show(struct kobject *kobj,
494 struct kobj_attribute *a, char *buf)
495{
496 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
497 int quota_override;
498
499 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
500 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
501}
502
503static ssize_t quota_override_store(struct kobject *kobj,
504 struct kobj_attribute *a,
505 const char *buf, size_t len)
506{
507 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
508 unsigned long knob;
509 int err;
510
511 if (!fs_info)
512 return -EPERM;
513
514 if (!capable(CAP_SYS_RESOURCE))
515 return -EPERM;
516
517 err = kstrtoul(buf, 10, &knob);
518 if (err)
519 return err;
520 if (knob > 1)
521 return -EINVAL;
522
523 if (knob)
524 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
525 else
526 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
527
528 return len;
529}
530
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200531BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000532
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200533static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
534 struct kobj_attribute *a, char *buf)
535{
536 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
537
538 return snprintf(buf, PAGE_SIZE, "%pU\n",
539 fs_info->fs_devices->metadata_uuid);
540}
541
542BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
543
Anand Jain0dd29062015-03-10 06:38:27 +0800544static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200545 BTRFS_ATTR_PTR(, label),
546 BTRFS_ATTR_PTR(, nodesize),
547 BTRFS_ATTR_PTR(, sectorsize),
548 BTRFS_ATTR_PTR(, clone_alignment),
549 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200550 BTRFS_ATTR_PTR(, metadata_uuid),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400551 NULL,
552};
553
Anand Jainc1b7e472015-08-14 18:32:50 +0800554static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400555{
Anand Jain2e7910d2015-03-10 06:38:29 +0800556 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800557
Anand Jainc1b7e472015-08-14 18:32:50 +0800558 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800559 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400560}
561
562static struct kobj_type btrfs_ktype = {
563 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800564 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400565};
566
Anand Jain2e7910d2015-03-10 06:38:29 +0800567static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
568{
569 if (kobj->ktype != &btrfs_ktype)
570 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800571 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800572}
573
Jeff Mahoney510d7362013-11-01 13:06:59 -0400574static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
575{
576 if (kobj->ktype != &btrfs_ktype)
577 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800578 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400579}
Josef Bacik58176a92007-08-29 15:47:34 -0400580
Jeff Mahoneye453d982013-11-21 10:37:16 -0500581#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900582#define BTRFS_FEATURE_NAME_MAX 13
583static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
584static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500585
Tomohiro Misono6c521572018-05-16 17:09:26 +0900586static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500587 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
588 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
589 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
590};
591
592static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400593{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500594 int set;
595
596 for (set = 0; set < FEAT_MAX; set++) {
597 int i;
598 struct attribute *attrs[2];
599 struct attribute_group agroup = {
600 .name = "features",
601 .attrs = attrs,
602 };
603 u64 features = get_features(fs_info, set);
604 features &= ~supported_feature_masks[set];
605
606 if (!features)
607 continue;
608
609 attrs[1] = NULL;
610 for (i = 0; i < NUM_FEATURE_BITS; i++) {
611 struct btrfs_feature_attr *fa;
612
613 if (!(features & (1ULL << i)))
614 continue;
615
616 fa = &btrfs_feature_attrs[set][i];
617 attrs[0] = &fa->kobj_attr.attr;
618 if (add) {
619 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800620 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500621 &agroup);
622 if (ret)
623 return ret;
624 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800625 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500626 &agroup);
627 }
628
629 }
630 return 0;
631}
632
Anand Jain2e3e1282015-03-10 06:38:32 +0800633static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500634{
Anand Jain2e7910d2015-03-10 06:38:29 +0800635 if (fs_devs->device_dir_kobj) {
636 kobject_del(fs_devs->device_dir_kobj);
637 kobject_put(fs_devs->device_dir_kobj);
638 fs_devs->device_dir_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800639 }
640
Anand Jainc1b7e472015-08-14 18:32:50 +0800641 if (fs_devs->fsid_kobj.state_initialized) {
642 kobject_del(&fs_devs->fsid_kobj);
643 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800644 wait_for_completion(&fs_devs->kobj_unregister);
645 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400646}
647
Anand Jain2e3e1282015-03-10 06:38:32 +0800648/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800649void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800650{
651 struct list_head *fs_uuids = btrfs_get_fs_uuids();
652
653 if (fs_devs) {
654 __btrfs_sysfs_remove_fsid(fs_devs);
655 return;
656 }
657
Anand Jainc4babc52018-04-12 10:29:25 +0800658 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800659 __btrfs_sysfs_remove_fsid(fs_devs);
660 }
661}
662
Anand Jain6618a592015-08-14 18:32:47 +0800663void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500664{
Anand Jain5a13f432015-03-10 06:38:31 +0800665 btrfs_reset_fs_info_ptr(fs_info);
666
Jeff Mahoneye453d982013-11-21 10:37:16 -0500667 if (fs_info->space_info_kobj) {
668 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
669 kobject_del(fs_info->space_info_kobj);
670 kobject_put(fs_info->space_info_kobj);
671 }
Jeff Mahoneye453d982013-11-21 10:37:16 -0500672 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800673 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
674 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jain32576042015-08-14 18:32:49 +0800675 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500676}
677
David Sterbaf10152b2019-08-01 19:07:55 +0200678static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400679 [FEAT_COMPAT] = "compat",
680 [FEAT_COMPAT_RO] = "compat_ro",
681 [FEAT_INCOMPAT] = "incompat",
682};
683
David Sterbaf10152b2019-08-01 19:07:55 +0200684const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
685{
686 return btrfs_feature_set_names[set];
687}
688
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400689char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
690{
691 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
692 int len = 0;
693 int i;
694 char *str;
695
696 str = kmalloc(bufsize, GFP_KERNEL);
697 if (!str)
698 return str;
699
700 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
701 const char *name;
702
703 if (!(flags & (1ULL << i)))
704 continue;
705
706 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
707 len += snprintf(str + len, bufsize - len, "%s%s",
708 len ? "," : "", name);
709 }
710
711 return str;
712}
713
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400714static void init_feature_attrs(void)
715{
716 struct btrfs_feature_attr *fa;
717 int set, i;
718
719 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
720 ARRAY_SIZE(btrfs_feature_attrs));
721 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
722 ARRAY_SIZE(btrfs_feature_attrs[0]));
723
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400724 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
725 memset(btrfs_unknown_feature_names, 0,
726 sizeof(btrfs_unknown_feature_names));
727
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400728 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
729 struct btrfs_feature_attr *sfa;
730 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400731 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400732 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400733 bit = ilog2(sfa->feature_bit);
734 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400735
736 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
737 }
738
739 for (set = 0; set < FEAT_MAX; set++) {
740 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
741 char *name = btrfs_unknown_feature_names[set][i];
742 fa = &btrfs_feature_attrs[set][i];
743
744 if (fa->kobj_attr.attr.name)
745 continue;
746
Tomohiro Misono6c521572018-05-16 17:09:26 +0900747 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400748 btrfs_feature_set_names[set], i);
749
750 fa->kobj_attr.attr.name = name;
751 fa->kobj_attr.attr.mode = S_IRUGO;
752 fa->feature_set = set;
753 fa->feature_bit = 1ULL << i;
754 }
755 }
756}
757
David Sterba32a99912019-08-01 17:49:55 +0200758/*
759 * Create a sysfs entry for a given block group type at path
760 * /sys/fs/btrfs/UUID/allocation/data/TYPE
761 */
762void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
763{
764 struct btrfs_fs_info *fs_info = cache->fs_info;
765 struct btrfs_space_info *space_info = cache->space_info;
766 struct raid_kobject *rkobj;
767 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
768 unsigned int nofs_flag;
769 int ret;
770
771 /*
772 * Setup a NOFS context because kobject_add(), deep in its call chain,
773 * does GFP_KERNEL allocations, and we are often called in a context
774 * where if reclaim is triggered we can deadlock (we are either holding
775 * a transaction handle or some lock required for a transaction
776 * commit).
777 */
778 nofs_flag = memalloc_nofs_save();
779
780 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
781 if (!rkobj) {
782 memalloc_nofs_restore(nofs_flag);
783 btrfs_warn(cache->fs_info,
784 "couldn't alloc memory for raid level kobject");
785 return;
786 }
787
788 rkobj->flags = cache->flags;
789 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
790 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
791 btrfs_bg_type_to_raid_name(rkobj->flags));
792 memalloc_nofs_restore(nofs_flag);
793 if (ret) {
794 kobject_put(&rkobj->kobj);
795 btrfs_warn(fs_info,
796 "failed to add kobject for block cache, ignoring");
797 return;
798 }
799
800 space_info->block_group_kobjs[index] = &rkobj->kobj;
801}
802
David Sterbab8823272019-08-01 18:50:16 +0200803static const char *alloc_name(u64 flags)
804{
805 switch (flags) {
806 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
807 return "mixed";
808 case BTRFS_BLOCK_GROUP_METADATA:
809 return "metadata";
810 case BTRFS_BLOCK_GROUP_DATA:
811 return "data";
812 case BTRFS_BLOCK_GROUP_SYSTEM:
813 return "system";
814 default:
815 WARN_ON(1);
816 return "invalid-combination";
817 };
818}
819
820/*
821 * Create a sysfs entry for a space info type at path
822 * /sys/fs/btrfs/UUID/allocation/TYPE
823 */
824int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
825 struct btrfs_space_info *space_info)
826{
827 int ret;
828
829 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
830 fs_info->space_info_kobj, "%s",
831 alloc_name(space_info->flags));
832 if (ret) {
833 kobject_put(&space_info->kobj);
834 return ret;
835 }
836
837 return 0;
838}
839
Anand Jaine7e1aa92015-03-10 06:38:21 +0800840/* when one_device is NULL, it removes all device links */
841
Anand Jain32576042015-08-14 18:32:49 +0800842int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +0800843 struct btrfs_device *one_device)
844{
845 struct hd_struct *disk;
846 struct kobject *disk_kobj;
847
Anand Jain6c14a162015-03-10 06:38:34 +0800848 if (!fs_devices->device_dir_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +0800849 return -EINVAL;
850
Liu Bo87fa3bb2014-07-29 19:09:39 +0800851 if (one_device && one_device->bdev) {
Anand Jain99994cd2014-06-03 11:36:00 +0800852 disk = one_device->bdev->bd_part;
853 disk_kobj = &part_to_dev(disk)->kobj;
854
Anand Jain6c14a162015-03-10 06:38:34 +0800855 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jain99994cd2014-06-03 11:36:00 +0800856 disk_kobj->name);
857 }
858
Anand Jaine7e1aa92015-03-10 06:38:21 +0800859 if (one_device)
860 return 0;
861
862 list_for_each_entry(one_device,
Anand Jain6c14a162015-03-10 06:38:34 +0800863 &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +0800864 if (!one_device->bdev)
865 continue;
866 disk = one_device->bdev->bd_part;
867 disk_kobj = &part_to_dev(disk)->kobj;
868
Anand Jain6c14a162015-03-10 06:38:34 +0800869 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jaine7e1aa92015-03-10 06:38:21 +0800870 disk_kobj->name);
871 }
872
Anand Jain99994cd2014-06-03 11:36:00 +0800873 return 0;
874}
875
Anand Jain2e7910d2015-03-10 06:38:29 +0800876int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
Anand Jain00c921c2015-03-10 06:38:28 +0800877{
Anand Jain2e7910d2015-03-10 06:38:29 +0800878 if (!fs_devs->device_dir_kobj)
879 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
Anand Jainc1b7e472015-08-14 18:32:50 +0800880 &fs_devs->fsid_kobj);
Anand Jain00c921c2015-03-10 06:38:28 +0800881
Anand Jain2e7910d2015-03-10 06:38:29 +0800882 if (!fs_devs->device_dir_kobj)
Anand Jain00c921c2015-03-10 06:38:28 +0800883 return -ENOMEM;
884
885 return 0;
886}
887
Anand Jaine3bd6972015-08-14 18:32:48 +0800888int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +0800889 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400890{
891 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400892 struct btrfs_device *dev;
893
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400894 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +0800895 struct hd_struct *disk;
896 struct kobject *disk_kobj;
897
898 if (!dev->bdev)
899 continue;
900
Anand Jain0d393762014-06-03 11:36:01 +0800901 if (one_device && one_device != dev)
902 continue;
903
Anand Jainf0853812014-01-15 17:22:28 +0800904 disk = dev->bdev->bd_part;
905 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400906
Anand Jain2e7910d2015-03-10 06:38:29 +0800907 error = sysfs_create_link(fs_devices->device_dir_kobj,
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400908 disk_kobj, disk_kobj->name);
909 if (error)
910 break;
911 }
912
913 return error;
914}
915
David Sterba5b286922019-08-01 18:50:16 +0200916void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
917{
918 int ret;
919
920 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
921 if (ret)
922 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
923 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
924 &disk_to_dev(bdev->bd_disk)->kobj);
925}
926
Jeff Mahoney510d7362013-11-01 13:06:59 -0400927/* /sys/fs/btrfs/ entry */
928static struct kset *btrfs_kset;
929
Anand Jain72059212015-03-10 06:38:26 +0800930/*
931 * Can be called by the device discovery thread.
932 * And parent can be specified for seed device
933 */
Anand Jain0c10e2d2015-03-10 06:38:35 +0800934int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
Anand Jain72059212015-03-10 06:38:26 +0800935 struct kobject *parent)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400936{
937 int error;
938
Anand Jain2e7910d2015-03-10 06:38:29 +0800939 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +0800940 fs_devs->fsid_kobj.kset = btrfs_kset;
941 error = kobject_init_and_add(&fs_devs->fsid_kobj,
Anand Jain24bd69c2015-03-10 06:38:39 +0800942 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +1000943 if (error) {
944 kobject_put(&fs_devs->fsid_kobj);
945 return error;
946 }
947
948 return 0;
Anand Jain72059212015-03-10 06:38:26 +0800949}
950
Anand Jain96f31362015-08-14 18:32:46 +0800951int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +0800952{
953 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +0800954 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +0800955 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +0800956
Anand Jain5a13f432015-03-10 06:38:31 +0800957 btrfs_set_fs_info_ptr(fs_info);
958
Anand Jaine3bd6972015-08-14 18:32:48 +0800959 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500960 if (error)
961 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400962
Anand Jainc1b7e472015-08-14 18:32:50 +0800963 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500964 if (error) {
Anand Jain32576042015-08-14 18:32:49 +0800965 btrfs_sysfs_rm_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500966 return error;
967 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400968
Anand Jainc1b7e472015-08-14 18:32:50 +0800969 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +0800970 &btrfs_feature_attr_group);
971 if (error)
972 goto failure;
973
David Sterba6e369fe2019-06-13 17:23:02 +0200974#ifdef CONFIG_BTRFS_DEBUG
975 error = sysfs_create_group(fsid_kobj,
976 &btrfs_debug_feature_attr_group);
977 if (error)
978 goto failure;
979#endif
980
Jeff Mahoneye453d982013-11-21 10:37:16 -0500981 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400982 if (error)
983 goto failure;
984
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400985 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +0800986 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400987 if (!fs_info->space_info_kobj) {
988 error = -ENOMEM;
989 goto failure;
990 }
991
992 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
993 if (error)
994 goto failure;
995
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400996 return 0;
997failure:
Anand Jain6618a592015-08-14 18:32:47 +0800998 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400999 return error;
1000}
1001
David Sterba444e7512016-01-21 18:50:40 +01001002
1003/*
1004 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1005 * values in superblock. Call after any changes to incompat/compat_ro flags
1006 */
1007void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1008 u64 bit, enum btrfs_feature_set set)
1009{
1010 struct btrfs_fs_devices *fs_devs;
1011 struct kobject *fsid_kobj;
1012 u64 features;
1013 int ret;
1014
1015 if (!fs_info)
1016 return;
1017
1018 features = get_features(fs_info, set);
1019 ASSERT(bit & supported_feature_masks[set]);
1020
1021 fs_devs = fs_info->fs_devices;
1022 fsid_kobj = &fs_devs->fsid_kobj;
1023
David Sterbabf609202016-01-27 14:06:29 +01001024 if (!fsid_kobj->state_initialized)
1025 return;
1026
David Sterba444e7512016-01-21 18:50:40 +01001027 /*
1028 * FIXME: this is too heavy to update just one value, ideally we'd like
1029 * to use sysfs_update_group but some refactoring is needed first.
1030 */
1031 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1032 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1033}
1034
Liu Bof5c29bd2017-11-02 17:21:50 -06001035int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001036{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001037 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001038
Greg KHe3fe4e72008-02-20 14:14:16 -05001039 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1040 if (!btrfs_kset)
1041 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001042
David Sterba1bae3092014-02-05 15:36:18 +01001043 init_feature_attrs();
1044 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001045 if (ret)
1046 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001047 ret = sysfs_merge_group(&btrfs_kset->kobj,
1048 &btrfs_static_feature_attr_group);
1049 if (ret)
1050 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001051
David Sterba6e369fe2019-06-13 17:23:02 +02001052#ifdef CONFIG_BTRFS_DEBUG
1053 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1054 if (ret)
1055 goto out2;
1056#endif
1057
Filipe Manana001a6482015-01-23 18:27:00 +00001058 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001059
1060out_remove_group:
1061 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001062out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001063 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001064
1065 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001066}
1067
David Sterbae67c7182018-02-19 17:24:18 +01001068void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001069{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001070 sysfs_unmerge_group(&btrfs_kset->kobj,
1071 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001072 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -05001073 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001074}
Chris Mason55d47412008-02-20 16:02:51 -05001075