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"
42 * How many messages do we queue at most per client?
44 #define MAX_PENDING 1024
48 * Our datastore plugin.
50 struct DatastorePlugin
54 * API of the transport as returned by the plugin's
55 * initialization function.
57 struct GNUNET_DATASTORE_PluginFunctions *api;
60 * Short name for the plugin (i.e. "sqlite").
65 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
70 * Environment this transport service is using
73 struct GNUNET_DATASTORE_PluginEnvironment env;
79 * Linked list of active reservations.
81 struct ReservationList
85 * This is a linked list.
87 struct ReservationList *next;
90 * Client that made the reservation.
92 struct GNUNET_SERVER_Client *client;
95 * Number of bytes (still) reserved.
100 * Number of items (still) reserved.
105 * Reservation identifier.
113 * Our datastore plugin (NULL if not available).
115 static struct DatastorePlugin *plugin;
118 * Linked list of space reservations made by clients.
120 static struct ReservationList *reservations;
123 * Bloomfilter to quickly tell if we don't have the content.
125 static struct GNUNET_CONTAINER_BloomFilter *filter;
128 * Static counter to produce reservation identifiers.
130 static int reservation_gen;
133 * How much space are we allowed to use?
135 static unsigned long long quota;
139 * Function called once the transmit operation has
140 * either failed or succeeded.
143 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
145 typedef void (*TransmitContinuation)(void *cls,
148 struct TransmitCallbackContext
151 * The message that we're asked to transmit.
153 struct GNUNET_MessageHeader *msg;
156 * Client that we are transmitting to.
158 struct GNUNET_SERVER_Client *client;
161 * Function to call once msg has been transmitted
162 * (or at least added to the buffer).
164 TransmitContinuation tc;
172 * GNUNET_YES if we are supposed to signal the server
173 * completion of the client's request.
180 * Function called to notify a client about the socket
181 * begin ready to queue more data. "buf" will be
182 * NULL and "size" zero if the socket was closed for
183 * writing in the meantime.
186 * @param size number of bytes available in buf
187 * @param buf where the callee should write the message
188 * @return number of bytes written to buf
191 transmit_callback (void *cls,
192 size_t size, void *buf)
194 struct TransmitCallbackContext *tcc = cls;
197 msize = ntohs(tcc->msg->size);
201 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
202 if (GNUNET_YES == tcc->end)
203 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
204 GNUNET_free (tcc->msg);
208 GNUNET_assert (size >= msize);
209 memcpy (buf, tcc->msg, msize);
211 tcc->tc (tcc->tc_cls, GNUNET_OK);
212 if (GNUNET_YES == tcc->end)
213 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
214 GNUNET_free (tcc->msg);
221 * Transmit the given message to the client.
223 * @param client target of the message
224 * @param msg message to transmit, will be freed!
225 * @param end is this the last response (and we should
226 * signal the server completion accodingly after
227 * transmitting this message)?
230 transmit (struct GNUNET_SERVER_Client *client,
231 struct GNUNET_MessageHeader *msg,
232 TransmitContinuation tc,
236 struct TransmitCallbackContext *tcc;
238 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
240 tcc->client = client;
242 tcc->tc_cls = tc_cls;
246 GNUNET_SERVER_notify_transmit_ready (client,
248 GNUNET_TIME_UNIT_FOREVER_REL,
253 if (GNUNET_YES == end)
254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
256 tc (tc_cls, GNUNET_SYSERR);
264 * Transmit a status code to the client.
266 * @param client receiver of the response
267 * @param code status code
268 * @param msg optional error message (can be NULL)
271 transmit_status (struct GNUNET_SERVER_Client *client,
275 struct StatusMessage *sm;
278 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
279 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
280 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
281 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
282 sm->status = htonl(code);
283 memcpy (&sm[1], msg, slen);
284 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
289 * Function called once the transmit operation has
290 * either failed or succeeded.
293 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
296 get_next(void *next_cls,
299 if (status != GNUNET_OK)
301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
302 _("Failed to transmit an item to the client; aborting iteration.\n"));
303 plugin->api->next_request (next_cls, GNUNET_YES);
306 plugin->api->next_request (next_cls, GNUNET_NO);
311 * Function that will transmit the given datastore entry
314 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
315 * @param next_cls closure to use to ask for the next item
316 * @param key key for the content
317 * @param size number of bytes in data
318 * @param data content stored
319 * @param type type of the content
320 * @param priority priority of the content
321 * @param anonymity anonymity-level for the content
322 * @param expiration expiration time for the content
323 * @param uid unique identifier for the datum;
324 * maybe 0 if no unique identifier is available
326 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
327 * GNUNET_NO to delete the item and continue (if supported)
330 transmit_item (void *cls,
332 const GNUNET_HashCode * key,
338 struct GNUNET_TIME_Absolute
339 expiration, uint64_t uid)
341 struct GNUNET_SERVER_Client *client = cls;
342 struct GNUNET_MessageHeader *end;
343 struct DataMessage *dm;
347 /* transmit 'DATA_END' */
348 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
349 end->size = htons(sizeof(struct GNUNET_MessageHeader));
350 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
351 transmit (client, end, NULL, NULL, GNUNET_YES);
352 GNUNET_SERVER_client_drop (client);
355 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
356 dm->header.size = htons(sizeof(struct DataMessage) + size);
357 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
359 dm->size = htonl(size);
360 dm->type = htonl(type);
361 dm->priority = htonl(priority);
362 dm->anonymity = htonl(anonymity);
363 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
364 dm->uid = GNUNET_htonll(uid);
366 memcpy (&dm[1], data, size);
367 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
373 * Handle RESERVE-message.
376 * @param client identification of the client
377 * @param message the actual message
380 handle_reserve (void *cls,
381 struct GNUNET_SERVER_Client *client,
382 const struct GNUNET_MessageHeader *message)
384 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
385 struct ReservationList *e;
387 /* FIXME: check if we have that much space... */
388 e = GNUNET_malloc (sizeof(struct ReservationList));
389 e->next = reservations;
392 e->size = GNUNET_ntohll(msg->size);
393 e->items = GNUNET_ntohll(msg->items);
394 e->rid = ++reservation_gen;
395 if (reservation_gen < 0)
396 reservation_gen = 0; /* wrap around */
397 transmit_status (client, e->rid, NULL);
402 * Handle RELEASE_RESERVE-message.
405 * @param client identification of the client
406 * @param message the actual message
409 handle_release_reserve (void *cls,
410 struct GNUNET_SERVER_Client *client,
411 const struct GNUNET_MessageHeader *message)
413 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
414 struct ReservationList *pos;
415 struct ReservationList *prev;
416 struct ReservationList *next;
418 int rid = ntohl(msg->rid);
421 while (NULL != (pos = next))
430 /* FIXME: released remaining reserved space! */
432 transmit_status (client, GNUNET_OK, NULL);
438 transmit_status (client, GNUNET_SYSERR, "Could not find matching reservation");
443 * Check that the given message is a valid data message.
445 * @return NULL if the message is not well-formed, otherwise the message
447 static const struct DataMessage *
448 check_data (const struct GNUNET_MessageHeader *message)
452 const struct DataMessage *dm;
454 size = ntohs(message->size);
455 if (size < sizeof(struct DataMessage))
460 dm = (const struct DataMessage *) message;
461 dsize = ntohl(dm->size);
462 if (size != dsize + sizeof(struct DataMessage))
467 if (ntohl(dm->type) == 0)
477 * Handle PUT-message.
480 * @param client identification of the client
481 * @param message the actual message
484 handle_put (void *cls,
485 struct GNUNET_SERVER_Client *client,
486 const struct GNUNET_MessageHeader *message)
488 const struct DataMessage *dm = check_data (message);
496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
499 rid = ntohl(dm->rid);
502 /* FIXME: find reservation, update remaining! */
505 ret = plugin->api->put (plugin->api->cls,
511 ntohl(dm->anonymity),
512 GNUNET_TIME_absolute_ntoh(dm->expiration),
514 if (GNUNET_OK == ret)
515 GNUNET_CONTAINER_bloomfilter_add (filter,
517 transmit_status (client,
518 GNUNET_SYSERR == ret ? GNUNET_SYSERR : GNUNET_OK,
520 GNUNET_free_non_null (msg);
525 * Handle GET-message.
528 * @param client identification of the client
529 * @param message the actual message
532 handle_get (void *cls,
533 struct GNUNET_SERVER_Client *client,
534 const struct GNUNET_MessageHeader *message)
536 static struct GNUNET_TIME_Absolute zero;
537 const struct GetMessage *msg;
540 size = ntohs(message->size);
541 if ( (size != sizeof(struct GetMessage)) &&
542 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548 msg = (const struct GetMessage*) message;
549 if ( (size == sizeof(struct GetMessage)) &&
550 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
553 /* don't bother database... */
554 transmit_item (client,
555 NULL, NULL, 0, NULL, 0, 0, 0, zero, 0);
558 GNUNET_SERVER_client_drop (client);
559 plugin->api->get (plugin->api->cls,
560 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
569 * Handle UPDATE-message.
572 * @param client identification of the client
573 * @param message the actual message
576 handle_update (void *cls,
577 struct GNUNET_SERVER_Client *client,
578 const struct GNUNET_MessageHeader *message)
580 const struct UpdateMessage *msg;
584 msg = (const struct UpdateMessage*) message;
586 ret = plugin->api->update (plugin->api->cls,
587 GNUNET_ntohll(msg->uid),
588 (int32_t) ntohl(msg->priority),
589 GNUNET_TIME_absolute_ntoh(msg->expiration),
591 transmit_status (client, ret, emsg);
592 GNUNET_free_non_null (emsg);
597 * Handle GET_RANDOM-message.
600 * @param client identification of the client
601 * @param message the actual message
604 handle_get_random (void *cls,
605 struct GNUNET_SERVER_Client *client,
606 const struct GNUNET_MessageHeader *message)
608 GNUNET_SERVER_client_drop (client);
609 plugin->api->iter_migration_order (plugin->api->cls,
617 * Context for the 'remove_callback'.
622 * Client for whom we're doing the remvoing.
624 struct GNUNET_SERVER_Client *client;
627 * GNUNET_YES if we managed to remove something.
634 * Callback function that will cause the item that is passed
635 * in to be deleted (by returning GNUNET_NO).
638 remove_callback (void *cls,
640 const GNUNET_HashCode * key,
646 struct GNUNET_TIME_Absolute
647 expiration, uint64_t uid)
649 struct RemoveContext *rc = cls;
652 if (GNUNET_YES == rc->found)
653 transmit_status (rc->client, GNUNET_OK, NULL);
655 transmit_status (rc->client, GNUNET_SYSERR, _("Content not found"));
656 GNUNET_SERVER_client_drop (rc->client);
658 return GNUNET_OK; /* last item */
660 rc->found = GNUNET_YES;
661 plugin->api->next_request (next_cls, GNUNET_YES);
662 GNUNET_CONTAINER_bloomfilter_remove (filter,
669 * Handle REMOVE-message.
672 * @param client identification of the client
673 * @param message the actual message
676 handle_remove (void *cls,
677 struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *message)
680 const struct DataMessage *dm = check_data (message);
681 GNUNET_HashCode vhash;
682 struct RemoveContext *rc;
687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
690 rc = GNUNET_malloc (sizeof(struct RemoveContext));
691 GNUNET_SERVER_client_keep (client);
693 GNUNET_CRYPTO_hash (&dm[1],
696 plugin->api->get (plugin->api->cls,
706 * Handle DROP-message.
709 * @param client identification of the client
710 * @param message the actual message
713 handle_drop (void *cls,
714 struct GNUNET_SERVER_Client *client,
715 const struct GNUNET_MessageHeader *message)
717 plugin->api->drop (plugin->api->cls);
718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
723 * List of handlers for the messages understood by this
726 static struct GNUNET_SERVER_MessageHandler handlers[] = {
727 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
728 sizeof(struct ReserveMessage) },
729 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
730 sizeof(struct ReleaseReserveMessage) },
731 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
732 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
733 sizeof (struct UpdateMessage) },
734 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
735 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
736 sizeof(struct GNUNET_MessageHeader) },
737 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
738 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
739 sizeof(struct GNUNET_MessageHeader) },
746 * Load the datastore plugin.
748 static struct DatastorePlugin *
749 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
750 struct GNUNET_SCHEDULER_Handle *sched)
752 struct DatastorePlugin *ret;
757 GNUNET_CONFIGURATION_get_value_string (cfg,
758 "DATASTORE", "DATABASE", &name))
760 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
761 _("No `%s' specified for `%s' in configuration!\n"),
766 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
768 ret->env.sched = sched;
769 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
770 _("Loading `%s' datastore plugin\n"), name);
771 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
772 ret->short_name = GNUNET_strdup (name);
773 ret->lib_name = libname;
774 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
775 if (ret->api == NULL)
777 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
778 _("Failed to load datastore plugin for `%s'\n"), name);
779 GNUNET_free (ret->short_name);
780 GNUNET_free (libname);
789 * Function called when the service shuts
790 * down. Unloads our datastore plugin.
792 * @param plug plugin to unload
795 unload_plugin (struct DatastorePlugin *plug)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Datastore service is unloading plugin...\n");
801 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
802 GNUNET_free (plug->lib_name);
803 GNUNET_free (plug->short_name);
809 * Last task run during shutdown. Disconnects us from
810 * the transport and core.
813 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
815 unload_plugin (plugin);
821 * Function that removes all active reservations made
822 * by the given client and releases the space for other
826 * @param client identification of the client
829 cleanup_reservations (void *cls,
830 struct GNUNET_SERVER_Client
838 * Process datastore requests.
841 * @param sched scheduler to use
842 * @param server the initialized server
843 * @param cfg configuration to use
847 struct GNUNET_SCHEDULER_Handle *sched,
848 struct GNUNET_SERVER_Handle *server,
849 struct GNUNET_CONFIGURATION_Handle *cfg)
852 unsigned int bf_size;
855 GNUNET_CONFIGURATION_get_value_number (cfg,
856 "DATASTORE", "QUOTA", "a))
858 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
859 _("No `%s' specified for `%s' in configuration!\n"),
864 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
867 GNUNET_CONFIGURATION_get_value_filename (cfg,
872 GNUNET_DISK_directory_create_for_file (fn)) )
874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
875 _("Could not use specified filename `%s' for bloomfilter.\n"),
876 fn != NULL ? fn : "");
877 GNUNET_free_non_null (fn);
880 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
881 GNUNET_free_non_null (fn);
884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
885 _("Failed to initialize bloomfilter.\n"));
888 plugin = load_plugin (cfg, sched);
891 GNUNET_CONTAINER_bloomfilter_free (filter);
894 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
895 GNUNET_SERVER_add_handlers (server, handlers);
896 GNUNET_SCHEDULER_add_delayed (sched,
898 GNUNET_SCHEDULER_PRIORITY_IDLE,
899 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
900 GNUNET_TIME_UNIT_FOREVER_REL,
901 &cleaning_task, NULL);
906 * The main function for the datastore service.
908 * @param argc number of arguments from the command line
909 * @param argv command line arguments
910 * @return 0 ok, 1 on error
913 main (int argc, char *const *argv)
918 GNUNET_SERVICE_run (argc,
920 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
925 /* end of gnunet-service-datastore.c */