^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2016 Anton Ivanov (aivanov@brocade.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001 Ridgerun,Inc (glonnon@ridgerun.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <netinet/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sys/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sys/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <endian.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <byteswap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "ubd.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct pollfd kernel_pollfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int start_io_thread(unsigned long sp, int *fd_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int pid, fds[2], err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) err = os_pipe(fds, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if(err < 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) printk("start_io_thread - os_pipe failed, err = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) kernel_fd = fds[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) kernel_pollfd.fd = kernel_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) kernel_pollfd.events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *fd_out = fds[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = os_set_fd_block(*fd_out, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) err = os_set_fd_block(kernel_fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) printk("start_io_thread - failed to set nonblocking I/O.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) pid = clone(io_thread, (void *) sp, CLONE_FILES | CLONE_VM, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if(pid < 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) printk("start_io_thread - clone failed : errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) os_close_file(fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) os_close_file(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) kernel_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *fd_out = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int ubd_read_poll(int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kernel_pollfd.events = POLLIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return poll(&kernel_pollfd, 1, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int ubd_write_poll(int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) kernel_pollfd.events = POLLOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return poll(&kernel_pollfd, 1, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)