Contiki 3.x
cfs-coffee.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 2009, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Coffee: A file system for a variety of storage types in
36  * memory-constrained devices.
37  *
38  * For further information, see "Enabling Large-Scale Storage in
39  * Sensor Networks with the Coffee File System" in the proceedings
40  * of ACM/IEEE IPSN 2009.
41  *
42  * \author
43  * Nicolas Tsiftes <nvt@sics.se>
44  */
45 
46 #include <limits.h>
47 #include <string.h>
48 
49 #define DEBUG 0
50 #if DEBUG
51 #include <stdio.h>
52 #define PRINTF(...) printf(__VA_ARGS__)
53 #else
54 #define PRINTF(...)
55 #endif
56 
57 #include "contiki-conf.h"
58 #include "cfs/cfs.h"
59 #include "cfs-coffee-arch.h"
60 #include "cfs/cfs-coffee.h"
61 
62 /* Micro logs enable modifications on storage types that do not support
63  in-place updates. This applies primarily to flash memories. */
64 #ifndef COFFEE_MICRO_LOGS
65 #define COFFEE_MICRO_LOGS 1
66 #endif
67 
68 /* Small headers reduces the size of file headers by removing the micro log
69  related parameters. */
70 #ifndef COFFEE_SMALL_HEADERS
71 #define COFFEE_SMALL_HEADERS 0
72 #endif
73 
74 #if COFFEE_SMALL_HEADERS && COFFEE_MICRO_LOGS
75 #error "Cannot have COFFEE_SMALL_HEADERS set when COFFEE_MICRO_LOGS is set."
76 #endif
77 
78 /* If the files are expected to be appended to only, this parameter
79  can be set to save some code space. */
80 #ifndef COFFEE_APPEND_ONLY
81 #define COFFEE_APPEND_ONLY 0
82 #endif
83 
84 #if COFFEE_MICRO_LOGS && COFFEE_APPEND_ONLY
85 #error "Cannot have COFFEE_APPEND_ONLY set when COFFEE_MICRO_LOGS is set."
86 #endif
87 
88 /* I/O semantics can be set on file descriptors in order to optimize
89  file access on certain storage types. */
90 #ifndef COFFEE_IO_SEMANTICS
91 #define COFFEE_IO_SEMANTICS 0
92 #endif
93 
94 /*
95  * Prevent sectors from being erased directly after file removal.
96  * This will level the wear across sectors better, but may lead
97  * to longer garbage collection procedures.
98  */
99 #ifndef COFFEE_EXTENDED_WEAR_LEVELLING
100 #define COFFEE_EXTENDED_WEAR_LEVELLING 1
101 #endif
102 
103 #if COFFEE_START & (COFFEE_SECTOR_SIZE - 1)
104 #error COFFEE_START must point to the first byte in a sector.
105 #endif
106 
107 #define COFFEE_FD_FREE 0x0
108 #define COFFEE_FD_READ 0x1
109 #define COFFEE_FD_WRITE 0x2
110 #define COFFEE_FD_APPEND 0x4
111 
112 #define COFFEE_FILE_MODIFIED 0x1
113 
114 #define INVALID_PAGE ((coffee_page_t)-1)
115 #define UNKNOWN_OFFSET ((cfs_offset_t)-1)
116 
117 #define REMOVE_LOG 1
118 #define CLOSE_FDS 1
119 #define ALLOW_GC 1
120 
121 /* "Greedy" garbage collection erases as many sectors as possible. */
122 #define GC_GREEDY 0
123 /* "Reluctant" garbage collection stops after erasing one sector. */
124 #define GC_RELUCTANT 1
125 
126 /* File descriptor macros. */
127 #define FD_VALID(fd) ((fd) >= 0 && (fd) < COFFEE_FD_SET_SIZE && \
128  coffee_fd_set[(fd)].flags != COFFEE_FD_FREE)
129 #define FD_READABLE(fd) (coffee_fd_set[(fd)].flags & CFS_READ)
130 #define FD_WRITABLE(fd) (coffee_fd_set[(fd)].flags & CFS_WRITE)
131 #define FD_APPENDABLE(fd) (coffee_fd_set[(fd)].flags & CFS_APPEND)
132 
133 /* File object macros. */
134 #define FILE_MODIFIED(file) ((file)->flags & COFFEE_FILE_MODIFIED)
135 #define FILE_FREE(file) ((file)->max_pages == 0)
136 #define FILE_UNREFERENCED(file) ((file)->references == 0)
137 
138 /* File header flags. */
139 #define HDR_FLAG_VALID 0x01 /* Completely written header. */
140 #define HDR_FLAG_ALLOCATED 0x02 /* Allocated file. */
141 #define HDR_FLAG_OBSOLETE 0x04 /* File marked for GC. */
142 #define HDR_FLAG_MODIFIED 0x08 /* Modified file, log exists. */
143 #define HDR_FLAG_LOG 0x10 /* Log file. */
144 #define HDR_FLAG_ISOLATED 0x20 /* Isolated page. */
145 
146 /* File header macros. */
147 #define CHECK_FLAG(hdr, flag) ((hdr).flags & (flag))
148 #define HDR_VALID(hdr) CHECK_FLAG(hdr, HDR_FLAG_VALID)
149 #define HDR_ALLOCATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ALLOCATED)
150 #define HDR_FREE(hdr) !HDR_ALLOCATED(hdr)
151 #define HDR_LOG(hdr) CHECK_FLAG(hdr, HDR_FLAG_LOG)
152 #define HDR_MODIFIED(hdr) CHECK_FLAG(hdr, HDR_FLAG_MODIFIED)
153 #define HDR_ISOLATED(hdr) CHECK_FLAG(hdr, HDR_FLAG_ISOLATED)
154 #define HDR_OBSOLETE(hdr) CHECK_FLAG(hdr, HDR_FLAG_OBSOLETE)
155 #define HDR_ACTIVE(hdr) (HDR_ALLOCATED(hdr) && \
156  !HDR_OBSOLETE(hdr) && \
157  !HDR_ISOLATED(hdr))
158 
159 /* Shortcuts derived from the hardware-dependent configuration of Coffee. */
160 #define COFFEE_SECTOR_COUNT \
161  (coffee_page_t)(COFFEE_SIZE / COFFEE_SECTOR_SIZE)
162 #define COFFEE_PAGE_COUNT \
163  ((coffee_page_t)(COFFEE_SIZE / COFFEE_PAGE_SIZE))
164 #define COFFEE_PAGES_PER_SECTOR \
165  ((coffee_page_t)(COFFEE_SECTOR_SIZE / COFFEE_PAGE_SIZE))
166 
167 /* This structure is used for garbage collection statistics. */
168 struct sector_status {
169  coffee_page_t active;
170  coffee_page_t obsolete;
171  coffee_page_t free;
172 };
173 
174 /* The structure of cached file objects. */
175 struct file {
176  cfs_offset_t end;
177  coffee_page_t page;
178  coffee_page_t max_pages;
179  int16_t record_count;
180  uint8_t references;
181  uint8_t flags;
182 };
183 
184 /* The file descriptor structure. */
185 struct file_desc {
186  cfs_offset_t offset;
187  struct file *file;
188  uint8_t flags;
189 #if COFFEE_IO_SEMANTICS
190  uint8_t io_flags;
191 #endif
192 };
193 
194 /* The file header structure mimics the representation of file headers
195  in the physical storage medium. */
196 struct file_header {
197 #if !COFFEE_SMALL_HEADERS
198  coffee_page_t log_page;
199  uint16_t log_records;
200  uint16_t log_record_size;
201 #endif /* !COFFEE_SMALL_HEADERS */
202  coffee_page_t max_pages;
203 #if !COFFEE_SMALL_HEADERS
204  uint8_t deprecated_eof_hint;
205 #endif /* !COFFEE_SMALL_HEADERS */
206  uint8_t flags;
207  char name[COFFEE_NAME_LENGTH];
208 };
209 
210 #if COFFEE_MICRO_LOGS
211 /* This is needed because of a buggy compiler. */
212 struct log_param {
213  cfs_offset_t offset;
214  char *buf;
215  uint16_t size;
216 };
217 #endif /* COFFEE_MICRO_LOGS */
218 
219 /*
220  * Variables that keep track of opened files and internal
221  * optimization information for Coffee.
222  */
223 static struct file coffee_files[COFFEE_MAX_OPEN_FILES];
224 static struct file_desc coffee_fd_set[COFFEE_FD_SET_SIZE];
225 static coffee_page_t next_free;
226 static char gc_wait;
227 
228 /*---------------------------------------------------------------------------*/
229 static void
230 write_header(struct file_header *hdr, coffee_page_t page)
231 {
232  hdr->flags |= HDR_FLAG_VALID;
233  COFFEE_WRITE(hdr, sizeof(*hdr), page * COFFEE_PAGE_SIZE);
234 }
235 /*---------------------------------------------------------------------------*/
236 static void
237 read_header(struct file_header *hdr, coffee_page_t page)
238 {
239  COFFEE_READ(hdr, sizeof(*hdr), page * COFFEE_PAGE_SIZE);
240 #if DEBUG
241  if(HDR_ACTIVE(*hdr) && !HDR_VALID(*hdr)) {
242  PRINTF("Invalid header at page %u!\n", (unsigned)page);
243  }
244 #endif
245 }
246 /*---------------------------------------------------------------------------*/
247 static cfs_offset_t
248 absolute_offset(coffee_page_t page, cfs_offset_t offset)
249 {
250  return page * COFFEE_PAGE_SIZE + sizeof(struct file_header) + offset;
251 }
252 /*---------------------------------------------------------------------------*/
253 static coffee_page_t
254 get_sector_status(coffee_page_t sector, struct sector_status *stats)
255 {
256  static coffee_page_t skip_pages;
257  static char last_pages_are_active;
258  struct file_header hdr;
259  coffee_page_t active, obsolete, free;
260  coffee_page_t sector_start, sector_end;
261  coffee_page_t page;
262 
263  memset(stats, 0, sizeof(*stats));
264  active = obsolete = free = 0;
265 
266  /*
267  * get_sector_status() is an iterative function using local static
268  * state. It therefore requires that the caller starts iterating from
269  * sector 0 in order to reset the internal state.
270  */
271  if(sector == 0) {
272  skip_pages = 0;
273  last_pages_are_active = 0;
274  }
275 
276  sector_start = sector * COFFEE_PAGES_PER_SECTOR;
277  sector_end = sector_start + COFFEE_PAGES_PER_SECTOR;
278 
279  /*
280  * Account for pages belonging to a file starting in a previous
281  * segment that extends into this segment. If the whole segment is
282  * covered, we do not need to continue counting pages in this iteration.
283  */
284  if(last_pages_are_active) {
285  if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
286  stats->active = COFFEE_PAGES_PER_SECTOR;
287  skip_pages -= COFFEE_PAGES_PER_SECTOR;
288  return 0;
289  }
290  active = skip_pages;
291  } else {
292  if(skip_pages >= COFFEE_PAGES_PER_SECTOR) {
293  stats->obsolete = COFFEE_PAGES_PER_SECTOR;
294  skip_pages -= COFFEE_PAGES_PER_SECTOR;
295  return skip_pages >= COFFEE_PAGES_PER_SECTOR ? 0 : skip_pages;
296  }
297  obsolete = skip_pages;
298  }
299 
300  /* Determine the amount of pages of each type that have not been
301  accounted for yet in the current sector. */
302  for(page = sector_start + skip_pages; page < sector_end;) {
303  read_header(&hdr, page);
304  last_pages_are_active = 0;
305  if(HDR_ACTIVE(hdr)) {
306  last_pages_are_active = 1;
307  page += hdr.max_pages;
308  active += hdr.max_pages;
309  } else if(HDR_ISOLATED(hdr)) {
310  page++;
311  obsolete++;
312  } else if(HDR_OBSOLETE(hdr)) {
313  page += hdr.max_pages;
314  obsolete += hdr.max_pages;
315  } else {
316  free = sector_end - page;
317  break;
318  }
319  }
320 
321  /*
322  * Determine the amount of pages in the following sectors that
323  * should be remembered for the next iteration. This is necessary
324  * because no file page except the first contains information
325  * about what type of page it is. A side effect of remembering this
326  * amount is that there is no need to read in the headers of each
327  * of these pages from the storage.
328  */
329  skip_pages = active + obsolete + free - COFFEE_PAGES_PER_SECTOR;
330  if(skip_pages > 0) {
331  if(last_pages_are_active) {
332  active = COFFEE_PAGES_PER_SECTOR - obsolete;
333  } else {
334  obsolete = COFFEE_PAGES_PER_SECTOR - active;
335  }
336  }
337 
338  stats->active = active;
339  stats->obsolete = obsolete;
340  stats->free = free;
341 
342  /*
343  * To avoid unnecessary page isolation, we notify the caller that
344  * "skip_pages" pages should be isolated only if the current file extent
345  * ends in the next sector. If the file extent ends in a more distant
346  * sector, however, the garbage collection can free the next sector
347  * immediately without requiring page isolation.
348  */
349  return (last_pages_are_active || (skip_pages >= COFFEE_PAGES_PER_SECTOR)) ?
350  0 : skip_pages;
351 }
352 /*---------------------------------------------------------------------------*/
353 static void
354 isolate_pages(coffee_page_t start, coffee_page_t skip_pages)
355 {
356  struct file_header hdr;
357  coffee_page_t page;
358 
359  /* Split an obsolete file starting in the previous sector and mark
360  the following pages as isolated. */
361  memset(&hdr, 0, sizeof(hdr));
362  hdr.flags = HDR_FLAG_ALLOCATED | HDR_FLAG_ISOLATED;
363 
364  /* Isolation starts from the next sector. */
365  for(page = 0; page < skip_pages; page++) {
366  write_header(&hdr, start + page);
367  }
368  PRINTF("Coffee: Isolated %u pages starting in sector %d\n",
369  (unsigned)skip_pages, (int)start / COFFEE_PAGES_PER_SECTOR);
370 }
371 /*---------------------------------------------------------------------------*/
372 static void
373 collect_garbage(int mode)
374 {
375  coffee_page_t sector;
376  struct sector_status stats;
377  coffee_page_t first_page, isolation_count;
378 
379  PRINTF("Coffee: Running the garbage collector in %s mode\n",
380  mode == GC_RELUCTANT ? "reluctant" : "greedy");
381  /*
382  * The garbage collector erases as many sectors as possible. A sector is
383  * erasable if there are only free or obsolete pages in it.
384  */
385  for(sector = 0; sector < COFFEE_SECTOR_COUNT; sector++) {
386  isolation_count = get_sector_status(sector, &stats);
387  PRINTF("Coffee: Sector %u has %u active, %u obsolete, and %u free pages.\n",
388  (unsigned)sector, (unsigned)stats.active,
389  (unsigned)stats.obsolete, (unsigned)stats.free);
390 
391  if(stats.active > 0) {
392  continue;
393  }
394 
395  if((mode == GC_RELUCTANT && stats.free == 0) ||
396  (mode == GC_GREEDY && stats.obsolete > 0)) {
397  first_page = sector * COFFEE_PAGES_PER_SECTOR;
398  if(first_page < next_free) {
399  next_free = first_page;
400  }
401 
402  if(isolation_count > 0) {
403  isolate_pages(first_page + COFFEE_PAGES_PER_SECTOR, isolation_count);
404  }
405 
406  COFFEE_ERASE(sector);
407  PRINTF("Coffee: Erased sector %d!\n", sector);
408 
409  if(mode == GC_RELUCTANT && isolation_count > 0) {
410  break;
411  }
412  }
413  }
414 }
415 /*---------------------------------------------------------------------------*/
416 static coffee_page_t
417 next_file(coffee_page_t page, struct file_header *hdr)
418 {
419  /*
420  * The quick-skip algorithm for finding file extents is the most
421  * essential part of Coffee. The file allocation rules enable this
422  * algorithm to quickly jump over free areas and allocated extents
423  * after reading single headers and determining their status.
424  *
425  * The worst-case performance occurs when we encounter multiple long
426  * sequences of isolated pages, but such sequences are uncommon and
427  * always shorter than a sector.
428  */
429  if(HDR_FREE(*hdr)) {
430  return (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
431  } else if(HDR_ISOLATED(*hdr)) {
432  return page + 1;
433  }
434  return page + hdr->max_pages;
435 }
436 /*---------------------------------------------------------------------------*/
437 static struct file *
438 load_file(coffee_page_t start, struct file_header *hdr)
439 {
440  int i, unreferenced, free;
441  struct file *file;
442 
443  /*
444  * We prefer to overwrite a free slot since unreferenced ones
445  * contain usable data. Free slots are designated by the page
446  * value INVALID_PAGE.
447  */
448  for(i = 0, unreferenced = free = -1; i < COFFEE_MAX_OPEN_FILES; i++) {
449  if(FILE_FREE(&coffee_files[i])) {
450  free = i;
451  break;
452  } else if(FILE_UNREFERENCED(&coffee_files[i])) {
453  unreferenced = i;
454  }
455  }
456 
457  if(free == -1) {
458  if(unreferenced != -1) {
459  i = unreferenced;
460  } else {
461  return NULL;
462  }
463  }
464 
465  file = &coffee_files[i];
466  file->page = start;
467  file->end = UNKNOWN_OFFSET;
468  file->max_pages = hdr->max_pages;
469  file->flags = 0;
470  if(HDR_MODIFIED(*hdr)) {
471  file->flags |= COFFEE_FILE_MODIFIED;
472  }
473  /* We don't know the amount of records yet. */
474  file->record_count = -1;
475 
476  return file;
477 }
478 /*---------------------------------------------------------------------------*/
479 static struct file *
480 find_file(const char *name)
481 {
482  int i;
483  struct file_header hdr;
484  coffee_page_t page;
485 
486  /* First check if the file metadata is cached. */
487  for(i = 0; i < COFFEE_MAX_OPEN_FILES; i++) {
488  if(FILE_FREE(&coffee_files[i])) {
489  continue;
490  }
491 
492  read_header(&hdr, coffee_files[i].page);
493  if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
494  return &coffee_files[i];
495  }
496  }
497 
498  /* Scan the flash memory sequentially otherwise. */
499  for(page = 0; page < COFFEE_PAGE_COUNT; page = next_file(page, &hdr)) {
500  read_header(&hdr, page);
501  if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr) && strcmp(name, hdr.name) == 0) {
502  return load_file(page, &hdr);
503  }
504  }
505 
506  return NULL;
507 }
508 /*---------------------------------------------------------------------------*/
509 static cfs_offset_t
510 file_end(coffee_page_t start)
511 {
512  struct file_header hdr;
513  unsigned char buf[COFFEE_PAGE_SIZE];
514  coffee_page_t page;
515  int i;
516 
517  read_header(&hdr, start);
518 
519  /*
520  * Move from the end of the range towards the beginning and look for
521  * a byte that has been modified.
522  *
523  * An important implication of this is that if the last written bytes
524  * are zeroes, then these are skipped from the calculation.
525  */
526 
527  for(page = hdr.max_pages - 1; page >= 0; page--) {
528  COFFEE_READ(buf, sizeof(buf), (start + page) * COFFEE_PAGE_SIZE);
529  for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
530  if(buf[i] != 0) {
531  if(page == 0 && i < sizeof(hdr)) {
532  return 0;
533  }
534  return 1 + i + (page * COFFEE_PAGE_SIZE) - sizeof(hdr);
535  }
536  }
537  }
538 
539  /* All bytes are writable. */
540  return 0;
541 }
542 /*---------------------------------------------------------------------------*/
543 static coffee_page_t
544 find_contiguous_pages(coffee_page_t amount)
545 {
546  coffee_page_t page, start;
547  struct file_header hdr;
548 
549  start = INVALID_PAGE;
550  for(page = next_free; page < COFFEE_PAGE_COUNT;) {
551  read_header(&hdr, page);
552  if(HDR_FREE(hdr)) {
553  if(start == INVALID_PAGE) {
554  start = page;
555  if(start + amount >= COFFEE_PAGE_COUNT) {
556  /* We can stop immediately if the remaining pages are not enough. */
557  break;
558  }
559  }
560 
561  /* All remaining pages in this sector are free --
562  jump to the next sector. */
563  page = next_file(page, &hdr);
564 
565  if(start + amount <= page) {
566  if(start == next_free) {
567  next_free = start + amount;
568  }
569  return start;
570  }
571  } else {
572  start = INVALID_PAGE;
573  page = next_file(page, &hdr);
574  }
575  }
576  return INVALID_PAGE;
577 }
578 /*---------------------------------------------------------------------------*/
579 static int
580 remove_by_page(coffee_page_t page, int remove_log, int close_fds,
581  int gc_allowed)
582 {
583  struct file_header hdr;
584  int i;
585 
586  read_header(&hdr, page);
587  if(!HDR_ACTIVE(hdr)) {
588  return -1;
589  }
590 
591 #if COFFEE_MICRO_LOGS
592  if(remove_log && HDR_MODIFIED(hdr)) {
593  if(remove_by_page(hdr.log_page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
594  return -1;
595  }
596  }
597 #endif /* COFFEE_MICRO_LOGS */
598 
599  hdr.flags |= HDR_FLAG_OBSOLETE;
600  write_header(&hdr, page);
601 
602  gc_wait = 0;
603 
604  /* Close all file descriptors that reference the removed file. */
605  if(close_fds) {
606  for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
607  if(coffee_fd_set[i].file != NULL && coffee_fd_set[i].file->page == page) {
608  coffee_fd_set[i].flags = COFFEE_FD_FREE;
609  }
610  }
611  }
612 
613  for(i = 0; i < COFFEE_MAX_OPEN_FILES; i++) {
614  if(coffee_files[i].page == page) {
615  coffee_files[i].page = INVALID_PAGE;
616  coffee_files[i].references = 0;
617  coffee_files[i].max_pages = 0;
618  }
619  }
620 
621 #if !COFFEE_EXTENDED_WEAR_LEVELLING
622  if(gc_allowed) {
623  collect_garbage(GC_RELUCTANT);
624  }
625 #endif
626 
627  return 0;
628 }
629 /*---------------------------------------------------------------------------*/
630 static coffee_page_t
631 page_count(cfs_offset_t size)
632 {
633  return (size + sizeof(struct file_header) + COFFEE_PAGE_SIZE - 1) /
635 }
636 /*---------------------------------------------------------------------------*/
637 static struct file *
638 reserve(const char *name, coffee_page_t pages,
639  int allow_duplicates, unsigned flags)
640 {
641  struct file_header hdr;
642  coffee_page_t page;
643  struct file *file;
644 
645  if(!allow_duplicates && find_file(name) != NULL) {
646  return NULL;
647  }
648 
649  page = find_contiguous_pages(pages);
650  if(page == INVALID_PAGE) {
651  if(gc_wait) {
652  return NULL;
653  }
654  collect_garbage(GC_GREEDY);
655  page = find_contiguous_pages(pages);
656  if(page == INVALID_PAGE) {
657  gc_wait = 1;
658  return NULL;
659  }
660  }
661 
662  memset(&hdr, 0, sizeof(hdr));
663  strncpy(hdr.name, name, sizeof(hdr.name) - 1);
664  hdr.max_pages = pages;
665  hdr.flags = HDR_FLAG_ALLOCATED | flags;
666  write_header(&hdr, page);
667 
668  PRINTF("Coffee: Reserved %u pages starting from %u for file %s\n",
669  (unsigned)pages, (unsigned)page, name);
670 
671  file = load_file(page, &hdr);
672  if(file != NULL) {
673  file->end = 0;
674  }
675 
676  return file;
677 }
678 /*---------------------------------------------------------------------------*/
679 #if COFFEE_MICRO_LOGS
680 static void
681 adjust_log_config(struct file_header *hdr,
682  uint16_t *log_record_size, uint16_t *log_records)
683 {
684  *log_record_size = hdr->log_record_size == 0 ?
685  COFFEE_PAGE_SIZE : hdr->log_record_size;
686  *log_records = hdr->log_records == 0 ?
687  COFFEE_LOG_SIZE / *log_record_size : hdr->log_records;
688 }
689 #endif /* COFFEE_MICRO_LOGS */
690 /*---------------------------------------------------------------------------*/
691 #if COFFEE_MICRO_LOGS
692 static uint16_t
693 modify_log_buffer(uint16_t log_record_size,
694  cfs_offset_t *offset, uint16_t *size)
695 {
696  uint16_t region;
697 
698  region = *offset / log_record_size;
699  *offset %= log_record_size;
700 
701  if(*size > log_record_size - *offset) {
702  *size = log_record_size - *offset;
703  }
704 
705  return region;
706 }
707 #endif /* COFFEE_MICRO_LOGS */
708 /*---------------------------------------------------------------------------*/
709 #if COFFEE_MICRO_LOGS
710 static int
711 get_record_index(coffee_page_t log_page, uint16_t search_records,
712  uint16_t region)
713 {
714  cfs_offset_t base;
715  uint16_t processed;
716  uint16_t batch_size;
717  int16_t match_index, i;
718 
719  base = absolute_offset(log_page, sizeof(uint16_t) * search_records);
720  batch_size = search_records > COFFEE_LOG_TABLE_LIMIT ?
721  COFFEE_LOG_TABLE_LIMIT : search_records;
722  processed = 0;
723  match_index = -1;
724 
725  {
726  uint16_t indices[batch_size];
727 
728  while(processed < search_records && match_index < 0) {
729  if(batch_size + processed > search_records) {
730  batch_size = search_records - processed;
731  }
732 
733  base -= batch_size * sizeof(indices[0]);
734  COFFEE_READ(&indices, sizeof(indices[0]) * batch_size, base);
735 
736  for(i = batch_size - 1; i >= 0; i--) {
737  if(indices[i] - 1 == region) {
738  match_index = search_records - processed - (batch_size - i);
739  break;
740  }
741  }
742 
743  processed += batch_size;
744  }
745  }
746 
747  return match_index;
748 }
749 #endif /* COFFEE_MICRO_LOGS */
750 /*---------------------------------------------------------------------------*/
751 #if COFFEE_MICRO_LOGS
752 static int
753 read_log_page(struct file_header *hdr, int16_t record_count,
754  struct log_param *lp)
755 {
756  uint16_t region;
757  int16_t match_index;
758  uint16_t log_record_size;
759  uint16_t log_records;
760  cfs_offset_t base;
761  uint16_t search_records;
762 
763  adjust_log_config(hdr, &log_record_size, &log_records);
764  region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
765 
766  search_records = record_count < 0 ? log_records : record_count;
767  match_index = get_record_index(hdr->log_page, search_records, region);
768  if(match_index < 0) {
769  return -1;
770  }
771 
772  base = absolute_offset(hdr->log_page, log_records * sizeof(region));
773  base += (cfs_offset_t)match_index * log_record_size;
774  base += lp->offset;
775  COFFEE_READ(lp->buf, lp->size, base);
776 
777  return lp->size;
778 }
779 #endif /* COFFEE_MICRO_LOGS */
780 /*---------------------------------------------------------------------------*/
781 #if COFFEE_MICRO_LOGS
782 static coffee_page_t
783 create_log(struct file *file, struct file_header *hdr)
784 {
785  uint16_t log_record_size, log_records;
786  cfs_offset_t size;
787  struct file *log_file;
788 
789  adjust_log_config(hdr, &log_record_size, &log_records);
790 
791  /* Log index size + log data size. */
792  size = log_records * (sizeof(uint16_t) + log_record_size);
793 
794  log_file = reserve(hdr->name, page_count(size), 1, HDR_FLAG_LOG);
795  if(log_file == NULL) {
796  return INVALID_PAGE;
797  }
798 
799  hdr->flags |= HDR_FLAG_MODIFIED;
800  hdr->log_page = log_file->page;
801  write_header(hdr, file->page);
802 
803  file->flags |= COFFEE_FILE_MODIFIED;
804  return log_file->page;
805 }
806 #endif /* COFFEE_MICRO_LOGS */
807 /*---------------------------------------------------------------------------*/
808 static int
809 merge_log(coffee_page_t file_page, int extend)
810 {
811  struct file_header hdr, hdr2;
812  int fd, n;
813  cfs_offset_t offset;
814  coffee_page_t max_pages;
815  struct file *new_file;
816  int i;
817 
818  read_header(&hdr, file_page);
819 
820  fd = cfs_open(hdr.name, CFS_READ);
821  if(fd < 0) {
822  return -1;
823  }
824 
825  /*
826  * The reservation function adds extra space for the header, which has
827  * already been accounted for in the previous reservation.
828  */
829  max_pages = hdr.max_pages << extend;
830  new_file = reserve(hdr.name, max_pages, 1, 0);
831  if(new_file == NULL) {
832  cfs_close(fd);
833  return -1;
834  }
835 
836  offset = 0;
837  do {
838 #if COFFEE_MICRO_LOGS
839  char buf[hdr.log_record_size == 0 ? COFFEE_PAGE_SIZE : hdr.log_record_size];
840 #else
841  char buf[COFFEE_PAGE_SIZE];
842 #endif /* COFFEE_MICRO_LOGS */
843  n = cfs_read(fd, buf, sizeof(buf));
844  if(n < 0) {
845  remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, ALLOW_GC);
846  cfs_close(fd);
847  return -1;
848  } else if(n > 0) {
849  COFFEE_WRITE(buf, n, absolute_offset(new_file->page, offset));
850  offset += n;
851  }
852  } while(n != 0);
853 
854  for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
855  if(coffee_fd_set[i].flags != COFFEE_FD_FREE &&
856  coffee_fd_set[i].file->page == file_page) {
857  coffee_fd_set[i].file = new_file;
858  new_file->references++;
859  }
860  }
861 
862  if(remove_by_page(file_page, REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
863  remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC);
864  cfs_close(fd);
865  return -1;
866  }
867 
868  /* Copy the log configuration. */
869  read_header(&hdr2, new_file->page);
870 #if COFFEE_MICRO_LOGS
871  hdr2.log_record_size = hdr.log_record_size;
872  hdr2.log_records = hdr.log_records;
873 #endif /* COFFEE_MICRO_LOGS */
874  write_header(&hdr2, new_file->page);
875 
876  new_file->flags &= ~COFFEE_FILE_MODIFIED;
877  new_file->end = offset;
878 
879  cfs_close(fd);
880 
881  return 0;
882 }
883 /*---------------------------------------------------------------------------*/
884 #if COFFEE_MICRO_LOGS
885 static int
886 find_next_record(struct file *file, coffee_page_t log_page,
887  int log_records)
888 {
889  int log_record, preferred_batch_size;
890 
891  if(file->record_count >= 0) {
892  return file->record_count;
893  }
894 
895  preferred_batch_size = log_records > COFFEE_LOG_TABLE_LIMIT ?
896  COFFEE_LOG_TABLE_LIMIT : log_records;
897  {
898  /* The next log record is unknown at this point; search for it. */
899  uint16_t indices[preferred_batch_size];
900  uint16_t processed;
901  uint16_t batch_size;
902 
903  log_record = log_records;
904  for(processed = 0; processed < log_records; processed += batch_size) {
905  batch_size = log_records - processed >= preferred_batch_size ?
906  preferred_batch_size : log_records - processed;
907 
908  COFFEE_READ(&indices, batch_size * sizeof(indices[0]),
909  absolute_offset(log_page, processed * sizeof(indices[0])));
910  for(log_record = 0; log_record < batch_size; log_record++) {
911  if(indices[log_record] == 0) {
912  log_record += processed;
913  break;
914  }
915  }
916  }
917  }
918 
919  return log_record;
920 }
921 #endif /* COFFEE_MICRO_LOGS */
922 /*---------------------------------------------------------------------------*/
923 #if COFFEE_MICRO_LOGS
924 static int
925 write_log_page(struct file *file, struct log_param *lp)
926 {
927  struct file_header hdr;
928  uint16_t region;
929  coffee_page_t log_page;
930  int16_t log_record;
931  uint16_t log_record_size;
932  uint16_t log_records;
933  cfs_offset_t offset;
934  struct log_param lp_out;
935 
936  read_header(&hdr, file->page);
937 
938  adjust_log_config(&hdr, &log_record_size, &log_records);
939  region = modify_log_buffer(log_record_size, &lp->offset, &lp->size);
940 
941  log_page = 0;
942  if(HDR_MODIFIED(hdr)) {
943  /* A log structure has already been created. */
944  log_page = hdr.log_page;
945  log_record = find_next_record(file, log_page, log_records);
946  if(log_record >= log_records) {
947  /* The log is full; merge the log. */
948  PRINTF("Coffee: Merging the file %s with its log\n", hdr.name);
949  return merge_log(file->page, 0);
950  }
951  } else {
952  /* Create a log structure. */
953  log_page = create_log(file, &hdr);
954  if(log_page == INVALID_PAGE) {
955  return -1;
956  }
957  PRINTF("Coffee: Created a log structure for file %s at page %u\n",
958  hdr.name, (unsigned)log_page);
959  hdr.log_page = log_page;
960  log_record = 0;
961  }
962 
963  {
964  char copy_buf[log_record_size];
965 
966  lp_out.offset = offset = region * log_record_size;
967  lp_out.buf = copy_buf;
968  lp_out.size = log_record_size;
969 
970  if((lp->offset > 0 || lp->size != log_record_size) &&
971  read_log_page(&hdr, log_record, &lp_out) < 0) {
972  COFFEE_READ(copy_buf, sizeof(copy_buf),
973  absolute_offset(file->page, offset));
974  }
975 
976  memcpy(&copy_buf[lp->offset], lp->buf, lp->size);
977 
978  /*
979  * Write the region number in the region index table.
980  * The region number is incremented to avoid values of zero.
981  */
982  offset = absolute_offset(log_page, 0);
983  ++region;
984  COFFEE_WRITE(&region, sizeof(region),
985  offset + log_record * sizeof(region));
986 
987  offset += log_records * sizeof(region);
988  COFFEE_WRITE(copy_buf, sizeof(copy_buf),
989  offset + log_record * log_record_size);
990  file->record_count = log_record + 1;
991  }
992 
993  return lp->size;
994 }
995 #endif /* COFFEE_MICRO_LOGS */
996 /*---------------------------------------------------------------------------*/
997 static int
998 get_available_fd(void)
999 {
1000  int i;
1001 
1002  for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
1003  if(coffee_fd_set[i].flags == COFFEE_FD_FREE) {
1004  return i;
1005  }
1006  }
1007  return -1;
1008 }
1009 /*---------------------------------------------------------------------------*/
1010 int
1011 cfs_open(const char *name, int flags)
1012 {
1013  int fd;
1014  struct file_desc *fdp;
1015 
1016  fd = get_available_fd();
1017  if(fd < 0) {
1018  PRINTF("Coffee: Failed to allocate a new file descriptor!\n");
1019  return -1;
1020  }
1021 
1022  fdp = &coffee_fd_set[fd];
1023  fdp->flags = 0;
1024 
1025  fdp->file = find_file(name);
1026  if(fdp->file == NULL) {
1027  if((flags & (CFS_READ | CFS_WRITE)) == CFS_READ) {
1028  return -1;
1029  }
1030  fdp->file = reserve(name, page_count(COFFEE_DYN_SIZE), 1, 0);
1031  if(fdp->file == NULL) {
1032  return -1;
1033  }
1034  fdp->file->end = 0;
1035  } else if(fdp->file->end == UNKNOWN_OFFSET) {
1036  fdp->file->end = file_end(fdp->file->page);
1037  }
1038 
1039  fdp->flags |= flags;
1040  fdp->offset = flags & CFS_APPEND ? fdp->file->end : 0;
1041  fdp->file->references++;
1042 
1043  return fd;
1044 }
1045 /*---------------------------------------------------------------------------*/
1046 void
1047 cfs_close(int fd)
1048 {
1049  if(FD_VALID(fd)) {
1050  coffee_fd_set[fd].flags = COFFEE_FD_FREE;
1051  coffee_fd_set[fd].file->references--;
1052  coffee_fd_set[fd].file = NULL;
1053  }
1054 }
1055 /*---------------------------------------------------------------------------*/
1056 cfs_offset_t
1057 cfs_seek(int fd, cfs_offset_t offset, int whence)
1058 {
1059  struct file_desc *fdp;
1060  cfs_offset_t new_offset;
1061 
1062  if(!FD_VALID(fd)) {
1063  return -1;
1064  }
1065  fdp = &coffee_fd_set[fd];
1066 
1067  if(whence == CFS_SEEK_SET) {
1068  new_offset = offset;
1069  } else if(whence == CFS_SEEK_END) {
1070  new_offset = fdp->file->end + offset;
1071  } else if(whence == CFS_SEEK_CUR) {
1072  new_offset = fdp->offset + offset;
1073  } else {
1074  return (cfs_offset_t)-1;
1075  }
1076 
1077  if(new_offset < 0 || new_offset > fdp->file->max_pages * COFFEE_PAGE_SIZE) {
1078  return -1;
1079  }
1080 
1081  if(fdp->file->end < new_offset) {
1082  if(FD_WRITABLE(fd)) {
1083  fdp->file->end = new_offset;
1084  } else {
1085  /* Disallow seeking past the end of the file for read only FDs */
1086  return (cfs_offset_t)-1;
1087  }
1088  }
1089 
1090  return fdp->offset = new_offset;
1091 }
1092 /*---------------------------------------------------------------------------*/
1093 int
1094 cfs_remove(const char *name)
1095 {
1096  struct file *file;
1097 
1098  /*
1099  * Coffee removes files by marking them as obsolete. The space
1100  * is not guaranteed to be reclaimed immediately, but must be
1101  * sweeped by the garbage collector. The garbage collector is
1102  * called once a file reservation request cannot be granted.
1103  */
1104  file = find_file(name);
1105  if(file == NULL) {
1106  return -1;
1107  }
1108 
1109  return remove_by_page(file->page, REMOVE_LOG, CLOSE_FDS, ALLOW_GC);
1110 }
1111 /*---------------------------------------------------------------------------*/
1112 int
1113 cfs_read(int fd, void *buf, unsigned size)
1114 {
1115  struct file_desc *fdp;
1116  struct file *file;
1117 #if COFFEE_MICRO_LOGS
1118  struct file_header hdr;
1119  struct log_param lp;
1120  unsigned bytes_left;
1121  int r;
1122 #endif
1123 
1124  if(!(FD_VALID(fd) && FD_READABLE(fd))) {
1125  return -1;
1126  }
1127 
1128  fdp = &coffee_fd_set[fd];
1129  file = fdp->file;
1130 
1131 #if COFFEE_IO_SEMANTICS
1132  if(fdp->io_flags & CFS_COFFEE_IO_ENSURE_READ_LENGTH) {
1133  while(fdp->offset + size > file->end) {
1134  ((char*)buf)[--size] = 0;
1135  }
1136  } else {
1137 #endif
1138  if(fdp->offset + size > file->end) {
1139  size = file->end - fdp->offset;
1140  }
1141 #if COFFEE_IO_SEMANTICS
1142  }
1143 #endif
1144 
1145  /* If the file is not modified, read directly from the file extent. */
1146  if(!FILE_MODIFIED(file)) {
1147  COFFEE_READ(buf, size, absolute_offset(file->page, fdp->offset));
1148  fdp->offset += size;
1149  return size;
1150  }
1151 
1152 #if COFFEE_MICRO_LOGS
1153  read_header(&hdr, file->page);
1154 
1155  /*
1156  * Copy the contents of the most recent log record. If there is
1157  * no log record for the file area to read from, we simply read
1158  * from the original file extent.
1159  */
1160  for(bytes_left = size; bytes_left > 0; bytes_left -= r) {
1161  lp.offset = fdp->offset;
1162  lp.buf = buf;
1163  lp.size = bytes_left;
1164  r = read_log_page(&hdr, file->record_count, &lp);
1165 
1166  /* Read from the original file if we cannot find the data in the log. */
1167  if(r < 0) {
1168  COFFEE_READ(buf, lp.size, absolute_offset(file->page, fdp->offset));
1169  r = lp.size;
1170  }
1171  fdp->offset += r;
1172  buf = (char *)buf + r;
1173  }
1174 #endif /* COFFEE_MICRO_LOGS */
1175 
1176  return size;
1177 }
1178 /*---------------------------------------------------------------------------*/
1179 int
1180 cfs_write(int fd, const void *buf, unsigned size)
1181 {
1182  struct file_desc *fdp;
1183  struct file *file;
1184 #if COFFEE_MICRO_LOGS
1185  int i;
1186  struct log_param lp;
1187  cfs_offset_t bytes_left;
1188  int8_t need_dummy_write;
1189  const char dummy[1] = { 0xff };
1190 #endif
1191 
1192  if(!(FD_VALID(fd) && FD_WRITABLE(fd))) {
1193  return -1;
1194  }
1195 
1196  fdp = &coffee_fd_set[fd];
1197  file = fdp->file;
1198 
1199  /* Attempt to extend the file if we try to write past the end. */
1200 #if COFFEE_IO_SEMANTICS
1201  if(!(fdp->io_flags & CFS_COFFEE_IO_FIRM_SIZE)) {
1202 #endif
1203  while(size + fdp->offset + sizeof(struct file_header) >
1204  (file->max_pages * COFFEE_PAGE_SIZE)) {
1205  if(merge_log(file->page, 1) < 0) {
1206  return -1;
1207  }
1208  file = fdp->file;
1209  PRINTF("Extended the file at page %u\n", (unsigned)file->page);
1210  }
1211 #if COFFEE_IO_SEMANTICS
1212 }
1213 #endif
1214 
1215 #if COFFEE_MICRO_LOGS
1216 #if COFFEE_IO_SEMANTICS
1217  if(!(fdp->io_flags & CFS_COFFEE_IO_FLASH_AWARE) &&
1218  (FILE_MODIFIED(file) || fdp->offset < file->end)) {
1219 #else
1220  if(FILE_MODIFIED(file) || fdp->offset < file->end) {
1221 #endif
1222  need_dummy_write = 0;
1223  for(bytes_left = size; bytes_left > 0;) {
1224  lp.offset = fdp->offset;
1225  lp.buf = (void *)buf;
1226  lp.size = bytes_left;
1227  i = write_log_page(file, &lp);
1228  if(i < 0) {
1229  /* Return -1 if we wrote nothing because the log write failed. */
1230  if(size == bytes_left) {
1231  return -1;
1232  }
1233  break;
1234  } else if(i == 0) {
1235  /* The file was merged with the log. */
1236  file = fdp->file;
1237  } else {
1238  /* A log record was written. */
1239  bytes_left -= i;
1240  fdp->offset += i;
1241  buf = (char *)buf + i;
1242 
1243  /* Update the file end for a potential log merge that might
1244  occur while writing log records. */
1245  if(fdp->offset > file->end) {
1246  file->end = fdp->offset;
1247  need_dummy_write = 1;
1248  }
1249  }
1250  }
1251 
1252  if(need_dummy_write) {
1253  /*
1254  * A log record has been written at an offset beyond the original
1255  * extent's end. Consequently, we need to write a dummy value at the
1256  * corresponding end offset in the original extent to ensure that
1257  * the correct file size is calculated when opening the file again.
1258  */
1259  COFFEE_WRITE(dummy, 1, absolute_offset(file->page, fdp->offset - 1));
1260  }
1261  } else {
1262 #endif /* COFFEE_MICRO_LOGS */
1263 #if COFFEE_APPEND_ONLY
1264  if(fdp->offset < file->end) {
1265  return -1;
1266  }
1267 #endif /* COFFEE_APPEND_ONLY */
1268 
1269  COFFEE_WRITE(buf, size, absolute_offset(file->page, fdp->offset));
1270  fdp->offset += size;
1271 #if COFFEE_MICRO_LOGS
1272 }
1273 #endif /* COFFEE_MICRO_LOGS */
1274 
1275  if(fdp->offset > file->end) {
1276  file->end = fdp->offset;
1277  }
1278 
1279  return size;
1280 }
1281 /*---------------------------------------------------------------------------*/
1282 int
1283 cfs_opendir(struct cfs_dir *dir, const char *name)
1284 {
1285  /*
1286  * Coffee is only guaranteed to support the directory names "/" and ".",
1287  * but it does not enforce this currently.
1288  */
1289  memset(dir->dummy_space, 0, sizeof(coffee_page_t));
1290  return 0;
1291 }
1292 /*---------------------------------------------------------------------------*/
1293 int
1294 cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
1295 {
1296  struct file_header hdr;
1297  coffee_page_t page;
1298  coffee_page_t next_page;
1299 
1300  memcpy(&page, dir->dummy_space, sizeof(coffee_page_t));
1301 
1302  while(page < COFFEE_PAGE_COUNT) {
1303  read_header(&hdr, page);
1304  if(HDR_ACTIVE(hdr) && !HDR_LOG(hdr)) {
1305  memcpy(record->name, hdr.name, sizeof(record->name));
1306  record->name[sizeof(record->name) - 1] = '\0';
1307  record->size = file_end(page);
1308 
1309  next_page = next_file(page, &hdr);
1310  memcpy(dir->dummy_space, &next_page, sizeof(coffee_page_t));
1311  return 0;
1312  }
1313  page = next_file(page, &hdr);
1314  }
1315 
1316  return -1;
1317 }
1318 /*---------------------------------------------------------------------------*/
1319 void
1320 cfs_closedir(struct cfs_dir *dir)
1321 {
1322  return;
1323 }
1324 /*---------------------------------------------------------------------------*/
1325 int
1326 cfs_coffee_reserve(const char *name, cfs_offset_t size)
1327 {
1328  return reserve(name, page_count(size), 0, 0) == NULL ? -1 : 0;
1329 }
1330 /*---------------------------------------------------------------------------*/
1331 int
1332 cfs_coffee_configure_log(const char *filename, unsigned log_size,
1333  unsigned log_record_size)
1334 {
1335 #if COFFEE_MICRO_LOGS
1336  struct file *file;
1337  struct file_header hdr;
1338 
1339  if(log_record_size == 0 || log_record_size > COFFEE_PAGE_SIZE ||
1340  log_size < log_record_size) {
1341  return -1;
1342  }
1343 
1344  file = find_file(filename);
1345  if(file == NULL) {
1346  return -1;
1347  }
1348 
1349  read_header(&hdr, file->page);
1350  if(HDR_MODIFIED(hdr)) {
1351  /* Too late to customize the log. */
1352  return -1;
1353  }
1354 
1355  hdr.log_records = log_size / log_record_size;
1356  hdr.log_record_size = log_record_size;
1357  write_header(&hdr, file->page);
1358 #endif /* COFFEE_MICRO_LOGS */
1359 
1360  return 0;
1361 }
1362 /*---------------------------------------------------------------------------*/
1363 #if COFFEE_IO_SEMANTICS
1364 int
1365 cfs_coffee_set_io_semantics(int fd, unsigned flags)
1366 {
1367  if(!FD_VALID(fd)) {
1368  return -1;
1369  }
1370 
1371  coffee_fd_set[fd].io_flags |= flags;
1372 
1373  return 0;
1374 }
1375 #endif
1376 /*---------------------------------------------------------------------------*/
1377 int
1379 {
1380  coffee_page_t i;
1381 
1382  PRINTF("Coffee: Formatting %u sectors", (unsigned)COFFEE_SECTOR_COUNT);
1383 
1384  for(i = 0; i < COFFEE_SECTOR_COUNT; i++) {
1385  COFFEE_ERASE(i);
1386  PRINTF(".");
1387  }
1388 
1389  /* Formatting invalidates the file information. */
1390  memset(&coffee_files, 0, sizeof(coffee_files));
1391  memset(&coffee_fd_set, 0, sizeof(coffee_fd_set));
1392  next_free = 0;
1393  gc_wait = 1;
1394 
1395  PRINTF(" done!\n");
1396 
1397  return 0;
1398 }
1399 /*---------------------------------------------------------------------------*/
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:1011
#define CFS_APPEND
Specify that cfs_open() should append written data to the file rather than overwriting it...
Definition: cfs.h:118
#define COFFEE_NAME_LENGTH
Maximal filename length.
#define CFS_WRITE
Specify that cfs_open() should open a file for writing.
Definition: cfs.h:104
#define CFS_COFFEE_IO_FLASH_AWARE
Instruct Coffee that the access pattern to this file is adapted to flash I/O semantics by design...
Definition: cfs-coffee.h:53
static void start(void)
Start measurement.
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1283
int16_t coffee_page_t
Page.
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1047
#define CFS_SEEK_SET
Specify that cfs_seek() should compute the offset from the beginning of the file. ...
Definition: cfs.h:127
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define COFFEE_LOG_SIZE
Default micro-log size.
#define COFFEE_LOG_TABLE_LIMIT
Maximal amount of log table entries read in one batch.
#define COFFEE_MAX_OPEN_FILES
Number of file cache entries.
#define CFS_SEEK_END
Specify that cfs_seek() should compute the offset from the end of the file.
Definition: cfs.h:145
int cfs_coffee_set_io_semantics(int fd, unsigned flags)
Set the I/O semantics for accessing a file.
#define CFS_SEEK_CUR
Specify that cfs_seek() should compute the offset from the current position of the file pointer...
Definition: cfs.h:136
#define CFS_COFFEE_IO_ENSURE_READ_LENGTH
Instruct Coffee to set unused bytes in the destination buffer to zero.
Definition: cfs-coffee.h:73
#define NULL
The null pointer.
int cfs_coffee_format(void)
Format the storage area assigned to Coffee.
Definition: cfs-coffee.c:1378
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1294
#define COFFEE_DYN_SIZE
Default reserved file size.
#define COFFEE_WRITE(buf, size, offset)
Write.
#define CFS_COFFEE_IO_FIRM_SIZE
Instruct Coffee not to attempt to extend the file upon a request to write past the reserved file size...
Definition: cfs-coffee.h:64
int cfs_remove(const char *name)
Remove a file.
Definition: cfs-coffee.c:1094
#define COFFEE_PAGE_SIZE
Logical page size.
Header for the Coffee file system.
int cfs_coffee_reserve(const char *name, cfs_offset_t size)
Reserve space for a file.
Definition: cfs-coffee.c:1326
void cfs_closedir(struct cfs_dir *dir)
Close a directory opened with cfs_opendir().
Definition: cfs-coffee.c:1320
#define COFFEE_FD_SET_SIZE
Number of file descriptor entries.
#define COFFEE_READ(buf, size, offset)
Read.
int cfs_coffee_configure_log(const char *filename, unsigned log_size, unsigned log_record_size)
Configure the on-demand log file.
Definition: cfs-coffee.c:1332
cfs_offset_t cfs_seek(int fd, cfs_offset_t offset, int whence)
Seek to a specified position in an open file.
Definition: cfs-coffee.c:1057
CFS header file.
#define COFFEE_ERASE(sector)
Erase.