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