Home | History | Annotate | Line # | Download | only in kern
vfs_getcwd.c revision 1.52.4.2
      1  1.52.4.1    martin /* $NetBSD: vfs_getcwd.c,v 1.52.4.2 2020/04/13 08:05:04 martin Exp $ */
      2       1.1  sommerfe 
      3       1.1  sommerfe /*-
      4  1.52.4.1    martin  * Copyright (c) 1999, 2020 The NetBSD Foundation, Inc.
      5       1.1  sommerfe  * All rights reserved.
      6       1.1  sommerfe  *
      7       1.1  sommerfe  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  sommerfe  * by Bill Sommerfeld.
      9       1.1  sommerfe  *
     10       1.1  sommerfe  * Redistribution and use in source and binary forms, with or without
     11       1.1  sommerfe  * modification, are permitted provided that the following conditions
     12       1.1  sommerfe  * are met:
     13       1.1  sommerfe  * 1. Redistributions of source code must retain the above copyright
     14       1.1  sommerfe  *    notice, this list of conditions and the following disclaimer.
     15       1.1  sommerfe  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  sommerfe  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  sommerfe  *    documentation and/or other materials provided with the distribution.
     18       1.1  sommerfe  *
     19       1.1  sommerfe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  sommerfe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  sommerfe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  sommerfe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  sommerfe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  sommerfe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  sommerfe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  sommerfe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  sommerfe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  sommerfe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  sommerfe  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  sommerfe  */
     31      1.15     lukem 
     32      1.15     lukem #include <sys/cdefs.h>
     33  1.52.4.1    martin __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.52.4.2 2020/04/13 08:05:04 martin Exp $");
     34       1.1  sommerfe 
     35       1.1  sommerfe #include <sys/param.h>
     36       1.1  sommerfe #include <sys/systm.h>
     37       1.1  sommerfe #include <sys/namei.h>
     38       1.1  sommerfe #include <sys/filedesc.h>
     39       1.1  sommerfe #include <sys/kernel.h>
     40       1.1  sommerfe #include <sys/file.h>
     41       1.1  sommerfe #include <sys/stat.h>
     42       1.1  sommerfe #include <sys/vnode.h>
     43       1.1  sommerfe #include <sys/mount.h>
     44       1.1  sommerfe #include <sys/proc.h>
     45       1.1  sommerfe #include <sys/uio.h>
     46      1.43      yamt #include <sys/kmem.h>
     47       1.1  sommerfe #include <sys/dirent.h>
     48      1.31      elad #include <sys/kauth.h>
     49      1.31      elad 
     50       1.1  sommerfe #include <ufs/ufs/dir.h>	/* XXX only for DIRBLKSIZ */
     51       1.1  sommerfe 
     52       1.1  sommerfe #include <sys/syscallargs.h>
     53       1.1  sommerfe 
     54       1.1  sommerfe /*
     55       1.9  sommerfe  * Vnode variable naming conventions in this file:
     56       1.9  sommerfe  *
     57       1.9  sommerfe  * rvp: the current root we're aiming towards.
     58       1.9  sommerfe  * lvp, *lvpp: the "lower" vnode
     59       1.9  sommerfe  * uvp, *uvpp: the "upper" vnode.
     60       1.9  sommerfe  *
     61       1.9  sommerfe  * Since all the vnodes we're dealing with are directories, and the
     62       1.9  sommerfe  * lookups are going *up* in the filesystem rather than *down*, the
     63       1.9  sommerfe  * usual "pvp" (parent) or "dvp" (directory) naming conventions are
     64       1.9  sommerfe  * too confusing.
     65       1.9  sommerfe  */
     66       1.9  sommerfe 
     67       1.9  sommerfe /*
     68       1.1  sommerfe  * XXX Will infinite loop in certain cases if a directory read reliably
     69       1.1  sommerfe  *	returns EINVAL on last block.
     70       1.1  sommerfe  * XXX is EINVAL the right thing to return if a directory is malformed?
     71       1.1  sommerfe  */
     72       1.1  sommerfe 
     73       1.1  sommerfe /*
     74       1.9  sommerfe  * XXX Untested vs. mount -o union; probably does the wrong thing.
     75       1.9  sommerfe  */
     76       1.9  sommerfe 
     77       1.9  sommerfe /*
     78       1.9  sommerfe  * Find parent vnode of *lvpp, return in *uvpp
     79       1.9  sommerfe  *
     80       1.9  sommerfe  * If we care about the name, scan it looking for name of directory
     81       1.9  sommerfe  * entry pointing at lvp.
     82       1.1  sommerfe  *
     83       1.1  sommerfe  * Place the name in the buffer which starts at bufp, immediately
     84       1.1  sommerfe  * before *bpp, and move bpp backwards to point at the start of it.
     85       1.8  sommerfe  *
     86       1.9  sommerfe  * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
     87       1.9  sommerfe  * On exit, *uvpp is either NULL or is a locked vnode reference.
     88       1.1  sommerfe  */
     89       1.1  sommerfe static int
     90  1.52.4.1    martin getcwd_scandir(struct vnode *lvp, struct vnode **uvpp, char **bpp,
     91      1.29  christos     char *bufp, struct lwp *l)
     92       1.1  sommerfe {
     93       1.1  sommerfe 	int     error = 0;
     94       1.1  sommerfe 	int     eofflag;
     95       1.1  sommerfe 	off_t   off;
     96       1.1  sommerfe 	int     tries;
     97       1.1  sommerfe 	struct uio uio;
     98       1.1  sommerfe 	struct iovec iov;
     99       1.1  sommerfe 	char   *dirbuf = NULL;
    100       1.1  sommerfe 	int	dirbuflen;
    101       1.1  sommerfe 	ino_t   fileno;
    102       1.1  sommerfe 	struct vattr va;
    103       1.9  sommerfe 	struct vnode *uvp = NULL;
    104      1.32        ad 	kauth_cred_t cred = l->l_cred;
    105       1.1  sommerfe 	struct componentname cn;
    106       1.1  sommerfe 	int len, reclen;
    107       1.1  sommerfe 	tries = 0;
    108       1.1  sommerfe 
    109  1.52.4.1    martin 	/* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
    110  1.52.4.1    martin 	KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
    111  1.52.4.1    martin 
    112       1.1  sommerfe 	/*
    113       1.8  sommerfe 	 * If we want the filename, get some info we need while the
    114       1.8  sommerfe 	 * current directory is still locked.
    115       1.8  sommerfe 	 */
    116       1.8  sommerfe 	if (bufp != NULL) {
    117      1.38     pooka 		error = VOP_GETATTR(lvp, &va, cred);
    118       1.8  sommerfe 		if (error) {
    119  1.52.4.1    martin 			VOP_UNLOCK(lvp);
    120       1.9  sommerfe 			*uvpp = NULL;
    121       1.8  sommerfe 			return error;
    122       1.8  sommerfe 		}
    123       1.8  sommerfe 	}
    124       1.8  sommerfe 
    125       1.8  sommerfe 	/*
    126       1.1  sommerfe 	 * Ok, we have to do it the hard way..
    127       1.8  sommerfe 	 * Next, get parent vnode using lookup of ..
    128       1.1  sommerfe 	 */
    129       1.1  sommerfe 	cn.cn_nameiop = LOOKUP;
    130       1.8  sommerfe 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
    131      1.31      elad 	cn.cn_cred = cred;
    132       1.1  sommerfe 	cn.cn_nameptr = "..";
    133       1.1  sommerfe 	cn.cn_namelen = 2;
    134       1.1  sommerfe 	cn.cn_consume = 0;
    135      1.24     enami 
    136      1.50   hannken 	/* At this point, lvp is locked  */
    137       1.9  sommerfe 	error = VOP_LOOKUP(lvp, uvpp, &cn);
    138  1.52.4.1    martin 	VOP_UNLOCK(lvp);
    139       1.1  sommerfe 	if (error) {
    140       1.9  sommerfe 		*uvpp = NULL;
    141       1.1  sommerfe 		return error;
    142       1.1  sommerfe 	}
    143       1.9  sommerfe 	uvp = *uvpp;
    144       1.1  sommerfe 	/* If we don't care about the pathname, we're done */
    145       1.8  sommerfe 	if (bufp == NULL) {
    146       1.1  sommerfe 		return 0;
    147       1.8  sommerfe 	}
    148      1.24     enami 
    149       1.1  sommerfe 	fileno = va.va_fileid;
    150       1.1  sommerfe 
    151      1.49  dholland 	/* I guess UFS_DIRBLKSIZ is a good guess at a good size to use? */
    152      1.49  dholland 	dirbuflen = UFS_DIRBLKSIZ;
    153       1.1  sommerfe 	if (dirbuflen < va.va_blocksize)
    154       1.1  sommerfe 		dirbuflen = va.va_blocksize;
    155      1.43      yamt 	dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
    156       1.1  sommerfe 
    157  1.52.4.1    martin 	/* Now lvp is unlocked, try to lock uvp */
    158  1.52.4.1    martin 	error = vn_lock(uvp, LK_SHARED);
    159  1.52.4.1    martin 	if (error) {
    160  1.52.4.1    martin 		vrele(uvp);
    161  1.52.4.1    martin 		*uvpp = NULL;
    162  1.52.4.1    martin 		return error;
    163  1.52.4.1    martin 	}
    164  1.52.4.1    martin 
    165       1.1  sommerfe #if 0
    166       1.1  sommerfe unionread:
    167       1.1  sommerfe #endif
    168       1.1  sommerfe 	off = 0;
    169       1.1  sommerfe 	do {
    170       1.1  sommerfe 		/* call VOP_READDIR of parent */
    171       1.1  sommerfe 		iov.iov_base = dirbuf;
    172       1.1  sommerfe 		iov.iov_len = dirbuflen;
    173       1.1  sommerfe 
    174       1.1  sommerfe 		uio.uio_iov = &iov;
    175       1.1  sommerfe 		uio.uio_iovcnt = 1;
    176       1.1  sommerfe 		uio.uio_offset = off;
    177       1.1  sommerfe 		uio.uio_resid = dirbuflen;
    178       1.1  sommerfe 		uio.uio_rw = UIO_READ;
    179      1.30      yamt 		UIO_SETUP_SYSSPACE(&uio);
    180       1.1  sommerfe 
    181       1.1  sommerfe 		eofflag = 0;
    182       1.1  sommerfe 
    183      1.31      elad 		error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
    184       1.1  sommerfe 
    185       1.1  sommerfe 		off = uio.uio_offset;
    186       1.1  sommerfe 
    187       1.1  sommerfe 		/*
    188       1.1  sommerfe 		 * Try again if NFS tosses its cookies.
    189       1.1  sommerfe 		 * XXX this can still loop forever if the directory is busted
    190       1.1  sommerfe 		 * such that the second or subsequent page of it always
    191       1.1  sommerfe 		 * returns EINVAL
    192       1.1  sommerfe 		 */
    193       1.1  sommerfe 		if ((error == EINVAL) && (tries < 3)) {
    194       1.1  sommerfe 			off = 0;
    195       1.1  sommerfe 			tries++;
    196       1.1  sommerfe 			continue;	/* once more, with feeling */
    197       1.1  sommerfe 		}
    198       1.2   nathanw 
    199       1.1  sommerfe 		if (!error) {
    200       1.1  sommerfe 			char   *cpos;
    201       1.1  sommerfe 			struct dirent *dp;
    202      1.24     enami 
    203       1.1  sommerfe 			cpos = dirbuf;
    204       1.1  sommerfe 			tries = 0;
    205      1.24     enami 
    206      1.24     enami 			/* scan directory page looking for matching vnode */
    207      1.24     enami 			for (len = (dirbuflen - uio.uio_resid); len > 0;
    208      1.24     enami 			    len -= reclen) {
    209       1.1  sommerfe 				dp = (struct dirent *) cpos;
    210       1.1  sommerfe 				reclen = dp->d_reclen;
    211       1.1  sommerfe 
    212       1.1  sommerfe 				/* check for malformed directory.. */
    213      1.52  riastrad 				if (reclen < _DIRENT_MINSIZE(dp) ||
    214      1.52  riastrad 				    reclen > len) {
    215       1.1  sommerfe 					error = EINVAL;
    216       1.1  sommerfe 					goto out;
    217       1.1  sommerfe 				}
    218       1.1  sommerfe 				/*
    219       1.1  sommerfe 				 * XXX should perhaps do VOP_LOOKUP to
    220       1.1  sommerfe 				 * check that we got back to the right place,
    221       1.1  sommerfe 				 * but getting the locking games for that
    222       1.1  sommerfe 				 * right would be heinous.
    223       1.1  sommerfe 				 */
    224       1.1  sommerfe 				if ((dp->d_type != DT_WHT) &&
    225       1.1  sommerfe 				    (dp->d_fileno == fileno)) {
    226       1.1  sommerfe 					char *bp = *bpp;
    227      1.24     enami 
    228       1.1  sommerfe 					bp -= dp->d_namlen;
    229       1.1  sommerfe 					if (bp <= bufp) {
    230       1.1  sommerfe 						error = ERANGE;
    231       1.1  sommerfe 						goto out;
    232       1.1  sommerfe 					}
    233       1.1  sommerfe 					memcpy(bp, dp->d_name, dp->d_namlen);
    234       1.1  sommerfe 					error = 0;
    235       1.1  sommerfe 					*bpp = bp;
    236       1.1  sommerfe 					goto out;
    237       1.1  sommerfe 				}
    238       1.1  sommerfe 				cpos += reclen;
    239       1.1  sommerfe 			}
    240      1.14      fvdl 		} else
    241      1.14      fvdl 			goto out;
    242       1.2   nathanw 	} while (!eofflag);
    243       1.1  sommerfe #if 0
    244       1.1  sommerfe 	/*
    245       1.1  sommerfe 	 * Deal with mount -o union, which unions only the
    246       1.1  sommerfe 	 * root directory of the mount.
    247       1.1  sommerfe 	 */
    248      1.37        ad 	if ((uvp->v_vflag & VV_ROOT) &&
    249       1.9  sommerfe 	    (uvp->v_mount->mnt_flag & MNT_UNION)) {
    250       1.9  sommerfe 		struct vnode *tvp = uvp;
    251      1.24     enami 
    252       1.9  sommerfe 		uvp = uvp->v_mount->mnt_vnodecovered;
    253       1.1  sommerfe 		vput(tvp);
    254      1.44     pooka 		vref(uvp);
    255       1.9  sommerfe 		*uvpp = uvp;
    256  1.52.4.1    martin 		vn_lock(uvp, LK_SHARED | LK_RETRY);
    257       1.1  sommerfe 		goto unionread;
    258       1.1  sommerfe 	}
    259      1.24     enami #endif
    260       1.1  sommerfe 	error = ENOENT;
    261      1.24     enami 
    262       1.1  sommerfe out:
    263  1.52.4.1    martin 	VOP_UNLOCK(uvp);
    264      1.43      yamt 	kmem_free(dirbuf, dirbuflen);
    265       1.1  sommerfe 	return error;
    266       1.1  sommerfe }
    267       1.1  sommerfe 
    268       1.1  sommerfe /*
    269       1.1  sommerfe  * common routine shared by sys___getcwd() and vn_isunder()
    270       1.1  sommerfe  */
    271      1.24     enami int
    272      1.27   thorpej getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
    273      1.29  christos     int limit, int flags, struct lwp *l)
    274       1.1  sommerfe {
    275      1.29  christos 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    276      1.32        ad 	kauth_cred_t cred = l->l_cred;
    277       1.9  sommerfe 	struct vnode *uvp = NULL;
    278       1.4  sommerfe 	char *bp = NULL;
    279       1.1  sommerfe 	int error;
    280      1.10  sommerfe 	int perms = VEXEC;
    281      1.10  sommerfe 
    282      1.34       chs 	error = 0;
    283       1.1  sommerfe 	if (rvp == NULL) {
    284       1.6   thorpej 		rvp = cwdi->cwdi_rdir;
    285       1.1  sommerfe 		if (rvp == NULL)
    286       1.1  sommerfe 			rvp = rootvnode;
    287       1.1  sommerfe 	}
    288      1.24     enami 
    289      1.44     pooka 	vref(rvp);
    290      1.44     pooka 	vref(lvp);
    291       1.1  sommerfe 
    292       1.1  sommerfe 	/*
    293       1.1  sommerfe 	 * Error handling invariant:
    294       1.1  sommerfe 	 * Before a `goto out':
    295  1.52.4.1    martin 	 *	lvp is either NULL, or held.
    296  1.52.4.1    martin 	 *	uvp is either NULL, or held.
    297       1.1  sommerfe 	 */
    298       1.1  sommerfe 
    299       1.1  sommerfe 	if (bufp)
    300       1.1  sommerfe 		bp = *bpp;
    301      1.34       chs 
    302       1.1  sommerfe 	/*
    303       1.1  sommerfe 	 * this loop will terminate when one of the following happens:
    304       1.1  sommerfe 	 *	- we hit the root
    305       1.1  sommerfe 	 *	- getdirentries or lookup fails
    306       1.1  sommerfe 	 *	- we run out of space in the buffer.
    307       1.1  sommerfe 	 */
    308       1.9  sommerfe 	if (lvp == rvp) {
    309       1.8  sommerfe 		if (bp)
    310       1.8  sommerfe 			*(--bp) = '/';
    311       1.1  sommerfe 		goto out;
    312       1.1  sommerfe 	}
    313       1.1  sommerfe 	do {
    314       1.1  sommerfe 		/*
    315       1.1  sommerfe 		 * access check here is optional, depending on
    316       1.1  sommerfe 		 * whether or not caller cares.
    317       1.1  sommerfe 		 */
    318  1.52.4.1    martin 		int chkaccess = (flags & GETCWD_CHECK_ACCESS);
    319  1.52.4.1    martin 		bool locked = false;
    320      1.24     enami 
    321       1.1  sommerfe 		/*
    322       1.1  sommerfe 		 * step up if we're a covered vnode..
    323  1.52.4.1    martin 		 * check access on the first vnode only.
    324       1.1  sommerfe 		 */
    325  1.52.4.1    martin 		if (lvp->v_vflag & VV_ROOT) {
    326  1.52.4.1    martin 			vn_lock(lvp, LK_SHARED | LK_RETRY);
    327  1.52.4.1    martin 			if (chkaccess) {
    328  1.52.4.1    martin 				error = VOP_ACCESS(lvp, perms, cred);
    329  1.52.4.1    martin 				if (error) {
    330  1.52.4.1    martin 					VOP_UNLOCK(lvp);
    331  1.52.4.1    martin 					goto out;
    332  1.52.4.1    martin 				}
    333  1.52.4.1    martin 				chkaccess = 0;
    334  1.52.4.1    martin 			}
    335  1.52.4.1    martin 			while (lvp->v_vflag & VV_ROOT) {
    336  1.52.4.1    martin 				struct vnode *tvp;
    337       1.1  sommerfe 
    338  1.52.4.1    martin 				if (lvp == rvp) {
    339  1.52.4.1    martin 					VOP_UNLOCK(lvp);
    340  1.52.4.1    martin 					goto out;
    341  1.52.4.1    martin 				}
    342      1.24     enami 
    343  1.52.4.1    martin 				tvp = lvp->v_mount->mnt_vnodecovered;
    344  1.52.4.1    martin 				/*
    345  1.52.4.1    martin 				 * hodie natus est radici frater
    346  1.52.4.1    martin 				 */
    347  1.52.4.1    martin 				if (tvp == NULL) {
    348  1.52.4.1    martin 					VOP_UNLOCK(lvp);
    349  1.52.4.1    martin 					error = ENOENT;
    350  1.52.4.1    martin 					goto out;
    351  1.52.4.1    martin 				}
    352  1.52.4.1    martin 				vref(tvp);
    353  1.52.4.1    martin 				vput(lvp);
    354  1.52.4.1    martin 				lvp = tvp;
    355  1.52.4.1    martin 				if (lvp->v_vflag & VV_ROOT)
    356  1.52.4.1    martin 					vn_lock(lvp, LK_SHARED | LK_RETRY);
    357       1.1  sommerfe 			}
    358  1.52.4.1    martin 		}
    359  1.52.4.1    martin 
    360  1.52.4.1    martin 		/* Do we need to check access to the directory? */
    361  1.52.4.1    martin 		if (chkaccess && !cache_have_id(lvp)) {
    362  1.52.4.1    martin 			/* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
    363  1.52.4.1    martin 			vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    364  1.52.4.1    martin 			error = VOP_ACCESS(lvp, perms, cred);
    365  1.52.4.1    martin 			if (error) {
    366  1.52.4.1    martin 				VOP_UNLOCK(lvp);
    367       1.1  sommerfe 				goto out;
    368       1.1  sommerfe 			}
    369  1.52.4.1    martin 			chkaccess = 0;
    370  1.52.4.1    martin 			locked = true;
    371       1.1  sommerfe 		}
    372  1.52.4.1    martin 
    373       1.1  sommerfe 		/*
    374       1.1  sommerfe 		 * Look in the name cache; if that fails, look in the
    375       1.1  sommerfe 		 * directory..
    376       1.1  sommerfe 		 */
    377  1.52.4.1    martin 		error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
    378  1.52.4.1    martin 		    perms);
    379      1.33  christos 		if (error == -1) {
    380  1.52.4.1    martin 			if (!locked) {
    381  1.52.4.1    martin 				locked = true;
    382  1.52.4.1    martin 				vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    383  1.52.4.1    martin 			}
    384      1.33  christos 			if (lvp->v_type != VDIR) {
    385  1.52.4.1    martin 				VOP_UNLOCK(lvp);
    386      1.33  christos 				error = ENOTDIR;
    387      1.33  christos 				goto out;
    388      1.33  christos 			}
    389  1.52.4.1    martin 			error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
    390  1.52.4.1    martin 			/* lvp now unlocked */
    391  1.52.4.1    martin 		} else if (locked) {
    392  1.52.4.1    martin 			VOP_UNLOCK(lvp);
    393      1.33  christos 		}
    394       1.1  sommerfe 		if (error)
    395       1.1  sommerfe 			goto out;
    396      1.24     enami #if DIAGNOSTIC
    397       1.1  sommerfe 		if (bufp && (bp <= bufp)) {
    398       1.1  sommerfe 			panic("getcwd: oops, went back too far");
    399       1.1  sommerfe 		}
    400      1.24     enami #endif
    401  1.52.4.1    martin 		perms = VEXEC | VREAD;
    402      1.24     enami 		if (bp)
    403       1.8  sommerfe 			*(--bp) = '/';
    404  1.52.4.1    martin 		vrele(lvp);
    405       1.9  sommerfe 		lvp = uvp;
    406       1.9  sommerfe 		uvp = NULL;
    407       1.1  sommerfe 		limit--;
    408      1.24     enami 	} while ((lvp != rvp) && (limit > 0));
    409       1.1  sommerfe 
    410       1.1  sommerfe out:
    411       1.8  sommerfe 	if (bpp)
    412       1.8  sommerfe 		*bpp = bp;
    413       1.9  sommerfe 	if (uvp)
    414  1.52.4.1    martin 		vrele(uvp);
    415       1.9  sommerfe 	if (lvp)
    416  1.52.4.1    martin 		vrele(lvp);
    417       1.1  sommerfe 	vrele(rvp);
    418       1.1  sommerfe 	return error;
    419       1.1  sommerfe }
    420       1.1  sommerfe 
    421       1.1  sommerfe /*
    422       1.1  sommerfe  * Check if one directory can be found inside another in the directory
    423       1.1  sommerfe  * hierarchy.
    424       1.1  sommerfe  *
    425       1.1  sommerfe  * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
    426       1.1  sommerfe  * chroot() actually means something.
    427       1.1  sommerfe  */
    428      1.11  jdolecek int
    429      1.29  christos vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
    430       1.1  sommerfe {
    431       1.1  sommerfe 	int error;
    432       1.1  sommerfe 
    433      1.29  christos 	error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
    434       1.1  sommerfe 
    435       1.1  sommerfe 	if (!error)
    436       1.1  sommerfe 		return 1;
    437       1.1  sommerfe 	else
    438       1.1  sommerfe 		return 0;
    439       1.1  sommerfe }
    440       1.3  sommerfe 
    441       1.3  sommerfe /*
    442       1.3  sommerfe  * Returns true if proc p1's root directory equal to or under p2's
    443       1.3  sommerfe  * root directory.
    444       1.3  sommerfe  *
    445       1.3  sommerfe  * Intended to be used from ptrace/procfs sorts of things.
    446       1.3  sommerfe  */
    447       1.3  sommerfe 
    448      1.11  jdolecek int
    449      1.29  christos proc_isunder(struct proc *p1, struct lwp *l2)
    450       1.3  sommerfe {
    451       1.6   thorpej 	struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
    452      1.29  christos 	struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
    453       1.6   thorpej 
    454       1.3  sommerfe 	if (r1 == NULL)
    455       1.3  sommerfe 		return (r2 == NULL);
    456       1.3  sommerfe 	else if (r2 == NULL)
    457       1.3  sommerfe 		return 1;
    458       1.3  sommerfe 	else
    459      1.29  christos 		return vn_isunder(r1, r2, l2);
    460       1.3  sommerfe }
    461       1.3  sommerfe 
    462       1.9  sommerfe /*
    463       1.9  sommerfe  * Find pathname of process's current directory.
    464       1.9  sommerfe  *
    465       1.9  sommerfe  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    466       1.9  sommerfe  * to reading directory contents.
    467       1.9  sommerfe  */
    468       1.9  sommerfe 
    469      1.11  jdolecek int
    470      1.41       dsl sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap, register_t *retval)
    471       1.1  sommerfe {
    472      1.41       dsl 	/* {
    473       1.1  sommerfe 		syscallarg(char *) bufp;
    474       1.1  sommerfe 		syscallarg(size_t) length;
    475      1.41       dsl 	} */
    476       1.1  sommerfe 
    477       1.1  sommerfe 	int     error;
    478       1.1  sommerfe 	char   *path;
    479       1.1  sommerfe 	char   *bp, *bend;
    480       1.1  sommerfe 	int     len = SCARG(uap, length);
    481       1.1  sommerfe 	int	lenused;
    482  1.52.4.1    martin 	struct	vnode *dvp;
    483       1.1  sommerfe 
    484      1.24     enami 	if (len > MAXPATHLEN * 4)
    485      1.24     enami 		len = MAXPATHLEN * 4;
    486       1.7  sommerfe 	else if (len < 2)
    487       1.1  sommerfe 		return ERANGE;
    488       1.1  sommerfe 
    489      1.43      yamt 	path = kmem_alloc(len, KM_SLEEP);
    490       1.1  sommerfe 	bp = &path[len];
    491       1.1  sommerfe 	bend = bp;
    492       1.1  sommerfe 	*(--bp) = '\0';
    493       1.1  sommerfe 
    494       1.8  sommerfe 	/*
    495       1.8  sommerfe 	 * 5th argument here is "max number of vnodes to traverse".
    496       1.8  sommerfe 	 * Since each entry takes up at least 2 bytes in the output buffer,
    497       1.8  sommerfe 	 * limit it to N/2 vnodes for an N byte buffer.
    498       1.8  sommerfe 	 */
    499  1.52.4.1    martin 	dvp = cwdcdir();
    500  1.52.4.1    martin 	error = getcwd_common(dvp, NULL, &bp, path,
    501      1.29  christos 	    len/2, GETCWD_CHECK_ACCESS, l);
    502  1.52.4.1    martin 	vrele(dvp);
    503       1.1  sommerfe 
    504       1.1  sommerfe 	if (error)
    505       1.1  sommerfe 		goto out;
    506       1.1  sommerfe 	lenused = bend - bp;
    507       1.1  sommerfe 	*retval = lenused;
    508       1.1  sommerfe 	/* put the result into user buffer */
    509       1.1  sommerfe 	error = copyout(bp, SCARG(uap, bufp), lenused);
    510       1.1  sommerfe 
    511       1.1  sommerfe out:
    512      1.43      yamt 	kmem_free(path, len);
    513       1.1  sommerfe 	return error;
    514       1.1  sommerfe }
    515      1.39  christos 
    516      1.39  christos /*
    517  1.52.4.1    martin  * Try to find a pathname for a vnode.  Since there is no mapping vnode ->
    518  1.52.4.1    martin  * parent directory, this needs the namecache to succeed.  Caller holds a
    519      1.46   hannken  * reference to the vnode.
    520      1.39  christos  */
    521      1.39  christos int
    522      1.39  christos vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
    523      1.39  christos     struct proc *p)
    524      1.39  christos {
    525      1.39  christos 	struct proc *curp = curl->l_proc;
    526      1.39  christos 	int error, lenused, elen;
    527      1.39  christos 	char *bp, *bend;
    528      1.39  christos 	struct vnode *dvp;
    529      1.39  christos 
    530      1.46   hannken 	KASSERT(vp->v_usecount > 0);
    531      1.46   hannken 
    532      1.39  christos 	bp = bend = &path[len];
    533      1.39  christos 	*(--bp) = '\0';
    534      1.39  christos 
    535  1.52.4.1    martin 	error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
    536      1.39  christos 	if (error != 0)
    537      1.39  christos 		return (error == -1 ? ENOENT : error);
    538      1.39  christos 
    539      1.39  christos 	*(--bp) = '/';
    540      1.46   hannken 	error = getcwd_common(dvp, NULL, &bp, path, len / 2,
    541      1.46   hannken 	    GETCWD_CHECK_ACCESS, curl);
    542      1.46   hannken 	vrele(dvp);
    543  1.52.4.2    martin 	if (error != 0)
    544  1.52.4.2    martin 		return error;
    545      1.39  christos 
    546      1.39  christos 	/*
    547      1.39  christos 	 * Strip off emulation path for emulated processes looking at
    548      1.39  christos 	 * the maps file of a process of the same emulation. (Won't
    549      1.39  christos 	 * work if /emul/xxx is a symlink..)
    550      1.39  christos 	 */
    551      1.39  christos 	if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
    552      1.39  christos 		elen = strlen(curp->p_emul->e_path);
    553      1.39  christos 		if (!strncmp(bp, curp->p_emul->e_path, elen))
    554      1.39  christos 			bp = &bp[elen];
    555      1.39  christos 	}
    556      1.39  christos 
    557      1.39  christos 	lenused = bend - bp;
    558      1.39  christos 
    559      1.39  christos 	memcpy(path, bp, lenused);
    560  1.52.4.2    martin 	path[lenused] = '\0';
    561      1.39  christos 
    562      1.39  christos 	return 0;
    563      1.39  christos }
    564