sysfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
  29. static ssize_t root_blocks_used_show(struct btrfs_root *root, char *buf)
  30. {
  31. return snprintf(buf, PAGE_SIZE, "%llu\n",
  32. (unsigned long long)btrfs_root_used(&root->root_item));
  33. }
  34. static ssize_t root_block_limit_show(struct btrfs_root *root, char *buf)
  35. {
  36. return snprintf(buf, PAGE_SIZE, "%llu\n",
  37. (unsigned long long)btrfs_root_limit(&root->root_item));
  38. }
  39. static ssize_t super_blocks_used_show(struct btrfs_fs_info *fs, char *buf)
  40. {
  41. return snprintf(buf, PAGE_SIZE, "%llu\n",
  42. (unsigned long long)btrfs_super_bytes_used(&fs->super_copy));
  43. }
  44. static ssize_t super_total_blocks_show(struct btrfs_fs_info *fs, char *buf)
  45. {
  46. return snprintf(buf, PAGE_SIZE, "%llu\n",
  47. (unsigned long long)btrfs_super_total_bytes(&fs->super_copy));
  48. }
  49. static ssize_t super_blocksize_show(struct btrfs_fs_info *fs, char *buf)
  50. {
  51. return snprintf(buf, PAGE_SIZE, "%llu\n",
  52. (unsigned long long)btrfs_super_sectorsize(&fs->super_copy));
  53. }
  54. /* this is for root attrs (subvols/snapshots) */
  55. struct btrfs_root_attr {
  56. struct attribute attr;
  57. ssize_t (*show)(struct btrfs_root *, char *);
  58. ssize_t (*store)(struct btrfs_root *, const char *, size_t);
  59. };
  60. #define ROOT_ATTR(name, mode, show, store) \
  61. static struct btrfs_root_attr btrfs_root_attr_##name = __ATTR(name, mode, 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, show, store)
  77. SUPER_ATTR(blocks_used, 0444, super_blocks_used_show, NULL);
  78. SUPER_ATTR(total_blocks, 0444, super_total_blocks_show, NULL);
  79. SUPER_ATTR(blocksize, 0444, super_blocksize_show, NULL);
  80. static struct attribute *btrfs_super_attrs[] = {
  81. &btrfs_super_attr_blocks_used.attr,
  82. &btrfs_super_attr_total_blocks.attr,
  83. &btrfs_super_attr_blocksize.attr,
  84. NULL,
  85. };
  86. static ssize_t btrfs_super_attr_show(struct kobject *kobj,
  87. struct attribute *attr, char *buf)
  88. {
  89. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  90. super_kobj);
  91. struct btrfs_super_attr *a = container_of(attr,
  92. struct btrfs_super_attr,
  93. attr);
  94. return a->show ? a->show(fs, buf) : 0;
  95. }
  96. static ssize_t btrfs_super_attr_store(struct kobject *kobj,
  97. struct attribute *attr,
  98. const char *buf, size_t len)
  99. {
  100. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  101. super_kobj);
  102. struct btrfs_super_attr *a = container_of(attr,
  103. struct btrfs_super_attr,
  104. attr);
  105. return a->store ? a->store(fs, buf, len) : 0;
  106. }
  107. static ssize_t btrfs_root_attr_show(struct kobject *kobj,
  108. struct attribute *attr, char *buf)
  109. {
  110. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  111. root_kobj);
  112. struct btrfs_root_attr *a = container_of(attr,
  113. struct btrfs_root_attr,
  114. attr);
  115. return a->show ? a->show(root, buf) : 0;
  116. }
  117. static ssize_t btrfs_root_attr_store(struct kobject *kobj,
  118. struct attribute *attr,
  119. const char *buf, size_t len)
  120. {
  121. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  122. root_kobj);
  123. struct btrfs_root_attr *a = container_of(attr,
  124. struct btrfs_root_attr,
  125. attr);
  126. return a->store ? a->store(root, buf, len) : 0;
  127. }
  128. static void btrfs_super_release(struct kobject *kobj)
  129. {
  130. struct btrfs_fs_info *fs = container_of(kobj, struct btrfs_fs_info,
  131. super_kobj);
  132. complete(&fs->kobj_unregister);
  133. }
  134. static void btrfs_root_release(struct kobject *kobj)
  135. {
  136. struct btrfs_root *root = container_of(kobj, struct btrfs_root,
  137. root_kobj);
  138. complete(&root->kobj_unregister);
  139. }
  140. static struct sysfs_ops btrfs_super_attr_ops = {
  141. .show = btrfs_super_attr_show,
  142. .store = btrfs_super_attr_store,
  143. };
  144. static struct sysfs_ops btrfs_root_attr_ops = {
  145. .show = btrfs_root_attr_show,
  146. .store = btrfs_root_attr_store,
  147. };
  148. static struct kobj_type btrfs_root_ktype = {
  149. .default_attrs = btrfs_root_attrs,
  150. .sysfs_ops = &btrfs_root_attr_ops,
  151. .release = btrfs_root_release,
  152. };
  153. static struct kobj_type btrfs_super_ktype = {
  154. .default_attrs = btrfs_super_attrs,
  155. .sysfs_ops = &btrfs_super_attr_ops,
  156. .release = btrfs_super_release,
  157. };
  158. /* /sys/fs/btrfs/ entry */
  159. static struct kset *btrfs_kset;
  160. int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
  161. {
  162. int error;
  163. char *name;
  164. char c;
  165. int len = strlen(fs->sb->s_id) + 1;
  166. int i;
  167. name = kmalloc(len, GFP_NOFS);
  168. if (!name) {
  169. error = -ENOMEM;
  170. goto fail;
  171. }
  172. for (i = 0; i < len; i++) {
  173. c = fs->sb->s_id[i];
  174. if (c == '/' || c == '\\')
  175. c = '!';
  176. name[i] = c;
  177. }
  178. name[len] = '\0';
  179. fs->super_kobj.kset = btrfs_kset;
  180. error = kobject_init_and_add(&fs->super_kobj, &btrfs_super_ktype,
  181. NULL, "%s", name);
  182. if (error)
  183. goto fail;
  184. kfree(name);
  185. return 0;
  186. fail:
  187. kfree(name);
  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()
  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()
  222. {
  223. kset_unregister(btrfs_kset);
  224. }
  225. #else
  226. int btrfs_sysfs_add_super(struct btrfs_fs_info *fs)
  227. {
  228. return 0;
  229. }
  230. int btrfs_sysfs_add_root(struct btrfs_root *root)
  231. {
  232. return 0;
  233. }
  234. void btrfs_sysfs_del_root(struct btrfs_root *root)
  235. {
  236. return;
  237. }
  238. void btrfs_sysfs_del_super(struct btrfs_fs_info *fs)
  239. {
  240. return;
  241. }
  242. int btrfs_init_sysfs()
  243. {
  244. return 0;
  245. }
  246. void btrfs_exit_sysfs()
  247. {
  248. return;
  249. }
  250. #endif