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