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>
13 #include <linux/capability.h>
15 #include <linux/namei.h>
17 #include <linux/file.h>
18 #include <linux/stat.h>
19 #include <linux/smp_lock.h>
20 #include <linux/limits.h>
21 #include <linux/resource.h>
22 #include <linux/quotaops.h>
23 #include <linux/mount.h>
24 #include <linux/vfs.h>
26 #include <asm/uaccess.h>
27 #include <asm/string.h>
28 #include <asm/ptrace.h>
42 s32 st_pad1
[3]; /* network id */
51 s32 st_pad3
; /* st_size, off_t expansion */
52 timestruct_t st_atime
;
53 timestruct_t st_mtime
;
54 timestruct_t st_ctime
;
58 s32 st_pad4
[8]; /* expansion area */
63 s32 st_pad1
[3]; /* network id */
72 timestruct_t st_atime
;
73 timestruct_t st_mtime
;
74 timestruct_t st_ctime
;
78 s32 st_pad4
[4]; /* expansion area */
81 #define UFSMAGIC (((unsigned)'u'<<24)||((unsigned)'f'<<16)||((unsigned)'s'<<8))
83 static inline int putstat(struct sol_stat __user
*ubuf
, struct kstat
*kbuf
)
87 if (kbuf
->size
> MAX_NON_LFS
||
88 !sysv_valid_dev(kbuf
->dev
) ||
89 !sysv_valid_dev(kbuf
->rdev
))
92 if (sizeof(ino
) < sizeof(kbuf
->ino
) && ino
!= kbuf
->ino
)
94 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
95 __put_user (ino
, &ubuf
->st_ino
) ||
96 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
97 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
98 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
99 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
100 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
101 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
102 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
103 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
104 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
105 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
106 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
107 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
108 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
109 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
110 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
115 static inline int putstat64(struct sol_stat64 __user
*ubuf
, struct kstat
*kbuf
)
117 if (!sysv_valid_dev(kbuf
->dev
) || !sysv_valid_dev(kbuf
->rdev
))
119 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
120 __put_user (kbuf
->ino
, &ubuf
->st_ino
) ||
121 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
122 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
123 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
124 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
125 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
126 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
127 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
128 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
129 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
130 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
131 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
132 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
133 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
134 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
135 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
140 asmlinkage
int solaris_stat(u32 filename
, u32 statbuf
)
143 int ret
= vfs_stat(A(filename
), &s
);
145 return putstat(A(statbuf
), &s
);
149 asmlinkage
int solaris_xstat(int vers
, u32 filename
, u32 statbuf
)
151 /* Solaris doesn't bother with looking at vers, so we do neither */
152 return solaris_stat(filename
, statbuf
);
155 asmlinkage
int solaris_stat64(u32 filename
, u32 statbuf
)
158 int ret
= vfs_stat(A(filename
), &s
);
160 return putstat64(A(statbuf
), &s
);
164 asmlinkage
int solaris_lstat(u32 filename
, u32 statbuf
)
167 int ret
= vfs_lstat(A(filename
), &s
);
169 return putstat(A(statbuf
), &s
);
173 asmlinkage
int solaris_lxstat(int vers
, u32 filename
, u32 statbuf
)
175 return solaris_lstat(filename
, statbuf
);
178 asmlinkage
int solaris_lstat64(u32 filename
, u32 statbuf
)
181 int ret
= vfs_lstat(A(filename
), &s
);
183 return putstat64(A(statbuf
), &s
);
187 asmlinkage
int solaris_fstat(unsigned int fd
, u32 statbuf
)
190 int ret
= vfs_fstat(fd
, &s
);
192 return putstat(A(statbuf
), &s
);
196 asmlinkage
int solaris_fxstat(int vers
, u32 fd
, u32 statbuf
)
198 return solaris_fstat(fd
, statbuf
);
201 asmlinkage
int solaris_fstat64(unsigned int fd
, u32 statbuf
)
204 int ret
= vfs_fstat(fd
, &s
);
206 return putstat64(A(statbuf
), &s
);
210 asmlinkage
int solaris_mknod(u32 path
, u32 mode
, s32 dev
)
212 int (*sys_mknod
)(const char __user
*,int,unsigned) =
213 (int (*)(const char __user
*,int,unsigned))SYS(mknod
);
214 int major
= sysv_major(dev
);
215 int minor
= sysv_minor(dev
);
217 /* minor is guaranteed to be OK for MKDEV, major might be not */
220 return sys_mknod(A(path
), mode
, new_encode_dev(MKDEV(major
,minor
)));
223 asmlinkage
int solaris_xmknod(int vers
, u32 path
, u32 mode
, s32 dev
)
225 return solaris_mknod(path
, mode
, dev
);
228 asmlinkage
int solaris_getdents64(unsigned int fd
, void __user
*dirent
, unsigned int count
)
230 int (*sys_getdents
)(unsigned int, void __user
*, unsigned int) =
231 (int (*)(unsigned int, void __user
*, unsigned int))SYS(getdents
);
233 return sys_getdents(fd
, dirent
, count
);
236 /* This statfs thingie probably will go in the near future, but... */
250 asmlinkage
int solaris_statfs(u32 path
, u32 buf
, int len
, int fstype
)
254 mm_segment_t old_fs
= get_fs();
255 int (*sys_statfs
)(const char __user
*,struct statfs __user
*) =
256 (int (*)(const char __user
*,struct statfs __user
*))SYS(statfs
);
257 struct sol_statfs __user
*ss
= A(buf
);
259 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
261 /* FIXME: mixing userland and kernel pointers */
263 ret
= sys_statfs(A(path
), &s
);
266 if (put_user (s
.f_type
, &ss
->f_type
) ||
267 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
268 __put_user (0, &ss
->f_frsize
) ||
269 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
270 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
271 __put_user (s
.f_files
, &ss
->f_files
) ||
272 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
273 __clear_user (&ss
->f_fname
, 12))
278 /* Linux can't stat unmounted filesystems so we
279 * simply lie and claim 100MB of 1GB is free. Sorry.
281 if (put_user (fstype
, &ss
->f_type
) ||
282 __put_user (1024, &ss
->f_bsize
) ||
283 __put_user (0, &ss
->f_frsize
) ||
284 __put_user (1024*1024, &ss
->f_blocks
) ||
285 __put_user (100*1024, &ss
->f_bfree
) ||
286 __put_user (60000, &ss
->f_files
) ||
287 __put_user (50000, &ss
->f_ffree
) ||
288 __clear_user (&ss
->f_fname
, 12))
293 asmlinkage
int solaris_fstatfs(u32 fd
, u32 buf
, int len
, int fstype
)
297 mm_segment_t old_fs
= get_fs();
298 int (*sys_fstatfs
)(unsigned,struct statfs __user
*) =
299 (int (*)(unsigned,struct statfs __user
*))SYS(fstatfs
);
300 struct sol_statfs __user
*ss
= A(buf
);
302 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
305 ret
= sys_fstatfs(fd
, &s
);
308 if (put_user (s
.f_type
, &ss
->f_type
) ||
309 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
310 __put_user (0, &ss
->f_frsize
) ||
311 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
312 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
313 __put_user (s
.f_files
, &ss
->f_files
) ||
314 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
315 __clear_user (&ss
->f_fname
, 12))
320 /* Otherwise fstatfs is the same as statfs */
321 return solaris_statfs(0, buf
, len
, fstype
);
341 struct sol_statvfs64
{
358 static int report_statvfs(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
362 struct sol_statvfs __user
*ss
= A(buf
);
364 error
= vfs_statfs(mnt
->mnt_root
, &s
);
366 const char *p
= mnt
->mnt_sb
->s_type
->name
;
371 if (IS_RDONLY(inode
)) i
= 1;
372 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
373 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
375 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
376 __put_user (0, &ss
->f_frsize
) ||
377 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
378 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
379 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
380 __put_user (s
.f_files
, &ss
->f_files
) ||
381 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
382 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
383 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
384 __copy_to_user (ss
->f_basetype
,p
,j
) ||
385 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
386 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
387 __put_user (i
, &ss
->f_flag
) ||
388 __clear_user (&ss
->f_fstr
, 32))
394 static int report_statvfs64(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
398 struct sol_statvfs64 __user
*ss
= A(buf
);
400 error
= vfs_statfs(mnt
->mnt_root
, &s
);
402 const char *p
= mnt
->mnt_sb
->s_type
->name
;
407 if (IS_RDONLY(inode
)) i
= 1;
408 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
409 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
411 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
412 __put_user (0, &ss
->f_frsize
) ||
413 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
414 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
415 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
416 __put_user (s
.f_files
, &ss
->f_files
) ||
417 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
418 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
419 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
420 __copy_to_user (ss
->f_basetype
,p
,j
) ||
421 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
422 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
423 __put_user (i
, &ss
->f_flag
) ||
424 __clear_user (&ss
->f_fstr
, 32))
430 asmlinkage
int solaris_statvfs(u32 path
, u32 buf
)
435 error
= user_path_walk(A(path
),&nd
);
437 struct inode
* inode
= nd
.dentry
->d_inode
;
438 error
= report_statvfs(nd
.mnt
, inode
, buf
);
444 asmlinkage
int solaris_fstatvfs(unsigned int fd
, u32 buf
)
452 error
= report_statvfs(file
->f_path
.mnt
, file
->f_path
.dentry
->d_inode
, buf
);
459 asmlinkage
int solaris_statvfs64(u32 path
, u32 buf
)
465 error
= user_path_walk(A(path
), &nd
);
467 struct inode
* inode
= nd
.dentry
->d_inode
;
468 error
= report_statvfs64(nd
.mnt
, inode
, buf
);
475 asmlinkage
int solaris_fstatvfs64(unsigned int fd
, u32 buf
)
484 error
= report_statvfs64(file
->f_path
.mnt
, file
->f_path
.dentry
->d_inode
, buf
);
491 extern asmlinkage
long sparc32_open(const char * filename
, int flags
, int mode
);
493 asmlinkage
int solaris_open(u32 fname
, int flags
, u32 mode
)
495 const char *filename
= (const char *)(long)fname
;
496 int fl
= flags
& 0xf;
498 /* Translate flags first. */
499 if (flags
& 0x2000) fl
|= O_LARGEFILE
;
500 if (flags
& 0x8050) fl
|= O_SYNC
;
501 if (flags
& 0x80) fl
|= O_NONBLOCK
;
502 if (flags
& 0x100) fl
|= O_CREAT
;
503 if (flags
& 0x200) fl
|= O_TRUNC
;
504 if (flags
& 0x400) fl
|= O_EXCL
;
505 if (flags
& 0x800) fl
|= O_NOCTTY
;
508 return sparc32_open(filename
, flags
, mode
);
511 #define SOL_F_SETLK 6
512 #define SOL_F_SETLKW 7
513 #define SOL_F_FREESP 11
514 #define SOL_F_ISSTREAM 13
515 #define SOL_F_GETLK 14
516 #define SOL_F_PRIV 15
517 #define SOL_F_NPRIV 16
518 #define SOL_F_QUOTACTL 17
519 #define SOL_F_BLOCKS 18
520 #define SOL_F_BLKSIZE 19
521 #define SOL_F_GETOWN 23
522 #define SOL_F_SETOWN 24
534 asmlinkage
int solaris_fcntl(unsigned fd
, unsigned cmd
, u32 arg
)
536 int (*sys_fcntl
)(unsigned,unsigned,unsigned long) =
537 (int (*)(unsigned,unsigned,unsigned long))SYS(fcntl
);
543 case F_SETFD
: return sys_fcntl(fd
, cmd
, (unsigned long)arg
);
545 flags
= sys_fcntl(fd
, cmd
, 0);
547 if (flags
& O_SYNC
) ret
|= 0x8050;
548 if (flags
& O_NONBLOCK
) ret
|= 0x80;
552 if (arg
& 0x8050) flags
|= O_SYNC
;
553 if (arg
& 0x80) flags
|= O_NONBLOCK
;
554 return sys_fcntl(fd
, cmd
, (long)flags
);
560 struct sol_flock __user
*p
= A(arg
);
561 mm_segment_t old_fs
= get_fs();
564 case SOL_F_GETLK
: cmd
= F_GETLK
; break;
565 case SOL_F_SETLK
: cmd
= F_SETLK
; break;
566 case SOL_F_SETLKW
: cmd
= F_SETLKW
; break;
569 if (get_user (f
.l_type
, &p
->l_type
) ||
570 __get_user (f
.l_whence
, &p
->l_whence
) ||
571 __get_user (f
.l_start
, &p
->l_start
) ||
572 __get_user (f
.l_len
, &p
->l_len
) ||
573 __get_user (f
.l_pid
, &p
->l_sysid
))
577 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
580 if (__put_user (f
.l_type
, &p
->l_type
) ||
581 __put_user (f
.l_whence
, &p
->l_whence
) ||
582 __put_user (f
.l_start
, &p
->l_start
) ||
583 __put_user (f
.l_len
, &p
->l_len
) ||
584 __put_user (f
.l_pid
, &p
->l_pid
) ||
585 __put_user (0, &p
->l_sysid
))
593 int (*sys_newftruncate
)(unsigned int, unsigned long)=
594 (int (*)(unsigned int, unsigned long))SYS(ftruncate
);
596 if (get_user(length
, &((struct sol_flock __user
*)A(arg
))->l_start
))
599 return sys_newftruncate(fd
, length
);
605 asmlinkage
int solaris_ulimit(int cmd
, int val
)
608 case 1: /* UL_GETFSIZE - in 512B chunks */
609 return current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
>> 9;
610 case 2: /* UL_SETFSIZE */
611 if ((unsigned long)val
> (LONG_MAX
>>9)) return -ERANGE
;
613 task_lock(current
->group_leader
);
614 if (val
> current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_max
) {
615 if (!capable(CAP_SYS_RESOURCE
)) {
616 task_unlock(current
->group_leader
);
619 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_max
= val
;
621 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= val
;
622 task_unlock(current
->group_leader
);
624 case 3: /* UL_GMEMLIM */
625 return current
->signal
->rlim
[RLIMIT_DATA
].rlim_cur
;
626 case 4: /* UL_GDESLIM */
632 /* At least at the time I'm writing this, Linux doesn't have ACLs, so we
634 asmlinkage
int solaris_acl(u32 filename
, int cmd
, int nentries
, u32 aclbufp
)
639 asmlinkage
int solaris_facl(unsigned int fd
, int cmd
, int nentries
, u32 aclbufp
)
644 asmlinkage
int solaris_pread(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
646 ssize_t (*sys_pread64
)(unsigned int, char __user
*, size_t, loff_t
) =
647 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pread64
);
649 return sys_pread64(fd
, buf
, count
, (loff_t
)pos
);
652 asmlinkage
int solaris_pwrite(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
654 ssize_t (*sys_pwrite64
)(unsigned int, char __user
*, size_t, loff_t
) =
655 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pwrite64
);
657 return sys_pwrite64(fd
, buf
, count
, (loff_t
)pos
);
661 #define _PC_LINK_MAX 1
662 #define _PC_MAX_CANON 2
663 #define _PC_MAX_INPUT 3
664 #define _PC_NAME_MAX 4
665 #define _PC_PATH_MAX 5
666 #define _PC_PIPE_BUF 6
667 #define _PC_NO_TRUNC 7
668 #define _PC_VDISABLE 8
669 #define _PC_CHOWN_RESTRICTED 9
671 #define _PC_ASYNC_IO 10
672 #define _PC_PRIO_IO 11
673 #define _PC_SYNC_IO 12
676 /* This is not a real and complete implementation yet, just to keep
677 * the easy Solaris binaries happy.
679 asmlinkage
int solaris_fpathconf(int fd
, int name
)
702 case _PC_CHOWN_RESTRICTED
:
716 asmlinkage
int solaris_pathconf(u32 path
, int name
)
718 return solaris_fpathconf(0, name
);
721 /* solaris_llseek returns long long - quite difficult */
722 asmlinkage
long solaris_llseek(struct pt_regs
*regs
, u32 off_hi
, u32 off_lo
, int whence
)
724 int (*sys_llseek
)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int) =
725 (int (*)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int))SYS(_llseek
);
727 mm_segment_t old_fs
= get_fs();
731 ret
= sys_llseek((unsigned int)regs
->u_regs
[UREG_I0
], off_hi
, off_lo
, &retval
, whence
);
733 if (ret
< 0) return ret
;
734 regs
->u_regs
[UREG_I1
] = (u32
)retval
;
735 return (retval
>> 32);
738 /* Have to mask out all but lower 3 bits */
739 asmlinkage
int solaris_access(u32 filename
, long mode
)
741 int (*sys_access
)(const char __user
*, int) =
742 (int (*)(const char __user
*, int))SYS(access
);
744 return sys_access(A(filename
), mode
& 7);