summaryrefslogtreecommitdiff
path: root/shared/utils/rmtfs/storage.c
blob: 107b29670a5462395250bff8d879fd34a17236a0 (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
288
289
290
291
292
293
294
295
296
297
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rmtfs.h"

#define MAX_CALLERS 10
#define STORAGE_MAX_SIZE (16 * 1024 * 1024)

#define BY_PARTLABEL_PATH "/dev/disk/by-partlabel"

#define MIN(x, y) ((x) < (y) ? (x) : (y))

struct partition {
	const char *path;
	const char *actual;
	const char *partlabel;
};

struct rmtfd {
	unsigned id;
	unsigned node;
	int fd;
	unsigned dev_error;
	const struct partition *partition;

	void *shadow_buf;
	size_t shadow_len;
};

static const char *storage_dir = "/boot";
static int storage_read_only;
static int storage_use_partitions;

static const struct partition partition_table[] = {
	{ "/boot/modem_fs1", "modem_fs1", "modemst1" },
	{ "/boot/modem_fs2", "modem_fs2", "modemst2" },
	{ "/boot/modem_fsc", "modem_fsc", "fsc" },
	{ "/boot/modem_fsg", "modem_fsg", "fsg" },
	{ "/boot/modem_tunning", "modem_tunning", "tunning" },
	{}
};

static struct rmtfd rmtfds[MAX_CALLERS];

static int storage_populate_shadow_buf(struct rmtfd *rmtfd, const char *file);

int storage_init(const char *storage_root, bool read_only, bool use_partitions)
{
	int i;

	if (storage_root)
		storage_dir = storage_root;

	if (use_partitions) {
		if (!storage_root)
			storage_dir = BY_PARTLABEL_PATH;
		storage_use_partitions = true;
	}

	storage_read_only = read_only;

	for (i = 0; i < MAX_CALLERS; i++) {
		rmtfds[i].id = i;
		rmtfds[i].fd = -1;
		rmtfds[i].shadow_buf = NULL;
	}

	return 0;
}

struct rmtfd *storage_open(unsigned node, const char *path)
{
	char *fspath;
	const struct partition *part;
	struct rmtfd *rmtfd = NULL;
	const char *file;
	size_t pathlen;
	int saved_errno;
	int ret;
	int fd;
	int i;

	for (part = partition_table; part->path; part++) {
		if (strcmp(part->path, path) == 0)
			goto found;
	}

	fprintf(stderr, "[RMTFS storage] request for unknown partition '%s', rejecting\n", path);
	return NULL;

found:
	/* Check if this node already has the requested path open */
	for (i = 0; i < MAX_CALLERS; i++) {
		if ((rmtfds[i].fd != -1 || rmtfds[i].shadow_buf) &&
		    rmtfds[i].node == node &&
		    rmtfds[i].partition == part)
			return &rmtfds[i];
	}

	for (i = 0; i < MAX_CALLERS; i++) {
		if (rmtfds[i].fd == -1 && !rmtfds[i].shadow_buf) {
			rmtfd = &rmtfds[i];
			break;
		}
	}
	if (!rmtfd) {
		fprintf(stderr, "[storage] out of free rmtfd handles\n");
		return NULL;
	}

	if (storage_use_partitions)
		file = part->partlabel;
	else
		file = part->actual;

	pathlen = strlen(storage_dir) + strlen(file) + 2;
	fspath = alloca(pathlen);
	snprintf(fspath, pathlen, "%s/%s", storage_dir, file);
	if (!storage_read_only) {
		fd = open(fspath, O_RDWR);
		if (fd < 0) {
			saved_errno = errno;
			fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
					fspath, part->path, strerror(saved_errno));
			errno = saved_errno;
			return NULL;
		}
		rmtfd->fd = fd;
		rmtfd->shadow_len = 0;
	} else {
		ret = storage_populate_shadow_buf(rmtfd, fspath);
		if (ret < 0) {
			saved_errno = errno;
			fprintf(stderr, "[storage] failed to open '%s' (requested '%s'): %s\n",
					fspath, part->path, strerror(saved_errno));
			errno = saved_errno;
			return NULL;
		}
	}

	rmtfd->node = node;
	rmtfd->partition = part;

	return rmtfd;
}

