123401Smckusick /* 237715Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337715Smckusick * All rights reserved. 423401Smckusick * 537715Smckusick * Redistribution and use in source and binary forms are permitted 637715Smckusick * provided that the above copyright notice and this paragraph are 737715Smckusick * duplicated in all such forms and that any documentation, 837715Smckusick * advertising materials, and other materials related to such 937715Smckusick * distribution and use acknowledge that the software was developed 1037715Smckusick * by the University of California, Berkeley. The name of the 1137715Smckusick * University may not be used to endorse or promote products derived 1237715Smckusick * from this software without specific prior written permission. 1337715Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437715Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537715Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637715Smckusick * 17*37742Smckusick * @(#)vfs_lookup.c 7.8 (Berkeley) 05/09/89 1823401Smckusick */ 1930Sbill 2017100Sbloom #include "param.h" 2137715Smckusick #include "time.h" 2237715Smckusick #include "namei.h" 2337715Smckusick #include "vnode.h" 2417100Sbloom #include "mount.h" 2537715Smckusick #include "errno.h" 2631650Smckusick #include "malloc.h" 2737715Smckusick 2837582Smarc #ifdef KTRACE 2937715Smckusick #include "user.h" 3037582Smarc #include "proc.h" 3137582Smarc #include "ktrace.h" 3237582Smarc #endif 3330Sbill 3430Sbill /* 3527268Smckusick * Convert a pathname into a pointer to a locked inode. 367534Sroot * This is a very central and rather complicated routine. 3730Sbill * 3837715Smckusick * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 3937715Smckusick * whether the name is to be looked up, created, renamed, or deleted. 4037715Smckusick * When CREATE, RENAME, or DELETE is specified, information usable in 4137715Smckusick * creating, renaming, or deleting a directory entry may be calculated. 4237715Smckusick * If flag has LOCKPARENT or'ed into it and the target of the pathname 4337715Smckusick * exists, namei returns both the target and its parent directory locked. 4437715Smckusick * When creating or renaming and LOCKPARENT is specified, the target may not 4537715Smckusick * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 469166Ssam * 4716688Smckusick * The FOLLOW flag is set when symbolic links are to be followed 489166Ssam * when they occur at the end of the name translation process. 4927268Smckusick * Symbolic links are always followed for all other pathname 5027268Smckusick * components other than the last. 519166Ssam * 5227268Smckusick * The segflg defines whether the name is to be copied from user 5327268Smckusick * space or kernel space. 5427268Smckusick * 5515798Smckusick * Overall outline of namei: 5615798Smckusick * 577534Sroot * copy in name 587534Sroot * get starting directory 597534Sroot * dirloop: 6016688Smckusick * copy next component of name to ndp->ni_dent 617534Sroot * handle degenerate case where name is null string 6237715Smckusick * if .. and on mounted filesys, find parent 6337715Smckusick * call lookup routine for next component name 6437715Smckusick * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 6537715Smckusick * component vnode returned in ni_vp (if it exists), locked. 667534Sroot * if symbolic link, massage name in buffer and continue at dirloop 6737715Smckusick * if result inode is mounted on, find mounted on vnode 687534Sroot * if more components of name, do next level at dirloop 6937715Smckusick * return the answer in ni_vp as locked vnode; 7037715Smckusick * if LOCKPARENT set, return locked parent in ni_dvp 719166Ssam * 7237715Smckusick * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked. 7330Sbill */ 7416688Smckusick namei(ndp) 7516688Smckusick register struct nameidata *ndp; 7630Sbill { 777534Sroot register char *cp; /* pointer into pathname argument */ 7837715Smckusick register struct vnode *dp = 0; /* the directory we are searching */ 7937715Smckusick register int i; /* Temp counter */ 8037715Smckusick struct vnode *tdp; /* saved dp */ 8137715Smckusick struct mount *mp; /* mount table entry */ 8218109Smckusick int docache; /* == 0 do not cache last component */ 8337715Smckusick int flag; /* LOOKUP, CREATE, RENAME or DELETE */ 8437715Smckusick int wantparent; /* 1 => wantparent or lockparent flag */ 8537715Smckusick int error = 0; 8630Sbill 8737715Smckusick flag = ndp->ni_nameiop & OPFLAG; 8837715Smckusick wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); 8916688Smckusick docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 9037715Smckusick if (flag == DELETE || wantparent) 9115798Smckusick docache = 0; 9230Sbill /* 937534Sroot * Get a buffer for the name to be translated, and copy the 947534Sroot * name into the buffer. 955972Swnj */ 9637715Smckusick MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 9716688Smckusick if (ndp->ni_segflg == UIO_SYSSPACE) 9837715Smckusick error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN, 9937715Smckusick &ndp->ni_pathlen); 10016688Smckusick else 10137715Smckusick error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN, 10237715Smckusick &ndp->ni_pathlen); 10316688Smckusick if (error) { 10437715Smckusick free(ndp->ni_pnbuf, M_NAMEI); 10537715Smckusick return (error); 1065972Swnj } 10737715Smckusick ndp->ni_ptr = ndp->ni_pnbuf; 10837715Smckusick ndp->ni_loopcnt = 0; 10937715Smckusick dp = ndp->ni_cdir; 11037715Smckusick dp->v_count++; 11137582Smarc #ifdef KTRACE 11237582Smarc if (KTRPOINT(u.u_procp, KTR_NAMEI)) 11337715Smckusick ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf); 11437582Smarc #endif 1157534Sroot 11637715Smckusick start: 1175972Swnj /* 1187534Sroot * Get starting directory. 11937715Smckusick * Done at start of translation and after symbolic link. 12030Sbill */ 12137715Smckusick if (*ndp->ni_ptr == '/') { 12237715Smckusick vrele(dp); 12337715Smckusick while (*ndp->ni_ptr == '/') { 12437715Smckusick ndp->ni_ptr++; 12537715Smckusick ndp->ni_pathlen--; 12637715Smckusick } 12737715Smckusick if ((dp = ndp->ni_rdir) == NULL) 12830Sbill dp = rootdir; 12937715Smckusick dp->v_count++; 13037715Smckusick } 13137715Smckusick VOP_LOCK(dp); 13218027Smckusick ndp->ni_endoff = 0; 1337534Sroot 1347534Sroot /* 1357534Sroot * We come to dirloop to search a new directory. 1367534Sroot */ 1376571Smckusic dirloop: 13830Sbill /* 13916688Smckusick * Copy next component of name to ndp->ni_dent. 14037715Smckusick * XXX kern_exec looks at d_name 14137715Smckusick * ??? The ni_hash value may be useful for vfs_cache 14237715Smckusick * XXX There must be the last component of the filename left 14337715Smckusick * somewhere accessible via. ndp for NFS (and any other stateless file 14437715Smckusick * systems) in case they are doing a CREATE. The "Towards a..." noted 14537715Smckusick * that ni_ptr would be left pointing to the last component, but since 14637715Smckusick * the ni_pnbuf gets free'd, that is not a good idea. 1477534Sroot */ 14837715Smckusick ndp->ni_hash = 0; 14937715Smckusick for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) { 1506571Smckusic if (i >= MAXNAMLEN) { 15137715Smckusick error = ENAMETOOLONG; 1527534Sroot goto bad; 1535972Swnj } 15421014Smckusick if (*cp & 0200) 15521014Smckusick if ((*cp&0377) == ('/'|0200) || flag != DELETE) { 15637715Smckusick error = EINVAL; 15721014Smckusick goto bad; 15821014Smckusick } 15916688Smckusick ndp->ni_dent.d_name[i++] = *cp; 16037715Smckusick ndp->ni_hash += (unsigned char)*cp * i; 1615972Swnj } 16237715Smckusick ndp->ni_namelen = i; 16316688Smckusick ndp->ni_dent.d_namlen = i; 16416688Smckusick ndp->ni_dent.d_name[i] = '\0'; 16537715Smckusick ndp->ni_pathlen -= i; 16637715Smckusick #ifdef NAMEI_DIAGNOSTIC 16737715Smckusick printf("{%s}: ", ndp->ni_dent.d_name); 16837715Smckusick #endif 16937715Smckusick ndp->ni_next = cp; 17037715Smckusick ndp->ni_makeentry = 1; 17118109Smckusick if (*cp == '\0' && docache == 0) 17237715Smckusick ndp->ni_makeentry = 0; 17337715Smckusick ndp->ni_isdotdot = (ndp->ni_namelen == 2 && 17437715Smckusick ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.'); 1757534Sroot 1767534Sroot /* 1777534Sroot * Check for degenerate name (e.g. / or "") 1787534Sroot * which is a way of talking about a directory, 1797534Sroot * e.g. like "/." or ".". 1807534Sroot */ 18137715Smckusick if (ndp->ni_ptr[0] == '\0') { 18237715Smckusick if (flag != LOOKUP || wantparent) { 18337715Smckusick error = EISDIR; 1847534Sroot goto bad; 1855972Swnj } 18637715Smckusick free(ndp->ni_pnbuf, M_NAMEI); 18737715Smckusick if (!(ndp->ni_nameiop & LOCKLEAF)) 18837715Smckusick VOP_UNLOCK(dp); 18937715Smckusick ndp->ni_vp = dp; 19037715Smckusick return (0); 1915972Swnj } 1927534Sroot 1936571Smckusic /* 19437715Smckusick * Handle "..": two special cases. 19537715Smckusick * 1. If at root directory (e.g. after chroot) 19637715Smckusick * then ignore it so can't get out. 19737715Smckusick * 2. If this vnode is the root of a mounted 19837715Smckusick * file system, then replace it with the 19937715Smckusick * vnode which was mounted on so we take the 20037715Smckusick * .. in the other file system. 20136547Smckusick */ 20237715Smckusick if (ndp->ni_isdotdot) { 20336547Smckusick for (;;) { 20437715Smckusick if (dp == ndp->ni_rdir || dp == rootdir) { 20537715Smckusick ndp->ni_dvp = dp; 20637715Smckusick dp->v_count++; 20737715Smckusick goto nextname; 20836547Smckusick } 20937715Smckusick if ((dp->v_flag & VROOT) == 0) 21036547Smckusick break; 21137715Smckusick tdp = dp; 21237715Smckusick dp = dp->v_mount->m_vnodecovered; 21337715Smckusick vput(tdp); 21437715Smckusick VOP_LOCK(dp); 21537715Smckusick dp->v_count++; 21636547Smckusick } 21736547Smckusick } 21836547Smckusick 21936547Smckusick /* 22015798Smckusick * We now have a segment name to search for, and a directory to search. 22115798Smckusick */ 22237715Smckusick if (error = VOP_LOOKUP(dp, ndp)) { 22337715Smckusick if (ndp->ni_vp != NULL) 22437715Smckusick panic("leaf should be empty"); 22537582Smarc #ifdef NAMEI_DIAGNOSTIC 22637715Smckusick printf("not found\n"); 22737582Smarc #endif 2285972Swnj /* 22937715Smckusick * If creating and at end of pathname, then can consider 23037715Smckusick * allowing file to be created. 2315972Swnj */ 23237715Smckusick if (ndp->ni_dvp->v_mount->m_flag & M_RDONLY) 23337715Smckusick error = EROFS; 234*37742Smckusick if (flag == LOOKUP || flag == DELETE || 235*37742Smckusick error != ENOENT || *cp != 0) 2367534Sroot goto bad; 2375972Swnj /* 23837715Smckusick * We return with ni_vp NULL to indicate that the entry 23937715Smckusick * doesn't currently exist, leaving a pointer to the 24037715Smckusick * (possibly locked) directory inode in ndp->ni_dvp. 2415972Swnj */ 24237715Smckusick FREE(ndp->ni_pnbuf, M_NAMEI); 24337715Smckusick return (0); /* should this be ENOENT? */ 2447534Sroot } 24537582Smarc #ifdef NAMEI_DIAGNOSTIC 24637715Smckusick printf("found\n"); 24737582Smarc #endif 2487534Sroot 2497534Sroot /* 25037715Smckusick * Check for symbolic link 2517534Sroot */ 25237715Smckusick dp = ndp->ni_vp; 25337715Smckusick if ((dp->v_type == VLNK) && 25437715Smckusick ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) { 25537715Smckusick struct iovec aiov; 25637715Smckusick struct uio auio; 25737715Smckusick int linklen; 2587534Sroot 25937715Smckusick if (++ndp->ni_loopcnt > MAXSYMLINKS) { 26037715Smckusick error = ELOOP; 26137715Smckusick goto bad2; 26237715Smckusick } 26337715Smckusick if (ndp->ni_pathlen == 1) 26437715Smckusick MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 2657534Sroot else 26637715Smckusick cp = ndp->ni_pnbuf; 26737715Smckusick aiov.iov_base = cp; 26837715Smckusick aiov.iov_len = MAXPATHLEN; 26937715Smckusick auio.uio_iov = &aiov; 27037715Smckusick auio.uio_iovcnt = 1; 27137715Smckusick auio.uio_offset = 0; 27237715Smckusick auio.uio_rw = UIO_READ; 27337715Smckusick auio.uio_segflg = UIO_SYSSPACE; 27437715Smckusick auio.uio_resid = MAXPATHLEN; 27537715Smckusick if (error = VOP_READLINK(dp, &auio, ndp->ni_cred)) { 27637715Smckusick if (ndp->ni_pathlen == 1) 27737715Smckusick free(cp, M_NAMEI); 27837715Smckusick goto bad2; 2799166Ssam } 28037715Smckusick linklen = MAXPATHLEN - auio.uio_resid; 28137715Smckusick if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 28237715Smckusick if (ndp->ni_pathlen == 1) 28337715Smckusick free(cp, M_NAMEI); 28437715Smckusick error = ENAMETOOLONG; 28512011Smckusick goto bad2; 28615798Smckusick } 28737715Smckusick if (ndp->ni_pathlen == 1) { 28837715Smckusick bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 28937715Smckusick FREE(ndp->ni_pnbuf, M_NAMEI); 29037715Smckusick ndp->ni_pnbuf = cp; 29137715Smckusick } else 29237715Smckusick ndp->ni_pnbuf[linklen] = '\0'; 29337715Smckusick ndp->ni_ptr = cp; 29437715Smckusick ndp->ni_pathlen += linklen; 29537715Smckusick vput(dp); 29637715Smckusick dp = ndp->ni_dvp; 29737715Smckusick goto start; 29815798Smckusick } 29915798Smckusick 3007534Sroot /* 30137715Smckusick * Check to see if the vnode has been mounted on; 30237715Smckusick * if so find the root of the mounted file system. 3037534Sroot */ 30437715Smckusick mntloop: 30537715Smckusick while (dp->v_type == VDIR && (mp = dp->v_mountedhere)) { 30637715Smckusick while(mp->m_flag & M_MLOCK) { 30737715Smckusick mp->m_flag |= M_MWAIT; 30837715Smckusick sleep((caddr_t)mp, PVFS); 30937715Smckusick goto mntloop; 31021014Smckusick } 31137715Smckusick error = VFS_ROOT(dp->v_mountedhere, &tdp); 31237715Smckusick if (error) 3137534Sroot goto bad2; 31437715Smckusick vput(dp); 31537715Smckusick ndp->ni_vp = dp = tdp; 31630Sbill } 3177534Sroot 31837715Smckusick nextname: 31930Sbill /* 3207534Sroot * Not a symbolic link. If more pathname, 3217534Sroot * continue at next component, else return. 32230Sbill */ 32337715Smckusick ndp->ni_ptr = ndp->ni_next; 32437715Smckusick if (*ndp->ni_ptr == '/') { 32537715Smckusick while (*ndp->ni_ptr == '/') { 32637715Smckusick ndp->ni_ptr++; 32737715Smckusick ndp->ni_pathlen--; 32837715Smckusick } 32937715Smckusick vrele(ndp->ni_dvp); 3307534Sroot goto dirloop; 33130Sbill } 3327534Sroot /* 33337715Smckusick * Check for read-only file systems and executing texts 3347534Sroot */ 33537715Smckusick if (flag != LOOKUP && (error = vn_access(dp, VWRITE, ndp->ni_cred))) 33637715Smckusick goto bad2; 33737715Smckusick if (!wantparent) 33837715Smckusick vrele(ndp->ni_dvp); 33937715Smckusick if ((ndp->ni_nameiop & LOCKLEAF) == 0) 34037715Smckusick VOP_UNLOCK(dp); 34137715Smckusick FREE(ndp->ni_pnbuf, M_NAMEI); 34237715Smckusick return (0); 3437534Sroot 34437715Smckusick bad2: 34537715Smckusick vrele(ndp->ni_dvp); 34637715Smckusick bad: 34737715Smckusick vput(dp); 34837715Smckusick ndp->ni_vp = NULL; 34937715Smckusick FREE(ndp->ni_pnbuf, M_NAMEI); 35010849Ssam return (error); 3515972Swnj } 352