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