radix-tree.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Copyright (C) 2001 Momchil Velikov
  3. * Portions Copyright (C) 2001 Christoph Hellwig
  4. * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "kerncompat.h"
  21. #include "radix-tree.h"
  22. #ifdef __KERNEL__
  23. #define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  24. #else
  25. #define RADIX_TREE_MAP_SHIFT 3 /* For more stressful testing */
  26. #endif
  27. #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
  28. #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
  29. #define RADIX_TREE_TAG_LONGS \
  30. ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
  31. struct radix_tree_node {
  32. unsigned int count;
  33. void *slots[RADIX_TREE_MAP_SIZE];
  34. unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS];
  35. };
  36. struct radix_tree_path {
  37. struct radix_tree_node *node;
  38. int offset;
  39. };
  40. #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
  41. #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
  42. static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH] __read_mostly;
  43. /*
  44. * Per-cpu pool of preloaded nodes
  45. */
  46. struct radix_tree_preload {
  47. int nr;
  48. struct radix_tree_node *nodes[RADIX_TREE_MAX_PATH];
  49. };
  50. struct radix_tree_preload radix_tree_preloads = { 0, };
  51. static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
  52. {
  53. return root->gfp_mask & __GFP_BITS_MASK;
  54. }
  55. static int internal_nodes = 0;
  56. /*
  57. * This assumes that the caller has performed appropriate preallocation, and
  58. * that the caller has pinned this thread of control to the current CPU.
  59. */
  60. static struct radix_tree_node *
  61. radix_tree_node_alloc(struct radix_tree_root *root)
  62. {
  63. struct radix_tree_node *ret;
  64. ret = malloc(sizeof(struct radix_tree_node));
  65. if (ret) {
  66. memset(ret, 0, sizeof(struct radix_tree_node));
  67. internal_nodes++;
  68. }
  69. return ret;
  70. }
  71. static inline void
  72. radix_tree_node_free(struct radix_tree_node *node)
  73. {
  74. internal_nodes--;
  75. free(node);
  76. }
  77. /*
  78. * Load up this CPU's radix_tree_node buffer with sufficient objects to
  79. * ensure that the addition of a single element in the tree cannot fail. On
  80. * success, return zero, with preemption disabled. On error, return -ENOMEM
  81. * with preemption not disabled.
  82. */
  83. int radix_tree_preload(gfp_t gfp_mask)
  84. {
  85. struct radix_tree_preload *rtp;
  86. struct radix_tree_node *node;
  87. int ret = -ENOMEM;
  88. preempt_disable();
  89. rtp = &__get_cpu_var(radix_tree_preloads);
  90. while (rtp->nr < ARRAY_SIZE(rtp->nodes)) {
  91. preempt_enable();
  92. node = radix_tree_node_alloc(NULL);
  93. if (node == NULL)
  94. goto out;
  95. preempt_disable();
  96. rtp = &__get_cpu_var(radix_tree_preloads);
  97. if (rtp->nr < ARRAY_SIZE(rtp->nodes))
  98. rtp->nodes[rtp->nr++] = node;
  99. else
  100. radix_tree_node_free(node);
  101. }
  102. ret = 0;
  103. out:
  104. return ret;
  105. }
  106. static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
  107. int offset)
  108. {
  109. __set_bit(offset, node->tags[tag]);
  110. }
  111. static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
  112. int offset)
  113. {
  114. __clear_bit(offset, node->tags[tag]);
  115. }
  116. static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
  117. int offset)
  118. {
  119. return test_bit(offset, node->tags[tag]);
  120. }
  121. static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
  122. {
  123. root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
  124. }
  125. static inline void root_tag_clear(struct radix_tree_root *root, unsigned int tag)
  126. {
  127. root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
  128. }
  129. static inline void root_tag_clear_all(struct radix_tree_root *root)
  130. {
  131. root->gfp_mask &= __GFP_BITS_MASK;
  132. }
  133. static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
  134. {
  135. return (__force unsigned)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
  136. }
  137. /*
  138. * Returns 1 if any slot in the node has this tag set.
  139. * Otherwise returns 0.
  140. */
  141. static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
  142. {
  143. int idx;
  144. for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
  145. if (node->tags[tag][idx])
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. /*
  151. * Return the maximum key which can be store into a
  152. * radix tree with height HEIGHT.
  153. */
  154. static inline unsigned long radix_tree_maxindex(unsigned int height)
  155. {
  156. return height_to_maxindex[height];
  157. }
  158. /*
  159. * Extend a radix tree so it can store key @index.
  160. */
  161. static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
  162. {
  163. struct radix_tree_node *node;
  164. unsigned int height;
  165. int tag;
  166. /* Figure out what the height should be. */
  167. height = root->height + 1;
  168. while (index > radix_tree_maxindex(height))
  169. height++;
  170. if (root->rnode == NULL) {
  171. root->height = height;
  172. goto out;
  173. }
  174. do {
  175. if (!(node = radix_tree_node_alloc(root)))
  176. return -ENOMEM;
  177. /* Increase the height. */
  178. node->slots[0] = root->rnode;
  179. /* Propagate the aggregated tag info into the new root */
  180. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  181. if (root_tag_get(root, tag))
  182. tag_set(node, tag, 0);
  183. }
  184. node->count = 1;
  185. root->rnode = node;
  186. root->height++;
  187. } while (height > root->height);
  188. out:
  189. return 0;
  190. }
  191. /**
  192. * radix_tree_insert - insert into a radix tree
  193. * @root: radix tree root
  194. * @index: index key
  195. * @item: item to insert
  196. *
  197. * Insert an item into the radix tree at position @index.
  198. */
  199. int radix_tree_insert(struct radix_tree_root *root,
  200. unsigned long index, void *item)
  201. {
  202. struct radix_tree_node *node = NULL, *slot;
  203. unsigned int height, shift;
  204. int offset;
  205. int error;
  206. /* Make sure the tree is high enough. */
  207. if (index > radix_tree_maxindex(root->height)) {
  208. error = radix_tree_extend(root, index);
  209. if (error)
  210. return error;
  211. }
  212. slot = root->rnode;
  213. height = root->height;
  214. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  215. offset = 0; /* uninitialised var warning */
  216. while (height > 0) {
  217. if (slot == NULL) {
  218. /* Have to add a child node. */
  219. if (!(slot = radix_tree_node_alloc(root)))
  220. return -ENOMEM;
  221. if (node) {
  222. node->slots[offset] = slot;
  223. node->count++;
  224. } else
  225. root->rnode = slot;
  226. }
  227. /* Go a level down */
  228. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  229. node = slot;
  230. slot = node->slots[offset];
  231. shift -= RADIX_TREE_MAP_SHIFT;
  232. height--;
  233. }
  234. if (slot != NULL)
  235. return -EEXIST;
  236. if (node) {
  237. node->count++;
  238. node->slots[offset] = item;
  239. BUG_ON(tag_get(node, 0, offset));
  240. BUG_ON(tag_get(node, 1, offset));
  241. } else {
  242. root->rnode = item;
  243. BUG_ON(root_tag_get(root, 0));
  244. BUG_ON(root_tag_get(root, 1));
  245. }
  246. return 0;
  247. }
  248. static inline void **__lookup_slot(struct radix_tree_root *root,
  249. unsigned long index)
  250. {
  251. unsigned int height, shift;
  252. struct radix_tree_node **slot;
  253. height = root->height;
  254. if (index > radix_tree_maxindex(height))
  255. return NULL;
  256. if (height == 0 && root->rnode)
  257. return (void **)&root->rnode;
  258. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  259. slot = &root->rnode;
  260. while (height > 0) {
  261. if (*slot == NULL)
  262. return NULL;
  263. slot = (struct radix_tree_node **)
  264. ((*slot)->slots +
  265. ((index >> shift) & RADIX_TREE_MAP_MASK));
  266. shift -= RADIX_TREE_MAP_SHIFT;
  267. height--;
  268. }
  269. return (void **)slot;
  270. }
  271. /**
  272. * radix_tree_lookup_slot - lookup a slot in a radix tree
  273. * @root: radix tree root
  274. * @index: index key
  275. *
  276. * Lookup the slot corresponding to the position @index in the radix tree
  277. * @root. This is useful for update-if-exists operations.
  278. */
  279. void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
  280. {
  281. return __lookup_slot(root, index);
  282. }
  283. /**
  284. * radix_tree_lookup - perform lookup operation on a radix tree
  285. * @root: radix tree root
  286. * @index: index key
  287. *
  288. * Lookup the item at the position @index in the radix tree @root.
  289. */
  290. void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
  291. {
  292. void **slot;
  293. slot = __lookup_slot(root, index);
  294. return slot != NULL ? *slot : NULL;
  295. }
  296. /**
  297. * radix_tree_tag_set - set a tag on a radix tree node
  298. * @root: radix tree root
  299. * @index: index key
  300. * @tag: tag index
  301. *
  302. * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
  303. * corresponding to @index in the radix tree. From
  304. * the root all the way down to the leaf node.
  305. *
  306. * Returns the address of the tagged item. Setting a tag on a not-present
  307. * item is a bug.
  308. */
  309. void *radix_tree_tag_set(struct radix_tree_root *root,
  310. unsigned long index, unsigned int tag)
  311. {
  312. unsigned int height, shift;
  313. struct radix_tree_node *slot;
  314. height = root->height;
  315. BUG_ON(index > radix_tree_maxindex(height));
  316. slot = root->rnode;
  317. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  318. while (height > 0) {
  319. int offset;
  320. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  321. if (!tag_get(slot, tag, offset))
  322. tag_set(slot, tag, offset);
  323. slot = slot->slots[offset];
  324. BUG_ON(slot == NULL);
  325. shift -= RADIX_TREE_MAP_SHIFT;
  326. height--;
  327. }
  328. /* set the root's tag bit */
  329. if (slot && !root_tag_get(root, tag))
  330. root_tag_set(root, tag);
  331. return slot;
  332. }
  333. /**
  334. * radix_tree_tag_clear - clear a tag on a radix tree node
  335. * @root: radix tree root
  336. * @index: index key
  337. * @tag: tag index
  338. *
  339. * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
  340. * corresponding to @index in the radix tree. If
  341. * this causes the leaf node to have no tags set then clear the tag in the
  342. * next-to-leaf node, etc.
  343. *
  344. * Returns the address of the tagged item on success, else NULL. ie:
  345. * has the same return value and semantics as radix_tree_lookup().
  346. */
  347. void *radix_tree_tag_clear(struct radix_tree_root *root,
  348. unsigned long index, unsigned int tag)
  349. {
  350. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  351. struct radix_tree_node *slot = NULL;
  352. unsigned int height, shift;
  353. height = root->height;
  354. if (index > radix_tree_maxindex(height))
  355. goto out;
  356. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  357. pathp->node = NULL;
  358. slot = root->rnode;
  359. while (height > 0) {
  360. int offset;
  361. if (slot == NULL)
  362. goto out;
  363. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  364. pathp[1].offset = offset;
  365. pathp[1].node = slot;
  366. slot = slot->slots[offset];
  367. pathp++;
  368. shift -= RADIX_TREE_MAP_SHIFT;
  369. height--;
  370. }
  371. if (slot == NULL)
  372. goto out;
  373. while (pathp->node) {
  374. if (!tag_get(pathp->node, tag, pathp->offset))
  375. goto out;
  376. tag_clear(pathp->node, tag, pathp->offset);
  377. if (any_tag_set(pathp->node, tag))
  378. goto out;
  379. pathp--;
  380. }
  381. /* clear the root's tag bit */
  382. if (root_tag_get(root, tag))
  383. root_tag_clear(root, tag);
  384. out:
  385. return slot;
  386. }
  387. #ifndef __KERNEL__ /* Only the test harness uses this at present */
  388. /**
  389. * radix_tree_tag_get - get a tag on a radix tree node
  390. * @root: radix tree root
  391. * @index: index key
  392. * @tag: tag index (< RADIX_TREE_MAX_TAGS)
  393. *
  394. * Return values:
  395. *
  396. * 0: tag not present or not set
  397. * 1: tag set
  398. */
  399. int radix_tree_tag_get(struct radix_tree_root *root,
  400. unsigned long index, unsigned int tag)
  401. {
  402. unsigned int height, shift;
  403. struct radix_tree_node *slot;
  404. int saw_unset_tag = 0;
  405. height = root->height;
  406. if (index > radix_tree_maxindex(height))
  407. return 0;
  408. /* check the root's tag bit */
  409. if (!root_tag_get(root, tag))
  410. return 0;
  411. if (height == 0)
  412. return 1;
  413. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  414. slot = root->rnode;
  415. for ( ; ; ) {
  416. int offset;
  417. if (slot == NULL)
  418. return 0;
  419. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  420. /*
  421. * This is just a debug check. Later, we can bale as soon as
  422. * we see an unset tag.
  423. */
  424. if (!tag_get(slot, tag, offset))
  425. saw_unset_tag = 1;
  426. if (height == 1) {
  427. int ret = tag_get(slot, tag, offset);
  428. BUG_ON(ret && saw_unset_tag);
  429. return !!ret;
  430. }
  431. slot = slot->slots[offset];
  432. shift -= RADIX_TREE_MAP_SHIFT;
  433. height--;
  434. }
  435. }
  436. #endif
  437. static unsigned int
  438. __lookup(struct radix_tree_root *root, void **results, unsigned long index,
  439. unsigned int max_items, unsigned long *next_index)
  440. {
  441. unsigned int nr_found = 0;
  442. unsigned int shift, height;
  443. struct radix_tree_node *slot;
  444. unsigned long i;
  445. height = root->height;
  446. if (height == 0) {
  447. if (root->rnode && index == 0)
  448. results[nr_found++] = root->rnode;
  449. goto out;
  450. }
  451. shift = (height-1) * RADIX_TREE_MAP_SHIFT;
  452. slot = root->rnode;
  453. for ( ; height > 1; height--) {
  454. for (i = (index >> shift) & RADIX_TREE_MAP_MASK ;
  455. i < RADIX_TREE_MAP_SIZE; i++) {
  456. if (slot->slots[i] != NULL)
  457. break;
  458. index &= ~((1UL << shift) - 1);
  459. index += 1UL << shift;
  460. if (index == 0)
  461. goto out; /* 32-bit wraparound */
  462. }
  463. if (i == RADIX_TREE_MAP_SIZE)
  464. goto out;
  465. shift -= RADIX_TREE_MAP_SHIFT;
  466. slot = slot->slots[i];
  467. }
  468. /* Bottom level: grab some items */
  469. for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
  470. index++;
  471. if (slot->slots[i]) {
  472. results[nr_found++] = slot->slots[i];
  473. if (nr_found == max_items)
  474. goto out;
  475. }
  476. }
  477. out:
  478. *next_index = index;
  479. return nr_found;
  480. }
  481. /**
  482. * radix_tree_gang_lookup - perform multiple lookup on a radix tree
  483. * @root: radix tree root
  484. * @results: where the results of the lookup are placed
  485. * @first_index: start the lookup from this key
  486. * @max_items: place up to this many items at *results
  487. *
  488. * Performs an index-ascending scan of the tree for present items. Places
  489. * them at *@results and returns the number of items which were placed at
  490. * *@results.
  491. *
  492. * The implementation is naive.
  493. */
  494. unsigned int
  495. radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
  496. unsigned long first_index, unsigned int max_items)
  497. {
  498. const unsigned long max_index = radix_tree_maxindex(root->height);
  499. unsigned long cur_index = first_index;
  500. unsigned int ret = 0;
  501. while (ret < max_items) {
  502. unsigned int nr_found;
  503. unsigned long next_index; /* Index of next search */
  504. if (cur_index > max_index)
  505. break;
  506. nr_found = __lookup(root, results + ret, cur_index,
  507. max_items - ret, &next_index);
  508. ret += nr_found;
  509. if (next_index == 0)
  510. break;
  511. cur_index = next_index;
  512. }
  513. return ret;
  514. }
  515. /*
  516. * FIXME: the two tag_get()s here should use find_next_bit() instead of
  517. * open-coding the search.
  518. */
  519. static unsigned int
  520. __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index,
  521. unsigned int max_items, unsigned long *next_index, unsigned int tag)
  522. {
  523. unsigned int nr_found = 0;
  524. unsigned int shift;
  525. unsigned int height = root->height;
  526. struct radix_tree_node *slot;
  527. if (height == 0) {
  528. if (root->rnode && index == 0)
  529. results[nr_found++] = root->rnode;
  530. goto out;
  531. }
  532. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  533. slot = root->rnode;
  534. do {
  535. unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK;
  536. for ( ; i < RADIX_TREE_MAP_SIZE; i++) {
  537. if (tag_get(slot, tag, i)) {
  538. BUG_ON(slot->slots[i] == NULL);
  539. break;
  540. }
  541. index &= ~((1UL << shift) - 1);
  542. index += 1UL << shift;
  543. if (index == 0)
  544. goto out; /* 32-bit wraparound */
  545. }
  546. if (i == RADIX_TREE_MAP_SIZE)
  547. goto out;
  548. height--;
  549. if (height == 0) { /* Bottom level: grab some items */
  550. unsigned long j = index & RADIX_TREE_MAP_MASK;
  551. for ( ; j < RADIX_TREE_MAP_SIZE; j++) {
  552. index++;
  553. if (tag_get(slot, tag, j)) {
  554. BUG_ON(slot->slots[j] == NULL);
  555. results[nr_found++] = slot->slots[j];
  556. if (nr_found == max_items)
  557. goto out;
  558. }
  559. }
  560. }
  561. shift -= RADIX_TREE_MAP_SHIFT;
  562. slot = slot->slots[i];
  563. } while (height > 0);
  564. out:
  565. *next_index = index;
  566. return nr_found;
  567. }
  568. /**
  569. * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
  570. * based on a tag
  571. * @root: radix tree root
  572. * @results: where the results of the lookup are placed
  573. * @first_index: start the lookup from this key
  574. * @max_items: place up to this many items at *results
  575. * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
  576. *
  577. * Performs an index-ascending scan of the tree for present items which
  578. * have the tag indexed by @tag set. Places the items at *@results and
  579. * returns the number of items which were placed at *@results.
  580. */
  581. unsigned int
  582. radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
  583. unsigned long first_index, unsigned int max_items,
  584. unsigned int tag)
  585. {
  586. const unsigned long max_index = radix_tree_maxindex(root->height);
  587. unsigned long cur_index = first_index;
  588. unsigned int ret = 0;
  589. /* check the root's tag bit */
  590. if (!root_tag_get(root, tag))
  591. return 0;
  592. while (ret < max_items) {
  593. unsigned int nr_found;
  594. unsigned long next_index; /* Index of next search */
  595. if (cur_index > max_index)
  596. break;
  597. nr_found = __lookup_tag(root, results + ret, cur_index,
  598. max_items - ret, &next_index, tag);
  599. ret += nr_found;
  600. if (next_index == 0)
  601. break;
  602. cur_index = next_index;
  603. }
  604. return ret;
  605. }
  606. /**
  607. * radix_tree_shrink - shrink height of a radix tree to minimal
  608. * @root radix tree root
  609. */
  610. static inline void radix_tree_shrink(struct radix_tree_root *root)
  611. {
  612. /* try to shrink tree height */
  613. while (root->height > 0 &&
  614. root->rnode->count == 1 &&
  615. root->rnode->slots[0]) {
  616. struct radix_tree_node *to_free = root->rnode;
  617. root->rnode = to_free->slots[0];
  618. root->height--;
  619. /* must only free zeroed nodes into the slab */
  620. tag_clear(to_free, 0, 0);
  621. tag_clear(to_free, 1, 0);
  622. to_free->slots[0] = NULL;
  623. to_free->count = 0;
  624. radix_tree_node_free(to_free);
  625. }
  626. }
  627. /**
  628. * radix_tree_delete - delete an item from a radix tree
  629. * @root: radix tree root
  630. * @index: index key
  631. *
  632. * Remove the item at @index from the radix tree rooted at @root.
  633. *
  634. * Returns the address of the deleted item, or NULL if it was not present.
  635. */
  636. void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
  637. {
  638. struct radix_tree_path path[RADIX_TREE_MAX_PATH], *pathp = path;
  639. struct radix_tree_node *slot = NULL;
  640. unsigned int height, shift;
  641. int tag;
  642. int offset;
  643. height = root->height;
  644. if (index > radix_tree_maxindex(height))
  645. goto out;
  646. slot = root->rnode;
  647. if (height == 0 && root->rnode) {
  648. root_tag_clear_all(root);
  649. root->rnode = NULL;
  650. goto out;
  651. }
  652. shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
  653. pathp->node = NULL;
  654. do {
  655. if (slot == NULL)
  656. goto out;
  657. pathp++;
  658. offset = (index >> shift) & RADIX_TREE_MAP_MASK;
  659. pathp->offset = offset;
  660. pathp->node = slot;
  661. slot = slot->slots[offset];
  662. shift -= RADIX_TREE_MAP_SHIFT;
  663. height--;
  664. } while (height > 0);
  665. if (slot == NULL)
  666. goto out;
  667. /*
  668. * Clear all tags associated with the just-deleted item
  669. */
  670. for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
  671. if (tag_get(pathp->node, tag, pathp->offset))
  672. radix_tree_tag_clear(root, index, tag);
  673. }
  674. /* Now free the nodes we do not need anymore */
  675. while (pathp->node) {
  676. pathp->node->slots[pathp->offset] = NULL;
  677. pathp->node->count--;
  678. if (pathp->node->count) {
  679. if (pathp->node == root->rnode)
  680. radix_tree_shrink(root);
  681. goto out;
  682. }
  683. /* Node with zero slots in use so free it */
  684. radix_tree_node_free(pathp->node);
  685. pathp--;
  686. }
  687. root_tag_clear_all(root);
  688. root->height = 0;
  689. root->rnode = NULL;
  690. out:
  691. return slot;
  692. }
  693. /**
  694. * radix_tree_tagged - test whether any items in the tree are tagged
  695. * @root: radix tree root
  696. * @tag: tag to test
  697. */
  698. int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
  699. {
  700. return root_tag_get(root, tag);
  701. }
  702. static unsigned long __maxindex(unsigned int height)
  703. {
  704. unsigned int tmp = height * RADIX_TREE_MAP_SHIFT;
  705. unsigned long index = (~0UL >> (RADIX_TREE_INDEX_BITS - tmp - 1)) >> 1;
  706. if (tmp >= RADIX_TREE_INDEX_BITS)
  707. index = ~0UL;
  708. return index;
  709. }
  710. static void radix_tree_init_maxindex(void)
  711. {
  712. unsigned int i;
  713. for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
  714. height_to_maxindex[i] = __maxindex(i);
  715. }
  716. void radix_tree_init(void)
  717. {
  718. radix_tree_init_maxindex();
  719. }