diff options
Diffstat (limited to 'fs/ceph/osd_client.c')
-rw-r--r-- | fs/ceph/osd_client.c | 1537 |
1 files changed, 1537 insertions, 0 deletions
diff --git a/fs/ceph/osd_client.c b/fs/ceph/osd_client.c new file mode 100644 index 000000000000..dbe63db9762f --- /dev/null +++ b/fs/ceph/osd_client.c | |||
@@ -0,0 +1,1537 @@ | |||
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" | ||
14 | #include "auth.h" | ||
15 | |||
16 | #define OSD_OP_FRONT_LEN 4096 | ||
17 | #define OSD_OPREPLY_FRONT_LEN 512 | ||
18 | |||
19 | const static struct ceph_connection_operations osd_con_ops; | ||
20 | static int __kick_requests(struct ceph_osd_client *osdc, | ||
21 | struct ceph_osd *kickosd); | ||
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 | |||
81 | /* | ||
82 | * requests | ||
83 | */ | ||
84 | void ceph_osdc_release_request(struct kref *kref) | ||
85 | { | ||
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); | ||
94 | if (req->r_con_filling_msg) { | ||
95 | dout("release_request revoking pages %p from con %p\n", | ||
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); | ||
100 | } | ||
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); | ||
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; | ||
139 | int num_op = 1 + do_sync; | ||
140 | size_t msg_size = sizeof(*head) + num_op*sizeof(*op); | ||
141 | int i; | ||
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) | ||
150 | return ERR_PTR(-ENOMEM); | ||
151 | |||
152 | req->r_osdc = osdc; | ||
153 | req->r_mempool = use_mempool; | ||
154 | kref_init(&req->r_kref); | ||
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 | |||
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, | ||
167 | OSD_OPREPLY_FRONT_LEN, 0, 0, NULL); | ||
168 | if (IS_ERR(msg)) { | ||
169 | ceph_osdc_put_request(req); | ||
170 | return ERR_PTR(PTR_ERR(msg)); | ||
171 | } | ||
172 | req->r_reply = msg; | ||
173 | |||
174 | /* create request message; allow space for oid */ | ||
175 | msg_size += 40; | ||
176 | if (snapc) | ||
177 | msg_size += sizeof(u64) * snapc->num_snaps; | ||
178 | if (use_mempool) | ||
179 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); | ||
180 | else | ||
181 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL); | ||
182 | if (IS_ERR(msg)) { | ||
183 | ceph_osdc_put_request(req); | ||
184 | return ERR_PTR(PTR_ERR(msg)); | ||
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 | } | ||
211 | op->extent.truncate_size = cpu_to_le64(truncate_size); | ||
212 | op->extent.truncate_seq = cpu_to_le32(truncate_seq); | ||
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 | |||
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); | ||
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); | ||
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 | /* | ||
306 | * If the osd connection drops, we need to resubmit all requests. | ||
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; | ||
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); | ||
336 | INIT_LIST_HEAD(&osd->o_osd_lru); | ||
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; | ||
343 | |||
344 | INIT_LIST_HEAD(&osd->o_keepalive_item); | ||
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); | ||
364 | if (atomic_dec_and_test(&osd->o_ref)) | ||
365 | kfree(osd); | ||
366 | } | ||
367 | |||
368 | /* | ||
369 | * remove an osd from our map | ||
370 | */ | ||
371 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) | ||
372 | { | ||
373 | dout("__remove_osd %p\n", osd); | ||
374 | BUG_ON(!list_empty(&osd->o_requests)); | ||
375 | rb_erase(&osd->o_node, &osdc->osds); | ||
376 | list_del_init(&osd->o_osd_lru); | ||
377 | ceph_con_close(&osd->o_con); | ||
378 | put_osd(osd); | ||
379 | } | ||
380 | |||
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 | |||
411 | /* | ||
412 | * reset osd connect | ||
413 | */ | ||
414 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) | ||
415 | { | ||
416 | int ret = 0; | ||
417 | |||
418 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); | ||
419 | if (list_empty(&osd->o_requests)) { | ||
420 | __remove_osd(osdc, osd); | ||
421 | } else { | ||
422 | ceph_con_close(&osd->o_con); | ||
423 | ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]); | ||
424 | osd->o_incarnation++; | ||
425 | } | ||
426 | return ret; | ||
427 | } | ||
428 | |||
429 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | ||
430 | { | ||
431 | struct rb_node **p = &osdc->osds.rb_node; | ||
432 | struct rb_node *parent = NULL; | ||
433 | struct ceph_osd *osd = NULL; | ||
434 | |||
435 | while (*p) { | ||
436 | parent = *p; | ||
437 | osd = rb_entry(parent, struct ceph_osd, o_node); | ||
438 | if (new->o_osd < osd->o_osd) | ||
439 | p = &(*p)->rb_left; | ||
440 | else if (new->o_osd > osd->o_osd) | ||
441 | p = &(*p)->rb_right; | ||
442 | else | ||
443 | BUG(); | ||
444 | } | ||
445 | |||
446 | rb_link_node(&new->o_node, parent, p); | ||
447 | rb_insert_color(&new->o_node, &osdc->osds); | ||
448 | } | ||
449 | |||
450 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | ||
451 | { | ||
452 | struct ceph_osd *osd; | ||
453 | struct rb_node *n = osdc->osds.rb_node; | ||
454 | |||
455 | while (n) { | ||
456 | osd = rb_entry(n, struct ceph_osd, o_node); | ||
457 | if (o < osd->o_osd) | ||
458 | n = n->rb_left; | ||
459 | else if (o > osd->o_osd) | ||
460 | n = n->rb_right; | ||
461 | else | ||
462 | return osd; | ||
463 | } | ||
464 | return NULL; | ||
465 | } | ||
466 | |||
467 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) | ||
468 | { | ||
469 | schedule_delayed_work(&osdc->timeout_work, | ||
470 | osdc->client->mount_args->osd_keepalive_timeout * HZ); | ||
471 | } | ||
472 | |||
473 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | ||
474 | { | ||
475 | cancel_delayed_work(&osdc->timeout_work); | ||
476 | } | ||
477 | |||
478 | /* | ||
479 | * Register request, assign tid. If this is the first request, set up | ||
480 | * the timeout event. | ||
481 | */ | ||
482 | static void register_request(struct ceph_osd_client *osdc, | ||
483 | struct ceph_osd_request *req) | ||
484 | { | ||
485 | mutex_lock(&osdc->request_mutex); | ||
486 | req->r_tid = ++osdc->last_tid; | ||
487 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); | ||
488 | INIT_LIST_HEAD(&req->r_req_lru_item); | ||
489 | |||
490 | dout("register_request %p tid %lld\n", req, req->r_tid); | ||
491 | __insert_request(osdc, req); | ||
492 | ceph_osdc_get_request(req); | ||
493 | osdc->num_requests++; | ||
494 | |||
495 | if (osdc->num_requests == 1) { | ||
496 | dout(" first request, scheduling timeout\n"); | ||
497 | __schedule_osd_timeout(osdc); | ||
498 | } | ||
499 | mutex_unlock(&osdc->request_mutex); | ||
500 | } | ||
501 | |||
502 | /* | ||
503 | * called under osdc->request_mutex | ||
504 | */ | ||
505 | static void __unregister_request(struct ceph_osd_client *osdc, | ||
506 | struct ceph_osd_request *req) | ||
507 | { | ||
508 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | ||
509 | rb_erase(&req->r_node, &osdc->requests); | ||
510 | osdc->num_requests--; | ||
511 | |||
512 | if (req->r_osd) { | ||
513 | /* make sure the original request isn't in flight. */ | ||
514 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | ||
515 | |||
516 | list_del_init(&req->r_osd_item); | ||
517 | if (list_empty(&req->r_osd->o_requests)) | ||
518 | __move_osd_to_lru(osdc, req->r_osd); | ||
519 | req->r_osd = NULL; | ||
520 | } | ||
521 | |||
522 | ceph_osdc_put_request(req); | ||
523 | |||
524 | list_del_init(&req->r_req_lru_item); | ||
525 | if (osdc->num_requests == 0) { | ||
526 | dout(" no requests, canceling timeout\n"); | ||
527 | __cancel_osd_timeout(osdc); | ||
528 | } | ||
529 | } | ||
530 | |||
531 | /* | ||
532 | * Cancel a previously queued request message | ||
533 | */ | ||
534 | static void __cancel_request(struct ceph_osd_request *req) | ||
535 | { | ||
536 | if (req->r_sent) { | ||
537 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | ||
538 | req->r_sent = 0; | ||
539 | } | ||
540 | list_del_init(&req->r_req_lru_item); | ||
541 | } | ||
542 | |||
543 | /* | ||
544 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | ||
545 | * (as needed), and set the request r_osd appropriately. If there is | ||
546 | * no up osd, set r_osd to NULL. | ||
547 | * | ||
548 | * Return 0 if unchanged, 1 if changed, or negative on error. | ||
549 | * | ||
550 | * Caller should hold map_sem for read and request_mutex. | ||
551 | */ | ||
552 | static int __map_osds(struct ceph_osd_client *osdc, | ||
553 | struct ceph_osd_request *req) | ||
554 | { | ||
555 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | ||
556 | struct ceph_pg pgid; | ||
557 | int o = -1; | ||
558 | int err; | ||
559 | |||
560 | dout("map_osds %p tid %lld\n", req, req->r_tid); | ||
561 | err = ceph_calc_object_layout(&reqhead->layout, req->r_oid, | ||
562 | &req->r_file_layout, osdc->osdmap); | ||
563 | if (err) | ||
564 | return err; | ||
565 | pgid = reqhead->layout.ol_pgid; | ||
566 | req->r_pgid = pgid; | ||
567 | |||
568 | o = ceph_calc_pg_primary(osdc->osdmap, pgid); | ||
569 | |||
570 | if ((req->r_osd && req->r_osd->o_osd == o && | ||
571 | req->r_sent >= req->r_osd->o_incarnation) || | ||
572 | (req->r_osd == NULL && o == -1)) | ||
573 | return 0; /* no change */ | ||
574 | |||
575 | dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n", | ||
576 | req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o, | ||
577 | req->r_osd ? req->r_osd->o_osd : -1); | ||
578 | |||
579 | if (req->r_osd) { | ||
580 | __cancel_request(req); | ||
581 | list_del_init(&req->r_osd_item); | ||
582 | req->r_osd = NULL; | ||
583 | } | ||
584 | |||
585 | req->r_osd = __lookup_osd(osdc, o); | ||
586 | if (!req->r_osd && o >= 0) { | ||
587 | err = -ENOMEM; | ||
588 | req->r_osd = create_osd(osdc); | ||
589 | if (!req->r_osd) | ||
590 | goto out; | ||
591 | |||
592 | dout("map_osds osd %p is osd%d\n", req->r_osd, o); | ||
593 | req->r_osd->o_osd = o; | ||
594 | req->r_osd->o_con.peer_name.num = cpu_to_le64(o); | ||
595 | __insert_osd(osdc, req->r_osd); | ||
596 | |||
597 | ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]); | ||
598 | } | ||
599 | |||
600 | if (req->r_osd) { | ||
601 | __remove_osd_from_lru(req->r_osd); | ||
602 | list_add(&req->r_osd_item, &req->r_osd->o_requests); | ||
603 | } | ||
604 | err = 1; /* osd changed */ | ||
605 | |||
606 | out: | ||
607 | return err; | ||
608 | } | ||
609 | |||
610 | /* | ||
611 | * caller should hold map_sem (for read) and request_mutex | ||
612 | */ | ||
613 | static int __send_request(struct ceph_osd_client *osdc, | ||
614 | struct ceph_osd_request *req) | ||
615 | { | ||
616 | struct ceph_osd_request_head *reqhead; | ||
617 | int err; | ||
618 | |||
619 | err = __map_osds(osdc, req); | ||
620 | if (err < 0) | ||
621 | return err; | ||
622 | if (req->r_osd == NULL) { | ||
623 | dout("send_request %p no up osds in pg\n", req); | ||
624 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
625 | return 0; | ||
626 | } | ||
627 | |||
628 | dout("send_request %p tid %llu to osd%d flags %d\n", | ||
629 | req, req->r_tid, req->r_osd->o_osd, req->r_flags); | ||
630 | |||
631 | reqhead = req->r_request->front.iov_base; | ||
632 | reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch); | ||
633 | reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */ | ||
634 | reqhead->reassert_version = req->r_reassert_version; | ||
635 | |||
636 | req->r_sent_stamp = jiffies; | ||
637 | list_move_tail(&osdc->req_lru, &req->r_req_lru_item); | ||
638 | |||
639 | ceph_msg_get(req->r_request); /* send consumes a ref */ | ||
640 | ceph_con_send(&req->r_osd->o_con, req->r_request); | ||
641 | req->r_sent = req->r_osd->o_incarnation; | ||
642 | return 0; | ||
643 | } | ||
644 | |||
645 | /* | ||
646 | * Timeout callback, called every N seconds when 1 or more osd | ||
647 | * requests has been active for more than N seconds. When this | ||
648 | * happens, we ping all OSDs with requests who have timed out to | ||
649 | * ensure any communications channel reset is detected. Reset the | ||
650 | * request timeouts another N seconds in the future as we go. | ||
651 | * Reschedule the timeout event another N seconds in future (unless | ||
652 | * there are no open requests). | ||
653 | */ | ||
654 | static void handle_timeout(struct work_struct *work) | ||
655 | { | ||
656 | struct ceph_osd_client *osdc = | ||
657 | container_of(work, struct ceph_osd_client, timeout_work.work); | ||
658 | struct ceph_osd_request *req, *last_req = NULL; | ||
659 | struct ceph_osd *osd; | ||
660 | unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ; | ||
661 | unsigned long keepalive = | ||
662 | osdc->client->mount_args->osd_keepalive_timeout * HZ; | ||
663 | unsigned long last_sent = 0; | ||
664 | struct rb_node *p; | ||
665 | struct list_head slow_osds; | ||
666 | |||
667 | dout("timeout\n"); | ||
668 | down_read(&osdc->map_sem); | ||
669 | |||
670 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
671 | |||
672 | mutex_lock(&osdc->request_mutex); | ||
673 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | ||
674 | req = rb_entry(p, struct ceph_osd_request, r_node); | ||
675 | |||
676 | if (req->r_resend) { | ||
677 | int err; | ||
678 | |||
679 | dout("osdc resending prev failed %lld\n", req->r_tid); | ||
680 | err = __send_request(osdc, req); | ||
681 | if (err) | ||
682 | dout("osdc failed again on %lld\n", req->r_tid); | ||
683 | else | ||
684 | req->r_resend = false; | ||
685 | continue; | ||
686 | } | ||
687 | } | ||
688 | |||
689 | /* | ||
690 | * reset osds that appear to be _really_ unresponsive. this | ||
691 | * is a failsafe measure.. we really shouldn't be getting to | ||
692 | * this point if the system is working properly. the monitors | ||
693 | * should mark the osd as failed and we should find out about | ||
694 | * it from an updated osd map. | ||
695 | */ | ||
696 | while (!list_empty(&osdc->req_lru)) { | ||
697 | req = list_entry(osdc->req_lru.next, struct ceph_osd_request, | ||
698 | r_req_lru_item); | ||
699 | |||
700 | if (time_before(jiffies, req->r_sent_stamp + timeout)) | ||
701 | break; | ||
702 | |||
703 | BUG_ON(req == last_req && req->r_sent_stamp == last_sent); | ||
704 | last_req = req; | ||
705 | last_sent = req->r_sent_stamp; | ||
706 | |||
707 | osd = req->r_osd; | ||
708 | BUG_ON(!osd); | ||
709 | pr_warning(" tid %llu timed out on osd%d, will reset osd\n", | ||
710 | req->r_tid, osd->o_osd); | ||
711 | __kick_requests(osdc, osd); | ||
712 | } | ||
713 | |||
714 | /* | ||
715 | * ping osds that are a bit slow. this ensures that if there | ||
716 | * is a break in the TCP connection we will notice, and reopen | ||
717 | * a connection with that osd (from the fault callback). | ||
718 | */ | ||
719 | INIT_LIST_HEAD(&slow_osds); | ||
720 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | ||
721 | if (time_before(jiffies, req->r_sent_stamp + keepalive)) | ||
722 | break; | ||
723 | |||
724 | osd = req->r_osd; | ||
725 | BUG_ON(!osd); | ||
726 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | ||
727 | req->r_tid, osd->o_osd); | ||
728 | list_move_tail(&osd->o_keepalive_item, &slow_osds); | ||
729 | } | ||
730 | while (!list_empty(&slow_osds)) { | ||
731 | osd = list_entry(slow_osds.next, struct ceph_osd, | ||
732 | o_keepalive_item); | ||
733 | list_del_init(&osd->o_keepalive_item); | ||
734 | ceph_con_keepalive(&osd->o_con); | ||
735 | } | ||
736 | |||
737 | __schedule_osd_timeout(osdc); | ||
738 | mutex_unlock(&osdc->request_mutex); | ||
739 | |||
740 | up_read(&osdc->map_sem); | ||
741 | } | ||
742 | |||
743 | static void handle_osds_timeout(struct work_struct *work) | ||
744 | { | ||
745 | struct ceph_osd_client *osdc = | ||
746 | container_of(work, struct ceph_osd_client, | ||
747 | osds_timeout_work.work); | ||
748 | unsigned long delay = | ||
749 | osdc->client->mount_args->osd_idle_ttl * HZ >> 2; | ||
750 | |||
751 | dout("osds timeout\n"); | ||
752 | down_read(&osdc->map_sem); | ||
753 | remove_old_osds(osdc, 0); | ||
754 | up_read(&osdc->map_sem); | ||
755 | |||
756 | schedule_delayed_work(&osdc->osds_timeout_work, | ||
757 | round_jiffies_relative(delay)); | ||
758 | } | ||
759 | |||
760 | /* | ||
761 | * handle osd op reply. either call the callback if it is specified, | ||
762 | * or do the completion to wake up the waiting thread. | ||
763 | */ | ||
764 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, | ||
765 | struct ceph_connection *con) | ||
766 | { | ||
767 | struct ceph_osd_reply_head *rhead = msg->front.iov_base; | ||
768 | struct ceph_osd_request *req; | ||
769 | u64 tid; | ||
770 | int numops, object_len, flags; | ||
771 | |||
772 | tid = le64_to_cpu(msg->hdr.tid); | ||
773 | if (msg->front.iov_len < sizeof(*rhead)) | ||
774 | goto bad; | ||
775 | numops = le32_to_cpu(rhead->num_ops); | ||
776 | object_len = le32_to_cpu(rhead->object_len); | ||
777 | if (msg->front.iov_len != sizeof(*rhead) + object_len + | ||
778 | numops * sizeof(struct ceph_osd_op)) | ||
779 | goto bad; | ||
780 | dout("handle_reply %p tid %llu\n", msg, tid); | ||
781 | |||
782 | /* lookup */ | ||
783 | mutex_lock(&osdc->request_mutex); | ||
784 | req = __lookup_request(osdc, tid); | ||
785 | if (req == NULL) { | ||
786 | dout("handle_reply tid %llu dne\n", tid); | ||
787 | mutex_unlock(&osdc->request_mutex); | ||
788 | return; | ||
789 | } | ||
790 | ceph_osdc_get_request(req); | ||
791 | flags = le32_to_cpu(rhead->flags); | ||
792 | |||
793 | /* | ||
794 | * if this connection filled our message, drop our reference now, to | ||
795 | * avoid a (safe but slower) revoke later. | ||
796 | */ | ||
797 | if (req->r_con_filling_msg == con && req->r_reply == msg) { | ||
798 | dout(" dropping con_filling_msg ref %p\n", con); | ||
799 | req->r_con_filling_msg = NULL; | ||
800 | ceph_con_put(con); | ||
801 | } | ||
802 | |||
803 | if (!req->r_got_reply) { | ||
804 | unsigned bytes; | ||
805 | |||
806 | req->r_result = le32_to_cpu(rhead->result); | ||
807 | bytes = le32_to_cpu(msg->hdr.data_len); | ||
808 | dout("handle_reply result %d bytes %d\n", req->r_result, | ||
809 | bytes); | ||
810 | if (req->r_result == 0) | ||
811 | req->r_result = bytes; | ||
812 | |||
813 | /* in case this is a write and we need to replay, */ | ||
814 | req->r_reassert_version = rhead->reassert_version; | ||
815 | |||
816 | req->r_got_reply = 1; | ||
817 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | ||
818 | dout("handle_reply tid %llu dup ack\n", tid); | ||
819 | mutex_unlock(&osdc->request_mutex); | ||
820 | goto done; | ||
821 | } | ||
822 | |||
823 | dout("handle_reply tid %llu flags %d\n", tid, flags); | ||
824 | |||
825 | /* either this is a read, or we got the safe response */ | ||
826 | if ((flags & CEPH_OSD_FLAG_ONDISK) || | ||
827 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) | ||
828 | __unregister_request(osdc, req); | ||
829 | |||
830 | mutex_unlock(&osdc->request_mutex); | ||
831 | |||
832 | if (req->r_callback) | ||
833 | req->r_callback(req, msg); | ||
834 | else | ||
835 | complete(&req->r_completion); | ||
836 | |||
837 | if (flags & CEPH_OSD_FLAG_ONDISK) { | ||
838 | if (req->r_safe_callback) | ||
839 | req->r_safe_callback(req, msg); | ||
840 | complete(&req->r_safe_completion); /* fsync waiter */ | ||
841 | } | ||
842 | |||
843 | done: | ||
844 | ceph_osdc_put_request(req); | ||
845 | return; | ||
846 | |||
847 | bad: | ||
848 | pr_err("corrupt osd_op_reply got %d %d expected %d\n", | ||
849 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len), | ||
850 | (int)sizeof(*rhead)); | ||
851 | ceph_msg_dump(msg); | ||
852 | } | ||
853 | |||
854 | |||
855 | static int __kick_requests(struct ceph_osd_client *osdc, | ||
856 | struct ceph_osd *kickosd) | ||
857 | { | ||
858 | struct ceph_osd_request *req; | ||
859 | struct rb_node *p, *n; | ||
860 | int needmap = 0; | ||
861 | int err; | ||
862 | |||
863 | dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1); | ||
864 | if (kickosd) { | ||
865 | __reset_osd(osdc, kickosd); | ||
866 | } else { | ||
867 | for (p = rb_first(&osdc->osds); p; p = n) { | ||
868 | struct ceph_osd *osd = | ||
869 | rb_entry(p, struct ceph_osd, o_node); | ||
870 | |||
871 | n = rb_next(p); | ||
872 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | ||
873 | memcmp(&osd->o_con.peer_addr, | ||
874 | ceph_osd_addr(osdc->osdmap, | ||
875 | osd->o_osd), | ||
876 | sizeof(struct ceph_entity_addr)) != 0) | ||
877 | __reset_osd(osdc, osd); | ||
878 | } | ||
879 | } | ||
880 | |||
881 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | ||
882 | req = rb_entry(p, struct ceph_osd_request, r_node); | ||
883 | |||
884 | if (req->r_resend) { | ||
885 | dout(" r_resend set on tid %llu\n", req->r_tid); | ||
886 | __cancel_request(req); | ||
887 | goto kick; | ||
888 | } | ||
889 | if (req->r_osd && kickosd == req->r_osd) { | ||
890 | __cancel_request(req); | ||
891 | goto kick; | ||
892 | } | ||
893 | |||
894 | err = __map_osds(osdc, req); | ||
895 | if (err == 0) | ||
896 | continue; /* no change */ | ||
897 | if (err < 0) { | ||
898 | /* | ||
899 | * FIXME: really, we should set the request | ||
900 | * error and fail if this isn't a 'nofail' | ||
901 | * request, but that's a fair bit more | ||
902 | * complicated to do. So retry! | ||
903 | */ | ||
904 | dout(" setting r_resend on %llu\n", req->r_tid); | ||
905 | req->r_resend = true; | ||
906 | continue; | ||
907 | } | ||
908 | if (req->r_osd == NULL) { | ||
909 | dout("tid %llu maps to no valid osd\n", req->r_tid); | ||
910 | needmap++; /* request a newer map */ | ||
911 | continue; | ||
912 | } | ||
913 | |||
914 | kick: | ||
915 | dout("kicking %p tid %llu osd%d\n", req, req->r_tid, | ||
916 | req->r_osd->o_osd); | ||
917 | req->r_flags |= CEPH_OSD_FLAG_RETRY; | ||
918 | err = __send_request(osdc, req); | ||
919 | if (err) { | ||
920 | dout(" setting r_resend on %llu\n", req->r_tid); | ||
921 | req->r_resend = true; | ||
922 | } | ||
923 | } | ||
924 | |||
925 | return needmap; | ||
926 | } | ||
927 | |||
928 | /* | ||
929 | * Resubmit osd requests whose osd or osd address has changed. Request | ||
930 | * a new osd map if osds are down, or we are otherwise unable to determine | ||
931 | * how to direct a request. | ||
932 | * | ||
933 | * Close connections to down osds. | ||
934 | * | ||
935 | * If @who is specified, resubmit requests for that specific osd. | ||
936 | * | ||
937 | * Caller should hold map_sem for read and request_mutex. | ||
938 | */ | ||
939 | static void kick_requests(struct ceph_osd_client *osdc, | ||
940 | struct ceph_osd *kickosd) | ||
941 | { | ||
942 | int needmap; | ||
943 | |||
944 | mutex_lock(&osdc->request_mutex); | ||
945 | needmap = __kick_requests(osdc, kickosd); | ||
946 | mutex_unlock(&osdc->request_mutex); | ||
947 | |||
948 | if (needmap) { | ||
949 | dout("%d requests for down osds, need new map\n", needmap); | ||
950 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
951 | } | ||
952 | |||
953 | } | ||
954 | /* | ||
955 | * Process updated osd map. | ||
956 | * | ||
957 | * The message contains any number of incremental and full maps, normally | ||
958 | * indicating some sort of topology change in the cluster. Kick requests | ||
959 | * off to different OSDs as needed. | ||
960 | */ | ||
961 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | ||
962 | { | ||
963 | void *p, *end, *next; | ||
964 | u32 nr_maps, maplen; | ||
965 | u32 epoch; | ||
966 | struct ceph_osdmap *newmap = NULL, *oldmap; | ||
967 | int err; | ||
968 | struct ceph_fsid fsid; | ||
969 | |||
970 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | ||
971 | p = msg->front.iov_base; | ||
972 | end = p + msg->front.iov_len; | ||
973 | |||
974 | /* verify fsid */ | ||
975 | ceph_decode_need(&p, end, sizeof(fsid), bad); | ||
976 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | ||
977 | if (ceph_check_fsid(osdc->client, &fsid) < 0) | ||
978 | return; | ||
979 | |||
980 | down_write(&osdc->map_sem); | ||
981 | |||
982 | /* incremental maps */ | ||
983 | ceph_decode_32_safe(&p, end, nr_maps, bad); | ||
984 | dout(" %d inc maps\n", nr_maps); | ||
985 | while (nr_maps > 0) { | ||
986 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | ||
987 | epoch = ceph_decode_32(&p); | ||
988 | maplen = ceph_decode_32(&p); | ||
989 | ceph_decode_need(&p, end, maplen, bad); | ||
990 | next = p + maplen; | ||
991 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | ||
992 | dout("applying incremental map %u len %d\n", | ||
993 | epoch, maplen); | ||
994 | newmap = osdmap_apply_incremental(&p, next, | ||
995 | osdc->osdmap, | ||
996 | osdc->client->msgr); | ||
997 | if (IS_ERR(newmap)) { | ||
998 | err = PTR_ERR(newmap); | ||
999 | goto bad; | ||
1000 | } | ||
1001 | BUG_ON(!newmap); | ||
1002 | if (newmap != osdc->osdmap) { | ||
1003 | ceph_osdmap_destroy(osdc->osdmap); | ||
1004 | osdc->osdmap = newmap; | ||
1005 | } | ||
1006 | } else { | ||
1007 | dout("ignoring incremental map %u len %d\n", | ||
1008 | epoch, maplen); | ||
1009 | } | ||
1010 | p = next; | ||
1011 | nr_maps--; | ||
1012 | } | ||
1013 | if (newmap) | ||
1014 | goto done; | ||
1015 | |||
1016 | /* full maps */ | ||
1017 | ceph_decode_32_safe(&p, end, nr_maps, bad); | ||
1018 | dout(" %d full maps\n", nr_maps); | ||
1019 | while (nr_maps) { | ||
1020 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | ||
1021 | epoch = ceph_decode_32(&p); | ||
1022 | maplen = ceph_decode_32(&p); | ||
1023 | ceph_decode_need(&p, end, maplen, bad); | ||
1024 | if (nr_maps > 1) { | ||
1025 | dout("skipping non-latest full map %u len %d\n", | ||
1026 | epoch, maplen); | ||
1027 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | ||
1028 | dout("skipping full map %u len %d, " | ||
1029 | "older than our %u\n", epoch, maplen, | ||
1030 | osdc->osdmap->epoch); | ||
1031 | } else { | ||
1032 | dout("taking full map %u len %d\n", epoch, maplen); | ||
1033 | newmap = osdmap_decode(&p, p+maplen); | ||
1034 | if (IS_ERR(newmap)) { | ||
1035 | err = PTR_ERR(newmap); | ||
1036 | goto bad; | ||
1037 | } | ||
1038 | BUG_ON(!newmap); | ||
1039 | oldmap = osdc->osdmap; | ||
1040 | osdc->osdmap = newmap; | ||
1041 | if (oldmap) | ||
1042 | ceph_osdmap_destroy(oldmap); | ||
1043 | } | ||
1044 | p += maplen; | ||
1045 | nr_maps--; | ||
1046 | } | ||
1047 | |||
1048 | done: | ||
1049 | downgrade_write(&osdc->map_sem); | ||
1050 | ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); | ||
1051 | if (newmap) | ||
1052 | kick_requests(osdc, NULL); | ||
1053 | up_read(&osdc->map_sem); | ||
1054 | return; | ||
1055 | |||
1056 | bad: | ||
1057 | pr_err("osdc handle_map corrupt msg\n"); | ||
1058 | ceph_msg_dump(msg); | ||
1059 | up_write(&osdc->map_sem); | ||
1060 | return; | ||
1061 | } | ||
1062 | |||
1063 | |||
1064 | /* | ||
1065 | * A read request prepares specific pages that data is to be read into. | ||
1066 | * When a message is being read off the wire, we call prepare_pages to | ||
1067 | * find those pages. | ||
1068 | * 0 = success, -1 failure. | ||
1069 | */ | ||
1070 | static int __prepare_pages(struct ceph_connection *con, | ||
1071 | struct ceph_msg_header *hdr, | ||
1072 | struct ceph_osd_request *req, | ||
1073 | u64 tid, | ||
1074 | struct ceph_msg *m) | ||
1075 | { | ||
1076 | struct ceph_osd *osd = con->private; | ||
1077 | struct ceph_osd_client *osdc; | ||
1078 | int ret = -1; | ||
1079 | int data_len = le32_to_cpu(hdr->data_len); | ||
1080 | unsigned data_off = le16_to_cpu(hdr->data_off); | ||
1081 | |||
1082 | int want = calc_pages_for(data_off & ~PAGE_MASK, data_len); | ||
1083 | |||
1084 | if (!osd) | ||
1085 | return -1; | ||
1086 | |||
1087 | osdc = osd->o_osdc; | ||
1088 | |||
1089 | dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m, | ||
1090 | tid, req->r_num_pages, want); | ||
1091 | if (unlikely(req->r_num_pages < want)) | ||
1092 | goto out; | ||
1093 | m->pages = req->r_pages; | ||
1094 | m->nr_pages = req->r_num_pages; | ||
1095 | ret = 0; /* success */ | ||
1096 | out: | ||
1097 | BUG_ON(ret < 0 || m->nr_pages < want); | ||
1098 | |||
1099 | return ret; | ||
1100 | } | ||
1101 | |||
1102 | /* | ||
1103 | * Register request, send initial attempt. | ||
1104 | */ | ||
1105 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | ||
1106 | struct ceph_osd_request *req, | ||
1107 | bool nofail) | ||
1108 | { | ||
1109 | int rc = 0; | ||
1110 | |||
1111 | req->r_request->pages = req->r_pages; | ||
1112 | req->r_request->nr_pages = req->r_num_pages; | ||
1113 | |||
1114 | register_request(osdc, req); | ||
1115 | |||
1116 | down_read(&osdc->map_sem); | ||
1117 | mutex_lock(&osdc->request_mutex); | ||
1118 | /* | ||
1119 | * a racing kick_requests() may have sent the message for us | ||
1120 | * while we dropped request_mutex above, so only send now if | ||
1121 | * the request still han't been touched yet. | ||
1122 | */ | ||
1123 | if (req->r_sent == 0) { | ||
1124 | rc = __send_request(osdc, req); | ||
1125 | if (rc) { | ||
1126 | if (nofail) { | ||
1127 | dout("osdc_start_request failed send, " | ||
1128 | " marking %lld\n", req->r_tid); | ||
1129 | req->r_resend = true; | ||
1130 | rc = 0; | ||
1131 | } else { | ||
1132 | __unregister_request(osdc, req); | ||
1133 | } | ||
1134 | } | ||
1135 | } | ||
1136 | mutex_unlock(&osdc->request_mutex); | ||
1137 | up_read(&osdc->map_sem); | ||
1138 | return rc; | ||
1139 | } | ||
1140 | |||
1141 | /* | ||
1142 | * wait for a request to complete | ||
1143 | */ | ||
1144 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | ||
1145 | struct ceph_osd_request *req) | ||
1146 | { | ||
1147 | int rc; | ||
1148 | |||
1149 | rc = wait_for_completion_interruptible(&req->r_completion); | ||
1150 | if (rc < 0) { | ||
1151 | mutex_lock(&osdc->request_mutex); | ||
1152 | __cancel_request(req); | ||
1153 | __unregister_request(osdc, req); | ||
1154 | mutex_unlock(&osdc->request_mutex); | ||
1155 | dout("wait_request tid %llu canceled/timed out\n", req->r_tid); | ||
1156 | return rc; | ||
1157 | } | ||
1158 | |||
1159 | dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); | ||
1160 | return req->r_result; | ||
1161 | } | ||
1162 | |||
1163 | /* | ||
1164 | * sync - wait for all in-flight requests to flush. avoid starvation. | ||
1165 | */ | ||
1166 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | ||
1167 | { | ||
1168 | struct ceph_osd_request *req; | ||
1169 | u64 last_tid, next_tid = 0; | ||
1170 | |||
1171 | mutex_lock(&osdc->request_mutex); | ||
1172 | last_tid = osdc->last_tid; | ||
1173 | while (1) { | ||
1174 | req = __lookup_request_ge(osdc, next_tid); | ||
1175 | if (!req) | ||
1176 | break; | ||
1177 | if (req->r_tid > last_tid) | ||
1178 | break; | ||
1179 | |||
1180 | next_tid = req->r_tid + 1; | ||
1181 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | ||
1182 | continue; | ||
1183 | |||
1184 | ceph_osdc_get_request(req); | ||
1185 | mutex_unlock(&osdc->request_mutex); | ||
1186 | dout("sync waiting on tid %llu (last is %llu)\n", | ||
1187 | req->r_tid, last_tid); | ||
1188 | wait_for_completion(&req->r_safe_completion); | ||
1189 | mutex_lock(&osdc->request_mutex); | ||
1190 | ceph_osdc_put_request(req); | ||
1191 | } | ||
1192 | mutex_unlock(&osdc->request_mutex); | ||
1193 | dout("sync done (thru tid %llu)\n", last_tid); | ||
1194 | } | ||
1195 | |||
1196 | /* | ||
1197 | * init, shutdown | ||
1198 | */ | ||
1199 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | ||
1200 | { | ||
1201 | int err; | ||
1202 | |||
1203 | dout("init\n"); | ||
1204 | osdc->client = client; | ||
1205 | osdc->osdmap = NULL; | ||
1206 | init_rwsem(&osdc->map_sem); | ||
1207 | init_completion(&osdc->map_waiters); | ||
1208 | osdc->last_requested_map = 0; | ||
1209 | mutex_init(&osdc->request_mutex); | ||
1210 | osdc->last_tid = 0; | ||
1211 | osdc->osds = RB_ROOT; | ||
1212 | INIT_LIST_HEAD(&osdc->osd_lru); | ||
1213 | osdc->requests = RB_ROOT; | ||
1214 | INIT_LIST_HEAD(&osdc->req_lru); | ||
1215 | osdc->num_requests = 0; | ||
1216 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | ||
1217 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); | ||
1218 | |||
1219 | schedule_delayed_work(&osdc->osds_timeout_work, | ||
1220 | round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ)); | ||
1221 | |||
1222 | err = -ENOMEM; | ||
1223 | osdc->req_mempool = mempool_create_kmalloc_pool(10, | ||
1224 | sizeof(struct ceph_osd_request)); | ||
1225 | if (!osdc->req_mempool) | ||
1226 | goto out; | ||
1227 | |||
1228 | err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true); | ||
1229 | if (err < 0) | ||
1230 | goto out_mempool; | ||
1231 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, | ||
1232 | OSD_OPREPLY_FRONT_LEN, 10, true); | ||
1233 | if (err < 0) | ||
1234 | goto out_msgpool; | ||
1235 | return 0; | ||
1236 | |||
1237 | out_msgpool: | ||
1238 | ceph_msgpool_destroy(&osdc->msgpool_op); | ||
1239 | out_mempool: | ||
1240 | mempool_destroy(osdc->req_mempool); | ||
1241 | out: | ||
1242 | return err; | ||
1243 | } | ||
1244 | |||
1245 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | ||
1246 | { | ||
1247 | cancel_delayed_work_sync(&osdc->timeout_work); | ||
1248 | cancel_delayed_work_sync(&osdc->osds_timeout_work); | ||
1249 | if (osdc->osdmap) { | ||
1250 | ceph_osdmap_destroy(osdc->osdmap); | ||
1251 | osdc->osdmap = NULL; | ||
1252 | } | ||
1253 | remove_old_osds(osdc, 1); | ||
1254 | mempool_destroy(osdc->req_mempool); | ||
1255 | ceph_msgpool_destroy(&osdc->msgpool_op); | ||
1256 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); | ||
1257 | } | ||
1258 | |||
1259 | /* | ||
1260 | * Read some contiguous pages. If we cross a stripe boundary, shorten | ||
1261 | * *plen. Return number of bytes read, or error. | ||
1262 | */ | ||
1263 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | ||
1264 | struct ceph_vino vino, struct ceph_file_layout *layout, | ||
1265 | u64 off, u64 *plen, | ||
1266 | u32 truncate_seq, u64 truncate_size, | ||
1267 | struct page **pages, int num_pages) | ||
1268 | { | ||
1269 | struct ceph_osd_request *req; | ||
1270 | int rc = 0; | ||
1271 | |||
1272 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | ||
1273 | vino.snap, off, *plen); | ||
1274 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, | ||
1275 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, | ||
1276 | NULL, 0, truncate_seq, truncate_size, NULL, | ||
1277 | false, 1); | ||
1278 | if (IS_ERR(req)) | ||
1279 | return PTR_ERR(req); | ||
1280 | |||
1281 | /* it may be a short read due to an object boundary */ | ||
1282 | req->r_pages = pages; | ||
1283 | num_pages = calc_pages_for(off, *plen); | ||
1284 | req->r_num_pages = num_pages; | ||
1285 | |||
1286 | dout("readpages final extent is %llu~%llu (%d pages)\n", | ||
1287 | off, *plen, req->r_num_pages); | ||
1288 | |||
1289 | rc = ceph_osdc_start_request(osdc, req, false); | ||
1290 | if (!rc) | ||
1291 | rc = ceph_osdc_wait_request(osdc, req); | ||
1292 | |||
1293 | ceph_osdc_put_request(req); | ||
1294 | dout("readpages result %d\n", rc); | ||
1295 | return rc; | ||
1296 | } | ||
1297 | |||
1298 | /* | ||
1299 | * do a synchronous write on N pages | ||
1300 | */ | ||
1301 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | ||
1302 | struct ceph_file_layout *layout, | ||
1303 | struct ceph_snap_context *snapc, | ||
1304 | u64 off, u64 len, | ||
1305 | u32 truncate_seq, u64 truncate_size, | ||
1306 | struct timespec *mtime, | ||
1307 | struct page **pages, int num_pages, | ||
1308 | int flags, int do_sync, bool nofail) | ||
1309 | { | ||
1310 | struct ceph_osd_request *req; | ||
1311 | int rc = 0; | ||
1312 | |||
1313 | BUG_ON(vino.snap != CEPH_NOSNAP); | ||
1314 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, | ||
1315 | CEPH_OSD_OP_WRITE, | ||
1316 | flags | CEPH_OSD_FLAG_ONDISK | | ||
1317 | CEPH_OSD_FLAG_WRITE, | ||
1318 | snapc, do_sync, | ||
1319 | truncate_seq, truncate_size, mtime, | ||
1320 | nofail, 1); | ||
1321 | if (IS_ERR(req)) | ||
1322 | return PTR_ERR(req); | ||
1323 | |||
1324 | /* it may be a short write due to an object boundary */ | ||
1325 | req->r_pages = pages; | ||
1326 | req->r_num_pages = calc_pages_for(off, len); | ||
1327 | dout("writepages %llu~%llu (%d pages)\n", off, len, | ||
1328 | req->r_num_pages); | ||
1329 | |||
1330 | rc = ceph_osdc_start_request(osdc, req, nofail); | ||
1331 | if (!rc) | ||
1332 | rc = ceph_osdc_wait_request(osdc, req); | ||
1333 | |||
1334 | ceph_osdc_put_request(req); | ||
1335 | if (rc == 0) | ||
1336 | rc = len; | ||
1337 | dout("writepages result %d\n", rc); | ||
1338 | return rc; | ||
1339 | } | ||
1340 | |||
1341 | /* | ||
1342 | * handle incoming message | ||
1343 | */ | ||
1344 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | ||
1345 | { | ||
1346 | struct ceph_osd *osd = con->private; | ||
1347 | struct ceph_osd_client *osdc; | ||
1348 | int type = le16_to_cpu(msg->hdr.type); | ||
1349 | |||
1350 | if (!osd) | ||
1351 | return; | ||
1352 | osdc = osd->o_osdc; | ||
1353 | |||
1354 | switch (type) { | ||
1355 | case CEPH_MSG_OSD_MAP: | ||
1356 | ceph_osdc_handle_map(osdc, msg); | ||
1357 | break; | ||
1358 | case CEPH_MSG_OSD_OPREPLY: | ||
1359 | handle_reply(osdc, msg, con); | ||
1360 | break; | ||
1361 | |||
1362 | default: | ||
1363 | pr_err("received unknown message type %d %s\n", type, | ||
1364 | ceph_msg_type_name(type)); | ||
1365 | } | ||
1366 | ceph_msg_put(msg); | ||
1367 | } | ||
1368 | |||
1369 | /* | ||
1370 | * lookup and return message for incoming reply | ||
1371 | */ | ||
1372 | static struct ceph_msg *get_reply(struct ceph_connection *con, | ||
1373 | struct ceph_msg_header *hdr, | ||
1374 | int *skip) | ||
1375 | { | ||
1376 | struct ceph_osd *osd = con->private; | ||
1377 | struct ceph_osd_client *osdc = osd->o_osdc; | ||
1378 | struct ceph_msg *m; | ||
1379 | struct ceph_osd_request *req; | ||
1380 | int front = le32_to_cpu(hdr->front_len); | ||
1381 | int data_len = le32_to_cpu(hdr->data_len); | ||
1382 | u64 tid; | ||
1383 | int err; | ||
1384 | |||
1385 | tid = le64_to_cpu(hdr->tid); | ||
1386 | mutex_lock(&osdc->request_mutex); | ||
1387 | req = __lookup_request(osdc, tid); | ||
1388 | if (!req) { | ||
1389 | *skip = 1; | ||
1390 | m = NULL; | ||
1391 | pr_info("get_reply unknown tid %llu from osd%d\n", tid, | ||
1392 | osd->o_osd); | ||
1393 | goto out; | ||
1394 | } | ||
1395 | |||
1396 | if (req->r_con_filling_msg) { | ||
1397 | dout("get_reply revoking msg %p from old con %p\n", | ||
1398 | req->r_reply, req->r_con_filling_msg); | ||
1399 | ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply); | ||
1400 | ceph_con_put(req->r_con_filling_msg); | ||
1401 | } | ||
1402 | |||
1403 | if (front > req->r_reply->front.iov_len) { | ||
1404 | pr_warning("get_reply front %d > preallocated %d\n", | ||
1405 | front, (int)req->r_reply->front.iov_len); | ||
1406 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL); | ||
1407 | if (IS_ERR(m)) | ||
1408 | goto out; | ||
1409 | ceph_msg_put(req->r_reply); | ||
1410 | req->r_reply = m; | ||
1411 | } | ||
1412 | m = ceph_msg_get(req->r_reply); | ||
1413 | |||
1414 | if (data_len > 0) { | ||
1415 | err = __prepare_pages(con, hdr, req, tid, m); | ||
1416 | if (err < 0) { | ||
1417 | *skip = 1; | ||
1418 | ceph_msg_put(m); | ||
1419 | m = ERR_PTR(err); | ||
1420 | } | ||
1421 | } | ||
1422 | *skip = 0; | ||
1423 | req->r_con_filling_msg = ceph_con_get(con); | ||
1424 | dout("get_reply tid %lld %p\n", tid, m); | ||
1425 | |||
1426 | out: | ||
1427 | mutex_unlock(&osdc->request_mutex); | ||
1428 | return m; | ||
1429 | |||
1430 | } | ||
1431 | |||
1432 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | ||
1433 | struct ceph_msg_header *hdr, | ||
1434 | int *skip) | ||
1435 | { | ||
1436 | struct ceph_osd *osd = con->private; | ||
1437 | int type = le16_to_cpu(hdr->type); | ||
1438 | int front = le32_to_cpu(hdr->front_len); | ||
1439 | |||
1440 | switch (type) { | ||
1441 | case CEPH_MSG_OSD_MAP: | ||
1442 | return ceph_msg_new(type, front, 0, 0, NULL); | ||
1443 | case CEPH_MSG_OSD_OPREPLY: | ||
1444 | return get_reply(con, hdr, skip); | ||
1445 | default: | ||
1446 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | ||
1447 | osd->o_osd); | ||
1448 | *skip = 1; | ||
1449 | return NULL; | ||
1450 | } | ||
1451 | } | ||
1452 | |||
1453 | /* | ||
1454 | * Wrappers to refcount containing ceph_osd struct | ||
1455 | */ | ||
1456 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | ||
1457 | { | ||
1458 | struct ceph_osd *osd = con->private; | ||
1459 | if (get_osd(osd)) | ||
1460 | return con; | ||
1461 | return NULL; | ||
1462 | } | ||
1463 | |||
1464 | static void put_osd_con(struct ceph_connection *con) | ||
1465 | { | ||
1466 | struct ceph_osd *osd = con->private; | ||
1467 | put_osd(osd); | ||
1468 | } | ||
1469 | |||
1470 | /* | ||
1471 | * authentication | ||
1472 | */ | ||
1473 | static int get_authorizer(struct ceph_connection *con, | ||
1474 | void **buf, int *len, int *proto, | ||
1475 | void **reply_buf, int *reply_len, int force_new) | ||
1476 | { | ||
1477 | struct ceph_osd *o = con->private; | ||
1478 | struct ceph_osd_client *osdc = o->o_osdc; | ||
1479 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
1480 | int ret = 0; | ||
1481 | |||
1482 | if (force_new && o->o_authorizer) { | ||
1483 | ac->ops->destroy_authorizer(ac, o->o_authorizer); | ||
1484 | o->o_authorizer = NULL; | ||
1485 | } | ||
1486 | if (o->o_authorizer == NULL) { | ||
1487 | ret = ac->ops->create_authorizer( | ||
1488 | ac, CEPH_ENTITY_TYPE_OSD, | ||
1489 | &o->o_authorizer, | ||
1490 | &o->o_authorizer_buf, | ||
1491 | &o->o_authorizer_buf_len, | ||
1492 | &o->o_authorizer_reply_buf, | ||
1493 | &o->o_authorizer_reply_buf_len); | ||
1494 | if (ret) | ||
1495 | return ret; | ||
1496 | } | ||
1497 | |||
1498 | *proto = ac->protocol; | ||
1499 | *buf = o->o_authorizer_buf; | ||
1500 | *len = o->o_authorizer_buf_len; | ||
1501 | *reply_buf = o->o_authorizer_reply_buf; | ||
1502 | *reply_len = o->o_authorizer_reply_buf_len; | ||
1503 | return 0; | ||
1504 | } | ||
1505 | |||
1506 | |||
1507 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | ||
1508 | { | ||
1509 | struct ceph_osd *o = con->private; | ||
1510 | struct ceph_osd_client *osdc = o->o_osdc; | ||
1511 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
1512 | |||
1513 | return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len); | ||
1514 | } | ||
1515 | |||
1516 | static int invalidate_authorizer(struct ceph_connection *con) | ||
1517 | { | ||
1518 | struct ceph_osd *o = con->private; | ||
1519 | struct ceph_osd_client *osdc = o->o_osdc; | ||
1520 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
1521 | |||
1522 | if (ac->ops->invalidate_authorizer) | ||
1523 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); | ||
1524 | |||
1525 | return ceph_monc_validate_auth(&osdc->client->monc); | ||
1526 | } | ||
1527 | |||
1528 | const static struct ceph_connection_operations osd_con_ops = { | ||
1529 | .get = get_osd_con, | ||
1530 | .put = put_osd_con, | ||
1531 | .dispatch = dispatch, | ||
1532 | .get_authorizer = get_authorizer, | ||
1533 | .verify_authorizer_reply = verify_authorizer_reply, | ||
1534 | .invalidate_authorizer = invalidate_authorizer, | ||
1535 | .alloc_msg = alloc_msg, | ||
1536 | .fault = osd_reset, | ||
1537 | }; | ||