4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/errno.h>
9 #include <linux/file.h>
10 #include <linux/smp_lock.h>
11 #include <linux/highuid.h>
13 #include <asm/uaccess.h>
16 * Revalidate the inode. This is required for proper NFS attribute caching.
19 do_revalidate(struct dentry
*dentry
)
21 struct inode
* inode
= dentry
->d_inode
;
22 if (inode
->i_op
&& inode
->i_op
->revalidate
)
23 return inode
->i_op
->revalidate(dentry
);
28 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
31 * For backward compatibility? Maybe this should be moved
32 * into arch/i386 instead?
34 static int cp_old_stat(struct inode
* inode
, struct __old_kernel_stat
* statbuf
)
36 static int warncount
= 5;
37 struct __old_kernel_stat tmp
;
41 printk("VFS: Warning: %s using old stat() call. Recompile your binary.\n",
43 } else if (warncount
< 0) {
44 /* it's laughable, but... */
48 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
49 tmp
.st_ino
= inode
->i_ino
;
50 tmp
.st_mode
= inode
->i_mode
;
51 tmp
.st_nlink
= inode
->i_nlink
;
52 SET_OLDSTAT_UID(tmp
, inode
->i_uid
);
53 SET_OLDSTAT_GID(tmp
, inode
->i_gid
);
54 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
55 tmp
.st_size
= inode
->i_size
;
56 tmp
.st_atime
= inode
->i_atime
;
57 tmp
.st_mtime
= inode
->i_mtime
;
58 tmp
.st_ctime
= inode
->i_ctime
;
59 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
64 static int cp_new_stat(struct inode
* inode
, struct stat
* statbuf
)
67 unsigned int blocks
, indirect
;
69 memset(&tmp
, 0, sizeof(tmp
));
70 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
71 tmp
.st_ino
= inode
->i_ino
;
72 tmp
.st_mode
= inode
->i_mode
;
73 tmp
.st_nlink
= inode
->i_nlink
;
74 SET_STAT_UID(tmp
, inode
->i_uid
);
75 SET_STAT_GID(tmp
, inode
->i_gid
);
76 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
77 tmp
.st_size
= inode
->i_size
;
78 tmp
.st_atime
= inode
->i_atime
;
79 tmp
.st_mtime
= inode
->i_mtime
;
80 tmp
.st_ctime
= inode
->i_ctime
;
82 * st_blocks and st_blksize are approximated with a simple algorithm if
83 * they aren't supported directly by the filesystem. The minix and msdos
84 * filesystems don't keep track of blocks, so they would either have to
85 * be counted explicitly (by delving into the file itself), or by using
86 * this simple algorithm to get a reasonable (although not 100% accurate)
91 * Use minix fs values for the number of direct and indirect blocks. The
92 * count is now exact for the minix fs except that it counts zero blocks.
93 * Everything is in units of BLOCK_SIZE until the assignment to
97 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
99 if (!inode
->i_blksize
) {
100 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
102 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
105 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
111 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
112 tmp
.st_blksize
= BLOCK_SIZE
;
114 tmp
.st_blocks
= inode
->i_blocks
;
115 tmp
.st_blksize
= inode
->i_blksize
;
117 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
121 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
123 * For backward compatibility? Maybe this should be moved
124 * into arch/i386 instead?
126 asmlinkage
long sys_stat(char * filename
, struct __old_kernel_stat
* statbuf
)
131 error
= user_path_walk(filename
, &nd
);
133 error
= do_revalidate(nd
.dentry
);
135 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
142 asmlinkage
long sys_newstat(char * filename
, struct stat
* statbuf
)
147 error
= user_path_walk(filename
, &nd
);
149 error
= do_revalidate(nd
.dentry
);
151 error
= cp_new_stat(nd
.dentry
->d_inode
, statbuf
);
157 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
160 * For backward compatibility? Maybe this should be moved
161 * into arch/i386 instead?
163 asmlinkage
long sys_lstat(char * filename
, struct __old_kernel_stat
* statbuf
)
168 error
= user_path_walk_link(filename
, &nd
);
170 error
= do_revalidate(nd
.dentry
);
172 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
180 asmlinkage
long sys_newlstat(char * filename
, struct stat
* statbuf
)
185 error
= user_path_walk_link(filename
, &nd
);
187 error
= do_revalidate(nd
.dentry
);
189 error
= cp_new_stat(nd
.dentry
->d_inode
, statbuf
);
195 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
198 * For backward compatibility? Maybe this should be moved
199 * into arch/i386 instead?
201 asmlinkage
long sys_fstat(unsigned int fd
, struct __old_kernel_stat
* statbuf
)
208 struct dentry
* dentry
= f
->f_dentry
;
210 err
= do_revalidate(dentry
);
212 err
= cp_old_stat(dentry
->d_inode
, statbuf
);
220 asmlinkage
long sys_newfstat(unsigned int fd
, struct stat
* statbuf
)
227 struct dentry
* dentry
= f
->f_dentry
;
229 err
= do_revalidate(dentry
);
231 err
= cp_new_stat(dentry
->d_inode
, statbuf
);
237 asmlinkage
long sys_readlink(const char * path
, char * buf
, int bufsiz
)
245 error
= user_path_walk_link(path
, &nd
);
247 struct inode
* inode
= nd
.dentry
->d_inode
;
250 if (inode
->i_op
&& inode
->i_op
->readlink
&&
251 !(error
= do_revalidate(nd
.dentry
))) {
253 error
= inode
->i_op
->readlink(nd
.dentry
, buf
, bufsiz
);
261 /* ---------- LFS-64 ----------- */
262 #if !defined(__alpha__) && !defined (__ia64__) && !defined(__mips64)
264 static long cp_new_stat64(struct inode
* inode
, struct stat64
* statbuf
)
267 unsigned int blocks
, indirect
;
269 memset(&tmp
, 0, sizeof(tmp
));
270 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
271 tmp
.st_ino
= inode
->i_ino
;
272 tmp
.st_mode
= inode
->i_mode
;
273 tmp
.st_nlink
= inode
->i_nlink
;
274 tmp
.st_uid
= inode
->i_uid
;
275 tmp
.st_gid
= inode
->i_gid
;
276 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
277 tmp
.st_atime
= inode
->i_atime
;
278 tmp
.st_mtime
= inode
->i_mtime
;
279 tmp
.st_ctime
= inode
->i_ctime
;
280 tmp
.st_size
= inode
->i_size
;
282 * st_blocks and st_blksize are approximated with a simple algorithm if
283 * they aren't supported directly by the filesystem. The minix and msdos
284 * filesystems don't keep track of blocks, so they would either have to
285 * be counted explicitly (by delving into the file itself), or by using
286 * this simple algorithm to get a reasonable (although not 100% accurate)
291 * Use minix fs values for the number of direct and indirect blocks. The
292 * count is now exact for the minix fs except that it counts zero blocks.
293 * Everything is in units of BLOCK_SIZE until the assignment to
297 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
299 if (!inode
->i_blksize
) {
300 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
302 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
305 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
311 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
312 tmp
.st_blksize
= BLOCK_SIZE
;
314 tmp
.st_blocks
= inode
->i_blocks
;
315 tmp
.st_blksize
= inode
->i_blksize
;
317 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
320 asmlinkage
long sys_stat64(char * filename
, struct stat64
* statbuf
, long flags
)
325 error
= user_path_walk(filename
, &nd
);
327 error
= do_revalidate(nd
.dentry
);
329 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
335 asmlinkage
long sys_lstat64(char * filename
, struct stat64
* statbuf
, long flags
)
340 error
= user_path_walk_link(filename
, &nd
);
342 error
= do_revalidate(nd
.dentry
);
344 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
350 asmlinkage
long sys_fstat64(unsigned long fd
, struct stat64
* statbuf
, long flags
)
357 struct dentry
* dentry
= f
->f_dentry
;
359 err
= do_revalidate(dentry
);
361 err
= cp_new_stat64(dentry
->d_inode
, statbuf
);