summaryrefslogtreecommitdiff
path: root/lib/diskio/file_blockbuf.go
blob: c6653e6d3027f67ac16f3bd11bc291db6e7d6b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package diskio

import (
	"context"
	"sync"

	"github.com/datawire/dlib/dlog"

	"git.lukeshu.com/btrfs-progs-ng/lib/containers"
)

type bufferedBlock[A ~int64] struct {
	Mu    sync.RWMutex
	Addr  A
	Dirty bool

	Dat []byte
	Err error
}

type bufferedFile[A ~int64] struct {
	ctx        context.Context //nolint:containedctx // don't have an option while keeping the io.ReaderAt/io.WriterAt API
	inner      File[A]
	blockSize  A
	blockCache containers.Cache[A, bufferedBlock[A]]
}

var _ File[assertAddr] = (*bufferedFile[assertAddr])(nil)

func NewBufferedFile[A ~int64](ctx context.Context, file File[A], blockSize A, cacheSize int) *bufferedFile[A] {
	ret := &bufferedFile[A]{
		ctx:       ctx,
		inner:     file,
		blockSize: blockSize,
	}
	ret.blockCache = containers.NewARCache[A, bufferedBlock[A]](cacheSize, bufferedBlockSource[A]{ret})
	return ret
}

type bufferedBlockSource[A ~int64] struct {
	bf *bufferedFile[A]
}

// Flush implements [containers.Source].
func (src bufferedBlockSource[A]) Flush(ctx context.Context, block *bufferedBlock[A]) {
	if !block.Dirty {
		return
	}
	if _, err := src.bf.inner.WriteAt(block.Dat, block.Addr); err != nil {
		dlog.Errorf(ctx, "i/o error: write: %v", err)
	}
	block.Dirty = false
}

// Load implements [containers.Source].
func (src bufferedBlockSource[A]) Load(ctx context.Context, blockAddr A, block *bufferedBlock[A]) {
	src.Flush(ctx, block)
	if block.Dat == nil {
		block.Dat = make([]byte, src.bf.blockSize)
	}
	n, err := src.bf.inner.ReadAt(block.Dat[:src.bf.blockSize], blockAddr)
	block.Addr = blockAddr
	block.Dat = block.Dat[:n]
	block.Err = err
}

// Name implements [File].
func (bf *bufferedFile[A]) Name() string { return bf.inner.Name() }

// Size implements [File].
func (bf *bufferedFile[A]) Size() A { return bf.inner.Size() }

func (bf *bufferedFile[A]) Flush() {
	bf.blockCache.Flush(bf.ctx)
}

// Close implements [File] and [io.Closer].
func (bf *bufferedFile[A]) Close() error {
	bf.Flush()
	return bf.inner.Close()
}

// ReadAt implements [File] and [ReaderAt].
func (bf *bufferedFile[A]) ReadAt(dat []byte, off A) (n int, err error) {
	done := 0
	for done < len(dat) {
		n, err := bf.maybeShortReadAt(dat[done:], off+A(done))
		done += n
		if err != nil {
			return done, err
		}
	}
	return done, nil
}

func (bf *bufferedFile[A]) maybeShortReadAt(dat []byte, off A) (n int, err error) {
	offsetWithinBlock := off % bf.blockSize
	blockOffset := off - offsetWithinBlock

	cachedBlock := bf.blockCache.Acquire(bf.ctx, blockOffset)
	defer bf.blockCache.Release(blockOffset)
	cachedBlock.Mu.RLock()
	defer cachedBlock.Mu.RUnlock()

	n = copy(dat, cachedBlock.Dat[offsetWithinBlock:])
	if n < len(dat) {
		return n, cachedBlock.Err
	}
	return n, nil
}

// WriteAt implements [File].
func (bf *bufferedFile[A]) WriteAt(dat []byte, off A) (n int, err error) {
	done := 0
	for done < len(dat) {
		n, err := bf.maybeShortWriteAt(dat[done:], off+A(done))
		done += n
		if err != nil {
			return done, err
		}
	}
	return done, nil
}

func (bf *bufferedFile[A]) maybeShortWriteAt(dat []byte, off A) (n int, err error) {
	offsetWithinBlock := off % bf.blockSize
	blockOffset := off - offsetWithinBlock

	cachedBlock := bf.blockCache.Acquire(bf.ctx, blockOffset)
	defer bf.blockCache.Release(blockOffset)
	cachedBlock.Mu.Lock()
	defer cachedBlock.Mu.Unlock()

	cachedBlock.Dirty = true
	n = copy(cachedBlock.Dat[offsetWithinBlock:], dat)
	if n < len(dat) {
		return n, cachedBlock.Err
	}
	return n, nil
}