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