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