summaryrefslogtreecommitdiffstats
path: root/src/VBox/Storage/VDPlugin.cpp
blob: 8cf7b885aba4829d7196080b3a42ba174e46b8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/* $Id: VDPlugin.cpp $ */
/** @file
 * VD - Virtual disk container implementation, plugin related bits.
 */

/*
 * Copyright (C) 2017-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_VD
#include <VBox/err.h>
#include <VBox/sup.h>
#include <VBox/log.h>
#include <VBox/vd-plugin.h>

#include <iprt/dir.h>
#include <iprt/ldr.h>
#include <iprt/mem.h>
#include <iprt/path.h>

#include "VDInternal.h"
#include "VDBackends.h"


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Plugin structure.
 */
typedef struct VDPLUGIN
{
    /** Pointer to the next plugin structure. */
    RTLISTNODE NodePlugin;
    /** Handle of loaded plugin library. */
    RTLDRMOD   hPlugin;
    /** Filename of the loaded plugin. */
    char       *pszFilename;
} VDPLUGIN;
/** Pointer to a plugin structure. */
typedef VDPLUGIN *PVDPLUGIN;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/** Head of loaded plugin list. */
static RTLISTANCHOR g_ListPluginsLoaded;
#endif

/** Number of image backends supported. */
static unsigned g_cBackends = 0;
/** Array of pointers to the image backends. */
static PCVDIMAGEBACKEND *g_apBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/** Array of handles to the corresponding plugin. */
static RTLDRMOD *g_ahBackendPlugins = NULL;
#endif
/**
 * Builtin image backends.
 *
 * @note As long as the pfnProb() calls aren't scored, the ordering influences
 *       which backend take precedence.  In particular, the RAW backend should
 *       be thowards the end of the list.
 */
static PCVDIMAGEBACKEND aStaticBackends[] =
{
    &g_VmdkBackend,
    &g_VDIBackend,
    &g_VhdBackend,
    &g_ParallelsBackend,
    &g_DmgBackend,
    &g_QedBackend,
    &g_QCowBackend,
    &g_VhdxBackend,
    &g_CueBackend,
    &g_VBoxIsoMakerBackend,
    &g_RawBackend,
    &g_ISCSIBackend
};

/** Number of supported cache backends. */
static unsigned g_cCacheBackends = 0;
/** Array of pointers to the cache backends. */
static PCVDCACHEBACKEND *g_apCacheBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/** Array of handles to the corresponding plugin.
 *
 * @todo r=bird: This looks rather pointless.
 */
static RTLDRMOD *g_ahCacheBackendPlugins = NULL;
#endif
/** Builtin cache backends. */
static PCVDCACHEBACKEND aStaticCacheBackends[] =
{
    &g_VciCacheBackend
};

/** Number of supported filter backends. */
static unsigned g_cFilterBackends = 0;
/** Array of pointers to the filters backends. */
static PCVDFILTERBACKEND *g_apFilterBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/** Array of handles to the corresponding plugin. */
static PRTLDRMOD g_pahFilterBackendPlugins = NULL;
#endif


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/

/**
 * Add an array of image format backends from the given plugin to the list of known
 * image formats.
 *
 * @returns VBox status code.
 * @param   hPlugin         The plugin handle the backends belong to, can be NIL_RTLDRMOD
 *                          for compiled in backends.
 * @param   ppBackends      The array of image backend descriptors to add.
 * @param   cBackends       Number of descriptors in the array.
 */
