1 /* $FreeBSD: src/sys/compat/linux/linux_getcwd.c,v 1.2.2.3 2001/11/05 19:08:22 marcel Exp $ */
2 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
3 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/namei.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
58 #include <machine/../linux/linux.h>
59 #include <machine/../linux/linux_proto.h>
60 #include <compat/linux/linux_util.h>
63 linux_getcwd_scandir __P((struct vnode **, struct vnode **,
64 char **, char *, struct proc *));
66 linux_getcwd_common __P((struct vnode *, struct vnode *,
67 char **, char *, int, int, struct proc *));
69 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
72 * Vnode variable naming conventions in this file:
74 * rvp: the current root we're aiming towards.
75 * lvp, *lvpp: the "lower" vnode
76 * uvp, *uvpp: the "upper" vnode.
78 * Since all the vnodes we're dealing with are directories, and the
79 * lookups are going *up* in the filesystem rather than *down*, the
80 * usual "pvp" (parent) or "dvp" (directory) naming conventions are
85 * XXX Will infinite loop in certain cases if a directory read reliably
86 * returns EINVAL on last block.
87 * XXX is EINVAL the right thing to return if a directory is malformed?
91 * XXX Untested vs. mount -o union; probably does the wrong thing.
95 * Find parent vnode of *lvpp, return in *uvpp
97 * If we care about the name, scan it looking for name of directory
98 * entry pointing at lvp.
100 * Place the name in the buffer which starts at bufp, immediately
101 * before *bpp, and move bpp backwards to point at the start of it.
103 * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
104 * On exit, *uvpp is either NULL or is a locked vnode reference.
107 linux_getcwd_scandir(lvpp, uvpp, bpp, bufp, p)
124 struct vnode *uvp = NULL;
125 struct vnode *lvp = *lvpp;
126 struct componentname cn;
131 * If we want the filename, get some info we need while the
132 * current directory is still locked.
135 error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
145 * Ok, we have to do it the hard way..
146 * Next, get parent vnode using lookup of ..
148 cn.cn_nameiop = LOOKUP;
149 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
151 cn.cn_cred = p->p_ucred;
153 cn.cn_nameptr = "..";
158 * At this point, lvp is locked and will be unlocked by the lookup.
159 * On successful return, *uvpp will be locked
161 error = VOP_LOOKUP(lvp, uvpp, &cn);
170 /* If we don't care about the pathname, we're done */
177 fileno = va.va_fileid;
179 dirbuflen = DIRBLKSIZ;
180 if (dirbuflen < va.va_blocksize)
181 dirbuflen = va.va_blocksize;
182 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
189 /* call VOP_READDIR of parent */
190 iov.iov_base = dirbuf;
191 iov.iov_len = dirbuflen;
195 uio.uio_offset = off;
196 uio.uio_resid = dirbuflen;
197 uio.uio_segflg = UIO_SYSSPACE;
198 uio.uio_rw = UIO_READ;
203 error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
205 off = uio.uio_offset;
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
213 if ((error == EINVAL) && (tries < 3)) {
216 continue; /* once more, with feeling */
226 /* scan directory page looking for matching vnode */
227 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
228 dp = (struct dirent *) cpos;
229 reclen = dp->d_reclen;
231 /* check for malformed directory.. */
232 if (reclen < DIRENT_MINSIZE) {
237 * XXX should perhaps do VOP_LOOKUP to
238 * check that we got back to the right place,
239 * but getting the locking games for that
240 * right would be heinous.
242 if ((dp->d_type != DT_WHT) &&
243 (dp->d_fileno == fileno)) {
251 bcopy(dp->d_name, bp, dp->d_namlen);
265 free(dirbuf, M_TEMP);
271 * common routine shared by sys___getcwd() and linux_vn_isunder()
274 #define GETCWD_CHECK_ACCESS 0x0001
277 linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, p)
286 struct filedesc *fdp = p->p_fd;
287 struct vnode *uvp = NULL;
302 * Error handling invariant:
303 * Before a `goto out':
304 * lvp is either NULL, or locked and held.
305 * uvp is either NULL, or locked and held.
308 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
317 * this loop will terminate when one of the following happens:
319 * - getdirentries or lookup fails
320 * - we run out of space in the buffer.
328 if (lvp->v_type != VDIR) {
334 * access check here is optional, depending on
335 * whether or not caller cares.
337 if (flags & GETCWD_CHECK_ACCESS) {
338 error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
345 * step up if we're a covered vnode..
347 while (lvp->v_flag & VROOT) {
354 lvp = lvp->v_mount->mnt_vnodecovered;
357 * hodie natus est radici frater
364 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
371 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
376 panic("getcwd: oops, forgot to null lvp");
377 if (bufp && (bp <= bufp)) {
378 panic("getcwd: oops, went back too far");
386 } while ((lvp != rvp) && (limit > 0));
401 * Find pathname of process's current directory.
403 * Use vfs vnode-to-name reverse cache; if that fails, fall back
404 * to reading directory contents.
408 linux_getcwd(struct proc *p, struct linux_getcwd_args *args)
410 struct __getcwd_args bsd;
411 caddr_t sg, bp, bend, path;
412 int error, len, lenused;
415 printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
416 args->buf, args->bufsize);
419 sg = stackgap_init();
420 bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
421 bsd.buflen = SPARE_USRSPACE;
422 error = __getcwd(p, &bsd);
424 lenused = strlen(bsd.buf) + 1;
425 if (lenused <= args->bufsize) {
426 p->p_retval[0] = lenused;
427 error = copyout(bsd.buf, args->buf, lenused);
434 if (len > MAXPATHLEN*4)
439 path = (char *)malloc(len, M_TEMP, M_WAITOK);
446 * 5th argument here is "max number of vnodes to traverse".
447 * Since each entry takes up at least 2 bytes in the output buffer,
448 * limit it to N/2 vnodes for an N byte buffer.
451 error = linux_getcwd_common (p->p_fd->fd_cdir, NULL,
452 &bp, path, len/2, GETCWD_CHECK_ACCESS, p);
457 p->p_retval[0] = lenused;
458 /* put the result into user buffer */
459 error = copyout(bp, args->buf, lenused);