sysfs.h 6.3 KB

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