Skip to content

Commit 024511d

Browse files
committed
fusefrontend: coalesce 4kB writes
This improves performance on hdds running ext4, and improves streaming write performance on hdds running btrfs. Tar extract slows down on btrfs for some reason. See #63 Benchmarks: encfs v1.9.1 ============ $ ./benchmark.bash -encfs /mnt/hdd-ext4 Testing EncFS at /mnt/hdd-ext4/benchmark.bash.u0g WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 1,48354 s, 88,4 MB/s UNTAR: 20.79 LS: 3.04 RM: 6.62 $ ./benchmark.bash -encfs /mnt/hdd-btrfs Testing EncFS at /mnt/hdd-btrfs/benchmark.bash.h40 WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 1,52552 s, 85,9 MB/s UNTAR: 24.51 LS: 2.73 RM: 5.32 gocryptfs v1.1.1-26-g4a7f8ef ============================ $ ./benchmark.bash /mnt/hdd-ext4 Testing gocryptfs at /mnt/hdd-ext4/benchmark.bash.1KG WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 1,55782 s, 84,1 MB/s UNTAR: 22.23 LS: 1.47 RM: 4.17 $ ./benchmark.bash /mnt/hdd-btrfs Testing gocryptfs at /mnt/hdd-btrfs/benchmark.bash.2t8 WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 6,87206 s, 19,1 MB/s UNTAR: 69.87 LS: 1.52 RM: 5.33 gocryptfs v1.1.1-32 =================== $ ./benchmark.bash /mnt/hdd-ext4 Testing gocryptfs at /mnt/hdd-ext4/benchmark.bash.Qt3 WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 1,22577 s, 107 MB/s UNTAR: 23.46 LS: 1.46 RM: 4.67 $ ./benchmark.bash /mnt/hdd-btrfs/ Testing gocryptfs at /mnt/hdd-btrfs//benchmark.bash.XVk WRITE: 131072000 bytes (131 MB, 125 MiB) copied, 3,68735 s, 35,5 MB/s UNTAR: 116.87 LS: 1.84 RM: 6.34
1 parent 80c50b9 commit 024511d

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

internal/fusefrontend/file.go

+29-27
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,14 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
255255
}
256256
fileID := f.fileTableEntry.ID
257257
defer f.fileTableEntry.IDLock.RUnlock()
258-
259-
var written uint32
258+
// Handle payload data
260259
status := fuse.OK
261260
dataBuf := bytes.NewBuffer(data)
262261
blocks := f.contentEnc.ExplodePlainRange(uint64(off), uint64(len(data)))
263-
for _, b := range blocks {
264-
262+
writeChain := make([][]byte, len(blocks))
263+
var numOutBytes int
264+
for i, b := range blocks {
265265
blockData := dataBuf.Next(int(b.Length))
266-
267266
// Incomplete block -> Read-Modify-Write
268267
if b.IsPartial() {
269268
// Read
@@ -272,38 +271,41 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) {
272271
oldData, status = f.doRead(o, f.contentEnc.PlainBS())
273272
if status != fuse.OK {
274273
tlog.Warn.Printf("ino%d fh%d: RMW read failed: %s", f.devIno.ino, f.intFd(), status.String())
275-
return written, status
274+
return 0, status
276275
}
277276
// Modify
278277
blockData = f.contentEnc.MergeBlocks(oldData, blockData, int(b.Skip))
279278
tlog.Debug.Printf("len(oldData)=%d len(blockData)=%d", len(oldData), len(blockData))
280279
}
281-
282280
// Encrypt
283-
blockOffset := b.BlockCipherOff()
284281
blockData = f.contentEnc.EncryptBlock(blockData, b.BlockNo, fileID)
285282
tlog.Debug.Printf("ino%d: Writing %d bytes to block #%d",
286283
f.devIno.ino, uint64(len(blockData))-f.contentEnc.BlockOverhead(), b.BlockNo)
287-
288-
// Prevent partially written (=corrupt) blocks by preallocating the space beforehand
289-
err := syscallcompat.EnospcPrealloc(int(f.fd.Fd()), int64(blockOffset), int64(len(blockData)))
290-
if err != nil {
291-
tlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %s", f.devIno.ino, f.intFd(), err.Error())
292-
status = fuse.ToStatus(err)
293-
break
294-
}
295-
296-
// Write
297-
_, err = f.fd.WriteAt(blockData, int64(blockOffset))
298-
299-
if err != nil {
300-
tlog.Warn.Printf("doWrite: Write failed: %s", err.Error())
301-
status = fuse.ToStatus(err)
302-
break
303-
}
304-
written += uint32(b.Length)
284+
// Store output data in the writeChain
285+
writeChain[i] = blockData
286+
numOutBytes += len(blockData)
287+
}
288+
// Concatenenate all elements in the writeChain into one contigous buffer
289+
tmp := make([]byte, numOutBytes)
290+
writeBuf := bytes.NewBuffer(tmp[:0])
291+
for _, w := range writeChain {
292+
writeBuf.Write(w)
293+
}
294+
// Preallocate so we cannot run out of space in the middle of the write.
295+
// This prevents partially written (=corrupt) blocks.
296+
cOff := blocks[0].BlockCipherOff()
297+
err := syscallcompat.EnospcPrealloc(int(f.fd.Fd()), int64(cOff), int64(writeBuf.Len()))
298+
if err != nil {
299+
tlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %s", f.devIno.ino, f.intFd(), err.Error())
300+
return 0, fuse.ToStatus(err)
301+
}
302+
// Write
303+
_, err = f.fd.WriteAt(writeBuf.Bytes(), int64(cOff))
304+
if err != nil {
305+
tlog.Warn.Printf("doWrite: Write failed: %s", err.Error())
306+
return 0, fuse.ToStatus(err)
305307
}
306-
return written, status
308+
return uint32(len(data)), fuse.OK
307309
}
308310

309311
// isConsecutiveWrite returns true if the current write

0 commit comments

Comments
 (0)