/*
Copyright (c) 2010-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 "server.h"
#include "server-helpers.h"
#include "gidcache.h"
#include "server-messages.h"
#include "syscall.h"
#include "defaults.h"
#include "default-args.h"
#include "server-common.h"
#include <fnmatch.h>
#include <pwd.h>
#include "compound-fop-utils.h"
/* based on nfs_fix_aux_groups() */
int
gid_resolve (server_conf_t *conf, call_stack_t *root)
{
int ret = 0;
struct passwd mypw;
char mystrs[1024];
struct passwd *result;
gid_t *mygroups;
gid_list_t gl;
const gid_list_t *agl;
int ngroups;
agl = gid_cache_lookup (&conf->gid_cache, root->uid, 0, 0);
if (agl) {
root->ngrps = agl->gl_count;
goto fill_groups;
}
ret = getpwuid_r (root->uid, &mypw, mystrs, sizeof(mystrs), &result);
if (ret != 0) {
gf_msg ("gid-cache", GF_LOG_ERROR, errno,
PS_MSG_GET_UID_FAILED, "getpwuid_r(%u) failed",
root->uid);
return -1;
}
if (!result) {
gf_msg ("gid-cache", GF_LOG_ERROR, 0, PS_MSG_UID_NOT_FOUND,
"getpwuid_r(%u) found nothing", root->uid);
return -1;
}
gf_msg_trace ("gid-cache", 0, "mapped %u => %s", root->uid,
result->pw_name);
ngroups = gf_getgrouplist (result->pw_name, root->gid, &mygroups);
if (ngroups == -1) {
gf_msg ("gid-cache", GF_LOG_ERROR, 0, PS_MSG_MAPPING_ERROR,
"could not map %s to group list (%d gids)",
result->pw_name, root->ngrps);
return -1;
}
root->ngrps = (uint16_t) ngroups;
fill_groups:
if (agl) {
/* the gl is not complete, we only use gl.gl_list later on */
gl.gl_list = agl->gl_list;
} else {
/* setup a full gid_list_t to add it to the gid_cache */
gl.gl_id = root->uid;
gl.gl_uid = root->uid;
gl.gl_gid = root->gid;
gl.gl_count = root->ngrps;
gl.gl_list = GF_MALLOC (root->ngrps * sizeof(gid_t),
gf_common_mt_groups_t);
if (gl.gl_list)
memcpy (gl.gl_list, mygroups,
sizeof(gid_t) * root->ngrps);
else {
GF_FREE (mygroups);
return -1;
}
}
if (root->ngrps == 0) {
ret = 0;
goto out;
}
call_stack_set_groups (root, root->ngrps, mygroups);
out:
if (agl) {
gid_cache_release (&conf->gid_cache, agl);
} else {
if (gid_cache_add (&conf->gid_cache, &gl) != 1)
GF_FREE (gl.gl_list);
}
return ret;
}
int
server_resolve_groups (call_frame_t *frame, rpcsvc_request_t *req)
{
xlator_t *this = NULL;
server_conf_t *conf = NULL;
GF_VALIDATE_OR_GOTO ("server", frame, out);
GF_VALIDATE_OR_GOTO ("server", req, out);
this = req->trans->xl;
conf = this->private;
return gid_resolve (conf, frame->root);
out:
return -1;
}
int
server_decode_groups (call_frame_t *frame, rpcsvc_request_t *req)
{
int i = 0;
GF_VALIDATE_OR_GOTO ("server", frame, out);
GF_VALIDATE_OR_GOTO ("server", req, out);
if (call_stack_alloc_groups (frame->root, req->auxgidcount) != 0)
return -1;
frame->root->ngrps = req->auxgidcount;
if (frame->root->ngrps == 0)
return 0;
/* ngrps cannot be bigger than USHRT_MAX(65535) */
if (frame->root->ngrps > GF_MAX_AUX_GROUPS)
return -1;
for (; i < frame->root->ngrps; ++i)
frame->root->groups[i] = req->auxgids[i];
out:
return 0;
}
void
server_loc_wipe (loc_t *loc)
{
if (loc->parent) {
inode_unref (loc->parent);
loc->parent = NULL;
}
if (loc->inode) {
inode_unref (loc->inode);
loc->inode = NULL;
}
GF_FREE ((void *)loc->path);
}
void
server_resolve_wipe (server_resolve_t *resolve)
{
GF_FREE ((void *)resolve->path);
GF_FREE ((void *)resolve->bname);
loc_wipe (&resolve->resolve_loc);
}
void
free_state (server_state_t *state)
{
if (state->xprt) {
rpc_transport_unref (state->xprt);
state->xprt = NULL;
}
if (state->fd) {
fd_unref (state->fd);
state->fd = NULL;
}
if (state->params) {
dict_unref (state->params);
state->params = NULL;
}
if (state->iobref) {
iobref_unref (state->iobref);
state->iobref = NULL;
}
if (state->iobuf) {
iobuf_unref (state->iobuf);
state->iobuf = NULL;
}
if (state->dict) {
dict_unref (state->dict);
state->dict = NULL;
}
if (state->xdata) {
dict_unref (state->xdata);
state->xdata = NULL;
}
GF_FREE ((void *)state->volume);
GF_FREE ((void *)state->name);
server_loc_wipe (&state->loc);
server_loc_wipe (&state->loc2);
server_resolve_wipe (&state->resolve);
server_resolve_wipe (&state->resolve2);
compound_args_cleanup (state->args);
GF_FREE (state);
}
static int
server_connection_cleanup_flush_cbk (call_frame_t *frame, void *cookie,
xlator_t *this, int32_t op_ret,
int32_t op_errno, dict_t *xdata)
{
int32_t ret = -1;
fd_t *fd = NULL;
client_t *client = NULL;
GF_VALIDATE_OR_GOTO ("server", this, out);
GF_VALIDATE_OR_GOTO ("server", frame, out);
fd = frame->local;
client = frame->root->client;
fd_unref (fd);
frame->local = NULL;
gf_client_unref (client);
STACK_DESTROY (frame->root);
ret = 0;
out:
return ret;
}
static int
do_fd_cleanup (xlator_t *this, client_t* client, fdentry_t *fdentries, int fd_count)
{
fd_t *fd = NULL;
int i = 0, ret = -1;
call_frame_t *tmp_frame = NULL;
xlator_t *bound_xl = NULL;
char *path = NULL;
GF_VALIDATE_OR_GOTO ("server", this, out);
GF_VALIDATE_OR_GOTO ("server", fdentries, out);
bound_xl = client->bound_xl;
for (i = 0;i < fd_count; i++) {
fd = fdentries[i].fd;
if (fd != NULL) {
tmp_frame = create_frame (this, this->ctx->pool);
if (tmp_frame == NULL) {
goto out;
}
GF_ASSERT (fd->inode);
ret = inode_path (fd->inode, NULL, &path);
if (ret > 0) {
gf_msg (this->name, GF_LOG_INFO, 0,
PS_MSG_FD_CLEANUP,
"fd cleanup on %s", path);
GF_FREE (path);
} else {
gf_msg (this->name, GF_LOG_INFO, 0,
PS_MSG_FD_CLEANUP,
"fd cleanup on inode with gfid %s",
uuid_utoa (fd->inode->gfid));
}
tmp_frame->local = fd;
tmp_frame->root->pid = 0;
gf_client_ref (client);
tmp_frame->root->client = client;
memset (&tmp_frame->root->lk_owner, 0,
sizeof (gf_lkowner_t));
STACK_WIND (tmp_frame,
server_connection_cleanup_flush_cbk,
bound_xl, bound_xl->fops->flush, fd, NULL);
}
}
GF_FREE (fdentries);
ret = 0;
out:
return ret;
}
int
server_connection_cleanup (xlator_t *this, client_t *client,
int32_t flags)
{
server_ctx_t *serv_ctx = NULL;
fdentry_t *fdentries = NULL;
uint32_t fd_count = 0;
int cd_ret = 0;
int ret = 0;
GF_VALIDATE_OR_GOTO (this->name, this, out);
GF_VALIDATE_OR_GOTO (this->name, client, out);
GF_VALIDATE_OR_GOTO (this->name, flags, out);
serv_ctx = server_ctx_get (client, client->this);
if (serv_ctx == NULL) {
gf_msg (this->name, GF_LOG_INFO, 0,
PS_MSG_SERVER_CTX_GET_FAILED, "server_ctx_get() "
"failed");
goto out;
}
LOCK (&serv_ctx->fdtable_lock);
{
if (serv_ctx->fdtable && (flags & POSIX_LOCKS))
fdentries = gf_fd_fdtable_get_all_fds (serv_ctx->fdtable,
&fd_count);
}
UNLOCK (&serv_ctx->fdtable_lock);
if (client->bound_xl == NULL)
goto out;
if (flags & INTERNAL_LOCKS) {
cd_ret = gf_client_disconnect (client);
}
if (fdentries != NULL) {
gf_msg_debug (this->name, 0, "Performing cleanup on %d "
"fdentries", fd_count);
ret = do_fd_cleanup (this, client, fdentries, fd_count);
}
else
gf_msg (this->name, GF_LOG_INFO, 0, PS_MSG_FDENTRY_NULL,
"no fdentries to clean");
if (cd_ret || ret)
ret = -1;
out:
return ret;
}
static call_frame_t *
server_alloc_frame (rpcsvc_request_t *req)
{
call_frame_t *frame = NULL;
server_state_t *state = NULL;
client_t *client = NULL;
GF_VALIDATE_OR_GOTO ("server", req, out);
GF_VALIDATE_OR_GOTO ("server", req->trans, out);
GF_VALIDATE_OR_GOTO ("server", req->svc, out);
GF_VALIDATE_OR_GOTO ("server", req->svc->ctx, out);
client = req->trans->xl_private;
GF_VALIDATE_OR_GOTO ("server", client, out);
frame = create_frame (client->this, req->svc->ctx->pool);
if (!frame)
goto out;
state = GF_CALLOC (1, sizeof (*state), gf_server_mt_state_t);
if (!state)
goto out;
if (client->bound_xl)
state->itable = client->bound_xl->itable;
state->xprt = rpc_transport_ref (req->trans);
state->resolve.fd_no = -1;
state->resolve2.fd_no = -1;
frame->root->client = client;
frame->root->state = state; /* which socket */
frame->root->unique = 0; /* which call */
frame->this = client->this;
out:
return frame;
}
call_frame_t *
get_frame_from_request (rpcsvc_request_t *req)
{
call_frame_t *frame = NULL;
client_t *client = NULL;
client_t *tmp_client = NULL;
xlator_t *this = NULL;
server_conf_t *priv = NULL;
clienttable_t *clienttable = NULL;
unsigned int i = 0;
rpc_transport_t *trans = NULL;
server_state_t *state = NULL;
GF_VALIDATE_OR_GOTO ("server", req, out);
client = req->trans->xl_private;
frame = server_alloc_frame (req);
if (!frame)
goto out;
frame->root->op = req->procnum;
frame->root->unique = req->xid;
client = req->trans->xl_private;
this = req->trans->xl;
priv = this->private;
clienttable = this->ctx->clienttable;
for (i = 0; i < clienttable->max_clients; i++) {
tmp_client = clienttable->cliententries[i].client;
if (client == tmp_client) {
/* for non trusted clients username and password
would not have been set. So for non trusted clients
(i.e clients not from the same machine as the brick,
and clients from outside the storage pool)
do the root-squashing.
TODO: If any client within the storage pool (i.e
mounting within a machine from the pool but using
other machine's ip/hostname from the same pool)
is present treat it as a trusted client
*/
if (!client->auth.username && req->pid != NFS_PID)
RPC_AUTH_ROOT_SQUASH (req);
/* Problem: If we just check whether the client is
trusted client and do not do root squashing for
them, then for smb clients and UFO clients root
squashing will never happen as they use the fuse
mounts done within the trusted pool (i.e they are
trusted clients).
Solution: To fix it, do root squashing for trusted
clients also. If one wants to have a client within
the storage pool for which root-squashing does not
happen, then the client has to be mounted with
--no-root-squash option. But for defrag client and
gsyncd client do not do root-squashing.
*/
if (client->auth.username &&
req->pid != GF_CLIENT_PID_NO_ROOT_SQUASH &&
req->pid != GF_CLIENT_PID_GSYNCD &&
req->pid != GF_CLIENT_PID_DEFRAG &&
req->pid != GF_CLIENT_PID_SELF_HEALD &&
req->pid != GF_CLIENT_PID_QUOTA_MOUNT)
RPC_AUTH_ROOT_SQUASH (req);
/* For nfs clients the server processes will be running
within the trusted storage pool machines. So if we
do not do root-squashing for nfs servers, thinking
that its a trusted client, then root-squashing wont
work for nfs clients.
*/
if (req->pid == NFS_PID)
RPC_AUTH_ROOT_SQUASH (req);
}
}
/* Add a ref for this fop */
if (client)
gf_client_ref (client);
frame->root->uid = req->uid;
frame->root->gid = req->gid;
frame->root->pid = req->pid;
frame->root->client = client;
frame->root->lk_owner = req->lk_owner;
if (priv->server_manage_gids)
server_resolve_groups (frame, req);
else
server_decode_groups (frame, req);
trans = req->trans;
if (trans) {
memcpy (&frame->root->identifier, trans->peerinfo.identifier,
sizeof (trans->peerinfo.identifier));
}
frame->local = req;
state = CALL_STATE (frame);
state->client = client;
out:
return frame;
}
int
server_build_config (xlator_t *this, server_conf_t *conf)
{
data_t *data = NULL;
int ret = -1;
struct stat buf = {0,};
GF_VALIDATE_OR_GOTO ("server", this, out);
GF_VALIDATE_OR_GOTO ("server", conf, out);
ret = dict_get_int32 (this->options, "inode-lru-limit",
&conf->inode_lru_limit);
if (ret < 0) {
conf->inode_lru_limit = 16384;
}
conf->verify_volfile = 1;
data = dict_get (this->options, "verify-volfile-checksum");
if (data) {
ret = gf_string2boolean(data->data, &conf->verify_volfile);
if (ret != 0) {
gf_msg (this->name, GF_LOG_WARNING, EINVAL,
PS_MSG_INVALID_ENTRY, "wrong value for '"
"verify-volfile-checksum', Neglecting option");
}
}
data = dict_get (this->options, "trace");
if (data) {
ret = gf_string2boolean (data->data, &conf->trace);
if (ret != 0) {
gf_msg (this->name, GF_LOG_WARNING, EINVAL,
PS_MSG_INVALID_ENTRY, "'trace' takes on only "
"boolean values. Neglecting option");
}
}
/* TODO: build_rpc_config (); */
ret = dict_get_int32 (this->options, "limits.transaction-size",
&conf->rpc_conf.max_block_size);
if (ret < 0) {
gf_msg_trace (this->name, 0, "defaulting limits.transaction-"
"size to %d", DEFAULT_BLOCK_SIZE);
conf->rpc_conf.max_block_size = DEFAULT_BLOCK_SIZE;
}
data = dict_get (this->options, "config-directory");
if (data) {
/* Check whether the specified directory exists,
or directory specified is non standard */
ret = sys_stat (data->data, &buf);
if ((ret != 0) || !S_ISDIR (buf.st_mode)) {
gf_msg (this->name, GF_LOG_ERROR, 0,
PS_MSG_DIR_NOT_FOUND, "Directory '%s' doesn't "
"exist, exiting.", data->data);
ret = -1;
goto out;
}
/* Make sure that conf-dir doesn't contain ".." in path */
if ((gf_strstr (data->data, "/", "..")) == -1) {
ret = -1;
gf_msg (this->name, GF_LOG_ERROR, 0,
PS_MSG_CONF_DIR_INVALID,
"%s: invalid conf_dir", data->data);
goto out;
}
conf->conf_dir = gf_strdup (data->data);
}
ret = 0;
out:
return ret;
}
void
print_caller (char *str, int size, call_frame_t *frame)
{
server_state_t *state = NULL;
GF_VALIDATE_OR_GOTO ("server", str, out);
GF_VALIDATE_OR_GOTO ("server", frame, out);
state = CALL_STATE (frame);
snprintf (str, size,
" Callid=%"PRId64", Client=%s",
frame->root->unique,
state->xprt->peerinfo.identifier);
out:
return;
}
void
server_print_resolve (char *str, int size, server_resolve_t *resolve)
{
int filled = 0;
GF_VALIDATE_OR_GOTO ("server", str, out);
if (!resolve) {
snprintf (str, size, "<nul>");
return;
}
filled += snprintf (str + filled, size - filled,
" Resolve={");
if (resolve->fd_no != -1)
filled += snprintf (str + filled, size - filled,
"fd=%"PRId64",", (uint64_t) resolve->fd_no);
if (resolve->bname)
filled += snprintf (str + filled, size - filled,
"bname=%s,", resolve->bname);
if (resolve->path)
filled += snprintf (str + filled, size - filled,
"path=%s", resolve->path);
snprintf (str + filled, size - filled, "}");
out:
return;
}
void
server_print_loc (char *str, int size, loc_t *loc)
{
int filled = 0;
GF_VALIDATE_OR_GOTO ("server", str, out);
if (!loc) {
snprintf (str, size, "<nul>");
return;
}
filled += snprintf (str + filled, size - filled,
" Loc={");
if (loc->path)
filled += snprintf (str + filled, size - filled,
"path=%s,", loc->path);
if (loc->inode)
filled += snprintf (str + filled, size - filled,
"inode=%p,", loc->inode);
if (loc->parent)
filled += snprintf (str + filled, size - filled,
"parent=%p", loc->parent);
snprintf (str + filled, size - filled, "}");
out:
return;
}
void
server_print_params (char *str, int size, server_state_t *state)
{
int filled = 0;
GF_VALIDATE_OR_GOTO ("server", str, out);
filled += snprintf (str + filled, size - filled,
" Params={");
if (state->fd)
filled += snprintf (str + filled, size - filled,
"fd=%p,", state->fd);
if (state->valid)
filled += snprintf (str + filled, size - filled,
"valid=%d,", state->valid);
if (state->flags)
filled += snprintf (str + filled, size - filled,
"flags=%d,", state->flags);
if (state->wbflags)
filled += snprintf (str + filled, size - filled,
"wbflags=%d,", state->wbflags);
if (state->size)
filled += snprintf (str + filled, size - filled,
"size=%zu,", state->size);
if (state->offset)
filled += snprintf (str + filled, size - filled,
"offset=%"PRId64",", state->offset);
if (state->cmd)
filled += snprintf (str + filled, size - filled,
"cmd=%d,", state->cmd);
if (state->type)
filled += snprintf (str + filled, size - filled,
"type=%d,", state->type);
if (state->name)
filled += snprintf (str + filled, size - filled,
"name=%s,", state->name);
if (state->mask)
filled += snprintf (str + filled, size - filled,
"mask=%d,", state->mask);
if (state->volume)
filled += snprintf (str + filled, size - filled,
"volume=%s,", state->volume);
/* FIXME
snprintf (str + filled, size - filled,
"bound_xl=%s}", state->client->bound_xl->name);
*/
out:
return;
}
int
server_resolve_is_empty (server_resolve_t *resolve)
{
if (resolve->fd_no != -1)
return 0;
if (resolve->path != 0)
return 0;
if (resolve->bname != 0)
return 0;
return 1;
}
void
server_print_reply (call_frame_t *frame, int op_ret, int op_errno)
{
server_conf_t *conf = NULL;
server_state_t *state = NULL;
xlator_t *this = NULL;
char caller[512];
char fdstr[32];
char *op = "UNKNOWN";
GF_VALIDATE_OR_GOTO ("server", frame, out);
this = frame->this;
conf = this->private;
GF_VALIDATE_OR_GOTO ("server", conf, out);
GF_VALIDATE_OR_GOTO ("server", conf->trace, out);
state = CALL_STATE (frame);
print_caller (caller, 256, frame);
switch (frame->root->type) {
case GF_OP_TYPE_FOP:
op = (char *)gf_fop_list[frame->root->op];
break;
default:
op = "";
}
fdstr[0] = '\0';
if (state->fd)
snprintf (fdstr, 32, " fd=%p", state->fd);
gf_msg (this->name, GF_LOG_INFO, op_errno, PS_MSG_SERVER_MSG,
"%s%s => (%d, %d)%s", op, caller, op_ret, op_errno, fdstr);
out:
return;
}
void
server_print_request (call_frame_t *frame)
{
server_conf_t *conf = NULL;
xlator_t *this = NULL;
server_state_t *state = NULL;
char *op = "UNKNOWN";
char resolve_vars[256];
char resolve2_vars[256];
char loc_vars[256];
char loc2_vars[256];
char other_vars[512];
char caller[512];
GF_VALIDATE_OR_GOTO ("server", frame, out);
this = frame->this;
conf = this->private;
GF_VALIDATE_OR_GOTO ("server", conf, out);
if (!conf->trace)
goto out;
state = CALL_STATE (frame);
memset (resolve_vars, '\0', 256);
memset (resolve2_vars, '\0', 256);
memset (loc_vars, '\0', 256);
memset (loc2_vars, '\0', 256);
memset (other_vars, '\0', 256);
print_caller (caller, 256, frame);
if (!server_resolve_is_empty (&state->resolve)) {
server_print_resolve (resolve_vars, 256, &state->resolve);
server_print_loc (loc_vars, 256, &state->loc);
}
if (!server_resolve_is_empty (&state->resolve2)) {
server_print_resolve (resolve2_vars, 256, &state->resolve2);
server_print_loc (loc2_vars, 256, &state->loc2);
}
server_print_params (other_vars, 512, state);
switch (frame->root->type) {
case GF_OP_TYPE_FOP:
op = (char *)gf_fop_list[frame->root->op];
break;
default:
op = "";
break;
}
gf_msg (this->name, GF_LOG_INFO, 0, PS_MSG_SERVER_MSG,
"%s%s%s%s%s%s%s", op, caller,
resolve_vars, loc_vars, resolve2_vars, loc2_vars, other_vars);
out:
return;
}
int
serialize_rsp_direntp (gf_dirent_t *entries, gfs3_readdirp_rsp *rsp)
{
gf_dirent_t *entry = NULL;
gfs3_dirplist *trav = NULL;
gfs3_dirplist *prev = NULL;
int ret = -1;
GF_VALIDATE_OR_GOTO ("server", entries, out);
GF_VALIDATE_OR_GOTO ("server", rsp, out);
list_for_each_entry (entry, &entries->list, list) {
trav = GF_CALLOC (1, sizeof (*trav), gf_server_mt_dirent_rsp_t);
if (!trav)
goto out;
trav->d_ino = entry->d_ino;
trav->d_off = entry->d_off;
trav->d_len = entry->d_len;
trav->d_type = entry->d_type;
trav->name = entry->d_name;
gf_stat_from_iatt (&trav->stat, &entry->d_stat);
/* if 'dict' is present, pack it */
if (entry->dict) {
trav->dict.dict_len = dict_serialized_length (entry->dict);
if (trav->dict.dict_len > UINT_MAX) {
gf_msg (THIS->name, GF_LOG_ERROR, EINVAL,
PS_MSG_INVALID_ENTRY, "failed to get "
"serialized length of reply dict");
errno = EINVAL;
trav->dict.dict_len = 0;
goto out;
}
trav->dict.dict_val = GF_CALLOC (1, trav->dict.dict_len,
gf_server_mt_rsp_buf_t);
if (!trav->dict.dict_val) {
errno = ENOMEM;
trav->dict.dict_len = 0;
goto out;
}
ret = dict_serialize (entry->dict, trav->dict.dict_val);
if (ret < 0) {
gf_msg (THIS->name, GF_LOG_ERROR, 0,
PS_MSG_DICT_SERIALIZE_FAIL,
"failed to serialize reply dict");
errno = -ret;
trav->dict.dict_len = 0;
goto out;
}
}
if (prev)
prev->nextentry = trav;
|