Home | History | Annotate | Line # | Download | only in kern
vfs_getcwd.c revision 1.20.2.2
      1 /* $NetBSD: vfs_getcwd.c,v 1.20.2.2 2004/08/03 10:52:59 skrll 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.20.2.2 2004/08/03 10:52:59 skrll 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(struct vnode **, struct vnode **,
     62     char **, char *, struct lwp *);
     63 static int
     64 getcwd_getcache(struct vnode **, struct vnode **,
     65     char **, char *, struct lwp *);
     66 
     67 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN + 1) + 4)
     68 
     69 /*
     70  * Vnode variable naming conventions in this file:
     71  *
     72  * rvp: the current root we're aiming towards.
     73  * lvp, *lvpp: the "lower" vnode
     74  * uvp, *uvpp: the "upper" vnode.
     75  *
     76  * Since all the vnodes we're dealing with are directories, and the
     77  * lookups are going *up* in the filesystem rather than *down*, the
     78  * usual "pvp" (parent) or "dvp" (directory) naming conventions are
     79  * too confusing.
     80  */
     81 
     82 /*
     83  * XXX Will infinite loop in certain cases if a directory read reliably
     84  *	returns EINVAL on last block.
     85  * XXX is EINVAL the right thing to return if a directory is malformed?
     86  */
     87 
     88 /*
     89  * XXX Untested vs. mount -o union; probably does the wrong thing.
     90  */
     91 
     92 /*
     93  * Find parent vnode of *lvpp, return in *uvpp
     94  *
     95  * If we care about the name, scan it looking for name of directory
     96  * entry pointing at lvp.
     97  *
     98  * Place the name in the buffer which starts at bufp, immediately
     99  * before *bpp, and move bpp backwards to point at the start of it.
    100  *
    101  * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
    102  * On exit, *uvpp is either NULL or is a locked vnode reference.
    103  */
    104 static int
    105 getcwd_scandir(lvpp, uvpp, bpp, bufp, l)
    106 	struct vnode **lvpp;
    107 	struct vnode **uvpp;
    108 	char **bpp;
    109 	char *bufp;
    110 	struct lwp *l;
    111 {
    112 	int     error = 0;
    113 	int     eofflag;
    114 	off_t   off;
    115 	int     tries;
    116 	struct uio uio;
    117 	struct iovec iov;
    118 	char   *dirbuf = NULL;
    119 	int	dirbuflen;
    120 	ino_t   fileno;
    121 	struct vattr va;
    122 	struct vnode *uvp = NULL;
    123 	struct vnode *lvp = *lvpp;
    124 	struct ucred *ucred = l->l_proc->p_ucred;
    125 	struct componentname cn;
    126 	int len, reclen;
    127 	tries = 0;
    128 
    129 	/*
    130 	 * If we want the filename, get some info we need while the
    131 	 * current directory is still locked.
    132 	 */
    133 	if (bufp != NULL) {
    134 		error = VOP_GETATTR(lvp, &va, ucred, l);
    135 		if (error) {
    136 			vput(lvp);
    137 			*lvpp = NULL;
    138 			*uvpp = NULL;
    139 			return error;
    140 		}
    141 	}
    142 
    143 	/*
    144 	 * Ok, we have to do it the hard way..
    145 	 * Next, get parent vnode using lookup of ..
    146 	 */
    147 	cn.cn_nameiop = LOOKUP;
    148 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
    149 	cn.cn_lwp = l;
    150 	cn.cn_cred = ucred;
    151 	cn.cn_pnbuf = NULL;
    152 	cn.cn_nameptr = "..";
    153 	cn.cn_namelen = 2;
    154 	cn.cn_hash = 0;
    155 	cn.cn_consume = 0;
    156 
    157 	/*
    158 	 * At this point, lvp is locked and will be unlocked by the lookup.
    159 	 * On successful return, *uvpp will be locked
    160 	 */
    161 	error = VOP_LOOKUP(lvp, uvpp, &cn);
    162 	if (error) {
    163 		vput(lvp);
    164 		*lvpp = NULL;
    165 		*uvpp = NULL;
    166 		return error;
    167 	}
    168 	uvp = *uvpp;
    169 
    170 	/* If we don't care about the pathname, we're done */
    171 	if (bufp == NULL) {
    172 		vrele(lvp);
    173 		*lvpp = NULL;
    174 		return 0;
    175 	}
    176 
    177 	fileno = va.va_fileid;
    178 
    179 	dirbuflen = DIRBLKSIZ;
    180 	if (dirbuflen < va.va_blocksize)
    181 		dirbuflen = va.va_blocksize;
    182 	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
    183 
    184 #if 0
    185 unionread:
    186 #endif
    187 	off = 0;
    188 	do {
    189 		/* call VOP_READDIR of parent */
    190 		iov.iov_base = dirbuf;
    191 		iov.iov_len = dirbuflen;
    192 
    193 		uio.uio_iov = &iov;
    194 		uio.uio_iovcnt = 1;
    195 		uio.uio_offset = off;
    196 		uio.uio_resid = dirbuflen;
    197 		uio.uio_segflg = UIO_SYSSPACE;
    198 		uio.uio_rw = UIO_READ;
    199 		uio.uio_lwp = l;
    200 
    201 		eofflag = 0;
    202 
    203 		error = VOP_READDIR(uvp, &uio, ucred, &eofflag, 0, 0);
    204 
    205 		off = uio.uio_offset;
    206 
    207 		/*
    208 		 * Try again if NFS tosses its cookies.
    209 		 * XXX this can still loop forever if the directory is busted
    210 		 * such that the second or subsequent page of it always
    211 		 * returns EINVAL
    212 		 */
    213 		if ((error == EINVAL) && (tries < 3)) {
    214 			off = 0;
    215 			tries++;
    216 			continue;	/* once more, with feeling */
    217 		}
    218 
    219 		if (!error) {
    220 			char   *cpos;
    221 			struct dirent *dp;
    222 
    223 			cpos = dirbuf;
    224 			tries = 0;
    225 
    226 			/* scan directory page looking for matching vnode */
    227 			for (len = (dirbuflen - uio.uio_resid); len > 0;
    228 			    len -= reclen) {
    229 				dp = (struct dirent *) cpos;
    230 				reclen = dp->d_reclen;
    231 
    232 				/* check for malformed directory.. */
    233 				if (reclen < DIRENT_MINSIZE) {
    234 					error = EINVAL;
    235 					goto out;
    236 				}
    237 				/*
    238 				 * XXX should perhaps do VOP_LOOKUP to
    239 				 * check that we got back to the right place,
    240 				 * but getting the locking games for that
    241 				 * right would be heinous.
    242 				 */
    243 				if ((dp->d_type != DT_WHT) &&
    244 				    (dp->d_fileno == fileno)) {
    245 					char *bp = *bpp;
    246 
    247 					bp -= dp->d_namlen;
    248 					if (bp <= bufp) {
    249 						error = ERANGE;
    250 						goto out;
    251 					}
    252 					memcpy(bp, dp->d_name, dp->d_namlen);
    253 					error = 0;
    254 					*bpp = bp;
    255 					goto out;
    256 				}
    257 				cpos += reclen;
    258 			}
    259 		} else
    260 			goto out;
    261 	} while (!eofflag);
    262 #if 0
    263 	/*
    264 	 * Deal with mount -o union, which unions only the
    265 	 * root directory of the mount.
    266 	 */
    267 	if ((uvp->v_flag & VROOT) &&
    268 	    (uvp->v_mount->mnt_flag & MNT_UNION)) {
    269 		struct vnode *tvp = uvp;
    270 
    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, l)
    309 	struct vnode **lvpp, **uvpp;
    310 	char **bpp;
    311 	char *bufp;
    312 	struct lwp *l;
    313 {
    314 	struct vnode *lvp, *uvp = NULL;
    315 	char *obp = *bpp;
    316 	int error;
    317 
    318 	lvp = *lvpp;
    319 
    320 	/*
    321 	 * This returns 0 on a cache hit, -1 on a clean cache miss,
    322 	 * or an errno on other failure.
    323 	 */
    324 	error = cache_revlookup(lvp, uvpp, bpp, bufp);
    325 	if (error) {
    326 		if (error != -1) {
    327 			vput(lvp);
    328 			*lvpp = NULL;
    329 			*uvpp = NULL;
    330 		}
    331 		return error;
    332 	}
    333 	uvp = *uvpp;
    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, l);
    343 	/*
    344 	 * Verify that vget succeeded while we were waiting for the
    345 	 * lock.
    346 	 */
    347 	if (error) {
    348 		/*
    349 		 * Oops, we missed.  If the vget failed try to get our
    350 		 * lock back; if that works, rewind the `bp' and tell
    351 		 * caller to try things the hard way, otherwise give
    352 		 * up.
    353 		 */
    354 		*uvpp = NULL;
    355 		error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    356 		if (error == 0) {
    357 			*bpp = obp;
    358 			return -1;
    359 		}
    360 	}
    361 	vrele(lvp);
    362 	*lvpp = NULL;
    363 
    364 	return error;
    365 }
    366 
    367 /*
    368  * common routine shared by sys___getcwd() and vn_isunder()
    369  */
    370 
    371 int
    372 getcwd_common(lvp, rvp, bpp, bufp, limit, flags, l)
    373 	struct vnode *lvp;
    374 	struct vnode *rvp;
    375 	char **bpp;
    376 	char *bufp;
    377 	int limit;
    378 	int flags;
    379 	struct lwp *l;
    380 {
    381 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    382 	struct ucred *ucred = l->l_proc->p_ucred;
    383 	struct vnode *uvp = NULL;
    384 	char *bp = NULL;
    385 	int error;
    386 	int perms = VEXEC;
    387 
    388 	if (rvp == NULL) {
    389 		rvp = cwdi->cwdi_rdir;
    390 		if (rvp == NULL)
    391 			rvp = rootvnode;
    392 	}
    393 
    394 	VREF(rvp);
    395 	VREF(lvp);
    396 
    397 	/*
    398 	 * Error handling invariant:
    399 	 * Before a `goto out':
    400 	 *	lvp is either NULL, or locked and held.
    401 	 *	uvp is either NULL, or locked and held.
    402 	 */
    403 
    404 	error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    405 	if (error) {
    406 		vrele(lvp);
    407 		lvp = NULL;
    408 		goto out;
    409 	}
    410 	if (bufp)
    411 		bp = *bpp;
    412 	/*
    413 	 * this loop will terminate when one of the following happens:
    414 	 *	- we hit the root
    415 	 *	- getdirentries or lookup fails
    416 	 *	- we run out of space in the buffer.
    417 	 */
    418 	if (lvp == rvp) {
    419 		if (bp)
    420 			*(--bp) = '/';
    421 		goto out;
    422 	}
    423 	do {
    424 		if (lvp->v_type != VDIR) {
    425 			error = ENOTDIR;
    426 			goto out;
    427 		}
    428 
    429 		/*
    430 		 * access check here is optional, depending on
    431 		 * whether or not caller cares.
    432 		 */
    433 		if (flags & GETCWD_CHECK_ACCESS) {
    434 			error = VOP_ACCESS(lvp, perms, ucred, l);
    435 			if (error)
    436 				goto out;
    437 			perms = VEXEC|VREAD;
    438 		}
    439 
    440 		/*
    441 		 * step up if we're a covered vnode..
    442 		 */
    443 		while (lvp->v_flag & VROOT) {
    444 			struct vnode *tvp;
    445 
    446 			if (lvp == rvp)
    447 				goto out;
    448 
    449 			tvp = lvp;
    450 			lvp = lvp->v_mount->mnt_vnodecovered;
    451 			vput(tvp);
    452 			/*
    453 			 * hodie natus est radici frater
    454 			 */
    455 			if (lvp == NULL) {
    456 				error = ENOENT;
    457 				goto out;
    458 			}
    459 			VREF(lvp);
    460 			error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    461 			if (error != 0) {
    462 				vrele(lvp);
    463 				lvp = NULL;
    464 				goto out;
    465 			}
    466 		}
    467 		/*
    468 		 * Look in the name cache; if that fails, look in the
    469 		 * directory..
    470 		 */
    471 		error = getcwd_getcache(&lvp, &uvp, &bp, bufp, l);
    472 		if (error == -1)
    473 			error = getcwd_scandir(&lvp, &uvp, &bp, bufp, l);
    474 		if (error)
    475 			goto out;
    476 #if DIAGNOSTIC
    477 		if (lvp != NULL)
    478 			panic("getcwd: oops, forgot to null lvp");
    479 		if (bufp && (bp <= bufp)) {
    480 			panic("getcwd: oops, went back too far");
    481 		}
    482 #endif
    483 		if (bp)
    484 			*(--bp) = '/';
    485 		lvp = uvp;
    486 		uvp = NULL;
    487 		limit--;
    488 	} while ((lvp != rvp) && (limit > 0));
    489 
    490 out:
    491 	if (bpp)
    492 		*bpp = bp;
    493 	if (uvp)
    494 		vput(uvp);
    495 	if (lvp)
    496 		vput(lvp);
    497 	vrele(rvp);
    498 	return error;
    499 }
    500 
    501 /*
    502  * Check if one directory can be found inside another in the directory
    503  * hierarchy.
    504  *
    505  * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
    506  * chroot() actually means something.
    507  */
    508 int
    509 vn_isunder(lvp, rvp, l)
    510 	struct vnode *lvp;
    511 	struct vnode *rvp;
    512 	struct lwp *l;
    513 {
    514 	int error;
    515 
    516 	error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
    517 
    518 	if (!error)
    519 		return 1;
    520 	else
    521 		return 0;
    522 }
    523 
    524 /*
    525  * Returns true if proc p1's root directory equal to or under p2's
    526  * root directory.
    527  *
    528  * Intended to be used from ptrace/procfs sorts of things.
    529  */
    530 
    531 int
    532 proc_isunder(p1, l2)
    533 	struct proc *p1;
    534 	struct lwp *l2;
    535 {
    536 	struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
    537 	struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
    538 
    539 	if (r1 == NULL)
    540 		return (r2 == NULL);
    541 	else if (r2 == NULL)
    542 		return 1;
    543 	else
    544 		return vn_isunder(r1, r2, l2);
    545 }
    546 
    547 /*
    548  * Find pathname of process's current directory.
    549  *
    550  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    551  * to reading directory contents.
    552  */
    553 
    554 int
    555 sys___getcwd(l, v, retval)
    556 	struct lwp *l;
    557 	void   *v;
    558 	register_t *retval;
    559 {
    560 	struct sys___getcwd_args /* {
    561 		syscallarg(char *) bufp;
    562 		syscallarg(size_t) length;
    563 	} */ *uap = v;
    564 
    565 	int     error;
    566 	char   *path;
    567 	char   *bp, *bend;
    568 	int     len = SCARG(uap, length);
    569 	int	lenused;
    570 
    571 	if (len > MAXPATHLEN * 4)
    572 		len = MAXPATHLEN * 4;
    573 	else if (len < 2)
    574 		return ERANGE;
    575 
    576 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
    577 	if (!path)
    578 		return ENOMEM;
    579 
    580 	bp = &path[len];
    581 	bend = bp;
    582 	*(--bp) = '\0';
    583 
    584 	/*
    585 	 * 5th argument here is "max number of vnodes to traverse".
    586 	 * Since each entry takes up at least 2 bytes in the output buffer,
    587 	 * limit it to N/2 vnodes for an N byte buffer.
    588 	 */
    589 	error = getcwd_common(l->l_proc->p_cwdi->cwdi_cdir, NULL, &bp, path,
    590 	    len/2, GETCWD_CHECK_ACCESS, l);
    591 
    592 	if (error)
    593 		goto out;
    594 	lenused = bend - bp;
    595 	*retval = lenused;
    596 	/* put the result into user buffer */
    597 	error = copyout(bp, SCARG(uap, bufp), lenused);
    598 
    599 out:
    600 	free(path, M_TEMP);
    601 	return error;
    602 }
    603