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