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
|
.\" $OpenBSD: namei.9,v 1.23 2023/07/15 23:01:25 kn Exp $
.\" $NetBSD: namei.9,v 1.9 2003/05/06 10:46:44 jmmv Exp $
.\"
.\" Copyright (c) 2001 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Gregory McGarry.
.\"
.\" 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``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 FOUNDATION OR CONTRIBUTORS
.\" 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.
.\"
.Dd $Mdocdate: July 15 2023 $
.Dt NAMEI 9
.Os
.Sh NAME
.Nm namei ,
.Nm vfs_lookup ,
.Nm vfs_relookup ,
.Nm NDINIT ,
.Nm NDINITAT
.Nd pathname lookup
.Sh SYNOPSIS
.In sys/param.h
.In sys/namei.h
.Ft int
.Fn namei "struct nameidata *ndp"
.Ft int
.Fn vfs_lookup "struct nameidata *ndp"
.Ft int
.Fn vfs_relookup "struct vnode *dvp" "struct vnode **vpp" \
"struct componentname *cnp"
.Ft void
.Fn NDINIT "struct nameidata *ndp" "u_long op" "u_long flags" \
"enum uio_seg segflg" "const char *namep" "struct proc *p"
.Ft void
.Fn NDINITAT "struct nameidata *ndp" "u_long op" "u_long flags" \
"enum uio_seg segflg" "int dirfd" "const char *namep" "struct proc *p"
.Sh DESCRIPTION
The
.Fn namei
function converts a pathname to a
.Xr vnode 9 .
It uses the following structure:
.Bd -literal
struct nameidata {
/*
* Arguments to namei/lookup.
*/
const char *ni_dirp; /* pathname pointer */
int ni_dirfd; /* AT_FDCWD or fd of base of */
/* relative paths */
enum uio_seg ni_segflg; /* location of pathname */
/*
* Arguments to lookup.
*/
struct vnode *ni_startdir; /* starting directory */
struct vnode *ni_rootdir; /* logical root directory */
uint64_t ni_pledge; /* expected pledge for namei */
u_char ni_unveil; /* required unveil flags for namei */
/*
* Results: returned from/manipulated by lookup
*/
struct vnode *ni_vp; /* vnode of result */
struct vnode *ni_dvp; /* vnode of intermediate dir */
/*
* Shared between namei and lookup/commit routines.
*/
size_t ni_pathlen; /* remaining chars in path */
char *ni_next; /* next location in pathname */
u_long ni_loopcnt; /* count of symlinks encountered */
struct unveil *ni_unveil_match; /* last matching unveil component */
/*
* Lookup parameters
*/
struct componentname ni_cnd;
};
.Ed
.Pp
The
.Fn namei
function accesses vnode operations by passing arguments in the
partially initialised
.Em componentname
structure
.Em ni_cnd .
This structure describes the subset of information from the nameidata
structure that is passed through to the vnode operations.
See
.Xr VOP_LOOKUP 9
for more information.
The details of the componentname structure are not absolutely necessary
since the members are initialised by the helper macros
.Fn NDINIT
and
.Fn NDINITAT .
It is useful to know the operations and flags as specified in
.Xr VOP_LOOKUP 9 .
.Pp
The
.Fn namei
function overloads
.Em ni_cnd.cn_flags
with some additional flags.
These flags should be specific to
.Fn namei
and ignored by vnode operations.
However, due to the historic close relationship between
.Fn namei
and the vnode operations, these flags are sometimes used
(and set) by vnode operations, particularly
.Fn VOP_LOOKUP .
The additional flags are:
.Pp
.Bl -tag -offset indent -width NOCROSSMOUNT -compact
.It NOCROSSMOUNT
do not cross mount points
.It RDONLY
lookup with read-only semantics
.It HASBUF
caller has allocated pathname buffer
.Em ni_cnd.cn_pnbuf
.It SAVENAME
save pathname buffer
.It SAVESTART
save starting directory
.It ISDOTDOT
current pathname component is ..
.It MAKEENTRY
add entry to the name cache
.It ISLASTCN
this is last component of pathname
.It ISSYMLINK
symlink needs interpretation
.It REALPATH
save pathname buffer for realpath
.It REQUIREDIR
must be a directory
.It STRIPSLASHES
strip trailing slashes from the pathname
.It PDIRUNLOCK
.Fn VOP_LOOKUP
unlocked parent dir
.It BYPASSUNVEIL
bypass pledge paths checks
.It KERNELPATH
access file as kernel, not process
.El
.Pp
If the caller of
.Fn namei
sets the SAVENAME flag, then it must free the buffer.
If
.Fn VOP_LOOKUP
sets the flag, then the buffer must be freed by either the commit
routine or the
.Fn VOP_ABORT
routine.
The SAVESTART flag is set only by the callers of
.Fn namei .
It implies SAVENAME plus the addition of saving the parent directory
that contains the name in
.Em ni_startdir .
It allows repeated calls to
.Fn vfs_lookup
for the name being sought.
The caller is responsible for releasing the buffer and for invoking
.Fn vrele
on
.Em ni_startdir .
.Pp
All access to
.Fn namei ,
.Fn vfs_lookup ,
and
.Fn vfs_relookup
must be in process context.
Pathname lookups cannot be done in interrupt context.
.Sh FUNCTIONS
.Bl -tag -width compact
.It Fn namei "ndp"
Convert a pathname into a pointer to a vnode.
The pathname is specified by
.Em ndp-\*[Gt]ni_dirp
and is of length
.Em ndp-\*[Gt]ni_pathlen .
The
.Em ndp-\*[Gt]segflg
flag defines whether the name in
.Em ndp-\*[Gt]ni_dirp
is an address in kernel space (UIO_SYSSPACE) or an address in user
space (UIO_USERSPACE).
The vnode for the pathname is referenced and returned in
.Em ndp-\*[Gt]ni_vp .
.Pp
If
.Em ndp-\*[Gt]ni_cnd.cn_flags
has the FOLLOW flag set then symbolic links are followed when they
occur at the end of the name translation process.
Symbolic links are always followed for all other pathname components
other than the last.
.Pp
If the LOCKLEAF flag is set, a locked vnode is returned.
.It Fn vfs_lookup "ndp"
Search for a pathname.
This is a very central and rather complicated routine.
.Pp
The pathname is specified by
.Em ndp-\*[Gt]ni_dirp
and is of length
.Em ndp-\*[Gt]ni_pathlen .
The starting directory is taken from
.Em ndp-\*[Gt]ni_startdir .
The pathname is descended until done, or a symbolic link is
encountered.
.Pp
The semantics of
.Fn vfs_lookup
are altered by the operation specified by
.Em ndp-\*[Gt]ni_cnd.cn_nameiop .
When CREATE, RENAME, or DELETE is specified, information usable in
creating, renaming, or deleting a directory entry may be calculated.
.Pp
If
.Em ndp-\*[Gt]ci_cnd.cn_flags
has LOCKPARENT set, the parent directory is returned locked in
.Em ndp-\*[Gt]ni_dvp .
If WANTPARENT is set, the parent directory is returned unlocked.
Otherwise the parent directory is not returned.
If the target of the pathname exists and LOCKLEAF is set, the target
is returned locked in
.Em ndp-\*[Gt]ni_vp ,
otherwise it is returned unlocked.
.It Fn vfs_relookup "dvp" "vpp" "cnp"
Reacquire a path name component in a directory.
This is a quicker way to lookup a pathname component when the parent
directory is known.
The unlocked parent directory vnode is specified by
.Fa dvp
and the pathname component by
.Fa cnp .
The vnode of the pathname is returned in the address specified by
.Fa vpp .
.It Fn NDINITAT "ndp" "op" "flags" "segflg" "dirfd" "namep" "p"
Initialise a nameidata structure pointed to by
.Fa ndp
for use by the
.Nm
interfaces.
It saves having to deal with the componentname structure inside
.Fa ndp .
The operation and flags are specified by
.Fa op
and
.Fa flags
respectively.
These are the values to which
.Em ndp-\*[Gt]ni_cnd.cn_nameiop
and
.Em ndp-\*[Gt]ni_cnd.cn_flags
are respectively set.
The segment flag, which defines whether the pathname is in kernel
address space or user address space, is specified by
.Fa segflg .
The directory from which relative pathnames will be looked up is
specified by
.Fa dirfd ,
with
.Dv AT_FDCWD
specifying use of the current working directory of process
.Fa p .
The argument
.Fa namep
is a pointer to the pathname that
.Em ndp-\*[Gt]ni_dirp
is set to and
.Fa p
is the calling process.
.It Fn NDINIT "ndp" "op" "flags" "segflg" "namep" "p"
Same as
.Fn NDINITAT "ndp" "op" "flags" "segflg" \fRAT_FDCWD\fP "namep" "p" .
.El
.Sh CODE REFERENCES
The name lookup subsystem is implemented within the file
.Pa sys/kern/vfs_lookup.c .
.Sh SEE ALSO
.Xr intro 9 ,
.\" .Xr namecache 9 ,
.Xr vfs 9 ,
.Xr vnode 9 ,
.Xr VOP_LOOKUP 9
.Sh HISTORY
The
.Fn namei
function first appeared in
.At v4 .
Its name is an abbreviation for the name-to-inode conversion
which it performed before the appearance of
.Xr vfs 9
in
.Bx 4.3 Reno .
.Sh BUGS
It is unfortunate that much of the
.Nm
interface makes assumptions on the underlying vnode operations.
These assumptions are an artefact of the introduction of the vfs
interface to split a file system interface which was historically
designed as a tightly coupled module.
|