summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/request.h
blob: 6786e72b866c1c6d7036981cf72b266d064eed6a (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
#pragma once
#include <kernel/types.h>
#include <shared/ring.h>
#include <stdbool.h>
#include <stddef.h>

struct ReqQueue {
	VfsReq *head;
};

struct VfsBackend {
	/* amount of using references
	 *  VfsMount
	 *  VfsReq
	 *  Handle
	 * once it reaches 0, it'll never increase */
	size_t usehcnt; /* VfsMount */

	bool is_user;
	union {
		struct {
			Proc *handler;
			ReqQueue queue; /* kernel backends keep their own queues */
			/* amount of processes that control this backend
			 * won't ever increase if it becomes 0 */
			size_t provhcnt;
		} user;
		struct {
			void (*accept)(VfsReq *);
			void (*cleanup)(VfsBackend *);
			void *data;
		} kern;
	};
};

/* describes an in-progress vfs call */
struct VfsReq {
	enum vfs_op type;
	struct {
		bool kern; // if false: use .buf ; if true: use .buf_kern
		union {
			char __user *buf;
			char *buf_kern;
		};
		size_t len;
	} input;
	struct {
		char __user *buf;
		size_t len;
	} output;

	// TODO why doesn't this just have a reference to the handle?

	void __user *id; // handle.file.id
	long offset;
	int flags;

	Proc *caller;
	VfsBackend *backend;

	VfsReq *reqqueue_next;
};

/** Assigns the vfs_request to the caller, and dispatches the call */
void vfsreq_dispatchcopy(VfsReq);
void vfsreq_finish(VfsReq*, char __user *stored, long ret, int flags, Proc *handler);

static inline void vfsreq_finish_short(VfsReq *req, long ret) {
	vfsreq_finish(req, (void __user *)ret, ret, 0, NULL);
}

/** Try to accept an enqueued request */
void vfsback_useraccept(VfsReq *);

/** Decrements the "user" reference count. */
void vfsback_userdown(VfsBackend *);

/** Decrements the "provider" reference count. */
void vfsback_provdown(VfsBackend *);

void reqqueue_init(ReqQueue *q);
void reqqueue_join(ReqQueue *q, VfsReq *req);
VfsReq *reqqueue_pop(ReqQueue *q);

/** If there are any pending read requests, and the ring buffer isn't empty,
 *  fulfill them all with a single read. */
void reqqueue_ringreadall(ReqQueue *q, ring_t *r);