1 /* $NetBSD: vfs_getcwd.c,v 1.43 2009/01/17 07:02:35 yamt 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 __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.43 2009/01/17 07:02:35 yamt Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/namei.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
47 #include <sys/dirent.h>
48 #include <sys/kauth.h>
50 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
52 #include <sys/syscallargs.h>
55 * Vnode variable naming conventions in this file:
57 * rvp: the current root we're aiming towards.
58 * lvp, *lvpp: the "lower" vnode
59 * uvp, *uvpp: the "upper" vnode.
61 * Since all the vnodes we're dealing with are directories, and the
62 * lookups are going *up* in the filesystem rather than *down*, the
63 * usual "pvp" (parent) or "dvp" (directory) naming conventions are
68 * XXX Will infinite loop in certain cases if a directory read reliably
69 * returns EINVAL on last block.
70 * XXX is EINVAL the right thing to return if a directory is malformed?
74 * XXX Untested vs. mount -o union; probably does the wrong thing.
78 * Find parent vnode of *lvpp, return in *uvpp
80 * If we care about the name, scan it looking for name of directory
81 * entry pointing at lvp.
83 * Place the name in the buffer which starts at bufp, immediately
84 * before *bpp, and move bpp backwards to point at the start of it.
86 * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
87 * On exit, *uvpp is either NULL or is a locked vnode reference.
90 getcwd_scandir(struct vnode
**lvpp
, struct vnode
**uvpp
, char **bpp
,
91 char *bufp
, struct lwp
*l
)
103 struct vnode
*uvp
= NULL
;
104 struct vnode
*lvp
= *lvpp
;
105 kauth_cred_t cred
= l
->l_cred
;
106 struct componentname cn
;
111 * If we want the filename, get some info we need while the
112 * current directory is still locked.
115 error
= VOP_GETATTR(lvp
, &va
, cred
);
125 * Ok, we have to do it the hard way..
126 * Next, get parent vnode using lookup of ..
128 cn
.cn_nameiop
= LOOKUP
;
129 cn
.cn_flags
= ISLASTCN
| ISDOTDOT
| RDONLY
;
132 cn
.cn_nameptr
= "..";
138 * At this point, lvp is locked.
139 * On successful return, *uvpp will be locked
141 error
= VOP_LOOKUP(lvp
, uvpp
, &cn
);
150 /* If we don't care about the pathname, we're done */
156 fileno
= va
.va_fileid
;
158 dirbuflen
= DIRBLKSIZ
;
159 if (dirbuflen
< va
.va_blocksize
)
160 dirbuflen
= va
.va_blocksize
;
161 dirbuf
= kmem_alloc(dirbuflen
, KM_SLEEP
);
168 /* call VOP_READDIR of parent */
169 iov
.iov_base
= dirbuf
;
170 iov
.iov_len
= dirbuflen
;
174 uio
.uio_offset
= off
;
175 uio
.uio_resid
= dirbuflen
;
176 uio
.uio_rw
= UIO_READ
;
177 UIO_SETUP_SYSSPACE(&uio
);
181 error
= VOP_READDIR(uvp
, &uio
, cred
, &eofflag
, 0, 0);
183 off
= uio
.uio_offset
;
186 * Try again if NFS tosses its cookies.
187 * XXX this can still loop forever if the directory is busted
188 * such that the second or subsequent page of it always
191 if ((error
== EINVAL
) && (tries
< 3)) {
194 continue; /* once more, with feeling */
204 /* scan directory page looking for matching vnode */
205 for (len
= (dirbuflen
- uio
.uio_resid
); len
> 0;
207 dp
= (struct dirent
*) cpos
;
208 reclen
= dp
->d_reclen
;
210 /* check for malformed directory.. */
211 if (reclen
< _DIRENT_MINSIZE(dp
)) {
216 * XXX should perhaps do VOP_LOOKUP to
217 * check that we got back to the right place,
218 * but getting the locking games for that
219 * right would be heinous.
221 if ((dp
->d_type
!= DT_WHT
) &&
222 (dp
->d_fileno
== fileno
)) {
230 memcpy(bp
, dp
->d_name
, dp
->d_namlen
);
242 * Deal with mount -o union, which unions only the
243 * root directory of the mount.
245 if ((uvp
->v_vflag
& VV_ROOT
) &&
246 (uvp
->v_mount
->mnt_flag
& MNT_UNION
)) {
247 struct vnode
*tvp
= uvp
;
249 uvp
= uvp
->v_mount
->mnt_vnodecovered
;
253 vn_lock(uvp
, LK_EXCLUSIVE
| LK_RETRY
);
261 kmem_free(dirbuf
, dirbuflen
);
266 * Look in the vnode-to-name reverse cache to see if
267 * we can find things the easy way.
269 * XXX vget failure path is untested.
271 * On entry, *lvpp is a locked vnode reference.
272 * On exit, one of the following is the case:
273 * 0) Both *lvpp and *uvpp are NULL and failure is returned.
274 * 1) *uvpp is NULL, *lvpp remains locked and -1 is returned (cache miss)
275 * 2) *uvpp is a locked vnode reference, *lvpp is vput and NULL'ed
276 * and 0 is returned (cache hit)
280 getcwd_getcache(struct vnode
**lvpp
, struct vnode
**uvpp
, char **bpp
,
283 struct vnode
*lvp
, *uvp
= NULL
;
290 * This returns 0 on a cache hit, -1 on a clean cache miss,
291 * or an errno on other failure.
293 error
= cache_revlookup(lvp
, uvpp
, bpp
, bufp
);
305 * Since we're going up, we have to release the current lock
306 * before we take the parent lock.
310 error
= vget(uvp
, LK_EXCLUSIVE
| LK_RETRY
);
313 * Verify that vget succeeded while we were waiting for the
319 * Oops, we missed. If the vget failed, get our lock back
320 * then rewind the `bp' and tell the caller to try things
324 vn_lock(lvp
, LK_EXCLUSIVE
| LK_RETRY
);
335 * common routine shared by sys___getcwd() and vn_isunder()
339 getcwd_common(struct vnode
*lvp
, struct vnode
*rvp
, char **bpp
, char *bufp
,
340 int limit
, int flags
, struct lwp
*l
)
342 struct cwdinfo
*cwdi
= l
->l_proc
->p_cwdi
;
343 kauth_cred_t cred
= l
->l_cred
;
344 struct vnode
*uvp
= NULL
;
351 rvp
= cwdi
->cwdi_rdir
;
360 * Error handling invariant:
361 * Before a `goto out':
362 * lvp is either NULL, or locked and held.
363 * uvp is either NULL, or locked and held.
366 vn_lock(lvp
, LK_EXCLUSIVE
| LK_RETRY
);
371 * this loop will terminate when one of the following happens:
373 * - getdirentries or lookup fails
374 * - we run out of space in the buffer.
383 * access check here is optional, depending on
384 * whether or not caller cares.
386 if (flags
& GETCWD_CHECK_ACCESS
) {
387 error
= VOP_ACCESS(lvp
, perms
, cred
);
394 * step up if we're a covered vnode..
396 while (lvp
->v_vflag
& VV_ROOT
) {
403 lvp
= lvp
->v_mount
->mnt_vnodecovered
;
406 * hodie natus est radici frater
413 error
= vn_lock(lvp
, LK_EXCLUSIVE
| LK_RETRY
);
421 * Look in the name cache; if that fails, look in the
424 error
= getcwd_getcache(&lvp
, &uvp
, &bp
, bufp
);
426 if (lvp
->v_type
!= VDIR
) {
430 error
= getcwd_scandir(&lvp
, &uvp
, &bp
, bufp
, l
);
436 panic("getcwd: oops, forgot to null lvp");
437 if (bufp
&& (bp
<= bufp
)) {
438 panic("getcwd: oops, went back too far");
446 } while ((lvp
!= rvp
) && (limit
> 0));
460 * Check if one directory can be found inside another in the directory
463 * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
464 * chroot() actually means something.
467 vn_isunder(struct vnode
*lvp
, struct vnode
*rvp
, struct lwp
*l
)
471 error
= getcwd_common(lvp
, rvp
, NULL
, NULL
, MAXPATHLEN
/ 2, 0, l
);
480 * Returns true if proc p1's root directory equal to or under p2's
483 * Intended to be used from ptrace/procfs sorts of things.
487 proc_isunder(struct proc
*p1
, struct lwp
*l2
)
489 struct vnode
*r1
= p1
->p_cwdi
->cwdi_rdir
;
490 struct vnode
*r2
= l2
->l_proc
->p_cwdi
->cwdi_rdir
;
497 return vn_isunder(r1
, r2
, l2
);
501 * Find pathname of process's current directory.
503 * Use vfs vnode-to-name reverse cache; if that fails, fall back
504 * to reading directory contents.
508 sys___getcwd(struct lwp
*l
, const struct sys___getcwd_args
*uap
, register_t
*retval
)
511 syscallarg(char *) bufp;
512 syscallarg(size_t) length;
518 int len
= SCARG(uap
, length
);
520 struct cwdinfo
*cwdi
;
522 if (len
> MAXPATHLEN
* 4)
523 len
= MAXPATHLEN
* 4;
527 path
= kmem_alloc(len
, KM_SLEEP
);
536 * 5th argument here is "max number of vnodes to traverse".
537 * Since each entry takes up at least 2 bytes in the output buffer,
538 * limit it to N/2 vnodes for an N byte buffer.
540 cwdi
= l
->l_proc
->p_cwdi
;
541 rw_enter(&cwdi
->cwdi_lock
, RW_READER
);
542 error
= getcwd_common(cwdi
->cwdi_cdir
, NULL
, &bp
, path
,
543 len
/2, GETCWD_CHECK_ACCESS
, l
);
544 rw_exit(&cwdi
->cwdi_lock
);
550 /* put the result into user buffer */
551 error
= copyout(bp
, SCARG(uap
, bufp
), lenused
);
554 kmem_free(path
, len
);
559 * Try to find a pathname for a vnode. Since there is no mapping
560 * vnode -> parent directory, this needs the NAMECACHE_ENTER_REVERSE
561 * option to work (to make cache_revlookup succeed).
564 vnode_to_path(char *path
, size_t len
, struct vnode
*vp
, struct lwp
*curl
,
567 struct proc
*curp
= curl
->l_proc
;
568 int error
, lenused
, elen
;
572 bp
= bend
= &path
[len
];
575 error
= vget(vp
, LK_EXCLUSIVE
| LK_RETRY
);
578 error
= cache_revlookup(vp
, &dvp
, &bp
, path
);
581 return (error
== -1 ? ENOENT
: error
);
583 error
= vget(dvp
, 0);
587 /* XXX GETCWD_CHECK_ACCESS == 0x0001 */
588 error
= getcwd_common(dvp
, NULL
, &bp
, path
, len
/ 2, 1, curl
);
591 * Strip off emulation path for emulated processes looking at
592 * the maps file of a process of the same emulation. (Won't
593 * work if /emul/xxx is a symlink..)
595 if (curp
->p_emul
== p
->p_emul
&& curp
->p_emul
->e_path
!= NULL
) {
596 elen
= strlen(curp
->p_emul
->e_path
);
597 if (!strncmp(bp
, curp
->p_emul
->e_path
, elen
))
603 memcpy(path
, bp
, lenused
);