Debian Codegraph @

device-src/xfer-dest-taper-cacher.c

raw
1 /*
2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2009-2012 Zmanda, Inc. All Rights Reserved.
4 * Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Contact information: Carbonite Inc., 756 N Pastoria Ave
21 * Sunnyvale, CA 94085, or: http://www.zmanda.com
22 */
23
24 #include "amanda.h"
25 #include "amxfer.h"
26 #include "xfer-device.h"
27 #include "conffile.h"
28
29 /* A transfer destination that writes an entire dumpfile to one or more files
30 * on one or more devices, caching each part so that it can be rewritten on a
31 * subsequent volume in the event of an unexpected EOM. This is designed to
32 * work in concert with Amanda::Taper::Scribe. */
33
34 /* Future Plans:
35 * - capture EOF early enough to avoid wasting a tape when the part size is an even multiple of the volume size - maybe reader thread can just go back and tag previous slab with EOF in that case?
36 * - use mmap to make the disk-cacher thread unnecessary, if supported, by simply mapping slabs into the disk cache file
37 * - can we find a way to fall back to mem_cache when the disk cache gets ENOSPC? Does it even make sense to try, since this would change the part size?
38 * - distinguish some permanent device errors and do not retry the part? (this will be a change of behavior)
39 */
40
41 /*
42 * Slabs
43 *
44 * Slabs are larger than blocks, and are the unit on which the element
45 * operates. They are designed to be a few times larger than a block, to
46 * achieve a corresponding reduction in the number of locks and unlocks used
47 * per block, and similar reduction in the the amount of memory overhead
48 * required.
49 */
50
51 typedef struct Slab {
52 struct Slab *next;
53
54 /* counts incoming pointers: the preceding slab's 'next' pointer, and pointers
55 * from any processes operating on the slab */
56 gint refcount;
57
58 /* number of this slab in the sequence, global to this element's lifetime.
59 * Since this counts slabs, which are about 1M, this can address 16
60 * yottabytes of data before wrapping. */
61 guint64 serial;
62
63 /* slab size; this is only less than the element's slab size if the
64 * transfer is at EOF. */
65 gsize size;
66
67 /* base of the slab_size buffer */
68 gchar *base;
69 } Slab;
70
71 /*
72 * Xfer Dest Taper
73 */
74
75 static GObjectClass *parent_class = NULL;
76
77 typedef struct XferDestTaperCacher {
78 XferDestTaper __parent__;
79
80 /* object parameters
81 *
82 * These values are supplied to the constructor, and can be assumed
83 * constant for the lifetime of the element.
84 */
85
86 /* maximum buffer space to use for streaming; this is unrelated to the
87 * fallback_splitsize */
88 gsize max_memory;
89
90 /* split buffering info; if we're doing memory buffering, use_mem_cache is
91 * true; if we're doing disk buffering, disk_cache_dirname is non-NULL and
92 * contains the (allocated) filename of the cache file. In any
93 * case, part_size gives the desired part size. If part_size is zero, then
94 * no splitting takes place (so part_size is effectively infinite). */
95 gboolean use_mem_cache;
96 char *disk_cache_dirname;
97 guint64 part_size; /* (bytes) */
98
99 /*
100 * threads
101 */
102
103 /* The thread doing the actual writes to tape; this also handles buffering
104 * for streaming */
105 GThread *device_thread;
106
107 /* The thread writing slabs to the disk cache, if any */
108 GThread *disk_cache_thread;
109
110 /* slab train
111 *
112 * All in-memory data is contained in a linked list called the "slab
113 * train". Various components are operating simultaneously at different
114 * points in this train. Data from the upstream XferElement is appended to
115 * the head of the train, and the device thread follows along behind,
116 * writing data to the device. When caching parts in memory, the slab
117 * train just grows to eventually contain the whole part. When using an
118 * on-disk cache, the disk cache thread writes the tail of the train to
119 * disk, freeing slabs to be re-used at the head of the train. Some
120 * careful coordination of these components allows them to operate as
121 * independently as possible within the limits of the user's configuration.
122 *
123 * Slabs are rarely, if ever, freed: the oldest_slab reference generally
124 * ensures that all slabs have refcount > 0, and this pointer is only
125 * advanced when re-using slabs that have been flushed to the disk cache or
126 * when freeing slabs after completion of the transfer. */
127
128 /* pointers into the slab train are all protected by this mutex. Note that
129 * the slabs themselves can be manipulated without this lock; it's only
130 * when changing the pointers that the mutex must be held. Furthermore, a
131 * foo_slab variable which is not NULL will not be changed except by its
132 * controlling thread (disk_cacher_slab is controlled by disk_cache_thread,
133 * and device_slab is controlled by device_thread). This means that a
134 * controlling thread can drop the slab_mutex once it has ensured its slab
135 * is non-NULL.
136 *
137 * Slab_cond is notified when a new slab is made available from the reader.
138 * Slab_free_cond is notified when a slab becomes available for
139 * reallocation.
140 *
141 * Any thread waiting on either condition variable should also check
142 * elt->cancelled, and act appropriately if awakened in a cancelled state.
143 */
144 GMutex *slab_mutex; GCond *slab_cond; GCond *slab_free_cond;
145
146 /* slabs in progress by each thread, or NULL if the thread is waiting on
147 * slab_cond. These can only be changed by their respective threads, except
148 * when they are NULL (in which case the reader will point them to a new
149 * slab and signal the slab_cond). */
150 Slab *volatile disk_cacher_slab;
151 Slab *volatile mem_cache_slab;
152 Slab *volatile device_slab;
153
154 /* tail and head of the slab train */
155 Slab *volatile oldest_slab;
156 Slab *volatile newest_slab;
157
158 /* thread-specific information
159 *
160 * These values are only used by one thread, and thus are not
161 * subject to any locking or concurrency constraints.
162 */
163
164 /* slab in progress by the reader (not in the slab train) */
165 Slab *reader_slab;
166
167 /* the serial to be assigned to reader_slab */
168 guint64 next_serial;
169
170 /* bytes written to the device in this part */
171 guint64 bytes_written;
172
173 /* bytes written to the device in the current slab */
174 guint64 slab_bytes_written;
175
176 /* element state
177 *
178 * "state" includes all of the variables below (including device
179 * parameters). Note that the device_thread reads state values when
180 * paused is false without locking the mutex. No other thread should
181 * change state when the element is not paused.
182 *
183 * If there is every any reason to lock both mutexes, acquire this one
184 * first.
185 *
186 * Any thread waiting on this condition variable should also check
187 * elt->cancelled, and act appropriately if awakened in a cancelled state.
188 */
189 GMutex *state_mutex;
190 GCond *state_cond;
191 volatile gboolean paused;
192
193 /* The device to write to, and the header to write to it */
194 Device *volatile device;
195 dumpfile_t *volatile part_header;
196
197 /* If true, when unpaused, the device should begin at the beginning of the
198 * cache; if false, it should proceed to the next part. */
199 volatile gboolean retry_part;
200
201 /* If true, the previous part was completed successfully; only used for
202 * assertions */
203 volatile gboolean last_part_successful;
204
205 /* part number in progress */
206 volatile guint64 partnum;
207
208 /* if true, the main thread should *not* call start_part */
209 volatile gboolean no_more_parts;
210
211 /* the first serial in this part, and the serial to stop at */
212 volatile guint64 part_first_serial, part_stop_serial;
213
214 /* read and write file descriptors for the disk cache file, in use by the
215 * disk_cache_thread. If these are -1, wait on state_cond until they are
216 * not; once the value is set, it will not change. */
217 volatile int disk_cache_read_fd;
218 volatile int disk_cache_write_fd;
219
220 /* device parameters
221 *
222 * Note that these values aren't known until we begin writing to the
223 * device; if block_size is zero, threads should block on state_cond until
224 * it is nonzero, at which point all of the dependent fields will have
225 * their correct values. Note that, since this value never changes after
226 * it has been set, it is safe to read block_size without acquiring the
227 * mutext first. */
228
229 /* this device's need for streaming */
230 StreamingRequirement streaming;
231
232 /* block size expected by the target device */
233 gsize block_size;
234
235 /* Size of a slab - some multiple of the block size */
236 gsize slab_size;
237
238 /* maximum number of slabs allowed, rounded up to the next whole slab. If
239 * using mem cache, this is the equivalent of part_size bytes; otherwise,
240 * it is equivalent to max_memory bytes. */
241 guint64 max_slabs;
242
243 /* number of slabs in a part */
244 guint64 slabs_per_part;
245
246 crc_t crc_before_part;
247 } XferDestTaperCacher;
248
249 static GType xfer_dest_taper_cacher_get_type(void);
250 #define XFER_DEST_TAPER_CACHER_TYPE (xfer_dest_taper_cacher_get_type())
251 #define XFER_DEST_TAPER_CACHER(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_taper_cacher_get_type(), XferDestTaperCacher)
252 #define XFER_DEST_TAPER_CACHER_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_dest_taper_cacher_get_type(), XferDestTaperCacher const)
253 #define XFER_DEST_TAPER_CACHER_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_dest_taper_cacher_get_type(), XferDestTaperCacherClass)
254 #define IS_XFER_DEST_TAPER_CACHER(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_dest_taper_cacher_get_type ())
255 #define XFER_DEST_TAPER_CACHER_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_dest_taper_cacher_get_type(), XferDestTaperCacherClass)
256
257 typedef struct {
258 XferDestTaperClass __parent__;
259
260 } XferDestTaperCacherClass;
261
262 /*
263 * Debug logging
264 */
265
266 #define DBG(LEVEL, ...) if (debug_taper >= LEVEL) { _xdt_dbg(__VA_ARGS__); }
267 static void
268 _xdt_dbg(const char *fmt, ...)
269 {
270 va_list argp;
271 char msg[1024];
272
273 arglist_start(argp, fmt);
274 g_vsnprintf(msg, sizeof(msg), fmt, argp);
275 arglist_end(argp);
276 g_debug("XDTC: %s", msg);
277 }
278
279 /*
280 * Slab handling
281 */
282
283 /* called with the slab_mutex held, this gets a new slab to write into, with
284 * refcount 1. It will block if max_memory slabs are already in use, and mem
285 * caching is not in use, although allocation may be forced with the 'force'
286 * parameter.
287 *
288 * If the memory allocation cannot be satisfied due to system constraints,
289 * this function will send an XMSG_ERROR, wait for the transfer to cancel, and
290 * return NULL. If the transfer is cancelled by some other means while this
291 * function is blocked awaiting a free slab, it will return NULL.
292 *
293 * @param self: the xfer element
294 * @param force: allocate a slab even if it would exceed max_memory
295 * @returns: a new slab, or NULL if the xfer is cancelled
296 */
297 static Slab *
298 alloc_slab(
299 XferDestTaperCacher *self,
300 gboolean force)
301 {
302 XferElement *elt = XFER_ELEMENT(self);
303 Slab *rv;
304
305 DBG(8, "alloc_slab(force=%d)", force);
306 if (!force) {
307 /* throttle based on maximum number of extant slabs */
308 while (G_UNLIKELY(
309 !elt->cancelled &&
310 self->oldest_slab &&
311 self->newest_slab &&
312 self->oldest_slab->refcount > 1 &&
313 (self->newest_slab->serial - self->oldest_slab->serial + 1) >= self->max_slabs)) {
314 DBG(9, "waiting for available slab");
315 g_cond_wait(self->slab_free_cond, self->slab_mutex);
316 }
317 DBG(9, "alloc_slab done waiting");
318
319 if (elt->cancelled)
320 return NULL;
321 }
322
323 /* if the oldest slab doesn't have anything else pointing to it, just use
324 * that */
325 if (self->oldest_slab && self->oldest_slab->refcount == 1) {
326 rv = self->oldest_slab;
327 self->oldest_slab = rv->next;
328 } else {
329 rv = g_new0(Slab, 1);
330 rv->refcount = 1;
331 rv->base = g_try_malloc(self->slab_size);
332 if (!rv->base) {
333 xfer_cancel_with_error(XFER_ELEMENT(self),
334 _("Could not allocate %zu bytes of memory: %s"), self->slab_size, strerror(errno));
335 g_free(rv);
336 return NULL;
337 }
338 }
339
340 rv->next = NULL;
341 rv->size = 0;
342 return rv;
343 }
344
345 /* called with the slab_mutex held, this frees the given slave entirely. The
346 * reference count is not consulted.
347 *
348 * @param slab: slab to free
349 */
350 static void
351 free_slab(
352 Slab *slab)
353 {
354 if (slab) {
355 if (slab->base)
356 g_free(slab->base);
357 g_free(slab);
358 }
359 }
360
361 /* called with the slab_mutex held, this decrements the refcount of the
362 * given slab
363 *
364 * @param self: xfer element
365 * @param slab: slab to free
366 */
367 static inline void
368 unref_slab(
369 XferDestTaperCacher *self,
370 Slab *slab)
371 {
372 g_assert(slab->refcount > 1);
373 slab->refcount--;
374 if (G_UNLIKELY(slab->refcount == 1 && slab == self->oldest_slab)) {
375 g_cond_broadcast(self->slab_free_cond);
376 } else if (G_UNLIKELY(slab->refcount == 0)) {
377 free_slab(slab);
378 }
379 }
380
381 /* called with the slab_mutex held, this sets *slabp to *slabp->next,
382 * adjusting refcounts appropriately, and returns the new value
383 *
384 * @param self: xfer element
385 * @param slabp: slab pointer to advance
386 * @returns: new value of *slabp
387 */
388 static inline Slab *
389 next_slab(
390 XferDestTaperCacher *self,
391 Slab * volatile *slabp)
392 {
393 Slab *next;
394
395 if (!slabp || !*slabp)
396 return NULL;
397
398 next = (*slabp)->next;
399 if (next)
400 next->refcount++;
401 if (*slabp)
402 unref_slab(self, *slabp);
403 *slabp = next;
404
405 return next;
406 }
407
408 /*
409 * Disk Cache
410 *
411 * The disk cache thread's job is simply to follow along the slab train at
412 * maximum speed, writing slabs to the disk cache file. */
413
414 static gboolean
415 open_disk_cache_fds(
416 XferDestTaperCacher *self)
417 {
418 char * filename;
419
420 g_assert(self->disk_cache_read_fd == -1);
421 g_assert(self->disk_cache_write_fd == -1);
422
423 g_mutex_lock(self->state_mutex);
424 filename = g_strdup_printf("%s/amanda-split-buffer-XXXXXX",
425 self->disk_cache_dirname);
426
427 self->disk_cache_write_fd = g_mkstemp(filename);
428 if (self->disk_cache_write_fd < 0) {
429 g_mutex_unlock(self->state_mutex);
430 xfer_cancel_with_error(XFER_ELEMENT(self),
431 _("Error creating cache file in '%s': %s"), self->disk_cache_dirname,
432 strerror(errno));
433 g_free(filename);
434 return FALSE;
435 }
436
437 /* open a separate copy of the file for reading */
438 self->disk_cache_read_fd = open(filename, O_RDONLY);
439 if (self->disk_cache_read_fd < 0) {
440 g_mutex_unlock(self->state_mutex);
441 xfer_cancel_with_error(XFER_ELEMENT(self),
442 _("Error opening cache file in '%s': %s"), self->disk_cache_dirname,
443 strerror(errno));
444 g_free(filename);
445 return FALSE;
446 }
447
448 /* signal anyone waiting for this value */
449 g_cond_broadcast(self->state_cond);
450 g_mutex_unlock(self->state_mutex);
451
452 /* errors from unlink are not fatal */
453 if (unlink(filename) < 0) {
454 g_warning("While unlinking '%s': %s (ignored)", filename, strerror(errno));
455 }
456
457 g_free(filename);
458 return TRUE;
459 }
460
461 static gpointer
462 disk_cache_thread(
463 gpointer data)
464 {
465 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(data);
466 XferElement *elt = XFER_ELEMENT(self);
467
468 DBG(1, "(this is the disk cache thread)");
469
470 /* open up the disk cache file first */
471 if (!open_disk_cache_fds(self))
472 return NULL;
473
474 while (!elt->cancelled) {
475 gboolean eof, eop;
476 guint64 stop_serial;
477 Slab *slab;
478
479 /* rewind to the begining of the disk cache file */
480 if (lseek(self->disk_cache_write_fd, 0, SEEK_SET) == -1) {
481 xfer_cancel_with_error(XFER_ELEMENT(self),
482 _("Error seeking disk cache file in '%s': %s"), self->disk_cache_dirname,
483 strerror(errno));
484 return NULL;
485 }
486
487 /* we need to sit and wait for the next part to begin, first making sure
488 * we have a slab .. */
489 g_mutex_lock(self->slab_mutex);
490 while (!self->disk_cacher_slab && !elt->cancelled) {
491 DBG(9, "waiting for a disk slab");
492 g_cond_wait(self->slab_cond, self->slab_mutex);
493 }
494 DBG(9, "disk_cache_thread done waiting");
495 g_mutex_unlock(self->slab_mutex);
496
497 if (elt->cancelled)
498 break;
499
500 /* this slab is now fixed until this thread changes it */
501 g_assert(self->disk_cacher_slab != NULL);
502
503 /* and then making sure we're ready to write that slab. */
504 g_mutex_lock(self->state_mutex);
505 while ((self->paused ||
506 (self->disk_cacher_slab && self->disk_cacher_slab->serial > self->part_first_serial))
507 && !elt->cancelled) {
508 DBG(9, "waiting for the disk slab to become current and un-paused");
509 g_cond_wait(self->state_cond, self->state_mutex);
510 }
511 DBG(9, "disk_cache_thread done waiting");
512
513 stop_serial = self->part_stop_serial;
514 g_mutex_unlock(self->state_mutex);
515
516 if (elt->cancelled)
517 break;
518
519 g_mutex_lock(self->slab_mutex);
520 slab = self->disk_cacher_slab;
521 eop = eof = FALSE;
522 while (!eop && !eof) {
523 /* if we're at the head of the slab train, wait for more data */
524 while (!self->disk_cacher_slab && !elt->cancelled) {
525 DBG(9, "waiting for the next disk slab");
526 g_cond_wait(self->slab_cond, self->slab_mutex);
527 }
528 DBG(9, "disk_cache_thread done waiting");
529
530 if (elt->cancelled)
531 break;
532
533 /* drop the lock long enough to write the slab; the refcount
534 * protects the slab during this time */
535 slab = self->disk_cacher_slab;
536 g_mutex_unlock(self->slab_mutex);
537
538 if (full_write(self->disk_cache_write_fd, slab->base, slab->size) < slab->size) {
539 xfer_cancel_with_error(XFER_ELEMENT(self),
540 _("Error writing to disk cache file in '%s': %s"), self->disk_cache_dirname,
541 strerror(errno));
542 return NULL;
543 }
544
545 eof = slab->size < self->slab_size;
546 eop = (slab->serial + 1 == stop_serial);
547
548 g_mutex_lock(self->slab_mutex);
549 next_slab(self, &self->disk_cacher_slab);
550 }
551 g_mutex_unlock(self->slab_mutex);
552
553 if (eof) {
554 /* this very thread should have just set this value to NULL, and since it's
555 * EOF, there should not be any 'next' slab */
556 g_assert(self->disk_cacher_slab == NULL);
557 break;
558 }
559 }
560
561 return NULL;
562 }
563
564 /*
565 * Device Thread
566 *
567 * The device thread's job is to write slabs to self->device, applying whatever
568 * streaming algorithms are required. It does this by alternately getting the
569 * next slab from a "slab source" and writing that slab to the device. Most of
570 * the slab source functions assume that self->slab_mutex is held, but may
571 * release the mutex (either explicitly or via a g_cond_wait), so it is not
572 * valid to assume that any slab pointers remain unchanged after a slab_source
573 * function invocation.
574 */
575
576 /* This struct tracks the current state of the slab source */
577 typedef struct slab_source_state {
578 /* temporary slab used for reading from disk */
579 Slab *tmp_slab;
580
581 /* next serial to read from disk */
582 guint64 next_serial;
583 } slab_source_state;
584
585 /* Called with the slab_mutex held, this function pre-buffers enough data into the slab
586 * train to meet the device's streaming needs. */
587 static gboolean
588 slab_source_prebuffer(
589 XferDestTaperCacher *self)
590 {
591 XferElement *elt = XFER_ELEMENT(self);
592 guint64 prebuffer_slabs = (self->max_memory + self->slab_size - 1) / self->slab_size;
593 guint64 i;
594 Slab *slab;
595
596 /* always prebuffer at least one slab, even if max_memory is 0 */
597 if (prebuffer_slabs == 0) prebuffer_slabs = 1;
598
599 /* pre-buffering is not necessary if we're retrying a part */
600 if (self->retry_part)
601 return TRUE;
602
603 /* pre-buffering means waiting until we have at least prebuffer_slabs in the
604 * slab train ahead of the device_slab, or the newest slab is at EOF. */
605 while (!elt->cancelled) {
606 gboolean eof_or_eop = FALSE;
607
608 /* see if there's enough data yet */
609 for (i = 0, slab = self->device_slab;
610 i < prebuffer_slabs && slab != NULL;
611 i++, slab = slab->next) {
612 eof_or_eop = (slab->size < self->slab_size)
613 || (slab->serial + 1 == self->part_stop_serial);
614 }
615 if (i == prebuffer_slabs || eof_or_eop)
616 break;
617
618 DBG(9, "prebuffering wait");
619 g_cond_wait(self->slab_cond, self->slab_mutex);
620 }
621 DBG(9, "slab_source_prebuffer done waiting");
622
623 if (elt->cancelled) {
624 self->last_part_successful = FALSE;
625 self->no_more_parts = TRUE;
626 return FALSE;
627 }
628
629 return TRUE;
630 }
631
632 /* Called without the slab_mutex held, this function sets up a new slab_source_state
633 * object based on the configuratino of the Xfer Element. */
634 static inline gboolean
635 slab_source_setup(
636 XferDestTaperCacher *self,
637 slab_source_state *state)
638 {
639 XferElement *elt = XFER_ELEMENT(self);
640 state->tmp_slab = NULL;
641 state->next_serial = G_MAXUINT64;
642
643 /* if we're to retry the part, rewind to the beginning */
644 if (self->retry_part) {
645 if (self->use_mem_cache) {
646 /* rewind device_slab to point to the mem_cache_slab */
647 g_mutex_lock(self->slab_mutex);
648 if (self->device_slab)
649 unref_slab(self, self->device_slab);
650 self->device_slab = self->mem_cache_slab;
651 if(self->device_slab != NULL)
652 self->device_slab->refcount++;
653 g_mutex_unlock(self->slab_mutex);
654 } else {
655 g_mutex_lock(self->slab_mutex);
656
657 /* we're going to read from the disk cache until we get to the oldest useful
658 * slab in memory, so it had best exist */
659 g_assert(self->oldest_slab != NULL);
660
661 /* point device_slab at the oldest slab we have */
662 self->oldest_slab->refcount++;
663 if (self->device_slab)
664 unref_slab(self, self->device_slab);
665 self->device_slab = self->oldest_slab;
666
667 /* and increment it until it is at least the slab we want to start from */
668 while (self->device_slab->serial < self->part_first_serial) {
669 next_slab(self, &self->device_slab);
670 }
671
672 /* get a new, temporary slab for use while reading */
673 state->tmp_slab = alloc_slab(self, TRUE);
674
675 g_mutex_unlock(self->slab_mutex);
676
677 if (!state->tmp_slab) {
678 /* if we couldn't allocate a slab, then we're cancelled, so we're done with
679 * this part. */
680 self->last_part_successful = FALSE;
681 self->no_more_parts = TRUE;
682 return FALSE;
683 }
684
685 state->tmp_slab->size = self->slab_size;
686 state->next_serial = self->part_first_serial;
687
688 /* We're reading from the disk cache, so we need a file descriptor
689 * to read from, so wait for disk_cache_thread to open the
690 * disk_cache_read_fd */
691 g_assert(self->disk_cache_dirname);
692 g_mutex_lock(self->state_mutex);
693 while (self->disk_cache_read_fd == -1 && !elt->cancelled) {
694 DBG(9, "waiting for disk_cache_thread to set disk_cache_read_fd");
695 g_cond_wait(self->state_cond, self->state_mutex);
696 }
697 DBG(9, "slab_source_setup done waiting");
698 g_mutex_unlock(self->state_mutex);
699
700 if (elt->cancelled) {
701 self->last_part_successful = FALSE;
702 self->no_more_parts = TRUE;
703 return FALSE;
704 }
705
706 /* rewind to the beginning */
707 if (lseek(self->disk_cache_read_fd, 0, SEEK_SET) == -1) {
708 xfer_cancel_with_error(XFER_ELEMENT(self),
709 _("Could not seek disk cache file for reading: %s"),
710 strerror(errno));
711 self->last_part_successful = FALSE;
712 self->no_more_parts = TRUE;
713 return FALSE;
714 }
715 }
716 }
717
718 /* if the streaming mode requires it, pre-buffer */
719 if (self->streaming == STREAMING_REQUIREMENT_DESIRED ||
720 self->streaming == STREAMING_REQUIREMENT_REQUIRED) {
721 gboolean prebuffer_ok;
722
723 g_mutex_lock(self->slab_mutex);
724 prebuffer_ok = slab_source_prebuffer(self);
725 g_mutex_unlock(self->slab_mutex);
726 if (!prebuffer_ok)
727 return FALSE;
728 }
729
730 return TRUE;
731 }
732
733 /* Called with the slab_mutex held, this does the work of slab_source_get when
734 * reading from the disk cache. Note that this explicitly releases the
735 * slab_mutex during execution - do not depend on any protected values across a
736 * call to this function. The mutex is held on return. */
737 static Slab *
738 slab_source_get_from_disk(
739 XferDestTaperCacher *self,
740 slab_source_state *state,
741 guint64 serial)
742 {
743 XferDestTaper *xdt = XFER_DEST_TAPER(self);
744 gsize bytes_read;
745
746 g_assert(state->next_serial == serial);
747
748 /* NOTE: slab_mutex is held, but we don't need it here, so release it for the moment */
749 g_mutex_unlock(self->slab_mutex);
750
751 bytes_read = read_fully(self->disk_cache_read_fd, state->tmp_slab->base,
752 self->slab_size, NULL);
753
754 if (bytes_read < self->slab_size) {
755 xfer_cancel_with_error(XFER_ELEMENT(xdt),
756 _("Error reading disk cache: %s"),
757 errno ? strerror(errno) : _("Unexpected EOF"));
758 goto fatal_error;
759 }
760
761 state->tmp_slab->serial = state->next_serial++;
762 g_mutex_lock(self->slab_mutex);
763 return state->tmp_slab;
764
765 fatal_error:
766 g_mutex_lock(self->slab_mutex);
767 self->last_part_successful = FALSE;
768 self->no_more_parts = TRUE;
769 return NULL;
770 }
771
772 /* Called with the slab_mutex held, this function gets the slab with the given
773 * serial number, waiting if necessary for that slab to be available. Note
774 * that the slab_mutex may be released during execution, although it is always
775 * held on return. */
776 static inline Slab *
777 slab_source_get(
778 XferDestTaperCacher *self,
779 slab_source_state *state,
780 guint64 serial)
781 {
782 XferElement *elt = (XferElement *)self;
783
784 /* device_slab is only NULL if we're following the slab train, so wait for
785 * a new slab */
786 if (!self->device_slab) {
787 /* if the streaming mode requires it, pre-buffer */
788 if (self->streaming == STREAMING_REQUIREMENT_DESIRED) {
789 if (!slab_source_prebuffer(self))
790 return NULL;
791
792 /* fall through to make sure we have a device_slab;
793 * slab_source_prebuffer doesn't guarantee device_slab != NULL */
794 }
795
796 while (self->device_slab == NULL && !elt->cancelled) {
797 DBG(9, "waiting for the next slab");
798 g_cond_wait(self->slab_cond, self->slab_mutex);
799 }
800 DBG(9, "slab_source_get done waiting");
801
802 if (elt->cancelled)
803 goto fatal_error;
804 }
805
806 /* device slab is now set, and only this thread can change it */
807 g_assert(self->device_slab);
808
809 /* if the next item in the device slab is the one we want, then the job is
810 * pretty easy */
811 if (G_LIKELY(serial == self->device_slab->serial))
812 return self->device_slab;
813
814 /* otherwise, we're reading from disk */
815 g_assert(serial < self->device_slab->serial);
816 return slab_source_get_from_disk(self, state, serial);
817
818 fatal_error:
819 self->last_part_successful = FALSE;
820 self->no_more_parts = TRUE;
821 return NULL;
822 }
823
824 /* Called without the slab_mutex held, this frees any resources assigned
825 * to the slab source state */
826 static inline void
827 slab_source_free(
828 XferDestTaperCacher *self,
829 slab_source_state *state)
830 {
831 if (state->tmp_slab) {
832 g_mutex_lock(self->slab_mutex);
833 free_slab(state->tmp_slab);
834 g_mutex_unlock(self->slab_mutex);
835 }
836 }
837
838 /* Called without the slab_mutex, this writes the given slab to the device */
839 static gboolean
840 write_slab_to_device(
841 XferDestTaperCacher *self,
842 Slab *slab)
843 {
844 XferElement *elt = XFER_ELEMENT(self);
845 gchar *buf = slab->base;
846 gsize remaining = slab->size;
847
848 while (remaining && !elt->cancelled) {
849 gsize write_size = MIN(self->block_size, remaining);
850 DeviceWriteResult ok;
851 ok = device_write_block(self->device, write_size, buf);
852 if (ok != WRITE_SUCCEED) {
853 self->bytes_written += slab->size - remaining;
854
855 /* TODO: handle an error without is_eom
856 * differently/fatally? or at least with a warning? */
857 self->last_part_successful = FALSE;
858 self->no_more_parts = FALSE;
859 return FALSE;
860 }
861
862 crc32_add((uint8_t *)buf, write_size, &elt->crc);
863 buf += write_size;
864 self->slab_bytes_written += write_size;
865 remaining -= write_size;
866 }
867
868 if (elt->cancelled) {
869 self->last_part_successful = FALSE;
870 self->no_more_parts = TRUE;
871 return FALSE;
872 }
873
874 self->bytes_written += slab->size;
875 self->slab_bytes_written = 0;
876 return TRUE;
877 }
878
879 static XMsg *
880 device_thread_write_part(
881 XferDestTaperCacher *self)
882 {
883 XferElement *elt = XFER_ELEMENT(self);
884 GTimer *timer = g_timer_new();
885 XMsg *msg;
886 slab_source_state src_state = {0, 0};
887 guint64 serial, stop_serial;
888 gboolean eof = FALSE;
889 int fileno = 0;
890 int failed = 0;
891 int slab_source_set = 0;
892
893 self->last_part_successful = FALSE;
894 self->bytes_written = 0;
895 self->crc_before_part = elt->crc;
896
897 if (!device_start_file(self->device, self->part_header)) {
898 failed = 1;
899 goto part_done;
900 }
901
902 dumpfile_free(self->part_header);
903 self->part_header = NULL;
904
905 fileno = self->device->file;
906 g_assert(fileno > 0);
907
908 if (!slab_source_setup(self, &src_state))
909 goto part_done;
910 slab_source_set = 1;
911
912 g_timer_start(timer);
913
914 stop_serial = self->part_stop_serial;
915 g_mutex_lock(self->slab_mutex);
916 for (serial = self->part_first_serial; serial < stop_serial && !eof; serial++) {
917 Slab *slab = slab_source_get(self, &src_state, serial);
918 DBG(8, "writing slab %p (serial %ju) to device", slab, serial);
919 g_mutex_unlock(self->slab_mutex);
920 if (!slab) {
921 failed = 1;
922 goto part_done;
923 }
924
925 eof = slab->size < self->slab_size;
926
927 if (!write_slab_to_device(self, slab)) {
928 failed = 1;
929 goto part_done;
930 }
931
932 g_mutex_lock(self->slab_mutex);
933 DBG(8, "wrote slab %p to device", slab);
934
935 /* if we're reading from the slab train, advance self->device_slab. */
936 if (slab == self->device_slab) {
937 next_slab(self, &self->device_slab);
938 }
939 }
940 g_mutex_unlock(self->slab_mutex);
941
942 part_done:
943 /* if we write all of the blocks, but the finish_file fails, then likely
944 * there was some buffering going on in the device driver, and the blocks
945 * did not all make it to permanent storage -- so it's a failed part. */
946 if (self->device->in_file && !device_finish_file(self->device))
947 failed = 1;
948
949 if (slab_source_set) {
950 slab_source_free(self, &src_state);
951 }
952
953 if (!failed) {
954 self->last_part_successful = TRUE;
955 self->no_more_parts = eof;
956 } else {
957 elt->crc = self->crc_before_part;
958 }
959
960 g_timer_stop(timer);
961
962 msg = xmsg_new(XFER_ELEMENT(self), XMSG_PART_DONE, 0);
963 msg->size = self->bytes_written;
964 msg->duration = g_timer_elapsed(timer, NULL);
965 msg->partnum = self->partnum;
966 msg->fileno = fileno;
967 msg->successful = self->last_part_successful;
968 msg->eom = !self->last_part_successful;
969 msg->eof = self->no_more_parts;
970
971 /* time runs backward on some test boxes, so make sure this is positive */
972 if (msg->duration < 0) msg->duration = 0;
973
974 if (self->last_part_successful)
975 self->partnum++;
976
977 g_timer_destroy(timer);
978
979 return msg;
980 }
981
982 /* Called with the status_mutex held, this frees any cached data for
983 * a successful part */
984 static void
985 release_part_cache(
986 XferDestTaperCacher *self)
987 {
988 if (self->use_mem_cache && self->mem_cache_slab) {
989 /* move up the mem_cache_slab to point to the first slab in
990 * the next part (probably NULL at this point), so that the
991 * reader can continue reading data into the new mem cache
992 * immediately. */
993 g_mutex_lock(self->slab_mutex);
994 unref_slab(self, self->mem_cache_slab);
995 self->mem_cache_slab = self->device_slab;
996 if (self->mem_cache_slab)
997 self->mem_cache_slab->refcount++;
998 g_mutex_unlock(self->slab_mutex);
999 }
1000
1001 /* the disk cache gets reused automatically (rewinding to offset 0), so
1002 * there's nothing else to do */
1003 }
1004
1005 static gpointer
1006 device_thread(
1007 gpointer data)
1008 {
1009 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(data);
1010 XferElement *elt = XFER_ELEMENT(self);
1011 XMsg *msg;
1012
1013 DBG(1, "(this is the device thread)");
1014
1015 if (self->disk_cache_dirname) {
1016 GError *error = NULL;
1017 self->disk_cache_thread = g_thread_create(disk_cache_thread, (gpointer)self, TRUE, &error);
1018 if (!self->disk_cache_thread) {
1019 g_critical(_("Error creating new thread: %s (%s)"),
1020 error->message, errno? strerror(errno) : _("no error code"));
1021 }
1022 }
1023
1024 /* This is the outer loop, that loops once for each split part written to
1025 * tape. */
1026 g_mutex_lock(self->state_mutex);
1027 while (1) {
1028 /* wait until the main thread un-pauses us, and check that we have
1029 * the relevant device info available (block_size) */
1030 while (self->paused && !elt->cancelled) {
1031 DBG(9, "waiting to be unpaused");
1032 g_cond_wait(self->state_cond, self->state_mutex);
1033 }
1034 DBG(9, "device_thread done waiting");
1035
1036 if (elt->cancelled)
1037 break;
1038
1039 g_mutex_unlock(self->state_mutex);
1040 self->slab_bytes_written = 0;
1041 DBG(2, "beginning to write part");
1042 msg = device_thread_write_part(self);
1043 DBG(2, "done writing part");
1044 g_mutex_lock(self->state_mutex);
1045
1046 /* release any cache of a successful part, but don't bother at EOF */
1047 if (msg->successful && !msg->eof)
1048 release_part_cache(self);
1049
1050 xfer_queue_message(elt->xfer, msg);
1051
1052 /* if this is the last part, we're done with the part loop */
1053 if (self->no_more_parts)
1054 break;
1055
1056 /* pause ourselves and await instructions from the main thread */
1057 self->paused = TRUE;
1058 }
1059
1060 g_mutex_unlock(self->state_mutex);
1061
1062 /* make sure the other thread is done before we send XMSG_DONE */
1063 if (self->disk_cache_thread)
1064 g_thread_join(self->disk_cache_thread);
1065
1066 g_debug("sending XMSG_CRC message");
1067 g_debug("xfer-dest-taper-cacher CRC %08x size %lld",
1068 crc32_finish(&elt->crc), (long long)elt->crc.size);
1069 msg = xmsg_new(XFER_ELEMENT(self), XMSG_CRC, 0);
1070 msg->crc = crc32_finish(&elt->crc);
1071 msg->size = elt->crc.size;
1072 xfer_queue_message(elt->xfer, msg);
1073
1074 /* tell the main thread we're done */
1075 xfer_queue_message(XFER_ELEMENT(self)->xfer, xmsg_new(XFER_ELEMENT(self), XMSG_DONE, 0));
1076
1077 return NULL;
1078 }
1079
1080 /*
1081 * Class mechanics
1082 */
1083
1084 /* called with the slab_mutex held, this adds the reader_slab to the head of
1085 * the slab train and signals the condition variable. */
1086 static void
1087 add_reader_slab_to_train(
1088 XferDestTaperCacher *self)
1089 {
1090 Slab *slab = self->reader_slab;
1091
1092 DBG(3, "adding slab of new data to the slab train");
1093
1094 if (self->newest_slab) {
1095 self->newest_slab->next = slab;
1096 slab->refcount++;
1097
1098 self->newest_slab->refcount--;
1099 }
1100
1101 self->newest_slab = slab; /* steal reader_slab's ref */
1102 self->reader_slab = NULL;
1103
1104 /* steal reader_slab's reference for newest_slab */
1105
1106 /* if any of the other pointers are waiting for this slab, update them */
1107 if (self->disk_cache_dirname && !self->disk_cacher_slab) {
1108 self->disk_cacher_slab = slab;
1109 slab->refcount++;
1110 }
1111 if (self->use_mem_cache && !self->mem_cache_slab) {
1112 self->mem_cache_slab = slab;
1113 slab->refcount++;
1114 }
1115 if (!self->device_slab) {
1116 self->device_slab = slab;
1117 slab->refcount++;
1118 }
1119 if (!self->oldest_slab) {
1120 self->oldest_slab = slab;
1121 slab->refcount++;
1122 }
1123
1124 g_cond_broadcast(self->slab_cond);
1125 }
1126
1127 static void
1128 push_buffer_static_impl(
1129 XferElement *elt,
1130 gpointer buf,
1131 size_t size)
1132 {
1133 XferDestTaperCacher *self = (XferDestTaperCacher *)elt;
1134 char *p;
1135
1136 DBG(3, "push_buffer_static(%p, %ju)", buf, (uintmax_t)size);
1137
1138 /* do nothing if cancelled */
1139 if (G_UNLIKELY(elt->cancelled)) {
1140 goto free_and_finish;
1141 }
1142
1143 /* handle EOF */
1144 if (G_UNLIKELY(buf == NULL) || size == 0) {
1145 /* send off the last, probably partial slab */
1146 g_mutex_lock(self->slab_mutex);
1147
1148 /* create a new, empty slab if necessary */
1149 if (!self->reader_slab) {
1150 self->reader_slab = alloc_slab(self, FALSE);
1151 if (!self->reader_slab) {
1152 /* we've been cancelled while waiting for a slab */
1153 g_mutex_unlock(self->slab_mutex);
1154
1155 /* wait for the xfer to cancel, so we don't get another buffer
1156 * pushed to us (and do so *without* the mutex held) */
1157 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
1158
1159 goto free_and_finish;
1160 }
1161 self->reader_slab->serial = self->next_serial++;
1162 }
1163
1164 add_reader_slab_to_train(self);
1165 g_mutex_unlock(self->slab_mutex);
1166
1167 goto free_and_finish;
1168 }
1169
1170 p = buf;
1171 while (1) {
1172 gsize copy_size;
1173
1174 /* get a fresh slab, if needed */
1175 if (G_UNLIKELY(!self->reader_slab) || self->reader_slab->size == self->slab_size) {
1176 g_mutex_lock(self->slab_mutex);
1177 if (self->reader_slab)
1178 add_reader_slab_to_train(self);
1179 self->reader_slab = alloc_slab(self, FALSE);
1180 if (!self->reader_slab) {
1181 /* we've been cancelled while waiting for a slab */
1182 g_mutex_unlock(self->slab_mutex);
1183
1184 /* wait for the xfer to cancel, so we don't get another buffer
1185 * pushed to us (and do so *without* the mutex held) */
1186 wait_until_xfer_cancelled(XFER_ELEMENT(self)->xfer);
1187
1188 goto free_and_finish;
1189 }
1190 self->reader_slab->serial = self->next_serial++;
1191 g_mutex_unlock(self->slab_mutex);
1192 }
1193
1194 if (size == 0)
1195 break;
1196
1197 copy_size = MIN(self->slab_size - self->reader_slab->size, size);
1198 memcpy(self->reader_slab->base+self->reader_slab->size, p, copy_size);
1199
1200 self->reader_slab->size += copy_size;
1201 p += copy_size;
1202 size -= copy_size;
1203 }
1204
1205 free_and_finish:
1206 return;
1207 }
1208
1209 static void
1210 push_buffer_impl(
1211 XferElement *elt,
1212 gpointer buf,
1213 size_t size)
1214 {
1215 push_buffer_static_impl(elt, buf, size);
1216
1217 if (buf)
1218 g_free(buf);
1219 }
1220
1221 /*
1222 * Element mechanics
1223 */
1224
1225 static gboolean
1226 start_impl(
1227 XferElement *elt)
1228 {
1229 XferDestTaperCacher *self = (XferDestTaperCacher *)elt;
1230 GError *error = NULL;
1231
1232 self->device_thread = g_thread_create(device_thread, (gpointer)self, FALSE, &error);
1233 if (!self->device_thread) {
1234 g_critical(_("Error creating new thread: %s (%s)"),
1235 error->message, errno? strerror(errno) : _("no error code"));
1236 }
1237
1238 return TRUE;
1239 }
1240
1241 static gboolean
1242 cancel_impl(
1243 XferElement *elt,
1244 gboolean expect_eof)
1245 {
1246 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(elt);
1247 gboolean rv;
1248
1249 /* chain up first */
1250 rv = XFER_ELEMENT_CLASS(parent_class)->cancel(elt, expect_eof);
1251
1252 /* then signal all of our condition variables, so that threads waiting on them
1253 * wake up and see elt->cancelled. */
1254 g_mutex_lock(self->slab_mutex);
1255 g_cond_broadcast(self->slab_cond);
1256 g_cond_broadcast(self->slab_free_cond);
1257 g_mutex_unlock(self->slab_mutex);
1258
1259 g_mutex_lock(self->state_mutex);
1260 g_cond_broadcast(self->state_cond);
1261 g_mutex_unlock(self->state_mutex);
1262
1263 return rv;
1264 }
1265
1266 static void
1267 start_part_impl(
1268 XferDestTaper *xdt,
1269 gboolean retry_part,
1270 dumpfile_t *header)
1271 {
1272 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(xdt);
1273
1274 g_assert(self->device != NULL);
1275 g_assert(!self->device->in_file);
1276 g_assert(header != NULL);
1277
1278 DBG(1, "start_part(retry_part=%d)", retry_part);
1279
1280 g_mutex_lock(self->state_mutex);
1281 g_assert(self->paused);
1282 g_assert(!self->no_more_parts);
1283
1284 if (self->part_header)
1285 dumpfile_free(self->part_header);
1286 self->part_header = dumpfile_copy(header);
1287
1288 if (retry_part) {
1289 g_assert(!self->last_part_successful);
1290 self->retry_part = TRUE;
1291 } else {
1292 g_assert(self->last_part_successful);
1293 self->retry_part = FALSE;
1294 self->part_first_serial = self->part_stop_serial;
1295 if (self->part_size != 0) {
1296 self->part_stop_serial = self->part_first_serial + self->slabs_per_part;
1297 } else {
1298 /* set part_stop_serial to an effectively infinite value */
1299 self->part_stop_serial = G_MAXUINT64;
1300 }
1301 }
1302
1303 DBG(1, "unpausing");
1304 self->paused = FALSE;
1305 g_cond_broadcast(self->state_cond);
1306
1307 g_mutex_unlock(self->state_mutex);
1308 }
1309
1310 static void
1311 use_device_impl(
1312 XferDestTaper *xdt,
1313 Device *device)
1314 {
1315 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(xdt);
1316 GValue val;
1317
1318 /* short-circuit if nothing is changing */
1319 if (self->device == device)
1320 return;
1321
1322 g_mutex_lock(self->state_mutex);
1323 if (self->device)
1324 g_object_unref(self->device);
1325 self->device = device;
1326 g_object_ref(device);
1327
1328 /* get this new device's streaming requirements */
1329 bzero(&val, sizeof(val));
1330 if (!device_property_get(self->device, PROPERTY_STREAMING, &val)
1331 || !G_VALUE_HOLDS(&val, STREAMING_REQUIREMENT_TYPE)) {
1332 g_warning("Couldn't get streaming type for %s", self->device->device_name);
1333 self->streaming = STREAMING_REQUIREMENT_REQUIRED;
1334 } else {
1335 self->streaming = g_value_get_enum(&val);
1336 }
1337 g_value_unset(&val);
1338
1339 /* check that the blocksize hasn't changed */
1340 if (self->block_size != device->block_size) {
1341 g_mutex_unlock(self->state_mutex);
1342 xfer_cancel_with_error(XFER_ELEMENT(self),
1343 _("All devices used by the taper must have the same block size"));
1344 return;
1345 }
1346 g_mutex_unlock(self->state_mutex);
1347 }
1348
1349 static guint64
1350 get_part_bytes_written_impl(
1351 XferDestTaper *xdt)
1352 {
1353 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(xdt);
1354
1355 /* NOTE: this access is unsafe and may return inconsistent results (e.g, a
1356 * partial write to the 64-bit value on a 32-bit system). This is ok for
1357 * the moment, as it's only informational, but be warned. */
1358 if (self->device) {
1359 return device_get_bytes_written(self->device);
1360 } else {
1361 return self->bytes_written + self->slab_bytes_written;
1362 }
1363
1364 }
1365
1366 static void
1367 instance_init(
1368 XferElement *elt)
1369 {
1370 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(elt);
1371 elt->can_generate_eof = FALSE;
1372
1373 self->state_mutex = g_mutex_new();
1374 self->state_cond = g_cond_new();
1375 self->slab_mutex = g_mutex_new();
1376 self->slab_cond = g_cond_new();
1377 self->slab_free_cond = g_cond_new();
1378
1379 self->last_part_successful = TRUE;
1380 self->paused = TRUE;
1381 self->part_stop_serial = 0;
1382 self->disk_cache_read_fd = -1;
1383 self->disk_cache_write_fd = -1;
1384 crc32_init(&elt->crc);
1385 }
1386
1387 static void
1388 finalize_impl(
1389 GObject * obj_self)
1390 {
1391 XferDestTaperCacher *self = XFER_DEST_TAPER_CACHER(obj_self);
1392 Slab *slab, *next_slab;
1393
1394 if (self->disk_cache_dirname)
1395 g_free(self->disk_cache_dirname);
1396
1397 g_mutex_free(self->state_mutex);
1398 g_cond_free(self->state_cond);
1399
1400 g_mutex_free(self->slab_mutex);
1401 g_cond_free(self->slab_cond);
1402 g_cond_free(self->slab_free_cond);
1403
1404 /* free the slab train, without reference to the refcounts */
1405 for (slab = self->oldest_slab; slab != NULL; slab = next_slab) {
1406 next_slab = slab->next;
1407 free_slab(slab);
1408 }
1409 self->disk_cacher_slab = NULL;
1410 self->mem_cache_slab = NULL;
1411 self->device_slab = NULL;
1412 self->oldest_slab = NULL;
1413 self->newest_slab = NULL;
1414
1415 if (self->reader_slab) {
1416 free_slab(self->reader_slab);
1417 self->reader_slab = NULL;
1418 }
1419
1420 if (self->part_header)
1421 dumpfile_free(self->part_header);
1422
1423 if (self->disk_cache_read_fd != -1)
1424 close(self->disk_cache_read_fd); /* ignore error */
1425 if (self->disk_cache_write_fd != -1)
1426 close(self->disk_cache_write_fd); /* ignore error */
1427
1428 if (self->device)
1429 g_object_unref(self->device);
1430
1431 /* chain up */
1432 G_OBJECT_CLASS(parent_class)->finalize(obj_self);
1433 }
1434
1435 static void
1436 class_init(
1437 XferDestTaperCacherClass * selfc)
1438 {
1439 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
1440 XferDestTaperClass *xdt_klass = XFER_DEST_TAPER_CLASS(selfc);
1441 GObjectClass *goc = G_OBJECT_CLASS(selfc);
1442 static xfer_element_mech_pair_t mech_pairs[] = {
1443 { XFER_MECH_PUSH_BUFFER, XFER_MECH_NONE, XFER_NROPS(1), XFER_NTHREADS(1), XFER_NALLOC(0) },
1444 { XFER_MECH_PUSH_BUFFER_STATIC, XFER_MECH_NONE, XFER_NROPS(1), XFER_NTHREADS(1), XFER_NALLOC(0) },
1445 { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0), XFER_NALLOC(0) }
1446 };
1447
1448 klass->start = start_impl;
1449 klass->cancel = cancel_impl;
1450 klass->push_buffer = push_buffer_impl;
1451 klass->push_buffer_static = push_buffer_static_impl;
1452 xdt_klass->start_part = start_part_impl;
1453 xdt_klass->use_device = use_device_impl;
1454 xdt_klass->get_part_bytes_written = get_part_bytes_written_impl;
1455 goc->finalize = finalize_impl;
1456
1457 klass->perl_class = "Amanda::Xfer::Dest::Taper::Cacher";
1458 klass->mech_pairs = mech_pairs;
1459
1460 parent_class = g_type_class_peek_parent(selfc);
1461 }
1462
1463 static GType
1464 xfer_dest_taper_cacher_get_type (void)
1465 {
1466 static GType type = 0;
1467
1468 if (G_UNLIKELY(type == 0)) {
1469 static const GTypeInfo info = {
1470 sizeof (XferDestTaperCacherClass),
1471 (GBaseInitFunc) NULL,
1472 (GBaseFinalizeFunc) NULL,
1473 (GClassInitFunc) class_init,
1474 (GClassFinalizeFunc) NULL,
1475 NULL /* class_data */,
1476 sizeof (XferDestTaperCacher),
1477 0 /* n_preallocs */,
1478 (GInstanceInitFunc) instance_init,
1479 NULL
1480 };
1481
1482 type = g_type_register_static (XFER_DEST_TAPER_TYPE, "XferDestTaperCacher", &info, 0);
1483 }
1484
1485 return type;
1486 }
1487
1488 /*
1489 * Constructor
1490 */
1491
1492 XferElement *
1493 xfer_dest_taper_cacher(
1494 Device *first_device,
1495 size_t max_memory,
1496 guint64 part_size,
1497 gboolean use_mem_cache,
1498 const char *disk_cache_dirname)
1499 {
1500 XferDestTaperCacher *self = (XferDestTaperCacher *)g_object_new(XFER_DEST_TAPER_CACHER_TYPE, NULL);
1501
1502 self->max_memory = max_memory;
1503 self->part_size = part_size;
1504 self->partnum = 1;
1505 self->device = first_device;
1506 g_object_ref(self->device);
1507
1508 /* pick only one caching mechanism, caller! */
1509 if (use_mem_cache)
1510 g_assert(!disk_cache_dirname);
1511 if (disk_cache_dirname)
1512 g_assert(!use_mem_cache);
1513
1514 /* and if part size is zero, then we don't do any caching */
1515 g_assert(part_size != 0 || (!use_mem_cache && !disk_cache_dirname));
1516
1517 self->use_mem_cache = use_mem_cache;
1518 if (disk_cache_dirname)
1519 self->disk_cache_dirname = g_strdup(disk_cache_dirname);
1520
1521 /* calculate the device-dependent parameters */
1522 self->block_size = first_device->block_size;
1523
1524 /* The slab size should be large enough to justify the overhead of all
1525 * of the mutexes, but it needs to be small enough to have a few slabs
1526 * available so that the threads are not constantly waiting on one
1527 * another. The choice is sixteen blocks, not more than a quarter of
1528 * the part size, and not more than 10MB. If we're not using the mem
1529 * cache, then avoid exceeding max_memory by keeping the slab size less
1530 * than a quarter of max_memory. */
1531
1532 self->slab_size = self->block_size * 16;
1533 if (self->part_size)
1534 self->slab_size = MIN(self->slab_size, self->part_size / 4);
1535 self->slab_size = MIN(self->slab_size, 10*1024*1024);
1536 if (!use_mem_cache)
1537 self->slab_size = MIN(self->slab_size, self->max_memory / 4);
1538
1539 /* round slab size up to the nearest multiple of the block size */
1540 self->slab_size =
1541 ((self->slab_size + self->block_size - 1) / self->block_size) * self->block_size;
1542
1543 /* round part size up to a multiple of the slab size */
1544 if (self->part_size != 0) {
1545 self->slabs_per_part = (self->part_size + self->slab_size - 1) / self->slab_size;
1546 self->part_size = self->slabs_per_part * self->slab_size;
1547 } else {
1548 self->slabs_per_part = 0;
1549 }
1550
1551 /* set max_slabs */
1552 if (use_mem_cache) {
1553 self->max_slabs = self->slabs_per_part; /* increase max_slabs to serve as mem buf */
1554 } else {
1555 self->max_slabs = (self->max_memory + self->slab_size - 1) / self->slab_size;
1556 }
1557
1558 /* Note that max_slabs == 1 will cause deadlocks, due to some assumptions in
1559 * alloc_slab, so we check here that it's at least 2. */
1560 if (self->max_slabs < 2)
1561 self->max_slabs = 2;
1562
1563 DBG(1, "using slab_size %zu and max_slabs %ju", self->slab_size, (uintmax_t)self->max_slabs);
1564
1565 return XFER_ELEMENT(self);
1566 }