eb96346f592450f9d0dcb2206fd582520f5b4aee
[dragonfly.git] / sys / emulation / linux / linux_getcwd.c
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.3 2003/06/23 17:55:26 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 $ */
5
6 /*-
7  * Copyright (c) 1999 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Bill Sommerfeld.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
28  *
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.
40  */
41 #include "opt_compat.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/namei.h>
47 #include <sys/filedesc.h>
48 #include <sys/kernel.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/uio.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <ufs/ufs/dir.h>        /* XXX only for DIRBLKSIZ */
58
59 #include <machine/../linux/linux.h>
60 #include <machine/../linux/linux_proto.h>
61 #include <compat/linux/linux_util.h>
62
63 static int
64 linux_getcwd_scandir __P((struct vnode **, struct vnode **,
65     char **, char *, struct proc *));
66 static int
67 linux_getcwd_common __P((struct vnode *, struct vnode *,
68                    char **, char *, int, int, struct proc *));
69
70 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
71
72 /*
73  * Vnode variable naming conventions in this file:
74  *
75  * rvp: the current root we're aiming towards.
76  * lvp, *lvpp: the "lower" vnode
77  * uvp, *uvpp: the "upper" vnode.
78  *
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
82  * too confusing.
83  */
84
85 /*
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?
89  */
90
91 /*
92  * XXX Untested vs. mount -o union; probably does the wrong thing.
93  */
94
95 /*
96  * Find parent vnode of *lvpp, return in *uvpp
97  *
98  * If we care about the name, scan it looking for name of directory
99  * entry pointing at lvp.
100  *
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.
103  *
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.
106  */
107 static int
108 linux_getcwd_scandir(lvpp, uvpp, bpp, bufp, p)
109         struct vnode **lvpp;
110         struct vnode **uvpp;
111         char **bpp;
112         char *bufp;
113         struct proc *p;
114 {
115         int     error = 0;
116         int     eofflag;
117         off_t   off;
118         int     tries;
119         struct uio uio;
120         struct iovec iov;
121         char   *dirbuf = NULL;
122         int     dirbuflen;
123         ino_t   fileno;
124         struct vattr va;
125         struct vnode *uvp = NULL;
126         struct vnode *lvp = *lvpp;      
127         struct componentname cn;
128         int len, reclen;
129         tries = 0;
130
131         /*
132          * If we want the filename, get some info we need while the
133          * current directory is still locked.
134          */
135         if (bufp != NULL) {
136                 error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
137                 if (error) {
138                         vput(lvp);
139                         *lvpp = NULL;
140                         *uvpp = NULL;
141                         return error;
142                 }
143         }
144
145         /*
146          * Ok, we have to do it the hard way..
147          * Next, get parent vnode using lookup of ..
148          */
149         cn.cn_nameiop = LOOKUP;
150         cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
151         cn.cn_proc = p;
152         cn.cn_cred = p->p_ucred;
153         cn.cn_pnbuf = NULL;
154         cn.cn_nameptr = "..";
155         cn.cn_namelen = 2;
156         cn.cn_consume = 0;
157         
158         /*
159          * At this point, lvp is locked and will be unlocked by the lookup.
160          * On successful return, *uvpp will be locked
161          */
162         error = VOP_LOOKUP(lvp, uvpp, &cn);
163         if (error) {
164                 vput(lvp);
165                 *lvpp = NULL;
166                 *uvpp = NULL;
167                 return error;
168         }
169         uvp = *uvpp;
170
171         /* If we don't care about the pathname, we're done */
172         if (bufp == NULL) {
173                 vrele(lvp);
174                 *lvpp = NULL;
175                 return 0;
176         }
177         
178         fileno = va.va_fileid;
179
180         dirbuflen = DIRBLKSIZ;
181         if (dirbuflen < va.va_blocksize)
182                 dirbuflen = va.va_blocksize;
183         dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
184
185 #if 0
186 unionread:
187 #endif
188         off = 0;
189         do {
190                 /* call VOP_READDIR of parent */
191                 iov.iov_base = dirbuf;
192                 iov.iov_len = dirbuflen;
193
194                 uio.uio_iov = &iov;
195                 uio.uio_iovcnt = 1;
196                 uio.uio_offset = off;
197                 uio.uio_resid = dirbuflen;
198                 uio.uio_segflg = UIO_SYSSPACE;
199                 uio.uio_rw = UIO_READ;
200                 uio.uio_procp = p;
201
202                 eofflag = 0;
203
204                 error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
205
206                 off = uio.uio_offset;
207
208                 /*
209                  * Try again if NFS tosses its cookies.
210                  * XXX this can still loop forever if the directory is busted
211                  * such that the second or subsequent page of it always
212                  * returns EINVAL
213                  */
214                 if ((error == EINVAL) && (tries < 3)) {
215                         off = 0;
216                         tries++;
217                         continue;       /* once more, with feeling */
218                 }
219
220                 if (!error) {
221                         char   *cpos;
222                         struct dirent *dp;
223                         
224                         cpos = dirbuf;
225                         tries = 0;
226                                 
227                         /* scan directory page looking for matching vnode */ 
228                         for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
229                                 dp = (struct dirent *) cpos;
230                                 reclen = dp->d_reclen;
231
232                                 /* check for malformed directory.. */
233                                 if (reclen < DIRENT_MINSIZE) {
234                                         error = EINVAL;
235                                         goto out;
236                                 }
237                                 /*
238                                  * XXX should perhaps do VOP_LOOKUP to
239                                  * check that we got back to the right place,
240                                  * but getting the locking games for that
241                                  * right would be heinous.
242                                  */
243                                 if ((dp->d_type != DT_WHT) &&
244                                     (dp->d_fileno == fileno)) {
245                                         char *bp = *bpp;
246                                         bp -= dp->d_namlen;
247                                         
248                                         if (bp <= bufp) {
249                                                 error = ERANGE;
250                                                 goto out;
251                                         }
252                                         bcopy(dp->d_name, bp, dp->d_namlen);
253                                         error = 0;
254                                         *bpp = bp;
255                                         goto out;
256                                 }
257                                 cpos += reclen;
258                         }
259                 }
260         } while (!eofflag);
261         error = ENOENT;
262                 
263 out:
264         vrele(lvp);
265         *lvpp = NULL;
266         free(dirbuf, M_TEMP);
267         return error;
268 }
269
270
271 /*
272  * common routine shared by sys___getcwd() and linux_vn_isunder()
273  */
274
275 #define GETCWD_CHECK_ACCESS 0x0001
276
277 static int
278 linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, p)
279         struct vnode *lvp;
280         struct vnode *rvp;
281         char **bpp;
282         char *bufp;
283         int limit;
284         int flags;
285         struct proc *p;
286 {
287         struct filedesc *fdp = p->p_fd;
288         struct vnode *uvp = NULL;
289         char *bp = NULL;
290         int error;
291         int perms = VEXEC;
292
293         if (rvp == NULL) {
294                 rvp = fdp->fd_rdir;
295                 if (rvp == NULL)
296                         rvp = rootvnode;
297         }
298         
299         VREF(rvp);
300         VREF(lvp);
301
302         /*
303          * Error handling invariant:
304          * Before a `goto out':
305          *      lvp is either NULL, or locked and held.
306          *      uvp is either NULL, or locked and held.
307          */
308
309         error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
310         if (error) {
311                 vrele(lvp);
312                 lvp = NULL;
313                 goto out;
314         }
315         if (bufp)
316                 bp = *bpp;
317         /*
318          * this loop will terminate when one of the following happens:
319          *      - we hit the root
320          *      - getdirentries or lookup fails
321          *      - we run out of space in the buffer.
322          */
323         if (lvp == rvp) {
324                 if (bp)
325                         *(--bp) = '/';
326                 goto out;
327         }
328         do {
329                 if (lvp->v_type != VDIR) {
330                         error = ENOTDIR;
331                         goto out;
332                 }
333                 
334                 /*
335                  * access check here is optional, depending on
336                  * whether or not caller cares.
337                  */
338                 if (flags & GETCWD_CHECK_ACCESS) {
339                         error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
340                         if (error)
341                                 goto out;
342                         perms = VEXEC|VREAD;
343                 }
344                 
345                 /*
346                  * step up if we're a covered vnode..
347                  */
348                 while (lvp->v_flag & VROOT) {
349                         struct vnode *tvp;
350
351                         if (lvp == rvp)
352                                 goto out;
353                         
354                         tvp = lvp;
355                         lvp = lvp->v_mount->mnt_vnodecovered;
356                         vput(tvp);
357                         /*
358                          * hodie natus est radici frater
359                          */
360                         if (lvp == NULL) {
361                                 error = ENOENT;
362                                 goto out;
363                         }
364                         VREF(lvp);
365                         error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
366                         if (error != 0) {
367                                 vrele(lvp);
368                                 lvp = NULL;
369                                 goto out;
370                         }
371                 }
372                 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
373                 if (error)
374                         goto out;
375 #if DIAGNOSTIC          
376                 if (lvp != NULL)
377                         panic("getcwd: oops, forgot to null lvp");
378                 if (bufp && (bp <= bufp)) {
379                         panic("getcwd: oops, went back too far");
380                 }
381 #endif          
382                 if (bp) 
383                         *(--bp) = '/';
384                 lvp = uvp;
385                 uvp = NULL;
386                 limit--;
387         } while ((lvp != rvp) && (limit > 0)); 
388
389 out:
390         if (bpp)
391                 *bpp = bp;
392         if (uvp)
393                 vput(uvp);
394         if (lvp)
395                 vput(lvp);
396         vrele(rvp);
397         return error;
398 }
399
400
401 /*
402  * Find pathname of process's current directory.
403  *
404  * Use vfs vnode-to-name reverse cache; if that fails, fall back
405  * to reading directory contents.
406  */
407
408 int
409 linux_getcwd(struct linux_getcwd_args *args)
410 {
411         struct proc *p = curproc;
412         struct __getcwd_args bsd;
413         caddr_t sg, bp, bend, path;
414         int error, len, lenused;
415
416 #ifdef DEBUG
417         printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
418                args->buf, args->bufsize);
419 #endif
420
421         sg = stackgap_init();
422         bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
423         bsd.buflen = SPARE_USRSPACE;
424         error = __getcwd(&bsd);
425         if (!error) {
426                 lenused = strlen(bsd.buf) + 1;
427                 if (lenused <= args->bufsize) {
428                         p->p_retval[0] = lenused;
429                         error = copyout(bsd.buf, args->buf, lenused);
430                 }
431                 else
432                         error = ERANGE;
433         } else {
434                 len = args->bufsize;
435
436                 if (len > MAXPATHLEN*4)
437                         len = MAXPATHLEN*4;
438                 else if (len < 2)
439                         return ERANGE;
440
441                 path = (char *)malloc(len, M_TEMP, M_WAITOK);
442
443                 bp = &path[len];
444                 bend = bp;
445                 *(--bp) = '\0';
446
447                 /*
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.
451                  */
452
453                 error = linux_getcwd_common (p->p_fd->fd_cdir, NULL,
454                     &bp, path, len/2, GETCWD_CHECK_ACCESS, p);
455
456                 if (error)
457                         goto out;
458                 lenused = bend - bp;
459                 p->p_retval[0] = lenused;
460                 /* put the result into user buffer */
461                 error = copyout(bp, args->buf, lenused);
462
463 out:
464                 free(path, M_TEMP);     
465         }
466         return (error);
467 }
468