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
279 static void delete_expired (void *cls,
280 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, const GNUNET_HashCode * key, uint32_t size,
305 const void *data, enum GNUNET_BLOCK_Type type,
306 uint32_t priority, uint32_t anonymity,
307 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
309 struct GNUNET_TIME_Absolute now;
314 GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY, &delete_expired, NULL);
315 return GNUNET_SYSERR;
317 now = GNUNET_TIME_absolute_get ();
318 if (expiration.abs_value > now.abs_value)
320 /* finished processing */
322 GNUNET_SCHEDULER_add_delayed (MAX_EXPIRE_DELAY, &delete_expired, NULL);
323 return GNUNET_SYSERR;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Deleting content `%s' of type %u that expired %llu ms ago\n",
328 GNUNET_h2s (key), type,
329 (unsigned long long) (now.abs_value - expiration.abs_value));
331 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes expired"), size,
333 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
335 GNUNET_SCHEDULER_add_delayed (MIN_EXPIRE_DELAY, &delete_expired, NULL);
341 * Task that is used to remove expired entries from
342 * the datastore. This task will schedule itself
343 * again automatically to always delete all expired
346 * @param cls not used
347 * @param tc task context
350 delete_expired (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
352 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
353 plugin->api->get_expiration (plugin->api->cls, &expired_processor, NULL);
358 * An iterator over a set of items stored in the datastore
359 * that deletes until we're happy with respect to our quota.
362 * @param key key for the content
363 * @param size number of bytes in data
364 * @param data content stored
365 * @param type type of the content
366 * @param priority priority of the content
367 * @param anonymity anonymity-level for the content
368 * @param expiration expiration time for the content
369 * @param uid unique identifier for the datum;
370 * maybe 0 if no unique identifier is available
372 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
373 * (continue on call to "next", of course),
374 * GNUNET_NO to delete the item and continue (if supported)
377 quota_processor (void *cls, const GNUNET_HashCode * key, uint32_t size,
378 const void *data, enum GNUNET_BLOCK_Type type,
379 uint32_t priority, uint32_t anonymity,
380 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
382 unsigned long long *need = cls;
385 return GNUNET_SYSERR;
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Deleting %llu bytes of low-priority content `%s' of type %u (still trying to free another %llu bytes)\n",
389 (unsigned long long) (size + GNUNET_DATASTORE_ENTRY_OVERHEAD),
390 GNUNET_h2s (key), type, *need);
392 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
395 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
396 GNUNET_STATISTICS_update (stats,
397 gettext_noop ("# bytes purged (low-priority)"),
399 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
405 * Manage available disk space by running tasks
406 * that will discard content if necessary. This
407 * function will be run whenever a request for
408 * "need" bytes of storage could only be satisfied
409 * by eating into the "cache" (and we want our cache
412 * @param need number of bytes of content that were
413 * placed into the "cache" (and hence the
414 * number of bytes that should be removed).
417 manage_space (unsigned long long need)
419 unsigned long long last;
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Asked to free up %llu bytes of cache space\n", need);
426 while ((need > 0) && (last != need))
429 plugin->api->get_expiration (plugin->api->cls, "a_processor, &need);
435 * Function called to notify a client about the socket
436 * begin ready to queue more data. "buf" will be
437 * NULL and "size" zero if the socket was closed for
438 * writing in the meantime.
441 * @param size number of bytes available in buf
442 * @param buf where the callee should write the message
443 * @return number of bytes written to buf
446 transmit_callback (void *cls, size_t size, void *buf)
448 struct TransmitCallbackContext *tcc = cls;
452 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
453 msize = ntohs (tcc->msg->size);
456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 _("Transmission to client failed!\n"));
458 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
459 GNUNET_SERVER_client_drop (tcc->client);
460 GNUNET_free (tcc->msg);
464 GNUNET_assert (size >= msize);
465 memcpy (buf, tcc->msg, msize);
466 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
467 GNUNET_SERVER_client_drop (tcc->client);
468 GNUNET_free (tcc->msg);
475 * Transmit the given message to the client.
477 * @param client target of the message
478 * @param msg message to transmit, will be freed!
481 transmit (struct GNUNET_SERVER_Client *client, struct GNUNET_MessageHeader *msg)
483 struct TransmitCallbackContext *tcc;
485 if (GNUNET_YES == cleaning_done)
488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489 "Shutdown in progress, aborting transmission.\n");
491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
495 tcc = GNUNET_malloc (sizeof (struct TransmitCallbackContext));
497 tcc->client = client;
500 GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
501 GNUNET_TIME_UNIT_FOREVER_REL,
502 &transmit_callback, tcc)))
505 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
510 GNUNET_SERVER_client_keep (client);
511 GNUNET_CONTAINER_DLL_insert (tcc_head, tcc_tail, tcc);
516 * Transmit a status code to the client.
518 * @param client receiver of the response
519 * @param code status code
520 * @param msg optional error message (can be NULL)
523 transmit_status (struct GNUNET_SERVER_Client *client, int code, const char *msg)
525 struct StatusMessage *sm;
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Transmitting `%s' message with value %d and message `%s'\n",
531 "STATUS", code, msg != NULL ? msg : "(none)");
533 slen = (msg == NULL) ? 0 : strlen (msg) + 1;
534 sm = GNUNET_malloc (sizeof (struct StatusMessage) + slen);
535 sm->header.size = htons (sizeof (struct StatusMessage) + slen);
536 sm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
537 sm->status = htonl (code);
539 memcpy (&sm[1], msg, slen);
540 transmit (client, &sm->header);
546 * Function that will transmit the given datastore entry
549 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
550 * @param key key for the content
551 * @param size number of bytes in data
552 * @param data content stored
553 * @param type type of the content
554 * @param priority priority of the content
555 * @param anonymity anonymity-level for the content
556 * @param expiration expiration time for the content
557 * @param uid unique identifier for the datum;
558 * maybe 0 if no unique identifier is available
560 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
561 * GNUNET_NO to delete the item and continue (if supported)
564 transmit_item (void *cls, const GNUNET_HashCode * key, uint32_t size,
565 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
566 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
569 struct GNUNET_SERVER_Client *client = cls;
570 struct GNUNET_MessageHeader *end;
571 struct DataMessage *dm;
575 /* transmit 'DATA_END' */
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' message\n",
580 end = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader));
581 end->size = htons (sizeof (struct GNUNET_MessageHeader));
582 end->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
583 transmit (client, end);
584 GNUNET_SERVER_client_drop (client);
587 GNUNET_assert (sizeof (struct DataMessage) + size <
588 GNUNET_SERVER_MAX_MESSAGE_SIZE);
589 dm = GNUNET_malloc (sizeof (struct DataMessage) + size);
590 dm->header.size = htons (sizeof (struct DataMessage) + size);
591 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
593 dm->size = htonl (size);
594 dm->type = htonl (type);
595 dm->priority = htonl (priority);
596 dm->anonymity = htonl (anonymity);
597 dm->replication = htonl (0);
598 dm->reserved = htonl (0);
599 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
600 dm->uid = GNUNET_htonll (uid);
602 memcpy (&dm[1], data, size);
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Transmitting `%s' message for `%s' of type %u with expiration %llu (now: %llu)\n",
606 "DATA", GNUNET_h2s (key), type,
607 (unsigned long long) expiration.abs_value,
608 (unsigned long long) GNUNET_TIME_absolute_get ().abs_value);
610 GNUNET_STATISTICS_update (stats, gettext_noop ("# results found"), 1,
612 transmit (client, &dm->header);
613 GNUNET_SERVER_client_drop (client);
619 * Handle RESERVE-message.
622 * @param client identification of the client
623 * @param message the actual message
626 handle_reserve (void *cls, struct GNUNET_SERVER_Client *client,
627 const struct GNUNET_MessageHeader *message)
630 * Static counter to produce reservation identifiers.
632 static int reservation_gen;
634 const struct ReserveMessage *msg = (const struct ReserveMessage *) message;
635 struct ReservationList *e;
636 unsigned long long used;
637 unsigned long long req;
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "RESERVE");
644 amount = GNUNET_ntohll (msg->amount);
645 entries = ntohl (msg->entries);
646 used = payload + reserved;
648 amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
649 if (used + req > quota)
652 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
655 ("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
656 quota - used, "RESERVE", req);
657 if (cache_size < req)
659 /* TODO: document this in the FAQ; essentially, if this
660 * message happens, the insertion request could be blocked
661 * by less-important content from migration because it is
662 * larger than 1/8th of the overall available space, and
663 * we only reserve 1/8th for "fresh" insertions */
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
666 ("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
668 transmit_status (client, 0,
670 ("Insufficient space to satisfy request and "
671 "requested amount is larger than cache size"));
675 transmit_status (client, 0,
676 gettext_noop ("Insufficient space to satisfy request"));
681 GNUNET_STATISTICS_set (stats, gettext_noop ("# reserved"), reserved,
683 e = GNUNET_malloc (sizeof (struct ReservationList));
684 e->next = reservations;
688 e->entries = entries;
689 e->rid = ++reservation_gen;
690 if (reservation_gen < 0)
691 reservation_gen = 0; /* wrap around */
692 transmit_status (client, e->rid, NULL);
697 * Handle RELEASE_RESERVE-message.
700 * @param client identification of the client
701 * @param message the actual message
704 handle_release_reserve (void *cls, struct GNUNET_SERVER_Client *client,
705 const struct GNUNET_MessageHeader *message)
707 const struct ReleaseReserveMessage *msg =
708 (const struct ReleaseReserveMessage *) message;
709 struct ReservationList *pos;
710 struct ReservationList *prev;
711 struct ReservationList *next;
712 int rid = ntohl (msg->rid);
713 unsigned long long rem;
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n",
721 while (NULL != (pos = next))
732 ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
733 GNUNET_assert (reserved >= rem);
735 GNUNET_STATISTICS_set (stats, gettext_noop ("# reserved"), reserved,
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Returning %llu remaining reserved bytes to storage pool\n",
743 transmit_status (client, GNUNET_OK, NULL);
749 transmit_status (client, GNUNET_SYSERR,
750 gettext_noop ("Could not find matching reservation"));
755 * Check that the given message is a valid data message.
757 * @return NULL if the message is not well-formed, otherwise the message
759 static const struct DataMessage *
760 check_data (const struct GNUNET_MessageHeader *message)
764 const struct DataMessage *dm;
766 size = ntohs (message->size);
767 if (size < sizeof (struct DataMessage))
772 dm = (const struct DataMessage *) message;
773 dsize = ntohl (dm->size);
774 if (size != dsize + sizeof (struct DataMessage))
784 * Context for a PUT request used to see if the content is
790 * Client to notify on completion.
792 struct GNUNET_SERVER_Client *client;
794 #if ! HAVE_UNALIGNED_64_ACCESS
798 /* followed by the 'struct DataMessage' */
803 * Actually put the data message.
806 execute_put (struct GNUNET_SERVER_Client *client, const struct DataMessage *dm)
812 size = ntohl (dm->size);
815 plugin->api->put (plugin->api->cls, &dm->key, size, &dm[1],
816 ntohl (dm->type), ntohl (dm->priority),
817 ntohl (dm->anonymity), ntohl (dm->replication),
818 GNUNET_TIME_absolute_ntoh (dm->expiration), &msg);
819 if (GNUNET_OK == ret)
821 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes stored"), size,
823 GNUNET_CONTAINER_bloomfilter_add (filter, &dm->key);
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Successfully stored %u bytes of type %u under key `%s'\n",
827 size, ntohl (dm->type), GNUNET_h2s (&dm->key));
830 transmit_status (client, ret, msg);
831 GNUNET_free_non_null (msg);
832 if (quota - reserved - cache_size < payload)
834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835 _("Need %llu bytes more space (%llu allowed, using %llu)\n"),
836 (unsigned long long) size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
837 (unsigned long long) (quota - reserved - cache_size),
838 (unsigned long long) payload);
839 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
845 * Function that will check if the given datastore entry
846 * matches the put and if none match executes the put.
848 * @param cls closure, pointer to the client (of type 'struct PutContext').
849 * @param key key for the content
850 * @param size number of bytes in data
851 * @param data content stored
852 * @param type type of the content
853 * @param priority priority of the content
854 * @param anonymity anonymity-level for the content
855 * @param expiration expiration time for the content
856 * @param uid unique identifier for the datum;
857 * maybe 0 if no unique identifier is available
859 * @return GNUNET_OK usually
860 * GNUNET_NO to delete the item
863 check_present (void *cls, const GNUNET_HashCode * key, uint32_t size,
864 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
865 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
868 struct PutContext *pc = cls;
869 const struct DataMessage *dm;
871 dm = (const struct DataMessage *) &pc[1];
874 execute_put (pc->client, dm);
875 GNUNET_SERVER_client_drop (pc->client);
879 if ((GNUNET_BLOCK_TYPE_FS_DBLOCK == type) ||
880 (GNUNET_BLOCK_TYPE_FS_IBLOCK == type) || ((size == ntohl (dm->size)) &&
882 memcmp (&dm[1], data, size))))
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Result already present in datastore\n");
888 /* FIXME: change API to allow increasing 'replication' counter */
889 if ((ntohl (dm->priority) > 0) ||
890 (GNUNET_TIME_absolute_ntoh (dm->expiration).abs_value >
891 expiration.abs_value))
892 plugin->api->update (plugin->api->cls, uid,
893 (int32_t) ntohl (dm->priority),
894 GNUNET_TIME_absolute_ntoh (dm->expiration), NULL);
895 transmit_status (pc->client, GNUNET_NO, NULL);
896 GNUNET_SERVER_client_drop (pc->client);
901 execute_put (pc->client, dm);
902 GNUNET_SERVER_client_drop (pc->client);
910 * Handle PUT-message.
913 * @param client identification of the client
914 * @param message the actual message
917 handle_put (void *cls, struct GNUNET_SERVER_Client *client,
918 const struct GNUNET_MessageHeader *message)
920 const struct DataMessage *dm = check_data (message);
922 struct ReservationList *pos;
923 struct PutContext *pc;
924 GNUNET_HashCode vhash;
927 if ((dm == NULL) || (ntohl (dm->type) == 0))
930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "Processing `%s' request for `%s' of type %u\n", "PUT",
936 GNUNET_h2s (&dm->key), ntohl (dm->type));
938 rid = ntohl (dm->rid);
939 size = ntohl (dm->size);
943 while ((NULL != pos) && (rid != pos->rid))
945 GNUNET_break (pos != NULL);
948 GNUNET_break (pos->entries > 0);
949 GNUNET_break (pos->amount >= size);
952 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
953 GNUNET_STATISTICS_set (stats, gettext_noop ("# reserved"), reserved,
957 if (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (filter, &dm->key))
959 GNUNET_CRYPTO_hash (&dm[1], size, &vhash);
960 pc = GNUNET_malloc (sizeof (struct PutContext) + size +
961 sizeof (struct DataMessage));
963 GNUNET_SERVER_client_keep (client);
964 memcpy (&pc[1], dm, size + sizeof (struct DataMessage));
965 plugin->api->get_key (plugin->api->cls, 0, &dm->key, &vhash,
966 ntohl (dm->type), &check_present, pc);
969 execute_put (client, dm);
974 * Handle GET-message.
977 * @param client identification of the client
978 * @param message the actual message
981 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
982 const struct GNUNET_MessageHeader *message)
984 const struct GetMessage *msg;
987 size = ntohs (message->size);
988 if ((size != sizeof (struct GetMessage)) &&
989 (size != sizeof (struct GetMessage) - sizeof (GNUNET_HashCode)))
992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
995 msg = (const struct GetMessage *) message;
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Processing `%s' request for `%s' of type %u\n", "GET",
999 GNUNET_h2s (&msg->key), ntohl (msg->type));
1001 GNUNET_STATISTICS_update (stats, gettext_noop ("# GET requests received"), 1,
1003 GNUNET_SERVER_client_keep (client);
1004 if ((size == sizeof (struct GetMessage)) &&
1005 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter, &msg->key)))
1007 /* don't bother database... */
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Empty result set for `%s' request for `%s' (bloomfilter).\n",
1011 "GET", GNUNET_h2s (&msg->key));
1013 GNUNET_STATISTICS_update (stats,
1015 ("# requests filtered by bloomfilter"), 1,
1017 transmit_item (client, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS,
1021 plugin->api->get_key (plugin->api->cls, GNUNET_ntohll (msg->offset),
1023 sizeof (struct GetMessage)) ? &msg->key : NULL), NULL,
1024 ntohl (msg->type), &transmit_item, client);
1029 * Handle UPDATE-message.
1031 * @param cls closure
1032 * @param client identification of the client
1033 * @param message the actual message
1036 handle_update (void *cls, struct GNUNET_SERVER_Client *client,
1037 const struct GNUNET_MessageHeader *message)
1039 const struct UpdateMessage *msg;
1043 GNUNET_STATISTICS_update (stats, gettext_noop ("# UPDATE requests received"),
1045 msg = (const struct UpdateMessage *) message;
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request for %llu\n",
1049 "UPDATE", (unsigned long long) GNUNET_ntohll (msg->uid));
1052 plugin->api->update (plugin->api->cls, GNUNET_ntohll (msg->uid),
1053 (int32_t) ntohl (msg->priority),
1054 GNUNET_TIME_absolute_ntoh (msg->expiration), &emsg);
1055 transmit_status (client, ret, emsg);
1056 GNUNET_free_non_null (emsg);
1061 * Handle GET_REPLICATION-message.
1063 * @param cls closure
1064 * @param client identification of the client
1065 * @param message the actual message
1068 handle_get_replication (void *cls, struct GNUNET_SERVER_Client *client,
1069 const struct GNUNET_MessageHeader *message)
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n",
1075 GNUNET_STATISTICS_update (stats,
1077 ("# GET REPLICATION requests received"), 1,
1079 GNUNET_SERVER_client_keep (client);
1080 plugin->api->get_replication (plugin->api->cls, &transmit_item, client);
1085 * Handle GET_ZERO_ANONYMITY-message.
1087 * @param cls closure
1088 * @param client identification of the client
1089 * @param message the actual message
1092 handle_get_zero_anonymity (void *cls, struct GNUNET_SERVER_Client *client,
1093 const struct GNUNET_MessageHeader *message)
1095 const struct GetZeroAnonymityMessage *msg =
1096 (const struct GetZeroAnonymityMessage *) message;
1097 enum GNUNET_BLOCK_Type type;
1099 type = (enum GNUNET_BLOCK_Type) ntohl (msg->type);
1100 if (type == GNUNET_BLOCK_TYPE_ANY)
1103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n",
1108 "GET_ZERO_ANONYMITY");
1110 GNUNET_STATISTICS_update (stats,
1112 ("# GET ZERO ANONYMITY requests received"), 1,
1114 GNUNET_SERVER_client_keep (client);
1115 plugin->api->get_zero_anonymity (plugin->api->cls,
1116 GNUNET_ntohll (msg->offset), type,
1117 &transmit_item, client);
1122 * Callback function that will cause the item that is passed
1123 * in to be deleted (by returning GNUNET_NO).
1126 remove_callback (void *cls, const GNUNET_HashCode * key, uint32_t size,
1127 const void *data, enum GNUNET_BLOCK_Type type,
1128 uint32_t priority, uint32_t anonymity,
1129 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
1131 struct GNUNET_SERVER_Client *client = cls;
1136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137 "No further matches for `%s' request.\n", "REMOVE");
1139 transmit_status (client, GNUNET_NO, _("Content not found"));
1140 GNUNET_SERVER_client_drop (client);
1141 return GNUNET_OK; /* last item */
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Item %llu matches `%s' request for key `%s' and type %u.\n",
1146 (unsigned long long) uid, "REMOVE", GNUNET_h2s (key), type);
1148 GNUNET_STATISTICS_update (stats,
1149 gettext_noop ("# bytes removed (explicit request)"),
1151 GNUNET_CONTAINER_bloomfilter_remove (filter, key);
1152 transmit_status (client, GNUNET_OK, NULL);
1153 GNUNET_SERVER_client_drop (client);
1159 * Handle REMOVE-message.
1161 * @param cls closure
1162 * @param client identification of the client
1163 * @param message the actual message
1166 handle_remove (void *cls, struct GNUNET_SERVER_Client *client,
1167 const struct GNUNET_MessageHeader *message)
1169 const struct DataMessage *dm = check_data (message);
1170 GNUNET_HashCode vhash;
1175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Processing `%s' request for `%s' of type %u\n", "REMOVE",
1181 GNUNET_h2s (&dm->key), ntohl (dm->type));
1183 GNUNET_STATISTICS_update (stats, gettext_noop ("# REMOVE requests received"),
1185 GNUNET_SERVER_client_keep (client);
1186 GNUNET_CRYPTO_hash (&dm[1], ntohl (dm->size), &vhash);
1187 plugin->api->get_key (plugin->api->cls, 0, &dm->key, &vhash,
1188 (enum GNUNET_BLOCK_Type) ntohl (dm->type),
1189 &remove_callback, client);
1194 * Handle DROP-message.
1196 * @param cls closure
1197 * @param client identification of the client
1198 * @param message the actual message
1201 handle_drop (void *cls, struct GNUNET_SERVER_Client *client,
1202 const struct GNUNET_MessageHeader *message)
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' request\n", "DROP");
1207 do_drop = GNUNET_YES;
1208 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1213 * Function called by plugins to notify us about a
1214 * change in their disk utilization.
1216 * @param cls closure (NULL)
1217 * @param delta change in disk utilization,
1218 * 0 for "reset to empty"
1221 disk_utilization_change_cb (void *cls, int delta)
1223 if ((delta < 0) && (payload < -delta))
1225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1227 ("Datastore payload inaccurate (%lld < %lld). Trying to fix.\n"),
1228 (long long) payload, (long long) -delta);
1229 payload = plugin->api->estimate_size (plugin->api->cls);
1235 if (lastSync >= MAX_STAT_SYNC_LAG)
1241 * Callback function to process statistic values.
1243 * @param cls closure (struct Plugin*)
1244 * @param subsystem name of subsystem that created the statistic
1245 * @param name the name of the datum
1246 * @param value the current value
1247 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1248 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1251 process_stat_in (void *cls, const char *subsystem, const char *name,
1252 uint64_t value, int is_persistent)
1254 GNUNET_assert (stats_worked == GNUNET_NO);
1255 stats_worked = GNUNET_YES;
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Notification from statistics about existing payload (%llu), new payload is %llu\n",
1260 abs_value, payload);
1267 process_stat_done (void *cls, int success)
1269 struct DatastorePlugin *plugin = cls;
1272 if (stats_worked == GNUNET_NO)
1273 payload = plugin->api->estimate_size (plugin->api->cls);
1278 * Load the datastore plugin.
1280 static struct DatastorePlugin *
1283 struct DatastorePlugin *ret;
1288 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
1291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1292 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
1296 ret = GNUNET_malloc (sizeof (struct DatastorePlugin));
1298 ret->env.duc = &disk_utilization_change_cb;
1299 ret->env.cls = NULL;
1300 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
1302 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1303 ret->short_name = name;
1304 ret->lib_name = libname;
1305 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1306 if (ret->api == NULL)
1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1309 _("Failed to load datastore plugin for `%s'\n"), name);
1310 GNUNET_free (ret->short_name);
1311 GNUNET_free (libname);
1320 * Function called when the service shuts
1321 * down. Unloads our datastore plugin.
1323 * @param plug plugin to unload
1326 unload_plugin (struct DatastorePlugin *plug)
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Datastore service is unloading plugin...\n");
1332 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1333 GNUNET_free (plug->lib_name);
1334 GNUNET_free (plug->short_name);
1340 * Final task run after shutdown. Unloads plugins and disconnects us from
1344 unload_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1346 if (GNUNET_YES == do_drop)
1347 plugin->api->drop (plugin->api->cls);
1348 unload_plugin (plugin);
1352 GNUNET_CONTAINER_bloomfilter_free (filter);
1357 if (stat_get != NULL)
1359 GNUNET_STATISTICS_get_cancel (stat_get);
1364 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1371 * Last task run during shutdown. Disconnects us from
1372 * the transport and core.
1375 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1377 struct TransmitCallbackContext *tcc;
1379 cleaning_done = GNUNET_YES;
1380 while (NULL != (tcc = tcc_head))
1382 GNUNET_CONTAINER_DLL_remove (tcc_head, tcc_tail, tcc);
1383 if (tcc->th != NULL)
1385 GNUNET_CONNECTION_notify_transmit_ready_cancel (tcc->th);
1386 GNUNET_SERVER_client_drop (tcc->client);
1388 GNUNET_free (tcc->msg);
1391 if (expired_kill_task != GNUNET_SCHEDULER_NO_TASK)
1393 GNUNET_SCHEDULER_cancel (expired_kill_task);
1394 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
1396 GNUNET_SCHEDULER_add_continuation (&unload_task, NULL,
1397 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1402 * Function that removes all active reservations made
1403 * by the given client and releases the space for other
1406 * @param cls closure
1407 * @param client identification of the client
1410 cleanup_reservations (void *cls, struct GNUNET_SERVER_Client *client)
1412 struct ReservationList *pos;
1413 struct ReservationList *prev;
1414 struct ReservationList *next;
1423 if (pos->client == client)
1426 reservations = next;
1429 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1438 GNUNET_STATISTICS_set (stats, gettext_noop ("# reserved"), reserved,
1444 * Process datastore requests.
1446 * @param cls closure
1447 * @param server the initialized server
1448 * @param c configuration to use
1451 run (void *cls, struct GNUNET_SERVER_Handle *server,
1452 const struct GNUNET_CONFIGURATION_Handle *c)
1454 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1455 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1456 sizeof (struct ReserveMessage)},
1457 {&handle_release_reserve, NULL,
1458 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1459 sizeof (struct ReleaseReserveMessage)},
1460 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0},
1461 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1462 sizeof (struct UpdateMessage)},
1463 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0},
1464 {&handle_get_replication, NULL,
1465 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION,
1466 sizeof (struct GNUNET_MessageHeader)},
1467 {&handle_get_zero_anonymity, NULL,
1468 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY,
1469 sizeof (struct GetZeroAnonymityMessage)},
1470 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0},
1471 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1472 sizeof (struct GNUNET_MessageHeader)},
1476 unsigned int bf_size;
1480 GNUNET_CONFIGURATION_get_value_number (cfg, "DATASTORE", "QUOTA", "a))
1482 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1483 _("No `%s' specified for `%s' in configuration!\n"), "QUOTA",
1487 stats = GNUNET_STATISTICS_create ("datastore", cfg);
1488 GNUNET_STATISTICS_set (stats, gettext_noop ("# quota"), quota, GNUNET_NO);
1489 cache_size = quota / 8; /* Or should we make this an option? */
1490 GNUNET_STATISTICS_set (stats, gettext_noop ("# cache size"), cache_size,
1492 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1495 GNUNET_CONFIGURATION_get_value_filename (cfg, "DATASTORE", "BLOOMFILTER",
1497 (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn)))
1499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1500 _("Could not use specified filename `%s' for bloomfilter.\n"),
1501 fn != NULL ? fn : "");
1502 GNUNET_free_non_null (fn);
1506 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1508 filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
1509 GNUNET_free_non_null (fn);
1512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1513 _("Failed to initialize bloomfilter.\n"));
1516 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1521 plugin = load_plugin ();
1524 GNUNET_CONTAINER_bloomfilter_free (filter);
1528 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1534 GNUNET_STATISTICS_get (stats, "datastore", QUOTA_STAT_NAME,
1535 GNUNET_TIME_UNIT_SECONDS, &process_stat_done,
1536 &process_stat_in, plugin);
1537 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1538 GNUNET_SERVER_add_handlers (server, handlers);
1540 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
1541 &delete_expired, NULL);
1542 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1548 * The main function for the datastore service.
1550 * @param argc number of arguments from the command line
1551 * @param argv command line arguments
1552 * @return 0 ok, 1 on error
1555 main (int argc, char *const *argv)
1561 GNUNET_SERVICE_run (argc, argv, "datastore", GNUNET_SERVICE_OPTION_NONE,
1562 &run, NULL)) ? 0 : 1;
1567 /* end of gnunet-service-datastore.c */