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