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