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