4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
13 #include <linux/highuid.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
18 #include <asm/uaccess.h>
20 void generic_fillattr(struct inode
*inode
, struct kstat
*stat
)
22 stat
->dev
= inode
->i_sb
->s_dev
;
23 stat
->ino
= inode
->i_ino
;
24 stat
->mode
= inode
->i_mode
;
25 stat
->nlink
= inode
->i_nlink
;
26 stat
->uid
= inode
->i_uid
;
27 stat
->gid
= inode
->i_gid
;
28 stat
->rdev
= inode
->i_rdev
;
29 stat
->atime
= inode
->i_atime
;
30 stat
->mtime
= inode
->i_mtime
;
31 stat
->ctime
= inode
->i_ctime
;
32 stat
->size
= i_size_read(inode
);
33 stat
->blocks
= inode
->i_blocks
;
34 stat
->blksize
= inode
->i_blksize
;
37 EXPORT_SYMBOL(generic_fillattr
);
39 int vfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
41 struct inode
*inode
= dentry
->d_inode
;
44 retval
= security_inode_getattr(mnt
, dentry
);
48 if (inode
->i_op
->getattr
)
49 return inode
->i_op
->getattr(mnt
, dentry
, stat
);
51 generic_fillattr(inode
, stat
);
53 struct super_block
*s
= inode
->i_sb
;
55 blocks
= (stat
->size
+s
->s_blocksize
-1) >> s
->s_blocksize_bits
;
56 stat
->blocks
= (s
->s_blocksize
/ 512) * blocks
;
57 stat
->blksize
= s
->s_blocksize
;
62 EXPORT_SYMBOL(vfs_getattr
);
64 int vfs_stat(char __user
*name
, struct kstat
*stat
)
69 error
= user_path_walk(name
, &nd
);
71 error
= vfs_getattr(nd
.mnt
, nd
.dentry
, stat
);
77 EXPORT_SYMBOL(vfs_stat
);
79 int vfs_lstat(char __user
*name
, struct kstat
*stat
)
84 error
= user_path_walk_link(name
, &nd
);
86 error
= vfs_getattr(nd
.mnt
, nd
.dentry
, stat
);
92 EXPORT_SYMBOL(vfs_lstat
);
94 int vfs_fstat(unsigned int fd
, struct kstat
*stat
)
96 struct file
*f
= fget(fd
);
100 error
= vfs_getattr(f
->f_vfsmnt
, f
->f_dentry
, stat
);
106 EXPORT_SYMBOL(vfs_fstat
);
108 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) \
109 && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) \
110 && !defined(__arm__) && !defined(CONFIG_V850) && !defined(__powerpc64__)
113 * For backward compatibility? Maybe this should be moved
114 * into arch/i386 instead?
116 static int cp_old_stat(struct kstat
*stat
, struct __old_kernel_stat __user
* statbuf
)
118 static int warncount
= 5;
119 struct __old_kernel_stat tmp
;
123 printk(KERN_WARNING
"VFS: Warning: %s using old stat() call. Recompile your binary.\n",
125 } else if (warncount
< 0) {
126 /* it's laughable, but... */
130 memset(&tmp
, 0, sizeof(struct __old_kernel_stat
));
131 tmp
.st_dev
= old_encode_dev(stat
->dev
);
132 tmp
.st_ino
= stat
->ino
;
133 tmp
.st_mode
= stat
->mode
;
134 tmp
.st_nlink
= stat
->nlink
;
135 SET_UID(tmp
.st_uid
, stat
->uid
);
136 SET_GID(tmp
.st_gid
, stat
->gid
);
137 tmp
.st_rdev
= old_encode_dev(stat
->rdev
);
138 #if BITS_PER_LONG == 32
139 if (stat
->size
> MAX_NON_LFS
)
142 tmp
.st_size
= stat
->size
;
143 tmp
.st_atime
= stat
->atime
.tv_sec
;
144 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
145 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
146 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
149 asmlinkage
long sys_stat(char __user
* filename
, struct __old_kernel_stat __user
* statbuf
)
152 int error
= vfs_stat(filename
, &stat
);
155 error
= cp_old_stat(&stat
, statbuf
);
159 asmlinkage
long sys_lstat(char __user
* filename
, struct __old_kernel_stat __user
* statbuf
)
162 int error
= vfs_lstat(filename
, &stat
);
165 error
= cp_old_stat(&stat
, statbuf
);
169 asmlinkage
long sys_fstat(unsigned int fd
, struct __old_kernel_stat __user
* statbuf
)
172 int error
= vfs_fstat(fd
, &stat
);
175 error
= cp_old_stat(&stat
, statbuf
);
182 static int cp_new_stat(struct kstat
*stat
, struct stat __user
*statbuf
)
186 #if BITS_PER_LONG == 32
187 if (!old_valid_dev(stat
->dev
) || !old_valid_dev(stat
->rdev
))
190 if (!new_valid_dev(stat
->dev
) || !new_valid_dev(stat
->rdev
))
194 memset(&tmp
, 0, sizeof(tmp
));
195 #if BITS_PER_LONG == 32
196 tmp
.st_dev
= old_encode_dev(stat
->dev
);
198 tmp
.st_dev
= new_encode_dev(stat
->dev
);
200 tmp
.st_ino
= stat
->ino
;
201 tmp
.st_mode
= stat
->mode
;
202 tmp
.st_nlink
= stat
->nlink
;
203 SET_UID(tmp
.st_uid
, stat
->uid
);
204 SET_GID(tmp
.st_gid
, stat
->gid
);
205 #if BITS_PER_LONG == 32
206 tmp
.st_rdev
= old_encode_dev(stat
->rdev
);
208 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
210 #if BITS_PER_LONG == 32
211 if (stat
->size
> MAX_NON_LFS
)
214 tmp
.st_size
= stat
->size
;
215 tmp
.st_atime
= stat
->atime
.tv_sec
;
216 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
217 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
218 #ifdef STAT_HAVE_NSEC
219 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
220 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
221 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
223 tmp
.st_blocks
= stat
->blocks
;
224 tmp
.st_blksize
= stat
->blksize
;
225 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
228 asmlinkage
long sys_newstat(char __user
* filename
, struct stat __user
* statbuf
)
231 int error
= vfs_stat(filename
, &stat
);
234 error
= cp_new_stat(&stat
, statbuf
);
238 asmlinkage
long sys_newlstat(char __user
* filename
, struct stat __user
* statbuf
)
241 int error
= vfs_lstat(filename
, &stat
);
244 error
= cp_new_stat(&stat
, statbuf
);
248 asmlinkage
long sys_newfstat(unsigned int fd
, struct stat __user
* statbuf
)
251 int error
= vfs_fstat(fd
, &stat
);
254 error
= cp_new_stat(&stat
, statbuf
);
259 asmlinkage
long sys_readlink(const char __user
* path
, char __user
* buf
, int bufsiz
)
267 error
= user_path_walk_link(path
, &nd
);
269 struct inode
* inode
= nd
.dentry
->d_inode
;
272 if (inode
->i_op
&& inode
->i_op
->readlink
) {
273 error
= security_inode_readlink(nd
.dentry
);
275 touch_atime(nd
.mnt
, nd
.dentry
);
276 error
= inode
->i_op
->readlink(nd
.dentry
, buf
, bufsiz
);
285 /* ---------- LFS-64 ----------- */
286 #if !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
288 static long cp_new_stat64(struct kstat
*stat
, struct stat64 __user
*statbuf
)
292 memset(&tmp
, 0, sizeof(struct stat64
));
294 /* mips has weird padding, so we don't get 64 bits there */
295 if (!new_valid_dev(stat
->dev
) || !new_valid_dev(stat
->rdev
))
297 tmp
.st_dev
= new_encode_dev(stat
->dev
);
298 tmp
.st_rdev
= new_encode_dev(stat
->rdev
);
300 tmp
.st_dev
= huge_encode_dev(stat
->dev
);
301 tmp
.st_rdev
= huge_encode_dev(stat
->rdev
);
303 tmp
.st_ino
= stat
->ino
;
304 #ifdef STAT64_HAS_BROKEN_ST_INO
305 tmp
.__st_ino
= stat
->ino
;
307 tmp
.st_mode
= stat
->mode
;
308 tmp
.st_nlink
= stat
->nlink
;
309 tmp
.st_uid
= stat
->uid
;
310 tmp
.st_gid
= stat
->gid
;
311 tmp
.st_atime
= stat
->atime
.tv_sec
;
312 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
313 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
314 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
315 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
316 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
317 tmp
.st_size
= stat
->size
;
318 tmp
.st_blocks
= stat
->blocks
;
319 tmp
.st_blksize
= stat
->blksize
;
320 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
323 asmlinkage
long sys_stat64(char __user
* filename
, struct stat64 __user
* statbuf
)
326 int error
= vfs_stat(filename
, &stat
);
329 error
= cp_new_stat64(&stat
, statbuf
);
333 asmlinkage
long sys_lstat64(char __user
* filename
, struct stat64 __user
* statbuf
)
336 int error
= vfs_lstat(filename
, &stat
);
339 error
= cp_new_stat64(&stat
, statbuf
);
343 asmlinkage
long sys_fstat64(unsigned long fd
, struct stat64 __user
* statbuf
)
346 int error
= vfs_fstat(fd
, &stat
);
349 error
= cp_new_stat64(&stat
, statbuf
);
356 void inode_add_bytes(struct inode
*inode
, loff_t bytes
)
358 spin_lock(&inode
->i_lock
);
359 inode
->i_blocks
+= bytes
>> 9;
361 inode
->i_bytes
+= bytes
;
362 if (inode
->i_bytes
>= 512) {
364 inode
->i_bytes
-= 512;
366 spin_unlock(&inode
->i_lock
);
369 EXPORT_SYMBOL(inode_add_bytes
);
371 void inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
373 spin_lock(&inode
->i_lock
);
374 inode
->i_blocks
-= bytes
>> 9;
376 if (inode
->i_bytes
< bytes
) {
378 inode
->i_bytes
+= 512;
380 inode
->i_bytes
-= bytes
;
381 spin_unlock(&inode
->i_lock
);
384 EXPORT_SYMBOL(inode_sub_bytes
);
386 loff_t
inode_get_bytes(struct inode
*inode
)
390 spin_lock(&inode
->i_lock
);
391 ret
= (((loff_t
)inode
->i_blocks
) << 9) + inode
->i_bytes
;
392 spin_unlock(&inode
->i_lock
);
396 EXPORT_SYMBOL(inode_get_bytes
);
398 void inode_set_bytes(struct inode
*inode
, loff_t bytes
)
400 inode
->i_blocks
= bytes
>> 9;
401 inode
->i_bytes
= bytes
& 511;
404 EXPORT_SYMBOL(inode_set_bytes
);