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