#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
+#include <signal.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>
{
struct rpc_fail_reply header;
- fprintf(stderr, "Sending RPC failure status %d\n", stat);
-
header.xid = htonl(req->xid);
header.type = htonl(1); /* REPLY */
header.stat = htonl(MSG_ACCEPTED);
header.verf_len = 0;
header.accept_stat = htonl(stat);
+ g_mutex_lock(req->connection->send_lock);
uint32_t fragment = htonl(sizeof(header) | 0x80000000);
async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
async_rpc_write(req->connection, (const char *)&header, sizeof(header));
g_io_channel_flush(req->connection->channel, NULL);
+ g_mutex_unlock(req->connection->send_lock);
if (req->args != NULL) {
char buf[4];
if (!req->xdr_args_free(&xdr, req->args)) {
fprintf(stderr, "unable to free arguments");
}
+ g_free(req->args);
}
if (req->raw_args != NULL)
g_string_free(req->raw_args, TRUE);
+ while (req->cleanup != NULL) {
+ struct cleanup_list *c = req->cleanup;
+ req->cleanup = c->next;
+ c->func(c->arg);
+ g_free(c);
+ }
+
g_free(req);
}
void
async_rpc_send_reply(RPCRequest *req, void *result)
{
- static char reply_buf[MAX_RPC_MSGSIZE];
+ bluesky_time_hires time_end;
+
+ GString *str = g_string_new("");
XDR xdr_out;
- xdrmem_create(&xdr_out, reply_buf, MAX_RPC_MSGSIZE, XDR_ENCODE);
- if (result != NULL && !req->xdr_result(&xdr_out, result)) {
+ xdr_string_create(&xdr_out, str, XDR_ENCODE);
+ if (!req->xdr_result(&xdr_out, result)) {
async_rpc_send_failure(req, SYSTEM_ERR);
+ g_string_free(str, TRUE);
return;
}
header.verf_len = 0;
header.accept_stat = 0;
- gsize msg_size = xdr_out.x_ops->x_getpostn(&xdr_out);
- printf("Have an RPC reply of size %zd bytes\n", msg_size);
+ g_mutex_lock(req->connection->send_lock);
+ gsize msg_size = str->len;
uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
async_rpc_write(req->connection, (const char *)&header, sizeof(header));
- async_rpc_write(req->connection, reply_buf, msg_size);
+ async_rpc_write(req->connection, str->str, str->len);
g_io_channel_flush(req->connection->channel, NULL);
+ g_mutex_unlock(req->connection->send_lock);
+
+ time_end = bluesky_now_hires();
+
+ printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
+ req->xid, time_end - req->time_start);
/* Clean up. */
+ g_string_free(str, TRUE);
+
if (req->args != NULL) {
char buf[4];
XDR xdr;
if (!req->xdr_args_free(&xdr, req->args)) {
fprintf(stderr, "unable to free arguments");
}
+ g_free(req->args);
}
if (req->raw_args != NULL)
g_string_free(req->raw_args, TRUE);
+ while (req->cleanup != NULL) {
+ struct cleanup_list *c = req->cleanup;
+ req->cleanup = c->next;
+ c->func(c->arg);
+ g_free(c);
+ }
+
g_free(req);
}
+static const char *nfs_proc_names[] = {
+ [NFSPROC3_NULL] = "NULL",
+ [NFSPROC3_GETATTR] = "GETATTR",
+ [NFSPROC3_SETATTR] = "SETATTR",
+ [NFSPROC3_LOOKUP] = "LOOKUP",
+ [NFSPROC3_ACCESS] = "ACCESS",
+ [NFSPROC3_READLINK] = "READLINK",
+ [NFSPROC3_READ] = "READ",
+ [NFSPROC3_WRITE] = "WRITE",
+ [NFSPROC3_CREATE] = "CREATE",
+ [NFSPROC3_MKDIR] = "MKDIR",
+ [NFSPROC3_SYMLINK] = "SYMLINK",
+ [NFSPROC3_MKNOD] = "MKNOD",
+ [NFSPROC3_REMOVE] = "REMOVE",
+ [NFSPROC3_RMDIR] = "RMDIR",
+ [NFSPROC3_RENAME] = "RENAME",
+ [NFSPROC3_LINK] = "LINK",
+ [NFSPROC3_READDIR] = "READDIR",
+ [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
+ [NFSPROC3_FSSTAT] = "FSSTAT",
+ [NFSPROC3_FSINFO] = "FSINFO",
+ [NFSPROC3_PATHCONF] = "PATHCONF",
+ [NFSPROC3_COMMIT] = "COMMIT",
+};
+
static void
nfs_program_3(RPCRequest *req)
{
xdrproc_t _xdr_argument, _xdr_result;
char *(*local)(char *, RPCRequest *);
+ if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
+ printf("Dispatched NFS RPC message type %s\n",
+ nfs_proc_names[req->req_proc]);
+ } else {
+ printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
+ }
+
switch (req->req_proc) {
case NFSPROC3_NULL:
_xdr_argument = (xdrproc_t) xdr_void;
req->xdr_result = _xdr_result;
result = (*local)((char *)req->args, req);
- bluesky_flushd_invoke(fs);
-
return;
}
static GMainContext *main_context;
static GMainLoop *main_loop;
+static GThreadPool *rpc_thread_pool;
+
+static volatile int fs_dump_requested = 0;
+
+static void sig_handler(int sig)
+{
+ if (sig == SIGUSR1) {
+ fs_dump_requested = 1;
+ }
+}
+
+static gboolean async_flushd(gpointer data)
+{
+ if (fs_dump_requested) {
+ bluesky_debug_dump(fs);
+ fs_dump_requested = 0;
+ }
+
+ 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, -1, 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 {
* 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;
RPCRequest *req = g_new0(RPCRequest, 1);
req->connection = rpc;
+ req->time_start = time_start;
req->xid = xid;
if (ntohl(header->prog) != NFS_PROGRAM) {
if (buf - msg->str > msg->len)
return FALSE;
- printf("Dispatching RPC procedure %d...\n", proc);
-
req->raw_args = msg;
req->raw_args_header_bytes = buf - msg->str;
req->req_proc = ntohl(header->proc);
rpc->msgbuf = g_string_new("");
- nfs_program_3(req);
+ if (bluesky_options.sync_frontends) {
+ nfs_program_3(req);
+ } else {
+ g_thread_pool_push(rpc_thread_pool, req, NULL);
+ }
return TRUE;
}
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;
}
rpc->frag_len = ntohl(rpc->frag_len);
g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
rpc->frag_hdr_bytes = 0;
- g_print("RPC fragment header: %08x\n", rpc->frag_len);
}
} else {
/* We were reading in the fragment body. */
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. */
- g_print("Complete RPC message: %zd bytes\n", rpc->msgbuf->len);
if (!async_rpc_dispatch(rpc)) {
fprintf(stderr, "Invalid RPC message, closing channel\n");
g_io_channel_shutdown(rpc->channel, TRUE, NULL);
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);