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