static int vdAddBackends(RTLDRMOD hPlugin, PCVDIMAGEBACKEND *ppBackends, unsigned cBackends)
{
    PCVDIMAGEBACKEND *pTmp = (PCVDIMAGEBACKEND *)RTMemRealloc(g_apBackends,
           (g_cBackends + cBackends) * sizeof(PCVDIMAGEBACKEND));
    if (RT_UNLIKELY(!pTmp))
        return VERR_NO_MEMORY;
    g_apBackends = pTmp;
    memcpy(&g_apBackends[g_cBackends], ppBackends, cBackends * sizeof(PCVDIMAGEBACKEND));

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    RTLDRMOD *pTmpPlugins = (RTLDRMOD*)RTMemRealloc(g_ahBackendPlugins,
           (g_cBackends + cBackends) * sizeof(RTLDRMOD));
    if (RT_UNLIKELY(!pTmpPlugins))
        return VERR_NO_MEMORY;
    g_ahBackendPlugins = pTmpPlugins;
    for (unsigned i = g_cBackends; i < g_cBackends + cBackends; i++)
        g_ahBackendPlugins[i] = hPlugin;
#else
    RT_NOREF(hPlugin);
#endif

    g_cBackends += cBackends;
    return VINF_SUCCESS;
}


/**
 * Add an array of cache format backends from the given plugin to the list of known
 * cache formats.
 *
 * @returns VBox status code.
 * @param   hPlugin         The plugin handle the backends belong to, can be NIL_RTLDRMOD
 *                          for compiled in backends.
 * @param   ppBackends      The array of cache backend descriptors to add.
 * @param   cBackends       Number of descriptors in the array.
 */
static int vdAddCacheBackends(RTLDRMOD hPlugin, PCVDCACHEBACKEND *ppBackends, unsigned cBackends)
{
    PCVDCACHEBACKEND *pTmp = (PCVDCACHEBACKEND*)RTMemReallocTag(g_apCacheBackends,
                                                                (g_cCacheBackends + cBackends) * sizeof(PCVDCACHEBACKEND),
                                                                "may-leak:vdAddCacheBackend");
    if (RT_UNLIKELY(!pTmp))
        return VERR_NO_MEMORY;
    g_apCacheBackends = pTmp;
    memcpy(&g_apCacheBackends[g_cCacheBackends], ppBackends, cBackends * sizeof(PCVDCACHEBACKEND));

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    RTLDRMOD *pTmpPlugins = (RTLDRMOD*)RTMemReallocTag(g_ahCacheBackendPlugins,
                                                       (g_cCacheBackends + cBackends) * sizeof(RTLDRMOD),
                                                       "may-leak:vdAddCacheBackend");
    if (RT_UNLIKELY(!pTmpPlugins))
        return VERR_NO_MEMORY;
    g_ahCacheBackendPlugins = pTmpPlugins;
    for (unsigned i = g_cCacheBackends; i < g_cCacheBackends + cBackends; i++)
        g_ahCacheBackendPlugins[i] = hPlugin;
#else
    RT_NOREF(hPlugin);
#endif

    g_cCacheBackends += cBackends;
    return VINF_SUCCESS;
}

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/**
 * Add a single image format backend to the list of known image formats.
 *
 * @returns VBox status code.
 * @param   hPlugin         The plugin handle the backend belongs to, can be NIL_RTLDRMOD
 *                          for compiled in backends.
 * @param   pBackend        The image backend descriptors to add.
 */
DECLINLINE(int) vdAddBackend(RTLDRMOD hPlugin, PCVDIMAGEBACKEND pBackend)
{
    return vdAddBackends(hPlugin, &pBackend, 1);
}


/**
 * Add a single cache format backend to the list of known cache formats.
 *
 * @returns VBox status code.
 * @param   hPlugin         The plugin handle the backend belongs to, can be NIL_RTLDRMOD
 *                          for compiled in backends.
 * @param   pBackend        The cache backend descriptors to add.
 */
DECLINLINE(int) vdAddCacheBackend(RTLDRMOD hPlugin, PCVDCACHEBACKEND pBackend)
{
    return vdAddCacheBackends(hPlugin, &pBackend, 1);
}


/**
 * Add several filter backends.
 *
 * @returns VBox status code.
 * @param   hPlugin       Plugin handle to add.
 * @param   ppBackends    Array of filter backends to add.
 * @param   cBackends     Number of backends to add.
 */
