xref: /freebsd/sys/compat/linux/linux_file.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 withough 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  *  $Id: linux_file.c,v 1.25 1999/05/08 06:39:26 phk Exp $
29  */
30 
31 #include "opt_compat.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/fcntl.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 #include <sys/lock.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/dirent.h>
44 #include <sys/conf.h>
45 #include <sys/tty.h>
46 
47 #include <i386/linux/linux.h>
48 #include <i386/linux/linux_proto.h>
49 #include <i386/linux/linux_util.h>
50 
51 int
52 linux_creat(struct proc *p, struct linux_creat_args *args)
53 {
54     struct open_args /* {
55 	char *path;
56 	int flags;
57 	int mode;
58     } */ bsd_open_args;
59     caddr_t sg;
60 
61     sg = stackgap_init();
62     CHECKALTCREAT(p, &sg, args->path);
63 
64 #ifdef DEBUG
65     printf("Linux-emul(%d): creat(%s, %d)\n",
66 	   p->p_pid, args->path, args->mode);
67 #endif
68     bsd_open_args.path = args->path;
69     bsd_open_args.mode = args->mode;
70     bsd_open_args.flags = O_WRONLY | O_CREAT | O_TRUNC;
71     return open(p, &bsd_open_args);
72 }
73 
74 int
75 linux_open(struct proc *p, struct linux_open_args *args)
76 {
77     struct open_args /* {
78 	char *path;
79 	int flags;
80 	int mode;
81     } */ bsd_open_args;
82     int error;
83     caddr_t sg;
84 
85     sg = stackgap_init();
86 
87     if (args->flags & LINUX_O_CREAT)
88 	CHECKALTCREAT(p, &sg, args->path);
89     else
90 	CHECKALTEXIST(p, &sg, args->path);
91 
92 #ifdef DEBUG
93     printf("Linux-emul(%d): open(%s, 0x%x, 0x%x)\n",
94 	   p->p_pid, args->path, args->flags, args->mode);
95 #endif
96     bsd_open_args.flags = 0;
97     if (args->flags & LINUX_O_RDONLY)
98 	bsd_open_args.flags |= O_RDONLY;
99     if (args->flags & LINUX_O_WRONLY)
100 	bsd_open_args.flags |= O_WRONLY;
101     if (args->flags & LINUX_O_RDWR)
102 	bsd_open_args.flags |= O_RDWR;
103     if (args->flags & LINUX_O_NDELAY)
104 	bsd_open_args.flags |= O_NONBLOCK;
105     if (args->flags & LINUX_O_APPEND)
106 	bsd_open_args.flags |= O_APPEND;
107     if (args->flags & LINUX_O_SYNC)
108 	bsd_open_args.flags |= O_FSYNC;
109     if (args->flags & LINUX_O_NONBLOCK)
110 	bsd_open_args.flags |= O_NONBLOCK;
111     if (args->flags & LINUX_FASYNC)
112 	bsd_open_args.flags |= O_ASYNC;
113     if (args->flags & LINUX_O_CREAT)
114 	bsd_open_args.flags |= O_CREAT;
115     if (args->flags & LINUX_O_TRUNC)
116 	bsd_open_args.flags |= O_TRUNC;
117     if (args->flags & LINUX_O_EXCL)
118 	bsd_open_args.flags |= O_EXCL;
119     if (args->flags & LINUX_O_NOCTTY)
120 	bsd_open_args.flags |= O_NOCTTY;
121     bsd_open_args.path = args->path;
122     bsd_open_args.mode = args->mode;
123 
124     error = open(p, &bsd_open_args);
125     if (!error && !(bsd_open_args.flags & O_NOCTTY) &&
126 	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
127 	struct filedesc *fdp = p->p_fd;
128 	struct file *fp = fdp->fd_ofiles[p->p_retval[0]];
129 
130 	if (fp->f_type == DTYPE_VNODE)
131 	    (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
132     }
133 #ifdef DEBUG
134     printf("Linux-emul(%d): open returns error %d\n",
135 	   p->p_pid, error);
136 #endif
137     return error;
138 }
139 
140 struct linux_flock {
141     short l_type;
142     short l_whence;
143     linux_off_t l_start;
144     linux_off_t l_len;
145     linux_pid_t l_pid;
146 };
147 
148 static void
149 linux_to_bsd_flock(struct linux_flock *linux_flock, struct flock *bsd_flock)
150 {
151     switch (linux_flock->l_type) {
152     case LINUX_F_RDLCK:
153 	bsd_flock->l_type = F_RDLCK;
154 	break;
155     case LINUX_F_WRLCK:
156 	bsd_flock->l_type = F_WRLCK;
157 	break;
158     case LINUX_F_UNLCK:
159 	bsd_flock->l_type = F_UNLCK;
160 	break;
161     }
162     bsd_flock->l_whence = linux_flock->l_whence;
163     bsd_flock->l_start = (off_t)linux_flock->l_start;
164     bsd_flock->l_len = (off_t)linux_flock->l_len;
165     bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
166 }
167 
168 static void
169 bsd_to_linux_flock(struct flock *bsd_flock, struct linux_flock *linux_flock)
170 {
171     switch (bsd_flock->l_type) {
172     case F_RDLCK:
173 	linux_flock->l_type = LINUX_F_RDLCK;
174 	break;
175     case F_WRLCK:
176 	linux_flock->l_type = LINUX_F_WRLCK;
177 	break;
178     case F_UNLCK:
179 	linux_flock->l_type = LINUX_F_UNLCK;
180 	break;
181     }
182     linux_flock->l_whence = bsd_flock->l_whence;
183     linux_flock->l_start = (linux_off_t)bsd_flock->l_start;
184     linux_flock->l_len = (linux_off_t)bsd_flock->l_len;
185     linux_flock->l_pid = (linux_pid_t)bsd_flock->l_pid;
186 }
187 
188 int
189 linux_fcntl(struct proc *p, struct linux_fcntl_args *args)
190 {
191     int error, result;
192     struct fcntl_args /* {
193 	int fd;
194 	int cmd;
195 	int arg;
196     } */ fcntl_args;
197     struct linux_flock linux_flock;
198     struct flock *bsd_flock;
199     struct filedesc *fdp;
200     struct file *fp;
201     struct vnode *vp;
202     struct vattr va;
203     long pgid;
204     struct pgrp *pgrp;
205     struct tty *tp, *(*d_tty) __P((dev_t));
206     caddr_t sg;
207     dev_t dev;
208 
209     sg = stackgap_init();
210     bsd_flock = (struct flock *)stackgap_alloc(&sg, sizeof(struct flock));
211     d_tty = NULL;
212 
213 #ifdef DEBUG
214     printf("Linux-emul(%d): fcntl(%d, %08x, *)\n",
215 	   p->p_pid, args->fd, args->cmd);
216 #endif
217     fcntl_args.fd = args->fd;
218     fcntl_args.arg = 0;
219 
220     switch (args->cmd) {
221     case LINUX_F_DUPFD:
222 	fcntl_args.cmd = F_DUPFD;
223 	return fcntl(p, &fcntl_args);
224 
225     case LINUX_F_GETFD:
226 	fcntl_args.cmd = F_GETFD;
227 	return fcntl(p, &fcntl_args);
228 
229     case LINUX_F_SETFD:
230 	fcntl_args.cmd = F_SETFD;
231 	return fcntl(p, &fcntl_args);
232 
233     case LINUX_F_GETFL:
234 	fcntl_args.cmd = F_GETFL;
235 	error = fcntl(p, &fcntl_args);
236 	result = p->p_retval[0];
237 	p->p_retval[0] = 0;
238 	if (result & O_RDONLY) p->p_retval[0] |= LINUX_O_RDONLY;
239 	if (result & O_WRONLY) p->p_retval[0] |= LINUX_O_WRONLY;
240 	if (result & O_RDWR) p->p_retval[0] |= LINUX_O_RDWR;
241 	if (result & O_NDELAY) p->p_retval[0] |= LINUX_O_NONBLOCK;
242 	if (result & O_APPEND) p->p_retval[0] |= LINUX_O_APPEND;
243 	if (result & O_FSYNC) p->p_retval[0] |= LINUX_O_SYNC;
244 	if (result & O_ASYNC) p->p_retval[0] |= LINUX_FASYNC;
245 	return error;
246 
247     case LINUX_F_SETFL:
248 	if (args->arg & LINUX_O_NDELAY) fcntl_args.arg |= O_NONBLOCK;
249 	if (args->arg & LINUX_O_APPEND) fcntl_args.arg |= O_APPEND;
250 	if (args->arg & LINUX_O_SYNC) fcntl_args.arg |= O_FSYNC;
251 	if (args->arg & LINUX_FASYNC) fcntl_args.arg |= O_ASYNC;
252 	fcntl_args.cmd = F_SETFL;
253 	return fcntl(p, &fcntl_args);
254 
255     case LINUX_F_GETLK:
256 	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
257 		   	    sizeof(struct linux_flock))))
258 	    return error;
259 	linux_to_bsd_flock(&linux_flock, bsd_flock);
260 	fcntl_args.cmd = F_GETLK;
261 	fcntl_args.arg = (int)bsd_flock;
262 	error = fcntl(p, &fcntl_args);
263 	if (error)
264 	    return error;
265 	bsd_to_linux_flock(bsd_flock, &linux_flock);
266 	return copyout((caddr_t)&linux_flock, (caddr_t)args->arg,
267 		       sizeof(struct linux_flock));
268 
269     case LINUX_F_SETLK:
270 	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
271 		   	    sizeof(struct linux_flock))))
272 	    return error;
273 	linux_to_bsd_flock(&linux_flock, bsd_flock);
274 	fcntl_args.cmd = F_SETLK;
275 	fcntl_args.arg = (int)bsd_flock;
276 	return fcntl(p, &fcntl_args);
277 
278     case LINUX_F_SETLKW:
279 	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
280 		   	    sizeof(struct linux_flock))))
281 	    return error;
282 	linux_to_bsd_flock(&linux_flock, bsd_flock);
283 	fcntl_args.cmd = F_SETLKW;
284 	fcntl_args.arg = (int)bsd_flock;
285 	return fcntl(p, &fcntl_args);
286 
287     case LINUX_F_SETOWN:
288     case LINUX_F_GETOWN:
289 	/*
290 	 * We need to route around the normal fcntl() for these calls,
291 	 * since it uses TIOC{G,S}PGRP, which is too restrictive for
292 	 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
293 	 * does not exist.
294 	 */
295 	fdp = p->p_fd;
296 	if ((u_int)args->fd >= fdp->fd_nfiles ||
297 		(fp = fdp->fd_ofiles[args->fd]) == NULL)
298 	    return EBADF;
299 	if (fp->f_type == DTYPE_SOCKET) {
300 	    fcntl_args.cmd = args->cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
301     	    fcntl_args.arg = args->arg;
302 	    return fcntl(p, &fcntl_args);
303 	}
304 	vp = (struct vnode *)fp->f_data;
305 	if (vp->v_type != VCHR)
306 	    return EINVAL;
307 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
308 	    return error;
309 
310 	dev = udev2dev(va.va_rdev, 0);		/* XXX vp->v_rdev ? */
311 	d_tty = devsw(dev)->d_devtotty;
312 	if (!d_tty || (!(tp = (*d_tty)(dev))))
313 	    return EINVAL;
314 	if (args->cmd == LINUX_F_GETOWN) {
315 	    p->p_retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
316 	    return 0;
317 	}
318 	if ((long)args->arg <= 0) {
319 	    pgid = -(long)args->arg;
320 	} else {
321 	    struct proc *p1 = pfind((long)args->arg);
322 	    if (p1 == 0)
323 		return (ESRCH);
324 	    pgid = (long)p1->p_pgrp->pg_id;
325 	}
326 	pgrp = pgfind(pgid);
327 	if (pgrp == NULL || pgrp->pg_session != p->p_session)
328 	    return EPERM;
329 	tp->t_pgrp = pgrp;
330 	return 0;
331     }
332     return EINVAL;
333 }
334 
335 int
336 linux_lseek(struct proc *p, struct linux_lseek_args *args)
337 {
338 
339     struct lseek_args /* {
340 	int fd;
341 	int pad;
342 	off_t offset;
343 	int whence;
344     } */ tmp_args;
345     int error;
346 
347 #ifdef DEBUG
348     printf("Linux-emul(%ld): lseek(%d, %ld, %d)\n",
349 	   (long)p->p_pid, args->fdes, args->off, args->whence);
350 #endif
351     tmp_args.fd = args->fdes;
352     tmp_args.offset = (off_t)args->off;
353     tmp_args.whence = args->whence;
354     error = lseek(p, &tmp_args);
355     return error;
356 }
357 
358 int
359 linux_llseek(struct proc *p, struct linux_llseek_args *args)
360 {
361 	struct lseek_args bsd_args;
362 	int error;
363 	off_t off;
364 
365 #ifdef DEBUG
366         printf("Linux-emul(%d): llseek(%d, %d:%d, %d)\n",
367 	   p->p_pid, args->fd, args->ohigh, args->olow, args->whence);
368 #endif
369 	off = (args->olow) | (((off_t) args->ohigh) << 32);
370 
371 	bsd_args.fd = args->fd;
372 	bsd_args.offset = off;
373 	bsd_args.whence = args->whence;
374 
375 	if ((error = lseek(p, &bsd_args)))
376 		return error;
377 
378 	if ((error = copyout(p->p_retval, (caddr_t)args->res, sizeof (off_t))))
379 		return error;
380 
381 	p->p_retval[0] = 0;
382 	return 0;
383 }
384 
385 
386 struct linux_dirent {
387     long dino;
388     linux_off_t doff;
389     unsigned short dreclen;
390     char dname[LINUX_NAME_MAX + 1];
391 };
392 
393 #define LINUX_RECLEN(de,namlen) \
394     ALIGN((((char *)&(de)->dname - (char *)de) + (namlen) + 1))
395 
396 int
397 linux_readdir(struct proc *p, struct linux_readdir_args *args)
398 {
399 	struct linux_getdents_args lda;
400 
401 	lda.fd = args->fd;
402 	lda.dent = args->dent;
403 	lda.count = 1;
404 	return linux_getdents(p, &lda);
405 }
406 
407 int
408 linux_getdents(struct proc *p, struct linux_getdents_args *args)
409 {
410     register struct dirent *bdp;
411     struct vnode *vp;
412     caddr_t inp, buf;		/* BSD-format */
413     int len, reclen;		/* BSD-format */
414     caddr_t outp;		/* Linux-format */
415     int resid, linuxreclen=0;	/* Linux-format */
416     struct file *fp;
417     struct uio auio;
418     struct iovec aiov;
419     struct vattr va;
420     off_t off;
421     struct linux_dirent linux_dirent;
422     int buflen, error, eofflag, nbytes, justone;
423     u_long *cookies = NULL, *cookiep;
424     int ncookies;
425 
426 #ifdef DEBUG
427     printf("Linux-emul(%d): getdents(%d, *, %d)\n",
428 	   p->p_pid, args->fd, args->count);
429 #endif
430     if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
431 	return (error);
432     }
433 
434     if ((fp->f_flag & FREAD) == 0)
435 	return (EBADF);
436 
437     vp = (struct vnode *) fp->f_data;
438 
439     if (vp->v_type != VDIR)
440 	return (EINVAL);
441 
442     if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
443 	return error;
444     }
445 
446     nbytes = args->count;
447     if (nbytes == 1) {
448 	nbytes = sizeof (struct linux_dirent);
449 	justone = 1;
450     }
451     else
452 	justone = 0;
453 
454     off = fp->f_offset;
455 #define	DIRBLKSIZ	512		/* XXX we used to use ufs's DIRBLKSIZ */
456     buflen = max(DIRBLKSIZ, nbytes);
457     buflen = min(buflen, MAXBSIZE);
458     buf = malloc(buflen, M_TEMP, M_WAITOK);
459     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
460 again:
461     aiov.iov_base = buf;
462     aiov.iov_len = buflen;
463     auio.uio_iov = &aiov;
464     auio.uio_iovcnt = 1;
465     auio.uio_rw = UIO_READ;
466     auio.uio_segflg = UIO_SYSSPACE;
467     auio.uio_procp = p;
468     auio.uio_resid = buflen;
469     auio.uio_offset = off;
470 
471     if (cookies) {
472 	free(cookies, M_TEMP);
473 	cookies = NULL;
474     }
475 
476     error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
477     if (error) {
478 	goto out;
479     }
480 
481     inp = buf;
482     outp = (caddr_t) args->dent;
483     resid = nbytes;
484     if ((len = buflen - auio.uio_resid) <= 0) {
485 	goto eof;
486     }
487 
488     cookiep = cookies;
489 
490     if (cookies) {
491 	/*
492 	 * When using cookies, the vfs has the option of reading from
493 	 * a different offset than that supplied (UFS truncates the
494 	 * offset to a block boundary to make sure that it never reads
495 	 * partway through a directory entry, even if the directory
496 	 * has been compacted).
497 	 */
498 	while (len > 0 && ncookies > 0 && *cookiep <= off) {
499 	    bdp = (struct dirent *) inp;
500 	    len -= bdp->d_reclen;
501 	    inp += bdp->d_reclen;
502 	    cookiep++;
503 	    ncookies--;
504 	}
505     }
506 
507     while (len > 0) {
508 	if (cookiep && ncookies == 0)
509 	    break;
510 	bdp = (struct dirent *) inp;
511 	reclen = bdp->d_reclen;
512 	if (reclen & 3) {
513 	    printf("linux_readdir: reclen=%d\n", reclen);
514 	    error = EFAULT;
515 	    goto out;
516 	}
517 
518 	if (bdp->d_fileno == 0) {
519 	    inp += reclen;
520 	    if (cookiep) {
521 		off = *cookiep++;
522 		ncookies--;
523 	    } else
524 		off += reclen;
525 	    len -= reclen;
526 	    continue;
527 	}
528 	linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
529 	if (reclen > len || resid < linuxreclen) {
530 	    outp++;
531 	    break;
532 	}
533 	linux_dirent.dino = (long) bdp->d_fileno;
534 	if (justone) {
535 	    /*
536 	     * old linux-style readdir usage.
537 	     */
538 	    linux_dirent.doff = (linux_off_t) linuxreclen;
539 	    linux_dirent.dreclen = (u_short) bdp->d_namlen;
540 	} else {
541 	    linux_dirent.doff = (linux_off_t) off;
542 	    linux_dirent.dreclen = (u_short) linuxreclen;
543 	}
544 	strcpy(linux_dirent.dname, bdp->d_name);
545 	if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
546 	    goto out;
547 	}
548 	inp += reclen;
549 	if (cookiep) {
550 	    off = *cookiep++;
551 	    ncookies--;
552 	} else
553 	    off += reclen;
554 	outp += linuxreclen;
555 	resid -= linuxreclen;
556 	len -= reclen;
557 	if (justone)
558 	    break;
559     }
560 
561     if (outp == (caddr_t) args->dent)
562 	goto again;
563     fp->f_offset = off;
564 
565     if (justone)
566 	nbytes = resid + linuxreclen;
567 
568 eof:
569     p->p_retval[0] = nbytes - resid;
570 out:
571     if (cookies)
572 	free(cookies, M_TEMP);
573     VOP_UNLOCK(vp, 0, p);
574     free(buf, M_TEMP);
575     return error;
576 }
577 
578 /*
579  * These exist mainly for hooks for doing /compat/linux translation.
580  */
581 
582 int
583 linux_access(struct proc *p, struct linux_access_args *args)
584 {
585 	struct access_args bsd;
586 	caddr_t sg;
587 
588 	sg = stackgap_init();
589 	CHECKALTEXIST(p, &sg, args->path);
590 
591 #ifdef DEBUG
592         printf("Linux-emul(%d): access(%s, %d)\n",
593 	    p->p_pid, args->path, args->flags);
594 #endif
595 	bsd.path = args->path;
596 	bsd.flags = args->flags;
597 
598 	return access(p, &bsd);
599 }
600 
601 int
602 linux_unlink(struct proc *p, struct linux_unlink_args *args)
603 {
604 	struct unlink_args bsd;
605 	caddr_t sg;
606 
607 	sg = stackgap_init();
608 	CHECKALTEXIST(p, &sg, args->path);
609 
610 #ifdef DEBUG
611 	printf("Linux-emul(%d): unlink(%s)\n",
612 	   p->p_pid, args->path);
613 #endif
614 	bsd.path = args->path;
615 
616 	return unlink(p, &bsd);
617 }
618 
619 int
620 linux_chdir(struct proc *p, struct linux_chdir_args *args)
621 {
622 	struct chdir_args bsd;
623 	caddr_t sg;
624 
625 	sg = stackgap_init();
626 	CHECKALTEXIST(p, &sg, args->path);
627 
628 #ifdef DEBUG
629 	printf("Linux-emul(%d): chdir(%s)\n",
630 	   p->p_pid, args->path);
631 #endif
632 	bsd.path = args->path;
633 
634 	return chdir(p, &bsd);
635 }
636 
637 int
638 linux_chmod(struct proc *p, struct linux_chmod_args *args)
639 {
640 	struct chmod_args bsd;
641 	caddr_t sg;
642 
643 	sg = stackgap_init();
644 	CHECKALTEXIST(p, &sg, args->path);
645 
646 #ifdef DEBUG
647         printf("Linux-emul(%d): chmod(%s, %d)\n",
648 	    p->p_pid, args->path, args->mode);
649 #endif
650 	bsd.path = args->path;
651 	bsd.mode = args->mode;
652 
653 	return chmod(p, &bsd);
654 }
655 
656 int
657 linux_chown(struct proc *p, struct linux_chown_args *args)
658 {
659 	struct chown_args bsd;
660 	caddr_t sg;
661 
662 	sg = stackgap_init();
663 	CHECKALTEXIST(p, &sg, args->path);
664 
665 #ifdef DEBUG
666         printf("Linux-emul(%d): chown(%s, %d, %d)\n",
667 	    p->p_pid, args->path, args->uid, args->gid);
668 #endif
669 	bsd.path = args->path;
670 	/* XXX size casts here */
671 	bsd.uid = args->uid;
672 	bsd.gid = args->gid;
673 
674 	return chown(p, &bsd);
675 }
676 
677 int
678 linux_lchown(struct proc *p, struct linux_lchown_args *args)
679 {
680 	struct lchown_args bsd;
681 	caddr_t sg;
682 
683 	sg = stackgap_init();
684 	CHECKALTEXIST(p, &sg, args->path);
685 
686 #ifdef DEBUG
687         printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
688 	    p->p_pid, args->path, args->uid, args->gid);
689 #endif
690 	bsd.path = args->path;
691 	/* XXX size casts here */
692 	bsd.uid = args->uid;
693 	bsd.gid = args->gid;
694 
695 	return lchown(p, &bsd);
696 }
697 
698 int
699 linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
700 {
701 	struct mkdir_args bsd;
702 	caddr_t sg;
703 
704 	sg = stackgap_init();
705 	CHECKALTCREAT(p, &sg, args->path);
706 
707 #ifdef DEBUG
708         printf("Linux-emul(%d): mkdir(%s, %d)\n",
709 	    p->p_pid, args->path, args->mode);
710 #endif
711 	bsd.path = args->path;
712 	bsd.mode = args->mode;
713 
714 	return mkdir(p, &bsd);
715 }
716 
717 int
718 linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
719 {
720 	struct rmdir_args bsd;
721 	caddr_t sg;
722 
723 	sg = stackgap_init();
724 	CHECKALTEXIST(p, &sg, args->path);
725 
726 #ifdef DEBUG
727         printf("Linux-emul(%d): rmdir(%s)\n",
728 	    p->p_pid, args->path);
729 #endif
730 	bsd.path = args->path;
731 
732 	return rmdir(p, &bsd);
733 }
734 
735 int
736 linux_rename(struct proc *p, struct linux_rename_args *args)
737 {
738 	struct rename_args bsd;
739 	caddr_t sg;
740 
741 	sg = stackgap_init();
742 	CHECKALTEXIST(p, &sg, args->from);
743 	CHECKALTCREAT(p, &sg, args->to);
744 
745 #ifdef DEBUG
746         printf("Linux-emul(%d): rename(%s, %s)\n",
747 	    p->p_pid, args->from, args->to);
748 #endif
749 	bsd.from = args->from;
750 	bsd.to = args->to;
751 
752 	return rename(p, &bsd);
753 }
754 
755 int
756 linux_symlink(struct proc *p, struct linux_symlink_args *args)
757 {
758 	struct symlink_args bsd;
759 	caddr_t sg;
760 
761 	sg = stackgap_init();
762 	CHECKALTEXIST(p, &sg, args->path);
763 	CHECKALTCREAT(p, &sg, args->to);
764 
765 #ifdef DEBUG
766         printf("Linux-emul(%d): symlink(%s, %s)\n",
767 	    p->p_pid, args->path, args->to);
768 #endif
769 	bsd.path = args->path;
770 	bsd.link = args->to;
771 
772 	return symlink(p, &bsd);
773 }
774 
775 int
776 linux_execve(struct proc *p, struct linux_execve_args *args)
777 {
778 	struct execve_args bsd;
779 	caddr_t sg;
780 
781 	sg = stackgap_init();
782 	CHECKALTEXIST(p, &sg, args->path);
783 
784 #ifdef DEBUG
785         printf("Linux-emul(%d): execve(%s)\n",
786 	    p->p_pid, args->path);
787 #endif
788 	bsd.fname = args->path;
789 	bsd.argv = args->argp;
790 	bsd.envv = args->envp;
791 
792 	return execve(p, &bsd);
793 }
794 
795 int
796 linux_readlink(struct proc *p, struct linux_readlink_args *args)
797 {
798 	struct readlink_args bsd;
799 	caddr_t sg;
800 
801 	sg = stackgap_init();
802 	CHECKALTEXIST(p, &sg, args->name);
803 
804 #ifdef DEBUG
805         printf("Linux-emul(%ld): readlink(%s, %p, %d)\n",
806 	    (long)p->p_pid, args->name, (void *)args->buf, args->count);
807 #endif
808 	bsd.path = args->name;
809 	bsd.buf = args->buf;
810 	bsd.count = args->count;
811 
812 	return readlink(p, &bsd);
813 }
814 
815 int
816 linux_truncate(struct proc *p, struct linux_truncate_args *args)
817 {
818 	struct truncate_args bsd;
819 	caddr_t sg;
820 
821 	sg = stackgap_init();
822 	CHECKALTEXIST(p, &sg, args->path);
823 
824 #ifdef DEBUG
825         printf("Linux-emul(%d): truncate(%s, %ld)\n",
826 	    p->p_pid, args->path, args->length);
827 #endif
828 	bsd.path = args->path;
829 	bsd.length = args->length;
830 
831 	return truncate(p, &bsd);
832 }
833 
834