vfs_lookup.c revision 1.61 1 /* $NetBSD: vfs_lookup.c,v 1.61 2005/06/23 00:30:28 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.61 2005/06/23 00:30:28 thorpej Exp $");
41
42 #include "opt_ktrace.h"
43 #include "opt_systrace.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/syslimits.h>
49 #include <sys/time.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/errno.h>
54 #include <sys/filedesc.h>
55 #include <sys/hash.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/syslog.h>
59
60 #ifdef KTRACE
61 #include <sys/ktrace.h>
62 #endif
63 #ifdef SYSTRACE
64 #include <sys/systrace.h>
65 #endif
66
67 struct pool pnbuf_pool; /* pathname buffer pool */
68 struct pool_cache pnbuf_cache; /* pathname buffer cache */
69
70 MALLOC_DEFINE(M_NAMEI, "namei", "namei path buffer");
71
72 /*
73 * Substitute replacement text for 'magic' strings in symlinks.
74 * Returns 0 if successful, and returns non-zero if an error
75 * occurs. (Currently, the only possible error is running out
76 * of temporary pathname space.)
77 *
78 * Looks for "@<string>" and "@<string>/", where <string> is a
79 * recognized 'magic' string. Replaces the "@<string>" with the
80 * appropriate replacement text. (Note that in some cases the
81 * replacement text may have zero length.)
82 *
83 * This would have been table driven, but the variance in
84 * replacement strings (and replacement string lengths) made
85 * that impractical.
86 */
87 #define MATCH(str) \
88 ((i + (sizeof(str) - 1) == *len) || \
89 ((i + (sizeof(str) - 1) < *len) && \
90 (cp[i + sizeof(str) - 1] == '/'))) && \
91 !strncmp((str), &cp[i], sizeof(str) - 1)
92
93 #define SUBSTITUTE(m, s, sl) \
94 if ((newlen + (sl)) > MAXPATHLEN) \
95 return (1); \
96 i += sizeof(m) - 1; \
97 memcpy(&tmp[newlen], (s), (sl)); \
98 newlen += (sl); \
99 change = 1;
100
101 static int
102 symlink_magic(char *cp, int *len)
103 {
104 char tmp[MAXPATHLEN];
105 int change, i, newlen;
106
107 for (change = i = newlen = 0; i < *len; ) {
108 if (cp[i] != '@')
109 tmp[newlen++] = cp[i++];
110 else {
111 i++;
112 /*
113 * The following checks should be ordered according
114 * to frequency of use.
115 */
116 if (MATCH("machine_arch")) {
117 SUBSTITUTE("machine_arch", MACHINE_ARCH,
118 sizeof(MACHINE_ARCH) - 1);
119 } else if (MATCH("machine")) {
120 SUBSTITUTE("machine", MACHINE,
121 sizeof(MACHINE) - 1);
122 } else if (MATCH("hostname")) {
123 SUBSTITUTE("hostname", hostname,
124 hostnamelen);
125 } else if (MATCH("osrelease")) {
126 SUBSTITUTE("osrelease", osrelease,
127 strlen(osrelease));
128 } else if (MATCH("kernel_ident")) {
129 SUBSTITUTE("kernel_ident", kernel_ident,
130 strlen(kernel_ident));
131 } else if (MATCH("domainname")) {
132 SUBSTITUTE("domainname", domainname,
133 domainnamelen);
134 } else if (MATCH("ostype")) {
135 SUBSTITUTE("ostype", ostype,
136 strlen(ostype));
137 } else
138 tmp[newlen++] = '@';
139 }
140 }
141
142 if (! change)
143 return (0);
144
145 memcpy(cp, tmp, newlen);
146 *len = newlen;
147
148 return (0);
149 }
150
151 /*
152 * Convert a pathname into a pointer to a locked inode.
153 *
154 * The FOLLOW flag is set when symbolic links are to be followed
155 * when they occur at the end of the name translation process.
156 * Symbolic links are always followed for all other pathname
157 * components other than the last.
158 *
159 * The segflg defines whether the name is to be copied from user
160 * space or kernel space.
161 *
162 * Overall outline of namei:
163 *
164 * copy in name
165 * get starting directory
166 * while (!done && !error) {
167 * call lookup to search path.
168 * if symbolic link, massage name in buffer and continue
169 * }
170 */
171 int
172 namei(struct nameidata *ndp)
173 {
174 struct cwdinfo *cwdi; /* pointer to cwd state */
175 char *cp; /* pointer into pathname argument */
176 struct vnode *dp; /* the directory we are searching */
177 struct iovec aiov; /* uio for reading symbolic links */
178 struct uio auio;
179 int error, linklen;
180 struct componentname *cnp = &ndp->ni_cnd;
181
182 #ifdef DIAGNOSTIC
183 if (!cnp->cn_cred || !cnp->cn_proc)
184 panic("namei: bad cred/proc");
185 if (cnp->cn_nameiop & (~OPMASK))
186 panic("namei: nameiop contaminated with flags");
187 if (cnp->cn_flags & OPMASK)
188 panic("namei: flags contaminated with nameiops");
189 #endif
190 cwdi = cnp->cn_proc->p_cwdi;
191
192 /*
193 * Get a buffer for the name to be translated, and copy the
194 * name into the buffer.
195 */
196 if ((cnp->cn_flags & HASBUF) == 0)
197 cnp->cn_pnbuf = PNBUF_GET();
198 if (ndp->ni_segflg == UIO_SYSSPACE)
199 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
200 MAXPATHLEN, &ndp->ni_pathlen);
201 else
202 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
203 MAXPATHLEN, &ndp->ni_pathlen);
204
205 /*
206 * POSIX.1 requirement: "" is not a valid file name.
207 */
208 if (!error && ndp->ni_pathlen == 1)
209 error = ENOENT;
210
211 if (error) {
212 PNBUF_PUT(cnp->cn_pnbuf);
213 ndp->ni_vp = NULL;
214 return (error);
215 }
216 ndp->ni_loopcnt = 0;
217
218 #ifdef KTRACE
219 if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
220 ktrnamei(cnp->cn_proc, cnp->cn_pnbuf);
221 #endif
222 #ifdef SYSTRACE
223 if (ISSET(cnp->cn_proc->p_flag, P_SYSTRACE))
224 systrace_namei(ndp);
225 #endif
226
227 /*
228 * Get starting point for the translation.
229 */
230 if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
231 ndp->ni_rootdir = rootvnode;
232 /*
233 * Check if starting from root directory or current directory.
234 */
235 if (cnp->cn_pnbuf[0] == '/') {
236 dp = ndp->ni_rootdir;
237 VREF(dp);
238 } else {
239 dp = cwdi->cwdi_cdir;
240 VREF(dp);
241 }
242 for (;;) {
243 if (!dp->v_mount)
244 {
245 /* Give up if the directory is no longer mounted */
246 PNBUF_PUT(cnp->cn_pnbuf);
247 return (ENOENT);
248 }
249 cnp->cn_nameptr = cnp->cn_pnbuf;
250 ndp->ni_startdir = dp;
251 if ((error = lookup(ndp)) != 0) {
252 PNBUF_PUT(cnp->cn_pnbuf);
253 return (error);
254 }
255 /*
256 * Check for symbolic link
257 */
258 if ((cnp->cn_flags & ISSYMLINK) == 0) {
259 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
260 PNBUF_PUT(cnp->cn_pnbuf);
261 else
262 cnp->cn_flags |= HASBUF;
263 return (0);
264 }
265 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
266 VOP_UNLOCK(ndp->ni_dvp, 0);
267 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
268 error = ELOOP;
269 break;
270 }
271 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
272 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
273 cnp->cn_proc);
274 if (error != 0)
275 break;
276 }
277 if (ndp->ni_pathlen > 1)
278 cp = PNBUF_GET();
279 else
280 cp = cnp->cn_pnbuf;
281 aiov.iov_base = cp;
282 aiov.iov_len = MAXPATHLEN;
283 auio.uio_iov = &aiov;
284 auio.uio_iovcnt = 1;
285 auio.uio_offset = 0;
286 auio.uio_rw = UIO_READ;
287 auio.uio_segflg = UIO_SYSSPACE;
288 auio.uio_procp = NULL;
289 auio.uio_resid = MAXPATHLEN;
290 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
291 if (error) {
292 badlink:
293 if (ndp->ni_pathlen > 1)
294 PNBUF_PUT(cp);
295 break;
296 }
297 linklen = MAXPATHLEN - auio.uio_resid;
298 if (linklen == 0) {
299 error = ENOENT;
300 goto badlink;
301 }
302 /*
303 * Do symlink substitution, if appropriate, and
304 * check length for potential overflow.
305 */
306 if (((ndp->ni_vp->v_mount->mnt_flag & MNT_MAGICLINKS) &&
307 symlink_magic(cp, &linklen)) ||
308 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
309 error = ENAMETOOLONG;
310 goto badlink;
311 }
312 if (ndp->ni_pathlen > 1) {
313 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
314 PNBUF_PUT(cnp->cn_pnbuf);
315 cnp->cn_pnbuf = cp;
316 } else
317 cnp->cn_pnbuf[linklen] = '\0';
318 ndp->ni_pathlen += linklen;
319 vput(ndp->ni_vp);
320 dp = ndp->ni_dvp;
321 /*
322 * Check if root directory should replace current directory.
323 */
324 if (cnp->cn_pnbuf[0] == '/') {
325 vrele(dp);
326 dp = ndp->ni_rootdir;
327 VREF(dp);
328 }
329 }
330 PNBUF_PUT(cnp->cn_pnbuf);
331 vrele(ndp->ni_dvp);
332 vput(ndp->ni_vp);
333 ndp->ni_vp = NULL;
334 return (error);
335 }
336
337 /*
338 * Determine the namei hash (for cn_hash) for name.
339 * If *ep != NULL, hash from name to ep-1.
340 * If *ep == NULL, hash from name until the first NUL or '/', and
341 * return the location of this termination character in *ep.
342 *
343 * This function returns an equivalent hash to the MI hash32_strn().
344 * The latter isn't used because in the *ep == NULL case, determining
345 * the length of the string to the first NUL or `/' and then calling
346 * hash32_strn() involves unnecessary double-handling of the data.
347 */
348 uint32_t
349 namei_hash(const char *name, const char **ep)
350 {
351 uint32_t hash;
352
353 hash = HASH32_STR_INIT;
354 if (*ep != NULL) {
355 for (; name < *ep; name++)
356 hash = hash * 33 + *(const uint8_t *)name;
357 } else {
358 for (; *name != '\0' && *name != '/'; name++)
359 hash = hash * 33 + *(const uint8_t *)name;
360 *ep = name;
361 }
362 return (hash + (hash >> 5));
363 }
364
365 /*
366 * Search a pathname.
367 * This is a very central and rather complicated routine.
368 *
369 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
370 * The starting directory is taken from ni_startdir. The pathname is
371 * descended until done, or a symbolic link is encountered. The variable
372 * ni_more is clear if the path is completed; it is set to one if a
373 * symbolic link needing interpretation is encountered.
374 *
375 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
376 * whether the name is to be looked up, created, renamed, or deleted.
377 * When CREATE, RENAME, or DELETE is specified, information usable in
378 * creating, renaming, or deleting a directory entry may be calculated.
379 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
380 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
381 * returned unlocked. Otherwise the parent directory is not returned. If
382 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
383 * the target is returned locked, otherwise it is returned unlocked.
384 * When creating or renaming and LOCKPARENT is specified, the target may not
385 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
386 *
387 * Overall outline of lookup:
388 *
389 * dirloop:
390 * identify next component of name at ndp->ni_ptr
391 * handle degenerate case where name is null string
392 * if .. and crossing mount points and on mounted filesys, find parent
393 * call VOP_LOOKUP routine for next component name
394 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
395 * component vnode returned in ni_vp (if it exists), locked.
396 * if result vnode is mounted on and crossing mount points,
397 * find mounted on vnode
398 * if more components of name, do next level at dirloop
399 * return the answer in ni_vp, locked if LOCKLEAF set
400 * if LOCKPARENT set, return locked parent in ni_dvp
401 * if WANTPARENT set, return unlocked parent in ni_dvp
402 */
403 int
404 lookup(struct nameidata *ndp)
405 {
406 const char *cp; /* pointer into pathname argument */
407 struct vnode *dp = 0; /* the directory we are searching */
408 struct vnode *tdp; /* saved dp */
409 struct mount *mp; /* mount table entry */
410 int docache; /* == 0 do not cache last component */
411 int wantparent; /* 1 => wantparent or lockparent flag */
412 int rdonly; /* lookup read-only flag bit */
413 int error = 0;
414 int slashes;
415 int dpunlocked = 0; /* dp has already been unlocked */
416 struct componentname *cnp = &ndp->ni_cnd;
417
418 /*
419 * Setup: break out flag bits into variables.
420 */
421 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
422 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
423 if (cnp->cn_nameiop == DELETE ||
424 (wantparent && cnp->cn_nameiop != CREATE))
425 docache = 0;
426 rdonly = cnp->cn_flags & RDONLY;
427 ndp->ni_dvp = NULL;
428 cnp->cn_flags &= ~ISSYMLINK;
429 dp = ndp->ni_startdir;
430 ndp->ni_startdir = NULLVP;
431 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
432
433 /*
434 * If we have a leading string of slashes, remove them, and just make
435 * sure the current node is a directory.
436 */
437 cp = cnp->cn_nameptr;
438 if (*cp == '/') {
439 do {
440 cp++;
441 } while (*cp == '/');
442 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
443 cnp->cn_nameptr = cp;
444
445 if (dp->v_type != VDIR) {
446 error = ENOTDIR;
447 goto bad;
448 }
449
450 /*
451 * If we've exhausted the path name, then just return the
452 * current node. If the caller requested the parent node (i.e.
453 * it's a CREATE, DELETE, or RENAME), and we don't have one
454 * (because this is the root directory), then we must fail.
455 */
456 if (cnp->cn_nameptr[0] == '\0') {
457 if (ndp->ni_dvp == NULL && wantparent) {
458 error = EISDIR;
459 goto bad;
460 }
461 ndp->ni_vp = dp;
462 cnp->cn_flags |= ISLASTCN;
463 goto terminal;
464 }
465 }
466
467 dirloop:
468 /*
469 * Search a new directory.
470 *
471 * The cn_hash value is for use by vfs_cache.
472 * The last component of the filename is left accessible via
473 * cnp->cn_nameptr for callers that need the name. Callers needing
474 * the name set the SAVENAME flag. When done, they assume
475 * responsibility for freeing the pathname buffer.
476 */
477 cnp->cn_consume = 0;
478 cp = NULL;
479 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
480 cnp->cn_namelen = cp - cnp->cn_nameptr;
481 if (cnp->cn_namelen > NAME_MAX) {
482 error = ENAMETOOLONG;
483 goto bad;
484 }
485 #ifdef NAMEI_DIAGNOSTIC
486 { char c = *cp;
487 *(char *)cp = '\0';
488 printf("{%s}: ", cnp->cn_nameptr);
489 *(char *)cp = c; }
490 #endif /* NAMEI_DIAGNOSTIC */
491 ndp->ni_pathlen -= cnp->cn_namelen;
492 ndp->ni_next = cp;
493 /*
494 * If this component is followed by a slash, then move the pointer to
495 * the next component forward, and remember that this component must be
496 * a directory.
497 */
498 if (*cp == '/') {
499 do {
500 cp++;
501 } while (*cp == '/');
502 slashes = cp - ndp->ni_next;
503 ndp->ni_pathlen -= slashes;
504 ndp->ni_next = cp;
505 cnp->cn_flags |= REQUIREDIR;
506 } else {
507 slashes = 0;
508 cnp->cn_flags &= ~REQUIREDIR;
509 }
510 /*
511 * We do special processing on the last component, whether or not it's
512 * a directory. Cache all intervening lookups, but not the final one.
513 */
514 if (*cp == '\0') {
515 if (docache)
516 cnp->cn_flags |= MAKEENTRY;
517 else
518 cnp->cn_flags &= ~MAKEENTRY;
519 cnp->cn_flags |= ISLASTCN;
520 } else {
521 cnp->cn_flags |= MAKEENTRY;
522 cnp->cn_flags &= ~ISLASTCN;
523 }
524 if (cnp->cn_namelen == 2 &&
525 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
526 cnp->cn_flags |= ISDOTDOT;
527 else
528 cnp->cn_flags &= ~ISDOTDOT;
529
530 /*
531 * Handle "..": two special cases.
532 * 1. If at root directory (e.g. after chroot)
533 * or at absolute root directory
534 * then ignore it so can't get out.
535 * 1a. If we have somehow gotten out of a jail, warn
536 * and also ignore it so we can't get farther out.
537 * 2. If this vnode is the root of a mounted
538 * filesystem, then replace it with the
539 * vnode which was mounted on so we take the
540 * .. in the other file system.
541 */
542 if (cnp->cn_flags & ISDOTDOT) {
543 for (;;) {
544 if (dp == ndp->ni_rootdir || dp == rootvnode) {
545 ndp->ni_dvp = dp;
546 ndp->ni_vp = dp;
547 VREF(dp);
548 goto nextname;
549 }
550 if (ndp->ni_rootdir != rootvnode) {
551 int retval;
552 VOP_UNLOCK(dp, 0);
553 retval = vn_isunder(dp, ndp->ni_rootdir,
554 cnp->cn_proc);
555 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
556 if (!retval) {
557 /* Oops! We got out of jail! */
558 log(LOG_WARNING,
559 "chrooted pid %d uid %d (%s) "
560 "detected outside of its chroot\n",
561 cnp->cn_proc->p_pid,
562 cnp->cn_proc->p_ucred->cr_uid,
563 cnp->cn_proc->p_comm);
564 /* Put us at the jail root. */
565 vput(dp);
566 dp = ndp->ni_rootdir;
567 ndp->ni_dvp = dp;
568 ndp->ni_vp = dp;
569 VREF(dp);
570 VREF(dp);
571 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
572 goto nextname;
573 }
574 }
575 if ((dp->v_flag & VROOT) == 0 ||
576 (cnp->cn_flags & NOCROSSMOUNT))
577 break;
578 tdp = dp;
579 dp = dp->v_mount->mnt_vnodecovered;
580 vput(tdp);
581 VREF(dp);
582 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
583 }
584 }
585
586 /*
587 * We now have a segment name to search for, and a directory to search.
588 */
589 unionlookup:
590 ndp->ni_dvp = dp;
591 ndp->ni_vp = NULL;
592 cnp->cn_flags &= ~PDIRUNLOCK;
593 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
594 #ifdef DIAGNOSTIC
595 if (ndp->ni_vp != NULL)
596 panic("leaf `%s' should be empty", cnp->cn_nameptr);
597 #endif /* DIAGNOSTIC */
598 #ifdef NAMEI_DIAGNOSTIC
599 printf("not found\n");
600 #endif /* NAMEI_DIAGNOSTIC */
601 if ((error == ENOENT) &&
602 (dp->v_flag & VROOT) &&
603 (dp->v_mount->mnt_flag & MNT_UNION)) {
604 tdp = dp;
605 dp = dp->v_mount->mnt_vnodecovered;
606 if (cnp->cn_flags & PDIRUNLOCK)
607 vrele(tdp);
608 else
609 vput(tdp);
610 VREF(dp);
611 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
612 goto unionlookup;
613 }
614
615 if (cnp->cn_flags & PDIRUNLOCK)
616 dpunlocked = 1;
617
618 if (error != EJUSTRETURN)
619 goto bad;
620 /*
621 * If this was not the last component, or there were trailing
622 * slashes, and we are not going to create a directory,
623 * then the name must exist.
624 */
625 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
626 error = ENOENT;
627 goto bad;
628 }
629 /*
630 * If creating and at end of pathname, then can consider
631 * allowing file to be created.
632 */
633 if (rdonly) {
634 error = EROFS;
635 goto bad;
636 }
637 /*
638 * We return with ni_vp NULL to indicate that the entry
639 * doesn't currently exist, leaving a pointer to the
640 * (possibly locked) directory inode in ndp->ni_dvp.
641 */
642 if (cnp->cn_flags & SAVESTART) {
643 ndp->ni_startdir = ndp->ni_dvp;
644 VREF(ndp->ni_startdir);
645 }
646 return (0);
647 }
648 #ifdef NAMEI_DIAGNOSTIC
649 printf("found\n");
650 #endif /* NAMEI_DIAGNOSTIC */
651
652 /*
653 * Take into account any additional components consumed by the
654 * underlying filesystem. This will include any trailing slashes after
655 * the last component consumed.
656 */
657 if (cnp->cn_consume > 0) {
658 ndp->ni_pathlen -= cnp->cn_consume - slashes;
659 ndp->ni_next += cnp->cn_consume - slashes;
660 cnp->cn_consume = 0;
661 if (ndp->ni_next[0] == '\0')
662 cnp->cn_flags |= ISLASTCN;
663 }
664
665 dp = ndp->ni_vp;
666 /*
667 * Check to see if the vnode has been mounted on;
668 * if so find the root of the mounted file system.
669 */
670 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
671 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
672 if (vfs_busy(mp, 0, 0))
673 continue;
674 VOP_UNLOCK(dp, 0);
675 error = VFS_ROOT(mp, &tdp);
676 vfs_unbusy(mp);
677 if (error) {
678 dpunlocked = 1;
679 goto bad2;
680 }
681 vrele(dp);
682 ndp->ni_vp = dp = tdp;
683 }
684
685 /*
686 * Check for symbolic link. Back up over any slashes that we skipped,
687 * as we will need them again.
688 */
689 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
690 ndp->ni_pathlen += slashes;
691 ndp->ni_next -= slashes;
692 cnp->cn_flags |= ISSYMLINK;
693 return (0);
694 }
695
696 /*
697 * Check for directory, if the component was followed by a series of
698 * slashes.
699 */
700 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
701 error = ENOTDIR;
702 goto bad2;
703 }
704
705 nextname:
706 /*
707 * Not a symbolic link. If this was not the last component, then
708 * continue at the next component, else return.
709 */
710 if (!(cnp->cn_flags & ISLASTCN)) {
711 cnp->cn_nameptr = ndp->ni_next;
712 vrele(ndp->ni_dvp);
713 goto dirloop;
714 }
715
716 terminal:
717 /*
718 * Disallow directory write attempts on read-only file systems.
719 */
720 if (rdonly &&
721 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
722 /*
723 * Disallow directory write attempts on read-only
724 * file systems.
725 */
726 error = EROFS;
727 goto bad2;
728 }
729 if (ndp->ni_dvp != NULL) {
730 if (cnp->cn_flags & SAVESTART) {
731 ndp->ni_startdir = ndp->ni_dvp;
732 VREF(ndp->ni_startdir);
733 }
734 if (!wantparent)
735 vrele(ndp->ni_dvp);
736 }
737 if ((cnp->cn_flags & LOCKLEAF) == 0)
738 VOP_UNLOCK(dp, 0);
739 return (0);
740
741 bad2:
742 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
743 ((cnp->cn_flags & PDIRUNLOCK) == 0))
744 VOP_UNLOCK(ndp->ni_dvp, 0);
745 vrele(ndp->ni_dvp);
746 bad:
747 if (dpunlocked)
748 vrele(dp);
749 else
750 vput(dp);
751 ndp->ni_vp = NULL;
752 return (error);
753 }
754
755 /*
756 * Reacquire a path name component.
757 */
758 int
759 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
760 {
761 struct vnode *dp = 0; /* the directory we are searching */
762 int wantparent; /* 1 => wantparent or lockparent flag */
763 int rdonly; /* lookup read-only flag bit */
764 int error = 0;
765 #ifdef DEBUG
766 u_long newhash; /* DEBUG: check name hash */
767 const char *cp; /* DEBUG: check name ptr/len */
768 #endif /* DEBUG */
769
770 /*
771 * Setup: break out flag bits into variables.
772 */
773 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
774 rdonly = cnp->cn_flags & RDONLY;
775 cnp->cn_flags &= ~ISSYMLINK;
776 dp = dvp;
777 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
778
779 /* dirloop: */
780 /*
781 * Search a new directory.
782 *
783 * The cn_hash value is for use by vfs_cache.
784 * The last component of the filename is left accessible via
785 * cnp->cn_nameptr for callers that need the name. Callers needing
786 * the name set the SAVENAME flag. When done, they assume
787 * responsibility for freeing the pathname buffer.
788 */
789 #ifdef DEBUG
790 cp = NULL;
791 newhash = namei_hash(cnp->cn_nameptr, &cp);
792 if (newhash != cnp->cn_hash)
793 panic("relookup: bad hash");
794 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
795 panic("relookup: bad len");
796 while (*cp == '/')
797 cp++;
798 if (*cp != 0)
799 panic("relookup: not last component");
800 #endif /* DEBUG */
801 #ifdef NAMEI_DIAGNOSTIC
802 printf("{%s}: ", cnp->cn_nameptr);
803 #endif /* NAMEI_DIAGNOSTIC */
804
805 /*
806 * Check for degenerate name (e.g. / or "")
807 * which is a way of talking about a directory,
808 * e.g. like "/." or ".".
809 */
810 if (cnp->cn_nameptr[0] == '\0')
811 panic("relookup: null name");
812
813 if (cnp->cn_flags & ISDOTDOT)
814 panic("relookup: lookup on dot-dot");
815
816 /*
817 * We now have a segment name to search for, and a directory to search.
818 */
819 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
820 #ifdef DIAGNOSTIC
821 if (*vpp != NULL)
822 panic("leaf `%s' should be empty", cnp->cn_nameptr);
823 #endif
824 if (error != EJUSTRETURN)
825 goto bad;
826 /*
827 * If creating and at end of pathname, then can consider
828 * allowing file to be created.
829 */
830 if (rdonly) {
831 error = EROFS;
832 goto bad;
833 }
834 /* ASSERT(dvp == ndp->ni_startdir) */
835 if (cnp->cn_flags & SAVESTART)
836 VREF(dvp);
837 /*
838 * We return with ni_vp NULL to indicate that the entry
839 * doesn't currently exist, leaving a pointer to the
840 * (possibly locked) directory inode in ndp->ni_dvp.
841 */
842 return (0);
843 }
844 dp = *vpp;
845
846 #ifdef DIAGNOSTIC
847 /*
848 * Check for symbolic link
849 */
850 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
851 panic("relookup: symlink found");
852 #endif
853
854 /*
855 * Check for read-only file systems.
856 */
857 if (rdonly &&
858 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
859 error = EROFS;
860 goto bad2;
861 }
862 /* ASSERT(dvp == ndp->ni_startdir) */
863 if (cnp->cn_flags & SAVESTART)
864 VREF(dvp);
865 if (!wantparent)
866 vrele(dvp);
867 if ((cnp->cn_flags & LOCKLEAF) == 0)
868 VOP_UNLOCK(dp, 0);
869 return (0);
870
871 bad2:
872 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
873 VOP_UNLOCK(dvp, 0);
874 vrele(dvp);
875 bad:
876 vput(dp);
877 *vpp = NULL;
878 return (error);
879 }
880