xref: /openbmc/linux/net/tipc/name_table.c (revision b52124a5)
1 /*
2  * net/tipc/name_table.c: TIPC name table code
3  *
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2004-2008, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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.
23  *
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.
35  */
36 
37 #include "core.h"
38 #include "config.h"
39 #include "name_table.h"
40 #include "name_distr.h"
41 #include "subscr.h"
42 #include "port.h"
43 
44 static int tipc_nametbl_size = 1024;		/* must be a power of 2 */
45 
46 /**
47  * struct name_info - name sequence publication info
48  * @node_list: circular list of publications made by own node
49  * @cluster_list: circular list of publications made by own cluster
50  * @zone_list: circular list of publications made by own zone
51  * @node_list_size: number of entries in "node_list"
52  * @cluster_list_size: number of entries in "cluster_list"
53  * @zone_list_size: number of entries in "zone_list"
54  *
55  * Note: The zone list always contains at least one entry, since all
56  *       publications of the associated name sequence belong to it.
57  *       (The cluster and node lists may be empty.)
58  */
59 
60 struct name_info {
61 	struct publication *node_list;
62 	struct publication *cluster_list;
63 	struct publication *zone_list;
64 	u32 node_list_size;
65 	u32 cluster_list_size;
66 	u32 zone_list_size;
67 };
68 
69 /**
70  * struct sub_seq - container for all published instances of a name sequence
71  * @lower: name sequence lower bound
72  * @upper: name sequence upper bound
73  * @info: pointer to name sequence publication info
74  */
75 
76 struct sub_seq {
77 	u32 lower;
78 	u32 upper;
79 	struct name_info *info;
80 };
81 
82 /**
83  * struct name_seq - container for all published instances of a name type
84  * @type: 32 bit 'type' value for name sequence
85  * @sseq: pointer to dynamically-sized array of sub-sequences of this 'type';
86  *        sub-sequences are sorted in ascending order
87  * @alloc: number of sub-sequences currently in array
88  * @first_free: array index of first unused sub-sequence entry
89  * @ns_list: links to adjacent name sequences in hash chain
90  * @subscriptions: list of subscriptions for this 'type'
91  * @lock: spinlock controlling access to publication lists of all sub-sequences
92  */
93 
94 struct name_seq {
95 	u32 type;
96 	struct sub_seq *sseqs;
97 	u32 alloc;
98 	u32 first_free;
99 	struct hlist_node ns_list;
100 	struct list_head subscriptions;
101 	spinlock_t lock;
102 };
103 
104 /**
105  * struct name_table - table containing all existing port name publications
106  * @types: pointer to fixed-sized array of name sequence lists,
107  *         accessed via hashing on 'type'; name sequence lists are *not* sorted
108  * @local_publ_count: number of publications issued by this node
109  */
110 
111 struct name_table {
112 	struct hlist_head *types;
113 	u32 local_publ_count;
114 };
115 
116 static struct name_table table;
117 static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
118 DEFINE_RWLOCK(tipc_nametbl_lock);
119 
120 
121 static int hash(int x)
122 {
123 	return x & (tipc_nametbl_size - 1);
124 }
125 
126 /**
127  * publ_create - create a publication structure
128  */
129 
130 static struct publication *publ_create(u32 type, u32 lower, u32 upper,
131 				       u32 scope, u32 node, u32 port_ref,
132 				       u32 key)
133 {
134 	struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
135 	if (publ == NULL) {
136 		warn("Publication creation failure, no memory\n");
137 		return NULL;
138 	}
139 
140 	publ->type = type;
141 	publ->lower = lower;
142 	publ->upper = upper;
143 	publ->scope = scope;
144 	publ->node = node;
145 	publ->ref = port_ref;
146 	publ->key = key;
147 	INIT_LIST_HEAD(&publ->local_list);
148 	INIT_LIST_HEAD(&publ->pport_list);
149 	INIT_LIST_HEAD(&publ->subscr.nodesub_list);
150 	return publ;
151 }
152 
153 /**
154  * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
155  */
156 
157 static struct sub_seq *tipc_subseq_alloc(u32 cnt)
158 {
159 	struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
160 	return sseq;
161 }
162 
163 /**
164  * tipc_nameseq_create - create a name sequence structure for the specified 'type'
165  *
166  * Allocates a single sub-sequence structure and sets it to all 0's.
167  */
168 
169 static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
170 {
171 	struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
172 	struct sub_seq *sseq = tipc_subseq_alloc(1);
173 
174 	if (!nseq || !sseq) {
175 		warn("Name sequence creation failed, no memory\n");
176 		kfree(nseq);
177 		kfree(sseq);
178 		return NULL;
179 	}
180 
181 	spin_lock_init(&nseq->lock);
182 	nseq->type = type;
183 	nseq->sseqs = sseq;
184 	nseq->alloc = 1;
185 	INIT_HLIST_NODE(&nseq->ns_list);
186 	INIT_LIST_HEAD(&nseq->subscriptions);
187 	hlist_add_head(&nseq->ns_list, seq_head);
188 	return nseq;
189 }
190 
191 /**
192  * nameseq_find_subseq - find sub-sequence (if any) matching a name instance
193  *
194  * Very time-critical, so binary searches through sub-sequence array.
195  */
196 
197 static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
198 					   u32 instance)
199 {
200 	struct sub_seq *sseqs = nseq->sseqs;
201 	int low = 0;
202 	int high = nseq->first_free - 1;
203 	int mid;
204 
205 	while (low <= high) {
206 		mid = (low + high) / 2;
207 		if (instance < sseqs[mid].lower)
208 			high = mid - 1;
209 		else if (instance > sseqs[mid].upper)
210 			low = mid + 1;
211 		else
212 			return &sseqs[mid];
213 	}
214 	return NULL;
215 }
216 
217 /**
218  * nameseq_locate_subseq - determine position of name instance in sub-sequence
219  *
220  * Returns index in sub-sequence array of the entry that contains the specified
221  * instance value; if no entry contains that value, returns the position
222  * where a new entry for it would be inserted in the array.
223  *
224  * Note: Similar to binary search code for locating a sub-sequence.
225  */
226 
227 static u32 nameseq_locate_subseq(struct name_seq *nseq, u32 instance)
228 {
229 	struct sub_seq *sseqs = nseq->sseqs;
230 	int low = 0;
231 	int high = nseq->first_free - 1;
232 	int mid;
233 
234 	while (low <= high) {
235 		mid = (low + high) / 2;
236 		if (instance < sseqs[mid].lower)
237 			high = mid - 1;
238 		else if (instance > sseqs[mid].upper)
239 			low = mid + 1;
240 		else
241 			return mid;
242 	}
243 	return low;
244 }
245 
246 /**
247  * tipc_nameseq_insert_publ -
248  */
249 
250 static struct publication *tipc_nameseq_insert_publ(struct name_seq *nseq,
251 						    u32 type, u32 lower, u32 upper,
252 						    u32 scope, u32 node, u32 port, u32 key)
253 {
254 	struct subscription *s;
255 	struct subscription *st;
256 	struct publication *publ;
257 	struct sub_seq *sseq;
258 	struct name_info *info;
259 	int created_subseq = 0;
260 
261 	sseq = nameseq_find_subseq(nseq, lower);
262 	if (sseq) {
263 
264 		/* Lower end overlaps existing entry => need an exact match */
265 
266 		if ((sseq->lower != lower) || (sseq->upper != upper)) {
267 			warn("Cannot publish {%u,%u,%u}, overlap error\n",
268 			     type, lower, upper);
269 			return NULL;
270 		}
271 
272 		info = sseq->info;
273 	} else {
274 		u32 inspos;
275 		struct sub_seq *freesseq;
276 
277 		/* Find where lower end should be inserted */
278 
279 		inspos = nameseq_locate_subseq(nseq, lower);
280 
281 		/* Fail if upper end overlaps into an existing entry */
282 
283 		if ((inspos < nseq->first_free) &&
284 		    (upper >= nseq->sseqs[inspos].lower)) {
285 			warn("Cannot publish {%u,%u,%u}, overlap error\n",
286 			     type, lower, upper);
287 			return NULL;
288 		}
289 
290 		/* Ensure there is space for new sub-sequence */
291 
292 		if (nseq->first_free == nseq->alloc) {
293 			struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
294 
295 			if (!sseqs) {
296 				warn("Cannot publish {%u,%u,%u}, no memory\n",
297 				     type, lower, upper);
298 				return NULL;
299 			}
300 			memcpy(sseqs, nseq->sseqs,
301 			       nseq->alloc * sizeof(struct sub_seq));
302 			kfree(nseq->sseqs);
303 			nseq->sseqs = sseqs;
304 			nseq->alloc *= 2;
305 		}
306 
307 		info = kzalloc(sizeof(*info), GFP_ATOMIC);
308 		if (!info) {
309 			warn("Cannot publish {%u,%u,%u}, no memory\n",
310 			     type, lower, upper);
311 			return NULL;
312 		}
313 
314 		/* Insert new sub-sequence */
315 
316 		sseq = &nseq->sseqs[inspos];
317 		freesseq = &nseq->sseqs[nseq->first_free];
318 		memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
319 		memset(sseq, 0, sizeof(*sseq));
320 		nseq->first_free++;
321 		sseq->lower = lower;
322 		sseq->upper = upper;
323 		sseq->info = info;
324 		created_subseq = 1;
325 	}
326 
327 	/* Insert a publication: */
328 
329 	publ = publ_create(type, lower, upper, scope, node, port, key);
330 	if (!publ)
331 		return NULL;
332 
333 	info->zone_list_size++;
334 	if (!info->zone_list)
335 		info->zone_list = publ->zone_list_next = publ;
336 	else {
337 		publ->zone_list_next = info->zone_list->zone_list_next;
338 		info->zone_list->zone_list_next = publ;
339 	}
340 
341 	if (in_own_cluster(node)) {
342 		info->cluster_list_size++;
343 		if (!info->cluster_list)
344 			info->cluster_list = publ->cluster_list_next = publ;
345 		else {
346 			publ->cluster_list_next =
347 			info->cluster_list->cluster_list_next;
348 			info->cluster_list->cluster_list_next = publ;
349 		}
350 	}
351 
352 	if (node == tipc_own_addr) {
353 		info->node_list_size++;
354 		if (!info->node_list)
355 			info->node_list = publ->node_list_next = publ;
356 		else {
357 			publ->node_list_next = info->node_list->node_list_next;
358 			info->node_list->node_list_next = publ;
359 		}
360 	}
361 
362 	/*
363 	 * Any subscriptions waiting for notification?
364 	 */
365 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
366 		tipc_subscr_report_overlap(s,
367 					   publ->lower,
368 					   publ->upper,
369 					   TIPC_PUBLISHED,
370 					   publ->ref,
371 					   publ->node,
372 					   created_subseq);
373 	}
374 	return publ;
375 }
376 
377 /**
378  * tipc_nameseq_remove_publ -
379  *
380  * NOTE: There may be cases where TIPC is asked to remove a publication
381  * that is not in the name table.  For example, if another node issues a
382  * publication for a name sequence that overlaps an existing name sequence
383  * the publication will not be recorded, which means the publication won't
384  * be found when the name sequence is later withdrawn by that node.
385  * A failed withdraw request simply returns a failure indication and lets the
386  * caller issue any error or warning messages associated with such a problem.
387  */
388 
389 static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
390 						    u32 node, u32 ref, u32 key)
391 {
392 	struct publication *publ;
393 	struct publication *curr;
394 	struct publication *prev;
395 	struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
396 	struct name_info *info;
397 	struct sub_seq *free;
398 	struct subscription *s, *st;
399 	int removed_subseq = 0;
400 
401 	if (!sseq)
402 		return NULL;
403 
404 	info = sseq->info;
405 
406 	/* Remove publication from zone scope list */
407 
408 	prev = info->zone_list;
409 	publ = info->zone_list->zone_list_next;
410 	while ((publ->key != key) || (publ->ref != ref) ||
411 	       (publ->node && (publ->node != node))) {
412 		prev = publ;
413 		publ = publ->zone_list_next;
414 		if (prev == info->zone_list) {
415 
416 			/* Prevent endless loop if publication not found */
417 
418 			return NULL;
419 		}
420 	}
421 	if (publ != info->zone_list)
422 		prev->zone_list_next = publ->zone_list_next;
423 	else if (publ->zone_list_next != publ) {
424 		prev->zone_list_next = publ->zone_list_next;
425 		info->zone_list = publ->zone_list_next;
426 	} else {
427 		info->zone_list = NULL;
428 	}
429 	info->zone_list_size--;
430 
431 	/* Remove publication from cluster scope list, if present */
432 
433 	if (in_own_cluster(node)) {
434 		prev = info->cluster_list;
435 		curr = info->cluster_list->cluster_list_next;
436 		while (curr != publ) {
437 			prev = curr;
438 			curr = curr->cluster_list_next;
439 			if (prev == info->cluster_list) {
440 
441 				/* Prevent endless loop for malformed list */
442 
443 				err("Unable to de-list cluster publication\n"
444 				    "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
445 				    publ->type, publ->lower, publ->node,
446 				    publ->ref, publ->key);
447 				goto end_cluster;
448 			}
449 		}
450 		if (publ != info->cluster_list)
451 			prev->cluster_list_next = publ->cluster_list_next;
452 		else if (publ->cluster_list_next != publ) {
453 			prev->cluster_list_next = publ->cluster_list_next;
454 			info->cluster_list = publ->cluster_list_next;
455 		} else {
456 			info->cluster_list = NULL;
457 		}
458 		info->cluster_list_size--;
459 	}
460 end_cluster:
461 
462 	/* Remove publication from node scope list, if present */
463 
464 	if (node == tipc_own_addr) {
465 		prev = info->node_list;
466 		curr = info->node_list->node_list_next;
467 		while (curr != publ) {
468 			prev = curr;
469 			curr = curr->node_list_next;
470 			if (prev == info->node_list) {
471 
472 				/* Prevent endless loop for malformed list */
473 
474 				err("Unable to de-list node publication\n"
475 				    "{%u%u}, node=0x%x, ref=%u, key=%u)\n",
476 				    publ->type, publ->lower, publ->node,
477 				    publ->ref, publ->key);
478 				goto end_node;
479 			}
480 		}
481 		if (publ != info->node_list)
482 			prev->node_list_next = publ->node_list_next;
483 		else if (publ->node_list_next != publ) {
484 			prev->node_list_next = publ->node_list_next;
485 			info->node_list = publ->node_list_next;
486 		} else {
487 			info->node_list = NULL;
488 		}
489 		info->node_list_size--;
490 	}
491 end_node:
492 
493 	/* Contract subseq list if no more publications for that subseq */
494 
495 	if (!info->zone_list) {
496 		kfree(info);
497 		free = &nseq->sseqs[nseq->first_free--];
498 		memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
499 		removed_subseq = 1;
500 	}
501 
502 	/* Notify any waiting subscriptions */
503 
504 	list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
505 		tipc_subscr_report_overlap(s,
506 					   publ->lower,
507 					   publ->upper,
508 					   TIPC_WITHDRAWN,
509 					   publ->ref,
510 					   publ->node,
511 					   removed_subseq);
512 	}
513 
514 	return publ;
515 }
516 
517 /**
518  * tipc_nameseq_subscribe: attach a subscription, and issue
519  * the prescribed number of events if there is any sub-
520  * sequence overlapping with the requested sequence
521  */
522 
523 static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
524 {
525 	struct sub_seq *sseq = nseq->sseqs;
526 
527 	list_add(&s->nameseq_list, &nseq->subscriptions);
528 
529 	if (!sseq)
530 		return;
531 
532 	while (sseq != &nseq->sseqs[nseq->first_free]) {
533 		struct publication *zl = sseq->info->zone_list;
534 		if (zl && tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
535 			struct publication *crs = zl;
536 			int must_report = 1;
537 
538 			do {
539 				tipc_subscr_report_overlap(s,
540 							   sseq->lower,
541 							   sseq->upper,
542 							   TIPC_PUBLISHED,
543 							   crs->ref,
544 							   crs->node,
545 							   must_report);
546 				must_report = 0;
547 				crs = crs->zone_list_next;
548 			} while (crs != zl);
549 		}
550 		sseq++;
551 	}
552 }
553 
554 static struct name_seq *nametbl_find_seq(u32 type)
555 {
556 	struct hlist_head *seq_head;
557 	struct hlist_node *seq_node;
558 	struct name_seq *ns;
559 
560 	seq_head = &table.types[hash(type)];
561 	hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
562 		if (ns->type == type)
563 			return ns;
564 	}
565 
566 	return NULL;
567 };
568 
569 struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
570 					     u32 scope, u32 node, u32 port, u32 key)
571 {
572 	struct name_seq *seq = nametbl_find_seq(type);
573 
574 	if (lower > upper) {
575 		warn("Failed to publish illegal {%u,%u,%u}\n",
576 		     type, lower, upper);
577 		return NULL;
578 	}
579 
580 	if (!seq)
581 		seq = tipc_nameseq_create(type, &table.types[hash(type)]);
582 	if (!seq)
583 		return NULL;
584 
585 	return tipc_nameseq_insert_publ(seq, type, lower, upper,
586 					scope, node, port, key);
587 }
588 
589 struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
590 					     u32 node, u32 ref, u32 key)
591 {
592 	struct publication *publ;
593 	struct name_seq *seq = nametbl_find_seq(type);
594 
595 	if (!seq)
596 		return NULL;
597 
598 	publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
599 
600 	if (!seq->first_free && list_empty(&seq->subscriptions)) {
601 		hlist_del_init(&seq->ns_list);
602 		kfree(seq->sseqs);
603 		kfree(seq);
604 	}
605 	return publ;
606 }
607 
608 /*
609  * tipc_nametbl_translate - translate name to port id
610  *
611  * Note: on entry 'destnode' is the search domain used during translation;
612  *       on exit it passes back the node address of the matching port (if any)
613  */
614 
615 u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
616 {
617 	struct sub_seq *sseq;
618 	struct name_info *info;
619 	struct publication *publ = NULL;
620 	struct name_seq *seq;
621 	u32 ref;
622 
623 	if (!tipc_in_scope(*destnode, tipc_own_addr))
624 		return 0;
625 
626 	read_lock_bh(&tipc_nametbl_lock);
627 	seq = nametbl_find_seq(type);
628 	if (unlikely(!seq))
629 		goto not_found;
630 	sseq = nameseq_find_subseq(seq, instance);
631 	if (unlikely(!sseq))
632 		goto not_found;
633 	spin_lock_bh(&seq->lock);
634 	info = sseq->info;
635 
636 	/* Closest-First Algorithm: */
637 	if (likely(!*destnode)) {
638 		publ = info->node_list;
639 		if (publ) {
640 			info->node_list = publ->node_list_next;
641 found:
642 			ref = publ->ref;
643 			*destnode = publ->node;
644 			spin_unlock_bh(&seq->lock);
645 			read_unlock_bh(&tipc_nametbl_lock);
646 			return ref;
647 		}
648 		publ = info->cluster_list;
649 		if (publ) {
650 			info->cluster_list = publ->cluster_list_next;
651 			goto found;
652 		}
653 		publ = info->zone_list;
654 		if (publ) {
655 			info->zone_list = publ->zone_list_next;
656 			goto found;
657 		}
658 	}
659 
660 	/* Round-Robin Algorithm: */
661 	else if (*destnode == tipc_own_addr) {
662 		publ = info->node_list;
663 		if (publ) {
664 			info->node_list = publ->node_list_next;
665 			goto found;
666 		}
667 	} else if (in_own_cluster(*destnode)) {
668 		publ = info->cluster_list;
669 		if (publ) {
670 			info->cluster_list = publ->cluster_list_next;
671 			goto found;
672 		}
673 	} else {
674 		publ = info->zone_list;
675 		if (publ) {
676 			info->zone_list = publ->zone_list_next;
677 			goto found;
678 		}
679 	}
680 	spin_unlock_bh(&seq->lock);
681 not_found:
682 	read_unlock_bh(&tipc_nametbl_lock);
683 	return 0;
684 }
685 
686 /**
687  * tipc_nametbl_mc_translate - find multicast destinations
688  *
689  * Creates list of all local ports that overlap the given multicast address;
690  * also determines if any off-node ports overlap.
691  *
692  * Note: Publications with a scope narrower than 'limit' are ignored.
693  * (i.e. local node-scope publications mustn't receive messages arriving
694  * from another node, even if the multcast link brought it here)
695  *
696  * Returns non-zero if any off-node ports overlap
697  */
698 
699 int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
700 			      struct port_list *dports)
701 {
702 	struct name_seq *seq;
703 	struct sub_seq *sseq;
704 	struct sub_seq *sseq_stop;
705 	struct name_info *info;
706 	int res = 0;
707 
708 	read_lock_bh(&tipc_nametbl_lock);
709 	seq = nametbl_find_seq(type);
710 	if (!seq)
711 		goto exit;
712 
713 	spin_lock_bh(&seq->lock);
714 
715 	sseq = seq->sseqs + nameseq_locate_subseq(seq, lower);
716 	sseq_stop = seq->sseqs + seq->first_free;
717 	for (; sseq != sseq_stop; sseq++) {
718 		struct publication *publ;
719 
720 		if (sseq->lower > upper)
721 			break;
722 
723 		info = sseq->info;
724 		publ = info->node_list;
725 		if (publ) {
726 			do {
727 				if (publ->scope <= limit)
728 					tipc_port_list_add(dports, publ->ref);
729 				publ = publ->node_list_next;
730 			} while (publ != info->node_list);
731 		}
732 
733 		if (info->cluster_list_size != info->node_list_size)
734 			res = 1;
735 	}
736 
737 	spin_unlock_bh(&seq->lock);
738 exit:
739 	read_unlock_bh(&tipc_nametbl_lock);
740 	return res;
741 }
742 
743 /**
744  * tipc_nametbl_publish_rsv - publish port name using a reserved name type
745  */
746 
747 int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
748 			struct tipc_name_seq const *seq)
749 {
750 	int res;
751 
752 	atomic_inc(&rsv_publ_ok);
753 	res = tipc_publish(ref, scope, seq);
754 	atomic_dec(&rsv_publ_ok);
755 	return res;
756 }
757 
758 /**
759  * tipc_nametbl_publish - add name publication to network name tables
760  */
761 
762 struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
763 				    u32 scope, u32 port_ref, u32 key)
764 {
765 	struct publication *publ;
766 
767 	if (table.local_publ_count >= tipc_max_publications) {
768 		warn("Publication failed, local publication limit reached (%u)\n",
769 		     tipc_max_publications);
770 		return NULL;
771 	}
772 	if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
773 		warn("Publication failed, reserved name {%u,%u,%u}\n",
774 		     type, lower, upper);
775 		return NULL;
776 	}
777 
778 	write_lock_bh(&tipc_nametbl_lock);
779 	table.local_publ_count++;
780 	publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
781 				   tipc_own_addr, port_ref, key);
782 	if (publ && (scope != TIPC_NODE_SCOPE))
783 		tipc_named_publish(publ);
784 	write_unlock_bh(&tipc_nametbl_lock);
785 	return publ;
786 }
787 
788 /**
789  * tipc_nametbl_withdraw - withdraw name publication from network name tables
790  */
791 
792 int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
793 {
794 	struct publication *publ;
795 
796 	write_lock_bh(&tipc_nametbl_lock);
797 	publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
798 	if (likely(publ)) {
799 		table.local_publ_count--;
800 		if (publ->scope != TIPC_NODE_SCOPE)
801 			tipc_named_withdraw(publ);
802 		write_unlock_bh(&tipc_nametbl_lock);
803 		list_del_init(&publ->pport_list);
804 		kfree(publ);
805 		return 1;
806 	}
807 	write_unlock_bh(&tipc_nametbl_lock);
808 	err("Unable to remove local publication\n"
809 	    "(type=%u, lower=%u, ref=%u, key=%u)\n",
810 	    type, lower, ref, key);
811 	return 0;
812 }
813 
814 /**
815  * tipc_nametbl_subscribe - add a subscription object to the name table
816  */
817 
818 void tipc_nametbl_subscribe(struct subscription *s)
819 {
820 	u32 type = s->seq.type;
821 	struct name_seq *seq;
822 
823 	write_lock_bh(&tipc_nametbl_lock);
824 	seq = nametbl_find_seq(type);
825 	if (!seq)
826 		seq = tipc_nameseq_create(type, &table.types[hash(type)]);
827 	if (seq) {
828 		spin_lock_bh(&seq->lock);
829 		tipc_nameseq_subscribe(seq, s);
830 		spin_unlock_bh(&seq->lock);
831 	} else {
832 		warn("Failed to create subscription for {%u,%u,%u}\n",
833 		     s->seq.type, s->seq.lower, s->seq.upper);
834 	}
835 	write_unlock_bh(&tipc_nametbl_lock);
836 }
837 
838 /**
839  * tipc_nametbl_unsubscribe - remove a subscription object from name table
840  */
841 
842 void tipc_nametbl_unsubscribe(struct subscription *s)
843 {
844 	struct name_seq *seq;
845 
846 	write_lock_bh(&tipc_nametbl_lock);
847 	seq = nametbl_find_seq(s->seq.type);
848 	if (seq != NULL) {
849 		spin_lock_bh(&seq->lock);
850 		list_del_init(&s->nameseq_list);
851 		spin_unlock_bh(&seq->lock);
852 		if ((seq->first_free == 0) && list_empty(&seq->subscriptions)) {
853 			hlist_del_init(&seq->ns_list);
854 			kfree(seq->sseqs);
855 			kfree(seq);
856 		}
857 	}
858 	write_unlock_bh(&tipc_nametbl_lock);
859 }
860 
861 
862 /**
863  * subseq_list: print specified sub-sequence contents into the given buffer
864  */
865 
866 static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
867 			u32 index)
868 {
869 	char portIdStr[27];
870 	const char *scope_str[] = {"", " zone", " cluster", " node"};
871 	struct publication *publ = sseq->info->zone_list;
872 
873 	tipc_printf(buf, "%-10u %-10u ", sseq->lower, sseq->upper);
874 
875 	if (depth == 2 || !publ) {
876 		tipc_printf(buf, "\n");
877 		return;
878 	}
879 
880 	do {
881 		sprintf(portIdStr, "<%u.%u.%u:%u>",
882 			 tipc_zone(publ->node), tipc_cluster(publ->node),
883 			 tipc_node(publ->node), publ->ref);
884 		tipc_printf(buf, "%-26s ", portIdStr);
885 		if (depth > 3) {
886 			tipc_printf(buf, "%-10u %s", publ->key,
887 				    scope_str[publ->scope]);
888 		}
889 
890 		publ = publ->zone_list_next;
891 		if (publ == sseq->info->zone_list)
892 			break;
893 
894 		tipc_printf(buf, "\n%33s", " ");
895 	} while (1);
896 
897 	tipc_printf(buf, "\n");
898 }
899 
900 /**
901  * nameseq_list: print specified name sequence contents into the given buffer
902  */
903 
904 static void nameseq_list(struct name_seq *seq, struct print_buf *buf, u32 depth,
905 			 u32 type, u32 lowbound, u32 upbound, u32 index)
906 {
907 	struct sub_seq *sseq;
908 	char typearea[11];
909 
910 	if (seq->first_free == 0)
911 		return;
912 
913 	sprintf(typearea, "%-10u", seq->type);
914 
915 	if (depth == 1) {
916 		tipc_printf(buf, "%s\n", typearea);
917 		return;
918 	}
919 
920 	for (sseq = seq->sseqs; sseq != &seq->sseqs[seq->first_free]; sseq++) {
921 		if ((lowbound <= sseq->upper) && (upbound >= sseq->lower)) {
922 			tipc_printf(buf, "%s ", typearea);
923 			spin_lock_bh(&seq->lock);
924 			subseq_list(sseq, buf, depth, index);
925 			spin_unlock_bh(&seq->lock);
926 			sprintf(typearea, "%10s", " ");
927 		}
928 	}
929 }
930 
931 /**
932  * nametbl_header - print name table header into the given buffer
933  */
934 
935 static void nametbl_header(struct print_buf *buf, u32 depth)
936 {
937 	const char *header[] = {
938 		"Type       ",
939 		"Lower      Upper      ",
940 		"Port Identity              ",
941 		"Publication Scope"
942 	};
943 
944 	int i;
945 
946 	if (depth > 4)
947 		depth = 4;
948 	for (i = 0; i < depth; i++)
949 		tipc_printf(buf, header[i]);
950 	tipc_printf(buf, "\n");
951 }
952 
953 /**
954  * nametbl_list - print specified name table contents into the given buffer
955  */
956 
957 static void nametbl_list(struct print_buf *buf, u32 depth_info,
958 			 u32 type, u32 lowbound, u32 upbound)
959 {
960 	struct hlist_head *seq_head;
961 	struct hlist_node *seq_node;
962 	struct name_seq *seq;
963 	int all_types;
964 	u32 depth;
965 	u32 i;
966 
967 	all_types = (depth_info & TIPC_NTQ_ALLTYPES);
968 	depth = (depth_info & ~TIPC_NTQ_ALLTYPES);
969 
970 	if (depth == 0)
971 		return;
972 
973 	if (all_types) {
974 		/* display all entries in name table to specified depth */
975 		nametbl_header(buf, depth);
976 		lowbound = 0;
977 		upbound = ~0;
978 		for (i = 0; i < tipc_nametbl_size; i++) {
979 			seq_head = &table.types[i];
980 			hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
981 				nameseq_list(seq, buf, depth, seq->type,
982 					     lowbound, upbound, i);
983 			}
984 		}
985 	} else {
986 		/* display only the sequence that matches the specified type */
987 		if (upbound < lowbound) {
988 			tipc_printf(buf, "invalid name sequence specified\n");
989 			return;
990 		}
991 		nametbl_header(buf, depth);
992 		i = hash(type);
993 		seq_head = &table.types[i];
994 		hlist_for_each_entry(seq, seq_node, seq_head, ns_list) {
995 			if (seq->type == type) {
996 				nameseq_list(seq, buf, depth, type,
997 					     lowbound, upbound, i);
998 				break;
999 			}
1000 		}
1001 	}
1002 }
1003 
1004 #define MAX_NAME_TBL_QUERY 32768
1005 
1006 struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
1007 {
1008 	struct sk_buff *buf;
1009 	struct tipc_name_table_query *argv;
1010 	struct tlv_desc *rep_tlv;
1011 	struct print_buf b;
1012 	int str_len;
1013 
1014 	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_NAME_TBL_QUERY))
1015 		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
1016 
1017 	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
1018 	if (!buf)
1019 		return NULL;
1020 
1021 	rep_tlv = (struct tlv_desc *)buf->data;
1022 	tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
1023 	argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
1024 	read_lock_bh(&tipc_nametbl_lock);
1025 	nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
1026 		     ntohl(argv->lowbound), ntohl(argv->upbound));
1027 	read_unlock_bh(&tipc_nametbl_lock);
1028 	str_len = tipc_printbuf_validate(&b);
1029 
1030 	skb_put(buf, TLV_SPACE(str_len));
1031 	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
1032 
1033 	return buf;
1034 }
1035 
1036 int tipc_nametbl_init(void)
1037 {
1038 	table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
1039 			      GFP_ATOMIC);
1040 	if (!table.types)
1041 		return -ENOMEM;
1042 
1043 	table.local_publ_count = 0;
1044 	return 0;
1045 }
1046 
1047 void tipc_nametbl_stop(void)
1048 {
1049 	u32 i;
1050 
1051 	if (!table.types)
1052 		return;
1053 
1054 	/* Verify name table is empty, then release it */
1055 
1056 	write_lock_bh(&tipc_nametbl_lock);
1057 	for (i = 0; i < tipc_nametbl_size; i++) {
1058 		if (!hlist_empty(&table.types[i]))
1059 			err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
1060 	}
1061 	kfree(table.types);
1062 	table.types = NULL;
1063 	write_unlock_bh(&tipc_nametbl_lock);
1064 }
1065 
1066