linux_file.c revision 111798
1251876Speter/*-
2251876Speter * Copyright (c) 1994-1995 S�ren Schmidt
3251876Speter * All rights reserved.
4251876Speter *
5251876Speter * Redistribution and use in source and binary forms, with or without
6251876Speter * modification, are permitted provided that the following conditions
7251876Speter * are met:
8251876Speter * 1. Redistributions of source code must retain the above copyright
9251876Speter *    notice, this list of conditions and the following disclaimer
10251876Speter *    in this position and unchanged.
11251876Speter * 2. Redistributions in binary form must reproduce the above copyright
12251876Speter *    notice, this list of conditions and the following disclaimer in the
13251876Speter *    documentation and/or other materials provided with the distribution.
14251876Speter * 3. The name of the author may not be used to endorse or promote products
15251876Speter *    derived from this software without specific prior written permission
16251876Speter *
17251876Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18251876Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19251876Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20251876Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21251876Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22251876Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23251876Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24251876Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25251876Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26251876Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27251876Speter *
28251876Speter * $FreeBSD: head/sys/compat/linux/linux_file.c 111798 2003-03-03 09:17:12Z des $
29251876Speter */
30251876Speter
31251876Speter#include "opt_compat.h"
32251876Speter#include "opt_mac.h"
33251876Speter
34251876Speter#include <sys/param.h>
35251876Speter#include <sys/systm.h>
36251876Speter#include <sys/conf.h>
37251876Speter#include <sys/dirent.h>
38251876Speter#include <sys/fcntl.h>
39251876Speter#include <sys/file.h>
40251876Speter#include <sys/filedesc.h>
41251876Speter#include <sys/lock.h>
42251876Speter#include <sys/mac.h>
43251876Speter#include <sys/malloc.h>
44251876Speter#include <sys/mount.h>
45251876Speter#include <sys/mutex.h>
46251876Speter#include <sys/proc.h>
47251876Speter#include <sys/syscallsubr.h>
48251876Speter#include <sys/sysproto.h>
49251876Speter#include <sys/tty.h>
50251876Speter#include <sys/vnode.h>
51251876Speter
52251876Speter#include <ufs/ufs/extattr.h>
53251876Speter#include <ufs/ufs/quota.h>
54251876Speter#include <ufs/ufs/ufsmount.h>
55251876Speter
56251876Speter#include <machine/../linux/linux.h>
57251876Speter#include <machine/../linux/linux_proto.h>
58251876Speter#include <compat/linux/linux_util.h>
59251876Speter
60251876Speter#ifndef __alpha__
61251876Speterint
62251876Speterlinux_creat(struct thread *td, struct linux_creat_args *args)
63251876Speter{
64251876Speter    char *path;
65251876Speter    int error;
66251876Speter
67251876Speter    LCONVPATHEXIST(td, args->path, &path);
68251876Speter
69251876Speter#ifdef DEBUG
70251876Speter	if (ldebug(creat))
71251876Speter		printf(ARGS(creat, "%s, %d"), path, args->mode);
72251876Speter#endif
73251876Speter    error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
74251876Speter	args->mode);
75251876Speter    LFREEPATH(path);
76251876Speter    return (error);
77251876Speter}
78251876Speter#endif /*!__alpha__*/
79251876Speter
80251876Speterint
81251876Speterlinux_open(struct thread *td, struct linux_open_args *args)
82251876Speter{
83251876Speter    struct proc *p = td->td_proc;
84251876Speter    char *path;
85251876Speter    int bsd_flags, error;
86251876Speter
87251876Speter    if (args->flags & LINUX_O_CREAT)
88251876Speter	LCONVPATHCREAT(td, args->path, &path);
89251876Speter    else
90251876Speter	LCONVPATHEXIST(td, args->path, &path);
91251876Speter
92251876Speter#ifdef DEBUG
93251876Speter	if (ldebug(open))
94251876Speter		printf(ARGS(open, "%s, 0x%x, 0x%x"),
95251876Speter		    path, args->flags, args->mode);
96251876Speter#endif
97251876Speter    bsd_flags = 0;
98251876Speter    if (args->flags & LINUX_O_RDONLY)
99251876Speter	bsd_flags |= O_RDONLY;
100251876Speter    if (args->flags & LINUX_O_WRONLY)
101251876Speter	bsd_flags |= O_WRONLY;
102251876Speter    if (args->flags & LINUX_O_RDWR)
103251876Speter	bsd_flags |= O_RDWR;
104251876Speter    if (args->flags & LINUX_O_NDELAY)
105251876Speter	bsd_flags |= O_NONBLOCK;
106251876Speter    if (args->flags & LINUX_O_APPEND)
107251876Speter	bsd_flags |= O_APPEND;
108251876Speter    if (args->flags & LINUX_O_SYNC)
109251876Speter	bsd_flags |= O_FSYNC;
110251876Speter    if (args->flags & LINUX_O_NONBLOCK)
111251876Speter	bsd_flags |= O_NONBLOCK;
112251876Speter    if (args->flags & LINUX_FASYNC)
113251876Speter	bsd_flags |= O_ASYNC;
114251876Speter    if (args->flags & LINUX_O_CREAT)
115251876Speter	bsd_flags |= O_CREAT;
116251876Speter    if (args->flags & LINUX_O_TRUNC)
117251876Speter	bsd_flags |= O_TRUNC;
118251876Speter    if (args->flags & LINUX_O_EXCL)
119251876Speter	bsd_flags |= O_EXCL;
120251876Speter    if (args->flags & LINUX_O_NOCTTY)
121251876Speter	bsd_flags |= O_NOCTTY;
122251876Speter
123251876Speter    error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
124251876Speter    PROC_LOCK(p);
125251876Speter    if (!error && !(bsd_flags & O_NOCTTY) &&
126251876Speter	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
127251876Speter	struct file *fp;
128251876Speter
129251876Speter	error = fget(td, td->td_retval[0], &fp);
130251876Speter	PROC_UNLOCK(p);
131251876Speter	if (!error) {
132251876Speter		if (fp->f_type == DTYPE_VNODE)
133251876Speter			fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
134251876Speter			    td);
135251876Speter	    fdrop(fp, td);
136251876Speter	}
137251876Speter    } else {
138251876Speter	PROC_UNLOCK(p);
139251876Speter#ifdef DEBUG
140251876Speter	if (ldebug(open))
141251876Speter		printf(LMSG("open returns error %d"), error);
142251876Speter#endif
143251876Speter    }
144251876Speter    LFREEPATH(path);
145251876Speter    return error;
146251876Speter}
147251876Speter
148251876Speterint
149251876Speterlinux_lseek(struct thread *td, struct linux_lseek_args *args)
150251876Speter{
151251876Speter
152251876Speter    struct lseek_args /* {
153251876Speter	int fd;
154251876Speter	int pad;
155251876Speter	off_t offset;
156251876Speter	int whence;
157251876Speter    } */ tmp_args;
158251876Speter    int error;
159251876Speter
160251876Speter#ifdef DEBUG
161251876Speter	if (ldebug(lseek))
162251876Speter		printf(ARGS(lseek, "%d, %ld, %d"),
163251876Speter		    args->fdes, (long)args->off, args->whence);
164251876Speter#endif
165251876Speter    tmp_args.fd = args->fdes;
166251876Speter    tmp_args.offset = (off_t)args->off;
167251876Speter    tmp_args.whence = args->whence;
168251876Speter    error = lseek(td, &tmp_args);
169251876Speter    return error;
170251876Speter}
171251876Speter
172251876Speter#ifndef __alpha__
173251876Speterint
174251876Speterlinux_llseek(struct thread *td, struct linux_llseek_args *args)
175251876Speter{
176251876Speter	struct lseek_args bsd_args;
177251876Speter	int error;
178251876Speter	off_t off;
179251876Speter
180251876Speter#ifdef DEBUG
181251876Speter	if (ldebug(llseek))
182251876Speter		printf(ARGS(llseek, "%d, %d:%d, %d"),
183251876Speter		    args->fd, args->ohigh, args->olow, args->whence);
184251876Speter#endif
185251876Speter	off = (args->olow) | (((off_t) args->ohigh) << 32);
186251876Speter
187251876Speter	bsd_args.fd = args->fd;
188251876Speter	bsd_args.offset = off;
189251876Speter	bsd_args.whence = args->whence;
190251876Speter
191251876Speter	if ((error = lseek(td, &bsd_args)))
192251876Speter		return error;
193251876Speter
194251876Speter	if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
195251876Speter		return error;
196251876Speter
197251876Speter	td->td_retval[0] = 0;
198251876Speter	return 0;
199251876Speter}
200251876Speter#endif /*!__alpha__*/
201251876Speter
202251876Speter#ifndef __alpha__
203251876Speterint
204251876Speterlinux_readdir(struct thread *td, struct linux_readdir_args *args)
205251876Speter{
206251876Speter	struct linux_getdents_args lda;
207251876Speter
208251876Speter	lda.fd = args->fd;
209251876Speter	lda.dent = args->dent;
210251876Speter	lda.count = 1;
211251876Speter	return linux_getdents(td, &lda);
212251876Speter}
213251876Speter#endif /*!__alpha__*/
214251876Speter
215251876Speter/*
216251876Speter * Note that linux_getdents(2) and linux_getdents64(2) have the same
217251876Speter * arguments. They only differ in the definition of struct dirent they
218251876Speter * operate on. We use this to common the code, with the exception of
219251876Speter * accessing struct dirent. Note that linux_readdir(2) is implemented
220251876Speter * by means of linux_getdents(2). In this case we never operate on
221251876Speter * struct dirent64 and thus don't need to handle it...
222251876Speter */
223251876Speter
224251876Speterstruct l_dirent {
225251876Speter	l_long		d_ino;
226251876Speter	l_off_t		d_off;
227251876Speter	l_ushort	d_reclen;
228251876Speter	char		d_name[LINUX_NAME_MAX + 1];
229251876Speter};
230251876Speter
231251876Speterstruct l_dirent64 {
232251876Speter	uint64_t	d_ino;
233251876Speter	int64_t		d_off;
234251876Speter	l_ushort	d_reclen;
235251876Speter	u_char		d_type;
236251876Speter	char		d_name[LINUX_NAME_MAX + 1];
237251876Speter};
238251876Speter
239251876Speter#define LINUX_RECLEN(de,namlen) \
240251876Speter    ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
241251876Speter
242251876Speter#define	LINUX_DIRBLKSIZ		512
243251876Speter
244251876Speterstatic int
245251876Spetergetdents_common(struct thread *td, struct linux_getdents64_args *args,
246251876Speter    int is64bit)
247251876Speter{
248251876Speter	struct dirent *bdp;
249251876Speter	struct vnode *vp;
250251876Speter	caddr_t inp, buf;		/* BSD-format */
251251876Speter	int len, reclen;		/* BSD-format */
252251876Speter	caddr_t outp;			/* Linux-format */
253251876Speter	int resid, linuxreclen=0;	/* Linux-format */
254251876Speter	struct file *fp;
255251876Speter	struct uio auio;
256251876Speter	struct iovec aiov;
257251876Speter	struct vattr va;
258251876Speter	off_t off;
259251876Speter	struct l_dirent linux_dirent;
260251876Speter	struct l_dirent64 linux_dirent64;
261251876Speter	int buflen, error, eofflag, nbytes, justone;
262251876Speter	u_long *cookies = NULL, *cookiep;
263251876Speter	int ncookies;
264251876Speter
265251876Speter	if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
266251876Speter		return (error);
267251876Speter
268251876Speter	if ((fp->f_flag & FREAD) == 0) {
269251876Speter		fdrop(fp, td);
270251876Speter		return (EBADF);
271251876Speter	}
272251876Speter
273251876Speter	vp = fp->f_data;
274251876Speter	if (vp->v_type != VDIR) {
275251876Speter		fdrop(fp, td);
276251876Speter		return (EINVAL);
277251876Speter	}
278251876Speter
279251876Speter	if ((error = VOP_GETATTR(vp, &va, td->td_ucred, td))) {
280251876Speter		fdrop(fp, td);
281251876Speter		return (error);
282251876Speter	}
283251876Speter
284251876Speter	nbytes = args->count;
285251876Speter	if (nbytes == 1) {
286251876Speter		/* readdir(2) case. Always struct dirent. */
287251876Speter		if (is64bit) {
288251876Speter			fdrop(fp, td);
289251876Speter			return (EINVAL);
290251876Speter		}
291251876Speter		nbytes = sizeof(linux_dirent);
292251876Speter		justone = 1;
293251876Speter	} else
294251876Speter		justone = 0;
295251876Speter
296251876Speter	off = fp->f_offset;
297251876Speter
298251876Speter	buflen = max(LINUX_DIRBLKSIZ, nbytes);
299251876Speter	buflen = min(buflen, MAXBSIZE);
300251876Speter	buf = malloc(buflen, M_TEMP, M_WAITOK);
301251876Speter	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
302251876Speter
303251876Speteragain:
304251876Speter	aiov.iov_base = buf;
305251876Speter	aiov.iov_len = buflen;
306251876Speter	auio.uio_iov = &aiov;
307251876Speter	auio.uio_iovcnt = 1;
308251876Speter	auio.uio_rw = UIO_READ;
309251876Speter	auio.uio_segflg = UIO_SYSSPACE;
310251876Speter	auio.uio_td = td;
311251876Speter	auio.uio_resid = buflen;
312251876Speter	auio.uio_offset = off;
313251876Speter
314251876Speter	if (cookies) {
315251876Speter		free(cookies, M_TEMP);
316251876Speter		cookies = NULL;
317251876Speter	}
318251876Speter
319251876Speter#ifdef MAC
320251876Speter	/*
321251876Speter	 * Do directory search MAC check using non-cached credentials.
322251876Speter	 */
323251876Speter	if ((error = mac_check_vnode_readdir(td->td_proc->p_ucred, vp)))
324251876Speter		goto out;
325251876Speter#endif /* MAC */
326251876Speter	if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
327251876Speter		 &cookies)))
328251876Speter		goto out;
329251876Speter
330251876Speter	inp = buf;
331251876Speter	outp = (caddr_t)args->dirent;
332251876Speter	resid = nbytes;
333251876Speter	if ((len = buflen - auio.uio_resid) <= 0)
334251876Speter		goto eof;
335251876Speter
336251876Speter	cookiep = cookies;
337251876Speter
338251876Speter	if (cookies) {
339251876Speter		/*
340251876Speter		 * When using cookies, the vfs has the option of reading from
341251876Speter		 * a different offset than that supplied (UFS truncates the
342251876Speter		 * offset to a block boundary to make sure that it never reads
343251876Speter		 * partway through a directory entry, even if the directory
344251876Speter		 * has been compacted).
345251876Speter		 */
346251876Speter		while (len > 0 && ncookies > 0 && *cookiep <= off) {
347251876Speter			bdp = (struct dirent *) inp;
348251876Speter			len -= bdp->d_reclen;
349251876Speter			inp += bdp->d_reclen;
350251876Speter			cookiep++;
351251876Speter			ncookies--;
352251876Speter		}
353251876Speter	}
354251876Speter
355251876Speter	while (len > 0) {
356251876Speter		if (cookiep && ncookies == 0)
357251876Speter			break;
358251876Speter		bdp = (struct dirent *) inp;
359251876Speter		reclen = bdp->d_reclen;
360251876Speter		if (reclen & 3) {
361251876Speter			error = EFAULT;
362251876Speter			goto out;
363251876Speter		}
364251876Speter
365251876Speter		if (bdp->d_fileno == 0) {
366251876Speter			inp += reclen;
367251876Speter			if (cookiep) {
368251876Speter				off = *cookiep++;
369251876Speter				ncookies--;
370251876Speter			} else
371251876Speter				off += reclen;
372251876Speter
373251876Speter			len -= reclen;
374251876Speter			continue;
375251876Speter		}
376251876Speter
377251876Speter		linuxreclen = (is64bit)
378251876Speter		    ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
379251876Speter		    : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
380251876Speter
381251876Speter		if (reclen > len || resid < linuxreclen) {
382251876Speter			outp++;
383251876Speter			break;
384251876Speter		}
385251876Speter
386251876Speter		if (justone) {
387251876Speter			/* readdir(2) case. */
388251876Speter			linux_dirent.d_ino = (l_long)bdp->d_fileno;
389251876Speter			linux_dirent.d_off = (l_off_t)linuxreclen;
390251876Speter			linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
391251876Speter			strcpy(linux_dirent.d_name, bdp->d_name);
392251876Speter			error = copyout(&linux_dirent, outp, linuxreclen);
393251876Speter		} else {
394251876Speter			if (is64bit) {
395251876Speter				linux_dirent64.d_ino = bdp->d_fileno;
396251876Speter				linux_dirent64.d_off = (cookiep)
397251876Speter				    ? (l_off_t)*cookiep
398251876Speter				    : (l_off_t)(off + reclen);
399				linux_dirent64.d_reclen =
400				    (l_ushort)linuxreclen;
401				linux_dirent64.d_type = bdp->d_type;
402				strcpy(linux_dirent64.d_name, bdp->d_name);
403				error = copyout(&linux_dirent64, outp,
404				    linuxreclen);
405			} else {
406				linux_dirent.d_ino = bdp->d_fileno;
407				linux_dirent.d_off = (cookiep)
408				    ? (l_off_t)*cookiep
409				    : (l_off_t)(off + reclen);
410				linux_dirent.d_reclen = (l_ushort)linuxreclen;
411				strcpy(linux_dirent.d_name, bdp->d_name);
412				error = copyout(&linux_dirent, outp,
413				    linuxreclen);
414			}
415		}
416		if (error)
417			goto out;
418
419		inp += reclen;
420		if (cookiep) {
421			off = *cookiep++;
422			ncookies--;
423		} else
424			off += reclen;
425
426		outp += linuxreclen;
427		resid -= linuxreclen;
428		len -= reclen;
429		if (justone)
430			break;
431	}
432
433	if (outp == (caddr_t)args->dirent)
434		goto again;
435
436	fp->f_offset = off;
437	if (justone)
438		nbytes = resid + linuxreclen;
439
440eof:
441	td->td_retval[0] = nbytes - resid;
442
443out:
444	if (cookies)
445		free(cookies, M_TEMP);
446
447	VOP_UNLOCK(vp, 0, td);
448	fdrop(fp, td);
449	free(buf, M_TEMP);
450	return (error);
451}
452
453int
454linux_getdents(struct thread *td, struct linux_getdents_args *args)
455{
456
457#ifdef DEBUG
458	if (ldebug(getdents))
459		printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
460#endif
461
462	return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
463}
464
465int
466linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
467{
468
469#ifdef DEBUG
470	if (ldebug(getdents64))
471		printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
472#endif
473
474	return (getdents_common(td, args, 1));
475}
476
477/*
478 * These exist mainly for hooks for doing /compat/linux translation.
479 */
480
481int
482linux_access(struct thread *td, struct linux_access_args *args)
483{
484	char *path;
485	int error;
486
487	LCONVPATHEXIST(td, args->path, &path);
488
489#ifdef DEBUG
490	if (ldebug(access))
491		printf(ARGS(access, "%s, %d"), path, args->flags);
492#endif
493	error = kern_access(td, path, UIO_SYSSPACE, args->flags);
494	LFREEPATH(path);
495	return (error);
496}
497
498int
499linux_unlink(struct thread *td, struct linux_unlink_args *args)
500{
501	char *path;
502	int error;
503
504	LCONVPATHEXIST(td, args->path, &path);
505
506#ifdef DEBUG
507	if (ldebug(unlink))
508		printf(ARGS(unlink, "%s"), path);
509#endif
510
511	error = kern_unlink(td, path, UIO_SYSSPACE);
512	LFREEPATH(path);
513	return (error);
514}
515
516int
517linux_chdir(struct thread *td, struct linux_chdir_args *args)
518{
519	char *path;
520	int error;
521
522	LCONVPATHEXIST(td, args->path, &path);
523
524#ifdef DEBUG
525	if (ldebug(chdir))
526		printf(ARGS(chdir, "%s"), path);
527#endif
528	error = kern_chdir(td, path, UIO_SYSSPACE);
529	LFREEPATH(path);
530	return (error);
531}
532
533int
534linux_chmod(struct thread *td, struct linux_chmod_args *args)
535{
536	char *path;
537	int error;
538
539	LCONVPATHEXIST(td, args->path, &path);
540
541#ifdef DEBUG
542	if (ldebug(chmod))
543		printf(ARGS(chmod, "%s, %d"), path, args->mode);
544#endif
545	error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
546	LFREEPATH(path);
547	return (error);
548}
549
550int
551linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
552{
553	char *path;
554	int error;
555
556	LCONVPATHCREAT(td, args->path, &path);
557
558#ifdef DEBUG
559	if (ldebug(mkdir))
560		printf(ARGS(mkdir, "%s, %d"), path, args->mode);
561#endif
562	error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
563	LFREEPATH(path);
564	return (error);
565}
566
567int
568linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
569{
570	char *path;
571	int error;
572
573	LCONVPATHEXIST(td, args->path, &path);
574
575#ifdef DEBUG
576	if (ldebug(rmdir))
577		printf(ARGS(rmdir, "%s"), path);
578#endif
579	error = kern_rmdir(td, path, UIO_SYSSPACE);
580	LFREEPATH(path);
581	return (error);
582}
583
584int
585linux_rename(struct thread *td, struct linux_rename_args *args)
586{
587	char *from, *to;
588	int error;
589
590	LCONVPATHEXIST(td, args->from, &from);
591	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
592	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
593	if (to == NULL) {
594		LFREEPATH(from);
595		return (error);
596	}
597
598#ifdef DEBUG
599	if (ldebug(rename))
600		printf(ARGS(rename, "%s, %s"), from, to);
601#endif
602	error = kern_rename(td, from, to, UIO_SYSSPACE);
603	LFREEPATH(from);
604	LFREEPATH(to);
605	return (error);
606}
607
608int
609linux_symlink(struct thread *td, struct linux_symlink_args *args)
610{
611	char *path, *to;
612	int error;
613
614	LCONVPATHEXIST(td, args->path, &path);
615	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
616	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
617	if (to == NULL) {
618		LFREEPATH(path);
619		return (error);
620	}
621
622#ifdef DEBUG
623	if (ldebug(symlink))
624		printf(ARGS(symlink, "%s, %s"), path, to);
625#endif
626	error = kern_symlink(td, path, to, UIO_SYSSPACE);
627	LFREEPATH(path);
628	LFREEPATH(to);
629	return (error);
630}
631
632int
633linux_readlink(struct thread *td, struct linux_readlink_args *args)
634{
635	char *name;
636	int error;
637
638	LCONVPATHEXIST(td, args->name, &name);
639
640#ifdef DEBUG
641	if (ldebug(readlink))
642		printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
643		    args->count);
644#endif
645	error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
646	    args->count);
647	LFREEPATH(name);
648	return (error);
649}
650
651int
652linux_truncate(struct thread *td, struct linux_truncate_args *args)
653{
654	char *path;
655	int error;
656
657	LCONVPATHEXIST(td, args->path, &path);
658
659#ifdef DEBUG
660	if (ldebug(truncate))
661		printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
662#endif
663
664	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
665	LFREEPATH(path);
666	return (error);
667}
668
669int
670linux_link(struct thread *td, struct linux_link_args *args)
671{
672	char *path, *to;
673	int error;
674
675	LCONVPATHEXIST(td, args->path, &path);
676	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
677	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
678	if (to == NULL) {
679		LFREEPATH(path);
680		return (error);
681	}
682
683#ifdef DEBUG
684	if (ldebug(link))
685		printf(ARGS(link, "%s, %s"), path, to);
686#endif
687	error = kern_link(td, path, to, UIO_SYSSPACE);
688	LFREEPATH(path);
689	LFREEPATH(to);
690	return (error);
691}
692
693#ifndef __alpha__
694int
695linux_fdatasync(td, uap)
696	struct thread *td;
697	struct linux_fdatasync_args *uap;
698{
699	struct fsync_args bsd;
700
701	bsd.fd = uap->fd;
702	return fsync(td, &bsd);
703}
704#endif /*!__alpha__*/
705
706int
707linux_pread(td, uap)
708	struct thread *td;
709	struct linux_pread_args *uap;
710{
711	struct pread_args bsd;
712
713	bsd.fd = uap->fd;
714	bsd.buf = uap->buf;
715	bsd.nbyte = uap->nbyte;
716	bsd.offset = uap->offset;
717	return pread(td, &bsd);
718}
719
720int
721linux_pwrite(td, uap)
722	struct thread *td;
723	struct linux_pwrite_args *uap;
724{
725	struct pwrite_args bsd;
726
727	bsd.fd = uap->fd;
728	bsd.buf = uap->buf;
729	bsd.nbyte = uap->nbyte;
730	bsd.offset = uap->offset;
731	return pwrite(td, &bsd);
732}
733
734int
735linux_mount(struct thread *td, struct linux_mount_args *args)
736{
737	struct ufs_args ufs;
738	char fstypename[MFSNAMELEN];
739	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
740	int error;
741	int fsflags;
742	const char *fstype;
743	void *fsdata;
744
745	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
746	    NULL);
747	if (error)
748		return (error);
749	error = copyinstr(args->specialfile, mntfromname, MFSNAMELEN - 1, NULL);
750	if (error)
751		return (error);
752	error = copyinstr(args->dir, mntonname, MFSNAMELEN - 1, NULL);
753	if (error)
754		return (error);
755
756#ifdef DEBUG
757	if (ldebug(mount))
758		printf(ARGS(mount, "%s, %s, %s"),
759		    fstypename, mntfromname, mntonname);
760#endif
761
762	if (strcmp(fstypename, "ext2") == 0) {
763		fstype = "ext2fs";
764		fsdata = &ufs;
765		ufs.fspec = mntfromname;
766#define DEFAULT_ROOTID		-2
767		ufs.export.ex_root = DEFAULT_ROOTID;
768		ufs.export.ex_flags =
769		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
770	} else if (strcmp(fstypename, "proc") == 0) {
771		fstype = "linprocfs";
772		fsdata = NULL;
773	} else {
774		return (ENODEV);
775	}
776
777	fsflags = 0;
778
779	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
780		/*
781		 * Linux SYNC flag is not included; the closest equivalent
782		 * FreeBSD has is !ASYNC, which is our default.
783		 */
784		if (args->rwflag & LINUX_MS_RDONLY)
785			fsflags |= MNT_RDONLY;
786		if (args->rwflag & LINUX_MS_NOSUID)
787			fsflags |= MNT_NOSUID;
788		if (args->rwflag & LINUX_MS_NODEV)
789			fsflags |= MNT_NODEV;
790		if (args->rwflag & LINUX_MS_NOEXEC)
791			fsflags |= MNT_NOEXEC;
792		if (args->rwflag & LINUX_MS_REMOUNT)
793			fsflags |= MNT_UPDATE;
794	}
795
796	return (vfs_mount(td, fstype, mntonname, fsflags, fsdata));
797}
798
799int
800linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
801{
802	struct linux_umount_args args2;
803
804	args2.path = args->path;
805	args2.flags = 0;
806	return (linux_umount(td, &args2));
807}
808
809int
810linux_umount(struct thread *td, struct linux_umount_args *args)
811{
812	struct unmount_args bsd;
813
814	bsd.path = args->path;
815	bsd.flags = args->flags;	/* XXX correct? */
816	return (unmount(td, &bsd));
817}
818
819/*
820 * fcntl family of syscalls
821 */
822
823struct l_flock {
824	l_short		l_type;
825	l_short		l_whence;
826	l_off_t		l_start;
827	l_off_t		l_len;
828	l_pid_t		l_pid;
829};
830
831static void
832linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
833{
834	switch (linux_flock->l_type) {
835	case LINUX_F_RDLCK:
836		bsd_flock->l_type = F_RDLCK;
837		break;
838	case LINUX_F_WRLCK:
839		bsd_flock->l_type = F_WRLCK;
840		break;
841	case LINUX_F_UNLCK:
842		bsd_flock->l_type = F_UNLCK;
843		break;
844	default:
845		bsd_flock->l_type = -1;
846		break;
847	}
848	bsd_flock->l_whence = linux_flock->l_whence;
849	bsd_flock->l_start = (off_t)linux_flock->l_start;
850	bsd_flock->l_len = (off_t)linux_flock->l_len;
851	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
852}
853
854static void
855bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
856{
857	switch (bsd_flock->l_type) {
858	case F_RDLCK:
859		linux_flock->l_type = LINUX_F_RDLCK;
860		break;
861	case F_WRLCK:
862		linux_flock->l_type = LINUX_F_WRLCK;
863		break;
864	case F_UNLCK:
865		linux_flock->l_type = LINUX_F_UNLCK;
866		break;
867	}
868	linux_flock->l_whence = bsd_flock->l_whence;
869	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
870	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
871	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
872}
873
874#if defined(__i386__)
875struct l_flock64 {
876	l_short		l_type;
877	l_short		l_whence;
878	l_loff_t	l_start;
879	l_loff_t	l_len;
880	l_pid_t		l_pid;
881};
882
883static void
884linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
885{
886	switch (linux_flock->l_type) {
887	case LINUX_F_RDLCK:
888		bsd_flock->l_type = F_RDLCK;
889		break;
890	case LINUX_F_WRLCK:
891		bsd_flock->l_type = F_WRLCK;
892		break;
893	case LINUX_F_UNLCK:
894		bsd_flock->l_type = F_UNLCK;
895		break;
896	default:
897		bsd_flock->l_type = -1;
898		break;
899	}
900	bsd_flock->l_whence = linux_flock->l_whence;
901	bsd_flock->l_start = (off_t)linux_flock->l_start;
902	bsd_flock->l_len = (off_t)linux_flock->l_len;
903	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
904}
905
906static void
907bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
908{
909	switch (bsd_flock->l_type) {
910	case F_RDLCK:
911		linux_flock->l_type = LINUX_F_RDLCK;
912		break;
913	case F_WRLCK:
914		linux_flock->l_type = LINUX_F_WRLCK;
915		break;
916	case F_UNLCK:
917		linux_flock->l_type = LINUX_F_UNLCK;
918		break;
919	}
920	linux_flock->l_whence = bsd_flock->l_whence;
921	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
922	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
923	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
924}
925#endif /* __i386__ */
926
927#if defined(__alpha__)
928#define	linux_fcntl64_args	linux_fcntl_args
929#endif
930
931static int
932fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
933{
934	struct l_flock linux_flock;
935	struct flock bsd_flock;
936	struct file *fp;
937	long arg;
938	int error, result;
939
940	switch (args->cmd) {
941	case LINUX_F_DUPFD:
942		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
943
944	case LINUX_F_GETFD:
945		return (kern_fcntl(td, args->fd, F_GETFD, 0));
946
947	case LINUX_F_SETFD:
948		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
949
950	case LINUX_F_GETFL:
951		error = kern_fcntl(td, args->fd, F_GETFL, 0);
952		result = td->td_retval[0];
953		td->td_retval[0] = 0;
954		if (result & O_RDONLY)
955			td->td_retval[0] |= LINUX_O_RDONLY;
956		if (result & O_WRONLY)
957			td->td_retval[0] |= LINUX_O_WRONLY;
958		if (result & O_RDWR)
959			td->td_retval[0] |= LINUX_O_RDWR;
960		if (result & O_NDELAY)
961			td->td_retval[0] |= LINUX_O_NONBLOCK;
962		if (result & O_APPEND)
963			td->td_retval[0] |= LINUX_O_APPEND;
964		if (result & O_FSYNC)
965			td->td_retval[0] |= LINUX_O_SYNC;
966		if (result & O_ASYNC)
967			td->td_retval[0] |= LINUX_FASYNC;
968		return (error);
969
970	case LINUX_F_SETFL:
971		arg = 0;
972		if (args->arg & LINUX_O_NDELAY)
973			arg |= O_NONBLOCK;
974		if (args->arg & LINUX_O_APPEND)
975			arg |= O_APPEND;
976		if (args->arg & LINUX_O_SYNC)
977			arg |= O_FSYNC;
978		if (args->arg & LINUX_FASYNC)
979			arg |= O_ASYNC;
980		return (kern_fcntl(td, args->fd, F_SETFL, arg));
981
982	case LINUX_F_GETLK:
983		error = copyin((void *)args->arg, &linux_flock,
984		    sizeof(linux_flock));
985		if (error)
986			return (error);
987		linux_to_bsd_flock(&linux_flock, &bsd_flock);
988		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
989		if (error)
990			return (error);
991		bsd_to_linux_flock(&bsd_flock, &linux_flock);
992		return (copyout(&linux_flock, (void *)args->arg,
993		    sizeof(linux_flock)));
994
995	case LINUX_F_SETLK:
996		error = copyin((void *)args->arg, &linux_flock,
997		    sizeof(linux_flock));
998		if (error)
999			return (error);
1000		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1001		return (kern_fcntl(td, args->fd, F_SETLK,
1002		    (intptr_t)&bsd_flock));
1003
1004	case LINUX_F_SETLKW:
1005		error = copyin((void *)args->arg, &linux_flock,
1006		    sizeof(linux_flock));
1007		if (error)
1008			return (error);
1009		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1010		return (kern_fcntl(td, args->fd, F_SETLKW,
1011		     (intptr_t)&bsd_flock));
1012
1013	case LINUX_F_GETOWN:
1014		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1015
1016	case LINUX_F_SETOWN:
1017		/*
1018		 * XXX some Linux applications depend on F_SETOWN having no
1019		 * significant effect for pipes (SIGIO is not delivered for
1020		 * pipes under Linux-2.2.35 at least).
1021		 */
1022		error = fget(td, args->fd, &fp);
1023		if (error)
1024			return (error);
1025		if (fp->f_type == DTYPE_PIPE) {
1026			fdrop(fp, td);
1027			return (EINVAL);
1028		}
1029		fdrop(fp, td);
1030
1031		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1032	}
1033
1034	return (EINVAL);
1035}
1036
1037int
1038linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1039{
1040	struct linux_fcntl64_args args64;
1041
1042#ifdef DEBUG
1043	if (ldebug(fcntl))
1044		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1045#endif
1046
1047	args64.fd = args->fd;
1048	args64.cmd = args->cmd;
1049	args64.arg = args->arg;
1050	return (fcntl_common(td, &args64));
1051}
1052
1053#if defined(__i386__)
1054int
1055linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1056{
1057	struct l_flock64 linux_flock;
1058	struct flock bsd_flock;
1059	int error;
1060
1061#ifdef DEBUG
1062	if (ldebug(fcntl64))
1063		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1064#endif
1065
1066	switch (args->cmd) {
1067	case LINUX_F_GETLK64:
1068		error = copyin((void *)args->arg, &linux_flock,
1069		    sizeof(linux_flock));
1070		if (error)
1071			return (error);
1072		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1073		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1074		if (error)
1075			return (error);
1076		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1077		return (copyout(&linux_flock, (void *)args->arg,
1078			    sizeof(linux_flock)));
1079
1080	case LINUX_F_SETLK64:
1081		error = copyin((void *)args->arg, &linux_flock,
1082		    sizeof(linux_flock));
1083		if (error)
1084			return (error);
1085		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1086		return (kern_fcntl(td, args->fd, F_SETLK,
1087		    (intptr_t)&bsd_flock));
1088
1089	case LINUX_F_SETLKW64:
1090		error = copyin((void *)args->arg, &linux_flock,
1091		    sizeof(linux_flock));
1092		if (error)
1093			return (error);
1094		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1095		return (kern_fcntl(td, args->fd, F_SETLKW,
1096		    (intptr_t)&bsd_flock));
1097	}
1098
1099	return (fcntl_common(td, args));
1100}
1101#endif /* __i386__ */
1102
1103int
1104linux_chown(struct thread *td, struct linux_chown_args *args)
1105{
1106	char *path;
1107	int error;
1108
1109	LCONVPATHEXIST(td, args->path, &path);
1110
1111#ifdef DEBUG
1112	if (ldebug(chown))
1113		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1114#endif
1115	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1116	LFREEPATH(path);
1117	return (error);
1118}
1119
1120int
1121linux_lchown(struct thread *td, struct linux_lchown_args *args)
1122{
1123	char *path;
1124	int error;
1125
1126	LCONVPATHEXIST(td, args->path, &path);
1127
1128#ifdef DEBUG
1129	if (ldebug(lchown))
1130		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1131#endif
1132	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1133	LFREEPATH(path);
1134	return (error);
1135}
1136