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