radix-tree.c 19 KB

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