-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.c
190 lines (158 loc) · 3.95 KB
/
storage.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
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "co.h"
#include "wrapper.h"
#include "storage.h"
#include "debug.h"
extern char storage_root[PATH_BUF];
/*
* convert a pathname, add prefix meta_root
*/
void get_chunk_path(char *dest, char *src)
{
sprintf(dest, "%s/%s", storage_root, src);
}
/*
* write the data to disk
* return the error information
*/
void *do_write(void *write_arg)
{
PDEBUG("[do_write thread]\n");
co_write_in *arg = (co_write_in*)write_arg;
char *buf = arg->buf.buf_val;
size_t size = arg->buf.buf_len;
off_t offset = arg->offset;
char path[PATH_BUF];
get_chunk_path(path, arg->chunkid);
int fd;
static int ret = 0;
ret = wrapper_open_mode(path, O_CREAT | O_WRONLY, 0644, &fd);
if(ret < 0)
return (void*)&ret;
ret = wrapper_pwrite(fd, buf, size, offset);
if(ret < 0)
return (void*)&ret;
ret = wrapper_close(fd);
return (void*)&ret;
}
/*
* copy data using rpc
*/
int rpccall_co_write(co_write_in *arg, co_write_out *res, char *ipp)
{
CLIENT *clnt;
clnt = clnt_create(ipp, CO_PROG, CO_VERS, "tcp");
if(!clnt) {
PDEBUG("%s: create client handle failed\n", ipp);
return -EREMOTE;
}
if(co_write_1(arg, res, clnt) != RPC_SUCCESS) {
PDEBUG(clnt_sperror(clnt, "call co_write_1 failed"));
return -EREMOTE;
}
clnt_destroy(clnt);
return 0;
}
int copy_data(co_write_in *arg, co_write_out *res, char *ipp)
{
int ret = rpccall_co_write(arg, res, ipp);
if(ret < 0)
return ret;
return res->err;
}
/*
* copy the data to the backup osd
* return the error information
*/
void *do_copy(void *copy_arg)
{
PDEBUG("[do_copy thread]\n");
static int ret = 0;
co_write_in *arg = (co_write_in*)copy_arg;
co_write_out res;
res.err = 0;
char ipp[IP_BUF];
get_addr(ipp, arg->ip);
arg->ip = 0;
ret = copy_data(arg, &res, ipp);
return (void*)&ret;
}
/*
* return the bytes written or errno
*/
bool_t co_write_1_svc(co_write_in *arg, co_write_out *res, struct svc_req *rqst)
{
PDEBUG("[co_write_1_svc] chunkid = %s, size = %u, offset = %u\n",
arg->chunkid, arg->buf.buf_len, arg->offset);
res->err = 0;
int *ret;
pthread_t tid1, tid2;
if(pthread_create(&tid1, NULL, do_write, (void*)arg)) {
return FALSE;
}
if(arg->ip){
if(pthread_create(&tid2, NULL, do_copy, (void*)arg)) {
return FALSE;
}
pthread_join(tid2, (void**)&ret);
res->err = *ret;
IS_ERR(res->err);
}
pthread_join(tid1, (void**)&ret);
res->err = *ret;
return TRUE;
}
/*
* return the bytes read or errno
*/
bool_t co_read_1_svc(struct co_read_in *arg, struct co_read_out *res, struct svc_req *rqst)
{
PDEBUG("[co_read_1_svc] chunkid = %s, size = %u, offset = %u\n",
arg->chunkid, arg->size, arg->offset);
size_t size = arg->size;
off_t offset = arg->offset;
char path[PATH_BUF];
get_chunk_path(path, arg->chunkid);
res->err = 0;
res->buf.buf_val = (char*)malloc(size);
if(!res->buf.buf_val) {
res->err = -ENOMEM;
res->buf.buf_len = 0;
res->buf.buf_val = NULL;
return TRUE;
}
int fd;
res->err = wrapper_open(path, O_RDONLY, &fd);
if(res->err < 0) {
res->err = -errno;
res->buf.buf_len = 0;
res->buf.buf_val = NULL;
return 0;
}
res->err = wrapper_pread(fd, res->buf.buf_val, size, offset);
IS_ERR(res->err);
res->buf.buf_len = size;
res->err = wrapper_close(fd);
return TRUE;
}
/*
* unlink the data file
*/
bool_t co_unlink_1_svc(struct co_unlink_in *arg, struct co_unlink_out *res, struct svc_req *rqst)
{
PDEBUG("[co_unlink_1_svc] chunkid = %s\n", arg->chunkid);
char path[PATH_BUF];
get_chunk_path(path, arg->chunkid);
res->err = 0;
res->err = wrapper_unlink(path);
return TRUE;
}
int co_prog_1_freeresult(SVCXPRT *transp, xdrproc_t xdr_result, caddr_t result)
{
xdr_free(xdr_result, result);
return 1;
}