Commit | Line | Data |
---|---|---|
f24e9980 SW |
1 | #include "ceph_debug.h" |
2 | ||
3 | #include <linux/err.h> | |
4 | #include <linux/highmem.h> | |
5 | #include <linux/mm.h> | |
6 | #include <linux/pagemap.h> | |
7 | #include <linux/slab.h> | |
8 | #include <linux/uaccess.h> | |
9 | ||
10 | #include "super.h" | |
11 | #include "osd_client.h" | |
12 | #include "messenger.h" | |
13 | #include "decode.h" | |
4e7a5dcd | 14 | #include "auth.h" |
f24e9980 | 15 | |
c16e7869 SW |
16 | #define OSD_OP_FRONT_LEN 4096 |
17 | #define OSD_OPREPLY_FRONT_LEN 512 | |
0d59ab81 | 18 | |
f24e9980 | 19 | const static struct ceph_connection_operations osd_con_ops; |
422d2cb8 YS |
20 | static int __kick_requests(struct ceph_osd_client *osdc, |
21 | struct ceph_osd *kickosd); | |
f24e9980 SW |
22 | |
23 | static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd); | |
24 | ||
25 | /* | |
26 | * Implement client access to distributed object storage cluster. | |
27 | * | |
28 | * All data objects are stored within a cluster/cloud of OSDs, or | |
29 | * "object storage devices." (Note that Ceph OSDs have _nothing_ to | |
30 | * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply | |
31 | * remote daemons serving up and coordinating consistent and safe | |
32 | * access to storage. | |
33 | * | |
34 | * Cluster membership and the mapping of data objects onto storage devices | |
35 | * are described by the osd map. | |
36 | * | |
37 | * We keep track of pending OSD requests (read, write), resubmit | |
38 | * requests to different OSDs when the cluster topology/data layout | |
39 | * change, or retry the affected requests when the communications | |
40 | * channel with an OSD is reset. | |
41 | */ | |
42 | ||
43 | /* | |
44 | * calculate the mapping of a file extent onto an object, and fill out the | |
45 | * request accordingly. shorten extent as necessary if it crosses an | |
46 | * object boundary. | |
47 | * | |
48 | * fill osd op in request message. | |
49 | */ | |
50 | static void calc_layout(struct ceph_osd_client *osdc, | |
51 | struct ceph_vino vino, struct ceph_file_layout *layout, | |
52 | u64 off, u64 *plen, | |
53 | struct ceph_osd_request *req) | |
54 | { | |
55 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | |
56 | struct ceph_osd_op *op = (void *)(reqhead + 1); | |
57 | u64 orig_len = *plen; | |
58 | u64 objoff, objlen; /* extent in object */ | |
59 | u64 bno; | |
60 | ||
61 | reqhead->snapid = cpu_to_le64(vino.snap); | |
62 | ||
63 | /* object extent? */ | |
64 | ceph_calc_file_object_mapping(layout, off, plen, &bno, | |
65 | &objoff, &objlen); | |
66 | if (*plen < orig_len) | |
67 | dout(" skipping last %llu, final file extent %llu~%llu\n", | |
68 | orig_len - *plen, off, *plen); | |
69 | ||
70 | sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno); | |
71 | req->r_oid_len = strlen(req->r_oid); | |
72 | ||
73 | op->extent.offset = cpu_to_le64(objoff); | |
74 | op->extent.length = cpu_to_le64(objlen); | |
75 | req->r_num_pages = calc_pages_for(off, *plen); | |
76 | ||
77 | dout("calc_layout %s (%d) %llu~%llu (%d pages)\n", | |
78 | req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages); | |
79 | } | |
80 | ||
f24e9980 SW |
81 | /* |
82 | * requests | |
83 | */ | |
415e49a9 | 84 | void ceph_osdc_release_request(struct kref *kref) |
f24e9980 | 85 | { |
415e49a9 SW |
86 | struct ceph_osd_request *req = container_of(kref, |
87 | struct ceph_osd_request, | |
88 | r_kref); | |
89 | ||
90 | if (req->r_request) | |
91 | ceph_msg_put(req->r_request); | |
92 | if (req->r_reply) | |
93 | ceph_msg_put(req->r_reply); | |
0d59ab81 | 94 | if (req->r_con_filling_msg) { |
350b1c32 | 95 | dout("release_request revoking pages %p from con %p\n", |
0d59ab81 YS |
96 | req->r_pages, req->r_con_filling_msg); |
97 | ceph_con_revoke_message(req->r_con_filling_msg, | |
98 | req->r_reply); | |
99 | ceph_con_put(req->r_con_filling_msg); | |
350b1c32 | 100 | } |
415e49a9 SW |
101 | if (req->r_own_pages) |
102 | ceph_release_page_vector(req->r_pages, | |
103 | req->r_num_pages); | |
104 | ceph_put_snap_context(req->r_snapc); | |
105 | if (req->r_mempool) | |
106 | mempool_free(req, req->r_osdc->req_mempool); | |
107 | else | |
108 | kfree(req); | |
f24e9980 SW |
109 | } |
110 | ||
111 | /* | |
112 | * build new request AND message, calculate layout, and adjust file | |
113 | * extent as needed. | |
114 | * | |
115 | * if the file was recently truncated, we include information about its | |
116 | * old and new size so that the object can be updated appropriately. (we | |
117 | * avoid synchronously deleting truncated objects because it's slow.) | |
118 | * | |
119 | * if @do_sync, include a 'startsync' command so that the osd will flush | |
120 | * data quickly. | |
121 | */ | |
122 | struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, | |
123 | struct ceph_file_layout *layout, | |
124 | struct ceph_vino vino, | |
125 | u64 off, u64 *plen, | |
126 | int opcode, int flags, | |
127 | struct ceph_snap_context *snapc, | |
128 | int do_sync, | |
129 | u32 truncate_seq, | |
130 | u64 truncate_size, | |
131 | struct timespec *mtime, | |
132 | bool use_mempool, int num_reply) | |
133 | { | |
134 | struct ceph_osd_request *req; | |
135 | struct ceph_msg *msg; | |
136 | struct ceph_osd_request_head *head; | |
137 | struct ceph_osd_op *op; | |
138 | void *p; | |
0c948992 | 139 | int num_op = 1 + do_sync; |
f24e9980 | 140 | size_t msg_size = sizeof(*head) + num_op*sizeof(*op); |
c16e7869 | 141 | int i; |
f24e9980 SW |
142 | |
143 | if (use_mempool) { | |
144 | req = mempool_alloc(osdc->req_mempool, GFP_NOFS); | |
145 | memset(req, 0, sizeof(*req)); | |
146 | } else { | |
147 | req = kzalloc(sizeof(*req), GFP_NOFS); | |
148 | } | |
149 | if (req == NULL) | |
a79832f2 | 150 | return NULL; |
f24e9980 | 151 | |
f24e9980 SW |
152 | req->r_osdc = osdc; |
153 | req->r_mempool = use_mempool; | |
415e49a9 | 154 | kref_init(&req->r_kref); |
f24e9980 SW |
155 | init_completion(&req->r_completion); |
156 | init_completion(&req->r_safe_completion); | |
157 | INIT_LIST_HEAD(&req->r_unsafe_item); | |
158 | req->r_flags = flags; | |
159 | ||
160 | WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0); | |
161 | ||
c16e7869 SW |
162 | /* create reply message */ |
163 | if (use_mempool) | |
164 | msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); | |
165 | else | |
166 | msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, | |
bb257664 | 167 | OSD_OPREPLY_FRONT_LEN); |
a79832f2 | 168 | if (!msg) { |
c16e7869 | 169 | ceph_osdc_put_request(req); |
a79832f2 | 170 | return NULL; |
c16e7869 SW |
171 | } |
172 | req->r_reply = msg; | |
173 | ||
174 | /* create request message; allow space for oid */ | |
f24e9980 SW |
175 | msg_size += 40; |
176 | if (snapc) | |
177 | msg_size += sizeof(u64) * snapc->num_snaps; | |
178 | if (use_mempool) | |
8f3bc053 | 179 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); |
f24e9980 | 180 | else |
bb257664 | 181 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size); |
a79832f2 | 182 | if (!msg) { |
f24e9980 | 183 | ceph_osdc_put_request(req); |
a79832f2 | 184 | return NULL; |
f24e9980 SW |
185 | } |
186 | msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP); | |
187 | memset(msg->front.iov_base, 0, msg->front.iov_len); | |
188 | head = msg->front.iov_base; | |
189 | op = (void *)(head + 1); | |
190 | p = (void *)(op + num_op); | |
191 | ||
192 | req->r_request = msg; | |
193 | req->r_snapc = ceph_get_snap_context(snapc); | |
194 | ||
195 | head->client_inc = cpu_to_le32(1); /* always, for now. */ | |
196 | head->flags = cpu_to_le32(flags); | |
197 | if (flags & CEPH_OSD_FLAG_WRITE) | |
198 | ceph_encode_timespec(&head->mtime, mtime); | |
199 | head->num_ops = cpu_to_le16(num_op); | |
200 | op->op = cpu_to_le16(opcode); | |
201 | ||
202 | /* calculate max write size */ | |
203 | calc_layout(osdc, vino, layout, off, plen, req); | |
204 | req->r_file_layout = *layout; /* keep a copy */ | |
205 | ||
206 | if (flags & CEPH_OSD_FLAG_WRITE) { | |
207 | req->r_request->hdr.data_off = cpu_to_le16(off); | |
208 | req->r_request->hdr.data_len = cpu_to_le32(*plen); | |
209 | op->payload_len = cpu_to_le32(*plen); | |
210 | } | |
0c948992 YS |
211 | op->extent.truncate_size = cpu_to_le64(truncate_size); |
212 | op->extent.truncate_seq = cpu_to_le32(truncate_seq); | |
f24e9980 SW |
213 | |
214 | /* fill in oid */ | |
215 | head->object_len = cpu_to_le32(req->r_oid_len); | |
216 | memcpy(p, req->r_oid, req->r_oid_len); | |
217 | p += req->r_oid_len; | |
218 | ||
f24e9980 SW |
219 | if (do_sync) { |
220 | op++; | |
221 | op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC); | |
222 | } | |
223 | if (snapc) { | |
224 | head->snap_seq = cpu_to_le64(snapc->seq); | |
225 | head->num_snaps = cpu_to_le32(snapc->num_snaps); | |
226 | for (i = 0; i < snapc->num_snaps; i++) { | |
227 | put_unaligned_le64(snapc->snaps[i], p); | |
228 | p += sizeof(u64); | |
229 | } | |
230 | } | |
231 | ||
232 | BUG_ON(p > msg->front.iov_base + msg->front.iov_len); | |
6f863e71 SW |
233 | msg_size = p - msg->front.iov_base; |
234 | msg->front.iov_len = msg_size; | |
235 | msg->hdr.front_len = cpu_to_le32(msg_size); | |
f24e9980 SW |
236 | return req; |
237 | } | |
238 | ||
239 | /* | |
240 | * We keep osd requests in an rbtree, sorted by ->r_tid. | |
241 | */ | |
242 | static void __insert_request(struct ceph_osd_client *osdc, | |
243 | struct ceph_osd_request *new) | |
244 | { | |
245 | struct rb_node **p = &osdc->requests.rb_node; | |
246 | struct rb_node *parent = NULL; | |
247 | struct ceph_osd_request *req = NULL; | |
248 | ||
249 | while (*p) { | |
250 | parent = *p; | |
251 | req = rb_entry(parent, struct ceph_osd_request, r_node); | |
252 | if (new->r_tid < req->r_tid) | |
253 | p = &(*p)->rb_left; | |
254 | else if (new->r_tid > req->r_tid) | |
255 | p = &(*p)->rb_right; | |
256 | else | |
257 | BUG(); | |
258 | } | |
259 | ||
260 | rb_link_node(&new->r_node, parent, p); | |
261 | rb_insert_color(&new->r_node, &osdc->requests); | |
262 | } | |
263 | ||
264 | static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, | |
265 | u64 tid) | |
266 | { | |
267 | struct ceph_osd_request *req; | |
268 | struct rb_node *n = osdc->requests.rb_node; | |
269 | ||
270 | while (n) { | |
271 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
272 | if (tid < req->r_tid) | |
273 | n = n->rb_left; | |
274 | else if (tid > req->r_tid) | |
275 | n = n->rb_right; | |
276 | else | |
277 | return req; | |
278 | } | |
279 | return NULL; | |
280 | } | |
281 | ||
282 | static struct ceph_osd_request * | |
283 | __lookup_request_ge(struct ceph_osd_client *osdc, | |
284 | u64 tid) | |
285 | { | |
286 | struct ceph_osd_request *req; | |
287 | struct rb_node *n = osdc->requests.rb_node; | |
288 | ||
289 | while (n) { | |
290 | req = rb_entry(n, struct ceph_osd_request, r_node); | |
291 | if (tid < req->r_tid) { | |
292 | if (!n->rb_left) | |
293 | return req; | |
294 | n = n->rb_left; | |
295 | } else if (tid > req->r_tid) { | |
296 | n = n->rb_right; | |
297 | } else { | |
298 | return req; | |
299 | } | |
300 | } | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | ||
305 | /* | |
81b024e7 | 306 | * If the osd connection drops, we need to resubmit all requests. |
f24e9980 SW |
307 | */ |
308 | static void osd_reset(struct ceph_connection *con) | |
309 | { | |
310 | struct ceph_osd *osd = con->private; | |
311 | struct ceph_osd_client *osdc; | |
312 | ||
313 | if (!osd) | |
314 | return; | |
315 | dout("osd_reset osd%d\n", osd->o_osd); | |
316 | osdc = osd->o_osdc; | |
f24e9980 SW |
317 | down_read(&osdc->map_sem); |
318 | kick_requests(osdc, osd); | |
319 | up_read(&osdc->map_sem); | |
320 | } | |
321 | ||
322 | /* | |
323 | * Track open sessions with osds. | |
324 | */ | |
325 | static struct ceph_osd *create_osd(struct ceph_osd_client *osdc) | |
326 | { | |
327 | struct ceph_osd *osd; | |
328 | ||
329 | osd = kzalloc(sizeof(*osd), GFP_NOFS); | |
330 | if (!osd) | |
331 | return NULL; | |
332 | ||
333 | atomic_set(&osd->o_ref, 1); | |
334 | osd->o_osdc = osdc; | |
335 | INIT_LIST_HEAD(&osd->o_requests); | |
f5a2041b | 336 | INIT_LIST_HEAD(&osd->o_osd_lru); |
f24e9980 SW |
337 | osd->o_incarnation = 1; |
338 | ||
339 | ceph_con_init(osdc->client->msgr, &osd->o_con); | |
340 | osd->o_con.private = osd; | |
341 | osd->o_con.ops = &osd_con_ops; | |
342 | osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD; | |
4e7a5dcd | 343 | |
422d2cb8 | 344 | INIT_LIST_HEAD(&osd->o_keepalive_item); |
f24e9980 SW |
345 | return osd; |
346 | } | |
347 | ||
348 | static struct ceph_osd *get_osd(struct ceph_osd *osd) | |
349 | { | |
350 | if (atomic_inc_not_zero(&osd->o_ref)) { | |
351 | dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, | |
352 | atomic_read(&osd->o_ref)); | |
353 | return osd; | |
354 | } else { | |
355 | dout("get_osd %p FAIL\n", osd); | |
356 | return NULL; | |
357 | } | |
358 | } | |
359 | ||
360 | static void put_osd(struct ceph_osd *osd) | |
361 | { | |
362 | dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), | |
363 | atomic_read(&osd->o_ref) - 1); | |
42ce56e5 | 364 | if (atomic_dec_and_test(&osd->o_ref)) |
f24e9980 | 365 | kfree(osd); |
f24e9980 SW |
366 | } |
367 | ||
368 | /* | |
369 | * remove an osd from our map | |
370 | */ | |
f5a2041b | 371 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 372 | { |
f5a2041b | 373 | dout("__remove_osd %p\n", osd); |
f24e9980 SW |
374 | BUG_ON(!list_empty(&osd->o_requests)); |
375 | rb_erase(&osd->o_node, &osdc->osds); | |
f5a2041b | 376 | list_del_init(&osd->o_osd_lru); |
f24e9980 SW |
377 | ceph_con_close(&osd->o_con); |
378 | put_osd(osd); | |
379 | } | |
380 | ||
f5a2041b YS |
381 | static void __move_osd_to_lru(struct ceph_osd_client *osdc, |
382 | struct ceph_osd *osd) | |
383 | { | |
384 | dout("__move_osd_to_lru %p\n", osd); | |
385 | BUG_ON(!list_empty(&osd->o_osd_lru)); | |
386 | list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); | |
387 | osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ; | |
388 | } | |
389 | ||
390 | static void __remove_osd_from_lru(struct ceph_osd *osd) | |
391 | { | |
392 | dout("__remove_osd_from_lru %p\n", osd); | |
393 | if (!list_empty(&osd->o_osd_lru)) | |
394 | list_del_init(&osd->o_osd_lru); | |
395 | } | |
396 | ||
397 | static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all) | |
398 | { | |
399 | struct ceph_osd *osd, *nosd; | |
400 | ||
401 | dout("__remove_old_osds %p\n", osdc); | |
402 | mutex_lock(&osdc->request_mutex); | |
403 | list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { | |
404 | if (!remove_all && time_before(jiffies, osd->lru_ttl)) | |
405 | break; | |
406 | __remove_osd(osdc, osd); | |
407 | } | |
408 | mutex_unlock(&osdc->request_mutex); | |
409 | } | |
410 | ||
f24e9980 SW |
411 | /* |
412 | * reset osd connect | |
413 | */ | |
f5a2041b | 414 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) |
f24e9980 | 415 | { |
87b315a5 | 416 | struct ceph_osd_request *req; |
f24e9980 SW |
417 | int ret = 0; |
418 | ||
f5a2041b | 419 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); |
f24e9980 | 420 | if (list_empty(&osd->o_requests)) { |
f5a2041b | 421 | __remove_osd(osdc, osd); |
87b315a5 SW |
422 | } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd], |
423 | &osd->o_con.peer_addr, | |
424 | sizeof(osd->o_con.peer_addr)) == 0 && | |
425 | !ceph_con_opened(&osd->o_con)) { | |
426 | dout(" osd addr hasn't changed and connection never opened," | |
427 | " letting msgr retry"); | |
428 | /* touch each r_stamp for handle_timeout()'s benfit */ | |
429 | list_for_each_entry(req, &osd->o_requests, r_osd_item) | |
430 | req->r_stamp = jiffies; | |
431 | ret = -EAGAIN; | |
f24e9980 SW |
432 | } else { |
433 | ceph_con_close(&osd->o_con); | |
434 | ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]); | |
435 | osd->o_incarnation++; | |
436 | } | |
437 | return ret; | |
438 | } | |
439 | ||
440 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | |
441 | { | |
442 | struct rb_node **p = &osdc->osds.rb_node; | |
443 | struct rb_node *parent = NULL; | |
444 | struct ceph_osd *osd = NULL; | |
445 | ||
446 | while (*p) { | |
447 | parent = *p; | |
448 | osd = rb_entry(parent, struct ceph_osd, o_node); | |
449 | if (new->o_osd < osd->o_osd) | |
450 | p = &(*p)->rb_left; | |
451 | else if (new->o_osd > osd->o_osd) | |
452 | p = &(*p)->rb_right; | |
453 | else | |
454 | BUG(); | |
455 | } | |
456 | ||
457 | rb_link_node(&new->o_node, parent, p); | |
458 | rb_insert_color(&new->o_node, &osdc->osds); | |
459 | } | |
460 | ||
461 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | |
462 | { | |
463 | struct ceph_osd *osd; | |
464 | struct rb_node *n = osdc->osds.rb_node; | |
465 | ||
466 | while (n) { | |
467 | osd = rb_entry(n, struct ceph_osd, o_node); | |
468 | if (o < osd->o_osd) | |
469 | n = n->rb_left; | |
470 | else if (o > osd->o_osd) | |
471 | n = n->rb_right; | |
472 | else | |
473 | return osd; | |
474 | } | |
475 | return NULL; | |
476 | } | |
477 | ||
422d2cb8 YS |
478 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) |
479 | { | |
480 | schedule_delayed_work(&osdc->timeout_work, | |
481 | osdc->client->mount_args->osd_keepalive_timeout * HZ); | |
482 | } | |
483 | ||
484 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | |
485 | { | |
486 | cancel_delayed_work(&osdc->timeout_work); | |
487 | } | |
f24e9980 SW |
488 | |
489 | /* | |
490 | * Register request, assign tid. If this is the first request, set up | |
491 | * the timeout event. | |
492 | */ | |
493 | static void register_request(struct ceph_osd_client *osdc, | |
494 | struct ceph_osd_request *req) | |
495 | { | |
f24e9980 SW |
496 | mutex_lock(&osdc->request_mutex); |
497 | req->r_tid = ++osdc->last_tid; | |
6df058c0 | 498 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); |
422d2cb8 | 499 | INIT_LIST_HEAD(&req->r_req_lru_item); |
f24e9980 SW |
500 | |
501 | dout("register_request %p tid %lld\n", req, req->r_tid); | |
502 | __insert_request(osdc, req); | |
503 | ceph_osdc_get_request(req); | |
504 | osdc->num_requests++; | |
505 | ||
f24e9980 | 506 | if (osdc->num_requests == 1) { |
422d2cb8 YS |
507 | dout(" first request, scheduling timeout\n"); |
508 | __schedule_osd_timeout(osdc); | |
f24e9980 SW |
509 | } |
510 | mutex_unlock(&osdc->request_mutex); | |
511 | } | |
512 | ||
513 | /* | |
514 | * called under osdc->request_mutex | |
515 | */ | |
516 | static void __unregister_request(struct ceph_osd_client *osdc, | |
517 | struct ceph_osd_request *req) | |
518 | { | |
519 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | |
520 | rb_erase(&req->r_node, &osdc->requests); | |
521 | osdc->num_requests--; | |
522 | ||
0ba6478d SW |
523 | if (req->r_osd) { |
524 | /* make sure the original request isn't in flight. */ | |
525 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | |
526 | ||
527 | list_del_init(&req->r_osd_item); | |
528 | if (list_empty(&req->r_osd->o_requests)) | |
f5a2041b | 529 | __move_osd_to_lru(osdc, req->r_osd); |
0ba6478d SW |
530 | req->r_osd = NULL; |
531 | } | |
f24e9980 SW |
532 | |
533 | ceph_osdc_put_request(req); | |
534 | ||
422d2cb8 YS |
535 | list_del_init(&req->r_req_lru_item); |
536 | if (osdc->num_requests == 0) { | |
537 | dout(" no requests, canceling timeout\n"); | |
538 | __cancel_osd_timeout(osdc); | |
f24e9980 SW |
539 | } |
540 | } | |
541 | ||
542 | /* | |
543 | * Cancel a previously queued request message | |
544 | */ | |
545 | static void __cancel_request(struct ceph_osd_request *req) | |
546 | { | |
547 | if (req->r_sent) { | |
548 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | |
549 | req->r_sent = 0; | |
550 | } | |
422d2cb8 | 551 | list_del_init(&req->r_req_lru_item); |
f24e9980 SW |
552 | } |
553 | ||
554 | /* | |
555 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | |
556 | * (as needed), and set the request r_osd appropriately. If there is | |
557 | * no up osd, set r_osd to NULL. | |
558 | * | |
559 | * Return 0 if unchanged, 1 if changed, or negative on error. | |
560 | * | |
561 | * Caller should hold map_sem for read and request_mutex. | |
562 | */ | |
563 | static int __map_osds(struct ceph_osd_client *osdc, | |
564 | struct ceph_osd_request *req) | |
565 | { | |
566 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | |
51042122 | 567 | struct ceph_pg pgid; |
d85b7056 SW |
568 | int acting[CEPH_PG_MAX_SIZE]; |
569 | int o = -1, num = 0; | |
f24e9980 | 570 | int err; |
f24e9980 SW |
571 | |
572 | dout("map_osds %p tid %lld\n", req, req->r_tid); | |
573 | err = ceph_calc_object_layout(&reqhead->layout, req->r_oid, | |
574 | &req->r_file_layout, osdc->osdmap); | |
575 | if (err) | |
576 | return err; | |
51042122 | 577 | pgid = reqhead->layout.ol_pgid; |
7740a42f SW |
578 | req->r_pgid = pgid; |
579 | ||
d85b7056 SW |
580 | err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting); |
581 | if (err > 0) { | |
582 | o = acting[0]; | |
583 | num = err; | |
584 | } | |
f24e9980 SW |
585 | |
586 | if ((req->r_osd && req->r_osd->o_osd == o && | |
d85b7056 SW |
587 | req->r_sent >= req->r_osd->o_incarnation && |
588 | req->r_num_pg_osds == num && | |
589 | memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) || | |
f24e9980 SW |
590 | (req->r_osd == NULL && o == -1)) |
591 | return 0; /* no change */ | |
592 | ||
51042122 SW |
593 | dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n", |
594 | req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o, | |
f24e9980 SW |
595 | req->r_osd ? req->r_osd->o_osd : -1); |
596 | ||
d85b7056 SW |
597 | /* record full pg acting set */ |
598 | memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num); | |
599 | req->r_num_pg_osds = num; | |
600 | ||
f24e9980 SW |
601 | if (req->r_osd) { |
602 | __cancel_request(req); | |
603 | list_del_init(&req->r_osd_item); | |
f24e9980 SW |
604 | req->r_osd = NULL; |
605 | } | |
606 | ||
607 | req->r_osd = __lookup_osd(osdc, o); | |
608 | if (!req->r_osd && o >= 0) { | |
c99eb1c7 SW |
609 | err = -ENOMEM; |
610 | req->r_osd = create_osd(osdc); | |
611 | if (!req->r_osd) | |
612 | goto out; | |
f24e9980 SW |
613 | |
614 | dout("map_osds osd %p is osd%d\n", req->r_osd, o); | |
615 | req->r_osd->o_osd = o; | |
616 | req->r_osd->o_con.peer_name.num = cpu_to_le64(o); | |
617 | __insert_osd(osdc, req->r_osd); | |
618 | ||
619 | ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]); | |
620 | } | |
621 | ||
f5a2041b YS |
622 | if (req->r_osd) { |
623 | __remove_osd_from_lru(req->r_osd); | |
f24e9980 | 624 | list_add(&req->r_osd_item, &req->r_osd->o_requests); |
f5a2041b | 625 | } |
d85b7056 | 626 | err = 1; /* osd or pg changed */ |
f24e9980 SW |
627 | |
628 | out: | |
f24e9980 SW |
629 | return err; |
630 | } | |
631 | ||
632 | /* | |
633 | * caller should hold map_sem (for read) and request_mutex | |
634 | */ | |
635 | static int __send_request(struct ceph_osd_client *osdc, | |
636 | struct ceph_osd_request *req) | |
637 | { | |
638 | struct ceph_osd_request_head *reqhead; | |
639 | int err; | |
640 | ||
641 | err = __map_osds(osdc, req); | |
642 | if (err < 0) | |
643 | return err; | |
644 | if (req->r_osd == NULL) { | |
645 | dout("send_request %p no up osds in pg\n", req); | |
646 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
647 | return 0; | |
648 | } | |
649 | ||
650 | dout("send_request %p tid %llu to osd%d flags %d\n", | |
651 | req, req->r_tid, req->r_osd->o_osd, req->r_flags); | |
652 | ||
653 | reqhead = req->r_request->front.iov_base; | |
654 | reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch); | |
655 | reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */ | |
656 | reqhead->reassert_version = req->r_reassert_version; | |
657 | ||
3dd72fc0 | 658 | req->r_stamp = jiffies; |
422d2cb8 | 659 | list_move_tail(&osdc->req_lru, &req->r_req_lru_item); |
f24e9980 SW |
660 | |
661 | ceph_msg_get(req->r_request); /* send consumes a ref */ | |
662 | ceph_con_send(&req->r_osd->o_con, req->r_request); | |
663 | req->r_sent = req->r_osd->o_incarnation; | |
664 | return 0; | |
665 | } | |
666 | ||
667 | /* | |
668 | * Timeout callback, called every N seconds when 1 or more osd | |
669 | * requests has been active for more than N seconds. When this | |
670 | * happens, we ping all OSDs with requests who have timed out to | |
671 | * ensure any communications channel reset is detected. Reset the | |
672 | * request timeouts another N seconds in the future as we go. | |
673 | * Reschedule the timeout event another N seconds in future (unless | |
674 | * there are no open requests). | |
675 | */ | |
676 | static void handle_timeout(struct work_struct *work) | |
677 | { | |
678 | struct ceph_osd_client *osdc = | |
679 | container_of(work, struct ceph_osd_client, timeout_work.work); | |
422d2cb8 | 680 | struct ceph_osd_request *req, *last_req = NULL; |
f24e9980 | 681 | struct ceph_osd *osd; |
6b805185 | 682 | unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ; |
422d2cb8 YS |
683 | unsigned long keepalive = |
684 | osdc->client->mount_args->osd_keepalive_timeout * HZ; | |
3dd72fc0 | 685 | unsigned long last_stamp = 0; |
f24e9980 | 686 | struct rb_node *p; |
422d2cb8 | 687 | struct list_head slow_osds; |
f24e9980 SW |
688 | |
689 | dout("timeout\n"); | |
690 | down_read(&osdc->map_sem); | |
691 | ||
692 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
693 | ||
694 | mutex_lock(&osdc->request_mutex); | |
695 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | |
696 | req = rb_entry(p, struct ceph_osd_request, r_node); | |
697 | ||
698 | if (req->r_resend) { | |
699 | int err; | |
700 | ||
701 | dout("osdc resending prev failed %lld\n", req->r_tid); | |
702 | err = __send_request(osdc, req); | |
703 | if (err) | |
704 | dout("osdc failed again on %lld\n", req->r_tid); | |
705 | else | |
706 | req->r_resend = false; | |
707 | continue; | |
708 | } | |
709 | } | |
f24e9980 | 710 | |
422d2cb8 YS |
711 | /* |
712 | * reset osds that appear to be _really_ unresponsive. this | |
713 | * is a failsafe measure.. we really shouldn't be getting to | |
714 | * this point if the system is working properly. the monitors | |
715 | * should mark the osd as failed and we should find out about | |
716 | * it from an updated osd map. | |
717 | */ | |
f26e681d | 718 | while (timeout && !list_empty(&osdc->req_lru)) { |
422d2cb8 YS |
719 | req = list_entry(osdc->req_lru.next, struct ceph_osd_request, |
720 | r_req_lru_item); | |
721 | ||
3dd72fc0 | 722 | if (time_before(jiffies, req->r_stamp + timeout)) |
422d2cb8 YS |
723 | break; |
724 | ||
3dd72fc0 | 725 | BUG_ON(req == last_req && req->r_stamp == last_stamp); |
422d2cb8 | 726 | last_req = req; |
3dd72fc0 | 727 | last_stamp = req->r_stamp; |
422d2cb8 YS |
728 | |
729 | osd = req->r_osd; | |
730 | BUG_ON(!osd); | |
731 | pr_warning(" tid %llu timed out on osd%d, will reset osd\n", | |
732 | req->r_tid, osd->o_osd); | |
733 | __kick_requests(osdc, osd); | |
734 | } | |
735 | ||
736 | /* | |
737 | * ping osds that are a bit slow. this ensures that if there | |
738 | * is a break in the TCP connection we will notice, and reopen | |
739 | * a connection with that osd (from the fault callback). | |
740 | */ | |
741 | INIT_LIST_HEAD(&slow_osds); | |
742 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | |
3dd72fc0 | 743 | if (time_before(jiffies, req->r_stamp + keepalive)) |
422d2cb8 YS |
744 | break; |
745 | ||
746 | osd = req->r_osd; | |
747 | BUG_ON(!osd); | |
748 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | |
f24e9980 | 749 | req->r_tid, osd->o_osd); |
422d2cb8 YS |
750 | list_move_tail(&osd->o_keepalive_item, &slow_osds); |
751 | } | |
752 | while (!list_empty(&slow_osds)) { | |
753 | osd = list_entry(slow_osds.next, struct ceph_osd, | |
754 | o_keepalive_item); | |
755 | list_del_init(&osd->o_keepalive_item); | |
f24e9980 SW |
756 | ceph_con_keepalive(&osd->o_con); |
757 | } | |
758 | ||
422d2cb8 | 759 | __schedule_osd_timeout(osdc); |
f24e9980 SW |
760 | mutex_unlock(&osdc->request_mutex); |
761 | ||
762 | up_read(&osdc->map_sem); | |
763 | } | |
764 | ||
f5a2041b YS |
765 | static void handle_osds_timeout(struct work_struct *work) |
766 | { | |
767 | struct ceph_osd_client *osdc = | |
768 | container_of(work, struct ceph_osd_client, | |
769 | osds_timeout_work.work); | |
770 | unsigned long delay = | |
771 | osdc->client->mount_args->osd_idle_ttl * HZ >> 2; | |
772 | ||
773 | dout("osds timeout\n"); | |
774 | down_read(&osdc->map_sem); | |
775 | remove_old_osds(osdc, 0); | |
776 | up_read(&osdc->map_sem); | |
777 | ||
778 | schedule_delayed_work(&osdc->osds_timeout_work, | |
779 | round_jiffies_relative(delay)); | |
780 | } | |
781 | ||
f24e9980 SW |
782 | /* |
783 | * handle osd op reply. either call the callback if it is specified, | |
784 | * or do the completion to wake up the waiting thread. | |
785 | */ | |
350b1c32 SW |
786 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, |
787 | struct ceph_connection *con) | |
f24e9980 SW |
788 | { |
789 | struct ceph_osd_reply_head *rhead = msg->front.iov_base; | |
790 | struct ceph_osd_request *req; | |
791 | u64 tid; | |
792 | int numops, object_len, flags; | |
0ceed5db | 793 | s32 result; |
f24e9980 | 794 | |
6df058c0 | 795 | tid = le64_to_cpu(msg->hdr.tid); |
f24e9980 SW |
796 | if (msg->front.iov_len < sizeof(*rhead)) |
797 | goto bad; | |
f24e9980 SW |
798 | numops = le32_to_cpu(rhead->num_ops); |
799 | object_len = le32_to_cpu(rhead->object_len); | |
0ceed5db | 800 | result = le32_to_cpu(rhead->result); |
f24e9980 SW |
801 | if (msg->front.iov_len != sizeof(*rhead) + object_len + |
802 | numops * sizeof(struct ceph_osd_op)) | |
803 | goto bad; | |
0ceed5db | 804 | dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result); |
f24e9980 SW |
805 | |
806 | /* lookup */ | |
807 | mutex_lock(&osdc->request_mutex); | |
808 | req = __lookup_request(osdc, tid); | |
809 | if (req == NULL) { | |
810 | dout("handle_reply tid %llu dne\n", tid); | |
811 | mutex_unlock(&osdc->request_mutex); | |
812 | return; | |
813 | } | |
814 | ceph_osdc_get_request(req); | |
815 | flags = le32_to_cpu(rhead->flags); | |
816 | ||
350b1c32 | 817 | /* |
0d59ab81 | 818 | * if this connection filled our message, drop our reference now, to |
350b1c32 SW |
819 | * avoid a (safe but slower) revoke later. |
820 | */ | |
0d59ab81 | 821 | if (req->r_con_filling_msg == con && req->r_reply == msg) { |
c16e7869 | 822 | dout(" dropping con_filling_msg ref %p\n", con); |
0d59ab81 | 823 | req->r_con_filling_msg = NULL; |
350b1c32 SW |
824 | ceph_con_put(con); |
825 | } | |
826 | ||
f24e9980 SW |
827 | if (!req->r_got_reply) { |
828 | unsigned bytes; | |
829 | ||
830 | req->r_result = le32_to_cpu(rhead->result); | |
831 | bytes = le32_to_cpu(msg->hdr.data_len); | |
832 | dout("handle_reply result %d bytes %d\n", req->r_result, | |
833 | bytes); | |
834 | if (req->r_result == 0) | |
835 | req->r_result = bytes; | |
836 | ||
837 | /* in case this is a write and we need to replay, */ | |
838 | req->r_reassert_version = rhead->reassert_version; | |
839 | ||
840 | req->r_got_reply = 1; | |
841 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | |
842 | dout("handle_reply tid %llu dup ack\n", tid); | |
34b43a56 | 843 | mutex_unlock(&osdc->request_mutex); |
f24e9980 SW |
844 | goto done; |
845 | } | |
846 | ||
847 | dout("handle_reply tid %llu flags %d\n", tid, flags); | |
848 | ||
849 | /* either this is a read, or we got the safe response */ | |
0ceed5db SW |
850 | if (result < 0 || |
851 | (flags & CEPH_OSD_FLAG_ONDISK) || | |
f24e9980 SW |
852 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) |
853 | __unregister_request(osdc, req); | |
854 | ||
855 | mutex_unlock(&osdc->request_mutex); | |
856 | ||
857 | if (req->r_callback) | |
858 | req->r_callback(req, msg); | |
859 | else | |
860 | complete(&req->r_completion); | |
861 | ||
862 | if (flags & CEPH_OSD_FLAG_ONDISK) { | |
863 | if (req->r_safe_callback) | |
864 | req->r_safe_callback(req, msg); | |
865 | complete(&req->r_safe_completion); /* fsync waiter */ | |
866 | } | |
867 | ||
868 | done: | |
869 | ceph_osdc_put_request(req); | |
870 | return; | |
871 | ||
872 | bad: | |
873 | pr_err("corrupt osd_op_reply got %d %d expected %d\n", | |
874 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len), | |
875 | (int)sizeof(*rhead)); | |
9ec7cab1 | 876 | ceph_msg_dump(msg); |
f24e9980 SW |
877 | } |
878 | ||
879 | ||
422d2cb8 | 880 | static int __kick_requests(struct ceph_osd_client *osdc, |
f24e9980 SW |
881 | struct ceph_osd *kickosd) |
882 | { | |
883 | struct ceph_osd_request *req; | |
884 | struct rb_node *p, *n; | |
885 | int needmap = 0; | |
886 | int err; | |
887 | ||
888 | dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1); | |
153a008b | 889 | if (kickosd) { |
87b315a5 SW |
890 | err = __reset_osd(osdc, kickosd); |
891 | if (err == -EAGAIN) | |
892 | return 1; | |
153a008b | 893 | } else { |
f24e9980 SW |
894 | for (p = rb_first(&osdc->osds); p; p = n) { |
895 | struct ceph_osd *osd = | |
896 | rb_entry(p, struct ceph_osd, o_node); | |
897 | ||
898 | n = rb_next(p); | |
899 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | |
103e2d3a SW |
900 | memcmp(&osd->o_con.peer_addr, |
901 | ceph_osd_addr(osdc->osdmap, | |
902 | osd->o_osd), | |
903 | sizeof(struct ceph_entity_addr)) != 0) | |
f5a2041b | 904 | __reset_osd(osdc, osd); |
f24e9980 SW |
905 | } |
906 | } | |
907 | ||
908 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | |
909 | req = rb_entry(p, struct ceph_osd_request, r_node); | |
910 | ||
911 | if (req->r_resend) { | |
912 | dout(" r_resend set on tid %llu\n", req->r_tid); | |
266673db | 913 | __cancel_request(req); |
f24e9980 SW |
914 | goto kick; |
915 | } | |
266673db SW |
916 | if (req->r_osd && kickosd == req->r_osd) { |
917 | __cancel_request(req); | |
f24e9980 | 918 | goto kick; |
266673db | 919 | } |
f24e9980 SW |
920 | |
921 | err = __map_osds(osdc, req); | |
922 | if (err == 0) | |
923 | continue; /* no change */ | |
924 | if (err < 0) { | |
925 | /* | |
926 | * FIXME: really, we should set the request | |
927 | * error and fail if this isn't a 'nofail' | |
928 | * request, but that's a fair bit more | |
929 | * complicated to do. So retry! | |
930 | */ | |
931 | dout(" setting r_resend on %llu\n", req->r_tid); | |
932 | req->r_resend = true; | |
933 | continue; | |
934 | } | |
935 | if (req->r_osd == NULL) { | |
936 | dout("tid %llu maps to no valid osd\n", req->r_tid); | |
937 | needmap++; /* request a newer map */ | |
938 | continue; | |
939 | } | |
940 | ||
941 | kick: | |
c1ea8823 | 942 | dout("kicking %p tid %llu osd%d\n", req, req->r_tid, |
12eadc19 | 943 | req->r_osd ? req->r_osd->o_osd : -1); |
f24e9980 SW |
944 | req->r_flags |= CEPH_OSD_FLAG_RETRY; |
945 | err = __send_request(osdc, req); | |
946 | if (err) { | |
947 | dout(" setting r_resend on %llu\n", req->r_tid); | |
948 | req->r_resend = true; | |
949 | } | |
950 | } | |
422d2cb8 YS |
951 | |
952 | return needmap; | |
953 | } | |
954 | ||
955 | /* | |
956 | * Resubmit osd requests whose osd or osd address has changed. Request | |
957 | * a new osd map if osds are down, or we are otherwise unable to determine | |
958 | * how to direct a request. | |
959 | * | |
960 | * Close connections to down osds. | |
961 | * | |
962 | * If @who is specified, resubmit requests for that specific osd. | |
963 | * | |
964 | * Caller should hold map_sem for read and request_mutex. | |
965 | */ | |
966 | static void kick_requests(struct ceph_osd_client *osdc, | |
967 | struct ceph_osd *kickosd) | |
968 | { | |
969 | int needmap; | |
970 | ||
971 | mutex_lock(&osdc->request_mutex); | |
972 | needmap = __kick_requests(osdc, kickosd); | |
f24e9980 SW |
973 | mutex_unlock(&osdc->request_mutex); |
974 | ||
975 | if (needmap) { | |
976 | dout("%d requests for down osds, need new map\n", needmap); | |
977 | ceph_monc_request_next_osdmap(&osdc->client->monc); | |
978 | } | |
f24e9980 | 979 | |
422d2cb8 | 980 | } |
f24e9980 SW |
981 | /* |
982 | * Process updated osd map. | |
983 | * | |
984 | * The message contains any number of incremental and full maps, normally | |
985 | * indicating some sort of topology change in the cluster. Kick requests | |
986 | * off to different OSDs as needed. | |
987 | */ | |
988 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | |
989 | { | |
990 | void *p, *end, *next; | |
991 | u32 nr_maps, maplen; | |
992 | u32 epoch; | |
993 | struct ceph_osdmap *newmap = NULL, *oldmap; | |
994 | int err; | |
995 | struct ceph_fsid fsid; | |
996 | ||
997 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | |
998 | p = msg->front.iov_base; | |
999 | end = p + msg->front.iov_len; | |
1000 | ||
1001 | /* verify fsid */ | |
1002 | ceph_decode_need(&p, end, sizeof(fsid), bad); | |
1003 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | |
0743304d SW |
1004 | if (ceph_check_fsid(osdc->client, &fsid) < 0) |
1005 | return; | |
f24e9980 SW |
1006 | |
1007 | down_write(&osdc->map_sem); | |
1008 | ||
1009 | /* incremental maps */ | |
1010 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1011 | dout(" %d inc maps\n", nr_maps); | |
1012 | while (nr_maps > 0) { | |
1013 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1014 | epoch = ceph_decode_32(&p); |
1015 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1016 | ceph_decode_need(&p, end, maplen, bad); |
1017 | next = p + maplen; | |
1018 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | |
1019 | dout("applying incremental map %u len %d\n", | |
1020 | epoch, maplen); | |
1021 | newmap = osdmap_apply_incremental(&p, next, | |
1022 | osdc->osdmap, | |
1023 | osdc->client->msgr); | |
1024 | if (IS_ERR(newmap)) { | |
1025 | err = PTR_ERR(newmap); | |
1026 | goto bad; | |
1027 | } | |
30dc6381 | 1028 | BUG_ON(!newmap); |
f24e9980 SW |
1029 | if (newmap != osdc->osdmap) { |
1030 | ceph_osdmap_destroy(osdc->osdmap); | |
1031 | osdc->osdmap = newmap; | |
1032 | } | |
1033 | } else { | |
1034 | dout("ignoring incremental map %u len %d\n", | |
1035 | epoch, maplen); | |
1036 | } | |
1037 | p = next; | |
1038 | nr_maps--; | |
1039 | } | |
1040 | if (newmap) | |
1041 | goto done; | |
1042 | ||
1043 | /* full maps */ | |
1044 | ceph_decode_32_safe(&p, end, nr_maps, bad); | |
1045 | dout(" %d full maps\n", nr_maps); | |
1046 | while (nr_maps) { | |
1047 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | |
c89136ea SW |
1048 | epoch = ceph_decode_32(&p); |
1049 | maplen = ceph_decode_32(&p); | |
f24e9980 SW |
1050 | ceph_decode_need(&p, end, maplen, bad); |
1051 | if (nr_maps > 1) { | |
1052 | dout("skipping non-latest full map %u len %d\n", | |
1053 | epoch, maplen); | |
1054 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | |
1055 | dout("skipping full map %u len %d, " | |
1056 | "older than our %u\n", epoch, maplen, | |
1057 | osdc->osdmap->epoch); | |
1058 | } else { | |
1059 | dout("taking full map %u len %d\n", epoch, maplen); | |
1060 | newmap = osdmap_decode(&p, p+maplen); | |
1061 | if (IS_ERR(newmap)) { | |
1062 | err = PTR_ERR(newmap); | |
1063 | goto bad; | |
1064 | } | |
30dc6381 | 1065 | BUG_ON(!newmap); |
f24e9980 SW |
1066 | oldmap = osdc->osdmap; |
1067 | osdc->osdmap = newmap; | |
1068 | if (oldmap) | |
1069 | ceph_osdmap_destroy(oldmap); | |
1070 | } | |
1071 | p += maplen; | |
1072 | nr_maps--; | |
1073 | } | |
1074 | ||
1075 | done: | |
1076 | downgrade_write(&osdc->map_sem); | |
1077 | ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); | |
1078 | if (newmap) | |
1079 | kick_requests(osdc, NULL); | |
1080 | up_read(&osdc->map_sem); | |
c473ad92 | 1081 | wake_up(&osdc->client->auth_wq); |
f24e9980 SW |
1082 | return; |
1083 | ||
1084 | bad: | |
1085 | pr_err("osdc handle_map corrupt msg\n"); | |
9ec7cab1 | 1086 | ceph_msg_dump(msg); |
f24e9980 SW |
1087 | up_write(&osdc->map_sem); |
1088 | return; | |
1089 | } | |
1090 | ||
f24e9980 SW |
1091 | /* |
1092 | * Register request, send initial attempt. | |
1093 | */ | |
1094 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | |
1095 | struct ceph_osd_request *req, | |
1096 | bool nofail) | |
1097 | { | |
c1ea8823 | 1098 | int rc = 0; |
f24e9980 SW |
1099 | |
1100 | req->r_request->pages = req->r_pages; | |
1101 | req->r_request->nr_pages = req->r_num_pages; | |
1102 | ||
1103 | register_request(osdc, req); | |
1104 | ||
1105 | down_read(&osdc->map_sem); | |
1106 | mutex_lock(&osdc->request_mutex); | |
c1ea8823 SW |
1107 | /* |
1108 | * a racing kick_requests() may have sent the message for us | |
1109 | * while we dropped request_mutex above, so only send now if | |
1110 | * the request still han't been touched yet. | |
1111 | */ | |
1112 | if (req->r_sent == 0) { | |
1113 | rc = __send_request(osdc, req); | |
1114 | if (rc) { | |
1115 | if (nofail) { | |
1116 | dout("osdc_start_request failed send, " | |
1117 | " marking %lld\n", req->r_tid); | |
1118 | req->r_resend = true; | |
1119 | rc = 0; | |
1120 | } else { | |
1121 | __unregister_request(osdc, req); | |
1122 | } | |
f24e9980 SW |
1123 | } |
1124 | } | |
1125 | mutex_unlock(&osdc->request_mutex); | |
1126 | up_read(&osdc->map_sem); | |
1127 | return rc; | |
1128 | } | |
1129 | ||
1130 | /* | |
1131 | * wait for a request to complete | |
1132 | */ | |
1133 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | |
1134 | struct ceph_osd_request *req) | |
1135 | { | |
1136 | int rc; | |
1137 | ||
1138 | rc = wait_for_completion_interruptible(&req->r_completion); | |
1139 | if (rc < 0) { | |
1140 | mutex_lock(&osdc->request_mutex); | |
1141 | __cancel_request(req); | |
529cfcc4 | 1142 | __unregister_request(osdc, req); |
f24e9980 | 1143 | mutex_unlock(&osdc->request_mutex); |
529cfcc4 | 1144 | dout("wait_request tid %llu canceled/timed out\n", req->r_tid); |
f24e9980 SW |
1145 | return rc; |
1146 | } | |
1147 | ||
1148 | dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); | |
1149 | return req->r_result; | |
1150 | } | |
1151 | ||
1152 | /* | |
1153 | * sync - wait for all in-flight requests to flush. avoid starvation. | |
1154 | */ | |
1155 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | |
1156 | { | |
1157 | struct ceph_osd_request *req; | |
1158 | u64 last_tid, next_tid = 0; | |
1159 | ||
1160 | mutex_lock(&osdc->request_mutex); | |
1161 | last_tid = osdc->last_tid; | |
1162 | while (1) { | |
1163 | req = __lookup_request_ge(osdc, next_tid); | |
1164 | if (!req) | |
1165 | break; | |
1166 | if (req->r_tid > last_tid) | |
1167 | break; | |
1168 | ||
1169 | next_tid = req->r_tid + 1; | |
1170 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | |
1171 | continue; | |
1172 | ||
1173 | ceph_osdc_get_request(req); | |
1174 | mutex_unlock(&osdc->request_mutex); | |
1175 | dout("sync waiting on tid %llu (last is %llu)\n", | |
1176 | req->r_tid, last_tid); | |
1177 | wait_for_completion(&req->r_safe_completion); | |
1178 | mutex_lock(&osdc->request_mutex); | |
1179 | ceph_osdc_put_request(req); | |
1180 | } | |
1181 | mutex_unlock(&osdc->request_mutex); | |
1182 | dout("sync done (thru tid %llu)\n", last_tid); | |
1183 | } | |
1184 | ||
1185 | /* | |
1186 | * init, shutdown | |
1187 | */ | |
1188 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | |
1189 | { | |
1190 | int err; | |
1191 | ||
1192 | dout("init\n"); | |
1193 | osdc->client = client; | |
1194 | osdc->osdmap = NULL; | |
1195 | init_rwsem(&osdc->map_sem); | |
1196 | init_completion(&osdc->map_waiters); | |
1197 | osdc->last_requested_map = 0; | |
1198 | mutex_init(&osdc->request_mutex); | |
f24e9980 SW |
1199 | osdc->last_tid = 0; |
1200 | osdc->osds = RB_ROOT; | |
f5a2041b | 1201 | INIT_LIST_HEAD(&osdc->osd_lru); |
f24e9980 | 1202 | osdc->requests = RB_ROOT; |
422d2cb8 | 1203 | INIT_LIST_HEAD(&osdc->req_lru); |
f24e9980 SW |
1204 | osdc->num_requests = 0; |
1205 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | |
f5a2041b YS |
1206 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); |
1207 | ||
1208 | schedule_delayed_work(&osdc->osds_timeout_work, | |
1209 | round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ)); | |
f24e9980 | 1210 | |
5f44f142 | 1211 | err = -ENOMEM; |
f24e9980 SW |
1212 | osdc->req_mempool = mempool_create_kmalloc_pool(10, |
1213 | sizeof(struct ceph_osd_request)); | |
1214 | if (!osdc->req_mempool) | |
5f44f142 | 1215 | goto out; |
f24e9980 | 1216 | |
c16e7869 | 1217 | err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true); |
f24e9980 | 1218 | if (err < 0) |
5f44f142 | 1219 | goto out_mempool; |
c16e7869 SW |
1220 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, |
1221 | OSD_OPREPLY_FRONT_LEN, 10, true); | |
1222 | if (err < 0) | |
1223 | goto out_msgpool; | |
f24e9980 | 1224 | return 0; |
5f44f142 | 1225 | |
c16e7869 SW |
1226 | out_msgpool: |
1227 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
5f44f142 SW |
1228 | out_mempool: |
1229 | mempool_destroy(osdc->req_mempool); | |
1230 | out: | |
1231 | return err; | |
f24e9980 SW |
1232 | } |
1233 | ||
1234 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | |
1235 | { | |
1236 | cancel_delayed_work_sync(&osdc->timeout_work); | |
f5a2041b | 1237 | cancel_delayed_work_sync(&osdc->osds_timeout_work); |
f24e9980 SW |
1238 | if (osdc->osdmap) { |
1239 | ceph_osdmap_destroy(osdc->osdmap); | |
1240 | osdc->osdmap = NULL; | |
1241 | } | |
f5a2041b | 1242 | remove_old_osds(osdc, 1); |
f24e9980 SW |
1243 | mempool_destroy(osdc->req_mempool); |
1244 | ceph_msgpool_destroy(&osdc->msgpool_op); | |
c16e7869 | 1245 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); |
f24e9980 SW |
1246 | } |
1247 | ||
1248 | /* | |
1249 | * Read some contiguous pages. If we cross a stripe boundary, shorten | |
1250 | * *plen. Return number of bytes read, or error. | |
1251 | */ | |
1252 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | |
1253 | struct ceph_vino vino, struct ceph_file_layout *layout, | |
1254 | u64 off, u64 *plen, | |
1255 | u32 truncate_seq, u64 truncate_size, | |
1256 | struct page **pages, int num_pages) | |
1257 | { | |
1258 | struct ceph_osd_request *req; | |
1259 | int rc = 0; | |
1260 | ||
1261 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | |
1262 | vino.snap, off, *plen); | |
1263 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, | |
1264 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, | |
1265 | NULL, 0, truncate_seq, truncate_size, NULL, | |
1266 | false, 1); | |
a79832f2 SW |
1267 | if (!req) |
1268 | return -ENOMEM; | |
f24e9980 SW |
1269 | |
1270 | /* it may be a short read due to an object boundary */ | |
1271 | req->r_pages = pages; | |
1272 | num_pages = calc_pages_for(off, *plen); | |
1273 | req->r_num_pages = num_pages; | |
1274 | ||
1275 | dout("readpages final extent is %llu~%llu (%d pages)\n", | |
1276 | off, *plen, req->r_num_pages); | |
1277 | ||
1278 | rc = ceph_osdc_start_request(osdc, req, false); | |
1279 | if (!rc) | |
1280 | rc = ceph_osdc_wait_request(osdc, req); | |
1281 | ||
1282 | ceph_osdc_put_request(req); | |
1283 | dout("readpages result %d\n", rc); | |
1284 | return rc; | |
1285 | } | |
1286 | ||
1287 | /* | |
1288 | * do a synchronous write on N pages | |
1289 | */ | |
1290 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | |
1291 | struct ceph_file_layout *layout, | |
1292 | struct ceph_snap_context *snapc, | |
1293 | u64 off, u64 len, | |
1294 | u32 truncate_seq, u64 truncate_size, | |
1295 | struct timespec *mtime, | |
1296 | struct page **pages, int num_pages, | |
1297 | int flags, int do_sync, bool nofail) | |
1298 | { | |
1299 | struct ceph_osd_request *req; | |
1300 | int rc = 0; | |
1301 | ||
1302 | BUG_ON(vino.snap != CEPH_NOSNAP); | |
1303 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, | |
1304 | CEPH_OSD_OP_WRITE, | |
1305 | flags | CEPH_OSD_FLAG_ONDISK | | |
1306 | CEPH_OSD_FLAG_WRITE, | |
1307 | snapc, do_sync, | |
1308 | truncate_seq, truncate_size, mtime, | |
1309 | nofail, 1); | |
a79832f2 SW |
1310 | if (!req) |
1311 | return -ENOMEM; | |
f24e9980 SW |
1312 | |
1313 | /* it may be a short write due to an object boundary */ | |
1314 | req->r_pages = pages; | |
1315 | req->r_num_pages = calc_pages_for(off, len); | |
1316 | dout("writepages %llu~%llu (%d pages)\n", off, len, | |
1317 | req->r_num_pages); | |
1318 | ||
1319 | rc = ceph_osdc_start_request(osdc, req, nofail); | |
1320 | if (!rc) | |
1321 | rc = ceph_osdc_wait_request(osdc, req); | |
1322 | ||
1323 | ceph_osdc_put_request(req); | |
1324 | if (rc == 0) | |
1325 | rc = len; | |
1326 | dout("writepages result %d\n", rc); | |
1327 | return rc; | |
1328 | } | |
1329 | ||
1330 | /* | |
1331 | * handle incoming message | |
1332 | */ | |
1333 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | |
1334 | { | |
1335 | struct ceph_osd *osd = con->private; | |
32c895e7 | 1336 | struct ceph_osd_client *osdc; |
f24e9980 SW |
1337 | int type = le16_to_cpu(msg->hdr.type); |
1338 | ||
1339 | if (!osd) | |
1340 | return; | |
32c895e7 | 1341 | osdc = osd->o_osdc; |
f24e9980 SW |
1342 | |
1343 | switch (type) { | |
1344 | case CEPH_MSG_OSD_MAP: | |
1345 | ceph_osdc_handle_map(osdc, msg); | |
1346 | break; | |
1347 | case CEPH_MSG_OSD_OPREPLY: | |
350b1c32 | 1348 | handle_reply(osdc, msg, con); |
f24e9980 SW |
1349 | break; |
1350 | ||
1351 | default: | |
1352 | pr_err("received unknown message type %d %s\n", type, | |
1353 | ceph_msg_type_name(type)); | |
1354 | } | |
1355 | ceph_msg_put(msg); | |
1356 | } | |
1357 | ||
5b3a4db3 | 1358 | /* |
21b667f6 SW |
1359 | * lookup and return message for incoming reply. set up reply message |
1360 | * pages. | |
5b3a4db3 SW |
1361 | */ |
1362 | static struct ceph_msg *get_reply(struct ceph_connection *con, | |
2450418c YS |
1363 | struct ceph_msg_header *hdr, |
1364 | int *skip) | |
f24e9980 SW |
1365 | { |
1366 | struct ceph_osd *osd = con->private; | |
1367 | struct ceph_osd_client *osdc = osd->o_osdc; | |
2450418c | 1368 | struct ceph_msg *m; |
0547a9b3 | 1369 | struct ceph_osd_request *req; |
5b3a4db3 SW |
1370 | int front = le32_to_cpu(hdr->front_len); |
1371 | int data_len = le32_to_cpu(hdr->data_len); | |
0547a9b3 | 1372 | u64 tid; |
f24e9980 | 1373 | |
0547a9b3 YS |
1374 | tid = le64_to_cpu(hdr->tid); |
1375 | mutex_lock(&osdc->request_mutex); | |
1376 | req = __lookup_request(osdc, tid); | |
1377 | if (!req) { | |
1378 | *skip = 1; | |
1379 | m = NULL; | |
c16e7869 | 1380 | pr_info("get_reply unknown tid %llu from osd%d\n", tid, |
5b3a4db3 | 1381 | osd->o_osd); |
0547a9b3 YS |
1382 | goto out; |
1383 | } | |
c16e7869 SW |
1384 | |
1385 | if (req->r_con_filling_msg) { | |
1386 | dout("get_reply revoking msg %p from old con %p\n", | |
1387 | req->r_reply, req->r_con_filling_msg); | |
1388 | ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply); | |
1389 | ceph_con_put(req->r_con_filling_msg); | |
6f46cb29 | 1390 | req->r_con_filling_msg = NULL; |
0547a9b3 YS |
1391 | } |
1392 | ||
c16e7869 SW |
1393 | if (front > req->r_reply->front.iov_len) { |
1394 | pr_warning("get_reply front %d > preallocated %d\n", | |
1395 | front, (int)req->r_reply->front.iov_len); | |
bb257664 | 1396 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front); |
a79832f2 | 1397 | if (!m) |
c16e7869 SW |
1398 | goto out; |
1399 | ceph_msg_put(req->r_reply); | |
1400 | req->r_reply = m; | |
1401 | } | |
1402 | m = ceph_msg_get(req->r_reply); | |
1403 | ||
0547a9b3 | 1404 | if (data_len > 0) { |
21b667f6 SW |
1405 | unsigned data_off = le16_to_cpu(hdr->data_off); |
1406 | int want = calc_pages_for(data_off & ~PAGE_MASK, data_len); | |
1407 | ||
1408 | if (unlikely(req->r_num_pages < want)) { | |
1409 | pr_warning("tid %lld reply %d > expected %d pages\n", | |
1410 | tid, want, m->nr_pages); | |
0547a9b3 YS |
1411 | *skip = 1; |
1412 | ceph_msg_put(m); | |
a79832f2 | 1413 | m = NULL; |
21b667f6 | 1414 | goto out; |
0547a9b3 | 1415 | } |
21b667f6 SW |
1416 | m->pages = req->r_pages; |
1417 | m->nr_pages = req->r_num_pages; | |
0547a9b3 | 1418 | } |
5b3a4db3 | 1419 | *skip = 0; |
c16e7869 SW |
1420 | req->r_con_filling_msg = ceph_con_get(con); |
1421 | dout("get_reply tid %lld %p\n", tid, m); | |
0547a9b3 YS |
1422 | |
1423 | out: | |
1424 | mutex_unlock(&osdc->request_mutex); | |
2450418c | 1425 | return m; |
5b3a4db3 SW |
1426 | |
1427 | } | |
1428 | ||
1429 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | |
1430 | struct ceph_msg_header *hdr, | |
1431 | int *skip) | |
1432 | { | |
1433 | struct ceph_osd *osd = con->private; | |
1434 | int type = le16_to_cpu(hdr->type); | |
1435 | int front = le32_to_cpu(hdr->front_len); | |
1436 | ||
1437 | switch (type) { | |
1438 | case CEPH_MSG_OSD_MAP: | |
bb257664 | 1439 | return ceph_msg_new(type, front); |
5b3a4db3 SW |
1440 | case CEPH_MSG_OSD_OPREPLY: |
1441 | return get_reply(con, hdr, skip); | |
1442 | default: | |
1443 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | |
1444 | osd->o_osd); | |
1445 | *skip = 1; | |
1446 | return NULL; | |
1447 | } | |
f24e9980 SW |
1448 | } |
1449 | ||
1450 | /* | |
1451 | * Wrappers to refcount containing ceph_osd struct | |
1452 | */ | |
1453 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | |
1454 | { | |
1455 | struct ceph_osd *osd = con->private; | |
1456 | if (get_osd(osd)) | |
1457 | return con; | |
1458 | return NULL; | |
1459 | } | |
1460 | ||
1461 | static void put_osd_con(struct ceph_connection *con) | |
1462 | { | |
1463 | struct ceph_osd *osd = con->private; | |
1464 | put_osd(osd); | |
1465 | } | |
1466 | ||
4e7a5dcd SW |
1467 | /* |
1468 | * authentication | |
1469 | */ | |
1470 | static int get_authorizer(struct ceph_connection *con, | |
50b885b9 SW |
1471 | void **buf, int *len, int *proto, |
1472 | void **reply_buf, int *reply_len, int force_new) | |
4e7a5dcd SW |
1473 | { |
1474 | struct ceph_osd *o = con->private; | |
1475 | struct ceph_osd_client *osdc = o->o_osdc; | |
1476 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
1477 | int ret = 0; | |
1478 | ||
1479 | if (force_new && o->o_authorizer) { | |
1480 | ac->ops->destroy_authorizer(ac, o->o_authorizer); | |
1481 | o->o_authorizer = NULL; | |
1482 | } | |
1483 | if (o->o_authorizer == NULL) { | |
1484 | ret = ac->ops->create_authorizer( | |
1485 | ac, CEPH_ENTITY_TYPE_OSD, | |
1486 | &o->o_authorizer, | |
1487 | &o->o_authorizer_buf, | |
1488 | &o->o_authorizer_buf_len, | |
1489 | &o->o_authorizer_reply_buf, | |
1490 | &o->o_authorizer_reply_buf_len); | |
1491 | if (ret) | |
1492 | return ret; | |
1493 | } | |
1494 | ||
1495 | *proto = ac->protocol; | |
1496 | *buf = o->o_authorizer_buf; | |
1497 | *len = o->o_authorizer_buf_len; | |
1498 | *reply_buf = o->o_authorizer_reply_buf; | |
1499 | *reply_len = o->o_authorizer_reply_buf_len; | |
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | ||
1504 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | |
1505 | { | |
1506 | struct ceph_osd *o = con->private; | |
1507 | struct ceph_osd_client *osdc = o->o_osdc; | |
1508 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
1509 | ||
1510 | return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len); | |
1511 | } | |
1512 | ||
9bd2e6f8 SW |
1513 | static int invalidate_authorizer(struct ceph_connection *con) |
1514 | { | |
1515 | struct ceph_osd *o = con->private; | |
1516 | struct ceph_osd_client *osdc = o->o_osdc; | |
1517 | struct ceph_auth_client *ac = osdc->client->monc.auth; | |
1518 | ||
1519 | if (ac->ops->invalidate_authorizer) | |
1520 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); | |
1521 | ||
1522 | return ceph_monc_validate_auth(&osdc->client->monc); | |
1523 | } | |
4e7a5dcd | 1524 | |
f24e9980 SW |
1525 | const static struct ceph_connection_operations osd_con_ops = { |
1526 | .get = get_osd_con, | |
1527 | .put = put_osd_con, | |
1528 | .dispatch = dispatch, | |
4e7a5dcd SW |
1529 | .get_authorizer = get_authorizer, |
1530 | .verify_authorizer_reply = verify_authorizer_reply, | |
9bd2e6f8 | 1531 | .invalidate_authorizer = invalidate_authorizer, |
f24e9980 | 1532 | .alloc_msg = alloc_msg, |
81b024e7 | 1533 | .fault = osd_reset, |
f24e9980 | 1534 | }; |