blob: 3717c864ba2316ccc9e9ef319c7a0008afa59039 [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>
7#include <linux/slab.h>
8#include <linux/spinlock.h>
9#include <linux/completion.h>
Josef Bacik58176a92007-08-29 15:47:34 -040010#include <linux/kobject.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040011#include <linux/bug.h>
David Sterba1bae3092014-02-05 15:36:18 +010012#include <linux/debugfs.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"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040019
Jeff Mahoney510d7362013-11-01 13:06:59 -040020static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080021static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040022
Jeff Mahoney510d7362013-11-01 13:06:59 -040023static u64 get_features(struct btrfs_fs_info *fs_info,
24 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040025{
Jeff Mahoney510d7362013-11-01 13:06:59 -040026 struct btrfs_super_block *disk_super = fs_info->super_copy;
27 if (set == FEAT_COMPAT)
28 return btrfs_super_compat_flags(disk_super);
29 else if (set == FEAT_COMPAT_RO)
30 return btrfs_super_compat_ro_flags(disk_super);
31 else
32 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040033}
34
Jeff Mahoneyba631942013-11-01 13:07:01 -040035static void set_features(struct btrfs_fs_info *fs_info,
36 enum btrfs_feature_set set, u64 features)
37{
38 struct btrfs_super_block *disk_super = fs_info->super_copy;
39 if (set == FEAT_COMPAT)
40 btrfs_set_super_compat_flags(disk_super, features);
41 else if (set == FEAT_COMPAT_RO)
42 btrfs_set_super_compat_ro_flags(disk_super, features);
43 else
44 btrfs_set_super_incompat_flags(disk_super, features);
45}
46
47static int can_modify_feature(struct btrfs_feature_attr *fa)
48{
49 int val = 0;
50 u64 set, clear;
51 switch (fa->feature_set) {
52 case FEAT_COMPAT:
53 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
54 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
55 break;
56 case FEAT_COMPAT_RO:
57 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
58 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
59 break;
60 case FEAT_INCOMPAT:
61 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
62 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
63 break;
64 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -040065 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +010066 fa->feature_set);
67 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -040068 }
69
70 if (set & fa->feature_bit)
71 val |= 1;
72 if (clear & fa->feature_bit)
73 val |= 2;
74
75 return val;
76}
77
Jeff Mahoney079b72b2013-11-01 13:06:57 -040078static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
79 struct kobj_attribute *a, char *buf)
80{
Jeff Mahoney510d7362013-11-01 13:06:59 -040081 int val = 0;
82 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -040083 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -040084 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -040085 u64 features = get_features(fs_info, fa->feature_set);
86 if (features & fa->feature_bit)
87 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -040088 } else
89 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -040090
91 return snprintf(buf, PAGE_SIZE, "%d\n", val);
92}
93
Jeff Mahoneyba631942013-11-01 13:07:01 -040094static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
95 struct kobj_attribute *a,
96 const char *buf, size_t count)
97{
98 struct btrfs_fs_info *fs_info;
99 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400100 u64 features, set, clear;
101 unsigned long val;
102 int ret;
103
104 fs_info = to_fs_info(kobj);
105 if (!fs_info)
106 return -EPERM;
107
David Howellsbc98a422017-07-17 08:45:34 +0100108 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100109 return -EROFS;
110
Jeff Mahoneyba631942013-11-01 13:07:01 -0400111 ret = kstrtoul(skip_spaces(buf), 0, &val);
112 if (ret)
113 return ret;
114
115 if (fa->feature_set == FEAT_COMPAT) {
116 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
117 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
118 } else if (fa->feature_set == FEAT_COMPAT_RO) {
119 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
120 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
121 } else {
122 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
123 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
124 }
125
126 features = get_features(fs_info, fa->feature_set);
127
128 /* Nothing to do */
129 if ((val && (features & fa->feature_bit)) ||
130 (!val && !(features & fa->feature_bit)))
131 return count;
132
133 if ((val && !(set & fa->feature_bit)) ||
134 (!val && !(clear & fa->feature_bit))) {
135 btrfs_info(fs_info,
136 "%sabling feature %s on mounted fs is not supported.",
137 val ? "En" : "Dis", fa->kobj_attr.attr.name);
138 return -EPERM;
139 }
140
141 btrfs_info(fs_info, "%s %s feature flag",
142 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
143
Jeff Mahoneyba631942013-11-01 13:07:01 -0400144 spin_lock(&fs_info->super_lock);
145 features = get_features(fs_info, fa->feature_set);
146 if (val)
147 features |= fa->feature_bit;
148 else
149 features &= ~fa->feature_bit;
150 set_features(fs_info, fa->feature_set, features);
151 spin_unlock(&fs_info->super_lock);
152
David Sterba0eae2742014-11-12 14:22:21 +0100153 /*
154 * We don't want to do full transaction commit from inside sysfs
155 */
156 btrfs_set_pending(fs_info, COMMIT);
157 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400158
159 return count;
160}
161
Jeff Mahoney510d7362013-11-01 13:06:59 -0400162static umode_t btrfs_feature_visible(struct kobject *kobj,
163 struct attribute *attr, int unused)
164{
165 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
166 umode_t mode = attr->mode;
167
168 if (fs_info) {
169 struct btrfs_feature_attr *fa;
170 u64 features;
171
172 fa = attr_to_btrfs_feature_attr(attr);
173 features = get_features(fs_info, fa->feature_set);
174
Jeff Mahoneyba631942013-11-01 13:07:01 -0400175 if (can_modify_feature(fa))
176 mode |= S_IWUSR;
177 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400178 mode = 0;
179 }
180
181 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400182}
183
184BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
185BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
186BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
187BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700188BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400189BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
190BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
191BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
192BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100193BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
David Sterba3b5bb732016-01-21 18:36:46 +0100194BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400195
196static struct attribute *btrfs_supported_feature_attrs[] = {
197 BTRFS_FEAT_ATTR_PTR(mixed_backref),
198 BTRFS_FEAT_ATTR_PTR(default_subvol),
199 BTRFS_FEAT_ATTR_PTR(mixed_groups),
200 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700201 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400202 BTRFS_FEAT_ATTR_PTR(big_metadata),
203 BTRFS_FEAT_ATTR_PTR(extended_iref),
204 BTRFS_FEAT_ATTR_PTR(raid56),
205 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100206 BTRFS_FEAT_ATTR_PTR(no_holes),
David Sterba3b5bb732016-01-21 18:36:46 +0100207 BTRFS_FEAT_ATTR_PTR(free_space_tree),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400208 NULL
209};
210
Misono Tomohirof902bd32018-05-17 14:24:51 +0900211/*
212 * Features which depend on feature bits and may differ between each fs.
213 *
214 * /sys/fs/btrfs/features lists all available features of this kernel while
215 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
216 * can be changed online.
217 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400218static const struct attribute_group btrfs_feature_attr_group = {
219 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400220 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400221 .attrs = btrfs_supported_feature_attrs,
222};
Josef Bacik58176a92007-08-29 15:47:34 -0400223
Misono Tomohirof902bd32018-05-17 14:24:51 +0900224static ssize_t rmdir_subvol_show(struct kobject *kobj,
225 struct kobj_attribute *ka, char *buf)
226{
227 return snprintf(buf, PAGE_SIZE, "0\n");
228}
229BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
230
231static struct attribute *btrfs_supported_static_feature_attrs[] = {
232 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
233 NULL
234};
235
236/*
237 * Features which only depend on kernel version.
238 *
239 * These are listed in /sys/fs/btrfs/features along with
240 * btrfs_feature_attr_group
241 */
242static const struct attribute_group btrfs_static_feature_attr_group = {
243 .name = "features",
244 .attrs = btrfs_supported_static_feature_attrs,
245};
246
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400247static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
248{
249 u64 val;
250 if (lock)
251 spin_lock(lock);
252 val = *value_ptr;
253 if (lock)
254 spin_unlock(lock);
255 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
256}
257
258static ssize_t global_rsv_size_show(struct kobject *kobj,
259 struct kobj_attribute *ka, char *buf)
260{
261 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
262 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
263 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
264}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200265BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400266
267static ssize_t global_rsv_reserved_show(struct kobject *kobj,
268 struct kobj_attribute *a, char *buf)
269{
270 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
271 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
272 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
273}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200274BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400275
276#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400277#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400278
279static ssize_t raid_bytes_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200281BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
282BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400283
284static ssize_t raid_bytes_show(struct kobject *kobj,
285 struct kobj_attribute *attr, char *buf)
286
287{
288 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
289 struct btrfs_block_group_cache *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400290 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400291 u64 val = 0;
292
293 down_read(&sinfo->groups_sem);
294 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200295 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400296 val += block_group->key.offset;
297 else
298 val += btrfs_block_group_used(&block_group->item);
299 }
300 up_read(&sinfo->groups_sem);
301 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
302}
303
304static struct attribute *raid_attributes[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200305 BTRFS_ATTR_PTR(raid, total_bytes),
306 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400307 NULL
308};
309
310static void release_raid_kobj(struct kobject *kobj)
311{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400312 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400313}
314
315struct kobj_type btrfs_raid_ktype = {
316 .sysfs_ops = &kobj_sysfs_ops,
317 .release = release_raid_kobj,
318 .default_attrs = raid_attributes,
319};
320
321#define SPACE_INFO_ATTR(field) \
322static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
323 struct kobj_attribute *a, \
324 char *buf) \
325{ \
326 struct btrfs_space_info *sinfo = to_space_info(kobj); \
327 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
328} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200329BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400330
331static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
332 struct kobj_attribute *a,
333 char *buf)
334{
335 struct btrfs_space_info *sinfo = to_space_info(kobj);
336 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
337 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
338}
339
340SPACE_INFO_ATTR(flags);
341SPACE_INFO_ATTR(total_bytes);
342SPACE_INFO_ATTR(bytes_used);
343SPACE_INFO_ATTR(bytes_pinned);
344SPACE_INFO_ATTR(bytes_reserved);
345SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800346SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400347SPACE_INFO_ATTR(disk_used);
348SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200349BTRFS_ATTR(space_info, total_bytes_pinned,
350 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400351
352static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200353 BTRFS_ATTR_PTR(space_info, flags),
354 BTRFS_ATTR_PTR(space_info, total_bytes),
355 BTRFS_ATTR_PTR(space_info, bytes_used),
356 BTRFS_ATTR_PTR(space_info, bytes_pinned),
357 BTRFS_ATTR_PTR(space_info, bytes_reserved),
358 BTRFS_ATTR_PTR(space_info, bytes_may_use),
359 BTRFS_ATTR_PTR(space_info, bytes_readonly),
360 BTRFS_ATTR_PTR(space_info, disk_used),
361 BTRFS_ATTR_PTR(space_info, disk_total),
362 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400363 NULL,
364};
365
366static void space_info_release(struct kobject *kobj)
367{
368 struct btrfs_space_info *sinfo = to_space_info(kobj);
369 percpu_counter_destroy(&sinfo->total_bytes_pinned);
370 kfree(sinfo);
371}
372
373struct kobj_type space_info_ktype = {
374 .sysfs_ops = &kobj_sysfs_ops,
375 .release = space_info_release,
376 .default_attrs = space_info_attrs,
377};
378
379static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200380 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
381 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400382 NULL,
383};
384
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400385static ssize_t btrfs_label_show(struct kobject *kobj,
386 struct kobj_attribute *a, char *buf)
387{
388 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900389 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200390 ssize_t ret;
391
392 spin_lock(&fs_info->super_lock);
393 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
394 spin_unlock(&fs_info->super_lock);
395
396 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400397}
398
399static ssize_t btrfs_label_store(struct kobject *kobj,
400 struct kobj_attribute *a,
401 const char *buf, size_t len)
402{
403 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900404 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400405
David Sterba66ac9fe2016-04-26 16:03:57 +0200406 if (!fs_info)
407 return -EPERM;
408
David Howellsbc98a422017-07-17 08:45:34 +0100409 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800410 return -EROFS;
411
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900412 /*
413 * p_len is the len until the first occurrence of either
414 * '\n' or '\0'
415 */
416 p_len = strcspn(buf, "\n");
417
418 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400419 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400420
David Sterbaa6f69dc2014-05-30 19:29:05 +0200421 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900422 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
423 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200424 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400425
David Sterbaa6f69dc2014-05-30 19:29:05 +0200426 /*
427 * We don't want to do full transaction commit from inside sysfs
428 */
429 btrfs_set_pending(fs_info, COMMIT);
430 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400431
David Sterbaa6f69dc2014-05-30 19:29:05 +0200432 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400433}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200434BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400435
David Sterbadf935892014-05-07 18:17:16 +0200436static ssize_t btrfs_nodesize_show(struct kobject *kobj,
437 struct kobj_attribute *a, char *buf)
438{
439 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
440
David Sterba093e0372018-03-16 14:31:43 +0100441 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200442}
443
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200444BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200445
446static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
447 struct kobj_attribute *a, char *buf)
448{
449 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
450
David Sterba093e0372018-03-16 14:31:43 +0100451 return snprintf(buf, PAGE_SIZE, "%u\n",
452 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200453}
454
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200455BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200456
457static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
458 struct kobj_attribute *a, char *buf)
459{
460 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
461
David Sterba093e0372018-03-16 14:31:43 +0100462 return snprintf(buf, PAGE_SIZE, "%u\n",
463 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200464}
465
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200466BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200467
Sargun Dhillon27234802017-05-11 21:18:03 +0000468static ssize_t quota_override_show(struct kobject *kobj,
469 struct kobj_attribute *a, char *buf)
470{
471 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
472 int quota_override;
473
474 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
475 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
476}
477
478static ssize_t quota_override_store(struct kobject *kobj,
479 struct kobj_attribute *a,
480 const char *buf, size_t len)
481{
482 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
483 unsigned long knob;
484 int err;
485
486 if (!fs_info)
487 return -EPERM;
488
489 if (!capable(CAP_SYS_RESOURCE))
490 return -EPERM;
491
492 err = kstrtoul(buf, 10, &knob);
493 if (err)
494 return err;
495 if (knob > 1)
496 return -EINVAL;
497
498 if (knob)
499 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
500 else
501 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
502
503 return len;
504}
505
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200506BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000507
Anand Jain0dd29062015-03-10 06:38:27 +0800508static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200509 BTRFS_ATTR_PTR(, label),
510 BTRFS_ATTR_PTR(, nodesize),
511 BTRFS_ATTR_PTR(, sectorsize),
512 BTRFS_ATTR_PTR(, clone_alignment),
513 BTRFS_ATTR_PTR(, quota_override),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400514 NULL,
515};
516
Anand Jainc1b7e472015-08-14 18:32:50 +0800517static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400518{
Anand Jain2e7910d2015-03-10 06:38:29 +0800519 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800520
Anand Jainc1b7e472015-08-14 18:32:50 +0800521 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800522 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400523}
524
525static struct kobj_type btrfs_ktype = {
526 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800527 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400528};
529
Anand Jain2e7910d2015-03-10 06:38:29 +0800530static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
531{
532 if (kobj->ktype != &btrfs_ktype)
533 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800534 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800535}
536
Jeff Mahoney510d7362013-11-01 13:06:59 -0400537static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
538{
539 if (kobj->ktype != &btrfs_ktype)
540 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800541 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400542}
Josef Bacik58176a92007-08-29 15:47:34 -0400543
Jeff Mahoneye453d982013-11-21 10:37:16 -0500544#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900545#define BTRFS_FEATURE_NAME_MAX 13
546static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
547static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500548
Tomohiro Misono6c521572018-05-16 17:09:26 +0900549static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500550 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
551 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
552 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
553};
554
555static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400556{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500557 int set;
558
559 for (set = 0; set < FEAT_MAX; set++) {
560 int i;
561 struct attribute *attrs[2];
562 struct attribute_group agroup = {
563 .name = "features",
564 .attrs = attrs,
565 };
566 u64 features = get_features(fs_info, set);
567 features &= ~supported_feature_masks[set];
568
569 if (!features)
570 continue;
571
572 attrs[1] = NULL;
573 for (i = 0; i < NUM_FEATURE_BITS; i++) {
574 struct btrfs_feature_attr *fa;
575
576 if (!(features & (1ULL << i)))
577 continue;
578
579 fa = &btrfs_feature_attrs[set][i];
580 attrs[0] = &fa->kobj_attr.attr;
581 if (add) {
582 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800583 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500584 &agroup);
585 if (ret)
586 return ret;
587 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800588 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500589 &agroup);
590 }
591
592 }
593 return 0;
594}
595
Anand Jain2e3e1282015-03-10 06:38:32 +0800596static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500597{
Anand Jain2e7910d2015-03-10 06:38:29 +0800598 if (fs_devs->device_dir_kobj) {
599 kobject_del(fs_devs->device_dir_kobj);
600 kobject_put(fs_devs->device_dir_kobj);
601 fs_devs->device_dir_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800602 }
603
Anand Jainc1b7e472015-08-14 18:32:50 +0800604 if (fs_devs->fsid_kobj.state_initialized) {
605 kobject_del(&fs_devs->fsid_kobj);
606 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800607 wait_for_completion(&fs_devs->kobj_unregister);
608 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400609}
610
Anand Jain2e3e1282015-03-10 06:38:32 +0800611/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800612void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800613{
614 struct list_head *fs_uuids = btrfs_get_fs_uuids();
615
616 if (fs_devs) {
617 __btrfs_sysfs_remove_fsid(fs_devs);
618 return;
619 }
620
Anand Jainc4babc52018-04-12 10:29:25 +0800621 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800622 __btrfs_sysfs_remove_fsid(fs_devs);
623 }
624}
625
Anand Jain6618a592015-08-14 18:32:47 +0800626void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500627{
Anand Jain5a13f432015-03-10 06:38:31 +0800628 btrfs_reset_fs_info_ptr(fs_info);
629
Jeff Mahoneye453d982013-11-21 10:37:16 -0500630 if (fs_info->space_info_kobj) {
631 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
632 kobject_del(fs_info->space_info_kobj);
633 kobject_put(fs_info->space_info_kobj);
634 }
Jeff Mahoneye453d982013-11-21 10:37:16 -0500635 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800636 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
637 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jain32576042015-08-14 18:32:49 +0800638 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500639}
640
Tomohiro Misono6c521572018-05-16 17:09:26 +0900641const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400642 [FEAT_COMPAT] = "compat",
643 [FEAT_COMPAT_RO] = "compat_ro",
644 [FEAT_INCOMPAT] = "incompat",
645};
646
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400647char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
648{
649 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
650 int len = 0;
651 int i;
652 char *str;
653
654 str = kmalloc(bufsize, GFP_KERNEL);
655 if (!str)
656 return str;
657
658 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
659 const char *name;
660
661 if (!(flags & (1ULL << i)))
662 continue;
663
664 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
665 len += snprintf(str + len, bufsize - len, "%s%s",
666 len ? "," : "", name);
667 }
668
669 return str;
670}
671
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400672static void init_feature_attrs(void)
673{
674 struct btrfs_feature_attr *fa;
675 int set, i;
676
677 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
678 ARRAY_SIZE(btrfs_feature_attrs));
679 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
680 ARRAY_SIZE(btrfs_feature_attrs[0]));
681
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400682 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
683 memset(btrfs_unknown_feature_names, 0,
684 sizeof(btrfs_unknown_feature_names));
685
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400686 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
687 struct btrfs_feature_attr *sfa;
688 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400689 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400690 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400691 bit = ilog2(sfa->feature_bit);
692 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400693
694 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
695 }
696
697 for (set = 0; set < FEAT_MAX; set++) {
698 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
699 char *name = btrfs_unknown_feature_names[set][i];
700 fa = &btrfs_feature_attrs[set][i];
701
702 if (fa->kobj_attr.attr.name)
703 continue;
704
Tomohiro Misono6c521572018-05-16 17:09:26 +0900705 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400706 btrfs_feature_set_names[set], i);
707
708 fa->kobj_attr.attr.name = name;
709 fa->kobj_attr.attr.mode = S_IRUGO;
710 fa->feature_set = set;
711 fa->feature_bit = 1ULL << i;
712 }
713 }
714}
715
Anand Jaine7e1aa92015-03-10 06:38:21 +0800716/* when one_device is NULL, it removes all device links */
717
Anand Jain32576042015-08-14 18:32:49 +0800718int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +0800719 struct btrfs_device *one_device)
720{
721 struct hd_struct *disk;
722 struct kobject *disk_kobj;
723
Anand Jain6c14a162015-03-10 06:38:34 +0800724 if (!fs_devices->device_dir_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +0800725 return -EINVAL;
726
Liu Bo87fa3bb2014-07-29 19:09:39 +0800727 if (one_device && one_device->bdev) {
Anand Jain99994cd2014-06-03 11:36:00 +0800728 disk = one_device->bdev->bd_part;
729 disk_kobj = &part_to_dev(disk)->kobj;
730
Anand Jain6c14a162015-03-10 06:38:34 +0800731 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jain99994cd2014-06-03 11:36:00 +0800732 disk_kobj->name);
733 }
734
Anand Jaine7e1aa92015-03-10 06:38:21 +0800735 if (one_device)
736 return 0;
737
738 list_for_each_entry(one_device,
Anand Jain6c14a162015-03-10 06:38:34 +0800739 &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +0800740 if (!one_device->bdev)
741 continue;
742 disk = one_device->bdev->bd_part;
743 disk_kobj = &part_to_dev(disk)->kobj;
744
Anand Jain6c14a162015-03-10 06:38:34 +0800745 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jaine7e1aa92015-03-10 06:38:21 +0800746 disk_kobj->name);
747 }
748
Anand Jain99994cd2014-06-03 11:36:00 +0800749 return 0;
750}
751
Anand Jain2e7910d2015-03-10 06:38:29 +0800752int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
Anand Jain00c921c2015-03-10 06:38:28 +0800753{
Anand Jain2e7910d2015-03-10 06:38:29 +0800754 if (!fs_devs->device_dir_kobj)
755 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
Anand Jainc1b7e472015-08-14 18:32:50 +0800756 &fs_devs->fsid_kobj);
Anand Jain00c921c2015-03-10 06:38:28 +0800757
Anand Jain2e7910d2015-03-10 06:38:29 +0800758 if (!fs_devs->device_dir_kobj)
Anand Jain00c921c2015-03-10 06:38:28 +0800759 return -ENOMEM;
760
761 return 0;
762}
763
Anand Jaine3bd6972015-08-14 18:32:48 +0800764int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +0800765 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400766{
767 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400768 struct btrfs_device *dev;
769
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400770 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +0800771 struct hd_struct *disk;
772 struct kobject *disk_kobj;
773
774 if (!dev->bdev)
775 continue;
776
Anand Jain0d393762014-06-03 11:36:01 +0800777 if (one_device && one_device != dev)
778 continue;
779
Anand Jainf0853812014-01-15 17:22:28 +0800780 disk = dev->bdev->bd_part;
781 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400782
Anand Jain2e7910d2015-03-10 06:38:29 +0800783 error = sysfs_create_link(fs_devices->device_dir_kobj,
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400784 disk_kobj, disk_kobj->name);
785 if (error)
786 break;
787 }
788
789 return error;
790}
791
Jeff Mahoney510d7362013-11-01 13:06:59 -0400792/* /sys/fs/btrfs/ entry */
793static struct kset *btrfs_kset;
794
David Sterba1bae3092014-02-05 15:36:18 +0100795/* /sys/kernel/debug/btrfs */
796static struct dentry *btrfs_debugfs_root_dentry;
797
798/* Debugging tunables and exported data */
799u64 btrfs_debugfs_test;
800
Anand Jain72059212015-03-10 06:38:26 +0800801/*
802 * Can be called by the device discovery thread.
803 * And parent can be specified for seed device
804 */
Anand Jain0c10e2d2015-03-10 06:38:35 +0800805int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
Anand Jain72059212015-03-10 06:38:26 +0800806 struct kobject *parent)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400807{
808 int error;
809
Anand Jain2e7910d2015-03-10 06:38:29 +0800810 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +0800811 fs_devs->fsid_kobj.kset = btrfs_kset;
812 error = kobject_init_and_add(&fs_devs->fsid_kobj,
Anand Jain24bd69c2015-03-10 06:38:39 +0800813 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
Anand Jain72059212015-03-10 06:38:26 +0800814 return error;
815}
816
Anand Jain96f31362015-08-14 18:32:46 +0800817int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +0800818{
819 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +0800820 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +0800821 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +0800822
Anand Jain5a13f432015-03-10 06:38:31 +0800823 btrfs_set_fs_info_ptr(fs_info);
824
Anand Jaine3bd6972015-08-14 18:32:48 +0800825 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500826 if (error)
827 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400828
Anand Jainc1b7e472015-08-14 18:32:50 +0800829 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500830 if (error) {
Anand Jain32576042015-08-14 18:32:49 +0800831 btrfs_sysfs_rm_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500832 return error;
833 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400834
Anand Jainc1b7e472015-08-14 18:32:50 +0800835 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +0800836 &btrfs_feature_attr_group);
837 if (error)
838 goto failure;
839
Jeff Mahoneye453d982013-11-21 10:37:16 -0500840 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400841 if (error)
842 goto failure;
843
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400844 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +0800845 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400846 if (!fs_info->space_info_kobj) {
847 error = -ENOMEM;
848 goto failure;
849 }
850
851 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
852 if (error)
853 goto failure;
854
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400855 return 0;
856failure:
Anand Jain6618a592015-08-14 18:32:47 +0800857 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400858 return error;
859}
860
David Sterba444e7512016-01-21 18:50:40 +0100861
862/*
863 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
864 * values in superblock. Call after any changes to incompat/compat_ro flags
865 */
866void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
867 u64 bit, enum btrfs_feature_set set)
868{
869 struct btrfs_fs_devices *fs_devs;
870 struct kobject *fsid_kobj;
871 u64 features;
872 int ret;
873
874 if (!fs_info)
875 return;
876
877 features = get_features(fs_info, set);
878 ASSERT(bit & supported_feature_masks[set]);
879
880 fs_devs = fs_info->fs_devices;
881 fsid_kobj = &fs_devs->fsid_kobj;
882
David Sterbabf609202016-01-27 14:06:29 +0100883 if (!fsid_kobj->state_initialized)
884 return;
885
David Sterba444e7512016-01-21 18:50:40 +0100886 /*
887 * FIXME: this is too heavy to update just one value, ideally we'd like
888 * to use sysfs_update_group but some refactoring is needed first.
889 */
890 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
891 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
892}
893
David Sterba1bae3092014-02-05 15:36:18 +0100894static int btrfs_init_debugfs(void)
895{
896#ifdef CONFIG_DEBUG_FS
897 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
898 if (!btrfs_debugfs_root_dentry)
899 return -ENOMEM;
900
David Sterbab0de6c42016-09-01 14:37:53 +0200901 /*
902 * Example code, how to export data through debugfs.
903 *
904 * file: /sys/kernel/debug/btrfs/test
905 * contents of: btrfs_debugfs_test
906 */
907#ifdef CONFIG_BTRFS_DEBUG
Eric Sandeen07f6a482016-08-31 16:49:29 -0500908 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
David Sterba1bae3092014-02-05 15:36:18 +0100909 &btrfs_debugfs_test);
910#endif
David Sterbab0de6c42016-09-01 14:37:53 +0200911
912#endif
David Sterba1bae3092014-02-05 15:36:18 +0100913 return 0;
914}
915
Liu Bof5c29bd2017-11-02 17:21:50 -0600916int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400917{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400918 int ret;
David Sterba1bae3092014-02-05 15:36:18 +0100919
Greg KHe3fe4e72008-02-20 14:14:16 -0500920 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
921 if (!btrfs_kset)
922 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400923
David Sterba1bae3092014-02-05 15:36:18 +0100924 ret = btrfs_init_debugfs();
925 if (ret)
Filipe Manana001a6482015-01-23 18:27:00 +0000926 goto out1;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400927
David Sterba1bae3092014-02-05 15:36:18 +0100928 init_feature_attrs();
929 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +0000930 if (ret)
931 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +0900932 ret = sysfs_merge_group(&btrfs_kset->kobj,
933 &btrfs_static_feature_attr_group);
934 if (ret)
935 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +0000936
937 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +0900938
939out_remove_group:
940 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +0000941out2:
942 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
943out1:
944 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +0100945
946 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -0400947}
948
David Sterbae67c7182018-02-19 17:24:18 +0100949void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400950{
Misono Tomohirof902bd32018-05-17 14:24:51 +0900951 sysfs_unmerge_group(&btrfs_kset->kobj,
952 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400953 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500954 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +0100955 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
Josef Bacik58176a92007-08-29 15:47:34 -0400956}
Chris Mason55d47412008-02-20 16:02:51 -0500957