]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
58176a96 JB |
19 | #include <linux/sched.h> |
20 | #include <linux/slab.h> | |
21 | #include <linux/spinlock.h> | |
22 | #include <linux/completion.h> | |
23 | #include <linux/buffer_head.h> | |
58176a96 | 24 | #include <linux/kobject.h> |
79da4fa4 | 25 | #include <linux/bug.h> |
58176a96 | 26 | |
bae45de0 CM |
27 | #include "ctree.h" |
28 | #include "disk-io.h" | |
29 | #include "transaction.h" | |
079b72bc JM |
30 | #include "sysfs.h" |
31 | ||
510d7360 | 32 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj); |
5ac1d209 | 33 | |
510d7360 JM |
34 | static u64 get_features(struct btrfs_fs_info *fs_info, |
35 | enum btrfs_feature_set set) | |
5ac1d209 | 36 | { |
510d7360 JM |
37 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
38 | if (set == FEAT_COMPAT) | |
39 | return btrfs_super_compat_flags(disk_super); | |
40 | else if (set == FEAT_COMPAT_RO) | |
41 | return btrfs_super_compat_ro_flags(disk_super); | |
42 | else | |
43 | return btrfs_super_incompat_flags(disk_super); | |
5ac1d209 JM |
44 | } |
45 | ||
ba631941 JM |
46 | static void set_features(struct btrfs_fs_info *fs_info, |
47 | enum btrfs_feature_set set, u64 features) | |
48 | { | |
49 | struct btrfs_super_block *disk_super = fs_info->super_copy; | |
50 | if (set == FEAT_COMPAT) | |
51 | btrfs_set_super_compat_flags(disk_super, features); | |
52 | else if (set == FEAT_COMPAT_RO) | |
53 | btrfs_set_super_compat_ro_flags(disk_super, features); | |
54 | else | |
55 | btrfs_set_super_incompat_flags(disk_super, features); | |
56 | } | |
57 | ||
58 | static int can_modify_feature(struct btrfs_feature_attr *fa) | |
59 | { | |
60 | int val = 0; | |
61 | u64 set, clear; | |
62 | switch (fa->feature_set) { | |
63 | case FEAT_COMPAT: | |
64 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; | |
65 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; | |
66 | break; | |
67 | case FEAT_COMPAT_RO: | |
68 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; | |
69 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; | |
70 | break; | |
71 | case FEAT_INCOMPAT: | |
72 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; | |
73 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; | |
74 | break; | |
75 | default: | |
76 | BUG(); | |
77 | } | |
78 | ||
79 | if (set & fa->feature_bit) | |
80 | val |= 1; | |
81 | if (clear & fa->feature_bit) | |
82 | val |= 2; | |
83 | ||
84 | return val; | |
85 | } | |
86 | ||
510d7360 JM |
87 | static ssize_t btrfs_feature_attr_show(struct kobject *kobj, |
88 | struct kobj_attribute *a, char *buf) | |
5ac1d209 | 89 | { |
510d7360 | 90 | int val = 0; |
5ac1d209 | 91 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
ba631941 | 92 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); |
510d7360 | 93 | if (fs_info) { |
510d7360 JM |
94 | u64 features = get_features(fs_info, fa->feature_set); |
95 | if (features & fa->feature_bit) | |
96 | val = 1; | |
ba631941 JM |
97 | } else |
98 | val = can_modify_feature(fa); | |
510d7360 JM |
99 | |
100 | return snprintf(buf, PAGE_SIZE, "%d\n", val); | |
5ac1d209 JM |
101 | } |
102 | ||
ba631941 JM |
103 | static ssize_t btrfs_feature_attr_store(struct kobject *kobj, |
104 | struct kobj_attribute *a, | |
105 | const char *buf, size_t count) | |
106 | { | |
107 | struct btrfs_fs_info *fs_info; | |
108 | struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a); | |
109 | struct btrfs_trans_handle *trans; | |
110 | u64 features, set, clear; | |
111 | unsigned long val; | |
112 | int ret; | |
113 | ||
114 | fs_info = to_fs_info(kobj); | |
115 | if (!fs_info) | |
116 | return -EPERM; | |
117 | ||
118 | ret = kstrtoul(skip_spaces(buf), 0, &val); | |
119 | if (ret) | |
120 | return ret; | |
121 | ||
122 | if (fa->feature_set == FEAT_COMPAT) { | |
123 | set = BTRFS_FEATURE_COMPAT_SAFE_SET; | |
124 | clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR; | |
125 | } else if (fa->feature_set == FEAT_COMPAT_RO) { | |
126 | set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET; | |
127 | clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR; | |
128 | } else { | |
129 | set = BTRFS_FEATURE_INCOMPAT_SAFE_SET; | |
130 | clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR; | |
131 | } | |
132 | ||
133 | features = get_features(fs_info, fa->feature_set); | |
134 | ||
135 | /* Nothing to do */ | |
136 | if ((val && (features & fa->feature_bit)) || | |
137 | (!val && !(features & fa->feature_bit))) | |
138 | return count; | |
139 | ||
140 | if ((val && !(set & fa->feature_bit)) || | |
141 | (!val && !(clear & fa->feature_bit))) { | |
142 | btrfs_info(fs_info, | |
143 | "%sabling feature %s on mounted fs is not supported.", | |
144 | val ? "En" : "Dis", fa->kobj_attr.attr.name); | |
145 | return -EPERM; | |
146 | } | |
147 | ||
148 | btrfs_info(fs_info, "%s %s feature flag", | |
149 | val ? "Setting" : "Clearing", fa->kobj_attr.attr.name); | |
150 | ||
151 | trans = btrfs_start_transaction(fs_info->fs_root, 1); | |
152 | if (IS_ERR(trans)) | |
153 | return PTR_ERR(trans); | |
154 | ||
155 | spin_lock(&fs_info->super_lock); | |
156 | features = get_features(fs_info, fa->feature_set); | |
157 | if (val) | |
158 | features |= fa->feature_bit; | |
159 | else | |
160 | features &= ~fa->feature_bit; | |
161 | set_features(fs_info, fa->feature_set, features); | |
162 | spin_unlock(&fs_info->super_lock); | |
163 | ||
164 | ret = btrfs_commit_transaction(trans, fs_info->fs_root); | |
165 | if (ret) | |
166 | return ret; | |
167 | ||
168 | return count; | |
169 | } | |
170 | ||
510d7360 JM |
171 | static umode_t btrfs_feature_visible(struct kobject *kobj, |
172 | struct attribute *attr, int unused) | |
079b72bc | 173 | { |
510d7360 JM |
174 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); |
175 | umode_t mode = attr->mode; | |
176 | ||
177 | if (fs_info) { | |
178 | struct btrfs_feature_attr *fa; | |
179 | u64 features; | |
180 | ||
181 | fa = attr_to_btrfs_feature_attr(attr); | |
182 | features = get_features(fs_info, fa->feature_set); | |
183 | ||
ba631941 JM |
184 | if (can_modify_feature(fa)) |
185 | mode |= S_IWUSR; | |
186 | else if (!(features & fa->feature_bit)) | |
510d7360 JM |
187 | mode = 0; |
188 | } | |
189 | ||
190 | return mode; | |
079b72bc JM |
191 | } |
192 | ||
193 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF); | |
194 | BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL); | |
195 | BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS); | |
196 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO); | |
197 | BTRFS_FEAT_ATTR_INCOMPAT(compress_lzov2, COMPRESS_LZOv2); | |
198 | BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA); | |
199 | BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF); | |
200 | BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56); | |
201 | BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA); | |
202 | ||
203 | static struct attribute *btrfs_supported_feature_attrs[] = { | |
204 | BTRFS_FEAT_ATTR_PTR(mixed_backref), | |
205 | BTRFS_FEAT_ATTR_PTR(default_subvol), | |
206 | BTRFS_FEAT_ATTR_PTR(mixed_groups), | |
207 | BTRFS_FEAT_ATTR_PTR(compress_lzo), | |
208 | BTRFS_FEAT_ATTR_PTR(compress_lzov2), | |
209 | BTRFS_FEAT_ATTR_PTR(big_metadata), | |
210 | BTRFS_FEAT_ATTR_PTR(extended_iref), | |
211 | BTRFS_FEAT_ATTR_PTR(raid56), | |
212 | BTRFS_FEAT_ATTR_PTR(skinny_metadata), | |
213 | NULL | |
214 | }; | |
215 | ||
216 | static const struct attribute_group btrfs_feature_attr_group = { | |
217 | .name = "features", | |
510d7360 | 218 | .is_visible = btrfs_feature_visible, |
079b72bc JM |
219 | .attrs = btrfs_supported_feature_attrs, |
220 | }; | |
58176a96 | 221 | |
6ab0a202 JM |
222 | static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf) |
223 | { | |
224 | u64 val; | |
225 | if (lock) | |
226 | spin_lock(lock); | |
227 | val = *value_ptr; | |
228 | if (lock) | |
229 | spin_unlock(lock); | |
230 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); | |
231 | } | |
232 | ||
233 | static ssize_t global_rsv_size_show(struct kobject *kobj, | |
234 | struct kobj_attribute *ka, char *buf) | |
235 | { | |
236 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); | |
237 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; | |
238 | return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf); | |
239 | } | |
240 | BTRFS_ATTR(global_rsv_size, 0444, global_rsv_size_show); | |
241 | ||
242 | static ssize_t global_rsv_reserved_show(struct kobject *kobj, | |
243 | struct kobj_attribute *a, char *buf) | |
244 | { | |
245 | struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent); | |
246 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; | |
247 | return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf); | |
248 | } | |
249 | BTRFS_ATTR(global_rsv_reserved, 0444, global_rsv_reserved_show); | |
250 | ||
251 | #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj) | |
252 | ||
253 | static ssize_t raid_bytes_show(struct kobject *kobj, | |
254 | struct kobj_attribute *attr, char *buf); | |
255 | BTRFS_RAID_ATTR(total_bytes, raid_bytes_show); | |
256 | BTRFS_RAID_ATTR(used_bytes, raid_bytes_show); | |
257 | ||
258 | static ssize_t raid_bytes_show(struct kobject *kobj, | |
259 | struct kobj_attribute *attr, char *buf) | |
260 | ||
261 | { | |
262 | struct btrfs_space_info *sinfo = to_space_info(kobj->parent); | |
263 | struct btrfs_block_group_cache *block_group; | |
264 | int index = kobj - sinfo->block_group_kobjs; | |
265 | u64 val = 0; | |
266 | ||
267 | down_read(&sinfo->groups_sem); | |
268 | list_for_each_entry(block_group, &sinfo->block_groups[index], list) { | |
269 | if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes)) | |
270 | val += block_group->key.offset; | |
271 | else | |
272 | val += btrfs_block_group_used(&block_group->item); | |
273 | } | |
274 | up_read(&sinfo->groups_sem); | |
275 | return snprintf(buf, PAGE_SIZE, "%llu\n", val); | |
276 | } | |
277 | ||
278 | static struct attribute *raid_attributes[] = { | |
279 | BTRFS_RAID_ATTR_PTR(total_bytes), | |
280 | BTRFS_RAID_ATTR_PTR(used_bytes), | |
281 | NULL | |
282 | }; | |
283 | ||
284 | static void release_raid_kobj(struct kobject *kobj) | |
285 | { | |
286 | kobject_put(kobj->parent); | |
287 | } | |
288 | ||
289 | struct kobj_type btrfs_raid_ktype = { | |
290 | .sysfs_ops = &kobj_sysfs_ops, | |
291 | .release = release_raid_kobj, | |
292 | .default_attrs = raid_attributes, | |
293 | }; | |
294 | ||
295 | #define SPACE_INFO_ATTR(field) \ | |
296 | static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \ | |
297 | struct kobj_attribute *a, \ | |
298 | char *buf) \ | |
299 | { \ | |
300 | struct btrfs_space_info *sinfo = to_space_info(kobj); \ | |
301 | return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \ | |
302 | } \ | |
303 | BTRFS_ATTR(field, 0444, btrfs_space_info_show_##field) | |
304 | ||
305 | static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj, | |
306 | struct kobj_attribute *a, | |
307 | char *buf) | |
308 | { | |
309 | struct btrfs_space_info *sinfo = to_space_info(kobj); | |
310 | s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned); | |
311 | return snprintf(buf, PAGE_SIZE, "%lld\n", val); | |
312 | } | |
313 | ||
314 | SPACE_INFO_ATTR(flags); | |
315 | SPACE_INFO_ATTR(total_bytes); | |
316 | SPACE_INFO_ATTR(bytes_used); | |
317 | SPACE_INFO_ATTR(bytes_pinned); | |
318 | SPACE_INFO_ATTR(bytes_reserved); | |
319 | SPACE_INFO_ATTR(bytes_may_use); | |
320 | SPACE_INFO_ATTR(disk_used); | |
321 | SPACE_INFO_ATTR(disk_total); | |
322 | BTRFS_ATTR(total_bytes_pinned, 0444, btrfs_space_info_show_total_bytes_pinned); | |
323 | ||
324 | static struct attribute *space_info_attrs[] = { | |
325 | BTRFS_ATTR_PTR(flags), | |
326 | BTRFS_ATTR_PTR(total_bytes), | |
327 | BTRFS_ATTR_PTR(bytes_used), | |
328 | BTRFS_ATTR_PTR(bytes_pinned), | |
329 | BTRFS_ATTR_PTR(bytes_reserved), | |
330 | BTRFS_ATTR_PTR(bytes_may_use), | |
331 | BTRFS_ATTR_PTR(disk_used), | |
332 | BTRFS_ATTR_PTR(disk_total), | |
333 | BTRFS_ATTR_PTR(total_bytes_pinned), | |
334 | NULL, | |
335 | }; | |
336 | ||
337 | static void space_info_release(struct kobject *kobj) | |
338 | { | |
339 | struct btrfs_space_info *sinfo = to_space_info(kobj); | |
340 | percpu_counter_destroy(&sinfo->total_bytes_pinned); | |
341 | kfree(sinfo); | |
342 | } | |
343 | ||
344 | struct kobj_type space_info_ktype = { | |
345 | .sysfs_ops = &kobj_sysfs_ops, | |
346 | .release = space_info_release, | |
347 | .default_attrs = space_info_attrs, | |
348 | }; | |
349 | ||
350 | static const struct attribute *allocation_attrs[] = { | |
351 | BTRFS_ATTR_PTR(global_rsv_reserved), | |
352 | BTRFS_ATTR_PTR(global_rsv_size), | |
353 | NULL, | |
354 | }; | |
355 | ||
510d7360 JM |
356 | static void btrfs_release_super_kobj(struct kobject *kobj) |
357 | { | |
358 | struct btrfs_fs_info *fs_info = to_fs_info(kobj); | |
359 | complete(&fs_info->kobj_unregister); | |
360 | } | |
361 | ||
362 | static struct kobj_type btrfs_ktype = { | |
363 | .sysfs_ops = &kobj_sysfs_ops, | |
364 | .release = btrfs_release_super_kobj, | |
365 | }; | |
366 | ||
367 | static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) | |
368 | { | |
369 | if (kobj->ktype != &btrfs_ktype) | |
370 | return NULL; | |
371 | return container_of(kobj, struct btrfs_fs_info, super_kobj); | |
372 | } | |
58176a96 | 373 | |
5ac1d209 JM |
374 | void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info) |
375 | { | |
6ab0a202 JM |
376 | sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs); |
377 | kobject_del(fs_info->space_info_kobj); | |
378 | kobject_put(fs_info->space_info_kobj); | |
5ac1d209 JM |
379 | kobject_del(&fs_info->super_kobj); |
380 | kobject_put(&fs_info->super_kobj); | |
381 | wait_for_completion(&fs_info->kobj_unregister); | |
382 | } | |
383 | ||
79da4fa4 JM |
384 | const char * const btrfs_feature_set_names[3] = { |
385 | [FEAT_COMPAT] = "compat", | |
386 | [FEAT_COMPAT_RO] = "compat_ro", | |
387 | [FEAT_INCOMPAT] = "incompat", | |
388 | }; | |
389 | ||
390 | #define NUM_FEATURE_BITS 64 | |
391 | static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13]; | |
392 | static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS]; | |
393 | ||
3b02a68a JM |
394 | char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags) |
395 | { | |
396 | size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */ | |
397 | int len = 0; | |
398 | int i; | |
399 | char *str; | |
400 | ||
401 | str = kmalloc(bufsize, GFP_KERNEL); | |
402 | if (!str) | |
403 | return str; | |
404 | ||
405 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { | |
406 | const char *name; | |
407 | ||
408 | if (!(flags & (1ULL << i))) | |
409 | continue; | |
410 | ||
411 | name = btrfs_feature_attrs[set][i].kobj_attr.attr.name; | |
412 | len += snprintf(str + len, bufsize - len, "%s%s", | |
413 | len ? "," : "", name); | |
414 | } | |
415 | ||
416 | return str; | |
417 | } | |
418 | ||
79da4fa4 JM |
419 | static void init_feature_attrs(void) |
420 | { | |
421 | struct btrfs_feature_attr *fa; | |
422 | int set, i; | |
423 | ||
424 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) != | |
425 | ARRAY_SIZE(btrfs_feature_attrs)); | |
426 | BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) != | |
427 | ARRAY_SIZE(btrfs_feature_attrs[0])); | |
428 | ||
3b02a68a JM |
429 | memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs)); |
430 | memset(btrfs_unknown_feature_names, 0, | |
431 | sizeof(btrfs_unknown_feature_names)); | |
432 | ||
79da4fa4 JM |
433 | for (i = 0; btrfs_supported_feature_attrs[i]; i++) { |
434 | struct btrfs_feature_attr *sfa; | |
435 | struct attribute *a = btrfs_supported_feature_attrs[i]; | |
3b02a68a | 436 | int bit; |
79da4fa4 | 437 | sfa = attr_to_btrfs_feature_attr(a); |
3b02a68a JM |
438 | bit = ilog2(sfa->feature_bit); |
439 | fa = &btrfs_feature_attrs[sfa->feature_set][bit]; | |
79da4fa4 JM |
440 | |
441 | fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name; | |
442 | } | |
443 | ||
444 | for (set = 0; set < FEAT_MAX; set++) { | |
445 | for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) { | |
446 | char *name = btrfs_unknown_feature_names[set][i]; | |
447 | fa = &btrfs_feature_attrs[set][i]; | |
448 | ||
449 | if (fa->kobj_attr.attr.name) | |
450 | continue; | |
451 | ||
452 | snprintf(name, 13, "%s:%u", | |
453 | btrfs_feature_set_names[set], i); | |
454 | ||
455 | fa->kobj_attr.attr.name = name; | |
456 | fa->kobj_attr.attr.mode = S_IRUGO; | |
457 | fa->feature_set = set; | |
458 | fa->feature_bit = 1ULL << i; | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | static u64 supported_feature_masks[3] = { | |
464 | [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP, | |
465 | [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP, | |
466 | [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP, | |
467 | }; | |
468 | ||
469 | static int add_unknown_feature_attrs(struct btrfs_fs_info *fs_info) | |
470 | { | |
471 | int set; | |
472 | ||
473 | for (set = 0; set < FEAT_MAX; set++) { | |
474 | int i, count, ret, index = 0; | |
475 | struct attribute **attrs; | |
476 | struct attribute_group agroup = { | |
477 | .name = "features", | |
478 | }; | |
479 | u64 features = get_features(fs_info, set); | |
480 | features &= ~supported_feature_masks[set]; | |
481 | ||
482 | count = hweight64(features); | |
483 | ||
484 | if (!count) | |
485 | continue; | |
486 | ||
487 | attrs = kcalloc(count + 1, sizeof(void *), GFP_KERNEL); | |
488 | ||
489 | for (i = 0; i < NUM_FEATURE_BITS; i++) { | |
490 | struct btrfs_feature_attr *fa; | |
491 | ||
492 | if (!(features & (1ULL << i))) | |
493 | continue; | |
494 | ||
495 | fa = &btrfs_feature_attrs[set][i]; | |
496 | attrs[index++] = &fa->kobj_attr.attr; | |
497 | } | |
498 | ||
499 | attrs[index] = NULL; | |
500 | agroup.attrs = attrs; | |
501 | ||
502 | ret = sysfs_merge_group(&fs_info->super_kobj, &agroup); | |
503 | kfree(attrs); | |
504 | if (ret) | |
505 | return ret; | |
506 | } | |
507 | return 0; | |
508 | } | |
509 | ||
510d7360 JM |
510 | /* /sys/fs/btrfs/ entry */ |
511 | static struct kset *btrfs_kset; | |
512 | ||
5ac1d209 JM |
513 | int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info) |
514 | { | |
515 | int error; | |
516 | ||
517 | init_completion(&fs_info->kobj_unregister); | |
510d7360 | 518 | fs_info->super_kobj.kset = btrfs_kset; |
5ac1d209 JM |
519 | error = kobject_init_and_add(&fs_info->super_kobj, &btrfs_ktype, NULL, |
520 | "%pU", fs_info->fsid); | |
510d7360 JM |
521 | |
522 | error = sysfs_create_group(&fs_info->super_kobj, | |
523 | &btrfs_feature_attr_group); | |
524 | if (error) | |
79da4fa4 JM |
525 | goto failure; |
526 | ||
527 | error = add_unknown_feature_attrs(fs_info); | |
528 | if (error) | |
529 | goto failure; | |
530 | ||
6ab0a202 JM |
531 | fs_info->space_info_kobj = kobject_create_and_add("allocation", |
532 | &fs_info->super_kobj); | |
533 | if (!fs_info->space_info_kobj) { | |
534 | error = -ENOMEM; | |
535 | goto failure; | |
536 | } | |
537 | ||
538 | error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs); | |
539 | if (error) | |
540 | goto failure; | |
541 | ||
79da4fa4 JM |
542 | return 0; |
543 | failure: | |
544 | btrfs_sysfs_remove_one(fs_info); | |
5ac1d209 JM |
545 | return error; |
546 | } | |
547 | ||
b214107e | 548 | int btrfs_init_sysfs(void) |
58176a96 | 549 | { |
079b72bc | 550 | int ret; |
e3fe4e71 GKH |
551 | btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj); |
552 | if (!btrfs_kset) | |
553 | return -ENOMEM; | |
079b72bc | 554 | |
79da4fa4 JM |
555 | init_feature_attrs(); |
556 | ||
079b72bc JM |
557 | ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
558 | if (ret) { | |
559 | kset_unregister(btrfs_kset); | |
560 | return ret; | |
561 | } | |
562 | ||
e3fe4e71 | 563 | return 0; |
58176a96 JB |
564 | } |
565 | ||
b214107e | 566 | void btrfs_exit_sysfs(void) |
58176a96 | 567 | { |
079b72bc | 568 | sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group); |
e3fe4e71 | 569 | kset_unregister(btrfs_kset); |
58176a96 | 570 | } |
55d47414 | 571 |