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