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