⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
static struct config_group *make_space(struct config_group *g, const char *name){	struct space *sp = NULL;	struct nodes *nds = NULL;	void *gps = NULL;	sp = kzalloc(sizeof(struct space), GFP_KERNEL);	gps = kcalloc(2, sizeof(struct config_group *), GFP_KERNEL);	nds = kzalloc(sizeof(struct nodes), GFP_KERNEL);	if (!sp || !gps || !nds)		goto fail;	config_group_init_type_name(&sp->group, name, &space_type);	config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);	sp->group.default_groups = gps;	sp->group.default_groups[0] = &nds->ns_group;	sp->group.default_groups[1] = NULL;	INIT_LIST_HEAD(&sp->members);	mutex_init(&sp->members_lock);	sp->members_count = 0;	return &sp->group; fail:	kfree(sp);	kfree(gps);	kfree(nds);	return NULL;}static void drop_space(struct config_group *g, struct config_item *i){	struct space *sp = to_space(i);	struct config_item *tmp;	int j;	/* assert list_empty(&sp->members) */	for (j = 0; sp->group.default_groups[j]; j++) {		tmp = &sp->group.default_groups[j]->cg_item;		sp->group.default_groups[j] = NULL;		config_item_put(tmp);	}	config_item_put(i);}static void release_space(struct config_item *i){	struct space *sp = to_space(i);	kfree(sp->group.default_groups);	kfree(sp);}static struct config_item *make_comm(struct config_group *g, const char *name){	struct comm *cm;	cm = kzalloc(sizeof(struct comm), GFP_KERNEL);	if (!cm)		return NULL;	config_item_init_type_name(&cm->item, name, &comm_type);	cm->nodeid = -1;	cm->local = 0;	cm->addr_count = 0;	return &cm->item;}static void drop_comm(struct config_group *g, struct config_item *i){	struct comm *cm = to_comm(i);	if (local_comm == cm)		local_comm = NULL;	dlm_lowcomms_close(cm->nodeid);	while (cm->addr_count--)		kfree(cm->addr[cm->addr_count]);	config_item_put(i);}static void release_comm(struct config_item *i){	struct comm *cm = to_comm(i);	kfree(cm);}static struct config_item *make_node(struct config_group *g, const char *name){	struct space *sp = to_space(g->cg_item.ci_parent);	struct node *nd;	nd = kzalloc(sizeof(struct node), GFP_KERNEL);	if (!nd)		return NULL;	config_item_init_type_name(&nd->item, name, &node_type);	nd->nodeid = -1;	nd->weight = 1;  /* default weight of 1 if none is set */	mutex_lock(&sp->members_lock);	list_add(&nd->list, &sp->members);	sp->members_count++;	mutex_unlock(&sp->members_lock);	return &nd->item;}static void drop_node(struct config_group *g, struct config_item *i){	struct space *sp = to_space(g->cg_item.ci_parent);	struct node *nd = to_node(i);	mutex_lock(&sp->members_lock);	list_del(&nd->list);	sp->members_count--;	mutex_unlock(&sp->members_lock);	config_item_put(i);}static void release_node(struct config_item *i){	struct node *nd = to_node(i);	kfree(nd);}static struct clusters clusters_root = {	.subsys = {		.su_group = {			.cg_item = {				.ci_namebuf = "dlm",				.ci_type = &clusters_type,			},		},	},};int dlm_config_init(void){	config_group_init(&clusters_root.subsys.su_group);	mutex_init(&clusters_root.subsys.su_mutex);	return configfs_register_subsystem(&clusters_root.subsys);}void dlm_config_exit(void){	configfs_unregister_subsystem(&clusters_root.subsys);}/* * Functions for user space to read/write attributes */static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a,			    char *buf){	struct cluster *cl = to_cluster(i);	struct cluster_attribute *cla =			container_of(a, struct cluster_attribute, attr);	return cla->show ? cla->show(cl, buf) : 0;}static ssize_t store_cluster(struct config_item *i,			     struct configfs_attribute *a,			     const char *buf, size_t len){	struct cluster *cl = to_cluster(i);	struct cluster_attribute *cla =		container_of(a, struct cluster_attribute, attr);	return cla->store ? cla->store(cl, buf, len) : -EINVAL;}static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a,			 char *buf){	struct comm *cm = to_comm(i);	struct comm_attribute *cma =			container_of(a, struct comm_attribute, attr);	return cma->show ? cma->show(cm, buf) : 0;}static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a,			  const char *buf, size_t len){	struct comm *cm = to_comm(i);	struct comm_attribute *cma =		container_of(a, struct comm_attribute, attr);	return cma->store ? cma->store(cm, buf, len) : -EINVAL;}static ssize_t comm_nodeid_read(struct comm *cm, char *buf){	return sprintf(buf, "%d\n", cm->nodeid);}static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len){	cm->nodeid = simple_strtol(buf, NULL, 0);	return len;}static ssize_t comm_local_read(struct comm *cm, char *buf){	return sprintf(buf, "%d\n", cm->local);}static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len){	cm->local= simple_strtol(buf, NULL, 0);	if (cm->local && !local_comm)		local_comm = cm;	return len;}static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len){	struct sockaddr_storage *addr;	if (len != sizeof(struct sockaddr_storage))		return -EINVAL;	if (cm->addr_count >= DLM_MAX_ADDR_COUNT)		return -ENOSPC;	addr = kzalloc(sizeof(*addr), GFP_KERNEL);	if (!addr)		return -ENOMEM;	memcpy(addr, buf, len);	cm->addr[cm->addr_count++] = addr;	return len;}static ssize_t show_node(struct config_item *i, struct configfs_attribute *a,			 char *buf){	struct node *nd = to_node(i);	struct node_attribute *nda =			container_of(a, struct node_attribute, attr);	return nda->show ? nda->show(nd, buf) : 0;}static ssize_t store_node(struct config_item *i, struct configfs_attribute *a,			  const char *buf, size_t len){	struct node *nd = to_node(i);	struct node_attribute *nda =		container_of(a, struct node_attribute, attr);	return nda->store ? nda->store(nd, buf, len) : -EINVAL;}static ssize_t node_nodeid_read(struct node *nd, char *buf){	return sprintf(buf, "%d\n", nd->nodeid);}static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len){	nd->nodeid = simple_strtol(buf, NULL, 0);	return len;}static ssize_t node_weight_read(struct node *nd, char *buf){	return sprintf(buf, "%d\n", nd->weight);}static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len){	nd->weight = simple_strtol(buf, NULL, 0);	return len;}/* * Functions for the dlm to get the info that's been configured */static struct space *get_space(char *name){	struct config_item *i;	if (!space_list)		return NULL;	mutex_lock(&space_list->cg_subsys->su_mutex);	i = config_group_find_item(space_list, name);	mutex_unlock(&space_list->cg_subsys->su_mutex);	return to_space(i);}static void put_space(struct space *sp){	config_item_put(&sp->group.cg_item);}static struct comm *get_comm(int nodeid, struct sockaddr_storage *addr){	struct config_item *i;	struct comm *cm = NULL;	int found = 0;	if (!comm_list)		return NULL;	mutex_lock(&clusters_root.subsys.su_mutex);	list_for_each_entry(i, &comm_list->cg_children, ci_entry) {		cm = to_comm(i);		if (nodeid) {			if (cm->nodeid != nodeid)				continue;			found = 1;			config_item_get(i);			break;		} else {			if (!cm->addr_count ||			    memcmp(cm->addr[0], addr, sizeof(*addr)))				continue;			found = 1;			config_item_get(i);			break;		}	}	mutex_unlock(&clusters_root.subsys.su_mutex);	if (!found)		cm = NULL;	return cm;}static void put_comm(struct comm *cm){	config_item_put(&cm->item);}/* caller must free mem */int dlm_nodeid_list(char *lsname, int **ids_out){	struct space *sp;	struct node *nd;	int i = 0, rv = 0;	int *ids;	sp = get_space(lsname);	if (!sp)		return -EEXIST;	mutex_lock(&sp->members_lock);	if (!sp->members_count) {		rv = 0;		goto out;	}	ids = kcalloc(sp->members_count, sizeof(int), GFP_KERNEL);	if (!ids) {		rv = -ENOMEM;		goto out;	}	rv = sp->members_count;	list_for_each_entry(nd, &sp->members, list)		ids[i++] = nd->nodeid;	if (rv != i)		printk("bad nodeid count %d %d\n", rv, i);	*ids_out = ids; out:	mutex_unlock(&sp->members_lock);	put_space(sp);	return rv;}int dlm_node_weight(char *lsname, int nodeid){	struct space *sp;	struct node *nd;	int w = -EEXIST;	sp = get_space(lsname);	if (!sp)		goto out;	mutex_lock(&sp->members_lock);	list_for_each_entry(nd, &sp->members, list) {		if (nd->nodeid != nodeid)			continue;		w = nd->weight;		break;	}	mutex_unlock(&sp->members_lock);	put_space(sp); out:	return w;}int dlm_nodeid_to_addr(int nodeid, struct sockaddr_storage *addr){	struct comm *cm = get_comm(nodeid, NULL);	if (!cm)		return -EEXIST;	if (!cm->addr_count)		return -ENOENT;	memcpy(addr, cm->addr[0], sizeof(*addr));	put_comm(cm);	return 0;}int dlm_addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid){	struct comm *cm = get_comm(0, addr);	if (!cm)		return -EEXIST;	*nodeid = cm->nodeid;	put_comm(cm);	return 0;}int dlm_our_nodeid(void){	return local_comm ? local_comm->nodeid : 0;}/* num 0 is first addr, num 1 is second addr */int dlm_our_addr(struct sockaddr_storage *addr, int num){	if (!local_comm)		return -1;	if (num + 1 > local_comm->addr_count)		return -1;	memcpy(addr, local_comm->addr[num], sizeof(*addr));	return 0;}/* Config file defaults */#define DEFAULT_TCP_PORT       21064#define DEFAULT_BUFFER_SIZE     4096#define DEFAULT_RSBTBL_SIZE      256#define DEFAULT_LKBTBL_SIZE     1024#define DEFAULT_DIRTBL_SIZE      512#define DEFAULT_RECOVER_TIMER      5#define DEFAULT_TOSS_SECS         10#define DEFAULT_SCAN_SECS          5#define DEFAULT_LOG_DEBUG          0#define DEFAULT_PROTOCOL           0#define DEFAULT_TIMEWARN_CS      500 /* 5 sec = 500 centiseconds */struct dlm_config_info dlm_config = {	.ci_tcp_port = DEFAULT_TCP_PORT,	.ci_buffer_size = DEFAULT_BUFFER_SIZE,	.ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,	.ci_lkbtbl_size = DEFAULT_LKBTBL_SIZE,	.ci_dirtbl_size = DEFAULT_DIRTBL_SIZE,	.ci_recover_timer = DEFAULT_RECOVER_TIMER,	.ci_toss_secs = DEFAULT_TOSS_SECS,	.ci_scan_secs = DEFAULT_SCAN_SECS,	.ci_log_debug = DEFAULT_LOG_DEBUG,	.ci_protocol = DEFAULT_PROTOCOL,	.ci_timewarn_cs = DEFAULT_TIMEWARN_CS};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -