clsync
cluster.h
Go to the documentation of this file.
1 /*
2  clsync - file tree sync utility based on inotify
3 
4  Copyright (C) 2013 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifdef CLUSTER_SUPPORT
21 
22 #include <glib.h>
23 
24 // Macros for reading messages
25 
26 #define CLUSTER_RESTDATALEN(clustercmd_p, data_type) \
27  __extension__((clustercmd_p)->h.data_len - sizeof(data_type) + sizeof(char *))
28 
29 #define CLUSTER_LOOP_EXPECTCMD(clustercmd_p, clustercmd_id, ret) {\
30  /* Exit if error */ \
31  if(ret == -1) { \
32  error("CLUSTER_LOOP_EXPECTCMD()"); \
33  return errno; \
34  }\
35  \
36  /* Is that the command we are expecting? Skipping if not. */\
37  if(clustercmd_p->h.cmd_id != clustercmd_id)\
38  continue;\
39  }
40 
41 // Macros for writing messages
42 
43 // calculated required memory for clustercmd packet
44 #define CLUSTER_REQMEM(data_type, restdata_len) \
45  __extension__(sizeof(clustercmdhdr_t) + sizeof(data_type) + (restdata_len) + 2)
46 
47 // calculated required memory for clustercmd packet with padding
48 #define CLUSTER_REQMEM_PADDED(data_type, restdata_len) \
49  CLUSTER_PAD(CLUSTER_REQMEM(data_type, restdata_len))
50 
51 // allocated memory for clustercmd packet with padding
52 #define CLUSTER_ALLOC(data_type, restdata_len, alloc_funct)\
53  (clustercmd_t *)memset((alloc_funct)(CLUSTER_REQMEM_PADDED(data_type, restdata_len)), 0, CLUSTER_REQMEM_PADDED(data_type, restdata_len))
54 
55 // allocated memory for clustercmd packet with padding with alloca()
56 #define CLUSTER_ALLOCA(data_type, restdata_len)\
57  CLUSTER_ALLOC(data_type, restdata_len, alloca)
58 
59 // allocated memory for clustercmd packet with padding with xmalloc()
60 #define CLUSTER_MALLOC(data_type, restdata_len)\
61  CLUSTER_ALLOC(data_type, restdata_len, xmalloc)
62 
63 // Common macros
64 
65 #define CLUSTER_PAD(size) ((((size) + 3) >> 2) << 2)
66 
67 #define CLUSTERCMD_SIZE(clustercmd_p) (sizeof(clustercmdhdr_t) + (*(clustercmd_p)).h.data_len)
68 #define CLUSTERCMD_SIZE_PADDED(clustercmd_p) (sizeof(clustercmdhdr_t) + CLUSTER_PAD((*(clustercmd_p)).h.data_len))
69 
70 // Types
71 
72 enum adler32_calc {
73  ADLER32_CALC_NONE = 0x00,
74  ADLER32_CALC_HEADER = 0x01,
75  ADLER32_CALC_DATA = 0x02,
76  ADLER32_CALC_ALL = 0x03,
77 };
78 typedef enum adler32_calc adler32_calc_t;
79 
80 enum cluster_read_flags {
81  CLREAD_NONE = 0x00,
82  CLREAD_ALL = 0xff
83 };
84 typedef enum cluster_read_flags cluster_read_flags_t;
85 
86 enum nodestatus {
87  NODESTATUS_DOESNTEXIST = 0,
88  NODESTATUS_OFFLINE,
89  NODESTATUS_SEEMSONLINE,
90  NODESTATUS_ONLINE,
91  NODESTATUS_BANNED
92 };
93 typedef enum nodestatus nodestatus_t;
94 
95 enum nodeid {
96  NODEID_NOID = MAXNODES
97 };
98 typedef enum nodeid nodeid_t;
99 
100 struct packets_stats {
101  uint64_t tot;
102  uint64_t rej;
103 };
104 typedef struct packets_stats packets_stats_t;
105 
106 struct nodeinfo {
107  uint8_t id;
108  uint8_t num;
109  nodestatus_t status;
110  uint32_t updatets;
111  GHashTable *modtime_ht;
112  GHashTable *serial2queuedpacket_ht;
113  packets_stats_t packets_in;
114  packets_stats_t packets_out;
115  uint32_t last_serial;
116  char *node_name;
117 };
118 typedef struct nodeinfo nodeinfo_t;
119 
120 enum clustercmd_id {
121  CLUSTERCMDID_PING = 0,
122  CLUSTERCMDID_ACK = 1,
123  CLUSTERCMDID_REG = 2,
124  CLUSTERCMDID_HELLO = 3,
125  CLUSTERCMDID_WELCOME = 4,
126  CLUSTERCMDID_DIE = 5,
127  CLUSTERCMDID_HT_EXCH = 6,
128  COUNT_CLUSTERCMDID
129 };
130 typedef enum clustercmd_id clustercmd_id_t;
131 
132 __extension__ struct clustercmd_hello {
133  char node_name[0];
134 };
135 typedef struct clustercmd_hello clustercmd_hello_t;
136 
137 #define welcome_to_node_name_len(cmd_p) ((cmd_p)->h.data_len-(((clustercmd_welcome_t *)&(cmd_p)->data)->from_node_name_len)-sizeof(clustercmd_welcome_t))
138 #define welcome_to_node_name(cmddata_p) (&cmddata_p->from_node_name[cmddata_p->from_node_name_len])
139 __extension__ struct clustercmd_welcome {
140  size_t from_node_name_len;
141  char from_node_name[0];
142 // to_node_name == my_node_name+my_node_name_len
143 };
144 typedef struct clustercmd_welcome clustercmd_welcome_t;
145 
146 __extension__ struct clustercmd_reg {
147  char node_name[0];
148 };
149 typedef struct clustercmd_reg clustercmd_reg_t;
150 
151 struct clustercmd_ack {
152  uint32_t serial;
153 };
154 typedef struct clustercmd_ack clustercmd_ack_t;
155 
156 enum reject_reason {
157  REJ_UNKNOWN = 0,
158  REJ_ADLER32MISMATCH,
159 };
160 typedef enum reject_reason reject_reason_t;
161 
162 struct clustercmd_rej {
163  uint32_t serial;
164  uint8_t reason;
165 };
166 typedef struct clustercmd_rej clustercmd_rej_t;
167 
168 __extension__ struct clustercmd_ht_exch {
169  time_t ctime;
170  size_t path_length;
171  char path[0];
172 };
173 typedef struct clustercmd_ht_exch clustercmd_ht_exch_t;
174 
175 struct clustercmdadler32 {
176  uint32_t hdr; // 32
177  uint32_t dat; // 64
178 };
179 typedef struct clustercmdadler32 clustercmdadler32_t;
180 
181 struct clustercmdhdr { // bits
182  uint8_t dst_node_id; // 8
183  uint8_t src_node_id; // 16
184  uint8_t flags; // 24 (for future compatibility)
185  uint8_t cmd_id; // 32
186  clustercmdadler32_t adler32; // 96
187  uint32_t data_len; // 128
188  uint32_t ts; // 160
189  uint32_t serial; // 192
190 };
191 typedef struct clustercmdhdr clustercmdhdr_t;
192 
193 typedef char clustercmd_die_t;
194 
195 __extension__ struct clustercmd {
196  clustercmdhdr_t h;
197  union data {
198  char p[0];
199  clustercmd_welcome_t welcome;
200  clustercmd_reg_t reg;
201  clustercmd_ack_t ack;
202  clustercmd_rej_t rej;
203  clustercmd_hello_t hello;
204  clustercmd_ht_exch_t ht_exch;
205  clustercmd_die_t die;
206  } data;
207 };
208 typedef struct clustercmd clustercmd_t;
209 
210 struct clustercmdqueuedpackethdri {
211  char dummy; // anti-warning
212 };
213 typedef struct clustercmdqueuedpackethdri clustercmdqueuedpackethdri_t;
214 
215 struct clustercmdqueuedpackethdro {
216  char ack_from[MAXNODES];
217  uint8_t ack_count;
218 };
219 typedef struct clustercmdqueuedpackethdro clustercmdqueuedpackethdro_t;
220 
221 struct clustercmdqueuedpackethdr {
222  unsigned int window_id;
223  union w {
224  clustercmdqueuedpackethdri_t i;
225  clustercmdqueuedpackethdro_t o;
226  } w;
227 };
228 typedef struct clustercmdqueuedpackethdr clustercmdqueuedpackethdr_t;
229 
230 struct clustercmdqueuedpacket {
231  clustercmdqueuedpackethdr_t h;
232  clustercmd_t cmd;
233 };
234 typedef struct clustercmdqueuedpacket clustercmdqueuedpacket_t;
235 
236 struct window_occupied_sides {
237  size_t left;
238  size_t right;
239 };
240 typedef struct window_occupied_sides window_occupied_sides_t;
241 
242 struct window {
243  unsigned int size; // Allocated cells
244  unsigned int packets_len; // Count of packets (are waiting for ACK-s)
245  unsigned int *packets_id; // Array of cells' id-s with packets
246  window_occupied_sides_t *occupied_sides; // Array of structures with coordinates in buffer of occupied space by cell ida (aka window_id)
247  size_t buf_size; // Allocated space of the buffer
248  char *buf; // Pointer to the buffer
249 };
250 typedef struct window window_t;
251 
252 typedef int ( *cluster_recvproc_funct_t ) ( clustercmd_t *clustercmd_p );
253 
254 // Externs
255 
256 extern int cluster_init ( struct ctx *ctx_p, struct indexes *indexes_p );
257 extern int cluster_deinit();
258 
259 extern int cluster_lock ( const char *fpath );
260 extern int cluster_lock_byindexes();
261 extern int cluster_unlock_all();
262 extern int cluster_capture ( const char *fpath );
263 
264 extern int cluster_modtime_update ( const char *dirpath, short int dirlevel, mode_t st_mode );
265 extern int cluster_initialsync();
266 
267 #endif
268 
ctx
Definition: ctx.h:315
indexes
Definition: indexes.h:34
adler32_calc
uint32_t adler32_calc(const unsigned char *const data, uint32_t len)
Calculated Adler32 value for char array.
Definition: calc.c:41
MAXNODES
#define MAXNODES
Definition: configuration.h:16
ctx_p
ctx_t * ctx_p
Definition: mon_kqueue.c:85