📄 config.c
字号:
/***************************************************************************************************************************************************************** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.**** This copyrighted material is made available to anyone wishing to use,** modify, copy, or redistribute it subject to the terms and conditions** of the GNU General Public License v.2.***************************************************************************************************************************************************************/#include <linux/kernel.h>#include <linux/module.h>#include <linux/configfs.h>#include <net/sock.h>#include "config.h"#include "lowcomms.h"/* * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight * /config/dlm/<cluster>/comms/<comm>/nodeid * /config/dlm/<cluster>/comms/<comm>/local * /config/dlm/<cluster>/comms/<comm>/addr * The <cluster> level is useless, but I haven't figured out how to avoid it. */static struct config_group *space_list;static struct config_group *comm_list;static struct comm *local_comm;struct clusters;struct cluster;struct spaces;struct space;struct comms;struct comm;struct nodes;struct node;static struct config_group *make_cluster(struct config_group *, const char *);static void drop_cluster(struct config_group *, struct config_item *);static void release_cluster(struct config_item *);static struct config_group *make_space(struct config_group *, const char *);static void drop_space(struct config_group *, struct config_item *);static void release_space(struct config_item *);static struct config_item *make_comm(struct config_group *, const char *);static void drop_comm(struct config_group *, struct config_item *);static void release_comm(struct config_item *);static struct config_item *make_node(struct config_group *, const char *);static void drop_node(struct config_group *, struct config_item *);static void release_node(struct config_item *);static ssize_t show_cluster(struct config_item *i, struct configfs_attribute *a, char *buf);static ssize_t store_cluster(struct config_item *i, struct configfs_attribute *a, const char *buf, size_t len);static ssize_t show_comm(struct config_item *i, struct configfs_attribute *a, char *buf);static ssize_t store_comm(struct config_item *i, struct configfs_attribute *a, const char *buf, size_t len);static ssize_t show_node(struct config_item *i, struct configfs_attribute *a, char *buf);static ssize_t store_node(struct config_item *i, struct configfs_attribute *a, const char *buf, size_t len);static ssize_t comm_nodeid_read(struct comm *cm, char *buf);static ssize_t comm_nodeid_write(struct comm *cm, const char *buf, size_t len);static ssize_t comm_local_read(struct comm *cm, char *buf);static ssize_t comm_local_write(struct comm *cm, const char *buf, size_t len);static ssize_t comm_addr_write(struct comm *cm, const char *buf, size_t len);static ssize_t node_nodeid_read(struct node *nd, char *buf);static ssize_t node_nodeid_write(struct node *nd, const char *buf, size_t len);static ssize_t node_weight_read(struct node *nd, char *buf);static ssize_t node_weight_write(struct node *nd, const char *buf, size_t len);struct cluster { struct config_group group; unsigned int cl_tcp_port; unsigned int cl_buffer_size; unsigned int cl_rsbtbl_size; unsigned int cl_lkbtbl_size; unsigned int cl_dirtbl_size; unsigned int cl_recover_timer; unsigned int cl_toss_secs; unsigned int cl_scan_secs; unsigned int cl_log_debug; unsigned int cl_protocol; unsigned int cl_timewarn_cs;};enum { CLUSTER_ATTR_TCP_PORT = 0, CLUSTER_ATTR_BUFFER_SIZE, CLUSTER_ATTR_RSBTBL_SIZE, CLUSTER_ATTR_LKBTBL_SIZE, CLUSTER_ATTR_DIRTBL_SIZE, CLUSTER_ATTR_RECOVER_TIMER, CLUSTER_ATTR_TOSS_SECS, CLUSTER_ATTR_SCAN_SECS, CLUSTER_ATTR_LOG_DEBUG, CLUSTER_ATTR_PROTOCOL, CLUSTER_ATTR_TIMEWARN_CS,};struct cluster_attribute { struct configfs_attribute attr; ssize_t (*show)(struct cluster *, char *); ssize_t (*store)(struct cluster *, const char *, size_t);};static ssize_t cluster_set(struct cluster *cl, unsigned int *cl_field, unsigned int *info_field, int check_zero, const char *buf, size_t len){ unsigned int x; if (!capable(CAP_SYS_ADMIN)) return -EACCES; x = simple_strtoul(buf, NULL, 0); if (check_zero && !x) return -EINVAL; *cl_field = x; *info_field = x; return len;}#define CLUSTER_ATTR(name, check_zero) \static ssize_t name##_write(struct cluster *cl, const char *buf, size_t len) \{ \ return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name, \ check_zero, buf, len); \} \static ssize_t name##_read(struct cluster *cl, char *buf) \{ \ return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name); \} \static struct cluster_attribute cluster_attr_##name = \__CONFIGFS_ATTR(name, 0644, name##_read, name##_write)CLUSTER_ATTR(tcp_port, 1);CLUSTER_ATTR(buffer_size, 1);CLUSTER_ATTR(rsbtbl_size, 1);CLUSTER_ATTR(lkbtbl_size, 1);CLUSTER_ATTR(dirtbl_size, 1);CLUSTER_ATTR(recover_timer, 1);CLUSTER_ATTR(toss_secs, 1);CLUSTER_ATTR(scan_secs, 1);CLUSTER_ATTR(log_debug, 0);CLUSTER_ATTR(protocol, 0);CLUSTER_ATTR(timewarn_cs, 1);static struct configfs_attribute *cluster_attrs[] = { [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr, [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size.attr, [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size.attr, [CLUSTER_ATTR_LKBTBL_SIZE] = &cluster_attr_lkbtbl_size.attr, [CLUSTER_ATTR_DIRTBL_SIZE] = &cluster_attr_dirtbl_size.attr, [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer.attr, [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs.attr, [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs.attr, [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug.attr, [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr, [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr, NULL,};enum { COMM_ATTR_NODEID = 0, COMM_ATTR_LOCAL, COMM_ATTR_ADDR,};struct comm_attribute { struct configfs_attribute attr; ssize_t (*show)(struct comm *, char *); ssize_t (*store)(struct comm *, const char *, size_t);};static struct comm_attribute comm_attr_nodeid = { .attr = { .ca_owner = THIS_MODULE, .ca_name = "nodeid", .ca_mode = S_IRUGO | S_IWUSR }, .show = comm_nodeid_read, .store = comm_nodeid_write,};static struct comm_attribute comm_attr_local = { .attr = { .ca_owner = THIS_MODULE, .ca_name = "local", .ca_mode = S_IRUGO | S_IWUSR }, .show = comm_local_read, .store = comm_local_write,};static struct comm_attribute comm_attr_addr = { .attr = { .ca_owner = THIS_MODULE, .ca_name = "addr", .ca_mode = S_IRUGO | S_IWUSR }, .store = comm_addr_write,};static struct configfs_attribute *comm_attrs[] = { [COMM_ATTR_NODEID] = &comm_attr_nodeid.attr, [COMM_ATTR_LOCAL] = &comm_attr_local.attr, [COMM_ATTR_ADDR] = &comm_attr_addr.attr, NULL,};enum { NODE_ATTR_NODEID = 0, NODE_ATTR_WEIGHT,};struct node_attribute { struct configfs_attribute attr; ssize_t (*show)(struct node *, char *); ssize_t (*store)(struct node *, const char *, size_t);};static struct node_attribute node_attr_nodeid = { .attr = { .ca_owner = THIS_MODULE, .ca_name = "nodeid", .ca_mode = S_IRUGO | S_IWUSR }, .show = node_nodeid_read, .store = node_nodeid_write,};static struct node_attribute node_attr_weight = { .attr = { .ca_owner = THIS_MODULE, .ca_name = "weight", .ca_mode = S_IRUGO | S_IWUSR }, .show = node_weight_read, .store = node_weight_write,};static struct configfs_attribute *node_attrs[] = { [NODE_ATTR_NODEID] = &node_attr_nodeid.attr, [NODE_ATTR_WEIGHT] = &node_attr_weight.attr, NULL,};struct clusters { struct configfs_subsystem subsys;};struct spaces { struct config_group ss_group;};struct space { struct config_group group; struct list_head members; struct mutex members_lock; int members_count;};struct comms { struct config_group cs_group;};struct comm { struct config_item item; int nodeid; int local; int addr_count; struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];};struct nodes { struct config_group ns_group;};struct node { struct config_item item; struct list_head list; /* space->members */ int nodeid; int weight;};static struct configfs_group_operations clusters_ops = { .make_group = make_cluster, .drop_item = drop_cluster,};static struct configfs_item_operations cluster_ops = { .release = release_cluster, .show_attribute = show_cluster, .store_attribute = store_cluster,};static struct configfs_group_operations spaces_ops = { .make_group = make_space, .drop_item = drop_space,};static struct configfs_item_operations space_ops = { .release = release_space,};static struct configfs_group_operations comms_ops = { .make_item = make_comm, .drop_item = drop_comm,};static struct configfs_item_operations comm_ops = { .release = release_comm, .show_attribute = show_comm, .store_attribute = store_comm,};static struct configfs_group_operations nodes_ops = { .make_item = make_node, .drop_item = drop_node,};static struct configfs_item_operations node_ops = { .release = release_node, .show_attribute = show_node, .store_attribute = store_node,};static struct config_item_type clusters_type = { .ct_group_ops = &clusters_ops, .ct_owner = THIS_MODULE,};static struct config_item_type cluster_type = { .ct_item_ops = &cluster_ops, .ct_attrs = cluster_attrs, .ct_owner = THIS_MODULE,};static struct config_item_type spaces_type = { .ct_group_ops = &spaces_ops, .ct_owner = THIS_MODULE,};static struct config_item_type space_type = { .ct_item_ops = &space_ops, .ct_owner = THIS_MODULE,};static struct config_item_type comms_type = { .ct_group_ops = &comms_ops, .ct_owner = THIS_MODULE,};static struct config_item_type comm_type = { .ct_item_ops = &comm_ops, .ct_attrs = comm_attrs, .ct_owner = THIS_MODULE,};static struct config_item_type nodes_type = { .ct_group_ops = &nodes_ops, .ct_owner = THIS_MODULE,};static struct config_item_type node_type = { .ct_item_ops = &node_ops, .ct_attrs = node_attrs, .ct_owner = THIS_MODULE,};static struct cluster *to_cluster(struct config_item *i){ return i ? container_of(to_config_group(i), struct cluster, group):NULL;}static struct space *to_space(struct config_item *i){ return i ? container_of(to_config_group(i), struct space, group) : NULL;}static struct comm *to_comm(struct config_item *i){ return i ? container_of(i, struct comm, item) : NULL;}static struct node *to_node(struct config_item *i){ return i ? container_of(i, struct node, item) : NULL;}static struct config_group *make_cluster(struct config_group *g, const char *name){ struct cluster *cl = NULL; struct spaces *sps = NULL; struct comms *cms = NULL; void *gps = NULL; cl = kzalloc(sizeof(struct cluster), GFP_KERNEL); gps = kcalloc(3, sizeof(struct config_group *), GFP_KERNEL); sps = kzalloc(sizeof(struct spaces), GFP_KERNEL); cms = kzalloc(sizeof(struct comms), GFP_KERNEL); if (!cl || !gps || !sps || !cms) goto fail; config_group_init_type_name(&cl->group, name, &cluster_type); config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type); config_group_init_type_name(&cms->cs_group, "comms", &comms_type); cl->group.default_groups = gps; cl->group.default_groups[0] = &sps->ss_group; cl->group.default_groups[1] = &cms->cs_group; cl->group.default_groups[2] = NULL; cl->cl_tcp_port = dlm_config.ci_tcp_port; cl->cl_buffer_size = dlm_config.ci_buffer_size; cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size; cl->cl_lkbtbl_size = dlm_config.ci_lkbtbl_size; cl->cl_dirtbl_size = dlm_config.ci_dirtbl_size; cl->cl_recover_timer = dlm_config.ci_recover_timer; cl->cl_toss_secs = dlm_config.ci_toss_secs; cl->cl_scan_secs = dlm_config.ci_scan_secs; cl->cl_log_debug = dlm_config.ci_log_debug; cl->cl_protocol = dlm_config.ci_protocol; cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs; space_list = &sps->ss_group; comm_list = &cms->cs_group; return &cl->group; fail: kfree(cl); kfree(gps); kfree(sps); kfree(cms); return NULL;}static void drop_cluster(struct config_group *g, struct config_item *i){ struct cluster *cl = to_cluster(i); struct config_item *tmp; int j; for (j = 0; cl->group.default_groups[j]; j++) { tmp = &cl->group.default_groups[j]->cg_item; cl->group.default_groups[j] = NULL; config_item_put(tmp); } space_list = NULL; comm_list = NULL; config_item_put(i);}static void release_cluster(struct config_item *i){ struct cluster *cl = to_cluster(i); kfree(cl->group.default_groups); kfree(cl);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -