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