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