Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / arch / sparc / kernel / sys_sunos.c
1 /* $Id: sys_sunos.c,v 1.137 2002/02/08 03:57:14 davem Exp $
2  * sys_sunos.c: SunOS specific syscall compatibility support.
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
6  *
7  * Based upon preliminary work which is:
8  *
9  * Copyright (C) 1995 Adrian M. Rodriguez (adrian@remus.rutgers.edu)
10  *
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/types.h>
16 #include <linux/mman.h>
17 #include <linux/mm.h>
18 #include <linux/swap.h>
19 #include <linux/fs.h>
20 #include <linux/file.h>
21 #include <linux/resource.h>
22 #include <linux/ipc.h>
23 #include <linux/shm.h>
24 #include <linux/msg.h>
25 #include <linux/sem.h>
26 #include <linux/signal.h>
27 #include <linux/uio.h>
28 #include <linux/utsname.h>
29 #include <linux/major.h>
30 #include <linux/stat.h>
31 #include <linux/slab.h>
32 #include <linux/pagemap.h>
33 #include <linux/capability.h>
34 #include <linux/errno.h>
35 #include <linux/smp.h>
36 #include <linux/smp_lock.h>
37 #include <linux/syscalls.h>
38 #include <linux/vs_cvirt.h>
39
40 #include <net/sock.h>
41
42 #include <asm/uaccess.h>
43 #ifndef KERNEL_DS
44 #include <linux/segment.h>
45 #endif
46
47 #include <asm/page.h>
48 #include <asm/pgtable.h>
49 #include <asm/pconf.h>
50 #include <asm/idprom.h> /* for gethostid() */
51 #include <asm/unistd.h>
52 #include <asm/system.h>
53
54 /* For the nfs mount emulation */
55 #include <linux/socket.h>
56 #include <linux/in.h>
57 #include <linux/nfs.h>
58 #include <linux/nfs2.h>
59 #include <linux/nfs_mount.h>
60
61 /* for sunos_select */
62 #include <linux/time.h>
63 #include <linux/personality.h>
64
65 /* NR_OPEN is now larger and dynamic in recent kernels. */
66 #define SUNOS_NR_OPEN   256
67
68 /* We use the SunOS mmap() semantics. */
69 asmlinkage unsigned long sunos_mmap(unsigned long addr, unsigned long len,
70                                     unsigned long prot, unsigned long flags,
71                                     unsigned long fd, unsigned long off)
72 {
73         struct file * file = NULL;
74         unsigned long retval, ret_type;
75
76         if (flags & MAP_NORESERVE) {
77                 static int cnt;
78                 if (cnt++ < 10)
79                         printk("%s: unimplemented SunOS MAP_NORESERVE mmap() flag\n",
80                                current->comm);
81                 flags &= ~MAP_NORESERVE;
82         }
83         retval = -EBADF;
84         if (!(flags & MAP_ANONYMOUS)) {
85                 if (fd >= SUNOS_NR_OPEN)
86                         goto out;
87                 file = fget(fd);
88                 if (!file)
89                         goto out;
90         }
91
92         retval = -EINVAL;
93         /* If this is ld.so or a shared library doing an mmap
94          * of /dev/zero, transform it into an anonymous mapping.
95          * SunOS is so stupid some times... hmph!
96          */
97         if (file) {
98                 if (imajor(file->f_dentry->d_inode) == MEM_MAJOR &&
99                     iminor(file->f_dentry->d_inode) == 5) {
100                         flags |= MAP_ANONYMOUS;
101                         fput(file);
102                         file = NULL;
103                 }
104         }
105         ret_type = flags & _MAP_NEW;
106         flags &= ~_MAP_NEW;
107
108         if (!(flags & MAP_FIXED))
109                 addr = 0;
110         else {
111                 if (ARCH_SUN4C_SUN4 &&
112                     (len > 0x20000000 ||
113                      ((flags & MAP_FIXED) &&
114                       addr < 0xe0000000 && addr + len > 0x20000000)))
115                         goto out_putf;
116
117                 /* See asm-sparc/uaccess.h */
118                 if (len > TASK_SIZE - PAGE_SIZE ||
119                     addr + len > TASK_SIZE - PAGE_SIZE)
120                         goto out_putf;
121         }
122
123         flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
124         down_write(&current->mm->mmap_sem);
125         retval = do_mmap(file, addr, len, prot, flags, off);
126         up_write(&current->mm->mmap_sem);
127         if (!ret_type)
128                 retval = ((retval < PAGE_OFFSET) ? 0 : retval);
129
130 out_putf:
131         if (file)
132                 fput(file);
133 out:
134         return retval;
135 }
136
137 /* lmbench calls this, just say "yeah, ok" */
138 asmlinkage int sunos_mctl(unsigned long addr, unsigned long len, int function, char *arg)
139 {
140         return 0;
141 }
142
143 /* SunOS is completely broken... it returns 0 on success, otherwise
144  * ENOMEM.  For sys_sbrk() it wants the old brk value as a return
145  * on success and ENOMEM as before on failure.
146  */
147 asmlinkage int sunos_brk(unsigned long brk)
148 {
149         int freepages, retval = -ENOMEM;
150         unsigned long rlim;
151         unsigned long newbrk, oldbrk;
152
153         down_write(&current->mm->mmap_sem);
154         if (ARCH_SUN4C_SUN4) {
155                 if (brk >= 0x20000000 && brk < 0xe0000000) {
156                         goto out;
157                 }
158         }
159
160         if (brk < current->mm->end_code)
161                 goto out;
162
163         newbrk = PAGE_ALIGN(brk);
164         oldbrk = PAGE_ALIGN(current->mm->brk);
165         retval = 0;
166         if (oldbrk == newbrk) {
167                 current->mm->brk = brk;
168                 goto out;
169         }
170
171         /*
172          * Always allow shrinking brk
173          */
174         if (brk <= current->mm->brk) {
175                 current->mm->brk = brk;
176                 do_munmap(current->mm, newbrk, oldbrk-newbrk);
177                 goto out;
178         }
179         /*
180          * Check against rlimit and stack..
181          */
182         retval = -ENOMEM;
183         rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
184         if (rlim >= RLIM_INFINITY)
185                 rlim = ~0;
186         if (brk - current->mm->end_code > rlim)
187                 goto out;
188
189         /*
190          * Check against existing mmap mappings.
191          */
192         if (find_vma_intersection(current->mm, oldbrk, newbrk+PAGE_SIZE))
193                 goto out;
194
195         /*
196          * stupid algorithm to decide if we have enough memory: while
197          * simple, it hopefully works in most obvious cases.. Easy to
198          * fool it, but this should catch most mistakes.
199          */
200         freepages = global_page_state(NR_FILE_PAGES);
201         freepages >>= 1;
202         freepages += nr_free_pages();
203         freepages += nr_swap_pages;
204         freepages -= num_physpages >> 4;
205         freepages -= (newbrk-oldbrk) >> PAGE_SHIFT;
206         if (freepages < 0)
207                 goto out;
208         /*
209          * Ok, we have probably got enough memory - let it rip.
210          */
211         current->mm->brk = brk;
212         do_brk(oldbrk, newbrk-oldbrk);
213         retval = 0;
214 out:
215         up_write(&current->mm->mmap_sem);
216         return retval;
217 }
218
219 asmlinkage unsigned long sunos_sbrk(int increment)
220 {
221         int error;
222         unsigned long oldbrk;
223
224         /* This should do it hopefully... */
225         lock_kernel();
226         oldbrk = current->mm->brk;
227         error = sunos_brk(((int) current->mm->brk) + increment);
228         if (!error)
229                 error = oldbrk;
230         unlock_kernel();
231         return error;
232 }
233
234 /* XXX Completely undocumented, and completely magic...
235  * XXX I believe it is to increase the size of the stack by
236  * XXX argument 'increment' and return the new end of stack
237  * XXX area.  Wheee...
238  */
239 asmlinkage unsigned long sunos_sstk(int increment)
240 {
241         lock_kernel();
242         printk("%s: Call to sunos_sstk(increment<%d>) is unsupported\n",
243                current->comm, increment);
244         unlock_kernel();
245         return -1;
246 }
247
248 /* Give hints to the kernel as to what paging strategy to use...
249  * Completely bogus, don't remind me.
250  */
251 #define VA_NORMAL     0 /* Normal vm usage expected */
252 #define VA_ABNORMAL   1 /* Abnormal/random vm usage probable */
253 #define VA_SEQUENTIAL 2 /* Accesses will be of a sequential nature */
254 #define VA_INVALIDATE 3 /* Page table entries should be flushed ??? */
255 static char *vstrings[] = {
256         "VA_NORMAL",
257         "VA_ABNORMAL",
258         "VA_SEQUENTIAL",
259         "VA_INVALIDATE",
260 };
261
262 asmlinkage void sunos_vadvise(unsigned long strategy)
263 {
264         /* I wanna see who uses this... */
265         lock_kernel();
266         printk("%s: Advises us to use %s paging strategy\n",
267                current->comm,
268                strategy <= 3 ? vstrings[strategy] : "BOGUS");
269         unlock_kernel();
270 }
271
272 /* This just wants the soft limit (ie. rlim_cur element) of the RLIMIT_NOFILE
273  * resource limit and is for backwards compatibility with older sunos
274  * revs.
275  */
276 asmlinkage long sunos_getdtablesize(void)
277 {
278         return SUNOS_NR_OPEN;
279 }
280
281 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
282
283 asmlinkage unsigned long sunos_sigblock(unsigned long blk_mask)
284 {
285         unsigned long old;
286
287         spin_lock_irq(&current->sighand->siglock);
288         old = current->blocked.sig[0];
289         current->blocked.sig[0] |= (blk_mask & _BLOCKABLE);
290         recalc_sigpending();
291         spin_unlock_irq(&current->sighand->siglock);
292         return old;
293 }
294
295 asmlinkage unsigned long sunos_sigsetmask(unsigned long newmask)
296 {
297         unsigned long retval;
298
299         spin_lock_irq(&current->sighand->siglock);
300         retval = current->blocked.sig[0];
301         current->blocked.sig[0] = (newmask & _BLOCKABLE);
302         recalc_sigpending();
303         spin_unlock_irq(&current->sighand->siglock);
304         return retval;
305 }
306
307 /* SunOS getdents is very similar to the newer Linux (iBCS2 compliant)    */
308 /* getdents system call, the format of the structure just has a different */
309 /* layout (d_off+d_ino instead of d_ino+d_off) */
310 struct sunos_dirent {
311     long           d_off;
312     unsigned long  d_ino;
313     unsigned short d_reclen;
314     unsigned short d_namlen;
315     char           d_name[1];
316 };
317
318 struct sunos_dirent_callback {
319     struct sunos_dirent __user *curr;
320     struct sunos_dirent __user *previous;
321     int count;
322     int error;
323 };
324
325 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
326 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
327
328 static int sunos_filldir(void * __buf, const char * name, int namlen,
329                          loff_t offset, u64 ino, unsigned int d_type)
330 {
331         struct sunos_dirent __user *dirent;
332         struct sunos_dirent_callback * buf = __buf;
333         unsigned long d_ino;
334         int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
335
336         buf->error = -EINVAL;   /* only used if we fail.. */
337         if (reclen > buf->count)
338                 return -EINVAL;
339         d_ino = ino;
340         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
341                 return -EOVERFLOW;
342         dirent = buf->previous;
343         if (dirent)
344                 put_user(offset, &dirent->d_off);
345         dirent = buf->curr;
346         buf->previous = dirent;
347         put_user(d_ino, &dirent->d_ino);
348         put_user(namlen, &dirent->d_namlen);
349         put_user(reclen, &dirent->d_reclen);
350         copy_to_user(dirent->d_name, name, namlen);
351         put_user(0, dirent->d_name + namlen);
352         dirent = (void __user *) dirent + reclen;
353         buf->curr = dirent;
354         buf->count -= reclen;
355         return 0;
356 }
357
358 asmlinkage int sunos_getdents(unsigned int fd, void __user *dirent, int cnt)
359 {
360         struct file * file;
361         struct sunos_dirent __user *lastdirent;
362         struct sunos_dirent_callback buf;
363         int error = -EBADF;
364
365         if (fd >= SUNOS_NR_OPEN)
366                 goto out;
367
368         file = fget(fd);
369         if (!file)
370                 goto out;
371
372         error = -EINVAL;
373         if (cnt < (sizeof(struct sunos_dirent) + 255))
374                 goto out_putf;
375
376         buf.curr = (struct sunos_dirent __user *) dirent;
377         buf.previous = NULL;
378         buf.count = cnt;
379         buf.error = 0;
380
381         error = vfs_readdir(file, sunos_filldir, &buf);
382         if (error < 0)
383                 goto out_putf;
384
385         lastdirent = buf.previous;
386         error = buf.error;
387         if (lastdirent) {
388                 put_user(file->f_pos, &lastdirent->d_off);
389                 error = cnt - buf.count;
390         }
391
392 out_putf:
393         fput(file);
394 out:
395         return error;
396 }
397
398 /* Old sunos getdirentries, severely broken compatibility stuff here. */
399 struct sunos_direntry {
400     unsigned long  d_ino;
401     unsigned short d_reclen;
402     unsigned short d_namlen;
403     char           d_name[1];
404 };
405
406 struct sunos_direntry_callback {
407     struct sunos_direntry __user *curr;
408     struct sunos_direntry __user *previous;
409     int count;
410     int error;
411 };
412
413 static int sunos_filldirentry(void * __buf, const char * name, int namlen,
414                               loff_t offset, u64 ino, unsigned int d_type)
415 {
416         struct sunos_direntry __user *dirent;
417         struct sunos_direntry_callback *buf = __buf;
418         unsigned long d_ino;
419         int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
420
421         buf->error = -EINVAL;   /* only used if we fail.. */
422         if (reclen > buf->count)
423                 return -EINVAL;
424         d_ino = ino;
425         if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
426                 return -EOVERFLOW;
427         dirent = buf->previous;
428         dirent = buf->curr;
429         buf->previous = dirent;
430         put_user(d_ino, &dirent->d_ino);
431         put_user(namlen, &dirent->d_namlen);
432         put_user(reclen, &dirent->d_reclen);
433         copy_to_user(dirent->d_name, name, namlen);
434         put_user(0, dirent->d_name + namlen);
435         dirent = (void __user *) dirent + reclen;
436         buf->curr = dirent;
437         buf->count -= reclen;
438         return 0;
439 }
440
441 asmlinkage int sunos_getdirentries(unsigned int fd, void __user *dirent,
442                                    int cnt, unsigned int __user *basep)
443 {
444         struct file * file;
445         struct sunos_direntry __user *lastdirent;
446         struct sunos_direntry_callback buf;
447         int error = -EBADF;
448
449         if (fd >= SUNOS_NR_OPEN)
450                 goto out;
451
452         file = fget(fd);
453         if (!file)
454                 goto out;
455
456         error = -EINVAL;
457         if (cnt < (sizeof(struct sunos_direntry) + 255))
458                 goto out_putf;
459
460         buf.curr = (struct sunos_direntry __user *) dirent;
461         buf.previous = NULL;
462         buf.count = cnt;
463         buf.error = 0;
464
465         error = vfs_readdir(file, sunos_filldirentry, &buf);
466         if (error < 0)
467                 goto out_putf;
468
469         lastdirent = buf.previous;
470         error = buf.error;
471         if (lastdirent) {
472                 put_user(file->f_pos, basep);
473                 error = cnt - buf.count;
474         }
475
476 out_putf:
477         fput(file);
478 out:
479         return error;
480 }
481
482 struct sunos_utsname {
483         char sname[9];
484         char nname[9];
485         char nnext[56];
486         char rel[9];
487         char ver[9];
488         char mach[9];
489 };
490
491 asmlinkage int sunos_uname(struct sunos_utsname __user *name)
492 {
493         int ret;
494         struct new_utsname *ptr;
495         down_read(&uts_sem);
496         ptr = vx_new_utsname();
497         ret = copy_to_user(&name->sname[0], ptr->sysname, sizeof(name->sname) - 1);
498         if (!ret) {
499                 ret |= __copy_to_user(&name->nname[0], ptr->nodename, sizeof(name->nname) - 1);
500                 ret |= __put_user('\0', &name->nname[8]);
501                 ret |= __copy_to_user(&name->rel[0], ptr->release, sizeof(name->rel) - 1);
502                 ret |= __copy_to_user(&name->ver[0], ptr->version, sizeof(name->ver) - 1);
503                 ret |= __copy_to_user(&name->mach[0], ptr->machine, sizeof(name->mach) - 1);
504         }
505         up_read(&uts_sem);
506         return ret ? -EFAULT : 0;
507 }
508
509 asmlinkage int sunos_nosys(void)
510 {
511         struct pt_regs *regs;
512         siginfo_t info;
513         static int cnt;
514
515         lock_kernel();
516         regs = current->thread.kregs;
517         info.si_signo = SIGSYS;
518         info.si_errno = 0;
519         info.si_code = __SI_FAULT|0x100;
520         info.si_addr = (void __user *)regs->pc;
521         info.si_trapno = regs->u_regs[UREG_G1];
522         send_sig_info(SIGSYS, &info, current);
523         if (cnt++ < 4) {
524                 printk("Process makes ni_syscall number %d, register dump:\n",
525                        (int) regs->u_regs[UREG_G1]);
526                 show_regs(regs);
527         }
528         unlock_kernel();
529         return -ENOSYS;
530 }
531
532 /* This is not a real and complete implementation yet, just to keep
533  * the easy SunOS binaries happy.
534  */
535 asmlinkage int sunos_fpathconf(int fd, int name)
536 {
537         int ret;
538
539         switch(name) {
540         case _PCONF_LINK:
541                 ret = LINK_MAX;
542                 break;
543         case _PCONF_CANON:
544                 ret = MAX_CANON;
545                 break;
546         case _PCONF_INPUT:
547                 ret = MAX_INPUT;
548                 break;
549         case _PCONF_NAME:
550                 ret = NAME_MAX;
551                 break;
552         case _PCONF_PATH:
553                 ret = PATH_MAX;
554                 break;
555         case _PCONF_PIPE:
556                 ret = PIPE_BUF;
557                 break;
558         case _PCONF_CHRESTRICT:         /* XXX Investigate XXX */
559                 ret = 1;
560                 break;
561         case _PCONF_NOTRUNC:            /* XXX Investigate XXX */
562         case _PCONF_VDISABLE:
563                 ret = 0;
564                 break;
565         default:
566                 ret = -EINVAL;
567                 break;
568         }
569         return ret;
570 }
571
572 asmlinkage int sunos_pathconf(char __user *path, int name)
573 {
574         int ret;
575
576         ret = sunos_fpathconf(0, name); /* XXX cheese XXX */
577         return ret;
578 }
579
580 /* SunOS mount system call emulation */
581
582 asmlinkage int sunos_select(int width, fd_set __user *inp, fd_set __user *outp,
583                             fd_set __user *exp, struct timeval __user *tvp)
584 {
585         int ret;
586
587         /* SunOS binaries expect that select won't change the tvp contents */
588         ret = sys_select (width, inp, outp, exp, tvp);
589         if (ret == -EINTR && tvp) {
590                 time_t sec, usec;
591
592                 __get_user(sec, &tvp->tv_sec);
593                 __get_user(usec, &tvp->tv_usec);
594
595                 if (sec == 0 && usec == 0)
596                         ret = 0;
597         }
598         return ret;
599 }
600
601 asmlinkage void sunos_nop(void)
602 {
603         return;
604 }
605
606 /* SunOS mount/umount. */
607 #define SMNT_RDONLY       1
608 #define SMNT_NOSUID       2
609 #define SMNT_NEWTYPE      4
610 #define SMNT_GRPID        8
611 #define SMNT_REMOUNT      16
612 #define SMNT_NOSUB        32
613 #define SMNT_MULTI        64
614 #define SMNT_SYS5         128
615
616 struct sunos_fh_t {
617         char fh_data [NFS_FHSIZE];
618 };
619
620 struct sunos_nfs_mount_args {
621         struct sockaddr_in  __user *addr; /* file server address */
622         struct nfs_fh __user *fh;     /* File handle to be mounted */
623         int        flags;      /* flags */
624         int        wsize;      /* write size in bytes */
625         int        rsize;      /* read size in bytes */
626         int        timeo;      /* initial timeout in .1 secs */
627         int        retrans;    /* times to retry send */
628         char       __user *hostname;  /* server's hostname */
629         int        acregmin;   /* attr cache file min secs */
630         int        acregmax;   /* attr cache file max secs */
631         int        acdirmin;   /* attr cache dir min secs */
632         int        acdirmax;   /* attr cache dir max secs */
633         char       __user *netname;   /* server's netname */
634 };
635
636
637 /* Bind the socket on a local reserved port and connect it to the
638  * remote server.  This on Linux/i386 is done by the mount program,
639  * not by the kernel.
640  */
641 static int
642 sunos_nfs_get_server_fd (int fd, struct sockaddr_in *addr)
643 {
644         struct sockaddr_in local;
645         struct sockaddr_in server;
646         int    try_port;
647         struct socket *socket;
648         struct inode  *inode;
649         struct file   *file;
650         int    ret, result = 0;
651
652         file = fget(fd);
653         if (!file)
654                 goto out;
655
656         inode = file->f_dentry->d_inode;
657
658         socket = SOCKET_I(inode);
659         local.sin_family = AF_INET;
660         local.sin_addr.s_addr = INADDR_ANY;
661
662         /* IPPORT_RESERVED = 1024, can't find the definition in the kernel */
663         try_port = 1024;
664         do {
665                 local.sin_port = htons (--try_port);
666                 ret = socket->ops->bind(socket, (struct sockaddr*)&local,
667                                         sizeof(local));
668         } while (ret && try_port > (1024 / 2));
669
670         if (ret)
671                 goto out_putf;
672
673         server.sin_family = AF_INET;
674         server.sin_addr = addr->sin_addr;
675         server.sin_port = NFS_PORT;
676
677         /* Call sys_connect */
678         ret = socket->ops->connect (socket, (struct sockaddr *) &server,
679                                     sizeof (server), file->f_flags);
680         if (ret >= 0)
681                 result = 1;
682
683 out_putf:
684         fput(file);
685 out:
686         return result;
687 }
688
689 static int get_default (int value, int def_value)
690 {
691     if (value)
692         return value;
693     else
694         return def_value;
695 }
696
697 static int sunos_nfs_mount(char *dir_name, int linux_flags, void __user *data)
698 {
699         int  server_fd, err;
700         char *the_name, *mount_page;
701         struct nfs_mount_data linux_nfs_mount;
702         struct sunos_nfs_mount_args sunos_mount;
703
704         /* Ok, here comes the fun part: Linux's nfs mount needs a
705          * socket connection to the server, but SunOS mount does not
706          * require this, so we use the information on the destination
707          * address to create a socket and bind it to a reserved
708          * port on this system
709          */
710         if (copy_from_user(&sunos_mount, data, sizeof(sunos_mount)))
711                 return -EFAULT;
712
713         server_fd = sys_socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
714         if (server_fd < 0)
715                 return -ENXIO;
716
717         if (copy_from_user(&linux_nfs_mount.addr,sunos_mount.addr,
718                                 sizeof(*sunos_mount.addr)) ||
719             copy_from_user(&linux_nfs_mount.root,sunos_mount.fh,
720                                 sizeof(*sunos_mount.fh))) {
721                 sys_close (server_fd);
722                 return -EFAULT;
723         }
724
725         if (!sunos_nfs_get_server_fd (server_fd, &linux_nfs_mount.addr)){
726                 sys_close (server_fd);
727                 return -ENXIO;
728         }
729
730         /* Now, bind it to a locally reserved port */
731         linux_nfs_mount.version  = NFS_MOUNT_VERSION;
732         linux_nfs_mount.flags    = sunos_mount.flags;
733         linux_nfs_mount.fd       = server_fd;
734         
735         linux_nfs_mount.rsize    = get_default (sunos_mount.rsize, 8192);
736         linux_nfs_mount.wsize    = get_default (sunos_mount.wsize, 8192);
737         linux_nfs_mount.timeo    = get_default (sunos_mount.timeo, 10);
738         linux_nfs_mount.retrans  = sunos_mount.retrans;
739         
740         linux_nfs_mount.acregmin = sunos_mount.acregmin;
741         linux_nfs_mount.acregmax = sunos_mount.acregmax;
742         linux_nfs_mount.acdirmin = sunos_mount.acdirmin;
743         linux_nfs_mount.acdirmax = sunos_mount.acdirmax;
744
745         the_name = getname(sunos_mount.hostname);
746         if (IS_ERR(the_name))
747                 return PTR_ERR(the_name);
748
749         strlcpy(linux_nfs_mount.hostname, the_name,
750                 sizeof(linux_nfs_mount.hostname));
751         putname (the_name);
752         
753         mount_page = (char *) get_zeroed_page(GFP_KERNEL);
754         if (!mount_page)
755                 return -ENOMEM;
756
757         memcpy(mount_page, &linux_nfs_mount, sizeof(linux_nfs_mount));
758
759         err = do_mount("", dir_name, "nfs", linux_flags, mount_page);
760
761         free_page((unsigned long) mount_page);
762         return err;
763 }
764
765 asmlinkage int
766 sunos_mount(char __user *type, char __user *dir, int flags, void __user *data)
767 {
768         int linux_flags = 0;
769         int ret = -EINVAL;
770         char *dev_fname = NULL;
771         char *dir_page, *type_page;
772
773         if (!capable (CAP_SYS_ADMIN))
774                 return -EPERM;
775                 
776         lock_kernel();
777         /* We don't handle the integer fs type */
778         if ((flags & SMNT_NEWTYPE) == 0)
779                 goto out;
780
781         /* Do not allow for those flags we don't support */
782         if (flags & (SMNT_GRPID|SMNT_NOSUB|SMNT_MULTI|SMNT_SYS5))
783                 goto out;
784
785         if (flags & SMNT_REMOUNT)
786                 linux_flags |= MS_REMOUNT;
787         if (flags & SMNT_RDONLY)
788                 linux_flags |= MS_RDONLY;
789         if (flags & SMNT_NOSUID)
790                 linux_flags |= MS_NOSUID;
791
792         dir_page = getname(dir);
793         ret = PTR_ERR(dir_page);
794         if (IS_ERR(dir_page))
795                 goto out;
796
797         type_page = getname(type);
798         ret = PTR_ERR(type_page);
799         if (IS_ERR(type_page))
800                 goto out1;
801
802         if (strcmp(type_page, "ext2") == 0) {
803                 dev_fname = getname(data);
804         } else if (strcmp(type_page, "iso9660") == 0) {
805                 dev_fname = getname(data);
806         } else if (strcmp(type_page, "minix") == 0) {
807                 dev_fname = getname(data);
808         } else if (strcmp(type_page, "nfs") == 0) {
809                 ret = sunos_nfs_mount (dir_page, flags, data);
810                 goto out2;
811         } else if (strcmp(type_page, "ufs") == 0) {
812                 printk("Warning: UFS filesystem mounts unsupported.\n");
813                 ret = -ENODEV;
814                 goto out2;
815         } else if (strcmp(type_page, "proc")) {
816                 ret = -ENODEV;
817                 goto out2;
818         }
819         ret = PTR_ERR(dev_fname);
820         if (IS_ERR(dev_fname))
821                 goto out2;
822         ret = do_mount(dev_fname, dir_page, type_page, linux_flags, NULL);
823         if (dev_fname)
824                 putname(dev_fname);
825 out2:
826         putname(type_page);
827 out1:
828         putname(dir_page);
829 out:
830         unlock_kernel();
831         return ret;
832 }
833
834
835 asmlinkage int sunos_setpgrp(pid_t pid, pid_t pgid)
836 {
837         int ret;
838
839         /* So stupid... */
840         if ((!pid || pid == current->pid) &&
841             !pgid) {
842                 sys_setsid();
843                 ret = 0;
844         } else {
845                 ret = sys_setpgid(pid, pgid);
846         }
847         return ret;
848 }
849
850 /* So stupid... */
851 asmlinkage int sunos_wait4(pid_t pid, unsigned int __user *stat_addr,
852                            int options, struct rusage __user*ru)
853 {
854         int ret;
855
856         ret = sys_wait4((pid ? pid : -1), stat_addr, options, ru);
857         return ret;
858 }
859
860 extern int kill_pg(int, int, int);
861 asmlinkage int sunos_killpg(int pgrp, int sig)
862 {
863         int ret;
864
865         lock_kernel();
866         ret = kill_pg(pgrp, sig, 0);
867         unlock_kernel();
868         return ret;
869 }
870
871 asmlinkage int sunos_audit(void)
872 {
873         lock_kernel();
874         printk ("sys_audit\n");
875         unlock_kernel();
876         return -1;
877 }
878
879 asmlinkage unsigned long sunos_gethostid(void)
880 {
881         unsigned long ret;
882
883         lock_kernel();
884         ret = ((unsigned long)idprom->id_machtype << 24) |
885                 (unsigned long)idprom->id_sernum;
886         unlock_kernel();
887         return ret;
888 }
889
890 /* sysconf options, for SunOS compatibility */
891 #define   _SC_ARG_MAX             1
892 #define   _SC_CHILD_MAX           2
893 #define   _SC_CLK_TCK             3
894 #define   _SC_NGROUPS_MAX         4
895 #define   _SC_OPEN_MAX            5
896 #define   _SC_JOB_CONTROL         6
897 #define   _SC_SAVED_IDS           7
898 #define   _SC_VERSION             8
899
900 asmlinkage long sunos_sysconf (int name)
901 {
902         long ret;
903
904         switch (name){
905         case _SC_ARG_MAX:
906                 ret = ARG_MAX;
907                 break;
908         case _SC_CHILD_MAX:
909                 ret = -1; /* no limit */
910                 break;
911         case _SC_CLK_TCK:
912                 ret = HZ;
913                 break;
914         case _SC_NGROUPS_MAX:
915                 ret = NGROUPS_MAX;
916                 break;
917         case _SC_OPEN_MAX:
918                 ret = OPEN_MAX;
919                 break;
920         case _SC_JOB_CONTROL:
921                 ret = 1;        /* yes, we do support job control */
922                 break;
923         case _SC_SAVED_IDS:
924                 ret = 1;        /* yes, we do support saved uids  */
925                 break;
926         case _SC_VERSION:
927                 /* mhm, POSIX_VERSION is in /usr/include/unistd.h
928                  * should it go on /usr/include/linux?
929                  */
930                 ret = 199009L; 
931                 break;
932         default:
933                 ret = -1;
934                 break;
935         };
936         return ret;
937 }
938
939 asmlinkage int sunos_semsys(int op, unsigned long arg1, unsigned long arg2,
940                             unsigned long arg3, void *ptr)
941 {
942         union semun arg4;
943         int ret;
944
945         switch (op) {
946         case 0:
947                 /* Most arguments match on a 1:1 basis but cmd doesn't */
948                 switch(arg3) {
949                 case 4:
950                         arg3=GETPID; break;
951                 case 5:
952                         arg3=GETVAL; break;
953                 case 6:
954                         arg3=GETALL; break;
955                 case 3:
956                         arg3=GETNCNT; break;
957                 case 7:
958                         arg3=GETZCNT; break;
959                 case 8:
960                         arg3=SETVAL; break;
961                 case 9:
962                         arg3=SETALL; break;
963                 }
964                 /* sys_semctl(): */
965                 /* value to modify semaphore to */
966                 arg4.__pad = (void __user *) ptr;
967                 ret = sys_semctl((int)arg1, (int)arg2, (int)arg3, arg4 );
968                 break;
969         case 1:
970                 /* sys_semget(): */
971                 ret = sys_semget((key_t)arg1, (int)arg2, (int)arg3);
972                 break;
973         case 2:
974                 /* sys_semop(): */
975                 ret = sys_semop((int)arg1, (struct sembuf __user *)arg2, (unsigned)arg3);
976                 break;
977         default:
978                 ret = -EINVAL;
979                 break;
980         };
981         return ret;
982 }
983
984 asmlinkage int sunos_msgsys(int op, unsigned long arg1, unsigned long arg2,
985                             unsigned long arg3, unsigned long arg4)
986 {
987         struct sparc_stackf *sp;
988         unsigned long arg5;
989         int rval;
990
991         switch(op) {
992         case 0:
993                 rval = sys_msgget((key_t)arg1, (int)arg2);
994                 break;
995         case 1:
996                 rval = sys_msgctl((int)arg1, (int)arg2,
997                                   (struct msqid_ds __user *)arg3);
998                 break;
999         case 2:
1000                 lock_kernel();
1001                 sp = (struct sparc_stackf *)current->thread.kregs->u_regs[UREG_FP];
1002                 arg5 = sp->xxargs[0];
1003                 unlock_kernel();
1004                 rval = sys_msgrcv((int)arg1, (struct msgbuf __user *)arg2,
1005                                   (size_t)arg3, (long)arg4, (int)arg5);
1006                 break;
1007         case 3:
1008                 rval = sys_msgsnd((int)arg1, (struct msgbuf __user *)arg2,
1009                                   (size_t)arg3, (int)arg4);
1010                 break;
1011         default:
1012                 rval = -EINVAL;
1013                 break;
1014         }
1015         return rval;
1016 }
1017
1018 asmlinkage int sunos_shmsys(int op, unsigned long arg1, unsigned long arg2,
1019                             unsigned long arg3)
1020 {
1021         unsigned long raddr;
1022         int rval;
1023
1024         switch(op) {
1025         case 0:
1026                 /* do_shmat(): attach a shared memory area */
1027                 rval = do_shmat((int)arg1,(char __user *)arg2,(int)arg3,&raddr);
1028                 if (!rval)
1029                         rval = (int) raddr;
1030                 break;
1031         case 1:
1032                 /* sys_shmctl(): modify shared memory area attr. */
1033                 rval = sys_shmctl((int)arg1,(int)arg2,(struct shmid_ds __user *)arg3);
1034                 break;
1035         case 2:
1036                 /* sys_shmdt(): detach a shared memory area */
1037                 rval = sys_shmdt((char __user *)arg1);
1038                 break;
1039         case 3:
1040                 /* sys_shmget(): get a shared memory area */
1041                 rval = sys_shmget((key_t)arg1,(int)arg2,(int)arg3);
1042                 break;
1043         default:
1044                 rval = -EINVAL;
1045                 break;
1046         };
1047         return rval;
1048 }
1049
1050 #define SUNOS_EWOULDBLOCK 35
1051
1052 /* see the sunos man page read(2v) for an explanation
1053    of this garbage. We use O_NDELAY to mark
1054    file descriptors that have been set non-blocking 
1055    using 4.2BSD style calls. (tridge) */
1056
1057 static inline int check_nonblock(int ret, int fd)
1058 {
1059         if (ret == -EAGAIN) {
1060                 struct file * file = fget(fd);
1061                 if (file) {
1062                         if (file->f_flags & O_NDELAY)
1063                                 ret = -SUNOS_EWOULDBLOCK;
1064                         fput(file);
1065                 }
1066         }
1067         return ret;
1068 }
1069
1070 asmlinkage int sunos_read(unsigned int fd, char __user *buf, int count)
1071 {
1072         int ret;
1073
1074         ret = check_nonblock(sys_read(fd,buf,count),fd);
1075         return ret;
1076 }
1077
1078 asmlinkage int sunos_readv(unsigned long fd, const struct iovec __user *vector,
1079                            long count)
1080 {
1081         int ret;
1082
1083         ret = check_nonblock(sys_readv(fd,vector,count),fd);
1084         return ret;
1085 }
1086
1087 asmlinkage int sunos_write(unsigned int fd, char __user *buf, int count)
1088 {
1089         int ret;
1090
1091         ret = check_nonblock(sys_write(fd,buf,count),fd);
1092         return ret;
1093 }
1094
1095 asmlinkage int sunos_writev(unsigned long fd,
1096                             const struct iovec __user *vector, long count)
1097 {
1098         int ret;
1099
1100         ret = check_nonblock(sys_writev(fd,vector,count),fd);
1101         return ret;
1102 }
1103
1104 asmlinkage int sunos_recv(int fd, void __user *ubuf, int size, unsigned flags)
1105 {
1106         int ret;
1107
1108         ret = check_nonblock(sys_recv(fd,ubuf,size,flags),fd);
1109         return ret;
1110 }
1111
1112 asmlinkage int sunos_send(int fd, void __user *buff, int len, unsigned flags)
1113 {
1114         int ret;
1115
1116         ret = check_nonblock(sys_send(fd,buff,len,flags),fd);
1117         return ret;
1118 }
1119
1120 asmlinkage int sunos_accept(int fd, struct sockaddr __user *sa,
1121                             int __user *addrlen)
1122 {
1123         int ret;
1124
1125         while (1) {
1126                 ret = check_nonblock(sys_accept(fd,sa,addrlen),fd);     
1127                 if (ret != -ENETUNREACH && ret != -EHOSTUNREACH)
1128                         break;
1129         }
1130
1131         return ret;
1132 }
1133
1134 #define SUNOS_SV_INTERRUPT 2
1135
1136 asmlinkage int
1137 sunos_sigaction(int sig, const struct old_sigaction __user *act,
1138                 struct old_sigaction __user *oact)
1139 {
1140         struct k_sigaction new_ka, old_ka;
1141         int ret;
1142
1143         if (act) {
1144                 old_sigset_t mask;
1145
1146                 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
1147                     __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
1148                     __get_user(new_ka.sa.sa_flags, &act->sa_flags))
1149                         return -EFAULT;
1150                 __get_user(mask, &act->sa_mask);
1151                 new_ka.sa.sa_restorer = NULL;
1152                 new_ka.ka_restorer = NULL;
1153                 siginitset(&new_ka.sa.sa_mask, mask);
1154                 new_ka.sa.sa_flags ^= SUNOS_SV_INTERRUPT;
1155         }
1156
1157         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
1158
1159         if (!ret && oact) {
1160                 /* In the clone() case we could copy half consistent
1161                  * state to the user, however this could sleep and
1162                  * deadlock us if we held the signal lock on SMP.  So for
1163                  * now I take the easy way out and do no locking.
1164                  * But then again we don't support SunOS lwp's anyways ;-)
1165                  */
1166                 old_ka.sa.sa_flags ^= SUNOS_SV_INTERRUPT;
1167                 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
1168                     __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
1169                     __put_user(old_ka.sa.sa_flags, &oact->sa_flags))
1170                          return -EFAULT;
1171                 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
1172         }
1173
1174         return ret;
1175 }
1176
1177
1178 asmlinkage int sunos_setsockopt(int fd, int level, int optname,
1179                                 char __user *optval, int optlen)
1180 {
1181         int tr_opt = optname;
1182         int ret;
1183
1184         if (level == SOL_IP) {
1185                 /* Multicast socketopts (ttl, membership) */
1186                 if (tr_opt >=2 && tr_opt <= 6)
1187                         tr_opt += 30;
1188         }
1189         ret = sys_setsockopt(fd, level, tr_opt, optval, optlen);
1190         return ret;
1191 }
1192
1193 asmlinkage int sunos_getsockopt(int fd, int level, int optname,
1194                                 char __user *optval, int __user *optlen)
1195 {
1196         int tr_opt = optname;
1197         int ret;
1198
1199         if (level == SOL_IP) {
1200                 /* Multicast socketopts (ttl, membership) */
1201                 if (tr_opt >=2 && tr_opt <= 6)
1202                         tr_opt += 30;
1203         }
1204         ret = sys_getsockopt(fd, level, tr_opt, optval, optlen);
1205         return ret;
1206 }