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