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
13 #include "ctree.h"
14 #include "disk-io.h"
15 #include "transaction.h"
16 #include "sysfs.h"
17 #include "volumes.h"
18 #include "space-info.h"
19 #include "block-group.h"
20
21 struct btrfs_feature_attr {
22 struct kobj_attribute kobj_attr;
23 enum btrfs_feature_set feature_set;
24 u64 feature_bit;
25 };
26
27 /* For raid type sysfs entries */
28 struct raid_kobject {
29 u64 flags;
30 struct kobject kobj;
31 };
32
33 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \
34 { \
35 .attr = { .name = __stringify(_name), .mode = _mode }, \
36 .show = _show, \
37 .store = _store, \
38 }
39
40 #define BTRFS_ATTR_RW(_prefix, _name, _show, _store) \
41 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
42 __INIT_KOBJ_ATTR(_name, 0644, _show, _store)
43
44 #define BTRFS_ATTR(_prefix, _name, _show) \
45 static struct kobj_attribute btrfs_attr_##_prefix##_##_name = \
46 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL)
47
48 #define BTRFS_ATTR_PTR(_prefix, _name) \
49 (&btrfs_attr_##_prefix##_##_name.attr)
50
51 #define BTRFS_FEAT_ATTR(_name, _feature_set, _feature_prefix, _feature_bit) \
52 static struct btrfs_feature_attr btrfs_attr_features_##_name = { \
53 .kobj_attr = __INIT_KOBJ_ATTR(_name, S_IRUGO, \
54 btrfs_feature_attr_show, \
55 btrfs_feature_attr_store), \
56 .feature_set = _feature_set, \
57 .feature_bit = _feature_prefix ##_## _feature_bit, \
58 }
59 #define BTRFS_FEAT_ATTR_PTR(_name) \
60 (&btrfs_attr_features_##_name.kobj_attr.attr)
61
62 #define BTRFS_FEAT_ATTR_COMPAT(name, feature) \
63 BTRFS_FEAT_ATTR(name, FEAT_COMPAT, BTRFS_FEATURE_COMPAT, feature)
64 #define BTRFS_FEAT_ATTR_COMPAT_RO(name, feature) \
65 BTRFS_FEAT_ATTR(name, FEAT_COMPAT_RO, BTRFS_FEATURE_COMPAT_RO, feature)
66 #define BTRFS_FEAT_ATTR_INCOMPAT(name, feature) \
67 BTRFS_FEAT_ATTR(name, FEAT_INCOMPAT, BTRFS_FEATURE_INCOMPAT, feature)
68
69 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
70 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
71
to_btrfs_feature_attr(struct kobj_attribute * a)72 static struct btrfs_feature_attr *to_btrfs_feature_attr(struct kobj_attribute *a)
73 {
74 return container_of(a, struct btrfs_feature_attr, kobj_attr);
75 }
76
attr_to_btrfs_attr(struct attribute * attr)77 static struct kobj_attribute *attr_to_btrfs_attr(struct attribute *attr)
78 {
79 return container_of(attr, struct kobj_attribute, attr);
80 }
81
attr_to_btrfs_feature_attr(struct attribute * attr)82 static struct btrfs_feature_attr *attr_to_btrfs_feature_attr(
83 struct attribute *attr)
84 {
85 return to_btrfs_feature_attr(attr_to_btrfs_attr(attr));
86 }
87
get_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set)88 static u64 get_features(struct btrfs_fs_info *fs_info,
89 enum btrfs_feature_set set)
90 {
91 struct btrfs_super_block *disk_super = fs_info->super_copy;
92 if (set == FEAT_COMPAT)
93 return btrfs_super_compat_flags(disk_super);
94 else if (set == FEAT_COMPAT_RO)
95 return btrfs_super_compat_ro_flags(disk_super);
96 else
97 return btrfs_super_incompat_flags(disk_super);
98 }
99
set_features(struct btrfs_fs_info * fs_info,enum btrfs_feature_set set,u64 features)100 static void set_features(struct btrfs_fs_info *fs_info,
101 enum btrfs_feature_set set, u64 features)
102 {
103 struct btrfs_super_block *disk_super = fs_info->super_copy;
104 if (set == FEAT_COMPAT)
105 btrfs_set_super_compat_flags(disk_super, features);
106 else if (set == FEAT_COMPAT_RO)
107 btrfs_set_super_compat_ro_flags(disk_super, features);
108 else
109 btrfs_set_super_incompat_flags(disk_super, features);
110 }
111
can_modify_feature(struct btrfs_feature_attr * fa)112 static int can_modify_feature(struct btrfs_feature_attr *fa)
113 {
114 int val = 0;
115 u64 set, clear;
116 switch (fa->feature_set) {
117 case FEAT_COMPAT:
118 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
119 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
120 break;
121 case FEAT_COMPAT_RO:
122 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
123 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
124 break;
125 case FEAT_INCOMPAT:
126 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
127 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
128 break;
129 default:
130 pr_warn("btrfs: sysfs: unknown feature set %d\n",
131 fa->feature_set);
132 return 0;
133 }
134
135 if (set & fa->feature_bit)
136 val |= 1;
137 if (clear & fa->feature_bit)
138 val |= 2;
139
140 return val;
141 }
142
btrfs_feature_attr_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)143 static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
144 struct kobj_attribute *a, char *buf)
145 {
146 int val = 0;
147 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
148 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
149 if (fs_info) {
150 u64 features = get_features(fs_info, fa->feature_set);
151 if (features & fa->feature_bit)
152 val = 1;
153 } else
154 val = can_modify_feature(fa);
155
156 return snprintf(buf, PAGE_SIZE, "%d\n", val);
157 }
158
btrfs_feature_attr_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t count)159 static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
160 struct kobj_attribute *a,
161 const char *buf, size_t count)
162 {
163 struct btrfs_fs_info *fs_info;
164 struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
165 u64 features, set, clear;
166 unsigned long val;
167 int ret;
168
169 fs_info = to_fs_info(kobj);
170 if (!fs_info)
171 return -EPERM;
172
173 if (sb_rdonly(fs_info->sb))
174 return -EROFS;
175
176 ret = kstrtoul(skip_spaces(buf), 0, &val);
177 if (ret)
178 return ret;
179
180 if (fa->feature_set == FEAT_COMPAT) {
181 set = BTRFS_FEATURE_COMPAT_SAFE_SET;
182 clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
183 } else if (fa->feature_set == FEAT_COMPAT_RO) {
184 set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
185 clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
186 } else {
187 set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
188 clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
189 }
190
191 features = get_features(fs_info, fa->feature_set);
192
193 /* Nothing to do */
194 if ((val && (features & fa->feature_bit)) ||
195 (!val && !(features & fa->feature_bit)))
196 return count;
197
198 if ((val && !(set & fa->feature_bit)) ||
199 (!val && !(clear & fa->feature_bit))) {
200 btrfs_info(fs_info,
201 "%sabling feature %s on mounted fs is not supported.",
202 val ? "En" : "Dis", fa->kobj_attr.attr.name);
203 return -EPERM;
204 }
205
206 btrfs_info(fs_info, "%s %s feature flag",
207 val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
208
209 spin_lock(&fs_info->super_lock);
210 features = get_features(fs_info, fa->feature_set);
211 if (val)
212 features |= fa->feature_bit;
213 else
214 features &= ~fa->feature_bit;
215 set_features(fs_info, fa->feature_set, features);
216 spin_unlock(&fs_info->super_lock);
217
218 /*
219 * We don't want to do full transaction commit from inside sysfs
220 */
221 btrfs_set_pending(fs_info, COMMIT);
222 wake_up_process(fs_info->transaction_kthread);
223
224 return count;
225 }
226
btrfs_feature_visible(struct kobject * kobj,struct attribute * attr,int unused)227 static umode_t btrfs_feature_visible(struct kobject *kobj,
228 struct attribute *attr, int unused)
229 {
230 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
231 umode_t mode = attr->mode;
232
233 if (fs_info) {
234 struct btrfs_feature_attr *fa;
235 u64 features;
236
237 fa = attr_to_btrfs_feature_attr(attr);
238 features = get_features(fs_info, fa->feature_set);
239
240 if (can_modify_feature(fa))
241 mode |= S_IWUSR;
242 else if (!(features & fa->feature_bit))
243 mode = 0;
244 }
245
246 return mode;
247 }
248
249 BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
250 BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
251 BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
252 BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
253 BTRFS_FEAT_ATTR_INCOMPAT(compress_zstd, COMPRESS_ZSTD);
254 BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
255 BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
256 BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
257 BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
258 BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
259 BTRFS_FEAT_ATTR_INCOMPAT(metadata_uuid, METADATA_UUID);
260 BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
261
262 static struct attribute *btrfs_supported_feature_attrs[] = {
263 BTRFS_FEAT_ATTR_PTR(mixed_backref),
264 BTRFS_FEAT_ATTR_PTR(default_subvol),
265 BTRFS_FEAT_ATTR_PTR(mixed_groups),
266 BTRFS_FEAT_ATTR_PTR(compress_lzo),
267 BTRFS_FEAT_ATTR_PTR(compress_zstd),
268 BTRFS_FEAT_ATTR_PTR(big_metadata),
269 BTRFS_FEAT_ATTR_PTR(extended_iref),
270 BTRFS_FEAT_ATTR_PTR(raid56),
271 BTRFS_FEAT_ATTR_PTR(skinny_metadata),
272 BTRFS_FEAT_ATTR_PTR(no_holes),
273 BTRFS_FEAT_ATTR_PTR(metadata_uuid),
274 BTRFS_FEAT_ATTR_PTR(free_space_tree),
275 NULL
276 };
277
278 /*
279 * Features which depend on feature bits and may differ between each fs.
280 *
281 * /sys/fs/btrfs/features lists all available features of this kernel while
282 * /sys/fs/btrfs/UUID/features shows features of the fs which are enabled or
283 * can be changed online.
284 */
285 static const struct attribute_group btrfs_feature_attr_group = {
286 .name = "features",
287 .is_visible = btrfs_feature_visible,
288 .attrs = btrfs_supported_feature_attrs,
289 };
290
rmdir_subvol_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)291 static ssize_t rmdir_subvol_show(struct kobject *kobj,
292 struct kobj_attribute *ka, char *buf)
293 {
294 return snprintf(buf, PAGE_SIZE, "0\n");
295 }
296 BTRFS_ATTR(static_feature, rmdir_subvol, rmdir_subvol_show);
297
298 static struct attribute *btrfs_supported_static_feature_attrs[] = {
299 BTRFS_ATTR_PTR(static_feature, rmdir_subvol),
300 NULL
301 };
302
303 /*
304 * Features which only depend on kernel version.
305 *
306 * These are listed in /sys/fs/btrfs/features along with
307 * btrfs_feature_attr_group
308 */
309 static const struct attribute_group btrfs_static_feature_attr_group = {
310 .name = "features",
311 .attrs = btrfs_supported_static_feature_attrs,
312 };
313
314 #ifdef CONFIG_BTRFS_DEBUG
315
316 /*
317 * Runtime debugging exported via sysfs
318 *
319 * /sys/fs/btrfs/debug - applies to module or all filesystems
320 * /sys/fs/btrfs/UUID - applies only to the given filesystem
321 */
322 static struct attribute *btrfs_debug_feature_attrs[] = {
323 NULL
324 };
325
326 static const struct attribute_group btrfs_debug_feature_attr_group = {
327 .name = "debug",
328 .attrs = btrfs_debug_feature_attrs,
329 };
330
331 #endif
332
btrfs_show_u64(u64 * value_ptr,spinlock_t * lock,char * buf)333 static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
334 {
335 u64 val;
336 if (lock)
337 spin_lock(lock);
338 val = *value_ptr;
339 if (lock)
340 spin_unlock(lock);
341 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
342 }
343
global_rsv_size_show(struct kobject * kobj,struct kobj_attribute * ka,char * buf)344 static ssize_t global_rsv_size_show(struct kobject *kobj,
345 struct kobj_attribute *ka, char *buf)
346 {
347 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
348 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
349 return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
350 }
351 BTRFS_ATTR(allocation, global_rsv_size, global_rsv_size_show);
352
global_rsv_reserved_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)353 static ssize_t global_rsv_reserved_show(struct kobject *kobj,
354 struct kobj_attribute *a, char *buf)
355 {
356 struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
357 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
358 return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
359 }
360 BTRFS_ATTR(allocation, global_rsv_reserved, global_rsv_reserved_show);
361
362 #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
363 #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
364
365 static ssize_t raid_bytes_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf);
367 BTRFS_ATTR(raid, total_bytes, raid_bytes_show);
368 BTRFS_ATTR(raid, used_bytes, raid_bytes_show);
369
raid_bytes_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)370 static ssize_t raid_bytes_show(struct kobject *kobj,
371 struct kobj_attribute *attr, char *buf)
372
373 {
374 struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
375 struct btrfs_block_group_cache *block_group;
376 int index = btrfs_bg_flags_to_raid_index(to_raid_kobj(kobj)->flags);
377 u64 val = 0;
378
379 down_read(&sinfo->groups_sem);
380 list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
381 if (&attr->attr == BTRFS_ATTR_PTR(raid, total_bytes))
382 val += block_group->key.offset;
383 else
384 val += btrfs_block_group_used(&block_group->item);
385 }
386 up_read(&sinfo->groups_sem);
387 return snprintf(buf, PAGE_SIZE, "%llu\n", val);
388 }
389
390 static struct attribute *raid_attrs[] = {
391 BTRFS_ATTR_PTR(raid, total_bytes),
392 BTRFS_ATTR_PTR(raid, used_bytes),
393 NULL
394 };
395 ATTRIBUTE_GROUPS(raid);
396
release_raid_kobj(struct kobject * kobj)397 static void release_raid_kobj(struct kobject *kobj)
398 {
399 kfree(to_raid_kobj(kobj));
400 }
401
402 static struct kobj_type btrfs_raid_ktype = {
403 .sysfs_ops = &kobj_sysfs_ops,
404 .release = release_raid_kobj,
405 .default_groups = raid_groups,
406 };
407
408 #define SPACE_INFO_ATTR(field) \
409 static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
410 struct kobj_attribute *a, \
411 char *buf) \
412 { \
413 struct btrfs_space_info *sinfo = to_space_info(kobj); \
414 return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
415 } \
416 BTRFS_ATTR(space_info, field, btrfs_space_info_show_##field)
417
btrfs_space_info_show_total_bytes_pinned(struct kobject * kobj,struct kobj_attribute * a,char * buf)418 static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
419 struct kobj_attribute *a,
420 char *buf)
421 {
422 struct btrfs_space_info *sinfo = to_space_info(kobj);
423 s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
424 return snprintf(buf, PAGE_SIZE, "%lld\n", val);
425 }
426
427 SPACE_INFO_ATTR(flags);
428 SPACE_INFO_ATTR(total_bytes);
429 SPACE_INFO_ATTR(bytes_used);
430 SPACE_INFO_ATTR(bytes_pinned);
431 SPACE_INFO_ATTR(bytes_reserved);
432 SPACE_INFO_ATTR(bytes_may_use);
433 SPACE_INFO_ATTR(bytes_readonly);
434 SPACE_INFO_ATTR(disk_used);
435 SPACE_INFO_ATTR(disk_total);
436 BTRFS_ATTR(space_info, total_bytes_pinned,
437 btrfs_space_info_show_total_bytes_pinned);
438
439 static struct attribute *space_info_attrs[] = {
440 BTRFS_ATTR_PTR(space_info, flags),
441 BTRFS_ATTR_PTR(space_info, total_bytes),
442 BTRFS_ATTR_PTR(space_info, bytes_used),
443 BTRFS_ATTR_PTR(space_info, bytes_pinned),
444 BTRFS_ATTR_PTR(space_info, bytes_reserved),
445 BTRFS_ATTR_PTR(space_info, bytes_may_use),
446 BTRFS_ATTR_PTR(space_info, bytes_readonly),
447 BTRFS_ATTR_PTR(space_info, disk_used),
448 BTRFS_ATTR_PTR(space_info, disk_total),
449 BTRFS_ATTR_PTR(space_info, total_bytes_pinned),
450 NULL,
451 };
452 ATTRIBUTE_GROUPS(space_info);
453
space_info_release(struct kobject * kobj)454 static void space_info_release(struct kobject *kobj)
455 {
456 struct btrfs_space_info *sinfo = to_space_info(kobj);
457 percpu_counter_destroy(&sinfo->total_bytes_pinned);
458 kfree(sinfo);
459 }
460
461 static struct kobj_type space_info_ktype = {
462 .sysfs_ops = &kobj_sysfs_ops,
463 .release = space_info_release,
464 .default_groups = space_info_groups,
465 };
466
467 static const struct attribute *allocation_attrs[] = {
468 BTRFS_ATTR_PTR(allocation, global_rsv_reserved),
469 BTRFS_ATTR_PTR(allocation, global_rsv_size),
470 NULL,
471 };
472
btrfs_label_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)473 static ssize_t btrfs_label_show(struct kobject *kobj,
474 struct kobj_attribute *a, char *buf)
475 {
476 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
477 char *label = fs_info->super_copy->label;
478 ssize_t ret;
479
480 spin_lock(&fs_info->super_lock);
481 ret = snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
482 spin_unlock(&fs_info->super_lock);
483
484 return ret;
485 }
486
btrfs_label_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)487 static ssize_t btrfs_label_store(struct kobject *kobj,
488 struct kobj_attribute *a,
489 const char *buf, size_t len)
490 {
491 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
492 size_t p_len;
493
494 if (!fs_info)
495 return -EPERM;
496
497 if (sb_rdonly(fs_info->sb))
498 return -EROFS;
499
500 /*
501 * p_len is the len until the first occurrence of either
502 * '\n' or '\0'
503 */
504 p_len = strcspn(buf, "\n");
505
506 if (p_len >= BTRFS_LABEL_SIZE)
507 return -EINVAL;
508
509 spin_lock(&fs_info->super_lock);
510 memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
511 memcpy(fs_info->super_copy->label, buf, p_len);
512 spin_unlock(&fs_info->super_lock);
513
514 /*
515 * We don't want to do full transaction commit from inside sysfs
516 */
517 btrfs_set_pending(fs_info, COMMIT);
518 wake_up_process(fs_info->transaction_kthread);
519
520 return len;
521 }
522 BTRFS_ATTR_RW(, label, btrfs_label_show, btrfs_label_store);
523
btrfs_nodesize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)524 static ssize_t btrfs_nodesize_show(struct kobject *kobj,
525 struct kobj_attribute *a, char *buf)
526 {
527 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
528
529 return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
530 }
531
532 BTRFS_ATTR(, nodesize, btrfs_nodesize_show);
533
btrfs_sectorsize_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)534 static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
535 struct kobj_attribute *a, char *buf)
536 {
537 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
538
539 return snprintf(buf, PAGE_SIZE, "%u\n",
540 fs_info->super_copy->sectorsize);
541 }
542
543 BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
544
btrfs_clone_alignment_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)545 static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
546 struct kobj_attribute *a, char *buf)
547 {
548 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
549
550 return snprintf(buf, PAGE_SIZE, "%u\n",
551 fs_info->super_copy->sectorsize);
552 }
553
554 BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
555
quota_override_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)556 static ssize_t quota_override_show(struct kobject *kobj,
557 struct kobj_attribute *a, char *buf)
558 {
559 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
560 int quota_override;
561
562 quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
563 return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
564 }
565
quota_override_store(struct kobject * kobj,struct kobj_attribute * a,const char * buf,size_t len)566 static ssize_t quota_override_store(struct kobject *kobj,
567 struct kobj_attribute *a,
568 const char *buf, size_t len)
569 {
570 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
571 unsigned long knob;
572 int err;
573
574 if (!fs_info)
575 return -EPERM;
576
577 if (!capable(CAP_SYS_RESOURCE))
578 return -EPERM;
579
580 err = kstrtoul(buf, 10, &knob);
581 if (err)
582 return err;
583 if (knob > 1)
584 return -EINVAL;
585
586 if (knob)
587 set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
588 else
589 clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
590
591 return len;
592 }
593
594 BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
595
btrfs_metadata_uuid_show(struct kobject * kobj,struct kobj_attribute * a,char * buf)596 static ssize_t btrfs_metadata_uuid_show(struct kobject *kobj,
597 struct kobj_attribute *a, char *buf)
598 {
599 struct btrfs_fs_info *fs_info = to_fs_info(kobj);
600
601 return snprintf(buf, PAGE_SIZE, "%pU\n",
602 fs_info->fs_devices->metadata_uuid);
603 }
604
605 BTRFS_ATTR(, metadata_uuid, btrfs_metadata_uuid_show);
606
607 static const struct attribute *btrfs_attrs[] = {
608 BTRFS_ATTR_PTR(, label),
609 BTRFS_ATTR_PTR(, nodesize),
610 BTRFS_ATTR_PTR(, sectorsize),
611 BTRFS_ATTR_PTR(, clone_alignment),
612 BTRFS_ATTR_PTR(, quota_override),
613 BTRFS_ATTR_PTR(, metadata_uuid),
614 NULL,
615 };
616
btrfs_release_fsid_kobj(struct kobject * kobj)617 static void btrfs_release_fsid_kobj(struct kobject *kobj)
618 {
619 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
620
621 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
622 complete(&fs_devs->kobj_unregister);
623 }
624
625 static struct kobj_type btrfs_ktype = {
626 .sysfs_ops = &kobj_sysfs_ops,
627 .release = btrfs_release_fsid_kobj,
628 };
629
to_fs_devs(struct kobject * kobj)630 static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
631 {
632 if (kobj->ktype != &btrfs_ktype)
633 return NULL;
634 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
635 }
636
to_fs_info(struct kobject * kobj)637 static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
638 {
639 if (kobj->ktype != &btrfs_ktype)
640 return NULL;
641 return to_fs_devs(kobj)->fs_info;
642 }
643
644 #define NUM_FEATURE_BITS 64
645 #define BTRFS_FEATURE_NAME_MAX 13
646 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
647 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
648
649 static const u64 supported_feature_masks[FEAT_MAX] = {
650 [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
651 [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
652 [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
653 };
654
addrm_unknown_feature_attrs(struct btrfs_fs_info * fs_info,bool add)655 static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
656 {
657 int set;
658
659 for (set = 0; set < FEAT_MAX; set++) {
660 int i;
661 struct attribute *attrs[2];
662 struct attribute_group agroup = {
663 .name = "features",
664 .attrs = attrs,
665 };
666 u64 features = get_features(fs_info, set);
667 features &= ~supported_feature_masks[set];
668
669 if (!features)
670 continue;
671
672 attrs[1] = NULL;
673 for (i = 0; i < NUM_FEATURE_BITS; i++) {
674 struct btrfs_feature_attr *fa;
675
676 if (!(features & (1ULL << i)))
677 continue;
678
679 fa = &btrfs_feature_attrs[set][i];
680 attrs[0] = &fa->kobj_attr.attr;
681 if (add) {
682 int ret;
683 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
684 &agroup);
685 if (ret)
686 return ret;
687 } else
688 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
689 &agroup);
690 }
691
692 }
693 return 0;
694 }
695
__btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)696 static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
697 {
698 if (fs_devs->device_dir_kobj) {
699 kobject_del(fs_devs->device_dir_kobj);
700 kobject_put(fs_devs->device_dir_kobj);
701 fs_devs->device_dir_kobj = NULL;
702 }
703
704 if (fs_devs->fsid_kobj.state_initialized) {
705 kobject_del(&fs_devs->fsid_kobj);
706 kobject_put(&fs_devs->fsid_kobj);
707 wait_for_completion(&fs_devs->kobj_unregister);
708 }
709 }
710
711 /* when fs_devs is NULL it will remove all fsid kobject */
btrfs_sysfs_remove_fsid(struct btrfs_fs_devices * fs_devs)712 void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
713 {
714 struct list_head *fs_uuids = btrfs_get_fs_uuids();
715
716 if (fs_devs) {
717 __btrfs_sysfs_remove_fsid(fs_devs);
718 return;
719 }
720
721 list_for_each_entry(fs_devs, fs_uuids, fs_list) {
722 __btrfs_sysfs_remove_fsid(fs_devs);
723 }
724 }
725
btrfs_sysfs_remove_mounted(struct btrfs_fs_info * fs_info)726 void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
727 {
728 btrfs_reset_fs_info_ptr(fs_info);
729
730 if (fs_info->space_info_kobj) {
731 sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
732 kobject_del(fs_info->space_info_kobj);
733 kobject_put(fs_info->space_info_kobj);
734 }
735 addrm_unknown_feature_attrs(fs_info, false);
736 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
737 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
738 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
739 }
740
741 static const char * const btrfs_feature_set_names[FEAT_MAX] = {
742 [FEAT_COMPAT] = "compat",
743 [FEAT_COMPAT_RO] = "compat_ro",
744 [FEAT_INCOMPAT] = "incompat",
745 };
746
btrfs_feature_set_name(enum btrfs_feature_set set)747 const char * const btrfs_feature_set_name(enum btrfs_feature_set set)
748 {
749 return btrfs_feature_set_names[set];
750 }
751
btrfs_printable_features(enum btrfs_feature_set set,u64 flags)752 char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
753 {
754 size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
755 int len = 0;
756 int i;
757 char *str;
758
759 str = kmalloc(bufsize, GFP_KERNEL);
760 if (!str)
761 return str;
762
763 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
764 const char *name;
765
766 if (!(flags & (1ULL << i)))
767 continue;
768
769 name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
770 len += snprintf(str + len, bufsize - len, "%s%s",
771 len ? "," : "", name);
772 }
773
774 return str;
775 }
776
init_feature_attrs(void)777 static void init_feature_attrs(void)
778 {
779 struct btrfs_feature_attr *fa;
780 int set, i;
781
782 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
783 ARRAY_SIZE(btrfs_feature_attrs));
784 BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
785 ARRAY_SIZE(btrfs_feature_attrs[0]));
786
787 memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
788 memset(btrfs_unknown_feature_names, 0,
789 sizeof(btrfs_unknown_feature_names));
790
791 for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
792 struct btrfs_feature_attr *sfa;
793 struct attribute *a = btrfs_supported_feature_attrs[i];
794 int bit;
795 sfa = attr_to_btrfs_feature_attr(a);
796 bit = ilog2(sfa->feature_bit);
797 fa = &btrfs_feature_attrs[sfa->feature_set][bit];
798
799 fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
800 }
801
802 for (set = 0; set < FEAT_MAX; set++) {
803 for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
804 char *name = btrfs_unknown_feature_names[set][i];
805 fa = &btrfs_feature_attrs[set][i];
806
807 if (fa->kobj_attr.attr.name)
808 continue;
809
810 snprintf(name, BTRFS_FEATURE_NAME_MAX, "%s:%u",
811 btrfs_feature_set_names[set], i);
812
813 fa->kobj_attr.attr.name = name;
814 fa->kobj_attr.attr.mode = S_IRUGO;
815 fa->feature_set = set;
816 fa->feature_bit = 1ULL << i;
817 }
818 }
819 }
820
821 /*
822 * Create a sysfs entry for a given block group type at path
823 * /sys/fs/btrfs/UUID/allocation/data/TYPE
824 */
btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache * cache)825 void btrfs_sysfs_add_block_group_type(struct btrfs_block_group_cache *cache)
826 {
827 struct btrfs_fs_info *fs_info = cache->fs_info;
828 struct btrfs_space_info *space_info = cache->space_info;
829 struct raid_kobject *rkobj;
830 const int index = btrfs_bg_flags_to_raid_index(cache->flags);
831 unsigned int nofs_flag;
832 int ret;
833
834 /*
835 * Setup a NOFS context because kobject_add(), deep in its call chain,
836 * does GFP_KERNEL allocations, and we are often called in a context
837 * where if reclaim is triggered we can deadlock (we are either holding
838 * a transaction handle or some lock required for a transaction
839 * commit).
840 */
841 nofs_flag = memalloc_nofs_save();
842
843 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
844 if (!rkobj) {
845 memalloc_nofs_restore(nofs_flag);
846 btrfs_warn(cache->fs_info,
847 "couldn't alloc memory for raid level kobject");
848 return;
849 }
850
851 rkobj->flags = cache->flags;
852 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
853 ret = kobject_add(&rkobj->kobj, &space_info->kobj, "%s",
854 btrfs_bg_type_to_raid_name(rkobj->flags));
855 memalloc_nofs_restore(nofs_flag);
856 if (ret) {
857 kobject_put(&rkobj->kobj);
858 btrfs_warn(fs_info,
859 "failed to add kobject for block cache, ignoring");
860 return;
861 }
862
863 space_info->block_group_kobjs[index] = &rkobj->kobj;
864 }
865
866 /*
867 * Remove sysfs directories for all block group types of a given space info and
868 * the space info as well
869 */
btrfs_sysfs_remove_space_info(struct btrfs_space_info * space_info)870 void btrfs_sysfs_remove_space_info(struct btrfs_space_info *space_info)
871 {
872 int i;
873
874 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
875 struct kobject *kobj;
876
877 kobj = space_info->block_group_kobjs[i];
878 space_info->block_group_kobjs[i] = NULL;
879 if (kobj) {
880 kobject_del(kobj);
881 kobject_put(kobj);
882 }
883 }
884 kobject_del(&space_info->kobj);
885 kobject_put(&space_info->kobj);
886 }
887
alloc_name(u64 flags)888 static const char *alloc_name(u64 flags)
889 {
890 switch (flags) {
891 case BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA:
892 return "mixed";
893 case BTRFS_BLOCK_GROUP_METADATA:
894 return "metadata";
895 case BTRFS_BLOCK_GROUP_DATA:
896 return "data";
897 case BTRFS_BLOCK_GROUP_SYSTEM:
898 return "system";
899 default:
900 WARN_ON(1);
901 return "invalid-combination";
902 };
903 }
904
905 /*
906 * Create a sysfs entry for a space info type at path
907 * /sys/fs/btrfs/UUID/allocation/TYPE
908 */
btrfs_sysfs_add_space_info_type(struct btrfs_fs_info * fs_info,struct btrfs_space_info * space_info)909 int btrfs_sysfs_add_space_info_type(struct btrfs_fs_info *fs_info,
910 struct btrfs_space_info *space_info)
911 {
912 int ret;
913
914 ret = kobject_init_and_add(&space_info->kobj, &space_info_ktype,
915 fs_info->space_info_kobj, "%s",
916 alloc_name(space_info->flags));
917 if (ret) {
918 kobject_put(&space_info->kobj);
919 return ret;
920 }
921
922 return 0;
923 }
924
925 /* when one_device is NULL, it removes all device links */
926
btrfs_sysfs_rm_device_link(struct btrfs_fs_devices * fs_devices,struct btrfs_device * one_device)927 int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
928 struct btrfs_device *one_device)
929 {
930 struct hd_struct *disk;
931 struct kobject *disk_kobj;
932
933 if (!fs_devices->device_dir_kobj)
934 return -EINVAL;
935
936 if (one_device && one_device->bdev) {
937 disk = one_device->bdev->bd_part;
938 disk_kobj = &part_to_dev(disk)->kobj;
939
940 sysfs_remove_link(fs_devices->device_dir_kobj,
941 disk_kobj->name);
942 }
943
944 if (one_device)
945 return 0;
946
947 list_for_each_entry(one_device,
948 &fs_devices->devices, dev_list) {
949 if (!one_device->bdev)
950 continue;
951 disk = one_device->bdev->bd_part;
952 disk_kobj = &part_to_dev(disk)->kobj;
953
954 sysfs_remove_link(fs_devices->device_dir_kobj,
955 disk_kobj->name);
956 }
957
958 return 0;
959 }
960
btrfs_sysfs_add_device(struct btrfs_fs_devices * fs_devs)961 int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
962 {
963 if (!fs_devs->device_dir_kobj)
964 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
965 &fs_devs->fsid_kobj);
966
967 if (!fs_devs->device_dir_kobj)
968 return -ENOMEM;
969
970 return 0;
971 }
972
btrfs_sysfs_add_device_link(struct btrfs_fs_devices * fs_devices,struct btrfs_device * one_device)973 int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
974 struct btrfs_device *one_device)
975 {
976 int error = 0;
977 struct btrfs_device *dev;
978
979 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
980 struct hd_struct *disk;
981 struct kobject *disk_kobj;
982
983 if (!dev->bdev)
984 continue;
985
986 if (one_device && one_device != dev)
987 continue;
988
989 disk = dev->bdev->bd_part;
990 disk_kobj = &part_to_dev(disk)->kobj;
991
992 error = sysfs_create_link(fs_devices->device_dir_kobj,
993 disk_kobj, disk_kobj->name);
994 if (error)
995 break;
996 }
997
998 return error;
999 }
1000
btrfs_kobject_uevent(struct block_device * bdev,enum kobject_action action)1001 void btrfs_kobject_uevent(struct block_device *bdev, enum kobject_action action)
1002 {
1003 int ret;
1004
1005 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
1006 if (ret)
1007 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
1008 action, kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
1009 &disk_to_dev(bdev->bd_disk)->kobj);
1010 }
1011
btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices * fs_devices,const u8 * fsid)1012 void btrfs_sysfs_update_sprout_fsid(struct btrfs_fs_devices *fs_devices,
1013 const u8 *fsid)
1014 {
1015 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
1016
1017 /*
1018 * Sprouting changes fsid of the mounted filesystem, rename the fsid
1019 * directory
1020 */
1021 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", fsid);
1022 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
1023 btrfs_warn(fs_devices->fs_info,
1024 "sysfs: failed to create fsid for sprout");
1025 }
1026
1027 /* /sys/fs/btrfs/ entry */
1028 static struct kset *btrfs_kset;
1029
1030 /*
1031 * Can be called by the device discovery thread.
1032 * And parent can be specified for seed device
1033 */
btrfs_sysfs_add_fsid(struct btrfs_fs_devices * fs_devs,struct kobject * parent)1034 int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
1035 struct kobject *parent)
1036 {
1037 int error;
1038
1039 init_completion(&fs_devs->kobj_unregister);
1040 fs_devs->fsid_kobj.kset = btrfs_kset;
1041 error = kobject_init_and_add(&fs_devs->fsid_kobj,
1042 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
1043 if (error) {
1044 kobject_put(&fs_devs->fsid_kobj);
1045 return error;
1046 }
1047
1048 return 0;
1049 }
1050
btrfs_sysfs_add_mounted(struct btrfs_fs_info * fs_info)1051 int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
1052 {
1053 int error;
1054 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
1055 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
1056
1057 btrfs_set_fs_info_ptr(fs_info);
1058
1059 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
1060 if (error)
1061 return error;
1062
1063 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
1064 if (error) {
1065 btrfs_sysfs_rm_device_link(fs_devs, NULL);
1066 return error;
1067 }
1068
1069 error = sysfs_create_group(fsid_kobj,
1070 &btrfs_feature_attr_group);
1071 if (error)
1072 goto failure;
1073
1074 #ifdef CONFIG_BTRFS_DEBUG
1075 error = sysfs_create_group(fsid_kobj,
1076 &btrfs_debug_feature_attr_group);
1077 if (error)
1078 goto failure;
1079 #endif
1080
1081 error = addrm_unknown_feature_attrs(fs_info, true);
1082 if (error)
1083 goto failure;
1084
1085 fs_info->space_info_kobj = kobject_create_and_add("allocation",
1086 fsid_kobj);
1087 if (!fs_info->space_info_kobj) {
1088 error = -ENOMEM;
1089 goto failure;
1090 }
1091
1092 error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
1093 if (error)
1094 goto failure;
1095
1096 return 0;
1097 failure:
1098 btrfs_sysfs_remove_mounted(fs_info);
1099 return error;
1100 }
1101
1102
1103 /*
1104 * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
1105 * values in superblock. Call after any changes to incompat/compat_ro flags
1106 */
btrfs_sysfs_feature_update(struct btrfs_fs_info * fs_info,u64 bit,enum btrfs_feature_set set)1107 void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
1108 u64 bit, enum btrfs_feature_set set)
1109 {
1110 struct btrfs_fs_devices *fs_devs;
1111 struct kobject *fsid_kobj;
1112 u64 features;
1113 int ret;
1114
1115 if (!fs_info)
1116 return;
1117
1118 features = get_features(fs_info, set);
1119 ASSERT(bit & supported_feature_masks[set]);
1120
1121 fs_devs = fs_info->fs_devices;
1122 fsid_kobj = &fs_devs->fsid_kobj;
1123
1124 if (!fsid_kobj->state_initialized)
1125 return;
1126
1127 /*
1128 * FIXME: this is too heavy to update just one value, ideally we'd like
1129 * to use sysfs_update_group but some refactoring is needed first.
1130 */
1131 sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
1132 ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
1133 }
1134
btrfs_init_sysfs(void)1135 int __init btrfs_init_sysfs(void)
1136 {
1137 int ret;
1138
1139 btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
1140 if (!btrfs_kset)
1141 return -ENOMEM;
1142
1143 init_feature_attrs();
1144 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1145 if (ret)
1146 goto out2;
1147 ret = sysfs_merge_group(&btrfs_kset->kobj,
1148 &btrfs_static_feature_attr_group);
1149 if (ret)
1150 goto out_remove_group;
1151
1152 #ifdef CONFIG_BTRFS_DEBUG
1153 ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_debug_feature_attr_group);
1154 if (ret)
1155 goto out2;
1156 #endif
1157
1158 return 0;
1159
1160 out_remove_group:
1161 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1162 out2:
1163 kset_unregister(btrfs_kset);
1164
1165 return ret;
1166 }
1167
btrfs_exit_sysfs(void)1168 void __cold btrfs_exit_sysfs(void)
1169 {
1170 sysfs_unmerge_group(&btrfs_kset->kobj,
1171 &btrfs_static_feature_attr_group);
1172 sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
1173 kset_unregister(btrfs_kset);
1174 }
1175
1176