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