-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathlibos_fs_pseudo.c
618 lines (520 loc) · 17.6 KB
/
libos_fs_pseudo.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2021 Intel Corporation
* Paweł Marczewski <[email protected]>
*/
/*
* This file contains implementation of the "pseudo" filesystem.
*
* We store a pointer to a `pseudo_node` object in `inode->data`. Note that since `inode->data`
* never changes, we do NOT take `inode->lock` when reading it.
*/
#include "libos_fs_pseudo.h"
#include "libos_lock.h"
#include "stat.h"
LISTP_TYPE(pseudo_node) g_pseudo_roots = LISTP_INIT;
/* Array of nodes by ID. Used for restoring a node from checkpoint (we send only node ID). We assume
* that all Gramine processes within a single instance create exactly the same set of nodes, in the
* same order, during initialization. */
static struct pseudo_node* g_pseudo_nodes[PSEUDO_MAX_NODES];
static unsigned int g_pseudo_node_count;
/* Find a root node with given name. */
static struct pseudo_node* pseudo_find_root(const char* name) {
struct pseudo_node* node;
LISTP_FOR_EACH_ENTRY(node, &g_pseudo_roots, siblings) {
if (node->name && strcmp(name, node->name) == 0) {
return node;
}
}
log_debug("Cannot find pseudofs node: %s", name);
return NULL;
}
/* Find a `pseudo_node` for given dentry. */
static struct pseudo_node* pseudo_find(struct libos_dentry* dent) {
assert(locked(&g_dcache_lock));
if (!dent->parent) {
/* This is the filesystem root */
return pseudo_find_root(dent->mount->uri);
}
assert(dent->parent != NULL);
assert(dent->parent->inode);
struct pseudo_node* parent_node = dent->parent->inode->data;
if (parent_node == NULL)
return NULL;
/* Look for a child node with matching name */
assert(parent_node->type == PSEUDO_DIR);
struct pseudo_node* node;
LISTP_FOR_EACH_ENTRY(node, &parent_node->dir.children, siblings) {
/* The node might have `name`, `name_exists`, or both. */
bool match;
if (node->name && node->name_exists) {
match = !strcmp(dent->name, node->name) && node->name_exists(dent->parent, dent->name);
} else if (node->name) {
match = !strcmp(dent->name, node->name);
} else if (node->name_exists) {
match = node->name_exists(dent->parent, dent->name);
} else {
match = false;
}
if (match)
return node;
}
return NULL;
}
static int pseudo_icheckpoint(struct libos_inode* inode, void** out_data, size_t* out_size) {
unsigned int* id = malloc(sizeof(*id));
if (!id)
return -ENOMEM;
struct pseudo_node* node = inode->data;
*id = node->id;
*out_data = (void*)id;
*out_size = sizeof(*id);
return 0;
}
static int pseudo_irestore(struct libos_inode* inode, void* data) {
unsigned int* id = data;
if (*id >= g_pseudo_node_count) {
log_error("Invalid pseudo_node id: %u", *id);
return -EINVAL;
}
struct pseudo_node* node = g_pseudo_nodes[*id];
assert(node);
inode->data = node;
return 0;
}
static int pseudo_mount(struct libos_mount_params* params, void** mount_data) {
if (!params->uri)
return -EINVAL;
__UNUSED(mount_data);
return 0;
}
static int pseudo_open(struct libos_handle* hdl, struct libos_dentry* dent, int flags) {
assert(locked(&g_dcache_lock));
assert(dent->inode);
struct pseudo_node* node = dent->inode->data;
int ret;
switch (node->type) {
case PSEUDO_DIR:
hdl->type = TYPE_PSEUDO;
/* This is a directory handle, so it will be initialized by `dentry_open`. */
break;
case PSEUDO_LINK:
return -EINVAL;
case PSEUDO_STR: {
char* str;
size_t len;
if (node->str.load) {
ret = node->str.load(dent, &str, &len);
if (ret < 0)
return ret;
if (len > 0)
assert(str);
} else {
len = 0;
str = NULL;
}
hdl->type = TYPE_STR;
hdl->seekable = true;
mem_file_init(&hdl->info.str.mem, str, len);
hdl->pos = 0;
break;
}
case PSEUDO_DEV: {
hdl->type = TYPE_DEV;
hdl->seekable = true;
hdl->pos = 0;
if (node->dev.dev_ops.open) {
ret = node->dev.dev_ops.open(hdl, dent, flags);
if (ret < 0)
return ret;
}
break;
}
}
return 0;
}
static int pseudo_lookup(struct libos_dentry* dent) {
assert(locked(&g_dcache_lock));
assert(!dent->inode);
struct pseudo_node* node = pseudo_find(dent);
if (!node)
return -ENOENT;
mode_t type;
switch (node->type) {
case PSEUDO_DIR:
type = S_IFDIR;
break;
case PSEUDO_LINK:
type = S_IFLNK;
break;
case PSEUDO_STR:
type = S_IFREG;
break;
case PSEUDO_DEV:
type = S_IFCHR;
break;
default:
BUG();
}
struct libos_inode* inode = get_new_inode(dent->mount, type, node->perm);
if (!inode)
return -ENOMEM;
inode->data = node;
dent->inode = inode;
return 0;
}
static int count_nlink(const char* name, void* arg) {
__UNUSED(name);
size_t* nlink = arg;
(*nlink)++;
return 0;
}
static uint64_t makedev(unsigned int major, unsigned int minor) {
uint64_t dev;
dev = (((uint64_t)(major & 0x00000fffu)) << 8);
dev |= (((uint64_t)(major & 0xfffff000u)) << 32);
dev |= (((uint64_t)(minor & 0x000000ffu)) << 0);
dev |= (((uint64_t)(minor & 0xffffff00u)) << 12);
return dev;
}
static int pseudo_istat(struct libos_dentry* dent, struct libos_inode* inode, struct stat* buf) {
memset(buf, 0, sizeof(*buf));
buf->st_dev = 1;
buf->st_mode = inode->type | inode->perm;
buf->st_uid = inode->uid;
buf->st_gid = inode->gid;
struct pseudo_node* node = inode->data;
switch (node->type) {
case PSEUDO_DIR: {
/*
* Count the actual number of children for `nlink`. This is not very efficient, but
* libraries like hwloc check `nlink` in some places.
*
* Note that we might not be holding `g_dcache_lock` here, so we cannot call
* `pseudo_readdir`.
*/
size_t nlink = 2; // Initialize to 2 for `.` and parent
struct pseudo_node* child_node;
LISTP_FOR_EACH_ENTRY(child_node, &node->dir.children, siblings) {
if (child_node->name) {
/* If `name_exists` callback is provided, check it. */
if (!child_node->name_exists || node->name_exists(dent, child_node->name))
nlink++;
}
if (child_node->list_names) {
int ret = child_node->list_names(dent, &count_nlink, &nlink);
if (ret < 0)
return ret;
}
}
buf->st_nlink = nlink;
break;
}
case PSEUDO_DEV:
buf->st_rdev = makedev(node->dev.major, node->dev.minor);
buf->st_nlink = 1;
break;
default:
buf->st_nlink = 1;
break;
}
return 0;
}
static int pseudo_stat(struct libos_dentry* dent, struct stat* buf) {
assert(locked(&g_dcache_lock));
assert(dent->inode);
return pseudo_istat(dent, dent->inode, buf);
}
static int pseudo_hstat(struct libos_handle* handle, struct stat* buf) {
/* Note: derefence handle->dentry in general has to be protected by handle->lock as it could
* change due to rename. However, as pseudo-fs does not support rename we can safely omit it
* here (or push it on the numerous callers of fs_op->hstat). */
return pseudo_istat(handle->dentry, handle->inode, buf);
}
static int pseudo_unlink(struct libos_dentry* dent) {
__UNUSED(dent);
return -EACCES;
}
static int pseudo_follow_link(struct libos_dentry* dent, char** out_target) {
assert(locked(&g_dcache_lock));
assert(dent->inode);
char* target;
struct pseudo_node* node = dent->inode->data;
if (node->type != PSEUDO_LINK)
return -EINVAL;
if (node->link.follow_link) {
int ret = node->link.follow_link(dent, &target);
if (ret < 0)
return ret;
*out_target = target;
return 0;
}
assert(node->link.target);
target = strdup(node->link.target);
if (!target)
return -ENOMEM;
*out_target = target;
return 0;
}
static int pseudo_readdir(struct libos_dentry* dent, readdir_callback_t callback, void* arg) {
assert(locked(&g_dcache_lock));
assert(dent->inode);
int ret;
struct pseudo_node* parent_node = dent->inode->data;
if (parent_node->type != PSEUDO_DIR)
return -ENOTDIR;
struct pseudo_node* node;
LISTP_FOR_EACH_ENTRY(node, &parent_node->dir.children, siblings) {
if (node->name) {
/* If `name_exists` callback is provided, check it. */
if (!node->name_exists || node->name_exists(dent, node->name)) {
ret = callback(node->name, arg);
if (ret < 0)
return ret;
}
}
if (node->list_names) {
ret = node->list_names(dent, callback, arg);
if (ret < 0)
return ret;
}
}
return 0;
}
static ssize_t pseudo_read(struct libos_handle* hdl, void* buf, size_t size, file_off_t* pos) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
assert(hdl->type == TYPE_STR);
lock(&hdl->lock);
ssize_t ret = mem_file_read(&hdl->info.str.mem, *pos, buf, size);
if (ret > 0)
*pos += ret;
unlock(&hdl->lock);
return ret;
}
case PSEUDO_DEV:
if (!node->dev.dev_ops.read)
return -EACCES;
return node->dev.dev_ops.read(hdl, buf, size);
default:
return -ENOSYS;
}
}
static ssize_t pseudo_write(struct libos_handle* hdl, const void* buf, size_t size,
file_off_t* pos) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
assert(hdl->type == TYPE_STR);
ssize_t ret;
lock(&hdl->lock);
struct libos_mem_file* mem = &hdl->info.str.mem;
if (node->str.save) {
/* If there's a `save` method, we want to invoke it, and the write should replace
* existing content. */
ret = node->str.save(hdl->dentry, buf, size);
if (ret < 0)
goto out;
ret = mem_file_truncate(mem, 0);
if (ret < 0)
goto out;
*pos = 0;
}
ret = mem_file_write(mem, *pos, buf, size);
if (ret < 0)
goto out;
*pos += ret;
out:
unlock(&hdl->lock);
return ret;
}
case PSEUDO_DEV:
if (!node->dev.dev_ops.write)
return -EACCES;
return node->dev.dev_ops.write(hdl, buf, size);
default:
return -ENOSYS;
}
}
static file_off_t pseudo_seek(struct libos_handle* hdl, file_off_t offset, int whence) {
file_off_t ret;
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
lock(&hdl->lock);
file_off_t pos = hdl->pos;
ret = generic_seek(pos, hdl->info.str.mem.size, offset, whence, &pos);
if (ret == 0) {
hdl->pos = pos;
ret = pos;
}
unlock(&hdl->lock);
return ret;
}
case PSEUDO_DEV:
if (!node->dev.dev_ops.seek)
return -EACCES;
return node->dev.dev_ops.seek(hdl, offset, whence);
default:
return -ENOSYS;
}
}
static int pseudo_truncate(struct libos_handle* hdl, file_off_t size) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR:
assert(hdl->type == TYPE_STR);
lock(&hdl->lock);
int ret = mem_file_truncate(&hdl->info.str.mem, size);
unlock(&hdl->lock);
return ret;
case PSEUDO_DEV:
if (!node->dev.dev_ops.truncate)
return -EINVAL;
return node->dev.dev_ops.truncate(hdl, size);
default:
return -ENOSYS;
}
}
static int pseudo_flush(struct libos_handle* hdl) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_DEV:
if (!node->dev.dev_ops.flush)
return -EINVAL;
return node->dev.dev_ops.flush(hdl);
default:
return -ENOSYS;
}
}
static int pseudo_close(struct libos_handle* hdl) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
lock(&hdl->lock);
mem_file_destroy(&hdl->info.str.mem);
unlock(&hdl->lock);
return 0;
}
case PSEUDO_DEV:
if (!node->dev.dev_ops.close)
return 0;
return node->dev.dev_ops.close(hdl);
default:
return 0;
}
}
static int pseudo_poll(struct libos_handle* hdl, int events, int* out_events) {
struct pseudo_node* node = hdl->inode->data;
switch (node->type) {
case PSEUDO_STR: {
assert(hdl->type == TYPE_STR);
lock(&hdl->lock);
int ret = mem_file_poll(&hdl->info.str.mem, hdl->pos, events, out_events);
unlock(&hdl->lock);
return ret;
}
case PSEUDO_DEV: {
if (node->dev.dev_ops.poll)
return node->dev.dev_ops.poll(hdl, events, out_events);
/* if no handle-specific poll, then use a generic one */
*out_events = 0;
if (node->dev.dev_ops.read)
*out_events |= events & (POLLIN | POLLRDNORM);
if (node->dev.dev_ops.write)
*out_events |= events & (POLLOUT | POLLWRNORM);
return 0;
}
default:
return -ENOSYS;
}
}
int pseudo_parse_ulong(const char* str, unsigned long max_value, unsigned long* out_value) {
unsigned long value;
const char* end;
if (str_to_ulong(str, 10, &value, &end) < 0 || *end != '\0' || value > max_value)
return -1;
/* no leading zeroes */
if (str[0] == '0' && str[1] != '\0')
return -1;
*out_value = value;
return 0;
}
static struct pseudo_node* pseudo_add_ent(struct pseudo_node* parent_node, const char* name,
enum pseudo_type type) {
if (g_pseudo_node_count >= PSEUDO_MAX_NODES) {
log_error("Pseudo node limit reached, increase PSEUDO_MAX_NODES");
abort();
}
unsigned int id = g_pseudo_node_count++;
struct pseudo_node* node = calloc(1, sizeof(*node));
if (!node) {
log_error("Out of memory when allocating pseudofs node");
abort();
}
node->name = name;
node->type = type;
node->id = id;
if (parent_node) {
assert(parent_node->type == PSEUDO_DIR);
node->parent = parent_node;
LISTP_ADD(node, &parent_node->dir.children, siblings);
} else {
LISTP_ADD(node, &g_pseudo_roots, siblings);
}
g_pseudo_nodes[id] = node;
return node;
}
struct pseudo_node* pseudo_add_root_dir(const char* name) {
return pseudo_add_dir(/*parent_node=*/NULL, name);
}
struct pseudo_node* pseudo_add_dir(struct pseudo_node* parent_node, const char* name) {
struct pseudo_node* node = pseudo_add_ent(parent_node, name, PSEUDO_DIR);
node->perm = PSEUDO_PERM_DIR;
return node;
}
struct pseudo_node* pseudo_add_link(struct pseudo_node* parent_node, const char* name,
int (*follow_link)(struct libos_dentry*, char**)) {
struct pseudo_node* node = pseudo_add_ent(parent_node, name, PSEUDO_LINK);
node->link.follow_link = follow_link;
node->perm = PSEUDO_PERM_LINK;
return node;
}
struct pseudo_node* pseudo_add_str(struct pseudo_node* parent_node, const char* name,
int (*load)(struct libos_dentry*, char**, size_t*)) {
struct pseudo_node* node = pseudo_add_ent(parent_node, name, PSEUDO_STR);
node->str.load = load;
node->perm = PSEUDO_PERM_FILE_R;
return node;
}
struct pseudo_node* pseudo_add_dev(struct pseudo_node* parent_node, const char* name) {
struct pseudo_node* node = pseudo_add_ent(parent_node, name, PSEUDO_DEV);
node->perm = PSEUDO_PERM_FILE_R;
return node;
}
struct libos_fs_ops pseudo_fs_ops = {
.mount = &pseudo_mount,
.hstat = &pseudo_hstat,
.read = &pseudo_read,
.write = &pseudo_write,
.seek = &pseudo_seek,
.truncate = &pseudo_truncate,
.close = &pseudo_close,
.flush = &pseudo_flush,
.poll = &pseudo_poll,
};
struct libos_d_ops pseudo_d_ops = {
.open = &pseudo_open,
.lookup = &pseudo_lookup,
.readdir = &pseudo_readdir,
.stat = &pseudo_stat,
.unlink = &pseudo_unlink,
.follow_link = &pseudo_follow_link,
.icheckpoint = &pseudo_icheckpoint,
.irestore = &pseudo_irestore,
};
struct libos_fs pseudo_builtin_fs = {
.name = "pseudo",
.fs_ops = &pseudo_fs_ops,
.d_ops = &pseudo_d_ops,
};