diff options
Diffstat (limited to 'rpc')
59 files changed, 25184 insertions, 0 deletions
diff --git a/rpc/Makefile.am b/rpc/Makefile.am new file mode 100644 index 00000000000..183b7a0352f --- /dev/null +++ b/rpc/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = xdr rpc-lib rpc-transport diff --git a/rpc/rpc-lib/Makefile.am b/rpc/rpc-lib/Makefile.am new file mode 100644 index 00000000000..af437a64d6d --- /dev/null +++ b/rpc/rpc-lib/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/rpc/rpc-lib/src/Makefile.am b/rpc/rpc-lib/src/Makefile.am new file mode 100644 index 00000000000..35c9db07e7f --- /dev/null +++ b/rpc/rpc-lib/src/Makefile.am @@ -0,0 +1,29 @@ +lib_LTLIBRARIES = libgfrpc.la + +libgfrpc_la_SOURCES = auth-unix.c rpcsvc-auth.c rpcsvc.c auth-null.c \ + rpc-transport.c xdr-rpc.c xdr-rpcclnt.c rpc-clnt.c auth-glusterfs.c \ + rpc-drc.c rpc-clnt-ping.c \ + autoscale-threads.c mgmt-pmap.c + +EXTRA_DIST = libgfrpc.sym + +libgfrpc_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la \ + $(top_builddir)/rpc/xdr/src/libgfxdr.la +libgfrpc_la_LDFLAGS = -version-info $(LIBGFRPC_LT_VERSION) $(GF_LDFLAGS) \ + -export-symbols $(top_srcdir)/rpc/rpc-lib/src/libgfrpc.sym + +libgfrpc_la_HEADERS = rpcsvc.h rpc-transport.h xdr-common.h xdr-rpc.h xdr-rpcclnt.h \ + rpc-clnt.h rpcsvc-common.h protocol-common.h rpc-drc.h rpc-clnt-ping.h \ + rpc-lib-messages.h + +libgfrpc_ladir = $(includedir)/glusterfs/rpc + +AM_CPPFLAGS = $(GF_CPPFLAGS) -I$(top_srcdir)/libglusterfs/src \ + -I$(top_srcdir)/rpc/xdr/src \ + -I$(top_builddir)/rpc/xdr/src \ + -DRPC_TRANSPORTDIR=\"$(libdir)/glusterfs/$(PACKAGE_VERSION)/rpc-transport\" \ + -I$(top_srcdir)/contrib/rbtree + +AM_CFLAGS = -Wall $(GF_CFLAGS) + +CLEANFILES = *~ diff --git a/rpc/rpc-lib/src/auth-glusterfs.c b/rpc/rpc-lib/src/auth-glusterfs.c new file mode 100644 index 00000000000..69a96f7512f --- /dev/null +++ b/rpc/rpc-lib/src/auth-glusterfs.c @@ -0,0 +1,386 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#include <glusterfs/dict.h> +#include "xdr-rpc.h" +#include "xdr-common.h" +#include "rpc-common-xdr.h" +#include "glusterfs4-xdr.h" + +/* V1 */ + +ssize_t +xdr_to_glusterfs_auth(char *buf, struct auth_glusterfs_parms *req) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!buf) || (!req)) + return -1; + + xdrmem_create(&xdr, buf, sizeof(struct auth_glusterfs_parms), XDR_DECODE); + if (!xdr_auth_glusterfs_parms(&xdr, req)) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs parameters"); + ret = -1; + goto ret; + } + + ret = (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)); +ret: + return ret; +} +int +auth_glusterfs_request_init(rpcsvc_request_t *req, void *priv) +{ + return 0; +} + +int +auth_glusterfs_authenticate(rpcsvc_request_t *req, void *priv) +{ + struct auth_glusterfs_parms au = { + 0, + }; + + int ret = RPCSVC_AUTH_REJECT; + int j = 0; + int i = 0; + int gidcount = 0; + + if (!req) + return ret; + + ret = xdr_to_glusterfs_auth(req->cred.authdata, &au); + if (ret == -1) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs credentials"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + req->pid = au.pid; + req->uid = au.uid; + req->gid = au.gid; + req->lk_owner.len = 8; + { + for (i = 0; i < req->lk_owner.len; i++, j += 8) + req->lk_owner.data[i] = (char)((au.lk_owner >> j) & 0xff); + } + req->auxgidcount = au.ngrps; + + if (req->auxgidcount > 16) { + gf_log("", GF_LOG_WARNING, + "more than 16 aux gids found, failing authentication"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + if (req->auxgidcount > SMALL_GROUP_COUNT) { + req->auxgidlarge = GF_CALLOC(req->auxgidcount, sizeof(req->auxgids[0]), + gf_common_mt_auxgids); + req->auxgids = req->auxgidlarge; + } else { + req->auxgids = req->auxgidsmall; + } + + if (!req->auxgids) { + gf_log("auth-glusterfs", GF_LOG_WARNING, "cannot allocate gid list"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + for (gidcount = 0; gidcount < au.ngrps; ++gidcount) + req->auxgids[gidcount] = au.groups[gidcount]; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "Auth Info: pid: %u, uid: %d" + ", gid: %d, owner: %s", + req->pid, req->uid, req->gid, lkowner_utoa(&req->lk_owner)); + ret = RPCSVC_AUTH_ACCEPT; +err: + return ret; +} + +rpcsvc_auth_ops_t auth_glusterfs_ops = { + .transport_init = NULL, + .request_init = auth_glusterfs_request_init, + .authenticate = auth_glusterfs_authenticate}; + +rpcsvc_auth_t rpcsvc_auth_glusterfs = {.authname = "AUTH_GLUSTERFS", + .authnum = AUTH_GLUSTERFS, + .authops = &auth_glusterfs_ops, + .authprivate = NULL}; + +rpcsvc_auth_t * +rpcsvc_auth_glusterfs_init(rpcsvc_t *svc, dict_t *options) +{ + return &rpcsvc_auth_glusterfs; +} + +/* V2 */ + +ssize_t +xdr_to_glusterfs_auth_v2(char *buf, struct auth_glusterfs_parms_v2 *req) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!buf) || (!req)) + return -1; + + xdrmem_create(&xdr, buf, GF_MAX_AUTH_BYTES, XDR_DECODE); + if (!xdr_auth_glusterfs_parms_v2(&xdr, req)) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs v2 parameters"); + ret = -1; + goto ret; + } + + ret = (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)); +ret: + return ret; +} +int +auth_glusterfs_v2_request_init(rpcsvc_request_t *req, void *priv) +{ + return 0; +} + +int +auth_glusterfs_v2_authenticate(rpcsvc_request_t *req, void *priv) +{ + struct auth_glusterfs_parms_v2 au = { + 0, + }; + int ret = RPCSVC_AUTH_REJECT; + int i = 0; + int max_groups = 0; + int max_lk_owner_len = 0; + + if (!req) + return ret; + + ret = xdr_to_glusterfs_auth_v2(req->cred.authdata, &au); + if (ret == -1) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs credentials"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + req->pid = au.pid; + req->uid = au.uid; + req->gid = au.gid; + req->lk_owner.len = au.lk_owner.lk_owner_len; + req->auxgidcount = au.groups.groups_len; + + /* the number of groups and size of lk_owner depend on each other */ + max_groups = GF_AUTH_GLUSTERFS_MAX_GROUPS(req->lk_owner.len, + AUTH_GLUSTERFS_v2); + max_lk_owner_len = GF_AUTH_GLUSTERFS_MAX_LKOWNER(req->auxgidcount, + AUTH_GLUSTERFS_v2); + + if (req->auxgidcount > max_groups) { + gf_log("", GF_LOG_WARNING, + "more than max aux gids found (%d) , truncating it " + "to %d and continuing", + au.groups.groups_len, max_groups); + req->auxgidcount = max_groups; + } + + if (req->lk_owner.len > max_lk_owner_len) { + gf_log("", GF_LOG_WARNING, + "lkowner field to big (%d), depends on the number of " + "groups (%d), failing authentication", + req->lk_owner.len, req->auxgidcount); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + if (req->auxgidcount > SMALL_GROUP_COUNT) { + req->auxgidlarge = GF_CALLOC(req->auxgidcount, sizeof(req->auxgids[0]), + gf_common_mt_auxgids); + req->auxgids = req->auxgidlarge; + } else { + req->auxgids = req->auxgidsmall; + } + + if (!req->auxgids) { + gf_log("auth-glusterfs-v2", GF_LOG_WARNING, "cannot allocate gid list"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + for (i = 0; i < req->auxgidcount; ++i) + req->auxgids[i] = au.groups.groups_val[i]; + + for (i = 0; i < au.lk_owner.lk_owner_len; ++i) + req->lk_owner.data[i] = au.lk_owner.lk_owner_val[i]; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "Auth Info: pid: %u, uid: %d" + ", gid: %d, owner: %s", + req->pid, req->uid, req->gid, lkowner_utoa(&req->lk_owner)); + ret = RPCSVC_AUTH_ACCEPT; +err: + /* TODO: instead use alloca() for these variables */ + free(au.groups.groups_val); + free(au.lk_owner.lk_owner_val); + + return ret; +} + +rpcsvc_auth_ops_t auth_glusterfs_ops_v2 = { + .transport_init = NULL, + .request_init = auth_glusterfs_v2_request_init, + .authenticate = auth_glusterfs_v2_authenticate}; + +rpcsvc_auth_t rpcsvc_auth_glusterfs_v2 = {.authname = "AUTH_GLUSTERFS-v2", + .authnum = AUTH_GLUSTERFS_v2, + .authops = &auth_glusterfs_ops_v2, + .authprivate = NULL}; + +rpcsvc_auth_t * +rpcsvc_auth_glusterfs_v2_init(rpcsvc_t *svc, dict_t *options) +{ + return &rpcsvc_auth_glusterfs_v2; +} + +/* V3 */ + +ssize_t +xdr_to_glusterfs_auth_v3(char *buf, struct auth_glusterfs_params_v3 *req) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!buf) || (!req)) + return -1; + + xdrmem_create(&xdr, buf, GF_MAX_AUTH_BYTES, XDR_DECODE); + if (!xdr_auth_glusterfs_params_v3(&xdr, req)) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs v3 parameters"); + ret = -1; + goto ret; + } + + ret = (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)); +ret: + return ret; +} + +int +auth_glusterfs_v3_request_init(rpcsvc_request_t *req, void *priv) +{ + return 0; +} + +int +auth_glusterfs_v3_authenticate(rpcsvc_request_t *req, void *priv) +{ + struct auth_glusterfs_params_v3 au = { + 0, + }; + int ret = RPCSVC_AUTH_REJECT; + int i = 0; + int max_groups = 0; + int max_lk_owner_len = 0; + + if (!req) + return ret; + + ret = xdr_to_glusterfs_auth_v3(req->cred.authdata, &au); + if (ret == -1) { + gf_log("", GF_LOG_WARNING, "failed to decode glusterfs credentials"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + req->pid = au.pid; + req->uid = au.uid; + req->gid = au.gid; + req->lk_owner.len = au.lk_owner.lk_owner_len; + req->auxgidcount = au.groups.groups_len; + + /* the number of groups and size of lk_owner depend on each other */ + max_groups = GF_AUTH_GLUSTERFS_MAX_GROUPS(req->lk_owner.len, + AUTH_GLUSTERFS_v3); + max_lk_owner_len = GF_AUTH_GLUSTERFS_MAX_LKOWNER(req->auxgidcount, + AUTH_GLUSTERFS_v3); + + if (req->auxgidcount > max_groups) { + gf_log("", GF_LOG_WARNING, + "more than max aux gids found (%d) , truncating it " + "to %d and continuing", + au.groups.groups_len, max_groups); + req->auxgidcount = max_groups; + } + + if (req->lk_owner.len > max_lk_owner_len) { + gf_log("", GF_LOG_WARNING, + "lkowner field to big (%d), depends on the number of " + "groups (%d), failing authentication", + req->lk_owner.len, req->auxgidcount); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + if (req->auxgidcount > SMALL_GROUP_COUNT) { + req->auxgidlarge = GF_CALLOC(req->auxgidcount, sizeof(req->auxgids[0]), + gf_common_mt_auxgids); + req->auxgids = req->auxgidlarge; + } else { + req->auxgids = req->auxgidsmall; + } + + if (!req->auxgids) { + gf_log("auth-glusterfs-v2", GF_LOG_WARNING, "cannot allocate gid list"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + for (i = 0; i < req->auxgidcount; ++i) + req->auxgids[i] = au.groups.groups_val[i]; + + for (i = 0; i < au.lk_owner.lk_owner_len; ++i) + req->lk_owner.data[i] = au.lk_owner.lk_owner_val[i]; + + /* All new things, starting glusterfs-4.0.0 */ + req->flags = au.flags; + req->ctime.tv_sec = au.ctime_sec; + req->ctime.tv_nsec = au.ctime_nsec; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "Auth Info: pid: %u, uid: %d" + ", gid: %d, owner: %s, flags: %d", + req->pid, req->uid, req->gid, lkowner_utoa(&req->lk_owner), + req->flags); + ret = RPCSVC_AUTH_ACCEPT; +err: + /* TODO: instead use alloca() for these variables */ + free(au.groups.groups_val); + free(au.lk_owner.lk_owner_val); + + return ret; +} + +rpcsvc_auth_ops_t auth_glusterfs_ops_v3 = { + .transport_init = NULL, + .request_init = auth_glusterfs_v3_request_init, + .authenticate = auth_glusterfs_v3_authenticate}; + +rpcsvc_auth_t rpcsvc_auth_glusterfs_v3 = {.authname = "AUTH_GLUSTERFS-v3", + .authnum = AUTH_GLUSTERFS_v3, + .authops = &auth_glusterfs_ops_v3, + .authprivate = NULL}; + +rpcsvc_auth_t * +rpcsvc_auth_glusterfs_v3_init(rpcsvc_t *svc, dict_t *options) +{ + return &rpcsvc_auth_glusterfs_v3; +} diff --git a/rpc/rpc-lib/src/auth-null.c b/rpc/rpc-lib/src/auth-null.c new file mode 100644 index 00000000000..6d059b9da50 --- /dev/null +++ b/rpc/rpc-lib/src/auth-null.c @@ -0,0 +1,40 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#include <glusterfs/dict.h> + +int +auth_null_request_init(rpcsvc_request_t *req, void *priv) +{ + return 0; +} + +int +auth_null_authenticate(rpcsvc_request_t *req, void *priv) +{ + /* Always succeed. */ + return RPCSVC_AUTH_ACCEPT; +} + +rpcsvc_auth_ops_t auth_null_ops = {.transport_init = NULL, + .request_init = auth_null_request_init, + .authenticate = auth_null_authenticate}; + +rpcsvc_auth_t rpcsvc_auth_null = {.authname = "AUTH_NULL", + .authnum = AUTH_NULL, + .authops = &auth_null_ops, + .authprivate = NULL}; + +rpcsvc_auth_t * +rpcsvc_auth_null_init(rpcsvc_t *svc, dict_t *options) +{ + return &rpcsvc_auth_null; +} diff --git a/rpc/rpc-lib/src/auth-unix.c b/rpc/rpc-lib/src/auth-unix.c new file mode 100644 index 00000000000..61d475a5e84 --- /dev/null +++ b/rpc/rpc-lib/src/auth-unix.c @@ -0,0 +1,66 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#include <glusterfs/dict.h> +#include "xdr-rpc.h" + +int +auth_unix_request_init(rpcsvc_request_t *req, void *priv) +{ + return 0; +} + +int +auth_unix_authenticate(rpcsvc_request_t *req, void *priv) +{ + int ret = RPCSVC_AUTH_REJECT; + struct authunix_parms aup; + char machname[MAX_MACHINE_NAME]; + + if (!req) + return ret; + + req->auxgids = req->auxgidsmall; + ret = xdr_to_auth_unix_cred(req->cred.authdata, req->cred.datalen, &aup, + machname, req->auxgids); + if (ret == -1) { + gf_log("", GF_LOG_WARNING, "failed to decode unix credentials"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + req->uid = aup.aup_uid; + req->gid = aup.aup_gid; + req->auxgidcount = aup.aup_len; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "Auth Info: machine name: %s, uid: %d" + ", gid: %d", + machname, req->uid, req->gid); + ret = RPCSVC_AUTH_ACCEPT; +err: + return ret; +} + +rpcsvc_auth_ops_t auth_unix_ops = {.transport_init = NULL, + .request_init = auth_unix_request_init, + .authenticate = auth_unix_authenticate}; + +rpcsvc_auth_t rpcsvc_auth_unix = {.authname = "AUTH_UNIX", + .authnum = AUTH_UNIX, + .authops = &auth_unix_ops, + .authprivate = NULL}; + +rpcsvc_auth_t * +rpcsvc_auth_unix_init(rpcsvc_t *svc, dict_t *options) +{ + return &rpcsvc_auth_unix; +} diff --git a/rpc/rpc-lib/src/autoscale-threads.c b/rpc/rpc-lib/src/autoscale-threads.c new file mode 100644 index 00000000000..a954ae7a27a --- /dev/null +++ b/rpc/rpc-lib/src/autoscale-threads.c @@ -0,0 +1,22 @@ +/* + Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <glusterfs/gf-event.h> +#include "rpcsvc.h" + +void +rpcsvc_autoscale_threads(glusterfs_ctx_t *ctx, rpcsvc_t *rpc, int incr) +{ + struct event_pool *pool = ctx->event_pool; + int thread_count = pool->eventthreadcount; + + pool->auto_thread_count += incr; + (void)gf_event_reconfigure_threads(pool, thread_count + incr); +} diff --git a/rpc/rpc-lib/src/libgfrpc.sym b/rpc/rpc-lib/src/libgfrpc.sym new file mode 100644 index 00000000000..e026d80259b --- /dev/null +++ b/rpc/rpc-lib/src/libgfrpc.sym @@ -0,0 +1,68 @@ +is_rpc_clnt_disconnected +rpcclnt_cbk_program_register +rpc_clnt_cleanup_and_start +rpc_clnt_connection_cleanup +rpc_clnt_disable +rpc_clnt_new +rpc_clnt_reconfig +rpc_clnt_reconnect +rpc_clnt_reconnect_cleanup +rpc_clnt_ref +rpc_clnt_register_notify +rpc_clnt_start +rpc_clnt_submit +rpc_clnt_unref +rpc_reply_to_xdr +rpcsvc_auth_array +rpcsvc_auth_check +rpcsvc_auth_reconf +rpcsvc_auth_unix_auxgids +rpcsvc_callback_submit +rpcsvc_create_listener +rpcsvc_create_listeners +rpcsvc_drc_init +rpcsvc_drc_priv +rpcsvc_drc_reconfigure +rpcsvc_get_program_vector_sizer +rpcsvc_init +rpcsvc_destroy +rpcsvc_init_options +rpcsvc_listener_destroy +rpcsvc_program_register +rpcsvc_program_register_portmap +rpcsvc_program_register_rpcbind6 +rpcsvc_program_unregister +rpcsvc_program_unregister_portmap +rpcsvc_program_unregister_rpcbind6 +rpcsvc_reconfigure_options +rpcsvc_register_notify +rpcsvc_register_portmap_enabled +rpcsvc_request_submit +rpcsvc_set_outstanding_rpc_limit +rpcsvc_set_throttle_on +rpcsvc_submit_generic +rpcsvc_submit_message +rpcsvc_transport_peeraddr +rpcsvc_transport_peername +rpcsvc_transport_privport_check +rpcsvc_transport_unix_options_build +rpcsvc_transport_volume_allowed +rpcsvc_transport_connect +rpcsvc_transport_getpeeraddr +rpcsvc_unregister_notify +rpcsvc_volume_allowed +rpc_transport_count +rpc_transport_connect +rpc_transport_disconnect +rpc_transport_get_peeraddr +rpc_transport_inet_options_build +rpc_transport_keepalive_options_set +rpc_transport_notify +rpc_transport_pollin_alloc +rpc_transport_pollin_destroy +rpc_transport_ref +rpc_transport_unix_options_build +rpc_transport_unref +rpc_clnt_mgmt_pmap_signout +rpcsvc_autoscale_threads +rpcsvc_statedump diff --git a/rpc/rpc-lib/src/mgmt-pmap.c b/rpc/rpc-lib/src/mgmt-pmap.c new file mode 100644 index 00000000000..25a7148e5a3 --- /dev/null +++ b/rpc/rpc-lib/src/mgmt-pmap.c @@ -0,0 +1,147 @@ +/* + Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "portmap-xdr.h" +#include "protocol-common.h" +#include "rpc-clnt.h" +#include "xdr-generic.h" + +/* Defining a minimal RPC client program for portmap signout + */ +char *clnt_pmap_signout_procs[GF_PMAP_MAXVALUE] = { + [GF_PMAP_SIGNOUT] = "SIGNOUT", +}; + +rpc_clnt_prog_t clnt_pmap_signout_prog = { + .progname = "Gluster Portmap", + .prognum = GLUSTER_PMAP_PROGRAM, + .progver = GLUSTER_PMAP_VERSION, + .procnames = clnt_pmap_signout_procs, +}; + +static int +mgmt_pmap_signout_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + pmap_signout_rsp rsp = { + 0, + }; + int ret = 0; + call_frame_t *frame = NULL; + + frame = myframe; + if (-1 == req->rpc_status) { + rsp.op_ret = -1; + rsp.op_errno = EINVAL; + goto out; + } + + ret = xdr_to_generic(*iov, &rsp, (xdrproc_t)xdr_pmap_signout_rsp); + if (ret < 0) { + gf_log(THIS->name, GF_LOG_ERROR, "XDR decoding failed"); + rsp.op_ret = -1; + rsp.op_errno = EINVAL; + goto out; + } + + if (-1 == rsp.op_ret) { + gf_log(THIS->name, GF_LOG_ERROR, + "failed to register the port with glusterd"); + goto out; + } +out: + if (frame) { + STACK_DESTROY(frame->root); + } + + return 0; +} + +int +rpc_clnt_mgmt_pmap_signout(glusterfs_ctx_t *ctx, char *brickname) +{ + int ret = 0; + pmap_signout_req req = { + 0, + }; + call_frame_t *frame = NULL; + cmd_args_t *cmd_args = NULL; + char brick_name[PATH_MAX] = { + 0, + }; + struct iovec iov = { + 0, + }; + struct iobuf *iobuf = NULL; + struct iobref *iobref = NULL; + ssize_t xdr_size = 0; + + frame = create_frame(THIS, ctx->pool); + cmd_args = &ctx->cmd_args; + + if (!cmd_args->brick_port && (!cmd_args->brick_name || !brickname)) { + gf_log("fsd-mgmt", GF_LOG_DEBUG, + "portmapper signout arguments not given"); + goto out; + } + + if (cmd_args->volfile_server_transport && + !strcmp(cmd_args->volfile_server_transport, "rdma")) { + snprintf(brick_name, sizeof(brick_name), "%s.rdma", + cmd_args->brick_name); + req.brick = brick_name; + } else { + if (brickname) + req.brick = brickname; + else + req.brick = cmd_args->brick_name; + } + + req.port = cmd_args->brick_port; + req.rdma_port = cmd_args->brick_port2; + + /* mgmt_submit_request is not available in libglusterfs. + * Need to serialize and submit manually. + */ + iobref = iobref_new(); + if (!iobref) { + goto out; + } + + xdr_size = xdr_sizeof((xdrproc_t)xdr_pmap_signout_req, &req); + iobuf = iobuf_get2(ctx->iobuf_pool, xdr_size); + if (!iobuf) { + goto out; + }; + + iobref_add(iobref, iobuf); + + iov.iov_base = iobuf->ptr; + iov.iov_len = iobuf_pagesize(iobuf); + + /* Create the xdr payload */ + ret = xdr_serialize_generic(iov, &req, (xdrproc_t)xdr_pmap_signout_req); + if (ret == -1) { + gf_log(THIS->name, GF_LOG_WARNING, "failed to create XDR payload"); + goto out; + } + iov.iov_len = ret; + + ret = rpc_clnt_submit(ctx->mgmt, &clnt_pmap_signout_prog, GF_PMAP_SIGNOUT, + mgmt_pmap_signout_cbk, &iov, 1, NULL, 0, iobref, + frame, NULL, 0, NULL, 0, NULL); +out: + if (iobref) + iobref_unref(iobref); + + if (iobuf) + iobuf_unref(iobuf); + return ret; +} diff --git a/rpc/rpc-lib/src/protocol-common.h b/rpc/rpc-lib/src/protocol-common.h new file mode 100644 index 00000000000..0cb5862e9a9 --- /dev/null +++ b/rpc/rpc-lib/src/protocol-common.h @@ -0,0 +1,382 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _PROTOCOL_COMMON_H +#define _PROTOCOL_COMMON_H + +enum gf_fop_procnum { + GFS3_OP_NULL, /* 0 */ + GFS3_OP_STAT, + GFS3_OP_READLINK, + GFS3_OP_MKNOD, + GFS3_OP_MKDIR, + GFS3_OP_UNLINK, + GFS3_OP_RMDIR, + GFS3_OP_SYMLINK, + GFS3_OP_RENAME, + GFS3_OP_LINK, + GFS3_OP_TRUNCATE, + GFS3_OP_OPEN, + GFS3_OP_READ, + GFS3_OP_WRITE, + GFS3_OP_STATFS, + GFS3_OP_FLUSH, + GFS3_OP_FSYNC, + GFS3_OP_SETXATTR, + GFS3_OP_GETXATTR, + GFS3_OP_REMOVEXATTR, + GFS3_OP_OPENDIR, + GFS3_OP_FSYNCDIR, + GFS3_OP_ACCESS, + GFS3_OP_CREATE, + GFS3_OP_FTRUNCATE, + GFS3_OP_FSTAT, + GFS3_OP_LK, + GFS3_OP_LOOKUP, + GFS3_OP_READDIR, + GFS3_OP_INODELK, + GFS3_OP_FINODELK, + GFS3_OP_ENTRYLK, + GFS3_OP_FENTRYLK, + GFS3_OP_XATTROP, + GFS3_OP_FXATTROP, + GFS3_OP_FGETXATTR, + GFS3_OP_FSETXATTR, + GFS3_OP_RCHECKSUM, + GFS3_OP_SETATTR, + GFS3_OP_FSETATTR, + GFS3_OP_READDIRP, + GFS3_OP_RELEASE, + GFS3_OP_RELEASEDIR, + GFS3_OP_FREMOVEXATTR, + GFS3_OP_FALLOCATE, + GFS3_OP_DISCARD, + GFS3_OP_ZEROFILL, + GFS3_OP_IPC, + GFS3_OP_SEEK, + GFS3_OP_COMPOUND, + GFS3_OP_LEASE, + GFS3_OP_GETACTIVELK, + GFS3_OP_SETACTIVELK, + GFS3_OP_ICREATE, + GFS3_OP_NAMELINK, + GFS3_OP_PUT, + GFS3_OP_COPY_FILE_RANGE, + GFS3_OP_MAXVALUE, +}; + +enum gf_handshake_procnum { + GF_HNDSK_NULL, + GF_HNDSK_SETVOLUME, + GF_HNDSK_GETSPEC, + GF_HNDSK_PING, + GF_HNDSK_SET_LK_VER, + GF_HNDSK_EVENT_NOTIFY, + GF_HNDSK_GET_VOLUME_INFO, + GF_HNDSK_GET_SNAPSHOT_INFO, + GF_HNDSK_MAXVALUE, +}; + +enum gf_pmap_procnum { + GF_PMAP_NULL = 0, + GF_PMAP_PORTBYBRICK, + GF_PMAP_BRICKBYPORT, + /* + * SIGNUP is not used, and shouldn't be used. It was kept here only + * to avoid changing the numbers for things that come after it in this + * list. + */ + GF_PMAP_SIGNUP, + GF_PMAP_SIGNIN, + GF_PMAP_SIGNOUT, + GF_PMAP_MAXVALUE, +}; + +enum gf_aggregator_procnum { + GF_AGGREGATOR_NULL = 0, + GF_AGGREGATOR_LOOKUP, + GF_AGGREGATOR_GETLIMIT, + GF_AGGREGATOR_MAXVALUE, +}; + +enum gf_pmap_port_type { + GF_PMAP_PORT_FREE = 0, + GF_PMAP_PORT_FOREIGN, /* it actually means, not sure who is using it, but it + is in-use */ + GF_PMAP_PORT_LEASED, + GF_PMAP_PORT_ANY, + GF_PMAP_PORT_BRICKSERVER, /* port used by brick process */ +}; +typedef enum gf_pmap_port_type gf_pmap_port_type_t; + +enum gf_probe_resp { + GF_PROBE_SUCCESS, + GF_PROBE_LOCALHOST, + GF_PROBE_FRIEND, + GF_PROBE_ANOTHER_CLUSTER, + GF_PROBE_VOLUME_CONFLICT, + GF_PROBE_SAME_UUID, + GF_PROBE_UNKNOWN_PEER, + GF_PROBE_ADD_FAILED, + GF_PROBE_QUORUM_NOT_MET, + GF_PROBE_MISSED_SNAP_CONFLICT, + GF_PROBE_SNAP_CONFLICT, + GF_PROBE_FRIEND_DETACHING, +}; + +enum gf_deprobe_resp { + GF_DEPROBE_SUCCESS, + GF_DEPROBE_LOCALHOST, + GF_DEPROBE_NOT_FRIEND, + GF_DEPROBE_BRICK_EXIST, + GF_DEPROBE_FRIEND_DOWN, + GF_DEPROBE_QUORUM_NOT_MET, + GF_DEPROBE_FRIEND_DETACHING, + GF_DEPROBE_SNAP_BRICK_EXIST, +}; + +enum gf_cbk_procnum { + GF_CBK_NULL = 0, + GF_CBK_FETCHSPEC, + GF_CBK_INO_FLUSH, + GF_CBK_EVENT_NOTIFY, + GF_CBK_GET_SNAPS, + GF_CBK_CACHE_INVALIDATION, + GF_CBK_CHILD_UP, + GF_CBK_CHILD_DOWN, + GF_CBK_RECALL_LEASE, + GF_CBK_STATEDUMP, + GF_CBK_INODELK_CONTENTION, + GF_CBK_ENTRYLK_CONTENTION, + GF_CBK_MAXVALUE, +}; + +enum gluster_cli_procnum { + GLUSTER_CLI_NULL, /* 0 */ + GLUSTER_CLI_PROBE, + GLUSTER_CLI_DEPROBE, + GLUSTER_CLI_LIST_FRIENDS, + GLUSTER_CLI_CREATE_VOLUME, + GLUSTER_CLI_GET_VOLUME, + GLUSTER_CLI_GET_NEXT_VOLUME, + GLUSTER_CLI_DELETE_VOLUME, + GLUSTER_CLI_START_VOLUME, + GLUSTER_CLI_STOP_VOLUME, + GLUSTER_CLI_RENAME_VOLUME, + GLUSTER_CLI_DEFRAG_VOLUME, + GLUSTER_CLI_SET_VOLUME, + GLUSTER_CLI_ADD_BRICK, + GLUSTER_CLI_REMOVE_BRICK, + GLUSTER_CLI_REPLACE_BRICK, + GLUSTER_CLI_LOG_ROTATE, + GLUSTER_CLI_GETSPEC, + GLUSTER_CLI_PMAP_PORTBYBRICK, + GLUSTER_CLI_SYNC_VOLUME, + GLUSTER_CLI_RESET_VOLUME, + GLUSTER_CLI_FSM_LOG, + GLUSTER_CLI_GSYNC_SET, + GLUSTER_CLI_PROFILE_VOLUME, + GLUSTER_CLI_QUOTA, + GLUSTER_CLI_TOP_VOLUME, + GLUSTER_CLI_GETWD, + GLUSTER_CLI_STATUS_VOLUME, + GLUSTER_CLI_STATUS_ALL, + GLUSTER_CLI_MOUNT, + GLUSTER_CLI_UMOUNT, + GLUSTER_CLI_HEAL_VOLUME, + GLUSTER_CLI_STATEDUMP_VOLUME, + GLUSTER_CLI_LIST_VOLUME, + GLUSTER_CLI_CLRLOCKS_VOLUME, + GLUSTER_CLI_UUID_RESET, + GLUSTER_CLI_UUID_GET, + GLUSTER_CLI_COPY_FILE, + GLUSTER_CLI_SYS_EXEC, + GLUSTER_CLI_SNAP, + GLUSTER_CLI_BARRIER_VOLUME, + GLUSTER_CLI_GET_VOL_OPT, + GLUSTER_CLI_GANESHA, + GLUSTER_CLI_BITROT, + GLUSTER_CLI_ATTACH_TIER, + GLUSTER_CLI_TIER, + GLUSTER_CLI_GET_STATE, + GLUSTER_CLI_RESET_BRICK, + GLUSTER_CLI_REMOVE_TIER_BRICK, + GLUSTER_CLI_ADD_TIER_BRICK, + GLUSTER_CLI_MAXVALUE, +}; + +enum glusterd_mgmt_procnum { + GLUSTERD_MGMT_NULL, /* 0 */ + GLUSTERD_MGMT_CLUSTER_LOCK, + GLUSTERD_MGMT_CLUSTER_UNLOCK, + GLUSTERD_MGMT_STAGE_OP, + GLUSTERD_MGMT_COMMIT_OP, + GLUSTERD_MGMT_MAXVALUE, +}; + +enum glusterd_friend_procnum { + GLUSTERD_FRIEND_NULL, /* 0 */ + GLUSTERD_PROBE_QUERY, + GLUSTERD_FRIEND_ADD, + GLUSTERD_FRIEND_REMOVE, + GLUSTERD_FRIEND_UPDATE, + GLUSTERD_FRIEND_MAXVALUE, +}; + +enum glusterd_brick_procnum { + GLUSTERD_BRICK_NULL, /* 0 */ + GLUSTERD_BRICK_TERMINATE, + GLUSTERD_BRICK_XLATOR_INFO, + GLUSTERD_BRICK_XLATOR_OP, + GLUSTERD_BRICK_STATUS, + GLUSTERD_BRICK_OP, + GLUSTERD_BRICK_XLATOR_DEFRAG, + GLUSTERD_NODE_PROFILE, + GLUSTERD_NODE_STATUS, + GLUSTERD_VOLUME_BARRIER_OP, + GLUSTERD_BRICK_BARRIER, + GLUSTERD_NODE_BITROT, + GLUSTERD_BRICK_ATTACH, + GLUSTERD_DUMP_METRICS, + GLUSTERD_SVC_ATTACH, + GLUSTERD_SVC_DETACH, + GLUSTERD_BRICK_MAXVALUE, +}; + +enum glusterd_mgmt_hndsk_procnum { + GD_MGMT_HNDSK_NULL, + GD_MGMT_HNDSK_VERSIONS, + GD_MGMT_HNDSK_VERSIONS_ACK, + GD_MGMT_HNDSK_MAXVALUE, +}; + +typedef enum { + GF_SHD_OP_INVALID, + GF_SHD_OP_HEAL_INDEX, + GF_SHD_OP_HEAL_FULL, + GF_SHD_OP_INDEX_SUMMARY, + GF_SHD_OP_HEALED_FILES, + GF_SHD_OP_HEAL_FAILED_FILES, + GF_SHD_OP_SPLIT_BRAIN_FILES, + GF_SHD_OP_STATISTICS, + GF_SHD_OP_STATISTICS_HEAL_COUNT, + GF_SHD_OP_STATISTICS_HEAL_COUNT_PER_REPLICA, + GF_SHD_OP_SBRAIN_HEAL_FROM_BIGGER_FILE, + GF_SHD_OP_SBRAIN_HEAL_FROM_BRICK, + GF_SHD_OP_HEAL_ENABLE, + GF_SHD_OP_HEAL_DISABLE, + GF_SHD_OP_SBRAIN_HEAL_FROM_LATEST_MTIME, + GF_SHD_OP_GRANULAR_ENTRY_HEAL_ENABLE, + GF_SHD_OP_GRANULAR_ENTRY_HEAL_DISABLE, + GF_SHD_OP_HEAL_SUMMARY, +} gf_xl_afr_op_t; + +struct gf_gsync_detailed_status_ { + char node[NAME_MAX]; + char master[NAME_MAX]; + char brick[PATH_MAX]; + char slave_user[NAME_MAX]; + char slave[NAME_MAX]; + char slave_node[NAME_MAX]; + char worker_status[NAME_MAX]; + char crawl_status[NAME_MAX]; + char last_synced[NAME_MAX]; + char last_synced_utc[NAME_MAX]; + char entry[NAME_MAX]; + char data[NAME_MAX]; + char meta[NAME_MAX]; + char failures[NAME_MAX]; + char checkpoint_time[NAME_MAX]; + char checkpoint_time_utc[NAME_MAX]; + char checkpoint_completed[NAME_MAX]; + char checkpoint_completion_time[NAME_MAX]; + char checkpoint_completion_time_utc[NAME_MAX]; + char brick_host_uuid[NAME_MAX]; + char slavekey[NAME_MAX]; + char session_slave[NAME_MAX]; +}; + +enum glusterd_mgmt_v3_procnum { + GLUSTERD_MGMT_V3_NULL, /* 0 */ + GLUSTERD_MGMT_V3_LOCK, + GLUSTERD_MGMT_V3_PRE_VALIDATE, + GLUSTERD_MGMT_V3_BRICK_OP, + GLUSTERD_MGMT_V3_COMMIT, + GLUSTERD_MGMT_V3_POST_COMMIT, + GLUSTERD_MGMT_V3_POST_VALIDATE, + GLUSTERD_MGMT_V3_UNLOCK, + GLUSTERD_MGMT_V3_MAXVALUE, +}; + +typedef struct gf_gsync_detailed_status_ gf_gsync_status_t; + +enum gf_get_volume_info_type { + GF_GET_VOLUME_NONE, /* 0 */ + GF_GET_VOLUME_UUID +}; + +typedef enum gf_get_volume_info_type gf_get_volume_info_type; + +enum gf_get_snapshot_info_type { + GF_GET_SNAPSHOT_LIST, +}; +typedef enum gf_get_snapshot_info_type gf_get_snapshot_info_type; + +enum gf_getspec_flags_type { GF_GETSPEC_FLAG_SERVERS_LIST = 1 }; +typedef enum gf_getspec_flags_type gf_getspec_flags_type; + +#define GLUSTER_HNDSK_PROGRAM 14398633 /* Completely random */ +#define GLUSTER_HNDSK_VERSION 2 /* 0.0.2 */ + +#define GLUSTER_PMAP_PROGRAM 34123456 +#define GLUSTER_PMAP_VERSION 1 + +#define GLUSTER_CBK_PROGRAM 52743234 /* Completely random */ +#define GLUSTER_CBK_VERSION 1 /* 0.0.1 */ + +#define GLUSTER_FOP_PROGRAM 1298437 /* Completely random */ +#define GLUSTER_FOP_VERSION 330 /* 3.3.0 */ +#define GLUSTER_FOP_PROCCNT GFS3_OP_MAXVALUE + +#define GLUSTER_FOP_VERSION_v2 400 /* 4.0.0 */ + +/* Aggregator */ +#define GLUSTER_AGGREGATOR_PROGRAM 29852134 /* Completely random */ +#define GLUSTER_AGGREGATOR_VERSION 1 + +/* Second version */ +#define GD_MGMT_PROGRAM 1238433 /* Completely random */ +#define GD_MGMT_VERSION 2 /* 0.0.2 */ + +#define GD_FRIEND_PROGRAM 1238437 /* Completely random */ +#define GD_FRIEND_VERSION 2 /* 0.0.2 */ + +#define GLUSTER_CLI_PROGRAM 1238463 /* Completely random */ +#define GLUSTER_CLI_VERSION 2 /* 0.0.2 */ + +#define GD_BRICK_PROGRAM 4867634 /*Completely random*/ +#define GD_BRICK_VERSION 2 + +/* Third version */ +#define GD_MGMT_V3_VERSION 3 + +/* OP-VERSION handshake */ +#define GD_MGMT_HNDSK_PROGRAM 1239873 /* Completely random */ +#define GD_MGMT_HNDSK_VERSION 1 + +#define GD_VOLUME_NAME_MAX \ + ((NAME_MAX + 1) - 5) /* Maximum size of volume name */ +#define GD_VOLUME_NAME_MAX_TIER \ + (GD_VOLUME_NAME_MAX + 5) /* +5 needed for '-hot' \ + and '-cold' suffixes*/ + +#define GLUSTER_PROCESS_UUID_FMT \ + "CTX_ID:%s-GRAPH_ID:%d-PID:%d-HOST:%s-PC_NAME:%s-RECON_NO:%s" +#endif /* !_PROTOCOL_COMMON_H */ diff --git a/rpc/rpc-lib/src/rpc-clnt-ping.c b/rpc/rpc-lib/src/rpc-clnt-ping.c new file mode 100644 index 00000000000..31f17841bea --- /dev/null +++ b/rpc/rpc-lib/src/rpc-clnt-ping.c @@ -0,0 +1,357 @@ +/* + Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpc-clnt.h" +#include "rpc-clnt-ping.h" +#include <glusterfs/byte-order.h> +#include "xdr-rpcclnt.h" +#include "rpc-transport.h" +#include "protocol-common.h" +#include <glusterfs/mem-pool.h> +#include "xdr-rpc.h" +#include "rpc-common-xdr.h" +#include <glusterfs/timespec.h> + +char *clnt_ping_procs[GF_DUMP_MAXVALUE] = { + [GF_DUMP_PING] = "NULL", +}; +struct rpc_clnt_program clnt_ping_prog = { + .progname = "GF-DUMP", + .prognum = GLUSTER_DUMP_PROGRAM, + .progver = GLUSTER_DUMP_VERSION, + .procnames = clnt_ping_procs, +}; + +struct ping_local { + struct rpc_clnt *rpc; + struct timespec submit_time; +}; + +/* Must be called under conn->lock */ +static int +__rpc_clnt_rearm_ping_timer(struct rpc_clnt *rpc, gf_timer_cbk_t cbk) +{ + rpc_clnt_connection_t *conn = &rpc->conn; + rpc_transport_t *trans = conn->trans; + struct timespec timeout = { + 0, + }; + gf_timer_t *timer = NULL; + + if (conn->ping_timer) { + gf_log_callingfn("", GF_LOG_CRITICAL, + "%s: ping timer event already scheduled", + conn->trans->peerinfo.identifier); + return -1; + } + + timeout.tv_sec = conn->ping_timeout; + timeout.tv_nsec = 0; + + rpc_clnt_ref(rpc); + timer = gf_timer_call_after(rpc->ctx, timeout, cbk, (void *)rpc); + if (timer == NULL) { + gf_log(trans->name, GF_LOG_WARNING, "unable to setup ping timer"); + + /* This unref can't be the last. We just took a ref few lines + * above. So this can be performed under conn->lock. */ + rpc_clnt_unref(rpc); + conn->ping_started = 0; + return -1; + } + + conn->ping_timer = timer; + conn->ping_started = 1; + return 0; +} + +/* Must be called under conn->lock */ +int +rpc_clnt_remove_ping_timer_locked(struct rpc_clnt *rpc) +{ + rpc_clnt_connection_t *conn = &rpc->conn; + gf_timer_t *timer = NULL; + + if (conn->ping_timer) { + timer = conn->ping_timer; + conn->ping_timer = NULL; + gf_timer_call_cancel(rpc->ctx, timer); + conn->ping_started = 0; + return 1; + } + + /* This is to account for rpc_clnt_disable that might have set + * conn->trans to NULL. */ + if (conn->trans) + gf_log_callingfn("", GF_LOG_DEBUG, + "%s: ping timer event " + "already removed", + conn->trans->peerinfo.identifier); + + return 0; +} + +static void +rpc_clnt_start_ping(void *rpc_ptr); + +void +rpc_clnt_ping_timer_expired(void *rpc_ptr) +{ + struct rpc_clnt *rpc = NULL; + rpc_transport_t *trans = NULL; + rpc_clnt_connection_t *conn = NULL; + int disconnect = 0; + struct timespec current = { + 0, + }; + int unref = 0; + + rpc = (struct rpc_clnt *)rpc_ptr; + conn = &rpc->conn; + trans = conn->trans; + + if (!trans) { + gf_log("ping-timer", GF_LOG_WARNING, "transport not initialized"); + goto out; + } + + timespec_now_realtime(¤t); + pthread_mutex_lock(&conn->lock); + { + unref = rpc_clnt_remove_ping_timer_locked(rpc); + + if (((current.tv_sec - conn->last_received.tv_sec) < + conn->ping_timeout) || + ((current.tv_sec - conn->last_sent.tv_sec) < conn->ping_timeout)) { + gf_log(trans->name, GF_LOG_TRACE, + "ping timer expired but transport activity " + "detected - not bailing transport"); + if (__rpc_clnt_rearm_ping_timer(rpc, rpc_clnt_ping_timer_expired) == + -1) { + gf_log(trans->name, GF_LOG_WARNING, + "unable to setup ping timer"); + } + } else { + conn->ping_started = 0; + disconnect = 1; + } + } + pthread_mutex_unlock(&conn->lock); + + if (unref) + rpc_clnt_unref(rpc); + + if (disconnect) { + gf_log(trans->name, GF_LOG_CRITICAL, + "server %s has not responded in the last %d " + "seconds, disconnecting.", + trans->peerinfo.identifier, conn->ping_timeout); + + rpc_transport_disconnect(conn->trans, _gf_false); + } + +out: + return; +} + +int +rpc_clnt_ping_cbk(struct rpc_req *req, struct iovec *iov, int count, + void *myframe) +{ + struct ping_local *local = NULL; + xlator_t *this = NULL; + rpc_clnt_connection_t *conn = NULL; + call_frame_t *frame = NULL; + int unref = 0; + gf_boolean_t call_notify = _gf_false; + + struct timespec now; + struct timespec delta; + int64_t latency_msec = 0; + int ret = 0; + + if (!myframe) { + gf_log(THIS->name, GF_LOG_WARNING, "frame with the request is NULL"); + goto out; + } + + frame = myframe; + this = frame->this; + local = frame->local; + conn = &local->rpc->conn; + + timespec_now(&now); + timespec_sub(&local->submit_time, &now, &delta); + latency_msec = delta.tv_sec * 1000 + delta.tv_nsec / 1000000; + + gf_log(THIS->name, GF_LOG_DEBUG, "Ping latency is %" PRIu64 "ms", + latency_msec); + call_notify = _gf_true; + + pthread_mutex_lock(&conn->lock); + { + unref = rpc_clnt_remove_ping_timer_locked(local->rpc); + if (req->rpc_status == -1) { + conn->ping_started = 0; + pthread_mutex_unlock(&conn->lock); + if (unref) { + gf_log(this->name, GF_LOG_WARNING, + "socket or ib related error"); + + } else { + /* timer expired and transport bailed out */ + gf_log(this->name, GF_LOG_WARNING, "socket disconnected"); + } + goto after_unlock; + } + + if (__rpc_clnt_rearm_ping_timer(local->rpc, rpc_clnt_start_ping) == + -1) { + /* unlock before logging error */ + pthread_mutex_unlock(&conn->lock); + gf_log(this->name, GF_LOG_WARNING, "failed to set the ping timer"); + } else { + /* just unlock the mutex */ + pthread_mutex_unlock(&conn->lock); + } + } +after_unlock: + if (call_notify) { + ret = local->rpc->notifyfn(local->rpc, this, RPC_CLNT_PING, + (void *)(uintptr_t)latency_msec); + if (ret) { + gf_log(this->name, GF_LOG_WARNING, "RPC_CLNT_PING notify failed"); + } + } +out: + if (unref) + rpc_clnt_unref(local->rpc); + + if (frame) { + GF_FREE(frame->local); + frame->local = NULL; + STACK_DESTROY(frame->root); + } + return 0; +} + +int +rpc_clnt_ping(struct rpc_clnt *rpc) +{ + call_frame_t *frame = NULL; + int32_t ret = -1; + rpc_clnt_connection_t *conn = NULL; + struct ping_local *local = NULL; + + conn = &rpc->conn; + local = GF_MALLOC(sizeof(struct ping_local), gf_common_ping_local_t); + if (!local) + return ret; + frame = create_frame(THIS, THIS->ctx->pool); + if (!frame) { + GF_FREE(local); + return ret; + } + + local->rpc = rpc; + timespec_now(&local->submit_time); + frame->local = local; + + ret = rpc_clnt_submit(rpc, &clnt_ping_prog, GF_DUMP_PING, rpc_clnt_ping_cbk, + NULL, 0, NULL, 0, NULL, frame, NULL, 0, NULL, 0, + NULL); + if (ret) { + /* FIXME: should we free the frame here? Methinks so! */ + gf_log(THIS->name, GF_LOG_ERROR, "failed to start ping timer"); + } else { + /* ping successfully queued in list of saved frames + * for the connection*/ + pthread_mutex_lock(&conn->lock); + conn->pingcnt++; + pthread_mutex_unlock(&conn->lock); + } + + return ret; +} + +static void +rpc_clnt_start_ping(void *rpc_ptr) +{ + struct rpc_clnt *rpc = NULL; + rpc_clnt_connection_t *conn = NULL; + int frame_count = 0; + int unref = 0; + + rpc = (struct rpc_clnt *)rpc_ptr; + conn = &rpc->conn; + + if (conn->ping_timeout == 0) { + gf_log(THIS->name, GF_LOG_DEBUG, + "ping timeout is 0," + " returning"); + return; + } + + pthread_mutex_lock(&conn->lock); + { + unref = rpc_clnt_remove_ping_timer_locked(rpc); + + if (conn->saved_frames) { + GF_ASSERT(conn->saved_frames->count >= 0); + /* treat the case where conn->saved_frames is NULL + as no pending frames */ + frame_count = conn->saved_frames->count; + } + + if ((frame_count == 0) || !conn->connected) { + gf_log(THIS->name, GF_LOG_DEBUG, + "returning as transport is already disconnected" + " OR there are no frames (%d || %d)", + !conn->connected, frame_count); + + pthread_mutex_unlock(&conn->lock); + if (unref) + rpc_clnt_unref(rpc); + return; + } + + if (__rpc_clnt_rearm_ping_timer(rpc, rpc_clnt_ping_timer_expired) == + -1) { + gf_log(THIS->name, GF_LOG_WARNING, "unable to setup ping timer"); + pthread_mutex_unlock(&conn->lock); + if (unref) + rpc_clnt_unref(rpc); + return; + } + } + pthread_mutex_unlock(&conn->lock); + if (unref) + rpc_clnt_unref(rpc); + + rpc_clnt_ping(rpc); +} + +void +rpc_clnt_check_and_start_ping(struct rpc_clnt *rpc) +{ + char start_ping = 0; + + pthread_mutex_lock(&rpc->conn.lock); + { + if (!rpc->conn.ping_started) + start_ping = 1; + } + pthread_mutex_unlock(&rpc->conn.lock); + + if (start_ping) + rpc_clnt_start_ping((void *)rpc); + + return; +} diff --git a/rpc/rpc-lib/src/rpc-clnt-ping.h b/rpc/rpc-lib/src/rpc-clnt-ping.h new file mode 100644 index 00000000000..e5466a828c2 --- /dev/null +++ b/rpc/rpc-lib/src/rpc-clnt-ping.h @@ -0,0 +1,16 @@ +/* + Copyright (c) 2014 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +struct rpc_clnt; +#define RPC_DEFAULT_PING_TIMEOUT 30 +void +rpc_clnt_check_and_start_ping(struct rpc_clnt *rpc_ptr); +int +rpc_clnt_remove_ping_timer_locked(struct rpc_clnt *rpc); diff --git a/rpc/rpc-lib/src/rpc-clnt.c b/rpc/rpc-lib/src/rpc-clnt.c new file mode 100644 index 00000000000..517037c4a5d --- /dev/null +++ b/rpc/rpc-lib/src/rpc-clnt.c @@ -0,0 +1,1979 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#define RPC_CLNT_DEFAULT_REQUEST_COUNT 512 + +#include "rpc-clnt.h" +#include "rpc-clnt-ping.h" +#include <glusterfs/byte-order.h> +#include "xdr-rpcclnt.h" +#include "rpc-transport.h" +#include "protocol-common.h" +#include <glusterfs/mem-pool.h> +#include "xdr-rpc.h" +#include "rpc-common-xdr.h" + +void +rpc_clnt_reply_deinit(struct rpc_req *req, struct mem_pool *pool); + +struct saved_frame * +__saved_frames_get_timedout(struct saved_frames *frames, uint32_t timeout, + struct timeval *current) +{ + struct saved_frame *bailout_frame = NULL, *tmp = NULL; + + if (!list_empty(&frames->sf.list)) { + tmp = list_entry(frames->sf.list.next, typeof(*tmp), list); + if ((tmp->saved_at.tv_sec + timeout) <= current->tv_sec) { + bailout_frame = tmp; + list_del_init(&bailout_frame->list); + frames->count--; + } + } + + return bailout_frame; +} + +static int +_is_lock_fop(struct saved_frame *sframe) +{ + int fop = 0; + + if (SFRAME_GET_PROGNUM(sframe) == GLUSTER_FOP_PROGRAM && + SFRAME_GET_PROGVER(sframe) == GLUSTER_FOP_VERSION) + fop = SFRAME_GET_PROCNUM(sframe); + + return ((fop == GFS3_OP_LK) || (fop == GFS3_OP_INODELK) || + (fop == GFS3_OP_FINODELK) || (fop == GFS3_OP_ENTRYLK) || + (fop == GFS3_OP_FENTRYLK)); +} + +static struct saved_frame * +__saved_frames_put(struct saved_frames *frames, void *frame, + struct rpc_req *rpcreq) +{ + struct saved_frame *saved_frame = mem_get( + rpcreq->conn->rpc_clnt->saved_frames_pool); + + if (!saved_frame) { + goto out; + } + /* THIS should be saved and set back */ + + INIT_LIST_HEAD(&saved_frame->list); + + saved_frame->capital_this = THIS; + saved_frame->frame = frame; + saved_frame->rpcreq = rpcreq; + gettimeofday(&saved_frame->saved_at, NULL); + memset(&saved_frame->rsp, 0, sizeof(rpc_transport_rsp_t)); + + if (_is_lock_fop(saved_frame)) + list_add_tail(&saved_frame->list, &frames->lk_sf.list); + else + list_add_tail(&saved_frame->list, &frames->sf.list); + + frames->count++; + +out: + return saved_frame; +} + +static void +call_bail(void *data) +{ + rpc_transport_t *trans = NULL; + struct rpc_clnt *clnt = NULL; + rpc_clnt_connection_t *conn = NULL; + struct timeval current; + struct list_head list; + struct saved_frame *saved_frame = NULL; + struct saved_frame *trav = NULL; + struct saved_frame *tmp = NULL; + char frame_sent[GF_TIMESTR_SIZE] = { + 0, + }; + struct timespec timeout = { + 0, + }; + char peerid[UNIX_PATH_MAX] = {0}; + gf_boolean_t need_unref = _gf_false; + + GF_VALIDATE_OR_GOTO("client", data, out); + + clnt = data; + + conn = &clnt->conn; + pthread_mutex_lock(&conn->lock); + { + trans = conn->trans; + if (trans) { + (void)snprintf(peerid, sizeof(peerid), "%s", + conn->trans->peerinfo.identifier); + } + } + pthread_mutex_unlock(&conn->lock); + /*rpc_clnt_connection_cleanup will be unwinding all saved frames, + * bailed or otherwise*/ + if (!trans) + goto out; + + gettimeofday(¤t, NULL); + INIT_LIST_HEAD(&list); + + pthread_mutex_lock(&conn->lock); + { + /* Chaining to get call-always functionality from + call-once timer */ + if (conn->timer) { + timeout.tv_sec = 10; + timeout.tv_nsec = 0; + + /* Ref rpc as it's added to timer event queue */ + rpc_clnt_ref(clnt); + gf_timer_call_cancel(clnt->ctx, conn->timer); + conn->timer = gf_timer_call_after(clnt->ctx, timeout, call_bail, + (void *)clnt); + + if (conn->timer == NULL) { + gf_log(conn->name, GF_LOG_WARNING, + "Cannot create bailout timer for %s", peerid); + need_unref = _gf_true; + } + } + + do { + saved_frame = __saved_frames_get_timedout( + conn->saved_frames, conn->frame_timeout, ¤t); + if (saved_frame) + list_add(&saved_frame->list, &list); + + } while (saved_frame); + } + pthread_mutex_unlock(&conn->lock); + + if (list_empty(&list)) + goto out; + + list_for_each_entry_safe(trav, tmp, &list, list) + { + gf_time_fmt_tv(frame_sent, sizeof frame_sent, &trav->saved_at, + gf_timefmt_FT); + + gf_log(conn->name, GF_LOG_ERROR, + "bailing out frame type(%s), op(%s(%d)), xid = 0x%x, " + "unique = %" PRIu64 ", sent = %s, timeout = %d for %s", + trav->rpcreq->prog->progname, + (trav->rpcreq->prog->procnames) + ? trav->rpcreq->prog->procnames[trav->rpcreq->procnum] + : "--", + trav->rpcreq->procnum, trav->rpcreq->xid, + ((call_frame_t *)(trav->frame))->root->unique, frame_sent, + conn->frame_timeout, peerid); + + clnt = rpc_clnt_ref(clnt); + trav->rpcreq->rpc_status = -1; + trav->rpcreq->cbkfn(trav->rpcreq, NULL, 0, trav->frame); + + rpc_clnt_reply_deinit(trav->rpcreq, clnt->reqpool); + clnt = rpc_clnt_unref(clnt); + list_del_init(&trav->list); + mem_put(trav); + } +out: + rpc_clnt_unref(clnt); + if (need_unref) + rpc_clnt_unref(clnt); + return; +} + +/* to be called with conn->lock held */ +static struct saved_frame * +__save_frame(struct rpc_clnt *rpc_clnt, call_frame_t *frame, + struct rpc_req *rpcreq) +{ + rpc_clnt_connection_t *conn = &rpc_clnt->conn; + struct timespec timeout = { + 0, + }; + struct saved_frame *saved_frame = __saved_frames_put(conn->saved_frames, + frame, rpcreq); + + if (saved_frame == NULL) { + goto out; + } + + /* TODO: make timeout configurable */ + if (conn->timer == NULL) { + timeout.tv_sec = 10; + timeout.tv_nsec = 0; + rpc_clnt_ref(rpc_clnt); + conn->timer = gf_timer_call_after(rpc_clnt->ctx, timeout, call_bail, + (void *)rpc_clnt); + } + +out: + return saved_frame; +} + +struct saved_frames * +saved_frames_new(void) +{ + struct saved_frames *saved_frames = NULL; + + saved_frames = GF_CALLOC(1, sizeof(*saved_frames), + gf_common_mt_rpcclnt_savedframe_t); + if (!saved_frames) { + return NULL; + } + + INIT_LIST_HEAD(&saved_frames->sf.list); + INIT_LIST_HEAD(&saved_frames->lk_sf.list); + + return saved_frames; +} + +int +__saved_frame_copy(struct saved_frames *frames, int64_t callid, + struct saved_frame *saved_frame) +{ + struct saved_frame *tmp = NULL; + int ret = -1; + + if (!saved_frame) { + ret = 0; + goto out; + } + + list_for_each_entry(tmp, &frames->sf.list, list) + { + if (tmp->rpcreq->xid == callid) { + *saved_frame = *tmp; + ret = 0; + goto out; + } + } + + list_for_each_entry(tmp, &frames->lk_sf.list, list) + { + if (tmp->rpcreq->xid == callid) { + *saved_frame = *tmp; + ret = 0; + goto out; + } + } + +out: + return ret; +} + +struct saved_frame * +__saved_frame_get(struct saved_frames *frames, int64_t callid) +{ + struct saved_frame *saved_frame = NULL; + struct saved_frame *tmp = NULL; + + list_for_each_entry(tmp, &frames->sf.list, list) + { + if (tmp->rpcreq->xid == callid) { + list_del_init(&tmp->list); + frames->count--; + saved_frame = tmp; + goto out; + } + } + + list_for_each_entry(tmp, &frames->lk_sf.list, list) + { + if (tmp->rpcreq->xid == callid) { + list_del_init(&tmp->list); + frames->count--; + saved_frame = tmp; + goto out; + } + } + +out: + if (saved_frame) { + THIS = saved_frame->capital_this; + } + + return saved_frame; +} + +void +saved_frames_unwind(struct saved_frames *saved_frames) +{ + struct saved_frame *trav = NULL; + struct saved_frame *tmp = NULL; + char timestr[GF_TIMESTR_SIZE] = { + 0, + }; + + list_splice_init(&saved_frames->lk_sf.list, &saved_frames->sf.list); + + list_for_each_entry_safe(trav, tmp, &saved_frames->sf.list, list) + { + gf_time_fmt_tv(timestr, sizeof timestr, &trav->saved_at, gf_timefmt_FT); + + if (!trav->rpcreq || !trav->rpcreq->prog) + continue; + + gf_log_callingfn( + trav->rpcreq->conn->name, GF_LOG_ERROR, + "forced unwinding frame type(%s) op(%s(%d)) " + "called at %s (xid=0x%x)", + trav->rpcreq->prog->progname, + ((trav->rpcreq->prog->procnames) + ? trav->rpcreq->prog->procnames[trav->rpcreq->procnum] + : "--"), + trav->rpcreq->procnum, timestr, trav->rpcreq->xid); + saved_frames->count--; + + trav->rpcreq->rpc_status = -1; + trav->rpcreq->cbkfn(trav->rpcreq, NULL, 0, trav->frame); + + rpc_clnt_reply_deinit(trav->rpcreq, + trav->rpcreq->conn->rpc_clnt->reqpool); + + list_del_init(&trav->list); + mem_put(trav); + } +} + +void +saved_frames_destroy(struct saved_frames *frames) +{ + if (!frames) + return; + + saved_frames_unwind(frames); + + GF_FREE(frames); +} + +void +rpc_clnt_reconnect(void *conn_ptr) +{ + rpc_transport_t *trans = NULL; + rpc_clnt_connection_t *conn = NULL; + struct timespec ts = {0, 0}; + struct rpc_clnt *clnt = NULL; + gf_boolean_t need_unref = _gf_false; + gf_boolean_t canceled_unref = _gf_false; + + conn = conn_ptr; + clnt = conn->rpc_clnt; + pthread_mutex_lock(&conn->lock); + { + trans = conn->trans; + if (!trans) + goto out_unlock; + + if (conn->reconnect) { + if (!gf_timer_call_cancel(clnt->ctx, conn->reconnect)) + canceled_unref = _gf_true; + } + conn->reconnect = 0; + + if ((conn->connected == 0) && !clnt->disabled) { + ts.tv_sec = 3; + ts.tv_nsec = 0; + + gf_log(conn->name, GF_LOG_TRACE, "attempting reconnect"); + (void)rpc_transport_connect(trans, conn->config.remote_port); + rpc_clnt_ref(clnt); + conn->reconnect = gf_timer_call_after(clnt->ctx, ts, + rpc_clnt_reconnect, conn); + if (!conn->reconnect) { + need_unref = _gf_true; + gf_log(conn->name, GF_LOG_ERROR, + "Error adding to timer event queue"); + } + } else { + gf_log(conn->name, GF_LOG_TRACE, "breaking reconnect chain"); + } + } +out_unlock: + pthread_mutex_unlock(&conn->lock); + + rpc_clnt_unref(clnt); + if (need_unref) + rpc_clnt_unref(clnt); + if (canceled_unref) + rpc_clnt_unref(clnt); + return; +} + +int +rpc_clnt_fill_request_info(struct rpc_clnt *clnt, rpc_request_info_t *info) +{ + struct saved_frame saved_frame; + int ret = -1; + + pthread_mutex_lock(&clnt->conn.lock); + { + ret = __saved_frame_copy(clnt->conn.saved_frames, info->xid, + &saved_frame); + } + pthread_mutex_unlock(&clnt->conn.lock); + + if (ret == -1) { + gf_log(clnt->conn.name, GF_LOG_CRITICAL, + "cannot lookup the saved " + "frame corresponding to xid (%d)", + info->xid); + goto out; + } + + info->prognum = saved_frame.rpcreq->prog->prognum; + info->procnum = saved_frame.rpcreq->procnum; + info->progver = saved_frame.rpcreq->prog->progver; + info->rpc_req = saved_frame.rpcreq; + info->rsp = saved_frame.rsp; + + ret = 0; +out: + return ret; +} + +int +rpc_clnt_reconnect_cleanup(rpc_clnt_connection_t *conn) +{ + struct rpc_clnt *clnt = NULL; + int ret = 0; + gf_boolean_t reconnect_unref = _gf_false; + + if (!conn) { + goto out; + } + + clnt = conn->rpc_clnt; + + pthread_mutex_lock(&conn->lock); + { + if (conn->reconnect) { + ret = gf_timer_call_cancel(clnt->ctx, conn->reconnect); + if (!ret) { + reconnect_unref = _gf_true; + conn->cleanup_gen++; + } + conn->reconnect = NULL; + } + } + pthread_mutex_unlock(&conn->lock); + + if (reconnect_unref) + rpc_clnt_unref(clnt); + +out: + return 0; +} + +/* + * client_protocol_cleanup - cleanup function + * @trans: transport object + * + */ +int +rpc_clnt_connection_cleanup(rpc_clnt_connection_t *conn) +{ + struct saved_frames *saved_frames = NULL; + struct rpc_clnt *clnt = NULL; + int unref = 0; + int ret = 0; + gf_boolean_t timer_unref = _gf_false; + gf_boolean_t reconnect_unref = _gf_false; + + if (!conn) { + goto out; + } + + clnt = conn->rpc_clnt; + + pthread_mutex_lock(&conn->lock); + { + saved_frames = conn->saved_frames; + conn->saved_frames = saved_frames_new(); + + /* bailout logic cleanup */ + if (conn->timer) { + ret = gf_timer_call_cancel(clnt->ctx, conn->timer); + if (!ret) + timer_unref = _gf_true; + conn->timer = NULL; + } + if (conn->reconnect) { + ret = gf_timer_call_cancel(clnt->ctx, conn->reconnect); + if (!ret) + reconnect_unref = _gf_true; + conn->reconnect = NULL; + } + + conn->connected = 0; + conn->disconnected = 1; + + unref = rpc_clnt_remove_ping_timer_locked(clnt); + /*reset rpc msgs stats*/ + conn->pingcnt = 0; + conn->msgcnt = 0; + conn->cleanup_gen++; + } + pthread_mutex_unlock(&conn->lock); + + saved_frames_destroy(saved_frames); + if (unref) + rpc_clnt_unref(clnt); + + if (timer_unref) + rpc_clnt_unref(clnt); + + if (reconnect_unref) + rpc_clnt_unref(clnt); +out: + return 0; +} + +/* + * lookup_frame - lookup call frame corresponding to a given callid + * @trans: transport object + * @callid: call id of the frame + * + * not for external reference + */ + +static struct saved_frame * +lookup_frame(rpc_clnt_connection_t *conn, int64_t callid) +{ + struct saved_frame *frame = NULL; + + pthread_mutex_lock(&conn->lock); + { + frame = __saved_frame_get(conn->saved_frames, callid); + } + pthread_mutex_unlock(&conn->lock); + + return frame; +} + +int +rpc_clnt_reply_fill(rpc_transport_pollin_t *msg, rpc_clnt_connection_t *conn, + struct rpc_msg *replymsg, struct iovec progmsg, + struct rpc_req *req, struct saved_frame *saved_frame) +{ + int ret = -1; + + if ((!conn) || (!replymsg) || (!req) || (!saved_frame) || (!msg)) { + goto out; + } + + req->rpc_status = 0; + if ((rpc_reply_status(replymsg) == MSG_DENIED) || + (rpc_accepted_reply_status(replymsg) != SUCCESS)) { + req->rpc_status = -1; + } + + req->rsp[0] = progmsg; + req->rsp_iobref = iobref_ref(msg->iobref); + + if (msg->vectored) { + req->rsp[1] = msg->vector[1]; + req->rspcnt = 2; + } else { + req->rspcnt = 1; + } + + /* By this time, the data bytes for the auth scheme would have already + * been copied into the required sections of the req structure, + * we just need to fill in the meta-data about it now. + */ + if (req->rpc_status == 0) { + /* + * req->verf.flavour = rpc_reply_verf_flavour (replymsg); + * req->verf.datalen = rpc_reply_verf_len (replymsg); + */ + } + + ret = 0; + +out: + return ret; +} + +void +rpc_clnt_reply_deinit(struct rpc_req *req, struct mem_pool *pool) +{ + if (!req) { + goto out; + } + + if (req->rsp_iobref) { + iobref_unref(req->rsp_iobref); + } + + mem_put(req); +out: + return; +} + +/* TODO: use mem-pool for allocating requests */ +int +rpc_clnt_reply_init(rpc_clnt_connection_t *conn, rpc_transport_pollin_t *msg, + struct rpc_req *req, struct saved_frame *saved_frame) +{ + char *msgbuf = NULL; + struct rpc_msg rpcmsg; + struct iovec progmsg; /* RPC Program payload */ + size_t msglen = 0; + int ret = -1; + + msgbuf = msg->vector[0].iov_base; + msglen = msg->vector[0].iov_len; + + ret = xdr_to_rpc_reply(msgbuf, msglen, &rpcmsg, &progmsg, + req->verf.authdata); + if (ret != 0) { + gf_log(conn->name, GF_LOG_WARNING, "RPC reply decoding failed"); + goto out; + } + + ret = rpc_clnt_reply_fill(msg, conn, &rpcmsg, progmsg, req, saved_frame); + if (ret != 0) { + goto out; + } + + gf_log(conn->name, GF_LOG_TRACE, + "received rpc message (RPC XID: 0x%x" + " Program: %s, ProgVers: %d, Proc: %d) from rpc-transport (%s)", + saved_frame->rpcreq->xid, saved_frame->rpcreq->prog->progname, + saved_frame->rpcreq->prog->progver, saved_frame->rpcreq->procnum, + conn->name); + +out: + if (ret != 0) { + req->rpc_status = -1; + } + + return ret; +} + +int +rpc_clnt_handle_cbk(struct rpc_clnt *clnt, rpc_transport_pollin_t *msg) +{ + char *msgbuf = NULL; + rpcclnt_cb_program_t *program = NULL; + struct rpc_msg rpcmsg; + struct iovec progmsg; /* RPC Program payload */ + size_t msglen = 0; + int found = 0; + int ret = -1; + int procnum = 0; + + msgbuf = msg->vector[0].iov_base; + msglen = msg->vector[0].iov_len; + + clnt = rpc_clnt_ref(clnt); + ret = xdr_to_rpc_call(msgbuf, msglen, &rpcmsg, &progmsg, NULL, NULL); + if (ret == -1) { + gf_log(clnt->conn.name, GF_LOG_WARNING, "RPC call decoding failed"); + goto out; + } + + gf_log(clnt->conn.name, GF_LOG_TRACE, + "receivd rpc message (XID: 0x%" GF_PRI_RPC_XID + ", " + "Ver: %" GF_PRI_RPC_VERSION ", Program: %" GF_PRI_RPC_PROG_ID + ", " + "ProgVers: %" GF_PRI_RPC_PROG_VERS ", Proc: %" GF_PRI_RPC_PROC + ") " + "from rpc-transport (%s)", + rpc_call_xid(&rpcmsg), rpc_call_rpcvers(&rpcmsg), + rpc_call_program(&rpcmsg), rpc_call_progver(&rpcmsg), + rpc_call_progproc(&rpcmsg), clnt->conn.name); + + procnum = rpc_call_progproc(&rpcmsg); + + pthread_mutex_lock(&clnt->lock); + { + list_for_each_entry(program, &clnt->programs, program) + { + if ((program->prognum == rpc_call_program(&rpcmsg)) && + (program->progver == rpc_call_progver(&rpcmsg))) { + found = 1; + break; + } + } + } + pthread_mutex_unlock(&clnt->lock); + + if (found && (procnum < program->numactors) && + (program->actors[procnum].actor)) { + program->actors[procnum].actor(clnt, program->mydata, &progmsg); + } + +out: + rpc_clnt_unref(clnt); + return ret; +} + +int +rpc_clnt_handle_reply(struct rpc_clnt *clnt, rpc_transport_pollin_t *pollin) +{ + rpc_clnt_connection_t *conn = NULL; + struct saved_frame *saved_frame = NULL; + int ret = -1; + struct rpc_req *req = NULL; + uint32_t xid = 0; + + clnt = rpc_clnt_ref(clnt); + conn = &clnt->conn; + + xid = ntoh32(*((uint32_t *)pollin->vector[0].iov_base)); + saved_frame = lookup_frame(conn, xid); + if (saved_frame == NULL) { + gf_log(conn->name, GF_LOG_ERROR, + "cannot lookup the saved frame for reply with xid (%u)", xid); + goto out; + } + + req = saved_frame->rpcreq; + if (req == NULL) { + gf_log(conn->name, GF_LOG_ERROR, "no request with frame for xid (%u)", + xid); + goto out; + } + + ret = rpc_clnt_reply_init(conn, pollin, req, saved_frame); + if (ret != 0) { + req->rpc_status = -1; + gf_log(conn->name, GF_LOG_WARNING, "initialising rpc reply failed"); + } + + req->cbkfn(req, req->rsp, req->rspcnt, saved_frame->frame); + + if (req) { + rpc_clnt_reply_deinit(req, conn->rpc_clnt->reqpool); + } +out: + + if (saved_frame) { + mem_put(saved_frame); + } + + rpc_clnt_unref(clnt); + return ret; +} + +gf_boolean_t +is_rpc_clnt_disconnected(rpc_clnt_connection_t *conn) +{ + gf_boolean_t disconnected = _gf_true; + + if (!conn) + return disconnected; + + pthread_mutex_lock(&conn->lock); + { + disconnected = conn->disconnected; + } + pthread_mutex_unlock(&conn->lock); + + return disconnected; +} + +static void +rpc_clnt_destroy(struct rpc_clnt *rpc); + +#define RPC_THIS_SAVE(xl) \ + do { \ + old_THIS = THIS; \ + if (!old_THIS) \ + gf_log_callingfn("rpc", GF_LOG_CRITICAL, \ + "THIS is not initialised."); \ + THIS = xl; \ + } while (0) + +#define RPC_THIS_RESTORE (THIS = old_THIS) + +static int +rpc_clnt_handle_disconnect(struct rpc_clnt *clnt, rpc_clnt_connection_t *conn) +{ + struct timespec ts = { + 0, + }; + gf_boolean_t unref_clnt = _gf_false; + uint64_t pre_notify_gen = 0, post_notify_gen = 0; + + pthread_mutex_lock(&conn->lock); + { + pre_notify_gen = conn->cleanup_gen; + } + pthread_mutex_unlock(&conn->lock); + + if (clnt->notifyfn) + clnt->notifyfn(clnt, clnt->mydata, RPC_CLNT_DISCONNECT, NULL); + + pthread_mutex_lock(&conn->lock); + { + post_notify_gen = conn->cleanup_gen; + } + pthread_mutex_unlock(&conn->lock); + + if (pre_notify_gen == post_notify_gen) { + /* program didn't invoke cleanup, so rpc has to do it */ + rpc_clnt_connection_cleanup(conn); + } + + pthread_mutex_lock(&conn->lock); + { + if (!conn->rpc_clnt->disabled && (conn->reconnect == NULL)) { + ts.tv_sec = 3; + ts.tv_nsec = 0; + + rpc_clnt_ref(clnt); + conn->reconnect = gf_timer_call_after(clnt->ctx, ts, + rpc_clnt_reconnect, conn); + if (conn->reconnect == NULL) { + gf_log(conn->name, GF_LOG_WARNING, + "Cannot create rpc_clnt_reconnect timer"); + unref_clnt = _gf_true; + } + } + } + pthread_mutex_unlock(&conn->lock); + + if (unref_clnt) + rpc_clnt_unref(clnt); + + return 0; +} + +int +rpc_clnt_notify(rpc_transport_t *trans, void *mydata, + rpc_transport_event_t event, void *data, ...) +{ + rpc_clnt_connection_t *conn = NULL; + struct rpc_clnt *clnt = NULL; + int ret = -1; + rpc_request_info_t *req_info = NULL; + rpc_transport_pollin_t *pollin = NULL; + void *clnt_mydata = NULL; + DECLARE_OLD_THIS; + + conn = mydata; + if (conn == NULL) { + goto out; + } + clnt = conn->rpc_clnt; + if (!clnt) + goto out; + + RPC_THIS_SAVE(clnt->owner); + + switch (event) { + case RPC_TRANSPORT_DISCONNECT: { + rpc_clnt_handle_disconnect(clnt, conn); + /* The auth_value was being reset to AUTH_GLUSTERFS_v2. + * if (clnt->auth_value) + * clnt->auth_value = AUTH_GLUSTERFS_v2; + * It should not be reset here. The disconnect during + * portmap request can race with handshake. If handshake + * happens first and disconnect later, auth_value would set + * to default value and it never sets back to actual auth_value + * supported by server. But it's important to set to lower + * version supported in the case where the server downgrades. + * So moving this code to RPC_TRANSPORT_CONNECT. Note that + * CONNECT cannot race with handshake as by nature it is + * serialized with handhake. An handshake can happen only + * on a connected transport and hence its strictly serialized. + */ + break; + } + + case RPC_TRANSPORT_CLEANUP: + if (clnt->notifyfn) { + clnt_mydata = clnt->mydata; + clnt->mydata = NULL; + ret = clnt->notifyfn(clnt, clnt_mydata, RPC_CLNT_DESTROY, NULL); + if (ret < 0) { + gf_log(trans->name, GF_LOG_WARNING, + "client notify handler returned error " + "while handling RPC_CLNT_DESTROY"); + } + } + rpc_clnt_destroy(clnt); + ret = 0; + break; + + case RPC_TRANSPORT_MAP_XID_REQUEST: { + req_info = data; + ret = rpc_clnt_fill_request_info(clnt, req_info); + break; + } + + case RPC_TRANSPORT_MSG_RECEIVED: { + timespec_now_realtime(&conn->last_received); + + pollin = data; + if (pollin->is_reply) + ret = rpc_clnt_handle_reply(clnt, pollin); + else + ret = rpc_clnt_handle_cbk(clnt, pollin); + /* ret = clnt->notifyfn (clnt, clnt->mydata, RPC_CLNT_MSG, + * data); + */ + break; + } + + case RPC_TRANSPORT_MSG_SENT: { + timespec_now_realtime(&conn->last_sent); + ret = 0; + break; + } + + case RPC_TRANSPORT_CONNECT: { + pthread_mutex_lock(&conn->lock); + { + /* Every time there is a disconnection, processes + * should try to connect to 'glusterd' (ie, default + * port) or whichever port given as 'option remote-port' + * in volume file. */ + /* Below code makes sure the (re-)configured port lasts + * for just one successful attempt */ + conn->config.remote_port = 0; + conn->connected = 1; + conn->disconnected = 0; + pthread_cond_broadcast(&conn->cond); + } + pthread_mutex_unlock(&conn->lock); + + /* auth value should be set to lower version available + * and will be set to appropriate version supported by + * server after the handshake. + */ + if (clnt->auth_value) + clnt->auth_value = AUTH_GLUSTERFS_v2; + if (clnt->notifyfn) + ret = clnt->notifyfn(clnt, clnt->mydata, RPC_CLNT_CONNECT, + NULL); + + break; + } + + case RPC_TRANSPORT_ACCEPT: + /* only meaningful on a server, no need of handling this event + * in a client. + */ + ret = 0; + break; + + case RPC_TRANSPORT_EVENT_THREAD_DIED: + /* only meaningful on a server, no need of handling this event on a + * client */ + ret = 0; + break; + } + +out: + RPC_THIS_RESTORE; + return ret; +} + +static int +rpc_clnt_connection_init(struct rpc_clnt *clnt, glusterfs_ctx_t *ctx, + dict_t *options, char *name) +{ + int ret = -1; + rpc_clnt_connection_t *conn = NULL; + rpc_transport_t *trans = NULL; + + conn = &clnt->conn; + pthread_mutex_init(&clnt->conn.lock, NULL); + pthread_cond_init(&clnt->conn.cond, NULL); + + conn->name = gf_strdup(name); + if (!conn->name) { + ret = -1; + goto out; + } + + ret = dict_get_int32(options, "frame-timeout", &conn->frame_timeout); + if (ret >= 0) { + gf_log(name, GF_LOG_INFO, "setting frame-timeout to %d", + conn->frame_timeout); + } else { + gf_log(name, GF_LOG_DEBUG, "defaulting frame-timeout to 30mins"); + conn->frame_timeout = 1800; + } + conn->rpc_clnt = clnt; + + ret = dict_get_int32(options, "ping-timeout", &conn->ping_timeout); + if (ret >= 0) { + gf_log(name, GF_LOG_DEBUG, "setting ping-timeout to %d", + conn->ping_timeout); + } else { + /*TODO: Once the epoll thread model is fixed, + change the default ping-timeout to 30sec */ + gf_log(name, GF_LOG_DEBUG, "disable ping-timeout"); + conn->ping_timeout = 0; + } + + trans = rpc_transport_load(ctx, options, name); + if (!trans) { + gf_log(name, GF_LOG_WARNING, + "loading of new rpc-transport" + " failed"); + ret = -1; + goto out; + } + rpc_transport_ref(trans); + + pthread_mutex_lock(&conn->lock); + { + conn->trans = trans; + trans = NULL; + } + pthread_mutex_unlock(&conn->lock); + + ret = rpc_transport_register_notify(conn->trans, rpc_clnt_notify, conn); + if (ret == -1) { + gf_log(name, GF_LOG_WARNING, "registering notify failed"); + goto out; + } + + conn->saved_frames = saved_frames_new(); + if (!conn->saved_frames) { + gf_log(name, GF_LOG_WARNING, + "creation of saved_frames " + "failed"); + ret = -1; + goto out; + } + + ret = 0; + +out: + if (ret) { + pthread_mutex_lock(&conn->lock); + { + trans = conn->trans; + conn->trans = NULL; + } + pthread_mutex_unlock(&conn->lock); + if (trans) + rpc_transport_unref(trans); + // conn cleanup needs to be done since we might have failed to + // register notification. + rpc_clnt_connection_cleanup(conn); + } + return ret; +} + +struct rpc_clnt * +rpc_clnt_new(dict_t *options, xlator_t *owner, char *name, + uint32_t reqpool_size) +{ + int ret = -1; + struct rpc_clnt *rpc = NULL; + glusterfs_ctx_t *ctx = owner->ctx; + + rpc = GF_CALLOC(1, sizeof(*rpc), gf_common_mt_rpcclnt_t); + if (!rpc) { + goto out; + } + + pthread_mutex_init(&rpc->lock, NULL); + rpc->ctx = ctx; + rpc->owner = owner; + GF_ATOMIC_INIT(rpc->xid, 1); + + if (!reqpool_size) + reqpool_size = RPC_CLNT_DEFAULT_REQUEST_COUNT; + + rpc->reqpool = mem_pool_new(struct rpc_req, reqpool_size); + if (rpc->reqpool == NULL) { + pthread_mutex_destroy(&rpc->lock); + GF_FREE(rpc); + rpc = NULL; + goto out; + } + + rpc->saved_frames_pool = mem_pool_new(struct saved_frame, reqpool_size); + if (rpc->saved_frames_pool == NULL) { + pthread_mutex_destroy(&rpc->lock); + mem_pool_destroy(rpc->reqpool); + GF_FREE(rpc); + rpc = NULL; + goto out; + } + + ret = rpc_clnt_connection_init(rpc, ctx, options, name); + if (ret == -1) { + pthread_mutex_destroy(&rpc->lock); + mem_pool_destroy(rpc->reqpool); + mem_pool_destroy(rpc->saved_frames_pool); + GF_FREE(rpc); + rpc = NULL; + goto out; + } + + /* This is handled to make sure we have modularity in getting the + auth data changed */ + gf_boolean_t auth_null = dict_get_str_boolean(options, "auth-null", 0); + + rpc->auth_value = (auth_null) ? 0 : AUTH_GLUSTERFS_v2; + + rpc = rpc_clnt_ref(rpc); + INIT_LIST_HEAD(&rpc->programs); + +out: + return rpc; +} + +int +rpc_clnt_start(struct rpc_clnt *rpc) +{ + struct rpc_clnt_connection *conn = NULL; + + if (!rpc) + return -1; + + conn = &rpc->conn; + + pthread_mutex_lock(&conn->lock); + { + rpc->disabled = 0; + } + pthread_mutex_unlock(&conn->lock); + /* Corresponding unref will be either on successful timer cancel or last + * rpc_clnt_reconnect fire event. + */ + rpc_clnt_ref(rpc); + rpc_clnt_reconnect(conn); + + return 0; +} + +int +rpc_clnt_cleanup_and_start(struct rpc_clnt *rpc) +{ + struct rpc_clnt_connection *conn = NULL; + + if (!rpc) + return -1; + + conn = &rpc->conn; + + rpc_clnt_connection_cleanup(conn); + + pthread_mutex_lock(&conn->lock); + { + rpc->disabled = 0; + } + pthread_mutex_unlock(&conn->lock); + /* Corresponding unref will be either on successful timer cancel or last + * rpc_clnt_reconnect fire event. + */ + rpc_clnt_ref(rpc); + rpc_clnt_reconnect(conn); + + return 0; +} + +int +rpc_clnt_register_notify(struct rpc_clnt *rpc, rpc_clnt_notify_t fn, + void *mydata) +{ + rpc->mydata = mydata; + rpc->notifyfn = fn; + + return 0; +} + +/* used for GF_LOG_OCCASIONALLY() */ +static int gf_auth_max_groups_log = 0; + +static inline int +setup_glusterfs_auth_param_v3(call_frame_t *frame, auth_glusterfs_params_v3 *au, + int lk_owner_len, char *owner_data) +{ + int ret = -1; + unsigned int max_groups = 0; + int max_lkowner_len = 0; + + au->pid = frame->root->pid; + au->uid = frame->root->uid; + au->gid = frame->root->gid; + + au->flags = frame->root->flags; + au->ctime_sec = frame->root->ctime.tv_sec; + au->ctime_nsec = frame->root->ctime.tv_nsec; + + au->lk_owner.lk_owner_val = owner_data; + au->lk_owner.lk_owner_len = lk_owner_len; + au->groups.groups_val = frame->root->groups; + au->groups.groups_len = frame->root->ngrps; + + /* The number of groups and the size of lk_owner depend on oneother. + * We can truncate the groups, but should not touch the lk_owner. */ + max_groups = GF_AUTH_GLUSTERFS_MAX_GROUPS(lk_owner_len, AUTH_GLUSTERFS_v3); + if (au->groups.groups_len > max_groups) { + GF_LOG_OCCASIONALLY(gf_auth_max_groups_log, "rpc-auth", GF_LOG_WARNING, + "truncating grouplist " + "from %d to %d", + au->groups.groups_len, max_groups); + + au->groups.groups_len = max_groups; + } + + max_lkowner_len = GF_AUTH_GLUSTERFS_MAX_LKOWNER(au->groups.groups_len, + AUTH_GLUSTERFS_v3); + if (lk_owner_len > max_lkowner_len) { + gf_log("rpc-clnt", GF_LOG_ERROR, + "lkowner field is too " + "big (%d), it does not fit in the rpc-header", + au->lk_owner.lk_owner_len); + errno = E2BIG; + goto out; + } + + ret = 0; +out: + return ret; +} + +static inline int +setup_glusterfs_auth_param_v2(call_frame_t *frame, auth_glusterfs_parms_v2 *au, + int lk_owner_len, char *owner_data) +{ + unsigned int max_groups = 0; + int max_lkowner_len = 0; + int ret = -1; + + au->pid = frame->root->pid; + au->uid = frame->root->uid; + au->gid = frame->root->gid; + + au->lk_owner.lk_owner_val = owner_data; + au->lk_owner.lk_owner_len = lk_owner_len; + au->groups.groups_val = frame->root->groups; + au->groups.groups_len = frame->root->ngrps; + + /* The number of groups and the size of lk_owner depend on oneother. + * We can truncate the groups, but should not touch the lk_owner. */ + max_groups = GF_AUTH_GLUSTERFS_MAX_GROUPS(lk_owner_len, AUTH_GLUSTERFS_v2); + if (au->groups.groups_len > max_groups) { + GF_LOG_OCCASIONALLY(gf_auth_max_groups_log, "rpc-auth", GF_LOG_WARNING, + "truncating grouplist " + "from %d to %d", + au->groups.groups_len, max_groups); + + au->groups.groups_len = max_groups; + } + + max_lkowner_len = GF_AUTH_GLUSTERFS_MAX_LKOWNER(au->groups.groups_len, + AUTH_GLUSTERFS_v2); + if (lk_owner_len > max_lkowner_len) { + gf_log("rpc-auth", GF_LOG_ERROR, + "lkowner field is too " + "big (%d), it does not fit in the rpc-header", + au->lk_owner.lk_owner_len); + errno = E2BIG; + goto out; + } + + ret = 0; +out: + return ret; +} + +static ssize_t +xdr_serialize_glusterfs_auth(struct rpc_clnt *clnt, call_frame_t *frame, + char *dest) +{ + ssize_t ret = -1; + XDR xdr; + char owner[4] = { + 0, + }; + int32_t pid = 0; + char *lk_owner_data = NULL; + int lk_owner_len = 0; + + if ((!dest)) + return -1; + + xdrmem_create(&xdr, dest, GF_MAX_AUTH_BYTES, XDR_ENCODE); + + if (frame->root->lk_owner.len) { + lk_owner_data = frame->root->lk_owner.data; + lk_owner_len = frame->root->lk_owner.len; + } else { + pid = frame->root->pid; + owner[0] = (char)(pid & 0xff); + owner[1] = (char)((pid >> 8) & 0xff); + owner[2] = (char)((pid >> 16) & 0xff); + owner[3] = (char)((pid >> 24) & 0xff); + + lk_owner_data = owner; + lk_owner_len = 4; + } + + if (clnt->auth_value == AUTH_GLUSTERFS_v2) { + auth_glusterfs_parms_v2 au_v2 = { + 0, + }; + + ret = setup_glusterfs_auth_param_v2(frame, &au_v2, lk_owner_len, + lk_owner_data); + if (ret) + goto out; + if (!xdr_auth_glusterfs_parms_v2(&xdr, &au_v2)) { + gf_log(THIS->name, GF_LOG_WARNING, + "failed to encode auth glusterfs elements"); + ret = -1; + goto out; + } + } else if (clnt->auth_value == AUTH_GLUSTERFS_v3) { + auth_glusterfs_params_v3 au_v3 = { + 0, + }; + + ret = setup_glusterfs_auth_param_v3(frame, &au_v3, lk_owner_len, + lk_owner_data); + if (ret) + goto out; + + if (!xdr_auth_glusterfs_params_v3(&xdr, &au_v3)) { + gf_log(THIS->name, GF_LOG_WARNING, + "failed to encode auth glusterfs elements"); + ret = -1; + goto out; + } + } else { + gf_log(THIS->name, GF_LOG_WARNING, + "failed to encode auth glusterfs elements"); + ret = -1; + goto out; + } + + ret = (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)); + +out: + return ret; +} + +int +rpc_clnt_fill_request(struct rpc_clnt *clnt, int prognum, int progver, + int procnum, uint64_t xid, call_frame_t *fr, + struct rpc_msg *request, char *auth_data) +{ + int ret = -1; + + if (!request) { + goto out; + } + + memset(request, 0, sizeof(*request)); + + request->rm_xid = xid; + request->rm_direction = CALL; + + request->rm_call.cb_rpcvers = 2; + request->rm_call.cb_prog = prognum; + request->rm_call.cb_vers = progver; + request->rm_call.cb_proc = procnum; + + if (!clnt->auth_value) { + request->rm_call.cb_cred.oa_flavor = AUTH_NULL; + request->rm_call.cb_cred.oa_base = NULL; + request->rm_call.cb_cred.oa_length = 0; + } else { + ret = xdr_serialize_glusterfs_auth(clnt, fr, auth_data); + if (ret == -1) { + gf_log("rpc-clnt", GF_LOG_WARNING, + "cannot encode auth credentials"); + goto out; + } + + request->rm_call.cb_cred.oa_flavor = clnt->auth_value; + request->rm_call.cb_cred.oa_base = auth_data; + request->rm_call.cb_cred.oa_length = ret; + } + request->rm_call.cb_verf.oa_flavor = AUTH_NONE; + request->rm_call.cb_verf.oa_base = NULL; + request->rm_call.cb_verf.oa_length = 0; + + ret = 0; +out: + return ret; +} + +struct iovec +rpc_clnt_record_build_header(char *recordstart, size_t rlen, + struct rpc_msg *request, size_t payload) +{ + struct iovec requesthdr = { + 0, + }; + struct iovec txrecord = {0, 0}; + int ret = -1; + size_t fraglen = 0; + + ret = rpc_request_to_xdr(request, recordstart, rlen, &requesthdr); + if (ret == -1) { + gf_log("rpc-clnt", GF_LOG_DEBUG, "Failed to create RPC request"); + goto out; + } + + fraglen = payload + requesthdr.iov_len; + gf_log("rpc-clnt", GF_LOG_TRACE, + "Request fraglen %zu, payload: %zu, " + "rpc hdr: %zu", + fraglen, payload, requesthdr.iov_len); + + txrecord.iov_base = recordstart; + + /* Remember, this is only the vec for the RPC header and does not + * include the payload above. We needed the payload only to calculate + * the size of the full fragment. This size is sent in the fragment + * header. + */ + txrecord.iov_len = requesthdr.iov_len; + +out: + return txrecord; +} + +struct iobuf * +rpc_clnt_record_build_record(struct rpc_clnt *clnt, call_frame_t *fr, + int prognum, int progver, int procnum, + size_t hdrsize, uint64_t xid, struct iovec *recbuf) +{ + struct rpc_msg request = { + 0, + }; + struct iobuf *request_iob = NULL; + char *record = NULL; + struct iovec recordhdr = { + 0, + }; + size_t pagesize = 0; + int ret = -1; + size_t xdr_size = 0; + char auth_data[GF_MAX_AUTH_BYTES] = { + 0, + }; + + if ((!clnt) || (!recbuf)) { + goto out; + } + + /* Fill the rpc structure and XDR it into the buffer got above. */ + ret = rpc_clnt_fill_request(clnt, prognum, progver, procnum, xid, fr, + &request, auth_data); + + if (ret == -1) { + gf_log(clnt->conn.name, GF_LOG_WARNING, + "cannot build a rpc-request xid (%" PRIu64 ")", xid); + goto out; + } + + xdr_size = xdr_sizeof((xdrproc_t)xdr_callmsg, &request); + + /* First, try to get a pointer into the buffer which the RPC + * layer can use. + */ + request_iob = iobuf_get2(clnt->ctx->iobuf_pool, (xdr_size + hdrsize)); + if (!request_iob) { + goto out; + } + + pagesize = iobuf_pagesize(request_iob); + + record = iobuf_ptr(request_iob); /* Now we have it. */ + + recordhdr = rpc_clnt_record_build_header(record, pagesize, &request, + hdrsize); + + if (!recordhdr.iov_base) { + gf_log(clnt->conn.name, GF_LOG_ERROR, "Failed to build record header"); + iobuf_unref(request_iob); + request_iob = NULL; + recbuf->iov_base = NULL; + goto out; + } + + recbuf->iov_base = recordhdr.iov_base; + recbuf->iov_len = recordhdr.iov_len; + +out: + return request_iob; +} + +static inline struct iobuf * +rpc_clnt_record(struct rpc_clnt *clnt, call_frame_t *call_frame, + rpc_clnt_prog_t *prog, int procnum, size_t hdrlen, + struct iovec *rpchdr, uint64_t callid) +{ + if (!prog || !rpchdr || !call_frame) { + return NULL; + } + + return rpc_clnt_record_build_record(clnt, call_frame, prog->prognum, + prog->progver, procnum, hdrlen, callid, + rpchdr); +} + +int +rpcclnt_cbk_program_register(struct rpc_clnt *clnt, + rpcclnt_cb_program_t *program, void *mydata) +{ + int ret = -1; + char already_registered = 0; + rpcclnt_cb_program_t *tmp = NULL; + + if (!clnt) + goto out; + + if (program->actors == NULL) + goto out; + + pthread_mutex_lock(&clnt->lock); + { + list_for_each_entry(tmp, &clnt->programs, program) + { + if ((program->prognum == tmp->prognum) && + (program->progver == tmp->progver)) { + already_registered = 1; + break; + } + } + } + pthread_mutex_unlock(&clnt->lock); + + if (already_registered) { + gf_log_callingfn(clnt->conn.name, GF_LOG_DEBUG, "already registered"); + ret = 0; + goto out; + } + + tmp = GF_MALLOC(sizeof(*tmp), gf_common_mt_rpcclnt_cb_program_t); + if (tmp == NULL) { + goto out; + } + + memcpy(tmp, program, sizeof(*tmp)); + INIT_LIST_HEAD(&tmp->program); + + tmp->mydata = mydata; + + pthread_mutex_lock(&clnt->lock); + { + list_add_tail(&tmp->program, &clnt->programs); + } + pthread_mutex_unlock(&clnt->lock); + + ret = 0; + gf_log(clnt->conn.name, GF_LOG_DEBUG, + "New program registered: %s, Num: %d, Ver: %d", program->progname, + program->prognum, program->progver); + +out: + if (ret == -1 && clnt) { + gf_log(clnt->conn.name, GF_LOG_ERROR, + "Program registration failed:" + " %s, Num: %d, Ver: %d", + program->progname, program->prognum, program->progver); + } + + return ret; +} + +int +rpc_clnt_submit(struct rpc_clnt *rpc, rpc_clnt_prog_t *prog, int procnum, + fop_cbk_fn_t cbkfn, struct iovec *proghdr, int proghdrcount, + struct iovec *progpayload, int progpayloadcount, + struct iobref *iobref, void *frame, struct iovec *rsphdr, + int rsphdr_count, struct iovec *rsp_payload, + int rsp_payload_count, struct iobref *rsp_iobref) +{ + rpc_clnt_connection_t *conn = NULL; + struct iobuf *request_iob = NULL; + struct iovec rpchdr = { + 0, + }; + struct rpc_req *rpcreq = NULL; + rpc_transport_req_t req; + int ret = -1; + int proglen = 0; + char new_iobref = 0; + uint64_t callid = 0; + gf_boolean_t need_unref = _gf_false; + call_frame_t *cframe = frame; + + if (!rpc || !prog || !frame) { + goto out; + } + + conn = &rpc->conn; + + rpcreq = mem_get(rpc->reqpool); + if (rpcreq == NULL) { + goto out; + } + + memset(rpcreq, 0, sizeof(*rpcreq)); + memset(&req, 0, sizeof(req)); + + if (!iobref) { + iobref = iobref_new(); + if (!iobref) { + goto out; + } + + new_iobref = 1; + } + + callid = GF_ATOMIC_INC(rpc->xid); + + rpcreq->prog = prog; + rpcreq->procnum = procnum; + rpcreq->conn = conn; + rpcreq->xid = callid; + rpcreq->cbkfn = cbkfn; + + ret = -1; + + if (proghdr) { + proglen += iov_length(proghdr, proghdrcount); + } + + request_iob = rpc_clnt_record(rpc, frame, prog, procnum, proglen, &rpchdr, + callid); + if (!request_iob) { + gf_log(conn->name, GF_LOG_WARNING, "cannot build rpc-record"); + goto out; + } + + iobref_add(iobref, request_iob); + + req.msg.rpchdr = &rpchdr; + req.msg.rpchdrcount = 1; + req.msg.proghdr = proghdr; + req.msg.proghdrcount = proghdrcount; + req.msg.progpayload = progpayload; + req.msg.progpayloadcount = progpayloadcount; + req.msg.iobref = iobref; + + req.rsp.rsphdr = rsphdr; + req.rsp.rsphdr_count = rsphdr_count; + req.rsp.rsp_payload = rsp_payload; + req.rsp.rsp_payload_count = rsp_payload_count; + req.rsp.rsp_iobref = rsp_iobref; + req.rpc_req = rpcreq; + + pthread_mutex_lock(&conn->lock); + { + if (conn->connected == 0) { + if (rpc->disabled) + goto unlock; + ret = rpc_transport_connect(conn->trans, conn->config.remote_port); + if (ret < 0) { + gf_log(conn->name, + (errno == EINPROGRESS) ? GF_LOG_DEBUG : GF_LOG_WARNING, + "error returned while attempting to " + "connect to host:%s, port:%d", + conn->config.remote_host, conn->config.remote_port); + goto unlock; + } + } + + ret = rpc_transport_submit_request(conn->trans, &req); + if (ret == -1) { + gf_log(conn->name, GF_LOG_WARNING, + "failed to submit rpc-request " + "(unique: %" PRIu64 + ", XID: 0x%x Program: %s, " + "ProgVers: %d, Proc: %d) to rpc-transport (%s)", + cframe->root->unique, rpcreq->xid, rpcreq->prog->progname, + rpcreq->prog->progver, rpcreq->procnum, conn->name); + } else if ((ret >= 0) && frame) { + /* Save the frame in queue */ + __save_frame(rpc, frame, rpcreq); + + /* A ref on rpc-clnt object is taken while registering + * call_bail to timer in __save_frame. If it fails to + * register, it needs an unref and should happen outside + * conn->lock which otherwise leads to deadlocks */ + if (conn->timer == NULL) + need_unref = _gf_true; + + conn->msgcnt++; + + gf_log("rpc-clnt", GF_LOG_TRACE, + "submitted request " + "(unique: %" PRIu64 + ", XID: 0x%x, Program: %s, " + "ProgVers: %d, Proc: %d) to rpc-transport (%s)", + cframe->root->unique, rpcreq->xid, rpcreq->prog->progname, + rpcreq->prog->progver, rpcreq->procnum, conn->name); + } + } +unlock: + pthread_mutex_unlock(&conn->lock); + + if (need_unref) + rpc_clnt_unref(rpc); + + if (ret == -1) { + goto out; + } + + rpc_clnt_check_and_start_ping(rpc); + ret = 0; + +out: + if (request_iob) { + iobuf_unref(request_iob); + } + + if (new_iobref && iobref) { + iobref_unref(iobref); + } + + if (frame && (ret == -1)) { + if (rpcreq) { + rpcreq->rpc_status = -1; + cbkfn(rpcreq, NULL, 0, frame); + mem_put(rpcreq); + } + } + return ret; +} + +struct rpc_clnt * +rpc_clnt_ref(struct rpc_clnt *rpc) +{ + if (!rpc) + return NULL; + + GF_ATOMIC_INC(rpc->refcount); + return rpc; +} + +static void +rpc_clnt_trigger_destroy(struct rpc_clnt *rpc) +{ + rpc_clnt_connection_t *conn = NULL; + rpc_transport_t *trans = NULL; + + if (!rpc) + return; + + /* reading conn->trans outside conn->lock is OK, since this is the last + * ref*/ + conn = &rpc->conn; + trans = conn->trans; + rpc_clnt_disable(rpc); + + /* This is to account for rpc_clnt_disable that might have been called + * before rpc_clnt_unref */ + if (trans) { + /* set conn->trans to NULL before rpc_transport_unref + * as rpc_transport_unref can potentially free conn + */ + conn->trans = NULL; + rpc_transport_unref(trans); + } +} + +static void +rpc_clnt_destroy(struct rpc_clnt *rpc) +{ + rpcclnt_cb_program_t *program = NULL; + rpcclnt_cb_program_t *tmp = NULL; + struct saved_frames *saved_frames = NULL; + rpc_clnt_connection_t *conn = NULL; + + if (!rpc) + return; + + conn = &rpc->conn; + GF_FREE(rpc->conn.name); + /* Access saved_frames in critical-section to avoid + crash in rpc_clnt_connection_cleanup at the time + of destroying saved frames + */ + pthread_mutex_lock(&conn->lock); + { + saved_frames = conn->saved_frames; + conn->saved_frames = NULL; + } + pthread_mutex_unlock(&conn->lock); + + saved_frames_destroy(saved_frames); + pthread_mutex_destroy(&rpc->lock); + pthread_mutex_destroy(&rpc->conn.lock); + pthread_cond_destroy(&rpc->conn.cond); + + /* mem-pool should be destroyed, otherwise, + it will cause huge memory leaks */ + mem_pool_destroy(rpc->reqpool); + mem_pool_destroy(rpc->saved_frames_pool); + + list_for_each_entry_safe(program, tmp, &rpc->programs, program) + { + GF_FREE(program); + } + + GF_FREE(rpc); + return; +} + +struct rpc_clnt * +rpc_clnt_unref(struct rpc_clnt *rpc) +{ + int count = 0; + + if (!rpc) + return NULL; + + count = GF_ATOMIC_DEC(rpc->refcount); + + if (!count) { + rpc_clnt_trigger_destroy(rpc); + return NULL; + } + return rpc; +} + +int +rpc_clnt_disable(struct rpc_clnt *rpc) +{ + rpc_clnt_connection_t *conn = NULL; + rpc_transport_t *trans = NULL; + int unref = 0; + int ret = 0; + gf_boolean_t timer_unref = _gf_false; + gf_boolean_t reconnect_unref = _gf_false; + + if (!rpc) { + goto out; + } + + conn = &rpc->conn; + + pthread_mutex_lock(&conn->lock); + { + rpc->disabled = 1; + + if (conn->timer) { + ret = gf_timer_call_cancel(rpc->ctx, conn->timer); + /* If the event is not fired and it actually cancelled + * the timer, do the unref else registered call back + * function will take care of it. + */ + if (!ret) + timer_unref = _gf_true; + conn->timer = NULL; + } + + if (conn->reconnect) { + ret = gf_timer_call_cancel(rpc->ctx, conn->reconnect); + if (!ret) + reconnect_unref = _gf_true; + conn->reconnect = NULL; + } + conn->connected = 0; + + unref = rpc_clnt_remove_ping_timer_locked(rpc); + trans = conn->trans; + } + pthread_mutex_unlock(&conn->lock); + + ret = -1; + if (trans) { + ret = rpc_transport_disconnect(trans, _gf_true); + /* The auth_value was being reset to AUTH_GLUSTERFS_v2. + * if (clnt->auth_value) + * clnt->auth_value = AUTH_GLUSTERFS_v2; + * It should not be reset here. The disconnect during + * portmap request can race with handshake. If handshake + * happens first and disconnect later, auth_value would set + * to default value and it never sets back to actual auth_value + * supported by server. But it's important to set to lower + * version supported in the case where the server downgrades. + * So moving this code to RPC_TRANSPORT_CONNECT. Note that + * CONNECT cannot race with handshake as by nature it is + * serialized with handhake. An handshake can happen only + * on a connected transport and hence its strictly serialized. + */ + } + if (unref) + rpc_clnt_unref(rpc); + + if (timer_unref) + rpc_clnt_unref(rpc); + + if (reconnect_unref) + rpc_clnt_unref(rpc); + +out: + return ret; +} + +void +rpc_clnt_reconfig(struct rpc_clnt *rpc, struct rpc_clnt_config *config) +{ + if (config->ping_timeout) { + if (config->ping_timeout != rpc->conn.ping_timeout) + gf_log(rpc->conn.name, GF_LOG_INFO, + "changing ping timeout to %d (from %d)", + config->ping_timeout, rpc->conn.ping_timeout); + + pthread_mutex_lock(&rpc->conn.lock); + { + rpc->conn.ping_timeout = config->ping_timeout; + } + pthread_mutex_unlock(&rpc->conn.lock); + } + + if (config->rpc_timeout) { + if (config->rpc_timeout != rpc->conn.config.rpc_timeout) + gf_log(rpc->conn.name, GF_LOG_INFO, + "changing timeout to %d (from %d)", config->rpc_timeout, + rpc->conn.config.rpc_timeout); + rpc->conn.config.rpc_timeout = config->rpc_timeout; + } + + if (config->remote_port) { + if (config->remote_port != rpc->conn.config.remote_port) + gf_log(rpc->conn.name, GF_LOG_INFO, "changing port to %d (from %d)", + config->remote_port, rpc->conn.config.remote_port); + + rpc->conn.config.remote_port = config->remote_port; + } + + if (config->remote_host) { + if (rpc->conn.config.remote_host) { + if (strcmp(rpc->conn.config.remote_host, config->remote_host)) + gf_log(rpc->conn.name, GF_LOG_INFO, + "changing hostname to %s (from %s)", config->remote_host, + rpc->conn.config.remote_host); + GF_FREE(rpc->conn.config.remote_host); + } else { + gf_log(rpc->conn.name, GF_LOG_INFO, "setting hostname to %s", + config->remote_host); + } + + rpc->conn.config.remote_host = gf_strdup(config->remote_host); + } +} diff --git a/rpc/rpc-lib/src/rpc-clnt.h b/rpc/rpc-lib/src/rpc-clnt.h new file mode 100644 index 00000000000..2945265200b --- /dev/null +++ b/rpc/rpc-lib/src/rpc-clnt.h @@ -0,0 +1,260 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef __RPC_CLNT_H +#define __RPC_CLNT_H + +#include <glusterfs/stack.h> +#include "rpc-transport.h" +#include <glusterfs/timer.h> +#include "xdr-common.h" +#include "glusterfs3.h" + +typedef enum { + RPC_CLNT_CONNECT, + RPC_CLNT_DISCONNECT, + RPC_CLNT_PING, + RPC_CLNT_MSG, + RPC_CLNT_DESTROY +} rpc_clnt_event_t; + +#define SFRAME_GET_PROGNUM(sframe) (sframe->rpcreq->prog->prognum) +#define SFRAME_GET_PROGVER(sframe) (sframe->rpcreq->prog->progver) +#define SFRAME_GET_PROCNUM(sframe) (sframe->rpcreq->procnum) + +struct rpc_req; +struct rpc_clnt; +struct rpc_clnt_config; +struct rpc_clnt_program; + +typedef int (*rpc_clnt_notify_t)(struct rpc_clnt *rpc, void *mydata, + rpc_clnt_event_t fn, void *data); + +typedef int (*fop_cbk_fn_t)(struct rpc_req *req, struct iovec *iov, int count, + void *myframe); + +typedef int (*clnt_fn_t)(call_frame_t *fr, xlator_t *xl, void *args); + +struct saved_frame { + union { + struct list_head list; + struct { + struct saved_frame *frame_next; + struct saved_frame *frame_prev; + }; + }; + void *capital_this; + void *frame; + struct rpc_req *rpcreq; + struct timeval saved_at; + rpc_transport_rsp_t rsp; +}; + +struct saved_frames { + int64_t count; + struct saved_frame sf; + struct saved_frame lk_sf; +}; + +/* Initialized by procnum */ +typedef struct rpc_clnt_procedure { + char *procname; + clnt_fn_t fn; +} rpc_clnt_procedure_t; + +typedef struct rpc_clnt_program { + char *progname; + int prognum; + int progver; + rpc_clnt_procedure_t *proctable; + char **procnames; + int numproc; +} rpc_clnt_prog_t; + +typedef int (*rpcclnt_cb_fn)(struct rpc_clnt *rpc, void *mydata, void *data); + +/* The descriptor for each procedure/actor that runs + * over the RPC service. + */ +typedef struct rpcclnt_actor_desc { + char procname[32]; + rpcclnt_cb_fn actor; + int procnum; +} rpcclnt_cb_actor_t; + +/* Describes a program and its version along with the function pointers + * required to handle the procedures/actors of each program/version. + * Never changed ever by any thread so no need for a lock. + */ +typedef struct rpcclnt_cb_program { + char progname[32]; + int prognum; + int progver; + rpcclnt_cb_actor_t *actors; /* All procedure handlers */ + /* Program specific state handed to actors */ + void *private; + + /* list member to link to list of registered services with rpc_clnt */ + struct list_head program; + + /* Needed for passing back in cb_actor */ + void *mydata; + int numactors; /* Num actors in actor array */ + +} rpcclnt_cb_program_t; + +typedef struct rpc_auth_data { + int flavour; + int datalen; + char authdata[GF_MAX_AUTH_BYTES]; +} rpc_auth_data_t; + +struct rpc_clnt_config { + int rpc_timeout; + int remote_port; + char *remote_host; + int ping_timeout; +}; + +#define rpc_auth_flavour(au) ((au).flavour) + +struct rpc_clnt_connection { + pthread_mutex_t lock; + pthread_cond_t cond; + rpc_transport_t *trans; + struct rpc_clnt_config config; + gf_timer_t *reconnect; + gf_timer_t *timer; + gf_timer_t *ping_timer; + struct rpc_clnt *rpc_clnt; + struct saved_frames *saved_frames; + struct timespec last_sent; + struct timespec last_received; + uint64_t pingcnt; + uint64_t msgcnt; + uint64_t cleanup_gen; + char *name; + int32_t ping_started; + int32_t frame_timeout; + int32_t ping_timeout; + gf_boolean_t disconnected; + char connected; +}; +typedef struct rpc_clnt_connection rpc_clnt_connection_t; + +struct rpc_req { + rpc_clnt_connection_t *conn; + struct iovec req[2]; + struct iobref *req_iobref; + struct iovec rsp[2]; + int rspcnt; + int reqcnt; + struct iobref *rsp_iobref; + rpc_clnt_prog_t *prog; + rpc_auth_data_t verf; + fop_cbk_fn_t cbkfn; + void *conn_private; + int procnum; + int rpc_status; + uint32_t xid; +}; + +typedef struct rpc_clnt { + pthread_mutex_t lock; + rpc_clnt_notify_t notifyfn; + rpc_clnt_connection_t conn; + void *mydata; + gf_atomic_t xid; + + /* list of cb programs registered with rpc-clnt */ + struct list_head programs; + + /* Memory pool for rpc_req_t */ + struct mem_pool *reqpool; + + struct mem_pool *saved_frames_pool; + + glusterfs_ctx_t *ctx; + gf_atomic_t refcount; + xlator_t *owner; + int auth_value; + char disabled; +} rpc_clnt_t; + +struct rpc_clnt * +rpc_clnt_new(dict_t *options, xlator_t *owner, char *name, + uint32_t reqpool_size); + +int +rpc_clnt_start(struct rpc_clnt *rpc); + +int +rpc_clnt_cleanup_and_start(struct rpc_clnt *rpc); + +int +rpc_clnt_register_notify(struct rpc_clnt *rpc, rpc_clnt_notify_t fn, + void *mydata); + +/* Some preconditions related to vectors holding responses. + * @rsphdr: should contain pointer to buffer which can hold response header + * and length of the program header. In case of procedures whose + * respnose size is not bounded (eg., glusterfs lookup), the length + * should be equal to size of buffer. + * @rsp_payload: should contain pointer and length of the bu + * + * 1. Both @rsp_hdr and @rsp_payload are optional. + * 2. The user of rpc_clnt_submit, if wants response hdr and payload in its own + * buffers, then it has to populate @rsphdr and @rsp_payload. + * 3. when @rsp_payload is not NULL, @rsphdr should + * also be filled with pointer to buffer to hold header and length + * of the header. + */ + +int +rpc_clnt_submit(struct rpc_clnt *rpc, rpc_clnt_prog_t *prog, int procnum, + fop_cbk_fn_t cbkfn, struct iovec *proghdr, int proghdrcount, + struct iovec *progpayload, int progpayloadcount, + struct iobref *iobref, void *frame, struct iovec *rsphdr, + int rsphdr_count, struct iovec *rsp_payload, + int rsp_payload_count, struct iobref *rsp_iobref); + +struct rpc_clnt * +rpc_clnt_ref(struct rpc_clnt *rpc); + +struct rpc_clnt * +rpc_clnt_unref(struct rpc_clnt *rpc); + +int +rpc_clnt_connection_cleanup(rpc_clnt_connection_t *conn); +int +rpc_clnt_reconnect_cleanup(rpc_clnt_connection_t *conn); +gf_boolean_t +is_rpc_clnt_disconnected(rpc_clnt_connection_t *conn); + +void +rpc_clnt_reconnect(void *trans_ptr); + +void +rpc_clnt_reconfig(struct rpc_clnt *rpc, struct rpc_clnt_config *config); + +/* All users of RPC services should use this API to register their + * procedure handlers. + */ +int +rpcclnt_cbk_program_register(struct rpc_clnt *svc, + rpcclnt_cb_program_t *program, void *mydata); + +int +rpc_clnt_disable(struct rpc_clnt *rpc); + +int +rpc_clnt_mgmt_pmap_signout(glusterfs_ctx_t *ctx, char *brick_name); + +#endif /* !_RPC_CLNT_H */ diff --git a/rpc/rpc-lib/src/rpc-drc.c b/rpc/rpc-lib/src/rpc-drc.c new file mode 100644 index 00000000000..de8dc630626 --- /dev/null +++ b/rpc/rpc-lib/src/rpc-drc.c @@ -0,0 +1,855 @@ +/* + Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#ifndef RPC_DRC_H +#include "rpc-drc.h" +#endif +#include <glusterfs/locking.h> +#include <glusterfs/statedump.h> +#include <glusterfs/mem-pool.h> + +#include <netinet/in.h> +#include <unistd.h> + +/** + * rpcsvc_drc_op_destroy - Destroys the cached reply + * + * @param drc - the main drc structure + * @param reply - the cached reply to destroy + * @return NULL if reply is destroyed, reply otherwise + */ +static drc_cached_op_t * +rpcsvc_drc_op_destroy(rpcsvc_drc_globals_t *drc, drc_cached_op_t *reply) +{ + GF_ASSERT(drc); + GF_ASSERT(reply); + + if (reply->state == DRC_OP_IN_TRANSIT) + return reply; + + iobref_unref(reply->msg.iobref); + if (reply->msg.rpchdr) + GF_FREE(reply->msg.rpchdr); + if (reply->msg.proghdr) + GF_FREE(reply->msg.proghdr); + if (reply->msg.progpayload) + GF_FREE(reply->msg.progpayload); + + list_del(&reply->global_list); + reply->client->op_count--; + drc->op_count--; + mem_put(reply); + reply = NULL; + + return reply; +} + +/** + * rpcsvc_drc_op_rb_unref - This function is used in rb tree cleanup only + * + * @param reply - the cached reply to unref + * @param drc - the main drc structure + * @return void + */ +static void +rpcsvc_drc_rb_op_destroy(void *reply, void *drc) +{ + rpcsvc_drc_op_destroy(drc, (drc_cached_op_t *)reply); +} + +/** + * rpcsvc_remove_drc_client - Cleanup the drc client + * + * @param client - the drc client to be removed + * @return void + */ +static void +rpcsvc_remove_drc_client(drc_client_t *client) +{ + rb_destroy(client->rbtree, rpcsvc_drc_rb_op_destroy); + list_del(&client->client_list); + GF_FREE(client); +} + +/** + * rpcsvc_client_lookup - Given a sockaddr_storage, find the client if it exists + * + * @param drc - the main drc structure + * @param sockaddr - the network address of the client to be looked up + * @return drc client if it exists, NULL otherwise + */ +static drc_client_t * +rpcsvc_client_lookup(rpcsvc_drc_globals_t *drc, + struct sockaddr_storage *sockaddr) +{ + drc_client_t *client = NULL; + + GF_ASSERT(drc); + GF_ASSERT(sockaddr); + + if (list_empty(&drc->clients_head)) + return NULL; + + list_for_each_entry(client, &drc->clients_head, client_list) + { + if (gf_sock_union_equal_addr(&client->sock_union, + (union gf_sock_union *)sockaddr)) + return client; + } + + return NULL; +} + +/** + * drc_compare_reqs - Used by rbtree to determine if incoming req matches with + * an existing node(cached reply) in rbtree + * + * @param item - pointer to the incoming req + * @param rb_node_data - pointer to an rbtree node (cached reply) + * @param param - drc pointer - unused here, but used in *op_destroy + * @return 0 if req matches reply, else (req->xid - reply->xid) + */ +int +drc_compare_reqs(const void *item, const void *rb_node_data, void *param) +{ + int ret = -1; + drc_cached_op_t *req = NULL; + drc_cached_op_t *reply = NULL; + + GF_ASSERT(item); + GF_ASSERT(rb_node_data); + GF_ASSERT(param); + + req = (drc_cached_op_t *)item; + reply = (drc_cached_op_t *)rb_node_data; + + ret = req->xid - reply->xid; + if (ret != 0) + return ret; + + if (req->prognum == reply->prognum && req->procnum == reply->procnum && + req->progversion == reply->progversion) + return 0; + + return 1; +} + +/** + * drc_init_client_cache - initialize a drc client and its rb tree + * + * @param drc - the main drc structure + * @param client - the drc client to be initialized + * @return 0 on success, -1 on failure + */ +static int +drc_init_client_cache(rpcsvc_drc_globals_t *drc, drc_client_t *client) +{ + GF_ASSERT(drc); + GF_ASSERT(client); + + client->rbtree = rb_create(drc_compare_reqs, drc, NULL); + if (!client->rbtree) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "rb tree creation failed"); + return -1; + } + + return 0; +} + +/** + * rpcsvc_get_drc_client - find the drc client with given sockaddr, else + * allocate and initialize a new drc client + * + * @param drc - the main drc structure + * @param sockaddr - network address of client + * @return drc client on success, NULL on failure + */ +static drc_client_t * +rpcsvc_get_drc_client(rpcsvc_drc_globals_t *drc, + struct sockaddr_storage *sockaddr) +{ + drc_client_t *client = NULL; + + GF_ASSERT(drc); + GF_ASSERT(sockaddr); + + client = rpcsvc_client_lookup(drc, sockaddr); + if (client) + goto out; + + /* if lookup fails, allocate cache for the new client */ + client = GF_CALLOC(1, sizeof(drc_client_t), gf_common_mt_drc_client_t); + if (!client) + goto out; + + GF_ATOMIC_INIT(client->ref, 0); + client->sock_union = (union gf_sock_union) * sockaddr; + client->op_count = 0; + INIT_LIST_HEAD(&client->client_list); + + if (drc_init_client_cache(drc, client)) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "initialization of drc client failed"); + GF_FREE(client); + client = NULL; + goto out; + } + drc->client_count++; + + list_add(&client->client_list, &drc->clients_head); + +out: + return client; +} + +/** + * rpcsvc_need_drc - Determine if a request needs DRC service + * + * @param req - incoming request + * @return 1 if DRC is needed for req, 0 otherwise + */ +int +rpcsvc_need_drc(rpcsvc_request_t *req) +{ + rpcsvc_actor_t *actor = NULL; + rpcsvc_drc_globals_t *drc = NULL; + + GF_ASSERT(req); + GF_ASSERT(req->svc); + + drc = req->svc->drc; + + if (!drc || drc->status == DRC_UNINITIATED) + return 0; + + actor = rpcsvc_program_actor(req); + if (!actor) + return 0; + + return (actor->op_type == DRC_NON_IDEMPOTENT && drc->type != DRC_TYPE_NONE); +} + +/** + * rpcsvc_drc_client_ref - ref the drc client + * + * @param client - the drc client to ref + * @return client + */ +static drc_client_t * +rpcsvc_drc_client_ref(drc_client_t *client) +{ + GF_ASSERT(client); + GF_ATOMIC_INC(client->ref); + return client; +} + +/** + * rpcsvc_drc_client_unref - unref the drc client, and destroy + * the client on last unref + * + * @param drc - the main drc structure + * @param client - the drc client to unref + * @return NULL if it is the last unref, client otherwise + */ +static drc_client_t * +rpcsvc_drc_client_unref(rpcsvc_drc_globals_t *drc, drc_client_t *client) +{ + uint32_t refcount; + + GF_ASSERT(drc); + + refcount = GF_ATOMIC_DEC(client->ref); + if (!refcount) { + drc->client_count--; + rpcsvc_remove_drc_client(client); + client = NULL; + } + + return client; +} + +/** + * rpcsvc_drc_lookup - lookup a request to see if it is already cached + * + * @param req - incoming request + * @return cached reply of req if found, NULL otherwise + */ +drc_cached_op_t * +rpcsvc_drc_lookup(rpcsvc_request_t *req) +{ + drc_client_t *client = NULL; + drc_cached_op_t *reply = NULL; + drc_cached_op_t new = { + .xid = req->xid, + .prognum = req->prognum, + .progversion = req->progver, + .procnum = req->procnum, + }; + + GF_ASSERT(req); + + if (!req->trans->drc_client) { + client = rpcsvc_get_drc_client(req->svc->drc, + &req->trans->peerinfo.sockaddr); + if (!client) + goto out; + + req->trans->drc_client = rpcsvc_drc_client_ref(client); + } + + client = req->trans->drc_client; + + if (client->op_count == 0) + goto out; + + reply = rb_find(client->rbtree, &new); + +out: + return reply; +} + +/** + * rpcsvc_send_cached_reply - send the cached reply for the incoming request + * + * @param req - incoming request (which is a duplicate in this case) + * @param reply - the cached reply for req + * @return 0 on successful reply submission, -1 or other non-zero value + * otherwise + */ +int +rpcsvc_send_cached_reply(rpcsvc_request_t *req, drc_cached_op_t *reply) +{ + int ret = 0; + + GF_ASSERT(req); + GF_ASSERT(reply); + + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "sending cached reply: xid: %d, " + "client: %s", + req->xid, req->trans->peerinfo.identifier); + + rpcsvc_drc_client_ref(reply->client); + ret = rpcsvc_transport_submit( + req->trans, reply->msg.rpchdr, reply->msg.rpchdrcount, + reply->msg.proghdr, reply->msg.proghdrcount, reply->msg.progpayload, + reply->msg.progpayloadcount, reply->msg.iobref, req->trans_private); + rpcsvc_drc_client_unref(req->svc->drc, reply->client); + + return ret; +} + +/** + * rpcsvc_cache_reply - cache the reply for the processed request 'req' + * + * @param req - processed request + * @param iobref - iobref structure of the reply + * @param rpchdr - rpc header of the reply + * @param rpchdrcount - size of rpchdr + * @param proghdr - program header of the reply + * @param proghdrcount - size of proghdr + * @param payload - payload of the reply if any + * @param payloadcount - size of payload + * @return 0 on success, -1 on failure + */ +int +rpcsvc_cache_reply(rpcsvc_request_t *req, struct iobref *iobref, + struct iovec *rpchdr, int rpchdrcount, struct iovec *proghdr, + int proghdrcount, struct iovec *payload, int payloadcount) +{ + int ret = -1; + drc_cached_op_t *reply = NULL; + + GF_ASSERT(req); + GF_ASSERT(req->reply); + + reply = req->reply; + + reply->state = DRC_OP_CACHED; + + reply->msg.iobref = iobref_ref(iobref); + + reply->msg.rpchdrcount = rpchdrcount; + reply->msg.rpchdr = iov_dup(rpchdr, rpchdrcount); + + reply->msg.proghdrcount = proghdrcount; + reply->msg.proghdr = iov_dup(proghdr, proghdrcount); + + reply->msg.progpayloadcount = payloadcount; + if (payloadcount) + reply->msg.progpayload = iov_dup(payload, payloadcount); + + // rpcsvc_drc_client_unref (req->svc->drc, req->trans->drc_client); + // rpcsvc_drc_op_unref (req->svc->drc, reply); + ret = 0; + + return ret; +} + +/** + * rpcsvc_vacate_drc_entries - free up some percentage of drc cache + * based on the lru factor + * + * @param drc - the main drc structure + * @return void + */ +static void +rpcsvc_vacate_drc_entries(rpcsvc_drc_globals_t *drc) +{ + uint32_t i = 0; + uint32_t n = 0; + drc_cached_op_t *reply = NULL; + drc_cached_op_t *tmp = NULL; + drc_client_t *client = NULL; + + GF_ASSERT(drc); + + n = drc->global_cache_size / drc->lru_factor; + + list_for_each_entry_safe_reverse(reply, tmp, &drc->cache_head, global_list) + { + /* Don't delete ops that are in transit */ + if (reply->state == DRC_OP_IN_TRANSIT) + continue; + + client = reply->client; + + rb_delete(client->rbtree, reply); + + rpcsvc_drc_op_destroy(drc, reply); + rpcsvc_drc_client_unref(drc, client); + i++; + if (i >= n) + break; + } +} + +/** + * rpcsvc_add_op_to_cache - insert the cached op into the client rbtree and drc + * list + * + * @param drc - the main drc structure + * @param reply - the op to be inserted + * @return 0 on success, -1 on failure + */ +static int +rpcsvc_add_op_to_cache(rpcsvc_drc_globals_t *drc, drc_cached_op_t *reply) +{ + drc_client_t *client = NULL; + drc_cached_op_t **tmp_reply = NULL; + + GF_ASSERT(drc); + GF_ASSERT(reply); + + client = reply->client; + + /* cache is full, free up some space */ + if (drc->op_count >= drc->global_cache_size) + rpcsvc_vacate_drc_entries(drc); + + tmp_reply = (drc_cached_op_t **)rb_probe(client->rbtree, reply); + if (!tmp_reply) { + /* mem alloc failed */ + return -1; + } else if (*tmp_reply != reply) { + /* should never happen */ + gf_log(GF_RPCSVC, GF_LOG_ERROR, "DRC failed to detect duplicates"); + return -1; + } + + client->op_count++; + list_add(&reply->global_list, &drc->cache_head); + drc->op_count++; + + return 0; +} + +/** + * rpcsvc_cache_request - cache the in-transition incoming request + * + * @param req - incoming request + * @return 0 on success, -1 on failure + */ +int +rpcsvc_cache_request(rpcsvc_request_t *req) +{ + int ret = -1; + drc_client_t *client = NULL; + drc_cached_op_t *reply = NULL; + rpcsvc_drc_globals_t *drc = NULL; + + GF_ASSERT(req); + + drc = req->svc->drc; + + client = req->trans->drc_client; + if (!client) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "drc client is NULL"); + goto out; + } + + reply = mem_get0(drc->mempool); + if (!reply) + goto out; + + reply->client = rpcsvc_drc_client_ref(client); + reply->xid = req->xid; + reply->prognum = req->prognum; + reply->progversion = req->progver; + reply->procnum = req->procnum; + reply->state = DRC_OP_IN_TRANSIT; + req->reply = reply; + INIT_LIST_HEAD(&reply->global_list); + + ret = rpcsvc_add_op_to_cache(drc, reply); + if (ret) { + req->reply = NULL; + rpcsvc_drc_op_destroy(drc, reply); + rpcsvc_drc_client_unref(drc, client); + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "Failed to add op to drc cache"); + } + +out: + return ret; +} + +/** + * + * rpcsvc_drc_priv - function which dumps the drc state + * + * @param drc - the main drc structure + * @return 0 on success, -1 on failure + */ +int32_t +rpcsvc_drc_priv(rpcsvc_drc_globals_t *drc) +{ + int i = 0; + char key[GF_DUMP_MAX_BUF_LEN] = {0}; + drc_client_t *client = NULL; + char ip[INET6_ADDRSTRLEN] = {0}; + + if (!drc || drc->status == DRC_UNINITIATED) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "DRC is " + "uninitialized, not dumping its state"); + return 0; + } + + gf_proc_dump_add_section("rpc.drc"); + + if (TRY_LOCK(&drc->lock)) + return -1; + + gf_proc_dump_build_key(key, "drc", "type"); + gf_proc_dump_write(key, "%d", drc->type); + + gf_proc_dump_build_key(key, "drc", "client_count"); + gf_proc_dump_write(key, "%d", drc->client_count); + + gf_proc_dump_build_key(key, "drc", "current_cache_size"); + gf_proc_dump_write(key, "%d", drc->op_count); + + gf_proc_dump_build_key(key, "drc", "max_cache_size"); + gf_proc_dump_write(key, "%d", drc->global_cache_size); + + gf_proc_dump_build_key(key, "drc", "lru_factor"); + gf_proc_dump_write(key, "%d", drc->lru_factor); + + gf_proc_dump_build_key(key, "drc", "duplicate_request_count"); + gf_proc_dump_write(key, "%" PRIu64, drc->cache_hits); + + gf_proc_dump_build_key(key, "drc", "in_transit_duplicate_requests"); + gf_proc_dump_write(key, "%" PRIu64, drc->intransit_hits); + + list_for_each_entry(client, &drc->clients_head, client_list) + { + gf_proc_dump_build_key(key, "client", "%d.ip-address", i); + memset(ip, 0, INET6_ADDRSTRLEN); + switch (client->sock_union.storage.ss_family) { + case AF_INET: + gf_proc_dump_write( + key, "%s", + inet_ntop(AF_INET, &client->sock_union.sin.sin_addr.s_addr, + ip, INET_ADDRSTRLEN)); + break; + case AF_INET6: + gf_proc_dump_write( + key, "%s", + inet_ntop(AF_INET6, &client->sock_union.sin6.sin6_addr, ip, + INET6_ADDRSTRLEN)); + break; + default: + gf_proc_dump_write(key, "%s", "N/A"); + } + + gf_proc_dump_build_key(key, "client", "%d.ref_count", i); + gf_proc_dump_write(key, "%" PRIu32, GF_ATOMIC_GET(client->ref)); + gf_proc_dump_build_key(key, "client", "%d.op_count", i); + gf_proc_dump_write(key, "%d", client->op_count); + i++; + } + + UNLOCK(&drc->lock); + return 0; +} + +/** + * rpcsvc_drc_notify - function which is notified of RPC transport events + * + * @param svc - pointer to rpcsvc_t structure of the rpc + * @param xl - pointer to the xlator + * @param event - the event which triggered this notify + * @param data - the transport structure + * @return 0 on success, -1 on failure + */ +int +rpcsvc_drc_notify(rpcsvc_t *svc, void *xl, rpcsvc_event_t event, void *data) +{ + int ret = -1; + rpc_transport_t *trans = NULL; + drc_client_t *client = NULL; + rpcsvc_drc_globals_t *drc = NULL; + + GF_ASSERT(svc); + GF_ASSERT(svc->drc); + GF_ASSERT(data); + + drc = svc->drc; + + if (drc->status == DRC_UNINITIATED || drc->type == DRC_TYPE_NONE) + return 0; + + LOCK(&drc->lock); + { + trans = (rpc_transport_t *)data; + client = rpcsvc_get_drc_client(drc, &trans->peerinfo.sockaddr); + if (!client) + goto unlock; + + switch (event) { + case RPCSVC_EVENT_ACCEPT: + trans->drc_client = rpcsvc_drc_client_ref(client); + ret = 0; + break; + + case RPCSVC_EVENT_DISCONNECT: + ret = 0; + if (list_empty(&drc->clients_head)) + break; + /* should be the last unref */ + trans->drc_client = NULL; + rpcsvc_drc_client_unref(drc, client); + break; + + default: + break; + } + } +unlock: + UNLOCK(&drc->lock); + return ret; +} + +/** + * rpcsvc_drc_init - Initialize the duplicate request cache service + * + * @param svc - pointer to rpcsvc_t structure of the rpc + * @param options - the options dictionary which configures drc + * @return 0 on success, non-zero integer on failure + */ +int +rpcsvc_drc_init(rpcsvc_t *svc, dict_t *options) +{ + int ret = 0; + uint32_t drc_type = 0; + uint32_t drc_size = 0; + uint32_t drc_factor = 0; + rpcsvc_drc_globals_t *drc = NULL; + + GF_ASSERT(svc); + GF_ASSERT(options); + + /* Toggle DRC on/off, when more drc types(persistent/cluster) + * are added, we shouldn't treat this as boolean. */ + ret = dict_get_str_boolean(options, "nfs.drc", _gf_false); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_INFO, "drc user options need second look"); + ret = _gf_false; + } + + gf_log(GF_RPCSVC, GF_LOG_INFO, "DRC is turned %s", (ret ? "ON" : "OFF")); + + /*DRC off, nothing to do */ + if (ret == _gf_false) + return (0); + + drc = GF_CALLOC(1, sizeof(rpcsvc_drc_globals_t), + gf_common_mt_drc_globals_t); + if (!drc) + return (-1); + + LOCK_INIT(&drc->lock); + svc->drc = drc; + + /* Specify type of DRC to be used */ + ret = dict_get_uint32(options, "nfs.drc-type", &drc_type); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "drc type not set. Continuing with default"); + drc_type = DRC_DEFAULT_TYPE; + } + + /* Set the global cache size (no. of ops to cache) */ + ret = dict_get_uint32(options, "nfs.drc-size", &drc_size); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "drc size not set. Continuing with default size"); + drc_size = DRC_DEFAULT_CACHE_SIZE; + } + + LOCK(&drc->lock); + + drc->type = drc_type; + drc->global_cache_size = drc_size; + + /* Mempool for cached ops */ + drc->mempool = mem_pool_new(drc_cached_op_t, drc->global_cache_size); + if (!drc->mempool) { + UNLOCK(&drc->lock); + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to get mempool for DRC, drc-size: %d", drc_size); + ret = -1; + goto post_unlock; + } + + /* What percent of cache to be evicted whenever it fills up */ + ret = dict_get_uint32(options, "nfs.drc-lru-factor", &drc_factor); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "drc lru factor not set. Continuing with policy default"); + drc_factor = DRC_DEFAULT_LRU_FACTOR; + } + + drc->lru_factor = (drc_lru_factor_t)drc_factor; + + INIT_LIST_HEAD(&drc->clients_head); + INIT_LIST_HEAD(&drc->cache_head); + + ret = rpcsvc_register_notify(svc, rpcsvc_drc_notify, THIS); + if (ret) { + UNLOCK(&drc->lock); + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "registration of drc_notify function failed"); + goto post_unlock; + } + + drc->status = DRC_INITIATED; + UNLOCK(&drc->lock); + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "drc init successful"); +post_unlock: + if (ret == -1) { + if (drc->mempool) { + mem_pool_destroy(drc->mempool); + drc->mempool = NULL; + } + GF_FREE(drc); + svc->drc = NULL; + } + return ret; +} + +int +rpcsvc_drc_deinit(rpcsvc_t *svc) +{ + rpcsvc_drc_globals_t *drc = NULL; + + if (!svc) + return (-1); + + drc = svc->drc; + if (!drc) + return (0); + + LOCK(&drc->lock); + (void)rpcsvc_unregister_notify(svc, rpcsvc_drc_notify, THIS); + if (drc->mempool) { + mem_pool_destroy(drc->mempool); + drc->mempool = NULL; + } + UNLOCK(&drc->lock); + + GF_FREE(drc); + svc->drc = NULL; + + return (0); +} + +int +rpcsvc_drc_reconfigure(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + gf_boolean_t enable_drc = _gf_false; + rpcsvc_drc_globals_t *drc = NULL; + uint32_t drc_size = 0; + + /* Input sanitization */ + if ((!svc) || (!options)) + return (-1); + + /* If DRC was not enabled before, Let rpcsvc_drc_init() to + * take care of DRC initialization part. + */ + drc = svc->drc; + if (!drc) { + return rpcsvc_drc_init(svc, options); + } + + /* DRC was already enabled before. Going to be reconfigured. Check + * if reconfigured options contain "nfs.drc" and "nfs.drc-size". + * + * NB: If DRC is "OFF", "drc-size" has no role to play. + * So, "drc-size" gets evaluated IFF DRC is "ON". + * + * If DRC is reconfigured, + * case 1: DRC is "ON" + * sub-case 1: drc-size remains same + * ACTION: Nothing to do. + * sub-case 2: drc-size just changed + * ACTION: rpcsvc_drc_deinit() followed by + * rpcsvc_drc_init(). + * + * case 2: DRC is "OFF" + * ACTION: rpcsvc_drc_deinit() + */ + ret = dict_get_str_boolean(options, "nfs.drc", _gf_false); + if (ret < 0) + ret = _gf_false; + + enable_drc = ret; + gf_log(GF_RPCSVC, GF_LOG_INFO, "DRC is turned %s", (ret ? "ON" : "OFF")); + + /* case 1: DRC is "ON"*/ + if (enable_drc) { + /* Fetch drc-size if reconfigured */ + if (dict_get_uint32(options, "nfs.drc-size", &drc_size)) + drc_size = DRC_DEFAULT_CACHE_SIZE; + + /* case 1: sub-case 1*/ + if (drc->global_cache_size == drc_size) + return (0); + + /* case 1: sub-case 2*/ + (void)rpcsvc_drc_deinit(svc); + return rpcsvc_drc_init(svc, options); + } + + /* case 2: DRC is "OFF" */ + return rpcsvc_drc_deinit(svc); +} diff --git a/rpc/rpc-lib/src/rpc-drc.h b/rpc/rpc-lib/src/rpc-drc.h new file mode 100644 index 00000000000..ce66430809b --- /dev/null +++ b/rpc/rpc-lib/src/rpc-drc.h @@ -0,0 +1,97 @@ +/* + Copyright (c) 2013 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef RPC_DRC_H +#define RPC_DRC_H + +#include "rpcsvc-common.h" +#include "rpcsvc.h" +#include <glusterfs/locking.h> +#include <glusterfs/dict.h> +#include "rb.h" + +/* per-client cache structure */ +struct drc_client { + union gf_sock_union sock_union; + /* pointers to the cache */ + struct rb_table *rbtree; + /* no. of ops currently cached */ + uint32_t op_count; + gf_atomic_uint32_t ref; + struct list_head client_list; +}; + +struct drc_cached_op { + drc_op_state_t state; + int prognum; + int progversion; + int procnum; + rpc_transport_msg_t msg; + drc_client_t *client; + struct list_head client_list; + struct list_head global_list; + int32_t ref; + uint32_t xid; +}; + +/* global drc definitions */ +enum drc_status { DRC_UNINITIATED, DRC_INITIATED }; +typedef enum drc_status drc_status_t; + +struct drc_globals { + /* allocator must be the first member since + * it is used so in gf_libavl_allocator + */ + struct libavl_allocator allocator; + /* configurable size parameter */ + gf_lock_t lock; + uint64_t cache_hits; + uint64_t intransit_hits; + struct mem_pool *mempool; + struct list_head cache_head; + struct list_head clients_head; + uint32_t op_count; + uint32_t client_count; + uint32_t global_cache_size; + drc_type_t type; + drc_lru_factor_t lru_factor; + drc_status_t status; +}; + +int +rpcsvc_need_drc(rpcsvc_request_t *req); + +drc_cached_op_t * +rpcsvc_drc_lookup(rpcsvc_request_t *req); + +int +rpcsvc_send_cached_reply(rpcsvc_request_t *req, drc_cached_op_t *reply); + +int +rpcsvc_cache_reply(rpcsvc_request_t *req, struct iobref *iobref, + struct iovec *rpchdr, int rpchdrcount, struct iovec *proghdr, + int proghdrcount, struct iovec *payload, int payloadcount); + +int +rpcsvc_cache_request(rpcsvc_request_t *req); + +int32_t +rpcsvc_drc_priv(rpcsvc_drc_globals_t *drc); + +int +rpcsvc_drc_init(rpcsvc_t *svc, dict_t *options); + +int +rpcsvc_drc_deinit(rpcsvc_t *svc); + +int +rpcsvc_drc_reconfigure(rpcsvc_t *svc, dict_t *options); + +#endif /* RPC_DRC_H */ diff --git a/rpc/rpc-lib/src/rpc-lib-messages.h b/rpc/rpc-lib/src/rpc-lib-messages.h new file mode 100644 index 00000000000..2c0b820dbf9 --- /dev/null +++ b/rpc/rpc-lib/src/rpc-lib-messages.h @@ -0,0 +1,34 @@ +/* + Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _RPC_LIB_MESSAGES_H_ +#define _RPC_LIB_MESSAGES_H_ + +#include <glusterfs/glfs-message-id.h> + +/* To add new message IDs, append new identifiers at the end of the list. + * + * Never remove a message ID. If it's not used anymore, you can rename it or + * leave it as it is, but not delete it. This is to prevent reutilization of + * IDs by other messages. + * + * The component name must match one of the entries defined in + * glfs-message-id.h. + */ + +GLFS_MSGID(RPC_LIB, TRANS_MSG_ADDR_FAMILY_NOT_SPECIFIED, + TRANS_MSG_UNKNOWN_ADDR_FAMILY, TRANS_MSG_REMOTE_HOST_ERROR, + TRANS_MSG_DNS_RESOL_FAILED, TRANS_MSG_LISTEN_PATH_ERROR, + TRANS_MSG_CONNECT_PATH_ERROR, TRANS_MSG_GET_ADDR_INFO_FAILED, + TRANS_MSG_PORT_BIND_FAILED, TRANS_MSG_INET_ERROR, + TRANS_MSG_GET_NAME_INFO_FAILED, TRANS_MSG_TRANSPORT_ERROR, + TRANS_MSG_TIMEOUT_EXCEEDED, TRANS_MSG_SOCKET_BIND_ERROR); + +#endif /* !_RPC_LIB_MESSAGES_H_ */ diff --git a/rpc/rpc-lib/src/rpc-transport.c b/rpc/rpc-lib/src/rpc-transport.c new file mode 100644 index 00000000000..a6e201a9b36 --- /dev/null +++ b/rpc/rpc-lib/src/rpc-transport.c @@ -0,0 +1,672 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <dlfcn.h> +#include <stdlib.h> +#include <stdio.h> +#include <sys/poll.h> +#include <stdint.h> + +#include "rpc-transport.h" + +#ifndef GF_OPTION_LIST_EMPTY +#define GF_OPTION_LIST_EMPTY(_opt) (_opt->value[0] == NULL) +#endif + +int32_t +rpc_transport_count(const char *transport_type) +{ + char *transport_dup = NULL; + char *saveptr = NULL; + char *ptr = NULL; + int count = 0; + + if (transport_type == NULL) + return -1; + + transport_dup = gf_strdup(transport_type); + if (transport_dup == NULL) { + return -1; + } + + ptr = strtok_r(transport_dup, ",", &saveptr); + while (ptr != NULL) { + count++; + ptr = strtok_r(NULL, ",", &saveptr); + } + + GF_FREE(transport_dup); + return count; +} + +int +rpc_transport_get_myaddr(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, size_t salen) +{ + int32_t ret = -1; + GF_VALIDATE_OR_GOTO("rpc", this, out); + + ret = this->ops->get_myaddr(this, peeraddr, addrlen, sa, salen); + +out: + return ret; +} + +int32_t +rpc_transport_get_peername(rpc_transport_t *this, char *hostname, int hostlen) +{ + int32_t ret = -1; + GF_VALIDATE_OR_GOTO("rpc", this, out); + + ret = this->ops->get_peername(this, hostname, hostlen); +out: + return ret; +} + +int +rpc_transport_throttle(rpc_transport_t *this, gf_boolean_t onoff) +{ + if (!this->ops->throttle) + return -ENOSYS; + + return this->ops->throttle(this, onoff); +} + +int32_t +rpc_transport_get_peeraddr(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, size_t salen) +{ + int32_t ret = -1; + GF_VALIDATE_OR_GOTO("rpc", this, out); + + ret = this->ops->get_peeraddr(this, peeraddr, addrlen, sa, salen); +out: + return ret; +} + +void +rpc_transport_pollin_destroy(rpc_transport_pollin_t *pollin) +{ + GF_VALIDATE_OR_GOTO("rpc", pollin, out); + + if (pollin->iobref) { + iobref_unref(pollin->iobref); + } + + if (pollin->private) { + /* */ + GF_FREE(pollin->private); + } + + GF_FREE(pollin); +out: + return; +} + +rpc_transport_pollin_t * +rpc_transport_pollin_alloc(rpc_transport_t *this, struct iovec *vector, + int count, struct iobuf *hdr_iobuf, + struct iobref *iobref, void *private) +{ + rpc_transport_pollin_t *msg = NULL; + msg = GF_CALLOC(1, sizeof(*msg), gf_common_mt_rpc_trans_pollin_t); + if (!msg) { + goto out; + } + + msg->trans = this; + + if (count > 1) { + msg->vectored = 1; + } + + memcpy(msg->vector, vector, count * sizeof(*vector)); + msg->count = count; + msg->iobref = iobref_ref(iobref); + msg->private = private; + if (hdr_iobuf) + iobref_add(iobref, hdr_iobuf); + +out: + return msg; +} + +void +rpc_transport_cleanup(rpc_transport_t *trans) +{ + if (!trans) + return; + + if (trans->fini) + trans->fini(trans); + + if (trans->options) { + dict_unref(trans->options); + trans->options = NULL; + } + + GF_FREE(trans->name); + + if (trans->xl) + pthread_mutex_destroy(&trans->lock); + + if (trans->dl_handle) + dlclose(trans->dl_handle); + + GF_FREE(trans); +} + +rpc_transport_t * +rpc_transport_load(glusterfs_ctx_t *ctx, dict_t *options, char *trans_name) +{ + struct rpc_transport *trans = NULL, *return_trans = NULL; + char *name = NULL; + void *handle = NULL; + char *type = NULL; + static char str[] = "ERROR"; + int32_t ret = -1; + int is_tcp = 0, is_unix = 0, is_ibsdp = 0; + volume_opt_list_t *vol_opt = NULL; + gf_boolean_t bind_insecure = _gf_false; + xlator_t *this = NULL; + gf_boolean_t success = _gf_false; + + GF_VALIDATE_OR_GOTO("rpc-transport", options, fail); + GF_VALIDATE_OR_GOTO("rpc-transport", ctx, fail); + GF_VALIDATE_OR_GOTO("rpc-transport", trans_name, fail); + + trans = GF_CALLOC(1, sizeof(struct rpc_transport), + gf_common_mt_rpc_trans_t); + if (!trans) + goto fail; + + trans->name = gf_strdup(trans_name); + if (!trans->name) + goto fail; + + trans->ctx = ctx; + type = str; + + /* Backward compatibility */ + ret = dict_get_str_sizen(options, "transport-type", &type); + if (ret < 0) { + ret = dict_set_str_sizen(options, "transport-type", "socket"); + if (ret < 0) + gf_log("dict", GF_LOG_DEBUG, "setting transport-type failed"); + else + gf_log("rpc-transport", GF_LOG_DEBUG, + "missing 'option transport-type'. defaulting to " + "\"socket\""); + } else { + { + /* Backward compatibility to handle * /client, + * * /server. + */ + char *tmp = strchr(type, '/'); + if (tmp) + *tmp = '\0'; + } + + is_tcp = strcmp(type, "tcp"); + is_unix = strcmp(type, "unix"); + is_ibsdp = strcmp(type, "ib-sdp"); + if ((is_tcp == 0) || (is_unix == 0) || (is_ibsdp == 0)) { + if (is_unix == 0) + ret = dict_set_str_sizen(options, "transport.address-family", + "unix"); + if (is_ibsdp == 0) + ret = dict_set_str_sizen(options, "transport.address-family", + "inet-sdp"); + + if (ret < 0) + gf_log("dict", GF_LOG_DEBUG, "setting address-family failed"); + + ret = dict_set_str_sizen(options, "transport-type", "socket"); + if (ret < 0) + gf_log("dict", GF_LOG_DEBUG, "setting transport-type failed"); + } + } + + /* client-bind-insecure is for clients protocol, and + * bind-insecure for glusterd. Both mutually exclusive + */ + ret = dict_get_str_sizen(options, "client-bind-insecure", &type); + if (ret) + ret = dict_get_str_sizen(options, "bind-insecure", &type); + if (ret == 0) { + ret = gf_string2boolean(type, &bind_insecure); + if (ret < 0) { + gf_log("rcp-transport", GF_LOG_WARNING, + "bind-insecure option %s is not a" + " valid bool option", + type); + goto fail; + } + if (_gf_true == bind_insecure) + trans->bind_insecure = 1; + else + trans->bind_insecure = 0; + } else { + /* By default allow bind insecure */ + trans->bind_insecure = 1; + } + + ret = dict_get_str_sizen(options, "transport-type", &type); + if (ret < 0) { + gf_log("rpc-transport", GF_LOG_ERROR, + "'option transport-type <xx>' missing in volume '%s'", + trans_name); + goto fail; + } + + ret = gf_asprintf(&name, "%s/%s.so", RPC_TRANSPORTDIR, type); + if (-1 == ret) { + goto fail; + } + + if (dict_get_sizen(options, "notify-poller-death")) { + trans->notify_poller_death = 1; + } + + gf_log("rpc-transport", GF_LOG_DEBUG, "attempt to load file %s", name); + + handle = dlopen(name, RTLD_NOW); + if (handle == NULL) { + gf_log("rpc-transport", GF_LOG_ERROR, "%s", dlerror()); + gf_log("rpc-transport", GF_LOG_WARNING, + "volume '%s': transport-type '%s' is not valid or " + "not found on this machine", + trans_name, type); + goto fail; + } + + trans->dl_handle = handle; + + trans->ops = dlsym(handle, "tops"); + if (trans->ops == NULL) { + gf_log("rpc-transport", GF_LOG_ERROR, "dlsym (rpc_transport_ops) on %s", + dlerror()); + goto fail; + } + + *VOID(&(trans->init)) = dlsym(handle, "init"); + if (trans->init == NULL) { + gf_log("rpc-transport", GF_LOG_ERROR, + "dlsym (gf_rpc_transport_init) on %s", dlerror()); + goto fail; + } + + *VOID(&(trans->fini)) = dlsym(handle, "fini"); + if (trans->fini == NULL) { + gf_log("rpc-transport", GF_LOG_ERROR, + "dlsym (gf_rpc_transport_fini) on %s", dlerror()); + goto fail; + } + + *VOID(&(trans->reconfigure)) = dlsym(handle, "reconfigure"); + if (trans->reconfigure == NULL) { + gf_log("rpc-transport", GF_LOG_DEBUG, + "dlsym (gf_rpc_transport_reconfigure) on %s", dlerror()); + } + + vol_opt = GF_CALLOC(1, sizeof(volume_opt_list_t), + gf_common_mt_volume_opt_list_t); + if (!vol_opt) { + goto fail; + } + + this = THIS; + vol_opt->given_opt = dlsym(handle, "options"); + if (vol_opt->given_opt == NULL) { + gf_log("rpc-transport", GF_LOG_DEBUG, + "volume option validation not specified"); + } else { + INIT_LIST_HEAD(&vol_opt->list); + list_add_tail(&vol_opt->list, &(this->volume_options)); + if (xlator_options_validate_list(this, options, vol_opt, NULL)) { + gf_log("rpc-transport", GF_LOG_ERROR, + "volume option validation failed"); + goto fail; + } + } + + trans->options = dict_ref(options); + + pthread_mutex_init(&trans->lock, NULL); + trans->xl = this; + + ret = trans->init(trans); + if (ret != 0) { + gf_log("rpc-transport", GF_LOG_WARNING, "'%s' initialization failed", + type); + goto fail; + } + + INIT_LIST_HEAD(&trans->list); + GF_ATOMIC_INIT(trans->disconnect_progress, 0); + + return_trans = trans; + + GF_FREE(name); + + success = _gf_true; + +fail: + if (!success) { + rpc_transport_cleanup(trans); + GF_FREE(name); + + return_trans = NULL; + } + + if (vol_opt) { + if (!list_empty(&vol_opt->list)) { + list_del_init(&vol_opt->list); + } + GF_FREE(vol_opt); + } + + return return_trans; +} + +int32_t +rpc_transport_submit_request(rpc_transport_t *this, rpc_transport_req_t *req) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + GF_VALIDATE_OR_GOTO("rpc_transport", this->ops, fail); + + ret = this->ops->submit_request(this, req); +fail: + return ret; +} + +int32_t +rpc_transport_submit_reply(rpc_transport_t *this, rpc_transport_reply_t *reply) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + GF_VALIDATE_OR_GOTO("rpc_transport", this->ops, fail); + + ret = this->ops->submit_reply(this, reply); +fail: + return ret; +} + +int32_t +rpc_transport_connect(rpc_transport_t *this, int port) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + + ret = this->ops->connect(this, port); +fail: + return ret; +} + +int32_t +rpc_transport_listen(rpc_transport_t *this) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + + ret = this->ops->listen(this); +fail: + return ret; +} + +int32_t +rpc_transport_disconnect(rpc_transport_t *this, gf_boolean_t wait) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + + ret = this->ops->disconnect(this, wait); + +fail: + return ret; +} + +static void +rpc_transport_destroy(rpc_transport_t *this) +{ + struct dnscache6 *cache = NULL; + + if (this->clnt_options) + dict_unref(this->clnt_options); + if (this->options) + dict_unref(this->options); + if (this->fini) + this->fini(this); + + pthread_mutex_destroy(&this->lock); + + GF_FREE(this->name); + + if (this->dl_handle) + dlclose(this->dl_handle); + + if (this->ssl_name) { + GF_FREE(this->ssl_name); + } + + if (this->dnscache) { + cache = this->dnscache; + if (cache->first) + freeaddrinfo(cache->first); + GF_FREE(this->dnscache); + } + + GF_FREE(this); +} + +rpc_transport_t * +rpc_transport_ref(rpc_transport_t *this) +{ + rpc_transport_t *return_this = NULL; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + + GF_ATOMIC_INC(this->refcount); + + return_this = this; +fail: + return return_this; +} + +int32_t +rpc_transport_unref(rpc_transport_t *this) +{ + int32_t refcount = 0; + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("rpc_transport", this, fail); + + refcount = GF_ATOMIC_DEC(this->refcount); + + if (refcount == 0) { + if (this->mydata) + this->notify(this, this->mydata, RPC_TRANSPORT_CLEANUP, NULL); + this->mydata = NULL; + this->notify = NULL; + rpc_transport_destroy(this); + } + + ret = 0; +fail: + return ret; +} + +int32_t +rpc_transport_notify(rpc_transport_t *this, rpc_transport_event_t event, + void *data, ...) +{ + int32_t ret = -1; + GF_VALIDATE_OR_GOTO("rpc", this, out); + + if (this->notify != NULL) { + ret = this->notify(this, this->mydata, event, data); + } else { + ret = 0; + } +out: + return ret; +} + +int +rpc_transport_register_notify(rpc_transport_t *trans, + rpc_transport_notify_t notify, void *mydata) +{ + int32_t ret = -1; + GF_VALIDATE_OR_GOTO("rpc", trans, out); + + trans->notify = notify; + trans->mydata = mydata; + + ret = 0; +out: + return ret; +} + +// give negative values to skip setting that value +// this function asserts if both the values are negative. +// why call it if you don't set it. +int +rpc_transport_keepalive_options_set(dict_t *options, int32_t interval, + int32_t time, int32_t timeout) +{ + int ret = -1; + + GF_ASSERT(options); + GF_ASSERT((interval > 0) || (time > 0)); + + ret = dict_set_int32_sizen(options, "transport.socket.keepalive-interval", + interval); + if (ret) + goto out; + + ret = dict_set_int32_sizen(options, "transport.socket.keepalive-time", + time); + if (ret) + goto out; + + ret = dict_set_int32_sizen(options, "transport.tcp-user-timeout", timeout); + if (ret) + goto out; +out: + return ret; +} + +int +rpc_transport_unix_options_build(dict_t *dict, char *filepath, + int frame_timeout) +{ + char *fpath = NULL; + int ret = -1; + + GF_ASSERT(filepath); + GF_VALIDATE_OR_GOTO("rpc-transport", dict, out); + + fpath = gf_strdup(filepath); + if (!fpath) { + ret = -1; + goto out; + } + + ret = dict_set_dynstr_sizen(dict, "transport.socket.connect-path", fpath); + if (ret) { + GF_FREE(fpath); + goto out; + } + + ret = dict_set_str_sizen(dict, "transport.address-family", "unix"); + if (ret) + goto out; + + ret = dict_set_str_sizen(dict, "transport.socket.nodelay", "off"); + if (ret) + goto out; + + ret = dict_set_str_sizen(dict, "transport-type", "socket"); + if (ret) + goto out; + + ret = dict_set_str_sizen(dict, "transport.socket.keepalive", "off"); + if (ret) + goto out; + + if (frame_timeout > 0) { + ret = dict_set_int32_sizen(dict, "frame-timeout", frame_timeout); + if (ret) + goto out; + } +out: + return ret; +} + +int +rpc_transport_inet_options_build(dict_t *dict, const char *hostname, int port, + char *af) +{ + char *host = NULL; + int ret = -1; +#ifdef IPV6_DEFAULT + static char *addr_family = "inet6"; +#else + static char *addr_family = "inet"; +#endif + + GF_ASSERT(hostname); + GF_ASSERT(port >= 1024); + GF_VALIDATE_OR_GOTO("rpc-transport", dict, out); + + host = gf_strdup((char *)hostname); + if (!host) { + ret = -1; + goto out; + } + + ret = dict_set_dynstr_sizen(dict, "remote-host", host); + if (ret) { + gf_log(THIS->name, GF_LOG_WARNING, "failed to set remote-host with %s", + host); + GF_FREE(host); + goto out; + } + + ret = dict_set_int32_sizen(dict, "remote-port", port); + if (ret) { + gf_log(THIS->name, GF_LOG_WARNING, "failed to set remote-port with %d", + port); + goto out; + } + + ret = dict_set_str_sizen(dict, "address-family", + (af != NULL ? af : addr_family)); + if (ret) { + gf_log(THIS->name, GF_LOG_WARNING, "failed to set address-family to %s", + addr_family); + goto out; + } + + ret = dict_set_str_sizen(dict, "transport-type", "socket"); + if (ret) { + gf_log(THIS->name, GF_LOG_WARNING, + "failed to set trans-type with socket"); + goto out; + } +out: + return ret; +} diff --git a/rpc/rpc-lib/src/rpc-transport.h b/rpc/rpc-lib/src/rpc-transport.h new file mode 100644 index 00000000000..c499f0bb955 --- /dev/null +++ b/rpc/rpc-lib/src/rpc-transport.h @@ -0,0 +1,312 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef __RPC_TRANSPORT_H__ +#define __RPC_TRANSPORT_H__ + +#include <inttypes.h> +#ifdef GF_SOLARIS_HOST_OS +#include <rpc/auth.h> +#else +#include <rpc/rpc.h> +#endif + +#include <rpc/rpc_msg.h> + +#ifndef MAX_IOVEC +#define MAX_IOVEC 16 +#endif + +#ifndef AI_ADDRCONFIG +#define AI_ADDRCONFIG 0 +#endif /* AI_ADDRCONFIG */ + +/* Given the 4-byte fragment header, returns non-zero if this fragment + * is the last fragment for the RPC record being assembled. + * RPC Record marking standard defines a 32 bit value as the fragment + * header with the MSB signifying whether the fragment is the last + * fragment for the record being assembled. + */ +#define RPC_LASTFRAG(fraghdr) ((uint32_t)(fraghdr & 0x80000000U)) + +/* Given the 4-byte fragment header, extracts the bits that contain + * the fragment size. + */ +#define RPC_FRAGSIZE(fraghdr) ((uint32_t)(fraghdr & 0x7fffffffU)) + +#define RPC_FRAGHDR_SIZE 4 +#define RPC_MSGTYPE_SIZE 8 + +/* size of the msg from the start of call-body till and including credlen */ +#define RPC_CALL_BODY_SIZE 24 + +#define RPC_REPLY_STATUS_SIZE 4 + +#define RPC_AUTH_FLAVOUR_N_LENGTH_SIZE 8 + +#define RPC_ACCEPT_STATUS_LEN 4 + +struct rpc_transport_ops; +typedef struct rpc_transport rpc_transport_t; + +#include <glusterfs/dict.h> +#include <glusterfs/compat.h> +#include <glusterfs/async.h> +#include "rpcsvc-common.h" + +struct peer_info { + // OP-VERSION of clients + uint32_t max_op_version; + uint32_t min_op_version; + struct sockaddr_storage sockaddr; + socklen_t sockaddr_len; + char identifier[UNIX_PATH_MAX]; + // Volume mounted by client + char volname[NAME_MAX]; +}; +typedef struct peer_info peer_info_t; + +typedef enum msg_type msg_type_t; + +typedef enum { + RPC_TRANSPORT_ACCEPT, /* New client has been accepted */ + RPC_TRANSPORT_DISCONNECT, /* Connection is disconnected */ + RPC_TRANSPORT_CLEANUP, /* connection is about to be freed */ + /*RPC_TRANSPORT_READ,*/ /* An event used to enable rpcsvc to instruct + * transport the number of bytes to read. + * This helps in reading large msgs, wherein + * the rpc actors might decide to place the + * actor's payload in new iobufs separate + * from the rpc header, proghdr and + * authentication information. glusterfs/nfs + * read and write actors are few examples + * that might beniefit from this. While + * reading a single msg, this event may be + * delivered more than once. + */ + RPC_TRANSPORT_MAP_XID_REQUEST, /* receiver of this event should send + * the prognum and procnum corresponding + * to xid. + */ + RPC_TRANSPORT_MSG_RECEIVED, /* Complete rpc msg has been read */ + RPC_TRANSPORT_CONNECT, /* client is connected to server */ + RPC_TRANSPORT_MSG_SENT, + RPC_TRANSPORT_EVENT_THREAD_DIED /* event-thread has died */ +} rpc_transport_event_t; + +struct rpc_transport_msg { + struct iovec *rpchdr; + struct iovec *proghdr; + int rpchdrcount; + int proghdrcount; + struct iovec *progpayload; + struct iobref *iobref; + int progpayloadcount; +}; +typedef struct rpc_transport_msg rpc_transport_msg_t; + +struct rpc_transport_rsp { + struct iovec *rsphdr; + struct iovec *rsp_payload; + int rsphdr_count; + int rsp_payload_count; + struct iobref *rsp_iobref; +}; +typedef struct rpc_transport_rsp rpc_transport_rsp_t; + +struct rpc_transport_req { + struct rpc_req *rpc_req; + rpc_transport_msg_t msg; + rpc_transport_rsp_t rsp; +}; +typedef struct rpc_transport_req rpc_transport_req_t; + +struct rpc_transport_reply { + void *private; + rpc_transport_msg_t msg; +}; +typedef struct rpc_transport_reply rpc_transport_reply_t; + +struct rpc_transport_data { + union { + rpc_transport_req_t req; + rpc_transport_reply_t reply; + } data; + char is_request; +}; +typedef struct rpc_transport_data rpc_transport_data_t; + +/* FIXME: prognum, procnum and progver are already present in + * rpc_request, hence these should be removed from request_info + */ +struct rpc_request_info { + int prognum; + int progver; + void *rpc_req; /* struct rpc_req */ + rpc_transport_rsp_t rsp; + int procnum; + uint32_t xid; +}; +typedef struct rpc_request_info rpc_request_info_t; + +typedef int (*rpc_transport_notify_t)(rpc_transport_t *, void *mydata, + rpc_transport_event_t, void *data, ...); + +struct rpc_transport { + struct rpc_transport_ops *ops; + rpc_transport_t *listener; /* listener transport to which + * request for creation of this + * transport came from. valid only + * on server process. + */ + + void *private; + struct _client *xl_private; + void *xl; /* Used for THIS */ + void *mydata; + pthread_mutex_t lock; + gf_atomic_t refcount; + glusterfs_ctx_t *ctx; + dict_t *options; + char *name; + void *dnscache; + void *drc_client; + data_t *buf; + int32_t (*init)(rpc_transport_t *this); + void (*fini)(rpc_transport_t *this); + int (*reconfigure)(rpc_transport_t *this, dict_t *options); + rpc_transport_notify_t notify; + void *notify_data; + peer_info_t peerinfo; + peer_info_t myinfo; + + uint64_t total_bytes_read; + uint64_t total_bytes_write; + uint32_t xid; /* RPC/XID used for callbacks */ + int32_t outstanding_rpc_count; + + struct list_head list; + void *dl_handle; /* handle of dlopen() */ + char *ssl_name; + dict_t *clnt_options; /* store options received from + * client */ + gf_atomic_t disconnect_progress; + int bind_insecure; + /* connect_failed: saves the connect() syscall status as socket_t + * member holding connect() status can't be accessed by higher gfapi + * layer or in client management notification handler functions + */ + gf_boolean_t connect_failed; + char notify_poller_death; + char poller_death_accept; +}; + +struct rpc_transport_pollin { + struct rpc_transport *trans; + void *private; + struct iobref *iobref; + struct iovec vector[MAX_IOVEC]; + gf_async_t async; + int count; + char is_reply; + char vectored; +}; +typedef struct rpc_transport_pollin rpc_transport_pollin_t; + +struct rpc_transport_ops { + /* no need of receive op, msg will be delivered through an event + * notification + */ + int32_t (*submit_request)(rpc_transport_t *this, rpc_transport_req_t *req); + int32_t (*submit_reply)(rpc_transport_t *this, + rpc_transport_reply_t *reply); + int32_t (*connect)(rpc_transport_t *this, int port); + int32_t (*listen)(rpc_transport_t *this); + int32_t (*disconnect)(rpc_transport_t *this, gf_boolean_t wait); + int32_t (*get_peername)(rpc_transport_t *this, char *hostname, int hostlen); + int32_t (*get_peeraddr)(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, socklen_t sasize); + int32_t (*get_myname)(rpc_transport_t *this, char *hostname, int hostlen); + int32_t (*get_myaddr)(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, socklen_t sasize); + int32_t (*throttle)(rpc_transport_t *this, gf_boolean_t onoff); +}; + +int32_t +rpc_transport_count(const char *transport_type); + +int32_t +rpc_transport_listen(rpc_transport_t *this); + +int32_t +rpc_transport_connect(rpc_transport_t *this, int port); + +int32_t +rpc_transport_disconnect(rpc_transport_t *this, gf_boolean_t wait); + +int32_t +rpc_transport_notify(rpc_transport_t *this, rpc_transport_event_t event, + void *data, ...); + +int32_t +rpc_transport_submit_request(rpc_transport_t *this, rpc_transport_req_t *req); + +int32_t +rpc_transport_submit_reply(rpc_transport_t *this, rpc_transport_reply_t *reply); + +rpc_transport_t * +rpc_transport_load(glusterfs_ctx_t *ctx, dict_t *options, char *name); + +rpc_transport_t * +rpc_transport_ref(rpc_transport_t *trans); + +int32_t +rpc_transport_unref(rpc_transport_t *trans); + +int +rpc_transport_register_notify(rpc_transport_t *trans, rpc_transport_notify_t, + void *mydata); + +int32_t +rpc_transport_get_peername(rpc_transport_t *this, char *hostname, int hostlen); + +int32_t +rpc_transport_get_peeraddr(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, size_t salen); + +int32_t +rpc_transport_get_myaddr(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, size_t salen); + +int +rpc_transport_throttle(rpc_transport_t *this, gf_boolean_t onoff); + +rpc_transport_pollin_t * +rpc_transport_pollin_alloc(rpc_transport_t *this, struct iovec *vector, + int count, struct iobuf *hdr_iobuf, + struct iobref *iobref, void *private); +void +rpc_transport_pollin_destroy(rpc_transport_pollin_t *pollin); + +int +rpc_transport_keepalive_options_set(dict_t *options, int32_t interval, + int32_t time, int32_t timeout); + +int +rpc_transport_unix_options_build(dict_t *options, char *filepath, + int frame_timeout); + +int +rpc_transport_inet_options_build(dict_t *options, const char *hostname, + int port, char *af); + +void +rpc_transport_cleanup(rpc_transport_t *); +#endif /* __RPC_TRANSPORT_H__ */ diff --git a/rpc/rpc-lib/src/rpcsvc-auth.c b/rpc/rpc-lib/src/rpcsvc-auth.c new file mode 100644 index 00000000000..8e76b4188bb --- /dev/null +++ b/rpc/rpc-lib/src/rpcsvc-auth.c @@ -0,0 +1,561 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#include <glusterfs/dict.h> + +extern rpcsvc_auth_t * +rpcsvc_auth_null_init(rpcsvc_t *svc, dict_t *options); + +extern rpcsvc_auth_t * +rpcsvc_auth_unix_init(rpcsvc_t *svc, dict_t *options); + +extern rpcsvc_auth_t * +rpcsvc_auth_glusterfs_init(rpcsvc_t *svc, dict_t *options); +extern rpcsvc_auth_t * +rpcsvc_auth_glusterfs_v2_init(rpcsvc_t *svc, dict_t *options); +extern rpcsvc_auth_t * +rpcsvc_auth_glusterfs_v3_init(rpcsvc_t *svc, dict_t *options); + +int +rpcsvc_auth_add_initer(struct list_head *list, char *idfier, + rpcsvc_auth_initer_t init) +{ + struct rpcsvc_auth_list *new = NULL; + + if ((!list) || (!init) || (!idfier)) + return -1; + + new = GF_CALLOC(1, sizeof(*new), gf_common_mt_rpcsvc_auth_list); + if (!new) { + return -1; + } + + new->init = init; + strncpy(new->name, idfier, sizeof(new->name) - 1); + INIT_LIST_HEAD(&new->authlist); + list_add_tail(&new->authlist, list); + return 0; +} + +int +rpcsvc_auth_add_initers(rpcsvc_t *svc) +{ + int ret = -1; + + ret = rpcsvc_auth_add_initer( + &svc->authschemes, "auth-glusterfs", + (rpcsvc_auth_initer_t)rpcsvc_auth_glusterfs_init); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add AUTH_GLUSTERFS"); + goto err; + } + + ret = rpcsvc_auth_add_initer( + &svc->authschemes, "auth-glusterfs-v2", + (rpcsvc_auth_initer_t)rpcsvc_auth_glusterfs_v2_init); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add AUTH_GLUSTERFS-v2"); + goto err; + } + + ret = rpcsvc_auth_add_initer( + &svc->authschemes, "auth-glusterfs-v3", + (rpcsvc_auth_initer_t)rpcsvc_auth_glusterfs_v3_init); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add AUTH_GLUSTERFS-v3"); + goto err; + } + + ret = rpcsvc_auth_add_initer(&svc->authschemes, "auth-unix", + (rpcsvc_auth_initer_t)rpcsvc_auth_unix_init); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add AUTH_UNIX"); + goto err; + } + + ret = rpcsvc_auth_add_initer(&svc->authschemes, "auth-null", + (rpcsvc_auth_initer_t)rpcsvc_auth_null_init); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add AUTH_NULL"); + goto err; + } + + ret = 0; +err: + return ret; +} + +int +rpcsvc_auth_init_auth(rpcsvc_t *svc, dict_t *options, + struct rpcsvc_auth_list *authitem) +{ + int ret = -1; + + if ((!svc) || (!options) || (!authitem)) + return -1; + + if (!authitem->init) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "No init function defined"); + ret = -1; + goto err; + } + + authitem->auth = authitem->init(svc, options); + if (!authitem->auth) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Registration of auth failed:" + " %s", + authitem->name); + ret = -1; + goto err; + } + + authitem->enable = 1; + gf_log(GF_RPCSVC, GF_LOG_TRACE, "Authentication enabled: %s", + authitem->auth->authname); + + ret = 0; +err: + return ret; +} + +int +rpcsvc_auth_init_auths(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + struct rpcsvc_auth_list *auth = NULL; + struct rpcsvc_auth_list *tmp = NULL; + + if (!svc) + return -1; + + if (list_empty(&svc->authschemes)) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "No authentication!"); + ret = 0; + goto err; + } + + /* If auth null and sys are not disabled by the user, we must enable + * it by default. This is a globally default rule, the user is still + * allowed to disable the two for particular subvolumes. + */ + if (!dict_get(options, "rpc-auth.auth-null")) { + ret = dict_set_str(options, "rpc-auth.auth-null", "on"); + if (ret) + gf_log("rpc-auth", GF_LOG_DEBUG, "dict_set failed for 'auth-nill'"); + } + + if (!dict_get(options, "rpc-auth.auth-unix")) { + ret = dict_set_str(options, "rpc-auth.auth-unix", "on"); + if (ret) + gf_log("rpc-auth", GF_LOG_DEBUG, "dict_set failed for 'auth-unix'"); + } + + if (!dict_get(options, "rpc-auth.auth-glusterfs")) { + ret = dict_set_str(options, "rpc-auth.auth-glusterfs", "on"); + if (ret) + gf_log("rpc-auth", GF_LOG_DEBUG, "dict_set failed for 'auth-unix'"); + } + + list_for_each_entry_safe(auth, tmp, &svc->authschemes, authlist) + { + ret = rpcsvc_auth_init_auth(svc, options, auth); + if (ret == -1) + goto err; + } + + ret = 0; +err: + return ret; +} + +int +rpcsvc_set_addr_namelookup(rpcsvc_t *svc, dict_t *options) +{ + int ret; + static char *addrlookup_key = "rpc-auth.addr.namelookup"; + + if (!svc || !options) + return (-1); + + /* By default it's disabled */ + ret = dict_get_str_boolean(options, addrlookup_key, _gf_false); + if (ret < 0) { + svc->addr_namelookup = _gf_false; + } else { + svc->addr_namelookup = ret; + } + + if (svc->addr_namelookup) + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "Addr-Name lookup enabled"); + + return (0); +} + +int +rpcsvc_set_allow_insecure(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + char *allow_insecure_str = NULL; + gf_boolean_t is_allow_insecure = _gf_false; + + GF_ASSERT(svc); + GF_ASSERT(options); + + ret = dict_get_str(options, "rpc-auth-allow-insecure", &allow_insecure_str); + if (0 == ret) { + ret = gf_string2boolean(allow_insecure_str, &is_allow_insecure); + if (0 == ret) { + if (_gf_true == is_allow_insecure) + svc->allow_insecure = 1; + else + svc->allow_insecure = 0; + } + } else { + /* By default set allow-insecure to true */ + svc->allow_insecure = 1; + + /* setting in options for the sake of functions that look + * configuration params for allow insecure, eg: gf_auth + */ + ret = dict_set_str(options, "rpc-auth-allow-insecure", "on"); + if (ret < 0) + gf_log("rpc-auth", GF_LOG_DEBUG, + "dict_set failed for 'allow-insecure'"); + } + + return ret; +} + +int +rpcsvc_set_root_squash(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + uid_t anonuid = -1; + gid_t anongid = -1; + + GF_ASSERT(svc); + GF_ASSERT(options); + + ret = dict_get_str_boolean(options, "root-squash", 0); + if (ret != -1) + svc->root_squash = ret; + else + svc->root_squash = _gf_false; + + ret = dict_get_uint32(options, "anonuid", &anonuid); + if (!ret) + svc->anonuid = anonuid; + else + svc->anonuid = RPC_NOBODY_UID; + + ret = dict_get_uint32(options, "anongid", &anongid); + if (!ret) + svc->anongid = anongid; + else + svc->anongid = RPC_NOBODY_GID; + + if (svc->root_squash) + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "root squashing enabled " + "(uid=%d, gid=%d)", + svc->anonuid, svc->anongid); + + return 0; +} + +int +rpcsvc_set_all_squash(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + + uid_t anonuid = -1; + gid_t anongid = -1; + + GF_ASSERT(svc); + GF_ASSERT(options); + + ret = dict_get_str_boolean(options, "all-squash", 0); + if (ret != -1) + svc->all_squash = ret; + else + svc->all_squash = _gf_false; + + ret = dict_get_uint32(options, "anonuid", &anonuid); + if (!ret) + svc->anonuid = anonuid; + else + svc->anonuid = RPC_NOBODY_UID; + + ret = dict_get_uint32(options, "anongid", &anongid); + if (!ret) + svc->anongid = anongid; + else + svc->anongid = RPC_NOBODY_GID; + + if (svc->all_squash) + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "all squashing enabled " + "(uid=%d, gid=%d)", + svc->anonuid, svc->anongid); + + return 0; +} + +int +rpcsvc_auth_init(rpcsvc_t *svc, dict_t *options) +{ + int ret = -1; + + if ((!svc) || (!options)) + return -1; + + (void)rpcsvc_set_allow_insecure(svc, options); + (void)rpcsvc_set_root_squash(svc, options); + (void)rpcsvc_set_all_squash(svc, options); + (void)rpcsvc_set_addr_namelookup(svc, options); + ret = rpcsvc_auth_add_initers(svc); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to add initers"); + goto out; + } + + ret = rpcsvc_auth_init_auths(svc, options); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to init auth schemes"); + goto out; + } + +out: + return ret; +} + +int +rpcsvc_auth_reconf(rpcsvc_t *svc, dict_t *options) +{ + int ret = 0; + + if ((!svc) || (!options)) + return (-1); + + ret = rpcsvc_set_allow_insecure(svc, options); + if (ret) + return (-1); + + ret = rpcsvc_set_root_squash(svc, options); + if (ret) + return (-1); + + ret = rpcsvc_set_all_squash(svc, options); + if (ret) + return (-1); + + return rpcsvc_set_addr_namelookup(svc, options); +} + +rpcsvc_auth_t * +__rpcsvc_auth_get_handler(rpcsvc_request_t *req) +{ + struct rpcsvc_auth_list *auth = NULL; + struct rpcsvc_auth_list *tmp = NULL; + rpcsvc_t *svc = NULL; + + if (!req) + return NULL; + + svc = req->svc; + if (!svc) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "!svc"); + goto err; + } + + if (list_empty(&svc->authschemes)) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "No authentication!"); + goto err; + } + + list_for_each_entry_safe(auth, tmp, &svc->authschemes, authlist) + { + if (!auth->enable) + continue; + if (auth->auth->authnum == req->cred.flavour) + goto err; + } + + auth = NULL; +err: + if (auth) + return auth->auth; + else + return NULL; +} + +rpcsvc_auth_t * +rpcsvc_auth_get_handler(rpcsvc_request_t *req) +{ + rpcsvc_auth_t *auth = NULL; + + auth = __rpcsvc_auth_get_handler(req); + if (auth) + goto ret; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, "No auth handler: %d", req->cred.flavour); + + /* The requested scheme was not available so fall back the to one + * scheme that will always be present. + */ + req->cred.flavour = AUTH_NULL; + req->verf.flavour = AUTH_NULL; + auth = __rpcsvc_auth_get_handler(req); +ret: + return auth; +} + +int +rpcsvc_auth_request_init(rpcsvc_request_t *req, struct rpc_msg *callmsg) +{ + int32_t ret = 0; + rpcsvc_auth_t *auth = NULL; + + if (!req || !callmsg) { + ret = -1; + goto err; + } + + req->cred.flavour = rpc_call_cred_flavour(callmsg); + req->cred.datalen = rpc_call_cred_len(callmsg); + req->verf.flavour = rpc_call_verf_flavour(callmsg); + req->verf.datalen = rpc_call_verf_len(callmsg); + + auth = rpcsvc_auth_get_handler(req); + if (!auth) { + ret = -1; + goto err; + } + + gf_log(GF_RPCSVC, GF_LOG_TRACE, "Auth handler: %s", auth->authname); + + if (auth->authops->request_init) + ret = auth->authops->request_init(req, auth->authprivate); + + /* reset to auxgidlarge during + unsersialize if necessary */ + req->auxgids = req->auxgidsmall; + req->auxgidlarge = NULL; +err: + return ret; +} + +int +rpcsvc_authenticate(rpcsvc_request_t *req) +{ + int ret = RPCSVC_AUTH_REJECT; + rpcsvc_auth_t *auth = NULL; + int minauth = 0; + + if (!req) + return ret; + + /* FIXME use rpcsvc_request_prog_minauth() */ + minauth = 0; + if (minauth > rpcsvc_request_cred_flavour(req)) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "Auth too weak"); + rpcsvc_request_set_autherr(req, AUTH_TOOWEAK); + goto err; + } + + auth = rpcsvc_auth_get_handler(req); + if (!auth) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "No auth handler found"); + goto err; + } + + if (auth->authops->authenticate) + ret = auth->authops->authenticate(req, auth->authprivate); + +err: + return ret; +} + +int +rpcsvc_auth_array(rpcsvc_t *svc, char *volname, int *autharr, int arrlen) +{ + int count = 0; + int result = RPCSVC_AUTH_REJECT; + char *srchstr = NULL; + int ret = 0; + + struct rpcsvc_auth_list *auth = NULL; + struct rpcsvc_auth_list *tmp = NULL; + + if ((!svc) || (!autharr) || (!volname)) + return -1; + + memset(autharr, 0, arrlen * sizeof(int)); + if (list_empty(&svc->authschemes)) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "No authentication!"); + goto err; + } + + list_for_each_entry_safe(auth, tmp, &svc->authschemes, authlist) + { + if (count >= arrlen) + break; + + result = gf_asprintf(&srchstr, "rpc-auth.%s.%s", auth->name, volname); + if (result == -1) { + count = -1; + goto err; + } + + ret = dict_get_str_boolean(svc->options, srchstr, 0xC00FFEE); + GF_FREE(srchstr); + + switch (ret) { + case _gf_true: + autharr[count] = auth->auth->authnum; + ++count; + break; + + default: + /* nothing to do */ + break; + } + } + +err: + return count; +} + +gid_t * +rpcsvc_auth_unix_auxgids(rpcsvc_request_t *req, int *arrlen) +{ + if ((!req) || (!arrlen)) + return NULL; + + /* In case of AUTH_NULL auxgids are not used */ + switch (req->cred.flavour) { + case AUTH_UNIX: + case AUTH_GLUSTERFS: + case AUTH_GLUSTERFS_v2: + case AUTH_GLUSTERFS_v3: + break; + default: + gf_log("rpc", GF_LOG_DEBUG, "auth type not unix or glusterfs"); + return NULL; + } + + *arrlen = req->auxgidcount; + if (*arrlen == 0) + return NULL; + + return &req->auxgids[0]; +} diff --git a/rpc/rpc-lib/src/rpcsvc-common.h b/rpc/rpc-lib/src/rpcsvc-common.h new file mode 100644 index 00000000000..6c4ec49a6ef --- /dev/null +++ b/rpc/rpc-lib/src/rpcsvc-common.h @@ -0,0 +1,113 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _RPCSVC_COMMON_H +#define _RPCSVC_COMMON_H + +#include <pthread.h> +#include <glusterfs/compat.h> +#include <glusterfs/dict.h> + +typedef enum { + RPCSVC_EVENT_ACCEPT, + RPCSVC_EVENT_DISCONNECT, + RPCSVC_EVENT_TRANSPORT_DESTROY, + RPCSVC_EVENT_LISTENER_DEAD, +} rpcsvc_event_t; + +struct rpcsvc_state; + +typedef int (*rpcsvc_notify_t)(struct rpcsvc_state *, void *mydata, + rpcsvc_event_t, void *data); + +struct drc_globals; +typedef struct drc_globals rpcsvc_drc_globals_t; + +/* Contains global state required for all the RPC services. + */ +typedef struct rpcsvc_state { + /* Contains list of (program, version) handlers. + * other options. + */ + + pthread_rwlock_t rpclock; + + /* List of the authentication schemes available. */ + struct list_head authschemes; + + /* Reference to the options */ + dict_t *options; + + uid_t anonuid; + gid_t anongid; + glusterfs_ctx_t *ctx; + + /* list of connections which will listen for incoming connections */ + struct list_head listeners; + + /* list of programs registered with rpcsvc */ + struct list_head programs; + + /* list of notification callbacks */ + struct list_head notify; + int notify_count; + + unsigned int memfactor; + + xlator_t *xl; /* xlator */ + void *mydata; + rpcsvc_notify_t notifyfn; + struct mem_pool *rxpool; + rpcsvc_drc_globals_t *drc; + + /* per-client limit of outstanding rpc requests */ + int outstanding_rpc_limit; + gf_boolean_t addr_namelookup; + /* determine whether throttling is needed, by default OFF */ + gf_boolean_t throttle; + /* Allow insecure ports. */ + gf_boolean_t allow_insecure; + gf_boolean_t register_portmap; + gf_boolean_t root_squash; + gf_boolean_t all_squash; +} rpcsvc_t; + +/* DRC START */ +enum drc_op_type { DRC_NA = 0, DRC_IDEMPOTENT = 1, DRC_NON_IDEMPOTENT = 2 }; +typedef enum drc_op_type drc_op_type_t; + +enum drc_type { DRC_TYPE_NONE = 0, DRC_TYPE_IN_MEMORY = 1 }; +typedef enum drc_type drc_type_t; + +enum drc_lru_factor { + DRC_LRU_5_PC = 20, + DRC_LRU_10_PC = 10, + DRC_LRU_25_PC = 4, + DRC_LRU_50_PC = 2 +}; +typedef enum drc_lru_factor drc_lru_factor_t; + +enum drc_xid_state { DRC_XID_MONOTONOUS = 0, DRC_XID_WRAPPED = 1 }; +typedef enum drc_xid_state drc_xid_state_t; + +enum drc_op_state { DRC_OP_IN_TRANSIT = 0, DRC_OP_CACHED = 1 }; +typedef enum drc_op_state drc_op_state_t; + +enum drc_policy { DRC_LRU = 0 }; +typedef enum drc_policy drc_policy_t; + +/* Default policies for DRC */ +#define DRC_DEFAULT_TYPE DRC_TYPE_IN_MEMORY +#define DRC_DEFAULT_CACHE_SIZE 0x20000 +#define DRC_DEFAULT_LRU_FACTOR DRC_LRU_25_PC + +/* DRC END */ + +#endif /* #ifndef _RPCSVC_COMMON_H */ diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c new file mode 100644 index 00000000000..39910d481bf --- /dev/null +++ b/rpc/rpc-lib/src/rpcsvc.c @@ -0,0 +1,3295 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "rpcsvc.h" +#include "rpc-transport.h" +#include <glusterfs/dict.h> +#include <glusterfs/byte-order.h> +#include <glusterfs/compat-errno.h> +#include <glusterfs/statedump.h> +#include "xdr-rpc.h" +#include <glusterfs/iobuf.h> +#include "xdr-common.h" +#include "xdr-generic.h" +#include "rpc-common-xdr.h" +#include <glusterfs/syncop.h> +#include "rpc-drc.h" +#include "protocol-common.h" + +#include <errno.h> +#include <pthread.h> +#include <stdlib.h> +#include <rpc/rpc.h> +#include <rpc/pmap_clnt.h> +#include <arpa/inet.h> +#include <rpc/xdr.h> +#include <fnmatch.h> +#include <stdarg.h> +#include <stdio.h> +#include <dlfcn.h> + +#ifdef IPV6_DEFAULT +#include <netconfig.h> +#endif + +#include "xdr-rpcclnt.h" +#include <glusterfs/glusterfs-acl.h> + +#ifndef PTHREAD_MUTEX_ADAPTIVE_NP +#define PTHREAD_MUTEX_ADAPTIVE_NP PTHREAD_MUTEX_DEFAULT +#endif + +static struct rpcsvc_program gluster_dump_prog; + +#define rpcsvc_alloc_request(svc, request) \ + do { \ + request = (rpcsvc_request_t *)mem_get((svc)->rxpool); \ + if (request) { \ + memset(request, 0, sizeof(rpcsvc_request_t)); \ + } else { \ + gf_log("rpcsvc", GF_LOG_ERROR, \ + "error getting memory for rpc request"); \ + } \ + } while (0) + +rpcsvc_listener_t * +rpcsvc_get_listener(rpcsvc_t *svc, uint16_t port, rpc_transport_t *trans); + +int +rpcsvc_notify(rpc_transport_t *trans, void *mydata, rpc_transport_event_t event, + void *data, ...); +void * +rpcsvc_request_handler(void *arg); + +static int +rpcsvc_match_subnet_v4(const char *addrtok, const char *ipaddr); + +static void +rpcsvc_toggle_queue_status(rpcsvc_program_t *prog, + rpcsvc_request_queue_t *queue, + unsigned long status[]) +{ + unsigned queue_index = queue - prog->request_queue; + + status[queue_index / __BITS_PER_LONG] ^= (1UL << (queue_index % + __BITS_PER_LONG)); +} + +int +rpcsvc_get_free_queue_index(rpcsvc_program_t *prog) +{ + unsigned i, j = 0; + + for (i = 0; i < EVENT_MAX_THREADS / __BITS_PER_LONG; i++) + if (prog->request_queue_status[i] != ULONG_MAX) { + j = __builtin_ctzl(~prog->request_queue_status[i]); + break; + } + + if (i == EVENT_MAX_THREADS / __BITS_PER_LONG) + return -1; + + prog->request_queue_status[i] |= (1UL << j); + return i * __BITS_PER_LONG + j; +} + +rpcsvc_notify_wrapper_t * +rpcsvc_notify_wrapper_alloc(void) +{ + rpcsvc_notify_wrapper_t *wrapper = NULL; + + wrapper = GF_CALLOC(1, sizeof(*wrapper), gf_common_mt_rpcsvc_wrapper_t); + if (!wrapper) { + goto out; + } + + INIT_LIST_HEAD(&wrapper->list); +out: + return wrapper; +} + +void +rpcsvc_listener_destroy(rpcsvc_listener_t *listener) +{ + rpcsvc_t *svc = NULL; + + if (!listener) { + goto out; + } + + svc = listener->svc; + if (!svc) { + goto listener_free; + } + + pthread_rwlock_wrlock(&svc->rpclock); + { + list_del_init(&listener->list); + } + pthread_rwlock_unlock(&svc->rpclock); + +listener_free: + GF_FREE(listener); +out: + return; +} + +rpcsvc_vector_sizer +rpcsvc_get_program_vector_sizer(rpcsvc_t *svc, uint32_t prognum, + uint32_t progver, int procnum) +{ + rpcsvc_program_t *program = NULL; + char found = 0; + + if (!svc) + return NULL; + + pthread_rwlock_rdlock(&svc->rpclock); + { + /* Find the matching RPC program from registered list */ + list_for_each_entry(program, &svc->programs, program) + { + if ((program->prognum == prognum) && + (program->progver == progver)) { + found = 1; + break; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + + if (found) { + /* Make sure the requested procnum is supported by RPC prog */ + if ((procnum < 0) || (procnum >= program->numactors)) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "RPC procedure %d not available for Program %s", procnum, + program->progname); + return NULL; + } + + /* SUCCESS: Supported procedure */ + return program->actors[procnum].vector_sizer; + } + + return NULL; /* FAIL */ +} + +gf_boolean_t +rpcsvc_can_outstanding_req_be_ignored(rpcsvc_request_t *req) +{ + /* + * If outstanding_rpc_limit is reached because of blocked locks and + * throttling is attempted then no unlock requests will be received. So + * the outstanding request count will never change i.e. it will always + * be equal to the limit. This also leads to ping timer expiry on + * client. + */ + + /* + * This is a hack and a necessity until grantedlock == fop completion. + * Ideally if we get a blocking lock request which cannot be granted + * right now, we should unwind the fop saying “request registered, will + * notify you when granted”, which is very hard to implement at the + * moment. Until we bring in such mechanism, we will need to live with + * not rate-limiting INODELK/ENTRYLK/LK fops + */ + + if ((req->prognum == GLUSTER_FOP_PROGRAM) && + (req->progver == GLUSTER_FOP_VERSION)) { + if ((req->procnum == GFS3_OP_INODELK) || + (req->procnum == GFS3_OP_FINODELK) || + (req->procnum == GFS3_OP_ENTRYLK) || + (req->procnum == GFS3_OP_FENTRYLK) || (req->procnum == GFS3_OP_LK)) + return _gf_true; + } + return _gf_false; +} + +int +rpcsvc_request_outstanding(rpcsvc_request_t *req, int delta) +{ + int ret = -1; + int old_count = 0; + int new_count = 0; + int limit = 0; + gf_boolean_t throttle = _gf_false; + + if (!req) + goto out; + + throttle = rpcsvc_get_throttle(req->svc); + if (!throttle) { + ret = 0; + goto out; + } + + if (rpcsvc_can_outstanding_req_be_ignored(req)) { + ret = 0; + goto out; + } + + pthread_mutex_lock(&req->trans->lock); + { + limit = req->svc->outstanding_rpc_limit; + if (!limit) + goto unlock; + + old_count = req->trans->outstanding_rpc_count; + req->trans->outstanding_rpc_count += delta; + new_count = req->trans->outstanding_rpc_count; + + if (old_count <= limit && new_count > limit) + ret = rpc_transport_throttle(req->trans, _gf_true); + + if (old_count > limit && new_count <= limit) + ret = rpc_transport_throttle(req->trans, _gf_false); + } +unlock: + pthread_mutex_unlock(&req->trans->lock); + +out: + return ret; +} + +/* This needs to change to returning errors, since + * we need to return RPC specific error messages when some + * of the pointers below are NULL. + */ +rpcsvc_actor_t * +rpcsvc_program_actor(rpcsvc_request_t *req) +{ + rpcsvc_program_t *program = NULL; + int err = SYSTEM_ERR; + rpcsvc_actor_t *actor = NULL; + rpcsvc_t *svc = NULL; + char found = 0; + char *peername = NULL; + + if (!req) + goto err; + + svc = req->svc; + peername = req->trans->peerinfo.identifier; + pthread_rwlock_rdlock(&svc->rpclock); + { + list_for_each_entry(program, &svc->programs, program) + { + if (program->prognum == req->prognum) { + err = PROG_MISMATCH; + } + + if ((program->prognum == req->prognum) && + (program->progver == req->progver)) { + found = 1; + break; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + + if (!found) { + if (err != PROG_MISMATCH) { + /* log in DEBUG when nfs clients try to see if + * ACL requests are accepted by nfs server + */ + gf_log( + GF_RPCSVC, + (req->prognum == ACL_PROGRAM) ? GF_LOG_DEBUG : GF_LOG_WARNING, + "RPC program not available (req %u %u) for %s", req->prognum, + req->progver, peername); + err = PROG_UNAVAIL; + goto err; + } + + gf_log(GF_RPCSVC, GF_LOG_WARNING, + "RPC program version not available (req %u %u) for %s", + req->prognum, req->progver, peername); + goto err; + } + req->prog = program; + if (!program->actors) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, + "RPC Actor not found for program %s %d for %s", + program->progname, program->prognum, peername); + err = SYSTEM_ERR; + goto err; + } + + if ((req->procnum < 0) || (req->procnum >= program->numactors)) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "RPC Program procedure not" + " available for procedure %d in %s for %s", + req->procnum, program->progname, peername); + err = PROC_UNAVAIL; + goto err; + } + + actor = &program->actors[req->procnum]; + if (!actor->actor) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "RPC Program procedure not" + " available for procedure %d in %s for %s", + req->procnum, program->progname, peername); + err = PROC_UNAVAIL; + actor = NULL; + goto err; + } + + if (svc->xl->ctx->measure_latency) { + timespec_now(&req->begin); + } + + req->ownthread = program->ownthread; + req->synctask = program->synctask; + + err = SUCCESS; + gf_log(GF_RPCSVC, GF_LOG_TRACE, "Actor found: %s - %s for %s", + program->progname, actor->procname, peername); +err: + if (req) + req->rpc_err = err; + + return actor; +} + +/* this procedure can only pass 4 arguments to registered notifyfn. To send more + * arguments call wrapper->notify directly. + */ +static void +rpcsvc_program_notify(rpcsvc_listener_t *listener, rpcsvc_event_t event, + void *data) +{ + rpcsvc_notify_wrapper_t *wrapper = NULL; + + if (!listener) { + goto out; + } + + list_for_each_entry(wrapper, &listener->svc->notify, list) + { + if (wrapper->notify) { + wrapper->notify(listener->svc, wrapper->data, event, data); + } + } + +out: + return; +} + +static int +rpcsvc_accept(rpcsvc_t *svc, rpc_transport_t *listen_trans, + rpc_transport_t *new_trans) +{ + rpcsvc_listener_t *listener = NULL; + int32_t ret = -1; + + listener = rpcsvc_get_listener(svc, -1, listen_trans); + if (listener == NULL) { + goto out; + } + + rpcsvc_program_notify(listener, RPCSVC_EVENT_ACCEPT, new_trans); + ret = 0; +out: + return ret; +} + +void +rpcsvc_request_destroy(rpcsvc_request_t *req) +{ + if (!req) { + goto out; + } + + if (req->iobref) { + iobref_unref(req->iobref); + } + + /* This marks the "end" of an RPC request. Reply is + completely written to the socket and is on the way + to the client. It is time to decrement the + outstanding request counter by 1. + */ + if (req->prognum) // Only for initialized requests + rpcsvc_request_outstanding(req, -1); + + rpc_transport_unref(req->trans); + + GF_FREE(req->auxgidlarge); + + mem_put(req); + +out: + return; +} + +rpcsvc_request_t * +rpcsvc_request_init(rpcsvc_t *svc, rpc_transport_t *trans, + struct rpc_msg *callmsg, struct iovec progmsg, + rpc_transport_pollin_t *msg, rpcsvc_request_t *req) +{ + int i = 0; + + if ((!trans) || (!callmsg) || (!req) || (!msg)) + return NULL; + + /* We start a RPC request as always denied. */ + req->rpc_status = MSG_DENIED; + req->xid = rpc_call_xid(callmsg); + req->prognum = rpc_call_program(callmsg); + req->progver = rpc_call_progver(callmsg); + req->procnum = rpc_call_progproc(callmsg); + req->trans = rpc_transport_ref(trans); + req->count = msg->count; + req->msg[0] = progmsg; + req->iobref = iobref_ref(msg->iobref); + if (msg->vectored) { + /* msg->vector[MAX_IOVEC] is defined in structure. prevent a + out of bound access */ + for (i = 1; i < min(msg->count, MAX_IOVEC); i++) { + req->msg[i] = msg->vector[i]; + } + } + + req->svc = svc; + req->trans_private = msg->private; + + INIT_LIST_HEAD(&req->txlist); + INIT_LIST_HEAD(&req->request_list); + req->payloadsize = 0; + + /* By this time, the data bytes for the auth scheme would have already + * been copied into the required sections of the req structure, + * we just need to fill in the meta-data about it now. + */ + rpcsvc_auth_request_init(req, callmsg); + return req; +} + +rpcsvc_request_t * +rpcsvc_request_create(rpcsvc_t *svc, rpc_transport_t *trans, + rpc_transport_pollin_t *msg) +{ + char *msgbuf = NULL; + struct rpc_msg rpcmsg; + struct iovec progmsg; /* RPC Program payload */ + rpcsvc_request_t *req = NULL; + size_t msglen = 0; + int ret = -1; + + if (!svc || !trans || !svc->rxpool) + return NULL; + + /* We need to allocate the request before actually calling + * rpcsvc_request_init on the request so that we, can fill the auth + * data directly into the request structure from the message iobuf. + * This avoids a need to keep a temp buffer into which the auth data + * would've been copied otherwise. + */ + rpcsvc_alloc_request(svc, req); + if (!req) { + goto err; + } + + msgbuf = msg->vector[0].iov_base; + msglen = msg->vector[0].iov_len; + + ret = xdr_to_rpc_call(msgbuf, msglen, &rpcmsg, &progmsg, req->cred.authdata, + req->verf.authdata); + + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "RPC call decoding failed"); + rpcsvc_request_seterr(req, GARBAGE_ARGS); + req->trans = rpc_transport_ref(trans); + req->svc = svc; + goto err; + } + + ret = -1; + rpcsvc_request_init(svc, trans, &rpcmsg, progmsg, msg, req); + + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "received rpc-message " + "(XID: 0x%" GF_PRI_RPC_XID ", Ver: %" GF_PRI_RPC_VERSION + ", Program: %" GF_PRI_RPC_PROG_ID + ", " + "ProgVers: %" GF_PRI_RPC_PROG_VERS ", Proc: %" GF_PRI_RPC_PROC + ") " + "from rpc-transport (%s)", + rpc_call_xid(&rpcmsg), rpc_call_rpcvers(&rpcmsg), + rpc_call_program(&rpcmsg), rpc_call_progver(&rpcmsg), + rpc_call_progproc(&rpcmsg), trans->name); + + /* We just received a new request from the wire. Account for + it in the outsanding request counter to make sure we don't + ingest too many concurrent requests from the same client. + */ + if (req->prognum) // Only for initialized requests + ret = rpcsvc_request_outstanding(req, +1); + + if (rpc_call_rpcvers(&rpcmsg) != 2) { + /* LOG- TODO: print rpc version, also print the peerinfo + from transport */ + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "RPC version not supported " + "(XID: 0x%" GF_PRI_RPC_XID ", Ver: %" GF_PRI_RPC_VERSION + ", Program: %" GF_PRI_RPC_PROG_ID + ", " + "ProgVers: %" GF_PRI_RPC_PROG_VERS ", Proc: %" GF_PRI_RPC_PROC + ") " + "from trans (%s)", + rpc_call_xid(&rpcmsg), rpc_call_rpcvers(&rpcmsg), + rpc_call_program(&rpcmsg), rpc_call_progver(&rpcmsg), + rpc_call_progproc(&rpcmsg), trans->name); + rpcsvc_request_seterr(req, RPC_MISMATCH); + goto err; + } + + ret = rpcsvc_authenticate(req); + if (ret == RPCSVC_AUTH_REJECT) { + /* No need to set auth_err, that is the responsibility of + * the authentication handler since only that know what exact + * error happened. + */ + rpcsvc_request_seterr(req, AUTH_ERROR); + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "auth failed on request. " + "(XID: 0x%" GF_PRI_RPC_XID ", Ver: %" GF_PRI_RPC_VERSION + ", Program: %" GF_PRI_RPC_PROG_ID + ", " + "ProgVers: %" GF_PRI_RPC_PROG_VERS ", Proc: %" GF_PRI_RPC_PROC + ") " + "from trans (%s)", + rpc_call_xid(&rpcmsg), rpc_call_rpcvers(&rpcmsg), + rpc_call_program(&rpcmsg), rpc_call_progver(&rpcmsg), + rpc_call_progproc(&rpcmsg), trans->name); + ret = -1; + goto err; + } + + /* If the error is not RPC_MISMATCH, we consider the call as accepted + * since we are not handling authentication failures for now. + */ + req->rpc_status = MSG_ACCEPTED; + req->reply = NULL; + ret = 0; +err: + if (ret == -1) { + ret = rpcsvc_error_reply(req); + if (ret) + gf_log("rpcsvc", GF_LOG_WARNING, "failed to queue error reply"); + req = NULL; + } + + return req; +} + +int +rpcsvc_check_and_reply_error(int ret, call_frame_t *frame, void *opaque) +{ + rpcsvc_request_t *req = NULL; + + req = opaque; + + if (ret) + gf_log("rpcsvc", GF_LOG_ERROR, + "rpc actor (%d:%d:%d) failed to complete successfully", + req->prognum, req->progver, req->procnum); + + if (ret == RPCSVC_ACTOR_ERROR) { + ret = rpcsvc_error_reply(req); + if (ret) + gf_log("rpcsvc", GF_LOG_WARNING, "failed to queue error reply"); + } + + return 0; +} + +void +rpcsvc_queue_event_thread_death(rpcsvc_t *svc, rpcsvc_program_t *prog, int gen) +{ + rpcsvc_request_queue_t *queue = NULL; + int num = 0; + void *value = NULL; + rpcsvc_request_t *req = NULL; + char empty = 0; + + value = pthread_getspecific(prog->req_queue_key); + if (value == NULL) { + return; + } + + num = ((unsigned long)value) - 1; + + queue = &prog->request_queue[num]; + + if (queue->gen == gen) { + /* duplicate event */ + gf_log(GF_RPCSVC, GF_LOG_INFO, + "not queuing duplicate event thread death. " + "queue %d program %s", + num, prog->progname); + return; + } + + rpcsvc_alloc_request(svc, req); + req->prognum = RPCSVC_INFRA_PROGRAM; + req->procnum = RPCSVC_PROC_EVENT_THREAD_DEATH; + gf_log(GF_RPCSVC, GF_LOG_INFO, + "queuing event thread death request to queue %d of program %s", num, + prog->progname); + + pthread_mutex_lock(&queue->queue_lock); + { + empty = list_empty(&queue->request_queue); + + list_add_tail(&req->request_list, &queue->request_queue); + queue->gen = gen; + + if (empty && queue->waiting) + pthread_cond_signal(&queue->queue_cond); + } + pthread_mutex_unlock(&queue->queue_lock); + + return; +} + +int +rpcsvc_handle_event_thread_death(rpcsvc_t *svc, rpc_transport_t *trans, int gen) +{ + rpcsvc_program_t *prog = NULL; + + pthread_rwlock_rdlock(&svc->rpclock); + { + list_for_each_entry(prog, &svc->programs, program) + { + if (prog->ownthread) + rpcsvc_queue_event_thread_death(svc, prog, gen); + } + } + pthread_rwlock_unlock(&svc->rpclock); + + return 0; +} + +int +rpcsvc_handle_rpc_call(rpcsvc_t *svc, rpc_transport_t *trans, + rpc_transport_pollin_t *msg) +{ + rpcsvc_actor_t *actor = NULL; + rpcsvc_actor actor_fn = NULL; + rpcsvc_request_t *req = NULL; + int ret = -1; + uint16_t port = 0; + gf_boolean_t is_unix = _gf_false, empty = _gf_false; + gf_boolean_t unprivileged = _gf_false, spawn_request_handler = 0; + drc_cached_op_t *reply = NULL; + rpcsvc_drc_globals_t *drc = NULL; + rpcsvc_request_queue_t *queue = NULL; + long num = 0; + void *value = NULL; + + if (!trans || !svc) + return -1; + + switch (trans->peerinfo.sockaddr.ss_family) { + case AF_INET: + port = ((struct sockaddr_in *)&trans->peerinfo.sockaddr)->sin_port; + break; + + case AF_INET6: + port = ((struct sockaddr_in6 *)&trans->peerinfo.sockaddr) + ->sin6_port; + break; + case AF_UNIX: + is_unix = _gf_true; + break; + default: + gf_log(GF_RPCSVC, GF_LOG_ERROR, "invalid address family (%d)", + trans->peerinfo.sockaddr.ss_family); + return -1; + } + + if (is_unix == _gf_false) { + port = ntohs(port); + + gf_log("rpcsvc", GF_LOG_TRACE, "Client port: %d", (int)port); + + if (port >= 1024) + unprivileged = _gf_true; + } + + req = rpcsvc_request_create(svc, trans, msg); + if (!req) + goto out; + + if (!rpcsvc_request_accepted(req)) + goto err_reply; + + actor = rpcsvc_program_actor(req); + if (!actor) + goto err_reply; + + if (0 == svc->allow_insecure && unprivileged && !actor->unprivileged) { + /* Non-privileged user, fail request */ + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Request received from non-" + "privileged port. Failing request for %s.", + req->trans->peerinfo.identifier); + req->rpc_status = MSG_DENIED; + req->rpc_err = AUTH_ERROR; + req->auth_err = RPCSVC_AUTH_REJECT; + goto err_reply; + } + + /* DRC */ + if (rpcsvc_need_drc(req)) { + drc = req->svc->drc; + + LOCK(&drc->lock); + { + reply = rpcsvc_drc_lookup(req); + + /* retransmission of completed request, send cached reply */ + if (reply && reply->state == DRC_OP_CACHED) { + gf_log(GF_RPCSVC, GF_LOG_INFO, + "duplicate request:" + " XID: 0x%x", + req->xid); + ret = rpcsvc_send_cached_reply(req, reply); + drc->cache_hits++; + UNLOCK(&drc->lock); + goto out; + + } /* retransmitted request, original op in transit, drop it */ + else if (reply && reply->state == DRC_OP_IN_TRANSIT) { + gf_log(GF_RPCSVC, GF_LOG_INFO, + "op in transit," + " discarding. XID: 0x%x", + req->xid); + ret = 0; + drc->intransit_hits++; + rpcsvc_request_destroy(req); + UNLOCK(&drc->lock); + goto out; + + } /* fresh request, cache it as in-transit and proceed */ + else { + ret = rpcsvc_cache_request(req); + } + } + UNLOCK(&drc->lock); + } + + if (req->rpc_err == SUCCESS) { + /* Before going to xlator code, set the THIS properly */ + THIS = svc->xl; + + actor_fn = actor->actor; + + if (!actor_fn) { + rpcsvc_request_seterr(req, PROC_UNAVAIL); + /* LOG TODO: print more info about procnum, + prognum etc, also print transport info */ + gf_log(GF_RPCSVC, GF_LOG_ERROR, "No vectored handler present"); + ret = RPCSVC_ACTOR_ERROR; + goto err_reply; + } + + if (req->synctask) { + ret = synctask_new(THIS->ctx->env, (synctask_fn_t)actor_fn, + rpcsvc_check_and_reply_error, NULL, req); + } else if (req->ownthread) { + value = pthread_getspecific(req->prog->req_queue_key); + if (value == NULL) { + pthread_mutex_lock(&req->prog->thr_lock); + { + num = rpcsvc_get_free_queue_index(req->prog); + if (num != -1) { + num++; + value = (void *)num; + ret = pthread_setspecific(req->prog->req_queue_key, + value); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, + "setting request queue in TLS failed"); + rpcsvc_toggle_queue_status( + req->prog, &req->prog->request_queue[num - 1], + req->prog->request_queue_status); + num = -1; + } else { + spawn_request_handler = 1; + } + } + } + pthread_mutex_unlock(&req->prog->thr_lock); + } + + if (num == -1) + goto noqueue; + + num = ((unsigned long)value) - 1; + + queue = &req->prog->request_queue[num]; + + if (spawn_request_handler) { + ret = gf_thread_create(&queue->thread, NULL, + rpcsvc_request_handler, queue, + "rpcrqhnd"); + if (!ret) { + gf_log(GF_RPCSVC, GF_LOG_INFO, + "spawned a request handler thread for queue %d", + (int)num); + + req->prog->threadcount++; + } else { + gf_log( + GF_RPCSVC, GF_LOG_INFO, + "spawning a request handler thread for queue %d failed", + (int)num); + ret = pthread_setspecific(req->prog->req_queue_key, 0); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, + "resetting request queue in TLS failed"); + } + + rpcsvc_toggle_queue_status( + req->prog, &req->prog->request_queue[num - 1], + req->prog->request_queue_status); + + goto noqueue; + } + } + + pthread_mutex_lock(&queue->queue_lock); + { + empty = list_empty(&queue->request_queue); + + list_add_tail(&req->request_list, &queue->request_queue); + + if (empty && queue->waiting) + pthread_cond_signal(&queue->queue_cond); + } + pthread_mutex_unlock(&queue->queue_lock); + + ret = 0; + } else { + noqueue: + ret = actor_fn(req); + } + } + +err_reply: + + ret = rpcsvc_check_and_reply_error(ret, NULL, req); + /* No need to propagate error beyond this function since the reply + * has now been queued. */ + ret = 0; + +out: + return ret; +} + +int +rpcsvc_handle_disconnect(rpcsvc_t *svc, rpc_transport_t *trans) +{ + rpcsvc_event_t event; + rpcsvc_notify_wrapper_t *wrappers = NULL, *wrapper; + int32_t ret = -1, i = 0, wrapper_count = 0; + rpcsvc_listener_t *listener = NULL; + + event = (trans->listener == NULL) ? RPCSVC_EVENT_LISTENER_DEAD + : RPCSVC_EVENT_DISCONNECT; + + pthread_rwlock_rdlock(&svc->rpclock); + { + if (!svc->notify_count) + goto unlock; + + wrappers = GF_CALLOC(svc->notify_count, sizeof(*wrapper), + gf_common_mt_rpcsvc_wrapper_t); + if (!wrappers) { + goto unlock; + } + + list_for_each_entry(wrapper, &svc->notify, list) + { + if (wrapper->notify) { + wrappers[i++] = *wrapper; + } + } + + wrapper_count = i; + } +unlock: + pthread_rwlock_unlock(&svc->rpclock); + + if (wrappers) { + for (i = 0; i < wrapper_count; i++) { + wrappers[i].notify(svc, wrappers[i].data, event, trans); + } + + GF_FREE(wrappers); + } + + if (event == RPCSVC_EVENT_LISTENER_DEAD) { + listener = rpcsvc_get_listener(svc, -1, trans->listener); + rpcsvc_listener_destroy(listener); + } + + return ret; +} + +int +rpcsvc_notify(rpc_transport_t *trans, void *mydata, rpc_transport_event_t event, + void *data, ...) +{ + int ret = -1; + rpc_transport_pollin_t *msg = NULL; + rpc_transport_t *new_trans = NULL; + rpcsvc_t *svc = NULL; + rpcsvc_listener_t *listener = NULL; + + svc = mydata; + if (svc == NULL) { + goto out; + } + + switch (event) { + case RPC_TRANSPORT_ACCEPT: + new_trans = data; + ret = rpcsvc_accept(svc, trans, new_trans); + break; + + case RPC_TRANSPORT_DISCONNECT: + ret = rpcsvc_handle_disconnect(svc, trans); + break; + + case RPC_TRANSPORT_MSG_RECEIVED: + msg = data; + ret = rpcsvc_handle_rpc_call(svc, trans, msg); + break; + + case RPC_TRANSPORT_MSG_SENT: + ret = 0; + break; + + case RPC_TRANSPORT_CONNECT: + /* do nothing, no need for rpcsvc to handle this, client should + * handle this event + */ + /* print info about transport too : LOG TODO */ + gf_log("rpcsvc", GF_LOG_CRITICAL, + "got CONNECT event, which should have not come"); + ret = 0; + break; + + case RPC_TRANSPORT_CLEANUP: + listener = rpcsvc_get_listener(svc, -1, trans->listener); + if (listener == NULL) { + goto out; + } + + rpcsvc_program_notify(listener, RPCSVC_EVENT_TRANSPORT_DESTROY, + trans); + ret = 0; + break; + + case RPC_TRANSPORT_MAP_XID_REQUEST: + /* FIXME: think about this later */ + gf_log("rpcsvc", GF_LOG_CRITICAL, + "got MAP_XID event, which should have not come"); + ret = 0; + break; + + case RPC_TRANSPORT_EVENT_THREAD_DIED: + rpcsvc_handle_event_thread_death(svc, trans, + (int)(unsigned long)data); + ret = 0; + break; + } + +out: + return ret; +} + +/* Given the RPC reply structure and the payload handed by the RPC program, + * encode the RPC record header into the buffer pointed by recordstart. + */ +struct iovec +rpcsvc_record_build_header(char *recordstart, size_t rlen, struct rpc_msg reply, + size_t payload) +{ + struct iovec replyhdr; + struct iovec txrecord = {0, 0}; + size_t fraglen = 0; + int ret = -1; + + /* After leaving aside the 4 bytes for the fragment header, lets + * encode the RPC reply structure into the buffer given to us. + */ + ret = rpc_reply_to_xdr(&reply, recordstart, rlen, &replyhdr); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "Failed to create RPC reply"); + goto err; + } + + fraglen = payload + replyhdr.iov_len; + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "Reply fraglen %zu, payload: %zu, " + "rpc hdr: %zu", + fraglen, payload, replyhdr.iov_len); + + txrecord.iov_base = recordstart; + + /* Remember, this is only the vec for the RPC header and does not + * include the payload above. We needed the payload only to calculate + * the size of the full fragment. This size is sent in the fragment + * header. + */ + txrecord.iov_len = replyhdr.iov_len; +err: + return txrecord; +} + +static uint32_t +rpc_callback_new_callid(struct rpc_transport *trans) +{ + uint32_t callid = 0; + + pthread_mutex_lock(&trans->lock); + { + callid = ++trans->xid; + } + pthread_mutex_unlock(&trans->lock); + + return callid; +} + +int +rpcsvc_fill_callback(int prognum, int progver, int procnum, int payload, + uint32_t xid, struct rpc_msg *request) +{ + int ret = -1; + + if (!request) { + goto out; + } + + memset(request, 0, sizeof(*request)); + + request->rm_xid = xid; + request->rm_direction = CALL; + + request->rm_call.cb_rpcvers = 2; + request->rm_call.cb_prog = prognum; + request->rm_call.cb_vers = progver; + request->rm_call.cb_proc = procnum; + + request->rm_call.cb_cred.oa_flavor = AUTH_NONE; + request->rm_call.cb_cred.oa_base = NULL; + request->rm_call.cb_cred.oa_length = 0; + + request->rm_call.cb_verf.oa_flavor = AUTH_NONE; + request->rm_call.cb_verf.oa_base = NULL; + request->rm_call.cb_verf.oa_length = 0; + + ret = 0; +out: + return ret; +} + +struct iovec +rpcsvc_callback_build_header(char *recordstart, size_t rlen, + struct rpc_msg *request, size_t payload) +{ + struct iovec requesthdr = { + 0, + }; + struct iovec txrecord = {0, 0}; + int ret = -1; + size_t fraglen = 0; + + ret = rpc_request_to_xdr(request, recordstart, rlen, &requesthdr); + if (ret == -1) { + gf_log("rpcsvc", GF_LOG_WARNING, "Failed to create RPC request"); + goto out; + } + + fraglen = payload + requesthdr.iov_len; + gf_log("rpcsvc", GF_LOG_TRACE, + "Request fraglen %zu, payload: %zu, " + "rpc hdr: %zu", + fraglen, payload, requesthdr.iov_len); + + txrecord.iov_base = recordstart; + + /* Remember, this is only the vec for the RPC header and does not + * include the payload above. We needed the payload only to calculate + * the size of the full fragment. This size is sent in the fragment + * header. + */ + txrecord.iov_len = requesthdr.iov_len; + +out: + return txrecord; +} + +static struct iobuf * +rpcsvc_callback_build_record(rpcsvc_t *rpc, int prognum, int progver, + int procnum, size_t payload, u_long xid, + struct iovec *recbuf) +{ + struct rpc_msg request = { + 0, + }; + struct iobuf *request_iob = NULL; + char *record = NULL; + struct iovec recordhdr = { + 0, + }; + size_t pagesize = 0; + size_t xdr_size = 0; + int ret = -1; + + if ((!rpc) || (!recbuf)) { + goto out; + } + + /* Fill the rpc structure and XDR it into the buffer got above. */ + ret = rpcsvc_fill_callback(prognum, progver, procnum, payload, xid, + &request); + if (ret == -1) { + gf_log("rpcsvc", GF_LOG_WARNING, + "cannot build a rpc-request " + "xid (%lu)", + xid); + goto out; + } + + /* First, try to get a pointer into the buffer which the RPC + * layer can use. + */ + xdr_size = xdr_sizeof((xdrproc_t)xdr_callmsg, &request); + + request_iob = iobuf_get2(rpc->ctx->iobuf_pool, (xdr_size + payload)); + if (!request_iob) { + goto out; + } + + pagesize = iobuf_pagesize(request_iob); + + record = iobuf_ptr(request_iob); /* Now we have it. */ + + recordhdr = rpcsvc_callback_build_header(record, pagesize, &request, + payload); + + if (!recordhdr.iov_base) { + gf_log("rpc-clnt", GF_LOG_ERROR, + "Failed to build record " + " header"); + iobuf_unref(request_iob); + request_iob = NULL; + recbuf->iov_base = NULL; + goto out; + } + + recbuf->iov_base = recordhdr.iov_base; + recbuf->iov_len = recordhdr.iov_len; + +out: + return request_iob; +} + +int +rpcsvc_request_submit(rpcsvc_t *rpc, rpc_transport_t *trans, + rpcsvc_cbk_program_t *prog, int procnum, void *req, + glusterfs_ctx_t *ctx, xdrproc_t xdrproc) +{ + int ret = -1; + int count = 0; + struct iovec iov = { + 0, + }; + struct iobuf *iobuf = NULL; + ssize_t xdr_size = 0; + struct iobref *iobref = NULL; + + if (!req) + goto out; + + xdr_size = xdr_sizeof(xdrproc, req); + + iobuf = iobuf_get2(ctx->iobuf_pool, xdr_size); + if (!iobuf) + goto out; + + iov.iov_base = iobuf->ptr; + iov.iov_len = iobuf_pagesize(iobuf); + + ret = xdr_serialize_generic(iov, req, xdrproc); + if (ret == -1) { + gf_log(THIS->name, GF_LOG_WARNING, "failed to create XDR payload"); + goto out; + } + iov.iov_len = ret; + count = 1; + + iobref = iobref_new(); + if (!iobref) { + ret = -1; + gf_log("rpcsvc", GF_LOG_WARNING, "Failed to create iobref"); + goto out; + } + + iobref_add(iobref, iobuf); + + ret = rpcsvc_callback_submit(rpc, trans, prog, procnum, &iov, count, + iobref); + +out: + if (iobuf) + iobuf_unref(iobuf); + + if (iobref) + iobref_unref(iobref); + + return ret; +} + +int +rpcsvc_callback_submit(rpcsvc_t *rpc, rpc_transport_t *trans, + rpcsvc_cbk_program_t *prog, int procnum, + struct iovec *proghdr, int proghdrcount, + struct iobref *iobref) +{ + struct iobuf *request_iob = NULL; + struct iovec rpchdr = { + 0, + }; + rpc_transport_req_t req; + int ret = -1; + int proglen = 0; + uint32_t xid = 0; + gf_boolean_t new_iobref = _gf_false; + + if (!rpc) { + goto out; + } + + memset(&req, 0, sizeof(req)); + + if (proghdr) { + proglen += iov_length(proghdr, proghdrcount); + } + + xid = rpc_callback_new_callid(trans); + + request_iob = rpcsvc_callback_build_record( + rpc, prog->prognum, prog->progver, procnum, proglen, xid, &rpchdr); + if (!request_iob) { + gf_log("rpcsvc", GF_LOG_WARNING, "cannot build rpc-record"); + goto out; + } + if (!iobref) { + iobref = iobref_new(); + if (!iobref) { + gf_log("rpcsvc", GF_LOG_WARNING, "Failed to create iobref"); + goto out; + } + new_iobref = 1; + } + + iobref_add(iobref, request_iob); + + req.msg.rpchdr = &rpchdr; + req.msg.rpchdrcount = 1; + req.msg.proghdr = proghdr; + req.msg.proghdrcount = proghdrcount; + req.msg.iobref = iobref; + + ret = rpc_transport_submit_request(trans, &req); + if (ret == -1) { + gf_log("rpcsvc", GF_LOG_WARNING, "transmission of rpc-request failed"); + goto out; + } + + ret = 0; + +out: + iobuf_unref(request_iob); + + if (new_iobref) + iobref_unref(iobref); + + return ret; +} + +int +rpcsvc_transport_submit(rpc_transport_t *trans, struct iovec *rpchdr, + int rpchdrcount, struct iovec *proghdr, + int proghdrcount, struct iovec *progpayload, + int progpayloadcount, struct iobref *iobref, void *priv) +{ + int ret = -1; + rpc_transport_reply_t reply = { + 0, + }; + + if ((!trans) || (!rpchdr) || (!rpchdr->iov_base)) { + goto out; + } + + reply.msg.rpchdr = rpchdr; + reply.msg.rpchdrcount = rpchdrcount; + reply.msg.proghdr = proghdr; + reply.msg.proghdrcount = proghdrcount; + reply.msg.progpayload = progpayload; + reply.msg.progpayloadcount = progpayloadcount; + reply.msg.iobref = iobref; + reply.private = priv; + + ret = rpc_transport_submit_reply(trans, &reply); + +out: + return ret; +} + +int +rpcsvc_fill_reply(rpcsvc_request_t *req, struct rpc_msg *reply) +{ + int ret = -1; + rpcsvc_program_t *prog = NULL; + if ((!req) || (!reply)) + goto out; + + ret = 0; + rpc_fill_empty_reply(reply, req->xid); + if (req->rpc_status == MSG_DENIED) { + rpc_fill_denied_reply(reply, req->rpc_err, req->auth_err); + goto out; + } + + prog = rpcsvc_request_program(req); + + if (req->rpc_status == MSG_ACCEPTED) + rpc_fill_accepted_reply( + reply, req->rpc_err, (prog) ? prog->proglowvers : 0, + (prog) ? prog->proghighvers : 0, req->verf.flavour, + req->verf.datalen, req->verf.authdata); + else + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Invalid rpc_status value"); + +out: + return ret; +} + +/* Given a request and the reply payload, build a reply and encodes the reply + * into a record header. This record header is encoded into the vector pointed + * to be recbuf. + * msgvec is the buffer that points to the payload of the RPC program. + * This buffer can be NULL, if an RPC error reply is being constructed. + * The only reason it is needed here is that in case the buffer is provided, + * we should account for the length of that buffer in the RPC fragment header. + */ +struct iobuf * +rpcsvc_record_build_record(rpcsvc_request_t *req, size_t payload, size_t hdrlen, + struct iovec *recbuf) +{ + struct rpc_msg reply; + struct iobuf *replyiob = NULL; + char *record = NULL; + struct iovec recordhdr = { + 0, + }; + size_t pagesize = 0; + size_t xdr_size = 0; + rpcsvc_t *svc = NULL; + int ret = -1; + + if ((!req) || (!req->trans) || (!req->svc) || (!recbuf)) + return NULL; + + svc = req->svc; + + /* Fill the rpc structure and XDR it into the buffer got above. */ + ret = rpcsvc_fill_reply(req, &reply); + if (ret) + goto err_exit; + + xdr_size = xdr_sizeof((xdrproc_t)xdr_replymsg, &reply); + + /* Payload would include 'readv' size etc too, where as + that comes as another payload iobuf */ + replyiob = iobuf_get2(svc->ctx->iobuf_pool, (xdr_size + hdrlen)); + if (!replyiob) { + goto err_exit; + } + + pagesize = iobuf_pagesize(replyiob); + + record = iobuf_ptr(replyiob); /* Now we have it. */ + + recordhdr = rpcsvc_record_build_header(record, pagesize, reply, payload); + if (!recordhdr.iov_base) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to build record " + " header"); + iobuf_unref(replyiob); + replyiob = NULL; + recbuf->iov_base = NULL; + goto err_exit; + } + + recbuf->iov_base = recordhdr.iov_base; + recbuf->iov_len = recordhdr.iov_len; +err_exit: + return replyiob; +} + +/* + * The function to submit a program message to the RPC service. + * This message is added to the transmission queue of the + * conn. + * + * Program callers are not expected to use the msgvec->iov_base + * address for anything else. + * Nor are they expected to free it once this function returns. + * Once the transmission of the buffer is completed by the RPC service, + * the memory area as referenced through @msg will be unrefed. + * If a higher layer does not want anything to do with this iobuf + * after this function returns, it should call unref on it. For keeping + * it around till the transmission is actually complete, rpcsvc also refs it. + * * + * If this function returns an error by returning -1, the + * higher layer programs should assume that a disconnection happened + * and should know that the conn memory area as well as the req structure + * has been freed internally. + * + * For now, this function assumes that a submit is always called + * to send a new record. Later, if there is a situation where different + * buffers for the same record come from different sources, then we'll + * need to change this code to account for multiple submit calls adding + * the buffers into a single record. + */ + +int +rpcsvc_submit_generic(rpcsvc_request_t *req, struct iovec *proghdr, + int hdrcount, struct iovec *payload, int payloadcount, + struct iobref *iobref) +{ + int ret = -1, i = 0; + struct iobuf *replyiob = NULL; + struct iovec recordhdr = { + 0, + }; + rpc_transport_t *trans = NULL; + size_t msglen = 0; + size_t hdrlen = 0; + char new_iobref = 0; + rpcsvc_drc_globals_t *drc = NULL; + gf_latency_t *lat = NULL; + + if ((!req) || (!req->trans)) + return -1; + + if (req->prog && req->begin.tv_sec) { + if ((req->procnum >= 0) && (req->procnum < req->prog->numactors)) { + timespec_now(&req->end); + lat = &req->prog->latencies[req->procnum]; + gf_latency_update(lat, &req->begin, &req->end); + } + } + trans = req->trans; + + for (i = 0; i < hdrcount; i++) { + msglen += proghdr[i].iov_len; + } + + for (i = 0; i < payloadcount; i++) { + msglen += payload[i].iov_len; + } + + gf_log(GF_RPCSVC, GF_LOG_TRACE, "Tx message: %zu", msglen); + + /* Build the buffer containing the encoded RPC reply. */ + replyiob = rpcsvc_record_build_record(req, msglen, hdrlen, &recordhdr); + if (!replyiob) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Reply record creation failed"); + goto disconnect_exit; + } + + if (!iobref) { + iobref = iobref_new(); + if (!iobref) { + goto disconnect_exit; + } + + new_iobref = 1; + } + + iobref_add(iobref, replyiob); + + /* cache the request in the duplicate request cache for appropriate ops */ + if ((req->reply) && (rpcsvc_need_drc(req))) { + drc = req->svc->drc; + + LOCK(&drc->lock); + ret = rpcsvc_cache_reply(req, iobref, &recordhdr, 1, proghdr, hdrcount, + payload, payloadcount); + UNLOCK(&drc->lock); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "failed to cache reply"); + } + } + + ret = rpcsvc_transport_submit(trans, &recordhdr, 1, proghdr, hdrcount, + payload, payloadcount, iobref, + req->trans_private); + + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "failed to submit message " + "(XID: 0x%x, Program: %s, ProgVers: %d, Proc: %d) to " + "rpc-transport (%s)", + req->xid, req->prog ? req->prog->progname : "(not matched)", + req->prog ? req->prog->progver : 0, req->procnum, + trans ? trans->name : ""); + } else { + gf_log(GF_RPCSVC, GF_LOG_TRACE, + "submitted reply for rpc-message (XID: 0x%x, " + "Program: %s, ProgVers: %d, Proc: %d) to rpc-transport " + "(%s)", + req->xid, req->prog ? req->prog->progname : "-", + req->prog ? req->prog->progver : 0, req->procnum, + trans ? trans->name : ""); + } + +disconnect_exit: + if (replyiob) { + iobuf_unref(replyiob); + } + + if (new_iobref) { + iobref_unref(iobref); + } + + rpcsvc_request_destroy(req); + + return ret; +} + +int +rpcsvc_error_reply(rpcsvc_request_t *req) +{ + struct iovec dummyvec = { + 0, + }; + + if (!req) + return -1; + + gf_log_callingfn("", GF_LOG_DEBUG, "sending a RPC error reply"); + + /* At this point the req should already have been filled with the + * appropriate RPC error numbers. + */ + return rpcsvc_submit_generic(req, &dummyvec, 0, NULL, 0, NULL); +} + +#ifdef IPV6_DEFAULT +int +rpcsvc_program_register_rpcbind6(rpcsvc_program_t *newprog, uint32_t port) +{ + const int IP_BUF_LEN = 64; + char addr_buf[IP_BUF_LEN]; + + int err = 0; + bool_t success = 0; + struct netconfig *nc; + struct netbuf *nb; + + if (!newprog) { + goto out; + } + + nc = getnetconfigent("tcp6"); + if (!nc) { + err = -1; + goto out; + } + + err = sprintf(addr_buf, "::.%d.%d", port >> 8 & 0xff, port & 0xff); + if (err < 0) { + err = -1; + goto out; + } + + nb = uaddr2taddr(nc, addr_buf); + if (!nb) { + err = -1; + goto out; + } + + /* Force the unregistration of the program first. + * This call may fail if nothing has been registered, + * which is fine. + */ + rpcsvc_program_unregister_rpcbind6(newprog); + + success = rpcb_set(newprog->prognum, newprog->progver, nc, nb); + if (!success) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Could not register the IPv6" + " service with rpcbind"); + } + + err = 0; + +out: + return err; +} + +int +rpcsvc_program_unregister_rpcbind6(rpcsvc_program_t *newprog) +{ + int err = 0; + bool_t success = 0; + struct netconfig *nc; + + if (!newprog) { + goto out; + } + + nc = getnetconfigent("tcp6"); + if (!nc) { + err = -1; + goto out; + } + + success = rpcb_unset(newprog->prognum, newprog->progver, nc); + if (!success) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Could not unregister the IPv6" + " service with rpcbind"); + } + + err = 0; +out: + return err; +} +#endif + +/* Register the program with the local portmapper service. */ +int +rpcsvc_program_register_portmap(rpcsvc_program_t *newprog, uint32_t port) +{ + int ret = -1; /* FAIL */ + + if (!newprog) { + goto out; + } + + /* pmap_set() returns 0 for FAIL and 1 for SUCCESS */ + if (!(pmap_set(newprog->prognum, newprog->progver, IPPROTO_TCP, port))) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Could not register with" + " portmap %d %d %u", + newprog->prognum, newprog->progver, port); + goto out; + } + + ret = 0; /* SUCCESS */ +out: + return ret; +} + +int +rpcsvc_program_unregister_portmap(rpcsvc_program_t *prog) +{ + int ret = -1; + + if (!prog) + goto out; + + if (!(pmap_unset(prog->prognum, prog->progver))) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Could not unregister with" + " portmap"); + goto out; + } + + ret = 0; +out: + return ret; +} + +int +rpcsvc_register_portmap_enabled(rpcsvc_t *svc) +{ + return svc->register_portmap; +} + +int32_t +rpcsvc_get_listener_port(rpcsvc_listener_t *listener) +{ + int32_t listener_port = -1; + + if ((listener == NULL) || (listener->trans == NULL)) { + goto out; + } + + switch (listener->trans->myinfo.sockaddr.ss_family) { + case AF_INET: + listener_port = ((struct sockaddr_in *)&listener->trans->myinfo + .sockaddr) + ->sin_port; + break; + + case AF_INET6: + listener_port = ((struct sockaddr_in6 *)&listener->trans->myinfo + .sockaddr) + ->sin6_port; + break; + + default: + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "invalid address family (%d)", + listener->trans->myinfo.sockaddr.ss_family); + goto out; + } + + listener_port = ntohs(listener_port); + +out: + return listener_port; +} + +rpcsvc_listener_t * +rpcsvc_get_listener(rpcsvc_t *svc, uint16_t port, rpc_transport_t *trans) +{ + rpcsvc_listener_t *listener = NULL; + char found = 0; + rpcsvc_listener_t *next = NULL; + uint32_t listener_port = 0; + + if (!svc) { + goto out; + } + + pthread_rwlock_rdlock(&svc->rpclock); + { + list_for_each_entry_safe(listener, next, &svc->listeners, list) + { + if (trans != NULL) { + if (listener->trans == trans) { + found = 1; + break; + } + + continue; + } + + listener_port = rpcsvc_get_listener_port(listener); + if (listener_port == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "invalid port for listener %s", + listener->trans->name); + continue; + } + + if (listener_port == port) { + found = 1; + break; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + + if (!found) { + listener = NULL; + } + +out: + return listener; +} + +/* The only difference between the generic submit and this one is that the + * generic submit is also used for submitting RPC error replies in where there + * are no payloads so the msgvec and msgbuf can be NULL. + * Since RPC programs should be using this function along with their payloads + * we must perform NULL checks before calling the generic submit. + */ +int +rpcsvc_submit_message(rpcsvc_request_t *req, struct iovec *proghdr, + int hdrcount, struct iovec *payload, int payloadcount, + struct iobref *iobref) +{ + if ((!req) || (!req->trans) || (!proghdr) || (!proghdr->iov_base)) + return -1; + + return rpcsvc_submit_generic(req, proghdr, hdrcount, payload, payloadcount, + iobref); +} + +void +rpcsvc_program_destroy(rpcsvc_program_t *program) +{ + if (program) { + GF_FREE(program->latencies); + GF_FREE(program); + } +} + +int +rpcsvc_program_unregister(rpcsvc_t *svc, rpcsvc_program_t *program) +{ + int ret = -1; + rpcsvc_program_t *prog = NULL; + if (!svc || !program) { + goto out; + } + + pthread_rwlock_rdlock(&svc->rpclock); + { + list_for_each_entry(prog, &svc->programs, program) + { + if ((prog->prognum == program->prognum) && + (prog->progver == program->progver)) { + break; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + + ret = rpcsvc_program_unregister_portmap(program); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "portmap unregistration of" + " program failed"); + goto out; + } +#ifdef IPV6_DEFAULT + ret = rpcsvc_program_unregister_rpcbind6(program); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "rpcbind (ipv6)" + " unregistration of program failed"); + goto out; + } +#endif + + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "Program unregistered: %s, Num: %d," + " Ver: %d, Port: %d", + prog->progname, prog->prognum, prog->progver, prog->progport); + + if (prog->ownthread) { + prog->alive = _gf_false; + ret = 0; + goto out; + } + + pthread_rwlock_wrlock(&svc->rpclock); + { + list_del_init(&prog->program); + } + pthread_rwlock_unlock(&svc->rpclock); + + ret = 0; +out: + rpcsvc_program_destroy(prog); + + if (ret == -1) { + if (program) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Program " + "unregistration failed" + ": %s, Num: %d, Ver: %d, Port: %d", + program->progname, program->prognum, program->progver, + program->progport); + } else { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Program not found"); + } + } + + return ret; +} + +int +rpcsvc_transport_peername(rpc_transport_t *trans, char *hostname, int hostlen) +{ + if (!trans) { + return -1; + } + + return rpc_transport_get_peername(trans, hostname, hostlen); +} + +int +rpcsvc_transport_peeraddr(rpc_transport_t *trans, char *addrstr, int addrlen, + struct sockaddr_storage *sa, socklen_t sasize) +{ + if (!trans) { + return -1; + } + + return rpc_transport_get_peeraddr(trans, addrstr, addrlen, sa, sasize); +} + +rpcsvc_listener_t * +rpcsvc_listener_alloc(rpcsvc_t *svc, rpc_transport_t *trans) +{ + rpcsvc_listener_t *listener = NULL; + + listener = GF_CALLOC(1, sizeof(*listener), gf_common_mt_rpcsvc_listener_t); + if (!listener) { + goto out; + } + + listener->trans = trans; + listener->svc = svc; + + INIT_LIST_HEAD(&listener->list); + + pthread_rwlock_wrlock(&svc->rpclock); + { + list_add_tail(&listener->list, &svc->listeners); + } + pthread_rwlock_unlock(&svc->rpclock); +out: + return listener; +} + +int32_t +rpcsvc_create_listener(rpcsvc_t *svc, dict_t *options, char *name) +{ + rpc_transport_t *trans = NULL; + rpcsvc_listener_t *listener = NULL; + int32_t ret = -1; + + if (!svc || !options) { + goto out; + } + + trans = rpc_transport_load(svc->ctx, options, name); + if (!trans) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, + "cannot create listener, " + "initing the transport failed"); + goto out; + } + + ret = rpc_transport_listen(trans); + if (ret == -EADDRINUSE || ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "listening on transport failed"); + goto out; + } + + ret = rpc_transport_register_notify(trans, rpcsvc_notify, svc); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_WARNING, "registering notify failed"); + goto out; + } + + listener = rpcsvc_listener_alloc(svc, trans); + if (listener == NULL) { + ret = -1; + goto out; + } + + ret = 0; +out: + if (!listener && trans) { + rpc_transport_disconnect(trans, _gf_true); + rpc_transport_cleanup(trans); + } + + return ret; +} + +int32_t +rpcsvc_create_listeners(rpcsvc_t *svc, dict_t *options, char *name) +{ + int32_t ret = -1, count = 0; + data_t *data = NULL; + char *str = NULL, *ptr = NULL, *transport_name = NULL; + char *transport_type = NULL, *saveptr = NULL, *tmp = NULL; + + if ((svc == NULL) || (options == NULL) || (name == NULL)) { + goto out; + } + + data = dict_get(options, "transport-type"); + if (data == NULL) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "option transport-type not set"); + goto out; + } + + transport_type = data_to_str(data); + if (transport_type == NULL) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "option transport-type not set"); + goto out; + } + + /* duplicate transport_type, since following dict_set will free it */ + transport_type = gf_strdup(transport_type); + if (transport_type == NULL) { + goto out; + } + + str = gf_strdup(transport_type); + if (str == NULL) { + goto out; + } + + ptr = strtok_r(str, ",", &saveptr); + + while (ptr != NULL) { + tmp = gf_strdup(ptr); + if (tmp == NULL) { + goto out; + } + + ret = gf_asprintf(&transport_name, "%s.%s", tmp, name); + if (ret == -1) { + goto out; + } + + ret = dict_set_dynstr(options, "transport-type", tmp); + if (ret == -1) { + goto out; + } + + tmp = NULL; + ptr = strtok_r(NULL, ",", &saveptr); + + ret = rpcsvc_create_listener(svc, options, transport_name); + if (ret != 0) { + goto out; + } + + dict_del(options, "notify-poller-death"); + GF_FREE(transport_name); + transport_name = NULL; + count++; + } + + ret = dict_set_dynstr(options, "transport-type", transport_type); + if (ret == -1) { + goto out; + } + + transport_type = NULL; + +out: + GF_FREE(str); + + GF_FREE(transport_type); + + GF_FREE(tmp); + + GF_FREE(transport_name); + + if (count > 0) { + return count; + } else { + return ret; + } +} + +int +rpcsvc_unregister_notify(rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata) +{ + rpcsvc_notify_wrapper_t *wrapper = NULL, *tmp = NULL; + int ret = 0; + + if (!svc || !notify) { + goto out; + } + + pthread_rwlock_wrlock(&svc->rpclock); + { + list_for_each_entry_safe(wrapper, tmp, &svc->notify, list) + { + if ((wrapper->notify == notify) && (mydata == wrapper->data)) { + list_del_init(&wrapper->list); + GF_FREE(wrapper); + ret++; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + +out: + return ret; +} + +int +rpcsvc_register_notify(rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata) +{ + rpcsvc_notify_wrapper_t *wrapper = NULL; + int ret = -1; + + wrapper = rpcsvc_notify_wrapper_alloc(); + if (!wrapper) { + goto out; + } + svc->mydata = mydata; + wrapper->data = mydata; + wrapper->notify = notify; + + pthread_rwlock_wrlock(&svc->rpclock); + { + list_add_tail(&wrapper->list, &svc->notify); + svc->notify_count++; + } + pthread_rwlock_unlock(&svc->rpclock); + + ret = 0; +out: + return ret; +} + +void * +rpcsvc_request_handler(void *arg) +{ + rpcsvc_request_queue_t *queue = NULL; + rpcsvc_program_t *program = NULL; + rpcsvc_request_t *req = NULL, *tmp_req = NULL; + rpcsvc_actor_t *actor = NULL; + gf_boolean_t done = _gf_false; + int ret = 0; + struct list_head tmp_list; + + queue = arg; + program = queue->program; + + INIT_LIST_HEAD(&tmp_list); + + if (!program) + return NULL; + + while (1) { + pthread_mutex_lock(&queue->queue_lock); + { + if (!program->alive && list_empty(&queue->request_queue)) { + done = 1; + goto unlock; + } + + while (list_empty(&queue->request_queue)) { + queue->waiting = _gf_true; + pthread_cond_wait(&queue->queue_cond, &queue->queue_lock); + } + + queue->waiting = _gf_false; + + if (!list_empty(&queue->request_queue)) { + INIT_LIST_HEAD(&tmp_list); + list_splice_init(&queue->request_queue, &tmp_list); + } + } + unlock: + pthread_mutex_unlock(&queue->queue_lock); + + list_for_each_entry_safe(req, tmp_req, &tmp_list, request_list) + { + if (req) { + list_del_init(&req->request_list); + + if (req->prognum == RPCSVC_INFRA_PROGRAM) { + switch (req->procnum) { + case RPCSVC_PROC_EVENT_THREAD_DEATH: + gf_log(GF_RPCSVC, GF_LOG_INFO, + "event thread died, exiting request handler " + "thread for queue %d of program %s", + (int)(queue - &program->request_queue[0]), + program->progname); + done = 1; + pthread_mutex_lock(&program->thr_lock); + { + rpcsvc_toggle_queue_status( + program, queue, + program->request_queue_status); + program->threadcount--; + } + pthread_mutex_unlock(&program->thr_lock); + rpcsvc_request_destroy(req); + break; + + default: + break; + } + } else { + THIS = req->svc->xl; + actor = rpcsvc_program_actor(req); + ret = actor->actor(req); + + if (ret != 0) { + rpcsvc_check_and_reply_error(ret, NULL, req); + } + req = NULL; + } + } + } + + if (done) + break; + } + + return NULL; +} + +int +rpcsvc_program_register(rpcsvc_t *svc, rpcsvc_program_t *program, + gf_boolean_t add_to_head) +{ + int ret = -1, i = 0; + rpcsvc_program_t *newprog = NULL; + char already_registered = 0; + pthread_mutexattr_t attr[EVENT_MAX_THREADS]; + pthread_mutexattr_t thr_attr; + + if (!svc) { + goto out; + } + + if (program->actors == NULL) { + goto out; + } + + pthread_rwlock_rdlock(&svc->rpclock); + { + list_for_each_entry(newprog, &svc->programs, program) + { + if ((newprog->prognum == program->prognum) && + (newprog->progver == program->progver)) { + already_registered = 1; + break; + } + } + } + pthread_rwlock_unlock(&svc->rpclock); + + if (already_registered) { + ret = 0; + goto out; + } + + newprog = GF_CALLOC(1, sizeof(*newprog), gf_common_mt_rpcsvc_program_t); + if (newprog == NULL) { + goto out; + } + + memcpy(newprog, program, sizeof(*program)); + newprog->latencies = gf_latency_new(program->numactors); + if (!newprog->latencies) { + rpcsvc_program_destroy(newprog); + goto out; + } + + INIT_LIST_HEAD(&newprog->program); + pthread_mutexattr_init(&thr_attr); + pthread_mutexattr_settype(&thr_attr, PTHREAD_MUTEX_ADAPTIVE_NP); + + for (i = 0; i < EVENT_MAX_THREADS; i++) { + pthread_mutexattr_init(&attr[i]); + pthread_mutexattr_settype(&attr[i], PTHREAD_MUTEX_ADAPTIVE_NP); + INIT_LIST_HEAD(&newprog->request_queue[i].request_queue); + pthread_mutex_init(&newprog->request_queue[i].queue_lock, &attr[i]); + pthread_cond_init(&newprog->request_queue[i].queue_cond, NULL); + newprog->request_queue[i].program = newprog; + } + + pthread_mutex_init(&newprog->thr_lock, &thr_attr); + pthread_cond_init(&newprog->thr_cond, NULL); + + newprog->alive = _gf_true; + + if (gf_async_ctrl.enabled) { + newprog->ownthread = _gf_false; + newprog->synctask = _gf_false; + } + + /* make sure synctask gets priority over ownthread */ + if (newprog->synctask) + newprog->ownthread = _gf_false; + + if (newprog->ownthread) { + struct event_pool *ep = svc->ctx->event_pool; + newprog->eventthreadcount = ep->eventthreadcount; + + pthread_key_create(&newprog->req_queue_key, NULL); + newprog->thr_queue = 1; + } + + pthread_rwlock_wrlock(&svc->rpclock); + { + if (add_to_head) + list_add(&newprog->program, &svc->programs); + else + list_add_tail(&newprog->program, &svc->programs); + } + pthread_rwlock_unlock(&svc->rpclock); + + ret = 0; + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "New program registered: %s, Num: %d," + " Ver: %d, Port: %d", + newprog->progname, newprog->prognum, newprog->progver, + newprog->progport); + +out: + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Program registration failed:" + " %s, Num: %d, Ver: %d, Port: %d", + program->progname, program->prognum, program->progver, + program->progport); + } + + return ret; +} + +static void +free_prog_details(gf_dump_rsp *rsp) +{ + gf_prog_detail *prev = NULL; + gf_prog_detail *trav = NULL; + + trav = rsp->prog; + while (trav) { + prev = trav; + trav = trav->next; + GF_FREE(prev); + } +} + +static int +build_prog_details(rpcsvc_request_t *req, gf_dump_rsp *rsp) +{ + int ret = -1; + rpcsvc_program_t *program = NULL; + gf_prog_detail *prog = NULL; + gf_prog_detail *prev = NULL; + + if (!req || !req->trans || !req->svc) + goto out; + + pthread_rwlock_rdlock(&req->svc->rpclock); + { + list_for_each_entry(program, &req->svc->programs, program) + { + prog = GF_CALLOC(1, sizeof(*prog), 0); + if (!prog) + goto unlock; + + prog->progname = program->progname; + prog->prognum = program->prognum; + prog->progver = program->progver; + + if (!rsp->prog) + rsp->prog = prog; + if (prev) + prev->next = prog; + prev = prog; + } + if (prev) + ret = 0; + } +unlock: + pthread_rwlock_unlock(&req->svc->rpclock); +out: + return ret; +} + +static int +rpcsvc_ping(rpcsvc_request_t *req) +{ + char rsp_buf[8 * 1024] = { + 0, + }; + gf_common_rsp rsp = { + 0, + }; + struct iovec iov = { + 0, + }; + int ret = -1; + uint32_t ping_rsp_len = 0; + + ping_rsp_len = xdr_sizeof((xdrproc_t)xdr_gf_common_rsp, &rsp); + + iov.iov_base = rsp_buf; + iov.iov_len = ping_rsp_len; + + ret = xdr_serialize_generic(iov, &rsp, (xdrproc_t)xdr_gf_common_rsp); + if (ret < 0) { + ret = RPCSVC_ACTOR_ERROR; + } else { + rsp.op_ret = 0; + rpcsvc_submit_generic(req, &iov, 1, NULL, 0, NULL); + } + + return 0; +} + +static int +rpcsvc_dump(rpcsvc_request_t *req) +{ + char rsp_buf[8 * 1024] = { + 0, + }; + gf_dump_rsp rsp = { + 0, + }; + struct iovec iov = { + 0, + }; + int op_errno = EINVAL; + int ret = -1; + uint32_t dump_rsp_len = 0; + + if (!req) + goto sendrsp; + + ret = build_prog_details(req, &rsp); + if (ret < 0) { + op_errno = -ret; + goto sendrsp; + } + + op_errno = 0; + +sendrsp: + rsp.op_errno = gf_errno_to_error(op_errno); + rsp.op_ret = ret; + + dump_rsp_len = xdr_sizeof((xdrproc_t)xdr_gf_dump_rsp, &rsp); + + iov.iov_base = rsp_buf; + iov.iov_len = dump_rsp_len; + + ret = xdr_serialize_generic(iov, &rsp, (xdrproc_t)xdr_gf_dump_rsp); + if (ret < 0) { + ret = RPCSVC_ACTOR_ERROR; + } else { + rpcsvc_submit_generic(req, &iov, 1, NULL, 0, NULL); + ret = 0; + } + + free_prog_details(&rsp); + + return ret; +} + +int +rpcsvc_init_options(rpcsvc_t *svc, dict_t *options) +{ + char *optstr = NULL; + int ret = -1; + + if ((!svc) || (!options)) + return -1; + + svc->memfactor = RPCSVC_DEFAULT_MEMFACTOR; + + svc->register_portmap = _gf_true; + if (dict_get(options, "rpc.register-with-portmap")) { + ret = dict_get_str(options, "rpc.register-with-portmap", &optstr); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to parse " + "dict"); + goto out; + } + + ret = gf_string2boolean(optstr, &svc->register_portmap); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to parse bool " + "string"); + goto out; + } + } + + if (!svc->register_portmap) + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "Portmap registration " + "disabled"); + ret = 0; +out: + return ret; +} + +int +rpcsvc_reconfigure_options(rpcsvc_t *svc, dict_t *options) +{ + xlator_t *xlator = NULL; + xlator_list_t *volentry = NULL; + char *srchkey = NULL; + char *keyval = NULL; + int ret = -1; + + if ((!svc) || (!svc->options) || (!options)) + return (-1); + + /* Fetch the xlator from svc */ + xlator = svc->xl; + if (!xlator) + return (-1); + + /* Reconfigure the volume specific rpc-auth.addr allow part */ + volentry = xlator->children; + while (volentry) { + ret = gf_asprintf(&srchkey, "rpc-auth.addr.%s.allow", + volentry->xlator->name); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + return (-1); + } + + /* key-string: rpc-auth.addr.<volname>.allow + * + * IMP: Delete the OLD key/value pair from dict. + * And set the NEW key/value pair IFF the option is SET + * in reconfigured volfile. + * + * NB: If rpc-auth.addr.<volname>.allow is not SET explicitly, + * build_nfs_graph() sets it as "*" i.e. anonymous. + */ + dict_del(svc->options, srchkey); + if (!dict_get_str(options, srchkey, &keyval)) { + ret = dict_set_dynstr_with_alloc(svc->options, srchkey, keyval); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "dict_set_str error"); + GF_FREE(srchkey); + return (-1); + } + } + + GF_FREE(srchkey); + volentry = volentry->next; + } + + /* Reconfigure the volume specific rpc-auth.addr reject part */ + volentry = xlator->children; + while (volentry) { + ret = gf_asprintf(&srchkey, "rpc-auth.addr.%s.reject", + volentry->xlator->name); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + return (-1); + } + + /* key-string: rpc-auth.addr.<volname>.reject + * + * IMP: Delete the OLD key/value pair from dict. + * And set the NEW key/value pair IFF the option is SET + * in reconfigured volfile. + * + * NB: No default value for reject key. + */ + dict_del(svc->options, srchkey); + if (!dict_get_str(options, srchkey, &keyval)) { + ret = dict_set_dynstr_with_alloc(svc->options, srchkey, keyval); + if (ret < 0) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "dict_set_str error"); + GF_FREE(srchkey); + return (-1); + } + } + + GF_FREE(srchkey); + volentry = volentry->next; + } + + ret = rpcsvc_init_options(svc, options); + if (ret) + return (-1); + + return rpcsvc_auth_reconf(svc, options); +} + +int +rpcsvc_transport_unix_options_build(dict_t *dict, char *filepath) +{ + char *fpath = NULL; + int ret = -1; + + GF_ASSERT(filepath); + GF_VALIDATE_OR_GOTO("rpcsvc", dict, out); + + fpath = gf_strdup(filepath); + if (!fpath) { + ret = -1; + goto out; + } + + ret = dict_set_dynstr(dict, "transport.socket.listen-path", fpath); + if (ret) + goto out; + + ret = dict_set_str(dict, "transport.address-family", "unix"); + if (ret) + goto out; + + ret = dict_set_str(dict, "transport.socket.nodelay", "off"); + if (ret) + goto out; + + ret = dict_set_str(dict, "transport-type", "socket"); + if (ret) + goto out; +out: + if (ret) { + GF_FREE(fpath); + } + return ret; +} + +/* + * Configure() the rpc.outstanding-rpc-limit param. + * If dict_get_int32() for dict-key "rpc.outstanding-rpc-limit" FAILS, + * it would set the value as "defvalue". Otherwise it would fetch the + * value and round up to multiple-of-8. defvalue must be +ve. + * + * NB: defval or set-value "0" is special which means unlimited/65536. + */ +int +rpcsvc_set_outstanding_rpc_limit(rpcsvc_t *svc, dict_t *options, int defvalue) +{ + int ret = -1; /* FAILURE */ + int rpclim = 0; + static char *rpclimkey = "rpc.outstanding-rpc-limit"; + + if ((!svc) || (!options)) + return (-1); + + if ((defvalue < RPCSVC_MIN_OUTSTANDING_RPC_LIMIT) || + (defvalue > RPCSVC_MAX_OUTSTANDING_RPC_LIMIT)) { + return (-1); + } + + /* Fetch the rpc.outstanding-rpc-limit from dict. */ + ret = dict_get_int32(options, rpclimkey, &rpclim); + if (ret < 0) { + /* Fall back to default for FAILURE */ + rpclim = defvalue; + } + + /* Round up to multiple-of-8. It must not exceed + * RPCSVC_MAX_OUTSTANDING_RPC_LIMIT. + */ + rpclim = ((rpclim + 8 - 1) >> 3) * 8; + if (rpclim > RPCSVC_MAX_OUTSTANDING_RPC_LIMIT) { + rpclim = RPCSVC_MAX_OUTSTANDING_RPC_LIMIT; + } + + if (svc->outstanding_rpc_limit != rpclim) { + svc->outstanding_rpc_limit = rpclim; + gf_log(GF_RPCSVC, GF_LOG_INFO, "Configured %s with value %d", rpclimkey, + rpclim); + } + + return (0); +} + +/* + * Enable throttling for rpcsvc_t svc. + * Returns 0 on success, -1 otherwise. + */ +int +rpcsvc_set_throttle_on(rpcsvc_t *svc) +{ + if (!svc) + return -1; + + svc->throttle = _gf_true; + + return 0; +} + +/* + * Disable throttling for rpcsvc_t svc. + * Returns 0 on success, -1 otherwise. + */ +int +rpcsvc_set_throttle_off(rpcsvc_t *svc) +{ + if (!svc) + return -1; + + svc->throttle = _gf_false; + + return 0; +} + +/* + * Get throttle state for rpcsvc_t svc. + * Returns value of attribute throttle on success, _gf_false otherwise. + */ +gf_boolean_t +rpcsvc_get_throttle(rpcsvc_t *svc) +{ + if (!svc) + return _gf_false; + + return svc->throttle; +} + +/* Function call to cleanup resources for svc + */ +int +rpcsvc_destroy(rpcsvc_t *svc) +{ + struct rpcsvc_auth_list *auth = NULL; + struct rpcsvc_auth_list *tmp = NULL; + rpcsvc_listener_t *listener = NULL; + rpcsvc_listener_t *next = NULL; + int ret = 0; + + if (!svc) + return ret; + + list_for_each_entry_safe(listener, next, &svc->listeners, list) + { + rpcsvc_listener_destroy(listener); + } + + list_for_each_entry_safe(auth, tmp, &svc->authschemes, authlist) + { + list_del_init(&auth->authlist); + GF_FREE(auth); + } + + rpcsvc_program_unregister(svc, &gluster_dump_prog); + if (svc->rxpool) { + mem_pool_destroy(svc->rxpool); + svc->rxpool = NULL; + } + + pthread_rwlock_destroy(&svc->rpclock); + GF_FREE(svc); + + return ret; +} + +/* The global RPC service initializer. + */ +rpcsvc_t * +rpcsvc_init(xlator_t *xl, glusterfs_ctx_t *ctx, dict_t *options, + uint32_t poolcount) +{ + rpcsvc_t *svc = NULL; + int ret = -1; + + if ((!xl) || (!ctx) || (!options)) + return NULL; + + svc = GF_CALLOC(1, sizeof(*svc), gf_common_mt_rpcsvc_t); + if (!svc) + return NULL; + + pthread_rwlock_init(&svc->rpclock, NULL); + INIT_LIST_HEAD(&svc->authschemes); + INIT_LIST_HEAD(&svc->notify); + INIT_LIST_HEAD(&svc->listeners); + INIT_LIST_HEAD(&svc->programs); + + ret = rpcsvc_init_options(svc, options); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "Failed to init options"); + goto free_svc; + } + + if (!poolcount) + poolcount = RPCSVC_POOLCOUNT_MULT * svc->memfactor; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, "rx pool: %d", poolcount); + svc->rxpool = mem_pool_new(rpcsvc_request_t, poolcount); + /* TODO: leak */ + if (!svc->rxpool) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "mem pool allocation failed"); + goto free_svc; + } + + ret = rpcsvc_auth_init(svc, options); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to init " + "authentication"); + goto free_svc; + } + + ret = -1; + svc->options = options; + svc->ctx = ctx; + svc->xl = xl; + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "RPC service inited."); + + gluster_dump_prog.options = options; + + ret = rpcsvc_program_register(svc, &gluster_dump_prog, _gf_false); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "failed to register DUMP program"); + goto free_svc; + } + + ret = 0; +free_svc: + if (ret == -1) { + GF_FREE(svc); + svc = NULL; + } + + return svc; +} + +int +rpcsvc_transport_peer_check_search(dict_t *options, char *pattern, char *ip, + char *hostname) +{ + int ret = -1; + char *addrtok = NULL; + char *addrstr = NULL; + char *dup_addrstr = NULL; + char *svptr = NULL; + + if ((!options) || (!ip)) + return -1; + + ret = dict_get_str(options, pattern, &addrstr); + if (ret < 0) { + ret = -1; + goto err; + } + + if (!addrstr) { + ret = -1; + goto err; + } + + dup_addrstr = gf_strdup(addrstr); + if (dup_addrstr == NULL) { + ret = -1; + goto err; + } + addrtok = strtok_r(dup_addrstr, ",", &svptr); + while (addrtok) { + /* CASEFOLD not present on Solaris */ +#ifdef FNM_CASEFOLD + ret = fnmatch(addrtok, ip, FNM_CASEFOLD); +#else + ret = fnmatch(addrtok, ip, 0); +#endif + if (ret == 0) + goto err; + + /* compare hostnames if applicable */ + if (hostname) { +#ifdef FNM_CASEFOLD + ret = fnmatch(addrtok, hostname, FNM_CASEFOLD); +#else + ret = fnmatch(addrtok, hostname, 0); +#endif + if (ret == 0) + goto err; + } + + /* Compare IPv4 subnetwork, TODO: IPv6 subnet support */ + if (strchr(addrtok, '/')) { + ret = rpcsvc_match_subnet_v4(addrtok, ip); + if (ret == 0) + goto err; + } + + addrtok = strtok_r(NULL, ",", &svptr); + } + + ret = -1; +err: + GF_FREE(dup_addrstr); + + return ret; +} + +static int +rpcsvc_transport_peer_check_allow(dict_t *options, char *volname, char *ip, + char *hostname) +{ + int ret = RPCSVC_AUTH_DONTCARE; + char *srchstr = NULL; + + if ((!options) || (!ip) || (!volname)) + return ret; + + ret = gf_asprintf(&srchstr, "rpc-auth.addr.%s.allow", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + ret = RPCSVC_AUTH_DONTCARE; + goto out; + } + + ret = rpcsvc_transport_peer_check_search(options, srchstr, ip, hostname); + GF_FREE(srchstr); + + if (ret == 0) + ret = RPCSVC_AUTH_ACCEPT; + else + ret = RPCSVC_AUTH_REJECT; +out: + return ret; +} + +static int +rpcsvc_transport_peer_check_reject(dict_t *options, char *volname, char *ip, + char *hostname) +{ + int ret = RPCSVC_AUTH_DONTCARE; + char *srchstr = NULL; + + if ((!options) || (!ip) || (!volname)) + return ret; + + ret = gf_asprintf(&srchstr, "rpc-auth.addr.%s.reject", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + ret = RPCSVC_AUTH_REJECT; + goto out; + } + + ret = rpcsvc_transport_peer_check_search(options, srchstr, ip, hostname); + GF_FREE(srchstr); + + if (ret == 0) + ret = RPCSVC_AUTH_REJECT; + else + ret = RPCSVC_AUTH_DONTCARE; +out: + return ret; +} + +/* Combines rpc auth's allow and reject options. + * Order of checks is important. + * First, REJECT if either rejects. + * If neither rejects, ACCEPT if either accepts. + * If neither accepts, DONTCARE + */ +int +rpcsvc_combine_allow_reject_volume_check(int allow, int reject) +{ + if (allow == RPCSVC_AUTH_REJECT || reject == RPCSVC_AUTH_REJECT) + return RPCSVC_AUTH_REJECT; + + if (allow == RPCSVC_AUTH_ACCEPT || reject == RPCSVC_AUTH_ACCEPT) + return RPCSVC_AUTH_ACCEPT; + + return RPCSVC_AUTH_DONTCARE; +} + +int +rpcsvc_auth_check(rpcsvc_t *svc, char *volname, char *ipaddr) +{ + int ret = RPCSVC_AUTH_REJECT; + int accept = RPCSVC_AUTH_REJECT; + int reject = RPCSVC_AUTH_REJECT; + char *hostname = NULL; + char *allow_str = NULL; + char *reject_str = NULL; + char *srchstr = NULL; + dict_t *options = NULL; + + if (!svc || !volname || !ipaddr) + return ret; + + /* Fetch the options from svc struct and validate */ + options = svc->options; + if (!options) + return ret; + + /* Accept if its the default case: Allow all, Reject none + * The default volfile always contains a 'allow *' rule + * for each volume. If allow rule is missing (which implies + * there is some bad volfile generating code doing this), we + * assume no one is allowed mounts, and thus, we reject mounts. + */ + ret = gf_asprintf(&srchstr, "rpc-auth.addr.%s.allow", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + return RPCSVC_AUTH_REJECT; + } + + ret = dict_get_str(options, srchstr, &allow_str); + GF_FREE(srchstr); + if (ret < 0) + return RPCSVC_AUTH_REJECT; + + ret = gf_asprintf(&srchstr, "rpc-auth.addr.%s.reject", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + return RPCSVC_AUTH_REJECT; + } + + ret = dict_get_str(options, srchstr, &reject_str); + GF_FREE(srchstr); + + /* + * If "reject_str" is being set as '*' (anonymous), then NFS-server + * would reject everything. If the "reject_str" is not set and + * "allow_str" is set as '*' (anonymous), then NFS-server would + * accept mount requests from all clients. + */ + if (reject_str != NULL) { + if (!strcmp("*", reject_str)) + return RPCSVC_AUTH_REJECT; + } else { + if (!strcmp("*", allow_str)) + return RPCSVC_AUTH_ACCEPT; + } + + /* addr-namelookup check */ + if (svc->addr_namelookup == _gf_true) { + ret = gf_get_hostname_from_ip(ipaddr, &hostname); + if (ret) { + if (hostname) + GF_FREE(hostname); + /* failed to get hostname, but hostname auth + * is enabled, so authentication will not be + * 100% correct. reject mounts + */ + return RPCSVC_AUTH_REJECT; + } + } + + accept = rpcsvc_transport_peer_check_allow(options, volname, ipaddr, + hostname); + + reject = rpcsvc_transport_peer_check_reject(options, volname, ipaddr, + hostname); + + if (hostname) + GF_FREE(hostname); + return rpcsvc_combine_allow_reject_volume_check(accept, reject); +} + +int +rpcsvc_transport_privport_check(rpcsvc_t *svc, char *volname, uint16_t port) +{ + int ret = RPCSVC_AUTH_REJECT; + char *srchstr = NULL; + char *valstr = NULL; + gf_boolean_t insecure = _gf_false; + + if ((!svc) || (!volname)) + return ret; + + gf_log(GF_RPCSVC, GF_LOG_TRACE, "Client port: %d", (int)port); + /* If the port is already a privileged one, don't bother with checking + * options. + */ + if (port <= 1024) { + ret = RPCSVC_AUTH_ACCEPT; + goto err; + } + + /* Disabled by default */ + ret = gf_asprintf(&srchstr, "rpc-auth.ports.%s.insecure", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + ret = RPCSVC_AUTH_REJECT; + goto err; + } + + ret = dict_get_str(svc->options, srchstr, &valstr); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to" + " read rpc-auth.ports.insecure value"); + goto err; + } + + ret = gf_string2boolean(valstr, &insecure); + if (ret) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, + "Failed to" + " convert rpc-auth.ports.insecure value"); + goto err; + } + + ret = insecure ? RPCSVC_AUTH_ACCEPT : RPCSVC_AUTH_REJECT; + + if (ret == RPCSVC_AUTH_ACCEPT) + gf_log(GF_RPCSVC, GF_LOG_DEBUG, "Unprivileged port allowed"); + else + gf_log(GF_RPCSVC, GF_LOG_DEBUG, + "Unprivileged port not" + " allowed"); + +err: + if (srchstr) + GF_FREE(srchstr); + + return ret; +} + +char * +rpcsvc_volume_allowed(dict_t *options, char *volname) +{ + char globalrule[] = "rpc-auth.addr.allow"; + char *srchstr = NULL; + char *addrstr = NULL; + int ret = -1; + + if ((!options) || (!volname)) + return NULL; + + ret = gf_asprintf(&srchstr, "rpc-auth.addr.%s.allow", volname); + if (ret == -1) { + gf_log(GF_RPCSVC, GF_LOG_ERROR, "asprintf failed"); + goto out; + } + + if (!dict_get(options, srchstr)) + ret = dict_get_str(options, globalrule, &addrstr); + else + ret = dict_get_str(options, srchstr, &addrstr); + +out: + GF_FREE(srchstr); + + return addrstr; +} + +/* + * rpcsvc_match_subnet_v4() takes subnetwork address pattern and checks + * if the target IPv4 address has the same network address with the help + * of network mask. + * + * Returns 0 for SUCCESS and -1 otherwise. + * + * NB: Validation of subnetwork address pattern is not required + * as it's already being done at the time of CLI SET. + */ +static int +rpcsvc_match_subnet_v4(const char *addrtok, const char *ipaddr) +{ + char *slash = NULL; + char *netaddr = NULL; + int ret = -1; + uint32_t prefixlen = 0; + uint32_t shift = 0; + struct sockaddr_in sin1 = { + 0, + }; + struct sockaddr_in sin2 = { + 0, + }; + struct sockaddr_in mask = { + 0, + }; + + /* Copy the input */ + netaddr = gf_strdup(addrtok); + if (netaddr == NULL) /* ENOMEM */ + goto out; + + /* Find the network socket addr of target */ + if (inet_pton(AF_INET, ipaddr, &sin1.sin_addr) == 0) + goto out; + + slash = strchr(netaddr, '/'); + if (slash) { + *slash = '\0'; + /* + * Find the IPv4 network mask in network byte order. + * IMP: String slash+1 is already validated, it can't have value + * more than IPv4_ADDR_SIZE (32). + */ + prefixlen = (uint32_t)atoi(slash + 1); + if (prefixlen > 31) + goto out; + } else { + /* if there is no '/', then this function wouldn't be called */ + goto out; + } + + /* Need to do this after removing '/', as inet_pton() take IP address as + * second argument. Once we get sin2, then comparison is oranges to orange + */ + if (inet_pton(AF_INET, netaddr, &sin2.sin_addr) == 0) + goto out; + + shift = IPv4_ADDR_SIZE - prefixlen; + mask.sin_addr.s_addr = htonl((uint32_t)~0 << shift); + + if (mask_match(sin1.sin_addr.s_addr, sin2.sin_addr.s_addr, + mask.sin_addr.s_addr)) { + ret = 0; /* SUCCESS */ + } +out: + GF_FREE(netaddr); + return ret; +} + +void +rpcsvc_program_dump(rpcsvc_program_t *prog) +{ + char key_prefix[GF_DUMP_MAX_BUF_LEN]; + char key[GF_DUMP_MAX_BUF_LEN]; + int i; + + snprintf(key_prefix, GF_DUMP_MAX_BUF_LEN, "%s", prog->progname); + gf_proc_dump_add_section("%s", key_prefix); + + gf_proc_dump_build_key(key, key_prefix, "program-number"); + gf_proc_dump_write(key, "%d", prog->prognum); + + gf_proc_dump_build_key(key, key_prefix, "program-version"); + gf_proc_dump_write(key, "%d", prog->progver); + + strncat(key_prefix, ".latency", + sizeof(key_prefix) - strlen(key_prefix) - 1); + + for (i = 0; i < prog->numactors; i++) { + gf_proc_dump_build_key(key, key_prefix, "%s", prog->actors[i].procname); + gf_latency_statedump_and_reset(key, &prog->latencies[i]); + } +} + +void +rpcsvc_statedump(rpcsvc_t *svc) +{ + rpcsvc_program_t *prog = NULL; + int ret = 0; + ret = pthread_rwlock_tryrdlock(&svc->rpclock); + if (ret) + return; + { + list_for_each_entry(prog, &svc->programs, program) + { + rpcsvc_program_dump(prog); + } + } + pthread_rwlock_unlock(&svc->rpclock); +} + +static rpcsvc_actor_t gluster_dump_actors[GF_DUMP_MAXVALUE] = { + [GF_DUMP_NULL] = {"NULL", NULL, NULL, GF_DUMP_NULL, DRC_NA, 0}, + [GF_DUMP_DUMP] = {"DUMP", rpcsvc_dump, NULL, GF_DUMP_DUMP, DRC_NA, 0}, + [GF_DUMP_PING] = {"PING", rpcsvc_ping, NULL, GF_DUMP_PING, DRC_NA, 0}, +}; + +static struct rpcsvc_program gluster_dump_prog = { + .progname = "GF-DUMP", + .prognum = GLUSTER_DUMP_PROGRAM, + .progver = GLUSTER_DUMP_VERSION, + .actors = gluster_dump_actors, + .numactors = GF_DUMP_MAXVALUE, +}; diff --git a/rpc/rpc-lib/src/rpcsvc.h b/rpc/rpc-lib/src/rpcsvc.h new file mode 100644 index 00000000000..7b3030926c8 --- /dev/null +++ b/rpc/rpc-lib/src/rpcsvc.h @@ -0,0 +1,694 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _RPCSVC_H +#define _RPCSVC_H + +#include <glusterfs/gf-event.h> +#include "rpc-transport.h" +#include <glusterfs/dict.h> +#include "xdr-rpc.h" +#include "rpcsvc-common.h" + +#include <pthread.h> +#include <sys/uio.h> +#include <inttypes.h> +#include <rpc/rpc_msg.h> +#include <glusterfs/compat.h> +#include <glusterfs/client_t.h> + +#ifndef MAX_IOVEC +#define MAX_IOVEC 16 +#endif + +/* TODO: we should store prognums at a centralized location to avoid conflict + or use a robust random number generator to avoid conflicts +*/ + +#define RPCSVC_INFRA_PROGRAM 7712846 /* random number */ + +typedef enum { + RPCSVC_PROC_EVENT_THREAD_DEATH = 0, +} rpcsvc_infra_procnum_t; + +#define RPCSVC_DEFAULT_OUTSTANDING_RPC_LIMIT \ + 64 /* Default for protocol/server */ +#define RPCSVC_DEF_NFS_OUTSTANDING_RPC_LIMIT 16 /* Default for nfs/server */ +#define RPCSVC_MAX_OUTSTANDING_RPC_LIMIT 65536 +#define RPCSVC_MIN_OUTSTANDING_RPC_LIMIT 0 /* No limit i.e. Unlimited */ + +#define GF_RPCSVC "rpc-service" +#define RPCSVC_THREAD_STACK_SIZE ((size_t)(1024 * GF_UNIT_KB)) + +#define RPCSVC_FRAGHDR_SIZE 4 /* 4-byte RPC fragment header size */ +#define RPCSVC_DEFAULT_LISTEN_PORT GF_DEFAULT_BASE_PORT +#define RPCSVC_DEFAULT_MEMFACTOR 8 +#define RPCSVC_EVENTPOOL_SIZE_MULT 1024 +#define RPCSVC_POOLCOUNT_MULT 64 +#define RPCSVC_CONN_READ (128 * GF_UNIT_KB) +#define RPCSVC_PAGE_SIZE (128 * GF_UNIT_KB) +#define RPC_ROOT_UID 0 +#define RPC_ROOT_GID 0 +#define RPC_NOBODY_UID 65534 +#define RPC_NOBODY_GID 65534 + +/* RPC Record States */ +#define RPCSVC_READ_FRAGHDR 1 +#define RPCSVC_READ_FRAG 2 +/* The size in bytes, if crossed by a fragment will be handed over to the + * vectored actor so that it can allocate its buffers the way it wants. + * In our RPC layer, we assume that vectored RPC requests/records are never + * spread over multiple RPC fragments since that prevents us from determining + * whether the record should be handled in RPC layer completely or handed to + * the vectored handler. + */ +#define RPCSVC_VECTORED_FRAGSZ 4096 +#define RPCSVC_VECTOR_READCRED 1003 +#define RPCSVC_VECTOR_READVERFSZ 1004 +#define RPCSVC_VECTOR_READVERF 1005 +#define RPCSVC_VECTOR_IGNORE 1006 +#define RPCSVC_VECTOR_READVEC 1007 +#define RPCSVC_VECTOR_READPROCHDR 1008 + +#define rpcsvc_record_vectored_baremsg(rs) \ + (((rs)->state == RPCSVC_READ_FRAG) && (rs)->vecstate == 0) +#define rpcsvc_record_vectored_cred(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_READCRED) +#define rpcsvc_record_vectored_verfsz(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_READVERFSZ) +#define rpcsvc_record_vectored_verfread(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_READVERF) +#define rpcsvc_record_vectored_ignore(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_IGNORE) +#define rpcsvc_record_vectored_readvec(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_READVEC) +#define rpcsvc_record_vectored_readprochdr(rs) \ + ((rs)->vecstate == RPCSVC_VECTOR_READPROCHDR) +#define rpcsvc_record_vectored(rs) ((rs)->fragsize > RPCSVC_VECTORED_FRAGSZ) +/* Includes bytes up to and including the credential length field. The credlen + * will be followed by @credlen bytes of credential data which will have to be + * read separately by the vectored reader. After the credentials comes the + * verifier which will also have to be read separately including the 8 bytes of + * verf flavour and verflen. + */ +#define RPCSVC_BARERPC_MSGSZ 32 +#define rpcsvc_record_readfraghdr(rs) ((rs)->state == RPCSVC_READ_FRAGHDR) +#define rpcsvc_record_readfrag(rs) ((rs)->state == RPCSVC_READ_FRAG) + +#define RPCSVC_LOWVERS 2 +#define RPCSVC_HIGHVERS 2 + +#if 0 +#error "defined in /usr/include/rpc/auth.h" + +#define AUTH_NONE 0 /* no authentication */ +#define AUTH_NULL 0 /* backward compatibility */ +#define AUTH_SYS 1 /* unix style (uid, gids) */ +#define AUTH_UNIX AUTH_SYS +#define AUTH_SHORT 2 /* short hand unix style */ +#define AUTH_DES 3 /* des style (encrypted timestamps) */ +#define AUTH_DH AUTH_DES /* Diffie-Hellman (this is DES) */ +#define AUTH_KERB 4 /* kerberos style */ +#endif /* */ + +typedef struct rpcsvc_program rpcsvc_program_t; + +struct rpcsvc_notify_wrapper { + struct list_head list; + void *data; + rpcsvc_notify_t notify; +}; +typedef struct rpcsvc_notify_wrapper rpcsvc_notify_wrapper_t; + +typedef struct rpcsvc_request rpcsvc_request_t; + +typedef struct { + rpc_transport_t *trans; + rpcsvc_t *svc; + /* FIXME: remove address from this structure. Instead use get_myaddr + * interface implemented by individual transports. + */ + struct sockaddr_storage sa; + struct list_head list; +} rpcsvc_listener_t; + +struct rpcsvc_config { + int max_block_size; +}; + +#define rpcsvc_auth_flavour(au) ((au).flavour) + +typedef struct drc_client drc_client_t; +typedef struct drc_cached_op drc_cached_op_t; + +/* The container for the RPC call handed up to an actor. + * Dynamically allocated. Lives till the call reply is completely + * transmitted. + * */ +struct rpcsvc_request { + /* connection over which this request came. */ + rpc_transport_t *trans; + + rpcsvc_t *svc; + + rpcsvc_program_t *prog; + + int prognum; + + int progver; + + int procnum; + + int type; + + /* Uid and gid filled by the rpc-auth module during the authentication + * phase. + */ + uid_t uid; + gid_t gid; + pid_t pid; + + gf_lkowner_t lk_owner; + uint64_t gfs_id; + + /* Might want to move this to AUTH_UNIX specific state since this array + * is not available for every authentication scheme. + */ + gid_t *auxgids; + gid_t auxgidsmall[SMALL_GROUP_COUNT]; + gid_t *auxgidlarge; + int auxgidcount; + + /* The RPC message payload, contains the data required + * by the program actors. This is the buffer that will need to + * be de-xdred by the actor. + */ + int count; + struct iovec msg[MAX_IOVEC]; + + struct iobref *iobref; + + /* There can be cases of RPC requests where the reply needs to + * be built from multiple sources. E.g. where even the NFS reply + * can contain a payload, as in the NFSv3 read reply. Here the RPC header + * ,NFS header and the read data are brought together separately from + * different buffers, so we need to stage the buffers temporarily here + * before all of them get added to the connection's transmission list. + */ + struct list_head txlist; + + /* While the reply record is being built, this variable keeps track + * of how many bytes have been added to the record. + */ + size_t payloadsize; + + /* The credentials extracted from the rpc request */ + client_auth_data_t cred; + + /* The verified extracted from the rpc request. In request side + * processing this contains the verifier sent by the client, on reply + * side processing, it is filled with the verified that will be + * sent to the client. + */ + client_auth_data_t verf; + /* Container for a RPC program wanting to store a temp + * request-specific item. + */ + void *private; + + /* Container for transport to store request-specific item */ + void *trans_private; + + /* pointer to cached reply for use in DRC */ + drc_cached_op_t *reply; + + /* request queue in rpcsvc */ + struct list_head request_list; + + /* Status of the RPC call, whether it was accepted or denied. */ + int rpc_status; + + /* In case, the call was denied, the RPC error is stored here + * till the reply is sent. + */ + int rpc_err; + + /* In case the failure happened because of an authentication problem + * , this value needs to be assigned the correct auth error number. + */ + int auth_err; + + /* Things passed to rpc layer from client */ + + /* @flags: Can be used for binary data passed in xdata to be + passed here instead */ + unsigned int flags; + + /* ctime: origin of time on the client side, ideally this is + the one we should consider for time */ + struct timespec ctime; + + /* The identifier for the call from client. + * Needed to pair the reply with the call. + */ + uint32_t xid; + + /* Execute this request's actor function in ownthread of program?*/ + gf_boolean_t ownthread; + + gf_boolean_t synctask; + struct timespec begin; /*req handling start time*/ + struct timespec end; /*req handling end time*/ +}; + +#define rpcsvc_request_program(req) ((rpcsvc_program_t *)((req)->prog)) +#define rpcsvc_request_procnum(req) (((req)->procnum)) +#define rpcsvc_request_program_private(req) \ + (((rpcsvc_program_t *)((req)->prog))->private) +#define rpcsvc_request_accepted(req) ((req)->rpc_status == MSG_ACCEPTED) +#define rpcsvc_request_accepted_success(req) ((req)->rpc_err == SUCCESS) +#define rpcsvc_request_prog_minauth(req) (rpcsvc_request_program(req)->min_auth) +#define rpcsvc_request_cred_flavour(req) (rpcsvc_auth_flavour(req->cred)) +#define rpcsvc_request_verf_flavour(req) (rpcsvc_auth_flavour(req->verf)) +#define rpcsvc_request_service(req) ((req)->svc) +#define rpcsvc_request_uid(req) ((req)->uid) +#define rpcsvc_request_gid(req) ((req)->gid) +#define rpcsvc_request_private(req) ((req)->private) +#define rpcsvc_request_xid(req) ((req)->xid) +#define rpcsvc_request_set_private(req, prv) (req)->private = (void *)(prv) +#define rpcsvc_request_iobref_ref(req) (iobref_ref((req)->iobref)) +#define rpcsvc_request_record_ref(req) (iobuf_ref((req)->recordiob)) +#define rpcsvc_request_record_unref(req) (iobuf_unref((req)->recordiob)) +#define rpcsvc_request_record_iob(req) ((req)->recordiob) +#define rpcsvc_request_set_vecstate(req, state) ((req)->vecstate = state) +#define rpcsvc_request_vecstate(req) ((req)->vecstate) +#define rpcsvc_request_transport(req) ((req)->trans) +#define rpcsvc_request_transport_ref(req) (rpc_transport_ref((req)->trans)) +#define RPC_AUTH_ROOT_SQUASH(req) \ + do { \ + int gidcount = 0; \ + if (req->svc->root_squash) { \ + if (req->uid == RPC_ROOT_UID) \ + req->uid = req->svc->anonuid; \ + if (req->gid == RPC_ROOT_GID) \ + req->gid = req->svc->anongid; \ + \ + for (gidcount = 0; gidcount < req->auxgidcount; ++gidcount) { \ + if (!req->auxgids[gidcount]) \ + req->auxgids[gidcount] = req->svc->anongid; \ + } \ + } \ + } while (0); + +#define RPC_AUTH_ALL_SQUASH(req) \ + do { \ + int gidcount = 0; \ + if (req->svc->all_squash) { \ + req->uid = req->svc->anonuid; \ + req->gid = req->svc->anongid; \ + \ + for (gidcount = 0; gidcount < req->auxgidcount; ++gidcount) { \ + if (!req->auxgids[gidcount]) \ + req->auxgids[gidcount] = req->svc->anongid; \ + } \ + } \ + } while (0); + +#define RPCSVC_ACTOR_SUCCESS 0 +#define RPCSVC_ACTOR_ERROR (-1) +#define RPCSVC_ACTOR_IGNORE (-2) + +/* Functor for every type of protocol actor + * must be defined like this. + * + * See the request structure for info on how to handle the request + * in the program actor. + * + * On successful santify checks inside the actor, it should return + * RPCSVC_ACTOR_SUCCESS. + * On an error, on which the RPC layer is expected to return a reply, the actor + * should return RPCSVC_ACTOR_ERROR. + * + */ +typedef int (*rpcsvc_actor)(rpcsvc_request_t *req); +typedef int (*rpcsvc_vector_sizer)(int state, ssize_t *readsize, + char *base_addr, char *curr_addr); + +/* Every protocol actor will also need to specify the function the RPC layer + * will use to serialize or encode the message into XDR format just before + * transmitting on the connection. + */ +typedef void *(*rpcsvc_encode_reply)(void *msg); + +/* Once the reply has been transmitted, the message will have to be de-allocated + * , so every actor will need to provide a function that deallocates the message + * it had allocated as a response. + */ +typedef void (*rpcsvc_deallocate_reply)(void *msg); + +#define RPCSVC_NAME_MAX 32 +/* The descriptor for each procedure/actor that runs + * over the RPC service. + */ +typedef struct rpcsvc_actor_desc { + char procname[RPCSVC_NAME_MAX]; + rpcsvc_actor actor; + + /* Handler for cases where the RPC requests fragments are large enough + * to benefit from being decoded into aligned memory addresses. While + * decoding the request in a non-vectored manner, due to the nature of + * the XDR scheme, RPC cannot guarantee memory aligned addresses for + * the resulting message-specific structures. Allowing a specialized + * handler for letting the RPC program read the data from the network + * directly into its aligned buffers. + */ + rpcsvc_vector_sizer vector_sizer; + + int procnum; + + /* Can actor be ran on behalf an unprivileged requestor? */ + drc_op_type_t op_type; + gf_boolean_t unprivileged; +} rpcsvc_actor_t; + +typedef struct rpcsvc_request_queue { + struct list_head request_queue; + pthread_mutex_t queue_lock; + pthread_cond_t queue_cond; + pthread_t thread; + struct rpcsvc_program *program; + int gen; + gf_boolean_t waiting; +} rpcsvc_request_queue_t; + +/* Describes a program and its version along with the function pointers + * required to handle the procedures/actors of each program/version. + * Never changed ever by any thread so no need for a lock. + */ +struct rpcsvc_program { + char progname[RPCSVC_NAME_MAX]; + int prognum; + int progver; + /* FIXME */ + dict_t *options; /* An opaque dictionary + * populated by the program + * (probably from xl->options) + * which contain enough + * information for transport to + * initialize. As a part of + * cleanup, the members of + * options which are of interest + * to transport should be put + * into a structure for better + * readability and structure + * should replace options member + * here. + */ +#if 0 + int progaddrfamily; /* AF_INET or AF_INET6 */ + char *proghost; /* Bind host, can be NULL */ +#endif + rpcsvc_actor_t *actors; /* All procedure handlers */ + int numactors; /* Num actors in actor array */ + int proghighvers; /* Highest ver for program + supported by the system. */ + /* Program specific state handed to actors */ + void *private; + gf_latency_t *latencies; /*Tracks latency statistics for the rpc call*/ + + /* This upcall is provided by the program during registration. + * It is used to notify the program about events like connection being + * destroyed etc. The rpc program may take appropriate actions, for eg., + * in the case of connection being destroyed, it should cleanup its + * state stored in the connection. + */ + rpcsvc_notify_t notify; + + int proglowvers; /* Lowest ver */ + + /* An integer that identifies the min auth strength that is required + * by this protocol, for eg. MOUNT3 needs AUTH_UNIX at least. + * See RFC 1813, Section 5.2.1. + */ + int min_auth; + + /* list member to link to list of registered services with rpcsvc */ + struct list_head program; + rpcsvc_request_queue_t request_queue[EVENT_MAX_THREADS]; + pthread_mutex_t thr_lock; + pthread_cond_t thr_cond; + int threadcount; + int thr_queue; + pthread_key_t req_queue_key; + + /* eventthreadcount is just a readonly copy of the actual value + * owned by the event sub-system + * It is used to control the scaling of rpcsvc_request_handler threads + */ + int eventthreadcount; + uint16_t progport; /* Registered with portmap */ + /* Execute actor function in program's own thread? This will reduce */ + /* the workload on poller threads */ + gf_boolean_t ownthread; + gf_boolean_t alive; + + gf_boolean_t synctask; + unsigned long request_queue_status[EVENT_MAX_THREADS / __BITS_PER_LONG]; +}; + +typedef struct rpcsvc_cbk_program { + char *progname; + int prognum; + int progver; +} rpcsvc_cbk_program_t; +/* All users of RPC services should use this API to register their + * procedure handlers. + */ +extern int +rpcsvc_program_register(rpcsvc_t *svc, rpcsvc_program_t *program, + gf_boolean_t add_to_head); + +extern int +rpcsvc_program_unregister(rpcsvc_t *svc, rpcsvc_program_t *program); + +/* This will create and add a listener to listener pool. Programs can + * use any of the listener in this pool. A single listener can be used by + * multiple programs and vice versa. There can also be a one to one mapping + * between a program and a listener. After registering a program with rpcsvc, + * the program has to be associated with a listener using + * rpcsvc_program_register_portmap. + */ +/* FIXME: can multiple programs registered on same port? */ +extern int32_t +rpcsvc_create_listeners(rpcsvc_t *svc, dict_t *options, char *name); + +void +rpcsvc_listener_destroy(rpcsvc_listener_t *listener); + +extern int +rpcsvc_program_register_portmap(rpcsvc_program_t *newprog, uint32_t port); + +#ifdef IPV6_DEFAULT +extern int +rpcsvc_program_register_rpcbind6(rpcsvc_program_t *newprog, uint32_t port); +extern int +rpcsvc_program_unregister_rpcbind6(rpcsvc_program_t *newprog); +#endif + +extern int +rpcsvc_program_unregister_portmap(rpcsvc_program_t *newprog); + +extern int +rpcsvc_register_portmap_enabled(rpcsvc_t *svc); + +/* Inits the global RPC service data structures. + * Called in main. + */ +extern rpcsvc_t * +rpcsvc_init(xlator_t *xl, glusterfs_ctx_t *ctx, dict_t *options, + uint32_t poolcount); + +extern int +rpcsvc_reconfigure_options(rpcsvc_t *svc, dict_t *options); + +int +rpcsvc_register_notify(rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata); + +/* unregister a notification callback @notify with data @mydata from svc. + * returns the number of notification callbacks unregistered. + */ +int +rpcsvc_unregister_notify(rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata); + +int +rpcsvc_transport_submit(rpc_transport_t *trans, struct iovec *rpchdr, + int rpchdrcount, struct iovec *proghdr, + int proghdrcount, struct iovec *progpayload, + int progpayloadcount, struct iobref *iobref, + void *priv); + +int +rpcsvc_submit_message(rpcsvc_request_t *req, struct iovec *proghdr, + int hdrcount, struct iovec *payload, int payloadcount, + struct iobref *iobref); + +int +rpcsvc_submit_generic(rpcsvc_request_t *req, struct iovec *proghdr, + int hdrcount, struct iovec *payload, int payloadcount, + struct iobref *iobref); + +extern int +rpcsvc_error_reply(rpcsvc_request_t *req); + +#define RPCSVC_PEER_STRLEN 1024 +#define RPCSVC_AUTH_ACCEPT 1 +#define RPCSVC_AUTH_REJECT 2 +#define RPCSVC_AUTH_DONTCARE 3 + +extern int +rpcsvc_transport_peername(rpc_transport_t *trans, char *hostname, int hostlen); + +extern int +rpcsvc_transport_peeraddr(rpc_transport_t *trans, char *addrstr, int addrlen, + struct sockaddr_storage *returnsa, socklen_t sasize); + +extern int +rpcsvc_auth_check(rpcsvc_t *svc, char *volname, char *ipaddr); + +extern int +rpcsvc_transport_privport_check(rpcsvc_t *svc, char *volname, uint16_t port); + +#define rpcsvc_request_seterr(req, err) ((req)->rpc_err = (int)(err)) +#define rpcsvc_request_set_autherr(req, err) ((req)->auth_err = (int)(err)) + +extern int +rpcsvc_submit_vectors(rpcsvc_request_t *req); + +extern int +rpcsvc_request_attach_vector(rpcsvc_request_t *req, struct iovec msgvec, + struct iobuf *iob, struct iobref *ioref, + int finalvector); + +typedef int (*auth_init_trans)(rpc_transport_t *trans, void *priv); +typedef int (*auth_init_request)(rpcsvc_request_t *req, void *priv); +typedef int (*auth_request_authenticate)(rpcsvc_request_t *req, void *priv); + +/* This structure needs to be registered by every authentication scheme. + * Our authentication schemes are stored per connection because + * each connection will end up using a different authentication scheme. + */ +typedef struct rpcsvc_auth_ops { + auth_init_trans transport_init; + auth_init_request request_init; + auth_request_authenticate authenticate; +} rpcsvc_auth_ops_t; + +typedef struct rpcsvc_auth_flavour_desc { + char authname[RPCSVC_NAME_MAX]; + rpcsvc_auth_ops_t *authops; + void *authprivate; + int authnum; +} rpcsvc_auth_t; + +typedef void *(*rpcsvc_auth_initer_t)(rpcsvc_t *svc, dict_t *options); + +struct rpcsvc_auth_list { + struct list_head authlist; + rpcsvc_auth_initer_t init; + /* Should be the name with which we identify the auth scheme given + * in the volfile options. + * This should be different from the authname in rpc_auth_t + * in way that makes it easier to specify this scheme in the volfile. + * This is because the technical names of the schemes can be a bit + * arcane. + */ + char name[RPCSVC_NAME_MAX]; + rpcsvc_auth_t *auth; + int enable; +}; + +extern int +rpcsvc_auth_request_init(rpcsvc_request_t *req, struct rpc_msg *callmsg); + +extern int +rpcsvc_auth_init(rpcsvc_t *svc, dict_t *options); + +extern int +rpcsvc_auth_reconf(rpcsvc_t *svc, dict_t *options); + +extern int +rpcsvc_auth_transport_init(rpc_transport_t *xprt); + +extern int +rpcsvc_authenticate(rpcsvc_request_t *req); + +extern int +rpcsvc_auth_array(rpcsvc_t *svc, char *volname, int *autharr, int arrlen); + +/* If the request has been sent using AUTH_UNIX, this function returns the + * auxiliary gids as an array, otherwise, it returns NULL. + * Move to auth-unix specific source file when we need to modularize the + * authentication code even further to support mode auth schemes. + */ +extern gid_t * +rpcsvc_auth_unix_auxgids(rpcsvc_request_t *req, int *arrlen); + +extern char * +rpcsvc_volume_allowed(dict_t *options, char *volname); + +int +rpcsvc_request_submit(rpcsvc_t *rpc, rpc_transport_t *trans, + rpcsvc_cbk_program_t *prog, int procnum, void *req, + glusterfs_ctx_t *ctx, xdrproc_t xdrproc); + +int +rpcsvc_callback_submit(rpcsvc_t *rpc, rpc_transport_t *trans, + rpcsvc_cbk_program_t *prog, int procnum, + struct iovec *proghdr, int proghdrcount, + struct iobref *iobref); + +rpcsvc_actor_t * +rpcsvc_program_actor(rpcsvc_request_t *req); + +int +rpcsvc_transport_unix_options_build(dict_t *options, char *filepath); +int +rpcsvc_set_allow_insecure(rpcsvc_t *svc, dict_t *options); +int +rpcsvc_set_addr_namelookup(rpcsvc_t *svc, dict_t *options); +int +rpcsvc_set_root_squash(rpcsvc_t *svc, dict_t *options); +int +rpcsvc_set_all_squash(rpcsvc_t *svc, dict_t *options); +int +rpcsvc_set_outstanding_rpc_limit(rpcsvc_t *svc, dict_t *options, int defvalue); + +int +rpcsvc_set_throttle_on(rpcsvc_t *svc); + +int +rpcsvc_set_throttle_off(rpcsvc_t *svc); + +gf_boolean_t +rpcsvc_get_throttle(rpcsvc_t *svc); + +int +rpcsvc_auth_array(rpcsvc_t *svc, char *volname, int *autharr, int arrlen); +rpcsvc_vector_sizer +rpcsvc_get_program_vector_sizer(rpcsvc_t *svc, uint32_t prognum, + uint32_t progver, int procnum); +void +rpcsvc_autoscale_threads(glusterfs_ctx_t *ctx, rpcsvc_t *rpc, int incr); + +extern int +rpcsvc_destroy(rpcsvc_t *svc); +void +rpcsvc_statedump(rpcsvc_t *svc); +#endif diff --git a/rpc/rpc-lib/src/xdr-common.h b/rpc/rpc-lib/src/xdr-common.h new file mode 100644 index 00000000000..752736b3d4d --- /dev/null +++ b/rpc/rpc-lib/src/xdr-common.h @@ -0,0 +1,116 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _XDR_COMMON_H_ +#define _XDR_COMMON_H_ + +#include <rpc/types.h> +#include <sys/types.h> +#include <rpc/xdr.h> +#include <rpc/auth.h> +#include <sys/uio.h> + +#ifdef __NetBSD__ +#include <dirent.h> +#endif /* __NetBSD__ */ + +enum gf_dump_procnum { + GF_DUMP_NULL, + GF_DUMP_DUMP, + GF_DUMP_PING, + GF_DUMP_MAXVALUE, +}; + +#define GLUSTER_DUMP_PROGRAM 123451501 /* Completely random */ +#define GLUSTER_DUMP_VERSION 1 + +/* MAX_AUTH_BYTES is restricted to 400 bytes, see + * http://tools.ietf.org/html/rfc5531#section-8.2 */ +#define GF_MAX_AUTH_BYTES MAX_AUTH_BYTES + +/* The size of an AUTH_GLUSTERFS_V2 structure: + * + * 1 | pid + * 1 | uid + * 1 | gid + * 1 | groups_len + * XX | groups_val (GF_MAX_AUX_GROUPS=65535) + * 1 | lk_owner_len + * YY | lk_owner_val (GF_MAX_LOCK_OWNER_LEN=1024) + * ----+------------------------------------------- + * 5 | total xdr-units + * + * one XDR-unit is defined as BYTES_PER_XDR_UNIT = 4 bytes + * MAX_AUTH_BYTES = 400 is the maximum, this is 100 xdr-units. + * XX + YY can be 95 to fill the 100 xdr-units. + * + * Note that the on-wire protocol has tighter requirements than the internal + * structures. It is possible for xlators to use more groups and a bigger + * lk_owner than that can be sent by a GlusterFS-client. + * + * ------- + * On v3, there are 4 more units, and hence it will be 9 xdr-units + */ +#define GF_AUTH_GLUSTERFS_MAX_GROUPS(lk_len, type) \ + ((type == AUTH_GLUSTERFS_v2) ? (95 - lk_len) : (91 - lk_len)) +#define GF_AUTH_GLUSTERFS_MAX_LKOWNER(groups_len, type) \ + ((type == AUTH_GLUSTERFS_v2) ? (95 - groups_len) : (91 - groups_len)) + +#ifdef GF_LINUX_HOST_OS +#define xdr_u_int32_t xdr_uint32_t +#define xdr_u_int64_t xdr_uint64_t +unsigned long +xdr_sizeof(xdrproc_t func, void *data); +#endif + +#ifdef GF_DARWIN_HOST_OS +#define xdr_u_quad_t xdr_u_int64_t +#define xdr_quad_t xdr_int64_t +#define xdr_uint32_t xdr_u_int32_t +#define xdr_uint64_t xdr_u_int64_t +#define uint64_t u_int64_t +unsigned long +xdr_sizeof(xdrproc_t func, void *data); +#endif + +#if defined(__NetBSD__) +#define xdr_u_quad_t xdr_u_int64_t +#define xdr_quad_t xdr_int64_t +#define xdr_uint32_t xdr_u_int32_t +#define xdr_uint64_t xdr_u_int64_t +#endif + +#ifdef GF_SOLARIS_HOST_OS +#define u_quad_t uint64_t +#define quad_t int64_t +#define xdr_u_quad_t xdr_uint64_t +#define xdr_quad_t xdr_int64_t +#endif + +/* Returns the address of the byte that follows the + * last byte used for decoding the previous xdr component. + * E.g. once the RPC call for NFS has been decoded, the macro will return + * the address from which the NFS header starts. + */ +#define xdr_decoded_remaining_addr(xdr) ((&xdr)->x_private) + +/* Returns the length of the remaining record after the previous decode + * operation completed. + */ +#define xdr_decoded_remaining_len(xdr) ((&xdr)->x_handy) + +/* Returns the number of bytes used by the last encode operation. */ +#define xdr_encoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) + +#define xdr_decoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) + +#endif diff --git a/rpc/rpc-lib/src/xdr-rpc.c b/rpc/rpc-lib/src/xdr-rpc.c new file mode 100644 index 00000000000..4992dc5a7ce --- /dev/null +++ b/rpc/rpc-lib/src/xdr-rpc.c @@ -0,0 +1,198 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <rpc/rpc.h> +#include <rpc/xdr.h> +#include <sys/uio.h> +#include <rpc/auth_unix.h> + +#include "xdr-rpc.h" +#include "xdr-common.h" +#include <glusterfs/common-utils.h> + +/* Decodes the XDR format in msgbuf into rpc_msg. + * The remaining payload is returned into payload. + */ +int +xdr_to_rpc_call(char *msgbuf, size_t len, struct rpc_msg *call, + struct iovec *payload, char *credbytes, char *verfbytes) +{ + XDR xdr; + char opaquebytes[GF_MAX_AUTH_BYTES]; + struct opaque_auth *oa = NULL; + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", msgbuf, out); + GF_VALIDATE_OR_GOTO("rpc", call, out); + + memset(call, 0, sizeof(*call)); + + oa = &call->rm_call.cb_cred; + if (!credbytes) + oa->oa_base = opaquebytes; + else + oa->oa_base = credbytes; + + oa = &call->rm_call.cb_verf; + if (!verfbytes) + oa->oa_base = opaquebytes; + else + oa->oa_base = verfbytes; + + xdrmem_create(&xdr, msgbuf, len, XDR_DECODE); + if (!xdr_callmsg(&xdr, call)) { + gf_log("rpc", GF_LOG_WARNING, "failed to decode call msg"); + goto out; + } + + if (payload) { + payload->iov_base = xdr_decoded_remaining_addr(xdr); + payload->iov_len = xdr_decoded_remaining_len(xdr); + } + + ret = 0; +out: + return ret; +} + +bool_t +true_func(XDR *s, caddr_t *a) +{ + return TRUE; +} + +int +rpc_fill_empty_reply(struct rpc_msg *reply, uint32_t xid) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", reply, out); + + /* Setting to 0 also results in reply verifier flavor to be + * set to AUTH_NULL which is what we want right now. + */ + memset(reply, 0, sizeof(*reply)); + reply->rm_xid = xid; + reply->rm_direction = REPLY; + + ret = 0; +out: + return ret; +} + +int +rpc_fill_denied_reply(struct rpc_msg *reply, int rjstat, int auth_err) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", reply, out); + + reply->rm_reply.rp_stat = MSG_DENIED; + reply->rjcted_rply.rj_stat = rjstat; + if (rjstat == RPC_MISMATCH) { + /* No problem with hardcoding + * RPC version numbers. We only support + * v2 anyway. + */ + reply->rjcted_rply.rj_vers.low = 2; + reply->rjcted_rply.rj_vers.high = 2; + } else if (rjstat == AUTH_ERROR) + reply->rjcted_rply.rj_why = auth_err; + + ret = 0; +out: + return ret; +} + +int +rpc_fill_accepted_reply(struct rpc_msg *reply, int arstat, int proglow, + int proghigh, int verf, int len, char *vdata) +{ + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", reply, out); + + reply->rm_reply.rp_stat = MSG_ACCEPTED; + reply->acpted_rply.ar_stat = arstat; + + reply->acpted_rply.ar_verf.oa_flavor = verf; + reply->acpted_rply.ar_verf.oa_length = len; + reply->acpted_rply.ar_verf.oa_base = vdata; + if (arstat == PROG_MISMATCH) { + reply->acpted_rply.ar_vers.low = proglow; + reply->acpted_rply.ar_vers.high = proghigh; + } else if (arstat == SUCCESS) { + /* This is a hack. I'd really like to build a custom + * XDR library because Sun RPC interface is not very flexible. + */ + reply->acpted_rply.ar_results.proc = (xdrproc_t)true_func; + reply->acpted_rply.ar_results.where = NULL; + } + + ret = 0; +out: + return ret; +} + +int +rpc_reply_to_xdr(struct rpc_msg *reply, char *dest, size_t len, + struct iovec *dst) +{ + XDR xdr; + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", reply, out); + GF_VALIDATE_OR_GOTO("rpc", dest, out); + GF_VALIDATE_OR_GOTO("rpc", dst, out); + + xdrmem_create(&xdr, dest, len, XDR_ENCODE); + if (!xdr_replymsg(&xdr, reply)) { + gf_log("rpc", GF_LOG_WARNING, "failed to encode reply msg"); + goto out; + } + + dst->iov_base = dest; + dst->iov_len = xdr_encoded_length(xdr); + + ret = 0; +out: + return ret; +} + +int +xdr_to_auth_unix_cred(char *msgbuf, int msglen, struct authunix_parms *au, + char *machname, gid_t *gids) +{ + XDR xdr; + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", msgbuf, out); + GF_VALIDATE_OR_GOTO("rpc", machname, out); + GF_VALIDATE_OR_GOTO("rpc", gids, out); + GF_VALIDATE_OR_GOTO("rpc", au, out); + + au->aup_machname = machname; +#if defined(GF_DARWIN_HOST_OS) || defined(__FreeBSD__) + au->aup_gids = (int *)gids; +#else + au->aup_gids = gids; +#endif + + xdrmem_create(&xdr, msgbuf, msglen, XDR_DECODE); + + if (!xdr_authunix_parms(&xdr, au)) { + gf_log("rpc", GF_LOG_WARNING, "failed to decode auth unix parms"); + goto out; + } + + ret = 0; +out: + return ret; +} diff --git a/rpc/rpc-lib/src/xdr-rpc.h b/rpc/rpc-lib/src/xdr-rpc.h new file mode 100644 index 00000000000..7baed273846 --- /dev/null +++ b/rpc/rpc-lib/src/xdr-rpc.h @@ -0,0 +1,95 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _XDR_RPC_H_ +#define _XDR_RPC_H_ + +#ifndef GF_SOLARIS_HOST_OS +#include <rpc/rpc.h> +#endif + +#ifdef GF_SOLARIS_HOST_OS +#include <rpc/auth.h> +#include <rpc/auth_sys.h> +#endif + +#include <arpa/inet.h> +#include <rpc/xdr.h> +#include <sys/uio.h> + +#include "xdr-common.h" + +typedef enum { + AUTH_GLUSTERFS = 5, + AUTH_GLUSTERFS_v2 = 390039, /* using a number from 'unused' range, + from the list available in RFC5531 */ + AUTH_GLUSTERFS_v3 = 390040, /* this too is unused */ +} gf_rpc_authtype_t; + +/* Converts a given network buffer from its XDR format to a structure + * that contains everything an RPC call needs to work. + */ +extern int +xdr_to_rpc_call(char *msgbuf, size_t len, struct rpc_msg *call, + struct iovec *payload, char *credbytes, char *verfbytes); + +extern int +rpc_fill_empty_reply(struct rpc_msg *reply, uint32_t xid); + +extern int +rpc_fill_denied_reply(struct rpc_msg *reply, int rjstat, int auth_err); + +extern int +rpc_fill_accepted_reply(struct rpc_msg *reply, int arstat, int proglow, + int proghigh, int verf, int len, char *vdata); +extern int +rpc_reply_to_xdr(struct rpc_msg *reply, char *dest, size_t len, + struct iovec *dst); + +extern int +xdr_to_auth_unix_cred(char *msgbuf, int msglen, struct authunix_parms *au, + char *machname, gid_t *gids); +/* Macros that simplify accessing the members of an RPC call structure. */ +#define rpc_call_xid(call) ((call)->rm_xid) +#define rpc_call_direction(call) ((call)->rm_direction) +#define rpc_call_rpcvers(call) ((call)->ru.RM_cmb.cb_rpcvers) +#define rpc_call_program(call) ((call)->ru.RM_cmb.cb_prog) +#define rpc_call_progver(call) ((call)->ru.RM_cmb.cb_vers) +#define rpc_call_progproc(call) ((call)->ru.RM_cmb.cb_proc) +#define rpc_opaque_auth_flavour(oa) ((oa)->oa_flavor) +#define rpc_opaque_auth_len(oa) ((oa)->oa_length) + +#define rpc_call_cred_flavour(call) \ + (rpc_opaque_auth_flavour((&(call)->ru.RM_cmb.cb_cred))) +#define rpc_call_cred_len(call) \ + (rpc_opaque_auth_len((&(call)->ru.RM_cmb.cb_cred))) + +#define rpc_call_verf_flavour(call) \ + (rpc_opaque_auth_flavour((&(call)->ru.RM_cmb.cb_verf))) +#define rpc_call_verf_len(call) \ + (rpc_opaque_auth_len((&(call)->ru.RM_cmb.cb_verf))) + +#if defined(GF_DARWIN_HOST_OS) || !defined(HAVE_RPC_RPC_H) +#define GF_PRI_RPC_XID PRIu32 +#define GF_PRI_RPC_VERSION PRIu32 +#define GF_PRI_RPC_PROG_ID PRIu32 +#define GF_PRI_RPC_PROG_VERS PRIu32 +#define GF_PRI_RPC_PROC PRIu32 +#define GF_PRI_RPC_PROC_VERSION PRIu32 +#else +#define GF_PRI_RPC_XID PRIu64 +#define GF_PRI_RPC_VERSION PRIu64 +#define GF_PRI_RPC_PROG_ID PRIu64 +#define GF_PRI_RPC_PROG_VERS PRIu64 +#define GF_PRI_RPC_PROC PRIu64 +#define GF_PRI_RPC_PROC_VERSION PRIu64 +#endif + +#endif diff --git a/rpc/rpc-lib/src/xdr-rpcclnt.c b/rpc/rpc-lib/src/xdr-rpcclnt.c new file mode 100644 index 00000000000..8dcdcfeda83 --- /dev/null +++ b/rpc/rpc-lib/src/xdr-rpcclnt.c @@ -0,0 +1,105 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <rpc/rpc.h> +#include <rpc/xdr.h> +#include <sys/uio.h> +#include <rpc/auth_unix.h> +#include <errno.h> + +#include "xdr-rpc.h" +#include "xdr-common.h" +#include <glusterfs/common-utils.h> + +/* Decodes the XDR format in msgbuf into rpc_msg. + * The remaining payload is returned into payload. + */ +int +xdr_to_rpc_reply(char *msgbuf, size_t len, struct rpc_msg *reply, + struct iovec *payload, char *verfbytes) +{ + XDR xdr; + int ret = -EINVAL; + + GF_VALIDATE_OR_GOTO("rpc", msgbuf, out); + GF_VALIDATE_OR_GOTO("rpc", reply, out); + + memset(reply, 0, sizeof(struct rpc_msg)); + + reply->acpted_rply.ar_verf = _null_auth; + reply->acpted_rply.ar_results.where = NULL; + reply->acpted_rply.ar_results.proc = (xdrproc_t)(xdr_void); + + xdrmem_create(&xdr, msgbuf, len, XDR_DECODE); + if (!xdr_replymsg(&xdr, reply)) { + gf_log("rpc", GF_LOG_WARNING, "failed to decode reply msg"); + goto out; + } + if (payload) { + payload->iov_base = xdr_decoded_remaining_addr(xdr); + payload->iov_len = xdr_decoded_remaining_len(xdr); + } + + ret = 0; +out: + return ret; +} + +int +rpc_request_to_xdr(struct rpc_msg *request, char *dest, size_t len, + struct iovec *dst) +{ + XDR xdr; + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", dest, out); + GF_VALIDATE_OR_GOTO("rpc", request, out); + GF_VALIDATE_OR_GOTO("rpc", dst, out); + + xdrmem_create(&xdr, dest, len, XDR_ENCODE); + if (!xdr_callmsg(&xdr, request)) { + gf_log("rpc", GF_LOG_WARNING, "failed to encode call msg"); + goto out; + } + + dst->iov_base = dest; + dst->iov_len = xdr_encoded_length(xdr); + + ret = 0; + +out: + return ret; +} + +int +auth_unix_cred_to_xdr(struct authunix_parms *au, char *dest, size_t len, + struct iovec *iov) +{ + XDR xdr; + int ret = -1; + + GF_VALIDATE_OR_GOTO("rpc", au, out); + GF_VALIDATE_OR_GOTO("rpc", dest, out); + GF_VALIDATE_OR_GOTO("rpc", iov, out); + + xdrmem_create(&xdr, dest, len, XDR_DECODE); + + if (!xdr_authunix_parms(&xdr, au)) { + gf_log("rpc", GF_LOG_WARNING, "failed to decode authunix parms"); + goto out; + } + + iov->iov_base = dest; + iov->iov_len = xdr_encoded_length(xdr); + + ret = 0; +out: + return ret; +} diff --git a/rpc/rpc-lib/src/xdr-rpcclnt.h b/rpc/rpc-lib/src/xdr-rpcclnt.h new file mode 100644 index 00000000000..58eda4892a9 --- /dev/null +++ b/rpc/rpc-lib/src/xdr-rpcclnt.h @@ -0,0 +1,36 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _XDR_RPCCLNT_H +#define _XDR_RPCCLNT_H + +#include <arpa/inet.h> +#include <rpc/xdr.h> +#include <sys/uio.h> +#include <rpc/rpc_msg.h> +#include <rpc/auth_unix.h> + +/* Macros that simplify accessing the members of an RPC call structure. */ +#define rpc_reply_xid(reply) ((reply)->rm_xid) +#define rpc_reply_status(reply) ((reply)->ru.RM_rmb.rp_stat) +#define rpc_accepted_reply_status(reply) ((reply)->acpted_rply.ar_stat) +#define rpc_reply_verf_flavour(reply) ((reply)->acpted_rply.ar_verf.oa_flavor) + +int +xdr_to_rpc_reply(char *msgbuf, size_t len, struct rpc_msg *reply, + struct iovec *payload, char *verfbytes); +int +rpc_request_to_xdr(struct rpc_msg *request, char *dest, size_t len, + struct iovec *dst); +int +auth_unix_cred_to_xdr(struct authunix_parms *au, char *dest, size_t len, + struct iovec *iov); + +#endif diff --git a/rpc/rpc-transport/Makefile.am b/rpc/rpc-transport/Makefile.am new file mode 100644 index 00000000000..7dd9f026cfc --- /dev/null +++ b/rpc/rpc-transport/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = socket diff --git a/rpc/rpc-transport/socket/Makefile.am b/rpc/rpc-transport/socket/Makefile.am new file mode 100644 index 00000000000..f963effea22 --- /dev/null +++ b/rpc/rpc-transport/socket/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src
\ No newline at end of file diff --git a/rpc/rpc-transport/socket/src/Makefile.am b/rpc/rpc-transport/socket/src/Makefile.am new file mode 100644 index 00000000000..7b488583771 --- /dev/null +++ b/rpc/rpc-transport/socket/src/Makefile.am @@ -0,0 +1,22 @@ +noinst_HEADERS = socket.h name.h socket-mem-types.h + +rpctransport_LTLIBRARIES = socket.la +rpctransportdir = $(libdir)/glusterfs/$(PACKAGE_VERSION)/rpc-transport + +socket_la_LDFLAGS = -module -avoid-version + +socket_la_SOURCES = socket.c name.c +socket_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la \ + $(top_builddir)/rpc/xdr/src/libgfxdr.la \ + $(top_builddir)/rpc/rpc-lib/src/libgfrpc.la \ + -lssl + +AM_CPPFLAGS = $(GF_CPPFLAGS) \ + -I$(top_srcdir)/libglusterfs/src \ + -I$(top_srcdir)/rpc/rpc-lib/src/ \ + -I$(top_srcdir)/rpc/xdr/src/ \ + -I$(top_builddir)/rpc/xdr/src/ + +AM_CFLAGS = -Wall $(GF_CFLAGS) + +CLEANFILES = *~ diff --git a/rpc/rpc-transport/socket/src/name.c b/rpc/rpc-transport/socket/src/name.c new file mode 100644 index 00000000000..9286bbb236d --- /dev/null +++ b/rpc/rpc-transport/socket/src/name.c @@ -0,0 +1,768 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <errno.h> +#include <netdb.h> +#include <string.h> + +#ifndef AF_INET_SDP +#define AF_INET_SDP 27 +#endif + +#include "rpc-transport.h" +#include "socket.h" +#include <glusterfs/common-utils.h> + +static void +_assign_port(struct sockaddr *sockaddr, uint16_t port) +{ + switch (sockaddr->sa_family) { + case AF_INET6: + ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port); + break; + + case AF_INET_SDP: + case AF_INET: + ((struct sockaddr_in *)sockaddr)->sin_port = htons(port); + break; + } +} + +static int32_t +af_inet_bind_to_port_lt_ceiling(int fd, struct sockaddr *sockaddr, + socklen_t sockaddr_len, uint32_t ceiling) +{ +#if GF_DISABLE_PRIVPORT_TRACKING + _assign_port(sockaddr, 0); + return bind(fd, sockaddr, sockaddr_len); +#else + int32_t ret = -1; + uint16_t port = ceiling - 1; + unsigned char ports[GF_PORT_ARRAY_SIZE] = { + 0, + }; + int i = 0; + +loop: + ret = gf_process_reserved_ports(ports, ceiling); + + while (port) { + if (port == GF_CLIENT_PORT_CEILING) { + ret = -1; + break; + } + + /* ignore the reserved ports */ + if (BIT_VALUE(ports, port)) { + port--; + continue; + } + + _assign_port(sockaddr, port); + + ret = bind(fd, sockaddr, sockaddr_len); + + if (ret == 0) + break; + + if (ret == -1 && errno == EACCES) + break; + + port--; + } + + /* In case if all the secure ports are exhausted, we are no more + * binding to secure ports, hence instead of getting a random + * port, lets define the range to restrict it from getting from + * ports reserved for bricks i.e from range of 49152 - 65535 + * which further may lead to port clash */ + if (!port) { + ceiling = port = GF_CLNT_INSECURE_PORT_CEILING; + for (i = 0; i <= ceiling; i++) + BIT_CLEAR(ports, i); + goto loop; + } + + return ret; +#endif /* GF_DISABLE_PRIVPORT_TRACKING */ +} + +static int32_t +af_unix_client_bind(rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t sockaddr_len, int sock) +{ + data_t *path_data = NULL; + struct sockaddr_un *addr = NULL; + int32_t ret = 0; + + path_data = dict_get_sizen(this->options, "transport.socket.bind-path"); + if (path_data) { + char *path = data_to_str(path_data); + if (!path || path_data->len > 108) { /* 108 = addr->sun_path length */ + gf_log(this->name, GF_LOG_TRACE, + "bind-path not specified for unix socket, " + "letting connect to assign default value"); + goto err; + } + + addr = (struct sockaddr_un *)sockaddr; + strcpy(addr->sun_path, path); + ret = bind(sock, (struct sockaddr *)addr, sockaddr_len); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, + "cannot bind to unix-domain socket %d (%s)", sock, + strerror(errno)); + goto err; + } + } else { + gf_log(this->name, GF_LOG_TRACE, + "bind-path not specified for unix socket, " + "letting connect to assign default value"); + } + +err: + return ret; +} + +static int32_t +client_fill_address_family(rpc_transport_t *this, sa_family_t *sa_family) +{ + data_t *address_family_data = NULL; + int32_t ret = -1; + + if (sa_family == NULL) { + gf_log_callingfn("", GF_LOG_WARNING, "sa_family argument is NULL"); + goto out; + } + + address_family_data = dict_get_sizen(this->options, + "transport.address-family"); + if (!address_family_data) { + data_t *remote_host_data = NULL, *connect_path_data = NULL; + remote_host_data = dict_get_sizen(this->options, "remote-host"); + connect_path_data = dict_get_sizen(this->options, + "transport.socket.connect-path"); + + if (!(remote_host_data || connect_path_data) || + (remote_host_data && connect_path_data)) { + gf_log(this->name, GF_LOG_ERROR, + "transport.address-family not specified. " + "Could not guess default value from (remote-host:%s or " + "transport.unix.connect-path:%s) options", + data_to_str(remote_host_data), + data_to_str(connect_path_data)); + *sa_family = AF_UNSPEC; + goto out; + } + + if (remote_host_data) { + gf_log(this->name, GF_LOG_DEBUG, + "address-family not specified, marking it as unspec " + "for getaddrinfo to resolve from (remote-host: %s)", + data_to_str(remote_host_data)); + *sa_family = AF_UNSPEC; + } else { + gf_log(this->name, GF_LOG_DEBUG, + "address-family not specified, guessing it " + "to be unix from (transport.unix.connect-path: %s)", + data_to_str(connect_path_data)); + *sa_family = AF_UNIX; + } + + } else { + const char *address_family = data_to_str(address_family_data); + if (!strcasecmp(address_family, "unix")) { + *sa_family = AF_UNIX; + } else if (!strcasecmp(address_family, "inet")) { + *sa_family = AF_INET; + } else if (!strcasecmp(address_family, "inet6")) { + *sa_family = AF_INET6; + } else if (!strcasecmp(address_family, "inet-sdp")) { + *sa_family = AF_INET_SDP; + } else { + gf_log(this->name, GF_LOG_ERROR, + "unknown address-family (%s) specified", address_family); + *sa_family = AF_UNSPEC; + goto out; + } + } + + ret = 0; + +out: + return ret; +} + +static int32_t +af_inet_client_get_remote_sockaddr(rpc_transport_t *this, + struct sockaddr *sockaddr, + socklen_t *sockaddr_len) +{ + dict_t *options = this->options; + data_t *remote_host_data = NULL; + data_t *remote_port_data = NULL; + char *remote_host = NULL; + uint16_t remote_port = GF_DEFAULT_SOCKET_LISTEN_PORT; + struct addrinfo *addr_info = NULL; + int32_t ret = 0; + struct in6_addr serveraddr; + + remote_host_data = dict_get_sizen(options, "remote-host"); + if (remote_host_data == NULL) { + gf_log(this->name, GF_LOG_ERROR, + "option remote-host missing in volume %s", this->name); + ret = -1; + goto err; + } + + remote_host = data_to_str(remote_host_data); + if (remote_host == NULL) { + gf_log(this->name, GF_LOG_ERROR, + "option remote-host has data NULL in volume %s", this->name); + ret = -1; + goto err; + } + + remote_port_data = dict_get_sizen(options, "remote-port"); + if (remote_port_data == NULL) { + gf_log(this->name, GF_LOG_TRACE, + "option remote-port missing in volume %s. Defaulting to %d", + this->name, GF_DEFAULT_SOCKET_LISTEN_PORT); + } else { + remote_port = data_to_uint16(remote_port_data); + if (remote_port == (uint16_t)-1) { + gf_log(this->name, GF_LOG_ERROR, + "option remote-port has invalid port in volume %s", + this->name); + ret = -1; + goto err; + } + } + + /* Need to update transport-address family if address-family is not provided + to command-line arguments + */ + if (inet_pton(AF_INET6, remote_host, &serveraddr)) { + sockaddr->sa_family = AF_INET6; + } + + /* TODO: gf_resolve is a blocking call. kick in some + non blocking dns techniques */ + ret = gf_resolve_ip6(remote_host, remote_port, sockaddr->sa_family, + &this->dnscache, &addr_info); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, "DNS resolution failed on host %s", + remote_host); + goto err; + } + + memcpy(sockaddr, addr_info->ai_addr, addr_info->ai_addrlen); + *sockaddr_len = addr_info->ai_addrlen; + +err: + return ret; +} + +static int32_t +af_unix_client_get_remote_sockaddr(rpc_transport_t *this, + struct sockaddr *sockaddr, + socklen_t *sockaddr_len) +{ + struct sockaddr_un *sockaddr_un = NULL; + char *connect_path = NULL; + data_t *connect_path_data = NULL; + int32_t ret = -1; + + connect_path_data = dict_get_sizen(this->options, + "transport.socket.connect-path"); + if (!connect_path_data) { + gf_log(this->name, GF_LOG_ERROR, + "option transport.unix.connect-path not specified for " + "address-family unix"); + goto err; + } + + /* 108 = sockaddr_un->sun_path length */ + if ((connect_path_data->len + 1) > 108) { + gf_log(this->name, GF_LOG_ERROR, + "connect-path value length %d > %d octets", + connect_path_data->len + 1, UNIX_PATH_MAX); + goto err; + } + + connect_path = data_to_str(connect_path_data); + if (!connect_path) { + gf_log(this->name, GF_LOG_ERROR, + "transport.unix.connect-path is null-string"); + goto err; + } + + gf_log(this->name, GF_LOG_TRACE, "using connect-path %s", connect_path); + sockaddr_un = (struct sockaddr_un *)sockaddr; + strcpy(sockaddr_un->sun_path, connect_path); + *sockaddr_len = sizeof(struct sockaddr_un); + + ret = 0; +err: + return ret; +} + +static int32_t +af_unix_server_get_local_sockaddr(rpc_transport_t *this, struct sockaddr *addr, + socklen_t *addr_len) +{ + data_t *listen_path_data = NULL; + char *listen_path = NULL; + int32_t ret = 0; + struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; + + listen_path_data = dict_get_sizen(this->options, + "transport.socket.listen-path"); + if (!listen_path_data) { + gf_log(this->name, GF_LOG_ERROR, + "missing option transport.socket.listen-path"); + ret = -1; + goto err; + } + + listen_path = data_to_str(listen_path_data); + +#ifndef UNIX_PATH_MAX +#define UNIX_PATH_MAX 108 +#endif + + if ((listen_path_data->len + 1) > UNIX_PATH_MAX) { + gf_log(this->name, GF_LOG_ERROR, + "option transport.unix.listen-path has value length " + "%" GF_PRI_SIZET " > %d", + strlen(listen_path), UNIX_PATH_MAX); + ret = -1; + goto err; + } + + sunaddr->sun_family = AF_UNIX; + strcpy(sunaddr->sun_path, listen_path); + *addr_len = sizeof(struct sockaddr_un); + +err: + return ret; +} + +static int32_t +af_inet_server_get_local_sockaddr(rpc_transport_t *this, struct sockaddr *addr, + socklen_t *addr_len) +{ + struct addrinfo hints, *res = 0, *rp = NULL; + data_t *listen_port_data = NULL, *listen_host_data = NULL; + uint16_t listen_port = 0; + char service[NI_MAXSERV], *listen_host = NULL; + dict_t *options = NULL; + int32_t ret = 0; + + /* initializes addr_len */ + *addr_len = 0; + + options = this->options; + + listen_port_data = dict_get_sizen(options, "transport.socket.listen-port"); + if (listen_port_data) { + listen_port = data_to_uint16(listen_port_data); + } else { + listen_port = GF_DEFAULT_SOCKET_LISTEN_PORT; + } + + listen_host_data = dict_get_sizen(options, "transport.socket.bind-address"); + if (listen_host_data) { + listen_host = data_to_str(listen_host_data); + } else { + if (addr->sa_family == AF_INET6) { + struct sockaddr_in6 *in = (struct sockaddr_in6 *)addr; + in->sin6_addr = in6addr_any; + in->sin6_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in6); + goto out; + } else if (addr->sa_family == AF_INET) { + struct sockaddr_in *in = (struct sockaddr_in *)addr; + in->sin_addr.s_addr = htonl(INADDR_ANY); + in->sin_port = htons(listen_port); + *addr_len = sizeof(struct sockaddr_in); + goto out; + } + } + + sprintf(service, "%d", listen_port); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = addr->sa_family; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + ret = getaddrinfo(listen_host, service, &hints, &res); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "getaddrinfo failed for host %s, service %s (%s)", listen_host, + service, gai_strerror(ret)); + ret = -1; + goto out; + } + /* IPV6 server can handle both ipv4 and ipv6 clients */ + for (rp = res; rp != NULL; rp = rp->ai_next) { + if (rp->ai_addr == NULL) + continue; + if (rp->ai_family == AF_INET6) { + memcpy(addr, rp->ai_addr, rp->ai_addrlen); + *addr_len = rp->ai_addrlen; + } + } + + if (!(*addr_len)) { + if (res && res->ai_addr) { + memcpy(addr, res->ai_addr, res->ai_addrlen); + *addr_len = res->ai_addrlen; + } else { + ret = -1; + } + } + + freeaddrinfo(res); + +out: + return ret; +} + +int32_t +client_bind(rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t *sockaddr_len, int sock) +{ + int ret = 0; + + *sockaddr_len = sizeof(struct sockaddr_in6); + switch (sockaddr->sa_family) { + case AF_INET_SDP: + case AF_INET: + *sockaddr_len = sizeof(struct sockaddr_in); + /* Fall through */ + case AF_INET6: + if (!this->bind_insecure) { + ret = af_inet_bind_to_port_lt_ceiling( + sock, sockaddr, *sockaddr_len, GF_CLIENT_PORT_CEILING); + if (ret == -1) { + gf_log(this->name, GF_LOG_DEBUG, + "cannot bind inet socket (%d) " + "to port less than %d (%s)", + sock, GF_CLIENT_PORT_CEILING, strerror(errno)); + ret = 0; + } + } else { + ret = af_inet_bind_to_port_lt_ceiling( + sock, sockaddr, *sockaddr_len, GF_IANA_PRIV_PORTS_START); + if (ret == -1) { + gf_log(this->name, GF_LOG_DEBUG, + "failed while binding to less than " + "%d (%s)", + GF_IANA_PRIV_PORTS_START, strerror(errno)); + ret = 0; + } + } + break; + + case AF_UNIX: + *sockaddr_len = sizeof(struct sockaddr_un); + ret = af_unix_client_bind(this, (struct sockaddr *)sockaddr, + *sockaddr_len, sock); + break; + + default: + gf_log(this->name, GF_LOG_ERROR, "unknown address family %d", + sockaddr->sa_family); + ret = -1; + break; + } + + return ret; +} + +int32_t +socket_client_get_remote_sockaddr(rpc_transport_t *this, + struct sockaddr *sockaddr, + socklen_t *sockaddr_len, + sa_family_t *sa_family) +{ + int32_t ret = 0; + + GF_VALIDATE_OR_GOTO("socket", sockaddr, err); + GF_VALIDATE_OR_GOTO("socket", sockaddr_len, err); + GF_VALIDATE_OR_GOTO("socket", sa_family, err); + + ret = client_fill_address_family(this, &sockaddr->sa_family); + if (ret) { + ret = -1; + goto err; + } + + *sa_family = sockaddr->sa_family; + + switch (sockaddr->sa_family) { + case AF_INET_SDP: + sockaddr->sa_family = AF_INET; + /* Fall through */ + case AF_INET: + case AF_INET6: + case AF_UNSPEC: + ret = af_inet_client_get_remote_sockaddr(this, sockaddr, + sockaddr_len); + break; + + case AF_UNIX: + ret = af_unix_client_get_remote_sockaddr(this, sockaddr, + sockaddr_len); + break; + + default: + gf_log(this->name, GF_LOG_ERROR, "unknown address-family %d", + sockaddr->sa_family); + ret = -1; + } + + /* Address-family is updated based on remote_host in + af_inet_client_get_remote_sockaddr + */ + if (*sa_family != sockaddr->sa_family) { + *sa_family = sockaddr->sa_family; + } + +err: + return ret; +} + +static int32_t +server_fill_address_family(rpc_transport_t *this, sa_family_t *sa_family) +{ + data_t *address_family_data = NULL; + int32_t ret = -1; + +#ifdef IPV6_DEFAULT + const char *addr_family = "inet6"; + sa_family_t default_family = AF_INET6; +#else + const char *addr_family = "inet"; + sa_family_t default_family = AF_INET; +#endif + + GF_VALIDATE_OR_GOTO("socket", sa_family, out); + + address_family_data = dict_get_sizen(this->options, + "transport.address-family"); + if (address_family_data) { + char *address_family = NULL; + address_family = data_to_str(address_family_data); + + if (!strcasecmp(address_family, "inet")) { + *sa_family = AF_INET; + } else if (!strcasecmp(address_family, "inet6")) { + *sa_family = AF_INET6; + } else if (!strcasecmp(address_family, "inet-sdp")) { + *sa_family = AF_INET_SDP; + } else if (!strcasecmp(address_family, "unix")) { + *sa_family = AF_UNIX; + } else { + gf_log(this->name, GF_LOG_ERROR, + "unknown address family (%s) specified", address_family); + *sa_family = AF_UNSPEC; + goto out; + } + } else { + gf_log(this->name, GF_LOG_DEBUG, + "option address-family not specified, " + "defaulting to %s", + addr_family); + *sa_family = default_family; + } + + ret = 0; +out: + return ret; +} + +int32_t +socket_server_get_local_sockaddr(rpc_transport_t *this, struct sockaddr *addr, + socklen_t *addr_len, sa_family_t *sa_family) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("socket", sa_family, err); + GF_VALIDATE_OR_GOTO("socket", addr, err); + GF_VALIDATE_OR_GOTO("socket", addr_len, err); + + ret = server_fill_address_family(this, &addr->sa_family); + if (ret == -1) { + goto err; + } + + *sa_family = addr->sa_family; + + switch (addr->sa_family) { + case AF_INET_SDP: + addr->sa_family = AF_INET; + /* Fall through */ + case AF_INET: + case AF_INET6: + case AF_UNSPEC: + ret = af_inet_server_get_local_sockaddr(this, addr, addr_len); + break; + + case AF_UNIX: + ret = af_unix_server_get_local_sockaddr(this, addr, addr_len); + break; + } + + if (*sa_family == AF_UNSPEC) { + *sa_family = addr->sa_family; + } + +err: + return ret; +} + +static int32_t +fill_inet6_inet_identifiers(rpc_transport_t *this, + struct sockaddr_storage *addr, int32_t addr_len, + char *identifier) +{ + union gf_sock_union sock_union; + + char service[NI_MAXSERV] = { + 0, + }; + char host[NI_MAXHOST] = { + 0, + }; + int32_t ret = 0; + int32_t tmpaddr_len = 0; + int32_t one_to_four = 0; + int32_t four_to_eight = 0; + int32_t twelve_to_sixteen = 0; + int16_t eight_to_ten = 0; + int16_t ten_to_twelve = 0; + + memset(&sock_union, 0, sizeof(sock_union)); + sock_union.storage = *addr; + tmpaddr_len = addr_len; + + if (sock_union.sa.sa_family == AF_INET6) { + one_to_four = sock_union.sin6.sin6_addr.s6_addr32[0]; + four_to_eight = sock_union.sin6.sin6_addr.s6_addr32[1]; +#ifdef GF_SOLARIS_HOST_OS + eight_to_ten = S6_ADDR16(sock_union.sin6.sin6_addr)[4]; +#else + eight_to_ten = sock_union.sin6.sin6_addr.s6_addr16[4]; +#endif + +#ifdef GF_SOLARIS_HOST_OS + ten_to_twelve = S6_ADDR16(sock_union.sin6.sin6_addr)[5]; +#else + ten_to_twelve = sock_union.sin6.sin6_addr.s6_addr16[5]; +#endif + + twelve_to_sixteen = sock_union.sin6.sin6_addr.s6_addr32[3]; + + /* ipv4 mapped ipv6 address has + bits 0-80: 0 + bits 80-96: 0xffff + bits 96-128: ipv4 address + */ + + if (one_to_four == 0 && four_to_eight == 0 && eight_to_ten == 0 && + ten_to_twelve == -1) { + struct sockaddr_in *in_ptr = &sock_union.sin; + memset(&sock_union, 0, sizeof(sock_union)); + + in_ptr->sin_family = AF_INET; + in_ptr->sin_port = ((struct sockaddr_in6 *)addr)->sin6_port; + in_ptr->sin_addr.s_addr = twelve_to_sixteen; + tmpaddr_len = sizeof(*in_ptr); + } + } + + ret = getnameinfo(&sock_union.sa, tmpaddr_len, host, sizeof(host), service, + sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, "getnameinfo failed (%s)", + gai_strerror(ret)); + } + + sprintf(identifier, "%s:%s", host, service); + + return ret; +} + +int32_t +get_transport_identifiers(rpc_transport_t *this) +{ + int32_t ret = 0; + char is_inet_sdp = 0; + + switch (((struct sockaddr *)&this->myinfo.sockaddr)->sa_family) { + case AF_INET_SDP: + is_inet_sdp = 1; + ((struct sockaddr *)&this->peerinfo.sockaddr) + ->sa_family = ((struct sockaddr *)&this->myinfo.sockaddr) + ->sa_family = AF_INET; + /* Fall through */ + case AF_INET: + case AF_INET6: { + ret = fill_inet6_inet_identifiers(this, &this->myinfo.sockaddr, + this->myinfo.sockaddr_len, + this->myinfo.identifier); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, + "cannot fill inet/inet6 identifier for server"); + goto err; + } + + ret = fill_inet6_inet_identifiers(this, &this->peerinfo.sockaddr, + this->peerinfo.sockaddr_len, + this->peerinfo.identifier); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, + "cannot fill inet/inet6 identifier for client"); + goto err; + } + + if (is_inet_sdp) { + ((struct sockaddr *)&this->peerinfo.sockaddr) + ->sa_family = ((struct sockaddr *)&this->myinfo.sockaddr) + ->sa_family = AF_INET_SDP; + } + } break; + + case AF_UNIX: { + struct sockaddr_un *sunaddr = NULL; + + sunaddr = (struct sockaddr_un *)&this->myinfo.sockaddr; + strcpy(this->myinfo.identifier, sunaddr->sun_path); + + sunaddr = (struct sockaddr_un *)&this->peerinfo.sockaddr; + strcpy(this->peerinfo.identifier, sunaddr->sun_path); + } break; + + default: + gf_log(this->name, GF_LOG_ERROR, "unknown address family (%d)", + ((struct sockaddr *)&this->myinfo.sockaddr)->sa_family); + ret = -1; + break; + } + +err: + return ret; +} diff --git a/rpc/rpc-transport/socket/src/name.h b/rpc/rpc-transport/socket/src/name.h new file mode 100644 index 00000000000..080c7588f5a --- /dev/null +++ b/rpc/rpc-transport/socket/src/name.h @@ -0,0 +1,33 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _SOCKET_NAME_H +#define _SOCKET_NAME_H + +#include <glusterfs/compat.h> + +int32_t +client_bind(rpc_transport_t *this, struct sockaddr *sockaddr, + socklen_t *sockaddr_len, int sock); + +int32_t +socket_client_get_remote_sockaddr(rpc_transport_t *this, + struct sockaddr *sockaddr, + socklen_t *sockaddr_len, + sa_family_t *sa_family); + +int32_t +socket_server_get_local_sockaddr(rpc_transport_t *this, struct sockaddr *addr, + socklen_t *addr_len, sa_family_t *sa_family); + +int32_t +get_transport_identifiers(rpc_transport_t *this); + +#endif /* _SOCKET_NAME_H */ diff --git a/rpc/rpc-transport/socket/src/socket-mem-types.h b/rpc/rpc-transport/socket/src/socket-mem-types.h new file mode 100644 index 00000000000..241ce67f670 --- /dev/null +++ b/rpc/rpc-transport/socket/src/socket-mem-types.h @@ -0,0 +1,22 @@ +/* + Copyright (c) 2008-2014 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef __SOCKET_MEM_TYPES_H__ +#define __SOCKET_MEM_TYPES_H__ + +#include <glusterfs/mem-types.h> + +typedef enum gf_sock_mem_types_ { + gf_sock_connect_error_state_t = gf_common_mt_end + 1, + gf_sock_mt_lock_array, + gf_sock_mt_end +} gf_sock_mem_types_t; + +#endif diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c new file mode 100644 index 00000000000..ed8b473be23 --- /dev/null +++ b/rpc/rpc-transport/socket/src/socket.c @@ -0,0 +1,4801 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "socket.h" +#include "name.h" +#include <glusterfs/dict.h> +#include <glusterfs/syscall.h> +#include <glusterfs/byte-order.h> +#include <glusterfs/compat-errno.h> +#include "socket-mem-types.h" + +/* ugly #includes below */ +#include "protocol-common.h" +#include "glusterfs3-xdr.h" +#include "glusterfs4-xdr.h" +#include "rpcsvc.h" + +/* for TCP_USER_TIMEOUT */ +#if !defined(TCP_USER_TIMEOUT) && defined(GF_LINUX_HOST_OS) +#include <linux/tcp.h> +#else +#include <netinet/tcp.h> +#endif + +#include <errno.h> +#include <rpc/xdr.h> +#include <sys/ioctl.h> +#define GF_LOG_ERRNO(errno) ((errno == ENOTCONN) ? GF_LOG_DEBUG : GF_LOG_ERROR) +#define SA(ptr) ((struct sockaddr *)ptr) + +#define SSL_ENABLED_OPT "transport.socket.ssl-enabled" +#define SSL_OWN_CERT_OPT "transport.socket.ssl-own-cert" +#define SSL_PRIVATE_KEY_OPT "transport.socket.ssl-private-key" +#define SSL_CA_LIST_OPT "transport.socket.ssl-ca-list" +#define SSL_CERT_DEPTH_OPT "transport.socket.ssl-cert-depth" +#define SSL_CIPHER_LIST_OPT "transport.socket.ssl-cipher-list" +#define SSL_DH_PARAM_OPT "transport.socket.ssl-dh-param" +#define SSL_EC_CURVE_OPT "transport.socket.ssl-ec-curve" +#define SSL_CRL_PATH_OPT "transport.socket.ssl-crl-path" +#define OWN_THREAD_OPT "transport.socket.own-thread" + +/* TBD: do automake substitutions etc. (ick) to set these. */ +#if !defined(DEFAULT_ETC_SSL) +#ifdef GF_LINUX_HOST_OS +#define DEFAULT_ETC_SSL "/etc/ssl" +#endif +#ifdef GF_BSD_HOST_OS +#define DEFAULT_ETC_SSL "/etc/openssl" +#endif +#ifdef GF_DARWIN_HOST_OS +#define DEFAULT_ETC_SSL "/usr/local/etc/openssl" +#endif +#if !defined(DEFAULT_ETC_SSL) +#define DEFAULT_ETC_SSL "/etc/ssl" +#endif +#endif + +#if !defined(DEFAULT_CERT_PATH) +#define DEFAULT_CERT_PATH DEFAULT_ETC_SSL "/glusterfs.pem" +#endif +#if !defined(DEFAULT_KEY_PATH) +#define DEFAULT_KEY_PATH DEFAULT_ETC_SSL "/glusterfs.key" +#endif +#if !defined(DEFAULT_CA_PATH) +#define DEFAULT_CA_PATH DEFAULT_ETC_SSL "/glusterfs.ca" +#endif +#if !defined(DEFAULT_VERIFY_DEPTH) +#define DEFAULT_VERIFY_DEPTH 1 +#endif +#define DEFAULT_CIPHER_LIST "EECDH:EDH:HIGH:!3DES:!RC4:!DES:!MD5:!aNULL:!eNULL" +#define DEFAULT_DH_PARAM DEFAULT_ETC_SSL "/dhparam.pem" +#define DEFAULT_EC_CURVE "prime256v1" + +#define POLL_MASK_INPUT (POLLIN | POLLPRI) +#define POLL_MASK_OUTPUT (POLLOUT) +#define POLL_MASK_ERROR (POLLERR | POLLHUP | POLLNVAL) + +typedef int +SSL_unary_func(SSL *); +typedef int +SSL_trinary_func(SSL *, void *, int); +static int +ssl_setup_connection_params(rpc_transport_t *this); + +#define __socket_proto_reset_pending(priv) \ + do { \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + memset(&frag->vector, 0, sizeof(frag->vector)); \ + frag->pending_vector = &frag->vector; \ + frag->pending_vector->iov_base = frag->fragcurrent; \ + priv->incoming.pending_vector = frag->pending_vector; \ + } while (0) + +#define __socket_proto_update_pending(priv) \ + do { \ + uint32_t remaining; \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + if (frag->pending_vector->iov_len == 0) { \ + remaining = (RPC_FRAGSIZE(priv->incoming.fraghdr) - \ + frag->bytes_read); \ + \ + frag->pending_vector->iov_len = (remaining > frag->remaining_size) \ + ? frag->remaining_size \ + : remaining; \ + \ + frag->remaining_size -= frag->pending_vector->iov_len; \ + } \ + } while (0) + +#define __socket_proto_update_priv_after_read(priv, ret, bytes_read) \ + { \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + frag->fragcurrent += bytes_read; \ + frag->bytes_read += bytes_read; \ + \ + if ((ret > 0) || (frag->remaining_size != 0)) { \ + if (frag->remaining_size != 0 && ret == 0) { \ + __socket_proto_reset_pending(priv); \ + } \ + \ + gf_log(this->name, GF_LOG_TRACE, \ + "partial read on non-blocking socket"); \ + ret = 0; \ + break; \ + } \ + } + +#define __socket_proto_init_pending(priv, size) \ + do { \ + uint32_t remaining = 0; \ + struct gf_sock_incoming_frag *frag; \ + frag = &priv->incoming.frag; \ + \ + remaining = (RPC_FRAGSIZE(priv->incoming.fraghdr) - frag->bytes_read); \ + \ + __socket_proto_reset_pending(priv); \ + \ + frag->pending_vector->iov_len = (remaining > size) ? size : remaining; \ + \ + frag->remaining_size = (size - frag->pending_vector->iov_len); \ + \ + } while (0) + +/* This will be used in a switch case and breaks from the switch case if all + * the pending data is not read. + */ +#define __socket_proto_read(priv, ret) \ + { \ + size_t bytes_read = 0; \ + struct gf_sock_incoming *in; \ + in = &priv->incoming; \ + \ + __socket_proto_update_pending(priv); \ + \ + ret = __socket_readv(this, in->pending_vector, 1, &in->pending_vector, \ + &in->pending_count, &bytes_read); \ + if (ret < 0) \ + break; \ + __socket_proto_update_priv_after_read(priv, ret, bytes_read); \ + } + +struct socket_connect_error_state_ { + xlator_t *this; + rpc_transport_t *trans; + gf_boolean_t refd; +}; +typedef struct socket_connect_error_state_ socket_connect_error_state_t; + +static int +socket_init(rpc_transport_t *this); +static int +__socket_nonblock(int fd); + +static void +socket_dump_info(struct sockaddr *sa, int is_server, int is_ssl, int sock, + char *log_domain, char *log_label) +{ + char addr_buf[INET6_ADDRSTRLEN + 1] = { + 0, + }; + char *addr = NULL; + const char *peer_type = NULL; + int af = sa->sa_family; + int so_error = -1; + socklen_t slen = sizeof(so_error); + + if (af == AF_UNIX) { + addr = ((struct sockaddr_un *)(sa))->sun_path; + } else { + if (af == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(sa); + + inet_ntop(af, &sin6->sin6_addr, addr_buf, sizeof(addr_buf)); + addr = addr_buf; + } else { + struct sockaddr_in *sin = (struct sockaddr_in *)(sa); + + inet_ntop(af, &sin->sin_addr, addr_buf, sizeof(addr_buf)); + addr = addr_buf; + } + } + if (is_server) + peer_type = "server"; + else + peer_type = "client"; + + (void)getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &slen); + + gf_log(log_domain, GF_LOG_TRACE, + "$$$ %s: %s (af:%d,sock:%d) %s %s (errno:%d:%s)", peer_type, + log_label, af, sock, addr, (is_ssl ? "SSL" : "non-SSL"), so_error, + strerror(so_error)); +} + +static void +ssl_dump_error_stack(const char *caller) +{ + unsigned long errnum = 0; + char errbuf[120] = { + 0, + }; + + /* OpenSSL docs explicitly give 120 as the error-string length. */ + + while ((errnum = ERR_get_error())) { + ERR_error_string(errnum, errbuf); + gf_log(caller, GF_LOG_ERROR, " %s", errbuf); + } +} + +static int +ssl_do(rpc_transport_t *this, void *buf, size_t len, SSL_trinary_func *func) +{ + int r = (-1); + socket_private_t *priv = NULL; + + priv = this->private; + + if (buf) { + if (priv->connected == -1) { + /* + * Fields in the SSL structure (especially + * the BIO pointers) are not valid at this + * point, so we'll segfault if we pass them + * to SSL_read/SSL_write. + */ + gf_log(this->name, GF_LOG_INFO, "lost connection in %s", __func__); + return -1; + } + r = func(priv->ssl_ssl, buf, len); + } else { + /* This should be treated as error */ + gf_log(this->name, GF_LOG_ERROR, "buffer is empty %s", __func__); + goto out; + } + switch (SSL_get_error(priv->ssl_ssl, r)) { + case SSL_ERROR_NONE: + /* fall through */ + case SSL_ERROR_WANT_READ: + /* fall through */ + case SSL_ERROR_WANT_WRITE: + errno = EAGAIN; + return r; + + case SSL_ERROR_SYSCALL: + /* Sometimes SSL_ERROR_SYSCALL returns errno as + * EAGAIN. In such a case we should reattempt operation + * So, for now, just return the return value and the + * errno as is. + */ + gf_log(this->name, GF_LOG_DEBUG, + "syscall error (probably remote disconnect) " + "errno:%d:%s", + errno, strerror(errno)); + return r; + default: + errno = EIO; + goto out; /* "break" would just loop again */ + } +out: + return -1; +} + +#define ssl_read_one(t, b, l) \ + ssl_do((t), (b), (l), (SSL_trinary_func *)SSL_read) +#define ssl_write_one(t, b, l) \ + ssl_do((t), (b), (l), (SSL_trinary_func *)SSL_write) + +/* set crl verify flags only for server */ +/* see man X509_VERIFY_PARAM_SET_FLAGS(3) + * X509_V_FLAG_CRL_CHECK enables CRL checking for the certificate chain + * leaf certificate. An error occurs if a suitable CRL cannot be found. + * Since we're never going to revoke a gluster node cert, we better disable + * CRL check for server certs to avoid getting error and failed connection + * attempts. + */ +static void +ssl_clear_crl_verify_flags(SSL_CTX *ssl_ctx) +{ +#ifdef X509_V_FLAG_CRL_CHECK_ALL +#ifdef HAVE_SSL_CTX_GET0_PARAM + X509_VERIFY_PARAM *vpm; + + vpm = SSL_CTX_get0_param(ssl_ctx); + if (vpm) { + X509_VERIFY_PARAM_clear_flags( + vpm, (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)); + } +#else + /* CRL verify flag need not be cleared for rhel6 kind of clients */ +#endif +#else + gf_log(this->name, GF_LOG_ERROR, "OpenSSL version does not support CRL"); +#endif + return; +} + +/* set crl verify flags only for server */ +static void +ssl_set_crl_verify_flags(SSL_CTX *ssl_ctx) +{ +#ifdef X509_V_FLAG_CRL_CHECK_ALL +#ifdef HAVE_SSL_CTX_GET0_PARAM + X509_VERIFY_PARAM *vpm; + + vpm = SSL_CTX_get0_param(ssl_ctx); + if (vpm) { + unsigned long flags; + + flags = X509_VERIFY_PARAM_get_flags(vpm); + flags |= (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); + X509_VERIFY_PARAM_set_flags(vpm, flags); + } +#else + X509_STORE *x509store; + + x509store = SSL_CTX_get_cert_store(ssl_ctx); + X509_STORE_set_flags(x509store, + X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); +#endif +#else + gf_log(this->name, GF_LOG_ERROR, "OpenSSL version does not support CRL"); +#endif +} + +static int +ssl_setup_connection_prefix(rpc_transport_t *this, gf_boolean_t server) +{ + int ret = -1; + socket_private_t *priv = NULL; + + priv = this->private; + + if (ssl_setup_connection_params(this) < 0) { + gf_log(this->name, GF_LOG_TRACE, + "+ ssl_setup_connection_params() failed!"); + goto done; + } else { + gf_log(this->name, GF_LOG_TRACE, + "+ ssl_setup_connection_params() done!"); + } + + priv->ssl_error_required = SSL_ERROR_NONE; + priv->ssl_connected = _gf_false; + priv->ssl_accepted = _gf_false; + priv->ssl_context_created = _gf_false; + + if (!server && priv->crl_path) + ssl_clear_crl_verify_flags(priv->ssl_ctx); + + priv->ssl_ssl = SSL_new(priv->ssl_ctx); + if (!priv->ssl_ssl) { + gf_log(this->name, GF_LOG_ERROR, "SSL_new failed"); + ssl_dump_error_stack(this->name); + goto done; + } + + priv->ssl_sbio = BIO_new_socket(priv->sock, BIO_NOCLOSE); + if (!priv->ssl_sbio) { + gf_log(this->name, GF_LOG_ERROR, "BIO_new_socket failed"); + ssl_dump_error_stack(this->name); + goto free_ssl; + } + + SSL_set_bio(priv->ssl_ssl, priv->ssl_sbio, priv->ssl_sbio); + ret = 0; + goto done; + +free_ssl: + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; +done: + return ret; +} + +static char * +ssl_setup_connection_postfix(rpc_transport_t *this) +{ + X509 *peer = NULL; + char peer_CN[256] = ""; + socket_private_t *priv = NULL; + + priv = this->private; + + /* Make sure _SSL verification_ succeeded, yielding an identity. */ + if (SSL_get_verify_result(priv->ssl_ssl) != X509_V_OK) { + goto ssl_error; + } + peer = SSL_get_peer_certificate(priv->ssl_ssl); + if (!peer) { + goto ssl_error; + } + + SSL_set_mode(priv->ssl_ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); + + /* Finally, everything seems OK. */ + X509_NAME_get_text_by_NID(X509_get_subject_name(peer), NID_commonName, + peer_CN, sizeof(peer_CN) - 1); + peer_CN[sizeof(peer_CN) - 1] = '\0'; + gf_log(this->name, GF_LOG_DEBUG, "peer CN = %s", peer_CN); + gf_log(this->name, GF_LOG_DEBUG, + "SSL verification succeeded (client: %s) (server: %s)", + this->peerinfo.identifier, this->myinfo.identifier); + X509_free(peer); + return gf_strdup(peer_CN); + + /* Error paths. */ +ssl_error: + gf_log(this->name, GF_LOG_ERROR, + "SSL connect error (client: %s) (server: %s)", + this->peerinfo.identifier, this->myinfo.identifier); + ssl_dump_error_stack(this->name); + + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; + return NULL; +} + +static int +ssl_complete_connection(rpc_transport_t *this) +{ + int ret = -1; /* 1 : implies go back to epoll_wait() + * 0 : implies successful ssl connection + * -1: implies continue processing current event + * as if EPOLLERR has been encountered + */ + char *cname = NULL; + int r = -1; + int ssl_error = -1; + socket_private_t *priv = NULL; + + priv = this->private; + + if (priv->is_server) { + r = SSL_accept(priv->ssl_ssl); + } else { + r = SSL_connect(priv->ssl_ssl); + } + + ssl_error = SSL_get_error(priv->ssl_ssl, r); + priv->ssl_error_required = ssl_error; + + switch (ssl_error) { + case SSL_ERROR_NONE: + cname = ssl_setup_connection_postfix(this); + if (!cname) { + /* we've failed to get the cname so + * we must close the connection + * + * treat this as EPOLLERR + */ + gf_log(this->name, GF_LOG_TRACE, "error getting cname"); + errno = ECONNRESET; + ret = -1; + } else { + this->ssl_name = cname; + if (priv->is_server) { + priv->ssl_accepted = _gf_true; + gf_log(this->name, GF_LOG_TRACE, "ssl_accepted!"); + } else { + priv->ssl_connected = _gf_true; + gf_log(this->name, GF_LOG_TRACE, "ssl_connected!"); + } + ret = 0; + } + break; + + case SSL_ERROR_WANT_READ: + /* fall through */ + case SSL_ERROR_WANT_WRITE: + errno = EAGAIN; + break; + + case SSL_ERROR_SYSCALL: + /* Sometimes SSL_ERROR_SYSCALL returns with errno as EAGAIN + * So, we should retry the operation. + * So, for now, we just return the return value and errno as is. + */ + break; + + case SSL_ERROR_SSL: + /* treat this as EPOLLERR */ + ret = -1; + break; + + default: + /* treat this as EPOLLERR */ + errno = EIO; + ret = -1; + break; + } + return ret; +} + +static void +ssl_teardown_connection(socket_private_t *priv) +{ + if (priv->ssl_ssl) { + SSL_shutdown(priv->ssl_ssl); + SSL_clear(priv->ssl_ssl); + SSL_free(priv->ssl_ssl); + SSL_CTX_free(priv->ssl_ctx); + priv->ssl_ssl = NULL; + priv->ssl_ctx = NULL; + if (priv->ssl_private_key) { + GF_FREE(priv->ssl_private_key); + priv->ssl_private_key = NULL; + } + if (priv->ssl_own_cert) { + GF_FREE(priv->ssl_own_cert); + priv->ssl_own_cert = NULL; + } + if (priv->ssl_ca_list) { + GF_FREE(priv->ssl_ca_list); + priv->ssl_ca_list = NULL; + } + } + priv->use_ssl = _gf_false; +} + +static ssize_t +__socket_ssl_readv(rpc_transport_t *this, struct iovec *opvector, int opcount) +{ + socket_private_t *priv = NULL; + int sock = -1; + int ret = -1; + + priv = this->private; + sock = priv->sock; + + if (priv->use_ssl) { + gf_log(this->name, GF_LOG_TRACE, "***** reading over SSL"); + ret = ssl_read_one(this, opvector->iov_base, opvector->iov_len); + } else { + gf_log(this->name, GF_LOG_TRACE, "***** reading over non-SSL"); + ret = sys_readv(sock, opvector, IOV_MIN(opcount)); + } + + return ret; +} + +static ssize_t +__socket_ssl_read(rpc_transport_t *this, void *buf, size_t count) +{ + struct iovec iov = { + 0, + }; + int ret = -1; + + iov.iov_base = buf; + iov.iov_len = count; + + ret = __socket_ssl_readv(this, &iov, 1); + + return ret; +} + +static int +__socket_cached_read(rpc_transport_t *this, struct iovec *opvector, int opcount) +{ + socket_private_t *priv = NULL; + struct gf_sock_incoming *in = NULL; + int req_len = -1; + int ret = -1; + + priv = this->private; + in = &priv->incoming; + req_len = iov_length(opvector, opcount); + + if (in->record_state == SP_STATE_READING_FRAGHDR) { + in->ra_read = 0; + in->ra_served = 0; + in->ra_max = 0; + in->ra_buf = NULL; + goto uncached; + } + + if (!in->ra_max) { + /* first call after passing SP_STATE_READING_FRAGHDR */ + in->ra_max = min(RPC_FRAGSIZE(in->fraghdr), GF_SOCKET_RA_MAX); + /* Note that the in->iobuf is the primary iobuf into which + headers are read into, and in->frag.fragcurrent points to + some position in the buffer. By using this itself as our + read-ahead cache, we can avoid memory copies in iov_load + */ + in->ra_buf = in->frag.fragcurrent; + } + + /* fill read-ahead */ + if (in->ra_read < in->ra_max) { + ret = __socket_ssl_read(this, &in->ra_buf[in->ra_read], + (in->ra_max - in->ra_read)); + if (ret > 0) + in->ra_read += ret; + + /* we proceed to test if there is still cached data to + be served even if readahead could not progress */ + } + + /* serve cached */ + if (in->ra_served < in->ra_read) { + ret = iov_load(opvector, opcount, &in->ra_buf[in->ra_served], + min(req_len, (in->ra_read - in->ra_served))); + + in->ra_served += ret; + /* Do not read uncached and cached in the same call */ + goto out; + } + + if (in->ra_read < in->ra_max) + /* If there was no cached data to be served, (and we are + guaranteed to have already performed an attempt to progress + readahead above), and we have not yet read out the full + readahead capacity, then bail out for now without doing + the uncached read below (as that will overtake future cached + read) + */ + goto out; +uncached: + ret = __socket_ssl_readv(this, opvector, opcount); +out: + return ret; +} + +static gf_boolean_t +__does_socket_rwv_error_need_logging(socket_private_t *priv, int write) +{ + int read = !write; + + if (priv->connected == -1) /* Didn't even connect, of course it fails */ + return _gf_false; + + if (read && (priv->read_fail_log == _gf_false)) + return _gf_false; + + return _gf_true; +} + +/* + * return value: + * 0 = success (completed) + * -1 = error + * > 0 = incomplete + */ + +static int +__socket_rwv(rpc_transport_t *this, struct iovec *vector, int count, + struct iovec **pending_vector, int *pending_count, size_t *bytes, + int write) +{ + socket_private_t *priv = NULL; + int sock = -1; + int ret = -1; + struct iovec *opvector = NULL; + int opcount = 0; + int moved = 0; + + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + sock = priv->sock; + + opvector = vector; + opcount = count; + + if (bytes != NULL) { + *bytes = 0; + } + + while (opcount > 0) { + if (opvector->iov_len == 0) { + gf_log(this->name, GF_LOG_DEBUG, + "would have passed zero length to read/write"); + ++opvector; + --opcount; + continue; + } + if (priv->use_ssl && !priv->ssl_ssl) { + /* + * We could end up here with priv->ssl_ssl still NULL + * if (a) the connection failed and (b) some fool + * called other socket functions anyway. Demoting to + * non-SSL might be insecure, so just fail it outright. + */ + ret = -1; + gf_log(this->name, GF_LOG_TRACE, + "### no priv->ssl_ssl yet; ret = -1;"); + } else if (write) { + if (priv->use_ssl) { + ret = ssl_write_one(this, opvector->iov_base, + opvector->iov_len); + } else { + ret = sys_writev(sock, opvector, IOV_MIN(opcount)); + } + + if ((ret == 0) || ((ret < 0) && (errno == EAGAIN))) { + /* done for now */ + break; + } else if (ret > 0) + this->total_bytes_write += ret; + } else { + ret = __socket_cached_read(this, opvector, opcount); + if (ret == 0) { + gf_log(this->name, GF_LOG_DEBUG, + "EOF on socket %d (errno:%d:%s); returning ENODATA", + sock, errno, strerror(errno)); + + errno = ENODATA; + ret = -1; + } + if ((ret < 0) && (errno == EAGAIN)) { + /* done for now */ + break; + } else if (ret > 0) + this->total_bytes_read += ret; + } + + if (ret == 0) { + /* Mostly due to 'umount' in client */ + + gf_log(this->name, GF_LOG_DEBUG, "EOF from peer %s", + this->peerinfo.identifier); + opcount = -1; + errno = ENOTCONN; + break; + } + if (ret < 0) { + if (errno == EINTR) + continue; + + if (__does_socket_rwv_error_need_logging(priv, write)) { + GF_LOG_OCCASIONALLY(priv->log_ctr, this->name, GF_LOG_WARNING, + "%s on %s failed (%s)", + write ? "writev" : "readv", + this->peerinfo.identifier, strerror(errno)); + } + + if (priv->use_ssl && priv->ssl_ssl) { + ssl_dump_error_stack(this->name); + } + opcount = -1; + break; + } + + if (bytes != NULL) { + *bytes += ret; + } + + moved = 0; + + while (moved < ret) { + if (!opcount) { + gf_log(this->name, GF_LOG_DEBUG, "ran out of iov, moved %d/%d", + moved, ret); + goto ran_out; + } + if (!opvector[0].iov_len) { + opvector++; + opcount--; + continue; + } + if ((ret - moved) >= opvector[0].iov_len) { + moved += opvector[0].iov_len; + opvector++; + opcount--; + } else { + opvector[0].iov_len -= (ret - moved); + opvector[0].iov_base += (ret - moved); + moved += (ret - moved); + } + } + } + +ran_out: + + if (pending_vector) + *pending_vector = opvector; + + if (pending_count) + *pending_count = opcount; + +out: + return opcount; +} + +static int +__socket_readv(rpc_transport_t *this, struct iovec *vector, int count, + struct iovec **pending_vector, int *pending_count, size_t *bytes) +{ + return __socket_rwv(this, vector, count, pending_vector, pending_count, + bytes, 0); +} + +static int +__socket_writev(rpc_transport_t *this, struct iovec *vector, int count, + struct iovec **pending_vector, int *pending_count) +{ + return __socket_rwv(this, vector, count, pending_vector, pending_count, + NULL, 1); +} + +static int +__socket_shutdown(rpc_transport_t *this) +{ + int ret = -1; + socket_private_t *priv = this->private; + + priv->connected = -1; + ret = shutdown(priv->sock, SHUT_RDWR); + if (ret) { + /* its already disconnected.. no need to understand + why it failed to shutdown in normal cases */ + gf_log(this->name, GF_LOG_DEBUG, "shutdown() returned %d. %s", ret, + strerror(errno)); + } else { + GF_LOG_OCCASIONALLY(priv->shutdown_log_ctr, this->name, GF_LOG_INFO, + "intentional socket shutdown(%d)", priv->sock); + } + + return ret; +} + +static int +__socket_teardown_connection(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + + priv = this->private; + + if (priv->use_ssl) + ssl_teardown_connection(priv); + + return __socket_shutdown(this); +} + +static int +__socket_disconnect(rpc_transport_t *this) +{ + int ret = -1; + socket_private_t *priv = NULL; + + priv = this->private; + + gf_log(this->name, GF_LOG_TRACE, "disconnecting %p, sock=%d", this, + priv->sock); + + if (priv->sock >= 0) { + gf_log_callingfn(this->name, GF_LOG_TRACE, + "tearing down socket connection"); + ret = __socket_teardown_connection(this); + if (ret) { + gf_log(this->name, GF_LOG_DEBUG, + "__socket_teardown_connection () failed: %s", + strerror(errno)); + } + } + + return ret; +} + +static int +__socket_server_bind(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + cmd_args_t *cmd_args = NULL; + struct sockaddr_storage unix_addr = {0}; + int ret = -1; + int opt = 1; + int reuse_check_sock = -1; + uint16_t sin_port = 0; + int retries = 0; + + priv = this->private; + ctx = this->ctx; + cmd_args = &ctx->cmd_args; + + ret = setsockopt(priv->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setsockopt() for SO_REUSEADDR failed (%s)", strerror(errno)); + } + + /* reuse-address doesn't work for unix type sockets */ + if (AF_UNIX == SA(&this->myinfo.sockaddr)->sa_family) { + memcpy(&unix_addr, SA(&this->myinfo.sockaddr), + this->myinfo.sockaddr_len); + reuse_check_sock = sys_socket(AF_UNIX, SOCK_STREAM, 0); + if (reuse_check_sock >= 0) { + ret = connect(reuse_check_sock, SA(&unix_addr), + this->myinfo.sockaddr_len); + if ((ret != 0) && (ECONNREFUSED == errno)) { + sys_unlink(((struct sockaddr_un *)&unix_addr)->sun_path); + } + gf_log(this->name, GF_LOG_INFO, + "closing (AF_UNIX) reuse check socket %d", reuse_check_sock); + sys_close(reuse_check_sock); + } + } + + if (AF_UNIX != SA(&this->myinfo.sockaddr)->sa_family) { + sin_port = (int)ntohs( + ((struct sockaddr_in *)&this->myinfo.sockaddr)->sin_port); + if (!sin_port) { + sin_port = GF_DEFAULT_SOCKET_LISTEN_PORT; + ((struct sockaddr_in *)&this->myinfo.sockaddr)->sin_port = htons( + sin_port); + } + retries = 10; + while (retries) { + ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, + this->myinfo.sockaddr_len); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s", + this->myinfo.identifier, strerror(errno)); + if (errno == EADDRINUSE) { + gf_log(this->name, GF_LOG_ERROR, "Port is already in use"); + sleep(1); + retries--; + } else { + break; + } + } else { + break; + } + } + } else { + ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, + this->myinfo.sockaddr_len); + + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s", + this->myinfo.identifier, strerror(errno)); + if (errno == EADDRINUSE) { + gf_log(this->name, GF_LOG_ERROR, "Port is already in use"); + } + } + } + if (AF_UNIX != SA(&this->myinfo.sockaddr)->sa_family) { + if (getsockname(priv->sock, SA(&this->myinfo.sockaddr), + &this->myinfo.sockaddr_len) != 0) { + gf_log(this->name, GF_LOG_WARNING, + "getsockname on (%d) failed (%s)", priv->sock, + strerror(errno)); + ret = -1; + goto out; + } + if (!cmd_args->brick_port) { + cmd_args->brick_port = (int)ntohs( + ((struct sockaddr_in *)&this->myinfo.sockaddr)->sin_port); + gf_log(this->name, GF_LOG_INFO, + "process started listening on port (%d)", + cmd_args->brick_port); + } + } + +out: + return ret; +} + +static int +__socket_nonblock(int fd) +{ + int flags = 0; + int ret = -1; + + flags = fcntl(fd, F_GETFL); + + if (flags >= 0) + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); + + return ret; +} + +static int +__socket_nodelay(int fd) +{ + int on = 1; + int ret = -1; + + ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); + if (!ret) + gf_log(THIS->name, GF_LOG_TRACE, "NODELAY enabled for socket %d", fd); + + return ret; +} + +static int +__socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle, + int keepalivecnt, int timeout) +{ + int on = 1; + int ret = -1; +#if defined(TCP_USER_TIMEOUT) + int timeout_ms = timeout * 1000; +#endif + + ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set keep alive option on socket %d", fd); + goto err; + } + + if (keepaliveintvl == GF_USE_DEFAULT_KEEPALIVE) + goto done; + +#if !defined(GF_LINUX_HOST_OS) && !defined(__NetBSD__) +#if defined(GF_SOLARIS_HOST_OS) || defined(__FreeBSD__) + ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepaliveintvl, + sizeof(keepaliveintvl)); +#else + ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &keepaliveintvl, + sizeof(keepaliveintvl)); +#endif + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set keep alive interval on socket %d", fd); + goto err; + } +#else + if (family != AF_INET && family != AF_INET6) + goto done; + + ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepaliveidle, + sizeof(keepaliveidle)); + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set keep idle %d on socket %d, %s", keepaliveidle, fd, + strerror(errno)); + goto err; + } + ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepaliveintvl, + sizeof(keepaliveintvl)); + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set keep interval %d on socket %d, %s", + keepaliveintvl, fd, strerror(errno)); + goto err; + } + +#if defined(TCP_USER_TIMEOUT) + if (timeout_ms < 0) + goto done; + ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout_ms, + sizeof(timeout_ms)); + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set " + "TCP_USER_TIMEOUT %d on socket %d, %s", + timeout_ms, fd, strerror(errno)); + goto err; + } +#endif +#if defined(TCP_KEEPCNT) + ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepalivecnt, + sizeof(keepalivecnt)); + if (ret != 0) { + gf_log("socket", GF_LOG_WARNING, + "failed to set " + "TCP_KEEPCNT %d on socket %d, %s", + keepalivecnt, fd, strerror(errno)); + goto err; + } +#endif +#endif + +done: + gf_log(THIS->name, GF_LOG_TRACE, + "Keep-alive enabled for socket: %d, " + "(idle: %d, interval: %d, max-probes: %d, timeout: %d)", + fd, keepaliveidle, keepaliveintvl, keepalivecnt, timeout); + +err: + return ret; +} + +static int +__socket_connect_finish(int fd) +{ + int ret = -1; + int optval = 0; + socklen_t optlen = sizeof(int); + + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen); + + if (ret == 0 && optval) { + errno = optval; + ret = -1; + } + + return ret; +} + +static void +__socket_reset(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + + priv = this->private; + + /* TODO: use mem-pool on incoming data */ + + if (priv->incoming.iobref) { + iobref_unref(priv->incoming.iobref); + priv->incoming.iobref = NULL; + } + + if (priv->incoming.iobuf) { + iobuf_unref(priv->incoming.iobuf); + priv->incoming.iobuf = NULL; + } + + GF_FREE(priv->incoming.request_info); + + memset(&priv->incoming, 0, sizeof(priv->incoming)); + + gf_event_unregister_close(this->ctx->event_pool, priv->sock, priv->idx); + if (priv->use_ssl && priv->ssl_ssl) { + SSL_clear(priv->ssl_ssl); + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; + } + if (priv->ssl_ctx) { + SSL_CTX_free(priv->ssl_ctx); + priv->ssl_ctx = NULL; + } + priv->sock = -1; + priv->idx = -1; + priv->connected = -1; + priv->ssl_connected = _gf_false; + priv->ssl_accepted = _gf_false; + priv->ssl_context_created = _gf_false; + + if (priv->ssl_private_key) { + GF_FREE(priv->ssl_private_key); + priv->ssl_private_key = NULL; + } + if (priv->ssl_own_cert) { + GF_FREE(priv->ssl_own_cert); + priv->ssl_own_cert = NULL; + } + if (priv->ssl_ca_list) { + GF_FREE(priv->ssl_ca_list); + priv->ssl_ca_list = NULL; + } +} + +static void +socket_set_lastfrag(uint32_t *fragsize) +{ + (*fragsize) |= 0x80000000U; +} + +static void +socket_set_frag_header_size(uint32_t size, char *haddr) +{ + size = htonl(size); + memcpy(haddr, &size, sizeof(size)); +} + +static void +socket_set_last_frag_header_size(uint32_t size, char *haddr) +{ + socket_set_lastfrag(&size); + socket_set_frag_header_size(size, haddr); +} + +static struct ioq * +__socket_ioq_new(rpc_transport_t *this, rpc_transport_msg_t *msg) +{ + struct ioq *entry = NULL; + int count = 0; + uint32_t size = 0; + + /* TODO: use mem-pool */ + entry = GF_CALLOC(1, sizeof(*entry), gf_common_mt_ioq); + if (!entry) + return NULL; + + count = msg->rpchdrcount + msg->proghdrcount + msg->progpayloadcount; + + GF_ASSERT(count <= (MAX_IOVEC - 1)); + + size = iov_length(msg->rpchdr, msg->rpchdrcount) + + iov_length(msg->proghdr, msg->proghdrcount) + + iov_length(msg->progpayload, msg->progpayloadcount); + + if (size > RPC_MAX_FRAGMENT_SIZE) { + gf_log(this->name, GF_LOG_ERROR, + "msg size (%u) bigger than the maximum allowed size on " + "sockets (%u)", + size, RPC_MAX_FRAGMENT_SIZE); + GF_FREE(entry); + return NULL; + } + + socket_set_last_frag_header_size(size, (char *)&entry->fraghdr); + + entry->vector[0].iov_base = (char *)&entry->fraghdr; + entry->vector[0].iov_len = sizeof(entry->fraghdr); + entry->count = 1; + + if (msg->rpchdr != NULL) { + memcpy(&entry->vector[1], msg->rpchdr, + sizeof(struct iovec) * msg->rpchdrcount); + entry->count += msg->rpchdrcount; + } + + if (msg->proghdr != NULL) { + memcpy(&entry->vector[entry->count], msg->proghdr, + sizeof(struct iovec) * msg->proghdrcount); + entry->count += msg->proghdrcount; + } + + if (msg->progpayload != NULL) { + memcpy(&entry->vector[entry->count], msg->progpayload, + sizeof(struct iovec) * msg->progpayloadcount); + entry->count += msg->progpayloadcount; + } + + entry->pending_vector = entry->vector; + entry->pending_count = entry->count; + + if (msg->iobref != NULL) + entry->iobref = iobref_ref(msg->iobref); + + INIT_LIST_HEAD(&entry->list); + + return entry; +} + +static void +__socket_ioq_entry_free(struct ioq *entry) +{ + GF_VALIDATE_OR_GOTO("socket", entry, out); + + list_del_init(&entry->list); + if (entry->iobref) + iobref_unref(entry->iobref); + + /* TODO: use mem-pool */ + GF_FREE(entry); + +out: + return; +} + +static void +__socket_ioq_flush(socket_private_t *priv) +{ + struct ioq *entry = NULL; + + while (!list_empty(&priv->ioq)) { + entry = priv->ioq_next; + __socket_ioq_entry_free(entry); + } +} + +static int +__socket_ioq_churn_entry(rpc_transport_t *this, struct ioq *entry) +{ + int ret = -1; + + ret = __socket_writev(this, entry->pending_vector, entry->pending_count, + &entry->pending_vector, &entry->pending_count); + + if (ret == 0) { + /* current entry was completely written */ + GF_ASSERT(entry->pending_count == 0); + __socket_ioq_entry_free(entry); + } + + return ret; +} + +static int +__socket_ioq_churn(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = 0; + struct ioq *entry = NULL; + + priv = this->private; + + while (!list_empty(&priv->ioq)) { + /* pick next entry */ + entry = priv->ioq_next; + + ret = __socket_ioq_churn_entry(this, entry); + + if (ret != 0) + break; + } + + if (list_empty(&priv->ioq)) { + /* all pending writes done, not interested in POLLOUT */ + priv->idx = gf_event_select_on(this->ctx->event_pool, priv->sock, + priv->idx, -1, 0); + } + + return ret; +} + +static gf_boolean_t +socket_event_poll_err(rpc_transport_t *this, int gen, int idx) +{ + socket_private_t *priv = NULL; + gf_boolean_t socket_closed = _gf_false; + + priv = this->private; + + pthread_mutex_lock(&priv->out_lock); + { + if ((priv->gen == gen) && (priv->idx == idx) && (priv->sock >= 0)) { + __socket_ioq_flush(priv); + __socket_reset(this); + socket_closed = _gf_true; + } + } + pthread_mutex_unlock(&priv->out_lock); + + if (socket_closed) { + pthread_mutex_lock(&priv->notify.lock); + { + while (priv->notify.in_progress) + pthread_cond_wait(&priv->notify.cond, &priv->notify.lock); + } + pthread_mutex_unlock(&priv->notify.lock); + + rpc_transport_notify(this, RPC_TRANSPORT_DISCONNECT, this); + } + + return socket_closed; +} + +static int +socket_event_poll_out(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = -1; + + priv = this->private; + + pthread_mutex_lock(&priv->out_lock); + { + if (priv->connected == 1) { + ret = __socket_ioq_churn(this); + + if (ret < 0) { + gf_log(this->name, GF_LOG_TRACE, + "__socket_ioq_churn returned -1; " + "disconnecting socket"); + __socket_disconnect(this); + } + } + } + pthread_mutex_unlock(&priv->out_lock); + + if (ret == 0) + rpc_transport_notify(this, RPC_TRANSPORT_MSG_SENT, NULL); + + if (ret > 0) + ret = 0; + + return ret; +} + +static int +__socket_read_simple_msg(rpc_transport_t *this) +{ + int ret = 0; + uint32_t remaining_size = 0; + size_t bytes_read = 0; + socket_private_t *priv = NULL; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + + in = &priv->incoming; + frag = &in->frag; + + switch (frag->simple_state) { + case SP_STATE_SIMPLE_MSG_INIT: + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + __socket_proto_init_pending(priv, remaining_size); + + frag->simple_state = SP_STATE_READING_SIMPLE_MSG; + + /* fall through */ + + case SP_STATE_READING_SIMPLE_MSG: + ret = 0; + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if (remaining_size > 0) { + ret = __socket_readv(this, in->pending_vector, 1, + &in->pending_vector, &in->pending_count, + &bytes_read); + } + + if (ret < 0) { + gf_log(this->name, GF_LOG_WARNING, + "reading from socket failed. Error (%s), " + "peer (%s)", + strerror(errno), this->peerinfo.identifier); + break; + } + + frag->bytes_read += bytes_read; + frag->fragcurrent += bytes_read; + + if (ret > 0) { + gf_log(this->name, GF_LOG_TRACE, + "partial read on non-blocking socket."); + ret = 0; + break; + } + + if (ret == 0) { + frag->simple_state = SP_STATE_SIMPLE_MSG_INIT; + } + } + +out: + return ret; +} + +#define rpc_cred_addr(buf) (buf + RPC_MSGTYPE_SIZE + RPC_CALL_BODY_SIZE - 4) + +#define rpc_verf_addr(fragcurrent) (fragcurrent - 4) + +#define rpc_msgtype_addr(buf) (buf + 4) + +#define rpc_prognum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 4) +#define rpc_progver_addr(buf) (buf + RPC_MSGTYPE_SIZE + 8) +#define rpc_procnum_addr(buf) (buf + RPC_MSGTYPE_SIZE + 12) + +static int +__socket_read_vectored_request(rpc_transport_t *this, + rpcsvc_vector_sizer vector_sizer) +{ + socket_private_t *priv = NULL; + int ret = 0; + uint32_t credlen = 0, verflen = 0; + char *addr = NULL; + struct iobuf *iobuf = NULL; + uint32_t remaining_size = 0; + ssize_t readsize = 0; + size_t size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + sp_rpcfrag_request_state_t *request = NULL; + + priv = this->private; + + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + request = &frag->call_body.request; + + switch (request->vector_state) { + case SP_STATE_VECTORED_REQUEST_INIT: + request->vector_sizer_state = 0; + + addr = rpc_cred_addr(iobuf_ptr(in->iobuf)); + + /* also read verf flavour and verflen */ + credlen = ntoh32(*((uint32_t *)addr)) + + RPC_AUTH_FLAVOUR_N_LENGTH_SIZE; + + __socket_proto_init_pending(priv, credlen); + + request->vector_state = SP_STATE_READING_CREDBYTES; + + /* fall through */ + + case SP_STATE_READING_CREDBYTES: + __socket_proto_read(priv, ret); + + request->vector_state = SP_STATE_READ_CREDBYTES; + + /* fall through */ + + case SP_STATE_READ_CREDBYTES: + addr = rpc_verf_addr(frag->fragcurrent); + verflen = ntoh32(*((uint32_t *)addr)); + + if (verflen == 0) { + request->vector_state = SP_STATE_READ_VERFBYTES; + goto sp_state_read_verfbytes; + } + __socket_proto_init_pending(priv, verflen); + + request->vector_state = SP_STATE_READING_VERFBYTES; + + /* fall through */ + + case SP_STATE_READING_VERFBYTES: + __socket_proto_read(priv, ret); + + request->vector_state = SP_STATE_READ_VERFBYTES; + + /* fall through */ + + case SP_STATE_READ_VERFBYTES: + sp_state_read_verfbytes: + /* set the base_addr 'persistently' across multiple calls + into the state machine */ + in->proghdr_base_addr = frag->fragcurrent; + + request->vector_sizer_state = vector_sizer( + request->vector_sizer_state, &readsize, in->proghdr_base_addr, + frag->fragcurrent); + __socket_proto_init_pending(priv, readsize); + + request->vector_state = SP_STATE_READING_PROGHDR; + + /* fall through */ + + case SP_STATE_READING_PROGHDR: + __socket_proto_read(priv, ret); + + request->vector_state = SP_STATE_READ_PROGHDR; + + /* fall through */ + + case SP_STATE_READ_PROGHDR: + sp_state_read_proghdr: + request->vector_sizer_state = vector_sizer( + request->vector_sizer_state, &readsize, in->proghdr_base_addr, + frag->fragcurrent); + if (readsize == 0) { + request->vector_state = SP_STATE_READ_PROGHDR_XDATA; + goto sp_state_read_proghdr_xdata; + } + + __socket_proto_init_pending(priv, readsize); + + request->vector_state = SP_STATE_READING_PROGHDR_XDATA; + + /* fall through */ + + case SP_STATE_READING_PROGHDR_XDATA: + __socket_proto_read(priv, ret); + + request->vector_state = SP_STATE_READ_PROGHDR; + /* check if the vector_sizer() has more to say */ + goto sp_state_read_proghdr; + + case SP_STATE_READ_PROGHDR_XDATA: + sp_state_read_proghdr_xdata: + if (in->payload_vector.iov_base == NULL) { + size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + iobuf = iobuf_get2(this->ctx->iobuf_pool, size); + if (!iobuf) { + ret = -1; + break; + } + + if (in->iobref == NULL) { + in->iobref = iobref_new(); + if (in->iobref == NULL) { + ret = -1; + iobuf_unref(iobuf); + break; + } + } + + iobref_add(in->iobref, iobuf); + + in->payload_vector.iov_base = iobuf_ptr(iobuf); + frag->fragcurrent = iobuf_ptr(iobuf); + + iobuf_unref(iobuf); + } + + request->vector_state = SP_STATE_READING_PROG; + + /* fall through */ + + case SP_STATE_READING_PROG: + /* now read the remaining rpc msg into buffer pointed by + * fragcurrent + */ + + ret = __socket_read_simple_msg(this); + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + RPC_LASTFRAG(in->fraghdr))) { + request->vector_state = SP_STATE_VECTORED_REQUEST_INIT; + in->payload_vector.iov_len = ((unsigned long)frag->fragcurrent - + (unsigned long) + in->payload_vector.iov_base); + } + break; + } + + return ret; +} + +static int +__socket_read_request(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + uint32_t prognum = 0, procnum = 0, progver = 0; + uint32_t remaining_size = 0; + int ret = -1; + char *buf = NULL; + rpcsvc_vector_sizer vector_sizer = NULL; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + sp_rpcfrag_request_state_t *request = NULL; + + priv = this->private; + + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + request = &frag->call_body.request; + + switch (request->header_state) { + case SP_STATE_REQUEST_HEADER_INIT: + + __socket_proto_init_pending(priv, RPC_CALL_BODY_SIZE); + + request->header_state = SP_STATE_READING_RPCHDR1; + + /* fall through */ + + case SP_STATE_READING_RPCHDR1: + __socket_proto_read(priv, ret); + + request->header_state = SP_STATE_READ_RPCHDR1; + + /* fall through */ + + case SP_STATE_READ_RPCHDR1: + buf = rpc_prognum_addr(iobuf_ptr(in->iobuf)); + prognum = ntoh32(*((uint32_t *)buf)); + + buf = rpc_progver_addr(iobuf_ptr(in->iobuf)); + progver = ntoh32(*((uint32_t *)buf)); + + buf = rpc_procnum_addr(iobuf_ptr(in->iobuf)); + procnum = ntoh32(*((uint32_t *)buf)); + + if (priv->is_server) { + /* this check is needed as rpcsvc and rpc-clnt + * actor structures are not same */ + vector_sizer = rpcsvc_get_program_vector_sizer( + (rpcsvc_t *)this->mydata, prognum, progver, procnum); + } + + if (vector_sizer) { + ret = __socket_read_vectored_request(this, vector_sizer); + } else { + ret = __socket_read_simple_msg(this); + } + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + (RPC_LASTFRAG(in->fraghdr)))) { + request->header_state = SP_STATE_REQUEST_HEADER_INIT; + } + + break; + } + + return ret; +} + +static int +__socket_read_accepted_successful_reply(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = 0; + struct iobuf *iobuf = NULL; + gfs3_read_rsp read_rsp = { + 0, + }; + ssize_t size = 0; + ssize_t default_read_size = 0; + XDR xdr; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + uint32_t remaining_size = 0; + + priv = this->private; + + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + switch (frag->call_body.reply.accepted_success_state) { + case SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT: + default_read_size = xdr_sizeof((xdrproc_t)xdr_gfs3_read_rsp, + &read_rsp); + + /* We need to store the current base address because we will + * need it after a partial read. */ + in->proghdr_base_addr = frag->fragcurrent; + + __socket_proto_init_pending(priv, default_read_size); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READING_PROC_HEADER; + + /* fall through */ + + case SP_STATE_READING_PROC_HEADER: + __socket_proto_read(priv, ret); + + /* there can be 'xdata' in read response, figure it out */ + default_read_size = frag->fragcurrent - in->proghdr_base_addr; + xdrmem_create(&xdr, in->proghdr_base_addr, default_read_size, + XDR_DECODE); + + /* This will fail if there is xdata sent from server, if not, + well and good, we don't need to worry about */ + xdr_gfs3_read_rsp(&xdr, &read_rsp); + + free(read_rsp.xdata.xdata_val); + + /* need to round off to proper gf_roof (%4), as XDR packing pads + the end of opaque object with '0' */ + size = gf_roof(read_rsp.xdata.xdata_len, 4); + + if (!size) { + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_OPAQUE; + goto read_proc_opaque; + } + + __socket_proto_init_pending(priv, size); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READING_PROC_OPAQUE; + /* fall through */ + + case SP_STATE_READING_PROC_OPAQUE: + __socket_proto_read(priv, ret); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_OPAQUE; + /* fall through */ + + case SP_STATE_READ_PROC_OPAQUE: + read_proc_opaque: + if (in->payload_vector.iov_base == NULL) { + size = (RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read); + + iobuf = iobuf_get2(this->ctx->iobuf_pool, size); + if (iobuf == NULL) { + ret = -1; + goto out; + } + + if (in->iobref == NULL) { + in->iobref = iobref_new(); + if (in->iobref == NULL) { + ret = -1; + iobuf_unref(iobuf); + goto out; + } + } + + ret = iobref_add(in->iobref, iobuf); + iobuf_unref(iobuf); + if (ret < 0) { + goto out; + } + + in->payload_vector.iov_base = iobuf_ptr(iobuf); + in->payload_vector.iov_len = size; + } + + frag->fragcurrent = in->payload_vector.iov_base; + + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_HEADER; + + /* fall through */ + + case SP_STATE_READ_PROC_HEADER: + /* now read the entire remaining msg into new iobuf */ + ret = __socket_read_simple_msg(this); + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + RPC_LASTFRAG(in->fraghdr))) { + frag->call_body.reply.accepted_success_state = + SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT; + } + + break; + } + +out: + return ret; +} + +static int +__socket_read_accepted_successful_reply_v2(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = 0; + struct iobuf *iobuf = NULL; + gfx_read_rsp read_rsp = { + 0, + }; + ssize_t size = 0; + ssize_t default_read_size = 0; + XDR xdr; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + uint32_t remaining_size = 0; + + priv = this->private; + + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + switch (frag->call_body.reply.accepted_success_state) { + case SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT: + default_read_size = xdr_sizeof((xdrproc_t)xdr_gfx_read_rsp, + &read_rsp); + + /* We need to store the current base address because we will + * need it after a partial read. */ + in->proghdr_base_addr = frag->fragcurrent; + + __socket_proto_init_pending(priv, default_read_size); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READING_PROC_HEADER; + + /* fall through */ + + case SP_STATE_READING_PROC_HEADER: + __socket_proto_read(priv, ret); + + /* there can be 'xdata' in read response, figure it out */ + default_read_size = frag->fragcurrent - in->proghdr_base_addr; + + xdrmem_create(&xdr, in->proghdr_base_addr, default_read_size, + XDR_DECODE); + + /* This will fail if there is xdata sent from server, if not, + well and good, we don't need to worry about */ + xdr_gfx_read_rsp(&xdr, &read_rsp); + + free(read_rsp.xdata.pairs.pairs_val); + + /* need to round off to proper gf_roof (%4), as XDR packing pads + the end of opaque object with '0' */ + size = gf_roof(read_rsp.xdata.xdr_size, 4); + + if (!size) { + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_OPAQUE; + goto read_proc_opaque; + } + + __socket_proto_init_pending(priv, size); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READING_PROC_OPAQUE; + /* fall through */ + + case SP_STATE_READING_PROC_OPAQUE: + __socket_proto_read(priv, ret); + + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_OPAQUE; + /* fall through */ + + case SP_STATE_READ_PROC_OPAQUE: + read_proc_opaque: + if (in->payload_vector.iov_base == NULL) { + size = (RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read); + + iobuf = iobuf_get2(this->ctx->iobuf_pool, size); + if (iobuf == NULL) { + ret = -1; + goto out; + } + + if (in->iobref == NULL) { + in->iobref = iobref_new(); + if (in->iobref == NULL) { + ret = -1; + iobuf_unref(iobuf); + goto out; + } + } + + ret = iobref_add(in->iobref, iobuf); + iobuf_unref(iobuf); + if (ret < 0) { + goto out; + } + + in->payload_vector.iov_base = iobuf_ptr(iobuf); + in->payload_vector.iov_len = size; + } + + frag->fragcurrent = in->payload_vector.iov_base; + + frag->call_body.reply + .accepted_success_state = SP_STATE_READ_PROC_HEADER; + + /* fall through */ + + case SP_STATE_READ_PROC_HEADER: + /* now read the entire remaining msg into new iobuf */ + ret = __socket_read_simple_msg(this); + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + RPC_LASTFRAG(in->fraghdr))) { + frag->call_body.reply.accepted_success_state = + SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT; + } + + break; + } + +out: + return ret; +} + +#define rpc_reply_verflen_addr(fragcurrent) ((char *)fragcurrent - 4) +#define rpc_reply_accept_status_addr(fragcurrent) ((char *)fragcurrent - 4) + +static int +__socket_read_accepted_reply(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = -1; + char *buf = NULL; + uint32_t verflen = 0, len = 0; + uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + priv = this->private; + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + switch (frag->call_body.reply.accepted_state) { + case SP_STATE_ACCEPTED_REPLY_INIT: + __socket_proto_init_pending(priv, RPC_AUTH_FLAVOUR_N_LENGTH_SIZE); + + frag->call_body.reply + .accepted_state = SP_STATE_READING_REPLY_VERFLEN; + + /* fall through */ + + case SP_STATE_READING_REPLY_VERFLEN: + __socket_proto_read(priv, ret); + + frag->call_body.reply.accepted_state = SP_STATE_READ_REPLY_VERFLEN; + + /* fall through */ + + case SP_STATE_READ_REPLY_VERFLEN: + buf = rpc_reply_verflen_addr(frag->fragcurrent); + + verflen = ntoh32(*((uint32_t *)buf)); + + /* also read accept status along with verf data */ + len = verflen + RPC_ACCEPT_STATUS_LEN; + + __socket_proto_init_pending(priv, len); + + frag->call_body.reply + .accepted_state = SP_STATE_READING_REPLY_VERFBYTES; + + /* fall through */ + + case SP_STATE_READING_REPLY_VERFBYTES: + __socket_proto_read(priv, ret); + + frag->call_body.reply + .accepted_state = SP_STATE_READ_REPLY_VERFBYTES; + + buf = rpc_reply_accept_status_addr(frag->fragcurrent); + + frag->call_body.reply.accept_status = ntoh32(*(uint32_t *)buf); + + /* fall through */ + + case SP_STATE_READ_REPLY_VERFBYTES: + + if (frag->call_body.reply.accept_status == SUCCESS) { + /* Need two different methods here for different protocols + Mainly because the exact XDR is used to calculate the + size of response */ + if ((in->request_info->procnum == GFS3_OP_READ) && + (in->request_info->prognum == GLUSTER_FOP_PROGRAM) && + (in->request_info->progver == GLUSTER_FOP_VERSION_v2)) { + ret = __socket_read_accepted_successful_reply_v2(this); + } else { + ret = __socket_read_accepted_successful_reply(this); + } + } else { + /* read entire remaining msg into buffer pointed to by + * fragcurrent + */ + ret = __socket_read_simple_msg(this); + } + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + (RPC_LASTFRAG(in->fraghdr)))) { + frag->call_body.reply + .accepted_state = SP_STATE_ACCEPTED_REPLY_INIT; + } + + break; + } + + return ret; +} + +static int +__socket_read_denied_reply(rpc_transport_t *this) +{ + return __socket_read_simple_msg(this); +} + +#define rpc_reply_status_addr(fragcurrent) ((char *)fragcurrent - 4) + +static int +__socket_read_vectored_reply(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = 0; + char *buf = NULL; + uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + priv = this->private; + in = &priv->incoming; + frag = &in->frag; + + switch (frag->call_body.reply.status_state) { + case SP_STATE_ACCEPTED_REPLY_INIT: + __socket_proto_init_pending(priv, RPC_REPLY_STATUS_SIZE); + + frag->call_body.reply.status_state = SP_STATE_READING_REPLY_STATUS; + + /* fall through */ + + case SP_STATE_READING_REPLY_STATUS: + __socket_proto_read(priv, ret); + + buf = rpc_reply_status_addr(frag->fragcurrent); + + frag->call_body.reply.accept_status = ntoh32(*((uint32_t *)buf)); + + frag->call_body.reply.status_state = SP_STATE_READ_REPLY_STATUS; + + /* fall through */ + + case SP_STATE_READ_REPLY_STATUS: + if (frag->call_body.reply.accept_status == MSG_ACCEPTED) { + ret = __socket_read_accepted_reply(this); + } else { + ret = __socket_read_denied_reply(this); + } + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + (RPC_LASTFRAG(in->fraghdr)))) { + frag->call_body.reply + .status_state = SP_STATE_VECTORED_REPLY_STATUS_INIT; + in->payload_vector.iov_len = (unsigned long)frag->fragcurrent - + (unsigned long) + in->payload_vector.iov_base; + } + break; + } + + return ret; +} + +static int +__socket_read_simple_reply(rpc_transport_t *this) +{ + return __socket_read_simple_msg(this); +} + +#define rpc_xid_addr(buf) (buf) + +static int +__socket_read_reply(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + char *buf = NULL; + int32_t ret = -1; + rpc_request_info_t *request_info = NULL; + char map_xid = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + priv = this->private; + in = &priv->incoming; + frag = &in->frag; + + buf = rpc_xid_addr(iobuf_ptr(in->iobuf)); + + if (in->request_info == NULL) { + in->request_info = GF_CALLOC(1, sizeof(*request_info), + gf_common_mt_rpc_trans_reqinfo_t); + if (in->request_info == NULL) { + goto out; + } + + map_xid = 1; + } + + request_info = in->request_info; + + if (map_xid) { + request_info->xid = ntoh32(*((uint32_t *)buf)); + + /* release priv->lock, so as to avoid deadlock b/w conn->lock + * and priv->lock, since we are doing an upcall here. + */ + frag->state = SP_STATE_NOTIFYING_XID; + ret = rpc_transport_notify(this, RPC_TRANSPORT_MAP_XID_REQUEST, + in->request_info); + + /* Transition back to externally visible state. */ + frag->state = SP_STATE_READ_MSGTYPE; + + if (ret < 0) { + gf_log(this->name, GF_LOG_WARNING, + "notify for event MAP_XID failed for %s", + this->peerinfo.identifier); + goto out; + } + } + + if ((request_info->prognum == GLUSTER_FOP_PROGRAM) && + (request_info->procnum == GF_FOP_READ)) { + if (map_xid && request_info->rsp.rsp_payload_count != 0) { + in->iobref = iobref_ref(request_info->rsp.rsp_iobref); + in->payload_vector = *request_info->rsp.rsp_payload; + } + + ret = __socket_read_vectored_reply(this); + } else { + ret = __socket_read_simple_reply(this); + } +out: + return ret; +} + +/* returns the number of bytes yet to be read in a fragment */ +static int +__socket_read_frag(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int32_t ret = 0; + char *buf = NULL; + uint32_t remaining_size = 0; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + priv = this->private; + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + switch (frag->state) { + case SP_STATE_NADA: + __socket_proto_init_pending(priv, RPC_MSGTYPE_SIZE); + + frag->state = SP_STATE_READING_MSGTYPE; + + /* fall through */ + + case SP_STATE_READING_MSGTYPE: + __socket_proto_read(priv, ret); + + frag->state = SP_STATE_READ_MSGTYPE; + /* fall through */ + + case SP_STATE_READ_MSGTYPE: + buf = rpc_msgtype_addr(iobuf_ptr(in->iobuf)); + in->msg_type = ntoh32(*((uint32_t *)buf)); + + if (in->msg_type == CALL) { + ret = __socket_read_request(this); + } else if (in->msg_type == REPLY) { + ret = __socket_read_reply(this); + } else if (in->msg_type == (msg_type_t)GF_UNIVERSAL_ANSWER) { + gf_log("rpc", GF_LOG_ERROR, + "older version of protocol/process trying to " + "connect from %s. use newer version on that node", + this->peerinfo.identifier); + } else { + gf_log("rpc", GF_LOG_ERROR, + "wrong MSG-TYPE (%d) received from %s", in->msg_type, + this->peerinfo.identifier); + ret = -1; + } + + remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read; + + if ((ret < 0) || ((ret == 0) && (remaining_size == 0) && + (RPC_LASTFRAG(in->fraghdr)))) { + /* frag->state = SP_STATE_NADA; */ + frag->state = SP_STATE_RPCFRAG_INIT; + } + + break; + + case SP_STATE_NOTIFYING_XID: + /* Another epoll thread is notifying higher layers + *of reply's xid. */ + errno = EAGAIN; + return -1; + break; + } + + return ret; +} + +static void +__socket_reset_priv(socket_private_t *priv) +{ + struct gf_sock_incoming *in = NULL; + + /* used to reduce the indirection */ + in = &priv->incoming; + + if (in->iobref) { + iobref_unref(in->iobref); + in->iobref = NULL; + } + + if (in->iobuf) { + iobuf_unref(in->iobuf); + in->iobuf = NULL; + } + + if (in->request_info != NULL) { + GF_FREE(in->request_info); + in->request_info = NULL; + } + + memset(&in->payload_vector, 0, sizeof(in->payload_vector)); +} + +static int +__socket_proto_state_machine(rpc_transport_t *this, + rpc_transport_pollin_t **pollin) +{ + int ret = -1; + socket_private_t *priv = NULL; + struct iobuf *iobuf = NULL; + struct iobref *iobref = NULL; + struct iovec vector[2]; + struct gf_sock_incoming *in = NULL; + struct gf_sock_incoming_frag *frag = NULL; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + /* used to reduce the indirection */ + in = &priv->incoming; + frag = &in->frag; + + while (in->record_state != SP_STATE_COMPLETE) { + switch (in->record_state) { + case SP_STATE_NADA: + in->total_bytes_read = 0; + in->payload_vector.iov_len = 0; + + in->pending_vector = in->vector; + in->pending_vector->iov_base = &in->fraghdr; + + in->pending_vector->iov_len = sizeof(in->fraghdr); + + in->record_state = SP_STATE_READING_FRAGHDR; + + /* fall through */ + + case SP_STATE_READING_FRAGHDR: + ret = __socket_readv(this, in->pending_vector, 1, + &in->pending_vector, &in->pending_count, + NULL); + if (ret < 0) + goto out; + + if (ret > 0) { + gf_log(this->name, GF_LOG_TRACE, + "partial " + "fragment header read"); + ret = 0; + goto out; + } + + if (ret == 0) { + in->record_state = SP_STATE_READ_FRAGHDR; + } + /* fall through */ + + case SP_STATE_READ_FRAGHDR: + + in->fraghdr = ntoh32(in->fraghdr); + in->total_bytes_read += RPC_FRAGSIZE(in->fraghdr); + + if (in->total_bytes_read >= GF_UNIT_GB) { + ret = -1; + goto out; + } + + iobuf = iobuf_get2( + this->ctx->iobuf_pool, + (in->total_bytes_read + sizeof(in->fraghdr))); + if (!iobuf) { + ret = -1; + goto out; + } + + if (in->iobuf == NULL) { + /* first fragment */ + frag->fragcurrent = iobuf_ptr(iobuf); + } else { + /* second or further fragment */ + memcpy(iobuf_ptr(iobuf), iobuf_ptr(in->iobuf), + in->total_bytes_read - RPC_FRAGSIZE(in->fraghdr)); + iobuf_unref(in->iobuf); + frag->fragcurrent = (char *)iobuf_ptr(iobuf) + + in->total_bytes_read - + RPC_FRAGSIZE(in->fraghdr); + frag->pending_vector->iov_base = frag->fragcurrent; + in->pending_vector = frag->pending_vector; + } + + in->iobuf = iobuf; + in->iobuf_size = 0; + in->record_state = SP_STATE_READING_FRAG; + /* fall through */ + + case SP_STATE_READING_FRAG: + ret = __socket_read_frag(this); + + if ((ret < 0) || + (frag->bytes_read != RPC_FRAGSIZE(in->fraghdr))) { + goto out; + } + + frag->bytes_read = 0; + + if (!RPC_LASTFRAG(in->fraghdr)) { + in->pending_vector = in->vector; + in->pending_vector->iov_base = &in->fraghdr; + in->pending_vector->iov_len = sizeof(in->fraghdr); + in->record_state = SP_STATE_READING_FRAGHDR; + break; + } + + /* we've read the entire rpc record, notify the + * upper layers. + */ + if (pollin != NULL) { + int count = 0; + in->iobuf_size = (in->total_bytes_read - + in->payload_vector.iov_len); + + memset(vector, 0, sizeof(vector)); + + if (in->iobref == NULL) { + in->iobref = iobref_new(); + if (in->iobref == NULL) { + ret = -1; + goto out; + } + } + + vector[count].iov_base = iobuf_ptr(in->iobuf); + vector[count].iov_len = in->iobuf_size; + + iobref = in->iobref; + + count++; + + if (in->payload_vector.iov_base != NULL) { + vector[count] = in->payload_vector; + count++; + } + + *pollin = rpc_transport_pollin_alloc(this, vector, count, + in->iobuf, iobref, + in->request_info); + iobuf_unref(in->iobuf); + in->iobuf = NULL; + + if (*pollin == NULL) { + gf_log(this->name, GF_LOG_WARNING, + "transport pollin allocation failed"); + ret = -1; + goto out; + } + if (in->msg_type == REPLY) + (*pollin)->is_reply = 1; + + in->request_info = NULL; + } + in->record_state = SP_STATE_COMPLETE; + break; + + case SP_STATE_COMPLETE: + /* control should not reach here */ + gf_log(this->name, GF_LOG_WARNING, + "control reached to " + "SP_STATE_COMPLETE, which should not have " + "happened"); + break; + } + } + + if (in->record_state == SP_STATE_COMPLETE) { + in->record_state = SP_STATE_NADA; + __socket_reset_priv(priv); + } + +out: + return ret; +} + +static int +socket_proto_state_machine(rpc_transport_t *this, + rpc_transport_pollin_t **pollin) +{ + return __socket_proto_state_machine(this, pollin); +} + +static void +socket_event_poll_in_async(xlator_t *xl, gf_async_t *async) +{ + rpc_transport_pollin_t *pollin; + rpc_transport_t *this; + socket_private_t *priv; + + pollin = caa_container_of(async, rpc_transport_pollin_t, async); + this = pollin->trans; + priv = this->private; + + rpc_transport_notify(this, RPC_TRANSPORT_MSG_RECEIVED, pollin); + + rpc_transport_unref(this); + + rpc_transport_pollin_destroy(pollin); + + pthread_mutex_lock(&priv->notify.lock); + { + --priv->notify.in_progress; + + if (!priv->notify.in_progress) + pthread_cond_signal(&priv->notify.cond); + } + pthread_mutex_unlock(&priv->notify.lock); +} + +static int +socket_event_poll_in(rpc_transport_t *this, gf_boolean_t notify_handled) +{ + int ret = -1; + rpc_transport_pollin_t *pollin = NULL; + socket_private_t *priv = this->private; + glusterfs_ctx_t *ctx = NULL; + + ctx = this->ctx; + + ret = socket_proto_state_machine(this, &pollin); + + if (pollin) { + pthread_mutex_lock(&priv->notify.lock); + { + priv->notify.in_progress++; + } + pthread_mutex_unlock(&priv->notify.lock); + } + + if (notify_handled && (ret >= 0)) + gf_event_handled(ctx->event_pool, priv->sock, priv->idx, priv->gen); + + if (pollin) { + rpc_transport_ref(this); + gf_async(&pollin->async, THIS, socket_event_poll_in_async); + } + + return ret; +} + +static int +socket_connect_finish(rpc_transport_t *this) +{ + int ret = -1; + socket_private_t *priv = NULL; + rpc_transport_event_t event = 0; + char notify_rpc = 0; + + priv = this->private; + + pthread_mutex_lock(&priv->out_lock); + { + if (priv->connected != 0) + goto unlock; + + get_transport_identifiers(this); + + ret = __socket_connect_finish(priv->sock); + + if ((ret < 0) && (errno == EINPROGRESS)) + ret = 1; + + if ((ret < 0) && (errno != EINPROGRESS)) { + if (!priv->connect_finish_log) { + gf_log(this->name, GF_LOG_ERROR, + "connection to %s failed (%s); " + "disconnecting socket", + this->peerinfo.identifier, strerror(errno)); + priv->connect_finish_log = 1; + } + __socket_disconnect(this); + goto unlock; + } + + if (ret == 0) { + notify_rpc = 1; + + this->myinfo.sockaddr_len = sizeof(this->myinfo.sockaddr); + + ret = getsockname(priv->sock, SA(&this->myinfo.sockaddr), + &this->myinfo.sockaddr_len); + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, + "getsockname on (%d) failed (%s) - " + "disconnecting socket", + priv->sock, strerror(errno)); + __socket_disconnect(this); + event = RPC_TRANSPORT_DISCONNECT; + goto unlock; + } + + priv->connected = 1; + priv->connect_finish_log = 0; + event = RPC_TRANSPORT_CONNECT; + } + } +unlock: + pthread_mutex_unlock(&priv->out_lock); + + if (notify_rpc) { + rpc_transport_notify(this, event, this); + } + + return ret; +} + +static int +socket_disconnect(rpc_transport_t *this, gf_boolean_t wait); + +/* socket_is_connected() is for use only in socket_event_handler() */ +static inline gf_boolean_t +socket_is_connected(socket_private_t *priv) +{ + if (priv->use_ssl) { + return priv->is_server ? priv->ssl_accepted : priv->ssl_connected; + } else { + return priv->is_server ? priv->accepted : priv->connected; + } +} + +static void +ssl_rearm_event_fd(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + int idx = -1; + int gen = -1; + int fd = -1; + + priv = this->private; + ctx = this->ctx; + + idx = priv->idx; + gen = priv->gen; + fd = priv->sock; + + if (priv->ssl_error_required == SSL_ERROR_WANT_READ) + gf_event_select_on(ctx->event_pool, fd, idx, 1, -1); + if (priv->ssl_error_required == SSL_ERROR_WANT_WRITE) + gf_event_select_on(ctx->event_pool, fd, idx, -1, 1); + gf_event_handled(ctx->event_pool, fd, idx, gen); +} + +static int +ssl_handle_server_connection_attempt(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + int idx = -1; + int gen = -1; + int ret = -1; + int fd = -1; + + priv = this->private; + ctx = this->ctx; + + idx = priv->idx; + gen = priv->gen; + fd = priv->sock; + + if (!priv->ssl_context_created) { + ret = ssl_setup_connection_prefix(this, _gf_true); + if (ret < 0) { + gf_log(this->name, GF_LOG_TRACE, + "> ssl_setup_connection_prefix() failed!"); + ret = -1; + goto out; + } else { + priv->ssl_context_created = _gf_true; + } + } + ret = ssl_complete_connection(this); + if (ret == 0) { + /* nothing to do */ + gf_event_select_on(ctx->event_pool, fd, idx, 1, 0); + gf_event_handled(ctx->event_pool, fd, idx, gen); + ret = 1; + } else { + if (errno == EAGAIN) { + ssl_rearm_event_fd(this); + ret = 1; + } else { + ret = -1; + gf_log(this->name, GF_LOG_TRACE, + "ssl_complete_connection returned error"); + } + } +out: + return ret; +} + +static int +ssl_handle_client_connection_attempt(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + int idx = -1; + int ret = -1; + int fd = -1; + + priv = this->private; + ctx = this->ctx; + + idx = priv->idx; + fd = priv->sock; + + /* SSL client */ + if (priv->connect_failed) { + gf_log(this->name, GF_LOG_TRACE, ">>> disconnecting SSL socket"); + (void)socket_disconnect(this, _gf_false); + /* Force ret to be -1, as we are officially done with + this socket */ + ret = -1; + } else { + if (!priv->ssl_context_created) { + ret = ssl_setup_connection_prefix(this, _gf_false); + if (ret < 0) { + gf_log(this->name, GF_LOG_TRACE, + "> ssl_setup_connection_prefix() " + "failed!"); + ret = -1; + goto out; + } else { + priv->ssl_context_created = _gf_true; + } + } + ret = ssl_complete_connection(this); + if (ret == 0) { + ret = socket_connect_finish(this); + gf_event_select_on(ctx->event_pool, fd, idx, 1, 0); + gf_log(this->name, GF_LOG_TRACE, ">>> completed client connect"); + } else { + if (errno == EAGAIN) { + gf_log(this->name, GF_LOG_TRACE, + ">>> retrying client connect 2"); + ssl_rearm_event_fd(this); + ret = 1; + } else { + /* this is a connection failure */ + (void)socket_connect_finish(this); + gf_log(this->name, GF_LOG_TRACE, + "ssl_complete_connection " + "returned error"); + ret = -1; + } + } + } +out: + return ret; +} + +static int +socket_handle_client_connection_attempt(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + int idx = -1; + int gen = -1; + int ret = -1; + int fd = -1; + + priv = this->private; + ctx = this->ctx; + + idx = priv->idx; + gen = priv->gen; + fd = priv->sock; + + /* non-SSL client */ + if (priv->connect_failed) { + /* connect failed with some other error than + EINPROGRESS or ENOENT, so nothing more to + do, fail reading/writing anything even if + poll_in or poll_out + is set + */ + gf_log("transport", GF_LOG_DEBUG, + "connect failed with some other error " + "than EINPROGRESS or ENOENT, so " + "nothing more to do; disconnecting " + "socket"); + (void)socket_disconnect(this, _gf_false); + + /* Force ret to be -1, as we are officially + * done with this socket + */ + ret = -1; + } else { + ret = socket_connect_finish(this); + gf_log(this->name, GF_LOG_TRACE, "socket_connect_finish() returned %d", + ret); + if (ret == 0 || ret == 1) { + /* we don't want to do any reads or + * writes on the connection yet in + * socket_event_handler, so just + * return 1 + */ + ret = 1; + gf_event_handled(ctx->event_pool, fd, idx, gen); + } + } + return ret; +} + +static int +socket_complete_connection(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + glusterfs_ctx_t *ctx = NULL; + int idx = -1; + int gen = -1; + int ret = -1; + int fd = -1; + + priv = this->private; + ctx = this->ctx; + + idx = priv->idx; + gen = priv->gen; + fd = priv->sock; + + if (priv->use_ssl) { + if (priv->is_server) { + ret = ssl_handle_server_connection_attempt(this); + } else { + ret = ssl_handle_client_connection_attempt(this); + } + } else { + if (priv->is_server) { + /* non-SSL server: nothing much to do + * connection has already been accepted in + * socket_server_event_handler() + */ + priv->accepted = _gf_true; + gf_event_handled(ctx->event_pool, fd, idx, gen); + ret = 1; + } else { + ret = socket_handle_client_connection_attempt(this); + } + } + return ret; +} + +/* reads rpc_requests during pollin */ +static void +socket_event_handler(int fd, int idx, int gen, void *data, int poll_in, + int poll_out, int poll_err, char event_thread_died) +{ + rpc_transport_t *this = NULL; + socket_private_t *priv = NULL; + int ret = -1; + glusterfs_ctx_t *ctx = NULL; + gf_boolean_t socket_closed = _gf_false, notify_handled = _gf_false; + + this = data; + + if (event_thread_died) { + /* to avoid duplicate notifications, notify only for listener sockets */ + return; + } + + /* At this point we are sure no other thread is using the transport because + * we cannot receive more events until we call gf_event_handled(). However + * this function may call gf_event_handled() in some cases. When this is + * done, the transport may be destroyed at any moment if another thread + * handled an error event. To prevent that we take a reference here. */ + rpc_transport_ref(this); + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + GF_VALIDATE_OR_GOTO("socket", this->xl, out); + + THIS = this->xl; + priv = this->private; + ctx = this->ctx; + + pthread_mutex_lock(&priv->out_lock); + { + priv->idx = idx; + priv->gen = gen; + } + pthread_mutex_unlock(&priv->out_lock); + + gf_log(this->name, GF_LOG_TRACE, "%s (sock:%d) in:%d, out:%d, err:%d", + (priv->is_server ? "server" : "client"), priv->sock, poll_in, + poll_out, poll_err); + + if (!poll_err) { + if (!socket_is_connected(priv)) { + gf_log(this->name, GF_LOG_TRACE, + "%s (sock:%d) socket is not connected, " + "completing connection", + (priv->is_server ? "server" : "client"), priv->sock); + + ret = socket_complete_connection(this); + + gf_log(this->name, GF_LOG_TRACE, + "(sock:%d) " + "socket_complete_connection() returned %d", + priv->sock, ret); + + if (ret > 0) { + gf_log(this->name, GF_LOG_TRACE, + "(sock:%d) returning to wait on socket", priv->sock); + goto out; + } + } else { + char *sock_type = (priv->is_server ? "Server" : "Client"); + + gf_log(this->name, GF_LOG_TRACE, + "%s socket (%d) is already connected", sock_type, + priv->sock); + ret = 0; + } + } + + if (!ret && poll_out) { + ret = socket_event_poll_out(this); + gf_log(this->name, GF_LOG_TRACE, + "(sock:%d) " + "socket_event_poll_out returned %d", + priv->sock, ret); + } + + if (!ret && poll_in) { + ret = socket_event_poll_in(this, !poll_err); + gf_log(this->name, GF_LOG_TRACE, + "(sock:%d) " + "socket_event_poll_in returned %d", + priv->sock, ret); + notify_handled = _gf_true; + } + + if ((ret < 0) || poll_err) { + struct sockaddr *sa = SA(&this->peerinfo.sockaddr); + + if (priv->is_server && + SA(&this->myinfo.sockaddr)->sa_family == AF_UNIX) { + sa = SA(&this->myinfo.sockaddr); + } + + socket_dump_info(sa, priv->is_server, priv->use_ssl, priv->sock, + this->name, "disconnecting from"); + + /* Dump the SSL error stack to clear any errors that may otherwise + * resurface in the future. + */ + if (priv->use_ssl && priv->ssl_ssl) { + ssl_dump_error_stack(this->name); + } + + /* Logging has happened already in earlier cases */ + gf_log("transport", ((ret >= 0) ? GF_LOG_INFO : GF_LOG_DEBUG), + "EPOLLERR - disconnecting (sock:%d) (%s)", priv->sock, + (priv->use_ssl ? "SSL" : "non-SSL")); + + socket_closed = socket_event_poll_err(this, gen, idx); + + if (socket_closed) + rpc_transport_unref(this); + + } else if (!notify_handled) { + gf_event_handled(ctx->event_pool, fd, idx, gen); + } + +out: + rpc_transport_unref(this); +} + +static void +socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in, + int poll_out, int poll_err, char event_thread_died) +{ + rpc_transport_t *this = NULL; + socket_private_t *priv = NULL; + int ret = 0; + int new_sock = -1; + rpc_transport_t *new_trans = NULL; + struct sockaddr_storage new_sockaddr = { + 0, + }; + socklen_t addrlen = sizeof(new_sockaddr); + socket_private_t *new_priv = NULL; + glusterfs_ctx_t *ctx = NULL; + + this = data; + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + GF_VALIDATE_OR_GOTO("socket", this->xl, out); + GF_VALIDATE_OR_GOTO("socket", this->ctx, out); + + THIS = this->xl; + priv = this->private; + ctx = this->ctx; + + if (event_thread_died) { + rpc_transport_notify(this, RPC_TRANSPORT_EVENT_THREAD_DIED, + (void *)(unsigned long)gen); + return; + } + + /* NOTE: + * We have done away with the critical section in this function. since + * there's little that it helps with. There's no other code that + * attempts to unref the listener socket/transport from any other + * thread context while we are using it here. + */ + priv->idx = idx; + priv->gen = gen; + + if (poll_err) { + socket_event_poll_err(this, gen, idx); + goto out; + } + + if (poll_in) { + int aflags = 0; + + if (!priv->bio) + aflags = O_NONBLOCK; + + new_sock = sys_accept(priv->sock, SA(&new_sockaddr), &addrlen, aflags); + + gf_event_handled(ctx->event_pool, fd, idx, gen); + + if (new_sock < 0) { + gf_log(this->name, GF_LOG_WARNING, "accept on %d failed (%s)", + priv->sock, strerror(errno)); + goto out; + } + + if (new_sockaddr.ss_family != AF_UNIX) { + if (priv->nodelay) { + ret = __socket_nodelay(new_sock); + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, + "setsockopt() failed for " + "NODELAY (%s)", + strerror(errno)); + } + } + + if (priv->keepalive) { + ret = __socket_keepalive( + new_sock, new_sockaddr.ss_family, priv->keepaliveintvl, + priv->keepaliveidle, priv->keepalivecnt, priv->timeout); + if (ret != 0) + gf_log(this->name, GF_LOG_WARNING, + "Failed to set keep-alive: %s", strerror(errno)); + } + } + + new_trans = GF_CALLOC(1, sizeof(*new_trans), gf_common_mt_rpc_trans_t); + if (!new_trans) { + sys_close(new_sock); + gf_log(this->name, GF_LOG_WARNING, + "transport object allocation failure; " + "closed newly accepted socket %d", + new_sock); + goto out; + } + + ret = pthread_mutex_init(&new_trans->lock, NULL); + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, + "pthread_mutex_init() failed: %s; closing newly accepted " + "socket %d", + strerror(errno), new_sock); + sys_close(new_sock); + GF_FREE(new_trans); + goto out; + } + INIT_LIST_HEAD(&new_trans->list); + + new_trans->name = gf_strdup(this->name); + + memcpy(&new_trans->peerinfo.sockaddr, &new_sockaddr, addrlen); + new_trans->peerinfo.sockaddr_len = addrlen; + + new_trans->myinfo.sockaddr_len = sizeof(new_trans->myinfo.sockaddr); + + ret = getsockname(new_sock, SA(&new_trans->myinfo.sockaddr), + &new_trans->myinfo.sockaddr_len); + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, + "getsockname on socket %d " + "failed (errno:%s); closing newly accepted socket", + new_sock, strerror(errno)); + sys_close(new_sock); + GF_FREE(new_trans->name); + GF_FREE(new_trans); + goto out; + } + + get_transport_identifiers(new_trans); + gf_log(this->name, GF_LOG_TRACE, "XXX server:%s, client:%s", + new_trans->myinfo.identifier, new_trans->peerinfo.identifier); + + /* Make options available to local socket_init() to create new + * SSL_CTX per transport. A separate SSL_CTX per transport is + * required to avoid setting crl checking options for client + * connections. The verification options eventually get copied + * to the SSL object. Unfortunately, there's no way to identify + * whether socket_init() is being called after a client-side + * connect() or a server-side accept(). Although, we could pass + * a flag from the transport init() to the socket_init() and + * from this place, this doesn't identify the case where the + * server-side transport loading is done for the first time. + * Also, SSL doesn't apply for UNIX sockets. + */ + if (new_sockaddr.ss_family != AF_UNIX) + new_trans->options = dict_ref(this->options); + new_trans->ctx = this->ctx; + + ret = socket_init(new_trans); + + /* reset options to NULL to avoid double free */ + if (new_sockaddr.ss_family != AF_UNIX) { + dict_unref(new_trans->options); + new_trans->options = NULL; + } + + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, + "initialization of new_trans " + "failed; closing newly accepted socket %d", + new_sock); + sys_close(new_sock); + GF_FREE(new_trans->name); + GF_FREE(new_trans); + goto out; + } + new_trans->ops = this->ops; + new_trans->init = this->init; + new_trans->fini = this->fini; + new_trans->ctx = ctx; + new_trans->xl = this->xl; + new_trans->mydata = this->mydata; + new_trans->notify = this->notify; + new_trans->listener = this; + new_trans->notify_poller_death = this->poller_death_accept; + new_priv = new_trans->private; + + if (new_sockaddr.ss_family == AF_UNIX) { + new_priv->use_ssl = _gf_false; + } else { + switch (priv->srvr_ssl) { + case MGMT_SSL_ALWAYS: + /* Glusterd with secure_mgmt. */ + new_priv->use_ssl = _gf_true; + break; + case MGMT_SSL_COPY_IO: + /* Glusterfsd. */ + new_priv->use_ssl = priv->ssl_enabled; + break; + default: + new_priv->use_ssl = _gf_false; + } + } + + new_priv->sock = new_sock; + + new_priv->ssl_enabled = priv->ssl_enabled; + new_priv->connected = 1; + new_priv->is_server = _gf_true; + + /* + * This is the first ref on the newly accepted + * transport. + */ + rpc_transport_ref(new_trans); + + { + /* Take a ref on the new_trans to avoid + * getting deleted when event_register() + * causes socket_event_handler() to race + * ahead of this path to eventually find + * a disconnect and unref the transport + */ + rpc_transport_ref(new_trans); + + /* Send a notification to RPCSVC layer + * to save the new_trans in its service + * list before we register the new_sock + * with epoll to begin receiving notifications + * for data handling. + */ + ret = rpc_transport_notify(this, RPC_TRANSPORT_ACCEPT, new_trans); + + if (ret >= 0) { + new_priv->idx = gf_event_register( + ctx->event_pool, new_sock, socket_event_handler, new_trans, + 1, 0, new_trans->notify_poller_death); + if (new_priv->idx == -1) { + ret = -1; + gf_log(this->name, GF_LOG_ERROR, + "failed to register the socket " + "with event"); + + /* event_register() could have failed for some + * reason, implying that the new_sock cannot be + * added to the epoll set. If we won't get any + * more notifications for new_sock from epoll, + * then we better remove the corresponding + * new_trans object from the RPCSVC service list. + * Since we've notified RPC service of new_trans + * before we attempted event_register(), we better + * unlink the new_trans from the RPCSVC service list + * to cleanup the stateby sending out a DISCONNECT + * notification. + */ + rpc_transport_notify(this, RPC_TRANSPORT_DISCONNECT, + new_trans); + } + } + + /* this rpc_transport_unref() is for managing race between + * 1. socket_server_event_handler and + * 2. socket_event_handler + * trying to add and remove new_trans from the rpcsvc + * service list + * now that we are done with the notifications, lets + * reduce the reference + */ + rpc_transport_unref(new_trans); + } + + if (ret < 0) { + gf_log(this->name, GF_LOG_WARNING, "closing newly accepted socket"); + sys_close(new_sock); + /* this unref is to actually cause the destruction of + * the new_trans since we've failed at everything so far + */ + rpc_transport_unref(new_trans); + } + } +out: + return; +} + +static int +socket_disconnect(rpc_transport_t *this, gf_boolean_t wait) +{ + socket_private_t *priv = NULL; + int ret = -1; + + priv = this->private; + + pthread_mutex_lock(&priv->out_lock); + { + ret = __socket_disconnect(this); + } + pthread_mutex_unlock(&priv->out_lock); + + return ret; +} + +void * +socket_connect_error_cbk(void *opaque) +{ + socket_connect_error_state_t *arg; + + GF_ASSERT(opaque); + + arg = opaque; + THIS = arg->this; + + rpc_transport_notify(arg->trans, RPC_TRANSPORT_DISCONNECT, arg->trans); + + if (arg->refd) + rpc_transport_unref(arg->trans); + + GF_FREE(opaque); + return NULL; +} + +static void +socket_fix_ssl_opts(rpc_transport_t *this, socket_private_t *priv, + uint16_t port) +{ + if (port == GF_DEFAULT_SOCKET_LISTEN_PORT) { + gf_log(this->name, GF_LOG_DEBUG, "%s SSL for portmapper connection", + priv->mgmt_ssl ? "enabling" : "disabling"); + priv->use_ssl = priv->mgmt_ssl; + } else if (priv->ssl_enabled && !priv->use_ssl) { + gf_log(this->name, GF_LOG_DEBUG, "re-enabling SSL for I/O connection"); + priv->use_ssl = _gf_true; + } +} + +/* + * If we might just be trying to connect prematurely, e.g. to a brick that's + * slow coming up, all we need is a simple retry. Don't worry about sleeping + * in some arbitrary thread. The connect(2) could already have the exact same + * effect, and we deal with it in that case so we can deal with it for sleep(2) + * as well. + */ +static int +connect_loop(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +{ + int ret; + int connect_fails = 0; + + for (;;) { + ret = connect(sockfd, addr, addrlen); + if (ret >= 0) { + break; + } + if ((errno != ENOENT) || (++connect_fails >= 5)) { + break; + } + sleep(1); + } + + return ret; +} + +static int +socket_connect(rpc_transport_t *this, int port) +{ + int ret = -1; + int th_ret = -1; + int sock = -1; + socket_private_t *priv = NULL; + socklen_t sockaddr_len = 0; + glusterfs_ctx_t *ctx = NULL; + sa_family_t sa_family = { + 0, + }; + char *local_addr = NULL; + union gf_sock_union sock_union; + struct sockaddr_in *addr = NULL; + gf_boolean_t refd = _gf_false; + socket_connect_error_state_t *arg = NULL; + pthread_t th_id = { + 0, + }; + gf_boolean_t ign_enoent = _gf_false; + gf_boolean_t connect_attempted = _gf_false; + + GF_VALIDATE_OR_GOTO("socket", this, err); + GF_VALIDATE_OR_GOTO("socket", this->private, err); + + priv = this->private; + ctx = this->ctx; + + if (!priv) { + gf_log_callingfn(this->name, GF_LOG_WARNING, + "connect() called on uninitialized transport"); + goto err; + } + + pthread_mutex_lock(&priv->out_lock); + { + if (priv->sock >= 0) { + gf_log_callingfn(this->name, GF_LOG_TRACE, + "connect () called on transport " + "already connected"); + errno = EINPROGRESS; + ret = -1; + goto unlock; + } + + gf_log(this->name, GF_LOG_TRACE, "connecting %p, sock=%d", this, + priv->sock); + + ret = socket_client_get_remote_sockaddr(this, &sock_union.sa, + &sockaddr_len, &sa_family); + if (ret < 0) { + /* logged inside client_get_remote_sockaddr */ + goto unlock; + } + + if (sa_family == AF_UNIX) { + priv->ssl_enabled = _gf_false; + priv->mgmt_ssl = _gf_false; + } else { + if (port > 0) { + sock_union.sin.sin_port = htons(port); + } + socket_fix_ssl_opts(this, priv, ntohs(sock_union.sin.sin_port)); + } + + memcpy(&this->peerinfo.sockaddr, &sock_union.storage, sockaddr_len); + this->peerinfo.sockaddr_len = sockaddr_len; + + priv->sock = sys_socket(sa_family, SOCK_STREAM, 0); + if (priv->sock < 0) { + gf_log(this->name, GF_LOG_ERROR, "socket creation failed (%s)", + strerror(errno)); + ret = -1; + goto unlock; + } + + /* Can't help if setting socket options fails. We can continue + * working nonetheless. + */ + if (priv->windowsize != 0) { + if (setsockopt(priv->sock, SOL_SOCKET, SO_RCVBUF, &priv->windowsize, + sizeof(priv->windowsize)) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setting receive window " + "size failed: %d: %d: %s", + priv->sock, priv->windowsize, strerror(errno)); + } + + if (setsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &priv->windowsize, + sizeof(priv->windowsize)) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setting send window size " + "failed: %d: %d: %s", + priv->sock, priv->windowsize, strerror(errno)); + } + } + + /* Make sure we are not vulnerable to someone setting + * net.ipv6.bindv6only to 1 so that gluster services are + * available over IPv4 & IPv6. + */ +#ifdef IPV6_DEFAULT + int disable_v6only = 0; + if (setsockopt(priv->sock, IPPROTO_IPV6, IPV6_V6ONLY, + (void *)&disable_v6only, sizeof(disable_v6only)) < 0) { + gf_log(this->name, GF_LOG_WARNING, + "Error disabling sockopt IPV6_V6ONLY: \"%s\"", + strerror(errno)); + } +#endif + + if (sa_family != AF_UNIX) { + if (priv->nodelay) { + ret = __socket_nodelay(priv->sock); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "NODELAY on %d failed (%s)", priv->sock, + strerror(errno)); + } + } + + if (priv->keepalive) { + ret = __socket_keepalive( + priv->sock, sa_family, priv->keepaliveintvl, + priv->keepaliveidle, priv->keepalivecnt, priv->timeout); + if (ret != 0) + gf_log(this->name, GF_LOG_ERROR, + "Failed to set keep-alive: %s", strerror(errno)); + } + } + + SA(&this->myinfo.sockaddr)->sa_family = SA(&this->peerinfo.sockaddr) + ->sa_family; + + /* If a source addr is explicitly specified, use it */ + ret = dict_get_str_sizen(this->options, "transport.socket.source-addr", + &local_addr); + if (!ret && SA(&this->myinfo.sockaddr)->sa_family == AF_INET) { + addr = (struct sockaddr_in *)(&this->myinfo.sockaddr); + ret = inet_pton(AF_INET, local_addr, &(addr->sin_addr.s_addr)); + } + + /* If client wants ENOENT to be ignored */ + ign_enoent = dict_get_str_boolean( + this->options, "transport.socket.ignore-enoent", _gf_false); + + ret = client_bind(this, SA(&this->myinfo.sockaddr), + &this->myinfo.sockaddr_len, priv->sock); + if (ret < 0) { + gf_log(this->name, GF_LOG_WARNING, "client bind failed: %s", + strerror(errno)); + goto handler; + } + + /* make socket non-blocking for all types of sockets */ + if (!priv->bio) { + ret = __socket_nonblock(priv->sock); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, "NBIO on %d failed (%s)", + priv->sock, strerror(errno)); + goto handler; + } else { + gf_log(this->name, GF_LOG_TRACE, + ">>> connect() with non-blocking IO for ALL"); + } + } + this->connect_failed = _gf_false; + priv->connect_failed = 0; + priv->connected = 0; + + socket_dump_info(SA(&this->peerinfo.sockaddr), priv->is_server, + priv->use_ssl, priv->sock, this->name, + "connecting to"); + + if (ign_enoent) { + ret = connect_loop(priv->sock, SA(&this->peerinfo.sockaddr), + this->peerinfo.sockaddr_len); + } else { + ret = connect(priv->sock, SA(&this->peerinfo.sockaddr), + this->peerinfo.sockaddr_len); + } + + connect_attempted = _gf_true; + + if ((ret != 0) && (errno == ENOENT) && ign_enoent) { + gf_log(this->name, GF_LOG_WARNING, + "Ignore failed connection attempt on %s, (%s) ", + this->peerinfo.identifier, strerror(errno)); + + /* connect failed with some other error than EINPROGRESS + so, getsockopt (... SO_ERROR ...), will not catch any + errors and return them to us, we need to remember this + state, and take actions in socket_event_handler + appropriately */ + /* TBD: What about ENOENT, we will do getsockopt there + as well, so how is that exempt from such a problem? */ + priv->connect_failed = 1; + this->connect_failed = _gf_true; + + goto handler; + } + + if ((ret != 0) && (errno != EINPROGRESS) && (errno != ENOENT)) { + /* For unix path based sockets, the socket path is + * cryptic (md5sum of path) and may not be useful for + * the user in debugging so log it in DEBUG + */ + gf_log(this->name, + ((sa_family == AF_UNIX) ? GF_LOG_DEBUG : GF_LOG_ERROR), + "connection attempt on %s failed, (%s)", + this->peerinfo.identifier, strerror(errno)); + + /* connect failed with some other error than EINPROGRESS + so, getsockopt (... SO_ERROR ...), will not catch any + errors and return them to us, we need to remember this + state, and take actions in socket_event_handler + appropriately */ + /* TBD: What about ENOENT, we will do getsockopt there + as well, so how is that exempt from such a problem? */ + priv->connect_failed = 1; + + goto handler; + } else { + /* reset connect_failed so that any previous attempts + state is not carried forward */ + priv->connect_failed = 0; + ret = 0; + } + + handler: + if (ret < 0 && !connect_attempted) { + /* Ignore error from connect. epoll events + should be handled in the socket handler. shutdown(2) + will result in EPOLLERR, so cleanup is done in + socket_event_handler or socket_poller */ + shutdown(priv->sock, SHUT_RDWR); + ret = 0; + gf_log(this->name, GF_LOG_INFO, + "intentional client shutdown(%d, SHUT_RDWR)", priv->sock); + } + + priv->connected = 0; + priv->is_server = _gf_false; + rpc_transport_ref(this); + refd = _gf_true; + + this->listener = this; + priv->idx = gf_event_register(ctx->event_pool, priv->sock, + socket_event_handler, this, 1, 1, + this->notify_poller_death); + if (priv->idx == -1) { + gf_log("", GF_LOG_WARNING, + "failed to register the event; " + "closing socket %d", + priv->sock); + sys_close(priv->sock); + priv->sock = -1; + ret = -1; + } + + unlock: + sock = priv->sock; + } + pthread_mutex_unlock(&priv->out_lock); + +err: + /* if sock >= 0, then cleanup is done from the event handler */ + if ((ret < 0) && (sock < 0)) { + /* Cleaup requires to send notification to upper layer which + intern holds the big_lock. There can be dead-lock situation + if big_lock is already held by the current thread. + So transfer the ownership to separate thread for cleanup. + */ + arg = GF_CALLOC(1, sizeof(*arg), gf_sock_connect_error_state_t); + arg->this = THIS; + arg->trans = this; + arg->refd = refd; + th_ret = gf_thread_create_detached(&th_id, socket_connect_error_cbk, + arg, "scleanup"); + if (th_ret) { + /* Error will be logged by gf_thread_create_attached */ + gf_log(this->name, GF_LOG_ERROR, + "Thread creation " + "failed"); + GF_FREE(arg); + GF_ASSERT(0); + } + + ret = 0; + } + + return ret; +} + +static int +socket_listen(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + int ret = -1; + int sock = -1; + struct sockaddr_storage sockaddr; + socklen_t sockaddr_len = 0; + peer_info_t *myinfo = NULL; + glusterfs_ctx_t *ctx = NULL; + sa_family_t sa_family = { + 0, + }; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + myinfo = &this->myinfo; + ctx = this->ctx; + + pthread_mutex_lock(&priv->out_lock); + { + sock = priv->sock; + } + pthread_mutex_unlock(&priv->out_lock); + + if (sock >= 0) { + gf_log_callingfn(this->name, GF_LOG_DEBUG, "already listening"); + return ret; + } + + ret = socket_server_get_local_sockaddr(this, SA(&sockaddr), &sockaddr_len, + &sa_family); + if (ret < 0) { + return ret; + } + + pthread_mutex_lock(&priv->out_lock); + { + if (priv->sock >= 0) { + gf_log(this->name, GF_LOG_DEBUG, "already listening"); + goto unlock; + } + + memcpy(&myinfo->sockaddr, &sockaddr, sockaddr_len); + myinfo->sockaddr_len = sockaddr_len; + + priv->sock = sys_socket(sa_family, SOCK_STREAM, 0); + + if (priv->sock < 0) { + gf_log(this->name, GF_LOG_ERROR, "socket creation failed (%s)", + strerror(errno)); + goto unlock; + } + + /* Can't help if setting socket options fails. We can continue + * working nonetheless. + */ + if (priv->windowsize != 0) { + if (setsockopt(priv->sock, SOL_SOCKET, SO_RCVBUF, &priv->windowsize, + sizeof(priv->windowsize)) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setting receive window size " + "failed: %d: %d: %s", + priv->sock, priv->windowsize, strerror(errno)); + } + + if (setsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &priv->windowsize, + sizeof(priv->windowsize)) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setting send window size failed:" + " %d: %d: %s", + priv->sock, priv->windowsize, strerror(errno)); + } + } + + if (priv->nodelay && (sa_family != AF_UNIX)) { + ret = __socket_nodelay(priv->sock); + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "setsockopt() failed for NODELAY (%s)", strerror(errno)); + } + } + + if (!priv->bio) { + ret = __socket_nonblock(priv->sock); + + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "NBIO on socket %d failed " + "(errno:%s); closing socket", + priv->sock, strerror(errno)); + sys_close(priv->sock); + priv->sock = -1; + goto unlock; + } + } + + /* coverity[SLEEP] */ + ret = __socket_server_bind(this); + + if (ret < 0) { + /* logged inside __socket_server_bind() */ + gf_log(this->name, GF_LOG_ERROR, + "__socket_server_bind failed;" + "closing socket %d", + priv->sock); + sys_close(priv->sock); + priv->sock = -1; + goto unlock; + } + + socket_dump_info(SA(&this->myinfo.sockaddr), priv->is_server, + priv->use_ssl, priv->sock, this->name, "listening on"); + + ret = listen(priv->sock, priv->backlog); + + if (ret != 0) { + gf_log(this->name, GF_LOG_ERROR, + "could not set socket %d to listen mode (errno:%s); " + "closing socket", + priv->sock, strerror(errno)); + sys_close(priv->sock); + priv->sock = -1; + goto unlock; + } + + rpc_transport_ref(this); + + priv->idx = gf_event_register(ctx->event_pool, priv->sock, + socket_server_event_handler, this, 1, 0, + this->notify_poller_death); + + if (priv->idx == -1) { + gf_log(this->name, GF_LOG_WARNING, + "could not register socket %d with events; " + "closing socket", + priv->sock); + ret = -1; + sys_close(priv->sock); + priv->sock = -1; + goto unlock; + } + } +unlock: + pthread_mutex_unlock(&priv->out_lock); + +out: + return ret; +} + +static int32_t +socket_submit_outgoing_msg(rpc_transport_t *this, rpc_transport_msg_t *msg) +{ + int ret = -1; + char need_poll_out = 0; + char need_append = 1; + struct ioq *entry = NULL; + glusterfs_ctx_t *ctx = NULL; + socket_private_t *priv = NULL; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + ctx = this->ctx; + + pthread_mutex_lock(&priv->out_lock); + { + if (priv->connected != 1) { + if (!priv->submit_log && !priv->connect_finish_log) { + gf_log(this->name, GF_LOG_INFO, + "not connected (priv->connected = %d)", priv->connected); + priv->submit_log = 1; + } + goto unlock; + } + + priv->submit_log = 0; + entry = __socket_ioq_new(this, msg); + if (!entry) + goto unlock; + + if (list_empty(&priv->ioq)) { + ret = __socket_ioq_churn_entry(this, entry); + + if (ret == 0) { + need_append = 0; + } + if (ret > 0) { + need_poll_out = 1; + } + } + + if (need_append) { + list_add_tail(&entry->list, &priv->ioq); + ret = 0; + } + if (need_poll_out) { + /* first entry to wait. continue writing on POLLOUT */ + priv->idx = gf_event_select_on(ctx->event_pool, priv->sock, + priv->idx, -1, 1); + } + } +unlock: + pthread_mutex_unlock(&priv->out_lock); + +out: + return ret; +} + +static int32_t +socket_submit_request(rpc_transport_t *this, rpc_transport_req_t *req) +{ + return socket_submit_outgoing_msg(this, &req->msg); +} + +static int32_t +socket_submit_reply(rpc_transport_t *this, rpc_transport_reply_t *reply) +{ + return socket_submit_outgoing_msg(this, &reply->msg); +} + +static int32_t +socket_getpeername(rpc_transport_t *this, char *hostname, int hostlen) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", hostname, out); + + if (hostlen < (strlen(this->peerinfo.identifier) + 1)) { + goto out; + } + + strcpy(hostname, this->peerinfo.identifier); + ret = 0; +out: + return ret; +} + +static int32_t +socket_getpeeraddr(rpc_transport_t *this, char *peeraddr, int addrlen, + struct sockaddr_storage *sa, socklen_t salen) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", sa, out); + ret = 0; + + *sa = this->peerinfo.sockaddr; + + if (peeraddr != NULL) { + ret = socket_getpeername(this, peeraddr, addrlen); + } +out: + return ret; +} + +static int32_t +socket_getmyname(rpc_transport_t *this, char *hostname, int hostlen) +{ + int32_t ret = -1; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", hostname, out); + + if (hostlen < (strlen(this->myinfo.identifier) + 1)) { + goto out; + } + + strcpy(hostname, this->myinfo.identifier); + ret = 0; +out: + return ret; +} + +static int32_t +socket_getmyaddr(rpc_transport_t *this, char *myaddr, int addrlen, + struct sockaddr_storage *sa, socklen_t salen) +{ + int32_t ret = 0; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", sa, out); + + *sa = this->myinfo.sockaddr; + + if (myaddr != NULL) { + ret = socket_getmyname(this, myaddr, addrlen); + } + +out: + return ret; +} + +static int +socket_throttle(rpc_transport_t *this, gf_boolean_t onoff) +{ + socket_private_t *priv = NULL; + + priv = this->private; + + /* The way we implement throttling is by taking off + POLLIN event from the polled flags. This way we + never get called with the POLLIN event and therefore + will never read() any more data until throttling + is turned off. + */ + pthread_mutex_lock(&priv->out_lock); + { + /* Throttling is useless on a disconnected transport. In fact, + * it's dangerous since priv->idx and priv->sock are set to -1 + * on a disconnected transport, which breaks epoll's event to + * registered fd mapping. */ + + if (priv->connected == 1) + priv->idx = gf_event_select_on(this->ctx->event_pool, priv->sock, + priv->idx, (int)!onoff, -1); + } + pthread_mutex_unlock(&priv->out_lock); + return 0; +} + +struct rpc_transport_ops tops = { + .listen = socket_listen, + .connect = socket_connect, + .disconnect = socket_disconnect, + .submit_request = socket_submit_request, + .submit_reply = socket_submit_reply, + .get_peername = socket_getpeername, + .get_peeraddr = socket_getpeeraddr, + .get_myname = socket_getmyname, + .get_myaddr = socket_getmyaddr, + .throttle = socket_throttle, +}; + +int +reconfigure(rpc_transport_t *this, dict_t *options) +{ + socket_private_t *priv = NULL; + gf_boolean_t tmp_bool = _gf_false; + char *optstr = NULL; + int ret = -1; + uint32_t backlog = 0; + uint64_t windowsize = 0; + data_t *data; + + GF_VALIDATE_OR_GOTO("socket", this, out); + GF_VALIDATE_OR_GOTO("socket", this->private, out); + + priv = this->private; + + if (dict_get_str_sizen(options, "transport.socket.keepalive", &optstr) == + 0) { + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "'transport.socket.keepalive' takes only " + "boolean options, not taking any action"); + priv->keepalive = 1; + goto out; + } + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.socket.keepalive"); + + priv->keepalive = tmp_bool; + } else + priv->keepalive = 1; + + if (dict_get_int32_sizen(options, "transport.tcp-user-timeout", + &(priv->timeout)) != 0) + priv->timeout = GF_NETWORK_TIMEOUT; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.tcp-user-timeout=%d", priv->timeout); + + if (dict_get_uint32(options, "transport.listen-backlog", &backlog) == 0) { + priv->backlog = backlog; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.listen-backlog=%d", priv->backlog); + } + + if (priv->keepalive) { + if (dict_get_int32_sizen(options, "transport.socket.keepalive-time", + &(priv->keepaliveidle)) != 0) + priv->keepaliveidle = GF_KEEPALIVE_TIME; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.socket.keepalive-time=%d", + priv->keepaliveidle); + + if (dict_get_int32_sizen(options, "transport.socket.keepalive-interval", + &(priv->keepaliveintvl)) != 0) + priv->keepaliveintvl = GF_KEEPALIVE_INTERVAL; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.socket.keepalive-interval=%d", + priv->keepaliveintvl); + + if (dict_get_int32_sizen(options, "transport.socket.keepalive-count", + &(priv->keepalivecnt)) != 0) + priv->keepalivecnt = GF_KEEPALIVE_COUNT; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.socket.keepalive-count=%d", + priv->keepalivecnt); + } + + optstr = NULL; + if (dict_get_str_sizen(options, "tcp-window-size", &optstr) == 0) { + if (gf_string2uint64(optstr, &windowsize) != 0) { + gf_log(this->name, GF_LOG_ERROR, "invalid number format: %s", + optstr); + goto out; + } + } + + priv->windowsize = (int)windowsize; + + data = dict_get_sizen(options, "non-blocking-io"); + if (data) { + optstr = data_to_str(data); + + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "'non-blocking-io' takes only boolean options," + " not taking any action"); + tmp_bool = 1; + } + + if (!tmp_bool) { + priv->bio = 1; + gf_log(this->name, GF_LOG_WARNING, "disabling non-blocking IO"); + } + } + + if (!priv->bio) { + ret = __socket_nonblock(priv->sock); + if (ret != 0) { + gf_log(this->name, GF_LOG_WARNING, "NBIO on %d failed (%s)", + priv->sock, strerror(errno)); + goto out; + } + } + + ret = 0; +out: + return ret; +} + +#if OPENSSL_VERSION_NUMBER < 0x1010000f +static pthread_mutex_t *lock_array = NULL; + +static void +locking_func(int mode, int type, const char *file, int line) +{ + if (mode & CRYPTO_UNLOCK) { + pthread_mutex_unlock(&lock_array[type]); + } else { + pthread_mutex_lock(&lock_array[type]); + } +} + +#if OPENSSL_VERSION_NUMBER >= 0x1000000f +static void +threadid_func(CRYPTO_THREADID *id) +{ + /* + * We're not supposed to know whether a pthread_t is a number or a + * pointer, but we definitely need an unsigned long. Even though it + * happens to be an unsigned long already on Linux, do the cast just in + * case that's not so on another platform. Note that this can still + * break if any platforms are left where a pointer is larger than an + * unsigned long. In that case there's not much we can do; hopefully + * anyone porting to such a platform will be aware enough to notice the + * compile warnings about truncating the pointer value. + */ + CRYPTO_THREADID_set_numeric(id, (unsigned long)pthread_self()); +} +#else /* older openssl */ +static unsigned long +legacy_threadid_func(void) +{ + /* See comments above, it applies here too. */ + return (unsigned long)pthread_self(); +} +#endif /* OPENSSL_VERSION_NUMBER >= 0x1000000f */ +#endif /* OPENSSL_VERSION_NUMBER < 0x1010000f */ + +static void +init_openssl_mt(void) +{ + static gf_boolean_t initialized = _gf_false; + + if (initialized) { + /* this only needs to be initialized once GLOBALLY no + matter how many translators/sockets we end up with. */ + return; + } + + SSL_library_init(); + SSL_load_error_strings(); + + initialized = _gf_true; + +#if OPENSSL_VERSION_NUMBER < 0x1010000f + int num_locks = CRYPTO_num_locks(); + int i; + + lock_array = GF_CALLOC(num_locks, sizeof(pthread_mutex_t), + gf_sock_mt_lock_array); + if (lock_array) { + for (i = 0; i < num_locks; ++i) { + pthread_mutex_init(&lock_array[i], NULL); + } +#if OPENSSL_VERSION_NUMBER >= 0x1000000f + CRYPTO_THREADID_set_callback(threadid_func); +#else /* older openssl */ + CRYPTO_set_id_callback(legacy_threadid_func); +#endif + CRYPTO_set_locking_callback(locking_func); + } +#endif +} + +static void __attribute__((destructor)) fini_openssl_mt(void) +{ +#if OPENSSL_VERSION_NUMBER < 0x1010000f + int i; + + if (!lock_array) { + return; + } + + CRYPTO_set_locking_callback(NULL); +#if OPENSSL_VERSION_NUMBER >= 0x1000000f + CRYPTO_THREADID_set_callback(NULL); +#else /* older openssl */ + CRYPTO_set_id_callback(NULL); +#endif + + for (i = 0; i < CRYPTO_num_locks(); ++i) { + pthread_mutex_destroy(&lock_array[i]); + } + + GF_FREE(lock_array); + lock_array = NULL; +#endif + + ERR_free_strings(); +} + +/* The function returns 0 if AES bit is enabled on the CPU */ +static int +ssl_check_aes_bit(void) +{ + FILE *fp = fopen("/proc/cpuinfo", "r"); + int ret = 1; + size_t len = 0; + char *line = NULL; + char *match = NULL; + + GF_ASSERT(fp != NULL); + + while (getline(&line, &len, fp) > 0) { + if (!strncmp(line, "flags", 5)) { + match = strstr(line, " aes"); + if ((match != NULL) && ((match[4] == ' ') || (match[4] == 0))) { + ret = 0; + break; + } + } + } + + free(line); + fclose(fp); + + return ret; +} + +static int +ssl_setup_connection_params(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + char *optstr = NULL; + static int session_id = 1; + int32_t cert_depth = DEFAULT_VERIFY_DEPTH; + char *cipher_list = DEFAULT_CIPHER_LIST; + char *dh_param = DEFAULT_DH_PARAM; + char *ec_curve = DEFAULT_EC_CURVE; + gf_boolean_t dh_flag = _gf_false; + + priv = this->private; + + if (priv->ssl_ctx != NULL) { + gf_log(this->name, GF_LOG_TRACE, "found old SSL context!"); + return 0; + } + + if (!priv->ssl_enabled && !priv->mgmt_ssl) { + return 0; + } + + if (!ssl_check_aes_bit()) { + cipher_list = "AES128:" DEFAULT_CIPHER_LIST; + } + + priv->ssl_own_cert = DEFAULT_CERT_PATH; + if (dict_get_str_sizen(this->options, SSL_OWN_CERT_OPT, &optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name, GF_LOG_WARNING, + "%s specified without %s (ignored)", SSL_OWN_CERT_OPT, + SSL_ENABLED_OPT); + } + priv->ssl_own_cert = optstr; + } + priv->ssl_own_cert = gf_strdup(priv->ssl_own_cert); + + priv->ssl_private_key = DEFAULT_KEY_PATH; + if (dict_get_str_sizen(this->options, SSL_PRIVATE_KEY_OPT, &optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name, GF_LOG_WARNING, + "%s specified without %s (ignored)", SSL_PRIVATE_KEY_OPT, + SSL_ENABLED_OPT); + } + priv->ssl_private_key = optstr; + } + priv->ssl_private_key = gf_strdup(priv->ssl_private_key); + + priv->ssl_ca_list = DEFAULT_CA_PATH; + if (dict_get_str_sizen(this->options, SSL_CA_LIST_OPT, &optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name, GF_LOG_WARNING, + "%s specified without %s (ignored)", SSL_CA_LIST_OPT, + SSL_ENABLED_OPT); + } + priv->ssl_ca_list = optstr; + } + priv->ssl_ca_list = gf_strdup(priv->ssl_ca_list); + + optstr = NULL; + if (dict_get_str_sizen(this->options, SSL_CRL_PATH_OPT, &optstr) == 0) { + if (!priv->ssl_enabled) { + gf_log(this->name, GF_LOG_WARNING, + "%s specified without %s (ignored)", SSL_CRL_PATH_OPT, + SSL_ENABLED_OPT); + } + if (strcasecmp(optstr, "NULL") == 0) + priv->crl_path = NULL; + else + priv->crl_path = gf_strdup(optstr); + } + + if (!priv->mgmt_ssl) { + if (!dict_get_int32_sizen(this->options, SSL_CERT_DEPTH_OPT, + &cert_depth)) { + } + } else { + cert_depth = this->ctx->ssl_cert_depth; + } + gf_log(this->name, priv->ssl_enabled ? GF_LOG_INFO : GF_LOG_DEBUG, + "SSL support for MGMT is %s IO path is %s certificate depth is %d " + "for peer %s", + (priv->mgmt_ssl ? "ENABLED" : "NOT enabled"), + (priv->ssl_enabled ? "ENABLED" : "NOT enabled"), cert_depth, + this->peerinfo.identifier); + + if (!dict_get_str_sizen(this->options, SSL_CIPHER_LIST_OPT, &cipher_list)) { + gf_log(this->name, GF_LOG_INFO, "using cipher list %s", cipher_list); + } + if (!dict_get_str_sizen(this->options, SSL_DH_PARAM_OPT, &dh_param)) { + dh_flag = _gf_true; + gf_log(this->name, GF_LOG_INFO, "using DH parameters %s", dh_param); + } + if (!dict_get_str_sizen(this->options, SSL_EC_CURVE_OPT, &ec_curve)) { + gf_log(this->name, GF_LOG_INFO, "using EC curve %s", ec_curve); + } + + if (priv->ssl_enabled || priv->mgmt_ssl) { + BIO *bio = NULL; + +#if HAVE_TLS_METHOD + priv->ssl_meth = (SSL_METHOD *)TLS_method(); +#elif HAVE_TLSV1_2_METHOD + priv->ssl_meth = (SSL_METHOD *)TLSv1_2_method(); +#else +/* + * Nobody should use an OpenSSL so old it does not support TLS 1.2. + * If that is really required, build with -DUSE_INSECURE_OPENSSL + */ +#ifndef USE_INSECURE_OPENSSL +#error Old and insecure OpenSSL, use -DUSE_INSECURE_OPENSSL to use it anyway +#endif + /* SSLv23_method uses highest available protocol */ + priv->ssl_meth = SSLv23_method(); +#endif + priv->ssl_ctx = SSL_CTX_new(priv->ssl_meth); + + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_SSLv2); + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_SSLv3); +#ifdef SSL_OP_NO_TICKET + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_TICKET); +#endif +#ifdef SSL_OP_NO_COMPRESSION + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_NO_COMPRESSION); +#endif + /* Upload file to bio wrapper only if dh param is configured + */ + if (dh_flag) { + if ((bio = BIO_new_file(dh_param, "r")) == NULL) { + gf_log(this->name, GF_LOG_ERROR, + "failed to open %s, " + "DH ciphers are disabled", + dh_param); + } + } + + if (bio != NULL) { +#ifdef HAVE_OPENSSL_DH_H + DH *dh; + unsigned long err; + + dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); + BIO_free(bio); + if (dh != NULL) { + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_SINGLE_DH_USE); + SSL_CTX_set_tmp_dh(priv->ssl_ctx, dh); + DH_free(dh); + } else { + err = ERR_get_error(); + gf_log(this->name, GF_LOG_ERROR, + "failed to read DH param from %s: %s " + "DH ciphers are disabled.", + dh_param, ERR_error_string(err, NULL)); + } +#else /* HAVE_OPENSSL_DH_H */ + BIO_free(bio); + gf_log(this->name, GF_LOG_ERROR, "OpenSSL has no DH support"); +#endif /* HAVE_OPENSSL_DH_H */ + } + + if (ec_curve != NULL) { +#ifdef HAVE_OPENSSL_ECDH_H + EC_KEY *ecdh = NULL; + int nid; + unsigned long err; + + nid = OBJ_sn2nid(ec_curve); + if (nid != 0) + ecdh = EC_KEY_new_by_curve_name(nid); + + if (ecdh != NULL) { + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_SINGLE_ECDH_USE); + SSL_CTX_set_tmp_ecdh(priv->ssl_ctx, ecdh); + EC_KEY_free(ecdh); + } else { + err = ERR_get_error(); + gf_log(this->name, GF_LOG_ERROR, + "failed to load EC curve %s: %s. " + "ECDH ciphers are disabled.", + ec_curve, ERR_error_string(err, NULL)); + } +#else /* HAVE_OPENSSL_ECDH_H */ + gf_log(this->name, GF_LOG_ERROR, "OpenSSL has no ECDH support"); +#endif /* HAVE_OPENSSL_ECDH_H */ + } + + /* This must be done after DH and ECDH setups */ + if (SSL_CTX_set_cipher_list(priv->ssl_ctx, cipher_list) == 0) { + gf_log(this->name, GF_LOG_ERROR, + "failed to find any valid ciphers"); + goto err; + } + + SSL_CTX_set_options(priv->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); + + if (!SSL_CTX_use_certificate_chain_file(priv->ssl_ctx, + priv->ssl_own_cert)) { + gf_log(this->name, GF_LOG_ERROR, "could not load our cert at %s", + priv->ssl_own_cert); + ssl_dump_error_stack(this->name); + goto err; + } + + if (!SSL_CTX_use_PrivateKey_file(priv->ssl_ctx, priv->ssl_private_key, + SSL_FILETYPE_PEM)) { + gf_log(this->name, GF_LOG_ERROR, "could not load private key at %s", + priv->ssl_private_key); + ssl_dump_error_stack(this->name); + goto err; + } + + if (!SSL_CTX_load_verify_locations(priv->ssl_ctx, priv->ssl_ca_list, + priv->crl_path)) { + gf_log(this->name, GF_LOG_ERROR, "could not load CA list"); + goto err; + } + + SSL_CTX_set_verify_depth(priv->ssl_ctx, cert_depth); + + if (priv->crl_path) + ssl_set_crl_verify_flags(priv->ssl_ctx); + + priv->ssl_session_id = session_id++; + SSL_CTX_set_session_id_context(priv->ssl_ctx, + (void *)&priv->ssl_session_id, + sizeof(priv->ssl_session_id)); + + SSL_CTX_set_verify(priv->ssl_ctx, SSL_VERIFY_PEER, 0); + + /* + * Since glusterfs shares the same settings for client-side + * and server-side of SSL, we need to ignore any certificate + * usage specification (SSL client vs SSL server), otherwise + * SSL connexions will fail with 'unsupported cerritifcate" + */ + SSL_CTX_set_purpose(priv->ssl_ctx, X509_PURPOSE_ANY); + } + return 0; + +err: + return -1; +} + +static int +socket_init(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + gf_boolean_t tmp_bool = 0; + uint64_t windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE; + char *optstr = NULL; + data_t *data; + + if (this->private) { + gf_log_callingfn(this->name, GF_LOG_ERROR, "double init attempted"); + return -1; + } + + priv = GF_CALLOC(1, sizeof(*priv), gf_common_mt_socket_private_t); + if (!priv) { + return -1; + } + + this->private = priv; + pthread_mutex_init(&priv->out_lock, NULL); + pthread_mutex_init(&priv->cond_lock, NULL); + pthread_cond_init(&priv->cond, NULL); + + /*GF_REF_INIT (priv, socket_poller_mayday);*/ + + priv->sock = -1; + priv->idx = -1; + priv->connected = -1; + priv->nodelay = 1; + priv->bio = 0; + priv->ssl_accepted = _gf_false; + priv->ssl_connected = _gf_false; + priv->windowsize = GF_DEFAULT_SOCKET_WINDOW_SIZE; + INIT_LIST_HEAD(&priv->ioq); + pthread_mutex_init(&priv->notify.lock, NULL); + pthread_cond_init(&priv->notify.cond, NULL); + + /* All the below section needs 'this->options' to be present */ + if (!this->options) + goto out; + + data = dict_get_sizen(this->options, "non-blocking-io"); + if (data) { + optstr = data_to_str(data); + + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "'non-blocking-io' takes only boolean options," + " not taking any action"); + tmp_bool = 1; + } + + if (!tmp_bool) { + priv->bio = 1; + gf_log(this->name, GF_LOG_WARNING, "disabling non-blocking IO"); + } + } + + optstr = NULL; + + /* By default, we enable NODELAY */ + data = dict_get_sizen(this->options, "transport.socket.nodelay"); + if (data) { + optstr = data_to_str(data); + + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "'transport.socket.nodelay' takes only " + "boolean options, not taking any action"); + tmp_bool = 1; + } + if (!tmp_bool) { + priv->nodelay = 0; + gf_log(this->name, GF_LOG_DEBUG, "disabling nodelay"); + } + } + + optstr = NULL; + if (dict_get_str_sizen(this->options, "tcp-window-size", &optstr) == 0) { + if (gf_string2uint64(optstr, &windowsize) != 0) { + gf_log(this->name, GF_LOG_ERROR, "invalid number format: %s", + optstr); + return -1; + } + } + + priv->windowsize = (int)windowsize; + + optstr = NULL; + /* Enable Keep-alive by default. */ + priv->keepalive = 1; + priv->keepaliveintvl = GF_KEEPALIVE_INTERVAL; + priv->keepaliveidle = GF_KEEPALIVE_TIME; + priv->keepalivecnt = GF_KEEPALIVE_COUNT; + if (dict_get_str_sizen(this->options, "transport.socket.keepalive", + &optstr) == 0) { + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "'transport.socket.keepalive' takes only " + "boolean options, not taking any action"); + tmp_bool = 1; + } + + if (!tmp_bool) + priv->keepalive = 0; + } + + if (dict_get_int32_sizen(this->options, "transport.tcp-user-timeout", + &(priv->timeout)) != 0) + priv->timeout = GF_NETWORK_TIMEOUT; + gf_log(this->name, GF_LOG_DEBUG, "Configured transport.tcp-user-timeout=%d", + priv->timeout); + + if (priv->keepalive) { + if (dict_get_int32_sizen(this->options, + "transport.socket.keepalive-time", + &(priv->keepaliveidle)) != 0) { + priv->keepaliveidle = GF_KEEPALIVE_TIME; + } + + if (dict_get_int32_sizen(this->options, + "transport.socket.keepalive-interval", + &(priv->keepaliveintvl)) != 0) { + priv->keepaliveintvl = GF_KEEPALIVE_INTERVAL; + } + + if (dict_get_int32_sizen(this->options, + "transport.socket.keepalive-count", + &(priv->keepalivecnt)) != 0) + priv->keepalivecnt = GF_KEEPALIVE_COUNT; + gf_log(this->name, GF_LOG_DEBUG, + "Reconfigured transport.keepalivecnt=%d", priv->keepalivecnt); + } + + if (dict_get_uint32(this->options, "transport.listen-backlog", + &(priv->backlog)) != 0) { + priv->backlog = GLUSTERFS_SOCKET_LISTEN_BACKLOG; + } + + optstr = NULL; + + /* Check if socket read failures are to be logged */ + priv->read_fail_log = 1; + data = dict_get_sizen(this->options, "transport.socket.read-fail-log"); + if (data) { + optstr = data_to_str(data); + if (gf_string2boolean(optstr, &tmp_bool) != 0) { + gf_log(this->name, GF_LOG_WARNING, + "'transport.socket.read-fail-log' takes only " + "boolean options; logging socket read fails"); + } else if (tmp_bool == _gf_false) { + priv->read_fail_log = 0; + } + } + + priv->windowsize = (int)windowsize; + + priv->ssl_enabled = _gf_false; + if (dict_get_str_sizen(this->options, SSL_ENABLED_OPT, &optstr) == 0) { + if (gf_string2boolean(optstr, &priv->ssl_enabled) != 0) { + gf_log(this->name, GF_LOG_ERROR, + "invalid value given for ssl-enabled boolean"); + } + } + priv->mgmt_ssl = this->ctx->secure_mgmt; + priv->srvr_ssl = this->ctx->secure_srvr; + + ssl_setup_connection_params(this); +out: + this->private = priv; + return 0; +} + +void +fini(rpc_transport_t *this) +{ + socket_private_t *priv = NULL; + + if (!this) + return; + + priv = this->private; + if (priv) { + if (priv->sock >= 0) { + pthread_mutex_lock(&priv->out_lock); + { + __socket_ioq_flush(priv); + __socket_reset(this); + } + pthread_mutex_unlock(&priv->out_lock); + } + gf_log(this->name, GF_LOG_TRACE, "transport %p destroyed", this); + + pthread_mutex_destroy(&priv->out_lock); + pthread_mutex_destroy(&priv->cond_lock); + pthread_cond_destroy(&priv->cond); + + GF_ASSERT(priv->notify.in_progress == 0); + pthread_mutex_destroy(&priv->notify.lock); + pthread_cond_destroy(&priv->notify.cond); + + if (priv->use_ssl && priv->ssl_ssl) { + SSL_clear(priv->ssl_ssl); + SSL_free(priv->ssl_ssl); + priv->ssl_ssl = NULL; + } + if (priv->ssl_ctx) { + SSL_CTX_free(priv->ssl_ctx); + priv->ssl_ctx = NULL; + } + + if (priv->ssl_private_key) { + GF_FREE(priv->ssl_private_key); + } + if (priv->ssl_own_cert) { + GF_FREE(priv->ssl_own_cert); + } + if (priv->ssl_ca_list) { + GF_FREE(priv->ssl_ca_list); + } + GF_FREE(priv); + } + + this->private = NULL; +} + +int32_t +init(rpc_transport_t *this) +{ + int ret = -1; + + init_openssl_mt(); + + ret = socket_init(this); + + if (ret < 0) { + gf_log(this->name, GF_LOG_DEBUG, "socket_init() failed"); + } + + return ret; +} + +struct volume_options options[] = { + {.key = {"remote-port", "transport.remote-port", + "transport.socket.remote-port"}, + .type = GF_OPTION_TYPE_INT}, + {.key = {"transport.socket.listen-port", "listen-port"}, + .type = GF_OPTION_TYPE_INT}, + {.key = {"transport.socket.bind-address", "bind-address"}, + .type = GF_OPTION_TYPE_INTERNET_ADDRESS}, + {.key = {"transport.socket.connect-path", "connect-path"}, + .type = GF_OPTION_TYPE_ANY}, + {.key = {"transport.socket.bind-path", "bind-path"}, + .type = GF_OPTION_TYPE_ANY}, + {.key = {"transport.socket.listen-path", "listen-path"}, + .type = GF_OPTION_TYPE_ANY}, + {.key = {"transport.address-family", "address-family"}, + .value = {"inet", "inet6", "unix", "inet-sdp"}, + .op_version = {GD_OP_VERSION_3_7_4}, + .type = GF_OPTION_TYPE_STR}, + {.key = {"non-blocking-io"}, .type = GF_OPTION_TYPE_BOOL}, + {.key = {"tcp-window-size"}, + .type = GF_OPTION_TYPE_SIZET, + .op_version = {1}, + .flags = OPT_FLAG_SETTABLE, + .description = "Option to set TCP SEND/RECV BUFFER SIZE", + .min = GF_MIN_SOCKET_WINDOW_SIZE, + .max = GF_MAX_SOCKET_WINDOW_SIZE}, + { + .key = {"transport.listen-backlog"}, + .type = GF_OPTION_TYPE_SIZET, + .op_version = {GD_OP_VERSION_3_11_1}, + .flags = OPT_FLAG_SETTABLE, + .description = "This option uses the value of backlog argument that " + "defines the maximum length to which the queue of " + "pending connections for socket fd may grow.", + .default_value = "1024", + }, + {.key = {"transport.tcp-user-timeout"}, + .type = GF_OPTION_TYPE_INT, + .op_version = {GD_OP_VERSION_3_10_2}, + .default_value = TOSTRING(GF_NETWORK_TIMEOUT)}, + {.key = {"transport.socket.nodelay"}, + .type = GF_OPTION_TYPE_BOOL, + .default_value = "1"}, + {.key = {"transport.socket.keepalive"}, + .type = GF_OPTION_TYPE_BOOL, + .op_version = {1}, + .default_value = "1"}, + {.key = {"transport.socket.keepalive-interval"}, + .type = GF_OPTION_TYPE_INT, + .op_version = {GD_OP_VERSION_3_10_2}, + .default_value = "2"}, + {.key = {"transport.socket.keepalive-time"}, + .type = GF_OPTION_TYPE_INT, + .op_version = {GD_OP_VERSION_3_10_2}, + .default_value = "20"}, + {.key = {"transport.socket.keepalive-count"}, + .type = GF_OPTION_TYPE_INT, + .op_version = {GD_OP_VERSION_3_10_2}, + .default_value = "9"}, + {.key = {"transport.socket.read-fail-log"}, .type = GF_OPTION_TYPE_BOOL}, + {.key = {SSL_ENABLED_OPT}, .type = GF_OPTION_TYPE_BOOL}, + {.key = {SSL_OWN_CERT_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_PRIVATE_KEY_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_CA_LIST_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_CERT_DEPTH_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_CIPHER_LIST_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_DH_PARAM_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_EC_CURVE_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {SSL_CRL_PATH_OPT}, .type = GF_OPTION_TYPE_STR}, + {.key = {OWN_THREAD_OPT}, .type = GF_OPTION_TYPE_BOOL}, + {.key = {"ssl-own-cert"}, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .type = GF_OPTION_TYPE_STR, + .description = "SSL certificate. Ignored if SSL is not enabled."}, + {.key = {"ssl-private-key"}, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .type = GF_OPTION_TYPE_STR, + .description = "SSL private key. Ignored if SSL is not enabled."}, + {.key = {"ssl-ca-list"}, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .type = GF_OPTION_TYPE_STR, + .description = "SSL CA list. Ignored if SSL is not enabled."}, + {.key = {"ssl-cert-depth"}, + .type = GF_OPTION_TYPE_INT, + .op_version = {GD_OP_VERSION_3_6_0}, + .flags = OPT_FLAG_SETTABLE, + .description = "Maximum certificate-chain depth. If zero, the " + "peer's certificate itself must be in the local " + "certificate list. Otherwise, there may be up to N " + "signing certificates between the peer's and the " + "local list. Ignored if SSL is not enabled."}, + {.key = {"ssl-cipher-list"}, + .type = GF_OPTION_TYPE_STR, + .op_version = {GD_OP_VERSION_3_6_0}, + .flags = OPT_FLAG_SETTABLE, + .description = "Allowed SSL ciphers. Ignored if SSL is not enabled."}, + {.key = {"ssl-dh-param"}, + .type = GF_OPTION_TYPE_STR, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .description = "DH parameters file. Ignored if SSL is not enabled."}, + {.key = {"ssl-ec-curve"}, + .type = GF_OPTION_TYPE_STR, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .description = "ECDH curve name. Ignored if SSL is not enabled."}, + {.key = {"ssl-crl-path"}, + .type = GF_OPTION_TYPE_STR, + .op_version = {GD_OP_VERSION_3_7_4}, + .flags = OPT_FLAG_SETTABLE, + .description = "Path to directory containing CRL. " + "Ignored if SSL is not enabled."}, + {.key = {NULL}}}; diff --git a/rpc/rpc-transport/socket/src/socket.h b/rpc/rpc-transport/socket/src/socket.h new file mode 100644 index 00000000000..8a2eda70605 --- /dev/null +++ b/rpc/rpc-transport/socket/src/socket.h @@ -0,0 +1,276 @@ +/* + Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _SOCKET_H +#define _SOCKET_H + +#include <openssl/ssl.h> +#include <openssl/err.h> +#include <openssl/x509v3.h> +#include <openssl/x509_vfy.h> +#ifdef HAVE_OPENSSL_DH_H +#include <openssl/dh.h> +#endif +#ifdef HAVE_OPENSSL_ECDH_H +#include <openssl/ecdh.h> +#endif + +#include "rpc-transport.h" + +#ifndef MAX_IOVEC +#define MAX_IOVEC 16 +#endif /* MAX_IOVEC */ + +#define GF_DEFAULT_SOCKET_LISTEN_PORT GF_DEFAULT_BASE_PORT + +#define RPC_MAX_FRAGMENT_SIZE 0x7fffffff + +/* The default window size will be 0, indicating not to set + * it to any size. Default size of Linux is found to be + * performance friendly. + * Linux allows us to over-ride the max values for the system. + * Should we over-ride them? Because if we set a value larger than the default + * setsockopt will fail. Having larger values might be beneficial for + * IB links. + */ +#define GF_DEFAULT_SOCKET_WINDOW_SIZE (0) +#define GF_MAX_SOCKET_WINDOW_SIZE (1 * GF_UNIT_MB) +#define GF_MIN_SOCKET_WINDOW_SIZE (0) +#define GF_USE_DEFAULT_KEEPALIVE (-1) + +#define GF_KEEPALIVE_TIME (20) +#define GF_KEEPALIVE_INTERVAL (2) +#define GF_KEEPALIVE_COUNT (9) + +typedef enum { + SP_STATE_NADA = 0, + SP_STATE_COMPLETE, + SP_STATE_READING_FRAGHDR, + SP_STATE_READ_FRAGHDR, + SP_STATE_READING_FRAG, +} sp_rpcrecord_state_t; + +typedef enum { + SP_STATE_RPCFRAG_INIT, + SP_STATE_READING_MSGTYPE, + SP_STATE_READ_MSGTYPE, + SP_STATE_NOTIFYING_XID +} sp_rpcfrag_state_t; + +typedef enum { + SP_STATE_SIMPLE_MSG_INIT, + SP_STATE_READING_SIMPLE_MSG, +} sp_rpcfrag_simple_msg_state_t; + +typedef enum { + SP_STATE_VECTORED_REQUEST_INIT, + SP_STATE_READING_CREDBYTES, + SP_STATE_READ_CREDBYTES, /* read credential data. */ + SP_STATE_READING_VERFBYTES, + SP_STATE_READ_VERFBYTES, /* read verifier data */ + SP_STATE_READING_PROGHDR, + SP_STATE_READ_PROGHDR, + SP_STATE_READING_PROGHDR_XDATA, + SP_STATE_READ_PROGHDR_XDATA, /* It's a bad "name" in the generic + RPC state machine, but greatly + aids code review (and xdata is + the only "consumer" of this state) + */ + SP_STATE_READING_PROG, +} sp_rpcfrag_vectored_request_state_t; + +typedef enum { + SP_STATE_REQUEST_HEADER_INIT, + SP_STATE_READING_RPCHDR1, + SP_STATE_READ_RPCHDR1, /* read msg from beginning till and + * including credlen + */ +} sp_rpcfrag_request_header_state_t; + +struct ioq { + union { + struct list_head list; + struct { + struct ioq *next; + struct ioq *prev; + }; + }; + + struct iovec vector[MAX_IOVEC]; + struct iovec *pending_vector; + int count; + int pending_count; + struct iobref *iobref; + uint32_t fraghdr; + char _pad[4]; +}; + +typedef struct { + sp_rpcfrag_request_header_state_t header_state; + sp_rpcfrag_vectored_request_state_t vector_state; + int vector_sizer_state; +} sp_rpcfrag_request_state_t; + +typedef enum { + SP_STATE_VECTORED_REPLY_STATUS_INIT, + SP_STATE_READING_REPLY_STATUS, + SP_STATE_READ_REPLY_STATUS, +} sp_rpcfrag_vectored_reply_status_state_t; + +typedef enum { + SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT, + SP_STATE_READING_PROC_HEADER, + SP_STATE_READING_PROC_OPAQUE, + SP_STATE_READ_PROC_OPAQUE, + SP_STATE_READ_PROC_HEADER, +} sp_rpcfrag_vectored_reply_accepted_success_state_t; + +typedef enum { + SP_STATE_ACCEPTED_REPLY_INIT, + SP_STATE_READING_REPLY_VERFLEN, + SP_STATE_READ_REPLY_VERFLEN, + SP_STATE_READING_REPLY_VERFBYTES, + SP_STATE_READ_REPLY_VERFBYTES, +} sp_rpcfrag_vectored_reply_accepted_state_t; + +typedef struct { + uint32_t accept_status; + sp_rpcfrag_vectored_reply_status_state_t status_state; + sp_rpcfrag_vectored_reply_accepted_state_t accepted_state; + sp_rpcfrag_vectored_reply_accepted_success_state_t accepted_success_state; +} sp_rpcfrag_vectored_reply_state_t; + +struct gf_sock_incoming_frag { + char *fragcurrent; + uint32_t bytes_read; + uint32_t remaining_size; + struct iovec vector; + struct iovec *pending_vector; + union { + sp_rpcfrag_request_state_t request; + sp_rpcfrag_vectored_reply_state_t reply; + } call_body; + + sp_rpcfrag_simple_msg_state_t simple_state; + sp_rpcfrag_state_t state; +}; + +#define GF_SOCKET_RA_MAX 1024 + +struct gf_sock_incoming { + char *proghdr_base_addr; + struct iobuf *iobuf; + size_t iobuf_size; + struct gf_sock_incoming_frag frag; + struct iovec vector[2]; + struct iovec payload_vector; + struct iobref *iobref; + rpc_request_info_t *request_info; + struct iovec *pending_vector; + int count; + int pending_count; + size_t total_bytes_read; + + size_t ra_read; + size_t ra_max; + size_t ra_served; + char *ra_buf; + uint32_t fraghdr; + msg_type_t msg_type; + sp_rpcrecord_state_t record_state; + char _pad[4]; +}; + +typedef struct { + union { + struct list_head ioq; + struct { + struct ioq *ioq_next; + struct ioq *ioq_prev; + }; + }; + pthread_mutex_t out_lock; + pthread_mutex_t cond_lock; + pthread_cond_t cond; + int windowsize; + int keepalive; + int keepaliveidle; + int keepaliveintvl; + int keepalivecnt; + int timeout; + int log_ctr; + int shutdown_log_ctr; + /* ssl_error_required is used only during the SSL connection setup + * phase. + * It holds the error code returned by SSL_get_error() and is used to + * arm the epoll event set for the required event for the specific fd. + */ + int ssl_error_required; + int ssl_session_id; + + GF_REF_DECL; /* refcount to keep track of socket_poller + threads */ + struct { + pthread_mutex_t lock; + pthread_cond_t cond; + uint64_t in_progress; + } notify; + int32_t sock; + int32_t idx; + int32_t gen; + uint32_t backlog; + SSL_METHOD *ssl_meth; + SSL_CTX *ssl_ctx; + BIO *ssl_sbio; + SSL *ssl_ssl; + char *ssl_own_cert; + char *ssl_private_key; + char *ssl_ca_list; + char *crl_path; + struct gf_sock_incoming incoming; + mgmt_ssl_t srvr_ssl; + /* -1 = not connected. 0 = in progress. 1 = connected */ + char connected; + /* 1 = connect failed for reasons other than EINPROGRESS/ENOENT + see socket_connect for details */ + char connect_failed; + char bio; + char connect_finish_log; + char submit_log; + char nodelay; + gf_boolean_t read_fail_log; + gf_boolean_t ssl_enabled; /* outbound I/O */ + gf_boolean_t mgmt_ssl; /* outbound mgmt */ + gf_boolean_t is_server; + gf_boolean_t use_ssl; + gf_boolean_t ssl_accepted; /* To indicate SSL_accept() */ + gf_boolean_t ssl_connected; /* or SSL_connect() has been + * been completed on this socket. + * These are valid only when + * use_ssl is true. + */ + /* SSL_CTX is created for each transport. Since we are now using non- + * blocking mechanism for SSL_accept() and SSL_connect(), the SSL + * context is created on the first EPOLLIN event which may lead to + * SSL_ERROR_WANT_READ/SSL_ERROR_WANT_WRITE and may not complete the + * SSL connection at the first attempt. + * ssl_context_created is a flag to note that we've created the SSL + * context for the connection so that we don't blindly create any more + * while !ssl_accepted or !ssl_connected. + */ + gf_boolean_t ssl_context_created; + gf_boolean_t accepted; /* explicit flag to be set in + * socket_event_handler() for + * newly accepted socket + */ + char _pad[4]; +} socket_private_t; + +#endif diff --git a/rpc/xdr/Makefile.am b/rpc/xdr/Makefile.am new file mode 100644 index 00000000000..af437a64d6d --- /dev/null +++ b/rpc/xdr/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/rpc/xdr/src/.gitignore b/rpc/xdr/src/.gitignore new file mode 100644 index 00000000000..a0c8b7ca2b6 --- /dev/null +++ b/rpc/xdr/src/.gitignore @@ -0,0 +1,25 @@ +acl3-xdr.c +acl3-xdr.h +changelog-xdr.c +changelog-xdr.h +cli1-xdr.c +cli1-xdr.h +glusterd1-xdr.c +glusterd1-xdr.h +glusterfs3-xdr.c +glusterfs3-xdr.h +glusterfs4-xdr.c +glusterfs4-xdr.h +mount3udp.c +mount3udp.h +nlm4-xdr.c +nlm4-xdr.h +nlmcbk-xdr.c +nlmcbk-xdr.h +nsm-xdr.c +nsm-xdr.h +portmap-xdr.c +portmap-xdr.h +rpc-common-xdr.c +rpc-common-xdr.h +*-e diff --git a/rpc/xdr/src/Makefile.am b/rpc/xdr/src/Makefile.am new file mode 100644 index 00000000000..0e9c377ec93 --- /dev/null +++ b/rpc/xdr/src/Makefile.am @@ -0,0 +1,85 @@ +if BUILD_GNFS + NFS_XDRS = nlm4-xdr.x nsm-xdr.x acl3-xdr.x mount3udp.x + NFS_SRCS = xdr-nfs3.c msg-nfs3.c + NFS_HDRS = xdr-nfs3.h msg-nfs3.h +else + NFS_EXTRA_XDRS = nlm4-xdr.x nsm-xdr.x acl3-xdr.x mount3udp.x +endif + +XDRGENFILES = glusterfs3-xdr.x glusterfs4-xdr.x cli1-xdr.x \ + rpc-common-xdr.x glusterd1-xdr.x changelog-xdr.x \ + portmap-xdr.x ${NFS_XDRS} + +XDRHEADERS = $(XDRGENFILES:.x=.h) +XDRSOURCES = $(XDRGENFILES:.x=.c) + +EXTRA_DIST = $(XDRGENFILES) libgfxdr.sym ${NFS_EXTRA_XDRS} + +lib_LTLIBRARIES = libgfxdr.la + +libgfxdr_la_CFLAGS = -Wall $(GF_CFLAGS) $(GF_DARWIN_LIBGLUSTERFS_CFLAGS) + +libgfxdr_la_CPPFLAGS = $(GF_CPPFLAGS) -D__USE_FILE_OFFSET64 \ + -I$(top_srcdir)/libglusterfs/src -I$(top_srcdir)/rpc/rpc-lib/src \ + -I$(top_builddir)/rpc/xdr/src + +libgfxdr_la_LIBADD = $(top_builddir)/libglusterfs/src/libglusterfs.la + +libgfxdr_la_LDFLAGS = -version-info $(LIBGFXDR_LT_VERSION) $(GF_LDFLAGS) \ + -export-symbols $(top_srcdir)/rpc/xdr/src/libgfxdr.sym + +libgfxdr_la_SOURCES = xdr-generic.c ${NFS_SRCS} +nodist_libgfxdr_la_SOURCES = $(XDRSOURCES) + +libgfxdr_la_HEADERS = xdr-generic.h glusterfs3.h rpc-pragmas.h ${NFS_HDRS} +nodist_libgfxdr_la_HEADERS = $(XDRHEADERS) + +libgfxdr_ladir = $(includedir)/glusterfs/rpc + +CLEANFILES = $(XDRSOURCES) $(XDRHEADERS) + +# trick automake into doing BUILT_SOURCES magic +BUILT_SOURCES = $(XDRHEADERS) $(XDRSOURCES) + +xdrsrc=$(top_srcdir)/rpc/xdr/src +xdrdst=$(top_builddir)/rpc/xdr/src + +# make's dependency resolution may mean that it decides to run +# rpcgen again (unnecessarily), but as the .c file already exists, +# rpcgen will exit with an error, resulting in a build error. We +# could use a '-' (i.e. -@rpcgen ...) and suffer with noisy warnings +# in the build. Or we do this crufty thing instead. +$(XDRSOURCES): $(XDRGENFILES) + @if [ ! -e $(xdrdst)/$@ -o $(@:.c=.x) -nt $(xdrdst)/$@ ]; then \ + rpcgen -c -o $(xdrdst)/$@ $(@:.c=.x) ;\ + fi + +# d*mn sed in netbsd6 doesn't do -i (inline) +# (why are we still running smoke on netbsd6 and not netbsd7?) +$(XDRHEADERS): $(XDRGENFILES) + @if [ ! -e $(xdrdst)/$@ -o $(@:.h=.x) -nt $(xdrdst)/$@ ]; then \ + rpcgen -h -o $(@:.h=.tmp) $(@:.h=.x) && \ + sed -e '/#ifndef/ s/-/_/g' -e '/#define/ s/-/_/g' \ + -e '/#endif/ s/-/_/' -e 's/TMP_/H_/g' \ + $(@:.h=.tmp) > $(xdrdst)/$@ && \ + rm -f $(@:.h=.tmp) ; \ + fi + + +# link .x files when doing out-of-tree builds +# have to use .PHONY here to force it; all versions of make +# will think the file already exists "here" by virtue of the +# VPATH. And we have to have the .x file in $cwd in order to +# have rpcgen generate "nice" #include directives +# i.e. (nice): +# #include "acl3-xdr.h" +# versus (not nice): +# #include "../../../../foo/src/rpc/xdr/src/acl3-xdr.h" +.PHONY : $(XDRGENFILES) +$(XDRGENFILES): + @if [ ! -e $@ ]; then ln -s $(xdrsrc)/$@ . ; fi; + +clean-local: + @if [ $(top_builddir) != $(top_srcdir) ]; then \ + rm -f $(xdrdst)/*.x; \ + fi diff --git a/rpc/xdr/src/acl3-xdr.x b/rpc/xdr/src/acl3-xdr.x new file mode 100644 index 00000000000..7f7364971e6 --- /dev/null +++ b/rpc/xdr/src/acl3-xdr.x @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> +%#include "xdr-nfs3.h" + +struct aclentry { + int type; + int uid; + int perm; +}; + +struct getaclargs { + netobj fh; + int mask; +}; + +struct getaclreply { + int status; + int attr_follows; + fattr3 attr; + int mask; + int aclcount; + aclentry aclentry<>; + int daclcount; + aclentry daclentry<>; +}; + +struct setaclargs { + netobj fh; + int mask; + int aclcount; + aclentry aclentry<>; + int daclcount; + aclentry daclentry<>; +}; + +struct setaclreply { + int status; + int attr_follows; + fattr3 attr; +}; diff --git a/rpc/xdr/src/changelog-xdr.x b/rpc/xdr/src/changelog-xdr.x new file mode 100644 index 00000000000..5956245d5ce --- /dev/null +++ b/rpc/xdr/src/changelog-xdr.x @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + +/* XDR: libgfchangelog -> changelog */ + +struct changelog_probe_req { + unsigned int filter; + char sock[UNIX_PATH_MAX]; +}; + +struct changelog_probe_rsp { + int op_ret; +}; + +/* XDR: changelog -> libgfchangelog */ +struct changelog_event_req { + /* sequence number for the buffer */ + unsigned hyper seq; + + /* time of dispatch */ + unsigned hyper tv_sec; + unsigned hyper tv_usec; +}; + +struct changelog_event_rsp { + int op_ret; + + /* ack'd buffers sequence number */ + unsigned hyper seq; +}; diff --git a/rpc/xdr/src/cli1-xdr.x b/rpc/xdr/src/cli1-xdr.x new file mode 100644 index 00000000000..777cb0046a2 --- /dev/null +++ b/rpc/xdr/src/cli1-xdr.x @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + + enum gf_cli_defrag_type { + GF_DEFRAG_CMD_NONE = 0, + GF_DEFRAG_CMD_START, + GF_DEFRAG_CMD_STOP, + GF_DEFRAG_CMD_STATUS, + GF_DEFRAG_CMD_START_LAYOUT_FIX, + GF_DEFRAG_CMD_START_FORCE, /* used by remove-brick data migration */ + GF_DEFRAG_CMD_START_TIER, + GF_DEFRAG_CMD_STATUS_TIER, + GF_DEFRAG_CMD_START_DETACH_TIER, + GF_DEFRAG_CMD_STOP_DETACH_TIER, + GF_DEFRAG_CMD_PAUSE_TIER, + GF_DEFRAG_CMD_RESUME_TIER, + GF_DEFRAG_CMD_DETACH_STATUS, + GF_DEFRAG_CMD_STOP_TIER, + GF_DEFRAG_CMD_DETACH_START, + GF_DEFRAG_CMD_DETACH_COMMIT, + GF_DEFRAG_CMD_DETACH_COMMIT_FORCE, + GF_DEFRAG_CMD_DETACH_STOP, + GF_DEFRAG_CMD_TYPE_MAX +}; + + enum gf_defrag_status_t { + GF_DEFRAG_STATUS_NOT_STARTED, + GF_DEFRAG_STATUS_STARTED, + GF_DEFRAG_STATUS_STOPPED, + GF_DEFRAG_STATUS_COMPLETE, + GF_DEFRAG_STATUS_FAILED, + GF_DEFRAG_STATUS_LAYOUT_FIX_STARTED, + GF_DEFRAG_STATUS_LAYOUT_FIX_STOPPED, + GF_DEFRAG_STATUS_LAYOUT_FIX_COMPLETE, + GF_DEFRAG_STATUS_LAYOUT_FIX_FAILED, + GF_DEFRAG_STATUS_MAX +}; + +enum gf1_cluster_type { + GF_CLUSTER_TYPE_NONE = 0, + GF_CLUSTER_TYPE_STRIPE, + GF_CLUSTER_TYPE_REPLICATE, + GF_CLUSTER_TYPE_STRIPE_REPLICATE, + GF_CLUSTER_TYPE_DISPERSE, + GF_CLUSTER_TYPE_TIER, + GF_CLUSTER_TYPE_MAX +}; + +enum gf_bitrot_type { + GF_BITROT_OPTION_TYPE_NONE = 0, + GF_BITROT_OPTION_TYPE_ENABLE, + GF_BITROT_OPTION_TYPE_DISABLE, + GF_BITROT_OPTION_TYPE_SCRUB_THROTTLE, + GF_BITROT_OPTION_TYPE_SCRUB_FREQ, + GF_BITROT_OPTION_TYPE_SCRUB, + GF_BITROT_OPTION_TYPE_EXPIRY_TIME, + GF_BITROT_CMD_SCRUB_STATUS, + GF_BITROT_CMD_SCRUB_ONDEMAND, + GF_BITROT_OPTION_TYPE_SIGNER_THREADS, + GF_BITROT_OPTION_TYPE_MAX +}; + + enum gf1_op_commands { + GF_OP_CMD_NONE = 0, + GF_OP_CMD_START, + GF_OP_CMD_COMMIT, + GF_OP_CMD_STOP, + GF_OP_CMD_STATUS, + GF_OP_CMD_COMMIT_FORCE, + GF_OP_CMD_DETACH_START, + GF_OP_CMD_DETACH_COMMIT, + GF_OP_CMD_DETACH_COMMIT_FORCE, + GF_OP_CMD_STOP_DETACH_TIER +}; + +enum gf_quota_type { + GF_QUOTA_OPTION_TYPE_NONE = 0, + GF_QUOTA_OPTION_TYPE_ENABLE, + GF_QUOTA_OPTION_TYPE_DISABLE, + GF_QUOTA_OPTION_TYPE_LIMIT_USAGE, + GF_QUOTA_OPTION_TYPE_REMOVE, + GF_QUOTA_OPTION_TYPE_LIST, + GF_QUOTA_OPTION_TYPE_VERSION, + GF_QUOTA_OPTION_TYPE_ALERT_TIME, + GF_QUOTA_OPTION_TYPE_SOFT_TIMEOUT, + GF_QUOTA_OPTION_TYPE_HARD_TIMEOUT, + GF_QUOTA_OPTION_TYPE_DEFAULT_SOFT_LIMIT, + GF_QUOTA_OPTION_TYPE_VERSION_OBJECTS, + GF_QUOTA_OPTION_TYPE_LIMIT_OBJECTS, + GF_QUOTA_OPTION_TYPE_LIST_OBJECTS, + GF_QUOTA_OPTION_TYPE_REMOVE_OBJECTS, + GF_QUOTA_OPTION_TYPE_ENABLE_OBJECTS, + GF_QUOTA_OPTION_TYPE_UPGRADE, + GF_QUOTA_OPTION_TYPE_MAX +}; + +enum gf1_cli_friends_list { + GF_CLI_LIST_PEERS = 1, + GF_CLI_LIST_POOL_NODES = 2 +}; + +enum gf1_cli_get_volume { + GF_CLI_GET_VOLUME_ALL = 1, + GF_CLI_GET_VOLUME, + GF_CLI_GET_NEXT_VOLUME +}; + +enum gf1_cli_sync_volume { + GF_CLI_SYNC_ALL = 1 +}; + +enum gf1_cli_op_flags { + GF_CLI_FLAG_OP_FORCE = 1 +}; + +enum gf1_cli_gsync_set { + GF_GSYNC_OPTION_TYPE_NONE, + GF_GSYNC_OPTION_TYPE_START, + GF_GSYNC_OPTION_TYPE_STOP, + GF_GSYNC_OPTION_TYPE_CONFIG, + GF_GSYNC_OPTION_TYPE_STATUS, + GF_GSYNC_OPTION_TYPE_ROTATE, + GF_GSYNC_OPTION_TYPE_CREATE, + GF_GSYNC_OPTION_TYPE_DELETE, + GF_GSYNC_OPTION_TYPE_PAUSE, + GF_GSYNC_OPTION_TYPE_RESUME +}; + +enum gf1_cli_stats_op { + GF_CLI_STATS_NONE = 0, + GF_CLI_STATS_START = 1, + GF_CLI_STATS_STOP = 2, + GF_CLI_STATS_INFO = 3, + GF_CLI_STATS_TOP = 4 +}; + +enum gf1_cli_info_op { + GF_CLI_INFO_NONE = 0, + GF_CLI_INFO_ALL = 1, + GF_CLI_INFO_INCREMENTAL = 2, + GF_CLI_INFO_CUMULATIVE = 3, + GF_CLI_INFO_CLEAR = 4 +}; + +enum gf_cli_get_state_op { + GF_CLI_GET_STATE_DETAIL = 1, + GF_CLI_GET_STATE_VOLOPTS = 2 +}; + +enum gf1_cli_top_op { + GF_CLI_TOP_NONE = 0, + GF_CLI_TOP_OPEN, + GF_CLI_TOP_READ, + GF_CLI_TOP_WRITE, + GF_CLI_TOP_OPENDIR, + GF_CLI_TOP_READDIR, + GF_CLI_TOP_READ_PERF, + GF_CLI_TOP_WRITE_PERF +}; + +/* The unconventional hex numbers help us perform + bit-wise operations which reduces complexity */ +enum gf_cli_status_type { + GF_CLI_STATUS_NONE = 0x000000, + GF_CLI_STATUS_MEM = 0x000001, /*000000000000001*/ + GF_CLI_STATUS_CLIENTS = 0x000002, /*000000000000010*/ + GF_CLI_STATUS_INODE = 0x000004, /*000000000000100*/ + GF_CLI_STATUS_FD = 0x000008, /*000000000001000*/ + GF_CLI_STATUS_CALLPOOL = 0x000010, /*000000000010000*/ + GF_CLI_STATUS_DETAIL = 0x000020, /*000000000100000*/ + GF_CLI_STATUS_TASKS = 0x000040, /*00000001000000*/ + GF_CLI_STATUS_CLIENT_LIST = 0x000080, /*00000010000000*/ + GF_CLI_STATUS_MASK = 0x0000FF, /*000000011111111 Used to get the op*/ + GF_CLI_STATUS_VOL = 0x000100, /*00000000100000000*/ + GF_CLI_STATUS_ALL = 0x000200, /*00000001000000000*/ + GF_CLI_STATUS_BRICK = 0x000400, /*00000010000000000*/ + GF_CLI_STATUS_NFS = 0x000800, /*00000100000000000*/ + GF_CLI_STATUS_SHD = 0x001000, /*00001000000000000*/ + GF_CLI_STATUS_QUOTAD = 0x002000, /*00010000000000000*/ + GF_CLI_STATUS_SNAPD = 0x004000, /*00100000000000000*/ + GF_CLI_STATUS_BITD = 0x008000, /*01000000000000000*/ + GF_CLI_STATUS_SCRUB = 0x010000, /*10000000000000000*/ + GF_CLI_STATUS_TIERD = 0x020000 /*100000000000000000*/ +}; + +/* Identifiers for snapshot clis */ +enum gf1_cli_snapshot { + GF_SNAP_OPTION_TYPE_NONE = 0, + GF_SNAP_OPTION_TYPE_CREATE, + GF_SNAP_OPTION_TYPE_DELETE, + GF_SNAP_OPTION_TYPE_RESTORE, + GF_SNAP_OPTION_TYPE_ACTIVATE, + GF_SNAP_OPTION_TYPE_DEACTIVATE, + GF_SNAP_OPTION_TYPE_LIST, + GF_SNAP_OPTION_TYPE_STATUS, + GF_SNAP_OPTION_TYPE_CONFIG, + GF_SNAP_OPTION_TYPE_CLONE, + GF_SNAP_OPTION_TYPE_INFO +}; + +enum gf1_cli_snapshot_info { + GF_SNAP_INFO_TYPE_ALL = 0, + GF_SNAP_INFO_TYPE_SNAP, + GF_SNAP_INFO_TYPE_VOL +}; + +enum gf1_cli_snapshot_config { + GF_SNAP_CONFIG_TYPE_NONE = 0, + GF_SNAP_CONFIG_TYPE_SET, + GF_SNAP_CONFIG_DISPLAY +}; + +enum gf1_cli_snapshot_status { + GF_SNAP_STATUS_TYPE_ALL = 0, + GF_SNAP_STATUS_TYPE_SNAP, + GF_SNAP_STATUS_TYPE_VOL, + GF_SNAP_STATUS_TYPE_ITER +}; + +/* Changing order of GF_SNAP_DELETE_TYPE_VOL * + * and GF_SNAP_DELETE_TYPE_SNAP so that they don't * + * overlap with the enums of GF_SNAP_STATUS_TYPE_SNAP, * + * and GF_SNAP_STATUS_TYPE_VOL * + */ +enum gf1_cli_snapshot_delete { + GF_SNAP_DELETE_TYPE_ALL = 0, + GF_SNAP_DELETE_TYPE_VOL = 1, + GF_SNAP_DELETE_TYPE_SNAP = 2, + GF_SNAP_DELETE_TYPE_ITER = 3 +}; + +struct gf_cli_req { + opaque dict<>; +}; + + struct gf_cli_rsp { + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +}; + +struct gf1_cli_peer_list_req { + int flags; + opaque dict<>; +}; + +struct gf1_cli_peer_list_rsp { + int op_ret; + int op_errno; + opaque friends<>; +}; + +struct gf1_cli_fsm_log_req { + string name<>; +}; + +struct gf1_cli_fsm_log_rsp { + int op_ret; + int op_errno; + string op_errstr<>; + opaque fsm_log<>; +}; + +struct gf1_cli_getwd_req { + int unused; +}; + +struct gf1_cli_getwd_rsp { + int op_ret; + int op_errno; + string wd<>; +}; + +struct gf1_cli_mount_req { + string label<>; + opaque dict<>; +}; + +struct gf1_cli_mount_rsp { + int op_ret; + int op_errno; + string path<>; +}; + +struct gf1_cli_umount_req { + int lazy; + string path<>; +}; + +struct gf1_cli_umount_rsp { + int op_ret; + int op_errno; +}; diff --git a/rpc/xdr/src/glusterd1-xdr.x b/rpc/xdr/src/glusterd1-xdr.x new file mode 100644 index 00000000000..b631dea3502 --- /dev/null +++ b/rpc/xdr/src/glusterd1-xdr.x @@ -0,0 +1,249 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + + enum glusterd_volume_status { + GLUSTERD_STATUS_NONE = 0, + GLUSTERD_STATUS_STARTED, + GLUSTERD_STATUS_STOPPED +} ; + + struct gd1_mgmt_probe_req { + unsigned char uuid[16]; + string hostname<>; + int port; +} ; + + struct gd1_mgmt_probe_rsp { + unsigned char uuid[16]; + string hostname<>; + int port; + int op_ret; + int op_errno; + string op_errstr<>; +} ; + +struct gd1_mgmt_friend_req { + unsigned char uuid[16]; + string hostname<>; + int port; + opaque vols<>; +} ; + +struct gd1_mgmt_friend_rsp { + unsigned char uuid[16]; + string hostname<>; + int op_ret; + int op_errno; + int port; +} ; + +struct gd1_mgmt_unfriend_req { + unsigned char uuid[16]; + string hostname<>; + int port; +} ; + +struct gd1_mgmt_unfriend_rsp { + unsigned char uuid[16]; + string hostname<>; + int op_ret; + int op_errno; + int port; +} ; + +struct gd1_mgmt_cluster_lock_req { + unsigned char uuid[16]; +} ; + +struct gd1_mgmt_cluster_lock_rsp { + unsigned char uuid[16]; + int op_ret; + int op_errno; +} ; + +struct gd1_mgmt_cluster_unlock_req { + unsigned char uuid[16]; +} ; + +struct gd1_mgmt_cluster_unlock_rsp { + unsigned char uuid[16]; + int op_ret; + int op_errno; +} ; + +struct gd1_mgmt_stage_op_req { + unsigned char uuid[16]; + int op; + opaque buf<>; +} ; + + +struct gd1_mgmt_stage_op_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +} ; + +struct gd1_mgmt_commit_op_req { + unsigned char uuid[16]; + int op; + opaque buf<>; +} ; + + +struct gd1_mgmt_commit_op_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + opaque dict<>; + string op_errstr<>; +} ; + +struct gd1_mgmt_friend_update { + unsigned char uuid[16]; + opaque friends<>; + int port; +} ; + +struct gd1_mgmt_friend_update_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; +} ; + +struct gd1_mgmt_brick_op_req { + string name<>; + int op; + opaque input<>; + opaque dict<>; +} ; + +struct gd1_mgmt_brick_op_rsp { + int op_ret; + int op_errno; + opaque output<>; + string op_errstr<>; +} ; + +struct gd1_mgmt_v3_lock_req { + unsigned char uuid[16]; + unsigned char txn_id[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_lock_rsp { + unsigned char uuid[16]; + unsigned char txn_id[16]; + opaque dict<>; + int op_ret; + int op_errno; +} ; + +struct gd1_mgmt_v3_pre_val_req { + unsigned char uuid[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_pre_val_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_brick_op_req { + unsigned char uuid[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_brick_op_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_commit_req { + unsigned char uuid[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_commit_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + opaque dict<>; + string op_errstr<>; +} ; + +struct gd1_mgmt_v3_post_commit_req { + unsigned char uuid[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_post_commit_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + opaque dict<>; + string op_errstr<>; +} ; + +struct gd1_mgmt_v3_post_val_req { + unsigned char uuid[16]; + int op; + int op_ret; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_post_val_rsp { + unsigned char uuid[16]; + int op; + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_unlock_req { + unsigned char uuid[16]; + unsigned char txn_id[16]; + int op; + opaque dict<>; +} ; + +struct gd1_mgmt_v3_unlock_rsp { + unsigned char uuid[16]; + unsigned char txn_id[16]; + opaque dict<>; + int op_ret; + int op_errno; +} ; diff --git a/rpc/xdr/src/glusterfs3-xdr.x b/rpc/xdr/src/glusterfs3-xdr.x new file mode 100644 index 00000000000..1c99099a721 --- /dev/null +++ b/rpc/xdr/src/glusterfs3-xdr.x @@ -0,0 +1,890 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/glusterfs-fops.h> +%#include "rpc-common-xdr.h" + +#define GF_REQUEST_MAXGROUPS 16 +struct gf_statfs { + u_quad_t bsize; + u_quad_t frsize; + u_quad_t blocks; + u_quad_t bfree; + u_quad_t bavail; + u_quad_t files; + u_quad_t ffree; + u_quad_t favail; + u_quad_t fsid; + u_quad_t flag; + u_quad_t namemax; +}; + + +struct gf_proto_flock { + unsigned int type; + unsigned int whence; + u_quad_t start; + u_quad_t len; + unsigned int pid; + opaque lk_owner<>; +} ; + +struct gf_proto_lease { + unsigned int cmd; + unsigned int lease_type; + opaque lease_id[16]; + unsigned int lease_flags; +} ; + +struct gf_iatt { + opaque ia_gfid[16]; + u_quad_t ia_ino; /* inode number */ + u_quad_t ia_dev; /* backing device ID */ + unsigned int mode; /* mode (type + protection )*/ + unsigned int ia_nlink; /* Link count */ + unsigned int ia_uid; /* user ID of owner */ + unsigned int ia_gid; /* group ID of owner */ + u_quad_t ia_rdev; /* device ID (if special file) */ + u_quad_t ia_size; /* file size in bytes */ + unsigned int ia_blksize; /* blocksize for filesystem I/O */ + u_quad_t ia_blocks; /* number of 512B blocks allocated */ + unsigned int ia_atime; /* last access time */ + unsigned int ia_atime_nsec; + unsigned int ia_mtime; /* last modification time */ + unsigned int ia_mtime_nsec; + unsigned int ia_ctime; /* last status change time */ + unsigned int ia_ctime_nsec; +}; + + +struct gfs3_cbk_cache_invalidation_req { + string gfid<>; + unsigned int event_type; /* Upcall event type */ + unsigned int flags; /* or mask of events incase of inotify */ + unsigned int expire_time_attr; /* the amount of time which client + * can cache this entry */ + gf_iatt stat; /* Updated/current stat of the file/dir */ + gf_iatt parent_stat; /* Updated stat of the parent dir + * needed in case of create, mkdir, + * unlink, rmdir, rename fops */ + gf_iatt oldparent_stat; /* Updated stat of the oldparent dir + needed in case of rename fop */ + opaque xdata<>; /* Extra data */ +}; + +struct gfs3_stat_req { + opaque gfid[16]; + opaque xdata<>; /* Extra data */ +}; +struct gfs3_stat_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + opaque xdata<>; /* Extra data */ +} ; + + +struct gfs3_readlink_req { + opaque gfid[16]; + unsigned int size; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_readlink_rsp { + int op_ret; + int op_errno; + gf_iatt buf; + string path<>; /* NULL terminated */ + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_mknod_req { + opaque pargfid[16]; + u_quad_t dev; + unsigned int mode; + unsigned int umask; + string bname<>; /* NULL terminated */ + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_mknod_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_mkdir_req { + opaque pargfid[16]; + unsigned int mode; + unsigned int umask; + string bname<>; /* NULL terminated */ + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_mkdir_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_unlink_req { + opaque pargfid[16]; + string bname<>; /* NULL terminated */ + unsigned int xflags; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_unlink_rsp { + int op_ret; + int op_errno; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_rmdir_req { + opaque pargfid[16]; + int xflags; + string bname<>; /* NULL terminated */ + opaque xdata<>; /* Extra data */ +}; + struct gfs3_rmdir_rsp { + int op_ret; + int op_errno; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_symlink_req { + opaque pargfid[16]; + string bname<>; + unsigned int umask; + string linkname<>; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_symlink_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_rename_req { + opaque oldgfid[16]; + opaque newgfid[16]; + string oldbname<>; /* NULL terminated */ + string newbname<>; /* NULL terminated */ + opaque xdata<>; /* Extra data */ +}; + struct gfs3_rename_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt preoldparent; + gf_iatt postoldparent; + gf_iatt prenewparent; + gf_iatt postnewparent; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_link_req { + opaque oldgfid[16]; + opaque newgfid[16]; + string newbname<>; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_link_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +}; + + struct gfs3_truncate_req { + opaque gfid[16]; + u_quad_t offset; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_truncate_rsp { + int op_ret; + int op_errno; + gf_iatt prestat; + gf_iatt poststat; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_open_req { + opaque gfid[16]; + unsigned int flags; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_open_rsp { + int op_ret; + int op_errno; + quad_t fd; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_read_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + unsigned int flag; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_read_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + unsigned int size; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_lookup_req { + opaque gfid[16]; + opaque pargfid[16]; + unsigned int flags; + string bname<>; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_lookup_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +} ; + + + + struct gfs3_write_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + unsigned int flag; + opaque xdata<>; /* Extra data */ +}; + struct gfs3_write_rsp { + int op_ret; + int op_errno; + gf_iatt prestat; + gf_iatt poststat; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_statfs_req { + opaque gfid[16]; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_statfs_rsp { + int op_ret; + int op_errno; + gf_statfs statfs; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_lk_req { + opaque gfid[16]; + int64_t fd; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_lk_rsp { + int op_ret; + int op_errno; + gf_proto_flock flock; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_lease_req { + opaque gfid[16]; + gf_proto_lease lease; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_lease_rsp { + int op_ret; + int op_errno; + gf_proto_lease lease; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_recall_lease_req { + opaque gfid[16]; + unsigned int lease_type; + opaque tid[16]; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_inodelk_req { + opaque gfid[16]; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + string volume<>; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_finodelk_req { + opaque gfid[16]; + quad_t fd; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + string volume<>; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_flush_req { + opaque gfid[16]; + quad_t fd; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_fsync_req { + opaque gfid[16]; + quad_t fd; + unsigned int data; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_fsync_rsp { + int op_ret; + int op_errno; + gf_iatt prestat; + gf_iatt poststat; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_setxattr_req { + opaque gfid[16]; + unsigned int flags; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + + struct gfs3_fsetxattr_req { + opaque gfid[16]; + int64_t fd; + unsigned int flags; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + + struct gfs3_xattrop_req { + opaque gfid[16]; + unsigned int flags; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_xattrop_rsp { + int op_ret; + int op_errno; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_fxattrop_req { + opaque gfid[16]; + quad_t fd; + unsigned int flags; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_fxattrop_rsp { + int op_ret; + int op_errno; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_getxattr_req { + opaque gfid[16]; + unsigned int namelen; + string name<>; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_getxattr_rsp { + int op_ret; + int op_errno; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_fgetxattr_req { + opaque gfid[16]; + quad_t fd; + unsigned int namelen; + string name<>; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_fgetxattr_rsp { + int op_ret; + int op_errno; + opaque dict<>; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_removexattr_req { + opaque gfid[16]; + string name<>; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_fremovexattr_req { + opaque gfid[16]; + quad_t fd; + string name<>; + opaque xdata<>; /* Extra data */ +} ; + + + + struct gfs3_opendir_req { + opaque gfid[16]; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_opendir_rsp { + int op_ret; + int op_errno; + quad_t fd; + opaque xdata<>; /* Extra data */ +} ; + + + struct gfs3_fsyncdir_req { + opaque gfid[16]; + quad_t fd; + int data; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_readdir_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + opaque xdata<>; /* Extra data */ +}; + + struct gfs3_readdirp_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + opaque dict<>; +} ; + + +struct gfs3_access_req { + opaque gfid[16]; + unsigned int mask; + opaque xdata<>; /* Extra data */ +} ; + + +struct gfs3_create_req { + opaque pargfid[16]; + unsigned int flags; + unsigned int mode; + unsigned int umask; + string bname<>; + opaque xdata<>; /* Extra data */ +} ; +struct gfs3_create_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + u_quad_t fd; + gf_iatt preparent; + gf_iatt postparent; + opaque xdata<>; /* Extra data */ +} ; + + + +struct gfs3_ftruncate_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + opaque xdata<>; /* Extra data */ +} ; +struct gfs3_ftruncate_rsp { + int op_ret; + int op_errno; + gf_iatt prestat; + gf_iatt poststat; + opaque xdata<>; /* Extra data */ +} ; + + +struct gfs3_fstat_req { + opaque gfid[16]; + quad_t fd; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_fstat_rsp { + int op_ret; + int op_errno; + gf_iatt stat; + opaque xdata<>; /* Extra data */ +} ; + + + + struct gfs3_entrylk_req { + opaque gfid[16]; + unsigned int cmd; + unsigned int type; + u_quad_t namelen; + string name<>; + string volume<>; + opaque xdata<>; /* Extra data */ +}; + + struct gfs3_fentrylk_req { + opaque gfid[16]; + quad_t fd; + unsigned int cmd; + unsigned int type; + u_quad_t namelen; + string name<>; + string volume<>; + opaque xdata<>; /* Extra data */ +}; + + + struct gfs3_setattr_req { + opaque gfid[16]; + gf_iatt stbuf; + int valid; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_setattr_rsp { + int op_ret; + int op_errno; + gf_iatt statpre; + gf_iatt statpost; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_fsetattr_req { + quad_t fd; + gf_iatt stbuf; + int valid; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_fsetattr_rsp { + int op_ret; + int op_errno; + gf_iatt statpre; + gf_iatt statpost; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_fallocate_req { + opaque gfid[16]; + quad_t fd; + unsigned int flags; + u_quad_t offset; + u_quad_t size; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_fallocate_rsp { + int op_ret; + int op_errno; + gf_iatt statpre; + gf_iatt statpost; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_discard_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + u_quad_t size; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_discard_rsp { + int op_ret; + int op_errno; + gf_iatt statpre; + gf_iatt statpost; + opaque xdata<>; /* Extra data */ +} ; + + struct gfs3_zerofill_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + u_quad_t size; + opaque xdata<>; +} ; + + struct gfs3_zerofill_rsp { + int op_ret; + int op_errno; + gf_iatt statpre; + gf_iatt statpost; + opaque xdata<>; +} ; + + + struct gfs3_rchecksum_req { + quad_t fd; + u_quad_t offset; + unsigned int len; + opaque xdata<>; /* Extra data */ +} ; + struct gfs3_rchecksum_rsp { + int op_ret; + int op_errno; + unsigned int weak_checksum; + opaque strong_checksum<>; + opaque xdata<>; /* Extra data */ +} ; + + +struct gfs3_ipc_req { + int op; + opaque xdata<>; +}; + +struct gfs3_ipc_rsp { + int op_ret; + int op_errno; + opaque xdata<>; +}; + + +struct gfs3_seek_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + int what; + opaque xdata<>; +}; + +struct gfs3_seek_rsp { + int op_ret; + int op_errno; + u_quad_t offset; + opaque xdata<>; +}; + + + struct gf_setvolume_req { + opaque dict<>; +} ; + struct gf_setvolume_rsp { + int op_ret; + int op_errno; + opaque dict<>; +} ; + + + struct gf_getspec_req { + unsigned int flags; + string key<>; + opaque xdata<>; /* Extra data */ +} ; + struct gf_getspec_rsp { + int op_ret; + int op_errno; + string spec<>; + opaque xdata<>; /* Extra data */ +} ; + + struct gf_get_volume_info_req { + opaque dict<>; /* Extra data */ +} ; + struct gf_get_volume_info_rsp { + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; /* Extra data */ +} ; + + struct gf_mgmt_hndsk_req { + opaque hndsk<>; +} ; + + struct gf_mgmt_hndsk_rsp { + int op_ret; + int op_errno; + opaque hndsk<>; +} ; + + struct gf_log_req { + opaque msg<>; +} ; + + struct gf_notify_req { + unsigned int flags; + string buf<>; + opaque xdata<>; /* Extra data */ +} ; + struct gf_notify_rsp { + int op_ret; + int op_errno; + unsigned int flags; + string buf<>; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_releasedir_req { + opaque gfid[16]; + quad_t fd; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_release_req { + opaque gfid[16]; + quad_t fd; + opaque xdata<>; /* Extra data */ +} ; + +struct gfs3_dirlist { + u_quad_t d_ino; + u_quad_t d_off; + unsigned int d_len; + unsigned int d_type; + string name<>; + gfs3_dirlist *nextentry; +}; + + +struct gfs3_readdir_rsp { + int op_ret; + int op_errno; + gfs3_dirlist *reply; + opaque xdata<>; /* Extra data */ +}; + +struct gfs3_dirplist { + u_quad_t d_ino; + u_quad_t d_off; + unsigned int d_len; + unsigned int d_type; + string name<>; + gf_iatt stat; + opaque dict<>; + gfs3_dirplist *nextentry; +}; + +struct gfs3_readdirp_rsp { + int op_ret; + int op_errno; + gfs3_dirplist *reply; + opaque xdata<>; /* Extra data */ +}; + +struct gf_set_lk_ver_rsp { + int op_ret; + int op_errno; + int lk_ver; +}; + +struct gf_set_lk_ver_req { + string uid<>; + int lk_ver; +}; + +struct gf_event_notify_req { + int op; + opaque dict<>; +}; + +struct gf_event_notify_rsp { + int op_ret; + int op_errno; + opaque dict<>; +}; + + +struct gf_getsnap_name_uuid_req { + opaque dict<>; +}; + +struct gf_getsnap_name_uuid_rsp { + int op_ret; + int op_errno; + string op_errstr<>; + opaque dict<>; +}; + +struct gfs3_locklist { + gf_proto_flock flock; + string client_uid<>; + unsigned int lk_flags; + gfs3_locklist *nextentry; +}; + +struct gfs3_getactivelk_rsp { + int op_ret; + int op_errno; + gfs3_locklist *reply; + opaque xdata<>; +}; + +struct gfs3_getactivelk_req { + opaque gfid[16]; + opaque xdata<>; +}; + +struct gfs3_setactivelk_rsp { + int op_ret; + int op_errno; + opaque xdata<>; +}; + +struct gfs3_setactivelk_req { + opaque gfid[16]; + gfs3_locklist *request; + opaque xdata<>; +}; diff --git a/rpc/xdr/src/glusterfs3.h b/rpc/xdr/src/glusterfs3.h new file mode 100644 index 00000000000..86b3a4c0e5d --- /dev/null +++ b/rpc/xdr/src/glusterfs3.h @@ -0,0 +1,957 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _GLUSTERFS3_H +#define _GLUSTERFS3_H + +#include <sys/uio.h> + +#include "xdr-generic.h" +#include "glusterfs3-xdr.h" +#include "glusterfs4-xdr.h" +#include <glusterfs/iatt.h> +#include "protocol-common.h" +#include <glusterfs/upcall-utils.h> + +#define xdr_decoded_remaining_addr(xdr) ((&xdr)->x_private) +#define xdr_decoded_remaining_len(xdr) ((&xdr)->x_handy) +#define xdr_encoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) +#define xdr_decoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) + +#define GF_O_ACCMODE 003 +#define GF_O_RDONLY 00 +#define GF_O_WRONLY 01 +#define GF_O_RDWR 02 +#define GF_O_CREAT 0100 +#define GF_O_EXCL 0200 +#define GF_O_NOCTTY 0400 +#define GF_O_TRUNC 01000 +#define GF_O_APPEND 02000 +#define GF_O_NONBLOCK 04000 +#define GF_O_SYNC 010000 +#define GF_O_ASYNC 020000 + +#define GF_O_DIRECT 040000 +#define GF_O_DIRECTORY 0200000 +#define GF_O_NOFOLLOW 0400000 +#define GF_O_NOATIME 01000000 +#define GF_O_CLOEXEC 02000000 + +#define GF_O_LARGEFILE 0100000 + +#define GF_O_FMODE_EXEC 040 + +#define XLATE_BIT(from, to, bit) \ + do { \ + if (from & bit) \ + to = to | GF_##bit; \ + } while (0) + +#define UNXLATE_BIT(from, to, bit) \ + do { \ + if (from & GF_##bit) \ + to = to | bit; \ + } while (0) + +#define XLATE_ACCESSMODE(from, to) \ + do { \ + switch (from & O_ACCMODE) { \ + case O_RDONLY: \ + to |= GF_O_RDONLY; \ + break; \ + case O_WRONLY: \ + to |= GF_O_WRONLY; \ + break; \ + case O_RDWR: \ + to |= GF_O_RDWR; \ + break; \ + } \ + } while (0) + +#define UNXLATE_ACCESSMODE(from, to) \ + do { \ + switch (from & GF_O_ACCMODE) { \ + case GF_O_RDONLY: \ + to |= O_RDONLY; \ + break; \ + case GF_O_WRONLY: \ + to |= O_WRONLY; \ + break; \ + case GF_O_RDWR: \ + to |= O_RDWR; \ + break; \ + } \ + } while (0) + +static inline uint32_t +gf_flags_from_flags(uint32_t flags) +{ + uint32_t gf_flags = 0; + + XLATE_ACCESSMODE(flags, gf_flags); + + XLATE_BIT(flags, gf_flags, O_CREAT); + XLATE_BIT(flags, gf_flags, O_EXCL); + XLATE_BIT(flags, gf_flags, O_NOCTTY); + XLATE_BIT(flags, gf_flags, O_TRUNC); + XLATE_BIT(flags, gf_flags, O_APPEND); + XLATE_BIT(flags, gf_flags, O_NONBLOCK); + XLATE_BIT(flags, gf_flags, O_SYNC); + XLATE_BIT(flags, gf_flags, O_ASYNC); + + XLATE_BIT(flags, gf_flags, O_DIRECT); + XLATE_BIT(flags, gf_flags, O_DIRECTORY); + XLATE_BIT(flags, gf_flags, O_NOFOLLOW); +#ifdef O_NOATIME + XLATE_BIT(flags, gf_flags, O_NOATIME); +#endif +#ifdef O_CLOEXEC + XLATE_BIT(flags, gf_flags, O_CLOEXEC); +#endif + XLATE_BIT(flags, gf_flags, O_LARGEFILE); + XLATE_BIT(flags, gf_flags, O_FMODE_EXEC); + + return gf_flags; +} + +static inline uint32_t +gf_flags_to_flags(uint32_t gf_flags) +{ + uint32_t flags = 0; + + UNXLATE_ACCESSMODE(gf_flags, flags); + + UNXLATE_BIT(gf_flags, flags, O_CREAT); + UNXLATE_BIT(gf_flags, flags, O_EXCL); + UNXLATE_BIT(gf_flags, flags, O_NOCTTY); + UNXLATE_BIT(gf_flags, flags, O_TRUNC); + UNXLATE_BIT(gf_flags, flags, O_APPEND); + UNXLATE_BIT(gf_flags, flags, O_NONBLOCK); + UNXLATE_BIT(gf_flags, flags, O_SYNC); + UNXLATE_BIT(gf_flags, flags, O_ASYNC); + + UNXLATE_BIT(gf_flags, flags, O_DIRECT); + UNXLATE_BIT(gf_flags, flags, O_DIRECTORY); + UNXLATE_BIT(gf_flags, flags, O_NOFOLLOW); +#ifdef O_NOATIME + UNXLATE_BIT(gf_flags, flags, O_NOATIME); +#endif +#ifdef O_CLOEXEC + UNXLATE_BIT(gf_flags, flags, O_CLOEXEC); +#endif + UNXLATE_BIT(gf_flags, flags, O_LARGEFILE); + UNXLATE_BIT(gf_flags, flags, O_FMODE_EXEC); + + return flags; +} + +static inline void +gf_statfs_to_statfs(struct gf_statfs *gf_stat, struct statvfs *stat) +{ + if (!stat || !gf_stat) + return; + + stat->f_bsize = (gf_stat->bsize); + stat->f_frsize = (gf_stat->frsize); + stat->f_blocks = (gf_stat->blocks); + stat->f_bfree = (gf_stat->bfree); + stat->f_bavail = (gf_stat->bavail); + stat->f_files = (gf_stat->files); + stat->f_ffree = (gf_stat->ffree); + stat->f_favail = (gf_stat->favail); + stat->f_fsid = (gf_stat->fsid); + stat->f_flag = (gf_stat->flag); + stat->f_namemax = (gf_stat->namemax); +} + +static inline void +gf_statfs_from_statfs(struct gf_statfs *gf_stat, struct statvfs *stat) +{ + if (!stat || !gf_stat) + return; + + gf_stat->bsize = stat->f_bsize; + gf_stat->frsize = stat->f_frsize; + gf_stat->blocks = stat->f_blocks; + gf_stat->bfree = stat->f_bfree; + gf_stat->bavail = stat->f_bavail; + gf_stat->files = stat->f_files; + gf_stat->ffree = stat->f_ffree; + gf_stat->favail = stat->f_favail; + gf_stat->fsid = stat->f_fsid; + gf_stat->flag = stat->f_flag; + gf_stat->namemax = stat->f_namemax; +} + +static inline void +gf_proto_lease_to_lease(struct gf_proto_lease *gf_proto_lease, + struct gf_lease *gf_lease) +{ + if (!gf_lease || !gf_proto_lease) + return; + + gf_lease->cmd = gf_proto_lease->cmd; + gf_lease->lease_type = gf_proto_lease->lease_type; + memcpy(gf_lease->lease_id, gf_proto_lease->lease_id, LEASE_ID_SIZE); +} + +static inline void +gf_proto_lease_from_lease(struct gf_proto_lease *gf_proto_lease, + struct gf_lease *gf_lease) +{ + if (!gf_lease || !gf_proto_lease) + return; + + gf_proto_lease->cmd = gf_lease->cmd; + gf_proto_lease->lease_type = gf_lease->lease_type; + memcpy(gf_proto_lease->lease_id, gf_lease->lease_id, LEASE_ID_SIZE); +} + +static inline int +gf_proto_recall_lease_to_upcall(struct gfs3_recall_lease_req *recall_lease, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_recall_lease *tmp = NULL; + int ret = 0; + + GF_VALIDATE_OR_GOTO(THIS->name, recall_lease, out); + GF_VALIDATE_OR_GOTO(THIS->name, gf_up_data, out); + + tmp = (struct gf_upcall_recall_lease *)gf_up_data->data; + tmp->lease_type = recall_lease->lease_type; + memcpy(gf_up_data->gfid, recall_lease->gfid, 16); + memcpy(tmp->tid, recall_lease->tid, 16); + + GF_PROTOCOL_DICT_UNSERIALIZE( + THIS, tmp->dict, (recall_lease->xdata).xdata_val, + (recall_lease->xdata).xdata_len, ret, errno, out); +out: + return ret; +} + +static inline int +gf_proto_recall_lease_from_upcall(xlator_t *this, + struct gfs3_recall_lease_req *recall_lease, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_recall_lease *tmp = NULL; + int ret = 0; + + GF_VALIDATE_OR_GOTO(this->name, recall_lease, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + tmp = (struct gf_upcall_recall_lease *)gf_up_data->data; + recall_lease->lease_type = tmp->lease_type; + memcpy(recall_lease->gfid, gf_up_data->gfid, 16); + memcpy(recall_lease->tid, tmp->tid, 16); + + GF_PROTOCOL_DICT_SERIALIZE(this, tmp->dict, + &(recall_lease->xdata).xdata_val, + (recall_lease->xdata).xdata_len, ret, out); +out: + return ret; +} + +static inline void +gf_proto_flock_to_flock(struct gf_proto_flock *gf_proto_flock, + struct gf_flock *gf_flock) +{ + if (!gf_flock || !gf_proto_flock) + return; + + gf_flock->l_type = gf_proto_flock->type; + gf_flock->l_whence = gf_proto_flock->whence; + gf_flock->l_start = gf_proto_flock->start; + gf_flock->l_len = gf_proto_flock->len; + gf_flock->l_pid = gf_proto_flock->pid; + gf_flock->l_owner.len = gf_proto_flock->lk_owner.lk_owner_len; + if (gf_flock->l_owner.len && + (gf_flock->l_owner.len < GF_MAX_LOCK_OWNER_LEN)) + memcpy(gf_flock->l_owner.data, gf_proto_flock->lk_owner.lk_owner_val, + gf_flock->l_owner.len); +} + +static inline void +gf_proto_flock_from_flock(struct gf_proto_flock *gf_proto_flock, + struct gf_flock *gf_flock) +{ + if (!gf_flock || !gf_proto_flock) + return; + + gf_proto_flock->type = (gf_flock->l_type); + gf_proto_flock->whence = (gf_flock->l_whence); + gf_proto_flock->start = (gf_flock->l_start); + gf_proto_flock->len = (gf_flock->l_len); + gf_proto_flock->pid = (gf_flock->l_pid); + gf_proto_flock->lk_owner.lk_owner_len = gf_flock->l_owner.len; + if (gf_flock->l_owner.len) + gf_proto_flock->lk_owner.lk_owner_val = gf_flock->l_owner.data; +} + +static inline void +gf_stat_to_iatt(struct gf_iatt *gf_stat, struct iatt *iatt) +{ + if (!iatt || !gf_stat) + return; + + memcpy(iatt->ia_gfid, gf_stat->ia_gfid, 16); + iatt->ia_ino = gf_stat->ia_ino; + iatt->ia_dev = gf_stat->ia_dev; + iatt->ia_type = ia_type_from_st_mode(gf_stat->mode); + iatt->ia_prot = ia_prot_from_st_mode(gf_stat->mode); + iatt->ia_nlink = gf_stat->ia_nlink; + iatt->ia_uid = gf_stat->ia_uid; + iatt->ia_gid = gf_stat->ia_gid; + iatt->ia_rdev = gf_stat->ia_rdev; + iatt->ia_size = gf_stat->ia_size; + iatt->ia_blksize = gf_stat->ia_blksize; + iatt->ia_blocks = gf_stat->ia_blocks; + iatt->ia_atime = gf_stat->ia_atime; + iatt->ia_atime_nsec = gf_stat->ia_atime_nsec; + iatt->ia_mtime = gf_stat->ia_mtime; + iatt->ia_mtime_nsec = gf_stat->ia_mtime_nsec; + iatt->ia_ctime = gf_stat->ia_ctime; + iatt->ia_ctime_nsec = gf_stat->ia_ctime_nsec; +} + +static inline void +gf_stat_from_iatt(struct gf_iatt *gf_stat, struct iatt *iatt) +{ + if (!iatt || !gf_stat) + return; + + memcpy(gf_stat->ia_gfid, iatt->ia_gfid, 16); + gf_stat->ia_ino = iatt->ia_ino; + gf_stat->ia_dev = iatt->ia_dev; + gf_stat->mode = st_mode_from_ia(iatt->ia_prot, iatt->ia_type); + gf_stat->ia_nlink = iatt->ia_nlink; + gf_stat->ia_uid = iatt->ia_uid; + gf_stat->ia_gid = iatt->ia_gid; + gf_stat->ia_rdev = iatt->ia_rdev; + gf_stat->ia_size = iatt->ia_size; + gf_stat->ia_blksize = iatt->ia_blksize; + gf_stat->ia_blocks = iatt->ia_blocks; + gf_stat->ia_atime = iatt->ia_atime; + gf_stat->ia_atime_nsec = iatt->ia_atime_nsec; + gf_stat->ia_mtime = iatt->ia_mtime; + gf_stat->ia_mtime_nsec = iatt->ia_mtime_nsec; + gf_stat->ia_ctime = iatt->ia_ctime; + gf_stat->ia_ctime_nsec = iatt->ia_ctime_nsec; +} + +static inline int +gf_proto_cache_invalidation_from_upcall( + xlator_t *this, gfs3_cbk_cache_invalidation_req *gf_c_req, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_cache_invalidation *gf_c_data = NULL; + int is_cache_inval = 0; + int ret = -1; + + GF_VALIDATE_OR_GOTO(this->name, gf_c_req, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + is_cache_inval = ((gf_up_data->event_type == GF_UPCALL_CACHE_INVALIDATION) + ? 1 + : 0); + GF_VALIDATE_OR_GOTO(this->name, is_cache_inval, out); + + gf_c_data = (struct gf_upcall_cache_invalidation *)gf_up_data->data; + GF_VALIDATE_OR_GOTO(this->name, gf_c_data, out); + + gf_c_req->gfid = uuid_utoa(gf_up_data->gfid); + gf_c_req->event_type = gf_up_data->event_type; + gf_c_req->flags = gf_c_data->flags; + gf_c_req->expire_time_attr = gf_c_data->expire_time_attr; + gf_stat_from_iatt(&gf_c_req->stat, &gf_c_data->stat); + gf_stat_from_iatt(&gf_c_req->parent_stat, &gf_c_data->p_stat); + gf_stat_from_iatt(&gf_c_req->oldparent_stat, &gf_c_data->oldp_stat); + + ret = 0; + GF_PROTOCOL_DICT_SERIALIZE(this, gf_c_data->dict, + &(gf_c_req->xdata).xdata_val, + (gf_c_req->xdata).xdata_len, ret, out); +out: + return ret; +} + +static inline int +gf_proto_cache_invalidation_to_upcall(xlator_t *this, + gfs3_cbk_cache_invalidation_req *gf_c_req, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_cache_invalidation *gf_c_data = NULL; + int ret = -1; + + GF_VALIDATE_OR_GOTO(this->name, gf_c_req, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + gf_c_data = (struct gf_upcall_cache_invalidation *)gf_up_data->data; + GF_VALIDATE_OR_GOTO(this->name, gf_c_data, out); + + ret = gf_uuid_parse(gf_c_req->gfid, gf_up_data->gfid); + if (ret) { + gf_log(this->name, GF_LOG_WARNING, "gf_uuid_parse(%s) failed", + gf_c_req->gfid); + gf_up_data->event_type = GF_UPCALL_EVENT_NULL; + goto out; + } + + gf_up_data->event_type = gf_c_req->event_type; + + gf_c_data->flags = gf_c_req->flags; + gf_c_data->expire_time_attr = gf_c_req->expire_time_attr; + gf_stat_to_iatt(&gf_c_req->stat, &gf_c_data->stat); + gf_stat_to_iatt(&gf_c_req->parent_stat, &gf_c_data->p_stat); + gf_stat_to_iatt(&gf_c_req->oldparent_stat, &gf_c_data->oldp_stat); + + ret = 0; + GF_PROTOCOL_DICT_UNSERIALIZE(this, gf_c_data->dict, + (gf_c_req->xdata).xdata_val, + (gf_c_req->xdata).xdata_len, ret, ret, out); + + /* If no dict was sent, create an empty dict, so that each xlator + * need not check if empty then create new dict. Will be unref'd by the + * caller */ + if (!gf_c_data->dict) + gf_c_data->dict = dict_new(); +out: + return ret; +} + +static inline int +gf_proto_inodelk_contention_to_upcall(struct gfs4_inodelk_contention_req *lc, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_inodelk_contention *tmp = NULL; + xlator_t *this = NULL; + int ret = -1; + int op_errno = EINVAL; + + this = THIS; + + GF_VALIDATE_OR_GOTO(this->name, lc, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + tmp = (struct gf_upcall_inodelk_contention *)gf_up_data->data; + + gf_uuid_copy(gf_up_data->gfid, (unsigned char *)lc->gfid); + + gf_proto_flock_to_flock(&lc->flock, &tmp->flock); + tmp->pid = lc->pid; + tmp->domain = lc->domain; + if ((tmp->domain != NULL) && (*tmp->domain == 0)) { + tmp->domain = NULL; + } + + GF_PROTOCOL_DICT_UNSERIALIZE(this, tmp->xdata, lc->xdata.xdata_val, + lc->xdata.xdata_len, ret, op_errno, out); + + ret = 0; + +out: + if (ret < 0) { + ret = -op_errno; + } + + return ret; +} + +static inline int +gf_proto_inodelk_contention_from_upcall(xlator_t *this, + struct gfs4_inodelk_contention_req *lc, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_inodelk_contention *tmp = NULL; + int ret = -1; + int op_errno = EINVAL; + + GF_VALIDATE_OR_GOTO(this->name, lc, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + tmp = (struct gf_upcall_inodelk_contention *)gf_up_data->data; + + gf_uuid_copy((unsigned char *)lc->gfid, gf_up_data->gfid); + + gf_proto_flock_from_flock(&lc->flock, &tmp->flock); + lc->pid = tmp->pid; + lc->domain = (char *)tmp->domain; + if (lc->domain == NULL) { + lc->domain = ""; + } + + GF_PROTOCOL_DICT_SERIALIZE(this, tmp->xdata, &lc->xdata.xdata_val, + lc->xdata.xdata_len, op_errno, out); + + ret = 0; + +out: + if (ret < 0) { + ret = -op_errno; + } + + return ret; +} + +static inline int +gf_proto_entrylk_contention_to_upcall(struct gfs4_entrylk_contention_req *lc, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_entrylk_contention *tmp = NULL; + xlator_t *this = NULL; + int ret = -1; + int op_errno = EINVAL; + + this = THIS; + + GF_VALIDATE_OR_GOTO(this->name, lc, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + tmp = (struct gf_upcall_entrylk_contention *)gf_up_data->data; + + gf_uuid_copy(gf_up_data->gfid, (unsigned char *)lc->gfid); + + tmp->type = lc->type; + tmp->name = lc->name; + if ((tmp->name != NULL) && (*tmp->name == 0)) { + tmp->name = NULL; + } + tmp->pid = lc->pid; + tmp->domain = lc->domain; + if ((tmp->domain != NULL) && (*tmp->domain == 0)) { + tmp->domain = NULL; + } + + GF_PROTOCOL_DICT_UNSERIALIZE(this, tmp->xdata, lc->xdata.xdata_val, + lc->xdata.xdata_len, ret, op_errno, out); + + ret = 0; + +out: + if (ret < 0) { + ret = -op_errno; + } + + return ret; +} + +static inline int +gf_proto_entrylk_contention_from_upcall(xlator_t *this, + struct gfs4_entrylk_contention_req *lc, + struct gf_upcall *gf_up_data) +{ + struct gf_upcall_entrylk_contention *tmp = NULL; + int ret = -1; + int op_errno = EINVAL; + + GF_VALIDATE_OR_GOTO(this->name, lc, out); + GF_VALIDATE_OR_GOTO(this->name, gf_up_data, out); + + tmp = (struct gf_upcall_entrylk_contention *)gf_up_data->data; + + gf_uuid_copy((unsigned char *)lc->gfid, gf_up_data->gfid); + + lc->type = tmp->type; + lc->name = (char *)tmp->name; + if (lc->name == NULL) { + lc->name = ""; + } + lc->pid = tmp->pid; + lc->domain = (char *)tmp->domain; + if (lc->domain == NULL) { + lc->domain = ""; + } + + GF_PROTOCOL_DICT_SERIALIZE(this, tmp->xdata, &lc->xdata.xdata_val, + lc->xdata.xdata_len, op_errno, out); + + ret = 0; + +out: + if (ret < 0) { + ret = -op_errno; + } + + return ret; +} + +static inline void +gfx_mdata_iatt_to_mdata_iatt(struct gfx_mdata_iatt *gf_mdata_iatt, + struct mdata_iatt *mdata_iatt) +{ + if (!mdata_iatt || !gf_mdata_iatt) + return; + mdata_iatt->ia_atime = gf_mdata_iatt->ia_atime; + mdata_iatt->ia_atime_nsec = gf_mdata_iatt->ia_atime_nsec; + mdata_iatt->ia_mtime = gf_mdata_iatt->ia_mtime; + mdata_iatt->ia_mtime_nsec = gf_mdata_iatt->ia_mtime_nsec; + mdata_iatt->ia_ctime = gf_mdata_iatt->ia_ctime; + mdata_iatt->ia_ctime_nsec = gf_mdata_iatt->ia_ctime_nsec; +} + +static inline void +gfx_mdata_iatt_from_mdata_iatt(struct gfx_mdata_iatt *gf_mdata_iatt, + struct mdata_iatt *mdata_iatt) +{ + if (!mdata_iatt || !gf_mdata_iatt) + return; + gf_mdata_iatt->ia_atime = mdata_iatt->ia_atime; + gf_mdata_iatt->ia_atime_nsec = mdata_iatt->ia_atime_nsec; + gf_mdata_iatt->ia_mtime = mdata_iatt->ia_mtime; + gf_mdata_iatt->ia_mtime_nsec = mdata_iatt->ia_mtime_nsec; + gf_mdata_iatt->ia_ctime = mdata_iatt->ia_ctime; + gf_mdata_iatt->ia_ctime_nsec = mdata_iatt->ia_ctime_nsec; +} + +static inline void +gfx_stat_to_iattx(struct gfx_iattx *gf_stat, struct iatt *iatt) +{ + if (!iatt || !gf_stat) + return; + + memcpy(iatt->ia_gfid, gf_stat->ia_gfid, 16); + + iatt->ia_flags = gf_stat->ia_flags; + iatt->ia_ino = gf_stat->ia_ino; + iatt->ia_dev = gf_stat->ia_dev; + iatt->ia_rdev = gf_stat->ia_rdev; + iatt->ia_size = gf_stat->ia_size; + iatt->ia_nlink = gf_stat->ia_nlink; + iatt->ia_uid = gf_stat->ia_uid; + iatt->ia_gid = gf_stat->ia_gid; + iatt->ia_blksize = gf_stat->ia_blksize; + iatt->ia_blocks = gf_stat->ia_blocks; + iatt->ia_atime = gf_stat->ia_atime; + iatt->ia_atime_nsec = gf_stat->ia_atime_nsec; + iatt->ia_mtime = gf_stat->ia_mtime; + iatt->ia_mtime_nsec = gf_stat->ia_mtime_nsec; + iatt->ia_ctime = gf_stat->ia_ctime; + iatt->ia_ctime_nsec = gf_stat->ia_ctime_nsec; + iatt->ia_btime = gf_stat->ia_btime; + iatt->ia_btime_nsec = gf_stat->ia_btime_nsec; + iatt->ia_attributes = gf_stat->ia_attributes; + iatt->ia_attributes_mask = gf_stat->ia_attributes_mask; + + iatt->ia_type = ia_type_from_st_mode(gf_stat->mode); + iatt->ia_prot = ia_prot_from_st_mode(gf_stat->mode); +} + +static inline void +gfx_stat_from_iattx(struct gfx_iattx *gf_stat, struct iatt *iatt) +{ + if (!iatt || !gf_stat) + return; + + memcpy(gf_stat->ia_gfid, iatt->ia_gfid, 16); + gf_stat->ia_ino = iatt->ia_ino; + gf_stat->ia_dev = iatt->ia_dev; + + gf_stat->ia_nlink = iatt->ia_nlink; + gf_stat->ia_uid = iatt->ia_uid; + gf_stat->ia_gid = iatt->ia_gid; + gf_stat->ia_rdev = iatt->ia_rdev; + gf_stat->ia_size = iatt->ia_size; + gf_stat->ia_blksize = iatt->ia_blksize; + gf_stat->ia_blocks = iatt->ia_blocks; + gf_stat->ia_atime = iatt->ia_atime; + gf_stat->ia_atime_nsec = iatt->ia_atime_nsec; + gf_stat->ia_mtime = iatt->ia_mtime; + gf_stat->ia_mtime_nsec = iatt->ia_mtime_nsec; + gf_stat->ia_ctime = iatt->ia_ctime; + gf_stat->ia_ctime_nsec = iatt->ia_ctime_nsec; + + gf_stat->ia_flags = iatt->ia_flags; + gf_stat->ia_btime = iatt->ia_btime; + gf_stat->ia_btime_nsec = iatt->ia_btime_nsec; + gf_stat->ia_attributes = iatt->ia_attributes; + gf_stat->ia_attributes_mask = iatt->ia_attributes_mask; + + gf_stat->mode = st_mode_from_ia(iatt->ia_prot, iatt->ia_type); +} + +/* dict_to_xdr () */ +static inline int +dict_to_xdr(dict_t *this, gfx_dict *dict) +{ + int ret = -1; + int i = 0; + int index = 0; + data_pair_t *dpair = NULL; + gfx_dict_pair *xpair = NULL; + ssize_t size = 0; + + /* This is a failure as we expect destination to be valid */ + if (!dict) + goto out; + + /* This is OK as dictionary can be null, in which case, destination + should also know that it is NULL. */ + if (!this) { + /* encode special meaning data here, + while decoding, you know it is NULL dict */ + dict->count = -1; + /* everything else is normal */ + dict->pairs.pairs_len = 0; + ret = 0; + goto out; + } + + /* Do the whole operation in locked region */ + LOCK(&this->lock); + + dict->pairs.pairs_val = GF_CALLOC(1, (this->count * sizeof(gfx_dict_pair)), + gf_common_mt_char); + if (!dict->pairs.pairs_val) + goto out; + + dpair = this->members_list; + for (i = 0; i < this->count; i++) { + xpair = &dict->pairs.pairs_val[index]; + + xpair->key.key_val = dpair->key; + xpair->key.key_len = strlen(dpair->key) + 1; + xpair->value.type = dpair->value->data_type; + switch (dpair->value->data_type) { + /* Add more type here */ + case GF_DATA_TYPE_INT: + index++; + xpair->value.gfx_value_u.value_int = strtoll(dpair->value->data, + NULL, 0); + break; + case GF_DATA_TYPE_UINT: + index++; + xpair->value.gfx_value_u.value_uint = strtoull( + dpair->value->data, NULL, 0); + break; + case GF_DATA_TYPE_DOUBLE: + index++; + xpair->value.gfx_value_u.value_dbl = strtod(dpair->value->data, + NULL); + break; + case GF_DATA_TYPE_STR: + index++; + xpair->value.gfx_value_u.val_string + .val_string_val = dpair->value->data; + xpair->value.gfx_value_u.val_string + .val_string_len = dpair->value->len; + break; + case GF_DATA_TYPE_IATT: + index++; + gfx_stat_from_iattx(&xpair->value.gfx_value_u.iatt, + (struct iatt *)dpair->value->data); + break; + case GF_DATA_TYPE_MDATA: + index++; + gfx_mdata_iatt_from_mdata_iatt( + &xpair->value.gfx_value_u.mdata_iatt, + (struct mdata_iatt *)dpair->value->data); + break; + case GF_DATA_TYPE_GFUUID: + index++; + memcpy(&xpair->value.gfx_value_u.uuid, dpair->value->data, + sizeof(uuid_t)); + break; + + case GF_DATA_TYPE_PTR: + case GF_DATA_TYPE_STR_OLD: + index++; + /* Ideally, each type of data stored in dictionary + should have type. A pointer type shouldn't be + sent on wire */ + + /* This is done for backward compatibility as dict is + heavily used for transporting data over wire. + Ideally, wherever there is an issue, fix and + move on */ + xpair->value.gfx_value_u.other.other_val = dpair->value->data; + xpair->value.gfx_value_u.other.other_len = dpair->value->len; + + /* Change this to INFO, after taking the above down */ + gf_msg("dict", GF_LOG_DEBUG, EINVAL, LG_MSG_DICT_SERIAL_FAILED, + "key '%s' would not be sent on wire in the future", + dpair->key); + break; + default: + /* Unknown type and ptr type is not sent on wire */ + gf_msg("dict", GF_LOG_WARNING, EINVAL, + LG_MSG_DICT_SERIAL_FAILED, + "key '%s' is not sent on wire", dpair->key); + break; + } + dpair = dpair->next; + } + + dict->pairs.pairs_len = index; + dict->count = index; + + /* This is required mainly in the RPC layer to understand the + boundary for proper payload. Hence only send the size of + variable XDR size. ie, the formula should be: + xdr_size = total size - (xdr_size + count + pairs.pairs_len)) */ + size = xdr_sizeof((xdrproc_t)xdr_gfx_dict, dict); + + dict->xdr_size = (size > 12) ? (size - 12) : 0; + + ret = 0; +out: + /* this can be null here, so unlock only if its not null */ + if (this) + UNLOCK(&this->lock); + + return ret; +} + +static inline int +xdr_to_dict(gfx_dict *dict, dict_t **to) +{ + int ret = -1; + int index = 0; + char *key = NULL; + char *value = NULL; + gfx_dict_pair *xpair = NULL; + dict_t *this = NULL; + unsigned char *uuid = NULL; + struct iatt *iatt = NULL; + struct mdata_iatt *mdata_iatt = NULL; + + if (!to || !dict) + goto out; + + if (dict->count < 0) { + /* indicates NULL dict was passed for encoding */ + ret = 0; + goto out; + } + + this = dict_new(); + if (!this) + goto out; + + for (index = 0; index < dict->pairs.pairs_len; index++) { + ret = -1; + xpair = &dict->pairs.pairs_val[index]; + + key = xpair->key.key_val; + switch (xpair->value.type) { + /* Add more type here */ + case GF_DATA_TYPE_INT: + ret = dict_set_int64(this, key, + xpair->value.gfx_value_u.value_int); + break; + case GF_DATA_TYPE_UINT: + ret = dict_set_uint64(this, key, + xpair->value.gfx_value_u.value_uint); + break; + case GF_DATA_TYPE_DOUBLE: + ret = dict_set_double(this, key, + xpair->value.gfx_value_u.value_dbl); + break; + case GF_DATA_TYPE_STR: + value = GF_MALLOC( + xpair->value.gfx_value_u.val_string.val_string_len + 1, + gf_common_mt_char); + if (!value) { + errno = ENOMEM; + goto out; + } + memcpy(value, + xpair->value.gfx_value_u.val_string.val_string_val, + xpair->value.gfx_value_u.val_string.val_string_len); + value[xpair->value.gfx_value_u.val_string.val_string_len] = + '\0'; + free(xpair->value.gfx_value_u.val_string.val_string_val); + ret = dict_set_dynstr(this, key, value); + break; + case GF_DATA_TYPE_GFUUID: + uuid = GF_MALLOC(sizeof(uuid_t), gf_common_mt_uuid_t); + if (!uuid) { + errno = ENOMEM; + goto out; + } + memcpy(uuid, xpair->value.gfx_value_u.uuid, sizeof(uuid_t)); + ret = dict_set_gfuuid(this, key, uuid, false); + break; + case GF_DATA_TYPE_IATT: + iatt = GF_CALLOC(1, sizeof(struct iatt), gf_common_mt_char); + if (!iatt) { + errno = ENOMEM; + goto out; + } + gfx_stat_to_iattx(&xpair->value.gfx_value_u.iatt, iatt); + ret = dict_set_iatt(this, key, iatt, false); + break; + case GF_DATA_TYPE_MDATA: + mdata_iatt = GF_CALLOC(1, sizeof(struct mdata_iatt), + gf_common_mt_char); + if (!mdata_iatt) { + errno = ENOMEM; + gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, LG_MSG_NO_MEMORY, + "failed to allocate memory. key: %s", key); + ret = -1; + goto out; + } + gfx_mdata_iatt_to_mdata_iatt( + &xpair->value.gfx_value_u.mdata_iatt, mdata_iatt); + ret = dict_set_mdata(this, key, mdata_iatt, false); + if (ret != 0) { + GF_FREE(mdata_iatt); + gf_msg(THIS->name, GF_LOG_ERROR, ENOMEM, + LG_MSG_DICT_SET_FAILED, + "failed to set the key (%s)" + " into dict", + key); + ret = -1; + goto out; + } + break; + case GF_DATA_TYPE_PTR: + case GF_DATA_TYPE_STR_OLD: + value = GF_MALLOC(xpair->value.gfx_value_u.other.other_len + 1, + gf_common_mt_char); + if (!value) { + errno = ENOMEM; + goto out; + } + memcpy(value, xpair->value.gfx_value_u.other.other_val, + xpair->value.gfx_value_u.other.other_len); + value[xpair->value.gfx_value_u.other.other_len] = '\0'; + free(xpair->value.gfx_value_u.other.other_val); + ret = dict_set_dynptr(this, key, value, + xpair->value.gfx_value_u.other.other_len); + break; + default: + ret = 0; + /* Unknown type and ptr type is not sent on wire */ + break; + } + if (ret) { + gf_msg_debug(THIS->name, ENOMEM, + "failed to set the key (%s) into dict", key); + } + free(xpair->key.key_val); + } + + free(dict->pairs.pairs_val); + ret = 0; + + /* If everything is fine, assign the dictionary to target */ + *to = this; + this = NULL; + +out: + if (this) + dict_unref(this); + + return ret; +} + +#endif /* !_GLUSTERFS3_H */ diff --git a/rpc/xdr/src/glusterfs4-xdr.x b/rpc/xdr/src/glusterfs4-xdr.x new file mode 100644 index 00000000000..d3b1d0dfaf0 --- /dev/null +++ b/rpc/xdr/src/glusterfs4-xdr.x @@ -0,0 +1,797 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/glusterfs-fops.h> +%#include "glusterfs3-xdr.h" + +/* Need to consume iattx and new dict in all the fops */ +struct gfx_iattx { + opaque ia_gfid[16]; + + unsigned hyper ia_flags; + unsigned hyper ia_ino; /* inode number */ + unsigned hyper ia_dev; /* backing device ID */ + unsigned hyper ia_rdev; /* device ID (if special file) */ + unsigned hyper ia_size; /* file size in bytes */ + unsigned hyper ia_blocks; /* number of 512B blocks allocated */ + unsigned hyper ia_attributes; /* chattr related:compressed, immutable, + * append only, encrypted etc.*/ + unsigned hyper ia_attributes_mask; /* Mask for the attributes */ + + hyper ia_atime; /* last access time */ + hyper ia_mtime; /* last modification time */ + hyper ia_ctime; /* last status change time */ + hyper ia_btime; /* creation time. Fill using statx */ + + unsigned int ia_atime_nsec; + unsigned int ia_mtime_nsec; + unsigned int ia_ctime_nsec; + unsigned int ia_btime_nsec; + unsigned int ia_nlink; /* Link count */ + unsigned int ia_uid; /* user ID of owner */ + unsigned int ia_gid; /* group ID of owner */ + unsigned int ia_blksize; /* blocksize for filesystem I/O */ + unsigned int mode; /* type of file and rwx mode */ +}; + +struct gfx_mdata_iatt { + hyper ia_atime; /* last access time */ + hyper ia_mtime; /* last modification time */ + hyper ia_ctime; /* last status change time */ + + unsigned int ia_atime_nsec; + unsigned int ia_mtime_nsec; + unsigned int ia_ctime_nsec; +}; + +union gfx_value switch (int type) { + case GF_DATA_TYPE_INT: + hyper value_int; + case GF_DATA_TYPE_UINT: + unsigned hyper value_uint; + case GF_DATA_TYPE_DOUBLE: + double value_dbl; + case GF_DATA_TYPE_STR: + opaque val_string<>; + case GF_DATA_TYPE_IATT: + gfx_iattx iatt; + case GF_DATA_TYPE_GFUUID: + opaque uuid[16]; + case GF_DATA_TYPE_PTR: + case GF_DATA_TYPE_STR_OLD: + opaque other<>; + case GF_DATA_TYPE_MDATA: + gfx_mdata_iatt mdata_iatt; +}; + +/* AUTH */ +/* This is used in the rpc header part itself, And not program payload. + Avoid sending large data load here. Allowed maximum is 400 bytes. + Ref: http://tools.ietf.org/html/rfc5531#section-8.2 + this is also handled in xdr-common.h +*/ +struct auth_glusterfs_params_v3 { + int pid; + unsigned int uid; + unsigned int gid; + + /* flags */ + /* Makes sense to use it for each bits */ + /* 0x1 == IS_INTERNAL? */ + /* Another 31 bits are reserved */ + unsigned int flags; + + /* birth time of the frame / call */ + unsigned int ctime_nsec; /* good to have 32bit for this */ + unsigned hyper ctime_sec; + + unsigned int groups<>; + opaque lk_owner<>; +}; + +struct gfx_dict_pair { + opaque key<>; + gfx_value value; +}; + +struct gfx_dict { + unsigned int xdr_size; + int count; + gfx_dict_pair pairs<>; +}; + +/* FOPS */ +struct gfx_common_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_common_iatt_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; + gfx_iattx stat; +}; + +struct gfx_common_2iatt_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; + gfx_iattx prestat; + gfx_iattx poststat; +}; + +struct gfx_common_3iatt_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_iattx stat; + gfx_iattx preparent; + gfx_iattx postparent; +}; + +struct gfx_fsetattr_req { + opaque gfid[16]; + hyper fd; + gfx_iattx stbuf; + int valid; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_rchecksum_req { + opaque gfid[16]; + hyper fd; + unsigned hyper offset; + unsigned int len; + unsigned int flags; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_icreate_req { + opaque gfid[16]; + unsigned int mode; + gfx_dict xdata; +}; + +struct gfx_put_req { + opaque pargfid[16]; + string bname<>; + unsigned int mode; + unsigned int umask; + unsigned int flag; + u_quad_t offset; + unsigned int size; + gfx_dict xattr; + gfx_dict xdata; +}; + +struct gfx_namelink_req { + opaque pargfid[16]; + string bname<>; + gfx_dict xdata; +}; + +/* Define every fops */ +/* Changes from Version 3: + 1. Dict has its own type instead of being opaque + 2. Iattx instead of iatt on wire + 3. gfid has 4 extra bytes so it can be used for future +*/ +struct gfx_stat_req { + opaque gfid[16]; + gfx_dict xdata; +}; + +struct gfx_readlink_req { + opaque gfid[16]; + unsigned int size; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_readlink_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_iattx buf; + string path<>; /* NULL terminated */ +}; + +struct gfx_mknod_req { + opaque pargfid[16]; + u_quad_t dev; + unsigned int mode; + unsigned int umask; + string bname<>; /* NULL terminated */ + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_mkdir_req { + opaque pargfid[16]; + unsigned int mode; + unsigned int umask; + string bname<>; /* NULL terminated */ + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_unlink_req { + opaque pargfid[16]; + string bname<>; /* NULL terminated */ + unsigned int xflags; + gfx_dict xdata; /* Extra data */ +}; + + +struct gfx_rmdir_req { + opaque pargfid[16]; + int xflags; + string bname<>; /* NULL terminated */ + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_symlink_req { + opaque pargfid[16]; + string bname<>; + unsigned int umask; + string linkname<>; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_rename_req { + opaque oldgfid[16]; + opaque newgfid[16]; + string oldbname<>; /* NULL terminated */ + string newbname<>; /* NULL terminated */ + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_rename_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_iattx stat; + gfx_iattx preoldparent; + gfx_iattx postoldparent; + gfx_iattx prenewparent; + gfx_iattx postnewparent; +}; + + + struct gfx_link_req { + opaque oldgfid[16]; + opaque newgfid[16]; + string newbname<>; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_truncate_req { + opaque gfid[16]; + u_quad_t offset; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_open_req { + opaque gfid[16]; + unsigned int flags; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_open_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + quad_t fd; +}; + +struct gfx_opendir_req { + opaque gfid[16]; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_read_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + unsigned int flag; + gfx_dict xdata; /* Extra data */ +}; + struct gfx_read_rsp { + int op_ret; + int op_errno; + gfx_iattx stat; + unsigned int size; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_lookup_req { + opaque gfid[16]; + opaque pargfid[16]; + unsigned int flags; + string bname<>; + gfx_dict xdata; /* Extra data */ +}; + + + struct gfx_write_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + unsigned int flag; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_statfs_req { + opaque gfid[16]; + gfx_dict xdata; /* Extra data */ +} ; + struct gfx_statfs_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gf_statfs statfs; +} ; + + struct gfx_lk_req { + opaque gfid[16]; + int64_t fd; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + gfx_dict xdata; /* Extra data */ +} ; + struct gfx_lk_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gf_proto_flock flock; +} ; + +struct gfx_lease_req { + opaque gfid[16]; + gf_proto_lease lease; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_lease_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gf_proto_lease lease; +} ; + +struct gfx_recall_lease_req { + opaque gfid[16]; + unsigned int lease_type; + opaque tid[16]; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_inodelk_req { + opaque gfid[16]; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + string volume<>; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_finodelk_req { + opaque gfid[16]; + quad_t fd; + unsigned int cmd; + unsigned int type; + gf_proto_flock flock; + string volume<>; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_flush_req { + opaque gfid[16]; + quad_t fd; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_fsync_req { + opaque gfid[16]; + quad_t fd; + unsigned int data; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_setxattr_req { + opaque gfid[16]; + unsigned int flags; + gfx_dict dict; + gfx_dict xdata; /* Extra data */ +} ; + + + + struct gfx_fsetxattr_req { + opaque gfid[16]; + int64_t fd; + unsigned int flags; + gfx_dict dict; + gfx_dict xdata; /* Extra data */ +} ; + + + + struct gfx_xattrop_req { + opaque gfid[16]; + unsigned int flags; + gfx_dict dict; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_common_dict_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_dict dict; + gfx_iattx prestat; + gfx_iattx poststat; +}; + + + struct gfx_fxattrop_req { + opaque gfid[16]; + quad_t fd; + unsigned int flags; + gfx_dict dict; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_getxattr_req { + opaque gfid[16]; + unsigned int namelen; + string name<>; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_fgetxattr_req { + opaque gfid[16]; + quad_t fd; + unsigned int namelen; + string name<>; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_removexattr_req { + opaque gfid[16]; + string name<>; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_fremovexattr_req { + opaque gfid[16]; + quad_t fd; + string name<>; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_fsyncdir_req { + opaque gfid[16]; + quad_t fd; + int data; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_readdir_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_readdirp_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + unsigned int size; + gfx_dict xdata; +} ; + + +struct gfx_access_req { + opaque gfid[16]; + unsigned int mask; + gfx_dict xdata; /* Extra data */ +} ; + + +struct gfx_create_req { + opaque pargfid[16]; + unsigned int flags; + unsigned int mode; + unsigned int umask; + string bname<>; + gfx_dict xdata; /* Extra data */ +} ; +struct gfx_create_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_iattx stat; + u_quad_t fd; + gfx_iattx preparent; + gfx_iattx postparent; +} ; + +struct gfx_ftruncate_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + gfx_dict xdata; /* Extra data */ +} ; + + +struct gfx_fstat_req { + opaque gfid[16]; + quad_t fd; + gfx_dict xdata; /* Extra data */ +} ; + + +struct gfx_entrylk_req { + opaque gfid[16]; + unsigned int cmd; + unsigned int type; + u_quad_t namelen; + string name<>; + string volume<>; + gfx_dict xdata; /* Extra data */ +}; + +struct gfx_fentrylk_req { + opaque gfid[16]; + quad_t fd; + unsigned int cmd; + unsigned int type; + u_quad_t namelen; + string name<>; + string volume<>; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_setattr_req { + opaque gfid[16]; + gfx_iattx stbuf; + int valid; + gfx_dict xdata; /* Extra data */ +} ; + + struct gfx_fallocate_req { + opaque gfid[16]; + quad_t fd; + unsigned int flags; + u_quad_t offset; + u_quad_t size; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_discard_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + u_quad_t size; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_zerofill_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + u_quad_t size; + gfx_dict xdata; +} ; + +struct gfx_rchecksum_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + unsigned int flags; + unsigned int weak_checksum; + opaque strong_checksum<>; +} ; + + +struct gfx_ipc_req { + int op; + gfx_dict xdata; +}; + + +struct gfx_seek_req { + opaque gfid[16]; + quad_t fd; + u_quad_t offset; + int what; + gfx_dict xdata; +}; + +struct gfx_seek_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; + u_quad_t offset; +}; + + + struct gfx_setvolume_req { + gfx_dict dict; +} ; + + struct gfx_copy_file_range_req { + opaque gfid1[16]; + opaque gfid2[16]; + quad_t fd_in; + quad_t fd_out; + u_quad_t off_in; + u_quad_t off_out; + unsigned int size; + unsigned int flag; + gfx_dict xdata; /* Extra data */ +}; + + struct gfx_setvolume_rsp { + int op_ret; + int op_errno; + gfx_dict dict; +} ; + + + struct gfx_getspec_req { + unsigned int flags; + string key<>; + gfx_dict xdata; /* Extra data */ +} ; + struct gfx_getspec_rsp { + int op_ret; + int op_errno; + string spec<>; + gfx_dict xdata; /* Extra data */ +} ; + + + struct gfx_notify_req { + unsigned int flags; + string buf<>; + gfx_dict xdata; /* Extra data */ +} ; + struct gfx_notify_rsp { + int op_ret; + int op_errno; + unsigned int flags; + string buf<>; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_releasedir_req { + opaque gfid[16]; + quad_t fd; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_release_req { + opaque gfid[16]; + quad_t fd; + gfx_dict xdata; /* Extra data */ +} ; + +struct gfx_dirlist { + u_quad_t d_ino; + u_quad_t d_off; + unsigned int d_len; + unsigned int d_type; + string name<>; + gfx_dirlist *nextentry; +}; + + +struct gfx_readdir_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_dirlist *reply; +}; + +struct gfx_dirplist { + u_quad_t d_ino; + u_quad_t d_off; + unsigned int d_len; + unsigned int d_type; + string name<>; + gfx_iattx stat; + gfx_dict dict; + gfx_dirplist *nextentry; +}; + +struct gfx_readdirp_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; /* Extra data */ + gfx_dirplist *reply; +}; + +struct gfx_set_lk_ver_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; + int lk_ver; +}; + +struct gfx_set_lk_ver_req { + string uid<>; + int lk_ver; +}; + +struct gfx_event_notify_req { + int op; + gfx_dict dict; +}; + + +struct gfx_getsnap_name_uuid_req { + gfx_dict dict; +}; + +struct gfx_getsnap_name_uuid_rsp { + int op_ret; + int op_errno; + gfx_dict dict; + string op_errstr<>; +}; + +struct gfx_getactivelk_rsp { + int op_ret; + int op_errno; + gfx_dict xdata; + gfs3_locklist *reply; +}; + +struct gfx_getactivelk_req { + opaque gfid[16]; + gfx_dict xdata; +}; + +struct gfx_setactivelk_req { + opaque gfid[16]; + gfs3_locklist *request; + gfx_dict xdata; +}; + +struct gfs4_inodelk_contention_req { + opaque gfid[16]; + struct gf_proto_flock flock; + unsigned int pid; + string domain<>; + opaque xdata<>; +}; + +struct gfs4_entrylk_contention_req { + opaque gfid[16]; + unsigned int type; + unsigned int pid; + string name<>; + string domain<>; + opaque xdata<>; +}; diff --git a/rpc/xdr/src/libgfxdr.sym b/rpc/xdr/src/libgfxdr.sym new file mode 100644 index 00000000000..8fa0e0ddd8a --- /dev/null +++ b/rpc/xdr/src/libgfxdr.sym @@ -0,0 +1,350 @@ +xdr_auth_glusterfs_parms +xdr_auth_glusterfs_parms_v2 +xdr_auth_glusterfs_params_v3 +xdr_changelog_event_req +xdr_changelog_event_rsp +xdr_changelog_probe_req +xdr_changelog_probe_rsp +xdr_dirpath +xdr_free_exports_list +xdr_free_mountlist +xdr_gd1_mgmt_brick_op_req +xdr_gd1_mgmt_brick_op_rsp +xdr_gd1_mgmt_cluster_lock_req +xdr_gd1_mgmt_cluster_lock_rsp +xdr_gd1_mgmt_cluster_unlock_req +xdr_gd1_mgmt_cluster_unlock_rsp +xdr_gd1_mgmt_commit_op_req +xdr_gd1_mgmt_commit_op_rsp +xdr_gd1_mgmt_friend_req +xdr_gd1_mgmt_friend_rsp +xdr_gd1_mgmt_friend_update +xdr_gd1_mgmt_friend_update_rsp +xdr_gd1_mgmt_probe_req +xdr_gd1_mgmt_probe_rsp +xdr_gd1_mgmt_stage_op_req +xdr_gd1_mgmt_stage_op_rsp +xdr_gd1_mgmt_v3_brick_op_req +xdr_gd1_mgmt_v3_brick_op_rsp +xdr_gd1_mgmt_v3_commit_req +xdr_gd1_mgmt_v3_commit_rsp +xdr_gd1_mgmt_v3_post_commit_req +xdr_gd1_mgmt_v3_post_commit_rsp +xdr_gd1_mgmt_v3_lock_req +xdr_gd1_mgmt_v3_lock_rsp +xdr_gd1_mgmt_v3_post_val_req +xdr_gd1_mgmt_v3_post_val_rsp +xdr_gd1_mgmt_v3_pre_val_req +xdr_gd1_mgmt_v3_pre_val_rsp +xdr_gd1_mgmt_v3_unlock_req +xdr_gd1_mgmt_v3_unlock_rsp +xdr_gf1_cli_fsm_log_req +xdr_gf1_cli_fsm_log_rsp +xdr_gf1_cli_getwd_req +xdr_gf1_cli_getwd_rsp +xdr_gf1_cli_mount_req +xdr_gf1_cli_mount_rsp +xdr_gf1_cli_peer_list_req +xdr_gf1_cli_peer_list_rsp +xdr_gf1_cli_umount_req +xdr_gf1_cli_umount_rsp +xdr_gf_cli_req +xdr_gf_cli_rsp +xdr_gf_common_rsp +xdr_gf_dump_req +xdr_gf_dump_rsp +xdr_gf_event_notify_req +xdr_gf_event_notify_rsp +xdr_gf_getsnap_name_uuid_req +xdr_gf_getsnap_name_uuid_rsp +xdr_gf_getspec_req +xdr_gf_getspec_rsp +xdr_gf_get_volume_info_req +xdr_gf_get_volume_info_rsp +xdr_gf_mgmt_hndsk_req +xdr_gf_mgmt_hndsk_rsp +xdr_gfs3_access_req +xdr_gfs3_cbk_cache_invalidation_req +xdr_gfs3_compound_req +xdr_gfs3_compound_rsp +xdr_gfs3_create_req +xdr_gfs3_create_rsp +xdr_gfs3_discard_req +xdr_gfs3_discard_rsp +xdr_gfs3_entrylk_req +xdr_gfs3_fallocate_req +xdr_gfs3_fallocate_rsp +xdr_gfs3_fentrylk_req +xdr_gfs3_fgetxattr_req +xdr_gfs3_fgetxattr_rsp +xdr_gfs3_finodelk_req +xdr_gfs3_flush_req +xdr_gfs3_fremovexattr_req +xdr_gfs3_fsetattr_req +xdr_gfs3_fsetattr_req_v2 +xdr_gfs3_fsetattr_rsp +xdr_gfs3_fsetxattr_req +xdr_gfs3_fstat_req +xdr_gfs3_fstat_rsp +xdr_gfs3_fsyncdir_req +xdr_gfs3_fsync_req +xdr_gfs3_fsync_rsp +xdr_gfs3_ftruncate_req +xdr_gfs3_ftruncate_rsp +xdr_gfs3_fxattrop_req +xdr_gfs3_fxattrop_rsp +xdr_gfs3_getactivelk_req +xdr_gfs3_getactivelk_rsp +xdr_gfs3_getxattr_req +xdr_gfs3_getxattr_rsp +xdr_gfs3_inodelk_req +xdr_gfs3_ipc_req +xdr_gfs3_ipc_rsp +xdr_gfs3_lease_req +xdr_gfs3_lease_rsp +xdr_gfs3_link_req +xdr_gfs3_link_rsp +xdr_gfs3_lk_req +xdr_gfs3_lk_rsp +xdr_gfs3_lookup_req +xdr_gfs3_lookup_rsp +xdr_gfs3_mkdir_req +xdr_gfs3_mkdir_rsp +xdr_gfs3_mknod_req +xdr_gfs3_mknod_rsp +xdr_gfs3_opendir_req +xdr_gfs3_opendir_rsp +xdr_gfs3_open_req +xdr_gfs3_open_rsp +xdr_gfs3_rchecksum_req +xdr_gfs3_rchecksum_req_v2 +xdr_gfs3_rchecksum_rsp +xdr_gfs3_readdirp_req +xdr_gfs3_readdirp_rsp +xdr_gfs3_readdir_req +xdr_gfs3_readdir_rsp +xdr_gfs3_readlink_req +xdr_gfs3_readlink_rsp +xdr_gfs3_read_req +xdr_gfs3_read_rsp +xdr_gfs3_recall_lease_req +xdr_gfs3_releasedir_req +xdr_gfs3_release_req +xdr_gfs3_removexattr_req +xdr_gfs3_rename_req +xdr_gfs3_rename_rsp +xdr_gfs3_rmdir_req +xdr_gfs3_rmdir_rsp +xdr_gfs3_seek_req +xdr_gfs3_seek_rsp +xdr_gfs3_setactivelk_req +xdr_gfs3_setactivelk_rsp +xdr_gfs3_setattr_req +xdr_gfs3_setattr_rsp +xdr_gfs3_setxattr_req +xdr_gfs3_statfs_req +xdr_gfs3_statfs_rsp +xdr_gfs3_stat_req +xdr_gfs3_stat_rsp +xdr_gfs3_symlink_req +xdr_gfs3_symlink_rsp +xdr_gfs3_truncate_req +xdr_gfs3_truncate_rsp +xdr_gfs3_unlink_req +xdr_gfs3_unlink_rsp +xdr_gfs3_write_req +xdr_gfs3_write_rsp +xdr_gfs3_xattrop_req +xdr_gfs3_xattrop_rsp +xdr_gfs3_zerofill_req +xdr_gfs3_zerofill_rsp +xdr_gfs4_entrylk_contention_req +xdr_gfs4_entrylk_contention_rsp +xdr_gfs4_icreate_req +xdr_gfs4_icreate_rsp +xdr_gfs4_inodelk_contention_req +xdr_gfs4_inodelk_contention_rsp +xdr_gfs4_namelink_req +xdr_gfs4_namelink_rsp +xdr_gf_set_lk_ver_req +xdr_gf_set_lk_ver_rsp +xdr_gf_setvolume_req +xdr_gf_setvolume_rsp +xdr_gf_statedump +xdr_length_round_up +xdr_mon +xdr_mountres3 +xdr_mountstat3 +xdr_nlm_sm_status +xdr_pmap_brick_by_port_req +xdr_pmap_brick_by_port_rsp +xdr_pmap_port_by_brick_req +xdr_pmap_port_by_brick_rsp +xdr_pmap_signin_req +xdr_pmap_signin_rsp +xdr_pmap_signout_req +xdr_pmap_signout_rsp +xdr_serialize_access3res +xdr_serialize_commit3res +xdr_serialize_create3res +xdr_serialize_exports +xdr_serialize_fsinfo3res +xdr_serialize_fsstat3res +xdr_serialize_generic +xdr_serialize_getaclreply +xdr_serialize_getattr3res +xdr_serialize_link3res +xdr_serialize_lookup3res +xdr_serialize_mkdir3res +xdr_serialize_mknod3res +xdr_serialize_mountlist +xdr_serialize_mountres3 +xdr_serialize_mountstat3 +xdr_serialize_nlm4_res +xdr_serialize_nlm4_shareres +xdr_serialize_nlm4_testargs +xdr_serialize_nlm4_testres +xdr_serialize_pathconf3res +xdr_serialize_read3res +xdr_serialize_read3res_nocopy +xdr_serialize_readdir3res +xdr_serialize_readdirp3res +xdr_serialize_readlink3res +xdr_serialize_remove3res +xdr_serialize_rename3res +xdr_serialize_rmdir3res +xdr_serialize_setaclreply +xdr_serialize_setattr3res +xdr_serialize_symlink3res +xdr_serialize_write3res +xdr_sm_stat +xdr_sm_stat_res +xdr_to_access3args +xdr_to_commit3args +xdr_to_create3args +xdr_to_fsinfo3args +xdr_to_fsstat3args +xdr_to_generic +xdr_to_getaclargs +xdr_to_getattr3args +xdr_to_link3args +xdr_to_lookup3args +xdr_to_mkdir3args +xdr_to_mknod3args +xdr_to_mountpath +xdr_to_nlm4_cancelargs +xdr_to_nlm4_freeallargs +xdr_to_nlm4_lockargs +xdr_to_nlm4_shareargs +xdr_to_nlm4_testargs +xdr_to_nlm4_unlockargs +xdr_to_pathconf3args +xdr_to_read3args +xdr_to_readdir3args +xdr_to_readdirp3args +xdr_to_readlink3args +xdr_to_remove3args +xdr_to_rename3args +xdr_to_rmdir3args +xdr_to_setaclargs +xdr_to_setattr3args +xdr_to_symlink3args +xdr_to_write3args +xdr_vector_round_up +xdr_gfx_read_rsp +xdr_gfx_iattx +xdr_gfx_mdata_iatt +xdr_gfx_value +xdr_gfx_dict_pair +xdr_gfx_dict +xdr_gfx_common_rsp +xdr_gfx_common_iatt_rsp +xdr_gfx_common_2iatt_rsp +xdr_gfx_common_3iatt_rsp +xdr_gfx_fsetattr_req +xdr_gfx_rchecksum_req +xdr_gfx_icreate_req +xdr_gfx_namelink_req +xdr_gfx_stat_req +xdr_gfx_readlink_req +xdr_gfx_readlink_rsp +xdr_gfx_mknod_req +xdr_gfx_mkdir_req +xdr_gfx_unlink_req +xdr_gfx_rmdir_req +xdr_gfx_symlink_req +xdr_gfx_rename_req +xdr_gfx_rename_rsp +xdr_gfx_link_req +xdr_gfx_truncate_req +xdr_gfx_open_req +xdr_gfx_open_rsp +xdr_gfx_opendir_req +xdr_gfx_read_req +xdr_gfx_read_rsp +xdr_gfx_lookup_req +xdr_gfx_write_req +xdr_gfx_statfs_req +xdr_gfx_statfs_rsp +xdr_gfx_lk_req +xdr_gfx_lk_rsp +xdr_gfx_lease_req +xdr_gfx_lease_rsp +xdr_gfx_recall_lease_req +xdr_gfx_inodelk_req +xdr_gfx_finodelk_req +xdr_gfx_flush_req +xdr_gfx_fsync_req +xdr_gfx_setxattr_req +xdr_gfx_fsetxattr_req +xdr_gfx_xattrop_req +xdr_gfx_common_dict_rsp +xdr_gfx_fxattrop_req +xdr_gfx_getxattr_req +xdr_gfx_fgetxattr_req +xdr_gfx_removexattr_req +xdr_gfx_fremovexattr_req +xdr_gfx_fsyncdir_req +xdr_gfx_readdir_req +xdr_gfx_readdirp_req +xdr_gfx_access_req +xdr_gfx_create_req +xdr_gfx_create_rsp +xdr_gfx_ftruncate_req +xdr_gfx_fstat_req +xdr_gfx_entrylk_req +xdr_gfx_fentrylk_req +xdr_gfx_setattr_req +xdr_gfx_fallocate_req +xdr_gfx_discard_req +xdr_gfx_zerofill_req +xdr_gfx_rchecksum_rsp +xdr_gfx_ipc_req +xdr_gfx_seek_req +xdr_gfx_seek_rsp +xdr_gfx_setvolume_req +xdr_gfx_setvolume_rsp +xdr_gfx_getspec_req +xdr_gfx_getspec_rsp +xdr_gfx_notify_req +xdr_gfx_notify_rsp +xdr_gfx_releasedir_req +xdr_gfx_release_req +xdr_gfx_dirlist +xdr_gfx_readdir_rsp +xdr_gfx_dirplist +xdr_gfx_readdirp_rsp +xdr_gfx_set_lk_ver_rsp +xdr_gfx_set_lk_ver_req +xdr_gfx_event_notify_req +xdr_gfx_getsnap_name_uuid_req +xdr_gfx_getsnap_name_uuid_rsp +xdr_gfx_getactivelk_rsp +xdr_gfx_getactivelk_req +xdr_gfx_setactivelk_req +xdr_gfx_put_req +xdr_compound_req_v2 +xdr_gfx_compound_req +xdr_compound_rsp_v2 +xdr_gfx_compound_rsp +xdr_gfx_copy_file_range_req diff --git a/rpc/xdr/src/mount3udp.x b/rpc/xdr/src/mount3udp.x new file mode 100644 index 00000000000..e8366df400c --- /dev/null +++ b/rpc/xdr/src/mount3udp.x @@ -0,0 +1,32 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + +/* This is used by rpcgen to auto generate the rpc stubs. + * mount3udp_svc.c is heavily modified though + */ + +%#include "xdr-nfs3.h" + +const MNTUDPPATHLEN = 1024; + +typedef string mntudpdirpath<MNTPATHLEN>; + +program MOUNTUDP_PROGRAM { + version MOUNTUDP_V3 { + void MOUNTUDPPROC3_NULL(void) = 0; + mountres3 MOUNTUDPPROC3_MNT (mntudpdirpath) = 1; + mountstat3 MOUNTUDPPROC3_UMNT (mntudpdirpath) = 3; + } = 3; +} = 100005; diff --git a/rpc/xdr/src/msg-nfs3.c b/rpc/xdr/src/msg-nfs3.c new file mode 100644 index 00000000000..d14a731b62a --- /dev/null +++ b/rpc/xdr/src/msg-nfs3.c @@ -0,0 +1,481 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include <sys/uio.h> +#include <rpc/rpc.h> +#include <rpc/xdr.h> +#include <sys/types.h> + +#include "xdr-nfs3.h" +#include "msg-nfs3.h" +#include "xdr-generic.h" +#include "xdr-common.h" + +/* Decode the mount path from the network message in inmsg + * into the memory referenced by outpath.iov_base. + * The size allocated for outpath.iov_base is outpath.iov_len. + * The size of the path extracted from the message is returned. + */ +ssize_t +xdr_to_mountpath(struct iovec outpath, struct iovec inmsg) +{ + XDR xdr; + ssize_t ret = -1; + char *mntpath = NULL; + + if ((!outpath.iov_base) || (!inmsg.iov_base)) + return -1; + + xdrmem_create(&xdr, inmsg.iov_base, (unsigned int)inmsg.iov_len, + XDR_DECODE); + + mntpath = outpath.iov_base; + if (!xdr_dirpath(&xdr, (dirpath *)&mntpath)) { + ret = -1; + goto ret; + } + + ret = xdr_decoded_length(xdr); + +ret: + return ret; +} + +/* Translate the mountres3 structure in res into XDR format into memory + * referenced by outmsg.iov_base. + * Returns the number of bytes used in encoding into XDR format. + */ +ssize_t +xdr_serialize_mountres3(struct iovec outmsg, mountres3 *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_mountres3); +} + +ssize_t +xdr_serialize_mountbody(struct iovec outmsg, mountbody *mb) +{ + return xdr_serialize_generic(outmsg, (void *)mb, (xdrproc_t)xdr_mountbody); +} + +ssize_t +xdr_serialize_mountlist(struct iovec outmsg, mountlist *ml) +{ + return xdr_serialize_generic(outmsg, (void *)ml, (xdrproc_t)xdr_mountlist); +} + +ssize_t +xdr_serialize_mountstat3(struct iovec outmsg, mountstat3 *m) +{ + return xdr_serialize_generic(outmsg, (void *)m, (xdrproc_t)xdr_mountstat3); +} + +ssize_t +xdr_to_getattr3args(struct iovec inmsg, getattr3args *ga) +{ + return xdr_to_generic(inmsg, (void *)ga, (xdrproc_t)xdr_getattr3args); +} + +ssize_t +xdr_serialize_getattr3res(struct iovec outmsg, getattr3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_getattr3res); +} + +ssize_t +xdr_serialize_setattr3res(struct iovec outmsg, setattr3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_setattr3res); +} + +ssize_t +xdr_to_setattr3args(struct iovec inmsg, setattr3args *sa) +{ + return xdr_to_generic(inmsg, (void *)sa, (xdrproc_t)xdr_setattr3args); +} + +ssize_t +xdr_serialize_lookup3res(struct iovec outmsg, lookup3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_lookup3res); +} + +ssize_t +xdr_to_lookup3args(struct iovec inmsg, lookup3args *la) +{ + return xdr_to_generic(inmsg, (void *)la, (xdrproc_t)xdr_lookup3args); +} + +ssize_t +xdr_to_access3args(struct iovec inmsg, access3args *ac) +{ + return xdr_to_generic(inmsg, (void *)ac, (xdrproc_t)xdr_access3args); +} + +ssize_t +xdr_serialize_access3res(struct iovec outmsg, access3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_access3res); +} + +ssize_t +xdr_to_readlink3args(struct iovec inmsg, readlink3args *ra) +{ + return xdr_to_generic(inmsg, (void *)ra, (xdrproc_t)xdr_readlink3args); +} + +ssize_t +xdr_serialize_readlink3res(struct iovec outmsg, readlink3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_readlink3res); +} + +ssize_t +xdr_to_read3args(struct iovec inmsg, read3args *ra) +{ + return xdr_to_generic(inmsg, (void *)ra, (xdrproc_t)xdr_read3args); +} + +ssize_t +xdr_serialize_read3res(struct iovec outmsg, read3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_read3res); +} + +ssize_t +xdr_serialize_read3res_nocopy(struct iovec outmsg, read3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_read3res_nocopy); +} + +ssize_t +xdr_to_write3args(struct iovec inmsg, write3args *wa) +{ + return xdr_to_generic(inmsg, (void *)wa, (xdrproc_t)xdr_write3args); +} + +ssize_t +xdr_to_write3args_nocopy(struct iovec inmsg, write3args *wa, + struct iovec *payload) +{ + return xdr_to_generic_payload(inmsg, (void *)wa, (xdrproc_t)xdr_write3args, + payload); +} + +ssize_t +xdr_serialize_write3res(struct iovec outmsg, write3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_write3res); +} + +ssize_t +xdr_to_create3args(struct iovec inmsg, create3args *ca) +{ + return xdr_to_generic(inmsg, (void *)ca, (xdrproc_t)xdr_create3args); +} + +ssize_t +xdr_serialize_create3res(struct iovec outmsg, create3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_create3res); +} + +ssize_t +xdr_serialize_mkdir3res(struct iovec outmsg, mkdir3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_mkdir3res); +} + +ssize_t +xdr_to_mkdir3args(struct iovec inmsg, mkdir3args *ma) +{ + return xdr_to_generic(inmsg, (void *)ma, (xdrproc_t)xdr_mkdir3args); +} + +ssize_t +xdr_to_symlink3args(struct iovec inmsg, symlink3args *sa) +{ + return xdr_to_generic(inmsg, (void *)sa, (xdrproc_t)xdr_symlink3args); +} + +ssize_t +xdr_serialize_symlink3res(struct iovec outmsg, symlink3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_symlink3res); +} + +ssize_t +xdr_to_mknod3args(struct iovec inmsg, mknod3args *ma) +{ + return xdr_to_generic(inmsg, (void *)ma, (xdrproc_t)xdr_mknod3args); +} + +ssize_t +xdr_serialize_mknod3res(struct iovec outmsg, mknod3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_mknod3res); +} + +ssize_t +xdr_to_remove3args(struct iovec inmsg, remove3args *ra) +{ + return xdr_to_generic(inmsg, (void *)ra, (xdrproc_t)xdr_remove3args); +} + +ssize_t +xdr_serialize_remove3res(struct iovec outmsg, remove3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_remove3res); +} + +ssize_t +xdr_to_rmdir3args(struct iovec inmsg, rmdir3args *ra) +{ + return xdr_to_generic(inmsg, (void *)ra, (xdrproc_t)xdr_rmdir3args); +} + +ssize_t +xdr_serialize_rmdir3res(struct iovec outmsg, rmdir3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_rmdir3res); +} + +ssize_t +xdr_serialize_rename3res(struct iovec outmsg, rename3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_rename3res); +} + +ssize_t +xdr_to_rename3args(struct iovec inmsg, rename3args *ra) +{ + return xdr_to_generic(inmsg, (void *)ra, (xdrproc_t)xdr_rename3args); +} + +ssize_t +xdr_serialize_link3res(struct iovec outmsg, link3res *li) +{ + return xdr_serialize_generic(outmsg, (void *)li, (xdrproc_t)xdr_link3res); +} + +ssize_t +xdr_to_link3args(struct iovec inmsg, link3args *la) +{ + return xdr_to_generic(inmsg, (void *)la, (xdrproc_t)xdr_link3args); +} + +ssize_t +xdr_to_readdir3args(struct iovec inmsg, readdir3args *rd) +{ + return xdr_to_generic(inmsg, (void *)rd, (xdrproc_t)xdr_readdir3args); +} + +ssize_t +xdr_serialize_readdir3res(struct iovec outmsg, readdir3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_readdir3res); +} + +ssize_t +xdr_to_readdirp3args(struct iovec inmsg, readdirp3args *rp) +{ + return xdr_to_generic(inmsg, (void *)rp, (xdrproc_t)xdr_readdirp3args); +} + +ssize_t +xdr_serialize_readdirp3res(struct iovec outmsg, readdirp3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_readdirp3res); +} + +ssize_t +xdr_to_fsstat3args(struct iovec inmsg, fsstat3args *fa) +{ + return xdr_to_generic(inmsg, (void *)fa, (xdrproc_t)xdr_fsstat3args); +} + +ssize_t +xdr_serialize_fsstat3res(struct iovec outmsg, fsstat3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_fsstat3res); +} + +ssize_t +xdr_to_fsinfo3args(struct iovec inmsg, fsinfo3args *fi) +{ + return xdr_to_generic(inmsg, (void *)fi, (xdrproc_t)xdr_fsinfo3args); +} + +ssize_t +xdr_serialize_fsinfo3res(struct iovec outmsg, fsinfo3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_fsinfo3res); +} + +ssize_t +xdr_to_pathconf3args(struct iovec inmsg, pathconf3args *pc) +{ + return xdr_to_generic(inmsg, (void *)pc, (xdrproc_t)xdr_pathconf3args); +} + +ssize_t +xdr_serialize_pathconf3res(struct iovec outmsg, pathconf3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_pathconf3res); +} + +ssize_t +xdr_to_commit3args(struct iovec inmsg, commit3args *ca) +{ + return xdr_to_generic(inmsg, (void *)ca, (xdrproc_t)xdr_commit3args); +} + +ssize_t +xdr_serialize_commit3res(struct iovec outmsg, commit3res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_commit3res); +} + +ssize_t +xdr_serialize_exports(struct iovec outmsg, exports *elist) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!outmsg.iov_base) || (!elist)) + return -1; + + xdrmem_create(&xdr, outmsg.iov_base, (unsigned int)outmsg.iov_len, + XDR_ENCODE); + + if (!xdr_exports(&xdr, elist)) + goto ret; + + ret = xdr_decoded_length(xdr); + +ret: + return ret; +} + +ssize_t +xdr_serialize_nfsstat3(struct iovec outmsg, nfsstat3 *s) +{ + return xdr_serialize_generic(outmsg, (void *)s, (xdrproc_t)xdr_nfsstat3); +} + +ssize_t +xdr_to_nlm4_testargs(struct iovec inmsg, nlm4_testargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_testargs); +} + +ssize_t +xdr_serialize_nlm4_testres(struct iovec outmsg, nlm4_testres *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_nlm4_testres); +} + +ssize_t +xdr_to_nlm4_lockargs(struct iovec inmsg, nlm4_lockargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_lockargs); +} + +ssize_t +xdr_serialize_nlm4_res(struct iovec outmsg, nlm4_res *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, (xdrproc_t)xdr_nlm4_res); +} + +ssize_t +xdr_to_nlm4_cancelargs(struct iovec inmsg, nlm4_cancargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_cancargs); +} + +ssize_t +xdr_to_nlm4_unlockargs(struct iovec inmsg, nlm4_unlockargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_unlockargs); +} + +ssize_t +xdr_to_nlm4_shareargs(struct iovec inmsg, nlm4_shareargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_shareargs); +} + +ssize_t +xdr_serialize_nlm4_shareres(struct iovec outmsg, nlm4_shareres *res) +{ + return xdr_serialize_generic(outmsg, (void *)res, + (xdrproc_t)xdr_nlm4_shareres); +} + +ssize_t +xdr_serialize_nlm4_testargs(struct iovec outmsg, nlm4_testargs *args) +{ + return xdr_serialize_generic(outmsg, (void *)args, + (xdrproc_t)xdr_nlm4_testargs); +} + +ssize_t +xdr_to_nlm4_res(struct iovec inmsg, nlm4_res *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_res); +} + +ssize_t +xdr_to_nlm4_freeallargs(struct iovec inmsg, nlm4_freeallargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_nlm4_freeallargs); +} + +ssize_t +xdr_to_getaclargs(struct iovec inmsg, getaclargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_getaclargs); +} + +ssize_t +xdr_to_setaclargs(struct iovec inmsg, setaclargs *args) +{ + return xdr_to_generic(inmsg, (void *)args, (xdrproc_t)xdr_setaclargs); +} + +ssize_t +xdr_serialize_getaclreply(struct iovec inmsg, getaclreply *res) +{ + return xdr_serialize_generic(inmsg, (void *)res, + (xdrproc_t)xdr_getaclreply); +} + +ssize_t +xdr_serialize_setaclreply(struct iovec inmsg, setaclreply *res) +{ + return xdr_serialize_generic(inmsg, (void *)res, + (xdrproc_t)xdr_setaclreply); +} diff --git a/rpc/xdr/src/msg-nfs3.h b/rpc/xdr/src/msg-nfs3.h new file mode 100644 index 00000000000..869ddc3524a --- /dev/null +++ b/rpc/xdr/src/msg-nfs3.h @@ -0,0 +1,219 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _MSG_NFS3_H_ +#define _MSG_NFS3_H_ + +#include "xdr-nfs3.h" +#include "nlm4-xdr.h" +#include "acl3-xdr.h" +#include <sys/types.h> +#include <sys/uio.h> + +extern ssize_t +xdr_to_mountpath(struct iovec outpath, struct iovec inmsg); + +extern ssize_t +xdr_serialize_mountres3(struct iovec outmsg, mountres3 *res); + +extern ssize_t +xdr_serialize_mountbody(struct iovec outmsg, mountbody *mb); + +extern ssize_t +xdr_to_getattr3args(struct iovec inmsg, getattr3args *ga); + +extern ssize_t +xdr_serialize_getattr3res(struct iovec outmsg, getattr3res *res); + +extern ssize_t +xdr_serialize_setattr3res(struct iovec outmsg, setattr3res *res); + +extern ssize_t +xdr_to_setattr3args(struct iovec inmsg, setattr3args *sa); + +extern ssize_t +xdr_serialize_lookup3res(struct iovec outmsg, lookup3res *res); + +extern ssize_t +xdr_to_lookup3args(struct iovec inmsg, lookup3args *la); + +extern ssize_t +xdr_to_access3args(struct iovec inmsg, access3args *ac); + +extern ssize_t +xdr_serialize_access3res(struct iovec outmsg, access3res *res); + +extern ssize_t +xdr_to_readlink3args(struct iovec inmsg, readlink3args *ra); + +extern ssize_t +xdr_serialize_readlink3res(struct iovec outmsg, readlink3res *res); + +extern ssize_t +xdr_to_read3args(struct iovec inmsg, read3args *ra); + +extern ssize_t +xdr_serialize_read3res(struct iovec outmsg, read3res *res); + +extern ssize_t +xdr_serialize_read3res_nocopy(struct iovec outmsg, read3res *res); + +extern ssize_t +xdr_to_write3args(struct iovec inmsg, write3args *wa); + +extern ssize_t +xdr_to_write3args_nocopy(struct iovec inmsg, write3args *wa, + struct iovec *payload); + +extern ssize_t +xdr_serialize_write3res(struct iovec outmsg, write3res *res); + +extern ssize_t +xdr_to_create3args(struct iovec inmsg, create3args *ca); + +extern ssize_t +xdr_serialize_create3res(struct iovec outmsg, create3res *res); + +extern ssize_t +xdr_serialize_mkdir3res(struct iovec outmsg, mkdir3res *res); + +extern ssize_t +xdr_to_mkdir3args(struct iovec inmsg, mkdir3args *ma); + +extern ssize_t +xdr_to_symlink3args(struct iovec inmsg, symlink3args *sa); + +extern ssize_t +xdr_serialize_symlink3res(struct iovec outmsg, symlink3res *res); + +extern ssize_t +xdr_to_mknod3args(struct iovec inmsg, mknod3args *ma); + +extern ssize_t +xdr_serialize_mknod3res(struct iovec outmsg, mknod3res *res); + +extern ssize_t +xdr_to_remove3args(struct iovec inmsg, remove3args *ra); + +extern ssize_t +xdr_serialize_remove3res(struct iovec outmsg, remove3res *res); + +extern ssize_t +xdr_to_rmdir3args(struct iovec inmsg, rmdir3args *ra); + +extern ssize_t +xdr_serialize_rmdir3res(struct iovec outmsg, rmdir3res *res); + +extern ssize_t +xdr_serialize_rename3res(struct iovec outmsg, rename3res *res); + +extern ssize_t +xdr_to_rename3args(struct iovec inmsg, rename3args *ra); + +extern ssize_t +xdr_serialize_link3res(struct iovec outmsg, link3res *li); + +extern ssize_t +xdr_to_link3args(struct iovec inmsg, link3args *la); + +extern ssize_t +xdr_to_readdir3args(struct iovec inmsg, readdir3args *rd); + +extern ssize_t +xdr_serialize_readdir3res(struct iovec outmsg, readdir3res *res); + +extern ssize_t +xdr_to_readdirp3args(struct iovec inmsg, readdirp3args *rp); + +extern ssize_t +xdr_serialize_readdirp3res(struct iovec outmsg, readdirp3res *res); + +extern ssize_t +xdr_to_fsstat3args(struct iovec inmsg, fsstat3args *fa); + +extern ssize_t +xdr_serialize_fsstat3res(struct iovec outmsg, fsstat3res *res); + +extern ssize_t +xdr_to_fsinfo3args(struct iovec inmsg, fsinfo3args *fi); + +extern ssize_t +xdr_serialize_fsinfo3res(struct iovec outmsg, fsinfo3res *res); + +extern ssize_t +xdr_to_pathconf3args(struct iovec inmsg, pathconf3args *pc); + +extern ssize_t +xdr_serialize_pathconf3res(struct iovec outmsg, pathconf3res *res); + +extern ssize_t +xdr_to_commit3args(struct iovec inmsg, commit3args *ca); + +extern ssize_t +xdr_serialize_commit3res(struct iovec outmsg, commit3res *res); + +extern ssize_t +xdr_serialize_exports(struct iovec outmsg, exports *elist); + +extern ssize_t +xdr_serialize_mountlist(struct iovec outmsg, mountlist *ml); + +extern ssize_t +xdr_serialize_mountstat3(struct iovec outmsg, mountstat3 *m); + +extern ssize_t +xdr_serialize_nfsstat3(struct iovec outmsg, nfsstat3 *s); + +extern ssize_t +xdr_to_nlm4_testargs(struct iovec inmsg, nlm4_testargs *args); + +extern ssize_t +xdr_serialize_nlm4_testres(struct iovec outmsg, nlm4_testres *res); + +extern ssize_t +xdr_to_nlm4_lockargs(struct iovec inmsg, nlm4_lockargs *args); + +extern ssize_t +xdr_serialize_nlm4_res(struct iovec outmsg, nlm4_res *res); + +extern ssize_t +xdr_to_nlm4_cancelargs(struct iovec inmsg, nlm4_cancargs *args); + +extern ssize_t +xdr_to_nlm4_unlockargs(struct iovec inmsg, nlm4_unlockargs *args); + +extern ssize_t +xdr_to_nlm4_shareargs(struct iovec inmsg, nlm4_shareargs *args); + +extern ssize_t +xdr_serialize_nlm4_shareres(struct iovec outmsg, nlm4_shareres *res); + +extern ssize_t +xdr_serialize_nlm4_testargs(struct iovec outmsg, nlm4_testargs *args); + +extern ssize_t +xdr_to_nlm4_res(struct iovec inmsg, nlm4_res *args); + +extern ssize_t +xdr_to_nlm4_freeallargs(struct iovec inmsg, nlm4_freeallargs *args); + +extern ssize_t +xdr_to_getaclargs(struct iovec inmsg, getaclargs *args); + +extern ssize_t +xdr_to_setaclargs(struct iovec inmsg, setaclargs *args); + +extern ssize_t +xdr_serialize_getaclreply(struct iovec inmsg, getaclreply *res); + +extern ssize_t +xdr_serialize_setaclreply(struct iovec inmsg, setaclreply *res); + +#endif diff --git a/rpc/xdr/src/nlm4-xdr.x b/rpc/xdr/src/nlm4-xdr.x new file mode 100644 index 00000000000..847b0e64491 --- /dev/null +++ b/rpc/xdr/src/nlm4-xdr.x @@ -0,0 +1,167 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + +/* .x file defined as according to the RFC */ + +%#include "xdr-common.h" + +const MAXNETOBJ_SZ = 1024; +const LM_MAXSTRLEN = 1024; +const MAXNAMELEN = 1025; + +typedef opaque nlm4_netobj<MAXNETOBJ_SZ>; + +#ifdef RPC_HDR +%/* +% * The following enums are actually bit encoded for efficient +% * boolean algebra.... DON'T change them..... +% */ +#endif +enum fsh_mode { + fsm_DN = 0, /* deny none */ + fsm_DR = 1, /* deny read */ + fsm_DW = 2, /* deny write */ + fsm_DRW = 3 /* deny read/write */ +}; + +enum fsh_access { + fsa_NONE = 0, /* for completeness */ + fsa_R = 1, /* read only */ + fsa_W = 2, /* write only */ + fsa_RW = 3 /* read/write */ +}; + +#ifdef RPC_HDR +%/* definitions for NLM version 4 */ +#endif +enum nlm4_stats { + nlm4_granted = 0, + nlm4_denied = 1, + nlm4_denied_nolock = 2, + nlm4_blocked = 3, + nlm4_denied_grace_period = 4, + nlm4_deadlck = 5, + nlm4_rofs = 6, + nlm4_stale_fh = 7, + nlm4_fbig = 8, + nlm4_failed = 9 +}; + +struct nlm4_stat { + nlm4_stats stat; +}; + +struct nlm4_holder { + bool exclusive; + u_int32_t svid; + nlm4_netobj oh; + u_int64_t l_offset; + u_int64_t l_len; +}; + +struct nlm4_lock { + string caller_name<LM_MAXSTRLEN>; + nlm4_netobj fh; + nlm4_netobj oh; + u_int32_t svid; + u_int64_t l_offset; + u_int64_t l_len; +}; + +struct nlm4_share { + string caller_name<LM_MAXSTRLEN>; + nlm4_netobj fh; + nlm4_netobj oh; + fsh_mode mode; + fsh_access access; +}; + +union nlm4_testrply switch (nlm4_stats stat) { + case nlm4_denied: + nlm4_holder holder; + default: + void; +}; + +struct nlm4_testres { + nlm4_netobj cookie; + nlm4_testrply stat; +}; + +struct nlm4_testargs { + nlm4_netobj cookie; + bool exclusive; + nlm4_lock alock; +}; + +struct nlm4_res { + nlm4_netobj cookie; + nlm4_stat stat; +}; + +struct nlm4_lockargs { + nlm4_netobj cookie; + bool block; + bool exclusive; + nlm4_lock alock; + bool reclaim; /* used for recovering locks */ + int32_t state; /* specify local status monitor state */ +}; + +struct nlm4_cancargs { + nlm4_netobj cookie; + bool block; + bool exclusive; + nlm4_lock alock; +}; + +struct nlm4_unlockargs { + nlm4_netobj cookie; + nlm4_lock alock; +}; + +struct nlm4_shareargs { + nlm4_netobj cookie; + nlm4_share share; + bool reclaim; +}; + +struct nlm4_shareres { + nlm4_netobj cookie; + nlm4_stats stat; + int32_t sequence; +}; + +struct nlm4_freeallargs { + string name<LM_MAXSTRLEN>; /* client hostname */ + uint32_t state; /* unused */ +}; + +/* + * argument for the procedure called by rpc.statd when a monitored host + * status change. + * XXX assumes LM_MAXSTRLEN == SM_MAXSTRLEN + */ +struct nlm_sm_status { + string mon_name<LM_MAXSTRLEN>; /* name of host */ + int state; /* new state */ + opaque priv[16]; /* private data */ +}; + +program NLMCBK_PROGRAM { + version NLMCBK_V1 { + void NLMCBK_SM_NOTIFY(nlm_sm_status) = 16; + } = 1; +} = 100021; diff --git a/rpc/xdr/src/nsm-xdr.x b/rpc/xdr/src/nsm-xdr.x new file mode 100644 index 00000000000..7c16a741f1d --- /dev/null +++ b/rpc/xdr/src/nsm-xdr.x @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + +/* + * This defines the maximum length of the string + * identifying the caller. + */ +const SM_MAXSTRLEN = 1024; + +struct sm_name { + string mon_name<SM_MAXSTRLEN>; +}; + +enum res { + STAT_SUCC = 0, /* NSM agrees to monitor. */ + STAT_FAIL = 1 /* NSM cannot monitor. */ +}; + +struct sm_stat_res { + res res_stat; + int state; +}; + +struct sm_stat { + int state; /* state number of NSM */ +}; + +struct my_id { + string my_name<SM_MAXSTRLEN>; /* hostname */ + int my_prog; /* RPC program number */ + int my_vers; /* program version number */ + int my_proc; /* procedure number */ +}; + +struct mon_id { + string mon_name<SM_MAXSTRLEN>; /* name of the host to be monitored */ + my_id my_id; +}; + +struct mon { + mon_id mon_id; + opaque priv[16]; /* private information */ +}; + +struct nsm_callback_status { + string mon_name<SM_MAXSTRLEN>; + int state; + opaque priv[16]; /* for private information */ +}; + diff --git a/rpc/xdr/src/portmap-xdr.x b/rpc/xdr/src/portmap-xdr.x new file mode 100644 index 00000000000..23515572b9f --- /dev/null +++ b/rpc/xdr/src/portmap-xdr.x @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/compat.h> + +struct pmap_port_by_brick_req { + string brick<>; +}; + +struct pmap_port_by_brick_rsp { + int op_ret; + int op_errno; + int status; + int port; +}; + + +struct pmap_brick_by_port_req { + int port; +}; + +struct pmap_brick_by_port_rsp { + int op_ret; + int op_errno; + int status; + string brick<>; +}; + + +struct pmap_signin_req { + string brick<>; + int port; + int pid; +}; + +struct pmap_signin_rsp { + int op_ret; + int op_errno; +}; + +struct pmap_signout_req { + string brick<>; + int port; + int rdma_port; +}; + +struct pmap_signout_rsp { + int op_ret; + int op_errno; +}; diff --git a/rpc/xdr/src/rpc-common-xdr.x b/rpc/xdr/src/rpc-common-xdr.x new file mode 100644 index 00000000000..baf8b4313c8 --- /dev/null +++ b/rpc/xdr/src/rpc-common-xdr.x @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2012 Red Hat, Inc. <http://www.redhat.com> + * This file is part of GlusterFS. + * + * This file is licensed to you under your choice of the GNU Lesser + * General Public License, version 3 or any later version (LGPLv3 or + * later), or the GNU General Public License, version 2 (GPLv2), in all + * cases as published by the Free Software Foundation. + */ + +#ifdef RPC_XDR +%#include "rpc-pragmas.h" +#endif +%#include <glusterfs/glusterfs-fops.h> + +/* This file has definition of few XDR structures which are + * not captured in any section specific file */ + +%#include "xdr-common.h" + +struct auth_glusterfs_parms_v2 { + int pid; + unsigned int uid; + unsigned int gid; + unsigned int groups<>; + opaque lk_owner<>; +}; + +struct auth_glusterfs_parms { + u_quad_t lk_owner; + unsigned int pid; + unsigned int uid; + unsigned int gid; + unsigned int ngrps; + unsigned groups[16]; +}; + +struct gf_dump_req { + u_quad_t gfs_id; +}; + +struct gf_statedump { + unsigned int pid; +}; + +struct gf_prog_detail { + string progname<>; + u_quad_t prognum; + u_quad_t progver; + struct gf_prog_detail *next; +}; + + +struct gf_dump_rsp { + u_quad_t gfs_id; + int op_ret; + int op_errno; + struct gf_prog_detail *prog; +}; + + +struct gf_common_rsp { + int op_ret; + int op_errno; + opaque xdata<>; /* Extra data */ +} ; diff --git a/rpc/xdr/src/rpc-pragmas.h b/rpc/xdr/src/rpc-pragmas.h new file mode 100644 index 00000000000..4c54cf6f1df --- /dev/null +++ b/rpc/xdr/src/rpc-pragmas.h @@ -0,0 +1,28 @@ +/* + Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef GLUSTERFS_RPC_PRAGMAS_H +#define GLUSTERFS_RPC_PRAGMAS_H + +#if defined(__GNUC__) +#if __GNUC__ >= 4 +#if !defined(__clang__) +#if !defined(__NetBSD__) +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif +#else +#pragma clang diagnostic ignored "-Wunused-variable" +#pragma clang diagnostic ignored "-Wunused-value" +#endif +#endif +#endif + +#endif /* GLUSTERFS_RPC_PRAGMAS_H */ diff --git a/rpc/xdr/src/xdr-generic.c b/rpc/xdr/src/xdr-generic.c new file mode 100644 index 00000000000..20b54eb0a8a --- /dev/null +++ b/rpc/xdr/src/xdr-generic.c @@ -0,0 +1,120 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#include "xdr-generic.h" + +ssize_t +xdr_serialize_generic(struct iovec outmsg, void *res, xdrproc_t proc) +{ + ssize_t ret = -1; + XDR xdr; + + if ((!outmsg.iov_base) || (!res) || (!proc)) + return -1; + + xdrmem_create(&xdr, outmsg.iov_base, (unsigned int)outmsg.iov_len, + XDR_ENCODE); + + if (!PROC(&xdr, res)) { + ret = -1; + goto ret; + } + + ret = xdr_encoded_length(xdr); + +ret: + return ret; +} + +ssize_t +xdr_to_generic(struct iovec inmsg, void *args, xdrproc_t proc) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!inmsg.iov_base) || (!args) || (!proc)) + return -1; + + xdrmem_create(&xdr, inmsg.iov_base, (unsigned int)inmsg.iov_len, + XDR_DECODE); + + if (!PROC(&xdr, args)) { + ret = -1; + goto ret; + } + + ret = xdr_decoded_length(xdr); +ret: + return ret; +} + +ssize_t +xdr_to_generic_payload(struct iovec inmsg, void *args, xdrproc_t proc, + struct iovec *pendingpayload) +{ + XDR xdr; + ssize_t ret = -1; + + if ((!inmsg.iov_base) || (!args) || (!proc)) + return -1; + + xdrmem_create(&xdr, inmsg.iov_base, (unsigned int)inmsg.iov_len, + XDR_DECODE); + + if (!PROC(&xdr, args)) { + ret = -1; + goto ret; + } + + ret = xdr_decoded_length(xdr); + + if (pendingpayload) { + pendingpayload->iov_base = xdr_decoded_remaining_addr(xdr); + pendingpayload->iov_len = xdr_decoded_remaining_len(xdr); + } + +ret: + return ret; +} + +ssize_t +xdr_length_round_up(size_t len, size_t bufsize) +{ + int roundup = 0; + + roundup = len % XDR_BYTES_PER_UNIT; + if (roundup > 0) + roundup = XDR_BYTES_PER_UNIT - roundup; + + if ((roundup > 0) && ((roundup + len) <= bufsize)) + len += roundup; + + return len; +} + +int +xdr_bytes_round_up(struct iovec *vec, size_t bufsize) +{ + vec->iov_len = xdr_length_round_up(vec->iov_len, bufsize); + return 0; +} + +void +xdr_vector_round_up(struct iovec *vec, int vcount, uint32_t count) +{ + uint32_t round_count = 0; + + round_count = xdr_length_round_up(count, 1048576); + round_count -= count; + if (round_count == 0 || vcount <= 0) + return; + + vec[vcount - 1].iov_len += round_count; +} diff --git a/rpc/xdr/src/xdr-generic.h b/rpc/xdr/src/xdr-generic.h new file mode 100644 index 00000000000..794dda508cc --- /dev/null +++ b/rpc/xdr/src/xdr-generic.h @@ -0,0 +1,76 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _XDR_GENERIC_H +#define _XDR_GENERIC_H + +#include <sys/uio.h> +#include <rpc/types.h> +#include <rpc/xdr.h> + +#include <glusterfs/compat.h> + +#define xdr_decoded_remaining_addr(xdr) ((&xdr)->x_private) +#define xdr_decoded_remaining_len(xdr) ((&xdr)->x_handy) +#define xdr_encoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) +#define xdr_decoded_length(xdr) \ + (((size_t)(&xdr)->x_private) - ((size_t)(&xdr)->x_base)) + +#define XDR_BYTES_PER_UNIT 4 + +/* + On OSX > 10.9 + ------------- + typedef bool_t (*xdrproc_t)(XDR *, void *, unsigned int); + + On OSX < 10.9 + ------------ + typedef bool_t (*xdrproc_t)(XDR *, ...); + + FreeBSD all versions + ------------ + typedef bool_t (*xdrproc_t)(XDR *, ...); + + NetBSD 6.1.4 + ----------- + typedef bool_t (*xdrproc_t)(XDR *, const void *); + + Linux all versions + ----------- + typedef bool_t (*xdrproc_t)(XDR *, void *,...); +*/ + +#if defined(__NetBSD__) +#define PROC(xdr, res) proc(xdr, res) +#else +#define PROC(xdr, res) proc(xdr, res, 0) +#endif + +ssize_t +xdr_serialize_generic(struct iovec outmsg, void *res, xdrproc_t proc); + +ssize_t +xdr_to_generic(struct iovec inmsg, void *args, xdrproc_t proc); + +ssize_t +xdr_to_generic_payload(struct iovec inmsg, void *args, xdrproc_t proc, + struct iovec *pendingpayload); + +extern int +xdr_bytes_round_up(struct iovec *vec, size_t bufsize); + +extern ssize_t +xdr_length_round_up(size_t len, size_t bufsize); + +void +xdr_vector_round_up(struct iovec *vec, int vcount, uint32_t count); + +#endif /* !_XDR_GENERIC_H */ diff --git a/rpc/xdr/src/xdr-nfs3.c b/rpc/xdr/src/xdr-nfs3.c new file mode 100644 index 00000000000..cfccaaa89b8 --- /dev/null +++ b/rpc/xdr/src/xdr-nfs3.c @@ -0,0 +1,1907 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#if defined(__GNUC__) +#if __GNUC__ >= 4 +#if !defined(__clang__) +#if !defined(__NetBSD__) +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" +#pragma GCC diagnostic ignored "-Wunused-variable" +#endif +#else +#pragma clang diagnostic ignored "-Wunused-variable" +#pragma clang diagnostic ignored "-Wunused-value" +#endif +#endif +#endif + +#include "xdr-nfs3.h" +#include <glusterfs/mem-pool.h> +#include "xdr-common.h" + +bool_t +xdr_uint64(XDR *xdrs, uint64 *objp) +{ + if (!xdr_uint64_t(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_int64(XDR *xdrs, int64 *objp) +{ + if (!xdr_int64_t(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_uint32(XDR *xdrs, uint32 *objp) +{ + if (!xdr_uint32_t(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_int32(XDR *xdrs, int32 *objp) +{ + if (!xdr_int32_t(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_filename3(XDR *xdrs, filename3 *objp) +{ + if (!xdr_string(xdrs, objp, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_nfspath3(XDR *xdrs, nfspath3 *objp) +{ + if (!xdr_string(xdrs, objp, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fileid3(XDR *xdrs, fileid3 *objp) +{ + if (!xdr_uint64(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_cookie3(XDR *xdrs, cookie3 *objp) +{ + if (!xdr_uint64(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_cookieverf3(XDR *xdrs, cookieverf3 objp) +{ + if (!xdr_opaque(xdrs, objp, NFS3_COOKIEVERFSIZE)) + return FALSE; + return TRUE; +} + +bool_t +xdr_createverf3(XDR *xdrs, createverf3 objp) +{ + if (!xdr_opaque(xdrs, objp, NFS3_CREATEVERFSIZE)) + return FALSE; + return TRUE; +} + +bool_t +xdr_writeverf3(XDR *xdrs, writeverf3 objp) +{ + if (!xdr_opaque(xdrs, objp, NFS3_WRITEVERFSIZE)) + return FALSE; + return TRUE; +} + +bool_t +xdr_uid3(XDR *xdrs, uid3 *objp) +{ + if (!xdr_uint32(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_gid3(XDR *xdrs, gid3 *objp) +{ + if (!xdr_uint32(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_size3(XDR *xdrs, size3 *objp) +{ + if (!xdr_uint64(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_offset3(XDR *xdrs, offset3 *objp) +{ + if (!xdr_uint64(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mode3(XDR *xdrs, mode3 *objp) +{ + if (!xdr_uint32(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_count3(XDR *xdrs, count3 *objp) +{ + if (!xdr_uint32(xdrs, objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_nfsstat3(XDR *xdrs, nfsstat3 *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_ftype3(XDR *xdrs, ftype3 *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_specdata3(XDR *xdrs, specdata3 *objp) +{ + if (!xdr_uint32(xdrs, &objp->specdata1)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->specdata2)) + return FALSE; + return TRUE; +} + +bool_t +xdr_nfs_fh3(XDR *xdrs, nfs_fh3 *objp) +{ + if (!xdr_bytes(xdrs, (char **)&objp->data.data_val, + (u_int *)&objp->data.data_len, NFS3_FHSIZE)) + return FALSE; + return TRUE; +} + +bool_t +xdr_nfstime3(XDR *xdrs, nfstime3 *objp) +{ + if (!xdr_uint32(xdrs, &objp->seconds)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->nseconds)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fattr3(XDR *xdrs, fattr3 *objp) +{ + if (!xdr_ftype3(xdrs, &objp->type)) + return FALSE; + if (!xdr_mode3(xdrs, &objp->mode)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->nlink)) + return FALSE; + if (!xdr_uid3(xdrs, &objp->uid)) + return FALSE; + if (!xdr_gid3(xdrs, &objp->gid)) + return FALSE; + if (!xdr_size3(xdrs, &objp->size)) + return FALSE; + if (!xdr_size3(xdrs, &objp->used)) + return FALSE; + if (!xdr_specdata3(xdrs, &objp->rdev)) + return FALSE; + if (!xdr_uint64(xdrs, &objp->fsid)) + return FALSE; + if (!xdr_fileid3(xdrs, &objp->fileid)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->atime)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->mtime)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->ctime)) + return FALSE; + return TRUE; +} + +bool_t +xdr_post_op_attr(XDR *xdrs, post_op_attr *objp) +{ + if (!xdr_bool(xdrs, &objp->attributes_follow)) + return FALSE; + switch (objp->attributes_follow) { + case TRUE: + if (!xdr_fattr3(xdrs, &objp->post_op_attr_u.attributes)) + return FALSE; + break; + case FALSE: + break; + default: + return FALSE; + } + return TRUE; +} + +bool_t +xdr_wcc_attr(XDR *xdrs, wcc_attr *objp) +{ + if (!xdr_size3(xdrs, &objp->size)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->mtime)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->ctime)) + return FALSE; + return TRUE; +} + +bool_t +xdr_pre_op_attr(XDR *xdrs, pre_op_attr *objp) +{ + if (!xdr_bool(xdrs, &objp->attributes_follow)) + return FALSE; + switch (objp->attributes_follow) { + case TRUE: + if (!xdr_wcc_attr(xdrs, &objp->pre_op_attr_u.attributes)) + return FALSE; + break; + case FALSE: + break; + default: + return FALSE; + } + return TRUE; +} + +bool_t +xdr_wcc_data(XDR *xdrs, wcc_data *objp) +{ + if (!xdr_pre_op_attr(xdrs, &objp->before)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->after)) + return FALSE; + return TRUE; +} + +bool_t +xdr_post_op_fh3(XDR *xdrs, post_op_fh3 *objp) +{ + if (!xdr_bool(xdrs, &objp->handle_follows)) + return FALSE; + switch (objp->handle_follows) { + case TRUE: + if (!xdr_nfs_fh3(xdrs, &objp->post_op_fh3_u.handle)) + return FALSE; + break; + case FALSE: + break; + default: + return FALSE; + } + return TRUE; +} + +bool_t +xdr_time_how(XDR *xdrs, time_how *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_set_mode3(XDR *xdrs, set_mode3 *objp) +{ + if (!xdr_bool(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case TRUE: + if (!xdr_mode3(xdrs, &objp->set_mode3_u.mode)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_set_uid3(XDR *xdrs, set_uid3 *objp) +{ + if (!xdr_bool(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case TRUE: + if (!xdr_uid3(xdrs, &objp->set_uid3_u.uid)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_set_gid3(XDR *xdrs, set_gid3 *objp) +{ + if (!xdr_bool(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case TRUE: + if (!xdr_gid3(xdrs, &objp->set_gid3_u.gid)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_set_size3(XDR *xdrs, set_size3 *objp) +{ + if (!xdr_bool(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case TRUE: + if (!xdr_size3(xdrs, &objp->set_size3_u.size)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_set_atime(XDR *xdrs, set_atime *objp) +{ + if (!xdr_time_how(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case SET_TO_CLIENT_TIME: + if (!xdr_nfstime3(xdrs, &objp->set_atime_u.atime)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_set_mtime(XDR *xdrs, set_mtime *objp) +{ + if (!xdr_time_how(xdrs, &objp->set_it)) + return FALSE; + switch (objp->set_it) { + case SET_TO_CLIENT_TIME: + if (!xdr_nfstime3(xdrs, &objp->set_mtime_u.mtime)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_sattr3(XDR *xdrs, sattr3 *objp) +{ + if (!xdr_set_mode3(xdrs, &objp->mode)) + return FALSE; + if (!xdr_set_uid3(xdrs, &objp->uid)) + return FALSE; + if (!xdr_set_gid3(xdrs, &objp->gid)) + return FALSE; + if (!xdr_set_size3(xdrs, &objp->size)) + return FALSE; + if (!xdr_set_atime(xdrs, &objp->atime)) + return FALSE; + if (!xdr_set_mtime(xdrs, &objp->mtime)) + return FALSE; + return TRUE; +} + +bool_t +xdr_diropargs3(XDR *xdrs, diropargs3 *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->dir)) + return FALSE; + if (!xdr_filename3(xdrs, &objp->name)) + return FALSE; + return TRUE; +} + +bool_t +xdr_getattr3args(XDR *xdrs, getattr3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->object)) + return FALSE; + return TRUE; +} + +bool_t +xdr_getattr3resok(XDR *xdrs, getattr3resok *objp) +{ + if (!xdr_fattr3(xdrs, &objp->obj_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_getattr3res(XDR *xdrs, getattr3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_getattr3resok(xdrs, &objp->getattr3res_u.resok)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_sattrguard3(XDR *xdrs, sattrguard3 *objp) +{ + if (!xdr_bool(xdrs, &objp->check)) + return FALSE; + switch (objp->check) { + case TRUE: + if (!xdr_nfstime3(xdrs, &objp->sattrguard3_u.obj_ctime)) + return FALSE; + break; + case FALSE: + break; + default: + return FALSE; + } + return TRUE; +} + +bool_t +xdr_setattr3args(XDR *xdrs, setattr3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->object)) + return FALSE; + if (!xdr_sattr3(xdrs, &objp->new_attributes)) + return FALSE; + if (!xdr_sattrguard3(xdrs, &objp->guard)) + return FALSE; + return TRUE; +} + +bool_t +xdr_setattr3resok(XDR *xdrs, setattr3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->obj_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_setattr3resfail(XDR *xdrs, setattr3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->obj_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_setattr3res(XDR *xdrs, setattr3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_setattr3resok(xdrs, &objp->setattr3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_setattr3resfail(xdrs, &objp->setattr3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_lookup3args(XDR *xdrs, lookup3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->what)) + return FALSE; + return TRUE; +} + +bool_t +xdr_lookup3resok(XDR *xdrs, lookup3resok *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->object)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_lookup3resfail(XDR *xdrs, lookup3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_lookup3res(XDR *xdrs, lookup3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_lookup3resok(xdrs, &objp->lookup3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_lookup3resfail(xdrs, &objp->lookup3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_access3args(XDR *xdrs, access3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->object)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->access)) + return FALSE; + return TRUE; +} + +bool_t +xdr_access3resok(XDR *xdrs, access3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->access)) + return FALSE; + return TRUE; +} + +bool_t +xdr_access3resfail(XDR *xdrs, access3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_access3res(XDR *xdrs, access3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_access3resok(xdrs, &objp->access3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_access3resfail(xdrs, &objp->access3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_readlink3args(XDR *xdrs, readlink3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->symlink)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readlink3resok(XDR *xdrs, readlink3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->symlink_attributes)) + return FALSE; + if (!xdr_nfspath3(xdrs, &objp->data)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readlink3resfail(XDR *xdrs, readlink3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->symlink_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readlink3res(XDR *xdrs, readlink3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_readlink3resok(xdrs, &objp->readlink3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_readlink3resfail(xdrs, &objp->readlink3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_read3args(XDR *xdrs, read3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->file)) + return FALSE; + if (!xdr_offset3(xdrs, &objp->offset)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + return TRUE; +} + +bool_t +xdr_read3resok_nocopy(XDR *xdrs, read3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->file_attributes)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + if (!xdr_bool(xdrs, &objp->eof)) + return FALSE; + if (!xdr_u_int(xdrs, (u_int *)&objp->data.data_len)) + return FALSE; + return TRUE; +} + +bool_t +xdr_read3resok(XDR *xdrs, read3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->file_attributes)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + if (!xdr_bool(xdrs, &objp->eof)) + return FALSE; + if (!xdr_bytes(xdrs, (char **)&objp->data.data_val, + (u_int *)&objp->data.data_len, ~0)) + return FALSE; + return TRUE; +} + +bool_t +xdr_read3resfail(XDR *xdrs, read3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->file_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_read3res_nocopy(XDR *xdrs, read3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_read3resok_nocopy(xdrs, &objp->read3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_read3resfail(xdrs, &objp->read3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_read3res(XDR *xdrs, read3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_read3resok(xdrs, &objp->read3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_read3resfail(xdrs, &objp->read3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_stable_how(XDR *xdrs, stable_how *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_write3args(XDR *xdrs, write3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->file)) + return FALSE; + if (!xdr_offset3(xdrs, &objp->offset)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + if (!xdr_stable_how(xdrs, &objp->stable)) + return FALSE; + + /* Added specifically to avoid copies from the xdr buffer into + * the write3args structure, which will also require an already + * allocated buffer. That is not optimal. + */ + if (!xdr_u_int(xdrs, (u_int *)&objp->data.data_len)) + return FALSE; + + /* The remaining bytes in the xdr buffer are the bytes that need to be + * written. See how these bytes are extracted in the xdr_to_write3args + * code path. Be careful, while using the write3args structure, since + * only the data.data_len has been filled. The actual data is + * extracted in xdr_to_write3args path. + */ + + /* if (!xdr_bytes (xdrs, (char **)&objp->data.data_val, (u_int *) + &objp->data.data_len, ~0)) return FALSE; + */ + return TRUE; +} + +bool_t +xdr_write3resok(XDR *xdrs, write3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->file_wcc)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + if (!xdr_stable_how(xdrs, &objp->committed)) + return FALSE; + if (!xdr_writeverf3(xdrs, objp->verf)) + return FALSE; + return TRUE; +} + +bool_t +xdr_write3resfail(XDR *xdrs, write3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->file_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_write3res(XDR *xdrs, write3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_write3resok(xdrs, &objp->write3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_write3resfail(xdrs, &objp->write3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_createmode3(XDR *xdrs, createmode3 *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_createhow3(XDR *xdrs, createhow3 *objp) +{ + if (!xdr_createmode3(xdrs, &objp->mode)) + return FALSE; + switch (objp->mode) { + case UNCHECKED: + case GUARDED: + if (!xdr_sattr3(xdrs, &objp->createhow3_u.obj_attributes)) + return FALSE; + break; + case EXCLUSIVE: + if (!xdr_createverf3(xdrs, objp->createhow3_u.verf)) + return FALSE; + break; + default: + return FALSE; + } + return TRUE; +} + +bool_t +xdr_create3args(XDR *xdrs, create3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->where)) + return FALSE; + if (!xdr_createhow3(xdrs, &objp->how)) + return FALSE; + return TRUE; +} + +bool_t +xdr_create3resok(XDR *xdrs, create3resok *objp) +{ + if (!xdr_post_op_fh3(xdrs, &objp->obj)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_create3resfail(XDR *xdrs, create3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_create3res(XDR *xdrs, create3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_create3resok(xdrs, &objp->create3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_create3resfail(xdrs, &objp->create3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_mkdir3args(XDR *xdrs, mkdir3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->where)) + return FALSE; + if (!xdr_sattr3(xdrs, &objp->attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mkdir3resok(XDR *xdrs, mkdir3resok *objp) +{ + if (!xdr_post_op_fh3(xdrs, &objp->obj)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mkdir3resfail(XDR *xdrs, mkdir3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mkdir3res(XDR *xdrs, mkdir3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_mkdir3resok(xdrs, &objp->mkdir3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_mkdir3resfail(xdrs, &objp->mkdir3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_symlinkdata3(XDR *xdrs, symlinkdata3 *objp) +{ + if (!xdr_sattr3(xdrs, &objp->symlink_attributes)) + return FALSE; + if (!xdr_nfspath3(xdrs, &objp->symlink_data)) + return FALSE; + return TRUE; +} + +bool_t +xdr_symlink3args(XDR *xdrs, symlink3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->where)) + return FALSE; + if (!xdr_symlinkdata3(xdrs, &objp->symlink)) + return FALSE; + return TRUE; +} + +bool_t +xdr_symlink3resok(XDR *xdrs, symlink3resok *objp) +{ + if (!xdr_post_op_fh3(xdrs, &objp->obj)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_symlink3resfail(XDR *xdrs, symlink3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_symlink3res(XDR *xdrs, symlink3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_symlink3resok(xdrs, &objp->symlink3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_symlink3resfail(xdrs, &objp->symlink3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_devicedata3(XDR *xdrs, devicedata3 *objp) +{ + if (!xdr_sattr3(xdrs, &objp->dev_attributes)) + return FALSE; + if (!xdr_specdata3(xdrs, &objp->spec)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mknoddata3(XDR *xdrs, mknoddata3 *objp) +{ + if (!xdr_ftype3(xdrs, &objp->type)) + return FALSE; + switch (objp->type) { + case NF3CHR: + case NF3BLK: + if (!xdr_devicedata3(xdrs, &objp->mknoddata3_u.device)) + return FALSE; + break; + case NF3SOCK: + case NF3FIFO: + if (!xdr_sattr3(xdrs, &objp->mknoddata3_u.pipe_attributes)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_mknod3args(XDR *xdrs, mknod3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->where)) + return FALSE; + if (!xdr_mknoddata3(xdrs, &objp->what)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mknod3resok(XDR *xdrs, mknod3resok *objp) +{ + if (!xdr_post_op_fh3(xdrs, &objp->obj)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mknod3resfail(XDR *xdrs, mknod3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mknod3res(XDR *xdrs, mknod3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_mknod3resok(xdrs, &objp->mknod3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_mknod3resfail(xdrs, &objp->mknod3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_remove3args(XDR *xdrs, remove3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->object)) + return FALSE; + return TRUE; +} + +bool_t +xdr_remove3resok(XDR *xdrs, remove3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_remove3resfail(XDR *xdrs, remove3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_remove3res(XDR *xdrs, remove3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_remove3resok(xdrs, &objp->remove3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_remove3resfail(xdrs, &objp->remove3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_rmdir3args(XDR *xdrs, rmdir3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->object)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rmdir3resok(XDR *xdrs, rmdir3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rmdir3resfail(XDR *xdrs, rmdir3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->dir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rmdir3res(XDR *xdrs, rmdir3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_rmdir3resok(xdrs, &objp->rmdir3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_rmdir3resfail(xdrs, &objp->rmdir3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_rename3args(XDR *xdrs, rename3args *objp) +{ + if (!xdr_diropargs3(xdrs, &objp->from)) + return FALSE; + if (!xdr_diropargs3(xdrs, &objp->to)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rename3resok(XDR *xdrs, rename3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->fromdir_wcc)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->todir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rename3resfail(XDR *xdrs, rename3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->fromdir_wcc)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->todir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_rename3res(XDR *xdrs, rename3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_rename3resok(xdrs, &objp->rename3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_rename3resfail(xdrs, &objp->rename3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_link3args(XDR *xdrs, link3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->file)) + return FALSE; + if (!xdr_diropargs3(xdrs, &objp->link)) + return FALSE; + return TRUE; +} + +bool_t +xdr_link3resok(XDR *xdrs, link3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->file_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->linkdir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_link3resfail(XDR *xdrs, link3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->file_attributes)) + return FALSE; + if (!xdr_wcc_data(xdrs, &objp->linkdir_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_link3res(XDR *xdrs, link3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_link3resok(xdrs, &objp->link3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_link3resfail(xdrs, &objp->link3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_readdir3args(XDR *xdrs, readdir3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->dir)) + return FALSE; + if (!xdr_cookie3(xdrs, &objp->cookie)) + return FALSE; + if (!xdr_cookieverf3(xdrs, objp->cookieverf)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + return TRUE; +} + +bool_t +xdr_entry3(XDR *xdrs, entry3 *objp) +{ + if (!xdr_fileid3(xdrs, &objp->fileid)) + return FALSE; + if (!xdr_filename3(xdrs, &objp->name)) + return FALSE; + if (!xdr_cookie3(xdrs, &objp->cookie)) + return FALSE; + if (!xdr_pointer(xdrs, (char **)&objp->nextentry, sizeof(entry3), + (xdrproc_t)xdr_entry3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_dirlist3(XDR *xdrs, dirlist3 *objp) +{ + if (!xdr_pointer(xdrs, (char **)&objp->entries, sizeof(entry3), + (xdrproc_t)xdr_entry3)) + return FALSE; + if (!xdr_bool(xdrs, &objp->eof)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdir3resok(XDR *xdrs, readdir3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + if (!xdr_cookieverf3(xdrs, objp->cookieverf)) + return FALSE; + if (!xdr_dirlist3(xdrs, &objp->reply)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdir3resfail(XDR *xdrs, readdir3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdir3res(XDR *xdrs, readdir3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_readdir3resok(xdrs, &objp->readdir3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_readdir3resfail(xdrs, &objp->readdir3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_readdirp3args(XDR *xdrs, readdirp3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->dir)) + return FALSE; + if (!xdr_cookie3(xdrs, &objp->cookie)) + return FALSE; + if (!xdr_cookieverf3(xdrs, objp->cookieverf)) + return FALSE; + if (!xdr_count3(xdrs, &objp->dircount)) + return FALSE; + if (!xdr_count3(xdrs, &objp->maxcount)) + return FALSE; + return TRUE; +} + +bool_t +xdr_entryp3(XDR *xdrs, entryp3 *objp) +{ + if (!xdr_fileid3(xdrs, &objp->fileid)) + return FALSE; + if (!xdr_filename3(xdrs, &objp->name)) + return FALSE; + if (!xdr_cookie3(xdrs, &objp->cookie)) + return FALSE; + if (!xdr_post_op_attr(xdrs, &objp->name_attributes)) + return FALSE; + if (!xdr_post_op_fh3(xdrs, &objp->name_handle)) + return FALSE; + if (!xdr_pointer(xdrs, (char **)&objp->nextentry, sizeof(entryp3), + (xdrproc_t)xdr_entryp3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_dirlistp3(XDR *xdrs, dirlistp3 *objp) +{ + if (!xdr_pointer(xdrs, (char **)&objp->entries, sizeof(entryp3), + (xdrproc_t)xdr_entryp3)) + return FALSE; + if (!xdr_bool(xdrs, &objp->eof)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdirp3resok(XDR *xdrs, readdirp3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + if (!xdr_cookieverf3(xdrs, objp->cookieverf)) + return FALSE; + if (!xdr_dirlistp3(xdrs, &objp->reply)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdirp3resfail(XDR *xdrs, readdirp3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->dir_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_readdirp3res(XDR *xdrs, readdirp3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_readdirp3resok(xdrs, &objp->readdirp3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_readdirp3resfail(xdrs, &objp->readdirp3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_fsstat3args(XDR *xdrs, fsstat3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->fsroot)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsstat3resok(XDR *xdrs, fsstat3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_size3(xdrs, &objp->tbytes)) + return FALSE; + if (!xdr_size3(xdrs, &objp->fbytes)) + return FALSE; + if (!xdr_size3(xdrs, &objp->abytes)) + return FALSE; + if (!xdr_size3(xdrs, &objp->tfiles)) + return FALSE; + if (!xdr_size3(xdrs, &objp->ffiles)) + return FALSE; + if (!xdr_size3(xdrs, &objp->afiles)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->invarsec)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsstat3resfail(XDR *xdrs, fsstat3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsstat3res(XDR *xdrs, fsstat3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_fsstat3resok(xdrs, &objp->fsstat3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_fsstat3resfail(xdrs, &objp->fsstat3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_fsinfo3args(XDR *xdrs, fsinfo3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->fsroot)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsinfo3resok(XDR *xdrs, fsinfo3resok *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->rtmax)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->rtpref)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->rtmult)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->wtmax)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->wtpref)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->wtmult)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->dtpref)) + return FALSE; + if (!xdr_size3(xdrs, &objp->maxfilesize)) + return FALSE; + if (!xdr_nfstime3(xdrs, &objp->time_delta)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->properties)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsinfo3resfail(XDR *xdrs, fsinfo3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_fsinfo3res(XDR *xdrs, fsinfo3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_fsinfo3resok(xdrs, &objp->fsinfo3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_fsinfo3resfail(xdrs, &objp->fsinfo3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_pathconf3args(XDR *xdrs, pathconf3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->object)) + return FALSE; + return TRUE; +} + +bool_t +xdr_pathconf3resok(XDR *xdrs, pathconf3resok *objp) +{ + register int32_t *buf; + + if (xdrs->x_op == XDR_ENCODE) { + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->linkmax)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->name_max)) + return FALSE; + buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT); + if (buf == NULL) { + if (!xdr_bool(xdrs, &objp->no_trunc)) + return FALSE; + if (!xdr_bool(xdrs, &objp->chown_restricted)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_insensitive)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_preserving)) + return FALSE; + } else { + IXDR_PUT_BOOL(buf, objp->no_trunc); + IXDR_PUT_BOOL(buf, objp->chown_restricted); + IXDR_PUT_BOOL(buf, objp->case_insensitive); + IXDR_PUT_BOOL(buf, objp->case_preserving); + } + return TRUE; + } else if (xdrs->x_op == XDR_DECODE) { + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->linkmax)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->name_max)) + return FALSE; + buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT); + if (buf == NULL) { + if (!xdr_bool(xdrs, &objp->no_trunc)) + return FALSE; + if (!xdr_bool(xdrs, &objp->chown_restricted)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_insensitive)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_preserving)) + return FALSE; + } else { + objp->no_trunc = IXDR_GET_BOOL(buf); + objp->chown_restricted = IXDR_GET_BOOL(buf); + objp->case_insensitive = IXDR_GET_BOOL(buf); + objp->case_preserving = IXDR_GET_BOOL(buf); + } + return TRUE; + } + + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->linkmax)) + return FALSE; + if (!xdr_uint32(xdrs, &objp->name_max)) + return FALSE; + if (!xdr_bool(xdrs, &objp->no_trunc)) + return FALSE; + if (!xdr_bool(xdrs, &objp->chown_restricted)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_insensitive)) + return FALSE; + if (!xdr_bool(xdrs, &objp->case_preserving)) + return FALSE; + return TRUE; +} + +bool_t +xdr_pathconf3resfail(XDR *xdrs, pathconf3resfail *objp) +{ + if (!xdr_post_op_attr(xdrs, &objp->obj_attributes)) + return FALSE; + return TRUE; +} + +bool_t +xdr_pathconf3res(XDR *xdrs, pathconf3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_pathconf3resok(xdrs, &objp->pathconf3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_pathconf3resfail(xdrs, &objp->pathconf3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_commit3args(XDR *xdrs, commit3args *objp) +{ + if (!xdr_nfs_fh3(xdrs, &objp->file)) + return FALSE; + if (!xdr_offset3(xdrs, &objp->offset)) + return FALSE; + if (!xdr_count3(xdrs, &objp->count)) + return FALSE; + return TRUE; +} + +bool_t +xdr_commit3resok(XDR *xdrs, commit3resok *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->file_wcc)) + return FALSE; + if (!xdr_writeverf3(xdrs, objp->verf)) + return FALSE; + return TRUE; +} + +bool_t +xdr_commit3resfail(XDR *xdrs, commit3resfail *objp) +{ + if (!xdr_wcc_data(xdrs, &objp->file_wcc)) + return FALSE; + return TRUE; +} + +bool_t +xdr_commit3res(XDR *xdrs, commit3res *objp) +{ + if (!xdr_nfsstat3(xdrs, &objp->status)) + return FALSE; + switch (objp->status) { + case NFS3_OK: + if (!xdr_commit3resok(xdrs, &objp->commit3res_u.resok)) + return FALSE; + break; + default: + if (!xdr_commit3resfail(xdrs, &objp->commit3res_u.resfail)) + return FALSE; + break; + } + return TRUE; +} + +bool_t +xdr_fhandle3(XDR *xdrs, fhandle3 *objp) +{ + if (!xdr_bytes(xdrs, (char **)&objp->fhandle3_val, + (u_int *)&objp->fhandle3_len, FHSIZE3)) + return FALSE; + return TRUE; +} + +bool_t +xdr_dirpath(XDR *xdrs, dirpath *objp) +{ + if (!xdr_string(xdrs, objp, MNTPATHLEN)) + return FALSE; + return TRUE; +} + +bool_t +xdr_name(XDR *xdrs, name *objp) +{ + if (!xdr_string(xdrs, objp, MNTNAMLEN)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mountstat3(XDR *xdrs, mountstat3 *objp) +{ + if (!xdr_enum(xdrs, (enum_t *)objp)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mountres3_ok(XDR *xdrs, mountres3_ok *objp) +{ + if (!xdr_fhandle3(xdrs, &objp->fhandle)) + return FALSE; + if (!xdr_array(xdrs, (char **)&objp->auth_flavors.auth_flavors_val, + (u_int *)&objp->auth_flavors.auth_flavors_len, ~0, + sizeof(int), (xdrproc_t)xdr_int)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mountres3(XDR *xdrs, mountres3 *objp) +{ + if (!xdr_mountstat3(xdrs, &objp->fhs_status)) + return FALSE; + switch (objp->fhs_status) { + case MNT3_OK: + if (!xdr_mountres3_ok(xdrs, &objp->mountres3_u.mountinfo)) + return FALSE; + break; + default: + break; + } + return TRUE; +} + +bool_t +xdr_mountlist(XDR *xdrs, mountlist *objp) +{ + if (!xdr_pointer(xdrs, (char **)objp, sizeof(struct mountbody), + (xdrproc_t)xdr_mountbody)) + return FALSE; + return TRUE; +} + +bool_t +xdr_mountbody(XDR *xdrs, mountbody *objp) +{ + if (!xdr_name(xdrs, &objp->ml_hostname)) + return FALSE; + if (!xdr_dirpath(xdrs, &objp->ml_directory)) + return FALSE; + if (!xdr_mountlist(xdrs, &objp->ml_next)) + return FALSE; + return TRUE; +} + +bool_t +xdr_groups(XDR *xdrs, groups *objp) +{ + if (!xdr_pointer(xdrs, (char **)objp, sizeof(struct groupnode), + (xdrproc_t)xdr_groupnode)) + return FALSE; + return TRUE; +} + +bool_t +xdr_groupnode(XDR *xdrs, groupnode *objp) +{ + if (!xdr_name(xdrs, &objp->gr_name)) + return FALSE; + if (!xdr_groups(xdrs, &objp->gr_next)) + return FALSE; + return TRUE; +} + +bool_t +xdr_exports(XDR *xdrs, exports *objp) +{ + if (!xdr_pointer(xdrs, (char **)objp, sizeof(struct exportnode), + (xdrproc_t)xdr_exportnode)) + return FALSE; + return TRUE; +} + +bool_t +xdr_exportnode(XDR *xdrs, exportnode *objp) +{ + if (!xdr_dirpath(xdrs, &objp->ex_dir)) + return FALSE; + if (!xdr_groups(xdrs, &objp->ex_groups)) + return FALSE; + if (!xdr_exports(xdrs, &objp->ex_next)) + return FALSE; + return TRUE; +} + +static void +xdr_free_groupnode(struct groupnode *group) +{ + if (!group) + return; + + if (group->gr_next) + xdr_free_groupnode(group->gr_next); + + GF_FREE(group->gr_name); + GF_FREE(group); +} + +void +xdr_free_exports_list(struct exportnode *first) +{ + struct exportnode *elist = NULL; + + if (!first) + return; + + while (first) { + elist = first->ex_next; + GF_FREE(first->ex_dir); + + xdr_free_groupnode(first->ex_groups); + + GF_FREE(first); + first = elist; + } +} + +void +xdr_free_mountlist(mountlist ml) +{ + struct mountbody *next = NULL; + + if (!ml) + return; + + while (ml) { + GF_FREE(ml->ml_hostname); + GF_FREE(ml->ml_directory); + next = ml->ml_next; + GF_FREE(ml); + ml = next; + } + + return; +} + +/* Free statements are based on the way sunrpc xdr decoding + * code performs memory allocations. + */ +void +xdr_free_write3args_nocopy(write3args *wa) +{ + if (!wa) + return; + + FREE(wa->file.data.data_val); +} diff --git a/rpc/xdr/src/xdr-nfs3.h b/rpc/xdr/src/xdr-nfs3.h new file mode 100644 index 00000000000..b7f5abefffd --- /dev/null +++ b/rpc/xdr/src/xdr-nfs3.h @@ -0,0 +1,1351 @@ +/* + Copyright (c) 2007-2012 Red Hat, Inc. <http://www.redhat.com> + This file is part of GlusterFS. + + This file is licensed to you under your choice of the GNU Lesser + General Public License, version 3 or any later version (LGPLv3 or + later), or the GNU General Public License, version 2 (GPLv2), in all + cases as published by the Free Software Foundation. +*/ + +#ifndef _XDR_NFS3_H +#define _XDR_NFS3_H + +#include <rpc/rpc.h> +#include <sys/types.h> + +#define NFS3_FHSIZE 64 +#define NFS3_COOKIEVERFSIZE 8 +#define NFS3_CREATEVERFSIZE 8 +#define NFS3_WRITEVERFSIZE 8 + +#define NFS3_ENTRY3_FIXED_SIZE 24 +#define NFS3_POSTOPATTR_SIZE 88 +#define NFS3_READDIR_RESOK_SIZE \ + (NFS3_POSTOPATTR_SIZE + sizeof(bool_t) + NFS3_COOKIEVERFSIZE) + +/* In size of post_op_fh3, the length of the file handle will have to be + * included separately since we have variable length fh. Here we only account + * for the field for handle_follows and for the file handle length field. + */ +#define NFS3_POSTOPFH3_FIXED_SIZE (sizeof(bool_t) + sizeof(uint32_t)) + +/* Similarly, the size of the entry will have to include the variable length + * file handle and the length of the entry name. + */ +#define NFS3_ENTRYP3_FIXED_SIZE \ + (NFS3_ENTRY3_FIXED_SIZE + NFS3_POSTOPATTR_SIZE + NFS3_POSTOPFH3_FIXED_SIZE) + +typedef uint64_t uint64; +typedef int64_t int64; +typedef uint32_t uint32; +typedef int32_t int32; +typedef char *filename3; +typedef char *nfspath3; +typedef uint64 fileid3; +typedef uint64 cookie3; +typedef char cookieverf3[NFS3_COOKIEVERFSIZE]; +typedef char createverf3[NFS3_CREATEVERFSIZE]; +typedef char writeverf3[NFS3_WRITEVERFSIZE]; +typedef uint32 uid3; +typedef uint32 gid3; +typedef uint64 size3; +typedef uint64 offset3; +typedef uint32 mode3; +typedef uint32 count3; + +#define NFS3MODE_SETXUID 0x00800 +#define NFS3MODE_SETXGID 0x00400 +#define NFS3MODE_SAVESWAPTXT 0x00200 +#define NFS3MODE_ROWNER 0x00100 +#define NFS3MODE_WOWNER 0x00080 +#define NFS3MODE_XOWNER 0x00040 +#define NFS3MODE_RGROUP 0x00020 +#define NFS3MODE_WGROUP 0x00010 +#define NFS3MODE_XGROUP 0x00008 +#define NFS3MODE_ROTHER 0x00004 +#define NFS3MODE_WOTHER 0x00002 +#define NFS3MODE_XOTHER 0x00001 + +enum nfsstat3 { + NFS3_OK = 0, + NFS3ERR_PERM = 1, + NFS3ERR_NOENT = 2, + NFS3ERR_IO = 5, + NFS3ERR_NXIO = 6, + NFS3ERR_ACCES = 13, + NFS3ERR_EXIST = 17, + NFS3ERR_XDEV = 18, + NFS3ERR_NODEV = 19, + NFS3ERR_NOTDIR = 20, + NFS3ERR_ISDIR = 21, + NFS3ERR_INVAL = 22, + NFS3ERR_FBIG = 27, + NFS3ERR_NOSPC = 28, + NFS3ERR_ROFS = 30, + NFS3ERR_MLINK = 31, + NFS3ERR_NAMETOOLONG = 63, + NFS3ERR_NOTEMPTY = 66, + NFS3ERR_DQUOT = 69, + NFS3ERR_STALE = 70, + NFS3ERR_REMOTE = 71, + NFS3ERR_BADHANDLE = 10001, + NFS3ERR_NOT_SYNC = 10002, + NFS3ERR_BAD_COOKIE = 10003, + NFS3ERR_NOTSUPP = 10004, + NFS3ERR_TOOSMALL = 10005, + NFS3ERR_SERVERFAULT = 10006, + NFS3ERR_BADTYPE = 10007, + NFS3ERR_JUKEBOX = 10008, + NFS3ERR_END_OF_LIST = -1, +}; +typedef enum nfsstat3 nfsstat3; + +enum ftype3 { + NF3REG = 1, + NF3DIR = 2, + NF3BLK = 3, + NF3CHR = 4, + NF3LNK = 5, + NF3SOCK = 6, + NF3FIFO = 7, +}; +typedef enum ftype3 ftype3; + +struct specdata3 { + uint32 specdata1; + uint32 specdata2; +}; +typedef struct specdata3 specdata3; + +struct nfs_fh3 { + struct { + u_int data_len; + char *data_val; + } data; +}; +typedef struct nfs_fh3 nfs_fh3; + +struct nfstime3 { + uint32 seconds; + uint32 nseconds; +}; +typedef struct nfstime3 nfstime3; + +struct fattr3 { + ftype3 type; + mode3 mode; + uint32 nlink; + uid3 uid; + gid3 gid; + size3 size; + size3 used; + specdata3 rdev; + uint64 fsid; + fileid3 fileid; + nfstime3 atime; + nfstime3 mtime; + nfstime3 ctime; +}; +typedef struct fattr3 fattr3; + +struct post_op_attr { + bool_t attributes_follow; + union { + fattr3 attributes; + } post_op_attr_u; +}; +typedef struct post_op_attr post_op_attr; + +struct wcc_attr { + size3 size; + nfstime3 mtime; + nfstime3 ctime; +}; +typedef struct wcc_attr wcc_attr; + +struct pre_op_attr { + bool_t attributes_follow; + union { + wcc_attr attributes; + } pre_op_attr_u; +}; +typedef struct pre_op_attr pre_op_attr; + +struct wcc_data { + pre_op_attr before; + post_op_attr after; +}; +typedef struct wcc_data wcc_data; + +struct post_op_fh3 { + bool_t handle_follows; + union { + nfs_fh3 handle; + } post_op_fh3_u; +}; +typedef struct post_op_fh3 post_op_fh3; + +enum time_how { + DONT_CHANGE = 0, + SET_TO_SERVER_TIME = 1, + SET_TO_CLIENT_TIME = 2, +}; +typedef enum time_how time_how; + +struct set_mode3 { + bool_t set_it; + union { + mode3 mode; + } set_mode3_u; +}; +typedef struct set_mode3 set_mode3; + +struct set_uid3 { + bool_t set_it; + union { + uid3 uid; + } set_uid3_u; +}; +typedef struct set_uid3 set_uid3; + +struct set_gid3 { + bool_t set_it; + union { + gid3 gid; + } set_gid3_u; +}; +typedef struct set_gid3 set_gid3; + +struct set_size3 { + bool_t set_it; + union { + size3 size; + } set_size3_u; +}; +typedef struct set_size3 set_size3; + +struct set_atime { + time_how set_it; + union { + nfstime3 atime; + } set_atime_u; +}; +typedef struct set_atime set_atime; + +struct set_mtime { + time_how set_it; + union { + nfstime3 mtime; + } set_mtime_u; +}; +typedef struct set_mtime set_mtime; + +struct sattr3 { + set_mode3 mode; + set_uid3 uid; + set_gid3 gid; + set_size3 size; + set_atime atime; + set_mtime mtime; +}; +typedef struct sattr3 sattr3; + +struct diropargs3 { + nfs_fh3 dir; + filename3 name; +}; +typedef struct diropargs3 diropargs3; + +struct getattr3args { + nfs_fh3 object; +}; +typedef struct getattr3args getattr3args; + +struct getattr3resok { + fattr3 obj_attributes; +}; +typedef struct getattr3resok getattr3resok; + +struct getattr3res { + nfsstat3 status; + union { + getattr3resok resok; + } getattr3res_u; +}; +typedef struct getattr3res getattr3res; + +struct sattrguard3 { + bool_t check; + union { + nfstime3 obj_ctime; + } sattrguard3_u; +}; +typedef struct sattrguard3 sattrguard3; + +struct setattr3args { + nfs_fh3 object; + sattr3 new_attributes; + sattrguard3 guard; +}; +typedef struct setattr3args setattr3args; + +struct setattr3resok { + wcc_data obj_wcc; +}; +typedef struct setattr3resok setattr3resok; + +struct setattr3resfail { + wcc_data obj_wcc; +}; +typedef struct setattr3resfail setattr3resfail; + +struct setattr3res { + nfsstat3 status; + union { + setattr3resok resok; + setattr3resfail resfail; + } setattr3res_u; +}; +typedef struct setattr3res setattr3res; + +struct lookup3args { + diropargs3 what; +}; +typedef struct lookup3args lookup3args; + +struct lookup3resok { + nfs_fh3 object; + post_op_attr obj_attributes; + post_op_attr dir_attributes; +}; +typedef struct lookup3resok lookup3resok; + +struct lookup3resfail { + post_op_attr dir_attributes; +}; +typedef struct lookup3resfail lookup3resfail; + +struct lookup3res { + nfsstat3 status; + union { + lookup3resok resok; + lookup3resfail resfail; + } lookup3res_u; +}; +typedef struct lookup3res lookup3res; +#define ACCESS3_READ 0x0001 +#define ACCESS3_LOOKUP 0x0002 +#define ACCESS3_MODIFY 0x0004 +#define ACCESS3_EXTEND 0x0008 +#define ACCESS3_DELETE 0x0010 +#define ACCESS3_EXECUTE 0x0020 + +struct access3args { + nfs_fh3 object; + uint32 access; +}; +typedef struct access3args access3args; + +struct access3resok { + post_op_attr obj_attributes; + uint32 access; +}; +typedef struct access3resok access3resok; + +struct access3resfail { + post_op_attr obj_attributes; +}; +typedef struct access3resfail access3resfail; + +struct access3res { + nfsstat3 status; + union { + access3resok resok; + access3resfail resfail; + } access3res_u; +}; +typedef struct access3res access3res; + +struct readlink3args { + nfs_fh3 symlink; +}; +typedef struct readlink3args readlink3args; + +struct readlink3resok { + post_op_attr symlink_attributes; + nfspath3 data; +}; +typedef struct readlink3resok readlink3resok; + +struct readlink3resfail { + post_op_attr symlink_attributes; +}; +typedef struct readlink3resfail readlink3resfail; + +struct readlink3res { + nfsstat3 status; + union { + readlink3resok resok; + readlink3resfail resfail; + } readlink3res_u; +}; +typedef struct readlink3res readlink3res; + +struct read3args { + nfs_fh3 file; + offset3 offset; + count3 count; +}; +typedef struct read3args read3args; + +struct read3resok { + post_op_attr file_attributes; + count3 count; + bool_t eof; + struct { + u_int data_len; + char *data_val; + } data; +}; +typedef struct read3resok read3resok; + +struct read3resfail { + post_op_attr file_attributes; +}; +typedef struct read3resfail read3resfail; + +struct read3res { + nfsstat3 status; + union { + read3resok resok; + read3resfail resfail; + } read3res_u; +}; +typedef struct read3res read3res; + +enum stable_how { + UNSTABLE = 0, + DATA_SYNC = 1, + FILE_SYNC = 2, +}; +typedef enum stable_how stable_how; + +struct write3args { + nfs_fh3 file; + offset3 offset; + count3 count; + stable_how stable; + struct { + u_int data_len; + char *data_val; + } data; +}; +typedef struct write3args write3args; + +/* Generally, the protocol allows the file handle to be less than 64 bytes but + * our server does not return file handles less than 64b so we can safely say + * sizeof (nfs_fh3) rather than first trying to extract the fh size of the + * network followed by a sized-read of the file handle. + */ +#define NFS3_WRITE3ARGS_SIZE \ + (sizeof(uint32_t) + NFS3_FHSIZE + sizeof(offset3) + sizeof(count3) + \ + sizeof(uint32_t)) +struct write3resok { + wcc_data file_wcc; + count3 count; + stable_how committed; + writeverf3 verf; +}; +typedef struct write3resok write3resok; + +struct write3resfail { + wcc_data file_wcc; +}; +typedef struct write3resfail write3resfail; + +struct write3res { + nfsstat3 status; + union { + write3resok resok; + write3resfail resfail; + } write3res_u; +}; +typedef struct write3res write3res; + +enum createmode3 { + UNCHECKED = 0, + GUARDED = 1, + EXCLUSIVE = 2, +}; +typedef enum createmode3 createmode3; + +struct createhow3 { + createmode3 mode; + union { + sattr3 obj_attributes; + createverf3 verf; + } createhow3_u; +}; +typedef struct createhow3 createhow3; + +struct create3args { + diropargs3 where; + createhow3 how; +}; +typedef struct create3args create3args; + +struct create3resok { + post_op_fh3 obj; + post_op_attr obj_attributes; + wcc_data dir_wcc; +}; +typedef struct create3resok create3resok; + +struct create3resfail { + wcc_data dir_wcc; +}; +typedef struct create3resfail create3resfail; + +struct create3res { + nfsstat3 status; + union { + create3resok resok; + create3resfail resfail; + } create3res_u; +}; +typedef struct create3res create3res; + +struct mkdir3args { + diropargs3 where; + sattr3 attributes; +}; +typedef struct mkdir3args mkdir3args; + +struct mkdir3resok { + post_op_fh3 obj; + post_op_attr obj_attributes; + wcc_data dir_wcc; +}; +typedef struct mkdir3resok mkdir3resok; + +struct mkdir3resfail { + wcc_data dir_wcc; +}; +typedef struct mkdir3resfail mkdir3resfail; + +struct mkdir3res { + nfsstat3 status; + union { + mkdir3resok resok; + mkdir3resfail resfail; + } mkdir3res_u; +}; +typedef struct mkdir3res mkdir3res; + +struct symlinkdata3 { + sattr3 symlink_attributes; + nfspath3 symlink_data; +}; +typedef struct symlinkdata3 symlinkdata3; + +struct symlink3args { + diropargs3 where; + symlinkdata3 symlink; +}; +typedef struct symlink3args symlink3args; + +struct symlink3resok { + post_op_fh3 obj; + post_op_attr obj_attributes; + wcc_data dir_wcc; +}; +typedef struct symlink3resok symlink3resok; + +struct symlink3resfail { + wcc_data dir_wcc; +}; +typedef struct symlink3resfail symlink3resfail; + +struct symlink3res { + nfsstat3 status; + union { + symlink3resok resok; + symlink3resfail resfail; + } symlink3res_u; +}; +typedef struct symlink3res symlink3res; + +struct devicedata3 { + sattr3 dev_attributes; + specdata3 spec; +}; +typedef struct devicedata3 devicedata3; + +struct mknoddata3 { + ftype3 type; + union { + devicedata3 device; + sattr3 pipe_attributes; + } mknoddata3_u; +}; +typedef struct mknoddata3 mknoddata3; + +struct mknod3args { + diropargs3 where; + mknoddata3 what; +}; +typedef struct mknod3args mknod3args; + +struct mknod3resok { + post_op_fh3 obj; + post_op_attr obj_attributes; + wcc_data dir_wcc; +}; +typedef struct mknod3resok mknod3resok; + +struct mknod3resfail { + wcc_data dir_wcc; +}; +typedef struct mknod3resfail mknod3resfail; + +struct mknod3res { + nfsstat3 status; + union { + mknod3resok resok; + mknod3resfail resfail; + } mknod3res_u; +}; +typedef struct mknod3res mknod3res; + +struct remove3args { + diropargs3 object; +}; +typedef struct remove3args remove3args; + +struct remove3resok { + wcc_data dir_wcc; +}; +typedef struct remove3resok remove3resok; + +struct remove3resfail { + wcc_data dir_wcc; +}; +typedef struct remove3resfail remove3resfail; + +struct remove3res { + nfsstat3 status; + union { + remove3resok resok; + remove3resfail resfail; + } remove3res_u; +}; +typedef struct remove3res remove3res; + +struct rmdir3args { + diropargs3 object; +}; +typedef struct rmdir3args rmdir3args; + +struct rmdir3resok { + wcc_data dir_wcc; +}; +typedef struct rmdir3resok rmdir3resok; + +struct rmdir3resfail { + wcc_data dir_wcc; +}; +typedef struct rmdir3resfail rmdir3resfail; + +struct rmdir3res { + nfsstat3 status; + union { + rmdir3resok resok; + rmdir3resfail resfail; + } rmdir3res_u; +}; +typedef struct rmdir3res rmdir3res; + +struct rename3args { + diropargs3 from; + diropargs3 to; +}; +typedef struct rename3args rename3args; + +struct rename3resok { + wcc_data fromdir_wcc; + wcc_data todir_wcc; +}; +typedef struct rename3resok rename3resok; + +struct rename3resfail { + wcc_data fromdir_wcc; + wcc_data todir_wcc; +}; +typedef struct rename3resfail rename3resfail; + +struct rename3res { + nfsstat3 status; + union { + rename3resok resok; + rename3resfail resfail; + } rename3res_u; +}; +typedef struct rename3res rename3res; + +struct link3args { + nfs_fh3 file; + diropargs3 link; +}; +typedef struct link3args link3args; + +struct link3resok { + post_op_attr file_attributes; + wcc_data linkdir_wcc; +}; +typedef struct link3resok link3resok; + +struct link3resfail { + post_op_attr file_attributes; + wcc_data linkdir_wcc; +}; +typedef struct link3resfail link3resfail; + +struct link3res { + nfsstat3 status; + union { + link3resok resok; + link3resfail resfail; + } link3res_u; +}; +typedef struct link3res link3res; + +struct readdir3args { + nfs_fh3 dir; + cookie3 cookie; + cookieverf3 cookieverf; + count3 count; +}; +typedef struct readdir3args readdir3args; + +struct entry3 { + fileid3 fileid; + filename3 name; + cookie3 cookie; + struct entry3 *nextentry; +}; +typedef struct entry3 entry3; + +struct dirlist3 { + entry3 *entries; + bool_t eof; +}; +typedef struct dirlist3 dirlist3; + +struct readdir3resok { + post_op_attr dir_attributes; + cookieverf3 cookieverf; + dirlist3 reply; +}; +typedef struct readdir3resok readdir3resok; + +struct readdir3resfail { + post_op_attr dir_attributes; +}; +typedef struct readdir3resfail readdir3resfail; + +struct readdir3res { + nfsstat3 status; + union { + readdir3resok resok; + readdir3resfail resfail; + } readdir3res_u; +}; +typedef struct readdir3res readdir3res; + +struct readdirp3args { + nfs_fh3 dir; + cookie3 cookie; + cookieverf3 cookieverf; + count3 dircount; + count3 maxcount; +}; +typedef struct readdirp3args readdirp3args; + +struct entryp3 { + fileid3 fileid; + filename3 name; + cookie3 cookie; + post_op_attr name_attributes; + post_op_fh3 name_handle; + struct entryp3 *nextentry; +}; +typedef struct entryp3 entryp3; + +struct dirlistp3 { + entryp3 *entries; + bool_t eof; +}; +typedef struct dirlistp3 dirlistp3; + +struct readdirp3resok { + post_op_attr dir_attributes; + cookieverf3 cookieverf; + dirlistp3 reply; +}; +typedef struct readdirp3resok readdirp3resok; + +struct readdirp3resfail { + post_op_attr dir_attributes; +}; +typedef struct readdirp3resfail readdirp3resfail; + +struct readdirp3res { + nfsstat3 status; + union { + readdirp3resok resok; + readdirp3resfail resfail; + } readdirp3res_u; +}; +typedef struct readdirp3res readdirp3res; + +struct fsstat3args { + nfs_fh3 fsroot; +}; +typedef struct fsstat3args fsstat3args; + +struct fsstat3resok { + post_op_attr obj_attributes; + size3 tbytes; + size3 fbytes; + size3 abytes; + size3 tfiles; + size3 ffiles; + size3 afiles; + uint32 invarsec; +}; +typedef struct fsstat3resok fsstat3resok; + +struct fsstat3resfail { + post_op_attr obj_attributes; +}; +typedef struct fsstat3resfail fsstat3resfail; + +struct fsstat3res { + nfsstat3 status; + union { + fsstat3resok resok; + fsstat3resfail resfail; + } fsstat3res_u; +}; +typedef struct fsstat3res fsstat3res; +#define FSF3_LINK 0x0001 +#define FSF3_SYMLINK 0x0002 +#define FSF3_HOMOGENEOUS 0x0008 +#define FSF3_CANSETTIME 0x0010 + +struct fsinfo3args { + nfs_fh3 fsroot; +}; +typedef struct fsinfo3args fsinfo3args; + +struct fsinfo3resok { + post_op_attr obj_attributes; + uint32 rtmax; + uint32 rtpref; + uint32 rtmult; + uint32 wtmax; + uint32 wtpref; + uint32 wtmult; + uint32 dtpref; + size3 maxfilesize; + nfstime3 time_delta; + uint32 properties; +}; +typedef struct fsinfo3resok fsinfo3resok; + +struct fsinfo3resfail { + post_op_attr obj_attributes; +}; +typedef struct fsinfo3resfail fsinfo3resfail; + +struct fsinfo3res { + nfsstat3 status; + union { + fsinfo3resok resok; + fsinfo3resfail resfail; + } fsinfo3res_u; +}; +typedef struct fsinfo3res fsinfo3res; + +struct pathconf3args { + nfs_fh3 object; +}; +typedef struct pathconf3args pathconf3args; + +struct pathconf3resok { + post_op_attr obj_attributes; + uint32 linkmax; + uint32 name_max; + bool_t no_trunc; + bool_t chown_restricted; + bool_t case_insensitive; + bool_t case_preserving; +}; +typedef struct pathconf3resok pathconf3resok; + +struct pathconf3resfail { + post_op_attr obj_attributes; +}; +typedef struct pathconf3resfail pathconf3resfail; + +struct pathconf3res { + nfsstat3 status; + union { + pathconf3resok resok; + pathconf3resfail resfail; + } pathconf3res_u; +}; +typedef struct pathconf3res pathconf3res; + +struct commit3args { + nfs_fh3 file; + offset3 offset; + count3 count; +}; +typedef struct commit3args commit3args; + +struct commit3resok { + wcc_data file_wcc; + writeverf3 verf; +}; +typedef struct commit3resok commit3resok; + +struct commit3resfail { + wcc_data file_wcc; +}; +typedef struct commit3resfail commit3resfail; + +struct commit3res { + nfsstat3 status; + union { + commit3resok resok; + commit3resfail resfail; + } commit3res_u; +}; +typedef struct commit3res commit3res; +#define MNTPATHLEN 1024 +#define MNTNAMLEN 255 +#define FHSIZE3 NFS3_FHSIZE + +typedef struct { + u_int fhandle3_len; + char *fhandle3_val; +} fhandle3; + +typedef char *dirpath; + +typedef char *name; + +enum mountstat3 { + MNT3_OK = 0, + MNT3ERR_PERM = 1, + MNT3ERR_NOENT = 2, + MNT3ERR_IO = 5, + MNT3ERR_ACCES = 13, + MNT3ERR_NOTDIR = 20, + MNT3ERR_INVAL = 22, + MNT3ERR_NAMETOOLONG = 63, + MNT3ERR_NOTSUPP = 10004, + MNT3ERR_SERVERFAULT = 10006, +}; +typedef enum mountstat3 mountstat3; + +struct mountres3_ok { + fhandle3 fhandle; + struct { + u_int auth_flavors_len; + int *auth_flavors_val; + } auth_flavors; +}; +typedef struct mountres3_ok mountres3_ok; + +struct mountres3 { + mountstat3 fhs_status; + union { + mountres3_ok mountinfo; + } mountres3_u; +}; +typedef struct mountres3 mountres3; + +typedef struct mountbody *mountlist; + +struct mountbody { + name ml_hostname; + dirpath ml_directory; + mountlist ml_next; +}; +typedef struct mountbody mountbody; + +typedef struct groupnode *groups; + +struct groupnode { + name gr_name; + groups gr_next; +}; +typedef struct groupnode groupnode; + +typedef struct exportnode *exports; + +struct exportnode { + dirpath ex_dir; + groups ex_groups; + exports ex_next; +}; +typedef struct exportnode exportnode; + +#define NFS_PROGRAM 100003 +#define NFS_V3 3 + +#define NFS3_NULL 0 +#define NFS3_GETATTR 1 +#define NFS3_SETATTR 2 +#define NFS3_LOOKUP 3 +#define NFS3_ACCESS 4 +#define NFS3_READLINK 5 +#define NFS3_READ 6 +#define NFS3_WRITE 7 +#define NFS3_CREATE 8 +#define NFS3_MKDIR 9 +#define NFS3_SYMLINK 10 +#define NFS3_MKNOD 11 +#define NFS3_REMOVE 12 +#define NFS3_RMDIR 13 +#define NFS3_RENAME 14 +#define NFS3_LINK 15 +#define NFS3_READDIR 16 +#define NFS3_READDIRP 17 +#define NFS3_FSSTAT 18 +#define NFS3_FSINFO 19 +#define NFS3_PATHCONF 20 +#define NFS3_COMMIT 21 +#define NFS3_PROC_COUNT 22 + +#define MOUNT_PROGRAM 100005 +#define MOUNT_V3 3 +#define MOUNT_V1 1 + +#define MOUNT3_NULL 0 +#define MOUNT3_MNT 1 +#define MOUNT3_DUMP 2 +#define MOUNT3_UMNT 3 +#define MOUNT3_UMNTALL 4 +#define MOUNT3_EXPORT 5 +#define MOUNT3_PROC_COUNT 6 + +#define MOUNT1_NULL 0 +#define MOUNT1_MNT 1 +#define MOUNT1_DUMP 2 +#define MOUNT1_UMNT 3 +#define MOUNT1_UMNTALL 4 +#define MOUNT1_EXPORT 5 +#define MOUNT1_PROC_COUNT 6 +/* the xdr functions */ + +extern bool_t +xdr_uint64(XDR *, uint64 *); +extern bool_t +xdr_int64(XDR *, int64 *); +extern bool_t +xdr_uint32(XDR *, uint32 *); +extern bool_t +xdr_int32(XDR *, int32 *); +extern bool_t +xdr_filename3(XDR *, filename3 *); +extern bool_t +xdr_nfspath3(XDR *, nfspath3 *); +extern bool_t +xdr_fileid3(XDR *, fileid3 *); +extern bool_t +xdr_cookie3(XDR *, cookie3 *); +extern bool_t +xdr_cookieverf3(XDR *, cookieverf3); +extern bool_t +xdr_createverf3(XDR *, createverf3); +extern bool_t +xdr_writeverf3(XDR *, writeverf3); +extern bool_t +xdr_uid3(XDR *, uid3 *); +extern bool_t +xdr_gid3(XDR *, gid3 *); +extern bool_t +xdr_size3(XDR *, size3 *); +extern bool_t +xdr_offset3(XDR *, offset3 *); +extern bool_t +xdr_mode3(XDR *, mode3 *); +extern bool_t +xdr_count3(XDR *, count3 *); +extern bool_t +xdr_nfsstat3(XDR *, nfsstat3 *); +extern bool_t +xdr_ftype3(XDR *, ftype3 *); +extern bool_t +xdr_specdata3(XDR *, specdata3 *); +extern bool_t +xdr_nfs_fh3(XDR *, nfs_fh3 *); +extern bool_t +xdr_nfstime3(XDR *, nfstime3 *); +extern bool_t +xdr_fattr3(XDR *, fattr3 *); +extern bool_t +xdr_post_op_attr(XDR *, post_op_attr *); +extern bool_t +xdr_wcc_attr(XDR *, wcc_attr *); +extern bool_t +xdr_pre_op_attr(XDR *, pre_op_attr *); +extern bool_t +xdr_wcc_data(XDR *, wcc_data *); +extern bool_t +xdr_post_op_fh3(XDR *, post_op_fh3 *); +extern bool_t +xdr_time_how(XDR *, time_how *); +extern bool_t +xdr_set_mode3(XDR *, set_mode3 *); +extern bool_t +xdr_set_uid3(XDR *, set_uid3 *); +extern bool_t +xdr_set_gid3(XDR *, set_gid3 *); +extern bool_t +xdr_set_size3(XDR *, set_size3 *); +extern bool_t +xdr_set_atime(XDR *, set_atime *); +extern bool_t +xdr_set_mtime(XDR *, set_mtime *); +extern bool_t +xdr_sattr3(XDR *, sattr3 *); +extern bool_t +xdr_diropargs3(XDR *, diropargs3 *); +extern bool_t +xdr_getattr3args(XDR *, getattr3args *); +extern bool_t +xdr_getattr3resok(XDR *, getattr3resok *); +extern bool_t +xdr_getattr3res(XDR *, getattr3res *); +extern bool_t +xdr_sattrguard3(XDR *, sattrguard3 *); +extern bool_t +xdr_setattr3args(XDR *, setattr3args *); +extern bool_t +xdr_setattr3resok(XDR *, setattr3resok *); +extern bool_t +xdr_setattr3resfail(XDR *, setattr3resfail *); +extern bool_t +xdr_setattr3res(XDR *, setattr3res *); +extern bool_t +xdr_lookup3args(XDR *, lookup3args *); +extern bool_t +xdr_lookup3resok(XDR *, lookup3resok *); +extern bool_t +xdr_lookup3resfail(XDR *, lookup3resfail *); +extern bool_t +xdr_lookup3res(XDR *, lookup3res *); +extern bool_t +xdr_access3args(XDR *, access3args *); +extern bool_t +xdr_access3resok(XDR *, access3resok *); +extern bool_t +xdr_access3resfail(XDR *, access3resfail *); +extern bool_t +xdr_access3res(XDR *, access3res *); +extern bool_t +xdr_readlink3args(XDR *, readlink3args *); +extern bool_t +xdr_readlink3resok(XDR *, readlink3resok *); +extern bool_t +xdr_readlink3resfail(XDR *, readlink3resfail *); +extern bool_t +xdr_readlink3res(XDR *, readlink3res *); +extern bool_t +xdr_read3args(XDR *, read3args *); +extern bool_t +xdr_read3resok(XDR *, read3resok *); +extern bool_t +xdr_read3resfail(XDR *, read3resfail *); +extern bool_t +xdr_read3res(XDR *, read3res *); +extern bool_t +xdr_read3res_nocopy(XDR *xdrs, read3res *objp); +extern bool_t +xdr_stable_how(XDR *, stable_how *); +extern bool_t +xdr_write3args(XDR *, write3args *); +extern bool_t +xdr_write3resok(XDR *, write3resok *); +extern bool_t +xdr_write3resfail(XDR *, write3resfail *); +extern bool_t +xdr_write3res(XDR *, write3res *); +extern bool_t +xdr_createmode3(XDR *, createmode3 *); +extern bool_t +xdr_createhow3(XDR *, createhow3 *); +extern bool_t +xdr_create3args(XDR *, create3args *); +extern bool_t +xdr_create3resok(XDR *, create3resok *); +extern bool_t +xdr_create3resfail(XDR *, create3resfail *); +extern bool_t +xdr_create3res(XDR *, create3res *); +extern bool_t +xdr_mkdir3args(XDR *, mkdir3args *); +extern bool_t +xdr_mkdir3resok(XDR *, mkdir3resok *); +extern bool_t +xdr_mkdir3resfail(XDR *, mkdir3resfail *); +extern bool_t +xdr_mkdir3res(XDR *, mkdir3res *); +extern bool_t +xdr_symlinkdata3(XDR *, symlinkdata3 *); +extern bool_t +xdr_symlink3args(XDR *, symlink3args *); +extern bool_t +xdr_symlink3resok(XDR *, symlink3resok *); +extern bool_t +xdr_symlink3resfail(XDR *, symlink3resfail *); +extern bool_t +xdr_symlink3res(XDR *, symlink3res *); +extern bool_t +xdr_devicedata3(XDR *, devicedata3 *); +extern bool_t +xdr_mknoddata3(XDR *, mknoddata3 *); +extern bool_t +xdr_mknod3args(XDR *, mknod3args *); +extern bool_t +xdr_mknod3resok(XDR *, mknod3resok *); +extern bool_t +xdr_mknod3resfail(XDR *, mknod3resfail *); +extern bool_t +xdr_mknod3res(XDR *, mknod3res *); +extern bool_t +xdr_remove3args(XDR *, remove3args *); +extern bool_t +xdr_remove3resok(XDR *, remove3resok *); +extern bool_t +xdr_remove3resfail(XDR *, remove3resfail *); +extern bool_t +xdr_remove3res(XDR *, remove3res *); +extern bool_t +xdr_rmdir3args(XDR *, rmdir3args *); +extern bool_t +xdr_rmdir3resok(XDR *, rmdir3resok *); +extern bool_t +xdr_rmdir3resfail(XDR *, rmdir3resfail *); +extern bool_t +xdr_rmdir3res(XDR *, rmdir3res *); +extern bool_t +xdr_rename3args(XDR *, rename3args *); +extern bool_t +xdr_rename3resok(XDR *, rename3resok *); +extern bool_t +xdr_rename3resfail(XDR *, rename3resfail *); +extern bool_t +xdr_rename3res(XDR *, rename3res *); +extern bool_t +xdr_link3args(XDR *, link3args *); +extern bool_t +xdr_link3resok(XDR *, link3resok *); +extern bool_t +xdr_link3resfail(XDR *, link3resfail *); +extern bool_t +xdr_link3res(XDR *, link3res *); +extern bool_t +xdr_readdir3args(XDR *, readdir3args *); +extern bool_t +xdr_entry3(XDR *, entry3 *); +extern bool_t +xdr_dirlist3(XDR *, dirlist3 *); +extern bool_t +xdr_readdir3resok(XDR *, readdir3resok *); +extern bool_t +xdr_readdir3resfail(XDR *, readdir3resfail *); +extern bool_t +xdr_readdir3res(XDR *, readdir3res *); +extern bool_t +xdr_readdirp3args(XDR *, readdirp3args *); +extern bool_t +xdr_entryp3(XDR *, entryp3 *); +extern bool_t +xdr_dirlistp3(XDR *, dirlistp3 *); +extern bool_t +xdr_readdirp3resok(XDR *, readdirp3resok *); +extern bool_t +xdr_readdirp3resfail(XDR *, readdirp3resfail *); +extern bool_t +xdr_readdirp3res(XDR *, readdirp3res *); +extern bool_t +xdr_fsstat3args(XDR *, fsstat3args *); +extern bool_t +xdr_fsstat3resok(XDR *, fsstat3resok *); +extern bool_t +xdr_fsstat3resfail(XDR *, fsstat3resfail *); +extern bool_t +xdr_fsstat3res(XDR *, fsstat3res *); +extern bool_t +xdr_fsinfo3args(XDR *, fsinfo3args *); +extern bool_t +xdr_fsinfo3resok(XDR *, fsinfo3resok *); +extern bool_t +xdr_fsinfo3resfail(XDR *, fsinfo3resfail *); +extern bool_t +xdr_fsinfo3res(XDR *, fsinfo3res *); +extern bool_t +xdr_pathconf3args(XDR *, pathconf3args *); +extern bool_t +xdr_pathconf3resok(XDR *, pathconf3resok *); +extern bool_t +xdr_pathconf3resfail(XDR *, pathconf3resfail *); +extern bool_t +xdr_pathconf3res(XDR *, pathconf3res *); +extern bool_t +xdr_commit3args(XDR *, commit3args *); +extern bool_t +xdr_commit3resok(XDR *, commit3resok *); +extern bool_t +xdr_commit3resfail(XDR *, commit3resfail *); +extern bool_t +xdr_commit3res(XDR *, commit3res *); +extern bool_t +xdr_fhandle3(XDR *, fhandle3 *); +extern bool_t +xdr_dirpath(XDR *, dirpath *); +extern bool_t +xdr_name(XDR *, name *); +extern bool_t +xdr_mountstat3(XDR *, mountstat3 *); +extern bool_t +xdr_mountres3_ok(XDR *, mountres3_ok *); +extern bool_t +xdr_mountres3(XDR *, mountres3 *); +extern bool_t +xdr_mountlist(XDR *, mountlist *); +extern bool_t +xdr_mountbody(XDR *, mountbody *); +extern bool_t +xdr_groups(XDR *, groups *); +extern bool_t +xdr_groupnode(XDR *, groupnode *); +extern bool_t +xdr_exports(XDR *, exports *); +extern bool_t +xdr_exportnode(XDR *, exportnode *); + +extern void +xdr_free_exports_list(struct exportnode *first); +extern void +xdr_free_mountlist(mountlist ml); + +extern void +xdr_free_write3args_nocopy(write3args *wa); +#endif |
