]> asedeno.scripts.mit.edu Git - linux.git/blob - net/ceph/osd_client.c
libceph: don't abort reads in ceph_osdc_abort_on_full()
[linux.git] / net / ceph / osd_client.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <linux/ceph/ceph_debug.h>
4
5 #include <linux/module.h>
6 #include <linux/err.h>
7 #include <linux/highmem.h>
8 #include <linux/mm.h>
9 #include <linux/pagemap.h>
10 #include <linux/slab.h>
11 #include <linux/uaccess.h>
12 #ifdef CONFIG_BLOCK
13 #include <linux/bio.h>
14 #endif
15
16 #include <linux/ceph/ceph_features.h>
17 #include <linux/ceph/libceph.h>
18 #include <linux/ceph/osd_client.h>
19 #include <linux/ceph/messenger.h>
20 #include <linux/ceph/decode.h>
21 #include <linux/ceph/auth.h>
22 #include <linux/ceph/pagelist.h>
23 #include <linux/ceph/striper.h>
24
25 #define OSD_OPREPLY_FRONT_LEN   512
26
27 static struct kmem_cache        *ceph_osd_request_cache;
28
29 static const struct ceph_connection_operations osd_con_ops;
30
31 /*
32  * Implement client access to distributed object storage cluster.
33  *
34  * All data objects are stored within a cluster/cloud of OSDs, or
35  * "object storage devices."  (Note that Ceph OSDs have _nothing_ to
36  * do with the T10 OSD extensions to SCSI.)  Ceph OSDs are simply
37  * remote daemons serving up and coordinating consistent and safe
38  * access to storage.
39  *
40  * Cluster membership and the mapping of data objects onto storage devices
41  * are described by the osd map.
42  *
43  * We keep track of pending OSD requests (read, write), resubmit
44  * requests to different OSDs when the cluster topology/data layout
45  * change, or retry the affected requests when the communications
46  * channel with an OSD is reset.
47  */
48
49 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
50 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
51 static void link_linger(struct ceph_osd *osd,
52                         struct ceph_osd_linger_request *lreq);
53 static void unlink_linger(struct ceph_osd *osd,
54                           struct ceph_osd_linger_request *lreq);
55 static void clear_backoffs(struct ceph_osd *osd);
56
57 #if 1
58 static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
59 {
60         bool wrlocked = true;
61
62         if (unlikely(down_read_trylock(sem))) {
63                 wrlocked = false;
64                 up_read(sem);
65         }
66
67         return wrlocked;
68 }
69 static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
70 {
71         WARN_ON(!rwsem_is_locked(&osdc->lock));
72 }
73 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
74 {
75         WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
76 }
77 static inline void verify_osd_locked(struct ceph_osd *osd)
78 {
79         struct ceph_osd_client *osdc = osd->o_osdc;
80
81         WARN_ON(!(mutex_is_locked(&osd->lock) &&
82                   rwsem_is_locked(&osdc->lock)) &&
83                 !rwsem_is_wrlocked(&osdc->lock));
84 }
85 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
86 {
87         WARN_ON(!mutex_is_locked(&lreq->lock));
88 }
89 #else
90 static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
91 static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
92 static inline void verify_osd_locked(struct ceph_osd *osd) { }
93 static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
94 #endif
95
96 /*
97  * calculate the mapping of a file extent onto an object, and fill out the
98  * request accordingly.  shorten extent as necessary if it crosses an
99  * object boundary.
100  *
101  * fill osd op in request message.
102  */
103 static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
104                         u64 *objnum, u64 *objoff, u64 *objlen)
105 {
106         u64 orig_len = *plen;
107         u32 xlen;
108
109         /* object extent? */
110         ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
111                                           objoff, &xlen);
112         *objlen = xlen;
113         if (*objlen < orig_len) {
114                 *plen = *objlen;
115                 dout(" skipping last %llu, final file extent %llu~%llu\n",
116                      orig_len - *plen, off, *plen);
117         }
118
119         dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
120         return 0;
121 }
122
123 static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
124 {
125         memset(osd_data, 0, sizeof (*osd_data));
126         osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
127 }
128
129 static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
130                         struct page **pages, u64 length, u32 alignment,
131                         bool pages_from_pool, bool own_pages)
132 {
133         osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
134         osd_data->pages = pages;
135         osd_data->length = length;
136         osd_data->alignment = alignment;
137         osd_data->pages_from_pool = pages_from_pool;
138         osd_data->own_pages = own_pages;
139 }
140
141 static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
142                         struct ceph_pagelist *pagelist)
143 {
144         osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
145         osd_data->pagelist = pagelist;
146 }
147
148 #ifdef CONFIG_BLOCK
149 static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
150                                    struct ceph_bio_iter *bio_pos,
151                                    u32 bio_length)
152 {
153         osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
154         osd_data->bio_pos = *bio_pos;
155         osd_data->bio_length = bio_length;
156 }
157 #endif /* CONFIG_BLOCK */
158
159 static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
160                                      struct ceph_bvec_iter *bvec_pos,
161                                      u32 num_bvecs)
162 {
163         osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
164         osd_data->bvec_pos = *bvec_pos;
165         osd_data->num_bvecs = num_bvecs;
166 }
167
168 #define osd_req_op_data(oreq, whch, typ, fld)                           \
169 ({                                                                      \
170         struct ceph_osd_request *__oreq = (oreq);                       \
171         unsigned int __whch = (whch);                                   \
172         BUG_ON(__whch >= __oreq->r_num_ops);                            \
173         &__oreq->r_ops[__whch].typ.fld;                                 \
174 })
175
176 static struct ceph_osd_data *
177 osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
178 {
179         BUG_ON(which >= osd_req->r_num_ops);
180
181         return &osd_req->r_ops[which].raw_data_in;
182 }
183
184 struct ceph_osd_data *
185 osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
186                         unsigned int which)
187 {
188         return osd_req_op_data(osd_req, which, extent, osd_data);
189 }
190 EXPORT_SYMBOL(osd_req_op_extent_osd_data);
191
192 void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
193                         unsigned int which, struct page **pages,
194                         u64 length, u32 alignment,
195                         bool pages_from_pool, bool own_pages)
196 {
197         struct ceph_osd_data *osd_data;
198
199         osd_data = osd_req_op_raw_data_in(osd_req, which);
200         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
201                                 pages_from_pool, own_pages);
202 }
203 EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
204
205 void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
206                         unsigned int which, struct page **pages,
207                         u64 length, u32 alignment,
208                         bool pages_from_pool, bool own_pages)
209 {
210         struct ceph_osd_data *osd_data;
211
212         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
213         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
214                                 pages_from_pool, own_pages);
215 }
216 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
217
218 void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
219                         unsigned int which, struct ceph_pagelist *pagelist)
220 {
221         struct ceph_osd_data *osd_data;
222
223         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
224         ceph_osd_data_pagelist_init(osd_data, pagelist);
225 }
226 EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
227
228 #ifdef CONFIG_BLOCK
229 void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
230                                     unsigned int which,
231                                     struct ceph_bio_iter *bio_pos,
232                                     u32 bio_length)
233 {
234         struct ceph_osd_data *osd_data;
235
236         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
237         ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
238 }
239 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
240 #endif /* CONFIG_BLOCK */
241
242 void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
243                                       unsigned int which,
244                                       struct bio_vec *bvecs, u32 num_bvecs,
245                                       u32 bytes)
246 {
247         struct ceph_osd_data *osd_data;
248         struct ceph_bvec_iter it = {
249                 .bvecs = bvecs,
250                 .iter = { .bi_size = bytes },
251         };
252
253         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
254         ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
255 }
256 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
257
258 void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
259                                          unsigned int which,
260                                          struct ceph_bvec_iter *bvec_pos)
261 {
262         struct ceph_osd_data *osd_data;
263
264         osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
265         ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
266 }
267 EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
268
269 static void osd_req_op_cls_request_info_pagelist(
270                         struct ceph_osd_request *osd_req,
271                         unsigned int which, struct ceph_pagelist *pagelist)
272 {
273         struct ceph_osd_data *osd_data;
274
275         osd_data = osd_req_op_data(osd_req, which, cls, request_info);
276         ceph_osd_data_pagelist_init(osd_data, pagelist);
277 }
278
279 void osd_req_op_cls_request_data_pagelist(
280                         struct ceph_osd_request *osd_req,
281                         unsigned int which, struct ceph_pagelist *pagelist)
282 {
283         struct ceph_osd_data *osd_data;
284
285         osd_data = osd_req_op_data(osd_req, which, cls, request_data);
286         ceph_osd_data_pagelist_init(osd_data, pagelist);
287         osd_req->r_ops[which].cls.indata_len += pagelist->length;
288         osd_req->r_ops[which].indata_len += pagelist->length;
289 }
290 EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
291
292 void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
293                         unsigned int which, struct page **pages, u64 length,
294                         u32 alignment, bool pages_from_pool, bool own_pages)
295 {
296         struct ceph_osd_data *osd_data;
297
298         osd_data = osd_req_op_data(osd_req, which, cls, request_data);
299         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
300                                 pages_from_pool, own_pages);
301         osd_req->r_ops[which].cls.indata_len += length;
302         osd_req->r_ops[which].indata_len += length;
303 }
304 EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
305
306 void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
307                                        unsigned int which,
308                                        struct bio_vec *bvecs, u32 num_bvecs,
309                                        u32 bytes)
310 {
311         struct ceph_osd_data *osd_data;
312         struct ceph_bvec_iter it = {
313                 .bvecs = bvecs,
314                 .iter = { .bi_size = bytes },
315         };
316
317         osd_data = osd_req_op_data(osd_req, which, cls, request_data);
318         ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
319         osd_req->r_ops[which].cls.indata_len += bytes;
320         osd_req->r_ops[which].indata_len += bytes;
321 }
322 EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
323
324 void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
325                         unsigned int which, struct page **pages, u64 length,
326                         u32 alignment, bool pages_from_pool, bool own_pages)
327 {
328         struct ceph_osd_data *osd_data;
329
330         osd_data = osd_req_op_data(osd_req, which, cls, response_data);
331         ceph_osd_data_pages_init(osd_data, pages, length, alignment,
332                                 pages_from_pool, own_pages);
333 }
334 EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
335
336 static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
337 {
338         switch (osd_data->type) {
339         case CEPH_OSD_DATA_TYPE_NONE:
340                 return 0;
341         case CEPH_OSD_DATA_TYPE_PAGES:
342                 return osd_data->length;
343         case CEPH_OSD_DATA_TYPE_PAGELIST:
344                 return (u64)osd_data->pagelist->length;
345 #ifdef CONFIG_BLOCK
346         case CEPH_OSD_DATA_TYPE_BIO:
347                 return (u64)osd_data->bio_length;
348 #endif /* CONFIG_BLOCK */
349         case CEPH_OSD_DATA_TYPE_BVECS:
350                 return osd_data->bvec_pos.iter.bi_size;
351         default:
352                 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
353                 return 0;
354         }
355 }
356
357 static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
358 {
359         if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
360                 int num_pages;
361
362                 num_pages = calc_pages_for((u64)osd_data->alignment,
363                                                 (u64)osd_data->length);
364                 ceph_release_page_vector(osd_data->pages, num_pages);
365         }
366         ceph_osd_data_init(osd_data);
367 }
368
369 static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
370                         unsigned int which)
371 {
372         struct ceph_osd_req_op *op;
373
374         BUG_ON(which >= osd_req->r_num_ops);
375         op = &osd_req->r_ops[which];
376
377         switch (op->op) {
378         case CEPH_OSD_OP_READ:
379         case CEPH_OSD_OP_WRITE:
380         case CEPH_OSD_OP_WRITEFULL:
381                 ceph_osd_data_release(&op->extent.osd_data);
382                 break;
383         case CEPH_OSD_OP_CALL:
384                 ceph_osd_data_release(&op->cls.request_info);
385                 ceph_osd_data_release(&op->cls.request_data);
386                 ceph_osd_data_release(&op->cls.response_data);
387                 break;
388         case CEPH_OSD_OP_SETXATTR:
389         case CEPH_OSD_OP_CMPXATTR:
390                 ceph_osd_data_release(&op->xattr.osd_data);
391                 break;
392         case CEPH_OSD_OP_STAT:
393                 ceph_osd_data_release(&op->raw_data_in);
394                 break;
395         case CEPH_OSD_OP_NOTIFY_ACK:
396                 ceph_osd_data_release(&op->notify_ack.request_data);
397                 break;
398         case CEPH_OSD_OP_NOTIFY:
399                 ceph_osd_data_release(&op->notify.request_data);
400                 ceph_osd_data_release(&op->notify.response_data);
401                 break;
402         case CEPH_OSD_OP_LIST_WATCHERS:
403                 ceph_osd_data_release(&op->list_watchers.response_data);
404                 break;
405         default:
406                 break;
407         }
408 }
409
410 /*
411  * Assumes @t is zero-initialized.
412  */
413 static void target_init(struct ceph_osd_request_target *t)
414 {
415         ceph_oid_init(&t->base_oid);
416         ceph_oloc_init(&t->base_oloc);
417         ceph_oid_init(&t->target_oid);
418         ceph_oloc_init(&t->target_oloc);
419
420         ceph_osds_init(&t->acting);
421         ceph_osds_init(&t->up);
422         t->size = -1;
423         t->min_size = -1;
424
425         t->osd = CEPH_HOMELESS_OSD;
426 }
427
428 static void target_copy(struct ceph_osd_request_target *dest,
429                         const struct ceph_osd_request_target *src)
430 {
431         ceph_oid_copy(&dest->base_oid, &src->base_oid);
432         ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
433         ceph_oid_copy(&dest->target_oid, &src->target_oid);
434         ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
435
436         dest->pgid = src->pgid; /* struct */
437         dest->spgid = src->spgid; /* struct */
438         dest->pg_num = src->pg_num;
439         dest->pg_num_mask = src->pg_num_mask;
440         ceph_osds_copy(&dest->acting, &src->acting);
441         ceph_osds_copy(&dest->up, &src->up);
442         dest->size = src->size;
443         dest->min_size = src->min_size;
444         dest->sort_bitwise = src->sort_bitwise;
445
446         dest->flags = src->flags;
447         dest->paused = src->paused;
448
449         dest->epoch = src->epoch;
450         dest->last_force_resend = src->last_force_resend;
451
452         dest->osd = src->osd;
453 }
454
455 static void target_destroy(struct ceph_osd_request_target *t)
456 {
457         ceph_oid_destroy(&t->base_oid);
458         ceph_oloc_destroy(&t->base_oloc);
459         ceph_oid_destroy(&t->target_oid);
460         ceph_oloc_destroy(&t->target_oloc);
461 }
462
463 /*
464  * requests
465  */
466 static void request_release_checks(struct ceph_osd_request *req)
467 {
468         WARN_ON(!RB_EMPTY_NODE(&req->r_node));
469         WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
470         WARN_ON(!list_empty(&req->r_unsafe_item));
471         WARN_ON(req->r_osd);
472 }
473
474 static void ceph_osdc_release_request(struct kref *kref)
475 {
476         struct ceph_osd_request *req = container_of(kref,
477                                             struct ceph_osd_request, r_kref);
478         unsigned int which;
479
480         dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
481              req->r_request, req->r_reply);
482         request_release_checks(req);
483
484         if (req->r_request)
485                 ceph_msg_put(req->r_request);
486         if (req->r_reply)
487                 ceph_msg_put(req->r_reply);
488
489         for (which = 0; which < req->r_num_ops; which++)
490                 osd_req_op_data_release(req, which);
491
492         target_destroy(&req->r_t);
493         ceph_put_snap_context(req->r_snapc);
494
495         if (req->r_mempool)
496                 mempool_free(req, req->r_osdc->req_mempool);
497         else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
498                 kmem_cache_free(ceph_osd_request_cache, req);
499         else
500                 kfree(req);
501 }
502
503 void ceph_osdc_get_request(struct ceph_osd_request *req)
504 {
505         dout("%s %p (was %d)\n", __func__, req,
506              kref_read(&req->r_kref));
507         kref_get(&req->r_kref);
508 }
509 EXPORT_SYMBOL(ceph_osdc_get_request);
510
511 void ceph_osdc_put_request(struct ceph_osd_request *req)
512 {
513         if (req) {
514                 dout("%s %p (was %d)\n", __func__, req,
515                      kref_read(&req->r_kref));
516                 kref_put(&req->r_kref, ceph_osdc_release_request);
517         }
518 }
519 EXPORT_SYMBOL(ceph_osdc_put_request);
520
521 static void request_init(struct ceph_osd_request *req)
522 {
523         /* req only, each op is zeroed in _osd_req_op_init() */
524         memset(req, 0, sizeof(*req));
525
526         kref_init(&req->r_kref);
527         init_completion(&req->r_completion);
528         RB_CLEAR_NODE(&req->r_node);
529         RB_CLEAR_NODE(&req->r_mc_node);
530         INIT_LIST_HEAD(&req->r_unsafe_item);
531
532         target_init(&req->r_t);
533 }
534
535 /*
536  * This is ugly, but it allows us to reuse linger registration and ping
537  * requests, keeping the structure of the code around send_linger{_ping}()
538  * reasonable.  Setting up a min_nr=2 mempool for each linger request
539  * and dealing with copying ops (this blasts req only, watch op remains
540  * intact) isn't any better.
541  */
542 static void request_reinit(struct ceph_osd_request *req)
543 {
544         struct ceph_osd_client *osdc = req->r_osdc;
545         bool mempool = req->r_mempool;
546         unsigned int num_ops = req->r_num_ops;
547         u64 snapid = req->r_snapid;
548         struct ceph_snap_context *snapc = req->r_snapc;
549         bool linger = req->r_linger;
550         struct ceph_msg *request_msg = req->r_request;
551         struct ceph_msg *reply_msg = req->r_reply;
552
553         dout("%s req %p\n", __func__, req);
554         WARN_ON(kref_read(&req->r_kref) != 1);
555         request_release_checks(req);
556
557         WARN_ON(kref_read(&request_msg->kref) != 1);
558         WARN_ON(kref_read(&reply_msg->kref) != 1);
559         target_destroy(&req->r_t);
560
561         request_init(req);
562         req->r_osdc = osdc;
563         req->r_mempool = mempool;
564         req->r_num_ops = num_ops;
565         req->r_snapid = snapid;
566         req->r_snapc = snapc;
567         req->r_linger = linger;
568         req->r_request = request_msg;
569         req->r_reply = reply_msg;
570 }
571
572 struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
573                                                struct ceph_snap_context *snapc,
574                                                unsigned int num_ops,
575                                                bool use_mempool,
576                                                gfp_t gfp_flags)
577 {
578         struct ceph_osd_request *req;
579
580         if (use_mempool) {
581                 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
582                 req = mempool_alloc(osdc->req_mempool, gfp_flags);
583         } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
584                 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
585         } else {
586                 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
587                 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
588                               gfp_flags);
589         }
590         if (unlikely(!req))
591                 return NULL;
592
593         request_init(req);
594         req->r_osdc = osdc;
595         req->r_mempool = use_mempool;
596         req->r_num_ops = num_ops;
597         req->r_snapid = CEPH_NOSNAP;
598         req->r_snapc = ceph_get_snap_context(snapc);
599
600         dout("%s req %p\n", __func__, req);
601         return req;
602 }
603 EXPORT_SYMBOL(ceph_osdc_alloc_request);
604
605 static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
606 {
607         return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
608 }
609
610 int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
611 {
612         struct ceph_osd_client *osdc = req->r_osdc;
613         struct ceph_msg *msg;
614         int msg_size;
615
616         WARN_ON(ceph_oid_empty(&req->r_base_oid));
617         WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
618
619         /* create request message */
620         msg_size = CEPH_ENCODING_START_BLK_LEN +
621                         CEPH_PGID_ENCODING_LEN + 1; /* spgid */
622         msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
623         msg_size += CEPH_ENCODING_START_BLK_LEN +
624                         sizeof(struct ceph_osd_reqid); /* reqid */
625         msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
626         msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
627         msg_size += CEPH_ENCODING_START_BLK_LEN +
628                         ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
629         msg_size += 4 + req->r_base_oid.name_len; /* oid */
630         msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
631         msg_size += 8; /* snapid */
632         msg_size += 8; /* snap_seq */
633         msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
634         msg_size += 4 + 8; /* retry_attempt, features */
635
636         if (req->r_mempool)
637                 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
638         else
639                 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp, true);
640         if (!msg)
641                 return -ENOMEM;
642
643         memset(msg->front.iov_base, 0, msg->front.iov_len);
644         req->r_request = msg;
645
646         /* create reply message */
647         msg_size = OSD_OPREPLY_FRONT_LEN;
648         msg_size += req->r_base_oid.name_len;
649         msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
650
651         if (req->r_mempool)
652                 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
653         else
654                 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size, gfp, true);
655         if (!msg)
656                 return -ENOMEM;
657
658         req->r_reply = msg;
659
660         return 0;
661 }
662 EXPORT_SYMBOL(ceph_osdc_alloc_messages);
663
664 static bool osd_req_opcode_valid(u16 opcode)
665 {
666         switch (opcode) {
667 #define GENERATE_CASE(op, opcode, str)  case CEPH_OSD_OP_##op: return true;
668 __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
669 #undef GENERATE_CASE
670         default:
671                 return false;
672         }
673 }
674
675 /*
676  * This is an osd op init function for opcodes that have no data or
677  * other information associated with them.  It also serves as a
678  * common init routine for all the other init functions, below.
679  */
680 static struct ceph_osd_req_op *
681 _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
682                  u16 opcode, u32 flags)
683 {
684         struct ceph_osd_req_op *op;
685
686         BUG_ON(which >= osd_req->r_num_ops);
687         BUG_ON(!osd_req_opcode_valid(opcode));
688
689         op = &osd_req->r_ops[which];
690         memset(op, 0, sizeof (*op));
691         op->op = opcode;
692         op->flags = flags;
693
694         return op;
695 }
696
697 void osd_req_op_init(struct ceph_osd_request *osd_req,
698                      unsigned int which, u16 opcode, u32 flags)
699 {
700         (void)_osd_req_op_init(osd_req, which, opcode, flags);
701 }
702 EXPORT_SYMBOL(osd_req_op_init);
703
704 void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
705                                 unsigned int which, u16 opcode,
706                                 u64 offset, u64 length,
707                                 u64 truncate_size, u32 truncate_seq)
708 {
709         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
710                                                       opcode, 0);
711         size_t payload_len = 0;
712
713         BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
714                opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
715                opcode != CEPH_OSD_OP_TRUNCATE);
716
717         op->extent.offset = offset;
718         op->extent.length = length;
719         op->extent.truncate_size = truncate_size;
720         op->extent.truncate_seq = truncate_seq;
721         if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
722                 payload_len += length;
723
724         op->indata_len = payload_len;
725 }
726 EXPORT_SYMBOL(osd_req_op_extent_init);
727
728 void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
729                                 unsigned int which, u64 length)
730 {
731         struct ceph_osd_req_op *op;
732         u64 previous;
733
734         BUG_ON(which >= osd_req->r_num_ops);
735         op = &osd_req->r_ops[which];
736         previous = op->extent.length;
737
738         if (length == previous)
739                 return;         /* Nothing to do */
740         BUG_ON(length > previous);
741
742         op->extent.length = length;
743         if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
744                 op->indata_len -= previous - length;
745 }
746 EXPORT_SYMBOL(osd_req_op_extent_update);
747
748 void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
749                                 unsigned int which, u64 offset_inc)
750 {
751         struct ceph_osd_req_op *op, *prev_op;
752
753         BUG_ON(which + 1 >= osd_req->r_num_ops);
754
755         prev_op = &osd_req->r_ops[which];
756         op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
757         /* dup previous one */
758         op->indata_len = prev_op->indata_len;
759         op->outdata_len = prev_op->outdata_len;
760         op->extent = prev_op->extent;
761         /* adjust offset */
762         op->extent.offset += offset_inc;
763         op->extent.length -= offset_inc;
764
765         if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
766                 op->indata_len -= offset_inc;
767 }
768 EXPORT_SYMBOL(osd_req_op_extent_dup_last);
769
770 int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
771                         u16 opcode, const char *class, const char *method)
772 {
773         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
774                                                       opcode, 0);
775         struct ceph_pagelist *pagelist;
776         size_t payload_len = 0;
777         size_t size;
778
779         BUG_ON(opcode != CEPH_OSD_OP_CALL);
780
781         pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
782         if (!pagelist)
783                 return -ENOMEM;
784
785         ceph_pagelist_init(pagelist);
786
787         op->cls.class_name = class;
788         size = strlen(class);
789         BUG_ON(size > (size_t) U8_MAX);
790         op->cls.class_len = size;
791         ceph_pagelist_append(pagelist, class, size);
792         payload_len += size;
793
794         op->cls.method_name = method;
795         size = strlen(method);
796         BUG_ON(size > (size_t) U8_MAX);
797         op->cls.method_len = size;
798         ceph_pagelist_append(pagelist, method, size);
799         payload_len += size;
800
801         osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
802
803         op->indata_len = payload_len;
804         return 0;
805 }
806 EXPORT_SYMBOL(osd_req_op_cls_init);
807
808 int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
809                           u16 opcode, const char *name, const void *value,
810                           size_t size, u8 cmp_op, u8 cmp_mode)
811 {
812         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
813                                                       opcode, 0);
814         struct ceph_pagelist *pagelist;
815         size_t payload_len;
816
817         BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
818
819         pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
820         if (!pagelist)
821                 return -ENOMEM;
822
823         ceph_pagelist_init(pagelist);
824
825         payload_len = strlen(name);
826         op->xattr.name_len = payload_len;
827         ceph_pagelist_append(pagelist, name, payload_len);
828
829         op->xattr.value_len = size;
830         ceph_pagelist_append(pagelist, value, size);
831         payload_len += size;
832
833         op->xattr.cmp_op = cmp_op;
834         op->xattr.cmp_mode = cmp_mode;
835
836         ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
837         op->indata_len = payload_len;
838         return 0;
839 }
840 EXPORT_SYMBOL(osd_req_op_xattr_init);
841
842 /*
843  * @watch_opcode: CEPH_OSD_WATCH_OP_*
844  */
845 static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
846                                   u64 cookie, u8 watch_opcode)
847 {
848         struct ceph_osd_req_op *op;
849
850         op = _osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
851         op->watch.cookie = cookie;
852         op->watch.op = watch_opcode;
853         op->watch.gen = 0;
854 }
855
856 void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
857                                 unsigned int which,
858                                 u64 expected_object_size,
859                                 u64 expected_write_size)
860 {
861         struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
862                                                       CEPH_OSD_OP_SETALLOCHINT,
863                                                       0);
864
865         op->alloc_hint.expected_object_size = expected_object_size;
866         op->alloc_hint.expected_write_size = expected_write_size;
867
868         /*
869          * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
870          * not worth a feature bit.  Set FAILOK per-op flag to make
871          * sure older osds don't trip over an unsupported opcode.
872          */
873         op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
874 }
875 EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
876
877 static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
878                                 struct ceph_osd_data *osd_data)
879 {
880         u64 length = ceph_osd_data_length(osd_data);
881
882         if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
883                 BUG_ON(length > (u64) SIZE_MAX);
884                 if (length)
885                         ceph_msg_data_add_pages(msg, osd_data->pages,
886                                         length, osd_data->alignment);
887         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
888                 BUG_ON(!length);
889                 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
890 #ifdef CONFIG_BLOCK
891         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
892                 ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
893 #endif
894         } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
895                 ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
896         } else {
897                 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
898         }
899 }
900
901 static u32 osd_req_encode_op(struct ceph_osd_op *dst,
902                              const struct ceph_osd_req_op *src)
903 {
904         if (WARN_ON(!osd_req_opcode_valid(src->op))) {
905                 pr_err("unrecognized osd opcode %d\n", src->op);
906
907                 return 0;
908         }
909
910         switch (src->op) {
911         case CEPH_OSD_OP_STAT:
912                 break;
913         case CEPH_OSD_OP_READ:
914         case CEPH_OSD_OP_WRITE:
915         case CEPH_OSD_OP_WRITEFULL:
916         case CEPH_OSD_OP_ZERO:
917         case CEPH_OSD_OP_TRUNCATE:
918                 dst->extent.offset = cpu_to_le64(src->extent.offset);
919                 dst->extent.length = cpu_to_le64(src->extent.length);
920                 dst->extent.truncate_size =
921                         cpu_to_le64(src->extent.truncate_size);
922                 dst->extent.truncate_seq =
923                         cpu_to_le32(src->extent.truncate_seq);
924                 break;
925         case CEPH_OSD_OP_CALL:
926                 dst->cls.class_len = src->cls.class_len;
927                 dst->cls.method_len = src->cls.method_len;
928                 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
929                 break;
930         case CEPH_OSD_OP_WATCH:
931                 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
932                 dst->watch.ver = cpu_to_le64(0);
933                 dst->watch.op = src->watch.op;
934                 dst->watch.gen = cpu_to_le32(src->watch.gen);
935                 break;
936         case CEPH_OSD_OP_NOTIFY_ACK:
937                 break;
938         case CEPH_OSD_OP_NOTIFY:
939                 dst->notify.cookie = cpu_to_le64(src->notify.cookie);
940                 break;
941         case CEPH_OSD_OP_LIST_WATCHERS:
942                 break;
943         case CEPH_OSD_OP_SETALLOCHINT:
944                 dst->alloc_hint.expected_object_size =
945                     cpu_to_le64(src->alloc_hint.expected_object_size);
946                 dst->alloc_hint.expected_write_size =
947                     cpu_to_le64(src->alloc_hint.expected_write_size);
948                 break;
949         case CEPH_OSD_OP_SETXATTR:
950         case CEPH_OSD_OP_CMPXATTR:
951                 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
952                 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
953                 dst->xattr.cmp_op = src->xattr.cmp_op;
954                 dst->xattr.cmp_mode = src->xattr.cmp_mode;
955                 break;
956         case CEPH_OSD_OP_CREATE:
957         case CEPH_OSD_OP_DELETE:
958                 break;
959         default:
960                 pr_err("unsupported osd opcode %s\n",
961                         ceph_osd_op_name(src->op));
962                 WARN_ON(1);
963
964                 return 0;
965         }
966
967         dst->op = cpu_to_le16(src->op);
968         dst->flags = cpu_to_le32(src->flags);
969         dst->payload_len = cpu_to_le32(src->indata_len);
970
971         return src->indata_len;
972 }
973
974 /*
975  * build new request AND message, calculate layout, and adjust file
976  * extent as needed.
977  *
978  * if the file was recently truncated, we include information about its
979  * old and new size so that the object can be updated appropriately.  (we
980  * avoid synchronously deleting truncated objects because it's slow.)
981  */
982 struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
983                                                struct ceph_file_layout *layout,
984                                                struct ceph_vino vino,
985                                                u64 off, u64 *plen,
986                                                unsigned int which, int num_ops,
987                                                int opcode, int flags,
988                                                struct ceph_snap_context *snapc,
989                                                u32 truncate_seq,
990                                                u64 truncate_size,
991                                                bool use_mempool)
992 {
993         struct ceph_osd_request *req;
994         u64 objnum = 0;
995         u64 objoff = 0;
996         u64 objlen = 0;
997         int r;
998
999         BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
1000                opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
1001                opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
1002
1003         req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
1004                                         GFP_NOFS);
1005         if (!req) {
1006                 r = -ENOMEM;
1007                 goto fail;
1008         }
1009
1010         /* calculate max write size */
1011         r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
1012         if (r)
1013                 goto fail;
1014
1015         if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
1016                 osd_req_op_init(req, which, opcode, 0);
1017         } else {
1018                 u32 object_size = layout->object_size;
1019                 u32 object_base = off - objoff;
1020                 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
1021                         if (truncate_size <= object_base) {
1022                                 truncate_size = 0;
1023                         } else {
1024                                 truncate_size -= object_base;
1025                                 if (truncate_size > object_size)
1026                                         truncate_size = object_size;
1027                         }
1028                 }
1029                 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
1030                                        truncate_size, truncate_seq);
1031         }
1032
1033         req->r_abort_on_full = true;
1034         req->r_flags = flags;
1035         req->r_base_oloc.pool = layout->pool_id;
1036         req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
1037         ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
1038
1039         req->r_snapid = vino.snap;
1040         if (flags & CEPH_OSD_FLAG_WRITE)
1041                 req->r_data_offset = off;
1042
1043         r = ceph_osdc_alloc_messages(req, GFP_NOFS);
1044         if (r)
1045                 goto fail;
1046
1047         return req;
1048
1049 fail:
1050         ceph_osdc_put_request(req);
1051         return ERR_PTR(r);
1052 }
1053 EXPORT_SYMBOL(ceph_osdc_new_request);
1054
1055 /*
1056  * We keep osd requests in an rbtree, sorted by ->r_tid.
1057  */
1058 DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
1059 DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
1060
1061 /*
1062  * Call @fn on each OSD request as long as @fn returns 0.
1063  */
1064 static void for_each_request(struct ceph_osd_client *osdc,
1065                         int (*fn)(struct ceph_osd_request *req, void *arg),
1066                         void *arg)
1067 {
1068         struct rb_node *n, *p;
1069
1070         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
1071                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
1072
1073                 for (p = rb_first(&osd->o_requests); p; ) {
1074                         struct ceph_osd_request *req =
1075                             rb_entry(p, struct ceph_osd_request, r_node);
1076
1077                         p = rb_next(p);
1078                         if (fn(req, arg))
1079                                 return;
1080                 }
1081         }
1082
1083         for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
1084                 struct ceph_osd_request *req =
1085                     rb_entry(p, struct ceph_osd_request, r_node);
1086
1087                 p = rb_next(p);
1088                 if (fn(req, arg))
1089                         return;
1090         }
1091 }
1092
1093 static bool osd_homeless(struct ceph_osd *osd)
1094 {
1095         return osd->o_osd == CEPH_HOMELESS_OSD;
1096 }
1097
1098 static bool osd_registered(struct ceph_osd *osd)
1099 {
1100         verify_osdc_locked(osd->o_osdc);
1101
1102         return !RB_EMPTY_NODE(&osd->o_node);
1103 }
1104
1105 /*
1106  * Assumes @osd is zero-initialized.
1107  */
1108 static void osd_init(struct ceph_osd *osd)
1109 {
1110         refcount_set(&osd->o_ref, 1);
1111         RB_CLEAR_NODE(&osd->o_node);
1112         osd->o_requests = RB_ROOT;
1113         osd->o_linger_requests = RB_ROOT;
1114         osd->o_backoff_mappings = RB_ROOT;
1115         osd->o_backoffs_by_id = RB_ROOT;
1116         INIT_LIST_HEAD(&osd->o_osd_lru);
1117         INIT_LIST_HEAD(&osd->o_keepalive_item);
1118         osd->o_incarnation = 1;
1119         mutex_init(&osd->lock);
1120 }
1121
1122 static void osd_cleanup(struct ceph_osd *osd)
1123 {
1124         WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
1125         WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
1126         WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
1127         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
1128         WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
1129         WARN_ON(!list_empty(&osd->o_osd_lru));
1130         WARN_ON(!list_empty(&osd->o_keepalive_item));
1131
1132         if (osd->o_auth.authorizer) {
1133                 WARN_ON(osd_homeless(osd));
1134                 ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
1135         }
1136 }
1137
1138 /*
1139  * Track open sessions with osds.
1140  */
1141 static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
1142 {
1143         struct ceph_osd *osd;
1144
1145         WARN_ON(onum == CEPH_HOMELESS_OSD);
1146
1147         osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
1148         osd_init(osd);
1149         osd->o_osdc = osdc;
1150         osd->o_osd = onum;
1151
1152         ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
1153
1154         return osd;
1155 }
1156
1157 static struct ceph_osd *get_osd(struct ceph_osd *osd)
1158 {
1159         if (refcount_inc_not_zero(&osd->o_ref)) {
1160                 dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
1161                      refcount_read(&osd->o_ref));
1162                 return osd;
1163         } else {
1164                 dout("get_osd %p FAIL\n", osd);
1165                 return NULL;
1166         }
1167 }
1168
1169 static void put_osd(struct ceph_osd *osd)
1170 {
1171         dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
1172              refcount_read(&osd->o_ref) - 1);
1173         if (refcount_dec_and_test(&osd->o_ref)) {
1174                 osd_cleanup(osd);
1175                 kfree(osd);
1176         }
1177 }
1178
1179 DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
1180
1181 static void __move_osd_to_lru(struct ceph_osd *osd)
1182 {
1183         struct ceph_osd_client *osdc = osd->o_osdc;
1184
1185         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1186         BUG_ON(!list_empty(&osd->o_osd_lru));
1187
1188         spin_lock(&osdc->osd_lru_lock);
1189         list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
1190         spin_unlock(&osdc->osd_lru_lock);
1191
1192         osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
1193 }
1194
1195 static void maybe_move_osd_to_lru(struct ceph_osd *osd)
1196 {
1197         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1198             RB_EMPTY_ROOT(&osd->o_linger_requests))
1199                 __move_osd_to_lru(osd);
1200 }
1201
1202 static void __remove_osd_from_lru(struct ceph_osd *osd)
1203 {
1204         struct ceph_osd_client *osdc = osd->o_osdc;
1205
1206         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1207
1208         spin_lock(&osdc->osd_lru_lock);
1209         if (!list_empty(&osd->o_osd_lru))
1210                 list_del_init(&osd->o_osd_lru);
1211         spin_unlock(&osdc->osd_lru_lock);
1212 }
1213
1214 /*
1215  * Close the connection and assign any leftover requests to the
1216  * homeless session.
1217  */
1218 static void close_osd(struct ceph_osd *osd)
1219 {
1220         struct ceph_osd_client *osdc = osd->o_osdc;
1221         struct rb_node *n;
1222
1223         verify_osdc_wrlocked(osdc);
1224         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1225
1226         ceph_con_close(&osd->o_con);
1227
1228         for (n = rb_first(&osd->o_requests); n; ) {
1229                 struct ceph_osd_request *req =
1230                     rb_entry(n, struct ceph_osd_request, r_node);
1231
1232                 n = rb_next(n); /* unlink_request() */
1233
1234                 dout(" reassigning req %p tid %llu\n", req, req->r_tid);
1235                 unlink_request(osd, req);
1236                 link_request(&osdc->homeless_osd, req);
1237         }
1238         for (n = rb_first(&osd->o_linger_requests); n; ) {
1239                 struct ceph_osd_linger_request *lreq =
1240                     rb_entry(n, struct ceph_osd_linger_request, node);
1241
1242                 n = rb_next(n); /* unlink_linger() */
1243
1244                 dout(" reassigning lreq %p linger_id %llu\n", lreq,
1245                      lreq->linger_id);
1246                 unlink_linger(osd, lreq);
1247                 link_linger(&osdc->homeless_osd, lreq);
1248         }
1249         clear_backoffs(osd);
1250
1251         __remove_osd_from_lru(osd);
1252         erase_osd(&osdc->osds, osd);
1253         put_osd(osd);
1254 }
1255
1256 /*
1257  * reset osd connect
1258  */
1259 static int reopen_osd(struct ceph_osd *osd)
1260 {
1261         struct ceph_entity_addr *peer_addr;
1262
1263         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
1264
1265         if (RB_EMPTY_ROOT(&osd->o_requests) &&
1266             RB_EMPTY_ROOT(&osd->o_linger_requests)) {
1267                 close_osd(osd);
1268                 return -ENODEV;
1269         }
1270
1271         peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
1272         if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1273                         !ceph_con_opened(&osd->o_con)) {
1274                 struct rb_node *n;
1275
1276                 dout("osd addr hasn't changed and connection never opened, "
1277                      "letting msgr retry\n");
1278                 /* touch each r_stamp for handle_timeout()'s benfit */
1279                 for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
1280                         struct ceph_osd_request *req =
1281                             rb_entry(n, struct ceph_osd_request, r_node);
1282                         req->r_stamp = jiffies;
1283                 }
1284
1285                 return -EAGAIN;
1286         }
1287
1288         ceph_con_close(&osd->o_con);
1289         ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1290         osd->o_incarnation++;
1291
1292         return 0;
1293 }
1294
1295 static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
1296                                           bool wrlocked)
1297 {
1298         struct ceph_osd *osd;
1299
1300         if (wrlocked)
1301                 verify_osdc_wrlocked(osdc);
1302         else
1303                 verify_osdc_locked(osdc);
1304
1305         if (o != CEPH_HOMELESS_OSD)
1306                 osd = lookup_osd(&osdc->osds, o);
1307         else
1308                 osd = &osdc->homeless_osd;
1309         if (!osd) {
1310                 if (!wrlocked)
1311                         return ERR_PTR(-EAGAIN);
1312
1313                 osd = create_osd(osdc, o);
1314                 insert_osd(&osdc->osds, osd);
1315                 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
1316                               &osdc->osdmap->osd_addr[osd->o_osd]);
1317         }
1318
1319         dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
1320         return osd;
1321 }
1322
1323 /*
1324  * Create request <-> OSD session relation.
1325  *
1326  * @req has to be assigned a tid, @osd may be homeless.
1327  */
1328 static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1329 {
1330         verify_osd_locked(osd);
1331         WARN_ON(!req->r_tid || req->r_osd);
1332         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1333              req, req->r_tid);
1334
1335         if (!osd_homeless(osd))
1336                 __remove_osd_from_lru(osd);
1337         else
1338                 atomic_inc(&osd->o_osdc->num_homeless);
1339
1340         get_osd(osd);
1341         insert_request(&osd->o_requests, req);
1342         req->r_osd = osd;
1343 }
1344
1345 static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
1346 {
1347         verify_osd_locked(osd);
1348         WARN_ON(req->r_osd != osd);
1349         dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
1350              req, req->r_tid);
1351
1352         req->r_osd = NULL;
1353         erase_request(&osd->o_requests, req);
1354         put_osd(osd);
1355
1356         if (!osd_homeless(osd))
1357                 maybe_move_osd_to_lru(osd);
1358         else
1359                 atomic_dec(&osd->o_osdc->num_homeless);
1360 }
1361
1362 static bool __pool_full(struct ceph_pg_pool_info *pi)
1363 {
1364         return pi->flags & CEPH_POOL_FLAG_FULL;
1365 }
1366
1367 static bool have_pool_full(struct ceph_osd_client *osdc)
1368 {
1369         struct rb_node *n;
1370
1371         for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
1372                 struct ceph_pg_pool_info *pi =
1373                     rb_entry(n, struct ceph_pg_pool_info, node);
1374
1375                 if (__pool_full(pi))
1376                         return true;
1377         }
1378
1379         return false;
1380 }
1381
1382 static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
1383 {
1384         struct ceph_pg_pool_info *pi;
1385
1386         pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
1387         if (!pi)
1388                 return false;
1389
1390         return __pool_full(pi);
1391 }
1392
1393 /*
1394  * Returns whether a request should be blocked from being sent
1395  * based on the current osdmap and osd_client settings.
1396  */
1397 static bool target_should_be_paused(struct ceph_osd_client *osdc,
1398                                     const struct ceph_osd_request_target *t,
1399                                     struct ceph_pg_pool_info *pi)
1400 {
1401         bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
1402         bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
1403                        ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
1404                        __pool_full(pi);
1405
1406         WARN_ON(pi->id != t->target_oloc.pool);
1407         return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
1408                ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
1409                (osdc->osdmap->epoch < osdc->epoch_barrier);
1410 }
1411
1412 enum calc_target_result {
1413         CALC_TARGET_NO_ACTION = 0,
1414         CALC_TARGET_NEED_RESEND,
1415         CALC_TARGET_POOL_DNE,
1416 };
1417
1418 static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
1419                                            struct ceph_osd_request_target *t,
1420                                            struct ceph_connection *con,
1421                                            bool any_change)
1422 {
1423         struct ceph_pg_pool_info *pi;
1424         struct ceph_pg pgid, last_pgid;
1425         struct ceph_osds up, acting;
1426         bool force_resend = false;
1427         bool unpaused = false;
1428         bool legacy_change;
1429         bool split = false;
1430         bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
1431         bool recovery_deletes = ceph_osdmap_flag(osdc,
1432                                                  CEPH_OSDMAP_RECOVERY_DELETES);
1433         enum calc_target_result ct_res;
1434         int ret;
1435
1436         t->epoch = osdc->osdmap->epoch;
1437         pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
1438         if (!pi) {
1439                 t->osd = CEPH_HOMELESS_OSD;
1440                 ct_res = CALC_TARGET_POOL_DNE;
1441                 goto out;
1442         }
1443
1444         if (osdc->osdmap->epoch == pi->last_force_request_resend) {
1445                 if (t->last_force_resend < pi->last_force_request_resend) {
1446                         t->last_force_resend = pi->last_force_request_resend;
1447                         force_resend = true;
1448                 } else if (t->last_force_resend == 0) {
1449                         force_resend = true;
1450                 }
1451         }
1452
1453         /* apply tiering */
1454         ceph_oid_copy(&t->target_oid, &t->base_oid);
1455         ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
1456         if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
1457                 if (t->flags & CEPH_OSD_FLAG_READ && pi->read_tier >= 0)
1458                         t->target_oloc.pool = pi->read_tier;
1459                 if (t->flags & CEPH_OSD_FLAG_WRITE && pi->write_tier >= 0)
1460                         t->target_oloc.pool = pi->write_tier;
1461
1462                 pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
1463                 if (!pi) {
1464                         t->osd = CEPH_HOMELESS_OSD;
1465                         ct_res = CALC_TARGET_POOL_DNE;
1466                         goto out;
1467                 }
1468         }
1469
1470         ret = __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc,
1471                                           &pgid);
1472         if (ret) {
1473                 WARN_ON(ret != -ENOENT);
1474                 t->osd = CEPH_HOMELESS_OSD;
1475                 ct_res = CALC_TARGET_POOL_DNE;
1476                 goto out;
1477         }
1478         last_pgid.pool = pgid.pool;
1479         last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
1480
1481         ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
1482         if (any_change &&
1483             ceph_is_new_interval(&t->acting,
1484                                  &acting,
1485                                  &t->up,
1486                                  &up,
1487                                  t->size,
1488                                  pi->size,
1489                                  t->min_size,
1490                                  pi->min_size,
1491                                  t->pg_num,
1492                                  pi->pg_num,
1493                                  t->sort_bitwise,
1494                                  sort_bitwise,
1495                                  t->recovery_deletes,
1496                                  recovery_deletes,
1497                                  &last_pgid))
1498                 force_resend = true;
1499
1500         if (t->paused && !target_should_be_paused(osdc, t, pi)) {
1501                 t->paused = false;
1502                 unpaused = true;
1503         }
1504         legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
1505                         ceph_osds_changed(&t->acting, &acting, any_change);
1506         if (t->pg_num)
1507                 split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
1508
1509         if (legacy_change || force_resend || split) {
1510                 t->pgid = pgid; /* struct */
1511                 ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
1512                 ceph_osds_copy(&t->acting, &acting);
1513                 ceph_osds_copy(&t->up, &up);
1514                 t->size = pi->size;
1515                 t->min_size = pi->min_size;
1516                 t->pg_num = pi->pg_num;
1517                 t->pg_num_mask = pi->pg_num_mask;
1518                 t->sort_bitwise = sort_bitwise;
1519                 t->recovery_deletes = recovery_deletes;
1520
1521                 t->osd = acting.primary;
1522         }
1523
1524         if (unpaused || legacy_change || force_resend ||
1525             (split && con && CEPH_HAVE_FEATURE(con->peer_features,
1526                                                RESEND_ON_SPLIT)))
1527                 ct_res = CALC_TARGET_NEED_RESEND;
1528         else
1529                 ct_res = CALC_TARGET_NO_ACTION;
1530
1531 out:
1532         dout("%s t %p -> ct_res %d osd %d\n", __func__, t, ct_res, t->osd);
1533         return ct_res;
1534 }
1535
1536 static struct ceph_spg_mapping *alloc_spg_mapping(void)
1537 {
1538         struct ceph_spg_mapping *spg;
1539
1540         spg = kmalloc(sizeof(*spg), GFP_NOIO);
1541         if (!spg)
1542                 return NULL;
1543
1544         RB_CLEAR_NODE(&spg->node);
1545         spg->backoffs = RB_ROOT;
1546         return spg;
1547 }
1548
1549 static void free_spg_mapping(struct ceph_spg_mapping *spg)
1550 {
1551         WARN_ON(!RB_EMPTY_NODE(&spg->node));
1552         WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
1553
1554         kfree(spg);
1555 }
1556
1557 /*
1558  * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
1559  * ceph_pg_mapping.  Used to track OSD backoffs -- a backoff [range] is
1560  * defined only within a specific spgid; it does not pass anything to
1561  * children on split, or to another primary.
1562  */
1563 DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
1564                  RB_BYPTR, const struct ceph_spg *, node)
1565
1566 static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
1567 {
1568         return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
1569 }
1570
1571 static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
1572                                    void **pkey, size_t *pkey_len)
1573 {
1574         if (hoid->key_len) {
1575                 *pkey = hoid->key;
1576                 *pkey_len = hoid->key_len;
1577         } else {
1578                 *pkey = hoid->oid;
1579                 *pkey_len = hoid->oid_len;
1580         }
1581 }
1582
1583 static int compare_names(const void *name1, size_t name1_len,
1584                          const void *name2, size_t name2_len)
1585 {
1586         int ret;
1587
1588         ret = memcmp(name1, name2, min(name1_len, name2_len));
1589         if (!ret) {
1590                 if (name1_len < name2_len)
1591                         ret = -1;
1592                 else if (name1_len > name2_len)
1593                         ret = 1;
1594         }
1595         return ret;
1596 }
1597
1598 static int hoid_compare(const struct ceph_hobject_id *lhs,
1599                         const struct ceph_hobject_id *rhs)
1600 {
1601         void *effective_key1, *effective_key2;
1602         size_t effective_key1_len, effective_key2_len;
1603         int ret;
1604
1605         if (lhs->is_max < rhs->is_max)
1606                 return -1;
1607         if (lhs->is_max > rhs->is_max)
1608                 return 1;
1609
1610         if (lhs->pool < rhs->pool)
1611                 return -1;
1612         if (lhs->pool > rhs->pool)
1613                 return 1;
1614
1615         if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
1616                 return -1;
1617         if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
1618                 return 1;
1619
1620         ret = compare_names(lhs->nspace, lhs->nspace_len,
1621                             rhs->nspace, rhs->nspace_len);
1622         if (ret)
1623                 return ret;
1624
1625         hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
1626         hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
1627         ret = compare_names(effective_key1, effective_key1_len,
1628                             effective_key2, effective_key2_len);
1629         if (ret)
1630                 return ret;
1631
1632         ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
1633         if (ret)
1634                 return ret;
1635
1636         if (lhs->snapid < rhs->snapid)
1637                 return -1;
1638         if (lhs->snapid > rhs->snapid)
1639                 return 1;
1640
1641         return 0;
1642 }
1643
1644 /*
1645  * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
1646  * compat stuff here.
1647  *
1648  * Assumes @hoid is zero-initialized.
1649  */
1650 static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
1651 {
1652         u8 struct_v;
1653         u32 struct_len;
1654         int ret;
1655
1656         ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
1657                                   &struct_len);
1658         if (ret)
1659                 return ret;
1660
1661         if (struct_v < 4) {
1662                 pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
1663                 goto e_inval;
1664         }
1665
1666         hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
1667                                                 GFP_NOIO);
1668         if (IS_ERR(hoid->key)) {
1669                 ret = PTR_ERR(hoid->key);
1670                 hoid->key = NULL;
1671                 return ret;
1672         }
1673
1674         hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
1675                                                 GFP_NOIO);
1676         if (IS_ERR(hoid->oid)) {
1677                 ret = PTR_ERR(hoid->oid);
1678                 hoid->oid = NULL;
1679                 return ret;
1680         }
1681
1682         ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
1683         ceph_decode_32_safe(p, end, hoid->hash, e_inval);
1684         ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
1685
1686         hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
1687                                                    GFP_NOIO);
1688         if (IS_ERR(hoid->nspace)) {
1689                 ret = PTR_ERR(hoid->nspace);
1690                 hoid->nspace = NULL;
1691                 return ret;
1692         }
1693
1694         ceph_decode_64_safe(p, end, hoid->pool, e_inval);
1695
1696         ceph_hoid_build_hash_cache(hoid);
1697         return 0;
1698
1699 e_inval:
1700         return -EINVAL;
1701 }
1702
1703 static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
1704 {
1705         return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
1706                4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
1707 }
1708
1709 static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
1710 {
1711         ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
1712         ceph_encode_string(p, end, hoid->key, hoid->key_len);
1713         ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
1714         ceph_encode_64(p, hoid->snapid);
1715         ceph_encode_32(p, hoid->hash);
1716         ceph_encode_8(p, hoid->is_max);
1717         ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
1718         ceph_encode_64(p, hoid->pool);
1719 }
1720
1721 static void free_hoid(struct ceph_hobject_id *hoid)
1722 {
1723         if (hoid) {
1724                 kfree(hoid->key);
1725                 kfree(hoid->oid);
1726                 kfree(hoid->nspace);
1727                 kfree(hoid);
1728         }
1729 }
1730
1731 static struct ceph_osd_backoff *alloc_backoff(void)
1732 {
1733         struct ceph_osd_backoff *backoff;
1734
1735         backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
1736         if (!backoff)
1737                 return NULL;
1738
1739         RB_CLEAR_NODE(&backoff->spg_node);
1740         RB_CLEAR_NODE(&backoff->id_node);
1741         return backoff;
1742 }
1743
1744 static void free_backoff(struct ceph_osd_backoff *backoff)
1745 {
1746         WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
1747         WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
1748
1749         free_hoid(backoff->begin);
1750         free_hoid(backoff->end);
1751         kfree(backoff);
1752 }
1753
1754 /*
1755  * Within a specific spgid, backoffs are managed by ->begin hoid.
1756  */
1757 DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
1758                         RB_BYVAL, spg_node);
1759
1760 static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
1761                                             const struct ceph_hobject_id *hoid)
1762 {
1763         struct rb_node *n = root->rb_node;
1764
1765         while (n) {
1766                 struct ceph_osd_backoff *cur =
1767                     rb_entry(n, struct ceph_osd_backoff, spg_node);
1768                 int cmp;
1769
1770                 cmp = hoid_compare(hoid, cur->begin);
1771                 if (cmp < 0) {
1772                         n = n->rb_left;
1773                 } else if (cmp > 0) {
1774                         if (hoid_compare(hoid, cur->end) < 0)
1775                                 return cur;
1776
1777                         n = n->rb_right;
1778                 } else {
1779                         return cur;
1780                 }
1781         }
1782
1783         return NULL;
1784 }
1785
1786 /*
1787  * Each backoff has a unique id within its OSD session.
1788  */
1789 DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
1790
1791 static void clear_backoffs(struct ceph_osd *osd)
1792 {
1793         while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
1794                 struct ceph_spg_mapping *spg =
1795                     rb_entry(rb_first(&osd->o_backoff_mappings),
1796                              struct ceph_spg_mapping, node);
1797
1798                 while (!RB_EMPTY_ROOT(&spg->backoffs)) {
1799                         struct ceph_osd_backoff *backoff =
1800                             rb_entry(rb_first(&spg->backoffs),
1801                                      struct ceph_osd_backoff, spg_node);
1802
1803                         erase_backoff(&spg->backoffs, backoff);
1804                         erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
1805                         free_backoff(backoff);
1806                 }
1807                 erase_spg_mapping(&osd->o_backoff_mappings, spg);
1808                 free_spg_mapping(spg);
1809         }
1810 }
1811
1812 /*
1813  * Set up a temporary, non-owning view into @t.
1814  */
1815 static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
1816                                   const struct ceph_osd_request_target *t)
1817 {
1818         hoid->key = NULL;
1819         hoid->key_len = 0;
1820         hoid->oid = t->target_oid.name;
1821         hoid->oid_len = t->target_oid.name_len;
1822         hoid->snapid = CEPH_NOSNAP;
1823         hoid->hash = t->pgid.seed;
1824         hoid->is_max = false;
1825         if (t->target_oloc.pool_ns) {
1826                 hoid->nspace = t->target_oloc.pool_ns->str;
1827                 hoid->nspace_len = t->target_oloc.pool_ns->len;
1828         } else {
1829                 hoid->nspace = NULL;
1830                 hoid->nspace_len = 0;
1831         }
1832         hoid->pool = t->target_oloc.pool;
1833         ceph_hoid_build_hash_cache(hoid);
1834 }
1835
1836 static bool should_plug_request(struct ceph_osd_request *req)
1837 {
1838         struct ceph_osd *osd = req->r_osd;
1839         struct ceph_spg_mapping *spg;
1840         struct ceph_osd_backoff *backoff;
1841         struct ceph_hobject_id hoid;
1842
1843         spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
1844         if (!spg)
1845                 return false;
1846
1847         hoid_fill_from_target(&hoid, &req->r_t);
1848         backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
1849         if (!backoff)
1850                 return false;
1851
1852         dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
1853              __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
1854              backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
1855         return true;
1856 }
1857
1858 static void setup_request_data(struct ceph_osd_request *req,
1859                                struct ceph_msg *msg)
1860 {
1861         u32 data_len = 0;
1862         int i;
1863
1864         if (!list_empty(&msg->data))
1865                 return;
1866
1867         WARN_ON(msg->data_length);
1868         for (i = 0; i < req->r_num_ops; i++) {
1869                 struct ceph_osd_req_op *op = &req->r_ops[i];
1870
1871                 switch (op->op) {
1872                 /* request */
1873                 case CEPH_OSD_OP_WRITE:
1874                 case CEPH_OSD_OP_WRITEFULL:
1875                         WARN_ON(op->indata_len != op->extent.length);
1876                         ceph_osdc_msg_data_add(msg, &op->extent.osd_data);
1877                         break;
1878                 case CEPH_OSD_OP_SETXATTR:
1879                 case CEPH_OSD_OP_CMPXATTR:
1880                         WARN_ON(op->indata_len != op->xattr.name_len +
1881                                                   op->xattr.value_len);
1882                         ceph_osdc_msg_data_add(msg, &op->xattr.osd_data);
1883                         break;
1884                 case CEPH_OSD_OP_NOTIFY_ACK:
1885                         ceph_osdc_msg_data_add(msg,
1886                                                &op->notify_ack.request_data);
1887                         break;
1888
1889                 /* reply */
1890                 case CEPH_OSD_OP_STAT:
1891                         ceph_osdc_msg_data_add(req->r_reply,
1892                                                &op->raw_data_in);
1893                         break;
1894                 case CEPH_OSD_OP_READ:
1895                         ceph_osdc_msg_data_add(req->r_reply,
1896                                                &op->extent.osd_data);
1897                         break;
1898                 case CEPH_OSD_OP_LIST_WATCHERS:
1899                         ceph_osdc_msg_data_add(req->r_reply,
1900                                                &op->list_watchers.response_data);
1901                         break;
1902
1903                 /* both */
1904                 case CEPH_OSD_OP_CALL:
1905                         WARN_ON(op->indata_len != op->cls.class_len +
1906                                                   op->cls.method_len +
1907                                                   op->cls.indata_len);
1908                         ceph_osdc_msg_data_add(msg, &op->cls.request_info);
1909                         /* optional, can be NONE */
1910                         ceph_osdc_msg_data_add(msg, &op->cls.request_data);
1911                         /* optional, can be NONE */
1912                         ceph_osdc_msg_data_add(req->r_reply,
1913                                                &op->cls.response_data);
1914                         break;
1915                 case CEPH_OSD_OP_NOTIFY:
1916                         ceph_osdc_msg_data_add(msg,
1917                                                &op->notify.request_data);
1918                         ceph_osdc_msg_data_add(req->r_reply,
1919                                                &op->notify.response_data);
1920                         break;
1921                 }
1922
1923                 data_len += op->indata_len;
1924         }
1925
1926         WARN_ON(data_len != msg->data_length);
1927 }
1928
1929 static void encode_pgid(void **p, const struct ceph_pg *pgid)
1930 {
1931         ceph_encode_8(p, 1);
1932         ceph_encode_64(p, pgid->pool);
1933         ceph_encode_32(p, pgid->seed);
1934         ceph_encode_32(p, -1); /* preferred */
1935 }
1936
1937 static void encode_spgid(void **p, const struct ceph_spg *spgid)
1938 {
1939         ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
1940         encode_pgid(p, &spgid->pgid);
1941         ceph_encode_8(p, spgid->shard);
1942 }
1943
1944 static void encode_oloc(void **p, void *end,
1945                         const struct ceph_object_locator *oloc)
1946 {
1947         ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
1948         ceph_encode_64(p, oloc->pool);
1949         ceph_encode_32(p, -1); /* preferred */
1950         ceph_encode_32(p, 0);  /* key len */
1951         if (oloc->pool_ns)
1952                 ceph_encode_string(p, end, oloc->pool_ns->str,
1953                                    oloc->pool_ns->len);
1954         else
1955                 ceph_encode_32(p, 0);
1956 }
1957
1958 static void encode_request_partial(struct ceph_osd_request *req,
1959                                    struct ceph_msg *msg)
1960 {
1961         void *p = msg->front.iov_base;
1962         void *const end = p + msg->front_alloc_len;
1963         u32 data_len = 0;
1964         int i;
1965
1966         if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
1967                 /* snapshots aren't writeable */
1968                 WARN_ON(req->r_snapid != CEPH_NOSNAP);
1969         } else {
1970                 WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
1971                         req->r_data_offset || req->r_snapc);
1972         }
1973
1974         setup_request_data(req, msg);
1975
1976         encode_spgid(&p, &req->r_t.spgid); /* actual spg */
1977         ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
1978         ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
1979         ceph_encode_32(&p, req->r_flags);
1980
1981         /* reqid */
1982         ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
1983         memset(p, 0, sizeof(struct ceph_osd_reqid));
1984         p += sizeof(struct ceph_osd_reqid);
1985
1986         /* trace */
1987         memset(p, 0, sizeof(struct ceph_blkin_trace_info));
1988         p += sizeof(struct ceph_blkin_trace_info);
1989
1990         ceph_encode_32(&p, 0); /* client_inc, always 0 */
1991         ceph_encode_timespec(p, &req->r_mtime);
1992         p += sizeof(struct ceph_timespec);
1993
1994         encode_oloc(&p, end, &req->r_t.target_oloc);
1995         ceph_encode_string(&p, end, req->r_t.target_oid.name,
1996                            req->r_t.target_oid.name_len);
1997
1998         /* ops, can imply data */
1999         ceph_encode_16(&p, req->r_num_ops);
2000         for (i = 0; i < req->r_num_ops; i++) {
2001                 data_len += osd_req_encode_op(p, &req->r_ops[i]);
2002                 p += sizeof(struct ceph_osd_op);
2003         }
2004
2005         ceph_encode_64(&p, req->r_snapid); /* snapid */
2006         if (req->r_snapc) {
2007                 ceph_encode_64(&p, req->r_snapc->seq);
2008                 ceph_encode_32(&p, req->r_snapc->num_snaps);
2009                 for (i = 0; i < req->r_snapc->num_snaps; i++)
2010                         ceph_encode_64(&p, req->r_snapc->snaps[i]);
2011         } else {
2012                 ceph_encode_64(&p, 0); /* snap_seq */
2013                 ceph_encode_32(&p, 0); /* snaps len */
2014         }
2015
2016         ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
2017         BUG_ON(p > end - 8); /* space for features */
2018
2019         msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
2020         /* front_len is finalized in encode_request_finish() */
2021         msg->front.iov_len = p - msg->front.iov_base;
2022         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2023         msg->hdr.data_len = cpu_to_le32(data_len);
2024         /*
2025          * The header "data_off" is a hint to the receiver allowing it
2026          * to align received data into its buffers such that there's no
2027          * need to re-copy it before writing it to disk (direct I/O).
2028          */
2029         msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
2030
2031         dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
2032              req->r_t.target_oid.name, req->r_t.target_oid.name_len);
2033 }
2034
2035 static void encode_request_finish(struct ceph_msg *msg)
2036 {
2037         void *p = msg->front.iov_base;
2038         void *const partial_end = p + msg->front.iov_len;
2039         void *const end = p + msg->front_alloc_len;
2040
2041         if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
2042                 /* luminous OSD -- encode features and be done */
2043                 p = partial_end;
2044                 ceph_encode_64(&p, msg->con->peer_features);
2045         } else {
2046                 struct {
2047                         char spgid[CEPH_ENCODING_START_BLK_LEN +
2048                                    CEPH_PGID_ENCODING_LEN + 1];
2049                         __le32 hash;
2050                         __le32 epoch;
2051                         __le32 flags;
2052                         char reqid[CEPH_ENCODING_START_BLK_LEN +
2053                                    sizeof(struct ceph_osd_reqid)];
2054                         char trace[sizeof(struct ceph_blkin_trace_info)];
2055                         __le32 client_inc;
2056                         struct ceph_timespec mtime;
2057                 } __packed head;
2058                 struct ceph_pg pgid;
2059                 void *oloc, *oid, *tail;
2060                 int oloc_len, oid_len, tail_len;
2061                 int len;
2062
2063                 /*
2064                  * Pre-luminous OSD -- reencode v8 into v4 using @head
2065                  * as a temporary buffer.  Encode the raw PG; the rest
2066                  * is just a matter of moving oloc, oid and tail blobs
2067                  * around.
2068                  */
2069                 memcpy(&head, p, sizeof(head));
2070                 p += sizeof(head);
2071
2072                 oloc = p;
2073                 p += CEPH_ENCODING_START_BLK_LEN;
2074                 pgid.pool = ceph_decode_64(&p);
2075                 p += 4 + 4; /* preferred, key len */
2076                 len = ceph_decode_32(&p);
2077                 p += len;   /* nspace */
2078                 oloc_len = p - oloc;
2079
2080                 oid = p;
2081                 len = ceph_decode_32(&p);
2082                 p += len;
2083                 oid_len = p - oid;
2084
2085                 tail = p;
2086                 tail_len = partial_end - p;
2087
2088                 p = msg->front.iov_base;
2089                 ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
2090                 ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
2091                 ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
2092                 ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
2093
2094                 /* reassert_version */
2095                 memset(p, 0, sizeof(struct ceph_eversion));
2096                 p += sizeof(struct ceph_eversion);
2097
2098                 BUG_ON(p >= oloc);
2099                 memmove(p, oloc, oloc_len);
2100                 p += oloc_len;
2101
2102                 pgid.seed = le32_to_cpu(head.hash);
2103                 encode_pgid(&p, &pgid); /* raw pg */
2104
2105                 BUG_ON(p >= oid);
2106                 memmove(p, oid, oid_len);
2107                 p += oid_len;
2108
2109                 /* tail -- ops, snapid, snapc, retry_attempt */
2110                 BUG_ON(p >= tail);
2111                 memmove(p, tail, tail_len);
2112                 p += tail_len;
2113
2114                 msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
2115         }
2116
2117         BUG_ON(p > end);
2118         msg->front.iov_len = p - msg->front.iov_base;
2119         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
2120
2121         dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
2122              le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
2123              le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
2124              le16_to_cpu(msg->hdr.version));
2125 }
2126
2127 /*
2128  * @req has to be assigned a tid and registered.
2129  */
2130 static void send_request(struct ceph_osd_request *req)
2131 {
2132         struct ceph_osd *osd = req->r_osd;
2133
2134         verify_osd_locked(osd);
2135         WARN_ON(osd->o_osd != req->r_t.osd);
2136
2137         /* backoff? */
2138         if (should_plug_request(req))
2139                 return;
2140
2141         /*
2142          * We may have a previously queued request message hanging
2143          * around.  Cancel it to avoid corrupting the msgr.
2144          */
2145         if (req->r_sent)
2146                 ceph_msg_revoke(req->r_request);
2147
2148         req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
2149         if (req->r_attempts)
2150                 req->r_flags |= CEPH_OSD_FLAG_RETRY;
2151         else
2152                 WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
2153
2154         encode_request_partial(req, req->r_request);
2155
2156         dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
2157              __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
2158              req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
2159              req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
2160              req->r_attempts);
2161
2162         req->r_t.paused = false;
2163         req->r_stamp = jiffies;
2164         req->r_attempts++;
2165
2166         req->r_sent = osd->o_incarnation;
2167         req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
2168         ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
2169 }
2170
2171 static void maybe_request_map(struct ceph_osd_client *osdc)
2172 {
2173         bool continuous = false;
2174
2175         verify_osdc_locked(osdc);
2176         WARN_ON(!osdc->osdmap->epoch);
2177
2178         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2179             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
2180             ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2181                 dout("%s osdc %p continuous\n", __func__, osdc);
2182                 continuous = true;
2183         } else {
2184                 dout("%s osdc %p onetime\n", __func__, osdc);
2185         }
2186
2187         if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2188                                osdc->osdmap->epoch + 1, continuous))
2189                 ceph_monc_renew_subs(&osdc->client->monc);
2190 }
2191
2192 static void complete_request(struct ceph_osd_request *req, int err);
2193 static void send_map_check(struct ceph_osd_request *req);
2194
2195 static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
2196 {
2197         struct ceph_osd_client *osdc = req->r_osdc;
2198         struct ceph_osd *osd;
2199         enum calc_target_result ct_res;
2200         int err = 0;
2201         bool need_send = false;
2202         bool promoted = false;
2203
2204         WARN_ON(req->r_tid);
2205         dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
2206
2207 again:
2208         ct_res = calc_target(osdc, &req->r_t, NULL, false);
2209         if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
2210                 goto promote;
2211
2212         osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
2213         if (IS_ERR(osd)) {
2214                 WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
2215                 goto promote;
2216         }
2217
2218         if (osdc->abort_err) {
2219                 dout("req %p abort_err %d\n", req, osdc->abort_err);
2220                 err = osdc->abort_err;
2221         } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
2222                 dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
2223                      osdc->epoch_barrier);
2224                 req->r_t.paused = true;
2225                 maybe_request_map(osdc);
2226         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2227                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
2228                 dout("req %p pausewr\n", req);
2229                 req->r_t.paused = true;
2230                 maybe_request_map(osdc);
2231         } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
2232                    ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2233                 dout("req %p pauserd\n", req);
2234                 req->r_t.paused = true;
2235                 maybe_request_map(osdc);
2236         } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2237                    !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
2238                                      CEPH_OSD_FLAG_FULL_FORCE)) &&
2239                    (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2240                     pool_full(osdc, req->r_t.base_oloc.pool))) {
2241                 dout("req %p full/pool_full\n", req);
2242                 if (req->r_abort_on_full) {
2243                         err = -ENOSPC;
2244                 } else {
2245                         pr_warn_ratelimited("FULL or reached pool quota\n");
2246                         req->r_t.paused = true;
2247                         maybe_request_map(osdc);
2248                 }
2249         } else if (!osd_homeless(osd)) {
2250                 need_send = true;
2251         } else {
2252                 maybe_request_map(osdc);
2253         }
2254
2255         mutex_lock(&osd->lock);
2256         /*
2257          * Assign the tid atomically with send_request() to protect
2258          * multiple writes to the same object from racing with each
2259          * other, resulting in out of order ops on the OSDs.
2260          */
2261         req->r_tid = atomic64_inc_return(&osdc->last_tid);
2262         link_request(osd, req);
2263         if (need_send)
2264                 send_request(req);
2265         else if (err)
2266                 complete_request(req, err);
2267         mutex_unlock(&osd->lock);
2268
2269         if (!err && ct_res == CALC_TARGET_POOL_DNE)
2270                 send_map_check(req);
2271
2272         if (promoted)
2273                 downgrade_write(&osdc->lock);
2274         return;
2275
2276 promote:
2277         up_read(&osdc->lock);
2278         down_write(&osdc->lock);
2279         wrlocked = true;
2280         promoted = true;
2281         goto again;
2282 }
2283
2284 static void account_request(struct ceph_osd_request *req)
2285 {
2286         WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
2287         WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
2288
2289         req->r_flags |= CEPH_OSD_FLAG_ONDISK;
2290         atomic_inc(&req->r_osdc->num_requests);
2291
2292         req->r_start_stamp = jiffies;
2293 }
2294
2295 static void submit_request(struct ceph_osd_request *req, bool wrlocked)
2296 {
2297         ceph_osdc_get_request(req);
2298         account_request(req);
2299         __submit_request(req, wrlocked);
2300 }
2301
2302 static void finish_request(struct ceph_osd_request *req)
2303 {
2304         struct ceph_osd_client *osdc = req->r_osdc;
2305
2306         WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
2307         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2308
2309         if (req->r_osd)
2310                 unlink_request(req->r_osd, req);
2311         atomic_dec(&osdc->num_requests);
2312
2313         /*
2314          * If an OSD has failed or returned and a request has been sent
2315          * twice, it's possible to get a reply and end up here while the
2316          * request message is queued for delivery.  We will ignore the
2317          * reply, so not a big deal, but better to try and catch it.
2318          */
2319         ceph_msg_revoke(req->r_request);
2320         ceph_msg_revoke_incoming(req->r_reply);
2321 }
2322
2323 static void __complete_request(struct ceph_osd_request *req)
2324 {
2325         dout("%s req %p tid %llu cb %pf result %d\n", __func__, req,
2326              req->r_tid, req->r_callback, req->r_result);
2327
2328         if (req->r_callback)
2329                 req->r_callback(req);
2330         complete_all(&req->r_completion);
2331         ceph_osdc_put_request(req);
2332 }
2333
2334 static void complete_request_workfn(struct work_struct *work)
2335 {
2336         struct ceph_osd_request *req =
2337             container_of(work, struct ceph_osd_request, r_complete_work);
2338
2339         __complete_request(req);
2340 }
2341
2342 /*
2343  * This is open-coded in handle_reply().
2344  */
2345 static void complete_request(struct ceph_osd_request *req, int err)
2346 {
2347         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2348
2349         req->r_result = err;
2350         finish_request(req);
2351
2352         INIT_WORK(&req->r_complete_work, complete_request_workfn);
2353         queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
2354 }
2355
2356 static void cancel_map_check(struct ceph_osd_request *req)
2357 {
2358         struct ceph_osd_client *osdc = req->r_osdc;
2359         struct ceph_osd_request *lookup_req;
2360
2361         verify_osdc_wrlocked(osdc);
2362
2363         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2364         if (!lookup_req)
2365                 return;
2366
2367         WARN_ON(lookup_req != req);
2368         erase_request_mc(&osdc->map_checks, req);
2369         ceph_osdc_put_request(req);
2370 }
2371
2372 static void cancel_request(struct ceph_osd_request *req)
2373 {
2374         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
2375
2376         cancel_map_check(req);
2377         finish_request(req);
2378         complete_all(&req->r_completion);
2379         ceph_osdc_put_request(req);
2380 }
2381
2382 static void abort_request(struct ceph_osd_request *req, int err)
2383 {
2384         dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
2385
2386         cancel_map_check(req);
2387         complete_request(req, err);
2388 }
2389
2390 static int abort_fn(struct ceph_osd_request *req, void *arg)
2391 {
2392         int err = *(int *)arg;
2393
2394         abort_request(req, err);
2395         return 0; /* continue iteration */
2396 }
2397
2398 /*
2399  * Abort all in-flight requests with @err and arrange for all future
2400  * requests to be failed immediately.
2401  */
2402 void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
2403 {
2404         dout("%s osdc %p err %d\n", __func__, osdc, err);
2405         down_write(&osdc->lock);
2406         for_each_request(osdc, abort_fn, &err);
2407         osdc->abort_err = err;
2408         up_write(&osdc->lock);
2409 }
2410 EXPORT_SYMBOL(ceph_osdc_abort_requests);
2411
2412 static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2413 {
2414         if (likely(eb > osdc->epoch_barrier)) {
2415                 dout("updating epoch_barrier from %u to %u\n",
2416                                 osdc->epoch_barrier, eb);
2417                 osdc->epoch_barrier = eb;
2418                 /* Request map if we're not to the barrier yet */
2419                 if (eb > osdc->osdmap->epoch)
2420                         maybe_request_map(osdc);
2421         }
2422 }
2423
2424 void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
2425 {
2426         down_read(&osdc->lock);
2427         if (unlikely(eb > osdc->epoch_barrier)) {
2428                 up_read(&osdc->lock);
2429                 down_write(&osdc->lock);
2430                 update_epoch_barrier(osdc, eb);
2431                 up_write(&osdc->lock);
2432         } else {
2433                 up_read(&osdc->lock);
2434         }
2435 }
2436 EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
2437
2438 /*
2439  * We can end up releasing caps as a result of abort_request().
2440  * In that case, we probably want to ensure that the cap release message
2441  * has an updated epoch barrier in it, so set the epoch barrier prior to
2442  * aborting the first request.
2443  */
2444 static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
2445 {
2446         struct ceph_osd_client *osdc = req->r_osdc;
2447         bool *victims = arg;
2448
2449         if (req->r_abort_on_full &&
2450             (req->r_flags & CEPH_OSD_FLAG_WRITE) &&
2451             (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
2452              pool_full(osdc, req->r_t.base_oloc.pool))) {
2453                 if (!*victims) {
2454                         update_epoch_barrier(osdc, osdc->osdmap->epoch);
2455                         *victims = true;
2456                 }
2457                 abort_request(req, -ENOSPC);
2458         }
2459
2460         return 0; /* continue iteration */
2461 }
2462
2463 /*
2464  * Drop all pending requests that are stalled waiting on a full condition to
2465  * clear, and complete them with ENOSPC as the return code. Set the
2466  * osdc->epoch_barrier to the latest map epoch that we've seen if any were
2467  * cancelled.
2468  */
2469 static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
2470 {
2471         bool victims = false;
2472
2473         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc))
2474                 for_each_request(osdc, abort_on_full_fn, &victims);
2475 }
2476
2477 static void check_pool_dne(struct ceph_osd_request *req)
2478 {
2479         struct ceph_osd_client *osdc = req->r_osdc;
2480         struct ceph_osdmap *map = osdc->osdmap;
2481
2482         verify_osdc_wrlocked(osdc);
2483         WARN_ON(!map->epoch);
2484
2485         if (req->r_attempts) {
2486                 /*
2487                  * We sent a request earlier, which means that
2488                  * previously the pool existed, and now it does not
2489                  * (i.e., it was deleted).
2490                  */
2491                 req->r_map_dne_bound = map->epoch;
2492                 dout("%s req %p tid %llu pool disappeared\n", __func__, req,
2493                      req->r_tid);
2494         } else {
2495                 dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
2496                      req, req->r_tid, req->r_map_dne_bound, map->epoch);
2497         }
2498
2499         if (req->r_map_dne_bound) {
2500                 if (map->epoch >= req->r_map_dne_bound) {
2501                         /* we had a new enough map */
2502                         pr_info_ratelimited("tid %llu pool does not exist\n",
2503                                             req->r_tid);
2504                         complete_request(req, -ENOENT);
2505                 }
2506         } else {
2507                 send_map_check(req);
2508         }
2509 }
2510
2511 static void map_check_cb(struct ceph_mon_generic_request *greq)
2512 {
2513         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
2514         struct ceph_osd_request *req;
2515         u64 tid = greq->private_data;
2516
2517         WARN_ON(greq->result || !greq->u.newest);
2518
2519         down_write(&osdc->lock);
2520         req = lookup_request_mc(&osdc->map_checks, tid);
2521         if (!req) {
2522                 dout("%s tid %llu dne\n", __func__, tid);
2523                 goto out_unlock;
2524         }
2525
2526         dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
2527              req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
2528         if (!req->r_map_dne_bound)
2529                 req->r_map_dne_bound = greq->u.newest;
2530         erase_request_mc(&osdc->map_checks, req);
2531         check_pool_dne(req);
2532
2533         ceph_osdc_put_request(req);
2534 out_unlock:
2535         up_write(&osdc->lock);
2536 }
2537
2538 static void send_map_check(struct ceph_osd_request *req)
2539 {
2540         struct ceph_osd_client *osdc = req->r_osdc;
2541         struct ceph_osd_request *lookup_req;
2542         int ret;
2543
2544         verify_osdc_wrlocked(osdc);
2545
2546         lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
2547         if (lookup_req) {
2548                 WARN_ON(lookup_req != req);
2549                 return;
2550         }
2551
2552         ceph_osdc_get_request(req);
2553         insert_request_mc(&osdc->map_checks, req);
2554         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
2555                                           map_check_cb, req->r_tid);
2556         WARN_ON(ret);
2557 }
2558
2559 /*
2560  * lingering requests, watch/notify v2 infrastructure
2561  */
2562 static void linger_release(struct kref *kref)
2563 {
2564         struct ceph_osd_linger_request *lreq =
2565             container_of(kref, struct ceph_osd_linger_request, kref);
2566
2567         dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
2568              lreq->reg_req, lreq->ping_req);
2569         WARN_ON(!RB_EMPTY_NODE(&lreq->node));
2570         WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
2571         WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
2572         WARN_ON(!list_empty(&lreq->scan_item));
2573         WARN_ON(!list_empty(&lreq->pending_lworks));
2574         WARN_ON(lreq->osd);
2575
2576         if (lreq->reg_req)
2577                 ceph_osdc_put_request(lreq->reg_req);
2578         if (lreq->ping_req)
2579                 ceph_osdc_put_request(lreq->ping_req);
2580         target_destroy(&lreq->t);
2581         kfree(lreq);
2582 }
2583
2584 static void linger_put(struct ceph_osd_linger_request *lreq)
2585 {
2586         if (lreq)
2587                 kref_put(&lreq->kref, linger_release);
2588 }
2589
2590 static struct ceph_osd_linger_request *
2591 linger_get(struct ceph_osd_linger_request *lreq)
2592 {
2593         kref_get(&lreq->kref);
2594         return lreq;
2595 }
2596
2597 static struct ceph_osd_linger_request *
2598 linger_alloc(struct ceph_osd_client *osdc)
2599 {
2600         struct ceph_osd_linger_request *lreq;
2601
2602         lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
2603         if (!lreq)
2604                 return NULL;
2605
2606         kref_init(&lreq->kref);
2607         mutex_init(&lreq->lock);
2608         RB_CLEAR_NODE(&lreq->node);
2609         RB_CLEAR_NODE(&lreq->osdc_node);
2610         RB_CLEAR_NODE(&lreq->mc_node);
2611         INIT_LIST_HEAD(&lreq->scan_item);
2612         INIT_LIST_HEAD(&lreq->pending_lworks);
2613         init_completion(&lreq->reg_commit_wait);
2614         init_completion(&lreq->notify_finish_wait);
2615
2616         lreq->osdc = osdc;
2617         target_init(&lreq->t);
2618
2619         dout("%s lreq %p\n", __func__, lreq);
2620         return lreq;
2621 }
2622
2623 DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
2624 DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
2625 DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
2626
2627 /*
2628  * Create linger request <-> OSD session relation.
2629  *
2630  * @lreq has to be registered, @osd may be homeless.
2631  */
2632 static void link_linger(struct ceph_osd *osd,
2633                         struct ceph_osd_linger_request *lreq)
2634 {
2635         verify_osd_locked(osd);
2636         WARN_ON(!lreq->linger_id || lreq->osd);
2637         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2638              osd->o_osd, lreq, lreq->linger_id);
2639
2640         if (!osd_homeless(osd))
2641                 __remove_osd_from_lru(osd);
2642         else
2643                 atomic_inc(&osd->o_osdc->num_homeless);
2644
2645         get_osd(osd);
2646         insert_linger(&osd->o_linger_requests, lreq);
2647         lreq->osd = osd;
2648 }
2649
2650 static void unlink_linger(struct ceph_osd *osd,
2651                           struct ceph_osd_linger_request *lreq)
2652 {
2653         verify_osd_locked(osd);
2654         WARN_ON(lreq->osd != osd);
2655         dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
2656              osd->o_osd, lreq, lreq->linger_id);
2657
2658         lreq->osd = NULL;
2659         erase_linger(&osd->o_linger_requests, lreq);
2660         put_osd(osd);
2661
2662         if (!osd_homeless(osd))
2663                 maybe_move_osd_to_lru(osd);
2664         else
2665                 atomic_dec(&osd->o_osdc->num_homeless);
2666 }
2667
2668 static bool __linger_registered(struct ceph_osd_linger_request *lreq)
2669 {
2670         verify_osdc_locked(lreq->osdc);
2671
2672         return !RB_EMPTY_NODE(&lreq->osdc_node);
2673 }
2674
2675 static bool linger_registered(struct ceph_osd_linger_request *lreq)
2676 {
2677         struct ceph_osd_client *osdc = lreq->osdc;
2678         bool registered;
2679
2680         down_read(&osdc->lock);
2681         registered = __linger_registered(lreq);
2682         up_read(&osdc->lock);
2683
2684         return registered;
2685 }
2686
2687 static void linger_register(struct ceph_osd_linger_request *lreq)
2688 {
2689         struct ceph_osd_client *osdc = lreq->osdc;
2690
2691         verify_osdc_wrlocked(osdc);
2692         WARN_ON(lreq->linger_id);
2693
2694         linger_get(lreq);
2695         lreq->linger_id = ++osdc->last_linger_id;
2696         insert_linger_osdc(&osdc->linger_requests, lreq);
2697 }
2698
2699 static void linger_unregister(struct ceph_osd_linger_request *lreq)
2700 {
2701         struct ceph_osd_client *osdc = lreq->osdc;
2702
2703         verify_osdc_wrlocked(osdc);
2704
2705         erase_linger_osdc(&osdc->linger_requests, lreq);
2706         linger_put(lreq);
2707 }
2708
2709 static void cancel_linger_request(struct ceph_osd_request *req)
2710 {
2711         struct ceph_osd_linger_request *lreq = req->r_priv;
2712
2713         WARN_ON(!req->r_linger);
2714         cancel_request(req);
2715         linger_put(lreq);
2716 }
2717
2718 struct linger_work {
2719         struct work_struct work;
2720         struct ceph_osd_linger_request *lreq;
2721         struct list_head pending_item;
2722         unsigned long queued_stamp;
2723
2724         union {
2725                 struct {
2726                         u64 notify_id;
2727                         u64 notifier_id;
2728                         void *payload; /* points into @msg front */
2729                         size_t payload_len;
2730
2731                         struct ceph_msg *msg; /* for ceph_msg_put() */
2732                 } notify;
2733                 struct {
2734                         int err;
2735                 } error;
2736         };
2737 };
2738
2739 static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
2740                                        work_func_t workfn)
2741 {
2742         struct linger_work *lwork;
2743
2744         lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
2745         if (!lwork)
2746                 return NULL;
2747
2748         INIT_WORK(&lwork->work, workfn);
2749         INIT_LIST_HEAD(&lwork->pending_item);
2750         lwork->lreq = linger_get(lreq);
2751
2752         return lwork;
2753 }
2754
2755 static void lwork_free(struct linger_work *lwork)
2756 {
2757         struct ceph_osd_linger_request *lreq = lwork->lreq;
2758
2759         mutex_lock(&lreq->lock);
2760         list_del(&lwork->pending_item);
2761         mutex_unlock(&lreq->lock);
2762
2763         linger_put(lreq);
2764         kfree(lwork);
2765 }
2766
2767 static void lwork_queue(struct linger_work *lwork)
2768 {
2769         struct ceph_osd_linger_request *lreq = lwork->lreq;
2770         struct ceph_osd_client *osdc = lreq->osdc;
2771
2772         verify_lreq_locked(lreq);
2773         WARN_ON(!list_empty(&lwork->pending_item));
2774
2775         lwork->queued_stamp = jiffies;
2776         list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
2777         queue_work(osdc->notify_wq, &lwork->work);
2778 }
2779
2780 static void do_watch_notify(struct work_struct *w)
2781 {
2782         struct linger_work *lwork = container_of(w, struct linger_work, work);
2783         struct ceph_osd_linger_request *lreq = lwork->lreq;
2784
2785         if (!linger_registered(lreq)) {
2786                 dout("%s lreq %p not registered\n", __func__, lreq);
2787                 goto out;
2788         }
2789
2790         WARN_ON(!lreq->is_watch);
2791         dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
2792              __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
2793              lwork->notify.payload_len);
2794         lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
2795                   lwork->notify.notifier_id, lwork->notify.payload,
2796                   lwork->notify.payload_len);
2797
2798 out:
2799         ceph_msg_put(lwork->notify.msg);
2800         lwork_free(lwork);
2801 }
2802
2803 static void do_watch_error(struct work_struct *w)
2804 {
2805         struct linger_work *lwork = container_of(w, struct linger_work, work);
2806         struct ceph_osd_linger_request *lreq = lwork->lreq;
2807
2808         if (!linger_registered(lreq)) {
2809                 dout("%s lreq %p not registered\n", __func__, lreq);
2810                 goto out;
2811         }
2812
2813         dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
2814         lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
2815
2816 out:
2817         lwork_free(lwork);
2818 }
2819
2820 static void queue_watch_error(struct ceph_osd_linger_request *lreq)
2821 {
2822         struct linger_work *lwork;
2823
2824         lwork = lwork_alloc(lreq, do_watch_error);
2825         if (!lwork) {
2826                 pr_err("failed to allocate error-lwork\n");
2827                 return;
2828         }
2829
2830         lwork->error.err = lreq->last_error;
2831         lwork_queue(lwork);
2832 }
2833
2834 static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
2835                                        int result)
2836 {
2837         if (!completion_done(&lreq->reg_commit_wait)) {
2838                 lreq->reg_commit_error = (result <= 0 ? result : 0);
2839                 complete_all(&lreq->reg_commit_wait);
2840         }
2841 }
2842
2843 static void linger_commit_cb(struct ceph_osd_request *req)
2844 {
2845         struct ceph_osd_linger_request *lreq = req->r_priv;
2846
2847         mutex_lock(&lreq->lock);
2848         dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
2849              lreq->linger_id, req->r_result);
2850         linger_reg_commit_complete(lreq, req->r_result);
2851         lreq->committed = true;
2852
2853         if (!lreq->is_watch) {
2854                 struct ceph_osd_data *osd_data =
2855                     osd_req_op_data(req, 0, notify, response_data);
2856                 void *p = page_address(osd_data->pages[0]);
2857
2858                 WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
2859                         osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
2860
2861                 /* make note of the notify_id */
2862                 if (req->r_ops[0].outdata_len >= sizeof(u64)) {
2863                         lreq->notify_id = ceph_decode_64(&p);
2864                         dout("lreq %p notify_id %llu\n", lreq,
2865                              lreq->notify_id);
2866                 } else {
2867                         dout("lreq %p no notify_id\n", lreq);
2868                 }
2869         }
2870
2871         mutex_unlock(&lreq->lock);
2872         linger_put(lreq);
2873 }
2874
2875 static int normalize_watch_error(int err)
2876 {
2877         /*
2878          * Translate ENOENT -> ENOTCONN so that a delete->disconnection
2879          * notification and a failure to reconnect because we raced with
2880          * the delete appear the same to the user.
2881          */
2882         if (err == -ENOENT)
2883                 err = -ENOTCONN;
2884
2885         return err;
2886 }
2887
2888 static void linger_reconnect_cb(struct ceph_osd_request *req)
2889 {
2890         struct ceph_osd_linger_request *lreq = req->r_priv;
2891
2892         mutex_lock(&lreq->lock);
2893         dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
2894              lreq, lreq->linger_id, req->r_result, lreq->last_error);
2895         if (req->r_result < 0) {
2896                 if (!lreq->last_error) {
2897                         lreq->last_error = normalize_watch_error(req->r_result);
2898                         queue_watch_error(lreq);
2899                 }
2900         }
2901
2902         mutex_unlock(&lreq->lock);
2903         linger_put(lreq);
2904 }
2905
2906 static void send_linger(struct ceph_osd_linger_request *lreq)
2907 {
2908         struct ceph_osd_request *req = lreq->reg_req;
2909         struct ceph_osd_req_op *op = &req->r_ops[0];
2910
2911         verify_osdc_wrlocked(req->r_osdc);
2912         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
2913
2914         if (req->r_osd)
2915                 cancel_linger_request(req);
2916
2917         request_reinit(req);
2918         ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
2919         ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
2920         req->r_flags = lreq->t.flags;
2921         req->r_mtime = lreq->mtime;
2922
2923         mutex_lock(&lreq->lock);
2924         if (lreq->is_watch && lreq->committed) {
2925                 WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2926                         op->watch.cookie != lreq->linger_id);
2927                 op->watch.op = CEPH_OSD_WATCH_OP_RECONNECT;
2928                 op->watch.gen = ++lreq->register_gen;
2929                 dout("lreq %p reconnect register_gen %u\n", lreq,
2930                      op->watch.gen);
2931                 req->r_callback = linger_reconnect_cb;
2932         } else {
2933                 if (!lreq->is_watch)
2934                         lreq->notify_id = 0;
2935                 else
2936                         WARN_ON(op->watch.op != CEPH_OSD_WATCH_OP_WATCH);
2937                 dout("lreq %p register\n", lreq);
2938                 req->r_callback = linger_commit_cb;
2939         }
2940         mutex_unlock(&lreq->lock);
2941
2942         req->r_priv = linger_get(lreq);
2943         req->r_linger = true;
2944
2945         submit_request(req, true);
2946 }
2947
2948 static void linger_ping_cb(struct ceph_osd_request *req)
2949 {
2950         struct ceph_osd_linger_request *lreq = req->r_priv;
2951
2952         mutex_lock(&lreq->lock);
2953         dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
2954              __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
2955              lreq->last_error);
2956         if (lreq->register_gen == req->r_ops[0].watch.gen) {
2957                 if (!req->r_result) {
2958                         lreq->watch_valid_thru = lreq->ping_sent;
2959                 } else if (!lreq->last_error) {
2960                         lreq->last_error = normalize_watch_error(req->r_result);
2961                         queue_watch_error(lreq);
2962                 }
2963         } else {
2964                 dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
2965                      lreq->register_gen, req->r_ops[0].watch.gen);
2966         }
2967
2968         mutex_unlock(&lreq->lock);
2969         linger_put(lreq);
2970 }
2971
2972 static void send_linger_ping(struct ceph_osd_linger_request *lreq)
2973 {
2974         struct ceph_osd_client *osdc = lreq->osdc;
2975         struct ceph_osd_request *req = lreq->ping_req;
2976         struct ceph_osd_req_op *op = &req->r_ops[0];
2977
2978         if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
2979                 dout("%s PAUSERD\n", __func__);
2980                 return;
2981         }
2982
2983         lreq->ping_sent = jiffies;
2984         dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
2985              __func__, lreq, lreq->linger_id, lreq->ping_sent,
2986              lreq->register_gen);
2987
2988         if (req->r_osd)
2989                 cancel_linger_request(req);
2990
2991         request_reinit(req);
2992         target_copy(&req->r_t, &lreq->t);
2993
2994         WARN_ON(op->op != CEPH_OSD_OP_WATCH ||
2995                 op->watch.cookie != lreq->linger_id ||
2996                 op->watch.op != CEPH_OSD_WATCH_OP_PING);
2997         op->watch.gen = lreq->register_gen;
2998         req->r_callback = linger_ping_cb;
2999         req->r_priv = linger_get(lreq);
3000         req->r_linger = true;
3001
3002         ceph_osdc_get_request(req);
3003         account_request(req);
3004         req->r_tid = atomic64_inc_return(&osdc->last_tid);
3005         link_request(lreq->osd, req);
3006         send_request(req);
3007 }
3008
3009 static void linger_submit(struct ceph_osd_linger_request *lreq)
3010 {
3011         struct ceph_osd_client *osdc = lreq->osdc;
3012         struct ceph_osd *osd;
3013
3014         calc_target(osdc, &lreq->t, NULL, false);
3015         osd = lookup_create_osd(osdc, lreq->t.osd, true);
3016         link_linger(osd, lreq);
3017
3018         send_linger(lreq);
3019 }
3020
3021 static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
3022 {
3023         struct ceph_osd_client *osdc = lreq->osdc;
3024         struct ceph_osd_linger_request *lookup_lreq;
3025
3026         verify_osdc_wrlocked(osdc);
3027
3028         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3029                                        lreq->linger_id);
3030         if (!lookup_lreq)
3031                 return;
3032
3033         WARN_ON(lookup_lreq != lreq);
3034         erase_linger_mc(&osdc->linger_map_checks, lreq);
3035         linger_put(lreq);
3036 }
3037
3038 /*
3039  * @lreq has to be both registered and linked.
3040  */
3041 static void __linger_cancel(struct ceph_osd_linger_request *lreq)
3042 {
3043         if (lreq->is_watch && lreq->ping_req->r_osd)
3044                 cancel_linger_request(lreq->ping_req);
3045         if (lreq->reg_req->r_osd)
3046                 cancel_linger_request(lreq->reg_req);
3047         cancel_linger_map_check(lreq);
3048         unlink_linger(lreq->osd, lreq);
3049         linger_unregister(lreq);
3050 }
3051
3052 static void linger_cancel(struct ceph_osd_linger_request *lreq)
3053 {
3054         struct ceph_osd_client *osdc = lreq->osdc;
3055
3056         down_write(&osdc->lock);
3057         if (__linger_registered(lreq))
3058                 __linger_cancel(lreq);
3059         up_write(&osdc->lock);
3060 }
3061
3062 static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
3063
3064 static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
3065 {
3066         struct ceph_osd_client *osdc = lreq->osdc;
3067         struct ceph_osdmap *map = osdc->osdmap;
3068
3069         verify_osdc_wrlocked(osdc);
3070         WARN_ON(!map->epoch);
3071
3072         if (lreq->register_gen) {
3073                 lreq->map_dne_bound = map->epoch;
3074                 dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
3075                      lreq, lreq->linger_id);
3076         } else {
3077                 dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
3078                      __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3079                      map->epoch);
3080         }
3081
3082         if (lreq->map_dne_bound) {
3083                 if (map->epoch >= lreq->map_dne_bound) {
3084                         /* we had a new enough map */
3085                         pr_info("linger_id %llu pool does not exist\n",
3086                                 lreq->linger_id);
3087                         linger_reg_commit_complete(lreq, -ENOENT);
3088                         __linger_cancel(lreq);
3089                 }
3090         } else {
3091                 send_linger_map_check(lreq);
3092         }
3093 }
3094
3095 static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
3096 {
3097         struct ceph_osd_client *osdc = &greq->monc->client->osdc;
3098         struct ceph_osd_linger_request *lreq;
3099         u64 linger_id = greq->private_data;
3100
3101         WARN_ON(greq->result || !greq->u.newest);
3102
3103         down_write(&osdc->lock);
3104         lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
3105         if (!lreq) {
3106                 dout("%s linger_id %llu dne\n", __func__, linger_id);
3107                 goto out_unlock;
3108         }
3109
3110         dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
3111              __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
3112              greq->u.newest);
3113         if (!lreq->map_dne_bound)
3114                 lreq->map_dne_bound = greq->u.newest;
3115         erase_linger_mc(&osdc->linger_map_checks, lreq);
3116         check_linger_pool_dne(lreq);
3117
3118         linger_put(lreq);
3119 out_unlock:
3120         up_write(&osdc->lock);
3121 }
3122
3123 static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
3124 {
3125         struct ceph_osd_client *osdc = lreq->osdc;
3126         struct ceph_osd_linger_request *lookup_lreq;
3127         int ret;
3128
3129         verify_osdc_wrlocked(osdc);
3130
3131         lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
3132                                        lreq->linger_id);
3133         if (lookup_lreq) {
3134                 WARN_ON(lookup_lreq != lreq);
3135                 return;
3136         }
3137
3138         linger_get(lreq);
3139         insert_linger_mc(&osdc->linger_map_checks, lreq);
3140         ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
3141                                           linger_map_check_cb, lreq->linger_id);
3142         WARN_ON(ret);
3143 }
3144
3145 static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
3146 {
3147         int ret;
3148
3149         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3150         ret = wait_for_completion_interruptible(&lreq->reg_commit_wait);
3151         return ret ?: lreq->reg_commit_error;
3152 }
3153
3154 static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq)
3155 {
3156         int ret;
3157
3158         dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
3159         ret = wait_for_completion_interruptible(&lreq->notify_finish_wait);
3160         return ret ?: lreq->notify_finish_error;
3161 }
3162
3163 /*
3164  * Timeout callback, called every N seconds.  When 1 or more OSD
3165  * requests has been active for more than N seconds, we send a keepalive
3166  * (tag + timestamp) to its OSD to ensure any communications channel
3167  * reset is detected.
3168  */
3169 static void handle_timeout(struct work_struct *work)
3170 {
3171         struct ceph_osd_client *osdc =
3172                 container_of(work, struct ceph_osd_client, timeout_work.work);
3173         struct ceph_options *opts = osdc->client->options;
3174         unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
3175         unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
3176         LIST_HEAD(slow_osds);
3177         struct rb_node *n, *p;
3178
3179         dout("%s osdc %p\n", __func__, osdc);
3180         down_write(&osdc->lock);
3181
3182         /*
3183          * ping osds that are a bit slow.  this ensures that if there
3184          * is a break in the TCP connection we will notice, and reopen
3185          * a connection with that osd (from the fault callback).
3186          */
3187         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
3188                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3189                 bool found = false;
3190
3191                 for (p = rb_first(&osd->o_requests); p; ) {
3192                         struct ceph_osd_request *req =
3193                             rb_entry(p, struct ceph_osd_request, r_node);
3194
3195                         p = rb_next(p); /* abort_request() */
3196
3197                         if (time_before(req->r_stamp, cutoff)) {
3198                                 dout(" req %p tid %llu on osd%d is laggy\n",
3199                                      req, req->r_tid, osd->o_osd);
3200                                 found = true;
3201                         }
3202                         if (opts->osd_request_timeout &&
3203                             time_before(req->r_start_stamp, expiry_cutoff)) {
3204                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3205                                        req->r_tid, osd->o_osd);
3206                                 abort_request(req, -ETIMEDOUT);
3207                         }
3208                 }
3209                 for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
3210                         struct ceph_osd_linger_request *lreq =
3211                             rb_entry(p, struct ceph_osd_linger_request, node);
3212
3213                         dout(" lreq %p linger_id %llu is served by osd%d\n",
3214                              lreq, lreq->linger_id, osd->o_osd);
3215                         found = true;
3216
3217                         mutex_lock(&lreq->lock);
3218                         if (lreq->is_watch && lreq->committed && !lreq->last_error)
3219                                 send_linger_ping(lreq);
3220                         mutex_unlock(&lreq->lock);
3221                 }
3222
3223                 if (found)
3224                         list_move_tail(&osd->o_keepalive_item, &slow_osds);
3225         }
3226
3227         if (opts->osd_request_timeout) {
3228                 for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
3229                         struct ceph_osd_request *req =
3230                             rb_entry(p, struct ceph_osd_request, r_node);
3231
3232                         p = rb_next(p); /* abort_request() */
3233
3234                         if (time_before(req->r_start_stamp, expiry_cutoff)) {
3235                                 pr_err_ratelimited("tid %llu on osd%d timeout\n",
3236                                        req->r_tid, osdc->homeless_osd.o_osd);
3237                                 abort_request(req, -ETIMEDOUT);
3238                         }
3239                 }
3240         }
3241
3242         if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
3243                 maybe_request_map(osdc);
3244
3245         while (!list_empty(&slow_osds)) {
3246                 struct ceph_osd *osd = list_first_entry(&slow_osds,
3247                                                         struct ceph_osd,
3248                                                         o_keepalive_item);
3249                 list_del_init(&osd->o_keepalive_item);
3250                 ceph_con_keepalive(&osd->o_con);
3251         }
3252
3253         up_write(&osdc->lock);
3254         schedule_delayed_work(&osdc->timeout_work,
3255                               osdc->client->options->osd_keepalive_timeout);
3256 }
3257
3258 static void handle_osds_timeout(struct work_struct *work)
3259 {
3260         struct ceph_osd_client *osdc =
3261                 container_of(work, struct ceph_osd_client,
3262                              osds_timeout_work.work);
3263         unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
3264         struct ceph_osd *osd, *nosd;
3265
3266         dout("%s osdc %p\n", __func__, osdc);
3267         down_write(&osdc->lock);
3268         list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
3269                 if (time_before(jiffies, osd->lru_ttl))
3270                         break;
3271
3272                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
3273                 WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
3274                 close_osd(osd);
3275         }
3276
3277         up_write(&osdc->lock);
3278         schedule_delayed_work(&osdc->osds_timeout_work,
3279                               round_jiffies_relative(delay));
3280 }
3281
3282 static int ceph_oloc_decode(void **p, void *end,
3283                             struct ceph_object_locator *oloc)
3284 {
3285         u8 struct_v, struct_cv;
3286         u32 len;
3287         void *struct_end;
3288         int ret = 0;
3289
3290         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3291         struct_v = ceph_decode_8(p);
3292         struct_cv = ceph_decode_8(p);
3293         if (struct_v < 3) {
3294                 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
3295                         struct_v, struct_cv);
3296                 goto e_inval;
3297         }
3298         if (struct_cv > 6) {
3299                 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
3300                         struct_v, struct_cv);
3301                 goto e_inval;
3302         }
3303         len = ceph_decode_32(p);
3304         ceph_decode_need(p, end, len, e_inval);
3305         struct_end = *p + len;
3306
3307         oloc->pool = ceph_decode_64(p);
3308         *p += 4; /* skip preferred */
3309
3310         len = ceph_decode_32(p);
3311         if (len > 0) {
3312                 pr_warn("ceph_object_locator::key is set\n");
3313                 goto e_inval;
3314         }
3315
3316         if (struct_v >= 5) {
3317                 bool changed = false;
3318
3319                 len = ceph_decode_32(p);
3320                 if (len > 0) {
3321                         ceph_decode_need(p, end, len, e_inval);
3322                         if (!oloc->pool_ns ||
3323                             ceph_compare_string(oloc->pool_ns, *p, len))
3324                                 changed = true;
3325                         *p += len;
3326                 } else {
3327                         if (oloc->pool_ns)
3328                                 changed = true;
3329                 }
3330                 if (changed) {
3331                         /* redirect changes namespace */
3332                         pr_warn("ceph_object_locator::nspace is changed\n");
3333                         goto e_inval;
3334                 }
3335         }
3336
3337         if (struct_v >= 6) {
3338                 s64 hash = ceph_decode_64(p);
3339                 if (hash != -1) {
3340                         pr_warn("ceph_object_locator::hash is set\n");
3341                         goto e_inval;
3342                 }
3343         }
3344
3345         /* skip the rest */
3346         *p = struct_end;
3347 out:
3348         return ret;
3349
3350 e_inval:
3351         ret = -EINVAL;
3352         goto out;
3353 }
3354
3355 static int ceph_redirect_decode(void **p, void *end,
3356                                 struct ceph_request_redirect *redir)
3357 {
3358         u8 struct_v, struct_cv;
3359         u32 len;
3360         void *struct_end;
3361         int ret;
3362
3363         ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
3364         struct_v = ceph_decode_8(p);
3365         struct_cv = ceph_decode_8(p);
3366         if (struct_cv > 1) {
3367                 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
3368                         struct_v, struct_cv);
3369                 goto e_inval;
3370         }
3371         len = ceph_decode_32(p);
3372         ceph_decode_need(p, end, len, e_inval);
3373         struct_end = *p + len;
3374
3375         ret = ceph_oloc_decode(p, end, &redir->oloc);
3376         if (ret)
3377                 goto out;
3378
3379         len = ceph_decode_32(p);
3380         if (len > 0) {
3381                 pr_warn("ceph_request_redirect::object_name is set\n");
3382                 goto e_inval;
3383         }
3384
3385         len = ceph_decode_32(p);
3386         *p += len; /* skip osd_instructions */
3387
3388         /* skip the rest */
3389         *p = struct_end;
3390 out:
3391         return ret;
3392
3393 e_inval:
3394         ret = -EINVAL;
3395         goto out;
3396 }
3397
3398 struct MOSDOpReply {
3399         struct ceph_pg pgid;
3400         u64 flags;
3401         int result;
3402         u32 epoch;
3403         int num_ops;
3404         u32 outdata_len[CEPH_OSD_MAX_OPS];
3405         s32 rval[CEPH_OSD_MAX_OPS];
3406         int retry_attempt;
3407         struct ceph_eversion replay_version;
3408         u64 user_version;
3409         struct ceph_request_redirect redirect;
3410 };
3411
3412 static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
3413 {
3414         void *p = msg->front.iov_base;
3415         void *const end = p + msg->front.iov_len;
3416         u16 version = le16_to_cpu(msg->hdr.version);
3417         struct ceph_eversion bad_replay_version;
3418         u8 decode_redir;
3419         u32 len;
3420         int ret;
3421         int i;
3422
3423         ceph_decode_32_safe(&p, end, len, e_inval);
3424         ceph_decode_need(&p, end, len, e_inval);
3425         p += len; /* skip oid */
3426
3427         ret = ceph_decode_pgid(&p, end, &m->pgid);
3428         if (ret)
3429                 return ret;
3430
3431         ceph_decode_64_safe(&p, end, m->flags, e_inval);
3432         ceph_decode_32_safe(&p, end, m->result, e_inval);
3433         ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
3434         memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
3435         p += sizeof(bad_replay_version);
3436         ceph_decode_32_safe(&p, end, m->epoch, e_inval);
3437
3438         ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
3439         if (m->num_ops > ARRAY_SIZE(m->outdata_len))
3440                 goto e_inval;
3441
3442         ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
3443                          e_inval);
3444         for (i = 0; i < m->num_ops; i++) {
3445                 struct ceph_osd_op *op = p;
3446
3447                 m->outdata_len[i] = le32_to_cpu(op->payload_len);
3448                 p += sizeof(*op);
3449         }
3450
3451         ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
3452         for (i = 0; i < m->num_ops; i++)
3453                 ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
3454
3455         if (version >= 5) {
3456                 ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
3457                 memcpy(&m->replay_version, p, sizeof(m->replay_version));
3458                 p += sizeof(m->replay_version);
3459                 ceph_decode_64_safe(&p, end, m->user_version, e_inval);
3460         } else {
3461                 m->replay_version = bad_replay_version; /* struct */
3462                 m->user_version = le64_to_cpu(m->replay_version.version);
3463         }
3464
3465         if (version >= 6) {
3466                 if (version >= 7)
3467                         ceph_decode_8_safe(&p, end, decode_redir, e_inval);
3468                 else
3469                         decode_redir = 1;
3470         } else {
3471                 decode_redir = 0;
3472         }
3473
3474         if (decode_redir) {
3475                 ret = ceph_redirect_decode(&p, end, &m->redirect);
3476                 if (ret)
3477                         return ret;
3478         } else {
3479                 ceph_oloc_init(&m->redirect.oloc);
3480         }
3481
3482         return 0;
3483
3484 e_inval:
3485         return -EINVAL;
3486 }
3487
3488 /*
3489  * Handle MOSDOpReply.  Set ->r_result and call the callback if it is
3490  * specified.
3491  */
3492 static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
3493 {
3494         struct ceph_osd_client *osdc = osd->o_osdc;
3495         struct ceph_osd_request *req;
3496         struct MOSDOpReply m;
3497         u64 tid = le64_to_cpu(msg->hdr.tid);
3498         u32 data_len = 0;
3499         int ret;
3500         int i;
3501
3502         dout("%s msg %p tid %llu\n", __func__, msg, tid);
3503
3504         down_read(&osdc->lock);
3505         if (!osd_registered(osd)) {
3506                 dout("%s osd%d unknown\n", __func__, osd->o_osd);
3507                 goto out_unlock_osdc;
3508         }
3509         WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
3510
3511         mutex_lock(&osd->lock);
3512         req = lookup_request(&osd->o_requests, tid);
3513         if (!req) {
3514                 dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
3515                 goto out_unlock_session;
3516         }
3517
3518         m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
3519         ret = decode_MOSDOpReply(msg, &m);
3520         m.redirect.oloc.pool_ns = NULL;
3521         if (ret) {
3522                 pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
3523                        req->r_tid, ret);
3524                 ceph_msg_dump(msg);
3525                 goto fail_request;
3526         }
3527         dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
3528              __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
3529              m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
3530              le64_to_cpu(m.replay_version.version), m.user_version);
3531
3532         if (m.retry_attempt >= 0) {
3533                 if (m.retry_attempt != req->r_attempts - 1) {
3534                         dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
3535                              req, req->r_tid, m.retry_attempt,
3536                              req->r_attempts - 1);
3537                         goto out_unlock_session;
3538                 }
3539         } else {
3540                 WARN_ON(1); /* MOSDOpReply v4 is assumed */
3541         }
3542
3543         if (!ceph_oloc_empty(&m.redirect.oloc)) {
3544                 dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
3545                      m.redirect.oloc.pool);
3546                 unlink_request(osd, req);
3547                 mutex_unlock(&osd->lock);
3548
3549                 /*
3550                  * Not ceph_oloc_copy() - changing pool_ns is not
3551                  * supported.
3552                  */
3553                 req->r_t.target_oloc.pool = m.redirect.oloc.pool;
3554                 req->r_flags |= CEPH_OSD_FLAG_REDIRECTED;
3555                 req->r_tid = 0;
3556                 __submit_request(req, false);
3557                 goto out_unlock_osdc;
3558         }
3559
3560         if (m.num_ops != req->r_num_ops) {
3561                 pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
3562                        req->r_num_ops, req->r_tid);
3563                 goto fail_request;
3564         }
3565         for (i = 0; i < req->r_num_ops; i++) {
3566                 dout(" req %p tid %llu op %d rval %d len %u\n", req,
3567                      req->r_tid, i, m.rval[i], m.outdata_len[i]);
3568                 req->r_ops[i].rval = m.rval[i];
3569                 req->r_ops[i].outdata_len = m.outdata_len[i];
3570                 data_len += m.outdata_len[i];
3571         }
3572         if (data_len != le32_to_cpu(msg->hdr.data_len)) {
3573                 pr_err("sum of lens %u != %u for tid %llu\n", data_len,
3574                        le32_to_cpu(msg->hdr.data_len), req->r_tid);
3575                 goto fail_request;
3576         }
3577         dout("%s req %p tid %llu result %d data_len %u\n", __func__,
3578              req, req->r_tid, m.result, data_len);
3579
3580         /*
3581          * Since we only ever request ONDISK, we should only ever get
3582          * one (type of) reply back.
3583          */
3584         WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
3585         req->r_result = m.result ?: data_len;
3586         finish_request(req);
3587         mutex_unlock(&osd->lock);
3588         up_read(&osdc->lock);
3589
3590         __complete_request(req);
3591         return;
3592
3593 fail_request:
3594         complete_request(req, -EIO);
3595 out_unlock_session:
3596         mutex_unlock(&osd->lock);
3597 out_unlock_osdc:
3598         up_read(&osdc->lock);
3599 }
3600
3601 static void set_pool_was_full(struct ceph_osd_client *osdc)
3602 {
3603         struct rb_node *n;
3604
3605         for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
3606                 struct ceph_pg_pool_info *pi =
3607                     rb_entry(n, struct ceph_pg_pool_info, node);
3608
3609                 pi->was_full = __pool_full(pi);
3610         }
3611 }
3612
3613 static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
3614 {
3615         struct ceph_pg_pool_info *pi;
3616
3617         pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
3618         if (!pi)
3619                 return false;
3620
3621         return pi->was_full && !__pool_full(pi);
3622 }
3623
3624 static enum calc_target_result
3625 recalc_linger_target(struct ceph_osd_linger_request *lreq)
3626 {
3627         struct ceph_osd_client *osdc = lreq->osdc;
3628         enum calc_target_result ct_res;
3629
3630         ct_res = calc_target(osdc, &lreq->t, NULL, true);
3631         if (ct_res == CALC_TARGET_NEED_RESEND) {
3632                 struct ceph_osd *osd;
3633
3634                 osd = lookup_create_osd(osdc, lreq->t.osd, true);
3635                 if (osd != lreq->osd) {
3636                         unlink_linger(lreq->osd, lreq);
3637                         link_linger(osd, lreq);
3638                 }
3639         }
3640
3641         return ct_res;
3642 }
3643
3644 /*
3645  * Requeue requests whose mapping to an OSD has changed.
3646  */
3647 static void scan_requests(struct ceph_osd *osd,
3648                           bool force_resend,
3649                           bool cleared_full,
3650                           bool check_pool_cleared_full,
3651                           struct rb_root *need_resend,
3652                           struct list_head *need_resend_linger)
3653 {
3654         struct ceph_osd_client *osdc = osd->o_osdc;
3655         struct rb_node *n;
3656         bool force_resend_writes;
3657
3658         for (n = rb_first(&osd->o_linger_requests); n; ) {
3659                 struct ceph_osd_linger_request *lreq =
3660                     rb_entry(n, struct ceph_osd_linger_request, node);
3661                 enum calc_target_result ct_res;
3662
3663                 n = rb_next(n); /* recalc_linger_target() */
3664
3665                 dout("%s lreq %p linger_id %llu\n", __func__, lreq,
3666                      lreq->linger_id);
3667                 ct_res = recalc_linger_target(lreq);
3668                 switch (ct_res) {
3669                 case CALC_TARGET_NO_ACTION:
3670                         force_resend_writes = cleared_full ||
3671                             (check_pool_cleared_full &&
3672                              pool_cleared_full(osdc, lreq->t.base_oloc.pool));
3673                         if (!force_resend && !force_resend_writes)
3674                                 break;
3675
3676                         /* fall through */
3677                 case CALC_TARGET_NEED_RESEND:
3678                         cancel_linger_map_check(lreq);
3679                         /*
3680                          * scan_requests() for the previous epoch(s)
3681                          * may have already added it to the list, since
3682                          * it's not unlinked here.
3683                          */
3684                         if (list_empty(&lreq->scan_item))
3685                                 list_add_tail(&lreq->scan_item, need_resend_linger);
3686                         break;
3687                 case CALC_TARGET_POOL_DNE:
3688                         list_del_init(&lreq->scan_item);
3689                         check_linger_pool_dne(lreq);
3690                         break;
3691                 }
3692         }
3693
3694         for (n = rb_first(&osd->o_requests); n; ) {
3695                 struct ceph_osd_request *req =
3696                     rb_entry(n, struct ceph_osd_request, r_node);
3697                 enum calc_target_result ct_res;
3698
3699                 n = rb_next(n); /* unlink_request(), check_pool_dne() */
3700
3701                 dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
3702                 ct_res = calc_target(osdc, &req->r_t, &req->r_osd->o_con,
3703                                      false);
3704                 switch (ct_res) {
3705                 case CALC_TARGET_NO_ACTION:
3706                         force_resend_writes = cleared_full ||
3707                             (check_pool_cleared_full &&
3708                              pool_cleared_full(osdc, req->r_t.base_oloc.pool));
3709                         if (!force_resend &&
3710                             (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
3711                              !force_resend_writes))
3712                                 break;
3713
3714                         /* fall through */
3715                 case CALC_TARGET_NEED_RESEND:
3716                         cancel_map_check(req);
3717                         unlink_request(osd, req);
3718                         insert_request(need_resend, req);
3719                         break;
3720                 case CALC_TARGET_POOL_DNE:
3721                         check_pool_dne(req);
3722                         break;
3723                 }
3724         }
3725 }
3726
3727 static int handle_one_map(struct ceph_osd_client *osdc,
3728                           void *p, void *end, bool incremental,
3729                           struct rb_root *need_resend,
3730                           struct list_head *need_resend_linger)
3731 {
3732         struct ceph_osdmap *newmap;
3733         struct rb_node *n;
3734         bool skipped_map = false;
3735         bool was_full;
3736
3737         was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3738         set_pool_was_full(osdc);
3739
3740         if (incremental)
3741                 newmap = osdmap_apply_incremental(&p, end, osdc->osdmap);
3742         else
3743                 newmap = ceph_osdmap_decode(&p, end);
3744         if (IS_ERR(newmap))
3745                 return PTR_ERR(newmap);
3746
3747         if (newmap != osdc->osdmap) {
3748                 /*
3749                  * Preserve ->was_full before destroying the old map.
3750                  * For pools that weren't in the old map, ->was_full
3751                  * should be false.
3752                  */
3753                 for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
3754                         struct ceph_pg_pool_info *pi =
3755                             rb_entry(n, struct ceph_pg_pool_info, node);
3756                         struct ceph_pg_pool_info *old_pi;
3757
3758                         old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
3759                         if (old_pi)
3760                                 pi->was_full = old_pi->was_full;
3761                         else
3762                                 WARN_ON(pi->was_full);
3763                 }
3764
3765                 if (osdc->osdmap->epoch &&
3766                     osdc->osdmap->epoch + 1 < newmap->epoch) {
3767                         WARN_ON(incremental);
3768                         skipped_map = true;
3769                 }
3770
3771                 ceph_osdmap_destroy(osdc->osdmap);
3772                 osdc->osdmap = newmap;
3773         }
3774
3775         was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
3776         scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
3777                       need_resend, need_resend_linger);
3778
3779         for (n = rb_first(&osdc->osds); n; ) {
3780                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
3781
3782                 n = rb_next(n); /* close_osd() */
3783
3784                 scan_requests(osd, skipped_map, was_full, true, need_resend,
3785                               need_resend_linger);
3786                 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
3787                     memcmp(&osd->o_con.peer_addr,
3788                            ceph_osd_addr(osdc->osdmap, osd->o_osd),
3789                            sizeof(struct ceph_entity_addr)))
3790                         close_osd(osd);
3791         }
3792
3793         return 0;
3794 }
3795
3796 static void kick_requests(struct ceph_osd_client *osdc,
3797                           struct rb_root *need_resend,
3798                           struct list_head *need_resend_linger)
3799 {
3800         struct ceph_osd_linger_request *lreq, *nlreq;
3801         enum calc_target_result ct_res;
3802         struct rb_node *n;
3803
3804         /* make sure need_resend targets reflect latest map */
3805         for (n = rb_first(need_resend); n; ) {
3806                 struct ceph_osd_request *req =
3807                     rb_entry(n, struct ceph_osd_request, r_node);
3808
3809                 n = rb_next(n);
3810
3811                 if (req->r_t.epoch < osdc->osdmap->epoch) {
3812                         ct_res = calc_target(osdc, &req->r_t, NULL, false);
3813                         if (ct_res == CALC_TARGET_POOL_DNE) {
3814                                 erase_request(need_resend, req);
3815                                 check_pool_dne(req);
3816                         }
3817                 }
3818         }
3819
3820         for (n = rb_first(need_resend); n; ) {
3821                 struct ceph_osd_request *req =
3822                     rb_entry(n, struct ceph_osd_request, r_node);
3823                 struct ceph_osd *osd;
3824
3825                 n = rb_next(n);
3826                 erase_request(need_resend, req); /* before link_request() */
3827
3828                 osd = lookup_create_osd(osdc, req->r_t.osd, true);
3829                 link_request(osd, req);
3830                 if (!req->r_linger) {
3831                         if (!osd_homeless(osd) && !req->r_t.paused)
3832                                 send_request(req);
3833                 } else {
3834                         cancel_linger_request(req);
3835                 }
3836         }
3837
3838         list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
3839                 if (!osd_homeless(lreq->osd))
3840                         send_linger(lreq);
3841
3842                 list_del_init(&lreq->scan_item);
3843         }
3844 }
3845
3846 /*
3847  * Process updated osd map.
3848  *
3849  * The message contains any number of incremental and full maps, normally
3850  * indicating some sort of topology change in the cluster.  Kick requests
3851  * off to different OSDs as needed.
3852  */
3853 void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
3854 {
3855         void *p = msg->front.iov_base;
3856         void *const end = p + msg->front.iov_len;
3857         u32 nr_maps, maplen;
3858         u32 epoch;
3859         struct ceph_fsid fsid;
3860         struct rb_root need_resend = RB_ROOT;
3861         LIST_HEAD(need_resend_linger);
3862         bool handled_incremental = false;
3863         bool was_pauserd, was_pausewr;
3864         bool pauserd, pausewr;
3865         int err;
3866
3867         dout("%s have %u\n", __func__, osdc->osdmap->epoch);
3868         down_write(&osdc->lock);
3869
3870         /* verify fsid */
3871         ceph_decode_need(&p, end, sizeof(fsid), bad);
3872         ceph_decode_copy(&p, &fsid, sizeof(fsid));
3873         if (ceph_check_fsid(osdc->client, &fsid) < 0)
3874                 goto bad;
3875
3876         was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3877         was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3878                       ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3879                       have_pool_full(osdc);
3880
3881         /* incremental maps */
3882         ceph_decode_32_safe(&p, end, nr_maps, bad);
3883         dout(" %d inc maps\n", nr_maps);
3884         while (nr_maps > 0) {
3885                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3886                 epoch = ceph_decode_32(&p);
3887                 maplen = ceph_decode_32(&p);
3888                 ceph_decode_need(&p, end, maplen, bad);
3889                 if (osdc->osdmap->epoch &&
3890                     osdc->osdmap->epoch + 1 == epoch) {
3891                         dout("applying incremental map %u len %d\n",
3892                              epoch, maplen);
3893                         err = handle_one_map(osdc, p, p + maplen, true,
3894                                              &need_resend, &need_resend_linger);
3895                         if (err)
3896                                 goto bad;
3897                         handled_incremental = true;
3898                 } else {
3899                         dout("ignoring incremental map %u len %d\n",
3900                              epoch, maplen);
3901                 }
3902                 p += maplen;
3903                 nr_maps--;
3904         }
3905         if (handled_incremental)
3906                 goto done;
3907
3908         /* full maps */
3909         ceph_decode_32_safe(&p, end, nr_maps, bad);
3910         dout(" %d full maps\n", nr_maps);
3911         while (nr_maps) {
3912                 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
3913                 epoch = ceph_decode_32(&p);
3914                 maplen = ceph_decode_32(&p);
3915                 ceph_decode_need(&p, end, maplen, bad);
3916                 if (nr_maps > 1) {
3917                         dout("skipping non-latest full map %u len %d\n",
3918                              epoch, maplen);
3919                 } else if (osdc->osdmap->epoch >= epoch) {
3920                         dout("skipping full map %u len %d, "
3921                              "older than our %u\n", epoch, maplen,
3922                              osdc->osdmap->epoch);
3923                 } else {
3924                         dout("taking full map %u len %d\n", epoch, maplen);
3925                         err = handle_one_map(osdc, p, p + maplen, false,
3926                                              &need_resend, &need_resend_linger);
3927                         if (err)
3928                                 goto bad;
3929                 }
3930                 p += maplen;
3931                 nr_maps--;
3932         }
3933
3934 done:
3935         /*
3936          * subscribe to subsequent osdmap updates if full to ensure
3937          * we find out when we are no longer full and stop returning
3938          * ENOSPC.
3939          */
3940         pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
3941         pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
3942                   ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
3943                   have_pool_full(osdc);
3944         if (was_pauserd || was_pausewr || pauserd || pausewr ||
3945             osdc->osdmap->epoch < osdc->epoch_barrier)
3946                 maybe_request_map(osdc);
3947
3948         kick_requests(osdc, &need_resend, &need_resend_linger);
3949
3950         ceph_osdc_abort_on_full(osdc);
3951         ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
3952                           osdc->osdmap->epoch);
3953         up_write(&osdc->lock);
3954         wake_up_all(&osdc->client->auth_wq);
3955         return;
3956
3957 bad:
3958         pr_err("osdc handle_map corrupt msg\n");
3959         ceph_msg_dump(msg);
3960         up_write(&osdc->lock);
3961 }
3962
3963 /*
3964  * Resubmit requests pending on the given osd.
3965  */
3966 static void kick_osd_requests(struct ceph_osd *osd)
3967 {
3968         struct rb_node *n;
3969
3970         clear_backoffs(osd);
3971
3972         for (n = rb_first(&osd->o_requests); n; ) {
3973                 struct ceph_osd_request *req =
3974                     rb_entry(n, struct ceph_osd_request, r_node);
3975
3976                 n = rb_next(n); /* cancel_linger_request() */
3977
3978                 if (!req->r_linger) {
3979                         if (!req->r_t.paused)
3980                                 send_request(req);
3981                 } else {
3982                         cancel_linger_request(req);
3983                 }
3984         }
3985         for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
3986                 struct ceph_osd_linger_request *lreq =
3987                     rb_entry(n, struct ceph_osd_linger_request, node);
3988
3989                 send_linger(lreq);
3990         }
3991 }
3992
3993 /*
3994  * If the osd connection drops, we need to resubmit all requests.
3995  */
3996 static void osd_fault(struct ceph_connection *con)
3997 {
3998         struct ceph_osd *osd = con->private;
3999         struct ceph_osd_client *osdc = osd->o_osdc;
4000
4001         dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
4002
4003         down_write(&osdc->lock);
4004         if (!osd_registered(osd)) {
4005                 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4006                 goto out_unlock;
4007         }
4008
4009         if (!reopen_osd(osd))
4010                 kick_osd_requests(osd);
4011         maybe_request_map(osdc);
4012
4013 out_unlock:
4014         up_write(&osdc->lock);
4015 }
4016
4017 struct MOSDBackoff {
4018         struct ceph_spg spgid;
4019         u32 map_epoch;
4020         u8 op;
4021         u64 id;
4022         struct ceph_hobject_id *begin;
4023         struct ceph_hobject_id *end;
4024 };
4025
4026 static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
4027 {
4028         void *p = msg->front.iov_base;
4029         void *const end = p + msg->front.iov_len;
4030         u8 struct_v;
4031         u32 struct_len;
4032         int ret;
4033
4034         ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
4035         if (ret)
4036                 return ret;
4037
4038         ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
4039         if (ret)
4040                 return ret;
4041
4042         ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
4043         ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
4044         ceph_decode_8_safe(&p, end, m->op, e_inval);
4045         ceph_decode_64_safe(&p, end, m->id, e_inval);
4046
4047         m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
4048         if (!m->begin)
4049                 return -ENOMEM;
4050
4051         ret = decode_hoid(&p, end, m->begin);
4052         if (ret) {
4053                 free_hoid(m->begin);
4054                 return ret;
4055         }
4056
4057         m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
4058         if (!m->end) {
4059                 free_hoid(m->begin);
4060                 return -ENOMEM;
4061         }
4062
4063         ret = decode_hoid(&p, end, m->end);
4064         if (ret) {
4065                 free_hoid(m->begin);
4066                 free_hoid(m->end);
4067                 return ret;
4068         }
4069
4070         return 0;
4071
4072 e_inval:
4073         return -EINVAL;
4074 }
4075
4076 static struct ceph_msg *create_backoff_message(
4077                                 const struct ceph_osd_backoff *backoff,
4078                                 u32 map_epoch)
4079 {
4080         struct ceph_msg *msg;
4081         void *p, *end;
4082         int msg_size;
4083
4084         msg_size = CEPH_ENCODING_START_BLK_LEN +
4085                         CEPH_PGID_ENCODING_LEN + 1; /* spgid */
4086         msg_size += 4 + 1 + 8; /* map_epoch, op, id */
4087         msg_size += CEPH_ENCODING_START_BLK_LEN +
4088                         hoid_encoding_size(backoff->begin);
4089         msg_size += CEPH_ENCODING_START_BLK_LEN +
4090                         hoid_encoding_size(backoff->end);
4091
4092         msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
4093         if (!msg)
4094                 return NULL;
4095
4096         p = msg->front.iov_base;
4097         end = p + msg->front_alloc_len;
4098
4099         encode_spgid(&p, &backoff->spgid);
4100         ceph_encode_32(&p, map_epoch);
4101         ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
4102         ceph_encode_64(&p, backoff->id);
4103         encode_hoid(&p, end, backoff->begin);
4104         encode_hoid(&p, end, backoff->end);
4105         BUG_ON(p != end);
4106
4107         msg->front.iov_len = p - msg->front.iov_base;
4108         msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
4109         msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
4110
4111         return msg;
4112 }
4113
4114 static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
4115 {
4116         struct ceph_spg_mapping *spg;
4117         struct ceph_osd_backoff *backoff;
4118         struct ceph_msg *msg;
4119
4120         dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4121              m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4122
4123         spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
4124         if (!spg) {
4125                 spg = alloc_spg_mapping();
4126                 if (!spg) {
4127                         pr_err("%s failed to allocate spg\n", __func__);
4128                         return;
4129                 }
4130                 spg->spgid = m->spgid; /* struct */
4131                 insert_spg_mapping(&osd->o_backoff_mappings, spg);
4132         }
4133
4134         backoff = alloc_backoff();
4135         if (!backoff) {
4136                 pr_err("%s failed to allocate backoff\n", __func__);
4137                 return;
4138         }
4139         backoff->spgid = m->spgid; /* struct */
4140         backoff->id = m->id;
4141         backoff->begin = m->begin;
4142         m->begin = NULL; /* backoff now owns this */
4143         backoff->end = m->end;
4144         m->end = NULL;   /* ditto */
4145
4146         insert_backoff(&spg->backoffs, backoff);
4147         insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4148
4149         /*
4150          * Ack with original backoff's epoch so that the OSD can
4151          * discard this if there was a PG split.
4152          */
4153         msg = create_backoff_message(backoff, m->map_epoch);
4154         if (!msg) {
4155                 pr_err("%s failed to allocate msg\n", __func__);
4156                 return;
4157         }
4158         ceph_con_send(&osd->o_con, msg);
4159 }
4160
4161 static bool target_contained_by(const struct ceph_osd_request_target *t,
4162                                 const struct ceph_hobject_id *begin,
4163                                 const struct ceph_hobject_id *end)
4164 {
4165         struct ceph_hobject_id hoid;
4166         int cmp;
4167
4168         hoid_fill_from_target(&hoid, t);
4169         cmp = hoid_compare(&hoid, begin);
4170         return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
4171 }
4172
4173 static void handle_backoff_unblock(struct ceph_osd *osd,
4174                                    const struct MOSDBackoff *m)
4175 {
4176         struct ceph_spg_mapping *spg;
4177         struct ceph_osd_backoff *backoff;
4178         struct rb_node *n;
4179
4180         dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
4181              m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
4182
4183         backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
4184         if (!backoff) {
4185                 pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
4186                        __func__, osd->o_osd, m->spgid.pgid.pool,
4187                        m->spgid.pgid.seed, m->spgid.shard, m->id);
4188                 return;
4189         }
4190
4191         if (hoid_compare(backoff->begin, m->begin) &&
4192             hoid_compare(backoff->end, m->end)) {
4193                 pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
4194                        __func__, osd->o_osd, m->spgid.pgid.pool,
4195                        m->spgid.pgid.seed, m->spgid.shard, m->id);
4196                 /* unblock it anyway... */
4197         }
4198
4199         spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
4200         BUG_ON(!spg);
4201
4202         erase_backoff(&spg->backoffs, backoff);
4203         erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
4204         free_backoff(backoff);
4205
4206         if (RB_EMPTY_ROOT(&spg->backoffs)) {
4207                 erase_spg_mapping(&osd->o_backoff_mappings, spg);
4208                 free_spg_mapping(spg);
4209         }
4210
4211         for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
4212                 struct ceph_osd_request *req =
4213                     rb_entry(n, struct ceph_osd_request, r_node);
4214
4215                 if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
4216                         /*
4217                          * Match against @m, not @backoff -- the PG may
4218                          * have split on the OSD.
4219                          */
4220                         if (target_contained_by(&req->r_t, m->begin, m->end)) {
4221                                 /*
4222                                  * If no other installed backoff applies,
4223                                  * resend.
4224                                  */
4225                                 send_request(req);
4226                         }
4227                 }
4228         }
4229 }
4230
4231 static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
4232 {
4233         struct ceph_osd_client *osdc = osd->o_osdc;
4234         struct MOSDBackoff m;
4235         int ret;
4236
4237         down_read(&osdc->lock);
4238         if (!osd_registered(osd)) {
4239                 dout("%s osd%d unknown\n", __func__, osd->o_osd);
4240                 up_read(&osdc->lock);
4241                 return;
4242         }
4243         WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
4244
4245         mutex_lock(&osd->lock);
4246         ret = decode_MOSDBackoff(msg, &m);
4247         if (ret) {
4248                 pr_err("failed to decode MOSDBackoff: %d\n", ret);
4249                 ceph_msg_dump(msg);
4250                 goto out_unlock;
4251         }
4252
4253         switch (m.op) {
4254         case CEPH_OSD_BACKOFF_OP_BLOCK:
4255                 handle_backoff_block(osd, &m);
4256                 break;
4257         case CEPH_OSD_BACKOFF_OP_UNBLOCK:
4258                 handle_backoff_unblock(osd, &m);
4259                 break;
4260         default:
4261                 pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
4262         }
4263
4264         free_hoid(m.begin);
4265         free_hoid(m.end);
4266
4267 out_unlock:
4268         mutex_unlock(&osd->lock);
4269         up_read(&osdc->lock);
4270 }
4271
4272 /*
4273  * Process osd watch notifications
4274  */
4275 static void handle_watch_notify(struct ceph_osd_client *osdc,
4276                                 struct ceph_msg *msg)
4277 {
4278         void *p = msg->front.iov_base;
4279         void *const end = p + msg->front.iov_len;
4280         struct ceph_osd_linger_request *lreq;
4281         struct linger_work *lwork;
4282         u8 proto_ver, opcode;
4283         u64 cookie, notify_id;
4284         u64 notifier_id = 0;
4285         s32 return_code = 0;
4286         void *payload = NULL;
4287         u32 payload_len = 0;
4288
4289         ceph_decode_8_safe(&p, end, proto_ver, bad);
4290         ceph_decode_8_safe(&p, end, opcode, bad);
4291         ceph_decode_64_safe(&p, end, cookie, bad);
4292         p += 8; /* skip ver */
4293         ceph_decode_64_safe(&p, end, notify_id, bad);
4294
4295         if (proto_ver >= 1) {
4296                 ceph_decode_32_safe(&p, end, payload_len, bad);
4297                 ceph_decode_need(&p, end, payload_len, bad);
4298                 payload = p;
4299                 p += payload_len;
4300         }
4301
4302         if (le16_to_cpu(msg->hdr.version) >= 2)
4303                 ceph_decode_32_safe(&p, end, return_code, bad);
4304
4305         if (le16_to_cpu(msg->hdr.version) >= 3)
4306                 ceph_decode_64_safe(&p, end, notifier_id, bad);
4307
4308         down_read(&osdc->lock);
4309         lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
4310         if (!lreq) {
4311                 dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
4312                      cookie);
4313                 goto out_unlock_osdc;
4314         }
4315
4316         mutex_lock(&lreq->lock);
4317         dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
4318              opcode, cookie, lreq, lreq->is_watch);
4319         if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
4320                 if (!lreq->last_error) {
4321                         lreq->last_error = -ENOTCONN;
4322                         queue_watch_error(lreq);
4323                 }
4324         } else if (!lreq->is_watch) {
4325                 /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
4326                 if (lreq->notify_id && lreq->notify_id != notify_id) {
4327                         dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
4328                              lreq->notify_id, notify_id);
4329                 } else if (!completion_done(&lreq->notify_finish_wait)) {
4330                         struct ceph_msg_data *data =
4331                             list_first_entry_or_null(&msg->data,
4332                                                      struct ceph_msg_data,
4333                                                      links);
4334
4335                         if (data) {
4336                                 if (lreq->preply_pages) {
4337                                         WARN_ON(data->type !=
4338                                                         CEPH_MSG_DATA_PAGES);
4339                                         *lreq->preply_pages = data->pages;
4340                                         *lreq->preply_len = data->length;
4341                                 } else {
4342                                         ceph_release_page_vector(data->pages,
4343                                                calc_pages_for(0, data->length));
4344                                 }
4345                         }
4346                         lreq->notify_finish_error = return_code;
4347                         complete_all(&lreq->notify_finish_wait);
4348                 }
4349         } else {
4350                 /* CEPH_WATCH_EVENT_NOTIFY */
4351                 lwork = lwork_alloc(lreq, do_watch_notify);
4352                 if (!lwork) {
4353                         pr_err("failed to allocate notify-lwork\n");
4354                         goto out_unlock_lreq;
4355                 }
4356
4357                 lwork->notify.notify_id = notify_id;
4358                 lwork->notify.notifier_id = notifier_id;
4359                 lwork->notify.payload = payload;
4360                 lwork->notify.payload_len = payload_len;
4361                 lwork->notify.msg = ceph_msg_get(msg);
4362                 lwork_queue(lwork);
4363         }
4364
4365 out_unlock_lreq:
4366         mutex_unlock(&lreq->lock);
4367 out_unlock_osdc:
4368         up_read(&osdc->lock);
4369         return;
4370
4371 bad:
4372         pr_err("osdc handle_watch_notify corrupt msg\n");
4373 }
4374
4375 /*
4376  * Register request, send initial attempt.
4377  */
4378 int ceph_osdc_start_request(struct ceph_osd_client *osdc,
4379                             struct ceph_osd_request *req,
4380                             bool nofail)
4381 {
4382         down_read(&osdc->lock);
4383         submit_request(req, false);
4384         up_read(&osdc->lock);
4385
4386         return 0;
4387 }
4388 EXPORT_SYMBOL(ceph_osdc_start_request);
4389
4390 /*
4391  * Unregister a registered request.  The request is not completed:
4392  * ->r_result isn't set and __complete_request() isn't called.
4393  */
4394 void ceph_osdc_cancel_request(struct ceph_osd_request *req)
4395 {
4396         struct ceph_osd_client *osdc = req->r_osdc;
4397
4398         down_write(&osdc->lock);
4399         if (req->r_osd)
4400                 cancel_request(req);
4401         up_write(&osdc->lock);
4402 }
4403 EXPORT_SYMBOL(ceph_osdc_cancel_request);
4404
4405 /*
4406  * @timeout: in jiffies, 0 means "wait forever"
4407  */
4408 static int wait_request_timeout(struct ceph_osd_request *req,
4409                                 unsigned long timeout)
4410 {
4411         long left;
4412
4413         dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
4414         left = wait_for_completion_killable_timeout(&req->r_completion,
4415                                                 ceph_timeout_jiffies(timeout));
4416         if (left <= 0) {
4417                 left = left ?: -ETIMEDOUT;
4418                 ceph_osdc_cancel_request(req);
4419         } else {
4420                 left = req->r_result; /* completed */
4421         }
4422
4423         return left;
4424 }
4425
4426 /*
4427  * wait for a request to complete
4428  */
4429 int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
4430                            struct ceph_osd_request *req)
4431 {
4432         return wait_request_timeout(req, 0);
4433 }
4434 EXPORT_SYMBOL(ceph_osdc_wait_request);
4435
4436 /*
4437  * sync - wait for all in-flight requests to flush.  avoid starvation.
4438  */
4439 void ceph_osdc_sync(struct ceph_osd_client *osdc)
4440 {
4441         struct rb_node *n, *p;
4442         u64 last_tid = atomic64_read(&osdc->last_tid);
4443
4444 again:
4445         down_read(&osdc->lock);
4446         for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
4447                 struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
4448
4449                 mutex_lock(&osd->lock);
4450                 for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
4451                         struct ceph_osd_request *req =
4452                             rb_entry(p, struct ceph_osd_request, r_node);
4453
4454                         if (req->r_tid > last_tid)
4455                                 break;
4456
4457                         if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
4458                                 continue;
4459
4460                         ceph_osdc_get_request(req);
4461                         mutex_unlock(&osd->lock);
4462                         up_read(&osdc->lock);
4463                         dout("%s waiting on req %p tid %llu last_tid %llu\n",
4464                              __func__, req, req->r_tid, last_tid);
4465                         wait_for_completion(&req->r_completion);
4466                         ceph_osdc_put_request(req);
4467                         goto again;
4468                 }
4469
4470                 mutex_unlock(&osd->lock);
4471         }
4472
4473         up_read(&osdc->lock);
4474         dout("%s done last_tid %llu\n", __func__, last_tid);
4475 }
4476 EXPORT_SYMBOL(ceph_osdc_sync);
4477
4478 static struct ceph_osd_request *
4479 alloc_linger_request(struct ceph_osd_linger_request *lreq)
4480 {
4481         struct ceph_osd_request *req;
4482
4483         req = ceph_osdc_alloc_request(lreq->osdc, NULL, 1, false, GFP_NOIO);
4484         if (!req)
4485                 return NULL;
4486
4487         ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4488         ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4489
4490         if (ceph_osdc_alloc_messages(req, GFP_NOIO)) {
4491                 ceph_osdc_put_request(req);
4492                 return NULL;
4493         }
4494
4495         return req;
4496 }
4497
4498 /*
4499  * Returns a handle, caller owns a ref.
4500  */
4501 struct ceph_osd_linger_request *
4502 ceph_osdc_watch(struct ceph_osd_client *osdc,
4503                 struct ceph_object_id *oid,
4504                 struct ceph_object_locator *oloc,
4505                 rados_watchcb2_t wcb,
4506                 rados_watcherrcb_t errcb,
4507                 void *data)
4508 {
4509         struct ceph_osd_linger_request *lreq;
4510         int ret;
4511
4512         lreq = linger_alloc(osdc);
4513         if (!lreq)
4514                 return ERR_PTR(-ENOMEM);
4515
4516         lreq->is_watch = true;
4517         lreq->wcb = wcb;
4518         lreq->errcb = errcb;
4519         lreq->data = data;
4520         lreq->watch_valid_thru = jiffies;
4521
4522         ceph_oid_copy(&lreq->t.base_oid, oid);
4523         ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4524         lreq->t.flags = CEPH_OSD_FLAG_WRITE;
4525         ktime_get_real_ts(&lreq->mtime);
4526
4527         lreq->reg_req = alloc_linger_request(lreq);
4528         if (!lreq->reg_req) {
4529                 ret = -ENOMEM;
4530                 goto err_put_lreq;
4531         }
4532
4533         lreq->ping_req = alloc_linger_request(lreq);
4534         if (!lreq->ping_req) {
4535                 ret = -ENOMEM;
4536                 goto err_put_lreq;
4537         }
4538
4539         down_write(&osdc->lock);
4540         linger_register(lreq); /* before osd_req_op_* */
4541         osd_req_op_watch_init(lreq->reg_req, 0, lreq->linger_id,
4542                               CEPH_OSD_WATCH_OP_WATCH);
4543         osd_req_op_watch_init(lreq->ping_req, 0, lreq->linger_id,
4544                               CEPH_OSD_WATCH_OP_PING);
4545         linger_submit(lreq);
4546         up_write(&osdc->lock);
4547
4548         ret = linger_reg_commit_wait(lreq);
4549         if (ret) {
4550                 linger_cancel(lreq);
4551                 goto err_put_lreq;
4552         }
4553
4554         return lreq;
4555
4556 err_put_lreq:
4557         linger_put(lreq);
4558         return ERR_PTR(ret);
4559 }
4560 EXPORT_SYMBOL(ceph_osdc_watch);
4561
4562 /*
4563  * Releases a ref.
4564  *
4565  * Times out after mount_timeout to preserve rbd unmap behaviour
4566  * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
4567  * with mount_timeout").
4568  */
4569 int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
4570                       struct ceph_osd_linger_request *lreq)
4571 {
4572         struct ceph_options *opts = osdc->client->options;
4573         struct ceph_osd_request *req;
4574         int ret;
4575
4576         req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4577         if (!req)
4578                 return -ENOMEM;
4579
4580         ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
4581         ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
4582         req->r_flags = CEPH_OSD_FLAG_WRITE;
4583         ktime_get_real_ts(&req->r_mtime);
4584         osd_req_op_watch_init(req, 0, lreq->linger_id,
4585                               CEPH_OSD_WATCH_OP_UNWATCH);
4586
4587         ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4588         if (ret)
4589                 goto out_put_req;
4590
4591         ceph_osdc_start_request(osdc, req, false);
4592         linger_cancel(lreq);
4593         linger_put(lreq);
4594         ret = wait_request_timeout(req, opts->mount_timeout);
4595
4596 out_put_req:
4597         ceph_osdc_put_request(req);
4598         return ret;
4599 }
4600 EXPORT_SYMBOL(ceph_osdc_unwatch);
4601
4602 static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
4603                                       u64 notify_id, u64 cookie, void *payload,
4604                                       size_t payload_len)
4605 {
4606         struct ceph_osd_req_op *op;
4607         struct ceph_pagelist *pl;
4608         int ret;
4609
4610         op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
4611
4612         pl = kmalloc(sizeof(*pl), GFP_NOIO);
4613         if (!pl)
4614                 return -ENOMEM;
4615
4616         ceph_pagelist_init(pl);
4617         ret = ceph_pagelist_encode_64(pl, notify_id);
4618         ret |= ceph_pagelist_encode_64(pl, cookie);
4619         if (payload) {
4620                 ret |= ceph_pagelist_encode_32(pl, payload_len);
4621                 ret |= ceph_pagelist_append(pl, payload, payload_len);
4622         } else {
4623                 ret |= ceph_pagelist_encode_32(pl, 0);
4624         }
4625         if (ret) {
4626                 ceph_pagelist_release(pl);
4627                 return -ENOMEM;
4628         }
4629
4630         ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
4631         op->indata_len = pl->length;
4632         return 0;
4633 }
4634
4635 int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
4636                          struct ceph_object_id *oid,
4637                          struct ceph_object_locator *oloc,
4638                          u64 notify_id,
4639                          u64 cookie,
4640                          void *payload,
4641                          size_t payload_len)
4642 {
4643         struct ceph_osd_request *req;
4644         int ret;
4645
4646         req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4647         if (!req)
4648                 return -ENOMEM;
4649
4650         ceph_oid_copy(&req->r_base_oid, oid);
4651         ceph_oloc_copy(&req->r_base_oloc, oloc);
4652         req->r_flags = CEPH_OSD_FLAG_READ;
4653
4654         ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4655         if (ret)
4656                 goto out_put_req;
4657
4658         ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
4659                                          payload_len);
4660         if (ret)
4661                 goto out_put_req;
4662
4663         ceph_osdc_start_request(osdc, req, false);
4664         ret = ceph_osdc_wait_request(osdc, req);
4665
4666 out_put_req:
4667         ceph_osdc_put_request(req);
4668         return ret;
4669 }
4670 EXPORT_SYMBOL(ceph_osdc_notify_ack);
4671
4672 static int osd_req_op_notify_init(struct ceph_osd_request *req, int which,
4673                                   u64 cookie, u32 prot_ver, u32 timeout,
4674                                   void *payload, size_t payload_len)
4675 {
4676         struct ceph_osd_req_op *op;
4677         struct ceph_pagelist *pl;
4678         int ret;
4679
4680         op = _osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
4681         op->notify.cookie = cookie;
4682
4683         pl = kmalloc(sizeof(*pl), GFP_NOIO);
4684         if (!pl)
4685                 return -ENOMEM;
4686
4687         ceph_pagelist_init(pl);
4688         ret = ceph_pagelist_encode_32(pl, 1); /* prot_ver */
4689         ret |= ceph_pagelist_encode_32(pl, timeout);
4690         ret |= ceph_pagelist_encode_32(pl, payload_len);
4691         ret |= ceph_pagelist_append(pl, payload, payload_len);
4692         if (ret) {
4693                 ceph_pagelist_release(pl);
4694                 return -ENOMEM;
4695         }
4696
4697         ceph_osd_data_pagelist_init(&op->notify.request_data, pl);
4698         op->indata_len = pl->length;
4699         return 0;
4700 }
4701
4702 /*
4703  * @timeout: in seconds
4704  *
4705  * @preply_{pages,len} are initialized both on success and error.
4706  * The caller is responsible for:
4707  *
4708  *     ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
4709  */
4710 int ceph_osdc_notify(struct ceph_osd_client *osdc,
4711                      struct ceph_object_id *oid,
4712                      struct ceph_object_locator *oloc,
4713                      void *payload,
4714                      size_t payload_len,
4715                      u32 timeout,
4716                      struct page ***preply_pages,
4717                      size_t *preply_len)
4718 {
4719         struct ceph_osd_linger_request *lreq;
4720         struct page **pages;
4721         int ret;
4722
4723         WARN_ON(!timeout);
4724         if (preply_pages) {
4725                 *preply_pages = NULL;
4726                 *preply_len = 0;
4727         }
4728
4729         lreq = linger_alloc(osdc);
4730         if (!lreq)
4731                 return -ENOMEM;
4732
4733         lreq->preply_pages = preply_pages;
4734         lreq->preply_len = preply_len;
4735
4736         ceph_oid_copy(&lreq->t.base_oid, oid);
4737         ceph_oloc_copy(&lreq->t.base_oloc, oloc);
4738         lreq->t.flags = CEPH_OSD_FLAG_READ;
4739
4740         lreq->reg_req = alloc_linger_request(lreq);
4741         if (!lreq->reg_req) {
4742                 ret = -ENOMEM;
4743                 goto out_put_lreq;
4744         }
4745
4746         /* for notify_id */
4747         pages = ceph_alloc_page_vector(1, GFP_NOIO);
4748         if (IS_ERR(pages)) {
4749                 ret = PTR_ERR(pages);
4750                 goto out_put_lreq;
4751         }
4752
4753         down_write(&osdc->lock);
4754         linger_register(lreq); /* before osd_req_op_* */
4755         ret = osd_req_op_notify_init(lreq->reg_req, 0, lreq->linger_id, 1,
4756                                      timeout, payload, payload_len);
4757         if (ret) {
4758                 linger_unregister(lreq);
4759                 up_write(&osdc->lock);
4760                 ceph_release_page_vector(pages, 1);
4761                 goto out_put_lreq;
4762         }
4763         ceph_osd_data_pages_init(osd_req_op_data(lreq->reg_req, 0, notify,
4764                                                  response_data),
4765                                  pages, PAGE_SIZE, 0, false, true);
4766         linger_submit(lreq);
4767         up_write(&osdc->lock);
4768
4769         ret = linger_reg_commit_wait(lreq);
4770         if (!ret)
4771                 ret = linger_notify_finish_wait(lreq);
4772         else
4773                 dout("lreq %p failed to initiate notify %d\n", lreq, ret);
4774
4775         linger_cancel(lreq);
4776 out_put_lreq:
4777         linger_put(lreq);
4778         return ret;
4779 }
4780 EXPORT_SYMBOL(ceph_osdc_notify);
4781
4782 /*
4783  * Return the number of milliseconds since the watch was last
4784  * confirmed, or an error.  If there is an error, the watch is no
4785  * longer valid, and should be destroyed with ceph_osdc_unwatch().
4786  */
4787 int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
4788                           struct ceph_osd_linger_request *lreq)
4789 {
4790         unsigned long stamp, age;
4791         int ret;
4792
4793         down_read(&osdc->lock);
4794         mutex_lock(&lreq->lock);
4795         stamp = lreq->watch_valid_thru;
4796         if (!list_empty(&lreq->pending_lworks)) {
4797                 struct linger_work *lwork =
4798                     list_first_entry(&lreq->pending_lworks,
4799                                      struct linger_work,
4800                                      pending_item);
4801
4802                 if (time_before(lwork->queued_stamp, stamp))
4803                         stamp = lwork->queued_stamp;
4804         }
4805         age = jiffies - stamp;
4806         dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
4807              lreq, lreq->linger_id, age, lreq->last_error);
4808         /* we are truncating to msecs, so return a safe upper bound */
4809         ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
4810
4811         mutex_unlock(&lreq->lock);
4812         up_read(&osdc->lock);
4813         return ret;
4814 }
4815
4816 static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
4817 {
4818         u8 struct_v;
4819         u32 struct_len;
4820         int ret;
4821
4822         ret = ceph_start_decoding(p, end, 2, "watch_item_t",
4823                                   &struct_v, &struct_len);
4824         if (ret)
4825                 return ret;
4826
4827         ceph_decode_copy(p, &item->name, sizeof(item->name));
4828         item->cookie = ceph_decode_64(p);
4829         *p += 4; /* skip timeout_seconds */
4830         if (struct_v >= 2) {
4831                 ceph_decode_copy(p, &item->addr, sizeof(item->addr));
4832                 ceph_decode_addr(&item->addr);
4833         }
4834
4835         dout("%s %s%llu cookie %llu addr %s\n", __func__,
4836              ENTITY_NAME(item->name), item->cookie,
4837              ceph_pr_addr(&item->addr.in_addr));
4838         return 0;
4839 }
4840
4841 static int decode_watchers(void **p, void *end,
4842                            struct ceph_watch_item **watchers,
4843                            u32 *num_watchers)
4844 {
4845         u8 struct_v;
4846         u32 struct_len;
4847         int i;
4848         int ret;
4849
4850         ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
4851                                   &struct_v, &struct_len);
4852         if (ret)
4853                 return ret;
4854
4855         *num_watchers = ceph_decode_32(p);
4856         *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
4857         if (!*watchers)
4858                 return -ENOMEM;
4859
4860         for (i = 0; i < *num_watchers; i++) {
4861                 ret = decode_watcher(p, end, *watchers + i);
4862                 if (ret) {
4863                         kfree(*watchers);
4864                         return ret;
4865                 }
4866         }
4867
4868         return 0;
4869 }
4870
4871 /*
4872  * On success, the caller is responsible for:
4873  *
4874  *     kfree(watchers);
4875  */
4876 int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
4877                             struct ceph_object_id *oid,
4878                             struct ceph_object_locator *oloc,
4879                             struct ceph_watch_item **watchers,
4880                             u32 *num_watchers)
4881 {
4882         struct ceph_osd_request *req;
4883         struct page **pages;
4884         int ret;
4885
4886         req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4887         if (!req)
4888                 return -ENOMEM;
4889
4890         ceph_oid_copy(&req->r_base_oid, oid);
4891         ceph_oloc_copy(&req->r_base_oloc, oloc);
4892         req->r_flags = CEPH_OSD_FLAG_READ;
4893
4894         ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4895         if (ret)
4896                 goto out_put_req;
4897
4898         pages = ceph_alloc_page_vector(1, GFP_NOIO);
4899         if (IS_ERR(pages)) {
4900                 ret = PTR_ERR(pages);
4901                 goto out_put_req;
4902         }
4903
4904         osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
4905         ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
4906                                                  response_data),
4907                                  pages, PAGE_SIZE, 0, false, true);
4908
4909         ceph_osdc_start_request(osdc, req, false);
4910         ret = ceph_osdc_wait_request(osdc, req);
4911         if (ret >= 0) {
4912                 void *p = page_address(pages[0]);
4913                 void *const end = p + req->r_ops[0].outdata_len;
4914
4915                 ret = decode_watchers(&p, end, watchers, num_watchers);
4916         }
4917
4918 out_put_req:
4919         ceph_osdc_put_request(req);
4920         return ret;
4921 }
4922 EXPORT_SYMBOL(ceph_osdc_list_watchers);
4923
4924 /*
4925  * Call all pending notify callbacks - for use after a watch is
4926  * unregistered, to make sure no more callbacks for it will be invoked
4927  */
4928 void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
4929 {
4930         dout("%s osdc %p\n", __func__, osdc);
4931         flush_workqueue(osdc->notify_wq);
4932 }
4933 EXPORT_SYMBOL(ceph_osdc_flush_notifies);
4934
4935 void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
4936 {
4937         down_read(&osdc->lock);
4938         maybe_request_map(osdc);
4939         up_read(&osdc->lock);
4940 }
4941 EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
4942
4943 /*
4944  * Execute an OSD class method on an object.
4945  *
4946  * @flags: CEPH_OSD_FLAG_*
4947  * @resp_len: in/out param for reply length
4948  */
4949 int ceph_osdc_call(struct ceph_osd_client *osdc,
4950                    struct ceph_object_id *oid,
4951                    struct ceph_object_locator *oloc,
4952                    const char *class, const char *method,
4953                    unsigned int flags,
4954                    struct page *req_page, size_t req_len,
4955                    struct page *resp_page, size_t *resp_len)
4956 {
4957         struct ceph_osd_request *req;
4958         int ret;
4959
4960         if (req_len > PAGE_SIZE || (resp_page && *resp_len > PAGE_SIZE))
4961                 return -E2BIG;
4962
4963         req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
4964         if (!req)
4965                 return -ENOMEM;
4966
4967         ceph_oid_copy(&req->r_base_oid, oid);
4968         ceph_oloc_copy(&req->r_base_oloc, oloc);
4969         req->r_flags = flags;
4970
4971         ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
4972         if (ret)
4973                 goto out_put_req;
4974
4975         ret = osd_req_op_cls_init(req, 0, CEPH_OSD_OP_CALL, class, method);
4976         if (ret)
4977                 goto out_put_req;
4978
4979         if (req_page)
4980                 osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
4981                                                   0, false, false);
4982         if (resp_page)
4983                 osd_req_op_cls_response_data_pages(req, 0, &resp_page,
4984                                                    *resp_len, 0, false, false);
4985
4986         ceph_osdc_start_request(osdc, req, false);
4987         ret = ceph_osdc_wait_request(osdc, req);
4988         if (ret >= 0) {
4989                 ret = req->r_ops[0].rval;
4990                 if (resp_page)
4991                         *resp_len = req->r_ops[0].outdata_len;
4992         }
4993
4994 out_put_req:
4995         ceph_osdc_put_request(req);
4996         return ret;
4997 }
4998 EXPORT_SYMBOL(ceph_osdc_call);
4999
5000 /*
5001  * init, shutdown
5002  */
5003 int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
5004 {
5005         int err;
5006
5007         dout("init\n");
5008         osdc->client = client;
5009         init_rwsem(&osdc->lock);
5010         osdc->osds = RB_ROOT;
5011         INIT_LIST_HEAD(&osdc->osd_lru);
5012         spin_lock_init(&osdc->osd_lru_lock);
5013         osd_init(&osdc->homeless_osd);
5014         osdc->homeless_osd.o_osdc = osdc;
5015         osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
5016         osdc->last_linger_id = CEPH_LINGER_ID_START;
5017         osdc->linger_requests = RB_ROOT;
5018         osdc->map_checks = RB_ROOT;
5019         osdc->linger_map_checks = RB_ROOT;
5020         INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
5021         INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
5022
5023         err = -ENOMEM;
5024         osdc->osdmap = ceph_osdmap_alloc();
5025         if (!osdc->osdmap)
5026                 goto out;
5027
5028         osdc->req_mempool = mempool_create_slab_pool(10,
5029                                                      ceph_osd_request_cache);
5030         if (!osdc->req_mempool)
5031                 goto out_map;
5032
5033         err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
5034                                 PAGE_SIZE, 10, true, "osd_op");
5035         if (err < 0)
5036                 goto out_mempool;
5037         err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
5038                                 PAGE_SIZE, 10, true, "osd_op_reply");
5039         if (err < 0)
5040                 goto out_msgpool;
5041
5042         err = -ENOMEM;
5043         osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
5044         if (!osdc->notify_wq)
5045                 goto out_msgpool_reply;
5046
5047         osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
5048         if (!osdc->completion_wq)
5049                 goto out_notify_wq;
5050
5051         schedule_delayed_work(&osdc->timeout_work,
5052                               osdc->client->options->osd_keepalive_timeout);
5053         schedule_delayed_work(&osdc->osds_timeout_work,
5054             round_jiffies_relative(osdc->client->options->osd_idle_ttl));
5055
5056         return 0;
5057
5058 out_notify_wq:
5059         destroy_workqueue(osdc->notify_wq);
5060 out_msgpool_reply:
5061         ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5062 out_msgpool:
5063         ceph_msgpool_destroy(&osdc->msgpool_op);
5064 out_mempool:
5065         mempool_destroy(osdc->req_mempool);
5066 out_map:
5067         ceph_osdmap_destroy(osdc->osdmap);
5068 out:
5069         return err;
5070 }
5071
5072 void ceph_osdc_stop(struct ceph_osd_client *osdc)
5073 {
5074         destroy_workqueue(osdc->completion_wq);
5075         destroy_workqueue(osdc->notify_wq);
5076         cancel_delayed_work_sync(&osdc->timeout_work);
5077         cancel_delayed_work_sync(&osdc->osds_timeout_work);
5078
5079         down_write(&osdc->lock);
5080         while (!RB_EMPTY_ROOT(&osdc->osds)) {
5081                 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
5082                                                 struct ceph_osd, o_node);
5083                 close_osd(osd);
5084         }
5085         up_write(&osdc->lock);
5086         WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
5087         osd_cleanup(&osdc->homeless_osd);
5088
5089         WARN_ON(!list_empty(&osdc->osd_lru));
5090         WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
5091         WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
5092         WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
5093         WARN_ON(atomic_read(&osdc->num_requests));
5094         WARN_ON(atomic_read(&osdc->num_homeless));
5095
5096         ceph_osdmap_destroy(osdc->osdmap);
5097         mempool_destroy(osdc->req_mempool);
5098         ceph_msgpool_destroy(&osdc->msgpool_op);
5099         ceph_msgpool_destroy(&osdc->msgpool_op_reply);
5100 }
5101
5102 /*
5103  * Read some contiguous pages.  If we cross a stripe boundary, shorten
5104  * *plen.  Return number of bytes read, or error.
5105  */
5106 int ceph_osdc_readpages(struct ceph_osd_client *osdc,
5107                         struct ceph_vino vino, struct ceph_file_layout *layout,
5108                         u64 off, u64 *plen,
5109                         u32 truncate_seq, u64 truncate_size,
5110                         struct page **pages, int num_pages, int page_align)
5111 {
5112         struct ceph_osd_request *req;
5113         int rc = 0;
5114
5115         dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
5116              vino.snap, off, *plen);
5117         req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
5118                                     CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
5119                                     NULL, truncate_seq, truncate_size,
5120                                     false);
5121         if (IS_ERR(req))
5122                 return PTR_ERR(req);
5123
5124         /* it may be a short read due to an object boundary */
5125         osd_req_op_extent_osd_data_pages(req, 0,
5126                                 pages, *plen, page_align, false, false);
5127
5128         dout("readpages  final extent is %llu~%llu (%llu bytes align %d)\n",
5129              off, *plen, *plen, page_align);
5130
5131         rc = ceph_osdc_start_request(osdc, req, false);
5132         if (!rc)
5133                 rc = ceph_osdc_wait_request(osdc, req);
5134
5135         ceph_osdc_put_request(req);
5136         dout("readpages result %d\n", rc);
5137         return rc;
5138 }
5139 EXPORT_SYMBOL(ceph_osdc_readpages);
5140
5141 /*
5142  * do a synchronous write on N pages
5143  */
5144 int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
5145                          struct ceph_file_layout *layout,
5146                          struct ceph_snap_context *snapc,
5147                          u64 off, u64 len,
5148                          u32 truncate_seq, u64 truncate_size,
5149                          struct timespec *mtime,
5150                          struct page **pages, int num_pages)
5151 {
5152         struct ceph_osd_request *req;
5153         int rc = 0;
5154         int page_align = off & ~PAGE_MASK;
5155
5156         req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
5157                                     CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
5158                                     snapc, truncate_seq, truncate_size,
5159                                     true);
5160         if (IS_ERR(req))
5161                 return PTR_ERR(req);
5162
5163         /* it may be a short write due to an object boundary */
5164         osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
5165                                 false, false);
5166         dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
5167
5168         req->r_mtime = *mtime;
5169         rc = ceph_osdc_start_request(osdc, req, true);
5170         if (!rc)
5171                 rc = ceph_osdc_wait_request(osdc, req);
5172
5173         ceph_osdc_put_request(req);
5174         if (rc == 0)
5175                 rc = len;
5176         dout("writepages result %d\n", rc);
5177         return rc;
5178 }
5179 EXPORT_SYMBOL(ceph_osdc_writepages);
5180
5181 int __init ceph_osdc_setup(void)
5182 {
5183         size_t size = sizeof(struct ceph_osd_request) +
5184             CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
5185
5186         BUG_ON(ceph_osd_request_cache);
5187         ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
5188                                                    0, 0, NULL);
5189
5190         return ceph_osd_request_cache ? 0 : -ENOMEM;
5191 }
5192
5193 void ceph_osdc_cleanup(void)
5194 {
5195         BUG_ON(!ceph_osd_request_cache);
5196         kmem_cache_destroy(ceph_osd_request_cache);
5197         ceph_osd_request_cache = NULL;
5198 }
5199
5200 /*
5201  * handle incoming message
5202  */
5203 static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
5204 {
5205         struct ceph_osd *osd = con->private;
5206         struct ceph_osd_client *osdc = osd->o_osdc;
5207         int type = le16_to_cpu(msg->hdr.type);
5208
5209         switch (type) {
5210         case CEPH_MSG_OSD_MAP:
5211                 ceph_osdc_handle_map(osdc, msg);
5212                 break;
5213         case CEPH_MSG_OSD_OPREPLY:
5214                 handle_reply(osd, msg);
5215                 break;
5216         case CEPH_MSG_OSD_BACKOFF:
5217                 handle_backoff(osd, msg);
5218                 break;
5219         case CEPH_MSG_WATCH_NOTIFY:
5220                 handle_watch_notify(osdc, msg);
5221                 break;
5222
5223         default:
5224                 pr_err("received unknown message type %d %s\n", type,
5225                        ceph_msg_type_name(type));
5226         }
5227
5228         ceph_msg_put(msg);
5229 }
5230
5231 /*
5232  * Lookup and return message for incoming reply.  Don't try to do
5233  * anything about a larger than preallocated data portion of the
5234  * message at the moment - for now, just skip the message.
5235  */
5236 static struct ceph_msg *get_reply(struct ceph_connection *con,
5237                                   struct ceph_msg_header *hdr,
5238                                   int *skip)
5239 {
5240         struct ceph_osd *osd = con->private;
5241         struct ceph_osd_client *osdc = osd->o_osdc;
5242         struct ceph_msg *m = NULL;
5243         struct ceph_osd_request *req;
5244         int front_len = le32_to_cpu(hdr->front_len);
5245         int data_len = le32_to_cpu(hdr->data_len);
5246         u64 tid = le64_to_cpu(hdr->tid);
5247
5248         down_read(&osdc->lock);
5249         if (!osd_registered(osd)) {
5250                 dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
5251                 *skip = 1;
5252                 goto out_unlock_osdc;
5253         }
5254         WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
5255
5256         mutex_lock(&osd->lock);
5257         req = lookup_request(&osd->o_requests, tid);
5258         if (!req) {
5259                 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
5260                      osd->o_osd, tid);
5261                 *skip = 1;
5262                 goto out_unlock_session;
5263         }
5264
5265         ceph_msg_revoke_incoming(req->r_reply);
5266
5267         if (front_len > req->r_reply->front_alloc_len) {
5268                 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
5269                         __func__, osd->o_osd, req->r_tid, front_len,
5270                         req->r_reply->front_alloc_len);
5271                 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
5272                                  false);
5273                 if (!m)
5274                         goto out_unlock_session;
5275                 ceph_msg_put(req->r_reply);
5276                 req->r_reply = m;
5277         }
5278
5279         if (data_len > req->r_reply->data_length) {
5280                 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
5281                         __func__, osd->o_osd, req->r_tid, data_len,
5282                         req->r_reply->data_length);
5283                 m = NULL;
5284                 *skip = 1;
5285                 goto out_unlock_session;
5286         }
5287
5288         m = ceph_msg_get(req->r_reply);
5289         dout("get_reply tid %lld %p\n", tid, m);
5290
5291 out_unlock_session:
5292         mutex_unlock(&osd->lock);
5293 out_unlock_osdc:
5294         up_read(&osdc->lock);
5295         return m;
5296 }
5297
5298 /*
5299  * TODO: switch to a msg-owned pagelist
5300  */
5301 static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
5302 {
5303         struct ceph_msg *m;
5304         int type = le16_to_cpu(hdr->type);
5305         u32 front_len = le32_to_cpu(hdr->front_len);
5306         u32 data_len = le32_to_cpu(hdr->data_len);
5307
5308         m = ceph_msg_new(type, front_len, GFP_NOIO, false);
5309         if (!m)
5310                 return NULL;
5311
5312         if (data_len) {
5313                 struct page **pages;
5314                 struct ceph_osd_data osd_data;
5315
5316                 pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
5317                                                GFP_NOIO);
5318                 if (IS_ERR(pages)) {
5319                         ceph_msg_put(m);
5320                         return NULL;
5321                 }
5322
5323                 ceph_osd_data_pages_init(&osd_data, pages, data_len, 0, false,
5324                                          false);
5325                 ceph_osdc_msg_data_add(m, &osd_data);
5326         }
5327
5328         return m;
5329 }
5330
5331 static struct ceph_msg *alloc_msg(struct ceph_connection *con,
5332                                   struct ceph_msg_header *hdr,
5333                                   int *skip)
5334 {
5335         struct ceph_osd *osd = con->private;
5336         int type = le16_to_cpu(hdr->type);
5337
5338         *skip = 0;
5339         switch (type) {
5340         case CEPH_MSG_OSD_MAP:
5341         case CEPH_MSG_OSD_BACKOFF:
5342         case CEPH_MSG_WATCH_NOTIFY:
5343                 return alloc_msg_with_page_vector(hdr);
5344         case CEPH_MSG_OSD_OPREPLY:
5345                 return get_reply(con, hdr, skip);
5346         default:
5347                 pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
5348                         osd->o_osd, type);
5349                 *skip = 1;
5350                 return NULL;
5351         }
5352 }
5353
5354 /*
5355  * Wrappers to refcount containing ceph_osd struct
5356  */
5357 static struct ceph_connection *get_osd_con(struct ceph_connection *con)
5358 {
5359         struct ceph_osd *osd = con->private;
5360         if (get_osd(osd))
5361                 return con;
5362         return NULL;
5363 }
5364
5365 static void put_osd_con(struct ceph_connection *con)
5366 {
5367         struct ceph_osd *osd = con->private;
5368         put_osd(osd);
5369 }
5370
5371 /*
5372  * authentication
5373  */
5374 /*
5375  * Note: returned pointer is the address of a structure that's
5376  * managed separately.  Caller must *not* attempt to free it.
5377  */
5378 static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
5379                                         int *proto, int force_new)
5380 {
5381         struct ceph_osd *o = con->private;
5382         struct ceph_osd_client *osdc = o->o_osdc;
5383         struct ceph_auth_client *ac = osdc->client->monc.auth;
5384         struct ceph_auth_handshake *auth = &o->o_auth;
5385
5386         if (force_new && auth->authorizer) {
5387                 ceph_auth_destroy_authorizer(auth->authorizer);
5388                 auth->authorizer = NULL;
5389         }
5390         if (!auth->authorizer) {
5391                 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5392                                                       auth);
5393                 if (ret)
5394                         return ERR_PTR(ret);
5395         } else {
5396                 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
5397                                                      auth);
5398                 if (ret)
5399                         return ERR_PTR(ret);
5400         }
5401         *proto = ac->protocol;
5402
5403         return auth;
5404 }
5405
5406
5407 static int verify_authorizer_reply(struct ceph_connection *con)
5408 {
5409         struct ceph_osd *o = con->private;
5410         struct ceph_osd_client *osdc = o->o_osdc;
5411         struct ceph_auth_client *ac = osdc->client->monc.auth;
5412
5413         return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer);
5414 }
5415
5416 static int invalidate_authorizer(struct ceph_connection *con)
5417 {
5418         struct ceph_osd *o = con->private;
5419         struct ceph_osd_client *osdc = o->o_osdc;
5420         struct ceph_auth_client *ac = osdc->client->monc.auth;
5421
5422         ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
5423         return ceph_monc_validate_auth(&osdc->client->monc);
5424 }
5425
5426 static void osd_reencode_message(struct ceph_msg *msg)
5427 {
5428         int type = le16_to_cpu(msg->hdr.type);
5429
5430         if (type == CEPH_MSG_OSD_OP)
5431                 encode_request_finish(msg);
5432 }
5433
5434 static int osd_sign_message(struct ceph_msg *msg)
5435 {
5436         struct ceph_osd *o = msg->con->private;
5437         struct ceph_auth_handshake *auth = &o->o_auth;
5438
5439         return ceph_auth_sign_message(auth, msg);
5440 }
5441
5442 static int osd_check_message_signature(struct ceph_msg *msg)
5443 {
5444         struct ceph_osd *o = msg->con->private;
5445         struct ceph_auth_handshake *auth = &o->o_auth;
5446
5447         return ceph_auth_check_message_signature(auth, msg);
5448 }
5449
5450 static const struct ceph_connection_operations osd_con_ops = {
5451         .get = get_osd_con,
5452         .put = put_osd_con,
5453         .dispatch = dispatch,
5454         .get_authorizer = get_authorizer,
5455         .verify_authorizer_reply = verify_authorizer_reply,
5456         .invalidate_authorizer = invalidate_authorizer,
5457         .alloc_msg = alloc_msg,
5458         .reencode_message = osd_reencode_message,
5459         .sign_message = osd_sign_message,
5460         .check_message_signature = osd_check_message_signature,
5461         .fault = osd_fault,
5462 };