xref: /freebsd/sys/compat/linux/linux_file.c (revision 64ca32560de33b7d9e93ef9a226362c7dd105101)
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$
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 <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
49 #include <compat/linux/linux_util.h>
50 
51 #ifndef __alpha__
52 int
53 linux_creat(struct proc *p, struct linux_creat_args *args)
54 {
55     struct open_args /* {
56 	char *path;
57 	int flags;
58 	int mode;
59     } */ bsd_open_args;
60     caddr_t sg;
61 
62     sg = stackgap_init();
63     CHECKALTCREAT(p, &sg, args->path);
64 
65 #ifdef DEBUG
66     printf("Linux-emul(%d): creat(%s, %d)\n",
67 	   p->p_pid, args->path, args->mode);
68 #endif
69     bsd_open_args.path = args->path;
70     bsd_open_args.mode = args->mode;
71     bsd_open_args.flags = O_WRONLY | O_CREAT | O_TRUNC;
72     return open(p, &bsd_open_args);
73 }
74 #endif /*!__alpha__*/
75 
76 int
77 linux_open(struct proc *p, struct linux_open_args *args)
78 {
79     struct open_args /* {
80 	char *path;
81 	int flags;
82 	int mode;
83     } */ bsd_open_args;
84     int error;
85     caddr_t sg;
86 
87     sg = stackgap_init();
88 
89     if (args->flags & LINUX_O_CREAT)
90 	CHECKALTCREAT(p, &sg, args->path);
91     else
92 	CHECKALTEXIST(p, &sg, args->path);
93 
94 #ifdef DEBUG
95     printf("Linux-emul(%d): open(%s, 0x%x, 0x%x)\n",
96 	   p->p_pid, args->path, args->flags, args->mode);
97 #endif
98     bsd_open_args.flags = 0;
99     if (args->flags & LINUX_O_RDONLY)
100 	bsd_open_args.flags |= O_RDONLY;
101     if (args->flags & LINUX_O_WRONLY)
102 	bsd_open_args.flags |= O_WRONLY;
103     if (args->flags & LINUX_O_RDWR)
104 	bsd_open_args.flags |= O_RDWR;
105     if (args->flags & LINUX_O_NDELAY)
106 	bsd_open_args.flags |= O_NONBLOCK;
107     if (args->flags & LINUX_O_APPEND)
108 	bsd_open_args.flags |= O_APPEND;
109     if (args->flags & LINUX_O_SYNC)
110 	bsd_open_args.flags |= O_FSYNC;
111     if (args->flags & LINUX_O_NONBLOCK)
112 	bsd_open_args.flags |= O_NONBLOCK;
113     if (args->flags & LINUX_FASYNC)
114 	bsd_open_args.flags |= O_ASYNC;
115     if (args->flags & LINUX_O_CREAT)
116 	bsd_open_args.flags |= O_CREAT;
117     if (args->flags & LINUX_O_TRUNC)
118 	bsd_open_args.flags |= O_TRUNC;
119     if (args->flags & LINUX_O_EXCL)
120 	bsd_open_args.flags |= O_EXCL;
121     if (args->flags & LINUX_O_NOCTTY)
122 	bsd_open_args.flags |= O_NOCTTY;
123     bsd_open_args.path = args->path;
124     bsd_open_args.mode = args->mode;
125 
126     error = open(p, &bsd_open_args);
127     PROC_LOCK(p);
128     if (!error && !(bsd_open_args.flags & O_NOCTTY) &&
129 	SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
130 	struct filedesc *fdp = p->p_fd;
131 	struct file *fp = fdp->fd_ofiles[p->p_retval[0]];
132 
133 	PROC_UNLOCK(p);
134 	if (fp->f_type == DTYPE_VNODE)
135 	    fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
136     } else
137 	PROC_UNLOCK(p);
138 #ifdef DEBUG
139     printf("Linux-emul(%d): open returns error %d\n",
140 	   p->p_pid, error);
141 #endif
142     return error;
143 }
144 
145 struct linux_flock {
146     short l_type;
147     short l_whence;
148     linux_off_t l_start;
149     linux_off_t l_len;
150     linux_pid_t l_pid;
151 };
152 
153 static void
154 linux_to_bsd_flock(struct linux_flock *linux_flock, struct flock *bsd_flock)
155 {
156     switch (linux_flock->l_type) {
157     case LINUX_F_RDLCK:
158 	bsd_flock->l_type = F_RDLCK;
159 	break;
160     case LINUX_F_WRLCK:
161 	bsd_flock->l_type = F_WRLCK;
162 	break;
163     case LINUX_F_UNLCK:
164 	bsd_flock->l_type = F_UNLCK;
165 	break;
166     default:
167         bsd_flock->l_type = -1;
168         break;
169     }
170     bsd_flock->l_whence = linux_flock->l_whence;
171     bsd_flock->l_start = (off_t)linux_flock->l_start;
172     bsd_flock->l_len = (off_t)linux_flock->l_len;
173     bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
174 }
175 
176 static void
177 bsd_to_linux_flock(struct flock *bsd_flock, struct linux_flock *linux_flock)
178 {
179     switch (bsd_flock->l_type) {
180     case F_RDLCK:
181 	linux_flock->l_type = LINUX_F_RDLCK;
182 	break;
183     case F_WRLCK:
184 	linux_flock->l_type = LINUX_F_WRLCK;
185 	break;
186     case F_UNLCK:
187 	linux_flock->l_type = LINUX_F_UNLCK;
188 	break;
189     }
190     linux_flock->l_whence = bsd_flock->l_whence;
191     linux_flock->l_start = (linux_off_t)bsd_flock->l_start;
192     linux_flock->l_len = (linux_off_t)bsd_flock->l_len;
193     linux_flock->l_pid = (linux_pid_t)bsd_flock->l_pid;
194 }
195 
196 int
197 linux_fcntl(struct proc *p, struct linux_fcntl_args *args)
198 {
199     int error, result;
200     struct fcntl_args /* {
201 	int fd;
202 	int cmd;
203 	long arg;
204     } */ fcntl_args;
205     struct linux_flock linux_flock;
206     struct flock *bsd_flock;
207     struct filedesc *fdp;
208     struct file *fp;
209     caddr_t sg;
210 
211     sg = stackgap_init();
212     bsd_flock = (struct flock *)stackgap_alloc(&sg, sizeof(struct flock));
213 
214 #ifdef DEBUG
215     printf("Linux-emul(%ld): fcntl(%d, %08x, *)\n", (long)p->p_pid,
216 	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 = (long)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 = (long)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 = (long)bsd_flock;
288 	return fcntl(p, &fcntl_args);
289 
290     case LINUX_F_GETOWN:
291 	fcntl_args.cmd = F_GETOWN;
292 	return fcntl(p, &fcntl_args);
293 
294     case LINUX_F_SETOWN:
295 	/*
296 	 * XXX some Linux applications depend on F_SETOWN having no
297 	 * significant effect for pipes (SIGIO is not delivered for
298 	 * pipes under Linux-2.2.35 at least).
299 	 */
300 	fdp = p->p_fd;
301 	if ((u_int)args->fd >= fdp->fd_nfiles ||
302 	  (fp = fdp->fd_ofiles[args->fd]) == NULL)
303 	    return EBADF;
304 	if (fp->f_type == DTYPE_PIPE)
305 	    return EINVAL;
306 
307  	fcntl_args.cmd = F_SETOWN;
308 	fcntl_args.arg = args->arg;
309 	return fcntl(p, &fcntl_args);
310     }
311     return EINVAL;
312 }
313 
314 int
315 linux_lseek(struct proc *p, struct linux_lseek_args *args)
316 {
317 
318     struct lseek_args /* {
319 	int fd;
320 	int pad;
321 	off_t offset;
322 	int whence;
323     } */ tmp_args;
324     int error;
325 
326 #ifdef DEBUG
327     printf("Linux-emul(%ld): lseek(%d, %ld, %d)\n",
328 	   (long)p->p_pid, args->fdes, args->off, args->whence);
329 #endif
330     tmp_args.fd = args->fdes;
331     tmp_args.offset = (off_t)args->off;
332     tmp_args.whence = args->whence;
333     error = lseek(p, &tmp_args);
334     return error;
335 }
336 
337 #ifndef __alpha__
338 int
339 linux_llseek(struct proc *p, struct linux_llseek_args *args)
340 {
341 	struct lseek_args bsd_args;
342 	int error;
343 	off_t off;
344 
345 #ifdef DEBUG
346         printf("Linux-emul(%d): llseek(%d, %d:%d, %d)\n",
347 	   p->p_pid, args->fd, args->ohigh, args->olow, args->whence);
348 #endif
349 	off = (args->olow) | (((off_t) args->ohigh) << 32);
350 
351 	bsd_args.fd = args->fd;
352 	bsd_args.offset = off;
353 	bsd_args.whence = args->whence;
354 
355 	if ((error = lseek(p, &bsd_args)))
356 		return error;
357 
358 	if ((error = copyout(p->p_retval, (caddr_t)args->res, sizeof (off_t))))
359 		return error;
360 
361 	p->p_retval[0] = 0;
362 	return 0;
363 }
364 #endif /*!__alpha__*/
365 
366 
367 struct linux_dirent {
368     long dino;
369     linux_off_t doff;
370     unsigned short dreclen;
371     char dname[LINUX_NAME_MAX + 1];
372 };
373 
374 #define LINUX_RECLEN(de,namlen) \
375     ALIGN((((char *)&(de)->dname - (char *)de) + (namlen) + 1))
376 
377 #ifndef __alpha__
378 int
379 linux_readdir(struct proc *p, struct linux_readdir_args *args)
380 {
381 	struct linux_getdents_args lda;
382 
383 	lda.fd = args->fd;
384 	lda.dent = args->dent;
385 	lda.count = 1;
386 	return linux_getdents(p, &lda);
387 }
388 #endif /*!__alpha__*/
389 
390 int
391 linux_getdents(struct proc *p, struct linux_getdents_args *args)
392 {
393     register struct dirent *bdp;
394     struct vnode *vp;
395     caddr_t inp, buf;		/* BSD-format */
396     int len, reclen;		/* BSD-format */
397     caddr_t outp;		/* Linux-format */
398     int resid, linuxreclen=0;	/* Linux-format */
399     struct file *fp;
400     struct uio auio;
401     struct iovec aiov;
402     struct vattr va;
403     off_t off;
404     struct linux_dirent linux_dirent;
405     int buflen, error, eofflag, nbytes, justone;
406     u_long *cookies = NULL, *cookiep;
407     int ncookies;
408     struct ucred *uc;
409 
410 #ifdef DEBUG
411     printf("Linux-emul(%d): getdents(%d, *, %d)\n",
412 	   p->p_pid, args->fd, args->count);
413 #endif
414     if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
415 	return (error);
416     }
417 
418     if ((fp->f_flag & FREAD) == 0)
419 	return (EBADF);
420 
421     vp = (struct vnode *) fp->f_data;
422 
423     if (vp->v_type != VDIR)
424 	return (EINVAL);
425 
426     PROC_LOCK(p);
427     uc = p->p_ucred;
428     crhold(uc);
429     PROC_UNLOCK(p);
430     error = VOP_GETATTR(vp, &va, uc, p);
431     crfree(uc);
432     if (error) {
433 	return error;
434     }
435 
436     nbytes = args->count;
437     if (nbytes == 1) {
438 	nbytes = sizeof (struct linux_dirent);
439 	justone = 1;
440     }
441     else
442 	justone = 0;
443 
444     off = fp->f_offset;
445 #define	DIRBLKSIZ	512		/* XXX we used to use ufs's DIRBLKSIZ */
446     buflen = max(DIRBLKSIZ, nbytes);
447     buflen = min(buflen, MAXBSIZE);
448     buf = malloc(buflen, M_TEMP, M_WAITOK);
449     vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
450 again:
451     aiov.iov_base = buf;
452     aiov.iov_len = buflen;
453     auio.uio_iov = &aiov;
454     auio.uio_iovcnt = 1;
455     auio.uio_rw = UIO_READ;
456     auio.uio_segflg = UIO_SYSSPACE;
457     auio.uio_procp = p;
458     auio.uio_resid = buflen;
459     auio.uio_offset = off;
460 
461     if (cookies) {
462 	free(cookies, M_TEMP);
463 	cookies = NULL;
464     }
465 
466     error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
467     if (error) {
468 	goto out;
469     }
470 
471     inp = buf;
472     outp = (caddr_t) args->dent;
473     resid = nbytes;
474     if ((len = buflen - auio.uio_resid) <= 0) {
475 	goto eof;
476     }
477 
478     cookiep = cookies;
479 
480     if (cookies) {
481 	/*
482 	 * When using cookies, the vfs has the option of reading from
483 	 * a different offset than that supplied (UFS truncates the
484 	 * offset to a block boundary to make sure that it never reads
485 	 * partway through a directory entry, even if the directory
486 	 * has been compacted).
487 	 */
488 	while (len > 0 && ncookies > 0 && *cookiep <= off) {
489 	    bdp = (struct dirent *) inp;
490 	    len -= bdp->d_reclen;
491 	    inp += bdp->d_reclen;
492 	    cookiep++;
493 	    ncookies--;
494 	}
495     }
496 
497     while (len > 0) {
498 	if (cookiep && ncookies == 0)
499 	    break;
500 	bdp = (struct dirent *) inp;
501 	reclen = bdp->d_reclen;
502 	if (reclen & 3) {
503 	    printf("linux_readdir: reclen=%d\n", reclen);
504 	    error = EFAULT;
505 	    goto out;
506 	}
507 
508 	if (bdp->d_fileno == 0) {
509 	    inp += reclen;
510 	    if (cookiep) {
511 		off = *cookiep++;
512 		ncookies--;
513 	    } else
514 		off += reclen;
515 	    len -= reclen;
516 	    continue;
517 	}
518 	linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
519 	if (reclen > len || resid < linuxreclen) {
520 	    outp++;
521 	    break;
522 	}
523 	linux_dirent.dino = (long) bdp->d_fileno;
524 	if (justone) {
525 	    /*
526 	     * old linux-style readdir usage.
527 	     */
528 	    linux_dirent.doff = (linux_off_t) linuxreclen;
529 	    linux_dirent.dreclen = (u_short) bdp->d_namlen;
530 	} else {
531 	    if (cookiep)
532 		linux_dirent.doff = (linux_off_t)*cookiep;
533 	    else
534 		linux_dirent.doff = (linux_off_t)(off + reclen);
535 	    linux_dirent.dreclen = (u_short) linuxreclen;
536 	}
537 	strcpy(linux_dirent.dname, bdp->d_name);
538 	if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
539 	    goto out;
540 	}
541 	inp += reclen;
542 	if (cookiep) {
543 	    off = *cookiep++;
544 	    ncookies--;
545 	} else
546 	    off += reclen;
547 	outp += linuxreclen;
548 	resid -= linuxreclen;
549 	len -= reclen;
550 	if (justone)
551 	    break;
552     }
553 
554     if (outp == (caddr_t) args->dent)
555 	goto again;
556     fp->f_offset = off;
557 
558     if (justone)
559 	nbytes = resid + linuxreclen;
560 
561 eof:
562     p->p_retval[0] = nbytes - resid;
563 out:
564     if (cookies)
565 	free(cookies, M_TEMP);
566     VOP_UNLOCK(vp, 0, p);
567     free(buf, M_TEMP);
568     return error;
569 }
570 
571 /*
572  * These exist mainly for hooks for doing /compat/linux translation.
573  */
574 
575 int
576 linux_access(struct proc *p, struct linux_access_args *args)
577 {
578 	struct access_args bsd;
579 	caddr_t sg;
580 
581 	sg = stackgap_init();
582 	CHECKALTEXIST(p, &sg, args->path);
583 
584 #ifdef DEBUG
585         printf("Linux-emul(%d): access(%s, %d)\n",
586 	    p->p_pid, args->path, args->flags);
587 #endif
588 	bsd.path = args->path;
589 	bsd.flags = args->flags;
590 
591 	return access(p, &bsd);
592 }
593 
594 int
595 linux_unlink(struct proc *p, struct linux_unlink_args *args)
596 {
597 	struct unlink_args bsd;
598 	caddr_t sg;
599 
600 	sg = stackgap_init();
601 	CHECKALTEXIST(p, &sg, args->path);
602 
603 #ifdef DEBUG
604 	printf("Linux-emul(%d): unlink(%s)\n",
605 	   p->p_pid, args->path);
606 #endif
607 	bsd.path = args->path;
608 
609 	return unlink(p, &bsd);
610 }
611 
612 int
613 linux_chdir(struct proc *p, struct linux_chdir_args *args)
614 {
615 	struct chdir_args bsd;
616 	caddr_t sg;
617 
618 	sg = stackgap_init();
619 	CHECKALTEXIST(p, &sg, args->path);
620 
621 #ifdef DEBUG
622 	printf("Linux-emul(%d): chdir(%s)\n",
623 	   p->p_pid, args->path);
624 #endif
625 	bsd.path = args->path;
626 
627 	return chdir(p, &bsd);
628 }
629 
630 int
631 linux_chmod(struct proc *p, struct linux_chmod_args *args)
632 {
633 	struct chmod_args bsd;
634 	caddr_t sg;
635 
636 	sg = stackgap_init();
637 	CHECKALTEXIST(p, &sg, args->path);
638 
639 #ifdef DEBUG
640         printf("Linux-emul(%d): chmod(%s, %d)\n",
641 	    p->p_pid, args->path, args->mode);
642 #endif
643 	bsd.path = args->path;
644 	bsd.mode = args->mode;
645 
646 	return chmod(p, &bsd);
647 }
648 
649 int
650 linux_chown(struct proc *p, struct linux_chown_args *args)
651 {
652 	struct chown_args bsd;
653 	caddr_t sg;
654 
655 	sg = stackgap_init();
656 	CHECKALTEXIST(p, &sg, args->path);
657 
658 #ifdef DEBUG
659         printf("Linux-emul(%d): chown(%s, %d, %d)\n",
660 	    p->p_pid, args->path, args->uid, args->gid);
661 #endif
662 	bsd.path = args->path;
663 	/* XXX size casts here */
664 	bsd.uid = args->uid;
665 	bsd.gid = args->gid;
666 
667 	return chown(p, &bsd);
668 }
669 
670 int
671 linux_lchown(struct proc *p, struct linux_lchown_args *args)
672 {
673 	struct lchown_args bsd;
674 	caddr_t sg;
675 
676 	sg = stackgap_init();
677 	CHECKALTEXIST(p, &sg, args->path);
678 
679 #ifdef DEBUG
680         printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
681 	    p->p_pid, args->path, args->uid, args->gid);
682 #endif
683 	bsd.path = args->path;
684 	/* XXX size casts here */
685 	bsd.uid = args->uid;
686 	bsd.gid = args->gid;
687 
688 	return lchown(p, &bsd);
689 }
690 
691 int
692 linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
693 {
694 	struct mkdir_args bsd;
695 	caddr_t sg;
696 
697 	sg = stackgap_init();
698 	CHECKALTCREAT(p, &sg, args->path);
699 
700 #ifdef DEBUG
701         printf("Linux-emul(%d): mkdir(%s, %d)\n",
702 	    p->p_pid, args->path, args->mode);
703 #endif
704 	bsd.path = args->path;
705 	bsd.mode = args->mode;
706 
707 	return mkdir(p, &bsd);
708 }
709 
710 int
711 linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
712 {
713 	struct rmdir_args bsd;
714 	caddr_t sg;
715 
716 	sg = stackgap_init();
717 	CHECKALTEXIST(p, &sg, args->path);
718 
719 #ifdef DEBUG
720         printf("Linux-emul(%d): rmdir(%s)\n",
721 	    p->p_pid, args->path);
722 #endif
723 	bsd.path = args->path;
724 
725 	return rmdir(p, &bsd);
726 }
727 
728 int
729 linux_rename(struct proc *p, struct linux_rename_args *args)
730 {
731 	struct rename_args bsd;
732 	caddr_t sg;
733 
734 	sg = stackgap_init();
735 	CHECKALTEXIST(p, &sg, args->from);
736 	CHECKALTCREAT(p, &sg, args->to);
737 
738 #ifdef DEBUG
739         printf("Linux-emul(%d): rename(%s, %s)\n",
740 	    p->p_pid, args->from, args->to);
741 #endif
742 	bsd.from = args->from;
743 	bsd.to = args->to;
744 
745 	return rename(p, &bsd);
746 }
747 
748 int
749 linux_symlink(struct proc *p, struct linux_symlink_args *args)
750 {
751 	struct symlink_args bsd;
752 	caddr_t sg;
753 
754 	sg = stackgap_init();
755 	CHECKALTEXIST(p, &sg, args->path);
756 	CHECKALTCREAT(p, &sg, args->to);
757 
758 #ifdef DEBUG
759         printf("Linux-emul(%d): symlink(%s, %s)\n",
760 	    p->p_pid, args->path, args->to);
761 #endif
762 	bsd.path = args->path;
763 	bsd.link = args->to;
764 
765 	return symlink(p, &bsd);
766 }
767 
768 int
769 linux_readlink(struct proc *p, struct linux_readlink_args *args)
770 {
771 	struct readlink_args bsd;
772 	caddr_t sg;
773 
774 	sg = stackgap_init();
775 	CHECKALTEXIST(p, &sg, args->name);
776 
777 #ifdef DEBUG
778         printf("Linux-emul(%ld): readlink(%s, %p, %d)\n",
779 	    (long)p->p_pid, args->name, (void *)args->buf, args->count);
780 #endif
781 	bsd.path = args->name;
782 	bsd.buf = args->buf;
783 	bsd.count = args->count;
784 
785 	return readlink(p, &bsd);
786 }
787 
788 int
789 linux_truncate(struct proc *p, struct linux_truncate_args *args)
790 {
791 	struct truncate_args bsd;
792 	caddr_t sg;
793 
794 	sg = stackgap_init();
795 	CHECKALTEXIST(p, &sg, args->path);
796 
797 #ifdef DEBUG
798         printf("Linux-emul(%d): truncate(%s, %ld)\n",
799 	    p->p_pid, args->path, args->length);
800 #endif
801 	bsd.path = args->path;
802 	bsd.length = args->length;
803 
804 	return truncate(p, &bsd);
805 }
806 
807 int
808 linux_link(struct proc *p, struct linux_link_args *args)
809 {
810     struct link_args bsd;
811     caddr_t sg;
812 
813     sg = stackgap_init();
814     CHECKALTEXIST(p, &sg, args->path);
815     CHECKALTCREAT(p, &sg, args->to);
816 
817 #ifdef DEBUG
818     printf("Linux-emul(%d): link(%s, %s)\n", p->p_pid, args->path, args->to);
819 #endif
820 
821     bsd.path = args->path;
822     bsd.link = args->to;
823 
824     return link(p, &bsd);
825 }
826 
827 int
828 linux_getcwd(struct proc *p, struct linux_getcwd_args *args)
829 {
830 	struct __getcwd_args bsd;
831 	caddr_t sg;
832 	int error, len;
833 
834 #ifdef DEBUG
835 	printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
836 	       args->buf, args->bufsize);
837 #endif
838 
839 	sg = stackgap_init();
840 	bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
841 	bsd.buflen = SPARE_USRSPACE;
842 	error = __getcwd(p, &bsd);
843 	if (!error) {
844 		len = strlen(bsd.buf) + 1;
845 		if (len <= args->bufsize) {
846 			p->p_retval[0] = len;
847 			error = copyout(bsd.buf, args->buf, len);
848 		}
849 		else
850 			error = ERANGE;
851 	}
852 	return (error);
853 }
854 
855 #ifndef __alpha__
856 int
857 linux_fdatasync(p, uap)
858 	struct proc *p;
859 	struct linux_fdatasync_args *uap;
860 {
861 	struct fsync_args bsd;
862 
863 	bsd.fd = uap->fd;
864 	return fsync(p, &bsd);
865 }
866 #endif /*!__alpha__*/
867 
868 int
869 linux_pread(p, uap)
870 	struct proc *p;
871 	struct linux_pread_args *uap;
872 {
873 	struct pread_args bsd;
874 
875 	bsd.fd = uap->fd;
876 	bsd.buf = uap->buf;
877 	bsd.nbyte = uap->nbyte;
878 	bsd.offset = uap->offset;
879 	return pread(p, &bsd);
880 }
881 
882 int
883 linux_pwrite(p, uap)
884 	struct proc *p;
885 	struct linux_pwrite_args *uap;
886 {
887 	struct pwrite_args bsd;
888 
889 	bsd.fd = uap->fd;
890 	bsd.buf = uap->buf;
891 	bsd.nbyte = uap->nbyte;
892 	bsd.offset = uap->offset;
893 	return pwrite(p, &bsd);
894 }
895