Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/stat.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/highuid.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/namei.h> |
| 14 | #include <linux/security.h> |
| 15 | #include <linux/syscalls.h> |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <asm/uaccess.h> |
| 19 | #include <asm/unistd.h> |
| 20 | |
| 21 | void generic_fillattr(struct inode *inode, struct kstat *stat) |
| 22 | { |
| 23 | stat->dev = inode->i_sb->s_dev; |
| 24 | stat->ino = inode->i_ino; |
| 25 | stat->mode = inode->i_mode; |
| 26 | stat->nlink = inode->i_nlink; |
| 27 | stat->uid = inode->i_uid; |
| 28 | stat->gid = inode->i_gid; |
| 29 | stat->rdev = inode->i_rdev; |
| 30 | stat->atime = inode->i_atime; |
| 31 | stat->mtime = inode->i_mtime; |
| 32 | stat->ctime = inode->i_ctime; |
| 33 | stat->size = i_size_read(inode); |
| 34 | stat->blocks = inode->i_blocks; |
Theodore Ts'o | ba52de1 | 2006-09-27 01:50:49 -0700 | [diff] [blame] | 35 | stat->blksize = (1 << inode->i_blkbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | EXPORT_SYMBOL(generic_fillattr); |
| 39 | |
| 40 | int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 41 | { |
| 42 | struct inode *inode = dentry->d_inode; |
| 43 | int retval; |
| 44 | |
| 45 | retval = security_inode_getattr(mnt, dentry); |
| 46 | if (retval) |
| 47 | return retval; |
| 48 | |
| 49 | if (inode->i_op->getattr) |
| 50 | return inode->i_op->getattr(mnt, dentry, stat); |
| 51 | |
| 52 | generic_fillattr(inode, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | EXPORT_SYMBOL(vfs_getattr); |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | int vfs_fstat(unsigned int fd, struct kstat *stat) |
| 59 | { |
| 60 | struct file *f = fget(fd); |
| 61 | int error = -EBADF; |
| 62 | |
| 63 | if (f) { |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 64 | error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | fput(f); |
| 66 | } |
| 67 | return error; |
| 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | EXPORT_SYMBOL(vfs_fstat); |
| 70 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 71 | int vfs_fstatat(int dfd, char __user *filename, struct kstat *stat, int flag) |
| 72 | { |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 73 | struct path path; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 74 | int error = -EINVAL; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 75 | int lookup_flags = 0; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 76 | |
| 77 | if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) |
| 78 | goto out; |
| 79 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 80 | if (!(flag & AT_SYMLINK_NOFOLLOW)) |
| 81 | lookup_flags |= LOOKUP_FOLLOW; |
| 82 | |
| 83 | error = user_path_at(dfd, filename, lookup_flags, &path); |
| 84 | if (error) |
| 85 | goto out; |
| 86 | |
| 87 | error = vfs_getattr(path.mnt, path.dentry, stat); |
| 88 | path_put(&path); |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 89 | out: |
| 90 | return error; |
| 91 | } |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 92 | EXPORT_SYMBOL(vfs_fstatat); |
| 93 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 94 | int vfs_stat(char __user *name, struct kstat *stat) |
| 95 | { |
| 96 | return vfs_fstatat(AT_FDCWD, name, stat, 0); |
| 97 | } |
| 98 | EXPORT_SYMBOL(vfs_stat); |
| 99 | |
| 100 | int vfs_lstat(char __user *name, struct kstat *stat) |
| 101 | { |
| 102 | return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW); |
| 103 | } |
| 104 | EXPORT_SYMBOL(vfs_lstat); |
| 105 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #ifdef __ARCH_WANT_OLD_STAT |
| 108 | |
| 109 | /* |
| 110 | * For backward compatibility? Maybe this should be moved |
| 111 | * into arch/i386 instead? |
| 112 | */ |
| 113 | static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf) |
| 114 | { |
| 115 | static int warncount = 5; |
| 116 | struct __old_kernel_stat tmp; |
| 117 | |
| 118 | if (warncount > 0) { |
| 119 | warncount--; |
| 120 | printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n", |
| 121 | current->comm); |
| 122 | } else if (warncount < 0) { |
| 123 | /* it's laughable, but... */ |
| 124 | warncount = 0; |
| 125 | } |
| 126 | |
| 127 | memset(&tmp, 0, sizeof(struct __old_kernel_stat)); |
| 128 | tmp.st_dev = old_encode_dev(stat->dev); |
| 129 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 130 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 131 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | tmp.st_mode = stat->mode; |
| 133 | tmp.st_nlink = stat->nlink; |
| 134 | if (tmp.st_nlink != stat->nlink) |
| 135 | return -EOVERFLOW; |
| 136 | SET_UID(tmp.st_uid, stat->uid); |
| 137 | SET_GID(tmp.st_gid, stat->gid); |
| 138 | tmp.st_rdev = old_encode_dev(stat->rdev); |
| 139 | #if BITS_PER_LONG == 32 |
| 140 | if (stat->size > MAX_NON_LFS) |
| 141 | return -EOVERFLOW; |
| 142 | #endif |
| 143 | tmp.st_size = stat->size; |
| 144 | tmp.st_atime = stat->atime.tv_sec; |
| 145 | tmp.st_mtime = stat->mtime.tv_sec; |
| 146 | tmp.st_ctime = stat->ctime.tv_sec; |
| 147 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 148 | } |
| 149 | |
Heiko Carstens | bdc480e | 2009-01-14 14:14:12 +0100 | [diff] [blame] | 150 | SYSCALL_DEFINE2(stat, char __user *, filename, struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 153 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 155 | error = vfs_stat(filename, &stat); |
| 156 | if (error) |
| 157 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 159 | return cp_old_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 161 | |
| 162 | SYSCALL_DEFINE2(lstat, char __user *, filename, struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 165 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 167 | error = vfs_lstat(filename, &stat); |
| 168 | if (error) |
| 169 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 171 | return cp_old_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 173 | |
| 174 | SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
| 176 | struct kstat stat; |
| 177 | int error = vfs_fstat(fd, &stat); |
| 178 | |
| 179 | if (!error) |
| 180 | error = cp_old_stat(&stat, statbuf); |
| 181 | |
| 182 | return error; |
| 183 | } |
| 184 | |
| 185 | #endif /* __ARCH_WANT_OLD_STAT */ |
| 186 | |
| 187 | static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf) |
| 188 | { |
| 189 | struct stat tmp; |
| 190 | |
| 191 | #if BITS_PER_LONG == 32 |
| 192 | if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev)) |
| 193 | return -EOVERFLOW; |
| 194 | #else |
| 195 | if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev)) |
| 196 | return -EOVERFLOW; |
| 197 | #endif |
| 198 | |
| 199 | memset(&tmp, 0, sizeof(tmp)); |
| 200 | #if BITS_PER_LONG == 32 |
| 201 | tmp.st_dev = old_encode_dev(stat->dev); |
| 202 | #else |
| 203 | tmp.st_dev = new_encode_dev(stat->dev); |
| 204 | #endif |
| 205 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 206 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 207 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | tmp.st_mode = stat->mode; |
| 209 | tmp.st_nlink = stat->nlink; |
| 210 | if (tmp.st_nlink != stat->nlink) |
| 211 | return -EOVERFLOW; |
| 212 | SET_UID(tmp.st_uid, stat->uid); |
| 213 | SET_GID(tmp.st_gid, stat->gid); |
| 214 | #if BITS_PER_LONG == 32 |
| 215 | tmp.st_rdev = old_encode_dev(stat->rdev); |
| 216 | #else |
| 217 | tmp.st_rdev = new_encode_dev(stat->rdev); |
| 218 | #endif |
| 219 | #if BITS_PER_LONG == 32 |
| 220 | if (stat->size > MAX_NON_LFS) |
| 221 | return -EOVERFLOW; |
| 222 | #endif |
| 223 | tmp.st_size = stat->size; |
| 224 | tmp.st_atime = stat->atime.tv_sec; |
| 225 | tmp.st_mtime = stat->mtime.tv_sec; |
| 226 | tmp.st_ctime = stat->ctime.tv_sec; |
| 227 | #ifdef STAT_HAVE_NSEC |
| 228 | tmp.st_atime_nsec = stat->atime.tv_nsec; |
| 229 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; |
| 230 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; |
| 231 | #endif |
| 232 | tmp.st_blocks = stat->blocks; |
| 233 | tmp.st_blksize = stat->blksize; |
| 234 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 235 | } |
| 236 | |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 237 | SYSCALL_DEFINE2(newstat, char __user *, filename, struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 240 | int error = vfs_stat(filename, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 242 | if (error) |
| 243 | return error; |
| 244 | return cp_new_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 246 | |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 247 | SYSCALL_DEFINE2(newlstat, char __user *, filename, struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 250 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 252 | error = vfs_lstat(filename, &stat); |
| 253 | if (error) |
| 254 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 256 | return cp_new_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 258 | |
Andreas Schwab | 2833c28 | 2006-04-27 15:46:42 +0200 | [diff] [blame] | 259 | #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT) |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 260 | SYSCALL_DEFINE4(newfstatat, int, dfd, char __user *, filename, |
| 261 | struct stat __user *, statbuf, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 262 | { |
| 263 | struct kstat stat; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 264 | int error; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 265 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 266 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 267 | if (error) |
| 268 | return error; |
| 269 | return cp_new_stat(&stat, statbuf); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 270 | } |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 271 | #endif |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 272 | |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 273 | SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | struct kstat stat; |
| 276 | int error = vfs_fstat(fd, &stat); |
| 277 | |
| 278 | if (!error) |
| 279 | error = cp_new_stat(&stat, statbuf); |
| 280 | |
| 281 | return error; |
| 282 | } |
| 283 | |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 284 | SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname, |
| 285 | char __user *, buf, int, bufsiz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 287 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | int error; |
| 289 | |
| 290 | if (bufsiz <= 0) |
| 291 | return -EINVAL; |
| 292 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 293 | error = user_path_at(dfd, pathname, 0, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 295 | struct inode *inode = path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | error = -EINVAL; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 298 | if (inode->i_op->readlink) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 299 | error = security_inode_readlink(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (!error) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 301 | touch_atime(path.mnt, path.dentry); |
| 302 | error = inode->i_op->readlink(path.dentry, |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 303 | buf, bufsiz); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 306 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | return error; |
| 309 | } |
| 310 | |
Heiko Carstens | 002c897 | 2009-01-14 14:14:18 +0100 | [diff] [blame] | 311 | SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf, |
| 312 | int, bufsiz) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 313 | { |
| 314 | return sys_readlinkat(AT_FDCWD, path, buf, bufsiz); |
| 315 | } |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | /* ---------- LFS-64 ----------- */ |
| 319 | #ifdef __ARCH_WANT_STAT64 |
| 320 | |
| 321 | static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf) |
| 322 | { |
| 323 | struct stat64 tmp; |
| 324 | |
| 325 | memset(&tmp, 0, sizeof(struct stat64)); |
| 326 | #ifdef CONFIG_MIPS |
| 327 | /* mips has weird padding, so we don't get 64 bits there */ |
| 328 | if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev)) |
| 329 | return -EOVERFLOW; |
| 330 | tmp.st_dev = new_encode_dev(stat->dev); |
| 331 | tmp.st_rdev = new_encode_dev(stat->rdev); |
| 332 | #else |
| 333 | tmp.st_dev = huge_encode_dev(stat->dev); |
| 334 | tmp.st_rdev = huge_encode_dev(stat->rdev); |
| 335 | #endif |
| 336 | tmp.st_ino = stat->ino; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 337 | if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino) |
| 338 | return -EOVERFLOW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | #ifdef STAT64_HAS_BROKEN_ST_INO |
| 340 | tmp.__st_ino = stat->ino; |
| 341 | #endif |
| 342 | tmp.st_mode = stat->mode; |
| 343 | tmp.st_nlink = stat->nlink; |
| 344 | tmp.st_uid = stat->uid; |
| 345 | tmp.st_gid = stat->gid; |
| 346 | tmp.st_atime = stat->atime.tv_sec; |
| 347 | tmp.st_atime_nsec = stat->atime.tv_nsec; |
| 348 | tmp.st_mtime = stat->mtime.tv_sec; |
| 349 | tmp.st_mtime_nsec = stat->mtime.tv_nsec; |
| 350 | tmp.st_ctime = stat->ctime.tv_sec; |
| 351 | tmp.st_ctime_nsec = stat->ctime.tv_nsec; |
| 352 | tmp.st_size = stat->size; |
| 353 | tmp.st_blocks = stat->blocks; |
| 354 | tmp.st_blksize = stat->blksize; |
| 355 | return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0; |
| 356 | } |
| 357 | |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 358 | SYSCALL_DEFINE2(stat64, char __user *, filename, struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
| 360 | struct kstat stat; |
| 361 | int error = vfs_stat(filename, &stat); |
| 362 | |
| 363 | if (!error) |
| 364 | error = cp_new_stat64(&stat, statbuf); |
| 365 | |
| 366 | return error; |
| 367 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 368 | |
| 369 | SYSCALL_DEFINE2(lstat64, char __user *, filename, struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | struct kstat stat; |
| 372 | int error = vfs_lstat(filename, &stat); |
| 373 | |
| 374 | if (!error) |
| 375 | error = cp_new_stat64(&stat, statbuf); |
| 376 | |
| 377 | return error; |
| 378 | } |
Heiko Carstens | 257ac26 | 2009-01-14 14:14:13 +0100 | [diff] [blame] | 379 | |
| 380 | SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
| 382 | struct kstat stat; |
| 383 | int error = vfs_fstat(fd, &stat); |
| 384 | |
| 385 | if (!error) |
| 386 | error = cp_new_stat64(&stat, statbuf); |
| 387 | |
| 388 | return error; |
| 389 | } |
| 390 | |
Heiko Carstens | 6559eed8 | 2009-01-14 14:14:32 +0100 | [diff] [blame] | 391 | SYSCALL_DEFINE4(fstatat64, int, dfd, char __user *, filename, |
| 392 | struct stat64 __user *, statbuf, int, flag) |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 393 | { |
| 394 | struct kstat stat; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 395 | int error; |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 396 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 397 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 398 | if (error) |
| 399 | return error; |
| 400 | return cp_new_stat64(&stat, statbuf); |
Ulrich Drepper | cff2b76 | 2006-02-11 17:55:47 -0800 | [diff] [blame] | 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | #endif /* __ARCH_WANT_STAT64 */ |
| 403 | |
Dmitry Monakhov | b462707 | 2009-12-14 15:21:12 +0300 | [diff] [blame] | 404 | /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */ |
| 405 | void __inode_add_bytes(struct inode *inode, loff_t bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | inode->i_blocks += bytes >> 9; |
| 408 | bytes &= 511; |
| 409 | inode->i_bytes += bytes; |
| 410 | if (inode->i_bytes >= 512) { |
| 411 | inode->i_blocks++; |
| 412 | inode->i_bytes -= 512; |
| 413 | } |
Dmitry Monakhov | b462707 | 2009-12-14 15:21:12 +0300 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void inode_add_bytes(struct inode *inode, loff_t bytes) |
| 417 | { |
| 418 | spin_lock(&inode->i_lock); |
| 419 | __inode_add_bytes(inode, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | spin_unlock(&inode->i_lock); |
| 421 | } |
| 422 | |
| 423 | EXPORT_SYMBOL(inode_add_bytes); |
| 424 | |
| 425 | void inode_sub_bytes(struct inode *inode, loff_t bytes) |
| 426 | { |
| 427 | spin_lock(&inode->i_lock); |
| 428 | inode->i_blocks -= bytes >> 9; |
| 429 | bytes &= 511; |
| 430 | if (inode->i_bytes < bytes) { |
| 431 | inode->i_blocks--; |
| 432 | inode->i_bytes += 512; |
| 433 | } |
| 434 | inode->i_bytes -= bytes; |
| 435 | spin_unlock(&inode->i_lock); |
| 436 | } |
| 437 | |
| 438 | EXPORT_SYMBOL(inode_sub_bytes); |
| 439 | |
| 440 | loff_t inode_get_bytes(struct inode *inode) |
| 441 | { |
| 442 | loff_t ret; |
| 443 | |
| 444 | spin_lock(&inode->i_lock); |
| 445 | ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes; |
| 446 | spin_unlock(&inode->i_lock); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | EXPORT_SYMBOL(inode_get_bytes); |
| 451 | |
| 452 | void inode_set_bytes(struct inode *inode, loff_t bytes) |
| 453 | { |
| 454 | /* Caller is here responsible for sufficient locking |
| 455 | * (ie. inode->i_lock) */ |
| 456 | inode->i_blocks = bytes >> 9; |
| 457 | inode->i_bytes = bytes & 511; |
| 458 | } |
| 459 | |
| 460 | EXPORT_SYMBOL(inode_set_bytes); |