Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for fallocate #66

Merged
merged 2 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,21 @@ func convertInMessage(
Value: value,
Flags: in.Flags,
}
case fusekernel.OpFallocate:
type input fusekernel.FallocateIn
in := (*input)(inMsg.Consume(unsafe.Sizeof(input{})))
if in == nil {
err = errors.New("Corrupt OpFallocate")
return
}

o = &fuseops.FallocateOp{
Inode: fuseops.InodeID(inMsg.Header().Nodeid),
Handle: fuseops.HandleID(in.Fh),
Offset: in.Offset,
Length: in.Length,
Mode: in.Mode,
}

default:
o = &unknownOp{
Expand Down Expand Up @@ -793,6 +808,9 @@ func (c *Connection) kernelResponseForOp(
case *fuseops.SetXattrOp:
// Empty response

case *fuseops.FallocateOp:
// Empty response

case *initOp:
out := (*fusekernel.InitOut)(m.Grow(int(unsafe.Sizeof(fusekernel.InitOut{}))))

Expand Down
5 changes: 5 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func describeRequest(op interface{}) (s string) {

case *fuseops.SetXattrOp:
addComponent("name %s", typed.Name)

case *fuseops.FallocateOp:
addComponent("offset %d", typed.Offset)
addComponent("length %d", typed.Length)
addComponent("mode %d", typed.Mode)
}

// Use just the name if there is no extra info.
Expand Down
19 changes: 19 additions & 0 deletions fuseops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,3 +865,22 @@ type SetXattrOp struct {
// simply replace the value if the attribute exists.
Flags uint32
}

type FallocateOp struct {
// The inode and handle we are fallocating
Inode InodeID
Handle HandleID

// Start of the byte range
Offset uint64

// Length of the byte range
Length uint64

// If Mode is 0x0, allocate disk space within the range specified
// If Mode has 0x1, allocate the space but don't increase the file size
// If Mode has 0x2, deallocate space within the range specified
// If Mode has 0x2, it sbould also have 0x1 (deallocate should not increase
// file size)
Mode uint32
}
4 changes: 4 additions & 0 deletions fuseutil/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type FileSystem interface {
GetXattr(context.Context, *fuseops.GetXattrOp) error
ListXattr(context.Context, *fuseops.ListXattrOp) error
SetXattr(context.Context, *fuseops.SetXattrOp) error
Fallocate(context.Context, *fuseops.FallocateOp) error

// Regard all inodes (including the root inode) as having their lookup counts
// decremented to zero, and clean up any resources associated with the file
Expand Down Expand Up @@ -215,6 +216,9 @@ func (s *fileSystemServer) handleOp(

case *fuseops.SetXattrOp:
err = s.fs.SetXattr(ctx, typed)

case *fuseops.FallocateOp:
err = s.fs.Fallocate(ctx, typed)
}

c.Reply(ctx, err)
Expand Down
7 changes: 7 additions & 0 deletions fuseutil/not_implemented_file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,12 @@ func (fs *NotImplementedFileSystem) SetXattr(
return
}

func (fs *NotImplementedFileSystem) Fallocate(
ctx context.Context,
op *fuseops.FallocateOp) (err error) {
err = fuse.ENOSYS
return
}

func (fs *NotImplementedFileSystem) Destroy() {
}
9 changes: 9 additions & 0 deletions internal/fusekernel/fuse_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ const (
OpDestroy = 38
OpIoctl = 39 // Linux?
OpPoll = 40 // Linux?
OpFallocate = 43

// OS X
OpSetvolname = 61
Expand Down Expand Up @@ -665,6 +666,14 @@ type ListxattrIn struct {
Padding uint32
}

type FallocateIn struct {
Fh uint64
Offset uint64
Length uint64
Mode uint32
Padding uint32
}

type LkIn struct {
Fh uint64
Owner uint64
Expand Down
16 changes: 16 additions & 0 deletions samples/memfs/inode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"time"

"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/fuseutil"
)
Expand Down Expand Up @@ -382,3 +383,18 @@ func (in *inode) SetAttributes(
in.attrs.Mtime = *mtime
}
}

func (in *inode) Fallocate(mode uint32, offset uint64, length uint64) (
err error) {
if mode == 0 {
newSize := int(offset + length)
if newSize > len(in.contents) {
padding := make([]byte, newSize-len(in.contents))
in.contents = append(in.contents, padding...)
in.attrs.Size = offset + length
}
} else {
err = fuse.ENOSYS
}
return
}
9 changes: 9 additions & 0 deletions samples/memfs/memfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,12 @@ func (fs *memFS) SetXattr(ctx context.Context,

return
}

func (fs *memFS) Fallocate(ctx context.Context,
op *fuseops.FallocateOp) (err error) {
fs.mu.Lock()
defer fs.mu.Unlock()
inode := fs.getInodeOrDie(op.Inode)
inode.Fallocate(op.Mode, op.Length, op.Length)
return
}
29 changes: 29 additions & 0 deletions samples/memfs/memfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"
"time"

fallocate "github.com/detailyang/go-fallocate"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fusetesting"
"github.com/jacobsa/fuse/samples"
Expand Down Expand Up @@ -1955,3 +1956,31 @@ func (t *MknodTest) NonExistentParent() {
err = syscall.Mknod(p, syscall.S_IFREG|0600, 0)
ExpectEq(syscall.ENOENT, err)
}

func (t *MknodTest) Fallocate_Larger() {
var err error
fileName := path.Join(t.Dir, "foo")

// Create a file.
err = ioutil.WriteFile(fileName, []byte("taco"), 0600)
AssertEq(nil, err)

// Open it for modification.
f, err := os.OpenFile(fileName, os.O_RDWR, 0)
t.ToClose = append(t.ToClose, f)
AssertEq(nil, err)

// Truncate it.
err = fallocate.Fallocate(f, 5, 1)
AssertEq(nil, err)

// Stat it.
fi, err := f.Stat()
AssertEq(nil, err)
ExpectEq(6, fi.Size())

// Read the contents.
contents, err := ioutil.ReadFile(fileName)
AssertEq(nil, err)
ExpectEq("taco\x00\x00", string(contents))
}