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
)
85 if (kbuf
->size
> MAX_NON_LFS
||
86 !sysv_valid_dev(kbuf
->dev
) ||
87 !sysv_valid_dev(kbuf
->rdev
))
89 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
90 __put_user (kbuf
->ino
, &ubuf
->st_ino
) ||
91 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
92 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
93 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
94 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
95 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
96 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
97 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
98 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
99 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
100 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
101 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
102 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
103 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
104 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
105 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
110 static inline int putstat64(struct sol_stat64 __user
*ubuf
, struct kstat
*kbuf
)
112 if (!sysv_valid_dev(kbuf
->dev
) || !sysv_valid_dev(kbuf
->rdev
))
114 if (put_user (sysv_encode_dev(kbuf
->dev
), &ubuf
->st_dev
) ||
115 __put_user (kbuf
->ino
, &ubuf
->st_ino
) ||
116 __put_user (kbuf
->mode
, &ubuf
->st_mode
) ||
117 __put_user (kbuf
->nlink
, &ubuf
->st_nlink
) ||
118 __put_user (kbuf
->uid
, &ubuf
->st_uid
) ||
119 __put_user (kbuf
->gid
, &ubuf
->st_gid
) ||
120 __put_user (sysv_encode_dev(kbuf
->rdev
), &ubuf
->st_rdev
) ||
121 __put_user (kbuf
->size
, &ubuf
->st_size
) ||
122 __put_user (kbuf
->atime
.tv_sec
, &ubuf
->st_atime
.tv_sec
) ||
123 __put_user (kbuf
->atime
.tv_nsec
, &ubuf
->st_atime
.tv_nsec
) ||
124 __put_user (kbuf
->mtime
.tv_sec
, &ubuf
->st_mtime
.tv_sec
) ||
125 __put_user (kbuf
->mtime
.tv_nsec
, &ubuf
->st_mtime
.tv_nsec
) ||
126 __put_user (kbuf
->ctime
.tv_sec
, &ubuf
->st_ctime
.tv_sec
) ||
127 __put_user (kbuf
->ctime
.tv_nsec
, &ubuf
->st_ctime
.tv_nsec
) ||
128 __put_user (kbuf
->blksize
, &ubuf
->st_blksize
) ||
129 __put_user (kbuf
->blocks
, &ubuf
->st_blocks
) ||
130 __put_user (UFSMAGIC
, (unsigned __user
*)ubuf
->st_fstype
))
135 asmlinkage
int solaris_stat(u32 filename
, u32 statbuf
)
138 int ret
= vfs_stat(A(filename
), &s
);
140 return putstat(A(statbuf
), &s
);
144 asmlinkage
int solaris_xstat(int vers
, u32 filename
, u32 statbuf
)
146 /* Solaris doesn't bother with looking at vers, so we do neither */
147 return solaris_stat(filename
, statbuf
);
150 asmlinkage
int solaris_stat64(u32 filename
, u32 statbuf
)
153 int ret
= vfs_stat(A(filename
), &s
);
155 return putstat64(A(statbuf
), &s
);
159 asmlinkage
int solaris_lstat(u32 filename
, u32 statbuf
)
162 int ret
= vfs_lstat(A(filename
), &s
);
164 return putstat(A(statbuf
), &s
);
168 asmlinkage
int solaris_lxstat(int vers
, u32 filename
, u32 statbuf
)
170 return solaris_lstat(filename
, statbuf
);
173 asmlinkage
int solaris_lstat64(u32 filename
, u32 statbuf
)
176 int ret
= vfs_lstat(A(filename
), &s
);
178 return putstat64(A(statbuf
), &s
);
182 asmlinkage
int solaris_fstat(unsigned int fd
, u32 statbuf
)
185 int ret
= vfs_fstat(fd
, &s
);
187 return putstat(A(statbuf
), &s
);
191 asmlinkage
int solaris_fxstat(int vers
, u32 fd
, u32 statbuf
)
193 return solaris_fstat(fd
, statbuf
);
196 asmlinkage
int solaris_fstat64(unsigned int fd
, u32 statbuf
)
199 int ret
= vfs_fstat(fd
, &s
);
201 return putstat64(A(statbuf
), &s
);
205 asmlinkage
int solaris_mknod(u32 path
, u32 mode
, s32 dev
)
207 int (*sys_mknod
)(const char __user
*,int,unsigned) =
208 (int (*)(const char __user
*,int,unsigned))SYS(mknod
);
209 int major
= sysv_major(dev
);
210 int minor
= sysv_minor(dev
);
212 /* minor is guaranteed to be OK for MKDEV, major might be not */
215 return sys_mknod(A(path
), mode
, new_encode_dev(MKDEV(major
,minor
)));
218 asmlinkage
int solaris_xmknod(int vers
, u32 path
, u32 mode
, s32 dev
)
220 return solaris_mknod(path
, mode
, dev
);
223 asmlinkage
int solaris_getdents64(unsigned int fd
, void __user
*dirent
, unsigned int count
)
225 int (*sys_getdents
)(unsigned int, void __user
*, unsigned int) =
226 (int (*)(unsigned int, void __user
*, unsigned int))SYS(getdents
);
228 return sys_getdents(fd
, dirent
, count
);
231 /* This statfs thingie probably will go in the near future, but... */
245 asmlinkage
int solaris_statfs(u32 path
, u32 buf
, int len
, int fstype
)
249 mm_segment_t old_fs
= get_fs();
250 int (*sys_statfs
)(const char __user
*,struct statfs __user
*) =
251 (int (*)(const char __user
*,struct statfs __user
*))SYS(statfs
);
252 struct sol_statfs __user
*ss
= A(buf
);
254 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
256 /* FIXME: mixing userland and kernel pointers */
258 ret
= sys_statfs(A(path
), &s
);
261 if (put_user (s
.f_type
, &ss
->f_type
) ||
262 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
263 __put_user (0, &ss
->f_frsize
) ||
264 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
265 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
266 __put_user (s
.f_files
, &ss
->f_files
) ||
267 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
268 __clear_user (&ss
->f_fname
, 12))
273 /* Linux can't stat unmounted filesystems so we
274 * simply lie and claim 100MB of 1GB is free. Sorry.
276 if (put_user (fstype
, &ss
->f_type
) ||
277 __put_user (1024, &ss
->f_bsize
) ||
278 __put_user (0, &ss
->f_frsize
) ||
279 __put_user (1024*1024, &ss
->f_blocks
) ||
280 __put_user (100*1024, &ss
->f_bfree
) ||
281 __put_user (60000, &ss
->f_files
) ||
282 __put_user (50000, &ss
->f_ffree
) ||
283 __clear_user (&ss
->f_fname
, 12))
288 asmlinkage
int solaris_fstatfs(u32 fd
, u32 buf
, int len
, int fstype
)
292 mm_segment_t old_fs
= get_fs();
293 int (*sys_fstatfs
)(unsigned,struct statfs __user
*) =
294 (int (*)(unsigned,struct statfs __user
*))SYS(fstatfs
);
295 struct sol_statfs __user
*ss
= A(buf
);
297 if (len
!= sizeof(struct sol_statfs
)) return -EINVAL
;
300 ret
= sys_fstatfs(fd
, &s
);
303 if (put_user (s
.f_type
, &ss
->f_type
) ||
304 __put_user (s
.f_bsize
, &ss
->f_bsize
) ||
305 __put_user (0, &ss
->f_frsize
) ||
306 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
307 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
308 __put_user (s
.f_files
, &ss
->f_files
) ||
309 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
310 __clear_user (&ss
->f_fname
, 12))
315 /* Otherwise fstatfs is the same as statfs */
316 return solaris_statfs(0, buf
, len
, fstype
);
336 struct sol_statvfs64
{
353 static int report_statvfs(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
357 struct sol_statvfs __user
*ss
= A(buf
);
359 error
= vfs_statfs(mnt
->mnt_sb
, &s
);
361 const char *p
= mnt
->mnt_sb
->s_type
->name
;
366 if (IS_RDONLY(inode
)) i
= 1;
367 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
368 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
370 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
371 __put_user (0, &ss
->f_frsize
) ||
372 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
373 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
374 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
375 __put_user (s
.f_files
, &ss
->f_files
) ||
376 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
377 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
378 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
379 __copy_to_user (ss
->f_basetype
,p
,j
) ||
380 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
381 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
382 __put_user (i
, &ss
->f_flag
) ||
383 __clear_user (&ss
->f_fstr
, 32))
389 static int report_statvfs64(struct vfsmount
*mnt
, struct inode
*inode
, u32 buf
)
393 struct sol_statvfs64 __user
*ss
= A(buf
);
395 error
= vfs_statfs(mnt
->mnt_sb
, &s
);
397 const char *p
= mnt
->mnt_sb
->s_type
->name
;
402 if (IS_RDONLY(inode
)) i
= 1;
403 if (mnt
->mnt_flags
& MNT_NOSUID
) i
|= 2;
404 if (!sysv_valid_dev(inode
->i_sb
->s_dev
))
406 if (put_user (s
.f_bsize
, &ss
->f_bsize
) ||
407 __put_user (0, &ss
->f_frsize
) ||
408 __put_user (s
.f_blocks
, &ss
->f_blocks
) ||
409 __put_user (s
.f_bfree
, &ss
->f_bfree
) ||
410 __put_user (s
.f_bavail
, &ss
->f_bavail
) ||
411 __put_user (s
.f_files
, &ss
->f_files
) ||
412 __put_user (s
.f_ffree
, &ss
->f_ffree
) ||
413 __put_user (s
.f_ffree
, &ss
->f_favail
) ||
414 __put_user (sysv_encode_dev(inode
->i_sb
->s_dev
), &ss
->f_fsid
) ||
415 __copy_to_user (ss
->f_basetype
,p
,j
) ||
416 __put_user (0, (char __user
*)&ss
->f_basetype
[j
]) ||
417 __put_user (s
.f_namelen
, &ss
->f_namemax
) ||
418 __put_user (i
, &ss
->f_flag
) ||
419 __clear_user (&ss
->f_fstr
, 32))
425 asmlinkage
int solaris_statvfs(u32 path
, u32 buf
)
430 error
= user_path_walk(A(path
),&nd
);
432 struct inode
* inode
= nd
.dentry
->d_inode
;
433 error
= report_statvfs(nd
.mnt
, inode
, buf
);
439 asmlinkage
int solaris_fstatvfs(unsigned int fd
, u32 buf
)
447 error
= report_statvfs(file
->f_vfsmnt
, file
->f_dentry
->d_inode
, buf
);
454 asmlinkage
int solaris_statvfs64(u32 path
, u32 buf
)
460 error
= user_path_walk(A(path
), &nd
);
462 struct inode
* inode
= nd
.dentry
->d_inode
;
463 error
= report_statvfs64(nd
.mnt
, inode
, buf
);
470 asmlinkage
int solaris_fstatvfs64(unsigned int fd
, u32 buf
)
479 error
= report_statvfs64(file
->f_vfsmnt
, file
->f_dentry
->d_inode
, buf
);
486 extern asmlinkage
long sparc32_open(const char * filename
, int flags
, int mode
);
488 asmlinkage
int solaris_open(u32 fname
, int flags
, u32 mode
)
490 const char *filename
= (const char *)(long)fname
;
491 int fl
= flags
& 0xf;
493 /* Translate flags first. */
494 if (flags
& 0x2000) fl
|= O_LARGEFILE
;
495 if (flags
& 0x8050) fl
|= O_SYNC
;
496 if (flags
& 0x80) fl
|= O_NONBLOCK
;
497 if (flags
& 0x100) fl
|= O_CREAT
;
498 if (flags
& 0x200) fl
|= O_TRUNC
;
499 if (flags
& 0x400) fl
|= O_EXCL
;
500 if (flags
& 0x800) fl
|= O_NOCTTY
;
503 return sparc32_open(filename
, flags
, mode
);
506 #define SOL_F_SETLK 6
507 #define SOL_F_SETLKW 7
508 #define SOL_F_FREESP 11
509 #define SOL_F_ISSTREAM 13
510 #define SOL_F_GETLK 14
511 #define SOL_F_PRIV 15
512 #define SOL_F_NPRIV 16
513 #define SOL_F_QUOTACTL 17
514 #define SOL_F_BLOCKS 18
515 #define SOL_F_BLKSIZE 19
516 #define SOL_F_GETOWN 23
517 #define SOL_F_SETOWN 24
529 asmlinkage
int solaris_fcntl(unsigned fd
, unsigned cmd
, u32 arg
)
531 int (*sys_fcntl
)(unsigned,unsigned,unsigned long) =
532 (int (*)(unsigned,unsigned,unsigned long))SYS(fcntl
);
538 case F_SETFD
: return sys_fcntl(fd
, cmd
, (unsigned long)arg
);
540 flags
= sys_fcntl(fd
, cmd
, 0);
542 if (flags
& O_SYNC
) ret
|= 0x8050;
543 if (flags
& O_NONBLOCK
) ret
|= 0x80;
547 if (arg
& 0x8050) flags
|= O_SYNC
;
548 if (arg
& 0x80) flags
|= O_NONBLOCK
;
549 return sys_fcntl(fd
, cmd
, (long)flags
);
555 struct sol_flock __user
*p
= A(arg
);
556 mm_segment_t old_fs
= get_fs();
559 case SOL_F_GETLK
: cmd
= F_GETLK
; break;
560 case SOL_F_SETLK
: cmd
= F_SETLK
; break;
561 case SOL_F_SETLKW
: cmd
= F_SETLKW
; break;
564 if (get_user (f
.l_type
, &p
->l_type
) ||
565 __get_user (f
.l_whence
, &p
->l_whence
) ||
566 __get_user (f
.l_start
, &p
->l_start
) ||
567 __get_user (f
.l_len
, &p
->l_len
) ||
568 __get_user (f
.l_pid
, &p
->l_sysid
))
572 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
575 if (__put_user (f
.l_type
, &p
->l_type
) ||
576 __put_user (f
.l_whence
, &p
->l_whence
) ||
577 __put_user (f
.l_start
, &p
->l_start
) ||
578 __put_user (f
.l_len
, &p
->l_len
) ||
579 __put_user (f
.l_pid
, &p
->l_pid
) ||
580 __put_user (0, &p
->l_sysid
))
588 int (*sys_newftruncate
)(unsigned int, unsigned long)=
589 (int (*)(unsigned int, unsigned long))SYS(ftruncate
);
591 if (get_user(length
, &((struct sol_flock __user
*)A(arg
))->l_start
))
594 return sys_newftruncate(fd
, length
);
600 asmlinkage
int solaris_ulimit(int cmd
, int val
)
603 case 1: /* UL_GETFSIZE - in 512B chunks */
604 return current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
>> 9;
605 case 2: /* UL_SETFSIZE */
606 if ((unsigned long)val
> (LONG_MAX
>>9)) return -ERANGE
;
608 task_lock(current
->group_leader
);
609 if (val
> current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_max
) {
610 if (!capable(CAP_SYS_RESOURCE
)) {
611 task_unlock(current
->group_leader
);
614 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_max
= val
;
616 current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
= val
;
617 task_unlock(current
->group_leader
);
619 case 3: /* UL_GMEMLIM */
620 return current
->signal
->rlim
[RLIMIT_DATA
].rlim_cur
;
621 case 4: /* UL_GDESLIM */
627 /* At least at the time I'm writing this, Linux doesn't have ACLs, so we
629 asmlinkage
int solaris_acl(u32 filename
, int cmd
, int nentries
, u32 aclbufp
)
634 asmlinkage
int solaris_facl(unsigned int fd
, int cmd
, int nentries
, u32 aclbufp
)
639 asmlinkage
int solaris_pread(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
641 ssize_t (*sys_pread64
)(unsigned int, char __user
*, size_t, loff_t
) =
642 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pread64
);
644 return sys_pread64(fd
, buf
, count
, (loff_t
)pos
);
647 asmlinkage
int solaris_pwrite(unsigned int fd
, char __user
*buf
, u32 count
, u32 pos
)
649 ssize_t (*sys_pwrite64
)(unsigned int, char __user
*, size_t, loff_t
) =
650 (ssize_t (*)(unsigned int, char __user
*, size_t, loff_t
))SYS(pwrite64
);
652 return sys_pwrite64(fd
, buf
, count
, (loff_t
)pos
);
656 #define _PC_LINK_MAX 1
657 #define _PC_MAX_CANON 2
658 #define _PC_MAX_INPUT 3
659 #define _PC_NAME_MAX 4
660 #define _PC_PATH_MAX 5
661 #define _PC_PIPE_BUF 6
662 #define _PC_NO_TRUNC 7
663 #define _PC_VDISABLE 8
664 #define _PC_CHOWN_RESTRICTED 9
666 #define _PC_ASYNC_IO 10
667 #define _PC_PRIO_IO 11
668 #define _PC_SYNC_IO 12
671 /* This is not a real and complete implementation yet, just to keep
672 * the easy Solaris binaries happy.
674 asmlinkage
int solaris_fpathconf(int fd
, int name
)
697 case _PC_CHOWN_RESTRICTED
:
711 asmlinkage
int solaris_pathconf(u32 path
, int name
)
713 return solaris_fpathconf(0, name
);
716 /* solaris_llseek returns long long - quite difficult */
717 asmlinkage
long solaris_llseek(struct pt_regs
*regs
, u32 off_hi
, u32 off_lo
, int whence
)
719 int (*sys_llseek
)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int) =
720 (int (*)(unsigned int, unsigned long, unsigned long, loff_t __user
*, unsigned int))SYS(_llseek
);
722 mm_segment_t old_fs
= get_fs();
726 ret
= sys_llseek((unsigned int)regs
->u_regs
[UREG_I0
], off_hi
, off_lo
, &retval
, whence
);
728 if (ret
< 0) return ret
;
729 regs
->u_regs
[UREG_I1
] = (u32
)retval
;
730 return (retval
>> 32);
733 /* Have to mask out all but lower 3 bits */
734 asmlinkage
int solaris_access(u32 filename
, long mode
)
736 int (*sys_access
)(const char __user
*, int) =
737 (int (*)(const char __user
*, int))SYS(access
);
739 return sys_access(A(filename
), mode
& 7);