1 /* $NetBSD: rf_alloclist.c,v 1.30 2026/10/24 00:20:23 tls Exp $ */ 2 /* 3 * Copyright (c) 1995 Carnegie-Mellon University. 4 * All rights reserved. 5 * 6 * Author: Mark Holland 7 * 8 * Permission to use, copy, modify and distribute this software and 9 * its documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 /**************************************************************************** 30 * 31 * Alloclist.c -- code to manipulate allocation lists 32 * 33 * an allocation list is just a list of AllocListElem structures. Each 34 * such structure contains a fixed-size array of pointers. Calling 35 * FreeAList() causes each pointer to be freed. 36 * 37 ***************************************************************************/ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: rf_alloclist.c,v 1.30 2026/10/24 00:20:23 tls Exp $"); 41 42 #include <dev/raidframe/raidframevar.h> 43 44 #include "rf_options.h" 45 #include "rf_threadstuff.h" 46 #include "rf_raid.h" 47 #include "rf_alloclist.h" 48 #include "rf_debugMem.h" 49 #include "rf_etimer.h" 50 #include "rf_general.h" 51 #include "rf_shutdown.h" 52 #include "rf_netbsd.h" 53 54 #include <sys/pool.h> 55 56 #define RF_AL_INIT_MIN 4 57 58 static size_t rf_alloclist_primed; 59 60 static void rf_ShutdownAllocList(void *); 61 62 static void rf_ShutdownAllocList(void *ignored) 63 { 64 pool_destroy(&rf_alloclist_pool); 65 rf_alloclist_primed = 0; 66 } 67 68 int 69 rf_ConfigureAllocList(RF_ShutdownList_t **listp) 70 { 71 72 pool_init(&rf_alloclist_pool, sizeof(RF_AllocListElem_t), 0, 0, 0, "rf_alloclist_pl", NULL, IPL_BIO); 73 pool_sethiwat(&rf_alloclist_pool, 2 * RF_AL_INIT_MIN); 74 pool_prime(&rf_alloclist_pool, RF_AL_INIT_MIN); 75 rf_alloclist_primed = RF_AL_INIT_MIN; 76 77 rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL); 78 79 return (0); 80 } 81 82 /* 83 * Prime rf_alloclist_pool for an array's worst-case concurrent I/Os. 84 * Each admitted I/O uses 1 elem for desc->allocList plus 1 per DAG header, 85 * and parity rewrite / array cleanupList use 3 more. 86 */ 87 void 88 rf_GrowAllocListPool(RF_Raid_t *raidPtr) 89 { 90 size_t stripe_data_bytes, needed; 91 u_int max_stripes_per_bp, d, o; 92 93 d = raidPtr->Layout.numDataCol; 94 o = raidPtr->openings; 95 stripe_data_bytes = (size_t)raidPtr->Layout.dataSectorsPerStripe << 96 raidPtr->logBytesPerSector; 97 max_stripes_per_bp = howmany(MAXPHYS, stripe_data_bytes) + 1; 98 99 needed = o * (1 + max_stripes_per_bp + 2 * d) + 3; 100 pool_prime(&rf_alloclist_pool, needed); 101 rf_alloclist_primed += needed; 102 pool_sethiwat(&rf_alloclist_pool, 2 * rf_alloclist_primed); 103 } 104 105 106 /* we expect the lists to have at most one or two elements, so we're willing 107 * to search for the end. If you ever observe the lists growing longer, 108 * increase POINTERS_PER_ALLOC_LIST_ELEMENT. 109 */ 110 void 111 rf_real_AddToAllocList(RF_AllocListElem_t *l, void *p, int size) 112 { 113 RF_AllocListElem_t *newelem; 114 115 for (; l->next; l = l->next) 116 RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT); /* find end of list */ 117 118 RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT); 119 if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) { 120 newelem = rf_real_MakeAllocList(); 121 l->next = newelem; 122 l = newelem; 123 } 124 l->pointers[l->numPointers] = p; 125 l->sizes[l->numPointers] = size; 126 l->numPointers++; 127 128 } 129 130 void 131 rf_FreeAllocList(RF_AllocListElem_t *l) 132 { 133 int i; 134 RF_AllocListElem_t *temp, *p; 135 136 for (p = l; p; p = p->next) { 137 RF_ASSERT(p->numPointers >= 0 && 138 p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT); 139 for (i = 0; i < p->numPointers; i++) { 140 RF_ASSERT(p->pointers[i]); 141 RF_Free(p->pointers[i], p->sizes[i]); 142 } 143 } 144 while (l) { 145 temp = l; 146 l = l->next; 147 pool_put(&rf_alloclist_pool, temp); 148 } 149 } 150 151 RF_AllocListElem_t * 152 rf_real_MakeAllocList(void) 153 { 154 return pool_get(&rf_alloclist_pool, PR_WAITOK | PR_ZERO); 155 } 156