forked from sagimnl/pmfs2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinu.c
297 lines (249 loc) · 5.56 KB
/
kinu.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
// SPDX-License-Identifier: GPL-2.0
/* See module.c for license details. */
#include <sys/uio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <b-minmax.h>
#include "pmfs2.h"
/* iter */
#define iterate_iovec(i, n, __v, __p, skip, STEP) { \
size_t left; \
size_t wanted = n; \
__p = i->iov; \
__v.iov_len = min(n, __p->iov_len - skip); \
if (likely(__v.iov_len)) { \
__v.iov_base = __p->iov_base + skip; \
left = (STEP); \
__v.iov_len -= left; \
skip += __v.iov_len; \
n -= __v.iov_len; \
} else { \
left = 0; \
} \
while (unlikely(!left && n)) { \
__p++; \
__v.iov_len = min(n, __p->iov_len); \
if (unlikely(!__v.iov_len)) \
continue; \
__v.iov_base = __p->iov_base; \
left = (STEP); \
__v.iov_len -= left; \
skip = __v.iov_len; \
n -= __v.iov_len; \
} \
n = wanted - n; \
}
#define iterate_and_advance(i, n, v, I, B, K) { \
size_t skip = i->iov_offset; \
{ \
const struct iovec *iov; \
struct iovec v; \
iterate_iovec(i, n, v, iov, skip, (I)) \
if (skip == iov->iov_len) { \
iov++; \
skip = 0; \
} \
i->nr_segs -= iov - i->iov; \
i->iov = iov; \
} \
i->count -= n; \
i->iov_offset = skip; \
}
static ulong memcpy_skip(void *to, const void *from, ulong n)
{
memcpy(to, from, n);
return 0;
}
size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
{
const char *from = addr;
if (unlikely(bytes > i->count))
bytes = i->count;
if (unlikely(!bytes))
return 0;
iterate_and_advance(i, bytes, v,
memcpy_skip(v.iov_base,
(from += v.iov_len) - v.iov_len,
v.iov_len),
memcpy_to_page(v.bv_page, v.bv_offset,
(from += v.bv_len) - v.bv_len,
v.bv_len),
memcpy(v.iov_base,
(from += v.iov_len) - v.iov_len, v.iov_len)
)
return bytes;
}
static ulong memcpy_to_pmem_skip(void *to, const void *from, ulong n)
{
memcpy_to_pmem(to, from, n);
return 0;
}
size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
{
char *to = addr;
if (unlikely(bytes > i->count))
bytes = i->count;
if (unlikely(!bytes))
return 0;
iterate_and_advance(i, bytes, v,
memcpy_to_pmem_skip((to += v.iov_len) - v.iov_len,
v.iov_base, v.iov_len),
memcpy_from_page((to += v.bv_len) - v.bv_len,
v.bv_page, v.bv_offset, v.bv_len),
memcpy((to += v.iov_len) - v.iov_len,
v.iov_base, v.iov_len)
)
return bytes;
}
static ulong memset_skip(void *to, int val, ulong n)
{
memset(to, val, n);
return 0;
}
size_t iov_iter_zero(size_t bytes, struct iov_iter *i)
{
if (unlikely(bytes > i->count))
bytes = i->count;
if (unlikely(!bytes))
return 0;
iterate_and_advance(i, bytes, v,
memset_skip(v.iov_base, 0, v.iov_len),
memzero_page(v.bv_page, v.bv_offset, v.bv_len),
memset(v.iov_base, 0, v.iov_len)
)
return bytes;
}
void iov_iter_advance(struct iov_iter *i, size_t size)
{
iterate_and_advance(i, size, v, 0, 0, 0)
}
void iov_iter_init(struct iov_iter *i, const struct iovec *iov,
unsigned long nr_segs, size_t count)
{
i->iov = iov;
i->nr_segs = nr_segs;
i->iov_offset = 0;
i->count = count;
}
int iov_iter_init_single(struct iov_iter *i, void *buf, size_t len,
struct iovec *iov)
{
iov->iov_base = buf;
iov->iov_len = len;
iov_iter_init(i, iov, 1, len);
return 0;
}
ulong iov_iter_count(struct iov_iter *i)
{
return i->count;
}
void iov_iter_truncate(struct iov_iter *i, ulong count)
{
if (i->count > count)
i->count = count;
}
void *pmfs2_malloc(size_t size)
{
return zus_malloc(size);
}
void *pmfs2_calloc(size_t nmemb, size_t size)
{
return zus_calloc(nmemb, size);
}
void *pmfs2_zalloc(size_t size)
{
return pmfs2_calloc(1, size);
}
void pmfs2_free(void *ptr)
{
zus_free(ptr);
}
static const char *_pr_tag(enum pmfs2_trace_channel ch)
{
switch (ch) {
case PMFS2_TRACE_INFO:
return "info";
case PMFS2_TRACE_WARN:
return "warn";
case PMFS2_TRACE_ERROR:
return "error";
case PMFS2_TRACE_VFS:
return "vfs";
case PMFS2_TRACE_RW:
return "rw";
case PMFS2_TRACE_RECON:
return "recon";
case PMFS2_TRACE_XATTR:
return "xattr";
case PMFS2_TRACE_VERBOS:
return "verbos";
default:
break;
}
return "";
}
static const char *_pr_file(const char *path)
{
const char *base = strrchr(path, '/');
return likely(base) ? (base + 1) : path;
}
void pmfs2_pr(int dbg, enum pmfs2_trace_channel ch, const char *file, int line,
const char *func, const char *fmt, ...)
{
va_list ap;
FILE *fp = stdout;
if (dbg && !ZUS_DBGPRNT)
return;
flockfile(fp);
fprintf(fp, "%s [%s:%d %s] ", _pr_tag(ch), _pr_file(file), line, func);
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
funlockfile(fp);
}
int spin_lock_init(struct spinlock *sl)
{
return pthread_spin_init(&sl->sl, 0);
}
void spin_lock_fini(struct spinlock *sl)
{
pthread_spin_destroy(&sl->sl);
}
void spin_lock(struct spinlock *sl)
{
int err;
err = pthread_spin_lock(&sl->sl);
BUG_ON(err);
}
void spin_unlock(struct spinlock *sl)
{
int err;
err = pthread_spin_unlock(&sl->sl);
BUG_ON(err);
}
int spin_trylock(struct spinlock *sl)
{
int err;
err = pthread_spin_trylock(&sl->sl);
BUG_ON(!err && (err != EBUSY));
return -err;
}
int mutex_init(struct mutex *m)
{
return pthread_mutex_init(&m->m, NULL);
}
void mutex_fini(struct mutex *m)
{
pthread_mutex_destroy(&m->m);
}
void mutex_lock(struct mutex *m)
{
pthread_mutex_lock(&m->m);
}
void mutex_unlock(struct mutex *m)
{
pthread_mutex_unlock(&m->m);
}