linux_llseek.c revision 1.7 1 /* $NetBSD: linux_llseek.c,v 1.7 1995/07/24 06:35:28 fvdl 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(p->p_emul);
121 LINUX_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(p->p_emul);
150
151 fl = linux_to_bsd_ioflags(SCARG(uap, flags));
152
153 if (fl & O_CREAT)
154 LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
155 else
156 LINUX_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, 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 val = linux_to_bsd_ioflags((int)SCARG(uap, arg));
313 SCARG(&fca, fd) = fd;
314 SCARG(&fca, cmd) = F_SETFL;
315 SCARG(&fca, arg) = (caddr_t) val;
316 return fcntl(p, &fca, retval);
317 case LINUX_F_GETLK:
318 sg = stackgap_init(p->p_emul);
319 bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
320 SCARG(&fca, fd) = fd;
321 SCARG(&fca, cmd) = F_GETLK;
322 SCARG(&fca, arg) = bfp;
323 if ((error = fcntl(p, &fca, retval)))
324 return error;
325 if ((error = copyin(bfp, &bfl, sizeof bfl)))
326 return error;
327 bsd_to_linux_flock(&bfl, &lfl);
328 return copyout(&lfl, arg, sizeof lfl);
329 break;
330 case LINUX_F_SETLK:
331 case LINUX_F_SETLKW:
332 cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
333 if ((error = copyin(arg, &lfl, sizeof lfl)))
334 return error;
335 linux_to_bsd_flock(&lfl, &bfl);
336 sg = stackgap_init(p->p_emul);
337 bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
338 if ((error = copyout(&bfl, bfp, sizeof bfl)))
339 return error;
340 SCARG(&fca, fd) = fd;
341 SCARG(&fca, cmd) = cmd;
342 SCARG(&fca, arg) = bfp;
343 return fcntl(p, &fca, retval);
344 break;
345 case LINUX_F_SETOWN:
346 cmd = F_SETOWN;
347 break;
348 case LINUX_F_GETOWN:
349 cmd = F_GETOWN;
350 break;
351 default:
352 return EOPNOTSUPP;
353 }
354
355 SCARG(&fca, fd) = fd;
356 SCARG(&fca, cmd) = cmd;
357 SCARG(&fca, arg) = arg;
358 return fcntl(p, &fca, retval);
359 }
360
361 /*
362 * Convert a NetBSD stat structure to a Linux stat structure.
363 * Only the order of the fields and the padding in the structure
364 * is different.
365 */
366 static void
367 bsd_to_linux_stat(bsp, lsp)
368 struct stat *bsp;
369 struct linux_stat *lsp;
370 {
371 lsp->lst_dev = bsp->st_dev;
372 lsp->lst_ino = bsp->st_ino;
373 lsp->lst_mode = bsp->st_mode;
374 lsp->lst_nlink = bsp->st_nlink;
375 lsp->lst_uid = bsp->st_uid;
376 lsp->lst_gid = bsp->st_gid;
377 lsp->lst_rdev = bsp->st_rdev;
378 lsp->lst_size = bsp->st_size;
379 lsp->lst_blksize = bsp->st_blksize;
380 lsp->lst_blocks = bsp->st_blocks;
381 lsp->lst_atime = bsp->st_atime;
382 lsp->lst_mtime = bsp->st_mtime;
383 lsp->lst_ctime = bsp->st_ctime;
384 }
385
386 /*
387 * The stat functions below are plain sailing. stat and lstat are handled
388 * by one function to avoid code duplication.
389 */
390 int
391 linux_fstat(p, uap, retval)
392 struct proc *p;
393 struct linux_fstat_args /* {
394 syscallarg(int) fd;
395 syscallarg(linux_stat *) sp;
396 } */ *uap;
397 register_t *retval;
398 {
399 struct fstat_args fsa;
400 struct linux_stat tmplst;
401 struct stat *st,tmpst;
402 caddr_t sg;
403 int error;
404
405 sg = stackgap_init(p->p_emul);
406
407 st = stackgap_alloc(&sg, sizeof (struct stat));
408
409 SCARG(&fsa, fd) = SCARG(uap, fd);
410 SCARG(&fsa, sb) = st;
411
412 if ((error = fstat(p, &fsa, retval)))
413 return error;
414
415 if ((error = copyin(st, &tmpst, sizeof tmpst)))
416 return error;
417
418 bsd_to_linux_stat(&tmpst, &tmplst);
419
420 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
421 return error;
422
423 return 0;
424 }
425
426 static int
427 linux_stat1(p, uap, retval, dolstat)
428 struct proc *p;
429 struct linux_stat_args *uap;
430 register_t *retval;
431 int dolstat;
432 {
433 struct stat_args sa;
434 struct linux_stat tmplst;
435 struct stat *st, tmpst;
436 caddr_t sg;
437 int error;
438
439 sg = stackgap_init(p->p_emul);
440
441 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
442
443 st = stackgap_alloc(&sg, sizeof (struct stat));
444 SCARG(&sa, ub) = st;
445 SCARG(&sa, path) = SCARG(uap, path);
446
447 if ((error = (dolstat ? lstat(p, &sa, retval) : stat(p, &sa, retval))))
448 return error;
449
450 if ((error = copyin(st, &tmpst, sizeof tmpst)))
451 return error;
452
453 bsd_to_linux_stat(&tmpst, &tmplst);
454
455 if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
456 return error;
457
458 return 0;
459 }
460
461 int
462 linux_stat(p, uap, retval)
463 struct proc *p;
464 struct linux_stat_args /* {
465 syscallarg(char *) path;
466 syscallarg(struct linux_stat *) sp;
467 } */ *uap;
468 register_t *retval;
469 {
470 return linux_stat1(p, uap, retval, 0);
471 }
472
473 int
474 linux_lstat(p, uap, retval)
475 struct proc *p;
476 struct linux_lstat_args /* {
477 syscallarg(char *) path;
478 syscallarg(struct linux_stat *) sp;
479 } */ *uap;
480 register_t *retval;
481 {
482 return linux_stat1(p, uap, retval, 1);
483 }
484
485 /*
486 * The following syscalls are only here because of the alternate path check.
487 */
488 int
489 linux_access(p, uap, retval)
490 struct proc *p;
491 struct linux_access_args /* {
492 syscallarg(char *) path;
493 syscallarg(int) flags;
494 } */ *uap;
495 register_t *retval;
496 {
497 caddr_t sg = stackgap_init(p->p_emul);
498
499 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
500
501 return access(p, uap, retval);
502 }
503
504 int
505 linux_unlink(p, uap, retval)
506 struct proc *p;
507 struct linux_unlink_args /* {
508 syscallarg(char *) path;
509 } */ *uap;
510 register_t *retval;
511
512 {
513 caddr_t sg = stackgap_init(p->p_emul);
514
515 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
516
517 return unlink(p, uap, retval);
518 }
519
520 int linux_chdir(p, uap, retval)
521 struct proc *p;
522 struct linux_chdir_args /* {
523 syscallarg(char *) path;
524 } */ *uap;
525 register_t *retval;
526 {
527 caddr_t sg = stackgap_init(p->p_emul);
528
529 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
530
531 return chdir(p, uap, retval);
532 }
533
534 int
535 linux_mknod(p, uap, retval)
536 struct proc *p;
537 struct linux_mknod_args /* {
538 syscallarg(char *) path;
539 syscallarg(int) mode;
540 syscallarg(int) dev;
541 } */ *uap;
542 register_t *retval;
543 {
544 caddr_t sg = stackgap_init(p->p_emul);
545
546 LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
547
548 return mknod(p, uap, retval);
549 }
550
551 int
552 linux_chmod(p, uap, retval)
553 struct proc *p;
554 struct linux_chmod_args /* {
555 syscallarg(char *) path;
556 syscallarg(int) mode;
557 } */ *uap;
558 register_t *retval;
559 {
560 caddr_t sg = stackgap_init(p->p_emul);
561
562 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
563
564 return chmod(p, uap, retval);
565 }
566
567 int
568 linux_chown(p, uap, retval)
569 struct proc *p;
570 struct linux_chown_args /* {
571 syscallarg(char *) path;
572 syscallarg(int) uid;
573 syscallarg(int) gid;
574 } */ *uap;
575 register_t *retval;
576 {
577 caddr_t sg = stackgap_init(p->p_emul);
578
579 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
580
581 return chmod(p, uap, retval);
582 }
583
584 int
585 linux_rename(p, uap, retval)
586 struct proc *p;
587 struct linux_rename_args /* {
588 syscallarg(char *) from;
589 syscallarg(char *) to;
590 } */ *uap;
591 register_t *retval;
592 {
593 caddr_t sg = stackgap_init(p->p_emul);
594
595 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
596 LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
597
598 return rename(p, uap, retval);
599 }
600
601 int
602 linux_mkdir(p, uap, retval)
603 struct proc *p;
604 struct linux_mkdir_args /* {
605 syscallarg(char *) path;
606 syscallarg(int) mode;
607 } */ *uap;
608 register_t *retval;
609 {
610 caddr_t sg = stackgap_init(p->p_emul);
611
612 LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
613 return mkdir(p, uap, retval);
614 }
615
616 int
617 linux_rmdir(p, uap, retval)
618 struct proc *p;
619 struct linux_rmdir_args /* {
620 syscallarg(char *) path;
621 } */ *uap;
622 register_t *retval;
623 {
624 caddr_t sg = stackgap_init(p->p_emul);
625
626 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
627 return rmdir(p, uap, retval);
628 }
629
630 int
631 linux_symlink(p, uap, retval)
632 struct proc *p;
633 struct linux_symlink_args /* {
634 syscallarg(char *) path;
635 syscallarg(char *) to;
636 } */ *uap;
637 register_t *retval;
638 {
639 caddr_t sg = stackgap_init(p->p_emul);
640
641 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
642 LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
643
644 return symlink(p, uap, retval);
645 }
646
647 int
648 linux_readlink(p, uap, retval)
649 struct proc *p;
650 struct linux_readlink_args /* {
651 syscallarg(char *) name;
652 syscallarg(char *) buf;
653 syscallarg(int) count;
654 } */ *uap;
655 {
656 caddr_t sg = stackgap_init(p->p_emul);
657
658 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, name));
659 return readlink(p, uap, retval);
660 }
661
662 int
663 linux_truncate(p, uap, retval)
664 struct proc *p;
665 struct linux_truncate_args /* {
666 syscallarg(char *) path;
667 syscallarg(long) length;
668 } */ *uap;
669 {
670 caddr_t sg = stackgap_init(p->p_emul);
671
672 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
673 return compat_43_truncate(p, uap, retval);
674 }
675