Linux: Add cachestat, fchmodat2 syscalls (#958)

This commit is contained in:
Stephen Gregoratto 2023-11-20 14:01:20 +11:00 committed by GitHub
parent 69faf1b403
commit cc5c5319bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 287 additions and 1 deletions

47
libc/calls/cachestat.c Normal file
View file

@ -0,0 +1,47 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/cachestat.h"
#include "libc/intrin/strace.internal.h"
int sys_cachestat(int, struct cachestat_range *, struct cachestat *, uint32_t);
/**
* Query the page cache statistics of a file.
*
* @param fd The open file descriptor to retrieve statistics from.
* @param cstat_range The byte range in `fd` to query. When `len > 0`, the range
* is `[off..off + len]`. When `len` == 0, the range is from `off` to the end of
* `fd`.
* @param cstat The structure where page cache statistics are stored.
* @param flags Currently unused, and must be set to `0`.
* @return 0 on success, or -1 w/ errno.
* @raise EFAULT if `cstat_range` or `cstat` points to invalid memory
* @raise EINVAL if `flags` is nonzero
* @raise EBADF if `fd` is negative or not open
* @raise EOPNOTSUPP if `fd` refers to a hugetlbfs file
* @raise ENOSYS if not Linux 6.5
*/
int cachestat(int fd, struct cachestat_range *cstat_range,
struct cachestat *cstat, uint32_t flags) {
int rc;
rc = sys_cachestat(fd, cstat_range, cstat, flags);
STRACE("cachestat(%d, %p, %p, %#x) → %d% m", fd, cstat_range, cstat, flags);
return rc;
}

34
libc/calls/cachestat.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_CACHESTAT_H_
#define COSMOPOLITAN_LIBC_CALLS_CACHESTAT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct cachestat_range {
uint64_t off;
uint64_t len;
};
struct cachestat {
/** Number of cached pages. */
uint64_t nr_cache;
/** Number of dirty pages */
uint64_t nr_dirty;
/** Number of pages marked for writeback. */
uint64_t nr_writeback;
/** Number of pages evicted from the cache. */
uint64_t nr_evicted;
/**
* Number of recently evicted pages.
* A page is recently evicted if its last eviction was recent enough that its
* reentry to the cache would indicate that it is actively being used by the
* system, and that there is memory pressure on the system.
*/
uint64_t nr_recently_evicted;
};
int cachestat(int, struct cachestat_range *, struct cachestat *, uint32_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CALLS_LANDLOCK_H_ */

View file

@ -20,6 +20,7 @@
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
@ -28,6 +29,7 @@
#include "libc/sysv/errfuns.h"
int sys_fchmodat_linux(int, const char *, unsigned, int);
int sys_fchmodat2(int, const char *, unsigned, int);
/**
* Changes permissions on file, e.g.:
@ -40,6 +42,9 @@ int sys_fchmodat_linux(int, const char *, unsigned, int);
* @param mode contains octal flags (base 8)
* @param flags can have `AT_SYMLINK_NOFOLLOW`
* @raise EROFS if `dirfd` or `path` use zip file system
* @raise EOPNOTSUP on Linux if `path` is a symbolic link, `AT_SYMLINK_NOFOLLOW`
* is set in `flags`, and filesystem does not support setting the mode of
* symbolic links.
* @errors ENOENT, ENOTDIR, ENOSYS
* @asyncsignalsafe
* @see fchmod()
@ -53,7 +58,12 @@ int fchmodat(int dirfd, const char *path, uint32_t mode, int flags) {
rc = erofs();
} else if (!IsWindows()) {
if (IsLinux() && flags) {
rc = sys_fchmodat_linux(dirfd, path, mode, flags);
int serrno = errno;
rc = sys_fchmodat2(dirfd, path, mode, flags);
if (rc == -1 && errno == ENOSYS) {
errno = serrno;
rc = sys_fchmodat_linux(dirfd, path, mode, flags);
}
} else {
rc = sys_fchmodat(dirfd, path, mode, flags);
}