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 β€” the struct nameidata you're initializing. the main character of the operation, tbh.
  • op β€” the operation that namei() will perform. valid ones are: LOOKUP, CREATE, DELETE, and RENAME. okay but lowkey important: the latter three are just setup for those effects β€” just calling namei() will not result in VOP_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 β€” the UIO segment 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 for namei operations 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 with LK_EXCLUSIVE, unless LOCKSHARED is also set. you should use VOP_UNLOCK(9) to release the lock (or vput(9), which is equivalent to calling VOP_UNLOCK(9) followed by vrele(9), all in one β€” a combo meal, iconic). πŸ”₯
  • LOCKPARENT β€” this flag lets namei() return the parent (directory) vnode, ni_dvp, in locked state β€” unless it's identical to ni_vp, in which case ni_dvp is not locked per se (but may be locked due to LOCKLEAF, sneaky). if a lock is enforced, release it using vput(9) or VOP_UNLOCK(9) and vrele(9).
  • LOCKSHARED β€” lock the vnode on return with LK_SHARED. use VOP_UNLOCK(9) to release the lock (or vput(9), which is equivalent to calling VOP_UNLOCK(9) followed by vrele(9), all in one). same energy as LOCKLEAF but sharing is caring. 🫢
  • WANTPARENT β€” this flag allows namei() to return the parent (directory) vnode in an unlocked state. the parent vnode must be released separately by using vrele(9). separately!! don't forget it!! 😭
  • NOCACHE β€” avoid namei() creating this entry in the namecache if it's not already present. normally namei() 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 for FOLLOW. NOFOLLOW is 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 the namei() invocation; instead, free it later in NDFREE() 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 if LOCKPARENT or WANTPARENT is set. it is locked if LOCKPARENT is set. freeing this in NDFREE() can be inhibited by NDF_NO_DVP_RELE, NDF_NO_DVP_PUT, or NDF_NO_DVP_UNLOCK (with the obvious effects).
  • ni_vp β€” vnode pointer to the resulting object, NULL otherwise. the v_usecount field of this vnode is incremented. if LOCKLEAF is 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 the namei operations. it's managed by the uma(9) zone allocation interface. if the SAVESTART or SAVENAME flag is set, then the pathname buffer is available after calling the namei() 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 a DELETE or RENAME operation 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