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