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