1.\" 2.\" Copyright (c) 1998, 1999 Eivind Eklund 3.\" Copyright (c) 2003 Hiten M. Pandya 4.\" Copyright (c) 2005 Robert N. M. Watson 5.\" 6.\" All rights reserved. 7.\" 8.\" This program is free software. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 20.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 23.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29.\" 30.\" 31.\" If you integrate this manpage in another OS, I'd appreciate a note 32.\" - eivind@FreeBSD.org 33.\" 34.Dd December 17, 2024 35.Dt NAMEI 9 36.Os 37.Sh NAME 38.Nm namei , 39.Nm NDINIT , 40.Nm NDFREE 41.Nd pathname translation and lookup operations 42.Sh SYNOPSIS 43.In sys/param.h 44.In sys/fcntl.h 45.In sys/namei.h 46.Ft int 47.Fn namei "struct nameidata *ndp" 48.Ft void 49.Fo NDINIT 50.Fa "struct nameidata *ndp" "u_long op" "u_long flags" 51.Fa "enum uio_seg segflg" "const char *namep" "struct thread *td" 52.Fc 53.Ft void 54.Fn NDFREE "struct nameidata *ndp" "const uint flags" 55.Sh DESCRIPTION 56The 57.Nm 58facility allows the client to perform pathname translation and lookup 59operations. 60The 61.Nm 62functions will increment the reference count for the vnode in question. 63The reference count has to be decremented after use of the vnode, by 64using either 65.Xr vrele 9 66or 67.Xr vput 9 , 68depending on whether the 69.Dv LOCKLEAF 70flag was specified or not. 71.Pp 72The 73.Fn NDINIT 74function is used to initialize 75.Nm 76components. 77It takes the following arguments: 78.Bl -tag -width ".Fa segflg" 79.It Fa ndp 80The 81.Vt "struct nameidata" 82to initialize. 83.It Fa op 84The operation which 85.Fn namei 86will perform. 87The following operations are valid: 88.Dv LOOKUP , CREATE , DELETE , 89and 90.Dv RENAME . 91The latter three are just setup for those 92effects; just calling 93.Fn namei 94will not result in 95.Fn VOP_RENAME 96being called. 97.It Fa flags 98Operation flags. 99Several of these can be effective at the same time. 100.It Fa segflg 101UIO segment indicator. 102This indicates if the name of the object is in userspace 103.Pq Dv UIO_USERSPACE 104or in the kernel address space 105.Pq Dv UIO_SYSSPACE . 106.It Fa namep 107Pointer to the component's pathname buffer 108(the file or directory name that will be looked up). 109.It Fa td 110The thread context to use for 111.Nm 112operations and locks. 113.El 114.Sh NAMEI OPERATION FLAGS 115The 116.Fn namei 117function takes the following set of 118.Dq "operation flags" 119that influence its operation: 120.Bl -tag -width ".Dv WANTPARENT" 121.It Dv LOCKLEAF 122Lock vnode on return with 123.Dv LK_EXCLUSIVE 124unless 125.Dv LOCKSHARED 126is also set. 127.Xr VOP_UNLOCK 9 128should be used 129to release the lock (or 130.Xr vput 9 131which is equivalent to calling 132.Xr VOP_UNLOCK 9 133followed by 134.Xr vrele 9 , 135all in one). 136.It Dv LOCKPARENT 137This flag lets the 138.Fn namei 139function return the parent (directory) vnode, 140.Va ni_dvp 141in locked state, unless it is identical to 142.Va ni_vp , 143in which case 144.Va ni_dvp 145is not locked per se (but may be locked due to 146.Dv LOCKLEAF ) . 147If a lock is enforced, it should be released using 148.Xr vput 9 149or 150.Xr VOP_UNLOCK 9 151and 152.Xr vrele 9 . 153.It Dv LOCKSHARED 154Lock vnode on return with 155.Dv LK_SHARED , 156if permitted by the file system that owns the vnode. 157The file system must explicitly permit this by setting 158.Dv MNTK_LOOKUP_SHARED 159in 160.Dv mp->mnt_kern_flag 161during mount and by calling 162.Fn VN_LOCK_ASHARE 163when allocating the vnode. 164If 165.Dv LOCKLEAF 166is specified but shared locking is not permitted, then the vnode will be 167returned with 168.Dv LK_EXCLUSIVE . 169.Xr VOP_UNLOCK 9 170should be used 171to release the lock (or 172.Xr vput 9 173which is equivalent to calling 174.Xr VOP_UNLOCK 9 175followed by 176.Xr vrele 9 , 177all in one). 178.It Dv WANTPARENT 179This flag allows the 180.Fn namei 181function to return the parent (directory) vnode in an unlocked state. 182The parent vnode must be released separately by using 183.Xr vrele 9 . 184.It Dv NOCACHE 185Avoid 186.Fn namei 187creating this entry in the namecache if it is not 188already present. 189Normally, 190.Fn namei 191will add entries to the name cache 192if they are not already there. 193.It Dv FOLLOW 194With this flag, 195.Fn namei 196will follow the symbolic link if the last part 197of the path supplied is a symbolic link (i.e., it will return a vnode 198for whatever the link points at, instead for the link itself). 199.It Dv NOFOLLOW 200Do not follow symbolic links (pseudo). 201This flag is not looked for by the actual code, which looks for 202.Dv FOLLOW . 203.Dv NOFOLLOW 204is used to indicate to the source code reader that symlinks 205are intentionally not followed. 206.It Dv SAVENAME 207Do not free the pathname buffer at the end of the 208.Fn namei 209invocation; instead, free it later in 210.Fn NDFREE 211so that the caller may access the pathname buffer. 212See below for details. 213.It Dv SAVESTART 214Retain an additional reference to the parent directory; do not free 215the pathname buffer. 216See below for details. 217.El 218.Sh ALLOCATED ELEMENTS 219The 220.Vt nameidata 221structure is composed of the following fields: 222.Bl -tag -width ".Va ni_cnd.cn_pnbuf" 223.It Va ni_startdir 224In the normal case, this is either the current directory or the root. 225It is the current directory if the name passed in does not start with 226.Ql / 227and we have not gone through any symlinks with an absolute path, and 228the root otherwise. 229.Pp 230In this case, it is only used by 231.Fn vfs_lookup , 232and should not be 233considered valid after a call to 234.Fn namei . 235.It Va ni_dvp 236Vnode pointer to directory of the object on which lookup is performed. 237This is available on successful return if 238.Dv LOCKPARENT 239or 240.Dv WANTPARENT 241is set. 242It is locked if 243.Dv LOCKPARENT 244is set. 245.It Va ni_vp 246Vnode pointer to the resulting object, 247.Dv NULL 248otherwise. 249The 250.Va v_usecount 251field of this vnode is incremented. 252If 253.Dv LOCKLEAF 254is set, it is also locked. 255.Pp 256.It Va ni_cnd.cn_pnbuf 257The pathname buffer contains the location of the file or directory 258that will be used by the 259.Nm 260operations. 261It is managed by the 262.Xr uma 9 263zone allocation interface. 264.El 265.Sh RETURN VALUES 266If successful, 267.Fn namei 268will return 0, otherwise it will return an error. 269.Sh FILES 270.Bl -tag -width Pa 271.It Pa src/sys/kern/vfs_lookup.c 272.El 273.Sh ERRORS 274Errors which 275.Fn namei 276may return: 277.Bl -tag -width Er 278.It Bq Er ENOTDIR 279A component of the specified pathname is not a directory when a directory is 280expected. 281.It Bq Er ENAMETOOLONG 282A component of a pathname exceeded 255 characters, 283or an entire pathname exceeded 1023 characters. 284.It Bq Er ENOENT 285A component of the specified pathname does not exist, 286or the pathname is an empty string. 287.It Bq Er EACCES 288An attempt is made to access a file in a way forbidden by its file access 289permissions. 290.It Bq Er ELOOP 291Too many symbolic links were encountered in translating the pathname. 292.It Bq Er EISDIR 293An attempt is made to open a directory with write mode specified. 294.It Bq Er EINVAL 295The last component of the pathname specified for a 296.Dv DELETE 297or 298.Dv RENAME 299operation is 300.Ql \&. . 301.It Bq Er EROFS 302An attempt is made to modify a file or directory on a read-only file system. 303.El 304.Sh SEE ALSO 305.Xr uio 9 , 306.Xr uma 9 , 307.Xr VFS 9 , 308.Xr vnode 9 , 309.Xr vput 9 , 310.Xr vref 9 311.Sh AUTHORS 312.An -nosplit 313This manual page was written by 314.An Eivind Eklund Aq Mt eivind@FreeBSD.org 315and later significantly revised by 316.An Hiten M. Pandya Aq Mt hmp@FreeBSD.org . 317.Sh BUGS 318The 319.Dv LOCKPARENT 320flag does not always result in the parent vnode being locked. 321This results in complications when the 322.Dv LOCKPARENT 323is used. 324In order to solve this for the cases where both 325.Dv LOCKPARENT 326and 327.Dv LOCKLEAF 328are used, it is necessary to resort to recursive locking. 329