static int vdAddFilterBackends(RTLDRMOD hPlugin, PCVDFILTERBACKEND *ppBackends, unsigned cBackends)
{
    PCVDFILTERBACKEND *pTmp = (PCVDFILTERBACKEND *)RTMemRealloc(g_apFilterBackends,
           (g_cFilterBackends + cBackends) * sizeof(PCVDFILTERBACKEND));
    if (RT_UNLIKELY(!pTmp))
        return VERR_NO_MEMORY;
    g_apFilterBackends = pTmp;

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    PRTLDRMOD pTmpPlugins = (PRTLDRMOD)RTMemRealloc(g_pahFilterBackendPlugins,
                                                    (g_cFilterBackends + cBackends) * sizeof(RTLDRMOD));
    if (RT_UNLIKELY(!pTmpPlugins))
        return VERR_NO_MEMORY;

    g_pahFilterBackendPlugins = pTmpPlugins;
    memcpy(&g_apFilterBackends[g_cFilterBackends], ppBackends, cBackends * sizeof(PCVDFILTERBACKEND));
    for (unsigned i = g_cFilterBackends; i < g_cFilterBackends + cBackends; i++)
        g_pahFilterBackendPlugins[i] = hPlugin;
#else
    RT_NOREF(hPlugin);
#endif

    g_cFilterBackends += cBackends;
    return VINF_SUCCESS;
}


/**
 * Add a single filter backend to the list of supported filters.
 *
 * @returns VBox status code.
 * @param   hPlugin     Plugin handle to add.
 * @param   pBackend    The backend to add.
 */
DECLINLINE(int) vdAddFilterBackend(RTLDRMOD hPlugin, PCVDFILTERBACKEND pBackend)
{
    return vdAddFilterBackends(hPlugin, &pBackend, 1);
}

/**
 * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterImage}
 */
static DECLCALLBACK(int) vdPluginRegisterImage(void *pvUser, PCVDIMAGEBACKEND pBackend)
{
    int rc = VINF_SUCCESS;

    if (VD_VERSION_ARE_COMPATIBLE(VD_IMGBACKEND_VERSION, pBackend->u32Version))
        vdAddBackend((RTLDRMOD)pvUser, pBackend);
    else
    {
        LogFunc(("ignored plugin: pBackend->u32Version=%u rc=%Rrc\n", pBackend->u32Version, rc));
        rc = VERR_IGNORED;
    }

    return rc;
}

/**
 * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterCache}
 */
static DECLCALLBACK(int) vdPluginRegisterCache(void *pvUser, PCVDCACHEBACKEND pBackend)
{
    int rc = VINF_SUCCESS;

    if (VD_VERSION_ARE_COMPATIBLE(VD_CACHEBACKEND_VERSION, pBackend->u32Version))
        vdAddCacheBackend((RTLDRMOD)pvUser, pBackend);
    else
    {
        LogFunc(("ignored plugin: pBackend->u32Version=%u rc=%Rrc\n", pBackend->u32Version, rc));
        rc = VERR_IGNORED;
    }

    return rc;
}

/**
 * @interface_method_impl{VDBACKENDREGISTER,pfnRegisterFilter}
 */
static DECLCALLBACK(int) vdPluginRegisterFilter(void *pvUser, PCVDFILTERBACKEND pBackend)
{
    int rc = VINF_SUCCESS;

    if (VD_VERSION_ARE_COMPATIBLE(VD_FLTBACKEND_VERSION, pBackend->u32Version))
        vdAddFilterBackend((RTLDRMOD)pvUser, pBackend);
    else
    {
        LogFunc(("ignored plugin: pBackend->u32Version=%u rc=%Rrc\n", pBackend->u32Version, rc));
        rc = VERR_IGNORED;
    }

    return rc;
}

/**
 * Checks whether the given plugin filename was already loaded.
 *
 * @returns Pointer to already loaded plugin, NULL if not found.
 * @param   pszFilename    The filename to check.
 */
