2008-04-28 10:33:14 +00:00
|
|
|
/*
|
2009-02-09 12:02:10 +00:00
|
|
|
* Copyright (c) 2008, 2009, Swedish Institute of Computer Science
|
2008-04-28 10:33:14 +00:00
|
|
|
* 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
|
2011-02-09 14:03:57 +00:00
|
|
|
* Coffee: A file system for a variety of storage types in
|
|
|
|
* memory-constrained devices.
|
|
|
|
*
|
2015-02-17 10:02:06 +00:00
|
|
|
* For further information, see "Enabling Large-Scale Storage in
|
|
|
|
* Sensor Networks with the Coffee File System" in the proceedings
|
2011-02-09 14:03:57 +00:00
|
|
|
* of ACM/IEEE IPSN 2009.
|
|
|
|
*
|
2008-04-28 10:33:14 +00:00
|
|
|
* \author
|
2015-02-17 10:02:06 +00:00
|
|
|
* Nicolas Tsiftes <nvt@sics.se>
|
2008-04-28 10:33:14 +00:00
|
|
|
*/
|
|
|
|
|
2008-08-25 09:57:38 +00:00
|
|
|
#include <limits.h>
|
2008-04-28 10:33:14 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-06-29 13:33:12 +00:00
|
|
|
#define DEBUG 0
|
2008-04-28 10:33:14 +00:00
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2017-09-04 14:22:34 +00:00
|
|
|
#include "contiki.h"
|
2009-02-17 16:56:32 +00:00
|
|
|
#include "cfs/cfs.h"
|
|
|
|
#include "cfs-coffee-arch.h"
|
|
|
|
#include "cfs/cfs-coffee.h"
|
2010-01-14 21:16:58 +00:00
|
|
|
|
2011-02-09 14:03:57 +00:00
|
|
|
/* Micro logs enable modifications on storage types that do not support
|
|
|
|
in-place updates. This applies primarily to flash memories. */
|
|
|
|
#ifndef COFFEE_MICRO_LOGS
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_MICRO_LOGS 1
|
2011-02-09 14:03:57 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
/* If the files are expected to be appended to only, this parameter
|
2011-02-09 14:03:57 +00:00
|
|
|
can be set to save some code space. */
|
|
|
|
#ifndef COFFEE_APPEND_ONLY
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_APPEND_ONLY 0
|
2010-04-12 13:20:55 +00:00
|
|
|
#endif
|
2011-02-09 14:03:57 +00:00
|
|
|
|
|
|
|
#if COFFEE_MICRO_LOGS && COFFEE_APPEND_ONLY
|
|
|
|
#error "Cannot have COFFEE_APPEND_ONLY set when COFFEE_MICRO_LOGS is set."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent sectors from being erased directly after file removal.
|
|
|
|
* This will level the wear across sectors better, but may lead
|
|
|
|
* to longer garbage collection procedures.
|
|
|
|
*/
|
|
|
|
#ifndef COFFEE_EXTENDED_WEAR_LEVELLING
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_EXTENDED_WEAR_LEVELLING 1
|
2010-04-12 13:20:55 +00:00
|
|
|
#endif
|
|
|
|
|
2009-05-11 16:35:49 +00:00
|
|
|
#if COFFEE_START & (COFFEE_SECTOR_SIZE - 1)
|
|
|
|
#error COFFEE_START must point to the first byte in a sector.
|
2008-08-15 22:38:43 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
/* File descriptor flags. */
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_FD_FREE 0x0
|
|
|
|
#define COFFEE_FD_READ 0x1
|
|
|
|
#define COFFEE_FD_WRITE 0x2
|
|
|
|
#define COFFEE_FD_APPEND 0x4
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
/* File object flags. */
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_FILE_MODIFIED 0x1
|
2008-07-02 11:11:13 +00:00
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
/* Internal Coffee markers. */
|
|
|
|
#define INVALID_PAGE ((coffee_page_t)-1)
|
2015-02-17 10:02:06 +00:00
|
|
|
#define UNKNOWN_OFFSET ((cfs_offset_t)-1)
|
2008-12-09 16:01:18 +00:00
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
/* File removal actions. They can have the same values because
|
|
|
|
they are passed as separate parameters. */
|
|
|
|
#define REMOVE_LOG 1
|
|
|
|
#define CLOSE_FDS 1
|
|
|
|
#define ALLOW_GC 1
|
2010-02-08 14:08:14 +00:00
|
|
|
|
2009-04-02 11:22:23 +00:00
|
|
|
/* "Greedy" garbage collection erases as many sectors as possible. */
|
2016-06-13 13:55:58 +00:00
|
|
|
#define GC_GREEDY 0
|
2009-04-02 11:22:23 +00:00
|
|
|
/* "Reluctant" garbage collection stops after erasing one sector. */
|
2016-06-13 13:55:58 +00:00
|
|
|
#define GC_RELUCTANT 1
|
2009-04-02 11:22:23 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* File descriptor macros. */
|
2016-04-20 12:30:20 +00:00
|
|
|
#define FD_VALID(fd) ((fd) >= 0 && (fd) < COFFEE_FD_SET_SIZE && \
|
|
|
|
coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
|
2015-02-17 10:02:06 +00:00
|
|
|
#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-07-02 11:11:13 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* File object macros. */
|
2016-04-20 12:30:20 +00:00
|
|
|
#define FILE_MODIFIED(file) ((file)->flags & COFFEE_FILE_MODIFIED)
|
|
|
|
#define FILE_FREE(file) ((file)->max_pages == 0)
|
2015-02-17 10:02:06 +00:00
|
|
|
#define FILE_UNREFERENCED(file) ((file)->references == 0)
|
2008-07-02 11:11:13 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
/* File header flags. */
|
2016-04-20 12:30:20 +00:00
|
|
|
#define HDR_FLAG_VALID 0x01 /* Completely written header. */
|
|
|
|
#define HDR_FLAG_ALLOCATED 0x02 /* Allocated file. */
|
|
|
|
#define HDR_FLAG_OBSOLETE 0x04 /* File marked for GC. */
|
|
|
|
#define HDR_FLAG_MODIFIED 0x08 /* Modified file, log exists. */
|
|
|
|
#define HDR_FLAG_LOG 0x10 /* Log file. */
|
|
|
|
#define HDR_FLAG_ISOLATED 0x20 /* Isolated page. */
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* File header macros. */
|
2015-02-17 10:02:06 +00:00
|
|
|
#define CHECK_FLAG(hdr, flag) ((hdr).flags & (flag))
|
2016-04-20 12:30:20 +00:00
|
|
|
#define HDR_VALID(hdr) CHECK_FLAG(hdr, HDR_FLAG_VALID)
|
|
|
|
#define HDR_ALLOCATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ALLOCATED)
|
|
|
|
#define HDR_FREE(hdr) !HDR_ALLOCATED(hdr)
|
|
|
|
#define HDR_LOG(hdr) CHECK_FLAG(hdr, HDR_FLAG_LOG)
|
|
|
|
#define HDR_MODIFIED(hdr) CHECK_FLAG(hdr, HDR_FLAG_MODIFIED)
|
|
|
|
#define HDR_ISOLATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ISOLATED)
|
|
|
|
#define HDR_OBSOLETE(hdr) CHECK_FLAG(hdr, HDR_FLAG_OBSOLETE)
|
|
|
|
#define HDR_ACTIVE(hdr) (HDR_ALLOCATED(hdr) && \
|
|
|
|
!HDR_OBSOLETE(hdr) && \
|
|
|
|
!HDR_ISOLATED(hdr))
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* Shortcuts derived from the hardware-dependent configuration of Coffee. */
|
2016-04-20 12:30:20 +00:00
|
|
|
#define COFFEE_SECTOR_COUNT \
|
|
|
|
(coffee_page_t)(COFFEE_SIZE / COFFEE_SECTOR_SIZE)
|
2015-02-17 10:02:06 +00:00
|
|
|
#define COFFEE_PAGE_COUNT \
|
|
|
|
((coffee_page_t)(COFFEE_SIZE / COFFEE_PAGE_SIZE))
|
|
|
|
#define COFFEE_PAGES_PER_SECTOR \
|
|
|
|
((coffee_page_t)(COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE))
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* This structure is used for garbage collection statistics. */
|
2009-06-10 15:03:35 +00:00
|
|
|
struct sector_status {
|
2009-02-14 19:09:17 +00:00
|
|
|
coffee_page_t active;
|
|
|
|
coffee_page_t obsolete;
|
|
|
|
coffee_page_t free;
|
|
|
|
};
|
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* The structure of cached file objects. */
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file {
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t end;
|
2009-02-14 19:09:17 +00:00
|
|
|
coffee_page_t page;
|
2008-10-03 19:27:28 +00:00
|
|
|
coffee_page_t max_pages;
|
2009-07-16 11:58:44 +00:00
|
|
|
int16_t record_count;
|
2009-02-08 17:08:13 +00:00
|
|
|
uint8_t references;
|
2008-08-15 12:34:56 +00:00
|
|
|
uint8_t flags;
|
2008-07-11 21:39:26 +00:00
|
|
|
};
|
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/* The file descriptor structure. */
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file_desc {
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t offset;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
|
|
|
uint8_t flags;
|
2011-02-09 14:03:57 +00:00
|
|
|
uint8_t io_flags;
|
2008-07-02 11:11:13 +00:00
|
|
|
};
|
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
/* The file header structure mimics the representation of file headers
|
2010-01-19 16:18:34 +00:00
|
|
|
in the physical storage medium. */
|
2008-07-02 11:11:13 +00:00
|
|
|
struct file_header {
|
2008-10-03 19:27:28 +00:00
|
|
|
coffee_page_t log_page;
|
2008-11-04 23:09:55 +00:00
|
|
|
uint16_t log_records;
|
|
|
|
uint16_t log_record_size;
|
2008-10-03 19:27:28 +00:00
|
|
|
coffee_page_t max_pages;
|
2009-05-04 22:30:32 +00:00
|
|
|
uint8_t deprecated_eof_hint;
|
2008-08-15 12:34:56 +00:00
|
|
|
uint8_t flags;
|
2008-07-02 11:11:13 +00:00
|
|
|
char name[COFFEE_NAME_LENGTH];
|
2011-05-25 15:21:51 +00:00
|
|
|
};
|
2008-07-02 11:11:13 +00:00
|
|
|
|
|
|
|
/* This is needed because of a buggy compiler. */
|
|
|
|
struct log_param {
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t offset;
|
2016-05-28 20:47:58 +00:00
|
|
|
char *buf;
|
2008-07-02 11:11:13 +00:00
|
|
|
uint16_t size;
|
|
|
|
};
|
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
2016-04-20 10:47:37 +00:00
|
|
|
* Variables that keep track of opened files and internal
|
|
|
|
* optimization information for Coffee.
|
2010-01-19 16:18:34 +00:00
|
|
|
*/
|
2016-04-20 10:47:37 +00:00
|
|
|
static struct file coffee_files[COFFEE_MAX_OPEN_FILES];
|
|
|
|
static struct file_desc coffee_fd_set[COFFEE_FD_SET_SIZE];
|
|
|
|
static coffee_page_t next_free;
|
|
|
|
static char gc_wait;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-11-04 17:48:36 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
write_header(struct file_header *hdr, coffee_page_t page)
|
|
|
|
{
|
2009-02-14 19:09:17 +00:00
|
|
|
hdr->flags |= HDR_FLAG_VALID;
|
2008-11-04 17:48:36 +00:00
|
|
|
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);
|
2016-06-13 13:55:58 +00:00
|
|
|
if(DEBUG && HDR_ACTIVE(*hdr) && !HDR_VALID(*hdr)) {
|
|
|
|
PRINTF("Coffee: Invalid header at page %u!\n", (unsigned)page);
|
2009-02-14 19:09:17 +00:00
|
|
|
}
|
2008-11-04 17:48:36 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-27 14:25:37 +00:00
|
|
|
static cfs_offset_t
|
|
|
|
absolute_offset(coffee_page_t page, cfs_offset_t offset)
|
2008-12-09 16:01:18 +00:00
|
|
|
{
|
|
|
|
return page * COFFEE_PAGE_SIZE + sizeof(struct file_header) + offset;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-04-29 13:15:59 +00:00
|
|
|
static coffee_page_t
|
2016-04-20 12:30:20 +00:00
|
|
|
get_sector_status(coffee_page_t sector, struct sector_status *stats)
|
2009-04-29 13:15:59 +00:00
|
|
|
{
|
2009-02-17 16:56:32 +00:00
|
|
|
static coffee_page_t skip_pages;
|
2009-02-14 19:09:17 +00:00
|
|
|
static char last_pages_are_active;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct file_header hdr;
|
2009-02-17 16:56:32 +00:00
|
|
|
coffee_page_t active, obsolete, free;
|
|
|
|
coffee_page_t sector_start, sector_end;
|
|
|
|
coffee_page_t page;
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-06-10 15:03:35 +00:00
|
|
|
memset(stats, 0, sizeof(*stats));
|
2009-02-14 19:09:17 +00:00
|
|
|
active = obsolete = free = 0;
|
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
2015-02-17 10:02:06 +00:00
|
|
|
* get_sector_status() is an iterative function using local static
|
|
|
|
* state. It therefore requires that the caller starts iterating from
|
2010-01-19 16:18:34 +00:00
|
|
|
* sector 0 in order to reset the internal state.
|
|
|
|
*/
|
2008-07-02 11:11:13 +00:00
|
|
|
if(sector == 0) {
|
2008-04-28 10:33:14 +00:00
|
|
|
skip_pages = 0;
|
|
|
|
last_pages_are_active = 0;
|
|
|
|
}
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2009-04-29 13:15:59 +00:00
|
|
|
sector_start = sector * COFFEE_PAGES_PER_SECTOR;
|
|
|
|
sector_end = sector_start + COFFEE_PAGES_PER_SECTOR;
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
2015-02-17 10:02:06 +00:00
|
|
|
* Account for pages belonging to a file starting in a previous
|
|
|
|
* segment that extends into this segment. If the whole segment is
|
2010-01-19 16:18:34 +00:00
|
|
|
* covered, we do not need to continue counting pages in this iteration.
|
|
|
|
*/
|
2008-07-02 11:11:13 +00:00
|
|
|
if(last_pages_are_active) {
|
2009-06-10 15:03:35 +00:00
|
|
|
if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
|
|
|
|
stats->active = COFFEE_PAGES_PER_SECTOR;
|
|
|
|
skip_pages -= COFFEE_PAGES_PER_SECTOR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
active = skip_pages;
|
2008-04-28 10:33:14 +00:00
|
|
|
} else {
|
2009-06-10 15:03:35 +00:00
|
|
|
if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
|
|
|
|
stats->obsolete = COFFEE_PAGES_PER_SECTOR;
|
|
|
|
skip_pages -= COFFEE_PAGES_PER_SECTOR;
|
2009-07-09 22:16:22 +00:00
|
|
|
return skip_pages >= COFFEE_PAGES_PER_SECTOR ? 0 : skip_pages;
|
2009-06-10 15:03:35 +00:00
|
|
|
}
|
|
|
|
obsolete = skip_pages;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
/* Determine the amount of pages of each type that have not been
|
2010-01-19 16:18:34 +00:00
|
|
|
accounted for yet in the current sector. */
|
2009-02-17 16:56:32 +00:00
|
|
|
for(page = sector_start + skip_pages; page < sector_end;) {
|
|
|
|
read_header(&hdr, page);
|
2008-11-04 17:48:36 +00:00
|
|
|
last_pages_are_active = 0;
|
2009-02-08 17:08:13 +00:00
|
|
|
if(HDR_ACTIVE(hdr)) {
|
2008-04-28 10:33:14 +00:00
|
|
|
last_pages_are_active = 1;
|
2009-02-17 16:56:32 +00:00
|
|
|
page += hdr.max_pages;
|
|
|
|
active += hdr.max_pages;
|
2009-02-08 17:08:13 +00:00
|
|
|
} else if(HDR_ISOLATED(hdr)) {
|
2009-02-17 16:56:32 +00:00
|
|
|
page++;
|
2009-02-14 19:09:17 +00:00
|
|
|
obsolete++;
|
2009-02-08 17:08:13 +00:00
|
|
|
} else if(HDR_OBSOLETE(hdr)) {
|
2009-02-17 16:56:32 +00:00
|
|
|
page += hdr.max_pages;
|
|
|
|
obsolete += hdr.max_pages;
|
2009-02-14 19:09:17 +00:00
|
|
|
} else {
|
2009-04-29 13:15:59 +00:00
|
|
|
free = sector_end - page;
|
2008-08-15 12:34:56 +00:00
|
|
|
break;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
|
|
|
* Determine the amount of pages in the following sectors that
|
2015-02-17 10:02:06 +00:00
|
|
|
* should be remembered for the next iteration. This is necessary
|
2016-04-20 12:30:20 +00:00
|
|
|
* because no file page except the first contains information
|
2010-01-19 16:18:34 +00:00
|
|
|
* about what type of page it is. A side effect of remembering this
|
2015-02-17 10:02:06 +00:00
|
|
|
* amount is that there is no need to read in the headers of each
|
2010-01-19 16:18:34 +00:00
|
|
|
* of these pages from the storage.
|
|
|
|
*/
|
2009-02-14 19:09:17 +00:00
|
|
|
skip_pages = active + obsolete + free - COFFEE_PAGES_PER_SECTOR;
|
2008-07-02 11:11:13 +00:00
|
|
|
if(skip_pages > 0) {
|
|
|
|
if(last_pages_are_active) {
|
2009-02-14 19:09:17 +00:00
|
|
|
active = COFFEE_PAGES_PER_SECTOR - obsolete;
|
2008-04-28 10:33:14 +00:00
|
|
|
} else {
|
2009-02-14 19:09:17 +00:00
|
|
|
obsolete = COFFEE_PAGES_PER_SECTOR - active;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats->active = active;
|
|
|
|
stats->obsolete = obsolete;
|
|
|
|
stats->free = free;
|
2009-04-29 13:15:59 +00:00
|
|
|
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
2015-02-17 10:02:06 +00:00
|
|
|
* To avoid unnecessary page isolation, we notify the caller that
|
|
|
|
* "skip_pages" pages should be isolated only if the current file extent
|
|
|
|
* ends in the next sector. If the file extent ends in a more distant
|
|
|
|
* sector, however, the garbage collection can free the next sector
|
|
|
|
* immediately without requiring page isolation.
|
2010-01-19 16:18:34 +00:00
|
|
|
*/
|
2009-07-09 22:16:22 +00:00
|
|
|
return (last_pages_are_active || (skip_pages >= COFFEE_PAGES_PER_SECTOR)) ?
|
2015-02-17 10:02:06 +00:00
|
|
|
0 : skip_pages;
|
2009-04-29 13:15:59 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
isolate_pages(coffee_page_t start, coffee_page_t skip_pages)
|
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
coffee_page_t page;
|
|
|
|
|
|
|
|
/* Split an obsolete file starting in the previous sector and mark
|
|
|
|
the following pages as isolated. */
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
hdr.flags = HDR_FLAG_ALLOCATED | HDR_FLAG_ISOLATED;
|
|
|
|
|
|
|
|
/* Isolation starts from the next sector. */
|
|
|
|
for(page = 0; page < skip_pages; page++) {
|
|
|
|
write_header(&hdr, start + page);
|
|
|
|
}
|
|
|
|
PRINTF("Coffee: Isolated %u pages starting in sector %d\n",
|
|
|
|
(unsigned)skip_pages, (int)start / COFFEE_PAGES_PER_SECTOR);
|
2009-02-14 19:09:17 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2009-04-02 11:22:23 +00:00
|
|
|
collect_garbage(int mode)
|
2009-02-14 19:09:17 +00:00
|
|
|
{
|
2016-04-20 12:30:20 +00:00
|
|
|
coffee_page_t sector;
|
2009-06-10 15:03:35 +00:00
|
|
|
struct sector_status stats;
|
|
|
|
coffee_page_t first_page, isolation_count;
|
2009-02-14 19:09:17 +00:00
|
|
|
|
2016-04-20 12:30:20 +00:00
|
|
|
PRINTF("Coffee: Running the garbage collector in %s mode\n",
|
2015-02-17 10:02:06 +00:00
|
|
|
mode == GC_RELUCTANT ? "reluctant" : "greedy");
|
2009-02-14 19:09:17 +00:00
|
|
|
/*
|
|
|
|
* The garbage collector erases as many sectors as possible. A sector is
|
|
|
|
* erasable if there are only free or obsolete pages in it.
|
|
|
|
*/
|
|
|
|
for(sector = 0; sector < COFFEE_SECTOR_COUNT; sector++) {
|
2009-06-10 15:03:35 +00:00
|
|
|
isolation_count = get_sector_status(sector, &stats);
|
|
|
|
PRINTF("Coffee: Sector %u has %u active, %u obsolete, and %u free pages.\n",
|
2016-04-20 12:30:20 +00:00
|
|
|
(unsigned)sector, (unsigned)stats.active,
|
2015-02-17 10:02:06 +00:00
|
|
|
(unsigned)stats.obsolete, (unsigned)stats.free);
|
2009-04-29 13:15:59 +00:00
|
|
|
|
2009-04-13 22:26:31 +00:00
|
|
|
if(stats.active > 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-29 13:15:59 +00:00
|
|
|
|
2009-04-13 22:26:31 +00:00
|
|
|
if((mode == GC_RELUCTANT && stats.free == 0) ||
|
2009-04-29 13:15:59 +00:00
|
|
|
(mode == GC_GREEDY && stats.obsolete > 0)) {
|
2009-02-22 15:19:31 +00:00
|
|
|
first_page = sector * COFFEE_PAGES_PER_SECTOR;
|
2016-04-20 10:47:37 +00:00
|
|
|
if(first_page < next_free) {
|
|
|
|
next_free = first_page;
|
2009-02-22 15:19:31 +00:00
|
|
|
}
|
2009-04-29 13:15:59 +00:00
|
|
|
|
2009-06-10 15:03:35 +00:00
|
|
|
if(isolation_count > 0) {
|
|
|
|
isolate_pages(first_page + COFFEE_PAGES_PER_SECTOR, isolation_count);
|
2009-04-29 13:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
COFFEE_ERASE(sector);
|
|
|
|
PRINTF("Coffee: Erased sector %d!\n", sector);
|
|
|
|
|
2009-07-09 22:16:22 +00:00
|
|
|
if(mode == GC_RELUCTANT && isolation_count > 0) {
|
2009-04-13 22:26:31 +00:00
|
|
|
break;
|
2009-04-02 11:22:23 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-19 21:35:38 +00:00
|
|
|
static coffee_page_t
|
|
|
|
next_file(coffee_page_t page, struct file_header *hdr)
|
|
|
|
{
|
2010-01-19 16:18:34 +00:00
|
|
|
/*
|
2015-02-17 10:02:06 +00:00
|
|
|
* The quick-skip algorithm for finding file extents is the most
|
2016-04-20 12:30:20 +00:00
|
|
|
* essential part of Coffee. The file allocation rules enable this
|
2015-02-17 10:02:06 +00:00
|
|
|
* algorithm to quickly jump over free areas and allocated extents
|
2010-01-19 16:18:34 +00:00
|
|
|
* after reading single headers and determining their status.
|
|
|
|
*
|
2015-02-17 10:02:06 +00:00
|
|
|
* The worst-case performance occurs when we encounter multiple long
|
|
|
|
* sequences of isolated pages, but such sequences are uncommon and
|
2010-01-19 16:18:34 +00:00
|
|
|
* always shorter than a sector.
|
|
|
|
*/
|
2009-02-19 21:35:38 +00:00
|
|
|
if(HDR_FREE(*hdr)) {
|
|
|
|
return (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
|
|
|
|
} else if(HDR_ISOLATED(*hdr)) {
|
|
|
|
return page + 1;
|
|
|
|
}
|
2015-02-17 10:02:06 +00:00
|
|
|
return page + hdr->max_pages;
|
2009-02-19 21:35:38 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-08 17:08:13 +00:00
|
|
|
static struct file *
|
2009-05-11 16:35:49 +00:00
|
|
|
load_file(coffee_page_t start, struct file_header *hdr)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2009-02-08 17:08:13 +00:00
|
|
|
int i, unreferenced, free;
|
|
|
|
struct file *file;
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-02-17 16:56:32 +00:00
|
|
|
/*
|
2009-02-17 18:50:29 +00:00
|
|
|
* We prefer to overwrite a free slot since unreferenced ones
|
2009-02-17 16:56:32 +00:00
|
|
|
* contain usable data. Free slots are designated by the page
|
|
|
|
* value INVALID_PAGE.
|
|
|
|
*/
|
2009-02-08 17:08:13 +00:00
|
|
|
for(i = 0, unreferenced = free = -1; i < COFFEE_MAX_OPEN_FILES; i++) {
|
2009-05-13 13:27:19 +00:00
|
|
|
if(FILE_FREE(&coffee_files[i])) {
|
2009-02-08 17:08:13 +00:00
|
|
|
free = i;
|
2008-04-28 10:33:14 +00:00
|
|
|
break;
|
2009-05-13 13:27:19 +00:00
|
|
|
} else if(FILE_UNREFERENCED(&coffee_files[i])) {
|
2009-02-08 17:08:13 +00:00
|
|
|
unreferenced = i;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-05-11 16:35:49 +00:00
|
|
|
if(free == -1) {
|
|
|
|
if(unreferenced != -1) {
|
|
|
|
i = unreferenced;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2008-11-17 09:09:17 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
file = &coffee_files[i];
|
2009-02-14 19:09:17 +00:00
|
|
|
file->page = start;
|
2009-02-08 17:08:13 +00:00
|
|
|
file->end = UNKNOWN_OFFSET;
|
|
|
|
file->max_pages = hdr->max_pages;
|
2016-06-13 15:00:20 +00:00
|
|
|
file->flags = HDR_MODIFIED(*hdr) ? COFFEE_FILE_MODIFIED : 0;
|
2009-07-16 11:58:44 +00:00
|
|
|
/* We don't know the amount of records yet. */
|
|
|
|
file->record_count = -1;
|
2009-02-08 17:08:13 +00:00
|
|
|
|
|
|
|
return file;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-08 17:08:13 +00:00
|
|
|
static struct file *
|
2008-04-28 10:33:14 +00:00
|
|
|
find_file(const char *name)
|
|
|
|
{
|
2009-02-08 17:08:13 +00:00
|
|
|
int i;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct file_header hdr;
|
2008-11-10 23:07:53 +00:00
|
|
|
coffee_page_t page;
|
2015-02-17 10:02:06 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
/* First check if the file metadata is cached. */
|
|
|
|
for(i = 0; i < COFFEE_MAX_OPEN_FILES; i++) {
|
2009-05-13 13:27:19 +00:00
|
|
|
if(FILE_FREE(&coffee_files[i])) {
|
2009-02-08 17:08:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-12 11:24:22 +00:00
|
|
|
|
2009-02-14 19:09:17 +00:00
|
|
|
read_header(&hdr, coffee_files[i].page);
|
2009-05-11 16:35:49 +00:00
|
|
|
if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
|
2009-02-08 17:08:13 +00:00
|
|
|
return &coffee_files[i];
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2015-02-17 10:02:06 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
/* Scan the flash memory sequentially otherwise. */
|
2009-02-19 21:35:38 +00:00
|
|
|
for(page = 0; page < COFFEE_PAGE_COUNT; page = next_file(page, &hdr)) {
|
2008-11-04 17:48:36 +00:00
|
|
|
read_header(&hdr, page);
|
2009-05-11 16:35:49 +00:00
|
|
|
if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
|
|
|
|
return load_file(page, &hdr);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-02-19 21:35:38 +00:00
|
|
|
}
|
2008-11-17 09:09:17 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
return NULL;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-27 14:25:37 +00:00
|
|
|
static cfs_offset_t
|
2009-02-14 19:09:17 +00:00
|
|
|
file_end(coffee_page_t start)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
|
|
|
unsigned char buf[COFFEE_PAGE_SIZE];
|
2008-12-09 16:01:18 +00:00
|
|
|
coffee_page_t page;
|
2008-04-28 10:33:14 +00:00
|
|
|
int i;
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2009-02-14 19:09:17 +00:00
|
|
|
read_header(&hdr, start);
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2008-08-15 12:34:56 +00:00
|
|
|
/*
|
2008-08-25 09:57:38 +00:00
|
|
|
* Move from the end of the range towards the beginning and look for
|
2008-04-28 10:33:14 +00:00
|
|
|
* a byte that has been modified.
|
|
|
|
*
|
2008-08-15 12:34:56 +00:00
|
|
|
* An important implication of this is that if the last written bytes
|
2008-04-28 10:33:14 +00:00
|
|
|
* are zeroes, then these are skipped from the calculation.
|
2008-08-15 12:34:56 +00:00
|
|
|
*/
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-05-26 14:27:06 +00:00
|
|
|
for(page = hdr.max_pages - 1; page >= 0; page--) {
|
|
|
|
COFFEE_READ(buf, sizeof(buf), (start + page) * COFFEE_PAGE_SIZE);
|
2008-07-02 11:11:13 +00:00
|
|
|
for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
|
|
|
|
if(buf[i] != 0) {
|
2015-02-17 10:02:06 +00:00
|
|
|
if(page == 0 && i < sizeof(hdr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1 + i + (page * COFFEE_PAGE_SIZE) - sizeof(hdr);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-03 20:21:43 +00:00
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
/* All bytes are writable. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-12-09 16:01:18 +00:00
|
|
|
static coffee_page_t
|
2009-02-12 11:24:22 +00:00
|
|
|
find_contiguous_pages(coffee_page_t amount)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2008-12-09 16:01:18 +00:00
|
|
|
coffee_page_t page, start;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct file_header hdr;
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-02-22 15:19:31 +00:00
|
|
|
start = INVALID_PAGE;
|
2016-04-20 10:47:37 +00:00
|
|
|
for(page = next_free; page < COFFEE_PAGE_COUNT;) {
|
2008-11-04 17:48:36 +00:00
|
|
|
read_header(&hdr, page);
|
2009-02-08 17:08:13 +00:00
|
|
|
if(HDR_FREE(hdr)) {
|
2009-02-22 15:19:31 +00:00
|
|
|
if(start == INVALID_PAGE) {
|
2015-02-17 10:02:06 +00:00
|
|
|
start = page;
|
2011-01-25 18:39:09 +00:00
|
|
|
if(start + amount >= COFFEE_PAGE_COUNT) {
|
|
|
|
/* We can stop immediately if the remaining pages are not enough. */
|
|
|
|
break;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-02-14 19:09:17 +00:00
|
|
|
|
2009-04-02 14:20:04 +00:00
|
|
|
/* All remaining pages in this sector are free --
|
|
|
|
jump to the next sector. */
|
2009-02-19 21:35:38 +00:00
|
|
|
page = next_file(page, &hdr);
|
2009-02-14 19:09:17 +00:00
|
|
|
|
|
|
|
if(start + amount <= page) {
|
2016-04-20 10:47:37 +00:00
|
|
|
if(start == next_free) {
|
|
|
|
next_free = start + amount;
|
2015-02-17 10:02:06 +00:00
|
|
|
}
|
|
|
|
return start;
|
2009-02-14 19:09:17 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
} else {
|
2009-02-22 15:19:31 +00:00
|
|
|
start = INVALID_PAGE;
|
2009-02-19 21:35:38 +00:00
|
|
|
page = next_file(page, &hdr);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-22 15:19:31 +00:00
|
|
|
return INVALID_PAGE;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2016-06-13 13:55:58 +00:00
|
|
|
remove_by_page(coffee_page_t page, int remove_log,
|
|
|
|
int close_fds, int gc_allowed)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2009-03-19 20:44:43 +00:00
|
|
|
int i;
|
2008-08-15 22:38:43 +00:00
|
|
|
|
2008-11-04 17:48:36 +00:00
|
|
|
read_header(&hdr, page);
|
2009-02-08 17:08:13 +00:00
|
|
|
if(!HDR_ACTIVE(hdr)) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
if(remove_log && HDR_MODIFIED(hdr)) {
|
2010-02-08 14:08:14 +00:00
|
|
|
if(remove_by_page(hdr.log_page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
|
2008-12-18 15:49:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
hdr.flags |= HDR_FLAG_OBSOLETE;
|
2008-12-18 15:49:43 +00:00
|
|
|
write_header(&hdr, page);
|
|
|
|
|
2016-04-20 10:47:37 +00:00
|
|
|
gc_wait = 0;
|
2009-03-26 12:36:10 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
/* Close all file descriptors that reference the removed file. */
|
2008-07-02 11:11:13 +00:00
|
|
|
if(close_fds) {
|
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2009-02-25 23:59:26 +00:00
|
|
|
if(coffee_fd_set[i].file != NULL && coffee_fd_set[i].file->page == page) {
|
2015-02-17 10:02:06 +00:00
|
|
|
coffee_fd_set[i].flags = COFFEE_FD_FREE;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-19 20:44:43 +00:00
|
|
|
}
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2009-03-19 20:44:43 +00:00
|
|
|
for(i = 0; i < COFFEE_MAX_OPEN_FILES; i++) {
|
|
|
|
if(coffee_files[i].page == page) {
|
|
|
|
coffee_files[i].page = INVALID_PAGE;
|
|
|
|
coffee_files[i].references = 0;
|
2009-07-09 22:16:22 +00:00
|
|
|
coffee_files[i].max_pages = 0;
|
2009-02-08 17:08:13 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
if(!COFFEE_EXTENDED_WEAR_LEVELLING && gc_allowed) {
|
2009-04-02 11:22:23 +00:00
|
|
|
collect_garbage(GC_RELUCTANT);
|
|
|
|
}
|
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-12-09 16:01:18 +00:00
|
|
|
static coffee_page_t
|
2009-02-27 14:25:37 +00:00
|
|
|
page_count(cfs_offset_t size)
|
2008-12-09 16:01:18 +00:00
|
|
|
{
|
|
|
|
return (size + sizeof(struct file_header) + COFFEE_PAGE_SIZE - 1) /
|
2015-02-17 10:02:06 +00:00
|
|
|
COFFEE_PAGE_SIZE;
|
2008-12-09 16:01:18 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-08 17:08:13 +00:00
|
|
|
static struct file *
|
2009-05-13 13:27:19 +00:00
|
|
|
reserve(const char *name, coffee_page_t pages,
|
2015-02-17 10:02:06 +00:00
|
|
|
int allow_duplicates, unsigned flags)
|
2008-10-29 15:36:21 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-12-09 16:01:18 +00:00
|
|
|
coffee_page_t page;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
2008-10-29 15:36:21 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
if(!allow_duplicates && find_file(name) != NULL) {
|
|
|
|
return NULL;
|
2008-10-29 15:36:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-09 16:01:18 +00:00
|
|
|
page = find_contiguous_pages(pages);
|
|
|
|
if(page == INVALID_PAGE) {
|
2016-04-20 10:47:37 +00:00
|
|
|
if(gc_wait) {
|
2009-03-26 12:36:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-02 11:22:23 +00:00
|
|
|
collect_garbage(GC_GREEDY);
|
2008-12-09 16:01:18 +00:00
|
|
|
page = find_contiguous_pages(pages);
|
|
|
|
if(page == INVALID_PAGE) {
|
2016-04-20 10:47:37 +00:00
|
|
|
gc_wait = 1;
|
2009-02-08 17:08:13 +00:00
|
|
|
return NULL;
|
2008-10-29 15:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-18 15:49:43 +00:00
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
2014-06-10 19:32:29 +00:00
|
|
|
strncpy(hdr.name, name, sizeof(hdr.name) - 1);
|
2008-12-09 16:01:18 +00:00
|
|
|
hdr.max_pages = pages;
|
2009-05-13 13:27:19 +00:00
|
|
|
hdr.flags = HDR_FLAG_ALLOCATED | flags;
|
2008-11-04 17:48:36 +00:00
|
|
|
write_header(&hdr, page);
|
2008-10-29 15:36:21 +00:00
|
|
|
|
|
|
|
PRINTF("Coffee: Reserved %u pages starting from %u for file %s\n",
|
2016-04-20 12:30:20 +00:00
|
|
|
(unsigned)pages, (unsigned)page, name);
|
2009-02-12 11:24:22 +00:00
|
|
|
|
2009-05-11 16:35:49 +00:00
|
|
|
file = load_file(page, &hdr);
|
2010-10-01 20:14:14 +00:00
|
|
|
if(file != NULL) {
|
|
|
|
file->end = 0;
|
|
|
|
}
|
2008-11-04 17:23:59 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
return file;
|
2008-10-29 15:36:21 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-11-04 23:09:55 +00:00
|
|
|
static void
|
|
|
|
adjust_log_config(struct file_header *hdr,
|
2015-02-17 10:02:06 +00:00
|
|
|
uint16_t *log_record_size, uint16_t *log_records)
|
2008-11-04 23:09:55 +00:00
|
|
|
{
|
|
|
|
*log_record_size = hdr->log_record_size == 0 ?
|
2015-02-17 10:02:06 +00:00
|
|
|
COFFEE_PAGE_SIZE : hdr->log_record_size;
|
2008-11-04 23:09:55 +00:00
|
|
|
*log_records = hdr->log_records == 0 ?
|
2015-02-17 10:02:06 +00:00
|
|
|
COFFEE_LOG_SIZE / *log_record_size : hdr->log_records;
|
2008-11-04 23:09:55 +00:00
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-11-04 23:09:55 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-11-04 23:30:47 +00:00
|
|
|
static uint16_t
|
|
|
|
modify_log_buffer(uint16_t log_record_size,
|
2015-02-17 10:02:06 +00:00
|
|
|
cfs_offset_t *offset, uint16_t *size)
|
2008-11-04 23:30:47 +00:00
|
|
|
{
|
|
|
|
uint16_t region;
|
|
|
|
|
|
|
|
region = *offset / log_record_size;
|
|
|
|
*offset %= log_record_size;
|
2009-07-16 11:58:44 +00:00
|
|
|
|
2008-11-04 23:30:47 +00:00
|
|
|
if(*size > log_record_size - *offset) {
|
|
|
|
*size = log_record_size - *offset;
|
|
|
|
}
|
2009-07-16 11:58:44 +00:00
|
|
|
|
2008-11-04 23:30:47 +00:00
|
|
|
return region;
|
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-11-04 23:30:47 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-10-29 15:36:21 +00:00
|
|
|
static int
|
2009-01-23 16:27:49 +00:00
|
|
|
get_record_index(coffee_page_t log_page, uint16_t search_records,
|
2015-02-17 10:02:06 +00:00
|
|
|
uint16_t region)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t base;
|
2008-11-04 23:09:55 +00:00
|
|
|
uint16_t processed;
|
2008-11-17 10:34:57 +00:00
|
|
|
uint16_t batch_size;
|
|
|
|
int16_t match_index, i;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-12-09 16:01:18 +00:00
|
|
|
base = absolute_offset(log_page, sizeof(uint16_t) * search_records);
|
2009-05-11 16:35:49 +00:00
|
|
|
batch_size = search_records > COFFEE_LOG_TABLE_LIMIT ?
|
2015-02-17 10:02:06 +00:00
|
|
|
COFFEE_LOG_TABLE_LIMIT : search_records;
|
2008-11-04 23:09:55 +00:00
|
|
|
processed = 0;
|
|
|
|
match_index = -1;
|
2008-11-17 14:37:23 +00:00
|
|
|
|
|
|
|
{
|
2015-02-17 10:02:06 +00:00
|
|
|
uint16_t indices[batch_size];
|
2008-11-17 14:37:23 +00:00
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
while(processed < search_records && match_index < 0) {
|
|
|
|
if(batch_size + processed > search_records) {
|
|
|
|
batch_size = search_records - processed;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
base -= batch_size * sizeof(indices[0]);
|
|
|
|
COFFEE_READ(&indices, sizeof(indices[0]) * batch_size, base);
|
2008-08-28 12:50:09 +00:00
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
for(i = batch_size - 1; i >= 0; i--) {
|
|
|
|
if(indices[i] - 1 == region) {
|
|
|
|
match_index = search_records - processed - (batch_size - i);
|
|
|
|
break;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2008-08-28 12:50:09 +00:00
|
|
|
|
2015-02-17 10:02:06 +00:00
|
|
|
processed += batch_size;
|
|
|
|
}
|
2008-11-17 14:37:23 +00:00
|
|
|
}
|
2008-11-18 23:10:30 +00:00
|
|
|
|
2008-11-04 23:09:55 +00:00
|
|
|
return match_index;
|
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-11-04 23:09:55 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-11-04 23:09:55 +00:00
|
|
|
static int
|
2009-07-16 11:58:44 +00:00
|
|
|
read_log_page(struct file_header *hdr, int16_t record_count,
|
|
|
|
struct log_param *lp)
|
2008-11-04 23:09:55 +00:00
|
|
|
{
|
|
|
|
uint16_t region;
|
|
|
|
int16_t match_index;
|
|
|
|
uint16_t log_record_size;
|
|
|
|
uint16_t log_records;
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t base;
|
2008-11-04 23:09:55 +00:00
|
|
|
uint16_t search_records;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-11-04 23:09:55 +00:00
|
|
|
adjust_log_config(hdr, &log_record_size, &log_records);
|
2008-11-04 23:30:47 +00:00
|
|
|
region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
|
2008-11-04 23:09:55 +00:00
|
|
|
|
2009-07-16 11:58:44 +00:00
|
|
|
search_records = record_count < 0 ? log_records : record_count;
|
2008-11-17 14:37:23 +00:00
|
|
|
match_index = get_record_index(hdr->log_page, search_records, region);
|
2008-11-04 23:09:55 +00:00
|
|
|
if(match_index < 0) {
|
|
|
|
return -1;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
2009-02-19 21:35:38 +00:00
|
|
|
base = absolute_offset(hdr->log_page, log_records * sizeof(region));
|
2009-02-27 14:25:37 +00:00
|
|
|
base += (cfs_offset_t)match_index * log_record_size;
|
2008-11-04 23:09:55 +00:00
|
|
|
base += lp->offset;
|
|
|
|
COFFEE_READ(lp->buf, lp->size, base);
|
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
return lp->size;
|
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-04-28 10:33:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-10-03 19:27:28 +00:00
|
|
|
static coffee_page_t
|
2009-02-08 17:08:13 +00:00
|
|
|
create_log(struct file *file, struct file_header *hdr)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2008-11-04 23:09:55 +00:00
|
|
|
uint16_t log_record_size, log_records;
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t size;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *log_file;
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2009-01-23 16:27:49 +00:00
|
|
|
adjust_log_config(hdr, &log_record_size, &log_records);
|
2008-11-04 23:09:55 +00:00
|
|
|
|
2009-06-10 15:03:35 +00:00
|
|
|
/* Log index size + log data size. */
|
|
|
|
size = log_records * (sizeof(uint16_t) + log_record_size);
|
2008-08-28 12:50:09 +00:00
|
|
|
|
2009-05-13 13:27:19 +00:00
|
|
|
log_file = reserve(hdr->name, page_count(size), 1, HDR_FLAG_LOG);
|
2009-05-11 16:35:49 +00:00
|
|
|
if(log_file == NULL) {
|
2009-02-08 17:08:13 +00:00
|
|
|
return INVALID_PAGE;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2008-11-03 15:13:13 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
hdr->flags |= HDR_FLAG_MODIFIED;
|
2009-07-16 11:58:44 +00:00
|
|
|
hdr->log_page = log_file->page;
|
2009-02-14 19:09:17 +00:00
|
|
|
write_header(hdr, file->page);
|
2009-02-08 17:08:13 +00:00
|
|
|
|
|
|
|
file->flags |= COFFEE_FILE_MODIFIED;
|
2009-07-16 11:58:44 +00:00
|
|
|
return log_file->page;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-04-28 10:33:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2008-11-17 09:09:17 +00:00
|
|
|
merge_log(coffee_page_t file_page, int extend)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr, hdr2;
|
|
|
|
int fd, n;
|
2009-02-27 14:25:37 +00:00
|
|
|
cfs_offset_t offset;
|
2008-11-17 09:09:17 +00:00
|
|
|
coffee_page_t max_pages;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *new_file;
|
|
|
|
int i;
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2008-11-04 17:48:36 +00:00
|
|
|
read_header(&hdr, file_page);
|
2008-10-28 14:47:50 +00:00
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
fd = cfs_open(hdr.name, CFS_READ);
|
2008-07-02 11:11:13 +00:00
|
|
|
if(fd < 0) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-08-25 09:57:38 +00:00
|
|
|
|
2008-09-21 12:49:08 +00:00
|
|
|
/*
|
|
|
|
* The reservation function adds extra space for the header, which has
|
2012-10-30 00:56:23 +00:00
|
|
|
* already been accounted for in the previous reservation.
|
2008-09-21 12:49:08 +00:00
|
|
|
*/
|
2009-02-08 17:08:13 +00:00
|
|
|
max_pages = hdr.max_pages << extend;
|
2009-05-13 13:27:19 +00:00
|
|
|
new_file = reserve(hdr.name, max_pages, 1, 0);
|
2009-02-08 17:08:13 +00:00
|
|
|
if(new_file == NULL) {
|
2008-09-21 16:07:26 +00:00
|
|
|
cfs_close(fd);
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
do {
|
2008-11-04 23:09:55 +00:00
|
|
|
char buf[hdr.log_record_size == 0 ? COFFEE_PAGE_SIZE : hdr.log_record_size];
|
2008-08-25 09:57:38 +00:00
|
|
|
n = cfs_read(fd, buf, sizeof(buf));
|
2008-07-02 11:11:13 +00:00
|
|
|
if(n < 0) {
|
2010-02-08 14:08:14 +00:00
|
|
|
remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, ALLOW_GC);
|
2008-04-28 10:33:14 +00:00
|
|
|
cfs_close(fd);
|
|
|
|
return -1;
|
2008-07-02 11:11:13 +00:00
|
|
|
} else if(n > 0) {
|
2009-07-16 11:58:44 +00:00
|
|
|
COFFEE_WRITE(buf, n, absolute_offset(new_file->page, offset));
|
2008-04-28 10:33:14 +00:00
|
|
|
offset += n;
|
|
|
|
}
|
2008-07-02 11:11:13 +00:00
|
|
|
} while(n != 0);
|
2008-11-18 23:10:30 +00:00
|
|
|
|
2009-03-19 20:44:43 +00:00
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2015-02-17 10:02:06 +00:00
|
|
|
if(coffee_fd_set[i].flags != COFFEE_FD_FREE &&
|
2009-03-19 20:44:43 +00:00
|
|
|
coffee_fd_set[i].file->page == file_page) {
|
|
|
|
coffee_fd_set[i].file = new_file;
|
|
|
|
new_file->references++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 14:08:14 +00:00
|
|
|
if(remove_by_page(file_page, REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
|
|
|
|
remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC);
|
2008-04-28 10:33:14 +00:00
|
|
|
cfs_close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-10-28 14:47:50 +00:00
|
|
|
|
2016-04-20 12:30:20 +00:00
|
|
|
/* Copy the log configuration. */
|
2009-02-14 19:09:17 +00:00
|
|
|
read_header(&hdr2, new_file->page);
|
2008-11-04 23:09:55 +00:00
|
|
|
hdr2.log_record_size = hdr.log_record_size;
|
|
|
|
hdr2.log_records = hdr.log_records;
|
2009-02-14 19:09:17 +00:00
|
|
|
write_header(&hdr2, new_file->page);
|
|
|
|
|
2009-05-11 16:35:49 +00:00
|
|
|
new_file->flags &= ~COFFEE_FILE_MODIFIED;
|
2009-03-26 15:57:52 +00:00
|
|
|
new_file->end = offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
|
|
|
cfs_close(fd);
|
2008-10-28 14:47:50 +00:00
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-04-28 10:33:14 +00:00
|
|
|
static int
|
2009-02-08 17:08:13 +00:00
|
|
|
find_next_record(struct file *file, coffee_page_t log_page,
|
2015-02-17 10:02:06 +00:00
|
|
|
int log_records)
|
2008-11-17 14:37:23 +00:00
|
|
|
{
|
2009-05-11 16:35:49 +00:00
|
|
|
int log_record, preferred_batch_size;
|
2008-11-17 14:37:23 +00:00
|
|
|
|
2009-07-16 11:58:44 +00:00
|
|
|
if(file->record_count >= 0) {
|
|
|
|
return file->record_count;
|
2009-06-10 15:03:35 +00:00
|
|
|
}
|
|
|
|
|
2008-11-17 14:37:23 +00:00
|
|
|
preferred_batch_size = log_records > COFFEE_LOG_TABLE_LIMIT ?
|
2015-02-17 10:02:06 +00:00
|
|
|
COFFEE_LOG_TABLE_LIMIT : log_records;
|
2009-06-10 15:03:35 +00:00
|
|
|
{
|
2010-05-13 18:24:55 +00:00
|
|
|
/* The next log record is unknown at this point; search for it. */
|
2008-11-17 14:37:23 +00:00
|
|
|
uint16_t indices[preferred_batch_size];
|
|
|
|
uint16_t processed;
|
|
|
|
uint16_t batch_size;
|
|
|
|
|
|
|
|
log_record = log_records;
|
2008-11-24 10:56:55 +00:00
|
|
|
for(processed = 0; processed < log_records; processed += batch_size) {
|
2008-11-17 14:37:23 +00:00
|
|
|
batch_size = log_records - processed >= preferred_batch_size ?
|
2015-02-17 10:02:06 +00:00
|
|
|
preferred_batch_size : log_records - processed;
|
2008-11-24 10:56:55 +00:00
|
|
|
|
2008-11-17 14:37:23 +00:00
|
|
|
COFFEE_READ(&indices, batch_size * sizeof(indices[0]),
|
2015-02-17 10:02:06 +00:00
|
|
|
absolute_offset(log_page, processed * sizeof(indices[0])));
|
2009-05-11 16:35:49 +00:00
|
|
|
for(log_record = 0; log_record < batch_size; log_record++) {
|
2015-02-17 10:02:06 +00:00
|
|
|
if(indices[log_record] == 0) {
|
|
|
|
log_record += processed;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-17 14:37:23 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
}
|
2008-11-17 14:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return log_record;
|
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-11-17 14:37:23 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-11-17 14:37:23 +00:00
|
|
|
static int
|
2009-02-08 17:08:13 +00:00
|
|
|
write_log_page(struct file *file, struct log_param *lp)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-11-04 23:30:47 +00:00
|
|
|
uint16_t region;
|
2008-10-03 19:27:28 +00:00
|
|
|
coffee_page_t log_page;
|
2008-11-04 23:09:55 +00:00
|
|
|
int16_t log_record;
|
|
|
|
uint16_t log_record_size;
|
|
|
|
uint16_t log_records;
|
2009-05-13 13:27:19 +00:00
|
|
|
cfs_offset_t offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct log_param lp_out;
|
|
|
|
|
2009-02-14 19:09:17 +00:00
|
|
|
read_header(&hdr, file->page);
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-11-04 23:30:47 +00:00
|
|
|
adjust_log_config(&hdr, &log_record_size, &log_records);
|
|
|
|
region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
|
2008-04-28 10:33:14 +00:00
|
|
|
|
|
|
|
log_page = 0;
|
2009-02-08 17:08:13 +00:00
|
|
|
if(HDR_MODIFIED(hdr)) {
|
2008-04-28 10:33:14 +00:00
|
|
|
/* A log structure has already been created. */
|
|
|
|
log_page = hdr.log_page;
|
2009-02-08 17:08:13 +00:00
|
|
|
log_record = find_next_record(file, log_page, log_records);
|
2008-11-04 23:09:55 +00:00
|
|
|
if(log_record >= log_records) {
|
2008-11-03 15:13:13 +00:00
|
|
|
/* The log is full; merge the log. */
|
|
|
|
PRINTF("Coffee: Merging the file %s with its log\n", hdr.name);
|
2009-02-14 19:09:17 +00:00
|
|
|
return merge_log(file->page, 0);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Create a log structure. */
|
2009-02-08 17:08:13 +00:00
|
|
|
log_page = create_log(file, &hdr);
|
2008-12-09 16:01:18 +00:00
|
|
|
if(log_page == INVALID_PAGE) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-07-02 11:11:13 +00:00
|
|
|
PRINTF("Coffee: Created a log structure for file %s at page %u\n",
|
2015-02-17 10:02:06 +00:00
|
|
|
hdr.name, (unsigned)log_page);
|
2008-04-28 10:33:14 +00:00
|
|
|
hdr.log_page = log_page;
|
2008-11-04 23:09:55 +00:00
|
|
|
log_record = 0;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2010-11-11 13:08:23 +00:00
|
|
|
char copy_buf[log_record_size];
|
2008-10-16 12:54:36 +00:00
|
|
|
|
2009-05-13 13:27:19 +00:00
|
|
|
lp_out.offset = offset = region * log_record_size;
|
2008-04-28 10:33:14 +00:00
|
|
|
lp_out.buf = copy_buf;
|
2008-11-04 23:09:55 +00:00
|
|
|
lp_out.size = log_record_size;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-11-04 23:09:55 +00:00
|
|
|
if((lp->offset > 0 || lp->size != log_record_size) &&
|
2015-02-17 10:02:06 +00:00
|
|
|
read_log_page(&hdr, log_record, &lp_out) < 0) {
|
2008-08-25 09:57:38 +00:00
|
|
|
COFFEE_READ(copy_buf, sizeof(copy_buf),
|
2015-02-17 10:02:06 +00:00
|
|
|
absolute_offset(file->page, offset));
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2010-11-11 13:08:23 +00:00
|
|
|
memcpy(©_buf[lp->offset], lp->buf, lp->size);
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2010-11-11 13:08:23 +00:00
|
|
|
/*
|
|
|
|
* Write the region number in the region index table.
|
|
|
|
* The region number is incremented to avoid values of zero.
|
|
|
|
*/
|
2009-05-13 13:27:19 +00:00
|
|
|
offset = absolute_offset(log_page, 0);
|
2008-11-04 23:30:47 +00:00
|
|
|
++region;
|
|
|
|
COFFEE_WRITE(®ion, sizeof(region),
|
2015-02-17 10:02:06 +00:00
|
|
|
offset + log_record * sizeof(region));
|
2009-02-17 16:56:32 +00:00
|
|
|
|
2009-05-13 13:27:19 +00:00
|
|
|
offset += log_records * sizeof(region);
|
2009-02-17 16:56:32 +00:00
|
|
|
COFFEE_WRITE(copy_buf, sizeof(copy_buf),
|
2015-02-17 10:02:06 +00:00
|
|
|
offset + log_record * log_record_size);
|
2009-07-16 11:58:44 +00:00
|
|
|
file->record_count = log_record + 1;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
return lp->size;
|
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-04-28 10:33:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
get_available_fd(void)
|
|
|
|
{
|
|
|
|
int i;
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2008-07-02 11:11:13 +00:00
|
|
|
for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
|
2008-07-03 23:29:58 +00:00
|
|
|
if(coffee_fd_set[i].flags == COFFEE_FD_FREE) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_open(const char *name, int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
2008-11-04 16:58:52 +00:00
|
|
|
struct file_desc *fdp;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
|
|
|
fd = get_available_fd();
|
2008-07-02 11:11:13 +00:00
|
|
|
if(fd < 0) {
|
2008-09-21 16:07:26 +00:00
|
|
|
PRINTF("Coffee: Failed to allocate a new file descriptor!\n");
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-02-08 17:08:13 +00:00
|
|
|
|
2008-11-04 16:58:52 +00:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2009-02-08 17:08:13 +00:00
|
|
|
fdp->flags = 0;
|
2016-06-13 15:00:20 +00:00
|
|
|
fdp->io_flags = 0;
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
fdp->file = find_file(name);
|
|
|
|
if(fdp->file == NULL) {
|
2008-07-02 11:11:13 +00:00
|
|
|
if((flags & (CFS_READ | CFS_WRITE)) == CFS_READ) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-13 13:27:19 +00:00
|
|
|
fdp->file = reserve(name, page_count(COFFEE_DYN_SIZE), 1, 0);
|
2009-02-08 17:08:13 +00:00
|
|
|
if(fdp->file == NULL) {
|
|
|
|
return -1;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-02-08 17:08:13 +00:00
|
|
|
fdp->file->end = 0;
|
|
|
|
} else if(fdp->file->end == UNKNOWN_OFFSET) {
|
2009-02-14 19:09:17 +00:00
|
|
|
fdp->file->end = file_end(fdp->file->page);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
2008-11-04 16:58:52 +00:00
|
|
|
fdp->flags |= flags;
|
2009-02-08 17:08:13 +00:00
|
|
|
fdp->offset = flags & CFS_APPEND ? fdp->file->end : 0;
|
|
|
|
fdp->file->references++;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cfs_close(int fd)
|
|
|
|
{
|
2008-07-02 11:11:13 +00:00
|
|
|
if(FD_VALID(fd)) {
|
2008-07-03 23:29:58 +00:00
|
|
|
coffee_fd_set[fd].flags = COFFEE_FD_FREE;
|
2009-02-08 17:08:13 +00:00
|
|
|
coffee_fd_set[fd].file->references--;
|
2009-04-02 14:20:04 +00:00
|
|
|
coffee_fd_set[fd].file = NULL;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-27 14:52:52 +00:00
|
|
|
cfs_offset_t
|
|
|
|
cfs_seek(int fd, cfs_offset_t offset, int whence)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2008-12-09 16:01:18 +00:00
|
|
|
struct file_desc *fdp;
|
2010-05-13 18:24:55 +00:00
|
|
|
cfs_offset_t new_offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-07-02 11:11:13 +00:00
|
|
|
if(!FD_VALID(fd)) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-09 16:01:18 +00:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2008-08-28 14:31:10 +00:00
|
|
|
|
2009-02-27 14:25:37 +00:00
|
|
|
if(whence == CFS_SEEK_SET) {
|
2010-05-13 18:24:55 +00:00
|
|
|
new_offset = offset;
|
2009-02-27 14:25:37 +00:00
|
|
|
} else if(whence == CFS_SEEK_END) {
|
2010-05-13 18:24:55 +00:00
|
|
|
new_offset = fdp->file->end + offset;
|
2009-02-27 14:25:37 +00:00
|
|
|
} else if(whence == CFS_SEEK_CUR) {
|
2010-05-13 18:24:55 +00:00
|
|
|
new_offset = fdp->offset + offset;
|
2009-02-27 14:25:37 +00:00
|
|
|
} else {
|
|
|
|
return (cfs_offset_t)-1;
|
|
|
|
}
|
|
|
|
|
2010-05-13 18:24:55 +00:00
|
|
|
if(new_offset < 0 || new_offset > fdp->file->max_pages * COFFEE_PAGE_SIZE) {
|
2009-05-13 13:27:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-13 18:24:55 +00:00
|
|
|
if(fdp->file->end < new_offset) {
|
2016-04-23 08:03:09 +00:00
|
|
|
if(FD_WRITABLE(fd)) {
|
|
|
|
fdp->file->end = new_offset;
|
|
|
|
} else {
|
|
|
|
/* Disallow seeking past the end of the file for read only FDs */
|
|
|
|
return (cfs_offset_t)-1;
|
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2010-05-13 18:24:55 +00:00
|
|
|
return fdp->offset = new_offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-11-24 10:56:55 +00:00
|
|
|
cfs_remove(const char *name)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
2008-04-28 10:33:14 +00:00
|
|
|
|
2008-11-24 10:56:55 +00:00
|
|
|
/*
|
|
|
|
* Coffee removes files by marking them as obsolete. The space
|
|
|
|
* is not guaranteed to be reclaimed immediately, but must be
|
|
|
|
* sweeped by the garbage collector. The garbage collector is
|
|
|
|
* called once a file reservation request cannot be granted.
|
|
|
|
*/
|
2009-02-08 17:08:13 +00:00
|
|
|
file = find_file(name);
|
|
|
|
if(file == NULL) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-08 14:08:14 +00:00
|
|
|
return remove_by_page(file->page, REMOVE_LOG, CLOSE_FDS, ALLOW_GC);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_read(int fd, void *buf, unsigned size)
|
|
|
|
{
|
|
|
|
struct file_desc *fdp;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2010-12-15 14:01:07 +00:00
|
|
|
struct file_header hdr;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct log_param lp;
|
2010-12-15 14:01:07 +00:00
|
|
|
unsigned bytes_left;
|
|
|
|
int r;
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2008-09-21 15:01:48 +00:00
|
|
|
if(!(FD_VALID(fd) && FD_READABLE(fd))) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-03 23:29:58 +00:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2009-02-08 17:08:13 +00:00
|
|
|
file = fdp->file;
|
2018-02-23 12:47:18 +00:00
|
|
|
|
2016-04-29 06:12:55 +00:00
|
|
|
if(fdp->io_flags & CFS_COFFEE_IO_ENSURE_READ_LENGTH) {
|
2016-04-29 05:56:35 +00:00
|
|
|
while(fdp->offset + size > file->end) {
|
2016-06-13 13:55:58 +00:00
|
|
|
((char *)buf)[--size] = '\0';
|
2016-04-29 05:56:35 +00:00
|
|
|
}
|
2016-06-13 13:55:58 +00:00
|
|
|
} else if(fdp->offset + size > file->end) {
|
|
|
|
size = file->end - fdp->offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:30:20 +00:00
|
|
|
/* If the file is not modified, read directly from the file extent. */
|
2010-11-11 13:08:23 +00:00
|
|
|
if(!FILE_MODIFIED(file)) {
|
|
|
|
COFFEE_READ(buf, size, absolute_offset(file->page, fdp->offset));
|
|
|
|
fdp->offset += size;
|
|
|
|
return size;
|
2008-09-21 15:30:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-11 13:08:23 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
|
|
|
read_header(&hdr, file->page);
|
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
/*
|
2016-04-20 12:30:20 +00:00
|
|
|
* Copy the contents of the most recent log record. If there is
|
|
|
|
* no log record for the file area to read from, we simply read
|
|
|
|
* from the original file extent.
|
2008-04-28 10:33:14 +00:00
|
|
|
*/
|
2010-11-11 13:08:23 +00:00
|
|
|
for(bytes_left = size; bytes_left > 0; bytes_left -= r) {
|
|
|
|
lp.offset = fdp->offset;
|
|
|
|
lp.buf = buf;
|
|
|
|
lp.size = bytes_left;
|
|
|
|
r = read_log_page(&hdr, file->record_count, &lp);
|
|
|
|
|
2008-11-03 15:13:13 +00:00
|
|
|
/* Read from the original file if we cannot find the data in the log. */
|
2008-07-02 11:11:13 +00:00
|
|
|
if(r < 0) {
|
2011-07-30 08:05:23 +00:00
|
|
|
COFFEE_READ(buf, lp.size, absolute_offset(file->page, fdp->offset));
|
|
|
|
r = lp.size;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-05-13 13:27:19 +00:00
|
|
|
fdp->offset += r;
|
2011-05-25 15:21:51 +00:00
|
|
|
buf = (char *)buf + r;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2010-11-11 13:08:23 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
|
|
|
|
2009-05-13 13:27:19 +00:00
|
|
|
return size;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_write(int fd, const void *buf, unsigned size)
|
|
|
|
{
|
|
|
|
struct file_desc *fdp;
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2008-04-28 10:33:14 +00:00
|
|
|
int i;
|
|
|
|
struct log_param lp;
|
2009-05-13 13:27:19 +00:00
|
|
|
cfs_offset_t bytes_left;
|
2014-02-24 15:58:33 +00:00
|
|
|
int8_t need_dummy_write;
|
2009-06-10 15:03:35 +00:00
|
|
|
const char dummy[1] = { 0xff };
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif
|
2011-02-13 20:34:29 +00:00
|
|
|
|
2008-09-21 15:01:48 +00:00
|
|
|
if(!(FD_VALID(fd) && FD_WRITABLE(fd))) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2008-07-03 23:29:58 +00:00
|
|
|
fdp = &coffee_fd_set[fd];
|
2009-02-08 17:08:13 +00:00
|
|
|
file = fdp->file;
|
|
|
|
|
2008-11-17 09:09:17 +00:00
|
|
|
/* Attempt to extend the file if we try to write past the end. */
|
2011-02-09 14:03:57 +00:00
|
|
|
if(!(fdp->io_flags & CFS_COFFEE_IO_FIRM_SIZE)) {
|
2016-06-13 13:55:58 +00:00
|
|
|
while(size + fdp->offset + sizeof(struct file_header) >
|
|
|
|
(file->max_pages * COFFEE_PAGE_SIZE)) {
|
|
|
|
if(merge_log(file->page, 1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
file = fdp->file;
|
|
|
|
PRINTF("Extended the file at page %u\n", (unsigned)file->page);
|
2008-11-17 09:09:17 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2011-02-09 14:17:25 +00:00
|
|
|
if(!(fdp->io_flags & CFS_COFFEE_IO_FLASH_AWARE) &&
|
2011-02-09 14:03:57 +00:00
|
|
|
(FILE_MODIFIED(file) || fdp->offset < file->end)) {
|
2014-02-24 15:58:33 +00:00
|
|
|
need_dummy_write = 0;
|
2010-11-11 13:08:23 +00:00
|
|
|
for(bytes_left = size; bytes_left > 0;) {
|
2008-10-16 09:58:57 +00:00
|
|
|
lp.offset = fdp->offset;
|
2016-05-28 20:47:58 +00:00
|
|
|
lp.buf = (void *)buf;
|
2009-05-13 13:27:19 +00:00
|
|
|
lp.size = bytes_left;
|
2009-02-08 17:08:13 +00:00
|
|
|
i = write_log_page(file, &lp);
|
2008-10-16 09:58:57 +00:00
|
|
|
if(i < 0) {
|
2015-02-17 10:02:06 +00:00
|
|
|
/* Return -1 if we wrote nothing because the log write failed. */
|
|
|
|
if(size == bytes_left) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2009-05-13 13:27:19 +00:00
|
|
|
} else if(i == 0) {
|
|
|
|
/* The file was merged with the log. */
|
2015-02-17 10:02:06 +00:00
|
|
|
file = fdp->file;
|
2009-05-13 13:27:19 +00:00
|
|
|
} else {
|
2015-02-17 10:02:06 +00:00
|
|
|
/* A log record was written. */
|
|
|
|
bytes_left -= i;
|
|
|
|
fdp->offset += i;
|
|
|
|
buf = (char *)buf + i;
|
2010-11-11 13:08:23 +00:00
|
|
|
|
|
|
|
/* Update the file end for a potential log merge that might
|
|
|
|
occur while writing log records. */
|
|
|
|
if(fdp->offset > file->end) {
|
|
|
|
file->end = fdp->offset;
|
2014-02-24 15:58:33 +00:00
|
|
|
need_dummy_write = 1;
|
2010-11-11 13:08:23 +00:00
|
|
|
}
|
2008-10-16 09:58:57 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-05-11 16:35:49 +00:00
|
|
|
|
2014-02-24 15:58:33 +00:00
|
|
|
if(need_dummy_write) {
|
|
|
|
/*
|
|
|
|
* A log record has been written at an offset beyond the original
|
|
|
|
* extent's end. Consequently, we need to write a dummy value at the
|
|
|
|
* corresponding end offset in the original extent to ensure that
|
|
|
|
* the correct file size is calculated when opening the file again.
|
|
|
|
*/
|
|
|
|
COFFEE_WRITE(dummy, 1, absolute_offset(file->page, fdp->offset - 1));
|
2009-05-11 16:35:49 +00:00
|
|
|
}
|
2008-04-28 10:33:14 +00:00
|
|
|
} else {
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2016-06-13 13:55:58 +00:00
|
|
|
if(COFFEE_APPEND_ONLY && fdp->offset < file->end) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-11-11 13:08:23 +00:00
|
|
|
|
2016-06-13 13:55:58 +00:00
|
|
|
COFFEE_WRITE(buf, size, absolute_offset(file->page, fdp->offset));
|
|
|
|
fdp->offset += size;
|
2009-06-29 13:30:16 +00:00
|
|
|
#if COFFEE_MICRO_LOGS
|
2016-06-13 13:55:58 +00:00
|
|
|
}
|
2009-06-29 13:30:16 +00:00
|
|
|
#endif /* COFFEE_MICRO_LOGS */
|
2008-10-16 09:58:57 +00:00
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
if(fdp->offset > file->end) {
|
|
|
|
file->end = fdp->offset;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
cfs_opendir(struct cfs_dir *dir, const char *name)
|
2009-01-23 16:27:49 +00:00
|
|
|
{
|
2009-02-28 13:40:25 +00:00
|
|
|
/*
|
2016-04-20 12:30:20 +00:00
|
|
|
* Coffee is only guaranteed to support the directory names "/" and ".",
|
|
|
|
* but it does not enforce this currently.
|
2009-02-28 13:40:25 +00:00
|
|
|
*/
|
2016-06-13 15:00:20 +00:00
|
|
|
memset(dir->state, 0, sizeof(coffee_page_t));
|
2009-02-28 13:40:25 +00:00
|
|
|
return 0;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2008-11-04 23:09:55 +00:00
|
|
|
cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
|
|
|
struct file_header hdr;
|
2008-10-03 19:27:28 +00:00
|
|
|
coffee_page_t page;
|
2016-04-20 12:30:20 +00:00
|
|
|
coffee_page_t next_page;
|
2008-10-03 19:27:28 +00:00
|
|
|
|
2016-06-13 15:00:20 +00:00
|
|
|
memcpy(&page, dir->state, sizeof(coffee_page_t));
|
2010-11-11 13:08:23 +00:00
|
|
|
|
|
|
|
while(page < COFFEE_PAGE_COUNT) {
|
2008-11-04 17:48:36 +00:00
|
|
|
read_header(&hdr, page);
|
2009-02-19 21:35:38 +00:00
|
|
|
if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr)) {
|
|
|
|
memcpy(record->name, hdr.name, sizeof(record->name));
|
|
|
|
record->name[sizeof(record->name) - 1] = '\0';
|
|
|
|
record->size = file_end(page);
|
2010-11-11 13:08:23 +00:00
|
|
|
|
|
|
|
next_page = next_file(page, &hdr);
|
2016-06-13 15:00:20 +00:00
|
|
|
memcpy(dir->state, &next_page, sizeof(coffee_page_t));
|
2009-02-19 21:35:38 +00:00
|
|
|
return 0;
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
2009-02-19 21:35:38 +00:00
|
|
|
page = next_file(page, &hdr);
|
2008-04-28 10:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
cfs_closedir(struct cfs_dir *dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-03-20 15:15:43 +00:00
|
|
|
cfs_coffee_reserve(const char *name, cfs_offset_t size)
|
2008-10-28 14:47:50 +00:00
|
|
|
{
|
2009-05-13 13:27:19 +00:00
|
|
|
return reserve(name, page_count(size), 0, 0) == NULL ? -1 : 0;
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2009-02-08 17:08:13 +00:00
|
|
|
cfs_coffee_configure_log(const char *filename, unsigned log_size,
|
2015-02-17 10:02:06 +00:00
|
|
|
unsigned log_record_size)
|
2008-04-28 10:33:14 +00:00
|
|
|
{
|
2009-02-08 17:08:13 +00:00
|
|
|
struct file *file;
|
2008-04-28 10:33:14 +00:00
|
|
|
struct file_header hdr;
|
2008-10-15 13:57:30 +00:00
|
|
|
|
2008-11-04 23:30:47 +00:00
|
|
|
if(log_record_size == 0 || log_record_size > COFFEE_PAGE_SIZE ||
|
|
|
|
log_size < log_record_size) {
|
2008-10-15 13:57:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-08 17:08:13 +00:00
|
|
|
file = find_file(filename);
|
|
|
|
if(file == NULL) {
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-03 15:13:13 +00:00
|
|
|
|
2009-02-14 19:09:17 +00:00
|
|
|
read_header(&hdr, file->page);
|
2009-02-08 17:08:13 +00:00
|
|
|
if(HDR_MODIFIED(hdr)) {
|
2008-11-03 15:13:13 +00:00
|
|
|
/* Too late to customize the log. */
|
2008-04-28 10:33:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-03 15:13:13 +00:00
|
|
|
|
2008-11-04 23:09:55 +00:00
|
|
|
hdr.log_records = log_size / log_record_size;
|
|
|
|
hdr.log_record_size = log_record_size;
|
2009-02-14 19:09:17 +00:00
|
|
|
write_header(&hdr, file->page);
|
2008-11-03 15:13:13 +00:00
|
|
|
|
2008-04-28 10:33:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-07-02 11:11:13 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-02-09 14:03:57 +00:00
|
|
|
int
|
|
|
|
cfs_coffee_set_io_semantics(int fd, unsigned flags)
|
|
|
|
{
|
|
|
|
if(!FD_VALID(fd)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
coffee_fd_set[fd].io_flags |= flags;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-07-02 11:11:13 +00:00
|
|
|
int
|
|
|
|
cfs_coffee_format(void)
|
|
|
|
{
|
2016-04-20 12:30:20 +00:00
|
|
|
coffee_page_t i;
|
2008-07-02 11:11:13 +00:00
|
|
|
|
2016-04-20 12:30:20 +00:00
|
|
|
PRINTF("Coffee: Formatting %u sectors", (unsigned)COFFEE_SECTOR_COUNT);
|
2008-07-02 11:11:13 +00:00
|
|
|
|
2009-05-11 16:35:49 +00:00
|
|
|
for(i = 0; i < COFFEE_SECTOR_COUNT; i++) {
|
2008-07-02 11:11:13 +00:00
|
|
|
COFFEE_ERASE(i);
|
|
|
|
PRINTF(".");
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:56:32 +00:00
|
|
|
/* Formatting invalidates the file information. */
|
2016-04-20 10:47:37 +00:00
|
|
|
memset(&coffee_files, 0, sizeof(coffee_files));
|
|
|
|
memset(&coffee_fd_set, 0, sizeof(coffee_fd_set));
|
|
|
|
next_free = 0;
|
|
|
|
gc_wait = 1;
|
2008-09-21 12:49:08 +00:00
|
|
|
|
2009-05-25 22:10:04 +00:00
|
|
|
PRINTF(" done!\n");
|
2008-07-02 11:11:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-23 16:27:49 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|