1 /*
2 * linux/fs/stat.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 #include <linux/config.h>
8 #include <linux/mm.h>
9 #include <linux/errno.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/highuid.h>
13
14 #include <asm/uaccess.h>
15
16 /*
17 * Revalidate the inode. This is required for proper NFS attribute caching.
18 */
19 static __inline__ int
do_revalidate(struct dentry * dentry)20 do_revalidate(struct dentry *dentry)
21 {
22 struct inode * inode = dentry->d_inode;
23 if (inode->i_op && inode->i_op->revalidate)
24 return inode->i_op->revalidate(dentry);
25 return 0;
26 }
27
28
29 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
30
31 /*
32 * For backward compatibility? Maybe this should be moved
33 * into arch/i386 instead?
34 */
cp_old_stat(struct inode * inode,struct __old_kernel_stat * statbuf)35 static int cp_old_stat(struct inode * inode, struct __old_kernel_stat * statbuf)
36 {
37 static int warncount = 5;
38 struct __old_kernel_stat tmp;
39
40 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
41
42 if (warncount > 0) {
43 warncount--;
44 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
45 current->comm);
46 } else if (warncount < 0) {
47 /* it's laughable, but... */
48 warncount = 0;
49 }
50
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)
56 return -EOVERFLOW;
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)
62 return -EOVERFLOW;
63 #endif
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;
69 }
70
71 #endif
72
cp_new_stat(struct inode * inode,struct stat * statbuf)73 static int cp_new_stat(struct inode * inode, struct stat * statbuf)
74 {
75 struct stat tmp;
76 unsigned int blocks, indirect;
77
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)
84 return -EOVERFLOW;
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)
90 return -EOVERFLOW;
91 #endif
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;
96 /*
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)
102 * value.
103 */
104
105 /*
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
109 * tmp.st_blksize.
110 */
111 #define D_B 7
112 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
113
114 if (!inode->i_blksize) {
115 blocks = (tmp.st_size + BLOCK_SIZE - 1) / BLOCK_SIZE;
116 if (blocks > D_B) {
117 indirect = (blocks - D_B + I_B - 1) / I_B;
118 blocks += indirect;
119 if (indirect > 1) {
120 indirect = (indirect - 1 + I_B - 1) / I_B;
121 blocks += indirect;
122 if (indirect > 1)
123 blocks++;
124 }
125 }
126 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
127 tmp.st_blksize = BLOCK_SIZE;
128 } else {
129 tmp.st_blocks = inode->i_blocks;
130 tmp.st_blksize = inode->i_blksize;
131 }
132 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
133 }
134
135
136 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
137 /*
138 * For backward compatibility? Maybe this should be moved
139 * into arch/i386 instead?
140 */
sys_stat(char * filename,struct __old_kernel_stat * statbuf)141 asmlinkage long sys_stat(char * filename, struct __old_kernel_stat * statbuf)
142 {
143 struct nameidata nd;
144 int error;
145
146 error = user_path_walk(filename, &nd);
147 if (!error) {
148 error = do_revalidate(nd.dentry);
149 if (!error)
150 error = cp_old_stat(nd.dentry->d_inode, statbuf);
151 path_release(&nd);
152 }
153 return error;
154 }
155 #endif
156
sys_newstat(char * filename,struct stat * statbuf)157 asmlinkage long sys_newstat(char * filename, struct stat * statbuf)
158 {
159 struct nameidata nd;
160 int error;
161
162 error = user_path_walk(filename, &nd);
163 if (!error) {
164 error = do_revalidate(nd.dentry);
165 if (!error)
166 error = cp_new_stat(nd.dentry->d_inode, statbuf);
167 path_release(&nd);
168 }
169 return error;
170 }
171
172 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
173
174 /*
175 * For backward compatibility? Maybe this should be moved
176 * into arch/i386 instead?
177 */
sys_lstat(char * filename,struct __old_kernel_stat * statbuf)178 asmlinkage long sys_lstat(char * filename, struct __old_kernel_stat * statbuf)
179 {
180 struct nameidata nd;
181 int error;
182
183 error = user_path_walk_link(filename, &nd);
184 if (!error) {
185 error = do_revalidate(nd.dentry);
186 if (!error)
187 error = cp_old_stat(nd.dentry->d_inode, statbuf);
188 path_release(&nd);
189 }
190 return error;
191 }
192
193 #endif
194
sys_newlstat(char * filename,struct stat * statbuf)195 asmlinkage long sys_newlstat(char * filename, struct stat * statbuf)
196 {
197 struct nameidata nd;
198 int error;
199
200 error = user_path_walk_link(filename, &nd);
201 if (!error) {
202 error = do_revalidate(nd.dentry);
203 if (!error)
204 error = cp_new_stat(nd.dentry->d_inode, statbuf);
205 path_release(&nd);
206 }
207 return error;
208 }
209
210 #if !defined(__alpha__) && !defined(__sparc__) && !defined(__ia64__) && !defined(CONFIG_ARCH_S390) && !defined(__hppa__) && !defined(__x86_64__) && !defined(__mips__)
211
212 /*
213 * For backward compatibility? Maybe this should be moved
214 * into arch/i386 instead?
215 */
sys_fstat(unsigned int fd,struct __old_kernel_stat * statbuf)216 asmlinkage long sys_fstat(unsigned int fd, struct __old_kernel_stat * statbuf)
217 {
218 struct file * f;
219 int err = -EBADF;
220
221 f = fget(fd);
222 if (f) {
223 struct dentry * dentry = f->f_dentry;
224
225 err = do_revalidate(dentry);
226 if (!err)
227 err = cp_old_stat(dentry->d_inode, statbuf);
228 fput(f);
229 }
230 return err;
231 }
232
233 #endif
234
sys_newfstat(unsigned int fd,struct stat * statbuf)235 asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf)
236 {
237 struct file * f;
238 int err = -EBADF;
239
240 f = fget(fd);
241 if (f) {
242 struct dentry * dentry = f->f_dentry;
243
244 err = do_revalidate(dentry);
245 if (!err)
246 err = cp_new_stat(dentry->d_inode, statbuf);
247 fput(f);
248 }
249 return err;
250 }
251
sys_readlink(const char * path,char * buf,int bufsiz)252 asmlinkage long sys_readlink(const char * path, char * buf, int bufsiz)
253 {
254 struct nameidata nd;
255 int error;
256
257 if (bufsiz <= 0)
258 return -EINVAL;
259
260 error = user_path_walk_link(path, &nd);
261 if (!error) {
262 struct inode * inode = nd.dentry->d_inode;
263
264 error = -EINVAL;
265 if (inode->i_op && inode->i_op->readlink &&
266 !(error = do_revalidate(nd.dentry))) {
267 UPDATE_ATIME(inode);
268 error = inode->i_op->readlink(nd.dentry, buf, bufsiz);
269 }
270 path_release(&nd);
271 }
272 return error;
273 }
274
275
276 /* ---------- LFS-64 ----------- */
277 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips64) && !defined(__x86_64__) && !defined(CONFIG_ARCH_S390X)
278
cp_new_stat64(struct inode * inode,struct stat64 * statbuf)279 static long cp_new_stat64(struct inode * inode, struct stat64 * statbuf)
280 {
281 struct stat64 tmp;
282 unsigned int blocks, indirect;
283
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;
289 #endif
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;
299 /*
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)
305 * value.
306 */
307
308 /*
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
312 * tmp.st_blksize.
313 */
314 #define D_B 7
315 #define I_B (BLOCK_SIZE / sizeof(unsigned short))
316
317 if (!inode->i_blksize) {
318 blocks = (tmp.st_size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
319 if (blocks > D_B) {
320 indirect = (blocks - D_B + I_B - 1) / I_B;
321 blocks += indirect;
322 if (indirect > 1) {
323 indirect = (indirect - 1 + I_B - 1) / I_B;
324 blocks += indirect;
325 if (indirect > 1)
326 blocks++;
327 }
328 }
329 tmp.st_blocks = (BLOCK_SIZE / 512) * blocks;
330 tmp.st_blksize = BLOCK_SIZE;
331 } else {
332 tmp.st_blocks = inode->i_blocks;
333 tmp.st_blksize = inode->i_blksize;
334 }
335 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
336 }
337
sys_stat64(char * filename,struct stat64 * statbuf,long flags)338 asmlinkage long sys_stat64(char * filename, struct stat64 * statbuf, long flags)
339 {
340 struct nameidata nd;
341 int error;
342
343 error = user_path_walk(filename, &nd);
344 if (!error) {
345 error = do_revalidate(nd.dentry);
346 if (!error)
347 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
348 path_release(&nd);
349 }
350 return error;
351 }
352
sys_lstat64(char * filename,struct stat64 * statbuf,long flags)353 asmlinkage long sys_lstat64(char * filename, struct stat64 * statbuf, long flags)
354 {
355 struct nameidata nd;
356 int error;
357
358 error = user_path_walk_link(filename, &nd);
359 if (!error) {
360 error = do_revalidate(nd.dentry);
361 if (!error)
362 error = cp_new_stat64(nd.dentry->d_inode, statbuf);
363 path_release(&nd);
364 }
365 return error;
366 }
367
sys_fstat64(unsigned long fd,struct stat64 * statbuf,long flags)368 asmlinkage long sys_fstat64(unsigned long fd, struct stat64 * statbuf, long flags)
369 {
370 struct file * f;
371 int err = -EBADF;
372
373 f = fget(fd);
374 if (f) {
375 struct dentry * dentry = f->f_dentry;
376
377 err = do_revalidate(dentry);
378 if (!err)
379 err = cp_new_stat64(dentry->d_inode, statbuf);
380 fput(f);
381 }
382 return err;
383 }
384
385 #endif /* LFS-64 */
386