linux_getcwd.c revision 112430
182518Sgallatin/* $FreeBSD: head/sys/compat/linux/linux_getcwd.c 112430 2003-03-20 10:40:45Z phk $ */ 282518Sgallatin/* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */ 382518Sgallatin/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */ 482518Sgallatin 582518Sgallatin/*- 682518Sgallatin * Copyright (c) 1999 The NetBSD Foundation, Inc. 782518Sgallatin * All rights reserved. 882518Sgallatin * 982518Sgallatin * This code is derived from software contributed to The NetBSD Foundation 1082518Sgallatin * by Bill Sommerfeld. 1182518Sgallatin * 1282518Sgallatin * Redistribution and use in source and binary forms, with or without 1382518Sgallatin * modification, are permitted provided that the following conditions 1482518Sgallatin * are met: 1582518Sgallatin * 1. Redistributions of source code must retain the above copyright 1682518Sgallatin * notice, this list of conditions and the following disclaimer. 1782518Sgallatin * 2. Redistributions in binary form must reproduce the above copyright 1882518Sgallatin * notice, this list of conditions and the following disclaimer in the 1982518Sgallatin * documentation and/or other materials provided with the distribution. 2082518Sgallatin * 3. All advertising materials mentioning features or use of this software 2182518Sgallatin * must display the following acknowledgement: 2282518Sgallatin * This product includes software developed by the NetBSD 2382518Sgallatin * Foundation, Inc. and its contributors. 2482518Sgallatin * 4. Neither the name of The NetBSD Foundation nor the names of its 2582518Sgallatin * contributors may be used to endorse or promote products derived 2682518Sgallatin * from this software without specific prior written permission. 2782518Sgallatin * 2882518Sgallatin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2982518Sgallatin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 3082518Sgallatin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 3182518Sgallatin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3282518Sgallatin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3382518Sgallatin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3482518Sgallatin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3582518Sgallatin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3682518Sgallatin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3782518Sgallatin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3882518Sgallatin * POSSIBILITY OF SUCH DAMAGE. 3982518Sgallatin */ 40112430Sphk#include "opt_compat.h" 41112430Sphk#include "opt_mac.h" 4282518Sgallatin 4382518Sgallatin#include <sys/param.h> 4482518Sgallatin#include <sys/systm.h> 45112430Sphk#include <sys/namei.h> 46112430Sphk#include <sys/filedesc.h> 47112430Sphk#include <sys/kernel.h> 48112430Sphk#include <sys/file.h> 49112430Sphk#include <sys/stat.h> 50102872Siedowse#include <sys/syscallsubr.h> 51112430Sphk#include <sys/vnode.h> 52112430Sphk#include <sys/mount.h> 5382518Sgallatin#include <sys/proc.h> 54112430Sphk#include <sys/uio.h> 55112430Sphk#include <sys/mac.h> 56112430Sphk#include <sys/malloc.h> 57112430Sphk#include <sys/dirent.h> 58112430Sphk#include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */ 5982518Sgallatin 6082518Sgallatin#include <machine/../linux/linux.h> 6182518Sgallatin#include <machine/../linux/linux_proto.h> 62112430Sphk#include <compat/linux/linux_util.h> 6382518Sgallatin 64112430Sphkstatic int 65112430Sphklinux_getcwd_scandir(struct vnode **, struct vnode **, 66112430Sphk char **, char *, struct thread *); 67112430Sphkstatic int 68112430Sphklinux_getcwd_common(struct vnode *, struct vnode *, 69112430Sphk char **, char *, int, int, struct thread *); 70112430Sphk 71112430Sphk#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4) 72112430Sphk 7382518Sgallatin/* 74112430Sphk * Vnode variable naming conventions in this file: 75112430Sphk * 76112430Sphk * rvp: the current root we're aiming towards. 77112430Sphk * lvp, *lvpp: the "lower" vnode 78112430Sphk * uvp, *uvpp: the "upper" vnode. 79112430Sphk * 80112430Sphk * Since all the vnodes we're dealing with are directories, and the 81112430Sphk * lookups are going *up* in the filesystem rather than *down*, the 82112430Sphk * usual "pvp" (parent) or "dvp" (directory) naming conventions are 83112430Sphk * too confusing. 84112430Sphk */ 85112430Sphk 86112430Sphk/* 87112430Sphk * XXX Will infinite loop in certain cases if a directory read reliably 88112430Sphk * returns EINVAL on last block. 89112430Sphk * XXX is EINVAL the right thing to return if a directory is malformed? 90112430Sphk */ 91112430Sphk 92112430Sphk/* 93112430Sphk * XXX Untested vs. mount -o union; probably does the wrong thing. 94112430Sphk */ 95112430Sphk 96112430Sphk/* 97112430Sphk * Find parent vnode of *lvpp, return in *uvpp 98112430Sphk * 99112430Sphk * If we care about the name, scan it looking for name of directory 100112430Sphk * entry pointing at lvp. 101112430Sphk * 102112430Sphk * Place the name in the buffer which starts at bufp, immediately 103112430Sphk * before *bpp, and move bpp backwards to point at the start of it. 104112430Sphk * 105112430Sphk * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed 106112430Sphk * On exit, *uvpp is either NULL or is a locked vnode reference. 107112430Sphk */ 108112430Sphkstatic int 109112430Sphklinux_getcwd_scandir(lvpp, uvpp, bpp, bufp, td) 110112430Sphk struct vnode **lvpp; 111112430Sphk struct vnode **uvpp; 112112430Sphk char **bpp; 113112430Sphk char *bufp; 114112430Sphk struct thread *td; 115112430Sphk{ 116112430Sphk int error = 0; 117112430Sphk int eofflag; 118112430Sphk off_t off; 119112430Sphk int tries; 120112430Sphk struct uio uio; 121112430Sphk struct iovec iov; 122112430Sphk char *dirbuf = NULL; 123112430Sphk int dirbuflen; 124112430Sphk ino_t fileno; 125112430Sphk struct vattr va; 126112430Sphk struct vnode *uvp = NULL; 127112430Sphk struct vnode *lvp = *lvpp; 128112430Sphk struct componentname cn; 129112430Sphk int len, reclen; 130112430Sphk tries = 0; 131112430Sphk 132112430Sphk /* 133112430Sphk * If we want the filename, get some info we need while the 134112430Sphk * current directory is still locked. 135112430Sphk */ 136112430Sphk if (bufp != NULL) { 137112430Sphk error = VOP_GETATTR(lvp, &va, td->td_ucred, td); 138112430Sphk if (error) { 139112430Sphk vput(lvp); 140112430Sphk *lvpp = NULL; 141112430Sphk *uvpp = NULL; 142112430Sphk return error; 143112430Sphk } 144112430Sphk } 145112430Sphk 146112430Sphk /* 147112430Sphk * Ok, we have to do it the hard way.. 148112430Sphk * Next, get parent vnode using lookup of .. 149112430Sphk */ 150112430Sphk cn.cn_nameiop = LOOKUP; 151112430Sphk cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY; 152112430Sphk cn.cn_thread = td; 153112430Sphk cn.cn_cred = td->td_ucred; 154112430Sphk cn.cn_pnbuf = NULL; 155112430Sphk cn.cn_nameptr = ".."; 156112430Sphk cn.cn_namelen = 2; 157112430Sphk cn.cn_consume = 0; 158112430Sphk 159112430Sphk /* 160112430Sphk * At this point, lvp is locked and will be unlocked by the lookup. 161112430Sphk * On successful return, *uvpp will be locked 162112430Sphk */ 163112430Sphk error = VOP_LOOKUP(lvp, uvpp, &cn); 164112430Sphk if (error) { 165112430Sphk vput(lvp); 166112430Sphk *lvpp = NULL; 167112430Sphk *uvpp = NULL; 168112430Sphk return error; 169112430Sphk } 170112430Sphk uvp = *uvpp; 171112430Sphk 172112430Sphk /* If we don't care about the pathname, we're done */ 173112430Sphk if (bufp == NULL) { 174112430Sphk vrele(lvp); 175112430Sphk *lvpp = NULL; 176112430Sphk return 0; 177112430Sphk } 178112430Sphk 179112430Sphk fileno = va.va_fileid; 180112430Sphk 181112430Sphk dirbuflen = DIRBLKSIZ; 182112430Sphk if (dirbuflen < va.va_blocksize) 183112430Sphk dirbuflen = va.va_blocksize; 184112430Sphk dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK); 185112430Sphk 186112430Sphk#if 0 187112430Sphkunionread: 188112430Sphk#endif 189112430Sphk off = 0; 190112430Sphk do { 191112430Sphk /* call VOP_READDIR of parent */ 192112430Sphk iov.iov_base = dirbuf; 193112430Sphk iov.iov_len = dirbuflen; 194112430Sphk 195112430Sphk uio.uio_iov = &iov; 196112430Sphk uio.uio_iovcnt = 1; 197112430Sphk uio.uio_offset = off; 198112430Sphk uio.uio_resid = dirbuflen; 199112430Sphk uio.uio_segflg = UIO_SYSSPACE; 200112430Sphk uio.uio_rw = UIO_READ; 201112430Sphk uio.uio_td = td; 202112430Sphk 203112430Sphk eofflag = 0; 204112430Sphk 205112430Sphk#ifdef MAC 206112430Sphk error = mac_check_vnode_readdir(td->td_ucred, uvp); 207112430Sphk if (error == 0) 208112430Sphk#endif /* MAC */ 209112430Sphk error = VOP_READDIR(uvp, &uio, td->td_ucred, &eofflag, 210112430Sphk 0, 0); 211112430Sphk 212112430Sphk off = uio.uio_offset; 213112430Sphk 214112430Sphk /* 215112430Sphk * Try again if NFS tosses its cookies. 216112430Sphk * XXX this can still loop forever if the directory is busted 217112430Sphk * such that the second or subsequent page of it always 218112430Sphk * returns EINVAL 219112430Sphk */ 220112430Sphk if ((error == EINVAL) && (tries < 3)) { 221112430Sphk off = 0; 222112430Sphk tries++; 223112430Sphk continue; /* once more, with feeling */ 224112430Sphk } 225112430Sphk 226112430Sphk if (!error) { 227112430Sphk char *cpos; 228112430Sphk struct dirent *dp; 229112430Sphk 230112430Sphk cpos = dirbuf; 231112430Sphk tries = 0; 232112430Sphk 233112430Sphk /* scan directory page looking for matching vnode */ 234112430Sphk for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) { 235112430Sphk dp = (struct dirent *) cpos; 236112430Sphk reclen = dp->d_reclen; 237112430Sphk 238112430Sphk /* check for malformed directory.. */ 239112430Sphk if (reclen < DIRENT_MINSIZE) { 240112430Sphk error = EINVAL; 241112430Sphk goto out; 242112430Sphk } 243112430Sphk /* 244112430Sphk * XXX should perhaps do VOP_LOOKUP to 245112430Sphk * check that we got back to the right place, 246112430Sphk * but getting the locking games for that 247112430Sphk * right would be heinous. 248112430Sphk */ 249112430Sphk if ((dp->d_type != DT_WHT) && 250112430Sphk (dp->d_fileno == fileno)) { 251112430Sphk char *bp = *bpp; 252112430Sphk bp -= dp->d_namlen; 253112430Sphk 254112430Sphk if (bp <= bufp) { 255112430Sphk error = ERANGE; 256112430Sphk goto out; 257112430Sphk } 258112430Sphk bcopy(dp->d_name, bp, dp->d_namlen); 259112430Sphk error = 0; 260112430Sphk *bpp = bp; 261112430Sphk goto out; 262112430Sphk } 263112430Sphk cpos += reclen; 264112430Sphk } 265112430Sphk } 266112430Sphk } while (!eofflag); 267112430Sphk error = ENOENT; 268112430Sphk 269112430Sphkout: 270112430Sphk vrele(lvp); 271112430Sphk *lvpp = NULL; 272112430Sphk free(dirbuf, M_TEMP); 273112430Sphk return error; 274112430Sphk} 275112430Sphk 276112430Sphk 277112430Sphk/* 278112430Sphk * common routine shared by sys___getcwd() and linux_vn_isunder() 279112430Sphk */ 280112430Sphk 281112430Sphk#define GETCWD_CHECK_ACCESS 0x0001 282112430Sphk 283112430Sphkstatic int 284112430Sphklinux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, td) 285112430Sphk struct vnode *lvp; 286112430Sphk struct vnode *rvp; 287112430Sphk char **bpp; 288112430Sphk char *bufp; 289112430Sphk int limit; 290112430Sphk int flags; 291112430Sphk struct thread *td; 292112430Sphk{ 293112430Sphk struct filedesc *fdp = td->td_proc->p_fd; 294112430Sphk struct vnode *uvp = NULL; 295112430Sphk char *bp = NULL; 296112430Sphk int error; 297112430Sphk int perms = VEXEC; 298112430Sphk 299112430Sphk if (rvp == NULL) { 300112430Sphk rvp = fdp->fd_rdir; 301112430Sphk if (rvp == NULL) 302112430Sphk rvp = rootvnode; 303112430Sphk } 304112430Sphk 305112430Sphk VREF(rvp); 306112430Sphk VREF(lvp); 307112430Sphk 308112430Sphk /* 309112430Sphk * Error handling invariant: 310112430Sphk * Before a `goto out': 311112430Sphk * lvp is either NULL, or locked and held. 312112430Sphk * uvp is either NULL, or locked and held. 313112430Sphk */ 314112430Sphk 315112430Sphk error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td); 316112430Sphk if (error) { 317112430Sphk vrele(lvp); 318112430Sphk lvp = NULL; 319112430Sphk goto out; 320112430Sphk } 321112430Sphk if (bufp) 322112430Sphk bp = *bpp; 323112430Sphk /* 324112430Sphk * this loop will terminate when one of the following happens: 325112430Sphk * - we hit the root 326112430Sphk * - getdirentries or lookup fails 327112430Sphk * - we run out of space in the buffer. 328112430Sphk */ 329112430Sphk if (lvp == rvp) { 330112430Sphk if (bp) 331112430Sphk *(--bp) = '/'; 332112430Sphk goto out; 333112430Sphk } 334112430Sphk do { 335112430Sphk if (lvp->v_type != VDIR) { 336112430Sphk error = ENOTDIR; 337112430Sphk goto out; 338112430Sphk } 339112430Sphk 340112430Sphk /* 341112430Sphk * access check here is optional, depending on 342112430Sphk * whether or not caller cares. 343112430Sphk */ 344112430Sphk if (flags & GETCWD_CHECK_ACCESS) { 345112430Sphk error = VOP_ACCESS(lvp, perms, td->td_ucred, td); 346112430Sphk if (error) 347112430Sphk goto out; 348112430Sphk perms = VEXEC|VREAD; 349112430Sphk } 350112430Sphk 351112430Sphk /* 352112430Sphk * step up if we're a covered vnode.. 353112430Sphk */ 354112430Sphk while (lvp->v_vflag & VV_ROOT) { 355112430Sphk struct vnode *tvp; 356112430Sphk 357112430Sphk if (lvp == rvp) 358112430Sphk goto out; 359112430Sphk 360112430Sphk tvp = lvp; 361112430Sphk lvp = lvp->v_mount->mnt_vnodecovered; 362112430Sphk vput(tvp); 363112430Sphk /* 364112430Sphk * hodie natus est radici frater 365112430Sphk */ 366112430Sphk if (lvp == NULL) { 367112430Sphk error = ENOENT; 368112430Sphk goto out; 369112430Sphk } 370112430Sphk VREF(lvp); 371112430Sphk error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td); 372112430Sphk if (error != 0) { 373112430Sphk vrele(lvp); 374112430Sphk lvp = NULL; 375112430Sphk goto out; 376112430Sphk } 377112430Sphk } 378112430Sphk error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, td); 379112430Sphk if (error) 380112430Sphk goto out; 381112430Sphk#if DIAGNOSTIC 382112430Sphk if (lvp != NULL) 383112430Sphk panic("getcwd: oops, forgot to null lvp"); 384112430Sphk if (bufp && (bp <= bufp)) { 385112430Sphk panic("getcwd: oops, went back too far"); 386112430Sphk } 387112430Sphk#endif 388112430Sphk if (bp) 389112430Sphk *(--bp) = '/'; 390112430Sphk lvp = uvp; 391112430Sphk uvp = NULL; 392112430Sphk limit--; 393112430Sphk } while ((lvp != rvp) && (limit > 0)); 394112430Sphk 395112430Sphkout: 396112430Sphk if (bpp) 397112430Sphk *bpp = bp; 398112430Sphk if (uvp) 399112430Sphk vput(uvp); 400112430Sphk if (lvp) 401112430Sphk vput(lvp); 402112430Sphk vrele(rvp); 403112430Sphk return error; 404112430Sphk} 405112430Sphk 406112430Sphk 407112430Sphk/* 40882518Sgallatin * Find pathname of process's current directory. 40982518Sgallatin * 41082518Sgallatin * Use vfs vnode-to-name reverse cache; if that fails, fall back 411112430Sphk * to reading directory contents. 41282518Sgallatin */ 41382518Sgallatin 41482518Sgallatinint 41583366Sjulianlinux_getcwd(struct thread *td, struct linux_getcwd_args *args) 41682518Sgallatin{ 417112430Sphk caddr_t bp, bend, path; 418112430Sphk int error, len, lenused; 419112430Sphk 420112430Sphk#ifdef DEBUG 421112430Sphk printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)td->td_proc->p_pid, 422112430Sphk args->buf, (long)args->bufsize); 423112430Sphk#endif 424112430Sphk 425112430Sphk len = args->bufsize; 426112430Sphk 427112430Sphk if (len > MAXPATHLEN*4) 428112430Sphk len = MAXPATHLEN*4; 429112430Sphk else if (len < 2) 430112430Sphk return ERANGE; 431112430Sphk 432112430Sphk path = (char *)malloc(len, M_TEMP, M_WAITOK); 433112430Sphk 434112430Sphk error = kern___getcwd(td, path, UIO_SYSSPACE, len); 435112430Sphk if (!error) { 436112430Sphk lenused = strlen(path) + 1; 437112430Sphk if (lenused <= args->bufsize) { 438112430Sphk td->td_retval[0] = lenused; 439112430Sphk error = copyout(path, args->buf, lenused); 440112430Sphk } 441112430Sphk else 442112430Sphk error = ERANGE; 443112430Sphk } else { 444112430Sphk bp = &path[len]; 445112430Sphk bend = bp; 446112430Sphk *(--bp) = '\0'; 447112430Sphk 448112430Sphk /* 449112430Sphk * 5th argument here is "max number of vnodes to traverse". 450112430Sphk * Since each entry takes up at least 2 bytes in the output buffer, 451112430Sphk * limit it to N/2 vnodes for an N byte buffer. 452112430Sphk */ 453112430Sphk 454112430Sphk error = linux_getcwd_common (td->td_proc->p_fd->fd_cdir, NULL, 455112430Sphk &bp, path, len/2, GETCWD_CHECK_ACCESS, td); 456112430Sphk 457112430Sphk if (error) 458112430Sphk goto out; 459112430Sphk lenused = bend - bp; 460112430Sphk td->td_retval[0] = lenused; 461112430Sphk /* put the result into user buffer */ 462112430Sphk error = copyout(bp, args->buf, lenused); 463112430Sphk } 464112430Sphkout: 465112430Sphk free(path, M_TEMP); 466112430Sphk return (error); 46782518Sgallatin} 46882518Sgallatin 469