blob: c4ecd52c5737b5aeb8c8bfd671d05b8a76f995a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/stat.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/errno.h>
10#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/highuid.h>
12#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
Theodore Ts'oba52de12006-09-27 01:50:49 -070016#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/uaccess.h>
19#include <asm/unistd.h>
20
21void generic_fillattr(struct inode *inode, struct kstat *stat)
22{
23 stat->dev = inode->i_sb->s_dev;
24 stat->ino = inode->i_ino;
25 stat->mode = inode->i_mode;
26 stat->nlink = inode->i_nlink;
27 stat->uid = inode->i_uid;
28 stat->gid = inode->i_gid;
29 stat->rdev = inode->i_rdev;
30 stat->atime = inode->i_atime;
31 stat->mtime = inode->i_mtime;
32 stat->ctime = inode->i_ctime;
33 stat->size = i_size_read(inode);
34 stat->blocks = inode->i_blocks;
Theodore Ts'oba52de12006-09-27 01:50:49 -070035 stat->blksize = (1 << inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38EXPORT_SYMBOL(generic_fillattr);
39
40int vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
41{
42 struct inode *inode = dentry->d_inode;
43 int retval;
44
45 retval = security_inode_getattr(mnt, dentry);
46 if (retval)
47 return retval;
48
49 if (inode->i_op->getattr)
50 return inode->i_op->getattr(mnt, dentry, stat);
51
52 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return 0;
54}
55
56EXPORT_SYMBOL(vfs_getattr);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058int vfs_fstat(unsigned int fd, struct kstat *stat)
59{
60 struct file *f = fget(fd);
61 int error = -EBADF;
62
63 if (f) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -080064 error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 fput(f);
66 }
67 return error;
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069EXPORT_SYMBOL(vfs_fstat);
70
Oleg Drokin0112fc22009-04-08 20:05:42 +040071int vfs_fstatat(int dfd, char __user *filename, struct kstat *stat, int flag)
72{
Christoph Hellwig2eae7a12009-04-08 16:34:03 -040073 struct path path;
Oleg Drokin0112fc22009-04-08 20:05:42 +040074 int error = -EINVAL;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -040075 int lookup_flags = 0;
Oleg Drokin0112fc22009-04-08 20:05:42 +040076
77 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
78 goto out;
79
Christoph Hellwig2eae7a12009-04-08 16:34:03 -040080 if (!(flag & AT_SYMLINK_NOFOLLOW))
81 lookup_flags |= LOOKUP_FOLLOW;
82
83 error = user_path_at(dfd, filename, lookup_flags, &path);
84 if (error)
85 goto out;
86
87 error = vfs_getattr(path.mnt, path.dentry, stat);
88 path_put(&path);
Oleg Drokin0112fc22009-04-08 20:05:42 +040089out:
90 return error;
91}
Oleg Drokin0112fc22009-04-08 20:05:42 +040092EXPORT_SYMBOL(vfs_fstatat);
93
Christoph Hellwig2eae7a12009-04-08 16:34:03 -040094int vfs_stat(char __user *name, struct kstat *stat)
95{
96 return vfs_fstatat(AT_FDCWD, name, stat, 0);
97}
98EXPORT_SYMBOL(vfs_stat);
99
100int vfs_lstat(char __user *name, struct kstat *stat)
101{
102 return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
103}
104EXPORT_SYMBOL(vfs_lstat);
105
Oleg Drokin0112fc22009-04-08 20:05:42 +0400106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#ifdef __ARCH_WANT_OLD_STAT
108
109/*
110 * For backward compatibility? Maybe this should be moved
111 * into arch/i386 instead?
112 */
113static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
114{
115 static int warncount = 5;
116 struct __old_kernel_stat tmp;
117
118 if (warncount > 0) {
119 warncount--;
120 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
121 current->comm);
122 } else if (warncount < 0) {
123 /* it's laughable, but... */
124 warncount = 0;
125 }
126
127 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
128 tmp.st_dev = old_encode_dev(stat->dev);
129 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700130 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
131 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 tmp.st_mode = stat->mode;
133 tmp.st_nlink = stat->nlink;
134 if (tmp.st_nlink != stat->nlink)
135 return -EOVERFLOW;
136 SET_UID(tmp.st_uid, stat->uid);
137 SET_GID(tmp.st_gid, stat->gid);
138 tmp.st_rdev = old_encode_dev(stat->rdev);
139#if BITS_PER_LONG == 32
140 if (stat->size > MAX_NON_LFS)
141 return -EOVERFLOW;
142#endif
143 tmp.st_size = stat->size;
144 tmp.st_atime = stat->atime.tv_sec;
145 tmp.st_mtime = stat->mtime.tv_sec;
146 tmp.st_ctime = stat->ctime.tv_sec;
147 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
148}
149
Heiko Carstensbdc480e2009-01-14 14:14:12 +0100150SYSCALL_DEFINE2(stat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400153 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400155 error = vfs_stat(filename, &stat);
156 if (error)
157 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400159 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Heiko Carstens257ac262009-01-14 14:14:13 +0100161
162SYSCALL_DEFINE2(lstat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400165 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400167 error = vfs_lstat(filename, &stat);
168 if (error)
169 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400171 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
Heiko Carstens257ac262009-01-14 14:14:13 +0100173
174SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct kstat stat;
177 int error = vfs_fstat(fd, &stat);
178
179 if (!error)
180 error = cp_old_stat(&stat, statbuf);
181
182 return error;
183}
184
185#endif /* __ARCH_WANT_OLD_STAT */
186
187static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
188{
189 struct stat tmp;
190
191#if BITS_PER_LONG == 32
192 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
193 return -EOVERFLOW;
194#else
195 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
196 return -EOVERFLOW;
197#endif
198
199 memset(&tmp, 0, sizeof(tmp));
200#if BITS_PER_LONG == 32
201 tmp.st_dev = old_encode_dev(stat->dev);
202#else
203 tmp.st_dev = new_encode_dev(stat->dev);
204#endif
205 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700206 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
207 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 tmp.st_mode = stat->mode;
209 tmp.st_nlink = stat->nlink;
210 if (tmp.st_nlink != stat->nlink)
211 return -EOVERFLOW;
212 SET_UID(tmp.st_uid, stat->uid);
213 SET_GID(tmp.st_gid, stat->gid);
214#if BITS_PER_LONG == 32
215 tmp.st_rdev = old_encode_dev(stat->rdev);
216#else
217 tmp.st_rdev = new_encode_dev(stat->rdev);
218#endif
219#if BITS_PER_LONG == 32
220 if (stat->size > MAX_NON_LFS)
221 return -EOVERFLOW;
222#endif
223 tmp.st_size = stat->size;
224 tmp.st_atime = stat->atime.tv_sec;
225 tmp.st_mtime = stat->mtime.tv_sec;
226 tmp.st_ctime = stat->ctime.tv_sec;
227#ifdef STAT_HAVE_NSEC
228 tmp.st_atime_nsec = stat->atime.tv_nsec;
229 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
230 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
231#endif
232 tmp.st_blocks = stat->blocks;
233 tmp.st_blksize = stat->blksize;
234 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
235}
236
Heiko Carstens257ac262009-01-14 14:14:13 +0100237SYSCALL_DEFINE2(newstat, char __user *, filename, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400240 int error = vfs_stat(filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400242 if (error)
243 return error;
244 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800246
Heiko Carstens257ac262009-01-14 14:14:13 +0100247SYSCALL_DEFINE2(newlstat, char __user *, filename, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400250 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400252 error = vfs_lstat(filename, &stat);
253 if (error)
254 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400256 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800258
Andreas Schwab2833c282006-04-27 15:46:42 +0200259#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
Heiko Carstens6559eed82009-01-14 14:14:32 +0100260SYSCALL_DEFINE4(newfstatat, int, dfd, char __user *, filename,
261 struct stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800262{
263 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400264 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800265
Oleg Drokin0112fc22009-04-08 20:05:42 +0400266 error = vfs_fstatat(dfd, filename, &stat, flag);
267 if (error)
268 return error;
269 return cp_new_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800270}
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800271#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800272
Heiko Carstens257ac262009-01-14 14:14:13 +0100273SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct kstat stat;
276 int error = vfs_fstat(fd, &stat);
277
278 if (!error)
279 error = cp_new_stat(&stat, statbuf);
280
281 return error;
282}
283
Heiko Carstens6559eed82009-01-14 14:14:32 +0100284SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
285 char __user *, buf, int, bufsiz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Al Viro2d8f3032008-07-22 09:59:21 -0400287 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int error;
289
290 if (bufsiz <= 0)
291 return -EINVAL;
292
Al Viro2d8f3032008-07-22 09:59:21 -0400293 error = user_path_at(dfd, pathname, 0, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400295 struct inode *inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 error = -EINVAL;
Al Viroacfa4382008-12-04 10:06:33 -0500298 if (inode->i_op->readlink) {
Al Viro2d8f3032008-07-22 09:59:21 -0400299 error = security_inode_readlink(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400301 touch_atime(path.mnt, path.dentry);
302 error = inode->i_op->readlink(path.dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800303 buf, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 }
Al Viro2d8f3032008-07-22 09:59:21 -0400306 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return error;
309}
310
Heiko Carstens002c8972009-01-14 14:14:18 +0100311SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
312 int, bufsiz)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800313{
314 return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/* ---------- LFS-64 ----------- */
319#ifdef __ARCH_WANT_STAT64
320
321static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
322{
323 struct stat64 tmp;
324
325 memset(&tmp, 0, sizeof(struct stat64));
326#ifdef CONFIG_MIPS
327 /* mips has weird padding, so we don't get 64 bits there */
328 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
329 return -EOVERFLOW;
330 tmp.st_dev = new_encode_dev(stat->dev);
331 tmp.st_rdev = new_encode_dev(stat->rdev);
332#else
333 tmp.st_dev = huge_encode_dev(stat->dev);
334 tmp.st_rdev = huge_encode_dev(stat->rdev);
335#endif
336 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700337 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
338 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#ifdef STAT64_HAS_BROKEN_ST_INO
340 tmp.__st_ino = stat->ino;
341#endif
342 tmp.st_mode = stat->mode;
343 tmp.st_nlink = stat->nlink;
344 tmp.st_uid = stat->uid;
345 tmp.st_gid = stat->gid;
346 tmp.st_atime = stat->atime.tv_sec;
347 tmp.st_atime_nsec = stat->atime.tv_nsec;
348 tmp.st_mtime = stat->mtime.tv_sec;
349 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
350 tmp.st_ctime = stat->ctime.tv_sec;
351 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
352 tmp.st_size = stat->size;
353 tmp.st_blocks = stat->blocks;
354 tmp.st_blksize = stat->blksize;
355 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
356}
357
Heiko Carstens257ac262009-01-14 14:14:13 +0100358SYSCALL_DEFINE2(stat64, char __user *, filename, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct kstat stat;
361 int error = vfs_stat(filename, &stat);
362
363 if (!error)
364 error = cp_new_stat64(&stat, statbuf);
365
366 return error;
367}
Heiko Carstens257ac262009-01-14 14:14:13 +0100368
369SYSCALL_DEFINE2(lstat64, char __user *, filename, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct kstat stat;
372 int error = vfs_lstat(filename, &stat);
373
374 if (!error)
375 error = cp_new_stat64(&stat, statbuf);
376
377 return error;
378}
Heiko Carstens257ac262009-01-14 14:14:13 +0100379
380SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct kstat stat;
383 int error = vfs_fstat(fd, &stat);
384
385 if (!error)
386 error = cp_new_stat64(&stat, statbuf);
387
388 return error;
389}
390
Heiko Carstens6559eed82009-01-14 14:14:32 +0100391SYSCALL_DEFINE4(fstatat64, int, dfd, char __user *, filename,
392 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800393{
394 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400395 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800396
Oleg Drokin0112fc22009-04-08 20:05:42 +0400397 error = vfs_fstatat(dfd, filename, &stat, flag);
398 if (error)
399 return error;
400 return cp_new_stat64(&stat, statbuf);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#endif /* __ARCH_WANT_STAT64 */
403
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300404/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
405void __inode_add_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 inode->i_blocks += bytes >> 9;
408 bytes &= 511;
409 inode->i_bytes += bytes;
410 if (inode->i_bytes >= 512) {
411 inode->i_blocks++;
412 inode->i_bytes -= 512;
413 }
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300414}
415
416void inode_add_bytes(struct inode *inode, loff_t bytes)
417{
418 spin_lock(&inode->i_lock);
419 __inode_add_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 spin_unlock(&inode->i_lock);
421}
422
423EXPORT_SYMBOL(inode_add_bytes);
424
425void inode_sub_bytes(struct inode *inode, loff_t bytes)
426{
427 spin_lock(&inode->i_lock);
428 inode->i_blocks -= bytes >> 9;
429 bytes &= 511;
430 if (inode->i_bytes < bytes) {
431 inode->i_blocks--;
432 inode->i_bytes += 512;
433 }
434 inode->i_bytes -= bytes;
435 spin_unlock(&inode->i_lock);
436}
437
438EXPORT_SYMBOL(inode_sub_bytes);
439
440loff_t inode_get_bytes(struct inode *inode)
441{
442 loff_t ret;
443
444 spin_lock(&inode->i_lock);
445 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
446 spin_unlock(&inode->i_lock);
447 return ret;
448}
449
450EXPORT_SYMBOL(inode_get_bytes);
451
452void inode_set_bytes(struct inode *inode, loff_t bytes)
453{
454 /* Caller is here responsible for sufficient locking
455 * (ie. inode->i_lock) */
456 inode->i_blocks = bytes >> 9;
457 inode->i_bytes = bytes & 511;
458}
459
460EXPORT_SYMBOL(inode_set_bytes);