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