blob: d7c58c89fb9c1f19a67d11d5fd0226e518eb7773 [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"
Dennis Zhoudfb79dd2019-12-13 16:22:20 -080015#include "discard.h"
Chris Masonbae45de2007-04-04 21:22:22 -040016#include "disk-io.h"
17#include "transaction.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040018#include "sysfs.h"
Jeff Mahoney29e5be22013-11-01 13:07:05 -040019#include "volumes.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040020#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040021#include "block-group.h"
Qu Wenruo49e5fb42020-06-28 13:07:15 +080022#include "qgroup.h"
Jeff Mahoney079b72b2013-11-01 13:06:57 -040023
David Sterba9188db62019-08-02 12:52:48 +020024struct btrfs_feature_attr {
25 struct kobj_attribute kobj_attr;
26 enum btrfs_feature_set feature_set;
27 u64 feature_bit;
28};
29
30/* For raid type sysfs entries */
31struct raid_kobject {
32 u64 flags;
33 struct kobject kobj;
34};
35
36#define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
37{ \
38 .attr = { .name = __stringify(_name), .mode = _mode }, \
39 .show = _show, \
40 .store = _store, \
41}
42
43#define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
44 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
45 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
46
47#define BTRFS_ATTR(_prefix, _name, _show) \
48 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
49 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
50
51#define BTRFS_ATTR_PTR(_prefix, _name) \
52 (&btrfs_attr_##_prefix##_##_name.attr)
53
54#define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
55static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
56 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
57 btrfs_feature_attr_show, \
58 btrfs_feature_attr_store), \
59 .feature_set = _feature_set, \
60 .feature_bit = _feature_prefix ##_## _feature_bit, \
61}
62#define BTRFS_FEAT_ATTR_PTR(_name) \
63 (&btrfs_attr_features_##_name.kobj_attr.attr)
64
65#define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
66 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
67#define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
68 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
69#define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
70 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
71
Jeff Mahoney510d7362013-11-01 13:06:59 -040072static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +080073static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040074
David Sterba8f523162019-08-02 13:07:38 +020075static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
76{
77 return container_of(a, struct btrfs_feature_attr, kobj_attr);
78}
79
80static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
81{
82 return container_of(attr, struct kobj_attribute, attr);
83}
84
85static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
86 struct attribute *attr)
87{
88 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
89}
90
Jeff Mahoney510d7362013-11-01 13:06:59 -040091static u64 get_features(struct btrfs_fs_info *fs_info,
92 enum btrfs_feature_set set)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -040093{
Jeff Mahoney510d7362013-11-01 13:06:59 -040094 struct btrfs_super_block *disk_super = fs_info->super_copy;
95 if (set == FEAT_COMPAT)
96 return btrfs_super_compat_flags(disk_super);
97 else if (set == FEAT_COMPAT_RO)
98 return btrfs_super_compat_ro_flags(disk_super);
99 else
100 return btrfs_super_incompat_flags(disk_super);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400101}
102
Jeff Mahoneyba631942013-11-01 13:07:01 -0400103static void set_features(struct btrfs_fs_info *fs_info,
104 enum btrfs_feature_set set, u64 features)
105{
106 struct btrfs_super_block *disk_super = fs_info->super_copy;
107 if (set == FEAT_COMPAT)
108 btrfs_set_super_compat_flags(disk_super, features);
109 else if (set == FEAT_COMPAT_RO)
110 btrfs_set_super_compat_ro_flags(disk_super, features);
111 else
112 btrfs_set_super_incompat_flags(disk_super, features);
113}
114
115static int can_modify_feature(struct btrfs_feature_attr *fa)
116{
117 int val = 0;
118 u64 set, clear;
119 switch (fa->feature_set) {
120 case FEAT_COMPAT:
121 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
122 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
123 break;
124 case FEAT_COMPAT_RO:
125 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
126 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
127 break;
128 case FEAT_INCOMPAT:
129 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
130 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
131 break;
132 default:
Jeff Mahoney62e85572016-09-20 10:05:01 -0400133 pr_warn("btrfs: sysfs: unknown feature set %d\n",
David Sterbacc37bb02013-11-19 13:36:21 +0100134 fa->feature_set);
135 return 0;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400136 }
137
138 if (set & fa->feature_bit)
139 val |= 1;
140 if (clear & fa->feature_bit)
141 val |= 2;
142
143 return val;
144}
145
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400146static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
147 struct kobj_attribute *a, char *buf)
148{
Jeff Mahoney510d7362013-11-01 13:06:59 -0400149 int val = 0;
150 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400151 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400152 if (fs_info) {
Jeff Mahoney510d7362013-11-01 13:06:59 -0400153 u64 features = get_features(fs_info, fa->feature_set);
154 if (features & fa->feature_bit)
155 val = 1;
Jeff Mahoneyba631942013-11-01 13:07:01 -0400156 } else
157 val = can_modify_feature(fa);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400158
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100159 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400160}
161
Jeff Mahoneyba631942013-11-01 13:07:01 -0400162static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
163 struct kobj_attribute *a,
164 const char *buf, size_t count)
165{
166 struct btrfs_fs_info *fs_info;
167 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400168 u64 features, set, clear;
169 unsigned long val;
170 int ret;
171
172 fs_info = to_fs_info(kobj);
173 if (!fs_info)
174 return -EPERM;
175
David Howellsbc98a422017-07-17 08:45:34 +0100176 if (sb_rdonly(fs_info->sb))
David Sterbaee611132015-01-23 18:43:31 +0100177 return -EROFS;
178
Jeff Mahoneyba631942013-11-01 13:07:01 -0400179 ret = kstrtoul(skip_spaces(buf), 0, &val);
180 if (ret)
181 return ret;
182
183 if (fa->feature_set == FEAT_COMPAT) {
184 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
185 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
186 } else if (fa->feature_set == FEAT_COMPAT_RO) {
187 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
188 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
189 } else {
190 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
191 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
192 }
193
194 features = get_features(fs_info, fa->feature_set);
195
196 /* Nothing to do */
197 if ((val && (features & fa->feature_bit)) ||
198 (!val && !(features & fa->feature_bit)))
199 return count;
200
201 if ((val && !(set & fa->feature_bit)) ||
202 (!val && !(clear & fa->feature_bit))) {
203 btrfs_info(fs_info,
204 "%sabling feature %s on mounted fs is not supported.",
205 val ? "En" : "Dis", fa->kobj_attr.attr.name);
206 return -EPERM;
207 }
208
209 btrfs_info(fs_info, "%s %s feature flag",
210 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
211
Jeff Mahoneyba631942013-11-01 13:07:01 -0400212 spin_lock(&fs_info->super_lock);
213 features = get_features(fs_info, fa->feature_set);
214 if (val)
215 features |= fa->feature_bit;
216 else
217 features &= ~fa->feature_bit;
218 set_features(fs_info, fa->feature_set, features);
219 spin_unlock(&fs_info->super_lock);
220
David Sterba0eae2742014-11-12 14:22:21 +0100221 /*
222 * We don't want to do full transaction commit from inside sysfs
223 */
224 btrfs_set_pending(fs_info, COMMIT);
225 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyba631942013-11-01 13:07:01 -0400226
227 return count;
228}
229
Jeff Mahoney510d7362013-11-01 13:06:59 -0400230static umode_t btrfs_feature_visible(struct kobject *kobj,
231 struct attribute *attr, int unused)
232{
233 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
234 umode_t mode = attr->mode;
235
236 if (fs_info) {
237 struct btrfs_feature_attr *fa;
238 u64 features;
239
240 fa = attr_to_btrfs_feature_attr(attr);
241 features = get_features(fs_info, fa->feature_set);
242
Jeff Mahoneyba631942013-11-01 13:07:01 -0400243 if (can_modify_feature(fa))
244 mode |= S_IWUSR;
245 else if (!(features & fa->feature_bit))
Jeff Mahoney510d7362013-11-01 13:06:59 -0400246 mode = 0;
247 }
248
249 return mode;
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400250}
251
252BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
253BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
254BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
255BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
Nick Terrell5c1aab12017-08-09 19:39:02 -0700256BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400257BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
258BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
259BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
260BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
David Sterbac736c092014-01-21 18:56:09 +0100261BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200262BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
David Sterba3b5bb732016-01-21 18:36:46 +0100263BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
David Sterbacfbb8252018-07-10 18:15:05 +0200264BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400265
266static struct attribute *btrfs_supported_feature_attrs[] = {
267 BTRFS_FEAT_ATTR_PTR(mixed_backref),
268 BTRFS_FEAT_ATTR_PTR(default_subvol),
269 BTRFS_FEAT_ATTR_PTR(mixed_groups),
270 BTRFS_FEAT_ATTR_PTR(compress_lzo),
Nick Terrell5c1aab12017-08-09 19:39:02 -0700271 BTRFS_FEAT_ATTR_PTR(compress_zstd),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400272 BTRFS_FEAT_ATTR_PTR(big_metadata),
273 BTRFS_FEAT_ATTR_PTR(extended_iref),
274 BTRFS_FEAT_ATTR_PTR(raid56),
275 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
David Sterbac736c092014-01-21 18:56:09 +0100276 BTRFS_FEAT_ATTR_PTR(no_holes),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200277 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
David Sterba3b5bb732016-01-21 18:36:46 +0100278 BTRFS_FEAT_ATTR_PTR(free_space_tree),
David Sterbacfbb8252018-07-10 18:15:05 +0200279 BTRFS_FEAT_ATTR_PTR(raid1c34),
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400280 NULL
281};
282
Misono Tomohirof902bd32018-05-17 14:24:51 +0900283/*
284 * Features which depend on feature bits and may differ between each fs.
285 *
286 * /sys/fs/btrfs/features lists all available features of this kernel while
287 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
288 * can be changed online.
289 */
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400290static const struct attribute_group btrfs_feature_attr_group = {
291 .name = "features",
Jeff Mahoney510d7362013-11-01 13:06:59 -0400292 .is_visible = btrfs_feature_visible,
Jeff Mahoney079b72b2013-11-01 13:06:57 -0400293 .attrs = btrfs_supported_feature_attrs,
294};
Josef Bacik58176a92007-08-29 15:47:34 -0400295
Misono Tomohirof902bd32018-05-17 14:24:51 +0900296static ssize_t rmdir_subvol_show(struct kobject *kobj,
297 struct kobj_attribute *ka, char *buf)
298{
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100299 return scnprintf(buf, PAGE_SIZE, "0\n");
Misono Tomohirof902bd32018-05-17 14:24:51 +0900300}
301BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
302
David Sterbaf7cea562019-10-07 11:11:03 +0200303static ssize_t supported_checksums_show(struct kobject *kobj,
304 struct kobj_attribute *a, char *buf)
305{
306 ssize_t ret = 0;
307 int i;
308
309 for (i = 0; i < btrfs_get_num_csums(); i++) {
310 /*
311 * This "trick" only works as long as 'enum btrfs_csum_type' has
312 * no holes in it
313 */
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100314 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
David Sterbaf7cea562019-10-07 11:11:03 +0200315 (i == 0 ? "" : " "), btrfs_super_csum_name(i));
316
317 }
318
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100319 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
David Sterbaf7cea562019-10-07 11:11:03 +0200320 return ret;
321}
322BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
323
Misono Tomohirof902bd32018-05-17 14:24:51 +0900324static struct attribute *btrfs_supported_static_feature_attrs[] = {
325 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
David Sterbaf7cea562019-10-07 11:11:03 +0200326 BTRFS_ATTR_PTR(static_feature, supported_checksums),
Misono Tomohirof902bd32018-05-17 14:24:51 +0900327 NULL
328};
329
330/*
331 * Features which only depend on kernel version.
332 *
333 * These are listed in /sys/fs/btrfs/features along with
334 * btrfs_feature_attr_group
335 */
336static const struct attribute_group btrfs_static_feature_attr_group = {
337 .name = "features",
338 .attrs = btrfs_supported_static_feature_attrs,
339};
340
David Sterba6e369fe2019-06-13 17:23:02 +0200341#ifdef CONFIG_BTRFS_DEBUG
342
343/*
Dennis Zhoue4faab82019-12-13 16:22:19 -0800344 * Discard statistics and tunables
345 */
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800346#define discard_to_fs_info(_kobj) to_fs_info((_kobj)->parent->parent)
347
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800348static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
349 struct kobj_attribute *a,
350 char *buf)
351{
352 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
353
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100354 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800355 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
356}
357BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
358
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800359static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
360 struct kobj_attribute *a,
361 char *buf)
362{
363 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
364
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100365 return scnprintf(buf, PAGE_SIZE, "%d\n",
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800366 atomic_read(&fs_info->discard_ctl.discardable_extents));
367}
368BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
369
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500370static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
371 struct kobj_attribute *a,
372 char *buf)
373{
374 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
375
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100376 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500377 fs_info->discard_ctl.discard_bitmap_bytes);
378}
379BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
380
381static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
382 struct kobj_attribute *a,
383 char *buf)
384{
385 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
386
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100387 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500388 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
389}
390BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
391
392static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
393 struct kobj_attribute *a,
394 char *buf)
395{
396 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
397
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100398 return scnprintf(buf, PAGE_SIZE, "%lld\n",
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500399 fs_info->discard_ctl.discard_extent_bytes);
400}
401BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
402
Dennis Zhoua2309302020-01-02 16:26:35 -0500403static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
404 struct kobj_attribute *a,
405 char *buf)
406{
407 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
408
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100409 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoua2309302020-01-02 16:26:35 -0500410 READ_ONCE(fs_info->discard_ctl.iops_limit));
411}
412
413static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
414 struct kobj_attribute *a,
415 const char *buf, size_t len)
416{
417 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
418 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
419 u32 iops_limit;
420 int ret;
421
422 ret = kstrtou32(buf, 10, &iops_limit);
423 if (ret)
424 return -EINVAL;
425
426 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
427
428 return len;
429}
430BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
431 btrfs_discard_iops_limit_store);
432
Dennis Zhoue93591b2020-01-02 16:26:36 -0500433static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
434 struct kobj_attribute *a,
435 char *buf)
436{
437 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
438
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100439 return scnprintf(buf, PAGE_SIZE, "%u\n",
Dennis Zhoue93591b2020-01-02 16:26:36 -0500440 READ_ONCE(fs_info->discard_ctl.kbps_limit));
441}
442
443static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
444 struct kobj_attribute *a,
445 const char *buf, size_t len)
446{
447 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
448 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
449 u32 kbps_limit;
450 int ret;
451
452 ret = kstrtou32(buf, 10, &kbps_limit);
453 if (ret)
454 return -EINVAL;
455
456 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
457
458 return len;
459}
460BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
461 btrfs_discard_kbps_limit_store);
462
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500463static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
464 struct kobj_attribute *a,
465 char *buf)
466{
467 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
468
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100469 return scnprintf(buf, PAGE_SIZE, "%llu\n",
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500470 READ_ONCE(fs_info->discard_ctl.max_discard_size));
471}
472
473static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
474 struct kobj_attribute *a,
475 const char *buf, size_t len)
476{
477 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
478 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
479 u64 max_discard_size;
480 int ret;
481
482 ret = kstrtou64(buf, 10, &max_discard_size);
483 if (ret)
484 return -EINVAL;
485
486 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
487
488 return len;
489}
490BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
491 btrfs_discard_max_discard_size_store);
492
Dennis Zhoue4faab82019-12-13 16:22:19 -0800493static const struct attribute *discard_debug_attrs[] = {
Dennis Zhou5dc7c102019-12-13 16:22:21 -0800494 BTRFS_ATTR_PTR(discard, discardable_bytes),
Dennis Zhoudfb79dd2019-12-13 16:22:20 -0800495 BTRFS_ATTR_PTR(discard, discardable_extents),
Dennis Zhou9ddf6482020-01-02 16:26:41 -0500496 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
497 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
498 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
Dennis Zhoua2309302020-01-02 16:26:35 -0500499 BTRFS_ATTR_PTR(discard, iops_limit),
Dennis Zhoue93591b2020-01-02 16:26:36 -0500500 BTRFS_ATTR_PTR(discard, kbps_limit),
Dennis Zhou19b2a2c2020-01-02 16:26:38 -0500501 BTRFS_ATTR_PTR(discard, max_discard_size),
Dennis Zhoue4faab82019-12-13 16:22:19 -0800502 NULL,
503};
504
505/*
David Sterba6e369fe2019-06-13 17:23:02 +0200506 * Runtime debugging exported via sysfs
507 *
508 * /sys/fs/btrfs/debug - applies to module or all filesystems
509 * /sys/fs/btrfs/UUID - applies only to the given filesystem
510 */
Dennis Zhou93945cb2019-12-13 16:22:18 -0800511static const struct attribute *btrfs_debug_mount_attrs[] = {
512 NULL,
513};
514
David Sterba6e369fe2019-06-13 17:23:02 +0200515static struct attribute *btrfs_debug_feature_attrs[] = {
516 NULL
517};
518
519static const struct attribute_group btrfs_debug_feature_attr_group = {
520 .name = "debug",
521 .attrs = btrfs_debug_feature_attrs,
522};
523
524#endif
525
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400526static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
527{
528 u64 val;
529 if (lock)
530 spin_lock(lock);
531 val = *value_ptr;
532 if (lock)
533 spin_unlock(lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100534 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400535}
536
537static ssize_t global_rsv_size_show(struct kobject *kobj,
538 struct kobj_attribute *ka, char *buf)
539{
540 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
541 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
542 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
543}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200544BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400545
546static ssize_t global_rsv_reserved_show(struct kobject *kobj,
547 struct kobj_attribute *a, char *buf)
548{
549 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
550 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
551 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
552}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200553BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400554
555#define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400556#define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400557
558static ssize_t raid_bytes_show(struct kobject *kobj,
559 struct kobj_attribute *attr, char *buf);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200560BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
561BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400562
563static ssize_t raid_bytes_show(struct kobject *kobj,
564 struct kobj_attribute *attr, char *buf)
565
566{
567 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
David Sterba32da53862019-10-29 19:20:18 +0100568 struct btrfs_block_group *block_group;
Jeff Mahoney75cb3792018-03-20 15:25:26 -0400569 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400570 u64 val = 0;
571
572 down_read(&sinfo->groups_sem);
573 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200574 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
David Sterbab3470b52019-10-23 18:48:22 +0200575 val += block_group->length;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400576 else
David Sterbabf38be62019-10-23 18:48:11 +0200577 val += block_group->used;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400578 }
579 up_read(&sinfo->groups_sem);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100580 return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400581}
582
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400583static struct attribute *raid_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200584 BTRFS_ATTR_PTR(raid, total_bytes),
585 BTRFS_ATTR_PTR(raid, used_bytes),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400586 NULL
587};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400588ATTRIBUTE_GROUPS(raid);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400589
590static void release_raid_kobj(struct kobject *kobj)
591{
Jeff Mahoneyc1895442014-05-27 12:59:57 -0400592 kfree(to_raid_kobj(kobj));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400593}
594
David Sterba536ea452019-08-01 17:55:55 +0200595static struct kobj_type btrfs_raid_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400596 .sysfs_ops = &kobj_sysfs_ops,
597 .release = release_raid_kobj,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400598 .default_groups = raid_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400599};
600
601#define SPACE_INFO_ATTR(field) \
602static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
603 struct kobj_attribute *a, \
604 char *buf) \
605{ \
606 struct btrfs_space_info *sinfo = to_space_info(kobj); \
607 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
608} \
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200609BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400610
611static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
612 struct kobj_attribute *a,
613 char *buf)
614{
615 struct btrfs_space_info *sinfo = to_space_info(kobj);
616 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100617 return scnprintf(buf, PAGE_SIZE, "%lld\n", val);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400618}
619
620SPACE_INFO_ATTR(flags);
621SPACE_INFO_ATTR(total_bytes);
622SPACE_INFO_ATTR(bytes_used);
623SPACE_INFO_ATTR(bytes_pinned);
624SPACE_INFO_ATTR(bytes_reserved);
625SPACE_INFO_ATTR(bytes_may_use);
Wang Xiaoguangc1fd5c32016-06-21 11:12:56 +0800626SPACE_INFO_ATTR(bytes_readonly);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400627SPACE_INFO_ATTR(disk_used);
628SPACE_INFO_ATTR(disk_total);
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200629BTRFS_ATTR(space_info, total_bytes_pinned,
630 btrfs_space_info_show_total_bytes_pinned);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400631
632static struct attribute *space_info_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200633 BTRFS_ATTR_PTR(space_info, flags),
634 BTRFS_ATTR_PTR(space_info, total_bytes),
635 BTRFS_ATTR_PTR(space_info, bytes_used),
636 BTRFS_ATTR_PTR(space_info, bytes_pinned),
637 BTRFS_ATTR_PTR(space_info, bytes_reserved),
638 BTRFS_ATTR_PTR(space_info, bytes_may_use),
639 BTRFS_ATTR_PTR(space_info, bytes_readonly),
640 BTRFS_ATTR_PTR(space_info, disk_used),
641 BTRFS_ATTR_PTR(space_info, disk_total),
642 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400643 NULL,
644};
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400645ATTRIBUTE_GROUPS(space_info);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400646
647static void space_info_release(struct kobject *kobj)
648{
649 struct btrfs_space_info *sinfo = to_space_info(kobj);
650 percpu_counter_destroy(&sinfo->total_bytes_pinned);
651 kfree(sinfo);
652}
653
David Sterba27992d02019-08-01 17:55:55 +0200654static struct kobj_type space_info_ktype = {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400655 .sysfs_ops = &kobj_sysfs_ops,
656 .release = space_info_release,
Kimberly Brown7c7e3012019-05-02 13:34:45 -0400657 .default_groups = space_info_groups,
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400658};
659
660static const struct attribute *allocation_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200661 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
662 BTRFS_ATTR_PTR(allocation, global_rsv_size),
Jeff Mahoney6ab0a202013-11-01 13:07:04 -0400663 NULL,
664};
665
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400666static ssize_t btrfs_label_show(struct kobject *kobj,
667 struct kobj_attribute *a, char *buf)
668{
669 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900670 char *label = fs_info->super_copy->label;
David Sterbaee17fc82016-04-26 16:22:06 +0200671 ssize_t ret;
672
673 spin_lock(&fs_info->super_lock);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100674 ret = scnprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
David Sterbaee17fc82016-04-26 16:22:06 +0200675 spin_unlock(&fs_info->super_lock);
676
677 return ret;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400678}
679
680static ssize_t btrfs_label_store(struct kobject *kobj,
681 struct kobj_attribute *a,
682 const char *buf, size_t len)
683{
684 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900685 size_t p_len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400686
David Sterba66ac9fe2016-04-26 16:03:57 +0200687 if (!fs_info)
688 return -EPERM;
689
David Howellsbc98a422017-07-17 08:45:34 +0100690 if (sb_rdonly(fs_info->sb))
Anand Jain79aec2b2014-07-30 20:04:10 +0800691 return -EROFS;
692
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900693 /*
694 * p_len is the len until the first occurrence of either
695 * '\n' or '\0'
696 */
697 p_len = strcspn(buf, "\n");
698
699 if (p_len >= BTRFS_LABEL_SIZE)
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400700 return -EINVAL;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400701
David Sterbaa6f69dc2014-05-30 19:29:05 +0200702 spin_lock(&fs_info->super_lock);
Satoru Takeuchi48fcc3f2014-07-01 17:00:07 +0900703 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
704 memcpy(fs_info->super_copy->label, buf, p_len);
David Sterbaa6f69dc2014-05-30 19:29:05 +0200705 spin_unlock(&fs_info->super_lock);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400706
David Sterbaa6f69dc2014-05-30 19:29:05 +0200707 /*
708 * We don't want to do full transaction commit from inside sysfs
709 */
710 btrfs_set_pending(fs_info, COMMIT);
711 wake_up_process(fs_info->transaction_kthread);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400712
David Sterbaa6f69dc2014-05-30 19:29:05 +0200713 return len;
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400714}
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200715BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400716
David Sterbadf935892014-05-07 18:17:16 +0200717static ssize_t btrfs_nodesize_show(struct kobject *kobj,
718 struct kobj_attribute *a, char *buf)
719{
720 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
721
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100722 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
David Sterbadf935892014-05-07 18:17:16 +0200723}
724
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200725BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
David Sterbadf935892014-05-07 18:17:16 +0200726
727static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
728 struct kobj_attribute *a, char *buf)
729{
730 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
731
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100732 return scnprintf(buf, PAGE_SIZE, "%u\n",
733 fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200734}
735
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200736BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
David Sterbadf935892014-05-07 18:17:16 +0200737
738static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
739 struct kobj_attribute *a, char *buf)
740{
741 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
742
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100743 return scnprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
David Sterbadf935892014-05-07 18:17:16 +0200744}
745
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200746BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
David Sterbadf935892014-05-07 18:17:16 +0200747
Sargun Dhillon27234802017-05-11 21:18:03 +0000748static ssize_t quota_override_show(struct kobject *kobj,
749 struct kobj_attribute *a, char *buf)
750{
751 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
752 int quota_override;
753
754 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100755 return scnprintf(buf, PAGE_SIZE, "%d\n", quota_override);
Sargun Dhillon27234802017-05-11 21:18:03 +0000756}
757
758static ssize_t quota_override_store(struct kobject *kobj,
759 struct kobj_attribute *a,
760 const char *buf, size_t len)
761{
762 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
763 unsigned long knob;
764 int err;
765
766 if (!fs_info)
767 return -EPERM;
768
769 if (!capable(CAP_SYS_RESOURCE))
770 return -EPERM;
771
772 err = kstrtoul(buf, 10, &knob);
773 if (err)
774 return err;
775 if (knob > 1)
776 return -EINVAL;
777
778 if (knob)
779 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
780 else
781 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
782
783 return len;
784}
785
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200786BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
Sargun Dhillon27234802017-05-11 21:18:03 +0000787
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200788static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
789 struct kobj_attribute *a, char *buf)
790{
791 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
792
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100793 return scnprintf(buf, PAGE_SIZE, "%pU\n",
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200794 fs_info->fs_devices->metadata_uuid);
795}
796
797BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
798
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200799static ssize_t btrfs_checksum_show(struct kobject *kobj,
800 struct kobj_attribute *a, char *buf)
801{
802 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
803 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
804
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100805 return scnprintf(buf, PAGE_SIZE, "%s (%s)\n",
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200806 btrfs_super_csum_name(csum_type),
807 crypto_shash_driver_name(fs_info->csum_shash));
808}
809
810BTRFS_ATTR(, checksum, btrfs_checksum_show);
811
Anand Jain0dd29062015-03-10 06:38:27 +0800812static const struct attribute *btrfs_attrs[] = {
Hans van Kranenburga969f4c2017-10-08 22:30:58 +0200813 BTRFS_ATTR_PTR(, label),
814 BTRFS_ATTR_PTR(, nodesize),
815 BTRFS_ATTR_PTR(, sectorsize),
816 BTRFS_ATTR_PTR(, clone_alignment),
817 BTRFS_ATTR_PTR(, quota_override),
Nikolay Borisov56f20f42018-11-19 17:37:45 +0200818 BTRFS_ATTR_PTR(, metadata_uuid),
Johannes Thumshirn41e6d2a2019-10-07 11:11:04 +0200819 BTRFS_ATTR_PTR(, checksum),
Jeff Mahoneyf8ba9c12013-11-01 13:07:06 -0400820 NULL,
821};
822
Anand Jainc1b7e472015-08-14 18:32:50 +0800823static void btrfs_release_fsid_kobj(struct kobject *kobj)
Jeff Mahoney510d7362013-11-01 13:06:59 -0400824{
Anand Jain2e7910d2015-03-10 06:38:29 +0800825 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
Anand Jain248d2002015-03-10 06:38:19 +0800826
Anand Jainc1b7e472015-08-14 18:32:50 +0800827 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
Anand Jain2e7910d2015-03-10 06:38:29 +0800828 complete(&fs_devs->kobj_unregister);
Jeff Mahoney510d7362013-11-01 13:06:59 -0400829}
830
831static struct kobj_type btrfs_ktype = {
832 .sysfs_ops = &kobj_sysfs_ops,
Anand Jainc1b7e472015-08-14 18:32:50 +0800833 .release = btrfs_release_fsid_kobj,
Jeff Mahoney510d7362013-11-01 13:06:59 -0400834};
835
Anand Jain2e7910d2015-03-10 06:38:29 +0800836static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
837{
838 if (kobj->ktype != &btrfs_ktype)
839 return NULL;
Anand Jainc1b7e472015-08-14 18:32:50 +0800840 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
Anand Jain2e7910d2015-03-10 06:38:29 +0800841}
842
Jeff Mahoney510d7362013-11-01 13:06:59 -0400843static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
844{
845 if (kobj->ktype != &btrfs_ktype)
846 return NULL;
Anand Jain2e7910d2015-03-10 06:38:29 +0800847 return to_fs_devs(kobj)->fs_info;
Jeff Mahoney510d7362013-11-01 13:06:59 -0400848}
Josef Bacik58176a92007-08-29 15:47:34 -0400849
Jeff Mahoneye453d982013-11-21 10:37:16 -0500850#define NUM_FEATURE_BITS 64
Tomohiro Misono6c521572018-05-16 17:09:26 +0900851#define BTRFS_FEATURE_NAME_MAX 13
852static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
853static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
Jeff Mahoneye453d982013-11-21 10:37:16 -0500854
Tomohiro Misono6c521572018-05-16 17:09:26 +0900855static const u64 supported_feature_masks[FEAT_MAX] = {
Jeff Mahoneye453d982013-11-21 10:37:16 -0500856 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
857 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
858 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
859};
860
861static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400862{
Jeff Mahoneye453d982013-11-21 10:37:16 -0500863 int set;
864
865 for (set = 0; set < FEAT_MAX; set++) {
866 int i;
867 struct attribute *attrs[2];
868 struct attribute_group agroup = {
869 .name = "features",
870 .attrs = attrs,
871 };
872 u64 features = get_features(fs_info, set);
873 features &= ~supported_feature_masks[set];
874
875 if (!features)
876 continue;
877
878 attrs[1] = NULL;
879 for (i = 0; i < NUM_FEATURE_BITS; i++) {
880 struct btrfs_feature_attr *fa;
881
882 if (!(features & (1ULL << i)))
883 continue;
884
885 fa = &btrfs_feature_attrs[set][i];
886 attrs[0] = &fa->kobj_attr.attr;
887 if (add) {
888 int ret;
Anand Jainc1b7e472015-08-14 18:32:50 +0800889 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500890 &agroup);
891 if (ret)
892 return ret;
893 } else
Anand Jainc1b7e472015-08-14 18:32:50 +0800894 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
Jeff Mahoneye453d982013-11-21 10:37:16 -0500895 &agroup);
896 }
897
898 }
899 return 0;
900}
901
Anand Jain2e3e1282015-03-10 06:38:32 +0800902static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500903{
Anand Jaina013d142020-02-12 17:28:10 +0800904 if (fs_devs->devinfo_kobj) {
905 kobject_del(fs_devs->devinfo_kobj);
906 kobject_put(fs_devs->devinfo_kobj);
907 fs_devs->devinfo_kobj = NULL;
908 }
909
Anand Jainb5501502019-11-21 17:33:30 +0800910 if (fs_devs->devices_kobj) {
911 kobject_del(fs_devs->devices_kobj);
912 kobject_put(fs_devs->devices_kobj);
913 fs_devs->devices_kobj = NULL;
Anand Jainaaf13302015-03-10 06:38:24 +0800914 }
915
Anand Jainc1b7e472015-08-14 18:32:50 +0800916 if (fs_devs->fsid_kobj.state_initialized) {
917 kobject_del(&fs_devs->fsid_kobj);
918 kobject_put(&fs_devs->fsid_kobj);
Anand Jainf90fc542015-06-22 18:18:32 +0800919 wait_for_completion(&fs_devs->kobj_unregister);
920 }
Jeff Mahoney5ac1d202013-11-01 13:06:58 -0400921}
922
Anand Jain2e3e1282015-03-10 06:38:32 +0800923/* when fs_devs is NULL it will remove all fsid kobject */
Anand Jain1d1c1be2015-03-10 06:38:37 +0800924void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
Anand Jain2e3e1282015-03-10 06:38:32 +0800925{
926 struct list_head *fs_uuids = btrfs_get_fs_uuids();
927
928 if (fs_devs) {
929 __btrfs_sysfs_remove_fsid(fs_devs);
930 return;
931 }
932
Anand Jainc4babc52018-04-12 10:29:25 +0800933 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
Anand Jain2e3e1282015-03-10 06:38:32 +0800934 __btrfs_sysfs_remove_fsid(fs_devs);
935 }
936}
937
Anand Jain6618a592015-08-14 18:32:47 +0800938void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
Jeff Mahoneye453d982013-11-21 10:37:16 -0500939{
Nikolay Borisov3092c682020-07-03 11:13:15 +0300940 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
941
Anand Jain5a13f432015-03-10 06:38:31 +0800942 btrfs_reset_fs_info_ptr(fs_info);
943
Nikolay Borisov3092c682020-07-03 11:13:15 +0300944 sysfs_remove_link(fsid_kobj, "bdi");
945
Jeff Mahoneye453d982013-11-21 10:37:16 -0500946 if (fs_info->space_info_kobj) {
947 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
948 kobject_del(fs_info->space_info_kobj);
949 kobject_put(fs_info->space_info_kobj);
950 }
Dennis Zhou71e89782019-12-13 16:22:17 -0800951#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhoue4faab82019-12-13 16:22:19 -0800952 if (fs_info->discard_debug_kobj) {
953 sysfs_remove_files(fs_info->discard_debug_kobj,
954 discard_debug_attrs);
955 kobject_del(fs_info->discard_debug_kobj);
956 kobject_put(fs_info->discard_debug_kobj);
957 }
Dennis Zhou93945cb2019-12-13 16:22:18 -0800958 if (fs_info->debug_kobj) {
959 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
960 kobject_del(fs_info->debug_kobj);
961 kobject_put(fs_info->debug_kobj);
962 }
Dennis Zhou71e89782019-12-13 16:22:17 -0800963#endif
Jeff Mahoneye453d982013-11-21 10:37:16 -0500964 addrm_unknown_feature_attrs(fs_info, false);
Nikolay Borisov3092c682020-07-03 11:13:15 +0300965 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
966 sysfs_remove_files(fsid_kobj, btrfs_attrs);
Anand Jainf3cd2c52020-02-12 17:28:13 +0800967 btrfs_sysfs_remove_devices_dir(fs_info->fs_devices, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -0500968}
969
David Sterbaf10152b2019-08-01 19:07:55 +0200970static const char * const btrfs_feature_set_names[FEAT_MAX] = {
Jeff Mahoney79da4fa2013-11-01 13:07:00 -0400971 [FEAT_COMPAT] = "compat",
972 [FEAT_COMPAT_RO] = "compat_ro",
973 [FEAT_INCOMPAT] = "incompat",
974};
975
David Sterbaf10152b2019-08-01 19:07:55 +0200976const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
977{
978 return btrfs_feature_set_names[set];
979}
980
Jeff Mahoney3b02a682013-11-01 13:07:02 -0400981char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
982{
983 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
984 int len = 0;
985 int i;
986 char *str;
987
988 str = kmalloc(bufsize, GFP_KERNEL);
989 if (!str)
990 return str;
991
992 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
993 const char *name;
994
995 if (!(flags & (1ULL << i)))
996 continue;
997
998 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +0100999 len += scnprintf(str + len, bufsize - len, "%s%s",
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001000 len ? "," : "", name);
1001 }
1002
1003 return str;
1004}
1005
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001006static void init_feature_attrs(void)
1007{
1008 struct btrfs_feature_attr *fa;
1009 int set, i;
1010
1011 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
1012 ARRAY_SIZE(btrfs_feature_attrs));
1013 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
1014 ARRAY_SIZE(btrfs_feature_attrs[0]));
1015
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001016 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1017 memset(btrfs_unknown_feature_names, 0,
1018 sizeof(btrfs_unknown_feature_names));
1019
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001020 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1021 struct btrfs_feature_attr *sfa;
1022 struct attribute *a = btrfs_supported_feature_attrs[i];
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001023 int bit;
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001024 sfa = attr_to_btrfs_feature_attr(a);
Jeff Mahoney3b02a682013-11-01 13:07:02 -04001025 bit = ilog2(sfa->feature_bit);
1026 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001027
1028 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1029 }
1030
1031 for (set = 0; set < FEAT_MAX; set++) {
1032 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1033 char *name = btrfs_unknown_feature_names[set][i];
1034 fa = &btrfs_feature_attrs[set][i];
1035
1036 if (fa->kobj_attr.attr.name)
1037 continue;
1038
Tomohiro Misono6c521572018-05-16 17:09:26 +09001039 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001040 btrfs_feature_set_names[set], i);
1041
1042 fa->kobj_attr.attr.name = name;
1043 fa->kobj_attr.attr.mode = S_IRUGO;
1044 fa->feature_set = set;
1045 fa->feature_bit = 1ULL << i;
1046 }
1047 }
1048}
1049
David Sterba32a99912019-08-01 17:49:55 +02001050/*
1051 * Create a sysfs entry for a given block group type at path
1052 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1053 */
David Sterba32da53862019-10-29 19:20:18 +01001054void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
David Sterba32a99912019-08-01 17:49:55 +02001055{
1056 struct btrfs_fs_info *fs_info = cache->fs_info;
1057 struct btrfs_space_info *space_info = cache->space_info;
1058 struct raid_kobject *rkobj;
1059 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1060 unsigned int nofs_flag;
1061 int ret;
1062
1063 /*
1064 * Setup a NOFS context because kobject_add(), deep in its call chain,
1065 * does GFP_KERNEL allocations, and we are often called in a context
1066 * where if reclaim is triggered we can deadlock (we are either holding
1067 * a transaction handle or some lock required for a transaction
1068 * commit).
1069 */
1070 nofs_flag = memalloc_nofs_save();
1071
1072 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1073 if (!rkobj) {
1074 memalloc_nofs_restore(nofs_flag);
1075 btrfs_warn(cache->fs_info,
1076 "couldn't alloc memory for raid level kobject");
1077 return;
1078 }
1079
1080 rkobj->flags = cache->flags;
1081 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1082 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1083 btrfs_bg_type_to_raid_name(rkobj->flags));
1084 memalloc_nofs_restore(nofs_flag);
1085 if (ret) {
1086 kobject_put(&rkobj->kobj);
1087 btrfs_warn(fs_info,
1088 "failed to add kobject for block cache, ignoring");
1089 return;
1090 }
1091
1092 space_info->block_group_kobjs[index] = &rkobj->kobj;
1093}
1094
David Sterbab5865ba2019-08-01 18:50:16 +02001095/*
1096 * Remove sysfs directories for all block group types of a given space info and
1097 * the space info as well
1098 */
1099void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1100{
1101 int i;
1102
1103 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1104 struct kobject *kobj;
1105
1106 kobj = space_info->block_group_kobjs[i];
1107 space_info->block_group_kobjs[i] = NULL;
1108 if (kobj) {
1109 kobject_del(kobj);
1110 kobject_put(kobj);
1111 }
1112 }
1113 kobject_del(&space_info->kobj);
1114 kobject_put(&space_info->kobj);
1115}
1116
David Sterbab8823272019-08-01 18:50:16 +02001117static const char *alloc_name(u64 flags)
1118{
1119 switch (flags) {
1120 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1121 return "mixed";
1122 case BTRFS_BLOCK_GROUP_METADATA:
1123 return "metadata";
1124 case BTRFS_BLOCK_GROUP_DATA:
1125 return "data";
1126 case BTRFS_BLOCK_GROUP_SYSTEM:
1127 return "system";
1128 default:
1129 WARN_ON(1);
1130 return "invalid-combination";
1131 };
1132}
1133
1134/*
1135 * Create a sysfs entry for a space info type at path
1136 * /sys/fs/btrfs/UUID/allocation/TYPE
1137 */
1138int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1139 struct btrfs_space_info *space_info)
1140{
1141 int ret;
1142
1143 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1144 fs_info->space_info_kobj, "%s",
1145 alloc_name(space_info->flags));
1146 if (ret) {
1147 kobject_put(&space_info->kobj);
1148 return ret;
1149 }
1150
1151 return 0;
1152}
1153
Anand Jaine7e1aa92015-03-10 06:38:21 +08001154/* when one_device is NULL, it removes all device links */
1155
Anand Jainf3cd2c52020-02-12 17:28:13 +08001156int btrfs_sysfs_remove_devices_dir(struct btrfs_fs_devices *fs_devices,
Anand Jain99994cd2014-06-03 11:36:00 +08001157 struct btrfs_device *one_device)
1158{
1159 struct hd_struct *disk;
1160 struct kobject *disk_kobj;
1161
Anand Jainb5501502019-11-21 17:33:30 +08001162 if (!fs_devices->devices_kobj)
Anand Jain99994cd2014-06-03 11:36:00 +08001163 return -EINVAL;
1164
Anand Jain668e48af2020-01-06 19:38:31 +08001165 if (one_device) {
1166 if (one_device->bdev) {
1167 disk = one_device->bdev->bd_part;
1168 disk_kobj = &part_to_dev(disk)->kobj;
1169 sysfs_remove_link(fs_devices->devices_kobj,
1170 disk_kobj->name);
1171 }
Anand Jain99994cd2014-06-03 11:36:00 +08001172
Anand Jainb5ddcff2020-09-05 01:34:21 +08001173 if (one_device->devid_kobj.state_initialized) {
1174 kobject_del(&one_device->devid_kobj);
1175 kobject_put(&one_device->devid_kobj);
Anand Jain668e48af2020-01-06 19:38:31 +08001176
Anand Jainb5ddcff2020-09-05 01:34:21 +08001177 wait_for_completion(&one_device->kobj_unregister);
1178 }
Anand Jain668e48af2020-01-06 19:38:31 +08001179
1180 return 0;
Anand Jain99994cd2014-06-03 11:36:00 +08001181 }
1182
Anand Jain668e48af2020-01-06 19:38:31 +08001183 list_for_each_entry(one_device, &fs_devices->devices, dev_list) {
Anand Jaine7e1aa92015-03-10 06:38:21 +08001184
Anand Jain668e48af2020-01-06 19:38:31 +08001185 if (one_device->bdev) {
1186 disk = one_device->bdev->bd_part;
1187 disk_kobj = &part_to_dev(disk)->kobj;
1188 sysfs_remove_link(fs_devices->devices_kobj,
1189 disk_kobj->name);
1190 }
Anand Jainb5ddcff2020-09-05 01:34:21 +08001191 if (one_device->devid_kobj.state_initialized) {
1192 kobject_del(&one_device->devid_kobj);
1193 kobject_put(&one_device->devid_kobj);
Anand Jaine7e1aa92015-03-10 06:38:21 +08001194
Anand Jainb5ddcff2020-09-05 01:34:21 +08001195 wait_for_completion(&one_device->kobj_unregister);
1196 }
Anand Jaine7e1aa92015-03-10 06:38:21 +08001197 }
1198
Anand Jain99994cd2014-06-03 11:36:00 +08001199 return 0;
1200}
1201
Anand Jain668e48af2020-01-06 19:38:31 +08001202static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1203 struct kobj_attribute *a,
1204 char *buf)
1205{
1206 int val;
1207 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1208 devid_kobj);
1209
1210 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1211
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001212 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001213}
1214BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1215
Anand Jain25864772020-02-13 16:40:53 +08001216static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
Anand Jain668e48af2020-01-06 19:38:31 +08001217 struct kobj_attribute *a, char *buf)
1218{
1219 int val;
1220 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1221 devid_kobj);
1222
1223 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1224
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001225 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001226}
Anand Jain25864772020-02-13 16:40:53 +08001227BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
Anand Jain668e48af2020-01-06 19:38:31 +08001228
1229static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1230 struct kobj_attribute *a,
1231 char *buf)
1232{
1233 int val;
1234 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1235 devid_kobj);
1236
1237 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1238
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001239 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001240}
1241BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1242
1243static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1244 struct kobj_attribute *a, char *buf)
1245{
1246 int val;
1247 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1248 devid_kobj);
1249
1250 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1251
Takashi Iwaiabdd9fe2020-03-22 10:09:11 +01001252 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
Anand Jain668e48af2020-01-06 19:38:31 +08001253}
1254BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1255
1256static struct attribute *devid_attrs[] = {
1257 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1258 BTRFS_ATTR_PTR(devid, missing),
1259 BTRFS_ATTR_PTR(devid, replace_target),
1260 BTRFS_ATTR_PTR(devid, writeable),
1261 NULL
1262};
1263ATTRIBUTE_GROUPS(devid);
1264
1265static void btrfs_release_devid_kobj(struct kobject *kobj)
1266{
1267 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1268 devid_kobj);
1269
1270 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1271 complete(&device->kobj_unregister);
1272}
1273
1274static struct kobj_type devid_ktype = {
1275 .sysfs_ops = &kobj_sysfs_ops,
1276 .default_groups = devid_groups,
1277 .release = btrfs_release_devid_kobj,
1278};
1279
Anand Jainf3cd2c52020-02-12 17:28:13 +08001280int btrfs_sysfs_add_devices_dir(struct btrfs_fs_devices *fs_devices,
Anand Jain1ba43812015-03-10 06:38:33 +08001281 struct btrfs_device *one_device)
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001282{
1283 int error = 0;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001284 struct btrfs_device *dev;
Josef Bacika47bd782020-07-21 10:17:50 -04001285 unsigned int nofs_flag;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001286
Josef Bacika47bd782020-07-21 10:17:50 -04001287 nofs_flag = memalloc_nofs_save();
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001288 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Anand Jainf0853812014-01-15 17:22:28 +08001289
Anand Jain0d393762014-06-03 11:36:01 +08001290 if (one_device && one_device != dev)
1291 continue;
1292
Anand Jain668e48af2020-01-06 19:38:31 +08001293 if (dev->bdev) {
1294 struct hd_struct *disk;
1295 struct kobject *disk_kobj;
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001296
Anand Jain668e48af2020-01-06 19:38:31 +08001297 disk = dev->bdev->bd_part;
1298 disk_kobj = &part_to_dev(disk)->kobj;
1299
1300 error = sysfs_create_link(fs_devices->devices_kobj,
1301 disk_kobj, disk_kobj->name);
1302 if (error)
1303 break;
1304 }
1305
1306 init_completion(&dev->kobj_unregister);
1307 error = kobject_init_and_add(&dev->devid_kobj, &devid_ktype,
Anand Jain1b9867e2020-02-12 17:28:11 +08001308 fs_devices->devinfo_kobj, "%llu",
Anand Jain668e48af2020-01-06 19:38:31 +08001309 dev->devid);
1310 if (error) {
1311 kobject_put(&dev->devid_kobj);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001312 break;
Anand Jain668e48af2020-01-06 19:38:31 +08001313 }
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001314 }
Josef Bacika47bd782020-07-21 10:17:50 -04001315 memalloc_nofs_restore(nofs_flag);
Jeff Mahoney29e5be22013-11-01 13:07:05 -04001316
1317 return error;
1318}
1319
David Sterba5b286922019-08-01 18:50:16 +02001320void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1321{
1322 int ret;
1323
1324 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1325 if (ret)
1326 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1327 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1328 &disk_to_dev(bdev->bd_disk)->kobj);
1329}
1330
Nikolay Borisov8e560082020-08-12 16:18:51 +03001331void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1332
David Sterbaf93c3992019-08-01 18:50:16 +02001333{
1334 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1335
1336 /*
1337 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1338 * directory
1339 */
Nikolay Borisov8e560082020-08-12 16:18:51 +03001340 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
David Sterbaf93c3992019-08-01 18:50:16 +02001341 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1342 btrfs_warn(fs_devices->fs_info,
1343 "sysfs: failed to create fsid for sprout");
1344}
1345
Anand Jain668e48af2020-01-06 19:38:31 +08001346void btrfs_sysfs_update_devid(struct btrfs_device *device)
1347{
1348 char tmp[24];
1349
1350 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1351
1352 if (kobject_rename(&device->devid_kobj, tmp))
1353 btrfs_warn(device->fs_devices->fs_info,
1354 "sysfs: failed to update devid for %llu",
1355 device->devid);
1356}
1357
Jeff Mahoney510d7362013-11-01 13:06:59 -04001358/* /sys/fs/btrfs/ entry */
1359static struct kset *btrfs_kset;
1360
Anand Jain72059212015-03-10 06:38:26 +08001361/*
Anand Jainc6761a92019-11-21 17:33:32 +08001362 * Creates:
1363 * /sys/fs/btrfs/UUID
1364 *
Anand Jain72059212015-03-10 06:38:26 +08001365 * Can be called by the device discovery thread.
Anand Jain72059212015-03-10 06:38:26 +08001366 */
Anand Jainc6761a92019-11-21 17:33:32 +08001367int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001368{
1369 int error;
1370
Anand Jain2e7910d2015-03-10 06:38:29 +08001371 init_completion(&fs_devs->kobj_unregister);
Anand Jainc1b7e472015-08-14 18:32:50 +08001372 fs_devs->fsid_kobj.kset = btrfs_kset;
Anand Jainc6761a92019-11-21 17:33:32 +08001373 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1374 "%pU", fs_devs->fsid);
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001375 if (error) {
1376 kobject_put(&fs_devs->fsid_kobj);
1377 return error;
1378 }
1379
Anand Jainbc036bb2019-11-21 17:33:34 +08001380 fs_devs->devices_kobj = kobject_create_and_add("devices",
1381 &fs_devs->fsid_kobj);
1382 if (!fs_devs->devices_kobj) {
1383 btrfs_err(fs_devs->fs_info,
1384 "failed to init sysfs device interface");
Anand Jain1f6087e2020-02-12 17:28:12 +08001385 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainbc036bb2019-11-21 17:33:34 +08001386 return -ENOMEM;
1387 }
1388
Anand Jaina013d142020-02-12 17:28:10 +08001389 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1390 &fs_devs->fsid_kobj);
1391 if (!fs_devs->devinfo_kobj) {
1392 btrfs_err(fs_devs->fs_info,
1393 "failed to init sysfs devinfo kobject");
1394 btrfs_sysfs_remove_fsid(fs_devs);
1395 return -ENOMEM;
1396 }
1397
Tobin C. Hardinge3277332019-05-13 13:39:12 +10001398 return 0;
Anand Jain72059212015-03-10 06:38:26 +08001399}
1400
Anand Jain96f31362015-08-14 18:32:46 +08001401int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
Anand Jain72059212015-03-10 06:38:26 +08001402{
1403 int error;
Anand Jain2e7910d2015-03-10 06:38:29 +08001404 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
Anand Jainc1b7e472015-08-14 18:32:50 +08001405 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
Anand Jain72059212015-03-10 06:38:26 +08001406
Anand Jain5a13f432015-03-10 06:38:31 +08001407 btrfs_set_fs_info_ptr(fs_info);
1408
Anand Jainf3cd2c52020-02-12 17:28:13 +08001409 error = btrfs_sysfs_add_devices_dir(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001410 if (error)
1411 return error;
Jeff Mahoney510d7362013-11-01 13:06:59 -04001412
Anand Jainc1b7e472015-08-14 18:32:50 +08001413 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001414 if (error) {
Anand Jainf3cd2c52020-02-12 17:28:13 +08001415 btrfs_sysfs_remove_devices_dir(fs_devs, NULL);
Jeff Mahoneye453d982013-11-21 10:37:16 -05001416 return error;
1417 }
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001418
Anand Jainc1b7e472015-08-14 18:32:50 +08001419 error = sysfs_create_group(fsid_kobj,
Anand Jain0dd29062015-03-10 06:38:27 +08001420 &btrfs_feature_attr_group);
1421 if (error)
1422 goto failure;
1423
David Sterba6e369fe2019-06-13 17:23:02 +02001424#ifdef CONFIG_BTRFS_DEBUG
Dennis Zhou93945cb2019-12-13 16:22:18 -08001425 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1426 if (!fs_info->debug_kobj) {
1427 error = -ENOMEM;
1428 goto failure;
1429 }
1430
1431 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
David Sterba6e369fe2019-06-13 17:23:02 +02001432 if (error)
1433 goto failure;
Dennis Zhoue4faab82019-12-13 16:22:19 -08001434
1435 /* Discard directory */
1436 fs_info->discard_debug_kobj = kobject_create_and_add("discard",
1437 fs_info->debug_kobj);
1438 if (!fs_info->discard_debug_kobj) {
1439 error = -ENOMEM;
1440 goto failure;
1441 }
1442
1443 error = sysfs_create_files(fs_info->discard_debug_kobj,
1444 discard_debug_attrs);
1445 if (error)
1446 goto failure;
David Sterba6e369fe2019-06-13 17:23:02 +02001447#endif
1448
Jeff Mahoneye453d982013-11-21 10:37:16 -05001449 error = addrm_unknown_feature_attrs(fs_info, true);
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001450 if (error)
1451 goto failure;
1452
Nikolay Borisov3092c682020-07-03 11:13:15 +03001453 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1454 if (error)
1455 goto failure;
1456
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001457 fs_info->space_info_kobj = kobject_create_and_add("allocation",
Anand Jainc1b7e472015-08-14 18:32:50 +08001458 fsid_kobj);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04001459 if (!fs_info->space_info_kobj) {
1460 error = -ENOMEM;
1461 goto failure;
1462 }
1463
1464 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1465 if (error)
1466 goto failure;
1467
Jeff Mahoney79da4fa2013-11-01 13:07:00 -04001468 return 0;
1469failure:
Anand Jain6618a592015-08-14 18:32:47 +08001470 btrfs_sysfs_remove_mounted(fs_info);
Jeff Mahoney5ac1d202013-11-01 13:06:58 -04001471 return error;
1472}
1473
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001474static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
1475{
1476 return to_fs_info(kobj->parent->parent);
1477}
1478
1479#define QGROUP_ATTR(_member, _show_name) \
1480static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
1481 struct kobj_attribute *a, \
1482 char *buf) \
1483{ \
1484 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1485 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1486 struct btrfs_qgroup, kobj); \
1487 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
1488} \
1489BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
1490
1491#define QGROUP_RSV_ATTR(_name, _type) \
1492static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
1493 struct kobj_attribute *a, \
1494 char *buf) \
1495{ \
1496 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
1497 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
1498 struct btrfs_qgroup, kobj); \
1499 return btrfs_show_u64(&qgroup->rsv.values[_type], \
1500 &fs_info->qgroup_lock, buf); \
1501} \
1502BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
1503
1504QGROUP_ATTR(rfer, referenced);
1505QGROUP_ATTR(excl, exclusive);
1506QGROUP_ATTR(max_rfer, max_referenced);
1507QGROUP_ATTR(max_excl, max_exclusive);
1508QGROUP_ATTR(lim_flags, limit_flags);
1509QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
1510QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
1511QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
1512
1513static struct attribute *qgroup_attrs[] = {
1514 BTRFS_ATTR_PTR(qgroup, referenced),
1515 BTRFS_ATTR_PTR(qgroup, exclusive),
1516 BTRFS_ATTR_PTR(qgroup, max_referenced),
1517 BTRFS_ATTR_PTR(qgroup, max_exclusive),
1518 BTRFS_ATTR_PTR(qgroup, limit_flags),
1519 BTRFS_ATTR_PTR(qgroup, rsv_data),
1520 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
1521 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
1522 NULL
1523};
1524ATTRIBUTE_GROUPS(qgroup);
1525
1526static void qgroup_release(struct kobject *kobj)
1527{
1528 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
1529
1530 memset(&qgroup->kobj, 0, sizeof(*kobj));
1531}
1532
1533static struct kobj_type qgroup_ktype = {
1534 .sysfs_ops = &kobj_sysfs_ops,
1535 .release = qgroup_release,
1536 .default_groups = qgroup_groups,
1537};
1538
1539int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
1540 struct btrfs_qgroup *qgroup)
1541{
1542 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
1543 int ret;
1544
1545 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1546 return 0;
1547 if (qgroup->kobj.state_initialized)
1548 return 0;
1549 if (!qgroups_kobj)
1550 return -EINVAL;
1551
1552 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
1553 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
1554 btrfs_qgroup_subvolid(qgroup->qgroupid));
1555 if (ret < 0)
1556 kobject_put(&qgroup->kobj);
1557
1558 return ret;
1559}
1560
1561void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
1562{
1563 struct btrfs_qgroup *qgroup;
1564 struct btrfs_qgroup *next;
1565
1566 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1567 return;
1568
1569 rbtree_postorder_for_each_entry_safe(qgroup, next,
1570 &fs_info->qgroup_tree, node)
1571 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
Qu Wenruo62ab2cc2020-08-03 14:20:11 +08001572 if (fs_info->qgroups_kobj) {
1573 kobject_del(fs_info->qgroups_kobj);
1574 kobject_put(fs_info->qgroups_kobj);
1575 fs_info->qgroups_kobj = NULL;
1576 }
Qu Wenruo49e5fb42020-06-28 13:07:15 +08001577}
1578
1579/* Called when qgroups get initialized, thus there is no need for locking */
1580int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
1581{
1582 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1583 struct btrfs_qgroup *qgroup;
1584 struct btrfs_qgroup *next;
1585 int ret = 0;
1586
1587 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1588 return 0;
1589
1590 ASSERT(fsid_kobj);
1591 if (fs_info->qgroups_kobj)
1592 return 0;
1593
1594 fs_info->qgroups_kobj = kobject_create_and_add("qgroups", fsid_kobj);
1595 if (!fs_info->qgroups_kobj) {
1596 ret = -ENOMEM;
1597 goto out;
1598 }
1599 rbtree_postorder_for_each_entry_safe(qgroup, next,
1600 &fs_info->qgroup_tree, node) {
1601 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
1602 if (ret < 0)
1603 goto out;
1604 }
1605
1606out:
1607 if (ret < 0)
1608 btrfs_sysfs_del_qgroups(fs_info);
1609 return ret;
1610}
1611
1612void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
1613 struct btrfs_qgroup *qgroup)
1614{
1615 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
1616 return;
1617
1618 if (qgroup->kobj.state_initialized) {
1619 kobject_del(&qgroup->kobj);
1620 kobject_put(&qgroup->kobj);
1621 }
1622}
David Sterba444e7512016-01-21 18:50:40 +01001623
1624/*
1625 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1626 * values in superblock. Call after any changes to incompat/compat_ro flags
1627 */
1628void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1629 u64 bit, enum btrfs_feature_set set)
1630{
1631 struct btrfs_fs_devices *fs_devs;
1632 struct kobject *fsid_kobj;
1633 u64 features;
1634 int ret;
1635
1636 if (!fs_info)
1637 return;
1638
1639 features = get_features(fs_info, set);
1640 ASSERT(bit & supported_feature_masks[set]);
1641
1642 fs_devs = fs_info->fs_devices;
1643 fsid_kobj = &fs_devs->fsid_kobj;
1644
David Sterbabf609202016-01-27 14:06:29 +01001645 if (!fsid_kobj->state_initialized)
1646 return;
1647
David Sterba444e7512016-01-21 18:50:40 +01001648 /*
1649 * FIXME: this is too heavy to update just one value, ideally we'd like
1650 * to use sysfs_update_group but some refactoring is needed first.
1651 */
1652 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1653 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1654}
1655
Liu Bof5c29bd2017-11-02 17:21:50 -06001656int __init btrfs_init_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001657{
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001658 int ret;
David Sterba1bae3092014-02-05 15:36:18 +01001659
Greg KHe3fe4e72008-02-20 14:14:16 -05001660 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1661 if (!btrfs_kset)
1662 return -ENOMEM;
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001663
David Sterba1bae3092014-02-05 15:36:18 +01001664 init_feature_attrs();
1665 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001666 if (ret)
1667 goto out2;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001668 ret = sysfs_merge_group(&btrfs_kset->kobj,
1669 &btrfs_static_feature_attr_group);
1670 if (ret)
1671 goto out_remove_group;
Filipe Manana001a6482015-01-23 18:27:00 +00001672
David Sterba6e369fe2019-06-13 17:23:02 +02001673#ifdef CONFIG_BTRFS_DEBUG
1674 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1675 if (ret)
1676 goto out2;
1677#endif
1678
Filipe Manana001a6482015-01-23 18:27:00 +00001679 return 0;
Misono Tomohirof902bd32018-05-17 14:24:51 +09001680
1681out_remove_group:
1682 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Filipe Manana001a6482015-01-23 18:27:00 +00001683out2:
Filipe Manana001a6482015-01-23 18:27:00 +00001684 kset_unregister(btrfs_kset);
David Sterba1bae3092014-02-05 15:36:18 +01001685
1686 return ret;
Josef Bacik58176a92007-08-29 15:47:34 -04001687}
1688
David Sterbae67c7182018-02-19 17:24:18 +01001689void __cold btrfs_exit_sysfs(void)
Josef Bacik58176a92007-08-29 15:47:34 -04001690{
Misono Tomohirof902bd32018-05-17 14:24:51 +09001691 sysfs_unmerge_group(&btrfs_kset->kobj,
1692 &btrfs_static_feature_attr_group);
Jeff Mahoney079b72b2013-11-01 13:06:57 -04001693 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
Dennis Zhou71e89782019-12-13 16:22:17 -08001694#ifdef CONFIG_BTRFS_DEBUG
1695 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1696#endif
Greg KHe3fe4e72008-02-20 14:14:16 -05001697 kset_unregister(btrfs_kset);
Josef Bacik58176a92007-08-29 15:47:34 -04001698}
Chris Mason55d47412008-02-20 16:02:51 -05001699