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