1 #include <sys/mman.h>
2 #include <sys/socket.h>
3 #include <sys/types.h>
4 #include <ctype.h>
5 #include <dirent.h>
6 #include <err.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <libqrtr.h>
10 #include <limits.h>
11 #include <signal.h>
12 #include <stdarg.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "qmi_rmtfs.h"
21 #include "util.h"
22 #include "rmtfs.h"
23
24 #define RMTFS_QMI_SERVICE 14
25 #define RMTFS_QMI_VERSION 1
26 #define RMTFS_QMI_INSTANCE 0
27
28 static struct rmtfs_mem *rmem;
29 static sig_atomic_t sig_int_count;
30
31 static bool dbgprintf_enabled;
dbgprintf(const char * fmt,...)32 static void dbgprintf(const char *fmt, ...)
33 {
34 va_list ap;
35
36 if (!dbgprintf_enabled)
37 return;
38
39 va_start(ap, fmt);
40 vprintf(fmt, ap);
41 va_end(ap);
42 }
43
qmi_result_error(struct rmtfs_qmi_result * result,unsigned error)44 static void qmi_result_error(struct rmtfs_qmi_result *result, unsigned error)
45 {
46 /* Only propagate initial error */
47 if (result->result == QMI_RMTFS_RESULT_FAILURE)
48 return;
49
50 result->result = QMI_RMTFS_RESULT_FAILURE;
51 result->error = error;
52 }
53
rmtfs_open(int sock,const struct qrtr_packet * pkt)54 static void rmtfs_open(int sock, const struct qrtr_packet *pkt)
55 {
56 struct rmtfs_open_resp resp = {};
57 struct rmtfs_open_req req = {};
58 DEFINE_QRTR_PACKET(resp_buf, 256);
59 struct rmtfd *rmtfd;
60 unsigned int txn;
61 ssize_t len;
62 int caller_id = -1;
63 int ret;
64
65 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
66 QMI_RMTFS_OPEN, rmtfs_open_req_ei);
67 if (ret < 0) {
68 qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
69 goto respond;
70 }
71
72 rmtfd = storage_open(pkt->node, req.path);
73 if (!rmtfd) {
74 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
75 goto respond;
76 }
77
78 caller_id = storage_get_caller_id(rmtfd);
79 resp.caller_id = caller_id;
80 resp.caller_id_valid = true;
81
82 respond:
83 dbgprintf("[RMTFS] open %s => %d (%d:%d)\n",
84 req.path, caller_id, resp.result.result, resp.result.error);
85
86 len = qmi_encode_message(&resp_buf,
87 QMI_RESPONSE, QMI_RMTFS_OPEN, txn, &resp,
88 rmtfs_open_resp_ei);
89 if (len < 0) {
90 fprintf(stderr, "[RMTFS] failed to encode open-response: %s\n",
91 strerror(-len));
92 return;
93 }
94
95 ret = qrtr_sendto(sock, pkt->node, pkt->port,
96 resp_buf.data, resp_buf.data_len);
97 if (ret < 0)
98 fprintf(stderr, "[RMTFS] failed to send open-response: %s\n",
99 strerror(-ret));
100 }
101
rmtfs_close(int sock,const struct qrtr_packet * pkt)102 static void rmtfs_close(int sock, const struct qrtr_packet *pkt)
103 {
104 struct rmtfs_close_resp resp = {};
105 struct rmtfs_close_req req = {};
106 DEFINE_QRTR_PACKET(resp_buf, 256);
107 struct rmtfd *rmtfd;
108 unsigned int txn;
109 ssize_t len;
110 int ret;
111
112 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
113 QMI_RMTFS_CLOSE, rmtfs_close_req_ei);
114 if (ret < 0) {
115 qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
116 goto respond;
117 }
118
119 rmtfd = storage_get(pkt->node, req.caller_id);
120 if (!rmtfd) {
121 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
122 goto respond;
123 }
124
125 storage_close(rmtfd);
126 rmtfs_mem_free(rmem);
127
128 respond:
129 dbgprintf("[RMTFS] close %s => %d (%d:%d)\n",
130 req.caller_id, resp.result.result, resp.result.error);
131
132 len = qmi_encode_message(&resp_buf,
133 QMI_RESPONSE, QMI_RMTFS_CLOSE, txn, &resp,
134 rmtfs_close_resp_ei);
135 if (len < 0) {
136 fprintf(stderr, "[RMTFS] failed to encode close-response: %s\n",
137 strerror(-len));
138 return;
139 }
140
141 ret = qrtr_sendto(sock, pkt->node, pkt->port,
142 resp_buf.data, resp_buf.data_len);
143 if (ret < 0)
144 fprintf(stderr, "[RMTFS] failed to send close-response: %s\n",
145 strerror(-ret));
146 }
147
rmtfs_iovec(int sock,struct qrtr_packet * pkt)148 static void rmtfs_iovec(int sock, struct qrtr_packet *pkt)
149 {
150 struct rmtfs_iovec_entry *entries;
151 struct rmtfs_iovec_resp resp = {};
152 struct rmtfs_iovec_req req = {};
153 DEFINE_QRTR_PACKET(resp_buf, 256);
154 struct rmtfd *rmtfd;
155 uint32_t caller_id = 0;
156 size_t num_entries = 0;
157 off_t sector_base;
158 uint8_t is_write;
159 off_t phys_base;
160 uint8_t force = 0;
161 unsigned txn;
162 off_t offset;
163 ssize_t len;
164 ssize_t n;
165 char buf[SECTOR_SIZE];
166 int ret;
167 int i;
168 int j;
169
170 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
171 QMI_RMTFS_RW_IOVEC, rmtfs_iovec_req_ei);
172 if (ret < 0) {
173 qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
174 goto respond;
175 }
176
177 caller_id = req.caller_id;
178 is_write = req.direction;
179 entries = req.iovec;
180 num_entries = req.iovec_len;
181 force = req.is_force_sync;
182
183 rmtfd = storage_get(pkt->node, caller_id);
184 if (!rmtfd) {
185 fprintf(stderr, "[RMTFS] iovec request for non-existing caller\n");
186 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
187 goto respond;
188 }
189
190 for (i = 0; i < num_entries; i++) {
191 phys_base = entries[i].phys_offset;
192 sector_base = entries[i].sector_addr * SECTOR_SIZE;
193 offset = 0;
194
195 for (j = 0; j < entries[i].num_sector; j++) {
196 if (is_write) {
197 n = rmtfs_mem_read(rmem, phys_base + offset, buf, SECTOR_SIZE);
198 if (n == SECTOR_SIZE)
199 n = storage_pwrite(rmtfd, buf, n, sector_base + offset);
200 } else {
201 n = storage_pread(rmtfd, buf, SECTOR_SIZE, sector_base + offset);
202 if (n >= 0) {
203 if (n < SECTOR_SIZE)
204 memset(buf + n, 0, SECTOR_SIZE - n);
205 n = rmtfs_mem_write(rmem, phys_base + offset, buf, SECTOR_SIZE);
206 }
207 }
208
209 if (n != SECTOR_SIZE) {
210 fprintf(stderr, "[RMTFS] failed to %s sector %d\n",
211 is_write ? "write" : "read", entries[i].sector_addr + j);
212 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
213 goto respond;
214 }
215
216 offset += SECTOR_SIZE;
217 }
218 }
219
220 respond:
221 dbgprintf("[RMTFS] iovec %d, %sforced => (%d:%d)\n", caller_id, force ? "" : "not ",
222 resp.result.result, resp.result.error);
223 for (i = 0; i < num_entries; i++) {
224 dbgprintf("[RMTFS] %s %d:%d 0x%x\n", is_write ? "write" : "read",
225 entries[i].sector_addr,
226 entries[i].num_sector,
227 entries[i].phys_offset);
228 }
229
230 len = qmi_encode_message(&resp_buf,
231 QMI_RESPONSE, QMI_RMTFS_RW_IOVEC, txn, &resp,
232 rmtfs_iovec_resp_ei);
233 if (len < 0) {
234 fprintf(stderr, "[RMTFS] failed to encode iovec-response: %s\n",
235 strerror(-len));
236 return;
237 }
238
239 ret = qrtr_sendto(sock, pkt->node, pkt->port,
240 resp_buf.data, resp_buf.data_len);
241 if (ret < 0)
242 fprintf(stderr, "[RMTFS] failed to send iovec-response: %s\n",
243 strerror(-ret));
244 }
245
rmtfs_alloc_buf(int sock,struct qrtr_packet * pkt)246 static void rmtfs_alloc_buf(int sock, struct qrtr_packet *pkt)
247 {
248 struct rmtfs_alloc_buf_resp resp = {};
249 struct rmtfs_alloc_buf_req req = {};
250 DEFINE_QRTR_PACKET(resp_buf, 256);
251 uint32_t alloc_size = 0;
252 uint32_t caller_id = 0;
253 int64_t address = 0;
254 unsigned txn;
255 ssize_t len;
256 int ret;
257
258 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
259 QMI_RMTFS_ALLOC_BUFF, rmtfs_alloc_buf_req_ei);
260 if (ret < 0) {
261 qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
262 goto respond;
263 }
264
265 caller_id = req.caller_id;
266 alloc_size = req.buff_size;
267
268 address = rmtfs_mem_alloc(rmem, alloc_size);
269 if (address < 0) {
270 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
271 goto respond;
272 }
273
274 resp.buff_address = address;
275 resp.buff_address_valid = true;
276 respond:
277 dbgprintf("[RMTFS] alloc %d, %d => 0x%lx (%d:%d)\n", caller_id, alloc_size, address, resp.result.result, resp.result.error);
278
279 len = qmi_encode_message(&resp_buf,
280 QMI_RESPONSE, QMI_RMTFS_ALLOC_BUFF, txn, &resp,
281 rmtfs_alloc_buf_resp_ei);
282 if (len < 0) {
283 fprintf(stderr, "[RMTFS] failed to encode alloc-buf-response: %s\n",
284 strerror(-len));
285 return;
286 }
287
288 ret = qrtr_sendto(sock, pkt->node, pkt->port,
289 resp_buf.data, resp_buf.data_len);
290 if (ret < 0)
291 fprintf(stderr, "[RMTFS] failed to send alloc-buf-response: %s\n",
292 strerror(-ret));
293 }
294
rmtfs_get_dev_error(int sock,struct qrtr_packet * pkt)295 static void rmtfs_get_dev_error(int sock, struct qrtr_packet *pkt)
296 {
297 struct rmtfs_dev_error_resp resp = {};
298 struct rmtfs_dev_error_req req = {};
299 DEFINE_QRTR_PACKET(resp_buf, 256);
300 struct rmtfd *rmtfd;
301 unsigned txn;
302 ssize_t len;
303 int ret;
304
305 ret = qmi_decode_message(&req, &txn, pkt, QMI_REQUEST,
306 QMI_RMTFS_GET_DEV_ERROR,
307 rmtfs_dev_error_req_ei);
308 if (ret < 0) {
309 qmi_result_error(&resp.result, QMI_RMTFS_ERR_MALFORMED_MSG);
310 goto respond;
311 }
312
313 rmtfd = storage_get(pkt->node, req.caller_id);
314 if (rmtfd) {
315 qmi_result_error(&resp.result, QMI_RMTFS_ERR_INTERNAL);
316 goto respond;
317 }
318
319 resp.status = storage_get_error(rmtfd);
320 resp.status_valid = true;
321
322 respond:
323 dbgprintf("[RMTFS] dev_error %d => %d (%d:%d)\n", req.caller_id, resp.status, resp.result.result, resp.result.error);
324
325 len = qmi_encode_message(&resp_buf,
326 QMI_RESPONSE, QMI_RMTFS_GET_DEV_ERROR, txn,
327 &resp, rmtfs_dev_error_resp_ei);
328 if (len < 0) {
329 fprintf(stderr, "[RMTFS] failed to encode dev-error-response: %s\n",
330 strerror(-len));
331 return;
332 }
333
334 ret = qrtr_sendto(sock, pkt->node, pkt->port,
335 resp_buf.data, resp_buf.data_len);
336 if (ret < 0)
337 fprintf(stderr, "[RMTFS] failed to send dev-error-response: %s\n",
338 strerror(-ret));
339 }
340
rmtfs_bye(uint32_t node)341 static int rmtfs_bye(uint32_t node)
342 {
343 dbgprintf("[RMTFS] bye from %d\n", node);
344
345 return 0;
346 }
347
rmtfs_del_client(uint32_t node,uint32_t port)348 static int rmtfs_del_client(uint32_t node, uint32_t port)
349 {
350 dbgprintf("[RMTFS] del_client %d:%d\n", node, port);
351
352 return 0;
353 }
354
handle_rmtfs(int sock)355 static int handle_rmtfs(int sock)
356 {
357 struct sockaddr_qrtr sq;
358 struct qrtr_packet pkt;
359 unsigned int msg_id;
360 socklen_t sl;
361 char buf[4096];
362 int ret;
363
364 sl = sizeof(sq);
365 ret = recvfrom(sock, buf, sizeof(buf), 0, (void *)&sq, &sl);
366 if (ret < 0) {
367 ret = -errno;
368 if (ret != -ENETRESET)
369 fprintf(stderr, "[RMTFS] recvfrom failed: %d\n", ret);
370 return ret;
371 }
372
373 dbgprintf("[RMTFS] packet; from: %d:%d\n", sq.sq_node, sq.sq_port);
374
375 ret = qrtr_decode(&pkt, buf, ret, &sq);
376 if (ret < 0) {
377 fprintf(stderr, "[RMTFS] unable to decode qrtr packet\n");
378 return ret;
379 }
380
381 switch (pkt.type) {
382 case QRTR_TYPE_BYE:
383 return rmtfs_bye(pkt.node);
384 case QRTR_TYPE_DEL_CLIENT:
385 return rmtfs_del_client(pkt.node, pkt.port);
386 case QRTR_TYPE_DATA:
387 ret = qmi_decode_header(&pkt, &msg_id);
388 if (ret < 0)
389 return ret;
390
391 switch (msg_id) {
392 case QMI_RMTFS_OPEN:
393 rmtfs_open(sock, &pkt);
394 break;
395 case QMI_RMTFS_CLOSE:
396 rmtfs_close(sock, &pkt);
397 break;
398 case QMI_RMTFS_RW_IOVEC:
399 rmtfs_iovec(sock, &pkt);
400 break;
401 case QMI_RMTFS_ALLOC_BUFF:
402 rmtfs_alloc_buf(sock, &pkt);
403 break;
404 case QMI_RMTFS_GET_DEV_ERROR:
405 rmtfs_get_dev_error(sock, &pkt);
406 break;
407 default:
408 fprintf(stderr, "[RMTFS] Unknown request: %d\n", msg_id);
409 break;
410 }
411
412 return 0;
413 }
414
415 return ret;
416 }
417
418 static int sig_int_count;
419
run_rmtfs(int rprocfd)420 static int run_rmtfs(int rprocfd)
421 {
422 bool sig_int_handled = false;
423 int rmtfs_fd;
424 fd_set rfds;
425 char done;
426 int nfds;
427 int ret;
428
429 rmtfs_fd = qrtr_open(RMTFS_QMI_SERVICE);
430 if (rmtfs_fd < 0) {
431 fprintf(stderr, "failed to create qrtr socket\n");
432 return rmtfs_fd;
433 }
434
435 dbgprintf("registering services\n");
436
437 ret = qrtr_publish(rmtfs_fd, RMTFS_QMI_SERVICE,
438 RMTFS_QMI_VERSION, RMTFS_QMI_INSTANCE);
439 if (ret < 0) {
440 fprintf(stderr, "failed to publish rmtfs service");
441 return ret;
442 }
443
444 if (rprocfd >= 0)
445 rproc_start();
446
447 for (;;) {
448 if (rprocfd >= 0 && sig_int_count == 1 && !sig_int_handled) {
449 rproc_stop();
450 sig_int_handled = true;
451 } else if (sig_int_count > 1) {
452 break;
453 }
454
455 FD_ZERO(&rfds);
456 FD_SET(rmtfs_fd, &rfds);
457 if (rprocfd >= 0)
458 FD_SET(rprocfd, &rfds);
459 nfds = MAX(rmtfs_fd, rprocfd) + 1;
460
461 ret = select(nfds, &rfds, NULL, NULL, NULL);
462 if (ret < 0 && errno != EINTR)
463 break;
464 else if (ret < 0 && errno == EINTR)
465 continue;
466
467 if (rprocfd >= 0 && FD_ISSET(rprocfd, &rfds)) {
468 ret = read(rprocfd, &done, 1);
469 if (!ret || done == 'Y')
470 break;
471 }
472
473 if (FD_ISSET(rmtfs_fd, &rfds)) {
474 ret = handle_rmtfs(rmtfs_fd);
475 if (ret == -ENETRESET)
476 break;
477 }
478 }
479
480 close(rmtfs_fd);
481
482 return ret;
483 }
484
sig_int_handler(int signo __unused)485 static void sig_int_handler(int signo __unused)
486 {
487 sig_int_count++;
488 }
489
main(int argc,char ** argv)490 int main(int argc, char **argv)
491 {
492 struct sigaction action;
493 bool use_partitions = false;
494 bool read_only = false;
495 int rprocfd = -1;
496 int ret;
497 int option;
498 const char *storage_root = NULL;
499
500 while ((option = getopt(argc, argv, "o:Prsv")) != -1) {
501 switch (option) {
502 /*
503 * -o sets the directory where EFS images are stored,
504 * or sets the directory from where raw EFS partitions
505 * can be picked by-name when used with -P option.
506 */
507 case 'o':
508 storage_root = optarg;
509 break;
510
511 /* -P to find and use raw EFS partitions */
512 case 'P':
513 use_partitions = true;
514 break;
515
516 /* -r to avoid writing to storage */
517 case 'r':
518 read_only = true;
519 break;
520
521 /* enable sync for the mss rproc instance */
522 case 's':
523 rprocfd = rproc_init();
524 if (rprocfd < 0) {
525 fprintf(stderr, "Failed to get rprocfd\n");
526 return 1;
527 }
528
529 break;
530
531 /* -v is for verbose */
532 case 'v':
533 dbgprintf_enabled = 1;
534 break;
535
536 case '?':
537 fprintf(stderr, "Unknown option: -%c\n", option);
538 return 1;
539 }
540 }
541
542 sigemptyset(&action.sa_mask);
543 action.sa_handler = sig_int_handler;
544 action.sa_flags = 0;
545
546 sigaction(SIGINT, &action, NULL);
547 sigaction(SIGTERM, &action, NULL);
548
549 rmem = rmtfs_mem_open();
550 if (!rmem)
551 return 1;
552
553 ret = storage_init(storage_root, read_only, use_partitions);
554 if (ret) {
555 fprintf(stderr, "failed to initialize storage system\n");
556 goto close_rmtfs_mem;
557 }
558
559 do {
560 ret = run_rmtfs(rprocfd);
561 } while (ret == -ENETRESET);
562
563 storage_exit();
564 close_rmtfs_mem:
565 rmtfs_mem_close(rmem);
566
567 return 0;
568 }
569