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.1/sys/compat/linux/linux_file.c 144987 2005-04-13 04:31:43Z mdodd $");
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_NOEXEC)
790 fsflags |= MNT_NOEXEC;
791 if (args->rwflag & LINUX_MS_REMOUNT)
792 fsflags |= MNT_UPDATE;
793 }
794
795 if (strcmp(fstypename, "linprocfs") == 0) {
796 error = kernel_vmount(fsflags,
797 "fstype", fstypename,
798 "fspath", mntonname,
799 NULL);
800 } else
801 error = EOPNOTSUPP;
802 return (error);
803 }
804
805 int
806 linux_oldumount(struct thread *td, struct linux_oldumount_args *args)
807 {
808 struct linux_umount_args args2;
809
810 args2.path = args->path;
811 args2.flags = 0;
812 return (linux_umount(td, &args2));
813 }
814
815 int
816 linux_umount(struct thread *td, struct linux_umount_args *args)
817 {
818 struct unmount_args bsd;
819
820 bsd.path = args->path;
821 bsd.flags = args->flags; /* XXX correct? */
822 return (unmount(td, &bsd));
823 }
824
825 /*
826 * fcntl family of syscalls
827 */
828
829 struct l_flock {
830 l_short l_type;
831 l_short l_whence;
832 l_off_t l_start;
833 l_off_t l_len;
834 l_pid_t l_pid;
835 }
836 #if defined(__amd64__) && defined(COMPAT_LINUX32)
837 __packed
838 #endif
839 ;
840
841 static void
842 linux_to_bsd_flock(struct l_flock *linux_flock, struct flock *bsd_flock)
843 {
844 switch (linux_flock->l_type) {
845 case LINUX_F_RDLCK:
846 bsd_flock->l_type = F_RDLCK;
847 break;
848 case LINUX_F_WRLCK:
849 bsd_flock->l_type = F_WRLCK;
850 break;
851 case LINUX_F_UNLCK:
852 bsd_flock->l_type = F_UNLCK;
853 break;
854 default:
855 bsd_flock->l_type = -1;
856 break;
857 }
858 bsd_flock->l_whence = linux_flock->l_whence;
859 bsd_flock->l_start = (off_t)linux_flock->l_start;
860 bsd_flock->l_len = (off_t)linux_flock->l_len;
861 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
862 }
863
864 static void
865 bsd_to_linux_flock(struct flock *bsd_flock, struct l_flock *linux_flock)
866 {
867 switch (bsd_flock->l_type) {
868 case F_RDLCK:
869 linux_flock->l_type = LINUX_F_RDLCK;
870 break;
871 case F_WRLCK:
872 linux_flock->l_type = LINUX_F_WRLCK;
873 break;
874 case F_UNLCK:
875 linux_flock->l_type = LINUX_F_UNLCK;
876 break;
877 }
878 linux_flock->l_whence = bsd_flock->l_whence;
879 linux_flock->l_start = (l_off_t)bsd_flock->l_start;
880 linux_flock->l_len = (l_off_t)bsd_flock->l_len;
881 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
882 }
883
884 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
885 struct l_flock64 {
886 l_short l_type;
887 l_short l_whence;
888 l_loff_t l_start;
889 l_loff_t l_len;
890 l_pid_t l_pid;
891 }
892 #if defined(__amd64__) && defined(COMPAT_LINUX32)
893 __packed
894 #endif
895 ;
896
897 static void
898 linux_to_bsd_flock64(struct l_flock64 *linux_flock, struct flock *bsd_flock)
899 {
900 switch (linux_flock->l_type) {
901 case LINUX_F_RDLCK:
902 bsd_flock->l_type = F_RDLCK;
903 break;
904 case LINUX_F_WRLCK:
905 bsd_flock->l_type = F_WRLCK;
906 break;
907 case LINUX_F_UNLCK:
908 bsd_flock->l_type = F_UNLCK;
909 break;
910 default:
911 bsd_flock->l_type = -1;
912 break;
913 }
914 bsd_flock->l_whence = linux_flock->l_whence;
915 bsd_flock->l_start = (off_t)linux_flock->l_start;
916 bsd_flock->l_len = (off_t)linux_flock->l_len;
917 bsd_flock->l_pid = (pid_t)linux_flock->l_pid;
918 }
919
920 static void
921 bsd_to_linux_flock64(struct flock *bsd_flock, struct l_flock64 *linux_flock)
922 {
923 switch (bsd_flock->l_type) {
924 case F_RDLCK:
925 linux_flock->l_type = LINUX_F_RDLCK;
926 break;
927 case F_WRLCK:
928 linux_flock->l_type = LINUX_F_WRLCK;
929 break;
930 case F_UNLCK:
931 linux_flock->l_type = LINUX_F_UNLCK;
932 break;
933 }
934 linux_flock->l_whence = bsd_flock->l_whence;
935 linux_flock->l_start = (l_loff_t)bsd_flock->l_start;
936 linux_flock->l_len = (l_loff_t)bsd_flock->l_len;
937 linux_flock->l_pid = (l_pid_t)bsd_flock->l_pid;
938 }
939 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
940
941 #if defined(__alpha__)
942 #define linux_fcntl64_args linux_fcntl_args
943 #endif
944
945 static int
946 fcntl_common(struct thread *td, struct linux_fcntl64_args *args)
947 {
948 struct l_flock linux_flock;
949 struct flock bsd_flock;
950 struct file *fp;
951 long arg;
952 int error, result;
953
954 switch (args->cmd) {
955 case LINUX_F_DUPFD:
956 return (kern_fcntl(td, args->fd, F_DUPFD, args->arg));
957
958 case LINUX_F_GETFD:
959 return (kern_fcntl(td, args->fd, F_GETFD, 0));
960
961 case LINUX_F_SETFD:
962 return (kern_fcntl(td, args->fd, F_SETFD, args->arg));
963
964 case LINUX_F_GETFL:
965 error = kern_fcntl(td, args->fd, F_GETFL, 0);
966 result = td->td_retval[0];
967 td->td_retval[0] = 0;
968 if (result & O_RDONLY)
969 td->td_retval[0] |= LINUX_O_RDONLY;
970 if (result & O_WRONLY)
971 td->td_retval[0] |= LINUX_O_WRONLY;
972 if (result & O_RDWR)
973 td->td_retval[0] |= LINUX_O_RDWR;
974 if (result & O_NDELAY)
975 td->td_retval[0] |= LINUX_O_NONBLOCK;
976 if (result & O_APPEND)
977 td->td_retval[0] |= LINUX_O_APPEND;
978 if (result & O_FSYNC)
979 td->td_retval[0] |= LINUX_O_SYNC;
980 if (result & O_ASYNC)
981 td->td_retval[0] |= LINUX_FASYNC;
982 #ifdef LINUX_O_NOFOLLOW
983 if (result & O_NOFOLLOW)
984 td->td_retval[0] |= LINUX_O_NOFOLLOW;
985 #endif
986 #ifdef LINUX_O_DIRECT
987 if (result & O_DIRECT)
988 td->td_retval[0] |= LINUX_O_DIRECT;
989 #endif
990 return (error);
991
992 case LINUX_F_SETFL:
993 arg = 0;
994 if (args->arg & LINUX_O_NDELAY)
995 arg |= O_NONBLOCK;
996 if (args->arg & LINUX_O_APPEND)
997 arg |= O_APPEND;
998 if (args->arg & LINUX_O_SYNC)
999 arg |= O_FSYNC;
1000 if (args->arg & LINUX_FASYNC)
1001 arg |= O_ASYNC;
1002 #ifdef LINUX_O_NOFOLLOW
1003 if (args->arg & LINUX_O_NOFOLLOW)
1004 arg |= O_NOFOLLOW;
1005 #endif
1006 #ifdef LINUX_O_DIRECT
1007 if (args->arg & LINUX_O_DIRECT)
1008 arg |= O_DIRECT;
1009 #endif
1010 return (kern_fcntl(td, args->fd, F_SETFL, arg));
1011
1012 case LINUX_F_GETLK:
1013 error = copyin((void *)args->arg, &linux_flock,
1014 sizeof(linux_flock));
1015 if (error)
1016 return (error);
1017 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1018 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1019 if (error)
1020 return (error);
1021 bsd_to_linux_flock(&bsd_flock, &linux_flock);
1022 return (copyout(&linux_flock, (void *)args->arg,
1023 sizeof(linux_flock)));
1024
1025 case LINUX_F_SETLK:
1026 error = copyin((void *)args->arg, &linux_flock,
1027 sizeof(linux_flock));
1028 if (error)
1029 return (error);
1030 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1031 return (kern_fcntl(td, args->fd, F_SETLK,
1032 (intptr_t)&bsd_flock));
1033
1034 case LINUX_F_SETLKW:
1035 error = copyin((void *)args->arg, &linux_flock,
1036 sizeof(linux_flock));
1037 if (error)
1038 return (error);
1039 linux_to_bsd_flock(&linux_flock, &bsd_flock);
1040 return (kern_fcntl(td, args->fd, F_SETLKW,
1041 (intptr_t)&bsd_flock));
1042
1043 case LINUX_F_GETOWN:
1044 return (kern_fcntl(td, args->fd, F_GETOWN, 0));
1045
1046 case LINUX_F_SETOWN:
1047 /*
1048 * XXX some Linux applications depend on F_SETOWN having no
1049 * significant effect for pipes (SIGIO is not delivered for
1050 * pipes under Linux-2.2.35 at least).
1051 */
1052 error = fget(td, args->fd, &fp);
1053 if (error)
1054 return (error);
1055 if (fp->f_type == DTYPE_PIPE) {
1056 fdrop(fp, td);
1057 return (EINVAL);
1058 }
1059 fdrop(fp, td);
1060
1061 return (kern_fcntl(td, args->fd, F_SETOWN, args->arg));
1062 }
1063
1064 return (EINVAL);
1065 }
1066
1067 int
1068 linux_fcntl(struct thread *td, struct linux_fcntl_args *args)
1069 {
1070 struct linux_fcntl64_args args64;
1071
1072 #ifdef DEBUG
1073 if (ldebug(fcntl))
1074 printf(ARGS(fcntl, "%d, %08x, *"), args->fd, args->cmd);
1075 #endif
1076
1077 args64.fd = args->fd;
1078 args64.cmd = args->cmd;
1079 args64.arg = args->arg;
1080 return (fcntl_common(td, &args64));
1081 }
1082
1083 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1084 int
1085 linux_fcntl64(struct thread *td, struct linux_fcntl64_args *args)
1086 {
1087 struct l_flock64 linux_flock;
1088 struct flock bsd_flock;
1089 int error;
1090
1091 #ifdef DEBUG
1092 if (ldebug(fcntl64))
1093 printf(ARGS(fcntl64, "%d, %08x, *"), args->fd, args->cmd);
1094 #endif
1095
1096 switch (args->cmd) {
1097 case LINUX_F_GETLK64:
1098 error = copyin((void *)args->arg, &linux_flock,
1099 sizeof(linux_flock));
1100 if (error)
1101 return (error);
1102 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1103 error = kern_fcntl(td, args->fd, F_GETLK, (intptr_t)&bsd_flock);
1104 if (error)
1105 return (error);
1106 bsd_to_linux_flock64(&bsd_flock, &linux_flock);
1107 return (copyout(&linux_flock, (void *)args->arg,
1108 sizeof(linux_flock)));
1109
1110 case LINUX_F_SETLK64:
1111 error = copyin((void *)args->arg, &linux_flock,
1112 sizeof(linux_flock));
1113 if (error)
1114 return (error);
1115 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1116 return (kern_fcntl(td, args->fd, F_SETLK,
1117 (intptr_t)&bsd_flock));
1118
1119 case LINUX_F_SETLKW64:
1120 error = copyin((void *)args->arg, &linux_flock,
1121 sizeof(linux_flock));
1122 if (error)
1123 return (error);
1124 linux_to_bsd_flock64(&linux_flock, &bsd_flock);
1125 return (kern_fcntl(td, args->fd, F_SETLKW,
1126 (intptr_t)&bsd_flock));
1127 }
1128
1129 return (fcntl_common(td, args));
1130 }
1131 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1132
1133 int
1134 linux_chown(struct thread *td, struct linux_chown_args *args)
1135 {
1136 char *path;
1137 int error;
1138
1139 LCONVPATHEXIST(td, args->path, &path);
1140
1141 #ifdef DEBUG
1142 if (ldebug(chown))
1143 printf(ARGS(chown, "%s, %d, %d"), path, args->uid, args->gid);
1144 #endif
1145 error = kern_chown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1146 LFREEPATH(path);
1147 return (error);
1148 }
1149
1150 int
1151 linux_lchown(struct thread *td, struct linux_lchown_args *args)
1152 {
1153 char *path;
1154 int error;
1155
1156 LCONVPATHEXIST(td, args->path, &path);
1157
1158 #ifdef DEBUG
1159 if (ldebug(lchown))
1160 printf(ARGS(lchown, "%s, %d, %d"), path, args->uid, args->gid);
1161 #endif
1162 error = kern_lchown(td, path, UIO_SYSSPACE, args->uid, args->gid);
1163 LFREEPATH(path);
1164 return (error);
1165 }
Cache object: 3c7f5d25863833ab657f656ab3bd9710
|