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",
45 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
46 tmp
.st_ino
= inode
->i_ino
;
47 tmp
.st_mode
= inode
->i_mode
;
48 tmp
.st_nlink
= inode
->i_nlink
;
49 SET_OLDSTAT_UID(tmp
, inode
->i_uid
);
50 SET_OLDSTAT_GID(tmp
, inode
->i_gid
);
51 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
52 tmp
.st_size
= inode
->i_size
;
53 tmp
.st_atime
= inode
->i_atime
;
54 tmp
.st_mtime
= inode
->i_mtime
;
55 tmp
.st_ctime
= inode
->i_ctime
;
56 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
61 static int cp_new_stat(struct inode
* inode
, struct stat
* statbuf
)
64 unsigned int blocks
, indirect
;
66 memset(&tmp
, 0, sizeof(tmp
));
67 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
68 tmp
.st_ino
= inode
->i_ino
;
69 tmp
.st_mode
= inode
->i_mode
;
70 tmp
.st_nlink
= inode
->i_nlink
;
71 SET_STAT_UID(tmp
, inode
->i_uid
);
72 SET_STAT_GID(tmp
, inode
->i_gid
);
73 tmp
.st_rdev
= kdev_t_to_nr(inode
->i_rdev
);
74 tmp
.st_size
= inode
->i_size
;
75 tmp
.st_atime
= inode
->i_atime
;
76 tmp
.st_mtime
= inode
->i_mtime
;
77 tmp
.st_ctime
= inode
->i_ctime
;
79 * st_blocks and st_blksize are approximated with a simple algorithm if
80 * they aren't supported directly by the filesystem. The minix and msdos
81 * filesystems don't keep track of blocks, so they would either have to
82 * be counted explicitly (by delving into the file itself), or by using
83 * this simple algorithm to get a reasonable (although not 100% accurate)
88 * Use minix fs values for the number of direct and indirect blocks. The
89 * count is now exact for the minix fs except that it counts zero blocks.
90 * Everything is in units of BLOCK_SIZE until the assignment to
94 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
96 if (!inode
->i_blksize
) {
97 blocks
= (tmp
.st_size
+ BLOCK_SIZE
- 1) / BLOCK_SIZE
;
99 indirect
= (blocks
- D_B
+ I_B
- 1) / I_B
;
102 indirect
= (indirect
- 1 + I_B
- 1) / I_B
;
108 tmp
.st_blocks
= (BLOCK_SIZE
/ 512) * blocks
;
109 tmp
.st_blksize
= BLOCK_SIZE
;
111 tmp
.st_blocks
= inode
->i_blocks
;
112 tmp
.st_blksize
= inode
->i_blksize
;
114 return copy_to_user(statbuf
,&tmp
,sizeof(tmp
)) ? -EFAULT
: 0;
118 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
120 * For backward compatibility? Maybe this should be moved
121 * into arch/i386 instead?
123 asmlinkage
long sys_stat(char * filename
, struct __old_kernel_stat
* statbuf
)
129 error
= user_path_walk(filename
, &nd
);
131 error
= do_revalidate(nd
.dentry
);
133 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
141 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
);
158 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
161 * For backward compatibility? Maybe this should be moved
162 * into arch/i386 instead?
164 asmlinkage
long sys_lstat(char * filename
, struct __old_kernel_stat
* statbuf
)
170 error
= user_path_walk_link(filename
, &nd
);
172 error
= do_revalidate(nd
.dentry
);
174 error
= cp_old_stat(nd
.dentry
->d_inode
, statbuf
);
183 asmlinkage
long sys_newlstat(char * filename
, struct stat
* statbuf
)
189 error
= user_path_walk_link(filename
, &nd
);
191 error
= do_revalidate(nd
.dentry
);
193 error
= cp_new_stat(nd
.dentry
->d_inode
, statbuf
);
200 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
203 * For backward compatibility? Maybe this should be moved
204 * into arch/i386 instead?
206 asmlinkage
long sys_fstat(unsigned int fd
, struct __old_kernel_stat
* statbuf
)
214 struct dentry
* dentry
= f
->f_dentry
;
216 err
= do_revalidate(dentry
);
218 err
= cp_old_stat(dentry
->d_inode
, statbuf
);
227 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
);
246 asmlinkage
long sys_readlink(const char * path
, char * buf
, int bufsiz
)
255 error
= user_path_walk_link(path
, &nd
);
257 struct inode
* inode
= nd
.dentry
->d_inode
;
260 if (inode
->i_op
&& inode
->i_op
->readlink
&&
261 !(error
= do_revalidate(nd
.dentry
))) {
263 error
= inode
->i_op
->readlink(nd
.dentry
, buf
, bufsiz
);
272 /* ---------- LFS-64 ----------- */
273 #if !defined(__alpha__) && !defined (__ia64__) && !defined(__mips64)
275 static long cp_new_stat64(struct inode
* inode
, struct stat64
* statbuf
)
278 unsigned int blocks
, indirect
;
280 memset(&tmp
, 0, sizeof(tmp
));
281 tmp
.st_dev
= kdev_t_to_nr(inode
->i_dev
);
282 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
)
337 error
= user_path_walk(filename
, &nd
);
339 error
= do_revalidate(nd
.dentry
);
341 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
348 asmlinkage
long sys_lstat64(char * filename
, struct stat64
* statbuf
, long flags
)
354 error
= user_path_walk_link(filename
, &nd
);
356 error
= do_revalidate(nd
.dentry
);
358 error
= cp_new_stat64(nd
.dentry
->d_inode
, statbuf
);
365 asmlinkage
long sys_fstat64(unsigned long fd
, struct stat64
* statbuf
, long flags
)
373 struct dentry
* dentry
= f
->f_dentry
;
375 err
= do_revalidate(dentry
);
377 err
= cp_new_stat64(dentry
->d_inode
, statbuf
);