Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003 *******************************************************************************
0004 **
0005 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
0006 **  Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
0007 **
0008 **
0009 *******************************************************************************
0010 ******************************************************************************/
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/init.h>
0014 #include <linux/configfs.h>
0015 #include <linux/slab.h>
0016 #include <linux/in.h>
0017 #include <linux/in6.h>
0018 #include <linux/dlmconstants.h>
0019 #include <net/ipv6.h>
0020 #include <net/sock.h>
0021 
0022 #include "config.h"
0023 #include "midcomms.h"
0024 #include "lowcomms.h"
0025 
0026 /*
0027  * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/nodeid
0028  * /config/dlm/<cluster>/spaces/<space>/nodes/<node>/weight
0029  * /config/dlm/<cluster>/comms/<comm>/nodeid
0030  * /config/dlm/<cluster>/comms/<comm>/local
0031  * /config/dlm/<cluster>/comms/<comm>/addr      (write only)
0032  * /config/dlm/<cluster>/comms/<comm>/addr_list (read only)
0033  * The <cluster> level is useless, but I haven't figured out how to avoid it.
0034  */
0035 
0036 static struct config_group *space_list;
0037 static struct config_group *comm_list;
0038 static struct dlm_comm *local_comm;
0039 static uint32_t dlm_comm_count;
0040 
0041 struct dlm_clusters;
0042 struct dlm_cluster;
0043 struct dlm_spaces;
0044 struct dlm_space;
0045 struct dlm_comms;
0046 struct dlm_comm;
0047 struct dlm_nodes;
0048 struct dlm_node;
0049 
0050 static struct config_group *make_cluster(struct config_group *, const char *);
0051 static void drop_cluster(struct config_group *, struct config_item *);
0052 static void release_cluster(struct config_item *);
0053 static struct config_group *make_space(struct config_group *, const char *);
0054 static void drop_space(struct config_group *, struct config_item *);
0055 static void release_space(struct config_item *);
0056 static struct config_item *make_comm(struct config_group *, const char *);
0057 static void drop_comm(struct config_group *, struct config_item *);
0058 static void release_comm(struct config_item *);
0059 static struct config_item *make_node(struct config_group *, const char *);
0060 static void drop_node(struct config_group *, struct config_item *);
0061 static void release_node(struct config_item *);
0062 
0063 static struct configfs_attribute *comm_attrs[];
0064 static struct configfs_attribute *node_attrs[];
0065 
0066 struct dlm_cluster {
0067     struct config_group group;
0068     unsigned int cl_tcp_port;
0069     unsigned int cl_buffer_size;
0070     unsigned int cl_rsbtbl_size;
0071     unsigned int cl_recover_timer;
0072     unsigned int cl_toss_secs;
0073     unsigned int cl_scan_secs;
0074     unsigned int cl_log_debug;
0075     unsigned int cl_log_info;
0076     unsigned int cl_protocol;
0077     unsigned int cl_mark;
0078 #ifdef CONFIG_DLM_DEPRECATED_API
0079     unsigned int cl_timewarn_cs;
0080 #endif
0081     unsigned int cl_new_rsb_count;
0082     unsigned int cl_recover_callbacks;
0083     char cl_cluster_name[DLM_LOCKSPACE_LEN];
0084 
0085     struct dlm_spaces *sps;
0086     struct dlm_comms *cms;
0087 };
0088 
0089 static struct dlm_cluster *config_item_to_cluster(struct config_item *i)
0090 {
0091     return i ? container_of(to_config_group(i), struct dlm_cluster, group) :
0092            NULL;
0093 }
0094 
0095 enum {
0096     CLUSTER_ATTR_TCP_PORT = 0,
0097     CLUSTER_ATTR_BUFFER_SIZE,
0098     CLUSTER_ATTR_RSBTBL_SIZE,
0099     CLUSTER_ATTR_RECOVER_TIMER,
0100     CLUSTER_ATTR_TOSS_SECS,
0101     CLUSTER_ATTR_SCAN_SECS,
0102     CLUSTER_ATTR_LOG_DEBUG,
0103     CLUSTER_ATTR_LOG_INFO,
0104     CLUSTER_ATTR_PROTOCOL,
0105     CLUSTER_ATTR_MARK,
0106 #ifdef CONFIG_DLM_DEPRECATED_API
0107     CLUSTER_ATTR_TIMEWARN_CS,
0108 #endif
0109     CLUSTER_ATTR_NEW_RSB_COUNT,
0110     CLUSTER_ATTR_RECOVER_CALLBACKS,
0111     CLUSTER_ATTR_CLUSTER_NAME,
0112 };
0113 
0114 static ssize_t cluster_cluster_name_show(struct config_item *item, char *buf)
0115 {
0116     struct dlm_cluster *cl = config_item_to_cluster(item);
0117     return sprintf(buf, "%s\n", cl->cl_cluster_name);
0118 }
0119 
0120 static ssize_t cluster_cluster_name_store(struct config_item *item,
0121                       const char *buf, size_t len)
0122 {
0123     struct dlm_cluster *cl = config_item_to_cluster(item);
0124 
0125     strlcpy(dlm_config.ci_cluster_name, buf,
0126                 sizeof(dlm_config.ci_cluster_name));
0127     strlcpy(cl->cl_cluster_name, buf, sizeof(cl->cl_cluster_name));
0128     return len;
0129 }
0130 
0131 CONFIGFS_ATTR(cluster_, cluster_name);
0132 
0133 static ssize_t cluster_set(struct dlm_cluster *cl, unsigned int *cl_field,
0134                int *info_field, int (*check_cb)(unsigned int x),
0135                const char *buf, size_t len)
0136 {
0137     unsigned int x;
0138     int rc;
0139 
0140     if (!capable(CAP_SYS_ADMIN))
0141         return -EPERM;
0142     rc = kstrtouint(buf, 0, &x);
0143     if (rc)
0144         return rc;
0145 
0146     if (check_cb) {
0147         rc = check_cb(x);
0148         if (rc)
0149             return rc;
0150     }
0151 
0152     *cl_field = x;
0153     *info_field = x;
0154 
0155     return len;
0156 }
0157 
0158 #define CLUSTER_ATTR(name, check_cb)                                          \
0159 static ssize_t cluster_##name##_store(struct config_item *item, \
0160         const char *buf, size_t len) \
0161 {                                                                             \
0162     struct dlm_cluster *cl = config_item_to_cluster(item);            \
0163     return cluster_set(cl, &cl->cl_##name, &dlm_config.ci_##name,         \
0164                check_cb, buf, len);                               \
0165 }                                                                             \
0166 static ssize_t cluster_##name##_show(struct config_item *item, char *buf)     \
0167 {                                                                             \
0168     struct dlm_cluster *cl = config_item_to_cluster(item);            \
0169     return snprintf(buf, PAGE_SIZE, "%u\n", cl->cl_##name);               \
0170 }                                                                             \
0171 CONFIGFS_ATTR(cluster_, name);
0172 
0173 static int dlm_check_protocol_and_dlm_running(unsigned int x)
0174 {
0175     switch (x) {
0176     case 0:
0177         /* TCP */
0178         break;
0179     case 1:
0180         /* SCTP */
0181         break;
0182     default:
0183         return -EINVAL;
0184     }
0185 
0186     if (dlm_allow_conn)
0187         return -EBUSY;
0188 
0189     return 0;
0190 }
0191 
0192 static int dlm_check_zero_and_dlm_running(unsigned int x)
0193 {
0194     if (!x)
0195         return -EINVAL;
0196 
0197     if (dlm_allow_conn)
0198         return -EBUSY;
0199 
0200     return 0;
0201 }
0202 
0203 static int dlm_check_zero(unsigned int x)
0204 {
0205     if (!x)
0206         return -EINVAL;
0207 
0208     return 0;
0209 }
0210 
0211 static int dlm_check_buffer_size(unsigned int x)
0212 {
0213     if (x < DLM_MAX_SOCKET_BUFSIZE)
0214         return -EINVAL;
0215 
0216     return 0;
0217 }
0218 
0219 CLUSTER_ATTR(tcp_port, dlm_check_zero_and_dlm_running);
0220 CLUSTER_ATTR(buffer_size, dlm_check_buffer_size);
0221 CLUSTER_ATTR(rsbtbl_size, dlm_check_zero);
0222 CLUSTER_ATTR(recover_timer, dlm_check_zero);
0223 CLUSTER_ATTR(toss_secs, dlm_check_zero);
0224 CLUSTER_ATTR(scan_secs, dlm_check_zero);
0225 CLUSTER_ATTR(log_debug, NULL);
0226 CLUSTER_ATTR(log_info, NULL);
0227 CLUSTER_ATTR(protocol, dlm_check_protocol_and_dlm_running);
0228 CLUSTER_ATTR(mark, NULL);
0229 #ifdef CONFIG_DLM_DEPRECATED_API
0230 CLUSTER_ATTR(timewarn_cs, dlm_check_zero);
0231 #endif
0232 CLUSTER_ATTR(new_rsb_count, NULL);
0233 CLUSTER_ATTR(recover_callbacks, NULL);
0234 
0235 static struct configfs_attribute *cluster_attrs[] = {
0236     [CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port,
0237     [CLUSTER_ATTR_BUFFER_SIZE] = &cluster_attr_buffer_size,
0238     [CLUSTER_ATTR_RSBTBL_SIZE] = &cluster_attr_rsbtbl_size,
0239     [CLUSTER_ATTR_RECOVER_TIMER] = &cluster_attr_recover_timer,
0240     [CLUSTER_ATTR_TOSS_SECS] = &cluster_attr_toss_secs,
0241     [CLUSTER_ATTR_SCAN_SECS] = &cluster_attr_scan_secs,
0242     [CLUSTER_ATTR_LOG_DEBUG] = &cluster_attr_log_debug,
0243     [CLUSTER_ATTR_LOG_INFO] = &cluster_attr_log_info,
0244     [CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol,
0245     [CLUSTER_ATTR_MARK] = &cluster_attr_mark,
0246 #ifdef CONFIG_DLM_DEPRECATED_API
0247     [CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs,
0248 #endif
0249     [CLUSTER_ATTR_NEW_RSB_COUNT] = &cluster_attr_new_rsb_count,
0250     [CLUSTER_ATTR_RECOVER_CALLBACKS] = &cluster_attr_recover_callbacks,
0251     [CLUSTER_ATTR_CLUSTER_NAME] = &cluster_attr_cluster_name,
0252     NULL,
0253 };
0254 
0255 enum {
0256     COMM_ATTR_NODEID = 0,
0257     COMM_ATTR_LOCAL,
0258     COMM_ATTR_ADDR,
0259     COMM_ATTR_ADDR_LIST,
0260     COMM_ATTR_MARK,
0261 };
0262 
0263 enum {
0264     NODE_ATTR_NODEID = 0,
0265     NODE_ATTR_WEIGHT,
0266 };
0267 
0268 struct dlm_clusters {
0269     struct configfs_subsystem subsys;
0270 };
0271 
0272 struct dlm_spaces {
0273     struct config_group ss_group;
0274 };
0275 
0276 struct dlm_space {
0277     struct config_group group;
0278     struct list_head members;
0279     struct mutex members_lock;
0280     int members_count;
0281     struct dlm_nodes *nds;
0282 };
0283 
0284 struct dlm_comms {
0285     struct config_group cs_group;
0286 };
0287 
0288 struct dlm_comm {
0289     struct config_item item;
0290     int seq;
0291     int nodeid;
0292     int local;
0293     int addr_count;
0294     unsigned int mark;
0295     struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
0296 };
0297 
0298 struct dlm_nodes {
0299     struct config_group ns_group;
0300 };
0301 
0302 struct dlm_node {
0303     struct config_item item;
0304     struct list_head list; /* space->members */
0305     int nodeid;
0306     int weight;
0307     int new;
0308     int comm_seq; /* copy of cm->seq when nd->nodeid is set */
0309 };
0310 
0311 static struct configfs_group_operations clusters_ops = {
0312     .make_group = make_cluster,
0313     .drop_item = drop_cluster,
0314 };
0315 
0316 static struct configfs_item_operations cluster_ops = {
0317     .release = release_cluster,
0318 };
0319 
0320 static struct configfs_group_operations spaces_ops = {
0321     .make_group = make_space,
0322     .drop_item = drop_space,
0323 };
0324 
0325 static struct configfs_item_operations space_ops = {
0326     .release = release_space,
0327 };
0328 
0329 static struct configfs_group_operations comms_ops = {
0330     .make_item = make_comm,
0331     .drop_item = drop_comm,
0332 };
0333 
0334 static struct configfs_item_operations comm_ops = {
0335     .release = release_comm,
0336 };
0337 
0338 static struct configfs_group_operations nodes_ops = {
0339     .make_item = make_node,
0340     .drop_item = drop_node,
0341 };
0342 
0343 static struct configfs_item_operations node_ops = {
0344     .release = release_node,
0345 };
0346 
0347 static const struct config_item_type clusters_type = {
0348     .ct_group_ops = &clusters_ops,
0349     .ct_owner = THIS_MODULE,
0350 };
0351 
0352 static const struct config_item_type cluster_type = {
0353     .ct_item_ops = &cluster_ops,
0354     .ct_attrs = cluster_attrs,
0355     .ct_owner = THIS_MODULE,
0356 };
0357 
0358 static const struct config_item_type spaces_type = {
0359     .ct_group_ops = &spaces_ops,
0360     .ct_owner = THIS_MODULE,
0361 };
0362 
0363 static const struct config_item_type space_type = {
0364     .ct_item_ops = &space_ops,
0365     .ct_owner = THIS_MODULE,
0366 };
0367 
0368 static const struct config_item_type comms_type = {
0369     .ct_group_ops = &comms_ops,
0370     .ct_owner = THIS_MODULE,
0371 };
0372 
0373 static const struct config_item_type comm_type = {
0374     .ct_item_ops = &comm_ops,
0375     .ct_attrs = comm_attrs,
0376     .ct_owner = THIS_MODULE,
0377 };
0378 
0379 static const struct config_item_type nodes_type = {
0380     .ct_group_ops = &nodes_ops,
0381     .ct_owner = THIS_MODULE,
0382 };
0383 
0384 static const struct config_item_type node_type = {
0385     .ct_item_ops = &node_ops,
0386     .ct_attrs = node_attrs,
0387     .ct_owner = THIS_MODULE,
0388 };
0389 
0390 static struct dlm_space *config_item_to_space(struct config_item *i)
0391 {
0392     return i ? container_of(to_config_group(i), struct dlm_space, group) :
0393            NULL;
0394 }
0395 
0396 static struct dlm_comm *config_item_to_comm(struct config_item *i)
0397 {
0398     return i ? container_of(i, struct dlm_comm, item) : NULL;
0399 }
0400 
0401 static struct dlm_node *config_item_to_node(struct config_item *i)
0402 {
0403     return i ? container_of(i, struct dlm_node, item) : NULL;
0404 }
0405 
0406 static struct config_group *make_cluster(struct config_group *g,
0407                      const char *name)
0408 {
0409     struct dlm_cluster *cl = NULL;
0410     struct dlm_spaces *sps = NULL;
0411     struct dlm_comms *cms = NULL;
0412 
0413     cl = kzalloc(sizeof(struct dlm_cluster), GFP_NOFS);
0414     sps = kzalloc(sizeof(struct dlm_spaces), GFP_NOFS);
0415     cms = kzalloc(sizeof(struct dlm_comms), GFP_NOFS);
0416 
0417     if (!cl || !sps || !cms)
0418         goto fail;
0419 
0420     cl->sps = sps;
0421     cl->cms = cms;
0422 
0423     config_group_init_type_name(&cl->group, name, &cluster_type);
0424     config_group_init_type_name(&sps->ss_group, "spaces", &spaces_type);
0425     config_group_init_type_name(&cms->cs_group, "comms", &comms_type);
0426 
0427     configfs_add_default_group(&sps->ss_group, &cl->group);
0428     configfs_add_default_group(&cms->cs_group, &cl->group);
0429 
0430     cl->cl_tcp_port = dlm_config.ci_tcp_port;
0431     cl->cl_buffer_size = dlm_config.ci_buffer_size;
0432     cl->cl_rsbtbl_size = dlm_config.ci_rsbtbl_size;
0433     cl->cl_recover_timer = dlm_config.ci_recover_timer;
0434     cl->cl_toss_secs = dlm_config.ci_toss_secs;
0435     cl->cl_scan_secs = dlm_config.ci_scan_secs;
0436     cl->cl_log_debug = dlm_config.ci_log_debug;
0437     cl->cl_log_info = dlm_config.ci_log_info;
0438     cl->cl_protocol = dlm_config.ci_protocol;
0439 #ifdef CONFIG_DLM_DEPRECATED_API
0440     cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
0441 #endif
0442     cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
0443     cl->cl_recover_callbacks = dlm_config.ci_recover_callbacks;
0444     memcpy(cl->cl_cluster_name, dlm_config.ci_cluster_name,
0445            DLM_LOCKSPACE_LEN);
0446 
0447     space_list = &sps->ss_group;
0448     comm_list = &cms->cs_group;
0449     return &cl->group;
0450 
0451  fail:
0452     kfree(cl);
0453     kfree(sps);
0454     kfree(cms);
0455     return ERR_PTR(-ENOMEM);
0456 }
0457 
0458 static void drop_cluster(struct config_group *g, struct config_item *i)
0459 {
0460     struct dlm_cluster *cl = config_item_to_cluster(i);
0461 
0462     configfs_remove_default_groups(&cl->group);
0463 
0464     space_list = NULL;
0465     comm_list = NULL;
0466 
0467     config_item_put(i);
0468 }
0469 
0470 static void release_cluster(struct config_item *i)
0471 {
0472     struct dlm_cluster *cl = config_item_to_cluster(i);
0473 
0474     kfree(cl->sps);
0475     kfree(cl->cms);
0476     kfree(cl);
0477 }
0478 
0479 static struct config_group *make_space(struct config_group *g, const char *name)
0480 {
0481     struct dlm_space *sp = NULL;
0482     struct dlm_nodes *nds = NULL;
0483 
0484     sp = kzalloc(sizeof(struct dlm_space), GFP_NOFS);
0485     nds = kzalloc(sizeof(struct dlm_nodes), GFP_NOFS);
0486 
0487     if (!sp || !nds)
0488         goto fail;
0489 
0490     config_group_init_type_name(&sp->group, name, &space_type);
0491 
0492     config_group_init_type_name(&nds->ns_group, "nodes", &nodes_type);
0493     configfs_add_default_group(&nds->ns_group, &sp->group);
0494 
0495     INIT_LIST_HEAD(&sp->members);
0496     mutex_init(&sp->members_lock);
0497     sp->members_count = 0;
0498     sp->nds = nds;
0499     return &sp->group;
0500 
0501  fail:
0502     kfree(sp);
0503     kfree(nds);
0504     return ERR_PTR(-ENOMEM);
0505 }
0506 
0507 static void drop_space(struct config_group *g, struct config_item *i)
0508 {
0509     struct dlm_space *sp = config_item_to_space(i);
0510 
0511     /* assert list_empty(&sp->members) */
0512 
0513     configfs_remove_default_groups(&sp->group);
0514     config_item_put(i);
0515 }
0516 
0517 static void release_space(struct config_item *i)
0518 {
0519     struct dlm_space *sp = config_item_to_space(i);
0520     kfree(sp->nds);
0521     kfree(sp);
0522 }
0523 
0524 static struct config_item *make_comm(struct config_group *g, const char *name)
0525 {
0526     struct dlm_comm *cm;
0527 
0528     cm = kzalloc(sizeof(struct dlm_comm), GFP_NOFS);
0529     if (!cm)
0530         return ERR_PTR(-ENOMEM);
0531 
0532     config_item_init_type_name(&cm->item, name, &comm_type);
0533 
0534     cm->seq = dlm_comm_count++;
0535     if (!cm->seq)
0536         cm->seq = dlm_comm_count++;
0537 
0538     cm->nodeid = -1;
0539     cm->local = 0;
0540     cm->addr_count = 0;
0541     cm->mark = 0;
0542     return &cm->item;
0543 }
0544 
0545 static void drop_comm(struct config_group *g, struct config_item *i)
0546 {
0547     struct dlm_comm *cm = config_item_to_comm(i);
0548     if (local_comm == cm)
0549         local_comm = NULL;
0550     dlm_midcomms_close(cm->nodeid);
0551     while (cm->addr_count--)
0552         kfree(cm->addr[cm->addr_count]);
0553     config_item_put(i);
0554 }
0555 
0556 static void release_comm(struct config_item *i)
0557 {
0558     struct dlm_comm *cm = config_item_to_comm(i);
0559     kfree(cm);
0560 }
0561 
0562 static struct config_item *make_node(struct config_group *g, const char *name)
0563 {
0564     struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
0565     struct dlm_node *nd;
0566 
0567     nd = kzalloc(sizeof(struct dlm_node), GFP_NOFS);
0568     if (!nd)
0569         return ERR_PTR(-ENOMEM);
0570 
0571     config_item_init_type_name(&nd->item, name, &node_type);
0572     nd->nodeid = -1;
0573     nd->weight = 1;  /* default weight of 1 if none is set */
0574     nd->new = 1;     /* set to 0 once it's been read by dlm_nodeid_list() */
0575 
0576     mutex_lock(&sp->members_lock);
0577     list_add(&nd->list, &sp->members);
0578     sp->members_count++;
0579     mutex_unlock(&sp->members_lock);
0580 
0581     return &nd->item;
0582 }
0583 
0584 static void drop_node(struct config_group *g, struct config_item *i)
0585 {
0586     struct dlm_space *sp = config_item_to_space(g->cg_item.ci_parent);
0587     struct dlm_node *nd = config_item_to_node(i);
0588 
0589     mutex_lock(&sp->members_lock);
0590     list_del(&nd->list);
0591     sp->members_count--;
0592     mutex_unlock(&sp->members_lock);
0593 
0594     config_item_put(i);
0595 }
0596 
0597 static void release_node(struct config_item *i)
0598 {
0599     struct dlm_node *nd = config_item_to_node(i);
0600     kfree(nd);
0601 }
0602 
0603 static struct dlm_clusters clusters_root = {
0604     .subsys = {
0605         .su_group = {
0606             .cg_item = {
0607                 .ci_namebuf = "dlm",
0608                 .ci_type = &clusters_type,
0609             },
0610         },
0611     },
0612 };
0613 
0614 int __init dlm_config_init(void)
0615 {
0616     config_group_init(&clusters_root.subsys.su_group);
0617     mutex_init(&clusters_root.subsys.su_mutex);
0618     return configfs_register_subsystem(&clusters_root.subsys);
0619 }
0620 
0621 void dlm_config_exit(void)
0622 {
0623     configfs_unregister_subsystem(&clusters_root.subsys);
0624 }
0625 
0626 /*
0627  * Functions for user space to read/write attributes
0628  */
0629 
0630 static ssize_t comm_nodeid_show(struct config_item *item, char *buf)
0631 {
0632     return sprintf(buf, "%d\n", config_item_to_comm(item)->nodeid);
0633 }
0634 
0635 static ssize_t comm_nodeid_store(struct config_item *item, const char *buf,
0636                  size_t len)
0637 {
0638     int rc = kstrtoint(buf, 0, &config_item_to_comm(item)->nodeid);
0639 
0640     if (rc)
0641         return rc;
0642     return len;
0643 }
0644 
0645 static ssize_t comm_local_show(struct config_item *item, char *buf)
0646 {
0647     return sprintf(buf, "%d\n", config_item_to_comm(item)->local);
0648 }
0649 
0650 static ssize_t comm_local_store(struct config_item *item, const char *buf,
0651                 size_t len)
0652 {
0653     struct dlm_comm *cm = config_item_to_comm(item);
0654     int rc = kstrtoint(buf, 0, &cm->local);
0655 
0656     if (rc)
0657         return rc;
0658     if (cm->local && !local_comm)
0659         local_comm = cm;
0660     return len;
0661 }
0662 
0663 static ssize_t comm_addr_store(struct config_item *item, const char *buf,
0664         size_t len)
0665 {
0666     struct dlm_comm *cm = config_item_to_comm(item);
0667     struct sockaddr_storage *addr;
0668     int rv;
0669 
0670     if (len != sizeof(struct sockaddr_storage))
0671         return -EINVAL;
0672 
0673     if (cm->addr_count >= DLM_MAX_ADDR_COUNT)
0674         return -ENOSPC;
0675 
0676     addr = kzalloc(sizeof(*addr), GFP_NOFS);
0677     if (!addr)
0678         return -ENOMEM;
0679 
0680     memcpy(addr, buf, len);
0681 
0682     rv = dlm_lowcomms_addr(cm->nodeid, addr, len);
0683     if (rv) {
0684         kfree(addr);
0685         return rv;
0686     }
0687 
0688     cm->addr[cm->addr_count++] = addr;
0689     return len;
0690 }
0691 
0692 static ssize_t comm_addr_list_show(struct config_item *item, char *buf)
0693 {
0694     struct dlm_comm *cm = config_item_to_comm(item);
0695     ssize_t s;
0696     ssize_t allowance;
0697     int i;
0698     struct sockaddr_storage *addr;
0699     struct sockaddr_in *addr_in;
0700     struct sockaddr_in6 *addr_in6;
0701     
0702     /* Taken from ip6_addr_string() defined in lib/vsprintf.c */
0703     char buf0[sizeof("AF_INET6  xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255\n")];
0704     
0705 
0706     /* Derived from SIMPLE_ATTR_SIZE of fs/configfs/file.c */
0707     allowance = 4096;
0708     buf[0] = '\0';
0709 
0710     for (i = 0; i < cm->addr_count; i++) {
0711         addr = cm->addr[i];
0712 
0713         switch(addr->ss_family) {
0714         case AF_INET:
0715             addr_in = (struct sockaddr_in *)addr;
0716             s = sprintf(buf0, "AF_INET  %pI4\n", &addr_in->sin_addr.s_addr);
0717             break;
0718         case AF_INET6:
0719             addr_in6 = (struct sockaddr_in6 *)addr;
0720             s = sprintf(buf0, "AF_INET6 %pI6\n", &addr_in6->sin6_addr);
0721             break;
0722         default:
0723             s = sprintf(buf0, "%s\n", "<UNKNOWN>");
0724             break;
0725         }
0726         allowance -= s;
0727         if (allowance >= 0)
0728             strcat(buf, buf0);
0729         else {
0730             allowance += s;
0731             break;
0732         }
0733     }
0734     return 4096 - allowance;
0735 }
0736 
0737 static ssize_t comm_mark_show(struct config_item *item, char *buf)
0738 {
0739     return sprintf(buf, "%u\n", config_item_to_comm(item)->mark);
0740 }
0741 
0742 static ssize_t comm_mark_store(struct config_item *item, const char *buf,
0743                    size_t len)
0744 {
0745     struct dlm_comm *comm;
0746     unsigned int mark;
0747     int rc;
0748 
0749     rc = kstrtouint(buf, 0, &mark);
0750     if (rc)
0751         return rc;
0752 
0753     if (mark == 0)
0754         mark = dlm_config.ci_mark;
0755 
0756     comm = config_item_to_comm(item);
0757     rc = dlm_lowcomms_nodes_set_mark(comm->nodeid, mark);
0758     if (rc)
0759         return rc;
0760 
0761     comm->mark = mark;
0762     return len;
0763 }
0764 
0765 CONFIGFS_ATTR(comm_, nodeid);
0766 CONFIGFS_ATTR(comm_, local);
0767 CONFIGFS_ATTR(comm_, mark);
0768 CONFIGFS_ATTR_WO(comm_, addr);
0769 CONFIGFS_ATTR_RO(comm_, addr_list);
0770 
0771 static struct configfs_attribute *comm_attrs[] = {
0772     [COMM_ATTR_NODEID] = &comm_attr_nodeid,
0773     [COMM_ATTR_LOCAL] = &comm_attr_local,
0774     [COMM_ATTR_ADDR] = &comm_attr_addr,
0775     [COMM_ATTR_ADDR_LIST] = &comm_attr_addr_list,
0776     [COMM_ATTR_MARK] = &comm_attr_mark,
0777     NULL,
0778 };
0779 
0780 static ssize_t node_nodeid_show(struct config_item *item, char *buf)
0781 {
0782     return sprintf(buf, "%d\n", config_item_to_node(item)->nodeid);
0783 }
0784 
0785 static ssize_t node_nodeid_store(struct config_item *item, const char *buf,
0786                  size_t len)
0787 {
0788     struct dlm_node *nd = config_item_to_node(item);
0789     uint32_t seq = 0;
0790     int rc = kstrtoint(buf, 0, &nd->nodeid);
0791 
0792     if (rc)
0793         return rc;
0794     dlm_comm_seq(nd->nodeid, &seq);
0795     nd->comm_seq = seq;
0796     return len;
0797 }
0798 
0799 static ssize_t node_weight_show(struct config_item *item, char *buf)
0800 {
0801     return sprintf(buf, "%d\n", config_item_to_node(item)->weight);
0802 }
0803 
0804 static ssize_t node_weight_store(struct config_item *item, const char *buf,
0805                  size_t len)
0806 {
0807     int rc = kstrtoint(buf, 0, &config_item_to_node(item)->weight);
0808 
0809     if (rc)
0810         return rc;
0811     return len;
0812 }
0813 
0814 CONFIGFS_ATTR(node_, nodeid);
0815 CONFIGFS_ATTR(node_, weight);
0816 
0817 static struct configfs_attribute *node_attrs[] = {
0818     [NODE_ATTR_NODEID] = &node_attr_nodeid,
0819     [NODE_ATTR_WEIGHT] = &node_attr_weight,
0820     NULL,
0821 };
0822 
0823 /*
0824  * Functions for the dlm to get the info that's been configured
0825  */
0826 
0827 static struct dlm_space *get_space(char *name)
0828 {
0829     struct config_item *i;
0830 
0831     if (!space_list)
0832         return NULL;
0833 
0834     mutex_lock(&space_list->cg_subsys->su_mutex);
0835     i = config_group_find_item(space_list, name);
0836     mutex_unlock(&space_list->cg_subsys->su_mutex);
0837 
0838     return config_item_to_space(i);
0839 }
0840 
0841 static void put_space(struct dlm_space *sp)
0842 {
0843     config_item_put(&sp->group.cg_item);
0844 }
0845 
0846 static struct dlm_comm *get_comm(int nodeid)
0847 {
0848     struct config_item *i;
0849     struct dlm_comm *cm = NULL;
0850     int found = 0;
0851 
0852     if (!comm_list)
0853         return NULL;
0854 
0855     mutex_lock(&clusters_root.subsys.su_mutex);
0856 
0857     list_for_each_entry(i, &comm_list->cg_children, ci_entry) {
0858         cm = config_item_to_comm(i);
0859 
0860         if (cm->nodeid != nodeid)
0861             continue;
0862         found = 1;
0863         config_item_get(i);
0864         break;
0865     }
0866     mutex_unlock(&clusters_root.subsys.su_mutex);
0867 
0868     if (!found)
0869         cm = NULL;
0870     return cm;
0871 }
0872 
0873 static void put_comm(struct dlm_comm *cm)
0874 {
0875     config_item_put(&cm->item);
0876 }
0877 
0878 /* caller must free mem */
0879 int dlm_config_nodes(char *lsname, struct dlm_config_node **nodes_out,
0880              int *count_out)
0881 {
0882     struct dlm_space *sp;
0883     struct dlm_node *nd;
0884     struct dlm_config_node *nodes, *node;
0885     int rv, count;
0886 
0887     sp = get_space(lsname);
0888     if (!sp)
0889         return -EEXIST;
0890 
0891     mutex_lock(&sp->members_lock);
0892     if (!sp->members_count) {
0893         rv = -EINVAL;
0894         printk(KERN_ERR "dlm: zero members_count\n");
0895         goto out;
0896     }
0897 
0898     count = sp->members_count;
0899 
0900     nodes = kcalloc(count, sizeof(struct dlm_config_node), GFP_NOFS);
0901     if (!nodes) {
0902         rv = -ENOMEM;
0903         goto out;
0904     }
0905 
0906     node = nodes;
0907     list_for_each_entry(nd, &sp->members, list) {
0908         node->nodeid = nd->nodeid;
0909         node->weight = nd->weight;
0910         node->new = nd->new;
0911         node->comm_seq = nd->comm_seq;
0912         node++;
0913 
0914         nd->new = 0;
0915     }
0916 
0917     *count_out = count;
0918     *nodes_out = nodes;
0919     rv = 0;
0920  out:
0921     mutex_unlock(&sp->members_lock);
0922     put_space(sp);
0923     return rv;
0924 }
0925 
0926 int dlm_comm_seq(int nodeid, uint32_t *seq)
0927 {
0928     struct dlm_comm *cm = get_comm(nodeid);
0929     if (!cm)
0930         return -EEXIST;
0931     *seq = cm->seq;
0932     put_comm(cm);
0933     return 0;
0934 }
0935 
0936 int dlm_our_nodeid(void)
0937 {
0938     return local_comm ? local_comm->nodeid : 0;
0939 }
0940 
0941 /* num 0 is first addr, num 1 is second addr */
0942 int dlm_our_addr(struct sockaddr_storage *addr, int num)
0943 {
0944     if (!local_comm)
0945         return -1;
0946     if (num + 1 > local_comm->addr_count)
0947         return -1;
0948     memcpy(addr, local_comm->addr[num], sizeof(*addr));
0949     return 0;
0950 }
0951 
0952 /* Config file defaults */
0953 #define DEFAULT_TCP_PORT       21064
0954 #define DEFAULT_RSBTBL_SIZE     1024
0955 #define DEFAULT_RECOVER_TIMER      5
0956 #define DEFAULT_TOSS_SECS         10
0957 #define DEFAULT_SCAN_SECS          5
0958 #define DEFAULT_LOG_DEBUG          0
0959 #define DEFAULT_LOG_INFO           1
0960 #define DEFAULT_PROTOCOL           DLM_PROTO_TCP
0961 #define DEFAULT_MARK               0
0962 #ifdef CONFIG_DLM_DEPRECATED_API
0963 #define DEFAULT_TIMEWARN_CS      500 /* 5 sec = 500 centiseconds */
0964 #endif
0965 #define DEFAULT_NEW_RSB_COUNT    128
0966 #define DEFAULT_RECOVER_CALLBACKS  0
0967 #define DEFAULT_CLUSTER_NAME      ""
0968 
0969 struct dlm_config_info dlm_config = {
0970     .ci_tcp_port = DEFAULT_TCP_PORT,
0971     .ci_buffer_size = DLM_MAX_SOCKET_BUFSIZE,
0972     .ci_rsbtbl_size = DEFAULT_RSBTBL_SIZE,
0973     .ci_recover_timer = DEFAULT_RECOVER_TIMER,
0974     .ci_toss_secs = DEFAULT_TOSS_SECS,
0975     .ci_scan_secs = DEFAULT_SCAN_SECS,
0976     .ci_log_debug = DEFAULT_LOG_DEBUG,
0977     .ci_log_info = DEFAULT_LOG_INFO,
0978     .ci_protocol = DEFAULT_PROTOCOL,
0979     .ci_mark = DEFAULT_MARK,
0980 #ifdef CONFIG_DLM_DEPRECATED_API
0981     .ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
0982 #endif
0983     .ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT,
0984     .ci_recover_callbacks = DEFAULT_RECOVER_CALLBACKS,
0985     .ci_cluster_name = DEFAULT_CLUSTER_NAME
0986 };
0987