Merge with Linux 2.4.0-test5-pre6.
[linux-2.6/linux-mips.git] / fs / stat.c
blob3ca996ed15822bd3a5af3884393dff8b62e8d2c6
1 /*
2 * linux/fs/stat.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/mm.h>
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.
18 static __inline__ int
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);
24 return 0;
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;
39 if (warncount > 0) {
40 warncount--;
41 printk("VFS: Warning: %s using old stat() call. Recompile your binary.\n",
42 current->comm);
43 } else if (warncount < 0) {
44 /* it's laughable, but... */
45 warncount = 0;
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 tmp.st_size = inode->i_size;
56 tmp.st_atime = inode->i_atime;
57 tmp.st_mtime = inode->i_mtime;
58 tmp.st_ctime = inode->i_ctime;
59 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
62 #endif
64 static int cp_new_stat(struct inode * inode, struct stat * statbuf)
66 struct stat tmp;
67 unsigned int blocks, indirect;
69 memset(&tmp, 0, sizeof(tmp));
70 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
71 tmp.st_ino = inode->i_ino;
72 tmp.st_mode = inode->i_mode;
73 tmp.st_nlink = inode->i_nlink;
74 SET_STAT_UID(tmp, inode->i_uid);
75 SET_STAT_GID(tmp, inode->i_gid);
76 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
77 tmp.st_size = inode->i_size;
78 tmp.st_atime = inode->i_atime;
79 tmp.st_mtime = inode->i_mtime;
80 tmp.st_ctime = inode->i_ctime;
82 * st_blocks and st_blksize are approximated with a simple algorithm if
83 * they aren't supported directly by the filesystem. The minix and msdos
84 * filesystems don't keep track of blocks, so they would either have to
85 * be counted explicitly (by delving into the file itself), or by using
86 * this simple algorithm to get a reasonable (although not 100% accurate)
87 * value.
91 * Use minix fs values for the number of direct and indirect blocks. The
92 * count is now exact for the minix fs except that it counts zero blocks.
93 * Everything is in units of BLOCK_SIZE until the assignment to
94 * tmp.st_blksize.
96 #define D_B 7
97 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
99 if (!inode->i_blksize) {
100 blocks = (tmp.st_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
101 if (blocks > D_B) {
102 indirect = (blocks - D_B + I_B - 1) / I_B;
103 blocks += indirect;
104 if (indirect > 1) {
105 indirect = (indirect - 1 + I_B - 1) / I_B;
106 blocks += indirect;
107 if (indirect > 1)
108 blocks++;
111 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
112 tmp.st_blksize = BLOCK_SIZE;
113 } else {
114 tmp.st_blocks = inode->i_blocks;
115 tmp.st_blksize = inode->i_blksize;
117 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
121 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
123 * For backward compatibility? Maybe this should be moved
124 * into arch/i386 instead?
126 asmlinkage long sys_stat(char * filename, struct __old_kernel_stat * statbuf)
128 struct nameidata nd;
129 int error;
131 error = user_path_walk(filename, &nd);
132 if (!error) {
133 error = do_revalidate(nd.dentry);
134 if (!error)
135 error = cp_old_stat(nd.dentry->d_inode, statbuf);
136 path_release(&nd);
138 return error;
140 #endif
142 asmlinkage long sys_newstat(char * filename, struct stat * statbuf)
144 struct nameidata nd;
145 int error;
147 error = user_path_walk(filename, &nd);
148 if (!error) {
149 error = do_revalidate(nd.dentry);
150 if (!error)
151 error = cp_new_stat(nd.dentry->d_inode, statbuf);
152 path_release(&nd);
154 return error;
157 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
160 * For backward compatibility? Maybe this should be moved
161 * into arch/i386 instead?
163 asmlinkage long sys_lstat(char * filename, struct __old_kernel_stat * statbuf)
165 struct nameidata nd;
166 int error;
168 error = user_path_walk_link(filename, &nd);
169 if (!error) {
170 error = do_revalidate(nd.dentry);
171 if (!error)
172 error = cp_old_stat(nd.dentry->d_inode, statbuf);
173 path_release(&nd);
175 return error;
178 #endif
180 asmlinkage long sys_newlstat(char * filename, struct stat * statbuf)
182 struct nameidata nd;
183 int error;
185 error = user_path_walk_link(filename, &nd);
186 if (!error) {
187 error = do_revalidate(nd.dentry);
188 if (!error)
189 error = cp_new_stat(nd.dentry->d_inode, statbuf);
190 path_release(&nd);
192 return error;
195 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(__s390__)
198 * For backward compatibility? Maybe this should be moved
199 * into arch/i386 instead?
201 asmlinkage long sys_fstat(unsigned int fd, struct __old_kernel_stat * statbuf)
203 struct file * f;
204 int err = -EBADF;
206 f = fget(fd);
207 if (f) {
208 struct dentry * dentry = f->f_dentry;
210 err = do_revalidate(dentry);
211 if (!err)
212 err = cp_old_stat(dentry->d_inode, statbuf);
213 fput(f);
215 return err;
218 #endif
220 asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf)
222 struct file * f;
223 int err = -EBADF;
225 f = fget(fd);
226 if (f) {
227 struct dentry * dentry = f->f_dentry;
229 err = do_revalidate(dentry);
230 if (!err)
231 err = cp_new_stat(dentry->d_inode, statbuf);
232 fput(f);
234 return err;
237 asmlinkage long sys_readlink(const char * path, char * buf, int bufsiz)
239 struct nameidata nd;
240 int error;
242 if (bufsiz <= 0)
243 return -EINVAL;
245 error = user_path_walk_link(path, &nd);
246 if (!error) {
247 struct inode * inode = nd.dentry->d_inode;
249 error = -EINVAL;
250 if (inode->i_op && inode->i_op->readlink &&
251 !(error = do_revalidate(nd.dentry))) {
252 UPDATE_ATIME(inode);
253 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
255 path_release(&nd);
257 return error;
261 /* ---------- LFS-64 ----------- */
262 #if !defined(__alpha__) && !defined (__ia64__) && !defined(__mips64)
264 static long cp_new_stat64(struct inode * inode, struct stat64 * statbuf)
266 struct stat64 tmp;
267 unsigned int blocks, indirect;
269 memset(&tmp, 0, sizeof(tmp));
270 tmp.st_dev = kdev_t_to_nr(inode->i_dev);
271 tmp.st_ino = inode->i_ino;
272 tmp.st_mode = inode->i_mode;
273 tmp.st_nlink = inode->i_nlink;
274 tmp.st_uid = inode->i_uid;
275 tmp.st_gid = inode->i_gid;
276 tmp.st_rdev = kdev_t_to_nr(inode->i_rdev);
277 tmp.st_atime = inode->i_atime;
278 tmp.st_mtime = inode->i_mtime;
279 tmp.st_ctime = inode->i_ctime;
280 tmp.st_size = inode->i_size;
282 * st_blocks and st_blksize are approximated with a simple algorithm if
283 * they aren't supported directly by the filesystem. The minix and msdos
284 * filesystems don't keep track of blocks, so they would either have to
285 * be counted explicitly (by delving into the file itself), or by using
286 * this simple algorithm to get a reasonable (although not 100% accurate)
287 * value.
291 * Use minix fs values for the number of direct and indirect blocks. The
292 * count is now exact for the minix fs except that it counts zero blocks.
293 * Everything is in units of BLOCK_SIZE until the assignment to
294 * tmp.st_blksize.
296 #define D_B 7
297 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
299 if (!inode->i_blksize) {
300 blocks = (tmp.st_size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
301 if (blocks > D_B) {
302 indirect = (blocks - D_B + I_B - 1) / I_B;
303 blocks += indirect;
304 if (indirect > 1) {
305 indirect = (indirect - 1 + I_B - 1) / I_B;
306 blocks += indirect;
307 if (indirect > 1)
308 blocks++;
311 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
312 tmp.st_blksize = BLOCK_SIZE;
313 } else {
314 tmp.st_blocks = inode->i_blocks;
315 tmp.st_blksize = inode->i_blksize;
317 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
320 asmlinkage long sys_stat64(char * filename, struct stat64 * statbuf, long flags)
322 struct nameidata nd;
323 int error;
325 error = user_path_walk(filename, &nd);
326 if (!error) {
327 error = do_revalidate(nd.dentry);
328 if (!error)
329 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
330 path_release(&nd);
332 return error;
335 asmlinkage long sys_lstat64(char * filename, struct stat64 * statbuf, long flags)
337 struct nameidata nd;
338 int error;
340 error = user_path_walk_link(filename, &nd);
341 if (!error) {
342 error = do_revalidate(nd.dentry);
343 if (!error)
344 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
345 path_release(&nd);
347 return error;
350 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 * statbuf, long flags)
352 struct file * f;
353 int err = -EBADF;
355 f = fget(fd);
356 if (f) {
357 struct dentry * dentry = f->f_dentry;
359 err = do_revalidate(dentry);
360 if (!err)
361 err = cp_new_stat64(dentry->d_inode, statbuf);
362 fput(f);
364 return err;
367 #endif /* LFS-64 */