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 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
202 "Transmission failed.\n");
205 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
206 if (GNUNET_YES == tcc->end)
208 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
210 GNUNET_free (tcc->msg);
214 GNUNET_assert (size >= msize);
215 memcpy (buf, tcc->msg, msize);
217 tcc->tc (tcc->tc_cls, GNUNET_OK);
218 if (GNUNET_YES == tcc->end)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Request completed, ready for the next request!\n");
224 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Response transmitted, more pending!\n");
233 GNUNET_free (tcc->msg);
240 * Transmit the given message to the client.
242 * @param client target of the message
243 * @param msg message to transmit, will be freed!
244 * @param end is this the last response (and we should
245 * signal the server completion accodingly after
246 * transmitting this message)?
249 transmit (struct GNUNET_SERVER_Client *client,
250 struct GNUNET_MessageHeader *msg,
251 TransmitContinuation tc,
255 struct TransmitCallbackContext *tcc;
257 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
259 tcc->client = client;
261 tcc->tc_cls = tc_cls;
265 GNUNET_SERVER_notify_transmit_ready (client,
267 GNUNET_TIME_UNIT_FOREVER_REL,
272 if (GNUNET_YES == end)
275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
276 "Disconnecting client.\n");
278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
281 tc (tc_cls, GNUNET_SYSERR);
289 * Transmit a status code to the client.
291 * @param client receiver of the response
292 * @param code status code
293 * @param msg optional error message (can be NULL)
296 transmit_status (struct GNUNET_SERVER_Client *client,
300 struct StatusMessage *sm;
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Transmitting `%s' message with value %d and message %s\n",
308 msg != NULL ? msg : "(none)");
310 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
311 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
312 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
313 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
314 sm->status = htonl(code);
315 memcpy (&sm[1], msg, slen);
316 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
321 * Function called once the transmit operation has
322 * either failed or succeeded.
325 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
328 get_next(void *next_cls,
331 if (status != GNUNET_OK)
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
334 _("Failed to transmit an item to the client; aborting iteration.\n"));
335 plugin->api->next_request (next_cls, GNUNET_YES);
338 plugin->api->next_request (next_cls, GNUNET_NO);
343 * Function that will transmit the given datastore entry
346 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
347 * @param next_cls closure to use to ask for the next item
348 * @param key key for the content
349 * @param size number of bytes in data
350 * @param data content stored
351 * @param type type of the content
352 * @param priority priority of the content
353 * @param anonymity anonymity-level for the content
354 * @param expiration expiration time for the content
355 * @param uid unique identifier for the datum;
356 * maybe 0 if no unique identifier is available
358 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
359 * GNUNET_NO to delete the item and continue (if supported)
362 transmit_item (void *cls,
364 const GNUNET_HashCode * key,
370 struct GNUNET_TIME_Absolute
371 expiration, uint64_t uid)
373 struct GNUNET_SERVER_Client *client = cls;
374 struct GNUNET_MessageHeader *end;
375 struct DataMessage *dm;
379 /* transmit 'DATA_END' */
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Transmitting `%s' message\n",
385 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
386 end->size = htons(sizeof(struct GNUNET_MessageHeader));
387 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
388 transmit (client, end, NULL, NULL, GNUNET_YES);
389 GNUNET_SERVER_client_drop (client);
392 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
393 dm->header.size = htons(sizeof(struct DataMessage) + size);
394 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
396 dm->size = htonl(size);
397 dm->type = htonl(type);
398 dm->priority = htonl(priority);
399 dm->anonymity = htonl(anonymity);
400 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
401 dm->uid = GNUNET_htonll(uid);
403 memcpy (&dm[1], data, size);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Transmitting `%s' message\n",
409 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
415 * Handle RESERVE-message.
418 * @param client identification of the client
419 * @param message the actual message
422 handle_reserve (void *cls,
423 struct GNUNET_SERVER_Client *client,
424 const struct GNUNET_MessageHeader *message)
426 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
427 struct ReservationList *e;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Processing `%s' request\n",
434 /* FIXME: check if we have that much space... */
435 e = GNUNET_malloc (sizeof(struct ReservationList));
436 e->next = reservations;
439 e->amount = GNUNET_ntohll(msg->amount);
440 e->entries = GNUNET_ntohll(msg->entries);
441 e->rid = ++reservation_gen;
442 if (reservation_gen < 0)
443 reservation_gen = 0; /* wrap around */
444 transmit_status (client, e->rid, NULL);
449 * Handle RELEASE_RESERVE-message.
452 * @param client identification of the client
453 * @param message the actual message
456 handle_release_reserve (void *cls,
457 struct GNUNET_SERVER_Client *client,
458 const struct GNUNET_MessageHeader *message)
460 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
461 struct ReservationList *pos;
462 struct ReservationList *prev;
463 struct ReservationList *next;
464 int rid = ntohl(msg->rid);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Processing `%s' request\n",
473 while (NULL != (pos = next))
482 /* FIXME: released remaining reserved space! */
484 transmit_status (client, GNUNET_OK, NULL);
490 transmit_status (client, GNUNET_SYSERR, "Could not find matching reservation");
495 * Check that the given message is a valid data message.
497 * @return NULL if the message is not well-formed, otherwise the message
499 static const struct DataMessage *
500 check_data (const struct GNUNET_MessageHeader *message)
504 const struct DataMessage *dm;
506 size = ntohs(message->size);
507 if (size < sizeof(struct DataMessage))
512 dm = (const struct DataMessage *) message;
513 dsize = ntohl(dm->size);
514 if (size != dsize + sizeof(struct DataMessage))
524 * Handle PUT-message.
527 * @param client identification of the client
528 * @param message the actual message
531 handle_put (void *cls,
532 struct GNUNET_SERVER_Client *client,
533 const struct GNUNET_MessageHeader *message)
535 const struct DataMessage *dm = check_data (message);
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Processing `%s' request\n",
545 if (ntohl(dm->type) == 0)
553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
556 rid = ntohl(dm->rid);
559 /* FIXME: find reservation, update remaining! */
562 ret = plugin->api->put (plugin->api->cls,
568 ntohl(dm->anonymity),
569 GNUNET_TIME_absolute_ntoh(dm->expiration),
571 if (GNUNET_OK == ret)
572 GNUNET_CONTAINER_bloomfilter_add (filter,
574 transmit_status (client,
575 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
577 GNUNET_free_non_null (msg);
582 * Handle GET-message.
585 * @param client identification of the client
586 * @param message the actual message
589 handle_get (void *cls,
590 struct GNUNET_SERVER_Client *client,
591 const struct GNUNET_MessageHeader *message)
593 static struct GNUNET_TIME_Absolute zero;
594 const struct GetMessage *msg;
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Processing `%s' request\n",
602 size = ntohs(message->size);
603 if ( (size != sizeof(struct GetMessage)) &&
604 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
610 msg = (const struct GetMessage*) message;
611 if ( (size == sizeof(struct GetMessage)) &&
612 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
615 /* don't bother database... */
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Empty result set for `%s' request.\n",
621 transmit_item (client,
622 NULL, NULL, 0, NULL, 0, 0, 0, zero, 0);
625 GNUNET_SERVER_client_keep (client);
626 plugin->api->get (plugin->api->cls,
627 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
636 * Handle UPDATE-message.
639 * @param client identification of the client
640 * @param message the actual message
643 handle_update (void *cls,
644 struct GNUNET_SERVER_Client *client,
645 const struct GNUNET_MessageHeader *message)
647 const struct UpdateMessage *msg;
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Processing `%s' request\n",
656 msg = (const struct UpdateMessage*) message;
658 ret = plugin->api->update (plugin->api->cls,
659 GNUNET_ntohll(msg->uid),
660 (int32_t) ntohl(msg->priority),
661 GNUNET_TIME_absolute_ntoh(msg->expiration),
663 transmit_status (client, ret, emsg);
664 GNUNET_free_non_null (emsg);
669 * Handle GET_RANDOM-message.
672 * @param client identification of the client
673 * @param message the actual message
676 handle_get_random (void *cls,
677 struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *message)
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Processing `%s' request\n",
685 GNUNET_SERVER_client_keep (client);
686 plugin->api->iter_migration_order (plugin->api->cls,
694 * Context for the 'remove_callback'.
699 * Client for whom we're doing the remvoing.
701 struct GNUNET_SERVER_Client *client;
704 * GNUNET_YES if we managed to remove something.
711 * Callback function that will cause the item that is passed
712 * in to be deleted (by returning GNUNET_NO).
715 remove_callback (void *cls,
717 const GNUNET_HashCode * key,
723 struct GNUNET_TIME_Absolute
724 expiration, uint64_t uid)
726 struct RemoveContext *rc = cls;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "No further matches for `%s' request.\n",
735 if (GNUNET_YES == rc->found)
736 transmit_status (rc->client, GNUNET_OK, NULL);
738 transmit_status (rc->client, GNUNET_SYSERR, _("Content not found"));
739 GNUNET_SERVER_client_drop (rc->client);
741 return GNUNET_OK; /* last item */
743 rc->found = GNUNET_YES;
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Item %llu matches `%s' request.\n",
747 (unsigned long long) uid,
750 GNUNET_CONTAINER_bloomfilter_remove (filter,
752 plugin->api->next_request (next_cls, GNUNET_YES);
758 * Handle REMOVE-message.
761 * @param client identification of the client
762 * @param message the actual message
765 handle_remove (void *cls,
766 struct GNUNET_SERVER_Client *client,
767 const struct GNUNET_MessageHeader *message)
769 const struct DataMessage *dm = check_data (message);
770 GNUNET_HashCode vhash;
771 struct RemoveContext *rc;
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Processing `%s' request\n",
781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
784 rc = GNUNET_malloc (sizeof(struct RemoveContext));
785 GNUNET_SERVER_client_keep (client);
787 GNUNET_CRYPTO_hash (&dm[1],
790 GNUNET_SERVER_client_keep (client);
791 plugin->api->get (plugin->api->cls,
801 * Handle DROP-message.
804 * @param client identification of the client
805 * @param message the actual message
808 handle_drop (void *cls,
809 struct GNUNET_SERVER_Client *client,
810 const struct GNUNET_MessageHeader *message)
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Processing `%s' request\n",
817 plugin->api->drop (plugin->api->cls);
818 GNUNET_SERVER_receive_done (client, GNUNET_OK);
823 * List of handlers for the messages understood by this
826 static struct GNUNET_SERVER_MessageHandler handlers[] = {
827 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
828 sizeof(struct ReserveMessage) },
829 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
830 sizeof(struct ReleaseReserveMessage) },
831 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
832 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
833 sizeof (struct UpdateMessage) },
834 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
835 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
836 sizeof(struct GNUNET_MessageHeader) },
837 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
838 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
839 sizeof(struct GNUNET_MessageHeader) },
846 * Load the datastore plugin.
848 static struct DatastorePlugin *
849 load_plugin (struct GNUNET_CONFIGURATION_Handle *cfg,
850 struct GNUNET_SCHEDULER_Handle *sched)
852 struct DatastorePlugin *ret;
857 GNUNET_CONFIGURATION_get_value_string (cfg,
858 "DATASTORE", "DATABASE", &name))
860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
861 _("No `%s' specified for `%s' in configuration!\n"),
866 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
868 ret->env.sched = sched;
869 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
870 _("Loading `%s' datastore plugin\n"), name);
871 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
872 ret->short_name = name;
873 ret->lib_name = libname;
874 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
875 if (ret->api == NULL)
877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
878 _("Failed to load datastore plugin for `%s'\n"), name);
879 GNUNET_free (ret->short_name);
880 GNUNET_free (libname);
889 * Function called when the service shuts
890 * down. Unloads our datastore plugin.
892 * @param plug plugin to unload
895 unload_plugin (struct DatastorePlugin *plug)
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Datastore service is unloading plugin...\n");
901 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
902 GNUNET_free (plug->lib_name);
903 GNUNET_free (plug->short_name);
909 * Last task run during shutdown. Disconnects us from
910 * the transport and core.
913 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
915 unload_plugin (plugin);
921 * Function that removes all active reservations made
922 * by the given client and releases the space for other
926 * @param client identification of the client
929 cleanup_reservations (void *cls,
930 struct GNUNET_SERVER_Client
938 * Process datastore requests.
941 * @param sched scheduler to use
942 * @param server the initialized server
943 * @param cfg configuration to use
947 struct GNUNET_SCHEDULER_Handle *sched,
948 struct GNUNET_SERVER_Handle *server,
949 struct GNUNET_CONFIGURATION_Handle *cfg)
952 unsigned int bf_size;
955 GNUNET_CONFIGURATION_get_value_number (cfg,
956 "DATASTORE", "QUOTA", "a))
958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
959 _("No `%s' specified for `%s' in configuration!\n"),
964 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
967 GNUNET_CONFIGURATION_get_value_filename (cfg,
972 GNUNET_DISK_directory_create_for_file (fn)) )
974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
975 _("Could not use specified filename `%s' for bloomfilter.\n"),
976 fn != NULL ? fn : "");
977 GNUNET_free_non_null (fn);
980 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
981 GNUNET_free_non_null (fn);
984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
985 _("Failed to initialize bloomfilter.\n"));
988 plugin = load_plugin (cfg, sched);
991 GNUNET_CONTAINER_bloomfilter_free (filter);
994 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
995 GNUNET_SERVER_add_handlers (server, handlers);
996 GNUNET_SCHEDULER_add_delayed (sched,
998 GNUNET_SCHEDULER_PRIORITY_IDLE,
999 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1000 GNUNET_TIME_UNIT_FOREVER_REL,
1001 &cleaning_task, NULL);
1006 * The main function for the datastore service.
1008 * @param argc number of arguments from the command line
1009 * @param argv command line arguments
1010 * @return 0 ok, 1 on error
1013 main (int argc, char *const *argv)
1018 GNUNET_SERVICE_run (argc,
1020 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1025 /* end of gnunet-service-datastore.c */