]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/stat.c
sched/headers: Prepare to move the memalloc_noio_*() APIs to <linux/sched/mm.h>
[mirror_ubuntu-artful-kernel.git] / fs / stat.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/stat.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
630d9c47 7#include <linux/export.h>
1da177e4
LT
8#include <linux/mm.h>
9#include <linux/errno.h>
10#include <linux/file.h>
1da177e4
LT
11#include <linux/highuid.h>
12#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/security.h>
5b825c3a 15#include <linux/cred.h>
1da177e4 16#include <linux/syscalls.h>
ba52de12 17#include <linux/pagemap.h>
1da177e4 18
7c0f6ba6 19#include <linux/uaccess.h>
1da177e4
LT
20#include <asm/unistd.h>
21
22void generic_fillattr(struct inode *inode, struct kstat *stat)
23{
24 stat->dev = inode->i_sb->s_dev;
25 stat->ino = inode->i_ino;
26 stat->mode = inode->i_mode;
27 stat->nlink = inode->i_nlink;
28 stat->uid = inode->i_uid;
29 stat->gid = inode->i_gid;
30 stat->rdev = inode->i_rdev;
3ddcd056 31 stat->size = i_size_read(inode);
1da177e4
LT
32 stat->atime = inode->i_atime;
33 stat->mtime = inode->i_mtime;
34 stat->ctime = inode->i_ctime;
93407472 35 stat->blksize = i_blocksize(inode);
3ddcd056 36 stat->blocks = inode->i_blocks;
1da177e4
LT
37}
38
39EXPORT_SYMBOL(generic_fillattr);
40
b7a6ec52
BF
41/**
42 * vfs_getattr_nosec - getattr without security checks
43 * @path: file to get attributes from
44 * @stat: structure to return attributes in
45 *
46 * Get attributes without calling security_inode_getattr.
47 *
48 * Currently the only caller other than vfs_getattr is internal to the
49 * filehandle lookup code, which uses only the inode number and returns
50 * no attributes to any user. Any other code probably wants
51 * vfs_getattr.
52 */
53int vfs_getattr_nosec(struct path *path, struct kstat *stat)
1da177e4 54{
bb668734 55 struct inode *inode = d_backing_inode(path->dentry);
1da177e4
LT
56
57 if (inode->i_op->getattr)
3dadecce 58 return inode->i_op->getattr(path->mnt, path->dentry, stat);
1da177e4
LT
59
60 generic_fillattr(inode, stat);
1da177e4
LT
61 return 0;
62}
63
b7a6ec52
BF
64EXPORT_SYMBOL(vfs_getattr_nosec);
65
66int vfs_getattr(struct path *path, struct kstat *stat)
67{
68 int retval;
69
3f7036a0 70 retval = security_inode_getattr(path);
b7a6ec52
BF
71 if (retval)
72 return retval;
73 return vfs_getattr_nosec(path, stat);
74}
75
1da177e4
LT
76EXPORT_SYMBOL(vfs_getattr);
77
1da177e4
LT
78int vfs_fstat(unsigned int fd, struct kstat *stat)
79{
2903ff01 80 struct fd f = fdget_raw(fd);
1da177e4
LT
81 int error = -EBADF;
82
2903ff01 83 if (f.file) {
3dadecce 84 error = vfs_getattr(&f.file->f_path, stat);
2903ff01 85 fdput(f);
1da177e4
LT
86 }
87 return error;
88}
1da177e4
LT
89EXPORT_SYMBOL(vfs_fstat);
90
c7887325
DH
91int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
92 int flag)
0112fc22 93{
2eae7a18 94 struct path path;
0112fc22 95 int error = -EINVAL;
836fb7e7 96 unsigned int lookup_flags = 0;
0112fc22 97
65cfc672
AV
98 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT |
99 AT_EMPTY_PATH)) != 0)
0112fc22
OD
100 goto out;
101
2eae7a18
CH
102 if (!(flag & AT_SYMLINK_NOFOLLOW))
103 lookup_flags |= LOOKUP_FOLLOW;
65cfc672
AV
104 if (flag & AT_EMPTY_PATH)
105 lookup_flags |= LOOKUP_EMPTY;
836fb7e7 106retry:
2eae7a18
CH
107 error = user_path_at(dfd, filename, lookup_flags, &path);
108 if (error)
109 goto out;
110
3dadecce 111 error = vfs_getattr(&path, stat);
2eae7a18 112 path_put(&path);
836fb7e7
JL
113 if (retry_estale(error, lookup_flags)) {
114 lookup_flags |= LOOKUP_REVAL;
115 goto retry;
116 }
0112fc22
OD
117out:
118 return error;
119}
0112fc22
OD
120EXPORT_SYMBOL(vfs_fstatat);
121
c7887325 122int vfs_stat(const char __user *name, struct kstat *stat)
2eae7a18
CH
123{
124 return vfs_fstatat(AT_FDCWD, name, stat, 0);
125}
126EXPORT_SYMBOL(vfs_stat);
127
c7887325 128int vfs_lstat(const char __user *name, struct kstat *stat)
2eae7a18
CH
129{
130 return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
131}
132EXPORT_SYMBOL(vfs_lstat);
133
0112fc22 134
1da177e4
LT
135#ifdef __ARCH_WANT_OLD_STAT
136
137/*
138 * For backward compatibility? Maybe this should be moved
139 * into arch/i386 instead?
140 */
141static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
142{
143 static int warncount = 5;
144 struct __old_kernel_stat tmp;
145
146 if (warncount > 0) {
147 warncount--;
148 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
149 current->comm);
150 } else if (warncount < 0) {
151 /* it's laughable, but... */
152 warncount = 0;
153 }
154
155 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
156 tmp.st_dev = old_encode_dev(stat->dev);
157 tmp.st_ino = stat->ino;
afefdbb2
DH
158 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
159 return -EOVERFLOW;
1da177e4
LT
160 tmp.st_mode = stat->mode;
161 tmp.st_nlink = stat->nlink;
162 if (tmp.st_nlink != stat->nlink)
163 return -EOVERFLOW;
a7c1938e
EB
164 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
165 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
1da177e4
LT
166 tmp.st_rdev = old_encode_dev(stat->rdev);
167#if BITS_PER_LONG == 32
168 if (stat->size > MAX_NON_LFS)
169 return -EOVERFLOW;
170#endif
171 tmp.st_size = stat->size;
172 tmp.st_atime = stat->atime.tv_sec;
173 tmp.st_mtime = stat->mtime.tv_sec;
174 tmp.st_ctime = stat->ctime.tv_sec;
175 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
176}
177
c7887325
DH
178SYSCALL_DEFINE2(stat, const char __user *, filename,
179 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
180{
181 struct kstat stat;
2eae7a18 182 int error;
1da177e4 183
2eae7a18
CH
184 error = vfs_stat(filename, &stat);
185 if (error)
186 return error;
1da177e4 187
2eae7a18 188 return cp_old_stat(&stat, statbuf);
1da177e4 189}
257ac264 190
c7887325
DH
191SYSCALL_DEFINE2(lstat, const char __user *, filename,
192 struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
193{
194 struct kstat stat;
2eae7a18 195 int error;
1da177e4 196
2eae7a18
CH
197 error = vfs_lstat(filename, &stat);
198 if (error)
199 return error;
1da177e4 200
2eae7a18 201 return cp_old_stat(&stat, statbuf);
1da177e4 202}
257ac264
HC
203
204SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
1da177e4
LT
205{
206 struct kstat stat;
207 int error = vfs_fstat(fd, &stat);
208
209 if (!error)
210 error = cp_old_stat(&stat, statbuf);
211
212 return error;
213}
214
215#endif /* __ARCH_WANT_OLD_STAT */
216
a52dd971
LT
217#if BITS_PER_LONG == 32
218# define choose_32_64(a,b) a
219#else
220# define choose_32_64(a,b) b
221#endif
222
4c416f42 223#define valid_dev(x) choose_32_64(old_valid_dev(x),true)
a52dd971
LT
224#define encode_dev(x) choose_32_64(old_encode_dev,new_encode_dev)(x)
225
8529f613
LT
226#ifndef INIT_STRUCT_STAT_PADDING
227# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
228#endif
229
1da177e4
LT
230static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
231{
232 struct stat tmp;
233
a52dd971 234 if (!valid_dev(stat->dev) || !valid_dev(stat->rdev))
1da177e4 235 return -EOVERFLOW;
a52dd971
LT
236#if BITS_PER_LONG == 32
237 if (stat->size > MAX_NON_LFS)
1da177e4
LT
238 return -EOVERFLOW;
239#endif
240
8529f613 241 INIT_STRUCT_STAT_PADDING(tmp);
a52dd971 242 tmp.st_dev = encode_dev(stat->dev);
1da177e4 243 tmp.st_ino = stat->ino;
afefdbb2
DH
244 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
245 return -EOVERFLOW;
1da177e4
LT
246 tmp.st_mode = stat->mode;
247 tmp.st_nlink = stat->nlink;
248 if (tmp.st_nlink != stat->nlink)
249 return -EOVERFLOW;
a7c1938e
EB
250 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
251 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
a52dd971 252 tmp.st_rdev = encode_dev(stat->rdev);
1da177e4
LT
253 tmp.st_size = stat->size;
254 tmp.st_atime = stat->atime.tv_sec;
255 tmp.st_mtime = stat->mtime.tv_sec;
256 tmp.st_ctime = stat->ctime.tv_sec;
257#ifdef STAT_HAVE_NSEC
258 tmp.st_atime_nsec = stat->atime.tv_nsec;
259 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
260 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
261#endif
262 tmp.st_blocks = stat->blocks;
263 tmp.st_blksize = stat->blksize;
264 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
265}
266
c7887325
DH
267SYSCALL_DEFINE2(newstat, const char __user *, filename,
268 struct stat __user *, statbuf)
5590ff0d
UD
269{
270 struct kstat stat;
2eae7a18 271 int error = vfs_stat(filename, &stat);
5590ff0d 272
2eae7a18
CH
273 if (error)
274 return error;
275 return cp_new_stat(&stat, statbuf);
5590ff0d
UD
276}
277
c7887325
DH
278SYSCALL_DEFINE2(newlstat, const char __user *, filename,
279 struct stat __user *, statbuf)
1da177e4
LT
280{
281 struct kstat stat;
2eae7a18 282 int error;
1da177e4 283
2eae7a18
CH
284 error = vfs_lstat(filename, &stat);
285 if (error)
286 return error;
1da177e4 287
2eae7a18 288 return cp_new_stat(&stat, statbuf);
1da177e4 289}
5590ff0d 290
2833c28a 291#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
c7887325 292SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
6559eed8 293 struct stat __user *, statbuf, int, flag)
1da177e4
LT
294{
295 struct kstat stat;
0112fc22 296 int error;
1da177e4 297
0112fc22
OD
298 error = vfs_fstatat(dfd, filename, &stat, flag);
299 if (error)
300 return error;
301 return cp_new_stat(&stat, statbuf);
1da177e4 302}
cff2b760 303#endif
5590ff0d 304
257ac264 305SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
1da177e4
LT
306{
307 struct kstat stat;
308 int error = vfs_fstat(fd, &stat);
309
310 if (!error)
311 error = cp_new_stat(&stat, statbuf);
312
313 return error;
314}
315
6559eed8
HC
316SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
317 char __user *, buf, int, bufsiz)
1da177e4 318{
2d8f3038 319 struct path path;
1da177e4 320 int error;
1fa1e7f6 321 int empty = 0;
7955119e 322 unsigned int lookup_flags = LOOKUP_EMPTY;
1da177e4
LT
323
324 if (bufsiz <= 0)
325 return -EINVAL;
326
7955119e
JL
327retry:
328 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
1da177e4 329 if (!error) {
bb668734 330 struct inode *inode = d_backing_inode(path.dentry);
1da177e4 331
1fa1e7f6 332 error = empty ? -ENOENT : -EINVAL;
fd4a0edf
MS
333 /*
334 * AFS mountpoints allow readlink(2) but are not symlinks
335 */
336 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
2d8f3038 337 error = security_inode_readlink(path.dentry);
1da177e4 338 if (!error) {
68ac1234 339 touch_atime(&path);
fd4a0edf 340 error = vfs_readlink(path.dentry, buf, bufsiz);
1da177e4
LT
341 }
342 }
2d8f3038 343 path_put(&path);
7955119e
JL
344 if (retry_estale(error, lookup_flags)) {
345 lookup_flags |= LOOKUP_REVAL;
346 goto retry;
347 }
1da177e4
LT
348 }
349 return error;
350}
351
002c8976
HC
352SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
353 int, bufsiz)
5590ff0d
UD
354{
355 return sys_readlinkat(AT_FDCWD, path, buf, bufsiz);
356}
357
1da177e4
LT
358
359/* ---------- LFS-64 ----------- */
0753f70f 360#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
1da177e4 361
8529f613
LT
362#ifndef INIT_STRUCT_STAT64_PADDING
363# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
364#endif
365
1da177e4
LT
366static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
367{
368 struct stat64 tmp;
369
8529f613 370 INIT_STRUCT_STAT64_PADDING(tmp);
1da177e4
LT
371#ifdef CONFIG_MIPS
372 /* mips has weird padding, so we don't get 64 bits there */
1da177e4
LT
373 tmp.st_dev = new_encode_dev(stat->dev);
374 tmp.st_rdev = new_encode_dev(stat->rdev);
375#else
376 tmp.st_dev = huge_encode_dev(stat->dev);
377 tmp.st_rdev = huge_encode_dev(stat->rdev);
378#endif
379 tmp.st_ino = stat->ino;
afefdbb2
DH
380 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
381 return -EOVERFLOW;
1da177e4
LT
382#ifdef STAT64_HAS_BROKEN_ST_INO
383 tmp.__st_ino = stat->ino;
384#endif
385 tmp.st_mode = stat->mode;
386 tmp.st_nlink = stat->nlink;
a7c1938e
EB
387 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
388 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
1da177e4
LT
389 tmp.st_atime = stat->atime.tv_sec;
390 tmp.st_atime_nsec = stat->atime.tv_nsec;
391 tmp.st_mtime = stat->mtime.tv_sec;
392 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
393 tmp.st_ctime = stat->ctime.tv_sec;
394 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
395 tmp.st_size = stat->size;
396 tmp.st_blocks = stat->blocks;
397 tmp.st_blksize = stat->blksize;
398 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
399}
400
c7887325
DH
401SYSCALL_DEFINE2(stat64, const char __user *, filename,
402 struct stat64 __user *, statbuf)
1da177e4
LT
403{
404 struct kstat stat;
405 int error = vfs_stat(filename, &stat);
406
407 if (!error)
408 error = cp_new_stat64(&stat, statbuf);
409
410 return error;
411}
257ac264 412
c7887325
DH
413SYSCALL_DEFINE2(lstat64, const char __user *, filename,
414 struct stat64 __user *, statbuf)
1da177e4
LT
415{
416 struct kstat stat;
417 int error = vfs_lstat(filename, &stat);
418
419 if (!error)
420 error = cp_new_stat64(&stat, statbuf);
421
422 return error;
423}
257ac264
HC
424
425SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
1da177e4
LT
426{
427 struct kstat stat;
428 int error = vfs_fstat(fd, &stat);
429
430 if (!error)
431 error = cp_new_stat64(&stat, statbuf);
432
433 return error;
434}
435
c7887325 436SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
6559eed8 437 struct stat64 __user *, statbuf, int, flag)
cff2b760
UD
438{
439 struct kstat stat;
0112fc22 440 int error;
cff2b760 441
0112fc22
OD
442 error = vfs_fstatat(dfd, filename, &stat, flag);
443 if (error)
444 return error;
445 return cp_new_stat64(&stat, statbuf);
cff2b760 446}
0753f70f 447#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
1da177e4 448
b462707e
DM
449/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
450void __inode_add_bytes(struct inode *inode, loff_t bytes)
1da177e4 451{
1da177e4
LT
452 inode->i_blocks += bytes >> 9;
453 bytes &= 511;
454 inode->i_bytes += bytes;
455 if (inode->i_bytes >= 512) {
456 inode->i_blocks++;
457 inode->i_bytes -= 512;
458 }
b462707e
DM
459}
460
461void inode_add_bytes(struct inode *inode, loff_t bytes)
462{
463 spin_lock(&inode->i_lock);
464 __inode_add_bytes(inode, bytes);
1da177e4
LT
465 spin_unlock(&inode->i_lock);
466}
467
468EXPORT_SYMBOL(inode_add_bytes);
469
1c8924eb 470void __inode_sub_bytes(struct inode *inode, loff_t bytes)
1da177e4 471{
1da177e4
LT
472 inode->i_blocks -= bytes >> 9;
473 bytes &= 511;
474 if (inode->i_bytes < bytes) {
475 inode->i_blocks--;
476 inode->i_bytes += 512;
477 }
478 inode->i_bytes -= bytes;
1c8924eb
JK
479}
480
481EXPORT_SYMBOL(__inode_sub_bytes);
482
483void inode_sub_bytes(struct inode *inode, loff_t bytes)
484{
485 spin_lock(&inode->i_lock);
486 __inode_sub_bytes(inode, bytes);
1da177e4
LT
487 spin_unlock(&inode->i_lock);
488}
489
490EXPORT_SYMBOL(inode_sub_bytes);
491
492loff_t inode_get_bytes(struct inode *inode)
493{
494 loff_t ret;
495
496 spin_lock(&inode->i_lock);
497 ret = (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
498 spin_unlock(&inode->i_lock);
499 return ret;
500}
501
502EXPORT_SYMBOL(inode_get_bytes);
503
504void inode_set_bytes(struct inode *inode, loff_t bytes)
505{
506 /* Caller is here responsible for sufficient locking
507 * (ie. inode->i_lock) */
508 inode->i_blocks = bytes >> 9;
509 inode->i_bytes = bytes & 511;
510}
511
512EXPORT_SYMBOL(inode_set_bytes);