linux_file64.c revision 1.15 1 /* $NetBSD: linux_file64.c,v 1.15 2002/05/13 05:41:27 matt 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.15 2002/05/13 05:41:27 matt 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/dirent.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/filedesc.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/mount.h>
57 #include <sys/malloc.h>
58 #include <sys/vnode.h>
59 #include <sys/tty.h>
60 #include <sys/conf.h>
61
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 #include <compat/linux/common/linux_dirent.h>
70
71 #include <compat/linux/linux_syscallargs.h>
72
73 #ifndef alpha
74
75 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
76 static int linux_do_stat64 __P((struct proc *, void *, register_t *, int));
77
78 /*
79 * Convert a NetBSD stat structure to a Linux stat structure.
80 * Only the order of the fields and the padding in the structure
81 * is different. linux_fakedev is a machine-dependent function
82 * which optionally converts device driver major/minor numbers
83 * (XXX horrible, but what can you do against code that compares
84 * things against constant major device numbers? sigh)
85 */
86 static void
87 bsd_to_linux_stat(bsp, lsp)
88 struct stat *bsp;
89 struct linux_stat64 *lsp;
90 {
91 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
92 lsp->lst_ino = bsp->st_ino;
93 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
94 if (bsp->st_nlink >= (1 << 15))
95 lsp->lst_nlink = (1 << 15) - 1;
96 else
97 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
98 lsp->lst_uid = bsp->st_uid;
99 lsp->lst_gid = bsp->st_gid;
100 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
101 lsp->lst_size = bsp->st_size;
102 lsp->lst_blksize = bsp->st_blksize;
103 lsp->lst_blocks = bsp->st_blocks;
104 lsp->lst_atime = bsp->st_atime;
105 lsp->lst_mtime = bsp->st_mtime;
106 lsp->lst_ctime = bsp->st_ctime;
107 lsp->lst_ino64 = bsp->st_ino;
108 }
109
110 /*
111 * The stat functions below are plain sailing. stat and lstat are handled
112 * by one function to avoid code duplication.
113 */
114 int
115 linux_sys_fstat64(p, v, retval)
116 struct proc *p;
117 void *v;
118 register_t *retval;
119 {
120 struct linux_sys_fstat64_args /* {
121 syscallarg(int) fd;
122 syscallarg(struct linux_stat64 *) sp;
123 } */ *uap = v;
124 struct sys___fstat13_args fsa;
125 struct linux_stat64 tmplst;
126 struct stat *st,tmpst;
127 caddr_t sg;
128 int error;
129
130 sg = stackgap_init(p, 0);
131
132 st = stackgap_alloc(p, &sg, sizeof (struct stat));
133
134 SCARG(&fsa, fd) = SCARG(uap, fd);
135 SCARG(&fsa, sb) = st;
136
137 if ((error = sys___fstat13(p, &fsa, retval)))
138 return error;
139
140 if ((error = copyin(st, &tmpst, sizeof tmpst)))
141 return error;
142
143 bsd_to_linux_stat(&tmpst, &tmplst);
144
145 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
146 return error;
147
148 return 0;
149 }
150
151 static int
152 linux_do_stat64(p, v, retval, dolstat)
153 struct proc *p;
154 void *v;
155 register_t *retval;
156 int dolstat;
157 {
158 struct sys___stat13_args sa;
159 struct linux_stat64 tmplst;
160 struct stat *st, tmpst;
161 caddr_t sg;
162 int error;
163 struct linux_sys_stat64_args *uap = v;
164
165 sg = stackgap_init(p, 0);
166 st = stackgap_alloc(p, &sg, sizeof (struct stat));
167 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
168
169 SCARG(&sa, ub) = st;
170 SCARG(&sa, path) = SCARG(uap, path);
171
172 if ((error = (dolstat ? sys___lstat13(p, &sa, retval) :
173 sys___stat13(p, &sa, retval))))
174 return error;
175
176 if ((error = copyin(st, &tmpst, sizeof tmpst)))
177 return error;
178
179 bsd_to_linux_stat(&tmpst, &tmplst);
180
181 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
182 return error;
183
184 return 0;
185 }
186
187 int
188 linux_sys_stat64(p, v, retval)
189 struct proc *p;
190 void *v;
191 register_t *retval;
192 {
193 struct linux_sys_stat64_args /* {
194 syscallarg(const char *) path;
195 syscallarg(struct linux_stat64 *) sp;
196 } */ *uap = v;
197
198 return linux_do_stat64(p, uap, retval, 0);
199 }
200
201 int
202 linux_sys_lstat64(p, v, retval)
203 struct proc *p;
204 void *v;
205 register_t *retval;
206 {
207 struct linux_sys_lstat64_args /* {
208 syscallarg(const char *) path;
209 syscallarg(struct linux_stat64 *) sp;
210 } */ *uap = v;
211
212 return linux_do_stat64(p, uap, retval, 1);
213 }
214
215 int
216 linux_sys_truncate64(p, v, retval)
217 struct proc *p;
218 void *v;
219 register_t *retval;
220 {
221 struct linux_sys_truncate64_args /* {
222 syscallarg(const char *) path;
223 syscallarg(off_t) length;
224 } */ *uap = v;
225 caddr_t sg = stackgap_init(p, 0);
226
227 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
228
229 return sys_truncate(p, uap, retval);
230 }
231
232 #if !defined(__m68k__)
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
284 int
285 linux_sys_fcntl64(p, v, retval)
286 struct proc *p;
287 void *v;
288 register_t *retval;
289 {
290 struct linux_sys_fcntl64_args /* {
291 syscallarg(int) fd;
292 syscallarg(int) cmd;
293 syscallarg(void *) arg;
294 } */ *uap = v;
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(p, &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(p, &fca, retval);
336 default:
337 return linux_sys_fcntl(p, v, retval);
338 }
339 }
340 #endif /* !m68k */
341
342 #endif /* !alpha */
343
344 /*
345 * Linux 'readdir' call. This code is mostly taken from the
346 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
347 * an attempt has been made to keep it a little cleaner (failing
348 * miserably, because of the cruft needed if count 1 is passed).
349 *
350 * The d_off field should contain the offset of the next valid entry,
351 * but in Linux it has the offset of the entry itself. We emulate
352 * that bug here.
353 *
354 * Read in BSD-style entries, convert them, and copy them out.
355 *
356 * Note that this doesn't handle union-mounted filesystems.
357 */
358 int
359 linux_sys_getdents64(p, v, retval)
360 struct proc *p;
361 void *v;
362 register_t *retval;
363 {
364 struct linux_sys_getdents_args /* {
365 syscallarg(int) fd;
366 syscallarg(struct linux_dirent64 *) dent;
367 syscallarg(unsigned int) count;
368 } */ *uap = v;
369 struct dirent *bdp;
370 struct vnode *vp;
371 caddr_t inp, buf; /* BSD-format */
372 int len, reclen; /* BSD-format */
373 caddr_t outp; /* Linux-format */
374 int resid, linux_reclen = 0; /* Linux-format */
375 struct file *fp;
376 struct uio auio;
377 struct iovec aiov;
378 struct linux_dirent64 idb;
379 off_t off; /* true file offset */
380 int buflen, error, eofflag, nbytes, oldcall;
381 struct vattr va;
382 off_t *cookiebuf = NULL, *cookie;
383 int ncookies;
384
385 /* getvnode() will use the descriptor for us */
386 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
387 return (error);
388
389 if ((fp->f_flag & FREAD) == 0) {
390 error = EBADF;
391 goto out1;
392 }
393
394 vp = (struct vnode *)fp->f_data;
395 if (vp->v_type != VDIR) {
396 error = EINVAL;
397 goto out1;
398 }
399
400 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
401 goto out1;
402
403 nbytes = SCARG(uap, count);
404 if (nbytes == 1) { /* emulating old, broken behaviour */
405 nbytes = sizeof (idb);
406 buflen = max(va.va_blocksize, nbytes);
407 oldcall = 1;
408 } else {
409 buflen = min(MAXBSIZE, nbytes);
410 if (buflen < va.va_blocksize)
411 buflen = va.va_blocksize;
412 oldcall = 0;
413 }
414 buf = malloc(buflen, M_TEMP, M_WAITOK);
415
416 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
417 off = fp->f_offset;
418 again:
419 aiov.iov_base = buf;
420 aiov.iov_len = buflen;
421 auio.uio_iov = &aiov;
422 auio.uio_iovcnt = 1;
423 auio.uio_rw = UIO_READ;
424 auio.uio_segflg = UIO_SYSSPACE;
425 auio.uio_procp = p;
426 auio.uio_resid = buflen;
427 auio.uio_offset = off;
428 /*
429 * First we read into the malloc'ed buffer, then
430 * we massage it into user space, one record at a time.
431 */
432 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
433 &ncookies);
434 if (error)
435 goto out;
436
437 inp = buf;
438 outp = (caddr_t)SCARG(uap, dent);
439 resid = nbytes;
440 if ((len = buflen - auio.uio_resid) == 0)
441 goto eof;
442
443 for (cookie = cookiebuf; len > 0; len -= reclen) {
444 bdp = (struct dirent *)inp;
445 reclen = bdp->d_reclen;
446 if (reclen & 3)
447 panic("linux_readdir");
448 if (bdp->d_fileno == 0) {
449 inp += reclen; /* it is a hole; squish it out */
450 off = *cookie++;
451 continue;
452 }
453 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
454 if (reclen > len || resid < linux_reclen) {
455 /* entry too big for buffer, so just stop */
456 outp++;
457 break;
458 }
459 /*
460 * Massage in place to make a Linux-shaped dirent (otherwise
461 * we have to worry about touching user memory outside of
462 * the copyout() call).
463 */
464 idb.d_ino = bdp->d_fileno;
465 idb.d_type = bdp->d_type;
466 /*
467 * The old readdir() call misuses the offset and reclen fields.
468 */
469 if (oldcall) {
470 idb.d_off = linux_reclen;
471 idb.d_reclen = (u_short)bdp->d_namlen;
472 } else {
473 idb.d_off = off;
474 idb.d_reclen = (u_short)linux_reclen;
475 }
476 strcpy(idb.d_name, bdp->d_name);
477 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
478 goto out;
479 /* advance past this real entry */
480 inp += reclen;
481 off = *cookie++; /* each entry points to itself */
482 /* advance output past Linux-shaped entry */
483 outp += linux_reclen;
484 resid -= linux_reclen;
485 if (oldcall)
486 break;
487 }
488
489 /* if we squished out the whole block, try again */
490 if (outp == (caddr_t)SCARG(uap, dent))
491 goto again;
492 fp->f_offset = off; /* update the vnode offset */
493
494 if (oldcall)
495 nbytes = resid + linux_reclen;
496
497 eof:
498 *retval = nbytes - resid;
499 out:
500 VOP_UNLOCK(vp, 0);
501 if (cookiebuf)
502 free(cookiebuf, M_TEMP);
503 free(buf, M_TEMP);
504 out1:
505 FILE_UNUSE(fp, p);
506 return error;
507 }
508