sys.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2012
  3. * Sachin Bhamare <sbhamare@panasas.com>
  4. * Boaz Harrosh <ooo@electrozaur.com>
  5. *
  6. * This file is part of exofs.
  7. *
  8. * exofs is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * exofs is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with exofs; if not, write to the:
  19. * Free Software Foundation <licensing@fsf.org>
  20. */
  21. #include <linux/kobject.h>
  22. #include <linux/device.h>
  23. #include "exofs.h"
  24. struct odev_attr {
  25. struct attribute attr;
  26. ssize_t (*show)(struct exofs_dev *, char *);
  27. ssize_t (*store)(struct exofs_dev *, const char *, size_t);
  28. };
  29. static ssize_t odev_attr_show(struct kobject *kobj, struct attribute *attr,
  30. char *buf)
  31. {
  32. struct exofs_dev *edp = container_of(kobj, struct exofs_dev, ed_kobj);
  33. struct odev_attr *a = container_of(attr, struct odev_attr, attr);
  34. return a->show ? a->show(edp, buf) : 0;
  35. }
  36. static ssize_t odev_attr_store(struct kobject *kobj, struct attribute *attr,
  37. const char *buf, size_t len)
  38. {
  39. struct exofs_dev *edp = container_of(kobj, struct exofs_dev, ed_kobj);
  40. struct odev_attr *a = container_of(attr, struct odev_attr, attr);
  41. return a->store ? a->store(edp, buf, len) : len;
  42. }
  43. static const struct sysfs_ops odev_attr_ops = {
  44. .show = odev_attr_show,
  45. .store = odev_attr_store,
  46. };
  47. static struct kset *exofs_kset;
  48. static ssize_t osdname_show(struct exofs_dev *edp, char *buf)
  49. {
  50. struct osd_dev *odev = edp->ored.od;
  51. const struct osd_dev_info *odi = osduld_device_info(odev);
  52. return snprintf(buf, odi->osdname_len + 1, "%s", odi->osdname);
  53. }
  54. static ssize_t systemid_show(struct exofs_dev *edp, char *buf)
  55. {
  56. struct osd_dev *odev = edp->ored.od;
  57. const struct osd_dev_info *odi = osduld_device_info(odev);
  58. memcpy(buf, odi->systemid, odi->systemid_len);
  59. return odi->systemid_len;
  60. }
  61. static ssize_t uri_show(struct exofs_dev *edp, char *buf)
  62. {
  63. return snprintf(buf, edp->urilen, "%s", edp->uri);
  64. }
  65. static ssize_t uri_store(struct exofs_dev *edp, const char *buf, size_t len)
  66. {
  67. uint8_t *new_uri;
  68. edp->urilen = strlen(buf) + 1;
  69. new_uri = krealloc(edp->uri, edp->urilen, GFP_KERNEL);
  70. if (new_uri == NULL)
  71. return -ENOMEM;
  72. edp->uri = new_uri;
  73. strncpy(edp->uri, buf, edp->urilen);
  74. return edp->urilen;
  75. }
  76. #define OSD_ATTR(name, mode, show, store) \
  77. static struct odev_attr odev_attr_##name = \
  78. __ATTR(name, mode, show, store)
  79. OSD_ATTR(osdname, S_IRUGO, osdname_show, NULL);
  80. OSD_ATTR(systemid, S_IRUGO, systemid_show, NULL);
  81. OSD_ATTR(uri, S_IRWXU, uri_show, uri_store);
  82. static struct attribute *odev_attrs[] = {
  83. &odev_attr_osdname.attr,
  84. &odev_attr_systemid.attr,
  85. &odev_attr_uri.attr,
  86. NULL,
  87. };
  88. static struct kobj_type odev_ktype = {
  89. .default_attrs = odev_attrs,
  90. .sysfs_ops = &odev_attr_ops,
  91. };
  92. static struct kobj_type uuid_ktype = {
  93. };
  94. void exofs_sysfs_dbg_print(void)
  95. {
  96. #ifdef CONFIG_EXOFS_DEBUG
  97. struct kobject *k_name, *k_tmp;
  98. list_for_each_entry_safe(k_name, k_tmp, &exofs_kset->list, entry) {
  99. printk(KERN_INFO "%s: name %s ref %d\n",
  100. __func__, kobject_name(k_name),
  101. (int)atomic_read(&k_name->kref.refcount));
  102. }
  103. #endif
  104. }
  105. /*
  106. * This function removes all kobjects under exofs_kset
  107. * At the end of it, exofs_kset kobject will have a refcount
  108. * of 1 which gets decremented only on exofs module unload
  109. */
  110. void exofs_sysfs_sb_del(struct exofs_sb_info *sbi)
  111. {
  112. struct kobject *k_name, *k_tmp;
  113. struct kobject *s_kobj = &sbi->s_kobj;
  114. list_for_each_entry_safe(k_name, k_tmp, &exofs_kset->list, entry) {
  115. /* Remove all that are children of this SBI */
  116. if (k_name->parent == s_kobj)
  117. kobject_put(k_name);
  118. }
  119. kobject_put(s_kobj);
  120. }
  121. /*
  122. * This function creates sysfs entries to hold the current exofs cluster
  123. * instance (uniquely identified by osdname,pid tuple).
  124. * This function gets called once per exofs mount instance.
  125. */
  126. int exofs_sysfs_sb_add(struct exofs_sb_info *sbi,
  127. struct exofs_dt_device_info *dt_dev)
  128. {
  129. struct kobject *s_kobj;
  130. int retval = 0;
  131. uint64_t pid = sbi->one_comp.obj.partition;
  132. /* allocate new uuid dirent */
  133. s_kobj = &sbi->s_kobj;
  134. s_kobj->kset = exofs_kset;
  135. retval = kobject_init_and_add(s_kobj, &uuid_ktype,
  136. &exofs_kset->kobj, "%s_%llx", dt_dev->osdname, pid);
  137. if (retval) {
  138. EXOFS_ERR("ERROR: Failed to create sysfs entry for "
  139. "uuid-%s_%llx => %d\n", dt_dev->osdname, pid, retval);
  140. return -ENOMEM;
  141. }
  142. return 0;
  143. }
  144. int exofs_sysfs_odev_add(struct exofs_dev *edev, struct exofs_sb_info *sbi)
  145. {
  146. struct kobject *d_kobj;
  147. int retval = 0;
  148. /* create osd device group which contains following attributes
  149. * osdname, systemid & uri
  150. */
  151. d_kobj = &edev->ed_kobj;
  152. d_kobj->kset = exofs_kset;
  153. retval = kobject_init_and_add(d_kobj, &odev_ktype,
  154. &sbi->s_kobj, "dev%u", edev->did);
  155. if (retval) {
  156. EXOFS_ERR("ERROR: Failed to create sysfs entry for "
  157. "device dev%u\n", edev->did);
  158. return retval;
  159. }
  160. return 0;
  161. }
  162. int exofs_sysfs_init(void)
  163. {
  164. exofs_kset = kset_create_and_add("exofs", NULL, fs_kobj);
  165. if (!exofs_kset) {
  166. EXOFS_ERR("ERROR: kset_create_and_add exofs failed\n");
  167. return -ENOMEM;
  168. }
  169. return 0;
  170. }
  171. void exofs_sysfs_uninit(void)
  172. {
  173. kset_unregister(exofs_kset);
  174. }