Skip to content

Commit

Permalink
linux: use copy_file_range for uv_fs_copyfile when possible
Browse files Browse the repository at this point in the history
Refs: libuv#925 (comment)
PR-URL: libuv#2352
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Jameson Nash <vtjnash@gmail.com>
  • Loading branch information
CarterLi authored and JeffroMF committed May 16, 2022
1 parent 36adc01 commit 4117722
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/unix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -883,8 +883,27 @@ static ssize_t uv__fs_sendfile(uv_fs_t* req) {
ssize_t r;

off = req->off;

#ifdef __linux__
{
static int copy_file_range_support = 1;

if (copy_file_range_support) {
r = uv__fs_copy_file_range(in_fd, NULL, out_fd, &off, req->bufsml[0].len, 0);

if (r == -1 && errno == ENOSYS) {
errno = 0;
copy_file_range_support = 0;
} else {
goto ok;
}
}
}
#endif

r = sendfile(out_fd, in_fd, &off, req->bufsml[0].len);

ok:
/* sendfile() on SunOS returns EINVAL if the target fd is not a socket but
* it still writes out data. Fortunately, we can detect it by checking if
* the offset has been updated.
Expand Down
40 changes: 40 additions & 0 deletions src/unix/linux-syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@
# endif
#endif /* __NR_pwritev */

#ifndef __NR_copy_file_range
# if defined(__x86_64__)
# define __NR_copy_file_range 326
# elif defined(__i386__)
# define __NR_copy_file_range 377
# elif defined(__s390__)
# define __NR_copy_file_range 375
# elif defined(__arm__)
# define __NR_copy_file_range (UV_SYSCALL_BASE + 391)
# elif defined(__aarch64__)
# define __NR_copy_file_range 285
# elif defined(__powerpc__)
# define __NR_copy_file_range 379
# elif defined(__arc__)
# define __NR_copy_file_range 285
# endif
#endif /* __NR_copy_file_range */

#ifndef __NR_statx
# if defined(__x86_64__)
# define __NR_statx 332
Expand Down Expand Up @@ -180,6 +198,28 @@ int uv__dup3(int oldfd, int newfd, int flags) {
}


ssize_t
uv__fs_copy_file_range(int fd_in,
ssize_t* off_in,
int fd_out,
ssize_t* off_out,
size_t len,
unsigned int flags)
{
#ifdef __NR_copy_file_range
return syscall(__NR_copy_file_range,
fd_in,
off_in,
fd_out,
off_out,
len,
flags);
#else
return errno = ENOSYS, -1;
#endif
}


int uv__statx(int dirfd,
const char* path,
int flags,
Expand Down
7 changes: 7 additions & 0 deletions src/unix/linux-syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ struct uv__statx {
ssize_t uv__preadv(int fd, const struct iovec *iov, int iovcnt, int64_t offset);
ssize_t uv__pwritev(int fd, const struct iovec *iov, int iovcnt, int64_t offset);
int uv__dup3(int oldfd, int newfd, int flags);
ssize_t
uv__fs_copy_file_range(int fd_in,
ssize_t* off_in,
int fd_out,
ssize_t* off_out,
size_t len,
unsigned int flags);
int uv__statx(int dirfd,
const char* path,
int flags,
Expand Down

0 comments on commit 4117722

Please sign in to comment.