4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/errno.h>
8 #include <linux/string.h>
9 #include <linux/stat.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
17 #include <asm/uaccess.h>
20 * Revalidate the inode. This is required for proper NFS attribute caching.
23 do_revalidate(struct inode
*inode
)
25 if (inode
->i_op
&& inode
->i_op
->revalidate
)
26 return inode
->i_op
->revalidate(inode
);
31 #if !defined(__alpha__) && !defined(__sparc__)
34 * For backward compatibility? Maybe this should be moved
35 * into arch/i386 instead?
37 static int cp_old_stat(struct inode
* inode
, struct __old_kernel_stat
* statbuf
)
39 struct __old_kernel_stat tmp
;
41 printk("VFS: Warning: %s using old stat() call. Recompile your binary.\n",
43 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
44 tmp
.st_ino
= inode
->i_ino
;
45 tmp
.st_mode
= inode
->i_mode
;
46 tmp
.st_nlink
= inode
->i_nlink
;
47 tmp
.st_uid
= inode
->i_uid
;
48 tmp
.st_gid
= inode
->i_gid
;
49 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
50 tmp
.st_size
= inode
->i_size
;
51 tmp
.st_atime
= inode
->i_atime
;
52 tmp
.st_mtime
= inode
->i_mtime
;
53 tmp
.st_ctime
= inode
->i_ctime
;
54 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
59 static int cp_new_stat(struct inode
* inode
, struct stat
* statbuf
)
62 unsigned int blocks
, indirect
;
64 memset(&tmp
, 0, sizeof(tmp
));
65 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
66 tmp
.st_ino
= inode
->i_ino
;
67 tmp
.st_mode
= inode
->i_mode
;
68 tmp
.st_nlink
= inode
->i_nlink
;
69 tmp
.st_uid
= inode
->i_uid
;
70 tmp
.st_gid
= inode
->i_gid
;
71 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
72 tmp
.st_size
= inode
->i_size
;
73 tmp
.st_atime
= inode
->i_atime
;
74 tmp
.st_mtime
= inode
->i_mtime
;
75 tmp
.st_ctime
= inode
->i_ctime
;
77 * st_blocks and st_blksize are approximated with a simple algorithm if
78 * they aren't supported directly by the filesystem. The minix and msdos
79 * filesystems don't keep track of blocks, so they would either have to
80 * be counted explicitly (by delving into the file itself), or by using
81 * this simple algorithm to get a reasonable (although not 100% accurate)
86 * Use minix fs values for the number of direct and indirect blocks. The
87 * count is now exact for the minix fs except that it counts zero blocks.
88 * Everything is in BLOCK_SIZE'd units until the assignment to
92 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
94 if (!inode
->i_blksize
) {
95 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
97 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
100 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
106 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
107 tmp
.st_blksize
= BLOCK_SIZE
;
109 tmp
.st_blocks
= inode
->i_blocks
;
110 tmp
.st_blksize
= inode
->i_blksize
;
112 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
116 #if !defined(__alpha__) && !defined(__sparc__)
118 * For backward compatibility? Maybe this should be moved
119 * into arch/i386 instead?
121 asmlinkage
int sys_stat(char * filename
, struct __old_kernel_stat
* statbuf
)
123 struct dentry
* dentry
;
127 dentry
= namei(filename
);
129 error
= PTR_ERR(dentry
);
130 if (!IS_ERR(dentry
)) {
131 struct inode
* inode
= dentry
->d_inode
;
132 error
= do_revalidate(inode
);
134 error
= cp_old_stat(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 struct inode
* inode
= dentry
->d_inode
;
154 error
= do_revalidate(inode
);
156 error
= cp_new_stat(inode
,statbuf
);
164 #if !defined(__alpha__) && !defined(__sparc__)
167 * For backward compatibility? Maybe this should be moved
168 * into arch/i386 instead?
170 asmlinkage
int sys_lstat(char * filename
, struct __old_kernel_stat
* statbuf
)
172 struct dentry
* dentry
;
176 dentry
= lnamei(filename
);
178 error
= PTR_ERR(dentry
);
179 if (!IS_ERR(dentry
)) {
180 struct inode
* inode
= dentry
->d_inode
;
181 error
= do_revalidate(inode
);
183 error
= cp_old_stat(inode
, statbuf
);
193 asmlinkage
int sys_newlstat(char * filename
, struct stat
* statbuf
)
195 struct dentry
* dentry
;
199 dentry
= lnamei(filename
);
201 error
= PTR_ERR(dentry
);
202 if (!IS_ERR(dentry
)) {
203 struct inode
* inode
= dentry
->d_inode
;
204 error
= do_revalidate(inode
);
206 error
= cp_new_stat(inode
,statbuf
);
214 #if !defined(__alpha__) && !defined(__sparc__)
217 * For backward compatibility? Maybe this should be moved
218 * into arch/i386 instead?
220 asmlinkage
int sys_fstat(unsigned int fd
, struct __old_kernel_stat
* statbuf
)
226 if (fd
< NR_OPEN
&& (f
= current
->files
->fd
[fd
]) != NULL
) {
227 struct dentry
* dentry
= f
->f_dentry
;
228 struct inode
* inode
= dentry
->d_inode
;
230 err
= do_revalidate(inode
);
232 err
= cp_old_stat(inode
,statbuf
);
240 asmlinkage
int sys_newfstat(unsigned int fd
, struct stat
* statbuf
)
246 if (fd
< NR_OPEN
&& (f
= current
->files
->fd
[fd
]) != NULL
) {
247 struct dentry
* dentry
= f
->f_dentry
;
248 struct inode
* inode
= dentry
->d_inode
;
250 err
= do_revalidate(inode
);
252 err
= cp_new_stat(inode
,statbuf
);
258 asmlinkage
int sys_readlink(const char * path
, char * buf
, int bufsiz
)
260 struct dentry
* dentry
;
267 dentry
= lnamei(path
);
269 error
= PTR_ERR(dentry
);
270 if (!IS_ERR(dentry
)) {
271 struct inode
* inode
= dentry
->d_inode
;
274 if (inode
->i_op
&& inode
->i_op
->readlink
&& !(error
= do_revalidate(inode
))) {
276 error
= inode
->i_op
->readlink(inode
,buf
,bufsiz
);