-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibcachefilesd.c
292 lines (239 loc) · 6.17 KB
/
libcachefilesd.c
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
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <poll.h>
#include <stdint.h>
#include "internal.h"
#define NAME_MAX 512
struct fd_path_link {
int object_id;
int fd;
int size;
char path[NAME_MAX];
} links[32];
unsigned int link_num = 0;
static struct fd_path_link *find_fd_path_link(int object_id)
{
struct fd_path_link *link;
int i;
for (i = 0; i < link_num; i++) {
link = links + i;
if (link->object_id == object_id)
return link;
}
return NULL;
}
int process_open_req(int devfd, struct cachefiles_msg *msg)
{
struct cachefiles_open *load;
struct fd_path_link *link;
char *volume_key, *cookie_key;
struct stat stats;
char cmd[32];
int ret;
unsigned long long size;
load = (void *)msg->data;
volume_key = load->data;
cookie_key = load->data + load->volume_key_size;
printf("[OPEN] volume key %s (volume_key_size %lu), cookie key %s (cookie_key_size %lu), "
"object id %d, fd %d, flags %u\n",
volume_key, load->volume_key_size, cookie_key, load->cookie_key_size,
msg->object_id, load->fd, load->flags);
ret = stat(cookie_key, &stats);
if (ret) {
printf("stat %s failed, %d (%s)\n", cookie_key, errno, strerror(errno));
return -1;
}
size = stats.st_size;
snprintf(cmd, sizeof(cmd), "copen %u,%llu", msg->msg_id, size);
printf("Writing cmd: %s\n", cmd);
ret = write(devfd, cmd, strlen(cmd));
if (ret < 0) {
printf("write [copen] failed\n");
return -1;
}
if (link_num >= 32)
return -1;
link = links + link_num;
link_num ++;
link->object_id = msg->object_id;
link->fd = load->fd;
link->size = size;
strncpy(link->path, cookie_key, NAME_MAX);
return 0;
}
/* error injection - return error directly */
int process_open_req_fail(int devfd, struct cachefiles_msg *msg)
{
struct cachefiles_open *load;
char *volume_key, *cookie_key;
char cmd[32];
int ret, size;
load = (void *)msg->data;
volume_key = load->data;
cookie_key = load->data + load->volume_key_size;
printf("[OPEN] volume key %s (volume_key_size %lu), cookie key %s (cookie_key_size %lu), "
"object id %d, fd %d, flags %u\n",
volume_key, load->volume_key_size, cookie_key, load->cookie_key_size,
msg->object_id, load->fd, load->flags);
snprintf(cmd, sizeof(cmd), "copen %u,-1", msg->msg_id);
printf("Writing cmd: %s\n", cmd);
ret = write(devfd, cmd, strlen(cmd));
if (ret < 0) {
printf("write [copen] failed\n");
return -1;
}
return 0;
}
int process_close_req(int devfd, struct cachefiles_msg *msg)
{
struct fd_path_link *link;
link = find_fd_path_link(msg->object_id);
if (!link) {
printf("invalid object id %d\n", msg->object_id);
return -1;
}
printf("[CLOSE] object_id %d, fd %d\n", msg->object_id, link->fd);
close(link->fd);
return 0;
}
/* error injection - don't close anon_fd */
int process_close_req_fail(int devfd, struct cachefiles_msg *msg)
{
printf("[CLOSE] object_id %d\n", msg->object_id);
return 0;
}
/* 2MB buffer aligned with 512 (logical block size) for DIRECT IO */
#define BUF_SIZE (2*1024*1024)
static char readbuf[BUF_SIZE] __attribute__((aligned(512)));
static int do_process_read_req(int devfd, struct cachefiles_msg *msg, int ra)
{
struct cachefiles_read *read;
struct fd_path_link *link;
int i, ret, retval = -1;
int dst_fd, src_fd;
char *src_path = NULL;
size_t len;
unsigned long id;
read = (void *)msg->data;
link = find_fd_path_link(msg->object_id);
if (!link) {
printf("invalid object id %d\n", msg->object_id);
return -1;
}
src_path = link->path;
dst_fd = link->fd;
printf("[READ] object_id %d, fd %d, src_path %s, off %llx, len %llx\n",
msg->object_id, dst_fd, src_path, read->off, read->len);
src_fd = open(src_path, O_RDONLY);
if (src_fd < 0) {
printf("open src_path %s failed\n", src_path);
return -1;
}
len = read->len;
if (BUF_SIZE < len) {
printf("buffer overflow\n");
close(src_fd);
return -1;
}
if (ra && read->off + BUF_SIZE <= link->size)
len = BUF_SIZE;
ret = pread(src_fd, readbuf, len, read->off);
if (ret != len) {
printf("read src image failed, ret %d, %d (%s)\n", ret, errno, strerror(errno));
close(src_fd);
return -1;
}
ret = pwrite(dst_fd, readbuf, len, read->off);
if (ret != len) {
printf("write dst image failed, ret %d, %d (%s)\n", ret, errno, strerror(errno));
close(src_fd);
return -1;
}
id = msg->msg_id;
ret = ioctl(dst_fd, CACHEFILES_IOC_CREAD, id);
if (ret < 0) {
printf("send cread failed, %d (%s)\n", errno, strerror(errno));
close(src_fd);
return -1;
}
close(src_fd);
return 0;
}
int process_read_req(int devfd, struct cachefiles_msg *msg)
{
return do_process_read_req(devfd, msg, 0);
}
int process_read_req_ra(int devfd, struct cachefiles_msg *msg)
{
return do_process_read_req(devfd, msg, 1);
}
/* error injection - return error directly */
int process_read_req_fail(int devfd, struct cachefiles_msg *msg)
{
struct cachefiles_read *read;
struct fd_path_link *link;
int i, ret, retval = -1;
int dst_fd, src_fd;
char *src_path = NULL;
size_t len;
unsigned long id;
read = (void *)msg->data;
link = find_fd_path_link(msg->object_id);
if (!link) {
printf("invalid object id %d\n", msg->object_id);
return -1;
}
dst_fd = link->fd;
id = msg->msg_id;
ret = ioctl(dst_fd, CACHEFILES_IOC_CREAD, id);
if (ret < 0) {
printf("send cread failed, %d (%s)\n", errno, strerror(errno));
close(src_fd);
return -1;
}
return 0;
}
int daemon_get_devfd(const char *fscachedir, const char *tag)
{
char *cmd;
char cmdbuf[128];
int fd, ret;
if (!fscachedir)
return -1;
fd = open("/dev/cachefiles", O_RDWR);
if (fd < 0) {
printf("open /dev/cachefiles failed\n");
return -1;
}
snprintf(cmdbuf, sizeof(cmdbuf), "dir %s", fscachedir);
ret = write(fd, cmdbuf, strlen(cmdbuf));
if (ret < 0) {
printf("write dir failed, %d\n", errno);
goto error;
}
if (tag) {
snprintf(cmdbuf, sizeof(cmdbuf), "tag %s", tag);
ret = write(fd, cmdbuf, strlen(cmdbuf));
if (ret < 0) {
printf("write tag failed, %d\n", errno);
goto error;
}
}
cmd = "bind ondemand";
ret = write(fd, cmd, strlen(cmd));
if (ret < 0) {
printf("bind failed\n");
goto error;
}
return fd;
error:
close(fd);
return -1;
}