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