1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 */
5
6 #include <linux/sched.h>
7 #include <linux/sched/mm.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/completion.h>
11 #include <linux/bug.h>
12 #include <crypto/hash.h>
13
14 #include "ctree.h"
15 #include "discard.h"
16 #include "disk-io.h"
17 #include "send.h"
18 #include "transaction.h"
19 #include "sysfs.h"
20 #include "volumes.h"
21 #include "space-info.h"
22 #include "block-group.h"
23 #include "qgroup.h"
24 #include "misc.h"
25
26 /*
27 * Structure name Path
28 * --------------------------------------------------------------------------
29 * btrfs_supported_static_feature_attrs /sys/fs/btrfs/features
30 * btrfs_supported_feature_attrs /sys/fs/btrfs/features and
31 * /sys/fs/btrfs/<uuid>/features
32 * btrfs_attrs /sys/fs/btrfs/<uuid>
33 * devid_attrs /sys/fs/btrfs/<uuid>/devinfo/<devid>
34 * allocation_attrs /sys/fs/btrfs/<uuid>/allocation
35 * qgroup_attrs /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
36 * space_info_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>
37 * raid_attrs /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
38 * discard_attrs /sys/fs/btrfs/<uuid>/discard
39 *
40 * When built with BTRFS_CONFIG_DEBUG:
41 *
42 * btrfs_debug_feature_attrs /sys/fs/btrfs/debug
43 * btrfs_debug_mount_attrs /sys/fs/btrfs/<uuid>/debug
44 */
45
46 struct btrfs_feature_attr {
47 struct kobj_attribute kobj_attr;
48 enum btrfs_feature_set feature_set;
49 u64 feature_bit;
50 };
51
52 /* For raid type sysfs entries */
53 struct raid_kobject {
54 u64 flags;
55 struct kobject kobj;
56 };
57
58 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
59 { \
60 .attr = { .name = __stringify(_name), .mode = _mode }, \
61 .show = _show, \
62 .store = _store, \
63 }
64
65 #define BTRFS_ATTR_W(_prefix, _name, _store) \
66 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
67 __INIT_KOBJ_ATTR(_name, 0200, NULL, _store)
68
69 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
70 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
71 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
72
73 #define BTRFS_ATTR(_prefix, _name, _show) \
74 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
75 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
76
77 #define BTRFS_ATTR_PTR(_prefix, _name) \
78 (&btrfs_attr_##_prefix##_##_name.attr)
79
80 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
81 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
82 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
83 btrfs_feature_attr_show, \
84 btrfs_feature_attr_store), \
85 .feature_set = _feature_set, \
86 .feature_bit = _feature_prefix ##_## _feature_bit, \
87 }
88 #define BTRFS_FEAT_ATTR_PTR(_name) \
89 (&btrfs_attr_features_##_name.kobj_attr.attr)
90
91 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
92 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
93 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
94 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
95 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
96 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
97
98 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
99 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
100 static struct kobject *get_btrfs_kobj(struct kobject *kobj);
101
to_btrfs_feature_attr(struct kobj_attribute * a)102 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
103 {
104 return container_of(a, struct btrfs_feature_attr, kobj_attr);
105 }
106
attr_to_btrfs_attr(struct attribute * attr)107 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
108 {
109 return container_of(attr, struct kobj_attribute, attr);
110 }
111
attr_to_btrfs_feature_attr(struct attribute * attr)112 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
113 struct attribute *attr)
114 {
115 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
116 }
117
get_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set)118 static u64 get_features(struct btrfs_fs_info *fs_info,
119 enum btrfs_feature_set set)
120 {
121 struct btrfs_super_block *disk_super = fs_info->super_copy;
122 if (set == FEAT_COMPAT)
123 return btrfs_super_compat_flags(disk_super);
124 else if (set == FEAT_COMPAT_RO)
125 return btrfs_super_compat_ro_flags(disk_super);
126 else
127 return btrfs_super_incompat_flags(disk_super);
128 }
129
set_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set,u64 features)130 static void set_features(struct btrfs_fs_info *fs_info,
131 enum btrfs_feature_set set, u64 features)
132 {
133 struct btrfs_super_block *disk_super = fs_info->super_copy;
134 if (set == FEAT_COMPAT)
135 btrfs_set_super_compat_flags(disk_super, features);
136 else if (set == FEAT_COMPAT_RO)
137 btrfs_set_super_compat_ro_flags(disk_super, features);
138 else
139 btrfs_set_super_incompat_flags(disk_super, features);
140 }
141
can_modify_feature(struct btrfs_feature_attr * fa)142 static int can_modify_feature(struct btrfs_feature_attr *fa)
143 {
144 int val = 0;
145 u64 set, clear;
146 switch (fa->feature_set) {
147 case FEAT_COMPAT:
148 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
149 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
150 break;
151 case FEAT_COMPAT_RO:
152 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
153 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
154 break;
155 case FEAT_INCOMPAT:
156 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
157 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
158 break;
159 default:
160 pr_warn("btrfs: sysfs: unknown feature set %d\n",
161 fa->feature_set);
162 return 0;
163 }
164
165 if (set & fa->feature_bit)
166 val |= 1;
167 if (clear & fa->feature_bit)
168 val |= 2;
169
170 return val;
171 }
172
btrfs_feature_attr_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)173 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
174 struct kobj_attribute *a, char *buf)
175 {
176 int val = 0;
177 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
178 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
179 if (fs_info) {
180 u64 features = get_features(fs_info, fa->feature_set);
181 if (features & fa->feature_bit)
182 val = 1;
183 } else
184 val = can_modify_feature(fa);
185
186 return sysfs_emit(buf, "%d\n", val);
187 }
188
btrfs_feature_attr_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t count)189 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
190 struct kobj_attribute *a,
191 const char *buf, size_t count)
192 {
193 struct btrfs_fs_info *fs_info;
194 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
195 u64 features, set, clear;
196 unsigned long val;
197 int ret;
198
199 fs_info = to_fs_info(kobj);
200 if (!fs_info)
201 return -EPERM;
202
203 if (sb_rdonly(fs_info->sb))
204 return -EROFS;
205
206 ret = kstrtoul(skip_spaces(buf), 0, &val);
207 if (ret)
208 return ret;
209
210 if (fa->feature_set == FEAT_COMPAT) {
211 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
212 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
213 } else if (fa->feature_set == FEAT_COMPAT_RO) {
214 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
215 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
216 } else {
217 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
218 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
219 }
220
221 features = get_features(fs_info, fa->feature_set);
222
223 /* Nothing to do */
224 if ((val && (features & fa->feature_bit)) ||
225 (!val && !(features & fa->feature_bit)))
226 return count;
227
228 if ((val && !(set & fa->feature_bit)) ||
229 (!val && !(clear & fa->feature_bit))) {
230 btrfs_info(fs_info,
231 "%sabling feature %s on mounted fs is not supported.",
232 val ? "En" : "Dis", fa->kobj_attr.attr.name);
233 return -EPERM;
234 }
235
236 btrfs_info(fs_info, "%s %s feature flag",
237 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
238
239 spin_lock(&fs_info->super_lock);
240 features = get_features(fs_info, fa->feature_set);
241 if (val)
242 features |= fa->feature_bit;
243 else
244 features &= ~fa->feature_bit;
245 set_features(fs_info, fa->feature_set, features);
246 spin_unlock(&fs_info->super_lock);
247
248 /*
249 * We don't want to do full transaction commit from inside sysfs
250 */
251 btrfs_set_pending(fs_info, COMMIT);
252 wake_up_process(fs_info->transaction_kthread);
253
254 return count;
255 }
256
btrfs_feature_visible(struct kobject * kobj,struct attribute * attr,int unused)257 static umode_t btrfs_feature_visible(struct kobject *kobj,
258 struct attribute *attr, int unused)
259 {
260 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
261 umode_t mode = attr->mode;
262
263 if (fs_info) {
264 struct btrfs_feature_attr *fa;
265 u64 features;
266
267 fa = attr_to_btrfs_feature_attr(attr);
268 features = get_features(fs_info, fa->feature_set);
269
270 if (can_modify_feature(fa))
271 mode |= S_IWUSR;
272 else if (!(features & fa->feature_bit))
273 mode = 0;
274 }
275
276 return mode;
277 }
278
279 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
280 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
281 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
282 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
283 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
284 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
285 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
286 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
287 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
288 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
289 BTRFS_FEAT_ATTR_COMPAT_RO(block_group_tree, BLOCK_GROUP_TREE);
290 BTRFS_FEAT_ATTR_INCOMPAT(raid1c34, RAID1C34);
291 #ifdef CONFIG_BLK_DEV_ZONED
292 BTRFS_FEAT_ATTR_INCOMPAT(zoned, ZONED);
293 #endif
294 #ifdef CONFIG_BTRFS_DEBUG
295 /* Remove once support for extent tree v2 is feature complete */
296 BTRFS_FEAT_ATTR_INCOMPAT(extent_tree_v2, EXTENT_TREE_V2);
297 #endif
298 #ifdef CONFIG_FS_VERITY
299 BTRFS_FEAT_ATTR_COMPAT_RO(verity, VERITY);
300 #endif
301
302 /*
303 * Features which depend on feature bits and may differ between each fs.
304 *
305 * /sys/fs/btrfs/features - all available features implemented by this version
306 * /sys/fs/btrfs/UUID/features - features of the fs which are enabled or
307 * can be changed on a mounted filesystem.
308 */
309 static struct attribute *btrfs_supported_feature_attrs[] = {
310 BTRFS_FEAT_ATTR_PTR(default_subvol),
311 BTRFS_FEAT_ATTR_PTR(mixed_groups),
312 BTRFS_FEAT_ATTR_PTR(compress_lzo),
313 BTRFS_FEAT_ATTR_PTR(compress_zstd),
314 BTRFS_FEAT_ATTR_PTR(extended_iref),
315 BTRFS_FEAT_ATTR_PTR(raid56),
316 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
317 BTRFS_FEAT_ATTR_PTR(no_holes),
318 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
319 BTRFS_FEAT_ATTR_PTR(free_space_tree),
320 BTRFS_FEAT_ATTR_PTR(raid1c34),
321 BTRFS_FEAT_ATTR_PTR(block_group_tree),
322 #ifdef CONFIG_BLK_DEV_ZONED
323 BTRFS_FEAT_ATTR_PTR(zoned),
324 #endif
325 #ifdef CONFIG_BTRFS_DEBUG
326 BTRFS_FEAT_ATTR_PTR(extent_tree_v2),
327 #endif
328 #ifdef CONFIG_FS_VERITY
329 BTRFS_FEAT_ATTR_PTR(verity),
330 #endif
331 NULL
332 };
333
334 static const struct attribute_group btrfs_feature_attr_group = {
335 .name = "features",
336 .is_visible = btrfs_feature_visible,
337 .attrs = btrfs_supported_feature_attrs,
338 };
339
rmdir_subvol_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)340 static ssize_t rmdir_subvol_show(struct kobject *kobj,
341 struct kobj_attribute *ka, char *buf)
342 {
343 return sysfs_emit(buf, "0\n");
344 }
345 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
346
supported_checksums_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)347 static ssize_t supported_checksums_show(struct kobject *kobj,
348 struct kobj_attribute *a, char *buf)
349 {
350 ssize_t ret = 0;
351 int i;
352
353 for (i = 0; i < btrfs_get_num_csums(); i++) {
354 /*
355 * This "trick" only works as long as 'enum btrfs_csum_type' has
356 * no holes in it
357 */
358 ret += sysfs_emit_at(buf, ret, "%s%s", (i == 0 ? "" : " "),
359 btrfs_super_csum_name(i));
360
361 }
362
363 ret += sysfs_emit_at(buf, ret, "\n");
364 return ret;
365 }
366 BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
367
send_stream_version_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)368 static ssize_t send_stream_version_show(struct kobject *kobj,
369 struct kobj_attribute *ka, char *buf)
370 {
371 return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
372 }
373 BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
374
375 static const char *rescue_opts[] = {
376 "usebackuproot",
377 "nologreplay",
378 "ignorebadroots",
379 "ignoredatacsums",
380 "all",
381 };
382
supported_rescue_options_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)383 static ssize_t supported_rescue_options_show(struct kobject *kobj,
384 struct kobj_attribute *a,
385 char *buf)
386 {
387 ssize_t ret = 0;
388 int i;
389
390 for (i = 0; i < ARRAY_SIZE(rescue_opts); i++)
391 ret += sysfs_emit_at(buf, ret, "%s%s", (i ? " " : ""), rescue_opts[i]);
392 ret += sysfs_emit_at(buf, ret, "\n");
393 return ret;
394 }
395 BTRFS_ATTR(static_feature, supported_rescue_options,
396 supported_rescue_options_show);
397
supported_sectorsizes_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)398 static ssize_t supported_sectorsizes_show(struct kobject *kobj,
399 struct kobj_attribute *a,
400 char *buf)
401 {
402 ssize_t ret = 0;
403
404 /* An artificial limit to only support 4K and PAGE_SIZE */
405 if (PAGE_SIZE > SZ_4K)
406 ret += sysfs_emit_at(buf, ret, "%u ", SZ_4K);
407 ret += sysfs_emit_at(buf, ret, "%lu\n", PAGE_SIZE);
408
409 return ret;
410 }
411 BTRFS_ATTR(static_feature, supported_sectorsizes,
412 supported_sectorsizes_show);
413
414 /*
415 * Features which only depend on kernel version.
416 *
417 * These are listed in /sys/fs/btrfs/features along with
418 * btrfs_supported_feature_attrs.
419 */
420 static struct attribute *btrfs_supported_static_feature_attrs[] = {
421 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
422 BTRFS_ATTR_PTR(static_feature, supported_checksums),
423 BTRFS_ATTR_PTR(static_feature, send_stream_version),
424 BTRFS_ATTR_PTR(static_feature, supported_rescue_options),
425 BTRFS_ATTR_PTR(static_feature, supported_sectorsizes),
426 NULL
427 };
428
429 static const struct attribute_group btrfs_static_feature_attr_group = {
430 .name = "features",
431 .attrs = btrfs_supported_static_feature_attrs,
432 };
433
434 /*
435 * Discard statistics and tunables
436 */
437 #define discard_to_fs_info(_kobj) to_fs_info(get_btrfs_kobj(_kobj))
438
btrfs_discardable_bytes_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)439 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
440 struct kobj_attribute *a,
441 char *buf)
442 {
443 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
444
445 return sysfs_emit(buf, "%lld\n",
446 atomic64_read(&fs_info->discard_ctl.discardable_bytes));
447 }
448 BTRFS_ATTR(discard, discardable_bytes, btrfs_discardable_bytes_show);
449
btrfs_discardable_extents_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)450 static ssize_t btrfs_discardable_extents_show(struct kobject *kobj,
451 struct kobj_attribute *a,
452 char *buf)
453 {
454 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
455
456 return sysfs_emit(buf, "%d\n",
457 atomic_read(&fs_info->discard_ctl.discardable_extents));
458 }
459 BTRFS_ATTR(discard, discardable_extents, btrfs_discardable_extents_show);
460
btrfs_discard_bitmap_bytes_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)461 static ssize_t btrfs_discard_bitmap_bytes_show(struct kobject *kobj,
462 struct kobj_attribute *a,
463 char *buf)
464 {
465 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
466
467 return sysfs_emit(buf, "%llu\n",
468 fs_info->discard_ctl.discard_bitmap_bytes);
469 }
470 BTRFS_ATTR(discard, discard_bitmap_bytes, btrfs_discard_bitmap_bytes_show);
471
btrfs_discard_bytes_saved_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)472 static ssize_t btrfs_discard_bytes_saved_show(struct kobject *kobj,
473 struct kobj_attribute *a,
474 char *buf)
475 {
476 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
477
478 return sysfs_emit(buf, "%lld\n",
479 atomic64_read(&fs_info->discard_ctl.discard_bytes_saved));
480 }
481 BTRFS_ATTR(discard, discard_bytes_saved, btrfs_discard_bytes_saved_show);
482
btrfs_discard_extent_bytes_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)483 static ssize_t btrfs_discard_extent_bytes_show(struct kobject *kobj,
484 struct kobj_attribute *a,
485 char *buf)
486 {
487 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
488
489 return sysfs_emit(buf, "%llu\n",
490 fs_info->discard_ctl.discard_extent_bytes);
491 }
492 BTRFS_ATTR(discard, discard_extent_bytes, btrfs_discard_extent_bytes_show);
493
btrfs_discard_iops_limit_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)494 static ssize_t btrfs_discard_iops_limit_show(struct kobject *kobj,
495 struct kobj_attribute *a,
496 char *buf)
497 {
498 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
499
500 return sysfs_emit(buf, "%u\n",
501 READ_ONCE(fs_info->discard_ctl.iops_limit));
502 }
503
btrfs_discard_iops_limit_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)504 static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
505 struct kobj_attribute *a,
506 const char *buf, size_t len)
507 {
508 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
509 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
510 u32 iops_limit;
511 int ret;
512
513 ret = kstrtou32(buf, 10, &iops_limit);
514 if (ret)
515 return -EINVAL;
516
517 WRITE_ONCE(discard_ctl->iops_limit, iops_limit);
518 btrfs_discard_calc_delay(discard_ctl);
519 btrfs_discard_schedule_work(discard_ctl, true);
520 return len;
521 }
522 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
523 btrfs_discard_iops_limit_store);
524
btrfs_discard_kbps_limit_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)525 static ssize_t btrfs_discard_kbps_limit_show(struct kobject *kobj,
526 struct kobj_attribute *a,
527 char *buf)
528 {
529 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
530
531 return sysfs_emit(buf, "%u\n",
532 READ_ONCE(fs_info->discard_ctl.kbps_limit));
533 }
534
btrfs_discard_kbps_limit_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)535 static ssize_t btrfs_discard_kbps_limit_store(struct kobject *kobj,
536 struct kobj_attribute *a,
537 const char *buf, size_t len)
538 {
539 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
540 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
541 u32 kbps_limit;
542 int ret;
543
544 ret = kstrtou32(buf, 10, &kbps_limit);
545 if (ret)
546 return -EINVAL;
547
548 WRITE_ONCE(discard_ctl->kbps_limit, kbps_limit);
549 btrfs_discard_schedule_work(discard_ctl, true);
550 return len;
551 }
552 BTRFS_ATTR_RW(discard, kbps_limit, btrfs_discard_kbps_limit_show,
553 btrfs_discard_kbps_limit_store);
554
btrfs_discard_max_discard_size_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)555 static ssize_t btrfs_discard_max_discard_size_show(struct kobject *kobj,
556 struct kobj_attribute *a,
557 char *buf)
558 {
559 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
560
561 return sysfs_emit(buf, "%llu\n",
562 READ_ONCE(fs_info->discard_ctl.max_discard_size));
563 }
564
btrfs_discard_max_discard_size_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)565 static ssize_t btrfs_discard_max_discard_size_store(struct kobject *kobj,
566 struct kobj_attribute *a,
567 const char *buf, size_t len)
568 {
569 struct btrfs_fs_info *fs_info = discard_to_fs_info(kobj);
570 struct btrfs_discard_ctl *discard_ctl = &fs_info->discard_ctl;
571 u64 max_discard_size;
572 int ret;
573
574 ret = kstrtou64(buf, 10, &max_discard_size);
575 if (ret)
576 return -EINVAL;
577
578 WRITE_ONCE(discard_ctl->max_discard_size, max_discard_size);
579
580 return len;
581 }
582 BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
583 btrfs_discard_max_discard_size_store);
584
585 /*
586 * Per-filesystem stats for discard (when mounted with discard=async).
587 *
588 * Path: /sys/fs/btrfs/<uuid>/discard/
589 */
590 static const struct attribute *discard_attrs[] = {
591 BTRFS_ATTR_PTR(discard, discardable_bytes),
592 BTRFS_ATTR_PTR(discard, discardable_extents),
593 BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
594 BTRFS_ATTR_PTR(discard, discard_bytes_saved),
595 BTRFS_ATTR_PTR(discard, discard_extent_bytes),
596 BTRFS_ATTR_PTR(discard, iops_limit),
597 BTRFS_ATTR_PTR(discard, kbps_limit),
598 BTRFS_ATTR_PTR(discard, max_discard_size),
599 NULL,
600 };
601
602 #ifdef CONFIG_BTRFS_DEBUG
603
604 /*
605 * Per-filesystem runtime debugging exported via sysfs.
606 *
607 * Path: /sys/fs/btrfs/UUID/debug/
608 */
609 static const struct attribute *btrfs_debug_mount_attrs[] = {
610 NULL,
611 };
612
613 /*
614 * Runtime debugging exported via sysfs, applies to all mounted filesystems.
615 *
616 * Path: /sys/fs/btrfs/debug
617 */
618 static struct attribute *btrfs_debug_feature_attrs[] = {
619 NULL
620 };
621
622 static const struct attribute_group btrfs_debug_feature_attr_group = {
623 .name = "debug",
624 .attrs = btrfs_debug_feature_attrs,
625 };
626
627 #endif
628
btrfs_show_u64(u64 * value_ptr,spinlock_t * lock,char * buf)629 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
630 {
631 u64 val;
632 if (lock)
633 spin_lock(lock);
634 val = *value_ptr;
635 if (lock)
636 spin_unlock(lock);
637 return sysfs_emit(buf, "%llu\n", val);
638 }
639
global_rsv_size_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)640 static ssize_t global_rsv_size_show(struct kobject *kobj,
641 struct kobj_attribute *ka, char *buf)
642 {
643 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
644 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
645 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
646 }
647 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
648
global_rsv_reserved_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)649 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
650 struct kobj_attribute *a, char *buf)
651 {
652 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
653 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
654 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
655 }
656 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
657
658 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
659 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
660
661 static ssize_t raid_bytes_show(struct kobject *kobj,
662 struct kobj_attribute *attr, char *buf);
663 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
664 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
665
raid_bytes_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)666 static ssize_t raid_bytes_show(struct kobject *kobj,
667 struct kobj_attribute *attr, char *buf)
668
669 {
670 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
671 struct btrfs_block_group *block_group;
672 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
673 u64 val = 0;
674
675 down_read(&sinfo->groups_sem);
676 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
677 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
678 val += block_group->length;
679 else
680 val += block_group->used;
681 }
682 up_read(&sinfo->groups_sem);
683 return sysfs_emit(buf, "%llu\n", val);
684 }
685
686 /*
687 * Allocation information about block group profiles.
688 *
689 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>/
690 */
691 static struct attribute *raid_attrs[] = {
692 BTRFS_ATTR_PTR(raid, total_bytes),
693 BTRFS_ATTR_PTR(raid, used_bytes),
694 NULL
695 };
696 ATTRIBUTE_GROUPS(raid);
697
release_raid_kobj(struct kobject * kobj)698 static void release_raid_kobj(struct kobject *kobj)
699 {
700 kfree(to_raid_kobj(kobj));
701 }
702
703 static struct kobj_type btrfs_raid_ktype = {
704 .sysfs_ops = &kobj_sysfs_ops,
705 .release = release_raid_kobj,
706 .default_groups = raid_groups,
707 };
708
709 #define SPACE_INFO_ATTR(field) \
710 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
711 struct kobj_attribute *a, \
712 char *buf) \
713 { \
714 struct btrfs_space_info *sinfo = to_space_info(kobj); \
715 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
716 } \
717 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
718
btrfs_chunk_size_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)719 static ssize_t btrfs_chunk_size_show(struct kobject *kobj,
720 struct kobj_attribute *a, char *buf)
721 {
722 struct btrfs_space_info *sinfo = to_space_info(kobj);
723
724 return sysfs_emit(buf, "%llu\n", READ_ONCE(sinfo->chunk_size));
725 }
726
727 /*
728 * Store new chunk size in space info. Can be called on a read-only filesystem.
729 *
730 * If the new chunk size value is larger than 10% of free space it is reduced
731 * to match that limit. Alignment must be to 256M and the system chunk size
732 * cannot be set.
733 */
btrfs_chunk_size_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)734 static ssize_t btrfs_chunk_size_store(struct kobject *kobj,
735 struct kobj_attribute *a,
736 const char *buf, size_t len)
737 {
738 struct btrfs_space_info *space_info = to_space_info(kobj);
739 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
740 char *retptr;
741 u64 val;
742
743 if (!capable(CAP_SYS_ADMIN))
744 return -EPERM;
745
746 if (!fs_info->fs_devices)
747 return -EINVAL;
748
749 if (btrfs_is_zoned(fs_info))
750 return -EINVAL;
751
752 /* System block type must not be changed. */
753 if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
754 return -EPERM;
755
756 val = memparse(buf, &retptr);
757 /* There could be trailing '\n', also catch any typos after the value */
758 retptr = skip_spaces(retptr);
759 if (*retptr != 0 || val == 0)
760 return -EINVAL;
761
762 val = min(val, BTRFS_MAX_DATA_CHUNK_SIZE);
763
764 /* Limit stripe size to 10% of available space. */
765 val = min(div_factor(fs_info->fs_devices->total_rw_bytes, 1), val);
766
767 /* Must be multiple of 256M. */
768 val &= ~((u64)SZ_256M - 1);
769
770 /* Must be at least 256M. */
771 if (val < SZ_256M)
772 return -EINVAL;
773
774 btrfs_update_space_info_chunk_size(space_info, val);
775
776 return len;
777 }
778
779 #ifdef CONFIG_BTRFS_DEBUG
780 /*
781 * Request chunk allocation with current chunk size.
782 */
btrfs_force_chunk_alloc_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)783 static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,
784 struct kobj_attribute *a,
785 const char *buf, size_t len)
786 {
787 struct btrfs_space_info *space_info = to_space_info(kobj);
788 struct btrfs_fs_info *fs_info = to_fs_info(get_btrfs_kobj(kobj));
789 struct btrfs_trans_handle *trans;
790 bool val;
791 int ret;
792
793 if (!capable(CAP_SYS_ADMIN))
794 return -EPERM;
795
796 if (sb_rdonly(fs_info->sb))
797 return -EROFS;
798
799 ret = kstrtobool(buf, &val);
800 if (ret)
801 return ret;
802
803 if (!val)
804 return -EINVAL;
805
806 /*
807 * This is unsafe to be called from sysfs context and may cause
808 * unexpected problems.
809 */
810 trans = btrfs_start_transaction(fs_info->tree_root, 0);
811 if (IS_ERR(trans))
812 return PTR_ERR(trans);
813 ret = btrfs_force_chunk_alloc(trans, space_info->flags);
814 btrfs_end_transaction(trans);
815
816 if (ret == 1)
817 return len;
818
819 return -ENOSPC;
820 }
821 BTRFS_ATTR_W(space_info, force_chunk_alloc, btrfs_force_chunk_alloc_store);
822
823 #endif
824
825 SPACE_INFO_ATTR(flags);
826 SPACE_INFO_ATTR(total_bytes);
827 SPACE_INFO_ATTR(bytes_used);
828 SPACE_INFO_ATTR(bytes_pinned);
829 SPACE_INFO_ATTR(bytes_reserved);
830 SPACE_INFO_ATTR(bytes_may_use);
831 SPACE_INFO_ATTR(bytes_readonly);
832 SPACE_INFO_ATTR(bytes_zone_unusable);
833 SPACE_INFO_ATTR(disk_used);
834 SPACE_INFO_ATTR(disk_total);
835 BTRFS_ATTR_RW(space_info, chunk_size, btrfs_chunk_size_show, btrfs_chunk_size_store);
836
btrfs_sinfo_bg_reclaim_threshold_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)837 static ssize_t btrfs_sinfo_bg_reclaim_threshold_show(struct kobject *kobj,
838 struct kobj_attribute *a,
839 char *buf)
840 {
841 struct btrfs_space_info *space_info = to_space_info(kobj);
842
843 return sysfs_emit(buf, "%d\n", READ_ONCE(space_info->bg_reclaim_threshold));
844 }
845
btrfs_sinfo_bg_reclaim_threshold_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)846 static ssize_t btrfs_sinfo_bg_reclaim_threshold_store(struct kobject *kobj,
847 struct kobj_attribute *a,
848 const char *buf, size_t len)
849 {
850 struct btrfs_space_info *space_info = to_space_info(kobj);
851 int thresh;
852 int ret;
853
854 ret = kstrtoint(buf, 10, &thresh);
855 if (ret)
856 return ret;
857
858 if (thresh < 0 || thresh > 100)
859 return -EINVAL;
860
861 WRITE_ONCE(space_info->bg_reclaim_threshold, thresh);
862
863 return len;
864 }
865
866 BTRFS_ATTR_RW(space_info, bg_reclaim_threshold,
867 btrfs_sinfo_bg_reclaim_threshold_show,
868 btrfs_sinfo_bg_reclaim_threshold_store);
869
870 /*
871 * Allocation information about block group types.
872 *
873 * Path: /sys/fs/btrfs/<uuid>/allocation/<bg-type>/
874 */
875 static struct attribute *space_info_attrs[] = {
876 BTRFS_ATTR_PTR(space_info, flags),
877 BTRFS_ATTR_PTR(space_info, total_bytes),
878 BTRFS_ATTR_PTR(space_info, bytes_used),
879 BTRFS_ATTR_PTR(space_info, bytes_pinned),
880 BTRFS_ATTR_PTR(space_info, bytes_reserved),
881 BTRFS_ATTR_PTR(space_info, bytes_may_use),
882 BTRFS_ATTR_PTR(space_info, bytes_readonly),
883 BTRFS_ATTR_PTR(space_info, bytes_zone_unusable),
884 BTRFS_ATTR_PTR(space_info, disk_used),
885 BTRFS_ATTR_PTR(space_info, disk_total),
886 BTRFS_ATTR_PTR(space_info, bg_reclaim_threshold),
887 BTRFS_ATTR_PTR(space_info, chunk_size),
888 #ifdef CONFIG_BTRFS_DEBUG
889 BTRFS_ATTR_PTR(space_info, force_chunk_alloc),
890 #endif
891 NULL,
892 };
893 ATTRIBUTE_GROUPS(space_info);
894
space_info_release(struct kobject * kobj)895 static void space_info_release(struct kobject *kobj)
896 {
897 struct btrfs_space_info *sinfo = to_space_info(kobj);
898 kfree(sinfo);
899 }
900
901 static struct kobj_type space_info_ktype = {
902 .sysfs_ops = &kobj_sysfs_ops,
903 .release = space_info_release,
904 .default_groups = space_info_groups,
905 };
906
907 /*
908 * Allocation information about block groups.
909 *
910 * Path: /sys/fs/btrfs/<uuid>/allocation/
911 */
912 static const struct attribute *allocation_attrs[] = {
913 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
914 BTRFS_ATTR_PTR(allocation, global_rsv_size),
915 NULL,
916 };
917
btrfs_label_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)918 static ssize_t btrfs_label_show(struct kobject *kobj,
919 struct kobj_attribute *a, char *buf)
920 {
921 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
922 char *label = fs_info->super_copy->label;
923 ssize_t ret;
924
925 spin_lock(&fs_info->super_lock);
926 ret = sysfs_emit(buf, label[0] ? "%s\n" : "%s", label);
927 spin_unlock(&fs_info->super_lock);
928
929 return ret;
930 }
931
btrfs_label_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)932 static ssize_t btrfs_label_store(struct kobject *kobj,
933 struct kobj_attribute *a,
934 const char *buf, size_t len)
935 {
936 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
937 size_t p_len;
938
939 if (!fs_info)
940 return -EPERM;
941
942 if (sb_rdonly(fs_info->sb))
943 return -EROFS;
944
945 /*
946 * p_len is the len until the first occurrence of either
947 * '\n' or '\0'
948 */
949 p_len = strcspn(buf, "\n");
950
951 if (p_len >= BTRFS_LABEL_SIZE)
952 return -EINVAL;
953
954 spin_lock(&fs_info->super_lock);
955 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
956 memcpy(fs_info->super_copy->label, buf, p_len);
957 spin_unlock(&fs_info->super_lock);
958
959 /*
960 * We don't want to do full transaction commit from inside sysfs
961 */
962 btrfs_set_pending(fs_info, COMMIT);
963 wake_up_process(fs_info->transaction_kthread);
964
965 return len;
966 }
967 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
968
btrfs_nodesize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)969 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
970 struct kobj_attribute *a, char *buf)
971 {
972 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
973
974 return sysfs_emit(buf, "%u\n", fs_info->super_copy->nodesize);
975 }
976
977 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
978
btrfs_sectorsize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)979 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
980 struct kobj_attribute *a, char *buf)
981 {
982 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
983
984 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
985 }
986
987 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
988
btrfs_commit_stats_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)989 static ssize_t btrfs_commit_stats_show(struct kobject *kobj,
990 struct kobj_attribute *a, char *buf)
991 {
992 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
993
994 return sysfs_emit(buf,
995 "commits %llu\n"
996 "last_commit_ms %llu\n"
997 "max_commit_ms %llu\n"
998 "total_commit_ms %llu\n",
999 fs_info->commit_stats.commit_count,
1000 div_u64(fs_info->commit_stats.last_commit_dur, NSEC_PER_MSEC),
1001 div_u64(fs_info->commit_stats.max_commit_dur, NSEC_PER_MSEC),
1002 div_u64(fs_info->commit_stats.total_commit_dur, NSEC_PER_MSEC));
1003 }
1004
btrfs_commit_stats_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)1005 static ssize_t btrfs_commit_stats_store(struct kobject *kobj,
1006 struct kobj_attribute *a,
1007 const char *buf, size_t len)
1008 {
1009 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1010 unsigned long val;
1011 int ret;
1012
1013 if (!fs_info)
1014 return -EPERM;
1015
1016 if (!capable(CAP_SYS_RESOURCE))
1017 return -EPERM;
1018
1019 ret = kstrtoul(buf, 10, &val);
1020 if (ret)
1021 return ret;
1022 if (val)
1023 return -EINVAL;
1024
1025 WRITE_ONCE(fs_info->commit_stats.max_commit_dur, 0);
1026
1027 return len;
1028 }
1029 BTRFS_ATTR_RW(, commit_stats, btrfs_commit_stats_show, btrfs_commit_stats_store);
1030
btrfs_clone_alignment_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1031 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
1032 struct kobj_attribute *a, char *buf)
1033 {
1034 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1035
1036 return sysfs_emit(buf, "%u\n", fs_info->super_copy->sectorsize);
1037 }
1038
1039 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
1040
quota_override_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1041 static ssize_t quota_override_show(struct kobject *kobj,
1042 struct kobj_attribute *a, char *buf)
1043 {
1044 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1045 int quota_override;
1046
1047 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1048 return sysfs_emit(buf, "%d\n", quota_override);
1049 }
1050
quota_override_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)1051 static ssize_t quota_override_store(struct kobject *kobj,
1052 struct kobj_attribute *a,
1053 const char *buf, size_t len)
1054 {
1055 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1056 unsigned long knob;
1057 int err;
1058
1059 if (!fs_info)
1060 return -EPERM;
1061
1062 if (!capable(CAP_SYS_RESOURCE))
1063 return -EPERM;
1064
1065 err = kstrtoul(buf, 10, &knob);
1066 if (err)
1067 return err;
1068 if (knob > 1)
1069 return -EINVAL;
1070
1071 if (knob)
1072 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1073 else
1074 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
1075
1076 return len;
1077 }
1078
1079 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
1080
btrfs_metadata_uuid_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1081 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
1082 struct kobj_attribute *a, char *buf)
1083 {
1084 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1085
1086 return sysfs_emit(buf, "%pU\n", fs_info->fs_devices->metadata_uuid);
1087 }
1088
1089 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
1090
btrfs_checksum_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1091 static ssize_t btrfs_checksum_show(struct kobject *kobj,
1092 struct kobj_attribute *a, char *buf)
1093 {
1094 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1095 u16 csum_type = btrfs_super_csum_type(fs_info->super_copy);
1096
1097 return sysfs_emit(buf, "%s (%s)\n",
1098 btrfs_super_csum_name(csum_type),
1099 crypto_shash_driver_name(fs_info->csum_shash));
1100 }
1101
1102 BTRFS_ATTR(, checksum, btrfs_checksum_show);
1103
btrfs_exclusive_operation_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1104 static ssize_t btrfs_exclusive_operation_show(struct kobject *kobj,
1105 struct kobj_attribute *a, char *buf)
1106 {
1107 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1108 const char *str;
1109
1110 switch (READ_ONCE(fs_info->exclusive_operation)) {
1111 case BTRFS_EXCLOP_NONE:
1112 str = "none\n";
1113 break;
1114 case BTRFS_EXCLOP_BALANCE:
1115 str = "balance\n";
1116 break;
1117 case BTRFS_EXCLOP_BALANCE_PAUSED:
1118 str = "balance paused\n";
1119 break;
1120 case BTRFS_EXCLOP_DEV_ADD:
1121 str = "device add\n";
1122 break;
1123 case BTRFS_EXCLOP_DEV_REMOVE:
1124 str = "device remove\n";
1125 break;
1126 case BTRFS_EXCLOP_DEV_REPLACE:
1127 str = "device replace\n";
1128 break;
1129 case BTRFS_EXCLOP_RESIZE:
1130 str = "resize\n";
1131 break;
1132 case BTRFS_EXCLOP_SWAP_ACTIVATE:
1133 str = "swap activate\n";
1134 break;
1135 default:
1136 str = "UNKNOWN\n";
1137 break;
1138 }
1139 return sysfs_emit(buf, "%s", str);
1140 }
1141 BTRFS_ATTR(, exclusive_operation, btrfs_exclusive_operation_show);
1142
btrfs_generation_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1143 static ssize_t btrfs_generation_show(struct kobject *kobj,
1144 struct kobj_attribute *a, char *buf)
1145 {
1146 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1147
1148 return sysfs_emit(buf, "%llu\n", fs_info->generation);
1149 }
1150 BTRFS_ATTR(, generation, btrfs_generation_show);
1151
1152 static const char * const btrfs_read_policy_name[] = { "pid" };
1153
btrfs_read_policy_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1154 static ssize_t btrfs_read_policy_show(struct kobject *kobj,
1155 struct kobj_attribute *a, char *buf)
1156 {
1157 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1158 ssize_t ret = 0;
1159 int i;
1160
1161 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1162 if (fs_devices->read_policy == i)
1163 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s[%s]",
1164 (ret == 0 ? "" : " "),
1165 btrfs_read_policy_name[i]);
1166 else
1167 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
1168 (ret == 0 ? "" : " "),
1169 btrfs_read_policy_name[i]);
1170 }
1171
1172 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
1173
1174 return ret;
1175 }
1176
btrfs_read_policy_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)1177 static ssize_t btrfs_read_policy_store(struct kobject *kobj,
1178 struct kobj_attribute *a,
1179 const char *buf, size_t len)
1180 {
1181 struct btrfs_fs_devices *fs_devices = to_fs_devs(kobj);
1182 int i;
1183
1184 for (i = 0; i < BTRFS_NR_READ_POLICY; i++) {
1185 if (sysfs_streq(buf, btrfs_read_policy_name[i])) {
1186 if (i != fs_devices->read_policy) {
1187 fs_devices->read_policy = i;
1188 btrfs_info(fs_devices->fs_info,
1189 "read policy set to '%s'",
1190 btrfs_read_policy_name[i]);
1191 }
1192 return len;
1193 }
1194 }
1195
1196 return -EINVAL;
1197 }
1198 BTRFS_ATTR_RW(, read_policy, btrfs_read_policy_show, btrfs_read_policy_store);
1199
btrfs_bg_reclaim_threshold_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1200 static ssize_t btrfs_bg_reclaim_threshold_show(struct kobject *kobj,
1201 struct kobj_attribute *a,
1202 char *buf)
1203 {
1204 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1205
1206 return sysfs_emit(buf, "%d\n", READ_ONCE(fs_info->bg_reclaim_threshold));
1207 }
1208
btrfs_bg_reclaim_threshold_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)1209 static ssize_t btrfs_bg_reclaim_threshold_store(struct kobject *kobj,
1210 struct kobj_attribute *a,
1211 const char *buf, size_t len)
1212 {
1213 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
1214 int thresh;
1215 int ret;
1216
1217 ret = kstrtoint(buf, 10, &thresh);
1218 if (ret)
1219 return ret;
1220
1221 if (thresh != 0 && (thresh <= 50 || thresh > 100))
1222 return -EINVAL;
1223
1224 WRITE_ONCE(fs_info->bg_reclaim_threshold, thresh);
1225
1226 return len;
1227 }
1228 BTRFS_ATTR_RW(, bg_reclaim_threshold, btrfs_bg_reclaim_threshold_show,
1229 btrfs_bg_reclaim_threshold_store);
1230
1231 /*
1232 * Per-filesystem information and stats.
1233 *
1234 * Path: /sys/fs/btrfs/<uuid>/
1235 */
1236 static const struct attribute *btrfs_attrs[] = {
1237 BTRFS_ATTR_PTR(, label),
1238 BTRFS_ATTR_PTR(, nodesize),
1239 BTRFS_ATTR_PTR(, sectorsize),
1240 BTRFS_ATTR_PTR(, clone_alignment),
1241 BTRFS_ATTR_PTR(, quota_override),
1242 BTRFS_ATTR_PTR(, metadata_uuid),
1243 BTRFS_ATTR_PTR(, checksum),
1244 BTRFS_ATTR_PTR(, exclusive_operation),
1245 BTRFS_ATTR_PTR(, generation),
1246 BTRFS_ATTR_PTR(, read_policy),
1247 BTRFS_ATTR_PTR(, bg_reclaim_threshold),
1248 BTRFS_ATTR_PTR(, commit_stats),
1249 NULL,
1250 };
1251
btrfs_release_fsid_kobj(struct kobject * kobj)1252 static void btrfs_release_fsid_kobj(struct kobject *kobj)
1253 {
1254 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
1255
1256 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
1257 complete(&fs_devs->kobj_unregister);
1258 }
1259
1260 static struct kobj_type btrfs_ktype = {
1261 .sysfs_ops = &kobj_sysfs_ops,
1262 .release = btrfs_release_fsid_kobj,
1263 };
1264
to_fs_devs(struct kobject * kobj)1265 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
1266 {
1267 if (kobj->ktype != &btrfs_ktype)
1268 return NULL;
1269 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
1270 }
1271
to_fs_info(struct kobject * kobj)1272 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
1273 {
1274 if (kobj->ktype != &btrfs_ktype)
1275 return NULL;
1276 return to_fs_devs(kobj)->fs_info;
1277 }
1278
get_btrfs_kobj(struct kobject * kobj)1279 static struct kobject *get_btrfs_kobj(struct kobject *kobj)
1280 {
1281 while (kobj) {
1282 if (kobj->ktype == &btrfs_ktype)
1283 return kobj;
1284 kobj = kobj->parent;
1285 }
1286 return NULL;
1287 }
1288
1289 #define NUM_FEATURE_BITS 64
1290 #define BTRFS_FEATURE_NAME_MAX 13
1291 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
1292 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
1293
1294 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
1295 ARRAY_SIZE(btrfs_feature_attrs));
1296 static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
1297 ARRAY_SIZE(btrfs_feature_attrs[0]));
1298
1299 static const u64 supported_feature_masks[FEAT_MAX] = {
1300 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
1301 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
1302 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
1303 };
1304
addrm_unknown_feature_attrs(struct btrfs_fs_info * fs_info,bool add)1305 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
1306 {
1307 int set;
1308
1309 for (set = 0; set < FEAT_MAX; set++) {
1310 int i;
1311 struct attribute *attrs[2];
1312 struct attribute_group agroup = {
1313 .name = "features",
1314 .attrs = attrs,
1315 };
1316 u64 features = get_features(fs_info, set);
1317 features &= ~supported_feature_masks[set];
1318
1319 if (!features)
1320 continue;
1321
1322 attrs[1] = NULL;
1323 for (i = 0; i < NUM_FEATURE_BITS; i++) {
1324 struct btrfs_feature_attr *fa;
1325
1326 if (!(features & (1ULL << i)))
1327 continue;
1328
1329 fa = &btrfs_feature_attrs[set][i];
1330 attrs[0] = &fa->kobj_attr.attr;
1331 if (add) {
1332 int ret;
1333 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
1334 &agroup);
1335 if (ret)
1336 return ret;
1337 } else
1338 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
1339 &agroup);
1340 }
1341
1342 }
1343 return 0;
1344 }
1345
__btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)1346 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1347 {
1348 if (fs_devs->devinfo_kobj) {
1349 kobject_del(fs_devs->devinfo_kobj);
1350 kobject_put(fs_devs->devinfo_kobj);
1351 fs_devs->devinfo_kobj = NULL;
1352 }
1353
1354 if (fs_devs->devices_kobj) {
1355 kobject_del(fs_devs->devices_kobj);
1356 kobject_put(fs_devs->devices_kobj);
1357 fs_devs->devices_kobj = NULL;
1358 }
1359
1360 if (fs_devs->fsid_kobj.state_initialized) {
1361 kobject_del(&fs_devs->fsid_kobj);
1362 kobject_put(&fs_devs->fsid_kobj);
1363 wait_for_completion(&fs_devs->kobj_unregister);
1364 }
1365 }
1366
1367 /* when fs_devs is NULL it will remove all fsid kobject */
btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)1368 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
1369 {
1370 struct list_head *fs_uuids = btrfs_get_fs_uuids();
1371
1372 if (fs_devs) {
1373 __btrfs_sysfs_remove_fsid(fs_devs);
1374 return;
1375 }
1376
1377 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
1378 __btrfs_sysfs_remove_fsid(fs_devs);
1379 }
1380 }
1381
btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices * fs_devices)1382 static void btrfs_sysfs_remove_fs_devices(struct btrfs_fs_devices *fs_devices)
1383 {
1384 struct btrfs_device *device;
1385 struct btrfs_fs_devices *seed;
1386
1387 list_for_each_entry(device, &fs_devices->devices, dev_list)
1388 btrfs_sysfs_remove_device(device);
1389
1390 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1391 list_for_each_entry(device, &seed->devices, dev_list)
1392 btrfs_sysfs_remove_device(device);
1393 }
1394 }
1395
btrfs_sysfs_remove_mounted(struct btrfs_fs_info * fs_info)1396 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
1397 {
1398 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
1399
1400 sysfs_remove_link(fsid_kobj, "bdi");
1401
1402 if (fs_info->space_info_kobj) {
1403 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
1404 kobject_del(fs_info->space_info_kobj);
1405 kobject_put(fs_info->space_info_kobj);
1406 }
1407 if (fs_info->discard_kobj) {
1408 sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
1409 kobject_del(fs_info->discard_kobj);
1410 kobject_put(fs_info->discard_kobj);
1411 }
1412 #ifdef CONFIG_BTRFS_DEBUG
1413 if (fs_info->debug_kobj) {
1414 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1415 kobject_del(fs_info->debug_kobj);
1416 kobject_put(fs_info->debug_kobj);
1417 }
1418 #endif
1419 addrm_unknown_feature_attrs(fs_info, false);
1420 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1421 sysfs_remove_files(fsid_kobj, btrfs_attrs);
1422 btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
1423 }
1424
1425 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
1426 [FEAT_COMPAT] = "compat",
1427 [FEAT_COMPAT_RO] = "compat_ro",
1428 [FEAT_INCOMPAT] = "incompat",
1429 };
1430
btrfs_feature_set_name(enum btrfs_feature_set set)1431 const char *btrfs_feature_set_name(enum btrfs_feature_set set)
1432 {
1433 return btrfs_feature_set_names[set];
1434 }
1435
btrfs_printable_features(enum btrfs_feature_set set,u64 flags)1436 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
1437 {
1438 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
1439 int len = 0;
1440 int i;
1441 char *str;
1442
1443 str = kmalloc(bufsize, GFP_KERNEL);
1444 if (!str)
1445 return str;
1446
1447 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1448 const char *name;
1449
1450 if (!(flags & (1ULL << i)))
1451 continue;
1452
1453 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
1454 len += scnprintf(str + len, bufsize - len, "%s%s",
1455 len ? "," : "", name);
1456 }
1457
1458 return str;
1459 }
1460
init_feature_attrs(void)1461 static void init_feature_attrs(void)
1462 {
1463 struct btrfs_feature_attr *fa;
1464 int set, i;
1465
1466 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
1467 memset(btrfs_unknown_feature_names, 0,
1468 sizeof(btrfs_unknown_feature_names));
1469
1470 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
1471 struct btrfs_feature_attr *sfa;
1472 struct attribute *a = btrfs_supported_feature_attrs[i];
1473 int bit;
1474 sfa = attr_to_btrfs_feature_attr(a);
1475 bit = ilog2(sfa->feature_bit);
1476 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
1477
1478 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
1479 }
1480
1481 for (set = 0; set < FEAT_MAX; set++) {
1482 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
1483 char *name = btrfs_unknown_feature_names[set][i];
1484 fa = &btrfs_feature_attrs[set][i];
1485
1486 if (fa->kobj_attr.attr.name)
1487 continue;
1488
1489 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
1490 btrfs_feature_set_names[set], i);
1491
1492 fa->kobj_attr.attr.name = name;
1493 fa->kobj_attr.attr.mode = S_IRUGO;
1494 fa->feature_set = set;
1495 fa->feature_bit = 1ULL << i;
1496 }
1497 }
1498 }
1499
1500 /*
1501 * Create a sysfs entry for a given block group type at path
1502 * /sys/fs/btrfs/UUID/allocation/data/TYPE
1503 */
btrfs_sysfs_add_block_group_type(struct btrfs_block_group * cache)1504 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group *cache)
1505 {
1506 struct btrfs_fs_info *fs_info = cache->fs_info;
1507 struct btrfs_space_info *space_info = cache->space_info;
1508 struct raid_kobject *rkobj;
1509 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
1510 unsigned int nofs_flag;
1511 int ret;
1512
1513 /*
1514 * Setup a NOFS context because kobject_add(), deep in its call chain,
1515 * does GFP_KERNEL allocations, and we are often called in a context
1516 * where if reclaim is triggered we can deadlock (we are either holding
1517 * a transaction handle or some lock required for a transaction
1518 * commit).
1519 */
1520 nofs_flag = memalloc_nofs_save();
1521
1522 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
1523 if (!rkobj) {
1524 memalloc_nofs_restore(nofs_flag);
1525 btrfs_warn(cache->fs_info,
1526 "couldn't alloc memory for raid level kobject");
1527 return;
1528 }
1529
1530 rkobj->flags = cache->flags;
1531 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
1532
1533 /*
1534 * We call this either on mount, or if we've created a block group for a
1535 * new index type while running (i.e. when restriping). The running
1536 * case is tricky because we could race with other threads, so we need
1537 * to have this check to make sure we didn't already init the kobject.
1538 *
1539 * We don't have to protect on the free side because it only happens on
1540 * unmount.
1541 */
1542 spin_lock(&space_info->lock);
1543 if (space_info->block_group_kobjs[index]) {
1544 spin_unlock(&space_info->lock);
1545 kobject_put(&rkobj->kobj);
1546 return;
1547 } else {
1548 space_info->block_group_kobjs[index] = &rkobj->kobj;
1549 }
1550 spin_unlock(&space_info->lock);
1551
1552 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
1553 btrfs_bg_type_to_raid_name(rkobj->flags));
1554 memalloc_nofs_restore(nofs_flag);
1555 if (ret) {
1556 spin_lock(&space_info->lock);
1557 space_info->block_group_kobjs[index] = NULL;
1558 spin_unlock(&space_info->lock);
1559 kobject_put(&rkobj->kobj);
1560 btrfs_warn(fs_info,
1561 "failed to add kobject for block cache, ignoring");
1562 return;
1563 }
1564 }
1565
1566 /*
1567 * Remove sysfs directories for all block group types of a given space info and
1568 * the space info as well
1569 */
btrfs_sysfs_remove_space_info(struct btrfs_space_info * space_info)1570 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
1571 {
1572 int i;
1573
1574 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1575 struct kobject *kobj;
1576
1577 kobj = space_info->block_group_kobjs[i];
1578 space_info->block_group_kobjs[i] = NULL;
1579 if (kobj) {
1580 kobject_del(kobj);
1581 kobject_put(kobj);
1582 }
1583 }
1584 kobject_del(&space_info->kobj);
1585 kobject_put(&space_info->kobj);
1586 }
1587
alloc_name(u64 flags)1588 static const char *alloc_name(u64 flags)
1589 {
1590 switch (flags) {
1591 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
1592 return "mixed";
1593 case BTRFS_BLOCK_GROUP_METADATA:
1594 return "metadata";
1595 case BTRFS_BLOCK_GROUP_DATA:
1596 return "data";
1597 case BTRFS_BLOCK_GROUP_SYSTEM:
1598 return "system";
1599 default:
1600 WARN_ON(1);
1601 return "invalid-combination";
1602 }
1603 }
1604
1605 /*
1606 * Create a sysfs entry for a space info type at path
1607 * /sys/fs/btrfs/UUID/allocation/TYPE
1608 */
btrfs_sysfs_add_space_info_type(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)1609 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
1610 struct btrfs_space_info *space_info)
1611 {
1612 int ret;
1613
1614 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
1615 fs_info->space_info_kobj, "%s",
1616 alloc_name(space_info->flags));
1617 if (ret) {
1618 kobject_put(&space_info->kobj);
1619 return ret;
1620 }
1621
1622 return 0;
1623 }
1624
btrfs_sysfs_remove_device(struct btrfs_device * device)1625 void btrfs_sysfs_remove_device(struct btrfs_device *device)
1626 {
1627 struct kobject *devices_kobj;
1628
1629 /*
1630 * Seed fs_devices devices_kobj aren't used, fetch kobject from the
1631 * fs_info::fs_devices.
1632 */
1633 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1634 ASSERT(devices_kobj);
1635
1636 if (device->bdev)
1637 sysfs_remove_link(devices_kobj, bdev_kobj(device->bdev)->name);
1638
1639 if (device->devid_kobj.state_initialized) {
1640 kobject_del(&device->devid_kobj);
1641 kobject_put(&device->devid_kobj);
1642 wait_for_completion(&device->kobj_unregister);
1643 }
1644 }
1645
btrfs_devinfo_in_fs_metadata_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1646 static ssize_t btrfs_devinfo_in_fs_metadata_show(struct kobject *kobj,
1647 struct kobj_attribute *a,
1648 char *buf)
1649 {
1650 int val;
1651 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1652 devid_kobj);
1653
1654 val = !!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
1655
1656 return sysfs_emit(buf, "%d\n", val);
1657 }
1658 BTRFS_ATTR(devid, in_fs_metadata, btrfs_devinfo_in_fs_metadata_show);
1659
btrfs_devinfo_missing_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1660 static ssize_t btrfs_devinfo_missing_show(struct kobject *kobj,
1661 struct kobj_attribute *a, char *buf)
1662 {
1663 int val;
1664 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1665 devid_kobj);
1666
1667 val = !!test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
1668
1669 return sysfs_emit(buf, "%d\n", val);
1670 }
1671 BTRFS_ATTR(devid, missing, btrfs_devinfo_missing_show);
1672
btrfs_devinfo_replace_target_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1673 static ssize_t btrfs_devinfo_replace_target_show(struct kobject *kobj,
1674 struct kobj_attribute *a,
1675 char *buf)
1676 {
1677 int val;
1678 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1679 devid_kobj);
1680
1681 val = !!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
1682
1683 return sysfs_emit(buf, "%d\n", val);
1684 }
1685 BTRFS_ATTR(devid, replace_target, btrfs_devinfo_replace_target_show);
1686
btrfs_devinfo_scrub_speed_max_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1687 static ssize_t btrfs_devinfo_scrub_speed_max_show(struct kobject *kobj,
1688 struct kobj_attribute *a,
1689 char *buf)
1690 {
1691 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1692 devid_kobj);
1693
1694 return sysfs_emit(buf, "%llu\n", READ_ONCE(device->scrub_speed_max));
1695 }
1696
btrfs_devinfo_scrub_speed_max_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)1697 static ssize_t btrfs_devinfo_scrub_speed_max_store(struct kobject *kobj,
1698 struct kobj_attribute *a,
1699 const char *buf, size_t len)
1700 {
1701 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1702 devid_kobj);
1703 char *endptr;
1704 unsigned long long limit;
1705
1706 limit = memparse(buf, &endptr);
1707 WRITE_ONCE(device->scrub_speed_max, limit);
1708 return len;
1709 }
1710 BTRFS_ATTR_RW(devid, scrub_speed_max, btrfs_devinfo_scrub_speed_max_show,
1711 btrfs_devinfo_scrub_speed_max_store);
1712
btrfs_devinfo_writeable_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1713 static ssize_t btrfs_devinfo_writeable_show(struct kobject *kobj,
1714 struct kobj_attribute *a, char *buf)
1715 {
1716 int val;
1717 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1718 devid_kobj);
1719
1720 val = !!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
1721
1722 return sysfs_emit(buf, "%d\n", val);
1723 }
1724 BTRFS_ATTR(devid, writeable, btrfs_devinfo_writeable_show);
1725
btrfs_devinfo_fsid_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1726 static ssize_t btrfs_devinfo_fsid_show(struct kobject *kobj,
1727 struct kobj_attribute *a, char *buf)
1728 {
1729 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1730 devid_kobj);
1731
1732 return sysfs_emit(buf, "%pU\n", device->fs_devices->fsid);
1733 }
1734 BTRFS_ATTR(devid, fsid, btrfs_devinfo_fsid_show);
1735
btrfs_devinfo_error_stats_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)1736 static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
1737 struct kobj_attribute *a, char *buf)
1738 {
1739 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1740 devid_kobj);
1741
1742 if (!device->dev_stats_valid)
1743 return sysfs_emit(buf, "invalid\n");
1744
1745 /*
1746 * Print all at once so we get a snapshot of all values from the same
1747 * time. Keep them in sync and in order of definition of
1748 * btrfs_dev_stat_values.
1749 */
1750 return sysfs_emit(buf,
1751 "write_errs %d\n"
1752 "read_errs %d\n"
1753 "flush_errs %d\n"
1754 "corruption_errs %d\n"
1755 "generation_errs %d\n",
1756 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_WRITE_ERRS),
1757 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_READ_ERRS),
1758 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_FLUSH_ERRS),
1759 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_CORRUPTION_ERRS),
1760 btrfs_dev_stat_read(device, BTRFS_DEV_STAT_GENERATION_ERRS));
1761 }
1762 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
1763
1764 /*
1765 * Information about one device.
1766 *
1767 * Path: /sys/fs/btrfs/<uuid>/devinfo/<devid>/
1768 */
1769 static struct attribute *devid_attrs[] = {
1770 BTRFS_ATTR_PTR(devid, error_stats),
1771 BTRFS_ATTR_PTR(devid, fsid),
1772 BTRFS_ATTR_PTR(devid, in_fs_metadata),
1773 BTRFS_ATTR_PTR(devid, missing),
1774 BTRFS_ATTR_PTR(devid, replace_target),
1775 BTRFS_ATTR_PTR(devid, scrub_speed_max),
1776 BTRFS_ATTR_PTR(devid, writeable),
1777 NULL
1778 };
1779 ATTRIBUTE_GROUPS(devid);
1780
btrfs_release_devid_kobj(struct kobject * kobj)1781 static void btrfs_release_devid_kobj(struct kobject *kobj)
1782 {
1783 struct btrfs_device *device = container_of(kobj, struct btrfs_device,
1784 devid_kobj);
1785
1786 memset(&device->devid_kobj, 0, sizeof(struct kobject));
1787 complete(&device->kobj_unregister);
1788 }
1789
1790 static struct kobj_type devid_ktype = {
1791 .sysfs_ops = &kobj_sysfs_ops,
1792 .default_groups = devid_groups,
1793 .release = btrfs_release_devid_kobj,
1794 };
1795
btrfs_sysfs_add_device(struct btrfs_device * device)1796 int btrfs_sysfs_add_device(struct btrfs_device *device)
1797 {
1798 int ret;
1799 unsigned int nofs_flag;
1800 struct kobject *devices_kobj;
1801 struct kobject *devinfo_kobj;
1802
1803 /*
1804 * Make sure we use the fs_info::fs_devices to fetch the kobjects even
1805 * for the seed fs_devices
1806 */
1807 devices_kobj = device->fs_info->fs_devices->devices_kobj;
1808 devinfo_kobj = device->fs_info->fs_devices->devinfo_kobj;
1809 ASSERT(devices_kobj);
1810 ASSERT(devinfo_kobj);
1811
1812 nofs_flag = memalloc_nofs_save();
1813
1814 if (device->bdev) {
1815 struct kobject *disk_kobj = bdev_kobj(device->bdev);
1816
1817 ret = sysfs_create_link(devices_kobj, disk_kobj, disk_kobj->name);
1818 if (ret) {
1819 btrfs_warn(device->fs_info,
1820 "creating sysfs device link for devid %llu failed: %d",
1821 device->devid, ret);
1822 goto out;
1823 }
1824 }
1825
1826 init_completion(&device->kobj_unregister);
1827 ret = kobject_init_and_add(&device->devid_kobj, &devid_ktype,
1828 devinfo_kobj, "%llu", device->devid);
1829 if (ret) {
1830 kobject_put(&device->devid_kobj);
1831 btrfs_warn(device->fs_info,
1832 "devinfo init for devid %llu failed: %d",
1833 device->devid, ret);
1834 }
1835
1836 out:
1837 memalloc_nofs_restore(nofs_flag);
1838 return ret;
1839 }
1840
btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices * fs_devices)1841 static int btrfs_sysfs_add_fs_devices(struct btrfs_fs_devices *fs_devices)
1842 {
1843 int ret;
1844 struct btrfs_device *device;
1845 struct btrfs_fs_devices *seed;
1846
1847 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1848 ret = btrfs_sysfs_add_device(device);
1849 if (ret)
1850 goto fail;
1851 }
1852
1853 list_for_each_entry(seed, &fs_devices->seed_list, seed_list) {
1854 list_for_each_entry(device, &seed->devices, dev_list) {
1855 ret = btrfs_sysfs_add_device(device);
1856 if (ret)
1857 goto fail;
1858 }
1859 }
1860
1861 return 0;
1862
1863 fail:
1864 btrfs_sysfs_remove_fs_devices(fs_devices);
1865 return ret;
1866 }
1867
btrfs_kobject_uevent(struct block_device * bdev,enum kobject_action action)1868 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1869 {
1870 int ret;
1871
1872 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1873 if (ret)
1874 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1875 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1876 &disk_to_dev(bdev->bd_disk)->kobj);
1877 }
1878
btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices * fs_devices)1879 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices)
1880
1881 {
1882 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1883
1884 /*
1885 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1886 * directory
1887 */
1888 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fs_devices->fsid);
1889 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1890 btrfs_warn(fs_devices->fs_info,
1891 "sysfs: failed to create fsid for sprout");
1892 }
1893
btrfs_sysfs_update_devid(struct btrfs_device * device)1894 void btrfs_sysfs_update_devid(struct btrfs_device *device)
1895 {
1896 char tmp[24];
1897
1898 snprintf(tmp, sizeof(tmp), "%llu", device->devid);
1899
1900 if (kobject_rename(&device->devid_kobj, tmp))
1901 btrfs_warn(device->fs_devices->fs_info,
1902 "sysfs: failed to update devid for %llu",
1903 device->devid);
1904 }
1905
1906 /* /sys/fs/btrfs/ entry */
1907 static struct kset *btrfs_kset;
1908
1909 /*
1910 * Creates:
1911 * /sys/fs/btrfs/UUID
1912 *
1913 * Can be called by the device discovery thread.
1914 */
btrfs_sysfs_add_fsid(struct btrfs_fs_devices * fs_devs)1915 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs)
1916 {
1917 int error;
1918
1919 init_completion(&fs_devs->kobj_unregister);
1920 fs_devs->fsid_kobj.kset = btrfs_kset;
1921 error = kobject_init_and_add(&fs_devs->fsid_kobj, &btrfs_ktype, NULL,
1922 "%pU", fs_devs->fsid);
1923 if (error) {
1924 kobject_put(&fs_devs->fsid_kobj);
1925 return error;
1926 }
1927
1928 fs_devs->devices_kobj = kobject_create_and_add("devices",
1929 &fs_devs->fsid_kobj);
1930 if (!fs_devs->devices_kobj) {
1931 btrfs_err(fs_devs->fs_info,
1932 "failed to init sysfs device interface");
1933 btrfs_sysfs_remove_fsid(fs_devs);
1934 return -ENOMEM;
1935 }
1936
1937 fs_devs->devinfo_kobj = kobject_create_and_add("devinfo",
1938 &fs_devs->fsid_kobj);
1939 if (!fs_devs->devinfo_kobj) {
1940 btrfs_err(fs_devs->fs_info,
1941 "failed to init sysfs devinfo kobject");
1942 btrfs_sysfs_remove_fsid(fs_devs);
1943 return -ENOMEM;
1944 }
1945
1946 return 0;
1947 }
1948
btrfs_sysfs_add_mounted(struct btrfs_fs_info * fs_info)1949 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1950 {
1951 int error;
1952 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1953 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1954
1955 error = btrfs_sysfs_add_fs_devices(fs_devs);
1956 if (error)
1957 return error;
1958
1959 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1960 if (error) {
1961 btrfs_sysfs_remove_fs_devices(fs_devs);
1962 return error;
1963 }
1964
1965 error = sysfs_create_group(fsid_kobj,
1966 &btrfs_feature_attr_group);
1967 if (error)
1968 goto failure;
1969
1970 #ifdef CONFIG_BTRFS_DEBUG
1971 fs_info->debug_kobj = kobject_create_and_add("debug", fsid_kobj);
1972 if (!fs_info->debug_kobj) {
1973 error = -ENOMEM;
1974 goto failure;
1975 }
1976
1977 error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
1978 if (error)
1979 goto failure;
1980 #endif
1981
1982 /* Discard directory */
1983 fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
1984 if (!fs_info->discard_kobj) {
1985 error = -ENOMEM;
1986 goto failure;
1987 }
1988
1989 error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
1990 if (error)
1991 goto failure;
1992
1993 error = addrm_unknown_feature_attrs(fs_info, true);
1994 if (error)
1995 goto failure;
1996
1997 error = sysfs_create_link(fsid_kobj, &fs_info->sb->s_bdi->dev->kobj, "bdi");
1998 if (error)
1999 goto failure;
2000
2001 fs_info->space_info_kobj = kobject_create_and_add("allocation",
2002 fsid_kobj);
2003 if (!fs_info->space_info_kobj) {
2004 error = -ENOMEM;
2005 goto failure;
2006 }
2007
2008 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
2009 if (error)
2010 goto failure;
2011
2012 return 0;
2013 failure:
2014 btrfs_sysfs_remove_mounted(fs_info);
2015 return error;
2016 }
2017
qgroup_enabled_show(struct kobject * qgroups_kobj,struct kobj_attribute * a,char * buf)2018 static ssize_t qgroup_enabled_show(struct kobject *qgroups_kobj,
2019 struct kobj_attribute *a,
2020 char *buf)
2021 {
2022 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2023 bool enabled;
2024
2025 spin_lock(&fs_info->qgroup_lock);
2026 enabled = fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_ON;
2027 spin_unlock(&fs_info->qgroup_lock);
2028
2029 return sysfs_emit(buf, "%d\n", enabled);
2030 }
2031 BTRFS_ATTR(qgroups, enabled, qgroup_enabled_show);
2032
qgroup_inconsistent_show(struct kobject * qgroups_kobj,struct kobj_attribute * a,char * buf)2033 static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
2034 struct kobj_attribute *a,
2035 char *buf)
2036 {
2037 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2038 bool inconsistent;
2039
2040 spin_lock(&fs_info->qgroup_lock);
2041 inconsistent = (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
2042 spin_unlock(&fs_info->qgroup_lock);
2043
2044 return sysfs_emit(buf, "%d\n", inconsistent);
2045 }
2046 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
2047
qgroup_drop_subtree_thres_show(struct kobject * qgroups_kobj,struct kobj_attribute * a,char * buf)2048 static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
2049 struct kobj_attribute *a,
2050 char *buf)
2051 {
2052 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2053 u8 result;
2054
2055 spin_lock(&fs_info->qgroup_lock);
2056 result = fs_info->qgroup_drop_subtree_thres;
2057 spin_unlock(&fs_info->qgroup_lock);
2058
2059 return sysfs_emit(buf, "%d\n", result);
2060 }
2061
qgroup_drop_subtree_thres_store(struct kobject * qgroups_kobj,struct kobj_attribute * a,const char * buf,size_t len)2062 static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
2063 struct kobj_attribute *a,
2064 const char *buf, size_t len)
2065 {
2066 struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
2067 u8 new_thres;
2068 int ret;
2069
2070 ret = kstrtou8(buf, 10, &new_thres);
2071 if (ret)
2072 return -EINVAL;
2073
2074 if (new_thres > BTRFS_MAX_LEVEL)
2075 return -EINVAL;
2076
2077 spin_lock(&fs_info->qgroup_lock);
2078 fs_info->qgroup_drop_subtree_thres = new_thres;
2079 spin_unlock(&fs_info->qgroup_lock);
2080
2081 return len;
2082 }
2083 BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
2084 qgroup_drop_subtree_thres_store);
2085
2086 /*
2087 * Qgroups global info
2088 *
2089 * Path: /sys/fs/btrfs/<uuid>/qgroups/
2090 */
2091 static struct attribute *qgroups_attrs[] = {
2092 BTRFS_ATTR_PTR(qgroups, enabled),
2093 BTRFS_ATTR_PTR(qgroups, inconsistent),
2094 BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
2095 NULL
2096 };
2097 ATTRIBUTE_GROUPS(qgroups);
2098
qgroups_release(struct kobject * kobj)2099 static void qgroups_release(struct kobject *kobj)
2100 {
2101 kfree(kobj);
2102 }
2103
2104 static struct kobj_type qgroups_ktype = {
2105 .sysfs_ops = &kobj_sysfs_ops,
2106 .default_groups = qgroups_groups,
2107 .release = qgroups_release,
2108 };
2109
qgroup_kobj_to_fs_info(struct kobject * kobj)2110 static inline struct btrfs_fs_info *qgroup_kobj_to_fs_info(struct kobject *kobj)
2111 {
2112 return to_fs_info(kobj->parent->parent);
2113 }
2114
2115 #define QGROUP_ATTR(_member, _show_name) \
2116 static ssize_t btrfs_qgroup_show_##_member(struct kobject *qgroup_kobj, \
2117 struct kobj_attribute *a, \
2118 char *buf) \
2119 { \
2120 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2121 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2122 struct btrfs_qgroup, kobj); \
2123 return btrfs_show_u64(&qgroup->_member, &fs_info->qgroup_lock, buf); \
2124 } \
2125 BTRFS_ATTR(qgroup, _show_name, btrfs_qgroup_show_##_member)
2126
2127 #define QGROUP_RSV_ATTR(_name, _type) \
2128 static ssize_t btrfs_qgroup_rsv_show_##_name(struct kobject *qgroup_kobj, \
2129 struct kobj_attribute *a, \
2130 char *buf) \
2131 { \
2132 struct btrfs_fs_info *fs_info = qgroup_kobj_to_fs_info(qgroup_kobj); \
2133 struct btrfs_qgroup *qgroup = container_of(qgroup_kobj, \
2134 struct btrfs_qgroup, kobj); \
2135 return btrfs_show_u64(&qgroup->rsv.values[_type], \
2136 &fs_info->qgroup_lock, buf); \
2137 } \
2138 BTRFS_ATTR(qgroup, rsv_##_name, btrfs_qgroup_rsv_show_##_name)
2139
2140 QGROUP_ATTR(rfer, referenced);
2141 QGROUP_ATTR(excl, exclusive);
2142 QGROUP_ATTR(max_rfer, max_referenced);
2143 QGROUP_ATTR(max_excl, max_exclusive);
2144 QGROUP_ATTR(lim_flags, limit_flags);
2145 QGROUP_RSV_ATTR(data, BTRFS_QGROUP_RSV_DATA);
2146 QGROUP_RSV_ATTR(meta_pertrans, BTRFS_QGROUP_RSV_META_PERTRANS);
2147 QGROUP_RSV_ATTR(meta_prealloc, BTRFS_QGROUP_RSV_META_PREALLOC);
2148
2149 /*
2150 * Qgroup information.
2151 *
2152 * Path: /sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>/
2153 */
2154 static struct attribute *qgroup_attrs[] = {
2155 BTRFS_ATTR_PTR(qgroup, referenced),
2156 BTRFS_ATTR_PTR(qgroup, exclusive),
2157 BTRFS_ATTR_PTR(qgroup, max_referenced),
2158 BTRFS_ATTR_PTR(qgroup, max_exclusive),
2159 BTRFS_ATTR_PTR(qgroup, limit_flags),
2160 BTRFS_ATTR_PTR(qgroup, rsv_data),
2161 BTRFS_ATTR_PTR(qgroup, rsv_meta_pertrans),
2162 BTRFS_ATTR_PTR(qgroup, rsv_meta_prealloc),
2163 NULL
2164 };
2165 ATTRIBUTE_GROUPS(qgroup);
2166
qgroup_release(struct kobject * kobj)2167 static void qgroup_release(struct kobject *kobj)
2168 {
2169 struct btrfs_qgroup *qgroup = container_of(kobj, struct btrfs_qgroup, kobj);
2170
2171 memset(&qgroup->kobj, 0, sizeof(*kobj));
2172 }
2173
2174 static struct kobj_type qgroup_ktype = {
2175 .sysfs_ops = &kobj_sysfs_ops,
2176 .release = qgroup_release,
2177 .default_groups = qgroup_groups,
2178 };
2179
btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info * fs_info,struct btrfs_qgroup * qgroup)2180 int btrfs_sysfs_add_one_qgroup(struct btrfs_fs_info *fs_info,
2181 struct btrfs_qgroup *qgroup)
2182 {
2183 struct kobject *qgroups_kobj = fs_info->qgroups_kobj;
2184 int ret;
2185
2186 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2187 return 0;
2188 if (qgroup->kobj.state_initialized)
2189 return 0;
2190 if (!qgroups_kobj)
2191 return -EINVAL;
2192
2193 ret = kobject_init_and_add(&qgroup->kobj, &qgroup_ktype, qgroups_kobj,
2194 "%hu_%llu", btrfs_qgroup_level(qgroup->qgroupid),
2195 btrfs_qgroup_subvolid(qgroup->qgroupid));
2196 if (ret < 0)
2197 kobject_put(&qgroup->kobj);
2198
2199 return ret;
2200 }
2201
btrfs_sysfs_del_qgroups(struct btrfs_fs_info * fs_info)2202 void btrfs_sysfs_del_qgroups(struct btrfs_fs_info *fs_info)
2203 {
2204 struct btrfs_qgroup *qgroup;
2205 struct btrfs_qgroup *next;
2206
2207 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2208 return;
2209
2210 rbtree_postorder_for_each_entry_safe(qgroup, next,
2211 &fs_info->qgroup_tree, node)
2212 btrfs_sysfs_del_one_qgroup(fs_info, qgroup);
2213 if (fs_info->qgroups_kobj) {
2214 kobject_del(fs_info->qgroups_kobj);
2215 kobject_put(fs_info->qgroups_kobj);
2216 fs_info->qgroups_kobj = NULL;
2217 }
2218 }
2219
2220 /* Called when qgroups get initialized, thus there is no need for locking */
btrfs_sysfs_add_qgroups(struct btrfs_fs_info * fs_info)2221 int btrfs_sysfs_add_qgroups(struct btrfs_fs_info *fs_info)
2222 {
2223 struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
2224 struct btrfs_qgroup *qgroup;
2225 struct btrfs_qgroup *next;
2226 int ret = 0;
2227
2228 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2229 return 0;
2230
2231 ASSERT(fsid_kobj);
2232 if (fs_info->qgroups_kobj)
2233 return 0;
2234
2235 fs_info->qgroups_kobj = kzalloc(sizeof(struct kobject), GFP_KERNEL);
2236 if (!fs_info->qgroups_kobj)
2237 return -ENOMEM;
2238
2239 ret = kobject_init_and_add(fs_info->qgroups_kobj, &qgroups_ktype,
2240 fsid_kobj, "qgroups");
2241 if (ret < 0)
2242 goto out;
2243
2244 rbtree_postorder_for_each_entry_safe(qgroup, next,
2245 &fs_info->qgroup_tree, node) {
2246 ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
2247 if (ret < 0)
2248 goto out;
2249 }
2250
2251 out:
2252 if (ret < 0)
2253 btrfs_sysfs_del_qgroups(fs_info);
2254 return ret;
2255 }
2256
btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info * fs_info,struct btrfs_qgroup * qgroup)2257 void btrfs_sysfs_del_one_qgroup(struct btrfs_fs_info *fs_info,
2258 struct btrfs_qgroup *qgroup)
2259 {
2260 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state))
2261 return;
2262
2263 if (qgroup->kobj.state_initialized) {
2264 kobject_del(&qgroup->kobj);
2265 kobject_put(&qgroup->kobj);
2266 }
2267 }
2268
2269 /*
2270 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
2271 * values in superblock. Call after any changes to incompat/compat_ro flags
2272 */
btrfs_sysfs_feature_update(struct btrfs_fs_info * fs_info,u64 bit,enum btrfs_feature_set set)2273 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
2274 u64 bit, enum btrfs_feature_set set)
2275 {
2276 struct btrfs_fs_devices *fs_devs;
2277 struct kobject *fsid_kobj;
2278 u64 __maybe_unused features;
2279 int __maybe_unused ret;
2280
2281 if (!fs_info)
2282 return;
2283
2284 /*
2285 * See 14e46e04958df74 and e410e34fad913dd, feature bit updates are not
2286 * safe when called from some contexts (eg. balance)
2287 */
2288 features = get_features(fs_info, set);
2289 ASSERT(bit & supported_feature_masks[set]);
2290
2291 fs_devs = fs_info->fs_devices;
2292 fsid_kobj = &fs_devs->fsid_kobj;
2293
2294 if (!fsid_kobj->state_initialized)
2295 return;
2296
2297 /*
2298 * FIXME: this is too heavy to update just one value, ideally we'd like
2299 * to use sysfs_update_group but some refactoring is needed first.
2300 */
2301 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
2302 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
2303 }
2304
btrfs_init_sysfs(void)2305 int __init btrfs_init_sysfs(void)
2306 {
2307 int ret;
2308
2309 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
2310 if (!btrfs_kset)
2311 return -ENOMEM;
2312
2313 init_feature_attrs();
2314 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2315 if (ret)
2316 goto out2;
2317 ret = sysfs_merge_group(&btrfs_kset->kobj,
2318 &btrfs_static_feature_attr_group);
2319 if (ret)
2320 goto out_remove_group;
2321
2322 #ifdef CONFIG_BTRFS_DEBUG
2323 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2324 if (ret) {
2325 sysfs_unmerge_group(&btrfs_kset->kobj,
2326 &btrfs_static_feature_attr_group);
2327 goto out_remove_group;
2328 }
2329 #endif
2330
2331 return 0;
2332
2333 out_remove_group:
2334 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2335 out2:
2336 kset_unregister(btrfs_kset);
2337
2338 return ret;
2339 }
2340
btrfs_exit_sysfs(void)2341 void __cold btrfs_exit_sysfs(void)
2342 {
2343 sysfs_unmerge_group(&btrfs_kset->kobj,
2344 &btrfs_static_feature_attr_group);
2345 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
2346 #ifdef CONFIG_BTRFS_DEBUG
2347 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
2348 #endif
2349 kset_unregister(btrfs_kset);
2350 }
2351