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