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 without 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/6.3/sys/compat/linux/linux_file.c 172004 2007-08-29 15:04:26Z netchild $");
31
32 #include "opt_compat.h"
33 #include "opt_mac.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/dirent.h>
39 #include <sys/fcntl.h>
40 #include <sys/file.h>
41 #include <sys/filedesc.h>
42 #include <sys/lock.h>
43 #include <sys/mac.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/stat.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/sysproto.h>
51 #include <sys/unistd.h>
52 #include <sys/tty.h>
53 #include <sys/vnode.h>
54
55 #include <ufs/ufs/extattr.h>
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/ufsmount.h>
58
59 #include "opt_compat.h"
60
61 #ifdef COMPAT_LINUX32
62 #include <machine/../linux32/linux.h>
63 #include <machine/../linux32/linux32_proto.h>
64 #else
65 #include <machine/../linux/linux.h>
66 #include <machine/../linux/linux_proto.h>
67 #endif
68 #include <compat/linux/linux_util.h>
69
70 #ifndef __alpha__
71 int
72 linux_creat(struct thread *td, struct linux_creat_args *args)
73 {
74 char *path;
75 int error;
76
77 LCONVPATHEXIST(td, args->path, &path);
78
79 #ifdef DEBUG
80 if (ldebug(creat))
81 printf(ARGS(creat, "%s, %d"), path, args->mode);
82 #endif
83 error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
84 args->mode);
85 LFREEPATH(path);
86 return (error);
87 }
88 #endif /*!__alpha__*/
89
90 int
91 linux_open(struct thread *td, struct linux_open_args *args)
92 {
93 struct proc *p = td->td_proc;
94 char *path;
95 int bsd_flags, error;
96
97 if (args->flags & LINUX_O_CREAT)
98 LCONVPATHCREAT(td, args->path, &path);
99 else
100 LCONVPATHEXIST(td, args->path, &path);
101
102 #ifdef DEBUG
103 if (ldebug(open))
104 printf(ARGS(open, "%s, 0x%x, 0x%x"),
105 path, args->flags, args->mode);
106 #endif
107 bsd_flags = 0;
108 if (args->flags & LINUX_O_RDONLY)
109 bsd_flags |= O_RDONLY;
110 if (args->flags & LINUX_O_WRONLY)
111 bsd_flags |= O_WRONLY;
112 if (args->flags & LINUX_O_RDWR)
113 bsd_flags |= O_RDWR;
114 if (args->flags & LINUX_O_NDELAY)
115 bsd_flags |= O_NONBLOCK;
116 if (args->flags & LINUX_O_APPEND)
117 bsd_flags |= O_APPEND;
118 if (args->flags & LINUX_O_SYNC)
119 bsd_flags |= O_FSYNC;
120 if (args->flags & LINUX_O_NONBLOCK)
121 bsd_flags |= O_NONBLOCK;
122 if (args->flags & LINUX_FASYNC)
123 bsd_flags |= O_ASYNC;
124 if (args->flags & LINUX_O_CREAT)
125 bsd_flags |= O_CREAT;
126 if (args->flags & LINUX_O_TRUNC)
127 bsd_flags |= O_TRUNC;
128 if (args->flags & LINUX_O_EXCL)
129 bsd_flags |= O_EXCL;
130 if (args->flags & LINUX_O_NOCTTY)
131 bsd_flags |= O_NOCTTY;
132
133 error = kern_open(td, path, UIO_SYSSPACE, bsd_flags, args->mode);
134 PROC_LOCK(p);
135 if (!error && !(bsd_flags & O_NOCTTY) &&
136 SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
137 struct file *fp;
138
139 PROC_UNLOCK(p);
140 error = fget(td, td->td_retval[0], &fp);
141 if (!error) {
142 if (fp->f_type == DTYPE_VNODE)
143 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
144 td);
145 fdrop(fp, td);
146 }
147 } else {
148 PROC_UNLOCK(p);
149 #ifdef DEBUG
150 if (ldebug(open))
151 printf(LMSG("open returns error %d"), error);
152 #endif
153 }
154 LFREEPATH(path);
155 return error;
156 }
157
158 int
159 linux_lseek(struct thread *td, struct linux_lseek_args *args)
160 {
161
162 struct lseek_args /* {
163 int fd;
164 int pad;
165 off_t offset;
166 int whence;
167 } */ tmp_args;
168 int error;
169
170 #ifdef DEBUG
171 if (ldebug(lseek))
172 printf(ARGS(lseek, "%d, %ld, %d"),
173 args->fdes, (long)args->off, args->whence);
174 #endif
175 tmp_args.fd = args->fdes;
176 tmp_args.offset = (off_t)args->off;
177 tmp_args.whence = args->whence;
178 error = lseek(td, &tmp_args);
179 return error;
180 }
181
182 #ifndef __alpha__
183 int
184 linux_llseek(struct thread *td, struct linux_llseek_args *args)
185 {
186 struct lseek_args bsd_args;
187 int error;
188 off_t off;
189
190 #ifdef DEBUG
191 if (ldebug(llseek))
192 printf(ARGS(llseek, "%d, %d:%d, %d"),
193 args->fd, args->ohigh, args->olow, args->whence);
194 #endif
195 off = (args->olow) | (((off_t) args->ohigh) << 32);
196
197 bsd_args.fd = args->fd;
198 bsd_args.offset = off;
199 bsd_args.whence = args->whence;
200
201 if ((error = lseek(td, &bsd_args)))
202 return error;
203
204 if ((error = copyout(td->td_retval, args->res, sizeof (off_t))))
205 return error;
206
207 td->td_retval[0] = 0;
208 return 0;
209 }
210 #endif /*!__alpha__*/
211
212 #ifndef __alpha__
213 int
214 linux_readdir(struct thread *td, struct linux_readdir_args *args)
215 {
216 struct linux_getdents_args lda;
217
218 lda.fd = args->fd;
219 lda.dent = args->dent;
220 lda.count = 1;
221 return linux_getdents(td, &lda);
222 }
223 #endif /*!__alpha__*/
224
225 /*
226 * Note that linux_getdents(2) and linux_getdents64(2) have the same
227 * arguments. They only differ in the definition of struct dirent they
228 * operate on. We use this to common the code, with the exception of
229 * accessing struct dirent. Note that linux_readdir(2) is implemented
230 * by means of linux_getdents(2). In this case we never operate on
231 * struct dirent64 and thus don't need to handle it...
232 */
233
234 struct l_dirent {
235 l_long d_ino;
236 l_off_t d_off;
237 l_ushort d_reclen;
238 char d_name[LINUX_NAME_MAX + 1];
239 };
240
241 struct l_dirent64 {
242 uint64_t d_ino;
243 int64_t d_off;
244 l_ushort d_reclen;
245 u_char d_type;
246 char d_name[LINUX_NAME_MAX + 1];
247 };
248
249 #define LINUX_RECLEN(de,namlen) \
250 ALIGN((((char *)&(de)->d_name - (char *)de) + (namlen) + 1))
251
252 #define LINUX_DIRBLKSIZ 512
253
254 static int
255 getdents_common(struct thread *td, struct linux_getdents64_args *args,
256 int is64bit)
257 {
258 struct dirent *bdp;
259 struct vnode *vp;
260 caddr_t inp, buf; /* BSD-format */
261 int len, reclen; /* BSD-format */
262 caddr_t outp; /* Linux-format */
263 int resid, linuxreclen=0; /* Linux-format */
264 struct file *fp;
265 struct uio auio;
266 struct iovec aiov;
267 off_t off;
268 struct l_dirent linux_dirent;
269 struct l_dirent64 linux_dirent64;
270 int buflen, error, eofflag, nbytes, justone;
271 u_long *cookies = NULL, *cookiep;
272 int ncookies, vfslocked;
273
274 nbytes = args->count;
275 if (nbytes == 1) {
276 /* readdir(2) case. Always struct dirent. */
277 if (is64bit)
278 return (EINVAL);
279 nbytes = sizeof(linux_dirent);
280 justone = 1;
281 } else
282 justone = 0;
283
284 if ((error = getvnode(td->td_proc->p_fd, args->fd, &fp)) != 0)
285 return (error);
286
287 if ((fp->f_flag & FREAD) == 0) {
288 fdrop(fp, td);
289 return (EBADF);
290 }
291
292 vp = fp->f_vnode;
293 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
294 if (vp->v_type != VDIR) {
295 VFS_UNLOCK_GIANT(vfslocked);
296 fdrop(fp, td);
297 return (EINVAL);
298 }
299
300 off = fp->f_offset;
301
302 buflen = max(LINUX_DIRBLKSIZ, nbytes);
303 buflen = min(buflen, MAXBSIZE);
304 buf = malloc(buflen, M_TEMP, M_WAITOK);
305 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
306
307 again:
308 aiov.iov_base = buf;
309 aiov.iov_len = buflen;
310 auio.uio_iov = &aiov;
311 auio.uio_iovcnt = 1;
312 auio.uio_rw = UIO_READ;
313 auio.uio_segflg = UIO_SYSSPACE;
314 auio.uio_td = td;
315 auio.uio_resid = buflen;
316 auio.uio_offset = off;
317
318 if (cookies) {
319 free(cookies, M_TEMP);
320 cookies = NULL;
321 }
322
323 #ifdef MAC
324 /*
325 * Do directory search MAC check using non-cached credentials.
326 */
327 if ((error = mac_check_vnode_readdir(td->td_ucred, vp)))
328 goto out;
329 #endif /* MAC */
330 if ((error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &ncookies,
331 &cookies)))
332 goto out;
333
334 inp = buf;
335 outp = (caddr_t)args->dirent;
336 resid = nbytes;
337 if ((len = buflen - auio.uio_resid) <= 0)
338 goto eof;
339
340 cookiep = cookies;
341
342 if (cookies) {
343 /*
344 * When using cookies, the vfs has the option of reading from
345 * a different offset than that supplied (UFS truncates the
346 * offset to a block boundary to make sure that it never reads
347 * partway through a directory entry, even if the directory
348 * has been compacted).
349 */
350 while (len > 0 && ncookies > 0 && *cookiep <= off) {
351 bdp = (struct dirent *) inp;
352 len -= bdp->d_reclen;
353 inp += bdp->d_reclen;
354 cookiep++;
355 ncookies--;
356 }
357 }
358
359 while (len > 0) {
360 if (cookiep && ncookies == 0)
361 break;
362 bdp = (struct dirent *) inp;
363 reclen = bdp->d_reclen;
364 if (reclen & 3) {
365 error = EFAULT;
366 goto out;
367 }
368
369 if (bdp->d_fileno == 0) {
370 inp += reclen;
371 if (cookiep) {
372 off = *cookiep++;
373 ncookies--;
374 } else
375 off += reclen;
376
377 len -= reclen;
378 continue;
379 }
380
381 linuxreclen = (is64bit)
382 ? LINUX_RECLEN(&linux_dirent64, bdp->d_namlen)
383 : LINUX_RECLEN(&linux_dirent, bdp->d_namlen);
384
385 if (reclen > len || resid < linuxreclen) {
386 outp++;
387 break;
388 }
389
390 if (justone) {
391 /* readdir(2) case. */
392 linux_dirent.d_ino = (l_long)bdp->d_fileno;
393 linux_dirent.d_off = (l_off_t)linuxreclen;
394 linux_dirent.d_reclen = (l_ushort)bdp->d_namlen;
395 strcpy(linux_dirent.d_name, bdp->d_name);
396 error = copyout(&linux_dirent, outp, linuxreclen);
397 } else {
398 if (is64bit) {
399 linux_dirent64.d_ino = bdp->d_fileno;
400 linux_dirent64.d_off = (cookiep)
401 ? (l_off_t)*cookiep
402 : (l_off_t)(off + reclen);
403 linux_dirent64.d_reclen =
404 (l_ushort)linuxreclen;
405 linux_dirent64.d_type = bdp->d_type;
406 strcpy(linux_dirent64.d_name, bdp->d_name);
407 error = copyout(&linux_dirent64, outp,
408 linuxreclen);
409 } else {
410 linux_dirent.d_ino = bdp->d_fileno;
411 linux_dirent.d_off = (cookiep)
412 ? (l_off_t)*cookiep
413 : (l_off_t)(off + reclen);
414 linux_dirent.d_reclen = (l_ushort)linuxreclen;
415 strcpy(linux_dirent.d_name, bdp->d_name);
416 error = copyout(&linux_dirent, outp,
417 linuxreclen);
418 }
419 }
420 if (error)
421 goto out;
422
423 inp += reclen;
424 if (cookiep) {
425 off = *cookiep++;
426 ncookies--;
427 } else
428 off += reclen;
429
430 outp += linuxreclen;
431 resid -= linuxreclen;
432 len -= reclen;
433 if (justone)
434 break;
435 }
436
437 if (outp == (caddr_t)args->dirent)
438 goto again;
439
440 fp->f_offset = off;
441 if (justone)
442 nbytes = resid + linuxreclen;
443
444 eof:
445 td->td_retval[0] = nbytes - resid;
446
447 out:
448 if (cookies)
449 free(cookies, M_TEMP);
450
451 VOP_UNLOCK(vp, 0, td);
452 VFS_UNLOCK_GIANT(vfslocked);
453 fdrop(fp, td);
454 free(buf, M_TEMP);
455 return (error);
456 }
457
458 int
459 linux_getdents(struct thread *td, struct linux_getdents_args *args)
460 {
461
462 #ifdef DEBUG
463 if (ldebug(getdents))
464 printf(ARGS(getdents, "%d, *, %d"), args->fd, args->count);
465 #endif
466
467 return (getdents_common(td, (struct linux_getdents64_args*)args, 0));
468 }
469
470 int
471 linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
472 {
473
474 #ifdef DEBUG
475 if (ldebug(getdents64))
476 printf(ARGS(getdents64, "%d, *, %d"), args->fd, args->count);
477 #endif
478
479 return (getdents_common(td, args, 1));
480 }
481
482 /*
483 * These exist mainly for hooks for doing /compat/linux translation.
484 */
485
486 int
487 linux_access(struct thread *td, struct linux_access_args *args)
488 {
489 char *path;
490 int error;
491
492 /* linux convention */
493 if (args->flags & ~(F_OK | X_OK | W_OK | R_OK))
494 return (EINVAL);
495
496 LCONVPATHEXIST(td, args->path, &path);
497
498 #ifdef DEBUG
499 if (ldebug(access))
500 printf(ARGS(access, "%s, %d"), path, args->flags);
501 #endif
502 error = kern_access(td, path, UIO_SYSSPACE, args->flags);
503 LFREEPATH(path);
504
505 return (error);
506 }
507
508 int
509 linux_unlink(struct thread *td, struct linux_unlink_args *args)
510 {
511 char *path;
512 int error;
513 struct stat st;
514
515 LCONVPATHEXIST(td, args->path, &path);
516
517 #ifdef DEBUG
518 if (ldebug(unlink))
519 printf(ARGS(unlink, "%s"), path);
520 #endif
521
522 error = kern_unlink(td, path, UIO_SYSSPACE);
523 if (error == EPERM)
524 /* Introduce POSIX noncompliant behaviour of Linux */
525 if (kern_stat(td, path, UIO_SYSSPACE, &st) == 0)
526 if (S_ISDIR(st.st_mode))
527 error = EISDIR;
528 LFREEPATH(path);
529 return (error);
530 }
531
532 int
533 linux_chdir(struct thread *td, struct linux_chdir_args *args)
534 {
535 char *path;
536 int error;
537
538 LCONVPATHEXIST(td, args->path, &path);
539
540 #ifdef DEBUG
541 if (ldebug(chdir))
542 printf(ARGS(chdir, "%s"), path);
543 #endif
544 error = kern_chdir(td, path, UIO_SYSSPACE);
545 LFREEPATH(path);
546 return (error);
547 }
548
549 int
550 linux_chmod(struct thread *td, struct linux_chmod_args *args)
551 {
552 char *path;
553 int error;
554
555 LCONVPATHEXIST(td, args->path, &path);
556
557 #ifdef DEBUG
558 if (ldebug(chmod))
559 printf(ARGS(chmod, "%s, %d"), path, args->mode);
560 #endif
561 error = kern_chmod(td, path, UIO_SYSSPACE, args->mode);
562 LFREEPATH(path);
563 return (error);
564 }
565
566 int
567 linux_mkdir(struct thread *td, struct linux_mkdir_args *args)
568 {
569 char *path;
570 int error;
571
572 LCONVPATHCREAT(td, args->path, &path);
573
574 #ifdef DEBUG
575 if (ldebug(mkdir))
576 printf(ARGS(mkdir, "%s, %d"), path, args->mode);
577 #endif
578 error = kern_mkdir(td, path, UIO_SYSSPACE, args->mode);
579 LFREEPATH(path);
580 return (error);
581 }
582
583 int
584 linux_rmdir(struct thread *td, struct linux_rmdir_args *args)
585 {
586 char *path;
587 int error;
588
589 LCONVPATHEXIST(td, args->path, &path);
590
591 #ifdef DEBUG
592 if (ldebug(rmdir))
593 printf(ARGS(rmdir, "%s"), path);
594 #endif
595 error = kern_rmdir(td, path, UIO_SYSSPACE);
596 LFREEPATH(path);
597 return (error);
598 }
599
600 int
601 linux_rename(struct thread *td, struct linux_rename_args *args)
602 {
603 char *from, *to;
604 int error;
605
606 LCONVPATHEXIST(td, args->from, &from);
607 /* Expand LCONVPATHCREATE so that `from' can be freed on errors */
608 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
609 if (to == NULL) {
610 LFREEPATH(from);
611 return (error);
612 }
613
614 #ifdef DEBUG
615 if (ldebug(rename))
616 printf(ARGS(rename, "%s, %s"), from, to);
617 #endif
618 error = kern_rename(td, from, to, UIO_SYSSPACE);
619 LFREEPATH(from);
620 LFREEPATH(to);
621 return (error);
622 }
623
624 int
625 linux_symlink(struct thread *td, struct linux_symlink_args *args)
626 {
627 char *path, *to;
628 int error;
629
630 LCONVPATHEXIST(td, args->path, &path);
631 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
632 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
633 if (to == NULL) {
634 LFREEPATH(path);
635 return (error);
636 }
637
638 #ifdef DEBUG
639 if (ldebug(symlink))
640 printf(ARGS(symlink, "%s, %s"), path, to);
641 #endif
642 error = kern_symlink(td, path, to, UIO_SYSSPACE);
643 LFREEPATH(path);
644 LFREEPATH(to);
645 return (error);
646 }
647
648 int
649 linux_readlink(struct thread *td, struct linux_readlink_args *args)
650 {
651 char *name;
652 int error;
653
654 LCONVPATHEXIST(td, args->name, &name);
655
656 #ifdef DEBUG
657 if (ldebug(readlink))
658 printf(ARGS(readlink, "%s, %p, %d"), name, (void *)args->buf,
659 args->count);
660 #endif
661 error = kern_readlink(td, name, UIO_SYSSPACE, args->buf, UIO_USERSPACE,
662 args->count);
663 LFREEPATH(name);
664 return (error);
665 }
666
667 int
668 linux_truncate(struct thread *td, struct linux_truncate_args *args)
669 {
670 char *path;
671 int error;
672
673 LCONVPATHEXIST(td, args->path, &path);
674
675 #ifdef DEBUG
676 if (ldebug(truncate))
677 printf(ARGS(truncate, "%s, %ld"), path, (long)args->length);
678 #endif
679
680 error = kern_truncate(td, path, UIO_SYSSPACE, args->length);
681 LFREEPATH(path);
682 return (error);
683 }
684
685 int
686 linux_ftruncate(struct thread *td, struct linux_ftruncate_args *args)
687 {
688 struct ftruncate_args /* {
689 int fd;
690 int pad;
691 off_t length;
692 } */ nuap;
693
694 nuap.fd = args->fd;
695 nuap.pad = 0;
696 nuap.length = args->length;
697 return (ftruncate(td, &nuap));
698 }
699
700 int
701 linux_link(struct thread *td, struct linux_link_args *args)
702 {
703 char *path, *to;
704 int error;
705
706 LCONVPATHEXIST(td, args->path, &path);
707 /* Expand LCONVPATHCREATE so that `path' can be freed on errors */
708 error = linux_emul_convpath(td, args->to, UIO_USERSPACE, &to, 1);
709 if (to == NULL) {
710 LFREEPATH(path);
711 return (error);
712 }
713
714 #ifdef DEBUG
715 if (ldebug(link))
716 printf(ARGS(link, "%s, %s"), path, to);
717 #endif
718 error = kern_link(td, path, to, UIO_SYSSPACE);
719 LFREEPATH(path);
720 LFREEPATH(to);
721 return (error);
722 }
723
724 #ifndef __alpha__
725 int
726 linux_fdatasync(td, uap)
727 struct thread *td;
728 struct linux_fdatasync_args *uap;
729 {
730 struct fsync_args bsd;
731
732 bsd.fd = uap->fd;
733 return fsync(td, &bsd);
734 }
735 #endif /*!__alpha__*/
736
737 int
738 linux_pread(td, uap)
739 struct thread *td;
740 struct linux_pread_args *uap;
741 {
742 struct pread_args bsd;
743 struct vnode *vp;
744 int error;
745
746 bsd.fd = uap->fd;
747 bsd.buf = uap->buf;
748 bsd.nbyte = uap->nbyte;
749 bsd.offset = uap->offset;
750
751 error = pread(td, &bsd);
752
753 if (error == 0) {
754 /* This seems to violate POSIX but linux does it */
755 if ((error = fgetvp(td, uap->fd, &vp)) != 0)
756 return (error);
757 if (vp->v_type == VDIR) {
758 vrele(vp);
759 return (EISDIR);
760 }
761 vrele(vp);
762 }
763
764 return (error);
765 }
766
767 int
768 linux_pwrite(td, uap)
769 struct thread *td;
770 struct linux_pwrite_args *uap;
771 {
772 struct pwrite_args bsd;
773
774 bsd.fd = uap->fd;
775 bsd.buf = uap->buf;
776 bsd.nbyte = uap->nbyte;
777 bsd.offset = uap->offset;
778 return pwrite(td, &bsd);
779 }
780
781 int
782 linux_mount(struct thread *td, struct linux_mount_args *args)
783 {
784 struct ufs_args ufs;
785 char fstypename[MFSNAMELEN];
786 char mntonname[MNAMELEN], mntfromname[MNAMELEN];
787 int error;
788 int fsflags;
789 void *fsdata;
790
791 error = copyinstr(args->filesystemtype, fstypename, MFSNAMELEN - 1,
792 NULL);
793 if (error)
794 return (error);
795 error = copyinstr(args->specialfile, mntfromname, MNAMELEN - 1, NULL);
796 if (error)
797 return (error);
798 error = copyinstr(args->dir, mntonname, MNAMELEN - 1, NULL);
799 if (error)
800 return (error);
801
802 #ifdef DEBUG
803 if (ldebug(mount))
804 printf(ARGS(mount, "%s, %s, %s"),
805 fstypename, mntfromname, mntonname);
806 #endif
807
808 if (strcmp(fstypename, "ext2") == 0) {
809 strcpy(fstypename, "ext2fs");
810 fsdata = &ufs;
811 ufs.fspec = mntfromname;
812 #define DEFAULT_ROOTID -2
813 ufs.export.ex_root = DEFAULT_ROOTID;
814 ufs.export.ex_flags =
815 args->rwflag & LINUX_MS_RDONLY ? MNT_EXRDONLY : 0;
816 } else if (strcmp(fstypename, "proc") == 0) {
817 strcpy(fstypename, "linprocfs");
818 fsdata = NULL;
819 } else {
820 return (ENODEV);
821 }
822
823 fsflags = 0;
824
825 if ((args->rwflag & 0xffff0000) == 0xc0ed0000) {
826 /*
827 * Linux SYNC flag is not included; the closest equivalent
828 * FreeBSD has is !ASYNC, which is our default.
829 */
830 if (args->rwflag & LINUX_MS_RDONLY)
831 fsflags |= MNT_RDONLY;
832 if (args->rwflag & LINUX_MS_NOSUID)
833 fsflags |= MNT_NOSUID;
834 if (args->rwflag & LINUX_MS_NOEXEC)
835 fsflags |= MNT_NOEXEC;
836 if (args->rwflag & LINUX_MS_REMOUNT)
837 fsflags |= MNT_UPDATE;
838 }
839
840 if (strcmp(fstypename, "linprocfs") == 0) {
841 error = kernel_vmount(fsflags,
842 "fstype", fstypename,
843 "fspath", mntonname,
844 NULL);
845 } else
846 error = EOPNOTSUPP;
847 return (error);
848 }
849
850 int
851 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
852 {
853 struct linux_umount_args args2;
854
855 args2.path = args->path;
856 args2.flags = 0;
857 return (linux_umount(td, &args2));
858 }
859
860 int
861 linux_umount(struct thread *td, struct linux_umount_args *args)
862 {
863 struct unmount_args bsd;
864
865 bsd.path = args->path;
866 bsd.flags = args->flags; /* XXX correct? */
867 return (unmount(td, &bsd));
868 }
869
870 /*
871 * fcntl family of syscalls
872 */
873
874 struct l_flock {
875 l_short l_type;
876 l_short l_whence;
877 l_off_t l_start;
878 l_off_t l_len;
879 l_pid_t l_pid;
880 }
881 #if defined(__amd64__) && defined(COMPAT_LINUX32)
882 __packed
883 #endif
884 ;
885
886 static void
887 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
888 {
889 switch (linux_flock->l_type) {
890 case LINUX_F_RDLCK:
891 bsd_flock->l_type = F_RDLCK;
892 break;
893 case LINUX_F_WRLCK:
894 bsd_flock->l_type = F_WRLCK;
895 break;
896 case LINUX_F_UNLCK:
897 bsd_flock->l_type = F_UNLCK;
898 break;
899 default:
900 bsd_flock->l_type = -1;
901 break;
902 }
903 bsd_flock->l_whence = linux_flock->l_whence;
904 bsd_flock->l_start = (off_t)linux_flock->l_start;
905 bsd_flock->l_len = (off_t)linux_flock->l_len;
906 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
907 }
908
909 static void
910 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
911 {
912 switch (bsd_flock->l_type) {
913 case F_RDLCK:
914 linux_flock->l_type = LINUX_F_RDLCK;
915 break;
916 case F_WRLCK:
917 linux_flock->l_type = LINUX_F_WRLCK;
918 break;
919 case F_UNLCK:
920 linux_flock->l_type = LINUX_F_UNLCK;
921 break;
922 }
923 linux_flock->l_whence = bsd_flock->l_whence;
924 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
925 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
926 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
927 }
928
929 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
930 struct l_flock64 {
931 l_short l_type;
932 l_short l_whence;
933 l_loff_t l_start;
934 l_loff_t l_len;
935 l_pid_t l_pid;
936 }
937 #if defined(__amd64__) && defined(COMPAT_LINUX32)
938 __packed
939 #endif
940 ;
941
942 static void
943 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
944 {
945 switch (linux_flock->l_type) {
946 case LINUX_F_RDLCK:
947 bsd_flock->l_type = F_RDLCK;
948 break;
949 case LINUX_F_WRLCK:
950 bsd_flock->l_type = F_WRLCK;
951 break;
952 case LINUX_F_UNLCK:
953 bsd_flock->l_type = F_UNLCK;
954 break;
955 default:
956 bsd_flock->l_type = -1;
957 break;
958 }
959 bsd_flock->l_whence = linux_flock->l_whence;
960 bsd_flock->l_start = (off_t)linux_flock->l_start;
961 bsd_flock->l_len = (off_t)linux_flock->l_len;
962 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
963 }
964
965 static void
966 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
967 {
968 switch (bsd_flock->l_type) {
969 case F_RDLCK:
970 linux_flock->l_type = LINUX_F_RDLCK;
971 break;
972 case F_WRLCK:
973 linux_flock->l_type = LINUX_F_WRLCK;
974 break;
975 case F_UNLCK:
976 linux_flock->l_type = LINUX_F_UNLCK;
977 break;
978 }
979 linux_flock->l_whence = bsd_flock->l_whence;
980 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
981 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
982 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
983 }
984 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
985
986 #if defined(__alpha__)
987 #define linux_fcntl64_args linux_fcntl_args
988 #endif
989
990 static int
991 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
992 {
993 struct l_flock linux_flock;
994 struct flock bsd_flock;
995 struct file *fp;
996 long arg;
997 int error, result;
998
999 switch (args->cmd) {
1000 case LINUX_F_DUPFD:
1001 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
1002
1003 case LINUX_F_GETFD:
1004 return (kern_fcntl(td, args->fd, F_GETFD, 0));
1005
1006 case LINUX_F_SETFD:
1007 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
1008
1009 case LINUX_F_GETFL:
1010 error = kern_fcntl(td, args->fd, F_GETFL, 0);
1011 result = td->td_retval[0];
1012 td->td_retval[0] = 0;
1013 if (result & O_RDONLY)
1014 td->td_retval[0] |= LINUX_O_RDONLY;
1015 if (result & O_WRONLY)
1016 td->td_retval[0] |= LINUX_O_WRONLY;
1017 if (result & O_RDWR)
1018 td->td_retval[0] |= LINUX_O_RDWR;
1019 if (result & O_NDELAY)
1020 td->td_retval[0] |= LINUX_O_NONBLOCK;
1021 if (result & O_APPEND)
1022 td->td_retval[0] |= LINUX_O_APPEND;
1023 if (result & O_FSYNC)
1024 td->td_retval[0] |= LINUX_O_SYNC;
1025 if (result & O_ASYNC)
1026 td->td_retval[0] |= LINUX_FASYNC;
1027 #ifdef LINUX_O_NOFOLLOW
1028 if (result & O_NOFOLLOW)
1029 td->td_retval[0] |= LINUX_O_NOFOLLOW;
1030 #endif
1031 #ifdef LINUX_O_DIRECT
1032 if (result & O_DIRECT)
1033 td->td_retval[0] |= LINUX_O_DIRECT;
1034 #endif
1035 return (error);
1036
1037 case LINUX_F_SETFL:
1038 arg = 0;
1039 if (args->arg & LINUX_O_NDELAY)
1040 arg |= O_NONBLOCK;
1041 if (args->arg & LINUX_O_APPEND)
1042 arg |= O_APPEND;
1043 if (args->arg & LINUX_O_SYNC)
1044 arg |= O_FSYNC;
1045 if (args->arg & LINUX_FASYNC)
1046 arg |= O_ASYNC;
1047 #ifdef LINUX_O_NOFOLLOW
1048 if (args->arg & LINUX_O_NOFOLLOW)
1049 arg |= O_NOFOLLOW;
1050 #endif
1051 #ifdef LINUX_O_DIRECT
1052 if (args->arg & LINUX_O_DIRECT)
1053 arg |= O_DIRECT;
1054 #endif
1055 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1056
1057 case LINUX_F_GETLK:
1058 error = copyin((void *)args->arg, &linux_flock,
1059 sizeof(linux_flock));
1060 if (error)
1061 return (error);
1062 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1063 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1064 if (error)
1065 return (error);
1066 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1067 return (copyout(&linux_flock, (void *)args->arg,
1068 sizeof(linux_flock)));
1069
1070 case LINUX_F_SETLK:
1071 error = copyin((void *)args->arg, &linux_flock,
1072 sizeof(linux_flock));
1073 if (error)
1074 return (error);
1075 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1076 return (kern_fcntl(td, args->fd, F_SETLK,
1077 (intptr_t)&bsd_flock));
1078
1079 case LINUX_F_SETLKW:
1080 error = copyin((void *)args->arg, &linux_flock,
1081 sizeof(linux_flock));
1082 if (error)
1083 return (error);
1084 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1085 return (kern_fcntl(td, args->fd, F_SETLKW,
1086 (intptr_t)&bsd_flock));
1087
1088 case LINUX_F_GETOWN:
1089 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1090
1091 case LINUX_F_SETOWN:
1092 /*
1093 * XXX some Linux applications depend on F_SETOWN having no
1094 * significant effect for pipes (SIGIO is not delivered for
1095 * pipes under Linux-2.2.35 at least).
1096 */
1097 error = fget(td, args->fd, &fp);
1098 if (error)
1099 return (error);
1100 if (fp->f_type == DTYPE_PIPE) {
1101 fdrop(fp, td);
1102 return (EINVAL);
1103 }
1104 fdrop(fp, td);
1105
1106 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1107 }
1108
1109 return (EINVAL);
1110 }
1111
1112 int
1113 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1114 {
1115 struct linux_fcntl64_args args64;
1116
1117 #ifdef DEBUG
1118 if (ldebug(fcntl))
1119 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1120 #endif
1121
1122 args64.fd = args->fd;
1123 args64.cmd = args->cmd;
1124 args64.arg = args->arg;
1125 return (fcntl_common(td, &args64));
1126 }
1127
1128 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1129 int
1130 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1131 {
1132 struct l_flock64 linux_flock;
1133 struct flock bsd_flock;
1134 int error;
1135
1136 #ifdef DEBUG
1137 if (ldebug(fcntl64))
1138 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1139 #endif
1140
1141 switch (args->cmd) {
1142 case LINUX_F_GETLK64:
1143 error = copyin((void *)args->arg, &linux_flock,
1144 sizeof(linux_flock));
1145 if (error)
1146 return (error);
1147 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1148 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1149 if (error)
1150 return (error);
1151 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1152 return (copyout(&linux_flock, (void *)args->arg,
1153 sizeof(linux_flock)));
1154
1155 case LINUX_F_SETLK64:
1156 error = copyin((void *)args->arg, &linux_flock,
1157 sizeof(linux_flock));
1158 if (error)
1159 return (error);
1160 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1161 return (kern_fcntl(td, args->fd, F_SETLK,
1162 (intptr_t)&bsd_flock));
1163
1164 case LINUX_F_SETLKW64:
1165 error = copyin((void *)args->arg, &linux_flock,
1166 sizeof(linux_flock));
1167 if (error)
1168 return (error);
1169 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1170 return (kern_fcntl(td, args->fd, F_SETLKW,
1171 (intptr_t)&bsd_flock));
1172 }
1173
1174 return (fcntl_common(td, args));
1175 }
1176 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1177
1178 int
1179 linux_chown(struct thread *td, struct linux_chown_args *args)
1180 {
1181 char *path;
1182 int error;
1183
1184 LCONVPATHEXIST(td, args->path, &path);
1185
1186 #ifdef DEBUG
1187 if (ldebug(chown))
1188 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1189 #endif
1190 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1191 LFREEPATH(path);
1192 return (error);
1193 }
1194
1195 int
1196 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1197 {
1198 char *path;
1199 int error;
1200
1201 LCONVPATHEXIST(td, args->path, &path);
1202
1203 #ifdef DEBUG
1204 if (ldebug(lchown))
1205 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1206 #endif
1207 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1208 LFREEPATH(path);
1209 return (error);
1210 }
Cache object: fa6e34dad87f59510297a8e12ccd6387
|