4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/errno.h>
9 #include <linux/file.h>
10 #include <linux/smp_lock.h>
12 #include <asm/uaccess.h>
15 * Revalidate the inode. This is required for proper NFS attribute caching.
18 do_revalidate(struct dentry
*dentry
)
20 struct inode
* inode
= dentry
->d_inode
;
21 if (inode
->i_op
&& inode
->i_op
->revalidate
)
22 return inode
->i_op
->revalidate(dentry
);
27 #if !defined(__alpha__) && !defined(__sparc__)
30 * For backward compatibility? Maybe this should be moved
31 * into arch/i386 instead?
33 static int cp_old_stat(struct inode
* inode
, struct __old_kernel_stat
* statbuf
)
35 static int warncount
= 5;
36 struct __old_kernel_stat tmp
;
40 printk("VFS: Warning: %s using old stat() call. Recompile your binary.\n",
44 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
45 tmp
.st_ino
= inode
->i_ino
;
46 tmp
.st_mode
= inode
->i_mode
;
47 tmp
.st_nlink
= inode
->i_nlink
;
48 tmp
.st_uid
= inode
->i_uid
;
49 tmp
.st_gid
= inode
->i_gid
;
50 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
51 tmp
.st_size
= inode
->i_size
;
52 tmp
.st_atime
= inode
->i_atime
;
53 tmp
.st_mtime
= inode
->i_mtime
;
54 tmp
.st_ctime
= inode
->i_ctime
;
55 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
60 static int cp_new_stat(struct inode
* inode
, struct stat
* statbuf
)
63 unsigned int blocks
, indirect
;
65 memset(&tmp
, 0, sizeof(tmp
));
66 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
67 tmp
.st_ino
= inode
->i_ino
;
68 tmp
.st_mode
= inode
->i_mode
;
69 tmp
.st_nlink
= inode
->i_nlink
;
70 tmp
.st_uid
= inode
->i_uid
;
71 tmp
.st_gid
= inode
->i_gid
;
72 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
73 tmp
.st_size
= inode
->i_size
;
74 tmp
.st_atime
= inode
->i_atime
;
75 tmp
.st_mtime
= inode
->i_mtime
;
76 tmp
.st_ctime
= inode
->i_ctime
;
78 * st_blocks and st_blksize are approximated with a simple algorithm if
79 * they aren't supported directly by the filesystem. The minix and msdos
80 * filesystems don't keep track of blocks, so they would either have to
81 * be counted explicitly (by delving into the file itself), or by using
82 * this simple algorithm to get a reasonable (although not 100% accurate)
87 * Use minix fs values for the number of direct and indirect blocks. The
88 * count is now exact for the minix fs except that it counts zero blocks.
89 * Everything is in units of BLOCK_SIZE until the assignment to
93 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
95 if (!inode
->i_blksize
) {
96 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
98 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
101 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
107 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
108 tmp
.st_blksize
= BLOCK_SIZE
;
110 tmp
.st_blocks
= inode
->i_blocks
;
111 tmp
.st_blksize
= inode
->i_blksize
;
113 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
117 #if !defined(__alpha__) && !defined(__sparc__)
119 * For backward compatibility? Maybe this should be moved
120 * into arch/i386 instead?
122 asmlinkage
int sys_stat(char * filename
, struct __old_kernel_stat
* statbuf
)
124 struct dentry
* dentry
;
128 dentry
= namei(filename
);
130 error
= PTR_ERR(dentry
);
131 if (!IS_ERR(dentry
)) {
132 error
= do_revalidate(dentry
);
134 error
= cp_old_stat(dentry
->d_inode
, statbuf
);
143 asmlinkage
int sys_newstat(char * filename
, struct stat
* statbuf
)
145 struct dentry
* dentry
;
149 dentry
= namei(filename
);
151 error
= PTR_ERR(dentry
);
152 if (!IS_ERR(dentry
)) {
153 error
= do_revalidate(dentry
);
155 error
= cp_new_stat(dentry
->d_inode
, statbuf
);
163 #if !defined(__alpha__) && !defined(__sparc__)
166 * For backward compatibility? Maybe this should be moved
167 * into arch/i386 instead?
169 asmlinkage
int sys_lstat(char * filename
, struct __old_kernel_stat
* statbuf
)
171 struct dentry
* dentry
;
175 dentry
= lnamei(filename
);
177 error
= PTR_ERR(dentry
);
178 if (!IS_ERR(dentry
)) {
179 error
= do_revalidate(dentry
);
181 error
= cp_old_stat(dentry
->d_inode
, statbuf
);
191 asmlinkage
int sys_newlstat(char * filename
, struct stat
* statbuf
)
193 struct dentry
* dentry
;
197 dentry
= lnamei(filename
);
199 error
= PTR_ERR(dentry
);
200 if (!IS_ERR(dentry
)) {
201 error
= do_revalidate(dentry
);
203 error
= cp_new_stat(dentry
->d_inode
, statbuf
);
211 #if !defined(__alpha__) && !defined(__sparc__)
214 * For backward compatibility? Maybe this should be moved
215 * into arch/i386 instead?
217 asmlinkage
int sys_fstat(unsigned int fd
, struct __old_kernel_stat
* statbuf
)
225 struct dentry
* dentry
= f
->f_dentry
;
227 err
= do_revalidate(dentry
);
229 err
= cp_old_stat(dentry
->d_inode
, statbuf
);
238 asmlinkage
int sys_newfstat(unsigned int fd
, struct stat
* statbuf
)
246 struct dentry
* dentry
= f
->f_dentry
;
248 err
= do_revalidate(dentry
);
250 err
= cp_new_stat(dentry
->d_inode
, statbuf
);
257 asmlinkage
int sys_readlink(const char * path
, char * buf
, int bufsiz
)
259 struct dentry
* dentry
;
266 dentry
= lnamei(path
);
268 error
= PTR_ERR(dentry
);
269 if (!IS_ERR(dentry
)) {
270 struct inode
* inode
= dentry
->d_inode
;
273 if (inode
->i_op
&& inode
->i_op
->readlink
&&
274 !(error
= do_revalidate(dentry
))) {
276 error
= inode
->i_op
->readlink(dentry
, buf
, bufsiz
);