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