proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[dragonfly.git] / sys / emulation / linux / linux_util.c
CommitLineData
984263bc
MD
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 $
41c20dac 31 * $DragonFly: src/sys/emulation/linux/linux_util.c,v 1.3 2003/06/23 17:55:27 dillon Exp $
984263bc
MD
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/namei.h>
37#include <sys/proc.h>
38#include <sys/malloc.h>
39#include <sys/vnode.h>
40
41#include <compat/linux/linux_util.h>
42
43const char linux_emul_path[] = "/compat/linux";
44
45/*
46 * Search an alternate path before passing pathname arguments on
47 * to system calls. Useful for keeping a separate 'emulation tree'.
48 *
49 * If cflag is set, we check if an attempt can be made to create
50 * the named file, i.e. we check if the directory it should
51 * be in exists.
52 */
53int
54linux_emul_find(p, sgp, prefix, path, pbuf, cflag)
41c20dac 55 struct proc *p;
984263bc
MD
56 caddr_t *sgp; /* Pointer to stackgap memory */
57 const char *prefix;
58 char *path;
59 char **pbuf;
60 int cflag;
61{
62 struct nameidata nd;
63 struct nameidata ndroot;
64 struct vattr vat;
65 struct vattr vatroot;
66 int error;
67 char *ptr, *buf, *cp;
68 size_t sz, len;
69
70 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
71 *pbuf = path;
72
73 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++)
74 continue;
75
76 sz = MAXPATHLEN - (ptr - buf);
77
78 /*
79 * If sgp is not given then the path is already in kernel space
80 */
81 if (sgp == NULL)
82 error = copystr(path, ptr, sz, &len);
83 else
84 error = copyinstr(path, ptr, sz, &len);
85
86 if (error) {
87 free(buf, M_TEMP);
88 return error;
89 }
90
91 if (*ptr != '/') {
92 free(buf, M_TEMP);
93 return EINVAL;
94 }
95
96 /*
97 * We know that there is a / somewhere in this pathname.
98 * Search backwards for it, to find the file's parent dir
99 * to see if it exists in the alternate tree. If it does,
100 * and we want to create a file (cflag is set). We don't
101 * need to worry about the root comparison in this case.
102 */
103
104 if (cflag) {
105 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
106 *cp = '\0';
107
108 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
109
110 if ((error = namei(&nd)) != 0) {
111 free(buf, M_TEMP);
112 return error;
113 }
114
115 *cp = '/';
116 }
117 else {
118 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
119
120 if ((error = namei(&nd)) != 0) {
121 free(buf, M_TEMP);
122 return error;
123 }
124
125 /*
126 * We now compare the vnode of the linux_root to the one
127 * vnode asked. If they resolve to be the same, then we
128 * ignore the match so that the real root gets used.
129 * This avoids the problem of traversing "../.." to find the
130 * root directory and never finding it, because "/" resolves
131 * to the emulation root directory. This is expensive :-(
132 */
133 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path,
134 p);
135
136 if ((error = namei(&ndroot)) != 0) {
137 /* Cannot happen! */
138 free(buf, M_TEMP);
139 NDFREE(&nd, NDF_ONLY_PNBUF);
140 vrele(nd.ni_vp);
141 return error;
142 }
143
144 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) {
145 goto bad;
146 }
147
148 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
149 != 0) {
150 goto bad;
151 }
152
153 if (vat.va_fsid == vatroot.va_fsid &&
154 vat.va_fileid == vatroot.va_fileid) {
155 error = ENOENT;
156 goto bad;
157 }
158
159 }
160 if (sgp == NULL)
161 *pbuf = buf;
162 else {
163 sz = &ptr[len] - buf;
164 *pbuf = stackgap_alloc(sgp, sz + 1);
165 if (*pbuf != NULL)
166 error = copyout(buf, *pbuf, sz);
167 else
168 error = ENAMETOOLONG;
169 free(buf, M_TEMP);
170 }
171
172 NDFREE(&nd, NDF_ONLY_PNBUF);
173 vrele(nd.ni_vp);
174 if (!cflag) {
175 NDFREE(&ndroot, NDF_ONLY_PNBUF);
176 vrele(ndroot.ni_vp);
177 }
178 return error;
179
180bad:
181 NDFREE(&ndroot, NDF_ONLY_PNBUF);
182 vrele(ndroot.ni_vp);
183 NDFREE(&nd, NDF_ONLY_PNBUF);
184 vrele(nd.ni_vp);
185 free(buf, M_TEMP);
186 return error;
187}