This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
fs.c
298 lines (222 loc) · 6.46 KB
/
fs.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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* This file contains code for implementation of 'str' filesystem. It is used by pseudo filesystems
* (/proc, /dev, /sys) and by tmpfs.
*/
#include <asm/fcntl.h>
#include <asm/mman.h>
#include <asm/unistd.h>
#include <errno.h>
#include <linux/fcntl.h>
#include "pal.h"
#include "pal_error.h"
#include "shim_fs.h"
#include "shim_internal.h"
int str_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
struct shim_str_data* data = dent->data;
/* when str file is opened, it must have non-NULL `data` field */
if (!dent->data)
return -ENOENT;
REF_INC(data->ref_count);
hdl->type = TYPE_STR;
/* note that if file was just created, then `str` and `len` are guaranteed to be NULL
* and zero */
hdl->info.str.data = data;
hdl->info.str.ptr = data->str;
if (flags & O_APPEND)
hdl->info.str.ptr += data->len;
hdl->dentry = dent;
hdl->flags = flags;
return 0;
}
int str_dput(struct shim_dentry* dent) {
struct shim_str_data* data = dent->data;
if (!data || REF_DEC(data->ref_count) > 1)
return 0;
if (data->str) {
free(data->str);
data->str = NULL;
}
data->len = 0;
data->buf_size = 0;
free(dent->data);
dent->data = NULL;
return 0;
}
int str_close(struct shim_handle* hdl) {
assert(hdl->type == TYPE_STR);
if (hdl->flags & (O_WRONLY | O_RDWR)) {
int ret = str_flush(hdl);
if (ret < 0)
return ret;
}
str_dput(hdl->dentry);
if (hdl->info.str.data) {
free(hdl->info.str.data->str);
free(hdl->info.str.data);
hdl->info.str.data = NULL;
}
return 0;
}
ssize_t str_read(struct shim_handle* hdl, void* buf, size_t count) {
ssize_t ret = 0;
if (!(hdl->acc_mode & MAY_READ)) {
ret = -EACCES;
goto out;
}
assert(hdl->type == TYPE_STR);
struct shim_str_handle* strhdl = &hdl->info.str;
assert(hdl->dentry);
assert(strhdl->data);
struct shim_str_data* data = strhdl->data;
if (!data->str) {
log_warning("str_read: str_data has no str");
ret = 0;
goto out;
}
if (!strhdl->ptr)
strhdl->ptr = data->str;
off_t offset = strhdl->ptr - data->str;
if (data->len <= offset) {
ret = 0;
goto out;
}
size_t remain = data->len - offset;
if (count >= remain) {
memcpy(buf, strhdl->ptr, remain);
strhdl->ptr += remain;
ret = remain;
goto out;
}
memcpy(buf, strhdl->ptr, count);
strhdl->ptr += count;
ret = count;
out:
return ret;
}
static ssize_t str_maybe_expand_buf(struct shim_str_handle* strhdl, size_t size) {
struct shim_str_data* data = strhdl->data;
if (!data->str || size > data->buf_size) {
size_t new_size = 0;
if (data->str) {
new_size = data->buf_size ?: 1;
while (size > new_size) {
new_size *= 2;
}
} else {
new_size = size;
}
char* new_data_str = calloc(1, new_size);
if (!new_data_str)
return -ENOMEM;
if (data->str) {
memcpy(new_data_str, data->str, data->len);
free(data->str);
}
strhdl->ptr = new_data_str + (strhdl->ptr - data->str);
data->str = new_data_str;
data->buf_size = new_size;
}
return 0;
}
ssize_t str_write(struct shim_handle* hdl, const void* buf, size_t count) {
ssize_t ret = 0;
if (!(hdl->acc_mode & MAY_WRITE))
return -EACCES;
struct shim_str_handle* strhdl = &hdl->info.str;
assert(strhdl->data);
struct shim_str_data* data = strhdl->data;
ret = str_maybe_expand_buf(strhdl, strhdl->ptr - data->str + count);
if (ret < 0)
return ret;
memcpy(strhdl->ptr, buf, count);
strhdl->ptr += count;
data->dirty = true;
if (strhdl->ptr >= data->str + data->len)
data->len = strhdl->ptr - data->str;
return count;
}
off_t str_seek(struct shim_handle* hdl, off_t offset, int whence) {
struct shim_str_handle* strhdl = &hdl->info.str;
assert(hdl->dentry);
assert(strhdl->data);
struct shim_str_data* data = strhdl->data;
switch (whence) {
case SEEK_SET:
if (offset < 0)
return -EINVAL;
strhdl->ptr = data->str + offset;
break;
case SEEK_CUR:
if (strhdl->ptr + offset < data->str)
return -EINVAL;
strhdl->ptr += offset;
break;
case SEEK_END:
if (data->len + offset < 0)
return -EINVAL;
strhdl->ptr = data->str + data->len + offset;
break;
}
return strhdl->ptr - data->str;
}
int str_flush(struct shim_handle* hdl) {
struct shim_str_handle* strhdl = &hdl->info.str;
assert(hdl->dentry);
assert(strhdl->data);
struct shim_str_data* data = strhdl->data;
if (!data->dirty)
return 0;
if (!data->modify)
return -EACCES;
return data->modify(hdl);
}
int str_truncate(struct shim_handle* hdl, off_t len) {
int ret = 0;
if (!(hdl->acc_mode & MAY_WRITE))
return -EACCES;
struct shim_str_handle* strhdl = &hdl->info.str;
assert(strhdl->data);
struct shim_str_data* data = strhdl->data;
if (!data->str && len == 0)
return 0;
ret = str_maybe_expand_buf(strhdl, (size_t)len);
if (ret < 0)
return ret;
data->len = len;
data->dirty = true;
return ret;
}
off_t str_poll(struct shim_handle* hdl, int poll_type) {
assert(hdl->type == TYPE_STR);
struct shim_str_handle* strhdl = &hdl->info.str;
struct shim_str_data* data = strhdl->data;
assert(data);
if (poll_type == FS_POLL_SZ)
return data->len;
off_t ret = 0;
if (poll_type & FS_POLL_RD) {
if (data->len > 0) {
assert(data->str);
if (!strhdl->ptr || strhdl->ptr < (data->str + data->len))
ret |= FS_POLL_RD;
}
}
if (poll_type & FS_POLL_WR)
ret |= FS_POLL_WR;
return ret;
}
struct shim_fs_ops str_fs_ops = {
.close = &str_close,
.read = &str_read,
.write = &str_write,
.seek = &str_seek,
.flush = &str_flush,
.truncate = &str_truncate,
.poll = &str_poll,
};
struct shim_d_ops str_d_ops = {
.open = &str_open,
.dput = &str_dput,
};