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