2 * net/tipc/name_table.c: TIPC name table code
4 * Copyright (c) 2000-2006, 2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2014, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
39 #include "name_table.h"
40 #include "name_distr.h"
43 #define TIPC_NAMETBL_SIZE 1024 /* must be a power of 2 */
45 static const struct nla_policy
46 tipc_nl_name_table_policy[TIPC_NLA_NAME_TABLE_MAX + 1] = {
47 [TIPC_NLA_NAME_TABLE_UNSPEC] = { .type = NLA_UNSPEC },
48 [TIPC_NLA_NAME_TABLE_PUBL] = { .type = NLA_NESTED }
52 * struct name_info - name sequence publication info
53 * @node_list: circular list of publications made by own node
54 * @cluster_list: circular list of publications made by own cluster
55 * @zone_list: circular list of publications made by own zone
56 * @node_list_size: number of entries in "node_list"
57 * @cluster_list_size: number of entries in "cluster_list"
58 * @zone_list_size: number of entries in "zone_list"
60 * Note: The zone list always contains at least one entry, since all
61 * publications of the associated name sequence belong to it.
62 * (The cluster and node lists may be empty.)
65 struct list_head node_list;
66 struct list_head cluster_list;
67 struct list_head zone_list;
69 u32 cluster_list_size;
74 * struct sub_seq - container for all published instances of a name sequence
75 * @lower: name sequence lower bound
76 * @upper: name sequence upper bound
77 * @info: pointer to name sequence publication info
82 struct name_info *info;
86 * struct name_seq - container for all published instances of a name type
87 * @type: 32 bit 'type' value for name sequence
88 * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
89 * sub-sequences are sorted in ascending order
90 * @alloc: number of sub-sequences currently in array
91 * @first_free: array index of first unused sub-sequence entry
92 * @ns_list: links to adjacent name sequences in hash chain
93 * @subscriptions: list of subscriptions for this 'type'
94 * @lock: spinlock controlling access to publication lists of all sub-sequences
98 struct sub_seq *sseqs;
101 struct hlist_node ns_list;
102 struct list_head subscriptions;
106 struct name_table *tipc_nametbl;
107 DEFINE_RWLOCK(tipc_nametbl_lock);
109 static int hash(int x)
111 return x & (TIPC_NAMETBL_SIZE - 1);
115 * publ_create - create a publication structure
117 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
118 u32 scope, u32 node, u32 port_ref,
121 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
123 pr_warn("Publication creation failure, no memory\n");
132 publ->ref = port_ref;
134 INIT_LIST_HEAD(&publ->local_list);
135 INIT_LIST_HEAD(&publ->pport_list);
136 INIT_LIST_HEAD(&publ->nodesub_list);
141 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
143 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
145 return kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
149 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
151 * Allocates a single sub-sequence structure and sets it to all 0's.
153 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
155 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
156 struct sub_seq *sseq = tipc_subseq_alloc(1);
158 if (!nseq || !sseq) {
159 pr_warn("Name sequence creation failed, no memory\n");
165 spin_lock_init(&nseq->lock);
169 INIT_HLIST_NODE(&nseq->ns_list);
170 INIT_LIST_HEAD(&nseq->subscriptions);
171 hlist_add_head(&nseq->ns_list, seq_head);
176 * nameseq_delete_empty - deletes a name sequence structure if now unused
178 static void nameseq_delete_empty(struct name_seq *seq)
180 if (!seq->first_free && list_empty(&seq->subscriptions)) {
181 hlist_del_init(&seq->ns_list);
188 * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
190 * Very time-critical, so binary searches through sub-sequence array.
192 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
195 struct sub_seq *sseqs = nseq->sseqs;
197 int high = nseq->first_free - 1;
200 while (low <= high) {
201 mid = (low + high) / 2;
202 if (instance < sseqs[mid].lower)
204 else if (instance > sseqs[mid].upper)
213 * nameseq_locate_subseq - determine position of name instance in sub-sequence
215 * Returns index in sub-sequence array of the entry that contains the specified
216 * instance value; if no entry contains that value, returns the position
217 * where a new entry for it would be inserted in the array.
219 * Note: Similar to binary search code for locating a sub-sequence.
221 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
223 struct sub_seq *sseqs = nseq->sseqs;
225 int high = nseq->first_free - 1;
228 while (low <= high) {
229 mid = (low + high) / 2;
230 if (instance < sseqs[mid].lower)
232 else if (instance > sseqs[mid].upper)
241 * tipc_nameseq_insert_publ
243 static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
244 u32 type, u32 lower, u32 upper,
245 u32 scope, u32 node, u32 port, u32 key)
247 struct tipc_subscription *s;
248 struct tipc_subscription *st;
249 struct publication *publ;
250 struct sub_seq *sseq;
251 struct name_info *info;
252 int created_subseq = 0;
254 sseq = nameseq_find_subseq(nseq, lower);
257 /* Lower end overlaps existing entry => need an exact match */
258 if ((sseq->lower != lower) || (sseq->upper != upper)) {
264 /* Check if an identical publication already exists */
265 list_for_each_entry(publ, &info->zone_list, zone_list) {
266 if ((publ->ref == port) && (publ->key == key) &&
267 (!publ->node || (publ->node == node)))
272 struct sub_seq *freesseq;
274 /* Find where lower end should be inserted */
275 inspos = nameseq_locate_subseq(nseq, lower);
277 /* Fail if upper end overlaps into an existing entry */
278 if ((inspos < nseq->first_free) &&
279 (upper >= nseq->sseqs[inspos].lower)) {
283 /* Ensure there is space for new sub-sequence */
284 if (nseq->first_free == nseq->alloc) {
285 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
288 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
292 memcpy(sseqs, nseq->sseqs,
293 nseq->alloc * sizeof(struct sub_seq));
299 info = kzalloc(sizeof(*info), GFP_ATOMIC);
301 pr_warn("Cannot publish {%u,%u,%u}, no memory\n",
306 INIT_LIST_HEAD(&info->node_list);
307 INIT_LIST_HEAD(&info->cluster_list);
308 INIT_LIST_HEAD(&info->zone_list);
310 /* Insert new sub-sequence */
311 sseq = &nseq->sseqs[inspos];
312 freesseq = &nseq->sseqs[nseq->first_free];
313 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
314 memset(sseq, 0, sizeof(*sseq));
322 /* Insert a publication */
323 publ = publ_create(type, lower, upper, scope, node, port, key);
327 list_add(&publ->zone_list, &info->zone_list);
328 info->zone_list_size++;
330 if (in_own_cluster(node)) {
331 list_add(&publ->cluster_list, &info->cluster_list);
332 info->cluster_list_size++;
335 if (in_own_node(node)) {
336 list_add(&publ->node_list, &info->node_list);
337 info->node_list_size++;
340 /* Any subscriptions waiting for notification? */
341 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
342 tipc_subscr_report_overlap(s,
354 * tipc_nameseq_remove_publ
356 * NOTE: There may be cases where TIPC is asked to remove a publication
357 * that is not in the name table. For example, if another node issues a
358 * publication for a name sequence that overlaps an existing name sequence
359 * the publication will not be recorded, which means the publication won't
360 * be found when the name sequence is later withdrawn by that node.
361 * A failed withdraw request simply returns a failure indication and lets the
362 * caller issue any error or warning messages associated with such a problem.
364 static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
365 u32 node, u32 ref, u32 key)
367 struct publication *publ;
368 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
369 struct name_info *info;
370 struct sub_seq *free;
371 struct tipc_subscription *s, *st;
372 int removed_subseq = 0;
379 /* Locate publication, if it exists */
380 list_for_each_entry(publ, &info->zone_list, zone_list) {
381 if ((publ->key == key) && (publ->ref == ref) &&
382 (!publ->node || (publ->node == node)))
388 /* Remove publication from zone scope list */
389 list_del(&publ->zone_list);
390 info->zone_list_size--;
392 /* Remove publication from cluster scope list, if present */
393 if (in_own_cluster(node)) {
394 list_del(&publ->cluster_list);
395 info->cluster_list_size--;
398 /* Remove publication from node scope list, if present */
399 if (in_own_node(node)) {
400 list_del(&publ->node_list);
401 info->node_list_size--;
404 /* Contract subseq list if no more publications for that subseq */
405 if (list_empty(&info->zone_list)) {
407 free = &nseq->sseqs[nseq->first_free--];
408 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
412 /* Notify any waiting subscriptions */
413 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
414 tipc_subscr_report_overlap(s,
427 * tipc_nameseq_subscribe - attach a subscription, and issue
428 * the prescribed number of events if there is any sub-
429 * sequence overlapping with the requested sequence
431 static void tipc_nameseq_subscribe(struct name_seq *nseq,
432 struct tipc_subscription *s)
434 struct sub_seq *sseq = nseq->sseqs;
436 list_add(&s->nameseq_list, &nseq->subscriptions);
441 while (sseq != &nseq->sseqs[nseq->first_free]) {
442 if (tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
443 struct publication *crs;
444 struct name_info *info = sseq->info;
447 list_for_each_entry(crs, &info->zone_list, zone_list) {
448 tipc_subscr_report_overlap(s,
462 static struct name_seq *nametbl_find_seq(u32 type)
464 struct hlist_head *seq_head;
467 seq_head = &tipc_nametbl->seq_hlist[hash(type)];
468 hlist_for_each_entry(ns, seq_head, ns_list) {
469 if (ns->type == type)
476 struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
477 u32 scope, u32 node, u32 port, u32 key)
479 struct name_seq *seq = nametbl_find_seq(type);
480 int index = hash(type);
482 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE) ||
484 pr_debug("Failed to publish illegal {%u,%u,%u} with scope %u\n",
485 type, lower, upper, scope);
490 seq = tipc_nameseq_create(type,
491 &tipc_nametbl->seq_hlist[index]);
495 return tipc_nameseq_insert_publ(seq, type, lower, upper,
496 scope, node, port, key);
499 struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
500 u32 node, u32 ref, u32 key)
502 struct publication *publ;
503 struct name_seq *seq = nametbl_find_seq(type);
508 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
509 nameseq_delete_empty(seq);
514 * tipc_nametbl_translate - perform name translation
516 * On entry, 'destnode' is the search domain used during translation.
519 * - if name translation is deferred to another node/cluster/zone,
520 * leaves 'destnode' unchanged (will be non-zero) and returns 0
521 * - if name translation is attempted and succeeds, sets 'destnode'
522 * to publishing node and returns port reference (will be non-zero)
523 * - if name translation is attempted and fails, sets 'destnode' to 0
526 u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
528 struct sub_seq *sseq;
529 struct name_info *info;
530 struct publication *publ;
531 struct name_seq *seq;
535 if (!tipc_in_scope(*destnode, tipc_own_addr))
538 read_lock_bh(&tipc_nametbl_lock);
539 seq = nametbl_find_seq(type);
542 sseq = nameseq_find_subseq(seq, instance);
545 spin_lock_bh(&seq->lock);
548 /* Closest-First Algorithm */
549 if (likely(!*destnode)) {
550 if (!list_empty(&info->node_list)) {
551 publ = list_first_entry(&info->node_list,
554 list_move_tail(&publ->node_list,
556 } else if (!list_empty(&info->cluster_list)) {
557 publ = list_first_entry(&info->cluster_list,
560 list_move_tail(&publ->cluster_list,
561 &info->cluster_list);
563 publ = list_first_entry(&info->zone_list,
566 list_move_tail(&publ->zone_list,
571 /* Round-Robin Algorithm */
572 else if (*destnode == tipc_own_addr) {
573 if (list_empty(&info->node_list))
575 publ = list_first_entry(&info->node_list, struct publication,
577 list_move_tail(&publ->node_list, &info->node_list);
578 } else if (in_own_cluster_exact(*destnode)) {
579 if (list_empty(&info->cluster_list))
581 publ = list_first_entry(&info->cluster_list, struct publication,
583 list_move_tail(&publ->cluster_list, &info->cluster_list);
585 publ = list_first_entry(&info->zone_list, struct publication,
587 list_move_tail(&publ->zone_list, &info->zone_list);
593 spin_unlock_bh(&seq->lock);
595 read_unlock_bh(&tipc_nametbl_lock);
601 * tipc_nametbl_mc_translate - find multicast destinations
603 * Creates list of all local ports that overlap the given multicast address;
604 * also determines if any off-node ports overlap.
606 * Note: Publications with a scope narrower than 'limit' are ignored.
607 * (i.e. local node-scope publications mustn't receive messages arriving
608 * from another node, even if the multcast link brought it here)
610 * Returns non-zero if any off-node ports overlap
612 int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
613 struct tipc_port_list *dports)
615 struct name_seq *seq;
616 struct sub_seq *sseq;
617 struct sub_seq *sseq_stop;
618 struct name_info *info;
621 read_lock_bh(&tipc_nametbl_lock);
622 seq = nametbl_find_seq(type);
626 spin_lock_bh(&seq->lock);
628 sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
629 sseq_stop = seq->sseqs + seq->first_free;
630 for (; sseq != sseq_stop; sseq++) {
631 struct publication *publ;
633 if (sseq->lower > upper)
637 list_for_each_entry(publ, &info->node_list, node_list) {
638 if (publ->scope <= limit)
639 tipc_port_list_add(dports, publ->ref);
642 if (info->cluster_list_size != info->node_list_size)
646 spin_unlock_bh(&seq->lock);
648 read_unlock_bh(&tipc_nametbl_lock);
653 * tipc_nametbl_publish - add name publication to network name tables
655 struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
656 u32 scope, u32 port_ref, u32 key)
658 struct publication *publ;
659 struct sk_buff *buf = NULL;
661 if (tipc_nametbl->local_publ_count >= TIPC_MAX_PUBLICATIONS) {
662 pr_warn("Publication failed, local publication limit reached (%u)\n",
663 TIPC_MAX_PUBLICATIONS);
667 write_lock_bh(&tipc_nametbl_lock);
668 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
669 tipc_own_addr, port_ref, key);
671 tipc_nametbl->local_publ_count++;
672 buf = tipc_named_publish(publ);
673 /* Any pending external events? */
674 tipc_named_process_backlog();
676 write_unlock_bh(&tipc_nametbl_lock);
679 named_cluster_distribute(buf);
684 * tipc_nametbl_withdraw - withdraw name publication from network name tables
686 int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
688 struct publication *publ;
691 write_lock_bh(&tipc_nametbl_lock);
692 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
694 tipc_nametbl->local_publ_count--;
695 buf = tipc_named_withdraw(publ);
696 /* Any pending external events? */
697 tipc_named_process_backlog();
698 write_unlock_bh(&tipc_nametbl_lock);
699 list_del_init(&publ->pport_list);
703 named_cluster_distribute(buf);
706 write_unlock_bh(&tipc_nametbl_lock);
707 pr_err("Unable to remove local publication\n"
708 "(type=%u, lower=%u, ref=%u, key=%u)\n",
709 type, lower, ref, key);
714 * tipc_nametbl_subscribe - add a subscription object to the name table
716 void tipc_nametbl_subscribe(struct tipc_subscription *s)
718 u32 type = s->seq.type;
719 int index = hash(type);
720 struct name_seq *seq;
722 write_lock_bh(&tipc_nametbl_lock);
723 seq = nametbl_find_seq(type);
725 seq = tipc_nameseq_create(type,
726 &tipc_nametbl->seq_hlist[index]);
728 spin_lock_bh(&seq->lock);
729 tipc_nameseq_subscribe(seq, s);
730 spin_unlock_bh(&seq->lock);
732 pr_warn("Failed to create subscription for {%u,%u,%u}\n",
733 s->seq.type, s->seq.lower, s->seq.upper);
735 write_unlock_bh(&tipc_nametbl_lock);
739 * tipc_nametbl_unsubscribe - remove a subscription object from name table
741 void tipc_nametbl_unsubscribe(struct tipc_subscription *s)
743 struct name_seq *seq;
745 write_lock_bh(&tipc_nametbl_lock);
746 seq = nametbl_find_seq(s->seq.type);
748 spin_lock_bh(&seq->lock);
749 list_del_init(&s->nameseq_list);
750 spin_unlock_bh(&seq->lock);
751 nameseq_delete_empty(seq);
753 write_unlock_bh(&tipc_nametbl_lock);
758 * subseq_list - print specified sub-sequence contents into the given buffer
760 static int subseq_list(struct sub_seq *sseq, char *buf, int len, u32 depth,
764 const char *scope_str[] = {"", " zone", " cluster", " node"};
765 struct publication *publ;
766 struct name_info *info;
769 ret = tipc_snprintf(buf, len, "%-10u %-10u ", sseq->lower, sseq->upper);
772 ret += tipc_snprintf(buf - ret, len + ret, "\n");
778 list_for_each_entry(publ, &info->zone_list, zone_list) {
779 sprintf(portIdStr, "<%u.%u.%u:%u>",
780 tipc_zone(publ->node), tipc_cluster(publ->node),
781 tipc_node(publ->node), publ->ref);
782 ret += tipc_snprintf(buf + ret, len - ret, "%-26s ", portIdStr);
784 ret += tipc_snprintf(buf + ret, len - ret, "%-10u %s",
785 publ->key, scope_str[publ->scope]);
787 if (!list_is_last(&publ->zone_list, &info->zone_list))
788 ret += tipc_snprintf(buf + ret, len - ret,
792 ret += tipc_snprintf(buf + ret, len - ret, "\n");
797 * nameseq_list - print specified name sequence contents into the given buffer
799 static int nameseq_list(struct name_seq *seq, char *buf, int len, u32 depth,
800 u32 type, u32 lowbound, u32 upbound, u32 index)
802 struct sub_seq *sseq;
806 if (seq->first_free == 0)
809 sprintf(typearea, "%-10u", seq->type);
812 ret += tipc_snprintf(buf, len, "%s\n", typearea);
816 for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
817 if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
818 ret += tipc_snprintf(buf + ret, len - ret, "%s ",
820 spin_lock_bh(&seq->lock);
821 ret += subseq_list(sseq, buf + ret, len - ret,
823 spin_unlock_bh(&seq->lock);
824 sprintf(typearea, "%10s", " ");
831 * nametbl_header - print name table header into the given buffer
833 static int nametbl_header(char *buf, int len, u32 depth)
835 const char *header[] = {
847 for (i = 0; i < depth; i++)
848 ret += tipc_snprintf(buf + ret, len - ret, header[i]);
849 ret += tipc_snprintf(buf + ret, len - ret, "\n");
854 * nametbl_list - print specified name table contents into the given buffer
856 static int nametbl_list(char *buf, int len, u32 depth_info,
857 u32 type, u32 lowbound, u32 upbound)
859 struct hlist_head *seq_head;
860 struct name_seq *seq;
866 all_types = (depth_info & TIPC_NTQ_ALLTYPES);
867 depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
873 /* display all entries in name table to specified depth */
874 ret += nametbl_header(buf, len, depth);
877 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
878 seq_head = &tipc_nametbl->seq_hlist[i];
879 hlist_for_each_entry(seq, seq_head, ns_list) {
880 ret += nameseq_list(seq, buf + ret, len - ret,
882 lowbound, upbound, i);
886 /* display only the sequence that matches the specified type */
887 if (upbound < lowbound) {
888 ret += tipc_snprintf(buf + ret, len - ret,
889 "invalid name sequence specified\n");
892 ret += nametbl_header(buf + ret, len - ret, depth);
894 seq_head = &tipc_nametbl->seq_hlist[i];
895 hlist_for_each_entry(seq, seq_head, ns_list) {
896 if (seq->type == type) {
897 ret += nameseq_list(seq, buf + ret, len - ret,
899 lowbound, upbound, i);
907 struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
910 struct tipc_name_table_query *argv;
911 struct tlv_desc *rep_tlv;
916 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
917 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
919 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
923 rep_tlv = (struct tlv_desc *)buf->data;
924 pb = TLV_DATA(rep_tlv);
925 pb_len = ULTRA_STRING_MAX_LEN;
926 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
927 read_lock_bh(&tipc_nametbl_lock);
928 str_len = nametbl_list(pb, pb_len, ntohl(argv->depth),
930 ntohl(argv->lowbound), ntohl(argv->upbound));
931 read_unlock_bh(&tipc_nametbl_lock);
932 str_len += 1; /* for "\0" */
933 skb_put(buf, TLV_SPACE(str_len));
934 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
939 int tipc_nametbl_init(void)
943 tipc_nametbl = kzalloc(sizeof(*tipc_nametbl), GFP_ATOMIC);
947 for (i = 0; i < TIPC_NAMETBL_SIZE; i++)
948 INIT_HLIST_HEAD(&tipc_nametbl->seq_hlist[i]);
950 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_ZONE_SCOPE]);
951 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_CLUSTER_SCOPE]);
952 INIT_LIST_HEAD(&tipc_nametbl->publ_list[TIPC_NODE_SCOPE]);
957 * tipc_purge_publications - remove all publications for a given type
959 * tipc_nametbl_lock must be held when calling this function
961 static void tipc_purge_publications(struct name_seq *seq)
963 struct publication *publ, *safe;
964 struct sub_seq *sseq;
965 struct name_info *info;
969 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
970 tipc_nametbl_remove_publ(publ->type, publ->lower, publ->node,
971 publ->ref, publ->key);
974 hlist_del_init(&seq->ns_list);
979 void tipc_nametbl_stop(void)
982 struct name_seq *seq;
983 struct hlist_head *seq_head;
984 struct hlist_node *safe;
986 /* Verify name table is empty and purge any lingering
987 * publications, then release the name table
989 write_lock_bh(&tipc_nametbl_lock);
990 for (i = 0; i < TIPC_NAMETBL_SIZE; i++) {
991 if (hlist_empty(&tipc_nametbl->seq_hlist[i]))
993 seq_head = &tipc_nametbl->seq_hlist[i];
994 hlist_for_each_entry_safe(seq, safe, seq_head, ns_list) {
995 tipc_purge_publications(seq);
998 write_unlock_bh(&tipc_nametbl_lock);
1000 kfree(tipc_nametbl);
1004 static int __tipc_nl_add_nametable_publ(struct tipc_nl_msg *msg,
1005 struct name_seq *seq,
1006 struct sub_seq *sseq, u32 *last_publ)
1009 struct nlattr *attrs;
1010 struct nlattr *publ;
1011 struct publication *p;
1014 list_for_each_entry(p, &sseq->info->zone_list, zone_list)
1015 if (p->key == *last_publ)
1017 if (p->key != *last_publ)
1020 p = list_first_entry(&sseq->info->zone_list, struct publication,
1024 list_for_each_entry_from(p, &sseq->info->zone_list, zone_list) {
1025 *last_publ = p->key;
1027 hdr = genlmsg_put(msg->skb, msg->portid, msg->seq,
1028 &tipc_genl_v2_family, NLM_F_MULTI,
1029 TIPC_NL_NAME_TABLE_GET);
1033 attrs = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE);
1037 publ = nla_nest_start(msg->skb, TIPC_NLA_NAME_TABLE_PUBL);
1041 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_TYPE, seq->type))
1043 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_LOWER, sseq->lower))
1045 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_UPPER, sseq->upper))
1047 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_SCOPE, p->scope))
1049 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_NODE, p->node))
1051 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_REF, p->ref))
1053 if (nla_put_u32(msg->skb, TIPC_NLA_PUBL_KEY, p->key))
1056 nla_nest_end(msg->skb, publ);
1057 nla_nest_end(msg->skb, attrs);
1058 genlmsg_end(msg->skb, hdr);
1065 nla_nest_cancel(msg->skb, publ);
1067 nla_nest_cancel(msg->skb, attrs);
1069 genlmsg_cancel(msg->skb, hdr);
1074 static int __tipc_nl_subseq_list(struct tipc_nl_msg *msg, struct name_seq *seq,
1075 u32 *last_lower, u32 *last_publ)
1077 struct sub_seq *sseq;
1078 struct sub_seq *sseq_start;
1082 sseq_start = nameseq_find_subseq(seq, *last_lower);
1086 sseq_start = seq->sseqs;
1089 for (sseq = sseq_start; sseq != &seq->sseqs[seq->first_free]; sseq++) {
1090 err = __tipc_nl_add_nametable_publ(msg, seq, sseq, last_publ);
1092 *last_lower = sseq->lower;
1101 static int __tipc_nl_seq_list(struct tipc_nl_msg *msg, u32 *last_type,
1102 u32 *last_lower, u32 *last_publ)
1104 struct hlist_head *seq_head;
1105 struct name_seq *seq;
1110 i = hash(*last_type);
1114 for (; i < TIPC_NAMETBL_SIZE; i++) {
1115 seq_head = &tipc_nametbl->seq_hlist[i];
1118 seq = nametbl_find_seq(*last_type);
1122 seq = hlist_entry_safe((seq_head)->first,
1123 struct name_seq, ns_list);
1128 hlist_for_each_entry_from(seq, ns_list) {
1129 spin_lock_bh(&seq->lock);
1131 err = __tipc_nl_subseq_list(msg, seq, last_lower,
1135 *last_type = seq->type;
1136 spin_unlock_bh(&seq->lock);
1139 spin_unlock_bh(&seq->lock);
1146 int tipc_nl_name_table_dump(struct sk_buff *skb, struct netlink_callback *cb)
1149 int done = cb->args[3];
1150 u32 last_type = cb->args[0];
1151 u32 last_lower = cb->args[1];
1152 u32 last_publ = cb->args[2];
1153 struct tipc_nl_msg msg;
1159 msg.portid = NETLINK_CB(cb->skb).portid;
1160 msg.seq = cb->nlh->nlmsg_seq;
1162 read_lock_bh(&tipc_nametbl_lock);
1164 err = __tipc_nl_seq_list(&msg, &last_type, &last_lower, &last_publ);
1167 } else if (err != -EMSGSIZE) {
1168 /* We never set seq or call nl_dump_check_consistent() this
1169 * means that setting prev_seq here will cause the consistence
1170 * check to fail in the netlink callback handler. Resulting in
1171 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if
1177 read_unlock_bh(&tipc_nametbl_lock);
1179 cb->args[0] = last_type;
1180 cb->args[1] = last_lower;
1181 cb->args[2] = last_publ;