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
10 #ifndef __KERNFS_INTERNAL_H
11 #define __KERNFS_INTERNAL_H
12
13 #include <linux/lockdep.h>
14 #include <linux/fs.h>
15 #include <linux/mutex.h>
16 #include <linux/rwsem.h>
17 #include <linux/xattr.h>
18
19 #include <linux/kernfs.h>
20 #include <linux/fs_context.h>
21
22 struct kernfs_iattrs {
23 kuid_t ia_uid;
24 kgid_t ia_gid;
25 struct timespec64 ia_atime;
26 struct timespec64 ia_mtime;
27 struct timespec64 ia_ctime;
28
29 struct simple_xattrs xattrs;
30 atomic_t nr_user_xattrs;
31 atomic_t user_xattr_size;
32 };
33
34 /* +1 to avoid triggering overflow warning when negating it */
35 #define KN_DEACTIVATED_BIAS (INT_MIN + 1)
36
37 /* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
38
39 /**
40 * kernfs_root - find out the kernfs_root a kernfs_node belongs to
41 * @kn: kernfs_node of interest
42 *
43 * Return the kernfs_root @kn belongs to.
44 */
kernfs_root(struct kernfs_node * kn)45 static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn)
46 {
47 /* if parent exists, it's always a dir; otherwise, @sd is a dir */
48 if (kn->parent)
49 kn = kn->parent;
50 return kn->dir.root;
51 }
52
kernfs_rwsem(struct kernfs_root * root)53 static inline struct rw_semaphore *kernfs_rwsem(struct kernfs_root *root)
54 {
55 struct kernfs_root_ext *root_ext;
56
57 root_ext = container_of(root, struct kernfs_root_ext, root);
58 return &root_ext->kernfs_rwsem;
59 }
60
61 /*
62 * mount.c
63 */
64 struct kernfs_super_info {
65 struct super_block *sb;
66
67 /*
68 * The root associated with this super_block. Each super_block is
69 * identified by the root and ns it's associated with.
70 */
71 struct kernfs_root *root;
72
73 /*
74 * Each sb is associated with one namespace tag, currently the
75 * network namespace of the task which mounted this kernfs
76 * instance. If multiple tags become necessary, make the following
77 * an array and compare kernfs_node tag against every entry.
78 */
79 const void *ns;
80
81 /* anchored at kernfs_root->supers, protected by kernfs_rwsem */
82 struct list_head node;
83 };
84 #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
85
kernfs_dentry_node(struct dentry * dentry)86 static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry)
87 {
88 if (d_really_is_negative(dentry))
89 return NULL;
90 return d_inode(dentry)->i_private;
91 }
92
kernfs_set_rev(struct kernfs_node * parent,struct dentry * dentry)93 static inline void kernfs_set_rev(struct kernfs_node *parent,
94 struct dentry *dentry)
95 {
96 dentry->d_time = parent->dir.rev;
97 }
98
kernfs_inc_rev(struct kernfs_node * parent)99 static inline void kernfs_inc_rev(struct kernfs_node *parent)
100 {
101 parent->dir.rev++;
102 }
103
kernfs_dir_changed(struct kernfs_node * parent,struct dentry * dentry)104 static inline bool kernfs_dir_changed(struct kernfs_node *parent,
105 struct dentry *dentry)
106 {
107 if (parent->dir.rev != dentry->d_time)
108 return true;
109 return false;
110 }
111
112 extern const struct super_operations kernfs_sops;
113 extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
114
115 /*
116 * inode.c
117 */
118 extern const struct xattr_handler *kernfs_xattr_handlers[];
119 void kernfs_evict_inode(struct inode *inode);
120 int kernfs_iop_permission(struct user_namespace *mnt_userns,
121 struct inode *inode, int mask);
122 int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
123 struct iattr *iattr);
124 int kernfs_iop_getattr(struct user_namespace *mnt_userns,
125 const struct path *path, struct kstat *stat,
126 u32 request_mask, unsigned int query_flags);
127 ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size);
128 int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
129
130 /*
131 * dir.c
132 */
133 extern const struct dentry_operations kernfs_dops;
134 extern const struct file_operations kernfs_dir_fops;
135 extern const struct inode_operations kernfs_dir_iops;
136
137 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
138 void kernfs_put_active(struct kernfs_node *kn);
139 int kernfs_add_one(struct kernfs_node *kn);
140 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
141 const char *name, umode_t mode,
142 kuid_t uid, kgid_t gid,
143 unsigned flags);
144
145 /*
146 * file.c
147 */
148 extern const struct file_operations kernfs_file_fops;
149
150 void kernfs_drain_open_files(struct kernfs_node *kn);
151
152 /*
153 * symlink.c
154 */
155 extern const struct inode_operations kernfs_symlink_iops;
156
157 #endif /* __KERNFS_INTERNAL_H */
158