static PVDPLUGIN vdPluginFind(const char *pszFilename)
{
    PVDPLUGIN pIt;
    RTListForEach(&g_ListPluginsLoaded, pIt, VDPLUGIN, NodePlugin)
    {
        if (!RTStrCmp(pIt->pszFilename, pszFilename))
            return pIt;
    }

    return NULL;
}

/**
 * Adds a plugin to the list of loaded plugins.
 *
 * @returns VBox status code.
 * @param   hPlugin     Plugin handle to add.
 * @param   pszFilename The associated filename, used for finding duplicates.
 */
static int vdAddPlugin(RTLDRMOD hPlugin, const char *pszFilename)
{
    int rc = VINF_SUCCESS;
    PVDPLUGIN pPlugin = (PVDPLUGIN)RTMemAllocZ(sizeof(VDPLUGIN));

    if (pPlugin)
    {
        pPlugin->hPlugin = hPlugin;
        pPlugin->pszFilename = RTStrDup(pszFilename);
        if (pPlugin->pszFilename)
            RTListAppend(&g_ListPluginsLoaded, &pPlugin->NodePlugin);
        else
        {
            RTMemFree(pPlugin);
            rc = VERR_NO_MEMORY;
        }
    }
    else
        rc = VERR_NO_MEMORY;

    return rc;
}

/**
 * Removes a single plugin given by the filename.
 *
 * @returns VBox status code.
 * @param   pszFilename     The plugin filename to remove.
 */
static int vdRemovePlugin(const char *pszFilename)
{
    /* Find plugin to be removed from the list. */
    PVDPLUGIN pIt = vdPluginFind(pszFilename);
    if (!pIt)
        return VINF_SUCCESS;

    /** @todo r=klaus: need to add a plugin entry point for unregistering the
     * backends. Only if this doesn't exist (or fails to work) we should fall
     * back to the following uncoordinated backend cleanup. */
    for (unsigned i = 0; i < g_cBackends; i++)
    {
        while (i < g_cBackends && g_ahBackendPlugins[i] == pIt->hPlugin)
        {
            memmove(&g_apBackends[i], &g_apBackends[i + 1], (g_cBackends - i - 1) * sizeof(PCVDIMAGEBACKEND));
            memmove(&g_ahBackendPlugins[i], &g_ahBackendPlugins[i + 1], (g_cBackends - i - 1) * sizeof(RTLDRMOD));
            /** @todo for now skip reallocating, doesn't save much */
            g_cBackends--;
        }
    }
    for (unsigned i = 0; i < g_cCacheBackends; i++)
    {
        while (i < g_cCacheBackends && g_ahCacheBackendPlugins[i] == pIt->hPlugin)
        {
            memmove(&g_apCacheBackends[i], &g_apCacheBackends[i + 1], (g_cCacheBackends - i - 1) * sizeof(PCVDCACHEBACKEND));
            memmove(&g_ahCacheBackendPlugins[i], &g_ahCacheBackendPlugins[i + 1], (g_cCacheBackends - i - 1) * sizeof(RTLDRMOD));
            /** @todo for now skip reallocating, doesn't save much */
            g_cCacheBackends--;
        }
    }
    for (unsigned i = 0; i < g_cFilterBackends; i++)
    {
        while (i < g_cFilterBackends && g_pahFilterBackendPlugins[i] == pIt->hPlugin)
        {
            memmove(&g_apFilterBackends[i], &g_apFilterBackends[i + 1], (g_cFilterBackends - i - 1) * sizeof(PCVDFILTERBACKEND));
            memmove(&g_pahFilterBackendPlugins[i], &g_pahFilterBackendPlugins[i + 1], (g_cFilterBackends - i - 1) * sizeof(RTLDRMOD));
            /** @todo for now skip reallocating, doesn't save much */
            g_cFilterBackends--;
        }
    }

    /* Remove the plugin node now, all traces of it are gone. */
    RTListNodeRemove(&pIt->NodePlugin);
    RTLdrClose(pIt->hPlugin);
    RTStrFree(pIt->pszFilename);
    RTMemFree(pIt);

    return VINF_SUCCESS;
}

