1 /* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
2 /* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/namei.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
49 #include <sys/malloc.h>
50 #include <sys/dirent.h>
51 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
57 #include <machine/../linux/linux.h>
58 #include <machine/../linux/linux_proto.h>
60 #include <compat/linux/linux_util.h>
62 #include <security/mac/mac_framework.h>
65 linux_getcwd_scandir(struct vnode **, struct vnode **,
66 char **, char *, struct thread *);
68 linux_getcwd_common(struct vnode *, struct vnode *,
69 char **, char *, int, int, struct thread *);
71 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
74 * Vnode variable naming conventions in this file:
76 * rvp: the current root we're aiming towards.
77 * lvp, *lvpp: the "lower" vnode
78 * uvp, *uvpp: the "upper" vnode.
80 * Since all the vnodes we're dealing with are directories, and the
81 * lookups are going *up* in the filesystem rather than *down*, the
82 * usual "pvp" (parent) or "dvp" (directory) naming conventions are
87 * XXX Will infinite loop in certain cases if a directory read reliably
88 * returns EINVAL on last block.
89 * XXX is EINVAL the right thing to return if a directory is malformed?
93 * XXX Untested vs. mount -o union; probably does the wrong thing.
97 * Find parent vnode of *lvpp, return in *uvpp
99 * If we care about the name, scan it looking for name of directory
100 * entry pointing at lvp.
102 * Place the name in the buffer which starts at bufp, immediately
103 * before *bpp, and move bpp backwards to point at the start of it.
105 * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
106 * On exit, *uvpp is either NULL or is a locked vnode reference.
109 linux_getcwd_scandir(lvpp, uvpp, bpp, bufp, td)
126 struct vnode *uvp = NULL;
127 struct vnode *lvp = *lvpp;
128 struct componentname cn;
133 * If we want the filename, get some info we need while the
134 * current directory is still locked.
137 error = VOP_GETATTR(lvp, &va, td->td_ucred);
147 * Ok, we have to do it the hard way..
148 * Next, get parent vnode using lookup of ..
150 cn.cn_nameiop = LOOKUP;
151 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
153 cn.cn_cred = td->td_ucred;
155 cn.cn_nameptr = "..";
158 cn.cn_lkflags = LK_SHARED;
161 * At this point, lvp is locked and will be unlocked by the lookup.
162 * On successful return, *uvpp will be locked
165 error = mac_vnode_check_lookup(td->td_ucred, lvp, &cn);
168 error = VOP_LOOKUP(lvp, uvpp, &cn);
177 /* If we don't care about the pathname, we're done */
184 fileno = va.va_fileid;
186 dirbuflen = DIRBLKSIZ;
187 if (dirbuflen < va.va_blocksize)
188 dirbuflen = va.va_blocksize;
189 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
196 /* call VOP_READDIR of parent */
197 iov.iov_base = dirbuf;
198 iov.iov_len = dirbuflen;
202 uio.uio_offset = off;
203 uio.uio_resid = dirbuflen;
204 uio.uio_segflg = UIO_SYSSPACE;
205 uio.uio_rw = UIO_READ;
211 error = mac_vnode_check_readdir(td->td_ucred, uvp);
214 error = VOP_READDIR(uvp, &uio, td->td_ucred, &eofflag,
217 off = uio.uio_offset;
220 * Try again if NFS tosses its cookies.
221 * XXX this can still loop forever if the directory is busted
222 * such that the second or subsequent page of it always
225 if ((error == EINVAL) && (tries < 3)) {
228 continue; /* once more, with feeling */
238 /* scan directory page looking for matching vnode */
239 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
240 dp = (struct dirent *) cpos;
241 reclen = dp->d_reclen;
243 /* check for malformed directory.. */
244 if (reclen < DIRENT_MINSIZE) {
249 * XXX should perhaps do VOP_LOOKUP to
250 * check that we got back to the right place,
251 * but getting the locking games for that
252 * right would be heinous.
254 if ((dp->d_type != DT_WHT) &&
255 (dp->d_fileno == fileno)) {
263 bcopy(dp->d_name, bp, dp->d_namlen);
277 free(dirbuf, M_TEMP);
283 * common routine shared by sys___getcwd() and linux_vn_isunder()
286 #define GETCWD_CHECK_ACCESS 0x0001
289 linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, td)
298 struct filedesc *fdp = td->td_proc->p_fd;
299 struct vnode *uvp = NULL;
302 accmode_t accmode = VEXEC;
314 * Error handling invariant:
315 * Before a `goto out':
316 * lvp is either NULL, or locked and held.
317 * uvp is either NULL, or locked and held.
320 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
322 panic("vn_lock LK_RETRY returned error %d", error);
326 * this loop will terminate when one of the following happens:
328 * - getdirentries or lookup fails
329 * - we run out of space in the buffer.
337 if (lvp->v_type != VDIR) {
343 * access check here is optional, depending on
344 * whether or not caller cares.
346 if (flags & GETCWD_CHECK_ACCESS) {
347 error = VOP_ACCESS(lvp, accmode, td->td_ucred, td);
350 accmode = VEXEC|VREAD;
354 * step up if we're a covered vnode..
356 while (lvp->v_vflag & VV_ROOT) {
363 lvp = lvp->v_mount->mnt_vnodecovered;
366 * hodie natus est radici frater
373 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
375 panic("vn_lock LK_RETRY returned %d", error);
377 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, td);
382 panic("getcwd: oops, forgot to null lvp");
383 if (bufp && (bp <= bufp)) {
384 panic("getcwd: oops, went back too far");
392 } while ((lvp != rvp) && (limit > 0));
407 * Find pathname of process's current directory.
409 * Use vfs vnode-to-name reverse cache; if that fails, fall back
410 * to reading directory contents.
414 linux_getcwd(struct thread *td, struct linux_getcwd_args *args)
416 caddr_t bp, bend, path;
417 int error, len, lenused;
421 printf(ARGS(getcwd, "%p, %ld"), args->buf, (long)args->bufsize);
426 if (len > MAXPATHLEN*4)
431 path = (char *)malloc(len, M_TEMP, M_WAITOK);
433 error = kern___getcwd(td, path, UIO_SYSSPACE, len);
435 lenused = strlen(path) + 1;
436 if (lenused <= args->bufsize) {
437 td->td_retval[0] = lenused;
438 error = copyout(path, args->buf, lenused);
448 * 5th argument here is "max number of vnodes to traverse".
449 * Since each entry takes up at least 2 bytes in the output buffer,
450 * limit it to N/2 vnodes for an N byte buffer.
454 error = linux_getcwd_common (td->td_proc->p_fd->fd_cdir, NULL,
455 &bp, path, len/2, GETCWD_CHECK_ACCESS, td);
461 td->td_retval[0] = lenused;
462 /* put the result into user buffer */
463 error = copyout(bp, args->buf, lenused);