+
+ bluesky_flushd_invoke(fs);
+ return TRUE;
+}
+
+static void async_rpc_task(gpointer data, gpointer user_data)
+{
+ nfs_program_3((RPCRequest *)data);
+}
+
+static async_rpc_init()
+{
+ main_context = g_main_context_new();
+ main_loop = g_main_loop_new(main_context, FALSE);
+
+ rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL,
+ bluesky_max_threads, FALSE, NULL);
+
+ /* Arrange to have the cache writeback code run every five seconds. */
+ GSource *source = g_timeout_source_new_seconds(5);
+ g_source_set_callback(source, async_flushd, NULL, NULL);
+ g_source_attach(source, main_context);
+ g_source_unref(source);
+
+ /* Signal USR1 is used to request a debugging dump of filesyste info */
+ struct sigaction sa;
+ sa.sa_handler = sig_handler;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = SA_RESTART;
+ if (sigaction(SIGUSR1, &sa, NULL) < 0) {
+ perror("sigaction");
+ }
+}
+
+struct rpc_call_header {
+ uint32_t xid;
+ uint32_t mtype;
+ uint32_t rpcvers;
+ uint32_t prog;
+ uint32_t vers;
+ uint32_t proc;
+};
+
+struct rpc_auth {
+ uint32_t flavor;
+ uint32_t len;
+};
+
+/* Decode an RPC message and process it. Returns a boolean indicating whether
+ * the message could be processed; if false, an unrecoverable error occurred
+ * and the transport should be closed. */
+static gboolean async_rpc_dispatch(RPCConnection *rpc)
+{
+ bluesky_time_hires time_start = bluesky_now_hires();
+ int i;
+ GString *msg = rpc->msgbuf;
+ const char *buf = msg->str;
+
+ if (msg->len < sizeof(struct rpc_call_header)) {
+ fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
+ return FALSE;
+ }
+
+ struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
+ uint32_t xid = ntohl(header->xid);
+
+ if (ntohl(header->mtype) != 0) {
+ /* Not an RPC call */
+ return FALSE;
+ }
+
+ if (ntohl(header->rpcvers) != 2) {
+ return FALSE;
+ }
+
+ RPCRequest *req = g_new0(RPCRequest, 1);
+ req->connection = rpc;
+ req->time_start = time_start;
+ req->xid = xid;
+
+ if (ntohl(header->prog) != NFS_PROGRAM) {
+ async_rpc_send_failure(req, PROG_UNAVAIL);
+ return TRUE;
+ } else if (ntohl(header->vers) != NFS_V3) {
+ /* FIXME: Should be PROG_MISMATCH */
+ async_rpc_send_failure(req, PROG_UNAVAIL);
+ return TRUE;
+ }
+
+ uint32_t proc = ntohl(header->proc);
+
+ /* Next, skip over authentication headers. */
+ buf += sizeof(struct rpc_call_header);
+ for (i = 0; i < 2; i++) {
+ struct rpc_auth *auth = (struct rpc_auth *)buf;
+ if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
+ return FALSE;
+
+ gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
+ if (authsize > MAX_RPC_MSGSIZE)
+ return FALSE;
+
+ buf += authsize;
+ }
+
+ if (buf - msg->str > msg->len)
+ return FALSE;
+
+ req->raw_args = msg;
+ req->raw_args_header_bytes = buf - msg->str;
+ req->req_proc = ntohl(header->proc);
+ rpc->msgbuf = g_string_new("");
+
+ if (bluesky_options.sync_frontends) {
+ nfs_program_3(req);
+ } else {
+ g_thread_pool_push(rpc_thread_pool, req, NULL);
+ }
+
+ return TRUE;
+}
+
+/* Write the given data to the RPC socket. */
+static void async_rpc_write(RPCConnection *rpc,
+ const char *buf, gsize len)
+{
+ if (rpc->udp_transport) {
+ g_string_append_len(rpc->sendbuf, buf, len);
+ return;
+ }
+
+ /* Normal TCP path */
+ while (len > 0) {
+ gsize written = 0;
+ switch (g_io_channel_write_chars(rpc->channel, buf, len,
+ &written, NULL)) {
+ case G_IO_STATUS_ERROR:
+ case G_IO_STATUS_EOF:
+ case G_IO_STATUS_AGAIN:
+ fprintf(stderr, "Error writing to socket!\n");
+ return;
+ case G_IO_STATUS_NORMAL:
+ len -= written;
+ buf += written;
+ break;
+ }
+ }
+
+ // g_io_channel_flush(rpc->channel, NULL);
+}
+
+/* Flush a completed message out to the RPC socket */
+static void async_rpc_flush(RPCConnection *rpc)
+{
+ if (rpc->udp_transport) {
+ sendto(g_io_channel_unix_get_fd(rpc->channel),
+ rpc->sendbuf->str, rpc->sendbuf->len, 0,
+ (struct sockaddr *)&rpc->peer, sizeof(struct sockaddr_in));
+ return;
+ } else {
+ g_io_channel_flush(rpc->channel, NULL);
+ }
+}
+
+static gboolean async_rpc_do_read(GIOChannel *channel,
+ GIOCondition condition,
+ gpointer data)
+{
+ RPCConnection *rpc = (RPCConnection *)data;
+
+ gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
+
+ /* If we have not yet read in the fragment header, do that first. This is
+ * 4 bytes that indicates the number of bytes in the message to follow
+ * (with the high bit set if this is the last fragment making up the
+ * message). */
+ if (rpc->frag_len == 0) {
+ bytes_to_read = 4 - rpc->frag_hdr_bytes;
+ } else {
+ bytes_to_read = rpc->frag_len & 0x7fffffff;
+ }
+
+ if (bytes_to_read > MAX_RPC_MSGSIZE
+ || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
+ {
+ fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
+ bytes_to_read);
+ g_io_channel_shutdown(rpc->channel, TRUE, NULL);
+ return FALSE;
+ }
+
+ gsize bytes_read = 0;
+ g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
+ char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
+ switch (g_io_channel_read_chars(rpc->channel, buf,
+ bytes_to_read, &bytes_read, NULL)) {
+ case G_IO_STATUS_NORMAL:
+ break;
+ case G_IO_STATUS_AGAIN:
+ return TRUE;
+ case G_IO_STATUS_EOF:
+ if (bytes_read == bytes_to_read)
+ break;
+ /* else fall through */
+ case G_IO_STATUS_ERROR:
+ fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
+ g_io_channel_unix_get_fd(rpc->channel));
+ g_io_channel_shutdown(rpc->channel, TRUE, NULL);
+ /* TODO: Clean up connection object. */
+ return FALSE;
+ }
+
+ g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
+
+ g_string_set_size(rpc->msgbuf,
+ rpc->msgbuf->len - (bytes_to_read - bytes_read));
+
+ if (rpc->frag_len == 0) {
+ /* Handle reading in the fragment header. If we've read the complete
+ * header, store the fragment size. */
+ rpc->frag_hdr_bytes += bytes_read;
+ if (rpc->frag_hdr_bytes == 4) {
+ memcpy((char *)&rpc->frag_len,
+ &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
+ rpc->frag_len = ntohl(rpc->frag_len);
+ g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
+ rpc->frag_hdr_bytes = 0;
+ }
+ } else {
+ /* We were reading in the fragment body. */
+ rpc->frag_len -= bytes_read;
+
+ if (rpc->frag_len == 0x80000000) {
+ /* We have a complete message since this was the last fragment and
+ * there are no more bytes in it. Dispatch the message. */
+ if (!async_rpc_dispatch(rpc)) {
+ fprintf(stderr, "Invalid RPC message, closing channel\n");
+ g_io_channel_shutdown(rpc->channel, TRUE, NULL);
+ return FALSE;
+ }
+ rpc->frag_len = 0;
+ g_string_set_size(rpc->msgbuf, 0);
+ }
+ }
+
+ return TRUE;
+}
+
+static gboolean async_rpc_do_accept(GIOChannel *channel,
+ GIOCondition condition,
+ gpointer data)
+{
+ int fd = g_io_channel_unix_get_fd(channel);
+ struct sockaddr_in addr;
+ socklen_t addrlen = sizeof(addr);
+
+ g_print("Received new connection on fd %d!\n", fd);
+ int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
+ if (nfd < 0) {
+ fprintf(stderr, "Error accepting connection: %m\n");
+ return TRUE;
+ }
+
+ RPCConnection *rpc = g_new0(RPCConnection, 1);
+ rpc->channel = g_io_channel_unix_new(nfd);
+ rpc->msgbuf = g_string_new("");
+ g_io_channel_set_encoding(rpc->channel, NULL, NULL);
+ rpc->send_lock = g_mutex_new();
+ GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
+ g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
+ rpc, NULL);
+ g_source_attach(source, main_context);
+ g_source_unref(source);
+
+ return TRUE;
+}
+
+static async_rpc_register_listening(int fd)
+{
+ GIOChannel *channel = g_io_channel_unix_new(fd);
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ GSource *source = g_io_create_watch(channel, G_IO_IN);
+ g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
+ NULL, NULL);
+ g_source_attach(source, main_context);
+ g_source_unref(source);
+}
+
+static gboolean async_rpc_do_udp(GIOChannel *channel,
+ GIOCondition condition,
+ gpointer data)
+{
+ char buf[65536];
+
+ struct sockaddr_in src;
+ socklen_t addrlen = sizeof(struct sockaddr_in);
+ ssize_t len = recvfrom(g_io_channel_unix_get_fd(channel),
+ buf, sizeof(buf), 0,
+ (struct sockaddr *)&src, &addrlen);
+ if (len < 0) {
+ fprintf(stderr, "UDP read error: %m, shutting down UDP\n");
+ return FALSE;
+ }
+
+ g_assert(len < sizeof(buf));
+
+ RPCConnection *rpc = g_new0(RPCConnection, 1);
+ rpc->channel = channel;
+ rpc->msgbuf = g_string_new_len(buf, len);
+ rpc->send_lock = g_mutex_new();
+ rpc->udp_transport = TRUE;
+ memcpy(&rpc->peer, &src, sizeof(struct sockaddr_in));
+ rpc->sendbuf = g_string_new("");
+
+ /* We have a complete message since this was the last fragment and
+ * there are no more bytes in it. Dispatch the message. */
+ async_rpc_dispatch(rpc);
+
+ return TRUE;
+}
+
+static async_rpc_register_listening_udp(int fd)
+{
+ GIOChannel *channel = g_io_channel_unix_new(fd);
+ g_io_channel_set_encoding(channel, NULL, NULL);
+ GSource *source = g_io_create_watch(channel, G_IO_IN);
+ g_source_set_callback(source, (GSourceFunc)async_rpc_do_udp,
+ NULL, NULL);
+ g_source_attach(source, main_context);
+ g_source_unref(source);
+}
+
+static gpointer async_rpc_run(gpointer data)
+{
+ g_print("Starting NFS main loop...\n");
+ g_main_loop_run(main_loop);