52 #define PRINTF(...) printf(__VA_ARGS__)
57 #include "cfs-coffee-arch.h"
58 #include "contiki-conf.h"
60 #include "cfs-coffee-arch.h"
65 #ifndef COFFEE_MICRO_LOGS
66 #define COFFEE_MICRO_LOGS 1
71 #ifndef COFFEE_APPEND_ONLY
72 #define COFFEE_APPEND_ONLY 0
75 #if COFFEE_MICRO_LOGS && COFFEE_APPEND_ONLY
76 #error "Cannot have COFFEE_APPEND_ONLY set when COFFEE_MICRO_LOGS is set."
81 #ifndef COFFEE_IO_SEMANTICS
82 #define COFFEE_IO_SEMANTICS 0
90 #ifndef COFFEE_EXTENDED_WEAR_LEVELLING
91 #define COFFEE_EXTENDED_WEAR_LEVELLING 1
94 #if COFFEE_START & (COFFEE_SECTOR_SIZE - 1)
95 #error COFFEE_START must point to the first byte in a sector.
98 #define COFFEE_FD_FREE 0x0
99 #define COFFEE_FD_READ 0x1
100 #define COFFEE_FD_WRITE 0x2
101 #define COFFEE_FD_APPEND 0x4
103 #define COFFEE_FILE_MODIFIED 0x1
105 #define INVALID_PAGE ((coffee_page_t)-1)
106 #define UNKNOWN_OFFSET ((cfs_offset_t)-1)
115 #define GC_RELUCTANT 1
118 #define FD_VALID(fd) \
119 ((fd) >= 0 && (fd) < COFFEE_FD_SET_SIZE && \
120 coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
121 #define FD_READABLE(fd) (coffee_fd_set[(fd)].flags & CFS_READ)
122 #define FD_WRITABLE(fd) (coffee_fd_set[(fd)].flags & CFS_WRITE)
123 #define FD_APPENDABLE(fd) (coffee_fd_set[(fd)].flags & CFS_APPEND)
126 #define FILE_MODIFIED(file) ((file)->flags & COFFEE_FILE_MODIFIED)
127 #define FILE_FREE(file) ((file)->max_pages == 0)
128 #define FILE_UNREFERENCED(file) ((file)->references == 0)
131 #define HDR_FLAG_VALID 0x1
132 #define HDR_FLAG_ALLOCATED 0x2
133 #define HDR_FLAG_OBSOLETE 0x4
134 #define HDR_FLAG_MODIFIED 0x8
135 #define HDR_FLAG_LOG 0x10
136 #define HDR_FLAG_ISOLATED 0x20
139 #define CHECK_FLAG(hdr, flag) ((hdr).flags & (flag))
140 #define HDR_VALID(hdr) CHECK_FLAG(hdr, HDR_FLAG_VALID)
141 #define HDR_ALLOCATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ALLOCATED)
142 #define HDR_FREE(hdr) !HDR_ALLOCATED(hdr)
143 #define HDR_LOG(hdr) CHECK_FLAG(hdr, HDR_FLAG_LOG)
144 #define HDR_MODIFIED(hdr) CHECK_FLAG(hdr, HDR_FLAG_MODIFIED)
145 #define HDR_ISOLATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ISOLATED)
146 #define HDR_OBSOLETE(hdr) CHECK_FLAG(hdr, HDR_FLAG_OBSOLETE)
147 #define HDR_ACTIVE(hdr) (HDR_ALLOCATED(hdr) && \
148 !HDR_OBSOLETE(hdr) && \
152 #define COFFEE_SECTOR_COUNT (unsigned)(COFFEE_SIZE / COFFEE_SECTOR_SIZE)
153 #define COFFEE_PAGE_COUNT \
154 ((coffee_page_t)(COFFEE_SIZE / COFFEE_PAGE_SIZE))
155 #define COFFEE_PAGES_PER_SECTOR \
156 ((coffee_page_t)(COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE))
159 struct sector_status {
160 coffee_page_t active;
161 coffee_page_t obsolete;
169 coffee_page_t max_pages;
170 int16_t record_count;
180 #if COFFEE_IO_SEMANTICS
188 coffee_page_t log_page;
189 uint16_t log_records;
190 uint16_t log_record_size;
191 coffee_page_t max_pages;
192 uint8_t deprecated_eof_hint;
194 char name[COFFEE_NAME_LENGTH];
210 static struct protected_mem_t {
212 struct file_desc coffee_fd_set[COFFEE_FD_SET_SIZE];
213 coffee_page_t next_free;
216 static struct file *
const coffee_files = protected_mem.coffee_files;
217 static struct file_desc *
const coffee_fd_set = protected_mem.coffee_fd_set;
218 static coffee_page_t *
const next_free = &protected_mem.next_free;
219 static char *
const gc_wait = &protected_mem.gc_wait;
223 write_header(
struct file_header *hdr, coffee_page_t page)
225 hdr->flags |= HDR_FLAG_VALID;
226 COFFEE_WRITE(hdr,
sizeof(*hdr), page * COFFEE_PAGE_SIZE);
230 read_header(
struct file_header *hdr, coffee_page_t page)
232 COFFEE_READ(hdr,
sizeof(*hdr), page * COFFEE_PAGE_SIZE);
234 if(HDR_ACTIVE(*hdr) && !HDR_VALID(*hdr)) {
235 PRINTF(
"Invalid header at page %u!\n", (
unsigned)page);
241 absolute_offset(coffee_page_t page, cfs_offset_t offset)
243 return page * COFFEE_PAGE_SIZE +
sizeof(
struct file_header) + offset;
247 get_sector_status(uint16_t sector,
struct sector_status *stats)
249 static coffee_page_t skip_pages;
250 static char last_pages_are_active;
251 struct file_header hdr;
252 coffee_page_t active, obsolete, free;
253 coffee_page_t sector_start, sector_end;
256 memset(stats, 0,
sizeof(*stats));
257 active = obsolete = free = 0;
266 last_pages_are_active = 0;
269 sector_start = sector * COFFEE_PAGES_PER_SECTOR;
270 sector_end = sector_start + COFFEE_PAGES_PER_SECTOR;
277 if(last_pages_are_active) {
278 if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
279 stats->active = COFFEE_PAGES_PER_SECTOR;
280 skip_pages -= COFFEE_PAGES_PER_SECTOR;
285 if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
286 stats->obsolete = COFFEE_PAGES_PER_SECTOR;
287 skip_pages -= COFFEE_PAGES_PER_SECTOR;
288 return skip_pages >= COFFEE_PAGES_PER_SECTOR ? 0 : skip_pages;
290 obsolete = skip_pages;
295 for(page = sector_start + skip_pages; page < sector_end;) {
296 read_header(&hdr, page);
297 last_pages_are_active = 0;
298 if(HDR_ACTIVE(hdr)) {
299 last_pages_are_active = 1;
300 page += hdr.max_pages;
301 active += hdr.max_pages;
302 }
else if(HDR_ISOLATED(hdr)) {
305 }
else if(HDR_OBSOLETE(hdr)) {
306 page += hdr.max_pages;
307 obsolete += hdr.max_pages;
309 free = sector_end - page;
322 skip_pages = active + obsolete + free - COFFEE_PAGES_PER_SECTOR;
324 if(last_pages_are_active) {
325 active = COFFEE_PAGES_PER_SECTOR - obsolete;
327 obsolete = COFFEE_PAGES_PER_SECTOR - active;
331 stats->active = active;
332 stats->obsolete = obsolete;
342 return (last_pages_are_active || (skip_pages >= COFFEE_PAGES_PER_SECTOR)) ?
347 isolate_pages(coffee_page_t start, coffee_page_t skip_pages)
349 struct file_header hdr;
354 memset(&hdr, 0,
sizeof(hdr));
355 hdr.flags = HDR_FLAG_ALLOCATED | HDR_FLAG_ISOLATED;
358 for(page = 0; page < skip_pages; page++) {
359 write_header(&hdr, start + page);
361 PRINTF(
"Coffee: Isolated %u pages starting in sector %d\n",
362 (
unsigned)skip_pages, (
int)start / COFFEE_PAGES_PER_SECTOR);
367 collect_garbage(
int mode)
370 struct sector_status stats;
371 coffee_page_t first_page, isolation_count;
373 PRINTF(
"Coffee: Running the file system garbage collector in %s mode\n",
374 mode == GC_RELUCTANT ?
"reluctant" :
"greedy");
379 for(sector = 0; sector < COFFEE_SECTOR_COUNT; sector++) {
380 isolation_count = get_sector_status(sector, &stats);
381 PRINTF(
"Coffee: Sector %u has %u active, %u obsolete, and %u free pages.\n",
382 sector, (
unsigned)stats.active,
383 (
unsigned)stats.obsolete, (
unsigned)stats.free);
385 if(stats.active > 0) {
389 if((mode == GC_RELUCTANT && stats.free == 0) ||
390 (mode == GC_GREEDY && stats.obsolete > 0)) {
391 first_page = sector * COFFEE_PAGES_PER_SECTOR;
392 if(first_page < *next_free) {
393 *next_free = first_page;
396 if(isolation_count > 0) {
397 isolate_pages(first_page + COFFEE_PAGES_PER_SECTOR, isolation_count);
400 COFFEE_ERASE(sector);
401 PRINTF(
"Coffee: Erased sector %d!\n", sector);
403 if(mode == GC_RELUCTANT && isolation_count > 0) {
411 next_file(coffee_page_t page,
struct file_header *hdr)
424 return (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
425 }
else if(HDR_ISOLATED(*hdr)) {
428 return page + hdr->max_pages;
432 load_file(coffee_page_t start,
struct file_header *hdr)
434 int i, unreferenced, free;
443 if(FILE_FREE(&coffee_files[i])) {
446 }
else if(FILE_UNREFERENCED(&coffee_files[i])) {
452 if(unreferenced != -1) {
459 file = &coffee_files[i];
461 file->end = UNKNOWN_OFFSET;
462 file->max_pages = hdr->max_pages;
464 if(HDR_MODIFIED(*hdr)) {
465 file->flags |= COFFEE_FILE_MODIFIED;
468 file->record_count = -1;
474 find_file(
const char *name)
477 struct file_header hdr;
482 if(FILE_FREE(&coffee_files[i])) {
486 read_header(&hdr, coffee_files[i].page);
487 if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
488 return &coffee_files[i];
493 for(page = 0; page < COFFEE_PAGE_COUNT; page = next_file(page, &hdr)) {
494 read_header(&hdr, page);
495 if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
496 return load_file(page, &hdr);
504 file_end(coffee_page_t start)
506 struct file_header hdr;
507 unsigned char buf[COFFEE_PAGE_SIZE];
511 read_header(&hdr, start);
521 for(page = hdr.max_pages - 1; page >= 0; page--) {
522 COFFEE_READ(buf,
sizeof(buf), (start + page) * COFFEE_PAGE_SIZE);
523 for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
525 if(page == 0 && i <
sizeof(hdr)) {
528 return 1 + i + (page * COFFEE_PAGE_SIZE) -
sizeof(hdr);
538 find_contiguous_pages(coffee_page_t amount)
540 coffee_page_t page, start;
541 struct file_header hdr;
543 start = INVALID_PAGE;
544 for(page = *next_free; page < COFFEE_PAGE_COUNT;) {
545 read_header(&hdr, page);
547 if(start == INVALID_PAGE) {
549 if(start + amount >= COFFEE_PAGE_COUNT) {
557 page = next_file(page, &hdr);
559 if(start + amount <= page) {
560 if(start == *next_free) {
561 *next_free = start + amount;
566 start = INVALID_PAGE;
567 page = next_file(page, &hdr);
574 remove_by_page(coffee_page_t page,
int remove_log,
int close_fds,
577 struct file_header hdr;
580 read_header(&hdr, page);
581 if(!HDR_ACTIVE(hdr)) {
585 if(remove_log && HDR_MODIFIED(hdr)) {
586 if(remove_by_page(hdr.log_page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
591 hdr.flags |= HDR_FLAG_OBSOLETE;
592 write_header(&hdr, page);
598 for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
599 if(coffee_fd_set[i].file !=
NULL && coffee_fd_set[i].file->page == page) {
600 coffee_fd_set[i].flags = COFFEE_FD_FREE;
606 if(coffee_files[i].page == page) {
607 coffee_files[i].page = INVALID_PAGE;
608 coffee_files[i].references = 0;
609 coffee_files[i].max_pages = 0;
613 #if !COFFEE_EXTENDED_WEAR_LEVELLING
615 collect_garbage(GC_RELUCTANT);
623 page_count(cfs_offset_t size)
625 return (size +
sizeof(
struct file_header) + COFFEE_PAGE_SIZE - 1) /
630 reserve(
const char *name, coffee_page_t pages,
631 int allow_duplicates,
unsigned flags)
633 struct file_header hdr;
637 if(!allow_duplicates && find_file(name) !=
NULL) {
641 page = find_contiguous_pages(pages);
642 if(page == INVALID_PAGE) {
646 collect_garbage(GC_GREEDY);
647 page = find_contiguous_pages(pages);
648 if(page == INVALID_PAGE) {
654 memset(&hdr, 0,
sizeof(hdr));
655 memcpy(hdr.name, name,
sizeof(hdr.name) - 1);
656 hdr.max_pages = pages;
657 hdr.flags = HDR_FLAG_ALLOCATED |
flags;
658 write_header(&hdr, page);
660 PRINTF(
"Coffee: Reserved %u pages starting from %u for file %s\n",
663 file = load_file(page, &hdr);
671 #if COFFEE_MICRO_LOGS
673 adjust_log_config(
struct file_header *hdr,
674 uint16_t *log_record_size, uint16_t *log_records)
676 *log_record_size = hdr->log_record_size == 0 ?
677 COFFEE_PAGE_SIZE : hdr->log_record_size;
678 *log_records = hdr->log_records == 0 ?
679 COFFEE_LOG_SIZE / *log_record_size : hdr->log_records;
683 #if COFFEE_MICRO_LOGS
685 modify_log_buffer(uint16_t log_record_size,
686 cfs_offset_t *offset, uint16_t *size)
690 region = *offset / log_record_size;
691 *offset %= log_record_size;
693 if(*size > log_record_size - *offset) {
694 *size = log_record_size - *offset;
701 #if COFFEE_MICRO_LOGS
703 get_record_index(coffee_page_t log_page, uint16_t search_records,
709 int16_t match_index, i;
711 base = absolute_offset(log_page,
sizeof(uint16_t) * search_records);
712 batch_size = search_records > COFFEE_LOG_TABLE_LIMIT ?
713 COFFEE_LOG_TABLE_LIMIT : search_records;
718 uint16_t indices[batch_size];
720 while(processed < search_records && match_index < 0) {
721 if(batch_size + processed > search_records) {
722 batch_size = search_records - processed;
725 base -= batch_size *
sizeof(indices[0]);
726 COFFEE_READ(&indices,
sizeof(indices[0]) * batch_size, base);
728 for(i = batch_size - 1; i >= 0; i--) {
729 if(indices[i] - 1 == region) {
730 match_index = search_records - processed - (batch_size - i);
735 processed += batch_size;
743 #if COFFEE_MICRO_LOGS
745 read_log_page(
struct file_header *hdr, int16_t record_count,
746 struct log_param *lp)
750 uint16_t log_record_size;
751 uint16_t log_records;
753 uint16_t search_records;
755 adjust_log_config(hdr, &log_record_size, &log_records);
756 region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
758 search_records = record_count < 0 ? log_records : record_count;
759 match_index = get_record_index(hdr->log_page, search_records, region);
760 if(match_index < 0) {
764 base = absolute_offset(hdr->log_page, log_records *
sizeof(region));
765 base += (cfs_offset_t)match_index * log_record_size;
767 COFFEE_READ(lp->buf, lp->size, base);
773 #if COFFEE_MICRO_LOGS
775 create_log(
struct file *file,
struct file_header *hdr)
777 uint16_t log_record_size, log_records;
779 struct file *log_file;
781 adjust_log_config(hdr, &log_record_size, &log_records);
784 size = log_records * (
sizeof(uint16_t) + log_record_size);
786 log_file = reserve(hdr->name, page_count(size), 1, HDR_FLAG_LOG);
787 if(log_file ==
NULL) {
791 hdr->flags |= HDR_FLAG_MODIFIED;
792 hdr->log_page = log_file->page;
793 write_header(hdr, file->page);
795 file->flags |= COFFEE_FILE_MODIFIED;
796 return log_file->page;
801 merge_log(coffee_page_t file_page,
int extend)
803 struct file_header hdr, hdr2;
806 coffee_page_t max_pages;
807 struct file *new_file;
810 read_header(&hdr, file_page);
821 max_pages = hdr.max_pages << extend;
822 new_file = reserve(hdr.name, max_pages, 1, 0);
823 if(new_file ==
NULL) {
830 char buf[hdr.log_record_size == 0 ? COFFEE_PAGE_SIZE : hdr.log_record_size];
831 n = cfs_read(fd, buf,
sizeof(buf));
833 remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, ALLOW_GC);
837 COFFEE_WRITE(buf, n, absolute_offset(new_file->page, offset));
842 for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
843 if(coffee_fd_set[i].flags != COFFEE_FD_FREE &&
844 coffee_fd_set[i].file->page == file_page) {
845 coffee_fd_set[i].file = new_file;
846 new_file->references++;
850 if(remove_by_page(file_page, REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
851 remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC);
857 read_header(&hdr2, new_file->page);
858 hdr2.log_record_size = hdr.log_record_size;
859 hdr2.log_records = hdr.log_records;
860 write_header(&hdr2, new_file->page);
862 new_file->flags &= ~COFFEE_FILE_MODIFIED;
863 new_file->end = offset;
870 #if COFFEE_MICRO_LOGS
872 find_next_record(
struct file *file, coffee_page_t log_page,
875 int log_record, preferred_batch_size;
877 if(file->record_count >= 0) {
878 return file->record_count;
881 preferred_batch_size = log_records > COFFEE_LOG_TABLE_LIMIT ?
882 COFFEE_LOG_TABLE_LIMIT : log_records;
885 uint16_t indices[preferred_batch_size];
889 log_record = log_records;
890 for(processed = 0; processed < log_records; processed += batch_size) {
891 batch_size = log_records - processed >= preferred_batch_size ?
892 preferred_batch_size : log_records - processed;
894 COFFEE_READ(&indices, batch_size *
sizeof(indices[0]),
895 absolute_offset(log_page, processed *
sizeof(indices[0])));
896 for(log_record = 0; log_record < batch_size; log_record++) {
897 if(indices[log_record] == 0) {
898 log_record += processed;
909 #if COFFEE_MICRO_LOGS
911 write_log_page(
struct file *file,
struct log_param *lp)
913 struct file_header hdr;
915 coffee_page_t log_page;
917 uint16_t log_record_size;
918 uint16_t log_records;
920 struct log_param lp_out;
922 read_header(&hdr, file->page);
924 adjust_log_config(&hdr, &log_record_size, &log_records);
925 region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
928 if(HDR_MODIFIED(hdr)) {
930 log_page = hdr.log_page;
931 log_record = find_next_record(file, log_page, log_records);
932 if(log_record >= log_records) {
934 PRINTF(
"Coffee: Merging the file %s with its log\n", hdr.name);
935 return merge_log(file->page, 0);
939 log_page = create_log(file, &hdr);
940 if(log_page == INVALID_PAGE) {
943 PRINTF(
"Coffee: Created a log structure for file %s at page %u\n",
944 hdr.name, (
unsigned)log_page);
945 hdr.log_page = log_page;
950 char copy_buf[log_record_size];
952 lp_out.offset = offset = region * log_record_size;
953 lp_out.buf = copy_buf;
954 lp_out.size = log_record_size;
956 if((lp->offset > 0 || lp->size != log_record_size) &&
957 read_log_page(&hdr, log_record, &lp_out) < 0) {
958 COFFEE_READ(copy_buf,
sizeof(copy_buf),
959 absolute_offset(file->page, offset));
962 memcpy(©_buf[lp->offset], lp->buf, lp->size);
968 offset = absolute_offset(log_page, 0);
970 COFFEE_WRITE(®ion,
sizeof(region),
971 offset + log_record *
sizeof(region));
973 offset += log_records *
sizeof(region);
974 COFFEE_WRITE(copy_buf,
sizeof(copy_buf),
975 offset + log_record * log_record_size);
976 file->record_count = log_record + 1;
984 get_available_fd(
void)
986 PRINTF(
"get_available_fd()\n");
989 for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
990 if(coffee_fd_set[i].flags == COFFEE_FD_FREE) {
1001 struct file_desc *fdp;
1003 fd = get_available_fd();
1005 PRINTF(
"Coffee: Failed to allocate a new file descriptor!\n");
1009 fdp = &coffee_fd_set[fd];
1012 PRINTF(
"Coffee: open file %s\n",name);
1013 fdp->file = find_file(name);
1014 if(fdp->file ==
NULL) {
1018 fdp->file = reserve(name, page_count(COFFEE_DYN_SIZE), 1, 0);
1019 if(fdp->file ==
NULL) {
1023 }
else if(fdp->file->end == UNKNOWN_OFFSET) {
1024 fdp->file->end = file_end(fdp->file->page);
1027 fdp->flags |=
flags;
1028 fdp->offset = flags &
CFS_APPEND ? fdp->file->end : 0;
1029 fdp->file->references++;
1038 coffee_fd_set[fd].flags = COFFEE_FD_FREE;
1039 coffee_fd_set[fd].file->references--;
1040 coffee_fd_set[fd].file =
NULL;
1047 struct file_desc *fdp;
1048 cfs_offset_t new_offset;
1053 fdp = &coffee_fd_set[fd];
1056 new_offset = offset;
1058 new_offset = fdp->file->end + offset;
1060 new_offset = fdp->offset + offset;
1062 return (cfs_offset_t)-1;
1065 if(new_offset < 0 || new_offset > fdp->file->max_pages * COFFEE_PAGE_SIZE) {
1069 if(fdp->file->end < new_offset) {
1070 fdp->file->end = new_offset;
1073 return fdp->offset = new_offset;
1087 file = find_file(name);
1092 return remove_by_page(file->page, REMOVE_LOG, CLOSE_FDS, ALLOW_GC);
1096 cfs_read(
int fd,
void *buf,
unsigned size)
1098 struct file_desc *fdp;
1100 #if COFFEE_MICRO_LOGS
1101 struct file_header hdr;
1102 struct log_param lp;
1103 unsigned bytes_left;
1107 if(!(FD_VALID(fd) && FD_READABLE(fd))) {
1111 fdp = &coffee_fd_set[fd];
1113 if(fdp->offset + size > file->end) {
1114 size = file->end - fdp->offset;
1118 if(!FILE_MODIFIED(file)) {
1119 COFFEE_READ(buf, size, absolute_offset(file->page, fdp->offset));
1120 fdp->offset += size;
1124 #if COFFEE_MICRO_LOGS
1125 read_header(&hdr, file->page);
1131 for(bytes_left = size; bytes_left > 0; bytes_left -= r) {
1134 lp.offset = fdp->offset;
1136 lp.size = bytes_left;
1137 r = read_log_page(&hdr, file->record_count, &lp);
1141 COFFEE_READ(buf, lp.size, absolute_offset(file->page, fdp->offset));
1145 buf = (
char *)buf + r;
1153 cfs_write(
int fd,
const void *buf,
unsigned size)
1155 struct file_desc *fdp;
1157 #if COFFEE_MICRO_LOGS
1159 struct log_param lp;
1160 cfs_offset_t bytes_left;
1161 const char dummy[1] = { 0xff };
1164 if(!(FD_VALID(fd) && FD_WRITABLE(fd))) {
1168 fdp = &coffee_fd_set[fd];
1172 #if COFFEE_IO_SEMANTICS
1175 while(size + fdp->offset +
sizeof(
struct file_header) >
1176 (file->max_pages * COFFEE_PAGE_SIZE)) {
1177 if(merge_log(file->page, 1) < 0) {
1181 PRINTF(
"Extended the file at page %u\n", (
unsigned)file->page);
1183 #if COFFEE_IO_SEMANTICS
1187 #if COFFEE_MICRO_LOGS
1188 #if COFFEE_IO_SEMANTICS
1190 (FILE_MODIFIED(file) || fdp->offset < file->end)) {
1192 if(FILE_MODIFIED(file) || fdp->offset < file->end) {
1194 for(bytes_left = size; bytes_left > 0;) {
1195 lp.offset = fdp->offset;
1197 lp.size = bytes_left;
1198 i = write_log_page(file, &lp);
1201 if(size == bytes_left) {
1212 buf = (
char *)buf + i;
1216 if(fdp->offset > file->end) {
1217 file->end = fdp->offset;
1222 if(fdp->offset > file->end) {
1224 COFFEE_WRITE(dummy, 1, absolute_offset(file->page, fdp->offset));
1228 #if COFFEE_APPEND_ONLY
1229 if(fdp->offset < file->end) {
1234 COFFEE_WRITE(buf, size, absolute_offset(file->page, fdp->offset));
1235 fdp->offset += size;
1236 #if COFFEE_MICRO_LOGS
1240 if(fdp->offset > file->end) {
1241 file->end = fdp->offset;
1254 memset(dir->dummy_space, 0,
sizeof(coffee_page_t));
1261 struct file_header hdr;
1264 memcpy(&page, dir->dummy_space,
sizeof(coffee_page_t));
1266 while(page < COFFEE_PAGE_COUNT) {
1267 read_header(&hdr, page);
1268 if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr)) {
1269 coffee_page_t next_page;
1270 memcpy(record->name, hdr.name,
sizeof(record->name));
1271 record->name[
sizeof(record->name) - 1] =
'\0';
1272 record->size = file_end(page);
1274 next_page = next_file(page, &hdr);
1275 memcpy(dir->dummy_space, &next_page,
sizeof(coffee_page_t));
1278 page = next_file(page, &hdr);
1293 return reserve(name, page_count(size), 0, 0) ==
NULL ? -1 : 0;
1298 unsigned log_record_size)
1301 struct file_header hdr;
1303 if(log_record_size == 0 || log_record_size > COFFEE_PAGE_SIZE ||
1304 log_size < log_record_size) {
1308 file = find_file(filename);
1313 read_header(&hdr, file->page);
1314 if(HDR_MODIFIED(hdr)) {
1319 hdr.log_records = log_size / log_record_size;
1320 hdr.log_record_size = log_record_size;
1321 write_header(&hdr, file->page);
1326 #if COFFEE_IO_SEMANTICS
1334 coffee_fd_set[fd].io_flags |=
flags;
1345 PRINTF(
"Coffee: Formatting %u sectors", COFFEE_SECTOR_COUNT);
1349 for(i = 0; i < COFFEE_SECTOR_COUNT; i++) {
1355 memset(&protected_mem, 0,
sizeof(protected_mem));
1365 *size =
sizeof(protected_mem);
1366 return &protected_mem;