Import 2.3.99pre7-7
[davej-history.git] / fs / stat.c
blob367c3cb4b659926d4afd84480d20d01c42741357
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) {
40 warncount--;
41 printk("VFS: Warning: %s using old stat() call. Recompile your binary.\n",
42 current->comm);
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;
59 #endif
61 static int cp_new_stat(struct inode * inode, struct stat * statbuf)
63 struct stat tmp;
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)
84 * value.
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
91 * tmp.st_blksize.
93 #define D_B 7
94 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
96 if (!inode->i_blksize) {
97 blocks = (tmp.st_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
98 if (blocks > D_B) {
99 indirect = (blocks - D_B + I_B - 1) / I_B;
100 blocks += indirect;
101 if (indirect > 1) {
102 indirect = (indirect - 1 + I_B - 1) / I_B;
103 blocks += indirect;
104 if (indirect > 1)
105 blocks++;
108 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
109 tmp.st_blksize = BLOCK_SIZE;
110 } else {
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)
125 struct nameidata nd;
126 int error;
128 lock_kernel();
129 error = user_path_walk(filename, &nd);
130 if (!error) {
131 error = do_revalidate(nd.dentry);
132 if (!error)
133 error = cp_old_stat(nd.dentry->d_inode, statbuf);
134 path_release(&nd);
136 unlock_kernel();
137 return error;
139 #endif
141 asmlinkage long sys_newstat(char * filename, struct stat * statbuf)
143 struct nameidata nd;
144 int error;
146 lock_kernel();
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 unlock_kernel();
155 return error;
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)
166 struct nameidata nd;
167 int error;
169 lock_kernel();
170 error = user_path_walk_link(filename, &nd);
171 if (!error) {
172 error = do_revalidate(nd.dentry);
173 if (!error)
174 error = cp_old_stat(nd.dentry->d_inode, statbuf);
175 path_release(&nd);
177 unlock_kernel();
178 return error;
181 #endif
183 asmlinkage long sys_newlstat(char * filename, struct stat * statbuf)
185 struct nameidata nd;
186 int error;
188 lock_kernel();
189 error = user_path_walk_link(filename, &nd);
190 if (!error) {
191 error = do_revalidate(nd.dentry);
192 if (!error)
193 error = cp_new_stat(nd.dentry->d_inode, statbuf);
194 path_release(&nd);
196 unlock_kernel();
197 return error;
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)
208 struct file * f;
209 int err = -EBADF;
211 lock_kernel();
212 f = fget(fd);
213 if (f) {
214 struct dentry * dentry = f->f_dentry;
216 err = do_revalidate(dentry);
217 if (!err)
218 err = cp_old_stat(dentry->d_inode, statbuf);
219 fput(f);
221 unlock_kernel();
222 return err;
225 #endif
227 asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf)
229 struct file * f;
230 int err = -EBADF;
232 lock_kernel();
233 f = fget(fd);
234 if (f) {
235 struct dentry * dentry = f->f_dentry;
237 err = do_revalidate(dentry);
238 if (!err)
239 err = cp_new_stat(dentry->d_inode, statbuf);
240 fput(f);
242 unlock_kernel();
243 return err;
246 asmlinkage long sys_readlink(const char * path, char * buf, int bufsiz)
248 struct nameidata nd;
249 int error;
251 if (bufsiz <= 0)
252 return -EINVAL;
254 lock_kernel();
255 error = user_path_walk_link(path, &nd);
256 if (!error) {
257 struct inode * inode = nd.dentry->d_inode;
259 error = -EINVAL;
260 if (inode->i_op && inode->i_op->readlink &&
261 !(error = do_revalidate(nd.dentry))) {
262 UPDATE_ATIME(inode);
263 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
265 path_release(&nd);
267 unlock_kernel();
268 return error;
272 /* ---------- LFS-64 ----------- */
273 #if !defined(__alpha__) && !defined(__ia64__)
275 static long cp_new_stat64(struct inode * inode, struct stat64 * statbuf)
277 struct stat64 tmp;
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)
298 * value.
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
305 * tmp.st_blksize.
307 #define D_B 7
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;
312 if (blocks > D_B) {
313 indirect = (blocks - D_B + I_B - 1) / I_B;
314 blocks += indirect;
315 if (indirect > 1) {
316 indirect = (indirect - 1 + I_B - 1) / I_B;
317 blocks += indirect;
318 if (indirect > 1)
319 blocks++;
322 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
323 tmp.st_blksize = BLOCK_SIZE;
324 } else {
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)
333 struct nameidata nd;
334 int error;
336 lock_kernel();
337 error = user_path_walk(filename, &nd);
338 if (!error) {
339 error = do_revalidate(nd.dentry);
340 if (!error)
341 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
342 path_release(&nd);
344 unlock_kernel();
345 return error;
348 asmlinkage long sys_lstat64(char * filename, struct stat64 * statbuf, long flags)
350 struct nameidata nd;
351 int error;
353 lock_kernel();
354 error = user_path_walk_link(filename, &nd);
355 if (!error) {
356 error = do_revalidate(nd.dentry);
357 if (!error)
358 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
359 path_release(&nd);
361 unlock_kernel();
362 return error;
365 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 * statbuf, long flags)
367 struct file * f;
368 int err = -EBADF;
370 lock_kernel();
371 f = fget(fd);
372 if (f) {
373 struct dentry * dentry = f->f_dentry;
375 err = do_revalidate(dentry);
376 if (!err)
377 err = cp_new_stat64(dentry->d_inode, statbuf);
378 fput(f);
380 unlock_kernel();
381 return err;
384 #endif /* LFS-64 */