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