Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:coreos/torus
Browse files Browse the repository at this point in the history
  • Loading branch information
barakmich committed Sep 15, 2016
2 parents 04ed92b + 15b77f3 commit 4d22ad3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
15 changes: 13 additions & 2 deletions cmd/torusblk/nbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ var (
Short: "serve a block volume over the NBD protocol",
Run: nbdServeAction,
}
)

var (
serveListenAddress string
detachDevice string
)

func init() {
rootCommand.AddCommand(nbdCommand)
rootCommand.AddCommand(nbdServeCommand)

nbdCommand.Flags().StringVarP(&detachDevice, "detach", "d", "", "detach an NBD device from a block volume. (e.g. torsublk nbd -d /dev/nbd0)")
nbdServeCommand.Flags().StringVarP(&serveListenAddress, "listen", "l", "0.0.0.0:10809", "nbd server listen address")
rootCommand.AddCommand(nbdServeCommand)
}

func nbdAction(cmd *cobra.Command, args []string) {
if len(detachDevice) > 0 && len(args) == 0 {
if err := nbd.Detach(detachDevice); err != nil {
die("failed to detach: %v", err)
}
os.Exit(0)
}

if len(args) != 1 && len(args) != 2 {
cmd.Usage()
os.Exit(1)
Expand Down Expand Up @@ -116,7 +127,7 @@ func connectNBD(srv *torus.Server, f *block.BlockFile, target string, closer cha
fmt.Fprintf(os.Stderr, "error from nbd server: %s\n", err)
os.Exit(1)
}
return handle.Close()
return nil
}

type finder struct {
Expand Down
64 changes: 34 additions & 30 deletions internal/nbd/nbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,51 +218,55 @@ func (nbd *NBD) Serve() error {
}(nbd)
}

waitDisconnect(nbd.nbd)
// NBD_DO_IT does not return until disconnect
if err := ioctl(nbd.nbd.Fd(), ioctlDoIt, 0); err != nil {
clog.Errorf("error %s: ioctl returned %v", nbd.nbd.Name(), err)
}

wg.Wait()
return nil
}

func (nbd *NBD) Close() error {
// FIXME: Now I understand what these did, and it terrifies me.
// syscall.Write(nbd.setsocket, make([]byte, 28))
// syscall.Close(nbd.setsocket)
nbd.Disconnect()
syscall.Close(nbd.socket)
return nbd.nbd.Close()
func Detach(dev string) error {
f, err := os.Open(dev)
if err != nil {
return err
}
n := &NBD{nbd: f}
n.Disconnect()
return nil
}

func (nbd *NBD) Disconnect() error {
func (nbd *NBD) Disconnect() {
var err error
if nbd.nbd == nil {
return nil
return
}
if err := ioctl(nbd.nbd.Fd(), ioctlDisconnect, 0); err != nil {
return &os.PathError{
Path: nbd.nbd.Name(),
Op: "ioctl NBD_DISCONNECT",
Err: err,
}
}
return nil
}

func waitDisconnect(f *os.File) {
clog.Infof("Running disconnection to %s", nbd.nbd.Name())

runtime.LockOSThread()
defer runtime.UnlockOSThread()

// NBD_DO_IT does not return until disconnect
if err := ioctl(f.Fd(), ioctlDoIt, 0); err != nil {
clog.Errorf("error %s: ioctl returned %v", f.Name(), err)
err = ioctl(nbd.nbd.Fd(), ioctlDisconnect, 0)
if err != nil {
clog.Errorf("error disconnecting %s. ioctl returned: %v", nbd.nbd.Name(), err)
}

clog.Debugf("Running disconnection to %s", f.Name())

if err := ioctl(f.Fd(), ioctlClearQueue, 0); err != nil {
clog.Errorf("error clear queue for %s. ioctl returned: %v", f.Name(), err)
err = ioctl(nbd.nbd.Fd(), ioctlClearSock, 0)
if err != nil {
clog.Errorf("error clear socket for %s. ioctl returned: %v", nbd.nbd.Name(), err)
}
err = ioctl(nbd.nbd.Fd(), ioctlClearQueue, 0)
if err != nil {
clog.Errorf("error clear queue for %s. ioctl returned: %v", nbd.nbd.Name(), err)
}
if err := ioctl(f.Fd(), ioctlClearSock, 0); err != nil {
clog.Errorf("error clear socket for %s. ioctl returned: %v", f.Name(), err)
err = ioctl(nbd.nbd.Fd(), ioctlClearQueue, 0)
if err != nil {
clog.Errorf("error clear queue for %s. ioctl returned: %v", nbd.nbd.Name(), err)
}
err = nbd.nbd.Close()
if err != nil {
clog.Errorf("error close nbd device %s. ioctl returned: %v", nbd.nbd.Name(), err)
}
}

Expand Down

0 comments on commit 4d22ad3

Please sign in to comment.