vfs_lookup.c revision 1.131.2.1 1 1.131.2.1 jruoho /* $NetBSD: vfs_lookup.c,v 1.131.2.1 2011/06/06 09:09:40 jruoho 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.131.2.1 jruoho __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.131.2.1 2011/06/06 09:09:40 jruoho Exp $");
41 1.27 thorpej
42 1.67 chs #include "opt_magiclinks.h"
43 1.10 cgd
44 1.10 cgd #include <sys/param.h>
45 1.15 cgd #include <sys/systm.h>
46 1.61 thorpej #include <sys/kernel.h>
47 1.10 cgd #include <sys/syslimits.h>
48 1.10 cgd #include <sys/time.h>
49 1.10 cgd #include <sys/namei.h>
50 1.10 cgd #include <sys/vnode.h>
51 1.10 cgd #include <sys/mount.h>
52 1.10 cgd #include <sys/errno.h>
53 1.39 lukem #include <sys/filedesc.h>
54 1.39 lukem #include <sys/hash.h>
55 1.10 cgd #include <sys/proc.h>
56 1.40 wrstuden #include <sys/syslog.h>
57 1.70 elad #include <sys/kauth.h>
58 1.97 ad #include <sys/ktrace.h>
59 1.12 mycroft
60 1.67 chs #ifndef MAGICLINKS
61 1.67 chs #define MAGICLINKS 0
62 1.67 chs #endif
63 1.67 chs
64 1.67 chs int vfs_magiclinks = MAGICLINKS;
65 1.67 chs
66 1.10 cgd /*
67 1.61 thorpej * Substitute replacement text for 'magic' strings in symlinks.
68 1.61 thorpej * Returns 0 if successful, and returns non-zero if an error
69 1.61 thorpej * occurs. (Currently, the only possible error is running out
70 1.61 thorpej * of temporary pathname space.)
71 1.61 thorpej *
72 1.61 thorpej * Looks for "@<string>" and "@<string>/", where <string> is a
73 1.61 thorpej * recognized 'magic' string. Replaces the "@<string>" with the
74 1.61 thorpej * appropriate replacement text. (Note that in some cases the
75 1.61 thorpej * replacement text may have zero length.)
76 1.61 thorpej *
77 1.61 thorpej * This would have been table driven, but the variance in
78 1.61 thorpej * replacement strings (and replacement string lengths) made
79 1.61 thorpej * that impractical.
80 1.61 thorpej */
81 1.63 thorpej #define VNL(x) \
82 1.63 thorpej (sizeof(x) - 1)
83 1.63 thorpej
84 1.63 thorpej #define VO '{'
85 1.63 thorpej #define VC '}'
86 1.63 thorpej
87 1.61 thorpej #define MATCH(str) \
88 1.63 thorpej ((termchar == '/' && i + VNL(str) == *len) || \
89 1.63 thorpej (i + VNL(str) < *len && \
90 1.63 thorpej cp[i + VNL(str)] == termchar)) && \
91 1.63 thorpej !strncmp((str), &cp[i], VNL(str))
92 1.61 thorpej
93 1.61 thorpej #define SUBSTITUTE(m, s, sl) \
94 1.115 christos if ((newlen + (sl)) >= MAXPATHLEN) \
95 1.115 christos return 1; \
96 1.63 thorpej i += VNL(m); \
97 1.63 thorpej if (termchar != '/') \
98 1.63 thorpej i++; \
99 1.115 christos (void)memcpy(&tmp[newlen], (s), (sl)); \
100 1.63 thorpej newlen += (sl); \
101 1.63 thorpej change = 1; \
102 1.63 thorpej termchar = '/';
103 1.61 thorpej
104 1.61 thorpej static int
105 1.115 christos symlink_magic(struct proc *p, char *cp, size_t *len)
106 1.61 thorpej {
107 1.66 yamt char *tmp;
108 1.115 christos size_t change, i, newlen, slen;
109 1.115 christos char termchar = '/';
110 1.115 christos char idtmp[11]; /* enough for 32 bit *unsigned* integer */
111 1.101 mjf
112 1.61 thorpej
113 1.66 yamt tmp = PNBUF_GET();
114 1.61 thorpej for (change = i = newlen = 0; i < *len; ) {
115 1.63 thorpej if (cp[i] != '@') {
116 1.61 thorpej tmp[newlen++] = cp[i++];
117 1.63 thorpej continue;
118 1.63 thorpej }
119 1.63 thorpej
120 1.63 thorpej i++;
121 1.63 thorpej
122 1.63 thorpej /* Check for @{var} syntax. */
123 1.63 thorpej if (cp[i] == VO) {
124 1.63 thorpej termchar = VC;
125 1.61 thorpej i++;
126 1.63 thorpej }
127 1.63 thorpej
128 1.63 thorpej /*
129 1.63 thorpej * The following checks should be ordered according
130 1.63 thorpej * to frequency of use.
131 1.63 thorpej */
132 1.63 thorpej if (MATCH("machine_arch")) {
133 1.115 christos slen = VNL(MACHINE_ARCH);
134 1.115 christos SUBSTITUTE("machine_arch", MACHINE_ARCH, slen);
135 1.63 thorpej } else if (MATCH("machine")) {
136 1.115 christos slen = VNL(MACHINE);
137 1.115 christos SUBSTITUTE("machine", MACHINE, slen);
138 1.63 thorpej } else if (MATCH("hostname")) {
139 1.115 christos SUBSTITUTE("hostname", hostname, hostnamelen);
140 1.63 thorpej } else if (MATCH("osrelease")) {
141 1.115 christos slen = strlen(osrelease);
142 1.115 christos SUBSTITUTE("osrelease", osrelease, slen);
143 1.63 thorpej } else if (MATCH("emul")) {
144 1.115 christos slen = strlen(p->p_emul->e_name);
145 1.115 christos SUBSTITUTE("emul", p->p_emul->e_name, slen);
146 1.63 thorpej } else if (MATCH("kernel_ident")) {
147 1.115 christos slen = strlen(kernel_ident);
148 1.115 christos SUBSTITUTE("kernel_ident", kernel_ident, slen);
149 1.63 thorpej } else if (MATCH("domainname")) {
150 1.115 christos SUBSTITUTE("domainname", domainname, domainnamelen);
151 1.63 thorpej } else if (MATCH("ostype")) {
152 1.115 christos slen = strlen(ostype);
153 1.115 christos SUBSTITUTE("ostype", ostype, slen);
154 1.72 elad } else if (MATCH("uid")) {
155 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
156 1.72 elad kauth_cred_geteuid(kauth_cred_get()));
157 1.115 christos SUBSTITUTE("uid", idtmp, slen);
158 1.101 mjf } else if (MATCH("ruid")) {
159 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
160 1.101 mjf kauth_cred_getuid(kauth_cred_get()));
161 1.115 christos SUBSTITUTE("ruid", idtmp, slen);
162 1.115 christos } else if (MATCH("gid")) {
163 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
164 1.115 christos kauth_cred_getegid(kauth_cred_get()));
165 1.115 christos SUBSTITUTE("gid", idtmp, slen);
166 1.115 christos } else if (MATCH("rgid")) {
167 1.115 christos slen = snprintf(idtmp, sizeof(idtmp), "%u",
168 1.115 christos kauth_cred_getgid(kauth_cred_get()));
169 1.115 christos SUBSTITUTE("rgid", idtmp, slen);
170 1.63 thorpej } else {
171 1.63 thorpej tmp[newlen++] = '@';
172 1.63 thorpej if (termchar == VC)
173 1.63 thorpej tmp[newlen++] = VO;
174 1.61 thorpej }
175 1.61 thorpej }
176 1.61 thorpej
177 1.66 yamt if (change) {
178 1.115 christos (void)memcpy(cp, tmp, newlen);
179 1.66 yamt *len = newlen;
180 1.66 yamt }
181 1.66 yamt PNBUF_PUT(tmp);
182 1.61 thorpej
183 1.115 christos return 0;
184 1.61 thorpej }
185 1.61 thorpej
186 1.63 thorpej #undef VNL
187 1.63 thorpej #undef VO
188 1.63 thorpej #undef VC
189 1.63 thorpej #undef MATCH
190 1.63 thorpej #undef SUBSTITUTE
191 1.63 thorpej
192 1.123 dholland ////////////////////////////////////////////////////////////
193 1.123 dholland
194 1.123 dholland /*
195 1.131 dholland * Determine the namei hash (for cn_hash) for name.
196 1.131 dholland * If *ep != NULL, hash from name to ep-1.
197 1.131 dholland * If *ep == NULL, hash from name until the first NUL or '/', and
198 1.131 dholland * return the location of this termination character in *ep.
199 1.131 dholland *
200 1.131 dholland * This function returns an equivalent hash to the MI hash32_strn().
201 1.131 dholland * The latter isn't used because in the *ep == NULL case, determining
202 1.131 dholland * the length of the string to the first NUL or `/' and then calling
203 1.131 dholland * hash32_strn() involves unnecessary double-handling of the data.
204 1.131 dholland */
205 1.131 dholland uint32_t
206 1.131 dholland namei_hash(const char *name, const char **ep)
207 1.131 dholland {
208 1.131 dholland uint32_t hash;
209 1.131 dholland
210 1.131 dholland hash = HASH32_STR_INIT;
211 1.131 dholland if (*ep != NULL) {
212 1.131 dholland for (; name < *ep; name++)
213 1.131 dholland hash = hash * 33 + *(const uint8_t *)name;
214 1.131 dholland } else {
215 1.131 dholland for (; *name != '\0' && *name != '/'; name++)
216 1.131 dholland hash = hash * 33 + *(const uint8_t *)name;
217 1.131 dholland *ep = name;
218 1.131 dholland }
219 1.131 dholland return (hash + (hash >> 5));
220 1.131 dholland }
221 1.131 dholland
222 1.131 dholland ////////////////////////////////////////////////////////////
223 1.131 dholland
224 1.131 dholland /*
225 1.123 dholland * Sealed abstraction for pathnames.
226 1.123 dholland *
227 1.123 dholland * System-call-layer level code that is going to call namei should
228 1.123 dholland * first create a pathbuf and adjust all the bells and whistles on it
229 1.131.2.1 jruoho * as needed by context.
230 1.123 dholland */
231 1.123 dholland
232 1.123 dholland struct pathbuf {
233 1.123 dholland char *pb_path;
234 1.123 dholland char *pb_pathcopy;
235 1.123 dholland unsigned pb_pathcopyuses;
236 1.123 dholland };
237 1.123 dholland
238 1.123 dholland static struct pathbuf *
239 1.123 dholland pathbuf_create_raw(void)
240 1.123 dholland {
241 1.123 dholland struct pathbuf *pb;
242 1.123 dholland
243 1.123 dholland pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
244 1.123 dholland if (pb == NULL) {
245 1.123 dholland return NULL;
246 1.123 dholland }
247 1.123 dholland pb->pb_path = PNBUF_GET();
248 1.123 dholland if (pb->pb_path == NULL) {
249 1.123 dholland kmem_free(pb, sizeof(*pb));
250 1.123 dholland return NULL;
251 1.123 dholland }
252 1.123 dholland pb->pb_pathcopy = NULL;
253 1.123 dholland pb->pb_pathcopyuses = 0;
254 1.123 dholland return pb;
255 1.123 dholland }
256 1.123 dholland
257 1.123 dholland void
258 1.123 dholland pathbuf_destroy(struct pathbuf *pb)
259 1.123 dholland {
260 1.123 dholland KASSERT(pb->pb_pathcopyuses == 0);
261 1.123 dholland KASSERT(pb->pb_pathcopy == NULL);
262 1.123 dholland PNBUF_PUT(pb->pb_path);
263 1.123 dholland kmem_free(pb, sizeof(*pb));
264 1.123 dholland }
265 1.123 dholland
266 1.123 dholland struct pathbuf *
267 1.124 dholland pathbuf_assimilate(char *pnbuf)
268 1.124 dholland {
269 1.124 dholland struct pathbuf *pb;
270 1.124 dholland
271 1.124 dholland pb = kmem_alloc(sizeof(*pb), KM_SLEEP);
272 1.124 dholland if (pb == NULL) {
273 1.124 dholland return NULL;
274 1.124 dholland }
275 1.124 dholland pb->pb_path = pnbuf;
276 1.124 dholland pb->pb_pathcopy = NULL;
277 1.124 dholland pb->pb_pathcopyuses = 0;
278 1.124 dholland return pb;
279 1.124 dholland }
280 1.124 dholland
281 1.124 dholland struct pathbuf *
282 1.123 dholland pathbuf_create(const char *path)
283 1.123 dholland {
284 1.123 dholland struct pathbuf *pb;
285 1.123 dholland int error;
286 1.123 dholland
287 1.123 dholland pb = pathbuf_create_raw();
288 1.123 dholland if (pb == NULL) {
289 1.123 dholland return NULL;
290 1.123 dholland }
291 1.123 dholland error = copystr(path, pb->pb_path, PATH_MAX, NULL);
292 1.123 dholland if (error != 0) {
293 1.123 dholland KASSERT(!"kernel path too long in pathbuf_create");
294 1.123 dholland /* make sure it's null-terminated, just in case */
295 1.123 dholland pb->pb_path[PATH_MAX-1] = '\0';
296 1.123 dholland }
297 1.123 dholland return pb;
298 1.123 dholland }
299 1.123 dholland
300 1.123 dholland int
301 1.123 dholland pathbuf_copyin(const char *userpath, struct pathbuf **ret)
302 1.123 dholland {
303 1.123 dholland struct pathbuf *pb;
304 1.123 dholland int error;
305 1.123 dholland
306 1.123 dholland pb = pathbuf_create_raw();
307 1.123 dholland if (pb == NULL) {
308 1.123 dholland return ENOMEM;
309 1.123 dholland }
310 1.123 dholland error = copyinstr(userpath, pb->pb_path, PATH_MAX, NULL);
311 1.123 dholland if (error) {
312 1.123 dholland pathbuf_destroy(pb);
313 1.123 dholland return error;
314 1.123 dholland }
315 1.123 dholland *ret = pb;
316 1.123 dholland return 0;
317 1.123 dholland }
318 1.123 dholland
319 1.123 dholland /*
320 1.131.2.1 jruoho * XXX should not exist:
321 1.131.2.1 jruoho * 1. whether a pointer is kernel or user should be statically checkable.
322 1.131.2.1 jruoho * 2. copyin should be handled by the upper part of the syscall layer,
323 1.131.2.1 jruoho * not in here.
324 1.123 dholland */
325 1.123 dholland int
326 1.123 dholland pathbuf_maybe_copyin(const char *path, enum uio_seg seg, struct pathbuf **ret)
327 1.123 dholland {
328 1.123 dholland if (seg == UIO_USERSPACE) {
329 1.123 dholland return pathbuf_copyin(path, ret);
330 1.123 dholland } else {
331 1.123 dholland *ret = pathbuf_create(path);
332 1.123 dholland if (*ret == NULL) {
333 1.123 dholland return ENOMEM;
334 1.123 dholland }
335 1.123 dholland return 0;
336 1.123 dholland }
337 1.123 dholland }
338 1.123 dholland
339 1.123 dholland /*
340 1.123 dholland * Get a copy of the path buffer as it currently exists. If this is
341 1.123 dholland * called after namei starts the results may be arbitrary.
342 1.123 dholland */
343 1.123 dholland void
344 1.123 dholland pathbuf_copystring(const struct pathbuf *pb, char *buf, size_t maxlen)
345 1.123 dholland {
346 1.123 dholland strlcpy(buf, pb->pb_path, maxlen);
347 1.123 dholland }
348 1.123 dholland
349 1.123 dholland /*
350 1.123 dholland * These two functions allow access to a saved copy of the original
351 1.123 dholland * path string. The first copy should be gotten before namei is
352 1.123 dholland * called. Each copy that is gotten should be put back.
353 1.123 dholland */
354 1.123 dholland
355 1.123 dholland const char *
356 1.123 dholland pathbuf_stringcopy_get(struct pathbuf *pb)
357 1.123 dholland {
358 1.123 dholland if (pb->pb_pathcopyuses == 0) {
359 1.123 dholland pb->pb_pathcopy = PNBUF_GET();
360 1.123 dholland strcpy(pb->pb_pathcopy, pb->pb_path);
361 1.123 dholland }
362 1.123 dholland pb->pb_pathcopyuses++;
363 1.123 dholland return pb->pb_pathcopy;
364 1.123 dholland }
365 1.123 dholland
366 1.123 dholland void
367 1.123 dholland pathbuf_stringcopy_put(struct pathbuf *pb, const char *str)
368 1.123 dholland {
369 1.123 dholland KASSERT(str == pb->pb_pathcopy);
370 1.123 dholland KASSERT(pb->pb_pathcopyuses > 0);
371 1.123 dholland pb->pb_pathcopyuses--;
372 1.123 dholland if (pb->pb_pathcopyuses == 0) {
373 1.123 dholland PNBUF_PUT(pb->pb_pathcopy);
374 1.123 dholland pb->pb_pathcopy = NULL;
375 1.123 dholland }
376 1.123 dholland }
377 1.123 dholland
378 1.123 dholland
379 1.123 dholland ////////////////////////////////////////////////////////////
380 1.123 dholland
381 1.61 thorpej /*
382 1.131.2.1 jruoho * namei: convert a pathname into a pointer to a (maybe-locked) vnode,
383 1.131.2.1 jruoho * and maybe also its parent directory vnode, and assorted other guff.
384 1.131.2.1 jruoho * See namei(9) for the interface documentation.
385 1.131.2.1 jruoho *
386 1.10 cgd *
387 1.10 cgd * The FOLLOW flag is set when symbolic links are to be followed
388 1.10 cgd * when they occur at the end of the name translation process.
389 1.10 cgd * Symbolic links are always followed for all other pathname
390 1.10 cgd * components other than the last.
391 1.10 cgd *
392 1.10 cgd * The segflg defines whether the name is to be copied from user
393 1.10 cgd * space or kernel space.
394 1.10 cgd *
395 1.10 cgd * Overall outline of namei:
396 1.10 cgd *
397 1.10 cgd * copy in name
398 1.10 cgd * get starting directory
399 1.10 cgd * while (!done && !error) {
400 1.10 cgd * call lookup to search path.
401 1.10 cgd * if symbolic link, massage name in buffer and continue
402 1.10 cgd * }
403 1.10 cgd */
404 1.117 dholland
405 1.117 dholland /*
406 1.131.2.1 jruoho * Search a pathname.
407 1.131.2.1 jruoho * This is a very central and rather complicated routine.
408 1.131.2.1 jruoho *
409 1.131.2.1 jruoho * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
410 1.131.2.1 jruoho * The starting directory is passed in. The pathname is descended
411 1.131.2.1 jruoho * until done, or a symbolic link is encountered. The variable ni_more
412 1.131.2.1 jruoho * is clear if the path is completed; it is set to one if a symbolic
413 1.131.2.1 jruoho * link needing interpretation is encountered.
414 1.131.2.1 jruoho *
415 1.131.2.1 jruoho * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
416 1.131.2.1 jruoho * whether the name is to be looked up, created, renamed, or deleted.
417 1.131.2.1 jruoho * When CREATE, RENAME, or DELETE is specified, information usable in
418 1.131.2.1 jruoho * creating, renaming, or deleting a directory entry may be calculated.
419 1.131.2.1 jruoho * If flag has LOCKPARENT or'ed into it, the parent directory is returned
420 1.131.2.1 jruoho * locked. Otherwise the parent directory is not returned. If the target
421 1.131.2.1 jruoho * of the pathname exists and LOCKLEAF is or'ed into the flag the target
422 1.131.2.1 jruoho * is returned locked, otherwise it is returned unlocked. When creating
423 1.131.2.1 jruoho * or renaming and LOCKPARENT is specified, the target may not be ".".
424 1.131.2.1 jruoho * When deleting and LOCKPARENT is specified, the target may be ".".
425 1.131.2.1 jruoho *
426 1.131.2.1 jruoho * Overall outline of lookup:
427 1.131.2.1 jruoho *
428 1.131.2.1 jruoho * dirloop:
429 1.131.2.1 jruoho * identify next component of name at ndp->ni_ptr
430 1.131.2.1 jruoho * handle degenerate case where name is null string
431 1.131.2.1 jruoho * if .. and crossing mount points and on mounted filesys, find parent
432 1.131.2.1 jruoho * call VOP_LOOKUP routine for next component name
433 1.131.2.1 jruoho * directory vnode returned in ni_dvp, locked.
434 1.131.2.1 jruoho * component vnode returned in ni_vp (if it exists), locked.
435 1.131.2.1 jruoho * if result vnode is mounted on and crossing mount points,
436 1.131.2.1 jruoho * find mounted on vnode
437 1.131.2.1 jruoho * if more components of name, do next level at dirloop
438 1.131.2.1 jruoho * return the answer in ni_vp, locked if LOCKLEAF set
439 1.131.2.1 jruoho * if LOCKPARENT set, return locked parent in ni_dvp
440 1.131.2.1 jruoho */
441 1.131.2.1 jruoho
442 1.131.2.1 jruoho
443 1.131.2.1 jruoho /*
444 1.117 dholland * Internal state for a namei operation.
445 1.131.2.1 jruoho *
446 1.131.2.1 jruoho * cnp is always equal to &ndp->ni_cnp.
447 1.117 dholland */
448 1.117 dholland struct namei_state {
449 1.117 dholland struct nameidata *ndp;
450 1.117 dholland struct componentname *cnp;
451 1.117 dholland
452 1.118 dholland int docache; /* == 0 do not cache last component */
453 1.118 dholland int rdonly; /* lookup read-only flag bit */
454 1.118 dholland int slashes;
455 1.117 dholland
456 1.131.2.1 jruoho unsigned attempt_retry:1; /* true if error allows emul retry */
457 1.131.2.1 jruoho };
458 1.118 dholland
459 1.118 dholland
460 1.117 dholland /*
461 1.117 dholland * Initialize the namei working state.
462 1.117 dholland */
463 1.117 dholland static void
464 1.117 dholland namei_init(struct namei_state *state, struct nameidata *ndp)
465 1.117 dholland {
466 1.117 dholland state->ndp = ndp;
467 1.117 dholland state->cnp = &ndp->ni_cnd;
468 1.129 dholland KASSERT((state->cnp->cn_flags & INRELOOKUP) == 0);
469 1.117 dholland
470 1.118 dholland state->docache = 0;
471 1.118 dholland state->rdonly = 0;
472 1.118 dholland state->slashes = 0;
473 1.10 cgd
474 1.12 mycroft #ifdef DIAGNOSTIC
475 1.117 dholland if (!state->cnp->cn_cred)
476 1.58 christos panic("namei: bad cred/proc");
477 1.117 dholland if (state->cnp->cn_nameiop & (~OPMASK))
478 1.58 christos panic("namei: nameiop contaminated with flags");
479 1.117 dholland if (state->cnp->cn_flags & OPMASK)
480 1.58 christos panic("namei: flags contaminated with nameiops");
481 1.12 mycroft #endif
482 1.10 cgd
483 1.10 cgd /*
484 1.124 dholland * The buffer for name translation shall be the one inside the
485 1.124 dholland * pathbuf.
486 1.10 cgd */
487 1.124 dholland state->ndp->ni_pnbuf = state->ndp->ni_pathbuf->pb_path;
488 1.117 dholland }
489 1.117 dholland
490 1.117 dholland /*
491 1.131.2.1 jruoho * Clean up the working namei state, leaving things ready for return
492 1.131.2.1 jruoho * from namei.
493 1.117 dholland */
494 1.131.2.1 jruoho static void
495 1.131.2.1 jruoho namei_cleanup(struct namei_state *state)
496 1.131.2.1 jruoho {
497 1.131.2.1 jruoho KASSERT(state->cnp == &state->ndp->ni_cnd);
498 1.131.2.1 jruoho
499 1.131.2.1 jruoho /* nothing for now */
500 1.131.2.1 jruoho (void)state;
501 1.131.2.1 jruoho }
502 1.131.2.1 jruoho
503 1.131.2.1 jruoho //////////////////////////////
504 1.131.2.1 jruoho
505 1.131.2.1 jruoho /*
506 1.131.2.1 jruoho * Get the directory context.
507 1.131.2.1 jruoho * Initializes the rootdir and erootdir state and returns a reference
508 1.131.2.1 jruoho * to the starting dir.
509 1.131.2.1 jruoho */
510 1.131.2.1 jruoho static struct vnode *
511 1.131.2.1 jruoho namei_getstartdir(struct namei_state *state)
512 1.117 dholland {
513 1.117 dholland struct nameidata *ndp = state->ndp;
514 1.117 dholland struct componentname *cnp = state->cnp;
515 1.117 dholland struct cwdinfo *cwdi; /* pointer to cwd state */
516 1.117 dholland struct lwp *self = curlwp; /* thread doing namei() */
517 1.131.2.1 jruoho struct vnode *rootdir, *erootdir, *curdir, *startdir;
518 1.117 dholland
519 1.131.2.1 jruoho cwdi = self->l_proc->p_cwdi;
520 1.131.2.1 jruoho rw_enter(&cwdi->cwdi_lock, RW_READER);
521 1.21 kleink
522 1.131.2.1 jruoho /* root dir */
523 1.131.2.1 jruoho if (cwdi->cwdi_rdir == NULL || (cnp->cn_flags & NOCHROOT)) {
524 1.131.2.1 jruoho rootdir = rootvnode;
525 1.131.2.1 jruoho } else {
526 1.131.2.1 jruoho rootdir = cwdi->cwdi_rdir;
527 1.10 cgd }
528 1.123 dholland
529 1.131.2.1 jruoho /* emulation root dir, if any */
530 1.131.2.1 jruoho if ((cnp->cn_flags & TRYEMULROOT) == 0) {
531 1.131.2.1 jruoho /* if we don't want it, don't fetch it */
532 1.131.2.1 jruoho erootdir = NULL;
533 1.131.2.1 jruoho } else if (cnp->cn_flags & EMULROOTSET) {
534 1.131.2.1 jruoho /* explicitly set emulroot; "/../" doesn't override this */
535 1.131.2.1 jruoho erootdir = ndp->ni_erootdir;
536 1.131.2.1 jruoho } else if (!strncmp(ndp->ni_pnbuf, "/../", 4)) {
537 1.131.2.1 jruoho /* explicit reference to real rootdir */
538 1.131.2.1 jruoho erootdir = NULL;
539 1.131.2.1 jruoho } else {
540 1.131.2.1 jruoho /* may be null */
541 1.131.2.1 jruoho erootdir = cwdi->cwdi_edir;
542 1.131.2.1 jruoho }
543 1.21 kleink
544 1.131.2.1 jruoho /* current dir */
545 1.131.2.1 jruoho curdir = cwdi->cwdi_cdir;
546 1.85 dsl
547 1.131.2.1 jruoho if (ndp->ni_pnbuf[0] != '/') {
548 1.131.2.1 jruoho startdir = curdir;
549 1.131.2.1 jruoho erootdir = NULL;
550 1.131.2.1 jruoho } else if (cnp->cn_flags & TRYEMULROOT && erootdir != NULL) {
551 1.131.2.1 jruoho startdir = erootdir;
552 1.23 mycroft } else {
553 1.131.2.1 jruoho startdir = rootdir;
554 1.131.2.1 jruoho erootdir = NULL;
555 1.23 mycroft }
556 1.131.2.1 jruoho
557 1.131.2.1 jruoho state->ndp->ni_rootdir = rootdir;
558 1.131.2.1 jruoho state->ndp->ni_erootdir = erootdir;
559 1.117 dholland
560 1.117 dholland /*
561 1.131.2.1 jruoho * Get a reference to the start dir so we can safely unlock cwdi.
562 1.131.2.1 jruoho *
563 1.131.2.1 jruoho * XXX: should we hold references to rootdir and erootdir while
564 1.131.2.1 jruoho * we're running? What happens if a multithreaded process chroots
565 1.131.2.1 jruoho * during namei?
566 1.117 dholland */
567 1.131.2.1 jruoho vref(startdir);
568 1.131.2.1 jruoho
569 1.131.2.1 jruoho rw_exit(&cwdi->cwdi_lock);
570 1.131.2.1 jruoho return startdir;
571 1.131.2.1 jruoho }
572 1.131.2.1 jruoho
573 1.131.2.1 jruoho /*
574 1.131.2.1 jruoho * Get the directory context for the nfsd case, in parallel to
575 1.131.2.1 jruoho * getstartdir. Initializes the rootdir and erootdir state and
576 1.131.2.1 jruoho * returns a reference to the passed-in starting dir.
577 1.131.2.1 jruoho */
578 1.131.2.1 jruoho static struct vnode *
579 1.131.2.1 jruoho namei_getstartdir_for_nfsd(struct namei_state *state, struct vnode *startdir)
580 1.131.2.1 jruoho {
581 1.131.2.1 jruoho /* always use the real root, and never set an emulation root */
582 1.131.2.1 jruoho state->ndp->ni_rootdir = rootvnode;
583 1.131.2.1 jruoho state->ndp->ni_erootdir = NULL;
584 1.131.2.1 jruoho
585 1.131.2.1 jruoho vref(startdir);
586 1.131.2.1 jruoho return startdir;
587 1.131.2.1 jruoho }
588 1.131.2.1 jruoho
589 1.131.2.1 jruoho
590 1.131.2.1 jruoho /*
591 1.131.2.1 jruoho * Ktrace the namei operation.
592 1.131.2.1 jruoho */
593 1.131.2.1 jruoho static void
594 1.131.2.1 jruoho namei_ktrace(struct namei_state *state)
595 1.131.2.1 jruoho {
596 1.131.2.1 jruoho struct nameidata *ndp = state->ndp;
597 1.131.2.1 jruoho struct componentname *cnp = state->cnp;
598 1.131.2.1 jruoho struct lwp *self = curlwp; /* thread doing namei() */
599 1.131.2.1 jruoho const char *emul_path;
600 1.131.2.1 jruoho
601 1.97 ad if (ktrpoint(KTR_NAMEI)) {
602 1.90 dsl if (ndp->ni_erootdir != NULL) {
603 1.89 dsl /*
604 1.89 dsl * To make any sense, the trace entry need to have the
605 1.89 dsl * text of the emulation path prepended.
606 1.89 dsl * Usually we can get this from the current process,
607 1.89 dsl * but when called from emul_find_interp() it is only
608 1.89 dsl * in the exec_package - so we get it passed in ni_next
609 1.89 dsl * (this is a hack).
610 1.89 dsl */
611 1.88 dsl if (cnp->cn_flags & EMULROOTSET)
612 1.89 dsl emul_path = ndp->ni_next;
613 1.88 dsl else
614 1.117 dholland emul_path = self->l_proc->p_emul->e_path;
615 1.97 ad ktrnamei2(emul_path, strlen(emul_path),
616 1.124 dholland ndp->ni_pnbuf, ndp->ni_pathlen);
617 1.88 dsl } else
618 1.124 dholland ktrnamei(ndp->ni_pnbuf, ndp->ni_pathlen);
619 1.88 dsl }
620 1.117 dholland }
621 1.117 dholland
622 1.117 dholland /*
623 1.131.2.1 jruoho * Start up namei. Find the root dir and cwd, establish the starting
624 1.131.2.1 jruoho * directory for lookup, and lock it. Also calls ktrace when
625 1.131.2.1 jruoho * appropriate.
626 1.117 dholland */
627 1.131.2.1 jruoho static int
628 1.131.2.1 jruoho namei_start(struct namei_state *state, struct vnode *forcecwd,
629 1.131.2.1 jruoho struct vnode **startdir_ret)
630 1.117 dholland {
631 1.131.2.1 jruoho struct nameidata *ndp = state->ndp;
632 1.131.2.1 jruoho struct vnode *startdir;
633 1.131.2.1 jruoho
634 1.131.2.1 jruoho /* length includes null terminator (was originally from copyinstr) */
635 1.131.2.1 jruoho ndp->ni_pathlen = strlen(ndp->ni_pnbuf) + 1;
636 1.131.2.1 jruoho
637 1.131.2.1 jruoho /*
638 1.131.2.1 jruoho * POSIX.1 requirement: "" is not a valid file name.
639 1.131.2.1 jruoho */
640 1.131.2.1 jruoho if (ndp->ni_pathlen == 1) {
641 1.131.2.1 jruoho return ENOENT;
642 1.131.2.1 jruoho }
643 1.131.2.1 jruoho
644 1.131.2.1 jruoho ndp->ni_loopcnt = 0;
645 1.131.2.1 jruoho
646 1.131.2.1 jruoho /* Get starting directory, set up root, and ktrace. */
647 1.131.2.1 jruoho if (forcecwd != NULL) {
648 1.131.2.1 jruoho startdir = namei_getstartdir_for_nfsd(state, forcecwd);
649 1.131.2.1 jruoho /* no ktrace */
650 1.131.2.1 jruoho } else {
651 1.131.2.1 jruoho startdir = namei_getstartdir(state);
652 1.131.2.1 jruoho namei_ktrace(state);
653 1.131.2.1 jruoho }
654 1.131.2.1 jruoho
655 1.131.2.1 jruoho vn_lock(startdir, LK_EXCLUSIVE | LK_RETRY);
656 1.131.2.1 jruoho
657 1.131.2.1 jruoho *startdir_ret = startdir;
658 1.131.2.1 jruoho return 0;
659 1.117 dholland }
660 1.117 dholland
661 1.117 dholland /*
662 1.131.2.1 jruoho * Check for being at a symlink that we're going to follow.
663 1.117 dholland */
664 1.117 dholland static inline int
665 1.131.2.1 jruoho namei_atsymlink(struct namei_state *state, struct vnode *foundobj)
666 1.117 dholland {
667 1.131.2.1 jruoho return (foundobj->v_type == VLNK) &&
668 1.131.2.1 jruoho (state->cnp->cn_flags & (FOLLOW|REQUIREDIR));
669 1.117 dholland }
670 1.117 dholland
671 1.117 dholland /*
672 1.117 dholland * Follow a symlink.
673 1.131.2.1 jruoho *
674 1.131.2.1 jruoho * Updates searchdir. inhibitmagic causes magic symlinks to not be
675 1.131.2.1 jruoho * interpreted; this is used by nfsd.
676 1.131.2.1 jruoho *
677 1.131.2.1 jruoho * Unlocks foundobj on success (ugh)
678 1.117 dholland */
679 1.117 dholland static inline int
680 1.131.2.1 jruoho namei_follow(struct namei_state *state, int inhibitmagic,
681 1.131.2.1 jruoho struct vnode *searchdir, struct vnode *foundobj,
682 1.131.2.1 jruoho struct vnode **newsearchdir_ret)
683 1.117 dholland {
684 1.117 dholland struct nameidata *ndp = state->ndp;
685 1.117 dholland struct componentname *cnp = state->cnp;
686 1.117 dholland
687 1.117 dholland struct lwp *self = curlwp; /* thread doing namei() */
688 1.117 dholland struct iovec aiov; /* uio for reading symbolic links */
689 1.117 dholland struct uio auio;
690 1.117 dholland char *cp; /* pointer into pathname argument */
691 1.117 dholland size_t linklen;
692 1.117 dholland int error;
693 1.117 dholland
694 1.131.2.1 jruoho KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
695 1.131.2.1 jruoho KASSERT(VOP_ISLOCKED(foundobj) == LK_EXCLUSIVE);
696 1.117 dholland if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
697 1.117 dholland return ELOOP;
698 1.117 dholland }
699 1.131.2.1 jruoho if (foundobj->v_mount->mnt_flag & MNT_SYMPERM) {
700 1.131.2.1 jruoho error = VOP_ACCESS(foundobj, VEXEC, cnp->cn_cred);
701 1.117 dholland if (error != 0)
702 1.117 dholland return error;
703 1.117 dholland }
704 1.124 dholland
705 1.124 dholland /* FUTURE: fix this to not use a second buffer */
706 1.124 dholland cp = PNBUF_GET();
707 1.117 dholland aiov.iov_base = cp;
708 1.117 dholland aiov.iov_len = MAXPATHLEN;
709 1.117 dholland auio.uio_iov = &aiov;
710 1.117 dholland auio.uio_iovcnt = 1;
711 1.117 dholland auio.uio_offset = 0;
712 1.117 dholland auio.uio_rw = UIO_READ;
713 1.117 dholland auio.uio_resid = MAXPATHLEN;
714 1.117 dholland UIO_SETUP_SYSSPACE(&auio);
715 1.131.2.1 jruoho error = VOP_READLINK(foundobj, &auio, cnp->cn_cred);
716 1.117 dholland if (error) {
717 1.124 dholland PNBUF_PUT(cp);
718 1.117 dholland return error;
719 1.117 dholland }
720 1.117 dholland linklen = MAXPATHLEN - auio.uio_resid;
721 1.117 dholland if (linklen == 0) {
722 1.124 dholland PNBUF_PUT(cp);
723 1.124 dholland return ENOENT;
724 1.117 dholland }
725 1.117 dholland
726 1.117 dholland /*
727 1.117 dholland * Do symlink substitution, if appropriate, and
728 1.117 dholland * check length for potential overflow.
729 1.131.2.1 jruoho *
730 1.131.2.1 jruoho * Inhibit symlink substitution for nfsd.
731 1.131.2.1 jruoho * XXX: This is how it was before; is that a bug or a feature?
732 1.117 dholland */
733 1.131.2.1 jruoho if ((!inhibitmagic && vfs_magiclinks &&
734 1.117 dholland symlink_magic(self->l_proc, cp, &linklen)) ||
735 1.117 dholland (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
736 1.124 dholland PNBUF_PUT(cp);
737 1.124 dholland return ENAMETOOLONG;
738 1.117 dholland }
739 1.117 dholland if (ndp->ni_pathlen > 1) {
740 1.124 dholland /* includes a null-terminator */
741 1.117 dholland memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
742 1.124 dholland } else {
743 1.124 dholland cp[linklen] = '\0';
744 1.124 dholland }
745 1.117 dholland ndp->ni_pathlen += linklen;
746 1.124 dholland memcpy(ndp->ni_pnbuf, cp, ndp->ni_pathlen);
747 1.124 dholland PNBUF_PUT(cp);
748 1.131.2.1 jruoho
749 1.131.2.1 jruoho /* we're now starting from the beginning of the buffer again */
750 1.131.2.1 jruoho cnp->cn_nameptr = ndp->ni_pnbuf;
751 1.131.2.1 jruoho
752 1.131.2.1 jruoho /* must unlock this before relocking searchdir */
753 1.131.2.1 jruoho VOP_UNLOCK(foundobj);
754 1.117 dholland
755 1.117 dholland /*
756 1.117 dholland * Check if root directory should replace current directory.
757 1.117 dholland */
758 1.124 dholland if (ndp->ni_pnbuf[0] == '/') {
759 1.131.2.1 jruoho vput(searchdir);
760 1.117 dholland /* Keep absolute symbolic links inside emulation root */
761 1.131.2.1 jruoho searchdir = ndp->ni_erootdir;
762 1.131.2.1 jruoho if (searchdir == NULL ||
763 1.124 dholland (ndp->ni_pnbuf[1] == '.'
764 1.124 dholland && ndp->ni_pnbuf[2] == '.'
765 1.124 dholland && ndp->ni_pnbuf[3] == '/')) {
766 1.117 dholland ndp->ni_erootdir = NULL;
767 1.131.2.1 jruoho searchdir = ndp->ni_rootdir;
768 1.117 dholland }
769 1.131.2.1 jruoho vref(searchdir);
770 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
771 1.117 dholland }
772 1.117 dholland
773 1.131.2.1 jruoho *newsearchdir_ret = searchdir;
774 1.131.2.1 jruoho KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
775 1.117 dholland return 0;
776 1.117 dholland }
777 1.117 dholland
778 1.117 dholland //////////////////////////////
779 1.117 dholland
780 1.39 lukem /*
781 1.131.2.1 jruoho * Inspect the leading path component and update the state accordingly.
782 1.118 dholland */
783 1.118 dholland static int
784 1.118 dholland lookup_parsepath(struct namei_state *state)
785 1.118 dholland {
786 1.118 dholland const char *cp; /* pointer into pathname argument */
787 1.118 dholland
788 1.118 dholland struct componentname *cnp = state->cnp;
789 1.118 dholland struct nameidata *ndp = state->ndp;
790 1.118 dholland
791 1.118 dholland KASSERT(cnp == &ndp->ni_cnd);
792 1.118 dholland
793 1.10 cgd /*
794 1.10 cgd * Search a new directory.
795 1.10 cgd *
796 1.12 mycroft * The cn_hash value is for use by vfs_cache.
797 1.10 cgd * The last component of the filename is left accessible via
798 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
799 1.10 cgd * the name set the SAVENAME flag. When done, they assume
800 1.10 cgd * responsibility for freeing the pathname buffer.
801 1.127 yamt *
802 1.131.2.1 jruoho * At this point, our only vnode state is that the search dir
803 1.131.2.1 jruoho * is held and locked.
804 1.10 cgd */
805 1.12 mycroft cnp->cn_consume = 0;
806 1.39 lukem cp = NULL;
807 1.39 lukem cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
808 1.12 mycroft cnp->cn_namelen = cp - cnp->cn_nameptr;
809 1.12 mycroft if (cnp->cn_namelen > NAME_MAX) {
810 1.118 dholland return ENAMETOOLONG;
811 1.10 cgd }
812 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
813 1.10 cgd { char c = *cp;
814 1.41 soren *(char *)cp = '\0';
815 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
816 1.41 soren *(char *)cp = c; }
817 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
818 1.12 mycroft ndp->ni_pathlen -= cnp->cn_namelen;
819 1.10 cgd ndp->ni_next = cp;
820 1.23 mycroft /*
821 1.23 mycroft * If this component is followed by a slash, then move the pointer to
822 1.23 mycroft * the next component forward, and remember that this component must be
823 1.23 mycroft * a directory.
824 1.23 mycroft */
825 1.23 mycroft if (*cp == '/') {
826 1.23 mycroft do {
827 1.23 mycroft cp++;
828 1.23 mycroft } while (*cp == '/');
829 1.118 dholland state->slashes = cp - ndp->ni_next;
830 1.118 dholland ndp->ni_pathlen -= state->slashes;
831 1.23 mycroft ndp->ni_next = cp;
832 1.23 mycroft cnp->cn_flags |= REQUIREDIR;
833 1.23 mycroft } else {
834 1.118 dholland state->slashes = 0;
835 1.23 mycroft cnp->cn_flags &= ~REQUIREDIR;
836 1.23 mycroft }
837 1.23 mycroft /*
838 1.23 mycroft * We do special processing on the last component, whether or not it's
839 1.23 mycroft * a directory. Cache all intervening lookups, but not the final one.
840 1.23 mycroft */
841 1.23 mycroft if (*cp == '\0') {
842 1.118 dholland if (state->docache)
843 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
844 1.23 mycroft else
845 1.23 mycroft cnp->cn_flags &= ~MAKEENTRY;
846 1.23 mycroft cnp->cn_flags |= ISLASTCN;
847 1.23 mycroft } else {
848 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
849 1.23 mycroft cnp->cn_flags &= ~ISLASTCN;
850 1.23 mycroft }
851 1.12 mycroft if (cnp->cn_namelen == 2 &&
852 1.12 mycroft cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
853 1.12 mycroft cnp->cn_flags |= ISDOTDOT;
854 1.12 mycroft else
855 1.12 mycroft cnp->cn_flags &= ~ISDOTDOT;
856 1.10 cgd
857 1.118 dholland return 0;
858 1.118 dholland }
859 1.118 dholland
860 1.131.2.1 jruoho /*
861 1.131.2.1 jruoho * Call VOP_LOOKUP for a single lookup; return a new search directory
862 1.131.2.1 jruoho * (used when crossing mountpoints up or searching union mounts down) and
863 1.131.2.1 jruoho * the found object, which for create operations may be NULL on success.
864 1.131.2.1 jruoho */
865 1.118 dholland static int
866 1.131.2.1 jruoho lookup_once(struct namei_state *state,
867 1.131.2.1 jruoho struct vnode *searchdir,
868 1.131.2.1 jruoho struct vnode **newsearchdir_ret,
869 1.131.2.1 jruoho struct vnode **foundobj_ret)
870 1.118 dholland {
871 1.131.2.1 jruoho struct vnode *tmpvn; /* scratch vnode */
872 1.131.2.1 jruoho struct vnode *foundobj; /* result */
873 1.118 dholland struct mount *mp; /* mount table entry */
874 1.118 dholland struct lwp *l = curlwp;
875 1.118 dholland int error;
876 1.118 dholland
877 1.118 dholland struct componentname *cnp = state->cnp;
878 1.118 dholland struct nameidata *ndp = state->ndp;
879 1.118 dholland
880 1.118 dholland KASSERT(cnp == &ndp->ni_cnd);
881 1.131.2.1 jruoho KASSERT(VOP_ISLOCKED(searchdir) == LK_EXCLUSIVE);
882 1.131.2.1 jruoho *newsearchdir_ret = searchdir;
883 1.118 dholland
884 1.10 cgd /*
885 1.10 cgd * Handle "..": two special cases.
886 1.10 cgd * 1. If at root directory (e.g. after chroot)
887 1.12 mycroft * or at absolute root directory
888 1.10 cgd * then ignore it so can't get out.
889 1.85 dsl * 1a. If at the root of the emulation filesystem go to the real
890 1.85 dsl * root. So "/../<path>" is always absolute.
891 1.85 dsl * 1b. If we have somehow gotten out of a jail, warn
892 1.40 wrstuden * and also ignore it so we can't get farther out.
893 1.10 cgd * 2. If this vnode is the root of a mounted
894 1.10 cgd * filesystem, then replace it with the
895 1.10 cgd * vnode which was mounted on so we take the
896 1.10 cgd * .. in the other file system.
897 1.10 cgd */
898 1.12 mycroft if (cnp->cn_flags & ISDOTDOT) {
899 1.64 christos struct proc *p = l->l_proc;
900 1.64 christos
901 1.10 cgd for (;;) {
902 1.131.2.1 jruoho if (searchdir == ndp->ni_rootdir ||
903 1.131.2.1 jruoho searchdir == rootvnode) {
904 1.131.2.1 jruoho foundobj = searchdir;
905 1.131.2.1 jruoho vref(foundobj);
906 1.131.2.1 jruoho *foundobj_ret = foundobj;
907 1.131.2.1 jruoho error = 0;
908 1.131.2.1 jruoho goto done;
909 1.40 wrstuden }
910 1.40 wrstuden if (ndp->ni_rootdir != rootvnode) {
911 1.40 wrstuden int retval;
912 1.73 chs
913 1.131.2.1 jruoho VOP_UNLOCK(searchdir);
914 1.131.2.1 jruoho retval = vn_isunder(searchdir, ndp->ni_rootdir, l);
915 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
916 1.40 wrstuden if (!retval) {
917 1.40 wrstuden /* Oops! We got out of jail! */
918 1.40 wrstuden log(LOG_WARNING,
919 1.40 wrstuden "chrooted pid %d uid %d (%s) "
920 1.40 wrstuden "detected outside of its chroot\n",
921 1.71 ad p->p_pid, kauth_cred_geteuid(l->l_cred),
922 1.64 christos p->p_comm);
923 1.40 wrstuden /* Put us at the jail root. */
924 1.131.2.1 jruoho vput(searchdir);
925 1.131.2.1 jruoho searchdir = NULL;
926 1.131.2.1 jruoho foundobj = ndp->ni_rootdir;
927 1.131.2.1 jruoho vref(foundobj);
928 1.131.2.1 jruoho vref(foundobj);
929 1.131.2.1 jruoho vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
930 1.131.2.1 jruoho *newsearchdir_ret = foundobj;
931 1.131.2.1 jruoho *foundobj_ret = foundobj;
932 1.131.2.1 jruoho error = 0;
933 1.131.2.1 jruoho goto done;
934 1.40 wrstuden }
935 1.10 cgd }
936 1.131.2.1 jruoho if ((searchdir->v_vflag & VV_ROOT) == 0 ||
937 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT))
938 1.10 cgd break;
939 1.131.2.1 jruoho tmpvn = searchdir;
940 1.131.2.1 jruoho searchdir = searchdir->v_mount->mnt_vnodecovered;
941 1.131.2.1 jruoho vref(searchdir);
942 1.131.2.1 jruoho vput(tmpvn);
943 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
944 1.131.2.1 jruoho *newsearchdir_ret = searchdir;
945 1.10 cgd }
946 1.10 cgd }
947 1.10 cgd
948 1.10 cgd /*
949 1.10 cgd * We now have a segment name to search for, and a directory to search.
950 1.131.2.1 jruoho * Our vnode state here is that "searchdir" is held and locked.
951 1.10 cgd */
952 1.12 mycroft unionlookup:
953 1.131.2.1 jruoho foundobj = NULL;
954 1.131.2.1 jruoho error = VOP_LOOKUP(searchdir, &foundobj, cnp);
955 1.131.2.1 jruoho
956 1.73 chs if (error != 0) {
957 1.10 cgd #ifdef DIAGNOSTIC
958 1.131.2.1 jruoho if (foundobj != NULL)
959 1.43 christos panic("leaf `%s' should be empty", cnp->cn_nameptr);
960 1.52 yamt #endif /* DIAGNOSTIC */
961 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
962 1.19 christos printf("not found\n");
963 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
964 1.12 mycroft if ((error == ENOENT) &&
965 1.131.2.1 jruoho (searchdir->v_vflag & VV_ROOT) &&
966 1.131.2.1 jruoho (searchdir->v_mount->mnt_flag & MNT_UNION)) {
967 1.131.2.1 jruoho tmpvn = searchdir;
968 1.131.2.1 jruoho searchdir = searchdir->v_mount->mnt_vnodecovered;
969 1.131.2.1 jruoho vref(searchdir);
970 1.131.2.1 jruoho vput(tmpvn);
971 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
972 1.131.2.1 jruoho *newsearchdir_ret = searchdir;
973 1.12 mycroft goto unionlookup;
974 1.10 cgd }
975 1.12 mycroft
976 1.10 cgd if (error != EJUSTRETURN)
977 1.131.2.1 jruoho goto done;
978 1.73 chs
979 1.10 cgd /*
980 1.23 mycroft * If this was not the last component, or there were trailing
981 1.51 christos * slashes, and we are not going to create a directory,
982 1.51 christos * then the name must exist.
983 1.23 mycroft */
984 1.51 christos if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
985 1.131.2.1 jruoho error = ENOENT;
986 1.131.2.1 jruoho goto done;
987 1.23 mycroft }
988 1.73 chs
989 1.23 mycroft /*
990 1.10 cgd * If creating and at end of pathname, then can consider
991 1.10 cgd * allowing file to be created.
992 1.10 cgd */
993 1.118 dholland if (state->rdonly) {
994 1.131.2.1 jruoho error = EROFS;
995 1.131.2.1 jruoho goto done;
996 1.10 cgd }
997 1.73 chs
998 1.10 cgd /*
999 1.131.2.1 jruoho * We return success and a NULL foundobj to indicate
1000 1.131.2.1 jruoho * that the entry doesn't currently exist, leaving a
1001 1.131.2.1 jruoho * pointer to the (normally, locked) directory vnode
1002 1.131.2.1 jruoho * as searchdir.
1003 1.10 cgd */
1004 1.131.2.1 jruoho *foundobj_ret = NULL;
1005 1.131.2.1 jruoho error = 0;
1006 1.131.2.1 jruoho goto done;
1007 1.10 cgd }
1008 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
1009 1.19 christos printf("found\n");
1010 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
1011 1.10 cgd
1012 1.12 mycroft /*
1013 1.23 mycroft * Take into account any additional components consumed by the
1014 1.23 mycroft * underlying filesystem. This will include any trailing slashes after
1015 1.23 mycroft * the last component consumed.
1016 1.12 mycroft */
1017 1.12 mycroft if (cnp->cn_consume > 0) {
1018 1.118 dholland ndp->ni_pathlen -= cnp->cn_consume - state->slashes;
1019 1.118 dholland ndp->ni_next += cnp->cn_consume - state->slashes;
1020 1.12 mycroft cnp->cn_consume = 0;
1021 1.23 mycroft if (ndp->ni_next[0] == '\0')
1022 1.23 mycroft cnp->cn_flags |= ISLASTCN;
1023 1.12 mycroft }
1024 1.12 mycroft
1025 1.73 chs /*
1026 1.131.2.1 jruoho * "foundobj" and "searchdir" are both locked and held,
1027 1.73 chs * and may be the same vnode.
1028 1.73 chs */
1029 1.73 chs
1030 1.10 cgd /*
1031 1.10 cgd * Check to see if the vnode has been mounted on;
1032 1.10 cgd * if so find the root of the mounted file system.
1033 1.10 cgd */
1034 1.131.2.1 jruoho while (foundobj->v_type == VDIR &&
1035 1.131.2.1 jruoho (mp = foundobj->v_mountedhere) != NULL &&
1036 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1037 1.108 ad error = vfs_busy(mp, NULL);
1038 1.107 ad if (error != 0) {
1039 1.131.2.1 jruoho vput(foundobj);
1040 1.131.2.1 jruoho goto done;
1041 1.107 ad }
1042 1.131.2.1 jruoho KASSERT(searchdir != foundobj);
1043 1.131.2.1 jruoho VOP_UNLOCK(searchdir);
1044 1.131.2.1 jruoho vput(foundobj);
1045 1.131.2.1 jruoho error = VFS_ROOT(mp, &foundobj);
1046 1.106 ad vfs_unbusy(mp, false, NULL);
1047 1.32 wrstuden if (error) {
1048 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1049 1.131.2.1 jruoho goto done;
1050 1.32 wrstuden }
1051 1.131.2.1 jruoho VOP_UNLOCK(foundobj);
1052 1.131.2.1 jruoho vn_lock(searchdir, LK_EXCLUSIVE | LK_RETRY);
1053 1.131.2.1 jruoho vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
1054 1.14 mycroft }
1055 1.14 mycroft
1056 1.131.2.1 jruoho *foundobj_ret = foundobj;
1057 1.131.2.1 jruoho error = 0;
1058 1.131.2.1 jruoho done:
1059 1.131.2.1 jruoho KASSERT(VOP_ISLOCKED(*newsearchdir_ret) == LK_EXCLUSIVE);
1060 1.131.2.1 jruoho /*
1061 1.131.2.1 jruoho * *foundobj_ret is valid only if error == 0.
1062 1.131.2.1 jruoho */
1063 1.131.2.1 jruoho KASSERT(error != 0 || *foundobj_ret == NULL ||
1064 1.131.2.1 jruoho VOP_ISLOCKED(*foundobj_ret) == LK_EXCLUSIVE);
1065 1.131.2.1 jruoho return error;
1066 1.118 dholland }
1067 1.118 dholland
1068 1.131.2.1 jruoho //////////////////////////////
1069 1.131.2.1 jruoho
1070 1.131.2.1 jruoho /*
1071 1.131.2.1 jruoho * Do a complete path search from a single root directory.
1072 1.131.2.1 jruoho * (This is called up to twice if TRYEMULROOT is in effect.)
1073 1.131.2.1 jruoho */
1074 1.118 dholland static int
1075 1.131.2.1 jruoho namei_oneroot(struct namei_state *state, struct vnode *forcecwd,
1076 1.131.2.1 jruoho int neverfollow, int inhibitmagic)
1077 1.118 dholland {
1078 1.118 dholland struct nameidata *ndp = state->ndp;
1079 1.131.2.1 jruoho struct componentname *cnp = state->cnp;
1080 1.131.2.1 jruoho struct vnode *searchdir, *foundobj;
1081 1.131.2.1 jruoho const char *cp;
1082 1.131.2.1 jruoho int error;
1083 1.118 dholland
1084 1.131.2.1 jruoho error = namei_start(state, forcecwd, &searchdir);
1085 1.118 dholland if (error) {
1086 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1087 1.131.2.1 jruoho ndp->ni_vp = NULL;
1088 1.131.2.1 jruoho return error;
1089 1.118 dholland }
1090 1.118 dholland
1091 1.14 mycroft /*
1092 1.131.2.1 jruoho * Setup: break out flag bits into variables.
1093 1.14 mycroft */
1094 1.131.2.1 jruoho state->docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1095 1.131.2.1 jruoho if (cnp->cn_nameiop == DELETE)
1096 1.131.2.1 jruoho state->docache = 0;
1097 1.131.2.1 jruoho state->rdonly = cnp->cn_flags & RDONLY;
1098 1.10 cgd
1099 1.23 mycroft /*
1100 1.131.2.1 jruoho * Keep going until we run out of path components.
1101 1.23 mycroft */
1102 1.131.2.1 jruoho cnp->cn_nameptr = ndp->ni_pnbuf;
1103 1.131.2.1 jruoho for (;;) {
1104 1.23 mycroft
1105 1.131.2.1 jruoho /*
1106 1.131.2.1 jruoho * If the directory we're on is unmounted, bail out.
1107 1.131.2.1 jruoho * XXX: should this also check if it's unlinked?
1108 1.131.2.1 jruoho */
1109 1.131.2.1 jruoho if (searchdir->v_mount == NULL) {
1110 1.131.2.1 jruoho vput(searchdir);
1111 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1112 1.131.2.1 jruoho ndp->ni_vp = NULL;
1113 1.131.2.1 jruoho return (ENOENT);
1114 1.131.2.1 jruoho }
1115 1.131.2.1 jruoho
1116 1.131.2.1 jruoho /*
1117 1.131.2.1 jruoho * Look up the next path component.
1118 1.131.2.1 jruoho * (currently, this may consume more than one)
1119 1.131.2.1 jruoho */
1120 1.131.2.1 jruoho
1121 1.131.2.1 jruoho /*
1122 1.131.2.1 jruoho * If we have a leading string of slashes, remove
1123 1.131.2.1 jruoho * them, and just make sure the current node is a
1124 1.131.2.1 jruoho * directory.
1125 1.131.2.1 jruoho */
1126 1.131.2.1 jruoho cp = cnp->cn_nameptr;
1127 1.131.2.1 jruoho if (*cp == '/') {
1128 1.131.2.1 jruoho do {
1129 1.131.2.1 jruoho cp++;
1130 1.131.2.1 jruoho } while (*cp == '/');
1131 1.131.2.1 jruoho ndp->ni_pathlen -= cp - cnp->cn_nameptr;
1132 1.131.2.1 jruoho cnp->cn_nameptr = cp;
1133 1.131.2.1 jruoho
1134 1.131.2.1 jruoho if (searchdir->v_type != VDIR) {
1135 1.131.2.1 jruoho vput(searchdir);
1136 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1137 1.131.2.1 jruoho ndp->ni_vp = NULL;
1138 1.131.2.1 jruoho state->attempt_retry = 1;
1139 1.131.2.1 jruoho return ENOTDIR;
1140 1.131.2.1 jruoho }
1141 1.131.2.1 jruoho }
1142 1.131.2.1 jruoho
1143 1.131.2.1 jruoho /*
1144 1.131.2.1 jruoho * If we've exhausted the path name, then just return the
1145 1.131.2.1 jruoho * current node.
1146 1.131.2.1 jruoho */
1147 1.131.2.1 jruoho if (cnp->cn_nameptr[0] == '\0') {
1148 1.131.2.1 jruoho foundobj = searchdir;
1149 1.131.2.1 jruoho searchdir = NULL;
1150 1.131.2.1 jruoho cnp->cn_flags |= ISLASTCN;
1151 1.131.2.1 jruoho
1152 1.131.2.1 jruoho /* bleh */
1153 1.131.2.1 jruoho break;
1154 1.131.2.1 jruoho }
1155 1.131.2.1 jruoho
1156 1.131.2.1 jruoho error = lookup_parsepath(state);
1157 1.131.2.1 jruoho if (error) {
1158 1.131.2.1 jruoho vput(searchdir);
1159 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1160 1.131.2.1 jruoho ndp->ni_vp = NULL;
1161 1.131.2.1 jruoho state->attempt_retry = 1;
1162 1.131.2.1 jruoho return (error);
1163 1.131.2.1 jruoho }
1164 1.131.2.1 jruoho
1165 1.131.2.1 jruoho error = lookup_once(state, searchdir, &searchdir, &foundobj);
1166 1.131.2.1 jruoho if (error) {
1167 1.131.2.1 jruoho vput(searchdir);
1168 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1169 1.131.2.1 jruoho ndp->ni_vp = NULL;
1170 1.131.2.1 jruoho /*
1171 1.131.2.1 jruoho * Note that if we're doing TRYEMULROOT we can
1172 1.131.2.1 jruoho * retry with the normal root. Where this is
1173 1.131.2.1 jruoho * currently set matches previous practice,
1174 1.131.2.1 jruoho * but the previous practice didn't make much
1175 1.131.2.1 jruoho * sense and somebody should sit down and
1176 1.131.2.1 jruoho * figure out which cases should cause retry
1177 1.131.2.1 jruoho * and which shouldn't. XXX.
1178 1.131.2.1 jruoho */
1179 1.131.2.1 jruoho state->attempt_retry = 1;
1180 1.131.2.1 jruoho return (error);
1181 1.131.2.1 jruoho }
1182 1.131.2.1 jruoho
1183 1.131.2.1 jruoho if (foundobj == NULL) {
1184 1.131.2.1 jruoho /*
1185 1.131.2.1 jruoho * Success with no object returned means we're
1186 1.131.2.1 jruoho * creating something and it isn't already
1187 1.131.2.1 jruoho * there. Break out of the main loop now so
1188 1.131.2.1 jruoho * the code below doesn't have to test for
1189 1.131.2.1 jruoho * foundobj == NULL.
1190 1.131.2.1 jruoho */
1191 1.131.2.1 jruoho break;
1192 1.131.2.1 jruoho }
1193 1.131.2.1 jruoho
1194 1.131.2.1 jruoho /*
1195 1.131.2.1 jruoho * Check for symbolic link. If we've reached one,
1196 1.131.2.1 jruoho * follow it, unless we aren't supposed to. Back up
1197 1.131.2.1 jruoho * over any slashes that we skipped, as we will need
1198 1.131.2.1 jruoho * them again.
1199 1.131.2.1 jruoho */
1200 1.131.2.1 jruoho if (namei_atsymlink(state, foundobj)) {
1201 1.131.2.1 jruoho ndp->ni_pathlen += state->slashes;
1202 1.131.2.1 jruoho ndp->ni_next -= state->slashes;
1203 1.131.2.1 jruoho if (neverfollow) {
1204 1.131.2.1 jruoho error = EINVAL;
1205 1.131.2.1 jruoho } else {
1206 1.131.2.1 jruoho /*
1207 1.131.2.1 jruoho * dholland 20110410: if we're at a
1208 1.131.2.1 jruoho * union mount it might make sense to
1209 1.131.2.1 jruoho * use the top of the union stack here
1210 1.131.2.1 jruoho * rather than the layer we found the
1211 1.131.2.1 jruoho * symlink in. (FUTURE)
1212 1.131.2.1 jruoho */
1213 1.131.2.1 jruoho error = namei_follow(state, inhibitmagic,
1214 1.131.2.1 jruoho searchdir, foundobj,
1215 1.131.2.1 jruoho &searchdir);
1216 1.131.2.1 jruoho }
1217 1.131.2.1 jruoho if (error) {
1218 1.131.2.1 jruoho KASSERT(searchdir != foundobj);
1219 1.131.2.1 jruoho vput(searchdir);
1220 1.131.2.1 jruoho vput(foundobj);
1221 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1222 1.131.2.1 jruoho ndp->ni_vp = NULL;
1223 1.131.2.1 jruoho return error;
1224 1.131.2.1 jruoho }
1225 1.131.2.1 jruoho /* namei_follow unlocks it (ugh) so rele, not put */
1226 1.131.2.1 jruoho vrele(foundobj);
1227 1.131.2.1 jruoho foundobj = NULL;
1228 1.131.2.1 jruoho continue;
1229 1.131.2.1 jruoho }
1230 1.131.2.1 jruoho
1231 1.131.2.1 jruoho /*
1232 1.131.2.1 jruoho * Not a symbolic link.
1233 1.131.2.1 jruoho *
1234 1.131.2.1 jruoho * Check for directory, if the component was
1235 1.131.2.1 jruoho * followed by a series of slashes.
1236 1.131.2.1 jruoho */
1237 1.131.2.1 jruoho if ((foundobj->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
1238 1.131.2.1 jruoho KASSERT(foundobj != searchdir);
1239 1.131.2.1 jruoho if (searchdir) {
1240 1.131.2.1 jruoho vput(searchdir);
1241 1.131.2.1 jruoho }
1242 1.131.2.1 jruoho vput(foundobj);
1243 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1244 1.131.2.1 jruoho ndp->ni_vp = NULL;
1245 1.131.2.1 jruoho state->attempt_retry = 1;
1246 1.131.2.1 jruoho return ENOTDIR;
1247 1.131.2.1 jruoho }
1248 1.131.2.1 jruoho
1249 1.131.2.1 jruoho /*
1250 1.131.2.1 jruoho * Stop if we've reached the last component.
1251 1.131.2.1 jruoho */
1252 1.131.2.1 jruoho if (cnp->cn_flags & ISLASTCN) {
1253 1.131.2.1 jruoho break;
1254 1.131.2.1 jruoho }
1255 1.131.2.1 jruoho
1256 1.131.2.1 jruoho /*
1257 1.131.2.1 jruoho * Continue with the next component.
1258 1.131.2.1 jruoho */
1259 1.12 mycroft cnp->cn_nameptr = ndp->ni_next;
1260 1.131.2.1 jruoho if (searchdir == foundobj) {
1261 1.131.2.1 jruoho vrele(searchdir);
1262 1.73 chs } else {
1263 1.131.2.1 jruoho vput(searchdir);
1264 1.73 chs }
1265 1.131.2.1 jruoho searchdir = foundobj;
1266 1.131.2.1 jruoho foundobj = NULL;
1267 1.10 cgd }
1268 1.23 mycroft
1269 1.131.2.1 jruoho if (foundobj != NULL) {
1270 1.131.2.1 jruoho if (foundobj == ndp->ni_erootdir) {
1271 1.131.2.1 jruoho /*
1272 1.131.2.1 jruoho * We are about to return the emulation root.
1273 1.131.2.1 jruoho * This isn't a good idea because code might
1274 1.131.2.1 jruoho * repeatedly lookup ".." until the file
1275 1.131.2.1 jruoho * matches that returned for "/" and loop
1276 1.131.2.1 jruoho * forever. So convert it to the real root.
1277 1.131.2.1 jruoho */
1278 1.131.2.1 jruoho if (searchdir != NULL) {
1279 1.131.2.1 jruoho if (searchdir == foundobj)
1280 1.131.2.1 jruoho vrele(searchdir);
1281 1.131.2.1 jruoho else
1282 1.131.2.1 jruoho vput(searchdir);
1283 1.131.2.1 jruoho searchdir = NULL;
1284 1.131.2.1 jruoho }
1285 1.131.2.1 jruoho vput(foundobj);
1286 1.131.2.1 jruoho foundobj = ndp->ni_rootdir;
1287 1.131.2.1 jruoho vref(foundobj);
1288 1.131.2.1 jruoho vn_lock(foundobj, LK_EXCLUSIVE | LK_RETRY);
1289 1.131.2.1 jruoho }
1290 1.131.2.1 jruoho
1291 1.87 dsl /*
1292 1.131.2.1 jruoho * If the caller requested the parent node (i.e. it's
1293 1.131.2.1 jruoho * a CREATE, DELETE, or RENAME), and we don't have one
1294 1.131.2.1 jruoho * (because this is the root directory, or we crossed
1295 1.131.2.1 jruoho * a mount point), then we must fail.
1296 1.87 dsl */
1297 1.131.2.1 jruoho if (cnp->cn_nameiop != LOOKUP &&
1298 1.131.2.1 jruoho (searchdir == NULL ||
1299 1.131.2.1 jruoho searchdir->v_mount != foundobj->v_mount)) {
1300 1.131.2.1 jruoho if (searchdir) {
1301 1.131.2.1 jruoho vput(searchdir);
1302 1.131.2.1 jruoho }
1303 1.131.2.1 jruoho vput(foundobj);
1304 1.131.2.1 jruoho foundobj = NULL;
1305 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1306 1.131.2.1 jruoho ndp->ni_vp = NULL;
1307 1.131.2.1 jruoho state->attempt_retry = 1;
1308 1.131.2.1 jruoho
1309 1.131.2.1 jruoho switch (cnp->cn_nameiop) {
1310 1.131.2.1 jruoho case CREATE:
1311 1.131.2.1 jruoho return EEXIST;
1312 1.131.2.1 jruoho case DELETE:
1313 1.131.2.1 jruoho case RENAME:
1314 1.131.2.1 jruoho return EBUSY;
1315 1.131.2.1 jruoho default:
1316 1.131.2.1 jruoho break;
1317 1.131.2.1 jruoho }
1318 1.131.2.1 jruoho panic("Invalid nameiop\n");
1319 1.131.2.1 jruoho }
1320 1.87 dsl
1321 1.131.2.1 jruoho /*
1322 1.131.2.1 jruoho * Disallow directory write attempts on read-only lookups.
1323 1.131.2.1 jruoho * Prefers EEXIST over EROFS for the CREATE case.
1324 1.131.2.1 jruoho */
1325 1.131.2.1 jruoho if (state->rdonly &&
1326 1.131.2.1 jruoho (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1327 1.131.2.1 jruoho if (searchdir) {
1328 1.131.2.1 jruoho if (foundobj != searchdir) {
1329 1.131.2.1 jruoho vput(searchdir);
1330 1.131.2.1 jruoho } else {
1331 1.131.2.1 jruoho vrele(searchdir);
1332 1.131.2.1 jruoho }
1333 1.131.2.1 jruoho searchdir = NULL;
1334 1.131.2.1 jruoho }
1335 1.131.2.1 jruoho vput(foundobj);
1336 1.131.2.1 jruoho foundobj = NULL;
1337 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1338 1.131.2.1 jruoho ndp->ni_vp = NULL;
1339 1.131.2.1 jruoho state->attempt_retry = 1;
1340 1.131.2.1 jruoho return EROFS;
1341 1.131.2.1 jruoho }
1342 1.131.2.1 jruoho if ((cnp->cn_flags & LOCKLEAF) == 0) {
1343 1.131.2.1 jruoho /*
1344 1.131.2.1 jruoho * Note: if LOCKPARENT but not LOCKLEAF is
1345 1.131.2.1 jruoho * set, and searchdir == foundobj, this code
1346 1.131.2.1 jruoho * necessarily unlocks the parent as well as
1347 1.131.2.1 jruoho * the leaf. That is, just because you specify
1348 1.131.2.1 jruoho * LOCKPARENT doesn't mean you necessarily get
1349 1.131.2.1 jruoho * a locked parent vnode. The code in
1350 1.131.2.1 jruoho * vfs_syscalls.c, and possibly elsewhere,
1351 1.131.2.1 jruoho * that uses this combination "knows" this, so
1352 1.131.2.1 jruoho * it can't be safely changed. Feh. XXX
1353 1.131.2.1 jruoho */
1354 1.131.2.1 jruoho VOP_UNLOCK(foundobj);
1355 1.87 dsl }
1356 1.87 dsl }
1357 1.73 chs
1358 1.10 cgd /*
1359 1.131.2.1 jruoho * Done.
1360 1.10 cgd */
1361 1.131.2.1 jruoho
1362 1.131.2.1 jruoho /*
1363 1.131.2.1 jruoho * If LOCKPARENT is not set, the parent directory isn't returned.
1364 1.131.2.1 jruoho */
1365 1.131.2.1 jruoho if ((cnp->cn_flags & LOCKPARENT) == 0 && searchdir != NULL) {
1366 1.131.2.1 jruoho if (searchdir == foundobj) {
1367 1.131.2.1 jruoho vrele(searchdir);
1368 1.131.2.1 jruoho } else {
1369 1.131.2.1 jruoho vput(searchdir);
1370 1.73 chs }
1371 1.131.2.1 jruoho searchdir = NULL;
1372 1.73 chs }
1373 1.10 cgd
1374 1.131.2.1 jruoho ndp->ni_dvp = searchdir;
1375 1.131.2.1 jruoho ndp->ni_vp = foundobj;
1376 1.131.2.1 jruoho return 0;
1377 1.12 mycroft }
1378 1.12 mycroft
1379 1.131.2.1 jruoho /*
1380 1.131.2.1 jruoho * Do namei; wrapper layer that handles TRYEMULROOT.
1381 1.131.2.1 jruoho */
1382 1.131 dholland static int
1383 1.131.2.1 jruoho namei_tryemulroot(struct namei_state *state, struct vnode *forcecwd,
1384 1.131.2.1 jruoho int neverfollow, int inhibitmagic)
1385 1.131 dholland {
1386 1.131 dholland int error;
1387 1.131 dholland
1388 1.131 dholland struct nameidata *ndp = state->ndp;
1389 1.131 dholland struct componentname *cnp = state->cnp;
1390 1.131 dholland const char *savepath = NULL;
1391 1.131 dholland
1392 1.131 dholland KASSERT(cnp == &ndp->ni_cnd);
1393 1.131 dholland
1394 1.131 dholland if (cnp->cn_flags & TRYEMULROOT) {
1395 1.131 dholland savepath = pathbuf_stringcopy_get(ndp->ni_pathbuf);
1396 1.131 dholland }
1397 1.131 dholland
1398 1.131 dholland emul_retry:
1399 1.131.2.1 jruoho state->attempt_retry = 0;
1400 1.131 dholland
1401 1.131.2.1 jruoho error = namei_oneroot(state, forcecwd, neverfollow, inhibitmagic);
1402 1.131 dholland if (error) {
1403 1.131 dholland /*
1404 1.131.2.1 jruoho * Once namei has started up, the existence of ni_erootdir
1405 1.131.2.1 jruoho * tells us whether we're working from an emulation root.
1406 1.131.2.1 jruoho * The TRYEMULROOT flag isn't necessarily authoritative.
1407 1.131 dholland */
1408 1.131.2.1 jruoho if (ndp->ni_erootdir != NULL && state->attempt_retry) {
1409 1.131.2.1 jruoho /* Retry the whole thing using the normal root */
1410 1.131.2.1 jruoho cnp->cn_flags &= ~TRYEMULROOT;
1411 1.131.2.1 jruoho state->attempt_retry = 0;
1412 1.131 dholland
1413 1.131.2.1 jruoho /* kinda gross */
1414 1.131.2.1 jruoho strcpy(ndp->ni_pathbuf->pb_path, savepath);
1415 1.131.2.1 jruoho pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1416 1.131.2.1 jruoho savepath = NULL;
1417 1.131 dholland
1418 1.131.2.1 jruoho goto emul_retry;
1419 1.131 dholland }
1420 1.131 dholland }
1421 1.131 dholland if (savepath != NULL) {
1422 1.131 dholland pathbuf_stringcopy_put(ndp->ni_pathbuf, savepath);
1423 1.131 dholland }
1424 1.131.2.1 jruoho return error;
1425 1.131 dholland }
1426 1.131 dholland
1427 1.131.2.1 jruoho /*
1428 1.131.2.1 jruoho * External interface.
1429 1.131.2.1 jruoho */
1430 1.131 dholland int
1431 1.131 dholland namei(struct nameidata *ndp)
1432 1.131 dholland {
1433 1.131 dholland struct namei_state state;
1434 1.131 dholland int error;
1435 1.131 dholland
1436 1.131 dholland namei_init(&state, ndp);
1437 1.131.2.1 jruoho error = namei_tryemulroot(&state, NULL,
1438 1.131.2.1 jruoho 0/*!neverfollow*/, 0/*!inhibitmagic*/);
1439 1.131 dholland namei_cleanup(&state);
1440 1.131 dholland
1441 1.131.2.1 jruoho if (error) {
1442 1.131.2.1 jruoho /* make sure no stray refs leak out */
1443 1.131.2.1 jruoho KASSERT(ndp->ni_dvp == NULL);
1444 1.131.2.1 jruoho KASSERT(ndp->ni_vp == NULL);
1445 1.131.2.1 jruoho }
1446 1.131.2.1 jruoho
1447 1.131 dholland return error;
1448 1.131 dholland }
1449 1.131 dholland
1450 1.131 dholland ////////////////////////////////////////////////////////////
1451 1.131 dholland
1452 1.12 mycroft /*
1453 1.131.2.1 jruoho * External interface used by nfsd. This is basically different from
1454 1.131.2.1 jruoho * namei only in that it has the ability to pass in the "current
1455 1.131.2.1 jruoho * directory", and uses an extra flag "neverfollow" for which there's
1456 1.131.2.1 jruoho * no physical flag defined in namei.h. (There used to be a cut&paste
1457 1.131.2.1 jruoho * copy of about half of namei in nfsd to allow these minor
1458 1.131.2.1 jruoho * adjustments to exist.)
1459 1.119 dholland *
1460 1.131.2.1 jruoho * XXX: the namei interface should be adjusted so nfsd can just use
1461 1.131.2.1 jruoho * ordinary namei().
1462 1.118 dholland */
1463 1.119 dholland int
1464 1.131.2.1 jruoho lookup_for_nfsd(struct nameidata *ndp, struct vnode *forcecwd, int neverfollow)
1465 1.119 dholland {
1466 1.119 dholland struct namei_state state;
1467 1.119 dholland int error;
1468 1.119 dholland
1469 1.119 dholland namei_init(&state, ndp);
1470 1.131.2.1 jruoho error = namei_tryemulroot(&state, forcecwd,
1471 1.131.2.1 jruoho neverfollow, 1/*inhibitmagic*/);
1472 1.131.2.1 jruoho namei_cleanup(&state);
1473 1.120 dholland
1474 1.131.2.1 jruoho if (error) {
1475 1.131.2.1 jruoho /* make sure no stray refs leak out */
1476 1.131.2.1 jruoho KASSERT(ndp->ni_dvp == NULL);
1477 1.131.2.1 jruoho KASSERT(ndp->ni_vp == NULL);
1478 1.131.2.1 jruoho }
1479 1.131.2.1 jruoho
1480 1.131.2.1 jruoho return error;
1481 1.131.2.1 jruoho }
1482 1.131.2.1 jruoho
1483 1.131.2.1 jruoho /*
1484 1.131.2.1 jruoho * A second external interface used by nfsd. This turns out to be a
1485 1.131.2.1 jruoho * single lookup used by the WebNFS code (ha!) to get "index.html" or
1486 1.131.2.1 jruoho * equivalent when asked for a directory. It should eventually evolve
1487 1.131.2.1 jruoho * into some kind of namei_once() call; for the time being it's kind
1488 1.131.2.1 jruoho * of a mess. XXX.
1489 1.131.2.1 jruoho *
1490 1.131.2.1 jruoho * dholland 20110109: I don't think it works, and I don't think it
1491 1.131.2.1 jruoho * worked before I started hacking and slashing either, and I doubt
1492 1.131.2.1 jruoho * anyone will ever notice.
1493 1.131.2.1 jruoho */
1494 1.120 dholland
1495 1.131.2.1 jruoho /*
1496 1.131.2.1 jruoho * Internals. This calls lookup_once() after setting up the assorted
1497 1.131.2.1 jruoho * pieces of state the way they ought to be.
1498 1.131.2.1 jruoho */
1499 1.131.2.1 jruoho static int
1500 1.131.2.1 jruoho do_lookup_for_nfsd_index(struct namei_state *state, struct vnode *startdir)
1501 1.131.2.1 jruoho {
1502 1.131.2.1 jruoho int error = 0;
1503 1.120 dholland
1504 1.131.2.1 jruoho struct componentname *cnp = state->cnp;
1505 1.131.2.1 jruoho struct nameidata *ndp = state->ndp;
1506 1.131.2.1 jruoho struct vnode *foundobj;
1507 1.131.2.1 jruoho const char *cp; /* pointer into pathname argument */
1508 1.120 dholland
1509 1.131.2.1 jruoho KASSERT(cnp == &ndp->ni_cnd);
1510 1.120 dholland
1511 1.131.2.1 jruoho cnp->cn_nameptr = ndp->ni_pnbuf;
1512 1.131.2.1 jruoho state->docache = 1;
1513 1.131.2.1 jruoho state->rdonly = cnp->cn_flags & RDONLY;
1514 1.131.2.1 jruoho ndp->ni_dvp = NULL;
1515 1.120 dholland
1516 1.131.2.1 jruoho cnp->cn_consume = 0;
1517 1.131.2.1 jruoho cp = NULL;
1518 1.131.2.1 jruoho cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
1519 1.131.2.1 jruoho cnp->cn_namelen = cp - cnp->cn_nameptr;
1520 1.131.2.1 jruoho KASSERT(cnp->cn_namelen <= NAME_MAX);
1521 1.131.2.1 jruoho ndp->ni_pathlen -= cnp->cn_namelen;
1522 1.131.2.1 jruoho ndp->ni_next = cp;
1523 1.131.2.1 jruoho state->slashes = 0;
1524 1.131.2.1 jruoho cnp->cn_flags &= ~REQUIREDIR;
1525 1.131.2.1 jruoho cnp->cn_flags |= MAKEENTRY|ISLASTCN;
1526 1.120 dholland
1527 1.131.2.1 jruoho if (cnp->cn_namelen == 2 &&
1528 1.131.2.1 jruoho cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1529 1.131.2.1 jruoho cnp->cn_flags |= ISDOTDOT;
1530 1.131.2.1 jruoho else
1531 1.131.2.1 jruoho cnp->cn_flags &= ~ISDOTDOT;
1532 1.120 dholland
1533 1.120 dholland /*
1534 1.131.2.1 jruoho * Because lookup_once can change the startdir, we need our
1535 1.131.2.1 jruoho * own reference to it to avoid consuming the caller's.
1536 1.120 dholland */
1537 1.131.2.1 jruoho vref(startdir);
1538 1.131.2.1 jruoho vn_lock(startdir, LK_EXCLUSIVE | LK_RETRY);
1539 1.131.2.1 jruoho error = lookup_once(state, startdir, &startdir, &foundobj);
1540 1.131.2.1 jruoho vput(startdir);
1541 1.131.2.1 jruoho if (error) {
1542 1.131.2.1 jruoho goto bad;
1543 1.120 dholland }
1544 1.131.2.1 jruoho ndp->ni_vp = foundobj;
1545 1.120 dholland
1546 1.131.2.1 jruoho if (foundobj == NULL) {
1547 1.131.2.1 jruoho return 0;
1548 1.131.2.1 jruoho }
1549 1.120 dholland
1550 1.131.2.1 jruoho KASSERT((cnp->cn_flags & LOCKPARENT) == 0);
1551 1.131.2.1 jruoho if ((cnp->cn_flags & LOCKLEAF) == 0) {
1552 1.131.2.1 jruoho VOP_UNLOCK(foundobj);
1553 1.131.2.1 jruoho }
1554 1.131.2.1 jruoho return (0);
1555 1.119 dholland
1556 1.131.2.1 jruoho bad:
1557 1.131.2.1 jruoho ndp->ni_vp = NULL;
1558 1.131.2.1 jruoho return (error);
1559 1.119 dholland }
1560 1.119 dholland
1561 1.131.2.1 jruoho /*
1562 1.131.2.1 jruoho * External interface. The partitioning between this function and the
1563 1.131.2.1 jruoho * above isn't very clear - the above function exists mostly so code
1564 1.131.2.1 jruoho * that uses "state->" can be shuffled around without having to change
1565 1.131.2.1 jruoho * it to "state.".
1566 1.131.2.1 jruoho */
1567 1.118 dholland int
1568 1.128 dholland lookup_for_nfsd_index(struct nameidata *ndp, struct vnode *startdir)
1569 1.118 dholland {
1570 1.118 dholland struct namei_state state;
1571 1.118 dholland int error;
1572 1.118 dholland
1573 1.131.2.1 jruoho /*
1574 1.131.2.1 jruoho * Note: the name sent in here (is not|should not be) allowed
1575 1.131.2.1 jruoho * to contain a slash.
1576 1.131.2.1 jruoho */
1577 1.131.2.1 jruoho if (strlen(ndp->ni_pathbuf->pb_path) > NAME_MAX) {
1578 1.131.2.1 jruoho return ENAMETOOLONG;
1579 1.131.2.1 jruoho }
1580 1.131.2.1 jruoho if (strchr(ndp->ni_pathbuf->pb_path, '/')) {
1581 1.131.2.1 jruoho return EINVAL;
1582 1.131.2.1 jruoho }
1583 1.128 dholland
1584 1.131.2.1 jruoho ndp->ni_pathlen = strlen(ndp->ni_pathbuf->pb_path) + 1;
1585 1.131.2.1 jruoho ndp->ni_pnbuf = NULL;
1586 1.131.2.1 jruoho ndp->ni_cnd.cn_nameptr = NULL;
1587 1.124 dholland
1588 1.118 dholland namei_init(&state, ndp);
1589 1.131.2.1 jruoho error = do_lookup_for_nfsd_index(&state, startdir);
1590 1.118 dholland namei_cleanup(&state);
1591 1.118 dholland
1592 1.118 dholland return error;
1593 1.118 dholland }
1594 1.118 dholland
1595 1.131 dholland ////////////////////////////////////////////////////////////
1596 1.131 dholland
1597 1.118 dholland /*
1598 1.12 mycroft * Reacquire a path name component.
1599 1.73 chs * dvp is locked on entry and exit.
1600 1.73 chs * *vpp is locked on exit unless it's NULL.
1601 1.12 mycroft */
1602 1.12 mycroft int
1603 1.130 dholland relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int dummy)
1604 1.12 mycroft {
1605 1.12 mycroft int rdonly; /* lookup read-only flag bit */
1606 1.12 mycroft int error = 0;
1607 1.52 yamt #ifdef DEBUG
1608 1.81 chs uint32_t newhash; /* DEBUG: check name hash */
1609 1.41 soren const char *cp; /* DEBUG: check name ptr/len */
1610 1.52 yamt #endif /* DEBUG */
1611 1.12 mycroft
1612 1.130 dholland (void)dummy;
1613 1.130 dholland
1614 1.12 mycroft /*
1615 1.12 mycroft * Setup: break out flag bits into variables.
1616 1.12 mycroft */
1617 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
1618 1.12 mycroft
1619 1.12 mycroft /*
1620 1.12 mycroft * Search a new directory.
1621 1.12 mycroft *
1622 1.12 mycroft * The cn_hash value is for use by vfs_cache.
1623 1.12 mycroft * The last component of the filename is left accessible via
1624 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
1625 1.12 mycroft * the name set the SAVENAME flag. When done, they assume
1626 1.12 mycroft * responsibility for freeing the pathname buffer.
1627 1.12 mycroft */
1628 1.52 yamt #ifdef DEBUG
1629 1.39 lukem cp = NULL;
1630 1.39 lukem newhash = namei_hash(cnp->cn_nameptr, &cp);
1631 1.81 chs if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
1632 1.12 mycroft panic("relookup: bad hash");
1633 1.12 mycroft if (cnp->cn_namelen != cp - cnp->cn_nameptr)
1634 1.58 christos panic("relookup: bad len");
1635 1.53 yamt while (*cp == '/')
1636 1.53 yamt cp++;
1637 1.12 mycroft if (*cp != 0)
1638 1.12 mycroft panic("relookup: not last component");
1639 1.52 yamt #endif /* DEBUG */
1640 1.12 mycroft
1641 1.12 mycroft /*
1642 1.12 mycroft * Check for degenerate name (e.g. / or "")
1643 1.12 mycroft * which is a way of talking about a directory,
1644 1.12 mycroft * e.g. like "/." or ".".
1645 1.12 mycroft */
1646 1.23 mycroft if (cnp->cn_nameptr[0] == '\0')
1647 1.23 mycroft panic("relookup: null name");
1648 1.12 mycroft
1649 1.12 mycroft if (cnp->cn_flags & ISDOTDOT)
1650 1.58 christos panic("relookup: lookup on dot-dot");
1651 1.12 mycroft
1652 1.12 mycroft /*
1653 1.12 mycroft * We now have a segment name to search for, and a directory to search.
1654 1.12 mycroft */
1655 1.129 dholland cnp->cn_flags |= INRELOOKUP;
1656 1.129 dholland error = VOP_LOOKUP(dvp, vpp, cnp);
1657 1.129 dholland cnp->cn_flags &= ~INRELOOKUP;
1658 1.129 dholland if ((error) != 0) {
1659 1.12 mycroft #ifdef DIAGNOSTIC
1660 1.12 mycroft if (*vpp != NULL)
1661 1.43 christos panic("leaf `%s' should be empty", cnp->cn_nameptr);
1662 1.12 mycroft #endif
1663 1.12 mycroft if (error != EJUSTRETURN)
1664 1.12 mycroft goto bad;
1665 1.12 mycroft }
1666 1.12 mycroft
1667 1.12 mycroft #ifdef DIAGNOSTIC
1668 1.12 mycroft /*
1669 1.12 mycroft * Check for symbolic link
1670 1.12 mycroft */
1671 1.81 chs if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1672 1.58 christos panic("relookup: symlink found");
1673 1.12 mycroft #endif
1674 1.12 mycroft
1675 1.12 mycroft /*
1676 1.94 pooka * Check for read-only lookups.
1677 1.12 mycroft */
1678 1.81 chs if (rdonly && cnp->cn_nameiop != LOOKUP) {
1679 1.26 fvdl error = EROFS;
1680 1.81 chs if (*vpp) {
1681 1.81 chs vput(*vpp);
1682 1.81 chs }
1683 1.73 chs goto bad;
1684 1.12 mycroft }
1685 1.12 mycroft return (0);
1686 1.12 mycroft
1687 1.12 mycroft bad:
1688 1.12 mycroft *vpp = NULL;
1689 1.10 cgd return (error);
1690 1.10 cgd }
1691 1.116 dholland
1692 1.116 dholland /*
1693 1.116 dholland * namei_simple - simple forms of namei.
1694 1.116 dholland *
1695 1.116 dholland * These are wrappers to allow the simple case callers of namei to be
1696 1.116 dholland * left alone while everything else changes under them.
1697 1.116 dholland */
1698 1.116 dholland
1699 1.116 dholland /* Flags */
1700 1.116 dholland struct namei_simple_flags_type {
1701 1.116 dholland int dummy;
1702 1.116 dholland };
1703 1.116 dholland static const struct namei_simple_flags_type ns_nn, ns_nt, ns_fn, ns_ft;
1704 1.116 dholland const namei_simple_flags_t NSM_NOFOLLOW_NOEMULROOT = &ns_nn;
1705 1.116 dholland const namei_simple_flags_t NSM_NOFOLLOW_TRYEMULROOT = &ns_nt;
1706 1.116 dholland const namei_simple_flags_t NSM_FOLLOW_NOEMULROOT = &ns_fn;
1707 1.116 dholland const namei_simple_flags_t NSM_FOLLOW_TRYEMULROOT = &ns_ft;
1708 1.116 dholland
1709 1.116 dholland static
1710 1.116 dholland int
1711 1.116 dholland namei_simple_convert_flags(namei_simple_flags_t sflags)
1712 1.116 dholland {
1713 1.116 dholland if (sflags == NSM_NOFOLLOW_NOEMULROOT)
1714 1.116 dholland return NOFOLLOW | 0;
1715 1.116 dholland if (sflags == NSM_NOFOLLOW_TRYEMULROOT)
1716 1.116 dholland return NOFOLLOW | TRYEMULROOT;
1717 1.116 dholland if (sflags == NSM_FOLLOW_NOEMULROOT)
1718 1.116 dholland return FOLLOW | 0;
1719 1.116 dholland if (sflags == NSM_FOLLOW_TRYEMULROOT)
1720 1.116 dholland return FOLLOW | TRYEMULROOT;
1721 1.116 dholland panic("namei_simple_convert_flags: bogus sflags\n");
1722 1.116 dholland return 0;
1723 1.116 dholland }
1724 1.116 dholland
1725 1.116 dholland int
1726 1.116 dholland namei_simple_kernel(const char *path, namei_simple_flags_t sflags,
1727 1.116 dholland struct vnode **vp_ret)
1728 1.116 dholland {
1729 1.116 dholland struct nameidata nd;
1730 1.123 dholland struct pathbuf *pb;
1731 1.116 dholland int err;
1732 1.116 dholland
1733 1.123 dholland pb = pathbuf_create(path);
1734 1.123 dholland if (pb == NULL) {
1735 1.123 dholland return ENOMEM;
1736 1.123 dholland }
1737 1.123 dholland
1738 1.116 dholland NDINIT(&nd,
1739 1.116 dholland LOOKUP,
1740 1.116 dholland namei_simple_convert_flags(sflags),
1741 1.123 dholland pb);
1742 1.116 dholland err = namei(&nd);
1743 1.116 dholland if (err != 0) {
1744 1.123 dholland pathbuf_destroy(pb);
1745 1.116 dholland return err;
1746 1.116 dholland }
1747 1.116 dholland *vp_ret = nd.ni_vp;
1748 1.123 dholland pathbuf_destroy(pb);
1749 1.116 dholland return 0;
1750 1.116 dholland }
1751 1.116 dholland
1752 1.116 dholland int
1753 1.116 dholland namei_simple_user(const char *path, namei_simple_flags_t sflags,
1754 1.116 dholland struct vnode **vp_ret)
1755 1.116 dholland {
1756 1.123 dholland struct pathbuf *pb;
1757 1.116 dholland struct nameidata nd;
1758 1.116 dholland int err;
1759 1.116 dholland
1760 1.123 dholland err = pathbuf_copyin(path, &pb);
1761 1.123 dholland if (err) {
1762 1.123 dholland return err;
1763 1.123 dholland }
1764 1.123 dholland
1765 1.116 dholland NDINIT(&nd,
1766 1.116 dholland LOOKUP,
1767 1.116 dholland namei_simple_convert_flags(sflags),
1768 1.123 dholland pb);
1769 1.116 dholland err = namei(&nd);
1770 1.116 dholland if (err != 0) {
1771 1.123 dholland pathbuf_destroy(pb);
1772 1.116 dholland return err;
1773 1.116 dholland }
1774 1.116 dholland *vp_ret = nd.ni_vp;
1775 1.123 dholland pathbuf_destroy(pb);
1776 1.116 dholland return 0;
1777 1.116 dholland }
1778