VFS messaging/interfacing work stage 9/99: VFS 'NEW' API WORK.
[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.10 2004/11/12 00:09:18 dillon Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/nlookup.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 nlookupdata nd, ndroot;
57         struct vattr vat, vatroot;
58         struct vnode *vp, *vproot;
59         char *buf, *cp;
60         int error, length, dummy;
61
62         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
63         *kname = buf;
64
65         /*
66          * Don't bother trying to translate if the path is relative.
67          */
68         if (fubyte(uname) != '/')
69                 goto dont_translate;
70
71         /*
72          * The path is absolute.  Prepend the buffer with the emulation
73          * path and copy in.
74          */
75         length = strlen(linux_emul_path);
76         bcopy(linux_emul_path, buf, length);
77         error = copyinstr(uname, buf + length, MAXPATHLEN - length, &dummy);
78         if (error)
79                 goto done;
80
81         switch (flags) {
82         case LINUX_PATH_CREATE:
83                 /*
84                  * Check to see if the parent directory exists in the
85                  * emulation tree.  Walk the string backwards to find
86                  * the last '/'.
87                  */
88                 cp = buf + strlen(buf);
89                 while (--cp >= buf) {
90                         if (*cp == '/')
91                                 break;
92                 }
93                 if (cp < buf)
94                         goto dont_translate;
95                 *cp = 0;
96
97                 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
98                 if (error == 0)
99                         error = nlookup(&nd);
100                 nlookup_done(&nd);
101                 if (error)
102                         goto dont_translate;
103                 *cp = '/';
104                 return (0);
105         case LINUX_PATH_EXISTS:
106                 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
107                 if (error == 0)
108                         error = nlookup(&nd);
109                 vp = NULL;
110                 if (error == 0)
111                         error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
112                 nlookup_done(&nd);
113                 if (error)
114                         goto dont_translate;
115
116                 /*
117                  * We now compare the vnode of the linux_root to the one
118                  * vnode asked. If they resolve to be the same, then we
119                  * ignore the match so that the real root gets used.
120                  * This avoids the problem of traversing "../.." to find the
121                  * root directory and never finding it, because "/" resolves
122                  * to the emulation root directory. This is expensive :-(
123                  *
124                  * The next three function calls should not return errors.
125                  * If they do something is seriously wrong, eg. the
126                  * emulation subtree does not exist.  Cross our fingers
127                  * and return the untranslated path if something happens.
128                  */
129                 error = nlookup_init(&ndroot, linux_emul_path, UIO_SYSSPACE,
130                                         NLC_FOLLOW);
131                 if (error == 0)
132                         error = nlookup(&ndroot);
133                 vproot = NULL;
134                 if (error == 0) {
135                         error = cache_vref(ndroot.nl_ncp, ndroot.nl_cred,
136                                                 &vproot);
137                 }
138                 nlookup_done(&ndroot);
139                 if (error) {
140                         vrele(vp);
141                         goto dont_translate;
142                 }
143                 
144                 error = VOP_GETATTR(vp, &vat, td);
145                 if (error == 0) {
146                         error = VOP_GETATTR(vproot, &vatroot, td);
147                         if (error == 0) {
148                                 if (vat.va_fsid == vatroot.va_fsid &&
149                                     vat.va_fileid == vatroot.va_fileid)
150                                         error = ENOENT;
151                         }
152                 }
153                 vrele(vp);
154                 vrele(vproot);
155                 if (error)
156                         goto dont_translate;
157                 return (0);
158         default:
159                 error = EINVAL;
160                 goto done;
161         }
162         
163 dont_translate:
164         error = copyinstr(uname, buf, MAXPATHLEN, &dummy);
165 done:
166         if (error)
167                 linux_free_path(kname);
168         return (error);
169 }
170
171 /*
172  * Smaller version of the above for translating in kernel buffers.  Only
173  * used in exec_linux_imgact_try().  Only check is path exists.
174  */
175 int
176 linux_translate_path(char *path, int size)
177 {
178         struct nlookupdata nd;
179         char *buf;
180         int error, length, dummy;
181
182         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
183         length = strlen(linux_emul_path);
184         bcopy(linux_emul_path, buf, length);
185         error = copystr(path, buf + length, MAXPATHLEN - length, &dummy);
186         if (error)
187                 goto cleanup;
188         
189         /*
190          * If this errors, then the path probably doesn't exist.
191          */
192         error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
193         if (error == 0)
194                 error = nlookup(&nd);
195         nlookup_done(&nd);
196         if (error) {
197                 error = 0;
198                 goto cleanup;
199         }
200
201         /*
202          * The alternate path does exist.  Return it in the buffer if
203          * it fits.
204          */
205         if (strlen(buf) + 1 <= size)
206                 error = copystr(buf, path, size, &dummy);
207
208 cleanup:
209
210         free(buf, M_TEMP);
211         return (error);
212 }