@@ -481,6 +481,7 @@ int ImageFile::init_image_file() {
481481 goto ERROR_EXIT;
482482 }
483483
484+ // have only RO layers
484485 if (upper.index () == " " || upper.data () == " " ) {
485486 LOG_INFO (" RW layer path not set. return RO layers." );
486487 m_file = lower_file;
@@ -493,29 +494,36 @@ int ImageFile::init_image_file() {
493494 LOG_ERROR (" open upper layer failed." );
494495 goto ERROR_EXIT;
495496 }
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.
501509 //
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 .
504512 stack_ret = LSMT::stack_files (upper_file, lower_file, false , false );
505513 if (!stack_ret) {
506514 LOG_ERROR (" LSMT::stack_files(`, `)" , (uint64_t )upper_file, true );
507515 goto ERROR_EXIT;
508516 }
509517 m_file = stack_ret;
510518 read_only = false ;
511- delete upper_file;
512519 m_lower_file = lower_file;
520+ m_upper_file = upper_file;
513521
514522SUCCESS_EXIT:
515523 if (conf.download ().enable () && !record_no_download) {
516524 start_bk_dl_thread ();
517525 }
518- if (m_prefetcher != nullptr ) {
526+ if (m_prefetcher && lower_file ) {
519527 m_prefetcher->replay (lower_file);
520528 }
521529 return 1 ;
0 commit comments