1 /* $Id: fs.c,v 1.23 2000/08/29 07:01:54 davem Exp $
2 * fs.c: fs related syscall emulation for Solaris
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * 1999-08-19 Implemented solaris F_FREESP (truncate)
7 * fcntl, by Jason Rappleye (rappleye@ccr.buffalo.edu)
10 #include <linux/types.h>
11 #include <linux/sched.h>
12 #include <linux/malloc.h>
15 #include <linux/file.h>
16 #include <linux/stat.h>
17 #include <linux/smp_lock.h>
18 #include <linux/limits.h>
19 #include <linux/resource.h>
20 #include <linux/quotaops.h>
22 #include <asm/uaccess.h>
23 #include <asm/string.h>
24 #include <asm/ptrace.h>
28 #define R4_DEV(DEV) ((DEV & 0xff) | ((DEV & 0xff00) << 10))
29 #define R4_MAJOR(DEV) (((DEV) >> 18) & 0x3fff)
30 #define R4_MINOR(DEV) ((DEV) & 0x3ffff)
41 s32 st_pad1
[3]; /* network id */
50 s32 st_pad3
; /* st_size, off_t expansion */
51 timestruct_t st_atime
;
52 timestruct_t st_mtime
;
53 timestruct_t st_ctime
;
57 s32 st_pad4
[8]; /* expansion area */
62 s32 st_pad1
[3]; /* network id */
71 timestruct_t st_atime
;
72 timestruct_t st_mtime
;
73 timestruct_t st_ctime
;
77 s32 st_pad4
[4]; /* expansion area */
80 #define UFSMAGIC (((unsigned)'u'<<24)||((unsigned)'f'<<16)||((unsigned)'s'<<8))
82 static inline int putstat(struct sol_stat
*ubuf
, struct stat
*kbuf
)
84 if (put_user (R4_DEV(kbuf
->st_dev
), &ubuf
->st_dev
) ||
85 __put_user (kbuf
->st_ino
, &ubuf
->st_ino
) ||
86 __put_user (kbuf
->st_mode
, &ubuf
->st_mode
) ||
87 __put_user (kbuf
->st_nlink
, &ubuf
->st_nlink
) ||
88 __put_user (kbuf
->st_uid
, &ubuf
->st_uid
) ||
89 __put_user (kbuf
->st_gid
, &ubuf
->st_gid
) ||
90 __put_user (R4_DEV(kbuf
->st_rdev
), &ubuf
->st_rdev
) ||
91 __put_user (kbuf
->st_size
, &ubuf
->st_size
) ||
92 __put_user (kbuf
->st_atime
, &ubuf
->st_atime
.tv_sec
) ||
93 __put_user (0, &ubuf
->st_atime
.tv_nsec
) ||
94 __put_user (kbuf
->st_mtime
, &ubuf
->st_mtime
.tv_sec
) ||
95 __put_user (0, &ubuf
->st_mtime
.tv_nsec
) ||
96 __put_user (kbuf
->st_ctime
, &ubuf
->st_ctime
.tv_sec
) ||
97 __put_user (0, &ubuf
->st_ctime
.tv_nsec
) ||
98 __put_user (kbuf
->st_blksize
, &ubuf
->st_blksize
) ||
99 __put_user (kbuf
->st_blocks
, &ubuf
->st_blocks
) ||
100 __put_user (UFSMAGIC
, (unsigned *)ubuf
->st_fstype
))
105 static inline int putstat64(struct sol_stat64
*ubuf
, struct stat
*kbuf
)
107 if (put_user (R4_DEV(kbuf
->st_dev
), &ubuf
->st_dev
) ||
108 __put_user (kbuf
->st_ino
, &ubuf
->st_ino
) ||
109 __put_user (kbuf
->st_mode
, &ubuf
->st_mode
) ||
110 __put_user (kbuf
->st_nlink
, &ubuf
->st_nlink
) ||
111 __put_user (kbuf
->st_uid
, &ubuf
->st_uid
) ||
112 __put_user (kbuf
->st_gid
, &ubuf
->st_gid
) ||
113 __put_user (R4_DEV(kbuf
->st_rdev
), &ubuf
->st_rdev
) ||
114 __put_user (kbuf
->st_size
, &ubuf
->st_size
) ||
115 __put_user (kbuf
->st_atime
, &ubuf
->st_atime
.tv_sec
) ||
116 __put_user (0, &ubuf
->st_atime
.tv_nsec
) ||
117 __put_user (kbuf
->st_mtime
, &ubuf
->st_mtime
.tv_sec
) ||
118 __put_user (0, &ubuf
->st_mtime
.tv_nsec
) ||
119 __put_user (kbuf
->st_ctime
, &ubuf
->st_ctime
.tv_sec
) ||
120 __put_user (0, &ubuf
->st_ctime
.tv_nsec
) ||
121 __put_user (kbuf
->st_blksize
, &ubuf
->st_blksize
) ||
122 __put_user (kbuf
->st_blocks
, &ubuf
->st_blocks
) ||
123 __put_user (UFSMAGIC
, (unsigned *)ubuf
->st_fstype
))
128 asmlinkage
int solaris_stat(u32 filename
, u32 statbuf
)
133 mm_segment_t old_fs
= get_fs();
134 int (*sys_newstat
)(char *,struct stat
*) =
135 (int (*)(char *,struct stat
*))SYS(stat
);
137 filenam
= getname ((char *)A(filename
));
138 ret
= PTR_ERR(filenam
);
139 if (!IS_ERR(filenam
)) {
141 ret
= sys_newstat(filenam
, &s
);
144 if (putstat ((struct sol_stat
*)A(statbuf
), &s
))
150 asmlinkage
int solaris_xstat(int vers
, u32 filename
, u32 statbuf
)
152 /* Solaris doesn't bother with looking at vers, so we do neither */
153 return solaris_stat(filename
, statbuf
);
156 asmlinkage
int solaris_stat64(u32 filename
, u32 statbuf
)
161 mm_segment_t old_fs
= get_fs();
162 int (*sys_newstat
)(char *,struct stat
*) =
163 (int (*)(char *,struct stat
*))SYS(stat
);
165 filenam
= getname ((char *)A(filename
));
166 ret
= PTR_ERR(filenam
);
167 if (!IS_ERR(filenam
)) {
169 ret
= sys_newstat(filenam
, &s
);
172 if (putstat64 ((struct sol_stat64
*)A(statbuf
), &s
))
178 asmlinkage
int solaris_lstat(u32 filename
, u32 statbuf
)
183 mm_segment_t old_fs
= get_fs();
184 int (*sys_newlstat
)(char *,struct stat
*) =
185 (int (*)(char *,struct stat
*))SYS(lstat
);
187 filenam
= getname ((char *)A(filename
));
188 ret
= PTR_ERR(filenam
);
189 if (!IS_ERR(filenam
)) {
191 ret
= sys_newlstat(filenam
, &s
);
194 if (putstat ((struct sol_stat
*)A(statbuf
), &s
))
200 asmlinkage
int solaris_lxstat(int vers
, u32 filename
, u32 statbuf
)
202 return solaris_lstat(filename
, statbuf
);
205 asmlinkage
int solaris_lstat64(u32 filename
, u32 statbuf
)
210 mm_segment_t old_fs
= get_fs();
211 int (*sys_newlstat
)(char *,struct stat
*) =
212 (int (*)(char *,struct stat
*))SYS(lstat
);
214 filenam
= getname ((char *)A(filename
));
215 ret
= PTR_ERR(filenam
);
216 if (!IS_ERR(filenam
)) {
218 ret
= sys_newlstat(filenam
, &s
);
221 if (putstat64 ((struct sol_stat64
*)A(statbuf
), &s
))
227 asmlinkage
int solaris_fstat(unsigned int fd
, u32 statbuf
)
231 mm_segment_t old_fs
= get_fs();
232 int (*sys_newfstat
)(unsigned,struct stat
*) =
233 (int (*)(unsigned,struct stat
*))SYS(fstat
);
236 ret
= sys_newfstat(fd
, &s
);
238 if (putstat ((struct sol_stat
*)A(statbuf
), &s
))
243 asmlinkage
int solaris_fxstat(int vers
, u32 fd
, u32 statbuf
)
245 return solaris_fstat(fd
, statbuf
);
248 asmlinkage
int solaris_fstat64(unsigned int fd
, u32 statbuf
)
252 mm_segment_t old_fs
= get_fs();
253 int (*sys_newfstat
)(unsigned,struct stat
*) =
254 (int (*)(unsigned,struct stat
*))SYS(fstat
);
257 ret
= sys_newfstat(fd
, &s
);
259 if (putstat64 ((struct sol_stat64
*)A(statbuf
), &s
))
264 asmlinkage
int solaris_mknod(u32 path
, u32 mode
, s32 dev
)
266 int (*sys_mknod
)(const char *,int,dev_t
) =
267 (int (*)(const char *,int,dev_t
))SYS(mknod
);
270 if ((major
= R4_MAJOR(dev
)) > 255 ||
271 (minor
= R4_MINOR(dev
)) > 255) return -EINVAL
;
272 return sys_mknod((const char *)A(path
), mode
, MKDEV(major
,minor
));
275 asmlinkage
int solaris_xmknod(int vers
, u32 path
, u32 mode
, s32 dev
)
277 return solaris_mknod(path
, mode
, dev
);
280 asmlinkage
int solaris_getdents64(unsigned int fd
, void *dirent
, unsigned int count
)
282 int (*sys_getdents
)(unsigned int, void *, unsigned int) =
283 (int (*)(unsigned int, void *, unsigned int))SYS(getdents
);
285 return sys_getdents(fd
, dirent
, count
);
288 /* This statfs thingie probably will go in the near future, but... */
302 asmlinkage
int solaris_statfs(u32 path
, u32 buf
, int len
, int fstype
)
306 mm_segment_t old_fs
= get_fs();
307 int (*sys_statfs
)(const char *,struct statfs
*) =
308 (int (*)(const char *,struct statfs
*))SYS(statfs
);
309 struct sol_statfs
*ss
= (struct sol_statfs
*)A(buf
);
311 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
314 ret
= sys_statfs((const char *)A(path
), &s
);
317 if (put_user (s
.f_type
, &ss
->f_type
) ||
318 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
319 __put_user (0, &ss
->f_frsize
) ||
320 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
321 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
322 __put_user (s
.f_files
, &ss
->f_files
) ||
323 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
324 __clear_user (&ss
->f_fname
, 12))
329 /* Linux can't stat unmounted filesystems so we
330 * simply lie and claim 100MB of 1GB is free. Sorry.
332 if (put_user (fstype
, &ss
->f_type
) ||
333 __put_user (1024, &ss
->f_bsize
) ||
334 __put_user (0, &ss
->f_frsize
) ||
335 __put_user (1024*1024, &ss
->f_blocks
) ||
336 __put_user (100*1024, &ss
->f_bfree
) ||
337 __put_user (60000, &ss
->f_files
) ||
338 __put_user (50000, &ss
->f_ffree
) ||
339 __clear_user (&ss
->f_fname
, 12))
344 asmlinkage
int solaris_fstatfs(u32 fd
, u32 buf
, int len
, int fstype
)
348 mm_segment_t old_fs
= get_fs();
349 int (*sys_fstatfs
)(unsigned,struct statfs
*) =
350 (int (*)(unsigned,struct statfs
*))SYS(fstatfs
);
351 struct sol_statfs
*ss
= (struct sol_statfs
*)A(buf
);
353 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
356 ret
= sys_fstatfs(fd
, &s
);
359 if (put_user (s
.f_type
, &ss
->f_type
) ||
360 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
361 __put_user (0, &ss
->f_frsize
) ||
362 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
363 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
364 __put_user (s
.f_files
, &ss
->f_files
) ||
365 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
366 __clear_user (&ss
->f_fname
, 12))
371 /* Otherwise fstatfs is the same as statfs */
372 return solaris_statfs(0, buf
, len
, fstype
);
392 struct sol_statvfs64
{
409 static int report_statvfs(struct inode
*inode
, u32 buf
)
413 struct sol_statvfs
*ss
= (struct sol_statvfs
*)A(buf
);
415 error
= vfs_statfs(inode
->i_sb
, &s
);
417 const char *p
= inode
->i_sb
->s_type
->name
;
422 if (IS_RDONLY(inode
)) i
= 1;
423 if (IS_NOSUID(inode
)) i
|= 2;
424 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
425 __put_user (0, &ss
->f_frsize
) ||
426 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
427 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
428 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
429 __put_user (s
.f_files
, &ss
->f_files
) ||
430 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
431 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
432 __put_user (R4_DEV(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
433 __copy_to_user (ss
->f_basetype
,p
,j
) ||
434 __put_user (0, (char *)&ss
->f_basetype
[j
]) ||
435 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
436 __put_user (i
, &ss
->f_flag
) ||
437 __clear_user (&ss
->f_fstr
, 32))
443 static int report_statvfs64(struct inode
*inode
, u32 buf
)
447 struct sol_statvfs64
*ss
= (struct sol_statvfs64
*)A(buf
);
449 error
= vfs_statfs(inode
->i_sb
, &s
);
451 const char *p
= inode
->i_sb
->s_type
->name
;
456 if (IS_RDONLY(inode
)) i
= 1;
457 if (IS_NOSUID(inode
)) i
|= 2;
458 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
459 __put_user (0, &ss
->f_frsize
) ||
460 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
461 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
462 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
463 __put_user (s
.f_files
, &ss
->f_files
) ||
464 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
465 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
466 __put_user (R4_DEV(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
467 __copy_to_user (ss
->f_basetype
,p
,j
) ||
468 __put_user (0, (char *)&ss
->f_basetype
[j
]) ||
469 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
470 __put_user (i
, &ss
->f_flag
) ||
471 __clear_user (&ss
->f_fstr
, 32))
477 asmlinkage
int solaris_statvfs(u32 path
, u32 buf
)
482 error
= user_path_walk((const char *)A(path
),&nd
);
484 struct inode
* inode
= nd
.dentry
->d_inode
;
485 error
= report_statvfs(inode
, buf
);
491 asmlinkage
int solaris_fstatvfs(unsigned int fd
, u32 buf
)
499 error
= report_statvfs(file
->f_dentry
->d_inode
, buf
);
506 asmlinkage
int solaris_statvfs64(u32 path
, u32 buf
)
512 error
= user_path_walk((const char *)A(path
), &nd
);
514 struct inode
* inode
= nd
.dentry
->d_inode
;
515 error
= report_statvfs64(inode
, buf
);
522 asmlinkage
int solaris_fstatvfs64(unsigned int fd
, u32 buf
)
531 error
= report_statvfs64(file
->f_dentry
->d_inode
, buf
);
538 extern asmlinkage
long sparc32_open(const char * filename
, int flags
, int mode
);
540 asmlinkage
int solaris_open(u32 fname
, int flags
, u32 mode
)
542 const char *filename
= (const char *)(long)fname
;
543 int fl
= flags
& 0xf;
545 /* Translate flags first. */
546 if (flags
& 0x2000) fl
|= O_LARGEFILE
;
547 if (flags
& 0x8050) fl
|= O_SYNC
;
548 if (flags
& 0x80) fl
|= O_NONBLOCK
;
549 if (flags
& 0x100) fl
|= O_CREAT
;
550 if (flags
& 0x200) fl
|= O_TRUNC
;
551 if (flags
& 0x400) fl
|= O_EXCL
;
552 if (flags
& 0x800) fl
|= O_NOCTTY
;
555 return sparc32_open(filename
, flags
, mode
);
558 #define SOL_F_SETLK 6
559 #define SOL_F_SETLKW 7
560 #define SOL_F_FREESP 11
561 #define SOL_F_ISSTREAM 13
562 #define SOL_F_GETLK 14
563 #define SOL_F_PRIV 15
564 #define SOL_F_NPRIV 16
565 #define SOL_F_QUOTACTL 17
566 #define SOL_F_BLOCKS 18
567 #define SOL_F_BLKSIZE 19
568 #define SOL_F_GETOWN 23
569 #define SOL_F_SETOWN 24
581 asmlinkage
int solaris_fcntl(unsigned fd
, unsigned cmd
, u32 arg
)
583 int (*sys_fcntl
)(unsigned,unsigned,unsigned long) =
584 (int (*)(unsigned,unsigned,unsigned long))SYS(fcntl
);
590 case F_SETFD
: return sys_fcntl(fd
, cmd
, (unsigned long)arg
);
592 flags
= sys_fcntl(fd
, cmd
, 0);
594 if (flags
& O_SYNC
) ret
|= 0x8050;
595 if (flags
& O_NONBLOCK
) ret
|= 0x80;
599 if (arg
& 0x8050) flags
|= O_SYNC
;
600 if (arg
& 0x80) flags
|= O_NONBLOCK
;
601 return sys_fcntl(fd
, cmd
, (long)flags
);
607 mm_segment_t old_fs
= get_fs();
610 case SOL_F_GETLK
: cmd
= F_GETLK
; break;
611 case SOL_F_SETLK
: cmd
= F_SETLK
; break;
612 case SOL_F_SETLKW
: cmd
= F_SETLKW
; break;
615 if (get_user (f
.l_type
, &((struct sol_flock
*)A(arg
))->l_type
) ||
616 __get_user (f
.l_whence
, &((struct sol_flock
*)A(arg
))->l_whence
) ||
617 __get_user (f
.l_start
, &((struct sol_flock
*)A(arg
))->l_start
) ||
618 __get_user (f
.l_len
, &((struct sol_flock
*)A(arg
))->l_len
) ||
619 __get_user (f
.l_pid
, &((struct sol_flock
*)A(arg
))->l_sysid
))
623 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
626 if (__put_user (f
.l_type
, &((struct sol_flock
*)A(arg
))->l_type
) ||
627 __put_user (f
.l_whence
, &((struct sol_flock
*)A(arg
))->l_whence
) ||
628 __put_user (f
.l_start
, &((struct sol_flock
*)A(arg
))->l_start
) ||
629 __put_user (f
.l_len
, &((struct sol_flock
*)A(arg
))->l_len
) ||
630 __put_user (f
.l_pid
, &((struct sol_flock
*)A(arg
))->l_pid
) ||
631 __put_user (0, &((struct sol_flock
*)A(arg
))->l_sysid
))
639 int (*sys_newftruncate
)(unsigned int, unsigned long)=
640 (int (*)(unsigned int, unsigned long))SYS(ftruncate
);
642 if (get_user(length
, &((struct sol_flock
*)A(arg
))->l_start
))
645 return sys_newftruncate(fd
, length
);
651 asmlinkage
int solaris_ulimit(int cmd
, int val
)
654 case 1: /* UL_GETFSIZE - in 512B chunks */
655 return current
->rlim
[RLIMIT_FSIZE
].rlim_cur
>> 9;
656 case 2: /* UL_SETFSIZE */
657 if ((unsigned long)val
> (LONG_MAX
>>9)) return -ERANGE
;
660 if (val
> current
->rlim
[RLIMIT_FSIZE
].rlim_max
) {
661 if (!capable(CAP_SYS_RESOURCE
)) {
665 current
->rlim
[RLIMIT_FSIZE
].rlim_max
= val
;
667 current
->rlim
[RLIMIT_FSIZE
].rlim_cur
= val
;
670 case 3: /* UL_GMEMLIM */
671 return current
->rlim
[RLIMIT_DATA
].rlim_cur
;
672 case 4: /* UL_GDESLIM */
678 /* At least at the time I'm writing this, Linux doesn't have ACLs, so we
680 asmlinkage
int solaris_acl(u32 filename
, int cmd
, int nentries
, u32 aclbufp
)
685 asmlinkage
int solaris_facl(unsigned int fd
, int cmd
, int nentries
, u32 aclbufp
)
690 asmlinkage
int solaris_pread(unsigned int fd
, char *buf
, u32 count
, u32 pos
)
692 ssize_t (*sys_pread
)(unsigned int, char *, size_t, loff_t
) =
693 (ssize_t (*)(unsigned int, char *, size_t, loff_t
))SYS(pread
);
695 return sys_pread(fd
, buf
, count
, (loff_t
)pos
);
698 asmlinkage
int solaris_pwrite(unsigned int fd
, char *buf
, u32 count
, u32 pos
)
700 ssize_t (*sys_pwrite
)(unsigned int, char *, size_t, loff_t
) =
701 (ssize_t (*)(unsigned int, char *, size_t, loff_t
))SYS(pwrite
);
703 return sys_pwrite(fd
, buf
, count
, (loff_t
)pos
);
707 #define _PC_LINK_MAX 1
708 #define _PC_MAX_CANON 2
709 #define _PC_MAX_INPUT 3
710 #define _PC_NAME_MAX 4
711 #define _PC_PATH_MAX 5
712 #define _PC_PIPE_BUF 6
713 #define _PC_NO_TRUNC 7
714 #define _PC_VDISABLE 8
715 #define _PC_CHOWN_RESTRICTED 9
717 #define _PC_ASYNC_IO 10
718 #define _PC_PRIO_IO 11
719 #define _PC_SYNC_IO 12
722 /* This is not a real and complete implementation yet, just to keep
723 * the easy Solaris binaries happy.
725 asmlinkage
int solaris_fpathconf(int fd
, int name
)
748 case _PC_CHOWN_RESTRICTED
:
762 asmlinkage
int solaris_pathconf(u32 path
, int name
)
764 return solaris_fpathconf(0, name
);
767 /* solaris_llseek returns long long - quite difficult */
768 asmlinkage
long solaris_llseek(struct pt_regs
*regs
, u32 off_hi
, u32 off_lo
, int whence
)
770 int (*sys_llseek
)(unsigned int, unsigned long, unsigned long, loff_t
*, unsigned int) =
771 (int (*)(unsigned int, unsigned long, unsigned long, loff_t
*, unsigned int))SYS(_llseek
);
773 mm_segment_t old_fs
= get_fs();
777 ret
= sys_llseek((unsigned int)regs
->u_regs
[UREG_I0
], off_hi
, off_lo
, &retval
, whence
);
779 if (ret
< 0) return ret
;
780 regs
->u_regs
[UREG_I1
] = (u32
)retval
;
781 return (retval
>> 32);
784 /* Have to mask out all but lower 3 bits */
785 asmlinkage
int solaris_access(u32 filename
, long mode
)
787 int (*sys_access
)(const char *, int) =
788 (int (*)(const char *, int))SYS(access
);
790 return sys_access((const char *)A(filename
), mode
& 7);