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;
32 /* TCP port number to use for NFS protocol. (Should be 2049.) */
33 #define NFS_SERVICE_PORT 2051
35 /* Maximum size of a single RPC message that we will accept (8 MB). */
36 #define MAX_RPC_MSGSIZE (8 << 20)
39 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
42 dirpath mountproc3_mnt_3_arg;
43 dirpath mountproc3_umnt_3_arg;
46 xdrproc_t _xdr_argument, _xdr_result;
47 char *(*local)(char *, struct svc_req *);
49 switch (rqstp->rq_proc) {
51 _xdr_argument = (xdrproc_t) xdr_void;
52 _xdr_result = (xdrproc_t) xdr_void;
53 local = (char *(*)(char *, struct svc_req *)) mountproc3_null_3_svc;
57 _xdr_argument = (xdrproc_t) xdr_dirpath;
58 _xdr_result = (xdrproc_t) xdr_mountres3;
59 local = (char *(*)(char *, struct svc_req *)) mountproc3_mnt_3_svc;
63 _xdr_argument = (xdrproc_t) xdr_void;
64 _xdr_result = (xdrproc_t) xdr_mountlist;
65 local = (char *(*)(char *, struct svc_req *)) mountproc3_dump_3_svc;
69 _xdr_argument = (xdrproc_t) xdr_dirpath;
70 _xdr_result = (xdrproc_t) xdr_void;
71 local = (char *(*)(char *, struct svc_req *)) mountproc3_umnt_3_svc;
74 case MOUNTPROC3_UMNTALL:
75 _xdr_argument = (xdrproc_t) xdr_void;
76 _xdr_result = (xdrproc_t) xdr_void;
77 local = (char *(*)(char *, struct svc_req *)) mountproc3_umntall_3_svc;
80 case MOUNTPROC3_EXPORT:
81 _xdr_argument = (xdrproc_t) xdr_void;
82 _xdr_result = (xdrproc_t) xdr_exports;
83 local = (char *(*)(char *, struct svc_req *)) mountproc3_export_3_svc;
87 svcerr_noproc (transp);
90 memset ((char *)&argument, 0, sizeof (argument));
91 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
92 svcerr_decode (transp);
95 result = (*local)((char *)&argument, rqstp);
96 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
97 svcerr_systemerr (transp);
99 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
100 fprintf (stderr, "%s", "unable to free arguments");
110 uint32_t verf_flavor;
112 uint32_t accept_stat;
115 static void async_rpc_write(RPCConnection *rpc,
116 const char *buf, gsize len);
117 static void async_rpc_flush(RPCConnection *rpc);
119 struct rpc_fail_reply {
123 uint32_t verf_flavor;
125 uint32_t accept_stat;
129 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
131 struct rpc_fail_reply header;
133 g_atomic_int_add(&outstanding_rpcs, -1);
135 header.xid = htonl(req->xid);
136 header.type = htonl(1); /* REPLY */
137 header.stat = htonl(MSG_ACCEPTED);
138 header.verf_flavor = 0;
140 header.accept_stat = htonl(stat);
142 g_mutex_lock(req->connection->send_lock);
143 uint32_t fragment = htonl(sizeof(header) | 0x80000000);
144 if (!req->connection->udp_transport)
145 async_rpc_write(req->connection, (const char *)&fragment,
147 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
148 async_rpc_flush(req->connection);
149 g_mutex_unlock(req->connection->send_lock);
151 if (req->args != NULL) {
154 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
155 if (!req->xdr_args_free(&xdr, req->args)) {
156 fprintf(stderr, "unable to free arguments");
161 if (req->raw_args != NULL)
162 g_string_free(req->raw_args, TRUE);
164 while (req->cleanup != NULL) {
165 struct cleanup_list *c = req->cleanup;
166 req->cleanup = c->next;
171 if (req->connection->udp_transport) {
172 /* For UDP, a connection only exists for the duration of a single
174 g_mutex_free(req->connection->send_lock);
175 g_string_free(req->connection->sendbuf, TRUE);
176 g_free(req->connection);
183 async_rpc_send_reply(RPCRequest *req, void *result)
185 bluesky_time_hires time_end;
187 GString *str = g_string_new("");
189 xdr_string_create(&xdr_out, str, XDR_ENCODE);
190 if (!req->xdr_result(&xdr_out, result)) {
191 async_rpc_send_failure(req, SYSTEM_ERR);
192 g_string_free(str, TRUE);
196 g_atomic_int_add(&outstanding_rpcs, -1);
198 struct rpc_reply header;
199 header.xid = htonl(req->xid);
200 header.type = htonl(1); /* REPLY */
201 header.stat = htonl(MSG_ACCEPTED);
202 header.verf_flavor = 0;
204 header.accept_stat = 0;
206 g_mutex_lock(req->connection->send_lock);
207 gsize msg_size = str->len;
208 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
209 if (!req->connection->udp_transport)
210 async_rpc_write(req->connection, (const char *)&fragment,
212 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
213 async_rpc_write(req->connection, str->str, str->len);
214 async_rpc_flush(req->connection);
215 g_mutex_unlock(req->connection->send_lock);
217 time_end = bluesky_now_hires();
219 printf("RPC[%"PRIx32"]: time = %"PRId64" ns\n",
220 req->xid, time_end - req->time_start);
223 g_string_free(str, TRUE);
225 if (req->args != NULL) {
228 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
229 if (!req->xdr_args_free(&xdr, req->args)) {
230 fprintf(stderr, "unable to free arguments");
235 if (req->raw_args != NULL)
236 g_string_free(req->raw_args, TRUE);
238 while (req->cleanup != NULL) {
239 struct cleanup_list *c = req->cleanup;
240 req->cleanup = c->next;
245 if (req->connection->udp_transport) {
246 /* For UDP, a connection only exists for the duration of a single
248 g_mutex_free(req->connection->send_lock);
249 g_string_free(req->connection->sendbuf, TRUE);
250 g_free(req->connection);
256 static const char *nfs_proc_names[] = {
257 [NFSPROC3_NULL] = "NULL",
258 [NFSPROC3_GETATTR] = "GETATTR",
259 [NFSPROC3_SETATTR] = "SETATTR",
260 [NFSPROC3_LOOKUP] = "LOOKUP",
261 [NFSPROC3_ACCESS] = "ACCESS",
262 [NFSPROC3_READLINK] = "READLINK",
263 [NFSPROC3_READ] = "READ",
264 [NFSPROC3_WRITE] = "WRITE",
265 [NFSPROC3_CREATE] = "CREATE",
266 [NFSPROC3_MKDIR] = "MKDIR",
267 [NFSPROC3_SYMLINK] = "SYMLINK",
268 [NFSPROC3_MKNOD] = "MKNOD",
269 [NFSPROC3_REMOVE] = "REMOVE",
270 [NFSPROC3_RMDIR] = "RMDIR",
271 [NFSPROC3_RENAME] = "RENAME",
272 [NFSPROC3_LINK] = "LINK",
273 [NFSPROC3_READDIR] = "READDIR",
274 [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
275 [NFSPROC3_FSSTAT] = "FSSTAT",
276 [NFSPROC3_FSINFO] = "FSINFO",
277 [NFSPROC3_PATHCONF] = "PATHCONF",
278 [NFSPROC3_COMMIT] = "COMMIT",
282 nfs_program_3(RPCRequest *req)
284 RPCConnection *connection = req->connection;
285 uint32_t xid = req->xid;
286 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
287 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
290 nfs_fh3 nfsproc3_getattr_3_arg;
291 setattr3args nfsproc3_setattr_3_arg;
292 diropargs3 nfsproc3_lookup_3_arg;
293 access3args nfsproc3_access_3_arg;
294 nfs_fh3 nfsproc3_readlink_3_arg;
295 read3args nfsproc3_read_3_arg;
296 write3args nfsproc3_write_3_arg;
297 create3args nfsproc3_create_3_arg;
298 mkdir3args nfsproc3_mkdir_3_arg;
299 symlink3args nfsproc3_symlink_3_arg;
300 mknod3args nfsproc3_mknod_3_arg;
301 diropargs3 nfsproc3_remove_3_arg;
302 diropargs3 nfsproc3_rmdir_3_arg;
303 rename3args nfsproc3_rename_3_arg;
304 link3args nfsproc3_link_3_arg;
305 readdir3args nfsproc3_readdir_3_arg;
306 readdirplus3args nfsproc3_readdirplus_3_arg;
307 nfs_fh3 nfsproc3_fsstat_3_arg;
308 nfs_fh3 nfsproc3_fsinfo_3_arg;
309 nfs_fh3 nfsproc3_pathconf_3_arg;
310 commit3args nfsproc3_commit_3_arg;
313 xdrproc_t _xdr_argument, _xdr_result;
314 char *(*local)(char *, RPCRequest *);
316 if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
317 printf("Dispatched NFS RPC message type %s\n",
318 nfs_proc_names[req->req_proc]);
320 printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
323 switch (req->req_proc) {
325 _xdr_argument = (xdrproc_t) xdr_void;
326 _xdr_result = (xdrproc_t) xdr_void;
327 local = (char *(*)(char *, RPCRequest *)) nfsproc3_null_3_svc;
330 case NFSPROC3_GETATTR:
331 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
332 _xdr_result = (xdrproc_t) xdr_getattr3res;
333 local = (char *(*)(char *, RPCRequest *)) nfsproc3_getattr_3_svc;
336 case NFSPROC3_SETATTR:
337 _xdr_argument = (xdrproc_t) xdr_setattr3args;
338 _xdr_result = (xdrproc_t) xdr_wccstat3;
339 local = (char *(*)(char *, RPCRequest *)) nfsproc3_setattr_3_svc;
342 case NFSPROC3_LOOKUP:
343 _xdr_argument = (xdrproc_t) xdr_diropargs3;
344 _xdr_result = (xdrproc_t) xdr_lookup3res;
345 local = (char *(*)(char *, RPCRequest *)) nfsproc3_lookup_3_svc;
348 case NFSPROC3_ACCESS:
349 _xdr_argument = (xdrproc_t) xdr_access3args;
350 _xdr_result = (xdrproc_t) xdr_access3res;
351 local = (char *(*)(char *, RPCRequest *)) nfsproc3_access_3_svc;
354 case NFSPROC3_READLINK:
355 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
356 _xdr_result = (xdrproc_t) xdr_readlink3res;
357 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readlink_3_svc;
361 _xdr_argument = (xdrproc_t) xdr_read3args;
362 _xdr_result = (xdrproc_t) xdr_read3res;
363 local = (char *(*)(char *, RPCRequest *)) nfsproc3_read_3_svc;
367 _xdr_argument = (xdrproc_t) xdr_write3args;
368 _xdr_result = (xdrproc_t) xdr_write3res;
369 local = (char *(*)(char *, RPCRequest *)) nfsproc3_write_3_svc;
372 case NFSPROC3_CREATE:
373 _xdr_argument = (xdrproc_t) xdr_create3args;
374 _xdr_result = (xdrproc_t) xdr_diropres3;
375 local = (char *(*)(char *, RPCRequest *)) nfsproc3_create_3_svc;
379 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
380 _xdr_result = (xdrproc_t) xdr_diropres3;
381 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mkdir_3_svc;
384 case NFSPROC3_SYMLINK:
385 _xdr_argument = (xdrproc_t) xdr_symlink3args;
386 _xdr_result = (xdrproc_t) xdr_diropres3;
387 local = (char *(*)(char *, RPCRequest *)) nfsproc3_symlink_3_svc;
391 _xdr_argument = (xdrproc_t) xdr_mknod3args;
392 _xdr_result = (xdrproc_t) xdr_diropres3;
393 local = (char *(*)(char *, RPCRequest *)) nfsproc3_mknod_3_svc;
396 case NFSPROC3_REMOVE:
397 _xdr_argument = (xdrproc_t) xdr_diropargs3;
398 _xdr_result = (xdrproc_t) xdr_wccstat3;
399 local = (char *(*)(char *, RPCRequest *)) nfsproc3_remove_3_svc;
403 _xdr_argument = (xdrproc_t) xdr_diropargs3;
404 _xdr_result = (xdrproc_t) xdr_wccstat3;
405 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rmdir_3_svc;
408 case NFSPROC3_RENAME:
409 _xdr_argument = (xdrproc_t) xdr_rename3args;
410 _xdr_result = (xdrproc_t) xdr_rename3res;
411 local = (char *(*)(char *, RPCRequest *)) nfsproc3_rename_3_svc;
415 _xdr_argument = (xdrproc_t) xdr_link3args;
416 _xdr_result = (xdrproc_t) xdr_link3res;
417 local = (char *(*)(char *, RPCRequest *)) nfsproc3_link_3_svc;
420 case NFSPROC3_READDIR:
421 _xdr_argument = (xdrproc_t) xdr_readdir3args;
422 _xdr_result = (xdrproc_t) xdr_readdir3res;
423 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdir_3_svc;
426 case NFSPROC3_READDIRPLUS:
427 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
428 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
429 local = (char *(*)(char *, RPCRequest *)) nfsproc3_readdirplus_3_svc;
432 case NFSPROC3_FSSTAT:
433 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
434 _xdr_result = (xdrproc_t) xdr_fsstat3res;
435 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsstat_3_svc;
438 case NFSPROC3_FSINFO:
439 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
440 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
441 local = (char *(*)(char *, RPCRequest *)) nfsproc3_fsinfo_3_svc;
444 case NFSPROC3_PATHCONF:
445 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
446 _xdr_result = (xdrproc_t) xdr_pathconf3res;
447 local = (char *(*)(char *, RPCRequest *)) nfsproc3_pathconf_3_svc;
450 case NFSPROC3_COMMIT:
451 _xdr_argument = (xdrproc_t) xdr_commit3args;
452 _xdr_result = (xdrproc_t) xdr_commit3res;
453 local = (char *(*)(char *, RPCRequest *)) nfsproc3_commit_3_svc;
457 async_rpc_send_failure(req, PROC_UNAVAIL);
461 /* Decode incoming message */
462 req->xdr_args_free = _xdr_argument;
463 req->args = g_new0(union argtype, 1);
465 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
466 if (!_xdr_argument(&xdr_in, req->args)) {
467 async_rpc_send_failure(req, GARBAGE_ARGS);
468 fprintf(stderr, "RPC decode error!\n");
472 /* Perform the call. */
473 req->xdr_result = _xdr_result;
474 result = (*local)((char *)req->args, req);
479 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
480 * built-in sunrpc parsing and dispatch that will allow for processing multiple
481 * requests at the same time. */
482 static GMainContext *main_context;
483 static GMainLoop *main_loop;
485 static GThreadPool *rpc_thread_pool;
487 static volatile int fs_dump_requested = 0;
489 static void sig_handler(int sig)
491 if (sig == SIGUSR1) {
492 fs_dump_requested = 1;
496 static gboolean async_flushd(gpointer data)
498 int rpc_count = g_atomic_int_get(&outstanding_rpcs);
499 if (rpc_count != 0) {
500 g_print("Currently outstanding RPC requests: %d\n", rpc_count);
503 if (fs_dump_requested) {
504 bluesky_debug_dump(fs);
505 fs_dump_requested = 0;
508 bluesky_flushd_invoke(fs);
512 static void async_rpc_task(gpointer data, gpointer user_data)
514 nfs_program_3((RPCRequest *)data);
517 static async_rpc_init()
519 main_context = g_main_context_new();
520 main_loop = g_main_loop_new(main_context, FALSE);
522 rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL,
523 bluesky_max_threads, FALSE, NULL);
525 /* Arrange to have the cache writeback code run every five seconds. */
526 GSource *source = g_timeout_source_new_seconds(5);
527 g_source_set_callback(source, async_flushd, NULL, NULL);
528 g_source_attach(source, main_context);
529 g_source_unref(source);
531 /* Signal USR1 is used to request a debugging dump of filesyste info */
533 sa.sa_handler = sig_handler;
534 sigemptyset(&sa.sa_mask);
535 sa.sa_flags = SA_RESTART;
536 if (sigaction(SIGUSR1, &sa, NULL) < 0) {
541 struct rpc_call_header {
555 /* Decode an RPC message and process it. Returns a boolean indicating whether
556 * the message could be processed; if false, an unrecoverable error occurred
557 * and the transport should be closed. */
558 static gboolean async_rpc_dispatch(RPCConnection *rpc)
560 bluesky_time_hires time_start = bluesky_now_hires();
562 GString *msg = rpc->msgbuf;
563 const char *buf = msg->str;
565 if (msg->len < sizeof(struct rpc_call_header)) {
566 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
570 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
571 uint32_t xid = ntohl(header->xid);
573 if (ntohl(header->mtype) != 0) {
574 /* Not an RPC call */
578 if (ntohl(header->rpcvers) != 2) {
582 g_atomic_int_add(&outstanding_rpcs, 1);
584 RPCRequest *req = g_new0(RPCRequest, 1);
585 req->connection = rpc;
586 req->time_start = time_start;
589 if (ntohl(header->prog) != NFS_PROGRAM) {
590 async_rpc_send_failure(req, PROG_UNAVAIL);
592 } else if (ntohl(header->vers) != NFS_V3) {
593 /* FIXME: Should be PROG_MISMATCH */
594 async_rpc_send_failure(req, PROG_UNAVAIL);
598 uint32_t proc = ntohl(header->proc);
600 /* Next, skip over authentication headers. */
601 buf += sizeof(struct rpc_call_header);
602 for (i = 0; i < 2; i++) {
603 struct rpc_auth *auth = (struct rpc_auth *)buf;
604 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len) {
605 g_atomic_int_add(&outstanding_rpcs, -1);
609 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
610 if (authsize > MAX_RPC_MSGSIZE) {
611 g_atomic_int_add(&outstanding_rpcs, -1);
618 if (buf - msg->str > msg->len) {
619 g_atomic_int_add(&outstanding_rpcs, -1);
624 req->raw_args_header_bytes = buf - msg->str;
625 req->req_proc = ntohl(header->proc);
626 rpc->msgbuf = g_string_new("");
628 if (bluesky_options.sync_frontends) {
631 g_thread_pool_push(rpc_thread_pool, req, NULL);
637 /* Write the given data to the RPC socket. */
638 static void async_rpc_write(RPCConnection *rpc,
639 const char *buf, gsize len)
641 if (rpc->udp_transport) {
642 g_string_append_len(rpc->sendbuf, buf, len);
646 /* Normal TCP path */
649 switch (g_io_channel_write_chars(rpc->channel, buf, len,
651 case G_IO_STATUS_ERROR:
652 case G_IO_STATUS_EOF:
653 case G_IO_STATUS_AGAIN:
654 fprintf(stderr, "Error writing to socket!\n");
656 case G_IO_STATUS_NORMAL:
663 // g_io_channel_flush(rpc->channel, NULL);
666 /* Flush a completed message out to the RPC socket */
667 static void async_rpc_flush(RPCConnection *rpc)
669 if (rpc->udp_transport) {
670 sendto(g_io_channel_unix_get_fd(rpc->channel),
671 rpc->sendbuf->str, rpc->sendbuf->len, 0,
672 (struct sockaddr *)&rpc->peer, sizeof(struct sockaddr_in));
675 g_io_channel_flush(rpc->channel, NULL);
679 static gboolean async_rpc_do_read(GIOChannel *channel,
680 GIOCondition condition,
683 RPCConnection *rpc = (RPCConnection *)data;
685 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
687 /* If we have not yet read in the fragment header, do that first. This is
688 * 4 bytes that indicates the number of bytes in the message to follow
689 * (with the high bit set if this is the last fragment making up the
691 if (rpc->frag_len == 0) {
692 bytes_to_read = 4 - rpc->frag_hdr_bytes;
694 bytes_to_read = rpc->frag_len & 0x7fffffff;
697 if (bytes_to_read > MAX_RPC_MSGSIZE
698 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
700 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
702 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
706 gsize bytes_read = 0;
707 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
708 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
709 switch (g_io_channel_read_chars(rpc->channel, buf,
710 bytes_to_read, &bytes_read, NULL)) {
711 case G_IO_STATUS_NORMAL:
713 case G_IO_STATUS_AGAIN:
715 case G_IO_STATUS_EOF:
716 if (bytes_read == bytes_to_read)
718 /* else fall through */
719 case G_IO_STATUS_ERROR:
720 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
721 g_io_channel_unix_get_fd(rpc->channel));
722 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
723 /* TODO: Clean up connection object. */
727 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
729 g_string_set_size(rpc->msgbuf,
730 rpc->msgbuf->len - (bytes_to_read - bytes_read));
732 if (rpc->frag_len == 0) {
733 /* Handle reading in the fragment header. If we've read the complete
734 * header, store the fragment size. */
735 rpc->frag_hdr_bytes += bytes_read;
736 if (rpc->frag_hdr_bytes == 4) {
737 memcpy((char *)&rpc->frag_len,
738 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
739 rpc->frag_len = ntohl(rpc->frag_len);
740 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
741 rpc->frag_hdr_bytes = 0;
744 /* We were reading in the fragment body. */
745 rpc->frag_len -= bytes_read;
747 if (rpc->frag_len == 0x80000000) {
748 /* We have a complete message since this was the last fragment and
749 * there are no more bytes in it. Dispatch the message. */
750 if (!async_rpc_dispatch(rpc)) {
751 fprintf(stderr, "Invalid RPC message, closing channel\n");
752 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
756 g_string_set_size(rpc->msgbuf, 0);
763 static gboolean async_rpc_do_accept(GIOChannel *channel,
764 GIOCondition condition,
767 int fd = g_io_channel_unix_get_fd(channel);
768 struct sockaddr_in addr;
769 socklen_t addrlen = sizeof(addr);
771 g_print("Received new connection on fd %d!\n", fd);
772 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
774 fprintf(stderr, "Error accepting connection: %m\n");
778 RPCConnection *rpc = g_new0(RPCConnection, 1);
779 rpc->channel = g_io_channel_unix_new(nfd);
780 rpc->msgbuf = g_string_new("");
781 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
782 rpc->send_lock = g_mutex_new();
783 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
784 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
786 g_source_attach(source, main_context);
787 g_source_unref(source);
792 static async_rpc_register_listening(int fd)
794 GIOChannel *channel = g_io_channel_unix_new(fd);
795 g_io_channel_set_encoding(channel, NULL, NULL);
796 GSource *source = g_io_create_watch(channel, G_IO_IN);
797 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
799 g_source_attach(source, main_context);
800 g_source_unref(source);
803 static gboolean async_rpc_do_udp(GIOChannel *channel,
804 GIOCondition condition,
809 struct sockaddr_in src;
810 socklen_t addrlen = sizeof(struct sockaddr_in);
811 ssize_t len = recvfrom(g_io_channel_unix_get_fd(channel),
813 (struct sockaddr *)&src, &addrlen);
815 fprintf(stderr, "UDP read error: %m, shutting down UDP\n");
819 g_assert(len < sizeof(buf));
821 RPCConnection *rpc = g_new0(RPCConnection, 1);
822 rpc->channel = channel;
823 rpc->msgbuf = g_string_new_len(buf, len);
824 rpc->send_lock = g_mutex_new();
825 rpc->udp_transport = TRUE;
826 memcpy(&rpc->peer, &src, sizeof(struct sockaddr_in));
827 rpc->sendbuf = g_string_new("");
829 /* We have a complete message since this was the last fragment and
830 * there are no more bytes in it. Dispatch the message. */
831 async_rpc_dispatch(rpc);
836 static async_rpc_register_listening_udp(int fd)
838 GIOChannel *channel = g_io_channel_unix_new(fd);
839 g_io_channel_set_encoding(channel, NULL, NULL);
840 GSource *source = g_io_create_watch(channel, G_IO_IN);
841 g_source_set_callback(source, (GSourceFunc)async_rpc_do_udp,
843 g_source_attach(source, main_context);
844 g_source_unref(source);
847 static gpointer async_rpc_run(gpointer data)
849 g_print("Starting NFS main loop...\n");
850 g_main_loop_run(main_loop);
860 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
862 transp = svcudp_create(RPC_ANYSOCK);
863 if (transp == NULL) {
864 fprintf(stderr, "%s", "cannot create udp service.");
867 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
868 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
872 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
873 if (transp == NULL) {
874 fprintf(stderr, "%s", "cannot create tcp service.");
877 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
878 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
882 /* NFS protocol (version 3) */
883 pmap_unset (NFS_PROGRAM, NFS_V3);
885 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
887 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
892 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
894 struct sockaddr_in addr;
895 addr.sin_family = AF_INET;
896 addr.sin_port = htons(NFS_SERVICE_PORT);
897 addr.sin_addr.s_addr = INADDR_ANY;
898 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
899 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
903 if (listen(fd, SOMAXCONN) < 0) {
904 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
908 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
909 fprintf(stderr, "Could not register NFS RPC service!\n");
913 async_rpc_register_listening(fd);
915 /* Minimal UDP NFSv3 support */
916 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
918 fprintf(stderr, "Unable to create NFS UDP socket: %m\n");
922 addr.sin_family = AF_INET;
923 addr.sin_port = htons(NFS_SERVICE_PORT);
924 addr.sin_addr.s_addr = INADDR_ANY;
925 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
926 fprintf(stderr, "Unable to bind to NFS UDP address: %m\n");
930 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_UDP, NFS_SERVICE_PORT)) {
931 fprintf(stderr, "Could not register NFS UDP RPC service!\n");
935 async_rpc_register_listening_udp(fd);
937 g_thread_create(async_rpc_run, NULL, TRUE, NULL);