#endif /* VBOX_HDD_NO_DYNAMIC_BACKENDS*/

/**
 * Returns the number of known image format backends.
 *
 * @returns Number of image formats known.
 */
DECLHIDDEN(uint32_t) vdGetImageBackendCount(void)
{
    return g_cBackends;
}


/**
 * Queries a image backend descriptor by the index.
 *
 * @returns VBox status code.
 * @param   idx             The index of the backend to query.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdQueryImageBackend(uint32_t idx, PCVDIMAGEBACKEND *ppBackend)
{
    if (idx >= g_cBackends)
        return VERR_OUT_OF_RANGE;

    *ppBackend = g_apBackends[idx];
    return VINF_SUCCESS;
}


/**
 * Returns the image backend descriptor matching the given identifier if known.
 *
 * @returns VBox status code.
 * @param   pszBackend      The backend identifier to look for.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdFindImageBackend(const char *pszBackend, PCVDIMAGEBACKEND *ppBackend)
{
    int rc = VERR_NOT_FOUND;
    PCVDIMAGEBACKEND pBackend = NULL;

    if (!g_apBackends)
        VDInit();

    for (unsigned i = 0; i < g_cBackends; i++)
    {
        if (!RTStrICmp(pszBackend, g_apBackends[i]->pszBackendName))
        {
            pBackend = g_apBackends[i];
            rc = VINF_SUCCESS;
            break;
        }
    }
    *ppBackend = pBackend;
    return rc;
}

/**
 * Returns the number of known cache format backends.
 *
 * @returns Number of image formats known.
 */
DECLHIDDEN(uint32_t) vdGetCacheBackendCount(void)
{
    return g_cCacheBackends;
}


/**
 * Queries a cache backend descriptor by the index.
 *
 * @returns VBox status code.
 * @param   idx             The index of the backend to query.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdQueryCacheBackend(uint32_t idx, PCVDCACHEBACKEND *ppBackend)
{
    if (idx >= g_cCacheBackends)
        return VERR_OUT_OF_RANGE;

    *ppBackend = g_apCacheBackends[idx];
    return VINF_SUCCESS;
}


/**
 * Returns the cache backend descriptor matching the given identifier if known.
 *
 * @returns VBox status code.
 * @param   pszBackend      The backend identifier to look for.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdFindCacheBackend(const char *pszBackend, PCVDCACHEBACKEND *ppBackend)
{
    int rc = VERR_NOT_FOUND;
    PCVDCACHEBACKEND pBackend = NULL;

    if (!g_apCacheBackends)
        VDInit();

    for (unsigned i = 0; i < g_cCacheBackends; i++)
    {
        if (!RTStrICmp(pszBackend, g_apCacheBackends[i]->pszBackendName))
        {
            pBackend = g_apCacheBackends[i];
            rc = VINF_SUCCESS;
            break;
        }
    }
    *ppBackend = pBackend;
    return rc;
}


/**
 * Returns the number of known filter backends.
 *
 * @returns Number of image formats known.
 */
DECLHIDDEN(uint32_t) vdGetFilterBackendCount(void)
{
    return g_cFilterBackends;
}


/**
 * Queries a filter backend descriptor by the index.
 *
 * @returns VBox status code.
 * @param   idx             The index of the backend to query.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdQueryFilterBackend(uint32_t idx, PCVDFILTERBACKEND *ppBackend)
{
    if (idx >= g_cFilterBackends)
        return VERR_OUT_OF_RANGE;

    *ppBackend = g_apFilterBackends[idx];
    return VINF_SUCCESS;
}


/**
 * Returns the filter backend descriptor matching the given identifier if known.
 *
 * @returns VBox status code.
 * @param   pszFilter       The filter identifier to look for.
 * @param   ppBackend       Where to store the pointer to the backend descriptor on success.
 */