void storage_close(struct rmtfd *rmtfd)
{
	if (rmtfd->fd >= 0) {
		close(rmtfd->fd);
		rmtfd->fd = -1;
	}

	free(rmtfd->shadow_buf);
	rmtfd->shadow_buf = NULL;
	rmtfd->shadow_len = 0;

	rmtfd->partition = NULL;
}

struct rmtfd *storage_get(unsigned node, int caller_id)
{
	struct rmtfd *rmtfd;

	if (caller_id >= MAX_CALLERS)
		return NULL;

	rmtfd = &rmtfds[caller_id];
	if (rmtfd->node != node)
		return NULL;

	return rmtfd;
}

int storage_get_caller_id(const struct rmtfd *rmtfd)
{
	return rmtfd->id;
}

int storage_get_error(const struct rmtfd *rmtfd)
{
	return rmtfd->dev_error;
}

void storage_exit(void)
{
	int i;

	for (i = 0; i < MAX_CALLERS; i++)
		storage_close(&rmtfds[i]);
}

ssize_t storage_pread(const struct rmtfd *rmtfd, void *buf, size_t nbyte, off_t offset)
{
	ssize_t n;

	if (!storage_read_only) {
		n = pread(rmtfd->fd, buf, nbyte, offset);
	} else {
		n = MIN((ssize_t)nbyte, (ssize_t)rmtfd->shadow_len - offset);
		if (n > 0)
			memcpy(buf, (char*)rmtfd->shadow_buf + offset, n);
		else
			n = 0;
	}

	if (n < nbyte)
		memset((char*)buf + n, 0, nbyte - n);

	return nbyte;
}

ssize_t storage_pwrite(struct rmtfd *rmtfd, const void *buf, size_t nbyte, off_t offset)
{
	size_t new_len = offset + nbyte;
	void *new_buf;

	if (!storage_read_only)
		return pwrite(rmtfd->fd, buf, nbyte, offset);

	if (new_len >= STORAGE_MAX_SIZE) {
		fprintf(stderr, "write to %zd bytes exceededs max size\n", new_len);
		errno = -EINVAL;
		return -1;
	}

	if (new_len > rmtfd->shadow_len) {
		new_buf = realloc(rmtfd->shadow_buf, new_len);
		if (!new_buf) {
			errno = -ENOMEM;
			return -1;
		}

		rmtfd->shadow_buf = new_buf;
		rmtfd->shadow_len = new_len;
	}

	memcpy((char*)rmtfd->shadow_buf + offset, buf, nbyte);

	return nbyte;
}

int storage_sync(struct rmtfd *rmtfd)
{
	if (storage_read_only)
		return 0;
	
	return fdatasync(rmtfd->fd);
}

static int storage_populate_shadow_buf(struct rmtfd *rmtfd, const char *file)
{
	ssize_t len;
	ssize_t n;
	void *buf;
	int ret;
	int fd;

	fd = open(file, O_RDONLY);
	if (fd < 0)
		return -1;

	len = lseek(fd, 0, SEEK_END);
	if (len < 0) {
		ret = -1;
		goto err_close_fd;
	}

	lseek(fd, 0, SEEK_SET);

	buf = calloc(1, len);
	if (!buf) {
		ret = -1;
		goto err_close_fd;
	}

	n = read(fd, buf, len);
	if (n < 0) {
		ret = -1;
		goto err_close_fd;
	}

	rmtfd->shadow_buf = buf;
	rmtfd->shadow_len = n;

	ret = 0;

err_close_fd:
	close(fd);

	return ret;
}