@@ -481,6 +481,7 @@ int ImageFile::init_image_file() {
481
481
goto ERROR_EXIT;
482
482
}
483
483
484
+ // have only RO layers
484
485
if (upper.index () == " " || upper.data () == " " ) {
485
486
LOG_INFO (" RW layer path not set. return RO layers." );
486
487
m_file = lower_file;
@@ -493,29 +494,36 @@ int ImageFile::init_image_file() {
493
494
LOG_ERROR (" open upper layer failed." );
494
495
goto ERROR_EXIT;
495
496
}
496
- // We have to maintain the lower_file because prefetcher need the readonly
497
- // lower_file but not the writable m_file.
498
- //
499
- // Otherwise, lower_file and upper_file will lose the ownership of m_files
500
- // after stack_files(...), so we could safely delete them.
497
+
498
+ // have only one RW layer
499
+ if (!lower_file) {
500
+ LOG_INFO (" RO layers path not set. return RW layer." );
501
+ m_file = upper_file;
502
+ read_only = false ;
503
+ goto SUCCESS_EXIT;
504
+ }
505
+
506
+ // stack_files(..., ownership=true) will destruct lower_file and upper_file
507
+ // immediately, but the read-only lower_file is needed by prefetcher, so we
508
+ // have to use stack_files(..., ownership=false) instead.
501
509
//
502
- // upper_file will be deleted immediately since it's useless.
503
- // lower_file will be held by ImageFile until deconstruct .
510
+ // For this reason, lower_file and upper_file must be maintained until m_file
511
+ // is deconstructed .
504
512
stack_ret = LSMT::stack_files (upper_file, lower_file, false , false );
505
513
if (!stack_ret) {
506
514
LOG_ERROR (" LSMT::stack_files(`, `)" , (uint64_t )upper_file, true );
507
515
goto ERROR_EXIT;
508
516
}
509
517
m_file = stack_ret;
510
518
read_only = false ;
511
- delete upper_file;
512
519
m_lower_file = lower_file;
520
+ m_upper_file = upper_file;
513
521
514
522
SUCCESS_EXIT:
515
523
if (conf.download ().enable () && !record_no_download) {
516
524
start_bk_dl_thread ();
517
525
}
518
- if (m_prefetcher != nullptr ) {
526
+ if (m_prefetcher && lower_file ) {
519
527
m_prefetcher->replay (lower_file);
520
528
}
521
529
return 1 ;
0 commit comments