linux_file64.c revision 1.38 1 /* $NetBSD: linux_file64.c,v 1.38 2007/04/22 08:29:57 dsl 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.38 2007/04/22 08:29:57 dsl 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 int error;
158 struct linux_sys_stat64_args *uap = v;
159
160 error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
161 if (error != 0)
162 return error;
163
164 bsd_to_linux_stat(&tmpst, &tmplst);
165
166 return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
167 }
168
169 int
170 linux_sys_stat64(l, v, retval)
171 struct lwp *l;
172 void *v;
173 register_t *retval;
174 {
175 struct linux_sys_stat64_args /* {
176 syscallarg(const char *) path;
177 syscallarg(struct linux_stat64 *) sp;
178 } */ *uap = v;
179
180 return linux_do_stat64(l, uap, retval, FOLLOW);
181 }
182
183 int
184 linux_sys_lstat64(l, v, retval)
185 struct lwp *l;
186 void *v;
187 register_t *retval;
188 {
189 struct linux_sys_lstat64_args /* {
190 syscallarg(const char *) path;
191 syscallarg(struct linux_stat64 *) sp;
192 } */ *uap = v;
193
194 return linux_do_stat64(l, uap, retval, NOFOLLOW);
195 }
196
197 int
198 linux_sys_truncate64(l, v, retval)
199 struct lwp *l;
200 void *v;
201 register_t *retval;
202 {
203 struct linux_sys_truncate64_args /* {
204 syscallarg(const char *) path;
205 syscallarg(off_t) length;
206 } */ *uap = v;
207 struct sys_truncate_args ta;
208
209 /* Linux doesn't have the 'pad' pseudo-parameter */
210 SCARG(&ta, path) = SCARG(uap, path);
211 SCARG(&ta, pad) = 0;
212 SCARG(&ta, length) = SCARG(uap, length);
213
214 return sys_truncate(l, &ta, retval);
215 }
216
217 int
218 linux_sys_ftruncate64(l, v, retval)
219 struct lwp *l;
220 void *v;
221 register_t *retval;
222 {
223 struct linux_sys_ftruncate64_args /* {
224 syscallarg(unsigned int) fd;
225 syscallarg(off_t) length;
226 } */ *uap = v;
227 struct sys_ftruncate_args ta;
228
229 /* Linux doesn't have the 'pad' pseudo-parameter */
230 SCARG(&ta, fd) = SCARG(uap, fd);
231 SCARG(&ta, pad) = 0;
232 SCARG(&ta, length) = SCARG(uap, length);
233
234 return sys_ftruncate(l, &ta, retval);
235 }
236 # endif /* !COMPAT_LINUX32 */
237
238 # if !defined(__m68k__) && (!defined(__amd64__) || defined(COMPAT_LINUX32))
239 static void bsd_to_linux_flock64 __P((struct linux_flock64 *,
240 const struct flock *));
241 static void linux_to_bsd_flock64 __P((struct flock *,
242 const struct linux_flock64 *));
243
244 static void
245 bsd_to_linux_flock64(lfp, bfp)
246 struct linux_flock64 *lfp;
247 const struct flock *bfp;
248 {
249
250 lfp->l_start = bfp->l_start;
251 lfp->l_len = bfp->l_len;
252 lfp->l_pid = bfp->l_pid;
253 lfp->l_whence = bfp->l_whence;
254 switch (bfp->l_type) {
255 case F_RDLCK:
256 lfp->l_type = LINUX_F_RDLCK;
257 break;
258 case F_UNLCK:
259 lfp->l_type = LINUX_F_UNLCK;
260 break;
261 case F_WRLCK:
262 lfp->l_type = LINUX_F_WRLCK;
263 break;
264 }
265 }
266
267 static void
268 linux_to_bsd_flock64(bfp, lfp)
269 struct flock *bfp;
270 const struct linux_flock64 *lfp;
271 {
272
273 bfp->l_start = lfp->l_start;
274 bfp->l_len = lfp->l_len;
275 bfp->l_pid = lfp->l_pid;
276 bfp->l_whence = lfp->l_whence;
277 switch (lfp->l_type) {
278 case LINUX_F_RDLCK:
279 bfp->l_type = F_RDLCK;
280 break;
281 case LINUX_F_UNLCK:
282 bfp->l_type = F_UNLCK;
283 break;
284 case LINUX_F_WRLCK:
285 bfp->l_type = F_WRLCK;
286 break;
287 }
288 }
289
290 int
291 linux_sys_fcntl64(l, v, retval)
292 struct lwp *l;
293 void *v;
294 register_t *retval;
295 {
296 struct linux_sys_fcntl64_args /* {
297 syscallarg(int) fd;
298 syscallarg(int) cmd;
299 syscallarg(void *) arg;
300 } */ *uap = v;
301 struct proc *p = l->l_proc;
302 struct sys_fcntl_args fca;
303 struct linux_flock64 lfl;
304 struct flock bfl, *bfp;
305 int error;
306 void *sg;
307 void *arg = SCARG(uap, arg);
308 int cmd = SCARG(uap, cmd);
309 int fd = SCARG(uap, fd);
310
311 switch (cmd) {
312 case LINUX_F_GETLK64:
313 sg = stackgap_init(p, 0);
314 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
315 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
316 return error;
317 linux_to_bsd_flock64(&bfl, &lfl);
318 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
319 return error;
320 SCARG(&fca, fd) = fd;
321 SCARG(&fca, cmd) = F_GETLK;
322 SCARG(&fca, arg) = bfp;
323 if ((error = sys_fcntl(l, &fca, retval)) != 0)
324 return error;
325 if ((error = copyin(bfp, &bfl, sizeof bfl)) != 0)
326 return error;
327 bsd_to_linux_flock64(&lfl, &bfl);
328 return copyout(&lfl, arg, sizeof lfl);
329 case LINUX_F_SETLK64:
330 case LINUX_F_SETLKW64:
331 cmd = (cmd == LINUX_F_SETLK64 ? F_SETLK : F_SETLKW);
332 if ((error = copyin(arg, &lfl, sizeof lfl)) != 0)
333 return error;
334 linux_to_bsd_flock64(&bfl, &lfl);
335 sg = stackgap_init(p, 0);
336 bfp = (struct flock *) stackgap_alloc(p, &sg, sizeof *bfp);
337 if ((error = copyout(&bfl, bfp, sizeof bfl)) != 0)
338 return error;
339 SCARG(&fca, fd) = fd;
340 SCARG(&fca, cmd) = cmd;
341 SCARG(&fca, arg) = bfp;
342 return sys_fcntl(l, &fca, retval);
343 default:
344 return linux_sys_fcntl(l, v, retval);
345 }
346 }
347 # endif /* !m69k && !amd64 && !COMPAT_LINUX32 */
348
349 #endif /* !alpha */
350
351 /*
352 * Linux 'readdir' call. This code is mostly taken from the
353 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
354 * an attempt has been made to keep it a little cleaner.
355 *
356 * The d_off field contains the offset of the next valid entry,
357 * unless the older Linux getdents(2), which used to have it set
358 * to the offset of the entry itself. This function also doesn't
359 * need to deal with the old count == 1 glibc problem.
360 *
361 * Read in BSD-style entries, convert them, and copy them out.
362 *
363 * Note that this doesn't handle union-mounted filesystems.
364 */
365 #ifndef COMPAT_LINUX32
366 int
367 linux_sys_getdents64(l, v, retval)
368 struct lwp *l;
369 void *v;
370 register_t *retval;
371 {
372 struct linux_sys_getdents_args /* {
373 syscallarg(int) fd;
374 syscallarg(struct linux_dirent64 *) dent;
375 syscallarg(unsigned int) count;
376 } */ *uap = v;
377 struct dirent *bdp;
378 struct vnode *vp;
379 char *inp, *tbuf; /* BSD-format */
380 int len, reclen; /* BSD-format */
381 char *outp; /* Linux-format */
382 int resid, linux_reclen = 0; /* Linux-format */
383 struct file *fp;
384 struct uio auio;
385 struct iovec aiov;
386 struct linux_dirent64 idb;
387 off_t off; /* true file offset */
388 int buflen, error, eofflag, nbytes;
389 struct vattr va;
390 off_t *cookiebuf = NULL, *cookie;
391 int ncookies;
392
393 /* getvnode() will use the descriptor for us */
394 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
395 return (error);
396
397 if ((fp->f_flag & FREAD) == 0) {
398 error = EBADF;
399 goto out1;
400 }
401
402 vp = (struct vnode *)fp->f_data;
403 if (vp->v_type != VDIR) {
404 error = EINVAL;
405 goto out1;
406 }
407
408 if ((error = VOP_GETATTR(vp, &va, l->l_cred, l)))
409 goto out1;
410
411 nbytes = SCARG(uap, count);
412 buflen = min(MAXBSIZE, nbytes);
413 if (buflen < va.va_blocksize)
414 buflen = va.va_blocksize;
415 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
416
417 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
418 off = fp->f_offset;
419 again:
420 aiov.iov_base = tbuf;
421 aiov.iov_len = buflen;
422 auio.uio_iov = &aiov;
423 auio.uio_iovcnt = 1;
424 auio.uio_rw = UIO_READ;
425 auio.uio_resid = buflen;
426 auio.uio_offset = off;
427 UIO_SETUP_SYSSPACE(&auio);
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 = tbuf;
438 outp = (void *)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 if (cookie)
451 off = *cookie++;
452 else
453 off += reclen;
454 continue;
455 }
456 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
457 if (reclen > len || resid < linux_reclen) {
458 /* entry too big for buffer, so just stop */
459 outp++;
460 break;
461 }
462 if (cookie)
463 off = *cookie++; /* each entry points to next */
464 else
465 off += reclen;
466 /*
467 * Massage in place to make a Linux-shaped dirent (otherwise
468 * we have to worry about touching user memory outside of
469 * the copyout() call).
470 */
471 idb.d_ino = bdp->d_fileno;
472 idb.d_type = bdp->d_type;
473 idb.d_off = off;
474 idb.d_reclen = (u_short)linux_reclen;
475 strcpy(idb.d_name, bdp->d_name);
476 if ((error = copyout((void *)&idb, outp, linux_reclen)))
477 goto out;
478 /* advance past this real entry */
479 inp += reclen;
480 /* advance output past Linux-shaped entry */
481 outp += linux_reclen;
482 resid -= linux_reclen;
483 }
484
485 /* if we squished out the whole block, try again */
486 if (outp == (void *)SCARG(uap, dent))
487 goto again;
488 fp->f_offset = off; /* update the vnode offset */
489
490 eof:
491 *retval = nbytes - resid;
492 out:
493 VOP_UNLOCK(vp, 0);
494 if (cookiebuf)
495 free(cookiebuf, M_TEMP);
496 free(tbuf, M_TEMP);
497 out1:
498 FILE_UNUSE(fp, l);
499 return error;
500 }
501 #endif /* !COMPAT_LINUX32 */
502