linux_file64.c revision 1.2.4.9 1 /* $NetBSD: linux_file64.c,v 1.2.4.9 2002/05/29 21:32:38 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Linux 64bit filesystem calls. Used on 32bit archs, not used on 64bit ones.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: linux_file64.c,v 1.2.4.9 2002/05/29 21:32:38 nathanw Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/proc.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/filedesc.h>
53 #include <sys/ioctl.h>
54 #include <sys/kernel.h>
55 #include <sys/mount.h>
56 #include <sys/malloc.h>
57 #include <sys/vnode.h>
58 #include <sys/tty.h>
59 #include <sys/conf.h>
60
61 #include <sys/sa.h>
62 #include <sys/syscallargs.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_fcntl.h>
67 #include <compat/linux/common/linux_util.h>
68 #include <compat/linux/common/linux_machdep.h>
69
70 #include <compat/linux/linux_syscallargs.h>
71
72 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
73 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
74
75 /*
76 * Convert a NetBSD stat structure to a Linux stat structure.
77 * Only the order of the fields and the padding in the structure
78 * is different. linux_fakedev is a machine-dependent function
79 * which optionally converts device driver major/minor numbers
80 * (XXX horrible, but what can you do against code that compares
81 * things against constant major device numbers? sigh)
82 */
83 static void
84 bsd_to_linux_stat(bsp, lsp)
85 struct stat *bsp;
86 struct linux_stat64 *lsp;
87 {
88 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
89 lsp->lst_ino = bsp->st_ino;
90 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
91 if (bsp->st_nlink >= (1 << 15))
92 lsp->lst_nlink = (1 << 15) - 1;
93 else
94 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
95 lsp->lst_uid = bsp->st_uid;
96 lsp->lst_gid = bsp->st_gid;
97 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
98 lsp->lst_size = bsp->st_size;
99 lsp->lst_blksize = bsp->st_blksize;
100 lsp->lst_blocks = bsp->st_blocks;
101 lsp->lst_atime = bsp->st_atime;
102 lsp->lst_mtime = bsp->st_mtime;
103 lsp->lst_ctime = bsp->st_ctime;
104 lsp->lst_ino64 = bsp->st_ino;
105 }
106
107 /*
108 * The stat functions below are plain sailing. stat and lstat are handled
109 * by one function to avoid code duplication.
110 */
111 int
112 linux_sys_fstat64(l, v, retval)
113 struct lwp *l;
114 void *v;
115 register_t *retval;
116 {
117 struct linux_sys_fstat64_args /* {
118 syscallarg(int) fd;
119 syscallarg(struct linux_stat64 *) sp;
120 } */ *uap = v;
121 struct proc *p = l->l_proc;
122 struct sys___fstat13_args fsa;
123 struct linux_stat64 tmplst;
124 struct stat *st,tmpst;
125 caddr_t sg;
126 int error;
127
128 sg = stackgap_init(p, 0);
129
130 st = stackgap_alloc(p, &sg, sizeof (struct stat));
131
132 SCARG(&fsa, fd) = SCARG(uap, fd);
133 SCARG(&fsa, sb) = st;
134
135 if ((error = sys___fstat13(l, &fsa, retval)))
136 return error;
137
138 if ((error = copyin(st, &tmpst, sizeof tmpst)))
139 return error;
140
141 bsd_to_linux_stat(&tmpst, &tmplst);
142
143 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
144 return error;
145
146 return 0;
147 }
148
149 static int
150 linux_do_stat64(l, v, retval, dolstat)
151 struct lwp *l;
152 void *v;
153 register_t *retval;
154 int dolstat;
155 {
156 struct proc *p = l->l_proc;
157 struct sys___stat13_args sa;
158 struct linux_stat64 tmplst;
159 struct stat *st, tmpst;
160 caddr_t sg;
161 int error;
162 struct linux_sys_stat64_args *uap = v;
163
164 sg = stackgap_init(p, 0);
165 st = stackgap_alloc(p, &sg, sizeof (struct stat));
166 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
167
168 SCARG(&sa, ub) = st;
169 SCARG(&sa, path) = SCARG(uap, path);
170
171 if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
172 sys___stat13(l, &sa, retval))))
173 return error;
174
175 if ((error = copyin(st, &tmpst, sizeof tmpst)))
176 return error;
177
178 bsd_to_linux_stat(&tmpst, &tmplst);
179
180 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
181 return error;
182
183 return 0;
184 }
185
186 int
187 linux_sys_stat64(l, v, retval)
188 struct lwp *l;
189 void *v;
190 register_t *retval;
191 {
192 struct linux_sys_stat64_args /* {
193 syscallarg(const char *) path;
194 syscallarg(struct linux_stat64 *) sp;
195 } */ *uap = v;
196
197 return linux_do_stat64(l, uap, retval, 0);
198 }
199
200 int
201 linux_sys_lstat64(l, v, retval)
202 struct lwp *l;
203 void *v;
204 register_t *retval;
205 {
206 struct linux_sys_lstat64_args /* {
207 syscallarg(const char *) path;
208 syscallarg(struct linux_stat64 *) sp;
209 } */ *uap = v;
210
211 return linux_do_stat64(l, uap, retval, 1);
212 }
213
214 int
215 linux_sys_truncate64(l, v, retval)
216 struct lwp *l;
217 void *v;
218 register_t *retval;
219 {
220 struct linux_sys_truncate64_args /* {
221 syscallarg(const char *) path;
222 syscallarg(off_t) length;
223 } */ *uap = v;
224 struct proc *p = l->l_proc;
225 caddr_t sg = stackgap_init(p, 0);
226
227 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
228
229 return sys_truncate(l, uap, retval);
230 }
231
232 #if defined(__mips__) || defined(__i386__) /* powerpc could use it too */
233 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
234 const struct flock *));
235 static void linux_to_bsd_flock64 __P((struct flock *,
236 const struct linux_flock64 *));
237
238 static void
239 bsd_to_linux_flock64(lfp, bfp)
240 struct linux_flock64 *lfp;
241 const struct flock *bfp;
242 {
243
244 lfp->l_start = bfp->l_start;
245 lfp->l_len = bfp->l_len;
246 lfp->l_pid = bfp->l_pid;
247 lfp->l_whence = bfp->l_whence;
248 switch (bfp->l_type) {
249 case F_RDLCK:
250 lfp->l_type = LINUX_F_RDLCK;
251 break;
252 case F_UNLCK:
253 lfp->l_type = LINUX_F_UNLCK;
254 break;
255 case F_WRLCK:
256 lfp->l_type = LINUX_F_WRLCK;
257 break;
258 }
259 }
260
261 static void
262 linux_to_bsd_flock64(bfp, lfp)
263 struct flock *bfp;
264 const struct linux_flock64 *lfp;
265 {
266
267 bfp->l_start = lfp->l_start;
268 bfp->l_len = lfp->l_len;
269 bfp->l_pid = lfp->l_pid;
270 bfp->l_whence = lfp->l_whence;
271 switch (lfp->l_type) {
272 case LINUX_F_RDLCK:
273 bfp->l_type = F_RDLCK;
274 break;
275 case LINUX_F_UNLCK:
276 bfp->l_type = F_UNLCK;
277 break;
278 case LINUX_F_WRLCK:
279 bfp->l_type = F_WRLCK;
280 break;
281 }
282 }
283 int
284 linux_sys_fcntl64(l, v, retval)
285 struct lwp *l;
286 void *v;
287 register_t *retval;
288 {
289 struct linux_sys_fcntl64_args /* {
290 syscallarg(int) fd;
291 syscallarg(int) cmd;
292 syscallarg(void *) arg;
293 } */ *uap = v;
294 struct proc *p = l->l_proc;
295 struct sys_fcntl_args fca;
296 struct linux_flock64 lfl;
297 struct flock bfl, *bfp;
298 int error;
299 caddr_t sg;
300 void *arg = SCARG(uap, arg);
301 int cmd = SCARG(uap, cmd);
302 int fd = SCARG(uap, fd);
303
304 switch (cmd) {
305 case LINUX_F_GETLK64:
306 sg = stackgap_init(p, 0);
307 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
308 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
309 return error;
310 linux_to_bsd_flock64(&bfl, &lfl);
311 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
312 return error;
313 SCARG(&fca, fd) = fd;
314 SCARG(&fca, cmd) = F_GETLK;
315 SCARG(&fca, arg) = bfp;
316 if ((error = sys_fcntl(l, &fca, retval)) != 0)
317 return error;
318 if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
319 return error;
320 bsd_to_linux_flock64(&lfl, &bfl);
321 return copyout(&lfl, arg, sizeof lfl);
322 case LINUX_F_SETLK64:
323 case LINUX_F_SETLKW64:
324 cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
325 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
326 return error;
327 linux_to_bsd_flock64(&bfl, &lfl);
328 sg = stackgap_init(p, 0);
329 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
330 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
331 return error;
332 SCARG(&fca, fd) = fd;
333 SCARG(&fca, cmd) = cmd;
334 SCARG(&fca, arg) = bfp;
335 return sys_fcntl(l, &fca, retval);
336 default:
337 return linux_sys_fcntl(l, v, retval);
338 }
339
340 return error;
341 }
342 #endif /* __mips__ || __i386__ */
343