📄 client-protocol.c
字号:
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)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_READDIR, request); freee (fd_str); dict_destroy (request); return ret;}/** * client_closedir - closedir function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * * external reference through client_protocol_xlator->fops->closedir */int32_t client_closedir (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_CLOSEDIR, request); dict_destroy (request); } else { gf_log (this->name, GF_LOG_WARNING, "no proper 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); free (key); return ret;}/** * client_fsyncdir - fsyncdir function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * @flags: * * external reference through client_protocol_xlator->fops->fsyncdir */int32_t client_fsyncdir (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags){ int32_t ret = -1; 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 -1; } gf_log (this->name, GF_LOG_ERROR, "Function not implemented"); frame->root->rsp_refs = NULL; STACK_UNWIND (frame, -1, ENOSYS); return ret;}/** * client_access - access function for client protocol * @frame: call frame * @this: this translator structure * @loc: location structure * @mode: * * external reference through client_protocol_xlator->fops->access */int32_t client_access (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t mask){ 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, 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)); dict_set (request, "MASK", data_from_int64 (mask)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_ACCESS, request); dict_destroy (request); return ret;}/** * client_ftrucate - ftruncate function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * @offset: offset to truncate to * * external reference through client_protocol_xlator->fops->ftruncate */int32_t client_ftruncate (call_frame_t *frame, xlator_t *this, fd_t *fd, off_t offset){ 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, NULL); 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)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_FTRUNCATE, request); freee (fd_str); dict_destroy (request); return ret;}/** * client_fstat - fstat function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * * external reference through client_protocol_xlator->fops->fstat */int32_t client_fstat (call_frame_t *frame, xlator_t *this, fd_t *fd){ 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); return 0; } fd_str = strdup (data_to_str (fd_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_FSTAT, request); freee (fd_str); dict_destroy (request); return ret;}/** * client_lk - lk function for client protocol * @frame: call frame * @this: this translator structure * @fd: file descriptor structure * @cmd: lock command * @lock: * * external reference through client_protocol_xlator->fops->lk */int32_t client_lk (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t cmd, struct flock *lock){ int32_t ret = -1; char *fd_str = NULL; dict_t *request = NULL; data_t *ctx_data = NULL; int32_t gf_cmd = 0; int32_t gf_type = 0; 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, NULL); return 0; } if (cmd == F_GETLK || cmd == F_GETLK64) gf_cmd = GF_LK_GETLK; else if (cmd == F_SETLK || cmd == F_SETLK64) gf_cmd = GF_LK_SETLK; else if (cmd == F_SETLKW || cmd == F_SETLKW64) gf_cmd = GF_LK_SETLKW; else gf_log (this->name, GF_LOG_ERROR, "Unknown cmd (%d)!", gf_cmd); switch (lock->l_type) { case F_RDLCK: gf_type = GF_LK_F_RDLCK; break; case F_WRLCK: gf_type = GF_LK_F_WRLCK; break; case F_UNLCK: gf_type = GF_LK_F_UNLCK; break; } fd_str = strdup (data_to_str (ctx_data)); request = get_new_dict (); dict_set (request, "FD", str_to_data (fd_str)); dict_set (request, "CMD", data_from_int32 (gf_cmd)); dict_set (request, "TYPE", data_from_int16 (gf_type)); dict_set (request, "WHENCE", data_from_int16 (lock->l_whence)); dict_set (request, "START", data_from_int64 (lock->l_start)); dict_set (request, "LEN", data_from_int64 (lock->l_len)); dict_set (request, "PID", data_from_uint64 (lock->l_pid)); dict_set (request, "CLIENT_PID", data_from_uint64 (getpid ())); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_LK, request); freee (fd_str); dict_destroy (request); return ret;}/** * client_writedir - */int32_tclient_setdents (call_frame_t *frame, xlator_t *this, fd_t *fd, int32_t flags, dir_entry_t *entries, int32_t count){ int32_t ret = -1; char *buffer = NULL; 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); 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, "FLAGS", data_from_int32 (flags)); dict_set (request, "NR_ENTRIES", data_from_int32 (count)); { dir_entry_t *trav = entries->next; uint32_t len = 0; char *ptr = NULL; while (trav) { len += strlen (trav->name); len += 1; len += strlen (trav->link); len += 1; len += 256; // max possible for statbuf; trav = trav->next; } buffer = calloc (1, len); ptr = buffer; trav = entries->next; while (trav) { int32_t this_len = 0; char *tmp_buf = NULL; struct stat *stbuf = &trav->buf; { /* Convert the stat buf to string */ uint64_t dev = stbuf->st_dev; uint64_t ino = stbuf->st_ino; uint32_t mode = stbuf->st_mode; uint32_t nlink = stbuf->st_nlink; uint32_t uid = stbuf->st_uid; uint32_t gid = stbuf->st_gid; uint64_t rdev = stbuf->st_rdev; uint64_t size = stbuf->st_size; uint32_t blksize = stbuf->st_blksize; uint64_t blocks = stbuf->st_blocks; uint32_t atime = stbuf->st_atime; uint32_t mtime = stbuf->st_mtime; uint32_t ctime = stbuf->st_ctime;#ifdef HAVE_TV_NSEC uint32_t atime_nsec = stbuf->st_atim.tv_nsec; uint32_t mtime_nsec = stbuf->st_mtim.tv_nsec; uint32_t ctime_nsec = stbuf->st_ctim.tv_nsec;#else uint32_t atime_nsec = 0; uint32_t mtime_nsec = 0; uint32_t ctime_nsec = 0;#endif asprintf (&tmp_buf, GF_STAT_PRINT_FMT_STR, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks, atime, atime_nsec, mtime, mtime_nsec, ctime, ctime_nsec); } this_len = sprintf (ptr, "%s/%s%s\n", trav->name, tmp_buf, trav->link); free (tmp_buf); trav = trav->next; ptr += this_len; } dict_set (request, "DENTRIES", data_from_dynstr (buffer)); } ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_SETDENTS, request); freee (fd_str); dict_destroy (request); return ret;}/* * client_lookup - lookup function for client protocol * @frame: call frame * @this: * @loc: location * * not for external reference */int32_t client_lookup (call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t need_xattr){ ino_t ino = 0; int32_t ret = -1; dict_t *request = NULL; data_t *ino_data = 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) { /* revalidate */ ino = data_to_uint64 (ino_data); } local = calloc (1, sizeof (client_local_t)); local->inode = loc->inode; 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)); dict_set (request, "NEED_XATTR", data_from_int32 (need_xattr)); ret = client_protocol_xfer (frame, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_LOOKUP, request); dict_destroy (request); return ret;}/* * client_forget - forget function for client protocol * @frame: call frame * @this: * @inode: * * not for external reference */int32_tclient_forget (call_frame_t *frame, xlator_t *this, inode_t *inode){ ino_t ino = 0; int32_t ret = 0; data_t *ino_data = NULL; call_frame_t *fr = NULL; if (inode && inode->ctx) ino_data = dict_get (inode->ctx, this->name); if (ino_data) { dict_t *request = get_new_dict (); ino = data_to_uint64 (ino_data); fr = create_frame (this, this->ctx->pool); dict_set (request, "INODE", data_from_uint64 (ino)); ret = client_protocol_xfer (fr, this, GF_OP_TYPE_FOP_REQUEST, GF_FOP_FORGET, request); dict_destroy (request); } return ret;}/* * client_fchmod * */int32_tclient_fchmod (call_frame_t *frame, xlator_t *this, fd_t *fd, mode_t mode){ 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); return 0; } fd_str = strdup (data_to_str (fd_data));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -