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