sysfs.c 22 KB

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