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;
238 * Have we already clean ed up the TCCs and are hence no longer
239 * willing (or able) to transmit anything to anyone?
241 static int cleaning_done;
244 * Task that is used to remove expired entries from
245 * the datastore. This task will schedule itself
246 * again automatically to always delete all expired
249 * @param cls not used
250 * @param tc task context
253 delete_expired (void *cls,
254 const struct GNUNET_SCHEDULER_TaskContext *tc);
258 * Iterate over the expired items stored in the datastore.
259 * Delete all expired items; once we have processed all
260 * expired items, re-schedule the "delete_expired" task.
262 * @param cls not used
263 * @param next_cls closure to pass to the "next" function.
264 * @param key key for the content
265 * @param size number of bytes in data
266 * @param data content stored
267 * @param type type of the content
268 * @param priority priority of the content
269 * @param anonymity anonymity-level for the content
270 * @param expiration expiration time for the content
271 * @param uid unique identifier for the datum;
272 * maybe 0 if no unique identifier is available
274 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
275 * (continue on call to "next", of course),
276 * GNUNET_NO to delete the item and continue (if supported)
279 expired_processor (void *cls,
281 const GNUNET_HashCode * key,
287 struct GNUNET_TIME_Absolute
291 struct GNUNET_TIME_Absolute now;
293 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
297 = GNUNET_SCHEDULER_add_delayed (sched,
301 return GNUNET_SYSERR;
303 now = GNUNET_TIME_absolute_get ();
304 if (expiration.value > now.value)
306 /* finished processing */
307 plugin->api->next_request (next_cls, GNUNET_YES);
308 return GNUNET_SYSERR;
310 plugin->api->next_request (next_cls, GNUNET_NO);
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Deleting content that expired %llu ms ago\n",
314 (unsigned long long) (now.value - expiration.value));
316 GNUNET_CONTAINER_bloomfilter_remove (filter,
318 return GNUNET_NO; /* delete */
323 * Task that is used to remove expired entries from
324 * the datastore. This task will schedule itself
325 * again automatically to always delete all expired
328 * @param cls not used
329 * @param tc task context
332 delete_expired (void *cls,
333 const struct GNUNET_SCHEDULER_TaskContext *tc)
335 plugin->api->iter_ascending_expiration (plugin->api->cls,
343 * An iterator over a set of items stored in the datastore.
346 * @param next_cls closure to pass to the "next" function.
347 * @param key key for the content
348 * @param size number of bytes in data
349 * @param data content stored
350 * @param type type of the content
351 * @param priority priority of the content
352 * @param anonymity anonymity-level for the content
353 * @param expiration expiration time for the content
354 * @param uid unique identifier for the datum;
355 * maybe 0 if no unique identifier is available
357 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
358 * (continue on call to "next", of course),
359 * GNUNET_NO to delete the item and continue (if supported)
364 const GNUNET_HashCode * key,
370 struct GNUNET_TIME_Absolute
374 unsigned long long *need = cls;
379 return GNUNET_SYSERR;
381 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
384 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
385 plugin->api->next_request (next_cls,
386 (0 == *need) ? GNUNET_YES : GNUNET_NO);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
390 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
393 GNUNET_CONTAINER_bloomfilter_remove (filter,
400 * Manage available disk space by running tasks
401 * that will discard content if necessary. This
402 * function will be run whenever a request for
403 * "need" bytes of storage could only be satisfied
404 * by eating into the "cache" (and we want our cache
407 * @param need number of bytes of content that were
408 * placed into the "cache" (and hence the
409 * number of bytes that should be removed).
412 manage_space (unsigned long long need)
414 unsigned long long *n;
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Asked to free up %llu bytes of cache space\n",
421 n = GNUNET_malloc (sizeof(unsigned long long));
423 plugin->api->iter_low_priority (plugin->api->cls,
431 * Function called to notify a client about the socket
432 * begin ready to queue more data. "buf" will be
433 * NULL and "size" zero if the socket was closed for
434 * writing in the meantime.
437 * @param size number of bytes available in buf
438 * @param buf where the callee should write the message
439 * @return number of bytes written to buf
442 transmit_callback (void *cls,
443 size_t size, void *buf)
445 struct TransmitCallbackContext *tcc = cls;
449 GNUNET_CONTAINER_DLL_remove (tcc_head,
452 msize = ntohs(tcc->msg->size);
456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 "Transmission failed.\n");
460 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
461 if (GNUNET_YES == tcc->end)
462 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
463 GNUNET_SERVER_client_drop (tcc->client);
464 GNUNET_free (tcc->msg);
468 GNUNET_assert (size >= msize);
469 memcpy (buf, tcc->msg, msize);
471 tcc->tc (tcc->tc_cls, GNUNET_OK);
472 if (GNUNET_YES == tcc->end)
474 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Response transmitted, more pending!\n");
483 GNUNET_SERVER_client_drop (tcc->client);
484 GNUNET_free (tcc->msg);
491 * Transmit the given message to the client.
493 * @param client target of the message
494 * @param msg message to transmit, will be freed!
495 * @param tc function to call afterwards
496 * @param tc_cls closure for tc
497 * @param end is this the last response (and we should
498 * signal the server completion accodingly after
499 * transmitting this message)?
502 transmit (struct GNUNET_SERVER_Client *client,
503 struct GNUNET_MessageHeader *msg,
504 TransmitContinuation tc,
508 struct TransmitCallbackContext *tcc;
510 if (GNUNET_YES == cleaning_done)
513 tc (tc_cls, GNUNET_SYSERR);
516 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
518 tcc->client = client;
520 tcc->tc_cls = tc_cls;
523 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
525 GNUNET_TIME_UNIT_FOREVER_REL,
530 if (GNUNET_YES == end)
533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
534 "Disconnecting client.\n");
536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
539 tc (tc_cls, GNUNET_SYSERR);
544 GNUNET_SERVER_client_keep (client);
545 GNUNET_CONTAINER_DLL_insert (tcc_head,
552 * Transmit a status code to the client.
554 * @param client receiver of the response
555 * @param code status code
556 * @param msg optional error message (can be NULL)
559 transmit_status (struct GNUNET_SERVER_Client *client,
563 struct StatusMessage *sm;
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Transmitting `%s' message with value %d and message `%s'\n",
571 msg != NULL ? msg : "(none)");
573 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
574 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
575 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
576 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
577 sm->status = htonl(code);
579 memcpy (&sm[1], msg, slen);
580 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
585 * Function called once the transmit operation has
586 * either failed or succeeded.
588 * @param next_cls closure for calling "next_request" callback
589 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
592 get_next(void *next_cls,
595 if (status != GNUNET_OK)
597 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
598 _("Failed to transmit an item to the client; aborting iteration.\n"));
600 plugin->api->next_request (next_cls, GNUNET_YES);
603 plugin->api->next_request (next_cls, GNUNET_NO);
608 * Function that will transmit the given datastore entry
611 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
612 * @param next_cls closure to use to ask for the next item
613 * @param key key for the content
614 * @param size number of bytes in data
615 * @param data content stored
616 * @param type type of the content
617 * @param priority priority of the content
618 * @param anonymity anonymity-level for the content
619 * @param expiration expiration time for the content
620 * @param uid unique identifier for the datum;
621 * maybe 0 if no unique identifier is available
623 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
624 * GNUNET_NO to delete the item and continue (if supported)
627 transmit_item (void *cls,
629 const GNUNET_HashCode * key,
635 struct GNUNET_TIME_Absolute
636 expiration, uint64_t uid)
638 struct GNUNET_SERVER_Client *client = cls;
639 struct GNUNET_MessageHeader *end;
640 struct DataMessage *dm;
644 /* transmit 'DATA_END' */
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Transmitting `%s' message\n",
650 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
651 end->size = htons(sizeof(struct GNUNET_MessageHeader));
652 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
653 transmit (client, end, NULL, NULL, GNUNET_YES);
654 GNUNET_SERVER_client_drop (client);
657 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
658 dm->header.size = htons(sizeof(struct DataMessage) + size);
659 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
661 dm->size = htonl(size);
662 dm->type = htonl(type);
663 dm->priority = htonl(priority);
664 dm->anonymity = htonl(anonymity);
665 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
666 dm->uid = GNUNET_htonll(uid);
668 memcpy (&dm[1], data, size);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Transmitting `%s' message\n",
674 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
680 * Handle RESERVE-message.
683 * @param client identification of the client
684 * @param message the actual message
687 handle_reserve (void *cls,
688 struct GNUNET_SERVER_Client *client,
689 const struct GNUNET_MessageHeader *message)
691 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
692 struct ReservationList *e;
693 unsigned long long used;
694 unsigned long long req;
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Processing `%s' request\n",
703 amount = GNUNET_ntohll(msg->amount);
704 entries = ntohl(msg->entries);
705 used = plugin->api->get_size (plugin->api->cls) + reserved;
706 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
707 if (used + req > quota)
710 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
711 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
712 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
716 if (cache_size < req)
718 /* TODO: document this in the FAQ; essentially, if this
719 message happens, the insertion request could be blocked
720 by less-important content from migration because it is
721 larger than 1/8th of the overall available space, and
722 we only reserve 1/8th for "fresh" insertions */
723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
724 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
727 transmit_status (client, 0,
728 gettext_noop ("Insufficient space to satisfy request and "
729 "requested amount is larger than cache size"));
733 transmit_status (client, 0,
734 gettext_noop ("Insufficient space to satisfy request"));
739 e = GNUNET_malloc (sizeof(struct ReservationList));
740 e->next = reservations;
744 e->entries = entries;
745 e->rid = ++reservation_gen;
746 if (reservation_gen < 0)
747 reservation_gen = 0; /* wrap around */
748 transmit_status (client, e->rid, NULL);
753 * Handle RELEASE_RESERVE-message.
756 * @param client identification of the client
757 * @param message the actual message
760 handle_release_reserve (void *cls,
761 struct GNUNET_SERVER_Client *client,
762 const struct GNUNET_MessageHeader *message)
764 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
765 struct ReservationList *pos;
766 struct ReservationList *prev;
767 struct ReservationList *next;
768 int rid = ntohl(msg->rid);
769 unsigned long long rem;
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Processing `%s' request\n",
778 while (NULL != (pos = next))
787 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
788 GNUNET_assert (reserved >= rem);
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Returning %llu remaining reserved bytes to storage pool\n",
796 transmit_status (client, GNUNET_OK, NULL);
802 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
807 * Check that the given message is a valid data message.
809 * @return NULL if the message is not well-formed, otherwise the message
811 static const struct DataMessage *
812 check_data (const struct GNUNET_MessageHeader *message)
816 const struct DataMessage *dm;
818 size = ntohs(message->size);
819 if (size < sizeof(struct DataMessage))
824 dm = (const struct DataMessage *) message;
825 dsize = ntohl(dm->size);
826 if (size != dsize + sizeof(struct DataMessage))
836 * Handle PUT-message.
839 * @param client identification of the client
840 * @param message the actual message
843 handle_put (void *cls,
844 struct GNUNET_SERVER_Client *client,
845 const struct GNUNET_MessageHeader *message)
847 const struct DataMessage *dm = check_data (message);
851 struct ReservationList *pos;
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Processing `%s' request\n",
859 if (ntohl(dm->type) == 0)
867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
870 rid = ntohl(dm->rid);
871 size = ntohl(dm->size);
875 while ( (NULL != pos) &&
878 GNUNET_break (pos != NULL);
881 GNUNET_break (pos->entries > 0);
882 GNUNET_break (pos->amount > size);
885 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
889 ret = plugin->api->put (plugin->api->cls,
895 ntohl(dm->anonymity),
896 GNUNET_TIME_absolute_ntoh(dm->expiration),
898 if (GNUNET_OK == ret)
900 GNUNET_CONTAINER_bloomfilter_add (filter,
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Successfully stored %u bytes under key `%s'\n",
906 GNUNET_h2s (&dm->key));
909 transmit_status (client,
910 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
912 GNUNET_free_non_null (msg);
913 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
914 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
919 * Handle GET-message.
922 * @param client identification of the client
923 * @param message the actual message
926 handle_get (void *cls,
927 struct GNUNET_SERVER_Client *client,
928 const struct GNUNET_MessageHeader *message)
930 const struct GetMessage *msg;
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "Processing `%s' request\n",
938 size = ntohs(message->size);
939 if ( (size != sizeof(struct GetMessage)) &&
940 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
946 GNUNET_SERVER_client_keep (client);
947 msg = (const struct GetMessage*) message;
948 if ( (size == sizeof(struct GetMessage)) &&
949 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
952 /* don't bother database... */
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955 "Empty result set for `%s' request for `%s'.\n",
957 GNUNET_h2s (&msg->key));
959 transmit_item (client,
960 NULL, NULL, 0, NULL, 0, 0, 0,
961 GNUNET_TIME_UNIT_ZERO_ABS, 0);
964 plugin->api->get (plugin->api->cls,
965 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
974 * Handle UPDATE-message.
977 * @param client identification of the client
978 * @param message the actual message
981 handle_update (void *cls,
982 struct GNUNET_SERVER_Client *client,
983 const struct GNUNET_MessageHeader *message)
985 const struct UpdateMessage *msg;
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Processing `%s' request\n",
994 msg = (const struct UpdateMessage*) message;
996 ret = plugin->api->update (plugin->api->cls,
997 GNUNET_ntohll(msg->uid),
998 (int32_t) ntohl(msg->priority),
999 GNUNET_TIME_absolute_ntoh(msg->expiration),
1001 transmit_status (client, ret, emsg);
1002 GNUNET_free_non_null (emsg);
1007 * Handle GET_RANDOM-message.
1009 * @param cls closure
1010 * @param client identification of the client
1011 * @param message the actual message
1014 handle_get_random (void *cls,
1015 struct GNUNET_SERVER_Client *client,
1016 const struct GNUNET_MessageHeader *message)
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020 "Processing `%s' request\n",
1023 GNUNET_SERVER_client_keep (client);
1024 plugin->api->iter_migration_order (plugin->api->cls,
1032 * Context for the 'remove_callback'.
1034 struct RemoveContext
1037 * Client for whom we're doing the remvoing.
1039 struct GNUNET_SERVER_Client *client;
1042 * GNUNET_YES if we managed to remove something.
1049 * Callback function that will cause the item that is passed
1050 * in to be deleted (by returning GNUNET_NO).
1053 remove_callback (void *cls,
1055 const GNUNET_HashCode * key,
1061 struct GNUNET_TIME_Absolute
1062 expiration, uint64_t uid)
1064 struct RemoveContext *rc = cls;
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "No further matches for `%s' request.\n",
1073 if (GNUNET_YES == rc->found)
1074 transmit_status (rc->client, GNUNET_OK, NULL);
1076 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1077 GNUNET_SERVER_client_drop (rc->client);
1079 return GNUNET_OK; /* last item */
1081 rc->found = GNUNET_YES;
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Item %llu matches `%s' request.\n",
1085 (unsigned long long) uid,
1088 GNUNET_CONTAINER_bloomfilter_remove (filter,
1090 plugin->api->next_request (next_cls, GNUNET_YES);
1096 * Handle REMOVE-message.
1098 * @param cls closure
1099 * @param client identification of the client
1100 * @param message the actual message
1103 handle_remove (void *cls,
1104 struct GNUNET_SERVER_Client *client,
1105 const struct GNUNET_MessageHeader *message)
1107 const struct DataMessage *dm = check_data (message);
1108 GNUNET_HashCode vhash;
1109 struct RemoveContext *rc;
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "Processing `%s' request\n",
1119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1122 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1123 GNUNET_SERVER_client_keep (client);
1124 rc->client = client;
1125 GNUNET_CRYPTO_hash (&dm[1],
1128 plugin->api->get (plugin->api->cls,
1138 * Handle DROP-message.
1140 * @param cls closure
1141 * @param client identification of the client
1142 * @param message the actual message
1145 handle_drop (void *cls,
1146 struct GNUNET_SERVER_Client *client,
1147 const struct GNUNET_MessageHeader *message)
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Processing `%s' request\n",
1154 plugin->api->drop (plugin->api->cls);
1155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1160 * List of handlers for the messages understood by this
1163 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1164 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1165 sizeof(struct ReserveMessage) },
1166 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1167 sizeof(struct ReleaseReserveMessage) },
1168 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1169 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1170 sizeof (struct UpdateMessage) },
1171 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1172 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1173 sizeof(struct GNUNET_MessageHeader) },
1174 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1175 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1176 sizeof(struct GNUNET_MessageHeader) },
1183 * Load the datastore plugin.
1185 static struct DatastorePlugin *
1188 struct DatastorePlugin *ret;
1193 GNUNET_CONFIGURATION_get_value_string (cfg,
1194 "DATASTORE", "DATABASE", &name))
1196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1197 _("No `%s' specified for `%s' in configuration!\n"),
1202 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1204 ret->env.sched = sched;
1205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1206 _("Loading `%s' datastore plugin\n"), name);
1207 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1208 ret->short_name = name;
1209 ret->lib_name = libname;
1210 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1211 if (ret->api == NULL)
1213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1214 _("Failed to load datastore plugin for `%s'\n"), name);
1215 GNUNET_free (ret->short_name);
1216 GNUNET_free (libname);
1225 * Function called when the service shuts
1226 * down. Unloads our datastore plugin.
1228 * @param plug plugin to unload
1231 unload_plugin (struct DatastorePlugin *plug)
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Datastore service is unloading plugin...\n");
1237 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1238 GNUNET_free (plug->lib_name);
1239 GNUNET_free (plug->short_name);
1245 * Final task run after shutdown. Unloads plugins and disconnects us from
1249 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1251 unload_plugin (plugin);
1255 GNUNET_CONTAINER_bloomfilter_free (filter);
1258 GNUNET_ARM_stop_services (cfg, tc->sched, "statistics", NULL);
1263 * Last task run during shutdown. Disconnects us from
1264 * the transport and core.
1267 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1269 struct TransmitCallbackContext *tcc;
1271 cleaning_done = GNUNET_YES;
1272 while (NULL != (tcc = tcc_head))
1274 GNUNET_CONTAINER_DLL_remove (tcc_head,
1277 if (tcc->th != NULL)
1279 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1280 GNUNET_SERVER_client_drop (tcc->client);
1282 if (NULL != tcc->tc)
1283 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1284 GNUNET_free (tcc->msg);
1287 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1289 GNUNET_SCHEDULER_cancel (sched,
1291 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1293 GNUNET_SCHEDULER_add_continuation (sched,
1296 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1301 * Function that removes all active reservations made
1302 * by the given client and releases the space for other
1305 * @param cls closure
1306 * @param client identification of the client
1309 cleanup_reservations (void *cls,
1310 struct GNUNET_SERVER_Client
1313 struct ReservationList *pos;
1314 struct ReservationList *prev;
1315 struct ReservationList *next;
1324 if (pos->client == client)
1327 reservations = next;
1330 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1343 * Process datastore requests.
1345 * @param cls closure
1346 * @param s scheduler to use
1347 * @param server the initialized server
1348 * @param c configuration to use
1352 struct GNUNET_SCHEDULER_Handle *s,
1353 struct GNUNET_SERVER_Handle *server,
1354 const struct GNUNET_CONFIGURATION_Handle *c)
1357 unsigned int bf_size;
1362 GNUNET_CONFIGURATION_get_value_number (cfg,
1363 "DATASTORE", "QUOTA", "a))
1365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1366 _("No `%s' specified for `%s' in configuration!\n"),
1371 cache_size = quota / 8; /* Or should we make this an option? */
1372 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1375 GNUNET_CONFIGURATION_get_value_filename (cfg,
1380 GNUNET_DISK_directory_create_for_file (fn)) )
1382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1383 _("Could not use specified filename `%s' for bloomfilter.\n"),
1384 fn != NULL ? fn : "");
1385 GNUNET_free_non_null (fn);
1388 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1389 GNUNET_free_non_null (fn);
1392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1393 _("Failed to initialize bloomfilter.\n"));
1396 GNUNET_ARM_start_services (cfg, sched, "statistics", NULL);
1397 plugin = load_plugin ();
1400 GNUNET_CONTAINER_bloomfilter_free (filter);
1402 GNUNET_ARM_stop_services (cfg, sched, "statistics", NULL);
1405 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1406 GNUNET_SERVER_add_handlers (server, handlers);
1408 = GNUNET_SCHEDULER_add_with_priority (sched,
1409 GNUNET_SCHEDULER_PRIORITY_IDLE,
1410 &delete_expired, NULL);
1411 GNUNET_SCHEDULER_add_delayed (sched,
1412 GNUNET_TIME_UNIT_FOREVER_REL,
1413 &cleaning_task, NULL);
1419 * The main function for the datastore service.
1421 * @param argc number of arguments from the command line
1422 * @param argv command line arguments
1423 * @return 0 ok, 1 on error
1426 main (int argc, char *const *argv)
1431 GNUNET_SERVICE_run (argc,
1434 GNUNET_SERVICE_OPTION_NONE,
1435 &run, NULL)) ? 0 : 1;
1440 /* end of gnunet-service-datastore.c */