OSDN Git Service

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