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