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
11enum { MAX_NESTED_LINKS = 8 };
12
13#define MAXSYMLINKS 40
14
15/*
16 * Type of the last component on LOOKUP_PARENT
17 */
18enum {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
52extern int path_pts(struct path *path);
53
54extern int user_path_at(int, const char __user *, unsigned, struct path *);
55
56struct dentry *lookup_one_qstr_excl(const struct qstr *name,
57 struct dentry *base,
58 unsigned int flags);
59extern int kern_path(const char *, unsigned, struct path *);
60struct dentry *kern_path_parent(const char *name, struct path *parent);
61
62extern struct dentry *start_creating_path(int, const char *, struct path *, unsigned int);
63extern struct dentry *start_creating_user_path(int, const char __user *, struct path *, unsigned int);
64extern void end_creating_path(const struct path *, struct dentry *);
65extern struct dentry *start_removing_path(const char *, struct path *);
66extern struct dentry *start_removing_user_path_at(int , const char __user *, struct path *);
67static inline void end_removing_path(const struct path *path , struct dentry *dentry)
68{
69 end_creating_path(path, dentry);
70}
71int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
72 struct path *parent, struct qstr *last, int *type,
73 const struct path *root);
74int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *,
75 unsigned int, struct path *);
76
77extern struct dentry *try_lookup_noperm(struct qstr *, struct dentry *);
78extern struct dentry *lookup_noperm(struct qstr *, struct dentry *);
79extern struct dentry *lookup_noperm_unlocked(struct qstr *, struct dentry *);
80extern struct dentry *lookup_noperm_positive_unlocked(struct qstr *, struct dentry *);
81struct dentry *lookup_one(struct mnt_idmap *, struct qstr *, struct dentry *);
82struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
83 struct qstr *name, struct dentry *base);
84struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
85 struct qstr *name,
86 struct dentry *base);
87struct dentry *lookup_one_positive_killable(struct mnt_idmap *idmap,
88 struct qstr *name,
89 struct dentry *base);
90
91extern int follow_down_one(struct path *);
92extern int follow_down(struct path *path, unsigned int flags);
93extern int follow_up(struct path *);
94
95extern struct dentry *lock_rename(struct dentry *, struct dentry *);
96extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
97extern void unlock_rename(struct dentry *, struct dentry *);
98
99/**
100 * mode_strip_umask - handle vfs umask stripping
101 * @dir: parent directory of the new inode
102 * @mode: mode of the new inode to be created in @dir
103 *
104 * In most filesystems, umask stripping depends on whether or not the
105 * filesystem supports POSIX ACLs. If the filesystem doesn't support it umask
106 * stripping is done directly in here. If the filesystem does support POSIX
107 * ACLs umask stripping is deferred until the filesystem calls
108 * posix_acl_create().
109 *
110 * Some filesystems (like NFSv4) also want to avoid umask stripping by the
111 * VFS, but don't support POSIX ACLs. Those filesystems can set SB_I_NOUMASK
112 * to get this effect without declaring that they support POSIX ACLs.
113 *
114 * Returns: mode
115 */
116static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
117{
118 if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
119 mode &= ~current_umask();
120 return mode;
121}
122
123extern int __must_check nd_jump_link(const struct path *path);
124
125static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
126{
127 ((char *) name)[min(len, maxlen)] = '\0';
128}
129
130/**
131 * retry_estale - determine whether the caller should retry an operation
132 * @error: the error that would currently be returned
133 * @flags: flags being used for next lookup attempt
134 *
135 * Check to see if the error code was -ESTALE, and then determine whether
136 * to retry the call based on whether "flags" already has LOOKUP_REVAL set.
137 *
138 * Returns true if the caller should try the operation again.
139 */
140static inline bool
141retry_estale(const long error, const unsigned int flags)
142{
143 return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
144}
145
146#endif /* _LINUX_NAMEI_H */
147