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