DECLHIDDEN(int) vdFindFilterBackend(const char *pszFilter, PCVDFILTERBACKEND *ppBackend)
{
    int rc = VERR_NOT_FOUND;
    PCVDFILTERBACKEND pBackend = NULL;

    for (unsigned i = 0; i < g_cFilterBackends; i++)
    {
        if (!RTStrICmp(pszFilter, g_apFilterBackends[i]->pszBackendName))
        {
            pBackend = g_apFilterBackends[i];
            rc = VINF_SUCCESS;
            break;
        }
    }
    *ppBackend = pBackend;
    return rc;
}


/**
 * Worker for VDPluginLoadFromFilename() and vdPluginLoadFromPath().
 *
 * @returns VBox status code.
 * @param   pszFilename    The plugin filename to load.
 */
DECLHIDDEN(int) vdPluginLoadFromFilename(const char *pszFilename)
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    /* Plugin loaded? Nothing to do. */
    if (vdPluginFind(pszFilename))
    {
        LogFlowFunc(("Plugin '%s' already loaded\n", pszFilename));
        return VINF_SUCCESS;
    }

    RTLDRMOD hPlugin = NIL_RTLDRMOD;
    int rc = SUPR3HardenedLdrLoadPlugIn(pszFilename, &hPlugin, NULL);
    LogFlowFunc(("SUPR3HardenedLdrLoadPlugIn('%s') -> %Rrc\n", pszFilename, rc));
    if (RT_SUCCESS(rc))
    {
        VDBACKENDREGISTER BackendRegister;
        PFNVDPLUGINLOAD pfnVDPluginLoad = NULL;

        BackendRegister.u32Version        = VD_BACKENDREG_CB_VERSION;
        BackendRegister.pfnRegisterImage  = vdPluginRegisterImage;
        BackendRegister.pfnRegisterCache  = vdPluginRegisterCache;
        BackendRegister.pfnRegisterFilter = vdPluginRegisterFilter;

        rc = RTLdrGetSymbol(hPlugin, VD_PLUGIN_LOAD_NAME, (void**)&pfnVDPluginLoad);
        if (RT_FAILURE(rc) || !pfnVDPluginLoad)
        {
            LogFunc(("error resolving the entry point %s in plugin %s, rc=%Rrc, pfnVDPluginLoad=%#p\n",
                     VD_PLUGIN_LOAD_NAME, pszFilename, rc, pfnVDPluginLoad));
            if (RT_SUCCESS(rc))
                rc = VERR_SYMBOL_NOT_FOUND;
        }

        if (RT_SUCCESS(rc))
        {
            /* Get the function table. */
            rc = pfnVDPluginLoad(hPlugin, &BackendRegister);
        }
        else
            LogFunc(("ignored plugin '%s': rc=%Rrc\n", pszFilename, rc));

        /* Create a plugin entry on success. */
        if (RT_SUCCESS(rc))
            vdAddPlugin(hPlugin, pszFilename);
        else
            RTLdrClose(hPlugin);
    }

    return rc;
#else
    RT_NOREF1(pszFilename);
    return VERR_NOT_IMPLEMENTED;
#endif
}

/**
 * Worker for VDPluginLoadFromPath() and vdLoadDynamicBackends().
 *
 * @returns VBox status code.
 * @param   pszPath        The path to load plugins from.
 */
DECLHIDDEN(int) vdPluginLoadFromPath(const char *pszPath)
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    /* To get all entries with VBoxHDD as prefix. */
    char *pszPluginFilter = RTPathJoinA(pszPath, VD_PLUGIN_PREFIX "*");
    if (!pszPluginFilter)
        return VERR_NO_STR_MEMORY;

    PRTDIRENTRYEX pPluginDirEntry = NULL;
    RTDIR hPluginDir;
    size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
    int rc = RTDirOpenFiltered(&hPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0 /*fFlags*/);
    if (RT_SUCCESS(rc))
    {
        pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
        if (pPluginDirEntry)
        {
            while (   (rc = RTDirReadEx(hPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK))
                   != VERR_NO_MORE_FILES)
            {
                char *pszPluginPath = NULL;

                if (rc == VERR_BUFFER_OVERFLOW)
                {
                    /* allocate new buffer. */
                    RTMemFree(pPluginDirEntry);
                    pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
                    if (!pPluginDirEntry)
                    {
                        rc = VERR_NO_MEMORY;
                        break;
                    }
                    /* Retry. */
                    rc = RTDirReadEx(hPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
                    if (RT_FAILURE(rc))
                        break;
                }
                else if (RT_FAILURE(rc))
                    break;

                /* We got the new entry. */
                if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
                    continue;

                /* Prepend the path to the libraries. */
                pszPluginPath = RTPathJoinA(pszPath, pPluginDirEntry->szName);
                if (!pszPluginPath)
                {
                    rc = VERR_NO_STR_MEMORY;
                    break;
                }

                rc = vdPluginLoadFromFilename(pszPluginPath);
                RTStrFree(pszPluginPath);
            }

            RTMemFree(pPluginDirEntry);
        }
        else
            rc = VERR_NO_MEMORY;

        RTDirClose(hPluginDir);
    }
    else
    {
        /* On Windows the above immediately signals that there are no
         * files matching, while on other platforms enumerating the
         * files below fails. Either way: no plugins. */
    }

    if (rc == VERR_NO_MORE_FILES)
        rc = VINF_SUCCESS;
    RTStrFree(pszPluginFilter);
    return rc;
#else
    RT_NOREF1(pszPath);
    return VERR_NOT_IMPLEMENTED;
#endif
}

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
/**
 * internal: scans plugin directory and loads found plugins.
 */
static int vdLoadDynamicBackends(void)
{
    /*
     * Enumerate plugin backends from the application directory where the other
     * shared libraries are.
     */
    char szPath[RTPATH_MAX];
    int rc = RTPathAppPrivateArch(szPath, sizeof(szPath));
    if (RT_FAILURE(rc))
        return rc;

    return vdPluginLoadFromPath(szPath);
}
#endif

/**
 * Worker for VDPluginUnloadFromFilename() and vdPluginUnloadFromPath().
 *
 * @returns VBox status code.
 * @param   pszFilename    The plugin filename to unload.
 */
DECLHIDDEN(int) vdPluginUnloadFromFilename(const char *pszFilename)
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    return vdRemovePlugin(pszFilename);
#else
    RT_NOREF1(pszFilename);
    return VERR_NOT_IMPLEMENTED;
#endif
}

/**
 * Worker for VDPluginUnloadFromPath().
 *
 * @returns VBox status code.
 * @param   pszPath        The path to unload plugins from.
 */
