1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 1998-2022 Erez Zadok
4 * Copyright (c) 2009 Shrikar Archak
5 * Copyright (c) 2003-2022 Stony Brook University
6 * Copyright (c) 2003-2022 The Research Foundation of SUNY
7 * Copyright (c) 2023 Huawei Device Co., Ltd.
8 */
9
10 #ifndef _SHAREFS_H_
11 #define _SHAREFS_H_
12
13 #include <linux/dcache.h>
14 #include <linux/file.h>
15 #include <linux/fs.h>
16 #include <linux/mm.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/seq_file.h>
20 #include <linux/statfs.h>
21 #include <linux/fs_stack.h>
22 #include <linux/magic.h>
23 #include <linux/uaccess.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/xattr.h>
27 #include <linux/exportfs.h>
28 #include <linux/pagemap.h>
29
30 /* the file system name */
31 #define SHAREFS_NAME "sharefs"
32
33 /* sharefs root inode number */
34 #define SHAREFS_ROOT_INO 1
35 #define OID_ROOT 0
36 #define ROOT_UID KUIDT_INIT(OID_ROOT)
37 #define ROOT_GID KGIDT_INIT(OID_ROOT)
38 #define SHAREFS_SUPER_MAGIC 0x20230212
39
40 /* useful for tracking code reachability */
41 #define UDBG printk(KERN_DEFAULT "DBG:%s:%s:%d\n", __FILE__, __func__, __LINE__)
42
43 /* file private data */
44 struct sharefs_file_info {
45 struct file *lower_file;
46 const struct vm_operations_struct *lower_vm_ops;
47 };
48
49 /* sharefs inode data in memory */
50 struct sharefs_inode_info {
51 struct inode *lower_inode;
52 struct inode vfs_inode;
53 __u16 perm;
54 };
55
56 /* sharefs dentry data in memory */
57 struct sharefs_dentry_info {
58 spinlock_t lock; /* protects lower_path */
59 struct path lower_path;
60 };
61
62 /* sharefs super-block data in memory */
63 struct sharefs_sb_info {
64 struct super_block *lower_sb;
65 /* multi user */
66 unsigned int user_id;
67 };
68
69 /* operations vectors defined in specific files */
70 extern const struct file_operations sharefs_main_fops;
71 extern const struct file_operations sharefs_dir_fops;
72 extern const struct inode_operations sharefs_main_iops;
73 extern const struct inode_operations sharefs_dir_iops;
74 extern const struct inode_operations sharefs_symlink_iops;
75 extern const struct super_operations sharefs_sops;
76 extern const struct dentry_operations sharefs_dops;
77
78 extern int sharefs_init_inode_cache(void);
79 extern void sharefs_destroy_inode_cache(void);
80 extern int sharefs_init_dentry_cache(void);
81 extern void sharefs_destroy_dentry_cache(void);
82 extern int new_dentry_private_data(struct dentry *dentry);
83 extern void free_dentry_private_data(struct dentry *dentry);
84 extern struct dentry *sharefs_lookup(struct inode *dir, struct dentry *dentry,
85 unsigned int flags);
86 extern struct inode *sharefs_iget(struct super_block *sb,
87 struct inode *lower_inode);
88 extern int sharefs_interpose(struct dentry *dentry, struct super_block *sb,
89 struct path *lower_path);
90 extern int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
91 const char *name, unsigned int flags,
92 struct path *path);
93 extern int sharefs_parse_options(struct sharefs_sb_info *sbi,
94 const char *data);
95
96 /*
97 * inode to private data
98 *
99 * Since we use containers and the struct inode is _inside_ the
100 * sharefs_inode_info structure, SHAREFS_I will always (given a non-NULL
101 * inode pointer), return a valid non-NULL pointer.
102 */
SHAREFS_I(const struct inode * inode)103 static inline struct sharefs_inode_info *SHAREFS_I(const struct inode *inode)
104 {
105 return container_of(inode, struct sharefs_inode_info, vfs_inode);
106 }
107
108 /* dentry to private data */
109 #define SHAREFS_D(dent) ((struct sharefs_dentry_info *)(dent)->d_fsdata)
110
111 /* superblock to private data */
112 #define SHAREFS_SB(super) ((struct sharefs_sb_info *)(super)->s_fs_info)
113
114 /* file to private Data */
115 #define SHAREFS_F(file) ((struct sharefs_file_info *)((file)->private_data))
116
117 /* file to lower file */
sharefs_lower_file(const struct file * f)118 static inline struct file *sharefs_lower_file(const struct file *f)
119 {
120 return SHAREFS_F(f)->lower_file;
121 }
122
sharefs_set_lower_file(struct file * f,struct file * val)123 static inline void sharefs_set_lower_file(struct file *f, struct file *val)
124 {
125 SHAREFS_F(f)->lower_file = val;
126 }
127
128 /* inode to lower inode. */
sharefs_lower_inode(const struct inode * i)129 static inline struct inode *sharefs_lower_inode(const struct inode *i)
130 {
131 return SHAREFS_I(i)->lower_inode;
132 }
133
sharefs_set_lower_inode(struct inode * i,struct inode * val)134 static inline void sharefs_set_lower_inode(struct inode *i, struct inode *val)
135 {
136 SHAREFS_I(i)->lower_inode = val;
137 }
138
139 /* superblock to lower superblock */
sharefs_lower_super(const struct super_block * sb)140 static inline struct super_block *sharefs_lower_super(
141 const struct super_block *sb)
142 {
143 return SHAREFS_SB(sb)->lower_sb;
144 }
145
sharefs_set_lower_super(struct super_block * sb,struct super_block * val)146 static inline void sharefs_set_lower_super(struct super_block *sb,
147 struct super_block *val)
148 {
149 SHAREFS_SB(sb)->lower_sb = val;
150 }
151
152 /* path based (dentry/mnt) macros */
pathcpy(struct path * dst,const struct path * src)153 static inline void pathcpy(struct path *dst, const struct path *src)
154 {
155 dst->dentry = src->dentry;
156 dst->mnt = src->mnt;
157 }
158 /* Returns struct path. Caller must path_put it. */
sharefs_get_lower_path(const struct dentry * dent,struct path * lower_path)159 static inline void sharefs_get_lower_path(const struct dentry *dent,
160 struct path *lower_path)
161 {
162 spin_lock(&SHAREFS_D(dent)->lock);
163 pathcpy(lower_path, &SHAREFS_D(dent)->lower_path);
164 path_get(lower_path);
165 spin_unlock(&SHAREFS_D(dent)->lock);
166 return;
167 }
sharefs_put_lower_path(const struct dentry * dent,struct path * lower_path)168 static inline void sharefs_put_lower_path(const struct dentry *dent,
169 struct path *lower_path)
170 {
171 path_put(lower_path);
172 return;
173 }
sharefs_set_lower_path(const struct dentry * dent,struct path * lower_path)174 static inline void sharefs_set_lower_path(const struct dentry *dent,
175 struct path *lower_path)
176 {
177 spin_lock(&SHAREFS_D(dent)->lock);
178 pathcpy(&SHAREFS_D(dent)->lower_path, lower_path);
179 spin_unlock(&SHAREFS_D(dent)->lock);
180 return;
181 }
sharefs_reset_lower_path(const struct dentry * dent)182 static inline void sharefs_reset_lower_path(const struct dentry *dent)
183 {
184 spin_lock(&SHAREFS_D(dent)->lock);
185 SHAREFS_D(dent)->lower_path.dentry = NULL;
186 SHAREFS_D(dent)->lower_path.mnt = NULL;
187 spin_unlock(&SHAREFS_D(dent)->lock);
188 return;
189 }
sharefs_put_reset_lower_path(const struct dentry * dent)190 static inline void sharefs_put_reset_lower_path(const struct dentry *dent)
191 {
192 struct path lower_path;
193 spin_lock(&SHAREFS_D(dent)->lock);
194 pathcpy(&lower_path, &SHAREFS_D(dent)->lower_path);
195 SHAREFS_D(dent)->lower_path.dentry = NULL;
196 SHAREFS_D(dent)->lower_path.mnt = NULL;
197 spin_unlock(&SHAREFS_D(dent)->lock);
198 path_put(&lower_path);
199 return;
200 }
201
202 /* locking helpers */
lock_parent(struct dentry * dentry)203 static inline struct dentry *lock_parent(struct dentry *dentry)
204 {
205 struct dentry *dir = dget_parent(dentry);
206 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
207 return dir;
208 }
209
unlock_dir(struct dentry * dir)210 static inline void unlock_dir(struct dentry *dir)
211 {
212 inode_unlock(d_inode(dir));
213 dput(dir);
214 }
215
str_n_case_eq(const char * s1,const char * s2,size_t len)216 static inline bool str_n_case_eq(const char *s1, const char *s2, size_t len)
217 {
218 return !strncasecmp(s1, s2, len);
219 }
220
qstr_case_eq(const struct qstr * q1,const struct qstr * q2)221 static inline bool qstr_case_eq(const struct qstr *q1, const struct qstr *q2)
222 {
223 return q1->len == q2->len && str_n_case_eq(q1->name, q2->name, q2->len);
224 }
225 /*****************************************************************************
226 * log print helpers
227 *****************************************************************************/
228 __printf(4, 5) void __sharefs_log(const char *level, const bool ratelimited,
229 const char *function, const char *fmt, ...);
230 #define sharefs_err(fmt, ...) \
231 __sharefs_log(KERN_ERR, false, __func__, fmt, ##__VA_ARGS__)
232 #define sharefs_warning(fmt, ...) \
233 __sharefs_log(KERN_WARNING, false, __func__, fmt, ##__VA_ARGS__)
234 #define sharefs_info(fmt, ...) \
235 __sharefs_log(KERN_INFO, false, __func__, fmt, ##__VA_ARGS__)
236 #define sharefs_err_ratelimited(fmt, ...) \
237 __sharefs_log(KERN_ERR, true, __func__, fmt, ##__VA_ARGS__)
238 #define sharefs_warning_ratelimited(fmt, ...) \
239 __sharefs_log(KERN_WARNING, true, __func__, fmt, ##__VA_ARGS__)
240 #define sharefs_info_ratelimited(fmt, ...) \
241 __sharefs_log(KERN_INFO, true, __func__, fmt, ##__VA_ARGS__)
242
243 #endif /* not _SHAREFS_H_ */
244