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