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