configfs_macros.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * configfs_macros.h - extends macros for configfs
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. *
  21. * Based on sysfs:
  22. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  23. *
  24. * Based on kobject.h:
  25. * Copyright (c) 2002-2003 Patrick Mochel
  26. * Copyright (c) 2002-2003 Open Source Development Labs
  27. *
  28. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  29. *
  30. * Added CONFIGFS_EATTR() macros from original configfs.h macros
  31. * Copright (C) 2008-2009 Nicholas A. Bellinger <nab@linux-iscsi.org>
  32. *
  33. * Please read Documentation/filesystems/configfs/configfs.txt before using
  34. * the configfs interface, ESPECIALLY the parts about reference counts and
  35. * item destructors.
  36. */
  37. #ifndef _CONFIGFS_MACROS_H_
  38. #define _CONFIGFS_MACROS_H_
  39. #include <linux/configfs.h>
  40. /*
  41. * Users often need to create attribute structures for their configurable
  42. * attributes, containing a configfs_attribute member and function pointers
  43. * for the show() and store() operations on that attribute. If they don't
  44. * need anything else on the extended attribute structure, they can use
  45. * this macro to define it. The argument _name isends up as
  46. * 'struct _name_attribute, as well as names of to CONFIGFS_ATTR_OPS() below.
  47. * The argument _item is the name of the structure containing the
  48. * struct config_item or struct config_group structure members
  49. */
  50. #define CONFIGFS_EATTR_STRUCT(_name, _item) \
  51. struct _name##_attribute { \
  52. struct configfs_attribute attr; \
  53. ssize_t (*show)(struct _item *, char *); \
  54. ssize_t (*store)(struct _item *, const char *, size_t); \
  55. }
  56. /*
  57. * With the extended attribute structure, users can use this macro
  58. * (similar to sysfs' __ATTR) to make defining attributes easier.
  59. * An example:
  60. * #define MYITEM_EATTR(_name, _mode, _show, _store) \
  61. * struct myitem_attribute childless_attr_##_name = \
  62. * __CONFIGFS_EATTR(_name, _mode, _show, _store)
  63. */
  64. #define __CONFIGFS_EATTR(_name, _mode, _show, _store) \
  65. { \
  66. .attr = { \
  67. .ca_name = __stringify(_name), \
  68. .ca_mode = _mode, \
  69. .ca_owner = THIS_MODULE, \
  70. }, \
  71. .show = _show, \
  72. .store = _store, \
  73. }
  74. /* Here is a readonly version, only requiring a show() operation */
  75. #define __CONFIGFS_EATTR_RO(_name, _show) \
  76. { \
  77. .attr = { \
  78. .ca_name = __stringify(_name), \
  79. .ca_mode = 0444, \
  80. .ca_owner = THIS_MODULE, \
  81. }, \
  82. .show = _show, \
  83. }
  84. /*
  85. * With these extended attributes, the simple show_attribute() and
  86. * store_attribute() operations need to call the show() and store() of the
  87. * attributes. This is a common pattern, so we provide a macro to define
  88. * them. The argument _name is the name of the attribute defined by
  89. * CONFIGFS_ATTR_STRUCT(). The argument _item is the name of the structure
  90. * containing the struct config_item or struct config_group structure member.
  91. * The argument _item_member is the actual name of the struct config_* struct
  92. * in your _item structure. Meaning my_structure->some_config_group.
  93. * ^^_item^^^^^ ^^_item_member^^^
  94. * This macro expects the attributes to be named "struct <name>_attribute".
  95. */
  96. #define CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member) \
  97. static struct _item *to_##_name(struct config_item *ci) \
  98. { \
  99. return (ci) ? container_of(to_config_group(ci), struct _item, \
  100. _item_member) : NULL; \
  101. }
  102. #define CONFIGFS_EATTR_OPS_SHOW(_name, _item) \
  103. static ssize_t _name##_attr_show(struct config_item *item, \
  104. struct configfs_attribute *attr, \
  105. char *page) \
  106. { \
  107. struct _item *_item = to_##_name(item); \
  108. struct _name##_attribute * _name##_attr = \
  109. container_of(attr, struct _name##_attribute, attr); \
  110. ssize_t ret = 0; \
  111. \
  112. if (_name##_attr->show) \
  113. ret = _name##_attr->show(_item, page); \
  114. return ret; \
  115. }
  116. #define CONFIGFS_EATTR_OPS_STORE(_name, _item) \
  117. static ssize_t _name##_attr_store(struct config_item *item, \
  118. struct configfs_attribute *attr, \
  119. const char *page, size_t count) \
  120. { \
  121. struct _item *_item = to_##_name(item); \
  122. struct _name##_attribute * _name##_attr = \
  123. container_of(attr, struct _name##_attribute, attr); \
  124. ssize_t ret = -EINVAL; \
  125. \
  126. if (_name##_attr->store) \
  127. ret = _name##_attr->store(_item, page, count); \
  128. return ret; \
  129. }
  130. #define CONFIGFS_EATTR_OPS(_name, _item, _item_member) \
  131. CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \
  132. CONFIGFS_EATTR_OPS_SHOW(_name, _item); \
  133. CONFIGFS_EATTR_OPS_STORE(_name, _item);
  134. #define CONFIGFS_EATTR_OPS_RO(_name, _item, _item_member) \
  135. CONFIGFS_EATTR_OPS_TO_FUNC(_name, _item, _item_member); \
  136. CONFIGFS_EATTR_OPS_SHOW(_name, _item);
  137. #endif /* _CONFIGFS_MACROS_H_ */