xref: /linux/include/linux/namei.h (revision 96a377ba007a6a1ee9fa9f0ba48812fe9284f0ce)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_NAMEI_H
3 #define _LINUX_NAMEI_H
4 
5 #include <linux/fs.h>
6 #include <linux/kernel.h>
7 #include <linux/path.h>
8 #include <linux/fcntl.h>
9 #include <linux/errno.h>
10 #include <linux/fs_struct.h>
11 
12 enum { MAX_NESTED_LINKS = 8 };
13 
14 #define MAXSYMLINKS 40
15 
16 /* pathwalk mode */
17 #define LOOKUP_FOLLOW		BIT(0)	/* follow links at the end */
18 #define LOOKUP_DIRECTORY	BIT(1)	/* require a directory */
19 #define LOOKUP_AUTOMOUNT	BIT(2)  /* force terminal automount */
20 #define LOOKUP_EMPTY		BIT(3)	/* accept empty path [user_... only] */
21 #define LOOKUP_LINKAT_EMPTY	BIT(4) /* Linkat request with empty path. */
22 #define LOOKUP_DOWN		BIT(5)	/* follow mounts in the starting point */
23 #define LOOKUP_MOUNTPOINT	BIT(6)	/* follow mounts in the end */
24 #define LOOKUP_REVAL		BIT(7)	/* tell ->d_revalidate() to trust no cache */
25 #define LOOKUP_RCU		BIT(8)	/* RCU pathwalk mode; semi-internal */
26 #define LOOKUP_CACHED		BIT(9) /* Only do cached lookup */
27 #define LOOKUP_PARENT		BIT(10)	/* Looking up final parent in path */
28 /* 5 spare bits for pathwalk */
29 
30 /* These tell filesystem methods that we are dealing with the final component... */
31 #define LOOKUP_OPEN		BIT(16)	/* ... in open */
32 #define LOOKUP_CREATE		BIT(17)	/* ... in object creation */
33 #define LOOKUP_EXCL		BIT(18)	/* ... in target must not exist */
34 #define LOOKUP_RENAME_TARGET	BIT(19)	/* ... in destination of rename() */
35 #define LOOKUP_SHARED		BIT(20) /* Parent lock is held shared */
36 
37 /* 3 spare bits for intent */
38 
39 /* Scoping flags for lookup. */
40 #define LOOKUP_NO_SYMLINKS	BIT(24) /* No symlink crossing. */
41 #define LOOKUP_NO_MAGICLINKS	BIT(25) /* No nd_jump_link() crossing. */
42 #define LOOKUP_NO_XDEV		BIT(26) /* No mountpoint crossing. */
43 #define LOOKUP_BENEATH		BIT(27) /* No escaping from starting point. */
44 #define LOOKUP_IN_ROOT		BIT(28) /* Treat dirfd as fs root. */
45 /* LOOKUP_* flags which do scope-related checks based on the dirfd. */
46 #define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT)
47 /* 3 spare bits for scoping */
48 
49 extern int path_pts(struct path *path);
50 
51 extern int user_path_at(int, const char __user *, unsigned, struct path *);
52 
53 extern int kern_path(const char *, unsigned, struct path *);
54 struct dentry *kern_path_parent(const char *name, struct path *parent);
55 
56 extern struct dentry *start_creating_path(int, const char *, struct path *, unsigned int);
57 extern struct dentry *start_creating_user_path(int, const char __user *, struct path *, unsigned int);
58 extern void end_creating_path(const struct path *, struct dentry *);
59 extern struct dentry *start_removing_path(const char *, struct path *);
end_removing_path(const struct path * path,struct dentry * dentry)60 static inline void end_removing_path(const struct path *path , struct dentry *dentry)
61 {
62 	end_creating_path(path, dentry);
63 }
64 int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
65 			   struct path *parent, struct qstr *last,
66 			   const struct path *root);
67 int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *,
68 		    unsigned int, struct path *);
69 
70 extern struct dentry *try_lookup_noperm(struct qstr *, struct dentry *);
71 extern struct dentry *lookup_noperm(struct qstr *, struct dentry *);
72 extern struct dentry *lookup_noperm_unlocked(struct qstr *, struct dentry *);
73 extern struct dentry *lookup_noperm_positive_unlocked(struct qstr *, struct dentry *);
74 struct dentry *lookup_one(const struct mnt_idmap *, struct qstr *, struct dentry *);
75 struct dentry *lookup_one_unlocked(const struct mnt_idmap *idmap,
76 				   struct qstr *name, struct dentry *base);
77 struct dentry *lookup_one_positive_unlocked(const struct mnt_idmap *idmap,
78 					    struct qstr *name,
79 					    struct dentry *base);
80 struct dentry *lookup_one_positive_killable(const struct mnt_idmap *idmap,
81 					    struct qstr *name,
82 					    struct dentry *base);
83 
84 struct dentry *start_creating(const struct mnt_idmap *idmap, struct dentry *parent,
85 			      struct qstr *name);
86 struct dentry *start_removing(const struct mnt_idmap *idmap, struct dentry *parent,
87 			      struct qstr *name);
88 struct dentry *start_creating_killable(const struct mnt_idmap *idmap,
89 				       struct dentry *parent,
90 				       struct qstr *name);
91 struct dentry *start_removing_killable(const struct mnt_idmap *idmap,
92 				       struct dentry *parent,
93 				       struct qstr *name);
94 struct dentry *start_creating_noperm(struct dentry *parent, struct qstr *name);
95 struct dentry *start_removing_noperm(struct dentry *parent, struct qstr *name);
96 struct dentry *start_creating_dentry(struct dentry *parent,
97 				     struct dentry *child);
98 struct dentry *start_removing_dentry(struct dentry *parent,
99 				     struct dentry *child);
100 
101 struct file *vfs_lookup_open(struct path *parent, struct qstr *last,
102 			     int open_flag, umode_t mode);
103 
104 /* end_creating - finish action started with start_creating
105  * @child: dentry returned by start_creating() or vfs_mkdir()
106  *
107  * Unlock and release the child. This can be called after
108  * start_creating() whether that function succeeded or not,
109  * but it is not needed on failure.
110  *
111  * If vfs_mkdir() was called then the value returned from that function
112  * should be given for @child rather than the original dentry, as vfs_mkdir()
113  * may have provided a new dentry.
114  *
115  *
116  * If vfs_mkdir() was not called, then @child will be a valid dentry and
117  * @parent will be ignored.
118  */
end_creating(struct dentry * child)119 static inline void end_creating(struct dentry *child)
120 {
121 	end_dirop(child);
122 }
123 
124 /* end_creating_keep - finish action started with start_creating() and return result
125  * @child: dentry returned by start_creating() or vfs_mkdir()
126  *
127  * Unlock and return the child. This can be called after
128  * start_creating() whether that function succeeded or not,
129  * but it is not needed on failure.
130  *
131  * If vfs_mkdir() was called then the value returned from that function
132  * should be given for @child rather than the original dentry, as vfs_mkdir()
133  * may have provided a new dentry.
134  *
135  * Returns: @child, which may be a dentry or an error.
136  *
137  */
end_creating_keep(struct dentry * child)138 static inline struct dentry *end_creating_keep(struct dentry *child)
139 {
140 	if (!IS_ERR(child))
141 		dget(child);
142 	end_dirop(child);
143 	return child;
144 }
145 
146 /**
147  * end_removing - finish action started with start_removing
148  * @child:  dentry returned by start_removing()
149  * @parent: dentry given to start_removing()
150  *
151  * Unlock and release the child.
152  *
153  * This is identical to end_dirop().  It can be passed the result of
154  * start_removing() whether that was successful or not, but it not needed
155  * if start_removing() failed.
156  */
end_removing(struct dentry * child)157 static inline void end_removing(struct dentry *child)
158 {
159 	end_dirop(child);
160 }
161 
162 extern int follow_down_one(struct path *);
163 extern int follow_down(struct path *path, unsigned int flags);
164 extern int follow_up(struct path *);
165 
166 int start_renaming(struct renamedata *rd, int lookup_flags,
167 		   struct qstr *old_last, struct qstr *new_last);
168 int start_renaming_dentry(struct renamedata *rd, int lookup_flags,
169 			  struct dentry *old_dentry, struct qstr *new_last);
170 int start_renaming_two_dentries(struct renamedata *rd,
171 				struct dentry *old_dentry, struct dentry *new_dentry);
172 void end_renaming(struct renamedata *rd);
173 
174 /**
175  * mode_strip_umask - handle vfs umask stripping
176  * @dir:	parent directory of the new inode
177  * @mode:	mode of the new inode to be created in @dir
178  *
179  * In most filesystems, umask stripping depends on whether or not the
180  * filesystem supports POSIX ACLs. If the filesystem doesn't support it umask
181  * stripping is done directly in here. If the filesystem does support POSIX
182  * ACLs umask stripping is deferred until the filesystem calls
183  * posix_acl_create().
184  *
185  * Some filesystems (like NFSv4) also want to avoid umask stripping by the
186  * VFS, but don't support POSIX ACLs. Those filesystems can set SB_I_NOUMASK
187  * to get this effect without declaring that they support POSIX ACLs.
188  *
189  * Returns: mode
190  */
mode_strip_umask(const struct inode * dir,umode_t mode)191 static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
192 {
193 	if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
194 		mode &= ~current_umask();
195 	return mode;
196 }
197 
198 extern int __must_check nd_jump_link(const struct path *path);
199 
nd_terminate_link(void * name,size_t len,size_t maxlen)200 static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
201 {
202 	((char *) name)[min(len, maxlen)] = '\0';
203 }
204 
205 /**
206  * retry_estale - determine whether the caller should retry an operation
207  * @error: the error that would currently be returned
208  * @flags: flags being used for next lookup attempt
209  *
210  * Check to see if the error code was -ESTALE, and then determine whether
211  * to retry the call based on whether "flags" already has LOOKUP_REVAL set.
212  *
213  * Returns true if the caller should try the operation again.
214  */
215 static inline bool
retry_estale(const long error,const unsigned int flags)216 retry_estale(const long error, const unsigned int flags)
217 {
218 	return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
219 }
220 
221 #endif /* _LINUX_NAMEI_H */
222