linux_file.c revision 132708
1/*-
2 * Copyright (c) 1994-1995 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/compat/linux/linux_file.c 132708 2004-07-27 21:38:42Z phk $");
31
32#include "opt_compat.h"
33#include "opt_mac.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/conf.h>
38#include <sys/dirent.h>
39#include <sys/fcntl.h>
40#include <sys/file.h>
41#include <sys/filedesc.h>
42#include <sys/lock.h>
43#include <sys/mac.h>
44#include <sys/malloc.h>
45#include <sys/mount.h>
46#include <sys/mutex.h>
47#include <sys/proc.h>
48#include <sys/syscallsubr.h>
49#include <sys/sysproto.h>
50#include <sys/tty.h>
51#include <sys/vnode.h>
52
53#include <ufs/ufs/extattr.h>
54#include <ufs/ufs/quota.h>
55#include <ufs/ufs/ufsmount.h>
56
57#include <machine/../linux/linux.h>
58#include <machine/../linux/linux_proto.h>
59#include <compat/linux/linux_util.h>
60
61#ifndef __alpha__
62int
63linux_creat(struct thread *td, struct linux_creat_args *args)
64{
65    char *path;
66    int error;
67
68    LCONVPATHEXIST(td, args->path, &path);
69
70#ifdef DEBUG
71	if (ldebug(creat))
72		printf(ARGS(creat, "%s, %d"), path, args->mode);
73#endif
74    error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
75	args->mode);
76    LFREEPATH(path);
77    return (error);
78}
79#endif /*!__alpha__*/
80
81int
82linux_open(struct thread *td, struct linux_open_args *args)
83{
84    struct proc *p = td->td_proc;
85    char *path;
86    int bsd_flags, error;
87
88    if (args->flags & LINUX_O_CREAT)
89	LCONVPATHCREAT(td, args->path, &path);
90    else
91	LCONVPATHEXIST(td, args->path, &path);
92
93#ifdef DEBUG
94	if (ldebug(open))
95		printf(ARGS(open, "%s, 0x%x, 0x%x"),
96		    path, args->flags, args->mode);
97#endif
98    bsd_flags = 0;
99    if (args->flags & LINUX_O_RDONLY)
100	bsd_flags |= O_RDONLY;
101    if (args->flags & LINUX_O_WRONLY)
102	bsd_flags |= O_WRONLY;
103    if (args->flags & LINUX_O_RDWR)
104	bsd_flags |= O_RDWR;
105    if (args->flags & LINUX_O_NDELAY)
106	bsd_flags |= O_NONBLOCK;
107    if (args->flags & LINUX_O_APPEND)
108	bsd_flags |= O_APPEND;
109    if (args->flags & LINUX_O_SYNC)
110	bsd_flags |= O_FSYNC;
111    if (args->flags & LINUX_O_NONBLOCK)
112	bsd_flags |= O_NONBLOCK;
113    if (args->flags & LINUX_FASYNC)
114	bsd_flags |= O_ASYNC;
115    if (args->flags & LINUX_O_CREAT)
116	bsd_flags |= O_CREAT;
117    if (args->flags & LINUX_O_TRUNC)
118	bsd_flags |= O_TRUNC;
119    if (args->flags & LINUX_O_EXCL)
120	bsd_flags |= O_EXCL;
121    if (args->flags & LINUX_O_NOCTTY)
122	bsd_flags |= O_NOCTTY;
123
124    error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
125    PROC_LOCK(p);
126    if (!error && !(bsd_flags & O_NOCTTY) &&
127	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
128	struct file *fp;
129
130	PROC_UNLOCK(p);
131	error = fget(td, td->td_retval[0], &fp);
132	if (!error) {
133		if (fp->f_type == DTYPE_VNODE)
134			fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
135			    td);
136	    fdrop(fp, td);
137	}
138    } else {
139	PROC_UNLOCK(p);
140#ifdef DEBUG
141	if (ldebug(open))
142		printf(LMSG("open returns error %d"), error);
143#endif
144    }
145    LFREEPATH(path);
146    return error;
147}
148
149int
150linux_lseek(struct thread *td, struct linux_lseek_args *args)
151{
152
153    struct lseek_args /* {
154	int fd;
155	int pad;
156	off_t offset;
157	int whence;
158    } */ tmp_args;
159    int error;
160
161#ifdef DEBUG
162	if (ldebug(lseek))
163		printf(ARGS(lseek, "%d, %ld, %d"),
164		    args->fdes, (long)args->off, args->whence);
165#endif
166    tmp_args.fd = args->fdes;
167    tmp_args.offset = (off_t)args->off;
168    tmp_args.whence = args->whence;
169    error = lseek(td, &tmp_args);
170    return error;
171}
172
173#ifndef __alpha__
174int
175linux_llseek(struct thread *td, struct linux_llseek_args *args)
176{
177	struct lseek_args bsd_args;
178	int error;
179	off_t off;
180
181#ifdef DEBUG
182	if (ldebug(llseek))
183		printf(ARGS(llseek, "%d, %d:%d, %d"),
184		    args->fd, args->ohigh, args->olow, args->whence);
185#endif
186	off = (args->olow) | (((off_t) args->ohigh) << 32);
187
188	bsd_args.fd = args->fd;
189	bsd_args.offset = off;
190	bsd_args.whence = args->whence;
191
192	if ((error = lseek(td, &bsd_args)))
193		return error;
194
195	if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
196		return error;
197
198	td->td_retval[0] = 0;
199	return 0;
200}
201#endif /*!__alpha__*/
202
203#ifndef __alpha__
204int
205linux_readdir(struct thread *td, struct linux_readdir_args *args)
206{
207	struct linux_getdents_args lda;
208
209	lda.fd = args->fd;
210	lda.dent = args->dent;
211	lda.count = 1;
212	return linux_getdents(td, &lda);
213}
214#endif /*!__alpha__*/
215
216/*
217 * Note that linux_getdents(2) and linux_getdents64(2) have the same
218 * arguments. They only differ in the definition of struct dirent they
219 * operate on. We use this to common the code, with the exception of
220 * accessing struct dirent. Note that linux_readdir(2) is implemented
221 * by means of linux_getdents(2). In this case we never operate on
222 * struct dirent64 and thus don't need to handle it...
223 */
224
225struct l_dirent {
226	l_long		d_ino;
227	l_off_t		d_off;
228	l_ushort	d_reclen;
229	char		d_name[LINUX_NAME_MAX + 1];
230};
231
232struct l_dirent64 {
233	uint64_t	d_ino;
234	int64_t		d_off;
235	l_ushort	d_reclen;
236	u_char		d_type;
237	char		d_name[LINUX_NAME_MAX + 1];
238};
239
240#define LINUX_RECLEN(de,namlen) \
241    ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
242
243#define	LINUX_DIRBLKSIZ		512
244
245static int
246getdents_common(struct thread *td, struct linux_getdents64_args *args,
247    int is64bit)
248{
249	struct dirent *bdp;
250	struct vnode *vp;
251	caddr_t inp, buf;		/* BSD-format */
252	int len, reclen;		/* BSD-format */
253	caddr_t outp;			/* Linux-format */
254	int resid, linuxreclen=0;	/* Linux-format */
255	struct file *fp;
256	struct uio auio;
257	struct iovec aiov;
258	off_t off;
259	struct l_dirent linux_dirent;
260	struct l_dirent64 linux_dirent64;
261	int buflen, error, eofflag, nbytes, justone;
262	u_long *cookies = NULL, *cookiep;
263	int ncookies;
264
265	if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
266		return (error);
267
268	if ((fp->f_flag & FREAD) == 0) {
269		fdrop(fp, td);
270		return (EBADF);
271	}
272
273	vp = fp->f_vnode;
274	if (vp->v_type != VDIR) {
275		fdrop(fp, td);
276		return (EINVAL);
277	}
278
279	nbytes = args->count;
280	if (nbytes == 1) {
281		/* readdir(2) case. Always struct dirent. */
282		if (is64bit) {
283			fdrop(fp, td);
284			return (EINVAL);
285		}
286		nbytes = sizeof(linux_dirent);
287		justone = 1;
288	} else
289		justone = 0;
290
291	off = fp->f_offset;
292
293	buflen = max(LINUX_DIRBLKSIZ, nbytes);
294	buflen = min(buflen, MAXBSIZE);
295	buf = malloc(buflen, M_TEMP, M_WAITOK);
296	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
297
298again:
299	aiov.iov_base = buf;
300	aiov.iov_len = buflen;
301	auio.uio_iov = &aiov;
302	auio.uio_iovcnt = 1;
303	auio.uio_rw = UIO_READ;
304	auio.uio_segflg = UIO_SYSSPACE;
305	auio.uio_td = td;
306	auio.uio_resid = buflen;
307	auio.uio_offset = off;
308
309	if (cookies) {
310		free(cookies, M_TEMP);
311		cookies = NULL;
312	}
313
314#ifdef MAC
315	/*
316	 * Do directory search MAC check using non-cached credentials.
317	 */
318	if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
319		goto out;
320#endif /* MAC */
321	if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
322		 &cookies)))
323		goto out;
324
325	inp = buf;
326	outp = (caddr_t)args->dirent;
327	resid = nbytes;
328	if ((len = buflen - auio.uio_resid) <= 0)
329		goto eof;
330
331	cookiep = cookies;
332
333	if (cookies) {
334		/*
335		 * When using cookies, the vfs has the option of reading from
336		 * a different offset than that supplied (UFS truncates the
337		 * offset to a block boundary to make sure that it never reads
338		 * partway through a directory entry, even if the directory
339		 * has been compacted).
340		 */
341		while (len > 0 && ncookies > 0 && *cookiep <= off) {
342			bdp = (struct dirent *) inp;
343			len -= bdp->d_reclen;
344			inp += bdp->d_reclen;
345			cookiep++;
346			ncookies--;
347		}
348	}
349
350	while (len > 0) {
351		if (cookiep && ncookies == 0)
352			break;
353		bdp = (struct dirent *) inp;
354		reclen = bdp->d_reclen;
355		if (reclen & 3) {
356			error = EFAULT;
357			goto out;
358		}
359
360		if (bdp->d_fileno == 0) {
361			inp += reclen;
362			if (cookiep) {
363				off = *cookiep++;
364				ncookies--;
365			} else
366				off += reclen;
367
368			len -= reclen;
369			continue;
370		}
371
372		linuxreclen = (is64bit)
373		    ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
374		    : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
375
376		if (reclen > len || resid < linuxreclen) {
377			outp++;
378			break;
379		}
380
381		if (justone) {
382			/* readdir(2) case. */
383			linux_dirent.d_ino = (l_long)bdp->d_fileno;
384			linux_dirent.d_off = (l_off_t)linuxreclen;
385			linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
386			strcpy(linux_dirent.d_name, bdp->d_name);
387			error = copyout(&linux_dirent, outp, linuxreclen);
388		} else {
389			if (is64bit) {
390				linux_dirent64.d_ino = bdp->d_fileno;
391				linux_dirent64.d_off = (cookiep)
392				    ? (l_off_t)*cookiep
393				    : (l_off_t)(off + reclen);
394				linux_dirent64.d_reclen =
395				    (l_ushort)linuxreclen;
396				linux_dirent64.d_type = bdp->d_type;
397				strcpy(linux_dirent64.d_name, bdp->d_name);
398				error = copyout(&linux_dirent64, outp,
399				    linuxreclen);
400			} else {
401				linux_dirent.d_ino = bdp->d_fileno;
402				linux_dirent.d_off = (cookiep)
403				    ? (l_off_t)*cookiep
404				    : (l_off_t)(off + reclen);
405				linux_dirent.d_reclen = (l_ushort)linuxreclen;
406				strcpy(linux_dirent.d_name, bdp->d_name);
407				error = copyout(&linux_dirent, outp,
408				    linuxreclen);
409			}
410		}
411		if (error)
412			goto out;
413
414		inp += reclen;
415		if (cookiep) {
416			off = *cookiep++;
417			ncookies--;
418		} else
419			off += reclen;
420
421		outp += linuxreclen;
422		resid -= linuxreclen;
423		len -= reclen;
424		if (justone)
425			break;
426	}
427
428	if (outp == (caddr_t)args->dirent)
429		goto again;
430
431	fp->f_offset = off;
432	if (justone)
433		nbytes = resid + linuxreclen;
434
435eof:
436	td->td_retval[0] = nbytes - resid;
437
438out:
439	if (cookies)
440		free(cookies, M_TEMP);
441
442	VOP_UNLOCK(vp, 0, td);
443	fdrop(fp, td);
444	free(buf, M_TEMP);
445	return (error);
446}
447
448int
449linux_getdents(struct thread *td, struct linux_getdents_args *args)
450{
451
452#ifdef DEBUG
453	if (ldebug(getdents))
454		printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
455#endif
456
457	return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
458}
459
460int
461linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
462{
463
464#ifdef DEBUG
465	if (ldebug(getdents64))
466		printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
467#endif
468
469	return (getdents_common(td, args, 1));
470}
471
472/*
473 * These exist mainly for hooks for doing /compat/linux translation.
474 */
475
476int
477linux_access(struct thread *td, struct linux_access_args *args)
478{
479	char *path;
480	int error;
481
482	LCONVPATHEXIST(td, args->path, &path);
483
484#ifdef DEBUG
485	if (ldebug(access))
486		printf(ARGS(access, "%s, %d"), path, args->flags);
487#endif
488	error = kern_access(td, path, UIO_SYSSPACE, args->flags);
489	LFREEPATH(path);
490	return (error);
491}
492
493int
494linux_unlink(struct thread *td, struct linux_unlink_args *args)
495{
496	char *path;
497	int error;
498
499	LCONVPATHEXIST(td, args->path, &path);
500
501#ifdef DEBUG
502	if (ldebug(unlink))
503		printf(ARGS(unlink, "%s"), path);
504#endif
505
506	error = kern_unlink(td, path, UIO_SYSSPACE);
507	LFREEPATH(path);
508	return (error);
509}
510
511int
512linux_chdir(struct thread *td, struct linux_chdir_args *args)
513{
514	char *path;
515	int error;
516
517	LCONVPATHEXIST(td, args->path, &path);
518
519#ifdef DEBUG
520	if (ldebug(chdir))
521		printf(ARGS(chdir, "%s"), path);
522#endif
523	error = kern_chdir(td, path, UIO_SYSSPACE);
524	LFREEPATH(path);
525	return (error);
526}
527
528int
529linux_chmod(struct thread *td, struct linux_chmod_args *args)
530{
531	char *path;
532	int error;
533
534	LCONVPATHEXIST(td, args->path, &path);
535
536#ifdef DEBUG
537	if (ldebug(chmod))
538		printf(ARGS(chmod, "%s, %d"), path, args->mode);
539#endif
540	error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
541	LFREEPATH(path);
542	return (error);
543}
544
545int
546linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
547{
548	char *path;
549	int error;
550
551	LCONVPATHCREAT(td, args->path, &path);
552
553#ifdef DEBUG
554	if (ldebug(mkdir))
555		printf(ARGS(mkdir, "%s, %d"), path, args->mode);
556#endif
557	error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
558	LFREEPATH(path);
559	return (error);
560}
561
562int
563linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
564{
565	char *path;
566	int error;
567
568	LCONVPATHEXIST(td, args->path, &path);
569
570#ifdef DEBUG
571	if (ldebug(rmdir))
572		printf(ARGS(rmdir, "%s"), path);
573#endif
574	error = kern_rmdir(td, path, UIO_SYSSPACE);
575	LFREEPATH(path);
576	return (error);
577}
578
579int
580linux_rename(struct thread *td, struct linux_rename_args *args)
581{
582	char *from, *to;
583	int error;
584
585	LCONVPATHEXIST(td, args->from, &from);
586	/* Expand LCONVPATHCREATE so that `from' can be freed on errors */
587	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
588	if (to == NULL) {
589		LFREEPATH(from);
590		return (error);
591	}
592
593#ifdef DEBUG
594	if (ldebug(rename))
595		printf(ARGS(rename, "%s, %s"), from, to);
596#endif
597	error = kern_rename(td, from, to, UIO_SYSSPACE);
598	LFREEPATH(from);
599	LFREEPATH(to);
600	return (error);
601}
602
603int
604linux_symlink(struct thread *td, struct linux_symlink_args *args)
605{
606	char *path, *to;
607	int error;
608
609	LCONVPATHEXIST(td, args->path, &path);
610	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
611	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
612	if (to == NULL) {
613		LFREEPATH(path);
614		return (error);
615	}
616
617#ifdef DEBUG
618	if (ldebug(symlink))
619		printf(ARGS(symlink, "%s, %s"), path, to);
620#endif
621	error = kern_symlink(td, path, to, UIO_SYSSPACE);
622	LFREEPATH(path);
623	LFREEPATH(to);
624	return (error);
625}
626
627int
628linux_readlink(struct thread *td, struct linux_readlink_args *args)
629{
630	char *name;
631	int error;
632
633	LCONVPATHEXIST(td, args->name, &name);
634
635#ifdef DEBUG
636	if (ldebug(readlink))
637		printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
638		    args->count);
639#endif
640	error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
641	    args->count);
642	LFREEPATH(name);
643	return (error);
644}
645
646int
647linux_truncate(struct thread *td, struct linux_truncate_args *args)
648{
649	char *path;
650	int error;
651
652	LCONVPATHEXIST(td, args->path, &path);
653
654#ifdef DEBUG
655	if (ldebug(truncate))
656		printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
657#endif
658
659	error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
660	LFREEPATH(path);
661	return (error);
662}
663
664int
665linux_link(struct thread *td, struct linux_link_args *args)
666{
667	char *path, *to;
668	int error;
669
670	LCONVPATHEXIST(td, args->path, &path);
671	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
672	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
673	if (to == NULL) {
674		LFREEPATH(path);
675		return (error);
676	}
677
678#ifdef DEBUG
679	if (ldebug(link))
680		printf(ARGS(link, "%s, %s"), path, to);
681#endif
682	error = kern_link(td, path, to, UIO_SYSSPACE);
683	LFREEPATH(path);
684	LFREEPATH(to);
685	return (error);
686}
687
688#ifndef __alpha__
689int
690linux_fdatasync(td, uap)
691	struct thread *td;
692	struct linux_fdatasync_args *uap;
693{
694	struct fsync_args bsd;
695
696	bsd.fd = uap->fd;
697	return fsync(td, &bsd);
698}
699#endif /*!__alpha__*/
700
701int
702linux_pread(td, uap)
703	struct thread *td;
704	struct linux_pread_args *uap;
705{
706	struct pread_args bsd;
707
708	bsd.fd = uap->fd;
709	bsd.buf = uap->buf;
710	bsd.nbyte = uap->nbyte;
711	bsd.offset = uap->offset;
712	return pread(td, &bsd);
713}
714
715int
716linux_pwrite(td, uap)
717	struct thread *td;
718	struct linux_pwrite_args *uap;
719{
720	struct pwrite_args bsd;
721
722	bsd.fd = uap->fd;
723	bsd.buf = uap->buf;
724	bsd.nbyte = uap->nbyte;
725	bsd.offset = uap->offset;
726	return pwrite(td, &bsd);
727}
728
729int
730linux_mount(struct thread *td, struct linux_mount_args *args)
731{
732	struct ufs_args ufs;
733	char fstypename[MFSNAMELEN];
734	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
735	int error;
736	int fsflags;
737	void *fsdata;
738
739	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
740	    NULL);
741	if (error)
742		return (error);
743	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
744	if (error)
745		return (error);
746	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
747	if (error)
748		return (error);
749
750#ifdef DEBUG
751	if (ldebug(mount))
752		printf(ARGS(mount, "%s, %s, %s"),
753		    fstypename, mntfromname, mntonname);
754#endif
755
756	if (strcmp(fstypename, "ext2") == 0) {
757		strcpy(fstypename, "ext2fs");
758		fsdata = &ufs;
759		ufs.fspec = mntfromname;
760#define DEFAULT_ROOTID		-2
761		ufs.export.ex_root = DEFAULT_ROOTID;
762		ufs.export.ex_flags =
763		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
764	} else if (strcmp(fstypename, "proc") == 0) {
765		strcpy(fstypename, "linprocfs");
766		fsdata = NULL;
767	} else {
768		return (ENODEV);
769	}
770
771	fsflags = 0;
772
773	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
774		/*
775		 * Linux SYNC flag is not included; the closest equivalent
776		 * FreeBSD has is !ASYNC, which is our default.
777		 */
778		if (args->rwflag & LINUX_MS_RDONLY)
779			fsflags |= MNT_RDONLY;
780		if (args->rwflag & LINUX_MS_NOSUID)
781			fsflags |= MNT_NOSUID;
782		if (args->rwflag & LINUX_MS_NODEV)
783			fsflags |= MNT_NODEV;
784		if (args->rwflag & LINUX_MS_NOEXEC)
785			fsflags |= MNT_NOEXEC;
786		if (args->rwflag & LINUX_MS_REMOUNT)
787			fsflags |= MNT_UPDATE;
788	}
789
790	if (strcmp(fstypename, "linprocfs") == 0) {
791		error = kernel_vmount(fsflags,
792			"fstype", fstypename,
793			"fspath", mntonname,
794			NULL);
795	} else
796		error = vfs_mount(td, fstypename, mntonname, fsflags, fsdata);
797	return (error);
798}
799
800int
801linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
802{
803	struct linux_umount_args args2;
804
805	args2.path = args->path;
806	args2.flags = 0;
807	return (linux_umount(td, &args2));
808}
809
810int
811linux_umount(struct thread *td, struct linux_umount_args *args)
812{
813	struct unmount_args bsd;
814
815	bsd.path = args->path;
816	bsd.flags = args->flags;	/* XXX correct? */
817	return (unmount(td, &bsd));
818}
819
820/*
821 * fcntl family of syscalls
822 */
823
824struct l_flock {
825	l_short		l_type;
826	l_short		l_whence;
827	l_off_t		l_start;
828	l_off_t		l_len;
829	l_pid_t		l_pid;
830};
831
832static void
833linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
834{
835	switch (linux_flock->l_type) {
836	case LINUX_F_RDLCK:
837		bsd_flock->l_type = F_RDLCK;
838		break;
839	case LINUX_F_WRLCK:
840		bsd_flock->l_type = F_WRLCK;
841		break;
842	case LINUX_F_UNLCK:
843		bsd_flock->l_type = F_UNLCK;
844		break;
845	default:
846		bsd_flock->l_type = -1;
847		break;
848	}
849	bsd_flock->l_whence = linux_flock->l_whence;
850	bsd_flock->l_start = (off_t)linux_flock->l_start;
851	bsd_flock->l_len = (off_t)linux_flock->l_len;
852	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
853}
854
855static void
856bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
857{
858	switch (bsd_flock->l_type) {
859	case F_RDLCK:
860		linux_flock->l_type = LINUX_F_RDLCK;
861		break;
862	case F_WRLCK:
863		linux_flock->l_type = LINUX_F_WRLCK;
864		break;
865	case F_UNLCK:
866		linux_flock->l_type = LINUX_F_UNLCK;
867		break;
868	}
869	linux_flock->l_whence = bsd_flock->l_whence;
870	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
871	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
872	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
873}
874
875#if defined(__i386__)
876struct l_flock64 {
877	l_short		l_type;
878	l_short		l_whence;
879	l_loff_t	l_start;
880	l_loff_t	l_len;
881	l_pid_t		l_pid;
882};
883
884static void
885linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
886{
887	switch (linux_flock->l_type) {
888	case LINUX_F_RDLCK:
889		bsd_flock->l_type = F_RDLCK;
890		break;
891	case LINUX_F_WRLCK:
892		bsd_flock->l_type = F_WRLCK;
893		break;
894	case LINUX_F_UNLCK:
895		bsd_flock->l_type = F_UNLCK;
896		break;
897	default:
898		bsd_flock->l_type = -1;
899		break;
900	}
901	bsd_flock->l_whence = linux_flock->l_whence;
902	bsd_flock->l_start = (off_t)linux_flock->l_start;
903	bsd_flock->l_len = (off_t)linux_flock->l_len;
904	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
905}
906
907static void
908bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
909{
910	switch (bsd_flock->l_type) {
911	case F_RDLCK:
912		linux_flock->l_type = LINUX_F_RDLCK;
913		break;
914	case F_WRLCK:
915		linux_flock->l_type = LINUX_F_WRLCK;
916		break;
917	case F_UNLCK:
918		linux_flock->l_type = LINUX_F_UNLCK;
919		break;
920	}
921	linux_flock->l_whence = bsd_flock->l_whence;
922	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
923	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
924	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
925}
926#endif /* __i386__ */
927
928#if defined(__alpha__)
929#define	linux_fcntl64_args	linux_fcntl_args
930#endif
931
932static int
933fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
934{
935	struct l_flock linux_flock;
936	struct flock bsd_flock;
937	struct file *fp;
938	long arg;
939	int error, result;
940
941	switch (args->cmd) {
942	case LINUX_F_DUPFD:
943		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
944
945	case LINUX_F_GETFD:
946		return (kern_fcntl(td, args->fd, F_GETFD, 0));
947
948	case LINUX_F_SETFD:
949		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
950
951	case LINUX_F_GETFL:
952		error = kern_fcntl(td, args->fd, F_GETFL, 0);
953		result = td->td_retval[0];
954		td->td_retval[0] = 0;
955		if (result & O_RDONLY)
956			td->td_retval[0] |= LINUX_O_RDONLY;
957		if (result & O_WRONLY)
958			td->td_retval[0] |= LINUX_O_WRONLY;
959		if (result & O_RDWR)
960			td->td_retval[0] |= LINUX_O_RDWR;
961		if (result & O_NDELAY)
962			td->td_retval[0] |= LINUX_O_NONBLOCK;
963		if (result & O_APPEND)
964			td->td_retval[0] |= LINUX_O_APPEND;
965		if (result & O_FSYNC)
966			td->td_retval[0] |= LINUX_O_SYNC;
967		if (result & O_ASYNC)
968			td->td_retval[0] |= LINUX_FASYNC;
969		return (error);
970
971	case LINUX_F_SETFL:
972		arg = 0;
973		if (args->arg & LINUX_O_NDELAY)
974			arg |= O_NONBLOCK;
975		if (args->arg & LINUX_O_APPEND)
976			arg |= O_APPEND;
977		if (args->arg & LINUX_O_SYNC)
978			arg |= O_FSYNC;
979		if (args->arg & LINUX_FASYNC)
980			arg |= O_ASYNC;
981		return (kern_fcntl(td, args->fd, F_SETFL, arg));
982
983	case LINUX_F_GETLK:
984		error = copyin((void *)args->arg, &linux_flock,
985		    sizeof(linux_flock));
986		if (error)
987			return (error);
988		linux_to_bsd_flock(&linux_flock, &bsd_flock);
989		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
990		if (error)
991			return (error);
992		bsd_to_linux_flock(&bsd_flock, &linux_flock);
993		return (copyout(&linux_flock, (void *)args->arg,
994		    sizeof(linux_flock)));
995
996	case LINUX_F_SETLK:
997		error = copyin((void *)args->arg, &linux_flock,
998		    sizeof(linux_flock));
999		if (error)
1000			return (error);
1001		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1002		return (kern_fcntl(td, args->fd, F_SETLK,
1003		    (intptr_t)&bsd_flock));
1004
1005	case LINUX_F_SETLKW:
1006		error = copyin((void *)args->arg, &linux_flock,
1007		    sizeof(linux_flock));
1008		if (error)
1009			return (error);
1010		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1011		return (kern_fcntl(td, args->fd, F_SETLKW,
1012		     (intptr_t)&bsd_flock));
1013
1014	case LINUX_F_GETOWN:
1015		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1016
1017	case LINUX_F_SETOWN:
1018		/*
1019		 * XXX some Linux applications depend on F_SETOWN having no
1020		 * significant effect for pipes (SIGIO is not delivered for
1021		 * pipes under Linux-2.2.35 at least).
1022		 */
1023		error = fget(td, args->fd, &fp);
1024		if (error)
1025			return (error);
1026		if (fp->f_type == DTYPE_PIPE) {
1027			fdrop(fp, td);
1028			return (EINVAL);
1029		}
1030		fdrop(fp, td);
1031
1032		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1033	}
1034
1035	return (EINVAL);
1036}
1037
1038int
1039linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1040{
1041	struct linux_fcntl64_args args64;
1042
1043#ifdef DEBUG
1044	if (ldebug(fcntl))
1045		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1046#endif
1047
1048	args64.fd = args->fd;
1049	args64.cmd = args->cmd;
1050	args64.arg = args->arg;
1051	return (fcntl_common(td, &args64));
1052}
1053
1054#if defined(__i386__)
1055int
1056linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1057{
1058	struct l_flock64 linux_flock;
1059	struct flock bsd_flock;
1060	int error;
1061
1062#ifdef DEBUG
1063	if (ldebug(fcntl64))
1064		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1065#endif
1066
1067	switch (args->cmd) {
1068	case LINUX_F_GETLK64:
1069		error = copyin((void *)args->arg, &linux_flock,
1070		    sizeof(linux_flock));
1071		if (error)
1072			return (error);
1073		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1074		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1075		if (error)
1076			return (error);
1077		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1078		return (copyout(&linux_flock, (void *)args->arg,
1079			    sizeof(linux_flock)));
1080
1081	case LINUX_F_SETLK64:
1082		error = copyin((void *)args->arg, &linux_flock,
1083		    sizeof(linux_flock));
1084		if (error)
1085			return (error);
1086		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1087		return (kern_fcntl(td, args->fd, F_SETLK,
1088		    (intptr_t)&bsd_flock));
1089
1090	case LINUX_F_SETLKW64:
1091		error = copyin((void *)args->arg, &linux_flock,
1092		    sizeof(linux_flock));
1093		if (error)
1094			return (error);
1095		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1096		return (kern_fcntl(td, args->fd, F_SETLKW,
1097		    (intptr_t)&bsd_flock));
1098	}
1099
1100	return (fcntl_common(td, args));
1101}
1102#endif /* __i386__ */
1103
1104int
1105linux_chown(struct thread *td, struct linux_chown_args *args)
1106{
1107	char *path;
1108	int error;
1109
1110	LCONVPATHEXIST(td, args->path, &path);
1111
1112#ifdef DEBUG
1113	if (ldebug(chown))
1114		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1115#endif
1116	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1117	LFREEPATH(path);
1118	return (error);
1119}
1120
1121int
1122linux_lchown(struct thread *td, struct linux_lchown_args *args)
1123{
1124	char *path;
1125	int error;
1126
1127	LCONVPATHEXIST(td, args->path, &path);
1128
1129#ifdef DEBUG
1130	if (ldebug(lchown))
1131		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1132#endif
1133	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1134	LFREEPATH(path);
1135	return (error);
1136}
1137