1 #include <linux/syscalls.h>
2 #include <linux/export.h>
4 #include <linux/file.h>
5 #include <linux/mount.h>
6 #include <linux/namei.h>
7 #include <linux/statfs.h>
8 #include <linux/security.h>
9 #include <linux/uaccess.h>
12 static int flags_by_mnt(int mnt_flags
)
16 if (mnt_flags
& MNT_READONLY
)
18 if (mnt_flags
& MNT_NOSUID
)
20 if (mnt_flags
& MNT_NODEV
)
22 if (mnt_flags
& MNT_NOEXEC
)
24 if (mnt_flags
& MNT_NOATIME
)
26 if (mnt_flags
& MNT_NODIRATIME
)
27 flags
|= ST_NODIRATIME
;
28 if (mnt_flags
& MNT_RELATIME
)
33 static int flags_by_sb(int s_flags
)
36 if (s_flags
& MS_SYNCHRONOUS
)
37 flags
|= ST_SYNCHRONOUS
;
38 if (s_flags
& MS_MANDLOCK
)
43 static int calculate_f_flags(struct vfsmount
*mnt
)
45 return ST_VALID
| flags_by_mnt(mnt
->mnt_flags
) |
46 flags_by_sb(mnt
->mnt_sb
->s_flags
);
49 static int statfs_by_dentry(struct dentry
*dentry
, struct kstatfs
*buf
)
53 if (!dentry
->d_sb
->s_op
->statfs
)
56 memset(buf
, 0, sizeof(*buf
));
57 retval
= security_sb_statfs(dentry
);
60 retval
= dentry
->d_sb
->s_op
->statfs(dentry
, buf
);
61 if (retval
== 0 && buf
->f_frsize
== 0)
62 buf
->f_frsize
= buf
->f_bsize
;
66 int vfs_statfs(struct path
*path
, struct kstatfs
*buf
)
70 error
= statfs_by_dentry(path
->dentry
, buf
);
72 buf
->f_flags
= calculate_f_flags(path
->mnt
);
75 EXPORT_SYMBOL(vfs_statfs
);
77 int user_statfs(const char __user
*pathname
, struct kstatfs
*st
)
80 int error
= user_path_at(AT_FDCWD
, pathname
, LOOKUP_FOLLOW
|LOOKUP_AUTOMOUNT
, &path
);
82 error
= vfs_statfs(&path
, st
);
88 int fd_statfs(int fd
, struct kstatfs
*st
)
91 struct file
*file
= fget_light(fd
, &fput_needed
);
94 error
= vfs_statfs(&file
->f_path
, st
);
95 fput_light(file
, fput_needed
);
100 static int do_statfs_native(struct kstatfs
*st
, struct statfs __user
*p
)
104 if (sizeof(buf
) == sizeof(*st
))
105 memcpy(&buf
, st
, sizeof(*st
));
107 if (sizeof buf
.f_blocks
== 4) {
108 if ((st
->f_blocks
| st
->f_bfree
| st
->f_bavail
|
109 st
->f_bsize
| st
->f_frsize
) &
110 0xffffffff00000000ULL
)
113 * f_files and f_ffree may be -1; it's okay to stuff
116 if (st
->f_files
!= -1 &&
117 (st
->f_files
& 0xffffffff00000000ULL
))
119 if (st
->f_ffree
!= -1 &&
120 (st
->f_ffree
& 0xffffffff00000000ULL
))
124 buf
.f_type
= st
->f_type
;
125 buf
.f_bsize
= st
->f_bsize
;
126 buf
.f_blocks
= st
->f_blocks
;
127 buf
.f_bfree
= st
->f_bfree
;
128 buf
.f_bavail
= st
->f_bavail
;
129 buf
.f_files
= st
->f_files
;
130 buf
.f_ffree
= st
->f_ffree
;
131 buf
.f_fsid
= st
->f_fsid
;
132 buf
.f_namelen
= st
->f_namelen
;
133 buf
.f_frsize
= st
->f_frsize
;
134 buf
.f_flags
= st
->f_flags
;
135 memset(buf
.f_spare
, 0, sizeof(buf
.f_spare
));
137 if (copy_to_user(p
, &buf
, sizeof(buf
)))
142 static int do_statfs64(struct kstatfs
*st
, struct statfs64 __user
*p
)
145 if (sizeof(buf
) == sizeof(*st
))
146 memcpy(&buf
, st
, sizeof(*st
));
148 buf
.f_type
= st
->f_type
;
149 buf
.f_bsize
= st
->f_bsize
;
150 buf
.f_blocks
= st
->f_blocks
;
151 buf
.f_bfree
= st
->f_bfree
;
152 buf
.f_bavail
= st
->f_bavail
;
153 buf
.f_files
= st
->f_files
;
154 buf
.f_ffree
= st
->f_ffree
;
155 buf
.f_fsid
= st
->f_fsid
;
156 buf
.f_namelen
= st
->f_namelen
;
157 buf
.f_frsize
= st
->f_frsize
;
158 buf
.f_flags
= st
->f_flags
;
159 memset(buf
.f_spare
, 0, sizeof(buf
.f_spare
));
161 if (copy_to_user(p
, &buf
, sizeof(buf
)))
166 SYSCALL_DEFINE2(statfs
, const char __user
*, pathname
, struct statfs __user
*, buf
)
169 int error
= user_statfs(pathname
, &st
);
171 error
= do_statfs_native(&st
, buf
);
175 SYSCALL_DEFINE3(statfs64
, const char __user
*, pathname
, size_t, sz
, struct statfs64 __user
*, buf
)
179 if (sz
!= sizeof(*buf
))
181 error
= user_statfs(pathname
, &st
);
183 error
= do_statfs64(&st
, buf
);
187 SYSCALL_DEFINE2(fstatfs
, unsigned int, fd
, struct statfs __user
*, buf
)
190 int error
= fd_statfs(fd
, &st
);
192 error
= do_statfs_native(&st
, buf
);
196 SYSCALL_DEFINE3(fstatfs64
, unsigned int, fd
, size_t, sz
, struct statfs64 __user
*, buf
)
201 if (sz
!= sizeof(*buf
))
204 error
= fd_statfs(fd
, &st
);
206 error
= do_statfs64(&st
, buf
);
210 int vfs_ustat(dev_t dev
, struct kstatfs
*sbuf
)
212 struct super_block
*s
= user_get_super(dev
);
217 err
= statfs_by_dentry(s
->s_root
, sbuf
);
222 SYSCALL_DEFINE2(ustat
, unsigned, dev
, struct ustat __user
*, ubuf
)
226 int err
= vfs_ustat(new_decode_dev(dev
), &sbuf
);
230 memset(&tmp
,0,sizeof(struct ustat
));
231 tmp
.f_tfree
= sbuf
.f_bfree
;
232 tmp
.f_tinode
= sbuf
.f_ffree
;
234 return copy_to_user(ubuf
, &tmp
, sizeof(struct ustat
)) ? -EFAULT
: 0;