xref: /linux-6.15/include/linux/namei.h (revision c86b300b)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_NAMEI_H
31da177e4SLinus Torvalds #define _LINUX_NAMEI_H
41da177e4SLinus Torvalds 
5adb21d2bSAleksa Sarai #include <linux/fs.h>
6b853a161SAl Viro #include <linux/kernel.h>
7c5e725f3SJan Blunck #include <linux/path.h>
8b853a161SAl Viro #include <linux/fcntl.h>
9b853a161SAl Viro #include <linux/errno.h>
101da177e4SLinus Torvalds 
11737bebd1SAl Viro enum { MAX_NESTED_LINKS = 8 };
121da177e4SLinus Torvalds 
13894bc8c4SAl Viro #define MAXSYMLINKS 40
14894bc8c4SAl Viro 
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  * Type of the last component on LOOKUP_PARENT
171da177e4SLinus Torvalds  */
18b4c03536SAl Viro enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT};
191da177e4SLinus Torvalds 
206b61aed0SAl Viro /* pathwalk mode */
212c3230fbSNeilBrown #define LOOKUP_FOLLOW		BIT(0)	/* follow links at the end */
222c3230fbSNeilBrown #define LOOKUP_DIRECTORY	BIT(1)	/* require a directory */
232c3230fbSNeilBrown #define LOOKUP_AUTOMOUNT	BIT(2)  /* force terminal automount */
242c3230fbSNeilBrown #define LOOKUP_EMPTY		BIT(3)	/* accept empty path [user_... only] */
252c3230fbSNeilBrown #define LOOKUP_LINKAT_EMPTY	BIT(4) /* Linkat request with empty path. */
262c3230fbSNeilBrown #define LOOKUP_DOWN		BIT(5)	/* follow mounts in the starting point */
272c3230fbSNeilBrown #define LOOKUP_MOUNTPOINT	BIT(6)	/* follow mounts in the end */
282c3230fbSNeilBrown #define LOOKUP_REVAL		BIT(7)	/* tell ->d_revalidate() to trust no cache */
292c3230fbSNeilBrown #define LOOKUP_RCU		BIT(8)	/* RCU pathwalk mode; semi-internal */
302c3230fbSNeilBrown #define LOOKUP_CACHED		BIT(9) /* Only do cached lookup */
312c3230fbSNeilBrown #define LOOKUP_PARENT		BIT(10)	/* Looking up final parent in path */
322c3230fbSNeilBrown /* 5 spare bits for pathwalk */
336b61aed0SAl Viro 
346b61aed0SAl Viro /* These tell filesystem methods that we are dealing with the final component... */
352c3230fbSNeilBrown #define LOOKUP_OPEN		BIT(16)	/* ... in open */
362c3230fbSNeilBrown #define LOOKUP_CREATE		BIT(17)	/* ... in object creation */
372c3230fbSNeilBrown #define LOOKUP_EXCL		BIT(18)	/* ... in target must not exist */
382c3230fbSNeilBrown #define LOOKUP_RENAME_TARGET	BIT(19)	/* ... in destination of rename() */
396b61aed0SAl Viro 
402c3230fbSNeilBrown /* 4 spare bits for intent */
4116c2cd71SAl Viro 
4227812141SAleksa Sarai /* Scoping flags for lookup. */
432c3230fbSNeilBrown #define LOOKUP_NO_SYMLINKS	BIT(24) /* No symlink crossing. */
442c3230fbSNeilBrown #define LOOKUP_NO_MAGICLINKS	BIT(25) /* No nd_jump_link() crossing. */
452c3230fbSNeilBrown #define LOOKUP_NO_XDEV		BIT(26) /* No mountpoint crossing. */
462c3230fbSNeilBrown #define LOOKUP_BENEATH		BIT(27) /* No escaping from starting point. */
472c3230fbSNeilBrown #define LOOKUP_IN_ROOT		BIT(28) /* Treat dirfd as fs root. */
48adb21d2bSAleksa Sarai /* LOOKUP_* flags which do scope-related checks based on the dirfd. */
498db52c7eSAleksa Sarai #define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT)
502c3230fbSNeilBrown /* 3 spare bits for scoping */
5127812141SAleksa Sarai 
52eedf265aSEric W. Biederman extern int path_pts(struct path *path);
53eedf265aSEric W. Biederman 
54dff60734SMateusz Guzik extern int user_path_at(int, const char __user *, unsigned, struct path *);
55b853a161SAl Viro 
5674d7970fSNamjae Jeon struct dentry *lookup_one_qstr_excl(const struct qstr *name,
5774d7970fSNamjae Jeon 				    struct dentry *base,
5874d7970fSNamjae Jeon 				    unsigned int flags);
59d1811465SAl Viro extern int kern_path(const char *, unsigned, struct path *);
60d1811465SAl Viro 
611ac12b4bSJeff Layton extern struct dentry *kern_path_create(int, const char *, struct path *, unsigned int);
621ac12b4bSJeff Layton extern struct dentry *user_path_create(int, const char __user *, struct path *, unsigned int);
63921a1650SAl Viro extern void done_path_create(struct path *, struct dentry *);
6479714f72SAl Viro extern struct dentry *kern_path_locked(const char *, struct path *);
65*c86b300bSChristian Brauner extern struct dentry *kern_path_locked_negative(const char *, struct path *);
6674d016ecSAl Viro extern struct dentry *user_path_locked_at(int , const char __user *, struct path *);
6774d7970fSNamjae Jeon int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
6874d7970fSNamjae Jeon 			   struct path *parent, struct qstr *last, int *type,
6974d7970fSNamjae Jeon 			   const struct path *root);
70211db0acSNamjae Jeon int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *,
71211db0acSNamjae Jeon 		    unsigned int, struct path *);
721da177e4SLinus Torvalds 
730da0b7fdSDavid Howells extern struct dentry *try_lookup_one_len(const char *, struct dentry *, int);
741da177e4SLinus Torvalds extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
75bbddca8eSNeilBrown extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
766c2d4798SAl Viro extern struct dentry *lookup_positive_unlocked(const char *, struct dentry *, int);
774609e1f1SChristian Brauner struct dentry *lookup_one(struct mnt_idmap *, const char *, struct dentry *, int);
784609e1f1SChristian Brauner struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
7900675017SChristian Brauner 				   const char *name, struct dentry *base,
8000675017SChristian Brauner 				   int len);
814609e1f1SChristian Brauner struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
8200675017SChristian Brauner 					    const char *name,
8300675017SChristian Brauner 					    struct dentry *base, int len);
841da177e4SLinus Torvalds 
85cc53ce53SDavid Howells extern int follow_down_one(struct path *);
86e1f19857SRichard Weinberger extern int follow_down(struct path *path, unsigned int flags);
87bab77ebfSAl Viro extern int follow_up(struct path *);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds extern struct dentry *lock_rename(struct dentry *, struct dentry *);
909bc37e04SAl Viro extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
911da177e4SLinus Torvalds extern void unlock_rename(struct dentry *, struct dentry *);
921da177e4SLinus Torvalds 
935aa8fd9cSJeff Layton /**
945aa8fd9cSJeff Layton  * mode_strip_umask - handle vfs umask stripping
955aa8fd9cSJeff Layton  * @dir:	parent directory of the new inode
965aa8fd9cSJeff Layton  * @mode:	mode of the new inode to be created in @dir
975aa8fd9cSJeff Layton  *
985aa8fd9cSJeff Layton  * In most filesystems, umask stripping depends on whether or not the
995aa8fd9cSJeff Layton  * filesystem supports POSIX ACLs. If the filesystem doesn't support it umask
1005aa8fd9cSJeff Layton  * stripping is done directly in here. If the filesystem does support POSIX
1015aa8fd9cSJeff Layton  * ACLs umask stripping is deferred until the filesystem calls
1025aa8fd9cSJeff Layton  * posix_acl_create().
1035aa8fd9cSJeff Layton  *
1045aa8fd9cSJeff Layton  * Some filesystems (like NFSv4) also want to avoid umask stripping by the
1055aa8fd9cSJeff Layton  * VFS, but don't support POSIX ACLs. Those filesystems can set SB_I_NOUMASK
1065aa8fd9cSJeff Layton  * to get this effect without declaring that they support POSIX ACLs.
1075aa8fd9cSJeff Layton  *
1085aa8fd9cSJeff Layton  * Returns: mode
1095aa8fd9cSJeff Layton  */
mode_strip_umask(const struct inode * dir,umode_t mode)1105aa8fd9cSJeff Layton static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
1115aa8fd9cSJeff Layton {
1125aa8fd9cSJeff Layton 	if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
1135aa8fd9cSJeff Layton 		mode &= ~current_umask();
1145aa8fd9cSJeff Layton 	return mode;
1155aa8fd9cSJeff Layton }
1165aa8fd9cSJeff Layton 
117ea4af4aaSAl Viro extern int __must_check nd_jump_link(const struct path *path);
1181da177e4SLinus Torvalds 
nd_terminate_link(void * name,size_t len,size_t maxlen)11903514685SDuane Griffin static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
12003514685SDuane Griffin {
12103514685SDuane Griffin 	((char *) name)[min(len, maxlen)] = '\0';
12203514685SDuane Griffin }
12303514685SDuane Griffin 
124b9d6ba94SJeff Layton /**
125b9d6ba94SJeff Layton  * retry_estale - determine whether the caller should retry an operation
126b9d6ba94SJeff Layton  * @error: the error that would currently be returned
127b9d6ba94SJeff Layton  * @flags: flags being used for next lookup attempt
128b9d6ba94SJeff Layton  *
129b9d6ba94SJeff Layton  * Check to see if the error code was -ESTALE, and then determine whether
130b9d6ba94SJeff Layton  * to retry the call based on whether "flags" already has LOOKUP_REVAL set.
131b9d6ba94SJeff Layton  *
132b9d6ba94SJeff Layton  * Returns true if the caller should try the operation again.
133b9d6ba94SJeff Layton  */
134b9d6ba94SJeff Layton static inline bool
retry_estale(const long error,const unsigned int flags)135b9d6ba94SJeff Layton retry_estale(const long error, const unsigned int flags)
136b9d6ba94SJeff Layton {
13795e93d17SMateusz Guzik 	return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
138b9d6ba94SJeff Layton }
139b9d6ba94SJeff Layton 
1401da177e4SLinus Torvalds #endif /* _LINUX_NAMEI_H */
141