blob: 16379f491ca1db5523eac50c32cf23faafc33b11 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Josef Bacik58176a92007-08-29 15:47:34 -04006#include <linux/sched.h>
David Sterba32a99912019-08-01 17:49:55 +02007#include <linux/sched/mm.h>
Josef Bacik58176a92007-08-29 15:47:34 -04008#include <linux/slab.h>
9#include <linux/spinlock.h>
10#include <linux/completion.h>
Jeff Mahoney79da4fa2013-11-01 13:07:00 -040011#include <linux/bug.h>
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +020012#include <crypto/hash.h>
Josef Bacik58176a92007-08-29 15:47:34 -040013
Chris Masonbae45de2007-04-04 21:22:22 -040014#include "ctree.h"
15#include "disk-io.h"
16#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040017#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040018#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040019#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040020#include "block-group.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040021
David Sterba9188db62019-08-02 12:52:48 +020022struct btrfs_feature_attr {
23 struct kobj_attribute kobj_attr;
24 enum btrfs_feature_set feature_set;
25 u64 feature_bit;
26};
27
28/* For raid type sysfs entries */
29struct raid_kobject {
30 u64 flags;
31 struct kobject kobj;
32};
33
34#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
35{ \
36 .attr = { .name = __stringify(_name), .mode = _mode }, \
37 .show = _show, \
38 .store = _store, \
39}
40
41#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
42 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
43 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
44
45#define BTRFS_ATTR(_prefix, _name, _show) \
46 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
47 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
48
49#define BTRFS_ATTR_PTR(_prefix, _name) \
50 (&btrfs_attr_##_prefix##_##_name.attr)
51
52#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
53static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
54 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
55 btrfs_feature_attr_show, \
56 btrfs_feature_attr_store), \
57 .feature_set = _feature_set, \
58 .feature_bit = _feature_prefix ##_## _feature_bit, \
59}
60#define BTRFS_FEAT_ATTR_PTR(_name) \
61 (&btrfs_attr_features_##_name.kobj_attr.attr)
62
63#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
64 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
65#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
66 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
67#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
68 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
69
Jeff Mahoney510d7362013-11-01 13:06:59 -040070static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080071static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040072
David Sterba8f523162019-08-02 13:07:38 +020073static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
74{
75 return container_of(a, struct btrfs_feature_attr, kobj_attr);
76}
77
78static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
79{
80 return container_of(attr, struct kobj_attribute, attr);
81}
82
83static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
84 struct attribute *attr)
85{
86 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
87}
88
Jeff Mahoney510d7362013-11-01 13:06:59 -040089static u64 get_features(struct btrfs_fs_info *fs_info,
90 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040091{
Jeff Mahoney510d7362013-11-01 13:06:59 -040092 struct btrfs_super_block *disk_super = fs_info->super_copy;
93 if (set == FEAT_COMPAT)
94 return btrfs_super_compat_flags(disk_super);
95 else if (set == FEAT_COMPAT_RO)
96 return btrfs_super_compat_ro_flags(disk_super);
97 else
98 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040099}
100
Jeff Mahoneyba631942013-11-01 13:07:01 -0400101static void set_features(struct btrfs_fs_info *fs_info,
102 enum btrfs_feature_set set, u64 features)
103{
104 struct btrfs_super_block *disk_super = fs_info->super_copy;
105 if (set == FEAT_COMPAT)
106 btrfs_set_super_compat_flags(disk_super, features);
107 else if (set == FEAT_COMPAT_RO)
108 btrfs_set_super_compat_ro_flags(disk_super, features);
109 else
110 btrfs_set_super_incompat_flags(disk_super, features);
111}
112
113static int can_modify_feature(struct btrfs_feature_attr *fa)
114{
115 int val = 0;
116 u64 set, clear;
117 switch (fa->feature_set) {
118 case FEAT_COMPAT:
119 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
120 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
121 break;
122 case FEAT_COMPAT_RO:
123 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
124 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
125 break;
126 case FEAT_INCOMPAT:
127 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
128 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
129 break;
130 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -0400131 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +0100132 fa->feature_set);
133 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400134 }
135
136 if (set & fa->feature_bit)
137 val |= 1;
138 if (clear & fa->feature_bit)
139 val |= 2;
140
141 return val;
142}
143
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400144static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
145 struct kobj_attribute *a, char *buf)
146{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400147 int val = 0;
148 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400149 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400150 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400151 u64 features = get_features(fs_info, fa->feature_set);
152 if (features & fa->feature_bit)
153 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400154 } else
155 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400156
157 return snprintf(buf, PAGE_SIZE, "%d\n", val);
158}
159
Jeff Mahoneyba631942013-11-01 13:07:01 -0400160static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
161 struct kobj_attribute *a,
162 const char *buf, size_t count)
163{
164 struct btrfs_fs_info *fs_info;
165 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400166 u64 features, set, clear;
167 unsigned long val;
168 int ret;
169
170 fs_info = to_fs_info(kobj);
171 if (!fs_info)
172 return -EPERM;
173
David Howellsbc98a422017-07-17 08:45:34 +0100174 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100175 return -EROFS;
176
Jeff Mahoneyba631942013-11-01 13:07:01 -0400177 ret = kstrtoul(skip_spaces(buf), 0, &val);
178 if (ret)
179 return ret;
180
181 if (fa->feature_set == FEAT_COMPAT) {
182 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
183 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
184 } else if (fa->feature_set == FEAT_COMPAT_RO) {
185 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
186 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
187 } else {
188 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
189 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
190 }
191
192 features = get_features(fs_info, fa->feature_set);
193
194 /* Nothing to do */
195 if ((val && (features & fa->feature_bit)) ||
196 (!val && !(features & fa->feature_bit)))
197 return count;
198
199 if ((val && !(set & fa->feature_bit)) ||
200 (!val && !(clear & fa->feature_bit))) {
201 btrfs_info(fs_info,
202 "%sabling feature %s on mounted fs is not supported.",
203 val ? "En" : "Dis", fa->kobj_attr.attr.name);
204 return -EPERM;
205 }
206
207 btrfs_info(fs_info, "%s %s feature flag",
208 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
209
Jeff Mahoneyba631942013-11-01 13:07:01 -0400210 spin_lock(&fs_info->super_lock);
211 features = get_features(fs_info, fa->feature_set);
212 if (val)
213 features |= fa->feature_bit;
214 else
215 features &= ~fa->feature_bit;
216 set_features(fs_info, fa->feature_set, features);
217 spin_unlock(&fs_info->super_lock);
218
David Sterba0eae2742014-11-12 14:22:21 +0100219 /*
220 * We don't want to do full transaction commit from inside sysfs
221 */
222 btrfs_set_pending(fs_info, COMMIT);
223 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400224
225 return count;
226}
227
Jeff Mahoney510d7362013-11-01 13:06:59 -0400228static umode_t btrfs_feature_visible(struct kobject *kobj,
229 struct attribute *attr, int unused)
230{
231 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
232 umode_t mode = attr->mode;
233
234 if (fs_info) {
235 struct btrfs_feature_attr *fa;
236 u64 features;
237
238 fa = attr_to_btrfs_feature_attr(attr);
239 features = get_features(fs_info, fa->feature_set);
240
Jeff Mahoneyba631942013-11-01 13:07:01 -0400241 if (can_modify_feature(fa))
242 mode |= S_IWUSR;
243 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400244 mode = 0;
245 }
246
247 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400248}
249
250BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
251BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
252BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
253BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700254BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400255BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
256BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
257BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
258BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100259BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200260BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100261BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
David Sterbacfbb8252018-07-10 18:15:05 +0200262BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400263
264static struct attribute *btrfs_supported_feature_attrs[] = {
265 BTRFS_FEAT_ATTR_PTR(mixed_backref),
266 BTRFS_FEAT_ATTR_PTR(default_subvol),
267 BTRFS_FEAT_ATTR_PTR(mixed_groups),
268 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700269 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400270 BTRFS_FEAT_ATTR_PTR(big_metadata),
271 BTRFS_FEAT_ATTR_PTR(extended_iref),
272 BTRFS_FEAT_ATTR_PTR(raid56),
273 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100274 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200275 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100276 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200277 BTRFS_FEAT_ATTR_PTR(raid1c34),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400278 NULL
279};
280
Misono Tomohirof902bd32018-05-17 14:24:51 +0900281/*
282 * Features which depend on feature bits and may differ between each fs.
283 *
284 * /sys/fs/btrfs/features lists all available features of this kernel while
285 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
286 * can be changed online.
287 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400288static const struct attribute_group btrfs_feature_attr_group = {
289 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400290 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400291 .attrs = btrfs_supported_feature_attrs,
292};
Josef Bacik58176a92007-08-29 15:47:34 -0400293
Misono Tomohirof902bd32018-05-17 14:24:51 +0900294static ssize_t rmdir_subvol_show(struct kobject *kobj,
295 struct kobj_attribute *ka, char *buf)
296{
297 return snprintf(buf, PAGE_SIZE, "0\n");
298}
299BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
300
David Sterbaf7cea562019-10-07 11:11:03 +0200301static ssize_t supported_checksums_show(struct kobject *kobj,
302 struct kobj_attribute *a, char *buf)
303{
304 ssize_t ret = 0;
305 int i;
306
307 for (i = 0; i < btrfs_get_num_csums(); i++) {
308 /*
309 * This "trick" only works as long as 'enum btrfs_csum_type' has
310 * no holes in it
311 */
312 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
313 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
314
315 }
316
317 ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
318 return ret;
319}
320BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
321
Misono Tomohirof902bd32018-05-17 14:24:51 +0900322static struct attribute *btrfs_supported_static_feature_attrs[] = {
323 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200324 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900325 NULL
326};
327
328/*
329 * Features which only depend on kernel version.
330 *
331 * These are listed in /sys/fs/btrfs/features along with
332 * btrfs_feature_attr_group
333 */
334static const struct attribute_group btrfs_static_feature_attr_group = {
335 .name = "features",
336 .attrs = btrfs_supported_static_feature_attrs,
337};
338
David Sterba6e369fe2019-06-13 17:23:02 +0200339#ifdef CONFIG_BTRFS_DEBUG
340
341/*
342 * Runtime debugging exported via sysfs
343 *
344 * /sys/fs/btrfs/debug - applies to module or all filesystems
345 * /sys/fs/btrfs/UUID - applies only to the given filesystem
346 */
347static struct attribute *btrfs_debug_feature_attrs[] = {
348 NULL
349};
350
351static const struct attribute_group btrfs_debug_feature_attr_group = {
352 .name = "debug",
353 .attrs = btrfs_debug_feature_attrs,
354};
355
356#endif
357
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400358static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
359{
360 u64 val;
361 if (lock)
362 spin_lock(lock);
363 val = *value_ptr;
364 if (lock)
365 spin_unlock(lock);
366 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
367}
368
369static ssize_t global_rsv_size_show(struct kobject *kobj,
370 struct kobj_attribute *ka, char *buf)
371{
372 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
373 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
374 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
375}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200376BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400377
378static ssize_t global_rsv_reserved_show(struct kobject *kobj,
379 struct kobj_attribute *a, char *buf)
380{
381 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
382 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
383 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
384}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200385BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400386
387#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400388#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400389
390static ssize_t raid_bytes_show(struct kobject *kobj,
391 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200392BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
393BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400394
395static ssize_t raid_bytes_show(struct kobject *kobj,
396 struct kobj_attribute *attr, char *buf)
397
398{
399 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100400 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400401 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400402 u64 val = 0;
403
404 down_read(&sinfo->groups_sem);
405 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200406 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200407 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400408 else
David Sterbabf38be62019-10-23 18:48:11 +0200409 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400410 }
411 up_read(&sinfo->groups_sem);
412 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
413}
414
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400415static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200416 BTRFS_ATTR_PTR(raid, total_bytes),
417 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400418 NULL
419};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400420ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400421
422static void release_raid_kobj(struct kobject *kobj)
423{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400424 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400425}
426
David Sterba536ea452019-08-01 17:55:55 +0200427static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400428 .sysfs_ops = &kobj_sysfs_ops,
429 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400430 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400431};
432
433#define SPACE_INFO_ATTR(field) \
434static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
435 struct kobj_attribute *a, \
436 char *buf) \
437{ \
438 struct btrfs_space_info *sinfo = to_space_info(kobj); \
439 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
440} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200441BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400442
443static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
444 struct kobj_attribute *a,
445 char *buf)
446{
447 struct btrfs_space_info *sinfo = to_space_info(kobj);
448 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
449 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
450}
451
452SPACE_INFO_ATTR(flags);
453SPACE_INFO_ATTR(total_bytes);
454SPACE_INFO_ATTR(bytes_used);
455SPACE_INFO_ATTR(bytes_pinned);
456SPACE_INFO_ATTR(bytes_reserved);
457SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800458SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400459SPACE_INFO_ATTR(disk_used);
460SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200461BTRFS_ATTR(space_info, total_bytes_pinned,
462 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400463
464static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200465 BTRFS_ATTR_PTR(space_info, flags),
466 BTRFS_ATTR_PTR(space_info, total_bytes),
467 BTRFS_ATTR_PTR(space_info, bytes_used),
468 BTRFS_ATTR_PTR(space_info, bytes_pinned),
469 BTRFS_ATTR_PTR(space_info, bytes_reserved),
470 BTRFS_ATTR_PTR(space_info, bytes_may_use),
471 BTRFS_ATTR_PTR(space_info, bytes_readonly),
472 BTRFS_ATTR_PTR(space_info, disk_used),
473 BTRFS_ATTR_PTR(space_info, disk_total),
474 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400475 NULL,
476};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400477ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400478
479static void space_info_release(struct kobject *kobj)
480{
481 struct btrfs_space_info *sinfo = to_space_info(kobj);
482 percpu_counter_destroy(&sinfo->total_bytes_pinned);
483 kfree(sinfo);
484}
485
David Sterba27992d02019-08-01 17:55:55 +0200486static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400487 .sysfs_ops = &kobj_sysfs_ops,
488 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400489 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400490};
491
492static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200493 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
494 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400495 NULL,
496};
497
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400498static ssize_t btrfs_label_show(struct kobject *kobj,
499 struct kobj_attribute *a, char *buf)
500{
501 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900502 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200503 ssize_t ret;
504
505 spin_lock(&fs_info->super_lock);
506 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
507 spin_unlock(&fs_info->super_lock);
508
509 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400510}
511
512static ssize_t btrfs_label_store(struct kobject *kobj,
513 struct kobj_attribute *a,
514 const char *buf, size_t len)
515{
516 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900517 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400518
David Sterba66ac9fe2016-04-26 16:03:57 +0200519 if (!fs_info)
520 return -EPERM;
521
David Howellsbc98a422017-07-17 08:45:34 +0100522 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800523 return -EROFS;
524
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900525 /*
526 * p_len is the len until the first occurrence of either
527 * '\n' or '\0'
528 */
529 p_len = strcspn(buf, "\n");
530
531 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400532 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400533
David Sterbaa6f69dc2014-05-30 19:29:05 +0200534 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900535 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
536 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200537 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400538
David Sterbaa6f69dc2014-05-30 19:29:05 +0200539 /*
540 * We don't want to do full transaction commit from inside sysfs
541 */
542 btrfs_set_pending(fs_info, COMMIT);
543 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400544
David Sterbaa6f69dc2014-05-30 19:29:05 +0200545 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400546}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200547BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400548
David Sterbadf935892014-05-07 18:17:16 +0200549static ssize_t btrfs_nodesize_show(struct kobject *kobj,
550 struct kobj_attribute *a, char *buf)
551{
552 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
553
David Sterba093e0372018-03-16 14:31:43 +0100554 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200555}
556
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200557BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200558
559static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
560 struct kobj_attribute *a, char *buf)
561{
562 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
563
David Sterba093e0372018-03-16 14:31:43 +0100564 return snprintf(buf, PAGE_SIZE, "%u\n",
565 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200566}
567
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200568BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200569
570static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
571 struct kobj_attribute *a, char *buf)
572{
573 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
574
David Sterba093e0372018-03-16 14:31:43 +0100575 return snprintf(buf, PAGE_SIZE, "%u\n",
576 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200577}
578
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200579BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200580
Sargun Dhillon27234802017-05-11 21:18:03 +0000581static ssize_t quota_override_show(struct kobject *kobj,
582 struct kobj_attribute *a, char *buf)
583{
584 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
585 int quota_override;
586
587 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
588 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
589}
590
591static ssize_t quota_override_store(struct kobject *kobj,
592 struct kobj_attribute *a,
593 const char *buf, size_t len)
594{
595 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
596 unsigned long knob;
597 int err;
598
599 if (!fs_info)
600 return -EPERM;
601
602 if (!capable(CAP_SYS_RESOURCE))
603 return -EPERM;
604
605 err = kstrtoul(buf, 10, &knob);
606 if (err)
607 return err;
608 if (knob > 1)
609 return -EINVAL;
610
611 if (knob)
612 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
613 else
614 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
615
616 return len;
617}
618
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200619BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000620
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200621static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
622 struct kobj_attribute *a, char *buf)
623{
624 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
625
626 return snprintf(buf, PAGE_SIZE, "%pU\n",
627 fs_info->fs_devices->metadata_uuid);
628}
629
630BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
631
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200632static ssize_t btrfs_checksum_show(struct kobject *kobj,
633 struct kobj_attribute *a, char *buf)
634{
635 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
636 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
637
638 return snprintf(buf, PAGE_SIZE, "%s (%s)\n",
639 btrfs_super_csum_name(csum_type),
640 crypto_shash_driver_name(fs_info->csum_shash));
641}
642
643BTRFS_ATTR(, checksum, btrfs_checksum_show);
644
Anand Jain0dd29062015-03-10 06:38:27 +0800645static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200646 BTRFS_ATTR_PTR(, label),
647 BTRFS_ATTR_PTR(, nodesize),
648 BTRFS_ATTR_PTR(, sectorsize),
649 BTRFS_ATTR_PTR(, clone_alignment),
650 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200651 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200652 BTRFS_ATTR_PTR(, checksum),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400653 NULL,
654};
655
Anand Jainc1b7e472015-08-14 18:32:50 +0800656static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400657{
Anand Jain2e7910d2015-03-10 06:38:29 +0800658 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800659
Anand Jainc1b7e472015-08-14 18:32:50 +0800660 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800661 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400662}
663
664static struct kobj_type btrfs_ktype = {
665 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800666 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400667};
668
Anand Jain2e7910d2015-03-10 06:38:29 +0800669static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
670{
671 if (kobj->ktype != &btrfs_ktype)
672 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800673 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800674}
675
Jeff Mahoney510d7362013-11-01 13:06:59 -0400676static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
677{
678 if (kobj->ktype != &btrfs_ktype)
679 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800680 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400681}
Josef Bacik58176a92007-08-29 15:47:34 -0400682
Jeff Mahoneye453d982013-11-21 10:37:16 -0500683#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900684#define BTRFS_FEATURE_NAME_MAX 13
685static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
686static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500687
Tomohiro Misono6c521572018-05-16 17:09:26 +0900688static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500689 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
690 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
691 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
692};
693
694static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400695{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500696 int set;
697
698 for (set = 0; set < FEAT_MAX; set++) {
699 int i;
700 struct attribute *attrs[2];
701 struct attribute_group agroup = {
702 .name = "features",
703 .attrs = attrs,
704 };
705 u64 features = get_features(fs_info, set);
706 features &= ~supported_feature_masks[set];
707
708 if (!features)
709 continue;
710
711 attrs[1] = NULL;
712 for (i = 0; i < NUM_FEATURE_BITS; i++) {
713 struct btrfs_feature_attr *fa;
714
715 if (!(features & (1ULL << i)))
716 continue;
717
718 fa = &btrfs_feature_attrs[set][i];
719 attrs[0] = &fa->kobj_attr.attr;
720 if (add) {
721 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800722 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500723 &agroup);
724 if (ret)
725 return ret;
726 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800727 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500728 &agroup);
729 }
730
731 }
732 return 0;
733}
734
Anand Jain2e3e1282015-03-10 06:38:32 +0800735static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500736{
Anand Jainb5501502019-11-21 17:33:30 +0800737 if (fs_devs->devices_kobj) {
738 kobject_del(fs_devs->devices_kobj);
739 kobject_put(fs_devs->devices_kobj);
740 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800741 }
742
Anand Jainc1b7e472015-08-14 18:32:50 +0800743 if (fs_devs->fsid_kobj.state_initialized) {
744 kobject_del(&fs_devs->fsid_kobj);
745 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800746 wait_for_completion(&fs_devs->kobj_unregister);
747 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400748}
749
Anand Jain2e3e1282015-03-10 06:38:32 +0800750/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800751void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800752{
753 struct list_head *fs_uuids = btrfs_get_fs_uuids();
754
755 if (fs_devs) {
756 __btrfs_sysfs_remove_fsid(fs_devs);
757 return;
758 }
759
Anand Jainc4babc52018-04-12 10:29:25 +0800760 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800761 __btrfs_sysfs_remove_fsid(fs_devs);
762 }
763}
764
Anand Jain6618a592015-08-14 18:32:47 +0800765void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500766{
Anand Jain5a13f432015-03-10 06:38:31 +0800767 btrfs_reset_fs_info_ptr(fs_info);
768
Jeff Mahoneye453d982013-11-21 10:37:16 -0500769 if (fs_info->space_info_kobj) {
770 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
771 kobject_del(fs_info->space_info_kobj);
772 kobject_put(fs_info->space_info_kobj);
773 }
Jeff Mahoneye453d982013-11-21 10:37:16 -0500774 addrm_unknown_feature_attrs(fs_info, false);
Anand Jainc1b7e472015-08-14 18:32:50 +0800775 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
776 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
Anand Jain32576042015-08-14 18:32:49 +0800777 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500778}
779
David Sterbaf10152b2019-08-01 19:07:55 +0200780static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400781 [FEAT_COMPAT] = "compat",
782 [FEAT_COMPAT_RO] = "compat_ro",
783 [FEAT_INCOMPAT] = "incompat",
784};
785
David Sterbaf10152b2019-08-01 19:07:55 +0200786const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
787{
788 return btrfs_feature_set_names[set];
789}
790
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400791char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
792{
793 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
794 int len = 0;
795 int i;
796 char *str;
797
798 str = kmalloc(bufsize, GFP_KERNEL);
799 if (!str)
800 return str;
801
802 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
803 const char *name;
804
805 if (!(flags & (1ULL << i)))
806 continue;
807
808 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
809 len += snprintf(str + len, bufsize - len, "%s%s",
810 len ? "," : "", name);
811 }
812
813 return str;
814}
815
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400816static void init_feature_attrs(void)
817{
818 struct btrfs_feature_attr *fa;
819 int set, i;
820
821 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
822 ARRAY_SIZE(btrfs_feature_attrs));
823 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
824 ARRAY_SIZE(btrfs_feature_attrs[0]));
825
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400826 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
827 memset(btrfs_unknown_feature_names, 0,
828 sizeof(btrfs_unknown_feature_names));
829
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400830 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
831 struct btrfs_feature_attr *sfa;
832 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400833 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400834 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400835 bit = ilog2(sfa->feature_bit);
836 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400837
838 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
839 }
840
841 for (set = 0; set < FEAT_MAX; set++) {
842 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
843 char *name = btrfs_unknown_feature_names[set][i];
844 fa = &btrfs_feature_attrs[set][i];
845
846 if (fa->kobj_attr.attr.name)
847 continue;
848
Tomohiro Misono6c521572018-05-16 17:09:26 +0900849 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400850 btrfs_feature_set_names[set], i);
851
852 fa->kobj_attr.attr.name = name;
853 fa->kobj_attr.attr.mode = S_IRUGO;
854 fa->feature_set = set;
855 fa->feature_bit = 1ULL << i;
856 }
857 }
858}
859
David Sterba32a99912019-08-01 17:49:55 +0200860/*
861 * Create a sysfs entry for a given block group type at path
862 * /sys/fs/btrfs/UUID/allocation/data/TYPE
863 */
David Sterba32da53862019-10-29 19:20:18 +0100864void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +0200865{
866 struct btrfs_fs_info *fs_info = cache->fs_info;
867 struct btrfs_space_info *space_info = cache->space_info;
868 struct raid_kobject *rkobj;
869 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
870 unsigned int nofs_flag;
871 int ret;
872
873 /*
874 * Setup a NOFS context because kobject_add(), deep in its call chain,
875 * does GFP_KERNEL allocations, and we are often called in a context
876 * where if reclaim is triggered we can deadlock (we are either holding
877 * a transaction handle or some lock required for a transaction
878 * commit).
879 */
880 nofs_flag = memalloc_nofs_save();
881
882 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
883 if (!rkobj) {
884 memalloc_nofs_restore(nofs_flag);
885 btrfs_warn(cache->fs_info,
886 "couldn't alloc memory for raid level kobject");
887 return;
888 }
889
890 rkobj->flags = cache->flags;
891 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
892 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
893 btrfs_bg_type_to_raid_name(rkobj->flags));
894 memalloc_nofs_restore(nofs_flag);
895 if (ret) {
896 kobject_put(&rkobj->kobj);
897 btrfs_warn(fs_info,
898 "failed to add kobject for block cache, ignoring");
899 return;
900 }
901
902 space_info->block_group_kobjs[index] = &rkobj->kobj;
903}
904
David Sterbab5865ba2019-08-01 18:50:16 +0200905/*
906 * Remove sysfs directories for all block group types of a given space info and
907 * the space info as well
908 */
909void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
910{
911 int i;
912
913 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
914 struct kobject *kobj;
915
916 kobj = space_info->block_group_kobjs[i];
917 space_info->block_group_kobjs[i] = NULL;
918 if (kobj) {
919 kobject_del(kobj);
920 kobject_put(kobj);
921 }
922 }
923 kobject_del(&space_info->kobj);
924 kobject_put(&space_info->kobj);
925}
926
David Sterbab8823272019-08-01 18:50:16 +0200927static const char *alloc_name(u64 flags)
928{
929 switch (flags) {
930 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
931 return "mixed";
932 case BTRFS_BLOCK_GROUP_METADATA:
933 return "metadata";
934 case BTRFS_BLOCK_GROUP_DATA:
935 return "data";
936 case BTRFS_BLOCK_GROUP_SYSTEM:
937 return "system";
938 default:
939 WARN_ON(1);
940 return "invalid-combination";
941 };
942}
943
944/*
945 * Create a sysfs entry for a space info type at path
946 * /sys/fs/btrfs/UUID/allocation/TYPE
947 */
948int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
949 struct btrfs_space_info *space_info)
950{
951 int ret;
952
953 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
954 fs_info->space_info_kobj, "%s",
955 alloc_name(space_info->flags));
956 if (ret) {
957 kobject_put(&space_info->kobj);
958 return ret;
959 }
960
961 return 0;
962}
963
Anand Jaine7e1aa92015-03-10 06:38:21 +0800964/* when one_device is NULL, it removes all device links */
965
Anand Jain32576042015-08-14 18:32:49 +0800966int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +0800967 struct btrfs_device *one_device)
968{
969 struct hd_struct *disk;
970 struct kobject *disk_kobj;
971
Anand Jainb5501502019-11-21 17:33:30 +0800972 if (!fs_devices->devices_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +0800973 return -EINVAL;
974
Liu Bo87fa3bb2014-07-29 19:09:39 +0800975 if (one_device && one_device->bdev) {
Anand Jain99994cd2014-06-03 11:36:00 +0800976 disk = one_device->bdev->bd_part;
977 disk_kobj = &part_to_dev(disk)->kobj;
978
Anand Jainb5501502019-11-21 17:33:30 +0800979 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
Anand Jain99994cd2014-06-03 11:36:00 +0800980 }
981
Anand Jaine7e1aa92015-03-10 06:38:21 +0800982 if (one_device)
983 return 0;
984
985 list_for_each_entry(one_device,
Anand Jain6c14a162015-03-10 06:38:34 +0800986 &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +0800987 if (!one_device->bdev)
988 continue;
989 disk = one_device->bdev->bd_part;
990 disk_kobj = &part_to_dev(disk)->kobj;
991
Anand Jainb5501502019-11-21 17:33:30 +0800992 sysfs_remove_link(fs_devices->devices_kobj, disk_kobj->name);
Anand Jaine7e1aa92015-03-10 06:38:21 +0800993 }
994
Anand Jain99994cd2014-06-03 11:36:00 +0800995 return 0;
996}
997
Anand Jaine3bd6972015-08-14 18:32:48 +0800998int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +0800999 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001000{
1001 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001002 struct btrfs_device *dev;
1003
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001004 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +08001005 struct hd_struct *disk;
1006 struct kobject *disk_kobj;
1007
1008 if (!dev->bdev)
1009 continue;
1010
Anand Jain0d393762014-06-03 11:36:01 +08001011 if (one_device && one_device != dev)
1012 continue;
1013
Anand Jainf0853812014-01-15 17:22:28 +08001014 disk = dev->bdev->bd_part;
1015 disk_kobj = &part_to_dev(disk)->kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001016
Anand Jainb5501502019-11-21 17:33:30 +08001017 error = sysfs_create_link(fs_devices->devices_kobj,
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001018 disk_kobj, disk_kobj->name);
1019 if (error)
1020 break;
1021 }
1022
1023 return error;
1024}
1025
David Sterba5b286922019-08-01 18:50:16 +02001026void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1027{
1028 int ret;
1029
1030 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1031 if (ret)
1032 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1033 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1034 &disk_to_dev(bdev->bd_disk)->kobj);
1035}
1036
David Sterbaf93c3992019-08-01 18:50:16 +02001037void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1038 const u8 *fsid)
1039{
1040 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1041
1042 /*
1043 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1044 * directory
1045 */
1046 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1047 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1048 btrfs_warn(fs_devices->fs_info,
1049 "sysfs: failed to create fsid for sprout");
1050}
1051
Jeff Mahoney510d7362013-11-01 13:06:59 -04001052/* /sys/fs/btrfs/ entry */
1053static struct kset *btrfs_kset;
1054
Anand Jain72059212015-03-10 06:38:26 +08001055/*
Anand Jainc6761a92019-11-21 17:33:32 +08001056 * Creates:
1057 * /sys/fs/btrfs/UUID
1058 *
Anand Jain72059212015-03-10 06:38:26 +08001059 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001060 */
Anand Jainc6761a92019-11-21 17:33:32 +08001061int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001062{
1063 int error;
1064
Anand Jain2e7910d2015-03-10 06:38:29 +08001065 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001066 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001067 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1068 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001069 if (error) {
1070 kobject_put(&fs_devs->fsid_kobj);
1071 return error;
1072 }
1073
Anand Jainbc036bb2019-11-21 17:33:34 +08001074 fs_devs->devices_kobj = kobject_create_and_add("devices",
1075 &fs_devs->fsid_kobj);
1076 if (!fs_devs->devices_kobj) {
1077 btrfs_err(fs_devs->fs_info,
1078 "failed to init sysfs device interface");
1079 kobject_put(&fs_devs->fsid_kobj);
1080 return -ENOMEM;
1081 }
1082
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001083 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001084}
1085
Anand Jain96f31362015-08-14 18:32:46 +08001086int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001087{
1088 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001089 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001090 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001091
Anand Jain5a13f432015-03-10 06:38:31 +08001092 btrfs_set_fs_info_ptr(fs_info);
1093
Anand Jaine3bd6972015-08-14 18:32:48 +08001094 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001095 if (error)
1096 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001097
Anand Jainc1b7e472015-08-14 18:32:50 +08001098 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001099 if (error) {
Anand Jain32576042015-08-14 18:32:49 +08001100 btrfs_sysfs_rm_device_link(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001101 return error;
1102 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001103
Anand Jainc1b7e472015-08-14 18:32:50 +08001104 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001105 &btrfs_feature_attr_group);
1106 if (error)
1107 goto failure;
1108
David Sterba6e369fe2019-06-13 17:23:02 +02001109#ifdef CONFIG_BTRFS_DEBUG
1110 error = sysfs_create_group(fsid_kobj,
1111 &btrfs_debug_feature_attr_group);
1112 if (error)
1113 goto failure;
1114#endif
1115
Jeff Mahoneye453d982013-11-21 10:37:16 -05001116 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001117 if (error)
1118 goto failure;
1119
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001120 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001121 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001122 if (!fs_info->space_info_kobj) {
1123 error = -ENOMEM;
1124 goto failure;
1125 }
1126
1127 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1128 if (error)
1129 goto failure;
1130
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001131 return 0;
1132failure:
Anand Jain6618a592015-08-14 18:32:47 +08001133 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001134 return error;
1135}
1136
David Sterba444e7512016-01-21 18:50:40 +01001137
1138/*
1139 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1140 * values in superblock. Call after any changes to incompat/compat_ro flags
1141 */
1142void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1143 u64 bit, enum btrfs_feature_set set)
1144{
1145 struct btrfs_fs_devices *fs_devs;
1146 struct kobject *fsid_kobj;
1147 u64 features;
1148 int ret;
1149
1150 if (!fs_info)
1151 return;
1152
1153 features = get_features(fs_info, set);
1154 ASSERT(bit & supported_feature_masks[set]);
1155
1156 fs_devs = fs_info->fs_devices;
1157 fsid_kobj = &fs_devs->fsid_kobj;
1158
David Sterbabf609202016-01-27 14:06:29 +01001159 if (!fsid_kobj->state_initialized)
1160 return;
1161
David Sterba444e7512016-01-21 18:50:40 +01001162 /*
1163 * FIXME: this is too heavy to update just one value, ideally we'd like
1164 * to use sysfs_update_group but some refactoring is needed first.
1165 */
1166 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1167 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1168}
1169
Liu Bof5c29bd2017-11-02 17:21:50 -06001170int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001171{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001172 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001173
Greg KHe3fe4e72008-02-20 14:14:16 -05001174 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1175 if (!btrfs_kset)
1176 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001177
David Sterba1bae3092014-02-05 15:36:18 +01001178 init_feature_attrs();
1179 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001180 if (ret)
1181 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001182 ret = sysfs_merge_group(&btrfs_kset->kobj,
1183 &btrfs_static_feature_attr_group);
1184 if (ret)
1185 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001186
David Sterba6e369fe2019-06-13 17:23:02 +02001187#ifdef CONFIG_BTRFS_DEBUG
1188 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1189 if (ret)
1190 goto out2;
1191#endif
1192
Filipe Manana001a6482015-01-23 18:27:00 +00001193 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001194
1195out_remove_group:
1196 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001197out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001198 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001199
1200 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001201}
1202
David Sterbae67c7182018-02-19 17:24:18 +01001203void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001204{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001205 sysfs_unmerge_group(&btrfs_kset->kobj,
1206 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001207 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Greg KHe3fe4e72008-02-20 14:14:16 -05001208 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001209}
Chris Mason55d47412008-02-20 16:02:51 -05001210