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_protocols.h"
30 #include "gnunet_statistics_service.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 * Handle for reporting statistics.
169 static struct GNUNET_STATISTICS_Handle *stats;
173 * Function called once the transmit operation has
174 * either failed or succeeded.
177 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
179 typedef void (*TransmitContinuation)(void *cls,
184 * Context for transmitting replies to clients.
186 struct TransmitCallbackContext
190 * We keep these in a doubly-linked list (for cleanup).
192 struct TransmitCallbackContext *next;
195 * We keep these in a doubly-linked list (for cleanup).
197 struct TransmitCallbackContext *prev;
200 * The message that we're asked to transmit.
202 struct GNUNET_MessageHeader *msg;
205 * Handle for the transmission request.
207 struct GNUNET_CONNECTION_TransmitHandle *th;
210 * Client that we are transmitting to.
212 struct GNUNET_SERVER_Client *client;
215 * Function to call once msg has been transmitted
216 * (or at least added to the buffer).
218 TransmitContinuation tc;
226 * GNUNET_YES if we are supposed to signal the server
227 * completion of the client's request.
234 * Head of the doubly-linked list (for cleanup).
236 static struct TransmitCallbackContext *tcc_head;
239 * Tail of the doubly-linked list (for cleanup).
241 static struct TransmitCallbackContext *tcc_tail;
244 * Have we already cleaned up the TCCs and are hence no longer
245 * willing (or able) to transmit anything to anyone?
247 static int cleaning_done;
250 * Task that is used to remove expired entries from
251 * the datastore. This task will schedule itself
252 * again automatically to always delete all expired
255 * @param cls not used
256 * @param tc task context
259 delete_expired (void *cls,
260 const struct GNUNET_SCHEDULER_TaskContext *tc);
264 * Iterate over the expired items stored in the datastore.
265 * Delete all expired items; once we have processed all
266 * expired items, re-schedule the "delete_expired" task.
268 * @param cls not used
269 * @param next_cls closure to pass to the "next" function.
270 * @param key key for the content
271 * @param size number of bytes in data
272 * @param data content stored
273 * @param type type of the content
274 * @param priority priority of the content
275 * @param anonymity anonymity-level for the content
276 * @param expiration expiration time for the content
277 * @param uid unique identifier for the datum;
278 * maybe 0 if no unique identifier is available
280 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
281 * (continue on call to "next", of course),
282 * GNUNET_NO to delete the item and continue (if supported)
285 expired_processor (void *cls,
287 const GNUNET_HashCode * key,
290 enum GNUNET_BLOCK_Type type,
293 struct GNUNET_TIME_Absolute
297 struct GNUNET_TIME_Absolute now;
302 = GNUNET_SCHEDULER_add_delayed (sched,
306 return GNUNET_SYSERR;
308 now = GNUNET_TIME_absolute_get ();
309 if (expiration.value > now.value)
311 /* finished processing */
312 plugin->api->next_request (next_cls, GNUNET_YES);
313 return GNUNET_SYSERR;
315 plugin->api->next_request (next_cls, GNUNET_NO);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "Deleting content that expired %llu ms ago\n",
319 (unsigned long long) (now.value - expiration.value));
321 GNUNET_STATISTICS_update (stats,
322 gettext_noop ("# bytes expired"),
325 GNUNET_CONTAINER_bloomfilter_remove (filter,
327 return GNUNET_NO; /* delete */
332 * Task that is used to remove expired entries from
333 * the datastore. This task will schedule itself
334 * again automatically to always delete all expired
337 * @param cls not used
338 * @param tc task context
341 delete_expired (void *cls,
342 const struct GNUNET_SCHEDULER_TaskContext *tc)
344 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
345 plugin->api->iter_ascending_expiration (plugin->api->cls,
353 * An iterator over a set of items stored in the datastore.
356 * @param next_cls closure to pass to the "next" function.
357 * @param key key for the content
358 * @param size number of bytes in data
359 * @param data content stored
360 * @param type type of the content
361 * @param priority priority of the content
362 * @param anonymity anonymity-level for the content
363 * @param expiration expiration time for the content
364 * @param uid unique identifier for the datum;
365 * maybe 0 if no unique identifier is available
367 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
368 * (continue on call to "next", of course),
369 * GNUNET_NO to delete the item and continue (if supported)
374 const GNUNET_HashCode * key,
377 enum GNUNET_BLOCK_Type type,
380 struct GNUNET_TIME_Absolute
384 unsigned long long *need = cls;
389 return GNUNET_SYSERR;
391 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
394 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
395 plugin->api->next_request (next_cls,
396 (0 == *need) ? GNUNET_YES : GNUNET_NO);
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Deleting %llu bytes of low-priority content (still trying to free another %llu bytes)\n",
400 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
403 GNUNET_STATISTICS_update (stats,
404 gettext_noop ("# bytes purged (low-priority)"),
407 GNUNET_CONTAINER_bloomfilter_remove (filter,
414 * Manage available disk space by running tasks
415 * that will discard content if necessary. This
416 * function will be run whenever a request for
417 * "need" bytes of storage could only be satisfied
418 * by eating into the "cache" (and we want our cache
421 * @param need number of bytes of content that were
422 * placed into the "cache" (and hence the
423 * number of bytes that should be removed).
426 manage_space (unsigned long long need)
428 unsigned long long *n;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Asked to free up %llu bytes of cache space\n",
435 n = GNUNET_malloc (sizeof(unsigned long long));
437 plugin->api->iter_low_priority (plugin->api->cls,
445 * Function called to notify a client about the socket
446 * begin ready to queue more data. "buf" will be
447 * NULL and "size" zero if the socket was closed for
448 * writing in the meantime.
451 * @param size number of bytes available in buf
452 * @param buf where the callee should write the message
453 * @return number of bytes written to buf
456 transmit_callback (void *cls,
457 size_t size, void *buf)
459 struct TransmitCallbackContext *tcc = cls;
463 GNUNET_CONTAINER_DLL_remove (tcc_head,
466 msize = ntohs(tcc->msg->size);
470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
471 "Transmission failed.\n");
474 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
475 if (GNUNET_YES == tcc->end)
476 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
477 GNUNET_SERVER_client_drop (tcc->client);
478 GNUNET_free (tcc->msg);
482 GNUNET_assert (size >= msize);
483 memcpy (buf, tcc->msg, msize);
485 tcc->tc (tcc->tc_cls, GNUNET_OK);
486 if (GNUNET_YES == tcc->end)
488 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Response transmitted, more pending!\n");
497 GNUNET_SERVER_client_drop (tcc->client);
498 GNUNET_free (tcc->msg);
505 * Transmit the given message to the client.
507 * @param client target of the message
508 * @param msg message to transmit, will be freed!
509 * @param tc function to call afterwards
510 * @param tc_cls closure for tc
511 * @param end is this the last response (and we should
512 * signal the server completion accodingly after
513 * transmitting this message)?
516 transmit (struct GNUNET_SERVER_Client *client,
517 struct GNUNET_MessageHeader *msg,
518 TransmitContinuation tc,
522 struct TransmitCallbackContext *tcc;
524 if (GNUNET_YES == cleaning_done)
527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
528 "Shutdown in progress, aborting transmission.\n");
532 tc (tc_cls, GNUNET_SYSERR);
535 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
537 tcc->client = client;
539 tcc->tc_cls = tc_cls;
542 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
544 GNUNET_TIME_UNIT_FOREVER_REL,
549 if (GNUNET_YES == end)
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 "Disconnecting client.\n");
555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558 tc (tc_cls, GNUNET_SYSERR);
563 GNUNET_SERVER_client_keep (client);
564 GNUNET_CONTAINER_DLL_insert (tcc_head,
571 * Transmit a status code to the client.
573 * @param client receiver of the response
574 * @param code status code
575 * @param msg optional error message (can be NULL)
578 transmit_status (struct GNUNET_SERVER_Client *client,
582 struct StatusMessage *sm;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Transmitting `%s' message with value %d and message `%s'\n",
590 msg != NULL ? msg : "(none)");
592 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
593 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
594 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
595 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
596 sm->status = htonl(code);
598 memcpy (&sm[1], msg, slen);
599 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
604 * Function called once the transmit operation has
605 * either failed or succeeded.
607 * @param next_cls closure for calling "next_request" callback
608 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
611 get_next(void *next_cls,
614 if (status != GNUNET_OK)
616 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
617 _("Failed to transmit an item to the client; aborting iteration.\n"));
619 plugin->api->next_request (next_cls, GNUNET_YES);
622 plugin->api->next_request (next_cls, GNUNET_NO);
627 * Function that will transmit the given datastore entry
630 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
631 * @param next_cls closure to use to ask for the next item
632 * @param key key for the content
633 * @param size number of bytes in data
634 * @param data content stored
635 * @param type type of the content
636 * @param priority priority of the content
637 * @param anonymity anonymity-level for the content
638 * @param expiration expiration time for the content
639 * @param uid unique identifier for the datum;
640 * maybe 0 if no unique identifier is available
642 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
643 * GNUNET_NO to delete the item and continue (if supported)
646 transmit_item (void *cls,
648 const GNUNET_HashCode * key,
651 enum GNUNET_BLOCK_Type type,
654 struct GNUNET_TIME_Absolute
655 expiration, uint64_t uid)
657 struct GNUNET_SERVER_Client *client = cls;
658 struct GNUNET_MessageHeader *end;
659 struct DataMessage *dm;
663 /* transmit 'DATA_END' */
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Transmitting `%s' message\n",
669 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
670 end->size = htons(sizeof(struct GNUNET_MessageHeader));
671 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
672 transmit (client, end, NULL, NULL, GNUNET_YES);
673 GNUNET_SERVER_client_drop (client);
676 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
677 dm->header.size = htons(sizeof(struct DataMessage) + size);
678 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
680 dm->size = htonl(size);
681 dm->type = htonl(type);
682 dm->priority = htonl(priority);
683 dm->anonymity = htonl(anonymity);
684 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
685 dm->uid = GNUNET_htonll(uid);
687 memcpy (&dm[1], data, size);
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Transmitting `%s' message\n",
693 GNUNET_STATISTICS_update (stats,
694 gettext_noop ("# results found"),
697 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
703 * Handle RESERVE-message.
706 * @param client identification of the client
707 * @param message the actual message
710 handle_reserve (void *cls,
711 struct GNUNET_SERVER_Client *client,
712 const struct GNUNET_MessageHeader *message)
714 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
715 struct ReservationList *e;
716 unsigned long long used;
717 unsigned long long req;
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Processing `%s' request\n",
726 amount = GNUNET_ntohll(msg->amount);
727 entries = ntohl(msg->entries);
728 used = plugin->api->get_size (plugin->api->cls) + reserved;
729 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
730 if (used + req > quota)
733 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
735 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
739 if (cache_size < req)
741 /* TODO: document this in the FAQ; essentially, if this
742 message happens, the insertion request could be blocked
743 by less-important content from migration because it is
744 larger than 1/8th of the overall available space, and
745 we only reserve 1/8th for "fresh" insertions */
746 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
747 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
750 transmit_status (client, 0,
751 gettext_noop ("Insufficient space to satisfy request and "
752 "requested amount is larger than cache size"));
756 transmit_status (client, 0,
757 gettext_noop ("Insufficient space to satisfy request"));
762 e = GNUNET_malloc (sizeof(struct ReservationList));
763 e->next = reservations;
767 e->entries = entries;
768 e->rid = ++reservation_gen;
769 if (reservation_gen < 0)
770 reservation_gen = 0; /* wrap around */
771 transmit_status (client, e->rid, NULL);
776 * Handle RELEASE_RESERVE-message.
779 * @param client identification of the client
780 * @param message the actual message
783 handle_release_reserve (void *cls,
784 struct GNUNET_SERVER_Client *client,
785 const struct GNUNET_MessageHeader *message)
787 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
788 struct ReservationList *pos;
789 struct ReservationList *prev;
790 struct ReservationList *next;
791 int rid = ntohl(msg->rid);
792 unsigned long long rem;
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Processing `%s' request\n",
801 while (NULL != (pos = next))
810 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
811 GNUNET_assert (reserved >= rem);
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Returning %llu remaining reserved bytes to storage pool\n",
819 transmit_status (client, GNUNET_OK, NULL);
825 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
830 * Check that the given message is a valid data message.
832 * @return NULL if the message is not well-formed, otherwise the message
834 static const struct DataMessage *
835 check_data (const struct GNUNET_MessageHeader *message)
839 const struct DataMessage *dm;
841 size = ntohs(message->size);
842 if (size < sizeof(struct DataMessage))
847 dm = (const struct DataMessage *) message;
848 dsize = ntohl(dm->size);
849 if (size != dsize + sizeof(struct DataMessage))
859 * Handle PUT-message.
862 * @param client identification of the client
863 * @param message the actual message
866 handle_put (void *cls,
867 struct GNUNET_SERVER_Client *client,
868 const struct GNUNET_MessageHeader *message)
870 const struct DataMessage *dm = check_data (message);
874 struct ReservationList *pos;
878 (ntohl(dm->type) == 0) )
881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Processing `%s' request for `%s'\n",
888 GNUNET_h2s (&dm->key));
890 rid = ntohl(dm->rid);
891 size = ntohl(dm->size);
895 while ( (NULL != pos) &&
898 GNUNET_break (pos != NULL);
901 GNUNET_break (pos->entries > 0);
902 GNUNET_break (pos->amount > size);
905 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
909 ret = plugin->api->put (plugin->api->cls,
915 ntohl(dm->anonymity),
916 GNUNET_TIME_absolute_ntoh(dm->expiration),
918 if (GNUNET_OK == ret)
920 GNUNET_STATISTICS_update (stats,
921 gettext_noop ("# bytes stored"),
924 GNUNET_CONTAINER_bloomfilter_add (filter,
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Successfully stored %u bytes under key `%s'\n",
930 GNUNET_h2s (&dm->key));
933 transmit_status (client,
934 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
936 GNUNET_free_non_null (msg);
937 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
938 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
943 * Handle GET-message.
946 * @param client identification of the client
947 * @param message the actual message
950 handle_get (void *cls,
951 struct GNUNET_SERVER_Client *client,
952 const struct GNUNET_MessageHeader *message)
954 const struct GetMessage *msg;
957 size = ntohs(message->size);
958 if ( (size != sizeof(struct GetMessage)) &&
959 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
962 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
965 msg = (const struct GetMessage*) message;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Processing `%s' request for `%s' of type %u\n",
970 GNUNET_h2s (&msg->key),
973 GNUNET_STATISTICS_update (stats,
974 gettext_noop ("# GET requests received"),
977 GNUNET_SERVER_client_keep (client);
978 if ( (size == sizeof(struct GetMessage)) &&
979 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
982 /* don't bother database... */
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Empty result set for `%s' request for `%s'.\n",
987 GNUNET_h2s (&msg->key));
989 GNUNET_STATISTICS_update (stats,
990 gettext_noop ("# requests filtered by bloomfilter"),
993 transmit_item (client,
994 NULL, NULL, 0, NULL, 0, 0, 0,
995 GNUNET_TIME_UNIT_ZERO_ABS, 0);
998 plugin->api->get (plugin->api->cls,
999 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1008 * Handle UPDATE-message.
1010 * @param cls closure
1011 * @param client identification of the client
1012 * @param message the actual message
1015 handle_update (void *cls,
1016 struct GNUNET_SERVER_Client *client,
1017 const struct GNUNET_MessageHeader *message)
1019 const struct UpdateMessage *msg;
1023 GNUNET_STATISTICS_update (stats,
1024 gettext_noop ("# UPDATE requests received"),
1027 msg = (const struct UpdateMessage*) message;
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Processing `%s' request for %llu\n",
1033 (unsigned long long) GNUNET_ntohll (msg->uid));
1035 ret = plugin->api->update (plugin->api->cls,
1036 GNUNET_ntohll(msg->uid),
1037 (int32_t) ntohl(msg->priority),
1038 GNUNET_TIME_absolute_ntoh(msg->expiration),
1040 transmit_status (client, ret, emsg);
1041 GNUNET_free_non_null (emsg);
1046 * Handle GET_RANDOM-message.
1048 * @param cls closure
1049 * @param client identification of the client
1050 * @param message the actual message
1053 handle_get_random (void *cls,
1054 struct GNUNET_SERVER_Client *client,
1055 const struct GNUNET_MessageHeader *message)
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1059 "Processing `%s' request\n",
1062 GNUNET_STATISTICS_update (stats,
1063 gettext_noop ("# GET RANDOM requests received"),
1066 GNUNET_SERVER_client_keep (client);
1067 plugin->api->iter_migration_order (plugin->api->cls,
1075 * Context for the 'remove_callback'.
1077 struct RemoveContext
1080 * Client for whom we're doing the remvoing.
1082 struct GNUNET_SERVER_Client *client;
1085 * GNUNET_YES if we managed to remove something.
1092 * Callback function that will cause the item that is passed
1093 * in to be deleted (by returning GNUNET_NO).
1096 remove_callback (void *cls,
1098 const GNUNET_HashCode * key,
1101 enum GNUNET_BLOCK_Type type,
1104 struct GNUNET_TIME_Absolute
1105 expiration, uint64_t uid)
1107 struct RemoveContext *rc = cls;
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1113 "No further matches for `%s' request.\n",
1116 if (GNUNET_YES == rc->found)
1117 transmit_status (rc->client, GNUNET_OK, NULL);
1119 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1120 GNUNET_SERVER_client_drop (rc->client);
1122 return GNUNET_OK; /* last item */
1124 rc->found = GNUNET_YES;
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Item %llu matches `%s' request for key `%s'.\n",
1128 (unsigned long long) uid,
1132 GNUNET_STATISTICS_update (stats,
1133 gettext_noop ("# bytes removed (explicit request)"),
1136 GNUNET_CONTAINER_bloomfilter_remove (filter,
1138 plugin->api->next_request (next_cls, GNUNET_YES);
1144 * Handle REMOVE-message.
1146 * @param cls closure
1147 * @param client identification of the client
1148 * @param message the actual message
1151 handle_remove (void *cls,
1152 struct GNUNET_SERVER_Client *client,
1153 const struct GNUNET_MessageHeader *message)
1155 const struct DataMessage *dm = check_data (message);
1156 GNUNET_HashCode vhash;
1157 struct RemoveContext *rc;
1162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Processing `%s' request for `%s'\n",
1169 GNUNET_h2s (&dm->key));
1171 GNUNET_STATISTICS_update (stats,
1172 gettext_noop ("# REMOVE requests received"),
1175 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1176 GNUNET_SERVER_client_keep (client);
1177 rc->client = client;
1178 GNUNET_CRYPTO_hash (&dm[1],
1181 plugin->api->get (plugin->api->cls,
1191 * Handle DROP-message.
1193 * @param cls closure
1194 * @param client identification of the client
1195 * @param message the actual message
1198 handle_drop (void *cls,
1199 struct GNUNET_SERVER_Client *client,
1200 const struct GNUNET_MessageHeader *message)
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Processing `%s' request\n",
1207 plugin->api->drop (plugin->api->cls);
1208 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1213 * List of handlers for the messages understood by this
1216 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1217 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1218 sizeof(struct ReserveMessage) },
1219 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1220 sizeof(struct ReleaseReserveMessage) },
1221 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1222 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1223 sizeof (struct UpdateMessage) },
1224 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1225 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1226 sizeof(struct GNUNET_MessageHeader) },
1227 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1228 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1229 sizeof(struct GNUNET_MessageHeader) },
1236 * Load the datastore plugin.
1238 static struct DatastorePlugin *
1241 struct DatastorePlugin *ret;
1246 GNUNET_CONFIGURATION_get_value_string (cfg,
1247 "DATASTORE", "DATABASE", &name))
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1250 _("No `%s' specified for `%s' in configuration!\n"),
1255 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1257 ret->env.sched = sched;
1258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1259 _("Loading `%s' datastore plugin\n"), name);
1260 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1261 ret->short_name = name;
1262 ret->lib_name = libname;
1263 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1264 if (ret->api == NULL)
1266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1267 _("Failed to load datastore plugin for `%s'\n"), name);
1268 GNUNET_free (ret->short_name);
1269 GNUNET_free (libname);
1278 * Function called when the service shuts
1279 * down. Unloads our datastore plugin.
1281 * @param plug plugin to unload
1284 unload_plugin (struct DatastorePlugin *plug)
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Datastore service is unloading plugin...\n");
1290 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1291 GNUNET_free (plug->lib_name);
1292 GNUNET_free (plug->short_name);
1298 * Final task run after shutdown. Unloads plugins and disconnects us from
1302 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1304 unload_plugin (plugin);
1308 GNUNET_CONTAINER_bloomfilter_free (filter);
1313 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1320 * Last task run during shutdown. Disconnects us from
1321 * the transport and core.
1324 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1326 struct TransmitCallbackContext *tcc;
1328 cleaning_done = GNUNET_YES;
1329 while (NULL != (tcc = tcc_head))
1331 GNUNET_CONTAINER_DLL_remove (tcc_head,
1334 if (tcc->th != NULL)
1336 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1337 GNUNET_SERVER_client_drop (tcc->client);
1339 if (NULL != tcc->tc)
1340 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1341 GNUNET_free (tcc->msg);
1344 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1346 GNUNET_SCHEDULER_cancel (sched,
1348 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1350 GNUNET_SCHEDULER_add_continuation (sched,
1353 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1358 * Function that removes all active reservations made
1359 * by the given client and releases the space for other
1362 * @param cls closure
1363 * @param client identification of the client
1366 cleanup_reservations (void *cls,
1367 struct GNUNET_SERVER_Client
1370 struct ReservationList *pos;
1371 struct ReservationList *prev;
1372 struct ReservationList *next;
1381 if (pos->client == client)
1384 reservations = next;
1387 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1400 * Process datastore requests.
1402 * @param cls closure
1403 * @param s scheduler to use
1404 * @param server the initialized server
1405 * @param c configuration to use
1409 struct GNUNET_SCHEDULER_Handle *s,
1410 struct GNUNET_SERVER_Handle *server,
1411 const struct GNUNET_CONFIGURATION_Handle *c)
1414 unsigned int bf_size;
1419 GNUNET_CONFIGURATION_get_value_number (cfg,
1420 "DATASTORE", "QUOTA", "a))
1422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1423 _("No `%s' specified for `%s' in configuration!\n"),
1428 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1429 cache_size = quota / 8; /* Or should we make this an option? */
1430 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1433 GNUNET_CONFIGURATION_get_value_filename (cfg,
1438 GNUNET_DISK_directory_create_for_file (fn)) )
1440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1441 _("Could not use specified filename `%s' for bloomfilter.\n"),
1442 fn != NULL ? fn : "");
1443 GNUNET_free_non_null (fn);
1446 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1447 GNUNET_free_non_null (fn);
1450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1451 _("Failed to initialize bloomfilter.\n"));
1454 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1459 plugin = load_plugin ();
1462 GNUNET_CONTAINER_bloomfilter_free (filter);
1466 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1471 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1472 GNUNET_SERVER_add_handlers (server, handlers);
1474 = GNUNET_SCHEDULER_add_with_priority (sched,
1475 GNUNET_SCHEDULER_PRIORITY_IDLE,
1476 &delete_expired, NULL);
1477 GNUNET_SCHEDULER_add_delayed (sched,
1478 GNUNET_TIME_UNIT_FOREVER_REL,
1479 &cleaning_task, NULL);
1485 * The main function for the datastore service.
1487 * @param argc number of arguments from the command line
1488 * @param argv command line arguments
1489 * @return 0 ok, 1 on error
1492 main (int argc, char *const *argv)
1497 GNUNET_SERVICE_run (argc,
1500 GNUNET_SERVICE_OPTION_NONE,
1501 &run, NULL)) ? 0 : 1;
1506 /* end of gnunet-service-datastore.c */