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