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