static struct event_pool * event_pool_new_epoll (int count, int eventthreadcount) { struct event_pool *event_pool = NULL; int epfd = -1; event_pool = GF_CALLOC (1, sizeof (*event_pool), gf_common_mt_event_pool); if (!event_pool) goto out; epfd = epoll_create (count); if (epfd == -1) { gf_msg ("epoll", GF_LOG_ERROR, errno, LG_MSG_EPOLL_FD_CREATE_FAILED, "epoll fd creation " "failed"); GF_FREE (event_pool->reg); GF_FREE (event_pool); event_pool = NULL; goto out; } event_pool->fd = epfd; event_pool->count = count; event_pool->eventthreadcount = eventthreadcount; pthread_mutex_init (&event_pool->mutex, NULL); out: return event_pool; }
int xlator_volopt_dynload (char *xlator_type, void **dl_handle, volume_opt_list_t *opt_list) { int ret = -1; char *name = NULL; void *handle = NULL; volume_opt_list_t *vol_opt = NULL; GF_VALIDATE_OR_GOTO ("xlator", xlator_type, out); GF_ASSERT (dl_handle); if (*dl_handle) if (dlclose (*dl_handle)) gf_log ("xlator", GF_LOG_WARNING, "Unable to close " "previously opened handle( may be stale)." "Ignoring the invalid handle"); ret = gf_asprintf (&name, "%s/%s.so", XLATORDIR, xlator_type); if (-1 == ret) { gf_log ("xlator", GF_LOG_ERROR, "asprintf failed"); goto out; } ret = -1; gf_log ("xlator", GF_LOG_TRACE, "attempt to load file %s", name); handle = dlopen (name, RTLD_NOW|RTLD_GLOBAL); if (!handle) { gf_log ("xlator", GF_LOG_WARNING, "%s", dlerror ()); goto out; } *dl_handle = handle; vol_opt = GF_CALLOC (1, sizeof (volume_opt_list_t), gf_common_mt_volume_opt_list_t); if (!vol_opt) { goto out; } if (!(vol_opt->given_opt = dlsym (handle, "options"))) { dlerror (); gf_log ("xlator", GF_LOG_DEBUG, "Strict option validation not enforced -- neglecting"); } opt_list->given_opt = vol_opt->given_opt; INIT_LIST_HEAD (&vol_opt->list); list_add_tail (&vol_opt->list, &opt_list->list); ret = 0; out: gf_log ("xlator", GF_LOG_DEBUG, "Returning %d", ret); return ret; }
int lic_dict_add_words(dict_t *dict, const char **words, int wordcount) { char *cmd = NULL; int len = 0; int i = 0; int ret = 0; for (i=0; i<wordcount; i++) len += strlen(words[i]) + 1; if (len < 1) goto out; cmd = GF_CALLOC(1, len, gf_common_mt_char); if (!cmd) { ret = -1; goto out; } for (i=0; i<wordcount; i++) { strncat(cmd, words[i], strlen(words[i])); if (i < wordcount-1) strncat (cmd, " ", strlen (" ")); } ret = dict_set_dynstr (dict, "cmd-str", cmd); if (ret) goto out; out: return ret; }
static struct event_pool * event_pool_new_epoll (int count, int eventthreadcount) { struct event_pool *event_pool = NULL; int epfd = -1; event_pool = GF_CALLOC (1, sizeof (*event_pool), gf_common_mt_event_pool); if (!event_pool) goto out; epfd = epoll_create (count); if (epfd == -1) { gf_log ("epoll", GF_LOG_ERROR, "epoll fd creation failed (%s)", strerror (errno)); GF_FREE (event_pool->reg); GF_FREE (event_pool); event_pool = NULL; goto out; } event_pool->fd = epfd; event_pool->count = count; event_pool->eventthreadcount = eventthreadcount; pthread_mutex_init (&event_pool->mutex, NULL); out: return event_pool; }
int make_export_path (const char *real_path, char **path) { int ret = -1; char *tmp = NULL; char *export_path = NULL; char *dup = NULL; char *ptr = NULL; char *freeptr = NULL; uuid_t gfid = {0, }; export_path = GF_CALLOC (1, sizeof (char) * PATH_MAX, 0); if (!export_path) goto out; dup = gf_strdup (real_path); if (!dup) goto out; freeptr = dup; ret = solaris_getxattr ("/", GFID_XATTR_KEY, gfid, 16); /* Return value of getxattr */ if (ret == 16) { if (__is_root_gfid (gfid)) { strcat (export_path, "/"); ret = 0; goto done; } } do { ptr = strtok_r (dup, "/", &tmp); if (!ptr) break; strcat (export_path, dup); ret = solaris_getxattr (export_path, GFID_XATTR_KEY, gfid, 16); if (ret == 16) { if (__is_root_gfid (gfid)) { ret = 0; goto done; } } strcat (export_path, "/"); dup = tmp; } while (ptr); goto out; done: if (!ret) { *path = export_path; } out: if (freeptr) GF_FREE (freeptr); if (ret && export_path) GF_FREE (export_path); return ret; }
gf_timer_registry_t * gf_timer_registry_init (glusterfs_ctx_t *ctx) { if (ctx == NULL) { gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument"); return NULL; } if (!ctx->timer) { gf_timer_registry_t *reg = NULL; reg = GF_CALLOC (1, sizeof (*reg), gf_common_mt_gf_timer_registry_t); if (!reg) goto out; pthread_mutex_init (®->lock, NULL); reg->active.next = ®->active; reg->active.prev = ®->active; reg->stale.next = ®->stale; reg->stale.prev = ®->stale; ctx->timer = reg; pthread_create (®->th, NULL, gf_timer_proc, ctx); } out: return ctx->timer; }
/** * _export_file_init -- Initialize an exports file structure. * * @return : success: Pointer to an allocated exports file struct * failure: NULL * * Not for external use. */ struct exports_file * _exports_file_init() { struct exports_file *file = NULL; file = GF_CALLOC(1, sizeof(*file), gf_common_mt_nfs_exports); if (!file) { gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY, "Failed to allocate file struct!"); goto out; } file->exports_dict = dict_new(); file->exports_map = dict_new(); if (!file->exports_dict || !file->exports_map) { gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY, "Failed to allocate dict!"); goto free_and_out; } goto out; free_and_out: if (file->exports_dict) dict_unref(file->exports_dict); GF_FREE(file); file = NULL; out: return file; }
static int glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx) { cmd_args_t *cmd_args = NULL; struct rlimit lim = {0, }; call_pool_t *pool = NULL; xlator_mem_acct_init (THIS, cli_mt_end); ctx->process_uuid = generate_uuid (); if (!ctx->process_uuid) return -1; ctx->page_size = 128 * GF_UNIT_KB; ctx->iobuf_pool = iobuf_pool_new (8 * GF_UNIT_MB, ctx->page_size); if (!ctx->iobuf_pool) return -1; ctx->event_pool = event_pool_new (DEFAULT_EVENT_POOL_SIZE); if (!ctx->event_pool) return -1; pool = GF_CALLOC (1, sizeof (call_pool_t), cli_mt_call_pool_t); if (!pool) return -1; /* frame_mem_pool size 112 * 16k */ pool->frame_mem_pool = mem_pool_new (call_frame_t, 16384); if (!pool->frame_mem_pool) return -1; /* stack_mem_pool size 256 * 8k */ pool->stack_mem_pool = mem_pool_new (call_stack_t, 8192); if (!pool->stack_mem_pool) return -1; ctx->stub_mem_pool = mem_pool_new (call_stub_t, 1024); if (!ctx->stub_mem_pool) return -1; INIT_LIST_HEAD (&pool->all_frames); LOCK_INIT (&pool->lock); ctx->pool = pool; pthread_mutex_init (&(ctx->lock), NULL); cmd_args = &ctx->cmd_args; INIT_LIST_HEAD (&cmd_args->xlator_options); lim.rlim_cur = RLIM_INFINITY; lim.rlim_max = RLIM_INFINITY; setrlimit (RLIMIT_CORE, &lim); return 0; }
clienttable_t * gf_clienttable_alloc (void) { clienttable_t *clienttable = NULL; int result = 0; clienttable = GF_CALLOC (1, sizeof (clienttable_t), gf_common_mt_clienttable_t); if (!clienttable) return NULL; LOCK_INIT (&clienttable->lock); result = gf_client_clienttable_expand (clienttable, GF_CLIENTTABLE_INITIAL_SIZE); if (result != 0) { gf_msg ("client_t", GF_LOG_ERROR, 0, LG_MSG_EXPAND_CLIENT_TABLE_FAILED, "gf_client_clienttable_expand failed"); GF_FREE (clienttable); return NULL; } return clienttable; }
void runner_log (runner_t *runner, const char *dom, gf_loglevel_t lvl, const char *msg) { char *buf = NULL; size_t len = 0; int i = 0; if (runner->runerr) return; for (i = 0;; i++) { if (runner->argv[i] == NULL) break; len += (strlen (runner->argv[i]) + 1); } buf = GF_CALLOC (1, len + 1, gf_common_mt_run_logbuf); if (!buf) { runner->runerr = errno; return; } for (i = 0;; i++) { if (runner->argv[i] == NULL) break; strcat (buf, runner->argv[i]); strcat (buf, " "); } if (len > 0) buf[len - 1] = '\0'; gf_msg_callingfn (dom, lvl, 0, LG_MSG_RUNNER_LOG, "%s: %s", msg, buf); GF_FREE (buf); }
br_stub_fd_t * br_stub_fd_new(void) { br_stub_fd_t *br_stub_fd = NULL; br_stub_fd = GF_CALLOC(1, sizeof(*br_stub_fd), gf_br_stub_mt_br_stub_fd_t); return br_stub_fd; }
strfd_t * strfd_open () { strfd_t *strfd = NULL; strfd = GF_CALLOC(1, sizeof(*strfd), gf_common_mt_strfd_t); return strfd; }
struct mem_pool * mem_pool_new_fn (unsigned long sizeof_type, unsigned long count) { struct mem_pool *mem_pool = NULL; unsigned long padded_sizeof_type = 0; void *pool = NULL; int i = 0; struct list_head *list = NULL; if (!sizeof_type || !count) { gf_log ("mem-pool", GF_LOG_ERROR, "invalid argument"); return NULL; } padded_sizeof_type = sizeof_type + GF_MEM_POOL_PAD_BOUNDARY; mem_pool = GF_CALLOC (sizeof (*mem_pool), 1, gf_common_mt_mem_pool); if (!mem_pool) return NULL; LOCK_INIT (&mem_pool->lock); INIT_LIST_HEAD (&mem_pool->list); mem_pool->padded_sizeof_type = padded_sizeof_type; mem_pool->cold_count = count; mem_pool->real_sizeof_type = sizeof_type; pool = GF_CALLOC (count, padded_sizeof_type, gf_common_mt_long); if (!pool) { GF_FREE (mem_pool); return NULL; } for (i = 0; i < count; i++) { list = pool + (i * (padded_sizeof_type)); INIT_LIST_HEAD (list); list_add_tail (list, &mem_pool->list); } mem_pool->pool = pool; mem_pool->pool_end = pool + (count * (padded_sizeof_type)); return mem_pool; }
int32_t alloc_format(crypt_local_t *local, size_t size) { if (size > 0) { local->format = GF_CALLOC(1, size, gf_crypt_mt_mtd); if (!local->format) return ENOMEM; } local->format_size = size; return 0; }
/** * _export_dir_init -- Initialize an export directory structure. * * @return : success: Pointer to an allocated exports directory struct * failure: NULL * * Not for external use. */ static struct export_dir * _export_dir_init() { struct export_dir *expdir = GF_CALLOC(1, sizeof(*expdir), gf_common_mt_nfs_exports); if (!expdir) gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY, "Failed to allocate export dir structure!"); return expdir; }
/** * _export_item_init -- Initialize an export item structure * * @return : success: Pointer to an allocated exports item struct * failure: NULL * * Not for external use. */ static struct export_item * _export_item_init () { struct export_item *item = GF_CALLOC (1, sizeof (*item), gf_common_mt_nfs_exports); if (!item) gf_msg (GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY, "Failed to allocate export item!"); return item; }
/** * auth_cache_entry_init -- Initialize an auth cache entry * * @return: Pointer to an allocated auth cache entry, NULL if allocation * failed. */ struct auth_cache_entry * auth_cache_entry_init () { struct auth_cache_entry *entry = NULL; entry = GF_CALLOC (1, sizeof (*entry), gf_nfs_mt_auth_cache_entry); if (!entry) gf_msg (GF_NFS, GF_LOG_WARNING, ENOMEM, NFS_MSG_NO_MEMORY, "failed to allocate entry"); return entry; }
/** * _export_host_init -- Initialize an export options struct * * @return : success: Pointer to an allocated options struct * failure: NULL * * Not for external use. */ static struct export_options * _export_options_init() { struct export_options *opts = GF_CALLOC(1, sizeof(*opts), gf_common_mt_nfs_exports); if (!opts) gf_msg(GF_EXP, GF_LOG_CRITICAL, ENOMEM, NFS_MSG_NO_MEMORY, "Failed to allocate options structure!"); return opts; }
/** * auth_cache_init -- Initialize an auth cache and set the ttl_sec * * @ttl_sec : The TTL to set in seconds * * @return : allocated auth cache struct, NULL if allocation failed. */ struct auth_cache * auth_cache_init (time_t ttl_sec) { struct auth_cache *cache = GF_CALLOC (1, sizeof (*cache), gf_nfs_mt_auth_cache); GF_VALIDATE_OR_GOTO ("auth-cache", cache, out); cache->ttl_sec = ttl_sec; out: return cache; }
// 插入一个事件到定时器链表中 gf_timer_t * gf_timer_call_after (glusterfs_ctx_t *ctx, struct timespec delta, gf_timer_cbk_t callbk, void *data) { gf_timer_registry_t *reg = NULL; gf_timer_t *event = NULL; gf_timer_t *trav = NULL; uint64_t at = 0; if (ctx == NULL) { gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument"); return NULL; } reg = gf_timer_registry_init (ctx); if (!reg) { gf_log_callingfn ("timer", GF_LOG_ERROR, "!reg"); return NULL; } event = GF_CALLOC (1, sizeof (*event), gf_common_mt_gf_timer_t); if (!event) { return NULL; } timespec_now (&event->at); timespec_adjust_delta (&event->at, delta); at = TS (event->at); event->callbk = callbk; event->data = data; event->xl = THIS; pthread_mutex_lock (®->lock); { //列表最后一个 trav = reg->active.prev; //找最后一个时间比我早的(链表是按时间排序的) while (trav != ®->active) { if (TS (trav->at) < at) break; trav = trav->prev; } event->prev = trav; event->next = event->prev->next; event->prev->next = event; event->next->prev = event; } pthread_mutex_unlock (®->lock); return event; }
int clrlk_parse_args (const char* cmd, clrlk_args *args) { char *opts = NULL; char *cur = NULL; char *tok = NULL; char *sptr = NULL; char *free_ptr = NULL; char kw[KW_MAX] = {[KW_TYPE] = 't', [KW_KIND] = 'k', }; int ret = -1; int i = 0; GF_ASSERT (cmd); free_ptr = opts = GF_CALLOC (1, strlen (cmd), gf_common_mt_char); if (!opts) goto out; if (sscanf (cmd, GF_XATTR_CLRLK_CMD".%s", opts) < 1) { ret = -1; goto out; } /*clr_lk_prefix.ttype.kkind.args, args - type specific*/ cur = opts; for (i = 0; i < KW_MAX && (tok = strtok_r (cur, ".", &sptr)); cur = NULL, i++) { if (tok[0] != kw[i]) { ret = -1; goto out; } if (i == KW_TYPE) args->type = clrlk_get_type (tok+1); if (i == KW_KIND) args->kind = clrlk_get_kind (tok+1); } if ((args->type == CLRLK_TYPE_MAX) || (args->kind == CLRLK_KIND_MAX)) goto out; /*optional args, neither range nor basename can 'legally' contain * "/" in them*/ tok = strtok_r (NULL, "/", &sptr); if (tok) args->opts = gf_strdup (tok); ret = 0; out: GF_FREE (free_ptr); return ret; }
gf_timer_t * gf_timer_call_after (glusterfs_ctx_t *ctx, struct timespec delta, gf_timer_cbk_t callbk, void *data) { gf_timer_registry_t *reg = NULL; gf_timer_t *event = NULL; gf_timer_t *trav = NULL; uint64_t at = 0; if (ctx == NULL) { gf_msg_callingfn ("timer", GF_LOG_ERROR, EINVAL, LG_MSG_INVALID_ARG, "invalid argument"); return NULL; } reg = gf_timer_registry_init (ctx); if (!reg) { gf_msg_callingfn ("timer", GF_LOG_ERROR, 0, LG_MSG_TIMER_REGISTER_ERROR, "!reg"); return NULL; } event = GF_CALLOC (1, sizeof (*event), gf_common_mt_gf_timer_t); if (!event) { return NULL; } timespec_now (&event->at); timespec_adjust_delta (&event->at, delta); at = TS (event->at); event->callbk = callbk; event->data = data; event->xl = THIS; LOCK (®->lock); { trav = reg->active.prev; while (trav != ®->active) { if (TS (trav->at) < at) break; trav = trav->prev; } event->prev = trav; event->next = event->prev->next; event->prev->next = event; event->next->prev = event; } UNLOCK (®->lock); return event; }
gf_timer_t * gf_timer_call_after (glusterfs_ctx_t *ctx, struct timeval delta, gf_timer_cbk_t callbk, void *data) { gf_timer_registry_t *reg = NULL; gf_timer_t *event = NULL; gf_timer_t *trav = NULL; unsigned long long at = 0L; if (ctx == NULL) { gf_log_callingfn ("timer", GF_LOG_ERROR, "invalid argument"); return NULL; } reg = gf_timer_registry_init (ctx); if (!reg) { gf_log_callingfn ("timer", GF_LOG_ERROR, "!reg"); return NULL; } event = GF_CALLOC (1, sizeof (*event), gf_common_mt_gf_timer_t); if (!event) { return NULL; } gettimeofday (&event->at, NULL); event->at.tv_usec = ((event->at.tv_usec + delta.tv_usec) % 1000000); event->at.tv_sec += ((event->at.tv_usec + delta.tv_usec) / 1000000); event->at.tv_sec += delta.tv_sec; at = TS (event->at); event->callbk = callbk; event->data = data; event->xl = THIS; pthread_mutex_lock (®->lock); { trav = reg->active.prev; while (trav != ®->active) { if (TS (trav->at) < at) break; trav = trav->prev; } event->prev = trav; event->next = event->prev->next; event->prev->next = event; event->next->prev = event; } pthread_mutex_unlock (®->lock); return event; }
trie_t * trie_new () { trie_t *trie = NULL; trie = GF_CALLOC (1, sizeof (*trie), gf_common_mt_trie_trie); if (!trie) return NULL; trie->root.trie = trie; return trie; }
mountres3 * mountudpproc3_mnt_3_svc(dirpath **dpp, struct svc_req *req) { struct mountres3 *res = NULL; int *autharr = NULL; struct nfs3_fh *fh = NULL; char *tmp = NULL; tmp = (char *)*dpp; while (*tmp == '/') tmp++; fh = nfs3_rootfh (tmp); if (fh == NULL) { gf_log (GF_MNT, GF_LOG_DEBUG, "unable to get fh for %s", tmp); goto err; } res = GF_CALLOC (1, sizeof(*res), gf_nfs_mt_mountres3); if (res == NULL) { gf_log (GF_MNT, GF_LOG_ERROR, "unable to allocate memory"); goto err; } autharr = GF_CALLOC (1, sizeof(*autharr), gf_nfs_mt_int); if (autharr == NULL) { gf_log (GF_MNT, GF_LOG_ERROR, "unable to allocate memory"); goto err; } autharr[0] = AUTH_UNIX; *res = mnt3svc_set_mountres3 (MNT3_OK, fh, autharr, 1); mount3udp_add_mountlist (mnthost, *dpp); return res; err: GF_FREE (fh); GF_FREE (res); GF_FREE (autharr); return NULL; }
/****************************************************************************** * * Util functions * * ***************************************************************************/ inline gf_sql_connection_t * gf_sql_connection_init () { gf_sql_connection_t *gf_sql_conn = NULL; gf_sql_conn = GF_CALLOC (1, sizeof(gf_sql_connection_t), gf_mt_sql_connection_t); if (gf_sql_conn == NULL) { gf_log (GFDB_STR_SQLITE3, GF_LOG_ERROR, "Error allocating memory to gf_sql_connection_t "); } return gf_sql_conn; }
mountstat3 * mountudpproc3_umnt_3_svc(dirpath **dp, struct svc_req *req) { mountstat3 *stat = NULL; stat = GF_CALLOC (1, sizeof(mountstat3), gf_nfs_mt_mountstat3); if (stat == NULL) { gf_log (GF_MNT, GF_LOG_ERROR, "unable to allocate memory"); return NULL; } *stat = MNT3_OK; mount3udp_delete_mountlist (mnthost, *dp); return stat; }
marker_inode_ctx_t * marker_inode_ctx_new () { marker_inode_ctx_t *ctx = NULL; ctx = GF_CALLOC (1, sizeof (marker_inode_ctx_t), gf_marker_mt_marker_inode_ctx_t); if (ctx == NULL) goto out; ctx->quota_ctx = NULL; out: return ctx; }
int trienode_get_word (trienode_t *node, char **bufp) { char *buf = NULL; buf = GF_CALLOC (1, node->depth + 1, gf_common_mt_trie_buf); if (!buf) return -1; *bufp = buf; print_node (node, &buf); return 0; }
/* hold lock while calling this function */ int __cb_add_entry_buffer (buffer_t *buffer, void *item) { circular_buffer_t *ptr = NULL; int ret = -1; //DO we really need the assert here? GF_ASSERT (buffer->used_len <= buffer->size_buffer); if (buffer->use_once == _gf_true && buffer->used_len == buffer->size_buffer) { gf_msg ("circ-buff", GF_LOG_WARNING, 0, LG_MSG_BUFFER_ERROR, "buffer %p is use once buffer", buffer); return -1; } else { if (buffer->used_len == buffer->size_buffer) { if (buffer->cb[buffer->w_index]) { ptr = buffer->cb[buffer->w_index]; if (ptr->data) { cb_destroy_data (ptr, buffer->destroy_buffer_data); ptr->data = NULL; GF_FREE (ptr); } buffer->cb[buffer->w_index] = NULL; ptr = NULL; } } buffer->cb[buffer->w_index] = GF_CALLOC (1, sizeof (circular_buffer_t), gf_common_mt_circular_buffer_t); if (!buffer->cb[buffer->w_index]) return -1; buffer->cb[buffer->w_index]->data = item; ret = gettimeofday (&buffer->cb[buffer->w_index]->tv, NULL); if (ret == -1) gf_msg_callingfn ("circ-buff", GF_LOG_WARNING, 0, LG_MSG_GETTIMEOFDAY_FAILED, "getting time of the day failed"); buffer->w_index++; buffer->w_index %= buffer->size_buffer; //used_buffer size cannot be greater than the total buffer size if (buffer->used_len < buffer->size_buffer) buffer->used_len++; return buffer->w_index; } }