master
  1/*	$NetBSD: extent.h,v 1.26 2017/08/24 11:33:28 jmcneill Exp $	*/
  2
  3/*-
  4 * Copyright (c) 1996, 1998 The NetBSD Foundation, Inc.
  5 * All rights reserved.
  6 *
  7 * This code is derived from software contributed to The NetBSD Foundation
  8 * by Jason R. Thorpe.
  9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 *    notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 *    notice, this list of conditions and the following disclaimer in the
 17 *    documentation and/or other materials provided with the distribution.
 18 *
 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 29 * POSSIBILITY OF SUCH DAMAGE.
 30 */
 31
 32#ifndef _SYS_EXTENT_H_
 33#define _SYS_EXTENT_H_
 34
 35#include <sys/queue.h>
 36#include <sys/mutex.h>
 37#include <sys/condvar.h>
 38
 39#ifndef _KERNEL
 40#include <stdbool.h>
 41#endif
 42
 43struct extent_region {
 44	LIST_ENTRY(extent_region) er_link;	/* link in region list */
 45	u_long 	er_start;		/* start of region */
 46	u_long	er_end;			/* end of region */
 47	int	er_flags;		/* misc. flags */
 48};
 49
 50/* er_flags */
 51#define ER_ALLOC	0x01	/* region descriptor dynamically allocated */
 52
 53struct extent {
 54	const char *ex_name;		/* name of extent */
 55	kmutex_t ex_lock;		/* lock on this extent */
 56	kcondvar_t ex_cv;		/* synchronization */
 57					/* allocated regions in extent */
 58	LIST_HEAD(, extent_region) ex_regions;
 59	u_long	ex_start;		/* start of extent */
 60	u_long	ex_end;			/* end of extent */
 61	int	ex_flags;		/* misc. information */
 62	bool	ex_flwanted;		/* someone asleep on freelist */
 63};
 64
 65struct extent_fixed {
 66	struct extent	fex_extent;	/* MUST BE FIRST */
 67					/* freelist of region descriptors */
 68	LIST_HEAD(, extent_region) fex_freelist;
 69	void *		fex_storage;	/* storage space for descriptors */
 70	size_t		fex_storagesize; /* size of storage space */
 71};
 72
 73/* ex_flags; for internal use only */
 74#define EXF_FIXED	__BIT(0)	/* extent uses fixed storage */
 75#define EXF_NOCOALESCE	__BIT(1)	/* coalescing of regions not allowed */
 76#define EXF_EARLY	__BIT(2)	/* no need to lock */
 77
 78#define EXF_BITS	"\20\3EARLY\2NOCOALESCE\1FIXED"
 79
 80/* misc. flags passed to extent functions */
 81#define EX_NOWAIT	0		/* not safe to sleep */
 82#define EX_WAITOK	__BIT(0)	/* safe to sleep */
 83#define EX_FAST		__BIT(1)	/* take first fit in extent_alloc() */
 84#define EX_CATCH	__BIT(2)	/* catch signals while sleeping */
 85#define EX_NOCOALESCE	__BIT(3)	/* create a non-coalescing extent */
 86#define EX_MALLOCOK	__BIT(4)	/* safe to call kmem_alloc() */
 87#define EX_WAITSPACE	__BIT(5)	/* wait for space to become free */
 88#define EX_BOUNDZERO	__BIT(6)	/* boundary lines start at 0 */
 89#define EX_EARLY	__BIT(7)	/* safe for early kernel bootstrap */
 90
 91/*
 92 * Special place holders for "alignment" and "boundary" arguments,
 93 * in the event the caller doesn't wish to use those features.
 94 */
 95#define EX_NOALIGN	1		/* don't do alignment */
 96#define EX_NOBOUNDARY	0		/* don't do boundary checking */
 97
 98#if defined(_KERNEL) || defined(_EXTENT_TESTING)
 99#define EXTENT_FIXED_STORAGE_SIZE(_nregions)		\
100	(ALIGN(sizeof(struct extent_fixed)) +		\
101	((ALIGN(sizeof(struct extent_region))) *	\
102	 (_nregions)))
103
104struct	extent *extent_create(const char *, u_long, u_long,
105	    void *, size_t, int);
106void	extent_destroy(struct extent *);
107int	extent_alloc_subregion1(struct extent *, u_long, u_long,
108	    u_long, u_long, u_long, u_long, int, u_long *);
109int	extent_alloc_subregion(struct extent *, u_long, u_long,
110	    u_long, u_long, u_long, int, u_long *);
111int	extent_alloc_region(struct extent *, u_long, u_long, int);
112int	extent_alloc1(struct extent *, u_long, u_long, u_long, u_long, int,
113	    u_long *);
114int	extent_alloc(struct extent *, u_long, u_long, u_long, int, u_long *);
115int	extent_free(struct extent *, u_long, u_long, int);
116void	extent_print(struct extent *);
117void	extent_init(void);
118
119#endif /* _KERNEL || _EXTENT_TESTING */
120
121#endif /* ! _SYS_EXTENT_H_ */