linux_file.c revision 34941
162587Sitojun/*-
278064Sume * Copyright (c) 1994-1995 S�ren Schmidt
362587Sitojun * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer
1053541Sshin *    in this position and unchanged.
1153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1253541Sshin *    notice, this list of conditions and the following disclaimer in the
1353541Sshin *    documentation and/or other materials provided with the distribution.
1453541Sshin * 3. The name of the author may not be used to endorse or promote products
1553541Sshin *    derived from this software withough specific prior written permission
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1853541Sshin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1953541Sshin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2053541Sshin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2153541Sshin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2253541Sshin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2353541Sshin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2453541Sshin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2553541Sshin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2653541Sshin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2753541Sshin *
2853541Sshin *  $Id: linux_file.c,v 1.19 1998/01/05 01:17:42 jmb Exp $
2953541Sshin */
3053541Sshin
3153541Sshin#include "opt_compat.h"
3253541Sshin
3353541Sshin#include <sys/param.h>
3453541Sshin#include <sys/systm.h>
3553541Sshin#include <sys/sysproto.h>
3653541Sshin#include <sys/fcntl.h>
3753541Sshin#include <sys/file.h>
3853541Sshin#include <sys/filedesc.h>
3953541Sshin#include <sys/lock.h>
4053541Sshin#include <sys/proc.h>
4153541Sshin#include <sys/vnode.h>
4253541Sshin#include <sys/malloc.h>
4353541Sshin#include <sys/dirent.h>
4453541Sshin#include <sys/conf.h>
4553541Sshin#include <sys/tty.h>
4653541Sshin
4753541Sshin#include <i386/linux/linux.h>
4853541Sshin#include <i386/linux/linux_proto.h>
4953541Sshin#include <i386/linux/linux_util.h>
5053541Sshin
5153541Sshinint
5253541Sshinlinux_creat(struct proc *p, struct linux_creat_args *args)
5353541Sshin{
5453541Sshin    struct open_args /* {
5553541Sshin	char *path;
5653541Sshin	int flags;
5753541Sshin	int mode;
5853541Sshin    } */ bsd_open_args;
5953541Sshin    caddr_t sg;
6053541Sshin
6153541Sshin    sg = stackgap_init();
6253541Sshin    CHECKALTCREAT(p, &sg, args->path);
6353541Sshin
6453541Sshin#ifdef DEBUG
6553541Sshin    printf("Linux-emul(%d): creat(%s, %d)\n",
6653541Sshin	   p->p_pid, args->path, args->mode);
6753541Sshin#endif
6853541Sshin    bsd_open_args.path = args->path;
6953541Sshin    bsd_open_args.mode = args->mode;
7053541Sshin    bsd_open_args.flags = O_WRONLY | O_CREAT | O_TRUNC;
7153541Sshin    return open(p, &bsd_open_args);
7253541Sshin}
7353541Sshin
7453541Sshinint
7564118Speterlinux_open(struct proc *p, struct linux_open_args *args)
7664118Speter{
7764118Speter    struct open_args /* {
7864118Speter	char *path;
7964118Speter	int flags;
8053541Sshin	int mode;
8153541Sshin    } */ bsd_open_args;
8253541Sshin    int error;
8353541Sshin    caddr_t sg;
8462587Sitojun
8562587Sitojun    sg = stackgap_init();
8662587Sitojun
8762587Sitojun    if (args->flags & LINUX_O_CREAT)
8862587Sitojun	CHECKALTCREAT(p, &sg, args->path);
8962587Sitojun    else
9078064Sume	CHECKALTEXIST(p, &sg, args->path);
9178064Sume
9262587Sitojun#ifdef DEBUG
9353541Sshin    printf("Linux-emul(%d): open(%s, 0x%x, 0x%x)\n",
9453541Sshin	   p->p_pid, args->path, args->flags, args->mode);
9553541Sshin#endif
9653541Sshin    bsd_open_args.flags = 0;
9778064Sume    if (args->flags & LINUX_O_RDONLY)
9878064Sume	bsd_open_args.flags |= O_RDONLY;
9978064Sume    if (args->flags & LINUX_O_WRONLY)
10078064Sume	bsd_open_args.flags |= O_WRONLY;
10178064Sume    if (args->flags & LINUX_O_RDWR)
10278064Sume	bsd_open_args.flags |= O_RDWR;
10378064Sume    if (args->flags & LINUX_O_NDELAY)
10478064Sume	bsd_open_args.flags |= O_NONBLOCK;
10578064Sume    if (args->flags & LINUX_O_APPEND)
10678064Sume	bsd_open_args.flags |= O_APPEND;
10778064Sume    if (args->flags & LINUX_O_SYNC)
10878064Sume	bsd_open_args.flags |= O_FSYNC;
10978064Sume    if (args->flags & LINUX_O_NONBLOCK)
11078064Sume	bsd_open_args.flags |= O_NONBLOCK;
11153541Sshin    if (args->flags & LINUX_FASYNC)
11253541Sshin	bsd_open_args.flags |= O_ASYNC;
11362587Sitojun    if (args->flags & LINUX_O_CREAT)
11478064Sume	bsd_open_args.flags |= O_CREAT;
11562587Sitojun    if (args->flags & LINUX_O_TRUNC)
11662587Sitojun	bsd_open_args.flags |= O_TRUNC;
11778064Sume    if (args->flags & LINUX_O_EXCL)
11878064Sume	bsd_open_args.flags |= O_EXCL;
11978064Sume    if (args->flags & LINUX_O_NOCTTY)
12078064Sume	bsd_open_args.flags |= O_NOCTTY;
12178064Sume    bsd_open_args.path = args->path;
12253541Sshin    bsd_open_args.mode = args->mode;
12353541Sshin
12453541Sshin    error = open(p, &bsd_open_args);
12566586Sitojun    if (!error && !(bsd_open_args.flags & O_NOCTTY) &&
12653541Sshin	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
12753541Sshin	struct filedesc *fdp = p->p_fd;
12853541Sshin	struct file *fp = fdp->fd_ofiles[p->p_retval[0]];
12953541Sshin
13053541Sshin	if (fp->f_type == DTYPE_VNODE)
13153541Sshin	    (fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t) 0, p);
13253541Sshin    }
13353541Sshin#ifdef DEBUG
13453541Sshin    printf("Linux-emul(%d): open returns error %d\n",
13553541Sshin	   p->p_pid, error);
13653541Sshin#endif
13753541Sshin    return error;
13853541Sshin}
13953541Sshin
14053541Sshinstruct linux_flock {
14153541Sshin    short l_type;
14253541Sshin    short l_whence;
14353541Sshin    linux_off_t l_start;
14453541Sshin    linux_off_t l_len;
14553541Sshin    linux_pid_t l_pid;
14653541Sshin};
14753541Sshin
14853541Sshinstatic void
14953541Sshinlinux_to_bsd_flock(struct linux_flock *linux_flock, struct flock *bsd_flock)
15053541Sshin{
15153541Sshin    switch (linux_flock->l_type) {
15253541Sshin    case LINUX_F_RDLCK:
15353541Sshin	bsd_flock->l_type = F_RDLCK;
15453541Sshin	break;
15553541Sshin    case LINUX_F_WRLCK:
15664118Speter	bsd_flock->l_type = F_WRLCK;
15753541Sshin	break;
15853541Sshin    case LINUX_F_UNLCK:
15953541Sshin	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
168static void
169bsd_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
188int
189linux_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
208    sg = stackgap_init();
209    bsd_flock = (struct flock *)stackgap_alloc(&sg, sizeof(struct flock));
210    d_tty = NULL;
211
212#ifdef DEBUG
213    printf("Linux-emul(%d): fcntl(%d, %08x, *)\n",
214	   p->p_pid, args->fd, args->cmd);
215#endif
216    fcntl_args.fd = args->fd;
217    fcntl_args.arg = 0;
218
219    switch (args->cmd) {
220    case LINUX_F_DUPFD:
221	fcntl_args.cmd = F_DUPFD;
222	return fcntl(p, &fcntl_args);
223
224    case LINUX_F_GETFD:
225	fcntl_args.cmd = F_GETFD;
226	return fcntl(p, &fcntl_args);
227
228    case LINUX_F_SETFD:
229	fcntl_args.cmd = F_SETFD;
230	return fcntl(p, &fcntl_args);
231
232    case LINUX_F_GETFL:
233	fcntl_args.cmd = F_GETFL;
234	error = fcntl(p, &fcntl_args);
235	result = p->p_retval[0];
236	p->p_retval[0] = 0;
237	if (result & O_RDONLY) p->p_retval[0] |= LINUX_O_RDONLY;
238	if (result & O_WRONLY) p->p_retval[0] |= LINUX_O_WRONLY;
239	if (result & O_RDWR) p->p_retval[0] |= LINUX_O_RDWR;
240	if (result & O_NDELAY) p->p_retval[0] |= LINUX_O_NONBLOCK;
241	if (result & O_APPEND) p->p_retval[0] |= LINUX_O_APPEND;
242	if (result & O_FSYNC) p->p_retval[0] |= LINUX_O_SYNC;
243	return error;
244
245    case LINUX_F_SETFL:
246	if (args->arg & LINUX_O_NDELAY) fcntl_args.arg |= O_NONBLOCK;
247	if (args->arg & LINUX_O_APPEND) fcntl_args.arg |= O_APPEND;
248	if (args->arg & LINUX_O_SYNC) fcntl_args.arg |= O_FSYNC;
249	fcntl_args.cmd = F_SETFL;
250	return fcntl(p, &fcntl_args);
251
252    case LINUX_F_GETLK:
253	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
254		   	    sizeof(struct linux_flock))))
255	    return error;
256	linux_to_bsd_flock(&linux_flock, bsd_flock);
257	fcntl_args.cmd = F_GETLK;
258	fcntl_args.arg = (int)bsd_flock;
259	if (error = fcntl(p, &fcntl_args))
260	    return error;
261	bsd_to_linux_flock(bsd_flock, &linux_flock);
262	return copyout((caddr_t)&linux_flock, (caddr_t)args->arg,
263		       sizeof(struct linux_flock));
264
265    case LINUX_F_SETLK:
266	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
267		   	    sizeof(struct linux_flock))))
268	    return error;
269	linux_to_bsd_flock(&linux_flock, bsd_flock);
270	fcntl_args.cmd = F_SETLK;
271	fcntl_args.arg = (int)bsd_flock;
272	return fcntl(p, &fcntl_args);
273
274    case LINUX_F_SETLKW:
275	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
276		   	    sizeof(struct linux_flock))))
277	    return error;
278	linux_to_bsd_flock(&linux_flock, bsd_flock);
279	fcntl_args.cmd = F_SETLKW;
280	fcntl_args.arg = (int)bsd_flock;
281	return fcntl(p, &fcntl_args);
282
283    case LINUX_F_SETOWN:
284    case LINUX_F_GETOWN:
285	/*
286	 * We need to route around the normal fcntl() for these calls,
287	 * since it uses TIOC{G,S}PGRP, which is too restrictive for
288	 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
289	 * does not exist.
290	 */
291	fdp = p->p_fd;
292	if ((u_int)args->fd >= fdp->fd_nfiles ||
293		(fp = fdp->fd_ofiles[args->fd]) == NULL)
294	    return EBADF;
295	if (fp->f_type == DTYPE_SOCKET) {
296	    fcntl_args.cmd = args->cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
297	    return fcntl(p, &fcntl_args);
298	}
299	vp = (struct vnode *)fp->f_data;
300	if (vp->v_type != VCHR)
301	    return EINVAL;
302	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
303	    return error;
304
305	d_tty = cdevsw[major(va.va_rdev)]->d_devtotty;
306	if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
307	    return EINVAL;
308	if (args->cmd == LINUX_F_GETOWN) {
309	    p->p_retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
310	    return 0;
311	}
312	if ((long)args->arg <= 0) {
313	    pgid = -(long)args->arg;
314	} else {
315	    struct proc *p1 = pfind((long)args->arg);
316	    if (p1 == 0)
317		return (ESRCH);
318	    pgid = (long)p1->p_pgrp->pg_id;
319	}
320	pgrp = pgfind(pgid);
321	if (pgrp == NULL || pgrp->pg_session != p->p_session)
322	    return EPERM;
323	tp->t_pgrp = pgrp;
324	return 0;
325    }
326    return EINVAL;
327}
328
329int
330linux_lseek(struct proc *p, struct linux_lseek_args *args)
331{
332
333    struct lseek_args /* {
334	int fd;
335	int pad;
336	off_t offset;
337	int whence;
338    } */ tmp_args;
339    int error;
340
341#ifdef DEBUG
342    printf("Linux-emul(%d): lseek(%d, %d, %d)\n",
343	   p->p_pid, args->fdes, args->off, args->whence);
344#endif
345    tmp_args.fd = args->fdes;
346    tmp_args.offset = (off_t)args->off;
347    tmp_args.whence = args->whence;
348    error = lseek(p, &tmp_args);
349    return error;
350}
351
352int
353linux_llseek(struct proc *p, struct linux_llseek_args *args)
354{
355	struct lseek_args bsd_args;
356	int error;
357	off_t off;
358
359#ifdef DEBUG
360        printf("Linux-emul(%d): llseek(%d, %d:%d, %d)\n",
361	   p->p_pid, args->fd, args->ohigh, args->olow, args->whence);
362#endif
363	off = (args->olow) | (((off_t) args->ohigh) << 32);
364
365	bsd_args.fd = args->fd;
366	bsd_args.offset = off;
367	bsd_args.whence = args->whence;
368
369	if ((error = lseek(p, &bsd_args)))
370		return error;
371
372	if ((error = copyout(p->p_retval, (caddr_t)args->res, sizeof (off_t))))
373		return error;
374
375	p->p_retval[0] = 0;
376	return 0;
377}
378
379
380struct linux_dirent {
381    long dino;
382    linux_off_t doff;
383    unsigned short dreclen;
384    char dname[LINUX_NAME_MAX + 1];
385};
386
387#define LINUX_RECLEN(de,namlen) \
388    ALIGN((((char *)&(de)->dname - (char *)de) + (namlen) + 1))
389
390int
391linux_readdir(struct proc *p, struct linux_readdir_args *args)
392{
393	struct linux_getdents_args lda;
394
395	lda.fd = args->fd;
396	lda.dent = args->dent;
397	lda.count = 1;
398	return linux_getdents(p, &lda);
399}
400
401int
402linux_getdents(struct proc *p, struct linux_getdents_args *args)
403{
404    register struct dirent *bdp;
405    struct vnode *vp;
406    caddr_t inp, buf;		/* BSD-format */
407    int len, reclen;		/* BSD-format */
408    caddr_t outp;		/* Linux-format */
409    int resid, linuxreclen=0;	/* Linux-format */
410    struct file *fp;
411    struct uio auio;
412    struct iovec aiov;
413    struct vattr va;
414    off_t off;
415    struct linux_dirent linux_dirent;
416    int buflen, error, eofflag, nbytes, justone;
417    u_long *cookies = NULL, *cookiep;
418    int ncookies;
419
420#ifdef DEBUG
421    printf("Linux-emul(%d): getdents(%d, *, %d)\n",
422	   p->p_pid, args->fd, args->count);
423#endif
424    if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
425	return (error);
426    }
427
428    if ((fp->f_flag & FREAD) == 0)
429	return (EBADF);
430
431    vp = (struct vnode *) fp->f_data;
432
433    if (vp->v_type != VDIR)
434	return (EINVAL);
435
436    if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
437	return error;
438    }
439
440    nbytes = args->count;
441    if (nbytes == 1) {
442	nbytes = sizeof (struct linux_dirent);
443	justone = 1;
444    }
445    else
446	justone = 0;
447
448    off = fp->f_offset;
449#define	DIRBLKSIZ	512		/* XXX we used to use ufs's DIRBLKSIZ */
450    buflen = max(DIRBLKSIZ, nbytes);
451    buflen = min(buflen, MAXBSIZE);
452    buf = malloc(buflen, M_TEMP, M_WAITOK);
453    vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
454again:
455    aiov.iov_base = buf;
456    aiov.iov_len = buflen;
457    auio.uio_iov = &aiov;
458    auio.uio_iovcnt = 1;
459    auio.uio_rw = UIO_READ;
460    auio.uio_segflg = UIO_SYSSPACE;
461    auio.uio_procp = p;
462    auio.uio_resid = buflen;
463    auio.uio_offset = off;
464
465    if (cookies) {
466	free(cookies, M_TEMP);
467	cookies = NULL;
468    }
469
470    error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
471    if (error) {
472	goto out;
473    }
474
475    inp = buf;
476    outp = (caddr_t) args->dent;
477    resid = nbytes;
478    if ((len = buflen - auio.uio_resid) <= 0) {
479	goto eof;
480    }
481
482    cookiep = cookies;
483
484    if (cookies) {
485	/*
486	 * When using cookies, the vfs has the option of reading from
487	 * a different offset than that supplied (UFS truncates the
488	 * offset to a block boundary to make sure that it never reads
489	 * partway through a directory entry, even if the directory
490	 * has been compacted).
491	 */
492	while (len > 0 && ncookies > 0 && *cookiep <= off) {
493	    bdp = (struct dirent *) inp;
494	    len -= bdp->d_reclen;
495	    inp += bdp->d_reclen;
496	    cookiep++;
497	    ncookies--;
498	}
499    }
500
501    while (len > 0) {
502	if (cookiep && ncookies == 0)
503	    break;
504	bdp = (struct dirent *) inp;
505	reclen = bdp->d_reclen;
506	if (reclen & 3) {
507	    printf("linux_readdir: reclen=%d\n", reclen);
508	    error = EFAULT;
509	    goto out;
510	}
511
512	if (bdp->d_fileno == 0) {
513	    inp += reclen;
514	    if (cookiep) {
515		off = *cookiep++;
516		ncookies--;
517	    } else
518		off += reclen;
519	    len -= reclen;
520	    continue;
521	}
522	linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
523	if (reclen > len || resid < linuxreclen) {
524	    outp++;
525	    break;
526	}
527	linux_dirent.dino = (long) bdp->d_fileno;
528	if (justone) {
529	    /*
530	     * old linux-style readdir usage.
531	     */
532	    linux_dirent.doff = (linux_off_t) linuxreclen;
533	    linux_dirent.dreclen = (u_short) bdp->d_namlen;
534	} else {
535	    linux_dirent.doff = (linux_off_t) off;
536	    linux_dirent.dreclen = (u_short) linuxreclen;
537	}
538	strcpy(linux_dirent.dname, bdp->d_name);
539	if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
540	    goto out;
541	}
542	inp += reclen;
543	if (cookiep) {
544	    off = *cookiep++;
545	    ncookies--;
546	} else
547	    off += reclen;
548	outp += linuxreclen;
549	resid -= linuxreclen;
550	len -= reclen;
551	if (justone)
552	    break;
553    }
554
555    if (outp == (caddr_t) args->dent)
556	goto again;
557    fp->f_offset = off;
558
559    if (justone)
560	nbytes = resid + linuxreclen;
561
562eof:
563    p->p_retval[0] = nbytes - resid;
564out:
565    if (cookies)
566	free(cookies, M_TEMP);
567    VOP_UNLOCK(vp, 0, p);
568    free(buf, M_TEMP);
569    return error;
570}
571
572/*
573 * These exist mainly for hooks for doing /compat/linux translation.
574 */
575
576int
577linux_access(struct proc *p, struct linux_access_args *args)
578{
579	struct access_args bsd;
580	caddr_t sg;
581
582	sg = stackgap_init();
583	CHECKALTEXIST(p, &sg, args->path);
584
585#ifdef DEBUG
586        printf("Linux-emul(%d): access(%s, %d)\n",
587	    p->p_pid, args->path, args->flags);
588#endif
589	bsd.path = args->path;
590	bsd.flags = args->flags;
591
592	return access(p, &bsd);
593}
594
595int
596linux_unlink(struct proc *p, struct linux_unlink_args *args)
597{
598	struct unlink_args bsd;
599	caddr_t sg;
600
601	sg = stackgap_init();
602	CHECKALTEXIST(p, &sg, args->path);
603
604#ifdef DEBUG
605	printf("Linux-emul(%d): unlink(%s)\n",
606	   p->p_pid, args->path);
607#endif
608	bsd.path = args->path;
609
610	return unlink(p, &bsd);
611}
612
613int
614linux_chdir(struct proc *p, struct linux_chdir_args *args)
615{
616	struct chdir_args bsd;
617	caddr_t sg;
618
619	sg = stackgap_init();
620	CHECKALTEXIST(p, &sg, args->path);
621
622#ifdef DEBUG
623	printf("Linux-emul(%d): chdir(%s)\n",
624	   p->p_pid, args->path);
625#endif
626	bsd.path = args->path;
627
628	return chdir(p, &bsd);
629}
630
631int
632linux_chmod(struct proc *p, struct linux_chmod_args *args)
633{
634	struct chmod_args bsd;
635	caddr_t sg;
636
637	sg = stackgap_init();
638	CHECKALTEXIST(p, &sg, args->path);
639
640#ifdef DEBUG
641        printf("Linux-emul(%d): chmod(%s, %d)\n",
642	    p->p_pid, args->path, args->mode);
643#endif
644	bsd.path = args->path;
645	bsd.mode = args->mode;
646
647	return chmod(p, &bsd);
648}
649
650int
651linux_chown(struct proc *p, struct linux_chown_args *args)
652{
653	struct chown_args bsd;
654	caddr_t sg;
655
656	sg = stackgap_init();
657	CHECKALTEXIST(p, &sg, args->path);
658
659#ifdef DEBUG
660        printf("Linux-emul(%d): chown(%s, %d, %d)\n",
661	    p->p_pid, args->path, args->uid, args->gid);
662#endif
663	bsd.path = args->path;
664	/* XXX size casts here */
665	bsd.uid = args->uid;
666	bsd.gid = args->gid;
667
668	return chown(p, &bsd);
669}
670
671int
672linux_lchown(struct proc *p, struct linux_lchown_args *args)
673{
674	struct lchown_args bsd;
675	caddr_t sg;
676
677	sg = stackgap_init();
678	CHECKALTEXIST(p, &sg, args->path);
679
680#ifdef DEBUG
681        printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
682	    p->p_pid, args->path, args->uid, args->gid);
683#endif
684	bsd.path = args->path;
685	/* XXX size casts here */
686	bsd.uid = args->uid;
687	bsd.gid = args->gid;
688
689	return lchown(p, &bsd);
690}
691
692int
693linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
694{
695	struct mkdir_args bsd;
696	caddr_t sg;
697
698	sg = stackgap_init();
699	CHECKALTCREAT(p, &sg, args->path);
700
701#ifdef DEBUG
702        printf("Linux-emul(%d): mkdir(%s, %d)\n",
703	    p->p_pid, args->path, args->mode);
704#endif
705	bsd.path = args->path;
706	bsd.mode = args->mode;
707
708	return mkdir(p, &bsd);
709}
710
711int
712linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
713{
714	struct rmdir_args bsd;
715	caddr_t sg;
716
717	sg = stackgap_init();
718	CHECKALTEXIST(p, &sg, args->path);
719
720#ifdef DEBUG
721        printf("Linux-emul(%d): rmdir(%s)\n",
722	    p->p_pid, args->path);
723#endif
724	bsd.path = args->path;
725
726	return rmdir(p, &bsd);
727}
728
729int
730linux_rename(struct proc *p, struct linux_rename_args *args)
731{
732	struct rename_args bsd;
733	caddr_t sg;
734
735	sg = stackgap_init();
736	CHECKALTEXIST(p, &sg, args->from);
737	CHECKALTCREAT(p, &sg, args->to);
738
739#ifdef DEBUG
740        printf("Linux-emul(%d): rename(%s, %s)\n",
741	    p->p_pid, args->from, args->to);
742#endif
743	bsd.from = args->from;
744	bsd.to = args->to;
745
746	return rename(p, &bsd);
747}
748
749int
750linux_symlink(struct proc *p, struct linux_symlink_args *args)
751{
752	struct symlink_args bsd;
753	caddr_t sg;
754
755	sg = stackgap_init();
756	CHECKALTEXIST(p, &sg, args->path);
757	CHECKALTCREAT(p, &sg, args->to);
758
759#ifdef DEBUG
760        printf("Linux-emul(%d): symlink(%s, %s)\n",
761	    p->p_pid, args->path, args->to);
762#endif
763	bsd.path = args->path;
764	bsd.link = args->to;
765
766	return symlink(p, &bsd);
767}
768
769int
770linux_execve(struct proc *p, struct linux_execve_args *args)
771{
772	struct execve_args bsd;
773	caddr_t sg;
774
775	sg = stackgap_init();
776	CHECKALTEXIST(p, &sg, args->path);
777
778#ifdef DEBUG
779        printf("Linux-emul(%d): execve(%s)\n",
780	    p->p_pid, args->path);
781#endif
782	bsd.fname = args->path;
783	bsd.argv = args->argp;
784	bsd.envv = args->envp;
785
786	return execve(p, &bsd);
787}
788
789int
790linux_readlink(struct proc *p, struct linux_readlink_args *args)
791{
792	struct readlink_args bsd;
793	caddr_t sg;
794
795	sg = stackgap_init();
796	CHECKALTEXIST(p, &sg, args->name);
797
798#ifdef DEBUG
799        printf("Linux-emul(%d): readlink(%s, 0x%x, %d)\n",
800	    p->p_pid, args->name, args->buf, args->count);
801#endif
802	bsd.path = args->name;
803	bsd.buf = args->buf;
804	bsd.count = args->count;
805
806	return readlink(p, &bsd);
807}
808
809int
810linux_truncate(struct proc *p, struct linux_truncate_args *args)
811{
812	struct otruncate_args bsd;
813	caddr_t sg;
814
815	sg = stackgap_init();
816	CHECKALTEXIST(p, &sg, args->path);
817
818#ifdef DEBUG
819        printf("Linux-emul(%d): truncate(%s, %ld)\n",
820	    p->p_pid, args->path, args->length);
821#endif
822	bsd.path = args->path;
823	bsd.length = args->length;
824
825	return otruncate(p, &bsd);
826}
827
828