summaryrefslogtreecommitdiff
path: root/lib/device/bcache-utils.c
blob: cf741445779b3e630d70ff45fa5f3d4204f54a82 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2018 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "lib/device/bcache.h"

// FIXME: need to define this in a common place (that doesn't pull in deps)
#ifndef SECTOR_SHIFT
#define SECTOR_SHIFT 9
#endif

//----------------------------------------------------------------

static void byte_range_to_block_range(struct bcache *cache, uint64_t start, size_t len,
				      block_address *bb, block_address *be)
{
	block_address block_size = bcache_block_sectors(cache) << SECTOR_SHIFT;
	*bb = start / block_size;
	*be = (start + len + block_size - 1) / block_size;
}

static uint64_t _min(uint64_t lhs, uint64_t rhs)
{
	if (rhs < lhs)
		return rhs;

	return lhs;
}

//----------------------------------------------------------------

void bcache_prefetch_bytes(struct bcache *cache, int fd, uint64_t start, size_t len)
{
	block_address bb, be;

	byte_range_to_block_range(cache, start, len, &bb, &be);
	while (bb < be) {
		bcache_prefetch(cache, fd, bb);
		bb++;
	}
}

//----------------------------------------------------------------

bool bcache_read_bytes(struct bcache *cache, int fd, uint64_t start, size_t len, void *data)
{
	struct block *b;
	block_address bb, be;
	uint64_t block_size = bcache_block_sectors(cache) << SECTOR_SHIFT;
	uint64_t block_offset = start % block_size;

	bcache_prefetch_bytes(cache, fd, start, len);

	byte_range_to_block_range(cache, start, len, &bb, &be);

	for (; bb != be; bb++) {
        	if (!bcache_get(cache, fd, bb, 0, &b))
			return false;

		size_t blen = _min(block_size - block_offset, len);
		memcpy(data, ((unsigned char *) b->data) + block_offset, blen);
		bcache_put(b);

		block_offset = 0;
		len -= blen;
		data = ((unsigned char *) data) + blen;
	}

	return true;
}

bool bcache_invalidate_bytes(struct bcache *cache, int fd, uint64_t start, size_t len)
{
	block_address bb, be;
	bool result = true;

	byte_range_to_block_range(cache, start, len, &bb, &be);

	for (; bb != be; bb++) {
		if (!bcache_invalidate(cache, fd, bb))
			result = false;
	}

	return result;
}

//----------------------------------------------------------------

// Writing bytes and zeroing bytes are very similar, so we factor out
// this common code.
 
struct updater;

typedef bool (*partial_update_fn)(struct updater *u, int fd, block_address bb, uint64_t offset, size_t len);
typedef bool (*whole_update_fn)(struct updater *u, int fd, block_address bb, block_address be);

struct updater {
	struct bcache *cache;
	partial_update_fn partial_fn;
	whole_update_fn whole_fn;
	void *data;
};

static bool _update_bytes(struct updater *u, int fd, uint64_t start, size_t len)
{
        struct bcache *cache = u->cache;
	block_address bb, be;
	uint64_t block_size = bcache_block_sectors(cache) << SECTOR_SHIFT;
	uint64_t block_offset = start % block_size;
	uint64_t nr_whole;

	byte_range_to_block_range(cache, start, len, &bb, &be);

	// If the last block is partial, we will require a read, so let's 
	// prefetch it.
	if ((start + len) % block_size)
        	bcache_prefetch(cache, fd, (start + len) / block_size);

	// First block may be partial
	if (block_offset) {
        	size_t blen = _min(block_size - block_offset, len);
		if (!u->partial_fn(u, fd, bb, block_offset, blen))
        		return false;

		len -= blen;
        	if (!len)
                	return true;

                bb++;
	}

        // Now we write out a set of whole blocks
        nr_whole = len / block_size;
        if (!u->whole_fn(u, fd, bb, bb + nr_whole))
                return false;

	bb += nr_whole;
	len -= nr_whole * block_size;

	if (!len)
        	return true;

        // Finally we write a partial end block
        return u->partial_fn(u, fd, bb, 0, len);
}

//----------------------------------------------------------------

static bool _write_partial(struct updater *u, int fd, block_address bb,
                           uint64_t offset, size_t len)
{
	struct block *b;

	if (!bcache_get(u->cache, fd, bb, GF_DIRTY, &b))
		return false;

	memcpy(((unsigned char *) b->data) + offset, u->data, len);
	u->data = ((unsigned char *) u->data) + len;

	bcache_put(b);
	return true;
}

static bool _write_whole(struct updater *u, int fd, block_address bb, block_address be)
{
	struct block *b;
	uint64_t block_size = bcache_block_sectors(u->cache) << SECTOR_SHIFT;

	for (; bb != be; bb++) {
        	// We don't need to read the block since we are overwriting
        	// it completely.
		if (!bcache_get(u->cache, fd, bb, GF_ZERO, &b))
        		return false;
		memcpy(b->data, u->data, block_size);
		u->data = ((unsigned char *) u->data) + block_size;
        	bcache_put(b);
	}

	return true;
}

bool bcache_write_bytes(struct bcache *cache, int fd, uint64_t start, size_t len, void *data)
{
        struct updater u;

        u.cache = cache;
        u.partial_fn = _write_partial;
        u.whole_fn = _write_whole;
        u.data = data;

	return _update_bytes(&u, fd, start, len);
}

//----------------------------------------------------------------

static bool _zero_partial(struct updater *u, int fd, block_address bb, uint64_t offset, size_t len)
{
	struct block *b;

	if (!bcache_get(u->cache, fd, bb, GF_DIRTY, &b))
		return false;

	memset(((unsigned char *) b->data) + offset, 0, len);
	bcache_put(b);

	return true;
}

static bool _zero_whole(struct updater *u, int fd, block_address bb, block_address be)
{
	struct block *b;

	for (; bb != be; bb++) {
		if (!bcache_get(u->cache, fd, bb, GF_ZERO, &b))
        		return false;
        	bcache_put(b);
	}

	return true;
}

bool bcache_zero_bytes(struct bcache *cache, int fd, uint64_t start, size_t len)
{
        struct updater u;

        u.cache = cache;
        u.partial_fn = _zero_partial;
        u.whole_fn = _zero_whole;
        u.data = NULL;

	return _update_bytes(&u, fd, start, len);
}

//----------------------------------------------------------------

static bool _set_partial(struct updater *u, int fd, block_address bb, uint64_t offset, size_t len)
{
	struct block *b;
	uint8_t val = *((uint8_t *) u->data);

	if (!bcache_get(u->cache, fd, bb, GF_DIRTY, &b))
		return false;

	memset(((unsigned char *) b->data) + offset, val, len);
	bcache_put(b);

	return true;
}

static bool _set_whole(struct updater *u, int fd, block_address bb, block_address be)
{
	struct block *b;
	uint8_t val = *((uint8_t *) u->data);
        uint64_t len = bcache_block_sectors(u->cache) * 512;

	for (; bb != be; bb++) {
		if (!bcache_get(u->cache, fd, bb, GF_ZERO, &b))
        		return false;
        	memset((unsigned char *) b->data, val, len);
        	bcache_put(b);
	}

	return true;
}

bool bcache_set_bytes(struct bcache *cache, int fd, uint64_t start, size_t len, uint8_t val)
{
        struct updater u;

        u.cache = cache;
        u.partial_fn = _set_partial;
        u.whole_fn = _set_whole;
        u.data = &val;

	return _update_bytes(&u, fd, start, len);
}