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 
11 enum { MAX_NESTED_LINKS = 8 };
12 
13 #define MAXSYMLINKS 40
14 
15 /*
16  * Type of the last component on LOOKUP_PARENT
17  */
18 enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT};
19 
20 /* pathwalk mode */
21 #define LOOKUP_FOLLOW		BIT(0)	/* follow links at the end */
22 #define LOOKUP_DIRECTORY	BIT(1)	/* require a directory */
23 #define LOOKUP_AUTOMOUNT	BIT(2)  /* force terminal automount */
24 #define LOOKUP_EMPTY		BIT(3)	/* accept empty path [user_... only] */
25 #define LOOKUP_LINKAT_EMPTY	BIT(4) /* Linkat request with empty path. */
26 #define LOOKUP_DOWN		BIT(5)	/* follow mounts in the starting point */
27 #define LOOKUP_MOUNTPOINT	BIT(6)	/* follow mounts in the end */
28 #define LOOKUP_REVAL		BIT(7)	/* tell ->d_revalidate() to trust no cache */
29 #define LOOKUP_RCU		BIT(8)	/* RCU pathwalk mode; semi-internal */
30 #define LOOKUP_CACHED		BIT(9) /* Only do cached lookup */
31 #define LOOKUP_PARENT		BIT(10)	/* Looking up final parent in path */
32 /* 5 spare bits for pathwalk */
33 
34 /* These tell filesystem methods that we are dealing with the final component... */
35 #define LOOKUP_OPEN		BIT(16)	/* ... in open */
36 #define LOOKUP_CREATE		BIT(17)	/* ... in object creation */
37 #define LOOKUP_EXCL		BIT(18)	/* ... in target must not exist */
38 #define LOOKUP_RENAME_TARGET	BIT(19)	/* ... in destination of rename() */
39 
40 /* 4 spare bits for intent */
41 
42 /* Scoping flags for lookup. */
43 #define LOOKUP_NO_SYMLINKS	BIT(24) /* No symlink crossing. */
44 #define LOOKUP_NO_MAGICLINKS	BIT(25) /* No nd_jump_link() crossing. */
45 #define LOOKUP_NO_XDEV		BIT(26) /* No mountpoint crossing. */
46 #define LOOKUP_BENEATH		BIT(27) /* No escaping from starting point. */
47 #define LOOKUP_IN_ROOT		BIT(28) /* Treat dirfd as fs root. */
48 /* LOOKUP_* flags which do scope-related checks based on the dirfd. */
49 #define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT)
50 /* 3 spare bits for scoping */
51 
52 extern int path_pts(struct path *path);
53 
54 extern int user_path_at(int, const char __user *, unsigned, struct path *);
55 
56 struct dentry *lookup_one_qstr_excl(const struct qstr *name,
57 				    struct dentry *base,
58 				    unsigned int flags);
59 extern int kern_path(const char *, unsigned, struct path *);
60 
61 extern struct dentry *kern_path_create(int, const char *, struct path *, unsigned int);
62 extern struct dentry *user_path_create(int, const char __user *, struct path *, unsigned int);
63 extern void done_path_create(struct path *, struct dentry *);
64 extern struct dentry *kern_path_locked(const char *, struct path *);
65 extern struct dentry *kern_path_locked_negative(const char *, struct path *);
66 extern struct dentry *user_path_locked_at(int , const char __user *, struct path *);
67 int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
68 			   struct path *parent, struct qstr *last, int *type,
69 			   const struct path *root);
70 int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *,
71 		    unsigned int, struct path *);
72 
73 extern struct dentry *try_lookup_one_len(const char *, struct dentry *, int);
74 extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
75 extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
76 extern struct dentry *lookup_positive_unlocked(const char *, struct dentry *, int);
77 struct dentry *lookup_one(struct mnt_idmap *, const char *, struct dentry *, int);
78 struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
79 				   const char *name, struct dentry *base,
80 				   int len);
81 struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
82 					    const char *name,
83 					    struct dentry *base, int len);
84 
85 extern int follow_down_one(struct path *);
86 extern int follow_down(struct path *path, unsigned int flags);
87 extern int follow_up(struct path *);
88 
89 extern struct dentry *lock_rename(struct dentry *, struct dentry *);
90 extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
91 extern void unlock_rename(struct dentry *, struct dentry *);
92 
93 /**
94  * mode_strip_umask - handle vfs umask stripping
95  * @dir:	parent directory of the new inode
96  * @mode:	mode of the new inode to be created in @dir
97  *
98  * In most filesystems, umask stripping depends on whether or not the
99  * filesystem supports POSIX ACLs. If the filesystem doesn't support it umask
100  * stripping is done directly in here. If the filesystem does support POSIX
101  * ACLs umask stripping is deferred until the filesystem calls
102  * posix_acl_create().
103  *
104  * Some filesystems (like NFSv4) also want to avoid umask stripping by the
105  * VFS, but don't support POSIX ACLs. Those filesystems can set SB_I_NOUMASK
106  * to get this effect without declaring that they support POSIX ACLs.
107  *
108  * Returns: mode
109  */
mode_strip_umask(const struct inode * dir,umode_t mode)110 static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
111 {
112 	if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
113 		mode &= ~current_umask();
114 	return mode;
115 }
116 
117 extern int __must_check nd_jump_link(const struct path *path);
118 
nd_terminate_link(void * name,size_t len,size_t maxlen)119 static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
120 {
121 	((char *) name)[min(len, maxlen)] = '\0';
122 }
123 
124 /**
125  * retry_estale - determine whether the caller should retry an operation
126  * @error: the error that would currently be returned
127  * @flags: flags being used for next lookup attempt
128  *
129  * Check to see if the error code was -ESTALE, and then determine whether
130  * to retry the call based on whether "flags" already has LOOKUP_REVAL set.
131  *
132  * Returns true if the caller should try the operation again.
133  */
134 static inline bool
retry_estale(const long error,const unsigned int flags)135 retry_estale(const long error, const unsigned int flags)
136 {
137 	return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
138 }
139 
140 #endif /* _LINUX_NAMEI_H */
141