|
Packit Service |
b3514a |
/*
|
|
Packit Service |
b3514a |
* Copyright (c) 2020 Red Hat, Inc.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is free software; you can redistribute it and/or
|
|
Packit Service |
b3514a |
* modify it under the terms of the GNU General Public License
|
|
Packit Service |
b3514a |
* as published by the Free Software Foundation; either version 2
|
|
Packit Service |
b3514a |
* of the License, or (at your option) any later version.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
b3514a |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b3514a |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
b3514a |
* GNU General Public License for more details.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
b3514a |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
b3514a |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
b3514a |
* 02110-1301, USA.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* $Id: //eng/vdo-releases/aluminum/src/c++/vdo/kernel/instanceNumber.c#1 $
|
|
Packit Service |
b3514a |
*/
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "instanceNumber.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include <linux/bitops.h>
|
|
Packit Service |
b3514a |
#include <linux/mutex.h>
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
#include "memoryAlloc.h"
|
|
Packit Service |
b3514a |
#include "numUtils.h"
|
|
Packit Service |
b3514a |
#include "permassert.h"
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/*
|
|
Packit Service |
b3514a |
* Track in-use instance numbers using a flat bit array.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* O(n) run time isn't ideal, but if we have 1000 VDO devices in use
|
|
Packit Service |
b3514a |
* simultaneously we still only need to scan 16 words, so it's not
|
|
Packit Service |
b3514a |
* likely to be a big deal compared to other resource usage.
|
|
Packit Service |
b3514a |
*/
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
enum {
|
|
Packit Service |
b3514a |
/**
|
|
Packit Service |
b3514a |
* This minimum size for the bit array creates a numbering space of 0-999,
|
|
Packit Service |
b3514a |
* which allows successive starts of the same volume to have different
|
|
Packit Service |
b3514a |
* instance numbers in any reasonably-sized test. Changing instances on
|
|
Packit Service |
b3514a |
* restart allows vdoMonReport to detect that the ephemeral stats have reset
|
|
Packit Service |
b3514a |
* to zero.
|
|
Packit Service |
b3514a |
**/
|
|
Packit Service |
b3514a |
BIT_COUNT_MINIMUM = 1000,
|
|
Packit Service |
b3514a |
/** Grow the bit array by this many bits when needed */
|
|
Packit Service |
b3514a |
BIT_COUNT_INCREMENT = 100,
|
|
Packit Service |
b3514a |
};
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
static struct mutex instanceNumberLock;
|
|
Packit Service |
b3514a |
static unsigned int bitCount;
|
|
Packit Service |
b3514a |
static unsigned long *words;
|
|
Packit Service |
b3514a |
static unsigned int instanceCount;
|
|
Packit Service |
b3514a |
static unsigned int nextInstance;
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**
|
|
Packit Service |
b3514a |
* Return the number of bytes needed to store a bit array of the specified
|
|
Packit Service |
b3514a |
* capacity in an array of unsigned longs.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @param bitCount The number of bits the array must hold
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @return the number of bytes needed for the array reperesentation
|
|
Packit Service |
b3514a |
**/
|
|
Packit Service |
b3514a |
static size_t getBitArraySize(unsigned int bitCount)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
// Round up to a multiple of the word size and convert to a byte count.
|
|
Packit Service |
b3514a |
return (computeBucketCount(bitCount, BITS_PER_LONG) * sizeof(unsigned long));
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**
|
|
Packit Service |
b3514a |
* Re-allocate the bitmap word array so there will more instance numbers that
|
|
Packit Service |
b3514a |
* can be allocated. Since the array is initially NULL, this also initializes
|
|
Packit Service |
b3514a |
* the array the first time we allocate an instance number.
|
|
Packit Service |
b3514a |
*
|
|
Packit Service |
b3514a |
* @return UDS_SUCCESS or an error code from the allocation
|
|
Packit Service |
b3514a |
**/
|
|
Packit Service |
b3514a |
static int growBitArray(void)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
unsigned int newCount = maxUInt(bitCount + BIT_COUNT_INCREMENT,
|
|
Packit Service |
b3514a |
BIT_COUNT_MINIMUM);
|
|
Packit Service |
b3514a |
unsigned long *newWords;
|
|
Packit Service |
b3514a |
int result = reallocateMemory(words,
|
|
Packit Service |
b3514a |
getBitArraySize(bitCount),
|
|
Packit Service |
b3514a |
getBitArraySize(newCount),
|
|
Packit Service |
b3514a |
"instance number bit array",
|
|
Packit Service |
b3514a |
&newWords);
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
bitCount = newCount;
|
|
Packit Service |
b3514a |
words = newWords;
|
|
Packit Service |
b3514a |
return UDS_SUCCESS;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
static int allocateKVDOInstanceLocked(unsigned int *instancePtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
// If there are no unallocated instances, grow the bit array.
|
|
Packit Service |
b3514a |
if (instanceCount >= bitCount) {
|
|
Packit Service |
b3514a |
int result = growBitArray();
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
// There must be a zero bit somewhere now. Find it, starting just after the
|
|
Packit Service |
b3514a |
// last instance allocated.
|
|
Packit Service |
b3514a |
unsigned int instance = find_next_zero_bit(words, bitCount, nextInstance);
|
|
Packit Service |
b3514a |
if (instance >= bitCount) {
|
|
Packit Service |
b3514a |
// Nothing free after nextInstance, so wrap around to instance zero.
|
|
Packit Service |
b3514a |
instance = find_first_zero_bit(words, bitCount);
|
|
Packit Service |
b3514a |
int result = ASSERT(instance < bitCount, "impossibly, no zero bit found");
|
|
Packit Service |
b3514a |
if (result != UDS_SUCCESS) {
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
__set_bit(instance, words);
|
|
Packit Service |
b3514a |
instanceCount += 1;
|
|
Packit Service |
b3514a |
nextInstance = instance + 1;
|
|
Packit Service |
b3514a |
*instancePtr = instance;
|
|
Packit Service |
b3514a |
return UDS_SUCCESS;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
int allocateKVDOInstance(unsigned int *instancePtr)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
mutex_lock(&instanceNumberLock);
|
|
Packit Service |
b3514a |
int result = allocateKVDOInstanceLocked(instancePtr);
|
|
Packit Service |
b3514a |
mutex_unlock(&instanceNumberLock);
|
|
Packit Service |
b3514a |
return result;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
void releaseKVDOInstance(unsigned int instance)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
mutex_lock(&instanceNumberLock);
|
|
Packit Service |
b3514a |
if (instance >= bitCount) {
|
|
Packit Service |
b3514a |
ASSERT_LOG_ONLY(false, "instance number %u must be less than bit count %u",
|
|
Packit Service |
b3514a |
instance, bitCount);
|
|
Packit Service |
b3514a |
} else if (test_bit(instance, words) == 0) {
|
|
Packit Service |
b3514a |
ASSERT_LOG_ONLY(false, "instance number %u must be allocated", instance);
|
|
Packit Service |
b3514a |
} else {
|
|
Packit Service |
b3514a |
__clear_bit(instance, words);
|
|
Packit Service |
b3514a |
instanceCount -= 1;
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
mutex_unlock(&instanceNumberLock);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
void initializeInstanceNumberTracking(void)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
mutex_init(&instanceNumberLock);
|
|
Packit Service |
b3514a |
}
|
|
Packit Service |
b3514a |
|
|
Packit Service |
b3514a |
/**********************************************************************/
|
|
Packit Service |
b3514a |
void cleanUpInstanceNumberTracking(void)
|
|
Packit Service |
b3514a |
{
|
|
Packit Service |
b3514a |
ASSERT_LOG_ONLY(instanceCount == 0,
|
|
Packit Service |
b3514a |
"should have no instance numbers still in use, but have %u",
|
|
Packit Service |
b3514a |
instanceCount);
|
|
Packit Service |
b3514a |
FREE(words);
|
|
Packit Service |
b3514a |
words = NULL;
|
|
Packit Service |
b3514a |
bitCount = 0;
|
|
Packit Service |
b3514a |
instanceCount = 0;
|
|
Packit Service |
b3514a |
nextInstance = 0;
|
|
Packit Service |
b3514a |
mutex_destroy(&instanceNumberLock);
|
|
Packit Service |
b3514a |
}
|