1139825Simp/*-
21541Srgrimes * Copyright (c) 1985, 1989, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2914512Shsu *	@(#)namei.h	8.5 (Berkeley) 1/9/95
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
331541Srgrimes#ifndef _SYS_NAMEI_H_
341541Srgrimes#define	_SYS_NAMEI_H_
351541Srgrimes
36255219Spjd#include <sys/caprights.h>
37247602Spjd#include <sys/filedesc.h>
386928Sphk#include <sys/queue.h>
3934924Sbde#include <sys/uio.h>
406928Sphk
4184061Sluigistruct componentname {
4284061Sluigi	/*
4384061Sluigi	 * Arguments to lookup.
4484061Sluigi	 */
4584061Sluigi	u_long	cn_nameiop;	/* namei operation */
46195508Skib	u_int64_t cn_flags;	/* flags to namei */
4784061Sluigi	struct	thread *cn_thread;/* thread requesting lookup */
4884061Sluigi	struct	ucred *cn_cred;	/* credentials */
49144285Sjeff	int	cn_lkflags;	/* Lock flags LK_EXCLUSIVE or LK_SHARED */
5084061Sluigi	/*
5184061Sluigi	 * Shared between lookup and commit routines.
5284061Sluigi	 */
5384061Sluigi	char	*cn_pnbuf;	/* pathname buffer */
5484061Sluigi	char	*cn_nameptr;	/* pointer to looked up name */
5584061Sluigi	long	cn_namelen;	/* length of looked up component */
5684061Sluigi	long	cn_consume;	/* chars to consume in lookup() */
5784061Sluigi};
5884061Sluigi
591541Srgrimes/*
601541Srgrimes * Encapsulation of namei parameters.
611541Srgrimes */
621541Srgrimesstruct nameidata {
631541Srgrimes	/*
641541Srgrimes	 * Arguments to namei/lookup.
651541Srgrimes	 */
6618027Sbde	const	char *ni_dirp;		/* pathname pointer */
671541Srgrimes	enum	uio_seg ni_segflg;	/* location of pathname */
68224810Sjonathan	cap_rights_t ni_rightsneeded;	/* rights required to look up vnode */
691541Srgrimes	/*
701541Srgrimes	 * Arguments to lookup.
711541Srgrimes	 */
72185029Spjd	struct  vnode *ni_startdir;	/* starting directory */
731541Srgrimes	struct	vnode *ni_rootdir;	/* logical root directory */
7451649Sphk	struct	vnode *ni_topdir;	/* logical top directory */
75177785Skib	int	ni_dirfd;		/* starting directory for *at functions */
76224810Sjonathan	int	ni_strictrelative;	/* relative lookup only; no '..' */
771541Srgrimes	/*
78224810Sjonathan	 * Results: returned from namei
79224810Sjonathan	 */
80247602Spjd	struct filecaps ni_filecaps;	/* rights the *at base has */
81224810Sjonathan	/*
821541Srgrimes	 * Results: returned from/manipulated by lookup
831541Srgrimes	 */
841541Srgrimes	struct	vnode *ni_vp;		/* vnode of result */
851541Srgrimes	struct	vnode *ni_dvp;		/* vnode of intermediate directory */
861541Srgrimes	/*
871541Srgrimes	 * Shared between namei and lookup/commit routines.
881541Srgrimes	 */
8936735Sdfr	size_t	ni_pathlen;		/* remaining chars in path */
901541Srgrimes	char	*ni_next;		/* next location in pathname */
91195508Skib	u_int	ni_loopcnt;		/* count of symlinks encountered */
921541Srgrimes	/*
931541Srgrimes	 * Lookup parameters: this structure describes the subset of
941541Srgrimes	 * information from the nameidata structure that is passed
951541Srgrimes	 * through the VOP interface.
961541Srgrimes	 */
9784061Sluigi	struct componentname ni_cnd;
981541Srgrimes};
991541Srgrimes
10055205Speter#ifdef _KERNEL
1011541Srgrimes/*
1021541Srgrimes * namei operations
1031541Srgrimes */
1041541Srgrimes#define	LOOKUP		0	/* perform name lookup only */
1051541Srgrimes#define	CREATE		1	/* setup for file creation */
1061541Srgrimes#define	DELETE		2	/* setup for file deletion */
1071541Srgrimes#define	RENAME		3	/* setup for file renaming */
1081541Srgrimes#define	OPMASK		3	/* mask for operation */
1091541Srgrimes/*
1101541Srgrimes * namei operational modifier flags, stored in ni_cnd.flags
1111541Srgrimes */
112228952Spluknet#define	LOCKLEAF	0x0004	/* lock vnode on return */
1131541Srgrimes#define	LOCKPARENT	0x0008	/* want parent vnode returned locked */
1141541Srgrimes#define	WANTPARENT	0x0010	/* want parent vnode returned unlocked */
1151541Srgrimes#define	NOCACHE		0x0020	/* name must not be left in cache */
1161541Srgrimes#define	FOLLOW		0x0040	/* follow symbolic links */
11792130Sjeff#define	LOCKSHARED	0x0100	/* Shared lock leaf */
1181541Srgrimes#define	NOFOLLOW	0x0000	/* do not follow symbolic links (pseudo) */
11992130Sjeff#define	MODMASK		0x01fc	/* mask of operational modifiers */
1201541Srgrimes/*
1211541Srgrimes * Namei parameter descriptors.
1221541Srgrimes *
1231541Srgrimes * SAVENAME may be set by either the callers of namei or by VOP_LOOKUP.
1241541Srgrimes * If the caller of namei sets the flag (for example execve wants to
1251541Srgrimes * know the name of the program that is being executed), then it must
1261541Srgrimes * free the buffer. If VOP_LOOKUP sets the flag, then the buffer must
1271541Srgrimes * be freed by either the commit routine or the VOP_ABORT routine.
1281541Srgrimes * SAVESTART is set only by the callers of namei. It implies SAVENAME
1291541Srgrimes * plus the addition of saving the parent directory that contains the
1301541Srgrimes * name in ni_startdir. It allows repeated calls to lookup for the
1311541Srgrimes * name being sought. The caller is responsible for releasing the
1321541Srgrimes * buffer and for vrele'ing ni_startdir.
1331541Srgrimes */
134192900Sdes#define	RDONLY		0x00000200 /* lookup with read-only semantics */
135192900Sdes#define	HASBUF		0x00000400 /* has allocated pathname buffer */
136192900Sdes#define	SAVENAME	0x00000800 /* save pathname buffer */
137192900Sdes#define	SAVESTART	0x00001000 /* save starting directory */
138192900Sdes#define	ISDOTDOT	0x00002000 /* current component name is .. */
139192900Sdes#define	MAKEENTRY	0x00004000 /* entry is to be added to name cache */
140192900Sdes#define	ISLASTCN	0x00008000 /* this is last component of pathname */
141192900Sdes#define	ISSYMLINK	0x00010000 /* symlink needs interpretation */
142192900Sdes#define	ISWHITEOUT	0x00020000 /* found whiteout */
143192900Sdes#define	DOWHITEOUT	0x00040000 /* do whiteouts */
144192900Sdes#define	WILLBEDIR	0x00080000 /* new files will be dirs; allow trailing / */
145192900Sdes#define	ISUNICODE	0x00100000 /* current component name is unicode*/
146192900Sdes#define	ISOPEN		0x00200000 /* caller is opening; return a real vnode. */
147192900Sdes#define	NOCROSSMOUNT	0x00400000 /* do not cross mount points */
148192900Sdes#define	NOMACCHECK	0x00800000 /* do not perform MAC checks */
149192900Sdes#define	AUDITVNODE1	0x04000000 /* audit the looked up vnode information */
150246903Spjd#define	AUDITVNODE2	0x08000000 /* audit the looked up vnode information */
151193028Sdes#define	TRAILINGSLASH	0x10000000 /* path ended in a slash */
152243612Spjd#define	NOCAPCHECK	0x20000000 /* do not perform capability checks */
153243612Spjd#define	PARAMASK	0x3ffffe00 /* mask of parameter descriptors */
15483366Sjulian
1551541Srgrimes/*
156108470Sschweikh * Initialization of a nameidata structure.
1571541Srgrimes */
158185029Spjd#define	NDINIT(ndp, op, flags, segflg, namep, td)			\
159224810Sjonathan	NDINIT_ALL(ndp, op, flags, segflg, namep, AT_FDCWD, NULL, 0, td)
160185029Spjd#define	NDINIT_AT(ndp, op, flags, segflg, namep, dirfd, td)		\
161224810Sjonathan	NDINIT_ALL(ndp, op, flags, segflg, namep, dirfd, NULL, 0, td)
162255219Spjd#define	NDINIT_ATRIGHTS(ndp, op, flags, segflg, namep, dirfd, rightsp, td) \
163255219Spjd	NDINIT_ALL(ndp, op, flags, segflg, namep, dirfd, NULL, rightsp, td)
164185029Spjd#define	NDINIT_ATVP(ndp, op, flags, segflg, namep, vp, td)		\
165224810Sjonathan	NDINIT_ALL(ndp, op, flags, segflg, namep, AT_FDCWD, vp, 0, td)
166177785Skib
167255219Spjdvoid NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags,
168255219Spjd    enum uio_seg segflg, const char *namep, int dirfd, struct vnode *startdir,
169255219Spjd    cap_rights_t *rightsp, struct thread *td);
17054655Seivind
17154655Seivind#define NDF_NO_DVP_RELE		0x00000001
17254655Seivind#define NDF_NO_DVP_UNLOCK	0x00000002
17354655Seivind#define NDF_NO_DVP_PUT		0x00000003
17454655Seivind#define NDF_NO_VP_RELE		0x00000004
17554655Seivind#define NDF_NO_VP_UNLOCK	0x00000008
17654655Seivind#define NDF_NO_VP_PUT		0x0000000c
17754655Seivind#define NDF_NO_STARTDIR_RELE	0x00000010
17854655Seivind#define NDF_NO_FREE_PNBUF	0x00000020
17954655Seivind#define NDF_ONLY_PNBUF		(~NDF_NO_FREE_PNBUF)
18054655Seivind
181118607Sjhbvoid NDFREE(struct nameidata *, const u_int);
18254655Seivind
18392719Salfredint	namei(struct nameidata *ndp);
18492719Salfredint	lookup(struct nameidata *ndp);
18592719Salfredint	relookup(struct vnode *dvp, struct vnode **vpp,
18692719Salfred	    struct componentname *cnp);
1871541Srgrimes#endif
1881541Srgrimes
1891541Srgrimes/*
1901541Srgrimes * Stats on usefulness of namei caches.
1911541Srgrimes */
19283045Sobrienstruct nchstats {
1931541Srgrimes	long	ncs_goodhits;		/* hits that we can really use */
1941541Srgrimes	long	ncs_neghits;		/* negative hits that we can use */
1951541Srgrimes	long	ncs_badhits;		/* hits we must drop */
1961541Srgrimes	long	ncs_falsehits;		/* hits with id mismatch */
1971541Srgrimes	long	ncs_miss;		/* misses */
1981541Srgrimes	long	ncs_long;		/* long names that ignore cache */
1991541Srgrimes	long	ncs_pass2;		/* names found with passes == 2 */
2001541Srgrimes	long	ncs_2passes;		/* number of times we attempt it */
2011541Srgrimes};
2029759Sbde
2039759Sbdeextern struct nchstats nchstats;
2049759Sbde
2051541Srgrimes#endif /* !_SYS_NAMEI_H_ */
206