/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2013 EMC Corp.4* Copyright (c) 2011 Jeffrey Roberson <[email protected]>5* Copyright (c) 2008 Mayur Shardul <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#ifndef _VM_RADIX_H_31#define _VM_RADIX_H_3233#include <vm/_vm_radix.h>3435#ifdef _KERNEL36#include <sys/pctrie.h>37#include <vm/vm_page.h>38#include <vm/vm.h>3940void vm_radix_wait(void);41void vm_radix_zinit(void);42void *vm_radix_node_alloc(struct pctrie *ptree);43void vm_radix_node_free(struct pctrie *ptree, void *node);44extern smr_t vm_radix_smr;4546static __inline void47vm_radix_init(struct vm_radix *rtree)48{49pctrie_init(&rtree->rt_trie);50}5152static __inline bool53vm_radix_is_empty(struct vm_radix *rtree)54{55return (pctrie_is_empty(&rtree->rt_trie));56}5758PCTRIE_DEFINE_SMR(VM_RADIX, vm_page, pindex, vm_radix_node_alloc,59vm_radix_node_free, vm_radix_smr);6061/*62* Inserts the key-value pair into the trie, starting search from root.63* Panics if the key already exists.64*/65static __inline int66vm_radix_insert(struct vm_radix *rtree, vm_page_t page)67{68return (VM_RADIX_PCTRIE_INSERT(&rtree->rt_trie, page));69}7071/*72* Inserts the key-value pair into the trie, starting search from iterator.73* Panics if the key already exists.74*/75static __inline int76vm_radix_iter_insert(struct pctrie_iter *pages, vm_page_t page)77{78return (VM_RADIX_PCTRIE_ITER_INSERT(pages, page));79}8081/*82* Returns the value stored at the index assuming there is an external lock.83*84* If the index is not present, NULL is returned.85*/86static __inline vm_page_t87vm_radix_lookup(struct vm_radix *rtree, vm_pindex_t index)88{89return (VM_RADIX_PCTRIE_LOOKUP(&rtree->rt_trie, index));90}9192/*93* Returns the value stored at the index without requiring an external lock.94*95* If the index is not present, NULL is returned.96*/97static __inline vm_page_t98vm_radix_lookup_unlocked(struct vm_radix *rtree, vm_pindex_t index)99{100return (VM_RADIX_PCTRIE_LOOKUP_UNLOCKED(&rtree->rt_trie, index));101}102103/*104* Returns the number of contiguous, non-NULL pages read into the ma[]105* array, without requiring an external lock.106*/107static __inline int108vm_radix_lookup_range_unlocked(struct vm_radix *rtree, vm_pindex_t index,109vm_page_t ma[], int count)110{111return (VM_RADIX_PCTRIE_LOOKUP_RANGE_UNLOCKED(&rtree->rt_trie, index,112ma, count));113}114115/*116* Returns the number of contiguous, non-NULL pages read into the ma[]117* array, without requiring an external lock.118*/119static __inline int120vm_radix_iter_lookup_range(struct pctrie_iter *pages, vm_pindex_t index,121vm_page_t ma[], int count)122{123return (VM_RADIX_PCTRIE_ITER_LOOKUP_RANGE(pages, index, ma, count));124}125126/*127* Initialize an iterator for vm_radix.128*/129static __inline void130vm_radix_iter_init(struct pctrie_iter *pages, struct vm_radix *rtree)131{132pctrie_iter_init(pages, &rtree->rt_trie);133}134135/*136* Initialize an iterator for vm_radix.137*/138static __inline void139vm_radix_iter_limit_init(struct pctrie_iter *pages, struct vm_radix *rtree,140vm_pindex_t limit)141{142pctrie_iter_limit_init(pages, &rtree->rt_trie, limit);143}144145/*146* Returns the value stored at the index.147* Requires that access be externally synchronized by a lock.148*149* If the index is not present, NULL is returned.150*/151static __inline vm_page_t152vm_radix_iter_lookup(struct pctrie_iter *pages, vm_pindex_t index)153{154return (VM_RADIX_PCTRIE_ITER_LOOKUP(pages, index));155}156157/*158* Returns the value stored 'stride' steps beyond the current position.159* Requires that access be externally synchronized by a lock.160*161* If the index is not present, NULL is returned.162*/163static __inline vm_page_t164vm_radix_iter_stride(struct pctrie_iter *pages, int stride)165{166return (VM_RADIX_PCTRIE_ITER_STRIDE(pages, stride));167}168169/*170* Returns the page with the least pindex that is greater than or equal to the171* specified pindex, or NULL if there are no such pages.172*173* Requires that access be externally synchronized by a lock.174*/175static __inline vm_page_t176vm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index)177{178return (VM_RADIX_PCTRIE_LOOKUP_GE(&rtree->rt_trie, index));179}180181/*182* Returns the page with the greatest pindex that is less than or equal to the183* specified pindex, or NULL if there are no such pages.184*185* Requires that access be externally synchronized by a lock.186*/187static __inline vm_page_t188vm_radix_lookup_le(struct vm_radix *rtree, vm_pindex_t index)189{190return (VM_RADIX_PCTRIE_LOOKUP_LE(&rtree->rt_trie, index));191}192193/*194* Remove the specified index from the trie, and return the value stored at195* that index. If the index is not present, return NULL.196*/197static __inline vm_page_t198vm_radix_remove(struct vm_radix *rtree, vm_pindex_t index)199{200return (VM_RADIX_PCTRIE_REMOVE_LOOKUP(&rtree->rt_trie, index));201}202203/*204* Remove the current page from the trie.205*/206static __inline void207vm_radix_iter_remove(struct pctrie_iter *pages)208{209VM_RADIX_PCTRIE_ITER_REMOVE(pages);210}211212/*213* Reclaim all the interior nodes of the trie, and invoke the callback214* on all the pages, in order.215*/216static __inline void217vm_radix_reclaim_callback(struct vm_radix *rtree,218void (*page_cb)(vm_page_t, void *), void *arg)219{220VM_RADIX_PCTRIE_RECLAIM_CALLBACK(&rtree->rt_trie, page_cb, arg);221}222223/*224* Initialize an iterator pointing to the page with the least pindex that is225* greater than or equal to the specified pindex, or NULL if there are no such226* pages. Return the page.227*228* Requires that access be externally synchronized by a lock.229*/230static __inline vm_page_t231vm_radix_iter_lookup_ge(struct pctrie_iter *pages, vm_pindex_t index)232{233return (VM_RADIX_PCTRIE_ITER_LOOKUP_GE(pages, index));234}235236/*237* Update the iterator to point to the page with the least pindex that is 'jump'238* or more greater than or equal to the current pindex, or NULL if there are no239* such pages. Return the page.240*241* Requires that access be externally synchronized by a lock.242*/243static __inline vm_page_t244vm_radix_iter_jump(struct pctrie_iter *pages, vm_pindex_t jump)245{246return (VM_RADIX_PCTRIE_ITER_JUMP_GE(pages, jump));247}248249/*250* Update the iterator to point to the page with the least pindex that is one or251* more greater than the current pindex, or NULL if there are no such pages.252* Return the page.253*254* Requires that access be externally synchronized by a lock.255*/256static __inline vm_page_t257vm_radix_iter_step(struct pctrie_iter *pages)258{259return (VM_RADIX_PCTRIE_ITER_STEP_GE(pages));260}261262/*263* Iterate over each non-NULL page from page 'start' to the end of the object.264*/265#define VM_RADIX_FOREACH_FROM(m, pages, start) \266for (m = vm_radix_iter_lookup_ge(pages, start); m != NULL; \267m = vm_radix_iter_step(pages))268269/*270* Iterate over each non-NULL page from the beginning to the end of the object.271*/272#define VM_RADIX_FOREACH(m, pages) VM_RADIX_FOREACH_FROM(m, pages, 0)273274/*275* Initialize an iterator pointing to the page with the greatest pindex that is276* less than or equal to the specified pindex, or NULL if there are no such277* pages. Return the page.278*279* Requires that access be externally synchronized by a lock.280*/281static __inline vm_page_t282vm_radix_iter_lookup_le(struct pctrie_iter *pages, vm_pindex_t index)283{284return (VM_RADIX_PCTRIE_ITER_LOOKUP_LE(pages, index));285}286287/*288* Initialize an iterator pointing to the page with the greatest pindex that is289* less than to the specified pindex, or NULL if there are no such290* pages. Return the page.291*292* Requires that access be externally synchronized by a lock.293*/294static __inline vm_page_t295vm_radix_iter_lookup_lt(struct pctrie_iter *pages, vm_pindex_t index)296{297return (index == 0 ? NULL : vm_radix_iter_lookup_le(pages, index - 1));298}299300/*301* Update the iterator to point to the page with the pindex that is one greater302* than the current pindex, or NULL if there is no such page. Return the page.303*304* Requires that access be externally synchronized by a lock.305*/306static __inline vm_page_t307vm_radix_iter_next(struct pctrie_iter *pages)308{309return (VM_RADIX_PCTRIE_ITER_NEXT(pages));310}311312/*313* Iterate over consecutive non-NULL pages from position 'start' to first NULL314* page.315*/316#define VM_RADIX_FORALL_FROM(m, pages, start) \317for (m = vm_radix_iter_lookup(pages, start); m != NULL; \318m = vm_radix_iter_next(pages))319320/*321* Iterate over consecutive non-NULL pages from the beginning to first NULL322* page.323*/324#define VM_RADIX_FORALL(m, pages) VM_RADIX_FORALL_FROM(m, pages, 0)325326/*327* Update the iterator to point to the page with the pindex that is one less328* than the current pindex, or NULL if there is no such page. Return the page.329*330* Requires that access be externally synchronized by a lock.331*/332static __inline vm_page_t333vm_radix_iter_prev(struct pctrie_iter *pages)334{335return (VM_RADIX_PCTRIE_ITER_PREV(pages));336}337338/*339* Return the current page.340*341* Requires that access be externally synchronized by a lock.342*/343static __inline vm_page_t344vm_radix_iter_page(struct pctrie_iter *pages)345{346return (VM_RADIX_PCTRIE_ITER_VALUE(pages));347}348349/*350* Replace an existing page in the trie with another one.351* Panics if there is not an old page in the trie at the new page's index.352*/353static __inline vm_page_t354vm_radix_replace(struct vm_radix *rtree, vm_page_t newpage)355{356return (VM_RADIX_PCTRIE_REPLACE(&rtree->rt_trie, newpage));357}358359#endif /* _KERNEL */360#endif /* !_VM_RADIX_H_ */361362363