sysfs.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 sysfs_dirent *sd, struct dentry *dentry,
  53. int mode, int (*init)(struct inode *));
  54. extern void release_sysfs_dirent(struct sysfs_dirent * sd);
  55. extern int sysfs_dirent_exist(struct sysfs_dirent *, const unsigned char *);
  56. extern struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode,
  57. int type);
  58. extern void sysfs_attach_dirent(struct sysfs_dirent *sd,
  59. struct sysfs_dirent *parent_sd,
  60. struct dentry *dentry);
  61. extern int sysfs_add_file(struct dentry *, const struct attribute *, int);
  62. extern int sysfs_hash_and_remove(struct dentry * dir, const char * name);
  63. extern struct sysfs_dirent *sysfs_find(struct sysfs_dirent *dir, const char * name);
  64. extern int sysfs_create_subdir(struct kobject *, const char *, struct dentry **);
  65. extern void sysfs_remove_subdir(struct dentry *);
  66. extern void sysfs_drop_dentry(struct sysfs_dirent *sd);
  67. extern int sysfs_setattr(struct dentry *dentry, struct iattr *iattr);
  68. extern spinlock_t sysfs_lock;
  69. extern spinlock_t kobj_sysfs_assoc_lock;
  70. extern struct rw_semaphore sysfs_rename_sem;
  71. extern struct super_block * sysfs_sb;
  72. extern const struct file_operations sysfs_dir_operations;
  73. extern const struct file_operations sysfs_file_operations;
  74. extern const struct file_operations bin_fops;
  75. extern const struct inode_operations sysfs_dir_inode_operations;
  76. extern const struct inode_operations sysfs_symlink_inode_operations;
  77. static inline struct sysfs_dirent * sysfs_get(struct sysfs_dirent * sd)
  78. {
  79. if (sd) {
  80. WARN_ON(!atomic_read(&sd->s_count));
  81. atomic_inc(&sd->s_count);
  82. }
  83. return sd;
  84. }
  85. static inline void sysfs_put(struct sysfs_dirent * sd)
  86. {
  87. if (sd && atomic_dec_and_test(&sd->s_count))
  88. release_sysfs_dirent(sd);
  89. }
  90. /**
  91. * sysfs_get_active - get an active reference to sysfs_dirent
  92. * @sd: sysfs_dirent to get an active reference to
  93. *
  94. * Get an active reference of @sd. This function is noop if @sd
  95. * is NULL.
  96. *
  97. * RETURNS:
  98. * Pointer to @sd on success, NULL on failure.
  99. */
  100. static inline struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
  101. {
  102. if (sd) {
  103. if (unlikely(!down_read_trylock(&sd->s_active)))
  104. sd = NULL;
  105. }
  106. return sd;
  107. }
  108. /**
  109. * sysfs_put_active - put an active reference to sysfs_dirent
  110. * @sd: sysfs_dirent to put an active reference to
  111. *
  112. * Put an active reference to @sd. This function is noop if @sd
  113. * is NULL.
  114. */
  115. static inline void sysfs_put_active(struct sysfs_dirent *sd)
  116. {
  117. if (sd)
  118. up_read(&sd->s_active);
  119. }
  120. /**
  121. * sysfs_get_active_two - get active references to sysfs_dirent and parent
  122. * @sd: sysfs_dirent of interest
  123. *
  124. * Get active reference to @sd and its parent. Parent's active
  125. * reference is grabbed first. This function is noop if @sd is
  126. * NULL.
  127. *
  128. * RETURNS:
  129. * Pointer to @sd on success, NULL on failure.
  130. */
  131. static inline struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
  132. {
  133. if (sd) {
  134. if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
  135. return NULL;
  136. if (unlikely(!sysfs_get_active(sd))) {
  137. sysfs_put_active(sd->s_parent);
  138. return NULL;
  139. }
  140. }
  141. return sd;
  142. }
  143. /**
  144. * sysfs_put_active_two - put active references to sysfs_dirent and parent
  145. * @sd: sysfs_dirent of interest
  146. *
  147. * Put active references to @sd and its parent. This function is
  148. * noop if @sd is NULL.
  149. */
  150. static inline void sysfs_put_active_two(struct sysfs_dirent *sd)
  151. {
  152. if (sd) {
  153. sysfs_put_active(sd);
  154. sysfs_put_active(sd->s_parent);
  155. }
  156. }
  157. /**
  158. * sysfs_deactivate - deactivate sysfs_dirent
  159. * @sd: sysfs_dirent to deactivate
  160. *
  161. * Deny new active references and drain existing ones. s_active
  162. * will be unlocked when the sysfs_dirent is released.
  163. */
  164. static inline void sysfs_deactivate(struct sysfs_dirent *sd)
  165. {
  166. down_write_nested(&sd->s_active, SYSFS_S_ACTIVE_DEACTIVATE);
  167. /* s_active will be unlocked by the thread doing the final put
  168. * on @sd. Lie to lockdep.
  169. */
  170. rwsem_release(&sd->s_active.dep_map, 1, _RET_IP_);
  171. }
  172. static inline int sysfs_is_shadowed_inode(struct inode *inode)
  173. {
  174. return S_ISDIR(inode->i_mode) && inode->i_op->follow_link;
  175. }