1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
.\"
.\" Copyright (c) 1998, 1999 Eivind Eklund
.\" Copyright (c) 2003 Hiten M. Pandya
.\" Copyright (c) 2005 Robert N. M. Watson
.\"
.\" All rights reserved.
.\"
.\" This program is free software.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\"
.\" If you integrate this manpage in another OS, I'd appreciate a note
.\" - eivind@FreeBSD.org
.\"
.Dd September 30, 2025
.Dt NAMEI 9
.Os
.Sh NAME
.Nm namei ,
.Nm NDINIT ,
.Nm NDINIT_AT ,
.Nm NDFREE_PNBUF
.Nd pathname translation and lookup operations
.Sh SYNOPSIS
.In sys/param.h
.In sys/fcntl.h
.In sys/namei.h
.Ft int
.Fn namei "struct nameidata *ndp"
.Ft void
.Fo NDINIT
.Fa "struct nameidata *ndp" "enum nameiop op" "u_int64_t flags"
.Fa "enum uio_seg segflg" "const char *namep"
.Fc
.Ft void
.Fo NDINIT_AT
.Fa "struct nameidata *ndp" "enum nameiop op" "u_int64_t flags"
.Fa "enum uio_seg segflg" "const char *namep" "int dirfd"
.Fc
.Ft void
.Fn NDFREE_PNBUF "struct nameidata *ndp"
.Sh DESCRIPTION
The
.Nm
facility allows the client to perform pathname translation and lookup
operations.
The
.Nm
functions will increment the reference count for the vnode in question.
The reference count has to be decremented after use of the vnode, by
using either
.Xr vrele 9
or
.Xr vput 9 ,
depending on whether the
.Dv LOCKLEAF
flag was specified or not.
.Pp
The
.Fn NDINIT
macro is used to initialize
.Nm
components.
It takes the following arguments:
.Bl -tag -width ".Fa segflg"
.It Fa ndp
A pointer to the
.Vt "struct nameidata"
to initialize.
.It Fa op
The operation which
.Fn namei
will perform.
The following operations are valid:
.Dv LOOKUP , CREATE , DELETE ,
and
.Dv RENAME .
The latter three are just setup for those
effects; just calling
.Fn namei
will not result in
.Fn VOP_RENAME
being called.
.It Fa flags
Operation flags, described in the next section.
Several of these can be effective at the same time.
.It Fa segflg
UIO segment indicator.
This indicates if the name of the object is in userspace
.Pq Dv UIO_USERSPACE
or in the kernel address space
.Pq Dv UIO_SYSSPACE .
.It Fa namep
Pointer to the component's pathname buffer
(the file or directory name that will be looked up).
.El
.Pp
The
.Fn NDINIT_AT
macro is similar to
.Fn NDINIT ,
but takes one extra argument:
.Bl -tag -width ".Fa segflg"
.It Fa dirfd
File descriptor referencing a directory, or the special value
.Dv AT_FDCWD
meaning the calling thread's current working directory.
Lookups will be performed relative to this directory.
.El
.Pp
The
.Fn NDFREE_PNBUF
macro is used to free the pathname buffer.
It must be called exactly once for each successful
.Fn namei
call.
It takes the following argument:
.Bl -tag -width ".Fa segflg"
.It Fa ndp
A pointer to a
.Vt "struct nameidata"
that was used in a successful
.Fn namei
call.
.El
.Sh NAMEI OPERATION FLAGS
The
.Fn namei
function takes the following set of
.Dq "operation flags"
that influence its operation:
.Bl -tag -width NC_KEEPPOSENTRY
.It Dv NC_NOMAKEENTRY
An alias for
.Dv NOCACHE .
.It Dv NC_KEEPPOSENTRY
Keep the positive-caching entry in cache.
This flag is typically combined with
.Dv NOCACHE
to not cache a new entry,
but keep existing one, or with
.Dv MAKEENTRY .
.It Dv NOCACHE
Avoid
.Fn namei
creating this entry in the namecache if it is not
already present.
Normally,
.Fn namei
will add entries to the name cache
if they are not already there.
.It Dv LOCKLEAF
Lock vnode on return with
.Dv LK_EXCLUSIVE
unless
.Dv LOCKSHARED
is also set.
.Xr VOP_UNLOCK 9
should be used
to release the lock (or
.Xr vput 9
which is equivalent to calling
.Xr VOP_UNLOCK 9
followed by
.Xr vrele 9 ,
all in one).
.It Dv LOCKPARENT
This flag lets the
.Fn namei
function return the parent (directory) vnode,
.Va ni_dvp
in locked state, unless it is identical to
.Va ni_vp ,
in which case
.Va ni_dvp
is not locked per se (but may be locked due to
.Dv LOCKLEAF ) .
If a lock is enforced, it should be released using
.Xr vput 9
or
.Xr VOP_UNLOCK 9
and
.Xr vrele 9 .
.It Dv WANTPARENT
This flag allows the
.Fn namei
function to return the parent (directory) vnode in an unlocked state.
The parent vnode must be released separately by using
.Xr vrele 9 .
.It Dv FAILIFEXISTS
Makes the
.Nm
operation fail if the target exists.
It requires that the
.Dv LOCKPARENT
flag is set and
.Dv LOCKLEAF
is not.
.It Dv FOLLOW
With this flag,
.Fn 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 for the link itself).
.It Dv EMPTYPATH
For
.Nm
call initialized with
.Fn NDINIT_AT ,
allow the
.Fa namep
path to be empty.
In this case, the
.Fa dirfd
file descriptor may reference a file of arbitrary type, not
necessary a directory, and lookup returns the vnode for this file.
.It Dv LOCKSHARED
Lock vnode on return with
.Dv LK_SHARED ,
if permitted by the file system that owns the vnode.
The file system must explicitly permit this by setting
.Dv MNTK_LOOKUP_SHARED
in
.Dv mp->mnt_kern_flag
during mount and by calling
.Fn VN_LOCK_ASHARE
when allocating the vnode.
If
.Dv LOCKLEAF
is specified but shared locking is not permitted, then the vnode will be
returned with
.Dv LK_EXCLUSIVE .
.Xr VOP_UNLOCK 9
should be used
to release the lock (or
.Xr vput 9
which is equivalent to calling
.Xr VOP_UNLOCK 9
followed by
.Xr vrele 9 ,
all in one).
.It Dv NOFOLLOW
Do not follow symbolic links (pseudo).
This flag is not looked for by the actual code, which looks for
.Dv FOLLOW .
.Dv NOFOLLOW
is used to indicate to the source code reader that symlinks
are intentionally not followed.
.It Dv RBENEATH
Requires that
.Nm
did not cross the
.Fa dirfd
directory.
The flag is used to implement
.Dv O_RESOLVE_BENEATH
flag for
.Xr openat 2 .
.It Dv NAMEILOOKUP
The component is embedded in a
.Nm
lookup structure, and the
.Fn vfs_lookup_nameidata
function can be used to obtain that structure.
This can be useful in
.Xr VOP_LOOKUP 9
implementations which need to obtain extra lookup metadata.
.El
.Sh PARAMETERS DESCRIPTORS FLAGS
These flags are used for several purposes.
Some of them affects the global
.Nm
operation, some provide information for processing of the specific
path element, for instance, by the
.Dv VOP_LOOKUP
implementation of the involved filesystem.
.Bl -tag -width IGNOREWHITEOUT
.It Dv RDONLY
Specifies that the lookup should act as if the final node is located on
read-only mount.
The flag is typically used by file servers, e.g. NFS,
to handle read-only exports.
.It Dv ISRESTARTED
The
.Nm
was restarted with
.Fn NDRESTART .
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 different root, once.
.It Dv IGNOREWHITEOUT
Ignore whiteouts, e.g. when checking if a directory is empty.
.It Dv ISWHITEOUT
The result of lookup is whiteout.
.It Dv DOWHITEOUT
Handle whiteouts, the instruction for the
.Fn VOP_LOOKUP
filesystem methods.
.It Dv WILLBEDIR
The lookup is done for creating a new entry that will be directory.
It allows the trailing slash in the path string.
.It Dv ISOPEN
The caller is the code that opens a file.
This allows to weaken the lock mode of the return vnode, if the
mount point indicates extended shared lock support.
.It Dv NOCROSSMOUNT
Do not cross mount points during lookup.
.Pp
For
.Dq ..
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.
.Pp
For other lookups passing over mount, do not jump into the mounted
filesystem.
This allows to descend into the file hierarchy otherwise shadowed
by the mount point.
.It Dv NOMACCHECK
Do not perform MAC checks during lookup.
.It Dv AUDITVNODE1
Audit the looked up vnode information, use the first slot for audit information.
.It Dv AUDITVNODE2
Same as
.Dv AUDITVNODE1
but use the second slot.
.It Dv NOCAPCHECK
Do not perform capability checks.
If the calling process is in capability mode, lookup is denied outright.
.It Dv OPENREAD
The lookup was for open and file will be opened for read.
.It Dv OPENWRITE
The lookup was for open and file will be opened for write.
.It Dv WANTIOCTLCAPS
Leave ioctl caps for the caller.
See the description of
.Nm
results.
.It Dv OPENNAMED
Opening a named attribute (directory).
.It Dv NOEXECCHECK
Do not perform check for allowed execution on the starting directory.
It is used to implement the POSIX-required semantic for
.Xr openat 2
lookups that must use the permissions from time the directory was
opened, and not when used for lookup.
.It Dv MAKEENTRY
Looked-up entry is to be added to name cache.
.It Dv ISSYMLINK
Current component is symlink, and it needs the interpretation
according to the
.Dv FOLLOW
or
.Dv NOFOLLOW
flags.
.It Dv ISLASTCN
This is last component of pathname.
It is handled specially, many flags augment its processing.
.It Dv ISDOTDOT
Current component name is
.Dq .. .
Usually implies a need to specially handle the vnode locking
for instantiation of the target vnode.
The generic
.Fn vn_vget_ino_gen
function and its more specialized variant
.Fn vn_vget_ino
might be useful.
.It Dv TRAILINGSLASH
Path ended in a slash.
.It Dv CREATENAMED
Create a named attribute dir.
.El
.Sh ALLOCATED ELEMENTS
The
.Vt nameidata
structure is composed of the following fields:
.Bl -tag -width ".Va ni_cnd.cn_pnbuf"
.It Va ni_startdir
In the normal case, this is either the current directory or the root.
It is the current directory if the name passed in does not start with
.Ql /
and we have not gone through any symlinks with an absolute path, and
the root otherwise.
.Pp
In this case, it is only used by
.Fn vfs_lookup ,
and should not be
considered valid after a call to
.Fn namei .
.It Va ni_dvp
Vnode pointer to directory of the object on which lookup is performed.
This is available on successful return if
.Dv LOCKPARENT
or
.Dv WANTPARENT
is set.
It is locked if
.Dv LOCKPARENT
is set.
.It Va ni_vp
Vnode pointer to the resulting object,
.Dv NULL
otherwise.
The
.Va v_usecount
field of this vnode is incremented.
If
.Dv LOCKLEAF
is set, it is also locked.
.Pp
.It Va ni_cnd.cn_pnbuf
The pathname buffer contains the location of the file or directory
that will be used by the
.Nm
operations.
It is managed by the
.Xr uma 9
zone allocation interface.
.El
.Sh RESULTS
The
.Vt struct namei
member
.Dv ni_resflags
returns the following flags giving some details of the successful operation:
.Bl -tag -width NIRES_EMPTYPATH
.It Dv NIRES_ABS
The path passed was absolute.
.It Dv NIRES_STRICTREL
Restricted lookup result.
Only relative lookups were done to resolve the path to vnode.
.It Dv NIRES_EMPTYPATH
The
.Dv EMPTYPATH
flag was provided and used.
In particular, the passed path was empty.
.El
.Pp
If the
.Dv WANTIOCTLCAPS
flag was specified, on return the
.Va ni_filecaps
member of the
.Vt struct namei
contains the capabilities of the file descriptor used as
the lookup starting point
.Pq Va dirfd .
.Sh RETURN VALUES
If successful,
.Fn namei
will return 0, otherwise it will return an error.
.Sh FILES
.Bl -tag -width Pa
.It Pa src/sys/kern/vfs_lookup.c
.El
.Sh EXAMPLES
Assuming the
.Dv path
variable contains a pointer to userspace path string, the following
example looks up the file named by it, and performs required error
and resource handling:
.Bd -literal
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
.Ed
.Sh ERRORS
Errors which
.Fn namei
may return:
.Bl -tag -width Er
.It Bq Er ENOTDIR
A component of the specified pathname is not a directory when a directory is
expected.
.It Bq Er ENAMETOOLONG
A component of a pathname exceeded 255 characters,
or an entire pathname exceeded 1023 characters.
.It Bq Er ENOENT
A component of the specified pathname does not exist,
or the pathname is an empty string.
.It Bq Er EACCES
An attempt is made to access a file in a way forbidden by its file access
permissions.
.It Bq Er ELOOP
Too many symbolic links were encountered in translating the pathname.
.It Bq Er EISDIR
An attempt is made to open a directory with write mode specified.
.It Bq Er EINVAL
The last component of the pathname specified for a
.Dv DELETE
or
.Dv RENAME
operation is
.Ql \&. .
.It Bq Er EROFS
An attempt is made to modify a file or directory on a read-only file system.
.El
.Sh SEE ALSO
.Xr uio 9 ,
.Xr uma 9 ,
.Xr VFS 9 ,
.Xr vnode 9 ,
.Xr vput 9 ,
.Xr vref 9 ,
.Xr vrele 9
.Sh AUTHORS
.An -nosplit
This manual page was written by
.An Eivind Eklund Aq Mt eivind@FreeBSD.org
and later significantly revised by
.An Hiten M. Pandya Aq Mt hmp@FreeBSD.org .
.Sh BUGS
The
.Dv LOCKPARENT
flag does not always result in the parent vnode being locked.
This results in complications when the
.Dv LOCKPARENT
is used.
In order to solve this for the cases where both
.Dv LOCKPARENT
and
.Dv LOCKLEAF
are used, it is necessary to resort to recursive locking.
|