radix-tree.c 19 KB

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