From 5128942779ffc0ee075929d255bdc02247f90da4 Mon Sep 17 00:00:00 2001 From: Ben McClelland Date: Mon, 30 Sep 2019 10:28:04 -0700 Subject: [PATCH] add statfsmore call to get filesystem mount ids --- c_defs_linux.go | 7 ++++++- examples/statfsmore/main.go | 37 +++++++++++++++++++++++++++++++++++++ scoutfs.go | 31 +++++++++++++++++++++++++++++++ scoutfsdefs.go | 8 ++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 examples/statfsmore/main.go diff --git a/c_defs_linux.go b/c_defs_linux.go index fb8a2c3..3ac2958 100644 --- a/c_defs_linux.go +++ b/c_defs_linux.go @@ -9,7 +9,7 @@ package scoutfs // use this to generate the types for scoutfs: -// go tool cgo -godefs c_defs.go >scoutfsdefs.go +// go tool cgo -godefs c_defs_linux.go >scoutfsdefs.go // #include // #include @@ -46,6 +46,7 @@ package scoutfs // typedef struct scoutfs_ioctl_setattr_more scoutfs_ioctl_setattr_more_t; // typedef struct scoutfs_ioctl_listxattr_hidden scoutfs_ioctl_listxattr_hidden_t; // typedef struct scoutfs_ioctl_find_xattrs scoutfs_ioctl_find_xattrs_t; +// typedef struct scoutfs_ioctl_statfs_more scoutfs_ioctl_statfs_more_t; import "C" const IOCQUERYINODES = C.SCOUTFS_IOC_WALK_INODES @@ -57,6 +58,7 @@ const IOCDATAWAITING = C.SCOUTFS_IOC_DATA_WAITING const IOCSETATTRMORE = C.SCOUTFS_IOC_SETATTR_MORE const IOCLISTXATTRHIDDEN = C.SCOUTFS_IOC_LISTXATTR_HIDDEN const IOCFINDXATTRS = C.SCOUTFS_IOC_FIND_XATTRS +const IOCSTATFSMORE = C.SCOUTFS_IOC_STATFS_MORE const QUERYINODESMETASEQ = C.SCOUTFS_IOC_WALK_INODES_META_SEQ const QUERYINODESDATASEQ = C.SCOUTFS_IOC_WALK_INODES_DATA_SEQ @@ -76,3 +78,6 @@ type dataWaiting C.scoutfs_ioctl_data_waiting_t type setattrMore C.scoutfs_ioctl_setattr_more_t type listXattrHidden C.scoutfs_ioctl_listxattr_hidden_t type findXattrs C.scoutfs_ioctl_find_xattrs_t +type statfsMore C.scoutfs_ioctl_statfs_more_t + +const sizeofstatfsMore = C.sizeof_scoutfs_ioctl_statfs_more_t diff --git a/examples/statfsmore/main.go b/examples/statfsmore/main.go new file mode 100644 index 0000000..e31cb1a --- /dev/null +++ b/examples/statfsmore/main.go @@ -0,0 +1,37 @@ +// Copyright (c) 2018 Versity Software, Inc. +// +// Use of this source code is governed by a BSD-3-Clause license +// that can be found in the LICENSE file in the root of the source +// tree. + +package main + +import ( + "fmt" + "log" + "os" + + scoutfs "github.com/versity/scoutfs-go" +) + +func main() { + if len(os.Args) != 2 || os.Args[1] == "-h" { + fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "") + os.Exit(1) + } + + f, err := os.Open(os.Args[1]) + if err != nil { + log.Fatalf("open %q: %v", os.Args[1], err) + } + defer f.Close() + + id, err := scoutfs.GetIDs(f) + if err != nil { + log.Fatalf("error GetIDs: %v", err) + } + + fmt.Printf("fsid %016x\n", id.FSID) + fmt.Printf("rid %016x\n", id.RandomID) + fmt.Printf("%v\n", id.ShortID) +} diff --git a/scoutfs.go b/scoutfs.go index e3111e9..7f7ae48 100644 --- a/scoutfs.go +++ b/scoutfs.go @@ -9,6 +9,7 @@ package scoutfs import ( "bytes" "encoding/binary" + "fmt" "math" "os" "time" @@ -480,3 +481,33 @@ func bufToStrings(b []byte) []string { } return s } + +// FSID contains the statfs more info for mounted scoutfs filesystem +type FSID struct { + FSID uint64 + RandomID uint64 + ShortID string +} + +// GetIDs gets the statfs more filesystem and random id from file handle within +// scoutfs filesystem +func GetIDs(f *os.File) (FSID, error) { + stfs := statfsMore{Bytes: sizeofstatfsMore} + + _, err := scoutfsctl(f.Fd(), IOCSTATFSMORE, uintptr(unsafe.Pointer(&stfs))) + if err != nil { + return FSID{}, err + } + if stfs.Bytes != sizeofstatfsMore { + return FSID{}, fmt.Errorf("unexpected return size: %v", stfs.Bytes) + } + + short := fmt.Sprintf("f.%v.r.%v", + fmt.Sprintf("%016x", stfs.Fsid)[:][:6], fmt.Sprintf("%016x", stfs.Rid)[:][:6]) + + return FSID{ + FSID: stfs.Fsid, + RandomID: stfs.Rid, + ShortID: short, + }, nil +} diff --git a/scoutfsdefs.go b/scoutfsdefs.go index fa0b76a..746eb63 100644 --- a/scoutfsdefs.go +++ b/scoutfsdefs.go @@ -12,6 +12,7 @@ const IOCDATAWAITING = 0x80287307 const IOCSETATTRMORE = 0x40287308 const IOCLISTXATTRHIDDEN = 0x80187309 const IOCFINDXATTRS = 0x8020730a +const IOCSTATFSMORE = 0x8018730b const QUERYINODESMETASEQ = 0x0 const QUERYINODESDATASEQ = 0x1 @@ -98,3 +99,10 @@ type findXattrs struct { Nr_inodes uint16 X_pad [4]uint8 } +type statfsMore struct { + Bytes uint64 + Fsid uint64 + Rid uint64 +} + +const sizeofstatfsMore = 0x18