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 clean ed 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 tc (tc_cls, GNUNET_SYSERR);
530 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
532 tcc->client = client;
534 tcc->tc_cls = tc_cls;
537 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
539 GNUNET_TIME_UNIT_FOREVER_REL,
544 if (GNUNET_YES == end)
547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
548 "Disconnecting client.\n");
550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
553 tc (tc_cls, GNUNET_SYSERR);
558 GNUNET_SERVER_client_keep (client);
559 GNUNET_CONTAINER_DLL_insert (tcc_head,
566 * Transmit a status code to the client.
568 * @param client receiver of the response
569 * @param code status code
570 * @param msg optional error message (can be NULL)
573 transmit_status (struct GNUNET_SERVER_Client *client,
577 struct StatusMessage *sm;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Transmitting `%s' message with value %d and message `%s'\n",
585 msg != NULL ? msg : "(none)");
587 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
588 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
589 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
590 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
591 sm->status = htonl(code);
593 memcpy (&sm[1], msg, slen);
594 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
599 * Function called once the transmit operation has
600 * either failed or succeeded.
602 * @param next_cls closure for calling "next_request" callback
603 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
606 get_next(void *next_cls,
609 if (status != GNUNET_OK)
611 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
612 _("Failed to transmit an item to the client; aborting iteration.\n"));
614 plugin->api->next_request (next_cls, GNUNET_YES);
617 plugin->api->next_request (next_cls, GNUNET_NO);
622 * Function that will transmit the given datastore entry
625 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
626 * @param next_cls closure to use to ask for the next item
627 * @param key key for the content
628 * @param size number of bytes in data
629 * @param data content stored
630 * @param type type of the content
631 * @param priority priority of the content
632 * @param anonymity anonymity-level for the content
633 * @param expiration expiration time for the content
634 * @param uid unique identifier for the datum;
635 * maybe 0 if no unique identifier is available
637 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
638 * GNUNET_NO to delete the item and continue (if supported)
641 transmit_item (void *cls,
643 const GNUNET_HashCode * key,
646 enum GNUNET_BLOCK_Type type,
649 struct GNUNET_TIME_Absolute
650 expiration, uint64_t uid)
652 struct GNUNET_SERVER_Client *client = cls;
653 struct GNUNET_MessageHeader *end;
654 struct DataMessage *dm;
658 /* transmit 'DATA_END' */
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "Transmitting `%s' message\n",
664 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
665 end->size = htons(sizeof(struct GNUNET_MessageHeader));
666 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
667 transmit (client, end, NULL, NULL, GNUNET_YES);
668 GNUNET_SERVER_client_drop (client);
671 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
672 dm->header.size = htons(sizeof(struct DataMessage) + size);
673 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
675 dm->size = htonl(size);
676 dm->type = htonl(type);
677 dm->priority = htonl(priority);
678 dm->anonymity = htonl(anonymity);
679 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
680 dm->uid = GNUNET_htonll(uid);
682 memcpy (&dm[1], data, size);
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Transmitting `%s' message\n",
688 GNUNET_STATISTICS_update (stats,
689 gettext_noop ("# results found"),
692 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
698 * Handle RESERVE-message.
701 * @param client identification of the client
702 * @param message the actual message
705 handle_reserve (void *cls,
706 struct GNUNET_SERVER_Client *client,
707 const struct GNUNET_MessageHeader *message)
709 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
710 struct ReservationList *e;
711 unsigned long long used;
712 unsigned long long req;
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Processing `%s' request\n",
721 amount = GNUNET_ntohll(msg->amount);
722 entries = ntohl(msg->entries);
723 used = plugin->api->get_size (plugin->api->cls) + reserved;
724 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
725 if (used + req > quota)
728 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
730 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
734 if (cache_size < req)
736 /* TODO: document this in the FAQ; essentially, if this
737 message happens, the insertion request could be blocked
738 by less-important content from migration because it is
739 larger than 1/8th of the overall available space, and
740 we only reserve 1/8th for "fresh" insertions */
741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
742 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
745 transmit_status (client, 0,
746 gettext_noop ("Insufficient space to satisfy request and "
747 "requested amount is larger than cache size"));
751 transmit_status (client, 0,
752 gettext_noop ("Insufficient space to satisfy request"));
757 e = GNUNET_malloc (sizeof(struct ReservationList));
758 e->next = reservations;
762 e->entries = entries;
763 e->rid = ++reservation_gen;
764 if (reservation_gen < 0)
765 reservation_gen = 0; /* wrap around */
766 transmit_status (client, e->rid, NULL);
771 * Handle RELEASE_RESERVE-message.
774 * @param client identification of the client
775 * @param message the actual message
778 handle_release_reserve (void *cls,
779 struct GNUNET_SERVER_Client *client,
780 const struct GNUNET_MessageHeader *message)
782 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
783 struct ReservationList *pos;
784 struct ReservationList *prev;
785 struct ReservationList *next;
786 int rid = ntohl(msg->rid);
787 unsigned long long rem;
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Processing `%s' request\n",
796 while (NULL != (pos = next))
805 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
806 GNUNET_assert (reserved >= rem);
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Returning %llu remaining reserved bytes to storage pool\n",
814 transmit_status (client, GNUNET_OK, NULL);
820 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
825 * Check that the given message is a valid data message.
827 * @return NULL if the message is not well-formed, otherwise the message
829 static const struct DataMessage *
830 check_data (const struct GNUNET_MessageHeader *message)
834 const struct DataMessage *dm;
836 size = ntohs(message->size);
837 if (size < sizeof(struct DataMessage))
842 dm = (const struct DataMessage *) message;
843 dsize = ntohl(dm->size);
844 if (size != dsize + sizeof(struct DataMessage))
854 * Handle PUT-message.
857 * @param client identification of the client
858 * @param message the actual message
861 handle_put (void *cls,
862 struct GNUNET_SERVER_Client *client,
863 const struct GNUNET_MessageHeader *message)
865 const struct DataMessage *dm = check_data (message);
869 struct ReservationList *pos;
873 (ntohl(dm->type) == 0) )
876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Processing `%s' request for `%s'\n",
883 GNUNET_h2s (&dm->key));
885 rid = ntohl(dm->rid);
886 size = ntohl(dm->size);
890 while ( (NULL != pos) &&
893 GNUNET_break (pos != NULL);
896 GNUNET_break (pos->entries > 0);
897 GNUNET_break (pos->amount > size);
900 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
904 ret = plugin->api->put (plugin->api->cls,
910 ntohl(dm->anonymity),
911 GNUNET_TIME_absolute_ntoh(dm->expiration),
913 if (GNUNET_OK == ret)
915 GNUNET_STATISTICS_update (stats,
916 gettext_noop ("# bytes stored"),
919 GNUNET_CONTAINER_bloomfilter_add (filter,
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Successfully stored %u bytes under key `%s'\n",
925 GNUNET_h2s (&dm->key));
928 transmit_status (client,
929 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
931 GNUNET_free_non_null (msg);
932 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
933 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
938 * Handle GET-message.
941 * @param client identification of the client
942 * @param message the actual message
945 handle_get (void *cls,
946 struct GNUNET_SERVER_Client *client,
947 const struct GNUNET_MessageHeader *message)
949 const struct GetMessage *msg;
952 size = ntohs(message->size);
953 if ( (size != sizeof(struct GetMessage)) &&
954 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
960 msg = (const struct GetMessage*) message;
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
963 "Processing `%s' request for `%s' of type %u\n",
965 GNUNET_h2s (&msg->key),
968 GNUNET_STATISTICS_update (stats,
969 gettext_noop ("# GET requests received"),
972 GNUNET_SERVER_client_keep (client);
973 if ( (size == sizeof(struct GetMessage)) &&
974 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
977 /* don't bother database... */
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Empty result set for `%s' request for `%s'.\n",
982 GNUNET_h2s (&msg->key));
984 GNUNET_STATISTICS_update (stats,
985 gettext_noop ("# requests filtered by bloomfilter"),
988 transmit_item (client,
989 NULL, NULL, 0, NULL, 0, 0, 0,
990 GNUNET_TIME_UNIT_ZERO_ABS, 0);
993 plugin->api->get (plugin->api->cls,
994 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1003 * Handle UPDATE-message.
1005 * @param cls closure
1006 * @param client identification of the client
1007 * @param message the actual message
1010 handle_update (void *cls,
1011 struct GNUNET_SERVER_Client *client,
1012 const struct GNUNET_MessageHeader *message)
1014 const struct UpdateMessage *msg;
1018 GNUNET_STATISTICS_update (stats,
1019 gettext_noop ("# UPDATE requests received"),
1022 msg = (const struct UpdateMessage*) message;
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Processing `%s' request for %llu\n",
1028 (unsigned long long) GNUNET_ntohll (msg->uid));
1030 ret = plugin->api->update (plugin->api->cls,
1031 GNUNET_ntohll(msg->uid),
1032 (int32_t) ntohl(msg->priority),
1033 GNUNET_TIME_absolute_ntoh(msg->expiration),
1035 transmit_status (client, ret, emsg);
1036 GNUNET_free_non_null (emsg);
1041 * Handle GET_RANDOM-message.
1043 * @param cls closure
1044 * @param client identification of the client
1045 * @param message the actual message
1048 handle_get_random (void *cls,
1049 struct GNUNET_SERVER_Client *client,
1050 const struct GNUNET_MessageHeader *message)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Processing `%s' request\n",
1057 GNUNET_STATISTICS_update (stats,
1058 gettext_noop ("# GET RANDOM requests received"),
1061 GNUNET_SERVER_client_keep (client);
1062 plugin->api->iter_migration_order (plugin->api->cls,
1070 * Context for the 'remove_callback'.
1072 struct RemoveContext
1075 * Client for whom we're doing the remvoing.
1077 struct GNUNET_SERVER_Client *client;
1080 * GNUNET_YES if we managed to remove something.
1087 * Callback function that will cause the item that is passed
1088 * in to be deleted (by returning GNUNET_NO).
1091 remove_callback (void *cls,
1093 const GNUNET_HashCode * key,
1096 enum GNUNET_BLOCK_Type type,
1099 struct GNUNET_TIME_Absolute
1100 expiration, uint64_t uid)
1102 struct RemoveContext *rc = cls;
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "No further matches for `%s' request.\n",
1111 if (GNUNET_YES == rc->found)
1112 transmit_status (rc->client, GNUNET_OK, NULL);
1114 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1115 GNUNET_SERVER_client_drop (rc->client);
1117 return GNUNET_OK; /* last item */
1119 rc->found = GNUNET_YES;
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Item %llu matches `%s' request for key `%s'.\n",
1123 (unsigned long long) uid,
1127 GNUNET_STATISTICS_update (stats,
1128 gettext_noop ("# bytes removed (explicit request)"),
1131 GNUNET_CONTAINER_bloomfilter_remove (filter,
1133 plugin->api->next_request (next_cls, GNUNET_YES);
1139 * Handle REMOVE-message.
1141 * @param cls closure
1142 * @param client identification of the client
1143 * @param message the actual message
1146 handle_remove (void *cls,
1147 struct GNUNET_SERVER_Client *client,
1148 const struct GNUNET_MessageHeader *message)
1150 const struct DataMessage *dm = check_data (message);
1151 GNUNET_HashCode vhash;
1152 struct RemoveContext *rc;
1157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Processing `%s' request for `%s'\n",
1164 GNUNET_h2s (&dm->key));
1166 GNUNET_STATISTICS_update (stats,
1167 gettext_noop ("# REMOVE requests received"),
1170 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1171 GNUNET_SERVER_client_keep (client);
1172 rc->client = client;
1173 GNUNET_CRYPTO_hash (&dm[1],
1176 plugin->api->get (plugin->api->cls,
1186 * Handle DROP-message.
1188 * @param cls closure
1189 * @param client identification of the client
1190 * @param message the actual message
1193 handle_drop (void *cls,
1194 struct GNUNET_SERVER_Client *client,
1195 const struct GNUNET_MessageHeader *message)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Processing `%s' request\n",
1202 plugin->api->drop (plugin->api->cls);
1203 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1208 * List of handlers for the messages understood by this
1211 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1212 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1213 sizeof(struct ReserveMessage) },
1214 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1215 sizeof(struct ReleaseReserveMessage) },
1216 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1217 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1218 sizeof (struct UpdateMessage) },
1219 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1220 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1221 sizeof(struct GNUNET_MessageHeader) },
1222 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1223 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1224 sizeof(struct GNUNET_MessageHeader) },
1231 * Load the datastore plugin.
1233 static struct DatastorePlugin *
1236 struct DatastorePlugin *ret;
1241 GNUNET_CONFIGURATION_get_value_string (cfg,
1242 "DATASTORE", "DATABASE", &name))
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 _("No `%s' specified for `%s' in configuration!\n"),
1250 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1252 ret->env.sched = sched;
1253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1254 _("Loading `%s' datastore plugin\n"), name);
1255 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1256 ret->short_name = name;
1257 ret->lib_name = libname;
1258 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1259 if (ret->api == NULL)
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 _("Failed to load datastore plugin for `%s'\n"), name);
1263 GNUNET_free (ret->short_name);
1264 GNUNET_free (libname);
1273 * Function called when the service shuts
1274 * down. Unloads our datastore plugin.
1276 * @param plug plugin to unload
1279 unload_plugin (struct DatastorePlugin *plug)
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Datastore service is unloading plugin...\n");
1285 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1286 GNUNET_free (plug->lib_name);
1287 GNUNET_free (plug->short_name);
1293 * Final task run after shutdown. Unloads plugins and disconnects us from
1297 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1299 unload_plugin (plugin);
1303 GNUNET_CONTAINER_bloomfilter_free (filter);
1308 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1315 * Last task run during shutdown. Disconnects us from
1316 * the transport and core.
1319 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1321 struct TransmitCallbackContext *tcc;
1323 cleaning_done = GNUNET_YES;
1324 while (NULL != (tcc = tcc_head))
1326 GNUNET_CONTAINER_DLL_remove (tcc_head,
1329 if (tcc->th != NULL)
1331 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1332 GNUNET_SERVER_client_drop (tcc->client);
1334 if (NULL != tcc->tc)
1335 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1336 GNUNET_free (tcc->msg);
1339 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1341 GNUNET_SCHEDULER_cancel (sched,
1343 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1345 GNUNET_SCHEDULER_add_continuation (sched,
1348 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1353 * Function that removes all active reservations made
1354 * by the given client and releases the space for other
1357 * @param cls closure
1358 * @param client identification of the client
1361 cleanup_reservations (void *cls,
1362 struct GNUNET_SERVER_Client
1365 struct ReservationList *pos;
1366 struct ReservationList *prev;
1367 struct ReservationList *next;
1376 if (pos->client == client)
1379 reservations = next;
1382 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1395 * Process datastore requests.
1397 * @param cls closure
1398 * @param s scheduler to use
1399 * @param server the initialized server
1400 * @param c configuration to use
1404 struct GNUNET_SCHEDULER_Handle *s,
1405 struct GNUNET_SERVER_Handle *server,
1406 const struct GNUNET_CONFIGURATION_Handle *c)
1409 unsigned int bf_size;
1414 GNUNET_CONFIGURATION_get_value_number (cfg,
1415 "DATASTORE", "QUOTA", "a))
1417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1418 _("No `%s' specified for `%s' in configuration!\n"),
1423 stats = GNUNET_STATISTICS_create (sched, "datastore", cfg);
1424 cache_size = quota / 8; /* Or should we make this an option? */
1425 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1428 GNUNET_CONFIGURATION_get_value_filename (cfg,
1433 GNUNET_DISK_directory_create_for_file (fn)) )
1435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1436 _("Could not use specified filename `%s' for bloomfilter.\n"),
1437 fn != NULL ? fn : "");
1438 GNUNET_free_non_null (fn);
1441 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1442 GNUNET_free_non_null (fn);
1445 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1446 _("Failed to initialize bloomfilter.\n"));
1449 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1454 plugin = load_plugin ();
1457 GNUNET_CONTAINER_bloomfilter_free (filter);
1461 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1466 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1467 GNUNET_SERVER_add_handlers (server, handlers);
1469 = GNUNET_SCHEDULER_add_with_priority (sched,
1470 GNUNET_SCHEDULER_PRIORITY_IDLE,
1471 &delete_expired, NULL);
1472 GNUNET_SCHEDULER_add_delayed (sched,
1473 GNUNET_TIME_UNIT_FOREVER_REL,
1474 &cleaning_task, NULL);
1480 * The main function for the datastore service.
1482 * @param argc number of arguments from the command line
1483 * @param argv command line arguments
1484 * @return 0 ok, 1 on error
1487 main (int argc, char *const *argv)
1492 GNUNET_SERVICE_run (argc,
1495 GNUNET_SERVICE_OPTION_NONE,
1496 &run, NULL)) ? 0 : 1;
1501 /* end of gnunet-service-datastore.c */