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