github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/fuse/fuse.go (about)

     1  // Copyright (c) 2015-2021, NVIDIA CORPORATION.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package fuse
     5  
     6  import (
     7  	"fmt"
     8  	"sync"
     9  
    10  	fuselib "bazil.org/fuse"
    11  	fusefslib "bazil.org/fuse/fs"
    12  	"golang.org/x/net/context"
    13  
    14  	"github.com/swiftstack/ProxyFS/blunder"
    15  	"github.com/swiftstack/ProxyFS/fs"
    16  	"github.com/swiftstack/ProxyFS/inode"
    17  )
    18  
    19  type ProxyFUSE struct {
    20  	volumeHandle fs.VolumeHandle
    21  	wg           sync.WaitGroup // Used to synchronize mount
    22  }
    23  
    24  func (pfs *ProxyFUSE) Root() (fusefslib.Node, error) {
    25  	root := Dir{volumeHandle: pfs.volumeHandle, inodeNumber: inode.RootDirInodeNumber}
    26  
    27  	// Signal any waiters that we have completed mounting the volume.
    28  	// We know this because this call is only made after the user level FUSE
    29  	// library and the FUSE driver have agreed on the FUSE prototocol level.
    30  	pfs.wg.Done()
    31  	return root, nil
    32  }
    33  
    34  func (pfs *ProxyFUSE) Statfs(ctx context.Context, req *fuselib.StatfsRequest, resp *fuselib.StatfsResponse) error {
    35  	enterGate()
    36  	defer leaveGate()
    37  
    38  	statvfs, err := pfs.volumeHandle.StatVfs()
    39  	if err != nil {
    40  		return newFuseError(err)
    41  	}
    42  	resp.Blocks = statvfs[fs.StatVFSTotalBlocks]
    43  	resp.Bfree = statvfs[fs.StatVFSFreeBlocks]
    44  	resp.Bavail = statvfs[fs.StatVFSAvailBlocks]
    45  	resp.Files = statvfs[fs.StatVFSTotalInodes]
    46  	resp.Ffree = statvfs[fs.StatVFSFreeInodes]
    47  	resp.Bsize = uint32(statvfs[fs.StatVFSBlockSize])
    48  	resp.Namelen = uint32(statvfs[fs.StatVFSMaxFilenameLen])
    49  	resp.Frsize = uint32(statvfs[fs.StatVFSFragmentSize])
    50  	return nil
    51  }
    52  
    53  type fuseError struct {
    54  	str   string
    55  	errno fuselib.Errno
    56  }
    57  
    58  func (fE *fuseError) Errno() fuselib.Errno {
    59  	return fE.errno
    60  }
    61  
    62  func (fE *fuseError) Error() string {
    63  	return fE.str
    64  }
    65  
    66  func newFuseError(err error) *fuseError {
    67  	return &fuseError{
    68  		str:   fmt.Sprintf("%v", err),
    69  		errno: fuselib.Errno(blunder.Errno(err)),
    70  	}
    71  }