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