blob: 9539f8143b7a5c2d433184536a049948fc8bcdb4 [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"
Josef Bacik8719aaa2019-06-18 16:09:16 -040019#include "space-info.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040020
Jeff Mahoney510d7362013-11-01 13:06:59 -040021static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080022static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040023
Jeff Mahoney510d7362013-11-01 13:06:59 -040024static u64 get_features(struct btrfs_fs_info *fs_info,
25 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040026{
Jeff Mahoney510d7362013-11-01 13:06:59 -040027 struct btrfs_super_block *disk_super = fs_info->super_copy;
28 if (set == FEAT_COMPAT)
29 return btrfs_super_compat_flags(disk_super);
30 else if (set == FEAT_COMPAT_RO)
31 return btrfs_super_compat_ro_flags(disk_super);
32 else
33 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040034}
35
Jeff Mahoneyba631942013-11-01 13:07:01 -040036static void set_features(struct btrfs_fs_info *fs_info,
37 enum btrfs_feature_set set, u64 features)
38{
39 struct btrfs_super_block *disk_super = fs_info->super_copy;
40 if (set == FEAT_COMPAT)
41 btrfs_set_super_compat_flags(disk_super, features);
42 else if (set == FEAT_COMPAT_RO)
43 btrfs_set_super_compat_ro_flags(disk_super, features);
44 else
45 btrfs_set_super_incompat_flags(disk_super, features);
46}
47
48static int can_modify_feature(struct btrfs_feature_attr *fa)
49{
50 int val = 0;
51 u64 set, clear;
52 switch (fa->feature_set) {
53 case FEAT_COMPAT:
54 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
55 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
56 break;
57 case FEAT_COMPAT_RO:
58 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
59 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
60 break;
61 case FEAT_INCOMPAT:
62 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
63 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
64 break;
65 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -040066 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +010067 fa->feature_set);
68 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -040069 }
70
71 if (set & fa->feature_bit)
72 val |= 1;
73 if (clear & fa->feature_bit)
74 val |= 2;
75
76 return val;
77}
78
Jeff Mahoney079b72b2013-11-01 13:06:57 -040079static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
80 struct kobj_attribute *a, char *buf)
81{
Jeff Mahoney510d7362013-11-01 13:06:59 -040082 int val = 0;
83 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -040084 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -040085 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -040086 u64 features = get_features(fs_info, fa->feature_set);
87 if (features & fa->feature_bit)
88 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -040089 } else
90 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -040091
92 return snprintf(buf, PAGE_SIZE, "%d\n", val);
93}
94
Jeff Mahoneyba631942013-11-01 13:07:01 -040095static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
96 struct kobj_attribute *a,
97 const char *buf, size_t count)
98{
99 struct btrfs_fs_info *fs_info;
100 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400101 u64 features, set, clear;
102 unsigned long val;
103 int ret;
104
105 fs_info = to_fs_info(kobj);
106 if (!fs_info)
107 return -EPERM;
108
David Howellsbc98a422017-07-17 08:45:34 +0100109 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100110 return -EROFS;
111
Jeff Mahoneyba631942013-11-01 13:07:01 -0400112 ret = kstrtoul(skip_spaces(buf), 0, &val);
113 if (ret)
114 return ret;
115
116 if (fa->feature_set == FEAT_COMPAT) {
117 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
118 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
119 } else if (fa->feature_set == FEAT_COMPAT_RO) {
120 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
121 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
122 } else {
123 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
124 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
125 }
126
127 features = get_features(fs_info, fa->feature_set);
128
129 /* Nothing to do */
130 if ((val && (features & fa->feature_bit)) ||
131 (!val && !(features & fa->feature_bit)))
132 return count;
133
134 if ((val && !(set & fa->feature_bit)) ||
135 (!val && !(clear & fa->feature_bit))) {
136 btrfs_info(fs_info,
137 "%sabling feature %s on mounted fs is not supported.",
138 val ? "En" : "Dis", fa->kobj_attr.attr.name);
139 return -EPERM;
140 }
141
142 btrfs_info(fs_info, "%s %s feature flag",
143 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
144
Jeff Mahoneyba631942013-11-01 13:07:01 -0400145 spin_lock(&fs_info->super_lock);
146 features = get_features(fs_info, fa->feature_set);
147 if (val)
148 features |= fa->feature_bit;
149 else
150 features &= ~fa->feature_bit;
151 set_features(fs_info, fa->feature_set, features);
152 spin_unlock(&fs_info->super_lock);
153
David Sterba0eae2742014-11-12 14:22:21 +0100154 /*
155 * We don't want to do full transaction commit from inside sysfs
156 */
157 btrfs_set_pending(fs_info, COMMIT);
158 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400159
160 return count;
161}
162
Jeff Mahoney510d7362013-11-01 13:06:59 -0400163static umode_t btrfs_feature_visible(struct kobject *kobj,
164 struct attribute *attr, int unused)
165{
166 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
167 umode_t mode = attr->mode;
168
169 if (fs_info) {
170 struct btrfs_feature_attr *fa;
171 u64 features;
172
173 fa = attr_to_btrfs_feature_attr(attr);
174 features = get_features(fs_info, fa->feature_set);
175
Jeff Mahoneyba631942013-11-01 13:07:01 -0400176 if (can_modify_feature(fa))
177 mode |= S_IWUSR;
178 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400179 mode = 0;
180 }
181
182 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400183}
184
185BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
186BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
187BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
188BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700189BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400190BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
191BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
192BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
193BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100194BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200195BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100196BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400197
198static struct attribute *btrfs_supported_feature_attrs[] = {
199 BTRFS_FEAT_ATTR_PTR(mixed_backref),
200 BTRFS_FEAT_ATTR_PTR(default_subvol),
201 BTRFS_FEAT_ATTR_PTR(mixed_groups),
202 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700203 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400204 BTRFS_FEAT_ATTR_PTR(big_metadata),
205 BTRFS_FEAT_ATTR_PTR(extended_iref),
206 BTRFS_FEAT_ATTR_PTR(raid56),
207 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100208 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200209 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100210 BTRFS_FEAT_ATTR_PTR(free_space_tree),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400211 NULL
212};
213
Misono Tomohirof902bd32018-05-17 14:24:51 +0900214/*
215 * Features which depend on feature bits and may differ between each fs.
216 *
217 * /sys/fs/btrfs/features lists all available features of this kernel while
218 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
219 * can be changed online.
220 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400221static const struct attribute_group btrfs_feature_attr_group = {
222 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400223 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400224 .attrs = btrfs_supported_feature_attrs,
225};
Josef Bacik58176a92007-08-29 15:47:34 -0400226
Misono Tomohirof902bd32018-05-17 14:24:51 +0900227static ssize_t rmdir_subvol_show(struct kobject *kobj,
228 struct kobj_attribute *ka, char *buf)
229{
230 return snprintf(buf, PAGE_SIZE, "0\n");
231}
232BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
233
234static struct attribute *btrfs_supported_static_feature_attrs[] = {
235 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
236 NULL
237};
238
239/*
240 * Features which only depend on kernel version.
241 *
242 * These are listed in /sys/fs/btrfs/features along with
243 * btrfs_feature_attr_group
244 */
245static const struct attribute_group btrfs_static_feature_attr_group = {
246 .name = "features",
247 .attrs = btrfs_supported_static_feature_attrs,
248};
249
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400250static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
251{
252 u64 val;
253 if (lock)
254 spin_lock(lock);
255 val = *value_ptr;
256 if (lock)
257 spin_unlock(lock);
258 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
259}
260
261static ssize_t global_rsv_size_show(struct kobject *kobj,
262 struct kobj_attribute *ka, char *buf)
263{
264 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
265 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
266 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
267}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200268BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400269
270static ssize_t global_rsv_reserved_show(struct kobject *kobj,
271 struct kobj_attribute *a, char *buf)
272{
273 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
274 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
275 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
276}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200277BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400278
279#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400280#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400281
282static ssize_t raid_bytes_show(struct kobject *kobj,
283 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200284BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
285BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400286
287static ssize_t raid_bytes_show(struct kobject *kobj,
288 struct kobj_attribute *attr, char *buf)
289
290{
291 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
292 struct btrfs_block_group_cache *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400293 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400294 u64 val = 0;
295
296 down_read(&sinfo->groups_sem);
297 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200298 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400299 val += block_group->key.offset;
300 else
301 val += btrfs_block_group_used(&block_group->item);
302 }
303 up_read(&sinfo->groups_sem);
304 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
305}
306
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400307static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200308 BTRFS_ATTR_PTR(raid, total_bytes),
309 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400310 NULL
311};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400312ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400313
314static void release_raid_kobj(struct kobject *kobj)
315{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400316 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400317}
318
319struct kobj_type btrfs_raid_ktype = {
320 .sysfs_ops = &kobj_sysfs_ops,
321 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400322 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400323};
324
325#define SPACE_INFO_ATTR(field) \
326static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
327 struct kobj_attribute *a, \
328 char *buf) \
329{ \
330 struct btrfs_space_info *sinfo = to_space_info(kobj); \
331 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
332} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200333BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400334
335static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
336 struct kobj_attribute *a,
337 char *buf)
338{
339 struct btrfs_space_info *sinfo = to_space_info(kobj);
340 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
341 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
342}
343
344SPACE_INFO_ATTR(flags);
345SPACE_INFO_ATTR(total_bytes);
346SPACE_INFO_ATTR(bytes_used);
347SPACE_INFO_ATTR(bytes_pinned);
348SPACE_INFO_ATTR(bytes_reserved);
349SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800350SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400351SPACE_INFO_ATTR(disk_used);
352SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200353BTRFS_ATTR(space_info, total_bytes_pinned,
354 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400355
356static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200357 BTRFS_ATTR_PTR(space_info, flags),
358 BTRFS_ATTR_PTR(space_info, total_bytes),
359 BTRFS_ATTR_PTR(space_info, bytes_used),
360 BTRFS_ATTR_PTR(space_info, bytes_pinned),
361 BTRFS_ATTR_PTR(space_info, bytes_reserved),
362 BTRFS_ATTR_PTR(space_info, bytes_may_use),
363 BTRFS_ATTR_PTR(space_info, bytes_readonly),
364 BTRFS_ATTR_PTR(space_info, disk_used),
365 BTRFS_ATTR_PTR(space_info, disk_total),
366 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400367 NULL,
368};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400369ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400370
371static void space_info_release(struct kobject *kobj)
372{
373 struct btrfs_space_info *sinfo = to_space_info(kobj);
374 percpu_counter_destroy(&sinfo->total_bytes_pinned);
375 kfree(sinfo);
376}
377
378struct kobj_type space_info_ktype = {
379 .sysfs_ops = &kobj_sysfs_ops,
380 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400381 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400382};
383
384static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200385 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
386 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400387 NULL,
388};
389
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400390static ssize_t btrfs_label_show(struct kobject *kobj,
391 struct kobj_attribute *a, char *buf)
392{
393 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900394 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200395 ssize_t ret;
396
397 spin_lock(&fs_info->super_lock);
398 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
399 spin_unlock(&fs_info->super_lock);
400
401 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400402}
403
404static ssize_t btrfs_label_store(struct kobject *kobj,
405 struct kobj_attribute *a,
406 const char *buf, size_t len)
407{
408 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900409 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400410
David Sterba66ac9fe2016-04-26 16:03:57 +0200411 if (!fs_info)
412 return -EPERM;
413
David Howellsbc98a422017-07-17 08:45:34 +0100414 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800415 return -EROFS;
416
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900417 /*
418 * p_len is the len until the first occurrence of either
419 * '\n' or '\0'
420 */
421 p_len = strcspn(buf, "\n");
422
423 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400424 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400425
David Sterbaa6f69dc2014-05-30 19:29:05 +0200426 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900427 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
428 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200429 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400430
David Sterbaa6f69dc2014-05-30 19:29:05 +0200431 /*
432 * We don't want to do full transaction commit from inside sysfs
433 */
434 btrfs_set_pending(fs_info, COMMIT);
435 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400436
David Sterbaa6f69dc2014-05-30 19:29:05 +0200437 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400438}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200439BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400440
David Sterbadf935892014-05-07 18:17:16 +0200441static ssize_t btrfs_nodesize_show(struct kobject *kobj,
442 struct kobj_attribute *a, char *buf)
443{
444 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
445
David Sterba093e0372018-03-16 14:31:43 +0100446 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200447}
448
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200449BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200450
451static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
452 struct kobj_attribute *a, char *buf)
453{
454 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
455
David Sterba093e0372018-03-16 14:31:43 +0100456 return snprintf(buf, PAGE_SIZE, "%u\n",
457 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200458}
459
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200460BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200461
462static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
463 struct kobj_attribute *a, char *buf)
464{
465 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
466
David Sterba093e0372018-03-16 14:31:43 +0100467 return snprintf(buf, PAGE_SIZE, "%u\n",
468 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200469}
470
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200471BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200472
Sargun Dhillon27234802017-05-11 21:18:03 +0000473static ssize_t quota_override_show(struct kobject *kobj,
474 struct kobj_attribute *a, char *buf)
475{
476 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
477 int quota_override;
478
479 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
480 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
481}
482
483static ssize_t quota_override_store(struct kobject *kobj,
484 struct kobj_attribute *a,
485 const char *buf, size_t len)
486{
487 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
488 unsigned long knob;
489 int err;
490
491 if (!fs_info)
492 return -EPERM;
493
494 if (!capable(CAP_SYS_RESOURCE))
495 return -EPERM;
496
497 err = kstrtoul(buf, 10, &knob);
498 if (err)
499 return err;
500 if (knob > 1)
501 return -EINVAL;
502
503 if (knob)
504 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
505 else
506 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
507
508 return len;
509}
510
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200511BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000512
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200513static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
514 struct kobj_attribute *a, char *buf)
515{
516 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
517
518 return snprintf(buf, PAGE_SIZE, "%pU\n",
519 fs_info->fs_devices->metadata_uuid);
520}
521
522BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
523
Anand Jain0dd29062015-03-10 06:38:27 +0800524static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200525 BTRFS_ATTR_PTR(, label),
526 BTRFS_ATTR_PTR(, nodesize),
527 BTRFS_ATTR_PTR(, sectorsize),
528 BTRFS_ATTR_PTR(, clone_alignment),
529 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200530 BTRFS_ATTR_PTR(, metadata_uuid),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400531 NULL,
532};
533
Anand Jainc1b7e472015-08-14 18:32:50 +0800534static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400535{
Anand Jain2e7910d2015-03-10 06:38:29 +0800536 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800537
Anand Jainc1b7e472015-08-14 18:32:50 +0800538 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800539 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400540}
541
542static struct kobj_type btrfs_ktype = {
543 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800544 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400545};
546
Anand Jain2e7910d2015-03-10 06:38:29 +0800547static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
548{
549 if (kobj->ktype != &btrfs_ktype)
550 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800551 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800552}
553
Jeff Mahoney510d7362013-11-01 13:06:59 -0400554static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
555{
556 if (kobj->ktype != &btrfs_ktype)
557 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800558 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400559}
Josef Bacik58176a92007-08-29 15:47:34 -0400560
Jeff Mahoneye453d982013-11-21 10:37:16 -0500561#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900562#define BTRFS_FEATURE_NAME_MAX 13
563static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
564static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500565
Tomohiro Misono6c521572018-05-16 17:09:26 +0900566static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500567 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
568 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
569 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
570};
571
572static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400573{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500574 int set;
575
576 for (set = 0; set < FEAT_MAX; set++) {
577 int i;
578 struct attribute *attrs[2];
579 struct attribute_group agroup = {
580 .name = "features",
581 .attrs = attrs,
582 };
583 u64 features = get_features(fs_info, set);
584 features &= ~supported_feature_masks[set];
585
586 if (!features)
587 continue;
588
589 attrs[1] = NULL;
590 for (i = 0; i < NUM_FEATURE_BITS; i++) {
591 struct btrfs_feature_attr *fa;
592
593 if (!(features & (1ULL << i)))
594 continue;
595
596 fa = &btrfs_feature_attrs[set][i];
597 attrs[0] = &fa->kobj_attr.attr;
598 if (add) {
599 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800600 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500601 &agroup);
602 if (ret)
603 return ret;
604 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800605 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500606 &agroup);
607 }
608
609 }
610 return 0;
611}
612
Anand Jain2e3e1282015-03-10 06:38:32 +0800613static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500614{
Anand Jain2e7910d2015-03-10 06:38:29 +0800615 if (fs_devs->device_dir_kobj) {
616 kobject_del(fs_devs->device_dir_kobj);
617 kobject_put(fs_devs->device_dir_kobj);
618 fs_devs->device_dir_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800619 }
620
Anand Jainc1b7e472015-08-14 18:32:50 +0800621 if (fs_devs->fsid_kobj.state_initialized) {
622 kobject_del(&fs_devs->fsid_kobj);
623 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800624 wait_for_completion(&fs_devs->kobj_unregister);
625 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400626}
627
Anand Jain2e3e1282015-03-10 06:38:32 +0800628/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800629void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800630{
631 struct list_head *fs_uuids = btrfs_get_fs_uuids();
632
633 if (fs_devs) {
634 __btrfs_sysfs_remove_fsid(fs_devs);
635 return;
636 }
637
Anand Jainc4babc52018-04-12 10:29:25 +0800638 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800639 __btrfs_sysfs_remove_fsid(fs_devs);
640 }
641}
642
Anand Jain6618a592015-08-14 18:32:47 +0800643void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500644{
Anand Jain5a13f432015-03-10 06:38:31 +0800645 btrfs_reset_fs_info_ptr(fs_info);
646
Jeff Mahoneye453d982013-11-21 10:37:16 -0500647 if (fs_info->space_info_kobj) {
648 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
649 kobject_del(fs_info->space_info_kobj);
650 kobject_put(fs_info->space_info_kobj);
651 }
Jeff Mahoneye453d982013-11-21 10:37:16 -0500652 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800653 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
654 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jain32576042015-08-14 18:32:49 +0800655 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500656}
657
Tomohiro Misono6c521572018-05-16 17:09:26 +0900658const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400659 [FEAT_COMPAT] = "compat",
660 [FEAT_COMPAT_RO] = "compat_ro",
661 [FEAT_INCOMPAT] = "incompat",
662};
663
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400664char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
665{
666 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
667 int len = 0;
668 int i;
669 char *str;
670
671 str = kmalloc(bufsize, GFP_KERNEL);
672 if (!str)
673 return str;
674
675 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
676 const char *name;
677
678 if (!(flags & (1ULL << i)))
679 continue;
680
681 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
682 len += snprintf(str + len, bufsize - len, "%s%s",
683 len ? "," : "", name);
684 }
685
686 return str;
687}
688
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400689static void init_feature_attrs(void)
690{
691 struct btrfs_feature_attr *fa;
692 int set, i;
693
694 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
695 ARRAY_SIZE(btrfs_feature_attrs));
696 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
697 ARRAY_SIZE(btrfs_feature_attrs[0]));
698
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400699 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
700 memset(btrfs_unknown_feature_names, 0,
701 sizeof(btrfs_unknown_feature_names));
702
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400703 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
704 struct btrfs_feature_attr *sfa;
705 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400706 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400707 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400708 bit = ilog2(sfa->feature_bit);
709 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400710
711 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
712 }
713
714 for (set = 0; set < FEAT_MAX; set++) {
715 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
716 char *name = btrfs_unknown_feature_names[set][i];
717 fa = &btrfs_feature_attrs[set][i];
718
719 if (fa->kobj_attr.attr.name)
720 continue;
721
Tomohiro Misono6c521572018-05-16 17:09:26 +0900722 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400723 btrfs_feature_set_names[set], i);
724
725 fa->kobj_attr.attr.name = name;
726 fa->kobj_attr.attr.mode = S_IRUGO;
727 fa->feature_set = set;
728 fa->feature_bit = 1ULL << i;
729 }
730 }
731}
732
Anand Jaine7e1aa92015-03-10 06:38:21 +0800733/* when one_device is NULL, it removes all device links */
734
Anand Jain32576042015-08-14 18:32:49 +0800735int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +0800736 struct btrfs_device *one_device)
737{
738 struct hd_struct *disk;
739 struct kobject *disk_kobj;
740
Anand Jain6c14a162015-03-10 06:38:34 +0800741 if (!fs_devices->device_dir_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +0800742 return -EINVAL;
743
Liu Bo87fa3bb2014-07-29 19:09:39 +0800744 if (one_device && one_device->bdev) {
Anand Jain99994cd2014-06-03 11:36:00 +0800745 disk = one_device->bdev->bd_part;
746 disk_kobj = &part_to_dev(disk)->kobj;
747
Anand Jain6c14a162015-03-10 06:38:34 +0800748 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jain99994cd2014-06-03 11:36:00 +0800749 disk_kobj->name);
750 }
751
Anand Jaine7e1aa92015-03-10 06:38:21 +0800752 if (one_device)
753 return 0;
754
755 list_for_each_entry(one_device,
Anand Jain6c14a162015-03-10 06:38:34 +0800756 &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +0800757 if (!one_device->bdev)
758 continue;
759 disk = one_device->bdev->bd_part;
760 disk_kobj = &part_to_dev(disk)->kobj;
761
Anand Jain6c14a162015-03-10 06:38:34 +0800762 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jaine7e1aa92015-03-10 06:38:21 +0800763 disk_kobj->name);
764 }
765
Anand Jain99994cd2014-06-03 11:36:00 +0800766 return 0;
767}
768
Anand Jain2e7910d2015-03-10 06:38:29 +0800769int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
Anand Jain00c921c2015-03-10 06:38:28 +0800770{
Anand Jain2e7910d2015-03-10 06:38:29 +0800771 if (!fs_devs->device_dir_kobj)
772 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
Anand Jainc1b7e472015-08-14 18:32:50 +0800773 &fs_devs->fsid_kobj);
Anand Jain00c921c2015-03-10 06:38:28 +0800774
Anand Jain2e7910d2015-03-10 06:38:29 +0800775 if (!fs_devs->device_dir_kobj)
Anand Jain00c921c2015-03-10 06:38:28 +0800776 return -ENOMEM;
777
778 return 0;
779}
780
Anand Jaine3bd6972015-08-14 18:32:48 +0800781int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +0800782 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400783{
784 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400785 struct btrfs_device *dev;
786
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400787 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +0800788 struct hd_struct *disk;
789 struct kobject *disk_kobj;
790
791 if (!dev->bdev)
792 continue;
793
Anand Jain0d393762014-06-03 11:36:01 +0800794 if (one_device && one_device != dev)
795 continue;
796
Anand Jainf0853812014-01-15 17:22:28 +0800797 disk = dev->bdev->bd_part;
798 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400799
Anand Jain2e7910d2015-03-10 06:38:29 +0800800 error = sysfs_create_link(fs_devices->device_dir_kobj,
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400801 disk_kobj, disk_kobj->name);
802 if (error)
803 break;
804 }
805
806 return error;
807}
808
Jeff Mahoney510d7362013-11-01 13:06:59 -0400809/* /sys/fs/btrfs/ entry */
810static struct kset *btrfs_kset;
811
David Sterba1bae3092014-02-05 15:36:18 +0100812/* /sys/kernel/debug/btrfs */
813static struct dentry *btrfs_debugfs_root_dentry;
814
815/* Debugging tunables and exported data */
816u64 btrfs_debugfs_test;
817
Anand Jain72059212015-03-10 06:38:26 +0800818/*
819 * Can be called by the device discovery thread.
820 * And parent can be specified for seed device
821 */
Anand Jain0c10e2d2015-03-10 06:38:35 +0800822int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
Anand Jain72059212015-03-10 06:38:26 +0800823 struct kobject *parent)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400824{
825 int error;
826
Anand Jain2e7910d2015-03-10 06:38:29 +0800827 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +0800828 fs_devs->fsid_kobj.kset = btrfs_kset;
829 error = kobject_init_and_add(&fs_devs->fsid_kobj,
Anand Jain24bd69c2015-03-10 06:38:39 +0800830 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +1000831 if (error) {
832 kobject_put(&fs_devs->fsid_kobj);
833 return error;
834 }
835
836 return 0;
Anand Jain72059212015-03-10 06:38:26 +0800837}
838
Anand Jain96f31362015-08-14 18:32:46 +0800839int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +0800840{
841 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +0800842 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +0800843 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +0800844
Anand Jain5a13f432015-03-10 06:38:31 +0800845 btrfs_set_fs_info_ptr(fs_info);
846
Anand Jaine3bd6972015-08-14 18:32:48 +0800847 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500848 if (error)
849 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400850
Anand Jainc1b7e472015-08-14 18:32:50 +0800851 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500852 if (error) {
Anand Jain32576042015-08-14 18:32:49 +0800853 btrfs_sysfs_rm_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500854 return error;
855 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400856
Anand Jainc1b7e472015-08-14 18:32:50 +0800857 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +0800858 &btrfs_feature_attr_group);
859 if (error)
860 goto failure;
861
Jeff Mahoneye453d982013-11-21 10:37:16 -0500862 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400863 if (error)
864 goto failure;
865
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400866 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +0800867 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400868 if (!fs_info->space_info_kobj) {
869 error = -ENOMEM;
870 goto failure;
871 }
872
873 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
874 if (error)
875 goto failure;
876
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400877 return 0;
878failure:
Anand Jain6618a592015-08-14 18:32:47 +0800879 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400880 return error;
881}
882
David Sterba444e7512016-01-21 18:50:40 +0100883
884/*
885 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
886 * values in superblock. Call after any changes to incompat/compat_ro flags
887 */
888void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
889 u64 bit, enum btrfs_feature_set set)
890{
891 struct btrfs_fs_devices *fs_devs;
892 struct kobject *fsid_kobj;
893 u64 features;
894 int ret;
895
896 if (!fs_info)
897 return;
898
899 features = get_features(fs_info, set);
900 ASSERT(bit & supported_feature_masks[set]);
901
902 fs_devs = fs_info->fs_devices;
903 fsid_kobj = &fs_devs->fsid_kobj;
904
David Sterbabf609202016-01-27 14:06:29 +0100905 if (!fsid_kobj->state_initialized)
906 return;
907
David Sterba444e7512016-01-21 18:50:40 +0100908 /*
909 * FIXME: this is too heavy to update just one value, ideally we'd like
910 * to use sysfs_update_group but some refactoring is needed first.
911 */
912 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
913 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
914}
915
Greg Kroah-Hartman36b7ee42019-01-04 14:25:20 +0100916static void btrfs_init_debugfs(void)
David Sterba1bae3092014-02-05 15:36:18 +0100917{
918#ifdef CONFIG_DEBUG_FS
919 btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
David Sterba1bae3092014-02-05 15:36:18 +0100920
David Sterbab0de6c42016-09-01 14:37:53 +0200921 /*
922 * Example code, how to export data through debugfs.
923 *
924 * file: /sys/kernel/debug/btrfs/test
925 * contents of: btrfs_debugfs_test
926 */
927#ifdef CONFIG_BTRFS_DEBUG
Eric Sandeen07f6a482016-08-31 16:49:29 -0500928 debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
David Sterba1bae3092014-02-05 15:36:18 +0100929 &btrfs_debugfs_test);
930#endif
David Sterbab0de6c42016-09-01 14:37:53 +0200931
932#endif
David Sterba1bae3092014-02-05 15:36:18 +0100933}
934
Liu Bof5c29bd2017-11-02 17:21:50 -0600935int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400936{
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400937 int ret;
David Sterba1bae3092014-02-05 15:36:18 +0100938
Greg KHe3fe4e72008-02-20 14:14:16 -0500939 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
940 if (!btrfs_kset)
941 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400942
Greg Kroah-Hartman36b7ee42019-01-04 14:25:20 +0100943 btrfs_init_debugfs();
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400944
David Sterba1bae3092014-02-05 15:36:18 +0100945 init_feature_attrs();
946 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +0000947 if (ret)
948 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +0900949 ret = sysfs_merge_group(&btrfs_kset->kobj,
950 &btrfs_static_feature_attr_group);
951 if (ret)
952 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +0000953
954 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +0900955
956out_remove_group:
957 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +0000958out2:
959 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
Filipe Manana001a6482015-01-23 18:27:00 +0000960 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +0100961
962 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -0400963}
964
David Sterbae67c7182018-02-19 17:24:18 +0100965void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -0400966{
Misono Tomohirof902bd32018-05-17 14:24:51 +0900967 sysfs_unmerge_group(&btrfs_kset->kobj,
968 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400969 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -0500970 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +0100971 debugfs_remove_recursive(btrfs_debugfs_root_dentry);
Josef Bacik58176a92007-08-29 15:47:34 -0400972}
Chris Mason55d47412008-02-20 16:02:51 -0500973