📄 client-protocol.c
字号:
vec.iov_base = ""; vec.iov_len = 0; gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD"); TRAP_ON (ctx_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EBADFD, &vec, &dummy); return 0; } fd_str = strdup (data_to_str (ctx_data)); request = get_new_dict (); dict_set (request, "FD", str_to_data (fd_str)); dict_set (request, "OFFSET", data_from_int64 (offset)); dict_set (request, "LEN", data_from_int64 (size)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_READ, request); dict_destroy (request); freee (fd_str); return ret;}/** * client_writev - writev function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * @vector: * @count: * @offset: * * external reference through client_protocol_xlator->fops->writev */int32_t client_writev (call_frame_t *frame, xlator_t *this, fd_t *fd, struct iovec *vector, int32_t count, off_t offset){ int32_t ret = -1; size_t size = 0, i; char *fd_str = NULL; dict_t *request = NULL; data_t *ctx_data = NULL; if (fd && fd->ctx) ctx_data = dict_get (fd->ctx, this->name); if (!ctx_data) { struct stat dummy = {0, }; gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD"); TRAP_ON (ctx_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EBADFD, &dummy); return 0; } for (i = 0; i<count; i++) size += vector[i].iov_len; fd_str = strdup (data_to_str (ctx_data)); request = get_new_dict (); dict_set (request, "FD", str_to_data (fd_str)); dict_set (request, "OFFSET", data_from_int64 (offset)); dict_set (request, "BUF", data_from_iovec (vector, count)); dict_set (request, "LEN", data_from_int64 (size)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_WRITE, request); dict_destroy (request); freee (fd_str); return ret;}/** * client_statfs - statfs function for client protocol * @frame: call frame * @this: this translator structure * @loc: location * * external reference through client_protocol_xlator->fops->statfs */int32_t client_statfs (call_frame_t *frame, xlator_t *this, loc_t *loc){ ino_t ino = 1; /* default it to root's inode number */ int32_t ret = -1; dict_t *request = NULL; request = get_new_dict (); dict_set (request, "PATH", str_to_data ((char *)loc->path)); dict_set (request, "INODE", data_from_uint64 (ino)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_STATFS, request); dict_destroy (request); return ret;}/** * client_flush - flush function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * * external reference through client_protocol_xlator->fops->flush */int32_t client_flush (call_frame_t *frame, xlator_t *this, fd_t *fd){ int32_t ret = -1; char *fd_str = NULL; dict_t *request = NULL; data_t *ctx_data = NULL; if (fd && fd->ctx) ctx_data = dict_get (fd->ctx, this->name); if (!ctx_data) { dict_destroy (request); gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD"); TRAP_ON (ctx_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EBADFD); return 0; } fd_str = strdup (data_to_str (ctx_data)); request = get_new_dict (); dict_set (request, "FD", str_to_data (fd_str)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_FLUSH, request); dict_destroy (request); freee (fd_str); return ret;}/** * client_close - close function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * * external reference through client_protocol_xlator->fops->close * * TODO: fd_t is top-down now... no need to do anything destructive. Also need to look into * cleanup(). */int32_t client_close (call_frame_t *frame, xlator_t *this, fd_t *fd){ int32_t ret = -1; char *key = NULL; char *fd_str = NULL; data_t *ctx_data = NULL; transport_t *trans = NULL; client_proto_priv_t *priv = NULL; trans = frame->this->private; if (fd && fd->ctx) ctx_data = dict_get (fd->ctx, this->name); if (ctx_data) { dict_t *request = get_new_dict (); fd_str = strdup (data_to_str (ctx_data)); dict_set (request, "FD", data_from_dynstr (fd_str)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_CLOSE, request); dict_destroy (request); } else { gf_log (this->name, GF_LOG_WARNING, "no valid fd found, returning"); STACK_UNWIND (frame, 0, 0); } priv = trans->xl_private; asprintf (&key, "%p", fd); pthread_mutex_lock (&priv->lock); { dict_del (priv->saved_fds, key); } pthread_mutex_unlock (&priv->lock); freee (key); return ret;}/** * client_fsync - fsync function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * @flags: * * external reference through client_protocol_xlator->fops->fsync */int32_t client_fsync (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags){ int32_t ret = -1; char *fd_str = NULL; dict_t *request = NULL; data_t *ctx_data = NULL; if (fd && fd->ctx) ctx_data = dict_get (fd->ctx, this->name); if (!ctx_data) { gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD"); TRAP_ON (ctx_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EBADFD); return 0; } request = get_new_dict (); fd_str = strdup (data_to_str (ctx_data)); dict_set (request, "FD", str_to_data (fd_str)); dict_set (request, "FLAGS", data_from_int64 (flags)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_FSYNC, request); dict_destroy (request); freee (fd_str); return ret;}int32_tclient_incver (call_frame_t *frame, xlator_t *this, const char *path){ int32_t ret = -1; dict_t *request = get_new_dict(); dict_set (request, "PATH", str_to_data ((char *) path)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_INCVER, request); dict_destroy (request); return ret;}/** * client_setxattr - setxattr function for client protocol * @frame: call frame * @this: this translator structure * @loc: location * @dict: dictionary which contains key:value to be set. * @flags: * * external reference through client_protocol_xlator->fops->setxattr */int32_t client_setxattr (call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *dict, int32_t flags){ ino_t ino = 0; int32_t ret = -1; dict_t *request = NULL; data_t *ino_data = NULL; if (loc->inode && loc->inode->ctx) ino_data = dict_get (loc->inode->ctx, this->name); if (ino_data) { ino = data_to_uint64 (ino_data); } else { if (!strncmp (loc->path, "/", 2)) { ino = 1; } else { gf_log (this->name, GF_LOG_ERROR, "%s: returning EINVAL", loc->path); TRAP_ON (ino_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EINVAL); return 0; } } request = get_new_dict (); dict_set (request, "PATH", str_to_data ((char *)loc->path)); dict_set (request, "INODE", data_from_uint64 (ino)); dict_set (request, "FLAGS", data_from_int64 (flags)); { /* Serialize the dictionary and set it as a parameter in 'request' dict */ int32_t len = dict_serialized_length (dict); char *dict_buf = alloca (len); dict_serialize (dict, dict_buf); dict_set (request, "DICT", bin_to_data (dict_buf, len)); } ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_SETXATTR, request); dict_destroy (request); return ret;}/** * client_getxattr - getxattr function for client protocol * @frame: call frame * @this: this translator structure * @loc: location structure * * external reference through client_protocol_xlator->fops->getxattr */int32_t client_getxattr (call_frame_t *frame, xlator_t *this, loc_t *loc){ ino_t ino = 0; int32_t ret = -1; dict_t *request = NULL; data_t *ino_data = NULL; if (loc && loc->inode && loc->inode->ctx) ino_data = dict_get (loc->inode->ctx, this->name); if (ino_data) { ino = data_to_uint64 (ino_data); } else { gf_log (this->name, GF_LOG_ERROR, "%s: returning EINVAL", loc->path); TRAP_ON (ino_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EINVAL, NULL); return 0; } request = get_new_dict (); dict_set (request, "PATH", str_to_data ((char *)loc->path)); dict_set (request, "INODE", data_from_uint64 (ino)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_GETXATTR, request); dict_destroy (request); return ret;}/** * client_removexattr - removexattr function for client protocol * @frame: call frame * @this: this translator structure * @loc: location structure * @name: * * external reference through client_protocol_xlator->fops->removexattr */ int32_t client_removexattr (call_frame_t *frame, xlator_t *this, loc_t *loc, const char *name){ ino_t ino = 0; int32_t ret = -1; dict_t *request = NULL; data_t *ino_data = NULL; if (loc && loc->inode && loc->inode->ctx) ino_data = dict_get (loc->inode->ctx, this->name); if (ino_data) { ino = data_to_uint64 (ino_data); } else { gf_log (this->name, GF_LOG_ERROR, "%s: returning EINVAL", loc->path); TRAP_ON (ino_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EINVAL); return 0; } request = get_new_dict (); dict_set (request, "PATH", str_to_data ((char *)loc->path)); dict_set (request, "INODE", data_from_uint64 (ino)); dict_set (request, "NAME", str_to_data ((char *)name)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_REMOVEXATTR, request); dict_destroy (request); return ret;}/** * client_opendir - opendir function for client protocol * @frame: call frame * @this: this translator structure * @loc: location structure * * external reference through client_protocol_xlator->fops->opendir */int32_t client_opendir (call_frame_t *frame, xlator_t *this, loc_t *loc, fd_t *fd){ int32_t ret = -1; ino_t ino = 0; data_t *ino_data = NULL; dict_t *request = NULL; client_local_t *local = NULL; if (loc && loc->inode && loc->inode->ctx) ino_data = dict_get (loc->inode->ctx, this->name); if (ino_data) { ino = data_to_uint64 (ino_data); } else { gf_log (this->name, GF_LOG_ERROR, "%s: returning EINVAL", loc->path); TRAP_ON (ino_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EINVAL, fd); return 0; } local = calloc (1, sizeof (client_local_t)); local->inode = loc->inode; local->fd = fd; frame->local = local; request = get_new_dict (); dict_set (request, "PATH", str_to_data ((char *)loc->path)); dict_set (request, "INODE", data_from_uint64 (ino)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_OPENDIR, request); dict_destroy (request); return ret;}/** * client_readdir - readdir function for client protocol * @frame: call frame * @this: this translator structure * * external reference through client_protocol_xlator->fops->readdir */int32_t client_getdents (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, off_t offset, int32_t flag){ int32_t ret = -1; char *fd_str = NULL; dict_t *request = NULL; data_t *fd_data = NULL; if (fd && fd->ctx) fd_data = dict_get (fd->ctx, this->name); if (!fd_data) { gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD"); TRAP_ON (fd_data == NULL); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, EBADFD, NULL, 0); return 0; } fd_str = strdup (data_to_str (fd_data)); request = get_new_dict (); dict_set (request, "FD", str_to_data (fd_str)); dict_set (request, "OFFSET", data_from_uint64 (offset)); dict_set (request, "SIZE", data_from_uint64 (size)); dict_set (request, "FLAG", data_from_uint32 (flag)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_GETDENTS, request); freee (fd_str); dict_destroy (request); return ret;}/** * client_readdir - readdir function for client protocol * @frame: call frame * @this: this translator structure * * external reference through client_protocol_xlator->fops->readdir */int32_t client_readdir (call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size, off_t offset){ int32_t ret = -1; char *fd_str = NULL; data_t *fd_data = NULL; dict_t *request = NULL; if (fd && fd->ctx) fd_data = dict_get (fd->ctx, this->name); if (!fd_data) { gf_log (this->name, GF_LOG_ERROR, ": returning EBADFD");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -