@@ -20,6 +20,11 @@ static LISTP_TYPE(libos_encrypted_files_key) g_keys = LISTP_INIT;
20
20
/* Protects the `g_keys` list, but also individual keys, since they can be updated */
21
21
static struct libos_lock g_keys_lock ;
22
22
23
+ static LISTP_TYPE (libos_encrypted_volume ) g_volumes = LISTP_INIT ;
24
+
25
+ /* Protects the `g_volumes` list. */
26
+ static struct libos_lock g_volumes_lock ;
27
+
23
28
static pf_status_t cb_read (pf_handle_t handle , void * buffer , uint64_t offset , size_t size ) {
24
29
PAL_HANDLE pal_handle = (PAL_HANDLE )handle ;
25
30
@@ -370,6 +375,8 @@ int init_encrypted_files(void) {
370
375
#endif
371
376
if (!create_lock (& g_keys_lock ))
372
377
return - ENOMEM ;
378
+ if (!create_lock (& g_volumes_lock ))
379
+ return - ENOMEM ;
373
380
374
381
pf_set_callbacks (& cb_read , & cb_write , & cb_fsync , & cb_truncate ,
375
382
& cb_aes_cmac , & cb_aes_gcm_encrypt , & cb_aes_gcm_decrypt ,
@@ -532,6 +539,86 @@ void update_encrypted_files_key(struct libos_encrypted_files_key* key, const pf_
532
539
unlock (& g_keys_lock );
533
540
}
534
541
542
+ static struct libos_encrypted_volume * get_volume (const char * mount_point_path ) {
543
+ assert (locked (& g_volumes_lock ));
544
+
545
+ struct libos_encrypted_volume * volume ;
546
+ LISTP_FOR_EACH_ENTRY (volume , & g_volumes , list ) {
547
+ if (!strcmp (volume -> mount_point_path , mount_point_path )) {
548
+ return volume ;
549
+ }
550
+ }
551
+
552
+ return NULL ;
553
+ }
554
+
555
+ static struct libos_encrypted_volume * get_or_create_volume (const char * mount_point_path ,
556
+ bool * out_created ) {
557
+ assert (locked (& g_volumes_lock ));
558
+
559
+ struct libos_encrypted_volume * volume = get_volume (mount_point_path );
560
+ if (volume ) {
561
+ * out_created = false;
562
+ return volume ;
563
+ }
564
+
565
+ volume = calloc (1 , sizeof (* volume ));
566
+ if (!volume )
567
+ return NULL ;
568
+ volume -> mount_point_path = strdup (mount_point_path );
569
+ if (!volume -> mount_point_path ) {
570
+ free (volume );
571
+ return NULL ;
572
+ }
573
+ LISTP_ADD_TAIL (volume , & g_volumes , list );
574
+ * out_created = true;
575
+ return volume ;
576
+ }
577
+
578
+ int get_or_create_encrypted_volume (const char * mount_point_path ,
579
+ struct libos_encrypted_volume * * out_volume , bool * out_created ) {
580
+ lock (& g_volumes_lock );
581
+
582
+ int ret ;
583
+
584
+ struct libos_encrypted_volume * volume = get_or_create_volume (mount_point_path , out_created );
585
+ if (!volume ) {
586
+ ret = - ENOMEM ;
587
+ goto out ;
588
+ }
589
+
590
+ * out_volume = volume ;
591
+ ret = 0 ;
592
+ out :
593
+ unlock (& g_volumes_lock );
594
+ return ret ;
595
+ }
596
+
597
+ struct libos_encrypted_volume * get_encrypted_volume (const char * mount_point_path ) {
598
+ lock (& g_volumes_lock );
599
+ struct libos_encrypted_volume * volume = get_volume (mount_point_path );
600
+ unlock (& g_volumes_lock );
601
+ return volume ;
602
+ }
603
+
604
+ int list_encrypted_volumes (int (* callback )(struct libos_encrypted_volume * volume , void * arg ),
605
+ void * arg ) {
606
+ lock (& g_volumes_lock );
607
+
608
+ int ret ;
609
+
610
+ struct libos_encrypted_volume * volume ;
611
+ LISTP_FOR_EACH_ENTRY (volume , & g_volumes , list ) {
612
+ ret = callback (volume , arg );
613
+ if (ret < 0 )
614
+ goto out ;
615
+ }
616
+ ret = 0 ;
617
+ out :
618
+ unlock (& g_volumes_lock );
619
+ return ret ;
620
+ }
621
+
535
622
static int encrypted_file_alloc (const char * uri , struct libos_encrypted_volume * volume ,
536
623
struct libos_encrypted_file * * out_enc ) {
537
624
assert (strstartswith (uri , URI_PREFIX_FILE ));
@@ -896,6 +983,21 @@ BEGIN_RS_FUNC(encrypted_files_key) {
896
983
}
897
984
END_RS_FUNC (encrypted_files_key )
898
985
986
+ /* Checkpoint the `g_volumes` list. */
987
+ BEGIN_CP_FUNC (all_encrypted_volumes ) {
988
+ __UNUSED (size );
989
+ __UNUSED (obj );
990
+ __UNUSED (objp );
991
+
992
+ lock (& g_volumes_lock );
993
+ struct libos_encrypted_volume * volume ;
994
+ LISTP_FOR_EACH_ENTRY (volume , & g_volumes , list ) {
995
+ DO_CP (encrypted_volume , volume , /*objp=*/ NULL );
996
+ }
997
+ unlock (& g_volumes_lock );
998
+ }
999
+ END_CP_FUNC_NO_RS (all_encrypted_volumes )
1000
+
899
1001
// TODO (MST): revisit below, probably not correct?!
900
1002
BEGIN_CP_FUNC (encrypted_volume ) {
901
1003
__UNUSED (size );
0 commit comments