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__)
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;
42 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
44 } else if (warncount < 0) {
45 /* it's laughable, but... */
49 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
50 tmp.st_ino = inode->i_ino;
51 tmp.st_mode = inode->i_mode;
52 tmp.st_nlink = inode->i_nlink;
53 SET_OLDSTAT_UID(tmp, inode->i_uid);
54 SET_OLDSTAT_GID(tmp, inode->i_gid);
55 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
56 #if BITS_PER_LONG == 32
57 if (inode->i_size > MAX_NON_LFS)
60 tmp.st_size = inode->i_size;
61 tmp.st_atime = inode->i_atime;
62 tmp.st_mtime = inode->i_mtime;
63 tmp.st_ctime = inode->i_ctime;
64 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
69 static int cp_new_stat(struct inode * inode, struct stat * statbuf)
72 unsigned int blocks, indirect;
74 memset(&tmp, 0, sizeof(tmp));
75 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
76 tmp.st_ino = inode->i_ino;
77 tmp.st_mode = inode->i_mode;
78 tmp.st_nlink = inode->i_nlink;
79 SET_STAT_UID(tmp, inode->i_uid);
80 SET_STAT_GID(tmp, inode->i_gid);
81 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
82 #if BITS_PER_LONG == 32
83 if (inode->i_size > MAX_NON_LFS)
86 tmp.st_size = inode->i_size;
87 tmp.st_atime = inode->i_atime;
88 tmp.st_mtime = inode->i_mtime;
89 tmp.st_ctime = inode->i_ctime;
91 * st_blocks and st_blksize are approximated with a simple algorithm if
92 * they aren't supported directly by the filesystem. The minix and msdos
93 * filesystems don't keep track of blocks, so they would either have to
94 * be counted explicitly (by delving into the file itself), or by using
95 * this simple algorithm to get a reasonable (although not 100% accurate)
100 * Use minix fs values for the number of direct and indirect blocks. The
101 * count is now exact for the minix fs except that it counts zero blocks.
102 * Everything is in units of BLOCK_SIZE until the assignment to
106 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
108 if (!inode->i_blksize) {
109 blocks = (tmp.st_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
111 indirect = (blocks - D_B + I_B - 1) / I_B;
114 indirect = (indirect - 1 + I_B - 1) / I_B;
120 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
121 tmp.st_blksize = BLOCK_SIZE;
123 tmp.st_blocks = inode->i_blocks;
124 tmp.st_blksize = inode->i_blksize;
126 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
130 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__)
132 * For backward compatibility? Maybe this should be moved
133 * into arch/i386 instead?
135 asmlinkage long sys_stat(char * filename, struct __old_kernel_stat * statbuf)
140 error = user_path_walk(filename, &nd);
142 error = do_revalidate(nd.dentry);
144 error = cp_old_stat(nd.dentry->d_inode, statbuf);
151 asmlinkage long sys_newstat(char * filename, struct stat * statbuf)
156 error = user_path_walk(filename, &nd);
158 error = do_revalidate(nd.dentry);
160 error = cp_new_stat(nd.dentry->d_inode, statbuf);
166 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__)
169 * For backward compatibility? Maybe this should be moved
170 * into arch/i386 instead?
172 asmlinkage long sys_lstat(char * filename, struct __old_kernel_stat * statbuf)
177 error = user_path_walk_link(filename, &nd);
179 error = do_revalidate(nd.dentry);
181 error = cp_old_stat(nd.dentry->d_inode, statbuf);
189 asmlinkage long sys_newlstat(char * filename, struct stat * statbuf)
194 error = user_path_walk_link(filename, &nd);
196 error = do_revalidate(nd.dentry);
198 error = cp_new_stat(nd.dentry->d_inode, statbuf);
204 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__)
207 * For backward compatibility? Maybe this should be moved
208 * into arch/i386 instead?
210 asmlinkage long sys_fstat(unsigned int fd, struct __old_kernel_stat * statbuf)
217 struct dentry * dentry = f->f_dentry;
219 err = do_revalidate(dentry);
221 err = cp_old_stat(dentry->d_inode, statbuf);
229 asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf)
236 struct dentry * dentry = f->f_dentry;
238 err = do_revalidate(dentry);
240 err = cp_new_stat(dentry->d_inode, statbuf);
246 asmlinkage long sys_readlink(const char * path, char * buf, int bufsiz)
254 error = user_path_walk_link(path, &nd);
256 struct inode * inode = nd.dentry->d_inode;
259 if (inode->i_op && inode->i_op->readlink &&
260 !(error = do_revalidate(nd.dentry))) {
262 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
270 /* ---------- LFS-64 ----------- */
271 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
273 static long cp_new_stat64(struct inode * inode, struct stat64 * statbuf)
276 unsigned int blocks, indirect;
278 memset(&tmp, 0, sizeof(tmp));
279 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
280 tmp.st_ino = inode->i_ino;
281 #ifdef STAT64_HAS_BROKEN_ST_INO
282 tmp.__st_ino = inode->i_ino;
284 tmp.st_mode = inode->i_mode;
285 tmp.st_nlink = inode->i_nlink;
286 tmp.st_uid = inode->i_uid;
287 tmp.st_gid = inode->i_gid;
288 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
289 tmp.st_atime = inode->i_atime;
290 tmp.st_mtime = inode->i_mtime;
291 tmp.st_ctime = inode->i_ctime;
292 tmp.st_size = inode->i_size;
294 * st_blocks and st_blksize are approximated with a simple algorithm if
295 * they aren't supported directly by the filesystem. The minix and msdos
296 * filesystems don't keep track of blocks, so they would either have to
297 * be counted explicitly (by delving into the file itself), or by using
298 * this simple algorithm to get a reasonable (although not 100% accurate)
303 * Use minix fs values for the number of direct and indirect blocks. The
304 * count is now exact for the minix fs except that it counts zero blocks.
305 * Everything is in units of BLOCK_SIZE until the assignment to
309 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
311 if (!inode->i_blksize) {
312 blocks = (tmp.st_size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
314 indirect = (blocks - D_B + I_B - 1) / I_B;
317 indirect = (indirect - 1 + I_B - 1) / I_B;
323 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
324 tmp.st_blksize = BLOCK_SIZE;
326 tmp.st_blocks = inode->i_blocks;
327 tmp.st_blksize = inode->i_blksize;
329 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
332 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);
347 asmlinkage long sys_lstat64(char * filename, struct stat64 * statbuf, long flags)
352 error = user_path_walk_link(filename, &nd);
354 error = do_revalidate(nd.dentry);
356 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
362 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 * statbuf, long flags)
369 struct dentry * dentry = f->f_dentry;
371 err = do_revalidate(dentry);
373 err = cp_new_stat64(dentry->d_inode, statbuf);