diff --git a/lib/libzfs/libzfs_sendrecv.c b/lib/libzfs/libzfs_sendrecv.c index e3572914c997..eb6f79203aa7 100644 --- a/lib/libzfs/libzfs_sendrecv.c +++ b/lib/libzfs/libzfs_sendrecv.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -3297,6 +3298,44 @@ zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags, int err; int cleanup_fd; uint64_t action_handle = 0; +#ifdef __linux__ + struct stat sb; + +#ifndef F_SETPIPE_SZ +#define F_SETPIPE_SZ (F_SETLEASE + 7) +#endif + +#ifndef F_GETPIPE_SZ +#define F_GETPIPE_SZ (F_GETLEASE + 7) +#endif + + if (fstat(infd, &sb) == -1) { + perror("fstat"); + return (-2); + } + + /* + * It is not uncommon for gigabytes to be processed in zfs receive. + * Speculatively increase the buffer size via Linux-specific fcntl() + * call. + */ + if (S_ISFIFO(sb.st_mode)) { + FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r"); + + if (procf != NULL) { + unsigned long pipe_max_size; + unsigned long pipe_current_size; + if (fscanf(procf, "%lu", &pipe_max_size) > 0) { + pipe_current_size = fcntl(infd, F_GETPIPE_SZ); + if (pipe_current_size > 0 && + pipe_max_size > pipe_current_size) + (void) fcntl(infd, F_SETPIPE_SZ, + pipe_max_size); + } + fclose(procf); + } + } +#endif cleanup_fd = open(ZFS_DEV, O_RDWR); VERIFY(cleanup_fd >= 0);