4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/highuid.h>
14 #include <linux/namei.h>
15 #include <linux/security.h>
17 #include <asm/uaccess.h>
19 void generic_fillattr(struct inode
*inode
, struct kstat
*stat
)
21 stat
->dev
= inode
->i_sb
->s_dev
;
22 stat
->ino
= inode
->i_ino
;
23 stat
->mode
= inode
->i_mode
;
24 stat
->nlink
= inode
->i_nlink
;
25 stat
->uid
= inode
->i_uid
;
26 stat
->gid
= inode
->i_gid
;
27 stat
->rdev
= kdev_t_to_nr(inode
->i_rdev
);
28 stat
->atime
= inode
->i_atime
;
29 stat
->mtime
= inode
->i_mtime
;
30 stat
->ctime
= inode
->i_ctime
;
31 stat
->size
= i_size_read(inode
);
32 stat
->blocks
= inode
->i_blocks
;
33 stat
->blksize
= inode
->i_blksize
;
36 int vfs_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
38 struct inode
*inode
= dentry
->d_inode
;
41 retval
= security_inode_getattr(mnt
, dentry
);
45 if (inode
->i_op
->getattr
)
46 return inode
->i_op
->getattr(mnt
, dentry
, stat
);
48 generic_fillattr(inode
, stat
);
50 struct super_block
*s
= inode
->i_sb
;
52 blocks
= (stat
->size
+s
->s_blocksize
-1) >> s
->s_blocksize_bits
;
53 stat
->blocks
= (s
->s_blocksize
/ 512) * blocks
;
54 stat
->blksize
= s
->s_blocksize
;
59 int vfs_stat(char __user
*name
, struct kstat
*stat
)
64 error
= user_path_walk(name
, &nd
);
66 error
= vfs_getattr(nd
.mnt
, nd
.dentry
, stat
);
72 int vfs_lstat(char __user
*name
, struct kstat
*stat
)
77 error
= user_path_walk_link(name
, &nd
);
79 error
= vfs_getattr(nd
.mnt
, nd
.dentry
, stat
);
85 int vfs_fstat(unsigned int fd
, struct kstat
*stat
)
87 struct file
*f
= fget(fd
);
91 error
= vfs_getattr(f
->f_vfsmnt
, f
->f_dentry
, stat
);
97 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) \
98 && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) \
99 && !defined(__arm__) && !defined(CONFIG_V850) && !defined(__powerpc64__)
102 * For backward compatibility? Maybe this should be moved
103 * into arch/i386 instead?
105 static int cp_old_stat(struct kstat
*stat
, struct __old_kernel_stat __user
* statbuf
)
107 static int warncount
= 5;
108 struct __old_kernel_stat tmp
;
112 printk(KERN_WARNING
"VFS: Warning: %s using old stat() call. Recompile your binary.\n",
114 } else if (warncount
< 0) {
115 /* it's laughable, but... */
119 tmp
.st_dev
= stat
->dev
;
120 tmp
.st_ino
= stat
->ino
;
121 tmp
.st_mode
= stat
->mode
;
122 tmp
.st_nlink
= stat
->nlink
;
123 SET_OLDSTAT_UID(tmp
, stat
->uid
);
124 SET_OLDSTAT_GID(tmp
, stat
->gid
);
125 tmp
.st_rdev
= stat
->rdev
;
126 #if BITS_PER_LONG == 32
127 if (stat
->size
> MAX_NON_LFS
)
130 tmp
.st_size
= stat
->size
;
131 tmp
.st_atime
= stat
->atime
.tv_sec
;
132 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
133 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
134 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
137 asmlinkage
long sys_stat(char __user
* filename
, struct __old_kernel_stat __user
* statbuf
)
140 int error
= vfs_stat(filename
, &stat
);
143 error
= cp_old_stat(&stat
, statbuf
);
147 asmlinkage
long sys_lstat(char __user
* filename
, struct __old_kernel_stat __user
* statbuf
)
150 int error
= vfs_lstat(filename
, &stat
);
153 error
= cp_old_stat(&stat
, statbuf
);
157 asmlinkage
long sys_fstat(unsigned int fd
, struct __old_kernel_stat __user
* statbuf
)
160 int error
= vfs_fstat(fd
, &stat
);
163 error
= cp_old_stat(&stat
, statbuf
);
170 static int cp_new_stat(struct kstat
*stat
, struct stat __user
*statbuf
)
174 memset(&tmp
, 0, sizeof(tmp
));
175 tmp
.st_dev
= stat
->dev
;
176 tmp
.st_ino
= stat
->ino
;
177 tmp
.st_mode
= stat
->mode
;
178 tmp
.st_nlink
= stat
->nlink
;
179 SET_STAT_UID(tmp
, stat
->uid
);
180 SET_STAT_GID(tmp
, stat
->gid
);
181 tmp
.st_rdev
= stat
->rdev
;
182 #if BITS_PER_LONG == 32
183 if (stat
->size
> MAX_NON_LFS
)
186 tmp
.st_size
= stat
->size
;
187 tmp
.st_atime
= stat
->atime
.tv_sec
;
188 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
189 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
190 #ifdef STAT_HAVE_NSEC
191 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
192 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
193 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
195 tmp
.st_blocks
= stat
->blocks
;
196 tmp
.st_blksize
= stat
->blksize
;
197 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
200 asmlinkage
long sys_newstat(char __user
* filename
, struct stat __user
* statbuf
)
203 int error
= vfs_stat(filename
, &stat
);
206 error
= cp_new_stat(&stat
, statbuf
);
210 asmlinkage
long sys_newlstat(char __user
* filename
, struct stat __user
* statbuf
)
213 int error
= vfs_lstat(filename
, &stat
);
216 error
= cp_new_stat(&stat
, statbuf
);
220 asmlinkage
long sys_newfstat(unsigned int fd
, struct stat __user
* statbuf
)
223 int error
= vfs_fstat(fd
, &stat
);
226 error
= cp_new_stat(&stat
, statbuf
);
231 asmlinkage
long sys_readlink(const char __user
* path
, char __user
* buf
, int bufsiz
)
239 error
= user_path_walk_link(path
, &nd
);
241 struct inode
* inode
= nd
.dentry
->d_inode
;
244 if (inode
->i_op
&& inode
->i_op
->readlink
) {
245 error
= security_inode_readlink(nd
.dentry
);
248 error
= inode
->i_op
->readlink(nd
.dentry
, buf
, bufsiz
);
257 /* ---------- LFS-64 ----------- */
258 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
260 static long cp_new_stat64(struct kstat
*stat
, struct stat64 __user
*statbuf
)
264 memset(&tmp
, 0, sizeof(struct stat64
));
265 tmp
.st_dev
= stat
->dev
;
266 tmp
.st_ino
= stat
->ino
;
267 #ifdef STAT64_HAS_BROKEN_ST_INO
268 tmp
.__st_ino
= stat
->ino
;
270 tmp
.st_mode
= stat
->mode
;
271 tmp
.st_nlink
= stat
->nlink
;
272 tmp
.st_uid
= stat
->uid
;
273 tmp
.st_gid
= stat
->gid
;
274 tmp
.st_rdev
= stat
->rdev
;
275 tmp
.st_atime
= stat
->atime
.tv_sec
;
276 tmp
.st_atime_nsec
= stat
->atime
.tv_nsec
;
277 tmp
.st_mtime
= stat
->mtime
.tv_sec
;
278 tmp
.st_mtime_nsec
= stat
->mtime
.tv_nsec
;
279 tmp
.st_ctime
= stat
->ctime
.tv_sec
;
280 tmp
.st_ctime_nsec
= stat
->ctime
.tv_nsec
;
281 tmp
.st_size
= stat
->size
;
282 tmp
.st_blocks
= stat
->blocks
;
283 tmp
.st_blksize
= stat
->blksize
;
284 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
287 asmlinkage
long sys_stat64(char __user
* filename
, struct stat64 __user
* statbuf
, long flags
)
290 int error
= vfs_stat(filename
, &stat
);
293 error
= cp_new_stat64(&stat
, statbuf
);
297 asmlinkage
long sys_lstat64(char __user
* filename
, struct stat64 __user
* statbuf
, long flags
)
300 int error
= vfs_lstat(filename
, &stat
);
303 error
= cp_new_stat64(&stat
, statbuf
);
307 asmlinkage
long sys_fstat64(unsigned long fd
, struct stat64 __user
* statbuf
, long flags
)
310 int error
= vfs_fstat(fd
, &stat
);
313 error
= cp_new_stat64(&stat
, statbuf
);
320 void inode_add_bytes(struct inode
*inode
, loff_t bytes
)
322 spin_lock(&inode
->i_lock
);
323 inode
->i_blocks
+= bytes
>> 9;
325 inode
->i_bytes
+= bytes
;
326 if (inode
->i_bytes
>= 512) {
328 inode
->i_bytes
-= 512;
330 spin_unlock(&inode
->i_lock
);
333 void inode_sub_bytes(struct inode
*inode
, loff_t bytes
)
335 spin_lock(&inode
->i_lock
);
336 inode
->i_blocks
-= bytes
>> 9;
338 if (inode
->i_bytes
< bytes
) {
340 inode
->i_bytes
+= 512;
342 inode
->i_bytes
-= bytes
;
343 spin_unlock(&inode
->i_lock
);
346 loff_t
inode_get_bytes(struct inode
*inode
)
350 spin_lock(&inode
->i_lock
);
351 ret
= (((loff_t
)inode
->i_blocks
) << 9) + inode
->i_bytes
;
352 spin_unlock(&inode
->i_lock
);
356 void inode_set_bytes(struct inode
*inode
, loff_t bytes
)
358 inode
->i_blocks
= bytes
>> 9;
359 inode
->i_bytes
= bytes
& 511;