sysfs.c 24 KB

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