sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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->super_copy->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",
  373. fs_info->super_copy->sectorsize);
  374. }
  375. BTRFS_ATTR(, sectorsize, btrfs_sectorsize_show);
  376. static ssize_t btrfs_clone_alignment_show(struct kobject *kobj,
  377. struct kobj_attribute *a, char *buf)
  378. {
  379. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  380. return snprintf(buf, PAGE_SIZE, "%u\n",
  381. fs_info->super_copy->sectorsize);
  382. }
  383. BTRFS_ATTR(, clone_alignment, btrfs_clone_alignment_show);
  384. static ssize_t quota_override_show(struct kobject *kobj,
  385. struct kobj_attribute *a, char *buf)
  386. {
  387. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  388. int quota_override;
  389. quota_override = test_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
  390. return snprintf(buf, PAGE_SIZE, "%d\n", quota_override);
  391. }
  392. static ssize_t quota_override_store(struct kobject *kobj,
  393. struct kobj_attribute *a,
  394. const char *buf, size_t len)
  395. {
  396. struct btrfs_fs_info *fs_info = to_fs_info(kobj);
  397. unsigned long knob;
  398. int err;
  399. if (!fs_info)
  400. return -EPERM;
  401. if (!capable(CAP_SYS_RESOURCE))
  402. return -EPERM;
  403. err = kstrtoul(buf, 10, &knob);
  404. if (err)
  405. return err;
  406. if (knob > 1)
  407. return -EINVAL;
  408. if (knob)
  409. set_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
  410. else
  411. clear_bit(BTRFS_FS_QUOTA_OVERRIDE, &fs_info->flags);
  412. return len;
  413. }
  414. BTRFS_ATTR_RW(, quota_override, quota_override_show, quota_override_store);
  415. static const struct attribute *btrfs_attrs[] = {
  416. BTRFS_ATTR_PTR(, label),
  417. BTRFS_ATTR_PTR(, nodesize),
  418. BTRFS_ATTR_PTR(, sectorsize),
  419. BTRFS_ATTR_PTR(, clone_alignment),
  420. BTRFS_ATTR_PTR(, quota_override),
  421. NULL,
  422. };
  423. static void btrfs_release_fsid_kobj(struct kobject *kobj)
  424. {
  425. struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
  426. memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
  427. complete(&fs_devs->kobj_unregister);
  428. }
  429. static struct kobj_type btrfs_ktype = {
  430. .sysfs_ops = &kobj_sysfs_ops,
  431. .release = btrfs_release_fsid_kobj,
  432. };
  433. static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
  434. {
  435. if (kobj->ktype != &btrfs_ktype)
  436. return NULL;
  437. return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
  438. }
  439. static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
  440. {
  441. if (kobj->ktype != &btrfs_ktype)
  442. return NULL;
  443. return to_fs_devs(kobj)->fs_info;
  444. }
  445. #define NUM_FEATURE_BITS 64
  446. static char btrfs_unknown_feature_names[3][NUM_FEATURE_BITS][13];
  447. static struct btrfs_feature_attr btrfs_feature_attrs[3][NUM_FEATURE_BITS];
  448. static const u64 supported_feature_masks[3] = {
  449. [FEAT_COMPAT] = BTRFS_FEATURE_COMPAT_SUPP,
  450. [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
  451. [FEAT_INCOMPAT] = BTRFS_FEATURE_INCOMPAT_SUPP,
  452. };
  453. static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
  454. {
  455. int set;
  456. for (set = 0; set < FEAT_MAX; set++) {
  457. int i;
  458. struct attribute *attrs[2];
  459. struct attribute_group agroup = {
  460. .name = "features",
  461. .attrs = attrs,
  462. };
  463. u64 features = get_features(fs_info, set);
  464. features &= ~supported_feature_masks[set];
  465. if (!features)
  466. continue;
  467. attrs[1] = NULL;
  468. for (i = 0; i < NUM_FEATURE_BITS; i++) {
  469. struct btrfs_feature_attr *fa;
  470. if (!(features & (1ULL << i)))
  471. continue;
  472. fa = &btrfs_feature_attrs[set][i];
  473. attrs[0] = &fa->kobj_attr.attr;
  474. if (add) {
  475. int ret;
  476. ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
  477. &agroup);
  478. if (ret)
  479. return ret;
  480. } else
  481. sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
  482. &agroup);
  483. }
  484. }
  485. return 0;
  486. }
  487. static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
  488. {
  489. if (fs_devs->device_dir_kobj) {
  490. kobject_del(fs_devs->device_dir_kobj);
  491. kobject_put(fs_devs->device_dir_kobj);
  492. fs_devs->device_dir_kobj = NULL;
  493. }
  494. if (fs_devs->fsid_kobj.state_initialized) {
  495. kobject_del(&fs_devs->fsid_kobj);
  496. kobject_put(&fs_devs->fsid_kobj);
  497. wait_for_completion(&fs_devs->kobj_unregister);
  498. }
  499. }
  500. /* when fs_devs is NULL it will remove all fsid kobject */
  501. void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
  502. {
  503. struct list_head *fs_uuids = btrfs_get_fs_uuids();
  504. if (fs_devs) {
  505. __btrfs_sysfs_remove_fsid(fs_devs);
  506. return;
  507. }
  508. list_for_each_entry(fs_devs, fs_uuids, list) {
  509. __btrfs_sysfs_remove_fsid(fs_devs);
  510. }
  511. }
  512. void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
  513. {
  514. btrfs_reset_fs_info_ptr(fs_info);
  515. if (fs_info->space_info_kobj) {
  516. sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
  517. kobject_del(fs_info->space_info_kobj);
  518. kobject_put(fs_info->space_info_kobj);
  519. }
  520. addrm_unknown_feature_attrs(fs_info, false);
  521. sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
  522. sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
  523. btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
  524. }
  525. const char * const btrfs_feature_set_names[3] = {
  526. [FEAT_COMPAT] = "compat",
  527. [FEAT_COMPAT_RO] = "compat_ro",
  528. [FEAT_INCOMPAT] = "incompat",
  529. };
  530. char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags)
  531. {
  532. size_t bufsize = 4096; /* safe max, 64 names * 64 bytes */
  533. int len = 0;
  534. int i;
  535. char *str;
  536. str = kmalloc(bufsize, GFP_KERNEL);
  537. if (!str)
  538. return str;
  539. for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
  540. const char *name;
  541. if (!(flags & (1ULL << i)))
  542. continue;
  543. name = btrfs_feature_attrs[set][i].kobj_attr.attr.name;
  544. len += snprintf(str + len, bufsize - len, "%s%s",
  545. len ? "," : "", name);
  546. }
  547. return str;
  548. }
  549. static void init_feature_attrs(void)
  550. {
  551. struct btrfs_feature_attr *fa;
  552. int set, i;
  553. BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
  554. ARRAY_SIZE(btrfs_feature_attrs));
  555. BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
  556. ARRAY_SIZE(btrfs_feature_attrs[0]));
  557. memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
  558. memset(btrfs_unknown_feature_names, 0,
  559. sizeof(btrfs_unknown_feature_names));
  560. for (i = 0; btrfs_supported_feature_attrs[i]; i++) {
  561. struct btrfs_feature_attr *sfa;
  562. struct attribute *a = btrfs_supported_feature_attrs[i];
  563. int bit;
  564. sfa = attr_to_btrfs_feature_attr(a);
  565. bit = ilog2(sfa->feature_bit);
  566. fa = &btrfs_feature_attrs[sfa->feature_set][bit];
  567. fa->kobj_attr.attr.name = sfa->kobj_attr.attr.name;
  568. }
  569. for (set = 0; set < FEAT_MAX; set++) {
  570. for (i = 0; i < ARRAY_SIZE(btrfs_feature_attrs[set]); i++) {
  571. char *name = btrfs_unknown_feature_names[set][i];
  572. fa = &btrfs_feature_attrs[set][i];
  573. if (fa->kobj_attr.attr.name)
  574. continue;
  575. snprintf(name, 13, "%s:%u",
  576. btrfs_feature_set_names[set], i);
  577. fa->kobj_attr.attr.name = name;
  578. fa->kobj_attr.attr.mode = S_IRUGO;
  579. fa->feature_set = set;
  580. fa->feature_bit = 1ULL << i;
  581. }
  582. }
  583. }
  584. /* when one_device is NULL, it removes all device links */
  585. int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
  586. struct btrfs_device *one_device)
  587. {
  588. struct hd_struct *disk;
  589. struct kobject *disk_kobj;
  590. if (!fs_devices->device_dir_kobj)
  591. return -EINVAL;
  592. if (one_device && one_device->bdev) {
  593. disk = one_device->bdev->bd_part;
  594. disk_kobj = &part_to_dev(disk)->kobj;
  595. sysfs_remove_link(fs_devices->device_dir_kobj,
  596. disk_kobj->name);
  597. }
  598. if (one_device)
  599. return 0;
  600. list_for_each_entry(one_device,
  601. &fs_devices->devices, dev_list) {
  602. if (!one_device->bdev)
  603. continue;
  604. disk = one_device->bdev->bd_part;
  605. disk_kobj = &part_to_dev(disk)->kobj;
  606. sysfs_remove_link(fs_devices->device_dir_kobj,
  607. disk_kobj->name);
  608. }
  609. return 0;
  610. }
  611. int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
  612. {
  613. if (!fs_devs->device_dir_kobj)
  614. fs_devs->device_dir_kobj = kobject_create_and_add("devices",
  615. &fs_devs->fsid_kobj);
  616. if (!fs_devs->device_dir_kobj)
  617. return -ENOMEM;
  618. return 0;
  619. }
  620. int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
  621. struct btrfs_device *one_device)
  622. {
  623. int error = 0;
  624. struct btrfs_device *dev;
  625. list_for_each_entry(dev, &fs_devices->devices, dev_list) {
  626. struct hd_struct *disk;
  627. struct kobject *disk_kobj;
  628. if (!dev->bdev)
  629. continue;
  630. if (one_device && one_device != dev)
  631. continue;
  632. disk = dev->bdev->bd_part;
  633. disk_kobj = &part_to_dev(disk)->kobj;
  634. error = sysfs_create_link(fs_devices->device_dir_kobj,
  635. disk_kobj, disk_kobj->name);
  636. if (error)
  637. break;
  638. }
  639. return error;
  640. }
  641. /* /sys/fs/btrfs/ entry */
  642. static struct kset *btrfs_kset;
  643. /* /sys/kernel/debug/btrfs */
  644. static struct dentry *btrfs_debugfs_root_dentry;
  645. /* Debugging tunables and exported data */
  646. u64 btrfs_debugfs_test;
  647. /*
  648. * Can be called by the device discovery thread.
  649. * And parent can be specified for seed device
  650. */
  651. int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
  652. struct kobject *parent)
  653. {
  654. int error;
  655. init_completion(&fs_devs->kobj_unregister);
  656. fs_devs->fsid_kobj.kset = btrfs_kset;
  657. error = kobject_init_and_add(&fs_devs->fsid_kobj,
  658. &btrfs_ktype, parent, "%pU", fs_devs->fsid);
  659. return error;
  660. }
  661. int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
  662. {
  663. int error;
  664. struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
  665. struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
  666. btrfs_set_fs_info_ptr(fs_info);
  667. error = btrfs_sysfs_add_device_link(fs_devs, NULL);
  668. if (error)
  669. return error;
  670. error = sysfs_create_files(fsid_kobj, btrfs_attrs);
  671. if (error) {
  672. btrfs_sysfs_rm_device_link(fs_devs, NULL);
  673. return error;
  674. }
  675. error = sysfs_create_group(fsid_kobj,
  676. &btrfs_feature_attr_group);
  677. if (error)
  678. goto failure;
  679. error = addrm_unknown_feature_attrs(fs_info, true);
  680. if (error)
  681. goto failure;
  682. fs_info->space_info_kobj = kobject_create_and_add("allocation",
  683. fsid_kobj);
  684. if (!fs_info->space_info_kobj) {
  685. error = -ENOMEM;
  686. goto failure;
  687. }
  688. error = sysfs_create_files(fs_info->space_info_kobj, allocation_attrs);
  689. if (error)
  690. goto failure;
  691. return 0;
  692. failure:
  693. btrfs_sysfs_remove_mounted(fs_info);
  694. return error;
  695. }
  696. /*
  697. * Change per-fs features in /sys/fs/btrfs/UUID/features to match current
  698. * values in superblock. Call after any changes to incompat/compat_ro flags
  699. */
  700. void btrfs_sysfs_feature_update(struct btrfs_fs_info *fs_info,
  701. u64 bit, enum btrfs_feature_set set)
  702. {
  703. struct btrfs_fs_devices *fs_devs;
  704. struct kobject *fsid_kobj;
  705. u64 features;
  706. int ret;
  707. if (!fs_info)
  708. return;
  709. features = get_features(fs_info, set);
  710. ASSERT(bit & supported_feature_masks[set]);
  711. fs_devs = fs_info->fs_devices;
  712. fsid_kobj = &fs_devs->fsid_kobj;
  713. if (!fsid_kobj->state_initialized)
  714. return;
  715. /*
  716. * FIXME: this is too heavy to update just one value, ideally we'd like
  717. * to use sysfs_update_group but some refactoring is needed first.
  718. */
  719. sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
  720. ret = sysfs_create_group(fsid_kobj, &btrfs_feature_attr_group);
  721. }
  722. static int btrfs_init_debugfs(void)
  723. {
  724. #ifdef CONFIG_DEBUG_FS
  725. btrfs_debugfs_root_dentry = debugfs_create_dir("btrfs", NULL);
  726. if (!btrfs_debugfs_root_dentry)
  727. return -ENOMEM;
  728. /*
  729. * Example code, how to export data through debugfs.
  730. *
  731. * file: /sys/kernel/debug/btrfs/test
  732. * contents of: btrfs_debugfs_test
  733. */
  734. #ifdef CONFIG_BTRFS_DEBUG
  735. debugfs_create_u64("test", S_IRUGO | S_IWUSR, btrfs_debugfs_root_dentry,
  736. &btrfs_debugfs_test);
  737. #endif
  738. #endif
  739. return 0;
  740. }
  741. int btrfs_init_sysfs(void)
  742. {
  743. int ret;
  744. btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
  745. if (!btrfs_kset)
  746. return -ENOMEM;
  747. ret = btrfs_init_debugfs();
  748. if (ret)
  749. goto out1;
  750. init_feature_attrs();
  751. ret = sysfs_create_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
  752. if (ret)
  753. goto out2;
  754. return 0;
  755. out2:
  756. debugfs_remove_recursive(btrfs_debugfs_root_dentry);
  757. out1:
  758. kset_unregister(btrfs_kset);
  759. return ret;
  760. }
  761. void btrfs_exit_sysfs(void)
  762. {
  763. sysfs_remove_group(&btrfs_kset->kobj, &btrfs_feature_attr_group);
  764. kset_unregister(btrfs_kset);
  765. debugfs_remove_recursive(btrfs_debugfs_root_dentry);
  766. }