]> Git Repo - linux.git/blob - fs/stat.c
fs: Simplify getattr interface function checking AT_GETATTR_NOSEC flag
[linux.git] / fs / stat.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/stat.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7
8 #include <linux/blkdev.h>
9 #include <linux/export.h>
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/highuid.h>
14 #include <linux/fs.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/cred.h>
18 #include <linux/syscalls.h>
19 #include <linux/pagemap.h>
20 #include <linux/compat.h>
21 #include <linux/iversion.h>
22
23 #include <linux/uaccess.h>
24 #include <asm/unistd.h>
25
26 #include "internal.h"
27 #include "mount.h"
28
29 /**
30  * generic_fillattr - Fill in the basic attributes from the inode struct
31  * @idmap:              idmap of the mount the inode was found from
32  * @request_mask:       statx request_mask
33  * @inode:              Inode to use as the source
34  * @stat:               Where to fill in the attributes
35  *
36  * Fill in the basic attributes in the kstat structure from data that's to be
37  * found on the VFS inode structure.  This is the default if no getattr inode
38  * operation is supplied.
39  *
40  * If the inode has been found through an idmapped mount the idmap of
41  * the vfsmount must be passed through @idmap. This function will then
42  * take care to map the inode according to @idmap before filling in the
43  * uid and gid filds. On non-idmapped mounts or if permission checking is to be
44  * performed on the raw inode simply pass @nop_mnt_idmap.
45  */
46 void generic_fillattr(struct mnt_idmap *idmap, u32 request_mask,
47                       struct inode *inode, struct kstat *stat)
48 {
49         vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode);
50         vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode);
51
52         stat->dev = inode->i_sb->s_dev;
53         stat->ino = inode->i_ino;
54         stat->mode = inode->i_mode;
55         stat->nlink = inode->i_nlink;
56         stat->uid = vfsuid_into_kuid(vfsuid);
57         stat->gid = vfsgid_into_kgid(vfsgid);
58         stat->rdev = inode->i_rdev;
59         stat->size = i_size_read(inode);
60         stat->atime = inode_get_atime(inode);
61         stat->mtime = inode_get_mtime(inode);
62         stat->ctime = inode_get_ctime(inode);
63         stat->blksize = i_blocksize(inode);
64         stat->blocks = inode->i_blocks;
65
66         if ((request_mask & STATX_CHANGE_COOKIE) && IS_I_VERSION(inode)) {
67                 stat->result_mask |= STATX_CHANGE_COOKIE;
68                 stat->change_cookie = inode_query_iversion(inode);
69         }
70
71 }
72 EXPORT_SYMBOL(generic_fillattr);
73
74 /**
75  * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
76  * @inode:      Inode to use as the source
77  * @stat:       Where to fill in the attribute flags
78  *
79  * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
80  * inode that are published on i_flags and enforced by the VFS.
81  */
82 void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
83 {
84         if (inode->i_flags & S_IMMUTABLE)
85                 stat->attributes |= STATX_ATTR_IMMUTABLE;
86         if (inode->i_flags & S_APPEND)
87                 stat->attributes |= STATX_ATTR_APPEND;
88         stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
89 }
90 EXPORT_SYMBOL(generic_fill_statx_attr);
91
92 /**
93  * generic_fill_statx_atomic_writes - Fill in atomic writes statx attributes
94  * @stat:       Where to fill in the attribute flags
95  * @unit_min:   Minimum supported atomic write length in bytes
96  * @unit_max:   Maximum supported atomic write length in bytes
97  *
98  * Fill in the STATX{_ATTR}_WRITE_ATOMIC flags in the kstat structure from
99  * atomic write unit_min and unit_max values.
100  */
101 void generic_fill_statx_atomic_writes(struct kstat *stat,
102                                       unsigned int unit_min,
103                                       unsigned int unit_max)
104 {
105         /* Confirm that the request type is known */
106         stat->result_mask |= STATX_WRITE_ATOMIC;
107
108         /* Confirm that the file attribute type is known */
109         stat->attributes_mask |= STATX_ATTR_WRITE_ATOMIC;
110
111         if (unit_min) {
112                 stat->atomic_write_unit_min = unit_min;
113                 stat->atomic_write_unit_max = unit_max;
114                 /* Initially only allow 1x segment */
115                 stat->atomic_write_segments_max = 1;
116
117                 /* Confirm atomic writes are actually supported */
118                 stat->attributes |= STATX_ATTR_WRITE_ATOMIC;
119         }
120 }
121 EXPORT_SYMBOL_GPL(generic_fill_statx_atomic_writes);
122
123 /**
124  * vfs_getattr_nosec - getattr without security checks
125  * @path: file to get attributes from
126  * @stat: structure to return attributes in
127  * @request_mask: STATX_xxx flags indicating what the caller wants
128  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
129  *
130  * Get attributes without calling security_inode_getattr.
131  *
132  * Currently the only caller other than vfs_getattr is internal to the
133  * filehandle lookup code, which uses only the inode number and returns no
134  * attributes to any user.  Any other code probably wants vfs_getattr.
135  */
136 int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
137                       u32 request_mask, unsigned int query_flags)
138 {
139         struct mnt_idmap *idmap;
140         struct inode *inode = d_backing_inode(path->dentry);
141
142         memset(stat, 0, sizeof(*stat));
143         stat->result_mask |= STATX_BASIC_STATS;
144         query_flags &= AT_STATX_SYNC_TYPE;
145
146         /* allow the fs to override these if it really wants to */
147         /* SB_NOATIME means filesystem supplies dummy atime value */
148         if (inode->i_sb->s_flags & SB_NOATIME)
149                 stat->result_mask &= ~STATX_ATIME;
150
151         /*
152          * Note: If you add another clause to set an attribute flag, please
153          * update attributes_mask below.
154          */
155         if (IS_AUTOMOUNT(inode))
156                 stat->attributes |= STATX_ATTR_AUTOMOUNT;
157
158         if (IS_DAX(inode))
159                 stat->attributes |= STATX_ATTR_DAX;
160
161         stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
162                                   STATX_ATTR_DAX);
163
164         idmap = mnt_idmap(path->mnt);
165         if (inode->i_op->getattr)
166                 return inode->i_op->getattr(idmap, path, stat,
167                                             request_mask,
168                                             query_flags);
169
170         generic_fillattr(idmap, request_mask, inode, stat);
171         return 0;
172 }
173 EXPORT_SYMBOL(vfs_getattr_nosec);
174
175 /*
176  * vfs_getattr - Get the enhanced basic attributes of a file
177  * @path: The file of interest
178  * @stat: Where to return the statistics
179  * @request_mask: STATX_xxx flags indicating what the caller wants
180  * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
181  *
182  * Ask the filesystem for a file's attributes.  The caller must indicate in
183  * request_mask and query_flags to indicate what they want.
184  *
185  * If the file is remote, the filesystem can be forced to update the attributes
186  * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
187  * suppress the update by passing AT_STATX_DONT_SYNC.
188  *
189  * Bits must have been set in request_mask to indicate which attributes the
190  * caller wants retrieving.  Any such attribute not requested may be returned
191  * anyway, but the value may be approximate, and, if remote, may not have been
192  * synchronised with the server.
193  *
194  * 0 will be returned on success, and a -ve error code if unsuccessful.
195  */
196 int vfs_getattr(const struct path *path, struct kstat *stat,
197                 u32 request_mask, unsigned int query_flags)
198 {
199         int retval;
200
201         retval = security_inode_getattr(path);
202         if (retval)
203                 return retval;
204         return vfs_getattr_nosec(path, stat, request_mask, query_flags);
205 }
206 EXPORT_SYMBOL(vfs_getattr);
207
208 /**
209  * vfs_fstat - Get the basic attributes by file descriptor
210  * @fd: The file descriptor referring to the file of interest
211  * @stat: The result structure to fill in.
212  *
213  * This function is a wrapper around vfs_getattr().  The main difference is
214  * that it uses a file descriptor to determine the file location.
215  *
216  * 0 will be returned on success, and a -ve error code if unsuccessful.
217  */
218 int vfs_fstat(int fd, struct kstat *stat)
219 {
220         CLASS(fd_raw, f)(fd);
221         if (fd_empty(f))
222                 return -EBADF;
223         return vfs_getattr(&fd_file(f)->f_path, stat, STATX_BASIC_STATS, 0);
224 }
225
226 static int statx_lookup_flags(int flags)
227 {
228         int lookup_flags = 0;
229
230         if (!(flags & AT_SYMLINK_NOFOLLOW))
231                 lookup_flags |= LOOKUP_FOLLOW;
232         if (!(flags & AT_NO_AUTOMOUNT))
233                 lookup_flags |= LOOKUP_AUTOMOUNT;
234
235         return lookup_flags;
236 }
237
238 static int vfs_statx_path(struct path *path, int flags, struct kstat *stat,
239                           u32 request_mask)
240 {
241         int error = vfs_getattr(path, stat, request_mask, flags);
242
243         if (request_mask & STATX_MNT_ID_UNIQUE) {
244                 stat->mnt_id = real_mount(path->mnt)->mnt_id_unique;
245                 stat->result_mask |= STATX_MNT_ID_UNIQUE;
246         } else {
247                 stat->mnt_id = real_mount(path->mnt)->mnt_id;
248                 stat->result_mask |= STATX_MNT_ID;
249         }
250
251         if (path_mounted(path))
252                 stat->attributes |= STATX_ATTR_MOUNT_ROOT;
253         stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
254
255         /*
256          * If this is a block device inode, override the filesystem
257          * attributes with the block device specific parameters that need to be
258          * obtained from the bdev backing inode.
259          */
260         if (S_ISBLK(stat->mode))
261                 bdev_statx(path, stat, request_mask);
262
263         return error;
264 }
265
266 static int vfs_statx_fd(int fd, int flags, struct kstat *stat,
267                           u32 request_mask)
268 {
269         CLASS(fd_raw, f)(fd);
270         if (fd_empty(f))
271                 return -EBADF;
272         return vfs_statx_path(&fd_file(f)->f_path, flags, stat, request_mask);
273 }
274
275 /**
276  * vfs_statx - Get basic and extra attributes by filename
277  * @dfd: A file descriptor representing the base dir for a relative filename
278  * @filename: The name of the file of interest
279  * @flags: Flags to control the query
280  * @stat: The result structure to fill in.
281  * @request_mask: STATX_xxx flags indicating what the caller wants
282  *
283  * This function is a wrapper around vfs_getattr().  The main difference is
284  * that it uses a filename and base directory to determine the file location.
285  * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
286  * at the given name from being referenced.
287  *
288  * 0 will be returned on success, and a -ve error code if unsuccessful.
289  */
290 static int vfs_statx(int dfd, struct filename *filename, int flags,
291               struct kstat *stat, u32 request_mask)
292 {
293         struct path path;
294         unsigned int lookup_flags = statx_lookup_flags(flags);
295         int error;
296
297         if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
298                       AT_STATX_SYNC_TYPE))
299                 return -EINVAL;
300
301 retry:
302         error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
303         if (error)
304                 return error;
305         error = vfs_statx_path(&path, flags, stat, request_mask);
306         path_put(&path);
307         if (retry_estale(error, lookup_flags)) {
308                 lookup_flags |= LOOKUP_REVAL;
309                 goto retry;
310         }
311         return error;
312 }
313
314 int vfs_fstatat(int dfd, const char __user *filename,
315                               struct kstat *stat, int flags)
316 {
317         int ret;
318         int statx_flags = flags | AT_NO_AUTOMOUNT;
319         struct filename *name = getname_maybe_null(filename, flags);
320
321         if (!name && dfd >= 0)
322                 return vfs_fstat(dfd, stat);
323
324         ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
325         putname(name);
326
327         return ret;
328 }
329
330 #ifdef __ARCH_WANT_OLD_STAT
331
332 /*
333  * For backward compatibility?  Maybe this should be moved
334  * into arch/i386 instead?
335  */
336 static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
337 {
338         static int warncount = 5;
339         struct __old_kernel_stat tmp;
340
341         if (warncount > 0) {
342                 warncount--;
343                 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
344                         current->comm);
345         } else if (warncount < 0) {
346                 /* it's laughable, but... */
347                 warncount = 0;
348         }
349
350         memset(&tmp, 0, sizeof(struct __old_kernel_stat));
351         tmp.st_dev = old_encode_dev(stat->dev);
352         tmp.st_ino = stat->ino;
353         if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
354                 return -EOVERFLOW;
355         tmp.st_mode = stat->mode;
356         tmp.st_nlink = stat->nlink;
357         if (tmp.st_nlink != stat->nlink)
358                 return -EOVERFLOW;
359         SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
360         SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
361         tmp.st_rdev = old_encode_dev(stat->rdev);
362 #if BITS_PER_LONG == 32
363         if (stat->size > MAX_NON_LFS)
364                 return -EOVERFLOW;
365 #endif
366         tmp.st_size = stat->size;
367         tmp.st_atime = stat->atime.tv_sec;
368         tmp.st_mtime = stat->mtime.tv_sec;
369         tmp.st_ctime = stat->ctime.tv_sec;
370         return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
371 }
372
373 SYSCALL_DEFINE2(stat, const char __user *, filename,
374                 struct __old_kernel_stat __user *, statbuf)
375 {
376         struct kstat stat;
377         int error;
378
379         error = vfs_stat(filename, &stat);
380         if (error)
381                 return error;
382
383         return cp_old_stat(&stat, statbuf);
384 }
385
386 SYSCALL_DEFINE2(lstat, const char __user *, filename,
387                 struct __old_kernel_stat __user *, statbuf)
388 {
389         struct kstat stat;
390         int error;
391
392         error = vfs_lstat(filename, &stat);
393         if (error)
394                 return error;
395
396         return cp_old_stat(&stat, statbuf);
397 }
398
399 SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
400 {
401         struct kstat stat;
402         int error = vfs_fstat(fd, &stat);
403
404         if (!error)
405                 error = cp_old_stat(&stat, statbuf);
406
407         return error;
408 }
409
410 #endif /* __ARCH_WANT_OLD_STAT */
411
412 #ifdef __ARCH_WANT_NEW_STAT
413
414 #ifndef INIT_STRUCT_STAT_PADDING
415 #  define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
416 #endif
417
418 static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
419 {
420         struct stat tmp;
421
422         if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
423                 return -EOVERFLOW;
424         if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
425                 return -EOVERFLOW;
426 #if BITS_PER_LONG == 32
427         if (stat->size > MAX_NON_LFS)
428                 return -EOVERFLOW;
429 #endif
430
431         INIT_STRUCT_STAT_PADDING(tmp);
432         tmp.st_dev = new_encode_dev(stat->dev);
433         tmp.st_ino = stat->ino;
434         if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
435                 return -EOVERFLOW;
436         tmp.st_mode = stat->mode;
437         tmp.st_nlink = stat->nlink;
438         if (tmp.st_nlink != stat->nlink)
439                 return -EOVERFLOW;
440         SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
441         SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
442         tmp.st_rdev = new_encode_dev(stat->rdev);
443         tmp.st_size = stat->size;
444         tmp.st_atime = stat->atime.tv_sec;
445         tmp.st_mtime = stat->mtime.tv_sec;
446         tmp.st_ctime = stat->ctime.tv_sec;
447 #ifdef STAT_HAVE_NSEC
448         tmp.st_atime_nsec = stat->atime.tv_nsec;
449         tmp.st_mtime_nsec = stat->mtime.tv_nsec;
450         tmp.st_ctime_nsec = stat->ctime.tv_nsec;
451 #endif
452         tmp.st_blocks = stat->blocks;
453         tmp.st_blksize = stat->blksize;
454         return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
455 }
456
457 SYSCALL_DEFINE2(newstat, const char __user *, filename,
458                 struct stat __user *, statbuf)
459 {
460         struct kstat stat;
461         int error = vfs_stat(filename, &stat);
462
463         if (error)
464                 return error;
465         return cp_new_stat(&stat, statbuf);
466 }
467
468 SYSCALL_DEFINE2(newlstat, const char __user *, filename,
469                 struct stat __user *, statbuf)
470 {
471         struct kstat stat;
472         int error;
473
474         error = vfs_lstat(filename, &stat);
475         if (error)
476                 return error;
477
478         return cp_new_stat(&stat, statbuf);
479 }
480
481 #if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
482 SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
483                 struct stat __user *, statbuf, int, flag)
484 {
485         struct kstat stat;
486         int error;
487
488         error = vfs_fstatat(dfd, filename, &stat, flag);
489         if (error)
490                 return error;
491         return cp_new_stat(&stat, statbuf);
492 }
493 #endif
494
495 SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
496 {
497         struct kstat stat;
498         int error = vfs_fstat(fd, &stat);
499
500         if (!error)
501                 error = cp_new_stat(&stat, statbuf);
502
503         return error;
504 }
505 #endif
506
507 static int do_readlinkat(int dfd, const char __user *pathname,
508                          char __user *buf, int bufsiz)
509 {
510         struct path path;
511         struct filename *name;
512         int error;
513         unsigned int lookup_flags = LOOKUP_EMPTY;
514
515         if (bufsiz <= 0)
516                 return -EINVAL;
517
518 retry:
519         name = getname_flags(pathname, lookup_flags);
520         error = filename_lookup(dfd, name, lookup_flags, &path, NULL);
521         if (unlikely(error)) {
522                 putname(name);
523                 return error;
524         }
525
526         /*
527          * AFS mountpoints allow readlink(2) but are not symlinks
528          */
529         if (d_is_symlink(path.dentry) ||
530             d_backing_inode(path.dentry)->i_op->readlink) {
531                 error = security_inode_readlink(path.dentry);
532                 if (!error) {
533                         touch_atime(&path);
534                         error = vfs_readlink(path.dentry, buf, bufsiz);
535                 }
536         } else {
537                 error = (name->name[0] == '\0') ? -ENOENT : -EINVAL;
538         }
539         path_put(&path);
540         putname(name);
541         if (retry_estale(error, lookup_flags)) {
542                 lookup_flags |= LOOKUP_REVAL;
543                 goto retry;
544         }
545         return error;
546 }
547
548 SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
549                 char __user *, buf, int, bufsiz)
550 {
551         return do_readlinkat(dfd, pathname, buf, bufsiz);
552 }
553
554 SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
555                 int, bufsiz)
556 {
557         return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
558 }
559
560
561 /* ---------- LFS-64 ----------- */
562 #if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
563
564 #ifndef INIT_STRUCT_STAT64_PADDING
565 #  define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
566 #endif
567
568 static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
569 {
570         struct stat64 tmp;
571
572         INIT_STRUCT_STAT64_PADDING(tmp);
573 #ifdef CONFIG_MIPS
574         /* mips has weird padding, so we don't get 64 bits there */
575         tmp.st_dev = new_encode_dev(stat->dev);
576         tmp.st_rdev = new_encode_dev(stat->rdev);
577 #else
578         tmp.st_dev = huge_encode_dev(stat->dev);
579         tmp.st_rdev = huge_encode_dev(stat->rdev);
580 #endif
581         tmp.st_ino = stat->ino;
582         if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
583                 return -EOVERFLOW;
584 #ifdef STAT64_HAS_BROKEN_ST_INO
585         tmp.__st_ino = stat->ino;
586 #endif
587         tmp.st_mode = stat->mode;
588         tmp.st_nlink = stat->nlink;
589         tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
590         tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
591         tmp.st_atime = stat->atime.tv_sec;
592         tmp.st_atime_nsec = stat->atime.tv_nsec;
593         tmp.st_mtime = stat->mtime.tv_sec;
594         tmp.st_mtime_nsec = stat->mtime.tv_nsec;
595         tmp.st_ctime = stat->ctime.tv_sec;
596         tmp.st_ctime_nsec = stat->ctime.tv_nsec;
597         tmp.st_size = stat->size;
598         tmp.st_blocks = stat->blocks;
599         tmp.st_blksize = stat->blksize;
600         return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
601 }
602
603 SYSCALL_DEFINE2(stat64, const char __user *, filename,
604                 struct stat64 __user *, statbuf)
605 {
606         struct kstat stat;
607         int error = vfs_stat(filename, &stat);
608
609         if (!error)
610                 error = cp_new_stat64(&stat, statbuf);
611
612         return error;
613 }
614
615 SYSCALL_DEFINE2(lstat64, const char __user *, filename,
616                 struct stat64 __user *, statbuf)
617 {
618         struct kstat stat;
619         int error = vfs_lstat(filename, &stat);
620
621         if (!error)
622                 error = cp_new_stat64(&stat, statbuf);
623
624         return error;
625 }
626
627 SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
628 {
629         struct kstat stat;
630         int error = vfs_fstat(fd, &stat);
631
632         if (!error)
633                 error = cp_new_stat64(&stat, statbuf);
634
635         return error;
636 }
637
638 SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
639                 struct stat64 __user *, statbuf, int, flag)
640 {
641         struct kstat stat;
642         int error;
643
644         error = vfs_fstatat(dfd, filename, &stat, flag);
645         if (error)
646                 return error;
647         return cp_new_stat64(&stat, statbuf);
648 }
649 #endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
650
651 static noinline_for_stack int
652 cp_statx(const struct kstat *stat, struct statx __user *buffer)
653 {
654         struct statx tmp;
655
656         memset(&tmp, 0, sizeof(tmp));
657
658         /* STATX_CHANGE_COOKIE is kernel-only for now */
659         tmp.stx_mask = stat->result_mask & ~STATX_CHANGE_COOKIE;
660         tmp.stx_blksize = stat->blksize;
661         /* STATX_ATTR_CHANGE_MONOTONIC is kernel-only for now */
662         tmp.stx_attributes = stat->attributes & ~STATX_ATTR_CHANGE_MONOTONIC;
663         tmp.stx_nlink = stat->nlink;
664         tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
665         tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
666         tmp.stx_mode = stat->mode;
667         tmp.stx_ino = stat->ino;
668         tmp.stx_size = stat->size;
669         tmp.stx_blocks = stat->blocks;
670         tmp.stx_attributes_mask = stat->attributes_mask;
671         tmp.stx_atime.tv_sec = stat->atime.tv_sec;
672         tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
673         tmp.stx_btime.tv_sec = stat->btime.tv_sec;
674         tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
675         tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
676         tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
677         tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
678         tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
679         tmp.stx_rdev_major = MAJOR(stat->rdev);
680         tmp.stx_rdev_minor = MINOR(stat->rdev);
681         tmp.stx_dev_major = MAJOR(stat->dev);
682         tmp.stx_dev_minor = MINOR(stat->dev);
683         tmp.stx_mnt_id = stat->mnt_id;
684         tmp.stx_dio_mem_align = stat->dio_mem_align;
685         tmp.stx_dio_offset_align = stat->dio_offset_align;
686         tmp.stx_subvol = stat->subvol;
687         tmp.stx_atomic_write_unit_min = stat->atomic_write_unit_min;
688         tmp.stx_atomic_write_unit_max = stat->atomic_write_unit_max;
689         tmp.stx_atomic_write_segments_max = stat->atomic_write_segments_max;
690
691         return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
692 }
693
694 int do_statx(int dfd, struct filename *filename, unsigned int flags,
695              unsigned int mask, struct statx __user *buffer)
696 {
697         struct kstat stat;
698         int error;
699
700         if (mask & STATX__RESERVED)
701                 return -EINVAL;
702         if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
703                 return -EINVAL;
704
705         /*
706          * STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
707          * from userland.
708          */
709         mask &= ~STATX_CHANGE_COOKIE;
710
711         error = vfs_statx(dfd, filename, flags, &stat, mask);
712         if (error)
713                 return error;
714
715         return cp_statx(&stat, buffer);
716 }
717
718 int do_statx_fd(int fd, unsigned int flags, unsigned int mask,
719              struct statx __user *buffer)
720 {
721         struct kstat stat;
722         int error;
723
724         if (mask & STATX__RESERVED)
725                 return -EINVAL;
726         if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
727                 return -EINVAL;
728
729         /*
730          * STATX_CHANGE_COOKIE is kernel-only for now. Ignore requests
731          * from userland.
732          */
733         mask &= ~STATX_CHANGE_COOKIE;
734
735         error = vfs_statx_fd(fd, flags, &stat, mask);
736         if (error)
737                 return error;
738
739         return cp_statx(&stat, buffer);
740 }
741
742 /**
743  * sys_statx - System call to get enhanced stats
744  * @dfd: Base directory to pathwalk from *or* fd to stat.
745  * @filename: File to stat or either NULL or "" with AT_EMPTY_PATH
746  * @flags: AT_* flags to control pathwalk.
747  * @mask: Parts of statx struct actually required.
748  * @buffer: Result buffer.
749  *
750  * Note that fstat() can be emulated by setting dfd to the fd of interest,
751  * supplying "" (or preferably NULL) as the filename and setting AT_EMPTY_PATH
752  * in the flags.
753  */
754 SYSCALL_DEFINE5(statx,
755                 int, dfd, const char __user *, filename, unsigned, flags,
756                 unsigned int, mask,
757                 struct statx __user *, buffer)
758 {
759         int ret;
760         struct filename *name = getname_maybe_null(filename, flags);
761
762         if (!name && dfd >= 0)
763                 return do_statx_fd(dfd, flags & ~AT_NO_AUTOMOUNT, mask, buffer);
764
765         ret = do_statx(dfd, name, flags, mask, buffer);
766         putname(name);
767
768         return ret;
769 }
770
771 #if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
772 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
773 {
774         struct compat_stat tmp;
775
776         if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
777                 return -EOVERFLOW;
778         if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
779                 return -EOVERFLOW;
780
781         memset(&tmp, 0, sizeof(tmp));
782         tmp.st_dev = new_encode_dev(stat->dev);
783         tmp.st_ino = stat->ino;
784         if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
785                 return -EOVERFLOW;
786         tmp.st_mode = stat->mode;
787         tmp.st_nlink = stat->nlink;
788         if (tmp.st_nlink != stat->nlink)
789                 return -EOVERFLOW;
790         SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
791         SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
792         tmp.st_rdev = new_encode_dev(stat->rdev);
793         if ((u64) stat->size > MAX_NON_LFS)
794                 return -EOVERFLOW;
795         tmp.st_size = stat->size;
796         tmp.st_atime = stat->atime.tv_sec;
797         tmp.st_atime_nsec = stat->atime.tv_nsec;
798         tmp.st_mtime = stat->mtime.tv_sec;
799         tmp.st_mtime_nsec = stat->mtime.tv_nsec;
800         tmp.st_ctime = stat->ctime.tv_sec;
801         tmp.st_ctime_nsec = stat->ctime.tv_nsec;
802         tmp.st_blocks = stat->blocks;
803         tmp.st_blksize = stat->blksize;
804         return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
805 }
806
807 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
808                        struct compat_stat __user *, statbuf)
809 {
810         struct kstat stat;
811         int error;
812
813         error = vfs_stat(filename, &stat);
814         if (error)
815                 return error;
816         return cp_compat_stat(&stat, statbuf);
817 }
818
819 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
820                        struct compat_stat __user *, statbuf)
821 {
822         struct kstat stat;
823         int error;
824
825         error = vfs_lstat(filename, &stat);
826         if (error)
827                 return error;
828         return cp_compat_stat(&stat, statbuf);
829 }
830
831 #ifndef __ARCH_WANT_STAT64
832 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
833                        const char __user *, filename,
834                        struct compat_stat __user *, statbuf, int, flag)
835 {
836         struct kstat stat;
837         int error;
838
839         error = vfs_fstatat(dfd, filename, &stat, flag);
840         if (error)
841                 return error;
842         return cp_compat_stat(&stat, statbuf);
843 }
844 #endif
845
846 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
847                        struct compat_stat __user *, statbuf)
848 {
849         struct kstat stat;
850         int error = vfs_fstat(fd, &stat);
851
852         if (!error)
853                 error = cp_compat_stat(&stat, statbuf);
854         return error;
855 }
856 #endif
857
858 /* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
859 void __inode_add_bytes(struct inode *inode, loff_t bytes)
860 {
861         inode->i_blocks += bytes >> 9;
862         bytes &= 511;
863         inode->i_bytes += bytes;
864         if (inode->i_bytes >= 512) {
865                 inode->i_blocks++;
866                 inode->i_bytes -= 512;
867         }
868 }
869 EXPORT_SYMBOL(__inode_add_bytes);
870
871 void inode_add_bytes(struct inode *inode, loff_t bytes)
872 {
873         spin_lock(&inode->i_lock);
874         __inode_add_bytes(inode, bytes);
875         spin_unlock(&inode->i_lock);
876 }
877
878 EXPORT_SYMBOL(inode_add_bytes);
879
880 void __inode_sub_bytes(struct inode *inode, loff_t bytes)
881 {
882         inode->i_blocks -= bytes >> 9;
883         bytes &= 511;
884         if (inode->i_bytes < bytes) {
885                 inode->i_blocks--;
886                 inode->i_bytes += 512;
887         }
888         inode->i_bytes -= bytes;
889 }
890
891 EXPORT_SYMBOL(__inode_sub_bytes);
892
893 void inode_sub_bytes(struct inode *inode, loff_t bytes)
894 {
895         spin_lock(&inode->i_lock);
896         __inode_sub_bytes(inode, bytes);
897         spin_unlock(&inode->i_lock);
898 }
899
900 EXPORT_SYMBOL(inode_sub_bytes);
901
902 loff_t inode_get_bytes(struct inode *inode)
903 {
904         loff_t ret;
905
906         spin_lock(&inode->i_lock);
907         ret = __inode_get_bytes(inode);
908         spin_unlock(&inode->i_lock);
909         return ret;
910 }
911
912 EXPORT_SYMBOL(inode_get_bytes);
913
914 void inode_set_bytes(struct inode *inode, loff_t bytes)
915 {
916         /* Caller is here responsible for sufficient locking
917          * (ie. inode->i_lock) */
918         inode->i_blocks = bytes >> 9;
919         inode->i_bytes = bytes & 511;
920 }
921
922 EXPORT_SYMBOL(inode_set_bytes);
This page took 0.090899 seconds and 4 git commands to generate.