From 8518d6d7bde33fdc7ef5bcb7c3c7709404392ad8 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 13 Dec 2015 17:17:01 +0100 Subject: [PATCH] Handle EINTR returned by Fallocate Fallocate can return EINTR (interrupted system call) and does so quite often when cpu profiling is enabled. --- pathfs_frontend/file.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/pathfs_frontend/file.go b/pathfs_frontend/file.go index f2350cc..1470da2 100644 --- a/pathfs_frontend/file.go +++ b/pathfs_frontend/file.go @@ -97,9 +97,9 @@ func (f *file) createHeader() error { // Prevent partially written (=corrupt) header by preallocating the space beforehand f.fdLock.Lock() defer f.fdLock.Unlock() - err := syscall.Fallocate(int(f.fd.Fd()), FALLOC_FL_KEEP_SIZE, 0, cryptfs.HEADER_LEN) + err := fallocateRetry(int(f.fd.Fd()), FALLOC_FL_KEEP_SIZE, 0, cryptfs.HEADER_LEN) if err != nil { - cryptfs.Warn.Printf("createHeader: Fallocate failed: %s\n", err.Error()) + cryptfs.Warn.Printf("createHeader: fallocateRetry failed: %s\n", err.Error()) return err } @@ -205,6 +205,17 @@ func (f *file) Read(buf []byte, off int64) (resultData fuse.ReadResult, code fus return fuse.ReadResultData(out), status } +// fallocateRetry - syscall.Fallocate() with retry for EINTR. +func fallocateRetry(fd int, mode uint32, off int64, len int64) (err error) { + for { + err = syscall.Fallocate(fd, mode, off, len) + if err == syscall.EINTR { + continue + } + return err + } +} + const FALLOC_FL_KEEP_SIZE = 0x01 // doWrite - encrypt "data" and write it to plaintext offset "off" @@ -258,10 +269,10 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) { // Prevent partially written (=corrupt) blocks by preallocating the space beforehand f.fdLock.Lock() - err := syscall.Fallocate(int(f.fd.Fd()), FALLOC_FL_KEEP_SIZE, int64(blockOffset), int64(blockLen)) + err := fallocateRetry(int(f.fd.Fd()), FALLOC_FL_KEEP_SIZE, int64(blockOffset), int64(blockLen)) f.fdLock.Unlock() if err != nil { - cryptfs.Warn.Printf("doWrite: Fallocate failed: %s\n", err.Error()) + cryptfs.Warn.Printf("doWrite: fallocateRetry failed: %s\n", err.Error()) status = fuse.ToStatus(err) break }