linux_file64.c revision 1.25 1 /* $NetBSD: linux_file64.c,v 1.25 2005/03/10 14:12:28 christos 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.25 2005/03/10 14:12:28 christos 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/sa.h>
63 #include <sys/syscallargs.h>
64
65 #include <compat/linux/common/linux_types.h>
66 #include <compat/linux/common/linux_signal.h>
67 #include <compat/linux/common/linux_fcntl.h>
68 #include <compat/linux/common/linux_util.h>
69 #include <compat/linux/common/linux_machdep.h>
70 #include <compat/linux/common/linux_dirent.h>
71
72 #include <compat/linux/linux_syscallargs.h>
73
74 #ifndef alpha
75
76 static void bsd_to_linux_stat __P((struct stat *, struct linux_stat64 *));
77 static int linux_do_stat64 __P((struct lwp *, void *, register_t *, int));
78
79 /*
80 * Convert a NetBSD stat structure to a Linux stat structure.
81 * Only the order of the fields and the padding in the structure
82 * is different. linux_fakedev is a machine-dependent function
83 * which optionally converts device driver major/minor numbers
84 * (XXX horrible, but what can you do against code that compares
85 * things against constant major device numbers? sigh)
86 */
87 static void
88 bsd_to_linux_stat(bsp, lsp)
89 struct stat *bsp;
90 struct linux_stat64 *lsp;
91 {
92 lsp->lst_dev = linux_fakedev(bsp->st_dev, 0);
93 lsp->lst_ino = bsp->st_ino;
94 lsp->lst_mode = (linux_mode_t)bsp->st_mode;
95 if (bsp->st_nlink >= (1 << 15))
96 lsp->lst_nlink = (1 << 15) - 1;
97 else
98 lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
99 lsp->lst_uid = bsp->st_uid;
100 lsp->lst_gid = bsp->st_gid;
101 lsp->lst_rdev = linux_fakedev(bsp->st_rdev, 1);
102 lsp->lst_size = bsp->st_size;
103 lsp->lst_blksize = bsp->st_blksize;
104 lsp->lst_blocks = bsp->st_blocks;
105 lsp->lst_atime = bsp->st_atime;
106 lsp->lst_mtime = bsp->st_mtime;
107 lsp->lst_ctime = bsp->st_ctime;
108 #ifdef LINUX_STAT64_HAS_NSEC
109 lsp->lst_atime_nsec = bsp->st_atimensec;
110 lsp->lst_mtime_nsec = bsp->st_mtimensec;
111 lsp->lst_ctime_nsec = bsp->st_ctimensec;
112 #endif
113 #if LINUX_STAT64_HAS_BROKEN_ST_INO
114 lsp->__lst_ino = (linux_ino_t) bsp->st_ino;
115 #endif
116 }
117
118 /*
119 * The stat functions below are plain sailing. stat and lstat are handled
120 * by one function to avoid code duplication.
121 */
122 int
123 linux_sys_fstat64(l, v, retval)
124 struct lwp *l;
125 void *v;
126 register_t *retval;
127 {
128 struct linux_sys_fstat64_args /* {
129 syscallarg(int) fd;
130 syscallarg(struct linux_stat64 *) sp;
131 } */ *uap = v;
132 struct proc *p = l->l_proc;
133 struct sys___fstat13_args fsa;
134 struct linux_stat64 tmplst;
135 struct stat *st,tmpst;
136 caddr_t sg;
137 int error;
138
139 sg = stackgap_init(p, 0);
140
141 st = stackgap_alloc(p, &sg, sizeof (struct stat));
142
143 SCARG(&fsa, fd) = SCARG(uap, fd);
144 SCARG(&fsa, sb) = st;
145
146 if ((error = sys___fstat13(l, &fsa, retval)))
147 return error;
148
149 if ((error = copyin(st, &tmpst, sizeof tmpst)))
150 return error;
151
152 bsd_to_linux_stat(&tmpst, &tmplst);
153
154 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
155 return error;
156
157 return 0;
158 }
159
160 static int
161 linux_do_stat64(l, v, retval, dolstat)
162 struct lwp *l;
163 void *v;
164 register_t *retval;
165 int dolstat;
166 {
167 struct proc *p = l->l_proc;
168 struct sys___stat13_args sa;
169 struct linux_stat64 tmplst;
170 struct stat *st, tmpst;
171 caddr_t sg;
172 int error;
173 struct linux_sys_stat64_args *uap = v;
174
175 sg = stackgap_init(p, 0);
176 st = stackgap_alloc(p, &sg, sizeof (struct stat));
177 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
178
179 SCARG(&sa, ub) = st;
180 SCARG(&sa, path) = SCARG(uap, path);
181
182 if ((error = (dolstat ? sys___lstat13(l, &sa, retval) :
183 sys___stat13(l, &sa, retval))))
184 return error;
185
186 if ((error = copyin(st, &tmpst, sizeof tmpst)))
187 return error;
188
189 bsd_to_linux_stat(&tmpst, &tmplst);
190
191 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
192 return error;
193
194 return 0;
195 }
196
197 int
198 linux_sys_stat64(l, v, retval)
199 struct lwp *l;
200 void *v;
201 register_t *retval;
202 {
203 struct linux_sys_stat64_args /* {
204 syscallarg(const char *) path;
205 syscallarg(struct linux_stat64 *) sp;
206 } */ *uap = v;
207
208 return linux_do_stat64(l, uap, retval, 0);
209 }
210
211 int
212 linux_sys_lstat64(l, v, retval)
213 struct lwp *l;
214 void *v;
215 register_t *retval;
216 {
217 struct linux_sys_lstat64_args /* {
218 syscallarg(const char *) path;
219 syscallarg(struct linux_stat64 *) sp;
220 } */ *uap = v;
221
222 return linux_do_stat64(l, uap, retval, 1);
223 }
224
225 int
226 linux_sys_truncate64(l, v, retval)
227 struct lwp *l;
228 void *v;
229 register_t *retval;
230 {
231 struct linux_sys_truncate64_args /* {
232 syscallarg(const char *) path;
233 syscallarg(off_t) length;
234 } */ *uap = v;
235 struct sys_truncate_args ta;
236 struct proc *p = l->l_proc;
237 caddr_t sg = stackgap_init(p, 0);
238
239 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
240
241 /* Linux doesn't have the 'pad' pseudo-parameter */
242 SCARG(&ta, path) = SCARG(uap, path);
243 SCARG(&ta, pad) = 0;
244 SCARG(&ta, length) = SCARG(uap, length);
245
246 return sys_truncate(l, &ta, retval);
247 }
248
249 int
250 linux_sys_ftruncate64(l, v, retval)
251 struct lwp *l;
252 void *v;
253 register_t *retval;
254 {
255 struct linux_sys_ftruncate64_args /* {
256 syscallarg(unsigned int) fd;
257 syscallarg(off_t) length;
258 } */ *uap = v;
259 struct sys_ftruncate_args ta;
260
261 /* Linux doesn't have the 'pad' pseudo-parameter */
262 SCARG(&ta, fd) = SCARG(uap, fd);
263 SCARG(&ta, pad) = 0;
264 SCARG(&ta, length) = SCARG(uap, length);
265
266 return sys_ftruncate(l, &ta, retval);
267 }
268
269 #if !defined(__m68k__)
270 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
271 const struct flock *));
272 static void linux_to_bsd_flock64 __P((struct flock *,
273 const struct linux_flock64 *));
274
275 static void
276 bsd_to_linux_flock64(lfp, bfp)
277 struct linux_flock64 *lfp;
278 const struct flock *bfp;
279 {
280
281 lfp->l_start = bfp->l_start;
282 lfp->l_len = bfp->l_len;
283 lfp->l_pid = bfp->l_pid;
284 lfp->l_whence = bfp->l_whence;
285 switch (bfp->l_type) {
286 case F_RDLCK:
287 lfp->l_type = LINUX_F_RDLCK;
288 break;
289 case F_UNLCK:
290 lfp->l_type = LINUX_F_UNLCK;
291 break;
292 case F_WRLCK:
293 lfp->l_type = LINUX_F_WRLCK;
294 break;
295 }
296 }
297
298 static void
299 linux_to_bsd_flock64(bfp, lfp)
300 struct flock *bfp;
301 const struct linux_flock64 *lfp;
302 {
303
304 bfp->l_start = lfp->l_start;
305 bfp->l_len = lfp->l_len;
306 bfp->l_pid = lfp->l_pid;
307 bfp->l_whence = lfp->l_whence;
308 switch (lfp->l_type) {
309 case LINUX_F_RDLCK:
310 bfp->l_type = F_RDLCK;
311 break;
312 case LINUX_F_UNLCK:
313 bfp->l_type = F_UNLCK;
314 break;
315 case LINUX_F_WRLCK:
316 bfp->l_type = F_WRLCK;
317 break;
318 }
319 }
320
321 int
322 linux_sys_fcntl64(l, v, retval)
323 struct lwp *l;
324 void *v;
325 register_t *retval;
326 {
327 struct linux_sys_fcntl64_args /* {
328 syscallarg(int) fd;
329 syscallarg(int) cmd;
330 syscallarg(void *) arg;
331 } */ *uap = v;
332 struct proc *p = l->l_proc;
333 struct sys_fcntl_args fca;
334 struct linux_flock64 lfl;
335 struct flock bfl, *bfp;
336 int error;
337 caddr_t sg;
338 void *arg = SCARG(uap, arg);
339 int cmd = SCARG(uap, cmd);
340 int fd = SCARG(uap, fd);
341
342 switch (cmd) {
343 case LINUX_F_GETLK64:
344 sg = stackgap_init(p, 0);
345 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
346 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
347 return error;
348 linux_to_bsd_flock64(&bfl, &lfl);
349 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
350 return error;
351 SCARG(&fca, fd) = fd;
352 SCARG(&fca, cmd) = F_GETLK;
353 SCARG(&fca, arg) = bfp;
354 if ((error = sys_fcntl(l, &fca, retval)) != 0)
355 return error;
356 if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
357 return error;
358 bsd_to_linux_flock64(&lfl, &bfl);
359 return copyout(&lfl, arg, sizeof lfl);
360 case LINUX_F_SETLK64:
361 case LINUX_F_SETLKW64:
362 cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
363 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
364 return error;
365 linux_to_bsd_flock64(&bfl, &lfl);
366 sg = stackgap_init(p, 0);
367 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
368 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
369 return error;
370 SCARG(&fca, fd) = fd;
371 SCARG(&fca, cmd) = cmd;
372 SCARG(&fca, arg) = bfp;
373 return sys_fcntl(l, &fca, retval);
374 default:
375 return linux_sys_fcntl(l, v, retval);
376 }
377 }
378 #endif /* !m68k */
379
380 #endif /* !alpha */
381
382 /*
383 * Linux 'readdir' call. This code is mostly taken from the
384 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
385 * an attempt has been made to keep it a little cleaner.
386 *
387 * The d_off field contains the offset of the next valid entry,
388 * unless the older Linux getdents(2), which used to have it set
389 * to the offset of the entry itself. This function also doesn't
390 * need to deal with the old count == 1 glibc problem.
391 *
392 * Read in BSD-style entries, convert them, and copy them out.
393 *
394 * Note that this doesn't handle union-mounted filesystems.
395 */
396 int
397 linux_sys_getdents64(l, v, retval)
398 struct lwp *l;
399 void *v;
400 register_t *retval;
401 {
402 struct linux_sys_getdents_args /* {
403 syscallarg(int) fd;
404 syscallarg(struct linux_dirent64 *) dent;
405 syscallarg(unsigned int) count;
406 } */ *uap = v;
407 struct proc *p = l->l_proc;
408 struct dirent *bdp;
409 struct vnode *vp;
410 caddr_t inp, buf; /* BSD-format */
411 int len, reclen; /* BSD-format */
412 caddr_t outp; /* Linux-format */
413 int resid, linux_reclen = 0; /* Linux-format */
414 struct file *fp;
415 struct uio auio;
416 struct iovec aiov;
417 struct linux_dirent64 idb;
418 off_t off; /* true file offset */
419 int buflen, error, eofflag, nbytes;
420 struct vattr va;
421 off_t *cookiebuf = NULL, *cookie;
422 int ncookies;
423
424 /* getvnode() will use the descriptor for us */
425 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
426 return (error);
427
428 if ((fp->f_flag & FREAD) == 0) {
429 error = EBADF;
430 goto out1;
431 }
432
433 vp = (struct vnode *)fp->f_data;
434 if (vp->v_type != VDIR) {
435 error = EINVAL;
436 goto out1;
437 }
438
439 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
440 goto out1;
441
442 nbytes = SCARG(uap, count);
443 buflen = min(MAXBSIZE, nbytes);
444 if (buflen < va.va_blocksize)
445 buflen = va.va_blocksize;
446 buf = malloc(buflen, M_TEMP, M_WAITOK);
447
448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
449 off = fp->f_offset;
450 again:
451 aiov.iov_base = buf;
452 aiov.iov_len = buflen;
453 auio.uio_iov = &aiov;
454 auio.uio_iovcnt = 1;
455 auio.uio_rw = UIO_READ;
456 auio.uio_segflg = UIO_SYSSPACE;
457 auio.uio_procp = NULL;
458 auio.uio_resid = buflen;
459 auio.uio_offset = off;
460 /*
461 * First we read into the malloc'ed buffer, then
462 * we massage it into user space, one record at a time.
463 */
464 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
465 &ncookies);
466 if (error)
467 goto out;
468
469 inp = buf;
470 outp = (caddr_t)SCARG(uap, dent);
471 resid = nbytes;
472 if ((len = buflen - auio.uio_resid) == 0)
473 goto eof;
474
475 for (cookie = cookiebuf; len > 0; len -= reclen) {
476 bdp = (struct dirent *)inp;
477 reclen = bdp->d_reclen;
478 if (reclen & 3)
479 panic("linux_readdir");
480 if (bdp->d_fileno == 0) {
481 inp += reclen; /* it is a hole; squish it out */
482 off = *cookie++;
483 continue;
484 }
485 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
486 if (reclen > len || resid < linux_reclen) {
487 /* entry too big for buffer, so just stop */
488 outp++;
489 break;
490 }
491 off = *cookie++; /* each entry points to next */
492 /*
493 * Massage in place to make a Linux-shaped dirent (otherwise
494 * we have to worry about touching user memory outside of
495 * the copyout() call).
496 */
497 idb.d_ino = bdp->d_fileno;
498 idb.d_type = bdp->d_type;
499 idb.d_off = off;
500 idb.d_reclen = (u_short)linux_reclen;
501 strcpy(idb.d_name, bdp->d_name);
502 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
503 goto out;
504 /* advance past this real entry */
505 inp += reclen;
506 /* advance output past Linux-shaped entry */
507 outp += linux_reclen;
508 resid -= linux_reclen;
509 }
510
511 /* if we squished out the whole block, try again */
512 if (outp == (caddr_t)SCARG(uap, dent))
513 goto again;
514 fp->f_offset = off; /* update the vnode offset */
515
516 eof:
517 *retval = nbytes - resid;
518 out:
519 VOP_UNLOCK(vp, 0);
520 if (cookiebuf)
521 free(cookiebuf, M_TEMP);
522 free(buf, M_TEMP);
523 out1:
524 FILE_UNUSE(fp, p);
525 return error;
526 }
527