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