linux_llseek.c revision 1.3 1 /* $NetBSD: linux_llseek.c,v 1.3 1995/04/04 04:21:30 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1995 Frank van der Linden
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Frank van der Linden
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/proc.h>
38 #include <sys/file.h>
39 #include <sys/stat.h>
40 #include <sys/filedesc.h>
41 #include <sys/ioctl.h>
42 #include <sys/kernel.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45
46 #include <sys/syscallargs.h>
47
48 #include <compat/linux/linux_types.h>
49 #include <compat/linux/linux_syscallargs.h>
50 #include <compat/linux/linux_fcntl.h>
51 #include <compat/linux/linux_util.h>
52
53 /*
54 * Some file-related calls are handled here. The usual flag conversion
55 * an structure conversion is done, and alternate emul path searching.
56 */
57
58 /*
59 * The next two functions convert between the Linux and NetBSD values
60 * of the flags used in open(2) and fcntl(2).
61 */
62 static int
63 linux_to_bsd_ioflags(int lflags)
64 {
65 int res = 0;
66
67 res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
68 res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
69 res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
70 res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
71 res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
72 res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
73 res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
74 res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
75 res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
76 res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
77 res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
78
79 return res;
80 }
81
82 static int
83 bsd_to_linux_ioflags(int bflags)
84 {
85 int res = 0;
86
87 res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
88 res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
89 res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
90 res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
91 res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
92 res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
93 res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
94 res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
95 res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
96 res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
97 res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
98
99 return res;
100 }
101
102 /*
103 * creat(2) is an obsolete function, but it's present as a Linux
104 * system call, so let's deal with it.
105 *
106 * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
107 */
108 int
109 linux_creat(p, uap, retval)
110 struct proc *p;
111 struct linux_creat_args /* {
112 syscallarg(char *) path;
113 syscallarg(int) mode;
114 } */ *uap;
115 register_t *retval;
116 {
117 struct open_args oa;
118 caddr_t sg;
119
120 sg = stackgap_init();
121 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
122
123 SCARG(&oa, path) = SCARG(uap, path);
124 SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
125 SCARG(&oa, mode) = SCARG(uap, mode);
126 return open(p, &oa, retval);
127 }
128
129 /*
130 * open(2). Take care of the different flag values, and let the
131 * NetBSD syscall do the real work. See if this operation
132 * gives the current process a controlling terminal.
133 * (XXX is this necessary?)
134 */
135 int
136 linux_open(p, uap, retval)
137 struct proc *p;
138 struct linux_open_args /* {
139 syscallarg(char *) path;
140 syscallarg(int) flags;
141 syscallarg(int) mode;
142 } */ *uap;
143 register_t *retval;
144 {
145 int error, fl;
146 struct open_args boa;
147 caddr_t sg;
148
149 sg = stackgap_init();
150
151 fl = linux_to_bsd_ioflags(SCARG(uap, flags));
152
153 if (fl & O_CREAT)
154 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
155 else
156 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
157
158 SCARG(&boa, path) = SCARG(uap, path);
159 SCARG(&boa, flags) = fl;
160 SCARG(&boa, mode) = SCARG(uap, mode);
161
162 if ((error = open(p, &boa, retval)))
163 return error;
164
165 /*
166 * this bit from sunos_misc.c (and svr4_fcntl.c).
167 * If we are a session leader, and we don't have a controlling
168 * terminal yet, and the O_NOCTTY flag is not set, try to make
169 * this the controlling terminal.
170 */
171 if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
172 struct filedesc *fdp = p->p_fd;
173 struct file *fp = fdp->fd_ofiles[*retval];
174
175 /* ignore any error, just give it a try */
176 if (fp->f_type == DTYPE_VNODE)
177 (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, (caddr_t) 0, p);
178 }
179 return 0;
180 }
181
182 /*
183 * This appears to be part of a Linux attempt to switch to 64 bits file sizes.
184 */
185 int
186 linux_llseek(p, uap, retval)
187 struct proc *p;
188 struct linux_llseek_args /* {
189 syscallarg(int) fd;
190 syscallarg(uint32_t) ohigh;
191 syscallarg(uint32_t) olow;
192 syscallarg(caddr_t) res;
193 syscallarg(int) whence;
194 } */ *uap;
195 register_t *retval;
196 {
197 struct lseek_args bla;
198 int error;
199 off_t off;
200
201 off = SCARG(uap, olow) | (((off_t) SCARG(uap, ohigh)) << 32);
202
203 SCARG(&bla, fd) = SCARG(uap, fd);
204 SCARG(&bla, offset) = off;
205 SCARG(&bla, whence) = SCARG(uap, whence);
206
207 if ((error = lseek(p, &bla, retval)))
208 return error;
209
210 if ((error = copyout(retval, SCARG(uap, res), sizeof (off_t))))
211 return error;
212
213 retval[0] = 0;
214 return 0;
215 }
216
217 /*
218 * The next two functions take care of converting the flock
219 * structure back and forth between Linux and NetBSD format.
220 * The only difference in the structures is the order of
221 * the fields, and the 'whence' value.
222 */
223 static void
224 bsd_to_linux_flock(bfp, lfp)
225 struct flock *bfp;
226 struct linux_flock *lfp;
227 {
228 lfp->l_start = bfp->l_start;
229 lfp->l_len = bfp->l_len;
230 lfp->l_pid = bfp->l_pid;
231 lfp->l_whence = bfp->l_whence;
232 switch (bfp->l_type) {
233 case F_RDLCK:
234 lfp->l_type = LINUX_F_RDLCK;
235 break;
236 case F_UNLCK:
237 lfp->l_type = LINUX_F_UNLCK;
238 break;
239 case F_WRLCK:
240 lfp->l_type = LINUX_F_WRLCK;
241 break;
242 }
243 }
244
245 static void
246 linux_to_bsd_flock(lfp, bfp)
247 struct linux_flock *lfp;
248 struct flock *bfp;
249 {
250 bfp->l_start = lfp->l_start;
251 bfp->l_len = lfp->l_len;
252 bfp->l_pid = lfp->l_pid;
253 bfp->l_whence = lfp->l_whence;
254 switch (lfp->l_type) {
255 case LINUX_F_RDLCK:
256 bfp->l_type = F_RDLCK;
257 break;
258 case LINUX_F_UNLCK:
259 bfp->l_type = F_UNLCK;
260 break;
261 case LINUX_F_WRLCK:
262 bfp->l_type = F_WRLCK;
263 break;
264 }
265 }
266
267 /*
268 * Most actions in the fcntl() call are straightforward; simply
269 * pass control to the NetBSD system call. A few commands need
270 * conversions after the actual system call has done its work,
271 * because the flag values and lock structure are different.
272 */
273 int
274 linux_fcntl(p, uap, retval)
275 struct proc *p;
276 struct linux_fcntl_args /* {
277 syscallarg(int) fd;
278 syscallarg(int) cmd;
279 syscallarg(void *) arg;
280 } */ *uap;
281 register_t *retval;
282 {
283 int fd, cmd, error, *tval,val;
284 caddr_t arg, sg;
285 struct linux_flock lfl;
286 struct flock *bfp, bfl;
287 struct fcntl_args fca;
288
289 fd = SCARG(uap, fd);
290 cmd = SCARG(uap, cmd);
291 arg = (caddr_t) SCARG(uap, arg);
292
293 switch (cmd) {
294 case LINUX_F_DUPFD:
295 cmd = F_DUPFD;
296 break;
297 case LINUX_F_GETFD:
298 cmd = F_GETFD;
299 break;
300 case LINUX_F_SETFD:
301 cmd = F_SETFD;
302 break;
303 case LINUX_F_GETFL:
304 SCARG(&fca, fd) = fd;
305 SCARG(&fca, cmd) = F_GETFL;
306 SCARG(&fca, arg) = arg;
307 if ((error = fcntl(p, &fca, retval)))
308 return error;
309 retval[0] = bsd_to_linux_ioflags(retval[0]);
310 return 0;
311 case LINUX_F_SETFL:
312 if ((error = copyin(SCARG(uap, arg), &val, sizeof (int))))
313 return error;
314 val = linux_to_bsd_ioflags(val);
315 sg = stackgap_init();
316 tval = (int *) stackgap_alloc(&sg, sizeof (int));
317 if ((error = copyout(&val, tval, sizeof (int))))
318 return error;
319 SCARG(&fca, fd) = fd;
320 SCARG(&fca, cmd) = F_SETFL;
321 SCARG(&fca, arg) = tval;
322 return fcntl(p, &fca, retval);
323 case LINUX_F_GETLK:
324 sg = stackgap_init();
325 bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
326 SCARG(&fca, fd) = fd;
327 SCARG(&fca, cmd) = F_GETLK;
328 SCARG(&fca, arg) = bfp;
329 if ((error = fcntl(p, &fca, retval)))
330 return error;
331 if ((error = copyin(bfp, &bfl, sizeof bfl)))
332 return error;
333 bsd_to_linux_flock(&bfl, &lfl);
334 return copyout(&lfl, arg, sizeof lfl);
335 break;
336 case LINUX_F_SETLK:
337 case LINUX_F_SETLKW:
338 cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
339 if ((error = copyin(arg, &lfl, sizeof lfl)))
340 return error;
341 linux_to_bsd_flock(&lfl, &bfl);
342 sg = stackgap_init();
343 bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
344 if ((error = copyout(&bfl, bfp, sizeof bfl)))
345 return error;
346 SCARG(&fca, fd) = fd;
347 SCARG(&fca, cmd) = cmd;
348 SCARG(&fca, arg) = bfp;
349 return fcntl(p, &fca, retval);
350 break;
351 case LINUX_F_SETOWN:
352 cmd = F_SETOWN;
353 break;
354 case LINUX_F_GETOWN:
355 cmd = F_GETOWN;
356 break;
357 default:
358 return EOPNOTSUPP;
359 }
360
361 SCARG(&fca, fd) = fd;
362 SCARG(&fca, cmd) = cmd;
363 SCARG(&fca, arg) = arg;
364 return fcntl(p, uap, retval);
365 }
366
367 /*
368 * Convert a NetBSD stat structure to a Linux stat structure.
369 * Only the order of the fields and the padding in the structure
370 * is different.
371 */
372 static void
373 bsd_to_linux_stat(bsp, lsp)
374 struct stat *bsp;
375 struct linux_stat *lsp;
376 {
377 lsp->lst_dev = bsp->st_dev;
378 lsp->lst_ino = bsp->st_ino;
379 lsp->lst_mode = bsp->st_mode;
380 lsp->lst_nlink = bsp->st_nlink;
381 lsp->lst_uid = bsp->st_uid;
382 lsp->lst_gid = bsp->st_gid;
383 lsp->lst_rdev = bsp->st_rdev;
384 lsp->lst_size = bsp->st_size;
385 lsp->lst_blksize = bsp->st_blksize;
386 lsp->lst_blocks = bsp->st_blocks;
387 lsp->lst_atime = bsp->st_atime;
388 lsp->lst_mtime = bsp->st_mtime;
389 lsp->lst_ctime = bsp->st_ctime;
390 }
391
392 /*
393 * The stat functions below are plain sailing. stat and lstat are handled
394 * by one function to avoid code duplication.
395 */
396 int
397 linux_fstat(p, uap, retval)
398 struct proc *p;
399 struct linux_fstat_args /* {
400 syscallarg(int) fd;
401 syscallarg(linux_stat *) sp;
402 } */ *uap;
403 register_t *retval;
404 {
405 struct fstat_args fsa;
406 struct linux_stat tmplst;
407 struct stat *st,tmpst;
408 caddr_t sg;
409 int error;
410
411 sg = stackgap_init();
412
413 st = stackgap_alloc(&sg, sizeof (struct stat));
414
415 SCARG(&fsa, fd) = SCARG(uap, fd);
416 SCARG(&fsa, sb) = st;
417
418 if ((error = fstat(p, &fsa, retval)))
419 return error;
420
421 if ((error = copyin(st, &tmpst, sizeof tmpst)))
422 return error;
423
424 bsd_to_linux_stat(&tmpst, &tmplst);
425
426 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
427 return error;
428
429 return 0;
430 }
431
432 static int
433 linux_stat1(p, uap, retval, dolstat)
434 struct proc *p;
435 struct linux_stat_args *uap;
436 register_t *retval;
437 int dolstat;
438 {
439 struct stat_args sa;
440 struct linux_stat tmplst;
441 struct stat *st, tmpst;
442 caddr_t sg;
443 int error;
444
445 sg = stackgap_init();
446
447 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
448
449 st = stackgap_alloc(&sg, sizeof (struct stat));
450 SCARG(&sa, ub) = st;
451 SCARG(&sa, path) = SCARG(uap, path);
452
453 if ((error = (dolstat ? lstat(p, &sa, retval) : stat(p, &sa, retval))))
454 return error;
455
456 if ((error = copyin(st, &tmpst, sizeof tmpst)))
457 return error;
458
459 bsd_to_linux_stat(&tmpst, &tmplst);
460
461 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
462 return error;
463
464 return 0;
465 }
466
467 int
468 linux_stat(p, uap, retval)
469 struct proc *p;
470 struct linux_stat_args /* {
471 syscallarg(char *) path;
472 syscallarg(struct linux_stat *) sp;
473 } */ *uap;
474 register_t *retval;
475 {
476 return linux_stat1(p, uap, retval, 0);
477 }
478
479 int
480 linux_lstat(p, uap, retval)
481 struct proc *p;
482 struct linux_lstat_args /* {
483 syscallarg(char *) path;
484 syscallarg(struct linux_stat *) sp;
485 } */ *uap;
486 register_t *retval;
487 {
488 return linux_stat1(p, uap, retval, 1);
489 }
490
491 /*
492 * The following syscalls are only here because of the alternate path check.
493 */
494 int
495 linux_access(p, uap, retval)
496 struct proc *p;
497 struct linux_access_args /* {
498 syscallarg(char *) path;
499 syscallarg(int) flags;
500 } */ *uap;
501 register_t *retval;
502 {
503 caddr_t sg = stackgap_init();
504
505 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
506
507 return access(p, uap, retval);
508 }
509
510 int
511 linux_unlink(p, uap, retval)
512 struct proc *p;
513 struct linux_unlink_args /* {
514 syscallarg(char *) path;
515 } */ *uap;
516 register_t *retval;
517
518 {
519 caddr_t sg = stackgap_init();
520
521 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
522
523 return unlink(p, uap, retval);
524 }
525
526 int linux_chdir(p, uap, retval)
527 struct proc *p;
528 struct linux_chdir_args /* {
529 syscallarg(char *) path;
530 } */ *uap;
531 register_t *retval;
532 {
533 caddr_t sg = stackgap_init();
534
535 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
536
537 return chdir(p, uap, retval);
538 }
539
540 int
541 linux_mknod(p, uap, retval)
542 struct proc *p;
543 struct linux_mknod_args /* {
544 syscallarg(char *) path;
545 syscallarg(int) mode;
546 syscallarg(int) dev;
547 } */ *uap;
548 register_t *retval;
549 {
550 caddr_t sg = stackgap_init();
551
552 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
553
554 return mknod(p, uap, retval);
555 }
556
557 int
558 linux_chmod(p, uap, retval)
559 struct proc *p;
560 struct linux_chmod_args /* {
561 syscallarg(char *) path;
562 syscallarg(int) mode;
563 } */ *uap;
564 register_t *retval;
565 {
566 caddr_t sg = stackgap_init();
567
568 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
569
570 return chmod(p, uap, retval);
571 }
572
573 int
574 linux_chown(p, uap, retval)
575 struct proc *p;
576 struct linux_chown_args /* {
577 syscallarg(char *) path;
578 syscallarg(int) uid;
579 syscallarg(int) gid;
580 } */ *uap;
581 register_t *retval;
582 {
583 caddr_t sg = stackgap_init();
584
585 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
586
587 return chmod(p, uap, retval);
588 }
589
590 int
591 linux_rename(p, uap, retval)
592 struct proc *p;
593 struct linux_rename_args /* {
594 syscallarg(char *) from;
595 syscallarg(char *) to;
596 } */ *uap;
597 register_t *retval;
598 {
599 caddr_t sg = stackgap_init();
600
601 CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
602 CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
603
604 return rename(p, uap, retval);
605 }
606
607 int
608 linux_mkdir(p, uap, retval)
609 struct proc *p;
610 struct linux_mkdir_args /* {
611 syscallarg(char *) path;
612 } */ *uap;
613 register_t *retval;
614 {
615 caddr_t sg = stackgap_init();
616
617 CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
618 return mkdir(p, uap, retval);
619 }
620
621 int
622 linux_rmdir(p, uap, retval)
623 struct proc *p;
624 struct linux_rmdir_args /* {
625 syscallarg(char *) path;
626 } */ *uap;
627 register_t *retval;
628 {
629 caddr_t sg = stackgap_init();
630
631 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
632 return rmdir(p, uap, retval);
633 }
634
635 int
636 linux_symlink(p, uap, retval)
637 struct proc *p;
638 struct linux_symlink_args /* {
639 syscallarg(char *) path;
640 syscallarg(char *) to;
641 } */ *uap;
642 register_t *retval;
643 {
644 caddr_t sg = stackgap_init();
645
646 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
647 CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
648
649 return symlink(p, uap, retval);
650 }
651
652 int
653 linux_readlink(p, uap, retval)
654 struct proc *p;
655 struct linux_readlink_args /* {
656 syscallarg(char *) name;
657 syscallarg(char *) buf;
658 syscallarg(int) count;
659 } */ *uap;
660 {
661 caddr_t sg = stackgap_init();
662
663 CHECK_ALT_EXIST(p, &sg, SCARG(uap, name));
664 return readlink(p, uap, retval);
665 }
666
667 int
668 linux_truncate(p, uap, retval)
669 struct proc *p;
670 struct linux_truncate_args /* {
671 syscallarg(char *) path;
672 syscallarg(long) length;
673 } */ *uap;
674 {
675 caddr_t sg = stackgap_init();
676
677 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
678 return compat_43_truncate(p, uap, retval);
679 }
680