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