Skip to content

Commit

Permalink
Call to btrfs device ready ioctl
Browse files Browse the repository at this point in the history
That helps when we build btrfs RAID0 and fixes following race condition
in tests:

[    1.608465] booster: loading module btrfs
[    1.695250] Btrfs loaded, crc32c=crc32c-intel, zoned=yes, fsverity=yes
[    1.695410] booster: udev event {Action:add KObj:/devices/virtual/misc/btrfs-control Env:map[ACTION:add DEVNAME:btrfs-control DEVPATH:/devices/virtual/misc/btrfs-control MAJOR:10 MINOR:234 SEQNUM:1156 SUBSYSTEM:misc]}
[    1.698089] booster: udev event {Action:add KObj:/module/btrfs Env:map[ACTION:add DEVPATH:/module/btrfs SEQNUM:1157 SUBSYSTEM:module]}
[    1.699492] booster: mounting /dev/sda1->/booster.root, fs=btrfs, flags=0x0, options=
[    1.700392] BTRFS: device fsid 5eaa0c1c-e1dc-4be7-9b03-9f1ed5a87289 devid 1 transid 8 /dev/sda1 scanned by init (129)
[    1.701508] booster: udev event {Action:add KObj:/devices/virtual/bdi/btrfs-1 Env:map[ACTION:add DEVPATH:/devices/virtual/bdi/btrfs-1 SEQNUM:1158 SUBSYSTEM:bdi]}
[    1.703030] BTRFS info (device sda1): using crc32c (crc32c-intel) checksum algorithm
[    1.703796] BTRFS info (device sda1): using free space tree
[    1.704984] BTRFS error (device sda1): devid 2 uuid c815d0ba-796d-4b7c-a5a9-3e6852c6ed0a is missing
[    1.705846] BTRFS error (device sda1): failed to read the system array: -2
[    1.706784] BTRFS error (device sda1): open_ctree failed
[    1.707380] booster: mount(/dev/sda1): invalid argument
[    1.707924] booster: mounting /dev/sda2->/booster.root, fs=btrfs, flags=0x0, options=
[    1.708708] booster: udev event {Action:remove KObj:/devices/virtual/bdi/btrfs-1 Env:map[ACTION:remove DEVPATH:/devices/virtual/bdi/btrfs-1 SEQNUM:1159 SUBSYSTEM:bdi]}
[    1.708780] BTRFS: device fsid 5eaa0c1c-e1dc-4be7-9b03-9f1ed5a87289 devid 2 transid 8 /dev/sda2 scanned by init (129)
[    1.711416] booster: udev event {Action:add KObj:/devices/virtual/bdi/btrfs-2 Env:map[ACTION:add DEVPATH:/devices/virtual/bdi/btrfs-2 SEQNUM:1160 SUBSYSTEM:bdi]}
[    1.711548] BTRFS info (device sda2): using crc32c (crc32c-intel) checksum algorithm
[    1.714017] BTRFS info (device sda2): using free space tree
[    1.715232] BTRFS error (device sda2): devid 1 uuid 58a3e858-0b27-4f7c-8aa5-0dfed774177a is missing
[    1.716119] BTRFS error (device sda2): failed to read the system array: -2
[    1.717021] BTRFS error (device sda2): open_ctree failed
[    1.717599] booster: mount(/dev/sda2): invalid argument
[    1.718186] booster: udev event {Action:remove KObj:/devices/virtual/bdi/btrfs-2 Env:map[ACTION:remove DEVPATH:/devices/virtual/bdi/btrfs-2 SEQNUM:1161 SUBSYSTEM:bdi]}
    btrfs_test.go:17:
        	Error Trace:	/home/anatol/sources/golang/booster/tests/btrfs_test.go:17
        	Error:      	Received unexpected error:
        	            	EOF
        	Test:       	TestBtrfsRaid0
2023/02/28 17:53:15 Got error while waiting for Qemu process completion: signal: killed
--- FAIL: TestBtrfsRaid0 (41.28s)

Issue #194
  • Loading branch information
anatol committed Mar 2, 2023
1 parent b8c0604 commit 8f5c866
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"sync"
"time"
"unsafe"

"github.com/yookoala/realpath"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -368,6 +369,12 @@ func mountRootFs(dev, fstype string) error {
wg := loadModules(fstype)
wg.Wait()

if fstype == "btrfs" {
if err := waitForBtrfsDevicesReady(dev); err != nil {
return err
}
}

rootMountingMutex.Lock()
defer rootMountingMutex.Unlock()

Expand All @@ -390,6 +397,27 @@ func mountRootFs(dev, fstype string) error {
return nil
}

// Wait until all devices of a multiple-device filesystem are scanned and registered within the kernel module
func waitForBtrfsDevicesReady(dev string) error {
controlFile, err := os.OpenFile("/dev/btrfs-control", os.O_RDWR, 0)
if err != nil {
return err
}
defer controlFile.Close()

/* this should be 4k */
var btrfsIoctlVolArgs struct {
fs int64
name [4088]uint8
}

copy(btrfsIoctlVolArgs.name[:], dev)

var BTRFS_IOCTL_MAGIC uintptr = 0x94
BTRFS_IOC_DEVICES_READY := ior(BTRFS_IOCTL_MAGIC, 39, unsafe.Sizeof(btrfsIoctlVolArgs))
return ioctl(controlFile.Fd(), BTRFS_IOC_DEVICES_READY, uintptr(unsafe.Pointer(&btrfsIoctlVolArgs)))
}

func mountFlags() (uintptr, string) {
rootMountFlags, options := sunderMountFlags(rootFlags, rootAutodiscoveryMountFlags)
if rootRo {
Expand Down

0 comments on commit 8f5c866

Please sign in to comment.