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