]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2013 eNovance SAS <licensing@enovance.com> | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | #include "include/page.h" | |
15 | ||
16 | #include "rgw_rest.h" | |
17 | #include "rgw_op.h" | |
18 | #include "rgw_rest_s3.h" | |
19 | #include "rgw_rest_metadata.h" | |
20 | #include "rgw_client_io.h" | |
21 | #include "common/errno.h" | |
22 | #include "common/strtol.h" | |
23 | #include "include/assert.h" | |
24 | ||
25 | #define dout_context g_ceph_context | |
26 | #define dout_subsys ceph_subsys_rgw | |
27 | ||
28 | const string RGWOp_Metadata_Get::name() { | |
29 | return "get_metadata"; | |
30 | } | |
31 | ||
32 | static inline void frame_metadata_key(req_state *s, string& out) { | |
33 | bool exists; | |
34 | string key = s->info.args.get("key", &exists); | |
35 | ||
36 | string section; | |
37 | if (!s->init_state.url_bucket.empty()) { | |
38 | section = s->init_state.url_bucket; | |
39 | } else { | |
40 | section = key; | |
41 | key.clear(); | |
42 | } | |
43 | ||
44 | out = section; | |
45 | ||
46 | if (!key.empty()) { | |
47 | out += string(":") + key; | |
48 | } | |
49 | } | |
50 | ||
51 | void RGWOp_Metadata_Get::execute() { | |
52 | string metadata_key; | |
53 | ||
54 | frame_metadata_key(s, metadata_key); | |
55 | ||
56 | /* Get keys */ | |
57 | http_ret = store->meta_mgr->get(metadata_key, s->formatter); | |
58 | if (http_ret < 0) { | |
59 | dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl; | |
60 | return; | |
61 | } | |
62 | ||
63 | http_ret = 0; | |
64 | } | |
65 | ||
66 | const string RGWOp_Metadata_List::name() { | |
67 | return "list_metadata"; | |
68 | } | |
69 | ||
70 | void RGWOp_Metadata_List::execute() { | |
71 | string metadata_key; | |
72 | ||
73 | frame_metadata_key(s, metadata_key); | |
74 | /* List keys */ | |
75 | void *handle; | |
76 | int max = 1000; | |
77 | ||
78 | http_ret = store->meta_mgr->list_keys_init(metadata_key, &handle); | |
79 | if (http_ret < 0) { | |
80 | dout(5) << "ERROR: can't get key: " << cpp_strerror(http_ret) << dendl; | |
81 | return; | |
82 | } | |
83 | ||
84 | bool truncated; | |
85 | ||
86 | s->formatter->open_array_section("keys"); | |
87 | ||
88 | do { | |
89 | list<string> keys; | |
90 | http_ret = store->meta_mgr->list_keys_next(handle, max, keys, &truncated); | |
91 | if (http_ret < 0) { | |
92 | dout(5) << "ERROR: lists_keys_next(): " << cpp_strerror(http_ret) | |
93 | << dendl; | |
94 | return; | |
95 | } | |
96 | ||
97 | for (list<string>::iterator iter = keys.begin(); iter != keys.end(); | |
98 | ++iter) { | |
99 | s->formatter->dump_string("key", *iter); | |
100 | } | |
101 | ||
102 | } while (truncated); | |
103 | ||
104 | s->formatter->close_section(); | |
105 | ||
106 | store->meta_mgr->list_keys_complete(handle); | |
107 | ||
108 | http_ret = 0; | |
109 | } | |
110 | ||
111 | int RGWOp_Metadata_Put::get_data(bufferlist& bl) { | |
112 | size_t cl = 0; | |
113 | char *data; | |
114 | int read_len; | |
115 | ||
116 | if (s->length) | |
117 | cl = atoll(s->length); | |
118 | if (cl) { | |
119 | data = (char *)malloc(cl + 1); | |
120 | if (!data) { | |
121 | return -ENOMEM; | |
122 | } | |
123 | read_len = recv_body(s, data, cl); | |
124 | if (cl != (size_t)read_len) { | |
125 | dout(10) << "recv_body incomplete" << dendl; | |
126 | } | |
127 | if (read_len < 0) { | |
128 | free(data); | |
129 | return read_len; | |
130 | } | |
131 | bl.append(data, read_len); | |
132 | } else { | |
133 | int chunk_size = CEPH_PAGE_SIZE; | |
134 | const char *enc = s->info.env->get("HTTP_TRANSFER_ENCODING"); | |
135 | if (!enc || strcmp(enc, "chunked")) { | |
136 | return -ERR_LENGTH_REQUIRED; | |
137 | } | |
138 | data = (char *)malloc(chunk_size); | |
139 | if (!data) { | |
140 | return -ENOMEM; | |
141 | } | |
142 | do { | |
143 | read_len = recv_body(s, data, chunk_size); | |
144 | if (read_len < 0) { | |
145 | free(data); | |
146 | return read_len; | |
147 | } | |
148 | bl.append(data, read_len); | |
149 | } while (read_len == chunk_size); | |
150 | } | |
151 | ||
152 | free(data); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | void RGWOp_Metadata_Put::execute() { | |
157 | bufferlist bl; | |
158 | string metadata_key; | |
159 | ||
160 | http_ret = get_data(bl); | |
161 | if (http_ret < 0) { | |
162 | return; | |
163 | } | |
164 | ||
31f18b77 FG |
165 | http_ret = do_aws4_auth_completion(); |
166 | if (http_ret < 0) { | |
167 | return; | |
7c673cae FG |
168 | } |
169 | ||
170 | frame_metadata_key(s, metadata_key); | |
171 | ||
172 | RGWMetadataHandler::sync_type_t sync_type = RGWMetadataHandler::APPLY_ALWAYS; | |
173 | ||
174 | bool mode_exists = false; | |
175 | string mode_string = s->info.args.get("update-type", &mode_exists); | |
176 | if (mode_exists) { | |
177 | bool parsed = RGWMetadataHandler::string_to_sync_type(mode_string, | |
178 | sync_type); | |
179 | if (!parsed) { | |
180 | http_ret = -EINVAL; | |
181 | return; | |
182 | } | |
183 | } | |
184 | ||
185 | http_ret = store->meta_mgr->put(metadata_key, bl, sync_type, | |
186 | &ondisk_version); | |
187 | if (http_ret < 0) { | |
188 | dout(5) << "ERROR: can't put key: " << cpp_strerror(http_ret) << dendl; | |
189 | return; | |
190 | } | |
191 | // translate internal codes into return header | |
192 | if (http_ret == STATUS_NO_APPLY) | |
193 | update_status = "skipped"; | |
194 | else if (http_ret == STATUS_APPLIED) | |
195 | update_status = "applied"; | |
196 | } | |
197 | ||
198 | void RGWOp_Metadata_Put::send_response() { | |
199 | int http_return_code = http_ret; | |
200 | if ((http_ret == STATUS_NO_APPLY) || (http_ret == STATUS_APPLIED)) | |
201 | http_return_code = STATUS_NO_CONTENT; | |
202 | set_req_state_err(s, http_return_code); | |
203 | dump_errno(s); | |
204 | stringstream ver_stream; | |
205 | ver_stream << "ver:" << ondisk_version.ver | |
206 | <<",tag:" << ondisk_version.tag; | |
207 | dump_header_if_nonempty(s, "RGWX_UPDATE_STATUS", update_status); | |
208 | dump_header_if_nonempty(s, "RGWX_UPDATE_VERSION", ver_stream.str()); | |
209 | end_header(s); | |
210 | } | |
211 | ||
212 | void RGWOp_Metadata_Delete::execute() { | |
213 | string metadata_key; | |
214 | ||
215 | frame_metadata_key(s, metadata_key); | |
216 | http_ret = store->meta_mgr->remove(metadata_key); | |
217 | if (http_ret < 0) { | |
218 | dout(5) << "ERROR: can't remove key: " << cpp_strerror(http_ret) << dendl; | |
219 | return; | |
220 | } | |
221 | http_ret = 0; | |
222 | } | |
223 | ||
224 | void RGWOp_Metadata_Lock::execute() { | |
225 | string duration_str, lock_id; | |
226 | string metadata_key; | |
227 | ||
228 | frame_metadata_key(s, metadata_key); | |
229 | ||
230 | http_ret = 0; | |
231 | ||
232 | duration_str = s->info.args.get("length"); | |
233 | lock_id = s->info.args.get("lock_id"); | |
234 | ||
235 | if ((!s->info.args.exists("key")) || | |
236 | (duration_str.empty()) || | |
237 | lock_id.empty()) { | |
238 | dout(5) << "Error invalid parameter list" << dendl; | |
239 | http_ret = -EINVAL; | |
240 | return; | |
241 | } | |
242 | ||
243 | int dur; | |
244 | string err; | |
245 | ||
246 | dur = strict_strtol(duration_str.c_str(), 10, &err); | |
247 | if (!err.empty() || dur <= 0) { | |
248 | dout(5) << "invalid length param " << duration_str << dendl; | |
249 | http_ret = -EINVAL; | |
250 | return; | |
251 | } | |
252 | http_ret = store->meta_mgr->lock_exclusive(metadata_key, make_timespan(dur), lock_id); | |
253 | if (http_ret == -EBUSY) | |
254 | http_ret = -ERR_LOCKED; | |
255 | } | |
256 | ||
257 | void RGWOp_Metadata_Unlock::execute() { | |
258 | string lock_id; | |
259 | string metadata_key; | |
260 | ||
261 | frame_metadata_key(s, metadata_key); | |
262 | ||
263 | http_ret = 0; | |
264 | ||
265 | lock_id = s->info.args.get("lock_id"); | |
266 | ||
267 | if ((!s->info.args.exists("key")) || | |
268 | lock_id.empty()) { | |
269 | dout(5) << "Error invalid parameter list" << dendl; | |
270 | http_ret = -EINVAL; | |
271 | return; | |
272 | } | |
273 | ||
274 | http_ret = store->meta_mgr->unlock(metadata_key, lock_id); | |
275 | } | |
276 | ||
277 | RGWOp *RGWHandler_Metadata::op_get() { | |
278 | if (s->info.args.exists("key")) | |
279 | return new RGWOp_Metadata_Get; | |
280 | else | |
281 | return new RGWOp_Metadata_List; | |
282 | } | |
283 | ||
284 | RGWOp *RGWHandler_Metadata::op_put() { | |
285 | return new RGWOp_Metadata_Put; | |
286 | } | |
287 | ||
288 | RGWOp *RGWHandler_Metadata::op_delete() { | |
289 | return new RGWOp_Metadata_Delete; | |
290 | } | |
291 | ||
292 | RGWOp *RGWHandler_Metadata::op_post() { | |
293 | if (s->info.args.exists("lock")) | |
294 | return new RGWOp_Metadata_Lock; | |
295 | else if (s->info.args.exists("unlock")) | |
296 | return new RGWOp_Metadata_Unlock; | |
297 | ||
298 | return NULL; | |
299 | } |