sysfs.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/module.h>
  24. #include <linux/kobject.h>
  25. #include "ctree.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
  29. {
  30. return snprintf(buf, PAGE_SIZE, "%llu\n",
  31. (unsigned long long)btrfs_root_used(&root->root_item));
  32. }
  33. static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
  34. {
  35. return snprintf(buf, PAGE_SIZE, "%llu\n",
  36. (unsigned long long)btrfs_root_limit(&root->root_item));
  37. }
  38. static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
  39. {
  40. return snprintf(buf, PAGE_SIZE, "%llu\n",
  41. (unsigned long long)btrfs_super_bytes_used(&fs->super_copy));
  42. }
  43. static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
  44. {
  45. return snprintf(buf, PAGE_SIZE, "%llu\n",
  46. (unsigned long long)btrfs_super_total_bytes(&fs->super_copy));
  47. }
  48. static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
  49. {
  50. return snprintf(buf, PAGE_SIZE, "%llu\n",
  51. (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
  52. }
  53. /* this is for root attrs (subvols/snapshots) */
  54. struct btrfs_root_attr {
  55. struct attribute attr;
  56. ssize_t (*show)(struct btrfs_root *, char *);
  57. ssize_t (*store)(struct btrfs_root *, const char *, size_t);
  58. };
  59. #define ROOT_ATTR(name, mode, show, store) \
  60. static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, \
  61. show, store)
  62. ROOT_ATTR(blocks_used, 0444, root_blocks_used_show, NULL);
  63. ROOT_ATTR(block_limit, 0644, root_block_limit_show, NULL);
  64. static struct attribute *btrfs_root_attrs[] = {
  65. &btrfs_root_attr_blocks_used.attr,
  66. &btrfs_root_attr_block_limit.attr,
  67. NULL,
  68. };
  69. /* this is for super attrs (actual full fs) */
  70. struct btrfs_super_attr {
  71. struct attribute attr;
  72. ssize_t (*show)(struct btrfs_fs_info *, char *);
  73. ssize_t (*store)(struct btrfs_fs_info *, const char *, size_t);
  74. };
  75. #define SUPER_ATTR(name, mode, show, store) \
  76. static struct btrfs_super_attr btrfs_super_attr_##name = __ATTR(name, mode, \
  77. show, store)
  78. SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
  79. SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
  80. SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
  81. static struct attribute *btrfs_super_attrs[] = {
  82. &btrfs_super_attr_blocks_used.attr,
  83. &btrfs_super_attr_total_blocks.attr,
  84. &btrfs_super_attr_blocksize.attr,
  85. NULL,
  86. };
  87. static ssize_t btrfs_super_attr_show(struct kobject *kobj,
  88. struct attribute *attr, char *buf)
  89. {
  90. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  91. super_kobj);
  92. struct btrfs_super_attr *a = container_of(attr,
  93. struct btrfs_super_attr,
  94. attr);
  95. return a->show ? a->show(fs, buf) : 0;
  96. }
  97. static ssize_t btrfs_super_attr_store(struct kobject *kobj,
  98. struct attribute *attr,
  99. const char *buf, size_t len)
  100. {
  101. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  102. super_kobj);
  103. struct btrfs_super_attr *a = container_of(attr,
  104. struct btrfs_super_attr,
  105. attr);
  106. return a->store ? a->store(fs, buf, len) : 0;
  107. }
  108. static ssize_t btrfs_root_attr_show(struct kobject *kobj,
  109. struct attribute *attr, char *buf)
  110. {
  111. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  112. root_kobj);
  113. struct btrfs_root_attr *a = container_of(attr,
  114. struct btrfs_root_attr,
  115. attr);
  116. return a->show ? a->show(root, buf) : 0;
  117. }
  118. static ssize_t btrfs_root_attr_store(struct kobject *kobj,
  119. struct attribute *attr,
  120. const char *buf, size_t len)
  121. {
  122. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  123. root_kobj);
  124. struct btrfs_root_attr *a = container_of(attr,
  125. struct btrfs_root_attr,
  126. attr);
  127. return a->store ? a->store(root, buf, len) : 0;
  128. }
  129. static void btrfs_super_release(struct kobject *kobj)
  130. {
  131. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  132. super_kobj);
  133. complete(&fs->kobj_unregister);
  134. }
  135. static void btrfs_root_release(struct kobject *kobj)
  136. {
  137. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  138. root_kobj);
  139. complete(&root->kobj_unregister);
  140. }
  141. static const struct sysfs_ops btrfs_super_attr_ops = {
  142. .show = btrfs_super_attr_show,
  143. .store = btrfs_super_attr_store,
  144. };
  145. static const struct sysfs_ops btrfs_root_attr_ops = {
  146. .show = btrfs_root_attr_show,
  147. .store = btrfs_root_attr_store,
  148. };
  149. /* /sys/fs/btrfs/ entry */
  150. static struct kset *btrfs_kset;
  151. int btrfs_init_sysfs(void)
  152. {
  153. btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
  154. if (!btrfs_kset)
  155. return -ENOMEM;
  156. return 0;
  157. }
  158. void btrfs_exit_sysfs(void)
  159. {
  160. kset_unregister(btrfs_kset);
  161. }