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>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
29 /* TCP port number to use for NFS protocol. (Should be 2049.) */
30 #define NFS_SERVICE_PORT 2051
32 /* Maximum size of a single RPC message that we will accept (8 MB). */
33 #define MAX_RPC_MSGSIZE (8 << 20)
36 mount_program_3(struct svc_req *rqstp, register SVCXPRT *transp)
39 dirpath mountproc3_mnt_3_arg;
40 dirpath mountproc3_umnt_3_arg;
43 xdrproc_t _xdr_argument, _xdr_result;
44 char *(*local)(char *, struct svc_req *);
46 switch (rqstp->rq_proc) {
48 _xdr_argument = (xdrproc_t) xdr_void;
49 _xdr_result = (xdrproc_t) xdr_void;
50 local = (char *(*)(char *, struct svc_req *)) mountproc3_null_3_svc;
54 _xdr_argument = (xdrproc_t) xdr_dirpath;
55 _xdr_result = (xdrproc_t) xdr_mountres3;
56 local = (char *(*)(char *, struct svc_req *)) mountproc3_mnt_3_svc;
60 _xdr_argument = (xdrproc_t) xdr_void;
61 _xdr_result = (xdrproc_t) xdr_mountlist;
62 local = (char *(*)(char *, struct svc_req *)) mountproc3_dump_3_svc;
66 _xdr_argument = (xdrproc_t) xdr_dirpath;
67 _xdr_result = (xdrproc_t) xdr_void;
68 local = (char *(*)(char *, struct svc_req *)) mountproc3_umnt_3_svc;
71 case MOUNTPROC3_UMNTALL:
72 _xdr_argument = (xdrproc_t) xdr_void;
73 _xdr_result = (xdrproc_t) xdr_void;
74 local = (char *(*)(char *, struct svc_req *)) mountproc3_umntall_3_svc;
77 case MOUNTPROC3_EXPORT:
78 _xdr_argument = (xdrproc_t) xdr_void;
79 _xdr_result = (xdrproc_t) xdr_exports;
80 local = (char *(*)(char *, struct svc_req *)) mountproc3_export_3_svc;
84 svcerr_noproc (transp);
87 memset ((char *)&argument, 0, sizeof (argument));
88 if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
89 svcerr_decode (transp);
92 result = (*local)((char *)&argument, rqstp);
93 if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
94 svcerr_systemerr (transp);
96 if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
97 fprintf (stderr, "%s", "unable to free arguments");
107 uint32_t verf_flavor;
109 uint32_t accept_stat;
112 static void async_rpc_write(RPCConnection *rpc,
113 const char *buf, gsize len);
115 struct rpc_fail_reply {
119 uint32_t verf_flavor;
121 uint32_t accept_stat;
125 async_rpc_send_failure(RPCRequest *req, enum accept_stat stat)
127 struct rpc_fail_reply header;
129 fprintf(stderr, "Sending RPC failure status %d\n", stat);
131 header.xid = htonl(req->xid);
132 header.type = htonl(1); /* REPLY */
133 header.stat = htonl(MSG_ACCEPTED);
134 header.verf_flavor = 0;
136 header.accept_stat = htonl(stat);
138 uint32_t fragment = htonl(sizeof(header) | 0x80000000);
139 async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
140 async_rpc_write(req->connection, (const char *)&header, sizeof(header));
141 g_io_channel_flush(req->connection->channel, NULL);
143 if (req->raw_args != NULL)
144 g_string_free(req->raw_args, TRUE);
146 if (req->args != NULL) {
149 xdrmem_create(&xdr, buf, sizeof(buf), XDR_FREE);
150 if (!req->xdr_args_free(&xdr, req->args)) {
151 fprintf(stderr, "unable to free arguments");
159 nfs_program_3(RPCRequest *req)
161 RPCConnection *connection = req->connection;
162 uint32_t xid = req->xid;
163 const char *msg_buf = req->raw_args->str + req->raw_args_header_bytes;
164 size_t msg_len = req->raw_args->len - req->raw_args_header_bytes;
167 nfs_fh3 nfsproc3_getattr_3_arg;
168 setattr3args nfsproc3_setattr_3_arg;
169 diropargs3 nfsproc3_lookup_3_arg;
170 access3args nfsproc3_access_3_arg;
171 nfs_fh3 nfsproc3_readlink_3_arg;
172 read3args nfsproc3_read_3_arg;
173 write3args nfsproc3_write_3_arg;
174 create3args nfsproc3_create_3_arg;
175 mkdir3args nfsproc3_mkdir_3_arg;
176 symlink3args nfsproc3_symlink_3_arg;
177 mknod3args nfsproc3_mknod_3_arg;
178 diropargs3 nfsproc3_remove_3_arg;
179 diropargs3 nfsproc3_rmdir_3_arg;
180 rename3args nfsproc3_rename_3_arg;
181 link3args nfsproc3_link_3_arg;
182 readdir3args nfsproc3_readdir_3_arg;
183 readdirplus3args nfsproc3_readdirplus_3_arg;
184 nfs_fh3 nfsproc3_fsstat_3_arg;
185 nfs_fh3 nfsproc3_fsinfo_3_arg;
186 nfs_fh3 nfsproc3_pathconf_3_arg;
187 commit3args nfsproc3_commit_3_arg;
190 xdrproc_t _xdr_argument, _xdr_result;
191 char *(*local)(char *, struct svc_req *);
193 switch (req->req_proc) {
195 _xdr_argument = (xdrproc_t) xdr_void;
196 _xdr_result = (xdrproc_t) xdr_void;
197 local = (char *(*)(char *, struct svc_req *)) nfsproc3_null_3_svc;
200 case NFSPROC3_GETATTR:
201 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
202 _xdr_result = (xdrproc_t) xdr_getattr3res;
203 local = (char *(*)(char *, struct svc_req *)) nfsproc3_getattr_3_svc;
206 case NFSPROC3_SETATTR:
207 _xdr_argument = (xdrproc_t) xdr_setattr3args;
208 _xdr_result = (xdrproc_t) xdr_wccstat3;
209 local = (char *(*)(char *, struct svc_req *)) nfsproc3_setattr_3_svc;
212 case NFSPROC3_LOOKUP:
213 _xdr_argument = (xdrproc_t) xdr_diropargs3;
214 _xdr_result = (xdrproc_t) xdr_lookup3res;
215 local = (char *(*)(char *, struct svc_req *)) nfsproc3_lookup_3_svc;
218 case NFSPROC3_ACCESS:
219 _xdr_argument = (xdrproc_t) xdr_access3args;
220 _xdr_result = (xdrproc_t) xdr_access3res;
221 local = (char *(*)(char *, struct svc_req *)) nfsproc3_access_3_svc;
224 case NFSPROC3_READLINK:
225 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
226 _xdr_result = (xdrproc_t) xdr_readlink3res;
227 local = (char *(*)(char *, struct svc_req *)) nfsproc3_readlink_3_svc;
231 _xdr_argument = (xdrproc_t) xdr_read3args;
232 _xdr_result = (xdrproc_t) xdr_read3res;
233 local = (char *(*)(char *, struct svc_req *)) nfsproc3_read_3_svc;
237 _xdr_argument = (xdrproc_t) xdr_write3args;
238 _xdr_result = (xdrproc_t) xdr_write3res;
239 local = (char *(*)(char *, struct svc_req *)) nfsproc3_write_3_svc;
242 case NFSPROC3_CREATE:
243 _xdr_argument = (xdrproc_t) xdr_create3args;
244 _xdr_result = (xdrproc_t) xdr_diropres3;
245 local = (char *(*)(char *, struct svc_req *)) nfsproc3_create_3_svc;
249 _xdr_argument = (xdrproc_t) xdr_mkdir3args;
250 _xdr_result = (xdrproc_t) xdr_diropres3;
251 local = (char *(*)(char *, struct svc_req *)) nfsproc3_mkdir_3_svc;
254 case NFSPROC3_SYMLINK:
255 _xdr_argument = (xdrproc_t) xdr_symlink3args;
256 _xdr_result = (xdrproc_t) xdr_diropres3;
257 local = (char *(*)(char *, struct svc_req *)) nfsproc3_symlink_3_svc;
261 _xdr_argument = (xdrproc_t) xdr_mknod3args;
262 _xdr_result = (xdrproc_t) xdr_diropres3;
263 local = (char *(*)(char *, struct svc_req *)) nfsproc3_mknod_3_svc;
266 case NFSPROC3_REMOVE:
267 _xdr_argument = (xdrproc_t) xdr_diropargs3;
268 _xdr_result = (xdrproc_t) xdr_wccstat3;
269 local = (char *(*)(char *, struct svc_req *)) nfsproc3_remove_3_svc;
273 _xdr_argument = (xdrproc_t) xdr_diropargs3;
274 _xdr_result = (xdrproc_t) xdr_wccstat3;
275 local = (char *(*)(char *, struct svc_req *)) nfsproc3_rmdir_3_svc;
278 case NFSPROC3_RENAME:
279 _xdr_argument = (xdrproc_t) xdr_rename3args;
280 _xdr_result = (xdrproc_t) xdr_rename3res;
281 local = (char *(*)(char *, struct svc_req *)) nfsproc3_rename_3_svc;
285 _xdr_argument = (xdrproc_t) xdr_link3args;
286 _xdr_result = (xdrproc_t) xdr_link3res;
287 local = (char *(*)(char *, struct svc_req *)) nfsproc3_link_3_svc;
290 case NFSPROC3_READDIR:
291 _xdr_argument = (xdrproc_t) xdr_readdir3args;
292 _xdr_result = (xdrproc_t) xdr_readdir3res;
293 local = (char *(*)(char *, struct svc_req *)) nfsproc3_readdir_3_svc;
296 case NFSPROC3_READDIRPLUS:
297 _xdr_argument = (xdrproc_t) xdr_readdirplus3args;
298 _xdr_result = (xdrproc_t) xdr_readdirplus3res;
299 local = (char *(*)(char *, struct svc_req *)) nfsproc3_readdirplus_3_svc;
302 case NFSPROC3_FSSTAT:
303 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
304 _xdr_result = (xdrproc_t) xdr_fsstat3res;
305 local = (char *(*)(char *, struct svc_req *)) nfsproc3_fsstat_3_svc;
308 case NFSPROC3_FSINFO:
309 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
310 _xdr_result = (xdrproc_t) xdr_fsinfo3res;
311 local = (char *(*)(char *, struct svc_req *)) nfsproc3_fsinfo_3_svc;
314 case NFSPROC3_PATHCONF:
315 _xdr_argument = (xdrproc_t) xdr_nfs_fh3;
316 _xdr_result = (xdrproc_t) xdr_pathconf3res;
317 local = (char *(*)(char *, struct svc_req *)) nfsproc3_pathconf_3_svc;
320 case NFSPROC3_COMMIT:
321 _xdr_argument = (xdrproc_t) xdr_commit3args;
322 _xdr_result = (xdrproc_t) xdr_commit3res;
323 local = (char *(*)(char *, struct svc_req *)) nfsproc3_commit_3_svc;
327 async_rpc_send_failure(req, PROC_UNAVAIL);
331 /* Decode incoming message */
332 req->xdr_args_free = _xdr_argument;
333 req->args = g_new0(union argtype, 1);
335 xdrmem_create(&xdr_in, (char *)msg_buf, msg_len, XDR_DECODE);
336 if (!_xdr_argument(&xdr_in, req->args)) {
337 async_rpc_send_failure(req, GARBAGE_ARGS);
338 fprintf(stderr, "RPC decode error!\n");
342 /* Perform the call. */
343 result = (*local)((char *)req->args, NULL);
345 /* Encode result and send reply. */
346 static char reply_buf[MAX_RPC_MSGSIZE];
348 xdrmem_create(&xdr_out, reply_buf, MAX_RPC_MSGSIZE, XDR_ENCODE);
349 if (result != NULL && !_xdr_result(&xdr_out, result)) {
350 async_rpc_send_failure(req, SYSTEM_ERR);
353 struct rpc_reply header;
354 header.xid = htonl(xid);
355 header.type = htonl(1); /* REPLY */
356 header.stat = htonl(MSG_ACCEPTED);
357 header.verf_flavor = 0;
359 header.accept_stat = 0;
361 gsize msg_size = xdr_out.x_ops->x_getpostn(&xdr_out);
362 printf("Have an RPC reply of size %zd bytes\n", msg_size);
363 uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
364 async_rpc_write(connection, (const char *)&fragment, sizeof(fragment));
365 async_rpc_write(connection, (const char *)&header, sizeof(header));
366 async_rpc_write(connection, reply_buf, msg_size);
367 g_io_channel_flush(connection->channel, NULL);
370 xdr_in.x_op = XDR_FREE;
371 if (!_xdr_argument(&xdr_in, (caddr_t)req->args)) {
372 fprintf (stderr, "%s", "unable to free arguments");
377 bluesky_flushd_invoke(fs);
382 /* Enhanced, asynchronous-friendly RPC layer. This is a replacement for the
383 * built-in sunrpc parsing and dispatch that will allow for processing multiple
384 * requests at the same time. */
385 static GMainContext *main_context;
386 static GMainLoop *main_loop;
388 static async_rpc_init()
390 main_context = g_main_context_new();
391 main_loop = g_main_loop_new(main_context, FALSE);
394 struct rpc_call_header {
408 /* Decode an RPC message and process it. Returns a boolean indicating whether
409 * the message could be processed; if false, an unrecoverable error occurred
410 * and the transport should be closed. */
411 static gboolean async_rpc_dispatch(RPCConnection *rpc)
414 GString *msg = rpc->msgbuf;
415 const char *buf = msg->str;
417 if (msg->len < sizeof(struct rpc_call_header)) {
418 fprintf(stderr, "Short RPC message: only %zd bytes!\n", msg->len);
422 struct rpc_call_header *header = (struct rpc_call_header *)(msg->str);
423 uint32_t xid = ntohl(header->xid);
425 if (ntohl(header->mtype) != 0) {
426 /* Not an RPC call */
430 if (ntohl(header->rpcvers) != 2) {
434 RPCRequest *req = g_new0(RPCRequest, 1);
435 req->connection = rpc;
438 if (ntohl(header->prog) != NFS_PROGRAM) {
439 async_rpc_send_failure(req, PROG_UNAVAIL);
441 } else if (ntohl(header->vers) != NFS_V3) {
442 /* FIXME: Should be PROG_MISMATCH */
443 async_rpc_send_failure(req, PROG_UNAVAIL);
447 uint32_t proc = ntohl(header->proc);
449 /* Next, skip over authentication headers. */
450 buf += sizeof(struct rpc_call_header);
451 for (i = 0; i < 2; i++) {
452 struct rpc_auth *auth = (struct rpc_auth *)buf;
453 if (buf - msg->str + sizeof(struct rpc_auth) > msg->len)
456 gsize authsize = ntohl(auth->len) + sizeof(struct rpc_auth);
457 if (authsize > MAX_RPC_MSGSIZE)
463 if (buf - msg->str > msg->len)
466 printf("Dispatching RPC procedure %d...\n", proc);
469 req->raw_args_header_bytes = buf - msg->str;
470 req->req_proc = ntohl(header->proc);
471 rpc->msgbuf = g_string_new("");
478 /* Write the given data to the RPC socket. */
479 static void async_rpc_write(RPCConnection *rpc,
480 const char *buf, gsize len)
484 switch (g_io_channel_write_chars(rpc->channel, buf, len,
486 case G_IO_STATUS_ERROR:
487 case G_IO_STATUS_EOF:
488 case G_IO_STATUS_AGAIN:
489 fprintf(stderr, "Error writing to socket!\n");
491 case G_IO_STATUS_NORMAL:
498 // g_io_channel_flush(rpc->channel, NULL);
501 static gboolean async_rpc_do_read(GIOChannel *channel,
502 GIOCondition condition,
505 RPCConnection *rpc = (RPCConnection *)data;
507 gsize bytes_to_read = 0; /* Number of bytes to attempt to read. */
509 /* If we have not yet read in the fragment header, do that first. This is
510 * 4 bytes that indicates the number of bytes in the message to follow
511 * (with the high bit set if this is the last fragment making up the
513 if (rpc->frag_len == 0) {
514 bytes_to_read = 4 - rpc->frag_hdr_bytes;
516 bytes_to_read = rpc->frag_len & 0x7fffffff;
519 if (bytes_to_read > MAX_RPC_MSGSIZE
520 || rpc->msgbuf->len + bytes_to_read > MAX_RPC_MSGSIZE)
522 fprintf(stderr, "Excessive fragment size for RPC: %zd bytes\n",
524 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
528 gsize bytes_read = 0;
529 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len + bytes_to_read);
530 char *buf = &rpc->msgbuf->str[rpc->msgbuf->len - bytes_to_read];
531 switch (g_io_channel_read_chars(rpc->channel, buf,
532 bytes_to_read, &bytes_read, NULL)) {
533 case G_IO_STATUS_NORMAL:
535 case G_IO_STATUS_AGAIN:
537 case G_IO_STATUS_EOF:
538 if (bytes_read == bytes_to_read)
540 /* else fall through */
541 case G_IO_STATUS_ERROR:
542 fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
543 g_io_channel_unix_get_fd(rpc->channel));
544 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
548 g_assert(bytes_read >= 0 && bytes_read <= bytes_to_read);
550 g_string_set_size(rpc->msgbuf,
551 rpc->msgbuf->len - (bytes_to_read - bytes_read));
553 if (rpc->frag_len == 0) {
554 /* Handle reading in the fragment header. If we've read the complete
555 * header, store the fragment size. */
556 rpc->frag_hdr_bytes += bytes_read;
557 if (rpc->frag_hdr_bytes == 4) {
558 memcpy((char *)&rpc->frag_len,
559 &rpc->msgbuf->str[rpc->msgbuf->len - 4], 4);
560 rpc->frag_len = ntohl(rpc->frag_len);
561 g_string_set_size(rpc->msgbuf, rpc->msgbuf->len - 4);
562 rpc->frag_hdr_bytes = 0;
563 g_print("RPC fragment header: %08x\n", rpc->frag_len);
566 /* We were reading in the fragment body. */
567 rpc->frag_len -= bytes_read;
569 if (rpc->frag_len = 0x80000000) {
570 /* We have a complete message since this was the last fragment and
571 * there are no more bytes in it. Dispatch the message. */
572 g_print("Complete RPC message: %zd bytes\n", rpc->msgbuf->len);
573 if (!async_rpc_dispatch(rpc)) {
574 fprintf(stderr, "Invalid RPC message, closing channel\n");
575 g_io_channel_shutdown(rpc->channel, TRUE, NULL);
579 g_string_set_size(rpc->msgbuf, 0);
586 static gboolean async_rpc_do_accept(GIOChannel *channel,
587 GIOCondition condition,
590 int fd = g_io_channel_unix_get_fd(channel);
591 struct sockaddr_in addr;
592 socklen_t addrlen = sizeof(addr);
594 g_print("Received new connection on fd %d!\n", fd);
595 int nfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
597 fprintf(stderr, "Error accepting connection: %m\n");
601 RPCConnection *rpc = g_new0(RPCConnection, 1);
602 rpc->channel = g_io_channel_unix_new(nfd);
603 rpc->msgbuf = g_string_new("");
604 g_io_channel_set_encoding(rpc->channel, NULL, NULL);
605 GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
606 g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
608 g_source_attach(source, main_context);
609 g_source_unref(source);
614 static async_rpc_register_listening(int fd)
616 GIOChannel *channel = g_io_channel_unix_new(fd);
617 g_io_channel_set_encoding(channel, NULL, NULL);
618 GSource *source = g_io_create_watch(channel, G_IO_IN);
619 g_source_set_callback(source, (GSourceFunc)async_rpc_do_accept,
621 g_source_attach(source, main_context);
622 g_source_unref(source);
625 static gpointer async_rpc_run(gpointer data)
627 g_print("Starting NFS main loop...\n");
628 g_main_loop_run(main_loop);
638 pmap_unset (MOUNT_PROGRAM, MOUNT_V3);
640 transp = svcudp_create(RPC_ANYSOCK);
641 if (transp == NULL) {
642 fprintf(stderr, "%s", "cannot create udp service.");
645 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_UDP)) {
646 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, udp).");
650 transp = svctcp_create(RPC_ANYSOCK, 0, 0);
651 if (transp == NULL) {
652 fprintf(stderr, "%s", "cannot create tcp service.");
655 if (!svc_register(transp, MOUNT_PROGRAM, MOUNT_V3, mount_program_3, IPPROTO_TCP)) {
656 fprintf(stderr, "%s", "unable to register (MOUNT_PROGRAM, MOUNT_V3, tcp).");
660 /* NFS protocol (version 3) */
661 pmap_unset (NFS_PROGRAM, NFS_V3);
663 int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
665 fprintf(stderr, "Unable to create NFS TCP socket: %m\n");
670 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n));
672 struct sockaddr_in addr;
673 addr.sin_family = AF_INET;
674 addr.sin_port = htons(NFS_SERVICE_PORT);
675 addr.sin_addr.s_addr = INADDR_ANY;
676 if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
677 fprintf(stderr, "Unable to bind to NFS TCP address: %m\n");
681 if (listen(fd, SOMAXCONN) < 0) {
682 fprintf(stderr, "Unable to listen on NFS TCP socket: %m\n");
686 if (!pmap_set(NFS_PROGRAM, NFS_V3, IPPROTO_TCP, NFS_SERVICE_PORT)) {
687 fprintf(stderr, "Could not register NFS RPC service!\n");
691 async_rpc_register_listening(fd);
693 g_thread_create(async_rpc_run, NULL, TRUE, NULL);