2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_protocols.h"
31 #include "plugin_datastore.h"
32 #include "datastore.h"
35 * How many messages do we queue at most per client?
37 #define MAX_PENDING 1024
40 * How long are we at most keeping "expired" content
41 * past the expiration date in the database?
43 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
48 * Our datastore plugin.
50 struct DatastorePlugin
54 * API of the transport as returned by the plugin's
55 * initialization function.
57 struct GNUNET_DATASTORE_PluginFunctions *api;
60 * Short name for the plugin (i.e. "sqlite").
65 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
70 * Environment this transport service is using
73 struct GNUNET_DATASTORE_PluginEnvironment env;
79 * Linked list of active reservations.
81 struct ReservationList
85 * This is a linked list.
87 struct ReservationList *next;
90 * Client that made the reservation.
92 struct GNUNET_SERVER_Client *client;
95 * Number of bytes (still) reserved.
100 * Number of items (still) reserved.
105 * Reservation identifier.
113 * Our datastore plugin (NULL if not available).
115 static struct DatastorePlugin *plugin;
118 * Linked list of space reservations made by clients.
120 static struct ReservationList *reservations;
123 * Bloomfilter to quickly tell if we don't have the content.
125 static struct GNUNET_CONTAINER_BloomFilter *filter;
128 * Static counter to produce reservation identifiers.
130 static int reservation_gen;
133 * How much space are we allowed to use?
135 static unsigned long long quota;
138 * How much space are we using for the cache? (space available for
139 * insertions that will be instantly reclaimed by discarding less
140 * important content --- or possibly whatever we just inserted into
143 static unsigned long long cache_size;
146 * How much space have we currently reserved?
148 static unsigned long long reserved;
151 * Identity of the task that is used to delete
154 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
159 const struct GNUNET_CONFIGURATION_Handle *cfg;
164 struct GNUNET_SCHEDULER_Handle *sched;
167 * Function called once the transmit operation has
168 * either failed or succeeded.
171 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
173 typedef void (*TransmitContinuation)(void *cls,
178 * Context for transmitting replies to clients.
180 struct TransmitCallbackContext
184 * We keep these in a doubly-linked list (for cleanup).
186 struct TransmitCallbackContext *next;
189 * We keep these in a doubly-linked list (for cleanup).
191 struct TransmitCallbackContext *prev;
194 * The message that we're asked to transmit.
196 struct GNUNET_MessageHeader *msg;
199 * Handle for the transmission request.
201 struct GNUNET_CONNECTION_TransmitHandle *th;
204 * Client that we are transmitting to.
206 struct GNUNET_SERVER_Client *client;
209 * Function to call once msg has been transmitted
210 * (or at least added to the buffer).
212 TransmitContinuation tc;
220 * GNUNET_YES if we are supposed to signal the server
221 * completion of the client's request.
228 * Head of the doubly-linked list (for cleanup).
230 static struct TransmitCallbackContext *tcc_head;
233 * Tail of the doubly-linked list (for cleanup).
235 static struct TransmitCallbackContext *tcc_tail;
239 * Task that is used to remove expired entries from
240 * the datastore. This task will schedule itself
241 * again automatically to always delete all expired
244 * @param cls not used
245 * @param tc task context
248 delete_expired (void *cls,
249 const struct GNUNET_SCHEDULER_TaskContext *tc);
253 * Iterate over the expired items stored in the datastore.
254 * Delete all expired items; once we have processed all
255 * expired items, re-schedule the "delete_expired" task.
257 * @param cls not used
258 * @param next_cls closure to pass to the "next" function.
259 * @param key key for the content
260 * @param size number of bytes in data
261 * @param data content stored
262 * @param type type of the content
263 * @param priority priority of the content
264 * @param anonymity anonymity-level for the content
265 * @param expiration expiration time for the content
266 * @param uid unique identifier for the datum;
267 * maybe 0 if no unique identifier is available
269 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
270 * (continue on call to "next", of course),
271 * GNUNET_NO to delete the item and continue (if supported)
274 expired_processor (void *cls,
276 const GNUNET_HashCode * key,
282 struct GNUNET_TIME_Absolute
286 struct GNUNET_TIME_Absolute now;
288 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
292 = GNUNET_SCHEDULER_add_delayed (sched,
296 return GNUNET_SYSERR;
298 now = GNUNET_TIME_absolute_get ();
299 if (expiration.value > now.value)
301 /* finished processing */
302 plugin->api->next_request (next_cls, GNUNET_YES);
303 return GNUNET_SYSERR;
305 plugin->api->next_request (next_cls, GNUNET_NO);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Deleting content that expired %llu ms ago\n",
309 (unsigned long long) (now.value - expiration.value));
311 GNUNET_CONTAINER_bloomfilter_remove (filter,
313 return GNUNET_NO; /* delete */
318 * Task that is used to remove expired entries from
319 * the datastore. This task will schedule itself
320 * again automatically to always delete all expired
323 * @param cls not used
324 * @param tc task context
327 delete_expired (void *cls,
328 const struct GNUNET_SCHEDULER_TaskContext *tc)
330 plugin->api->iter_ascending_expiration (plugin->api->cls,
338 * An iterator over a set of items stored in the datastore.
341 * @param next_cls closure to pass to the "next" function.
342 * @param key key for the content
343 * @param size number of bytes in data
344 * @param data content stored
345 * @param type type of the content
346 * @param priority priority of the content
347 * @param anonymity anonymity-level for the content
348 * @param expiration expiration time for the content
349 * @param uid unique identifier for the datum;
350 * maybe 0 if no unique identifier is available
352 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
353 * (continue on call to "next", of course),
354 * GNUNET_NO to delete the item and continue (if supported)
359 const GNUNET_HashCode * key,
365 struct GNUNET_TIME_Absolute
369 unsigned long long *need = cls;
374 return GNUNET_SYSERR;
376 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
379 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
380 plugin->api->next_request (next_cls,
381 (0 == *need) ? GNUNET_YES : GNUNET_NO);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
385 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
388 GNUNET_CONTAINER_bloomfilter_remove (filter,
395 * Manage available disk space by running tasks
396 * that will discard content if necessary. This
397 * function will be run whenever a request for
398 * "need" bytes of storage could only be satisfied
399 * by eating into the "cache" (and we want our cache
402 * @param need number of bytes of content that were
403 * placed into the "cache" (and hence the
404 * number of bytes that should be removed).
407 manage_space (unsigned long long need)
409 unsigned long long *n;
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Asked to free up %llu bytes of cache space\n",
416 n = GNUNET_malloc (sizeof(unsigned long long));
418 plugin->api->iter_low_priority (plugin->api->cls,
426 * Function called to notify a client about the socket
427 * begin ready to queue more data. "buf" will be
428 * NULL and "size" zero if the socket was closed for
429 * writing in the meantime.
432 * @param size number of bytes available in buf
433 * @param buf where the callee should write the message
434 * @return number of bytes written to buf
437 transmit_callback (void *cls,
438 size_t size, void *buf)
440 struct TransmitCallbackContext *tcc = cls;
444 GNUNET_CONTAINER_DLL_remove (tcc_head,
447 msize = ntohs(tcc->msg->size);
451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
452 "Transmission failed.\n");
455 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
456 if (GNUNET_YES == tcc->end)
457 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
458 GNUNET_SERVER_client_drop (tcc->client);
459 GNUNET_free (tcc->msg);
463 GNUNET_assert (size >= msize);
464 memcpy (buf, tcc->msg, msize);
466 tcc->tc (tcc->tc_cls, GNUNET_OK);
467 if (GNUNET_YES == tcc->end)
469 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Response transmitted, more pending!\n");
478 GNUNET_SERVER_client_drop (tcc->client);
479 GNUNET_free (tcc->msg);
486 * Transmit the given message to the client.
488 * @param client target of the message
489 * @param msg message to transmit, will be freed!
490 * @param tc function to call afterwards
491 * @param tc_cls closure for tc
492 * @param end is this the last response (and we should
493 * signal the server completion accodingly after
494 * transmitting this message)?
497 transmit (struct GNUNET_SERVER_Client *client,
498 struct GNUNET_MessageHeader *msg,
499 TransmitContinuation tc,
503 struct TransmitCallbackContext *tcc;
505 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
507 tcc->client = client;
509 tcc->tc_cls = tc_cls;
512 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
514 GNUNET_TIME_UNIT_FOREVER_REL,
519 if (GNUNET_YES == end)
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
523 "Disconnecting client.\n");
525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
528 tc (tc_cls, GNUNET_SYSERR);
533 GNUNET_SERVER_client_keep (client);
534 GNUNET_CONTAINER_DLL_insert (tcc_head,
541 * Transmit a status code to the client.
543 * @param client receiver of the response
544 * @param code status code
545 * @param msg optional error message (can be NULL)
548 transmit_status (struct GNUNET_SERVER_Client *client,
552 struct StatusMessage *sm;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Transmitting `%s' message with value %d and message `%s'\n",
560 msg != NULL ? msg : "(none)");
562 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
563 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
564 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
565 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
566 sm->status = htonl(code);
568 memcpy (&sm[1], msg, slen);
569 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
574 * Function called once the transmit operation has
575 * either failed or succeeded.
577 * @param next_cls closure for calling "next_request" callback
578 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
581 get_next(void *next_cls,
584 if (status != GNUNET_OK)
586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587 _("Failed to transmit an item to the client; aborting iteration.\n"));
589 plugin->api->next_request (next_cls, GNUNET_YES);
592 plugin->api->next_request (next_cls, GNUNET_NO);
597 * Function that will transmit the given datastore entry
600 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
601 * @param next_cls closure to use to ask for the next item
602 * @param key key for the content
603 * @param size number of bytes in data
604 * @param data content stored
605 * @param type type of the content
606 * @param priority priority of the content
607 * @param anonymity anonymity-level for the content
608 * @param expiration expiration time for the content
609 * @param uid unique identifier for the datum;
610 * maybe 0 if no unique identifier is available
612 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
613 * GNUNET_NO to delete the item and continue (if supported)
616 transmit_item (void *cls,
618 const GNUNET_HashCode * key,
624 struct GNUNET_TIME_Absolute
625 expiration, uint64_t uid)
627 struct GNUNET_SERVER_Client *client = cls;
628 struct GNUNET_MessageHeader *end;
629 struct DataMessage *dm;
633 /* transmit 'DATA_END' */
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Transmitting `%s' message\n",
639 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
640 end->size = htons(sizeof(struct GNUNET_MessageHeader));
641 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
642 transmit (client, end, NULL, NULL, GNUNET_YES);
643 GNUNET_SERVER_client_drop (client);
646 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
647 dm->header.size = htons(sizeof(struct DataMessage) + size);
648 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
650 dm->size = htonl(size);
651 dm->type = htonl(type);
652 dm->priority = htonl(priority);
653 dm->anonymity = htonl(anonymity);
654 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
655 dm->uid = GNUNET_htonll(uid);
657 memcpy (&dm[1], data, size);
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Transmitting `%s' message\n",
663 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
669 * Handle RESERVE-message.
672 * @param client identification of the client
673 * @param message the actual message
676 handle_reserve (void *cls,
677 struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *message)
680 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
681 struct ReservationList *e;
682 unsigned long long used;
683 unsigned long long req;
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Processing `%s' request\n",
692 amount = GNUNET_ntohll(msg->amount);
693 entries = ntohl(msg->entries);
694 used = plugin->api->get_size (plugin->api->cls) + reserved;
695 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
696 if (used + req > quota)
699 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
700 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
705 if (cache_size < req)
707 /* TODO: document this in the FAQ; essentially, if this
708 message happens, the insertion request could be blocked
709 by less-important content from migration because it is
710 larger than 1/8th of the overall available space, and
711 we only reserve 1/8th for "fresh" insertions */
712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
713 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
716 transmit_status (client, 0,
717 gettext_noop ("Insufficient space to satisfy request and "
718 "requested amount is larger than cache size"));
722 transmit_status (client, 0,
723 gettext_noop ("Insufficient space to satisfy request"));
728 e = GNUNET_malloc (sizeof(struct ReservationList));
729 e->next = reservations;
733 e->entries = entries;
734 e->rid = ++reservation_gen;
735 if (reservation_gen < 0)
736 reservation_gen = 0; /* wrap around */
737 transmit_status (client, e->rid, NULL);
742 * Handle RELEASE_RESERVE-message.
745 * @param client identification of the client
746 * @param message the actual message
749 handle_release_reserve (void *cls,
750 struct GNUNET_SERVER_Client *client,
751 const struct GNUNET_MessageHeader *message)
753 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
754 struct ReservationList *pos;
755 struct ReservationList *prev;
756 struct ReservationList *next;
757 int rid = ntohl(msg->rid);
758 unsigned long long rem;
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Processing `%s' request\n",
767 while (NULL != (pos = next))
776 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
777 GNUNET_assert (reserved >= rem);
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Returning %llu remaining reserved bytes to storage pool\n",
785 transmit_status (client, GNUNET_OK, NULL);
791 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
796 * Check that the given message is a valid data message.
798 * @return NULL if the message is not well-formed, otherwise the message
800 static const struct DataMessage *
801 check_data (const struct GNUNET_MessageHeader *message)
805 const struct DataMessage *dm;
807 size = ntohs(message->size);
808 if (size < sizeof(struct DataMessage))
813 dm = (const struct DataMessage *) message;
814 dsize = ntohl(dm->size);
815 if (size != dsize + sizeof(struct DataMessage))
825 * Handle PUT-message.
828 * @param client identification of the client
829 * @param message the actual message
832 handle_put (void *cls,
833 struct GNUNET_SERVER_Client *client,
834 const struct GNUNET_MessageHeader *message)
836 const struct DataMessage *dm = check_data (message);
840 struct ReservationList *pos;
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Processing `%s' request\n",
848 if (ntohl(dm->type) == 0)
856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
859 rid = ntohl(dm->rid);
860 size = ntohl(dm->size);
864 while ( (NULL != pos) &&
867 GNUNET_break (pos != NULL);
870 GNUNET_break (pos->entries > 0);
871 GNUNET_break (pos->amount > size);
874 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
878 ret = plugin->api->put (plugin->api->cls,
884 ntohl(dm->anonymity),
885 GNUNET_TIME_absolute_ntoh(dm->expiration),
887 if (GNUNET_OK == ret)
889 GNUNET_CONTAINER_bloomfilter_add (filter,
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 "Successfully stored %u bytes under key `%s'\n",
895 GNUNET_h2s (&dm->key));
898 transmit_status (client,
899 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
901 GNUNET_free_non_null (msg);
902 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
903 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
908 * Handle GET-message.
911 * @param client identification of the client
912 * @param message the actual message
915 handle_get (void *cls,
916 struct GNUNET_SERVER_Client *client,
917 const struct GNUNET_MessageHeader *message)
919 const struct GetMessage *msg;
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
924 "Processing `%s' request\n",
927 size = ntohs(message->size);
928 if ( (size != sizeof(struct GetMessage)) &&
929 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
935 GNUNET_SERVER_client_keep (client);
936 msg = (const struct GetMessage*) message;
937 if ( (size == sizeof(struct GetMessage)) &&
938 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
941 /* don't bother database... */
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Empty result set for `%s' request for `%s'.\n",
946 GNUNET_h2s (&msg->key));
948 transmit_item (client,
949 NULL, NULL, 0, NULL, 0, 0, 0,
950 GNUNET_TIME_UNIT_ZERO_ABS, 0);
953 plugin->api->get (plugin->api->cls,
954 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
963 * Handle UPDATE-message.
966 * @param client identification of the client
967 * @param message the actual message
970 handle_update (void *cls,
971 struct GNUNET_SERVER_Client *client,
972 const struct GNUNET_MessageHeader *message)
974 const struct UpdateMessage *msg;
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Processing `%s' request\n",
983 msg = (const struct UpdateMessage*) message;
985 ret = plugin->api->update (plugin->api->cls,
986 GNUNET_ntohll(msg->uid),
987 (int32_t) ntohl(msg->priority),
988 GNUNET_TIME_absolute_ntoh(msg->expiration),
990 transmit_status (client, ret, emsg);
991 GNUNET_free_non_null (emsg);
996 * Handle GET_RANDOM-message.
999 * @param client identification of the client
1000 * @param message the actual message
1003 handle_get_random (void *cls,
1004 struct GNUNET_SERVER_Client *client,
1005 const struct GNUNET_MessageHeader *message)
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "Processing `%s' request\n",
1012 GNUNET_SERVER_client_keep (client);
1013 plugin->api->iter_migration_order (plugin->api->cls,
1021 * Context for the 'remove_callback'.
1023 struct RemoveContext
1026 * Client for whom we're doing the remvoing.
1028 struct GNUNET_SERVER_Client *client;
1031 * GNUNET_YES if we managed to remove something.
1038 * Callback function that will cause the item that is passed
1039 * in to be deleted (by returning GNUNET_NO).
1042 remove_callback (void *cls,
1044 const GNUNET_HashCode * key,
1050 struct GNUNET_TIME_Absolute
1051 expiration, uint64_t uid)
1053 struct RemoveContext *rc = cls;
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "No further matches for `%s' request.\n",
1062 if (GNUNET_YES == rc->found)
1063 transmit_status (rc->client, GNUNET_OK, NULL);
1065 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1066 GNUNET_SERVER_client_drop (rc->client);
1068 return GNUNET_OK; /* last item */
1070 rc->found = GNUNET_YES;
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "Item %llu matches `%s' request.\n",
1074 (unsigned long long) uid,
1077 GNUNET_CONTAINER_bloomfilter_remove (filter,
1079 plugin->api->next_request (next_cls, GNUNET_YES);
1085 * Handle REMOVE-message.
1087 * @param cls closure
1088 * @param client identification of the client
1089 * @param message the actual message
1092 handle_remove (void *cls,
1093 struct GNUNET_SERVER_Client *client,
1094 const struct GNUNET_MessageHeader *message)
1096 const struct DataMessage *dm = check_data (message);
1097 GNUNET_HashCode vhash;
1098 struct RemoveContext *rc;
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Processing `%s' request\n",
1108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1111 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1112 GNUNET_SERVER_client_keep (client);
1113 rc->client = client;
1114 GNUNET_CRYPTO_hash (&dm[1],
1117 plugin->api->get (plugin->api->cls,
1127 * Handle DROP-message.
1129 * @param cls closure
1130 * @param client identification of the client
1131 * @param message the actual message
1134 handle_drop (void *cls,
1135 struct GNUNET_SERVER_Client *client,
1136 const struct GNUNET_MessageHeader *message)
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Processing `%s' request\n",
1143 plugin->api->drop (plugin->api->cls);
1144 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1149 * List of handlers for the messages understood by this
1152 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1153 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1154 sizeof(struct ReserveMessage) },
1155 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1156 sizeof(struct ReleaseReserveMessage) },
1157 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1158 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1159 sizeof (struct UpdateMessage) },
1160 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1161 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1162 sizeof(struct GNUNET_MessageHeader) },
1163 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1164 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1165 sizeof(struct GNUNET_MessageHeader) },
1172 * Load the datastore plugin.
1174 static struct DatastorePlugin *
1177 struct DatastorePlugin *ret;
1182 GNUNET_CONFIGURATION_get_value_string (cfg,
1183 "DATASTORE", "DATABASE", &name))
1185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1186 _("No `%s' specified for `%s' in configuration!\n"),
1191 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1193 ret->env.sched = sched;
1194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1195 _("Loading `%s' datastore plugin\n"), name);
1196 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1197 ret->short_name = name;
1198 ret->lib_name = libname;
1199 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1200 if (ret->api == NULL)
1202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1203 _("Failed to load datastore plugin for `%s'\n"), name);
1204 GNUNET_free (ret->short_name);
1205 GNUNET_free (libname);
1214 * Function called when the service shuts
1215 * down. Unloads our datastore plugin.
1217 * @param plug plugin to unload
1220 unload_plugin (struct DatastorePlugin *plug)
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Datastore service is unloading plugin...\n");
1226 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1227 GNUNET_free (plug->lib_name);
1228 GNUNET_free (plug->short_name);
1234 * Final task run after shutdown. Unloads plugins and disconnects us from
1238 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1240 unload_plugin (plugin);
1244 GNUNET_CONTAINER_bloomfilter_free (filter);
1247 GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
1252 * Last task run during shutdown. Disconnects us from
1253 * the transport and core.
1256 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1258 struct TransmitCallbackContext *tcc;
1260 while (NULL != (tcc = tcc_head))
1262 GNUNET_CONTAINER_DLL_remove (tcc_head,
1265 if (tcc->th != NULL)
1266 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1267 if (NULL != tcc->tc)
1268 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1269 GNUNET_free (tcc->msg);
1272 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1274 GNUNET_SCHEDULER_cancel (sched,
1276 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1278 GNUNET_SCHEDULER_add_continuation (sched,
1281 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1286 * Function that removes all active reservations made
1287 * by the given client and releases the space for other
1290 * @param cls closure
1291 * @param client identification of the client
1294 cleanup_reservations (void *cls,
1295 struct GNUNET_SERVER_Client
1298 struct ReservationList *pos;
1299 struct ReservationList *prev;
1300 struct ReservationList *next;
1309 if (pos->client == client)
1312 reservations = next;
1315 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1328 * Process datastore requests.
1330 * @param cls closure
1331 * @param s scheduler to use
1332 * @param server the initialized server
1333 * @param c configuration to use
1337 struct GNUNET_SCHEDULER_Handle *s,
1338 struct GNUNET_SERVER_Handle *server,
1339 const struct GNUNET_CONFIGURATION_Handle *c)
1342 unsigned int bf_size;
1347 GNUNET_CONFIGURATION_get_value_number (cfg,
1348 "DATASTORE", "QUOTA", "a))
1350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1351 _("No `%s' specified for `%s' in configuration!\n"),
1356 cache_size = quota / 8; /* Or should we make this an option? */
1357 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1360 GNUNET_CONFIGURATION_get_value_filename (cfg,
1365 GNUNET_DISK_directory_create_for_file (fn)) )
1367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1368 _("Could not use specified filename `%s' for bloomfilter.\n"),
1369 fn != NULL ? fn : "");
1370 GNUNET_free_non_null (fn);
1373 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1374 GNUNET_free_non_null (fn);
1377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1378 _("Failed to initialize bloomfilter.\n"));
1381 GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
1382 plugin = load_plugin ();
1385 GNUNET_CONTAINER_bloomfilter_free (filter);
1387 GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
1390 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1391 GNUNET_SERVER_add_handlers (server, handlers);
1393 = GNUNET_SCHEDULER_add_with_priority (sched,
1394 GNUNET_SCHEDULER_PRIORITY_IDLE,
1395 &delete_expired, NULL);
1396 GNUNET_SCHEDULER_add_delayed (sched,
1397 GNUNET_TIME_UNIT_FOREVER_REL,
1398 &cleaning_task, NULL);
1404 * The main function for the datastore service.
1406 * @param argc number of arguments from the command line
1407 * @param argv command line arguments
1408 * @return 0 ok, 1 on error
1411 main (int argc, char *const *argv)
1416 GNUNET_SERVICE_run (argc,
1419 GNUNET_SERVICE_OPTION_NONE,
1420 &run, NULL)) ? 0 : 1;
1425 /* end of gnunet-service-datastore.c */