kernfs-internal.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * fs/kernfs/kernfs-internal.h - kernfs internal header file
  4. *
  5. * Copyright (c) 2001-3 Patrick Mochel
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de>
  8. */
  9. #ifndef __KERNFS_INTERNAL_H
  10. #define __KERNFS_INTERNAL_H
  11. #include <linux/lockdep.h>
  12. #include <linux/fs.h>
  13. #include <linux/mutex.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/xattr.h>
  16. #include <linux/kernfs.h>
  17. #include <linux/fs_context.h>
  18. struct kernfs_iattrs {
  19. kuid_t ia_uid;
  20. kgid_t ia_gid;
  21. struct timespec64 ia_atime;
  22. struct timespec64 ia_mtime;
  23. struct timespec64 ia_ctime;
  24. struct simple_xattrs xattrs;
  25. atomic_t nr_user_xattrs;
  26. atomic_t user_xattr_size;
  27. };
  28. struct kernfs_root {
  29. /* published fields */
  30. struct kernfs_node *kn;
  31. unsigned int flags; /* KERNFS_ROOT_* flags */
  32. /* private fields, do not use outside kernfs proper */
  33. struct idr ino_idr;
  34. spinlock_t kernfs_idr_lock; /* root->ino_idr */
  35. u32 last_id_lowbits;
  36. u32 id_highbits;
  37. struct kernfs_syscall_ops *syscall_ops;
  38. /* list of kernfs_super_info of this root, protected by kernfs_rwsem */
  39. struct list_head supers;
  40. wait_queue_head_t deactivate_waitq;
  41. struct rw_semaphore kernfs_rwsem;
  42. struct rw_semaphore kernfs_iattr_rwsem;
  43. struct rw_semaphore kernfs_supers_rwsem;
  44. /* kn->parent and kn->name */
  45. rwlock_t kernfs_rename_lock;
  46. struct rcu_head rcu;
  47. };
  48. /* +1 to avoid triggering overflow warning when negating it */
  49. #define KN_DEACTIVATED_BIAS (INT_MIN + 1)
  50. /* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
  51. /**
  52. * kernfs_root - find out the kernfs_root a kernfs_node belongs to
  53. * @kn: kernfs_node of interest
  54. *
  55. * Return: the kernfs_root @kn belongs to.
  56. */
  57. static inline struct kernfs_root *kernfs_root(const struct kernfs_node *kn)
  58. {
  59. const struct kernfs_node *knp;
  60. /* if parent exists, it's always a dir; otherwise, @sd is a dir */
  61. guard(rcu)();
  62. knp = rcu_dereference(kn->__parent);
  63. if (knp)
  64. kn = knp;
  65. return kn->dir.root;
  66. }
  67. /*
  68. * mount.c
  69. */
  70. struct kernfs_super_info {
  71. struct super_block *sb;
  72. /*
  73. * The root associated with this super_block. Each super_block is
  74. * identified by the root and ns it's associated with.
  75. */
  76. struct kernfs_root *root;
  77. /*
  78. * Each sb is associated with one namespace tag, currently the
  79. * network namespace of the task which mounted this kernfs
  80. * instance. If multiple tags become necessary, make the following
  81. * an array and compare kernfs_node tag against every entry.
  82. */
  83. const struct ns_common *ns;
  84. /* anchored at kernfs_root->supers, protected by kernfs_rwsem */
  85. struct list_head node;
  86. };
  87. #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
  88. static inline bool kernfs_root_is_locked(const struct kernfs_node *kn)
  89. {
  90. return lockdep_is_held(&kernfs_root(kn)->kernfs_rwsem);
  91. }
  92. static inline bool kernfs_rename_is_locked(const struct kernfs_node *kn)
  93. {
  94. return lockdep_is_held(&kernfs_root(kn)->kernfs_rename_lock);
  95. }
  96. static inline const char *kernfs_rcu_name(const struct kernfs_node *kn)
  97. {
  98. return rcu_dereference_check(kn->name, kernfs_root_is_locked(kn));
  99. }
  100. static inline struct kernfs_node *kernfs_parent(const struct kernfs_node *kn)
  101. {
  102. /*
  103. * The kernfs_node::__parent remains valid within a RCU section. The kn
  104. * can be reparented (and renamed) which changes the entry. This can be
  105. * avoided by locking kernfs_root::kernfs_rwsem or
  106. * kernfs_root::kernfs_rename_lock.
  107. * Both locks can be used to obtain a reference on __parent. Once the
  108. * reference count reaches 0 then the node is about to be freed
  109. * and can not be renamed (or become a different parent) anymore.
  110. */
  111. return rcu_dereference_check(kn->__parent,
  112. kernfs_root_is_locked(kn) ||
  113. kernfs_rename_is_locked(kn) ||
  114. !atomic_read(&kn->count));
  115. }
  116. static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry)
  117. {
  118. if (d_really_is_negative(dentry))
  119. return NULL;
  120. return d_inode(dentry)->i_private;
  121. }
  122. static inline void kernfs_set_rev(struct kernfs_node *parent,
  123. struct dentry *dentry)
  124. {
  125. dentry->d_time = parent->dir.rev;
  126. }
  127. static inline void kernfs_inc_rev(struct kernfs_node *parent)
  128. {
  129. parent->dir.rev++;
  130. }
  131. static inline bool kernfs_dir_changed(struct kernfs_node *parent,
  132. struct dentry *dentry)
  133. {
  134. if (parent->dir.rev != dentry->d_time)
  135. return true;
  136. return false;
  137. }
  138. extern const struct super_operations kernfs_sops;
  139. extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
  140. /*
  141. * inode.c
  142. */
  143. extern const struct xattr_handler * const kernfs_xattr_handlers[];
  144. void kernfs_evict_inode(struct inode *inode);
  145. int kernfs_iop_permission(struct mnt_idmap *idmap,
  146. struct inode *inode, int mask);
  147. int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  148. struct iattr *iattr);
  149. int kernfs_iop_getattr(struct mnt_idmap *idmap,
  150. const struct path *path, struct kstat *stat,
  151. u32 request_mask, unsigned int query_flags);
  152. ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size);
  153. int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
  154. /*
  155. * dir.c
  156. */
  157. extern const struct dentry_operations kernfs_dops;
  158. extern const struct file_operations kernfs_dir_fops;
  159. extern const struct inode_operations kernfs_dir_iops;
  160. struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
  161. void kernfs_put_active(struct kernfs_node *kn);
  162. int kernfs_add_one(struct kernfs_node *kn);
  163. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  164. const char *name, umode_t mode,
  165. kuid_t uid, kgid_t gid,
  166. unsigned flags);
  167. /*
  168. * file.c
  169. */
  170. extern const struct file_operations kernfs_file_fops;
  171. bool kernfs_should_drain_open_files(struct kernfs_node *kn);
  172. void kernfs_drain_open_files(struct kernfs_node *kn);
  173. /*
  174. * symlink.c
  175. */
  176. extern const struct inode_operations kernfs_symlink_iops;
  177. /*
  178. * kernfs locks
  179. */
  180. extern struct kernfs_global_locks *kernfs_locks;
  181. #endif /* __KERNFS_INTERNAL_H */