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 "gnunet_datastore_plugin.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)
46 * How fast are we allowed to query the database for deleting
47 * expired content? (1 item per second).
49 #define MIN_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
52 #define QUOTA_STAT_NAME gettext_noop ("# bytes used in file-sharing datastore")
55 * After how many payload-changing operations
56 * do we sync our statistics?
58 #define MAX_STAT_SYNC_LAG 50
62 * Our datastore plugin.
64 struct DatastorePlugin
68 * API of the transport as returned by the plugin's
69 * initialization function.
71 struct GNUNET_DATASTORE_PluginFunctions *api;
74 * Short name for the plugin (i.e. "sqlite").
79 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
84 * Environment this transport service is using
87 struct GNUNET_DATASTORE_PluginEnvironment env;
93 * Linked list of active reservations.
95 struct ReservationList
99 * This is a linked list.
101 struct ReservationList *next;
104 * Client that made the reservation.
106 struct GNUNET_SERVER_Client *client;
109 * Number of bytes (still) reserved.
114 * Number of items (still) reserved.
119 * Reservation identifier.
128 * Our datastore plugin (NULL if not available).
130 static struct DatastorePlugin *plugin;
133 * Linked list of space reservations made by clients.
135 static struct ReservationList *reservations;
138 * Bloomfilter to quickly tell if we don't have the content.
140 static struct GNUNET_CONTAINER_BloomFilter *filter;
143 * How much space are we allowed to use?
145 static unsigned long long quota;
148 * How much space are we using for the cache? (space available for
149 * insertions that will be instantly reclaimed by discarding less
150 * important content --- or possibly whatever we just inserted into
153 static unsigned long long cache_size;
156 * How much space have we currently reserved?
158 static unsigned long long reserved;
161 * How much data are we currently storing
164 static unsigned long long payload;
167 * Number of updates that were made to the
168 * payload value since we last synchronized
169 * it with the statistics service.
171 static unsigned int lastSync;
174 * Did we get an answer from statistics?
176 static int stats_worked;
179 * Identity of the task that is used to delete
182 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
187 const struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Handle for reporting statistics.
193 static struct GNUNET_STATISTICS_Handle *stats;
197 * Synchronize our utilization statistics with the
198 * statistics service.
203 GNUNET_STATISTICS_set (stats,
214 * Function called once the transmit operation has
215 * either failed or succeeded.
218 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
220 typedef void (*TransmitContinuation)(void *cls,
225 * Context for transmitting replies to clients.
227 struct TransmitCallbackContext
231 * We keep these in a doubly-linked list (for cleanup).
233 struct TransmitCallbackContext *next;
236 * We keep these in a doubly-linked list (for cleanup).
238 struct TransmitCallbackContext *prev;
241 * The message that we're asked to transmit.
243 struct GNUNET_MessageHeader *msg;
246 * Handle for the transmission request.
248 struct GNUNET_CONNECTION_TransmitHandle *th;
251 * Client that we are transmitting to.
253 struct GNUNET_SERVER_Client *client;
256 * Function to call once msg has been transmitted
257 * (or at least added to the buffer).
259 TransmitContinuation tc;
267 * GNUNET_YES if we are supposed to signal the server
268 * completion of the client's request.
275 * Head of the doubly-linked list (for cleanup).
277 static struct TransmitCallbackContext *tcc_head;
280 * Tail of the doubly-linked list (for cleanup).
282 static struct TransmitCallbackContext *tcc_tail;
285 * Have we already cleaned up the TCCs and are hence no longer
286 * willing (or able) to transmit anything to anyone?
288 static int cleaning_done;
291 * Handle for pending get request.
293 static struct GNUNET_STATISTICS_GetHandle *stat_get;
297 * Task that is used to remove expired entries from
298 * the datastore. This task will schedule itself
299 * again automatically to always delete all expired
302 * @param cls not used
303 * @param tc task context
306 delete_expired (void *cls,
307 const struct GNUNET_SCHEDULER_TaskContext *tc);
311 * Iterate over the expired items stored in the datastore.
312 * Delete all expired items; once we have processed all
313 * expired items, re-schedule the "delete_expired" task.
315 * @param cls not used
316 * @param next_cls closure to pass to the "next" function.
317 * @param key key for the content
318 * @param size number of bytes in data
319 * @param data content stored
320 * @param type type of the content
321 * @param priority priority of the content
322 * @param anonymity anonymity-level for the content
323 * @param expiration expiration time for the content
324 * @param uid unique identifier for the datum;
325 * maybe 0 if no unique identifier is available
327 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
328 * (continue on call to "next", of course),
329 * GNUNET_NO to delete the item and continue (if supported)
332 expired_processor (void *cls,
334 const GNUNET_HashCode * key,
337 enum GNUNET_BLOCK_Type type,
340 struct GNUNET_TIME_Absolute
344 struct GNUNET_TIME_Absolute now;
349 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
352 return GNUNET_SYSERR;
354 now = GNUNET_TIME_absolute_get ();
355 if (expiration.abs_value > now.abs_value)
357 /* finished processing */
359 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
362 return GNUNET_SYSERR;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Deleting content `%s' of type %u that expired %llu ms ago\n",
369 (unsigned long long) (now.abs_value - expiration.abs_value));
371 GNUNET_STATISTICS_update (stats,
372 gettext_noop ("# bytes expired"),
375 GNUNET_CONTAINER_bloomfilter_remove (filter,
378 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY,
386 * Task that is used to remove expired entries from
387 * the datastore. This task will schedule itself
388 * again automatically to always delete all expired
391 * @param cls not used
392 * @param tc task context
395 delete_expired (void *cls,
396 const struct GNUNET_SCHEDULER_TaskContext *tc)
398 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
399 plugin->api->expiration_get (plugin->api->cls,
406 * An iterator over a set of items stored in the datastore
407 * that deletes until we're happy with respect to our quota.
410 * @param next_cls closure to pass to the "next" function.
411 * @param key key for the content
412 * @param size number of bytes in data
413 * @param data content stored
414 * @param type type of the content
415 * @param priority priority of the content
416 * @param anonymity anonymity-level for the content
417 * @param expiration expiration time for the content
418 * @param uid unique identifier for the datum;
419 * maybe 0 if no unique identifier is available
421 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
422 * (continue on call to "next", of course),
423 * GNUNET_NO to delete the item and continue (if supported)
426 quota_processor (void *cls,
428 const GNUNET_HashCode * key,
431 enum GNUNET_BLOCK_Type type,
434 struct GNUNET_TIME_Absolute expiration,
437 unsigned long long *need = cls;
440 return GNUNET_SYSERR;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
444 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
449 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
452 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
453 GNUNET_STATISTICS_update (stats,
454 gettext_noop ("# bytes purged (low-priority)"),
457 GNUNET_CONTAINER_bloomfilter_remove (filter,
464 * Manage available disk space by running tasks
465 * that will discard content if necessary. This
466 * function will be run whenever a request for
467 * "need" bytes of storage could only be satisfied
468 * by eating into the "cache" (and we want our cache
471 * @param need number of bytes of content that were
472 * placed into the "cache" (and hence the
473 * number of bytes that should be removed).
476 manage_space (unsigned long long need)
478 unsigned long long last;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Asked to free up %llu bytes of cache space\n",
486 while ( (need > 0) &&
490 plugin->api->expiration_get (plugin->api->cls,
498 * Function called to notify a client about the socket
499 * begin ready to queue more data. "buf" will be
500 * NULL and "size" zero if the socket was closed for
501 * writing in the meantime.
504 * @param size number of bytes available in buf
505 * @param buf where the callee should write the message
506 * @return number of bytes written to buf
509 transmit_callback (void *cls,
510 size_t size, void *buf)
512 struct TransmitCallbackContext *tcc = cls;
516 GNUNET_CONTAINER_DLL_remove (tcc_head,
519 msize = ntohs(tcc->msg->size);
522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
523 _("Transmission to client failed!\n"));
525 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
526 if (GNUNET_YES == tcc->end)
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 _("Disconnecting client due to transmission failure!\n"));
530 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
532 GNUNET_SERVER_client_drop (tcc->client);
533 GNUNET_free (tcc->msg);
537 GNUNET_assert (size >= msize);
538 memcpy (buf, tcc->msg, msize);
540 tcc->tc (tcc->tc_cls, GNUNET_OK);
541 if (GNUNET_YES == tcc->end)
543 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Response transmitted, more pending!\n");
552 GNUNET_SERVER_client_drop (tcc->client);
553 GNUNET_free (tcc->msg);
560 * Transmit the given message to the client.
562 * @param client target of the message
563 * @param msg message to transmit, will be freed!
564 * @param tc function to call afterwards
565 * @param tc_cls closure for tc
566 * @param end is this the last response (and we should
567 * signal the server completion accodingly after
568 * transmitting this message)?
571 transmit (struct GNUNET_SERVER_Client *client,
572 struct GNUNET_MessageHeader *msg,
573 TransmitContinuation tc,
577 struct TransmitCallbackContext *tcc;
579 if (GNUNET_YES == cleaning_done)
582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
583 "Shutdown in progress, aborting transmission.\n");
587 tc (tc_cls, GNUNET_SYSERR);
590 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
592 tcc->client = client;
594 tcc->tc_cls = tc_cls;
597 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
599 GNUNET_TIME_UNIT_FOREVER_REL,
604 if (GNUNET_YES == end)
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607 _("Forcefully disconnecting client.\n"));
608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
611 tc (tc_cls, GNUNET_SYSERR);
616 GNUNET_SERVER_client_keep (client);
617 GNUNET_CONTAINER_DLL_insert (tcc_head,
624 * Transmit a status code to the client.
626 * @param client receiver of the response
627 * @param code status code
628 * @param msg optional error message (can be NULL)
631 transmit_status (struct GNUNET_SERVER_Client *client,
635 struct StatusMessage *sm;
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Transmitting `%s' message with value %d and message `%s'\n",
643 msg != NULL ? msg : "(none)");
645 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
646 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
647 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
648 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
649 sm->status = htonl(code);
651 memcpy (&sm[1], msg, slen);
652 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
657 * Function called once the transmit operation has
658 * either failed or succeeded.
660 * @param next_cls closure for calling "next_request" callback
661 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
664 get_next(void *next_cls,
667 if (status != GNUNET_OK)
669 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
670 _("Failed to transmit an item to the client; aborting iteration.\n"));
672 plugin->api->next_request (next_cls, GNUNET_YES);
675 if (next_cls != NULL)
676 plugin->api->next_request (next_cls, GNUNET_NO);
681 * Function that will transmit the given datastore entry
684 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
685 * @param next_cls closure to use to ask for the next item
686 * @param key key for the content
687 * @param size number of bytes in data
688 * @param data content stored
689 * @param type type of the content
690 * @param priority priority of the content
691 * @param anonymity anonymity-level for the content
692 * @param expiration expiration time for the content
693 * @param uid unique identifier for the datum;
694 * maybe 0 if no unique identifier is available
696 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
697 * GNUNET_NO to delete the item and continue (if supported)
700 transmit_item (void *cls,
702 const GNUNET_HashCode * key,
705 enum GNUNET_BLOCK_Type type,
708 struct GNUNET_TIME_Absolute
709 expiration, uint64_t uid)
711 struct GNUNET_SERVER_Client *client = cls;
712 struct GNUNET_MessageHeader *end;
713 struct DataMessage *dm;
717 /* transmit 'DATA_END' */
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Transmitting `%s' message\n",
723 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
724 end->size = htons(sizeof(struct GNUNET_MessageHeader));
725 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
726 transmit (client, end, NULL, NULL, GNUNET_YES);
727 GNUNET_SERVER_client_drop (client);
730 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
731 dm->header.size = htons(sizeof(struct DataMessage) + size);
732 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
734 dm->size = htonl(size);
735 dm->type = htonl(type);
736 dm->priority = htonl(priority);
737 dm->anonymity = htonl(anonymity);
738 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
739 dm->uid = GNUNET_htonll(uid);
741 memcpy (&dm[1], data, size);
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Transmitting `%s' message for `%s' of type %u\n",
749 GNUNET_STATISTICS_update (stats,
750 gettext_noop ("# results found"),
753 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
759 * Handle RESERVE-message.
762 * @param client identification of the client
763 * @param message the actual message
766 handle_reserve (void *cls,
767 struct GNUNET_SERVER_Client *client,
768 const struct GNUNET_MessageHeader *message)
771 * Static counter to produce reservation identifiers.
773 static int reservation_gen;
775 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
776 struct ReservationList *e;
777 unsigned long long used;
778 unsigned long long req;
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Processing `%s' request\n",
787 amount = GNUNET_ntohll(msg->amount);
788 entries = ntohl(msg->entries);
789 used = payload + reserved;
790 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
791 if (used + req > quota)
794 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
796 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
800 if (cache_size < req)
802 /* TODO: document this in the FAQ; essentially, if this
803 message happens, the insertion request could be blocked
804 by less-important content from migration because it is
805 larger than 1/8th of the overall available space, and
806 we only reserve 1/8th for "fresh" insertions */
807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
808 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
811 transmit_status (client, 0,
812 gettext_noop ("Insufficient space to satisfy request and "
813 "requested amount is larger than cache size"));
817 transmit_status (client, 0,
818 gettext_noop ("Insufficient space to satisfy request"));
823 GNUNET_STATISTICS_set (stats,
824 gettext_noop ("# reserved"),
827 e = GNUNET_malloc (sizeof(struct ReservationList));
828 e->next = reservations;
832 e->entries = entries;
833 e->rid = ++reservation_gen;
834 if (reservation_gen < 0)
835 reservation_gen = 0; /* wrap around */
836 transmit_status (client, e->rid, NULL);
841 * Handle RELEASE_RESERVE-message.
844 * @param client identification of the client
845 * @param message the actual message
848 handle_release_reserve (void *cls,
849 struct GNUNET_SERVER_Client *client,
850 const struct GNUNET_MessageHeader *message)
852 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
853 struct ReservationList *pos;
854 struct ReservationList *prev;
855 struct ReservationList *next;
856 int rid = ntohl(msg->rid);
857 unsigned long long rem;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Processing `%s' request\n",
866 while (NULL != (pos = next))
875 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
876 GNUNET_assert (reserved >= rem);
878 GNUNET_STATISTICS_set (stats,
879 gettext_noop ("# reserved"),
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Returning %llu remaining reserved bytes to storage pool\n",
888 transmit_status (client, GNUNET_OK, NULL);
894 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
899 * Check that the given message is a valid data message.
901 * @return NULL if the message is not well-formed, otherwise the message
903 static const struct DataMessage *
904 check_data (const struct GNUNET_MessageHeader *message)
908 const struct DataMessage *dm;
910 size = ntohs(message->size);
911 if (size < sizeof(struct DataMessage))
916 dm = (const struct DataMessage *) message;
917 dsize = ntohl(dm->size);
918 if (size != dsize + sizeof(struct DataMessage))
928 * Context for a put request used to see if the content is
934 * Client to notify on completion.
936 struct GNUNET_SERVER_Client *client;
939 * Did we find the data already in the database?
943 /* followed by the 'struct DataMessage' */
948 * Actually put the data message.
951 execute_put (struct GNUNET_SERVER_Client *client,
952 const struct DataMessage *dm)
958 size = ntohl(dm->size);
960 ret = plugin->api->put (plugin->api->cls,
966 ntohl(dm->anonymity),
967 0 /* FIXME: replication */,
968 GNUNET_TIME_absolute_ntoh(dm->expiration),
970 if (GNUNET_OK == ret)
972 GNUNET_STATISTICS_update (stats,
973 gettext_noop ("# bytes stored"),
976 GNUNET_CONTAINER_bloomfilter_add (filter,
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Successfully stored %u bytes of type %u under key `%s'\n",
983 GNUNET_h2s (&dm->key));
986 transmit_status (client,
989 GNUNET_free_non_null (msg);
990 if (quota - reserved - cache_size < payload)
992 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
993 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
994 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
995 (unsigned long long) (quota - reserved - cache_size),
996 (unsigned long long) payload);
997 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1003 * Function that will check if the given datastore entry
1004 * matches the put and if none match executes the put.
1006 * @param cls closure, pointer to the client (of type 'struct PutContext').
1007 * @param next_cls closure to use to ask for the next item
1008 * @param key key for the content
1009 * @param size number of bytes in data
1010 * @param data content stored
1011 * @param type type of the content
1012 * @param priority priority of the content
1013 * @param anonymity anonymity-level for the content
1014 * @param expiration expiration time for the content
1015 * @param uid unique identifier for the datum;
1016 * maybe 0 if no unique identifier is available
1018 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
1019 * GNUNET_NO to delete the item and continue (if supported)
1022 check_present (void *cls,
1024 const GNUNET_HashCode * key,
1027 enum GNUNET_BLOCK_Type type,
1030 struct GNUNET_TIME_Absolute
1031 expiration, uint64_t uid)
1033 struct PutContext *pc = cls;
1034 const struct DataMessage *dm;
1036 dm = (const struct DataMessage*) &pc[1];
1039 if (pc->is_present == GNUNET_YES)
1040 transmit_status (pc->client, GNUNET_NO, NULL);
1042 execute_put (pc->client, dm);
1043 GNUNET_SERVER_client_drop (pc->client);
1045 return GNUNET_SYSERR;
1047 if ( (GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
1048 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
1049 ( (size == ntohl(dm->size)) &&
1050 (0 == memcmp (&dm[1],
1054 pc->is_present = GNUNET_YES;
1055 plugin->api->next_request (next_cls, GNUNET_YES);
1059 plugin->api->next_request (next_cls, GNUNET_NO);
1066 * Handle PUT-message.
1068 * @param cls closure
1069 * @param client identification of the client
1070 * @param message the actual message
1073 handle_put (void *cls,
1074 struct GNUNET_SERVER_Client *client,
1075 const struct GNUNET_MessageHeader *message)
1077 const struct DataMessage *dm = check_data (message);
1079 struct ReservationList *pos;
1080 struct PutContext *pc;
1083 if ( (dm == NULL) ||
1084 (ntohl(dm->type) == 0) )
1087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1092 "Processing `%s' request for `%s' of type %u\n",
1094 GNUNET_h2s (&dm->key),
1097 rid = ntohl(dm->rid);
1098 size = ntohl(dm->size);
1102 while ( (NULL != pos) &&
1105 GNUNET_break (pos != NULL);
1108 GNUNET_break (pos->entries > 0);
1109 GNUNET_break (pos->amount >= size);
1111 pos->amount -= size;
1112 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
1113 GNUNET_STATISTICS_set (stats,
1114 gettext_noop ("# reserved"),
1119 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter,
1122 pc = GNUNET_malloc (sizeof (struct PutContext) + size + sizeof (struct DataMessage));
1123 pc->client = client;
1124 GNUNET_SERVER_client_keep (client);
1125 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
1126 plugin->api->get (plugin->api->cls,
1134 execute_put (client, dm);
1139 * Handle GET-message.
1141 * @param cls closure
1142 * @param client identification of the client
1143 * @param message the actual message
1146 handle_get (void *cls,
1147 struct GNUNET_SERVER_Client *client,
1148 const struct GNUNET_MessageHeader *message)
1150 const struct GetMessage *msg;
1153 size = ntohs(message->size);
1154 if ( (size != sizeof(struct GetMessage)) &&
1155 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
1158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1161 msg = (const struct GetMessage*) message;
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Processing `%s' request for `%s' of type %u\n",
1166 GNUNET_h2s (&msg->key),
1169 GNUNET_STATISTICS_update (stats,
1170 gettext_noop ("# GET requests received"),
1173 GNUNET_SERVER_client_keep (client);
1174 if ( (size == sizeof(struct GetMessage)) &&
1175 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
1178 /* don't bother database... */
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1183 GNUNET_h2s (&msg->key));
1185 GNUNET_STATISTICS_update (stats,
1186 gettext_noop ("# requests filtered by bloomfilter"),
1189 transmit_item (client,
1190 NULL, NULL, 0, NULL, 0, 0, 0,
1191 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1194 plugin->api->get (plugin->api->cls,
1195 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
1204 * Handle UPDATE-message.
1206 * @param cls closure
1207 * @param client identification of the client
1208 * @param message the actual message
1211 handle_update (void *cls,
1212 struct GNUNET_SERVER_Client *client,
1213 const struct GNUNET_MessageHeader *message)
1215 const struct UpdateMessage *msg;
1219 GNUNET_STATISTICS_update (stats,
1220 gettext_noop ("# UPDATE requests received"),
1223 msg = (const struct UpdateMessage*) message;
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Processing `%s' request for %llu\n",
1229 (unsigned long long) GNUNET_ntohll (msg->uid));
1231 ret = plugin->api->update (plugin->api->cls,
1232 GNUNET_ntohll(msg->uid),
1233 (int32_t) ntohl(msg->priority),
1234 GNUNET_TIME_absolute_ntoh(msg->expiration),
1236 transmit_status (client, ret, emsg);
1237 GNUNET_free_non_null (emsg);
1242 * Handle GET_REPLICATION-message.
1244 * @param cls closure
1245 * @param client identification of the client
1246 * @param message the actual message
1249 handle_get_replication (void *cls,
1250 struct GNUNET_SERVER_Client *client,
1251 const struct GNUNET_MessageHeader *message)
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Processing `%s' request\n",
1258 GNUNET_STATISTICS_update (stats,
1259 gettext_noop ("# GET REPLICATION requests received"),
1262 GNUNET_SERVER_client_keep (client);
1263 plugin->api->replication_get (plugin->api->cls,
1270 * Handle GET_ZERO_ANONYMITY-message.
1272 * @param cls closure
1273 * @param client identification of the client
1274 * @param message the actual message
1277 handle_get_zero_anonymity (void *cls,
1278 struct GNUNET_SERVER_Client *client,
1279 const struct GNUNET_MessageHeader *message)
1281 const struct GetZeroAnonymityMessage * msg = (const struct GetZeroAnonymityMessage*) message;
1282 enum GNUNET_BLOCK_Type type;
1284 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1285 if (type == GNUNET_BLOCK_TYPE_ANY)
1288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Processing `%s' request\n",
1294 "GET_ZERO_ANONYMITY");
1296 GNUNET_STATISTICS_update (stats,
1297 gettext_noop ("# GET ZERO ANONYMITY requests received"),
1300 GNUNET_SERVER_client_keep (client);
1301 plugin->api->iter_zero_anonymity (plugin->api->cls,
1309 * Context for the 'remove_callback'.
1311 struct RemoveContext
1314 * Client for whom we're doing the remvoing.
1316 struct GNUNET_SERVER_Client *client;
1319 * GNUNET_YES if we managed to remove something.
1326 * Callback function that will cause the item that is passed
1327 * in to be deleted (by returning GNUNET_NO).
1330 remove_callback (void *cls,
1332 const GNUNET_HashCode * key,
1335 enum GNUNET_BLOCK_Type type,
1338 struct GNUNET_TIME_Absolute
1339 expiration, uint64_t uid)
1341 struct RemoveContext *rc = cls;
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "No further matches for `%s' request.\n",
1350 if (GNUNET_YES == rc->found)
1351 transmit_status (rc->client, GNUNET_OK, NULL);
1353 transmit_status (rc->client, GNUNET_NO, _("Content not found"));
1354 GNUNET_SERVER_client_drop (rc->client);
1356 return GNUNET_OK; /* last item */
1358 rc->found = GNUNET_YES;
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1362 (unsigned long long) uid,
1367 GNUNET_STATISTICS_update (stats,
1368 gettext_noop ("# bytes removed (explicit request)"),
1371 GNUNET_CONTAINER_bloomfilter_remove (filter,
1373 plugin->api->next_request (next_cls, GNUNET_YES);
1379 * Handle REMOVE-message.
1381 * @param cls closure
1382 * @param client identification of the client
1383 * @param message the actual message
1386 handle_remove (void *cls,
1387 struct GNUNET_SERVER_Client *client,
1388 const struct GNUNET_MessageHeader *message)
1390 const struct DataMessage *dm = check_data (message);
1391 GNUNET_HashCode vhash;
1392 struct RemoveContext *rc;
1397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Processing `%s' request for `%s' of type %u\n",
1404 GNUNET_h2s (&dm->key),
1407 GNUNET_STATISTICS_update (stats,
1408 gettext_noop ("# REMOVE requests received"),
1411 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1412 GNUNET_SERVER_client_keep (client);
1413 rc->client = client;
1414 GNUNET_CRYPTO_hash (&dm[1],
1417 plugin->api->get (plugin->api->cls,
1420 (enum GNUNET_BLOCK_Type) ntohl(dm->type),
1427 * Handle DROP-message.
1429 * @param cls closure
1430 * @param client identification of the client
1431 * @param message the actual message
1434 handle_drop (void *cls,
1435 struct GNUNET_SERVER_Client *client,
1436 const struct GNUNET_MessageHeader *message)
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1440 "Processing `%s' request\n",
1443 plugin->api->drop (plugin->api->cls);
1444 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1449 * Function called by plugins to notify us about a
1450 * change in their disk utilization.
1452 * @param cls closure (NULL)
1453 * @param delta change in disk utilization,
1454 * 0 for "reset to empty"
1457 disk_utilization_change_cb (void *cls,
1461 (payload < -delta) )
1463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1464 _("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1465 (long long) payload,
1466 (long long) -delta);
1467 payload = plugin->api->get_size (plugin->api->cls);
1473 if (lastSync >= MAX_STAT_SYNC_LAG)
1479 * Callback function to process statistic values.
1481 * @param cls closure (struct Plugin*)
1482 * @param subsystem name of subsystem that created the statistic
1483 * @param name the name of the datum
1484 * @param value the current value
1485 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1486 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1489 process_stat_in (void *cls,
1490 const char *subsystem,
1495 GNUNET_assert (stats_worked == GNUNET_NO);
1496 stats_worked = GNUNET_YES;
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1509 process_stat_done (void *cls,
1512 struct DatastorePlugin *plugin = cls;
1515 if (stats_worked == GNUNET_NO)
1516 payload = plugin->api->get_size (plugin->api->cls);
1521 * Load the datastore plugin.
1523 static struct DatastorePlugin *
1526 struct DatastorePlugin *ret;
1531 GNUNET_CONFIGURATION_get_value_string (cfg,
1532 "DATASTORE", "DATABASE", &name))
1534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1535 _("No `%s' specified for `%s' in configuration!\n"),
1540 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1542 ret->env.duc = &disk_utilization_change_cb;
1543 ret->env.cls = NULL;
1544 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1545 _("Loading `%s' datastore plugin\n"), name);
1546 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1547 ret->short_name = name;
1548 ret->lib_name = libname;
1549 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1550 if (ret->api == NULL)
1552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1553 _("Failed to load datastore plugin for `%s'\n"), name);
1554 GNUNET_free (ret->short_name);
1555 GNUNET_free (libname);
1564 * Function called when the service shuts
1565 * down. Unloads our datastore plugin.
1567 * @param plug plugin to unload
1570 unload_plugin (struct DatastorePlugin *plug)
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Datastore service is unloading plugin...\n");
1576 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1577 GNUNET_free (plug->lib_name);
1578 GNUNET_free (plug->short_name);
1584 * Final task run after shutdown. Unloads plugins and disconnects us from
1588 unload_task (void *cls,
1589 const struct GNUNET_SCHEDULER_TaskContext *tc)
1591 unload_plugin (plugin);
1595 GNUNET_CONTAINER_bloomfilter_free (filter);
1600 if (stat_get != NULL)
1602 GNUNET_STATISTICS_get_cancel (stat_get);
1607 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1614 * Last task run during shutdown. Disconnects us from
1615 * the transport and core.
1618 cleaning_task (void *cls,
1619 const struct GNUNET_SCHEDULER_TaskContext *tc)
1621 struct TransmitCallbackContext *tcc;
1623 cleaning_done = GNUNET_YES;
1624 while (NULL != (tcc = tcc_head))
1626 GNUNET_CONTAINER_DLL_remove (tcc_head,
1629 if (tcc->th != NULL)
1631 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1632 GNUNET_SERVER_client_drop (tcc->client);
1634 if (NULL != tcc->tc)
1635 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
1636 GNUNET_free (tcc->msg);
1639 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1641 GNUNET_SCHEDULER_cancel (expired_kill_task);
1642 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1644 GNUNET_SCHEDULER_add_continuation (&unload_task,
1646 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1651 * Function that removes all active reservations made
1652 * by the given client and releases the space for other
1655 * @param cls closure
1656 * @param client identification of the client
1659 cleanup_reservations (void *cls,
1660 struct GNUNET_SERVER_Client *client)
1662 struct ReservationList *pos;
1663 struct ReservationList *prev;
1664 struct ReservationList *next;
1673 if (pos->client == client)
1676 reservations = next;
1679 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1688 GNUNET_STATISTICS_set (stats,
1689 gettext_noop ("# reserved"),
1696 * Process datastore requests.
1698 * @param cls closure
1699 * @param server the initialized server
1700 * @param c configuration to use
1704 struct GNUNET_SERVER_Handle *server,
1705 const struct GNUNET_CONFIGURATION_Handle *c)
1707 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1708 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1709 sizeof(struct ReserveMessage) },
1710 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1711 sizeof(struct ReleaseReserveMessage) },
1712 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1713 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1714 sizeof (struct UpdateMessage) },
1715 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1716 {&handle_get_replication, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1717 sizeof(struct GNUNET_MessageHeader) },
1718 {&handle_get_zero_anonymity, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1719 sizeof(struct GetZeroAnonymityMessage) },
1720 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1721 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1722 sizeof(struct GNUNET_MessageHeader) },
1726 unsigned int bf_size;
1730 GNUNET_CONFIGURATION_get_value_number (cfg,
1731 "DATASTORE", "QUOTA", "a))
1733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1734 _("No `%s' specified for `%s' in configuration!\n"),
1739 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1740 GNUNET_STATISTICS_set (stats,
1741 gettext_noop ("# quota"),
1744 cache_size = quota / 8; /* Or should we make this an option? */
1745 GNUNET_STATISTICS_set (stats,
1746 gettext_noop ("# cache size"),
1749 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1752 GNUNET_CONFIGURATION_get_value_filename (cfg,
1757 GNUNET_DISK_directory_create_for_file (fn)) )
1759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1760 _("Could not use specified filename `%s' for bloomfilter.\n"),
1761 fn != NULL ? fn : "");
1762 GNUNET_free_non_null (fn);
1765 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1766 GNUNET_free_non_null (fn);
1769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1770 _("Failed to initialize bloomfilter.\n"));
1773 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1778 plugin = load_plugin ();
1781 GNUNET_CONTAINER_bloomfilter_free (filter);
1785 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1790 stat_get = GNUNET_STATISTICS_get (stats,
1793 GNUNET_TIME_UNIT_SECONDS,
1797 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1798 GNUNET_SERVER_add_handlers (server, handlers);
1800 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1801 &delete_expired, NULL);
1802 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1803 &cleaning_task, NULL);
1808 * The main function for the datastore service.
1810 * @param argc number of arguments from the command line
1811 * @param argv command line arguments
1812 * @return 0 ok, 1 on error
1815 main (int argc, char *const *argv)
1820 GNUNET_SERVICE_run (argc,
1823 GNUNET_SERVICE_OPTION_NONE,
1824 &run, NULL)) ? 0 : 1;
1829 /* end of gnunet-service-datastore.c */