sysfs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. struct sysfs_elem_dir {
  2. struct kobject * kobj;
  3. };
  4. struct sysfs_elem_symlink {
  5. struct sysfs_dirent * target_sd;
  6. };
  7. struct sysfs_elem_attr {
  8. struct attribute * attr;
  9. };
  10. struct sysfs_elem_bin_attr {
  11. struct bin_attribute * bin_attr;
  12. };
  13. /*
  14. * As long as s_count reference is held, the sysfs_dirent itself is
  15. * accessible. Dereferencing s_elem or any other outer entity
  16. * requires s_active reference.
  17. */
  18. struct sysfs_dirent {
  19. atomic_t s_count;
  20. struct rw_semaphore s_active;
  21. struct sysfs_dirent * s_parent;
  22. struct list_head s_sibling;
  23. struct list_head s_children;
  24. const char * s_name;
  25. union {
  26. struct sysfs_elem_dir dir;
  27. struct sysfs_elem_symlink symlink;
  28. struct sysfs_elem_attr attr;
  29. struct sysfs_elem_bin_attr bin_attr;
  30. } s_elem;
  31. int s_type;
  32. umode_t s_mode;
  33. ino_t s_ino;
  34. struct dentry * s_dentry;
  35. struct iattr * s_iattr;
  36. atomic_t s_event;
  37. };
  38. /*
  39. * A sysfs file which deletes another file when written to need to
  40. * write lock the s_active of the victim while its s_active is read
  41. * locked for the write operation. Tell lockdep that this is okay.
  42. */
  43. enum sysfs_s_active_class
  44. {
  45. SYSFS_S_ACTIVE_NORMAL, /* file r/w access, etc - default */
  46. SYSFS_S_ACTIVE_DEACTIVATE, /* file deactivation */
  47. };
  48. extern struct vfsmount * sysfs_mount;
  49. extern struct kmem_cache *sysfs_dir_cachep;
  50. extern void sysfs_delete_inode(struct inode *inode);
  51. extern struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent *);
  52. extern int sysfs_create(struct dentry *, int mode, int (*init)(struct inode *));
  53. extern void release_sysfs_dirent(struct sysfs_dirent * sd);
  54. extern int sysfs_dirent_exist(struct sysfs_dirent *, const unsigned char *);
  55. extern struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode,
  56. int type);
  57. extern void sysfs_attach_dirent(struct sysfs_dirent *sd,
  58. struct sysfs_dirent *parent_sd,
  59. struct dentry *dentry);
  60. extern int sysfs_add_file(struct dentry *, const struct attribute *, int);
  61. extern int sysfs_hash_and_remove(struct dentry * dir, const char * name);
  62. extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
  63. extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
  64. extern void sysfs_remove_subdir(struct dentry *);
  65. extern void sysfs_drop_dentry(struct sysfs_dirent *sd, struct dentry *parent);
  66. extern int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
  67. extern spinlock_t sysfs_lock;
  68. extern spinlock_t kobj_sysfs_assoc_lock;
  69. extern struct rw_semaphore sysfs_rename_sem;
  70. extern struct super_block * sysfs_sb;
  71. extern const struct file_operations sysfs_dir_operations;
  72. extern const struct file_operations sysfs_file_operations;
  73. extern const struct file_operations bin_fops;
  74. extern const struct inode_operations sysfs_dir_inode_operations;
  75. extern const struct inode_operations sysfs_symlink_inode_operations;
  76. struct sysfs_buffer {
  77. struct list_head associates;
  78. size_t count;
  79. loff_t pos;
  80. char * page;
  81. struct sysfs_ops * ops;
  82. struct semaphore sem;
  83. int orphaned;
  84. int needs_read_fill;
  85. int event;
  86. };
  87. struct sysfs_buffer_collection {
  88. struct list_head associates;
  89. };
  90. static inline struct sysfs_dirent * sysfs_get(struct sysfs_dirent * sd)
  91. {
  92. if (sd) {
  93. WARN_ON(!atomic_read(&sd->s_count));
  94. atomic_inc(&sd->s_count);
  95. }
  96. return sd;
  97. }
  98. static inline void sysfs_put(struct sysfs_dirent * sd)
  99. {
  100. if (sd && atomic_dec_and_test(&sd->s_count))
  101. release_sysfs_dirent(sd);
  102. }
  103. /**
  104. * sysfs_get_active - get an active reference to sysfs_dirent
  105. * @sd: sysfs_dirent to get an active reference to
  106. *
  107. * Get an active reference of @sd. This function is noop if @sd
  108. * is NULL.
  109. *
  110. * RETURNS:
  111. * Pointer to @sd on success, NULL on failure.
  112. */
  113. static inline struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  114. {
  115. if (sd) {
  116. if (unlikely(!down_read_trylock(&sd->s_active)))
  117. sd = NULL;
  118. }
  119. return sd;
  120. }
  121. /**
  122. * sysfs_put_active - put an active reference to sysfs_dirent
  123. * @sd: sysfs_dirent to put an active reference to
  124. *
  125. * Put an active reference to @sd. This function is noop if @sd
  126. * is NULL.
  127. */
  128. static inline void sysfs_put_active(struct sysfs_dirent *sd)
  129. {
  130. if (sd)
  131. up_read(&sd->s_active);
  132. }
  133. /**
  134. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  135. * @sd: sysfs_dirent of interest
  136. *
  137. * Get active reference to @sd and its parent. Parent's active
  138. * reference is grabbed first. This function is noop if @sd is
  139. * NULL.
  140. *
  141. * RETURNS:
  142. * Pointer to @sd on success, NULL on failure.
  143. */
  144. static inline struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  145. {
  146. if (sd) {
  147. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  148. return NULL;
  149. if (unlikely(!sysfs_get_active(sd))) {
  150. sysfs_put_active(sd->s_parent);
  151. return NULL;
  152. }
  153. }
  154. return sd;
  155. }
  156. /**
  157. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  158. * @sd: sysfs_dirent of interest
  159. *
  160. * Put active references to @sd and its parent. This function is
  161. * noop if @sd is NULL.
  162. */
  163. static inline void sysfs_put_active_two(struct sysfs_dirent *sd)
  164. {
  165. if (sd) {
  166. sysfs_put_active(sd);
  167. sysfs_put_active(sd->s_parent);
  168. }
  169. }
  170. /**
  171. * sysfs_deactivate - deactivate sysfs_dirent
  172. * @sd: sysfs_dirent to deactivate
  173. *
  174. * Deny new active references and drain existing ones. s_active
  175. * will be unlocked when the sysfs_dirent is released.
  176. */
  177. static inline void sysfs_deactivate(struct sysfs_dirent *sd)
  178. {
  179. down_write_nested(&sd->s_active, SYSFS_S_ACTIVE_DEACTIVATE);
  180. /* s_active will be unlocked by the thread doing the final put
  181. * on @sd. Lie to lockdep.
  182. */
  183. rwsem_release(&sd->s_active.dep_map, 1, _RET_IP_);
  184. }
  185. static inline int sysfs_is_shadowed_inode(struct inode *inode)
  186. {
  187. return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
  188. }