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)
458 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
460 GNUNET_free (tcc->msg);
464 GNUNET_assert (size >= msize);
465 memcpy (buf, tcc->msg, msize);
467 tcc->tc (tcc->tc_cls, GNUNET_OK);
468 if (GNUNET_YES == tcc->end)
470 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Response transmitted, more pending!\n");
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);
532 GNUNET_CONTAINER_DLL_insert (tcc_head,
539 * Transmit a status code to the client.
541 * @param client receiver of the response
542 * @param code status code
543 * @param msg optional error message (can be NULL)
546 transmit_status (struct GNUNET_SERVER_Client *client,
550 struct StatusMessage *sm;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Transmitting `%s' message with value %d and message `%s'\n",
558 msg != NULL ? msg : "(none)");
560 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
561 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
562 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
563 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
564 sm->status = htonl(code);
566 memcpy (&sm[1], msg, slen);
567 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
572 * Function called once the transmit operation has
573 * either failed or succeeded.
575 * @param next_cls closure for calling "next_request" callback
576 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
579 get_next(void *next_cls,
582 if (status != GNUNET_OK)
584 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
585 _("Failed to transmit an item to the client; aborting iteration.\n"));
587 plugin->api->next_request (next_cls, GNUNET_YES);
590 plugin->api->next_request (next_cls, GNUNET_NO);
595 * Function that will transmit the given datastore entry
598 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
599 * @param next_cls closure to use to ask for the next item
600 * @param key key for the content
601 * @param size number of bytes in data
602 * @param data content stored
603 * @param type type of the content
604 * @param priority priority of the content
605 * @param anonymity anonymity-level for the content
606 * @param expiration expiration time for the content
607 * @param uid unique identifier for the datum;
608 * maybe 0 if no unique identifier is available
610 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
611 * GNUNET_NO to delete the item and continue (if supported)
614 transmit_item (void *cls,
616 const GNUNET_HashCode * key,
622 struct GNUNET_TIME_Absolute
623 expiration, uint64_t uid)
625 struct GNUNET_SERVER_Client *client = cls;
626 struct GNUNET_MessageHeader *end;
627 struct DataMessage *dm;
631 /* transmit 'DATA_END' */
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Transmitting `%s' message\n",
637 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
638 end->size = htons(sizeof(struct GNUNET_MessageHeader));
639 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
640 transmit (client, end, NULL, NULL, GNUNET_YES);
641 GNUNET_SERVER_client_drop (client);
644 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
645 dm->header.size = htons(sizeof(struct DataMessage) + size);
646 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
648 dm->size = htonl(size);
649 dm->type = htonl(type);
650 dm->priority = htonl(priority);
651 dm->anonymity = htonl(anonymity);
652 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
653 dm->uid = GNUNET_htonll(uid);
655 memcpy (&dm[1], data, size);
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Transmitting `%s' message\n",
661 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
667 * Handle RESERVE-message.
670 * @param client identification of the client
671 * @param message the actual message
674 handle_reserve (void *cls,
675 struct GNUNET_SERVER_Client *client,
676 const struct GNUNET_MessageHeader *message)
678 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
679 struct ReservationList *e;
680 unsigned long long used;
681 unsigned long long req;
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Processing `%s' request\n",
690 amount = GNUNET_ntohll(msg->amount);
691 entries = ntohl(msg->entries);
692 used = plugin->api->get_size (plugin->api->cls) + reserved;
693 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
694 if (used + req > quota)
697 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
699 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
703 if (cache_size < req)
705 /* TODO: document this in the FAQ; essentially, if this
706 message happens, the insertion request could be blocked
707 by less-important content from migration because it is
708 larger than 1/8th of the overall available space, and
709 we only reserve 1/8th for "fresh" insertions */
710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
711 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
714 transmit_status (client, 0,
715 gettext_noop ("Insufficient space to satisfy request and "
716 "requested amount is larger than cache size"));
720 transmit_status (client, 0,
721 gettext_noop ("Insufficient space to satisfy request"));
726 e = GNUNET_malloc (sizeof(struct ReservationList));
727 e->next = reservations;
731 e->entries = entries;
732 e->rid = ++reservation_gen;
733 if (reservation_gen < 0)
734 reservation_gen = 0; /* wrap around */
735 transmit_status (client, e->rid, NULL);
740 * Handle RELEASE_RESERVE-message.
743 * @param client identification of the client
744 * @param message the actual message
747 handle_release_reserve (void *cls,
748 struct GNUNET_SERVER_Client *client,
749 const struct GNUNET_MessageHeader *message)
751 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
752 struct ReservationList *pos;
753 struct ReservationList *prev;
754 struct ReservationList *next;
755 int rid = ntohl(msg->rid);
756 unsigned long long rem;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Processing `%s' request\n",
765 while (NULL != (pos = next))
774 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
775 GNUNET_assert (reserved >= rem);
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Returning %llu remaining reserved bytes to storage pool\n",
783 transmit_status (client, GNUNET_OK, NULL);
789 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
794 * Check that the given message is a valid data message.
796 * @return NULL if the message is not well-formed, otherwise the message
798 static const struct DataMessage *
799 check_data (const struct GNUNET_MessageHeader *message)
803 const struct DataMessage *dm;
805 size = ntohs(message->size);
806 if (size < sizeof(struct DataMessage))
811 dm = (const struct DataMessage *) message;
812 dsize = ntohl(dm->size);
813 if (size != dsize + sizeof(struct DataMessage))
823 * Handle PUT-message.
826 * @param client identification of the client
827 * @param message the actual message
830 handle_put (void *cls,
831 struct GNUNET_SERVER_Client *client,
832 const struct GNUNET_MessageHeader *message)
834 const struct DataMessage *dm = check_data (message);
838 struct ReservationList *pos;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Processing `%s' request\n",
846 if (ntohl(dm->type) == 0)
854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
857 rid = ntohl(dm->rid);
858 size = ntohl(dm->size);
862 while ( (NULL != pos) &&
865 GNUNET_break (pos != NULL);
868 GNUNET_break (pos->entries > 0);
869 GNUNET_break (pos->amount > size);
872 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
876 ret = plugin->api->put (plugin->api->cls,
882 ntohl(dm->anonymity),
883 GNUNET_TIME_absolute_ntoh(dm->expiration),
885 if (GNUNET_OK == ret)
887 GNUNET_CONTAINER_bloomfilter_add (filter,
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891 "Successfully stored %u bytes under key `%s'\n",
893 GNUNET_h2s (&dm->key));
896 transmit_status (client,
897 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
899 GNUNET_free_non_null (msg);
900 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
901 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
906 * Handle GET-message.
909 * @param client identification of the client
910 * @param message the actual message
913 handle_get (void *cls,
914 struct GNUNET_SERVER_Client *client,
915 const struct GNUNET_MessageHeader *message)
917 const struct GetMessage *msg;
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Processing `%s' request\n",
925 size = ntohs(message->size);
926 if ( (size != sizeof(struct GetMessage)) &&
927 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
933 GNUNET_SERVER_client_keep (client);
934 msg = (const struct GetMessage*) message;
935 if ( (size == sizeof(struct GetMessage)) &&
936 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
939 /* don't bother database... */
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Empty result set for `%s' request for `%s'.\n",
944 GNUNET_h2s (&msg->key));
946 transmit_item (client,
947 NULL, NULL, 0, NULL, 0, 0, 0,
948 GNUNET_TIME_UNIT_ZERO_ABS, 0);
951 plugin->api->get (plugin->api->cls,
952 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
961 * Handle UPDATE-message.
964 * @param client identification of the client
965 * @param message the actual message
968 handle_update (void *cls,
969 struct GNUNET_SERVER_Client *client,
970 const struct GNUNET_MessageHeader *message)
972 const struct UpdateMessage *msg;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Processing `%s' request\n",
981 msg = (const struct UpdateMessage*) message;
983 ret = plugin->api->update (plugin->api->cls,
984 GNUNET_ntohll(msg->uid),
985 (int32_t) ntohl(msg->priority),
986 GNUNET_TIME_absolute_ntoh(msg->expiration),
988 transmit_status (client, ret, emsg);
989 GNUNET_free_non_null (emsg);
994 * Handle GET_RANDOM-message.
997 * @param client identification of the client
998 * @param message the actual message
1001 handle_get_random (void *cls,
1002 struct GNUNET_SERVER_Client *client,
1003 const struct GNUNET_MessageHeader *message)
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "Processing `%s' request\n",
1010 GNUNET_SERVER_client_keep (client);
1011 plugin->api->iter_migration_order (plugin->api->cls,
1019 * Context for the 'remove_callback'.
1021 struct RemoveContext
1024 * Client for whom we're doing the remvoing.
1026 struct GNUNET_SERVER_Client *client;
1029 * GNUNET_YES if we managed to remove something.
1036 * Callback function that will cause the item that is passed
1037 * in to be deleted (by returning GNUNET_NO).
1040 remove_callback (void *cls,
1042 const GNUNET_HashCode * key,
1048 struct GNUNET_TIME_Absolute
1049 expiration, uint64_t uid)
1051 struct RemoveContext *rc = cls;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "No further matches for `%s' request.\n",
1060 if (GNUNET_YES == rc->found)
1061 transmit_status (rc->client, GNUNET_OK, NULL);
1063 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1064 GNUNET_SERVER_client_drop (rc->client);
1066 return GNUNET_OK; /* last item */
1068 rc->found = GNUNET_YES;
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "Item %llu matches `%s' request.\n",
1072 (unsigned long long) uid,
1075 GNUNET_CONTAINER_bloomfilter_remove (filter,
1077 plugin->api->next_request (next_cls, GNUNET_YES);
1083 * Handle REMOVE-message.
1085 * @param cls closure
1086 * @param client identification of the client
1087 * @param message the actual message
1090 handle_remove (void *cls,
1091 struct GNUNET_SERVER_Client *client,
1092 const struct GNUNET_MessageHeader *message)
1094 const struct DataMessage *dm = check_data (message);
1095 GNUNET_HashCode vhash;
1096 struct RemoveContext *rc;
1099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1100 "Processing `%s' request\n",
1106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1109 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1110 GNUNET_SERVER_client_keep (client);
1111 rc->client = client;
1112 GNUNET_CRYPTO_hash (&dm[1],
1115 plugin->api->get (plugin->api->cls,
1125 * Handle DROP-message.
1127 * @param cls closure
1128 * @param client identification of the client
1129 * @param message the actual message
1132 handle_drop (void *cls,
1133 struct GNUNET_SERVER_Client *client,
1134 const struct GNUNET_MessageHeader *message)
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "Processing `%s' request\n",
1141 plugin->api->drop (plugin->api->cls);
1142 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1147 * List of handlers for the messages understood by this
1150 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1151 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1152 sizeof(struct ReserveMessage) },
1153 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1154 sizeof(struct ReleaseReserveMessage) },
1155 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1156 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1157 sizeof (struct UpdateMessage) },
1158 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1159 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1160 sizeof(struct GNUNET_MessageHeader) },
1161 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1162 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1163 sizeof(struct GNUNET_MessageHeader) },
1170 * Load the datastore plugin.
1172 static struct DatastorePlugin *
1175 struct DatastorePlugin *ret;
1180 GNUNET_CONFIGURATION_get_value_string (cfg,
1181 "DATASTORE", "DATABASE", &name))
1183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1184 _("No `%s' specified for `%s' in configuration!\n"),
1189 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1191 ret->env.sched = sched;
1192 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1193 _("Loading `%s' datastore plugin\n"), name);
1194 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1195 ret->short_name = name;
1196 ret->lib_name = libname;
1197 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1198 if (ret->api == NULL)
1200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1201 _("Failed to load datastore plugin for `%s'\n"), name);
1202 GNUNET_free (ret->short_name);
1203 GNUNET_free (libname);
1212 * Function called when the service shuts
1213 * down. Unloads our datastore plugin.
1215 * @param plug plugin to unload
1218 unload_plugin (struct DatastorePlugin *plug)
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 "Datastore service is unloading plugin...\n");
1224 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1225 GNUNET_free (plug->lib_name);
1226 GNUNET_free (plug->short_name);
1232 * Last task run during shutdown. Disconnects us from
1233 * the transport and core.
1236 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1238 struct TransmitCallbackContext *tcc;
1240 while (NULL != (tcc = tcc_head))
1242 GNUNET_CONTAINER_DLL_remove (tcc_head,
1245 if (tcc->th != NULL)
1246 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1247 if (NULL != tcc->tc)
1248 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1249 GNUNET_free (tcc->msg);
1252 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1254 GNUNET_SCHEDULER_cancel (sched,
1256 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1258 unload_plugin (plugin);
1262 GNUNET_CONTAINER_bloomfilter_free (filter);
1265 GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
1270 * Function that removes all active reservations made
1271 * by the given client and releases the space for other
1274 * @param cls closure
1275 * @param client identification of the client
1278 cleanup_reservations (void *cls,
1279 struct GNUNET_SERVER_Client
1282 struct ReservationList *pos;
1283 struct ReservationList *prev;
1284 struct ReservationList *next;
1293 if (pos->client == client)
1296 reservations = next;
1299 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1312 * Process datastore requests.
1314 * @param cls closure
1315 * @param s scheduler to use
1316 * @param server the initialized server
1317 * @param c configuration to use
1321 struct GNUNET_SCHEDULER_Handle *s,
1322 struct GNUNET_SERVER_Handle *server,
1323 const struct GNUNET_CONFIGURATION_Handle *c)
1326 unsigned int bf_size;
1331 GNUNET_CONFIGURATION_get_value_number (cfg,
1332 "DATASTORE", "QUOTA", "a))
1334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1335 _("No `%s' specified for `%s' in configuration!\n"),
1340 cache_size = quota / 8; /* Or should we make this an option? */
1341 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1344 GNUNET_CONFIGURATION_get_value_filename (cfg,
1349 GNUNET_DISK_directory_create_for_file (fn)) )
1351 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1352 _("Could not use specified filename `%s' for bloomfilter.\n"),
1353 fn != NULL ? fn : "");
1354 GNUNET_free_non_null (fn);
1357 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1358 GNUNET_free_non_null (fn);
1361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1362 _("Failed to initialize bloomfilter.\n"));
1365 GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
1366 plugin = load_plugin ();
1369 GNUNET_CONTAINER_bloomfilter_free (filter);
1371 GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
1374 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1375 GNUNET_SERVER_add_handlers (server, handlers);
1377 = GNUNET_SCHEDULER_add_with_priority (sched,
1378 GNUNET_SCHEDULER_PRIORITY_IDLE,
1379 &delete_expired, NULL);
1380 GNUNET_SCHEDULER_add_delayed (sched,
1381 GNUNET_TIME_UNIT_FOREVER_REL,
1382 &cleaning_task, NULL);
1388 * The main function for the datastore service.
1390 * @param argc number of arguments from the command line
1391 * @param argv command line arguments
1392 * @return 0 ok, 1 on error
1395 main (int argc, char *const *argv)
1400 GNUNET_SERVICE_run (argc,
1403 GNUNET_SERVICE_OPTION_NONE,
1404 &run, NULL)) ? 0 : 1;
1409 /* end of gnunet-service-datastore.c */