sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/completion.h>
  22. #include <linux/buffer_head.h>
  23. #include <linux/kobject.h>
  24. #include <linux/bug.h>
  25. #include <linux/genhd.h>
  26. #include <linux/debugfs.h>
  27. #include "ctree.h"
  28. #include "disk-io.h"
  29. #include "transaction.h"
  30. #include "sysfs.h"
  31. #include "volumes.h"
  32. static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj);
  33. static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj);
  34. static u64 get_features(struct btrfs_fs_info *fs_info,
  35. enum btrfs_feature_set set)
  36. {
  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);
  44. }
  45. static void set_features(struct btrfs_fs_info *fs_info,
  46. enum btrfs_feature_set set, u64 features)
  47. {
  48. struct btrfs_super_block *disk_super = fs_info->super_copy;
  49. if (set == FEAT_COMPAT)
  50. btrfs_set_super_compat_flags(disk_super, features);
  51. else if (set == FEAT_COMPAT_RO)
  52. btrfs_set_super_compat_ro_flags(disk_super, features);
  53. else
  54. btrfs_set_super_incompat_flags(disk_super, features);
  55. }
  56. static int can_modify_feature(struct btrfs_feature_attr *fa)
  57. {
  58. int val = 0;
  59. u64 set, clear;
  60. switch (fa->feature_set) {
  61. case FEAT_COMPAT:
  62. set = BTRFS_FEATURE_COMPAT_SAFE_SET;
  63. clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
  64. break;
  65. case FEAT_COMPAT_RO:
  66. set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
  67. clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
  68. break;
  69. case FEAT_INCOMPAT:
  70. set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
  71. clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
  72. break;
  73. default:
  74. printk(KERN_WARNING "btrfs: sysfs: unknown feature set %d\n",
  75. fa->feature_set);
  76. return 0;
  77. }
  78. if (set & fa->feature_bit)
  79. val |= 1;
  80. if (clear & fa->feature_bit)
  81. val |= 2;
  82. return val;
  83. }
  84. static ssize_t btrfs_feature_attr_show(struct kobject *kobj,
  85. struct kobj_attribute *a, char *buf)
  86. {
  87. int val = 0;
  88. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  89. struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
  90. if (fs_info) {
  91. u64 features = get_features(fs_info, fa->feature_set);
  92. if (features & fa->feature_bit)
  93. val = 1;
  94. } else
  95. val = can_modify_feature(fa);
  96. return snprintf(buf, PAGE_SIZE, "%d\n", val);
  97. }
  98. static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
  99. struct kobj_attribute *a,
  100. const char *buf, size_t count)
  101. {
  102. struct btrfs_fs_info *fs_info;
  103. struct btrfs_feature_attr *fa = to_btrfs_feature_attr(a);
  104. u64 features, set, clear;
  105. unsigned long val;
  106. int ret;
  107. fs_info = to_fs_info(kobj);
  108. if (!fs_info)
  109. return -EPERM;
  110. ret = kstrtoul(skip_spaces(buf), 0, &val);
  111. if (ret)
  112. return ret;
  113. if (fa->feature_set == FEAT_COMPAT) {
  114. set = BTRFS_FEATURE_COMPAT_SAFE_SET;
  115. clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
  116. } else if (fa->feature_set == FEAT_COMPAT_RO) {
  117. set = BTRFS_FEATURE_COMPAT_RO_SAFE_SET;
  118. clear = BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR;
  119. } else {
  120. set = BTRFS_FEATURE_INCOMPAT_SAFE_SET;
  121. clear = BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR;
  122. }
  123. features = get_features(fs_info, fa->feature_set);
  124. /* Nothing to do */
  125. if ((val && (features & fa->feature_bit)) ||
  126. (!val && !(features & fa->feature_bit)))
  127. return count;
  128. if ((val && !(set & fa->feature_bit)) ||
  129. (!val && !(clear & fa->feature_bit))) {
  130. btrfs_info(fs_info,
  131. "%sabling feature %s on mounted fs is not supported.",
  132. val ? "En" : "Dis", fa->kobj_attr.attr.name);
  133. return -EPERM;
  134. }
  135. btrfs_info(fs_info, "%s %s feature flag",
  136. val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
  137. spin_lock(&fs_info->super_lock);
  138. features = get_features(fs_info, fa->feature_set);
  139. if (val)
  140. features |= fa->feature_bit;
  141. else
  142. features &= ~fa->feature_bit;
  143. set_features(fs_info, fa->feature_set, features);
  144. spin_unlock(&fs_info->super_lock);
  145. /*
  146. * We don't want to do full transaction commit from inside sysfs
  147. */
  148. btrfs_set_pending(fs_info, COMMIT);
  149. wake_up_process(fs_info->transaction_kthread);
  150. return count;
  151. }
  152. static umode_t btrfs_feature_visible(struct kobject *kobj,
  153. struct attribute *attr, int unused)
  154. {
  155. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  156. umode_t mode = attr->mode;
  157. if (fs_info) {
  158. struct btrfs_feature_attr *fa;
  159. u64 features;
  160. fa = attr_to_btrfs_feature_attr(attr);
  161. features = get_features(fs_info, fa->feature_set);
  162. if (can_modify_feature(fa))
  163. mode |= S_IWUSR;
  164. else if (!(features & fa->feature_bit))
  165. mode = 0;
  166. }
  167. return mode;
  168. }
  169. BTRFS_FEAT_ATTR_INCOMPAT(mixed_backref, MIXED_BACKREF);
  170. BTRFS_FEAT_ATTR_INCOMPAT(default_subvol, DEFAULT_SUBVOL);
  171. BTRFS_FEAT_ATTR_INCOMPAT(mixed_groups, MIXED_GROUPS);
  172. BTRFS_FEAT_ATTR_INCOMPAT(compress_lzo, COMPRESS_LZO);
  173. BTRFS_FEAT_ATTR_INCOMPAT(big_metadata, BIG_METADATA);
  174. BTRFS_FEAT_ATTR_INCOMPAT(extended_iref, EXTENDED_IREF);
  175. BTRFS_FEAT_ATTR_INCOMPAT(raid56, RAID56);
  176. BTRFS_FEAT_ATTR_INCOMPAT(skinny_metadata, SKINNY_METADATA);
  177. BTRFS_FEAT_ATTR_INCOMPAT(no_holes, NO_HOLES);
  178. BTRFS_FEAT_ATTR_COMPAT_RO(free_space_tree, FREE_SPACE_TREE);
  179. static struct attribute *btrfs_supported_feature_attrs[] = {
  180. BTRFS_FEAT_ATTR_PTR(mixed_backref),
  181. BTRFS_FEAT_ATTR_PTR(default_subvol),
  182. BTRFS_FEAT_ATTR_PTR(mixed_groups),
  183. BTRFS_FEAT_ATTR_PTR(compress_lzo),
  184. BTRFS_FEAT_ATTR_PTR(big_metadata),
  185. BTRFS_FEAT_ATTR_PTR(extended_iref),
  186. BTRFS_FEAT_ATTR_PTR(raid56),
  187. BTRFS_FEAT_ATTR_PTR(skinny_metadata),
  188. BTRFS_FEAT_ATTR_PTR(no_holes),
  189. BTRFS_FEAT_ATTR_PTR(free_space_tree),
  190. NULL
  191. };
  192. static const struct attribute_group btrfs_feature_attr_group = {
  193. .name = "features",
  194. .is_visible = btrfs_feature_visible,
  195. .attrs = btrfs_supported_feature_attrs,
  196. };
  197. static ssize_t btrfs_show_u64(u64 *value_ptr, spinlock_t *lock, char *buf)
  198. {
  199. u64 val;
  200. if (lock)
  201. spin_lock(lock);
  202. val = *value_ptr;
  203. if (lock)
  204. spin_unlock(lock);
  205. return snprintf(buf, PAGE_SIZE, "%llu\n", val);
  206. }
  207. static ssize_t global_rsv_size_show(struct kobject *kobj,
  208. struct kobj_attribute *ka, char *buf)
  209. {
  210. struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
  211. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  212. return btrfs_show_u64(&block_rsv->size, &block_rsv->lock, buf);
  213. }
  214. BTRFS_ATTR(global_rsv_size, global_rsv_size_show);
  215. static ssize_t global_rsv_reserved_show(struct kobject *kobj,
  216. struct kobj_attribute *a, char *buf)
  217. {
  218. struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
  219. struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
  220. return btrfs_show_u64(&block_rsv->reserved, &block_rsv->lock, buf);
  221. }
  222. BTRFS_ATTR(global_rsv_reserved, global_rsv_reserved_show);
  223. #define to_space_info(_kobj) container_of(_kobj, struct btrfs_space_info, kobj)
  224. #define to_raid_kobj(_kobj) container_of(_kobj, struct raid_kobject, kobj)
  225. static ssize_t raid_bytes_show(struct kobject *kobj,
  226. struct kobj_attribute *attr, char *buf);
  227. BTRFS_RAID_ATTR(total_bytes, raid_bytes_show);
  228. BTRFS_RAID_ATTR(used_bytes, raid_bytes_show);
  229. static ssize_t raid_bytes_show(struct kobject *kobj,
  230. struct kobj_attribute *attr, char *buf)
  231. {
  232. struct btrfs_space_info *sinfo = to_space_info(kobj->parent);
  233. struct btrfs_block_group_cache *block_group;
  234. int index = to_raid_kobj(kobj)->raid_type;
  235. u64 val = 0;
  236. down_read(&sinfo->groups_sem);
  237. list_for_each_entry(block_group, &sinfo->block_groups[index], list) {
  238. if (&attr->attr == BTRFS_RAID_ATTR_PTR(total_bytes))
  239. val += block_group->key.offset;
  240. else
  241. val += btrfs_block_group_used(&block_group->item);
  242. }
  243. up_read(&sinfo->groups_sem);
  244. return snprintf(buf, PAGE_SIZE, "%llu\n", val);
  245. }
  246. static struct attribute *raid_attributes[] = {
  247. BTRFS_RAID_ATTR_PTR(total_bytes),
  248. BTRFS_RAID_ATTR_PTR(used_bytes),
  249. NULL
  250. };
  251. static void release_raid_kobj(struct kobject *kobj)
  252. {
  253. kfree(to_raid_kobj(kobj));
  254. }
  255. struct kobj_type btrfs_raid_ktype = {
  256. .sysfs_ops = &kobj_sysfs_ops,
  257. .release = release_raid_kobj,
  258. .default_attrs = raid_attributes,
  259. };
  260. #define SPACE_INFO_ATTR(field) \
  261. static ssize_t btrfs_space_info_show_##field(struct kobject *kobj, \
  262. struct kobj_attribute *a, \
  263. char *buf) \
  264. { \
  265. struct btrfs_space_info *sinfo = to_space_info(kobj); \
  266. return btrfs_show_u64(&sinfo->field, &sinfo->lock, buf); \
  267. } \
  268. BTRFS_ATTR(field, btrfs_space_info_show_##field)
  269. static ssize_t btrfs_space_info_show_total_bytes_pinned(struct kobject *kobj,
  270. struct kobj_attribute *a,
  271. char *buf)
  272. {
  273. struct btrfs_space_info *sinfo = to_space_info(kobj);
  274. s64 val = percpu_counter_sum(&sinfo->total_bytes_pinned);
  275. return snprintf(buf, PAGE_SIZE, "%lld\n", val);
  276. }
  277. SPACE_INFO_ATTR(flags);
  278. SPACE_INFO_ATTR(total_bytes);
  279. SPACE_INFO_ATTR(bytes_used);
  280. SPACE_INFO_ATTR(bytes_pinned);
  281. SPACE_INFO_ATTR(bytes_reserved);
  282. SPACE_INFO_ATTR(bytes_may_use);
  283. SPACE_INFO_ATTR(disk_used);
  284. SPACE_INFO_ATTR(disk_total);
  285. BTRFS_ATTR(total_bytes_pinned, btrfs_space_info_show_total_bytes_pinned);
  286. static struct attribute *space_info_attrs[] = {
  287. BTRFS_ATTR_PTR(flags),
  288. BTRFS_ATTR_PTR(total_bytes),
  289. BTRFS_ATTR_PTR(bytes_used),
  290. BTRFS_ATTR_PTR(bytes_pinned),
  291. BTRFS_ATTR_PTR(bytes_reserved),
  292. BTRFS_ATTR_PTR(bytes_may_use),
  293. BTRFS_ATTR_PTR(disk_used),
  294. BTRFS_ATTR_PTR(disk_total),
  295. BTRFS_ATTR_PTR(total_bytes_pinned),
  296. NULL,
  297. };
  298. static void space_info_release(struct kobject *kobj)
  299. {
  300. struct btrfs_space_info *sinfo = to_space_info(kobj);
  301. percpu_counter_destroy(&sinfo->total_bytes_pinned);
  302. kfree(sinfo);
  303. }
  304. struct kobj_type space_info_ktype = {
  305. .sysfs_ops = &kobj_sysfs_ops,
  306. .release = space_info_release,
  307. .default_attrs = space_info_attrs,
  308. };
  309. static const struct attribute *allocation_attrs[] = {
  310. BTRFS_ATTR_PTR(global_rsv_reserved),
  311. BTRFS_ATTR_PTR(global_rsv_size),
  312. NULL,
  313. };
  314. static ssize_t btrfs_label_show(struct kobject *kobj,
  315. struct kobj_attribute *a, char *buf)
  316. {
  317. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  318. char *label = fs_info->super_copy->label;
  319. return snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label);
  320. }
  321. static ssize_t btrfs_label_store(struct kobject *kobj,
  322. struct kobj_attribute *a,
  323. const char *buf, size_t len)
  324. {
  325. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  326. size_t p_len;
  327. if (fs_info->sb->s_flags & MS_RDONLY)
  328. return -EROFS;
  329. /*
  330. * p_len is the len until the first occurrence of either
  331. * '\n' or '\0'
  332. */
  333. p_len = strcspn(buf, "\n");
  334. if (p_len >= BTRFS_LABEL_SIZE)
  335. return -EINVAL;
  336. spin_lock(&fs_info->super_lock);
  337. memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
  338. memcpy(fs_info->super_copy->label, buf, p_len);
  339. spin_unlock(&fs_info->super_lock);
  340. /*
  341. * We don't want to do full transaction commit from inside sysfs
  342. */
  343. btrfs_set_pending(fs_info, COMMIT);
  344. wake_up_process(fs_info->transaction_kthread);
  345. return len;
  346. }
  347. BTRFS_ATTR_RW(label, btrfs_label_show, btrfs_label_store);
  348. static ssize_t btrfs_nodesize_show(struct kobject *kobj,
  349. struct kobj_attribute *a, char *buf)
  350. {
  351. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  352. return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->nodesize);
  353. }
  354. BTRFS_ATTR(nodesize, btrfs_nodesize_show);
  355. static ssize_t btrfs_sectorsize_show(struct kobject *kobj,
  356. struct kobj_attribute *a, char *buf)
  357. {
  358. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  359. return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
  360. }
  361. BTRFS_ATTR(sectorsize, btrfs_sectorsize_show);
  362. static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
  363. struct kobj_attribute *a, char *buf)
  364. {
  365. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  366. return snprintf(buf, PAGE_SIZE, "%u\n", fs_info->super_copy->sectorsize);
  367. }
  368. BTRFS_ATTR(clone_alignment, btrfs_clone_alignment_show);
  369. static const struct attribute *btrfs_attrs[] = {
  370. BTRFS_ATTR_PTR(label),
  371. BTRFS_ATTR_PTR(nodesize),
  372. BTRFS_ATTR_PTR(sectorsize),
  373. BTRFS_ATTR_PTR(clone_alignment),
  374. NULL,
  375. };
  376. static void btrfs_release_fsid_kobj(struct kobject *kobj)
  377. {
  378. struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
  379. memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
  380. complete(&fs_devs->kobj_unregister);
  381. }
  382. static struct kobj_type btrfs_ktype = {
  383. .sysfs_ops = &kobj_sysfs_ops,
  384. .release = btrfs_release_fsid_kobj,
  385. };
  386. static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
  387. {
  388. if (kobj->ktype != &btrfs_ktype)
  389. return NULL;
  390. return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
  391. }
  392. static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
  393. {
  394. if (kobj->ktype != &btrfs_ktype)
  395. return NULL;
  396. return to_fs_devs(kobj)->fs_info;
  397. }
  398. #define NUM_FEATURE_BITS 64
  399. static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
  400. static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
  401. static const u64 supported_feature_masks[3] = {
  402. [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
  403. [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
  404. [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
  405. };
  406. static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
  407. {
  408. int set;
  409. for (set = 0; set < FEAT_MAX; set++) {
  410. int i;
  411. struct attribute *attrs[2];
  412. struct attribute_group agroup = {
  413. .name = "features",
  414. .attrs = attrs,
  415. };
  416. u64 features = get_features(fs_info, set);
  417. features &= ~supported_feature_masks[set];
  418. if (!features)
  419. continue;
  420. attrs[1] = NULL;
  421. for (i = 0; i < NUM_FEATURE_BITS; i++) {
  422. struct btrfs_feature_attr *fa;
  423. if (!(features & (1ULL << i)))
  424. continue;
  425. fa = &btrfs_feature_attrs[set][i];
  426. attrs[0] = &fa->kobj_attr.attr;
  427. if (add) {
  428. int ret;
  429. ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
  430. &agroup);
  431. if (ret)
  432. return ret;
  433. } else
  434. sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
  435. &agroup);
  436. }
  437. }
  438. return 0;
  439. }
  440. static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
  441. {
  442. if (fs_devs->device_dir_kobj) {
  443. kobject_del(fs_devs->device_dir_kobj);
  444. kobject_put(fs_devs->device_dir_kobj);
  445. fs_devs->device_dir_kobj = NULL;
  446. }
  447. if (fs_devs->fsid_kobj.state_initialized) {
  448. kobject_del(&fs_devs->fsid_kobj);
  449. kobject_put(&fs_devs->fsid_kobj);
  450. wait_for_completion(&fs_devs->kobj_unregister);
  451. }
  452. }
  453. /* when fs_devs is NULL it will remove all fsid kobject */
  454. void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
  455. {
  456. struct list_head *fs_uuids = btrfs_get_fs_uuids();
  457. if (fs_devs) {
  458. __btrfs_sysfs_remove_fsid(fs_devs);
  459. return;
  460. }
  461. list_for_each_entry(fs_devs, fs_uuids, list) {
  462. __btrfs_sysfs_remove_fsid(fs_devs);
  463. }
  464. }
  465. void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
  466. {
  467. btrfs_reset_fs_info_ptr(fs_info);
  468. if (fs_info->space_info_kobj) {
  469. sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
  470. kobject_del(fs_info->space_info_kobj);
  471. kobject_put(fs_info->space_info_kobj);
  472. }
  473. addrm_unknown_feature_attrs(fs_info, false);
  474. sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
  475. sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
  476. btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
  477. }
  478. const char * const btrfs_feature_set_names[3] = {
  479. [FEAT_COMPAT] = "compat",
  480. [FEAT_COMPAT_RO] = "compat_ro",
  481. [FEAT_INCOMPAT] = "incompat",
  482. };
  483. char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
  484. {
  485. size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
  486. int len = 0;
  487. int i;
  488. char *str;
  489. str = kmalloc(bufsize, GFP_KERNEL);
  490. if (!str)
  491. return str;
  492. for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
  493. const char *name;
  494. if (!(flags & (1ULL << i)))
  495. continue;
  496. name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
  497. len += snprintf(str + len, bufsize - len, "%s%s",
  498. len ? "," : "", name);
  499. }
  500. return str;
  501. }
  502. static void init_feature_attrs(void)
  503. {
  504. struct btrfs_feature_attr *fa;
  505. int set, i;
  506. BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
  507. ARRAY_SIZE(btrfs_feature_attrs));
  508. BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
  509. ARRAY_SIZE(btrfs_feature_attrs[0]));
  510. memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
  511. memset(btrfs_unknown_feature_names, 0,
  512. sizeof(btrfs_unknown_feature_names));
  513. for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
  514. struct btrfs_feature_attr *sfa;
  515. struct attribute *a = btrfs_supported_feature_attrs[i];
  516. int bit;
  517. sfa = attr_to_btrfs_feature_attr(a);
  518. bit = ilog2(sfa->feature_bit);
  519. fa = &btrfs_feature_attrs[sfa->feature_set][bit];
  520. fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
  521. }
  522. for (set = 0; set < FEAT_MAX; set++) {
  523. for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
  524. char *name = btrfs_unknown_feature_names[set][i];
  525. fa = &btrfs_feature_attrs[set][i];
  526. if (fa->kobj_attr.attr.name)
  527. continue;
  528. snprintf(name, 13, "%s:%u",
  529. btrfs_feature_set_names[set], i);
  530. fa->kobj_attr.attr.name = name;
  531. fa->kobj_attr.attr.mode = S_IRUGO;
  532. fa->feature_set = set;
  533. fa->feature_bit = 1ULL << i;
  534. }
  535. }
  536. }
  537. /* when one_device is NULL, it removes all device links */
  538. int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
  539. struct btrfs_device *one_device)
  540. {
  541. struct hd_struct *disk;
  542. struct kobject *disk_kobj;
  543. if (!fs_devices->device_dir_kobj)
  544. return -EINVAL;
  545. if (one_device && one_device->bdev) {
  546. disk = one_device->bdev->bd_part;
  547. disk_kobj = &part_to_dev(disk)->kobj;
  548. sysfs_remove_link(fs_devices->device_dir_kobj,
  549. disk_kobj->name);
  550. }
  551. if (one_device)
  552. return 0;
  553. list_for_each_entry(one_device,
  554. &fs_devices->devices, dev_list) {
  555. if (!one_device->bdev)
  556. continue;
  557. disk = one_device->bdev->bd_part;
  558. disk_kobj = &part_to_dev(disk)->kobj;
  559. sysfs_remove_link(fs_devices->device_dir_kobj,
  560. disk_kobj->name);
  561. }
  562. return 0;
  563. }
  564. int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
  565. {
  566. if (!fs_devs->device_dir_kobj)
  567. fs_devs->device_dir_kobj = kobject_create_and_add("devices",
  568. &fs_devs->fsid_kobj);
  569. if (!fs_devs->device_dir_kobj)
  570. return -ENOMEM;
  571. return 0;
  572. }
  573. int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
  574. struct btrfs_device *one_device)
  575. {
  576. int error = 0;
  577. struct btrfs_device *dev;
  578. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  579. struct hd_struct *disk;
  580. struct kobject *disk_kobj;
  581. if (!dev->bdev)
  582. continue;
  583. if (one_device && one_device != dev)
  584. continue;
  585. disk = dev->bdev->bd_part;
  586. disk_kobj = &part_to_dev(disk)->kobj;
  587. error = sysfs_create_link(fs_devices->device_dir_kobj,
  588. disk_kobj, disk_kobj->name);
  589. if (error)
  590. break;
  591. }
  592. return error;
  593. }
  594. /* /sys/fs/btrfs/ entry */
  595. static struct kset *btrfs_kset;
  596. /* /sys/kernel/debug/btrfs */
  597. static struct dentry *btrfs_debugfs_root_dentry;
  598. /* Debugging tunables and exported data */
  599. u64 btrfs_debugfs_test;
  600. /*
  601. * Can be called by the device discovery thread.
  602. * And parent can be specified for seed device
  603. */
  604. int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
  605. struct kobject *parent)
  606. {
  607. int error;
  608. init_completion(&fs_devs->kobj_unregister);
  609. fs_devs->fsid_kobj.kset = btrfs_kset;
  610. error = kobject_init_and_add(&fs_devs->fsid_kobj,
  611. &btrfs_ktype, parent, "%pU", fs_devs->fsid);
  612. return error;
  613. }
  614. int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
  615. {
  616. int error;
  617. struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
  618. struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
  619. btrfs_set_fs_info_ptr(fs_info);
  620. error = btrfs_sysfs_add_device_link(fs_devs, NULL);
  621. if (error)
  622. return error;
  623. error = sysfs_create_files(fsid_kobj, btrfs_attrs);
  624. if (error) {
  625. btrfs_sysfs_rm_device_link(fs_devs, NULL);
  626. return error;
  627. }
  628. error = sysfs_create_group(fsid_kobj,
  629. &btrfs_feature_attr_group);
  630. if (error)
  631. goto failure;
  632. error = addrm_unknown_feature_attrs(fs_info, true);
  633. if (error)
  634. goto failure;
  635. fs_info->space_info_kobj = kobject_create_and_add("allocation",
  636. fsid_kobj);
  637. if (!fs_info->space_info_kobj) {
  638. error = -ENOMEM;
  639. goto failure;
  640. }
  641. error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
  642. if (error)
  643. goto failure;
  644. return 0;
  645. failure:
  646. btrfs_sysfs_remove_mounted(fs_info);
  647. return error;
  648. }
  649. /*
  650. * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
  651. * values in superblock. Call after any changes to incompat/compat_ro flags
  652. */
  653. void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
  654. u64 bit, enum btrfs_feature_set set)
  655. {
  656. struct btrfs_fs_devices *fs_devs;
  657. struct kobject *fsid_kobj;
  658. u64 features;
  659. int ret;
  660. if (!fs_info)
  661. return;
  662. features = get_features(fs_info, set);
  663. ASSERT(bit & supported_feature_masks[set]);
  664. fs_devs = fs_info->fs_devices;
  665. fsid_kobj = &fs_devs->fsid_kobj;
  666. if (!fsid_kobj->state_initialized)
  667. return;
  668. /*
  669. * FIXME: this is too heavy to update just one value, ideally we'd like
  670. * to use sysfs_update_group but some refactoring is needed first.
  671. */
  672. sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
  673. ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
  674. }
  675. static int btrfs_init_debugfs(void)
  676. {
  677. #ifdef CONFIG_DEBUG_FS
  678. btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
  679. if (!btrfs_debugfs_root_dentry)
  680. return -ENOMEM;
  681. debugfs_create_u64("test", S_IRUGO | S_IWUGO, btrfs_debugfs_root_dentry,
  682. &btrfs_debugfs_test);
  683. #endif
  684. return 0;
  685. }
  686. int btrfs_init_sysfs(void)
  687. {
  688. int ret;
  689. btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
  690. if (!btrfs_kset)
  691. return -ENOMEM;
  692. ret = btrfs_init_debugfs();
  693. if (ret)
  694. goto out1;
  695. init_feature_attrs();
  696. ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
  697. if (ret)
  698. goto out2;
  699. return 0;
  700. out2:
  701. debugfs_remove_recursive(btrfs_debugfs_root_dentry);
  702. out1:
  703. kset_unregister(btrfs_kset);
  704. return ret;
  705. }
  706. void btrfs_exit_sysfs(void)
  707. {
  708. sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
  709. kset_unregister(btrfs_kset);
  710. debugfs_remove_recursive(btrfs_debugfs_root_dentry);
  711. }