[BACK]Return to vfs_lookup.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/vfs_lookup.c, Revision 1.29

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

CVSweb <webmaster@jp.NetBSD.org>