sysfs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 struct sysfs_ops btrfs_super_attr_ops = {
  142. .show = btrfs_super_attr_show,
  143. .store = btrfs_super_attr_store,
  144. };
  145. static struct sysfs_ops btrfs_root_attr_ops = {
  146. .show = btrfs_root_attr_show,
  147. .store = btrfs_root_attr_store,
  148. };
  149. static struct kobj_type btrfs_root_ktype = {
  150. .default_attrs = btrfs_root_attrs,
  151. .sysfs_ops = &btrfs_root_attr_ops,
  152. .release = btrfs_root_release,
  153. };
  154. static struct kobj_type btrfs_super_ktype = {
  155. .default_attrs = btrfs_super_attrs,
  156. .sysfs_ops = &btrfs_super_attr_ops,
  157. .release = btrfs_super_release,
  158. };
  159. /* /sys/fs/btrfs/ entry */
  160. static struct kset *btrfs_kset;
  161. int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
  162. {
  163. int error;
  164. char *name;
  165. char c;
  166. int len = strlen(fs->sb->s_id) + 1;
  167. int i;
  168. name = kmalloc(len, GFP_NOFS);
  169. if (!name) {
  170. error = -ENOMEM;
  171. goto fail;
  172. }
  173. for (i = 0; i < len; i++) {
  174. c = fs->sb->s_id[i];
  175. if (c == '/' || c == '\\')
  176. c = '!';
  177. name[i] = c;
  178. }
  179. name[len] = '\0';
  180. fs->super_kobj.kset = btrfs_kset;
  181. error = kobject_init_and_add(&fs->super_kobj, &btrfs_super_ktype,
  182. NULL, "%s", name);
  183. kfree(name);
  184. if (error)
  185. goto fail;
  186. return 0;
  187. fail:
  188. printk(KERN_ERR "btrfs: sysfs creation for super failed\n");
  189. return error;
  190. }
  191. int btrfs_sysfs_add_root(struct btrfs_root *root)
  192. {
  193. int error;
  194. error = kobject_init_and_add(&root->root_kobj, &btrfs_root_ktype,
  195. &root->fs_info->super_kobj,
  196. "%s", root->name);
  197. if (error)
  198. goto fail;
  199. return 0;
  200. fail:
  201. printk(KERN_ERR "btrfs: sysfs creation for root failed\n");
  202. return error;
  203. }
  204. void btrfs_sysfs_del_root(struct btrfs_root *root)
  205. {
  206. kobject_put(&root->root_kobj);
  207. wait_for_completion(&root->kobj_unregister);
  208. }
  209. void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
  210. {
  211. kobject_put(&fs->super_kobj);
  212. wait_for_completion(&fs->kobj_unregister);
  213. }
  214. int btrfs_init_sysfs(void)
  215. {
  216. btrfs_kset = kset_create_and_add("btrfs", NULL, fs_kobj);
  217. if (!btrfs_kset)
  218. return -ENOMEM;
  219. return 0;
  220. }
  221. void btrfs_exit_sysfs(void)
  222. {
  223. kset_unregister(btrfs_kset);
  224. }