Skip to content

Commit

Permalink
rbd: Use assume_storage_prezeroed when formatting
Browse files Browse the repository at this point in the history
Instead of passing lazy_itable_init=1 and lazy_journal_init=1 to
mkfs.ext4, pass assume_storage_prezeroed=1 which is
stronger and allows the filesystem to skip inode table zeroing
completely instead of simply doing it lazily.

The support for this flag is checked by trying to format a fake
temporary image with mkfs.ext4 and checking its STDERR.

Closes: ceph#4948

Signed-off-by: Niraj Yadav <[email protected]>
  • Loading branch information
black-dragon74 committed Jan 8, 2025
1 parent 630c97a commit 3d4e46b
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions internal/rbd/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const (
xfsReflinkNoSupport
xfsReflinkSupport

ext4PrezeroedUnset
ext4PrezeroedNoSupport
ext4PrezeroedSupport

staticVol = "staticVolume"
volHealerCtx = "volumeHealerContext"
tryOtherMounters = "tryOtherMounters"
Expand Down Expand Up @@ -100,6 +104,10 @@ var (
// checking the support for reflink.
xfsHasReflink = xfsReflinkUnset

// ext4HasPrezeroed is set by ext4SupportsPrezeroed(), use the function when
// checking the support for assume_storage_prezeroed.
ext4HasPrezeroedSupport = ext4PrezeroedUnset

mkfsDefaultArgs = map[string][]string{
"ext4": {"-m0", "-Enodiscard,lazy_itable_init=1,lazy_journal_init=1"},
"xfs": {"-K"},
Expand Down Expand Up @@ -748,6 +756,7 @@ func (ns *NodeServer) NodePublishVolume(
return &csi.NodePublishVolumeResponse{}, nil
}

//nolint:cyclop,gocyclo // function is required to have multiple conditional checks
func (ns *NodeServer) mountVolumeToStagePath(
ctx context.Context,
req *csi.NodeStageVolumeRequest,
Expand Down Expand Up @@ -792,8 +801,12 @@ func (ns *NodeServer) mountVolumeToStagePath(
readOnly = true
}

//nolint:nestif // readability
if existingFormat == "" && !staticVol && !readOnly && !isBlock {
args := mkfsDefaultArgs[fsType]
if fsType == "ext4" && ns.ext4SupportsPrezeroed() {
args = []string{"-m0", "-Enodiscard,assume_storage_prezeroed=1"}
}

// if the VolumeContext contains "mkfsOptions", use those as args instead
volumeCtx := req.GetVolumeContext()
Expand Down Expand Up @@ -1314,6 +1327,55 @@ func (ns *NodeServer) xfsSupportsReflink() bool {
return false
}

// ext4SupportsPrezeroed checks if the ext4 filesystem supports the
// "assume_storage_prezeroed" option. It does this by creating a temporary
// image file, attempting to format it with the ext4 filesystem using the
// "assume_storage_prezeroed" option, and checking the output for errors.
func (ns *NodeServer) ext4SupportsPrezeroed() bool {
if ext4HasPrezeroedSupport != ext4PrezeroedUnset {
return ext4HasPrezeroedSupport == ext4PrezeroedSupport
}

ctx := context.TODO()
tempImgFile, err := os.CreateTemp(os.TempDir(), "prezeroed.img")
if err != nil {
log.WarningLog(ctx, "failed to create temporary image file: %v", err)

return false
}
defer os.Remove(tempImgFile.Name())

if err = createSparseFile(tempImgFile, 1); err != nil {
log.WarningLog(ctx, "failed to create sparse file: %v", err)

return false
}

diskMounter := &mount.SafeFormatAndMount{Interface: ns.Mounter, Exec: utilexec.New()}

// `-n` Causes mke2fs to not actually create a file system
// but display what it would do if it were to create a file system.
out, err := diskMounter.Exec.Command(
"mkfs.ext4",
"-n",
"-Enodiscard,assume_storage_prezeroed=1",
tempImgFile.Name(),
).CombinedOutput()
if err != nil {
if strings.Contains(string(out), "Bad option(s) specified") {
ext4HasPrezeroedSupport = ext4PrezeroedNoSupport
}

// if the error is not due to the missing option, we can't be sure
// if the option is supported or not, return false optimistically
return false
}

ext4HasPrezeroedSupport = ext4PrezeroedSupport

return true
}

// NodeGetVolumeStats returns volume stats.
func (ns *NodeServer) NodeGetVolumeStats(
ctx context.Context,
Expand Down Expand Up @@ -1397,3 +1459,20 @@ func getDeviceSize(ctx context.Context, devicePath string) (uint64, error) {

return size, nil
}

// createSpareFile makes `file` a sparse file of size `sizeMB`.
func createSparseFile(file *os.File, sizeMB int64) error {
sizeBytes := sizeMB * 1024 * 1024

// seek to the end of the file.
if _, err := file.Seek(sizeBytes-1, 0); err != nil {
return fmt.Errorf("failed to seek to the end of the file: %w", err)
}

// write a single byte, effectively making it a sparse file.
if _, err := file.Write([]byte{0}); err != nil {
return fmt.Errorf("failed to write to the end of the file: %w", err)
}

return nil
}

0 comments on commit 3d4e46b

Please sign in to comment.