-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom-isa.c
More file actions
377 lines (327 loc) · 10.8 KB
/
com-isa.c
File metadata and controls
377 lines (327 loc) · 10.8 KB
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
/**
*
# Copyright (C) 2013 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
* target: compress & decompress data for storage
* use ISA-L library to tuning the compress
* zlib for decompress
*/
#include <ctype.h>
#include <sys/uio.h>
#include <assert.h>
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "com-isa.h"
#include "com-common.h"
#include "decompress.h"
#include "compress.h"
/**
* A compress & decompress translator
* use isa for data compress
* use zlib for data decompress
*/
typedef struct {
char *data;
int len;
}create_cbk_private_t;
int32_t
com_create_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno, fd_t *fd,
inode_t *inode, struct iatt *buf, struct iatt *preparent,
struct iatt *postparent, dict_t *xdata){
create_cbk_private_t *retcbk = (create_cbk_private_t *)cookie;
if (retcbk != NULL) {
int cfd = open(retcbk->data, O_CREAT|O_WRONLY, 0);
if (cfd == -1) {
printf("create file error for file %s\n", retcbk->data);
}
set_fd_to_pool(this, fd, cfd, retcbk->data);
}
STACK_UNWIND_STRICT(create, frame, op_ret, op_errno, fd, inode, buf, preparent, postparent, xdata);
return 0;
}
int32_t com_create(call_frame_t *frame, xlator_t *this, loc_t *loc,
int32_t flags, mode_t mode, mode_t umask, fd_t *fd, dict_t *xdata) {
data_t *dir_data = NULL;
dir_data = dict_get(this->next->options, "directory");
char *real_path = (char *) malloc(strlen(dir_data->data) + strlen(loc->path) + 1);
strcpy(real_path, dir_data->data);
strcpy(real_path+strlen(dir_data->data), loc->path);
printf("create file path %s file name %s dir info is %s realpath is:%s\n", loc->path,
loc->name, dir_data->data, real_path);
create_cbk_private_t *retcbk = (create_cbk_private_t *)malloc(sizeof(create_cbk_private_t));
retcbk->data = real_path;
retcbk->len = strlen(real_path);
STACK_WIND_COOKIE (frame, com_create_cbk, retcbk, FIRST_CHILD (this),
FIRST_CHILD (this)->fops->create, loc, flags, mode,
umask, fd, xdata);
return 0;
}
//file exsit, compressed file, just open for read
//mark current open files
int32_t com_open(call_frame_t *frame, xlator_t *this, loc_t *loc, int32_t flags,
fd_t *fd, dict_t *xdata) {
data_t *dir_data = NULL;
dir_data = dict_get(this->next->options, "directory");
printf("open file path %s file name %s\n", loc->path, loc->name);
if (flags & O_WRONLY) {
//only read allow
printf("Open for write not allowed\n");
STACK_UNWIND_STRICT(open, frame, -1, errno, NULL, NULL);
return 0;
} else {
char *real_path = (char *) malloc(sizeof(char) * (strlen(loc->path) + strlen(dir_data->data) + 1));
strcpy(real_path, dir_data->data);
strcpy(real_path + strlen(dir_data->data), loc->path);
printf("Open file %s to read but you need to decompress it\n", real_path);
//find the cached undecompress file, if finded open add pair for <fd_t, fd>
// if not find, decompress file and cached the fd
decompress_fd_t *dfd = get_fd_from_decoms(real_path);
if (dfd == NULL) {
int tfd = open(real_path, flags, 0);
if (tfd == -1)
goto out;
int tdfd = open_decompress_file(real_path, fd, tfd);
if (tdfd == -1)
goto out;
STACK_UNWIND_STRICT(open, frame, 0, errno, fd, NULL);
return 0;
} else {
STACK_UNWIND_STRICT(open, frame,0, errno, dfd->_cached_fd, NULL);
return 0;
}
}
out:
{
printf("Open file error\n");
STACK_UNWIND_STRICT(open, frame, -1, errno, NULL, NULL);
return 0;
}
}
int32_t com_flush_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno, dict_t *xdata) {
STACK_UNWIND_STRICT(flush, frame, op_ret, op_errno, xdata);
return 0;
}
//when flush code, to end the stream
int32_t com_flush(call_frame_t *frame, xlator_t *this, fd_t *fd, dict_t *xdata) {
compress_fd_t *cfd = get_fd_from_pool(fd);
if (cfd != NULL) {
buf_flush(cfd, 1);
STACK_WIND(frame, com_flush_cbk, FIRST_CHILD(this),
FIRST_CHILD(this)->fops->flush, fd, xdata);
return 0;
}
decompress_fd_t *dfd = get_fd_from_decoms_0(fd);
if (dfd != NULL) {
close(dfd->_rfd->_fd);
close(dfd->_sfd->_fd);
STACK_UNWIND_STRICT(flush, frame, 0, 0, NULL);
return 0;
}
}
//callback function for writev
int com_isa_writev_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
struct iatt *postbuf, dict_t *xdata) {
if (cookie == NULL) {
STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, prebuf, postbuf,
xdata);
return 0;
}
com_isa_cbk_t *ret = (com_isa_cbk_t *) cookie;
int i, writed = 0;
for (i = 0; i < ret->cnt; i++) {
writed += ret->array[i];
}
STACK_UNWIND_STRICT(writev, frame, writed, op_errno, prebuf, postbuf,
xdata);
return 0;
}
int
isa_com_fdstat (xlator_t *this, int fd, struct iatt *stbuf_p)
{
int ret = 0;
struct stat fstatbuf = {0, };
struct iatt stbuf = {0, };
ret = fstat (fd, &fstatbuf);
if (ret == -1)
goto out;
if (fstatbuf.st_nlink && !S_ISDIR (fstatbuf.st_mode))
fstatbuf.st_nlink--;
iatt_from_stat (&stbuf, &fstatbuf);
/*
ret = posix_fill_gfid_fd (this, fd, &stbuf);
if (ret)
gf_log_callingfn (this->name, GF_LOG_DEBUG, "failed to get gfid");
posix_fill_ino_from_gfid (this, &stbuf);
*/
if (stbuf_p)
*stbuf_p = stbuf;
out:
return ret;
}
int32_t
isa_com_fstat_cbk (call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
int32_t op_errno, struct iatt *buf, dict_t *xdata) {
STACK_UNWIND_STRICT (fstat, frame, op_ret, op_errno, buf, xdata);
}
int32_t
isa_com_fstat (call_frame_t *frame, xlator_t *this,
fd_t *fd, dict_t *xdata){
int32_t op_ret = -1;
int32_t op_errno = 0;
struct iatt buf = {0,};
decompress_fd_t *dfd = get_fd_from_decoms_0(fd);
if (dfd == NULL) {
//not decompress fd, so deal by posix translator
STACK_WIND(frame, isa_com_fstat_cbk, FIRST_CHILD(this),FIRST_CHILD(this)->fops->fstat, fd, xdata);
}
op_ret = isa_com_fdstat(this, dfd->_rfd->_fd, &buf);
if (op_ret == -1) {
op_errno =errno;
printf("fstat file error");
goto out;
}
op_ret = 0;
out:
STACK_UNWIND_STRICT (fstat, frame, op_ret, op_errno, &buf, NULL);
return 0;
}
int decom_isa_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
off_t offset, uint32_t flags, dict_t *xdata) {
struct iatt stbuf = {0,};
decompress_fd_t *dfd = get_fd_from_decoms_0(fd);
if (dfd == NULL) {
printf("Read Error for decompress fd is NULL\n");
} else {
int op_errno = 0, op_ret = 0;
struct iobuf *iobuf = NULL;
struct iovec *vec = NULL;
struct iobref *iobref = NULL;
iobuf = iobuf_get2(this->ctx->iobuf_pool, size);
if (!iobuf) {
op_errno = EINVAL;
goto out;
}
printf("Read from %s the offset is %ld size is %ld\n", dfd->_rfd->_real_path, offset, size);
pthread_mutex_lock(dfd->_lock);
op_ret = pread(dfd->_rfd->_fd, iobuf->ptr, size, offset);
pthread_mutex_unlock(dfd->_lock);
if (op_ret == -1) {
printf("Read Error %d\n", errno);
op_errno = errno;
goto out;
}
vec = (struct iovec *)malloc(sizeof(struct iovec));
vec->iov_base = iobuf->ptr;
vec->iov_len = op_ret;
iobref = iobref_new();
iobref_add(iobref, iobuf);
isa_com_fdstat(this, dfd->_rfd->_fd, &stbuf);
out:
printf("Readed data len is %d\n", op_ret);
//return
STACK_UNWIND_STRICT (readv, frame, op_ret, op_errno,
vec, 1, &stbuf, iobref, NULL);
if (iobref)
iobref_unref (iobref);
if (iobuf)
iobuf_unref (iobuf);
}
return 0;
}
//call compress data function & call next translator writev
int com_isa_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
struct iovec *vector, int32_t count, off_t offset, uint32_t flags,
struct iobref *iobref, dict_t *xdata) {
struct iatt preop = {0,};
struct iatt postop = {0,};
uint64_t tmpfd = 0;
compress_fd_t *cfd = NULL;
fd_ctx_get(fd, this, &tmpfd);
cfd = (compress_fd_t *) (long)tmpfd;
//compress_fd_t *cfd = get_fd_from_pool(fd);
if (cfd == NULL) {
printf("Write file error for file is not created\n");
STACK_UNWIND_STRICT(writev, frame, -1, 0, NULL, NULL, NULL);
} else {
UINT32 i, sum = 0;
printf("Write count %d len %d\n", count, vector[0].iov_len);
for (i=0; i<count; i++) {
sum += add_to_buffer(cfd, vector[i].iov_base, vector[i].iov_len);
}
printf("Write to file offset %d\n", offset);
STACK_UNWIND_STRICT(writev, frame, sum, 0, &preop, &postop, NULL);
}
return 0;
}
int init(xlator_t *this) {
//init buf
init_inner_buffer();
init_com_file_pool();
init_decom_file_pool();
com_isa_private_t *priv = NULL;
if (!this->children) {
gf_log("com-isa", GF_LOG_ERROR, "FATAL: com-isa should have no child");
return -1;
}
if (!this->parents) {
gf_log("this->name", GF_LOG_WARNING,
"WARN: wrong volume config, check volume config file");
}
priv = GF_CALLOC(sizeof(com_isa_private_t), 1, 0);
if (!priv)
return -1;
priv->com_isa_write = 1;
priv->decom_isa_read = 1;
data_t *data = NULL;
data = dict_get(this->options, "com-isa-write");
if (data) {
if (gf_string2boolean(data->data, &priv->com_isa_write) == -1) {
gf_log(this->name, GF_LOG_ERROR,
"FATAL: config com-isa-write can only be boolean");
return -1;
}
}
data = dict_get(this->options, "com-isa-read");
if (data) {
if (gf_string2boolean(data->data, &priv->decom_isa_read) == -1) {
gf_log(this->name, GF_LOG_ERROR,
"FATAL: config decom-isa-read can only be boolean");
return -1;
}
}
this->private = priv;
gf_log("com-isa", GF_LOG_DEBUG, "this tranlator loaded");
return 0;
}
void fini(xlator_t *this) {
com_isa_private_t *priv = this->private;
if (!priv)
return;
this->private = NULL;
GF_FREE(priv);
return;
}
struct xlator_fops fops = { .readv = decom_isa_readv,
.create = com_create, .writev = com_isa_writev,
.flush = com_flush, .open = com_open, .fstat = isa_com_fstat
};
struct xlator_cbks cbks;
struct volume_options options[] = { { .key = { "com-isa-write" }, .type =
GF_OPTION_TYPE_BOOL }, { .key = { "decom-isa-read" }, .type =
GF_OPTION_TYPE_BOOL }, { .key = { NULL } } };