NAMEI(9)
πΈ NAMEI(9) πΈ
okay bestie, gather round, because we're about to talk about NAMEI(9) from the Kernel Developer's Manual and honestly? it's giving backstage pass to the kernel. this is where pathname translation and lookup operations live. π
β¨ NAME β¨
namei, NDINIT, NDFREE β pathname translation and lookup operations π¦
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/namei.h>
int
namei(struct nameidata *ndp);
void
NDINIT(struct nameidata *ndp, u_long op, u_long flags,
enum uio_seg segflg, const char *namep, struct thread *td);
void
NDFREE(struct nameidata *ndp, const uint flags);
β¨ DESCRIPTION β¨
so the namei facility is what lets the client do pathname translation and lookup operations. and here's the thing you CANNOT forget, bestie: the namei functions increment the reference count for the vnode in question. which means you have to decrement that reference count after you're done with the vnode, using either vrele(9) or vput(9) β and which one you use depends on whether you specified the LOCKLEAF flag or not. no skipping this step, it's not optional, it's the rules. π€
the NDINIT() function is the one that initializes your namei components. it takes these arguments:
ndpβ thestruct nameidatayou're initializing. the main character of the operation, tbh.opβ the operation thatnamei()will perform. valid ones are:LOOKUP,CREATE,DELETE, andRENAME. okay but lowkey important: the latter three are just setup for those effects β just callingnamei()will not result inVOP_RENAME()being called. so don't come for me when your rename doesn't rename, it was never gonna, that's just the deal. πflagsβ operation flags. several of these can be effective at the same time, they stack, they're a whole squad. β¨segflgβ theUIOsegment indicator. this tells you whether the name of the object is in userspace (UIO_USERSPACE) or in the kernel address space (UIO_SYSSPACE). location check, basically. πnamepβ pointer to the component's pathname buffer (that's the file or directory name that's gonna be looked up).tdβ the thread context to use fornameioperations and locks.
β¨ NAMEI OPERATION FLAGS β¨
the namei() function takes this set of "operation flags" that influence how it operates. let's go through every single one because we are thorough in this house: π
LOCKLEAFβ lock the vnode on return withLK_EXCLUSIVE, unlessLOCKSHAREDis also set. you should useVOP_UNLOCK(9)to release the lock (orvput(9), which is equivalent to callingVOP_UNLOCK(9)followed byvrele(9), all in one β a combo meal, iconic). π₯LOCKPARENTβ this flag letsnamei()return the parent (directory) vnode,ni_dvp, in locked state β unless it's identical toni_vp, in which caseni_dvpis not locked per se (but may be locked due toLOCKLEAF, sneaky). if a lock is enforced, release it usingvput(9)orVOP_UNLOCK(9)andvrele(9).LOCKSHAREDβ lock the vnode on return withLK_SHARED. useVOP_UNLOCK(9)to release the lock (orvput(9), which is equivalent to callingVOP_UNLOCK(9)followed byvrele(9), all in one). same energy asLOCKLEAFbut sharing is caring. π«ΆWANTPARENTβ this flag allowsnamei()to return the parent (directory) vnode in an unlocked state. the parent vnode must be released separately by usingvrele(9). separately!! don't forget it!! πNOCACHEβ avoidnamei()creating this entry in the namecache if it's not already present. normallynamei()will add entries to the name cache if they're not already there, so this is the "no thank you, I'm good" option.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'll return a vnode for whatever the link points at, instead of for the link itself. going where the vibes point. π¦NOFOLLOWβ do not follow symbolic links (pseudo). and here's the tea: 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. it's a note-to-self, not a command. πSAVENAMEβ do not free the pathname buffer at the end of thenamei()invocation; instead, free it later inNDFREE()so that the caller may access the pathname buffer. see below for details.SAVESTARTβ retain an additional reference to the parent directory; do not free the pathname buffer. see below for details.
β¨ ALLOCATED ELEMENTS β¨
the nameidata structure is composed of these fields, let's meet the cast:
ni_startdirβ in the normal case, this is either the current directory or the root. it's the current directory if the name passed in does not start with/and we haven't gone through any symlinks with an absolute path, and the root otherwise. π
in this case, it's only used by lookup(), and should not be considered valid after a call to namei(). if SAVESTART is set, this is set to the same as ni_dvp, with an extra vref(9). to block NDFREE() from releasing ni_startdir, the NDF_NO_STARTDIR_RELE can be set.
ni_dvpβ vnode pointer to the directory of the object on which lookup is performed. this is available on successful return ifLOCKPARENTorWANTPARENTis set. it is locked ifLOCKPARENTis set. freeing this inNDFREE()can be inhibited byNDF_NO_DVP_RELE,NDF_NO_DVP_PUT, orNDF_NO_DVP_UNLOCK(with the obvious effects).ni_vpβ vnode pointer to the resulting object,NULLotherwise. thev_usecountfield of this vnode is incremented. ifLOCKLEAFis set, it's also locked. π
freeing this in NDFREE() can be inhibited by NDF_NO_VP_RELE, NDF_NO_VP_PUT, or NDF_NO_VP_UNLOCK (with the obvious effects).
ni_cnd.cn_pnbufβ the pathname buffer contains the location of the file or directory that will be used by thenameioperations. it's managed by theuma(9)zone allocation interface. if theSAVESTARTorSAVENAMEflag is set, then the pathname buffer is available after calling thenamei()function. β¨
to only deallocate resources used by the pathname buffer, ni_cnd.cn_pnbuf, then the NDF_ONLY_PNBUF flag can be passed to the NDFREE() function. to keep the pathname buffer intact, the NDF_NO_FREE_PNBUF flag can be passed to the NDFREE() function.
β¨ RETURN VALUES β¨
if successful, namei() will return 0, otherwise it'll return an error. simple as that, no plot twist. π
β¨ FILES β¨
src/sys/kern/vfs_lookup.c
β¨ ERRORS β¨
errors that namei() may return, aka the mood-ruiners:
[ENOTDIR]β a component of the specified pathname is not a directory when a directory is expected. rude of it.[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire pathname exceeded 1023 characters. bestie. shorten the name. π[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.[ELOOP]β too many symbolic links were encountered in translating the pathname. it's giving link labyrinth. π[EISDIR]β an attempt is made to open a directory with write mode specified.[EINVAL]β the last component of the pathname specified for aDELETEorRENAMEoperation is.. you can't do that, that's the whole directory, sit down.[EROFS]β an attempt is made to modify a file or directory on a read-only file system. it said no and it meant no. π«
β¨ SEE ALSO β¨
uio(9), uma(9), VFS(9), vnode(9), vput(9), vref(9) β the whole friend group, go read them, iykyk π«Ά
β¨ 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 β¨
okay so, the tea: the LOCKPARENT flag does not always result in the parent vnode being locked. that results in complications when LOCKPARENT is used. and in order to solve this for the cases where both LOCKPARENT and LOCKLEAF are used, it is necessary to resort to recursive locking. so like... be aware. be very aware. π
and that's the whole page, no cap β every flag, every field, every error, all of it, still intact, just with better vibes π
this page came to you from Debian, dated May 23, 2015 β mwah, love that for us β¨ππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- 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 on : a language model's rewrite of the original, under its terms