-
Notifications
You must be signed in to change notification settings - Fork 135
storage: fsync staging directory before atomic rename #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
asahay19
wants to merge
1
commit into
podman-container-tools:main
Choose a base branch
from
asahay19:asahay-storage-fsync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| //go:build linux | ||
|
|
||
| package ioutils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // SyncDirectoryContents flushes file data and directory metadata under dir to | ||
| // physical storage. Call this before atomically renaming a fully populated | ||
| // staging directory to its final location. | ||
| func SyncDirectoryContents(dir string) error { | ||
| var dirs []string | ||
|
|
||
| err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, walkErr error) error { | ||
| if walkErr != nil { | ||
| return walkErr | ||
| } | ||
| if d.IsDir() { | ||
| dirs = append(dirs, path) | ||
| return nil | ||
| } | ||
|
|
||
| // Only regular files have data worth fdatasync-ing. Symlinks, | ||
| // device nodes, FIFOs, and sockets carry no separate file | ||
| // content: their state lives entirely in directory-entry / | ||
| // inode metadata, which is already covered once the parent | ||
| // directory is fsync'd below. Treating them like regular | ||
| // files is actively wrong: os.Open on a symlink dereferences | ||
| // it, so a symlink whose target does not happen to resolve | ||
| // from inside the staging tree (e.g. Debian's | ||
| // /etc/alternatives/*, or any link into a lower/not-yet- | ||
| // materialized layer) fails the whole sync with ENOENT, and | ||
| // opening a FIFO can block indefinitely waiting for a reader. | ||
| if !d.Type().IsRegular() { | ||
| return nil | ||
| } | ||
|
|
||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| syncErr := unix.Fdatasync(int(f.Fd())) | ||
| closeErr := f.Close() | ||
| if syncErr != nil { | ||
| return syncErr | ||
| } | ||
|
|
||
| return closeErr | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("sync directory contents in %q: %w", dir, err) | ||
| } | ||
|
|
||
| for i := len(dirs) - 1; i >= 0; i-- { | ||
| dfd, err := os.Open(dirs[i]) | ||
| if err != nil { | ||
| return fmt.Errorf("open directory %q for sync: %w", dirs[i], err) | ||
| } | ||
|
|
||
| syncErr := unix.Fsync(int(dfd.Fd())) | ||
| closeErr := dfd.Close() | ||
| if syncErr != nil { | ||
| return fmt.Errorf("sync directory %q: %w", dirs[i], syncErr) | ||
| } | ||
| if closeErr != nil { | ||
| return fmt.Errorf("close directory %q after sync: %w", dirs[i], closeErr) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //go:build linux | ||
|
|
||
| package ioutils | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSyncDirectoryContents(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| nested := filepath.Join(dir, "nested") | ||
| if err := os.MkdirAll(nested, 0o755); err != nil { | ||
| t.Fatalf("mkdir nested: %v", err) | ||
| } | ||
|
|
||
| files := []string{ | ||
| filepath.Join(dir, "file1"), | ||
| filepath.Join(nested, "file2"), | ||
| } | ||
| for _, file := range files { | ||
| if err := os.WriteFile(file, []byte("storage-resilience"), 0o644); err != nil { | ||
| t.Fatalf("write file %q: %v", file, err) | ||
| } | ||
| } | ||
|
|
||
| if err := SyncDirectoryContents(dir); err != nil { | ||
| t.Fatalf("SyncDirectoryContents: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // TestSyncDirectoryContentsDanglingSymlink reproduces the failure this test | ||
| // was written after observing in practice: pulling quay.io/crio/nginx | ||
| // through the partial-pull/staging path failed with | ||
| // | ||
| // sync directory contents in ".../overlay/staging/.../dir": open | ||
| // .../dir/etc/alternatives/awk.1.gz: no such file or directory | ||
| // | ||
| // Debian-based images populate /etc/alternatives with symlinks, some of | ||
| // which point outside the layer's own diff (e.g. into a lower layer, or a | ||
| // path never materialized in this staging directory at all). os.Open | ||
| // dereferences symlinks, so walking the tree and opening every non-dir | ||
| // entry -- including symlinks -- fails with ENOENT on any such link. A | ||
| // symlink has no file content of its own to fdatasync; its target string | ||
| // is directory-entry metadata covered by fsync-ing the parent directory. | ||
| func TestSyncDirectoryContentsDanglingSymlink(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| if err := os.WriteFile(filepath.Join(dir, "real-file"), []byte("data"), 0o644); err != nil { | ||
| t.Fatalf("write real-file: %v", err) | ||
| } | ||
|
|
||
| // Points at a target that does not exist anywhere on disk, exactly | ||
| // like a symlink whose target lives in a different layer than the | ||
| // one currently staged. | ||
| dangling := filepath.Join(dir, "awk.1.gz") | ||
| if err := os.Symlink("/nonexistent/mawk.1.gz", dangling); err != nil { | ||
| t.Fatalf("create dangling symlink: %v", err) | ||
| } | ||
|
|
||
| if err := SyncDirectoryContents(dir); err != nil { | ||
| t.Fatalf("SyncDirectoryContents should skip symlinks instead of dereferencing them: %v", err) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What documents that
fsyncon a directory also syncs all nodes of its children?