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