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
27 * quota management code:
29 * - track reservations
30 * - refuse above-quota
31 * - content expiration job
32 * - near-quota low-priority content discard job
36 #include "gnunet_util_lib.h"
37 #include "gnunet_protocols.h"
38 #include "plugin_datastore.h"
39 #include "datastore.h"
41 #define DEBUG_DATASTORE GNUNET_YES
44 * How many messages do we queue at most per client?
46 #define MAX_PENDING 1024
50 * Our datastore plugin.
52 struct DatastorePlugin
56 * API of the transport as returned by the plugin's
57 * initialization function.
59 struct GNUNET_DATASTORE_PluginFunctions *api;
62 * Short name for the plugin (i.e. "sqlite").
67 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
72 * Environment this transport service is using
75 struct GNUNET_DATASTORE_PluginEnvironment env;
81 * Linked list of active reservations.
83 struct ReservationList
87 * This is a linked list.
89 struct ReservationList *next;
92 * Client that made the reservation.
94 struct GNUNET_SERVER_Client *client;
97 * Number of bytes (still) reserved.
102 * Number of items (still) reserved.
107 * Reservation identifier.
115 * Our datastore plugin (NULL if not available).
117 static struct DatastorePlugin *plugin;
120 * Linked list of space reservations made by clients.
122 static struct ReservationList *reservations;
125 * Bloomfilter to quickly tell if we don't have the content.
127 static struct GNUNET_CONTAINER_BloomFilter *filter;
130 * Static counter to produce reservation identifiers.
132 static int reservation_gen;
135 * How much space are we allowed to use?
137 static unsigned long long quota;
141 * Function called once the transmit operation has
142 * either failed or succeeded.
145 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
147 typedef void (*TransmitContinuation)(void *cls,
150 struct TransmitCallbackContext
153 * The message that we're asked to transmit.
155 struct GNUNET_MessageHeader *msg;
158 * Client that we are transmitting to.
160 struct GNUNET_SERVER_Client *client;
163 * Function to call once msg has been transmitted
164 * (or at least added to the buffer).
166 TransmitContinuation tc;
174 * GNUNET_YES if we are supposed to signal the server
175 * completion of the client's request.
182 * Function called to notify a client about the socket
183 * begin ready to queue more data. "buf" will be
184 * NULL and "size" zero if the socket was closed for
185 * writing in the meantime.
188 * @param size number of bytes available in buf
189 * @param buf where the callee should write the message
190 * @return number of bytes written to buf
193 transmit_callback (void *cls,
194 size_t size, void *buf)
196 struct TransmitCallbackContext *tcc = cls;
199 msize = ntohs(tcc->msg->size);
203 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
204 "Transmission failed.\n");
207 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
208 if (GNUNET_YES == tcc->end)
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 "Disconnecting client.\n");
214 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
216 GNUNET_free (tcc->msg);
220 GNUNET_assert (size >= msize);
221 memcpy (buf, tcc->msg, msize);
223 tcc->tc (tcc->tc_cls, GNUNET_OK);
224 if (GNUNET_YES == tcc->end)
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Request completed, ready for the next request!\n");
230 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Response transmitted, more pending!\n");
239 GNUNET_free (tcc->msg);
246 * Transmit the given message to the client.
248 * @param client target of the message
249 * @param msg message to transmit, will be freed!
250 * @param end is this the last response (and we should
251 * signal the server completion accodingly after
252 * transmitting this message)?
255 transmit (struct GNUNET_SERVER_Client *client,
256 struct GNUNET_MessageHeader *msg,
257 TransmitContinuation tc,
261 struct TransmitCallbackContext *tcc;
263 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
265 tcc->client = client;
267 tcc->tc_cls = tc_cls;
271 GNUNET_SERVER_notify_transmit_ready (client,
273 GNUNET_TIME_UNIT_FOREVER_REL,
278 if (GNUNET_YES == end)
281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
282 "Disconnecting client.\n");
284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
287 tc (tc_cls, GNUNET_SYSERR);
295 * Transmit a status code to the client.
297 * @param client receiver of the response
298 * @param code status code
299 * @param msg optional error message (can be NULL)
302 transmit_status (struct GNUNET_SERVER_Client *client,
306 struct StatusMessage *sm;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Transmitting `s' message with value %d and message %s\n",
316 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
317 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
318 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
319 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
320 sm->status = htonl(code);
321 memcpy (&sm[1], msg, slen);
322 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
327 * Function called once the transmit operation has
328 * either failed or succeeded.
331 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
334 get_next(void *next_cls,
337 if (status != GNUNET_OK)
339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
340 _("Failed to transmit an item to the client; aborting iteration.\n"));
341 plugin->api->next_request (next_cls, GNUNET_YES);
344 plugin->api->next_request (next_cls, GNUNET_NO);
349 * Function that will transmit the given datastore entry
352 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
353 * @param next_cls closure to use to ask for the next item
354 * @param key key for the content
355 * @param size number of bytes in data
356 * @param data content stored
357 * @param type type of the content
358 * @param priority priority of the content
359 * @param anonymity anonymity-level for the content
360 * @param expiration expiration time for the content
361 * @param uid unique identifier for the datum;
362 * maybe 0 if no unique identifier is available
364 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
365 * GNUNET_NO to delete the item and continue (if supported)
368 transmit_item (void *cls,
370 const GNUNET_HashCode * key,
376 struct GNUNET_TIME_Absolute
377 expiration, uint64_t uid)
379 struct GNUNET_SERVER_Client *client = cls;
380 struct GNUNET_MessageHeader *end;
381 struct DataMessage *dm;
385 /* transmit 'DATA_END' */
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Transmitting `%s' message\n",
391 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
392 end->size = htons(sizeof(struct GNUNET_MessageHeader));
393 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
394 transmit (client, end, NULL, NULL, GNUNET_YES);
395 GNUNET_SERVER_client_drop (client);
398 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
399 dm->header.size = htons(sizeof(struct DataMessage) + size);
400 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
402 dm->size = htonl(size);
403 dm->type = htonl(type);
404 dm->priority = htonl(priority);
405 dm->anonymity = htonl(anonymity);
406 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
407 dm->uid = GNUNET_htonll(uid);
409 memcpy (&dm[1], data, size);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Transmitting `%s' message\n",
415 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
421 * Handle RESERVE-message.
424 * @param client identification of the client
425 * @param message the actual message
428 handle_reserve (void *cls,
429 struct GNUNET_SERVER_Client *client,
430 const struct GNUNET_MessageHeader *message)
432 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
433 struct ReservationList *e;
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Processing `%s' request\n",
440 /* FIXME: check if we have that much space... */
441 e = GNUNET_malloc (sizeof(struct ReservationList));
442 e->next = reservations;
445 e->amount = GNUNET_ntohll(msg->amount);
446 e->entries = GNUNET_ntohll(msg->entries);
447 e->rid = ++reservation_gen;
448 if (reservation_gen < 0)
449 reservation_gen = 0; /* wrap around */
450 transmit_status (client, e->rid, NULL);
455 * Handle RELEASE_RESERVE-message.
458 * @param client identification of the client
459 * @param message the actual message
462 handle_release_reserve (void *cls,
463 struct GNUNET_SERVER_Client *client,
464 const struct GNUNET_MessageHeader *message)
466 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
467 struct ReservationList *pos;
468 struct ReservationList *prev;
469 struct ReservationList *next;
470 int rid = ntohl(msg->rid);
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Processing `%s' request\n",
479 while (NULL != (pos = next))
488 /* FIXME: released remaining reserved space! */
490 transmit_status (client, GNUNET_OK, NULL);
496 transmit_status (client, GNUNET_SYSERR, "Could not find matching reservation");
501 * Check that the given message is a valid data message.
503 * @return NULL if the message is not well-formed, otherwise the message
505 static const struct DataMessage *
506 check_data (const struct GNUNET_MessageHeader *message)
510 const struct DataMessage *dm;
512 size = ntohs(message->size);
513 if (size < sizeof(struct DataMessage))
518 dm = (const struct DataMessage *) message;
519 dsize = ntohl(dm->size);
520 if (size != dsize + sizeof(struct DataMessage))
525 if (ntohl(dm->type) == 0)
535 * Handle PUT-message.
538 * @param client identification of the client
539 * @param message the actual message
542 handle_put (void *cls,
543 struct GNUNET_SERVER_Client *client,
544 const struct GNUNET_MessageHeader *message)
546 const struct DataMessage *dm = check_data (message);
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Processing `%s' request\n",
559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
562 rid = ntohl(dm->rid);
565 /* FIXME: find reservation, update remaining! */
568 ret = plugin->api->put (plugin->api->cls,
574 ntohl(dm->anonymity),
575 GNUNET_TIME_absolute_ntoh(dm->expiration),
577 if (GNUNET_OK == ret)
578 GNUNET_CONTAINER_bloomfilter_add (filter,
580 transmit_status (client,
581 GNUNET_SYSERR == ret ? GNUNET_SYSERR : GNUNET_OK,
583 GNUNET_free_non_null (msg);
588 * Handle GET-message.
591 * @param client identification of the client
592 * @param message the actual message
595 handle_get (void *cls,
596 struct GNUNET_SERVER_Client *client,
597 const struct GNUNET_MessageHeader *message)
599 static struct GNUNET_TIME_Absolute zero;
600 const struct GetMessage *msg;
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Processing `%s' request\n",
608 size = ntohs(message->size);
609 if ( (size != sizeof(struct GetMessage)) &&
610 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
616 msg = (const struct GetMessage*) message;
617 if ( (size == sizeof(struct GetMessage)) &&
618 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
621 /* don't bother database... */
622 transmit_item (client,
623 NULL, NULL, 0, NULL, 0, 0, 0, zero, 0);
626 GNUNET_SERVER_client_drop (client);
627 plugin->api->get (plugin->api->cls,
628 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
637 * Handle UPDATE-message.
640 * @param client identification of the client
641 * @param message the actual message
644 handle_update (void *cls,
645 struct GNUNET_SERVER_Client *client,
646 const struct GNUNET_MessageHeader *message)
648 const struct UpdateMessage *msg;
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Processing `%s' request\n",
657 msg = (const struct UpdateMessage*) message;
659 ret = plugin->api->update (plugin->api->cls,
660 GNUNET_ntohll(msg->uid),
661 (int32_t) ntohl(msg->priority),
662 GNUNET_TIME_absolute_ntoh(msg->expiration),
664 transmit_status (client, ret, emsg);
665 GNUNET_free_non_null (emsg);
670 * Handle GET_RANDOM-message.
673 * @param client identification of the client
674 * @param message the actual message
677 handle_get_random (void *cls,
678 struct GNUNET_SERVER_Client *client,
679 const struct GNUNET_MessageHeader *message)
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Processing `%s' request\n",
686 GNUNET_SERVER_client_drop (client); // FIXME: WTF?
687 plugin->api->iter_migration_order (plugin->api->cls,
695 * Context for the 'remove_callback'.
700 * Client for whom we're doing the remvoing.
702 struct GNUNET_SERVER_Client *client;
705 * GNUNET_YES if we managed to remove something.
712 * Callback function that will cause the item that is passed
713 * in to be deleted (by returning GNUNET_NO).
716 remove_callback (void *cls,
718 const GNUNET_HashCode * key,
724 struct GNUNET_TIME_Absolute
725 expiration, uint64_t uid)
727 struct RemoveContext *rc = cls;
730 if (GNUNET_YES == rc->found)
731 transmit_status (rc->client, GNUNET_OK, NULL);
733 transmit_status (rc->client, GNUNET_SYSERR, _("Content not found"));
734 GNUNET_SERVER_client_drop (rc->client);
736 return GNUNET_OK; /* last item */
738 rc->found = GNUNET_YES;
739 plugin->api->next_request (next_cls, GNUNET_YES);
740 GNUNET_CONTAINER_bloomfilter_remove (filter,
747 * Handle REMOVE-message.
750 * @param client identification of the client
751 * @param message the actual message
754 handle_remove (void *cls,
755 struct GNUNET_SERVER_Client *client,
756 const struct GNUNET_MessageHeader *message)
758 const struct DataMessage *dm = check_data (message);
759 GNUNET_HashCode vhash;
760 struct RemoveContext *rc;
763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
764 "Processing `%s' request\n",
770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
773 rc = GNUNET_malloc (sizeof(struct RemoveContext));
774 GNUNET_SERVER_client_keep (client);
776 GNUNET_CRYPTO_hash (&dm[1],
779 plugin->api->get (plugin->api->cls,
789 * Handle DROP-message.
792 * @param client identification of the client
793 * @param message the actual message
796 handle_drop (void *cls,
797 struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *message)
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Processing `%s' request\n",
805 plugin->api->drop (plugin->api->cls);
806 GNUNET_SERVER_receive_done (client, GNUNET_OK);
811 * List of handlers for the messages understood by this
814 static struct GNUNET_SERVER_MessageHandler handlers[] = {
815 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
816 sizeof(struct ReserveMessage) },
817 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
818 sizeof(struct ReleaseReserveMessage) },
819 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
820 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
821 sizeof (struct UpdateMessage) },
822 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
823 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
824 sizeof(struct GNUNET_MessageHeader) },
825 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
826 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
827 sizeof(struct GNUNET_MessageHeader) },
834 * Load the datastore plugin.
836 static struct DatastorePlugin *
837 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
838 struct GNUNET_SCHEDULER_Handle *sched)
840 struct DatastorePlugin *ret;
845 GNUNET_CONFIGURATION_get_value_string (cfg,
846 "DATASTORE", "DATABASE", &name))
848 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
849 _("No `%s' specified for `%s' in configuration!\n"),
854 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
856 ret->env.sched = sched;
857 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
858 _("Loading `%s' datastore plugin\n"), name);
859 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
860 ret->short_name = GNUNET_strdup (name);
861 ret->lib_name = libname;
862 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
863 if (ret->api == NULL)
865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
866 _("Failed to load datastore plugin for `%s'\n"), name);
867 GNUNET_free (ret->short_name);
868 GNUNET_free (libname);
877 * Function called when the service shuts
878 * down. Unloads our datastore plugin.
880 * @param plug plugin to unload
883 unload_plugin (struct DatastorePlugin *plug)
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Datastore service is unloading plugin...\n");
889 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
890 GNUNET_free (plug->lib_name);
891 GNUNET_free (plug->short_name);
897 * Last task run during shutdown. Disconnects us from
898 * the transport and core.
901 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
903 unload_plugin (plugin);
909 * Function that removes all active reservations made
910 * by the given client and releases the space for other
914 * @param client identification of the client
917 cleanup_reservations (void *cls,
918 struct GNUNET_SERVER_Client
926 * Process datastore requests.
929 * @param sched scheduler to use
930 * @param server the initialized server
931 * @param cfg configuration to use
935 struct GNUNET_SCHEDULER_Handle *sched,
936 struct GNUNET_SERVER_Handle *server,
937 struct GNUNET_CONFIGURATION_Handle *cfg)
940 unsigned int bf_size;
943 GNUNET_CONFIGURATION_get_value_number (cfg,
944 "DATASTORE", "QUOTA", "a))
946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
947 _("No `%s' specified for `%s' in configuration!\n"),
952 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
955 GNUNET_CONFIGURATION_get_value_filename (cfg,
960 GNUNET_DISK_directory_create_for_file (fn)) )
962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
963 _("Could not use specified filename `%s' for bloomfilter.\n"),
964 fn != NULL ? fn : "");
965 GNUNET_free_non_null (fn);
968 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
969 GNUNET_free_non_null (fn);
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 _("Failed to initialize bloomfilter.\n"));
976 plugin = load_plugin (cfg, sched);
979 GNUNET_CONTAINER_bloomfilter_free (filter);
982 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
983 GNUNET_SERVER_add_handlers (server, handlers);
984 GNUNET_SCHEDULER_add_delayed (sched,
986 GNUNET_SCHEDULER_PRIORITY_IDLE,
987 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
988 GNUNET_TIME_UNIT_FOREVER_REL,
989 &cleaning_task, NULL);
994 * The main function for the datastore service.
996 * @param argc number of arguments from the command line
997 * @param argv command line arguments
998 * @return 0 ok, 1 on error
1001 main (int argc, char *const *argv)
1006 GNUNET_SERVICE_run (argc,
1008 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1013 /* end of gnunet-service-datastore.c */