static FDFSTrunkNode *trunk_create_trunk_file(const int store_path_index, \ int *err_no) { FDFSTrunkNode *pTrunkNode; struct fast_mblock_node *pMblockNode; pMblockNode = fast_mblock_alloc(&free_blocks_man); if (pMblockNode == NULL) { *err_no = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(FDFSTrunkNode), \ *err_no, STRERROR(*err_no)); return NULL; } pTrunkNode = (FDFSTrunkNode *)pMblockNode->data; pTrunkNode->pMblockNode = pMblockNode; if (store_path_index >= 0) { pTrunkNode->trunk.path.store_path_index = store_path_index; } else { int result; int new_store_path_index; if ((result=storage_get_storage_path_index( \ &new_store_path_index)) != 0) { logError("file: "__FILE__", line: %d, " \ "get_storage_path_index fail, " \ "errno: %d, error info: %s", __LINE__, \ result, STRERROR(result)); return NULL; } pTrunkNode->trunk.path.store_path_index = new_store_path_index; } pTrunkNode->trunk.file.offset = 0; pTrunkNode->trunk.file.size = g_trunk_file_size; pTrunkNode->trunk.status = FDFS_TRUNK_STATUS_FREE; pTrunkNode->next = NULL; *err_no = trunk_create_next_file(&(pTrunkNode->trunk)); if (*err_no != 0) { fast_mblock_free(&free_blocks_man, pMblockNode); return NULL; } *err_no = trunk_mem_binlog_write(g_current_time, \ TRUNK_OP_TYPE_ADD_SPACE, &(pTrunkNode->trunk)); return pTrunkNode; }
static int trunk_split(FDFSTrunkNode *pNode, const int size) { int result; struct fast_mblock_node *pMblockNode; FDFSTrunkNode *pTrunkNode; if (pNode->trunk.file.size - size < g_slot_min_size) { return trunk_mem_binlog_write(g_current_time, \ TRUNK_OP_TYPE_DEL_SPACE, &(pNode->trunk)); } pMblockNode = fast_mblock_alloc(&free_blocks_man); if (pMblockNode == NULL) { result = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(FDFSTrunkNode), \ result, STRERROR(result)); return result; } result = trunk_mem_binlog_write(g_current_time, \ TRUNK_OP_TYPE_DEL_SPACE, &(pNode->trunk)); if (result != 0) { fast_mblock_free(&free_blocks_man, pMblockNode); return result; } pTrunkNode = (FDFSTrunkNode *)pMblockNode->data; memcpy(pTrunkNode, pNode, sizeof(FDFSTrunkNode)); pTrunkNode->pMblockNode = pMblockNode; pTrunkNode->trunk.file.offset = pNode->trunk.file.offset + size; pTrunkNode->trunk.file.size = pNode->trunk.file.size - size; pTrunkNode->trunk.status = FDFS_TRUNK_STATUS_FREE; pTrunkNode->next = NULL; result = trunk_add_free_block(pTrunkNode, true); if (result != 0) { return result; } pNode->trunk.file.size = size; return 0; }
static void trunk_delete_size_tree_entry(FDFSTrunkSlot *pSlot) { if (avl_tree_delete(&tree_info_by_size, pSlot) == 1) { fast_mblock_free(&tree_nodes_man, \ pSlot->pMblockNode); } else { logWarning("file: "__FILE__", line: %d, " \ "can't delete slot entry, size: %d", \ __LINE__, pSlot->size); } }
static int trunk_add_space_by_node(FDFSTrunkNode *pTrunkNode) { int result; if (pTrunkNode->trunk.file.size < g_slot_min_size) { logDebug("file: "__FILE__", line: %d, " \ "space: %d is too small, do not need recycle!", \ __LINE__, pTrunkNode->trunk.file.size); fast_mblock_free(&free_blocks_man, pTrunkNode->pMblockNode); return 0; } result = trunk_add_free_block(pTrunkNode, false); if (result == 0) { return 0; } else { fast_mblock_free(&free_blocks_man, pTrunkNode->pMblockNode); return (result == EEXIST) ? 0 : result; } }
static FDFSTrunkNode *trunk_create_trunk_file(int *err_no) { FDFSTrunkNode *pTrunkNode; struct fast_mblock_node *pMblockNode; pMblockNode = fast_mblock_alloc(&free_blocks_man); if (pMblockNode == NULL) { *err_no = errno != 0 ? errno : EIO; logError("file: "__FILE__", line: %d, " \ "malloc %d bytes fail, " \ "errno: %d, error info: %s", \ __LINE__, (int)sizeof(FDFSTrunkNode), \ *err_no, STRERROR(*err_no)); return NULL; } pTrunkNode = (FDFSTrunkNode *)pMblockNode->data; pTrunkNode->pMblockNode = pMblockNode; pTrunkNode->trunk.file.offset = 0; pTrunkNode->trunk.file.size = g_trunk_file_size; pTrunkNode->trunk.status = FDFS_TRUNK_STATUS_FREE; pTrunkNode->next = NULL; *err_no = trunk_create_next_file(&(pTrunkNode->trunk)); if (*err_no != 0) { fast_mblock_free(&free_blocks_man, pMblockNode); return NULL; } *err_no = trunk_mem_binlog_write(g_current_time, \ TRUNK_OP_TYPE_ADD_SPACE, &(pTrunkNode->trunk)); return pTrunkNode; }
static void storage_trunk_free_node(void *ptr) { fast_mblock_free(&free_blocks_man, \ ((FDFSTrunkNode *)ptr)->pMblockNode); }
static int trunk_delete_space(const FDFSTrunkFullInfo *pTrunkInfo, \ const bool bWriteBinLog) { int result; FDFSTrunkSlot target_slot; char buff[256]; FDFSTrunkSlot *pSlot; FDFSTrunkNode *pPrevious; FDFSTrunkNode *pCurrent; target_slot.size = pTrunkInfo->file.size; target_slot.head = NULL; pthread_mutex_lock(&trunk_mem_lock); pSlot = (FDFSTrunkSlot *)avl_tree_find(tree_info_by_sizes + \ pTrunkInfo->path.store_path_index, &target_slot); if (pSlot == NULL) { pthread_mutex_unlock(&trunk_mem_lock); logError("file: "__FILE__", line: %d, " \ "can't find trunk entry: %s", __LINE__, \ trunk_info_dump(pTrunkInfo, buff, sizeof(buff))); return ENOENT; } pPrevious = NULL; pCurrent = pSlot->head; while (pCurrent != NULL && memcmp(&(pCurrent->trunk), pTrunkInfo, \ sizeof(FDFSTrunkFullInfo)) != 0) { pPrevious = pCurrent; pCurrent = pCurrent->next; } if (pCurrent == NULL) { pthread_mutex_unlock(&trunk_mem_lock); logError("file: "__FILE__", line: %d, " \ "can't find trunk entry: %s", __LINE__, \ trunk_info_dump(pTrunkInfo, buff, sizeof(buff))); return ENOENT; } if (pPrevious == NULL) { pSlot->head = pCurrent->next; if (pSlot->head == NULL) { trunk_delete_size_tree_entry(pTrunkInfo->path. \ store_path_index, pSlot); } } else { pPrevious->next = pCurrent->next; } trunk_free_block_delete(&(pCurrent->trunk)); pthread_mutex_unlock(&trunk_mem_lock); if (bWriteBinLog) { result = trunk_mem_binlog_write(g_current_time, \ TRUNK_OP_TYPE_DEL_SPACE, &(pCurrent->trunk)); } else { pthread_mutex_lock(&trunk_file_lock); g_trunk_total_free_space -= pCurrent->trunk.file.size; pthread_mutex_unlock(&trunk_file_lock); result = 0; } fast_mblock_free(&free_blocks_man, pCurrent->pMblockNode); return result; }