proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / vfs_lookup.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
39 * $FreeBSD: src/sys/kern/vfs_lookup.c,v 1.38.2.3 2001/08/31 19:36:49 dillon Exp $
dadab5e9 40 * $DragonFly: src/sys/kern/vfs_lookup.c,v 1.3 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
984263bc
MD
47#include <sys/vnode.h>
48#include <sys/mount.h>
49#include <sys/filedesc.h>
50#include <sys/proc.h>
dadab5e9 51#include <sys/namei.h>
984263bc
MD
52
53#ifdef KTRACE
54#include <sys/ktrace.h>
55#endif
56
57#include <vm/vm_zone.h>
58
59/*
60 * Convert a pathname into a pointer to a locked inode.
61 *
62 * The FOLLOW flag is set when symbolic links are to be followed
63 * when they occur at the end of the name translation process.
64 * Symbolic links are always followed for all other pathname
65 * components other than the last.
66 *
67 * The segflg defines whether the name is to be copied from user
68 * space or kernel space.
69 *
70 * Overall outline of namei:
71 *
72 * copy in name
73 * get starting directory
74 * while (!done && !error) {
75 * call lookup to search path.
76 * if symbolic link, massage name in buffer and continue
77 * }
78 */
79int
dadab5e9 80namei(struct nameidata *ndp)
984263bc 81{
dadab5e9
MD
82 struct filedesc *fdp; /* pointer to file descriptor state */
83 char *cp; /* pointer into pathname argument */
84 struct vnode *dp; /* the directory we are searching */
984263bc
MD
85 struct iovec aiov; /* uio for reading symbolic links */
86 struct uio auio;
87 int error, linklen;
88 struct componentname *cnp = &ndp->ni_cnd;
dadab5e9 89 struct proc *p;
984263bc 90
dadab5e9
MD
91 KKASSERT(ndp->ni_cnd.cn_td != NULL);
92 p = cnp->cn_td->td_proc;
93 KKASSERT(p != NULL);
94 KASSERT(cnp->cn_cred, ("namei: bad cred/proc"));
95 KKASSERT(cnp->cn_cred == p->p_ucred); /* YYY */
984263bc
MD
96 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
97 ("namei: nameiop contaminated with flags"));
98 KASSERT((cnp->cn_flags & OPMASK) == 0,
99 ("namei: flags contaminated with nameiops"));
dadab5e9 100 fdp = p->p_fd;
984263bc
MD
101
102 /*
103 * Get a buffer for the name to be translated, and copy the
104 * name into the buffer.
105 */
106 if ((cnp->cn_flags & HASBUF) == 0)
107 cnp->cn_pnbuf = zalloc(namei_zone);
108 if (ndp->ni_segflg == UIO_SYSSPACE)
109 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
110 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
111 else
112 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
113 MAXPATHLEN, (size_t *)&ndp->ni_pathlen);
114
115 /*
116 * Don't allow empty pathnames.
117 */
118 if (!error && *cnp->cn_pnbuf == '\0')
119 error = ENOENT;
120
121 if (error) {
122 zfree(namei_zone, cnp->cn_pnbuf);
123 ndp->ni_vp = NULL;
124 return (error);
125 }
126 ndp->ni_loopcnt = 0;
127#ifdef KTRACE
dadab5e9
MD
128 if (KTRPOINT(cnp->cn_td, KTR_NAMEI))
129 ktrnamei(cnp->cn_td->td_proc->p_tracep, cnp->cn_pnbuf);
984263bc
MD
130#endif
131
132 /*
133 * Get starting point for the translation.
134 */
135 ndp->ni_rootdir = fdp->fd_rdir;
136 ndp->ni_topdir = fdp->fd_jdir;
137
138 dp = fdp->fd_cdir;
139 VREF(dp);
140 for (;;) {
141 /*
142 * Check if root directory should replace current directory.
143 * Done at start of translation and after symbolic link.
144 */
145 cnp->cn_nameptr = cnp->cn_pnbuf;
146 if (*(cnp->cn_nameptr) == '/') {
147 vrele(dp);
148 while (*(cnp->cn_nameptr) == '/') {
149 cnp->cn_nameptr++;
150 ndp->ni_pathlen--;
151 }
152 dp = ndp->ni_rootdir;
153 VREF(dp);
154 }
155 ndp->ni_startdir = dp;
156 error = lookup(ndp);
157 if (error) {
158 zfree(namei_zone, cnp->cn_pnbuf);
159 return (error);
160 }
161 /*
162 * Check for symbolic link
163 */
164 if ((cnp->cn_flags & ISSYMLINK) == 0) {
165 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
166 zfree(namei_zone, cnp->cn_pnbuf);
167 else
168 cnp->cn_flags |= HASBUF;
169
170 if (vn_canvmio(ndp->ni_vp) == TRUE &&
171 (cnp->cn_nameiop != DELETE) &&
172 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) ==
173 LOCKLEAF))
174 vfs_object_create(ndp->ni_vp,
dadab5e9 175 ndp->ni_cnd.cn_td,
984263bc
MD
176 ndp->ni_cnd.cn_cred);
177
178 return (0);
179 }
180 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
dadab5e9 181 VOP_UNLOCK(ndp->ni_dvp, 0, cnp->cn_td);
984263bc
MD
182 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
183 error = ELOOP;
184 break;
185 }
186 if (ndp->ni_pathlen > 1)
187 cp = zalloc(namei_zone);
188 else
189 cp = cnp->cn_pnbuf;
190 aiov.iov_base = cp;
191 aiov.iov_len = MAXPATHLEN;
192 auio.uio_iov = &aiov;
193 auio.uio_iovcnt = 1;
194 auio.uio_offset = 0;
195 auio.uio_rw = UIO_READ;
196 auio.uio_segflg = UIO_SYSSPACE;
dadab5e9 197 auio.uio_td = NULL;
984263bc
MD
198 auio.uio_resid = MAXPATHLEN;
199 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
200 if (error) {
201 if (ndp->ni_pathlen > 1)
202 zfree(namei_zone, cp);
203 break;
204 }
205 linklen = MAXPATHLEN - auio.uio_resid;
206 if (linklen == 0) {
207 if (ndp->ni_pathlen > 1)
208 zfree(namei_zone, cp);
209 error = ENOENT;
210 break;
211 }
212 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
213 if (ndp->ni_pathlen > 1)
214 zfree(namei_zone, cp);
215 error = ENAMETOOLONG;
216 break;
217 }
218 if (ndp->ni_pathlen > 1) {
219 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
220 zfree(namei_zone, cnp->cn_pnbuf);
221 cnp->cn_pnbuf = cp;
222 } else
223 cnp->cn_pnbuf[linklen] = '\0';
224 ndp->ni_pathlen += linklen;
225 vput(ndp->ni_vp);
226 dp = ndp->ni_dvp;
227 }
228 zfree(namei_zone, cnp->cn_pnbuf);
229 vrele(ndp->ni_dvp);
230 vput(ndp->ni_vp);
231 ndp->ni_vp = NULL;
232 return (error);
233}
234
235/*
236 * Search a pathname.
237 * This is a very central and rather complicated routine.
238 *
239 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
240 * The starting directory is taken from ni_startdir. The pathname is
241 * descended until done, or a symbolic link is encountered. The variable
242 * ni_more is clear if the path is completed; it is set to one if a
243 * symbolic link needing interpretation is encountered.
244 *
245 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
246 * whether the name is to be looked up, created, renamed, or deleted.
247 * When CREATE, RENAME, or DELETE is specified, information usable in
248 * creating, renaming, or deleting a directory entry may be calculated.
249 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
250 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
251 * returned unlocked. Otherwise the parent directory is not returned. If
252 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
253 * the target is returned locked, otherwise it is returned unlocked.
254 * When creating or renaming and LOCKPARENT is specified, the target may not
255 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
256 *
257 * Overall outline of lookup:
258 *
259 * dirloop:
260 * identify next component of name at ndp->ni_ptr
261 * handle degenerate case where name is null string
262 * if .. and crossing mount points and on mounted filesys, find parent
263 * call VOP_LOOKUP routine for next component name
264 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
265 * component vnode returned in ni_vp (if it exists), locked.
266 * if result vnode is mounted on and crossing mount points,
267 * find mounted on vnode
268 * if more components of name, do next level at dirloop
269 * return the answer in ni_vp, locked if LOCKLEAF set
270 * if LOCKPARENT set, return locked parent in ni_dvp
271 * if WANTPARENT set, return unlocked parent in ni_dvp
272 */
273int
274lookup(ndp)
275 register struct nameidata *ndp;
276{
277 register char *cp; /* pointer into pathname argument */
278 register struct vnode *dp = 0; /* the directory we are searching */
279 struct vnode *tdp; /* saved dp */
280 struct mount *mp; /* mount table entry */
281 int docache; /* == 0 do not cache last component */
282 int wantparent; /* 1 => wantparent or lockparent flag */
283 int rdonly; /* lookup read-only flag bit */
284 int trailing_slash;
285 int error = 0;
286 int dpunlocked = 0; /* dp has already been unlocked */
287 struct componentname *cnp = &ndp->ni_cnd;
dadab5e9 288 struct thread *td = cnp->cn_td;
984263bc
MD
289
290 /*
291 * Setup: break out flag bits into variables.
292 */
293 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
294 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
295 if (cnp->cn_nameiop == DELETE ||
296 (wantparent && cnp->cn_nameiop != CREATE &&
297 cnp->cn_nameiop != LOOKUP))
298 docache = 0;
299 rdonly = cnp->cn_flags & RDONLY;
300 ndp->ni_dvp = NULL;
301 cnp->cn_flags &= ~ISSYMLINK;
302 dp = ndp->ni_startdir;
303 ndp->ni_startdir = NULLVP;
dadab5e9 304 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
305
306dirloop:
307 /*
308 * Search a new directory.
309 *
310 * The last component of the filename is left accessible via
311 * cnp->cn_nameptr for callers that need the name. Callers needing
312 * the name set the SAVENAME flag. When done, they assume
313 * responsibility for freeing the pathname buffer.
314 */
315 cnp->cn_consume = 0;
316 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
317 continue;
318 cnp->cn_namelen = cp - cnp->cn_nameptr;
319 if (cnp->cn_namelen > NAME_MAX) {
320 error = ENAMETOOLONG;
321 goto bad;
322 }
323#ifdef NAMEI_DIAGNOSTIC
324 { char c = *cp;
325 *cp = '\0';
326 printf("{%s}: ", cnp->cn_nameptr);
327 *cp = c; }
328#endif
329 ndp->ni_pathlen -= cnp->cn_namelen;
330 ndp->ni_next = cp;
331
332 /*
333 * Replace multiple slashes by a single slash and trailing slashes
334 * by a null. This must be done before VOP_LOOKUP() because some
335 * fs's don't know about trailing slashes. Remember if there were
336 * trailing slashes to handle symlinks, existing non-directories
337 * and non-existing files that won't be directories specially later.
338 */
339 trailing_slash = 0;
340 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
341 cp++;
342 ndp->ni_pathlen--;
343 if (*cp == '\0') {
344 trailing_slash = 1;
345 *ndp->ni_next = '\0'; /* XXX for direnter() ... */
346 }
347 }
348 ndp->ni_next = cp;
349
350 cnp->cn_flags |= MAKEENTRY;
351 if (*cp == '\0' && docache == 0)
352 cnp->cn_flags &= ~MAKEENTRY;
353 if (cnp->cn_namelen == 2 &&
354 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
355 cnp->cn_flags |= ISDOTDOT;
356 else
357 cnp->cn_flags &= ~ISDOTDOT;
358 if (*ndp->ni_next == 0)
359 cnp->cn_flags |= ISLASTCN;
360 else
361 cnp->cn_flags &= ~ISLASTCN;
362
363
364 /*
365 * Check for degenerate name (e.g. / or "")
366 * which is a way of talking about a directory,
367 * e.g. like "/." or ".".
368 */
369 if (cnp->cn_nameptr[0] == '\0') {
370 if (dp->v_type != VDIR) {
371 error = ENOTDIR;
372 goto bad;
373 }
374 if (cnp->cn_nameiop != LOOKUP) {
375 error = EISDIR;
376 goto bad;
377 }
378 if (wantparent) {
379 ndp->ni_dvp = dp;
380 VREF(dp);
381 }
382 ndp->ni_vp = dp;
383 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
dadab5e9 384 VOP_UNLOCK(dp, 0, cnp->cn_td);
984263bc
MD
385 /* XXX This should probably move to the top of function. */
386 if (cnp->cn_flags & SAVESTART)
387 panic("lookup: SAVESTART");
388 return (0);
389 }
390
391 /*
392 * Handle "..": two special cases.
393 * 1. If at root directory (e.g. after chroot)
394 * or at absolute root directory
395 * then ignore it so can't get out.
396 * 2. If this vnode is the root of a mounted
397 * filesystem, then replace it with the
398 * vnode which was mounted on so we take the
399 * .. in the other file system.
400 * 3. If the vnode is the top directory of
401 * the jail or chroot, don't let them out.
402 */
403 if (cnp->cn_flags & ISDOTDOT) {
404 for (;;) {
405 if (dp == ndp->ni_rootdir ||
406 dp == ndp->ni_topdir ||
407 dp == rootvnode) {
408 ndp->ni_dvp = dp;
409 ndp->ni_vp = dp;
410 VREF(dp);
411 goto nextname;
412 }
413 if ((dp->v_flag & VROOT) == 0 ||
414 (cnp->cn_flags & NOCROSSMOUNT))
415 break;
416 if (dp->v_mount == NULL) { /* forced unmount */
417 error = EBADF;
418 goto bad;
419 }
420 tdp = dp;
421 dp = dp->v_mount->mnt_vnodecovered;
422 vput(tdp);
423 VREF(dp);
dadab5e9 424 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
425 }
426 }
427
428 /*
429 * We now have a segment name to search for, and a directory to search.
430 */
431unionlookup:
432 ndp->ni_dvp = dp;
433 ndp->ni_vp = NULL;
434 cnp->cn_flags &= ~PDIRUNLOCK;
435 ASSERT_VOP_LOCKED(dp, "lookup");
436 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
437 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
438#ifdef NAMEI_DIAGNOSTIC
439 printf("not found\n");
440#endif
441 if ((error == ENOENT) &&
442 (dp->v_flag & VROOT) && (dp->v_mount != NULL) &&
443 (dp->v_mount->mnt_flag & MNT_UNION)) {
444 tdp = dp;
445 dp = dp->v_mount->mnt_vnodecovered;
446 if (cnp->cn_flags & PDIRUNLOCK)
447 vrele(tdp);
448 else
449 vput(tdp);
450 VREF(dp);
dadab5e9 451 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
452 goto unionlookup;
453 }
454
455 if (error != EJUSTRETURN)
456 goto bad;
457 /*
458 * If creating and at end of pathname, then can consider
459 * allowing file to be created.
460 */
461 if (rdonly) {
462 error = EROFS;
463 goto bad;
464 }
465 if (*cp == '\0' && trailing_slash &&
466 !(cnp->cn_flags & WILLBEDIR)) {
467 error = ENOENT;
468 goto bad;
469 }
470 /*
471 * We return with ni_vp NULL to indicate that the entry
472 * doesn't currently exist, leaving a pointer to the
473 * (possibly locked) directory inode in ndp->ni_dvp.
474 */
475 if (cnp->cn_flags & SAVESTART) {
476 ndp->ni_startdir = ndp->ni_dvp;
477 VREF(ndp->ni_startdir);
478 }
479 return (0);
480 }
481#ifdef NAMEI_DIAGNOSTIC
482 printf("found\n");
483#endif
484
485 ASSERT_VOP_LOCKED(ndp->ni_vp, "lookup");
486
487 /*
488 * Take into account any additional components consumed by
489 * the underlying filesystem.
490 */
491 if (cnp->cn_consume > 0) {
492 cnp->cn_nameptr += cnp->cn_consume;
493 ndp->ni_next += cnp->cn_consume;
494 ndp->ni_pathlen -= cnp->cn_consume;
495 cnp->cn_consume = 0;
496 }
497
498 dp = ndp->ni_vp;
499
500 /*
501 * Check to see if the vnode has been mounted on;
502 * if so find the root of the mounted file system.
503 */
504 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
505 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
dadab5e9 506 if (vfs_busy(mp, 0, 0, td))
984263bc 507 continue;
dadab5e9 508 VOP_UNLOCK(dp, 0, td);
984263bc 509 error = VFS_ROOT(mp, &tdp);
dadab5e9 510 vfs_unbusy(mp, td);
984263bc
MD
511 if (error) {
512 dpunlocked = 1;
513 goto bad2;
514 }
515 vrele(dp);
516 ndp->ni_vp = dp = tdp;
517 }
518
519 /*
520 * Check for symbolic link
521 */
522 if ((dp->v_type == VLNK) &&
523 ((cnp->cn_flags & FOLLOW) || trailing_slash ||
524 *ndp->ni_next == '/')) {
525 cnp->cn_flags |= ISSYMLINK;
526 if (dp->v_mount == NULL) {
527 /* We can't know whether the directory was mounted with
528 * NOSYMFOLLOW, so we can't follow safely. */
529 error = EBADF;
530 goto bad2;
531 }
532 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
533 error = EACCES;
534 goto bad2;
535 }
536 return (0);
537 }
538
539 /*
540 * Check for bogus trailing slashes.
541 */
542 if (trailing_slash && dp->v_type != VDIR) {
543 error = ENOTDIR;
544 goto bad2;
545 }
546
547nextname:
548 /*
549 * Not a symbolic link. If more pathname,
550 * continue at next component, else return.
551 */
552 if (*ndp->ni_next == '/') {
553 cnp->cn_nameptr = ndp->ni_next;
554 while (*cnp->cn_nameptr == '/') {
555 cnp->cn_nameptr++;
556 ndp->ni_pathlen--;
557 }
558 if (ndp->ni_dvp != ndp->ni_vp)
559 ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "lookup");
560 vrele(ndp->ni_dvp);
561 goto dirloop;
562 }
563 /*
564 * Disallow directory write attempts on read-only file systems.
565 */
566 if (rdonly &&
567 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
568 error = EROFS;
569 goto bad2;
570 }
571 if (cnp->cn_flags & SAVESTART) {
572 ndp->ni_startdir = ndp->ni_dvp;
573 VREF(ndp->ni_startdir);
574 }
575 if (!wantparent)
576 vrele(ndp->ni_dvp);
577
578 if ((cnp->cn_flags & LOCKLEAF) == 0)
dadab5e9 579 VOP_UNLOCK(dp, 0, td);
984263bc
MD
580 return (0);
581
582bad2:
583 if ((cnp->cn_flags & (LOCKPARENT | PDIRUNLOCK)) == LOCKPARENT &&
584 *ndp->ni_next == '\0')
dadab5e9 585 VOP_UNLOCK(ndp->ni_dvp, 0, td);
984263bc
MD
586 vrele(ndp->ni_dvp);
587bad:
588 if (dpunlocked)
589 vrele(dp);
590 else
591 vput(dp);
592 ndp->ni_vp = NULL;
593 return (error);
594}
595
596/*
597 * relookup - lookup a path name component
598 * Used by lookup to re-aquire things.
599 */
600int
601relookup(dvp, vpp, cnp)
602 struct vnode *dvp, **vpp;
603 struct componentname *cnp;
604{
dadab5e9 605 struct thread *td = cnp->cn_td;
984263bc
MD
606 struct vnode *dp = 0; /* the directory we are searching */
607 int docache; /* == 0 do not cache last component */
608 int wantparent; /* 1 => wantparent or lockparent flag */
609 int rdonly; /* lookup read-only flag bit */
610 int error = 0;
611#ifdef NAMEI_DIAGNOSTIC
612 int newhash; /* DEBUG: check name hash */
613 char *cp; /* DEBUG: check name ptr/len */
614#endif
615
616 /*
617 * Setup: break out flag bits into variables.
618 */
619 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
620 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
621 if (cnp->cn_nameiop == DELETE ||
622 (wantparent && cnp->cn_nameiop != CREATE))
623 docache = 0;
624 rdonly = cnp->cn_flags & RDONLY;
625 cnp->cn_flags &= ~ISSYMLINK;
626 dp = dvp;
dadab5e9 627 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
628
629/* dirloop: */
630 /*
631 * Search a new directory.
632 *
633 * The last component of the filename is left accessible via
634 * cnp->cn_nameptr for callers that need the name. Callers needing
635 * the name set the SAVENAME flag. When done, they assume
636 * responsibility for freeing the pathname buffer.
637 */
638#ifdef NAMEI_DIAGNOSTIC
639 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
640 panic ("relookup: bad len");
641 if (*cp != 0)
642 panic("relookup: not last component");
643 printf("{%s}: ", cnp->cn_nameptr);
644#endif
645
646 /*
647 * Check for degenerate name (e.g. / or "")
648 * which is a way of talking about a directory,
649 * e.g. like "/." or ".".
650 */
651 if (cnp->cn_nameptr[0] == '\0') {
652 if (cnp->cn_nameiop != LOOKUP || wantparent) {
653 error = EISDIR;
654 goto bad;
655 }
656 if (dp->v_type != VDIR) {
657 error = ENOTDIR;
658 goto bad;
659 }
660 if (!(cnp->cn_flags & LOCKLEAF))
dadab5e9 661 VOP_UNLOCK(dp, 0, td);
984263bc
MD
662 *vpp = dp;
663 /* XXX This should probably move to the top of function. */
664 if (cnp->cn_flags & SAVESTART)
665 panic("lookup: SAVESTART");
666 return (0);
667 }
668
669 if (cnp->cn_flags & ISDOTDOT)
670 panic ("relookup: lookup on dot-dot");
671
672 /*
673 * We now have a segment name to search for, and a directory to search.
674 */
675 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
676 KASSERT(*vpp == NULL, ("leaf should be empty"));
677 if (error != EJUSTRETURN)
678 goto bad;
679 /*
680 * If creating and at end of pathname, then can consider
681 * allowing file to be created.
682 */
683 if (rdonly) {
684 error = EROFS;
685 goto bad;
686 }
687 /* ASSERT(dvp == ndp->ni_startdir) */
688 if (cnp->cn_flags & SAVESTART)
689 VREF(dvp);
690 /*
691 * We return with ni_vp NULL to indicate that the entry
692 * doesn't currently exist, leaving a pointer to the
693 * (possibly locked) directory inode in ndp->ni_dvp.
694 */
695 return (0);
696 }
697 dp = *vpp;
698
699 /*
700 * Check for symbolic link
701 */
702 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
703 ("relookup: symlink found.\n"));
704
705 /*
706 * Disallow directory write attempts on read-only file systems.
707 */
708 if (rdonly &&
709 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
710 error = EROFS;
711 goto bad2;
712 }
713 /* ASSERT(dvp == ndp->ni_startdir) */
714 if (cnp->cn_flags & SAVESTART)
715 VREF(dvp);
716
717 if (!wantparent)
718 vrele(dvp);
719
720 if (vn_canvmio(dp) == TRUE &&
721 ((cnp->cn_flags & (NOOBJ|LOCKLEAF)) == LOCKLEAF))
dadab5e9 722 vfs_object_create(dp, cnp->cn_td, cnp->cn_cred);
984263bc
MD
723
724 if ((cnp->cn_flags & LOCKLEAF) == 0)
dadab5e9 725 VOP_UNLOCK(dp, 0, td);
984263bc
MD
726 return (0);
727
728bad2:
729 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
dadab5e9 730 VOP_UNLOCK(dvp, 0, td);
984263bc
MD
731 vrele(dvp);
732bad:
733 vput(dp);
734 *vpp = NULL;
735 return (error);
736}