4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/highuid.h>
14 #include <asm/uaccess.h>
17 * Revalidate the inode. This is required for proper NFS attribute caching.
20 do_revalidate(struct dentry *dentry)
22 struct inode * inode = dentry->d_inode;
23 if (inode->i_op && inode->i_op->revalidate)
24 return inode->i_op->revalidate(dentry);
29 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
32 * For backward compatibility? Maybe this should be moved
33 * into arch/i386 instead?
35 static int cp_old_stat(struct inode * inode, struct __old_kernel_stat * statbuf)
37 static int warncount = 5;
38 struct __old_kernel_stat tmp;
40 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
44 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
46 } else if (warncount < 0) {
47 /* it's laughable, but... */
51 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
52 tmp.st_ino = inode->i_ino;
53 tmp.st_mode = inode->i_mode;
54 tmp.st_nlink = inode->i_nlink;
55 if (tmp.st_nlink != inode->i_nlink)
57 SET_OLDSTAT_UID(tmp, inode->i_uid);
58 SET_OLDSTAT_GID(tmp, inode->i_gid);
59 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
60 #if BITS_PER_LONG == 32
61 if (inode->i_size > MAX_NON_LFS)
64 tmp.st_size = inode->i_size;
65 tmp.st_atime = inode->i_atime;
66 tmp.st_mtime = inode->i_mtime;
67 tmp.st_ctime = inode->i_ctime;
68 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
73 static int cp_new_stat(struct inode * inode, struct stat * statbuf)
76 unsigned int blocks, indirect;
78 memset(&tmp, 0, sizeof(tmp));
79 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
80 tmp.st_ino = inode->i_ino;
81 tmp.st_mode = inode->i_mode;
82 tmp.st_nlink = inode->i_nlink;
83 if (tmp.st_nlink != inode->i_nlink)
85 SET_STAT_UID(tmp, inode->i_uid);
86 SET_STAT_GID(tmp, inode->i_gid);
87 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
88 #if BITS_PER_LONG == 32
89 if (inode->i_size > MAX_NON_LFS)
92 tmp.st_size = inode->i_size;
93 tmp.st_atime = inode->i_atime;
94 tmp.st_mtime = inode->i_mtime;
95 tmp.st_ctime = inode->i_ctime;
97 * st_blocks and st_blksize are approximated with a simple algorithm if
98 * they aren't supported directly by the filesystem. The minix and msdos
99 * filesystems don't keep track of blocks, so they would either have to
100 * be counted explicitly (by delving into the file itself), or by using
101 * this simple algorithm to get a reasonable (although not 100% accurate)
106 * Use minix fs values for the number of direct and indirect blocks. The
107 * count is now exact for the minix fs except that it counts zero blocks.
108 * Everything is in units of BLOCK_SIZE until the assignment to
112 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
114 if (!inode->i_blksize) {
115 blocks = (tmp.st_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
117 indirect = (blocks - D_B + I_B - 1) / I_B;
120 indirect = (indirect - 1 + I_B - 1) / I_B;
126 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
127 tmp.st_blksize = BLOCK_SIZE;
129 tmp.st_blocks = inode->i_blocks;
130 tmp.st_blksize = inode->i_blksize;
132 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
136 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
138 * For backward compatibility? Maybe this should be moved
139 * into arch/i386 instead?
141 asmlinkage long sys_stat(char * filename, struct __old_kernel_stat * statbuf)
146 error = user_path_walk(filename, &nd);
148 error = do_revalidate(nd.dentry);
150 error = cp_old_stat(nd.dentry->d_inode, statbuf);
157 asmlinkage long sys_newstat(char * filename, struct stat * statbuf)
162 error = user_path_walk(filename, &nd);
164 error = do_revalidate(nd.dentry);
166 error = cp_new_stat(nd.dentry->d_inode, statbuf);
172 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
175 * For backward compatibility? Maybe this should be moved
176 * into arch/i386 instead?
178 asmlinkage long sys_lstat(char * filename, struct __old_kernel_stat * statbuf)
183 error = user_path_walk_link(filename, &nd);
185 error = do_revalidate(nd.dentry);
187 error = cp_old_stat(nd.dentry->d_inode, statbuf);
195 asmlinkage long sys_newlstat(char * filename, struct stat * statbuf)
200 error = user_path_walk_link(filename, &nd);
202 error = do_revalidate(nd.dentry);
204 error = cp_new_stat(nd.dentry->d_inode, statbuf);
210 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
213 * For backward compatibility? Maybe this should be moved
214 * into arch/i386 instead?
216 asmlinkage long sys_fstat(unsigned int fd, struct __old_kernel_stat * statbuf)
223 struct dentry * dentry = f->f_dentry;
225 err = do_revalidate(dentry);
227 err = cp_old_stat(dentry->d_inode, statbuf);
235 asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf)
242 struct dentry * dentry = f->f_dentry;
244 err = do_revalidate(dentry);
246 err = cp_new_stat(dentry->d_inode, statbuf);
252 asmlinkage long sys_readlink(const char * path, char * buf, int bufsiz)
260 error = user_path_walk_link(path, &nd);
262 struct inode * inode = nd.dentry->d_inode;
265 if (inode->i_op && inode->i_op->readlink &&
266 !(error = do_revalidate(nd.dentry))) {
268 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
276 /* ---------- LFS-64 ----------- */
277 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
279 static long cp_new_stat64(struct inode * inode, struct stat64 * statbuf)
282 unsigned int blocks, indirect;
284 memset(&tmp, 0, sizeof(tmp));
285 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
286 tmp.st_ino = inode->i_ino;
287 #ifdef STAT64_HAS_BROKEN_ST_INO
288 tmp.__st_ino = inode->i_ino;
290 tmp.st_mode = inode->i_mode;
291 tmp.st_nlink = inode->i_nlink;
292 tmp.st_uid = inode->i_uid;
293 tmp.st_gid = inode->i_gid;
294 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
295 tmp.st_atime = inode->i_atime;
296 tmp.st_mtime = inode->i_mtime;
297 tmp.st_ctime = inode->i_ctime;
298 tmp.st_size = inode->i_size;
300 * st_blocks and st_blksize are approximated with a simple algorithm if
301 * they aren't supported directly by the filesystem. The minix and msdos
302 * filesystems don't keep track of blocks, so they would either have to
303 * be counted explicitly (by delving into the file itself), or by using
304 * this simple algorithm to get a reasonable (although not 100% accurate)
309 * Use minix fs values for the number of direct and indirect blocks. The
310 * count is now exact for the minix fs except that it counts zero blocks.
311 * Everything is in units of BLOCK_SIZE until the assignment to
315 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
317 if (!inode->i_blksize) {
318 blocks = (tmp.st_size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
320 indirect = (blocks - D_B + I_B - 1) / I_B;
323 indirect = (indirect - 1 + I_B - 1) / I_B;
329 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
330 tmp.st_blksize = BLOCK_SIZE;
332 tmp.st_blocks = inode->i_blocks;
333 tmp.st_blksize = inode->i_blksize;
335 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
338 asmlinkage long sys_stat64(char * filename, struct stat64 * statbuf, long flags)
343 error = user_path_walk(filename, &nd);
345 error = do_revalidate(nd.dentry);
347 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
353 asmlinkage long sys_lstat64(char * filename, struct stat64 * statbuf, long flags)
358 error = user_path_walk_link(filename, &nd);
360 error = do_revalidate(nd.dentry);
362 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
368 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 * statbuf, long flags)
375 struct dentry * dentry = f->f_dentry;
377 err = do_revalidate(dentry);
379 err = cp_new_stat64(dentry->d_inode, statbuf);