xref: /linux/include/linux/namei.h (revision a3d14d1602ca11429d242d230c31af8f822f614f)
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_noperm(struct qstr *, struct dentry *);
74 extern struct dentry *lookup_noperm(struct qstr *, struct dentry *);
75 extern struct dentry *lookup_noperm_unlocked(struct qstr *, struct dentry *);
76 extern struct dentry *lookup_noperm_positive_unlocked(struct qstr *, struct dentry *);
77 struct dentry *lookup_one(struct mnt_idmap *, struct qstr *, struct dentry *);
78 struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
79 				   struct qstr *name, struct dentry *base);
80 struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
81 					    struct qstr *name,
82 					    struct dentry *base);
83 
84 extern int follow_down_one(struct path *);
85 extern int follow_down(struct path *path, unsigned int flags);
86 extern int follow_up(struct path *);
87 
88 extern struct dentry *lock_rename(struct dentry *, struct dentry *);
89 extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
90 extern void unlock_rename(struct dentry *, struct dentry *);
91 
92 /**
93  * mode_strip_umask - handle vfs umask stripping
94  * @dir:	parent directory of the new inode
95  * @mode:	mode of the new inode to be created in @dir
96  *
97  * In most filesystems, umask stripping depends on whether or not the
98  * filesystem supports POSIX ACLs. If the filesystem doesn't support it umask
99  * stripping is done directly in here. If the filesystem does support POSIX
100  * ACLs umask stripping is deferred until the filesystem calls
101  * posix_acl_create().
102  *
103  * Some filesystems (like NFSv4) also want to avoid umask stripping by the
104  * VFS, but don't support POSIX ACLs. Those filesystems can set SB_I_NOUMASK
105  * to get this effect without declaring that they support POSIX ACLs.
106  *
107  * Returns: mode
108  */
mode_strip_umask(const struct inode * dir,umode_t mode)109 static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
110 {
111 	if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
112 		mode &= ~current_umask();
113 	return mode;
114 }
115 
116 extern int __must_check nd_jump_link(const struct path *path);
117 
nd_terminate_link(void * name,size_t len,size_t maxlen)118 static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
119 {
120 	((char *) name)[min(len, maxlen)] = '\0';
121 }
122 
123 /**
124  * retry_estale - determine whether the caller should retry an operation
125  * @error: the error that would currently be returned
126  * @flags: flags being used for next lookup attempt
127  *
128  * Check to see if the error code was -ESTALE, and then determine whether
129  * to retry the call based on whether "flags" already has LOOKUP_REVAL set.
130  *
131  * Returns true if the caller should try the operation again.
132  */
133 static inline bool
retry_estale(const long error,const unsigned int flags)134 retry_estale(const long error, const unsigned int flags)
135 {
136 	return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
137 }
138 
139 #endif /* _LINUX_NAMEI_H */
140