/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _LINUX_NAMEI_H
#define _LINUX_NAMEI_H

#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/path.h>
#include <linux/fcntl.h>
#include <linux/errno.h>
#include <linux/fs_struct.h>

enum { MAX_NESTED_LINKS = 8 };

#define MAXSYMLINKS 40


enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT};


#define LOOKUP_FOLLOW		BIT(0)	
#define LOOKUP_DIRECTORY	BIT(1)	
#define LOOKUP_AUTOMOUNT	BIT(2)  
#define LOOKUP_EMPTY		BIT(3)	
#define LOOKUP_LINKAT_EMPTY	BIT(4) 
#define LOOKUP_DOWN		BIT(5)	
#define LOOKUP_MOUNTPOINT	BIT(6)	
#define LOOKUP_REVAL		BIT(7)	
#define LOOKUP_RCU		BIT(8)	
#define LOOKUP_CACHED		BIT(9) 
#define LOOKUP_PARENT		BIT(10)	



#define LOOKUP_OPEN		BIT(16)	
#define LOOKUP_CREATE		BIT(17)	
#define LOOKUP_EXCL		BIT(18)	
#define LOOKUP_RENAME_TARGET	BIT(19)	




#define LOOKUP_NO_SYMLINKS	BIT(24) 
#define LOOKUP_NO_MAGICLINKS	BIT(25) 
#define LOOKUP_NO_XDEV		BIT(26) 
#define LOOKUP_BENEATH		BIT(27) 
#define LOOKUP_IN_ROOT		BIT(28) 

#define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT)


#define LOOKUP_KPRIV 0x400000
#define LOOKUP_WEAK_CACHED 0x800000

extern int path_pts(struct path *path);

extern int user_path_at(int, const char __user *, unsigned, struct path *);

struct dentry *lookup_one_qstr_excl(const struct qstr *name,
				    struct dentry *base,
				    unsigned int flags);
extern int kern_path(const char *, unsigned, struct path *);
struct dentry *kern_path_parent(const char *name, struct path *parent);

extern struct dentry *start_creating_path(int, const char *, struct path *, unsigned int);
extern struct dentry *start_creating_user_path(int, const char __user *, struct path *, unsigned int);
extern void end_creating_path(const struct path *, struct dentry *);
extern struct dentry *start_removing_path(const char *, struct path *);
extern struct dentry *start_removing_user_path_at(int , const char __user *, struct path *);
static inline void end_removing_path(const struct path *path , struct dentry *dentry)
{
	end_creating_path(path, dentry);
}
int vfs_path_parent_lookup(struct filename *filename, unsigned int flags,
			   struct path *parent, struct qstr *last, int *type,
			   const struct path *root);
int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *,
		    unsigned int, struct path *);

extern struct dentry *try_lookup_noperm(struct qstr *, struct dentry *);
extern struct dentry *lookup_noperm(struct qstr *, struct dentry *);
extern struct dentry *lookup_noperm_unlocked(struct qstr *, struct dentry *);
extern struct dentry *lookup_noperm_positive_unlocked(struct qstr *, struct dentry *);
struct dentry *lookup_one(struct mnt_idmap *, struct qstr *, struct dentry *);
struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap,
				   struct qstr *name, struct dentry *base);
struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap,
					    struct qstr *name,
					    struct dentry *base);
struct dentry *lookup_one_positive_killable(struct mnt_idmap *idmap,
					    struct qstr *name,
					    struct dentry *base);

struct dentry *start_creating(struct mnt_idmap *idmap, struct dentry *parent,
			      struct qstr *name);
struct dentry *start_removing(struct mnt_idmap *idmap, struct dentry *parent,
			      struct qstr *name);
struct dentry *start_creating_killable(struct mnt_idmap *idmap,
				       struct dentry *parent,
				       struct qstr *name);
struct dentry *start_removing_killable(struct mnt_idmap *idmap,
				       struct dentry *parent,
				       struct qstr *name);
struct dentry *start_creating_noperm(struct dentry *parent, struct qstr *name);
struct dentry *start_removing_noperm(struct dentry *parent, struct qstr *name);
struct dentry *start_creating_dentry(struct dentry *parent,
				     struct dentry *child);
struct dentry *start_removing_dentry(struct dentry *parent,
				     struct dentry *child);


static inline void end_creating(struct dentry *child)
{
	end_dirop(child);
}


static inline struct dentry *end_creating_keep(struct dentry *child)
{
	if (!IS_ERR(child))
		dget(child);
	end_dirop(child);
	return child;
}


static inline void end_removing(struct dentry *child)
{
	end_dirop(child);
}

extern int follow_down_one(struct path *);
extern int follow_down(struct path *path, unsigned int flags);
extern int follow_up(struct path *);

extern struct dentry *lock_rename(struct dentry *, struct dentry *);
extern struct dentry *lock_rename_child(struct dentry *, struct dentry *);
extern void unlock_rename(struct dentry *, struct dentry *);
int start_renaming(struct renamedata *rd, int lookup_flags,
		   struct qstr *old_last, struct qstr *new_last);
int start_renaming_dentry(struct renamedata *rd, int lookup_flags,
			  struct dentry *old_dentry, struct qstr *new_last);
int start_renaming_two_dentries(struct renamedata *rd,
				struct dentry *old_dentry, struct dentry *new_dentry);
void end_renaming(struct renamedata *rd);


static inline umode_t __must_check mode_strip_umask(const struct inode *dir, umode_t mode)
{
	if (!IS_POSIXACL(dir) && !(dir->i_sb->s_iflags & SB_I_NOUMASK))
		mode &= ~current_umask();
	return mode;
}

extern int __must_check nd_jump_link(const struct path *path);

static inline void nd_terminate_link(void *name, size_t len, size_t maxlen)
{
	((char *) name)[min(len, maxlen)] = '\0';
}


static inline bool
retry_estale(const long error, const unsigned int flags)
{
	return unlikely(error == -ESTALE && !(flags & LOOKUP_REVAL));
}

#endif 
