From bf5bfe589fb742ec06f6b5444494776040f62d1e Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Tue, 14 Sep 2021 11:34:25 -0700 Subject: [PATCH] xlmeta: Recover corrupted metadata (#13205) When unable to load existing metadata new versions would not be written. This would leave objects in a permanently unrecoverable state Instead, start with clean metadata and write the incoming data. --- cmd/xl-storage.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/xl-storage.go b/cmd/xl-storage.go index 63bcc5448..133d96292 100644 --- a/cmd/xl-storage.go +++ b/cmd/xl-storage.go @@ -991,6 +991,7 @@ func (s *xlStorage) WriteMetadata(ctx context.Context, volume, path string, fi F var xlMeta xlMetaV2 if !isXL2V1Format(buf) { + // This is both legacy and without proper version. err = xlMeta.AddVersion(fi) if err != nil { logger.LogIf(ctx, err) @@ -1006,7 +1007,8 @@ func (s *xlStorage) WriteMetadata(ctx context.Context, volume, path string, fi F } else { if err = xlMeta.Load(buf); err != nil { logger.LogIf(ctx, err) - return err + // Corrupted data, reset and write. + xlMeta = xlMetaV2{} } if err = xlMeta.AddVersion(fi); err != nil { @@ -1982,7 +1984,8 @@ func (s *xlStorage) RenameData(ctx context.Context, srcVolume, srcPath string, f if isXL2V1Format(dstBuf) { if err = xlMeta.Load(dstBuf); err != nil { logger.LogIf(s.ctx, err) - return err + // Data appears corrupt. Drop data. + xlMeta = xlMetaV2{} } } else { // This code-path is to preserve the legacy data. @@ -1990,13 +1993,13 @@ func (s *xlStorage) RenameData(ctx context.Context, srcVolume, srcPath string, f var json = jsoniter.ConfigCompatibleWithStandardLibrary if err := json.Unmarshal(dstBuf, xlMetaLegacy); err != nil { logger.LogIf(s.ctx, err) - return errFileCorrupt + // Data appears corrupt. Drop data. + } else { + if err = xlMeta.AddLegacy(xlMetaLegacy); err != nil { + logger.LogIf(s.ctx, err) + } + legacyPreserved = true } - if err = xlMeta.AddLegacy(xlMetaLegacy); err != nil { - logger.LogIf(s.ctx, err) - return errFileCorrupt - } - legacyPreserved = true } } else { s.RLock()