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