DECLHIDDEN(int) vdPluginUnloadFromPath(const char *pszPath)
{
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    /* To get all entries with VBoxHDD as prefix. */
    char *pszPluginFilter = RTPathJoinA(pszPath, VD_PLUGIN_PREFIX "*");
    if (!pszPluginFilter)
        return VERR_NO_STR_MEMORY;

    PRTDIRENTRYEX pPluginDirEntry = NULL;
    RTDIR hPluginDir;
    size_t cbPluginDirEntry = sizeof(RTDIRENTRYEX);
    int rc = RTDirOpenFiltered(&hPluginDir, pszPluginFilter, RTDIRFILTER_WINNT, 0 /*fFlags*/);
    if (RT_SUCCESS(rc))
    {
        pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(sizeof(RTDIRENTRYEX));
        if (pPluginDirEntry)
        {
            while ((rc = RTDirReadEx(hPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK)) != VERR_NO_MORE_FILES)
            {
                char *pszPluginPath = NULL;

                if (rc == VERR_BUFFER_OVERFLOW)
                {
                    /* allocate new buffer. */
                    RTMemFree(pPluginDirEntry);
                    pPluginDirEntry = (PRTDIRENTRYEX)RTMemAllocZ(cbPluginDirEntry);
                    if (!pPluginDirEntry)
                    {
                        rc = VERR_NO_MEMORY;
                        break;
                    }
                    /* Retry. */
                    rc = RTDirReadEx(hPluginDir, pPluginDirEntry, &cbPluginDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
                    if (RT_FAILURE(rc))
                        break;
                }
                else if (RT_FAILURE(rc))
                    break;

                /* We got the new entry. */
                if (!RTFS_IS_FILE(pPluginDirEntry->Info.Attr.fMode))
                    continue;

                /* Prepend the path to the libraries. */
                pszPluginPath = RTPathJoinA(pszPath, pPluginDirEntry->szName);
                if (!pszPluginPath)
                {
                    rc = VERR_NO_STR_MEMORY;
                    break;
                }

                rc = vdPluginUnloadFromFilename(pszPluginPath);
                RTStrFree(pszPluginPath);
            }

            RTMemFree(pPluginDirEntry);
        }
        else
            rc = VERR_NO_MEMORY;

        RTDirClose(hPluginDir);
    }
    else
    {
        /* On Windows the above immediately signals that there are no
         * files matching, while on other platforms enumerating the
         * files below fails. Either way: no plugins. */
    }

    if (rc == VERR_NO_MORE_FILES)
        rc = VINF_SUCCESS;
    RTStrFree(pszPluginFilter);
    return rc;
#else
    RT_NOREF1(pszPath);
    return VERR_NOT_IMPLEMENTED;
#endif
}


/**
 * Initializes the plugin state to be able to load further plugins and populates
 * the backend lists with the compiled in backends.
 *
 * @returns VBox status code.
 */
DECLHIDDEN(int) vdPluginInit(void)
{
    int rc = vdAddBackends(NIL_RTLDRMOD, aStaticBackends, RT_ELEMENTS(aStaticBackends));
    if (RT_SUCCESS(rc))
    {
        rc = vdAddCacheBackends(NIL_RTLDRMOD, aStaticCacheBackends, RT_ELEMENTS(aStaticCacheBackends));
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
        if (RT_SUCCESS(rc))
        {
            RTListInit(&g_ListPluginsLoaded);
            rc = vdLoadDynamicBackends();
        }
#endif
    }

    return rc;
}


/**
 * Tears down the plugin related state.
 *
 * @returns VBox status code.
 */
DECLHIDDEN(int) vdPluginTerm(void)
{
    if (!g_apBackends)
        return VERR_INTERNAL_ERROR;

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    if (g_pahFilterBackendPlugins)
        RTMemFree(g_pahFilterBackendPlugins);
#endif
    if (g_apFilterBackends)
        RTMemFree(g_apFilterBackends);
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    if (g_ahCacheBackendPlugins)
        RTMemFree(g_ahCacheBackendPlugins);
#endif
    if (g_apCacheBackends)
        RTMemFree(g_apCacheBackends);
    RTMemFree(g_apBackends);

    g_cBackends = 0;
    g_apBackends = NULL;

    /* Clear the supported cache backends. */
    g_cCacheBackends = 0;
    g_apCacheBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    g_ahCacheBackendPlugins = NULL;
#endif

    /* Clear the supported filter backends. */
    g_cFilterBackends = 0;
    g_apFilterBackends = NULL;
#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    g_pahFilterBackendPlugins = NULL;
#endif

#ifndef VBOX_HDD_NO_DYNAMIC_BACKENDS
    PVDPLUGIN pPlugin, pPluginNext;
    RTListForEachSafe(&g_ListPluginsLoaded, pPlugin, pPluginNext, VDPLUGIN, NodePlugin)
    {
        RTLdrClose(pPlugin->hPlugin);
        RTStrFree(pPlugin->pszFilename);
        RTListNodeRemove(&pPlugin->NodePlugin);
        RTMemFree(pPlugin);
    }
#endif

    return VINF_SUCCESS;
}


/**
 * Returns whether the plugin related state is initialized.
 *
 * @returns true if the plugin state is initialized and plugins can be loaded,
 *          false otherwise.
 */
DECLHIDDEN(bool) vdPluginIsInitialized(void)
{
    return g_apBackends != NULL;
}