blob: 54711662b8558460566a036f39e6ceb8f35faf7c [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
Ulrich Drepper5590ff02006-01-18 17:43:53 -080058int vfs_stat_fd(int dfd, char __user *name, struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Al Viro2d8f3032008-07-22 09:59:21 -040060 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int error;
62
Al Viro2d8f3032008-07-22 09:59:21 -040063 error = user_path_at(dfd, name, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -040065 error = vfs_getattr(path.mnt, path.dentry, stat);
66 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68 return error;
69}
70
Ulrich Drepper5590ff02006-01-18 17:43:53 -080071int vfs_stat(char __user *name, struct kstat *stat)
72{
73 return vfs_stat_fd(AT_FDCWD, name, stat);
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076EXPORT_SYMBOL(vfs_stat);
77
Ulrich Drepper5590ff02006-01-18 17:43:53 -080078int vfs_lstat_fd(int dfd, char __user *name, struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viro2d8f3032008-07-22 09:59:21 -040080 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int error;
82
Al Viro2d8f3032008-07-22 09:59:21 -040083 error = user_path_at(dfd, name, 0, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -040085 error = vfs_getattr(path.mnt, path.dentry, stat);
86 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 return error;
89}
90
Ulrich Drepper5590ff02006-01-18 17:43:53 -080091int vfs_lstat(char __user *name, struct kstat *stat)
92{
93 return vfs_lstat_fd(AT_FDCWD, name, stat);
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096EXPORT_SYMBOL(vfs_lstat);
97
98int vfs_fstat(unsigned int fd, struct kstat *stat)
99{
100 struct file *f = fget(fd);
101 int error = -EBADF;
102
103 if (f) {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800104 error = vfs_getattr(f->f_path.mnt, f->f_path.dentry, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 fput(f);
106 }
107 return error;
108}
109
110EXPORT_SYMBOL(vfs_fstat);
111
Oleg Drokin0112fc22009-04-08 20:05:42 +0400112int vfs_fstatat(int dfd, char __user *filename, struct kstat *stat, int flag)
113{
114 int error = -EINVAL;
115
116 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
117 goto out;
118
119 if (flag & AT_SYMLINK_NOFOLLOW)
120 error = vfs_lstat_fd(dfd, filename, stat);
121 else
122 error = vfs_stat_fd(dfd, filename, stat);
123out:
124 return error;
125}
126
127EXPORT_SYMBOL(vfs_fstatat);
128
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#ifdef __ARCH_WANT_OLD_STAT
131
132/*
133 * For backward compatibility? Maybe this should be moved
134 * into arch/i386 instead?
135 */
136static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
137{
138 static int warncount = 5;
139 struct __old_kernel_stat tmp;
140
141 if (warncount > 0) {
142 warncount--;
143 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
144 current->comm);
145 } else if (warncount < 0) {
146 /* it's laughable, but... */
147 warncount = 0;
148 }
149
150 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
151 tmp.st_dev = old_encode_dev(stat->dev);
152 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700153 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
154 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 tmp.st_mode = stat->mode;
156 tmp.st_nlink = stat->nlink;
157 if (tmp.st_nlink != stat->nlink)
158 return -EOVERFLOW;
159 SET_UID(tmp.st_uid, stat->uid);
160 SET_GID(tmp.st_gid, stat->gid);
161 tmp.st_rdev = old_encode_dev(stat->rdev);
162#if BITS_PER_LONG == 32
163 if (stat->size > MAX_NON_LFS)
164 return -EOVERFLOW;
165#endif
166 tmp.st_size = stat->size;
167 tmp.st_atime = stat->atime.tv_sec;
168 tmp.st_mtime = stat->mtime.tv_sec;
169 tmp.st_ctime = stat->ctime.tv_sec;
170 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
171}
172
Heiko Carstensbdc480e2009-01-14 14:14:12 +0100173SYSCALL_DEFINE2(stat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800176 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (!error)
179 error = cp_old_stat(&stat, statbuf);
180
181 return error;
182}
Heiko Carstens257ac262009-01-14 14:14:13 +0100183
184SYSCALL_DEFINE2(lstat, char __user *, filename, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800187 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (!error)
190 error = cp_old_stat(&stat, statbuf);
191
192 return error;
193}
Heiko Carstens257ac262009-01-14 14:14:13 +0100194
195SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct kstat stat;
198 int error = vfs_fstat(fd, &stat);
199
200 if (!error)
201 error = cp_old_stat(&stat, statbuf);
202
203 return error;
204}
205
206#endif /* __ARCH_WANT_OLD_STAT */
207
208static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
209{
210 struct stat tmp;
211
212#if BITS_PER_LONG == 32
213 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
214 return -EOVERFLOW;
215#else
216 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
217 return -EOVERFLOW;
218#endif
219
220 memset(&tmp, 0, sizeof(tmp));
221#if BITS_PER_LONG == 32
222 tmp.st_dev = old_encode_dev(stat->dev);
223#else
224 tmp.st_dev = new_encode_dev(stat->dev);
225#endif
226 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700227 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
228 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 tmp.st_mode = stat->mode;
230 tmp.st_nlink = stat->nlink;
231 if (tmp.st_nlink != stat->nlink)
232 return -EOVERFLOW;
233 SET_UID(tmp.st_uid, stat->uid);
234 SET_GID(tmp.st_gid, stat->gid);
235#if BITS_PER_LONG == 32
236 tmp.st_rdev = old_encode_dev(stat->rdev);
237#else
238 tmp.st_rdev = new_encode_dev(stat->rdev);
239#endif
240#if BITS_PER_LONG == 32
241 if (stat->size > MAX_NON_LFS)
242 return -EOVERFLOW;
243#endif
244 tmp.st_size = stat->size;
245 tmp.st_atime = stat->atime.tv_sec;
246 tmp.st_mtime = stat->mtime.tv_sec;
247 tmp.st_ctime = stat->ctime.tv_sec;
248#ifdef STAT_HAVE_NSEC
249 tmp.st_atime_nsec = stat->atime.tv_nsec;
250 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
251 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
252#endif
253 tmp.st_blocks = stat->blocks;
254 tmp.st_blksize = stat->blksize;
255 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
256}
257
Heiko Carstens257ac262009-01-14 14:14:13 +0100258SYSCALL_DEFINE2(newstat, char __user *, filename, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800261 int error = vfs_stat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!error)
264 error = cp_new_stat(&stat, statbuf);
265
266 return error;
267}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800268
Heiko Carstens257ac262009-01-14 14:14:13 +0100269SYSCALL_DEFINE2(newlstat, char __user *, filename, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct kstat stat;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800272 int error = vfs_lstat_fd(AT_FDCWD, filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (!error)
275 error = cp_new_stat(&stat, statbuf);
276
277 return error;
278}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800279
Andreas Schwab2833c282006-04-27 15:46:42 +0200280#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
Heiko Carstens6559eed82009-01-14 14:14:32 +0100281SYSCALL_DEFINE4(newfstatat, int, dfd, char __user *, filename,
282 struct stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800283{
284 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400285 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800286
Oleg Drokin0112fc22009-04-08 20:05:42 +0400287 error = vfs_fstatat(dfd, filename, &stat, flag);
288 if (error)
289 return error;
290 return cp_new_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800291}
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800292#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800293
Heiko Carstens257ac262009-01-14 14:14:13 +0100294SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct kstat stat;
297 int error = vfs_fstat(fd, &stat);
298
299 if (!error)
300 error = cp_new_stat(&stat, statbuf);
301
302 return error;
303}
304
Heiko Carstens6559eed82009-01-14 14:14:32 +0100305SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
306 char __user *, buf, int, bufsiz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Al Viro2d8f3032008-07-22 09:59:21 -0400308 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int error;
310
311 if (bufsiz <= 0)
312 return -EINVAL;
313
Al Viro2d8f3032008-07-22 09:59:21 -0400314 error = user_path_at(dfd, pathname, 0, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400316 struct inode *inode = path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 error = -EINVAL;
Al Viroacfa4382008-12-04 10:06:33 -0500319 if (inode->i_op->readlink) {
Al Viro2d8f3032008-07-22 09:59:21 -0400320 error = security_inode_readlink(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400322 touch_atime(path.mnt, path.dentry);
323 error = inode->i_op->readlink(path.dentry,
Jan Blunck4ac91372008-02-14 19:34:32 -0800324 buf, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 }
Al Viro2d8f3032008-07-22 09:59:21 -0400327 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 return error;
330}
331
Heiko Carstens002c8972009-01-14 14:14:18 +0100332SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
333 int, bufsiz)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800334{
335 return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* ---------- LFS-64 ----------- */
340#ifdef __ARCH_WANT_STAT64
341
342static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
343{
344 struct stat64 tmp;
345
346 memset(&tmp, 0, sizeof(struct stat64));
347#ifdef CONFIG_MIPS
348 /* mips has weird padding, so we don't get 64 bits there */
349 if (!new_valid_dev(stat->dev) || !new_valid_dev(stat->rdev))
350 return -EOVERFLOW;
351 tmp.st_dev = new_encode_dev(stat->dev);
352 tmp.st_rdev = new_encode_dev(stat->rdev);
353#else
354 tmp.st_dev = huge_encode_dev(stat->dev);
355 tmp.st_rdev = huge_encode_dev(stat->rdev);
356#endif
357 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700358 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
359 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#ifdef STAT64_HAS_BROKEN_ST_INO
361 tmp.__st_ino = stat->ino;
362#endif
363 tmp.st_mode = stat->mode;
364 tmp.st_nlink = stat->nlink;
365 tmp.st_uid = stat->uid;
366 tmp.st_gid = stat->gid;
367 tmp.st_atime = stat->atime.tv_sec;
368 tmp.st_atime_nsec = stat->atime.tv_nsec;
369 tmp.st_mtime = stat->mtime.tv_sec;
370 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
371 tmp.st_ctime = stat->ctime.tv_sec;
372 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
373 tmp.st_size = stat->size;
374 tmp.st_blocks = stat->blocks;
375 tmp.st_blksize = stat->blksize;
376 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
377}
378
Heiko Carstens257ac262009-01-14 14:14:13 +0100379SYSCALL_DEFINE2(stat64, char __user *, filename, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct kstat stat;
382 int error = vfs_stat(filename, &stat);
383
384 if (!error)
385 error = cp_new_stat64(&stat, statbuf);
386
387 return error;
388}
Heiko Carstens257ac262009-01-14 14:14:13 +0100389
390SYSCALL_DEFINE2(lstat64, char __user *, filename, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct kstat stat;
393 int error = vfs_lstat(filename, &stat);
394
395 if (!error)
396 error = cp_new_stat64(&stat, statbuf);
397
398 return error;
399}
Heiko Carstens257ac262009-01-14 14:14:13 +0100400
401SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct kstat stat;
404 int error = vfs_fstat(fd, &stat);
405
406 if (!error)
407 error = cp_new_stat64(&stat, statbuf);
408
409 return error;
410}
411
Heiko Carstens6559eed82009-01-14 14:14:32 +0100412SYSCALL_DEFINE4(fstatat64, int, dfd, char __user *, filename,
413 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800414{
415 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400416 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800417
Oleg Drokin0112fc22009-04-08 20:05:42 +0400418 error = vfs_fstatat(dfd, filename, &stat, flag);
419 if (error)
420 return error;
421 return cp_new_stat64(&stat, statbuf);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif /* __ARCH_WANT_STAT64 */
424
425void inode_add_bytes(struct inode *inode, loff_t bytes)
426{
427 spin_lock(&inode->i_lock);
428 inode->i_blocks += bytes >> 9;
429 bytes &= 511;
430 inode->i_bytes += bytes;
431 if (inode->i_bytes >= 512) {
432 inode->i_blocks++;
433 inode->i_bytes -= 512;
434 }
435 spin_unlock(&inode->i_lock);
436}
437
438EXPORT_SYMBOL(inode_add_bytes);
439
440void inode_sub_bytes(struct inode *inode, loff_t bytes)
441{
442 spin_lock(&inode->i_lock);
443 inode->i_blocks -= bytes >> 9;
444 bytes &= 511;
445 if (inode->i_bytes < bytes) {
446 inode->i_blocks--;
447 inode->i_bytes += 512;
448 }
449 inode->i_bytes -= bytes;
450 spin_unlock(&inode->i_lock);
451}
452
453EXPORT_SYMBOL(inode_sub_bytes);
454
455loff_t inode_get_bytes(struct inode *inode)
456{
457 loff_t ret;
458
459 spin_lock(&inode->i_lock);
460 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
461 spin_unlock(&inode->i_lock);
462 return ret;
463}
464
465EXPORT_SYMBOL(inode_get_bytes);
466
467void inode_set_bytes(struct inode *inode, loff_t bytes)
468{
469 /* Caller is here responsible for sufficient locking
470 * (ie. inode->i_lock) */
471 inode->i_blocks = bytes >> 9;
472 inode->i_bytes = bytes & 511;
473}
474
475EXPORT_SYMBOL(inode_set_bytes);