Home | History | Annotate | Line # | Download | only in kern
vfs_getcwd.c revision 1.16
      1 /* $NetBSD: vfs_getcwd.c,v 1.16 2003/01/18 10:06:37 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld.
      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 NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.16 2003/01/18 10:06:37 thorpej Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/namei.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/vnode.h>
     50 #include <sys/mount.h>
     51 #include <sys/proc.h>
     52 #include <sys/uio.h>
     53 #include <sys/malloc.h>
     54 #include <sys/dirent.h>
     55 #include <ufs/ufs/dir.h>	/* XXX only for DIRBLKSIZ */
     56 
     57 #include <sys/sa.h>
     58 #include <sys/syscallargs.h>
     59 
     60 static int
     61 getcwd_scandir __P((struct vnode **, struct vnode **,
     62     char **, char *, struct proc *));
     63 static int
     64 getcwd_getcache __P((struct vnode **, struct vnode **,
     65     char **, char *));
     66 int
     67 getcwd_common __P((struct vnode *, struct vnode *,
     68 		   char **, char *, int, int, struct proc *));
     69 
     70 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
     71 
     72 /*
     73  * Vnode variable naming conventions in this file:
     74  *
     75  * rvp: the current root we're aiming towards.
     76  * lvp, *lvpp: the "lower" vnode
     77  * uvp, *uvpp: the "upper" vnode.
     78  *
     79  * Since all the vnodes we're dealing with are directories, and the
     80  * lookups are going *up* in the filesystem rather than *down*, the
     81  * usual "pvp" (parent) or "dvp" (directory) naming conventions are
     82  * too confusing.
     83  */
     84 
     85 /*
     86  * XXX Will infinite loop in certain cases if a directory read reliably
     87  *	returns EINVAL on last block.
     88  * XXX is EINVAL the right thing to return if a directory is malformed?
     89  */
     90 
     91 /*
     92  * XXX Untested vs. mount -o union; probably does the wrong thing.
     93  */
     94 
     95 /*
     96  * Find parent vnode of *lvpp, return in *uvpp
     97  *
     98  * If we care about the name, scan it looking for name of directory
     99  * entry pointing at lvp.
    100  *
    101  * Place the name in the buffer which starts at bufp, immediately
    102  * before *bpp, and move bpp backwards to point at the start of it.
    103  *
    104  * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
    105  * On exit, *uvpp is either NULL or is a locked vnode reference.
    106  */
    107 static int
    108 getcwd_scandir(lvpp, uvpp, bpp, bufp, p)
    109 	struct vnode **lvpp;
    110 	struct vnode **uvpp;
    111 	char **bpp;
    112 	char *bufp;
    113 	struct proc *p;
    114 {
    115 	int     error = 0;
    116 	int     eofflag;
    117 	off_t   off;
    118 	int     tries;
    119 	struct uio uio;
    120 	struct iovec iov;
    121 	char   *dirbuf = NULL;
    122 	int	dirbuflen;
    123 	ino_t   fileno;
    124 	struct vattr va;
    125 	struct vnode *uvp = NULL;
    126 	struct vnode *lvp = *lvpp;
    127 	struct componentname cn;
    128 	int len, reclen;
    129 	tries = 0;
    130 
    131 	/*
    132 	 * If we want the filename, get some info we need while the
    133 	 * current directory is still locked.
    134 	 */
    135 	if (bufp != NULL) {
    136 		error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
    137 		if (error) {
    138 			vput(lvp);
    139 			*lvpp = NULL;
    140 			*uvpp = NULL;
    141 			return error;
    142 		}
    143 	}
    144 
    145 	/*
    146 	 * Ok, we have to do it the hard way..
    147 	 * Next, get parent vnode using lookup of ..
    148 	 */
    149 	cn.cn_nameiop = LOOKUP;
    150 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
    151 	cn.cn_proc = p;
    152 	cn.cn_cred = p->p_ucred;
    153 	cn.cn_pnbuf = NULL;
    154 	cn.cn_nameptr = "..";
    155 	cn.cn_namelen = 2;
    156 	cn.cn_hash = 0;
    157 	cn.cn_consume = 0;
    158 
    159 	/*
    160 	 * At this point, lvp is locked and will be unlocked by the lookup.
    161 	 * On successful return, *uvpp will be locked
    162 	 */
    163 	error = VOP_LOOKUP(lvp, uvpp, &cn);
    164 	if (error) {
    165 		vput(lvp);
    166 		*lvpp = NULL;
    167 		*uvpp = NULL;
    168 		return error;
    169 	}
    170 	uvp = *uvpp;
    171 
    172 	/* If we don't care about the pathname, we're done */
    173 	if (bufp == NULL) {
    174 		vrele(lvp);
    175 		*lvpp = NULL;
    176 		return 0;
    177 	}
    178 
    179 	fileno = va.va_fileid;
    180 
    181 	dirbuflen = DIRBLKSIZ;
    182 	if (dirbuflen < va.va_blocksize)
    183 		dirbuflen = va.va_blocksize;
    184 	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
    185 
    186 #if 0
    187 unionread:
    188 #endif
    189 	off = 0;
    190 	do {
    191 		/* call VOP_READDIR of parent */
    192 		iov.iov_base = dirbuf;
    193 		iov.iov_len = dirbuflen;
    194 
    195 		uio.uio_iov = &iov;
    196 		uio.uio_iovcnt = 1;
    197 		uio.uio_offset = off;
    198 		uio.uio_resid = dirbuflen;
    199 		uio.uio_segflg = UIO_SYSSPACE;
    200 		uio.uio_rw = UIO_READ;
    201 		uio.uio_procp = p;
    202 
    203 		eofflag = 0;
    204 
    205 		error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
    206 
    207 		off = uio.uio_offset;
    208 
    209 		/*
    210 		 * Try again if NFS tosses its cookies.
    211 		 * XXX this can still loop forever if the directory is busted
    212 		 * such that the second or subsequent page of it always
    213 		 * returns EINVAL
    214 		 */
    215 		if ((error == EINVAL) && (tries < 3)) {
    216 			off = 0;
    217 			tries++;
    218 			continue;	/* once more, with feeling */
    219 		}
    220 
    221 		if (!error) {
    222 			char   *cpos;
    223 			struct dirent *dp;
    224 
    225 			cpos = dirbuf;
    226 			tries = 0;
    227 
    228 			/* scan directory page looking for matching vnode */
    229 			for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
    230 				dp = (struct dirent *) cpos;
    231 				reclen = dp->d_reclen;
    232 
    233 				/* check for malformed directory.. */
    234 				if (reclen < DIRENT_MINSIZE) {
    235 					error = EINVAL;
    236 					goto out;
    237 				}
    238 				/*
    239 				 * XXX should perhaps do VOP_LOOKUP to
    240 				 * check that we got back to the right place,
    241 				 * but getting the locking games for that
    242 				 * right would be heinous.
    243 				 */
    244 				if ((dp->d_type != DT_WHT) &&
    245 				    (dp->d_fileno == fileno)) {
    246 					char *bp = *bpp;
    247 					bp -= dp->d_namlen;
    248 
    249 					if (bp <= bufp) {
    250 						error = ERANGE;
    251 						goto out;
    252 					}
    253 					memcpy(bp, dp->d_name, dp->d_namlen);
    254 					error = 0;
    255 					*bpp = bp;
    256 					goto out;
    257 				}
    258 				cpos += reclen;
    259 			}
    260 		} else
    261 			goto out;
    262 	} while (!eofflag);
    263 #if 0
    264 	/*
    265 	 * Deal with mount -o union, which unions only the
    266 	 * root directory of the mount.
    267 	 */
    268 	if ((uvp->v_flag & VROOT) &&
    269 	    (uvp->v_mount->mnt_flag & MNT_UNION)) {
    270 		struct vnode *tvp = uvp;
    271 		uvp = uvp->v_mount->mnt_vnodecovered;
    272 		vput(tvp);
    273 		VREF(uvp);
    274 		*uvpp = uvp;
    275 		error = vn_lock(uvp, LK_EXCLUSIVE | LK_RETRY);
    276 		if (error != 0) {
    277 			vrele(uvp);
    278 			*uvpp = uvp = NULL;
    279 			goto out;
    280 		}
    281 		goto unionread;
    282 	}
    283 #endif
    284 	error = ENOENT;
    285 
    286 out:
    287 	vrele(lvp);
    288 	*lvpp = NULL;
    289 	free(dirbuf, M_TEMP);
    290 	return error;
    291 }
    292 
    293 /*
    294  * Look in the vnode-to-name reverse cache to see if
    295  * we can find things the easy way.
    296  *
    297  * XXX vget failure path is untested.
    298  *
    299  * On entry, *lvpp is a locked vnode reference.
    300  * On exit, one of the following is the case:
    301  *	0) Both *lvpp and *uvpp are NULL and failure is returned.
    302  * 	1) *uvpp is NULL, *lvpp remains locked and -1 is returned (cache miss)
    303  *      2) *uvpp is a locked vnode reference, *lvpp is vput and NULL'ed
    304  *	   and 0 is returned (cache hit)
    305  */
    306 
    307 static int
    308 getcwd_getcache(lvpp, uvpp, bpp, bufp)
    309 	struct vnode **lvpp, **uvpp;
    310 	char **bpp;
    311 	char *bufp;
    312 {
    313 	struct vnode *lvp, *uvp = NULL;
    314 	int error;
    315 	int vpid;
    316 
    317 	lvp = *lvpp;
    318 
    319 	/*
    320 	 * This returns 0 on a cache hit, -1 on a clean cache miss,
    321 	 * or an errno on other failure.
    322 	 */
    323 	error = cache_revlookup(lvp, uvpp, bpp, bufp);
    324 	if (error) {
    325 		if (error != -1) {
    326 			vput(lvp);
    327 			*lvpp = NULL;
    328 			*uvpp = NULL;
    329 		}
    330 		return error;
    331 	}
    332 	uvp = *uvpp;
    333 	vpid = uvp->v_id;
    334 
    335 	/*
    336 	 * Since we're going up, we have to release the current lock
    337 	 * before we take the parent lock.
    338 	 */
    339 
    340 	VOP_UNLOCK(lvp, 0);
    341 
    342 	error = vget(uvp, LK_EXCLUSIVE | LK_RETRY);
    343 	if (error != 0)
    344 		*uvpp = NULL;
    345 	/*
    346 	 * Verify that vget succeeded, and check that vnode capability
    347 	 * didn't change while we were waiting for the lock.
    348 	 */
    349 	if (error || (vpid != uvp->v_id)) {
    350 		/*
    351 		 * Oops, we missed.  If the vget failed, or the
    352 		 * capability changed, try to get our lock back; if
    353 		 * that works, tell caller to try things the hard way,
    354 		 * otherwise give up.
    355 		 */
    356 		if (!error) vput(uvp);
    357 		*uvpp = NULL;
    358 
    359 		error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    360 
    361 		if (!error)
    362 			return -1;
    363 	}
    364 	vrele(lvp);
    365 	*lvpp = NULL;
    366 
    367 	return error;
    368 }
    369 
    370 /*
    371  * common routine shared by sys___getcwd() and vn_isunder()
    372  */
    373 
    374 #define GETCWD_CHECK_ACCESS 0x0001
    375 
    376 int
    377 getcwd_common (lvp, rvp, bpp, bufp, limit, flags, p)
    378 	struct vnode *lvp;
    379 	struct vnode *rvp;
    380 	char **bpp;
    381 	char *bufp;
    382 	int limit;
    383 	int flags;
    384 	struct proc *p;
    385 {
    386 	struct cwdinfo *cwdi = p->p_cwdi;
    387 	struct vnode *uvp = NULL;
    388 	char *bp = NULL;
    389 	int error;
    390 	int perms = VEXEC;
    391 
    392 	if (rvp == NULL) {
    393 		rvp = cwdi->cwdi_rdir;
    394 		if (rvp == NULL)
    395 			rvp = rootvnode;
    396 	}
    397 
    398 	VREF(rvp);
    399 	VREF(lvp);
    400 
    401 	/*
    402 	 * Error handling invariant:
    403 	 * Before a `goto out':
    404 	 *	lvp is either NULL, or locked and held.
    405 	 *	uvp is either NULL, or locked and held.
    406 	 */
    407 
    408 	error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    409 	if (error) {
    410 		vrele(lvp);
    411 		lvp = NULL;
    412 		goto out;
    413 	}
    414 	if (bufp)
    415 		bp = *bpp;
    416 	/*
    417 	 * this loop will terminate when one of the following happens:
    418 	 *	- we hit the root
    419 	 *	- getdirentries or lookup fails
    420 	 *	- we run out of space in the buffer.
    421 	 */
    422 	if (lvp == rvp) {
    423 		if (bp)
    424 			*(--bp) = '/';
    425 		goto out;
    426 	}
    427 	do {
    428 		if (lvp->v_type != VDIR) {
    429 			error = ENOTDIR;
    430 			goto out;
    431 		}
    432 
    433 		/*
    434 		 * access check here is optional, depending on
    435 		 * whether or not caller cares.
    436 		 */
    437 		if (flags & GETCWD_CHECK_ACCESS) {
    438 			error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
    439 			if (error)
    440 				goto out;
    441 			perms = VEXEC|VREAD;
    442 		}
    443 
    444 		/*
    445 		 * step up if we're a covered vnode..
    446 		 */
    447 		while (lvp->v_flag & VROOT) {
    448 			struct vnode *tvp;
    449 
    450 			if (lvp == rvp)
    451 				goto out;
    452 
    453 			tvp = lvp;
    454 			lvp = lvp->v_mount->mnt_vnodecovered;
    455 			vput(tvp);
    456 			/*
    457 			 * hodie natus est radici frater
    458 			 */
    459 			if (lvp == NULL) {
    460 				error = ENOENT;
    461 				goto out;
    462 			}
    463 			VREF(lvp);
    464 			error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    465 			if (error != 0) {
    466 				vrele(lvp);
    467 				lvp = NULL;
    468 				goto out;
    469 			}
    470 		}
    471 		/*
    472 		 * Look in the name cache; if that fails, look in the
    473 		 * directory..
    474 		 */
    475 		error = getcwd_getcache(&lvp, &uvp, &bp, bufp);
    476 		if (error == -1)
    477 			error = getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
    478 		if (error)
    479 			goto out;
    480 #if DIAGNOSTIC
    481 		if (lvp != NULL)
    482 			panic("getcwd: oops, forgot to null lvp");
    483 		if (bufp && (bp <= bufp)) {
    484 			panic("getcwd: oops, went back too far");
    485 		}
    486 #endif
    487 		if (bp)
    488 			*(--bp) = '/';
    489 		lvp = uvp;
    490 		uvp = NULL;
    491 		limit--;
    492 	} while ((lvp != rvp) && (limit > 0));
    493 
    494 out:
    495 	if (bpp)
    496 		*bpp = bp;
    497 	if (uvp)
    498 		vput(uvp);
    499 	if (lvp)
    500 		vput(lvp);
    501 	vrele(rvp);
    502 	return error;
    503 }
    504 
    505 /*
    506  * Check if one directory can be found inside another in the directory
    507  * hierarchy.
    508  *
    509  * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
    510  * chroot() actually means something.
    511  */
    512 int
    513 vn_isunder(lvp, rvp, p)
    514 	struct vnode *lvp;
    515 	struct vnode *rvp;
    516 	struct proc *p;
    517 {
    518 	int error;
    519 
    520 	error = getcwd_common (lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
    521 
    522 	if (!error)
    523 		return 1;
    524 	else
    525 		return 0;
    526 }
    527 
    528 /*
    529  * Returns true if proc p1's root directory equal to or under p2's
    530  * root directory.
    531  *
    532  * Intended to be used from ptrace/procfs sorts of things.
    533  */
    534 
    535 int
    536 proc_isunder (p1, p2)
    537 	struct proc *p1;
    538 	struct proc *p2;
    539 {
    540 	struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
    541 	struct vnode *r2 = p2->p_cwdi->cwdi_rdir;
    542 
    543 	if (r1 == NULL)
    544 		return (r2 == NULL);
    545 	else if (r2 == NULL)
    546 		return 1;
    547 	else
    548 		return vn_isunder(r1, r2, p2);
    549 }
    550 
    551 /*
    552  * Find pathname of process's current directory.
    553  *
    554  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    555  * to reading directory contents.
    556  */
    557 
    558 int
    559 sys___getcwd(l, v, retval)
    560 	struct lwp *l;
    561 	void   *v;
    562 	register_t *retval;
    563 {
    564 	struct sys___getcwd_args /* {
    565 		syscallarg(char *) bufp;
    566 		syscallarg(size_t) length;
    567 	} */ *uap = v;
    568 
    569 	int     error;
    570 	char   *path;
    571 	char   *bp, *bend;
    572 	int     len = SCARG(uap, length);
    573 	int	lenused;
    574 
    575 	if (len > MAXPATHLEN*4)
    576 		len = MAXPATHLEN*4;
    577 	else if (len < 2)
    578 		return ERANGE;
    579 
    580 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
    581 	if (!path)
    582 		return ENOMEM;
    583 
    584 	bp = &path[len];
    585 	bend = bp;
    586 	*(--bp) = '\0';
    587 
    588 	/*
    589 	 * 5th argument here is "max number of vnodes to traverse".
    590 	 * Since each entry takes up at least 2 bytes in the output buffer,
    591 	 * limit it to N/2 vnodes for an N byte buffer.
    592 	 */
    593 	error = getcwd_common (l->l_proc->p_cwdi->cwdi_cdir, NULL, &bp, path,
    594 	    len/2, GETCWD_CHECK_ACCESS, l->l_proc);
    595 
    596 	if (error)
    597 		goto out;
    598 	lenused = bend - bp;
    599 	*retval = lenused;
    600 	/* put the result into user buffer */
    601 	error = copyout(bp, SCARG(uap, bufp), lenused);
    602 
    603 out:
    604 	free(path, M_TEMP);
    605 	return error;
    606 }
    607