]> git.ipfire.org Git - thirdparty/linux.git/blame - net/tipc/name_table.c
tipc: Partition name table instance array info into two parts
[thirdparty/linux.git] / net / tipc / name_table.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/name_table.c: TIPC name table code
c4307285 3 *
593a5f22 4 * Copyright (c) 2000-2006, Ericsson AB
968edbe1 5 * Copyright (c) 2004-2008, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
b97bf3fd
PL
39#include "name_table.h"
40#include "name_distr.h"
b97bf3fd
PL
41#include "subscr.h"
42#include "port.h"
b97bf3fd 43
988f088a 44static int tipc_nametbl_size = 1024; /* must be a power of 2 */
b97bf3fd
PL
45
46/**
b52124a5 47 * struct name_info - name sequence publication info
968edbe1
AS
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.)
b97bf3fd
PL
58 */
59
b52124a5 60struct name_info {
b97bf3fd
PL
61 struct publication *node_list;
62 struct publication *cluster_list;
63 struct publication *zone_list;
968edbe1
AS
64 u32 node_list_size;
65 u32 cluster_list_size;
66 u32 zone_list_size;
b97bf3fd
PL
67};
68
b52124a5
AS
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
76struct sub_seq {
77 u32 lower;
78 u32 upper;
79 struct name_info *info;
80};
81
c4307285 82/**
b97bf3fd
PL
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
f131072c 88 * @first_free: array index of first unused sub-sequence entry
b97bf3fd
PL
89 * @ns_list: links to adjacent name sequences in hash chain
90 * @subscriptions: list of subscriptions for this 'type'
307fdf5e 91 * @lock: spinlock controlling access to publication lists of all sub-sequences
b97bf3fd
PL
92 */
93
94struct 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
c4307285 106 * @types: pointer to fixed-sized array of name sequence lists,
b97bf3fd
PL
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
111struct name_table {
112 struct hlist_head *types;
113 u32 local_publ_count;
114};
115
e3ec9c7d 116static struct name_table table;
b97bf3fd 117static atomic_t rsv_publ_ok = ATOMIC_INIT(0);
34af946a 118DEFINE_RWLOCK(tipc_nametbl_lock);
b97bf3fd
PL
119
120
05790c64 121static int hash(int x)
b97bf3fd 122{
a02cec21 123 return x & (tipc_nametbl_size - 1);
b97bf3fd
PL
124}
125
126/**
127 * publ_create - create a publication structure
128 */
129
c4307285
YH
130static struct publication *publ_create(u32 type, u32 lower, u32 upper,
131 u32 scope, u32 node, u32 port_ref,
b97bf3fd
PL
132 u32 key)
133{
0da974f4 134 struct publication *publ = kzalloc(sizeof(*publ), GFP_ATOMIC);
b97bf3fd 135 if (publ == NULL) {
a10bd924 136 warn("Publication creation failure, no memory\n");
1fc54d8f 137 return NULL;
b97bf3fd
PL
138 }
139
b97bf3fd
PL
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/**
4323add6 154 * tipc_subseq_alloc - allocate a specified number of sub-sequence structures
b97bf3fd
PL
155 */
156
988f088a 157static struct sub_seq *tipc_subseq_alloc(u32 cnt)
b97bf3fd 158{
0da974f4 159 struct sub_seq *sseq = kcalloc(cnt, sizeof(struct sub_seq), GFP_ATOMIC);
b97bf3fd
PL
160 return sseq;
161}
162
163/**
4323add6 164 * tipc_nameseq_create - create a name sequence structure for the specified 'type'
c4307285 165 *
b97bf3fd
PL
166 * Allocates a single sub-sequence structure and sets it to all 0's.
167 */
168
988f088a 169static struct name_seq *tipc_nameseq_create(u32 type, struct hlist_head *seq_head)
b97bf3fd 170{
0da974f4 171 struct name_seq *nseq = kzalloc(sizeof(*nseq), GFP_ATOMIC);
4323add6 172 struct sub_seq *sseq = tipc_subseq_alloc(1);
b97bf3fd
PL
173
174 if (!nseq || !sseq) {
a10bd924 175 warn("Name sequence creation failed, no memory\n");
b97bf3fd
PL
176 kfree(nseq);
177 kfree(sseq);
1fc54d8f 178 return NULL;
b97bf3fd
PL
179 }
180
34af946a 181 spin_lock_init(&nseq->lock);
b97bf3fd
PL
182 nseq->type = type;
183 nseq->sseqs = sseq;
b97bf3fd
PL
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
c4307285 193 *
b97bf3fd
PL
194 * Very time-critical, so binary searches through sub-sequence array.
195 */
196
05790c64
SR
197static struct sub_seq *nameseq_find_subseq(struct name_seq *nseq,
198 u32 instance)
b97bf3fd
PL
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 }
1fc54d8f 214 return NULL;
b97bf3fd
PL
215}
216
217/**
218 * nameseq_locate_subseq - determine position of name instance in sub-sequence
c4307285 219 *
b97bf3fd
PL
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
227static 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/**
c4307285 247 * tipc_nameseq_insert_publ -
b97bf3fd
PL
248 */
249
988f088a
AB
250static 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)
b97bf3fd
PL
253{
254 struct subscription *s;
255 struct subscription *st;
256 struct publication *publ;
257 struct sub_seq *sseq;
b52124a5 258 struct name_info *info;
b97bf3fd
PL
259 int created_subseq = 0;
260
b97bf3fd 261 sseq = nameseq_find_subseq(nseq, lower);
b97bf3fd
PL
262 if (sseq) {
263
264 /* Lower end overlaps existing entry => need an exact match */
265
266 if ((sseq->lower != lower) || (sseq->upper != upper)) {
f131072c
AS
267 warn("Cannot publish {%u,%u,%u}, overlap error\n",
268 type, lower, upper);
1fc54d8f 269 return NULL;
b97bf3fd 270 }
b52124a5
AS
271
272 info = sseq->info;
b97bf3fd
PL
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)) {
f131072c
AS
285 warn("Cannot publish {%u,%u,%u}, overlap error\n",
286 type, lower, upper);
1fc54d8f 287 return NULL;
b97bf3fd
PL
288 }
289
290 /* Ensure there is space for new sub-sequence */
291
292 if (nseq->first_free == nseq->alloc) {
9ab230f8
AS
293 struct sub_seq *sseqs = tipc_subseq_alloc(nseq->alloc * 2);
294
295 if (!sseqs) {
f131072c
AS
296 warn("Cannot publish {%u,%u,%u}, no memory\n",
297 type, lower, upper);
1fc54d8f 298 return NULL;
b97bf3fd 299 }
9ab230f8
AS
300 memcpy(sseqs, nseq->sseqs,
301 nseq->alloc * sizeof(struct sub_seq));
302 kfree(nseq->sseqs);
303 nseq->sseqs = sseqs;
304 nseq->alloc *= 2;
b97bf3fd 305 }
b97bf3fd 306
b52124a5
AS
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
b97bf3fd
PL
314 /* Insert new sub-sequence */
315
b97bf3fd
PL
316 sseq = &nseq->sseqs[inspos];
317 freesseq = &nseq->sseqs[nseq->first_free];
0e65967e
AS
318 memmove(sseq + 1, sseq, (freesseq - sseq) * sizeof(*sseq));
319 memset(sseq, 0, sizeof(*sseq));
b97bf3fd
PL
320 nseq->first_free++;
321 sseq->lower = lower;
322 sseq->upper = upper;
b52124a5 323 sseq->info = info;
b97bf3fd
PL
324 created_subseq = 1;
325 }
b97bf3fd
PL
326
327 /* Insert a publication: */
328
329 publ = publ_create(type, lower, upper, scope, node, port, key);
330 if (!publ)
1fc54d8f 331 return NULL;
b97bf3fd 332
b52124a5
AS
333 info->zone_list_size++;
334 if (!info->zone_list)
335 info->zone_list = publ->zone_list_next = publ;
b97bf3fd 336 else {
b52124a5
AS
337 publ->zone_list_next = info->zone_list->zone_list_next;
338 info->zone_list->zone_list_next = publ;
b97bf3fd
PL
339 }
340
341 if (in_own_cluster(node)) {
b52124a5
AS
342 info->cluster_list_size++;
343 if (!info->cluster_list)
344 info->cluster_list = publ->cluster_list_next = publ;
b97bf3fd
PL
345 else {
346 publ->cluster_list_next =
b52124a5
AS
347 info->cluster_list->cluster_list_next;
348 info->cluster_list->cluster_list_next = publ;
b97bf3fd
PL
349 }
350 }
351
352 if (node == tipc_own_addr) {
b52124a5
AS
353 info->node_list_size++;
354 if (!info->node_list)
355 info->node_list = publ->node_list_next = publ;
b97bf3fd 356 else {
b52124a5
AS
357 publ->node_list_next = info->node_list->node_list_next;
358 info->node_list->node_list_next = publ;
b97bf3fd
PL
359 }
360 }
361
c4307285
YH
362 /*
363 * Any subscriptions waiting for notification?
b97bf3fd
PL
364 */
365 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
4323add6
PL
366 tipc_subscr_report_overlap(s,
367 publ->lower,
368 publ->upper,
369 TIPC_PUBLISHED,
c4307285 370 publ->ref,
4323add6
PL
371 publ->node,
372 created_subseq);
b97bf3fd
PL
373 }
374 return publ;
375}
376
377/**
4323add6 378 * tipc_nameseq_remove_publ -
c4307285 379 *
f131072c
AS
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.
b97bf3fd
PL
387 */
388
988f088a
AB
389static struct publication *tipc_nameseq_remove_publ(struct name_seq *nseq, u32 inst,
390 u32 node, u32 ref, u32 key)
b97bf3fd
PL
391{
392 struct publication *publ;
f131072c 393 struct publication *curr;
b97bf3fd
PL
394 struct publication *prev;
395 struct sub_seq *sseq = nameseq_find_subseq(nseq, inst);
b52124a5 396 struct name_info *info;
b97bf3fd
PL
397 struct sub_seq *free;
398 struct subscription *s, *st;
399 int removed_subseq = 0;
400
f131072c 401 if (!sseq)
1fc54d8f 402 return NULL;
f131072c 403
b52124a5
AS
404 info = sseq->info;
405
f131072c
AS
406 /* Remove publication from zone scope list */
407
b52124a5
AS
408 prev = info->zone_list;
409 publ = info->zone_list->zone_list_next;
c4307285 410 while ((publ->key != key) || (publ->ref != ref) ||
b97bf3fd
PL
411 (publ->node && (publ->node != node))) {
412 prev = publ;
413 publ = publ->zone_list_next;
b52124a5 414 if (prev == info->zone_list) {
c4307285 415
f131072c
AS
416 /* Prevent endless loop if publication not found */
417
418 return NULL;
419 }
b97bf3fd 420 }
b52124a5 421 if (publ != info->zone_list)
b97bf3fd
PL
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;
b52124a5 425 info->zone_list = publ->zone_list_next;
b97bf3fd 426 } else {
b52124a5 427 info->zone_list = NULL;
b97bf3fd 428 }
b52124a5 429 info->zone_list_size--;
b97bf3fd 430
f131072c
AS
431 /* Remove publication from cluster scope list, if present */
432
b97bf3fd 433 if (in_own_cluster(node)) {
b52124a5
AS
434 prev = info->cluster_list;
435 curr = info->cluster_list->cluster_list_next;
f131072c
AS
436 while (curr != publ) {
437 prev = curr;
438 curr = curr->cluster_list_next;
b52124a5 439 if (prev == info->cluster_list) {
f131072c
AS
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",
c4307285 445 publ->type, publ->lower, publ->node,
f131072c
AS
446 publ->ref, publ->key);
447 goto end_cluster;
448 }
b97bf3fd 449 }
b52124a5 450 if (publ != info->cluster_list)
b97bf3fd
PL
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;
b52124a5 454 info->cluster_list = publ->cluster_list_next;
b97bf3fd 455 } else {
b52124a5 456 info->cluster_list = NULL;
b97bf3fd 457 }
b52124a5 458 info->cluster_list_size--;
b97bf3fd 459 }
f131072c
AS
460end_cluster:
461
462 /* Remove publication from node scope list, if present */
b97bf3fd
PL
463
464 if (node == tipc_own_addr) {
b52124a5
AS
465 prev = info->node_list;
466 curr = info->node_list->node_list_next;
f131072c
AS
467 while (curr != publ) {
468 prev = curr;
469 curr = curr->node_list_next;
b52124a5 470 if (prev == info->node_list) {
f131072c
AS
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",
c4307285 476 publ->type, publ->lower, publ->node,
f131072c
AS
477 publ->ref, publ->key);
478 goto end_node;
479 }
b97bf3fd 480 }
b52124a5 481 if (publ != info->node_list)
b97bf3fd
PL
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;
b52124a5 485 info->node_list = publ->node_list_next;
b97bf3fd 486 } else {
b52124a5 487 info->node_list = NULL;
b97bf3fd 488 }
b52124a5 489 info->node_list_size--;
b97bf3fd 490 }
f131072c 491end_node:
b97bf3fd 492
f131072c
AS
493 /* Contract subseq list if no more publications for that subseq */
494
b52124a5
AS
495 if (!info->zone_list) {
496 kfree(info);
b97bf3fd 497 free = &nseq->sseqs[nseq->first_free--];
0e65967e 498 memmove(sseq, sseq + 1, (free - (sseq + 1)) * sizeof(*sseq));
b97bf3fd
PL
499 removed_subseq = 1;
500 }
501
f131072c
AS
502 /* Notify any waiting subscriptions */
503
b97bf3fd 504 list_for_each_entry_safe(s, st, &nseq->subscriptions, nameseq_list) {
4323add6
PL
505 tipc_subscr_report_overlap(s,
506 publ->lower,
507 publ->upper,
c4307285
YH
508 TIPC_WITHDRAWN,
509 publ->ref,
4323add6
PL
510 publ->node,
511 removed_subseq);
b97bf3fd 512 }
f131072c 513
b97bf3fd
PL
514 return publ;
515}
516
517/**
4323add6 518 * tipc_nameseq_subscribe: attach a subscription, and issue
b97bf3fd
PL
519 * the prescribed number of events if there is any sub-
520 * sequence overlapping with the requested sequence
521 */
522
248bbf38 523static void tipc_nameseq_subscribe(struct name_seq *nseq, struct subscription *s)
b97bf3fd
PL
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]) {
b52124a5 533 struct publication *zl = sseq->info->zone_list;
0e65967e 534 if (zl && tipc_subscr_overlap(s, sseq->lower, sseq->upper)) {
b97bf3fd
PL
535 struct publication *crs = zl;
536 int must_report = 1;
537
538 do {
c4307285
YH
539 tipc_subscr_report_overlap(s,
540 sseq->lower,
4323add6
PL
541 sseq->upper,
542 TIPC_PUBLISHED,
543 crs->ref,
544 crs->node,
545 must_report);
b97bf3fd
PL
546 must_report = 0;
547 crs = crs->zone_list_next;
548 } while (crs != zl);
549 }
550 sseq++;
551 }
552}
553
554static 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
b97bf3fd
PL
560 seq_head = &table.types[hash(type)];
561 hlist_for_each_entry(ns, seq_node, seq_head, ns_list) {
b29f1428 562 if (ns->type == type)
b97bf3fd 563 return ns;
b97bf3fd
PL
564 }
565
1fc54d8f 566 return NULL;
b97bf3fd
PL
567};
568
4323add6
PL
569struct publication *tipc_nametbl_insert_publ(u32 type, u32 lower, u32 upper,
570 u32 scope, u32 node, u32 port, u32 key)
b97bf3fd
PL
571{
572 struct name_seq *seq = nametbl_find_seq(type);
573
b97bf3fd 574 if (lower > upper) {
f131072c 575 warn("Failed to publish illegal {%u,%u,%u}\n",
b97bf3fd 576 type, lower, upper);
1fc54d8f 577 return NULL;
b97bf3fd
PL
578 }
579
b29f1428 580 if (!seq)
4323add6 581 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
b97bf3fd 582 if (!seq)
1fc54d8f 583 return NULL;
b97bf3fd 584
4323add6
PL
585 return tipc_nameseq_insert_publ(seq, type, lower, upper,
586 scope, node, port, key);
b97bf3fd
PL
587}
588
c4307285 589struct publication *tipc_nametbl_remove_publ(u32 type, u32 lower,
4323add6 590 u32 node, u32 ref, u32 key)
b97bf3fd
PL
591{
592 struct publication *publ;
593 struct name_seq *seq = nametbl_find_seq(type);
594
595 if (!seq)
1fc54d8f 596 return NULL;
b97bf3fd 597
4323add6 598 publ = tipc_nameseq_remove_publ(seq, lower, node, ref, key);
b97bf3fd
PL
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/*
5d9c54c1 609 * tipc_nametbl_translate - translate name to port id
b97bf3fd
PL
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
4323add6 615u32 tipc_nametbl_translate(u32 type, u32 instance, u32 *destnode)
b97bf3fd
PL
616{
617 struct sub_seq *sseq;
b52124a5 618 struct name_info *info;
1fc54d8f 619 struct publication *publ = NULL;
b97bf3fd
PL
620 struct name_seq *seq;
621 u32 ref;
622
c68ca7b7 623 if (!tipc_in_scope(*destnode, tipc_own_addr))
b97bf3fd
PL
624 return 0;
625
4323add6 626 read_lock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
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);
b52124a5 634 info = sseq->info;
b97bf3fd
PL
635
636 /* Closest-First Algorithm: */
637 if (likely(!*destnode)) {
b52124a5 638 publ = info->node_list;
b97bf3fd 639 if (publ) {
b52124a5 640 info->node_list = publ->node_list_next;
b97bf3fd
PL
641found:
642 ref = publ->ref;
643 *destnode = publ->node;
644 spin_unlock_bh(&seq->lock);
4323add6 645 read_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
646 return ref;
647 }
b52124a5 648 publ = info->cluster_list;
b97bf3fd 649 if (publ) {
b52124a5 650 info->cluster_list = publ->cluster_list_next;
b97bf3fd
PL
651 goto found;
652 }
b52124a5 653 publ = info->zone_list;
b97bf3fd 654 if (publ) {
b52124a5 655 info->zone_list = publ->zone_list_next;
b97bf3fd
PL
656 goto found;
657 }
658 }
659
660 /* Round-Robin Algorithm: */
661 else if (*destnode == tipc_own_addr) {
b52124a5 662 publ = info->node_list;
b97bf3fd 663 if (publ) {
b52124a5 664 info->node_list = publ->node_list_next;
b97bf3fd
PL
665 goto found;
666 }
667 } else if (in_own_cluster(*destnode)) {
b52124a5 668 publ = info->cluster_list;
b97bf3fd 669 if (publ) {
b52124a5 670 info->cluster_list = publ->cluster_list_next;
b97bf3fd
PL
671 goto found;
672 }
673 } else {
b52124a5 674 publ = info->zone_list;
b97bf3fd 675 if (publ) {
b52124a5 676 info->zone_list = publ->zone_list_next;
b97bf3fd
PL
677 goto found;
678 }
679 }
680 spin_unlock_bh(&seq->lock);
681not_found:
4323add6 682 read_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
683 return 0;
684}
685
686/**
4323add6 687 * tipc_nametbl_mc_translate - find multicast destinations
c4307285 688 *
b97bf3fd
PL
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)
c4307285 695 *
b97bf3fd
PL
696 * Returns non-zero if any off-node ports overlap
697 */
698
4323add6
PL
699int tipc_nametbl_mc_translate(u32 type, u32 lower, u32 upper, u32 limit,
700 struct port_list *dports)
b97bf3fd
PL
701{
702 struct name_seq *seq;
703 struct sub_seq *sseq;
704 struct sub_seq *sseq_stop;
b52124a5 705 struct name_info *info;
b97bf3fd
PL
706 int res = 0;
707
4323add6 708 read_lock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
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;
968edbe1 722
b52124a5
AS
723 info = sseq->info;
724 publ = info->node_list;
968edbe1 725 if (publ) {
b97bf3fd 726 do {
968edbe1 727 if (publ->scope <= limit)
4323add6 728 tipc_port_list_add(dports, publ->ref);
968edbe1 729 publ = publ->node_list_next;
b52124a5 730 } while (publ != info->node_list);
968edbe1
AS
731 }
732
b52124a5 733 if (info->cluster_list_size != info->node_list_size)
968edbe1 734 res = 1;
b97bf3fd
PL
735 }
736
737 spin_unlock_bh(&seq->lock);
738exit:
4323add6 739 read_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
740 return res;
741}
742
743/**
4323add6 744 * tipc_nametbl_publish_rsv - publish port name using a reserved name type
b97bf3fd
PL
745 */
746
c4307285 747int tipc_nametbl_publish_rsv(u32 ref, unsigned int scope,
b97bf3fd
PL
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/**
4323add6 759 * tipc_nametbl_publish - add name publication to network name tables
b97bf3fd
PL
760 */
761
c4307285 762struct publication *tipc_nametbl_publish(u32 type, u32 lower, u32 upper,
b97bf3fd
PL
763 u32 scope, u32 port_ref, u32 key)
764{
765 struct publication *publ;
766
767 if (table.local_publ_count >= tipc_max_publications) {
c4307285 768 warn("Publication failed, local publication limit reached (%u)\n",
b97bf3fd 769 tipc_max_publications);
1fc54d8f 770 return NULL;
b97bf3fd
PL
771 }
772 if ((type < TIPC_RESERVED_TYPES) && !atomic_read(&rsv_publ_ok)) {
f131072c 773 warn("Publication failed, reserved name {%u,%u,%u}\n",
b97bf3fd 774 type, lower, upper);
1fc54d8f 775 return NULL;
b97bf3fd
PL
776 }
777
4323add6 778 write_lock_bh(&tipc_nametbl_lock);
b97bf3fd 779 table.local_publ_count++;
4323add6 780 publ = tipc_nametbl_insert_publ(type, lower, upper, scope,
b97bf3fd 781 tipc_own_addr, port_ref, key);
a016892c 782 if (publ && (scope != TIPC_NODE_SCOPE))
4323add6 783 tipc_named_publish(publ);
4323add6 784 write_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
785 return publ;
786}
787
788/**
4323add6 789 * tipc_nametbl_withdraw - withdraw name publication from network name tables
b97bf3fd
PL
790 */
791
4323add6 792int tipc_nametbl_withdraw(u32 type, u32 lower, u32 ref, u32 key)
b97bf3fd
PL
793{
794 struct publication *publ;
795
4323add6
PL
796 write_lock_bh(&tipc_nametbl_lock);
797 publ = tipc_nametbl_remove_publ(type, lower, tipc_own_addr, ref, key);
f131072c 798 if (likely(publ)) {
b97bf3fd
PL
799 table.local_publ_count--;
800 if (publ->scope != TIPC_NODE_SCOPE)
4323add6
PL
801 tipc_named_withdraw(publ);
802 write_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
803 list_del_init(&publ->pport_list);
804 kfree(publ);
805 return 1;
806 }
4323add6 807 write_unlock_bh(&tipc_nametbl_lock);
f131072c
AS
808 err("Unable to remove local publication\n"
809 "(type=%u, lower=%u, ref=%u, key=%u)\n",
810 type, lower, ref, key);
b97bf3fd
PL
811 return 0;
812}
813
814/**
4323add6 815 * tipc_nametbl_subscribe - add a subscription object to the name table
b97bf3fd
PL
816 */
817
f131072c 818void tipc_nametbl_subscribe(struct subscription *s)
b97bf3fd
PL
819{
820 u32 type = s->seq.type;
821 struct name_seq *seq;
822
c4307285 823 write_lock_bh(&tipc_nametbl_lock);
b97bf3fd 824 seq = nametbl_find_seq(type);
a016892c 825 if (!seq)
4323add6 826 seq = tipc_nameseq_create(type, &table.types[hash(type)]);
0e65967e 827 if (seq) {
c4307285 828 spin_lock_bh(&seq->lock);
c4307285
YH
829 tipc_nameseq_subscribe(seq, s);
830 spin_unlock_bh(&seq->lock);
831 } else {
f131072c
AS
832 warn("Failed to create subscription for {%u,%u,%u}\n",
833 s->seq.type, s->seq.lower, s->seq.upper);
c4307285
YH
834 }
835 write_unlock_bh(&tipc_nametbl_lock);
b97bf3fd
PL
836}
837
838/**
4323add6 839 * tipc_nametbl_unsubscribe - remove a subscription object from name table
b97bf3fd
PL
840 */
841
f131072c 842void tipc_nametbl_unsubscribe(struct subscription *s)
b97bf3fd
PL
843{
844 struct name_seq *seq;
845
c4307285
YH
846 write_lock_bh(&tipc_nametbl_lock);
847 seq = nametbl_find_seq(s->seq.type);
0e65967e 848 if (seq != NULL) {
c4307285
YH
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);
b97bf3fd
PL
859}
860
861
862/**
863 * subseq_list: print specified sub-sequence contents into the given buffer
864 */
865
866static void subseq_list(struct sub_seq *sseq, struct print_buf *buf, u32 depth,
867 u32 index)
868{
869 char portIdStr[27];
c2de5814 870 const char *scope_str[] = {"", " zone", " cluster", " node"};
b52124a5 871 struct publication *publ = sseq->info->zone_list;
b97bf3fd
PL
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 {
0e65967e 881 sprintf(portIdStr, "<%u.%u.%u:%u>",
b97bf3fd
PL
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) {
c2de5814
AS
886 tipc_printf(buf, "%-10u %s", publ->key,
887 scope_str[publ->scope]);
b97bf3fd
PL
888 }
889
890 publ = publ->zone_list_next;
b52124a5 891 if (publ == sseq->info->zone_list)
b97bf3fd
PL
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
904static 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
0f15d364
AS
910 if (seq->first_free == 0)
911 return;
912
b97bf3fd
PL
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);
307fdf5e 923 spin_lock_bh(&seq->lock);
b97bf3fd 924 subseq_list(sseq, buf, depth, index);
307fdf5e 925 spin_unlock_bh(&seq->lock);
b97bf3fd
PL
926 sprintf(typearea, "%10s", " ");
927 }
928 }
929}
930
931/**
932 * nametbl_header - print name table header into the given buffer
933 */
934
935static void nametbl_header(struct print_buf *buf, u32 depth)
936{
c2de5814
AS
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]);
b97bf3fd
PL
950 tipc_printf(buf, "\n");
951}
952
953/**
954 * nametbl_list - print specified name table contents into the given buffer
955 */
956
c4307285 957static void nametbl_list(struct print_buf *buf, u32 depth_info,
b97bf3fd
PL
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) {
c4307285 981 nameseq_list(seq, buf, depth, seq->type,
b97bf3fd
PL
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) {
c4307285 996 nameseq_list(seq, buf, depth, type,
b97bf3fd
PL
997 lowbound, upbound, i);
998 break;
999 }
1000 }
1001 }
1002}
1003
b97bf3fd
PL
1004#define MAX_NAME_TBL_QUERY 32768
1005
4323add6 1006struct sk_buff *tipc_nametbl_get(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
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))
4323add6 1015 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 1016
4323add6 1017 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_NAME_TBL_QUERY));
b97bf3fd
PL
1018 if (!buf)
1019 return NULL;
1020
1021 rep_tlv = (struct tlv_desc *)buf->data;
4323add6 1022 tipc_printbuf_init(&b, TLV_DATA(rep_tlv), MAX_NAME_TBL_QUERY);
b97bf3fd 1023 argv = (struct tipc_name_table_query *)TLV_DATA(req_tlv_area);
4323add6 1024 read_lock_bh(&tipc_nametbl_lock);
c4307285 1025 nametbl_list(&b, ntohl(argv->depth), ntohl(argv->type),
b97bf3fd 1026 ntohl(argv->lowbound), ntohl(argv->upbound));
4323add6
PL
1027 read_unlock_bh(&tipc_nametbl_lock);
1028 str_len = tipc_printbuf_validate(&b);
b97bf3fd
PL
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
4323add6 1036int tipc_nametbl_init(void)
b97bf3fd 1037{
4e3e6dcb
AS
1038 table.types = kcalloc(tipc_nametbl_size, sizeof(struct hlist_head),
1039 GFP_ATOMIC);
b97bf3fd
PL
1040 if (!table.types)
1041 return -ENOMEM;
1042
b97bf3fd 1043 table.local_publ_count = 0;
b97bf3fd
PL
1044 return 0;
1045}
1046
4323add6 1047void tipc_nametbl_stop(void)
b97bf3fd 1048{
b97bf3fd
PL
1049 u32 i;
1050
1051 if (!table.types)
1052 return;
1053
f131072c
AS
1054 /* Verify name table is empty, then release it */
1055
4323add6 1056 write_lock_bh(&tipc_nametbl_lock);
b97bf3fd 1057 for (i = 0; i < tipc_nametbl_size; i++) {
f131072c
AS
1058 if (!hlist_empty(&table.types[i]))
1059 err("tipc_nametbl_stop(): hash chain %u is non-null\n", i);
b97bf3fd
PL
1060 }
1061 kfree(table.types);
1062 table.types = NULL;
4323add6 1063 write_unlock_bh(&tipc_nametbl_lock);
b97bf3fd 1064}
f131072c 1065