activated EOF hints and fixed a bug in the garbage collector.
This commit is contained in:
parent
d28b27f031
commit
e483f335ce
@ -43,6 +43,7 @@
|
|||||||
#include "cfs/cfs-coffee.h"
|
#include "cfs/cfs-coffee.h"
|
||||||
#include "dev/watchdog.h"
|
#include "dev/watchdog.h"
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
@ -66,34 +67,36 @@
|
|||||||
#define COFFEE_FD_APPEND 0x4
|
#define COFFEE_FD_APPEND 0x4
|
||||||
#define COFFEE_FD_FREE 0x0
|
#define COFFEE_FD_FREE 0x0
|
||||||
|
|
||||||
#define COFFEE_PAGE_COUNT (COFFEE_SIZE / COFFEE_PAGE_SIZE)
|
|
||||||
#define COFFEE_PAGES_PER_SECTOR (COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE)
|
|
||||||
|
|
||||||
#define COFFEE_FLAG_VALID 0x1
|
|
||||||
#define COFFEE_FLAG_ALLOCATED 0x2
|
|
||||||
#define COFFEE_FLAG_OBSOLETE 0x4
|
|
||||||
#define COFFEE_FLAG_MODIFIED 0x8
|
|
||||||
#define COFFEE_FLAG_LOG 0x10
|
|
||||||
|
|
||||||
#define LOG_CMD_MAGIC 0x7a
|
|
||||||
|
|
||||||
#define COFFEE_PAGE_ALLOCATED(hdr) ((hdr).flags & COFFEE_FLAG_ALLOCATED)
|
|
||||||
#define COFFEE_PAGE_FREE(hdr) !COFFEE_PAGE_ALLOCATED(hdr)
|
|
||||||
#define COFFEE_PAGE_OBSOLETE(hdr) ((hdr).flags & COFFEE_FLAG_OBSOLETE)
|
|
||||||
#define COFFEE_PAGE_ACTIVE(hdr) \
|
|
||||||
(COFFEE_PAGE_ALLOCATED(hdr) && !COFFEE_PAGE_OBSOLETE(hdr))
|
|
||||||
#define COFFEE_PAGE_LOG(hdr) ((hdr).flags & COFFEE_FLAG_LOG)
|
|
||||||
#define COFFEE_PAGE_MODIFIED(hdr) ((hdr).flags & COFFEE_FLAG_MODIFIED)
|
|
||||||
|
|
||||||
#define FD_VALID(fd) \
|
#define FD_VALID(fd) \
|
||||||
((fd) < COFFEE_FD_SET_SIZE && coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
|
((fd) < COFFEE_FD_SET_SIZE && \
|
||||||
|
coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
|
||||||
#define FD_READABLE(fd) (coffee_fd_set[(fd)].flags & CFS_READ)
|
#define FD_READABLE(fd) (coffee_fd_set[(fd)].flags & CFS_READ)
|
||||||
#define FD_WRITABLE(fd) (coffee_fd_set[(fd)].flags & CFS_WRITE)
|
#define FD_WRITABLE(fd) (coffee_fd_set[(fd)].flags & CFS_WRITE)
|
||||||
#define FD_APPENDABLE(fd) (coffee_fd_set[(fd)].flags & CFS_APPEND)
|
#define FD_APPENDABLE(fd) (coffee_fd_set[(fd)].flags & CFS_APPEND)
|
||||||
|
|
||||||
|
/* File header flags. */
|
||||||
|
#define COFFEE_FLAG_VALID 0x1 /* Completely written header. */
|
||||||
|
#define COFFEE_FLAG_ALLOCATED 0x2 /* Allocated file. */
|
||||||
|
#define COFFEE_FLAG_OBSOLETE 0x4 /* File marked for GC. */
|
||||||
|
#define COFFEE_FLAG_MODIFIED 0x8 /* Modified file, log exists. */
|
||||||
|
#define COFFEE_FLAG_LOG 0x10 /* Log file. */
|
||||||
|
#define COFFEE_FLAG_ISOLATED 0x20 /* Isolated page. */
|
||||||
|
|
||||||
|
#define COFFEE_PAGE_ALLOCATED(hdr) ((hdr).flags & COFFEE_FLAG_ALLOCATED)
|
||||||
|
#define COFFEE_PAGE_FREE(hdr) !COFFEE_PAGE_ALLOCATED(hdr)
|
||||||
|
#define COFFEE_PAGE_LOG(hdr) ((hdr).flags & COFFEE_FLAG_LOG)
|
||||||
|
#define COFFEE_PAGE_MODIFIED(hdr) ((hdr).flags & COFFEE_FLAG_MODIFIED)
|
||||||
|
#define COFFEE_PAGE_ISOLATED(hdr) ((hdr).flags & COFFEE_FLAG_ISOLATED)
|
||||||
|
#define COFFEE_PAGE_OBSOLETE(hdr) ((hdr).flags & COFFEE_FLAG_OBSOLETE)
|
||||||
|
#define COFFEE_PAGE_ACTIVE(hdr) (COFFEE_PAGE_ALLOCATED(hdr) && \
|
||||||
|
!COFFEE_PAGE_OBSOLETE(hdr) && \
|
||||||
|
!COFFEE_PAGE_ISOLATED(hdr))
|
||||||
|
|
||||||
|
#define COFFEE_PAGE_COUNT (COFFEE_SIZE / COFFEE_PAGE_SIZE)
|
||||||
|
#define COFFEE_PAGES_PER_SECTOR (COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE)
|
||||||
|
|
||||||
#define READ_HEADER(hdr, page) \
|
#define READ_HEADER(hdr, page) \
|
||||||
COFFEE_READ((hdr), sizeof(*hdr), (page) * COFFEE_PAGE_SIZE)
|
COFFEE_READ((hdr), sizeof(*hdr), (page) * COFFEE_PAGE_SIZE)
|
||||||
|
|
||||||
#define WRITE_HEADER(hdr, page) \
|
#define WRITE_HEADER(hdr, page) \
|
||||||
COFFEE_WRITE((hdr), sizeof(*hdr), (page) * COFFEE_PAGE_SIZE)
|
COFFEE_WRITE((hdr), sizeof(*hdr), (page) * COFFEE_PAGE_SIZE)
|
||||||
|
|
||||||
@ -139,11 +142,12 @@ static struct file_desc coffee_fd_set[COFFEE_FD_SET_SIZE];
|
|||||||
static void
|
static void
|
||||||
get_sector_status(uint16_t sector, uint16_t *active,
|
get_sector_status(uint16_t sector, uint16_t *active,
|
||||||
uint16_t *free, uint16_t *obsolete) {
|
uint16_t *free, uint16_t *obsolete) {
|
||||||
uint32_t offset;
|
uint32_t offset, sector_start;
|
||||||
uint32_t end;
|
uint32_t end;
|
||||||
struct file_header hdr;
|
struct file_header hdr;
|
||||||
static int16_t skip_pages;
|
static int16_t skip_pages;
|
||||||
static int last_pages_are_active;
|
static int last_pages_are_active;
|
||||||
|
int i;
|
||||||
|
|
||||||
*active = *free = *obsolete = 0;
|
*active = *free = *obsolete = 0;
|
||||||
if(sector == 0) {
|
if(sector == 0) {
|
||||||
@ -159,12 +163,27 @@ get_sector_status(uint16_t sector, uint16_t *active,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sector_start = sector * COFFEE_SECTOR_SIZE;
|
||||||
|
|
||||||
if(last_pages_are_active) {
|
if(last_pages_are_active) {
|
||||||
*active = skip_pages;
|
*active = skip_pages;
|
||||||
} else {
|
} else {
|
||||||
*obsolete = skip_pages;
|
*obsolete = skip_pages;
|
||||||
|
|
||||||
|
/* Split an obsolete file starting in the previous sector and mark
|
||||||
|
the following pages as isolated. */
|
||||||
|
offset = sector_start;
|
||||||
|
for(i = 0; i < skip_pages; i++) {
|
||||||
|
COFFEE_READ(&hdr, sizeof(hdr), offset);
|
||||||
|
hdr.flags |= COFFEE_FLAG_ISOLATED;
|
||||||
|
COFFEE_WRITE(&hdr, sizeof(hdr), offset);
|
||||||
|
offset += COFFEE_PAGE_SIZE;
|
||||||
}
|
}
|
||||||
offset = sector * COFFEE_SECTOR_SIZE + skip_pages * COFFEE_PAGE_SIZE;
|
PRINTF("Coffee: Isolated %u pages starting in sector %d\n",
|
||||||
|
(unsigned)skip_pages, (int)sector);
|
||||||
|
}
|
||||||
|
|
||||||
|
offset = sector_start + skip_pages * COFFEE_PAGE_SIZE;
|
||||||
end = (sector + 1) * COFFEE_SECTOR_SIZE;
|
end = (sector + 1) * COFFEE_SECTOR_SIZE;
|
||||||
|
|
||||||
while(offset < end) {
|
while(offset < end) {
|
||||||
@ -177,6 +196,10 @@ get_sector_status(uint16_t sector, uint16_t *active,
|
|||||||
last_pages_are_active = 0;
|
last_pages_are_active = 0;
|
||||||
offset += hdr.max_pages * COFFEE_PAGE_SIZE;
|
offset += hdr.max_pages * COFFEE_PAGE_SIZE;
|
||||||
*obsolete += hdr.max_pages;
|
*obsolete += hdr.max_pages;
|
||||||
|
} else if(COFFEE_PAGE_ISOLATED(hdr)) {
|
||||||
|
last_pages_are_active = 0;
|
||||||
|
offset += COFFEE_PAGE_SIZE;
|
||||||
|
*obsolete++;
|
||||||
} else if(COFFEE_PAGE_FREE(hdr)) {
|
} else if(COFFEE_PAGE_FREE(hdr)) {
|
||||||
*free = (end - offset) / COFFEE_PAGE_SIZE;
|
*free = (end - offset) / COFFEE_PAGE_SIZE;
|
||||||
break;
|
break;
|
||||||
@ -262,6 +285,8 @@ find_file(const char *name)
|
|||||||
page += hdr.max_pages;
|
page += hdr.max_pages;
|
||||||
} else if(COFFEE_PAGE_OBSOLETE(hdr)) {
|
} else if(COFFEE_PAGE_OBSOLETE(hdr)) {
|
||||||
page += hdr.max_pages;
|
page += hdr.max_pages;
|
||||||
|
} else if(COFFEE_PAGE_ISOLATED(hdr)) {
|
||||||
|
++page;
|
||||||
} else {
|
} else {
|
||||||
/* It follows from the properties of the page allocation algorithm
|
/* It follows from the properties of the page allocation algorithm
|
||||||
that if a free page is encountered, then the rest of the sector
|
that if a free page is encountered, then the rest of the sector
|
||||||
@ -281,18 +306,31 @@ find_offset_in_file(int first_page)
|
|||||||
unsigned char buf[COFFEE_PAGE_SIZE];
|
unsigned char buf[COFFEE_PAGE_SIZE];
|
||||||
int page;
|
int page;
|
||||||
int i;
|
int i;
|
||||||
|
int search_limit, range_start, range_end, part_size;
|
||||||
|
|
||||||
READ_HEADER(&hdr, first_page);
|
READ_HEADER(&hdr, first_page);
|
||||||
|
for(search_limit = i = 0; i < sizeof(hdr.eof_hint) * CHAR_BIT; i++) {
|
||||||
|
if(hdr.eof_hint >> i) {
|
||||||
|
search_limit = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
part_size = hdr.max_pages / (sizeof(hdr.eof_hint) * CHAR_BIT);
|
||||||
|
range_start = part_size * search_limit;
|
||||||
|
range_end = range_start + part_size;
|
||||||
|
|
||||||
|
if(range_end > hdr.max_pages) {
|
||||||
|
range_end = hdr.max_pages;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Move from the end of the file towards the beginning and look for
|
* Move from the end of the range towards the beginning and look for
|
||||||
* a byte that has been modified.
|
* a byte that has been modified.
|
||||||
*
|
*
|
||||||
* An important implication of this is that if the last written bytes
|
* An important implication of this is that if the last written bytes
|
||||||
* are zeroes, then these are skipped from the calculation.
|
* are zeroes, then these are skipped from the calculation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
for(page = first_page + hdr.max_pages - 1; page >= first_page; page--) {
|
for(page = first_page + range_end; page >= first_page + range_start; page--) {
|
||||||
watchdog_periodic();
|
watchdog_periodic();
|
||||||
COFFEE_READ(buf, sizeof(buf), page * COFFEE_PAGE_SIZE);
|
COFFEE_READ(buf, sizeof(buf), page * COFFEE_PAGE_SIZE);
|
||||||
for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
|
for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
|
||||||
@ -329,6 +367,8 @@ find_contiguous_pages(unsigned wanted)
|
|||||||
}
|
}
|
||||||
/* Jump to the next sector. */
|
/* Jump to the next sector. */
|
||||||
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
||||||
|
} else if(COFFEE_PAGE_ISOLATED(hdr)) {
|
||||||
|
++page;
|
||||||
} else {
|
} else {
|
||||||
start = -1;
|
start = -1;
|
||||||
page += hdr.max_pages;
|
page += hdr.max_pages;
|
||||||
@ -361,21 +401,17 @@ cfs_garbage_collect(void)
|
|||||||
COFFEE_ERASE(sector);
|
COFFEE_ERASE(sector);
|
||||||
PRINTF("Coffee: Erased sector %d!\n", sector);
|
PRINTF("Coffee: Erased sector %d!\n", sector);
|
||||||
++sectors_in_row;
|
++sectors_in_row;
|
||||||
} else {
|
|
||||||
if(sectors_in_row > longest_row) {
|
if(sectors_in_row > longest_row) {
|
||||||
longest_row = sectors_in_row;
|
longest_row = sectors_in_row;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
sectors_in_row = 0;
|
sectors_in_row = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sectors_in_row > longest_row) {
|
|
||||||
longest_row = sectors_in_row;
|
|
||||||
}
|
|
||||||
|
|
||||||
watchdog_start();
|
watchdog_start();
|
||||||
|
|
||||||
return longest_row * (COFFEE_PAGES_PER_SECTOR);
|
return longest_row * COFFEE_PAGES_PER_SECTOR;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static int
|
static int
|
||||||
@ -653,7 +689,7 @@ write_log_page(struct file_desc *fdp, struct log_param *lp)
|
|||||||
|
|
||||||
if(log_entry >= log_entries) {
|
if(log_entry >= log_entries) {
|
||||||
/* The log is full. The new file should be written out. */
|
/* The log is full. The new file should be written out. */
|
||||||
PRINTF("Coffee: The log for file %s is full\n", hdr.name);
|
PRINTF("Coffee: Flushing the log for file %s\n", hdr.name);
|
||||||
fdp->next_log_entry = 0;
|
fdp->next_log_entry = 0;
|
||||||
return flush_log(fdp->file_page);
|
return flush_log(fdp->file_page);
|
||||||
}
|
}
|
||||||
@ -732,7 +768,8 @@ cfs_open(const char *name, int flags)
|
|||||||
if((page = cfs_coffee_reserve(name, COFFEE_DYN_SIZE)) < 0) {
|
if((page = cfs_coffee_reserve(name, COFFEE_DYN_SIZE)) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
coffee_fd_set[fd].max_pages = COFFEE_DYN_SIZE / COFFEE_PAGE_SIZE;
|
coffee_fd_set[fd].max_pages = (COFFEE_DYN_SIZE + sizeof(hdr) +
|
||||||
|
COFFEE_PAGE_SIZE - 1) / COFFEE_PAGE_SIZE;
|
||||||
} else {
|
} else {
|
||||||
READ_HEADER(&hdr, page);
|
READ_HEADER(&hdr, page);
|
||||||
coffee_fd_set[fd].max_pages = hdr.max_pages;
|
coffee_fd_set[fd].max_pages = hdr.max_pages;
|
||||||
@ -750,7 +787,25 @@ cfs_open(const char *name, int flags)
|
|||||||
void
|
void
|
||||||
cfs_close(int fd)
|
cfs_close(int fd)
|
||||||
{
|
{
|
||||||
|
struct file_header hdr;
|
||||||
|
uint16_t current_page, i;
|
||||||
|
int part_size;
|
||||||
|
uint8_t eof_hint;
|
||||||
|
|
||||||
if(FD_VALID(fd)) {
|
if(FD_VALID(fd)) {
|
||||||
|
READ_HEADER(&hdr, coffee_fd_set[fd].file_page);
|
||||||
|
current_page = (coffee_fd_set[fd].end + COFFEE_PAGE_SIZE - 1) / COFFEE_PAGE_SIZE;
|
||||||
|
part_size = hdr.max_pages / (sizeof(hdr.eof_hint) * CHAR_BIT);
|
||||||
|
|
||||||
|
for(i = eof_hint = 0; i < sizeof(eof_hint) * CHAR_BIT; i++) {
|
||||||
|
eof_hint |= (current_page > (i + 1) * part_size) << i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(eof_hint > hdr.eof_hint) {
|
||||||
|
hdr.eof_hint |= eof_hint;
|
||||||
|
WRITE_HEADER(&hdr, coffee_fd_set[fd].file_page);
|
||||||
|
}
|
||||||
|
|
||||||
coffee_fd_set[fd].flags = COFFEE_FD_FREE;
|
coffee_fd_set[fd].flags = COFFEE_FD_FREE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -918,16 +973,19 @@ cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *entry)
|
|||||||
for(page = *(uint16_t *)dir->dummy_space; page < COFFEE_PAGE_COUNT;) {
|
for(page = *(uint16_t *)dir->dummy_space; page < COFFEE_PAGE_COUNT;) {
|
||||||
watchdog_periodic();
|
watchdog_periodic();
|
||||||
READ_HEADER(&hdr, page);
|
READ_HEADER(&hdr, page);
|
||||||
|
if(COFFEE_PAGE_FREE(hdr)) {
|
||||||
|
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
||||||
|
} else if(COFFEE_PAGE_ISOLATED(hdr)) {
|
||||||
|
++page;
|
||||||
|
} else {
|
||||||
if(COFFEE_PAGE_ACTIVE(hdr) && !COFFEE_PAGE_LOG(hdr)) {
|
if(COFFEE_PAGE_ACTIVE(hdr) && !COFFEE_PAGE_LOG(hdr)) {
|
||||||
memcpy(entry->name, hdr.name, sizeof(entry->name));
|
memcpy(entry->name, hdr.name, sizeof(entry->name));
|
||||||
entry->name[sizeof(entry->name) - 1] = '\0';
|
entry->name[sizeof(entry->name) - 1] = '\0';
|
||||||
entry->size = find_offset_in_file(page);
|
entry->size = find_offset_in_file(page);
|
||||||
page += hdr.max_pages;
|
|
||||||
*(uint16_t *)dir->dummy_space = page;
|
*(uint16_t *)dir->dummy_space = page;
|
||||||
|
page += hdr.max_pages;
|
||||||
return 0;
|
return 0;
|
||||||
} else if (COFFEE_PAGE_FREE(hdr)) {
|
}
|
||||||
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
|
||||||
} else {
|
|
||||||
page += hdr.max_pages;
|
page += hdr.max_pages;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user