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