Rework cache flushing logic--this version should work much better.
[bluesky.git] / bluesky / bluesky-private.h
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 /* Declarations internal to the BlueSky library.  This header file should not
10  * be included by any users of the library (such as any filesystem
11  * proxy)--external users should only include bluesky.h. */
12
13 #ifndef _BLUESKY_PRIVATE_H
14 #define _BLUESKY_PRIVATE_H
15
16 #include "bluesky.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /* Target cache size levels. */
23 extern int bluesky_watermark_low_dirty;
24 extern int bluesky_watermark_medium_dirty;
25 extern int bluesky_watermark_high_dirty;
26
27 extern int bluesky_watermark_low_total;
28 extern int bluesky_watermark_medium_total;
29 extern int bluesky_watermark_high_total;
30
31 /* TODO: Make this go away entirely. */
32 BlueSkyFS *bluesky_new_fs(gchar *name);
33
34 /* Linked list update functions for LRU lists. */
35 void bluesky_list_unlink(GList *head, GList *item);
36 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode);
37 GList *bluesky_list_append(GList *head, BlueSkyInode *inode);
38 BlueSkyInode *bluesky_list_head(GList *head);
39 BlueSkyInode *bluesky_list_tail(GList *head);
40
41 /* Serialization and deserialization of filesystem data for storing to
42  * persistent storage. */
43 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs);
44 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf);
45 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode);
46 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf);
47
48 /* Storage layer.  Requests can be performed asynchronously, so these objects
49  * help keep track of operations in progress. */
50 typedef enum {
51     STORE_OP_NONE,
52     STORE_OP_GET,
53     STORE_OP_PUT,
54     STORE_OP_DELETE,
55     STORE_OP_BARRIER,       // Waits for other selected operations to complete
56 } BlueSkyStoreOp;
57
58 typedef enum {
59     ASYNC_NEW,              // Operation not yet submitted to storage layer
60     ASYNC_PENDING,          // Submitted to storage layer
61     ASYNC_RUNNING,          // Operation is in progress
62     ASYNC_COMPLETE,         // Operation finished, results available
63 } BlueSkyAsyncStatus;
64
65 struct BlueSkyNotifierList;
66 typedef struct _BlueSkyStoreAsync BlueSkyStoreAsync;
67 struct _BlueSkyStoreAsync {
68     BlueSkyStore *store;
69
70     GMutex *lock;
71     GCond *completion_cond;     /* Used to wait for operation to complete. */
72
73     gint refcount;              /* Reference count for destruction. */
74
75     BlueSkyAsyncStatus status;
76
77     BlueSkyStoreOp op;
78     gchar *key;                 /* Key to read/write */
79     BlueSkyRCStr *data;         /* Data read/to write */
80
81     int result;                 /* Result code; 0 for success. */
82     struct BlueSkyNotifierList *notifiers;
83     gint notifier_count;
84
85     /* The barrier waiting on this operation.  Support for more than one
86      * barrier for a single async is not well-supported and should be avoided
87      * if possible. */
88     BlueSkyStoreAsync *barrier;
89
90     bluesky_time_hires start_time;  /* Time operation was submitted. */
91     bluesky_time_hires exec_time;   /* Time processing started on operation. */
92
93     gpointer store_private;     /* For use by the storage implementation */
94 };
95
96 /* Support for notification lists.  These are lists of one-shot functions which
97  * can be called when certain events--primarily, competed storage
98  * events--occur.  Multiple notifiers can be added, but no particular order is
99  * guaranteed for the notification functions to be called. */
100 struct BlueSkyNotifierList {
101     struct BlueSkyNotifierList *next;
102     GFunc func;
103     BlueSkyStoreAsync *async;
104     gpointer user_data;     // Passed to the function when called
105 };
106
107 /* The abstraction layer for storage, allowing multiple implementations. */
108 typedef struct {
109     /* Create a new store instance and return a handle to it. */
110     gpointer (*create)(const gchar *path);
111
112     /* Clean up any resources used by this store. */
113     void (*destroy)(gpointer store);
114
115     /* Submit an operation (get/put/delete) to the storage layer to be
116      * performed asynchronously. */
117     void (*submit)(gpointer store, BlueSkyStoreAsync *async);
118
119     /* Clean up any implementation-private data in a BlueSkyStoreAsync. */
120     void (*cleanup)(gpointer store, BlueSkyStoreAsync *async);
121 } BlueSkyStoreImplementation;
122
123 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
124                             const gchar *name);
125
126 BlueSkyStoreAsync *bluesky_store_async_new(BlueSkyStore *store);
127 gpointer bluesky_store_async_get_handle(BlueSkyStoreAsync *async);
128 void bluesky_store_async_ref(BlueSkyStoreAsync *async);
129 void bluesky_store_async_unref(BlueSkyStoreAsync *async);
130 void bluesky_store_async_wait(BlueSkyStoreAsync *async);
131 void bluesky_store_async_add_notifier(BlueSkyStoreAsync *async,
132                                       GFunc func, gpointer user_data);
133 void bluesky_store_async_mark_complete(BlueSkyStoreAsync *async);
134 void bluesky_store_async_submit(BlueSkyStoreAsync *async);
135 void bluesky_store_sync(BlueSkyStore *store);
136
137 void bluesky_store_add_barrier(BlueSkyStoreAsync *barrier,
138                                BlueSkyStoreAsync *async);
139
140 void bluesky_inode_start_sync(BlueSkyInode *inode, BlueSkyStoreAsync *barrier);
141
142 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
143 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
144                          BlueSkyStoreAsync *barrier);
145 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
146                          BlueSkyStoreAsync *barrier);
147 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier);
148 void bluesky_file_drop_cached(BlueSkyInode *inode);
149
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #endif