vfs_lookup.c revision 1.208.6.2 1 1.208.6.2 martin /* $NetBSD: vfs_lookup.c,v 1.208.6.2 2020/04/08 14:08:52 martin Exp $ */
2 1.13 cgd
3 1.10 cgd /*
4 1.12 mycroft * Copyright (c) 1982, 1986, 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.10 cgd * (c) UNIX System Laboratories, Inc.
7 1.10 cgd * All or some portions of this file are derived from material licensed
8 1.10 cgd * to the University of California by American Telephone and Telegraph
9 1.10 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.10 cgd * the permission of UNIX System Laboratories, Inc.
11 1.10 cgd *
12 1.10 cgd * Redistribution and use in source and binary forms, with or without
13 1.10 cgd * modification, are permitted provided that the following conditions
14 1.10 cgd * are met:
15 1.10 cgd * 1. Redistributions of source code must retain the above copyright
16 1.10 cgd * notice, this list of conditions and the following disclaimer.
17 1.10 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.10 cgd * notice, this list of conditions and the following disclaimer in the
19 1.10 cgd * documentation and/or other materials provided with the distribution.
20 1.49 agc * 3. Neither the name of the University nor the names of its contributors
21 1.10 cgd * may be used to endorse or promote products derived from this software
22 1.10 cgd * without specific prior written permission.
23 1.10 cgd *
24 1.10 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.10 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.10 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.10 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.10 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.10 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.10 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.10 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.10 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.10 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.10 cgd * SUCH DAMAGE.
35 1.10 cgd *
36 1.26 fvdl * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
37 1.10 cgd */
38 1.38 lukem
39 1.38 lukem #include <sys/cdefs.h>
40 1.208.6.2 martin __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.208.6.2 2020/04/08 14:08:52 martin Exp $");
41 1.27 thorpej
42 1.203 pooka #ifdef _KERNEL_OPT
43 1.67 chs #include "opt_magiclinks.h"
44 1.203 pooka #endif
45 1.10 cgd
46 1.10 cgd #include <sys/param.h>
47 1.15 cgd #include <sys/systm.h>
48 1.61 thorpej #include <sys/kernel.h>
49 1.10 cgd #include <sys/syslimits.h>
50 1.10 cgd #include <sys/time.h>
51 1.10 cgd #include <sys/namei.h>
52 1.10 cgd #include <sys/vnode.h>
53 1.208.6.2 martin #include <sys/vnode_impl.h>
54 1.10 cgd #include <sys/mount.h>
55 1.10 cgd #include <sys/errno.h>
56 1.39 lukem #include <sys/filedesc.h>
57 1.39 lukem #include <sys/hash.h>
58 1.10 cgd #include <sys/proc.h>
59 1.40 wrstuden #include <sys/syslog.h>
60 1.70 elad #include <sys/kauth.h>
61 1.97 ad #include <sys/ktrace.h>
62 1.192 christos #include <sys/dirent.h>
63 1.12 mycroft
64 1.67 chs #ifndef MAGICLINKS
65 1.67 chs #define MAGICLINKS 0
66 1.67 chs #endif
67 1.67 chs
68 1.67 chs int vfs_magiclinks = MAGICLINKS;
69 1.67 chs
70 1.191 christos __CTASSERT(MAXNAMLEN == NAME_MAX);
71 1.191 christos
72 1.10 cgd /*
73 1.61 thorpej * Substitute replacement text for 'magic' strings in symlinks.
74 1.61 thorpej * Returns 0 if successful, and returns non-zero if an error
75 1.61 thorpej * occurs. (Currently, the only possible error is running out
76 1.61 thorpej * of temporary pathname space.)
77 1.61 thorpej *
78 1.61 thorpej * Looks for "@<string>" and "@<string>/", where <string> is a
79 1.61 thorpej * recognized 'magic' string. Replaces the "@<string>" with the
80 1.61 thorpej * appropriate replacement text. (Note that in some cases the
81 1.61 thorpej * replacement text may have zero length.)
82 1.61 thorpej *
83 1.61 thorpej * This would have been table driven, but the variance in
84 1.61 thorpej * replacement strings (and replacement string lengths) made
85 1.61 thorpej * that impractical.
86 1.61 thorpej */
87 1.63 thorpej #define VNL(x) \
88 1.63 thorpej (sizeof(x) - 1)
89 1.63 thorpej
90 1.63 thorpej #define VO '{'
91 1.63 thorpej #define VC '}'
92 1.63 thorpej
93 1.61 thorpej #define MATCH(str) \
94 1.63 thorpej ((termchar == '/' && i + VNL(str) == *len) || \
95 1.63 thorpej (i + VNL(str) < *len && \
96 1.63 thorpej cp[i + VNL(str)] == termchar)) && \
97 1.63 thorpej !strncmp((str), &cp[i], VNL(str))
98 1.61 thorpej
99 1.61 thorpej #define SUBSTITUTE(m, s, sl) \
100 1.115 christos if ((newlen + (sl)) >= MAXPATHLEN) \
101 1.115 christos return 1; \
102 1.63 thorpej i += VNL(m); \
103 1.63 thorpej if (termchar != '/') \
104 1.63 thorpej i++; \
105 1.115 christos (void)memcpy(&tmp[newlen], (s), (sl)); \
106 1.63 thorpej newlen += (sl); \
107 1.63 thorpej change = 1; \
108 1.63 thorpej termchar = '/';
109 1.61 thorpej
110 1.61 thorpej static int
111 1.115 christos symlink_magic(struct proc *p, char *cp, size_t *len)
112 1.61 thorpej {
113 1.66 yamt char *tmp;
114 1.115 christos size_t change, i, newlen, slen;
115 1.115 christos char termchar = '/';
116 1.115 christos char idtmp[11]; /* enough for 32 bit *unsigned* integer */
117 1.101 mjf
118 1.61 thorpej
119 1.66 yamt tmp = PNBUF_GET();
120 1.61 thorpej for (change = i = newlen = 0; i < *len; ) {
121 1.63 thorpej if (cp[i] != '@') {
122 1.61 thorpej tmp[newlen++] = cp[i++];
123 1.63 thorpej continue;
124 1.63 thorpej }
125 1.63 thorpej
126 1.63 thorpej i++;
127 1.63 thorpej
128 1.63 thorpej /* Check for @{var} syntax. */
129 1.63 thorpej if (cp[i] == VO) {
130 1.63 thorpej termchar = VC;
131 1.61 thorpej i++;
132 1.63 thorpej }
133 1.63 thorpej
134 1.63 thorpej /*
135 1.63 thorpej * The following checks should be ordered according
136 1.63 thorpej * to frequency of use.
137 1.63 thorpej */
138 1.63 thorpej if (MATCH("machine_arch")) {
139 1.115 christos slen = VNL(MACHINE_ARCH);
140 1.115 christos SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
141 1.63 thorpej } else if (MATCH("machine")) {
142 1.115 christos slen = VNL(MACHINE);
143 1.115 christos SUBSTITUTE("machine", MACHINE, slen);
144 1.63 thorpej } else if (MATCH("hostname")) {
145 1.115 christos SUBSTITUTE("hostname", hostname, hostnamelen);
146 1.63 thorpej } else if (MATCH("osrelease")) {
147 1.115 christos slen = strlen(osrelease);
148 1.115 christos SUBSTITUTE("osrelease", osrelease, slen);
149 1.63 thorpej } else if (MATCH("emul")) {
150 1.115 christos slen = strlen(p->p_emul->e_name);
151 1.115 christos SUBSTITUTE("emul", p->p_emul->e_name, slen);
152 1.63 thorpej } else if (MATCH("kernel_ident")) {
153 1.115 christos slen = strlen(kernel_ident);
154 1.115 christos SUBSTITUTE("kernel_ident", kernel_ident, slen);
155 1.63 thorpej } else if (MATCH("domainname")) {
156 1.115 christos SUBSTITUTE("domainname", domainname, domainnamelen);
157 1.63 thorpej } else if (MATCH("ostype")) {
158 1.115 christos slen = strlen(ostype);
159 1.115 christos SUBSTITUTE("ostype", ostype, slen);
160 1.72 elad } else if (MATCH("uid")) {
161 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
162 1.72 elad kauth_cred_geteuid(kauth_cred_get()));
163 1.115 christos SUBSTITUTE("uid", idtmp, slen);
164 1.101 mjf } else if (MATCH("ruid")) {
165 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
166 1.101 mjf kauth_cred_getuid(kauth_cred_get()));
167 1.115 christos SUBSTITUTE("ruid", idtmp, slen);
168 1.115 christos } else if (MATCH("gid")) {
169 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
170 1.115 christos kauth_cred_getegid(kauth_cred_get()));
171 1.115 christos SUBSTITUTE("gid", idtmp, slen);
172 1.115 christos } else if (MATCH("rgid")) {
173 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
174 1.115 christos kauth_cred_getgid(kauth_cred_get()));
175 1.115 christos SUBSTITUTE("rgid", idtmp, slen);
176 1.63 thorpej } else {
177 1.63 thorpej tmp[newlen++] = '@';
178 1.63 thorpej if (termchar == VC)
179 1.63 thorpej tmp[newlen++] = VO;
180 1.61 thorpej }
181 1.61 thorpej }
182 1.61 thorpej
183 1.66 yamt if (change) {
184 1.115 christos (void)memcpy(cp, tmp, newlen);
185 1.66 yamt *len = newlen;
186 1.66 yamt }
187 1.66 yamt PNBUF_PUT(tmp);
188 1.61 thorpej
189 1.115 christos return 0;
190 1.61 thorpej }
191 1.61 thorpej
192 1.63 thorpej #undef VNL
193 1.63 thorpej #undef VO
194 1.63 thorpej #undef VC
195 1.63 thorpej #undef MATCH
196 1.63 thorpej #undef SUBSTITUTE
197 1.63 thorpej
198 1.123 dholland ////////////////////////////////////////////////////////////
199 1.123 dholland
200 1.123 dholland /*
201 1.197 dholland * Determine the namei hash (for the namecache) for name.
202 1.131 dholland * If *ep != NULL, hash from name to ep-1.
203 1.131 dholland * If *ep == NULL, hash from name until the first NUL or '/', and
204 1.131 dholland * return the location of this termination character in *ep.
205 1.131 dholland *
206 1.131 dholland * This function returns an equivalent hash to the MI hash32_strn().
207 1.131 dholland * The latter isn't used because in the *ep == NULL case, determining
208 1.131 dholland * the length of the string to the first NUL or `/' and then calling
209 1.131 dholland * hash32_strn() involves unnecessary double-handling of the data.
210 1.131 dholland */
211 1.131 dholland uint32_t
212 1.131 dholland namei_hash(const char *name, const char **ep)
213 1.131 dholland {
214 1.131 dholland uint32_t hash;
215 1.131 dholland
216 1.131 dholland hash = HASH32_STR_INIT;
217 1.131 dholland if (*ep != NULL) {
218 1.131 dholland for (; name < *ep; name++)
219 1.131 dholland hash = hash * 33 + *(const uint8_t *)name;
220 1.131 dholland } else {
221 1.131 dholland for (; *name != '\0' && *name != '/'; name++)
222 1.131 dholland hash = hash * 33 + *(const uint8_t *)name;
223 1.131 dholland *ep = name;
224 1.131 dholland }
225 1.131 dholland return (hash + (hash >> 5));
226 1.131 dholland }
227 1.131 dholland
228 1.197 dholland /*
229 1.197 dholland * Find the end of the first path component in NAME and return its
230 1.197 dholland * length.
231 1.197 dholland */
232 1.197 dholland static size_t
233 1.197 dholland namei_getcomponent(const char *name)
234 1.197 dholland {
235 1.197 dholland size_t pos;
236 1.197 dholland
237 1.197 dholland pos = 0;
238 1.197 dholland while (name[pos] != '\0' && name[pos] != '/') {
239 1.197 dholland pos++;
240 1.197 dholland }
241 1.197 dholland return pos;
242 1.197 dholland }
243 1.197 dholland
244 1.131 dholland ////////////////////////////////////////////////////////////
245 1.131 dholland
246 1.131 dholland /*
247 1.123 dholland * Sealed abstraction for pathnames.
248 1.123 dholland *
249 1.123 dholland * System-call-layer level code that is going to call namei should
250 1.123 dholland * first create a pathbuf and adjust all the bells and whistles on it
251 1.176 dholland * as needed by context.
252 1.123 dholland */
253 1.123 dholland
254 1.123 dholland struct pathbuf {
255 1.123 dholland char *pb_path;
256 1.123 dholland char *pb_pathcopy;
257 1.123 dholland unsigned pb_pathcopyuses;
258 1.123 dholland };
259 1.123 dholland
260 1.123 dholland static struct pathbuf *
261 1.123 dholland pathbuf_create_raw(void)
262 1.123 dholland {
263 1.123 dholland struct pathbuf *pb;
264 1.123 dholland
265 1.123 dholland pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
266 1.123 dholland pb->pb_path = PNBUF_GET();
267 1.123 dholland if (pb->pb_path == NULL) {
268 1.123 dholland kmem_free(pb, sizeof(*pb));
269 1.123 dholland return NULL;
270 1.123 dholland }
271 1.123 dholland pb->pb_pathcopy = NULL;
272 1.123 dholland pb->pb_pathcopyuses = 0;
273 1.123 dholland return pb;
274 1.123 dholland }
275 1.123 dholland
276 1.123 dholland void
277 1.123 dholland pathbuf_destroy(struct pathbuf *pb)
278 1.123 dholland {
279 1.123 dholland KASSERT(pb->pb_pathcopyuses == 0);
280 1.123 dholland KASSERT(pb->pb_pathcopy == NULL);
281 1.123 dholland PNBUF_PUT(pb->pb_path);
282 1.123 dholland kmem_free(pb, sizeof(*pb));
283 1.123 dholland }
284 1.123 dholland
285 1.123 dholland struct pathbuf *
286 1.124 dholland pathbuf_assimilate(char *pnbuf)
287 1.124 dholland {
288 1.124 dholland struct pathbuf *pb;
289 1.124 dholland
290 1.124 dholland pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
291 1.124 dholland pb->pb_path = pnbuf;
292 1.124 dholland pb->pb_pathcopy = NULL;
293 1.124 dholland pb->pb_pathcopyuses = 0;
294 1.124 dholland return pb;
295 1.124 dholland }
296 1.124 dholland
297 1.124 dholland struct pathbuf *
298 1.123 dholland pathbuf_create(const char *path)
299 1.123 dholland {
300 1.123 dholland struct pathbuf *pb;
301 1.123 dholland int error;
302 1.123 dholland
303 1.123 dholland pb = pathbuf_create_raw();
304 1.123 dholland if (pb == NULL) {
305 1.123 dholland return NULL;
306 1.123 dholland }
307 1.123 dholland error = copystr(path, pb->pb_path, PATH_MAX, NULL);
308 1.123 dholland if (error != 0) {
309 1.123 dholland KASSERT(!"kernel path too long in pathbuf_create");
310 1.123 dholland /* make sure it's null-terminated, just in case */
311 1.123 dholland pb->pb_path[PATH_MAX-1] = '\0';
312 1.123 dholland }
313 1.123 dholland return pb;
314 1.123 dholland }
315 1.123 dholland
316 1.123 dholland int
317 1.123 dholland pathbuf_copyin(const char *userpath, struct pathbuf **ret)
318 1.123 dholland {
319 1.123 dholland struct pathbuf *pb;
320 1.123 dholland int error;
321 1.123 dholland
322 1.123 dholland pb = pathbuf_create_raw();
323 1.123 dholland if (pb == NULL) {
324 1.123 dholland return ENOMEM;
325 1.123 dholland }
326 1.123 dholland error = copyinstr(userpath, pb->pb_path, PATH_MAX, NULL);
327 1.123 dholland if (error) {
328 1.123 dholland pathbuf_destroy(pb);
329 1.123 dholland return error;
330 1.123 dholland }
331 1.123 dholland *ret = pb;
332 1.123 dholland return 0;
333 1.123 dholland }
334 1.123 dholland
335 1.123 dholland /*
336 1.173 dholland * XXX should not exist:
337 1.176 dholland * 1. whether a pointer is kernel or user should be statically checkable.
338 1.173 dholland * 2. copyin should be handled by the upper part of the syscall layer,
339 1.173 dholland * not in here.
340 1.123 dholland */
341 1.123 dholland int
342 1.123 dholland pathbuf_maybe_copyin(const char *path, enum uio_seg seg, struct pathbuf **ret)
343 1.123 dholland {
344 1.123 dholland if (seg == UIO_USERSPACE) {
345 1.123 dholland return pathbuf_copyin(path, ret);
346 1.123 dholland } else {
347 1.123 dholland *ret = pathbuf_create(path);
348 1.123 dholland if (*ret == NULL) {
349 1.123 dholland return ENOMEM;
350 1.123 dholland }
351 1.123 dholland return 0;
352 1.123 dholland }
353 1.123 dholland }
354 1.123 dholland
355 1.123 dholland /*
356 1.123 dholland * Get a copy of the path buffer as it currently exists. If this is
357 1.123 dholland * called after namei starts the results may be arbitrary.
358 1.123 dholland */
359 1.123 dholland void
360 1.123 dholland pathbuf_copystring(const struct pathbuf *pb, char *buf, size_t maxlen)
361 1.123 dholland {
362 1.123 dholland strlcpy(buf, pb->pb_path, maxlen);
363 1.123 dholland }
364 1.123 dholland
365 1.123 dholland /*
366 1.123 dholland * These two functions allow access to a saved copy of the original
367 1.123 dholland * path string. The first copy should be gotten before namei is
368 1.123 dholland * called. Each copy that is gotten should be put back.
369 1.123 dholland */
370 1.123 dholland
371 1.123 dholland const char *
372 1.123 dholland pathbuf_stringcopy_get(struct pathbuf *pb)
373 1.123 dholland {
374 1.123 dholland if (pb->pb_pathcopyuses == 0) {
375 1.123 dholland pb->pb_pathcopy = PNBUF_GET();
376 1.123 dholland strcpy(pb->pb_pathcopy, pb->pb_path);
377 1.123 dholland }
378 1.123 dholland pb->pb_pathcopyuses++;
379 1.123 dholland return pb->pb_pathcopy;
380 1.123 dholland }
381 1.123 dholland
382 1.123 dholland void
383 1.123 dholland pathbuf_stringcopy_put(struct pathbuf *pb, const char *str)
384 1.123 dholland {
385 1.123 dholland KASSERT(str == pb->pb_pathcopy);
386 1.123 dholland KASSERT(pb->pb_pathcopyuses > 0);
387 1.123 dholland pb->pb_pathcopyuses--;
388 1.123 dholland if (pb->pb_pathcopyuses == 0) {
389 1.123 dholland PNBUF_PUT(pb->pb_pathcopy);
390 1.123 dholland pb->pb_pathcopy = NULL;
391 1.123 dholland }
392 1.123 dholland }
393 1.123 dholland
394 1.123 dholland
395 1.123 dholland ////////////////////////////////////////////////////////////
396 1.123 dholland
397 1.61 thorpej /*
398 1.173 dholland * namei: convert a pathname into a pointer to a (maybe-locked) vnode,
399 1.173 dholland * and maybe also its parent directory vnode, and assorted other guff.
400 1.173 dholland * See namei(9) for the interface documentation.
401 1.173 dholland *
402 1.10 cgd *
403 1.10 cgd * The FOLLOW flag is set when symbolic links are to be followed
404 1.10 cgd * when they occur at the end of the name translation process.
405 1.10 cgd * Symbolic links are always followed for all other pathname
406 1.10 cgd * components other than the last.
407 1.10 cgd *
408 1.10 cgd * The segflg defines whether the name is to be copied from user
409 1.10 cgd * space or kernel space.
410 1.10 cgd *
411 1.10 cgd * Overall outline of namei:
412 1.10 cgd *
413 1.10 cgd * copy in name
414 1.10 cgd * get starting directory
415 1.10 cgd * while (!done && !error) {
416 1.10 cgd * call lookup to search path.
417 1.10 cgd * if symbolic link, massage name in buffer and continue
418 1.10 cgd * }
419 1.10 cgd */
420 1.117 dholland
421 1.117 dholland /*
422 1.173 dholland * Search a pathname.
423 1.173 dholland * This is a very central and rather complicated routine.
424 1.173 dholland *
425 1.173 dholland * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
426 1.173 dholland * The starting directory is passed in. The pathname is descended
427 1.173 dholland * until done, or a symbolic link is encountered. The variable ni_more
428 1.173 dholland * is clear if the path is completed; it is set to one if a symbolic
429 1.173 dholland * link needing interpretation is encountered.
430 1.173 dholland *
431 1.173 dholland * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
432 1.173 dholland * whether the name is to be looked up, created, renamed, or deleted.
433 1.173 dholland * When CREATE, RENAME, or DELETE is specified, information usable in
434 1.173 dholland * creating, renaming, or deleting a directory entry may be calculated.
435 1.173 dholland * If flag has LOCKPARENT or'ed into it, the parent directory is returned
436 1.173 dholland * locked. Otherwise the parent directory is not returned. If the target
437 1.173 dholland * of the pathname exists and LOCKLEAF is or'ed into the flag the target
438 1.173 dholland * is returned locked, otherwise it is returned unlocked. When creating
439 1.173 dholland * or renaming and LOCKPARENT is specified, the target may not be ".".
440 1.173 dholland * When deleting and LOCKPARENT is specified, the target may be ".".
441 1.173 dholland *
442 1.173 dholland * Overall outline of lookup:
443 1.173 dholland *
444 1.173 dholland * dirloop:
445 1.173 dholland * identify next component of name at ndp->ni_ptr
446 1.173 dholland * handle degenerate case where name is null string
447 1.173 dholland * if .. and crossing mount points and on mounted filesys, find parent
448 1.173 dholland * call VOP_LOOKUP routine for next component name
449 1.173 dholland * directory vnode returned in ni_dvp, locked.
450 1.173 dholland * component vnode returned in ni_vp (if it exists), locked.
451 1.173 dholland * if result vnode is mounted on and crossing mount points,
452 1.173 dholland * find mounted on vnode
453 1.173 dholland * if more components of name, do next level at dirloop
454 1.173 dholland * return the answer in ni_vp, locked if LOCKLEAF set
455 1.173 dholland * if LOCKPARENT set, return locked parent in ni_dvp
456 1.173 dholland */
457 1.173 dholland
458 1.173 dholland
459 1.173 dholland /*
460 1.117 dholland * Internal state for a namei operation.
461 1.173 dholland *
462 1.173 dholland * cnp is always equal to &ndp->ni_cnp.
463 1.117 dholland */
464 1.117 dholland struct namei_state {
465 1.117 dholland struct nameidata *ndp;
466 1.117 dholland struct componentname *cnp;
467 1.117 dholland
468 1.118 dholland int docache; /* == 0 do not cache last component */
469 1.118 dholland int rdonly; /* lookup read-only flag bit */
470 1.118 dholland int slashes;
471 1.137 dholland
472 1.137 dholland unsigned attempt_retry:1; /* true if error allows emul retry */
473 1.208.6.1 christos unsigned root_referenced:1; /* true if ndp->ni_rootdir and
474 1.208.6.1 christos ndp->ni_erootdir were referenced */
475 1.117 dholland };
476 1.117 dholland
477 1.118 dholland
478 1.117 dholland /*
479 1.117 dholland * Initialize the namei working state.
480 1.117 dholland */
481 1.117 dholland static void
482 1.117 dholland namei_init(struct namei_state *state, struct nameidata *ndp)
483 1.117 dholland {
484 1.202 riastrad
485 1.117 dholland state->ndp = ndp;
486 1.117 dholland state->cnp = &ndp->ni_cnd;
487 1.117 dholland
488 1.118 dholland state->docache = 0;
489 1.118 dholland state->rdonly = 0;
490 1.118 dholland state->slashes = 0;
491 1.133 dholland
492 1.208.6.1 christos state->root_referenced = 0;
493 1.208.6.1 christos
494 1.205 riastrad KASSERTMSG((state->cnp->cn_cred != NULL), "namei: bad cred/proc");
495 1.205 riastrad KASSERTMSG(((state->cnp->cn_nameiop & (~OPMASK)) == 0),
496 1.205 riastrad "namei: nameiop contaminated with flags: %08"PRIx32,
497 1.205 riastrad state->cnp->cn_nameiop);
498 1.205 riastrad KASSERTMSG(((state->cnp->cn_flags & OPMASK) == 0),
499 1.205 riastrad "name: flags contaminated with nameiops: %08"PRIx32,
500 1.205 riastrad state->cnp->cn_flags);
501 1.133 dholland
502 1.133 dholland /*
503 1.133 dholland * The buffer for name translation shall be the one inside the
504 1.133 dholland * pathbuf.
505 1.133 dholland */
506 1.133 dholland state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
507 1.117 dholland }
508 1.117 dholland
509 1.117 dholland /*
510 1.117 dholland * Clean up the working namei state, leaving things ready for return
511 1.117 dholland * from namei.
512 1.117 dholland */
513 1.117 dholland static void
514 1.117 dholland namei_cleanup(struct namei_state *state)
515 1.117 dholland {
516 1.117 dholland KASSERT(state->cnp == &state->ndp->ni_cnd);
517 1.117 dholland
518 1.208.6.1 christos if (state->root_referenced) {
519 1.208.6.1 christos vrele(state->ndp->ni_rootdir);
520 1.208.6.1 christos if (state->ndp->ni_erootdir != NULL)
521 1.208.6.1 christos vrele(state->ndp->ni_erootdir);
522 1.208.6.1 christos }
523 1.117 dholland }
524 1.117 dholland
525 1.117 dholland //////////////////////////////
526 1.117 dholland
527 1.117 dholland /*
528 1.133 dholland * Get the directory context.
529 1.133 dholland * Initializes the rootdir and erootdir state and returns a reference
530 1.133 dholland * to the starting dir.
531 1.117 dholland */
532 1.133 dholland static struct vnode *
533 1.196 dholland namei_getstartdir(struct namei_state *state)
534 1.117 dholland {
535 1.117 dholland struct nameidata *ndp = state->ndp;
536 1.117 dholland struct componentname *cnp = state->cnp;
537 1.117 dholland struct cwdinfo *cwdi; /* pointer to cwd state */
538 1.133 dholland struct vnode *rootdir, *erootdir, *curdir, *startdir;
539 1.117 dholland
540 1.208.6.1 christos if (state->root_referenced) {
541 1.208.6.1 christos vrele(state->ndp->ni_rootdir);
542 1.208.6.1 christos if (state->ndp->ni_erootdir != NULL)
543 1.208.6.1 christos vrele(state->ndp->ni_erootdir);
544 1.208.6.1 christos state->root_referenced = 0;
545 1.208.6.1 christos }
546 1.208.6.1 christos
547 1.208.6.2 martin /* NB: must not block while inspecting the cwdinfo. */
548 1.208.6.2 martin cwdi = cwdenter(RW_READER);
549 1.21 kleink
550 1.133 dholland /* root dir */
551 1.133 dholland if (cwdi->cwdi_rdir == NULL || (cnp->cn_flags & NOCHROOT)) {
552 1.133 dholland rootdir = rootvnode;
553 1.133 dholland } else {
554 1.133 dholland rootdir = cwdi->cwdi_rdir;
555 1.10 cgd }
556 1.123 dholland
557 1.133 dholland /* emulation root dir, if any */
558 1.133 dholland if ((cnp->cn_flags & TRYEMULROOT) == 0) {
559 1.133 dholland /* if we don't want it, don't fetch it */
560 1.133 dholland erootdir = NULL;
561 1.133 dholland } else if (cnp->cn_flags & EMULROOTSET) {
562 1.133 dholland /* explicitly set emulroot; "/../" doesn't override this */
563 1.133 dholland erootdir = ndp->ni_erootdir;
564 1.133 dholland } else if (!strncmp(ndp->ni_pnbuf, "/../", 4)) {
565 1.133 dholland /* explicit reference to real rootdir */
566 1.133 dholland erootdir = NULL;
567 1.133 dholland } else {
568 1.133 dholland /* may be null */
569 1.133 dholland erootdir = cwdi->cwdi_edir;
570 1.133 dholland }
571 1.21 kleink
572 1.133 dholland /* current dir */
573 1.196 dholland curdir = cwdi->cwdi_cdir;
574 1.85 dsl
575 1.133 dholland if (ndp->ni_pnbuf[0] != '/') {
576 1.198 dholland if (ndp->ni_atdir != NULL) {
577 1.198 dholland startdir = ndp->ni_atdir;
578 1.196 dholland } else {
579 1.196 dholland startdir = curdir;
580 1.196 dholland }
581 1.133 dholland erootdir = NULL;
582 1.133 dholland } else if (cnp->cn_flags & TRYEMULROOT && erootdir != NULL) {
583 1.133 dholland startdir = erootdir;
584 1.23 mycroft } else {
585 1.133 dholland startdir = rootdir;
586 1.133 dholland erootdir = NULL;
587 1.23 mycroft }
588 1.133 dholland
589 1.133 dholland state->ndp->ni_rootdir = rootdir;
590 1.133 dholland state->ndp->ni_erootdir = erootdir;
591 1.117 dholland
592 1.117 dholland /*
593 1.133 dholland * Get a reference to the start dir so we can safely unlock cwdi.
594 1.133 dholland *
595 1.208.6.1 christos * Must hold references to rootdir and erootdir while we're running.
596 1.208.6.1 christos * A multithreaded process may chroot during namei.
597 1.117 dholland */
598 1.133 dholland vref(startdir);
599 1.208.6.1 christos vref(state->ndp->ni_rootdir);
600 1.208.6.1 christos if (state->ndp->ni_erootdir != NULL)
601 1.208.6.1 christos vref(state->ndp->ni_erootdir);
602 1.208.6.1 christos state->root_referenced = 1;
603 1.133 dholland
604 1.208.6.2 martin cwdexit(cwdi);
605 1.133 dholland return startdir;
606 1.133 dholland }
607 1.133 dholland
608 1.133 dholland /*
609 1.133 dholland * Get the directory context for the nfsd case, in parallel to
610 1.133 dholland * getstartdir. Initializes the rootdir and erootdir state and
611 1.173 dholland * returns a reference to the passed-in starting dir.
612 1.133 dholland */
613 1.133 dholland static struct vnode *
614 1.196 dholland namei_getstartdir_for_nfsd(struct namei_state *state)
615 1.133 dholland {
616 1.198 dholland KASSERT(state->ndp->ni_atdir != NULL);
617 1.193 dholland
618 1.133 dholland /* always use the real root, and never set an emulation root */
619 1.133 dholland state->ndp->ni_rootdir = rootvnode;
620 1.133 dholland state->ndp->ni_erootdir = NULL;
621 1.133 dholland
622 1.198 dholland vref(state->ndp->ni_atdir);
623 1.208.6.1 christos KASSERT(! state->root_referenced);
624 1.208.6.1 christos vref(state->ndp->ni_rootdir);
625 1.208.6.1 christos state->root_referenced = 1;
626 1.198 dholland return state->ndp->ni_atdir;
627 1.133 dholland }
628 1.133 dholland
629 1.133 dholland
630 1.133 dholland /*
631 1.133 dholland * Ktrace the namei operation.
632 1.133 dholland */
633 1.133 dholland static void
634 1.133 dholland namei_ktrace(struct namei_state *state)
635 1.133 dholland {
636 1.133 dholland struct nameidata *ndp = state->ndp;
637 1.133 dholland struct componentname *cnp = state->cnp;
638 1.133 dholland struct lwp *self = curlwp; /* thread doing namei() */
639 1.133 dholland const char *emul_path;
640 1.133 dholland
641 1.97 ad if (ktrpoint(KTR_NAMEI)) {
642 1.90 dsl if (ndp->ni_erootdir != NULL) {
643 1.89 dsl /*
644 1.89 dsl * To make any sense, the trace entry need to have the
645 1.89 dsl * text of the emulation path prepended.
646 1.89 dsl * Usually we can get this from the current process,
647 1.89 dsl * but when called from emul_find_interp() it is only
648 1.89 dsl * in the exec_package - so we get it passed in ni_next
649 1.89 dsl * (this is a hack).
650 1.89 dsl */
651 1.88 dsl if (cnp->cn_flags & EMULROOTSET)
652 1.89 dsl emul_path = ndp->ni_next;
653 1.88 dsl else
654 1.117 dholland emul_path = self->l_proc->p_emul->e_path;
655 1.97 ad ktrnamei2(emul_path, strlen(emul_path),
656 1.124 dholland ndp->ni_pnbuf, ndp->ni_pathlen);
657 1.88 dsl } else
658 1.124 dholland ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
659 1.88 dsl }
660 1.133 dholland }
661 1.133 dholland
662 1.133 dholland /*
663 1.166 dholland * Start up namei. Find the root dir and cwd, establish the starting
664 1.166 dholland * directory for lookup, and lock it. Also calls ktrace when
665 1.133 dholland * appropriate.
666 1.133 dholland */
667 1.133 dholland static int
668 1.196 dholland namei_start(struct namei_state *state, int isnfsd,
669 1.140 dholland struct vnode **startdir_ret)
670 1.133 dholland {
671 1.133 dholland struct nameidata *ndp = state->ndp;
672 1.140 dholland struct vnode *startdir;
673 1.133 dholland
674 1.133 dholland /* length includes null terminator (was originally from copyinstr) */
675 1.133 dholland ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
676 1.133 dholland
677 1.133 dholland /*
678 1.133 dholland * POSIX.1 requirement: "" is not a valid file name.
679 1.133 dholland */
680 1.133 dholland if (ndp->ni_pathlen == 1) {
681 1.133 dholland return ENOENT;
682 1.133 dholland }
683 1.133 dholland
684 1.133 dholland ndp->ni_loopcnt = 0;
685 1.133 dholland
686 1.133 dholland /* Get starting directory, set up root, and ktrace. */
687 1.193 dholland if (isnfsd) {
688 1.196 dholland startdir = namei_getstartdir_for_nfsd(state);
689 1.133 dholland /* no ktrace */
690 1.133 dholland } else {
691 1.196 dholland startdir = namei_getstartdir(state);
692 1.133 dholland namei_ktrace(state);
693 1.133 dholland }
694 1.97 ad
695 1.200 manu /* NDAT may feed us with a non directory namei_getstartdir */
696 1.208 dholland if (startdir->v_type != VDIR) {
697 1.208 dholland vrele(startdir);
698 1.200 manu return ENOTDIR;
699 1.208 dholland }
700 1.200 manu
701 1.140 dholland *startdir_ret = startdir;
702 1.117 dholland return 0;
703 1.117 dholland }
704 1.117 dholland
705 1.117 dholland /*
706 1.173 dholland * Check for being at a symlink that we're going to follow.
707 1.117 dholland */
708 1.117 dholland static inline int
709 1.144 dholland namei_atsymlink(struct namei_state *state, struct vnode *foundobj)
710 1.117 dholland {
711 1.144 dholland return (foundobj->v_type == VLNK) &&
712 1.139 dholland (state->cnp->cn_flags & (FOLLOW|REQUIREDIR));
713 1.117 dholland }
714 1.117 dholland
715 1.117 dholland /*
716 1.117 dholland * Follow a symlink.
717 1.173 dholland *
718 1.173 dholland * Updates searchdir. inhibitmagic causes magic symlinks to not be
719 1.173 dholland * interpreted; this is used by nfsd.
720 1.174 jakllsch *
721 1.174 jakllsch * Unlocks foundobj on success (ugh)
722 1.117 dholland */
723 1.117 dholland static inline int
724 1.141 dholland namei_follow(struct namei_state *state, int inhibitmagic,
725 1.161 dholland struct vnode *searchdir, struct vnode *foundobj,
726 1.141 dholland struct vnode **newsearchdir_ret)
727 1.117 dholland {
728 1.117 dholland struct nameidata *ndp = state->ndp;
729 1.117 dholland struct componentname *cnp = state->cnp;
730 1.117 dholland
731 1.117 dholland struct lwp *self = curlwp; /* thread doing namei() */
732 1.117 dholland struct iovec aiov; /* uio for reading symbolic links */
733 1.117 dholland struct uio auio;
734 1.117 dholland char *cp; /* pointer into pathname argument */
735 1.117 dholland size_t linklen;
736 1.117 dholland int error;
737 1.117 dholland
738 1.117 dholland if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
739 1.117 dholland return ELOOP;
740 1.117 dholland }
741 1.208.6.2 martin
742 1.208.6.2 martin vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
743 1.161 dholland if (foundobj->v_mount->mnt_flag & MNT_SYMPERM) {
744 1.161 dholland error = VOP_ACCESS(foundobj, VEXEC, cnp->cn_cred);
745 1.208.6.2 martin if (error != 0) {
746 1.208.6.2 martin VOP_UNLOCK(foundobj);
747 1.117 dholland return error;
748 1.208.6.2 martin }
749 1.117 dholland }
750 1.124 dholland
751 1.124 dholland /* FUTURE: fix this to not use a second buffer */
752 1.124 dholland cp = PNBUF_GET();
753 1.117 dholland aiov.iov_base = cp;
754 1.117 dholland aiov.iov_len = MAXPATHLEN;
755 1.117 dholland auio.uio_iov = &aiov;
756 1.117 dholland auio.uio_iovcnt = 1;
757 1.117 dholland auio.uio_offset = 0;
758 1.117 dholland auio.uio_rw = UIO_READ;
759 1.117 dholland auio.uio_resid = MAXPATHLEN;
760 1.117 dholland UIO_SETUP_SYSSPACE(&auio);
761 1.161 dholland error = VOP_READLINK(foundobj, &auio, cnp->cn_cred);
762 1.208.6.2 martin VOP_UNLOCK(foundobj);
763 1.117 dholland if (error) {
764 1.124 dholland PNBUF_PUT(cp);
765 1.117 dholland return error;
766 1.117 dholland }
767 1.117 dholland linklen = MAXPATHLEN - auio.uio_resid;
768 1.117 dholland if (linklen == 0) {
769 1.124 dholland PNBUF_PUT(cp);
770 1.124 dholland return ENOENT;
771 1.117 dholland }
772 1.117 dholland
773 1.117 dholland /*
774 1.117 dholland * Do symlink substitution, if appropriate, and
775 1.117 dholland * check length for potential overflow.
776 1.134 dholland *
777 1.134 dholland * Inhibit symlink substitution for nfsd.
778 1.134 dholland * XXX: This is how it was before; is that a bug or a feature?
779 1.117 dholland */
780 1.134 dholland if ((!inhibitmagic && vfs_magiclinks &&
781 1.117 dholland symlink_magic(self->l_proc, cp, &linklen)) ||
782 1.117 dholland (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
783 1.124 dholland PNBUF_PUT(cp);
784 1.124 dholland return ENAMETOOLONG;
785 1.117 dholland }
786 1.117 dholland if (ndp->ni_pathlen > 1) {
787 1.124 dholland /* includes a null-terminator */
788 1.117 dholland memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
789 1.124 dholland } else {
790 1.124 dholland cp[linklen] = '\0';
791 1.124 dholland }
792 1.117 dholland ndp->ni_pathlen += linklen;
793 1.124 dholland memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
794 1.124 dholland PNBUF_PUT(cp);
795 1.167 dholland
796 1.167 dholland /* we're now starting from the beginning of the buffer again */
797 1.167 dholland cnp->cn_nameptr = ndp->ni_pnbuf;
798 1.117 dholland
799 1.117 dholland /*
800 1.117 dholland * Check if root directory should replace current directory.
801 1.117 dholland */
802 1.124 dholland if (ndp->ni_pnbuf[0] == '/') {
803 1.208.6.2 martin vrele(searchdir);
804 1.117 dholland /* Keep absolute symbolic links inside emulation root */
805 1.141 dholland searchdir = ndp->ni_erootdir;
806 1.141 dholland if (searchdir == NULL ||
807 1.124 dholland (ndp->ni_pnbuf[1] == '.'
808 1.124 dholland && ndp->ni_pnbuf[2] == '.'
809 1.124 dholland && ndp->ni_pnbuf[3] == '/')) {
810 1.117 dholland ndp->ni_erootdir = NULL;
811 1.141 dholland searchdir = ndp->ni_rootdir;
812 1.117 dholland }
813 1.141 dholland vref(searchdir);
814 1.186 dholland while (cnp->cn_nameptr[0] == '/') {
815 1.186 dholland cnp->cn_nameptr++;
816 1.186 dholland ndp->ni_pathlen--;
817 1.186 dholland }
818 1.117 dholland }
819 1.117 dholland
820 1.141 dholland *newsearchdir_ret = searchdir;
821 1.117 dholland return 0;
822 1.117 dholland }
823 1.117 dholland
824 1.117 dholland //////////////////////////////
825 1.117 dholland
826 1.39 lukem /*
827 1.173 dholland * Inspect the leading path component and update the state accordingly.
828 1.10 cgd */
829 1.118 dholland static int
830 1.118 dholland lookup_parsepath(struct namei_state *state)
831 1.118 dholland {
832 1.118 dholland const char *cp; /* pointer into pathname argument */
833 1.118 dholland
834 1.118 dholland struct componentname *cnp = state->cnp;
835 1.118 dholland struct nameidata *ndp = state->ndp;
836 1.118 dholland
837 1.118 dholland KASSERT(cnp == &ndp->ni_cnd);
838 1.118 dholland
839 1.10 cgd /*
840 1.10 cgd * Search a new directory.
841 1.10 cgd *
842 1.10 cgd * The last component of the filename is left accessible via
843 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
844 1.10 cgd * the name set the SAVENAME flag. When done, they assume
845 1.10 cgd * responsibility for freeing the pathname buffer.
846 1.127 yamt *
847 1.147 dholland * At this point, our only vnode state is that the search dir
848 1.208.6.2 martin * is held.
849 1.10 cgd */
850 1.12 mycroft cnp->cn_consume = 0;
851 1.197 dholland cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
852 1.197 dholland cp = cnp->cn_nameptr + cnp->cn_namelen;
853 1.191 christos if (cnp->cn_namelen > KERNEL_NAME_MAX) {
854 1.118 dholland return ENAMETOOLONG;
855 1.10 cgd }
856 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
857 1.10 cgd { char c = *cp;
858 1.41 soren *(char *)cp = '\0';
859 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
860 1.41 soren *(char *)cp = c; }
861 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
862 1.12 mycroft ndp->ni_pathlen -= cnp->cn_namelen;
863 1.10 cgd ndp->ni_next = cp;
864 1.23 mycroft /*
865 1.23 mycroft * If this component is followed by a slash, then move the pointer to
866 1.23 mycroft * the next component forward, and remember that this component must be
867 1.23 mycroft * a directory.
868 1.23 mycroft */
869 1.23 mycroft if (*cp == '/') {
870 1.23 mycroft do {
871 1.23 mycroft cp++;
872 1.23 mycroft } while (*cp == '/');
873 1.118 dholland state->slashes = cp - ndp->ni_next;
874 1.118 dholland ndp->ni_pathlen -= state->slashes;
875 1.23 mycroft ndp->ni_next = cp;
876 1.23 mycroft cnp->cn_flags |= REQUIREDIR;
877 1.23 mycroft } else {
878 1.118 dholland state->slashes = 0;
879 1.23 mycroft cnp->cn_flags &= ~REQUIREDIR;
880 1.23 mycroft }
881 1.23 mycroft /*
882 1.23 mycroft * We do special processing on the last component, whether or not it's
883 1.23 mycroft * a directory. Cache all intervening lookups, but not the final one.
884 1.23 mycroft */
885 1.23 mycroft if (*cp == '\0') {
886 1.118 dholland if (state->docache)
887 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
888 1.23 mycroft else
889 1.23 mycroft cnp->cn_flags &= ~MAKEENTRY;
890 1.23 mycroft cnp->cn_flags |= ISLASTCN;
891 1.23 mycroft } else {
892 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
893 1.23 mycroft cnp->cn_flags &= ~ISLASTCN;
894 1.23 mycroft }
895 1.12 mycroft if (cnp->cn_namelen == 2 &&
896 1.12 mycroft cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
897 1.12 mycroft cnp->cn_flags |= ISDOTDOT;
898 1.12 mycroft else
899 1.12 mycroft cnp->cn_flags &= ~ISDOTDOT;
900 1.10 cgd
901 1.118 dholland return 0;
902 1.118 dholland }
903 1.118 dholland
904 1.173 dholland /*
905 1.208.6.2 martin * Take care of crossing a mounted-on vnode. On error, foundobj_ret will be
906 1.208.6.2 martin * vrele'd, but searchdir is left alone.
907 1.208.6.2 martin */
908 1.208.6.2 martin static int
909 1.208.6.2 martin lookup_crossmount(struct namei_state *state,
910 1.208.6.2 martin struct vnode **searchdir_ret,
911 1.208.6.2 martin struct vnode **foundobj_ret,
912 1.208.6.2 martin bool *searchdir_locked)
913 1.208.6.2 martin {
914 1.208.6.2 martin struct componentname *cnp = state->cnp;
915 1.208.6.2 martin struct vnode *foundobj;
916 1.208.6.2 martin struct vnode *searchdir;
917 1.208.6.2 martin struct mount *mp;
918 1.208.6.2 martin int error, lktype;
919 1.208.6.2 martin
920 1.208.6.2 martin searchdir = *searchdir_ret;
921 1.208.6.2 martin foundobj = *foundobj_ret;
922 1.208.6.2 martin error = 0;
923 1.208.6.2 martin
924 1.208.6.2 martin KASSERT((cnp->cn_flags & NOCROSSMOUNT) == 0);
925 1.208.6.2 martin KASSERT(searchdir != NULL);
926 1.208.6.2 martin
927 1.208.6.2 martin /* First, unlock searchdir (oof). */
928 1.208.6.2 martin if (*searchdir_locked) {
929 1.208.6.2 martin lktype = VOP_ISLOCKED(searchdir);
930 1.208.6.2 martin VOP_UNLOCK(searchdir);
931 1.208.6.2 martin *searchdir_locked = false;
932 1.208.6.2 martin } else {
933 1.208.6.2 martin lktype = LK_NONE;
934 1.208.6.2 martin }
935 1.208.6.2 martin
936 1.208.6.2 martin /*
937 1.208.6.2 martin * Do an unlocked check to see if the vnode has been mounted on; if
938 1.208.6.2 martin * so find the root of the mounted file system.
939 1.208.6.2 martin */
940 1.208.6.2 martin while (foundobj->v_type == VDIR &&
941 1.208.6.2 martin (mp = foundobj->v_mountedhere) != NULL &&
942 1.208.6.2 martin (cnp->cn_flags & NOCROSSMOUNT) == 0) {
943 1.208.6.2 martin KASSERTMSG(searchdir != foundobj, "same vn %p", searchdir);
944 1.208.6.2 martin /*
945 1.208.6.2 martin * First get the vnode stable. LK_SHARED works brilliantly
946 1.208.6.2 martin * here because almost nothing else wants to lock the
947 1.208.6.2 martin * covered vnode.
948 1.208.6.2 martin */
949 1.208.6.2 martin error = vn_lock(foundobj, LK_SHARED);
950 1.208.6.2 martin if (error != 0) {
951 1.208.6.2 martin vrele(foundobj);
952 1.208.6.2 martin *foundobj_ret = NULL;
953 1.208.6.2 martin break;
954 1.208.6.2 martin }
955 1.208.6.2 martin
956 1.208.6.2 martin /* Then check to see if something is still mounted on it. */
957 1.208.6.2 martin if ((mp = foundobj->v_mountedhere) == NULL) {
958 1.208.6.2 martin VOP_UNLOCK(foundobj);
959 1.208.6.2 martin break;
960 1.208.6.2 martin }
961 1.208.6.2 martin
962 1.208.6.2 martin /* Get a reference to the mountpoint, and ditch foundobj. */
963 1.208.6.2 martin error = vfs_busy(mp);
964 1.208.6.2 martin vput(foundobj);
965 1.208.6.2 martin if (error != 0) {
966 1.208.6.2 martin *foundobj_ret = NULL;
967 1.208.6.2 martin break;
968 1.208.6.2 martin }
969 1.208.6.2 martin
970 1.208.6.2 martin /* Now get a reference on the root vnode, and drop mount. */
971 1.208.6.2 martin error = VFS_ROOT(mp, LK_NONE, &foundobj);
972 1.208.6.2 martin vfs_unbusy(mp);
973 1.208.6.2 martin if (error) {
974 1.208.6.2 martin *foundobj_ret = NULL;
975 1.208.6.2 martin break;
976 1.208.6.2 martin }
977 1.208.6.2 martin
978 1.208.6.2 martin /*
979 1.208.6.2 martin * Avoid locking vnodes from two filesystems because
980 1.208.6.2 martin * it's prone to deadlock, e.g. when using puffs.
981 1.208.6.2 martin * Also, it isn't a good idea to propagate slowness of
982 1.208.6.2 martin * a filesystem up to the root directory. For now,
983 1.208.6.2 martin * only handle the common case, where foundobj is
984 1.208.6.2 martin * VDIR.
985 1.208.6.2 martin *
986 1.208.6.2 martin * In this case set searchdir to null to avoid using
987 1.208.6.2 martin * it again. It is not correct to set searchdir ==
988 1.208.6.2 martin * foundobj here as that will confuse the caller.
989 1.208.6.2 martin * (See PR 40740.)
990 1.208.6.2 martin */
991 1.208.6.2 martin if (searchdir == NULL) {
992 1.208.6.2 martin /* already been here once; do nothing further */
993 1.208.6.2 martin } else if (foundobj->v_type == VDIR) {
994 1.208.6.2 martin vrele(searchdir);
995 1.208.6.2 martin *searchdir_ret = searchdir = NULL;
996 1.208.6.2 martin *foundobj_ret = foundobj;
997 1.208.6.2 martin lktype = LK_NONE;
998 1.208.6.2 martin }
999 1.208.6.2 martin }
1000 1.208.6.2 martin
1001 1.208.6.2 martin /* If searchdir is still around, re-lock it. */
1002 1.208.6.2 martin if (error == 0 && lktype != LK_NONE) {
1003 1.208.6.2 martin vn_lock(searchdir, lktype | LK_RETRY);
1004 1.208.6.2 martin *searchdir_locked = true;
1005 1.208.6.2 martin }
1006 1.208.6.2 martin return error;
1007 1.208.6.2 martin }
1008 1.208.6.2 martin
1009 1.208.6.2 martin /*
1010 1.173 dholland * Call VOP_LOOKUP for a single lookup; return a new search directory
1011 1.173 dholland * (used when crossing mountpoints up or searching union mounts down) and
1012 1.173 dholland * the found object, which for create operations may be NULL on success.
1013 1.204 dholland *
1014 1.204 dholland * Note that the new search directory may be null, which means the
1015 1.204 dholland * searchdir was unlocked and released. This happens in the common case
1016 1.204 dholland * when crossing a mount point downwards, in order to avoid coupling
1017 1.204 dholland * locks between different file system volumes. Importantly, this can
1018 1.204 dholland * happen even if the call fails. (XXX: this is gross and should be
1019 1.204 dholland * tidied somehow.)
1020 1.173 dholland */
1021 1.118 dholland static int
1022 1.147 dholland lookup_once(struct namei_state *state,
1023 1.147 dholland struct vnode *searchdir,
1024 1.150 dholland struct vnode **newsearchdir_ret,
1025 1.208.6.2 martin struct vnode **foundobj_ret,
1026 1.208.6.2 martin bool *newsearchdir_locked_ret)
1027 1.118 dholland {
1028 1.163 dholland struct vnode *tmpvn; /* scratch vnode */
1029 1.163 dholland struct vnode *foundobj; /* result */
1030 1.118 dholland struct lwp *l = curlwp;
1031 1.208.6.2 martin bool searchdir_locked = false;
1032 1.208.6.2 martin int error, lktype;
1033 1.118 dholland
1034 1.118 dholland struct componentname *cnp = state->cnp;
1035 1.118 dholland struct nameidata *ndp = state->ndp;
1036 1.118 dholland
1037 1.118 dholland KASSERT(cnp == &ndp->ni_cnd);
1038 1.154 dholland *newsearchdir_ret = searchdir;
1039 1.118 dholland
1040 1.10 cgd /*
1041 1.10 cgd * Handle "..": two special cases.
1042 1.10 cgd * 1. If at root directory (e.g. after chroot)
1043 1.12 mycroft * or at absolute root directory
1044 1.10 cgd * then ignore it so can't get out.
1045 1.85 dsl * 1a. If at the root of the emulation filesystem go to the real
1046 1.85 dsl * root. So "/../<path>" is always absolute.
1047 1.85 dsl * 1b. If we have somehow gotten out of a jail, warn
1048 1.40 wrstuden * and also ignore it so we can't get farther out.
1049 1.10 cgd * 2. If this vnode is the root of a mounted
1050 1.10 cgd * filesystem, then replace it with the
1051 1.10 cgd * vnode which was mounted on so we take the
1052 1.10 cgd * .. in the other file system.
1053 1.10 cgd */
1054 1.12 mycroft if (cnp->cn_flags & ISDOTDOT) {
1055 1.64 christos struct proc *p = l->l_proc;
1056 1.64 christos
1057 1.10 cgd for (;;) {
1058 1.154 dholland if (searchdir == ndp->ni_rootdir ||
1059 1.154 dholland searchdir == rootvnode) {
1060 1.147 dholland foundobj = searchdir;
1061 1.147 dholland vref(foundobj);
1062 1.147 dholland *foundobj_ret = foundobj;
1063 1.175 yamt error = 0;
1064 1.175 yamt goto done;
1065 1.40 wrstuden }
1066 1.40 wrstuden if (ndp->ni_rootdir != rootvnode) {
1067 1.40 wrstuden int retval;
1068 1.73 chs
1069 1.147 dholland retval = vn_isunder(searchdir, ndp->ni_rootdir, l);
1070 1.40 wrstuden if (!retval) {
1071 1.40 wrstuden /* Oops! We got out of jail! */
1072 1.40 wrstuden log(LOG_WARNING,
1073 1.40 wrstuden "chrooted pid %d uid %d (%s) "
1074 1.40 wrstuden "detected outside of its chroot\n",
1075 1.71 ad p->p_pid, kauth_cred_geteuid(l->l_cred),
1076 1.64 christos p->p_comm);
1077 1.40 wrstuden /* Put us at the jail root. */
1078 1.208.6.2 martin vrele(searchdir);
1079 1.147 dholland searchdir = NULL;
1080 1.147 dholland foundobj = ndp->ni_rootdir;
1081 1.147 dholland vref(foundobj);
1082 1.147 dholland vref(foundobj);
1083 1.150 dholland *newsearchdir_ret = foundobj;
1084 1.147 dholland *foundobj_ret = foundobj;
1085 1.175 yamt error = 0;
1086 1.175 yamt goto done;
1087 1.40 wrstuden }
1088 1.10 cgd }
1089 1.147 dholland if ((searchdir->v_vflag & VV_ROOT) == 0 ||
1090 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT))
1091 1.10 cgd break;
1092 1.163 dholland tmpvn = searchdir;
1093 1.147 dholland searchdir = searchdir->v_mount->mnt_vnodecovered;
1094 1.153 dholland vref(searchdir);
1095 1.208.6.2 martin vrele(tmpvn);
1096 1.154 dholland *newsearchdir_ret = searchdir;
1097 1.10 cgd }
1098 1.10 cgd }
1099 1.10 cgd
1100 1.10 cgd /*
1101 1.208.6.2 martin * If the file system supports VOP_LOOKUP() with a shared lock, and
1102 1.208.6.2 martin * we are not making any modifications (nameiop LOOKUP) or this is
1103 1.208.6.2 martin * not the last component then get a shared lock. Where we can't do
1104 1.208.6.2 martin * fast-forwarded lookups (for example with layered file systems)
1105 1.208.6.2 martin * then this is the fallback for reducing lock contention.
1106 1.208.6.2 martin */
1107 1.208.6.2 martin if ((searchdir->v_mount->mnt_iflag & IMNT_SHRLOOKUP) != 0 &&
1108 1.208.6.2 martin (cnp->cn_nameiop == LOOKUP || (cnp->cn_flags & ISLASTCN) == 0)) {
1109 1.208.6.2 martin lktype = LK_SHARED;
1110 1.208.6.2 martin } else {
1111 1.208.6.2 martin lktype = LK_EXCLUSIVE;
1112 1.208.6.2 martin }
1113 1.208.6.2 martin
1114 1.208.6.2 martin /*
1115 1.10 cgd * We now have a segment name to search for, and a directory to search.
1116 1.208.6.2 martin * Our vnode state here is that "searchdir" is held.
1117 1.10 cgd */
1118 1.12 mycroft unionlookup:
1119 1.148 dholland foundobj = NULL;
1120 1.208.6.2 martin if (!searchdir_locked) {
1121 1.208.6.2 martin vn_lock(searchdir, lktype | LK_RETRY);
1122 1.208.6.2 martin searchdir_locked = true;
1123 1.208.6.2 martin }
1124 1.148 dholland error = VOP_LOOKUP(searchdir, &foundobj, cnp);
1125 1.154 dholland
1126 1.73 chs if (error != 0) {
1127 1.205 riastrad KASSERTMSG((foundobj == NULL),
1128 1.205 riastrad "leaf `%s' should be empty but is %p",
1129 1.205 riastrad cnp->cn_nameptr, foundobj);
1130 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
1131 1.19 christos printf("not found\n");
1132 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
1133 1.208.6.2 martin
1134 1.208.6.2 martin /*
1135 1.208.6.2 martin * If ENOLCK, the file system needs us to retry the lookup
1136 1.208.6.2 martin * with an exclusive lock. It's likely nothing was found in
1137 1.208.6.2 martin * cache and/or modifications need to be made.
1138 1.208.6.2 martin */
1139 1.208.6.2 martin if (error == ENOLCK) {
1140 1.208.6.2 martin KASSERT(VOP_ISLOCKED(searchdir) == LK_SHARED);
1141 1.208.6.2 martin KASSERT(searchdir_locked);
1142 1.208.6.2 martin if (vn_lock(searchdir, LK_UPGRADE | LK_NOWAIT)) {
1143 1.208.6.2 martin VOP_UNLOCK(searchdir);
1144 1.208.6.2 martin searchdir_locked = false;
1145 1.208.6.2 martin }
1146 1.208.6.2 martin lktype = LK_EXCLUSIVE;
1147 1.208.6.2 martin goto unionlookup;
1148 1.208.6.2 martin }
1149 1.208.6.2 martin
1150 1.12 mycroft if ((error == ENOENT) &&
1151 1.147 dholland (searchdir->v_vflag & VV_ROOT) &&
1152 1.147 dholland (searchdir->v_mount->mnt_flag & MNT_UNION)) {
1153 1.163 dholland tmpvn = searchdir;
1154 1.147 dholland searchdir = searchdir->v_mount->mnt_vnodecovered;
1155 1.153 dholland vref(searchdir);
1156 1.163 dholland vput(tmpvn);
1157 1.208.6.2 martin searchdir_locked = false;
1158 1.154 dholland *newsearchdir_ret = searchdir;
1159 1.12 mycroft goto unionlookup;
1160 1.10 cgd }
1161 1.12 mycroft
1162 1.10 cgd if (error != EJUSTRETURN)
1163 1.175 yamt goto done;
1164 1.73 chs
1165 1.10 cgd /*
1166 1.23 mycroft * If this was not the last component, or there were trailing
1167 1.51 christos * slashes, and we are not going to create a directory,
1168 1.51 christos * then the name must exist.
1169 1.23 mycroft */
1170 1.51 christos if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
1171 1.175 yamt error = ENOENT;
1172 1.175 yamt goto done;
1173 1.23 mycroft }
1174 1.73 chs
1175 1.23 mycroft /*
1176 1.10 cgd * If creating and at end of pathname, then can consider
1177 1.10 cgd * allowing file to be created.
1178 1.10 cgd */
1179 1.118 dholland if (state->rdonly) {
1180 1.175 yamt error = EROFS;
1181 1.175 yamt goto done;
1182 1.10 cgd }
1183 1.73 chs
1184 1.10 cgd /*
1185 1.166 dholland * We return success and a NULL foundobj to indicate
1186 1.166 dholland * that the entry doesn't currently exist, leaving a
1187 1.173 dholland * pointer to the (normally, locked) directory vnode
1188 1.173 dholland * as searchdir.
1189 1.10 cgd */
1190 1.147 dholland *foundobj_ret = NULL;
1191 1.175 yamt error = 0;
1192 1.175 yamt goto done;
1193 1.10 cgd }
1194 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
1195 1.19 christos printf("found\n");
1196 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
1197 1.10 cgd
1198 1.12 mycroft /*
1199 1.23 mycroft * Take into account any additional components consumed by the
1200 1.23 mycroft * underlying filesystem. This will include any trailing slashes after
1201 1.23 mycroft * the last component consumed.
1202 1.12 mycroft */
1203 1.12 mycroft if (cnp->cn_consume > 0) {
1204 1.118 dholland ndp->ni_pathlen -= cnp->cn_consume - state->slashes;
1205 1.118 dholland ndp->ni_next += cnp->cn_consume - state->slashes;
1206 1.12 mycroft cnp->cn_consume = 0;
1207 1.23 mycroft if (ndp->ni_next[0] == '\0')
1208 1.23 mycroft cnp->cn_flags |= ISLASTCN;
1209 1.12 mycroft }
1210 1.12 mycroft
1211 1.208.6.2 martin /* Unlock, unless the caller needs the parent locked. */
1212 1.208.6.2 martin if (searchdir != NULL) {
1213 1.208.6.2 martin KASSERT(searchdir_locked);
1214 1.208.6.2 martin if ((cnp->cn_flags & (ISLASTCN | LOCKPARENT)) !=
1215 1.208.6.2 martin (ISLASTCN | LOCKPARENT)) {
1216 1.208.6.2 martin VOP_UNLOCK(searchdir);
1217 1.208.6.2 martin searchdir_locked = false;
1218 1.201 hannken }
1219 1.208.6.2 martin } else {
1220 1.208.6.2 martin KASSERT(!searchdir_locked);
1221 1.201 hannken }
1222 1.73 chs
1223 1.208.6.2 martin *foundobj_ret = foundobj;
1224 1.208.6.2 martin error = 0;
1225 1.208.6.2 martin done:
1226 1.208.6.2 martin *newsearchdir_locked_ret = searchdir_locked;
1227 1.208.6.2 martin return error;
1228 1.208.6.2 martin }
1229 1.204 dholland
1230 1.208.6.2 martin /*
1231 1.208.6.2 martin * Parse out the first path name component that we need to to consider.
1232 1.208.6.2 martin *
1233 1.208.6.2 martin * While doing this, attempt to use the name cache to fast-forward through
1234 1.208.6.2 martin * as many "easy" to find components of the path as possible.
1235 1.208.6.2 martin *
1236 1.208.6.2 martin * We use the namecache's node locks to form a chain, and avoid as many
1237 1.208.6.2 martin * vnode references and locks as possible. In the ideal case, only the
1238 1.208.6.2 martin * final vnode will have its reference count adjusted and lock taken.
1239 1.208.6.2 martin */
1240 1.208.6.2 martin static int
1241 1.208.6.2 martin lookup_fastforward(struct namei_state *state, struct vnode **searchdir_ret,
1242 1.208.6.2 martin struct vnode **foundobj_ret)
1243 1.208.6.2 martin {
1244 1.208.6.2 martin struct componentname *cnp = state->cnp;
1245 1.208.6.2 martin struct nameidata *ndp = state->ndp;
1246 1.208.6.2 martin krwlock_t *plock;
1247 1.208.6.2 martin struct vnode *foundobj, *searchdir;
1248 1.208.6.2 martin int error, error2;
1249 1.208.6.2 martin size_t oldpathlen;
1250 1.208.6.2 martin const char *oldnameptr;
1251 1.204 dholland
1252 1.208.6.2 martin /*
1253 1.208.6.2 martin * Eat as many path name components as possible before giving up and
1254 1.208.6.2 martin * letting lookup_once() handle it. Remember the starting point in
1255 1.208.6.2 martin * case we can't get vnode references and need to roll back.
1256 1.208.6.2 martin */
1257 1.208.6.2 martin plock = NULL;
1258 1.208.6.2 martin searchdir = *searchdir_ret;
1259 1.208.6.2 martin oldnameptr = cnp->cn_nameptr;
1260 1.208.6.2 martin oldpathlen = ndp->ni_pathlen;
1261 1.208.6.2 martin for (;;) {
1262 1.208.6.2 martin foundobj = NULL;
1263 1.208.6.2 martin
1264 1.208.6.2 martin /*
1265 1.208.6.2 martin * Get the next component name. There should be no slashes
1266 1.208.6.2 martin * here, and we shouldn't have looped around if we were
1267 1.208.6.2 martin * done.
1268 1.208.6.2 martin */
1269 1.208.6.2 martin KASSERT(cnp->cn_nameptr[0] != '/');
1270 1.208.6.2 martin KASSERT(cnp->cn_nameptr[0] != '\0');
1271 1.208.6.2 martin if ((error = lookup_parsepath(state)) != 0) {
1272 1.208.6.2 martin break;
1273 1.107 ad }
1274 1.208.6.2 martin
1275 1.208.6.2 martin /*
1276 1.208.6.2 martin * Can't deal with dotdot lookups, because it means lock
1277 1.208.6.2 martin * order reversal, and there are checks in lookup_once()
1278 1.208.6.2 martin * that need to be made. Also check for missing mountpoints.
1279 1.208.6.2 martin */
1280 1.208.6.2 martin if ((cnp->cn_flags & ISDOTDOT) != 0 ||
1281 1.208.6.2 martin searchdir->v_mount == NULL) {
1282 1.208.6.2 martin error = EOPNOTSUPP;
1283 1.208.6.2 martin break;
1284 1.190 yamt }
1285 1.208.6.2 martin
1286 1.208.6.2 martin /*
1287 1.208.6.2 martin * Can't deal with last component when modifying; this needs
1288 1.208.6.2 martin * searchdir locked and VOP_LOOKUP() called (which can and
1289 1.208.6.2 martin * does modify state, despite the name).
1290 1.208.6.2 martin */
1291 1.208.6.2 martin if ((cnp->cn_flags & ISLASTCN) != 0) {
1292 1.208.6.2 martin if (cnp->cn_nameiop != LOOKUP ||
1293 1.208.6.2 martin (cnp->cn_flags & LOCKPARENT) != 0) {
1294 1.208.6.2 martin error = EOPNOTSUPP;
1295 1.208.6.2 martin break;
1296 1.204 dholland }
1297 1.32 wrstuden }
1298 1.208.6.2 martin
1299 1.208.6.2 martin /* Can't deal with -o union lookups. */
1300 1.208.6.2 martin if ((searchdir->v_vflag & VV_ROOT) != 0 &&
1301 1.208.6.2 martin (searchdir->v_mount->mnt_flag & MNT_UNION) != 0) {
1302 1.208.6.2 martin error = EOPNOTSUPP;
1303 1.208.6.2 martin break;
1304 1.208.6.2 martin }
1305 1.208.6.2 martin
1306 1.190 yamt /*
1307 1.208.6.2 martin * Good, now look for it in cache. cache_lookup_linked()
1308 1.208.6.2 martin * will fail if there's nothing there, or if there's no
1309 1.208.6.2 martin * ownership info for the directory, or if the user doesn't
1310 1.208.6.2 martin * have permission to look up files in this directory.
1311 1.208.6.2 martin */
1312 1.208.6.2 martin if (!cache_lookup_linked(searchdir, cnp->cn_nameptr,
1313 1.208.6.2 martin cnp->cn_namelen, &foundobj, &plock, cnp->cn_cred)) {
1314 1.208.6.2 martin error = EOPNOTSUPP;
1315 1.208.6.2 martin break;
1316 1.208.6.2 martin }
1317 1.208.6.2 martin KASSERT(plock != NULL && rw_lock_held(plock));
1318 1.208.6.2 martin
1319 1.208.6.2 martin /* Scored a hit. Negative is good too (ENOENT). */
1320 1.208.6.2 martin if (foundobj == NULL) {
1321 1.208.6.2 martin error = ENOENT;
1322 1.208.6.2 martin break;
1323 1.208.6.2 martin }
1324 1.208.6.2 martin
1325 1.208.6.2 martin /*
1326 1.208.6.2 martin * Stop and get a hold on the vnode if there's something
1327 1.208.6.2 martin * that can't be handled here:
1328 1.204 dholland *
1329 1.208.6.2 martin * - we've reached the last component.
1330 1.208.6.2 martin * - or encountered a mount point that needs to be crossed.
1331 1.208.6.2 martin * - or encountered something other than a directory.
1332 1.190 yamt */
1333 1.208.6.2 martin if ((cnp->cn_flags & ISLASTCN) != 0 ||
1334 1.208.6.2 martin foundobj->v_type != VDIR ||
1335 1.208.6.2 martin (foundobj->v_type == VDIR &&
1336 1.208.6.2 martin foundobj->v_mountedhere != NULL)) {
1337 1.208.6.2 martin mutex_enter(foundobj->v_interlock);
1338 1.208.6.2 martin error = vcache_tryvget(foundobj);
1339 1.208.6.2 martin /* v_interlock now unheld */
1340 1.208.6.2 martin if (error != 0) {
1341 1.208.6.2 martin foundobj = NULL;
1342 1.208.6.2 martin }
1343 1.208.6.2 martin break;
1344 1.208.6.2 martin }
1345 1.208.6.2 martin
1346 1.208.6.2 martin /*
1347 1.208.6.2 martin * Otherwise, we're still in business. Set the found VDIR
1348 1.208.6.2 martin * vnode as the search dir for the next component and
1349 1.208.6.2 martin * continue on to it.
1350 1.208.6.2 martin */
1351 1.208.6.2 martin cnp->cn_nameptr = ndp->ni_next;
1352 1.208.6.2 martin searchdir = foundobj;
1353 1.208.6.2 martin }
1354 1.208.6.2 martin
1355 1.208.6.2 martin /*
1356 1.208.6.2 martin * If we ended up with a new search dir, ref it before dropping the
1357 1.208.6.2 martin * namecache's lock. The lock prevents both searchdir and foundobj
1358 1.208.6.2 martin * from disappearing. If we can't ref the new searchdir, we have a
1359 1.208.6.2 martin * bit of a problem. Roll back the fastforward to the beginning and
1360 1.208.6.2 martin * let lookup_once() take care of it.
1361 1.208.6.2 martin */
1362 1.208.6.2 martin if (searchdir != *searchdir_ret) {
1363 1.208.6.2 martin mutex_enter(searchdir->v_interlock);
1364 1.208.6.2 martin error2 = vcache_tryvget(searchdir);
1365 1.208.6.2 martin /* v_interlock now unheld */
1366 1.208.6.2 martin KASSERT(plock != NULL);
1367 1.208.6.2 martin rw_exit(plock);
1368 1.208.6.2 martin if (__predict_true(error2 == 0)) {
1369 1.208.6.2 martin /* Returning new searchdir, and maybe new foundobj. */
1370 1.208.6.2 martin vrele(*searchdir_ret);
1371 1.208.6.2 martin *searchdir_ret = searchdir;
1372 1.190 yamt } else {
1373 1.208.6.2 martin /* Returning nothing. */
1374 1.208.6.2 martin if (foundobj != NULL) {
1375 1.208.6.2 martin vrele(foundobj);
1376 1.208.6.2 martin foundobj = NULL;
1377 1.208.6.2 martin }
1378 1.208.6.2 martin cnp->cn_nameptr = oldnameptr;
1379 1.208.6.2 martin ndp->ni_pathlen = oldpathlen;
1380 1.208.6.2 martin error = lookup_parsepath(state);
1381 1.208.6.2 martin if (error == 0) {
1382 1.208.6.2 martin error = EOPNOTSUPP;
1383 1.208.6.2 martin }
1384 1.190 yamt }
1385 1.208.6.2 martin } else if (plock != NULL) {
1386 1.208.6.2 martin /* Drop any namecache lock still held. */
1387 1.208.6.2 martin rw_exit(plock);
1388 1.14 mycroft }
1389 1.14 mycroft
1390 1.208.6.2 martin KASSERT(error == 0 ? foundobj != NULL : foundobj == NULL);
1391 1.147 dholland *foundobj_ret = foundobj;
1392 1.175 yamt return error;
1393 1.118 dholland }
1394 1.118 dholland
1395 1.131 dholland //////////////////////////////
1396 1.131 dholland
1397 1.173 dholland /*
1398 1.173 dholland * Do a complete path search from a single root directory.
1399 1.173 dholland * (This is called up to twice if TRYEMULROOT is in effect.)
1400 1.173 dholland */
1401 1.131 dholland static int
1402 1.196 dholland namei_oneroot(struct namei_state *state,
1403 1.193 dholland int neverfollow, int inhibitmagic, int isnfsd)
1404 1.131 dholland {
1405 1.131 dholland struct nameidata *ndp = state->ndp;
1406 1.131 dholland struct componentname *cnp = state->cnp;
1407 1.146 dholland struct vnode *searchdir, *foundobj;
1408 1.208.6.2 martin bool searchdir_locked = false;
1409 1.137 dholland int error;
1410 1.131 dholland
1411 1.196 dholland error = namei_start(state, isnfsd, &searchdir);
1412 1.131 dholland if (error) {
1413 1.164 dholland ndp->ni_dvp = NULL;
1414 1.164 dholland ndp->ni_vp = NULL;
1415 1.131 dholland return error;
1416 1.131 dholland }
1417 1.185 dholland KASSERT(searchdir->v_type == VDIR);
1418 1.131 dholland
1419 1.133 dholland /*
1420 1.139 dholland * Setup: break out flag bits into variables.
1421 1.139 dholland */
1422 1.139 dholland state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1423 1.139 dholland if (cnp->cn_nameiop == DELETE)
1424 1.139 dholland state->docache = 0;
1425 1.139 dholland state->rdonly = cnp->cn_flags & RDONLY;
1426 1.139 dholland
1427 1.139 dholland /*
1428 1.133 dholland * Keep going until we run out of path components.
1429 1.133 dholland */
1430 1.139 dholland cnp->cn_nameptr = ndp->ni_pnbuf;
1431 1.185 dholland
1432 1.185 dholland /* drop leading slashes (already used them to choose startdir) */
1433 1.185 dholland while (cnp->cn_nameptr[0] == '/') {
1434 1.185 dholland cnp->cn_nameptr++;
1435 1.185 dholland ndp->ni_pathlen--;
1436 1.185 dholland }
1437 1.185 dholland /* was it just "/"? */
1438 1.185 dholland if (cnp->cn_nameptr[0] == '\0') {
1439 1.185 dholland foundobj = searchdir;
1440 1.185 dholland searchdir = NULL;
1441 1.185 dholland cnp->cn_flags |= ISLASTCN;
1442 1.185 dholland
1443 1.185 dholland /* bleh */
1444 1.185 dholland goto skiploop;
1445 1.185 dholland }
1446 1.185 dholland
1447 1.131 dholland for (;;) {
1448 1.204 dholland KASSERT(searchdir != NULL);
1449 1.208.6.2 martin KASSERT(!searchdir_locked);
1450 1.133 dholland
1451 1.133 dholland /*
1452 1.208.6.2 martin * Parse out the first path name component that we need to
1453 1.208.6.2 martin * to consider. While doing this, attempt to use the name
1454 1.208.6.2 martin * cache to fast-forward through as many "easy" to find
1455 1.208.6.2 martin * components of the path as possible.
1456 1.133 dholland */
1457 1.208.6.2 martin error = lookup_fastforward(state, &searchdir, &foundobj);
1458 1.133 dholland
1459 1.133 dholland /*
1460 1.208.6.2 martin * If we didn't get a good answer from the namecache, then
1461 1.208.6.2 martin * go directly to the file system.
1462 1.133 dholland */
1463 1.208.6.2 martin if (error != 0 && error != ENOENT) {
1464 1.208.6.2 martin error = lookup_once(state, searchdir, &searchdir,
1465 1.208.6.2 martin &foundobj, &searchdir_locked);
1466 1.208.6.2 martin }
1467 1.138 dholland
1468 1.208.6.2 martin /*
1469 1.208.6.2 martin * If the vnode we found is mounted on, then cross the mount
1470 1.208.6.2 martin * and get the root vnode in foundobj. If this encounters
1471 1.208.6.2 martin * an error, it will dispose of foundobj, but searchdir is
1472 1.208.6.2 martin * untouched.
1473 1.208.6.2 martin */
1474 1.208.6.2 martin if (error == 0 && foundobj != NULL &&
1475 1.208.6.2 martin foundobj->v_type == VDIR &&
1476 1.208.6.2 martin foundobj->v_mountedhere != NULL &&
1477 1.208.6.2 martin (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1478 1.208.6.2 martin error = lookup_crossmount(state, &searchdir,
1479 1.208.6.2 martin &foundobj, &searchdir_locked);
1480 1.131 dholland }
1481 1.138 dholland
1482 1.139 dholland if (error) {
1483 1.204 dholland if (searchdir != NULL) {
1484 1.208.6.2 martin if (searchdir_locked) {
1485 1.208.6.2 martin searchdir_locked = false;
1486 1.208.6.2 martin vput(searchdir);
1487 1.208.6.2 martin } else {
1488 1.208.6.2 martin vrele(searchdir);
1489 1.208.6.2 martin }
1490 1.204 dholland }
1491 1.168 dholland ndp->ni_dvp = NULL;
1492 1.139 dholland ndp->ni_vp = NULL;
1493 1.138 dholland /*
1494 1.139 dholland * Note that if we're doing TRYEMULROOT we can
1495 1.139 dholland * retry with the normal root. Where this is
1496 1.139 dholland * currently set matches previous practice,
1497 1.139 dholland * but the previous practice didn't make much
1498 1.139 dholland * sense and somebody should sit down and
1499 1.139 dholland * figure out which cases should cause retry
1500 1.139 dholland * and which shouldn't. XXX.
1501 1.138 dholland */
1502 1.139 dholland state->attempt_retry = 1;
1503 1.139 dholland return (error);
1504 1.139 dholland }
1505 1.157 dholland
1506 1.162 dholland if (foundobj == NULL) {
1507 1.162 dholland /*
1508 1.162 dholland * Success with no object returned means we're
1509 1.162 dholland * creating something and it isn't already
1510 1.181 dholland * there. Break out of the main loop now so
1511 1.162 dholland * the code below doesn't have to test for
1512 1.162 dholland * foundobj == NULL.
1513 1.162 dholland */
1514 1.204 dholland /* lookup_once can't have dropped the searchdir */
1515 1.204 dholland KASSERT(searchdir != NULL);
1516 1.181 dholland break;
1517 1.138 dholland }
1518 1.131 dholland
1519 1.131 dholland /*
1520 1.139 dholland * Check for symbolic link. If we've reached one,
1521 1.139 dholland * follow it, unless we aren't supposed to. Back up
1522 1.139 dholland * over any slashes that we skipped, as we will need
1523 1.139 dholland * them again.
1524 1.131 dholland */
1525 1.146 dholland if (namei_atsymlink(state, foundobj)) {
1526 1.208.6.2 martin /* Don't need searchdir locked any more. */
1527 1.208.6.2 martin if (searchdir_locked) {
1528 1.208.6.2 martin searchdir_locked = false;
1529 1.208.6.2 martin VOP_UNLOCK(searchdir);
1530 1.208.6.2 martin }
1531 1.139 dholland ndp->ni_pathlen += state->slashes;
1532 1.139 dholland ndp->ni_next -= state->slashes;
1533 1.134 dholland if (neverfollow) {
1534 1.134 dholland error = EINVAL;
1535 1.204 dholland } else if (searchdir == NULL) {
1536 1.204 dholland /*
1537 1.204 dholland * dholland 20160410: lookup_once only
1538 1.204 dholland * drops searchdir if it crossed a
1539 1.204 dholland * mount point. Therefore, if we get
1540 1.204 dholland * here it means we crossed a mount
1541 1.204 dholland * point to a mounted filesystem whose
1542 1.204 dholland * root vnode is a symlink. In theory
1543 1.204 dholland * we could continue at this point by
1544 1.204 dholland * using the pre-crossing searchdir
1545 1.204 dholland * (e.g. just take out an extra
1546 1.204 dholland * reference on it before calling
1547 1.204 dholland * lookup_once so we still have it),
1548 1.204 dholland * but this will make an ugly mess and
1549 1.204 dholland * it should never happen in practice
1550 1.204 dholland * as only badly broken filesystems
1551 1.204 dholland * have non-directory root vnodes. (I
1552 1.204 dholland * have seen this sort of thing with
1553 1.204 dholland * NFS occasionally but even then it
1554 1.204 dholland * means something's badly wrong.)
1555 1.204 dholland */
1556 1.204 dholland error = ENOTDIR;
1557 1.134 dholland } else {
1558 1.152 dholland /*
1559 1.152 dholland * dholland 20110410: if we're at a
1560 1.152 dholland * union mount it might make sense to
1561 1.152 dholland * use the top of the union stack here
1562 1.152 dholland * rather than the layer we found the
1563 1.152 dholland * symlink in. (FUTURE)
1564 1.152 dholland */
1565 1.141 dholland error = namei_follow(state, inhibitmagic,
1566 1.165 dholland searchdir, foundobj,
1567 1.152 dholland &searchdir);
1568 1.134 dholland }
1569 1.131 dholland if (error) {
1570 1.165 dholland KASSERT(searchdir != foundobj);
1571 1.204 dholland if (searchdir != NULL) {
1572 1.208.6.2 martin vrele(searchdir);
1573 1.204 dholland }
1574 1.208.6.2 martin vrele(foundobj);
1575 1.168 dholland ndp->ni_dvp = NULL;
1576 1.131 dholland ndp->ni_vp = NULL;
1577 1.131 dholland return error;
1578 1.131 dholland }
1579 1.174 jakllsch vrele(foundobj);
1580 1.167 dholland foundobj = NULL;
1581 1.189 riastrad
1582 1.189 riastrad /*
1583 1.189 riastrad * If we followed a symlink to `/' and there
1584 1.189 riastrad * are no more components after the symlink,
1585 1.189 riastrad * we're done with the loop and what we found
1586 1.189 riastrad * is the searchdir.
1587 1.189 riastrad */
1588 1.189 riastrad if (cnp->cn_nameptr[0] == '\0') {
1589 1.204 dholland KASSERT(searchdir != NULL);
1590 1.189 riastrad foundobj = searchdir;
1591 1.189 riastrad searchdir = NULL;
1592 1.189 riastrad cnp->cn_flags |= ISLASTCN;
1593 1.189 riastrad break;
1594 1.189 riastrad }
1595 1.189 riastrad
1596 1.139 dholland continue;
1597 1.139 dholland }
1598 1.139 dholland
1599 1.139 dholland /*
1600 1.183 dholland * Not a symbolic link.
1601 1.183 dholland *
1602 1.139 dholland * Check for directory, if the component was
1603 1.139 dholland * followed by a series of slashes.
1604 1.139 dholland */
1605 1.190 yamt if ((foundobj->v_type != VDIR) &&
1606 1.190 yamt (cnp->cn_flags & REQUIREDIR)) {
1607 1.204 dholland KASSERT(foundobj != searchdir);
1608 1.204 dholland if (searchdir) {
1609 1.208.6.2 martin if (searchdir_locked) {
1610 1.208.6.2 martin searchdir_locked = false;
1611 1.208.6.2 martin vput(searchdir);
1612 1.208.6.2 martin } else {
1613 1.208.6.2 martin vrele(searchdir);
1614 1.208.6.2 martin }
1615 1.208.6.2 martin } else {
1616 1.208.6.2 martin KASSERT(!searchdir_locked);
1617 1.139 dholland }
1618 1.208.6.2 martin vrele(foundobj);
1619 1.168 dholland ndp->ni_dvp = NULL;
1620 1.168 dholland ndp->ni_vp = NULL;
1621 1.139 dholland state->attempt_retry = 1;
1622 1.139 dholland return ENOTDIR;
1623 1.139 dholland }
1624 1.139 dholland
1625 1.139 dholland /*
1626 1.183 dholland * Stop if we've reached the last component.
1627 1.139 dholland */
1628 1.183 dholland if (cnp->cn_flags & ISLASTCN) {
1629 1.183 dholland break;
1630 1.139 dholland }
1631 1.139 dholland
1632 1.183 dholland /*
1633 1.183 dholland * Continue with the next component.
1634 1.183 dholland */
1635 1.183 dholland cnp->cn_nameptr = ndp->ni_next;
1636 1.208.6.2 martin if (searchdir != NULL) {
1637 1.208.6.2 martin if (searchdir_locked) {
1638 1.208.6.2 martin searchdir_locked = false;
1639 1.208.6.2 martin vput(searchdir);
1640 1.208.6.2 martin } else {
1641 1.208.6.2 martin vrele(searchdir);
1642 1.208.6.2 martin }
1643 1.183 dholland }
1644 1.183 dholland searchdir = foundobj;
1645 1.183 dholland foundobj = NULL;
1646 1.179 dholland }
1647 1.179 dholland
1648 1.208.6.2 martin KASSERT((cnp->cn_flags & LOCKPARENT) == 0 || searchdir == NULL ||
1649 1.208.6.2 martin VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
1650 1.208.6.2 martin
1651 1.185 dholland skiploop:
1652 1.185 dholland
1653 1.182 dholland if (foundobj != NULL) {
1654 1.146 dholland if (foundobj == ndp->ni_erootdir) {
1655 1.139 dholland /*
1656 1.139 dholland * We are about to return the emulation root.
1657 1.139 dholland * This isn't a good idea because code might
1658 1.139 dholland * repeatedly lookup ".." until the file
1659 1.139 dholland * matches that returned for "/" and loop
1660 1.139 dholland * forever. So convert it to the real root.
1661 1.139 dholland */
1662 1.170 dholland if (searchdir != NULL) {
1663 1.208.6.2 martin if (searchdir_locked) {
1664 1.157 dholland vput(searchdir);
1665 1.208.6.2 martin searchdir_locked = false;
1666 1.208.6.2 martin } else {
1667 1.208.6.2 martin vrele(searchdir);
1668 1.208.6.2 martin }
1669 1.170 dholland searchdir = NULL;
1670 1.170 dholland }
1671 1.208.6.2 martin vrele(foundobj);
1672 1.146 dholland foundobj = ndp->ni_rootdir;
1673 1.146 dholland vref(foundobj);
1674 1.131 dholland }
1675 1.139 dholland
1676 1.139 dholland /*
1677 1.158 dholland * If the caller requested the parent node (i.e. it's
1678 1.158 dholland * a CREATE, DELETE, or RENAME), and we don't have one
1679 1.158 dholland * (because this is the root directory, or we crossed
1680 1.158 dholland * a mount point), then we must fail.
1681 1.139 dholland */
1682 1.158 dholland if (cnp->cn_nameiop != LOOKUP &&
1683 1.158 dholland (searchdir == NULL ||
1684 1.158 dholland searchdir->v_mount != foundobj->v_mount)) {
1685 1.170 dholland if (searchdir) {
1686 1.208.6.2 martin if (searchdir_locked) {
1687 1.208.6.2 martin vput(searchdir);
1688 1.208.6.2 martin searchdir_locked = false;
1689 1.208.6.2 martin } else {
1690 1.208.6.2 martin vrele(searchdir);
1691 1.208.6.2 martin }
1692 1.208.6.2 martin searchdir = NULL;
1693 1.170 dholland }
1694 1.208.6.2 martin vrele(foundobj);
1695 1.170 dholland foundobj = NULL;
1696 1.170 dholland ndp->ni_dvp = NULL;
1697 1.170 dholland ndp->ni_vp = NULL;
1698 1.170 dholland state->attempt_retry = 1;
1699 1.170 dholland
1700 1.139 dholland switch (cnp->cn_nameiop) {
1701 1.139 dholland case CREATE:
1702 1.171 dholland return EEXIST;
1703 1.139 dholland case DELETE:
1704 1.139 dholland case RENAME:
1705 1.171 dholland return EBUSY;
1706 1.171 dholland default:
1707 1.139 dholland break;
1708 1.139 dholland }
1709 1.171 dholland panic("Invalid nameiop\n");
1710 1.139 dholland }
1711 1.139 dholland
1712 1.139 dholland /*
1713 1.139 dholland * Disallow directory write attempts on read-only lookups.
1714 1.139 dholland * Prefers EEXIST over EROFS for the CREATE case.
1715 1.139 dholland */
1716 1.139 dholland if (state->rdonly &&
1717 1.139 dholland (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1718 1.157 dholland if (searchdir) {
1719 1.208.6.2 martin if (searchdir_locked) {
1720 1.170 dholland vput(searchdir);
1721 1.208.6.2 martin searchdir_locked = false;
1722 1.170 dholland } else {
1723 1.170 dholland vrele(searchdir);
1724 1.170 dholland }
1725 1.170 dholland searchdir = NULL;
1726 1.157 dholland }
1727 1.208.6.2 martin vrele(foundobj);
1728 1.170 dholland foundobj = NULL;
1729 1.168 dholland ndp->ni_dvp = NULL;
1730 1.139 dholland ndp->ni_vp = NULL;
1731 1.139 dholland state->attempt_retry = 1;
1732 1.171 dholland return EROFS;
1733 1.139 dholland }
1734 1.208.6.2 martin
1735 1.208.6.2 martin /* Lock the leaf node if requested. */
1736 1.208.6.2 martin if ((cnp->cn_flags & (LOCKLEAF | LOCKPARENT)) == LOCKPARENT &&
1737 1.208.6.2 martin searchdir == foundobj) {
1738 1.172 dholland /*
1739 1.172 dholland * Note: if LOCKPARENT but not LOCKLEAF is
1740 1.172 dholland * set, and searchdir == foundobj, this code
1741 1.172 dholland * necessarily unlocks the parent as well as
1742 1.172 dholland * the leaf. That is, just because you specify
1743 1.172 dholland * LOCKPARENT doesn't mean you necessarily get
1744 1.172 dholland * a locked parent vnode. The code in
1745 1.172 dholland * vfs_syscalls.c, and possibly elsewhere,
1746 1.172 dholland * that uses this combination "knows" this, so
1747 1.172 dholland * it can't be safely changed. Feh. XXX
1748 1.172 dholland */
1749 1.208.6.2 martin KASSERT(searchdir_locked);
1750 1.208.6.2 martin VOP_UNLOCK(searchdir);
1751 1.208.6.2 martin searchdir_locked = false;
1752 1.208.6.2 martin } else if ((cnp->cn_flags & LOCKLEAF) != 0 &&
1753 1.208.6.2 martin (searchdir != foundobj ||
1754 1.208.6.2 martin (cnp->cn_flags & LOCKPARENT) == 0)) {
1755 1.208.6.2 martin const int lktype = (cnp->cn_flags & LOCKSHARED) != 0 ?
1756 1.208.6.2 martin LK_SHARED : LK_EXCLUSIVE;
1757 1.208.6.2 martin vn_lock(foundobj, lktype | LK_RETRY);
1758 1.131 dholland }
1759 1.179 dholland }
1760 1.139 dholland
1761 1.131 dholland /*
1762 1.133 dholland * Done.
1763 1.131 dholland */
1764 1.131 dholland
1765 1.133 dholland /*
1766 1.133 dholland * If LOCKPARENT is not set, the parent directory isn't returned.
1767 1.133 dholland */
1768 1.157 dholland if ((cnp->cn_flags & LOCKPARENT) == 0 && searchdir != NULL) {
1769 1.208.6.2 martin vrele(searchdir);
1770 1.157 dholland searchdir = NULL;
1771 1.131 dholland }
1772 1.131 dholland
1773 1.157 dholland ndp->ni_dvp = searchdir;
1774 1.165 dholland ndp->ni_vp = foundobj;
1775 1.137 dholland return 0;
1776 1.137 dholland }
1777 1.137 dholland
1778 1.173 dholland /*
1779 1.173 dholland * Do namei; wrapper layer that handles TRYEMULROOT.
1780 1.173 dholland */
1781 1.137 dholland static int
1782 1.196 dholland namei_tryemulroot(struct namei_state *state,
1783 1.193 dholland int neverfollow, int inhibitmagic, int isnfsd)
1784 1.137 dholland {
1785 1.137 dholland int error;
1786 1.137 dholland
1787 1.137 dholland struct nameidata *ndp = state->ndp;
1788 1.137 dholland struct componentname *cnp = state->cnp;
1789 1.137 dholland const char *savepath = NULL;
1790 1.137 dholland
1791 1.137 dholland KASSERT(cnp == &ndp->ni_cnd);
1792 1.137 dholland
1793 1.137 dholland if (cnp->cn_flags & TRYEMULROOT) {
1794 1.137 dholland savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
1795 1.137 dholland }
1796 1.137 dholland
1797 1.137 dholland emul_retry:
1798 1.137 dholland state->attempt_retry = 0;
1799 1.137 dholland
1800 1.196 dholland error = namei_oneroot(state, neverfollow, inhibitmagic, isnfsd);
1801 1.137 dholland if (error) {
1802 1.137 dholland /*
1803 1.137 dholland * Once namei has started up, the existence of ni_erootdir
1804 1.137 dholland * tells us whether we're working from an emulation root.
1805 1.137 dholland * The TRYEMULROOT flag isn't necessarily authoritative.
1806 1.137 dholland */
1807 1.137 dholland if (ndp->ni_erootdir != NULL && state->attempt_retry) {
1808 1.137 dholland /* Retry the whole thing using the normal root */
1809 1.137 dholland cnp->cn_flags &= ~TRYEMULROOT;
1810 1.137 dholland state->attempt_retry = 0;
1811 1.137 dholland
1812 1.137 dholland /* kinda gross */
1813 1.137 dholland strcpy(ndp->ni_pathbuf->pb_path, savepath);
1814 1.137 dholland pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1815 1.137 dholland savepath = NULL;
1816 1.137 dholland
1817 1.137 dholland goto emul_retry;
1818 1.137 dholland }
1819 1.137 dholland }
1820 1.131 dholland if (savepath != NULL) {
1821 1.131 dholland pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1822 1.131 dholland }
1823 1.137 dholland return error;
1824 1.131 dholland }
1825 1.131 dholland
1826 1.173 dholland /*
1827 1.173 dholland * External interface.
1828 1.173 dholland */
1829 1.131 dholland int
1830 1.131 dholland namei(struct nameidata *ndp)
1831 1.131 dholland {
1832 1.131 dholland struct namei_state state;
1833 1.131 dholland int error;
1834 1.131 dholland
1835 1.131 dholland namei_init(&state, ndp);
1836 1.196 dholland error = namei_tryemulroot(&state,
1837 1.193 dholland 0/*!neverfollow*/, 0/*!inhibitmagic*/,
1838 1.193 dholland 0/*isnfsd*/);
1839 1.131 dholland namei_cleanup(&state);
1840 1.131 dholland
1841 1.159 dholland if (error) {
1842 1.159 dholland /* make sure no stray refs leak out */
1843 1.164 dholland KASSERT(ndp->ni_dvp == NULL);
1844 1.164 dholland KASSERT(ndp->ni_vp == NULL);
1845 1.159 dholland }
1846 1.159 dholland
1847 1.131 dholland return error;
1848 1.131 dholland }
1849 1.131 dholland
1850 1.131 dholland ////////////////////////////////////////////////////////////
1851 1.131 dholland
1852 1.12 mycroft /*
1853 1.173 dholland * External interface used by nfsd. This is basically different from
1854 1.173 dholland * namei only in that it has the ability to pass in the "current
1855 1.173 dholland * directory", and uses an extra flag "neverfollow" for which there's
1856 1.173 dholland * no physical flag defined in namei.h. (There used to be a cut&paste
1857 1.173 dholland * copy of about half of namei in nfsd to allow these minor
1858 1.173 dholland * adjustments to exist.)
1859 1.119 dholland *
1860 1.173 dholland * XXX: the namei interface should be adjusted so nfsd can just use
1861 1.173 dholland * ordinary namei().
1862 1.118 dholland */
1863 1.134 dholland int
1864 1.135 dholland lookup_for_nfsd(struct nameidata *ndp, struct vnode *forcecwd, int neverfollow)
1865 1.134 dholland {
1866 1.134 dholland struct namei_state state;
1867 1.134 dholland int error;
1868 1.120 dholland
1869 1.198 dholland KASSERT(ndp->ni_atdir == NULL);
1870 1.198 dholland ndp->ni_atdir = forcecwd;
1871 1.194 dholland
1872 1.134 dholland namei_init(&state, ndp);
1873 1.196 dholland error = namei_tryemulroot(&state,
1874 1.193 dholland neverfollow, 1/*inhibitmagic*/, 1/*isnfsd*/);
1875 1.119 dholland namei_cleanup(&state);
1876 1.119 dholland
1877 1.159 dholland if (error) {
1878 1.159 dholland /* make sure no stray refs leak out */
1879 1.164 dholland KASSERT(ndp->ni_dvp == NULL);
1880 1.164 dholland KASSERT(ndp->ni_vp == NULL);
1881 1.159 dholland }
1882 1.159 dholland
1883 1.119 dholland return error;
1884 1.119 dholland }
1885 1.119 dholland
1886 1.173 dholland /*
1887 1.173 dholland * A second external interface used by nfsd. This turns out to be a
1888 1.173 dholland * single lookup used by the WebNFS code (ha!) to get "index.html" or
1889 1.173 dholland * equivalent when asked for a directory. It should eventually evolve
1890 1.173 dholland * into some kind of namei_once() call; for the time being it's kind
1891 1.173 dholland * of a mess. XXX.
1892 1.173 dholland *
1893 1.173 dholland * dholland 20110109: I don't think it works, and I don't think it
1894 1.173 dholland * worked before I started hacking and slashing either, and I doubt
1895 1.173 dholland * anyone will ever notice.
1896 1.173 dholland */
1897 1.173 dholland
1898 1.173 dholland /*
1899 1.173 dholland * Internals. This calls lookup_once() after setting up the assorted
1900 1.173 dholland * pieces of state the way they ought to be.
1901 1.173 dholland */
1902 1.136 dholland static int
1903 1.196 dholland do_lookup_for_nfsd_index(struct namei_state *state)
1904 1.136 dholland {
1905 1.136 dholland int error = 0;
1906 1.136 dholland
1907 1.136 dholland struct componentname *cnp = state->cnp;
1908 1.136 dholland struct nameidata *ndp = state->ndp;
1909 1.196 dholland struct vnode *startdir;
1910 1.147 dholland struct vnode *foundobj;
1911 1.208.6.2 martin bool startdir_locked;
1912 1.136 dholland const char *cp; /* pointer into pathname argument */
1913 1.136 dholland
1914 1.136 dholland KASSERT(cnp == &ndp->ni_cnd);
1915 1.136 dholland
1916 1.198 dholland startdir = state->ndp->ni_atdir;
1917 1.196 dholland
1918 1.136 dholland cnp->cn_nameptr = ndp->ni_pnbuf;
1919 1.136 dholland state->docache = 1;
1920 1.136 dholland state->rdonly = cnp->cn_flags & RDONLY;
1921 1.136 dholland ndp->ni_dvp = NULL;
1922 1.136 dholland
1923 1.136 dholland cnp->cn_consume = 0;
1924 1.197 dholland cnp->cn_namelen = namei_getcomponent(cnp->cn_nameptr);
1925 1.197 dholland cp = cnp->cn_nameptr + cnp->cn_namelen;
1926 1.191 christos KASSERT(cnp->cn_namelen <= KERNEL_NAME_MAX);
1927 1.136 dholland ndp->ni_pathlen -= cnp->cn_namelen;
1928 1.136 dholland ndp->ni_next = cp;
1929 1.136 dholland state->slashes = 0;
1930 1.136 dholland cnp->cn_flags &= ~REQUIREDIR;
1931 1.136 dholland cnp->cn_flags |= MAKEENTRY|ISLASTCN;
1932 1.136 dholland
1933 1.136 dholland if (cnp->cn_namelen == 2 &&
1934 1.136 dholland cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1935 1.136 dholland cnp->cn_flags |= ISDOTDOT;
1936 1.136 dholland else
1937 1.136 dholland cnp->cn_flags &= ~ISDOTDOT;
1938 1.136 dholland
1939 1.160 dholland /*
1940 1.160 dholland * Because lookup_once can change the startdir, we need our
1941 1.160 dholland * own reference to it to avoid consuming the caller's.
1942 1.160 dholland */
1943 1.160 dholland vref(startdir);
1944 1.208.6.2 martin error = lookup_once(state, startdir, &startdir, &foundobj,
1945 1.208.6.2 martin &startdir_locked);
1946 1.162 dholland
1947 1.208.6.2 martin KASSERT((cnp->cn_flags & LOCKPARENT) == 0);
1948 1.208.6.2 martin if (startdir_locked) {
1949 1.208.6.2 martin VOP_UNLOCK(startdir);
1950 1.208.6.2 martin startdir_locked = false;
1951 1.136 dholland }
1952 1.136 dholland
1953 1.208.6.2 martin /*
1954 1.208.6.2 martin * If the vnode we found is mounted on, then cross the mount and get
1955 1.208.6.2 martin * the root vnode in foundobj. If this encounters an error, it will
1956 1.208.6.2 martin * dispose of foundobj, but searchdir is untouched.
1957 1.208.6.2 martin */
1958 1.208.6.2 martin if (error == 0 && foundobj != NULL &&
1959 1.208.6.2 martin foundobj->v_type == VDIR &&
1960 1.208.6.2 martin foundobj->v_mountedhere != NULL &&
1961 1.208.6.2 martin (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1962 1.208.6.2 martin error = lookup_crossmount(state, &startdir, &foundobj,
1963 1.208.6.2 martin &startdir_locked);
1964 1.136 dholland }
1965 1.136 dholland
1966 1.208.6.2 martin /* Now toss startdir and see if we have an error. */
1967 1.208.6.2 martin if (startdir != NULL)
1968 1.208.6.2 martin vrele(startdir);
1969 1.208.6.2 martin if (error)
1970 1.208.6.2 martin foundobj = NULL;
1971 1.208.6.2 martin else if (foundobj != NULL && (cnp->cn_flags & LOCKLEAF) != 0)
1972 1.208.6.2 martin vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
1973 1.208.6.2 martin
1974 1.208.6.2 martin ndp->ni_vp = foundobj;
1975 1.136 dholland return (error);
1976 1.136 dholland }
1977 1.136 dholland
1978 1.173 dholland /*
1979 1.173 dholland * External interface. The partitioning between this function and the
1980 1.173 dholland * above isn't very clear - the above function exists mostly so code
1981 1.173 dholland * that uses "state->" can be shuffled around without having to change
1982 1.173 dholland * it to "state.".
1983 1.173 dholland */
1984 1.118 dholland int
1985 1.128 dholland lookup_for_nfsd_index(struct nameidata *ndp, struct vnode *startdir)
1986 1.118 dholland {
1987 1.118 dholland struct namei_state state;
1988 1.118 dholland int error;
1989 1.118 dholland
1990 1.198 dholland KASSERT(ndp->ni_atdir == NULL);
1991 1.198 dholland ndp->ni_atdir = startdir;
1992 1.194 dholland
1993 1.133 dholland /*
1994 1.135 dholland * Note: the name sent in here (is not|should not be) allowed
1995 1.135 dholland * to contain a slash.
1996 1.133 dholland */
1997 1.191 christos if (strlen(ndp->ni_pathbuf->pb_path) > KERNEL_NAME_MAX) {
1998 1.136 dholland return ENAMETOOLONG;
1999 1.136 dholland }
2000 1.136 dholland if (strchr(ndp->ni_pathbuf->pb_path, '/')) {
2001 1.136 dholland return EINVAL;
2002 1.136 dholland }
2003 1.133 dholland
2004 1.133 dholland ndp->ni_pathlen = strlen(ndp->ni_pathbuf->pb_path) + 1;
2005 1.133 dholland ndp->ni_pnbuf = NULL;
2006 1.133 dholland ndp->ni_cnd.cn_nameptr = NULL;
2007 1.133 dholland
2008 1.118 dholland namei_init(&state, ndp);
2009 1.196 dholland error = do_lookup_for_nfsd_index(&state);
2010 1.118 dholland namei_cleanup(&state);
2011 1.118 dholland
2012 1.118 dholland return error;
2013 1.118 dholland }
2014 1.118 dholland
2015 1.131 dholland ////////////////////////////////////////////////////////////
2016 1.131 dholland
2017 1.118 dholland /*
2018 1.12 mycroft * Reacquire a path name component.
2019 1.73 chs * dvp is locked on entry and exit.
2020 1.73 chs * *vpp is locked on exit unless it's NULL.
2021 1.12 mycroft */
2022 1.12 mycroft int
2023 1.130 dholland relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int dummy)
2024 1.12 mycroft {
2025 1.12 mycroft int rdonly; /* lookup read-only flag bit */
2026 1.12 mycroft int error = 0;
2027 1.52 yamt #ifdef DEBUG
2028 1.197 dholland size_t newlen; /* DEBUG: check name len */
2029 1.197 dholland const char *cp; /* DEBUG: check name ptr */
2030 1.52 yamt #endif /* DEBUG */
2031 1.12 mycroft
2032 1.130 dholland (void)dummy;
2033 1.130 dholland
2034 1.12 mycroft /*
2035 1.12 mycroft * Setup: break out flag bits into variables.
2036 1.12 mycroft */
2037 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
2038 1.12 mycroft
2039 1.12 mycroft /*
2040 1.12 mycroft * Search a new directory.
2041 1.12 mycroft *
2042 1.12 mycroft * The cn_hash value is for use by vfs_cache.
2043 1.12 mycroft * The last component of the filename is left accessible via
2044 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
2045 1.12 mycroft * the name set the SAVENAME flag. When done, they assume
2046 1.12 mycroft * responsibility for freeing the pathname buffer.
2047 1.12 mycroft */
2048 1.52 yamt #ifdef DEBUG
2049 1.197 dholland #if 0
2050 1.39 lukem cp = NULL;
2051 1.39 lukem newhash = namei_hash(cnp->cn_nameptr, &cp);
2052 1.81 chs if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
2053 1.12 mycroft panic("relookup: bad hash");
2054 1.197 dholland #endif
2055 1.199 para newlen = namei_getcomponent(cnp->cn_nameptr);
2056 1.197 dholland if (cnp->cn_namelen != newlen)
2057 1.58 christos panic("relookup: bad len");
2058 1.197 dholland cp = cnp->cn_nameptr + cnp->cn_namelen;
2059 1.53 yamt while (*cp == '/')
2060 1.53 yamt cp++;
2061 1.12 mycroft if (*cp != 0)
2062 1.12 mycroft panic("relookup: not last component");
2063 1.52 yamt #endif /* DEBUG */
2064 1.12 mycroft
2065 1.12 mycroft /*
2066 1.12 mycroft * Check for degenerate name (e.g. / or "")
2067 1.12 mycroft * which is a way of talking about a directory,
2068 1.12 mycroft * e.g. like "/." or ".".
2069 1.12 mycroft */
2070 1.23 mycroft if (cnp->cn_nameptr[0] == '\0')
2071 1.23 mycroft panic("relookup: null name");
2072 1.12 mycroft
2073 1.12 mycroft if (cnp->cn_flags & ISDOTDOT)
2074 1.58 christos panic("relookup: lookup on dot-dot");
2075 1.12 mycroft
2076 1.12 mycroft /*
2077 1.12 mycroft * We now have a segment name to search for, and a directory to search.
2078 1.12 mycroft */
2079 1.195 dholland *vpp = NULL;
2080 1.129 dholland error = VOP_LOOKUP(dvp, vpp, cnp);
2081 1.129 dholland if ((error) != 0) {
2082 1.205 riastrad KASSERTMSG((*vpp == NULL),
2083 1.205 riastrad "leaf `%s' should be empty but is %p",
2084 1.205 riastrad cnp->cn_nameptr, *vpp);
2085 1.12 mycroft if (error != EJUSTRETURN)
2086 1.12 mycroft goto bad;
2087 1.12 mycroft }
2088 1.12 mycroft
2089 1.12 mycroft /*
2090 1.12 mycroft * Check for symbolic link
2091 1.12 mycroft */
2092 1.205 riastrad KASSERTMSG((*vpp == NULL || (*vpp)->v_type != VLNK ||
2093 1.205 riastrad (cnp->cn_flags & FOLLOW) == 0),
2094 1.205 riastrad "relookup: symlink found");
2095 1.12 mycroft
2096 1.12 mycroft /*
2097 1.94 pooka * Check for read-only lookups.
2098 1.12 mycroft */
2099 1.81 chs if (rdonly && cnp->cn_nameiop != LOOKUP) {
2100 1.26 fvdl error = EROFS;
2101 1.81 chs if (*vpp) {
2102 1.201 hannken vrele(*vpp);
2103 1.81 chs }
2104 1.73 chs goto bad;
2105 1.12 mycroft }
2106 1.201 hannken /*
2107 1.201 hannken * Lock result.
2108 1.201 hannken */
2109 1.201 hannken if (*vpp && *vpp != dvp) {
2110 1.201 hannken error = vn_lock(*vpp, LK_EXCLUSIVE);
2111 1.201 hannken if (error != 0) {
2112 1.201 hannken vrele(*vpp);
2113 1.201 hannken goto bad;
2114 1.201 hannken }
2115 1.201 hannken }
2116 1.12 mycroft return (0);
2117 1.12 mycroft
2118 1.12 mycroft bad:
2119 1.12 mycroft *vpp = NULL;
2120 1.10 cgd return (error);
2121 1.10 cgd }
2122 1.116 dholland
2123 1.116 dholland /*
2124 1.116 dholland * namei_simple - simple forms of namei.
2125 1.116 dholland *
2126 1.116 dholland * These are wrappers to allow the simple case callers of namei to be
2127 1.116 dholland * left alone while everything else changes under them.
2128 1.116 dholland */
2129 1.116 dholland
2130 1.116 dholland /* Flags */
2131 1.116 dholland struct namei_simple_flags_type {
2132 1.116 dholland int dummy;
2133 1.116 dholland };
2134 1.116 dholland static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
2135 1.116 dholland const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
2136 1.116 dholland const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
2137 1.116 dholland const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
2138 1.116 dholland const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
2139 1.116 dholland
2140 1.116 dholland static
2141 1.116 dholland int
2142 1.116 dholland namei_simple_convert_flags(namei_simple_flags_t sflags)
2143 1.116 dholland {
2144 1.116 dholland if (sflags == NSM_NOFOLLOW_NOEMULROOT)
2145 1.116 dholland return NOFOLLOW | 0;
2146 1.116 dholland if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
2147 1.116 dholland return NOFOLLOW | TRYEMULROOT;
2148 1.116 dholland if (sflags == NSM_FOLLOW_NOEMULROOT)
2149 1.116 dholland return FOLLOW | 0;
2150 1.116 dholland if (sflags == NSM_FOLLOW_TRYEMULROOT)
2151 1.116 dholland return FOLLOW | TRYEMULROOT;
2152 1.116 dholland panic("namei_simple_convert_flags: bogus sflags\n");
2153 1.116 dholland return 0;
2154 1.116 dholland }
2155 1.116 dholland
2156 1.116 dholland int
2157 1.116 dholland namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
2158 1.200 manu struct vnode **vp_ret)
2159 1.200 manu {
2160 1.200 manu return nameiat_simple_kernel(NULL, path, sflags, vp_ret);
2161 1.200 manu }
2162 1.200 manu
2163 1.200 manu int
2164 1.200 manu nameiat_simple_kernel(struct vnode *dvp, const char *path,
2165 1.200 manu namei_simple_flags_t sflags, struct vnode **vp_ret)
2166 1.116 dholland {
2167 1.116 dholland struct nameidata nd;
2168 1.123 dholland struct pathbuf *pb;
2169 1.116 dholland int err;
2170 1.116 dholland
2171 1.123 dholland pb = pathbuf_create(path);
2172 1.123 dholland if (pb == NULL) {
2173 1.123 dholland return ENOMEM;
2174 1.123 dholland }
2175 1.123 dholland
2176 1.116 dholland NDINIT(&nd,
2177 1.116 dholland LOOKUP,
2178 1.116 dholland namei_simple_convert_flags(sflags),
2179 1.123 dholland pb);
2180 1.200 manu
2181 1.200 manu if (dvp != NULL)
2182 1.200 manu NDAT(&nd, dvp);
2183 1.200 manu
2184 1.116 dholland err = namei(&nd);
2185 1.116 dholland if (err != 0) {
2186 1.123 dholland pathbuf_destroy(pb);
2187 1.116 dholland return err;
2188 1.116 dholland }
2189 1.116 dholland *vp_ret = nd.ni_vp;
2190 1.123 dholland pathbuf_destroy(pb);
2191 1.116 dholland return 0;
2192 1.116 dholland }
2193 1.116 dholland
2194 1.116 dholland int
2195 1.116 dholland namei_simple_user(const char *path, namei_simple_flags_t sflags,
2196 1.200 manu struct vnode **vp_ret)
2197 1.200 manu {
2198 1.200 manu return nameiat_simple_user(NULL, path, sflags, vp_ret);
2199 1.200 manu }
2200 1.200 manu
2201 1.200 manu int
2202 1.200 manu nameiat_simple_user(struct vnode *dvp, const char *path,
2203 1.200 manu namei_simple_flags_t sflags, struct vnode **vp_ret)
2204 1.116 dholland {
2205 1.123 dholland struct pathbuf *pb;
2206 1.116 dholland struct nameidata nd;
2207 1.116 dholland int err;
2208 1.116 dholland
2209 1.123 dholland err = pathbuf_copyin(path, &pb);
2210 1.123 dholland if (err) {
2211 1.123 dholland return err;
2212 1.123 dholland }
2213 1.123 dholland
2214 1.116 dholland NDINIT(&nd,
2215 1.116 dholland LOOKUP,
2216 1.116 dholland namei_simple_convert_flags(sflags),
2217 1.123 dholland pb);
2218 1.200 manu
2219 1.200 manu if (dvp != NULL)
2220 1.200 manu NDAT(&nd, dvp);
2221 1.200 manu
2222 1.116 dholland err = namei(&nd);
2223 1.116 dholland if (err != 0) {
2224 1.123 dholland pathbuf_destroy(pb);
2225 1.116 dholland return err;
2226 1.116 dholland }
2227 1.116 dholland *vp_ret = nd.ni_vp;
2228 1.123 dholland pathbuf_destroy(pb);
2229 1.116 dholland return 0;
2230 1.116 dholland }
2231