2008-04-28 12:33:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2008-08-15 14:34:56 +02:00
|
|
|
* Coffee: A flash file system for memory-contrained sensor systems.
|
2008-04-28 12:33:14 +02:00
|
|
|
* \author
|
|
|
|
* Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
#include "contiki-conf.h"
|
2008-04-28 12:33:14 +02:00
|
|
|
#include "cfs/cfs.h"
|
2008-07-02 13:11:13 +02:00
|
|
|
#include "cfs-coffee-arch.h"
|
2008-04-28 12:33:14 +02:00
|
|
|
#include "cfs/cfs-coffee.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
|
2008-08-25 11:57:38 +02:00
|
|
|
#include <limits.h>
|
2008-04-28 12:33:14 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-07-02 14:06:02 +02:00
|
|
|
#define DEBUG 0
|
2008-04-28 12:33:14 +02:00
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2008-08-16 00:38:43 +02:00
|
|
|
#if COFFEE_PAGES_PER_SECTOR & (COFFEE_PAGES_PER_SECTOR - 1)
|
|
|
|
#error COFFEE_PAGES_PER_SECTOR must be a power of two.
|
|
|
|
#error Change COFFEE_PAGES_PER_SECTOR in cfs-coffee-arch.h.
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
#define kb * 1024UL
|
|
|
|
#define Mb * (1024 kb)
|
|
|
|
|
2008-09-21 17:01:48 +02:00
|
|
|
#define COFFEE_FD_FREE 0x0
|
2008-07-02 13:11:13 +02:00
|
|
|
#define COFFEE_FD_READ 0x1
|
|
|
|
#define COFFEE_FD_WRITE 0x2
|
|
|
|
#define COFFEE_FD_APPEND 0x4
|
2008-09-21 17:01:48 +02:00
|
|
|
#define COFFEE_FD_MODIFIED 0x8
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-08-25 11:57:38 +02:00
|
|
|
#define FD_VALID(fd) \
|
2008-09-21 14:49:08 +02:00
|
|
|
((fd) >= 0 && (fd) < COFFEE_FD_SET_SIZE && \
|
2008-08-25 11:57:38 +02:00
|
|
|
coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
|
|
|
|
#define FD_READABLE(fd) (coffee_fd_set[(fd)].flags & CFS_READ)
|
|
|
|
#define FD_WRITABLE(fd) (coffee_fd_set[(fd)].flags & CFS_WRITE)
|
|
|
|
#define FD_APPENDABLE(fd) (coffee_fd_set[(fd)].flags & CFS_APPEND)
|
2008-09-21 17:01:48 +02:00
|
|
|
#define FD_MODIFIED(fd) (coffee_fd_set[(fd)].flags & COFFEE_FD_MODIFIED)
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-08-25 11:57:38 +02:00
|
|
|
/* 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. */
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-11-19 00:10:30 +01:00
|
|
|
#define COFFEE_FILE_ALLOCATED(hdr) ((hdr).flags & COFFEE_FLAG_ALLOCATED)
|
|
|
|
#define COFFEE_FILE_FREE(hdr) !COFFEE_FILE_ALLOCATED(hdr)
|
|
|
|
#define COFFEE_FILE_LOG(hdr) ((hdr).flags & COFFEE_FLAG_LOG)
|
|
|
|
#define COFFEE_FILE_MODIFIED(hdr) ((hdr).flags & COFFEE_FLAG_MODIFIED)
|
|
|
|
#define COFFEE_FILE_ISOLATED(hdr) ((hdr).flags & COFFEE_FLAG_ISOLATED)
|
|
|
|
#define COFFEE_FILE_OBSOLETE(hdr) ((hdr).flags & COFFEE_FLAG_OBSOLETE)
|
|
|
|
#define COFFEE_FILE_ACTIVE(hdr) (COFFEE_FILE_ALLOCATED(hdr) && \
|
|
|
|
!COFFEE_FILE_OBSOLETE(hdr) && \
|
|
|
|
!COFFEE_FILE_ISOLATED(hdr))
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-08-25 11:57:38 +02:00
|
|
|
#define COFFEE_PAGE_COUNT (COFFEE_SIZE / COFFEE_PAGE_SIZE)
|
|
|
|
#define COFFEE_PAGES_PER_SECTOR (COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE)
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-07-11 23:39:26 +02:00
|
|
|
struct file_desc {
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t offset;
|
|
|
|
coffee_offset_t end;
|
|
|
|
coffee_page_t file_page;
|
|
|
|
coffee_page_t max_pages;
|
2008-11-19 00:10:30 +01:00
|
|
|
int16_t next_log_record;
|
2008-08-15 14:34:56 +02:00
|
|
|
uint8_t flags;
|
2008-07-11 23:39:26 +02:00
|
|
|
};
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
struct dir_cache {
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t page;
|
2008-07-11 23:39:26 +02:00
|
|
|
char filename_start;
|
2008-07-02 13:11:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct file_header {
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t log_page;
|
2008-11-05 00:09:55 +01:00
|
|
|
uint16_t log_records;
|
|
|
|
uint16_t log_record_size;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t max_pages;
|
2008-08-15 14:34:56 +02:00
|
|
|
uint8_t eof_hint;
|
|
|
|
uint8_t flags;
|
2008-07-02 13:11:13 +02:00
|
|
|
char name[COFFEE_NAME_LENGTH];
|
|
|
|
} __attribute__((packed));
|
|
|
|
|
|
|
|
/* This is needed because of a buggy compiler. */
|
|
|
|
struct log_param {
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t offset;
|
2008-07-02 13:11:13 +02:00
|
|
|
const char *buf;
|
|
|
|
uint16_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ABS_OFFSET(file_page, file_offset) \
|
|
|
|
((file_page) * COFFEE_PAGE_SIZE + \
|
2008-08-25 11:57:38 +02:00
|
|
|
sizeof(struct file_header) + (file_offset))
|
2008-07-02 13:11:13 +02:00
|
|
|
|
2008-11-05 00:12:44 +01:00
|
|
|
static struct dir_cache dir_cache[COFFEE_DIR_CACHE_ENTRIES];
|
2008-07-04 01:29:58 +02:00
|
|
|
static struct file_desc coffee_fd_set[COFFEE_FD_SET_SIZE];
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
write_header(struct file_header *hdr, coffee_page_t page)
|
|
|
|
{
|
|
|
|
COFFEE_WRITE(hdr, sizeof(*hdr), page * COFFEE_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
read_header(struct file_header *hdr, coffee_page_t page)
|
|
|
|
{
|
|
|
|
COFFEE_READ(hdr, sizeof(*hdr), page * COFFEE_PAGE_SIZE);
|
|
|
|
}
|
2008-04-28 12:33:14 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2008-10-03 21:27:28 +02:00
|
|
|
get_sector_status(uint16_t sector, coffee_page_t *active,
|
|
|
|
coffee_page_t *free, coffee_page_t *obsolete) {
|
2008-11-04 18:48:36 +01:00
|
|
|
static coffee_page_t skip_pages;
|
|
|
|
static int last_pages_are_active;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t offset, sector_start;
|
|
|
|
coffee_offset_t end;
|
2008-04-28 12:33:14 +02:00
|
|
|
struct file_header hdr;
|
2008-11-04 18:48:36 +01:00
|
|
|
coffee_page_t jump;
|
2008-08-25 11:57:38 +02:00
|
|
|
int i;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
*active = *free = *obsolete = 0;
|
2008-07-02 13:11:13 +02:00
|
|
|
if(sector == 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
skip_pages = 0;
|
|
|
|
last_pages_are_active = 0;
|
2008-07-02 13:11:13 +02:00
|
|
|
} else if(skip_pages > COFFEE_PAGES_PER_SECTOR) {
|
2008-04-28 12:33:14 +02:00
|
|
|
skip_pages -= COFFEE_PAGES_PER_SECTOR;
|
2008-07-02 13:11:13 +02:00
|
|
|
if(last_pages_are_active) {
|
2008-04-28 12:33:14 +02:00
|
|
|
*active = COFFEE_PAGES_PER_SECTOR;
|
|
|
|
} else {
|
|
|
|
*obsolete = COFFEE_PAGES_PER_SECTOR;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-08-25 11:57:38 +02:00
|
|
|
|
|
|
|
sector_start = sector * COFFEE_SECTOR_SIZE;
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
if(last_pages_are_active) {
|
2008-04-28 12:33:14 +02:00
|
|
|
*active = skip_pages;
|
|
|
|
} else {
|
|
|
|
*obsolete = skip_pages;
|
2008-08-25 11:57:38 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
PRINTF("Coffee: Isolated %u pages starting in sector %d\n",
|
|
|
|
(unsigned)skip_pages, (int)sector);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
2008-08-25 11:57:38 +02:00
|
|
|
|
|
|
|
offset = sector_start + skip_pages * COFFEE_PAGE_SIZE;
|
2008-04-28 12:33:14 +02:00
|
|
|
end = (sector + 1) * COFFEE_SECTOR_SIZE;
|
2008-11-04 18:48:36 +01:00
|
|
|
jump = 0;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
while(offset < end) {
|
2008-08-25 11:57:38 +02:00
|
|
|
COFFEE_READ(&hdr, sizeof(hdr), offset);
|
2008-11-04 18:48:36 +01:00
|
|
|
last_pages_are_active = 0;
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_ACTIVE(hdr)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
last_pages_are_active = 1;
|
2008-11-04 18:48:36 +01:00
|
|
|
jump = hdr.max_pages;
|
|
|
|
*active += jump;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_ISOLATED(hdr)) {
|
2008-11-04 18:48:36 +01:00
|
|
|
jump = 1;
|
2008-08-25 11:57:38 +02:00
|
|
|
*obsolete++;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_OBSOLETE(hdr)) {
|
2008-11-04 18:48:36 +01:00
|
|
|
jump = hdr.max_pages;
|
|
|
|
*obsolete += jump;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_FREE(hdr)) {
|
2008-08-15 14:34:56 +02:00
|
|
|
*free = (end - offset) / COFFEE_PAGE_SIZE;
|
|
|
|
break;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
2008-11-04 18:48:36 +01:00
|
|
|
offset += jump * COFFEE_PAGE_SIZE;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 14:34:56 +02:00
|
|
|
skip_pages = *active + *obsolete - COFFEE_PAGES_PER_SECTOR;
|
2008-07-02 13:11:13 +02:00
|
|
|
if(skip_pages > 0) {
|
|
|
|
if(last_pages_are_active) {
|
2008-08-15 14:34:56 +02:00
|
|
|
*active = COFFEE_PAGES_PER_SECTOR - *obsolete;
|
2008-04-28 12:33:14 +02:00
|
|
|
} else {
|
2008-08-15 14:34:56 +02:00
|
|
|
*obsolete = COFFEE_PAGES_PER_SECTOR - *active;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2008-10-03 21:27:28 +02:00
|
|
|
dir_cache_add(char c, coffee_offset_t page)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-11-05 00:12:44 +01:00
|
|
|
for(i = 0; i < COFFEE_DIR_CACHE_ENTRIES; i++) {
|
2008-07-02 13:11:13 +02:00
|
|
|
if(dir_cache[i].filename_start == '\0') {
|
2008-04-28 12:33:14 +02:00
|
|
|
dir_cache[i].filename_start = c;
|
|
|
|
dir_cache[i].page = page;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2008-10-03 21:27:28 +02:00
|
|
|
dir_cache_del(coffee_offset_t page)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-11-05 00:12:44 +01:00
|
|
|
for(i = 0; i < COFFEE_DIR_CACHE_ENTRIES; i++) {
|
2008-07-02 13:11:13 +02:00
|
|
|
if(dir_cache[i].page == page) {
|
2008-04-28 12:33:14 +02:00
|
|
|
dir_cache[i].filename_start = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-03 21:27:28 +02:00
|
|
|
static coffee_offset_t
|
2008-04-28 12:33:14 +02:00
|
|
|
dir_cache_find(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct file_header hdr;
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-11-05 00:12:44 +01:00
|
|
|
for(i = 0; i < COFFEE_DIR_CACHE_ENTRIES; i++) {
|
2008-07-02 13:11:13 +02:00
|
|
|
if(*name == dir_cache[i].filename_start) {
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, dir_cache[i].page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(!COFFEE_FILE_ACTIVE(hdr)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
dir_cache[i].filename_start = '\0';
|
2008-07-02 13:11:13 +02:00
|
|
|
} else if(strcmp(hdr.name, name) == 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return dir_cache[i].page;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 10:09:17 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-03 21:27:28 +02:00
|
|
|
static coffee_offset_t
|
2008-04-28 12:33:14 +02:00
|
|
|
find_file(const char *name)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-11-11 00:07:53 +01:00
|
|
|
coffee_page_t page;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
page = dir_cache_find(name);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(page >= 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return page;
|
|
|
|
}
|
2008-11-17 10:09:17 +01:00
|
|
|
|
2008-07-04 01:29:58 +02:00
|
|
|
page = 0;
|
2008-04-28 12:33:14 +02:00
|
|
|
do {
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_ACTIVE(hdr)) {
|
2008-11-13 15:46:32 +01:00
|
|
|
if(dir_cache_find(name) == -1) {
|
2008-04-28 12:33:14 +02:00
|
|
|
dir_cache_add(name[0], page);
|
2008-11-11 00:07:53 +01:00
|
|
|
}
|
|
|
|
if(strcmp(name, hdr.name) == 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return page;
|
|
|
|
}
|
2008-07-04 01:29:58 +02:00
|
|
|
page += hdr.max_pages;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_ISOLATED(hdr)) {
|
2008-08-25 11:57:38 +02:00
|
|
|
++page;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_OBSOLETE(hdr)) {
|
2008-08-28 14:50:09 +02:00
|
|
|
page += hdr.max_pages;
|
2008-04-28 12:33:14 +02:00
|
|
|
} else {
|
2008-07-04 04:54:30 +02:00
|
|
|
/* It follows from the properties of the page allocation algorithm
|
2008-08-15 14:34:56 +02:00
|
|
|
that if a free page is encountered, then the rest of the sector
|
|
|
|
is also free. */
|
2008-07-04 04:54:30 +02:00
|
|
|
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
watchdog_periodic();
|
2008-07-04 01:29:58 +02:00
|
|
|
} while(page < COFFEE_PAGE_COUNT);
|
2008-11-17 10:09:17 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-11-19 00:10:30 +01:00
|
|
|
static void
|
|
|
|
update_eof_hint(coffee_page_t file_page, coffee_offset_t end)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
coffee_page_t current_page;
|
|
|
|
int part_size, i;
|
|
|
|
uint8_t eof_hint;
|
|
|
|
|
|
|
|
read_header(&hdr, file_page);
|
|
|
|
current_page = (end + sizeof(hdr)) / COFFEE_PAGE_SIZE;
|
|
|
|
part_size = hdr.max_pages / (sizeof(hdr.eof_hint) * CHAR_BIT);
|
|
|
|
if(part_size == 0) {
|
|
|
|
part_size = 1;
|
|
|
|
}
|
|
|
|
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, file_page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-03 21:27:28 +02:00
|
|
|
static coffee_offset_t
|
2008-04-28 12:33:14 +02:00
|
|
|
find_offset_in_file(int first_page)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
unsigned char buf[COFFEE_PAGE_SIZE];
|
|
|
|
int page;
|
|
|
|
int i;
|
2008-08-28 16:31:10 +02:00
|
|
|
int search_limit;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t range_start, range_end, part_size;
|
2008-08-25 11:57:38 +02:00
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, first_page);
|
2008-11-03 21:21:43 +01:00
|
|
|
search_limit = 0;
|
2008-11-03 16:13:13 +01:00
|
|
|
for(i = 0; i < sizeof(hdr.eof_hint) * CHAR_BIT; i++) {
|
2008-08-25 11:57:38 +02:00
|
|
|
if(hdr.eof_hint >> i) {
|
|
|
|
search_limit = i + 1;
|
|
|
|
}
|
|
|
|
}
|
2008-08-28 14:50:09 +02:00
|
|
|
part_size = hdr.max_pages / sizeof(hdr.eof_hint) / CHAR_BIT;
|
2008-08-28 16:31:10 +02:00
|
|
|
if(part_size == 0) {
|
|
|
|
part_size = 1;
|
|
|
|
}
|
2008-11-03 21:21:43 +01:00
|
|
|
range_start = part_size * search_limit;
|
2008-08-25 11:57:38 +02:00
|
|
|
range_end = range_start + part_size;
|
2008-11-17 10:09:17 +01:00
|
|
|
if(range_end + 1 > hdr.max_pages) {
|
|
|
|
range_end = hdr.max_pages - 1;
|
2008-08-25 11:57:38 +02:00
|
|
|
}
|
|
|
|
|
2008-08-15 14:34:56 +02:00
|
|
|
/*
|
2008-08-25 11:57:38 +02:00
|
|
|
* Move from the end of the range towards the beginning and look for
|
2008-04-28 12:33:14 +02:00
|
|
|
* a byte that has been modified.
|
|
|
|
*
|
2008-08-15 14:34:56 +02:00
|
|
|
* An important implication of this is that if the last written bytes
|
2008-04-28 12:33:14 +02:00
|
|
|
* are zeroes, then these are skipped from the calculation.
|
2008-08-15 14:34:56 +02:00
|
|
|
*/
|
|
|
|
|
2008-11-17 10:09:17 +01:00
|
|
|
for(page = first_page + range_end; page >= first_page; page--) {
|
2008-04-28 12:33:14 +02:00
|
|
|
watchdog_periodic();
|
2008-08-25 11:57:38 +02:00
|
|
|
COFFEE_READ(buf, sizeof(buf), page * COFFEE_PAGE_SIZE);
|
2008-07-02 13:11:13 +02:00
|
|
|
for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
|
|
|
|
if(buf[i] != 0) {
|
|
|
|
if(page == first_page) {
|
2008-08-25 11:57:38 +02:00
|
|
|
return i < sizeof(hdr) ? 0 : 1 + i - sizeof(hdr);
|
2008-04-28 12:33:14 +02:00
|
|
|
} else {
|
2008-08-25 11:57:38 +02:00
|
|
|
return 1 + i + (page - first_page) * COFFEE_PAGE_SIZE - sizeof(hdr);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-03 21:21:43 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
/* All bytes are writable. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
find_contiguous_pages(unsigned wanted)
|
|
|
|
{
|
|
|
|
int page, start;
|
|
|
|
struct file_header hdr;
|
|
|
|
|
|
|
|
start = -1;
|
2008-07-02 13:11:13 +02:00
|
|
|
for(page = 0; page < COFFEE_PAGE_COUNT;) {
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_FREE(hdr)) {
|
2008-07-02 13:11:13 +02:00
|
|
|
if(start == -1) {
|
2008-04-28 12:33:14 +02:00
|
|
|
start = page;
|
|
|
|
} else {
|
2008-07-02 13:11:13 +02:00
|
|
|
if(start + wanted <= page) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
}
|
2008-08-16 00:38:43 +02:00
|
|
|
/* Jump to the next sector. */
|
2008-08-15 14:34:56 +02:00
|
|
|
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_ISOLATED(hdr)) {
|
2008-08-25 11:57:38 +02:00
|
|
|
++page;
|
2008-04-28 12:33:14 +02:00
|
|
|
} else {
|
|
|
|
start = -1;
|
|
|
|
page += hdr.max_pages;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
cfs_garbage_collect(void)
|
|
|
|
{
|
|
|
|
uint16_t sector;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t active_pages, free_pages, obsolete_pages;
|
2008-11-04 18:23:59 +01:00
|
|
|
int nerased;
|
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
watchdog_stop();
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
PRINTF("Coffee: Running the file system garbage collector...\n");
|
2008-04-28 12:33:14 +02:00
|
|
|
/*
|
|
|
|
* The garbage collector erases as many sectors as possible. A sector is
|
|
|
|
* erasable if there are only free or obsolete pages in it.
|
|
|
|
*/
|
2008-11-04 18:23:59 +01:00
|
|
|
for(nerased = sector = 0; sector < COFFEE_SIZE / COFFEE_SECTOR_SIZE; sector++) {
|
2008-04-28 12:33:14 +02:00
|
|
|
get_sector_status(sector, &active_pages, &free_pages, &obsolete_pages);
|
2008-07-02 13:11:13 +02:00
|
|
|
PRINTF("Coffee: Sector %u has %u active, %u free, and %u obsolete pages.\n",
|
2008-11-11 00:07:53 +01:00
|
|
|
sector, (unsigned)active_pages, (unsigned)free_pages, (unsigned)obsolete_pages);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(active_pages == 0 && obsolete_pages > 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
COFFEE_ERASE(sector);
|
2008-11-04 18:23:59 +01:00
|
|
|
nerased++;
|
2008-07-02 13:11:13 +02:00
|
|
|
PRINTF("Coffee: Erased sector %d!\n", sector);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
watchdog_start();
|
2008-11-04 18:23:59 +01:00
|
|
|
return nerased;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-10-03 21:27:28 +02:00
|
|
|
remove_by_page(coffee_page_t page, int remove_log, int close_fds)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
int i;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t log_page;
|
2008-08-16 00:38:43 +02:00
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
if(page >= COFFEE_PAGE_COUNT) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(!COFFEE_FILE_ACTIVE(hdr)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_cache_del(page);
|
|
|
|
hdr.flags |= COFFEE_FLAG_OBSOLETE;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(remove_log && COFFEE_FILE_MODIFIED(hdr)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
log_page = hdr.log_page;
|
|
|
|
dir_cache_del(log_page);
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, log_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
hdr.flags |= COFFEE_FLAG_OBSOLETE;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(&hdr, log_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close all file descriptors that reference the remove file. */
|
2008-07-02 13:11:13 +02:00
|
|
|
if(close_fds) {
|
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2008-07-04 01:29:58 +02:00
|
|
|
if(coffee_fd_set[i].file_page == page) {
|
|
|
|
coffee_fd_set[i].flags = COFFEE_FD_FREE;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-10-29 16:36:21 +01:00
|
|
|
reserve(const char *name, uint32_t size, int allow_duplicates)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
unsigned need_pages;
|
|
|
|
int page;
|
|
|
|
|
2008-11-19 14:42:07 +01:00
|
|
|
watchdog_stop();
|
|
|
|
|
2008-10-29 16:36:21 +01:00
|
|
|
if(!allow_duplicates && find_file(name) >= 0) {
|
2008-11-19 14:42:07 +01:00
|
|
|
watchdog_start();
|
2008-10-29 16:36:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
need_pages = (size + sizeof(hdr) + COFFEE_PAGE_SIZE - 1) / COFFEE_PAGE_SIZE;
|
|
|
|
|
|
|
|
page = find_contiguous_pages(need_pages);
|
|
|
|
if(page < 0) {
|
|
|
|
cfs_garbage_collect();
|
|
|
|
page = find_contiguous_pages(need_pages);
|
|
|
|
if(page < 0) {
|
2008-11-04 18:23:59 +01:00
|
|
|
watchdog_start();
|
2008-10-29 16:36:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(hdr.name, name, sizeof(hdr.name));
|
|
|
|
hdr.name[sizeof(hdr.name) - 1] = '\0';
|
|
|
|
hdr.max_pages = need_pages;
|
|
|
|
hdr.flags = COFFEE_FLAG_ALLOCATED | COFFEE_FLAG_VALID;
|
|
|
|
hdr.log_page = 0;
|
|
|
|
hdr.eof_hint = 0;
|
2008-11-05 00:09:55 +01:00
|
|
|
hdr.log_records = 0;
|
|
|
|
hdr.log_record_size = 0;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(&hdr, page);
|
2008-10-29 16:36:21 +01:00
|
|
|
|
|
|
|
PRINTF("Coffee: Reserved %u pages starting from %u for file %s\n",
|
|
|
|
need_pages, page, name);
|
|
|
|
dir_cache_add(name[0], page);
|
|
|
|
|
2008-11-04 18:23:59 +01:00
|
|
|
watchdog_start();
|
|
|
|
|
2008-10-29 16:36:21 +01:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-11-05 00:09:55 +01:00
|
|
|
static void
|
|
|
|
adjust_log_config(struct file_header *hdr,
|
|
|
|
uint16_t *log_record_size, uint16_t *log_records)
|
|
|
|
{
|
|
|
|
*log_record_size = hdr->log_record_size == 0 ?
|
|
|
|
COFFEE_PAGE_SIZE : hdr->log_record_size;
|
|
|
|
*log_records = hdr->log_records == 0 ?
|
|
|
|
COFFEE_LOG_SIZE / *log_record_size : hdr->log_records;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-11-05 00:30:47 +01:00
|
|
|
static uint16_t
|
|
|
|
modify_log_buffer(uint16_t log_record_size,
|
|
|
|
coffee_offset_t *offset, uint16_t *size)
|
|
|
|
{
|
|
|
|
uint16_t region;
|
|
|
|
|
|
|
|
region = *offset / log_record_size;
|
|
|
|
*offset %= log_record_size;
|
|
|
|
if(*size > log_record_size - *offset) {
|
|
|
|
*size = log_record_size - *offset;
|
|
|
|
}
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-29 16:36:21 +01:00
|
|
|
static int
|
2008-11-17 15:37:23 +01:00
|
|
|
get_record_index(coffee_page_t log_page, uint16_t search_records,
|
|
|
|
uint16_t region)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
2008-11-17 15:37:23 +01:00
|
|
|
coffee_offset_t base;
|
2008-11-05 00:09:55 +01:00
|
|
|
uint16_t processed;
|
2008-11-17 11:34:57 +01:00
|
|
|
uint16_t batch_size;
|
|
|
|
int16_t match_index, i;
|
2008-11-17 15:37:23 +01:00
|
|
|
uint16_t record_count;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-17 15:37:23 +01:00
|
|
|
base = ABS_OFFSET(log_page, sizeof(uint16_t) * search_records);
|
|
|
|
record_count = search_records > COFFEE_LOG_TABLE_LIMIT ?
|
|
|
|
COFFEE_LOG_TABLE_LIMIT : search_records;
|
2008-11-05 00:09:55 +01:00
|
|
|
processed = 0;
|
|
|
|
match_index = -1;
|
2008-11-17 15:37:23 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
uint16_t indices[record_count];
|
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
while(processed < search_records && match_index < 0) {
|
|
|
|
if(record_count + processed > search_records) {
|
2008-11-11 00:07:53 +01:00
|
|
|
batch_size = search_records - processed;
|
2008-11-05 00:09:55 +01:00
|
|
|
} else {
|
2008-11-11 00:07:53 +01:00
|
|
|
batch_size = record_count;
|
2008-11-05 00:09:55 +01:00
|
|
|
}
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-11 00:07:53 +01:00
|
|
|
base -= batch_size * sizeof(indices[0]);
|
|
|
|
COFFEE_READ(&indices, sizeof(indices[0]) * batch_size, base);
|
2008-08-28 14:50:09 +02:00
|
|
|
|
2008-11-11 00:07:53 +01:00
|
|
|
for(i = batch_size - 1; i >= 0; i--) {
|
2008-11-05 00:09:55 +01:00
|
|
|
if(indices[i] - 1 == region) {
|
2008-11-11 00:07:53 +01:00
|
|
|
match_index = search_records - processed - (batch_size - i);
|
2008-11-05 00:09:55 +01:00
|
|
|
break;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-28 14:50:09 +02:00
|
|
|
|
2008-11-11 00:07:53 +01:00
|
|
|
processed += batch_size;
|
2008-11-05 00:09:55 +01:00
|
|
|
}
|
2008-11-17 15:37:23 +01:00
|
|
|
}
|
2008-11-19 00:10:30 +01:00
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
return match_index;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
read_log_page(struct file_header *hdr, int16_t last_record, struct log_param *lp)
|
|
|
|
{
|
|
|
|
uint16_t region;
|
|
|
|
int16_t match_index;
|
|
|
|
uint16_t log_record_size;
|
|
|
|
uint16_t log_records;
|
2008-11-17 15:37:23 +01:00
|
|
|
coffee_offset_t base;
|
2008-11-05 00:09:55 +01:00
|
|
|
uint16_t search_records;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
adjust_log_config(hdr, &log_record_size, &log_records);
|
2008-11-05 00:30:47 +01:00
|
|
|
region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
|
2008-11-05 00:09:55 +01:00
|
|
|
|
|
|
|
search_records = last_record < 0 ? log_records : last_record + 1;
|
2008-11-17 15:37:23 +01:00
|
|
|
match_index = get_record_index(hdr->log_page, search_records, region);
|
2008-11-05 00:09:55 +01:00
|
|
|
if(match_index < 0) {
|
|
|
|
return -1;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
base = hdr->log_page * COFFEE_PAGE_SIZE;
|
|
|
|
base += sizeof(struct file_header) + log_records * sizeof(region);
|
2008-11-17 15:37:23 +01:00
|
|
|
base += (coffee_offset_t)match_index * log_record_size;
|
2008-11-05 00:09:55 +01:00
|
|
|
base += lp->offset;
|
|
|
|
COFFEE_READ(lp->buf, lp->size, base);
|
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return lp->size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static unsigned char *
|
|
|
|
create_log_name(unsigned char *new, int max_size, unsigned char *old)
|
|
|
|
{
|
|
|
|
unsigned char suffix[] = ".log";
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = strlen(old);
|
2008-08-25 11:57:38 +02:00
|
|
|
if(len > max_size - sizeof(suffix)) {
|
|
|
|
len = max_size - sizeof(suffix);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
memcpy(new, old, len);
|
2008-08-25 11:57:38 +02:00
|
|
|
memcpy(&new[len], suffix, sizeof(suffix));
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-10-03 21:27:28 +02:00
|
|
|
static coffee_page_t
|
|
|
|
create_log(coffee_page_t file_page, struct file_header *hdr)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t log_page;
|
2008-08-25 11:57:38 +02:00
|
|
|
unsigned char log_name[sizeof(hdr->name)];
|
2008-11-05 00:09:55 +01:00
|
|
|
uint16_t log_record_size, log_records;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t size;
|
2008-09-21 17:01:48 +02:00
|
|
|
int i;
|
2008-08-25 11:57:38 +02:00
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
adjust_log_config(hdr, &log_record_size, &log_records);
|
|
|
|
|
2008-11-11 00:07:53 +01:00
|
|
|
size = log_records * sizeof(uint16_t); /* Log index size. */
|
|
|
|
size += log_records * log_record_size; /* Log data size. */
|
2008-08-28 14:50:09 +02:00
|
|
|
|
2008-10-28 15:47:50 +01:00
|
|
|
log_page = reserve(create_log_name(log_name, sizeof(log_name), hdr->name),
|
|
|
|
size, 0);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(log_page < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
hdr->flags |= COFFEE_FLAG_MODIFIED;
|
|
|
|
hdr->log_page = log_page;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(hdr, file_page);
|
|
|
|
read_header(hdr, log_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
hdr->flags |= COFFEE_FLAG_LOG;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(hdr, log_page);
|
2008-09-21 17:01:48 +02:00
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2008-09-21 18:07:26 +02:00
|
|
|
if(coffee_fd_set[i].file_page == file_page &&
|
|
|
|
coffee_fd_set[i].flags != COFFEE_FD_FREE) {
|
2008-09-21 17:01:48 +02:00
|
|
|
coffee_fd_set[i].flags |= COFFEE_FD_MODIFIED;
|
|
|
|
}
|
|
|
|
}
|
2008-04-28 12:33:14 +02:00
|
|
|
return log_page;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-11-17 10:09:17 +01:00
|
|
|
merge_log(coffee_page_t file_page, int extend)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t log_page, new_file_page;
|
2008-04-28 12:33:14 +02:00
|
|
|
struct file_header hdr, hdr2;
|
|
|
|
int fd, n;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t offset;
|
2008-11-17 10:09:17 +01:00
|
|
|
coffee_page_t max_pages;
|
2008-08-25 11:57:38 +02:00
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, file_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
log_page = hdr.log_page;
|
2008-10-28 15:47:50 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
fd = cfs_open(hdr.name, CFS_READ);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(fd < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-08-25 11:57:38 +02:00
|
|
|
|
2008-09-21 14:49:08 +02:00
|
|
|
/*
|
|
|
|
* The reservation function adds extra space for the header, which has
|
2008-11-03 16:13:13 +01:00
|
|
|
* already been calculated with in the previous reservation.
|
2008-09-21 14:49:08 +02:00
|
|
|
*/
|
2008-11-17 10:09:17 +01:00
|
|
|
max_pages = hdr.max_pages * (1 << extend);
|
2008-11-03 16:13:13 +01:00
|
|
|
new_file_page = reserve(hdr.name,
|
2008-11-17 10:09:17 +01:00
|
|
|
max_pages * COFFEE_PAGE_SIZE - sizeof(hdr),
|
|
|
|
1);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(new_file_page < 0) {
|
2008-09-21 18:07:26 +02:00
|
|
|
cfs_close(fd);
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
do {
|
2008-11-05 00:09:55 +01:00
|
|
|
char buf[hdr.log_record_size == 0 ? COFFEE_PAGE_SIZE : hdr.log_record_size];
|
2008-07-02 17:18:43 +02:00
|
|
|
watchdog_periodic();
|
2008-08-25 11:57:38 +02:00
|
|
|
n = cfs_read(fd, buf, sizeof(buf));
|
2008-07-02 13:11:13 +02:00
|
|
|
if(n < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
remove_by_page(new_file_page, 0, 0);
|
|
|
|
cfs_close(fd);
|
|
|
|
return -1;
|
2008-07-02 13:11:13 +02:00
|
|
|
} else if(n > 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
COFFEE_WRITE(buf, n,
|
|
|
|
ABS_OFFSET(new_file_page, offset));
|
|
|
|
offset += n;
|
|
|
|
}
|
2008-07-02 13:11:13 +02:00
|
|
|
} while(n != 0);
|
2008-11-19 00:10:30 +01:00
|
|
|
|
2008-11-17 10:09:17 +01:00
|
|
|
if(remove_by_page(file_page, 1, 0) < 0) {
|
2008-11-19 00:10:30 +01:00
|
|
|
remove_by_page(new_file_page, 0, 0);
|
2008-04-28 12:33:14 +02:00
|
|
|
cfs_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-10-28 15:47:50 +01:00
|
|
|
|
2008-08-28 16:31:10 +02:00
|
|
|
/* Copy the log configuration and the EOF hint. */
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr2, new_file_page);
|
2008-11-05 00:09:55 +01:00
|
|
|
hdr2.log_record_size = hdr.log_record_size;
|
|
|
|
hdr2.log_records = hdr.log_records;
|
2008-08-28 16:31:10 +02:00
|
|
|
hdr2.eof_hint = hdr.eof_hint;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(&hdr2, new_file_page);
|
2008-08-28 16:31:10 +02:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
/* Point the file descriptors to the new file page. */
|
2008-07-02 13:11:13 +02:00
|
|
|
for(n = 0; n < COFFEE_FD_SET_SIZE; n++) {
|
2008-07-04 01:29:58 +02:00
|
|
|
if(coffee_fd_set[n].file_page == file_page) {
|
|
|
|
coffee_fd_set[n].file_page = new_file_page;
|
2008-11-17 10:09:17 +01:00
|
|
|
coffee_fd_set[n].max_pages = max_pages;
|
2008-09-21 18:07:26 +02:00
|
|
|
coffee_fd_set[n].flags &= ~COFFEE_FD_MODIFIED;
|
2008-11-19 00:10:30 +01:00
|
|
|
coffee_fd_set[n].next_log_record = -1;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfs_close(fd);
|
2008-10-28 15:47:50 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-11-17 15:37:23 +01:00
|
|
|
find_next_record(struct file_desc *fdp, coffee_page_t log_page,
|
|
|
|
int log_records)
|
|
|
|
{
|
|
|
|
int log_record, i, preferred_batch_size;
|
|
|
|
|
|
|
|
preferred_batch_size = log_records > COFFEE_LOG_TABLE_LIMIT ?
|
|
|
|
COFFEE_LOG_TABLE_LIMIT : log_records;
|
|
|
|
|
|
|
|
if(fdp->next_log_record == 0) {
|
|
|
|
/* The next log record is unknown. Search for it. */
|
|
|
|
uint16_t indices[preferred_batch_size];
|
|
|
|
uint16_t processed;
|
|
|
|
uint16_t batch_size;
|
|
|
|
|
|
|
|
log_record = log_records;
|
|
|
|
for(processed = 0; processed < log_records;) {
|
|
|
|
batch_size = log_records - processed >= preferred_batch_size ?
|
|
|
|
preferred_batch_size : log_records - processed;
|
|
|
|
COFFEE_READ(&indices, batch_size * sizeof(indices[0]),
|
|
|
|
ABS_OFFSET(log_page, processed * sizeof(indices[0])));
|
|
|
|
for(i = 0; i < batch_size && indices[i] != 0; i++);
|
|
|
|
log_record = i;
|
|
|
|
if(log_record < batch_size) {
|
|
|
|
log_record += processed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
processed += batch_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_record = fdp->next_log_record;
|
|
|
|
}
|
|
|
|
|
|
|
|
return log_record;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-04-28 12:33:14 +02:00
|
|
|
write_log_page(struct file_desc *fdp, struct log_param *lp)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-11-05 00:30:47 +01:00
|
|
|
uint16_t region;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t log_page;
|
2008-11-05 00:09:55 +01:00
|
|
|
int16_t log_record;
|
|
|
|
uint16_t log_record_size;
|
|
|
|
uint16_t log_records;
|
2008-11-17 15:37:23 +01:00
|
|
|
coffee_offset_t base;
|
2008-04-28 12:33:14 +02:00
|
|
|
struct log_param lp_out;
|
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, fdp->file_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-05 00:30:47 +01:00
|
|
|
adjust_log_config(&hdr, &log_record_size, &log_records);
|
|
|
|
region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
log_page = 0;
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_MODIFIED(hdr)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
/* A log structure has already been created. */
|
|
|
|
log_page = hdr.log_page;
|
2008-11-17 15:37:23 +01:00
|
|
|
log_record = find_next_record(fdp, log_page, log_records);
|
2008-11-05 00:09:55 +01:00
|
|
|
if(log_record >= log_records) {
|
2008-11-03 16:13:13 +01:00
|
|
|
/* The log is full; merge the log. */
|
|
|
|
PRINTF("Coffee: Merging the file %s with its log\n", hdr.name);
|
2008-11-19 00:10:30 +01:00
|
|
|
update_eof_hint(fdp->file_page, fdp->end);
|
2008-11-17 10:09:17 +01:00
|
|
|
return merge_log(fdp->file_page, 0);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Create a log structure. */
|
|
|
|
log_page = create_log(fdp->file_page, &hdr);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(log_page < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-07-02 13:11:13 +02:00
|
|
|
PRINTF("Coffee: Created a log structure for file %s at page %u\n",
|
2008-11-11 00:07:53 +01:00
|
|
|
hdr.name, (unsigned)log_page);
|
2008-04-28 12:33:14 +02:00
|
|
|
hdr.log_page = log_page;
|
2008-11-05 00:09:55 +01:00
|
|
|
log_record = 0;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2008-11-05 00:09:55 +01:00
|
|
|
unsigned char copy_buf[log_record_size];
|
2008-10-16 14:54:36 +02:00
|
|
|
|
2008-11-05 00:30:47 +01:00
|
|
|
lp_out.offset = region * log_record_size;
|
2008-04-28 12:33:14 +02:00
|
|
|
lp_out.buf = copy_buf;
|
2008-11-05 00:09:55 +01:00
|
|
|
lp_out.size = log_record_size;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
if((lp->offset > 0 || lp->size != log_record_size) &&
|
2008-11-05 00:30:47 +01:00
|
|
|
read_log_page(&hdr, fdp->next_log_record - 1, &lp_out) < 0) {
|
2008-08-25 11:57:38 +02:00
|
|
|
COFFEE_READ(copy_buf, sizeof(copy_buf),
|
2008-11-05 00:30:47 +01:00
|
|
|
ABS_OFFSET(fdp->file_page, region * log_record_size));
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((char *) ©_buf + lp->offset, lp->buf, lp->size);
|
|
|
|
|
2008-11-17 15:37:23 +01:00
|
|
|
base = (coffee_offset_t)log_page * COFFEE_PAGE_SIZE;
|
2008-08-25 11:57:38 +02:00
|
|
|
base += sizeof(hdr);
|
2008-11-05 00:30:47 +01:00
|
|
|
base += log_records * sizeof(region);
|
2008-11-17 15:37:23 +01:00
|
|
|
base += (coffee_offset_t)log_record * log_record_size;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
2008-08-25 11:57:38 +02:00
|
|
|
COFFEE_WRITE(copy_buf, sizeof(copy_buf), base);
|
2008-11-05 00:30:47 +01:00
|
|
|
++region;
|
|
|
|
COFFEE_WRITE(®ion, sizeof(region),
|
|
|
|
ABS_OFFSET(log_page, log_record * sizeof(region)));
|
2008-11-05 00:09:55 +01:00
|
|
|
fdp->next_log_record = log_record + 1;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return lp->size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
get_available_fd(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2008-07-04 01:29:58 +02:00
|
|
|
if(coffee_fd_set[i].flags == COFFEE_FD_FREE) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_open(const char *name, int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int page;
|
|
|
|
struct file_header hdr;
|
2008-11-04 17:58:52 +01:00
|
|
|
struct file_desc *fdp;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
fd = get_available_fd();
|
2008-07-02 13:11:13 +02:00
|
|
|
if(fd < 0) {
|
2008-09-21 18:07:26 +02:00
|
|
|
PRINTF("Coffee: Failed to allocate a new file descriptor!\n");
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-04 17:58:52 +01:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
page = find_file(name);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(page < 0) {
|
|
|
|
if((flags & (CFS_READ | CFS_WRITE)) == CFS_READ) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-10-28 15:47:50 +01:00
|
|
|
if((page = reserve(name, COFFEE_DYN_SIZE, 1)) < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-04 17:58:52 +01:00
|
|
|
fdp->max_pages = (COFFEE_DYN_SIZE + sizeof(hdr) +
|
|
|
|
COFFEE_PAGE_SIZE - 1) / COFFEE_PAGE_SIZE;
|
2008-04-28 12:33:14 +02:00
|
|
|
} else {
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_MODIFIED(hdr)) {
|
2008-11-04 17:58:52 +01:00
|
|
|
fdp->flags = COFFEE_FD_MODIFIED;
|
2008-09-21 17:30:51 +02:00
|
|
|
} else
|
2008-11-04 17:58:52 +01:00
|
|
|
fdp->max_pages = hdr.max_pages;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 17:58:52 +01:00
|
|
|
fdp->file_page = page;
|
|
|
|
fdp->flags |= flags;
|
|
|
|
fdp->end = find_offset_in_file(page);
|
|
|
|
fdp->offset = flags & CFS_APPEND ? fdp->end : 0;
|
2008-11-05 00:09:55 +01:00
|
|
|
fdp->next_log_record = 0;
|
2008-04-28 12:33:14 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cfs_close(int fd)
|
|
|
|
{
|
2008-07-02 13:11:13 +02:00
|
|
|
if(FD_VALID(fd)) {
|
2008-11-19 00:10:30 +01:00
|
|
|
update_eof_hint(coffee_fd_set[fd].file_page, coffee_fd_set[fd].end);
|
2008-07-04 01:29:58 +02:00
|
|
|
coffee_fd_set[fd].flags = COFFEE_FD_FREE;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
unsigned
|
|
|
|
cfs_seek(int fd, unsigned offset)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
if(!FD_VALID(fd)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, coffee_fd_set[fd].file_page);
|
2008-11-03 16:13:13 +01:00
|
|
|
|
|
|
|
/* Check if the offset is within the file boundary. */
|
2008-08-28 16:31:10 +02:00
|
|
|
if(sizeof(hdr) + offset >= hdr.max_pages * COFFEE_PAGE_SIZE ||
|
|
|
|
sizeof(hdr) + offset < offset) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-08-28 16:31:10 +02:00
|
|
|
|
2008-07-04 01:29:58 +02:00
|
|
|
if(coffee_fd_set[fd].end < offset) {
|
|
|
|
coffee_fd_set[fd].end = offset;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
2008-07-04 01:29:58 +02:00
|
|
|
return coffee_fd_set[fd].offset = offset;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-07-02 13:11:13 +02:00
|
|
|
cfs_coffee_remove(const char *name)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
|
|
|
int page;
|
|
|
|
|
|
|
|
page = find_file(name);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(page < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return remove_by_page(page, 1, 1);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_read(int fd, void *buf, unsigned size)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
struct file_desc *fdp;
|
2008-11-03 21:21:43 +01:00
|
|
|
unsigned remains, read_chunk;
|
2008-04-28 12:33:14 +02:00
|
|
|
int r;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_offset_t base, offset;
|
2008-04-28 12:33:14 +02:00
|
|
|
struct log_param lp;
|
|
|
|
|
2008-09-21 17:01:48 +02:00
|
|
|
if(!(FD_VALID(fd) && FD_READABLE(fd))) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-04 01:29:58 +02:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2008-07-02 13:11:13 +02:00
|
|
|
if(fdp->offset + size > fdp->end) {
|
2008-04-28 12:33:14 +02:00
|
|
|
size = fdp->end - fdp->offset;
|
|
|
|
}
|
|
|
|
|
2008-11-03 21:21:43 +01:00
|
|
|
read_chunk = COFFEE_PAGE_SIZE;
|
2008-09-21 17:30:51 +02:00
|
|
|
if(FD_MODIFIED(fd)) {
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, fdp->file_page);
|
2008-11-05 00:09:55 +01:00
|
|
|
if(hdr.log_record_size > 0) {
|
|
|
|
read_chunk = hdr.log_record_size;
|
2008-11-03 21:21:43 +01:00
|
|
|
}
|
2008-09-21 17:30:51 +02:00
|
|
|
}
|
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
remains = size;
|
|
|
|
base = fdp->offset;
|
|
|
|
offset = 0;
|
|
|
|
/*
|
|
|
|
* Fill the buffer by copying from the log in first hand, or the
|
2008-11-05 00:09:55 +01:00
|
|
|
* ordinary file if the page has no log record.
|
2008-04-28 12:33:14 +02:00
|
|
|
*/
|
2008-07-02 13:11:13 +02:00
|
|
|
while(remains) {
|
2008-07-02 17:18:43 +02:00
|
|
|
watchdog_periodic();
|
2008-04-28 12:33:14 +02:00
|
|
|
r = -1;
|
2008-09-21 17:01:48 +02:00
|
|
|
if(FD_MODIFIED(fd)) {
|
2008-04-28 12:33:14 +02:00
|
|
|
lp.offset = base + offset;
|
|
|
|
lp.buf = (char *)buf + offset;
|
|
|
|
lp.size = remains;
|
2008-11-05 00:30:47 +01:00
|
|
|
r = read_log_page(&hdr, fdp->next_log_record - 1, &lp);
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
2008-11-03 16:13:13 +01:00
|
|
|
/* Read from the original file if we cannot find the data in the log. */
|
2008-07-02 13:11:13 +02:00
|
|
|
if(r < 0) {
|
2008-11-03 21:21:43 +01:00
|
|
|
r = remains > read_chunk ? read_chunk : remains;
|
2008-04-28 12:33:14 +02:00
|
|
|
COFFEE_READ((char *) buf + offset, r,
|
|
|
|
ABS_OFFSET(fdp->file_page, base + offset));
|
|
|
|
}
|
|
|
|
remains -= r;
|
|
|
|
offset += r;
|
|
|
|
}
|
|
|
|
fdp->offset += offset;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_write(int fd, const void *buf, unsigned size)
|
|
|
|
{
|
|
|
|
struct file_desc *fdp;
|
|
|
|
int i;
|
|
|
|
struct log_param lp;
|
2008-10-16 11:58:57 +02:00
|
|
|
coffee_offset_t remains;
|
2008-11-13 01:58:51 +01:00
|
|
|
|
2008-09-21 17:01:48 +02:00
|
|
|
if(!(FD_VALID(fd) && FD_WRITABLE(fd))) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-04 01:29:58 +02:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2008-11-17 10:09:17 +01:00
|
|
|
/* Attempt to extend the file if we try to write past the end. */
|
|
|
|
while(size + fdp->offset + sizeof(struct file_header) >
|
2008-07-02 13:11:13 +02:00
|
|
|
fdp->max_pages * COFFEE_PAGE_SIZE) {
|
2008-11-19 00:10:30 +01:00
|
|
|
update_eof_hint(fdp->file_page, fdp->end);
|
2008-11-17 10:09:17 +01:00
|
|
|
if(merge_log(fdp->file_page, 1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
if(fdp->offset < fdp->end) {
|
2008-10-16 11:58:57 +02:00
|
|
|
remains = size;
|
|
|
|
while(remains) {
|
|
|
|
lp.offset = fdp->offset;
|
2008-11-03 21:21:43 +01:00
|
|
|
lp.buf = (char *)buf + size - remains;
|
2008-10-16 11:58:57 +02:00
|
|
|
lp.size = remains;
|
|
|
|
|
|
|
|
i = write_log_page(fdp, &lp);
|
|
|
|
if(i == 0) {
|
2008-11-19 14:42:07 +01:00
|
|
|
lp.offset = fdp->offset;
|
|
|
|
lp.buf = (char *)buf + size - remains;
|
|
|
|
lp.size = remains;
|
2008-11-03 16:13:13 +01:00
|
|
|
/* The file was merged with the log. Try again. */
|
2008-10-16 11:58:57 +02:00
|
|
|
i = write_log_page(fdp, &lp);
|
|
|
|
}
|
|
|
|
if(i < 0) {
|
|
|
|
return size - remains > 0 ? size - remains : -1;
|
|
|
|
}
|
|
|
|
remains -= i;
|
|
|
|
fdp->offset += i;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
COFFEE_WRITE(buf, size,
|
|
|
|
ABS_OFFSET(fdp->file_page, fdp->offset));
|
2008-10-16 11:58:57 +02:00
|
|
|
fdp->offset += size;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
2008-10-16 11:58:57 +02:00
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
if(fdp->offset > fdp->end) {
|
2008-04-28 12:33:14 +02:00
|
|
|
/*
|
|
|
|
* The file has been extended and the file descriptors
|
|
|
|
* referencing this file must be updated.
|
|
|
|
*/
|
2008-07-02 13:11:13 +02:00
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2008-07-04 01:29:58 +02:00
|
|
|
if(coffee_fd_set[i].file_page == fdp->file_page) {
|
|
|
|
coffee_fd_set[i].end = coffee_fd_set[i].offset;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_opendir(struct cfs_dir *dir, const char *name)
|
|
|
|
{
|
|
|
|
/* We have only a root directory. */
|
2008-07-02 13:11:13 +02:00
|
|
|
if(name[0] != '/' || name[1] != '\0') {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-03 21:27:28 +02:00
|
|
|
*(coffee_page_t *)dir->dummy_space = 0;
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-11-05 00:09:55 +01:00
|
|
|
cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t page;
|
|
|
|
|
|
|
|
for(page = *(coffee_page_t *)dir->dummy_space; page < COFFEE_PAGE_COUNT;) {
|
2008-04-28 12:33:14 +02:00
|
|
|
watchdog_periodic();
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_FREE(hdr)) {
|
2008-07-04 04:54:30 +02:00
|
|
|
page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_ISOLATED(hdr)) {
|
2008-08-25 11:57:38 +02:00
|
|
|
++page;
|
2008-11-19 00:10:30 +01:00
|
|
|
} else if(COFFEE_FILE_ACTIVE(hdr) && !COFFEE_FILE_LOG(hdr)) {
|
2008-11-05 00:09:55 +01:00
|
|
|
memcpy(record->name, hdr.name, sizeof(record->name));
|
|
|
|
record->name[sizeof(record->name) - 1] = '\0';
|
|
|
|
record->size = find_offset_in_file(page);
|
2008-08-28 14:50:09 +02:00
|
|
|
page += hdr.max_pages;
|
2008-10-03 21:27:28 +02:00
|
|
|
*(coffee_page_t *)dir->dummy_space = page;
|
2008-08-25 11:57:38 +02:00
|
|
|
return 0;
|
2008-08-28 14:50:09 +02:00
|
|
|
} else {
|
2008-07-04 04:54:30 +02:00
|
|
|
page += hdr.max_pages;
|
2008-04-28 12:33:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cfs_closedir(struct cfs_dir *dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-10-28 15:47:50 +01:00
|
|
|
cfs_coffee_reserve(const char *name, uint32_t size)
|
|
|
|
{
|
|
|
|
return reserve(name, size, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-11-05 00:30:47 +01:00
|
|
|
cfs_coffee_configure_log(const char *file, unsigned log_size,
|
|
|
|
unsigned log_record_size)
|
2008-04-28 12:33:14 +02:00
|
|
|
{
|
2008-10-03 21:27:28 +02:00
|
|
|
coffee_page_t page;
|
2008-04-28 12:33:14 +02:00
|
|
|
struct file_header hdr;
|
2008-10-15 15:57:30 +02:00
|
|
|
|
2008-11-05 00:30:47 +01:00
|
|
|
if(log_record_size == 0 || log_record_size > COFFEE_PAGE_SIZE ||
|
|
|
|
log_size < log_record_size) {
|
2008-10-15 15:57:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
page = find_file(file);
|
2008-07-02 13:11:13 +02:00
|
|
|
if(page < 0) {
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-11-04 18:48:36 +01:00
|
|
|
read_header(&hdr, page);
|
2008-11-19 00:10:30 +01:00
|
|
|
if(COFFEE_FILE_MODIFIED(hdr)) {
|
2008-11-03 16:13:13 +01:00
|
|
|
/* Too late to customize the log. */
|
2008-04-28 12:33:14 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-11-05 00:09:55 +01:00
|
|
|
hdr.log_records = log_size / log_record_size;
|
|
|
|
hdr.log_record_size = log_record_size;
|
2008-11-04 18:48:36 +01:00
|
|
|
write_header(&hdr, page);
|
2008-11-03 16:13:13 +01:00
|
|
|
|
2008-04-28 12:33:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2008-07-02 13:11:13 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_coffee_format(void)
|
|
|
|
{
|
|
|
|
int nsectors, i;
|
|
|
|
|
|
|
|
nsectors = COFFEE_SIZE / COFFEE_SECTOR_SIZE;
|
|
|
|
/* Reject format requests when the configuration is incorrect. */
|
2008-08-25 11:57:38 +02:00
|
|
|
if(nsectors < 1 || (COFFEE_START & (COFFEE_SECTOR_SIZE - 1))) {
|
2008-07-02 13:11:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF("Coffee: Formatting %d sectors", nsectors);
|
|
|
|
|
|
|
|
watchdog_stop();
|
2008-08-25 11:57:38 +02:00
|
|
|
for(i = 0; i < nsectors; i++) {
|
2008-07-02 13:11:13 +02:00
|
|
|
COFFEE_ERASE(i);
|
|
|
|
PRINTF(".");
|
|
|
|
}
|
|
|
|
watchdog_start();
|
|
|
|
|
2008-09-21 14:49:08 +02:00
|
|
|
/* All file descriptors have become invalid. */
|
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
|
|
|
coffee_fd_set[i].flags = COFFEE_FD_FREE;
|
|
|
|
}
|
|
|
|
|
2008-07-02 13:11:13 +02:00
|
|
|
PRINTF("done!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|