linux_file.c revision 48885
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.26 1999/05/11 19:54:19 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
51int
52linux_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
74int
75linux_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
140struct 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
148static void
149linux_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
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    long pgid;
203    struct pgrp *pgrp;
204    struct tty *tp, *(*d_tty) __P((dev_t));
205    caddr_t sg;
206    dev_t dev;
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	if (result & O_ASYNC) p->p_retval[0] |= LINUX_FASYNC;
244	return error;
245
246    case LINUX_F_SETFL:
247	if (args->arg & LINUX_O_NDELAY) fcntl_args.arg |= O_NONBLOCK;
248	if (args->arg & LINUX_O_APPEND) fcntl_args.arg |= O_APPEND;
249	if (args->arg & LINUX_O_SYNC) fcntl_args.arg |= O_FSYNC;
250	if (args->arg & LINUX_FASYNC) fcntl_args.arg |= O_ASYNC;
251	fcntl_args.cmd = F_SETFL;
252	return fcntl(p, &fcntl_args);
253
254    case LINUX_F_GETLK:
255	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
256		   	    sizeof(struct linux_flock))))
257	    return error;
258	linux_to_bsd_flock(&linux_flock, bsd_flock);
259	fcntl_args.cmd = F_GETLK;
260	fcntl_args.arg = (int)bsd_flock;
261	error = fcntl(p, &fcntl_args);
262	if (error)
263	    return error;
264	bsd_to_linux_flock(bsd_flock, &linux_flock);
265	return copyout((caddr_t)&linux_flock, (caddr_t)args->arg,
266		       sizeof(struct linux_flock));
267
268    case LINUX_F_SETLK:
269	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
270		   	    sizeof(struct linux_flock))))
271	    return error;
272	linux_to_bsd_flock(&linux_flock, bsd_flock);
273	fcntl_args.cmd = F_SETLK;
274	fcntl_args.arg = (int)bsd_flock;
275	return fcntl(p, &fcntl_args);
276
277    case LINUX_F_SETLKW:
278	if ((error = copyin((caddr_t)args->arg, (caddr_t)&linux_flock,
279		   	    sizeof(struct linux_flock))))
280	    return error;
281	linux_to_bsd_flock(&linux_flock, bsd_flock);
282	fcntl_args.cmd = F_SETLKW;
283	fcntl_args.arg = (int)bsd_flock;
284	return fcntl(p, &fcntl_args);
285
286    case LINUX_F_SETOWN:
287    case LINUX_F_GETOWN:
288	/*
289	 * We need to route around the normal fcntl() for these calls,
290	 * since it uses TIOC{G,S}PGRP, which is too restrictive for
291	 * Linux F_{G,S}ETOWN semantics. For sockets, this problem
292	 * does not exist.
293	 */
294	fdp = p->p_fd;
295	if ((u_int)args->fd >= fdp->fd_nfiles ||
296		(fp = fdp->fd_ofiles[args->fd]) == NULL)
297	    return EBADF;
298	if (fp->f_type == DTYPE_SOCKET) {
299	    fcntl_args.cmd = args->cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
300    	    fcntl_args.arg = args->arg;
301	    return fcntl(p, &fcntl_args);
302	}
303	vp = (struct vnode *)fp->f_data;
304	dev = vn_todev(vp);
305	if (vp->v_type != VCHR || dev == NODEV)
306	    return EINVAL;
307	d_tty = devsw(dev)->d_devtotty;
308	if (!d_tty || (!(tp = (*d_tty)(dev))))
309	    return EINVAL;
310	if (args->cmd == LINUX_F_GETOWN) {
311	    p->p_retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
312	    return 0;
313	}
314	if ((long)args->arg <= 0) {
315	    pgid = -(long)args->arg;
316	} else {
317	    struct proc *p1 = pfind((long)args->arg);
318	    if (p1 == 0)
319		return (ESRCH);
320	    pgid = (long)p1->p_pgrp->pg_id;
321	}
322	pgrp = pgfind(pgid);
323	if (pgrp == NULL || pgrp->pg_session != p->p_session)
324	    return EPERM;
325	tp->t_pgrp = pgrp;
326	return 0;
327    }
328    return EINVAL;
329}
330
331int
332linux_lseek(struct proc *p, struct linux_lseek_args *args)
333{
334
335    struct lseek_args /* {
336	int fd;
337	int pad;
338	off_t offset;
339	int whence;
340    } */ tmp_args;
341    int error;
342
343#ifdef DEBUG
344    printf("Linux-emul(%ld): lseek(%d, %ld, %d)\n",
345	   (long)p->p_pid, args->fdes, args->off, args->whence);
346#endif
347    tmp_args.fd = args->fdes;
348    tmp_args.offset = (off_t)args->off;
349    tmp_args.whence = args->whence;
350    error = lseek(p, &tmp_args);
351    return error;
352}
353
354int
355linux_llseek(struct proc *p, struct linux_llseek_args *args)
356{
357	struct lseek_args bsd_args;
358	int error;
359	off_t off;
360
361#ifdef DEBUG
362        printf("Linux-emul(%d): llseek(%d, %d:%d, %d)\n",
363	   p->p_pid, args->fd, args->ohigh, args->olow, args->whence);
364#endif
365	off = (args->olow) | (((off_t) args->ohigh) << 32);
366
367	bsd_args.fd = args->fd;
368	bsd_args.offset = off;
369	bsd_args.whence = args->whence;
370
371	if ((error = lseek(p, &bsd_args)))
372		return error;
373
374	if ((error = copyout(p->p_retval, (caddr_t)args->res, sizeof (off_t))))
375		return error;
376
377	p->p_retval[0] = 0;
378	return 0;
379}
380
381
382struct linux_dirent {
383    long dino;
384    linux_off_t doff;
385    unsigned short dreclen;
386    char dname[LINUX_NAME_MAX + 1];
387};
388
389#define LINUX_RECLEN(de,namlen) \
390    ALIGN((((char *)&(de)->dname - (char *)de) + (namlen) + 1))
391
392int
393linux_readdir(struct proc *p, struct linux_readdir_args *args)
394{
395	struct linux_getdents_args lda;
396
397	lda.fd = args->fd;
398	lda.dent = args->dent;
399	lda.count = 1;
400	return linux_getdents(p, &lda);
401}
402
403int
404linux_getdents(struct proc *p, struct linux_getdents_args *args)
405{
406    register struct dirent *bdp;
407    struct vnode *vp;
408    caddr_t inp, buf;		/* BSD-format */
409    int len, reclen;		/* BSD-format */
410    caddr_t outp;		/* Linux-format */
411    int resid, linuxreclen=0;	/* Linux-format */
412    struct file *fp;
413    struct uio auio;
414    struct iovec aiov;
415    struct vattr va;
416    off_t off;
417    struct linux_dirent linux_dirent;
418    int buflen, error, eofflag, nbytes, justone;
419    u_long *cookies = NULL, *cookiep;
420    int ncookies;
421
422#ifdef DEBUG
423    printf("Linux-emul(%d): getdents(%d, *, %d)\n",
424	   p->p_pid, args->fd, args->count);
425#endif
426    if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
427	return (error);
428    }
429
430    if ((fp->f_flag & FREAD) == 0)
431	return (EBADF);
432
433    vp = (struct vnode *) fp->f_data;
434
435    if (vp->v_type != VDIR)
436	return (EINVAL);
437
438    if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
439	return error;
440    }
441
442    nbytes = args->count;
443    if (nbytes == 1) {
444	nbytes = sizeof (struct linux_dirent);
445	justone = 1;
446    }
447    else
448	justone = 0;
449
450    off = fp->f_offset;
451#define	DIRBLKSIZ	512		/* XXX we used to use ufs's DIRBLKSIZ */
452    buflen = max(DIRBLKSIZ, nbytes);
453    buflen = min(buflen, MAXBSIZE);
454    buf = malloc(buflen, M_TEMP, M_WAITOK);
455    vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
456again:
457    aiov.iov_base = buf;
458    aiov.iov_len = buflen;
459    auio.uio_iov = &aiov;
460    auio.uio_iovcnt = 1;
461    auio.uio_rw = UIO_READ;
462    auio.uio_segflg = UIO_SYSSPACE;
463    auio.uio_procp = p;
464    auio.uio_resid = buflen;
465    auio.uio_offset = off;
466
467    if (cookies) {
468	free(cookies, M_TEMP);
469	cookies = NULL;
470    }
471
472    error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
473    if (error) {
474	goto out;
475    }
476
477    inp = buf;
478    outp = (caddr_t) args->dent;
479    resid = nbytes;
480    if ((len = buflen - auio.uio_resid) <= 0) {
481	goto eof;
482    }
483
484    cookiep = cookies;
485
486    if (cookies) {
487	/*
488	 * When using cookies, the vfs has the option of reading from
489	 * a different offset than that supplied (UFS truncates the
490	 * offset to a block boundary to make sure that it never reads
491	 * partway through a directory entry, even if the directory
492	 * has been compacted).
493	 */
494	while (len > 0 && ncookies > 0 && *cookiep <= off) {
495	    bdp = (struct dirent *) inp;
496	    len -= bdp->d_reclen;
497	    inp += bdp->d_reclen;
498	    cookiep++;
499	    ncookies--;
500	}
501    }
502
503    while (len > 0) {
504	if (cookiep && ncookies == 0)
505	    break;
506	bdp = (struct dirent *) inp;
507	reclen = bdp->d_reclen;
508	if (reclen & 3) {
509	    printf("linux_readdir: reclen=%d\n", reclen);
510	    error = EFAULT;
511	    goto out;
512	}
513
514	if (bdp->d_fileno == 0) {
515	    inp += reclen;
516	    if (cookiep) {
517		off = *cookiep++;
518		ncookies--;
519	    } else
520		off += reclen;
521	    len -= reclen;
522	    continue;
523	}
524	linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
525	if (reclen > len || resid < linuxreclen) {
526	    outp++;
527	    break;
528	}
529	linux_dirent.dino = (long) bdp->d_fileno;
530	if (justone) {
531	    /*
532	     * old linux-style readdir usage.
533	     */
534	    linux_dirent.doff = (linux_off_t) linuxreclen;
535	    linux_dirent.dreclen = (u_short) bdp->d_namlen;
536	} else {
537	    linux_dirent.doff = (linux_off_t) off;
538	    linux_dirent.dreclen = (u_short) linuxreclen;
539	}
540	strcpy(linux_dirent.dname, bdp->d_name);
541	if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
542	    goto out;
543	}
544	inp += reclen;
545	if (cookiep) {
546	    off = *cookiep++;
547	    ncookies--;
548	} else
549	    off += reclen;
550	outp += linuxreclen;
551	resid -= linuxreclen;
552	len -= reclen;
553	if (justone)
554	    break;
555    }
556
557    if (outp == (caddr_t) args->dent)
558	goto again;
559    fp->f_offset = off;
560
561    if (justone)
562	nbytes = resid + linuxreclen;
563
564eof:
565    p->p_retval[0] = nbytes - resid;
566out:
567    if (cookies)
568	free(cookies, M_TEMP);
569    VOP_UNLOCK(vp, 0, p);
570    free(buf, M_TEMP);
571    return error;
572}
573
574/*
575 * These exist mainly for hooks for doing /compat/linux translation.
576 */
577
578int
579linux_access(struct proc *p, struct linux_access_args *args)
580{
581	struct access_args bsd;
582	caddr_t sg;
583
584	sg = stackgap_init();
585	CHECKALTEXIST(p, &sg, args->path);
586
587#ifdef DEBUG
588        printf("Linux-emul(%d): access(%s, %d)\n",
589	    p->p_pid, args->path, args->flags);
590#endif
591	bsd.path = args->path;
592	bsd.flags = args->flags;
593
594	return access(p, &bsd);
595}
596
597int
598linux_unlink(struct proc *p, struct linux_unlink_args *args)
599{
600	struct unlink_args bsd;
601	caddr_t sg;
602
603	sg = stackgap_init();
604	CHECKALTEXIST(p, &sg, args->path);
605
606#ifdef DEBUG
607	printf("Linux-emul(%d): unlink(%s)\n",
608	   p->p_pid, args->path);
609#endif
610	bsd.path = args->path;
611
612	return unlink(p, &bsd);
613}
614
615int
616linux_chdir(struct proc *p, struct linux_chdir_args *args)
617{
618	struct chdir_args bsd;
619	caddr_t sg;
620
621	sg = stackgap_init();
622	CHECKALTEXIST(p, &sg, args->path);
623
624#ifdef DEBUG
625	printf("Linux-emul(%d): chdir(%s)\n",
626	   p->p_pid, args->path);
627#endif
628	bsd.path = args->path;
629
630	return chdir(p, &bsd);
631}
632
633int
634linux_chmod(struct proc *p, struct linux_chmod_args *args)
635{
636	struct chmod_args bsd;
637	caddr_t sg;
638
639	sg = stackgap_init();
640	CHECKALTEXIST(p, &sg, args->path);
641
642#ifdef DEBUG
643        printf("Linux-emul(%d): chmod(%s, %d)\n",
644	    p->p_pid, args->path, args->mode);
645#endif
646	bsd.path = args->path;
647	bsd.mode = args->mode;
648
649	return chmod(p, &bsd);
650}
651
652int
653linux_chown(struct proc *p, struct linux_chown_args *args)
654{
655	struct chown_args bsd;
656	caddr_t sg;
657
658	sg = stackgap_init();
659	CHECKALTEXIST(p, &sg, args->path);
660
661#ifdef DEBUG
662        printf("Linux-emul(%d): chown(%s, %d, %d)\n",
663	    p->p_pid, args->path, args->uid, args->gid);
664#endif
665	bsd.path = args->path;
666	/* XXX size casts here */
667	bsd.uid = args->uid;
668	bsd.gid = args->gid;
669
670	return chown(p, &bsd);
671}
672
673int
674linux_lchown(struct proc *p, struct linux_lchown_args *args)
675{
676	struct lchown_args bsd;
677	caddr_t sg;
678
679	sg = stackgap_init();
680	CHECKALTEXIST(p, &sg, args->path);
681
682#ifdef DEBUG
683        printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
684	    p->p_pid, args->path, args->uid, args->gid);
685#endif
686	bsd.path = args->path;
687	/* XXX size casts here */
688	bsd.uid = args->uid;
689	bsd.gid = args->gid;
690
691	return lchown(p, &bsd);
692}
693
694int
695linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
696{
697	struct mkdir_args bsd;
698	caddr_t sg;
699
700	sg = stackgap_init();
701	CHECKALTCREAT(p, &sg, args->path);
702
703#ifdef DEBUG
704        printf("Linux-emul(%d): mkdir(%s, %d)\n",
705	    p->p_pid, args->path, args->mode);
706#endif
707	bsd.path = args->path;
708	bsd.mode = args->mode;
709
710	return mkdir(p, &bsd);
711}
712
713int
714linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
715{
716	struct rmdir_args bsd;
717	caddr_t sg;
718
719	sg = stackgap_init();
720	CHECKALTEXIST(p, &sg, args->path);
721
722#ifdef DEBUG
723        printf("Linux-emul(%d): rmdir(%s)\n",
724	    p->p_pid, args->path);
725#endif
726	bsd.path = args->path;
727
728	return rmdir(p, &bsd);
729}
730
731int
732linux_rename(struct proc *p, struct linux_rename_args *args)
733{
734	struct rename_args bsd;
735	caddr_t sg;
736
737	sg = stackgap_init();
738	CHECKALTEXIST(p, &sg, args->from);
739	CHECKALTCREAT(p, &sg, args->to);
740
741#ifdef DEBUG
742        printf("Linux-emul(%d): rename(%s, %s)\n",
743	    p->p_pid, args->from, args->to);
744#endif
745	bsd.from = args->from;
746	bsd.to = args->to;
747
748	return rename(p, &bsd);
749}
750
751int
752linux_symlink(struct proc *p, struct linux_symlink_args *args)
753{
754	struct symlink_args bsd;
755	caddr_t sg;
756
757	sg = stackgap_init();
758	CHECKALTEXIST(p, &sg, args->path);
759	CHECKALTCREAT(p, &sg, args->to);
760
761#ifdef DEBUG
762        printf("Linux-emul(%d): symlink(%s, %s)\n",
763	    p->p_pid, args->path, args->to);
764#endif
765	bsd.path = args->path;
766	bsd.link = args->to;
767
768	return symlink(p, &bsd);
769}
770
771int
772linux_execve(struct proc *p, struct linux_execve_args *args)
773{
774	struct execve_args bsd;
775	caddr_t sg;
776
777	sg = stackgap_init();
778	CHECKALTEXIST(p, &sg, args->path);
779
780#ifdef DEBUG
781        printf("Linux-emul(%d): execve(%s)\n",
782	    p->p_pid, args->path);
783#endif
784	bsd.fname = args->path;
785	bsd.argv = args->argp;
786	bsd.envv = args->envp;
787
788	return execve(p, &bsd);
789}
790
791int
792linux_readlink(struct proc *p, struct linux_readlink_args *args)
793{
794	struct readlink_args bsd;
795	caddr_t sg;
796
797	sg = stackgap_init();
798	CHECKALTEXIST(p, &sg, args->name);
799
800#ifdef DEBUG
801        printf("Linux-emul(%ld): readlink(%s, %p, %d)\n",
802	    (long)p->p_pid, args->name, (void *)args->buf, args->count);
803#endif
804	bsd.path = args->name;
805	bsd.buf = args->buf;
806	bsd.count = args->count;
807
808	return readlink(p, &bsd);
809}
810
811int
812linux_truncate(struct proc *p, struct linux_truncate_args *args)
813{
814	struct truncate_args bsd;
815	caddr_t sg;
816
817	sg = stackgap_init();
818	CHECKALTEXIST(p, &sg, args->path);
819
820#ifdef DEBUG
821        printf("Linux-emul(%d): truncate(%s, %ld)\n",
822	    p->p_pid, args->path, args->length);
823#endif
824	bsd.path = args->path;
825	bsd.length = args->length;
826
827	return truncate(p, &bsd);
828}
829
830