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 * How much space are we allowed to use?
130 static unsigned long long quota;
133 * How much space are we using for the cache? (space available for
134 * insertions that will be instantly reclaimed by discarding less
135 * important content --- or possibly whatever we just inserted into
138 static unsigned long long cache_size;
141 * How much space have we currently reserved?
143 static unsigned long long reserved;
146 * Identity of the task that is used to delete
149 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
154 const struct GNUNET_CONFIGURATION_Handle *cfg;
159 struct GNUNET_SCHEDULER_Handle *sched;
162 * Handle for reporting statistics.
164 static struct GNUNET_STATISTICS_Handle *stats;
168 * Function called once the transmit operation has
169 * either failed or succeeded.
172 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
174 typedef void (*TransmitContinuation)(void *cls,
179 * Context for transmitting replies to clients.
181 struct TransmitCallbackContext
185 * We keep these in a doubly-linked list (for cleanup).
187 struct TransmitCallbackContext *next;
190 * We keep these in a doubly-linked list (for cleanup).
192 struct TransmitCallbackContext *prev;
195 * The message that we're asked to transmit.
197 struct GNUNET_MessageHeader *msg;
200 * Handle for the transmission request.
202 struct GNUNET_CONNECTION_TransmitHandle *th;
205 * Client that we are transmitting to.
207 struct GNUNET_SERVER_Client *client;
210 * Function to call once msg has been transmitted
211 * (or at least added to the buffer).
213 TransmitContinuation tc;
221 * GNUNET_YES if we are supposed to signal the server
222 * completion of the client's request.
229 * Head of the doubly-linked list (for cleanup).
231 static struct TransmitCallbackContext *tcc_head;
234 * Tail of the doubly-linked list (for cleanup).
236 static struct TransmitCallbackContext *tcc_tail;
239 * Have we already cleaned up the TCCs and are hence no longer
240 * willing (or able) to transmit anything to anyone?
242 static int cleaning_done;
245 * Task that is used to remove expired entries from
246 * the datastore. This task will schedule itself
247 * again automatically to always delete all expired
250 * @param cls not used
251 * @param tc task context
254 delete_expired (void *cls,
255 const struct GNUNET_SCHEDULER_TaskContext *tc);
259 * Iterate over the expired items stored in the datastore.
260 * Delete all expired items; once we have processed all
261 * expired items, re-schedule the "delete_expired" task.
263 * @param cls not used
264 * @param next_cls closure to pass to the "next" function.
265 * @param key key for the content
266 * @param size number of bytes in data
267 * @param data content stored
268 * @param type type of the content
269 * @param priority priority of the content
270 * @param anonymity anonymity-level for the content
271 * @param expiration expiration time for the content
272 * @param uid unique identifier for the datum;
273 * maybe 0 if no unique identifier is available
275 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
276 * (continue on call to "next", of course),
277 * GNUNET_NO to delete the item and continue (if supported)
280 expired_processor (void *cls,
282 const GNUNET_HashCode * key,
285 enum GNUNET_BLOCK_Type type,
288 struct GNUNET_TIME_Absolute
292 struct GNUNET_TIME_Absolute now;
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 `%s' of type %u that expired %llu ms ago\n",
316 (unsigned long long) (now.value - expiration.value));
318 GNUNET_STATISTICS_update (stats,
319 gettext_noop ("# bytes expired"),
322 GNUNET_CONTAINER_bloomfilter_remove (filter,
324 return GNUNET_NO; /* delete */
329 * Task that is used to remove expired entries from
330 * the datastore. This task will schedule itself
331 * again automatically to always delete all expired
334 * @param cls not used
335 * @param tc task context
338 delete_expired (void *cls,
339 const struct GNUNET_SCHEDULER_TaskContext *tc)
341 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
342 plugin->api->iter_ascending_expiration (plugin->api->cls,
350 * An iterator over a set of items stored in the datastore.
353 * @param next_cls closure to pass to the "next" function.
354 * @param key key for the content
355 * @param size number of bytes in data
356 * @param data content stored
357 * @param type type of the content
358 * @param priority priority of the content
359 * @param anonymity anonymity-level for the content
360 * @param expiration expiration time for the content
361 * @param uid unique identifier for the datum;
362 * maybe 0 if no unique identifier is available
364 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
365 * (continue on call to "next", of course),
366 * GNUNET_NO to delete the item and continue (if supported)
371 const GNUNET_HashCode * key,
374 enum GNUNET_BLOCK_Type type,
377 struct GNUNET_TIME_Absolute
381 unsigned long long *need = cls;
386 return GNUNET_SYSERR;
388 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
391 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
392 plugin->api->next_request (next_cls,
393 (0 == *need) ? GNUNET_YES : GNUNET_NO);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
397 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
402 GNUNET_STATISTICS_update (stats,
403 gettext_noop ("# bytes purged (low-priority)"),
406 GNUNET_CONTAINER_bloomfilter_remove (filter,
413 * Manage available disk space by running tasks
414 * that will discard content if necessary. This
415 * function will be run whenever a request for
416 * "need" bytes of storage could only be satisfied
417 * by eating into the "cache" (and we want our cache
420 * @param need number of bytes of content that were
421 * placed into the "cache" (and hence the
422 * number of bytes that should be removed).
425 manage_space (unsigned long long need)
427 unsigned long long *n;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Asked to free up %llu bytes of cache space\n",
434 n = GNUNET_malloc (sizeof(unsigned long long));
436 plugin->api->iter_low_priority (plugin->api->cls,
444 * Function called to notify a client about the socket
445 * begin ready to queue more data. "buf" will be
446 * NULL and "size" zero if the socket was closed for
447 * writing in the meantime.
450 * @param size number of bytes available in buf
451 * @param buf where the callee should write the message
452 * @return number of bytes written to buf
455 transmit_callback (void *cls,
456 size_t size, void *buf)
458 struct TransmitCallbackContext *tcc = cls;
462 GNUNET_CONTAINER_DLL_remove (tcc_head,
465 msize = ntohs(tcc->msg->size);
469 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
470 "Transmission failed.\n");
473 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
474 if (GNUNET_YES == tcc->end)
475 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
476 GNUNET_SERVER_client_drop (tcc->client);
477 GNUNET_free (tcc->msg);
481 GNUNET_assert (size >= msize);
482 memcpy (buf, tcc->msg, msize);
484 tcc->tc (tcc->tc_cls, GNUNET_OK);
485 if (GNUNET_YES == tcc->end)
487 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Response transmitted, more pending!\n");
496 GNUNET_SERVER_client_drop (tcc->client);
497 GNUNET_free (tcc->msg);
504 * Transmit the given message to the client.
506 * @param client target of the message
507 * @param msg message to transmit, will be freed!
508 * @param tc function to call afterwards
509 * @param tc_cls closure for tc
510 * @param end is this the last response (and we should
511 * signal the server completion accodingly after
512 * transmitting this message)?
515 transmit (struct GNUNET_SERVER_Client *client,
516 struct GNUNET_MessageHeader *msg,
517 TransmitContinuation tc,
521 struct TransmitCallbackContext *tcc;
523 if (GNUNET_YES == cleaning_done)
526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
527 "Shutdown in progress, aborting transmission.\n");
531 tc (tc_cls, GNUNET_SYSERR);
534 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
536 tcc->client = client;
538 tcc->tc_cls = tc_cls;
541 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
543 GNUNET_TIME_UNIT_FOREVER_REL,
548 if (GNUNET_YES == end)
551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
552 "Disconnecting client.\n");
554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
557 tc (tc_cls, GNUNET_SYSERR);
562 GNUNET_SERVER_client_keep (client);
563 GNUNET_CONTAINER_DLL_insert (tcc_head,
570 * Transmit a status code to the client.
572 * @param client receiver of the response
573 * @param code status code
574 * @param msg optional error message (can be NULL)
577 transmit_status (struct GNUNET_SERVER_Client *client,
581 struct StatusMessage *sm;
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Transmitting `%s' message with value %d and message `%s'\n",
589 msg != NULL ? msg : "(none)");
591 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
592 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
593 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
594 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
595 sm->status = htonl(code);
597 memcpy (&sm[1], msg, slen);
598 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
603 * Function called once the transmit operation has
604 * either failed or succeeded.
606 * @param next_cls closure for calling "next_request" callback
607 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
610 get_next(void *next_cls,
613 if (status != GNUNET_OK)
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 _("Failed to transmit an item to the client; aborting iteration.\n"));
618 plugin->api->next_request (next_cls, GNUNET_YES);
621 plugin->api->next_request (next_cls, GNUNET_NO);
626 * Function that will transmit the given datastore entry
629 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
630 * @param next_cls closure to use to ask for the next item
631 * @param key key for the content
632 * @param size number of bytes in data
633 * @param data content stored
634 * @param type type of the content
635 * @param priority priority of the content
636 * @param anonymity anonymity-level for the content
637 * @param expiration expiration time for the content
638 * @param uid unique identifier for the datum;
639 * maybe 0 if no unique identifier is available
641 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
642 * GNUNET_NO to delete the item and continue (if supported)
645 transmit_item (void *cls,
647 const GNUNET_HashCode * key,
650 enum GNUNET_BLOCK_Type type,
653 struct GNUNET_TIME_Absolute
654 expiration, uint64_t uid)
656 struct GNUNET_SERVER_Client *client = cls;
657 struct GNUNET_MessageHeader *end;
658 struct DataMessage *dm;
662 /* transmit 'DATA_END' */
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Transmitting `%s' message\n",
668 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
669 end->size = htons(sizeof(struct GNUNET_MessageHeader));
670 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
671 transmit (client, end, NULL, NULL, GNUNET_YES);
672 GNUNET_SERVER_client_drop (client);
675 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
676 dm->header.size = htons(sizeof(struct DataMessage) + size);
677 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
679 dm->size = htonl(size);
680 dm->type = htonl(type);
681 dm->priority = htonl(priority);
682 dm->anonymity = htonl(anonymity);
683 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
684 dm->uid = GNUNET_htonll(uid);
686 memcpy (&dm[1], data, size);
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
689 "Transmitting `%s' message for `%s' of type %u\n",
694 GNUNET_STATISTICS_update (stats,
695 gettext_noop ("# results found"),
698 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
704 * Handle RESERVE-message.
707 * @param client identification of the client
708 * @param message the actual message
711 handle_reserve (void *cls,
712 struct GNUNET_SERVER_Client *client,
713 const struct GNUNET_MessageHeader *message)
716 * Static counter to produce reservation identifiers.
718 static int reservation_gen;
720 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
721 struct ReservationList *e;
722 unsigned long long used;
723 unsigned long long req;
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Processing `%s' request\n",
732 amount = GNUNET_ntohll(msg->amount);
733 entries = ntohl(msg->entries);
734 used = plugin->api->get_size (plugin->api->cls) + reserved;
735 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
736 if (used + req > quota)
739 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
741 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
745 if (cache_size < req)
747 /* TODO: document this in the FAQ; essentially, if this
748 message happens, the insertion request could be blocked
749 by less-important content from migration because it is
750 larger than 1/8th of the overall available space, and
751 we only reserve 1/8th for "fresh" insertions */
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
756 transmit_status (client, 0,
757 gettext_noop ("Insufficient space to satisfy request and "
758 "requested amount is larger than cache size"));
762 transmit_status (client, 0,
763 gettext_noop ("Insufficient space to satisfy request"));
768 GNUNET_STATISTICS_set (stats,
769 gettext_noop ("# reserved"),
772 e = GNUNET_malloc (sizeof(struct ReservationList));
773 e->next = reservations;
777 e->entries = entries;
778 e->rid = ++reservation_gen;
779 if (reservation_gen < 0)
780 reservation_gen = 0; /* wrap around */
781 transmit_status (client, e->rid, NULL);
786 * Handle RELEASE_RESERVE-message.
789 * @param client identification of the client
790 * @param message the actual message
793 handle_release_reserve (void *cls,
794 struct GNUNET_SERVER_Client *client,
795 const struct GNUNET_MessageHeader *message)
797 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
798 struct ReservationList *pos;
799 struct ReservationList *prev;
800 struct ReservationList *next;
801 int rid = ntohl(msg->rid);
802 unsigned long long rem;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Processing `%s' request\n",
811 while (NULL != (pos = next))
820 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
821 GNUNET_assert (reserved >= rem);
823 GNUNET_STATISTICS_set (stats,
824 gettext_noop ("# reserved"),
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Returning %llu remaining reserved bytes to storage pool\n",
833 transmit_status (client, GNUNET_OK, NULL);
839 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
844 * Check that the given message is a valid data message.
846 * @return NULL if the message is not well-formed, otherwise the message
848 static const struct DataMessage *
849 check_data (const struct GNUNET_MessageHeader *message)
853 const struct DataMessage *dm;
855 size = ntohs(message->size);
856 if (size < sizeof(struct DataMessage))
861 dm = (const struct DataMessage *) message;
862 dsize = ntohl(dm->size);
863 if (size != dsize + sizeof(struct DataMessage))
873 * Handle PUT-message.
876 * @param client identification of the client
877 * @param message the actual message
880 handle_put (void *cls,
881 struct GNUNET_SERVER_Client *client,
882 const struct GNUNET_MessageHeader *message)
884 const struct DataMessage *dm = check_data (message);
888 struct ReservationList *pos;
892 (ntohl(dm->type) == 0) )
895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Processing `%s' request for `%s' of type %u\n",
902 GNUNET_h2s (&dm->key),
905 rid = ntohl(dm->rid);
906 size = ntohl(dm->size);
910 while ( (NULL != pos) &&
913 GNUNET_break (pos != NULL);
916 GNUNET_break (pos->entries > 0);
917 GNUNET_break (pos->amount > size);
920 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
921 GNUNET_STATISTICS_set (stats,
922 gettext_noop ("# reserved"),
928 ret = plugin->api->put (plugin->api->cls,
934 ntohl(dm->anonymity),
935 GNUNET_TIME_absolute_ntoh(dm->expiration),
937 if (GNUNET_OK == ret)
939 GNUNET_STATISTICS_update (stats,
940 gettext_noop ("# bytes stored"),
943 GNUNET_CONTAINER_bloomfilter_add (filter,
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Successfully stored %u bytes of type %u under key `%s'\n",
950 GNUNET_h2s (&dm->key));
953 transmit_status (client,
954 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
956 GNUNET_free_non_null (msg);
957 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
959 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
960 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
961 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
962 (unsigned long long) (quota - reserved - cache_size),
963 (unsigned long long) plugin->api->get_size (plugin->api->cls));
964 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
970 * Handle GET-message.
973 * @param client identification of the client
974 * @param message the actual message
977 handle_get (void *cls,
978 struct GNUNET_SERVER_Client *client,
979 const struct GNUNET_MessageHeader *message)
981 const struct GetMessage *msg;
984 size = ntohs(message->size);
985 if ( (size != sizeof(struct GetMessage)) &&
986 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
992 msg = (const struct GetMessage*) message;
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "Processing `%s' request for `%s' of type %u\n",
997 GNUNET_h2s (&msg->key),
1000 GNUNET_STATISTICS_update (stats,
1001 gettext_noop ("# GET requests received"),
1004 GNUNET_SERVER_client_keep (client);
1005 if ( (size == sizeof(struct GetMessage)) &&
1006 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1009 /* don't bother database... */
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1014 GNUNET_h2s (&msg->key));
1016 GNUNET_STATISTICS_update (stats,
1017 gettext_noop ("# requests filtered by bloomfilter"),
1020 transmit_item (client,
1021 NULL, NULL, 0, NULL, 0, 0, 0,
1022 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1025 plugin->api->get (plugin->api->cls,
1026 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1035 * Handle UPDATE-message.
1037 * @param cls closure
1038 * @param client identification of the client
1039 * @param message the actual message
1042 handle_update (void *cls,
1043 struct GNUNET_SERVER_Client *client,
1044 const struct GNUNET_MessageHeader *message)
1046 const struct UpdateMessage *msg;
1050 GNUNET_STATISTICS_update (stats,
1051 gettext_noop ("# UPDATE requests received"),
1054 msg = (const struct UpdateMessage*) message;
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Processing `%s' request for %llu\n",
1060 (unsigned long long) GNUNET_ntohll (msg->uid));
1062 ret = plugin->api->update (plugin->api->cls,
1063 GNUNET_ntohll(msg->uid),
1064 (int32_t) ntohl(msg->priority),
1065 GNUNET_TIME_absolute_ntoh(msg->expiration),
1067 transmit_status (client, ret, emsg);
1068 GNUNET_free_non_null (emsg);
1073 * Handle GET_RANDOM-message.
1075 * @param cls closure
1076 * @param client identification of the client
1077 * @param message the actual message
1080 handle_get_random (void *cls,
1081 struct GNUNET_SERVER_Client *client,
1082 const struct GNUNET_MessageHeader *message)
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "Processing `%s' request\n",
1089 GNUNET_STATISTICS_update (stats,
1090 gettext_noop ("# GET RANDOM requests received"),
1093 GNUNET_SERVER_client_keep (client);
1094 plugin->api->iter_migration_order (plugin->api->cls,
1102 * Context for the 'remove_callback'.
1104 struct RemoveContext
1107 * Client for whom we're doing the remvoing.
1109 struct GNUNET_SERVER_Client *client;
1112 * GNUNET_YES if we managed to remove something.
1119 * Callback function that will cause the item that is passed
1120 * in to be deleted (by returning GNUNET_NO).
1123 remove_callback (void *cls,
1125 const GNUNET_HashCode * key,
1128 enum GNUNET_BLOCK_Type type,
1131 struct GNUNET_TIME_Absolute
1132 expiration, uint64_t uid)
1134 struct RemoveContext *rc = cls;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "No further matches for `%s' request.\n",
1143 if (GNUNET_YES == rc->found)
1144 transmit_status (rc->client, GNUNET_OK, NULL);
1146 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1147 GNUNET_SERVER_client_drop (rc->client);
1149 return GNUNET_OK; /* last item */
1151 rc->found = GNUNET_YES;
1153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1154 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1155 (unsigned long long) uid,
1160 GNUNET_STATISTICS_update (stats,
1161 gettext_noop ("# bytes removed (explicit request)"),
1164 GNUNET_CONTAINER_bloomfilter_remove (filter,
1166 plugin->api->next_request (next_cls, GNUNET_YES);
1172 * Handle REMOVE-message.
1174 * @param cls closure
1175 * @param client identification of the client
1176 * @param message the actual message
1179 handle_remove (void *cls,
1180 struct GNUNET_SERVER_Client *client,
1181 const struct GNUNET_MessageHeader *message)
1183 const struct DataMessage *dm = check_data (message);
1184 GNUNET_HashCode vhash;
1185 struct RemoveContext *rc;
1190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Processing `%s' request for `%s' of type %u\n",
1197 GNUNET_h2s (&dm->key),
1200 GNUNET_STATISTICS_update (stats,
1201 gettext_noop ("# REMOVE requests received"),
1204 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1205 GNUNET_SERVER_client_keep (client);
1206 rc->client = client;
1207 GNUNET_CRYPTO_hash (&dm[1],
1210 plugin->api->get (plugin->api->cls,
1220 * Handle DROP-message.
1222 * @param cls closure
1223 * @param client identification of the client
1224 * @param message the actual message
1227 handle_drop (void *cls,
1228 struct GNUNET_SERVER_Client *client,
1229 const struct GNUNET_MessageHeader *message)
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Processing `%s' request\n",
1236 plugin->api->drop (plugin->api->cls);
1237 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1242 * Load the datastore plugin.
1244 static struct DatastorePlugin *
1247 struct DatastorePlugin *ret;
1252 GNUNET_CONFIGURATION_get_value_string (cfg,
1253 "DATASTORE", "DATABASE", &name))
1255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1256 _("No `%s' specified for `%s' in configuration!\n"),
1261 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1263 ret->env.sched = sched;
1264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1265 _("Loading `%s' datastore plugin\n"), name);
1266 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1267 ret->short_name = name;
1268 ret->lib_name = libname;
1269 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1270 if (ret->api == NULL)
1272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1273 _("Failed to load datastore plugin for `%s'\n"), name);
1274 GNUNET_free (ret->short_name);
1275 GNUNET_free (libname);
1284 * Function called when the service shuts
1285 * down. Unloads our datastore plugin.
1287 * @param plug plugin to unload
1290 unload_plugin (struct DatastorePlugin *plug)
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "Datastore service is unloading plugin...\n");
1296 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1297 GNUNET_free (plug->lib_name);
1298 GNUNET_free (plug->short_name);
1304 * Final task run after shutdown. Unloads plugins and disconnects us from
1308 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1310 unload_plugin (plugin);
1314 GNUNET_CONTAINER_bloomfilter_free (filter);
1319 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1326 * Last task run during shutdown. Disconnects us from
1327 * the transport and core.
1330 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1332 struct TransmitCallbackContext *tcc;
1334 cleaning_done = GNUNET_YES;
1335 while (NULL != (tcc = tcc_head))
1337 GNUNET_CONTAINER_DLL_remove (tcc_head,
1340 if (tcc->th != NULL)
1342 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1343 GNUNET_SERVER_client_drop (tcc->client);
1345 if (NULL != tcc->tc)
1346 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1347 GNUNET_free (tcc->msg);
1350 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1352 GNUNET_SCHEDULER_cancel (sched,
1354 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1356 GNUNET_SCHEDULER_add_continuation (sched,
1359 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1364 * Function that removes all active reservations made
1365 * by the given client and releases the space for other
1368 * @param cls closure
1369 * @param client identification of the client
1372 cleanup_reservations (void *cls,
1373 struct GNUNET_SERVER_Client
1376 struct ReservationList *pos;
1377 struct ReservationList *prev;
1378 struct ReservationList *next;
1387 if (pos->client == client)
1390 reservations = next;
1393 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1402 GNUNET_STATISTICS_set (stats,
1403 gettext_noop ("# reserved"),
1410 * Process datastore requests.
1412 * @param cls closure
1413 * @param s scheduler to use
1414 * @param server the initialized server
1415 * @param c configuration to use
1419 struct GNUNET_SCHEDULER_Handle *s,
1420 struct GNUNET_SERVER_Handle *server,
1421 const struct GNUNET_CONFIGURATION_Handle *c)
1423 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1424 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1425 sizeof(struct ReserveMessage) },
1426 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1427 sizeof(struct ReleaseReserveMessage) },
1428 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1429 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1430 sizeof (struct UpdateMessage) },
1431 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1432 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1433 sizeof(struct GNUNET_MessageHeader) },
1434 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1435 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1436 sizeof(struct GNUNET_MessageHeader) },
1440 unsigned int bf_size;
1445 GNUNET_CONFIGURATION_get_value_number (cfg,
1446 "DATASTORE", "QUOTA", "a))
1448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1449 _("No `%s' specified for `%s' in configuration!\n"),
1454 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1455 GNUNET_STATISTICS_set (stats,
1456 gettext_noop ("# quota"),
1459 cache_size = quota / 8; /* Or should we make this an option? */
1460 GNUNET_STATISTICS_set (stats,
1461 gettext_noop ("# cache size"),
1464 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1467 GNUNET_CONFIGURATION_get_value_filename (cfg,
1472 GNUNET_DISK_directory_create_for_file (fn)) )
1474 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1475 _("Could not use specified filename `%s' for bloomfilter.\n"),
1476 fn != NULL ? fn : "");
1477 GNUNET_free_non_null (fn);
1480 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1481 GNUNET_free_non_null (fn);
1484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1485 _("Failed to initialize bloomfilter.\n"));
1488 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1493 plugin = load_plugin ();
1496 GNUNET_CONTAINER_bloomfilter_free (filter);
1500 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1505 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1506 GNUNET_SERVER_add_handlers (server, handlers);
1508 = GNUNET_SCHEDULER_add_with_priority (sched,
1509 GNUNET_SCHEDULER_PRIORITY_IDLE,
1510 &delete_expired, NULL);
1511 GNUNET_SCHEDULER_add_delayed (sched,
1512 GNUNET_TIME_UNIT_FOREVER_REL,
1513 &cleaning_task, NULL);
1519 * The main function for the datastore service.
1521 * @param argc number of arguments from the command line
1522 * @param argv command line arguments
1523 * @return 0 ok, 1 on error
1526 main (int argc, char *const *argv)
1531 GNUNET_SERVICE_run (argc,
1534 GNUNET_SERVICE_OPTION_NONE,
1535 &run, NULL)) ? 0 : 1;
1540 /* end of gnunet-service-datastore.c */