summaryrefslogtreecommitdiffstats
path: root/common/block.c
blob: 24377c627d2f371ecf938f3c2930503718548c6c (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
/*
 * block.c - simple block layer
 *
 * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <common.h>
#include <block.h>
#include <linux/err.h>

#define BLOCKSIZE(blk)	(1 << blk->blockbits)

#define WRBUFFER_LAST(blk)	(blk->wrblock + blk->wrbufblocks - 1)

#ifdef CONFIG_BLOCK_WRITE
static int writebuffer_flush(struct block_device *blk)
{
	if (!blk->wrbufblocks)
		return 0;

	blk->ops->write(blk, blk->wrbuf, blk->wrblock,
			blk->wrbufblocks);

	blk->wrbufblocks = 0;

	return 0;
}

static int block_put(struct block_device *blk, const void *buf, int block)
{
	if (block >= blk->num_blocks)
		return -EIO;

	if (block < blk->wrblock || block > blk->wrblock + blk->wrbufblocks) {
		writebuffer_flush(blk);
	}

	if (blk->wrbufblocks == 0) {
		blk->wrblock = block;
		blk->wrbufblocks = 1;
	}

	memcpy(blk->wrbuf + (block - blk->wrblock) * BLOCKSIZE(blk),
			buf, BLOCKSIZE(blk));

	if (block > WRBUFFER_LAST(blk))
		blk->wrbufblocks++;

	if (blk->wrbufblocks == blk->wrbufsize)
		writebuffer_flush(blk);

	return 0;
}

#else
static int writebuffer_flush(struct block_device *blk)
{
	return 0;
}
#endif

static void *block_get(struct block_device *blk, int block)
{
	int ret;
	int num_blocks;

	if (block >= blk->num_blocks)
		return ERR_PTR(-EIO);

	/* first look into write buffer */
	if (block >= blk->wrblock && block <= WRBUFFER_LAST(blk))
		return blk->wrbuf + (block - blk->wrblock) * BLOCKSIZE(blk);

	/* then look into read buffer */
	if (block >= blk->rdblock && block <= blk->rdblockend)
		return blk->rdbuf + (block - blk->rdblock) * BLOCKSIZE(blk);

	/*
	 * If none of the buffers above match read the block from
	 * the device
	 */
	num_blocks = min(blk->rdbufsize, blk->num_blocks - block);

	ret = blk->ops->read(blk, blk->rdbuf, block, num_blocks);
	if (ret)
		return ERR_PTR(ret);

	blk->rdblock = block;
	blk->rdblockend = block + num_blocks - 1;

	return blk->rdbuf;
}

static ssize_t block_read(struct cdev *cdev, void *buf, size_t count,
		unsigned long offset, unsigned long flags)
{
	struct block_device *blk = cdev->priv;
	unsigned long mask = BLOCKSIZE(blk) - 1;
	unsigned long block = offset >> blk->blockbits;
	size_t icount = count;
	int blocks;

	if (offset & mask) {
		size_t now = BLOCKSIZE(blk) - (offset & mask);
		void *iobuf = block_get(blk, block);

		now = min(count, now);

		if (IS_ERR(iobuf))
			return PTR_ERR(iobuf);

		memcpy(buf, iobuf + (offset & mask), now);
		buf += now;
		count -= now;
		block++;
	}

	blocks = count >> blk->blockbits;

	while (blocks) {
		void *iobuf = block_get(blk, block);

		if (IS_ERR(iobuf))
			return PTR_ERR(iobuf);

		memcpy(buf, iobuf, BLOCKSIZE(blk));
		buf += BLOCKSIZE(blk);
		blocks--;
		block++;
		count -= BLOCKSIZE(blk);
	}

	if (count) {
		void *iobuf = block_get(blk, block);

		if (IS_ERR(iobuf))
			return PTR_ERR(iobuf);

		memcpy(buf, iobuf, count);
	}

	return icount;
}

#ifdef CONFIG_BLOCK_WRITE
static ssize_t block_write(struct cdev *cdev, const void *buf, size_t count,
		unsigned long offset, ulong flags)
{
	struct block_device *blk = cdev->priv;
	unsigned long mask = BLOCKSIZE(blk) - 1;
	unsigned long block = offset >> blk->blockbits;
	size_t icount = count;
	int blocks;

	if (offset & mask) {
		size_t now = BLOCKSIZE(blk) - (offset & mask);
		void *iobuf = block_get(blk, block);

		now = min(count, now);

		if (IS_ERR(iobuf))
			return PTR_ERR(iobuf);

		memcpy(iobuf + (offset & mask), buf, now);
		block_put(blk, iobuf, block);
		buf += now;
		count -= now;
		block++;
	}

	blocks = count >> blk->blockbits;

	while (blocks) {
		block_put(blk, buf, block);
		buf += BLOCKSIZE(blk);
		blocks--;
		block++;
		count -= BLOCKSIZE(blk);
	}

	if (count) {
		void *iobuf = block_get(blk, block);

		if (IS_ERR(iobuf))
			return PTR_ERR(iobuf);

		memcpy(iobuf, buf, count);
		block_put(blk, iobuf, block);
	}

	return icount;
}
#endif

static int block_close(struct cdev *cdev)
{
	struct block_device *blk = cdev->priv;

	return writebuffer_flush(blk);
}

static int block_flush(struct cdev *cdev)
{
	struct block_device *blk = cdev->priv;

	return writebuffer_flush(blk);
}

struct file_operations block_ops = {
	.read	= block_read,
#ifdef CONFIG_BLOCK_WRITE
	.write	= block_write,
#endif
	.close	= block_close,
	.flush	= block_flush,
	.lseek	= dev_lseek_default,
};

int blockdevice_register(struct block_device *blk)
{
	size_t size = blk->num_blocks * BLOCKSIZE(blk);
	int ret;

	blk->cdev.size = size;
	blk->cdev.dev = blk->dev;
	blk->cdev.ops = &block_ops;
	blk->cdev.priv = blk;
	blk->rdbufsize = PAGE_SIZE >> blk->blockbits;
	blk->rdbuf = xmalloc(PAGE_SIZE);
	blk->rdblock = 1;
	blk->rdblockend = 0;
	blk->wrbufsize = PAGE_SIZE >> blk->blockbits;
	blk->wrbuf = xmalloc(PAGE_SIZE);
	blk->wrblock = 0;
	blk->wrbufblocks = 0;

	ret = devfs_create(&blk->cdev);
	if (ret)
		return ret;

	return 0;
}

int blockdevice_unregister(struct block_device *blk)
{
	return 0;
}