2013-05-23 08:39:54 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-01-30 09:38:10 -05:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2013, 2014, 2015.
|
2013-05-23 08:39:54 -04:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
device.cpp
|
|
|
|
Block device.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-10-01 16:27:19 -04:00
|
|
|
#include <assert.h>
|
2015-01-30 18:19:54 -05:00
|
|
|
#include <pthread.h>
|
2013-05-23 08:39:54 -04:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2015-01-30 10:28:03 -05:00
|
|
|
#include <unistd.h>
|
2013-05-23 08:39:54 -04:00
|
|
|
|
|
|
|
#include "block.h"
|
|
|
|
#include "device.h"
|
|
|
|
#include "ioleast.h"
|
|
|
|
|
2015-01-30 18:19:54 -05:00
|
|
|
void* Device__SyncThread(void* ctx)
|
|
|
|
{
|
|
|
|
((Device*) ctx)->SyncThread();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-16 09:44:21 -05:00
|
|
|
Device::Device(int fd, const char* path, uint32_t block_size, bool write)
|
2013-05-23 08:39:54 -04:00
|
|
|
{
|
2015-07-09 11:14:37 -04:00
|
|
|
// sync_thread unset.
|
|
|
|
this->sync_thread_cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
this->sync_thread_idle_cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
this->sync_thread_lock = PTHREAD_MUTEX_INITIALIZER;
|
2013-05-23 08:39:54 -04:00
|
|
|
this->mru_block = NULL;
|
|
|
|
this->lru_block = NULL;
|
2014-10-01 16:27:19 -04:00
|
|
|
this->dirty_block = NULL;
|
2013-05-23 08:39:54 -04:00
|
|
|
for ( size_t i = 0; i < DEVICE_HASH_LENGTH; i++ )
|
|
|
|
hash_blocks[i] = NULL;
|
2015-07-09 11:14:37 -04:00
|
|
|
struct stat st;
|
|
|
|
fstat(fd, &st);
|
|
|
|
this->device_size = st.st_size;
|
|
|
|
this->path = path;
|
|
|
|
this->block_size = block_size;
|
|
|
|
this->fd = fd;
|
|
|
|
this->write = write;
|
2015-01-30 18:19:54 -05:00
|
|
|
this->has_sync_thread = false;
|
2015-07-09 11:14:37 -04:00
|
|
|
this->sync_thread_should_exit = false;
|
|
|
|
this->sync_in_transit = false;
|
2015-09-27 10:50:12 -04:00
|
|
|
this->block_count = 0;
|
|
|
|
#ifdef __sortix__
|
|
|
|
// TODO: This isn't scaleable if there's multiple filesystems mounted.
|
|
|
|
size_t memory;
|
|
|
|
memstat(NULL, &memory);
|
|
|
|
this->block_limit = (memory / 10) / block_size;
|
|
|
|
#else
|
|
|
|
this->block_limit = 32768;
|
|
|
|
#endif
|
2013-05-23 08:39:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Device::~Device()
|
|
|
|
{
|
2015-01-30 18:19:54 -05:00
|
|
|
if ( has_sync_thread )
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sync_thread_lock);
|
|
|
|
sync_thread_should_exit = true;
|
|
|
|
pthread_cond_signal(&sync_thread_cond);
|
|
|
|
pthread_mutex_unlock(&sync_thread_lock);
|
|
|
|
pthread_join(sync_thread, NULL);
|
|
|
|
has_sync_thread = false;
|
|
|
|
}
|
2013-05-23 08:39:54 -04:00
|
|
|
Sync();
|
|
|
|
while ( mru_block )
|
|
|
|
delete mru_block;
|
2015-01-30 10:28:03 -05:00
|
|
|
close(fd);
|
2013-05-23 08:39:54 -04:00
|
|
|
}
|
|
|
|
|
2015-01-30 18:19:54 -05:00
|
|
|
void Device::SpawnSyncThread()
|
|
|
|
{
|
|
|
|
if ( this->has_sync_thread )
|
|
|
|
return;
|
|
|
|
this->has_sync_thread = write &&
|
|
|
|
pthread_create(&this->sync_thread, NULL, Device__SyncThread, this) == 0;
|
|
|
|
}
|
|
|
|
|
2015-09-27 10:50:12 -04:00
|
|
|
Block* Device::AllocateBlock()
|
2013-05-23 08:39:54 -04:00
|
|
|
{
|
2015-09-27 10:50:12 -04:00
|
|
|
if ( block_limit <= block_count )
|
|
|
|
{
|
|
|
|
for ( Block* block = lru_block; block; block = block->prev_block )
|
|
|
|
{
|
|
|
|
if ( block->reference_count )
|
|
|
|
continue;
|
|
|
|
block->Destruct(); // Syncs.
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
}
|
2015-07-09 12:06:45 -04:00
|
|
|
uint8_t* data = new uint8_t[block_size];
|
|
|
|
if ( !data ) // TODO: Use operator new nothrow!
|
|
|
|
return NULL;
|
2015-09-27 10:50:12 -04:00
|
|
|
Block* block = new Block();
|
2015-07-09 12:06:45 -04:00
|
|
|
if ( !block ) // TODO: Use operator new nothrow!
|
|
|
|
return delete[] data, (Block*) NULL;
|
|
|
|
block->block_data = data;
|
2015-09-27 10:50:12 -04:00
|
|
|
block_count++;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block* Device::GetBlock(uint32_t block_id)
|
|
|
|
{
|
|
|
|
if ( Block* block = GetCachedBlock(block_id) )
|
|
|
|
return block;
|
|
|
|
Block* block = AllocateBlock();
|
|
|
|
if ( !block )
|
|
|
|
return NULL;
|
|
|
|
block->Construct(this, block_id);
|
2013-05-23 08:39:54 -04:00
|
|
|
off_t file_offset = (off_t) block_size * (off_t) block_id;
|
|
|
|
preadall(fd, block->block_data, block_size, file_offset);
|
|
|
|
block->Prelink();
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block* Device::GetBlockZeroed(uint32_t block_id)
|
|
|
|
{
|
2015-07-09 12:15:51 -04:00
|
|
|
assert(write);
|
2013-05-23 08:39:54 -04:00
|
|
|
if ( Block* block = GetCachedBlock(block_id) )
|
|
|
|
{
|
2015-01-30 09:38:10 -05:00
|
|
|
block->BeginWrite();
|
2013-05-23 08:39:54 -04:00
|
|
|
memset(block->block_data, 0, block_size);
|
2015-01-30 09:38:10 -05:00
|
|
|
block->FinishWrite();
|
2013-05-23 08:39:54 -04:00
|
|
|
return block;
|
|
|
|
}
|
2015-09-27 10:50:12 -04:00
|
|
|
Block* block = AllocateBlock();
|
|
|
|
if ( !block )
|
2015-07-09 12:06:45 -04:00
|
|
|
return NULL;
|
2015-09-27 10:50:12 -04:00
|
|
|
block->Construct(this, block_id);
|
2013-05-23 08:39:54 -04:00
|
|
|
memset(block->block_data, 0, block_size);
|
|
|
|
block->Prelink();
|
2015-01-30 09:38:10 -05:00
|
|
|
block->BeginWrite();
|
|
|
|
block->FinishWrite();
|
2013-05-23 08:39:54 -04:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block* Device::GetCachedBlock(uint32_t block_id)
|
|
|
|
{
|
|
|
|
size_t bin = block_id % DEVICE_HASH_LENGTH;
|
|
|
|
for ( Block* iter = hash_blocks[bin]; iter; iter = iter->next_hashed )
|
|
|
|
if ( iter->block_id == block_id )
|
|
|
|
return iter->Refer(), iter;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::Sync()
|
|
|
|
{
|
2015-01-30 18:19:54 -05:00
|
|
|
if ( has_sync_thread )
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&sync_thread_lock);
|
|
|
|
while ( dirty_block || sync_in_transit )
|
|
|
|
pthread_cond_wait(&sync_thread_cond, &sync_thread_lock);
|
|
|
|
pthread_mutex_unlock(&sync_thread_lock);
|
2015-05-16 18:26:23 -04:00
|
|
|
fsync(fd);
|
2015-01-30 18:19:54 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 16:27:19 -04:00
|
|
|
while ( dirty_block )
|
|
|
|
dirty_block->Sync();
|
2015-05-16 18:26:23 -04:00
|
|
|
fsync(fd);
|
2013-05-23 08:39:54 -04:00
|
|
|
}
|
2015-01-30 18:19:54 -05:00
|
|
|
|
|
|
|
void Device::SyncThread()
|
|
|
|
{
|
|
|
|
uint8_t transit_block_data[block_size];
|
|
|
|
pthread_mutex_lock(&sync_thread_lock);
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
while ( !(dirty_block || sync_thread_should_exit) )
|
|
|
|
pthread_cond_wait(&sync_thread_cond, &sync_thread_lock);
|
|
|
|
if ( sync_thread_should_exit )
|
|
|
|
break;
|
|
|
|
|
|
|
|
Block* block = dirty_block;
|
|
|
|
|
|
|
|
if ( block->next_dirty )
|
|
|
|
block->next_dirty->prev_dirty = NULL;
|
|
|
|
dirty_block = block->next_dirty;
|
|
|
|
block->next_dirty = NULL;
|
|
|
|
|
|
|
|
block->dirty = false;
|
|
|
|
block->is_in_transit = true;
|
|
|
|
sync_in_transit = true;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&sync_thread_lock);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&block->modify_lock);
|
|
|
|
memcpy(transit_block_data, block->block_data, block_size);
|
|
|
|
pthread_mutex_unlock(&block->modify_lock);
|
|
|
|
|
|
|
|
off_t offset = (off_t) block_size * (off_t) block->block_id;
|
|
|
|
pwriteall(fd, transit_block_data, block_size, offset);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&sync_thread_lock);
|
|
|
|
block->is_in_transit = false;
|
|
|
|
sync_in_transit = false;
|
|
|
|
pthread_cond_signal(&block->transit_done_cond);
|
|
|
|
if ( !dirty_block )
|
|
|
|
pthread_cond_signal(&sync_thread_idle_cond);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&sync_thread_lock);
|
|
|
|
}
|