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