sysfs.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /* children list starts here and goes through sd->s_sibling */
  19. struct sysfs_dirent *children;
  20. unsigned long subdirs;
  21. struct rb_root name_tree;
  22. };
  23. struct sysfs_elem_symlink {
  24. struct sysfs_dirent *target_sd;
  25. };
  26. struct sysfs_elem_attr {
  27. struct attribute *attr;
  28. struct sysfs_open_dirent *open;
  29. };
  30. struct sysfs_elem_bin_attr {
  31. struct bin_attribute *bin_attr;
  32. struct hlist_head buffers;
  33. };
  34. struct sysfs_inode_attrs {
  35. struct iattr ia_iattr;
  36. void *ia_secdata;
  37. u32 ia_secdata_len;
  38. };
  39. /*
  40. * sysfs_dirent - the building block of sysfs hierarchy. Each and
  41. * every sysfs node is represented by single sysfs_dirent.
  42. *
  43. * As long as s_count reference is held, the sysfs_dirent itself is
  44. * accessible. Dereferencing s_elem or any other outer entity
  45. * requires s_active reference.
  46. */
  47. struct sysfs_dirent {
  48. atomic_t s_count;
  49. atomic_t s_active;
  50. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  51. struct lockdep_map dep_map;
  52. #endif
  53. struct sysfs_dirent *s_parent;
  54. struct sysfs_dirent *s_sibling;
  55. const char *s_name;
  56. struct rb_node name_node;
  57. const void *s_ns; /* namespace tag */
  58. union {
  59. struct sysfs_elem_dir s_dir;
  60. struct sysfs_elem_symlink s_symlink;
  61. struct sysfs_elem_attr s_attr;
  62. struct sysfs_elem_bin_attr s_bin_attr;
  63. };
  64. unsigned int s_flags;
  65. unsigned short s_mode;
  66. ino_t s_ino;
  67. struct sysfs_inode_attrs *s_iattr;
  68. };
  69. #define SD_DEACTIVATED_BIAS INT_MIN
  70. #define SYSFS_TYPE_MASK 0x00ff
  71. #define SYSFS_DIR 0x0001
  72. #define SYSFS_KOBJ_ATTR 0x0002
  73. #define SYSFS_KOBJ_BIN_ATTR 0x0004
  74. #define SYSFS_KOBJ_LINK 0x0008
  75. #define SYSFS_COPY_NAME (SYSFS_DIR | SYSFS_KOBJ_LINK)
  76. #define SYSFS_ACTIVE_REF (SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR)
  77. /* identify any namespace tag on sysfs_dirents */
  78. #define SYSFS_NS_TYPE_MASK 0xff00
  79. #define SYSFS_NS_TYPE_SHIFT 8
  80. #define SYSFS_FLAG_MASK ~(SYSFS_NS_TYPE_MASK|SYSFS_TYPE_MASK)
  81. #define SYSFS_FLAG_REMOVED 0x020000
  82. static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
  83. {
  84. return sd->s_flags & SYSFS_TYPE_MASK;
  85. }
  86. /*
  87. * Return any namespace tags on this dirent.
  88. * enum kobj_ns_type is defined in linux/kobject.h
  89. */
  90. static inline enum kobj_ns_type sysfs_ns_type(struct sysfs_dirent *sd)
  91. {
  92. return (sd->s_flags & SYSFS_NS_TYPE_MASK) >> SYSFS_NS_TYPE_SHIFT;
  93. }
  94. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  95. #define sysfs_dirent_init_lockdep(sd) \
  96. do { \
  97. struct attribute *attr = sd->s_attr.attr; \
  98. struct lock_class_key *key = attr->key; \
  99. if (!key) \
  100. key = &attr->skey; \
  101. \
  102. lockdep_init_map(&sd->dep_map, "s_active", key, 0); \
  103. } while(0)
  104. #else
  105. #define sysfs_dirent_init_lockdep(sd) do {} while(0)
  106. #endif
  107. /*
  108. * Context structure to be used while adding/removing nodes.
  109. */
  110. struct sysfs_addrm_cxt {
  111. struct sysfs_dirent *parent_sd;
  112. struct sysfs_dirent *removed;
  113. };
  114. /*
  115. * mount.c
  116. */
  117. /*
  118. * Each sb is associated with a set of namespace tags (i.e.
  119. * the network namespace of the task which mounted this sysfs
  120. * instance).
  121. */
  122. struct sysfs_super_info {
  123. void *ns[KOBJ_NS_TYPES];
  124. };
  125. #define sysfs_info(SB) ((struct sysfs_super_info *)(SB->s_fs_info))
  126. extern struct sysfs_dirent sysfs_root;
  127. extern struct kmem_cache *sysfs_dir_cachep;
  128. /*
  129. * dir.c
  130. */
  131. extern struct mutex sysfs_mutex;
  132. extern spinlock_t sysfs_assoc_lock;
  133. extern const struct file_operations sysfs_dir_operations;
  134. extern const struct inode_operations sysfs_dir_inode_operations;
  135. struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
  136. struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
  137. void sysfs_put_active(struct sysfs_dirent *sd);
  138. void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
  139. struct sysfs_dirent *parent_sd);
  140. int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
  141. int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
  142. void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);
  143. void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt);
  144. struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
  145. const void *ns,
  146. const unsigned char *name);
  147. struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
  148. const void *ns,
  149. const unsigned char *name);
  150. struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type);
  151. void release_sysfs_dirent(struct sysfs_dirent *sd);
  152. int sysfs_create_subdir(struct kobject *kobj, const char *name,
  153. struct sysfs_dirent **p_sd);
  154. void sysfs_remove_subdir(struct sysfs_dirent *sd);
  155. int sysfs_rename(struct sysfs_dirent *sd,
  156. struct sysfs_dirent *new_parent_sd, const void *ns, const char *new_name);
  157. static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd)
  158. {
  159. if (sd) {
  160. WARN_ON(!atomic_read(&sd->s_count));
  161. atomic_inc(&sd->s_count);
  162. }
  163. return sd;
  164. }
  165. #define sysfs_get(sd) __sysfs_get(sd)
  166. static inline void __sysfs_put(struct sysfs_dirent *sd)
  167. {
  168. if (sd && atomic_dec_and_test(&sd->s_count))
  169. release_sysfs_dirent(sd);
  170. }
  171. #define sysfs_put(sd) __sysfs_put(sd)
  172. /*
  173. * inode.c
  174. */
  175. struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd);
  176. void sysfs_evict_inode(struct inode *inode);
  177. int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr);
  178. int sysfs_permission(struct inode *inode, int mask);
  179. int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
  180. int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
  181. int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  182. size_t size, int flags);
  183. int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, const char *name);
  184. int sysfs_inode_init(void);
  185. /*
  186. * file.c
  187. */
  188. extern const struct file_operations sysfs_file_operations;
  189. int sysfs_add_file(struct sysfs_dirent *dir_sd,
  190. const struct attribute *attr, int type);
  191. int sysfs_add_file_mode(struct sysfs_dirent *dir_sd,
  192. const struct attribute *attr, int type, mode_t amode);
  193. /*
  194. * bin.c
  195. */
  196. extern const struct file_operations bin_fops;
  197. void unmap_bin_file(struct sysfs_dirent *attr_sd);
  198. /*
  199. * symlink.c
  200. */
  201. extern const struct inode_operations sysfs_symlink_inode_operations;