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