more consistent error message language and format
[oweals/gnunet.git] / src / datastore / gnunet-service-datastore.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file datastore/gnunet-service-datastore.c
23  * @brief Management for the datastore for files stored on a GNUnet node
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "plugin_datastore.h"
31 #include "datastore.h"
32
33 /**
34  * How many messages do we queue at most per client?
35  */
36 #define MAX_PENDING 1024
37
38 /**
39  * How long are we at most keeping "expired" content
40  * past the expiration date in the database?
41  */
42 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
43
44
45
46 /**
47  * Our datastore plugin.
48  */
49 struct DatastorePlugin
50 {
51
52   /**
53    * API of the transport as returned by the plugin's
54    * initialization function.
55    */
56   struct GNUNET_DATASTORE_PluginFunctions *api;
57
58   /**
59    * Short name for the plugin (i.e. "sqlite").
60    */
61   char *short_name;
62
63   /**
64    * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
65    */
66   char *lib_name;
67
68   /**
69    * Environment this transport service is using
70    * for this plugin.
71    */
72   struct GNUNET_DATASTORE_PluginEnvironment env;
73
74 };
75
76
77 /**
78  * Linked list of active reservations.
79  */
80 struct ReservationList 
81 {
82
83   /**
84    * This is a linked list.
85    */
86   struct ReservationList *next;
87
88   /**
89    * Client that made the reservation.
90    */
91   struct GNUNET_SERVER_Client *client;
92
93   /**
94    * Number of bytes (still) reserved.
95    */
96   uint64_t amount;
97
98   /**
99    * Number of items (still) reserved.
100    */
101   uint64_t entries;
102
103   /**
104    * Reservation identifier.
105    */
106   int32_t rid;
107
108 };
109
110
111 /**
112  * Our datastore plugin (NULL if not available).
113  */
114 static struct DatastorePlugin *plugin;
115
116 /**
117  * Linked list of space reservations made by clients.
118  */
119 static struct ReservationList *reservations;
120
121 /**
122  * Bloomfilter to quickly tell if we don't have the content.
123  */
124 static struct GNUNET_CONTAINER_BloomFilter *filter;
125
126 /**
127  * Static counter to produce reservation identifiers.
128  */
129 static int reservation_gen;
130
131 /**
132  * How much space are we allowed to use?
133  */
134 static unsigned long long quota;
135
136 /**
137  * How much space are we using for the cache?
138  * (space available for insertions that will be
139  *  instantly reclaimed by discarding less 
140  *  important content --- or possibly whatever
141  *  we just inserted into the "cache").
142  */
143 static unsigned long long cache_size;
144
145 /**
146  * How much space have we currently reserved?
147  */
148 static unsigned long long reserved;
149
150 /**
151  * Identity of the task that is used to delete
152  * expired content.
153  */
154 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
155
156 /**
157  * Our configuration.
158  */
159 const struct GNUNET_CONFIGURATION_Handle *cfg;
160
161 /**
162  * Our scheduler.
163  */
164 struct GNUNET_SCHEDULER_Handle *sched; 
165
166 /**
167  * Function called once the transmit operation has
168  * either failed or succeeded.
169  *
170  * @param cls closure
171  * @param status GNUNET_OK on success, GNUNET_SYSERR on error
172  */
173 typedef void (*TransmitContinuation)(void *cls,
174                                      int status);
175
176
177 struct TransmitCallbackContext 
178 {
179   /**
180    * The message that we're asked to transmit.
181    */
182   struct GNUNET_MessageHeader *msg;
183
184   /**
185    * Client that we are transmitting to.
186    */
187   struct GNUNET_SERVER_Client *client;
188
189   /**
190    * Function to call once msg has been transmitted
191    * (or at least added to the buffer).
192    */
193   TransmitContinuation tc;
194
195   /**
196    * Closure for tc.
197    */
198   void *tc_cls;
199
200   /**
201    * GNUNET_YES if we are supposed to signal the server
202    * completion of the client's request.
203    */
204   int end;
205 };
206
207
208 /**
209  * Task that is used to remove expired entries from
210  * the datastore.  This task will schedule itself
211  * again automatically to always delete all expired
212  * content quickly.
213  *
214  * @param cls not used
215  * @param tc task context
216  */ 
217 static void
218 delete_expired (void *cls,
219                 const struct GNUNET_SCHEDULER_TaskContext *tc);
220
221
222 /**
223  * Iterate over the expired items stored in the datastore.
224  * Delete all expired items; once we have processed all
225  * expired items, re-schedule the "delete_expired" task.
226  *
227  * @param cls not used
228  * @param next_cls closure to pass to the "next" function.
229  * @param key key for the content
230  * @param size number of bytes in data
231  * @param data content stored
232  * @param type type of the content
233  * @param priority priority of the content
234  * @param anonymity anonymity-level for the content
235  * @param expiration expiration time for the content
236  * @param uid unique identifier for the datum;
237  *        maybe 0 if no unique identifier is available
238  *
239  * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
240  *         (continue on call to "next", of course),
241  *         GNUNET_NO to delete the item and continue (if supported)
242  */
243 static int 
244 expired_processor (void *cls,
245                    void *next_cls,
246                    const GNUNET_HashCode * key,
247                    uint32_t size,
248                    const void *data,
249                    uint32_t type,
250                    uint32_t priority,
251                    uint32_t anonymity,
252                    struct GNUNET_TIME_Absolute
253                    expiration, 
254                    uint64_t uid)
255 {
256   struct GNUNET_TIME_Absolute now;
257
258   expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
259   if (key == NULL) 
260     {
261       expired_kill_task 
262         = GNUNET_SCHEDULER_add_delayed (sched,
263                                         GNUNET_NO,
264                                         GNUNET_SCHEDULER_PRIORITY_IDLE,
265                                         GNUNET_SCHEDULER_NO_TASK,
266                                         MAX_EXPIRE_DELAY,
267                                         &delete_expired,
268                                         NULL);
269       return GNUNET_SYSERR;
270     }
271   now = GNUNET_TIME_absolute_get ();
272   if (expiration.value > now.value)
273     {
274       /* finished processing */
275       plugin->api->next_request (next_cls, GNUNET_YES);
276       return GNUNET_SYSERR;
277     }
278   plugin->api->next_request (next_cls, GNUNET_NO);
279 #if DEBUG_DATASTORE
280   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281               "Deleting content that expired %llu ms ago\n",
282               (unsigned long long) (now.value - expiration.value));
283 #endif
284   GNUNET_CONTAINER_bloomfilter_remove (filter,
285                                        key);
286   return GNUNET_NO; /* delete */
287 }
288
289
290 /**
291  * Task that is used to remove expired entries from
292  * the datastore.  This task will schedule itself
293  * again automatically to always delete all expired
294  * content quickly.
295  *
296  * @param cls not used
297  * @param tc task context
298  */ 
299 static void
300 delete_expired (void *cls,
301                 const struct GNUNET_SCHEDULER_TaskContext *tc)
302 {
303   plugin->api->iter_ascending_expiration (plugin->api->cls, 
304                                           0,
305                                           &expired_processor,
306                                           NULL);
307 }
308
309
310 /**
311  * An iterator over a set of items stored in the datastore.
312  *
313  * @param cls closure
314  * @param next_cls closure to pass to the "next" function.
315  * @param key key for the content
316  * @param size number of bytes in data
317  * @param data content stored
318  * @param type type of the content
319  * @param priority priority of the content
320  * @param anonymity anonymity-level for the content
321  * @param expiration expiration time for the content
322  * @param uid unique identifier for the datum;
323  *        maybe 0 if no unique identifier is available
324  *
325  * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
326  *         (continue on call to "next", of course),
327  *         GNUNET_NO to delete the item and continue (if supported)
328  */
329 static int 
330 manage (void *cls,
331         void *next_cls,
332         const GNUNET_HashCode * key,
333         uint32_t size,
334         const void *data,
335         uint32_t type,
336         uint32_t priority,
337         uint32_t anonymity,
338         struct GNUNET_TIME_Absolute
339         expiration, 
340         uint64_t uid)
341 {
342   unsigned long long *need = cls;
343
344   if (NULL == key)
345     {
346       GNUNET_free (need);
347       return GNUNET_SYSERR;
348     }
349   if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
350     *need = 0;
351   else
352     *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
353   plugin->api->next_request (next_cls, 
354                              (0 == *need) ? GNUNET_YES : GNUNET_NO);
355 #if DEBUG_DATASTORE
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357               "Deleting %llu bytes of low-priority content (still trying to recover %llu bytes)\n",
358               size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
359               *need);
360 #endif
361   GNUNET_CONTAINER_bloomfilter_remove (filter,
362                                        key);
363   return GNUNET_NO;
364 }
365
366
367 /**
368  * Manage available disk space by running tasks
369  * that will discard content if necessary.  This
370  * function will be run whenever a request for
371  * "need" bytes of storage could only be satisfied
372  * by eating into the "cache" (and we want our cache
373  * space back).
374  *
375  * @param need number of bytes of content that were
376  *        placed into the "cache" (and hence the
377  *        number of bytes that should be removed).
378  */
379 static void
380 manage_space (unsigned long long need)
381 {
382   unsigned long long *n;
383
384 #if DEBUG_DATASTORE
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386               "Asked to recover %llu bytes of cache space\n",
387               need);
388 #endif
389   n = GNUNET_malloc (sizeof(unsigned long long));
390   *n = need;
391   plugin->api->iter_low_priority (plugin->api->cls,
392                                   0,
393                                   &manage,
394                                   n);
395 }
396
397
398 /**
399  * Function called to notify a client about the socket
400  * begin ready to queue more data.  "buf" will be
401  * NULL and "size" zero if the socket was closed for
402  * writing in the meantime.
403  *
404  * @param cls closure
405  * @param size number of bytes available in buf
406  * @param buf where the callee should write the message
407  * @return number of bytes written to buf
408  */
409 static size_t
410 transmit_callback (void *cls,
411                    size_t size, void *buf)
412 {
413   struct TransmitCallbackContext *tcc = cls;
414   size_t msize;
415   
416   msize = ntohs(tcc->msg->size);
417   if (size == 0)
418     {
419 #if DEBUG_DATASTORE
420       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
421                   "Transmission failed.\n");
422 #endif
423       if (tcc->tc != NULL)
424         tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
425       if (GNUNET_YES == tcc->end)
426         {
427           GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
428         }
429       GNUNET_free (tcc->msg);
430       GNUNET_free (tcc);
431       return 0;
432     }
433   GNUNET_assert (size >= msize);
434   memcpy (buf, tcc->msg, msize);
435   if (tcc->tc != NULL)
436     tcc->tc (tcc->tc_cls, GNUNET_OK);
437   if (GNUNET_YES == tcc->end)
438     {
439       GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
440     }
441   else
442     {
443 #if DEBUG_DATASTORE
444       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445                   "Response transmitted, more pending!\n");
446 #endif
447     }
448   GNUNET_free (tcc->msg);
449   GNUNET_free (tcc);
450   return msize;
451 }
452
453
454 /**
455  * Transmit the given message to the client.
456  *
457  * @param client target of the message
458  * @param msg message to transmit, will be freed!
459  * @param tc function to call afterwards
460  * @param tc_cls closure for tc
461  * @param end is this the last response (and we should
462  *        signal the server completion accodingly after
463  *        transmitting this message)?
464  */
465 static void
466 transmit (struct GNUNET_SERVER_Client *client,
467           struct GNUNET_MessageHeader *msg,
468           TransmitContinuation tc,
469           void *tc_cls,
470           int end)
471 {
472   struct TransmitCallbackContext *tcc;
473
474   tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
475   tcc->msg = msg;
476   tcc->client = client;
477   tcc->tc = tc;
478   tcc->tc_cls = tc_cls;
479   tcc->end = end;
480
481   if (NULL ==
482       GNUNET_SERVER_notify_transmit_ready (client,
483                                            ntohs(msg->size),
484                                            GNUNET_TIME_UNIT_FOREVER_REL,
485                                            &transmit_callback,
486                                            tcc))
487     {
488       GNUNET_break (0);
489       if (GNUNET_YES == end)
490         {
491 #if DEBUG_DATASTORE
492           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493                       "Disconnecting client.\n");
494 #endif    
495           GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
496         }
497       if (NULL != tc)
498         tc (tc_cls, GNUNET_SYSERR);
499       GNUNET_free (msg);
500       GNUNET_free (tcc);
501     }
502 }
503
504
505 /**
506  * Transmit a status code to the client.
507  *
508  * @param client receiver of the response
509  * @param code status code
510  * @param msg optional error message (can be NULL)
511  */
512 static void
513 transmit_status (struct GNUNET_SERVER_Client *client,
514                  int code,
515                  const char *msg)
516 {
517   struct StatusMessage *sm;
518   size_t slen;
519
520 #if DEBUG_DATASTORE
521   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522               "Transmitting `%s' message with value %d and message `%s'\n",
523               "STATUS",
524               code,
525               msg != NULL ? msg : "(none)");
526 #endif
527   slen = (msg == NULL) ? 0 : strlen(msg) + 1;  
528   sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
529   sm->header.size = htons(sizeof(struct StatusMessage) + slen);
530   sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
531   sm->status = htonl(code);
532   if (slen > 0)
533     memcpy (&sm[1], msg, slen);  
534   transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
535 }
536
537
538 /**
539  * Function called once the transmit operation has
540  * either failed or succeeded.
541  *
542  * @param next_cls closure for calling "next_request" callback
543  * @param status GNUNET_OK on success, GNUNET_SYSERR on error
544  */
545 static void 
546 get_next(void *next_cls,
547          int status)
548 {
549   if (status != GNUNET_OK)
550     {
551       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
552                   _("Failed to transmit an item to the client; aborting iteration.\n"));    
553       plugin->api->next_request (next_cls, GNUNET_YES);
554       return;
555     }
556   plugin->api->next_request (next_cls, GNUNET_NO);
557 }
558
559
560 /**
561  * Function that will transmit the given datastore entry
562  * to the client.
563  *
564  * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
565  * @param next_cls closure to use to ask for the next item
566  * @param key key for the content
567  * @param size number of bytes in data
568  * @param data content stored
569  * @param type type of the content
570  * @param priority priority of the content
571  * @param anonymity anonymity-level for the content
572  * @param expiration expiration time for the content
573  * @param uid unique identifier for the datum;
574  *        maybe 0 if no unique identifier is available
575  *
576  * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
577  *         GNUNET_NO to delete the item and continue (if supported)
578  */
579 static int
580 transmit_item (void *cls,
581                void *next_cls,
582                const GNUNET_HashCode * key,
583                uint32_t size,
584                const void *data,
585                uint32_t type,
586                uint32_t priority,
587                uint32_t anonymity,
588                struct GNUNET_TIME_Absolute
589                expiration, uint64_t uid)
590 {
591   struct GNUNET_SERVER_Client *client = cls;
592   struct GNUNET_MessageHeader *end;
593   struct DataMessage *dm;
594
595   if (key == NULL)
596     {
597       /* transmit 'DATA_END' */
598 #if DEBUG_DATASTORE
599       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600                   "Transmitting `%s' message\n",
601                   "DATA_END");
602 #endif
603       end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
604       end->size = htons(sizeof(struct GNUNET_MessageHeader));
605       end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
606       transmit (client, end, NULL, NULL, GNUNET_YES);
607       GNUNET_SERVER_client_drop (client);
608       return GNUNET_OK;
609     }
610   dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
611   dm->header.size = htons(sizeof(struct DataMessage) + size);
612   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
613   dm->rid = htonl(0);
614   dm->size = htonl(size);
615   dm->type = htonl(type);
616   dm->priority = htonl(priority);
617   dm->anonymity = htonl(anonymity);
618   dm->expiration = GNUNET_TIME_absolute_hton(expiration);
619   dm->uid = GNUNET_htonll(uid);
620   dm->key = *key;
621   memcpy (&dm[1], data, size);
622 #if DEBUG_DATASTORE
623   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624               "Transmitting `%s' message\n",
625               "DATA");
626 #endif
627   transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
628   return GNUNET_OK;
629 }
630
631
632 /**
633  * Handle RESERVE-message.
634  *
635  * @param cls closure
636  * @param client identification of the client
637  * @param message the actual message
638  */
639 static void
640 handle_reserve (void *cls,
641                 struct GNUNET_SERVER_Client *client,
642                 const struct GNUNET_MessageHeader *message)
643 {
644   const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
645   struct ReservationList *e;
646   unsigned long long used;
647   unsigned long long req;
648   uint64_t amount;
649   uint32_t entries;
650
651 #if DEBUG_DATASTORE
652   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653               "Processing `%s' request\n",
654               "RESERVE");
655 #endif
656   amount = GNUNET_ntohll(msg->amount);
657   entries = ntohl(msg->entries);
658   used = plugin->api->get_size (plugin->api->cls) + reserved;
659   req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
660   if (used + req > quota)
661     {
662       if (quota < used)
663         used = quota; /* cheat a bit for error message (to avoid negative numbers) */
664       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665                   _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
666                   quota - used,
667                   "RESERVE",
668                   req);
669       if (cache_size < req)
670         {
671           /* TODO: document this in the FAQ; essentially, if this
672              message happens, the insertion request could be blocked
673              by less-important content from migration because it is
674              larger than 1/8th of the overall available space, and
675              we only reserve 1/8th for "fresh" insertions */
676           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677                       _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
678                       req,
679                       cache_size);
680           transmit_status (client, 0, 
681                            gettext_noop ("Insufficient space to satisfy request and "
682                                          "requested amount is larger than cache size"));
683         }
684       else
685         {
686           transmit_status (client, 0, 
687                            gettext_noop ("Insufficient space to satisfy request"));
688         }
689       return;      
690     }
691   reserved += req;
692   e = GNUNET_malloc (sizeof(struct ReservationList));
693   e->next = reservations;
694   reservations = e;
695   e->client = client;
696   e->amount = amount;
697   e->entries = entries;
698   e->rid = ++reservation_gen;
699   if (reservation_gen < 0)
700     reservation_gen = 0; /* wrap around */
701   transmit_status (client, e->rid, NULL);
702 }
703
704
705 /**
706  * Handle RELEASE_RESERVE-message.
707  *
708  * @param cls closure
709  * @param client identification of the client
710  * @param message the actual message
711  */
712 static void
713 handle_release_reserve (void *cls,
714                         struct GNUNET_SERVER_Client *client,
715                         const struct GNUNET_MessageHeader *message)
716 {
717   const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
718   struct ReservationList *pos;
719   struct ReservationList *prev;
720   struct ReservationList *next;
721   int rid = ntohl(msg->rid);
722   unsigned long long rem;
723
724 #if DEBUG_DATASTORE
725   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726               "Processing `%s' request\n",
727               "RELEASE_RESERVE");
728 #endif
729   next = reservations;
730   prev = NULL;
731   while (NULL != (pos = next))
732     {
733       next = pos->next;
734       if (rid == pos->rid)
735         {
736           if (prev == NULL)
737             reservations = next;
738           else
739             prev->next = next;
740           rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
741           GNUNET_assert (reserved >= rem);
742           reserved -= rem;
743 #if DEBUG_DATASTORE
744           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745                       "Returning %llu remaining reserved bytes to storage pool\n",
746                       rem);
747 #endif    
748           GNUNET_free (pos);
749           transmit_status (client, GNUNET_OK, NULL);
750           return;
751         }       
752       prev = pos;
753     }
754   GNUNET_break (0);
755   transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
756 }
757
758
759 /**
760  * Check that the given message is a valid data message.
761  *
762  * @return NULL if the message is not well-formed, otherwise the message
763  */
764 static const struct DataMessage *
765 check_data (const struct GNUNET_MessageHeader *message)
766 {
767   uint16_t size;
768   uint32_t dsize;
769   const struct DataMessage *dm;
770
771   size = ntohs(message->size);
772   if (size < sizeof(struct DataMessage))
773     { 
774       GNUNET_break (0);
775       return NULL;
776     }
777   dm = (const struct DataMessage *) message;
778   dsize = ntohl(dm->size);
779   if (size != dsize + sizeof(struct DataMessage))
780     {
781       GNUNET_break (0);
782       return NULL;
783     }
784   return dm;
785 }
786
787
788 /**
789  * Handle PUT-message.
790  *
791  * @param cls closure
792  * @param client identification of the client
793  * @param message the actual message
794  */
795 static void
796 handle_put (void *cls,
797             struct GNUNET_SERVER_Client *client,
798             const struct GNUNET_MessageHeader *message)
799 {
800   const struct DataMessage *dm = check_data (message);
801   char *msg;
802   int ret;
803   int rid;
804   struct ReservationList *pos;
805   uint32_t size;
806
807 #if DEBUG_DATASTORE
808   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809               "Processing `%s' request\n",
810               "PUT");
811 #endif
812   if (ntohl(dm->type) == 0) 
813     {
814       GNUNET_break (0);
815       dm = NULL;
816     }
817   if (dm == NULL)
818     {
819       GNUNET_break (0);
820       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
821       return;
822     }
823   rid = ntohl(dm->rid);
824   size = ntohl(dm->size);
825   if (rid > 0)
826     {
827       pos = reservations;
828       while ( (NULL != pos) &&
829               (rid != pos->rid) )
830         pos = pos->next;
831       GNUNET_break (pos != NULL);
832       if (NULL != pos)
833         {
834           GNUNET_break (pos->entries > 0);
835           GNUNET_break (pos->amount > size);
836           pos->entries--;
837           pos->amount -= size;
838           reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
839         }
840     }
841   msg = NULL;
842   ret = plugin->api->put (plugin->api->cls,
843                           &dm->key,
844                           size,
845                           &dm[1],
846                           ntohl(dm->type),
847                           ntohl(dm->priority),
848                           ntohl(dm->anonymity),
849                           GNUNET_TIME_absolute_ntoh(dm->expiration),
850                           &msg);
851   if (GNUNET_OK == ret)
852     GNUNET_CONTAINER_bloomfilter_add (filter,
853                                       &dm->key);
854   transmit_status (client, 
855                    (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK, 
856                    msg);
857   GNUNET_free_non_null (msg);
858   if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
859     manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
860 }
861
862
863 /**
864  * Handle GET-message.
865  *
866  * @param cls closure
867  * @param client identification of the client
868  * @param message the actual message
869  */
870 static void
871 handle_get (void *cls,
872              struct GNUNET_SERVER_Client *client,
873              const struct GNUNET_MessageHeader *message)
874 {
875   const struct GetMessage *msg;
876   uint16_t size;
877
878 #if DEBUG_DATASTORE
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880               "Processing `%s' request\n",
881               "GET");
882 #endif
883   size = ntohs(message->size);
884   if ( (size != sizeof(struct GetMessage)) &&
885        (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
886     {
887       GNUNET_break (0);
888       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
889       return;
890     }
891   msg = (const struct GetMessage*) message;
892   if ( (size == sizeof(struct GetMessage)) &&
893        (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
894                                                          &msg->key)) )
895     {
896       /* don't bother database... */
897 #if DEBUG_DATASTORE
898       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899                   "Empty result set for `%s' request.\n",
900                   "GET");
901 #endif  
902       GNUNET_SERVER_client_keep (client);
903       transmit_item (client,
904                      NULL, NULL, 0, NULL, 0, 0, 0, 
905                      GNUNET_TIME_UNIT_ZERO_ABS, 0);
906       return;
907     }
908   GNUNET_SERVER_client_keep (client);
909   plugin->api->get (plugin->api->cls,
910                     ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
911                     NULL,
912                     ntohl(msg->type),
913                     &transmit_item,
914                     client);    
915 }
916
917
918 /**
919  * Handle UPDATE-message.
920  *
921  * @param cls closure
922  * @param client identification of the client
923  * @param message the actual message
924  */
925 static void
926 handle_update (void *cls,
927                struct GNUNET_SERVER_Client *client,
928                const struct GNUNET_MessageHeader *message)
929 {
930   const struct UpdateMessage *msg;
931   int ret;
932   char *emsg;
933
934 #if DEBUG_DATASTORE
935   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936               "Processing `%s' request\n",
937               "UPDATE");
938 #endif
939   msg = (const struct UpdateMessage*) message;
940   emsg = NULL;
941   ret = plugin->api->update (plugin->api->cls,
942                              GNUNET_ntohll(msg->uid),
943                              (int32_t) ntohl(msg->priority),
944                              GNUNET_TIME_absolute_ntoh(msg->expiration),
945                              &emsg);
946   transmit_status (client, ret, emsg);
947   GNUNET_free_non_null (emsg);
948 }
949
950
951 /**
952  * Handle GET_RANDOM-message.
953  *
954  * @param cls closure
955  * @param client identification of the client
956  * @param message the actual message
957  */
958 static void
959 handle_get_random (void *cls,
960                    struct GNUNET_SERVER_Client *client,
961                    const struct GNUNET_MessageHeader *message)
962 {
963 #if DEBUG_DATASTORE
964   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965               "Processing `%s' request\n",
966               "GET_RANDOM");
967 #endif
968   GNUNET_SERVER_client_keep (client);
969   plugin->api->iter_migration_order (plugin->api->cls,
970                                      0,
971                                      &transmit_item,
972                                      client);  
973 }
974
975
976 /**
977  * Context for the 'remove_callback'.
978  */
979 struct RemoveContext 
980 {
981   /**
982    * Client for whom we're doing the remvoing.
983    */
984   struct GNUNET_SERVER_Client *client;
985
986   /**
987    * GNUNET_YES if we managed to remove something.
988    */
989   int found;
990 };
991
992
993 /**
994  * Callback function that will cause the item that is passed
995  * in to be deleted (by returning GNUNET_NO).
996  */
997 static int
998 remove_callback (void *cls,
999                  void *next_cls,
1000                  const GNUNET_HashCode * key,
1001                  uint32_t size,
1002                  const void *data,
1003                  uint32_t type,
1004                  uint32_t priority,
1005                  uint32_t anonymity,
1006                  struct GNUNET_TIME_Absolute
1007                  expiration, uint64_t uid)
1008 {
1009   struct RemoveContext *rc = cls;
1010
1011   if (key == NULL)
1012     {
1013 #if DEBUG_DATASTORE
1014       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015                   "No further matches for `%s' request.\n",
1016                   "REMOVE");
1017 #endif  
1018       if (GNUNET_YES == rc->found)
1019         transmit_status (rc->client, GNUNET_OK, NULL);       
1020       else
1021         transmit_status (rc->client, GNUNET_NO, _("Content not found"));        
1022       GNUNET_SERVER_client_drop (rc->client);
1023       GNUNET_free (rc);
1024       return GNUNET_OK; /* last item */
1025     }
1026   rc->found = GNUNET_YES;
1027 #if DEBUG_DATASTORE
1028   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029               "Item %llu matches `%s' request.\n",
1030               (unsigned long long) uid,
1031               "REMOVE");
1032 #endif  
1033   GNUNET_CONTAINER_bloomfilter_remove (filter,
1034                                        key);
1035   plugin->api->next_request (next_cls, GNUNET_YES);
1036   return GNUNET_NO;
1037 }
1038
1039
1040 /**
1041  * Handle REMOVE-message.
1042  *
1043  * @param cls closure
1044  * @param client identification of the client
1045  * @param message the actual message
1046  */
1047 static void
1048 handle_remove (void *cls,
1049              struct GNUNET_SERVER_Client *client,
1050              const struct GNUNET_MessageHeader *message)
1051 {
1052   const struct DataMessage *dm = check_data (message);
1053   GNUNET_HashCode vhash;
1054   struct RemoveContext *rc;
1055
1056 #if DEBUG_DATASTORE
1057   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058               "Processing `%s' request\n",
1059               "REMOVE");
1060 #endif
1061   if (dm == NULL)
1062     {
1063       GNUNET_break (0);
1064       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1065       return;
1066     }
1067   rc = GNUNET_malloc (sizeof(struct RemoveContext));
1068   GNUNET_SERVER_client_keep (client);
1069   rc->client = client;
1070   GNUNET_CRYPTO_hash (&dm[1],
1071                       ntohl(dm->size),
1072                       &vhash);
1073   plugin->api->get (plugin->api->cls,
1074                     &dm->key,
1075                     &vhash,
1076                     ntohl(dm->type),
1077                     &remove_callback,
1078                     rc);
1079 }
1080
1081
1082 /**
1083  * Handle DROP-message.
1084  *
1085  * @param cls closure
1086  * @param client identification of the client
1087  * @param message the actual message
1088  */
1089 static void
1090 handle_drop (void *cls,
1091              struct GNUNET_SERVER_Client *client,
1092              const struct GNUNET_MessageHeader *message)
1093 {
1094 #if DEBUG_DATASTORE
1095   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096               "Processing `%s' request\n",
1097               "DROP");
1098 #endif
1099   plugin->api->drop (plugin->api->cls);
1100   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1101 }
1102
1103
1104 /**
1105  * List of handlers for the messages understood by this
1106  * service.
1107  */
1108 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1109   {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE, 
1110    sizeof(struct ReserveMessage) }, 
1111   {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE, 
1112    sizeof(struct ReleaseReserveMessage) }, 
1113   {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 }, 
1114   {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE, 
1115    sizeof (struct UpdateMessage) }, 
1116   {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 }, 
1117   {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM, 
1118    sizeof(struct GNUNET_MessageHeader) }, 
1119   {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 }, 
1120   {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP, 
1121    sizeof(struct GNUNET_MessageHeader) }, 
1122   {NULL, NULL, 0, 0}
1123 };
1124
1125
1126
1127 /**
1128  * Load the datastore plugin.
1129  */
1130 static struct DatastorePlugin *
1131 load_plugin () 
1132 {
1133   struct DatastorePlugin *ret;
1134   char *libname;
1135   char *name;
1136
1137   if (GNUNET_OK !=
1138       GNUNET_CONFIGURATION_get_value_string (cfg,
1139                                              "DATASTORE", "DATABASE", &name))
1140     {
1141       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1142                   _("No `%s' specified for `%s' in configuration!\n"),
1143                   "DATABASE",
1144                   "DATASTORE");
1145       return NULL;
1146     }
1147   ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1148   ret->env.cfg = cfg;
1149   ret->env.sched = sched;  
1150   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1151               _("Loading `%s' datastore plugin\n"), name);
1152   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1153   ret->short_name = name;
1154   ret->lib_name = libname;
1155   ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1156   if (ret->api == NULL)
1157     {
1158       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1159                   _("Failed to load datastore plugin for `%s'\n"), name);
1160       GNUNET_free (ret->short_name);
1161       GNUNET_free (libname);
1162       GNUNET_free (ret);
1163       return NULL;
1164     }
1165   return ret;
1166 }
1167
1168
1169 /**
1170  * Function called when the service shuts
1171  * down.  Unloads our datastore plugin.
1172  *
1173  * @param plug plugin to unload
1174  */
1175 static void
1176 unload_plugin (struct DatastorePlugin *plug)
1177 {
1178 #if DEBUG_DATASTORE
1179   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180               "Datastore service is unloading plugin...\n");
1181 #endif
1182   GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1183   GNUNET_free (plug->lib_name);
1184   GNUNET_free (plug->short_name);
1185   GNUNET_free (plug);
1186 }
1187
1188
1189 /**
1190  * Last task run during shutdown.  Disconnects us from
1191  * the transport and core.
1192  */
1193 static void
1194 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1195 {
1196   unload_plugin (plugin);
1197   plugin = NULL;
1198 }
1199
1200
1201 /**
1202  * Function that removes all active reservations made
1203  * by the given client and releases the space for other
1204  * requests.
1205  *
1206  * @param cls closure
1207  * @param client identification of the client
1208  */
1209 static void
1210 cleanup_reservations (void *cls,
1211                       struct GNUNET_SERVER_Client
1212                       * client)
1213 {
1214   struct ReservationList *pos;
1215   struct ReservationList *prev;
1216   struct ReservationList *next;
1217
1218   prev = NULL;
1219   pos = reservations;
1220   while (NULL != pos)
1221     {
1222       next = pos->next;
1223       if (pos->client == client)
1224         {
1225           if (prev == NULL)
1226             reservations = next;
1227           else
1228             prev->next = next;
1229           reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1230           GNUNET_free (pos);
1231         }
1232       else
1233         {
1234           prev = pos;
1235         }
1236       pos = next;
1237     }
1238 }
1239
1240
1241 /**
1242  * Process datastore requests.
1243  *
1244  * @param cls closure
1245  * @param s scheduler to use
1246  * @param server the initialized server
1247  * @param c configuration to use
1248  */
1249 static void
1250 run (void *cls,
1251      struct GNUNET_SCHEDULER_Handle *s,
1252      struct GNUNET_SERVER_Handle *server,
1253      const struct GNUNET_CONFIGURATION_Handle *c)
1254 {
1255   char *fn;
1256   unsigned int bf_size;
1257
1258   sched = s;
1259   cfg = c;
1260   if (GNUNET_OK !=
1261       GNUNET_CONFIGURATION_get_value_number (cfg,
1262                                              "DATASTORE", "QUOTA", &quota))
1263     {
1264       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1265                   _("No `%s' specified for `%s' in configuration!\n"),
1266                   "QUOTA",
1267                   "DATASTORE");
1268       return;
1269     }
1270   cache_size = quota / 8; /* Or should we make this an option? */
1271   bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1272   fn = NULL;
1273   if ( (GNUNET_OK !=
1274         GNUNET_CONFIGURATION_get_value_filename (cfg,
1275                                                  "DATASTORE",
1276                                                  "BLOOMFILTER",
1277                                                  &fn)) ||
1278        (GNUNET_OK !=
1279         GNUNET_DISK_directory_create_for_file (fn)) )
1280     {
1281       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1282                   _("Could not use specified filename `%s' for bloomfilter.\n"),
1283                   fn != NULL ? fn : "");
1284       GNUNET_free_non_null (fn);
1285       fn = NULL;
1286     }
1287   filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5);  /* approx. 3% false positives at max use */  
1288   GNUNET_free_non_null (fn);
1289   if (filter == NULL)
1290     {
1291       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1292                   _("Failed to initialize bloomfilter.\n"));
1293       return;
1294     }
1295   plugin = load_plugin ();
1296   if (NULL == plugin)
1297     {
1298       GNUNET_CONTAINER_bloomfilter_free (filter);
1299       return;
1300     }
1301   GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1302   GNUNET_SERVER_add_handlers (server, handlers);
1303   expired_kill_task
1304     = GNUNET_SCHEDULER_add_delayed (sched,
1305                                     GNUNET_NO,
1306                                     GNUNET_SCHEDULER_PRIORITY_IDLE,
1307                                     GNUNET_SCHEDULER_NO_TASK,
1308                                     GNUNET_TIME_UNIT_ZERO,
1309                                     &delete_expired, NULL);
1310   GNUNET_SCHEDULER_add_delayed (sched,
1311                                 GNUNET_YES,
1312                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
1313                                 GNUNET_SCHEDULER_NO_TASK,
1314                                 GNUNET_TIME_UNIT_FOREVER_REL,
1315                                 &cleaning_task, NULL);
1316   
1317 }
1318
1319
1320 /**
1321  * The main function for the datastore service.
1322  *
1323  * @param argc number of arguments from the command line
1324  * @param argv command line arguments
1325  * @return 0 ok, 1 on error
1326  */
1327 int
1328 main (int argc, char *const *argv)
1329 {
1330   int ret;
1331
1332   ret = (GNUNET_OK ==
1333          GNUNET_SERVICE_run (argc,
1334                              argv,
1335                              "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1336   return ret;
1337 }
1338
1339
1340 /* end of gnunet-service-datastore.c */