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