Deleted Added
sdiff udiff text old ( 71699 ) new ( 72538 )
full compact
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 71699 2001-01-27 00:01:31Z jhb $
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__
52int
53linux_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
76int
77linux_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
145struct 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
153static void
154linux_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
176static void
177bsd_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
196int
197linux_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
314int
315linux_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__
338int
339linux_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
367struct 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__
378int
379linux_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
390int
391linux_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
409#ifdef DEBUG
410 printf("Linux-emul(%d): getdents(%d, *, %d)\n",
411 p->p_pid, args->fd, args->count);
412#endif
413 if ((error = getvnode(p->p_fd, args->fd, &fp)) != 0) {
414 return (error);
415 }
416
417 if ((fp->f_flag & FREAD) == 0)
418 return (EBADF);
419
420 vp = (struct vnode *) fp->f_data;
421
422 if (vp->v_type != VDIR)
423 return (EINVAL);
424
425 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p))) {
426 return error;
427 }
428
429 nbytes = args->count;
430 if (nbytes == 1) {
431 nbytes = sizeof (struct linux_dirent);
432 justone = 1;
433 }
434 else
435 justone = 0;
436
437 off = fp->f_offset;
438#define DIRBLKSIZ 512 /* XXX we used to use ufs's DIRBLKSIZ */
439 buflen = max(DIRBLKSIZ, nbytes);
440 buflen = min(buflen, MAXBSIZE);
441 buf = malloc(buflen, M_TEMP, M_WAITOK);
442 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
443again:
444 aiov.iov_base = buf;
445 aiov.iov_len = buflen;
446 auio.uio_iov = &aiov;
447 auio.uio_iovcnt = 1;
448 auio.uio_rw = UIO_READ;
449 auio.uio_segflg = UIO_SYSSPACE;
450 auio.uio_procp = p;
451 auio.uio_resid = buflen;
452 auio.uio_offset = off;
453
454 if (cookies) {
455 free(cookies, M_TEMP);
456 cookies = NULL;
457 }
458
459 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies, &cookies);
460 if (error) {
461 goto out;
462 }
463
464 inp = buf;
465 outp = (caddr_t) args->dent;
466 resid = nbytes;
467 if ((len = buflen - auio.uio_resid) <= 0) {
468 goto eof;
469 }
470
471 cookiep = cookies;
472
473 if (cookies) {
474 /*
475 * When using cookies, the vfs has the option of reading from
476 * a different offset than that supplied (UFS truncates the
477 * offset to a block boundary to make sure that it never reads
478 * partway through a directory entry, even if the directory
479 * has been compacted).
480 */
481 while (len > 0 && ncookies > 0 && *cookiep <= off) {
482 bdp = (struct dirent *) inp;
483 len -= bdp->d_reclen;
484 inp += bdp->d_reclen;
485 cookiep++;
486 ncookies--;
487 }
488 }
489
490 while (len > 0) {
491 if (cookiep && ncookies == 0)
492 break;
493 bdp = (struct dirent *) inp;
494 reclen = bdp->d_reclen;
495 if (reclen & 3) {
496 printf("linux_readdir: reclen=%d\n", reclen);
497 error = EFAULT;
498 goto out;
499 }
500
501 if (bdp->d_fileno == 0) {
502 inp += reclen;
503 if (cookiep) {
504 off = *cookiep++;
505 ncookies--;
506 } else
507 off += reclen;
508 len -= reclen;
509 continue;
510 }
511 linuxreclen = LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
512 if (reclen > len || resid < linuxreclen) {
513 outp++;
514 break;
515 }
516 linux_dirent.dino = (long) bdp->d_fileno;
517 if (justone) {
518 /*
519 * old linux-style readdir usage.
520 */
521 linux_dirent.doff = (linux_off_t) linuxreclen;
522 linux_dirent.dreclen = (u_short) bdp->d_namlen;
523 } else {
524 if (cookiep)
525 linux_dirent.doff = (linux_off_t)*cookiep;
526 else
527 linux_dirent.doff = (linux_off_t)(off + reclen);
528 linux_dirent.dreclen = (u_short) linuxreclen;
529 }
530 strcpy(linux_dirent.dname, bdp->d_name);
531 if ((error = copyout((caddr_t)&linux_dirent, outp, linuxreclen))) {
532 goto out;
533 }
534 inp += reclen;
535 if (cookiep) {
536 off = *cookiep++;
537 ncookies--;
538 } else
539 off += reclen;
540 outp += linuxreclen;
541 resid -= linuxreclen;
542 len -= reclen;
543 if (justone)
544 break;
545 }
546
547 if (outp == (caddr_t) args->dent)
548 goto again;
549 fp->f_offset = off;
550
551 if (justone)
552 nbytes = resid + linuxreclen;
553
554eof:
555 p->p_retval[0] = nbytes - resid;
556out:
557 if (cookies)
558 free(cookies, M_TEMP);
559 VOP_UNLOCK(vp, 0, p);
560 free(buf, M_TEMP);
561 return error;
562}
563
564/*
565 * These exist mainly for hooks for doing /compat/linux translation.
566 */
567
568int
569linux_access(struct proc *p, struct linux_access_args *args)
570{
571 struct access_args bsd;
572 caddr_t sg;
573
574 sg = stackgap_init();
575 CHECKALTEXIST(p, &sg, args->path);
576
577#ifdef DEBUG
578 printf("Linux-emul(%d): access(%s, %d)\n",
579 p->p_pid, args->path, args->flags);
580#endif
581 bsd.path = args->path;
582 bsd.flags = args->flags;
583
584 return access(p, &bsd);
585}
586
587int
588linux_unlink(struct proc *p, struct linux_unlink_args *args)
589{
590 struct unlink_args bsd;
591 caddr_t sg;
592
593 sg = stackgap_init();
594 CHECKALTEXIST(p, &sg, args->path);
595
596#ifdef DEBUG
597 printf("Linux-emul(%d): unlink(%s)\n",
598 p->p_pid, args->path);
599#endif
600 bsd.path = args->path;
601
602 return unlink(p, &bsd);
603}
604
605int
606linux_chdir(struct proc *p, struct linux_chdir_args *args)
607{
608 struct chdir_args bsd;
609 caddr_t sg;
610
611 sg = stackgap_init();
612 CHECKALTEXIST(p, &sg, args->path);
613
614#ifdef DEBUG
615 printf("Linux-emul(%d): chdir(%s)\n",
616 p->p_pid, args->path);
617#endif
618 bsd.path = args->path;
619
620 return chdir(p, &bsd);
621}
622
623int
624linux_chmod(struct proc *p, struct linux_chmod_args *args)
625{
626 struct chmod_args bsd;
627 caddr_t sg;
628
629 sg = stackgap_init();
630 CHECKALTEXIST(p, &sg, args->path);
631
632#ifdef DEBUG
633 printf("Linux-emul(%d): chmod(%s, %d)\n",
634 p->p_pid, args->path, args->mode);
635#endif
636 bsd.path = args->path;
637 bsd.mode = args->mode;
638
639 return chmod(p, &bsd);
640}
641
642int
643linux_chown(struct proc *p, struct linux_chown_args *args)
644{
645 struct chown_args bsd;
646 caddr_t sg;
647
648 sg = stackgap_init();
649 CHECKALTEXIST(p, &sg, args->path);
650
651#ifdef DEBUG
652 printf("Linux-emul(%d): chown(%s, %d, %d)\n",
653 p->p_pid, args->path, args->uid, args->gid);
654#endif
655 bsd.path = args->path;
656 /* XXX size casts here */
657 bsd.uid = args->uid;
658 bsd.gid = args->gid;
659
660 return chown(p, &bsd);
661}
662
663int
664linux_lchown(struct proc *p, struct linux_lchown_args *args)
665{
666 struct lchown_args bsd;
667 caddr_t sg;
668
669 sg = stackgap_init();
670 CHECKALTEXIST(p, &sg, args->path);
671
672#ifdef DEBUG
673 printf("Linux-emul(%d): lchown(%s, %d, %d)\n",
674 p->p_pid, args->path, args->uid, args->gid);
675#endif
676 bsd.path = args->path;
677 /* XXX size casts here */
678 bsd.uid = args->uid;
679 bsd.gid = args->gid;
680
681 return lchown(p, &bsd);
682}
683
684int
685linux_mkdir(struct proc *p, struct linux_mkdir_args *args)
686{
687 struct mkdir_args bsd;
688 caddr_t sg;
689
690 sg = stackgap_init();
691 CHECKALTCREAT(p, &sg, args->path);
692
693#ifdef DEBUG
694 printf("Linux-emul(%d): mkdir(%s, %d)\n",
695 p->p_pid, args->path, args->mode);
696#endif
697 bsd.path = args->path;
698 bsd.mode = args->mode;
699
700 return mkdir(p, &bsd);
701}
702
703int
704linux_rmdir(struct proc *p, struct linux_rmdir_args *args)
705{
706 struct rmdir_args bsd;
707 caddr_t sg;
708
709 sg = stackgap_init();
710 CHECKALTEXIST(p, &sg, args->path);
711
712#ifdef DEBUG
713 printf("Linux-emul(%d): rmdir(%s)\n",
714 p->p_pid, args->path);
715#endif
716 bsd.path = args->path;
717
718 return rmdir(p, &bsd);
719}
720
721int
722linux_rename(struct proc *p, struct linux_rename_args *args)
723{
724 struct rename_args bsd;
725 caddr_t sg;
726
727 sg = stackgap_init();
728 CHECKALTEXIST(p, &sg, args->from);
729 CHECKALTCREAT(p, &sg, args->to);
730
731#ifdef DEBUG
732 printf("Linux-emul(%d): rename(%s, %s)\n",
733 p->p_pid, args->from, args->to);
734#endif
735 bsd.from = args->from;
736 bsd.to = args->to;
737
738 return rename(p, &bsd);
739}
740
741int
742linux_symlink(struct proc *p, struct linux_symlink_args *args)
743{
744 struct symlink_args bsd;
745 caddr_t sg;
746
747 sg = stackgap_init();
748 CHECKALTEXIST(p, &sg, args->path);
749 CHECKALTCREAT(p, &sg, args->to);
750
751#ifdef DEBUG
752 printf("Linux-emul(%d): symlink(%s, %s)\n",
753 p->p_pid, args->path, args->to);
754#endif
755 bsd.path = args->path;
756 bsd.link = args->to;
757
758 return symlink(p, &bsd);
759}
760
761int
762linux_readlink(struct proc *p, struct linux_readlink_args *args)
763{
764 struct readlink_args bsd;
765 caddr_t sg;
766
767 sg = stackgap_init();
768 CHECKALTEXIST(p, &sg, args->name);
769
770#ifdef DEBUG
771 printf("Linux-emul(%ld): readlink(%s, %p, %d)\n",
772 (long)p->p_pid, args->name, (void *)args->buf, args->count);
773#endif
774 bsd.path = args->name;
775 bsd.buf = args->buf;
776 bsd.count = args->count;
777
778 return readlink(p, &bsd);
779}
780
781int
782linux_truncate(struct proc *p, struct linux_truncate_args *args)
783{
784 struct truncate_args bsd;
785 caddr_t sg;
786
787 sg = stackgap_init();
788 CHECKALTEXIST(p, &sg, args->path);
789
790#ifdef DEBUG
791 printf("Linux-emul(%d): truncate(%s, %ld)\n",
792 p->p_pid, args->path, args->length);
793#endif
794 bsd.path = args->path;
795 bsd.length = args->length;
796
797 return truncate(p, &bsd);
798}
799
800int
801linux_link(struct proc *p, struct linux_link_args *args)
802{
803 struct link_args bsd;
804 caddr_t sg;
805
806 sg = stackgap_init();
807 CHECKALTEXIST(p, &sg, args->path);
808 CHECKALTCREAT(p, &sg, args->to);
809
810#ifdef DEBUG
811 printf("Linux-emul(%d): link(%s, %s)\n", p->p_pid, args->path, args->to);
812#endif
813
814 bsd.path = args->path;
815 bsd.link = args->to;
816
817 return link(p, &bsd);
818}
819
820int
821linux_getcwd(struct proc *p, struct linux_getcwd_args *args)
822{
823 struct __getcwd_args bsd;
824 caddr_t sg;
825 int error, len;
826
827#ifdef DEBUG
828 printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)p->p_pid,
829 args->buf, args->bufsize);
830#endif
831
832 sg = stackgap_init();
833 bsd.buf = stackgap_alloc(&sg, SPARE_USRSPACE);
834 bsd.buflen = SPARE_USRSPACE;
835 error = __getcwd(p, &bsd);
836 if (!error) {
837 len = strlen(bsd.buf) + 1;
838 if (len <= args->bufsize) {
839 p->p_retval[0] = len;
840 error = copyout(bsd.buf, args->buf, len);
841 }
842 else
843 error = ERANGE;
844 }
845 return (error);
846}
847
848#ifndef __alpha__
849int
850linux_fdatasync(p, uap)
851 struct proc *p;
852 struct linux_fdatasync_args *uap;
853{
854 struct fsync_args bsd;
855
856 bsd.fd = uap->fd;
857 return fsync(p, &bsd);
858}
859#endif /*!__alpha__*/
860
861int
862linux_pread(p, uap)
863 struct proc *p;
864 struct linux_pread_args *uap;
865{
866 struct pread_args bsd;
867
868 bsd.fd = uap->fd;
869 bsd.buf = uap->buf;
870 bsd.nbyte = uap->nbyte;
871 bsd.offset = uap->offset;
872 return pread(p, &bsd);
873}
874
875int
876linux_pwrite(p, uap)
877 struct proc *p;
878 struct linux_pwrite_args *uap;
879{
880 struct pwrite_args bsd;
881
882 bsd.fd = uap->fd;
883 bsd.buf = uap->buf;
884 bsd.nbyte = uap->nbyte;
885 bsd.offset = uap->offset;
886 return pwrite(p, &bsd);
887}