/* TODO: Implement the function of breakpoint */ static BP* alloc_bp(){ test(free_head() != NULL, "free_: %p", free_head()); BP *ne; ne = free_head(); set_free_head(ne->next); add(&head, ne); return ne; }
void clear(void) { head_t* list = chain(); while (list) { head_t* curr = list; list = list->next_; free_head(curr); } alloc_.clear(); init(); }
int get_reply(int skt) { struct sbpfs_head head; char* rec_buf; u64_t rec_len; if (sbp_recv(skt, &rec_buf, &rec_len) == -1) { printf("failed to receive data\n"); return -1; } if (strlen(rec_buf) == 0) { return -1; } if (decode_head(rec_buf, rec_len, &head) == -1) { printf("decode head failed\n"); return -1; }/*head need free*/ if (strcmp(head.title, REQUEST_OK) == 0) { free_head(&head); return 0; } else { free_head(&head); return -1; } }
s32_t sbp_closedir(s32_t dirfd){ if(dirfd < 0)return -1; if (fds[dirfd] == NULL) return -1; if (fds[dirfd]->type != T_DIR) return -1; char tran_fd[32]; SBP_PREPARE_REQUEST sprintf(tran_fd, "%lld", fds[dirfd]->server_fd); mkent(head,METHOD,"CLOSEDIR"); mkent(head,ARGC,"1"); mkent(head,"Arg0",tran_fd); //mkent(head,"Arg1",fds[fd]->auth_code); mkent(head,CONTENT_LEN,"0"); SBP_SEND_AND_PROCESS_REPLY SBP_PROCESS_RESULT err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); free_sbpfd(fds[dirfd]); fds[dirfd] = NULL; return -1; ok_exit: free(data); free(rec_data); free_head(&head); free(usr); free(pass); free_sbpfd(fds[dirfd]); fds[dirfd] = NULL; return 0; }
int StackAllocator::restore_top(const int64_t top, const bool slow) { int err = OB_SUCCESS; int64_t last_top = top_; int64_t BATCH_FREE_LIMIT = 1<<30; if (top < 0 || top > top_) { err = OB_INVALID_ARGUMENT; } while(OB_SUCCESS == err && top_ > top) { if (NULL == head_) { err = OB_ERR_UNEXPECTED; TBSYS_LOG(ERROR, "restor_top(top=%ld): head_ == NULL", top); } else if (top + head_->pos_ <= top_) { err = free_head(); } else if (head_->pos_ - (int64_t)sizeof(*head_) < top_ - top) { err = OB_ERR_UNEXPECTED; TBSYS_LOG(ERROR, "restore_top(head->pos_=%ld, top=%ld, top_=%ld)=>%d", head_->pos_, top, top_, err); } else { head_->pos_ -= top_ - top; top_ = top; } if (slow && last_top > top_ + BATCH_FREE_LIMIT) { last_top = top_; usleep(10000); } } return err; }
/* * evict_node - Evict node when necessary * * Evict node with LRU-policy */ int evict_node(Cache cache, int res_size) { // 如果 cache 的总空间不够 就不驱逐 if (MAX_CACHE_SIZE < res_size) { return 0; } // 如果 cache 中还有足够的空间 就不驱逐 else if (cache->left_size > res_size) { return 0; } // 如果 cache 中的空间不够了 进行驱逐 else { // 驱逐最后一个节点 直到有足够的空间 while (cache->left_size < res_size) { free_head(cache); } } return cache->left_size; }
void* serve(void* arg) { struct list_entry* ent = (struct list_entry*) arg; struct sbpfs_head head; char* rec_buf; u64_t rec_len; //printf("start receive\n"); if (sbp_recv(ent->skt, &rec_buf, &rec_len) == -1) { printf("failed to receive data\n"); return NULL; } if (strlen(rec_buf) == 0) { goto err_exit; } if (decode_head(rec_buf, rec_len, &head) == -1) { printf("decode head failed\n"); goto err_exit; }/*head need free*/ char* method; if ((method = get_head_entry_value(&head, METHOD)) == NULL) { printf("get head entry failed\n"); goto err_exit2; } if (strcmp(method, "READ") == 0) { ent->req = 1; } else if (strcmp(method, "WRITE") == 0) { ent->req = 2; } else if (strcmp(method, "Copy") == 0) { ent->req = 3; } else { printf("invalid request\n"); goto err_exit2; } char* block_num_s; char* offset_s; char* length_s; char* content_len_s; if ((block_num_s = get_head_entry_value(&head, "Arg0")) == NULL) { printf("get head entry Arg0 failed\n"); goto err_exit2; } if ((offset_s = get_head_entry_value(&head, "Arg1")) == NULL) { printf("get head entry Arg1 failed\n"); goto err_exit2; } if ((length_s = get_head_entry_value(&head, "Arg2")) == NULL) { printf("get head entry Arg2 failed\n"); goto err_exit2; } if ((content_len_s = get_head_entry_value(&head, "Content-Length")) == NULL) { printf("get head entry Content-Length failed\n"); goto err_exit2; } ent->block_num = atoll(block_num_s); ent->offset = atoi(offset_s); ent->length = atoi(length_s); if (ent->req == 2 || ent->req == 3) { ent->data_length = atoll(content_len_s); ent->data = rec_buf + head.head_len; } //send_err(ent->skt, "a", "b"); process_req(ent); err_exit2: free_head(&head); err_exit: free(rec_buf); close(ent->skt); free(ent); return NULL; }
static s64_t sbp_read_or_write(s64_t fd, void* buf, u64_t len, char* method) { if (fds[fd] == NULL) return -1; char tran_fd[32]; char tran_offset[32]; char tran_len[32]; s64_t ret = -1; SBP_PREPARE_REQUEST sprintf(tran_fd, "%lld", fds[fd]->server_fd); sprintf(tran_offset, "%lld", fds[fd]->offset); sprintf(tran_len, "%lld", len); mkent(head,METHOD,method); mkent(head,ARGC,"3"); mkent(head,"Arg0",tran_fd); mkent(head,"Arg1",tran_offset); mkent(head,"Arg2",tran_len); mkent(head,CONTENT_LEN,"0"); SBP_SEND_AND_PROCESS_REPLY if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { u64_t content_l = -1; char* value = get_head_entry_value(&head, CONTENT_LEN); if (value == NULL) { seterr(HEAD_ERR,NO_CONTENT_LEN); goto err_exit3; } content_l = atoll(value); if (content_l % sizeof(struct block_entry)) { printf("content l :%lld :%ld\n", content_l, sizeof(struct block_entry)); seterr(DATA_ERR,DATA_LEN); goto err_exit3; } u64_t block_ent_num = content_l / sizeof(struct block_entry); struct block_entry* ents = (struct block_entry*) (rec_data + head.head_len); if (strcmp(method, "WRITE") == 0) { ret = write_blocks(ents, block_ent_num, buf, fds[fd]->auth_code); } else if (strcmp(method, "READ") == 0) { ret = read_blocks(ents, block_ent_num, buf, fds[fd]->auth_code); } if (ret >= 0){ fds[fd]->offset += ret; //printf("fds offset :%lld\n",fds[fd]->offset); } goto ok_exit; } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR, UNKNOWN_HEAD); } err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return ret; ok_exit: free(data); free(rec_data); free_head(&head); free(usr); free(pass); return ret; }
static s64_t write_block(struct block_entry* ents, int offset_in_ent_dnode, char* buf, char* auth_code) { s64_t ret = 0; struct sbpfs_head head; char* usr; char* pass; char* data; char* rec_data; char tran_usr[TRAN_USERNAME_LEN]; char tran_blocknum[32]; char tran_offset[32]; char tran_len[32]; if (sbp_getUandP(&usr, &pass) == -1) { printf("Can not get username and password\n"); return -1; } sprintf(tran_usr, "Client_%s", usr); s64_t sockfd; u64_t rec_len = 0; int data_len = 0; head.data = NULL; head.title = PROTOCOL; head.entry_num = 0; mkent(head,USER,tran_usr); mkent(head,PASS,pass); mkent(head,AUTH_CODE,auth_code); mkent(head,METHOD,"WRITE"); mkent(head,ARGC,"3"); bzero(tran_blocknum, 32); bzero(tran_offset, 32); bzero(tran_len, 32); sprintf(tran_blocknum, "%lld", ents->block_id); sprintf(tran_offset, "%d", ents->offset_in_block); sprintf(tran_len, "%d", ents->length_in_block); mkent(head,"Arg0",tran_blocknum); mkent(head,"Arg1",tran_offset); mkent(head,"Arg2",tran_len); mkent(head,CONTENT_LEN,tran_len); if (make_head(&data, &data_len, &head) == -1) { seterr(HEAD_ERR,MAKE_HEAD); goto err_exit; }/*data need free*/ if ((sockfd = sbp_connect(get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode]), DNODE_SERVICE_PORT)) < 0) { printf("connect ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, EST_SOCK); goto err_exit1; } if (sbp_send(sockfd, data, data_len) < 0) { printf("send head to ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, SEND); goto err_exit1; } if (sbp_send(sockfd, buf, ents->length_in_block) < 0) { printf("send data to ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, SEND); goto err_exit1; } if ((sbp_recv(sockfd, &rec_data, &rec_len)) < 0) { printf("receive from ip: %s err\n", get_dnode_hostname( ents->d_nodes[offset_in_ent_dnode])); seterr(SOCKET_ERR, RECV); goto err_exit1; } if (strlen(rec_data) == 0) { seterr(SOCKET_ERR,RECV); goto err_exit2; } if (decode_head(rec_data, rec_len, &head) == -1) { seterr("Data Error", "Can not decode SBPFS_HEAD"); goto err_exit2; }/*head need free*/ if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { ret = ents->length_in_block; free(data); free(rec_data); free_head(&head); // printf("succeeded write to %s block: %lld length:%d \n", // get_dnode_hostname(ents->d_nodes[offset_in_ent_dnode]), // ents->block_id, ents->length_in_block); } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR, UNKNOWN_HEAD); } goto ok_exit; err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return -1; ok_exit: free(usr); free(pass); return ret; }
static s64_t read_block(struct block_entry* ents, int offset_in_ent_dnode, char* buf, char* auth_code) { s64_t ret = 0; struct sbpfs_head head; char* usr; char* pass; char* data; char* rec_data; char tran_usr[TRAN_USERNAME_LEN]; char tran_blocknum[32]; char tran_offset[32]; char tran_len[32]; if (sbp_getUandP(&usr, &pass) == -1) { printf("Can not get username and password\n"); return -1; } sprintf(tran_usr, "Client_%s", usr); u64_t rec_len = 0; int data_len = 0; head.data = NULL; head.title = PROTOCOL; head.entry_num = 0; mkent(head,USER,tran_usr); mkent(head,PASS,pass); mkent(head,AUTH_CODE,auth_code); mkent(head,METHOD,"READ"); mkent(head,ARGC,"3"); bzero(tran_blocknum, 32); bzero(tran_offset, 32); bzero(tran_len, 32); sprintf(tran_blocknum, "%lld", ents->block_id); sprintf(tran_offset, "%d", ents->offset_in_block); sprintf(tran_len, "%d", ents->length_in_block); mkent(head,"Arg0",tran_blocknum); mkent(head,"Arg1",tran_offset); mkent(head,"Arg2",tran_len); mkent(head,CONTENT_LEN,"0"); if (make_head(&data, &data_len, &head) == -1) { seterr(HEAD_ERR,MAKE_HEAD); goto err_exit; }/*data need free*/ if (sendrec_hostname( get_dnode_hostname(ents->d_nodes[offset_in_ent_dnode]), DNODE_SERVICE_PORT, data, data_len, &rec_data, &rec_len) != 0) { goto err_exit1; }/*rec_data need free*/ if (strlen(rec_data) == 0) { seterr(SOCKET_ERR,RECV); goto err_exit2; } if (decode_head(rec_data, rec_len, &head) == -1) { seterr("Data Error", "Can not decode SBPFS_HEAD"); goto err_exit2; }/*head need free*/ if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { u64_t content_l = -1; char* value = get_head_entry_value(&head, CONTENT_LEN); if (value == NULL) { seterr(HEAD_ERR,NO_CONTENT_LEN); goto err_exit3; } content_l = atoll(value); if (content_l != ents->length_in_block) { seterr(DATA_ERR,DATA_LEN); goto err_exit3; } memcpy(buf, rec_data + head.head_len, content_l); ret = content_l; } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR, UNKNOWN_HEAD); } goto ok_exit; err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return -1; ok_exit: free(usr); free(pass); free(data); free(rec_data); free_head(&head); return ret; }
s32_t sbp_opendir(char* filename) { s32_t fd = get_slot(); SBP_PREPARE_REQUEST mkent(head,METHOD,"OPENDIR"); mkent(head,ARGC,"1"); mkent(head,"Arg0",filename); mkent(head,CONTENT_LEN,"0"); SBP_SEND_AND_PROCESS_REPLY if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { if ((fds[fd] = (struct sbp_filedesc *) malloc( sizeof(struct sbp_filedesc))) == NULL) { seterr(MEM_ERR,MALLOC); goto err_exit3; }// need sbp_close(fd) bzero(fds[fd], sizeof(struct sbp_filedesc)); if ((fds[fd]->filename = (char *) malloc(sizeof(strlen(filename) + 1))) == NULL) { seterr(MEM_ERR,MALLOC); goto err_exit4; } memcpy(fds[fd]->filename, filename, strlen(filename)); fds[fd]->filename[strlen(filename)] = 0; fds[fd]->offset = 0; fds[fd]->type = T_DIR; char* fd_s = get_head_entry_value(&head, DIR_FD); if(fd_s == NULL) { seterr(DATA_ERR,"Could not get FD"); goto err_exit4; } fds[fd]->server_fd = atoll(fd_s); // char* auth_code = get_head_entry_value(&head, AUTH_CODE); // if (auth_code == NULL) { // seterr(DATA_ERR,AUTH_LEN); // goto err_exit4; // } char* dir_ent_num = get_head_entry_value(&head, DIR_ENT_NUM); if (dir_ent_num == NULL) { seterr(DATA_ERR,DIR_ENT_LEN); goto err_exit4; } fds[fd]->length = atoll(dir_ent_num); // int auth_len = strlen(auth_code); // if (auth_len > AUTH_CODE_LEN) { // seterr(DATA_ERR,AUTH_LEN); // goto err_exit4; // } // memcpy(fds[fd]->auth_code, auth_code, auth_len); goto ok_exit; } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR, UNKNOWN_HEAD); } err_exit4: sbp_closedir(fd); err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return -1; ok_exit: free(data); free(rec_data); free_head(&head); free(usr); free(pass); return fd; }
struct sbp_dirent* sbp_readdir(s32_t dirfd) { if(dirfd<0)return NULL; if (fds[dirfd] == NULL) { return NULL; } struct sbpfs_head head; char* usr; char* pass; char* data; char* rec_data; char tran_usr[16 + 7]; u64_t rec_len = 0; int data_len = 0; head.data = ((void *)0); head.title = "SBPFS/1.0"; head.entry_num = 0; if (sbp_getUandP(&usr, &pass) == -1) { printf("Can not get username and password\n"); return NULL; } sprintf(tran_usr, "Client_%s", usr); head.entrys[head.entry_num].name = "User"; head.entrys[head.entry_num++].value = tran_usr;; head.entrys[head.entry_num].name = "Password"; head.entrys[head.entry_num++].value = pass;; char tran_fd[32]; char tran_offset[32]; sprintf(tran_fd,"%lld",fds[dirfd]->server_fd); sprintf(tran_offset,"%lld",fds[dirfd]->offset); mkent(head,METHOD,"READDIR"); mkent(head,ARGC,"2"); mkent(head,"Arg0",tran_fd); mkent(head,"Arg1",tran_offset); mkent(head,CONTENT_LEN,"0"); SBP_SEND_AND_PROCESS_REPLY if (strncmp(head.title, REQUEST_OK, strlen(REQUEST_OK)) == 0) { u64_t content_l = -1; char* value = get_head_entry_value(&head,CONTENT_LEN); if(value == NULL) { seterr(HEAD_ERR,NO_CONTENT_LEN); goto err_exit3; } content_l = atoll(value); if(content_l != sizeof(struct sbp_dirent_for_trans)) { goto err_exit3; } memcpy(&dir_ent,rec_data + head.head_len,content_l); dir_ent.offset = fds[dirfd]->offset++; goto ok_exit; } else if (strncmp(head.title, REQUEST_ERR, strlen(REQUEST_ERR)) == 0) { sbp_update_err(&head); goto err_exit3; } else { seterr(HEAD_ERR,UNKNOWN_HEAD); } err_exit3: free_head(&head); err_exit2: free(rec_data); err_exit1: free(data); err_exit: free(usr); free(pass); return NULL; ok_exit: free(data); free(rec_data); free_head(&head); free(usr); free(pass); return &dir_ent; }