linux_dsm_epyc7002/arch/s390/numa/toptree.c

352 lines
8.8 KiB
C
Raw Normal View History

License cleanup: add SPDX GPL-2.0 license identifier to files with no license Many source files in the tree are missing licensing information, which makes it harder for compliance tools to determine the correct license. By default all files without license information are under the default license of the kernel, which is GPL version 2. Update the files which contain no license information with the 'GPL-2.0' SPDX license identifier. The SPDX identifier is a legally binding shorthand, which can be used instead of the full boiler plate text. This patch is based on work done by Thomas Gleixner and Kate Stewart and Philippe Ombredanne. How this work was done: Patches were generated and checked against linux-4.14-rc6 for a subset of the use cases: - file had no licensing information it it. - file was a */uapi/* one with no licensing information in it, - file was a */uapi/* one with existing licensing information, Further patches will be generated in subsequent months to fix up cases where non-standard license headers were used, and references to license had to be inferred by heuristics based on keywords. The analysis to determine which SPDX License Identifier to be applied to a file was done in a spreadsheet of side by side results from of the output of two independent scanners (ScanCode & Windriver) producing SPDX tag:value files created by Philippe Ombredanne. Philippe prepared the base worksheet, and did an initial spot review of a few 1000 files. The 4.13 kernel was the starting point of the analysis with 60,537 files assessed. Kate Stewart did a file by file comparison of the scanner results in the spreadsheet to determine which SPDX license identifier(s) to be applied to the file. She confirmed any determination that was not immediately clear with lawyers working with the Linux Foundation. Criteria used to select files for SPDX license identifier tagging was: - Files considered eligible had to be source code files. - Make and config files were included as candidates if they contained >5 lines of source - File already had some variant of a license header in it (even if <5 lines). All documentation files were explicitly excluded. The following heuristics were used to determine which SPDX license identifiers to apply. - when both scanners couldn't find any license traces, file was considered to have no license information in it, and the top level COPYING file license applied. For non */uapi/* files that summary was: SPDX license identifier # files ---------------------------------------------------|------- GPL-2.0 11139 and resulted in the first patch in this series. If that file was a */uapi/* path one, it was "GPL-2.0 WITH Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was: SPDX license identifier # files ---------------------------------------------------|------- GPL-2.0 WITH Linux-syscall-note 930 and resulted in the second patch in this series. - if a file had some form of licensing information in it, and was one of the */uapi/* ones, it was denoted with the Linux-syscall-note if any GPL family license was found in the file or had no licensing in it (per prior point). Results summary: SPDX license identifier # files ---------------------------------------------------|------ GPL-2.0 WITH Linux-syscall-note 270 GPL-2.0+ WITH Linux-syscall-note 169 ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21 ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17 LGPL-2.1+ WITH Linux-syscall-note 15 GPL-1.0+ WITH Linux-syscall-note 14 ((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5 LGPL-2.0+ WITH Linux-syscall-note 4 LGPL-2.1 WITH Linux-syscall-note 3 ((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3 ((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1 and that resulted in the third patch in this series. - when the two scanners agreed on the detected license(s), that became the concluded license(s). - when there was disagreement between the two scanners (one detected a license but the other didn't, or they both detected different licenses) a manual inspection of the file occurred. - In most cases a manual inspection of the information in the file resulted in a clear resolution of the license that should apply (and which scanner probably needed to revisit its heuristics). - When it was not immediately clear, the license identifier was confirmed with lawyers working with the Linux Foundation. - If there was any question as to the appropriate license identifier, the file was flagged for further research and to be revisited later in time. In total, over 70 hours of logged manual review was done on the spreadsheet to determine the SPDX license identifiers to apply to the source files by Kate, Philippe, Thomas and, in some cases, confirmation by lawyers working with the Linux Foundation. Kate also obtained a third independent scan of the 4.13 code base from FOSSology, and compared selected files where the other two scanners disagreed against that SPDX file, to see if there was new insights. The Windriver scanner is based on an older version of FOSSology in part, so they are related. Thomas did random spot checks in about 500 files from the spreadsheets for the uapi headers and agreed with SPDX license identifier in the files he inspected. For the non-uapi files Thomas did random spot checks in about 15000 files. In initial set of patches against 4.14-rc6, 3 files were found to have copy/paste license identifier errors, and have been fixed to reflect the correct identifier. Additionally Philippe spent 10 hours this week doing a detailed manual inspection and review of the 12,461 patched files from the initial patch version early this week with: - a full scancode scan run, collecting the matched texts, detected license ids and scores - reviewing anything where there was a license detected (about 500+ files) to ensure that the applied SPDX license was correct - reviewing anything where there was no detection but the patch license was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied SPDX license was correct This produced a worksheet with 20 files needing minor correction. This worksheet was then exported into 3 different .csv files for the different types of files to be modified. These .csv files were then reviewed by Greg. Thomas wrote a script to parse the csv files and add the proper SPDX tag to the file, in the format that the file expected. This script was further refined by Greg based on the output to detect more types of files automatically and to distinguish between header and source .c files (which need different comment types.) Finally Greg ran the script using the .csv files to generate the patches. Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org> Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 21:07:57 +07:00
// SPDX-License-Identifier: GPL-2.0
/*
* NUMA support for s390
*
* A tree structure used for machine topology mangling
*
* Copyright IBM Corp. 2015
*/
#include <linux/kernel.h>
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
#include <linux/bootmem.h>
#include <linux/cpumask.h>
#include <linux/list.h>
#include <linux/list_sort.h>
#include <linux/slab.h>
#include <asm/numa.h>
#include "toptree.h"
/**
* toptree_alloc - Allocate and initialize a new tree node.
* @level: The node's vertical level; level 0 contains the leaves.
* @id: ID number, explicitly not unique beyond scope of node's siblings
*
* Allocate a new tree node and initialize it.
*
* RETURNS:
* Pointer to the new tree node or NULL on error
*/
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
struct toptree __ref *toptree_alloc(int level, int id)
{
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
struct toptree *res;
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
if (slab_is_available())
res = kzalloc(sizeof(*res), GFP_KERNEL);
else
res = memblock_virt_alloc(sizeof(*res), 8);
if (!res)
return res;
INIT_LIST_HEAD(&res->children);
INIT_LIST_HEAD(&res->sibling);
cpumask_clear(&res->mask);
res->level = level;
res->id = id;
return res;
}
/**
* toptree_remove - Remove a tree node from a tree
* @cand: Pointer to the node to remove
*
* The node is detached from its parent node. The parent node's
* masks will be updated to reflect the loss of the child.
*/
static void toptree_remove(struct toptree *cand)
{
struct toptree *oldparent;
list_del_init(&cand->sibling);
oldparent = cand->parent;
cand->parent = NULL;
toptree_update_mask(oldparent);
}
/**
* toptree_free - discard a tree node
* @cand: Pointer to the tree node to discard
*
* Checks if @cand is attached to a parent node. Detaches it
* cleanly using toptree_remove. Possible children are freed
* recursively. In the end @cand itself is freed.
*/
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
void __ref toptree_free(struct toptree *cand)
{
struct toptree *child, *tmp;
if (cand->parent)
toptree_remove(cand);
toptree_for_each_child_safe(child, tmp, cand)
toptree_free(child);
s390/numa: establish cpu to node mapping early Initialize the cpu topology and therefore also the cpu to node mapping much earlier. Fixes this warning and subsequent crashes when using the fake numa emulation mode on s390: WARNING: CPU: 0 PID: 1 at include/linux/cpumask.h:121 select_task_rq+0xe6/0x1a8 CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.6.0-rc6-00001-ge9d867a67fd0-dirty #28 task: 00000001dd270008 ti: 00000001eccb4000 task.ti: 00000001eccb4000 Krnl PSW : 0404c00180000000 0000000000176c56 (select_task_rq+0xe6/0x1a8) R:0 T:1 IO:0 EX:0 Key:0 M:1 W:0 P:0 AS:3 CC:0 PM:0 RI:0 EA:3 Call Trace: ([<0000000000176c30>] select_task_rq+0xc0/0x1a8) ([<0000000000177d64>] try_to_wake_up+0x2e4/0x478) ([<000000000015d46c>] create_worker+0x174/0x1c0) ([<0000000000161a98>] alloc_unbound_pwq+0x360/0x438) ([<0000000000162550>] apply_wqattrs_prepare+0x200/0x2a0) ([<000000000016266a>] apply_workqueue_attrs_locked+0x7a/0xb0) ([<0000000000162af0>] apply_workqueue_attrs+0x50/0x78) ([<000000000016441c>] __alloc_workqueue_key+0x304/0x520) ([<0000000000ee3706>] default_bdi_init+0x3e/0x70) ([<0000000000100270>] do_one_initcall+0x140/0x1d8) ([<0000000000ec9da8>] kernel_init_freeable+0x220/0x2d8) ([<0000000000984a7a>] kernel_init+0x2a/0x150) ([<00000000009913fa>] kernel_thread_starter+0x6/0xc) ([<00000000009913f4>] kernel_thread_starter+0x0/0xc) Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
2016-12-03 15:50:21 +07:00
if (slab_is_available())
kfree(cand);
else
memblock_free_early((unsigned long)cand, sizeof(*cand));
}
/**
* toptree_update_mask - Update node bitmasks
* @cand: Pointer to a tree node
*
* The node's cpumask will be updated by combining all children's
* masks. Then toptree_update_mask is called recursively for the
* parent if applicable.
*
* NOTE:
* This must not be called on leaves. If called on a leaf, its
* CPU mask is cleared and lost.
*/
void toptree_update_mask(struct toptree *cand)
{
struct toptree *child;
cpumask_clear(&cand->mask);
list_for_each_entry(child, &cand->children, sibling)
cpumask_or(&cand->mask, &cand->mask, &child->mask);
if (cand->parent)
toptree_update_mask(cand->parent);
}
/**
* toptree_insert - Insert a tree node into tree
* @cand: Pointer to the node to insert
* @target: Pointer to the node to which @cand will added as a child
*
* Insert a tree node into a tree. Masks will be updated automatically.
*
* RETURNS:
* 0 on success, -1 if NULL is passed as argument or the node levels
* don't fit.
*/
static int toptree_insert(struct toptree *cand, struct toptree *target)
{
if (!cand || !target)
return -1;
if (target->level != (cand->level + 1))
return -1;
list_add_tail(&cand->sibling, &target->children);
cand->parent = target;
toptree_update_mask(target);
return 0;
}
/**
* toptree_move_children - Move all child nodes of a node to a new place
* @cand: Pointer to the node whose children are to be moved
* @target: Pointer to the node to which @cand's children will be attached
*
* Take all child nodes of @cand and move them using toptree_move.
*/
static void toptree_move_children(struct toptree *cand, struct toptree *target)
{
struct toptree *child, *tmp;
toptree_for_each_child_safe(child, tmp, cand)
toptree_move(child, target);
}
/**
* toptree_unify - Merge children with same ID
* @cand: Pointer to node whose direct children should be made unique
*
* When mangling the tree it is possible that a node has two or more children
* which have the same ID. This routine merges these children into one and
* moves all children of the merged nodes into the unified node.
*/
void toptree_unify(struct toptree *cand)
{
struct toptree *child, *tmp, *cand_copy;
/* Threads cannot be split, cores are not split */
if (cand->level < 2)
return;
cand_copy = toptree_alloc(cand->level, 0);
toptree_for_each_child_safe(child, tmp, cand) {
struct toptree *tmpchild;
if (!cpumask_empty(&child->mask)) {
tmpchild = toptree_get_child(cand_copy, child->id);
toptree_move_children(child, tmpchild);
}
toptree_free(child);
}
toptree_move_children(cand_copy, cand);
toptree_free(cand_copy);
toptree_for_each_child(child, cand)
toptree_unify(child);
}
/**
* toptree_move - Move a node to another context
* @cand: Pointer to the node to move
* @target: Pointer to the node where @cand should go
*
* In the easiest case @cand is exactly on the level below @target
* and will be immediately moved to the target.
*
* If @target's level is not the direct parent level of @cand,
* nodes for the missing levels are created and put between
* @cand and @target. The "stacking" nodes' IDs are taken from
* @cand's parents.
*
* After this it is likely to have redundant nodes in the tree
* which are addressed by means of toptree_unify.
*/
void toptree_move(struct toptree *cand, struct toptree *target)
{
struct toptree *stack_target, *real_insert_point, *ptr, *tmp;
if (cand->level + 1 == target->level) {
toptree_remove(cand);
toptree_insert(cand, target);
return;
}
real_insert_point = NULL;
ptr = cand;
stack_target = NULL;
do {
tmp = stack_target;
stack_target = toptree_alloc(ptr->level + 1,
ptr->parent->id);
toptree_insert(tmp, stack_target);
if (!real_insert_point)
real_insert_point = stack_target;
ptr = ptr->parent;
} while (stack_target->level < (target->level - 1));
toptree_remove(cand);
toptree_insert(cand, real_insert_point);
toptree_insert(stack_target, target);
}
/**
* toptree_get_child - Access a tree node's child by its ID
* @cand: Pointer to tree node whose child is to access
* @id: The desired child's ID
*
* @cand's children are searched for a child with matching ID.
* If no match can be found, a new child with the desired ID
* is created and returned.
*/
struct toptree *toptree_get_child(struct toptree *cand, int id)
{
struct toptree *child;
toptree_for_each_child(child, cand)
if (child->id == id)
return child;
child = toptree_alloc(cand->level-1, id);
toptree_insert(child, cand);
return child;
}
/**
* toptree_first - Find the first descendant on specified level
* @context: Pointer to tree node whose descendants are to be used
* @level: The level of interest
*
* RETURNS:
* @context's first descendant on the specified level, or NULL
* if there is no matching descendant
*/
struct toptree *toptree_first(struct toptree *context, int level)
{
struct toptree *child, *tmp;
if (context->level == level)
return context;
if (!list_empty(&context->children)) {
list_for_each_entry(child, &context->children, sibling) {
tmp = toptree_first(child, level);
if (tmp)
return tmp;
}
}
return NULL;
}
/**
* toptree_next_sibling - Return next sibling
* @cur: Pointer to a tree node
*
* RETURNS:
* If @cur has a parent and is not the last in the parent's children list,
* the next sibling is returned. Or NULL when there are no siblings left.
*/
static struct toptree *toptree_next_sibling(struct toptree *cur)
{
if (cur->parent == NULL)
return NULL;
if (cur == list_last_entry(&cur->parent->children,
struct toptree, sibling))
return NULL;
return (struct toptree *) list_next_entry(cur, sibling);
}
/**
* toptree_next - Tree traversal function
* @cur: Pointer to current element
* @context: Pointer to the root node of the tree or subtree to
* be traversed.
* @level: The level of interest.
*
* RETURNS:
* Pointer to the next node on level @level
* or NULL when there is no next node.
*/
struct toptree *toptree_next(struct toptree *cur, struct toptree *context,
int level)
{
struct toptree *cur_context, *tmp;
if (!cur)
return NULL;
if (context->level == level)
return NULL;
tmp = toptree_next_sibling(cur);
if (tmp != NULL)
return tmp;
cur_context = cur;
while (cur_context->level < context->level - 1) {
/* Step up */
cur_context = cur_context->parent;
/* Step aside */
tmp = toptree_next_sibling(cur_context);
if (tmp != NULL) {
/* Step down */
tmp = toptree_first(tmp, level);
if (tmp != NULL)
return tmp;
}
}
return NULL;
}
/**
* toptree_count - Count descendants on specified level
* @context: Pointer to node whose descendants are to be considered
* @level: Only descendants on the specified level will be counted
*
* RETURNS:
* Number of descendants on the specified level
*/
int toptree_count(struct toptree *context, int level)
{
struct toptree *cur;
int cnt = 0;
toptree_for_each(cur, context, level)
cnt++;
return cnt;
}