NAMEI(9)
π NAME π
okay bestie, welcome to NAMEI(9) from the Kernel Developer's Manual β¨ this whole page is about namei, NDINIT, NDINIT_AT, and NDFREE_PNBUF β the pathname translation and lookup operations squad π«Ά
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/namei.h>
int
namei(struct nameidata *ndp);
void
NDINIT(struct nameidata *ndp, enum nameiop op, u_int64_t flags,
enum uio_seg segflg, const char *namep);
void
NDINIT_AT(struct nameidata *ndp, enum nameiop op, u_int64_t flags,
enum uio_seg segflg, const char *namep, int dirfd);
void
NDFREE_PNBUF(struct nameidata *ndp);
πΈ DESCRIPTION πΈ
so the namei facility is what lets a client do pathname translation and lookup operations π
the namei functions will increment the reference count for the vnode in question. after youβre done using that vnode, the refcount has to be decremented β you do that with either vrele(9) or vput(9), depending on whether the LOCKLEAF flag was specified or not.
π¨ bestie please donβt forget the vnode refcount cleanup. this is one of those βthe vibes are fine but the kernel will absolutely noticeβ situations π
the NDINIT() macro is used to initialize namei components. it takes these arguments:
ndpβ a pointer to thestruct nameidatayouβre initializing.opβ the operation thatnamei()will perform. valid operations are:LOOKUP,CREATE,DELETE, andRENAME. the latter three are just setup for those effects; just callingnamei()will not result inVOP_RENAME()being called.flagsβ operation flags, described in the next section. several of these can be effective at the same time.segflgβ UIO segment indicator. this says whether the name of the object is in userspace (UIO_USERSPACE) or in the kernel address space (UIO_SYSSPACE).namepβ pointer to the componentβs pathname buffer (the file or directory name that will be looked up).
the NDINIT_AT() macro is similar to NDINIT(), but it takes one extra argument:
dirfdβ file descriptor referencing a directory, or the special valueAT_FDCWD, which means the calling threadβs current working directory. lookups will be performed relative to this directory.
the NDFREE_PNBUF() macro is used to free the pathname buffer. it must be called exactly once for each successful namei() call. it takes this argument:
ndpβ a pointer to astruct nameidatathat was used in a successfulnamei()call.
π NAMEI OPERATION FLAGS π
the namei() function takes this set of βoperation flagsβ that influence how it operates:
NC_NOMAKEENTRYβ an alias forNOCACHE.NC_KEEPPOSENTRYβ keep the positive-caching entry in cache. this flag is typically combined withNOCACHEso it does not cache a new entry but keeps the existing one, or combined withMAKEENTRY.NOCACHEβ avoidnamei()creating this entry in the namecache if it is not already present. normally,namei()will add entries to the name cache if they are not already there.LOCKLEAFβ lock vnode on return withLK_EXCLUSIVEunlessLOCKSHAREDis also set.VOP_UNLOCK(9)should be used to release the lock (orvput(9), which is equivalent to callingVOP_UNLOCK(9)followed byvrele(9), all in one).LOCKPARENTβ this flag lets thenamei()function return the parent (directory) vnode,ni_dvp, in locked state, unless it is identical toni_vp, in which caseni_dvpis not locked per se (but may be locked due toLOCKLEAF). if a lock is enforced, it should be released usingvput(9)orVOP_UNLOCK(9)andvrele(9).WANTPARENTβ this flag allows thenamei()function to return the parent (directory) vnode in an unlocked state. the parent vnode must be released separately by usingvrele(9).FAILIFEXISTSβ makes the namei operation fail if the target exists. it requires that theLOCKPARENTflag is set andLOCKLEAFis not.FOLLOWβ with this flag,namei()will follow the symbolic link if the last part of the path supplied is a symbolic link (i.e., it will return a vnode for whatever the link points at, instead of the link itself).EMPTYPATHβ for anameicall initialized withNDINIT_AT(), allow thenameppath to be empty. in this case, thedirfdfile descriptor may reference a file of arbitrary type, not necessarily a directory, and lookup returns the vnode for this file.LOCKSHAREDβ lock vnode on return withLK_SHARED, if permitted by the file system that owns the vnode. the file system must explicitly permit this by settingMNTK_LOOKUP_SHAREDinmp->mnt_kern_flagduring mount and by callingVN_LOCK_ASHARE()when allocating the vnode. ifLOCKLEAFis specified but shared locking is not permitted, then the vnode will be returned withLK_EXCLUSIVE.VOP_UNLOCK(9)should be used to release the lock (orvput(9), which is equivalent to callingVOP_UNLOCK(9)followed byvrele(9), all in one).NOFOLLOWβ do not follow symbolic links (pseudo). this flag is not looked for by the actual code, which looks forFOLLOW.NOFOLLOWis used to indicate to the source code reader that symlinks are intentionally not followed.RBENEATHβ requires thatnameidid not cross thedirfddirectory. the flag is used to implement theO_RESOLVE_BENEATHflag foropenat(2).NAMEILOOKUPβ the component is embedded in a namei lookup structure, and thevfs_lookup_nameidata()function can be used to obtain that structure. this can be useful inVOP_LOOKUP(9)implementations which need to obtain extra lookup metadata.
π§ PARAMETERS DESCRIPTORS FLAGS π§
these flags are used for several purposes. some of them affect the global namei operation, and some provide information for processing of the specific path element β for instance, by the VOP_LOOKUP implementation of the involved filesystem π
RDONLYβ specifies that the lookup should act as if the final node is located on a read-only mount. the flag is typically used by file servers, e.g. NFS, to handle read-only exports.ISRESTARTEDβ the namei was restarted withNDRESTART(). this is used internally for double-root lookups used by ABI subsystems, after the lookup with native root failed. the components are reset to the original values, and lookup is repeated with a different root, once.IGNOREWHITEOUTβ ignore whiteouts, e.g. when checking if a directory is empty.ISWHITEOUTβ the result of lookup is whiteout.DOWHITEOUTβ handle whiteouts, the instruction for theVOP_LOOKUP()filesystem methods.WILLBEDIRβ the lookup is done for creating a new entry that will be a directory. it allows the trailing slash in the path string.ISOPENβ the caller is the code that opens a file. this allows weakening the lock mode of the returned vnode, if the mount point indicates extended shared lock support.NOCROSSMOUNTβ do not cross mount points during lookup.- For
..lookup leading to mount root, returns the root vnode of the mount instead of the covered vnode of the filesystem where the mount was placed. - For other lookups passing over mount, do not jump into the mounted filesystem. this allows descending into the file hierarchy otherwise shadowed by the mount point.
- For
NOMACCHECKβ do not perform MAC checks during lookup.AUDITVNODE1β audit the looked up vnode information, use the first slot for audit information.AUDITVNODE2β same asAUDITVNODE1but use the second slot.NOCAPCHECKβ do not perform capability checks. if the calling process is in capability mode, lookup is denied outright.OPENREADβ the lookup was for open and the file will be opened for read.OPENWRITEβ the lookup was for open and the file will be opened for write.WANTIOCTLCAPSβ leave ioctl caps for the caller. see the description of namei results.OPENNAMEDβ opening a named attribute (directory).NOEXECCHECKβ do not perform check for allowed execution on the starting directory. it is used to implement the POSIX-required semantic foropenat(2)lookups that must use the permissions from the time the directory was opened, and not when used for lookup.MAKEENTRYβ looked-up entry is to be added to name cache.ISSYMLINKβ current component is symlink, and it needs the interpretation according to theFOLLOWorNOFOLLOWflags.ISLASTCNβ this is the last component of pathname. it is handled specially, and many flags augment its processing.ISDOTDOTβ current component name is... usually implies a need to specially handle the vnode locking for instantiation of the target vnode. the genericvn_vget_ino_gen()function and its more specialized variantvn_vget_ino()might be useful.TRAILINGSLASHβ path ended in a slash.CREATENAMEDβ create a named attribute dir.
β¨ ALLOCATED ELEMENTS β¨
okay so the nameidata structure is literally a little bundle of fields, and here's the whole squad, bestie:
ni_startdirβ in the normal case this is either the current directory or the root, periodt. It's the current directory if the name you passed in does not start with/and we haven't gone through any symlinks with an absolute path β and it's the root otherwise. π In this case it's only used byvfs_lookup(), and you should not consider it valid after a call tonamei(). like, it's done, she's retired, let her go.ni_dvpβ the vnode pointer to the directory of the object on which lookup is performed. This one's available on successful return ifLOCKPARENTorWANTPARENTis set. And it is locked ifLOCKPARENTis set. πni_vpβ vnode pointer to the resulting object,NULLotherwise. Thev_usecountfield of this vnode is incremented. And ifLOCKLEAFis set, it's also locked. iconic behavior honestly.ni_cnd.cn_pnbufβ the pathname buffer, bestie. It contains the location of the file or directory that will be used by the namei operations. It's managed by theuma(9)zone allocation interface. π¦
π RESULTS π
the struct namei member ni_resflags returns the following flags, which give you some details about the successful operation (yes, it's "succesfull" in the original, she's quirky like that π):
NIRES_ABSβ the path passed was absolute.NIRES_STRICTRELβ restricted lookup result. Only relative lookups were done to resolve the path to vnode.NIRES_EMPTYPATHβ theEMPTYPATHflag was provided and used. In particular, the passed path was empty. π
and if the WANTIOCTLCAPS flag was specified, then on return the ni_filecaps member of the struct namei contains the capabilities of the file descriptor used as the lookup starting point (dirfd). π
π RETURN VALUES π
if it slays, namei() returns 0. otherwise it returns an error. that's it, that's the whole vibe check. β¨
π FILES π
src/sys/kern/vfs_lookup.c
π¦ EXAMPLES π¦
so let's say your path variable contains a pointer to a userspace path string β the following example looks up the file named by it, and does all the required error and resource handling. like a responsible main character. π
char *path;
struct nameidata nd;
int error;
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
path);
if ((error = namei(&nd)) != 0)
return (error);
NDFREE_PNBUF(&nd);
... use nd.ni_vp vnode
π ERRORS π
the errors namei() may return, no cap:
[ENOTDIR]β a component of the specified pathname is not a directory when a directory is expected.[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire pathname exceeded 1023 characters. that's a lot of characters, babe.[ENOENT]β a component of the specified pathname does not exist, or the pathname is an empty string.[EACCES]β an attempt is made to access a file in a way forbidden by its file access permissions. rude.[ELOOP]β too many symbolic links were encountered in translating the pathname. she went in circles bestie πΈ[EISDIR]β an attempt is made to open a directory with write mode specified.[EINVAL]β the last component of the pathname specified for aDELETEorRENAMEoperation is..[EROFS]β an attempt is made to modify a file or directory on a read-only file system.
π«Ά SEE ALSO π«Ά
uio(9), uma(9), VFS(9), vnode(9), vput(9), vref(9), vrele(9)
like the whole friend group, go say hi π
π AUTHORS π
this manual page was written by Eivind Eklund <eivind@FreeBSD.org> and later significantly revised by Hiten M. Pandya <hmp@FreeBSD.org>. absolute legends, we stan. π
π BUGS π
heads up bestie, this one's real: theLOCKPARENTflag does not always result in the parent vnode being locked. this results in complications whenLOCKPARENTis used. and in order to solve this for the cases where bothLOCKPARENTandLOCKLEAFare used, it is necessary to resort to recursive locking. so like... be careful, okay? not me crying about vnode locking π
aaaand that's a wrap on the whole page, bestie!! π this one's from Debian, dated September 30, 2025, and honestly the way she ate... mwah π β¨ go forth and look up paths responsibly π¦π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1998, 1999 Eivind Eklund
- Copyright (c) 2003 Hiten M. Pandya
- Copyright (c) 2005 Robert N. M. Watson
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms