sysfs.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * fs/sysfs/sysfs.h - sysfs internal header file
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/lockdep.h>
  11. #include <linux/kobject_ns.h>
  12. #include <linux/fs.h>
  13. #include <linux/rbtree.h>
  14. struct sysfs_open_dirent;
  15. /* type-specific structures for sysfs_dirent->s_* union members */
  16. struct sysfs_elem_dir {
  17. struct kobject *kobj;
  18. unsigned long subdirs;
  19. /* children rbtree starts here and goes through sd->s_rb */
  20. struct rb_root children;
  21. };
  22. struct sysfs_elem_symlink {
  23. struct sysfs_dirent *target_sd;
  24. };
  25. struct sysfs_elem_attr {
  26. struct attribute *attr;
  27. struct sysfs_open_dirent *open;
  28. };
  29. struct sysfs_elem_bin_attr {
  30. struct bin_attribute *bin_attr;
  31. struct hlist_head buffers;
  32. };
  33. struct sysfs_inode_attrs {
  34. struct iattr ia_iattr;
  35. void *ia_secdata;
  36. u32 ia_secdata_len;
  37. };
  38. /*
  39. * sysfs_dirent - the building block of sysfs hierarchy. Each and
  40. * every sysfs node is represented by single sysfs_dirent.
  41. *
  42. * As long as s_count reference is held, the sysfs_dirent itself is
  43. * accessible. Dereferencing s_elem or any other outer entity
  44. * requires s_active reference.
  45. */
  46. struct sysfs_dirent {
  47. atomic_t s_count;
  48. atomic_t s_active;
  49. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  50. struct lockdep_map dep_map;
  51. #endif
  52. struct sysfs_dirent *s_parent;
  53. const char *s_name;
  54. struct rb_node s_rb;
  55. union {
  56. struct completion *completion;
  57. struct sysfs_dirent *removed_list;
  58. } u;
  59. const void *s_ns; /* namespace tag */
  60. unsigned int s_hash; /* ns + name hash */
  61. union {
  62. struct sysfs_elem_dir s_dir;
  63. struct sysfs_elem_symlink s_symlink;
  64. struct sysfs_elem_attr s_attr;
  65. struct sysfs_elem_bin_attr s_bin_attr;
  66. };
  67. unsigned short s_flags;
  68. umode_t s_mode;
  69. unsigned int s_ino;
  70. struct sysfs_inode_attrs *s_iattr;
  71. };
  72. #define SD_DEACTIVATED_BIAS INT_MIN
  73. #define SYSFS_TYPE_MASK 0x00ff
  74. #define SYSFS_DIR 0x0001
  75. #define SYSFS_KOBJ_ATTR 0x0002
  76. #define SYSFS_KOBJ_BIN_ATTR 0x0004
  77. #define SYSFS_KOBJ_LINK 0x0008
  78. #define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK)
  79. #define SYSFS_ACTIVE_REF (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR)
  80. #define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK
  81. #define SYSFS_FLAG_HAS_NS 0x01000
  82. #define SYSFS_FLAG_REMOVED 0x02000
  83. static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
  84. {
  85. return sd->s_flags & SYSFS_TYPE_MASK;
  86. }
  87. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  88. #define sysfs_dirent_init_lockdep(sd) \
  89. do { \
  90. struct attribute *attr = sd->s_attr.attr; \
  91. struct lock_class_key *key = attr->key; \
  92. if (!key) \
  93. key = &attr->skey; \
  94. \
  95. lockdep_init_map(&sd->dep_map, "s_active", key, 0); \
  96. } while (0)
  97. /* Test for attributes that want to ignore lockdep for read-locking */
  98. static inline bool sysfs_ignore_lockdep(struct sysfs_dirent *sd)
  99. {
  100. return sysfs_type(sd) == SYSFS_KOBJ_ATTR &&
  101. sd->s_attr.attr->ignore_lockdep;
  102. }
  103. #else
  104. #define sysfs_dirent_init_lockdep(sd) do {} while (0)
  105. static inline bool sysfs_ignore_lockdep(struct sysfs_dirent *sd)
  106. {
  107. return true;
  108. }
  109. #endif
  110. /*
  111. * Context structure to be used while adding/removing nodes.
  112. */
  113. struct sysfs_addrm_cxt {
  114. struct sysfs_dirent *removed;
  115. };
  116. /*
  117. * mount.c
  118. */
  119. /*
  120. * Each sb is associated with one namespace tag, currently the network
  121. * namespace of the task which mounted this sysfs instance. If multiple
  122. * tags become necessary, make the following an array and compare
  123. * sysfs_dirent tag against every entry.
  124. */
  125. struct sysfs_super_info {
  126. void *ns;
  127. };
  128. #define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info))
  129. extern struct sysfs_dirent sysfs_root;
  130. extern struct kmem_cache *sysfs_dir_cachep;
  131. /*
  132. * dir.c
  133. */
  134. extern struct mutex sysfs_mutex;
  135. extern spinlock_t sysfs_assoc_lock;
  136. extern const struct dentry_operations sysfs_dentry_ops;
  137. extern const struct file_operations sysfs_dir_operations;
  138. extern const struct inode_operations sysfs_dir_inode_operations;
  139. struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
  140. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
  141. void sysfs_put_active(struct sysfs_dirent *sd);
  142. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt);
  143. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
  144. struct sysfs_dirent *parent_sd);
  145. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd,
  146. struct sysfs_dirent *parent_sd);
  147. void __sysfs_remove(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
  148. void sysfs_remove(struct sysfs_dirent *sd);
  149. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
  150. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  151. const unsigned char *name,
  152. const void *ns);
  153. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type);
  154. void release_sysfs_dirent(struct sysfs_dirent *sd);
  155. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  156. struct sysfs_dirent **p_sd);
  157. int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd,
  158. const char *new_name, const void *new_ns);
  159. static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd)
  160. {
  161. if (sd) {
  162. WARN_ON(!atomic_read(&sd->s_count));
  163. atomic_inc(&sd->s_count);
  164. }
  165. return sd;
  166. }
  167. #define sysfs_get(sd) __sysfs_get(sd)
  168. static inline void __sysfs_put(struct sysfs_dirent *sd)
  169. {
  170. if (sd && atomic_dec_and_test(&sd->s_count))
  171. release_sysfs_dirent(sd);
  172. }
  173. #define sysfs_put(sd) __sysfs_put(sd)
  174. /*
  175. * inode.c
  176. */
  177. struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd);
  178. void sysfs_evict_inode(struct inode *inode);
  179. int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
  180. int sysfs_permission(struct inode *inode, int mask);
  181. int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
  182. int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  183. struct kstat *stat);
  184. int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  185. size_t size, int flags);
  186. int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name,
  187. const void *ns);
  188. int sysfs_inode_init(void);
  189. /*
  190. * file.c
  191. */
  192. extern const struct file_operations sysfs_file_operations;
  193. extern const struct file_operations sysfs_bin_operations;
  194. int sysfs_add_file(struct sysfs_dirent *dir_sd,
  195. const struct attribute *attr, int type);
  196. int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
  197. const struct attribute *attr, int type,
  198. umode_t amode, const void *ns);
  199. void sysfs_unmap_bin_file(struct sysfs_dirent *sd);
  200. /*
  201. * symlink.c
  202. */
  203. extern const struct inode_operations sysfs_symlink_inode_operations;
  204. int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
  205. const char *name);