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__) && !defined(__hppa__)
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 #if BITS_PER_LONG == 32
56 if (inode
->i_size
> 0x7fffffff)
59 tmp
.st_size
= inode
->i_size
;
60 tmp
.st_atime
= inode
->i_atime
;
61 tmp
.st_mtime
= inode
->i_mtime
;
62 tmp
.st_ctime
= inode
->i_ctime
;
63 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
68 static int cp_new_stat(struct inode
* inode
, struct stat
* statbuf
)
71 unsigned int blocks
, indirect
;
73 memset(&tmp
, 0, sizeof(tmp
));
74 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
75 tmp
.st_ino
= inode
->i_ino
;
76 tmp
.st_mode
= inode
->i_mode
;
77 tmp
.st_nlink
= inode
->i_nlink
;
78 SET_STAT_UID(tmp
, inode
->i_uid
);
79 SET_STAT_GID(tmp
, inode
->i_gid
);
80 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
81 #if BITS_PER_LONG == 32
82 if (inode
->i_size
> 0x7fffffff)
85 tmp
.st_size
= inode
->i_size
;
86 tmp
.st_atime
= inode
->i_atime
;
87 tmp
.st_mtime
= inode
->i_mtime
;
88 tmp
.st_ctime
= inode
->i_ctime
;
90 * st_blocks and st_blksize are approximated with a simple algorithm if
91 * they aren't supported directly by the filesystem. The minix and msdos
92 * filesystems don't keep track of blocks, so they would either have to
93 * be counted explicitly (by delving into the file itself), or by using
94 * this simple algorithm to get a reasonable (although not 100% accurate)
99 * Use minix fs values for the number of direct and indirect blocks. The
100 * count is now exact for the minix fs except that it counts zero blocks.
101 * Everything is in units of BLOCK_SIZE until the assignment to
105 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
107 if (!inode
->i_blksize
) {
108 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
110 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
113 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
119 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
120 tmp
.st_blksize
= BLOCK_SIZE
;
122 tmp
.st_blocks
= inode
->i_blocks
;
123 tmp
.st_blksize
= inode
->i_blksize
;
125 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
129 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__) && !defined(__hppa__)
131 * For backward compatibility? Maybe this should be moved
132 * into arch/i386 instead?
134 asmlinkage
long sys_stat(char * filename
, struct __old_kernel_stat
* statbuf
)
139 error
= user_path_walk(filename
, &nd
);
141 error
= do_revalidate(nd
.dentry
);
143 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
150 asmlinkage
long sys_newstat(char * filename
, struct stat
* statbuf
)
155 error
= user_path_walk(filename
, &nd
);
157 error
= do_revalidate(nd
.dentry
);
159 error
= cp_new_stat(nd
.dentry
->d_inode
, statbuf
);
165 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__) && !defined(__hppa__)
168 * For backward compatibility? Maybe this should be moved
169 * into arch/i386 instead?
171 asmlinkage
long sys_lstat(char * filename
, struct __old_kernel_stat
* statbuf
)
176 error
= user_path_walk_link(filename
, &nd
);
178 error
= do_revalidate(nd
.dentry
);
180 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
188 asmlinkage
long sys_newlstat(char * filename
, struct stat
* statbuf
)
193 error
= user_path_walk_link(filename
, &nd
);
195 error
= do_revalidate(nd
.dentry
);
197 error
= cp_new_stat(nd
.dentry
->d_inode
, statbuf
);
203 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__) && !defined(__hppa__)
206 * For backward compatibility? Maybe this should be moved
207 * into arch/i386 instead?
209 asmlinkage
long sys_fstat(unsigned int fd
, struct __old_kernel_stat
* statbuf
)
216 struct dentry
* dentry
= f
->f_dentry
;
218 err
= do_revalidate(dentry
);
220 err
= cp_old_stat(dentry
->d_inode
, statbuf
);
228 asmlinkage
long sys_newfstat(unsigned int fd
, struct stat
* statbuf
)
235 struct dentry
* dentry
= f
->f_dentry
;
237 err
= do_revalidate(dentry
);
239 err
= cp_new_stat(dentry
->d_inode
, statbuf
);
245 asmlinkage
long sys_readlink(const char * path
, char * buf
, int bufsiz
)
253 error
= user_path_walk_link(path
, &nd
);
255 struct inode
* inode
= nd
.dentry
->d_inode
;
258 if (inode
->i_op
&& inode
->i_op
->readlink
&&
259 !(error
= do_revalidate(nd
.dentry
))) {
261 error
= inode
->i_op
->readlink(nd
.dentry
, buf
, bufsiz
);
269 /* ---------- LFS-64 ----------- */
270 #if !defined(__alpha__) && !defined (__ia64__) && !defined(__mips64)
272 static long cp_new_stat64(struct inode
* inode
, struct stat64
* statbuf
)
275 unsigned int blocks
, indirect
;
277 memset(&tmp
, 0, sizeof(tmp
));
278 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
279 tmp
.st_ino
= inode
->i_ino
;
280 #ifdef STAT64_HAS_BROKEN_ST_INO
281 tmp
.__st_ino
= inode
->i_ino
;
283 tmp
.st_mode
= inode
->i_mode
;
284 tmp
.st_nlink
= inode
->i_nlink
;
285 tmp
.st_uid
= inode
->i_uid
;
286 tmp
.st_gid
= inode
->i_gid
;
287 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
288 tmp
.st_atime
= inode
->i_atime
;
289 tmp
.st_mtime
= inode
->i_mtime
;
290 tmp
.st_ctime
= inode
->i_ctime
;
291 tmp
.st_size
= inode
->i_size
;
293 * st_blocks and st_blksize are approximated with a simple algorithm if
294 * they aren't supported directly by the filesystem. The minix and msdos
295 * filesystems don't keep track of blocks, so they would either have to
296 * be counted explicitly (by delving into the file itself), or by using
297 * this simple algorithm to get a reasonable (although not 100% accurate)
302 * Use minix fs values for the number of direct and indirect blocks. The
303 * count is now exact for the minix fs except that it counts zero blocks.
304 * Everything is in units of BLOCK_SIZE until the assignment to
308 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
310 if (!inode
->i_blksize
) {
311 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) >> BLOCK_SIZE_BITS
;
313 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
316 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
322 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
323 tmp
.st_blksize
= BLOCK_SIZE
;
325 tmp
.st_blocks
= inode
->i_blocks
;
326 tmp
.st_blksize
= inode
->i_blksize
;
328 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
331 asmlinkage
long sys_stat64(char * filename
, struct stat64
* statbuf
, long flags
)
336 error
= user_path_walk(filename
, &nd
);
338 error
= do_revalidate(nd
.dentry
);
340 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
346 asmlinkage
long sys_lstat64(char * filename
, struct stat64
* statbuf
, long flags
)
351 error
= user_path_walk_link(filename
, &nd
);
353 error
= do_revalidate(nd
.dentry
);
355 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
361 asmlinkage
long sys_fstat64(unsigned long fd
, struct stat64
* statbuf
, long flags
)
368 struct dentry
* dentry
= f
->f_dentry
;
370 err
= do_revalidate(dentry
);
372 err
= cp_new_stat64(dentry
->d_inode
, statbuf
);