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.7 2003/07/26 18:12:40 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/filedesc.h>
47 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
53 #include <sys/namei.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 thread *));
67 linux_getcwd_common __P((struct vnode *, struct vnode *,
68 char **, char *, int, int, struct thread *));
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, td)
115 struct proc *p = td->td_proc;
126 struct vnode *uvp = NULL;
127 struct vnode *lvp = *lvpp;
128 struct componentname cn;
135 * If we want the filename, get some info we need while the
136 * current directory is still locked.
139 error = VOP_GETATTR(lvp, &va, td);
149 * Ok, we have to do it the hard way..
150 * Next, get parent vnode using lookup of ..
152 cn.cn_nameiop = LOOKUP;
153 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
155 cn.cn_cred = p->p_ucred;
157 cn.cn_nameptr = "..";
162 * At this point, lvp is locked and will be unlocked by the lookup.
163 * On successful return, *uvpp will be locked
165 error = VOP_LOOKUP(lvp, uvpp, &cn);
174 /* If we don't care about the pathname, we're done */
181 fileno = va.va_fileid;
183 dirbuflen = DIRBLKSIZ;
184 if (dirbuflen < va.va_blocksize)
185 dirbuflen = va.va_blocksize;
186 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
193 /* call VOP_READDIR of parent */
194 iov.iov_base = dirbuf;
195 iov.iov_len = dirbuflen;
199 uio.uio_offset = off;
200 uio.uio_resid = dirbuflen;
201 uio.uio_segflg = UIO_SYSSPACE;
202 uio.uio_rw = UIO_READ;
207 error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
209 off = uio.uio_offset;
212 * Try again if NFS tosses its cookies.
213 * XXX this can still loop forever if the directory is busted
214 * such that the second or subsequent page of it always
217 if ((error == EINVAL) && (tries < 3)) {
220 continue; /* once more, with feeling */
230 /* scan directory page looking for matching vnode */
231 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
232 dp = (struct dirent *) cpos;
233 reclen = dp->d_reclen;
235 /* check for malformed directory.. */
236 if (reclen < DIRENT_MINSIZE) {
241 * XXX should perhaps do VOP_LOOKUP to
242 * check that we got back to the right place,
243 * but getting the locking games for that
244 * right would be heinous.
246 if ((dp->d_type != DT_WHT) &&
247 (dp->d_fileno == fileno)) {
255 bcopy(dp->d_name, bp, dp->d_namlen);
269 free(dirbuf, M_TEMP);
275 * common routine shared by sys___getcwd() and linux_vn_isunder()
278 #define GETCWD_CHECK_ACCESS 0x0001
281 linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, td)
290 struct proc *p = td->td_proc;
291 struct filedesc *fdp;
292 struct vnode *uvp = NULL;
310 * Error handling invariant:
311 * Before a `goto out':
312 * lvp is either NULL, or locked and held.
313 * uvp is either NULL, or locked and held.
316 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td);
325 * this loop will terminate when one of the following happens:
327 * - getdirentries or lookup fails
328 * - we run out of space in the buffer.
336 if (lvp->v_type != VDIR) {
342 * access check here is optional, depending on
343 * whether or not caller cares.
345 if (flags & GETCWD_CHECK_ACCESS) {
346 error = VOP_ACCESS(lvp, perms, p->p_ucred, td);
353 * step up if we're a covered vnode..
355 while (lvp->v_flag & VROOT) {
362 lvp = lvp->v_mount->mnt_vnodecovered;
365 * hodie natus est radici frater
372 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td);
379 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, td);
384 panic("getcwd: oops, forgot to null lvp");
385 if (bufp && (bp <= bufp)) {
386 panic("getcwd: oops, went back too far");
394 } while ((lvp != rvp) && (limit > 0));
409 * Find pathname of process's current directory.
411 * Use vfs vnode-to-name reverse cache; if that fails, fall back
412 * to reading directory contents.
416 linux_getcwd(struct linux_getcwd_args *args)
418 struct thread *td = curthread;
419 struct proc *p = td->td_proc;
420 struct __getcwd_args bsd;
421 caddr_t sg, bp, bend, path;
422 int error, len, lenused;
427 printf("Linux-emul(%ld): getcwd(%p, %d)\n", (long)p->p_pid,
428 args->buf, args->bufsize);
431 sg = stackgap_init();
432 bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
433 bsd.buflen = SPARE_USRSPACE;
434 bsd.lmsg.u.ms_result = 0;
435 error = __getcwd(&bsd);
436 args->lmsg.u.ms_result = bsd.lmsg.u.ms_result;
438 lenused = strlen(bsd.buf) + 1;
439 if (lenused <= args->bufsize) {
440 args->lmsg.u.ms_result = lenused;
441 error = copyout(bsd.buf, args->buf, lenused);
448 if (len > MAXPATHLEN*4)
453 path = (char *)malloc(len, M_TEMP, M_WAITOK);
460 * 5th argument here is "max number of vnodes to traverse".
461 * Since each entry takes up at least 2 bytes in the output buffer,
462 * limit it to N/2 vnodes for an N byte buffer.
465 error = linux_getcwd_common (p->p_fd->fd_cdir, NULL,
466 &bp, path, len/2, GETCWD_CHECK_ACCESS, td);
471 args->lmsg.u.ms_result = lenused;
472 /* put the result into user buffer */
473 error = copyout(bp, args->buf, lenused);