With vnode locking now mandatory a number of bugs have cropped up in the
[dragonfly.git] / sys / emulation / linux / linux_util.c
1 /*
2  * Copyright (c) 1994 Christos Zoulas
3  * Copyright (c) 1995 Frank van der Linden
4  * Copyright (c) 1995 Scott Bartram
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp
30  * $FreeBSD: src/sys/compat/linux/linux_util.c,v 1.12.2.2 2001/11/05 19:08:23 marcel Exp $
31  * $DragonFly: src/sys/emulation/linux/linux_util.c,v 1.9 2004/09/09 20:52:19 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/namei.h>
38 #include <sys/malloc.h>
39 #include <sys/vnode.h>
40
41 #include "linux_util.h"
42
43 const char      linux_emul_path[] = "/compat/linux";
44
45 /*
46  * Search for an alternate path before passing pathname arguments on
47  * to system calls.
48  *
49  * Only signal an error if something really bad happens.  In most cases
50  * we can just return the untranslated path, eg. name lookup failures.
51  */
52 int
53 linux_copyin_path(char *uname, char **kname, int flags)
54 {
55         struct thread *td = curthread;
56         struct nameidata nd, ndroot;
57         struct vattr vat, vatroot;
58         char *buf, *cp;
59         int error, length, dummy;
60
61         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
62         *kname = buf;
63
64         /*
65          * Don't bother trying to translate if the path is relative.
66          */
67         if (fubyte(uname) != '/')
68                 goto dont_translate;
69
70         /*
71          * The path is absolute.  Prepend the buffer with the emulation
72          * path and copy in.
73          */
74         length = strlen(linux_emul_path);
75         bcopy(linux_emul_path, buf, length);
76         error = copyinstr(uname, buf + length, MAXPATHLEN - length, &dummy);
77         if (error)
78                 goto done;
79
80         switch (flags) {
81         case LINUX_PATH_CREATE:
82                 /*
83                  * Check to see if the parent directory exists in the
84                  * emulation tree.  Walk the string backwards to find
85                  * the last '/'.
86                  */
87                 cp = buf + strlen(buf);
88                 while (--cp >= buf) {
89                         if (*cp == '/')
90                                 break;
91                 }
92                 if (cp < buf)
93                         goto dont_translate;
94                 *cp = 0;
95
96                 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, buf, td);
97                 error = namei(&nd);
98                 if (error)
99                         goto dont_translate;
100                 NDFREE(&nd, NDF_ONLY_PNBUF);
101                 vrele(nd.ni_vp);
102                 *cp = '/';
103                 return (0);
104         case LINUX_PATH_EXISTS:
105                 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, buf, td);
106                 error = namei(&nd);
107                 if (error)
108                         goto dont_translate;
109
110                 /*
111                  * We now compare the vnode of the linux_root to the one
112                  * vnode asked. If they resolve to be the same, then we
113                  * ignore the match so that the real root gets used.
114                  * This avoids the problem of traversing "../.." to find the
115                  * root directory and never finding it, because "/" resolves
116                  * to the emulation root directory. This is expensive :-(
117                  *
118                  * The next three function calls should not return errors.
119                  * If they do something is seriously wrong, eg. the
120                  * emulation subtree does not exist.  Cross our fingers
121                  * and return the untranslated path if something happens.
122                  */
123                 NDINIT(&ndroot, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE,
124                     linux_emul_path, td);
125                 error = namei(&ndroot);
126                 if (error) {
127                         NDFREE(&nd, NDF_ONLY_PNBUF);
128                         vrele(nd.ni_vp);
129                         goto dont_translate;
130                 }
131                 
132                 error = VOP_GETATTR(nd.ni_vp, &vat, td);
133                 if (error == 0) {
134                         error = VOP_GETATTR(ndroot.ni_vp, &vatroot, td);
135                         if (error == 0) {
136                                 if (vat.va_fsid == vatroot.va_fsid &&
137                                     vat.va_fileid == vatroot.va_fileid)
138                                         error = ENOENT;
139                         }
140                 }
141                 NDFREE(&nd, NDF_ONLY_PNBUF);
142                 vrele(nd.ni_vp);
143                 NDFREE(&ndroot, NDF_ONLY_PNBUF);
144                 vrele(ndroot.ni_vp);
145                 if (error)
146                         goto dont_translate;
147                 return (0);
148         default:
149                 error = EINVAL;
150                 goto done;
151         }
152         
153 dont_translate:
154         error = copyinstr(uname, buf, MAXPATHLEN, &dummy);
155 done:
156         if (error)
157                 linux_free_path(kname);
158         return (error);
159 }
160
161 /*
162  * Smaller version of the above for translating in kernel buffers.  Only
163  * used in exec_linux_imgact_try().  Only check is path exists.
164  */
165 int
166 linux_translate_path(char *path, int size)
167 {
168         struct thread *td = curthread;
169         struct nameidata nd;
170         char *buf;
171         int error, length, dummy;
172
173         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
174         length = strlen(linux_emul_path);
175         bcopy(linux_emul_path, buf, length);
176         error = copystr(path, buf + length, MAXPATHLEN - length, &dummy);
177         if (error)
178                 goto cleanup;
179         
180         /*
181          * If this errors, then the path probably doesn't exists.
182          */
183         NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW, UIO_SYSSPACE, buf, td);
184         error = namei(&nd);
185         if (error) {
186                 error = 0;
187                 goto cleanup;
188         }
189         NDFREE(&nd, NDF_ONLY_PNBUF);
190         vrele(nd.ni_vp);
191
192         /*
193          * The alternate path does exist.  Return it in the buffer if
194          * it fits.
195          */
196         if (strlen(buf) + 1 <= size)
197                 error = copystr(buf, path, size, &dummy);
198
199 cleanup:
200
201         free(buf, M_TEMP);
202         return (error);
203 }