sysfs.h 5.5 KB

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