blob: ef50573c72a2692908d5ad8bcd68e9a09e501c78 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/stat.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
Eric Biggers2d985f82022-08-26 23:58:45 -07008#include <linux/blkdev.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/mm.h>
11#include <linux/errno.h>
12#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/highuid.h>
14#include <linux/fs.h>
15#include <linux/namei.h>
16#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010017#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/syscalls.h>
Theodore Ts'oba52de12006-09-27 01:50:49 -070019#include <linux/pagemap.h>
Al Viroac565de2017-04-08 18:13:00 -040020#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/unistd.h>
24
Jens Axboe3934e362019-12-14 13:26:33 -070025#include "internal.h"
Miklos Szeredifa2fcf42020-05-14 16:44:24 +020026#include "mount.h"
Jens Axboe3934e362019-12-14 13:26:33 -070027
David Howellsa528d352017-01-31 16:46:22 +000028/**
29 * generic_fillattr - Fill in the basic attributes from the inode struct
Christian Brauner0d56a452021-01-21 14:19:30 +010030 * @mnt_userns: user namespace of the mount the inode was found from
31 * @inode: Inode to use as the source
32 * @stat: Where to fill in the attributes
David Howellsa528d352017-01-31 16:46:22 +000033 *
34 * Fill in the basic attributes in the kstat structure from data that's to be
35 * found on the VFS inode structure. This is the default if no getattr inode
36 * operation is supplied.
Christian Brauner0d56a452021-01-21 14:19:30 +010037 *
38 * If the inode has been found through an idmapped mount the user namespace of
39 * the vfsmount must be passed through @mnt_userns. This function will then
40 * take care to map the inode according to @mnt_userns before filling in the
41 * uid and gid filds. On non-idmapped mounts or if permission checking is to be
42 * performed on the raw inode simply passs init_user_ns.
David Howellsa528d352017-01-31 16:46:22 +000043 */
Christian Brauner0d56a452021-01-21 14:19:30 +010044void generic_fillattr(struct user_namespace *mnt_userns, struct inode *inode,
45 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 stat->dev = inode->i_sb->s_dev;
48 stat->ino = inode->i_ino;
49 stat->mode = inode->i_mode;
50 stat->nlink = inode->i_nlink;
Christian Brauner0d56a452021-01-21 14:19:30 +010051 stat->uid = i_uid_into_mnt(mnt_userns, inode);
52 stat->gid = i_gid_into_mnt(mnt_userns, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 stat->rdev = inode->i_rdev;
Linus Torvalds3ddcd052011-08-06 22:45:50 -070054 stat->size = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 stat->atime = inode->i_atime;
56 stat->mtime = inode->i_mtime;
57 stat->ctime = inode->i_ctime;
Fabian Frederick93407472017-02-27 14:28:32 -080058 stat->blksize = i_blocksize(inode);
Linus Torvalds3ddcd052011-08-06 22:45:50 -070059 stat->blocks = inode->i_blocks;
David Howellsa528d352017-01-31 16:46:22 +000060}
Linus Torvalds1da177e2005-04-16 15:20:36 -070061EXPORT_SYMBOL(generic_fillattr);
62
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040063/**
Amir Goldstein4f911132021-06-19 12:26:16 +030064 * generic_fill_statx_attr - Fill in the statx attributes from the inode flags
65 * @inode: Inode to use as the source
66 * @stat: Where to fill in the attribute flags
67 *
68 * Fill in the STATX_ATTR_* flags in the kstat structure for properties of the
69 * inode that are published on i_flags and enforced by the VFS.
70 */
71void generic_fill_statx_attr(struct inode *inode, struct kstat *stat)
72{
73 if (inode->i_flags & S_IMMUTABLE)
74 stat->attributes |= STATX_ATTR_IMMUTABLE;
75 if (inode->i_flags & S_APPEND)
76 stat->attributes |= STATX_ATTR_APPEND;
77 stat->attributes_mask |= KSTAT_ATTR_VFS_FLAGS;
78}
79EXPORT_SYMBOL(generic_fill_statx_attr);
80
81/**
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040082 * vfs_getattr_nosec - getattr without security checks
83 * @path: file to get attributes from
84 * @stat: structure to return attributes in
David Howellsa528d352017-01-31 16:46:22 +000085 * @request_mask: STATX_xxx flags indicating what the caller wants
Christoph Hellwigf2d077f2020-09-26 09:04:01 +020086 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040087 *
88 * Get attributes without calling security_inode_getattr.
89 *
90 * Currently the only caller other than vfs_getattr is internal to the
David Howellsa528d352017-01-31 16:46:22 +000091 * filehandle lookup code, which uses only the inode number and returns no
92 * attributes to any user. Any other code probably wants vfs_getattr.
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -040093 */
David Howellsa528d352017-01-31 16:46:22 +000094int vfs_getattr_nosec(const struct path *path, struct kstat *stat,
95 u32 request_mask, unsigned int query_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Christian Brauner549c7292021-01-21 14:19:43 +010097 struct user_namespace *mnt_userns;
David Howellsbb6687342015-03-17 22:26:21 +000098 struct inode *inode = d_backing_inode(path->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Howellsa528d352017-01-31 16:46:22 +0000100 memset(stat, 0, sizeof(*stat));
101 stat->result_mask |= STATX_BASIC_STATS;
Christoph Hellwigf2d077f2020-09-26 09:04:01 +0200102 query_flags &= AT_STATX_SYNC_TYPE;
Christoph Hellwig801e5232019-01-21 16:23:26 +0100103
104 /* allow the fs to override these if it really wants to */
Miklos Szeredi761e28f2020-05-14 16:44:24 +0200105 /* SB_NOATIME means filesystem supplies dummy atime value */
106 if (inode->i_sb->s_flags & SB_NOATIME)
Christoph Hellwig801e5232019-01-21 16:23:26 +0100107 stat->result_mask &= ~STATX_ATIME;
Theodore Ts'o5afa7e82021-04-17 23:03:50 -0400108
109 /*
110 * Note: If you add another clause to set an attribute flag, please
111 * update attributes_mask below.
112 */
Christoph Hellwig801e5232019-01-21 16:23:26 +0100113 if (IS_AUTOMOUNT(inode))
114 stat->attributes |= STATX_ATTR_AUTOMOUNT;
115
Ira Weiny712b2692020-04-30 07:41:34 -0700116 if (IS_DAX(inode))
117 stat->attributes |= STATX_ATTR_DAX;
118
Theodore Ts'o5afa7e82021-04-17 23:03:50 -0400119 stat->attributes_mask |= (STATX_ATTR_AUTOMOUNT |
120 STATX_ATTR_DAX);
121
Christian Brauner549c7292021-01-21 14:19:43 +0100122 mnt_userns = mnt_user_ns(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (inode->i_op->getattr)
Christian Brauner549c7292021-01-21 14:19:43 +0100124 return inode->i_op->getattr(mnt_userns, path, stat,
125 request_mask, query_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christian Brauner549c7292021-01-21 14:19:43 +0100127 generic_fillattr(mnt_userns, inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129}
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400130EXPORT_SYMBOL(vfs_getattr_nosec);
131
David Howellsa528d352017-01-31 16:46:22 +0000132/*
133 * vfs_getattr - Get the enhanced basic attributes of a file
134 * @path: The file of interest
135 * @stat: Where to return the statistics
136 * @request_mask: STATX_xxx flags indicating what the caller wants
Christoph Hellwigf2d077f2020-09-26 09:04:01 +0200137 * @query_flags: Query mode (AT_STATX_SYNC_TYPE)
David Howellsa528d352017-01-31 16:46:22 +0000138 *
139 * Ask the filesystem for a file's attributes. The caller must indicate in
140 * request_mask and query_flags to indicate what they want.
141 *
142 * If the file is remote, the filesystem can be forced to update the attributes
143 * from the backing store by passing AT_STATX_FORCE_SYNC in query_flags or can
144 * suppress the update by passing AT_STATX_DONT_SYNC.
145 *
146 * Bits must have been set in request_mask to indicate which attributes the
147 * caller wants retrieving. Any such attribute not requested may be returned
148 * anyway, but the value may be approximate, and, if remote, may not have been
149 * synchronised with the server.
150 *
151 * 0 will be returned on success, and a -ve error code if unsuccessful.
152 */
153int vfs_getattr(const struct path *path, struct kstat *stat,
154 u32 request_mask, unsigned int query_flags)
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400155{
156 int retval;
157
Al Viro3f7036a2015-03-08 19:28:30 -0400158 retval = security_inode_getattr(path);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400159 if (retval)
160 return retval;
David Howellsa528d352017-01-31 16:46:22 +0000161 return vfs_getattr_nosec(path, stat, request_mask, query_flags);
J. Bruce Fieldsb7a6ec52013-10-02 17:01:18 -0400162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163EXPORT_SYMBOL(vfs_getattr);
164
David Howellsa528d352017-01-31 16:46:22 +0000165/**
Christoph Hellwigda9aa5d2020-09-26 09:03:57 +0200166 * vfs_fstat - Get the basic attributes by file descriptor
David Howellsa528d352017-01-31 16:46:22 +0000167 * @fd: The file descriptor referring to the file of interest
168 * @stat: The result structure to fill in.
David Howellsa528d352017-01-31 16:46:22 +0000169 *
170 * This function is a wrapper around vfs_getattr(). The main difference is
171 * that it uses a file descriptor to determine the file location.
172 *
173 * 0 will be returned on success, and a -ve error code if unsuccessful.
174 */
Christoph Hellwigda9aa5d2020-09-26 09:03:57 +0200175int vfs_fstat(int fd, struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Eric Biggers8c7493a2017-03-31 18:31:32 +0100177 struct fd f;
Christoph Hellwigda9aa5d2020-09-26 09:03:57 +0200178 int error;
Eric Biggers8c7493a2017-03-31 18:31:32 +0100179
180 f = fdget_raw(fd);
Christoph Hellwigda9aa5d2020-09-26 09:03:57 +0200181 if (!f.file)
182 return -EBADF;
183 error = vfs_getattr(&f.file->f_path, stat, STATX_BASIC_STATS, 0);
184 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return error;
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800188int getname_statx_lookup_flags(int flags)
189{
190 int lookup_flags = 0;
191
192 if (!(flags & AT_SYMLINK_NOFOLLOW))
193 lookup_flags |= LOOKUP_FOLLOW;
194 if (!(flags & AT_NO_AUTOMOUNT))
195 lookup_flags |= LOOKUP_AUTOMOUNT;
196 if (flags & AT_EMPTY_PATH)
197 lookup_flags |= LOOKUP_EMPTY;
198
199 return lookup_flags;
200}
201
David Howellsa528d352017-01-31 16:46:22 +0000202/**
203 * vfs_statx - Get basic and extra attributes by filename
204 * @dfd: A file descriptor representing the base dir for a relative filename
205 * @filename: The name of the file of interest
206 * @flags: Flags to control the query
207 * @stat: The result structure to fill in.
208 * @request_mask: STATX_xxx flags indicating what the caller wants
209 *
210 * This function is a wrapper around vfs_getattr(). The main difference is
211 * that it uses a filename and base directory to determine the file location.
212 * Additionally, the use of AT_SYMLINK_NOFOLLOW in flags will prevent a symlink
213 * at the given name from being referenced.
214 *
David Howellsa528d352017-01-31 16:46:22 +0000215 * 0 will be returned on success, and a -ve error code if unsuccessful.
216 */
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800217static int vfs_statx(int dfd, struct filename *filename, int flags,
David Howellsa528d352017-01-31 16:46:22 +0000218 struct kstat *stat, u32 request_mask)
Oleg Drokin0112fc22009-04-08 20:05:42 +0400219{
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400220 struct path path;
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800221 unsigned int lookup_flags = getname_statx_lookup_flags(flags);
Christoph Hellwigb3f05152020-09-26 09:04:00 +0200222 int error;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400223
Christoph Hellwigb3f05152020-09-26 09:04:00 +0200224 if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
Christoph Hellwigf2d077f2020-09-26 09:04:01 +0200225 AT_STATX_SYNC_TYPE))
David Howellsa528d352017-01-31 16:46:22 +0000226 return -EINVAL;
Christoph Hellwigb3f05152020-09-26 09:04:00 +0200227
Jeff Layton836fb7e2012-12-11 12:10:05 -0500228retry:
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800229 error = filename_lookup(dfd, filename, lookup_flags, &path, NULL);
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400230 if (error)
231 goto out;
232
David Howellsa528d352017-01-31 16:46:22 +0000233 error = vfs_getattr(&path, stat, request_mask, flags);
Eric Biggers2d985f82022-08-26 23:58:45 -0700234
Miklos Szeredifa2fcf42020-05-14 16:44:24 +0200235 stat->mnt_id = real_mount(path.mnt)->mnt_id;
236 stat->result_mask |= STATX_MNT_ID;
Eric Biggers2d985f82022-08-26 23:58:45 -0700237
Miklos Szeredi80340fe2020-05-14 16:44:24 +0200238 if (path.mnt->mnt_root == path.dentry)
239 stat->attributes |= STATX_ATTR_MOUNT_ROOT;
240 stat->attributes_mask |= STATX_ATTR_MOUNT_ROOT;
Eric Biggers2d985f82022-08-26 23:58:45 -0700241
242 /* Handle STATX_DIOALIGN for block devices. */
243 if (request_mask & STATX_DIOALIGN) {
244 struct inode *inode = d_backing_inode(path.dentry);
245
246 if (S_ISBLK(inode->i_mode))
247 bdev_statx_dioalign(inode, stat);
248 }
249
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400250 path_put(&path);
Jeff Layton836fb7e2012-12-11 12:10:05 -0500251 if (retry_estale(error, lookup_flags)) {
252 lookup_flags |= LOOKUP_REVAL;
253 goto retry;
254 }
Oleg Drokin0112fc22009-04-08 20:05:42 +0400255out:
256 return error;
257}
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400258
Christoph Hellwig09f1bde2020-09-26 09:03:59 +0200259int vfs_fstatat(int dfd, const char __user *filename,
260 struct kstat *stat, int flags)
261{
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800262 int ret;
263 int statx_flags = flags | AT_NO_AUTOMOUNT;
264 struct filename *name;
265
266 name = getname_flags(filename, getname_statx_lookup_flags(statx_flags), NULL);
267 ret = vfs_statx(dfd, name, statx_flags, stat, STATX_BASIC_STATS);
268 putname(name);
269
270 return ret;
Christoph Hellwig09f1bde2020-09-26 09:03:59 +0200271}
Oleg Drokin0112fc22009-04-08 20:05:42 +0400272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#ifdef __ARCH_WANT_OLD_STAT
274
275/*
276 * For backward compatibility? Maybe this should be moved
277 * into arch/i386 instead?
278 */
279static int cp_old_stat(struct kstat *stat, struct __old_kernel_stat __user * statbuf)
280{
281 static int warncount = 5;
282 struct __old_kernel_stat tmp;
David Howellsa528d352017-01-31 16:46:22 +0000283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (warncount > 0) {
285 warncount--;
286 printk(KERN_WARNING "VFS: Warning: %s using old stat() call. Recompile your binary.\n",
287 current->comm);
288 } else if (warncount < 0) {
289 /* it's laughable, but... */
290 warncount = 0;
291 }
292
293 memset(&tmp, 0, sizeof(struct __old_kernel_stat));
294 tmp.st_dev = old_encode_dev(stat->dev);
295 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700296 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
297 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 tmp.st_mode = stat->mode;
299 tmp.st_nlink = stat->nlink;
300 if (tmp.st_nlink != stat->nlink)
301 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800302 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
303 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 tmp.st_rdev = old_encode_dev(stat->rdev);
305#if BITS_PER_LONG == 32
306 if (stat->size > MAX_NON_LFS)
307 return -EOVERFLOW;
David Howellsa528d352017-01-31 16:46:22 +0000308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 tmp.st_size = stat->size;
310 tmp.st_atime = stat->atime.tv_sec;
311 tmp.st_mtime = stat->mtime.tv_sec;
312 tmp.st_ctime = stat->ctime.tv_sec;
313 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
314}
315
David Howellsc7887322010-08-11 11:26:22 +0100316SYSCALL_DEFINE2(stat, const char __user *, filename,
317 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400320 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400322 error = vfs_stat(filename, &stat);
323 if (error)
324 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400326 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
Heiko Carstens257ac262009-01-14 14:14:13 +0100328
David Howellsc7887322010-08-11 11:26:22 +0100329SYSCALL_DEFINE2(lstat, const char __user *, filename,
330 struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400333 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400335 error = vfs_lstat(filename, &stat);
336 if (error)
337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400339 return cp_old_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
Heiko Carstens257ac262009-01-14 14:14:13 +0100341
342SYSCALL_DEFINE2(fstat, unsigned int, fd, struct __old_kernel_stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct kstat stat;
345 int error = vfs_fstat(fd, &stat);
346
347 if (!error)
348 error = cp_old_stat(&stat, statbuf);
349
350 return error;
351}
352
353#endif /* __ARCH_WANT_OLD_STAT */
354
Arnd Bergmann82b355d2018-04-13 11:50:12 +0200355#ifdef __ARCH_WANT_NEW_STAT
356
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700357#if BITS_PER_LONG == 32
358# define choose_32_64(a,b) a
359#else
360# define choose_32_64(a,b) b
361#endif
362
Linus Torvalds8529f612012-05-06 18:02:40 -0700363#ifndef INIT_STRUCT_STAT_PADDING
364# define INIT_STRUCT_STAT_PADDING(st) memset(&st, 0, sizeof(st))
365#endif
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int cp_new_stat(struct kstat *stat, struct stat __user *statbuf)
368{
369 struct stat tmp;
370
Mikulas Patocka932aba12022-04-12 05:41:00 -0400371 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
372 return -EOVERFLOW;
373 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -EOVERFLOW;
Linus Torvaldsa52dd972012-05-06 17:47:30 -0700375#if BITS_PER_LONG == 32
376 if (stat->size > MAX_NON_LFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EOVERFLOW;
378#endif
379
Linus Torvalds8529f612012-05-06 18:02:40 -0700380 INIT_STRUCT_STAT_PADDING(tmp);
Mikulas Patocka932aba12022-04-12 05:41:00 -0400381 tmp.st_dev = new_encode_dev(stat->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700383 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
384 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 tmp.st_mode = stat->mode;
386 tmp.st_nlink = stat->nlink;
387 if (tmp.st_nlink != stat->nlink)
388 return -EOVERFLOW;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800389 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
390 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Mikulas Patocka932aba12022-04-12 05:41:00 -0400391 tmp.st_rdev = new_encode_dev(stat->rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 tmp.st_size = stat->size;
393 tmp.st_atime = stat->atime.tv_sec;
394 tmp.st_mtime = stat->mtime.tv_sec;
395 tmp.st_ctime = stat->ctime.tv_sec;
396#ifdef STAT_HAVE_NSEC
397 tmp.st_atime_nsec = stat->atime.tv_nsec;
398 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
399 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
400#endif
401 tmp.st_blocks = stat->blocks;
402 tmp.st_blksize = stat->blksize;
403 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
404}
405
David Howellsc7887322010-08-11 11:26:22 +0100406SYSCALL_DEFINE2(newstat, const char __user *, filename,
407 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400410 int error = vfs_stat(filename, &stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400412 if (error)
413 return error;
414 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800416
David Howellsc7887322010-08-11 11:26:22 +0100417SYSCALL_DEFINE2(newlstat, const char __user *, filename,
418 struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct kstat stat;
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400421 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400423 error = vfs_lstat(filename, &stat);
424 if (error)
425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Christoph Hellwig2eae7a12009-04-08 16:34:03 -0400427 return cp_new_stat(&stat, statbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800429
Andreas Schwab2833c282006-04-27 15:46:42 +0200430#if !defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_SYS_NEWFSTATAT)
David Howellsc7887322010-08-11 11:26:22 +0100431SYSCALL_DEFINE4(newfstatat, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100432 struct stat __user *, statbuf, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800433{
434 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400435 int error;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800436
Oleg Drokin0112fc22009-04-08 20:05:42 +0400437 error = vfs_fstatat(dfd, filename, &stat, flag);
438 if (error)
439 return error;
440 return cp_new_stat(&stat, statbuf);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800441}
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800442#endif
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800443
Heiko Carstens257ac262009-01-14 14:14:13 +0100444SYSCALL_DEFINE2(newfstat, unsigned int, fd, struct stat __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct kstat stat;
447 int error = vfs_fstat(fd, &stat);
448
449 if (!error)
450 error = cp_new_stat(&stat, statbuf);
451
452 return error;
453}
Arnd Bergmann82b355d2018-04-13 11:50:12 +0200454#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100456static int do_readlinkat(int dfd, const char __user *pathname,
457 char __user *buf, int bufsiz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Al Viro2d8f3032008-07-22 09:59:21 -0400459 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int error;
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100461 int empty = 0;
Jeff Layton79551192012-12-11 12:10:06 -0500462 unsigned int lookup_flags = LOOKUP_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (bufsiz <= 0)
465 return -EINVAL;
466
Jeff Layton79551192012-12-11 12:10:06 -0500467retry:
468 error = user_path_at_empty(dfd, pathname, lookup_flags, &path, &empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!error) {
David Howellsbb6687342015-03-17 22:26:21 +0000470 struct inode *inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +0100472 error = empty ? -ENOENT : -EINVAL;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100473 /*
474 * AFS mountpoints allow readlink(2) but are not symlinks
475 */
476 if (d_is_symlink(path.dentry) || inode->i_op->readlink) {
Al Viro2d8f3032008-07-22 09:59:21 -0400477 error = security_inode_readlink(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!error) {
Al Viro68ac1232012-03-15 08:21:57 -0400479 touch_atime(&path);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +0100480 error = vfs_readlink(path.dentry, buf, bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
Al Viro2d8f3032008-07-22 09:59:21 -0400483 path_put(&path);
Jeff Layton79551192012-12-11 12:10:06 -0500484 if (retry_estale(error, lookup_flags)) {
485 lookup_flags |= LOOKUP_REVAL;
486 goto retry;
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 return error;
490}
491
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100492SYSCALL_DEFINE4(readlinkat, int, dfd, const char __user *, pathname,
493 char __user *, buf, int, bufsiz)
494{
495 return do_readlinkat(dfd, pathname, buf, bufsiz);
496}
497
Heiko Carstens002c8972009-01-14 14:14:18 +0100498SYSCALL_DEFINE3(readlink, const char __user *, path, char __user *, buf,
499 int, bufsiz)
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800500{
Dominik Brodowski2dae0242018-03-11 11:34:27 +0100501 return do_readlinkat(AT_FDCWD, path, buf, bufsiz);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505/* ---------- LFS-64 ----------- */
Catalin Marinas0753f702012-03-19 15:13:51 +0000506#if defined(__ARCH_WANT_STAT64) || defined(__ARCH_WANT_COMPAT_STAT64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds8529f612012-05-06 18:02:40 -0700508#ifndef INIT_STRUCT_STAT64_PADDING
509# define INIT_STRUCT_STAT64_PADDING(st) memset(&st, 0, sizeof(st))
510#endif
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static long cp_new_stat64(struct kstat *stat, struct stat64 __user *statbuf)
513{
514 struct stat64 tmp;
515
Linus Torvalds8529f612012-05-06 18:02:40 -0700516 INIT_STRUCT_STAT64_PADDING(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#ifdef CONFIG_MIPS
518 /* mips has weird padding, so we don't get 64 bits there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 tmp.st_dev = new_encode_dev(stat->dev);
520 tmp.st_rdev = new_encode_dev(stat->rdev);
521#else
522 tmp.st_dev = huge_encode_dev(stat->dev);
523 tmp.st_rdev = huge_encode_dev(stat->rdev);
524#endif
525 tmp.st_ino = stat->ino;
David Howellsafefdbb2006-10-03 01:13:46 -0700526 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
527 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#ifdef STAT64_HAS_BROKEN_ST_INO
529 tmp.__st_ino = stat->ino;
530#endif
531 tmp.st_mode = stat->mode;
532 tmp.st_nlink = stat->nlink;
Eric W. Biedermana7c19382012-02-09 09:10:30 -0800533 tmp.st_uid = from_kuid_munged(current_user_ns(), stat->uid);
534 tmp.st_gid = from_kgid_munged(current_user_ns(), stat->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 tmp.st_atime = stat->atime.tv_sec;
536 tmp.st_atime_nsec = stat->atime.tv_nsec;
537 tmp.st_mtime = stat->mtime.tv_sec;
538 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
539 tmp.st_ctime = stat->ctime.tv_sec;
540 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
541 tmp.st_size = stat->size;
542 tmp.st_blocks = stat->blocks;
543 tmp.st_blksize = stat->blksize;
544 return copy_to_user(statbuf,&tmp,sizeof(tmp)) ? -EFAULT : 0;
545}
546
David Howellsc7887322010-08-11 11:26:22 +0100547SYSCALL_DEFINE2(stat64, const char __user *, filename,
548 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct kstat stat;
551 int error = vfs_stat(filename, &stat);
552
553 if (!error)
554 error = cp_new_stat64(&stat, statbuf);
555
556 return error;
557}
Heiko Carstens257ac262009-01-14 14:14:13 +0100558
David Howellsc7887322010-08-11 11:26:22 +0100559SYSCALL_DEFINE2(lstat64, const char __user *, filename,
560 struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct kstat stat;
563 int error = vfs_lstat(filename, &stat);
564
565 if (!error)
566 error = cp_new_stat64(&stat, statbuf);
567
568 return error;
569}
Heiko Carstens257ac262009-01-14 14:14:13 +0100570
571SYSCALL_DEFINE2(fstat64, unsigned long, fd, struct stat64 __user *, statbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 struct kstat stat;
574 int error = vfs_fstat(fd, &stat);
575
576 if (!error)
577 error = cp_new_stat64(&stat, statbuf);
578
579 return error;
580}
581
David Howellsc7887322010-08-11 11:26:22 +0100582SYSCALL_DEFINE4(fstatat64, int, dfd, const char __user *, filename,
Heiko Carstens6559eed82009-01-14 14:14:32 +0100583 struct stat64 __user *, statbuf, int, flag)
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800584{
585 struct kstat stat;
Oleg Drokin0112fc22009-04-08 20:05:42 +0400586 int error;
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800587
Oleg Drokin0112fc22009-04-08 20:05:42 +0400588 error = vfs_fstatat(dfd, filename, &stat, flag);
589 if (error)
590 return error;
591 return cp_new_stat64(&stat, statbuf);
Ulrich Dreppercff2b762006-02-11 17:55:47 -0800592}
Catalin Marinas0753f702012-03-19 15:13:51 +0000593#endif /* __ARCH_WANT_STAT64 || __ARCH_WANT_COMPAT_STAT64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Bijan Mottahedeh6f88cc12020-05-22 21:31:19 -0700595static noinline_for_stack int
Eric Biggers64bd7202017-03-31 18:31:48 +0100596cp_statx(const struct kstat *stat, struct statx __user *buffer)
David Howellsa528d352017-01-31 16:46:22 +0000597{
Eric Biggers64bd7202017-03-31 18:31:48 +0100598 struct statx tmp;
David Howellsa528d352017-01-31 16:46:22 +0000599
Eric Biggers64bd7202017-03-31 18:31:48 +0100600 memset(&tmp, 0, sizeof(tmp));
David Howellsa528d352017-01-31 16:46:22 +0000601
Eric Biggers64bd7202017-03-31 18:31:48 +0100602 tmp.stx_mask = stat->result_mask;
603 tmp.stx_blksize = stat->blksize;
604 tmp.stx_attributes = stat->attributes;
605 tmp.stx_nlink = stat->nlink;
606 tmp.stx_uid = from_kuid_munged(current_user_ns(), stat->uid);
607 tmp.stx_gid = from_kgid_munged(current_user_ns(), stat->gid);
608 tmp.stx_mode = stat->mode;
609 tmp.stx_ino = stat->ino;
610 tmp.stx_size = stat->size;
611 tmp.stx_blocks = stat->blocks;
David Howells3209f682017-03-31 18:32:17 +0100612 tmp.stx_attributes_mask = stat->attributes_mask;
Eric Biggers64bd7202017-03-31 18:31:48 +0100613 tmp.stx_atime.tv_sec = stat->atime.tv_sec;
614 tmp.stx_atime.tv_nsec = stat->atime.tv_nsec;
615 tmp.stx_btime.tv_sec = stat->btime.tv_sec;
616 tmp.stx_btime.tv_nsec = stat->btime.tv_nsec;
617 tmp.stx_ctime.tv_sec = stat->ctime.tv_sec;
618 tmp.stx_ctime.tv_nsec = stat->ctime.tv_nsec;
619 tmp.stx_mtime.tv_sec = stat->mtime.tv_sec;
620 tmp.stx_mtime.tv_nsec = stat->mtime.tv_nsec;
621 tmp.stx_rdev_major = MAJOR(stat->rdev);
622 tmp.stx_rdev_minor = MINOR(stat->rdev);
623 tmp.stx_dev_major = MAJOR(stat->dev);
624 tmp.stx_dev_minor = MINOR(stat->dev);
Miklos Szeredifa2fcf42020-05-14 16:44:24 +0200625 tmp.stx_mnt_id = stat->mnt_id;
Eric Biggers825cf202022-08-26 23:58:44 -0700626 tmp.stx_dio_mem_align = stat->dio_mem_align;
627 tmp.stx_dio_offset_align = stat->dio_offset_align;
David Howellsa528d352017-01-31 16:46:22 +0000628
Eric Biggers64bd7202017-03-31 18:31:48 +0100629 return copy_to_user(buffer, &tmp, sizeof(tmp)) ? -EFAULT : 0;
David Howellsa528d352017-01-31 16:46:22 +0000630}
631
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800632int do_statx(int dfd, struct filename *filename, unsigned int flags,
Bijan Mottahedeh00187842020-05-22 21:31:17 -0700633 unsigned int mask, struct statx __user *buffer)
634{
635 struct kstat stat;
636 int error;
637
638 if (mask & STATX__RESERVED)
639 return -EINVAL;
640 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
641 return -EINVAL;
642
643 error = vfs_statx(dfd, filename, flags, &stat, mask);
644 if (error)
645 return error;
646
647 return cp_statx(&stat, buffer);
648}
649
David Howellsa528d352017-01-31 16:46:22 +0000650/**
651 * sys_statx - System call to get enhanced stats
652 * @dfd: Base directory to pathwalk from *or* fd to stat.
David Howells1e2f82d2017-04-26 22:15:55 +0100653 * @filename: File to stat or "" with AT_EMPTY_PATH
David Howellsa528d352017-01-31 16:46:22 +0000654 * @flags: AT_* flags to control pathwalk.
655 * @mask: Parts of statx struct actually required.
656 * @buffer: Result buffer.
657 *
David Howells1e2f82d2017-04-26 22:15:55 +0100658 * Note that fstat() can be emulated by setting dfd to the fd of interest,
659 * supplying "" as the filename and setting AT_EMPTY_PATH in the flags.
David Howellsa528d352017-01-31 16:46:22 +0000660 */
661SYSCALL_DEFINE5(statx,
662 int, dfd, const char __user *, filename, unsigned, flags,
663 unsigned int, mask,
664 struct statx __user *, buffer)
665{
Stefan Roesch1b6fe6e2022-02-25 10:53:26 -0800666 int ret;
667 struct filename *name;
668
669 name = getname_flags(filename, getname_statx_lookup_flags(flags), NULL);
670 ret = do_statx(dfd, name, flags, mask, buffer);
671 putname(name);
672
673 return ret;
David Howellsa528d352017-01-31 16:46:22 +0000674}
675
Guo Renf18ed302022-04-05 15:12:59 +0800676#if defined(CONFIG_COMPAT) && defined(__ARCH_WANT_COMPAT_STAT)
Al Viroac565de2017-04-08 18:13:00 -0400677static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
678{
679 struct compat_stat tmp;
680
Mikulas Patocka932aba12022-04-12 05:41:00 -0400681 if (sizeof(tmp.st_dev) < 4 && !old_valid_dev(stat->dev))
682 return -EOVERFLOW;
683 if (sizeof(tmp.st_rdev) < 4 && !old_valid_dev(stat->rdev))
Al Viroac565de2017-04-08 18:13:00 -0400684 return -EOVERFLOW;
685
686 memset(&tmp, 0, sizeof(tmp));
Mikulas Patocka932aba12022-04-12 05:41:00 -0400687 tmp.st_dev = new_encode_dev(stat->dev);
Al Viroac565de2017-04-08 18:13:00 -0400688 tmp.st_ino = stat->ino;
689 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
690 return -EOVERFLOW;
691 tmp.st_mode = stat->mode;
692 tmp.st_nlink = stat->nlink;
693 if (tmp.st_nlink != stat->nlink)
694 return -EOVERFLOW;
695 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
696 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
Mikulas Patocka932aba12022-04-12 05:41:00 -0400697 tmp.st_rdev = new_encode_dev(stat->rdev);
Al Viroac565de2017-04-08 18:13:00 -0400698 if ((u64) stat->size > MAX_NON_LFS)
699 return -EOVERFLOW;
700 tmp.st_size = stat->size;
701 tmp.st_atime = stat->atime.tv_sec;
702 tmp.st_atime_nsec = stat->atime.tv_nsec;
703 tmp.st_mtime = stat->mtime.tv_sec;
704 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
705 tmp.st_ctime = stat->ctime.tv_sec;
706 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
707 tmp.st_blocks = stat->blocks;
708 tmp.st_blksize = stat->blksize;
709 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
710}
711
712COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
713 struct compat_stat __user *, statbuf)
714{
715 struct kstat stat;
716 int error;
717
718 error = vfs_stat(filename, &stat);
719 if (error)
720 return error;
721 return cp_compat_stat(&stat, statbuf);
722}
723
724COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
725 struct compat_stat __user *, statbuf)
726{
727 struct kstat stat;
728 int error;
729
730 error = vfs_lstat(filename, &stat);
731 if (error)
732 return error;
733 return cp_compat_stat(&stat, statbuf);
734}
735
736#ifndef __ARCH_WANT_STAT64
737COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
738 const char __user *, filename,
739 struct compat_stat __user *, statbuf, int, flag)
740{
741 struct kstat stat;
742 int error;
743
744 error = vfs_fstatat(dfd, filename, &stat, flag);
745 if (error)
746 return error;
747 return cp_compat_stat(&stat, statbuf);
748}
749#endif
750
751COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
752 struct compat_stat __user *, statbuf)
753{
754 struct kstat stat;
755 int error = vfs_fstat(fd, &stat);
756
757 if (!error)
758 error = cp_compat_stat(&stat, statbuf);
759 return error;
760}
761#endif
762
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300763/* Caller is here responsible for sufficient locking (ie. inode->i_lock) */
764void __inode_add_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 inode->i_blocks += bytes >> 9;
767 bytes &= 511;
768 inode->i_bytes += bytes;
769 if (inode->i_bytes >= 512) {
770 inode->i_blocks++;
771 inode->i_bytes -= 512;
772 }
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300773}
Al Viroeb315d22017-06-08 21:15:03 -0400774EXPORT_SYMBOL(__inode_add_bytes);
Dmitry Monakhovb4627072009-12-14 15:21:12 +0300775
776void inode_add_bytes(struct inode *inode, loff_t bytes)
777{
778 spin_lock(&inode->i_lock);
779 __inode_add_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_unlock(&inode->i_lock);
781}
782
783EXPORT_SYMBOL(inode_add_bytes);
784
Jan Kara1c8924e2013-08-17 09:32:32 -0400785void __inode_sub_bytes(struct inode *inode, loff_t bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 inode->i_blocks -= bytes >> 9;
788 bytes &= 511;
789 if (inode->i_bytes < bytes) {
790 inode->i_blocks--;
791 inode->i_bytes += 512;
792 }
793 inode->i_bytes -= bytes;
Jan Kara1c8924e2013-08-17 09:32:32 -0400794}
795
796EXPORT_SYMBOL(__inode_sub_bytes);
797
798void inode_sub_bytes(struct inode *inode, loff_t bytes)
799{
800 spin_lock(&inode->i_lock);
801 __inode_sub_bytes(inode, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 spin_unlock(&inode->i_lock);
803}
804
805EXPORT_SYMBOL(inode_sub_bytes);
806
807loff_t inode_get_bytes(struct inode *inode)
808{
809 loff_t ret;
810
811 spin_lock(&inode->i_lock);
Jan Karaf4a8116a2017-08-08 09:54:36 +0200812 ret = __inode_get_bytes(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 spin_unlock(&inode->i_lock);
814 return ret;
815}
816
817EXPORT_SYMBOL(inode_get_bytes);
818
819void inode_set_bytes(struct inode *inode, loff_t bytes)
820{
821 /* Caller is here responsible for sufficient locking
822 * (ie. inode->i_lock) */
823 inode->i_blocks = bytes >> 9;
824 inode->i_bytes = bytes & 511;
825}
826
827EXPORT_SYMBOL(inode_set_bytes);