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
| 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
|
43#include <sys/syslimits.h> 44#include <sys/time.h> 45#include <sys/namei.h> 46#include <sys/vnode.h> 47#include <sys/mount.h> 48#include <sys/errno.h> 49#include <sys/malloc.h> 50#include <sys/filedesc.h> 51#include <sys/proc.h> 52 53#ifdef KTRACE 54#include <sys/ktrace.h> 55#endif 56 57/* 58 * Convert a pathname into a pointer to a locked inode. 59 * 60 * The FOLLOW flag is set when symbolic links are to be followed 61 * when they occur at the end of the name translation process. 62 * Symbolic links are always followed for all other pathname 63 * components other than the last. 64 * 65 * The segflg defines whether the name is to be copied from user 66 * space or kernel space. 67 * 68 * Overall outline of namei: 69 * 70 * copy in name 71 * get starting directory 72 * while (!done && !error) { 73 * call lookup to search path. 74 * if symbolic link, massage name in buffer and continue 75 * } 76 */ 77int 78namei(ndp) 79 register struct nameidata *ndp; 80{ 81 register struct filedesc *fdp; /* pointer to file descriptor state */ 82 register char *cp; /* pointer into pathname argument */ 83 register struct vnode *dp; /* the directory we are searching */ 84 struct iovec aiov; /* uio for reading symbolic links */ 85 struct uio auio; 86 int error, linklen; 87 struct componentname *cnp = &ndp->ni_cnd; 88 89 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred; 90#ifdef DIAGNOSTIC 91 if (!cnp->cn_cred || !cnp->cn_proc) 92 panic ("namei: bad cred/proc"); 93 if (cnp->cn_nameiop & (~OPMASK)) 94 panic ("namei: nameiop contaminated with flags"); 95 if (cnp->cn_flags & OPMASK) 96 panic ("namei: flags contaminated with nameiops"); 97#endif 98 fdp = cnp->cn_proc->p_fd; 99 100 /* 101 * Get a buffer for the name to be translated, and copy the 102 * name into the buffer. 103 */ 104 if ((cnp->cn_flags & HASBUF) == 0) 105 MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 106 if (ndp->ni_segflg == UIO_SYSSPACE) 107 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 108 MAXPATHLEN, &ndp->ni_pathlen); 109 else 110 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 111 MAXPATHLEN, &ndp->ni_pathlen); 112 if (error) { 113 free(cnp->cn_pnbuf, M_NAMEI); 114 ndp->ni_vp = NULL; 115 return (error); 116 } 117 ndp->ni_loopcnt = 0; 118#ifdef KTRACE 119 if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 120 ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 121#endif 122 123 /* 124 * Get starting point for the translation. 125 */ 126 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL) 127 ndp->ni_rootdir = rootvnode; 128 dp = fdp->fd_cdir; 129 VREF(dp); 130 for (;;) { 131 /* 132 * Check if root directory should replace current directory. 133 * Done at start of translation and after symbolic link. 134 */ 135 cnp->cn_nameptr = cnp->cn_pnbuf; 136 if (*(cnp->cn_nameptr) == '/') { 137 vrele(dp); 138 while (*(cnp->cn_nameptr) == '/') { 139 cnp->cn_nameptr++; 140 ndp->ni_pathlen--; 141 } 142 dp = ndp->ni_rootdir; 143 VREF(dp); 144 } 145 ndp->ni_startdir = dp; 146 if (error = lookup(ndp)) { 147 FREE(cnp->cn_pnbuf, M_NAMEI); 148 return (error); 149 } 150 /* 151 * Check for symbolic link 152 */ 153 if ((cnp->cn_flags & ISSYMLINK) == 0) { 154 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 155 FREE(cnp->cn_pnbuf, M_NAMEI); 156 else 157 cnp->cn_flags |= HASBUF; 158 return (0); 159 } 160 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 161 VOP_UNLOCK(ndp->ni_dvp); 162 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 163 error = ELOOP; 164 break; 165 } 166 if (ndp->ni_pathlen > 1) 167 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 168 else 169 cp = cnp->cn_pnbuf; 170 aiov.iov_base = cp; 171 aiov.iov_len = MAXPATHLEN; 172 auio.uio_iov = &aiov; 173 auio.uio_iovcnt = 1; 174 auio.uio_offset = 0; 175 auio.uio_rw = UIO_READ; 176 auio.uio_segflg = UIO_SYSSPACE; 177 auio.uio_procp = (struct proc *)0; 178 auio.uio_resid = MAXPATHLEN; 179 if (error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred)) { 180 if (ndp->ni_pathlen > 1) 181 free(cp, M_NAMEI); 182 break; 183 } 184 linklen = MAXPATHLEN - auio.uio_resid; 185 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 186 if (ndp->ni_pathlen > 1) 187 free(cp, M_NAMEI); 188 error = ENAMETOOLONG; 189 break; 190 } 191 if (ndp->ni_pathlen > 1) { 192 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 193 FREE(cnp->cn_pnbuf, M_NAMEI); 194 cnp->cn_pnbuf = cp; 195 } else 196 cnp->cn_pnbuf[linklen] = '\0'; 197 ndp->ni_pathlen += linklen; 198 vput(ndp->ni_vp); 199 dp = ndp->ni_dvp; 200 } 201 FREE(cnp->cn_pnbuf, M_NAMEI); 202 vrele(ndp->ni_dvp); 203 vput(ndp->ni_vp); 204 ndp->ni_vp = NULL; 205 return (error); 206} 207 208/* 209 * Search a pathname. 210 * This is a very central and rather complicated routine. 211 * 212 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 213 * The starting directory is taken from ni_startdir. The pathname is 214 * descended until done, or a symbolic link is encountered. The variable 215 * ni_more is clear if the path is completed; it is set to one if a 216 * symbolic link needing interpretation is encountered. 217 * 218 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 219 * whether the name is to be looked up, created, renamed, or deleted. 220 * When CREATE, RENAME, or DELETE is specified, information usable in 221 * creating, renaming, or deleting a directory entry may be calculated. 222 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 223 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 224 * returned unlocked. Otherwise the parent directory is not returned. If 225 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 226 * the target is returned locked, otherwise it is returned unlocked. 227 * When creating or renaming and LOCKPARENT is specified, the target may not 228 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 229 * 230 * Overall outline of lookup: 231 * 232 * dirloop: 233 * identify next component of name at ndp->ni_ptr 234 * handle degenerate case where name is null string 235 * if .. and crossing mount points and on mounted filesys, find parent 236 * call VOP_LOOKUP routine for next component name 237 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 238 * component vnode returned in ni_vp (if it exists), locked. 239 * if result vnode is mounted on and crossing mount points, 240 * find mounted on vnode 241 * if more components of name, do next level at dirloop 242 * return the answer in ni_vp, locked if LOCKLEAF set 243 * if LOCKPARENT set, return locked parent in ni_dvp 244 * if WANTPARENT set, return unlocked parent in ni_dvp 245 */ 246int 247lookup(ndp) 248 register struct nameidata *ndp; 249{ 250 register char *cp; /* pointer into pathname argument */ 251 register struct vnode *dp = 0; /* the directory we are searching */ 252 struct vnode *tdp; /* saved dp */ 253 struct mount *mp; /* mount table entry */ 254 int docache; /* == 0 do not cache last component */ 255 int wantparent; /* 1 => wantparent or lockparent flag */ 256 int rdonly; /* lookup read-only flag bit */ 257 int error = 0; 258 struct componentname *cnp = &ndp->ni_cnd; 259 260 /* 261 * Setup: break out flag bits into variables. 262 */ 263 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 264 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 265 if (cnp->cn_nameiop == DELETE || 266 (wantparent && cnp->cn_nameiop != CREATE)) 267 docache = 0; 268 rdonly = cnp->cn_flags & RDONLY; 269 ndp->ni_dvp = NULL; 270 cnp->cn_flags &= ~ISSYMLINK; 271 dp = ndp->ni_startdir; 272 ndp->ni_startdir = NULLVP; 273 VOP_LOCK(dp); 274 275dirloop: 276 /* 277 * Search a new directory. 278 * 279 * The cn_hash value is for use by vfs_cache. 280 * The last component of the filename is left accessible via 281 * cnp->cn_nameptr for callers that need the name. Callers needing 282 * the name set the SAVENAME flag. When done, they assume 283 * responsibility for freeing the pathname buffer. 284 */ 285 cnp->cn_consume = 0; 286 cnp->cn_hash = 0; 287 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 288 cnp->cn_hash += (unsigned char)*cp; 289 cnp->cn_namelen = cp - cnp->cn_nameptr; 290 if (cnp->cn_namelen > NAME_MAX) { 291 error = ENAMETOOLONG; 292 goto bad; 293 } 294#ifdef NAMEI_DIAGNOSTIC 295 { char c = *cp; 296 *cp = '\0'; 297 printf("{%s}: ", cnp->cn_nameptr); 298 *cp = c; } 299#endif 300 ndp->ni_pathlen -= cnp->cn_namelen; 301 ndp->ni_next = cp; 302 cnp->cn_flags |= MAKEENTRY; 303 if (*cp == '\0' && docache == 0) 304 cnp->cn_flags &= ~MAKEENTRY; 305 if (cnp->cn_namelen == 2 && 306 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 307 cnp->cn_flags |= ISDOTDOT; 308 else 309 cnp->cn_flags &= ~ISDOTDOT; 310 if (*ndp->ni_next == 0) 311 cnp->cn_flags |= ISLASTCN; 312 else 313 cnp->cn_flags &= ~ISLASTCN; 314 315 316 /* 317 * Check for degenerate name (e.g. / or "") 318 * which is a way of talking about a directory, 319 * e.g. like "/." or ".". 320 */ 321 if (cnp->cn_nameptr[0] == '\0') { 322 if (cnp->cn_nameiop != LOOKUP) { 323 error = EISDIR; 324 goto bad; 325 } 326 if (dp->v_type != VDIR) { 327 error = ENOTDIR; 328 goto bad; 329 } 330 if (wantparent) { 331 ndp->ni_dvp = dp; 332 VREF(dp); 333 } 334 ndp->ni_vp = dp; 335 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 336 VOP_UNLOCK(dp); 337 if (cnp->cn_flags & SAVESTART) 338 panic("lookup: SAVESTART"); 339 return (0); 340 } 341 342 /* 343 * Handle "..": two special cases. 344 * 1. If at root directory (e.g. after chroot) 345 * or at absolute root directory 346 * then ignore it so can't get out. 347 * 2. If this vnode is the root of a mounted 348 * filesystem, then replace it with the 349 * vnode which was mounted on so we take the 350 * .. in the other file system. 351 */ 352 if (cnp->cn_flags & ISDOTDOT) { 353 for (;;) { 354 if (dp == ndp->ni_rootdir || dp == rootvnode) { 355 ndp->ni_dvp = dp; 356 ndp->ni_vp = dp; 357 VREF(dp); 358 goto nextname; 359 } 360 if ((dp->v_flag & VROOT) == 0 || 361 (cnp->cn_flags & NOCROSSMOUNT)) 362 break; 363 tdp = dp; 364 dp = dp->v_mount->mnt_vnodecovered; 365 vput(tdp); 366 VREF(dp); 367 VOP_LOCK(dp); 368 } 369 } 370 371 /* 372 * We now have a segment name to search for, and a directory to search. 373 */ 374unionlookup: 375 ndp->ni_dvp = dp; 376 if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) { 377#ifdef DIAGNOSTIC 378 if (ndp->ni_vp != NULL) 379 panic("leaf should be empty"); 380#endif 381#ifdef NAMEI_DIAGNOSTIC 382 printf("not found\n"); 383#endif 384 if ((error == ENOENT) && 385 (dp->v_flag & VROOT) && 386 (dp->v_mount->mnt_flag & MNT_UNION)) { 387 tdp = dp; 388 dp = dp->v_mount->mnt_vnodecovered; 389 vput(tdp); 390 VREF(dp); 391 VOP_LOCK(dp); 392 goto unionlookup; 393 } 394 395 if (error != EJUSTRETURN) 396 goto bad; 397 /* 398 * If creating and at end of pathname, then can consider 399 * allowing file to be created. 400 */ 401 if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) { 402 error = EROFS; 403 goto bad; 404 } 405 /* 406 * We return with ni_vp NULL to indicate that the entry 407 * doesn't currently exist, leaving a pointer to the 408 * (possibly locked) directory inode in ndp->ni_dvp. 409 */ 410 if (cnp->cn_flags & SAVESTART) { 411 ndp->ni_startdir = ndp->ni_dvp; 412 VREF(ndp->ni_startdir); 413 } 414 return (0); 415 } 416#ifdef NAMEI_DIAGNOSTIC 417 printf("found\n"); 418#endif 419 420 /* 421 * Take into account any additional components consumed by 422 * the underlying filesystem. 423 */ 424 if (cnp->cn_consume > 0) { 425 cnp->cn_nameptr += cnp->cn_consume; 426 ndp->ni_next += cnp->cn_consume; 427 ndp->ni_pathlen -= cnp->cn_consume; 428 cnp->cn_consume = 0; 429 } 430 431 dp = ndp->ni_vp; 432 /* 433 * Check for symbolic link 434 */ 435 if ((dp->v_type == VLNK) && 436 ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) { 437 cnp->cn_flags |= ISSYMLINK; 438 return (0); 439 } 440 441 /* 442 * Check to see if the vnode has been mounted on; 443 * if so find the root of the mounted file system. 444 */ 445 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 446 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 447 if (mp->mnt_flag & MNT_MLOCK) { 448 mp->mnt_flag |= MNT_MWAIT; 449 sleep((caddr_t)mp, PVFS); 450 continue; 451 } 452 if (error = VFS_ROOT(dp->v_mountedhere, &tdp)) 453 goto bad2; 454 vput(dp); 455 ndp->ni_vp = dp = tdp; 456 } 457 458nextname: 459 /* 460 * Not a symbolic link. If more pathname, 461 * continue at next component, else return. 462 */ 463 if (*ndp->ni_next == '/') { 464 cnp->cn_nameptr = ndp->ni_next; 465 while (*cnp->cn_nameptr == '/') { 466 cnp->cn_nameptr++; 467 ndp->ni_pathlen--; 468 } 469 vrele(ndp->ni_dvp); 470 goto dirloop; 471 } 472 /* 473 * Check for read-only file systems. 474 */ 475 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) { 476 /* 477 * Disallow directory write attempts on read-only 478 * file systems. 479 */ 480 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) || 481 (wantparent && 482 (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) { 483 error = EROFS; 484 goto bad2; 485 } 486 } 487 if (cnp->cn_flags & SAVESTART) { 488 ndp->ni_startdir = ndp->ni_dvp; 489 VREF(ndp->ni_startdir); 490 } 491 if (!wantparent) 492 vrele(ndp->ni_dvp); 493 if ((cnp->cn_flags & LOCKLEAF) == 0) 494 VOP_UNLOCK(dp); 495 return (0); 496 497bad2: 498 if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 499 VOP_UNLOCK(ndp->ni_dvp); 500 vrele(ndp->ni_dvp); 501bad: 502 vput(dp); 503 ndp->ni_vp = NULL; 504 return (error); 505} 506 507
| 44#include <sys/syslimits.h> 45#include <sys/time.h> 46#include <sys/namei.h> 47#include <sys/vnode.h> 48#include <sys/mount.h> 49#include <sys/errno.h> 50#include <sys/malloc.h> 51#include <sys/filedesc.h> 52#include <sys/proc.h> 53 54#ifdef KTRACE 55#include <sys/ktrace.h> 56#endif 57 58/* 59 * Convert a pathname into a pointer to a locked inode. 60 * 61 * The FOLLOW flag is set when symbolic links are to be followed 62 * when they occur at the end of the name translation process. 63 * Symbolic links are always followed for all other pathname 64 * components other than the last. 65 * 66 * The segflg defines whether the name is to be copied from user 67 * space or kernel space. 68 * 69 * Overall outline of namei: 70 * 71 * copy in name 72 * get starting directory 73 * while (!done && !error) { 74 * call lookup to search path. 75 * if symbolic link, massage name in buffer and continue 76 * } 77 */ 78int 79namei(ndp) 80 register struct nameidata *ndp; 81{ 82 register struct filedesc *fdp; /* pointer to file descriptor state */ 83 register char *cp; /* pointer into pathname argument */ 84 register struct vnode *dp; /* the directory we are searching */ 85 struct iovec aiov; /* uio for reading symbolic links */ 86 struct uio auio; 87 int error, linklen; 88 struct componentname *cnp = &ndp->ni_cnd; 89 90 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred; 91#ifdef DIAGNOSTIC 92 if (!cnp->cn_cred || !cnp->cn_proc) 93 panic ("namei: bad cred/proc"); 94 if (cnp->cn_nameiop & (~OPMASK)) 95 panic ("namei: nameiop contaminated with flags"); 96 if (cnp->cn_flags & OPMASK) 97 panic ("namei: flags contaminated with nameiops"); 98#endif 99 fdp = cnp->cn_proc->p_fd; 100 101 /* 102 * Get a buffer for the name to be translated, and copy the 103 * name into the buffer. 104 */ 105 if ((cnp->cn_flags & HASBUF) == 0) 106 MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); 107 if (ndp->ni_segflg == UIO_SYSSPACE) 108 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 109 MAXPATHLEN, &ndp->ni_pathlen); 110 else 111 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 112 MAXPATHLEN, &ndp->ni_pathlen); 113 if (error) { 114 free(cnp->cn_pnbuf, M_NAMEI); 115 ndp->ni_vp = NULL; 116 return (error); 117 } 118 ndp->ni_loopcnt = 0; 119#ifdef KTRACE 120 if (KTRPOINT(cnp->cn_proc, KTR_NAMEI)) 121 ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf); 122#endif 123 124 /* 125 * Get starting point for the translation. 126 */ 127 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL) 128 ndp->ni_rootdir = rootvnode; 129 dp = fdp->fd_cdir; 130 VREF(dp); 131 for (;;) { 132 /* 133 * Check if root directory should replace current directory. 134 * Done at start of translation and after symbolic link. 135 */ 136 cnp->cn_nameptr = cnp->cn_pnbuf; 137 if (*(cnp->cn_nameptr) == '/') { 138 vrele(dp); 139 while (*(cnp->cn_nameptr) == '/') { 140 cnp->cn_nameptr++; 141 ndp->ni_pathlen--; 142 } 143 dp = ndp->ni_rootdir; 144 VREF(dp); 145 } 146 ndp->ni_startdir = dp; 147 if (error = lookup(ndp)) { 148 FREE(cnp->cn_pnbuf, M_NAMEI); 149 return (error); 150 } 151 /* 152 * Check for symbolic link 153 */ 154 if ((cnp->cn_flags & ISSYMLINK) == 0) { 155 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 156 FREE(cnp->cn_pnbuf, M_NAMEI); 157 else 158 cnp->cn_flags |= HASBUF; 159 return (0); 160 } 161 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 162 VOP_UNLOCK(ndp->ni_dvp); 163 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 164 error = ELOOP; 165 break; 166 } 167 if (ndp->ni_pathlen > 1) 168 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); 169 else 170 cp = cnp->cn_pnbuf; 171 aiov.iov_base = cp; 172 aiov.iov_len = MAXPATHLEN; 173 auio.uio_iov = &aiov; 174 auio.uio_iovcnt = 1; 175 auio.uio_offset = 0; 176 auio.uio_rw = UIO_READ; 177 auio.uio_segflg = UIO_SYSSPACE; 178 auio.uio_procp = (struct proc *)0; 179 auio.uio_resid = MAXPATHLEN; 180 if (error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred)) { 181 if (ndp->ni_pathlen > 1) 182 free(cp, M_NAMEI); 183 break; 184 } 185 linklen = MAXPATHLEN - auio.uio_resid; 186 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 187 if (ndp->ni_pathlen > 1) 188 free(cp, M_NAMEI); 189 error = ENAMETOOLONG; 190 break; 191 } 192 if (ndp->ni_pathlen > 1) { 193 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 194 FREE(cnp->cn_pnbuf, M_NAMEI); 195 cnp->cn_pnbuf = cp; 196 } else 197 cnp->cn_pnbuf[linklen] = '\0'; 198 ndp->ni_pathlen += linklen; 199 vput(ndp->ni_vp); 200 dp = ndp->ni_dvp; 201 } 202 FREE(cnp->cn_pnbuf, M_NAMEI); 203 vrele(ndp->ni_dvp); 204 vput(ndp->ni_vp); 205 ndp->ni_vp = NULL; 206 return (error); 207} 208 209/* 210 * Search a pathname. 211 * This is a very central and rather complicated routine. 212 * 213 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 214 * The starting directory is taken from ni_startdir. The pathname is 215 * descended until done, or a symbolic link is encountered. The variable 216 * ni_more is clear if the path is completed; it is set to one if a 217 * symbolic link needing interpretation is encountered. 218 * 219 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 220 * whether the name is to be looked up, created, renamed, or deleted. 221 * When CREATE, RENAME, or DELETE is specified, information usable in 222 * creating, renaming, or deleting a directory entry may be calculated. 223 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 224 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 225 * returned unlocked. Otherwise the parent directory is not returned. If 226 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 227 * the target is returned locked, otherwise it is returned unlocked. 228 * When creating or renaming and LOCKPARENT is specified, the target may not 229 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 230 * 231 * Overall outline of lookup: 232 * 233 * dirloop: 234 * identify next component of name at ndp->ni_ptr 235 * handle degenerate case where name is null string 236 * if .. and crossing mount points and on mounted filesys, find parent 237 * call VOP_LOOKUP routine for next component name 238 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 239 * component vnode returned in ni_vp (if it exists), locked. 240 * if result vnode is mounted on and crossing mount points, 241 * find mounted on vnode 242 * if more components of name, do next level at dirloop 243 * return the answer in ni_vp, locked if LOCKLEAF set 244 * if LOCKPARENT set, return locked parent in ni_dvp 245 * if WANTPARENT set, return unlocked parent in ni_dvp 246 */ 247int 248lookup(ndp) 249 register struct nameidata *ndp; 250{ 251 register char *cp; /* pointer into pathname argument */ 252 register struct vnode *dp = 0; /* the directory we are searching */ 253 struct vnode *tdp; /* saved dp */ 254 struct mount *mp; /* mount table entry */ 255 int docache; /* == 0 do not cache last component */ 256 int wantparent; /* 1 => wantparent or lockparent flag */ 257 int rdonly; /* lookup read-only flag bit */ 258 int error = 0; 259 struct componentname *cnp = &ndp->ni_cnd; 260 261 /* 262 * Setup: break out flag bits into variables. 263 */ 264 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 265 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 266 if (cnp->cn_nameiop == DELETE || 267 (wantparent && cnp->cn_nameiop != CREATE)) 268 docache = 0; 269 rdonly = cnp->cn_flags & RDONLY; 270 ndp->ni_dvp = NULL; 271 cnp->cn_flags &= ~ISSYMLINK; 272 dp = ndp->ni_startdir; 273 ndp->ni_startdir = NULLVP; 274 VOP_LOCK(dp); 275 276dirloop: 277 /* 278 * Search a new directory. 279 * 280 * The cn_hash value is for use by vfs_cache. 281 * The last component of the filename is left accessible via 282 * cnp->cn_nameptr for callers that need the name. Callers needing 283 * the name set the SAVENAME flag. When done, they assume 284 * responsibility for freeing the pathname buffer. 285 */ 286 cnp->cn_consume = 0; 287 cnp->cn_hash = 0; 288 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 289 cnp->cn_hash += (unsigned char)*cp; 290 cnp->cn_namelen = cp - cnp->cn_nameptr; 291 if (cnp->cn_namelen > NAME_MAX) { 292 error = ENAMETOOLONG; 293 goto bad; 294 } 295#ifdef NAMEI_DIAGNOSTIC 296 { char c = *cp; 297 *cp = '\0'; 298 printf("{%s}: ", cnp->cn_nameptr); 299 *cp = c; } 300#endif 301 ndp->ni_pathlen -= cnp->cn_namelen; 302 ndp->ni_next = cp; 303 cnp->cn_flags |= MAKEENTRY; 304 if (*cp == '\0' && docache == 0) 305 cnp->cn_flags &= ~MAKEENTRY; 306 if (cnp->cn_namelen == 2 && 307 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 308 cnp->cn_flags |= ISDOTDOT; 309 else 310 cnp->cn_flags &= ~ISDOTDOT; 311 if (*ndp->ni_next == 0) 312 cnp->cn_flags |= ISLASTCN; 313 else 314 cnp->cn_flags &= ~ISLASTCN; 315 316 317 /* 318 * Check for degenerate name (e.g. / or "") 319 * which is a way of talking about a directory, 320 * e.g. like "/." or ".". 321 */ 322 if (cnp->cn_nameptr[0] == '\0') { 323 if (cnp->cn_nameiop != LOOKUP) { 324 error = EISDIR; 325 goto bad; 326 } 327 if (dp->v_type != VDIR) { 328 error = ENOTDIR; 329 goto bad; 330 } 331 if (wantparent) { 332 ndp->ni_dvp = dp; 333 VREF(dp); 334 } 335 ndp->ni_vp = dp; 336 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 337 VOP_UNLOCK(dp); 338 if (cnp->cn_flags & SAVESTART) 339 panic("lookup: SAVESTART"); 340 return (0); 341 } 342 343 /* 344 * Handle "..": two special cases. 345 * 1. If at root directory (e.g. after chroot) 346 * or at absolute root directory 347 * then ignore it so can't get out. 348 * 2. If this vnode is the root of a mounted 349 * filesystem, then replace it with the 350 * vnode which was mounted on so we take the 351 * .. in the other file system. 352 */ 353 if (cnp->cn_flags & ISDOTDOT) { 354 for (;;) { 355 if (dp == ndp->ni_rootdir || dp == rootvnode) { 356 ndp->ni_dvp = dp; 357 ndp->ni_vp = dp; 358 VREF(dp); 359 goto nextname; 360 } 361 if ((dp->v_flag & VROOT) == 0 || 362 (cnp->cn_flags & NOCROSSMOUNT)) 363 break; 364 tdp = dp; 365 dp = dp->v_mount->mnt_vnodecovered; 366 vput(tdp); 367 VREF(dp); 368 VOP_LOCK(dp); 369 } 370 } 371 372 /* 373 * We now have a segment name to search for, and a directory to search. 374 */ 375unionlookup: 376 ndp->ni_dvp = dp; 377 if (error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) { 378#ifdef DIAGNOSTIC 379 if (ndp->ni_vp != NULL) 380 panic("leaf should be empty"); 381#endif 382#ifdef NAMEI_DIAGNOSTIC 383 printf("not found\n"); 384#endif 385 if ((error == ENOENT) && 386 (dp->v_flag & VROOT) && 387 (dp->v_mount->mnt_flag & MNT_UNION)) { 388 tdp = dp; 389 dp = dp->v_mount->mnt_vnodecovered; 390 vput(tdp); 391 VREF(dp); 392 VOP_LOCK(dp); 393 goto unionlookup; 394 } 395 396 if (error != EJUSTRETURN) 397 goto bad; 398 /* 399 * If creating and at end of pathname, then can consider 400 * allowing file to be created. 401 */ 402 if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) { 403 error = EROFS; 404 goto bad; 405 } 406 /* 407 * We return with ni_vp NULL to indicate that the entry 408 * doesn't currently exist, leaving a pointer to the 409 * (possibly locked) directory inode in ndp->ni_dvp. 410 */ 411 if (cnp->cn_flags & SAVESTART) { 412 ndp->ni_startdir = ndp->ni_dvp; 413 VREF(ndp->ni_startdir); 414 } 415 return (0); 416 } 417#ifdef NAMEI_DIAGNOSTIC 418 printf("found\n"); 419#endif 420 421 /* 422 * Take into account any additional components consumed by 423 * the underlying filesystem. 424 */ 425 if (cnp->cn_consume > 0) { 426 cnp->cn_nameptr += cnp->cn_consume; 427 ndp->ni_next += cnp->cn_consume; 428 ndp->ni_pathlen -= cnp->cn_consume; 429 cnp->cn_consume = 0; 430 } 431 432 dp = ndp->ni_vp; 433 /* 434 * Check for symbolic link 435 */ 436 if ((dp->v_type == VLNK) && 437 ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) { 438 cnp->cn_flags |= ISSYMLINK; 439 return (0); 440 } 441 442 /* 443 * Check to see if the vnode has been mounted on; 444 * if so find the root of the mounted file system. 445 */ 446 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 447 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 448 if (mp->mnt_flag & MNT_MLOCK) { 449 mp->mnt_flag |= MNT_MWAIT; 450 sleep((caddr_t)mp, PVFS); 451 continue; 452 } 453 if (error = VFS_ROOT(dp->v_mountedhere, &tdp)) 454 goto bad2; 455 vput(dp); 456 ndp->ni_vp = dp = tdp; 457 } 458 459nextname: 460 /* 461 * Not a symbolic link. If more pathname, 462 * continue at next component, else return. 463 */ 464 if (*ndp->ni_next == '/') { 465 cnp->cn_nameptr = ndp->ni_next; 466 while (*cnp->cn_nameptr == '/') { 467 cnp->cn_nameptr++; 468 ndp->ni_pathlen--; 469 } 470 vrele(ndp->ni_dvp); 471 goto dirloop; 472 } 473 /* 474 * Check for read-only file systems. 475 */ 476 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) { 477 /* 478 * Disallow directory write attempts on read-only 479 * file systems. 480 */ 481 if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) || 482 (wantparent && 483 (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) { 484 error = EROFS; 485 goto bad2; 486 } 487 } 488 if (cnp->cn_flags & SAVESTART) { 489 ndp->ni_startdir = ndp->ni_dvp; 490 VREF(ndp->ni_startdir); 491 } 492 if (!wantparent) 493 vrele(ndp->ni_dvp); 494 if ((cnp->cn_flags & LOCKLEAF) == 0) 495 VOP_UNLOCK(dp); 496 return (0); 497 498bad2: 499 if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0') 500 VOP_UNLOCK(ndp->ni_dvp); 501 vrele(ndp->ni_dvp); 502bad: 503 vput(dp); 504 ndp->ni_vp = NULL; 505 return (error); 506} 507 508
|