Commit c8b91acc authored by Al Viro's avatar Al Viro

clean statfs-like syscalls up

New helpers: user_statfs() and fd_statfs(), taking userland pathname and
descriptor resp. and filling struct kstatfs.  Syscalls of statfs family
(native, compat and foreign - osf and hpux on alpha and parisc resp.)
switched to those.  Removes some boilerplate code, simplifies cleanup
on errors...
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 73d049a4
...@@ -230,44 +230,24 @@ linux_to_osf_statfs(struct kstatfs *linux_stat, struct osf_statfs __user *osf_st ...@@ -230,44 +230,24 @@ linux_to_osf_statfs(struct kstatfs *linux_stat, struct osf_statfs __user *osf_st
return copy_to_user(osf_stat, &tmp_stat, bufsiz) ? -EFAULT : 0; return copy_to_user(osf_stat, &tmp_stat, bufsiz) ? -EFAULT : 0;
} }
static int SYSCALL_DEFINE3(osf_statfs, const char __user *, pathname,
do_osf_statfs(struct path *path, struct osf_statfs __user *buffer, struct osf_statfs __user *, buffer, unsigned long, bufsiz)
unsigned long bufsiz)
{ {
struct kstatfs linux_stat; struct kstatfs linux_stat;
int error = vfs_statfs(path, &linux_stat); int error = user_statfs(pathname, &linux_stat);
if (!error) if (!error)
error = linux_to_osf_statfs(&linux_stat, buffer, bufsiz); error = linux_to_osf_statfs(&linux_stat, buffer, bufsiz);
return error; return error;
} }
SYSCALL_DEFINE3(osf_statfs, const char __user *, pathname,
struct osf_statfs __user *, buffer, unsigned long, bufsiz)
{
struct path path;
int retval;
retval = user_path(pathname, &path);
if (!retval) {
retval = do_osf_statfs(&path, buffer, bufsiz);
path_put(&path);
}
return retval;
}
SYSCALL_DEFINE3(osf_fstatfs, unsigned long, fd, SYSCALL_DEFINE3(osf_fstatfs, unsigned long, fd,
struct osf_statfs __user *, buffer, unsigned long, bufsiz) struct osf_statfs __user *, buffer, unsigned long, bufsiz)
{ {
struct file *file; struct kstatfs linux_stat;
int retval; int error = fd_statfs(fd, &linux_stat);
if (!error)
retval = -EBADF; error = linux_to_osf_statfs(&linux_stat, buffer, bufsiz);
file = fget(fd); return error;
if (file) {
retval = do_osf_statfs(&file->f_path, buffer, bufsiz);
fput(file);
}
return retval;
} }
/* /*
......
...@@ -185,26 +185,21 @@ struct hpux_statfs { ...@@ -185,26 +185,21 @@ struct hpux_statfs {
int16_t f_pad; int16_t f_pad;
}; };
static int do_statfs_hpux(struct path *path, struct hpux_statfs *buf) static int do_statfs_hpux(struct kstatfs *st, struct hpux_statfs __user *p)
{ {
struct kstatfs st; struct hpux_statfs buf;
int retval; memset(&buf, 0, sizeof(buf));
buf.f_type = st->f_type;
retval = vfs_statfs(path, &st); buf.f_bsize = st->f_bsize;
if (retval) buf.f_blocks = st->f_blocks;
return retval; buf.f_bfree = st->f_bfree;
buf.f_bavail = st->f_bavail;
memset(buf, 0, sizeof(*buf)); buf.f_files = st->f_files;
buf->f_type = st.f_type; buf.f_ffree = st->f_ffree;
buf->f_bsize = st.f_bsize; buf.f_fsid[0] = st->f_fsid.val[0];
buf->f_blocks = st.f_blocks; buf.f_fsid[1] = st->f_fsid.val[1];
buf->f_bfree = st.f_bfree; if (copy_to_user(p, &buf, sizeof(buf)))
buf->f_bavail = st.f_bavail; return -EFAULT;
buf->f_files = st.f_files;
buf->f_ffree = st.f_ffree;
buf->f_fsid[0] = st.f_fsid.val[0];
buf->f_fsid[1] = st.f_fsid.val[1];
return 0; return 0;
} }
...@@ -212,35 +207,19 @@ static int do_statfs_hpux(struct path *path, struct hpux_statfs *buf) ...@@ -212,35 +207,19 @@ static int do_statfs_hpux(struct path *path, struct hpux_statfs *buf)
asmlinkage long hpux_statfs(const char __user *pathname, asmlinkage long hpux_statfs(const char __user *pathname,
struct hpux_statfs __user *buf) struct hpux_statfs __user *buf)
{ {
struct path path; struct kstatfs st;
int error; int error = user_statfs(pathname, &st);
if (!error)
error = user_path(pathname, &path); error = do_statfs_hpux(&st, buf);
if (!error) {
struct hpux_statfs tmp;
error = do_statfs_hpux(&path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
path_put(&path);
}
return error; return error;
} }
asmlinkage long hpux_fstatfs(unsigned int fd, struct hpux_statfs __user * buf) asmlinkage long hpux_fstatfs(unsigned int fd, struct hpux_statfs __user * buf)
{ {
struct file *file; struct kstatfs st;
struct hpux_statfs tmp; int error = fd_statfs(fd, &st);
int error; if (!error)
error = do_statfs_hpux(&st, buf);
error = -EBADF;
file = fget(fd);
if (!file)
goto out;
error = do_statfs_hpux(&file->f_path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
fput(file);
out:
return error; return error;
} }
......
...@@ -262,35 +262,19 @@ static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs * ...@@ -262,35 +262,19 @@ static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *
*/ */
asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf) asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
{ {
struct path path;
int error;
error = user_path(pathname, &path);
if (!error) {
struct kstatfs tmp; struct kstatfs tmp;
error = vfs_statfs(&path, &tmp); int error = user_statfs(pathname, &tmp);
if (!error) if (!error)
error = put_compat_statfs(buf, &tmp); error = put_compat_statfs(buf, &tmp);
path_put(&path);
}
return error; return error;
} }
asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf) asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
{ {
struct file * file;
struct kstatfs tmp; struct kstatfs tmp;
int error; int error = fd_statfs(fd, &tmp);
error = -EBADF;
file = fget(fd);
if (!file)
goto out;
error = vfs_statfs(&file->f_path, &tmp);
if (!error) if (!error)
error = put_compat_statfs(buf, &tmp); error = put_compat_statfs(buf, &tmp);
fput(file);
out:
return error; return error;
} }
...@@ -329,41 +313,29 @@ static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstat ...@@ -329,41 +313,29 @@ static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstat
asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf) asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
{ {
struct path path; struct kstatfs tmp;
int error; int error;
if (sz != sizeof(*buf)) if (sz != sizeof(*buf))
return -EINVAL; return -EINVAL;
error = user_path(pathname, &path); error = user_statfs(pathname, &tmp);
if (!error) {
struct kstatfs tmp;
error = vfs_statfs(&path, &tmp);
if (!error) if (!error)
error = put_compat_statfs64(buf, &tmp); error = put_compat_statfs64(buf, &tmp);
path_put(&path);
}
return error; return error;
} }
asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf) asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
{ {
struct file * file;
struct kstatfs tmp; struct kstatfs tmp;
int error; int error;
if (sz != sizeof(*buf)) if (sz != sizeof(*buf))
return -EINVAL; return -EINVAL;
error = -EBADF; error = fd_statfs(fd, &tmp);
file = fget(fd);
if (!file)
goto out;
error = vfs_statfs(&file->f_path, &tmp);
if (!error) if (!error)
error = put_compat_statfs64(buf, &tmp); error = put_compat_statfs64(buf, &tmp);
fput(file);
out:
return error; return error;
} }
......
...@@ -73,149 +73,135 @@ int vfs_statfs(struct path *path, struct kstatfs *buf) ...@@ -73,149 +73,135 @@ int vfs_statfs(struct path *path, struct kstatfs *buf)
} }
EXPORT_SYMBOL(vfs_statfs); EXPORT_SYMBOL(vfs_statfs);
static int do_statfs_native(struct path *path, struct statfs *buf) int user_statfs(const char __user *pathname, struct kstatfs *st)
{ {
struct kstatfs st; struct path path;
int retval; int error = user_path(pathname, &path);
if (!error) {
error = vfs_statfs(&path, st);
path_put(&path);
}
return error;
}
retval = vfs_statfs(path, &st); int fd_statfs(int fd, struct kstatfs *st)
if (retval) {
return retval; struct file *file = fget(fd);
int error = -EBADF;
if (file) {
error = vfs_statfs(&file->f_path, st);
fput(file);
}
return error;
}
if (sizeof(*buf) == sizeof(st)) static int do_statfs_native(struct kstatfs *st, struct statfs __user *p)
memcpy(buf, &st, sizeof(st)); {
struct statfs buf;
if (sizeof(buf) == sizeof(*st))
memcpy(&buf, st, sizeof(*st));
else { else {
if (sizeof buf->f_blocks == 4) { if (sizeof buf.f_blocks == 4) {
if ((st.f_blocks | st.f_bfree | st.f_bavail | if ((st->f_blocks | st->f_bfree | st->f_bavail |
st.f_bsize | st.f_frsize) & st->f_bsize | st->f_frsize) &
0xffffffff00000000ULL) 0xffffffff00000000ULL)
return -EOVERFLOW; return -EOVERFLOW;
/* /*
* f_files and f_ffree may be -1; it's okay to stuff * f_files and f_ffree may be -1; it's okay to stuff
* that into 32 bits * that into 32 bits
*/ */
if (st.f_files != -1 && if (st->f_files != -1 &&
(st.f_files & 0xffffffff00000000ULL)) (st->f_files & 0xffffffff00000000ULL))
return -EOVERFLOW; return -EOVERFLOW;
if (st.f_ffree != -1 && if (st->f_ffree != -1 &&
(st.f_ffree & 0xffffffff00000000ULL)) (st->f_ffree & 0xffffffff00000000ULL))
return -EOVERFLOW; return -EOVERFLOW;
} }
buf->f_type = st.f_type; buf.f_type = st->f_type;
buf->f_bsize = st.f_bsize; buf.f_bsize = st->f_bsize;
buf->f_blocks = st.f_blocks; buf.f_blocks = st->f_blocks;
buf->f_bfree = st.f_bfree; buf.f_bfree = st->f_bfree;
buf->f_bavail = st.f_bavail; buf.f_bavail = st->f_bavail;
buf->f_files = st.f_files; buf.f_files = st->f_files;
buf->f_ffree = st.f_ffree; buf.f_ffree = st->f_ffree;
buf->f_fsid = st.f_fsid; buf.f_fsid = st->f_fsid;
buf->f_namelen = st.f_namelen; buf.f_namelen = st->f_namelen;
buf->f_frsize = st.f_frsize; buf.f_frsize = st->f_frsize;
buf->f_flags = st.f_flags; buf.f_flags = st->f_flags;
memset(buf->f_spare, 0, sizeof(buf->f_spare)); memset(buf.f_spare, 0, sizeof(buf.f_spare));
} }
if (copy_to_user(p, &buf, sizeof(buf)))
return -EFAULT;
return 0; return 0;
} }
static int do_statfs64(struct path *path, struct statfs64 *buf) static int do_statfs64(struct kstatfs *st, struct statfs64 __user *p)
{ {
struct kstatfs st; struct statfs64 buf;
int retval; if (sizeof(buf) == sizeof(*st))
memcpy(&buf, st, sizeof(*st));
retval = vfs_statfs(path, &st);
if (retval)
return retval;
if (sizeof(*buf) == sizeof(st))
memcpy(buf, &st, sizeof(st));
else { else {
buf->f_type = st.f_type; buf.f_type = st->f_type;
buf->f_bsize = st.f_bsize; buf.f_bsize = st->f_bsize;
buf->f_blocks = st.f_blocks; buf.f_blocks = st->f_blocks;
buf->f_bfree = st.f_bfree; buf.f_bfree = st->f_bfree;
buf->f_bavail = st.f_bavail; buf.f_bavail = st->f_bavail;
buf->f_files = st.f_files; buf.f_files = st->f_files;
buf->f_ffree = st.f_ffree; buf.f_ffree = st->f_ffree;
buf->f_fsid = st.f_fsid; buf.f_fsid = st->f_fsid;
buf->f_namelen = st.f_namelen; buf.f_namelen = st->f_namelen;
buf->f_frsize = st.f_frsize; buf.f_frsize = st->f_frsize;
buf->f_flags = st.f_flags; buf.f_flags = st->f_flags;
memset(buf->f_spare, 0, sizeof(buf->f_spare)); memset(buf.f_spare, 0, sizeof(buf.f_spare));
} }
if (copy_to_user(p, &buf, sizeof(buf)))
return -EFAULT;
return 0; return 0;
} }
SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf) SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
{ {
struct path path; struct kstatfs st;
int error; int error = user_statfs(pathname, &st);
if (!error)
error = user_path(pathname, &path); error = do_statfs_native(&st, buf);
if (!error) {
struct statfs tmp;
error = do_statfs_native(&path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
path_put(&path);
}
return error; return error;
} }
SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf) SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
{ {
struct path path; struct kstatfs st;
long error; int error;
if (sz != sizeof(*buf)) if (sz != sizeof(*buf))
return -EINVAL; return -EINVAL;
error = user_path(pathname, &path); error = user_statfs(pathname, &st);
if (!error) { if (!error)
struct statfs64 tmp; error = do_statfs64(&st, buf);
error = do_statfs64(&path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
path_put(&path);
}
return error; return error;
} }
SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf) SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
{ {
struct file *file; struct kstatfs st;
struct statfs tmp; int error = fd_statfs(fd, &st);
int error; if (!error)
error = do_statfs_native(&st, buf);
error = -EBADF;
file = fget(fd);
if (!file)
goto out;
error = do_statfs_native(&file->f_path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
fput(file);
out:
return error; return error;
} }
SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf) SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
{ {
struct file *file; struct kstatfs st;
struct statfs64 tmp;
int error; int error;
if (sz != sizeof(*buf)) if (sz != sizeof(*buf))
return -EINVAL; return -EINVAL;
error = -EBADF; error = fd_statfs(fd, &st);
file = fget(fd); if (!error)
if (!file) error = do_statfs64(&st, buf);
goto out;
error = do_statfs64(&file->f_path, &tmp);
if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
error = -EFAULT;
fput(file);
out:
return error; return error;
} }
......
...@@ -1874,6 +1874,8 @@ extern void drop_collected_mounts(struct vfsmount *); ...@@ -1874,6 +1874,8 @@ extern void drop_collected_mounts(struct vfsmount *);
extern int iterate_mounts(int (*)(struct vfsmount *, void *), void *, extern int iterate_mounts(int (*)(struct vfsmount *, void *), void *,
struct vfsmount *); struct vfsmount *);
extern int vfs_statfs(struct path *, struct kstatfs *); extern int vfs_statfs(struct path *, struct kstatfs *);
extern int user_statfs(const char __user *, struct kstatfs *);
extern int fd_statfs(int, struct kstatfs *);
extern int statfs_by_dentry(struct dentry *, struct kstatfs *); extern int statfs_by_dentry(struct dentry *, struct kstatfs *);
extern int freeze_super(struct super_block *super); extern int freeze_super(struct super_block *super);
extern int thaw_super(struct super_block *super); extern int thaw_super(struct super_block *super);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment