libceph: check for invalid mapping
[linux-2.6-block.git] / net / ceph / osd_client.c
CommitLineData
3d14c5d2 1#include <linux/ceph/ceph_debug.h>
f24e9980 2
3d14c5d2 3#include <linux/module.h>
f24e9980
SW
4#include <linux/err.h>
5#include <linux/highmem.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/slab.h>
9#include <linux/uaccess.h>
68b4476b
YS
10#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
f24e9980 13
3d14c5d2
YS
14#include <linux/ceph/libceph.h>
15#include <linux/ceph/osd_client.h>
16#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/auth.h>
19#include <linux/ceph/pagelist.h>
f24e9980 20
c16e7869
SW
21#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
0d59ab81 23
9e32789f 24static const struct ceph_connection_operations osd_con_ops;
f24e9980 25
6f6c7006
SW
26static void send_queued(struct ceph_osd_client *osdc);
27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
a40c4f10
YS
28static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
56e925b6
SW
32static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
f24e9980 34
68b4476b
YS
35static int op_needs_trail(int op)
36{
37 switch (op) {
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
a40c4f10 42 case CEPH_OSD_OP_NOTIFY:
68b4476b
YS
43 return 1;
44 default:
45 return 0;
46 }
47}
48
49static int op_has_extent(int op)
50{
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
53}
54
d63b77f4 55int ceph_calc_raw_layout(struct ceph_osd_client *osdc,
3499e8a5
YS
56 struct ceph_file_layout *layout,
57 u64 snapid,
68b4476b
YS
58 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
3499e8a5
YS
61{
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
68b4476b 63 u64 orig_len = *plen;
3499e8a5 64 u64 objoff, objlen; /* extent in object */
d63b77f4 65 int r;
3499e8a5
YS
66
67 reqhead->snapid = cpu_to_le64(snapid);
68
69 /* object extent? */
d63b77f4
SW
70 r = ceph_calc_file_object_mapping(layout, off, plen, bno,
71 &objoff, &objlen);
72 if (r < 0)
73 return r;
68b4476b 74 if (*plen < orig_len)
3499e8a5 75 dout(" skipping last %llu, final file extent %llu~%llu\n",
68b4476b 76 orig_len - *plen, off, *plen);
3499e8a5 77
68b4476b
YS
78 if (op_has_extent(op->op)) {
79 op->extent.offset = objoff;
80 op->extent.length = objlen;
81 }
82 req->r_num_pages = calc_pages_for(off, *plen);
b7495fc2 83 req->r_page_alignment = off & ~PAGE_MASK;
3d14c5d2
YS
84 if (op->op == CEPH_OSD_OP_WRITE)
85 op->payload_len = *plen;
3499e8a5
YS
86
87 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
88 *bno, objoff, objlen, req->r_num_pages);
d63b77f4 89 return 0;
3499e8a5 90}
3d14c5d2 91EXPORT_SYMBOL(ceph_calc_raw_layout);
3499e8a5 92
f24e9980
SW
93/*
94 * Implement client access to distributed object storage cluster.
95 *
96 * All data objects are stored within a cluster/cloud of OSDs, or
97 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
98 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
99 * remote daemons serving up and coordinating consistent and safe
100 * access to storage.
101 *
102 * Cluster membership and the mapping of data objects onto storage devices
103 * are described by the osd map.
104 *
105 * We keep track of pending OSD requests (read, write), resubmit
106 * requests to different OSDs when the cluster topology/data layout
107 * change, or retry the affected requests when the communications
108 * channel with an OSD is reset.
109 */
110
111/*
112 * calculate the mapping of a file extent onto an object, and fill out the
113 * request accordingly. shorten extent as necessary if it crosses an
114 * object boundary.
115 *
116 * fill osd op in request message.
117 */
d63b77f4
SW
118static int calc_layout(struct ceph_osd_client *osdc,
119 struct ceph_vino vino,
120 struct ceph_file_layout *layout,
121 u64 off, u64 *plen,
122 struct ceph_osd_request *req,
123 struct ceph_osd_req_op *op)
f24e9980 124{
f24e9980 125 u64 bno;
d63b77f4 126 int r;
f24e9980 127
d63b77f4
SW
128 r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
129 plen, &bno, req, op);
130 if (r < 0)
131 return r;
f24e9980 132
2dab036b 133 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
f24e9980 134 req->r_oid_len = strlen(req->r_oid);
d63b77f4
SW
135
136 return r;
f24e9980
SW
137}
138
f24e9980
SW
139/*
140 * requests
141 */
415e49a9 142void ceph_osdc_release_request(struct kref *kref)
f24e9980 143{
415e49a9
SW
144 struct ceph_osd_request *req = container_of(kref,
145 struct ceph_osd_request,
146 r_kref);
147
148 if (req->r_request)
149 ceph_msg_put(req->r_request);
0d59ab81 150 if (req->r_con_filling_msg) {
8921d114 151 dout("%s revoking pages %p from con %p\n", __func__,
0d59ab81 152 req->r_pages, req->r_con_filling_msg);
8921d114 153 ceph_msg_revoke_incoming(req->r_reply);
0d47766f 154 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
350b1c32 155 }
ab8cb34a
AE
156 if (req->r_reply)
157 ceph_msg_put(req->r_reply);
415e49a9
SW
158 if (req->r_own_pages)
159 ceph_release_page_vector(req->r_pages,
160 req->r_num_pages);
68b4476b
YS
161#ifdef CONFIG_BLOCK
162 if (req->r_bio)
163 bio_put(req->r_bio);
164#endif
415e49a9 165 ceph_put_snap_context(req->r_snapc);
68b4476b
YS
166 if (req->r_trail) {
167 ceph_pagelist_release(req->r_trail);
168 kfree(req->r_trail);
169 }
415e49a9
SW
170 if (req->r_mempool)
171 mempool_free(req, req->r_osdc->req_mempool);
172 else
173 kfree(req);
f24e9980 174}
3d14c5d2 175EXPORT_SYMBOL(ceph_osdc_release_request);
68b4476b
YS
176
177static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
178{
179 int i = 0;
180
181 if (needs_trail)
182 *needs_trail = 0;
183 while (ops[i].op) {
184 if (needs_trail && op_needs_trail(ops[i].op))
185 *needs_trail = 1;
186 i++;
187 }
188
189 return i;
190}
191
3499e8a5
YS
192struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
193 int flags,
f24e9980 194 struct ceph_snap_context *snapc,
68b4476b 195 struct ceph_osd_req_op *ops,
3499e8a5
YS
196 bool use_mempool,
197 gfp_t gfp_flags,
68b4476b
YS
198 struct page **pages,
199 struct bio *bio)
f24e9980
SW
200{
201 struct ceph_osd_request *req;
202 struct ceph_msg *msg;
68b4476b
YS
203 int needs_trail;
204 int num_op = get_num_ops(ops, &needs_trail);
205 size_t msg_size = sizeof(struct ceph_osd_request_head);
3499e8a5 206
68b4476b 207 msg_size += num_op*sizeof(struct ceph_osd_op);
f24e9980
SW
208
209 if (use_mempool) {
3499e8a5 210 req = mempool_alloc(osdc->req_mempool, gfp_flags);
f24e9980
SW
211 memset(req, 0, sizeof(*req));
212 } else {
3499e8a5 213 req = kzalloc(sizeof(*req), gfp_flags);
f24e9980
SW
214 }
215 if (req == NULL)
a79832f2 216 return NULL;
f24e9980 217
f24e9980
SW
218 req->r_osdc = osdc;
219 req->r_mempool = use_mempool;
68b4476b 220
415e49a9 221 kref_init(&req->r_kref);
f24e9980
SW
222 init_completion(&req->r_completion);
223 init_completion(&req->r_safe_completion);
cd43045c 224 rb_init_node(&req->r_node);
f24e9980 225 INIT_LIST_HEAD(&req->r_unsafe_item);
a40c4f10
YS
226 INIT_LIST_HEAD(&req->r_linger_item);
227 INIT_LIST_HEAD(&req->r_linger_osd);
935b639a 228 INIT_LIST_HEAD(&req->r_req_lru_item);
cd43045c
SW
229 INIT_LIST_HEAD(&req->r_osd_item);
230
f24e9980
SW
231 req->r_flags = flags;
232
233 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
234
c16e7869
SW
235 /* create reply message */
236 if (use_mempool)
237 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
238 else
239 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
b61c2763 240 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
a79832f2 241 if (!msg) {
c16e7869 242 ceph_osdc_put_request(req);
a79832f2 243 return NULL;
c16e7869
SW
244 }
245 req->r_reply = msg;
246
68b4476b
YS
247 /* allocate space for the trailing data */
248 if (needs_trail) {
249 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
250 if (!req->r_trail) {
251 ceph_osdc_put_request(req);
252 return NULL;
253 }
254 ceph_pagelist_init(req->r_trail);
255 }
d50b409f 256
c16e7869 257 /* create request message; allow space for oid */
224736d9 258 msg_size += MAX_OBJ_NAME_SIZE;
f24e9980
SW
259 if (snapc)
260 msg_size += sizeof(u64) * snapc->num_snaps;
261 if (use_mempool)
8f3bc053 262 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
f24e9980 263 else
b61c2763 264 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
a79832f2 265 if (!msg) {
f24e9980 266 ceph_osdc_put_request(req);
a79832f2 267 return NULL;
f24e9980 268 }
68b4476b 269
f24e9980 270 memset(msg->front.iov_base, 0, msg->front.iov_len);
3499e8a5
YS
271
272 req->r_request = msg;
273 req->r_pages = pages;
68b4476b
YS
274#ifdef CONFIG_BLOCK
275 if (bio) {
276 req->r_bio = bio;
277 bio_get(req->r_bio);
278 }
279#endif
3499e8a5
YS
280
281 return req;
282}
3d14c5d2 283EXPORT_SYMBOL(ceph_osdc_alloc_request);
3499e8a5 284
68b4476b
YS
285static void osd_req_encode_op(struct ceph_osd_request *req,
286 struct ceph_osd_op *dst,
287 struct ceph_osd_req_op *src)
288{
289 dst->op = cpu_to_le16(src->op);
290
065a68f9 291 switch (src->op) {
68b4476b
YS
292 case CEPH_OSD_OP_READ:
293 case CEPH_OSD_OP_WRITE:
294 dst->extent.offset =
295 cpu_to_le64(src->extent.offset);
296 dst->extent.length =
297 cpu_to_le64(src->extent.length);
298 dst->extent.truncate_size =
299 cpu_to_le64(src->extent.truncate_size);
300 dst->extent.truncate_seq =
301 cpu_to_le32(src->extent.truncate_seq);
302 break;
303
304 case CEPH_OSD_OP_GETXATTR:
305 case CEPH_OSD_OP_SETXATTR:
306 case CEPH_OSD_OP_CMPXATTR:
307 BUG_ON(!req->r_trail);
308
309 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
310 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
311 dst->xattr.cmp_op = src->xattr.cmp_op;
312 dst->xattr.cmp_mode = src->xattr.cmp_mode;
313 ceph_pagelist_append(req->r_trail, src->xattr.name,
314 src->xattr.name_len);
315 ceph_pagelist_append(req->r_trail, src->xattr.val,
316 src->xattr.value_len);
317 break;
ae1533b6
YS
318 case CEPH_OSD_OP_CALL:
319 BUG_ON(!req->r_trail);
320
321 dst->cls.class_len = src->cls.class_len;
322 dst->cls.method_len = src->cls.method_len;
323 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
324
325 ceph_pagelist_append(req->r_trail, src->cls.class_name,
326 src->cls.class_len);
327 ceph_pagelist_append(req->r_trail, src->cls.method_name,
328 src->cls.method_len);
329 ceph_pagelist_append(req->r_trail, src->cls.indata,
330 src->cls.indata_len);
331 break;
332 case CEPH_OSD_OP_ROLLBACK:
333 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
334 break;
68b4476b
YS
335 case CEPH_OSD_OP_STARTSYNC:
336 break;
a40c4f10
YS
337 case CEPH_OSD_OP_NOTIFY:
338 {
339 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
340 __le32 timeout = cpu_to_le32(src->watch.timeout);
341
342 BUG_ON(!req->r_trail);
343
344 ceph_pagelist_append(req->r_trail,
345 &prot_ver, sizeof(prot_ver));
346 ceph_pagelist_append(req->r_trail,
347 &timeout, sizeof(timeout));
348 }
349 case CEPH_OSD_OP_NOTIFY_ACK:
350 case CEPH_OSD_OP_WATCH:
351 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
352 dst->watch.ver = cpu_to_le64(src->watch.ver);
353 dst->watch.flag = src->watch.flag;
354 break;
68b4476b
YS
355 default:
356 pr_err("unrecognized osd opcode %d\n", dst->op);
357 WARN_ON(1);
358 break;
359 }
360 dst->payload_len = cpu_to_le32(src->payload_len);
361}
362
3499e8a5
YS
363/*
364 * build new request AND message
365 *
366 */
367void ceph_osdc_build_request(struct ceph_osd_request *req,
68b4476b
YS
368 u64 off, u64 *plen,
369 struct ceph_osd_req_op *src_ops,
370 struct ceph_snap_context *snapc,
371 struct timespec *mtime,
372 const char *oid,
373 int oid_len)
3499e8a5
YS
374{
375 struct ceph_msg *msg = req->r_request;
376 struct ceph_osd_request_head *head;
68b4476b 377 struct ceph_osd_req_op *src_op;
3499e8a5
YS
378 struct ceph_osd_op *op;
379 void *p;
68b4476b 380 int num_op = get_num_ops(src_ops, NULL);
3499e8a5 381 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
3499e8a5 382 int flags = req->r_flags;
68b4476b
YS
383 u64 data_len = 0;
384 int i;
3499e8a5 385
f24e9980
SW
386 head = msg->front.iov_base;
387 op = (void *)(head + 1);
388 p = (void *)(op + num_op);
389
f24e9980
SW
390 req->r_snapc = ceph_get_snap_context(snapc);
391
392 head->client_inc = cpu_to_le32(1); /* always, for now. */
393 head->flags = cpu_to_le32(flags);
394 if (flags & CEPH_OSD_FLAG_WRITE)
395 ceph_encode_timespec(&head->mtime, mtime);
396 head->num_ops = cpu_to_le16(num_op);
f24e9980 397
f24e9980
SW
398
399 /* fill in oid */
3499e8a5
YS
400 head->object_len = cpu_to_le32(oid_len);
401 memcpy(p, oid, oid_len);
402 p += oid_len;
f24e9980 403
68b4476b
YS
404 src_op = src_ops;
405 while (src_op->op) {
406 osd_req_encode_op(req, op, src_op);
407 src_op++;
f24e9980 408 op++;
f24e9980 409 }
68b4476b
YS
410
411 if (req->r_trail)
412 data_len += req->r_trail->length;
413
f24e9980
SW
414 if (snapc) {
415 head->snap_seq = cpu_to_le64(snapc->seq);
416 head->num_snaps = cpu_to_le32(snapc->num_snaps);
417 for (i = 0; i < snapc->num_snaps; i++) {
418 put_unaligned_le64(snapc->snaps[i], p);
419 p += sizeof(u64);
420 }
421 }
422
68b4476b
YS
423 if (flags & CEPH_OSD_FLAG_WRITE) {
424 req->r_request->hdr.data_off = cpu_to_le16(off);
425 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
426 } else if (data_len) {
427 req->r_request->hdr.data_off = 0;
428 req->r_request->hdr.data_len = cpu_to_le32(data_len);
429 }
430
c5c6b19d
SW
431 req->r_request->page_alignment = req->r_page_alignment;
432
f24e9980 433 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
6f863e71
SW
434 msg_size = p - msg->front.iov_base;
435 msg->front.iov_len = msg_size;
436 msg->hdr.front_len = cpu_to_le32(msg_size);
3499e8a5
YS
437 return;
438}
3d14c5d2 439EXPORT_SYMBOL(ceph_osdc_build_request);
3499e8a5
YS
440
441/*
442 * build new request AND message, calculate layout, and adjust file
443 * extent as needed.
444 *
445 * if the file was recently truncated, we include information about its
446 * old and new size so that the object can be updated appropriately. (we
447 * avoid synchronously deleting truncated objects because it's slow.)
448 *
449 * if @do_sync, include a 'startsync' command so that the osd will flush
450 * data quickly.
451 */
452struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
453 struct ceph_file_layout *layout,
454 struct ceph_vino vino,
455 u64 off, u64 *plen,
456 int opcode, int flags,
457 struct ceph_snap_context *snapc,
458 int do_sync,
459 u32 truncate_seq,
460 u64 truncate_size,
461 struct timespec *mtime,
b7495fc2
SW
462 bool use_mempool, int num_reply,
463 int page_align)
3499e8a5 464{
68b4476b
YS
465 struct ceph_osd_req_op ops[3];
466 struct ceph_osd_request *req;
467
468 ops[0].op = opcode;
469 ops[0].extent.truncate_seq = truncate_seq;
470 ops[0].extent.truncate_size = truncate_size;
471 ops[0].payload_len = 0;
472
473 if (do_sync) {
474 ops[1].op = CEPH_OSD_OP_STARTSYNC;
475 ops[1].payload_len = 0;
476 ops[2].op = 0;
477 } else
478 ops[1].op = 0;
479
480 req = ceph_osdc_alloc_request(osdc, flags,
481 snapc, ops,
3499e8a5 482 use_mempool,
68b4476b 483 GFP_NOFS, NULL, NULL);
4ad12621
SW
484 if (!req)
485 return NULL;
3499e8a5
YS
486
487 /* calculate max write size */
68b4476b 488 calc_layout(osdc, vino, layout, off, plen, req, ops);
3499e8a5
YS
489 req->r_file_layout = *layout; /* keep a copy */
490
9bb0ce2b
SW
491 /* in case it differs from natural (file) alignment that
492 calc_layout filled in for us */
493 req->r_num_pages = calc_pages_for(page_align, *plen);
b7495fc2
SW
494 req->r_page_alignment = page_align;
495
68b4476b
YS
496 ceph_osdc_build_request(req, off, plen, ops,
497 snapc,
3499e8a5
YS
498 mtime,
499 req->r_oid, req->r_oid_len);
500
f24e9980
SW
501 return req;
502}
3d14c5d2 503EXPORT_SYMBOL(ceph_osdc_new_request);
f24e9980
SW
504
505/*
506 * We keep osd requests in an rbtree, sorted by ->r_tid.
507 */
508static void __insert_request(struct ceph_osd_client *osdc,
509 struct ceph_osd_request *new)
510{
511 struct rb_node **p = &osdc->requests.rb_node;
512 struct rb_node *parent = NULL;
513 struct ceph_osd_request *req = NULL;
514
515 while (*p) {
516 parent = *p;
517 req = rb_entry(parent, struct ceph_osd_request, r_node);
518 if (new->r_tid < req->r_tid)
519 p = &(*p)->rb_left;
520 else if (new->r_tid > req->r_tid)
521 p = &(*p)->rb_right;
522 else
523 BUG();
524 }
525
526 rb_link_node(&new->r_node, parent, p);
527 rb_insert_color(&new->r_node, &osdc->requests);
528}
529
530static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
531 u64 tid)
532{
533 struct ceph_osd_request *req;
534 struct rb_node *n = osdc->requests.rb_node;
535
536 while (n) {
537 req = rb_entry(n, struct ceph_osd_request, r_node);
538 if (tid < req->r_tid)
539 n = n->rb_left;
540 else if (tid > req->r_tid)
541 n = n->rb_right;
542 else
543 return req;
544 }
545 return NULL;
546}
547
548static struct ceph_osd_request *
549__lookup_request_ge(struct ceph_osd_client *osdc,
550 u64 tid)
551{
552 struct ceph_osd_request *req;
553 struct rb_node *n = osdc->requests.rb_node;
554
555 while (n) {
556 req = rb_entry(n, struct ceph_osd_request, r_node);
557 if (tid < req->r_tid) {
558 if (!n->rb_left)
559 return req;
560 n = n->rb_left;
561 } else if (tid > req->r_tid) {
562 n = n->rb_right;
563 } else {
564 return req;
565 }
566 }
567 return NULL;
568}
569
6f6c7006
SW
570/*
571 * Resubmit requests pending on the given osd.
572 */
573static void __kick_osd_requests(struct ceph_osd_client *osdc,
574 struct ceph_osd *osd)
575{
a40c4f10 576 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
577 int err;
578
579 dout("__kick_osd_requests osd%d\n", osd->o_osd);
580 err = __reset_osd(osdc, osd);
581 if (err == -EAGAIN)
582 return;
583
584 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
585 list_move(&req->r_req_lru_item, &osdc->req_unsent);
586 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
587 osd->o_osd);
a40c4f10
YS
588 if (!req->r_linger)
589 req->r_flags |= CEPH_OSD_FLAG_RETRY;
590 }
591
592 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
593 r_linger_osd) {
77f38e0e
SW
594 /*
595 * reregister request prior to unregistering linger so
596 * that r_osd is preserved.
597 */
598 BUG_ON(!list_empty(&req->r_req_lru_item));
a40c4f10 599 __register_request(osdc, req);
77f38e0e
SW
600 list_add(&req->r_req_lru_item, &osdc->req_unsent);
601 list_add(&req->r_osd_item, &req->r_osd->o_requests);
602 __unregister_linger_request(osdc, req);
a40c4f10
YS
603 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
604 osd->o_osd);
6f6c7006
SW
605 }
606}
607
608static void kick_osd_requests(struct ceph_osd_client *osdc,
609 struct ceph_osd *kickosd)
610{
611 mutex_lock(&osdc->request_mutex);
612 __kick_osd_requests(osdc, kickosd);
613 mutex_unlock(&osdc->request_mutex);
614}
f24e9980
SW
615
616/*
81b024e7 617 * If the osd connection drops, we need to resubmit all requests.
f24e9980
SW
618 */
619static void osd_reset(struct ceph_connection *con)
620{
621 struct ceph_osd *osd = con->private;
622 struct ceph_osd_client *osdc;
623
624 if (!osd)
625 return;
626 dout("osd_reset osd%d\n", osd->o_osd);
627 osdc = osd->o_osdc;
f24e9980 628 down_read(&osdc->map_sem);
6f6c7006
SW
629 kick_osd_requests(osdc, osd);
630 send_queued(osdc);
f24e9980
SW
631 up_read(&osdc->map_sem);
632}
633
634/*
635 * Track open sessions with osds.
636 */
e10006f8 637static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
f24e9980
SW
638{
639 struct ceph_osd *osd;
640
641 osd = kzalloc(sizeof(*osd), GFP_NOFS);
642 if (!osd)
643 return NULL;
644
645 atomic_set(&osd->o_ref, 1);
646 osd->o_osdc = osdc;
e10006f8 647 osd->o_osd = onum;
f24e9980 648 INIT_LIST_HEAD(&osd->o_requests);
a40c4f10 649 INIT_LIST_HEAD(&osd->o_linger_requests);
f5a2041b 650 INIT_LIST_HEAD(&osd->o_osd_lru);
f24e9980
SW
651 osd->o_incarnation = 1;
652
b7a9e5dd 653 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
4e7a5dcd 654
422d2cb8 655 INIT_LIST_HEAD(&osd->o_keepalive_item);
f24e9980
SW
656 return osd;
657}
658
659static struct ceph_osd *get_osd(struct ceph_osd *osd)
660{
661 if (atomic_inc_not_zero(&osd->o_ref)) {
662 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
663 atomic_read(&osd->o_ref));
664 return osd;
665 } else {
666 dout("get_osd %p FAIL\n", osd);
667 return NULL;
668 }
669}
670
671static void put_osd(struct ceph_osd *osd)
672{
673 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
674 atomic_read(&osd->o_ref) - 1);
a255651d 675 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
79494d1b
SW
676 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
677
a255651d 678 if (ac->ops && ac->ops->destroy_authorizer)
6c4a1915 679 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
f24e9980 680 kfree(osd);
79494d1b 681 }
f24e9980
SW
682}
683
684/*
685 * remove an osd from our map
686 */
f5a2041b 687static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 688{
f5a2041b 689 dout("__remove_osd %p\n", osd);
f24e9980
SW
690 BUG_ON(!list_empty(&osd->o_requests));
691 rb_erase(&osd->o_node, &osdc->osds);
f5a2041b 692 list_del_init(&osd->o_osd_lru);
f24e9980
SW
693 ceph_con_close(&osd->o_con);
694 put_osd(osd);
695}
696
aca420bc
SW
697static void remove_all_osds(struct ceph_osd_client *osdc)
698{
048a9d2d 699 dout("%s %p\n", __func__, osdc);
aca420bc
SW
700 mutex_lock(&osdc->request_mutex);
701 while (!RB_EMPTY_ROOT(&osdc->osds)) {
702 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
703 struct ceph_osd, o_node);
704 __remove_osd(osdc, osd);
705 }
706 mutex_unlock(&osdc->request_mutex);
707}
708
f5a2041b
YS
709static void __move_osd_to_lru(struct ceph_osd_client *osdc,
710 struct ceph_osd *osd)
711{
712 dout("__move_osd_to_lru %p\n", osd);
713 BUG_ON(!list_empty(&osd->o_osd_lru));
714 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
3d14c5d2 715 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
f5a2041b
YS
716}
717
718static void __remove_osd_from_lru(struct ceph_osd *osd)
719{
720 dout("__remove_osd_from_lru %p\n", osd);
721 if (!list_empty(&osd->o_osd_lru))
722 list_del_init(&osd->o_osd_lru);
723}
724
aca420bc 725static void remove_old_osds(struct ceph_osd_client *osdc)
f5a2041b
YS
726{
727 struct ceph_osd *osd, *nosd;
728
729 dout("__remove_old_osds %p\n", osdc);
730 mutex_lock(&osdc->request_mutex);
731 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
aca420bc 732 if (time_before(jiffies, osd->lru_ttl))
f5a2041b
YS
733 break;
734 __remove_osd(osdc, osd);
735 }
736 mutex_unlock(&osdc->request_mutex);
737}
738
f24e9980
SW
739/*
740 * reset osd connect
741 */
f5a2041b 742static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
f24e9980 743{
87b315a5 744 struct ceph_osd_request *req;
f24e9980
SW
745 int ret = 0;
746
f5a2041b 747 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
a40c4f10
YS
748 if (list_empty(&osd->o_requests) &&
749 list_empty(&osd->o_linger_requests)) {
f5a2041b 750 __remove_osd(osdc, osd);
87b315a5
SW
751 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
752 &osd->o_con.peer_addr,
753 sizeof(osd->o_con.peer_addr)) == 0 &&
754 !ceph_con_opened(&osd->o_con)) {
755 dout(" osd addr hasn't changed and connection never opened,"
756 " letting msgr retry");
757 /* touch each r_stamp for handle_timeout()'s benfit */
758 list_for_each_entry(req, &osd->o_requests, r_osd_item)
759 req->r_stamp = jiffies;
760 ret = -EAGAIN;
f24e9980
SW
761 } else {
762 ceph_con_close(&osd->o_con);
b7a9e5dd
SW
763 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
764 &osdc->osdmap->osd_addr[osd->o_osd]);
f24e9980
SW
765 osd->o_incarnation++;
766 }
767 return ret;
768}
769
770static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
771{
772 struct rb_node **p = &osdc->osds.rb_node;
773 struct rb_node *parent = NULL;
774 struct ceph_osd *osd = NULL;
775
aca420bc 776 dout("__insert_osd %p osd%d\n", new, new->o_osd);
f24e9980
SW
777 while (*p) {
778 parent = *p;
779 osd = rb_entry(parent, struct ceph_osd, o_node);
780 if (new->o_osd < osd->o_osd)
781 p = &(*p)->rb_left;
782 else if (new->o_osd > osd->o_osd)
783 p = &(*p)->rb_right;
784 else
785 BUG();
786 }
787
788 rb_link_node(&new->o_node, parent, p);
789 rb_insert_color(&new->o_node, &osdc->osds);
790}
791
792static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
793{
794 struct ceph_osd *osd;
795 struct rb_node *n = osdc->osds.rb_node;
796
797 while (n) {
798 osd = rb_entry(n, struct ceph_osd, o_node);
799 if (o < osd->o_osd)
800 n = n->rb_left;
801 else if (o > osd->o_osd)
802 n = n->rb_right;
803 else
804 return osd;
805 }
806 return NULL;
807}
808
422d2cb8
YS
809static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
810{
811 schedule_delayed_work(&osdc->timeout_work,
3d14c5d2 812 osdc->client->options->osd_keepalive_timeout * HZ);
422d2cb8
YS
813}
814
815static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
816{
817 cancel_delayed_work(&osdc->timeout_work);
818}
f24e9980
SW
819
820/*
821 * Register request, assign tid. If this is the first request, set up
822 * the timeout event.
823 */
a40c4f10
YS
824static void __register_request(struct ceph_osd_client *osdc,
825 struct ceph_osd_request *req)
f24e9980 826{
f24e9980 827 req->r_tid = ++osdc->last_tid;
6df058c0 828 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
77f38e0e 829 dout("__register_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
830 __insert_request(osdc, req);
831 ceph_osdc_get_request(req);
832 osdc->num_requests++;
f24e9980 833 if (osdc->num_requests == 1) {
422d2cb8
YS
834 dout(" first request, scheduling timeout\n");
835 __schedule_osd_timeout(osdc);
f24e9980 836 }
a40c4f10
YS
837}
838
839static void register_request(struct ceph_osd_client *osdc,
840 struct ceph_osd_request *req)
841{
842 mutex_lock(&osdc->request_mutex);
843 __register_request(osdc, req);
f24e9980
SW
844 mutex_unlock(&osdc->request_mutex);
845}
846
847/*
848 * called under osdc->request_mutex
849 */
850static void __unregister_request(struct ceph_osd_client *osdc,
851 struct ceph_osd_request *req)
852{
35f9f8a0
SW
853 if (RB_EMPTY_NODE(&req->r_node)) {
854 dout("__unregister_request %p tid %lld not registered\n",
855 req, req->r_tid);
856 return;
857 }
858
f24e9980
SW
859 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
860 rb_erase(&req->r_node, &osdc->requests);
861 osdc->num_requests--;
862
0ba6478d
SW
863 if (req->r_osd) {
864 /* make sure the original request isn't in flight. */
6740a845 865 ceph_msg_revoke(req->r_request);
0ba6478d
SW
866
867 list_del_init(&req->r_osd_item);
a40c4f10
YS
868 if (list_empty(&req->r_osd->o_requests) &&
869 list_empty(&req->r_osd->o_linger_requests)) {
870 dout("moving osd to %p lru\n", req->r_osd);
f5a2041b 871 __move_osd_to_lru(osdc, req->r_osd);
a40c4f10 872 }
fbdb9190 873 if (list_empty(&req->r_linger_item))
a40c4f10 874 req->r_osd = NULL;
0ba6478d 875 }
f24e9980
SW
876
877 ceph_osdc_put_request(req);
878
422d2cb8
YS
879 list_del_init(&req->r_req_lru_item);
880 if (osdc->num_requests == 0) {
881 dout(" no requests, canceling timeout\n");
882 __cancel_osd_timeout(osdc);
f24e9980
SW
883 }
884}
885
886/*
887 * Cancel a previously queued request message
888 */
889static void __cancel_request(struct ceph_osd_request *req)
890{
6bc18876 891 if (req->r_sent && req->r_osd) {
6740a845 892 ceph_msg_revoke(req->r_request);
f24e9980
SW
893 req->r_sent = 0;
894 }
895}
896
a40c4f10
YS
897static void __register_linger_request(struct ceph_osd_client *osdc,
898 struct ceph_osd_request *req)
899{
900 dout("__register_linger_request %p\n", req);
901 list_add_tail(&req->r_linger_item, &osdc->req_linger);
6194ea89
SW
902 if (req->r_osd)
903 list_add_tail(&req->r_linger_osd,
904 &req->r_osd->o_linger_requests);
a40c4f10
YS
905}
906
907static void __unregister_linger_request(struct ceph_osd_client *osdc,
908 struct ceph_osd_request *req)
909{
910 dout("__unregister_linger_request %p\n", req);
911 if (req->r_osd) {
912 list_del_init(&req->r_linger_item);
913 list_del_init(&req->r_linger_osd);
914
915 if (list_empty(&req->r_osd->o_requests) &&
916 list_empty(&req->r_osd->o_linger_requests)) {
917 dout("moving osd to %p lru\n", req->r_osd);
918 __move_osd_to_lru(osdc, req->r_osd);
919 }
fbdb9190
SW
920 if (list_empty(&req->r_osd_item))
921 req->r_osd = NULL;
a40c4f10
YS
922 }
923}
924
925void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
927{
928 mutex_lock(&osdc->request_mutex);
929 if (req->r_linger) {
930 __unregister_linger_request(osdc, req);
931 ceph_osdc_put_request(req);
932 }
933 mutex_unlock(&osdc->request_mutex);
934}
935EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
936
937void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
938 struct ceph_osd_request *req)
939{
940 if (!req->r_linger) {
941 dout("set_request_linger %p\n", req);
942 req->r_linger = 1;
943 /*
944 * caller is now responsible for calling
945 * unregister_linger_request
946 */
947 ceph_osdc_get_request(req);
948 }
949}
950EXPORT_SYMBOL(ceph_osdc_set_request_linger);
951
f24e9980
SW
952/*
953 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
954 * (as needed), and set the request r_osd appropriately. If there is
25985edc 955 * no up osd, set r_osd to NULL. Move the request to the appropriate list
6f6c7006 956 * (unsent, homeless) or leave on in-flight lru.
f24e9980
SW
957 *
958 * Return 0 if unchanged, 1 if changed, or negative on error.
959 *
960 * Caller should hold map_sem for read and request_mutex.
961 */
6f6c7006 962static int __map_request(struct ceph_osd_client *osdc,
38d6453c 963 struct ceph_osd_request *req, int force_resend)
f24e9980
SW
964{
965 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
51042122 966 struct ceph_pg pgid;
d85b7056
SW
967 int acting[CEPH_PG_MAX_SIZE];
968 int o = -1, num = 0;
f24e9980 969 int err;
f24e9980 970
6f6c7006 971 dout("map_request %p tid %lld\n", req, req->r_tid);
f24e9980
SW
972 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
973 &req->r_file_layout, osdc->osdmap);
6f6c7006
SW
974 if (err) {
975 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f24e9980 976 return err;
6f6c7006 977 }
51042122 978 pgid = reqhead->layout.ol_pgid;
7740a42f
SW
979 req->r_pgid = pgid;
980
d85b7056
SW
981 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
982 if (err > 0) {
983 o = acting[0];
984 num = err;
985 }
f24e9980 986
38d6453c
SW
987 if ((!force_resend &&
988 req->r_osd && req->r_osd->o_osd == o &&
d85b7056
SW
989 req->r_sent >= req->r_osd->o_incarnation &&
990 req->r_num_pg_osds == num &&
991 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
f24e9980
SW
992 (req->r_osd == NULL && o == -1))
993 return 0; /* no change */
994
6f6c7006 995 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
51042122 996 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
f24e9980
SW
997 req->r_osd ? req->r_osd->o_osd : -1);
998
d85b7056
SW
999 /* record full pg acting set */
1000 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1001 req->r_num_pg_osds = num;
1002
f24e9980
SW
1003 if (req->r_osd) {
1004 __cancel_request(req);
1005 list_del_init(&req->r_osd_item);
f24e9980
SW
1006 req->r_osd = NULL;
1007 }
1008
1009 req->r_osd = __lookup_osd(osdc, o);
1010 if (!req->r_osd && o >= 0) {
c99eb1c7 1011 err = -ENOMEM;
e10006f8 1012 req->r_osd = create_osd(osdc, o);
6f6c7006
SW
1013 if (!req->r_osd) {
1014 list_move(&req->r_req_lru_item, &osdc->req_notarget);
c99eb1c7 1015 goto out;
6f6c7006 1016 }
f24e9980 1017
6f6c7006 1018 dout("map_request osd %p is osd%d\n", req->r_osd, o);
f24e9980
SW
1019 __insert_osd(osdc, req->r_osd);
1020
b7a9e5dd
SW
1021 ceph_con_open(&req->r_osd->o_con,
1022 CEPH_ENTITY_TYPE_OSD, o,
1023 &osdc->osdmap->osd_addr[o]);
f24e9980
SW
1024 }
1025
f5a2041b
YS
1026 if (req->r_osd) {
1027 __remove_osd_from_lru(req->r_osd);
f24e9980 1028 list_add(&req->r_osd_item, &req->r_osd->o_requests);
6f6c7006
SW
1029 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1030 } else {
1031 list_move(&req->r_req_lru_item, &osdc->req_notarget);
f5a2041b 1032 }
d85b7056 1033 err = 1; /* osd or pg changed */
f24e9980
SW
1034
1035out:
f24e9980
SW
1036 return err;
1037}
1038
1039/*
1040 * caller should hold map_sem (for read) and request_mutex
1041 */
56e925b6
SW
1042static void __send_request(struct ceph_osd_client *osdc,
1043 struct ceph_osd_request *req)
f24e9980
SW
1044{
1045 struct ceph_osd_request_head *reqhead;
f24e9980
SW
1046
1047 dout("send_request %p tid %llu to osd%d flags %d\n",
1048 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1049
1050 reqhead = req->r_request->front.iov_base;
1051 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1052 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1053 reqhead->reassert_version = req->r_reassert_version;
1054
3dd72fc0 1055 req->r_stamp = jiffies;
07a27e22 1056 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
f24e9980
SW
1057
1058 ceph_msg_get(req->r_request); /* send consumes a ref */
1059 ceph_con_send(&req->r_osd->o_con, req->r_request);
1060 req->r_sent = req->r_osd->o_incarnation;
f24e9980
SW
1061}
1062
6f6c7006
SW
1063/*
1064 * Send any requests in the queue (req_unsent).
1065 */
1066static void send_queued(struct ceph_osd_client *osdc)
1067{
1068 struct ceph_osd_request *req, *tmp;
1069
1070 dout("send_queued\n");
1071 mutex_lock(&osdc->request_mutex);
1072 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1073 __send_request(osdc, req);
1074 }
1075 mutex_unlock(&osdc->request_mutex);
1076}
1077
f24e9980
SW
1078/*
1079 * Timeout callback, called every N seconds when 1 or more osd
1080 * requests has been active for more than N seconds. When this
1081 * happens, we ping all OSDs with requests who have timed out to
1082 * ensure any communications channel reset is detected. Reset the
1083 * request timeouts another N seconds in the future as we go.
1084 * Reschedule the timeout event another N seconds in future (unless
1085 * there are no open requests).
1086 */
1087static void handle_timeout(struct work_struct *work)
1088{
1089 struct ceph_osd_client *osdc =
1090 container_of(work, struct ceph_osd_client, timeout_work.work);
422d2cb8 1091 struct ceph_osd_request *req, *last_req = NULL;
f24e9980 1092 struct ceph_osd *osd;
3d14c5d2 1093 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
422d2cb8 1094 unsigned long keepalive =
3d14c5d2 1095 osdc->client->options->osd_keepalive_timeout * HZ;
3dd72fc0 1096 unsigned long last_stamp = 0;
422d2cb8 1097 struct list_head slow_osds;
f24e9980
SW
1098 dout("timeout\n");
1099 down_read(&osdc->map_sem);
1100
1101 ceph_monc_request_next_osdmap(&osdc->client->monc);
1102
1103 mutex_lock(&osdc->request_mutex);
f24e9980 1104
422d2cb8
YS
1105 /*
1106 * reset osds that appear to be _really_ unresponsive. this
1107 * is a failsafe measure.. we really shouldn't be getting to
1108 * this point if the system is working properly. the monitors
1109 * should mark the osd as failed and we should find out about
1110 * it from an updated osd map.
1111 */
f26e681d 1112 while (timeout && !list_empty(&osdc->req_lru)) {
422d2cb8
YS
1113 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1114 r_req_lru_item);
1115
4cf9d544 1116 /* hasn't been long enough since we sent it? */
3dd72fc0 1117 if (time_before(jiffies, req->r_stamp + timeout))
422d2cb8 1118 break;
4cf9d544
SW
1119
1120 /* hasn't been long enough since it was acked? */
1121 if (req->r_request->ack_stamp == 0 ||
1122 time_before(jiffies, req->r_request->ack_stamp + timeout))
1123 break;
422d2cb8 1124
3dd72fc0 1125 BUG_ON(req == last_req && req->r_stamp == last_stamp);
422d2cb8 1126 last_req = req;
3dd72fc0 1127 last_stamp = req->r_stamp;
422d2cb8
YS
1128
1129 osd = req->r_osd;
1130 BUG_ON(!osd);
1131 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1132 req->r_tid, osd->o_osd);
6f6c7006 1133 __kick_osd_requests(osdc, osd);
422d2cb8
YS
1134 }
1135
1136 /*
1137 * ping osds that are a bit slow. this ensures that if there
1138 * is a break in the TCP connection we will notice, and reopen
1139 * a connection with that osd (from the fault callback).
1140 */
1141 INIT_LIST_HEAD(&slow_osds);
1142 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
3dd72fc0 1143 if (time_before(jiffies, req->r_stamp + keepalive))
422d2cb8
YS
1144 break;
1145
1146 osd = req->r_osd;
1147 BUG_ON(!osd);
1148 dout(" tid %llu is slow, will send keepalive on osd%d\n",
f24e9980 1149 req->r_tid, osd->o_osd);
422d2cb8
YS
1150 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1151 }
1152 while (!list_empty(&slow_osds)) {
1153 osd = list_entry(slow_osds.next, struct ceph_osd,
1154 o_keepalive_item);
1155 list_del_init(&osd->o_keepalive_item);
f24e9980
SW
1156 ceph_con_keepalive(&osd->o_con);
1157 }
1158
422d2cb8 1159 __schedule_osd_timeout(osdc);
f24e9980 1160 mutex_unlock(&osdc->request_mutex);
6f6c7006 1161 send_queued(osdc);
f24e9980
SW
1162 up_read(&osdc->map_sem);
1163}
1164
f5a2041b
YS
1165static void handle_osds_timeout(struct work_struct *work)
1166{
1167 struct ceph_osd_client *osdc =
1168 container_of(work, struct ceph_osd_client,
1169 osds_timeout_work.work);
1170 unsigned long delay =
3d14c5d2 1171 osdc->client->options->osd_idle_ttl * HZ >> 2;
f5a2041b
YS
1172
1173 dout("osds timeout\n");
1174 down_read(&osdc->map_sem);
aca420bc 1175 remove_old_osds(osdc);
f5a2041b
YS
1176 up_read(&osdc->map_sem);
1177
1178 schedule_delayed_work(&osdc->osds_timeout_work,
1179 round_jiffies_relative(delay));
1180}
1181
25845472
SW
1182static void complete_request(struct ceph_osd_request *req)
1183{
1184 if (req->r_safe_callback)
1185 req->r_safe_callback(req, NULL);
1186 complete_all(&req->r_safe_completion); /* fsync waiter */
1187}
1188
f24e9980
SW
1189/*
1190 * handle osd op reply. either call the callback if it is specified,
1191 * or do the completion to wake up the waiting thread.
1192 */
350b1c32
SW
1193static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1194 struct ceph_connection *con)
f24e9980
SW
1195{
1196 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1197 struct ceph_osd_request *req;
1198 u64 tid;
1199 int numops, object_len, flags;
0ceed5db 1200 s32 result;
f24e9980 1201
6df058c0 1202 tid = le64_to_cpu(msg->hdr.tid);
f24e9980
SW
1203 if (msg->front.iov_len < sizeof(*rhead))
1204 goto bad;
f24e9980
SW
1205 numops = le32_to_cpu(rhead->num_ops);
1206 object_len = le32_to_cpu(rhead->object_len);
0ceed5db 1207 result = le32_to_cpu(rhead->result);
f24e9980
SW
1208 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1209 numops * sizeof(struct ceph_osd_op))
1210 goto bad;
0ceed5db 1211 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
f24e9980
SW
1212 /* lookup */
1213 mutex_lock(&osdc->request_mutex);
1214 req = __lookup_request(osdc, tid);
1215 if (req == NULL) {
1216 dout("handle_reply tid %llu dne\n", tid);
1217 mutex_unlock(&osdc->request_mutex);
1218 return;
1219 }
1220 ceph_osdc_get_request(req);
1221 flags = le32_to_cpu(rhead->flags);
1222
350b1c32 1223 /*
0d59ab81 1224 * if this connection filled our message, drop our reference now, to
350b1c32
SW
1225 * avoid a (safe but slower) revoke later.
1226 */
0d59ab81 1227 if (req->r_con_filling_msg == con && req->r_reply == msg) {
c16e7869 1228 dout(" dropping con_filling_msg ref %p\n", con);
0d59ab81 1229 req->r_con_filling_msg = NULL;
0d47766f 1230 con->ops->put(con);
350b1c32
SW
1231 }
1232
f24e9980 1233 if (!req->r_got_reply) {
95c96174 1234 unsigned int bytes;
f24e9980
SW
1235
1236 req->r_result = le32_to_cpu(rhead->result);
1237 bytes = le32_to_cpu(msg->hdr.data_len);
1238 dout("handle_reply result %d bytes %d\n", req->r_result,
1239 bytes);
1240 if (req->r_result == 0)
1241 req->r_result = bytes;
1242
1243 /* in case this is a write and we need to replay, */
1244 req->r_reassert_version = rhead->reassert_version;
1245
1246 req->r_got_reply = 1;
1247 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1248 dout("handle_reply tid %llu dup ack\n", tid);
34b43a56 1249 mutex_unlock(&osdc->request_mutex);
f24e9980
SW
1250 goto done;
1251 }
1252
1253 dout("handle_reply tid %llu flags %d\n", tid, flags);
1254
a40c4f10
YS
1255 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1256 __register_linger_request(osdc, req);
1257
f24e9980 1258 /* either this is a read, or we got the safe response */
0ceed5db
SW
1259 if (result < 0 ||
1260 (flags & CEPH_OSD_FLAG_ONDISK) ||
f24e9980
SW
1261 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1262 __unregister_request(osdc, req);
1263
1264 mutex_unlock(&osdc->request_mutex);
1265
1266 if (req->r_callback)
1267 req->r_callback(req, msg);
1268 else
03066f23 1269 complete_all(&req->r_completion);
f24e9980 1270
25845472
SW
1271 if (flags & CEPH_OSD_FLAG_ONDISK)
1272 complete_request(req);
f24e9980
SW
1273
1274done:
a40c4f10 1275 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
f24e9980
SW
1276 ceph_osdc_put_request(req);
1277 return;
1278
1279bad:
1280 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1281 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1282 (int)sizeof(*rhead));
9ec7cab1 1283 ceph_msg_dump(msg);
f24e9980
SW
1284}
1285
6f6c7006 1286static void reset_changed_osds(struct ceph_osd_client *osdc)
f24e9980 1287{
f24e9980 1288 struct rb_node *p, *n;
f24e9980 1289
6f6c7006
SW
1290 for (p = rb_first(&osdc->osds); p; p = n) {
1291 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
f24e9980 1292
6f6c7006
SW
1293 n = rb_next(p);
1294 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1295 memcmp(&osd->o_con.peer_addr,
1296 ceph_osd_addr(osdc->osdmap,
1297 osd->o_osd),
1298 sizeof(struct ceph_entity_addr)) != 0)
1299 __reset_osd(osdc, osd);
f24e9980 1300 }
422d2cb8
YS
1301}
1302
1303/*
6f6c7006
SW
1304 * Requeue requests whose mapping to an OSD has changed. If requests map to
1305 * no osd, request a new map.
422d2cb8
YS
1306 *
1307 * Caller should hold map_sem for read and request_mutex.
1308 */
38d6453c 1309static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
422d2cb8 1310{
a40c4f10 1311 struct ceph_osd_request *req, *nreq;
6f6c7006
SW
1312 struct rb_node *p;
1313 int needmap = 0;
1314 int err;
422d2cb8 1315
38d6453c 1316 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
422d2cb8 1317 mutex_lock(&osdc->request_mutex);
6194ea89 1318 for (p = rb_first(&osdc->requests); p; ) {
6f6c7006 1319 req = rb_entry(p, struct ceph_osd_request, r_node);
6194ea89 1320 p = rb_next(p);
38d6453c 1321 err = __map_request(osdc, req, force_resend);
6f6c7006
SW
1322 if (err < 0)
1323 continue; /* error */
1324 if (req->r_osd == NULL) {
1325 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1326 needmap++; /* request a newer map */
1327 } else if (err > 0) {
6194ea89
SW
1328 if (!req->r_linger) {
1329 dout("%p tid %llu requeued on osd%d\n", req,
1330 req->r_tid,
1331 req->r_osd ? req->r_osd->o_osd : -1);
a40c4f10 1332 req->r_flags |= CEPH_OSD_FLAG_RETRY;
6194ea89
SW
1333 }
1334 }
1335 if (req->r_linger && list_empty(&req->r_linger_item)) {
1336 /*
1337 * register as a linger so that we will
1338 * re-submit below and get a new tid
1339 */
1340 dout("%p tid %llu restart on osd%d\n",
1341 req, req->r_tid,
1342 req->r_osd ? req->r_osd->o_osd : -1);
1343 __register_linger_request(osdc, req);
1344 __unregister_request(osdc, req);
a40c4f10
YS
1345 }
1346 }
1347
1348 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1349 r_linger_item) {
1350 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1351
38d6453c 1352 err = __map_request(osdc, req, force_resend);
a40c4f10
YS
1353 if (err == 0)
1354 continue; /* no change and no osd was specified */
1355 if (err < 0)
1356 continue; /* hrm! */
1357 if (req->r_osd == NULL) {
1358 dout("tid %llu maps to no valid osd\n", req->r_tid);
1359 needmap++; /* request a newer map */
1360 continue;
6f6c7006 1361 }
a40c4f10
YS
1362
1363 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1364 req->r_osd ? req->r_osd->o_osd : -1);
1365 __unregister_linger_request(osdc, req);
1366 __register_request(osdc, req);
6f6c7006 1367 }
f24e9980
SW
1368 mutex_unlock(&osdc->request_mutex);
1369
1370 if (needmap) {
1371 dout("%d requests for down osds, need new map\n", needmap);
1372 ceph_monc_request_next_osdmap(&osdc->client->monc);
1373 }
422d2cb8 1374}
6f6c7006
SW
1375
1376
f24e9980
SW
1377/*
1378 * Process updated osd map.
1379 *
1380 * The message contains any number of incremental and full maps, normally
1381 * indicating some sort of topology change in the cluster. Kick requests
1382 * off to different OSDs as needed.
1383 */
1384void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1385{
1386 void *p, *end, *next;
1387 u32 nr_maps, maplen;
1388 u32 epoch;
1389 struct ceph_osdmap *newmap = NULL, *oldmap;
1390 int err;
1391 struct ceph_fsid fsid;
1392
1393 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1394 p = msg->front.iov_base;
1395 end = p + msg->front.iov_len;
1396
1397 /* verify fsid */
1398 ceph_decode_need(&p, end, sizeof(fsid), bad);
1399 ceph_decode_copy(&p, &fsid, sizeof(fsid));
0743304d
SW
1400 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1401 return;
f24e9980
SW
1402
1403 down_write(&osdc->map_sem);
1404
1405 /* incremental maps */
1406 ceph_decode_32_safe(&p, end, nr_maps, bad);
1407 dout(" %d inc maps\n", nr_maps);
1408 while (nr_maps > 0) {
1409 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1410 epoch = ceph_decode_32(&p);
1411 maplen = ceph_decode_32(&p);
f24e9980
SW
1412 ceph_decode_need(&p, end, maplen, bad);
1413 next = p + maplen;
1414 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1415 dout("applying incremental map %u len %d\n",
1416 epoch, maplen);
1417 newmap = osdmap_apply_incremental(&p, next,
1418 osdc->osdmap,
15d9882c 1419 &osdc->client->msgr);
f24e9980
SW
1420 if (IS_ERR(newmap)) {
1421 err = PTR_ERR(newmap);
1422 goto bad;
1423 }
30dc6381 1424 BUG_ON(!newmap);
f24e9980
SW
1425 if (newmap != osdc->osdmap) {
1426 ceph_osdmap_destroy(osdc->osdmap);
1427 osdc->osdmap = newmap;
1428 }
38d6453c 1429 kick_requests(osdc, 0);
6f6c7006 1430 reset_changed_osds(osdc);
f24e9980
SW
1431 } else {
1432 dout("ignoring incremental map %u len %d\n",
1433 epoch, maplen);
1434 }
1435 p = next;
1436 nr_maps--;
1437 }
1438 if (newmap)
1439 goto done;
1440
1441 /* full maps */
1442 ceph_decode_32_safe(&p, end, nr_maps, bad);
1443 dout(" %d full maps\n", nr_maps);
1444 while (nr_maps) {
1445 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
c89136ea
SW
1446 epoch = ceph_decode_32(&p);
1447 maplen = ceph_decode_32(&p);
f24e9980
SW
1448 ceph_decode_need(&p, end, maplen, bad);
1449 if (nr_maps > 1) {
1450 dout("skipping non-latest full map %u len %d\n",
1451 epoch, maplen);
1452 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1453 dout("skipping full map %u len %d, "
1454 "older than our %u\n", epoch, maplen,
1455 osdc->osdmap->epoch);
1456 } else {
38d6453c
SW
1457 int skipped_map = 0;
1458
f24e9980
SW
1459 dout("taking full map %u len %d\n", epoch, maplen);
1460 newmap = osdmap_decode(&p, p+maplen);
1461 if (IS_ERR(newmap)) {
1462 err = PTR_ERR(newmap);
1463 goto bad;
1464 }
30dc6381 1465 BUG_ON(!newmap);
f24e9980
SW
1466 oldmap = osdc->osdmap;
1467 osdc->osdmap = newmap;
38d6453c
SW
1468 if (oldmap) {
1469 if (oldmap->epoch + 1 < newmap->epoch)
1470 skipped_map = 1;
f24e9980 1471 ceph_osdmap_destroy(oldmap);
38d6453c
SW
1472 }
1473 kick_requests(osdc, skipped_map);
f24e9980
SW
1474 }
1475 p += maplen;
1476 nr_maps--;
1477 }
1478
1479done:
1480 downgrade_write(&osdc->map_sem);
1481 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
cd634fb6
SW
1482
1483 /*
1484 * subscribe to subsequent osdmap updates if full to ensure
1485 * we find out when we are no longer full and stop returning
1486 * ENOSPC.
1487 */
1488 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1489 ceph_monc_request_next_osdmap(&osdc->client->monc);
1490
6f6c7006 1491 send_queued(osdc);
f24e9980 1492 up_read(&osdc->map_sem);
03066f23 1493 wake_up_all(&osdc->client->auth_wq);
f24e9980
SW
1494 return;
1495
1496bad:
1497 pr_err("osdc handle_map corrupt msg\n");
9ec7cab1 1498 ceph_msg_dump(msg);
f24e9980
SW
1499 up_write(&osdc->map_sem);
1500 return;
1501}
1502
a40c4f10
YS
1503/*
1504 * watch/notify callback event infrastructure
1505 *
1506 * These callbacks are used both for watch and notify operations.
1507 */
1508static void __release_event(struct kref *kref)
1509{
1510 struct ceph_osd_event *event =
1511 container_of(kref, struct ceph_osd_event, kref);
1512
1513 dout("__release_event %p\n", event);
1514 kfree(event);
1515}
1516
1517static void get_event(struct ceph_osd_event *event)
1518{
1519 kref_get(&event->kref);
1520}
1521
1522void ceph_osdc_put_event(struct ceph_osd_event *event)
1523{
1524 kref_put(&event->kref, __release_event);
1525}
1526EXPORT_SYMBOL(ceph_osdc_put_event);
1527
1528static void __insert_event(struct ceph_osd_client *osdc,
1529 struct ceph_osd_event *new)
1530{
1531 struct rb_node **p = &osdc->event_tree.rb_node;
1532 struct rb_node *parent = NULL;
1533 struct ceph_osd_event *event = NULL;
1534
1535 while (*p) {
1536 parent = *p;
1537 event = rb_entry(parent, struct ceph_osd_event, node);
1538 if (new->cookie < event->cookie)
1539 p = &(*p)->rb_left;
1540 else if (new->cookie > event->cookie)
1541 p = &(*p)->rb_right;
1542 else
1543 BUG();
1544 }
1545
1546 rb_link_node(&new->node, parent, p);
1547 rb_insert_color(&new->node, &osdc->event_tree);
1548}
1549
1550static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1551 u64 cookie)
1552{
1553 struct rb_node **p = &osdc->event_tree.rb_node;
1554 struct rb_node *parent = NULL;
1555 struct ceph_osd_event *event = NULL;
1556
1557 while (*p) {
1558 parent = *p;
1559 event = rb_entry(parent, struct ceph_osd_event, node);
1560 if (cookie < event->cookie)
1561 p = &(*p)->rb_left;
1562 else if (cookie > event->cookie)
1563 p = &(*p)->rb_right;
1564 else
1565 return event;
1566 }
1567 return NULL;
1568}
1569
1570static void __remove_event(struct ceph_osd_event *event)
1571{
1572 struct ceph_osd_client *osdc = event->osdc;
1573
1574 if (!RB_EMPTY_NODE(&event->node)) {
1575 dout("__remove_event removed %p\n", event);
1576 rb_erase(&event->node, &osdc->event_tree);
1577 ceph_osdc_put_event(event);
1578 } else {
1579 dout("__remove_event didn't remove %p\n", event);
1580 }
1581}
1582
1583int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1584 void (*event_cb)(u64, u64, u8, void *),
1585 int one_shot, void *data,
1586 struct ceph_osd_event **pevent)
1587{
1588 struct ceph_osd_event *event;
1589
1590 event = kmalloc(sizeof(*event), GFP_NOIO);
1591 if (!event)
1592 return -ENOMEM;
1593
1594 dout("create_event %p\n", event);
1595 event->cb = event_cb;
1596 event->one_shot = one_shot;
1597 event->data = data;
1598 event->osdc = osdc;
1599 INIT_LIST_HEAD(&event->osd_node);
1600 kref_init(&event->kref); /* one ref for us */
1601 kref_get(&event->kref); /* one ref for the caller */
1602 init_completion(&event->completion);
1603
1604 spin_lock(&osdc->event_lock);
1605 event->cookie = ++osdc->event_count;
1606 __insert_event(osdc, event);
1607 spin_unlock(&osdc->event_lock);
1608
1609 *pevent = event;
1610 return 0;
1611}
1612EXPORT_SYMBOL(ceph_osdc_create_event);
1613
1614void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1615{
1616 struct ceph_osd_client *osdc = event->osdc;
1617
1618 dout("cancel_event %p\n", event);
1619 spin_lock(&osdc->event_lock);
1620 __remove_event(event);
1621 spin_unlock(&osdc->event_lock);
1622 ceph_osdc_put_event(event); /* caller's */
1623}
1624EXPORT_SYMBOL(ceph_osdc_cancel_event);
1625
1626
1627static void do_event_work(struct work_struct *work)
1628{
1629 struct ceph_osd_event_work *event_work =
1630 container_of(work, struct ceph_osd_event_work, work);
1631 struct ceph_osd_event *event = event_work->event;
1632 u64 ver = event_work->ver;
1633 u64 notify_id = event_work->notify_id;
1634 u8 opcode = event_work->opcode;
1635
1636 dout("do_event_work completing %p\n", event);
1637 event->cb(ver, notify_id, opcode, event->data);
1638 complete(&event->completion);
1639 dout("do_event_work completed %p\n", event);
1640 ceph_osdc_put_event(event);
1641 kfree(event_work);
1642}
1643
1644
1645/*
1646 * Process osd watch notifications
1647 */
1648void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1649{
1650 void *p, *end;
1651 u8 proto_ver;
1652 u64 cookie, ver, notify_id;
1653 u8 opcode;
1654 struct ceph_osd_event *event;
1655 struct ceph_osd_event_work *event_work;
1656
1657 p = msg->front.iov_base;
1658 end = p + msg->front.iov_len;
1659
1660 ceph_decode_8_safe(&p, end, proto_ver, bad);
1661 ceph_decode_8_safe(&p, end, opcode, bad);
1662 ceph_decode_64_safe(&p, end, cookie, bad);
1663 ceph_decode_64_safe(&p, end, ver, bad);
1664 ceph_decode_64_safe(&p, end, notify_id, bad);
1665
1666 spin_lock(&osdc->event_lock);
1667 event = __find_event(osdc, cookie);
1668 if (event) {
1669 get_event(event);
1670 if (event->one_shot)
1671 __remove_event(event);
1672 }
1673 spin_unlock(&osdc->event_lock);
1674 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1675 cookie, ver, event);
1676 if (event) {
1677 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
a40c4f10
YS
1678 if (!event_work) {
1679 dout("ERROR: could not allocate event_work\n");
1680 goto done_err;
1681 }
6b0ae409 1682 INIT_WORK(&event_work->work, do_event_work);
a40c4f10
YS
1683 event_work->event = event;
1684 event_work->ver = ver;
1685 event_work->notify_id = notify_id;
1686 event_work->opcode = opcode;
1687 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1688 dout("WARNING: failed to queue notify event work\n");
1689 goto done_err;
1690 }
1691 }
1692
1693 return;
1694
1695done_err:
1696 complete(&event->completion);
1697 ceph_osdc_put_event(event);
1698 return;
1699
1700bad:
1701 pr_err("osdc handle_watch_notify corrupt msg\n");
1702 return;
1703}
1704
1705int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1706{
1707 int err;
1708
1709 dout("wait_event %p\n", event);
1710 err = wait_for_completion_interruptible_timeout(&event->completion,
1711 timeout * HZ);
1712 ceph_osdc_put_event(event);
1713 if (err > 0)
1714 err = 0;
1715 dout("wait_event %p returns %d\n", event, err);
1716 return err;
1717}
1718EXPORT_SYMBOL(ceph_osdc_wait_event);
1719
f24e9980
SW
1720/*
1721 * Register request, send initial attempt.
1722 */
1723int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1724 struct ceph_osd_request *req,
1725 bool nofail)
1726{
c1ea8823 1727 int rc = 0;
f24e9980
SW
1728
1729 req->r_request->pages = req->r_pages;
1730 req->r_request->nr_pages = req->r_num_pages;
68b4476b
YS
1731#ifdef CONFIG_BLOCK
1732 req->r_request->bio = req->r_bio;
1733#endif
1734 req->r_request->trail = req->r_trail;
f24e9980
SW
1735
1736 register_request(osdc, req);
1737
1738 down_read(&osdc->map_sem);
1739 mutex_lock(&osdc->request_mutex);
c1ea8823
SW
1740 /*
1741 * a racing kick_requests() may have sent the message for us
1742 * while we dropped request_mutex above, so only send now if
1743 * the request still han't been touched yet.
1744 */
1745 if (req->r_sent == 0) {
38d6453c 1746 rc = __map_request(osdc, req, 0);
9d6fcb08
SW
1747 if (rc < 0) {
1748 if (nofail) {
1749 dout("osdc_start_request failed map, "
1750 " will retry %lld\n", req->r_tid);
1751 rc = 0;
1752 }
234af26f 1753 goto out_unlock;
9d6fcb08 1754 }
6f6c7006
SW
1755 if (req->r_osd == NULL) {
1756 dout("send_request %p no up osds in pg\n", req);
1757 ceph_monc_request_next_osdmap(&osdc->client->monc);
1758 } else {
56e925b6 1759 __send_request(osdc, req);
f24e9980 1760 }
56e925b6 1761 rc = 0;
f24e9980 1762 }
234af26f
DC
1763
1764out_unlock:
f24e9980
SW
1765 mutex_unlock(&osdc->request_mutex);
1766 up_read(&osdc->map_sem);
1767 return rc;
1768}
3d14c5d2 1769EXPORT_SYMBOL(ceph_osdc_start_request);
f24e9980
SW
1770
1771/*
1772 * wait for a request to complete
1773 */
1774int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1775 struct ceph_osd_request *req)
1776{
1777 int rc;
1778
1779 rc = wait_for_completion_interruptible(&req->r_completion);
1780 if (rc < 0) {
1781 mutex_lock(&osdc->request_mutex);
1782 __cancel_request(req);
529cfcc4 1783 __unregister_request(osdc, req);
f24e9980 1784 mutex_unlock(&osdc->request_mutex);
25845472 1785 complete_request(req);
529cfcc4 1786 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
f24e9980
SW
1787 return rc;
1788 }
1789
1790 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1791 return req->r_result;
1792}
3d14c5d2 1793EXPORT_SYMBOL(ceph_osdc_wait_request);
f24e9980
SW
1794
1795/*
1796 * sync - wait for all in-flight requests to flush. avoid starvation.
1797 */
1798void ceph_osdc_sync(struct ceph_osd_client *osdc)
1799{
1800 struct ceph_osd_request *req;
1801 u64 last_tid, next_tid = 0;
1802
1803 mutex_lock(&osdc->request_mutex);
1804 last_tid = osdc->last_tid;
1805 while (1) {
1806 req = __lookup_request_ge(osdc, next_tid);
1807 if (!req)
1808 break;
1809 if (req->r_tid > last_tid)
1810 break;
1811
1812 next_tid = req->r_tid + 1;
1813 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1814 continue;
1815
1816 ceph_osdc_get_request(req);
1817 mutex_unlock(&osdc->request_mutex);
1818 dout("sync waiting on tid %llu (last is %llu)\n",
1819 req->r_tid, last_tid);
1820 wait_for_completion(&req->r_safe_completion);
1821 mutex_lock(&osdc->request_mutex);
1822 ceph_osdc_put_request(req);
1823 }
1824 mutex_unlock(&osdc->request_mutex);
1825 dout("sync done (thru tid %llu)\n", last_tid);
1826}
3d14c5d2 1827EXPORT_SYMBOL(ceph_osdc_sync);
f24e9980
SW
1828
1829/*
1830 * init, shutdown
1831 */
1832int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1833{
1834 int err;
1835
1836 dout("init\n");
1837 osdc->client = client;
1838 osdc->osdmap = NULL;
1839 init_rwsem(&osdc->map_sem);
1840 init_completion(&osdc->map_waiters);
1841 osdc->last_requested_map = 0;
1842 mutex_init(&osdc->request_mutex);
f24e9980
SW
1843 osdc->last_tid = 0;
1844 osdc->osds = RB_ROOT;
f5a2041b 1845 INIT_LIST_HEAD(&osdc->osd_lru);
f24e9980 1846 osdc->requests = RB_ROOT;
422d2cb8 1847 INIT_LIST_HEAD(&osdc->req_lru);
6f6c7006
SW
1848 INIT_LIST_HEAD(&osdc->req_unsent);
1849 INIT_LIST_HEAD(&osdc->req_notarget);
a40c4f10 1850 INIT_LIST_HEAD(&osdc->req_linger);
f24e9980
SW
1851 osdc->num_requests = 0;
1852 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
f5a2041b 1853 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
a40c4f10
YS
1854 spin_lock_init(&osdc->event_lock);
1855 osdc->event_tree = RB_ROOT;
1856 osdc->event_count = 0;
f5a2041b
YS
1857
1858 schedule_delayed_work(&osdc->osds_timeout_work,
3d14c5d2 1859 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
f24e9980 1860
5f44f142 1861 err = -ENOMEM;
f24e9980
SW
1862 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1863 sizeof(struct ceph_osd_request));
1864 if (!osdc->req_mempool)
5f44f142 1865 goto out;
f24e9980 1866
d50b409f
SW
1867 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1868 OSD_OP_FRONT_LEN, 10, true,
4f48280e 1869 "osd_op");
f24e9980 1870 if (err < 0)
5f44f142 1871 goto out_mempool;
d50b409f 1872 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
4f48280e
SW
1873 OSD_OPREPLY_FRONT_LEN, 10, true,
1874 "osd_op_reply");
c16e7869
SW
1875 if (err < 0)
1876 goto out_msgpool;
a40c4f10
YS
1877
1878 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1879 if (IS_ERR(osdc->notify_wq)) {
1880 err = PTR_ERR(osdc->notify_wq);
1881 osdc->notify_wq = NULL;
1882 goto out_msgpool;
1883 }
f24e9980 1884 return 0;
5f44f142 1885
c16e7869
SW
1886out_msgpool:
1887 ceph_msgpool_destroy(&osdc->msgpool_op);
5f44f142
SW
1888out_mempool:
1889 mempool_destroy(osdc->req_mempool);
1890out:
1891 return err;
f24e9980 1892}
3d14c5d2 1893EXPORT_SYMBOL(ceph_osdc_init);
f24e9980
SW
1894
1895void ceph_osdc_stop(struct ceph_osd_client *osdc)
1896{
a40c4f10
YS
1897 flush_workqueue(osdc->notify_wq);
1898 destroy_workqueue(osdc->notify_wq);
f24e9980 1899 cancel_delayed_work_sync(&osdc->timeout_work);
f5a2041b 1900 cancel_delayed_work_sync(&osdc->osds_timeout_work);
f24e9980
SW
1901 if (osdc->osdmap) {
1902 ceph_osdmap_destroy(osdc->osdmap);
1903 osdc->osdmap = NULL;
1904 }
aca420bc 1905 remove_all_osds(osdc);
f24e9980
SW
1906 mempool_destroy(osdc->req_mempool);
1907 ceph_msgpool_destroy(&osdc->msgpool_op);
c16e7869 1908 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
f24e9980 1909}
3d14c5d2 1910EXPORT_SYMBOL(ceph_osdc_stop);
f24e9980
SW
1911
1912/*
1913 * Read some contiguous pages. If we cross a stripe boundary, shorten
1914 * *plen. Return number of bytes read, or error.
1915 */
1916int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1917 struct ceph_vino vino, struct ceph_file_layout *layout,
1918 u64 off, u64 *plen,
1919 u32 truncate_seq, u64 truncate_size,
b7495fc2 1920 struct page **pages, int num_pages, int page_align)
f24e9980
SW
1921{
1922 struct ceph_osd_request *req;
1923 int rc = 0;
1924
1925 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1926 vino.snap, off, *plen);
1927 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1928 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1929 NULL, 0, truncate_seq, truncate_size, NULL,
b7495fc2 1930 false, 1, page_align);
a79832f2
SW
1931 if (!req)
1932 return -ENOMEM;
f24e9980
SW
1933
1934 /* it may be a short read due to an object boundary */
1935 req->r_pages = pages;
f24e9980 1936
b7495fc2
SW
1937 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1938 off, *plen, req->r_num_pages, page_align);
f24e9980
SW
1939
1940 rc = ceph_osdc_start_request(osdc, req, false);
1941 if (!rc)
1942 rc = ceph_osdc_wait_request(osdc, req);
1943
1944 ceph_osdc_put_request(req);
1945 dout("readpages result %d\n", rc);
1946 return rc;
1947}
3d14c5d2 1948EXPORT_SYMBOL(ceph_osdc_readpages);
f24e9980
SW
1949
1950/*
1951 * do a synchronous write on N pages
1952 */
1953int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1954 struct ceph_file_layout *layout,
1955 struct ceph_snap_context *snapc,
1956 u64 off, u64 len,
1957 u32 truncate_seq, u64 truncate_size,
1958 struct timespec *mtime,
1959 struct page **pages, int num_pages,
1960 int flags, int do_sync, bool nofail)
1961{
1962 struct ceph_osd_request *req;
1963 int rc = 0;
b7495fc2 1964 int page_align = off & ~PAGE_MASK;
f24e9980
SW
1965
1966 BUG_ON(vino.snap != CEPH_NOSNAP);
1967 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1968 CEPH_OSD_OP_WRITE,
1969 flags | CEPH_OSD_FLAG_ONDISK |
1970 CEPH_OSD_FLAG_WRITE,
1971 snapc, do_sync,
1972 truncate_seq, truncate_size, mtime,
b7495fc2 1973 nofail, 1, page_align);
a79832f2
SW
1974 if (!req)
1975 return -ENOMEM;
f24e9980
SW
1976
1977 /* it may be a short write due to an object boundary */
1978 req->r_pages = pages;
f24e9980
SW
1979 dout("writepages %llu~%llu (%d pages)\n", off, len,
1980 req->r_num_pages);
1981
1982 rc = ceph_osdc_start_request(osdc, req, nofail);
1983 if (!rc)
1984 rc = ceph_osdc_wait_request(osdc, req);
1985
1986 ceph_osdc_put_request(req);
1987 if (rc == 0)
1988 rc = len;
1989 dout("writepages result %d\n", rc);
1990 return rc;
1991}
3d14c5d2 1992EXPORT_SYMBOL(ceph_osdc_writepages);
f24e9980
SW
1993
1994/*
1995 * handle incoming message
1996 */
1997static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1998{
1999 struct ceph_osd *osd = con->private;
32c895e7 2000 struct ceph_osd_client *osdc;
f24e9980
SW
2001 int type = le16_to_cpu(msg->hdr.type);
2002
2003 if (!osd)
4a32f93d 2004 goto out;
32c895e7 2005 osdc = osd->o_osdc;
f24e9980
SW
2006
2007 switch (type) {
2008 case CEPH_MSG_OSD_MAP:
2009 ceph_osdc_handle_map(osdc, msg);
2010 break;
2011 case CEPH_MSG_OSD_OPREPLY:
350b1c32 2012 handle_reply(osdc, msg, con);
f24e9980 2013 break;
a40c4f10
YS
2014 case CEPH_MSG_WATCH_NOTIFY:
2015 handle_watch_notify(osdc, msg);
2016 break;
f24e9980
SW
2017
2018 default:
2019 pr_err("received unknown message type %d %s\n", type,
2020 ceph_msg_type_name(type));
2021 }
4a32f93d 2022out:
f24e9980
SW
2023 ceph_msg_put(msg);
2024}
2025
5b3a4db3 2026/*
21b667f6
SW
2027 * lookup and return message for incoming reply. set up reply message
2028 * pages.
5b3a4db3
SW
2029 */
2030static struct ceph_msg *get_reply(struct ceph_connection *con,
2450418c
YS
2031 struct ceph_msg_header *hdr,
2032 int *skip)
f24e9980
SW
2033{
2034 struct ceph_osd *osd = con->private;
2035 struct ceph_osd_client *osdc = osd->o_osdc;
2450418c 2036 struct ceph_msg *m;
0547a9b3 2037 struct ceph_osd_request *req;
5b3a4db3
SW
2038 int front = le32_to_cpu(hdr->front_len);
2039 int data_len = le32_to_cpu(hdr->data_len);
0547a9b3 2040 u64 tid;
f24e9980 2041
0547a9b3
YS
2042 tid = le64_to_cpu(hdr->tid);
2043 mutex_lock(&osdc->request_mutex);
2044 req = __lookup_request(osdc, tid);
2045 if (!req) {
2046 *skip = 1;
2047 m = NULL;
756a16a5
SW
2048 dout("get_reply unknown tid %llu from osd%d\n", tid,
2049 osd->o_osd);
0547a9b3
YS
2050 goto out;
2051 }
c16e7869
SW
2052
2053 if (req->r_con_filling_msg) {
8921d114 2054 dout("%s revoking msg %p from old con %p\n", __func__,
c16e7869 2055 req->r_reply, req->r_con_filling_msg);
8921d114 2056 ceph_msg_revoke_incoming(req->r_reply);
0d47766f 2057 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
6f46cb29 2058 req->r_con_filling_msg = NULL;
0547a9b3
YS
2059 }
2060
c16e7869
SW
2061 if (front > req->r_reply->front.iov_len) {
2062 pr_warning("get_reply front %d > preallocated %d\n",
2063 front, (int)req->r_reply->front.iov_len);
b61c2763 2064 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
a79832f2 2065 if (!m)
c16e7869
SW
2066 goto out;
2067 ceph_msg_put(req->r_reply);
2068 req->r_reply = m;
2069 }
2070 m = ceph_msg_get(req->r_reply);
2071
0547a9b3 2072 if (data_len > 0) {
b7495fc2 2073 int want = calc_pages_for(req->r_page_alignment, data_len);
21b667f6
SW
2074
2075 if (unlikely(req->r_num_pages < want)) {
9bb0ce2b
SW
2076 pr_warning("tid %lld reply has %d bytes %d pages, we"
2077 " had only %d pages ready\n", tid, data_len,
2078 want, req->r_num_pages);
0547a9b3
YS
2079 *skip = 1;
2080 ceph_msg_put(m);
a79832f2 2081 m = NULL;
21b667f6 2082 goto out;
0547a9b3 2083 }
21b667f6
SW
2084 m->pages = req->r_pages;
2085 m->nr_pages = req->r_num_pages;
c5c6b19d 2086 m->page_alignment = req->r_page_alignment;
68b4476b
YS
2087#ifdef CONFIG_BLOCK
2088 m->bio = req->r_bio;
2089#endif
0547a9b3 2090 }
5b3a4db3 2091 *skip = 0;
0d47766f 2092 req->r_con_filling_msg = con->ops->get(con);
c16e7869 2093 dout("get_reply tid %lld %p\n", tid, m);
0547a9b3
YS
2094
2095out:
2096 mutex_unlock(&osdc->request_mutex);
2450418c 2097 return m;
5b3a4db3
SW
2098
2099}
2100
2101static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2102 struct ceph_msg_header *hdr,
2103 int *skip)
2104{
2105 struct ceph_osd *osd = con->private;
2106 int type = le16_to_cpu(hdr->type);
2107 int front = le32_to_cpu(hdr->front_len);
2108
1c20f2d2 2109 *skip = 0;
5b3a4db3
SW
2110 switch (type) {
2111 case CEPH_MSG_OSD_MAP:
a40c4f10 2112 case CEPH_MSG_WATCH_NOTIFY:
b61c2763 2113 return ceph_msg_new(type, front, GFP_NOFS, false);
5b3a4db3
SW
2114 case CEPH_MSG_OSD_OPREPLY:
2115 return get_reply(con, hdr, skip);
2116 default:
2117 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2118 osd->o_osd);
2119 *skip = 1;
2120 return NULL;
2121 }
f24e9980
SW
2122}
2123
2124/*
2125 * Wrappers to refcount containing ceph_osd struct
2126 */
2127static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2128{
2129 struct ceph_osd *osd = con->private;
2130 if (get_osd(osd))
2131 return con;
2132 return NULL;
2133}
2134
2135static void put_osd_con(struct ceph_connection *con)
2136{
2137 struct ceph_osd *osd = con->private;
2138 put_osd(osd);
2139}
2140
4e7a5dcd
SW
2141/*
2142 * authentication
2143 */
a3530df3
AE
2144/*
2145 * Note: returned pointer is the address of a structure that's
2146 * managed separately. Caller must *not* attempt to free it.
2147 */
2148static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
8f43fb53 2149 int *proto, int force_new)
4e7a5dcd
SW
2150{
2151 struct ceph_osd *o = con->private;
2152 struct ceph_osd_client *osdc = o->o_osdc;
2153 struct ceph_auth_client *ac = osdc->client->monc.auth;
74f1869f 2154 struct ceph_auth_handshake *auth = &o->o_auth;
4e7a5dcd 2155
74f1869f 2156 if (force_new && auth->authorizer) {
a255651d
AE
2157 if (ac->ops && ac->ops->destroy_authorizer)
2158 ac->ops->destroy_authorizer(ac, auth->authorizer);
74f1869f
AE
2159 auth->authorizer = NULL;
2160 }
a255651d 2161 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
a3530df3
AE
2162 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2163 auth);
4e7a5dcd 2164 if (ret)
a3530df3 2165 return ERR_PTR(ret);
4e7a5dcd 2166 }
4e7a5dcd 2167 *proto = ac->protocol;
74f1869f 2168
a3530df3 2169 return auth;
4e7a5dcd
SW
2170}
2171
2172
2173static int verify_authorizer_reply(struct ceph_connection *con, int len)
2174{
2175 struct ceph_osd *o = con->private;
2176 struct ceph_osd_client *osdc = o->o_osdc;
2177 struct ceph_auth_client *ac = osdc->client->monc.auth;
2178
a255651d
AE
2179 /*
2180 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2181 * XXX which do we do: succeed or fail?
2182 */
6c4a1915 2183 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
4e7a5dcd
SW
2184}
2185
9bd2e6f8
SW
2186static int invalidate_authorizer(struct ceph_connection *con)
2187{
2188 struct ceph_osd *o = con->private;
2189 struct ceph_osd_client *osdc = o->o_osdc;
2190 struct ceph_auth_client *ac = osdc->client->monc.auth;
2191
a255651d 2192 if (ac->ops && ac->ops->invalidate_authorizer)
9bd2e6f8
SW
2193 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2194
2195 return ceph_monc_validate_auth(&osdc->client->monc);
2196}
4e7a5dcd 2197
9e32789f 2198static const struct ceph_connection_operations osd_con_ops = {
f24e9980
SW
2199 .get = get_osd_con,
2200 .put = put_osd_con,
2201 .dispatch = dispatch,
4e7a5dcd
SW
2202 .get_authorizer = get_authorizer,
2203 .verify_authorizer_reply = verify_authorizer_reply,
9bd2e6f8 2204 .invalidate_authorizer = invalidate_authorizer,
f24e9980 2205 .alloc_msg = alloc_msg,
81b024e7 2206 .fault = osd_reset,
f24e9980 2207};