1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)vfs_lookup.c 7.8 (Berkeley) 05/09/89 18 */ 19 20 #include "param.h" 21 #include "time.h" 22 #include "namei.h" 23 #include "vnode.h" 24 #include "mount.h" 25 #include "errno.h" 26 #include "malloc.h" 27 28 #ifdef KTRACE 29 #include "user.h" 30 #include "proc.h" 31 #include "ktrace.h" 32 #endif 33 34 /* 35 * Convert a pathname into a pointer to a locked inode. 36 * This is a very central and rather complicated routine. 37 * 38 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 39 * whether the name is to be looked up, created, renamed, or deleted. 40 * When CREATE, RENAME, or DELETE is specified, information usable in 41 * creating, renaming, or deleting a directory entry may be calculated. 42 * If flag has LOCKPARENT or'ed into it and the target of the pathname 43 * exists, namei returns both the target and its parent directory locked. 44 * When creating or renaming and LOCKPARENT is specified, the target may not 45 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 46 * 47 * The FOLLOW flag is set when symbolic links are to be followed 48 * when they occur at the end of the name translation process. 49 * Symbolic links are always followed for all other pathname 50 * components other than the last. 51 * 52 * The segflg defines whether the name is to be copied from user 53 * space or kernel space. 54 * 55 * Overall outline of namei: 56 * 57 * copy in name 58 * get starting directory 59 * dirloop: 60 * copy next component of name to ndp->ni_dent 61 * handle degenerate case where name is null string 62 * if .. and on mounted filesys, find parent 63 * call lookup routine for next component name 64 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 65 * component vnode returned in ni_vp (if it exists), locked. 66 * if symbolic link, massage name in buffer and continue at dirloop 67 * if result inode is mounted on, find mounted on vnode 68 * if more components of name, do next level at dirloop 69 * return the answer in ni_vp as locked vnode; 70 * if LOCKPARENT set, return locked parent in ni_dvp 71 * 72 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked. 73 */ 74 namei(ndp) 75 register struct nameidata *ndp; 76 { 77 register char *cp; /* pointer into pathname argument */ 78 register struct vnode *dp = 0; /* the directory we are searching */ 79 register int i; /* Temp counter */ 80 struct vnode *tdp; /* saved dp */ 81 struct mount *mp; /* mount table entry */ 82 int docache; /* == 0 do not cache last component */ 83 int flag; /* LOOKUP, CREATE, RENAME or DELETE */ 84 int wantparent; /* 1 => wantparent or lockparent flag */ 85 int error = 0; 86 87 flag = ndp->ni_nameiop & OPFLAG; 88 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT); 89 docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE; 90 if (flag == DELETE || wantparent) 91 docache = 0; 92 /* 93 * Get a buffer for the name to be translated, and copy the 94 * name into the buffer. 95 */ 96 MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 97 if (ndp->ni_segflg == UIO_SYSSPACE) 98 error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN, 99 &ndp->ni_pathlen); 100 else 101 error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN, 102 &ndp->ni_pathlen); 103 if (error) { 104 free(ndp->ni_pnbuf, M_NAMEI); 105 return (error); 106 } 107 ndp->ni_ptr = ndp->ni_pnbuf; 108 ndp->ni_loopcnt = 0; 109 dp = ndp->ni_cdir; 110 dp->v_count++; 111 #ifdef KTRACE 112 if (KTRPOINT(u.u_procp, KTR_NAMEI)) 113 ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf); 114 #endif 115 116 start: 117 /* 118 * Get starting directory. 119 * Done at start of translation and after symbolic link. 120 */ 121 if (*ndp->ni_ptr == '/') { 122 vrele(dp); 123 while (*ndp->ni_ptr == '/') { 124 ndp->ni_ptr++; 125 ndp->ni_pathlen--; 126 } 127 if ((dp = ndp->ni_rdir) == NULL) 128 dp = rootdir; 129 dp->v_count++; 130 } 131 VOP_LOCK(dp); 132 ndp->ni_endoff = 0; 133 134 /* 135 * We come to dirloop to search a new directory. 136 */ 137 dirloop: 138 /* 139 * Copy next component of name to ndp->ni_dent. 140 * XXX kern_exec looks at d_name 141 * ??? The ni_hash value may be useful for vfs_cache 142 * XXX There must be the last component of the filename left 143 * somewhere accessible via. ndp for NFS (and any other stateless file 144 * systems) in case they are doing a CREATE. The "Towards a..." noted 145 * that ni_ptr would be left pointing to the last component, but since 146 * the ni_pnbuf gets free'd, that is not a good idea. 147 */ 148 ndp->ni_hash = 0; 149 for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) { 150 if (i >= MAXNAMLEN) { 151 error = ENAMETOOLONG; 152 goto bad; 153 } 154 if (*cp & 0200) 155 if ((*cp&0377) == ('/'|0200) || flag != DELETE) { 156 error = EINVAL; 157 goto bad; 158 } 159 ndp->ni_dent.d_name[i++] = *cp; 160 ndp->ni_hash += (unsigned char)*cp * i; 161 } 162 ndp->ni_namelen = i; 163 ndp->ni_dent.d_namlen = i; 164 ndp->ni_dent.d_name[i] = '\0'; 165 ndp->ni_pathlen -= i; 166 #ifdef NAMEI_DIAGNOSTIC 167 printf("{%s}: ", ndp->ni_dent.d_name); 168 #endif 169 ndp->ni_next = cp; 170 ndp->ni_makeentry = 1; 171 if (*cp == '\0' && docache == 0) 172 ndp->ni_makeentry = 0; 173 ndp->ni_isdotdot = (ndp->ni_namelen == 2 && 174 ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.'); 175 176 /* 177 * Check for degenerate name (e.g. / or "") 178 * which is a way of talking about a directory, 179 * e.g. like "/." or ".". 180 */ 181 if (ndp->ni_ptr[0] == '\0') { 182 if (flag != LOOKUP || wantparent) { 183 error = EISDIR; 184 goto bad; 185 } 186 free(ndp->ni_pnbuf, M_NAMEI); 187 if (!(ndp->ni_nameiop & LOCKLEAF)) 188 VOP_UNLOCK(dp); 189 ndp->ni_vp = dp; 190 return (0); 191 } 192 193 /* 194 * Handle "..": two special cases. 195 * 1. If at root directory (e.g. after chroot) 196 * then ignore it so can't get out. 197 * 2. If this vnode is the root of a mounted 198 * file system, then replace it with the 199 * vnode which was mounted on so we take the 200 * .. in the other file system. 201 */ 202 if (ndp->ni_isdotdot) { 203 for (;;) { 204 if (dp == ndp->ni_rdir || dp == rootdir) { 205 ndp->ni_dvp = dp; 206 dp->v_count++; 207 goto nextname; 208 } 209 if ((dp->v_flag & VROOT) == 0) 210 break; 211 tdp = dp; 212 dp = dp->v_mount->m_vnodecovered; 213 vput(tdp); 214 VOP_LOCK(dp); 215 dp->v_count++; 216 } 217 } 218 219 /* 220 * We now have a segment name to search for, and a directory to search. 221 */ 222 if (error = VOP_LOOKUP(dp, ndp)) { 223 if (ndp->ni_vp != NULL) 224 panic("leaf should be empty"); 225 #ifdef NAMEI_DIAGNOSTIC 226 printf("not found\n"); 227 #endif 228 /* 229 * If creating and at end of pathname, then can consider 230 * allowing file to be created. 231 */ 232 if (ndp->ni_dvp->v_mount->m_flag & M_RDONLY) 233 error = EROFS; 234 if (flag == LOOKUP || flag == DELETE || 235 error != ENOENT || *cp != 0) 236 goto bad; 237 /* 238 * We return with ni_vp NULL to indicate that the entry 239 * doesn't currently exist, leaving a pointer to the 240 * (possibly locked) directory inode in ndp->ni_dvp. 241 */ 242 FREE(ndp->ni_pnbuf, M_NAMEI); 243 return (0); /* should this be ENOENT? */ 244 } 245 #ifdef NAMEI_DIAGNOSTIC 246 printf("found\n"); 247 #endif 248 249 /* 250 * Check for symbolic link 251 */ 252 dp = ndp->ni_vp; 253 if ((dp->v_type == VLNK) && 254 ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) { 255 struct iovec aiov; 256 struct uio auio; 257 int linklen; 258 259 if (++ndp->ni_loopcnt > MAXSYMLINKS) { 260 error = ELOOP; 261 goto bad2; 262 } 263 if (ndp->ni_pathlen == 1) 264 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 265 else 266 cp = ndp->ni_pnbuf; 267 aiov.iov_base = cp; 268 aiov.iov_len = MAXPATHLEN; 269 auio.uio_iov = &aiov; 270 auio.uio_iovcnt = 1; 271 auio.uio_offset = 0; 272 auio.uio_rw = UIO_READ; 273 auio.uio_segflg = UIO_SYSSPACE; 274 auio.uio_resid = MAXPATHLEN; 275 if (error = VOP_READLINK(dp, &auio, ndp->ni_cred)) { 276 if (ndp->ni_pathlen == 1) 277 free(cp, M_NAMEI); 278 goto bad2; 279 } 280 linklen = MAXPATHLEN - auio.uio_resid; 281 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 282 if (ndp->ni_pathlen == 1) 283 free(cp, M_NAMEI); 284 error = ENAMETOOLONG; 285 goto bad2; 286 } 287 if (ndp->ni_pathlen == 1) { 288 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 289 FREE(ndp->ni_pnbuf, M_NAMEI); 290 ndp->ni_pnbuf = cp; 291 } else 292 ndp->ni_pnbuf[linklen] = '\0'; 293 ndp->ni_ptr = cp; 294 ndp->ni_pathlen += linklen; 295 vput(dp); 296 dp = ndp->ni_dvp; 297 goto start; 298 } 299 300 /* 301 * Check to see if the vnode has been mounted on; 302 * if so find the root of the mounted file system. 303 */ 304 mntloop: 305 while (dp->v_type == VDIR && (mp = dp->v_mountedhere)) { 306 while(mp->m_flag & M_MLOCK) { 307 mp->m_flag |= M_MWAIT; 308 sleep((caddr_t)mp, PVFS); 309 goto mntloop; 310 } 311 error = VFS_ROOT(dp->v_mountedhere, &tdp); 312 if (error) 313 goto bad2; 314 vput(dp); 315 ndp->ni_vp = dp = tdp; 316 } 317 318 nextname: 319 /* 320 * Not a symbolic link. If more pathname, 321 * continue at next component, else return. 322 */ 323 ndp->ni_ptr = ndp->ni_next; 324 if (*ndp->ni_ptr == '/') { 325 while (*ndp->ni_ptr == '/') { 326 ndp->ni_ptr++; 327 ndp->ni_pathlen--; 328 } 329 vrele(ndp->ni_dvp); 330 goto dirloop; 331 } 332 /* 333 * Check for read-only file systems and executing texts 334 */ 335 if (flag != LOOKUP && (error = vn_access(dp, VWRITE, ndp->ni_cred))) 336 goto bad2; 337 if (!wantparent) 338 vrele(ndp->ni_dvp); 339 if ((ndp->ni_nameiop & LOCKLEAF) == 0) 340 VOP_UNLOCK(dp); 341 FREE(ndp->ni_pnbuf, M_NAMEI); 342 return (0); 343 344 bad2: 345 vrele(ndp->ni_dvp); 346 bad: 347 vput(dp); 348 ndp->ni_vp = NULL; 349 FREE(ndp->ni_pnbuf, M_NAMEI); 350 return (error); 351 } 352