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