blob: 89781d94164518eaa897c3ed19f8fd7e4eb9e821 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Josef Bacik58176a92007-08-29 15:47:34 -04006#include <linux/sched.h>
David Sterba32a99912019-08-01 17:49:55 +02007#include <linux/sched/mm.h>
Josef Bacik58176a92007-08-29 15:47:34 -04008#include <linux/slab.h>
9#include <linux/spinlock.h>
10#include <linux/completion.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040011#include <linux/bug.h>
Josef Bacik58176a92007-08-29 15:47:34 -040012
Chris Masonbae45de2007-04-04 21:22:22 -040013#include "ctree.h"
14#include "disk-io.h"
15#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040016#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040017#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040018#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040019#include "block-group.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
David Sterba8f523162019-08-02 13:07:38 +020024static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
25{
26 return container_of(a, struct btrfs_feature_attr, kobj_attr);
27}
28
29static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
30{
31 return container_of(attr, struct kobj_attribute, attr);
32}
33
34static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
35 struct attribute *attr)
36{
37 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
38}
39
Jeff Mahoney510d7362013-11-01 13:06:59 -040040static u64 get_features(struct btrfs_fs_info *fs_info,
41 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040042{
Jeff Mahoney510d7362013-11-01 13:06:59 -040043 struct btrfs_super_block *disk_super = fs_info->super_copy;
44 if (set == FEAT_COMPAT)
45 return btrfs_super_compat_flags(disk_super);
46 else if (set == FEAT_COMPAT_RO)
47 return btrfs_super_compat_ro_flags(disk_super);
48 else
49 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040050}
51
Jeff Mahoneyba631942013-11-01 13:07:01 -040052static void set_features(struct btrfs_fs_info *fs_info,
53 enum btrfs_feature_set set, u64 features)
54{
55 struct btrfs_super_block *disk_super = fs_info->super_copy;
56 if (set == FEAT_COMPAT)
57 btrfs_set_super_compat_flags(disk_super, features);
58 else if (set == FEAT_COMPAT_RO)
59 btrfs_set_super_compat_ro_flags(disk_super, features);
60 else
61 btrfs_set_super_incompat_flags(disk_super, features);
62}
63
64static int can_modify_feature(struct btrfs_feature_attr *fa)
65{
66 int val = 0;
67 u64 set, clear;
68 switch (fa->feature_set) {
69 case FEAT_COMPAT:
70 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
71 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
72 break;
73 case FEAT_COMPAT_RO:
74 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
75 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
76 break;
77 case FEAT_INCOMPAT:
78 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
79 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
80 break;
81 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -040082 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +010083 fa->feature_set);
84 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -040085 }
86
87 if (set & fa->feature_bit)
88 val |= 1;
89 if (clear & fa->feature_bit)
90 val |= 2;
91
92 return val;
93}
94
Jeff Mahoney079b72b2013-11-01 13:06:57 -040095static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
96 struct kobj_attribute *a, char *buf)
97{
Jeff Mahoney510d7362013-11-01 13:06:59 -040098 int val = 0;
99 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400100 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400101 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400102 u64 features = get_features(fs_info, fa->feature_set);
103 if (features & fa->feature_bit)
104 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400105 } else
106 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400107
108 return snprintf(buf, PAGE_SIZE, "%d\n", val);
109}
110
Jeff Mahoneyba631942013-11-01 13:07:01 -0400111static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
112 struct kobj_attribute *a,
113 const char *buf, size_t count)
114{
115 struct btrfs_fs_info *fs_info;
116 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400117 u64 features, set, clear;
118 unsigned long val;
119 int ret;
120
121 fs_info = to_fs_info(kobj);
122 if (!fs_info)
123 return -EPERM;
124
David Howellsbc98a422017-07-17 08:45:34 +0100125 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100126 return -EROFS;
127
Jeff Mahoneyba631942013-11-01 13:07:01 -0400128 ret = kstrtoul(skip_spaces(buf), 0, &val);
129 if (ret)
130 return ret;
131
132 if (fa->feature_set == FEAT_COMPAT) {
133 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
134 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
135 } else if (fa->feature_set == FEAT_COMPAT_RO) {
136 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
137 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
138 } else {
139 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
140 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
141 }
142
143 features = get_features(fs_info, fa->feature_set);
144
145 /* Nothing to do */
146 if ((val && (features & fa->feature_bit)) ||
147 (!val && !(features & fa->feature_bit)))
148 return count;
149
150 if ((val && !(set & fa->feature_bit)) ||
151 (!val && !(clear & fa->feature_bit))) {
152 btrfs_info(fs_info,
153 "%sabling feature %s on mounted fs is not supported.",
154 val ? "En" : "Dis", fa->kobj_attr.attr.name);
155 return -EPERM;
156 }
157
158 btrfs_info(fs_info, "%s %s feature flag",
159 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
160
Jeff Mahoneyba631942013-11-01 13:07:01 -0400161 spin_lock(&fs_info->super_lock);
162 features = get_features(fs_info, fa->feature_set);
163 if (val)
164 features |= fa->feature_bit;
165 else
166 features &= ~fa->feature_bit;
167 set_features(fs_info, fa->feature_set, features);
168 spin_unlock(&fs_info->super_lock);
169
David Sterba0eae2742014-11-12 14:22:21 +0100170 /*
171 * We don't want to do full transaction commit from inside sysfs
172 */
173 btrfs_set_pending(fs_info, COMMIT);
174 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400175
176 return count;
177}
178
Jeff Mahoney510d7362013-11-01 13:06:59 -0400179static umode_t btrfs_feature_visible(struct kobject *kobj,
180 struct attribute *attr, int unused)
181{
182 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
183 umode_t mode = attr->mode;
184
185 if (fs_info) {
186 struct btrfs_feature_attr *fa;
187 u64 features;
188
189 fa = attr_to_btrfs_feature_attr(attr);
190 features = get_features(fs_info, fa->feature_set);
191
Jeff Mahoneyba631942013-11-01 13:07:01 -0400192 if (can_modify_feature(fa))
193 mode |= S_IWUSR;
194 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400195 mode = 0;
196 }
197
198 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400199}
200
201BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
202BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
203BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
204BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700205BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400206BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
207BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
208BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
209BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100210BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200211BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100212BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400213
214static struct attribute *btrfs_supported_feature_attrs[] = {
215 BTRFS_FEAT_ATTR_PTR(mixed_backref),
216 BTRFS_FEAT_ATTR_PTR(default_subvol),
217 BTRFS_FEAT_ATTR_PTR(mixed_groups),
218 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700219 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400220 BTRFS_FEAT_ATTR_PTR(big_metadata),
221 BTRFS_FEAT_ATTR_PTR(extended_iref),
222 BTRFS_FEAT_ATTR_PTR(raid56),
223 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100224 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200225 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100226 BTRFS_FEAT_ATTR_PTR(free_space_tree),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400227 NULL
228};
229
Misono Tomohirof902bd32018-05-17 14:24:51 +0900230/*
231 * Features which depend on feature bits and may differ between each fs.
232 *
233 * /sys/fs/btrfs/features lists all available features of this kernel while
234 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
235 * can be changed online.
236 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400237static const struct attribute_group btrfs_feature_attr_group = {
238 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400239 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400240 .attrs = btrfs_supported_feature_attrs,
241};
Josef Bacik58176a92007-08-29 15:47:34 -0400242
Misono Tomohirof902bd32018-05-17 14:24:51 +0900243static ssize_t rmdir_subvol_show(struct kobject *kobj,
244 struct kobj_attribute *ka, char *buf)
245{
246 return snprintf(buf, PAGE_SIZE, "0\n");
247}
248BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
249
250static struct attribute *btrfs_supported_static_feature_attrs[] = {
251 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
252 NULL
253};
254
255/*
256 * Features which only depend on kernel version.
257 *
258 * These are listed in /sys/fs/btrfs/features along with
259 * btrfs_feature_attr_group
260 */
261static const struct attribute_group btrfs_static_feature_attr_group = {
262 .name = "features",
263 .attrs = btrfs_supported_static_feature_attrs,
264};
265
David Sterba6e369fe2019-06-13 17:23:02 +0200266#ifdef CONFIG_BTRFS_DEBUG
267
268/*
269 * Runtime debugging exported via sysfs
270 *
271 * /sys/fs/btrfs/debug - applies to module or all filesystems
272 * /sys/fs/btrfs/UUID - applies only to the given filesystem
273 */
274static struct attribute *btrfs_debug_feature_attrs[] = {
275 NULL
276};
277
278static const struct attribute_group btrfs_debug_feature_attr_group = {
279 .name = "debug",
280 .attrs = btrfs_debug_feature_attrs,
281};
282
283#endif
284
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400285static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
286{
287 u64 val;
288 if (lock)
289 spin_lock(lock);
290 val = *value_ptr;
291 if (lock)
292 spin_unlock(lock);
293 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
294}
295
296static ssize_t global_rsv_size_show(struct kobject *kobj,
297 struct kobj_attribute *ka, char *buf)
298{
299 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
300 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
301 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
302}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200303BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400304
305static ssize_t global_rsv_reserved_show(struct kobject *kobj,
306 struct kobj_attribute *a, char *buf)
307{
308 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
309 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
310 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
311}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200312BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400313
314#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400315#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400316
317static ssize_t raid_bytes_show(struct kobject *kobj,
318 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200319BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
320BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400321
322static ssize_t raid_bytes_show(struct kobject *kobj,
323 struct kobj_attribute *attr, char *buf)
324
325{
326 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
327 struct btrfs_block_group_cache *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400328 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400329 u64 val = 0;
330
331 down_read(&sinfo->groups_sem);
332 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200333 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400334 val += block_group->key.offset;
335 else
336 val += btrfs_block_group_used(&block_group->item);
337 }
338 up_read(&sinfo->groups_sem);
339 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
340}
341
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400342static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200343 BTRFS_ATTR_PTR(raid, total_bytes),
344 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400345 NULL
346};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400347ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400348
349static void release_raid_kobj(struct kobject *kobj)
350{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400351 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400352}
353
David Sterba536ea452019-08-01 17:55:55 +0200354static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400355 .sysfs_ops = &kobj_sysfs_ops,
356 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400357 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400358};
359
360#define SPACE_INFO_ATTR(field) \
361static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
362 struct kobj_attribute *a, \
363 char *buf) \
364{ \
365 struct btrfs_space_info *sinfo = to_space_info(kobj); \
366 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
367} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200368BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400369
370static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
371 struct kobj_attribute *a,
372 char *buf)
373{
374 struct btrfs_space_info *sinfo = to_space_info(kobj);
375 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
376 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
377}
378
379SPACE_INFO_ATTR(flags);
380SPACE_INFO_ATTR(total_bytes);
381SPACE_INFO_ATTR(bytes_used);
382SPACE_INFO_ATTR(bytes_pinned);
383SPACE_INFO_ATTR(bytes_reserved);
384SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800385SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400386SPACE_INFO_ATTR(disk_used);
387SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200388BTRFS_ATTR(space_info, total_bytes_pinned,
389 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400390
391static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200392 BTRFS_ATTR_PTR(space_info, flags),
393 BTRFS_ATTR_PTR(space_info, total_bytes),
394 BTRFS_ATTR_PTR(space_info, bytes_used),
395 BTRFS_ATTR_PTR(space_info, bytes_pinned),
396 BTRFS_ATTR_PTR(space_info, bytes_reserved),
397 BTRFS_ATTR_PTR(space_info, bytes_may_use),
398 BTRFS_ATTR_PTR(space_info, bytes_readonly),
399 BTRFS_ATTR_PTR(space_info, disk_used),
400 BTRFS_ATTR_PTR(space_info, disk_total),
401 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400402 NULL,
403};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400404ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400405
406static void space_info_release(struct kobject *kobj)
407{
408 struct btrfs_space_info *sinfo = to_space_info(kobj);
409 percpu_counter_destroy(&sinfo->total_bytes_pinned);
410 kfree(sinfo);
411}
412
David Sterba27992d02019-08-01 17:55:55 +0200413static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400414 .sysfs_ops = &kobj_sysfs_ops,
415 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400416 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400417};
418
419static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200420 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
421 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400422 NULL,
423};
424
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400425static ssize_t btrfs_label_show(struct kobject *kobj,
426 struct kobj_attribute *a, char *buf)
427{
428 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900429 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200430 ssize_t ret;
431
432 spin_lock(&fs_info->super_lock);
433 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
434 spin_unlock(&fs_info->super_lock);
435
436 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400437}
438
439static ssize_t btrfs_label_store(struct kobject *kobj,
440 struct kobj_attribute *a,
441 const char *buf, size_t len)
442{
443 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900444 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400445
David Sterba66ac9fe2016-04-26 16:03:57 +0200446 if (!fs_info)
447 return -EPERM;
448
David Howellsbc98a422017-07-17 08:45:34 +0100449 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800450 return -EROFS;
451
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900452 /*
453 * p_len is the len until the first occurrence of either
454 * '\n' or '\0'
455 */
456 p_len = strcspn(buf, "\n");
457
458 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400459 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400460
David Sterbaa6f69dc2014-05-30 19:29:05 +0200461 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900462 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
463 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200464 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400465
David Sterbaa6f69dc2014-05-30 19:29:05 +0200466 /*
467 * We don't want to do full transaction commit from inside sysfs
468 */
469 btrfs_set_pending(fs_info, COMMIT);
470 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400471
David Sterbaa6f69dc2014-05-30 19:29:05 +0200472 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400473}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200474BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400475
David Sterbadf935892014-05-07 18:17:16 +0200476static ssize_t btrfs_nodesize_show(struct kobject *kobj,
477 struct kobj_attribute *a, char *buf)
478{
479 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
480
David Sterba093e0372018-03-16 14:31:43 +0100481 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200482}
483
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200484BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200485
486static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
487 struct kobj_attribute *a, char *buf)
488{
489 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
490
David Sterba093e0372018-03-16 14:31:43 +0100491 return snprintf(buf, PAGE_SIZE, "%u\n",
492 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200493}
494
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200495BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200496
497static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
498 struct kobj_attribute *a, char *buf)
499{
500 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
501
David Sterba093e0372018-03-16 14:31:43 +0100502 return snprintf(buf, PAGE_SIZE, "%u\n",
503 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200504}
505
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200506BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200507
Sargun Dhillon27234802017-05-11 21:18:03 +0000508static ssize_t quota_override_show(struct kobject *kobj,
509 struct kobj_attribute *a, char *buf)
510{
511 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
512 int quota_override;
513
514 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
515 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
516}
517
518static ssize_t quota_override_store(struct kobject *kobj,
519 struct kobj_attribute *a,
520 const char *buf, size_t len)
521{
522 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
523 unsigned long knob;
524 int err;
525
526 if (!fs_info)
527 return -EPERM;
528
529 if (!capable(CAP_SYS_RESOURCE))
530 return -EPERM;
531
532 err = kstrtoul(buf, 10, &knob);
533 if (err)
534 return err;
535 if (knob > 1)
536 return -EINVAL;
537
538 if (knob)
539 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
540 else
541 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
542
543 return len;
544}
545
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200546BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000547
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200548static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
549 struct kobj_attribute *a, char *buf)
550{
551 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
552
553 return snprintf(buf, PAGE_SIZE, "%pU\n",
554 fs_info->fs_devices->metadata_uuid);
555}
556
557BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
558
Anand Jain0dd29062015-03-10 06:38:27 +0800559static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200560 BTRFS_ATTR_PTR(, label),
561 BTRFS_ATTR_PTR(, nodesize),
562 BTRFS_ATTR_PTR(, sectorsize),
563 BTRFS_ATTR_PTR(, clone_alignment),
564 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200565 BTRFS_ATTR_PTR(, metadata_uuid),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400566 NULL,
567};
568
Anand Jainc1b7e472015-08-14 18:32:50 +0800569static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400570{
Anand Jain2e7910d2015-03-10 06:38:29 +0800571 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800572
Anand Jainc1b7e472015-08-14 18:32:50 +0800573 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800574 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400575}
576
577static struct kobj_type btrfs_ktype = {
578 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800579 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400580};
581
Anand Jain2e7910d2015-03-10 06:38:29 +0800582static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
583{
584 if (kobj->ktype != &btrfs_ktype)
585 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800586 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800587}
588
Jeff Mahoney510d7362013-11-01 13:06:59 -0400589static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
590{
591 if (kobj->ktype != &btrfs_ktype)
592 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800593 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400594}
Josef Bacik58176a92007-08-29 15:47:34 -0400595
Jeff Mahoneye453d982013-11-21 10:37:16 -0500596#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900597#define BTRFS_FEATURE_NAME_MAX 13
598static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
599static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500600
Tomohiro Misono6c521572018-05-16 17:09:26 +0900601static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500602 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
603 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
604 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
605};
606
607static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400608{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500609 int set;
610
611 for (set = 0; set < FEAT_MAX; set++) {
612 int i;
613 struct attribute *attrs[2];
614 struct attribute_group agroup = {
615 .name = "features",
616 .attrs = attrs,
617 };
618 u64 features = get_features(fs_info, set);
619 features &= ~supported_feature_masks[set];
620
621 if (!features)
622 continue;
623
624 attrs[1] = NULL;
625 for (i = 0; i < NUM_FEATURE_BITS; i++) {
626 struct btrfs_feature_attr *fa;
627
628 if (!(features & (1ULL << i)))
629 continue;
630
631 fa = &btrfs_feature_attrs[set][i];
632 attrs[0] = &fa->kobj_attr.attr;
633 if (add) {
634 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800635 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500636 &agroup);
637 if (ret)
638 return ret;
639 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800640 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500641 &agroup);
642 }
643
644 }
645 return 0;
646}
647
Anand Jain2e3e1282015-03-10 06:38:32 +0800648static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500649{
Anand Jain2e7910d2015-03-10 06:38:29 +0800650 if (fs_devs->device_dir_kobj) {
651 kobject_del(fs_devs->device_dir_kobj);
652 kobject_put(fs_devs->device_dir_kobj);
653 fs_devs->device_dir_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800654 }
655
Anand Jainc1b7e472015-08-14 18:32:50 +0800656 if (fs_devs->fsid_kobj.state_initialized) {
657 kobject_del(&fs_devs->fsid_kobj);
658 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800659 wait_for_completion(&fs_devs->kobj_unregister);
660 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400661}
662
Anand Jain2e3e1282015-03-10 06:38:32 +0800663/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800664void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800665{
666 struct list_head *fs_uuids = btrfs_get_fs_uuids();
667
668 if (fs_devs) {
669 __btrfs_sysfs_remove_fsid(fs_devs);
670 return;
671 }
672
Anand Jainc4babc52018-04-12 10:29:25 +0800673 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800674 __btrfs_sysfs_remove_fsid(fs_devs);
675 }
676}
677
Anand Jain6618a592015-08-14 18:32:47 +0800678void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500679{
Anand Jain5a13f432015-03-10 06:38:31 +0800680 btrfs_reset_fs_info_ptr(fs_info);
681
Jeff Mahoneye453d982013-11-21 10:37:16 -0500682 if (fs_info->space_info_kobj) {
683 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
684 kobject_del(fs_info->space_info_kobj);
685 kobject_put(fs_info->space_info_kobj);
686 }
Jeff Mahoneye453d982013-11-21 10:37:16 -0500687 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800688 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
689 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jain32576042015-08-14 18:32:49 +0800690 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500691}
692
David Sterbaf10152b2019-08-01 19:07:55 +0200693static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400694 [FEAT_COMPAT] = "compat",
695 [FEAT_COMPAT_RO] = "compat_ro",
696 [FEAT_INCOMPAT] = "incompat",
697};
698
David Sterbaf10152b2019-08-01 19:07:55 +0200699const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
700{
701 return btrfs_feature_set_names[set];
702}
703
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400704char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
705{
706 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
707 int len = 0;
708 int i;
709 char *str;
710
711 str = kmalloc(bufsize, GFP_KERNEL);
712 if (!str)
713 return str;
714
715 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
716 const char *name;
717
718 if (!(flags & (1ULL << i)))
719 continue;
720
721 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
722 len += snprintf(str + len, bufsize - len, "%s%s",
723 len ? "," : "", name);
724 }
725
726 return str;
727}
728
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400729static void init_feature_attrs(void)
730{
731 struct btrfs_feature_attr *fa;
732 int set, i;
733
734 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
735 ARRAY_SIZE(btrfs_feature_attrs));
736 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
737 ARRAY_SIZE(btrfs_feature_attrs[0]));
738
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400739 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
740 memset(btrfs_unknown_feature_names, 0,
741 sizeof(btrfs_unknown_feature_names));
742
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400743 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
744 struct btrfs_feature_attr *sfa;
745 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400746 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400747 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400748 bit = ilog2(sfa->feature_bit);
749 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400750
751 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
752 }
753
754 for (set = 0; set < FEAT_MAX; set++) {
755 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
756 char *name = btrfs_unknown_feature_names[set][i];
757 fa = &btrfs_feature_attrs[set][i];
758
759 if (fa->kobj_attr.attr.name)
760 continue;
761
Tomohiro Misono6c521572018-05-16 17:09:26 +0900762 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400763 btrfs_feature_set_names[set], i);
764
765 fa->kobj_attr.attr.name = name;
766 fa->kobj_attr.attr.mode = S_IRUGO;
767 fa->feature_set = set;
768 fa->feature_bit = 1ULL << i;
769 }
770 }
771}
772
David Sterba32a99912019-08-01 17:49:55 +0200773/*
774 * Create a sysfs entry for a given block group type at path
775 * /sys/fs/btrfs/UUID/allocation/data/TYPE
776 */
777void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
778{
779 struct btrfs_fs_info *fs_info = cache->fs_info;
780 struct btrfs_space_info *space_info = cache->space_info;
781 struct raid_kobject *rkobj;
782 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
783 unsigned int nofs_flag;
784 int ret;
785
786 /*
787 * Setup a NOFS context because kobject_add(), deep in its call chain,
788 * does GFP_KERNEL allocations, and we are often called in a context
789 * where if reclaim is triggered we can deadlock (we are either holding
790 * a transaction handle or some lock required for a transaction
791 * commit).
792 */
793 nofs_flag = memalloc_nofs_save();
794
795 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
796 if (!rkobj) {
797 memalloc_nofs_restore(nofs_flag);
798 btrfs_warn(cache->fs_info,
799 "couldn't alloc memory for raid level kobject");
800 return;
801 }
802
803 rkobj->flags = cache->flags;
804 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
805 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
806 btrfs_bg_type_to_raid_name(rkobj->flags));
807 memalloc_nofs_restore(nofs_flag);
808 if (ret) {
809 kobject_put(&rkobj->kobj);
810 btrfs_warn(fs_info,
811 "failed to add kobject for block cache, ignoring");
812 return;
813 }
814
815 space_info->block_group_kobjs[index] = &rkobj->kobj;
816}
817
David Sterbab5865ba2019-08-01 18:50:16 +0200818/*
819 * Remove sysfs directories for all block group types of a given space info and
820 * the space info as well
821 */
822void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
823{
824 int i;
825
826 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
827 struct kobject *kobj;
828
829 kobj = space_info->block_group_kobjs[i];
830 space_info->block_group_kobjs[i] = NULL;
831 if (kobj) {
832 kobject_del(kobj);
833 kobject_put(kobj);
834 }
835 }
836 kobject_del(&space_info->kobj);
837 kobject_put(&space_info->kobj);
838}
839
David Sterbab8823272019-08-01 18:50:16 +0200840static const char *alloc_name(u64 flags)
841{
842 switch (flags) {
843 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
844 return "mixed";
845 case BTRFS_BLOCK_GROUP_METADATA:
846 return "metadata";
847 case BTRFS_BLOCK_GROUP_DATA:
848 return "data";
849 case BTRFS_BLOCK_GROUP_SYSTEM:
850 return "system";
851 default:
852 WARN_ON(1);
853 return "invalid-combination";
854 };
855}
856
857/*
858 * Create a sysfs entry for a space info type at path
859 * /sys/fs/btrfs/UUID/allocation/TYPE
860 */
861int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
862 struct btrfs_space_info *space_info)
863{
864 int ret;
865
866 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
867 fs_info->space_info_kobj, "%s",
868 alloc_name(space_info->flags));
869 if (ret) {
870 kobject_put(&space_info->kobj);
871 return ret;
872 }
873
874 return 0;
875}
876
Anand Jaine7e1aa92015-03-10 06:38:21 +0800877/* when one_device is NULL, it removes all device links */
878
Anand Jain32576042015-08-14 18:32:49 +0800879int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +0800880 struct btrfs_device *one_device)
881{
882 struct hd_struct *disk;
883 struct kobject *disk_kobj;
884
Anand Jain6c14a162015-03-10 06:38:34 +0800885 if (!fs_devices->device_dir_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +0800886 return -EINVAL;
887
Liu Bo87fa3bb2014-07-29 19:09:39 +0800888 if (one_device && one_device->bdev) {
Anand Jain99994cd2014-06-03 11:36:00 +0800889 disk = one_device->bdev->bd_part;
890 disk_kobj = &part_to_dev(disk)->kobj;
891
Anand Jain6c14a162015-03-10 06:38:34 +0800892 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jain99994cd2014-06-03 11:36:00 +0800893 disk_kobj->name);
894 }
895
Anand Jaine7e1aa92015-03-10 06:38:21 +0800896 if (one_device)
897 return 0;
898
899 list_for_each_entry(one_device,
Anand Jain6c14a162015-03-10 06:38:34 +0800900 &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +0800901 if (!one_device->bdev)
902 continue;
903 disk = one_device->bdev->bd_part;
904 disk_kobj = &part_to_dev(disk)->kobj;
905
Anand Jain6c14a162015-03-10 06:38:34 +0800906 sysfs_remove_link(fs_devices->device_dir_kobj,
Anand Jaine7e1aa92015-03-10 06:38:21 +0800907 disk_kobj->name);
908 }
909
Anand Jain99994cd2014-06-03 11:36:00 +0800910 return 0;
911}
912
Anand Jain2e7910d2015-03-10 06:38:29 +0800913int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
Anand Jain00c921c2015-03-10 06:38:28 +0800914{
Anand Jain2e7910d2015-03-10 06:38:29 +0800915 if (!fs_devs->device_dir_kobj)
916 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
Anand Jainc1b7e472015-08-14 18:32:50 +0800917 &fs_devs->fsid_kobj);
Anand Jain00c921c2015-03-10 06:38:28 +0800918
Anand Jain2e7910d2015-03-10 06:38:29 +0800919 if (!fs_devs->device_dir_kobj)
Anand Jain00c921c2015-03-10 06:38:28 +0800920 return -ENOMEM;
921
922 return 0;
923}
924
Anand Jaine3bd6972015-08-14 18:32:48 +0800925int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +0800926 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400927{
928 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400929 struct btrfs_device *dev;
930
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400931 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +0800932 struct hd_struct *disk;
933 struct kobject *disk_kobj;
934
935 if (!dev->bdev)
936 continue;
937
Anand Jain0d393762014-06-03 11:36:01 +0800938 if (one_device && one_device != dev)
939 continue;
940
Anand Jainf0853812014-01-15 17:22:28 +0800941 disk = dev->bdev->bd_part;
942 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400943
Anand Jain2e7910d2015-03-10 06:38:29 +0800944 error = sysfs_create_link(fs_devices->device_dir_kobj,
Jeff Mahoney29e5be22013-11-01 13:07:05 -0400945 disk_kobj, disk_kobj->name);
946 if (error)
947 break;
948 }
949
950 return error;
951}
952
David Sterba5b286922019-08-01 18:50:16 +0200953void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
954{
955 int ret;
956
957 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
958 if (ret)
959 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
960 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
961 &disk_to_dev(bdev->bd_disk)->kobj);
962}
963
David Sterbaf93c3992019-08-01 18:50:16 +0200964void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
965 const u8 *fsid)
966{
967 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
968
969 /*
970 * Sprouting changes fsid of the mounted filesystem, rename the fsid
971 * directory
972 */
973 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
974 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
975 btrfs_warn(fs_devices->fs_info,
976 "sysfs: failed to create fsid for sprout");
977}
978
Jeff Mahoney510d7362013-11-01 13:06:59 -0400979/* /sys/fs/btrfs/ entry */
980static struct kset *btrfs_kset;
981
Anand Jain72059212015-03-10 06:38:26 +0800982/*
983 * Can be called by the device discovery thread.
984 * And parent can be specified for seed device
985 */
Anand Jain0c10e2d2015-03-10 06:38:35 +0800986int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
Anand Jain72059212015-03-10 06:38:26 +0800987 struct kobject *parent)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400988{
989 int error;
990
Anand Jain2e7910d2015-03-10 06:38:29 +0800991 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +0800992 fs_devs->fsid_kobj.kset = btrfs_kset;
993 error = kobject_init_and_add(&fs_devs->fsid_kobj,
Anand Jain24bd69c2015-03-10 06:38:39 +0800994 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +1000995 if (error) {
996 kobject_put(&fs_devs->fsid_kobj);
997 return error;
998 }
999
1000 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001001}
1002
Anand Jain96f31362015-08-14 18:32:46 +08001003int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001004{
1005 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001006 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001007 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001008
Anand Jain5a13f432015-03-10 06:38:31 +08001009 btrfs_set_fs_info_ptr(fs_info);
1010
Anand Jaine3bd6972015-08-14 18:32:48 +08001011 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001012 if (error)
1013 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001014
Anand Jainc1b7e472015-08-14 18:32:50 +08001015 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001016 if (error) {
Anand Jain32576042015-08-14 18:32:49 +08001017 btrfs_sysfs_rm_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001018 return error;
1019 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001020
Anand Jainc1b7e472015-08-14 18:32:50 +08001021 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001022 &btrfs_feature_attr_group);
1023 if (error)
1024 goto failure;
1025
David Sterba6e369fe2019-06-13 17:23:02 +02001026#ifdef CONFIG_BTRFS_DEBUG
1027 error = sysfs_create_group(fsid_kobj,
1028 &btrfs_debug_feature_attr_group);
1029 if (error)
1030 goto failure;
1031#endif
1032
Jeff Mahoneye453d982013-11-21 10:37:16 -05001033 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001034 if (error)
1035 goto failure;
1036
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001037 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001038 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001039 if (!fs_info->space_info_kobj) {
1040 error = -ENOMEM;
1041 goto failure;
1042 }
1043
1044 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1045 if (error)
1046 goto failure;
1047
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001048 return 0;
1049failure:
Anand Jain6618a592015-08-14 18:32:47 +08001050 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001051 return error;
1052}
1053
David Sterba444e7512016-01-21 18:50:40 +01001054
1055/*
1056 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1057 * values in superblock. Call after any changes to incompat/compat_ro flags
1058 */
1059void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1060 u64 bit, enum btrfs_feature_set set)
1061{
1062 struct btrfs_fs_devices *fs_devs;
1063 struct kobject *fsid_kobj;
1064 u64 features;
1065 int ret;
1066
1067 if (!fs_info)
1068 return;
1069
1070 features = get_features(fs_info, set);
1071 ASSERT(bit & supported_feature_masks[set]);
1072
1073 fs_devs = fs_info->fs_devices;
1074 fsid_kobj = &fs_devs->fsid_kobj;
1075
David Sterbabf609202016-01-27 14:06:29 +01001076 if (!fsid_kobj->state_initialized)
1077 return;
1078
David Sterba444e7512016-01-21 18:50:40 +01001079 /*
1080 * FIXME: this is too heavy to update just one value, ideally we'd like
1081 * to use sysfs_update_group but some refactoring is needed first.
1082 */
1083 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1084 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1085}
1086
Liu Bof5c29bd2017-11-02 17:21:50 -06001087int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001088{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001089 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001090
Greg KHe3fe4e72008-02-20 14:14:16 -05001091 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1092 if (!btrfs_kset)
1093 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001094
David Sterba1bae3092014-02-05 15:36:18 +01001095 init_feature_attrs();
1096 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001097 if (ret)
1098 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001099 ret = sysfs_merge_group(&btrfs_kset->kobj,
1100 &btrfs_static_feature_attr_group);
1101 if (ret)
1102 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001103
David Sterba6e369fe2019-06-13 17:23:02 +02001104#ifdef CONFIG_BTRFS_DEBUG
1105 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1106 if (ret)
1107 goto out2;
1108#endif
1109
Filipe Manana001a6482015-01-23 18:27:00 +00001110 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001111
1112out_remove_group:
1113 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001114out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001115 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001116
1117 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001118}
1119
David Sterbae67c7182018-02-19 17:24:18 +01001120void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001121{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001122 sysfs_unmerge_group(&btrfs_kset->kobj,
1123 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001124 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -05001125 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001126}
Chris Mason55d47412008-02-20 16:02:51 -05001127