1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
9 /* RPC handling: registration, marshalling and unmarshalling of messages. For
10 * now this uses the standard Sun RPC mechanisms in the standard C library.
11 * Later, it might be changed to use something better. Much of this code was
12 * generated with rpcgen from the XDR specifications, but has been hand-edited
15 #include "mount_prot.h"
16 #include "nfs3_prot.h"
19 #include <rpc/pmap_clnt.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/ip.h>
30 static int outstanding_rpcs = 0;
31 static struct bluesky_stats *rpc_recv_stats, *rpc_send_stats;
33 /* TCP port number to use for NFS protocol. (Should be 2049.) */
34 #define NFS_SERVICE_PORT 2051
36 /* Maximum size of a single RPC message that we will accept (8 MB). */
37 #define MAX_RPC_MSGSIZE (8 << 20)
40 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
43 dirpath mountproc3_mnt_3_arg;
44 dirpath mountproc3_umnt_3_arg;
47 xdrproc_t _xdr_argument, _xdr_result;
48 char *(*local)(char *, struct svc_req *);
50 switch (rqstp->rq_proc) {
52 _xdr_argument = (xdrproc_t) xdr_void;
53 _xdr_result = (xdrproc_t) xdr_void;
54 local = (char *(*)(char *, struct svc_req *)) mountproc3_null_3_svc;
58 _xdr_argument = (xdrproc_t) xdr_dirpath;
59 _xdr_result = (xdrproc_t) xdr_mountres3;
60 local = (char *(*)(char *, struct svc_req *)) mountproc3_mnt_3_svc;
64 _xdr_argument = (xdrproc_t) xdr_void;
65 _xdr_result = (xdrproc_t) xdr_mountlist;
66 local = (char *(*)(char *, struct svc_req *)) mountproc3_dump_3_svc;
70 _xdr_argument = (xdrproc_t) xdr_dirpath;
71 _xdr_result = (xdrproc_t) xdr_void;
72 local = (char *(*)(char *, struct svc_req *)) mountproc3_umnt_3_svc;
75 case MOUNTPROC3_UMNTALL:
76 _xdr_argument = (xdrproc_t) xdr_void;
77 _xdr_result = (xdrproc_t) xdr_void;
78 local = (char *(*)(char *, struct svc_req *)) mountproc3_umntall_3_svc;
81 case MOUNTPROC3_EXPORT:
82 _xdr_argument = (xdrproc_t) xdr_void;
83 _xdr_result = (xdrproc_t) xdr_exports;
84 local = (char *(*)(char *, struct svc_req *)) mountproc3_export_3_svc;
88 svcerr_noproc (transp);
91 memset ((char *)&argument, 0, sizeof (argument));
92 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
93 svcerr_decode (transp);
96 result = (*local)((char *)&argument, rqstp);
97 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
98 svcerr_systemerr (transp);
100 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
101 fprintf (stderr, "%s", "unable to free arguments");
111 uint32_t verf_flavor;
113 uint32_t accept_stat;
116 static void async_rpc_write(RPCConnection *rpc,
117 const char *buf, gsize len);
118 static void async_rpc_flush(RPCConnection *rpc);
120 struct rpc_fail_reply {
124 uint32_t verf_flavor;
126 uint32_t accept_stat;
130 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
132 struct rpc_fail_reply header;
134 g_atomic_int_add(&outstanding_rpcs, -1);
136 header.xid = htonl(req->xid);
137 header.type = htonl(1); /* REPLY */
138 header.stat = htonl(MSG_ACCEPTED);
139 header.verf_flavor = 0;
141 header.accept_stat = htonl(stat);
143 g_mutex_lock(req->connection->send_lock);
144 uint32_t fragment = htonl(sizeof(header) | 0x80000000);
145 if (!req->connection->udp_transport)
146 async_rpc_write(req->connection, (const char *)&fragment,
148 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
149 async_rpc_flush(req->connection);
150 g_mutex_unlock(req->connection->send_lock);
152 if (req->args != NULL) {
155 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
156 if (!req->xdr_args_free(&xdr, req->args)) {
157 fprintf(stderr, "unable to free arguments");
162 if (req->raw_args != NULL)
163 g_string_free(req->raw_args, TRUE);
165 while (req->cleanup != NULL) {
166 struct cleanup_list *c = req->cleanup;
167 req->cleanup = c->next;
172 if (req->connection->udp_transport) {
173 /* For UDP, a connection only exists for the duration of a single
175 g_mutex_free(req->connection->send_lock);
176 g_string_free(req->connection->msgbuf, TRUE);
177 g_string_free(req->connection->sendbuf, TRUE);
178 g_free(req->connection);
185 async_rpc_send_reply(RPCRequest *req, void *result)
187 bluesky_time_hires time_end;
189 GString *str = g_string_new("");
191 xdr_string_create(&xdr_out, str, XDR_ENCODE);
192 if (!req->xdr_result(&xdr_out, result)) {
193 async_rpc_send_failure(req, SYSTEM_ERR);
194 g_string_free(str, TRUE);
198 g_atomic_int_add(&outstanding_rpcs, -1);
199 bluesky_stats_add(rpc_send_stats, str->len);
201 struct rpc_reply header;
202 header.xid = htonl(req->xid);
203 header.type = htonl(1); /* REPLY */
204 header.stat = htonl(MSG_ACCEPTED);
205 header.verf_flavor = 0;
207 header.accept_stat = 0;
209 g_mutex_lock(req->connection->send_lock);
210 gsize msg_size = str->len;
211 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
212 if (!req->connection->udp_transport)
213 async_rpc_write(req->connection, (const char *)&fragment,
215 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
216 async_rpc_write(req->connection, str->str, str->len);
217 async_rpc_flush(req->connection);
218 g_mutex_unlock(req->connection->send_lock);
220 time_end = bluesky_now_hires();
223 printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
224 req->xid, time_end - req->time_start);
228 g_string_free(str, TRUE);
230 if (req->args != NULL) {
233 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
234 if (!req->xdr_args_free(&xdr, req->args)) {
235 fprintf(stderr, "unable to free arguments");
240 if (req->raw_args != NULL)
241 g_string_free(req->raw_args, TRUE);
243 while (req->cleanup != NULL) {
244 struct cleanup_list *c = req->cleanup;
245 req->cleanup = c->next;
250 if (req->connection->udp_transport) {
251 /* For UDP, a connection only exists for the duration of a single
253 g_mutex_free(req->connection->send_lock);
254 g_string_free(req->connection->msgbuf, TRUE);
255 g_string_free(req->connection->sendbuf, TRUE);
256 g_free(req->connection);
262 static const char *nfs_proc_names[] = {
263 [NFSPROC3_NULL] = "NULL",
264 [NFSPROC3_GETATTR] = "GETATTR",
265 [NFSPROC3_SETATTR] = "SETATTR",
266 [NFSPROC3_LOOKUP] = "LOOKUP",
267 [NFSPROC3_ACCESS] = "ACCESS",
268 [NFSPROC3_READLINK] = "READLINK",
269 [NFSPROC3_READ] = "READ",
270 [NFSPROC3_WRITE] = "WRITE",
271 [NFSPROC3_CREATE] = "CREATE",
272 [NFSPROC3_MKDIR] = "MKDIR",
273 [NFSPROC3_SYMLINK] = "SYMLINK",
274 [NFSPROC3_MKNOD] = "MKNOD",
275 [NFSPROC3_REMOVE] = "REMOVE",
276 [NFSPROC3_RMDIR] = "RMDIR",
277 [NFSPROC3_RENAME] = "RENAME",
278 [NFSPROC3_LINK] = "LINK",
279 [NFSPROC3_READDIR] = "READDIR",
280 [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
281 [NFSPROC3_FSSTAT] = "FSSTAT",
282 [NFSPROC3_FSINFO] = "FSINFO",
283 [NFSPROC3_PATHCONF] = "PATHCONF",
284 [NFSPROC3_COMMIT] = "COMMIT",
288 nfs_program_3(RPCRequest *req)
290 RPCConnection *connection = req->connection;
291 uint32_t xid = req->xid;
292 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
293 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
296 nfs_fh3 nfsproc3_getattr_3_arg;
297 setattr3args nfsproc3_setattr_3_arg;
298 diropargs3 nfsproc3_lookup_3_arg;
299 access3args nfsproc3_access_3_arg;
300 nfs_fh3 nfsproc3_readlink_3_arg;
301 read3args nfsproc3_read_3_arg;
302 write3args nfsproc3_write_3_arg;
303 create3args nfsproc3_create_3_arg;
304 mkdir3args nfsproc3_mkdir_3_arg;
305 symlink3args nfsproc3_symlink_3_arg;
306 mknod3args nfsproc3_mknod_3_arg;
307 diropargs3 nfsproc3_remove_3_arg;
308 diropargs3 nfsproc3_rmdir_3_arg;
309 rename3args nfsproc3_rename_3_arg;
310 link3args nfsproc3_link_3_arg;
311 readdir3args nfsproc3_readdir_3_arg;
312 readdirplus3args nfsproc3_readdirplus_3_arg;
313 nfs_fh3 nfsproc3_fsstat_3_arg;
314 nfs_fh3 nfsproc3_fsinfo_3_arg;
315 nfs_fh3 nfsproc3_pathconf_3_arg;
316 commit3args nfsproc3_commit_3_arg;
319 xdrproc_t _xdr_argument, _xdr_result;
320 char *(*local)(char *, RPCRequest *);
323 if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
324 printf("Dispatched NFS RPC message type %s\n",
325 nfs_proc_names[req->req_proc]);
327 printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
331 switch (req->req_proc) {
333 _xdr_argument = (xdrproc_t) xdr_void;
334 _xdr_result = (xdrproc_t) xdr_void;
335 local = (char *(*)(char *, RPCRequest *)) nfsproc3_null_3_svc;
338 case NFSPROC3_GETATTR:
339 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
340 _xdr_result = (xdrproc_t) xdr_getattr3res;
341 local = (char *(*)(char *, RPCRequest *)) nfsproc3_getattr_3_svc;
344 case NFSPROC3_SETATTR:
345 _xdr_argument = (xdrproc_t) xdr_setattr3args;
346 _xdr_result = (xdrproc_t) xdr_wccstat3;
347 local = (char *(*)(char *, RPCRequest *)) nfsproc3_setattr_3_svc;
350 case NFSPROC3_LOOKUP:
351 _xdr_argument = (xdrproc_t) xdr_diropargs3;
352 _xdr_result = (xdrproc_t) xdr_lookup3res;
353 local = (char *(*)(char *, RPCRequest *)) nfsproc3_lookup_3_svc;
356 case NFSPROC3_ACCESS:
357 _xdr_argument = (xdrproc_t) xdr_access3args;
358 _xdr_result = (xdrproc_t) xdr_access3res;
359 local = (char *(*)(char *, RPCRequest *)) nfsproc3_access_3_svc;
362 case NFSPROC3_READLINK:
363 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
364 _xdr_result = (xdrproc_t) xdr_readlink3res;
365 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readlink_3_svc;
369 _xdr_argument = (xdrproc_t) xdr_read3args;
370 _xdr_result = (xdrproc_t) xdr_read3res;
371 local = (char *(*)(char *, RPCRequest *)) nfsproc3_read_3_svc;
375 _xdr_argument = (xdrproc_t) xdr_write3args;
376 _xdr_result = (xdrproc_t) xdr_write3res;
377 local = (char *(*)(char *, RPCRequest *)) nfsproc3_write_3_svc;
380 case NFSPROC3_CREATE:
381 _xdr_argument = (xdrproc_t) xdr_create3args;
382 _xdr_result = (xdrproc_t) xdr_diropres3;
383 local = (char *(*)(char *, RPCRequest *)) nfsproc3_create_3_svc;
387 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
388 _xdr_result = (xdrproc_t) xdr_diropres3;
389 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mkdir_3_svc;
392 case NFSPROC3_SYMLINK:
393 _xdr_argument = (xdrproc_t) xdr_symlink3args;
394 _xdr_result = (xdrproc_t) xdr_diropres3;
395 local = (char *(*)(char *, RPCRequest *)) nfsproc3_symlink_3_svc;
399 _xdr_argument = (xdrproc_t) xdr_mknod3args;
400 _xdr_result = (xdrproc_t) xdr_diropres3;
401 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mknod_3_svc;
404 case NFSPROC3_REMOVE:
405 _xdr_argument = (xdrproc_t) xdr_diropargs3;
406 _xdr_result = (xdrproc_t) xdr_wccstat3;
407 local = (char *(*)(char *, RPCRequest *)) nfsproc3_remove_3_svc;
411 _xdr_argument = (xdrproc_t) xdr_diropargs3;
412 _xdr_result = (xdrproc_t) xdr_wccstat3;
413 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rmdir_3_svc;
416 case NFSPROC3_RENAME:
417 _xdr_argument = (xdrproc_t) xdr_rename3args;
418 _xdr_result = (xdrproc_t) xdr_rename3res;
419 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rename_3_svc;
423 _xdr_argument = (xdrproc_t) xdr_link3args;
424 _xdr_result = (xdrproc_t) xdr_link3res;
425 local = (char *(*)(char *, RPCRequest *)) nfsproc3_link_3_svc;
428 case NFSPROC3_READDIR:
429 _xdr_argument = (xdrproc_t) xdr_readdir3args;
430 _xdr_result = (xdrproc_t) xdr_readdir3res;
431 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdir_3_svc;
434 case NFSPROC3_READDIRPLUS:
435 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
436 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
437 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdirplus_3_svc;
440 case NFSPROC3_FSSTAT:
441 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
442 _xdr_result = (xdrproc_t) xdr_fsstat3res;
443 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsstat_3_svc;
446 case NFSPROC3_FSINFO:
447 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
448 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
449 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsinfo_3_svc;
452 case NFSPROC3_PATHCONF:
453 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
454 _xdr_result = (xdrproc_t) xdr_pathconf3res;
455 local = (char *(*)(char *, RPCRequest *)) nfsproc3_pathconf_3_svc;
458 case NFSPROC3_COMMIT:
459 _xdr_argument = (xdrproc_t) xdr_commit3args;
460 _xdr_result = (xdrproc_t) xdr_commit3res;
461 local = (char *(*)(char *, RPCRequest *)) nfsproc3_commit_3_svc;
465 async_rpc_send_failure(req, PROC_UNAVAIL);
469 /* Decode incoming message */
470 req->xdr_args_free = _xdr_argument;
471 req->args = g_new0(union argtype, 1);
473 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
474 if (!_xdr_argument(&xdr_in, req->args)) {
475 async_rpc_send_failure(req, GARBAGE_ARGS);
476 fprintf(stderr, "RPC decode error!\n");
480 /* Perform the call. */
481 req->xdr_result = _xdr_result;
482 result = (*local)((char *)req->args, req);
487 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
488 * built-in sunrpc parsing and dispatch that will allow for processing multiple
489 * requests at the same time. */
490 static GMainContext *main_context;
491 static GMainLoop *main_loop;
493 static GThreadPool *rpc_thread_pool;
495 static volatile int fs_dump_requested = 0;
497 static void sig_handler(int sig)
499 if (sig == SIGUSR1) {
500 fs_dump_requested = 1;
504 static gboolean async_flushd(gpointer data)
507 int rpc_count = g_atomic_int_get(&outstanding_rpcs);
508 if (rpc_count != 0) {
509 g_print("Currently outstanding RPC requests: %d\n", rpc_count);
513 if (fs_dump_requested) {
514 bluesky_debug_dump(fs);
515 bluesky_stats_dump_all();
516 fs_dump_requested = 0;
519 bluesky_flushd_invoke(fs);
523 static void async_rpc_task(gpointer data, gpointer user_data)
525 nfs_program_3((RPCRequest *)data);
528 static async_rpc_init()
530 main_context = g_main_context_new();
531 main_loop = g_main_loop_new(main_context, FALSE);
533 rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL,
534 bluesky_max_threads, FALSE, NULL);
536 /* Arrange to have the cache writeback code run every five seconds. */
537 GSource *source = g_timeout_source_new_seconds(5);
538 g_source_set_callback(source, async_flushd, NULL, NULL);
539 g_source_attach(source, main_context);
540 g_source_unref(source);
542 /* Signal USR1 is used to request a debugging dump of filesyste info */
544 sa.sa_handler = sig_handler;
545 sigemptyset(&sa.sa_mask);
546 sa.sa_flags = SA_RESTART;
547 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
552 struct rpc_call_header {
566 /* Decode an RPC message and process it. Returns a boolean indicating whether
567 * the message could be processed; if false, an unrecoverable error occurred
568 * and the transport should be closed. */
569 static gboolean async_rpc_dispatch(RPCConnection *rpc)
571 bluesky_time_hires time_start = bluesky_now_hires();
573 GString *msg = rpc->msgbuf;
574 const char *buf = msg->str;
576 bluesky_stats_add(rpc_recv_stats, msg->len);
578 if (msg->len < sizeof(struct rpc_call_header)) {
579 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
583 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
584 uint32_t xid = ntohl(header->xid);
586 if (ntohl(header->mtype) != 0) {
587 /* Not an RPC call */
591 if (ntohl(header->rpcvers) != 2) {
595 g_atomic_int_add(&outstanding_rpcs, 1);
597 RPCRequest *req = g_new0(RPCRequest, 1);
598 req->connection = rpc;
599 req->time_start = time_start;
602 if (ntohl(header->prog) != NFS_PROGRAM) {
603 async_rpc_send_failure(req, PROG_UNAVAIL);
605 } else if (ntohl(header->vers) != NFS_V3) {
606 /* FIXME: Should be PROG_MISMATCH */
607 async_rpc_send_failure(req, PROG_UNAVAIL);
611 uint32_t proc = ntohl(header->proc);
613 /* Next, skip over authentication headers. */
614 buf += sizeof(struct rpc_call_header);
615 for (i = 0; i < 2; i++) {
616 struct rpc_auth *auth = (struct rpc_auth *)buf;
617 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len) {
618 g_atomic_int_add(&outstanding_rpcs, -1);
622 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
623 if (authsize > MAX_RPC_MSGSIZE) {
624 g_atomic_int_add(&outstanding_rpcs, -1);
631 if (buf - msg->str > msg->len) {
632 g_atomic_int_add(&outstanding_rpcs, -1);
637 req->raw_args_header_bytes = buf - msg->str;
638 req->req_proc = ntohl(header->proc);
639 rpc->msgbuf = g_string_new("");
641 if (bluesky_options.sync_frontends) {
644 g_thread_pool_push(rpc_thread_pool, req, NULL);
650 /* Write the given data to the RPC socket. */
651 static void async_rpc_write(RPCConnection *rpc,
652 const char *buf, gsize len)
654 if (rpc->udp_transport) {
655 g_string_append_len(rpc->sendbuf, buf, len);
659 /* Normal TCP path */
662 switch (g_io_channel_write_chars(rpc->channel, buf, len,
664 case G_IO_STATUS_ERROR:
665 case G_IO_STATUS_EOF:
666 case G_IO_STATUS_AGAIN:
667 fprintf(stderr, "Error writing to socket!\n");
669 case G_IO_STATUS_NORMAL:
676 // g_io_channel_flush(rpc->channel, NULL);
679 /* Flush a completed message out to the RPC socket */
680 static void async_rpc_flush(RPCConnection *rpc)
682 if (rpc->udp_transport) {
683 sendto(g_io_channel_unix_get_fd(rpc->channel),
684 rpc->sendbuf->str, rpc->sendbuf->len, 0,
685 (struct sockaddr *)&rpc->peer, sizeof(struct sockaddr_in));
688 g_io_channel_flush(rpc->channel, NULL);
692 static gboolean async_rpc_do_read(GIOChannel *channel,
693 GIOCondition condition,
696 RPCConnection *rpc = (RPCConnection *)data;
698 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
700 /* If we have not yet read in the fragment header, do that first. This is
701 * 4 bytes that indicates the number of bytes in the message to follow
702 * (with the high bit set if this is the last fragment making up the
704 if (rpc->frag_len == 0) {
705 bytes_to_read = 4 - rpc->frag_hdr_bytes;
707 bytes_to_read = rpc->frag_len & 0x7fffffff;
710 if (bytes_to_read > MAX_RPC_MSGSIZE
711 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
713 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
715 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
719 gsize bytes_read = 0;
720 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
721 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
722 switch (g_io_channel_read_chars(rpc->channel, buf,
723 bytes_to_read, &bytes_read, NULL)) {
724 case G_IO_STATUS_NORMAL:
726 case G_IO_STATUS_AGAIN:
728 case G_IO_STATUS_EOF:
729 if (bytes_read == bytes_to_read)
731 /* else fall through */
732 case G_IO_STATUS_ERROR:
733 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
734 g_io_channel_unix_get_fd(rpc->channel));
735 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
736 /* TODO: Clean up connection object. */
740 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
742 g_string_set_size(rpc->msgbuf,
743 rpc->msgbuf->len - (bytes_to_read - bytes_read));
745 if (rpc->frag_len == 0) {
746 /* Handle reading in the fragment header. If we've read the complete
747 * header, store the fragment size. */
748 rpc->frag_hdr_bytes += bytes_read;
749 if (rpc->frag_hdr_bytes == 4) {
750 memcpy((char *)&rpc->frag_len,
751 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
752 rpc->frag_len = ntohl(rpc->frag_len);
753 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
754 rpc->frag_hdr_bytes = 0;
757 /* We were reading in the fragment body. */
758 rpc->frag_len -= bytes_read;
760 if (rpc->frag_len == 0x80000000) {
761 /* We have a complete message since this was the last fragment and
762 * there are no more bytes in it. Dispatch the message. */
763 if (!async_rpc_dispatch(rpc)) {
764 fprintf(stderr, "Invalid RPC message, closing channel\n");
765 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
769 g_string_set_size(rpc->msgbuf, 0);
776 static gboolean async_rpc_do_accept(GIOChannel *channel,
777 GIOCondition condition,
780 int fd = g_io_channel_unix_get_fd(channel);
781 struct sockaddr_in addr;
782 socklen_t addrlen = sizeof(addr);
784 g_print("Received new connection on fd %d!\n", fd);
785 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
787 fprintf(stderr, "Error accepting connection: %m\n");
791 RPCConnection *rpc = g_new0(RPCConnection, 1);
792 rpc->channel = g_io_channel_unix_new(nfd);
793 rpc->msgbuf = g_string_new("");
794 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
795 rpc->send_lock = g_mutex_new();
796 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
797 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
799 g_source_attach(source, main_context);
800 g_source_unref(source);
805 static async_rpc_register_listening(int fd)
807 GIOChannel *channel = g_io_channel_unix_new(fd);
808 g_io_channel_set_encoding(channel, NULL, NULL);
809 GSource *source = g_io_create_watch(channel, G_IO_IN);
810 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
812 g_source_attach(source, main_context);
813 g_source_unref(source);
816 static gboolean async_rpc_do_udp(GIOChannel *channel,
817 GIOCondition condition,
822 struct sockaddr_in src;
823 socklen_t addrlen = sizeof(struct sockaddr_in);
824 ssize_t len = recvfrom(g_io_channel_unix_get_fd(channel),
826 (struct sockaddr *)&src, &addrlen);
828 fprintf(stderr, "UDP read error: %m, shutting down UDP\n");
832 g_assert(len < sizeof(buf));
834 RPCConnection *rpc = g_new0(RPCConnection, 1);
835 rpc->channel = channel;
836 rpc->msgbuf = g_string_new_len(buf, len);
837 rpc->send_lock = g_mutex_new();
838 rpc->udp_transport = TRUE;
839 memcpy(&rpc->peer, &src, sizeof(struct sockaddr_in));
840 rpc->sendbuf = g_string_new("");
842 /* We have a complete message since this was the last fragment and
843 * there are no more bytes in it. Dispatch the message. */
844 async_rpc_dispatch(rpc);
849 static async_rpc_register_listening_udp(int fd)
851 GIOChannel *channel = g_io_channel_unix_new(fd);
852 g_io_channel_set_encoding(channel, NULL, NULL);
853 GSource *source = g_io_create_watch(channel, G_IO_IN);
854 g_source_set_callback(source, (GSourceFunc)async_rpc_do_udp,
856 g_source_attach(source, main_context);
857 g_source_unref(source);
860 static gpointer async_rpc_run(gpointer data)
862 g_print("Starting NFS main loop...\n");
863 g_main_loop_run(main_loop);
870 rpc_recv_stats = bluesky_stats_new("NFS RPC Messages In");
871 rpc_send_stats = bluesky_stats_new("NFS RPC Messages Out");
876 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
878 transp = svcudp_create(RPC_ANYSOCK);
879 if (transp == NULL) {
880 fprintf(stderr, "%s", "cannot create udp service.");
883 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
884 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
888 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
889 if (transp == NULL) {
890 fprintf(stderr, "%s", "cannot create tcp service.");
893 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
894 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
898 /* NFS protocol (version 3) */
899 pmap_unset (NFS_PROGRAM, NFS_V3);
901 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
903 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
908 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
910 struct sockaddr_in addr;
911 addr.sin_family = AF_INET;
912 addr.sin_port = htons(NFS_SERVICE_PORT);
913 addr.sin_addr.s_addr = INADDR_ANY;
914 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
915 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
919 if (listen(fd, SOMAXCONN) < 0) {
920 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
924 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
925 fprintf(stderr, "Could not register NFS RPC service!\n");
929 async_rpc_register_listening(fd);
931 /* Minimal UDP NFSv3 support */
932 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
934 fprintf(stderr, "Unable to create NFS UDP socket: %m\n");
938 addr.sin_family = AF_INET;
939 addr.sin_port = htons(NFS_SERVICE_PORT);
940 addr.sin_addr.s_addr = INADDR_ANY;
941 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
942 fprintf(stderr, "Unable to bind to NFS UDP address: %m\n");
946 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_UDP, NFS_SERVICE_PORT)) {
947 fprintf(stderr, "Could not register NFS UDP RPC service!\n");
951 async_rpc_register_listening_udp(fd);
953 g_thread_create(async_rpc_run, NULL, TRUE, NULL);