1 /* $FreeBSD: src/sys/compat/linux/linux_getcwd.c,v 1.2.2.3 2001/11/05 19:08:22 marcel Exp $ */
2 /* $DragonFly: src/sys/emulation/linux/linux_getcwd.c,v 1.2 2003/06/17 04:28:19 dillon Exp $ */
3 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
4 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
7 * Copyright (c) 1999 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
41 #include "opt_compat.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/namei.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
59 #include <machine/../linux/linux.h>
60 #include <machine/../linux/linux_proto.h>
61 #include <compat/linux/linux_util.h>
64 linux_getcwd_scandir __P((struct vnode **, struct vnode **,
65 char **, char *, struct proc *));
67 linux_getcwd_common __P((struct vnode *, struct vnode *,
68 char **, char *, int, int, struct proc *));
70 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
73 * Vnode variable naming conventions in this file:
75 * rvp: the current root we're aiming towards.
76 * lvp, *lvpp: the "lower" vnode
77 * uvp, *uvpp: the "upper" vnode.
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
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?
92 * XXX Untested vs. mount -o union; probably does the wrong thing.
96 * Find parent vnode of *lvpp, return in *uvpp
98 * If we care about the name, scan it looking for name of directory
99 * entry pointing at lvp.
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.
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.
108 linux_getcwd_scandir(lvpp, uvpp, bpp, bufp, p)
125 struct vnode *uvp = NULL;
126 struct vnode *lvp = *lvpp;
127 struct componentname cn;
132 * If we want the filename, get some info we need while the
133 * current directory is still locked.
136 error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
146 * Ok, we have to do it the hard way..
147 * Next, get parent vnode using lookup of ..
149 cn.cn_nameiop = LOOKUP;
150 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
152 cn.cn_cred = p->p_ucred;
154 cn.cn_nameptr = "..";
159 * At this point, lvp is locked and will be unlocked by the lookup.
160 * On successful return, *uvpp will be locked
162 error = VOP_LOOKUP(lvp, uvpp, &cn);
171 /* If we don't care about the pathname, we're done */
178 fileno = va.va_fileid;
180 dirbuflen = DIRBLKSIZ;
181 if (dirbuflen < va.va_blocksize)
182 dirbuflen = va.va_blocksize;
183 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
190 /* call VOP_READDIR of parent */
191 iov.iov_base = dirbuf;
192 iov.iov_len = dirbuflen;
196 uio.uio_offset = off;
197 uio.uio_resid = dirbuflen;
198 uio.uio_segflg = UIO_SYSSPACE;
199 uio.uio_rw = UIO_READ;
204 error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
206 off = uio.uio_offset;
209 * Try again if NFS tosses its cookies.
210 * XXX this can still loop forever if the directory is busted
211 * such that the second or subsequent page of it always
214 if ((error == EINVAL) && (tries < 3)) {
217 continue; /* once more, with feeling */
227 /* scan directory page looking for matching vnode */
228 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
229 dp = (struct dirent *) cpos;
230 reclen = dp->d_reclen;
232 /* check for malformed directory.. */
233 if (reclen < DIRENT_MINSIZE) {
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.
243 if ((dp->d_type != DT_WHT) &&
244 (dp->d_fileno == fileno)) {
252 bcopy(dp->d_name, bp, dp->d_namlen);
266 free(dirbuf, M_TEMP);
272 * common routine shared by sys___getcwd() and linux_vn_isunder()
275 #define GETCWD_CHECK_ACCESS 0x0001
278 linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, p)
287 struct filedesc *fdp = p->p_fd;
288 struct vnode *uvp = NULL;
303 * Error handling invariant:
304 * Before a `goto out':
305 * lvp is either NULL, or locked and held.
306 * uvp is either NULL, or locked and held.
309 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
318 * this loop will terminate when one of the following happens:
320 * - getdirentries or lookup fails
321 * - we run out of space in the buffer.
329 if (lvp->v_type != VDIR) {
335 * access check here is optional, depending on
336 * whether or not caller cares.
338 if (flags & GETCWD_CHECK_ACCESS) {
339 error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
346 * step up if we're a covered vnode..
348 while (lvp->v_flag & VROOT) {
355 lvp = lvp->v_mount->mnt_vnodecovered;
358 * hodie natus est radici frater
365 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
372 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
377 panic("getcwd: oops, forgot to null lvp");
378 if (bufp && (bp <= bufp)) {
379 panic("getcwd: oops, went back too far");
387 } while ((lvp != rvp) && (limit > 0));
402 * Find pathname of process's current directory.
404 * Use vfs vnode-to-name reverse cache; if that fails, fall back
405 * to reading directory contents.
409 linux_getcwd(struct proc *p, struct linux_getcwd_args *args)
411 struct __getcwd_args bsd;
412 caddr_t sg, bp, bend, path;
413 int error, len, lenused;
416 printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
417 args->buf, args->bufsize);
420 sg = stackgap_init();
421 bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
422 bsd.buflen = SPARE_USRSPACE;
423 error = __getcwd(p, &bsd);
425 lenused = strlen(bsd.buf) + 1;
426 if (lenused <= args->bufsize) {
427 p->p_retval[0] = lenused;
428 error = copyout(bsd.buf, args->buf, lenused);
435 if (len > MAXPATHLEN*4)
440 path = (char *)malloc(len, M_TEMP, M_WAITOK);
447 * 5th argument here is "max number of vnodes to traverse".
448 * Since each entry takes up at least 2 bytes in the output buffer,
449 * limit it to N/2 vnodes for an N byte buffer.
452 error = linux_getcwd_common (p->p_fd->fd_cdir, NULL,
453 &bp, path, len/2, GETCWD_CHECK_ACCESS, p);
458 p->p_retval[0] = lenused;
459 /* put the result into user buffer */
460 error = copyout(bp, args->buf, lenused);