12
12
13
13
#include " file/filename.h"
14
14
#include " port/port.h"
15
+ #include " test_util/sync_point.h"
15
16
16
17
namespace ROCKSDB_NAMESPACE {
17
18
namespace encryption {
@@ -267,6 +268,17 @@ Status KeyManagedEncryptedEnv::NewSequentialFile(
267
268
case EncryptionMethod::kAES192_CTR :
268
269
case EncryptionMethod::kAES256_CTR :
269
270
s = encrypted_env_->NewSequentialFile (fname, result, options);
271
+ // Hack: when upgrading from TiKV <= v5.0.0-rc, the old current
272
+ // file is encrypted but it could be replaced with a plaintext
273
+ // current file. The operation below guarantee that the current
274
+ // file is read correctly.
275
+ if (s.ok () && IsCurrentFile (fname)) {
276
+ if (!IsValidCurrentFile (std::move (*result))) {
277
+ s = target ()->NewSequentialFile (fname, result, options);
278
+ } else {
279
+ s = encrypted_env_->NewSequentialFile (fname, result, options);
280
+ }
281
+ }
270
282
break ;
271
283
default :
272
284
s = Status::InvalidArgument (
@@ -306,7 +318,8 @@ Status KeyManagedEncryptedEnv::NewWritableFile(
306
318
const EnvOptions& options) {
307
319
FileEncryptionInfo file_info;
308
320
Status s;
309
- bool skipped = ShouldSkipEncryption (fname);
321
+ bool skipped = IsCurrentFile (fname);
322
+ TEST_SYNC_POINT_CALLBACK (" KeyManagedEncryptedEnv::NewWritableFile" , &skipped);
310
323
if (!skipped) {
311
324
s = key_manager_->NewFile (fname, &file_info);
312
325
if (!s.ok ()) {
@@ -441,12 +454,12 @@ Status KeyManagedEncryptedEnv::DeleteFile(const std::string& fname) {
441
454
442
455
Status KeyManagedEncryptedEnv::LinkFile (const std::string& src_fname,
443
456
const std::string& dst_fname) {
444
- if (ShouldSkipEncryption (dst_fname)) {
445
- assert (ShouldSkipEncryption (src_fname));
457
+ if (IsCurrentFile (dst_fname)) {
458
+ assert (IsCurrentFile (src_fname));
446
459
Status s = target ()->LinkFile (src_fname, dst_fname);
447
460
return s;
448
461
} else {
449
- assert (!ShouldSkipEncryption (src_fname));
462
+ assert (!IsCurrentFile (src_fname));
450
463
}
451
464
Status s = key_manager_->LinkFile (src_fname, dst_fname);
452
465
if (!s.ok ()) {
@@ -463,11 +476,17 @@ Status KeyManagedEncryptedEnv::LinkFile(const std::string& src_fname,
463
476
464
477
Status KeyManagedEncryptedEnv::RenameFile (const std::string& src_fname,
465
478
const std::string& dst_fname) {
466
- if (ShouldSkipEncryption (dst_fname)) {
467
- assert (ShouldSkipEncryption (src_fname));
468
- return target ()->RenameFile (src_fname, dst_fname);
479
+ if (IsCurrentFile (dst_fname)) {
480
+ assert (IsCurrentFile (src_fname));
481
+ Status s = target ()->RenameFile (src_fname, dst_fname);
482
+ // Replacing with plaintext requires deleting the info in the key manager.
483
+ // The stale current file info exists when upgrading from TiKV <= v5.0.0-rc.
484
+ Status delete_status __attribute__ ((__unused__)) =
485
+ key_manager_->DeleteFile (dst_fname);
486
+ assert (delete_status.ok ());
487
+ return s;
469
488
} else {
470
- assert (!ShouldSkipEncryption (src_fname));
489
+ assert (!IsCurrentFile (src_fname));
471
490
}
472
491
// Link(copy)File instead of RenameFile to avoid losing src_fname info when
473
492
// failed to rename the src_fname in the file system.
0 commit comments