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 * Should the database be dropped on exit?
153 * How much space are we using for the cache? (space available for
154 * insertions that will be instantly reclaimed by discarding less
155 * important content --- or possibly whatever we just inserted into
158 static unsigned long long cache_size;
161 * How much space have we currently reserved?
163 static unsigned long long reserved;
166 * How much data are we currently storing
169 static unsigned long long payload;
172 * Number of updates that were made to the
173 * payload value since we last synchronized
174 * it with the statistics service.
176 static unsigned int lastSync;
179 * Did we get an answer from statistics?
181 static int stats_worked;
184 * Identity of the task that is used to delete
187 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
192 const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Handle for reporting statistics.
198 static struct GNUNET_STATISTICS_Handle *stats;
202 * Synchronize our utilization statistics with the
203 * statistics service.
208 GNUNET_STATISTICS_set (stats, QUOTA_STAT_NAME, payload, GNUNET_YES);
215 * Context for transmitting replies to clients.
217 struct TransmitCallbackContext
221 * We keep these in a doubly-linked list (for cleanup).
223 struct TransmitCallbackContext *next;
226 * We keep these in a doubly-linked list (for cleanup).
228 struct TransmitCallbackContext *prev;
231 * The message that we're asked to transmit.
233 struct GNUNET_MessageHeader *msg;
236 * Handle for the transmission request.
238 struct GNUNET_CONNECTION_TransmitHandle *th;
241 * Client that we are transmitting to.
243 struct GNUNET_SERVER_Client *client;
249 * Head of the doubly-linked list (for cleanup).
251 static struct TransmitCallbackContext *tcc_head;
254 * Tail of the doubly-linked list (for cleanup).
256 static struct TransmitCallbackContext *tcc_tail;
259 * Have we already cleaned up the TCCs and are hence no longer
260 * willing (or able) to transmit anything to anyone?
262 static int cleaning_done;
265 * Handle for pending get request.
267 static struct GNUNET_STATISTICS_GetHandle *stat_get;
271 * Task that is used to remove expired entries from
272 * the datastore. This task will schedule itself
273 * again automatically to always delete all expired
276 * @param cls not used
277 * @param tc task context
280 delete_expired (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
284 * Iterate over the expired items stored in the datastore.
285 * Delete all expired items; once we have processed all
286 * expired items, re-schedule the "delete_expired" task.
288 * @param cls not used
289 * @param key key for the content
290 * @param size number of bytes in data
291 * @param data content stored
292 * @param type type of the content
293 * @param priority priority of the content
294 * @param anonymity anonymity-level for the content
295 * @param expiration expiration time for the content
296 * @param uid unique identifier for the datum;
297 * maybe 0 if no unique identifier is available
299 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
300 * (continue on call to "next", of course),
301 * GNUNET_NO to delete the item and continue (if supported)
304 expired_processor (void *cls,
305 const GNUNET_HashCode * key,
308 enum GNUNET_BLOCK_Type type,
311 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
313 struct GNUNET_TIME_Absolute now;
318 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
319 &delete_expired, NULL);
320 return GNUNET_SYSERR;
322 now = GNUNET_TIME_absolute_get ();
323 if (expiration.abs_value > now.abs_value)
325 /* finished processing */
327 = GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY,
328 &delete_expired, NULL);
329 return GNUNET_SYSERR;
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Deleting content `%s' of type %u that expired %llu ms ago\n",
336 (unsigned long long) (now.abs_value - expiration.abs_value));
338 GNUNET_STATISTICS_update (stats,
339 gettext_noop ("# bytes expired"), size, GNUNET_YES);
340 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
342 = GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY, &delete_expired, NULL);
348 * Task that is used to remove expired entries from
349 * the datastore. This task will schedule itself
350 * again automatically to always delete all expired
353 * @param cls not used
354 * @param tc task context
357 delete_expired (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
359 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
360 plugin->api->get_expiration (plugin->api->cls, &expired_processor, NULL);
365 * An iterator over a set of items stored in the datastore
366 * that deletes until we're happy with respect to our quota.
369 * @param key key for the content
370 * @param size number of bytes in data
371 * @param data content stored
372 * @param type type of the content
373 * @param priority priority of the content
374 * @param anonymity anonymity-level for the content
375 * @param expiration expiration time for the content
376 * @param uid unique identifier for the datum;
377 * maybe 0 if no unique identifier is available
379 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
380 * (continue on call to "next", of course),
381 * GNUNET_NO to delete the item and continue (if supported)
384 quota_processor (void *cls,
385 const GNUNET_HashCode * key,
388 enum GNUNET_BLOCK_Type type,
391 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
393 unsigned long long *need = cls;
396 return GNUNET_SYSERR;
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
400 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
401 GNUNET_h2s (key), type, *need);
403 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
406 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
407 GNUNET_STATISTICS_update (stats,
408 gettext_noop ("# bytes purged (low-priority)"),
410 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
416 * Manage available disk space by running tasks
417 * that will discard content if necessary. This
418 * function will be run whenever a request for
419 * "need" bytes of storage could only be satisfied
420 * by eating into the "cache" (and we want our cache
423 * @param need number of bytes of content that were
424 * placed into the "cache" (and hence the
425 * number of bytes that should be removed).
428 manage_space (unsigned long long need)
430 unsigned long long last;
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Asked to free up %llu bytes of cache space\n", need);
437 while ((need > 0) && (last != need))
440 plugin->api->get_expiration (plugin->api->cls, "a_processor, &need);
446 * Function called to notify a client about the socket
447 * begin ready to queue more data. "buf" will be
448 * NULL and "size" zero if the socket was closed for
449 * writing in the meantime.
452 * @param size number of bytes available in buf
453 * @param buf where the callee should write the message
454 * @return number of bytes written to buf
457 transmit_callback (void *cls, size_t size, void *buf)
459 struct TransmitCallbackContext *tcc = cls;
463 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
464 msize = ntohs (tcc->msg->size);
467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
468 _("Transmission to client failed!\n"));
469 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
470 GNUNET_SERVER_client_drop (tcc->client);
471 GNUNET_free (tcc->msg);
475 GNUNET_assert (size >= msize);
476 memcpy (buf, tcc->msg, msize);
477 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
478 GNUNET_SERVER_client_drop (tcc->client);
479 GNUNET_free (tcc->msg);
486 * Transmit the given message to the client.
488 * @param client target of the message
489 * @param msg message to transmit, will be freed!
492 transmit (struct GNUNET_SERVER_Client *client, struct GNUNET_MessageHeader *msg)
494 struct TransmitCallbackContext *tcc;
496 if (GNUNET_YES == cleaning_done)
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
500 "Shutdown in progress, aborting transmission.\n");
502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
506 tcc = GNUNET_malloc (sizeof (struct TransmitCallbackContext));
508 tcc->client = client;
510 (tcc->th = GNUNET_SERVER_notify_transmit_ready (client,
512 GNUNET_TIME_UNIT_FOREVER_REL,
513 &transmit_callback, tcc)))
516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
521 GNUNET_SERVER_client_keep (client);
522 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
527 * Transmit a status code to the client.
529 * @param client receiver of the response
530 * @param code status code
531 * @param msg optional error message (can be NULL)
534 transmit_status (struct GNUNET_SERVER_Client *client, int code, const char *msg)
536 struct StatusMessage *sm;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Transmitting `%s' message with value %d and message `%s'\n",
542 "STATUS", code, msg != NULL ? msg : "(none)");
544 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
545 sm = GNUNET_malloc (sizeof (struct StatusMessage) + slen);
546 sm->header.size = htons (sizeof (struct StatusMessage) + slen);
547 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
548 sm->status = htonl (code);
550 memcpy (&sm[1], msg, slen);
551 transmit (client, &sm->header);
557 * Function that will transmit the given datastore entry
560 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
561 * @param key key for the content
562 * @param size number of bytes in data
563 * @param data content stored
564 * @param type type of the content
565 * @param priority priority of the content
566 * @param anonymity anonymity-level for the content
567 * @param expiration expiration time for the content
568 * @param uid unique identifier for the datum;
569 * maybe 0 if no unique identifier is available
571 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
572 * GNUNET_NO to delete the item and continue (if supported)
575 transmit_item (void *cls,
576 const GNUNET_HashCode * key,
579 enum GNUNET_BLOCK_Type type,
582 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
584 struct GNUNET_SERVER_Client *client = cls;
585 struct GNUNET_MessageHeader *end;
586 struct DataMessage *dm;
590 /* transmit 'DATA_END' */
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Transmitting `%s' message\n", "DATA_END");
595 end = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader));
596 end->size = htons (sizeof (struct GNUNET_MessageHeader));
597 end->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
598 transmit (client, end);
599 GNUNET_SERVER_client_drop (client);
602 GNUNET_assert (sizeof (struct DataMessage) + size <
603 GNUNET_SERVER_MAX_MESSAGE_SIZE);
604 dm = GNUNET_malloc (sizeof (struct DataMessage) + size);
605 dm->header.size = htons (sizeof (struct DataMessage) + size);
606 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
608 dm->size = htonl (size);
609 dm->type = htonl (type);
610 dm->priority = htonl (priority);
611 dm->anonymity = htonl (anonymity);
612 dm->replication = htonl (0);
613 dm->reserved = htonl (0);
614 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
615 dm->uid = GNUNET_htonll (uid);
617 memcpy (&dm[1], data, size);
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Transmitting `%s' message for `%s' of type %u with expiration %llu (now: %llu)\n",
624 (unsigned long long) expiration.abs_value,
625 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value);
627 GNUNET_STATISTICS_update (stats,
628 gettext_noop ("# results found"), 1, GNUNET_NO);
629 transmit (client, &dm->header);
630 GNUNET_SERVER_client_drop (client);
636 * Handle RESERVE-message.
639 * @param client identification of the client
640 * @param message the actual message
643 handle_reserve (void *cls,
644 struct GNUNET_SERVER_Client *client,
645 const struct GNUNET_MessageHeader *message)
648 * Static counter to produce reservation identifiers.
650 static int reservation_gen;
652 const struct ReserveMessage *msg = (const struct ReserveMessage *) message;
653 struct ReservationList *e;
654 unsigned long long used;
655 unsigned long long req;
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "RESERVE");
662 amount = GNUNET_ntohll (msg->amount);
663 entries = ntohl (msg->entries);
664 used = payload + reserved;
666 amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
667 if (used + req > quota)
670 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
673 ("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
674 quota - used, "RESERVE", req);
675 if (cache_size < req)
677 /* TODO: document this in the FAQ; essentially, if this
678 * message happens, the insertion request could be blocked
679 * by less-important content from migration because it is
680 * larger than 1/8th of the overall available space, and
681 * we only reserve 1/8th for "fresh" insertions */
682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
684 ("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
686 transmit_status (client, 0,
688 ("Insufficient space to satisfy request and "
689 "requested amount is larger than cache size"));
693 transmit_status (client, 0,
694 gettext_noop ("Insufficient space to satisfy request"));
699 GNUNET_STATISTICS_set (stats,
700 gettext_noop ("# reserved"), reserved, GNUNET_NO);
701 e = GNUNET_malloc (sizeof (struct ReservationList));
702 e->next = reservations;
706 e->entries = entries;
707 e->rid = ++reservation_gen;
708 if (reservation_gen < 0)
709 reservation_gen = 0; /* wrap around */
710 transmit_status (client, e->rid, NULL);
715 * Handle RELEASE_RESERVE-message.
718 * @param client identification of the client
719 * @param message the actual message
722 handle_release_reserve (void *cls,
723 struct GNUNET_SERVER_Client *client,
724 const struct GNUNET_MessageHeader *message)
726 const struct ReleaseReserveMessage *msg =
727 (const struct ReleaseReserveMessage *) message;
728 struct ReservationList *pos;
729 struct ReservationList *prev;
730 struct ReservationList *next;
731 int rid = ntohl (msg->rid);
732 unsigned long long rem;
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Processing `%s' request\n", "RELEASE_RESERVE");
740 while (NULL != (pos = next))
751 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
752 GNUNET_assert (reserved >= rem);
754 GNUNET_STATISTICS_set (stats,
755 gettext_noop ("# reserved"), reserved, GNUNET_NO);
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758 "Returning %llu remaining reserved bytes to storage pool\n",
762 transmit_status (client, GNUNET_OK, NULL);
768 transmit_status (client, GNUNET_SYSERR,
769 gettext_noop ("Could not find matching reservation"));
774 * Check that the given message is a valid data message.
776 * @return NULL if the message is not well-formed, otherwise the message
778 static const struct DataMessage *
779 check_data (const struct GNUNET_MessageHeader *message)
783 const struct DataMessage *dm;
785 size = ntohs (message->size);
786 if (size < sizeof (struct DataMessage))
791 dm = (const struct DataMessage *) message;
792 dsize = ntohl (dm->size);
793 if (size != dsize + sizeof (struct DataMessage))
803 * Context for a PUT request used to see if the content is
809 * Client to notify on completion.
811 struct GNUNET_SERVER_Client *client;
813 #if ! HAVE_UNALIGNED_64_ACCESS
817 /* followed by the 'struct DataMessage' */
822 * Actually put the data message.
825 execute_put (struct GNUNET_SERVER_Client *client, const struct DataMessage *dm)
831 size = ntohl (dm->size);
833 ret = plugin->api->put (plugin->api->cls,
838 ntohl (dm->priority),
839 ntohl (dm->anonymity),
840 ntohl (dm->replication),
841 GNUNET_TIME_absolute_ntoh (dm->expiration), &msg);
842 if (GNUNET_OK == ret)
844 GNUNET_STATISTICS_update (stats,
845 gettext_noop ("# bytes stored"),
847 GNUNET_CONTAINER_bloomfilter_add (filter, &dm->key);
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Successfully stored %u bytes of type %u under key `%s'\n",
851 size, ntohl (dm->type), GNUNET_h2s (&dm->key));
854 transmit_status (client, ret, msg);
855 GNUNET_free_non_null (msg);
856 if (quota - reserved - cache_size < payload)
858 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
859 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
860 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
861 (unsigned long long) (quota - reserved - cache_size),
862 (unsigned long long) payload);
863 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
869 * Function that will check if the given datastore entry
870 * matches the put and if none match executes the put.
872 * @param cls closure, pointer to the client (of type 'struct PutContext').
873 * @param key key for the content
874 * @param size number of bytes in data
875 * @param data content stored
876 * @param type type of the content
877 * @param priority priority of the content
878 * @param anonymity anonymity-level for the content
879 * @param expiration expiration time for the content
880 * @param uid unique identifier for the datum;
881 * maybe 0 if no unique identifier is available
883 * @return GNUNET_OK usually
884 * GNUNET_NO to delete the item
887 check_present (void *cls,
888 const GNUNET_HashCode * key,
891 enum GNUNET_BLOCK_Type type,
894 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
896 struct PutContext *pc = cls;
897 const struct DataMessage *dm;
899 dm = (const struct DataMessage *) &pc[1];
902 execute_put (pc->client, dm);
903 GNUNET_SERVER_client_drop (pc->client);
907 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
908 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) ||
909 ((size == ntohl (dm->size)) && (0 == memcmp (&dm[1], data, size))))
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Result already present in datastore\n");
915 /* FIXME: change API to allow increasing 'replication' counter */
916 if ((ntohl (dm->priority) > 0) ||
917 (GNUNET_TIME_absolute_ntoh (dm->expiration).abs_value >
918 expiration.abs_value))
919 plugin->api->update (plugin->api->cls,
921 (int32_t) ntohl (dm->priority),
922 GNUNET_TIME_absolute_ntoh (dm->expiration), NULL);
923 transmit_status (pc->client, GNUNET_NO, NULL);
924 GNUNET_SERVER_client_drop (pc->client);
929 execute_put (pc->client, dm);
930 GNUNET_SERVER_client_drop (pc->client);
938 * Handle PUT-message.
941 * @param client identification of the client
942 * @param message the actual message
945 handle_put (void *cls,
946 struct GNUNET_SERVER_Client *client,
947 const struct GNUNET_MessageHeader *message)
949 const struct DataMessage *dm = check_data (message);
951 struct ReservationList *pos;
952 struct PutContext *pc;
953 GNUNET_HashCode vhash;
956 if ((dm == NULL) || (ntohl (dm->type) == 0))
959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Processing `%s' request for `%s' of type %u\n",
965 "PUT", GNUNET_h2s (&dm->key), ntohl (dm->type));
967 rid = ntohl (dm->rid);
968 size = ntohl (dm->size);
972 while ((NULL != pos) && (rid != pos->rid))
974 GNUNET_break (pos != NULL);
977 GNUNET_break (pos->entries > 0);
978 GNUNET_break (pos->amount >= size);
981 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
982 GNUNET_STATISTICS_set (stats,
983 gettext_noop ("# reserved"), reserved, GNUNET_NO);
986 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter, &dm->key))
988 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
989 pc = GNUNET_malloc (sizeof (struct PutContext) + size +
990 sizeof (struct DataMessage));
992 GNUNET_SERVER_client_keep (client);
993 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
994 plugin->api->get_key (plugin->api->cls,
997 &vhash, ntohl (dm->type), &check_present, pc);
1000 execute_put (client, dm);
1005 * Handle GET-message.
1007 * @param cls closure
1008 * @param client identification of the client
1009 * @param message the actual message
1012 handle_get (void *cls,
1013 struct GNUNET_SERVER_Client *client,
1014 const struct GNUNET_MessageHeader *message)
1016 const struct GetMessage *msg;
1019 size = ntohs (message->size);
1020 if ((size != sizeof (struct GetMessage)) &&
1021 (size != sizeof (struct GetMessage) - sizeof (GNUNET_HashCode)))
1024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1027 msg = (const struct GetMessage *) message;
1029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1030 "Processing `%s' request for `%s' of type %u\n",
1031 "GET", GNUNET_h2s (&msg->key), ntohl (msg->type));
1033 GNUNET_STATISTICS_update (stats,
1034 gettext_noop ("# GET requests received"),
1036 GNUNET_SERVER_client_keep (client);
1037 if ((size == sizeof (struct GetMessage)) &&
1038 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter, &msg->key)))
1040 /* don't bother database... */
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1044 "GET", GNUNET_h2s (&msg->key));
1046 GNUNET_STATISTICS_update (stats,
1048 ("# requests filtered by bloomfilter"), 1,
1050 transmit_item (client, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1054 plugin->api->get_key (plugin->api->cls,
1055 GNUNET_ntohll (msg->offset),
1057 sizeof (struct GetMessage)) ? &msg->key : NULL), NULL,
1058 ntohl (msg->type), &transmit_item, client);
1063 * Handle UPDATE-message.
1065 * @param cls closure
1066 * @param client identification of the client
1067 * @param message the actual message
1070 handle_update (void *cls,
1071 struct GNUNET_SERVER_Client *client,
1072 const struct GNUNET_MessageHeader *message)
1074 const struct UpdateMessage *msg;
1078 GNUNET_STATISTICS_update (stats,
1079 gettext_noop ("# UPDATE requests received"),
1081 msg = (const struct UpdateMessage *) message;
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Processing `%s' request for %llu\n",
1086 "UPDATE", (unsigned long long) GNUNET_ntohll (msg->uid));
1088 ret = plugin->api->update (plugin->api->cls,
1089 GNUNET_ntohll (msg->uid),
1090 (int32_t) ntohl (msg->priority),
1091 GNUNET_TIME_absolute_ntoh (msg->expiration),
1093 transmit_status (client, ret, emsg);
1094 GNUNET_free_non_null (emsg);
1099 * Handle GET_REPLICATION-message.
1101 * @param cls closure
1102 * @param client identification of the client
1103 * @param message the actual message
1106 handle_get_replication (void *cls,
1107 struct GNUNET_SERVER_Client *client,
1108 const struct GNUNET_MessageHeader *message)
1111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1112 "Processing `%s' request\n", "GET_REPLICATION");
1114 GNUNET_STATISTICS_update (stats,
1116 ("# GET REPLICATION requests received"), 1,
1118 GNUNET_SERVER_client_keep (client);
1119 plugin->api->get_replication (plugin->api->cls, &transmit_item, client);
1124 * Handle GET_ZERO_ANONYMITY-message.
1126 * @param cls closure
1127 * @param client identification of the client
1128 * @param message the actual message
1131 handle_get_zero_anonymity (void *cls,
1132 struct GNUNET_SERVER_Client *client,
1133 const struct GNUNET_MessageHeader *message)
1135 const struct GetZeroAnonymityMessage *msg =
1136 (const struct GetZeroAnonymityMessage *) message;
1137 enum GNUNET_BLOCK_Type type;
1139 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1140 if (type == GNUNET_BLOCK_TYPE_ANY)
1143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "Processing `%s' request\n", "GET_ZERO_ANONYMITY");
1150 GNUNET_STATISTICS_update (stats,
1152 ("# GET ZERO ANONYMITY requests received"), 1,
1154 GNUNET_SERVER_client_keep (client);
1155 plugin->api->get_zero_anonymity (plugin->api->cls,
1156 GNUNET_ntohll (msg->offset),
1157 type, &transmit_item, client);
1162 * Callback function that will cause the item that is passed
1163 * in to be deleted (by returning GNUNET_NO).
1166 remove_callback (void *cls,
1167 const GNUNET_HashCode * key,
1170 enum GNUNET_BLOCK_Type type,
1173 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1175 struct GNUNET_SERVER_Client *client = cls;
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "No further matches for `%s' request.\n", "REMOVE");
1183 transmit_status (client, GNUNET_NO, _("Content not found"));
1184 GNUNET_SERVER_client_drop (client);
1185 return GNUNET_OK; /* last item */
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1190 (unsigned long long) uid, "REMOVE", GNUNET_h2s (key), type);
1192 GNUNET_STATISTICS_update (stats,
1193 gettext_noop ("# bytes removed (explicit request)"),
1195 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
1196 transmit_status (client, GNUNET_OK, NULL);
1197 GNUNET_SERVER_client_drop (client);
1203 * Handle REMOVE-message.
1205 * @param cls closure
1206 * @param client identification of the client
1207 * @param message the actual message
1210 handle_remove (void *cls,
1211 struct GNUNET_SERVER_Client *client,
1212 const struct GNUNET_MessageHeader *message)
1214 const struct DataMessage *dm = check_data (message);
1215 GNUNET_HashCode vhash;
1220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Processing `%s' request for `%s' of type %u\n",
1226 "REMOVE", GNUNET_h2s (&dm->key), ntohl (dm->type));
1228 GNUNET_STATISTICS_update (stats,
1229 gettext_noop ("# REMOVE requests received"),
1231 GNUNET_SERVER_client_keep (client);
1232 GNUNET_CRYPTO_hash (&dm[1], ntohl (dm->size), &vhash);
1233 plugin->api->get_key (plugin->api->cls,
1237 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1238 &remove_callback, client);
1243 * Handle DROP-message.
1245 * @param cls closure
1246 * @param client identification of the client
1247 * @param message the actual message
1250 handle_drop (void *cls,
1251 struct GNUNET_SERVER_Client *client,
1252 const struct GNUNET_MessageHeader *message)
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "DROP");
1257 do_drop = GNUNET_YES;
1258 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1263 * Function called by plugins to notify us about a
1264 * change in their disk utilization.
1266 * @param cls closure (NULL)
1267 * @param delta change in disk utilization,
1268 * 0 for "reset to empty"
1271 disk_utilization_change_cb (void *cls, int delta)
1273 if ((delta < 0) && (payload < -delta))
1275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1277 ("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1278 (long long) payload, (long long) -delta);
1279 payload = plugin->api->estimate_size (plugin->api->cls);
1285 if (lastSync >= MAX_STAT_SYNC_LAG)
1291 * Callback function to process statistic values.
1293 * @param cls closure (struct Plugin*)
1294 * @param subsystem name of subsystem that created the statistic
1295 * @param name the name of the datum
1296 * @param value the current value
1297 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1298 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1301 process_stat_in (void *cls,
1302 const char *subsystem,
1303 const char *name, uint64_t value, int is_persistent)
1305 GNUNET_assert (stats_worked == GNUNET_NO);
1306 stats_worked = GNUNET_YES;
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1311 abs_value, payload);
1318 process_stat_done (void *cls, int success)
1320 struct DatastorePlugin *plugin = cls;
1323 if (stats_worked == GNUNET_NO)
1324 payload = plugin->api->estimate_size (plugin->api->cls);
1329 * Load the datastore plugin.
1331 static struct DatastorePlugin *
1334 struct DatastorePlugin *ret;
1339 GNUNET_CONFIGURATION_get_value_string (cfg,
1340 "DATASTORE", "DATABASE", &name))
1342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1343 _("No `%s' specified for `%s' in configuration!\n"),
1344 "DATABASE", "DATASTORE");
1347 ret = GNUNET_malloc (sizeof (struct DatastorePlugin));
1349 ret->env.duc = &disk_utilization_change_cb;
1350 ret->env.cls = NULL;
1351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1352 _("Loading `%s' datastore plugin\n"), name);
1353 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1354 ret->short_name = name;
1355 ret->lib_name = libname;
1356 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1357 if (ret->api == NULL)
1359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1360 _("Failed to load datastore plugin for `%s'\n"), name);
1361 GNUNET_free (ret->short_name);
1362 GNUNET_free (libname);
1371 * Function called when the service shuts
1372 * down. Unloads our datastore plugin.
1374 * @param plug plugin to unload
1377 unload_plugin (struct DatastorePlugin *plug)
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Datastore service is unloading plugin...\n");
1383 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1384 GNUNET_free (plug->lib_name);
1385 GNUNET_free (plug->short_name);
1391 * Final task run after shutdown. Unloads plugins and disconnects us from
1395 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1397 if (GNUNET_YES == do_drop)
1398 plugin->api->drop (plugin->api->cls);
1399 unload_plugin (plugin);
1403 GNUNET_CONTAINER_bloomfilter_free (filter);
1408 if (stat_get != NULL)
1410 GNUNET_STATISTICS_get_cancel (stat_get);
1415 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1422 * Last task run during shutdown. Disconnects us from
1423 * the transport and core.
1426 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1428 struct TransmitCallbackContext *tcc;
1430 cleaning_done = GNUNET_YES;
1431 while (NULL != (tcc = tcc_head))
1433 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
1434 if (tcc->th != NULL)
1436 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1437 GNUNET_SERVER_client_drop (tcc->client);
1439 GNUNET_free (tcc->msg);
1442 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1444 GNUNET_SCHEDULER_cancel (expired_kill_task);
1445 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1447 GNUNET_SCHEDULER_add_continuation (&unload_task,
1448 NULL, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1453 * Function that removes all active reservations made
1454 * by the given client and releases the space for other
1457 * @param cls closure
1458 * @param client identification of the client
1461 cleanup_reservations (void *cls, struct GNUNET_SERVER_Client *client)
1463 struct ReservationList *pos;
1464 struct ReservationList *prev;
1465 struct ReservationList *next;
1474 if (pos->client == client)
1477 reservations = next;
1480 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1489 GNUNET_STATISTICS_set (stats,
1490 gettext_noop ("# reserved"), reserved, GNUNET_NO);
1495 * Process datastore requests.
1497 * @param cls closure
1498 * @param server the initialized server
1499 * @param c configuration to use
1503 struct GNUNET_SERVER_Handle *server,
1504 const struct GNUNET_CONFIGURATION_Handle *c)
1506 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1507 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1508 sizeof (struct ReserveMessage)},
1509 {&handle_release_reserve, NULL,
1510 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1511 sizeof (struct ReleaseReserveMessage)},
1512 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0},
1513 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1514 sizeof (struct UpdateMessage)},
1515 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0},
1516 {&handle_get_replication, NULL,
1517 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1518 sizeof (struct GNUNET_MessageHeader)},
1519 {&handle_get_zero_anonymity, NULL,
1520 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1521 sizeof (struct GetZeroAnonymityMessage)},
1522 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0},
1523 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1524 sizeof (struct GNUNET_MessageHeader)},
1528 unsigned int bf_size;
1532 GNUNET_CONFIGURATION_get_value_number (cfg, "DATASTORE", "QUOTA", "a))
1534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1535 _("No `%s' specified for `%s' in configuration!\n"),
1536 "QUOTA", "DATASTORE");
1539 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1540 GNUNET_STATISTICS_set (stats, gettext_noop ("# quota"), quota, GNUNET_NO);
1541 cache_size = quota / 8; /* Or should we make this an option? */
1542 GNUNET_STATISTICS_set (stats,
1543 gettext_noop ("# cache size"), cache_size, GNUNET_NO);
1544 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1547 GNUNET_CONFIGURATION_get_value_filename (cfg,
1551 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1554 _("Could not use specified filename `%s' for bloomfilter.\n"),
1555 fn != NULL ? fn : "");
1556 GNUNET_free_non_null (fn);
1560 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1562 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1563 GNUNET_free_non_null (fn);
1566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1567 _("Failed to initialize bloomfilter.\n"));
1570 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1575 plugin = load_plugin ();
1578 GNUNET_CONTAINER_bloomfilter_free (filter);
1582 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1587 stat_get = GNUNET_STATISTICS_get (stats,
1590 GNUNET_TIME_UNIT_SECONDS,
1592 &process_stat_in, plugin);
1593 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1594 GNUNET_SERVER_add_handlers (server, handlers);
1596 = GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1597 &delete_expired, NULL);
1598 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1599 &cleaning_task, NULL);
1604 * The main function for the datastore service.
1606 * @param argc number of arguments from the command line
1607 * @param argv command line arguments
1608 * @return 0 ok, 1 on error
1611 main (int argc, char *const *argv)
1616 GNUNET_SERVICE_run (argc,
1619 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1624 /* end of gnunet-service-datastore.c */