linux_file.c revision 156842
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 156842 2006-03-18 18:20:17Z netchild $");
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#ifdef COMPAT_LINUX32
58#include <machine/../linux32/linux.h>
59#include <machine/../linux32/linux32_proto.h>
60#else
61#include <machine/../linux/linux.h>
62#include <machine/../linux/linux_proto.h>
63#endif
64#include <compat/linux/linux_util.h>
65
66#ifndef __alpha__
67int
68linux_creat(struct thread *td, struct linux_creat_args *args)
69{
70    char *path;
71    int error;
72
73    LCONVPATHEXIST(td, args->path, &path);
74
75#ifdef DEBUG
76	if (ldebug(creat))
77		printf(ARGS(creat, "%s, %d"), path, args->mode);
78#endif
79    error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
80	args->mode);
81    LFREEPATH(path);
82    return (error);
83}
84#endif /*!__alpha__*/
85
86int
87linux_open(struct thread *td, struct linux_open_args *args)
88{
89    struct proc *p = td->td_proc;
90    char *path;
91    int bsd_flags, error;
92
93    if (args->flags & LINUX_O_CREAT)
94	LCONVPATHCREAT(td, args->path, &path);
95    else
96	LCONVPATHEXIST(td, args->path, &path);
97
98#ifdef DEBUG
99	if (ldebug(open))
100		printf(ARGS(open, "%s, 0x%x, 0x%x"),
101		    path, args->flags, args->mode);
102#endif
103    bsd_flags = 0;
104    if (args->flags & LINUX_O_RDONLY)
105	bsd_flags |= O_RDONLY;
106    if (args->flags & LINUX_O_WRONLY)
107	bsd_flags |= O_WRONLY;
108    if (args->flags & LINUX_O_RDWR)
109	bsd_flags |= O_RDWR;
110    if (args->flags & LINUX_O_NDELAY)
111	bsd_flags |= O_NONBLOCK;
112    if (args->flags & LINUX_O_APPEND)
113	bsd_flags |= O_APPEND;
114    if (args->flags & LINUX_O_SYNC)
115	bsd_flags |= O_FSYNC;
116    if (args->flags & LINUX_O_NONBLOCK)
117	bsd_flags |= O_NONBLOCK;
118    if (args->flags & LINUX_FASYNC)
119	bsd_flags |= O_ASYNC;
120    if (args->flags & LINUX_O_CREAT)
121	bsd_flags |= O_CREAT;
122    if (args->flags & LINUX_O_TRUNC)
123	bsd_flags |= O_TRUNC;
124    if (args->flags & LINUX_O_EXCL)
125	bsd_flags |= O_EXCL;
126    if (args->flags & LINUX_O_NOCTTY)
127	bsd_flags |= O_NOCTTY;
128
129    error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
130    PROC_LOCK(p);
131    if (!error && !(bsd_flags & O_NOCTTY) &&
132	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
133	struct file *fp;
134
135	PROC_UNLOCK(p);
136	error = fget(td, td->td_retval[0], &fp);
137	if (!error) {
138		if (fp->f_type == DTYPE_VNODE)
139			fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
140			    td);
141	    fdrop(fp, td);
142	}
143    } else {
144	PROC_UNLOCK(p);
145#ifdef DEBUG
146	if (ldebug(open))
147		printf(LMSG("open returns error %d"), error);
148#endif
149    }
150    LFREEPATH(path);
151    return error;
152}
153
154int
155linux_lseek(struct thread *td, struct linux_lseek_args *args)
156{
157
158    struct lseek_args /* {
159	int fd;
160	int pad;
161	off_t offset;
162	int whence;
163    } */ tmp_args;
164    int error;
165
166#ifdef DEBUG
167	if (ldebug(lseek))
168		printf(ARGS(lseek, "%d, %ld, %d"),
169		    args->fdes, (long)args->off, args->whence);
170#endif
171    tmp_args.fd = args->fdes;
172    tmp_args.offset = (off_t)args->off;
173    tmp_args.whence = args->whence;
174    error = lseek(td, &tmp_args);
175    return error;
176}
177
178#ifndef __alpha__
179int
180linux_llseek(struct thread *td, struct linux_llseek_args *args)
181{
182	struct lseek_args bsd_args;
183	int error;
184	off_t off;
185
186#ifdef DEBUG
187	if (ldebug(llseek))
188		printf(ARGS(llseek, "%d, %d:%d, %d"),
189		    args->fd, args->ohigh, args->olow, args->whence);
190#endif
191	off = (args->olow) | (((off_t) args->ohigh) << 32);
192
193	bsd_args.fd = args->fd;
194	bsd_args.offset = off;
195	bsd_args.whence = args->whence;
196
197	if ((error = lseek(td, &bsd_args)))
198		return error;
199
200	if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
201		return error;
202
203	td->td_retval[0] = 0;
204	return 0;
205}
206#endif /*!__alpha__*/
207
208#ifndef __alpha__
209int
210linux_readdir(struct thread *td, struct linux_readdir_args *args)
211{
212	struct linux_getdents_args lda;
213
214	lda.fd = args->fd;
215	lda.dent = args->dent;
216	lda.count = 1;
217	return linux_getdents(td, &lda);
218}
219#endif /*!__alpha__*/
220
221/*
222 * Note that linux_getdents(2) and linux_getdents64(2) have the same
223 * arguments. They only differ in the definition of struct dirent they
224 * operate on. We use this to common the code, with the exception of
225 * accessing struct dirent. Note that linux_readdir(2) is implemented
226 * by means of linux_getdents(2). In this case we never operate on
227 * struct dirent64 and thus don't need to handle it...
228 */
229
230struct l_dirent {
231	l_long		d_ino;
232	l_off_t		d_off;
233	l_ushort	d_reclen;
234	char		d_name[LINUX_NAME_MAX + 1];
235};
236
237struct l_dirent64 {
238	uint64_t	d_ino;
239	int64_t		d_off;
240	l_ushort	d_reclen;
241	u_char		d_type;
242	char		d_name[LINUX_NAME_MAX + 1];
243};
244
245#define LINUX_RECLEN(de,namlen) \
246    ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
247
248#define	LINUX_DIRBLKSIZ		512
249
250static int
251getdents_common(struct thread *td, struct linux_getdents64_args *args,
252    int is64bit)
253{
254	struct dirent *bdp;
255	struct vnode *vp;
256	caddr_t inp, buf;		/* BSD-format */
257	int len, reclen;		/* BSD-format */
258	caddr_t outp;			/* Linux-format */
259	int resid, linuxreclen=0;	/* Linux-format */
260	struct file *fp;
261	struct uio auio;
262	struct iovec aiov;
263	off_t off;
264	struct l_dirent linux_dirent;
265	struct l_dirent64 linux_dirent64;
266	int buflen, error, eofflag, nbytes, justone;
267	u_long *cookies = NULL, *cookiep;
268	int ncookies;
269
270	if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
271		return (error);
272
273	if ((fp->f_flag & FREAD) == 0) {
274		fdrop(fp, td);
275		return (EBADF);
276	}
277
278	vp = fp->f_vnode;
279	if (vp->v_type != VDIR) {
280		fdrop(fp, td);
281		return (EINVAL);
282	}
283
284	nbytes = args->count;
285	if (nbytes == 1) {
286		/* readdir(2) case. Always struct dirent. */
287		if (is64bit) {
288			fdrop(fp, td);
289			return (EINVAL);
290		}
291		nbytes = sizeof(linux_dirent);
292		justone = 1;
293	} else
294		justone = 0;
295
296	off = fp->f_offset;
297
298	buflen = max(LINUX_DIRBLKSIZ, nbytes);
299	buflen = min(buflen, MAXBSIZE);
300	buf = malloc(buflen, M_TEMP, M_WAITOK);
301	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
302
303again:
304	aiov.iov_base = buf;
305	aiov.iov_len = buflen;
306	auio.uio_iov = &aiov;
307	auio.uio_iovcnt = 1;
308	auio.uio_rw = UIO_READ;
309	auio.uio_segflg = UIO_SYSSPACE;
310	auio.uio_td = td;
311	auio.uio_resid = buflen;
312	auio.uio_offset = off;
313
314	if (cookies) {
315		free(cookies, M_TEMP);
316		cookies = NULL;
317	}
318
319#ifdef MAC
320	/*
321	 * Do directory search MAC check using non-cached credentials.
322	 */
323	if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
324		goto out;
325#endif /* MAC */
326	if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
327		 &cookies)))
328		goto out;
329
330	inp = buf;
331	outp = (caddr_t)args->dirent;
332	resid = nbytes;
333	if ((len = buflen - auio.uio_resid) <= 0)
334		goto eof;
335
336	cookiep = cookies;
337
338	if (cookies) {
339		/*
340		 * When using cookies, the vfs has the option of reading from
341		 * a different offset than that supplied (UFS truncates the
342		 * offset to a block boundary to make sure that it never reads
343		 * partway through a directory entry, even if the directory
344		 * has been compacted).
345		 */
346		while (len > 0 && ncookies > 0 && *cookiep <= off) {
347			bdp = (struct dirent *) inp;
348			len -= bdp->d_reclen;
349			inp += bdp->d_reclen;
350			cookiep++;
351			ncookies--;
352		}
353	}
354
355	while (len > 0) {
356		if (cookiep && ncookies == 0)
357			break;
358		bdp = (struct dirent *) inp;
359		reclen = bdp->d_reclen;
360		if (reclen & 3) {
361			error = EFAULT;
362			goto out;
363		}
364
365		if (bdp->d_fileno == 0) {
366			inp += reclen;
367			if (cookiep) {
368				off = *cookiep++;
369				ncookies--;
370			} else
371				off += reclen;
372
373			len -= reclen;
374			continue;
375		}
376
377		linuxreclen = (is64bit)
378		    ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
379		    : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
380
381		if (reclen > len || resid < linuxreclen) {
382			outp++;
383			break;
384		}
385
386		if (justone) {
387			/* readdir(2) case. */
388			linux_dirent.d_ino = (l_long)bdp->d_fileno;
389			linux_dirent.d_off = (l_off_t)linuxreclen;
390			linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
391			strcpy(linux_dirent.d_name, bdp->d_name);
392			error = copyout(&linux_dirent, outp, linuxreclen);
393		} else {
394			if (is64bit) {
395				linux_dirent64.d_ino = bdp->d_fileno;
396				linux_dirent64.d_off = (cookiep)
397				    ? (l_off_t)*cookiep
398				    : (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_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
671{
672	struct ftruncate_args /* {
673		int fd;
674		int pad;
675		off_t length;
676		} */ nuap;
677
678	nuap.fd = args->fd;
679	nuap.pad = 0;
680	nuap.length = args->length;
681	return (ftruncate(td, &nuap));
682}
683
684int
685linux_link(struct thread *td, struct linux_link_args *args)
686{
687	char *path, *to;
688	int error;
689
690	LCONVPATHEXIST(td, args->path, &path);
691	/* Expand LCONVPATHCREATE so that `path' can be freed on errors */
692	error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
693	if (to == NULL) {
694		LFREEPATH(path);
695		return (error);
696	}
697
698#ifdef DEBUG
699	if (ldebug(link))
700		printf(ARGS(link, "%s, %s"), path, to);
701#endif
702	error = kern_link(td, path, to, UIO_SYSSPACE);
703	LFREEPATH(path);
704	LFREEPATH(to);
705	return (error);
706}
707
708#ifndef __alpha__
709int
710linux_fdatasync(td, uap)
711	struct thread *td;
712	struct linux_fdatasync_args *uap;
713{
714	struct fsync_args bsd;
715
716	bsd.fd = uap->fd;
717	return fsync(td, &bsd);
718}
719#endif /*!__alpha__*/
720
721int
722linux_pread(td, uap)
723	struct thread *td;
724	struct linux_pread_args *uap;
725{
726	struct pread_args bsd;
727
728	bsd.fd = uap->fd;
729	bsd.buf = uap->buf;
730	bsd.nbyte = uap->nbyte;
731	bsd.offset = uap->offset;
732	return pread(td, &bsd);
733}
734
735int
736linux_pwrite(td, uap)
737	struct thread *td;
738	struct linux_pwrite_args *uap;
739{
740	struct pwrite_args bsd;
741
742	bsd.fd = uap->fd;
743	bsd.buf = uap->buf;
744	bsd.nbyte = uap->nbyte;
745	bsd.offset = uap->offset;
746	return pwrite(td, &bsd);
747}
748
749int
750linux_mount(struct thread *td, struct linux_mount_args *args)
751{
752	struct ufs_args ufs;
753	char fstypename[MFSNAMELEN];
754	char mntonname[MNAMELEN], mntfromname[MNAMELEN];
755	int error;
756	int fsflags;
757	void *fsdata;
758
759	error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
760	    NULL);
761	if (error)
762		return (error);
763	error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
764	if (error)
765		return (error);
766	error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
767	if (error)
768		return (error);
769
770#ifdef DEBUG
771	if (ldebug(mount))
772		printf(ARGS(mount, "%s, %s, %s"),
773		    fstypename, mntfromname, mntonname);
774#endif
775
776	if (strcmp(fstypename, "ext2") == 0) {
777		strcpy(fstypename, "ext2fs");
778		fsdata = &ufs;
779		ufs.fspec = mntfromname;
780#define DEFAULT_ROOTID		-2
781		ufs.export.ex_root = DEFAULT_ROOTID;
782		ufs.export.ex_flags =
783		    args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
784	} else if (strcmp(fstypename, "proc") == 0) {
785		strcpy(fstypename, "linprocfs");
786		fsdata = NULL;
787	} else {
788		return (ENODEV);
789	}
790
791	fsflags = 0;
792
793	if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
794		/*
795		 * Linux SYNC flag is not included; the closest equivalent
796		 * FreeBSD has is !ASYNC, which is our default.
797		 */
798		if (args->rwflag & LINUX_MS_RDONLY)
799			fsflags |= MNT_RDONLY;
800		if (args->rwflag & LINUX_MS_NOSUID)
801			fsflags |= MNT_NOSUID;
802		if (args->rwflag & LINUX_MS_NOEXEC)
803			fsflags |= MNT_NOEXEC;
804		if (args->rwflag & LINUX_MS_REMOUNT)
805			fsflags |= MNT_UPDATE;
806	}
807
808	if (strcmp(fstypename, "linprocfs") == 0) {
809		error = kernel_vmount(fsflags,
810			"fstype", fstypename,
811			"fspath", mntonname,
812			NULL);
813	} else
814		error = EOPNOTSUPP;
815	return (error);
816}
817
818int
819linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
820{
821	struct linux_umount_args args2;
822
823	args2.path = args->path;
824	args2.flags = 0;
825	return (linux_umount(td, &args2));
826}
827
828int
829linux_umount(struct thread *td, struct linux_umount_args *args)
830{
831	struct unmount_args bsd;
832
833	bsd.path = args->path;
834	bsd.flags = args->flags;	/* XXX correct? */
835	return (unmount(td, &bsd));
836}
837
838/*
839 * fcntl family of syscalls
840 */
841
842struct l_flock {
843	l_short		l_type;
844	l_short		l_whence;
845	l_off_t		l_start;
846	l_off_t		l_len;
847	l_pid_t		l_pid;
848}
849#if defined(__amd64__) && defined(COMPAT_LINUX32)
850__packed
851#endif
852;
853
854static void
855linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
856{
857	switch (linux_flock->l_type) {
858	case LINUX_F_RDLCK:
859		bsd_flock->l_type = F_RDLCK;
860		break;
861	case LINUX_F_WRLCK:
862		bsd_flock->l_type = F_WRLCK;
863		break;
864	case LINUX_F_UNLCK:
865		bsd_flock->l_type = F_UNLCK;
866		break;
867	default:
868		bsd_flock->l_type = -1;
869		break;
870	}
871	bsd_flock->l_whence = linux_flock->l_whence;
872	bsd_flock->l_start = (off_t)linux_flock->l_start;
873	bsd_flock->l_len = (off_t)linux_flock->l_len;
874	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
875}
876
877static void
878bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
879{
880	switch (bsd_flock->l_type) {
881	case F_RDLCK:
882		linux_flock->l_type = LINUX_F_RDLCK;
883		break;
884	case F_WRLCK:
885		linux_flock->l_type = LINUX_F_WRLCK;
886		break;
887	case F_UNLCK:
888		linux_flock->l_type = LINUX_F_UNLCK;
889		break;
890	}
891	linux_flock->l_whence = bsd_flock->l_whence;
892	linux_flock->l_start = (l_off_t)bsd_flock->l_start;
893	linux_flock->l_len = (l_off_t)bsd_flock->l_len;
894	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
895}
896
897#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
898struct l_flock64 {
899	l_short		l_type;
900	l_short		l_whence;
901	l_loff_t	l_start;
902	l_loff_t	l_len;
903	l_pid_t		l_pid;
904}
905#if defined(__amd64__) && defined(COMPAT_LINUX32)
906__packed
907#endif
908;
909
910static void
911linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
912{
913	switch (linux_flock->l_type) {
914	case LINUX_F_RDLCK:
915		bsd_flock->l_type = F_RDLCK;
916		break;
917	case LINUX_F_WRLCK:
918		bsd_flock->l_type = F_WRLCK;
919		break;
920	case LINUX_F_UNLCK:
921		bsd_flock->l_type = F_UNLCK;
922		break;
923	default:
924		bsd_flock->l_type = -1;
925		break;
926	}
927	bsd_flock->l_whence = linux_flock->l_whence;
928	bsd_flock->l_start = (off_t)linux_flock->l_start;
929	bsd_flock->l_len = (off_t)linux_flock->l_len;
930	bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
931}
932
933static void
934bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
935{
936	switch (bsd_flock->l_type) {
937	case F_RDLCK:
938		linux_flock->l_type = LINUX_F_RDLCK;
939		break;
940	case F_WRLCK:
941		linux_flock->l_type = LINUX_F_WRLCK;
942		break;
943	case F_UNLCK:
944		linux_flock->l_type = LINUX_F_UNLCK;
945		break;
946	}
947	linux_flock->l_whence = bsd_flock->l_whence;
948	linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
949	linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
950	linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
951}
952#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
953
954#if defined(__alpha__)
955#define	linux_fcntl64_args	linux_fcntl_args
956#endif
957
958static int
959fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
960{
961	struct l_flock linux_flock;
962	struct flock bsd_flock;
963	struct file *fp;
964	long arg;
965	int error, result;
966
967	switch (args->cmd) {
968	case LINUX_F_DUPFD:
969		return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
970
971	case LINUX_F_GETFD:
972		return (kern_fcntl(td, args->fd, F_GETFD, 0));
973
974	case LINUX_F_SETFD:
975		return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
976
977	case LINUX_F_GETFL:
978		error = kern_fcntl(td, args->fd, F_GETFL, 0);
979		result = td->td_retval[0];
980		td->td_retval[0] = 0;
981		if (result & O_RDONLY)
982			td->td_retval[0] |= LINUX_O_RDONLY;
983		if (result & O_WRONLY)
984			td->td_retval[0] |= LINUX_O_WRONLY;
985		if (result & O_RDWR)
986			td->td_retval[0] |= LINUX_O_RDWR;
987		if (result & O_NDELAY)
988			td->td_retval[0] |= LINUX_O_NONBLOCK;
989		if (result & O_APPEND)
990			td->td_retval[0] |= LINUX_O_APPEND;
991		if (result & O_FSYNC)
992			td->td_retval[0] |= LINUX_O_SYNC;
993		if (result & O_ASYNC)
994			td->td_retval[0] |= LINUX_FASYNC;
995#ifdef LINUX_O_NOFOLLOW
996		if (result & O_NOFOLLOW)
997			td->td_retval[0] |= LINUX_O_NOFOLLOW;
998#endif
999#ifdef LINUX_O_DIRECT
1000		if (result & O_DIRECT)
1001			td->td_retval[0] |= LINUX_O_DIRECT;
1002#endif
1003		return (error);
1004
1005	case LINUX_F_SETFL:
1006		arg = 0;
1007		if (args->arg & LINUX_O_NDELAY)
1008			arg |= O_NONBLOCK;
1009		if (args->arg & LINUX_O_APPEND)
1010			arg |= O_APPEND;
1011		if (args->arg & LINUX_O_SYNC)
1012			arg |= O_FSYNC;
1013		if (args->arg & LINUX_FASYNC)
1014			arg |= O_ASYNC;
1015#ifdef LINUX_O_NOFOLLOW
1016		if (args->arg & LINUX_O_NOFOLLOW)
1017			arg |= O_NOFOLLOW;
1018#endif
1019#ifdef LINUX_O_DIRECT
1020		if (args->arg & LINUX_O_DIRECT)
1021			arg |= O_DIRECT;
1022#endif
1023		return (kern_fcntl(td, args->fd, F_SETFL, arg));
1024
1025	case LINUX_F_GETLK:
1026		error = copyin((void *)args->arg, &linux_flock,
1027		    sizeof(linux_flock));
1028		if (error)
1029			return (error);
1030		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1031		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1032		if (error)
1033			return (error);
1034		bsd_to_linux_flock(&bsd_flock, &linux_flock);
1035		return (copyout(&linux_flock, (void *)args->arg,
1036		    sizeof(linux_flock)));
1037
1038	case LINUX_F_SETLK:
1039		error = copyin((void *)args->arg, &linux_flock,
1040		    sizeof(linux_flock));
1041		if (error)
1042			return (error);
1043		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1044		return (kern_fcntl(td, args->fd, F_SETLK,
1045		    (intptr_t)&bsd_flock));
1046
1047	case LINUX_F_SETLKW:
1048		error = copyin((void *)args->arg, &linux_flock,
1049		    sizeof(linux_flock));
1050		if (error)
1051			return (error);
1052		linux_to_bsd_flock(&linux_flock, &bsd_flock);
1053		return (kern_fcntl(td, args->fd, F_SETLKW,
1054		     (intptr_t)&bsd_flock));
1055
1056	case LINUX_F_GETOWN:
1057		return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1058
1059	case LINUX_F_SETOWN:
1060		/*
1061		 * XXX some Linux applications depend on F_SETOWN having no
1062		 * significant effect for pipes (SIGIO is not delivered for
1063		 * pipes under Linux-2.2.35 at least).
1064		 */
1065		error = fget(td, args->fd, &fp);
1066		if (error)
1067			return (error);
1068		if (fp->f_type == DTYPE_PIPE) {
1069			fdrop(fp, td);
1070			return (EINVAL);
1071		}
1072		fdrop(fp, td);
1073
1074		return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1075	}
1076
1077	return (EINVAL);
1078}
1079
1080int
1081linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1082{
1083	struct linux_fcntl64_args args64;
1084
1085#ifdef DEBUG
1086	if (ldebug(fcntl))
1087		printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1088#endif
1089
1090	args64.fd = args->fd;
1091	args64.cmd = args->cmd;
1092	args64.arg = args->arg;
1093	return (fcntl_common(td, &args64));
1094}
1095
1096#if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1097int
1098linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1099{
1100	struct l_flock64 linux_flock;
1101	struct flock bsd_flock;
1102	int error;
1103
1104#ifdef DEBUG
1105	if (ldebug(fcntl64))
1106		printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1107#endif
1108
1109	switch (args->cmd) {
1110	case LINUX_F_GETLK64:
1111		error = copyin((void *)args->arg, &linux_flock,
1112		    sizeof(linux_flock));
1113		if (error)
1114			return (error);
1115		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1116		error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1117		if (error)
1118			return (error);
1119		bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1120		return (copyout(&linux_flock, (void *)args->arg,
1121			    sizeof(linux_flock)));
1122
1123	case LINUX_F_SETLK64:
1124		error = copyin((void *)args->arg, &linux_flock,
1125		    sizeof(linux_flock));
1126		if (error)
1127			return (error);
1128		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1129		return (kern_fcntl(td, args->fd, F_SETLK,
1130		    (intptr_t)&bsd_flock));
1131
1132	case LINUX_F_SETLKW64:
1133		error = copyin((void *)args->arg, &linux_flock,
1134		    sizeof(linux_flock));
1135		if (error)
1136			return (error);
1137		linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1138		return (kern_fcntl(td, args->fd, F_SETLKW,
1139		    (intptr_t)&bsd_flock));
1140	}
1141
1142	return (fcntl_common(td, args));
1143}
1144#endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1145
1146int
1147linux_chown(struct thread *td, struct linux_chown_args *args)
1148{
1149	char *path;
1150	int error;
1151
1152	LCONVPATHEXIST(td, args->path, &path);
1153
1154#ifdef DEBUG
1155	if (ldebug(chown))
1156		printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1157#endif
1158	error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1159	LFREEPATH(path);
1160	return (error);
1161}
1162
1163int
1164linux_lchown(struct thread *td, struct linux_lchown_args *args)
1165{
1166	char *path;
1167	int error;
1168
1169	LCONVPATHEXIST(td, args->path, &path);
1170
1171#ifdef DEBUG
1172	if (ldebug(lchown))
1173		printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1174#endif
1175	error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1176	LFREEPATH(path);
1177	return (error);
1178}
1179