1 /* $Id: fs.c,v 1.27 2002/02/08 03:57:14 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/slab.h>
14 #include <linux/namei.h>
16 #include <linux/file.h>
17 #include <linux/stat.h>
18 #include <linux/smp_lock.h>
19 #include <linux/limits.h>
20 #include <linux/resource.h>
21 #include <linux/quotaops.h>
22 #include <linux/mount.h>
23 #include <linux/vfs.h>
25 #include <asm/uaccess.h>
26 #include <asm/string.h>
27 #include <asm/ptrace.h>
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 __user
*ubuf
, struct kstat
*kbuf
)
84 if (kbuf
->size
> MAX_NON_LFS
||
85 !sysv_valid_dev(kbuf
->dev
) ||
86 !sysv_valid_dev(kbuf
->rdev
))
88 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
89 __put_user (kbuf
->ino
, &ubuf
->st_ino
) ||
90 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
91 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
92 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
93 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
94 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
95 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
96 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
97 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
98 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
99 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
100 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
101 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
102 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
103 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
104 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
109 static inline int putstat64(struct sol_stat64 __user
*ubuf
, struct kstat
*kbuf
)
111 if (!sysv_valid_dev(kbuf
->dev
) || !sysv_valid_dev(kbuf
->rdev
))
113 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
114 __put_user (kbuf
->ino
, &ubuf
->st_ino
) ||
115 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
116 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
117 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
118 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
119 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
120 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
121 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
122 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
123 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
124 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
125 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
126 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
127 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
128 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
129 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
134 asmlinkage
int solaris_stat(u32 filename
, u32 statbuf
)
137 int ret
= vfs_stat(A(filename
), &s
);
139 return putstat(A(statbuf
), &s
);
143 asmlinkage
int solaris_xstat(int vers
, u32 filename
, u32 statbuf
)
145 /* Solaris doesn't bother with looking at vers, so we do neither */
146 return solaris_stat(filename
, statbuf
);
149 asmlinkage
int solaris_stat64(u32 filename
, u32 statbuf
)
152 int ret
= vfs_stat(A(filename
), &s
);
154 return putstat64(A(statbuf
), &s
);
158 asmlinkage
int solaris_lstat(u32 filename
, u32 statbuf
)
161 int ret
= vfs_lstat(A(filename
), &s
);
163 return putstat(A(statbuf
), &s
);
167 asmlinkage
int solaris_lxstat(int vers
, u32 filename
, u32 statbuf
)
169 return solaris_lstat(filename
, statbuf
);
172 asmlinkage
int solaris_lstat64(u32 filename
, u32 statbuf
)
175 int ret
= vfs_lstat(A(filename
), &s
);
177 return putstat64(A(statbuf
), &s
);
181 asmlinkage
int solaris_fstat(unsigned int fd
, u32 statbuf
)
184 int ret
= vfs_fstat(fd
, &s
);
186 return putstat(A(statbuf
), &s
);
190 asmlinkage
int solaris_fxstat(int vers
, u32 fd
, u32 statbuf
)
192 return solaris_fstat(fd
, statbuf
);
195 asmlinkage
int solaris_fstat64(unsigned int fd
, u32 statbuf
)
198 int ret
= vfs_fstat(fd
, &s
);
200 return putstat64(A(statbuf
), &s
);
204 asmlinkage
int solaris_mknod(u32 path
, u32 mode
, s32 dev
)
206 int (*sys_mknod
)(const char __user
*,int,unsigned) =
207 (int (*)(const char __user
*,int,unsigned))SYS(mknod
);
208 int major
= sysv_major(dev
);
209 int minor
= sysv_minor(dev
);
211 /* minor is guaranteed to be OK for MKDEV, major might be not */
214 return sys_mknod(A(path
), mode
, new_encode_dev(MKDEV(major
,minor
)));
217 asmlinkage
int solaris_xmknod(int vers
, u32 path
, u32 mode
, s32 dev
)
219 return solaris_mknod(path
, mode
, dev
);
222 asmlinkage
int solaris_getdents64(unsigned int fd
, void __user
*dirent
, unsigned int count
)
224 int (*sys_getdents
)(unsigned int, void __user
*, unsigned int) =
225 (int (*)(unsigned int, void __user
*, unsigned int))SYS(getdents
);
227 return sys_getdents(fd
, dirent
, count
);
230 /* This statfs thingie probably will go in the near future, but... */
244 asmlinkage
int solaris_statfs(u32 path
, u32 buf
, int len
, int fstype
)
248 mm_segment_t old_fs
= get_fs();
249 int (*sys_statfs
)(const char __user
*,struct statfs __user
*) =
250 (int (*)(const char __user
*,struct statfs __user
*))SYS(statfs
);
251 struct sol_statfs __user
*ss
= A(buf
);
253 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
255 /* FIXME: mixing userland and kernel pointers */
257 ret
= sys_statfs(A(path
), &s
);
260 if (put_user (s
.f_type
, &ss
->f_type
) ||
261 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
262 __put_user (0, &ss
->f_frsize
) ||
263 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
264 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
265 __put_user (s
.f_files
, &ss
->f_files
) ||
266 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
267 __clear_user (&ss
->f_fname
, 12))
272 /* Linux can't stat unmounted filesystems so we
273 * simply lie and claim 100MB of 1GB is free. Sorry.
275 if (put_user (fstype
, &ss
->f_type
) ||
276 __put_user (1024, &ss
->f_bsize
) ||
277 __put_user (0, &ss
->f_frsize
) ||
278 __put_user (1024*1024, &ss
->f_blocks
) ||
279 __put_user (100*1024, &ss
->f_bfree
) ||
280 __put_user (60000, &ss
->f_files
) ||
281 __put_user (50000, &ss
->f_ffree
) ||
282 __clear_user (&ss
->f_fname
, 12))
287 asmlinkage
int solaris_fstatfs(u32 fd
, u32 buf
, int len
, int fstype
)
291 mm_segment_t old_fs
= get_fs();
292 int (*sys_fstatfs
)(unsigned,struct statfs __user
*) =
293 (int (*)(unsigned,struct statfs __user
*))SYS(fstatfs
);
294 struct sol_statfs __user
*ss
= A(buf
);
296 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
299 ret
= sys_fstatfs(fd
, &s
);
302 if (put_user (s
.f_type
, &ss
->f_type
) ||
303 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
304 __put_user (0, &ss
->f_frsize
) ||
305 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
306 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
307 __put_user (s
.f_files
, &ss
->f_files
) ||
308 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
309 __clear_user (&ss
->f_fname
, 12))
314 /* Otherwise fstatfs is the same as statfs */
315 return solaris_statfs(0, buf
, len
, fstype
);
335 struct sol_statvfs64
{
352 static int report_statvfs(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
356 struct sol_statvfs __user
*ss
= A(buf
);
358 error
= vfs_statfs(mnt
->mnt_sb
, &s
);
360 const char *p
= mnt
->mnt_sb
->s_type
->name
;
365 if (IS_RDONLY(inode
)) i
= 1;
366 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
367 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
369 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
370 __put_user (0, &ss
->f_frsize
) ||
371 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
372 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
373 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
374 __put_user (s
.f_files
, &ss
->f_files
) ||
375 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
376 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
377 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
378 __copy_to_user (ss
->f_basetype
,p
,j
) ||
379 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
380 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
381 __put_user (i
, &ss
->f_flag
) ||
382 __clear_user (&ss
->f_fstr
, 32))
388 static int report_statvfs64(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
392 struct sol_statvfs64 __user
*ss
= A(buf
);
394 error
= vfs_statfs(mnt
->mnt_sb
, &s
);
396 const char *p
= mnt
->mnt_sb
->s_type
->name
;
401 if (IS_RDONLY(inode
)) i
= 1;
402 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
403 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
405 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
406 __put_user (0, &ss
->f_frsize
) ||
407 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
408 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
409 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
410 __put_user (s
.f_files
, &ss
->f_files
) ||
411 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
412 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
413 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
414 __copy_to_user (ss
->f_basetype
,p
,j
) ||
415 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
416 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
417 __put_user (i
, &ss
->f_flag
) ||
418 __clear_user (&ss
->f_fstr
, 32))
424 asmlinkage
int solaris_statvfs(u32 path
, u32 buf
)
429 error
= user_path_walk(A(path
),&nd
);
431 struct inode
* inode
= nd
.dentry
->d_inode
;
432 error
= report_statvfs(nd
.mnt
, inode
, buf
);
438 asmlinkage
int solaris_fstatvfs(unsigned int fd
, u32 buf
)
446 error
= report_statvfs(file
->f_vfsmnt
, file
->f_dentry
->d_inode
, buf
);
453 asmlinkage
int solaris_statvfs64(u32 path
, u32 buf
)
459 error
= user_path_walk(A(path
), &nd
);
461 struct inode
* inode
= nd
.dentry
->d_inode
;
462 error
= report_statvfs64(nd
.mnt
, inode
, buf
);
469 asmlinkage
int solaris_fstatvfs64(unsigned int fd
, u32 buf
)
478 error
= report_statvfs64(file
->f_vfsmnt
, file
->f_dentry
->d_inode
, buf
);
485 extern asmlinkage
long sparc32_open(const char * filename
, int flags
, int mode
);
487 asmlinkage
int solaris_open(u32 fname
, int flags
, u32 mode
)
489 const char *filename
= (const char *)(long)fname
;
490 int fl
= flags
& 0xf;
492 /* Translate flags first. */
493 if (flags
& 0x2000) fl
|= O_LARGEFILE
;
494 if (flags
& 0x8050) fl
|= O_SYNC
;
495 if (flags
& 0x80) fl
|= O_NONBLOCK
;
496 if (flags
& 0x100) fl
|= O_CREAT
;
497 if (flags
& 0x200) fl
|= O_TRUNC
;
498 if (flags
& 0x400) fl
|= O_EXCL
;
499 if (flags
& 0x800) fl
|= O_NOCTTY
;
502 return sparc32_open(filename
, flags
, mode
);
505 #define SOL_F_SETLK 6
506 #define SOL_F_SETLKW 7
507 #define SOL_F_FREESP 11
508 #define SOL_F_ISSTREAM 13
509 #define SOL_F_GETLK 14
510 #define SOL_F_PRIV 15
511 #define SOL_F_NPRIV 16
512 #define SOL_F_QUOTACTL 17
513 #define SOL_F_BLOCKS 18
514 #define SOL_F_BLKSIZE 19
515 #define SOL_F_GETOWN 23
516 #define SOL_F_SETOWN 24
528 asmlinkage
int solaris_fcntl(unsigned fd
, unsigned cmd
, u32 arg
)
530 int (*sys_fcntl
)(unsigned,unsigned,unsigned long) =
531 (int (*)(unsigned,unsigned,unsigned long))SYS(fcntl
);
537 case F_SETFD
: return sys_fcntl(fd
, cmd
, (unsigned long)arg
);
539 flags
= sys_fcntl(fd
, cmd
, 0);
541 if (flags
& O_SYNC
) ret
|= 0x8050;
542 if (flags
& O_NONBLOCK
) ret
|= 0x80;
546 if (arg
& 0x8050) flags
|= O_SYNC
;
547 if (arg
& 0x80) flags
|= O_NONBLOCK
;
548 return sys_fcntl(fd
, cmd
, (long)flags
);
554 struct sol_flock __user
*p
= A(arg
);
555 mm_segment_t old_fs
= get_fs();
558 case SOL_F_GETLK
: cmd
= F_GETLK
; break;
559 case SOL_F_SETLK
: cmd
= F_SETLK
; break;
560 case SOL_F_SETLKW
: cmd
= F_SETLKW
; break;
563 if (get_user (f
.l_type
, &p
->l_type
) ||
564 __get_user (f
.l_whence
, &p
->l_whence
) ||
565 __get_user (f
.l_start
, &p
->l_start
) ||
566 __get_user (f
.l_len
, &p
->l_len
) ||
567 __get_user (f
.l_pid
, &p
->l_sysid
))
571 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
574 if (__put_user (f
.l_type
, &p
->l_type
) ||
575 __put_user (f
.l_whence
, &p
->l_whence
) ||
576 __put_user (f
.l_start
, &p
->l_start
) ||
577 __put_user (f
.l_len
, &p
->l_len
) ||
578 __put_user (f
.l_pid
, &p
->l_pid
) ||
579 __put_user (0, &p
->l_sysid
))
587 int (*sys_newftruncate
)(unsigned int, unsigned long)=
588 (int (*)(unsigned int, unsigned long))SYS(ftruncate
);
590 if (get_user(length
, &((struct sol_flock __user
*)A(arg
))->l_start
))
593 return sys_newftruncate(fd
, length
);
599 asmlinkage
int solaris_ulimit(int cmd
, int val
)
602 case 1: /* UL_GETFSIZE - in 512B chunks */
603 return current
->rlim
[RLIMIT_FSIZE
].rlim_cur
>> 9;
604 case 2: /* UL_SETFSIZE */
605 if ((unsigned long)val
> (LONG_MAX
>>9)) return -ERANGE
;
608 if (val
> current
->rlim
[RLIMIT_FSIZE
].rlim_max
) {
609 if (!capable(CAP_SYS_RESOURCE
)) {
613 current
->rlim
[RLIMIT_FSIZE
].rlim_max
= val
;
615 current
->rlim
[RLIMIT_FSIZE
].rlim_cur
= val
;
618 case 3: /* UL_GMEMLIM */
619 return current
->rlim
[RLIMIT_DATA
].rlim_cur
;
620 case 4: /* UL_GDESLIM */
626 /* At least at the time I'm writing this, Linux doesn't have ACLs, so we
628 asmlinkage
int solaris_acl(u32 filename
, int cmd
, int nentries
, u32 aclbufp
)
633 asmlinkage
int solaris_facl(unsigned int fd
, int cmd
, int nentries
, u32 aclbufp
)
638 asmlinkage
int solaris_pread(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
640 ssize_t (*sys_pread64
)(unsigned int, char __user
*, size_t, loff_t
) =
641 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pread64
);
643 return sys_pread64(fd
, buf
, count
, (loff_t
)pos
);
646 asmlinkage
int solaris_pwrite(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
648 ssize_t (*sys_pwrite64
)(unsigned int, char __user
*, size_t, loff_t
) =
649 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pwrite64
);
651 return sys_pwrite64(fd
, buf
, count
, (loff_t
)pos
);
655 #define _PC_LINK_MAX 1
656 #define _PC_MAX_CANON 2
657 #define _PC_MAX_INPUT 3
658 #define _PC_NAME_MAX 4
659 #define _PC_PATH_MAX 5
660 #define _PC_PIPE_BUF 6
661 #define _PC_NO_TRUNC 7
662 #define _PC_VDISABLE 8
663 #define _PC_CHOWN_RESTRICTED 9
665 #define _PC_ASYNC_IO 10
666 #define _PC_PRIO_IO 11
667 #define _PC_SYNC_IO 12
670 /* This is not a real and complete implementation yet, just to keep
671 * the easy Solaris binaries happy.
673 asmlinkage
int solaris_fpathconf(int fd
, int name
)
696 case _PC_CHOWN_RESTRICTED
:
710 asmlinkage
int solaris_pathconf(u32 path
, int name
)
712 return solaris_fpathconf(0, name
);
715 /* solaris_llseek returns long long - quite difficult */
716 asmlinkage
long solaris_llseek(struct pt_regs
*regs
, u32 off_hi
, u32 off_lo
, int whence
)
718 int (*sys_llseek
)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int) =
719 (int (*)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int))SYS(_llseek
);
721 mm_segment_t old_fs
= get_fs();
725 ret
= sys_llseek((unsigned int)regs
->u_regs
[UREG_I0
], off_hi
, off_lo
, &retval
, whence
);
727 if (ret
< 0) return ret
;
728 regs
->u_regs
[UREG_I1
] = (u32
)retval
;
729 return (retval
>> 32);
732 /* Have to mask out all but lower 3 bits */
733 asmlinkage
int solaris_access(u32 filename
, long mode
)
735 int (*sys_access
)(const char __user
*, int) =
736 (int (*)(const char __user
*, int))SYS(access
);
738 return sys_access(A(filename
), mode
& 7);