sys.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2012
  3. * Sachin Bhamare <sbhamare@panasas.com>
  4. * Boaz Harrosh <bharrosh@panasas.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. edp->urilen = strlen(buf) + 1;
  68. edp->uri = krealloc(edp->uri, edp->urilen, GFP_KERNEL);
  69. strncpy(edp->uri, buf, edp->urilen);
  70. return edp->urilen;
  71. }
  72. #define OSD_ATTR(name, mode, show, store) \
  73. static struct odev_attr odev_attr_##name = \
  74. __ATTR(name, mode, show, store)
  75. OSD_ATTR(osdname, S_IRUGO, osdname_show, NULL);
  76. OSD_ATTR(systemid, S_IRUGO, systemid_show, NULL);
  77. OSD_ATTR(uri, S_IRWXU, uri_show, uri_store);
  78. static struct attribute *odev_attrs[] = {
  79. &odev_attr_osdname.attr,
  80. &odev_attr_systemid.attr,
  81. &odev_attr_uri.attr,
  82. NULL,
  83. };
  84. static struct kobj_type odev_ktype = {
  85. .default_attrs = odev_attrs,
  86. .sysfs_ops = &odev_attr_ops,
  87. };
  88. static struct kobj_type uuid_ktype = {
  89. };
  90. void exofs_sysfs_dbg_print(void)
  91. {
  92. #ifdef CONFIG_EXOFS_DEBUG
  93. struct kobject *k_name, *k_tmp;
  94. list_for_each_entry_safe(k_name, k_tmp, &exofs_kset->list, entry) {
  95. printk(KERN_INFO "%s: name %s ref %d\n",
  96. __func__, kobject_name(k_name),
  97. (int)atomic_read(&k_name->kref.refcount));
  98. }
  99. #endif
  100. }
  101. /*
  102. * This function removes all kobjects under exofs_kset
  103. * At the end of it, exofs_kset kobject will have a refcount
  104. * of 1 which gets decremented only on exofs module unload
  105. */
  106. void exofs_sysfs_sb_del(struct exofs_sb_info *sbi)
  107. {
  108. struct kobject *k_name, *k_tmp;
  109. struct kobject *s_kobj = &sbi->s_kobj;
  110. list_for_each_entry_safe(k_name, k_tmp, &exofs_kset->list, entry) {
  111. /* Remove all that are children of this SBI */
  112. if (k_name->parent == s_kobj)
  113. kobject_put(k_name);
  114. }
  115. kobject_put(s_kobj);
  116. }
  117. /*
  118. * This function creates sysfs entries to hold the current exofs cluster
  119. * instance (uniquely identified by osdname,pid tuple).
  120. * This function gets called once per exofs mount instance.
  121. */
  122. int exofs_sysfs_sb_add(struct exofs_sb_info *sbi,
  123. struct exofs_dt_device_info *dt_dev)
  124. {
  125. struct kobject *s_kobj;
  126. int retval = 0;
  127. uint64_t pid = sbi->one_comp.obj.partition;
  128. /* allocate new uuid dirent */
  129. s_kobj = &sbi->s_kobj;
  130. s_kobj->kset = exofs_kset;
  131. retval = kobject_init_and_add(s_kobj, &uuid_ktype,
  132. &exofs_kset->kobj, "%s_%llx", dt_dev->osdname, pid);
  133. if (retval) {
  134. EXOFS_ERR("ERROR: Failed to create sysfs entry for "
  135. "uuid-%s_%llx => %d\n", dt_dev->osdname, pid, retval);
  136. return -ENOMEM;
  137. }
  138. return 0;
  139. }
  140. int exofs_sysfs_odev_add(struct exofs_dev *edev, struct exofs_sb_info *sbi)
  141. {
  142. struct kobject *d_kobj;
  143. int retval = 0;
  144. /* create osd device group which contains following attributes
  145. * osdname, systemid & uri
  146. */
  147. d_kobj = &edev->ed_kobj;
  148. d_kobj->kset = exofs_kset;
  149. retval = kobject_init_and_add(d_kobj, &odev_ktype,
  150. &sbi->s_kobj, "dev%u", edev->did);
  151. if (retval) {
  152. EXOFS_ERR("ERROR: Failed to create sysfs entry for "
  153. "device dev%u\n", edev->did);
  154. return retval;
  155. }
  156. return 0;
  157. }
  158. int exofs_sysfs_init(void)
  159. {
  160. exofs_kset = kset_create_and_add("exofs", NULL, fs_kobj);
  161. if (!exofs_kset) {
  162. EXOFS_ERR("ERROR: kset_create_and_add exofs failed\n");
  163. return -ENOMEM;
  164. }
  165. return 0;
  166. }
  167. void exofs_sysfs_uninit(void)
  168. {
  169. kset_unregister(exofs_kset);
  170. }