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